]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/afs/cell.c
Merge tag 'arm64-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux
[mirror_ubuntu-jammy-kernel.git] / fs / afs / cell.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
ec26815a 2/* AFS cell and server record management
1da177e4 3 *
989782dc 4 * Copyright (C) 2002, 2017 Red Hat, Inc. All Rights Reserved.
1da177e4 5 * Written by David Howells (dhowells@redhat.com)
1da177e4
LT
6 */
7
1da177e4 8#include <linux/slab.h>
00d3b7a4
DH
9#include <linux/key.h>
10#include <linux/ctype.h>
07567a55 11#include <linux/dns_resolver.h>
e8edc6e0 12#include <linux/sched.h>
3838d3ec 13#include <linux/inet.h>
0da0b7fd 14#include <linux/namei.h>
00d3b7a4 15#include <keys/rxrpc-type.h>
1da177e4
LT
16#include "internal.h"
17
fe342cf7 18static unsigned __read_mostly afs_cell_gc_delay = 10;
ded2f4c5
DH
19static unsigned __read_mostly afs_cell_min_ttl = 10 * 60;
20static unsigned __read_mostly afs_cell_max_ttl = 24 * 60 * 60;
989782dc
DH
21
22static void afs_manage_cell(struct work_struct *);
23
24static void afs_dec_cells_outstanding(struct afs_net *net)
25{
26 if (atomic_dec_and_test(&net->cells_outstanding))
ab1fbe32 27 wake_up_var(&net->cells_outstanding);
989782dc
DH
28}
29
1da177e4 30/*
989782dc
DH
31 * Set the cell timer to fire after a given delay, assuming it's not already
32 * set for an earlier time.
1da177e4 33 */
989782dc 34static void afs_set_cell_timer(struct afs_net *net, time64_t delay)
1da177e4 35{
989782dc
DH
36 if (net->live) {
37 atomic_inc(&net->cells_outstanding);
38 if (timer_reduce(&net->cells_timer, jiffies + delay * HZ))
39 afs_dec_cells_outstanding(net);
40 }
41}
42
43/*
44 * Look up and get an activation reference on a cell record under RCU
45 * conditions. The caller must hold the RCU read lock.
46 */
47struct afs_cell *afs_lookup_cell_rcu(struct afs_net *net,
48 const char *name, unsigned int namesz)
49{
50 struct afs_cell *cell = NULL;
51 struct rb_node *p;
52 int n, seq = 0, ret = 0;
53
54 _enter("%*.*s", namesz, namesz, name);
55
56 if (name && namesz == 0)
57 return ERR_PTR(-EINVAL);
58 if (namesz > AFS_MAXCELLNAME)
59 return ERR_PTR(-ENAMETOOLONG);
60
61 do {
62 /* Unfortunately, rbtree walking doesn't give reliable results
63 * under just the RCU read lock, so we have to check for
64 * changes.
65 */
66 if (cell)
67 afs_put_cell(net, cell);
68 cell = NULL;
69 ret = -ENOENT;
70
71 read_seqbegin_or_lock(&net->cells_lock, &seq);
72
73 if (!name) {
74 cell = rcu_dereference_raw(net->ws_cell);
75 if (cell) {
76 afs_get_cell(cell);
a5fb8e6c 77 ret = 0;
fe342cf7 78 break;
989782dc
DH
79 }
80 ret = -EDESTADDRREQ;
81 continue;
82 }
83
84 p = rcu_dereference_raw(net->cells.rb_node);
85 while (p) {
86 cell = rb_entry(p, struct afs_cell, net_node);
87
88 n = strncasecmp(cell->name, name,
89 min_t(size_t, cell->name_len, namesz));
90 if (n == 0)
91 n = cell->name_len - namesz;
92 if (n < 0) {
93 p = rcu_dereference_raw(p->rb_left);
94 } else if (n > 0) {
95 p = rcu_dereference_raw(p->rb_right);
96 } else {
97 if (atomic_inc_not_zero(&cell->usage)) {
98 ret = 0;
99 break;
100 }
101 /* We want to repeat the search, this time with
102 * the lock properly locked.
103 */
104 }
105 cell = NULL;
106 }
1da177e4 107
989782dc 108 } while (need_seqretry(&net->cells_lock, seq));
1da177e4 109
989782dc 110 done_seqretry(&net->cells_lock, seq);
1da177e4 111
a5fb8e6c
DH
112 if (ret != 0 && cell)
113 afs_put_cell(net, cell);
114
989782dc
DH
115 return ret == 0 ? cell : ERR_PTR(ret);
116}
117
118/*
119 * Set up a cell record and fill in its name, VL server address list and
120 * allocate an anonymous key
121 */
122static struct afs_cell *afs_alloc_cell(struct afs_net *net,
123 const char *name, unsigned int namelen,
0a5143f2 124 const char *addresses)
989782dc 125{
ca1cbbdc 126 struct afs_vlserver_list *vllist;
989782dc
DH
127 struct afs_cell *cell;
128 int i, ret;
129
130 ASSERT(name);
131 if (namelen == 0)
132 return ERR_PTR(-EINVAL);
07567a55
WL
133 if (namelen > AFS_MAXCELLNAME) {
134 _leave(" = -ENAMETOOLONG");
00d3b7a4 135 return ERR_PTR(-ENAMETOOLONG);
07567a55 136 }
a45ea48e
DH
137
138 /* Prohibit cell names that contain unprintable chars, '/' and '@' or
139 * that begin with a dot. This also precludes "@cell".
140 */
141 if (name[0] == '.')
37ab6368 142 return ERR_PTR(-EINVAL);
a45ea48e
DH
143 for (i = 0; i < namelen; i++) {
144 char ch = name[i];
145 if (!isprint(ch) || ch == '/' || ch == '@')
146 return ERR_PTR(-EINVAL);
147 }
00d3b7a4 148
0a5143f2 149 _enter("%*.*s,%s", namelen, namelen, name, addresses);
989782dc
DH
150
151 cell = kzalloc(sizeof(struct afs_cell), GFP_KERNEL);
1da177e4
LT
152 if (!cell) {
153 _leave(" = -ENOMEM");
08e0e7c8 154 return ERR_PTR(-ENOMEM);
1da177e4
LT
155 }
156
f044c884 157 cell->net = net;
989782dc
DH
158 cell->name_len = namelen;
159 for (i = 0; i < namelen; i++)
160 cell->name[i] = tolower(name[i]);
161
162 atomic_set(&cell->usage, 2);
163 INIT_WORK(&cell->manager, afs_manage_cell);
d2ddc776
DH
164 INIT_LIST_HEAD(&cell->proc_volumes);
165 rwlock_init(&cell->proc_lock);
0a5143f2 166 rwlock_init(&cell->vl_servers_lock);
4d9df986 167
ca1cbbdc
DH
168 /* Provide a VL server list, filling it in if we were given a list of
169 * addresses to use.
989782dc 170 */
0a5143f2 171 if (addresses) {
0a5143f2
DH
172 vllist = afs_parse_text_addrs(net,
173 addresses, strlen(addresses), ':',
174 VL_SERVICE, AFS_VL_PORT);
175 if (IS_ERR(vllist)) {
176 ret = PTR_ERR(vllist);
8b2a464c
DH
177 goto parse_failed;
178 }
00d3b7a4 179
d5c32c89
DH
180 vllist->source = DNS_RECORD_FROM_CONFIG;
181 vllist->status = DNS_LOOKUP_NOT_DONE;
989782dc 182 cell->dns_expiry = TIME64_MAX;
ded2f4c5 183 } else {
ca1cbbdc
DH
184 ret = -ENOMEM;
185 vllist = afs_alloc_vlserver_list(0);
186 if (!vllist)
187 goto error;
d5c32c89
DH
188 vllist->source = DNS_RECORD_UNAVAILABLE;
189 vllist->status = DNS_LOOKUP_NOT_DONE;
ded2f4c5 190 cell->dns_expiry = ktime_get_real_seconds();
00d3b7a4 191 }
00d3b7a4 192
ca1cbbdc
DH
193 rcu_assign_pointer(cell->vl_servers, vllist);
194
d5c32c89
DH
195 cell->dns_source = vllist->source;
196 cell->dns_status = vllist->status;
197 smp_store_release(&cell->dns_lookup_count, 1); /* vs source/status */
198
00d3b7a4
DH
199 _leave(" = %p", cell);
200 return cell;
201
8b2a464c
DH
202parse_failed:
203 if (ret == -EINVAL)
204 printk(KERN_ERR "kAFS: bad VL server IP address\n");
ca1cbbdc 205error:
00d3b7a4
DH
206 kfree(cell);
207 _leave(" = %d", ret);
208 return ERR_PTR(ret);
209}
1da177e4 210
00d3b7a4 211/*
989782dc 212 * afs_lookup_cell - Look up or create a cell record.
f044c884 213 * @net: The network namespace
989782dc
DH
214 * @name: The name of the cell.
215 * @namesz: The strlen of the cell name.
216 * @vllist: A colon/comma separated list of numeric IP addresses or NULL.
217 * @excl: T if an error should be given if the cell name already exists.
218 *
219 * Look up a cell record by name and query the DNS for VL server addresses if
220 * needed. Note that that actual DNS query is punted off to the manager thread
221 * so that this function can return immediately if interrupted whilst allowing
222 * cell records to be shared even if not yet fully constructed.
00d3b7a4 223 */
989782dc
DH
224struct afs_cell *afs_lookup_cell(struct afs_net *net,
225 const char *name, unsigned int namesz,
226 const char *vllist, bool excl)
00d3b7a4 227{
989782dc
DH
228 struct afs_cell *cell, *candidate, *cursor;
229 struct rb_node *parent, **pp;
d5c32c89 230 enum afs_cell_state state;
989782dc
DH
231 int ret, n;
232
233 _enter("%s,%s", name, vllist);
234
235 if (!excl) {
236 rcu_read_lock();
237 cell = afs_lookup_cell_rcu(net, name, namesz);
238 rcu_read_unlock();
68327951 239 if (!IS_ERR(cell))
989782dc 240 goto wait_for_cell;
989782dc 241 }
00d3b7a4 242
989782dc
DH
243 /* Assume we're probably going to create a cell and preallocate and
244 * mostly set up a candidate record. We can then use this to stash the
245 * name, the net namespace and VL server addresses.
246 *
247 * We also want to do this before we hold any locks as it may involve
248 * upcalling to userspace to make DNS queries.
249 */
250 candidate = afs_alloc_cell(net, name, namesz, vllist);
251 if (IS_ERR(candidate)) {
252 _leave(" = %ld", PTR_ERR(candidate));
253 return candidate;
5214b729 254 }
5214b729 255
989782dc
DH
256 /* Find the insertion point and check to see if someone else added a
257 * cell whilst we were allocating.
258 */
259 write_seqlock(&net->cells_lock);
260
261 pp = &net->cells.rb_node;
262 parent = NULL;
263 while (*pp) {
264 parent = *pp;
265 cursor = rb_entry(parent, struct afs_cell, net_node);
266
267 n = strncasecmp(cursor->name, name,
268 min_t(size_t, cursor->name_len, namesz));
269 if (n == 0)
270 n = cursor->name_len - namesz;
271 if (n < 0)
272 pp = &(*pp)->rb_left;
273 else if (n > 0)
274 pp = &(*pp)->rb_right;
275 else
276 goto cell_already_exists;
00d3b7a4
DH
277 }
278
989782dc
DH
279 cell = candidate;
280 candidate = NULL;
281 rb_link_node_rcu(&cell->net_node, parent, pp);
282 rb_insert_color(&cell->net_node, &net->cells);
283 atomic_inc(&net->cells_outstanding);
284 write_sequnlock(&net->cells_lock);
1da177e4 285
989782dc 286 queue_work(afs_wq, &cell->manager);
1da177e4 287
989782dc
DH
288wait_for_cell:
289 _debug("wait_for_cell");
d5c32c89
DH
290 wait_var_event(&cell->state,
291 ({
292 state = smp_load_acquire(&cell->state); /* vs error */
293 state == AFS_CELL_ACTIVE || state == AFS_CELL_FAILED;
294 }));
295
296 /* Check the state obtained from the wait check. */
297 if (state == AFS_CELL_FAILED) {
989782dc
DH
298 ret = cell->error;
299 goto error;
989782dc 300 }
1da177e4 301
989782dc 302 _leave(" = %p [cell]", cell);
08e0e7c8 303 return cell;
1da177e4 304
989782dc
DH
305cell_already_exists:
306 _debug("cell exists");
307 cell = cursor;
308 if (excl) {
309 ret = -EEXIST;
310 } else {
989782dc
DH
311 afs_get_cell(cursor);
312 ret = 0;
313 }
314 write_sequnlock(&net->cells_lock);
315 kfree(candidate);
316 if (ret == 0)
317 goto wait_for_cell;
8b2a464c 318 goto error_noput;
ec26815a 319error:
989782dc 320 afs_put_cell(net, cell);
8b2a464c 321error_noput:
989782dc 322 _leave(" = %d [error]", ret);
08e0e7c8 323 return ERR_PTR(ret);
ec26815a 324}
1da177e4 325
1da177e4 326/*
08e0e7c8
DH
327 * set the root cell information
328 * - can be called with a module parameter string
329 * - can be called from a write to /proc/fs/afs/rootcell
1da177e4 330 */
989782dc 331int afs_cell_init(struct afs_net *net, const char *rootcell)
1da177e4
LT
332{
333 struct afs_cell *old_root, *new_root;
989782dc
DH
334 const char *cp, *vllist;
335 size_t len;
1da177e4
LT
336
337 _enter("");
338
339 if (!rootcell) {
340 /* module is loaded with no parameters, or built statically.
341 * - in the future we might initialize cell DB here.
342 */
08e0e7c8 343 _leave(" = 0 [no root]");
1da177e4
LT
344 return 0;
345 }
346
347 cp = strchr(rootcell, ':');
989782dc 348 if (!cp) {
07567a55 349 _debug("kAFS: no VL server IP addresses specified");
989782dc
DH
350 vllist = NULL;
351 len = strlen(rootcell);
352 } else {
353 vllist = cp + 1;
354 len = cp - rootcell;
355 }
1da177e4
LT
356
357 /* allocate a cell record for the root cell */
989782dc 358 new_root = afs_lookup_cell(net, rootcell, len, vllist, false);
08e0e7c8
DH
359 if (IS_ERR(new_root)) {
360 _leave(" = %ld", PTR_ERR(new_root));
361 return PTR_ERR(new_root);
1da177e4
LT
362 }
363
17814aef
DH
364 if (!test_and_set_bit(AFS_CELL_FL_NO_GC, &new_root->flags))
365 afs_get_cell(new_root);
989782dc 366
08e0e7c8 367 /* install the new cell */
989782dc 368 write_seqlock(&net->cells_lock);
1588def9
DH
369 old_root = rcu_access_pointer(net->ws_cell);
370 rcu_assign_pointer(net->ws_cell, new_root);
989782dc 371 write_sequnlock(&net->cells_lock);
1da177e4 372
989782dc 373 afs_put_cell(net, old_root);
08e0e7c8
DH
374 _leave(" = 0");
375 return 0;
ec26815a 376}
1da177e4 377
1da177e4 378/*
989782dc 379 * Update a cell's VL server address list from the DNS.
1da177e4 380 */
d5c32c89 381static int afs_update_cell(struct afs_cell *cell)
1da177e4 382{
d5c32c89 383 struct afs_vlserver_list *vllist, *old = NULL, *p;
ded2f4c5
DH
384 unsigned int min_ttl = READ_ONCE(afs_cell_min_ttl);
385 unsigned int max_ttl = READ_ONCE(afs_cell_max_ttl);
386 time64_t now, expiry = 0;
d5c32c89 387 int ret = 0;
1da177e4 388
989782dc
DH
389 _enter("%s", cell->name);
390
0a5143f2 391 vllist = afs_dns_query(cell, &expiry);
d5c32c89
DH
392 if (IS_ERR(vllist)) {
393 ret = PTR_ERR(vllist);
394
395 _debug("%s: fail %d", cell->name, ret);
396 if (ret == -ENOMEM)
397 goto out_wake;
398
399 ret = -ENOMEM;
400 vllist = afs_alloc_vlserver_list(0);
401 if (!vllist)
402 goto out_wake;
403
404 switch (ret) {
405 case -ENODATA:
406 case -EDESTADDRREQ:
407 vllist->status = DNS_LOOKUP_GOT_NOT_FOUND;
408 break;
409 case -EAGAIN:
410 case -ECONNREFUSED:
411 vllist->status = DNS_LOOKUP_GOT_TEMP_FAILURE;
412 break;
413 default:
414 vllist->status = DNS_LOOKUP_GOT_LOCAL_FAILURE;
415 break;
416 }
417 }
418
419 _debug("%s: got list %d %d", cell->name, vllist->source, vllist->status);
420 cell->dns_status = vllist->status;
ded2f4c5
DH
421
422 now = ktime_get_real_seconds();
423 if (min_ttl > max_ttl)
424 max_ttl = min_ttl;
425 if (expiry < now + min_ttl)
426 expiry = now + min_ttl;
427 else if (expiry > now + max_ttl)
428 expiry = now + max_ttl;
429
d5c32c89
DH
430 _debug("%s: status %d", cell->name, vllist->status);
431 if (vllist->source == DNS_RECORD_UNAVAILABLE) {
432 switch (vllist->status) {
433 case DNS_LOOKUP_GOT_NOT_FOUND:
ded2f4c5
DH
434 /* The DNS said that the cell does not exist or there
435 * weren't any addresses to be had.
436 */
ded2f4c5 437 cell->dns_expiry = expiry;
8b2a464c 438 break;
989782dc 439
d5c32c89
DH
440 case DNS_LOOKUP_BAD:
441 case DNS_LOOKUP_GOT_LOCAL_FAILURE:
442 case DNS_LOOKUP_GOT_TEMP_FAILURE:
443 case DNS_LOOKUP_GOT_NS_FAILURE:
8b2a464c 444 default:
ded2f4c5 445 cell->dns_expiry = now + 10;
8b2a464c
DH
446 break;
447 }
8b2a464c 448 } else {
8b2a464c 449 cell->dns_expiry = expiry;
8b2a464c 450 }
bec5eb61 451
d5c32c89
DH
452 /* Replace the VL server list if the new record has servers or the old
453 * record doesn't.
454 */
455 write_lock(&cell->vl_servers_lock);
456 p = rcu_dereference_protected(cell->vl_servers, true);
457 if (vllist->nr_servers > 0 || p->nr_servers == 0) {
458 rcu_assign_pointer(cell->vl_servers, vllist);
459 cell->dns_source = vllist->source;
460 old = p;
461 }
462 write_unlock(&cell->vl_servers_lock);
463 afs_put_vlserverlist(cell->net, old);
bec5eb61 464
d5c32c89
DH
465out_wake:
466 smp_store_release(&cell->dns_lookup_count,
467 cell->dns_lookup_count + 1); /* vs source/status */
468 wake_up_var(&cell->dns_lookup_count);
469 _leave(" = %d", ret);
470 return ret;
ec26815a 471}
1da177e4 472
1da177e4 473/*
989782dc 474 * Destroy a cell record
1da177e4 475 */
989782dc 476static void afs_cell_destroy(struct rcu_head *rcu)
1da177e4 477{
989782dc 478 struct afs_cell *cell = container_of(rcu, struct afs_cell, rcu);
1da177e4 479
989782dc 480 _enter("%p{%s}", cell, cell->name);
1da177e4 481
989782dc
DH
482 ASSERTCMP(atomic_read(&cell->usage), ==, 0);
483
0a5143f2 484 afs_put_vlserverlist(cell->net, rcu_access_pointer(cell->vl_servers));
989782dc
DH
485 key_put(cell->anonymous_key);
486 kfree(cell);
487
488 _leave(" [destroyed]");
ec26815a 489}
1da177e4 490
1da177e4 491/*
989782dc 492 * Queue the cell manager.
1da177e4 493 */
989782dc 494static void afs_queue_cell_manager(struct afs_net *net)
1da177e4 495{
989782dc 496 int outstanding = atomic_inc_return(&net->cells_outstanding);
1da177e4 497
989782dc 498 _enter("%d", outstanding);
1da177e4 499
989782dc
DH
500 if (!queue_work(afs_wq, &net->cells_manager))
501 afs_dec_cells_outstanding(net);
502}
503
504/*
505 * Cell management timer. We have an increment on cells_outstanding that we
506 * need to pass along to the work item.
507 */
508void afs_cells_timer(struct timer_list *timer)
509{
510 struct afs_net *net = container_of(timer, struct afs_net, cells_timer);
511
512 _enter("");
513 if (!queue_work(afs_wq, &net->cells_manager))
514 afs_dec_cells_outstanding(net);
515}
1da177e4 516
8b2a464c
DH
517/*
518 * Get a reference on a cell record.
519 */
520struct afs_cell *afs_get_cell(struct afs_cell *cell)
521{
522 atomic_inc(&cell->usage);
523 return cell;
524}
525
989782dc
DH
526/*
527 * Drop a reference on a cell record.
528 */
529void afs_put_cell(struct afs_net *net, struct afs_cell *cell)
530{
531 time64_t now, expire_delay;
1da177e4 532
989782dc 533 if (!cell)
1da177e4 534 return;
1da177e4 535
989782dc 536 _enter("%s", cell->name);
08e0e7c8 537
989782dc
DH
538 now = ktime_get_real_seconds();
539 cell->last_inactive = now;
540 expire_delay = 0;
d5c32c89 541 if (cell->vl_servers->nr_servers)
989782dc 542 expire_delay = afs_cell_gc_delay;
1da177e4 543
989782dc
DH
544 if (atomic_dec_return(&cell->usage) > 1)
545 return;
1da177e4 546
989782dc
DH
547 /* 'cell' may now be garbage collected. */
548 afs_set_cell_timer(net, expire_delay);
ec26815a 549}
1da177e4 550
1da177e4 551/*
989782dc 552 * Allocate a key to use as a placeholder for anonymous user security.
1da177e4 553 */
989782dc 554static int afs_alloc_anon_key(struct afs_cell *cell)
1da177e4 555{
989782dc
DH
556 struct key *key;
557 char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp;
1da177e4 558
989782dc
DH
559 /* Create a key to represent an anonymous user. */
560 memcpy(keyname, "afs@", 4);
561 dp = keyname + 4;
562 cp = cell->name;
563 do {
564 *dp++ = tolower(*cp);
565 } while (*cp++);
1da177e4 566
989782dc
DH
567 key = rxrpc_get_null_key(keyname);
568 if (IS_ERR(key))
569 return PTR_ERR(key);
1da177e4 570
989782dc 571 cell->anonymous_key = key;
1da177e4 572
989782dc
DH
573 _debug("anon key %p{%x}",
574 cell->anonymous_key, key_serial(cell->anonymous_key));
575 return 0;
576}
1da177e4 577
989782dc
DH
578/*
579 * Activate a cell.
580 */
581static int afs_activate_cell(struct afs_net *net, struct afs_cell *cell)
582{
6b3944e4
DH
583 struct hlist_node **p;
584 struct afs_cell *pcell;
989782dc
DH
585 int ret;
586
587 if (!cell->anonymous_key) {
588 ret = afs_alloc_anon_key(cell);
589 if (ret < 0)
590 return ret;
08e0e7c8
DH
591 }
592
989782dc
DH
593#ifdef CONFIG_AFS_FSCACHE
594 cell->cache = fscache_acquire_cookie(afs_cache_netfs.primary_index,
595 &afs_cell_cache_index_def,
402cb8dd
DH
596 cell->name, strlen(cell->name),
597 NULL, 0,
ee1235a9 598 cell, 0, true);
989782dc 599#endif
5b86d4ff 600 ret = afs_proc_cell_setup(cell);
989782dc
DH
601 if (ret < 0)
602 return ret;
0da0b7fd
DH
603
604 mutex_lock(&net->proc_cells_lock);
6b3944e4
DH
605 for (p = &net->proc_cells.first; *p; p = &(*p)->next) {
606 pcell = hlist_entry(*p, struct afs_cell, proc_link);
607 if (strcmp(cell->name, pcell->name) < 0)
608 break;
609 }
610
611 cell->proc_link.pprev = p;
612 cell->proc_link.next = *p;
613 rcu_assign_pointer(*p, &cell->proc_link.next);
614 if (cell->proc_link.next)
615 cell->proc_link.next->pprev = &cell->proc_link.next;
616
0da0b7fd
DH
617 afs_dynroot_mkdir(net, cell);
618 mutex_unlock(&net->proc_cells_lock);
989782dc
DH
619 return 0;
620}
621
622/*
623 * Deactivate a cell.
624 */
625static void afs_deactivate_cell(struct afs_net *net, struct afs_cell *cell)
626{
627 _enter("%s", cell->name);
1da177e4 628
5b86d4ff 629 afs_proc_cell_remove(cell);
1da177e4 630
0da0b7fd 631 mutex_lock(&net->proc_cells_lock);
6b3944e4 632 hlist_del_rcu(&cell->proc_link);
0da0b7fd
DH
633 afs_dynroot_rmdir(net, cell);
634 mutex_unlock(&net->proc_cells_lock);
1da177e4 635
9b3f26c9 636#ifdef CONFIG_AFS_FSCACHE
402cb8dd 637 fscache_relinquish_cookie(cell->cache, NULL, false);
989782dc 638 cell->cache = NULL;
1da177e4 639#endif
1da177e4 640
989782dc 641 _leave("");
ec26815a 642}
1da177e4 643
1da177e4 644/*
989782dc
DH
645 * Manage a cell record, initialising and destroying it, maintaining its DNS
646 * records.
1da177e4 647 */
989782dc 648static void afs_manage_cell(struct work_struct *work)
1da177e4 649{
989782dc
DH
650 struct afs_cell *cell = container_of(work, struct afs_cell, manager);
651 struct afs_net *net = cell->net;
652 bool deleted;
653 int ret, usage;
654
655 _enter("%s", cell->name);
656
657again:
658 _debug("state %u", cell->state);
659 switch (cell->state) {
660 case AFS_CELL_INACTIVE:
661 case AFS_CELL_FAILED:
662 write_seqlock(&net->cells_lock);
663 usage = 1;
664 deleted = atomic_try_cmpxchg_relaxed(&cell->usage, &usage, 0);
665 if (deleted)
666 rb_erase(&cell->net_node, &net->cells);
667 write_sequnlock(&net->cells_lock);
668 if (deleted)
669 goto final_destruction;
670 if (cell->state == AFS_CELL_FAILED)
671 goto done;
d5c32c89
DH
672 smp_store_release(&cell->state, AFS_CELL_UNSET);
673 wake_up_var(&cell->state);
989782dc
DH
674 goto again;
675
676 case AFS_CELL_UNSET:
d5c32c89
DH
677 smp_store_release(&cell->state, AFS_CELL_ACTIVATING);
678 wake_up_var(&cell->state);
989782dc
DH
679 goto again;
680
681 case AFS_CELL_ACTIVATING:
682 ret = afs_activate_cell(net, cell);
683 if (ret < 0)
684 goto activation_failed;
685
d5c32c89
DH
686 smp_store_release(&cell->state, AFS_CELL_ACTIVE);
687 wake_up_var(&cell->state);
989782dc
DH
688 goto again;
689
690 case AFS_CELL_ACTIVE:
691 if (atomic_read(&cell->usage) > 1) {
d5c32c89
DH
692 if (test_and_clear_bit(AFS_CELL_FL_DO_LOOKUP, &cell->flags)) {
693 ret = afs_update_cell(cell);
694 if (ret < 0)
695 cell->error = ret;
696 }
989782dc
DH
697 goto done;
698 }
d5c32c89
DH
699 smp_store_release(&cell->state, AFS_CELL_DEACTIVATING);
700 wake_up_var(&cell->state);
989782dc
DH
701 goto again;
702
703 case AFS_CELL_DEACTIVATING:
989782dc
DH
704 if (atomic_read(&cell->usage) > 1)
705 goto reverse_deactivation;
706 afs_deactivate_cell(net, cell);
d5c32c89
DH
707 smp_store_release(&cell->state, AFS_CELL_INACTIVE);
708 wake_up_var(&cell->state);
989782dc
DH
709 goto again;
710
711 default:
712 break;
713 }
714 _debug("bad state %u", cell->state);
715 BUG(); /* Unhandled state */
716
717activation_failed:
718 cell->error = ret;
719 afs_deactivate_cell(net, cell);
720
d5c32c89
DH
721 smp_store_release(&cell->state, AFS_CELL_FAILED); /* vs error */
722 wake_up_var(&cell->state);
989782dc
DH
723 goto again;
724
725reverse_deactivation:
d5c32c89
DH
726 smp_store_release(&cell->state, AFS_CELL_ACTIVE);
727 wake_up_var(&cell->state);
989782dc
DH
728 _leave(" [deact->act]");
729 return;
730
731done:
732 _leave(" [done %u]", cell->state);
733 return;
734
735final_destruction:
736 call_rcu(&cell->rcu, afs_cell_destroy);
737 afs_dec_cells_outstanding(net);
738 _leave(" [destruct %d]", atomic_read(&net->cells_outstanding));
739}
740
741/*
742 * Manage the records of cells known to a network namespace. This includes
743 * updating the DNS records and garbage collecting unused cells that were
744 * automatically added.
745 *
746 * Note that constructed cell records may only be removed from net->cells by
747 * this work item, so it is safe for this work item to stash a cursor pointing
748 * into the tree and then return to caller (provided it skips cells that are
749 * still under construction).
750 *
751 * Note also that we were given an increment on net->cells_outstanding by
752 * whoever queued us that we need to deal with before returning.
753 */
754void afs_manage_cells(struct work_struct *work)
755{
756 struct afs_net *net = container_of(work, struct afs_net, cells_manager);
757 struct rb_node *cursor;
758 time64_t now = ktime_get_real_seconds(), next_manage = TIME64_MAX;
759 bool purging = !net->live;
1da177e4
LT
760
761 _enter("");
762
989782dc
DH
763 /* Trawl the cell database looking for cells that have expired from
764 * lack of use and cells whose DNS results have expired and dispatch
765 * their managers.
766 */
767 read_seqlock_excl(&net->cells_lock);
1da177e4 768
989782dc
DH
769 for (cursor = rb_first(&net->cells); cursor; cursor = rb_next(cursor)) {
770 struct afs_cell *cell =
771 rb_entry(cursor, struct afs_cell, net_node);
772 unsigned usage;
773 bool sched_cell = false;
08e0e7c8 774
989782dc
DH
775 usage = atomic_read(&cell->usage);
776 _debug("manage %s %u", cell->name, usage);
777
778 ASSERTCMP(usage, >=, 1);
779
780 if (purging) {
781 if (test_and_clear_bit(AFS_CELL_FL_NO_GC, &cell->flags))
782 usage = atomic_dec_return(&cell->usage);
783 ASSERTCMP(usage, ==, 1);
784 }
1da177e4 785
989782dc 786 if (usage == 1) {
d5c32c89 787 struct afs_vlserver_list *vllist;
989782dc 788 time64_t expire_at = cell->last_inactive;
1da177e4 789
d5c32c89
DH
790 read_lock(&cell->vl_servers_lock);
791 vllist = rcu_dereference_protected(
792 cell->vl_servers,
793 lockdep_is_held(&cell->vl_servers_lock));
794 if (vllist->nr_servers > 0)
989782dc 795 expire_at += afs_cell_gc_delay;
d5c32c89 796 read_unlock(&cell->vl_servers_lock);
989782dc
DH
797 if (purging || expire_at <= now)
798 sched_cell = true;
799 else if (expire_at < next_manage)
800 next_manage = expire_at;
1da177e4
LT
801 }
802
989782dc 803 if (!purging) {
d5c32c89 804 if (test_bit(AFS_CELL_FL_DO_LOOKUP, &cell->flags))
989782dc 805 sched_cell = true;
989782dc
DH
806 }
807
808 if (sched_cell)
809 queue_work(afs_wq, &cell->manager);
810 }
811
812 read_sequnlock_excl(&net->cells_lock);
1da177e4 813
989782dc
DH
814 /* Update the timer on the way out. We have to pass an increment on
815 * cells_outstanding in the namespace that we are in to the timer or
816 * the work scheduler.
817 */
818 if (!purging && next_manage < TIME64_MAX) {
819 now = ktime_get_real_seconds();
1da177e4 820
989782dc
DH
821 if (next_manage - now <= 0) {
822 if (queue_work(afs_wq, &net->cells_manager))
823 atomic_inc(&net->cells_outstanding);
824 } else {
825 afs_set_cell_timer(net, next_manage - now);
1da177e4
LT
826 }
827 }
828
989782dc
DH
829 afs_dec_cells_outstanding(net);
830 _leave(" [%d]", atomic_read(&net->cells_outstanding));
831}
832
833/*
834 * Purge in-memory cell database.
835 */
836void afs_cell_purge(struct afs_net *net)
837{
838 struct afs_cell *ws;
839
840 _enter("");
841
842 write_seqlock(&net->cells_lock);
1588def9
DH
843 ws = rcu_access_pointer(net->ws_cell);
844 RCU_INIT_POINTER(net->ws_cell, NULL);
989782dc
DH
845 write_sequnlock(&net->cells_lock);
846 afs_put_cell(net, ws);
847
848 _debug("del timer");
849 if (del_timer_sync(&net->cells_timer))
850 atomic_dec(&net->cells_outstanding);
851
852 _debug("kick mgr");
853 afs_queue_cell_manager(net);
854
855 _debug("wait");
ab1fbe32
PZ
856 wait_var_event(&net->cells_outstanding,
857 !atomic_read(&net->cells_outstanding));
1da177e4 858 _leave("");
ec26815a 859}