]> asedeno.scripts.mit.edu Git - linux.git/blob - fs/afs/cell.c
963b6fa51fdf8a860abc287de970a8ece6e9743a
[linux.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                                 break;
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                 }
108
109         } while (need_seqretry(&net->cells_lock, seq));
110
111         done_seqretry(&net->cells_lock, seq);
112
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  */
120 static struct afs_cell *afs_alloc_cell(struct afs_net *net,
121                                        const char *name, unsigned int namelen,
122                                        const char *addresses)
123 {
124         struct afs_cell *cell;
125         int i, ret;
126
127         ASSERT(name);
128         if (namelen == 0)
129                 return ERR_PTR(-EINVAL);
130         if (namelen > AFS_MAXCELLNAME) {
131                 _leave(" = -ENAMETOOLONG");
132                 return ERR_PTR(-ENAMETOOLONG);
133         }
134         if (namelen == 5 && memcmp(name, "@cell", 5) == 0)
135                 return ERR_PTR(-EINVAL);
136
137         _enter("%*.*s,%s", namelen, namelen, name, addresses);
138
139         cell = kzalloc(sizeof(struct afs_cell), GFP_KERNEL);
140         if (!cell) {
141                 _leave(" = -ENOMEM");
142                 return ERR_PTR(-ENOMEM);
143         }
144
145         cell->net = net;
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);
152         cell->flags = ((1 << AFS_CELL_FL_NOT_READY) |
153                        (1 << AFS_CELL_FL_NO_LOOKUP_YET));
154         INIT_LIST_HEAD(&cell->proc_volumes);
155         rwlock_init(&cell->proc_lock);
156         rwlock_init(&cell->vl_servers_lock);
157
158         /* Fill in the VL server list if we were given a list of addresses to
159          * use.
160          */
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);
169                         goto parse_failed;
170                 }
171
172                 rcu_assign_pointer(cell->vl_servers, vllist);
173                 cell->dns_expiry = TIME64_MAX;
174         }
175
176         _leave(" = %p", cell);
177         return cell;
178
179 parse_failed:
180         if (ret == -EINVAL)
181                 printk(KERN_ERR "kAFS: bad VL server IP address\n");
182         kfree(cell);
183         _leave(" = %d", ret);
184         return ERR_PTR(ret);
185 }
186
187 /*
188  * afs_lookup_cell - Look up or create a cell record.
189  * @net:        The network namespace
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.
199  */
200 struct afs_cell *afs_lookup_cell(struct afs_net *net,
201                                  const char *name, unsigned int namesz,
202                                  const char *vllist, bool excl)
203 {
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();
214                 if (!IS_ERR(cell))
215                         goto wait_for_cell;
216         }
217
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;
229         }
230
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;
252         }
253
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);
260
261         queue_work(afs_wq, &cell->manager);
262
263 wait_for_cell:
264         _debug("wait_for_cell");
265         ret = wait_on_bit(&cell->flags, AFS_CELL_FL_NOT_READY, TASK_INTERRUPTIBLE);
266         smp_rmb();
267
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         }
278
279         _leave(" = %p [cell]", cell);
280         return cell;
281
282 cell_already_exists:
283         _debug("cell exists");
284         cell = cursor;
285         if (excl) {
286                 ret = -EEXIST;
287         } else {
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;
295         goto error_noput;
296 error:
297         afs_put_cell(net, cell);
298 error_noput:
299         _leave(" = %d [error]", ret);
300         return ERR_PTR(ret);
301 }
302
303 /*
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
307  */
308 int afs_cell_init(struct afs_net *net, const char *rootcell)
309 {
310         struct afs_cell *old_root, *new_root;
311         const char *cp, *vllist;
312         size_t len;
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                  */
320                 _leave(" = 0 [no root]");
321                 return 0;
322         }
323
324         cp = strchr(rootcell, ':');
325         if (!cp) {
326                 _debug("kAFS: no VL server IP addresses specified");
327                 vllist = NULL;
328                 len = strlen(rootcell);
329         } else {
330                 vllist = cp + 1;
331                 len = cp - rootcell;
332         }
333
334         /* allocate a cell record for the root cell */
335         new_root = afs_lookup_cell(net, rootcell, len, vllist, false);
336         if (IS_ERR(new_root)) {
337                 _leave(" = %ld", PTR_ERR(new_root));
338                 return PTR_ERR(new_root);
339         }
340
341         if (!test_and_set_bit(AFS_CELL_FL_NO_GC, &new_root->flags))
342                 afs_get_cell(new_root);
343
344         /* install the new cell */
345         write_seqlock(&net->cells_lock);
346         old_root = rcu_access_pointer(net->ws_cell);
347         rcu_assign_pointer(net->ws_cell, new_root);
348         write_sequnlock(&net->cells_lock);
349
350         afs_put_cell(net, old_root);
351         _leave(" = 0");
352         return 0;
353 }
354
355 /*
356  * Update a cell's VL server address list from the DNS.
357  */
358 static void afs_update_cell(struct afs_cell *cell)
359 {
360         struct afs_vlserver_list *vllist, *old;
361         time64_t now, expiry;
362
363         _enter("%s", cell->name);
364
365         vllist = afs_dns_query(cell, &expiry);
366         if (IS_ERR(vllist)) {
367                 switch (PTR_ERR(vllist)) {
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;
374
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                 }
382
383                 cell->error = -EDESTADDRREQ;
384         } else {
385                 clear_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags);
386                 clear_bit(AFS_CELL_FL_NOT_FOUND, &cell->flags);
387
388                 /* Exclusion on changing vl_addrs is achieved by a
389                  * non-reentrant work item.
390                  */
391                 old = rcu_dereference_protected(cell->vl_servers, true);
392                 rcu_assign_pointer(cell->vl_servers, vllist);
393                 cell->dns_expiry = expiry;
394
395                 if (old)
396                         afs_put_vlserverlist(cell->net, old);
397         }
398
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);
401
402         now = ktime_get_real_seconds();
403         afs_set_cell_timer(cell->net, cell->dns_expiry - now);
404         _leave("");
405 }
406
407 /*
408  * Destroy a cell record
409  */
410 static void afs_cell_destroy(struct rcu_head *rcu)
411 {
412         struct afs_cell *cell = container_of(rcu, struct afs_cell, rcu);
413
414         _enter("%p{%s}", cell, cell->name);
415
416         ASSERTCMP(atomic_read(&cell->usage), ==, 0);
417
418         afs_put_vlserverlist(cell->net, rcu_access_pointer(cell->vl_servers));
419         key_put(cell->anonymous_key);
420         kfree(cell);
421
422         _leave(" [destroyed]");
423 }
424
425 /*
426  * Queue the cell manager.
427  */
428 static void afs_queue_cell_manager(struct afs_net *net)
429 {
430         int outstanding = atomic_inc_return(&net->cells_outstanding);
431
432         _enter("%d", outstanding);
433
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  */
442 void 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 }
450
451 /*
452  * Get a reference on a cell record.
453  */
454 struct afs_cell *afs_get_cell(struct afs_cell *cell)
455 {
456         atomic_inc(&cell->usage);
457         return cell;
458 }
459
460 /*
461  * Drop a reference on a cell record.
462  */
463 void afs_put_cell(struct afs_net *net, struct afs_cell *cell)
464 {
465         time64_t now, expire_delay;
466
467         if (!cell)
468                 return;
469
470         _enter("%s", cell->name);
471
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;
478
479         if (atomic_dec_return(&cell->usage) > 1)
480                 return;
481
482         /* 'cell' may now be garbage collected. */
483         afs_set_cell_timer(net, expire_delay);
484 }
485
486 /*
487  * Allocate a key to use as a placeholder for anonymous user security.
488  */
489 static int afs_alloc_anon_key(struct afs_cell *cell)
490 {
491         struct key *key;
492         char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp;
493
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++);
501
502         key = rxrpc_get_null_key(keyname);
503         if (IS_ERR(key))
504                 return PTR_ERR(key);
505
506         cell->anonymous_key = key;
507
508         _debug("anon key %p{%x}",
509                cell->anonymous_key, key_serial(cell->anonymous_key));
510         return 0;
511 }
512
513 /*
514  * Activate a cell.
515  */
516 static int afs_activate_cell(struct afs_net *net, struct afs_cell *cell)
517 {
518         struct hlist_node **p;
519         struct afs_cell *pcell;
520         int ret;
521
522         if (!cell->anonymous_key) {
523                 ret = afs_alloc_anon_key(cell);
524                 if (ret < 0)
525                         return ret;
526         }
527
528 #ifdef CONFIG_AFS_FSCACHE
529         cell->cache = fscache_acquire_cookie(afs_cache_netfs.primary_index,
530                                              &afs_cell_cache_index_def,
531                                              cell->name, strlen(cell->name),
532                                              NULL, 0,
533                                              cell, 0, true);
534 #endif
535         ret = afs_proc_cell_setup(cell);
536         if (ret < 0)
537                 return ret;
538
539         mutex_lock(&net->proc_cells_lock);
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
552         afs_dynroot_mkdir(net, cell);
553         mutex_unlock(&net->proc_cells_lock);
554         return 0;
555 }
556
557 /*
558  * Deactivate a cell.
559  */
560 static void afs_deactivate_cell(struct afs_net *net, struct afs_cell *cell)
561 {
562         _enter("%s", cell->name);
563
564         afs_proc_cell_remove(cell);
565
566         mutex_lock(&net->proc_cells_lock);
567         hlist_del_rcu(&cell->proc_link);
568         afs_dynroot_rmdir(net, cell);
569         mutex_unlock(&net->proc_cells_lock);
570
571 #ifdef CONFIG_AFS_FSCACHE
572         fscache_relinquish_cookie(cell->cache, NULL, false);
573         cell->cache = NULL;
574 #endif
575
576         _leave("");
577 }
578
579 /*
580  * Manage a cell record, initialising and destroying it, maintaining its DNS
581  * records.
582  */
583 static void afs_manage_cell(struct work_struct *work)
584 {
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
592 again:
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
649 activation_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
659 reverse_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
667 done:
668         _leave(" [done %u]", cell->state);
669         return;
670
671 final_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  */
690 void 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;
696
697         _enter("");
698
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);
704
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;
710
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                 }
721
722                 if (usage == 1) {
723                         time64_t expire_at = cell->last_inactive;
724
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;
732                 }
733
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);
746
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();
753
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);
759                 }
760         }
761
762         afs_dec_cells_outstanding(net);
763         _leave(" [%d]", atomic_read(&net->cells_outstanding));
764 }
765
766 /*
767  * Purge in-memory cell database.
768  */
769 void afs_cell_purge(struct afs_net *net)
770 {
771         struct afs_cell *ws;
772
773         _enter("");
774
775         write_seqlock(&net->cells_lock);
776         ws = rcu_access_pointer(net->ws_cell);
777         RCU_INIT_POINTER(net->ws_cell, NULL);
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");
789         wait_var_event(&net->cells_outstanding,
790                        !atomic_read(&net->cells_outstanding));
791         _leave("");
792 }