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