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