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