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