^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* AFS cell alias detection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2020 Red Hat, Inc. All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Written by David Howells (dhowells@redhat.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <keys/rxrpc-type.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * Sample a volume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) static struct afs_volume *afs_sample_volume(struct afs_cell *cell, struct key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) const char *name, unsigned int namelen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct afs_volume *volume;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct afs_fs_context fc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) .type = 0, /* Explicitly leave it to the VLDB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) .volnamesz = namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) .volname = name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) .net = cell->net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) .cell = cell,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) .key = key, /* This might need to be something */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) volume = afs_create_volume(&fc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) _leave(" = %p", volume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) return volume;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * Compare two addresses.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static int afs_compare_addrs(const struct sockaddr_rxrpc *srx_a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) const struct sockaddr_rxrpc *srx_b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) short port_a, port_b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int addr_a, addr_b, diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) diff = (short)srx_a->transport_type - (short)srx_b->transport_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (diff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) switch (srx_a->transport_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) case AF_INET: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) const struct sockaddr_in *a = &srx_a->transport.sin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) const struct sockaddr_in *b = &srx_b->transport.sin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) addr_a = ntohl(a->sin_addr.s_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) addr_b = ntohl(b->sin_addr.s_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) diff = addr_a - addr_b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (diff == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) port_a = ntohs(a->sin_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) port_b = ntohs(b->sin_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) diff = port_a - port_b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) case AF_INET6: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) const struct sockaddr_in6 *a = &srx_a->transport.sin6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) const struct sockaddr_in6 *b = &srx_b->transport.sin6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) diff = memcmp(&a->sin6_addr, &b->sin6_addr, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (diff == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) port_a = ntohs(a->sin6_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) port_b = ntohs(b->sin6_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) diff = port_a - port_b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) diff = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * Compare the address lists of a pair of fileservers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static int afs_compare_fs_alists(const struct afs_server *server_a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) const struct afs_server *server_b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) const struct afs_addr_list *la, *lb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int a = 0, b = 0, addr_matches = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) la = rcu_dereference(server_a->addresses);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) lb = rcu_dereference(server_b->addresses);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) while (a < la->nr_addrs && b < lb->nr_addrs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) const struct sockaddr_rxrpc *srx_a = &la->addrs[a];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) const struct sockaddr_rxrpc *srx_b = &lb->addrs[b];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) int diff = afs_compare_addrs(srx_a, srx_b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (diff < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) a++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) } else if (diff > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) b++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) addr_matches++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) a++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) b++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return addr_matches;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * Compare the fileserver lists of two volumes. The server lists are sorted in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * order of ascending UUID.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static int afs_compare_volume_slists(const struct afs_volume *vol_a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) const struct afs_volume *vol_b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) const struct afs_server_list *la, *lb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) int i, a = 0, b = 0, uuid_matches = 0, addr_matches = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) la = rcu_dereference(vol_a->servers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) lb = rcu_dereference(vol_b->servers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) for (i = 0; i < AFS_MAXTYPES; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (la->vids[i] != lb->vids[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) while (a < la->nr_servers && b < lb->nr_servers) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) const struct afs_server *server_a = la->servers[a].server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) const struct afs_server *server_b = lb->servers[b].server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) int diff = memcmp(&server_a->uuid, &server_b->uuid, sizeof(uuid_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (diff < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) a++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) } else if (diff > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) b++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) uuid_matches++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) addr_matches += afs_compare_fs_alists(server_a, server_b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) a++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) b++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) _leave(" = %d [um %d]", addr_matches, uuid_matches);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return addr_matches;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * Compare root.cell volumes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static int afs_compare_cell_roots(struct afs_cell *cell)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct afs_cell *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) _enter("");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) hlist_for_each_entry_rcu(p, &cell->net->proc_cells, proc_link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (p == cell || p->alias_of)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (!p->root_volume)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) continue; /* Ignore cells that don't have a root.cell volume. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (afs_compare_volume_slists(cell->root_volume, p->root_volume) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) goto is_alias;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) _leave(" = 0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) is_alias:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) cell->alias_of = afs_use_cell(p, afs_cell_trace_use_alias);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * Query the new cell for a volume from a cell we're already using.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static int afs_query_for_alias_one(struct afs_cell *cell, struct key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) struct afs_cell *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) struct afs_volume *volume, *pvol = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) /* Arbitrarily pick a volume from the list. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) read_seqlock_excl(&p->volume_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (!RB_EMPTY_ROOT(&p->volumes))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) pvol = afs_get_volume(rb_entry(p->volumes.rb_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) struct afs_volume, cell_node),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) afs_volume_trace_get_query_alias);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) read_sequnlock_excl(&p->volume_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (!pvol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) _enter("%s:%s", cell->name, pvol->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) /* And see if it's in the new cell. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) volume = afs_sample_volume(cell, key, pvol->name, pvol->name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (IS_ERR(volume)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) afs_put_volume(cell->net, pvol, afs_volume_trace_put_query_alias);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (PTR_ERR(volume) != -ENOMEDIUM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return PTR_ERR(volume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) /* That volume is not in the new cell, so not an alias */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) /* The new cell has a like-named volume also - compare volume ID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * server and address lists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (pvol->vid == volume->vid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (afs_compare_volume_slists(volume, pvol))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) afs_put_volume(cell->net, volume, afs_volume_trace_put_query_alias);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) afs_put_volume(cell->net, pvol, afs_volume_trace_put_query_alias);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * Query the new cell for volumes we know exist in cells we're already using.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) static int afs_query_for_alias(struct afs_cell *cell, struct key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) struct afs_cell *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) _enter("%s", cell->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (mutex_lock_interruptible(&cell->net->proc_cells_lock) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) hlist_for_each_entry(p, &cell->net->proc_cells, proc_link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (p == cell || p->alias_of)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (RB_EMPTY_ROOT(&p->volumes))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (p->root_volume)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) continue; /* Ignore cells that have a root.cell volume. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) afs_use_cell(p, afs_cell_trace_use_check_alias);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) mutex_unlock(&cell->net->proc_cells_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) if (afs_query_for_alias_one(cell, key, p) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) goto is_alias;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (mutex_lock_interruptible(&cell->net->proc_cells_lock) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) afs_unuse_cell(cell->net, p, afs_cell_trace_unuse_check_alias);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) afs_unuse_cell(cell->net, p, afs_cell_trace_unuse_check_alias);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) mutex_unlock(&cell->net->proc_cells_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) _leave(" = 0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) is_alias:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) cell->alias_of = p; /* Transfer our ref */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) * Look up a VLDB record for a volume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) static char *afs_vl_get_cell_name(struct afs_cell *cell, struct key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) struct afs_vl_cursor vc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) char *cell_name = ERR_PTR(-EDESTADDRREQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) bool skipped = false, not_skipped = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (!afs_begin_vlserver_operation(&vc, cell, key))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) return ERR_PTR(-ERESTARTSYS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) while (afs_select_vlserver(&vc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if (!test_bit(AFS_VLSERVER_FL_IS_YFS, &vc.server->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) vc.ac.error = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) skipped = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) not_skipped = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) cell_name = afs_yfsvl_get_cell_name(&vc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) ret = afs_end_vlserver_operation(&vc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (skipped && !not_skipped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) ret = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) return ret < 0 ? ERR_PTR(ret) : cell_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static int yfs_check_canonical_cell_name(struct afs_cell *cell, struct key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) struct afs_cell *master;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) char *cell_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) cell_name = afs_vl_get_cell_name(cell, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (IS_ERR(cell_name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) return PTR_ERR(cell_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (strcmp(cell_name, cell->name) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) kfree(cell_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) master = afs_lookup_cell(cell->net, cell_name, strlen(cell_name),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) NULL, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) kfree(cell_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (IS_ERR(master))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) return PTR_ERR(master);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) cell->alias_of = master; /* Transfer our ref */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) static int afs_do_cell_detect_alias(struct afs_cell *cell, struct key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) struct afs_volume *root_volume;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) _enter("%s", cell->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) ret = yfs_check_canonical_cell_name(cell, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (ret != -EOPNOTSUPP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) /* Try and get the root.cell volume for comparison with other cells */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) root_volume = afs_sample_volume(cell, key, "root.cell", 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) if (!IS_ERR(root_volume)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) cell->root_volume = root_volume;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return afs_compare_cell_roots(cell);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (PTR_ERR(root_volume) != -ENOMEDIUM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) return PTR_ERR(root_volume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) /* Okay, this cell doesn't have an root.cell volume. We need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) * locate some other random volume and use that to check.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) return afs_query_for_alias(cell, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * Check to see if a new cell is an alias of a cell we already have. At this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) * point we have the cell's volume server list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) * Returns 0 if we didn't detect an alias, 1 if we found an alias and an error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) * if we had problems gathering the data required. In the case the we did
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * detect an alias, cell->alias_of is set to point to the assumed master.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) int afs_cell_detect_alias(struct afs_cell *cell, struct key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) struct afs_net *net = cell->net;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (mutex_lock_interruptible(&net->cells_alias_lock) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) if (test_bit(AFS_CELL_FL_CHECK_ALIAS, &cell->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) ret = afs_do_cell_detect_alias(cell, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) clear_bit_unlock(AFS_CELL_FL_CHECK_ALIAS, &cell->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) ret = cell->alias_of ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) mutex_unlock(&net->cells_alias_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (ret == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) pr_notice("kAFS: Cell %s is an alias of %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) cell->name, cell->alias_of->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }