xref: /openbmc/linux/fs/afs/cell.c (revision f66501dc)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* AFS cell and server record management
3  *
4  * Copyright (C) 2002, 2017 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7 
8 #include <linux/slab.h>
9 #include <linux/key.h>
10 #include <linux/ctype.h>
11 #include <linux/dns_resolver.h>
12 #include <linux/sched.h>
13 #include <linux/inet.h>
14 #include <linux/namei.h>
15 #include <keys/rxrpc-type.h>
16 #include "internal.h"
17 
18 static unsigned __read_mostly afs_cell_gc_delay = 10;
19 static unsigned __read_mostly afs_cell_min_ttl = 10 * 60;
20 static unsigned __read_mostly afs_cell_max_ttl = 24 * 60 * 60;
21 
22 static void afs_manage_cell(struct work_struct *);
23 
24 static void afs_dec_cells_outstanding(struct afs_net *net)
25 {
26 	if (atomic_dec_and_test(&net->cells_outstanding))
27 		wake_up_var(&net->cells_outstanding);
28 }
29 
30 /*
31  * Set the cell timer to fire after a given delay, assuming it's not already
32  * set for an earlier time.
33  */
34 static void afs_set_cell_timer(struct afs_net *net, time64_t delay)
35 {
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  */
47 struct 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);
77 				break;
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 		}
106 
107 	} while (need_seqretry(&net->cells_lock, seq));
108 
109 	done_seqretry(&net->cells_lock, seq);
110 
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  */
118 static struct afs_cell *afs_alloc_cell(struct afs_net *net,
119 				       const char *name, unsigned int namelen,
120 				       const char *addresses)
121 {
122 	struct afs_vlserver_list *vllist;
123 	struct afs_cell *cell;
124 	int i, ret;
125 
126 	ASSERT(name);
127 	if (namelen == 0)
128 		return ERR_PTR(-EINVAL);
129 	if (namelen > AFS_MAXCELLNAME) {
130 		_leave(" = -ENAMETOOLONG");
131 		return ERR_PTR(-ENAMETOOLONG);
132 	}
133 	if (namelen == 5 && memcmp(name, "@cell", 5) == 0)
134 		return ERR_PTR(-EINVAL);
135 
136 	_enter("%*.*s,%s", namelen, namelen, name, addresses);
137 
138 	cell = kzalloc(sizeof(struct afs_cell), GFP_KERNEL);
139 	if (!cell) {
140 		_leave(" = -ENOMEM");
141 		return ERR_PTR(-ENOMEM);
142 	}
143 
144 	cell->net = net;
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);
151 	INIT_LIST_HEAD(&cell->proc_volumes);
152 	rwlock_init(&cell->proc_lock);
153 	rwlock_init(&cell->vl_servers_lock);
154 
155 	/* Provide a VL server list, filling it in if we were given a list of
156 	 * addresses to use.
157 	 */
158 	if (addresses) {
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);
164 			goto parse_failed;
165 		}
166 
167 		vllist->source = DNS_RECORD_FROM_CONFIG;
168 		vllist->status = DNS_LOOKUP_NOT_DONE;
169 		cell->dns_expiry = TIME64_MAX;
170 	} else {
171 		ret = -ENOMEM;
172 		vllist = afs_alloc_vlserver_list(0);
173 		if (!vllist)
174 			goto error;
175 		vllist->source = DNS_RECORD_UNAVAILABLE;
176 		vllist->status = DNS_LOOKUP_NOT_DONE;
177 		cell->dns_expiry = ktime_get_real_seconds();
178 	}
179 
180 	rcu_assign_pointer(cell->vl_servers, vllist);
181 
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 
186 	_leave(" = %p", cell);
187 	return cell;
188 
189 parse_failed:
190 	if (ret == -EINVAL)
191 		printk(KERN_ERR "kAFS: bad VL server IP address\n");
192 error:
193 	kfree(cell);
194 	_leave(" = %d", ret);
195 	return ERR_PTR(ret);
196 }
197 
198 /*
199  * afs_lookup_cell - Look up or create a cell record.
200  * @net:	The network namespace
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.
210  */
211 struct afs_cell *afs_lookup_cell(struct afs_net *net,
212 				 const char *name, unsigned int namesz,
213 				 const char *vllist, bool excl)
214 {
215 	struct afs_cell *cell, *candidate, *cursor;
216 	struct rb_node *parent, **pp;
217 	enum afs_cell_state state;
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();
226 		if (!IS_ERR(cell))
227 			goto wait_for_cell;
228 	}
229 
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;
241 	}
242 
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;
264 	}
265 
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);
272 
273 	queue_work(afs_wq, &cell->manager);
274 
275 wait_for_cell:
276 	_debug("wait_for_cell");
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) {
285 		ret = cell->error;
286 		goto error;
287 	}
288 
289 	_leave(" = %p [cell]", cell);
290 	return cell;
291 
292 cell_already_exists:
293 	_debug("cell exists");
294 	cell = cursor;
295 	if (excl) {
296 		ret = -EEXIST;
297 	} else {
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;
305 	goto error_noput;
306 error:
307 	afs_put_cell(net, cell);
308 error_noput:
309 	_leave(" = %d [error]", ret);
310 	return ERR_PTR(ret);
311 }
312 
313 /*
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
317  */
318 int afs_cell_init(struct afs_net *net, const char *rootcell)
319 {
320 	struct afs_cell *old_root, *new_root;
321 	const char *cp, *vllist;
322 	size_t len;
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 		 */
330 		_leave(" = 0 [no root]");
331 		return 0;
332 	}
333 
334 	cp = strchr(rootcell, ':');
335 	if (!cp) {
336 		_debug("kAFS: no VL server IP addresses specified");
337 		vllist = NULL;
338 		len = strlen(rootcell);
339 	} else {
340 		vllist = cp + 1;
341 		len = cp - rootcell;
342 	}
343 
344 	/* allocate a cell record for the root cell */
345 	new_root = afs_lookup_cell(net, rootcell, len, vllist, false);
346 	if (IS_ERR(new_root)) {
347 		_leave(" = %ld", PTR_ERR(new_root));
348 		return PTR_ERR(new_root);
349 	}
350 
351 	if (!test_and_set_bit(AFS_CELL_FL_NO_GC, &new_root->flags))
352 		afs_get_cell(new_root);
353 
354 	/* install the new cell */
355 	write_seqlock(&net->cells_lock);
356 	old_root = rcu_access_pointer(net->ws_cell);
357 	rcu_assign_pointer(net->ws_cell, new_root);
358 	write_sequnlock(&net->cells_lock);
359 
360 	afs_put_cell(net, old_root);
361 	_leave(" = 0");
362 	return 0;
363 }
364 
365 /*
366  * Update a cell's VL server address list from the DNS.
367  */
368 static int afs_update_cell(struct afs_cell *cell)
369 {
370 	struct afs_vlserver_list *vllist, *old = NULL, *p;
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;
374 	int ret = 0;
375 
376 	_enter("%s", cell->name);
377 
378 	vllist = afs_dns_query(cell, &expiry);
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;
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 
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:
421 			/* The DNS said that the cell does not exist or there
422 			 * weren't any addresses to be had.
423 			 */
424 			cell->dns_expiry = expiry;
425 			break;
426 
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:
431 		default:
432 			cell->dns_expiry = now + 10;
433 			break;
434 		}
435 	} else {
436 		cell->dns_expiry = expiry;
437 	}
438 
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);
451 
452 out_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;
458 }
459 
460 /*
461  * Destroy a cell record
462  */
463 static void afs_cell_destroy(struct rcu_head *rcu)
464 {
465 	struct afs_cell *cell = container_of(rcu, struct afs_cell, rcu);
466 
467 	_enter("%p{%s}", cell, cell->name);
468 
469 	ASSERTCMP(atomic_read(&cell->usage), ==, 0);
470 
471 	afs_put_vlserverlist(cell->net, rcu_access_pointer(cell->vl_servers));
472 	key_put(cell->anonymous_key);
473 	kfree(cell);
474 
475 	_leave(" [destroyed]");
476 }
477 
478 /*
479  * Queue the cell manager.
480  */
481 static void afs_queue_cell_manager(struct afs_net *net)
482 {
483 	int outstanding = atomic_inc_return(&net->cells_outstanding);
484 
485 	_enter("%d", outstanding);
486 
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  */
495 void 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 }
503 
504 /*
505  * Get a reference on a cell record.
506  */
507 struct afs_cell *afs_get_cell(struct afs_cell *cell)
508 {
509 	atomic_inc(&cell->usage);
510 	return cell;
511 }
512 
513 /*
514  * Drop a reference on a cell record.
515  */
516 void afs_put_cell(struct afs_net *net, struct afs_cell *cell)
517 {
518 	time64_t now, expire_delay;
519 
520 	if (!cell)
521 		return;
522 
523 	_enter("%s", cell->name);
524 
525 	now = ktime_get_real_seconds();
526 	cell->last_inactive = now;
527 	expire_delay = 0;
528 	if (cell->vl_servers->nr_servers)
529 		expire_delay = afs_cell_gc_delay;
530 
531 	if (atomic_dec_return(&cell->usage) > 1)
532 		return;
533 
534 	/* 'cell' may now be garbage collected. */
535 	afs_set_cell_timer(net, expire_delay);
536 }
537 
538 /*
539  * Allocate a key to use as a placeholder for anonymous user security.
540  */
541 static int afs_alloc_anon_key(struct afs_cell *cell)
542 {
543 	struct key *key;
544 	char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp;
545 
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++);
553 
554 	key = rxrpc_get_null_key(keyname);
555 	if (IS_ERR(key))
556 		return PTR_ERR(key);
557 
558 	cell->anonymous_key = key;
559 
560 	_debug("anon key %p{%x}",
561 	       cell->anonymous_key, key_serial(cell->anonymous_key));
562 	return 0;
563 }
564 
565 /*
566  * Activate a cell.
567  */
568 static int afs_activate_cell(struct afs_net *net, struct afs_cell *cell)
569 {
570 	struct hlist_node **p;
571 	struct afs_cell *pcell;
572 	int ret;
573 
574 	if (!cell->anonymous_key) {
575 		ret = afs_alloc_anon_key(cell);
576 		if (ret < 0)
577 			return ret;
578 	}
579 
580 #ifdef CONFIG_AFS_FSCACHE
581 	cell->cache = fscache_acquire_cookie(afs_cache_netfs.primary_index,
582 					     &afs_cell_cache_index_def,
583 					     cell->name, strlen(cell->name),
584 					     NULL, 0,
585 					     cell, 0, true);
586 #endif
587 	ret = afs_proc_cell_setup(cell);
588 	if (ret < 0)
589 		return ret;
590 
591 	mutex_lock(&net->proc_cells_lock);
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 
604 	afs_dynroot_mkdir(net, cell);
605 	mutex_unlock(&net->proc_cells_lock);
606 	return 0;
607 }
608 
609 /*
610  * Deactivate a cell.
611  */
612 static void afs_deactivate_cell(struct afs_net *net, struct afs_cell *cell)
613 {
614 	_enter("%s", cell->name);
615 
616 	afs_proc_cell_remove(cell);
617 
618 	mutex_lock(&net->proc_cells_lock);
619 	hlist_del_rcu(&cell->proc_link);
620 	afs_dynroot_rmdir(net, cell);
621 	mutex_unlock(&net->proc_cells_lock);
622 
623 #ifdef CONFIG_AFS_FSCACHE
624 	fscache_relinquish_cookie(cell->cache, NULL, false);
625 	cell->cache = NULL;
626 #endif
627 
628 	_leave("");
629 }
630 
631 /*
632  * Manage a cell record, initialising and destroying it, maintaining its DNS
633  * records.
634  */
635 static void afs_manage_cell(struct work_struct *work)
636 {
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 
644 again:
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;
659 		smp_store_release(&cell->state, AFS_CELL_UNSET);
660 		wake_up_var(&cell->state);
661 		goto again;
662 
663 	case AFS_CELL_UNSET:
664 		smp_store_release(&cell->state, AFS_CELL_ACTIVATING);
665 		wake_up_var(&cell->state);
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 
673 		smp_store_release(&cell->state, AFS_CELL_ACTIVE);
674 		wake_up_var(&cell->state);
675 		goto again;
676 
677 	case AFS_CELL_ACTIVE:
678 		if (atomic_read(&cell->usage) > 1) {
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 			}
684 			goto done;
685 		}
686 		smp_store_release(&cell->state, AFS_CELL_DEACTIVATING);
687 		wake_up_var(&cell->state);
688 		goto again;
689 
690 	case AFS_CELL_DEACTIVATING:
691 		if (atomic_read(&cell->usage) > 1)
692 			goto reverse_deactivation;
693 		afs_deactivate_cell(net, cell);
694 		smp_store_release(&cell->state, AFS_CELL_INACTIVE);
695 		wake_up_var(&cell->state);
696 		goto again;
697 
698 	default:
699 		break;
700 	}
701 	_debug("bad state %u", cell->state);
702 	BUG(); /* Unhandled state */
703 
704 activation_failed:
705 	cell->error = ret;
706 	afs_deactivate_cell(net, cell);
707 
708 	smp_store_release(&cell->state, AFS_CELL_FAILED); /* vs error */
709 	wake_up_var(&cell->state);
710 	goto again;
711 
712 reverse_deactivation:
713 	smp_store_release(&cell->state, AFS_CELL_ACTIVE);
714 	wake_up_var(&cell->state);
715 	_leave(" [deact->act]");
716 	return;
717 
718 done:
719 	_leave(" [done %u]", cell->state);
720 	return;
721 
722 final_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  */
741 void 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;
747 
748 	_enter("");
749 
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);
755 
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;
761 
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 		}
772 
773 		if (usage == 1) {
774 			struct afs_vlserver_list *vllist;
775 			time64_t expire_at = cell->last_inactive;
776 
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)
782 				expire_at += afs_cell_gc_delay;
783 			read_unlock(&cell->vl_servers_lock);
784 			if (purging || expire_at <= now)
785 				sched_cell = true;
786 			else if (expire_at < next_manage)
787 				next_manage = expire_at;
788 		}
789 
790 		if (!purging) {
791 			if (test_bit(AFS_CELL_FL_DO_LOOKUP, &cell->flags))
792 				sched_cell = true;
793 		}
794 
795 		if (sched_cell)
796 			queue_work(afs_wq, &cell->manager);
797 	}
798 
799 	read_sequnlock_excl(&net->cells_lock);
800 
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();
807 
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);
813 		}
814 	}
815 
816 	afs_dec_cells_outstanding(net);
817 	_leave(" [%d]", atomic_read(&net->cells_outstanding));
818 }
819 
820 /*
821  * Purge in-memory cell database.
822  */
823 void afs_cell_purge(struct afs_net *net)
824 {
825 	struct afs_cell *ws;
826 
827 	_enter("");
828 
829 	write_seqlock(&net->cells_lock);
830 	ws = rcu_access_pointer(net->ws_cell);
831 	RCU_INIT_POINTER(net->ws_cell, NULL);
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");
843 	wait_var_event(&net->cells_outstanding,
844 		       !atomic_read(&net->cells_outstanding));
845 	_leave("");
846 }
847