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 static atomic_t cell_debug_id;
22
23 static void afs_queue_cell_manager(struct afs_net *);
24 static void afs_manage_cell_work(struct work_struct *);
25
afs_dec_cells_outstanding(struct afs_net * net)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 */
afs_set_cell_timer(struct afs_net * net,time64_t delay)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 } else {
43 afs_queue_cell_manager(net);
44 }
45 }
46
47 /*
48 * Look up and get an activation reference on a cell record. The caller must
49 * hold net->cells_lock at least read-locked.
50 */
afs_find_cell_locked(struct afs_net * net,const char * name,unsigned int namesz,enum afs_cell_trace reason)51 static struct afs_cell *afs_find_cell_locked(struct afs_net *net,
52 const char *name, unsigned int namesz,
53 enum afs_cell_trace reason)
54 {
55 struct afs_cell *cell = NULL;
56 struct rb_node *p;
57 int n;
58
59 _enter("%*.*s", namesz, namesz, name);
60
61 if (name && namesz == 0)
62 return ERR_PTR(-EINVAL);
63 if (namesz > AFS_MAXCELLNAME)
64 return ERR_PTR(-ENAMETOOLONG);
65
66 if (!name) {
67 cell = net->ws_cell;
68 if (!cell)
69 return ERR_PTR(-EDESTADDRREQ);
70 goto found;
71 }
72
73 p = net->cells.rb_node;
74 while (p) {
75 cell = rb_entry(p, struct afs_cell, net_node);
76
77 n = strncasecmp(cell->name, name,
78 min_t(size_t, cell->name_len, namesz));
79 if (n == 0)
80 n = cell->name_len - namesz;
81 if (n < 0)
82 p = p->rb_left;
83 else if (n > 0)
84 p = p->rb_right;
85 else
86 goto found;
87 }
88
89 return ERR_PTR(-ENOENT);
90
91 found:
92 return afs_use_cell(cell, reason);
93 }
94
95 /*
96 * Look up and get an activation reference on a cell record.
97 */
afs_find_cell(struct afs_net * net,const char * name,unsigned int namesz,enum afs_cell_trace reason)98 struct afs_cell *afs_find_cell(struct afs_net *net,
99 const char *name, unsigned int namesz,
100 enum afs_cell_trace reason)
101 {
102 struct afs_cell *cell;
103
104 down_read(&net->cells_lock);
105 cell = afs_find_cell_locked(net, name, namesz, reason);
106 up_read(&net->cells_lock);
107 return cell;
108 }
109
110 /*
111 * Set up a cell record and fill in its name, VL server address list and
112 * allocate an anonymous key
113 */
afs_alloc_cell(struct afs_net * net,const char * name,unsigned int namelen,const char * addresses)114 static struct afs_cell *afs_alloc_cell(struct afs_net *net,
115 const char *name, unsigned int namelen,
116 const char *addresses)
117 {
118 struct afs_vlserver_list *vllist;
119 struct afs_cell *cell;
120 int i, ret;
121
122 ASSERT(name);
123 if (namelen == 0)
124 return ERR_PTR(-EINVAL);
125 if (namelen > AFS_MAXCELLNAME) {
126 _leave(" = -ENAMETOOLONG");
127 return ERR_PTR(-ENAMETOOLONG);
128 }
129
130 /* Prohibit cell names that contain unprintable chars, '/' and '@' or
131 * that begin with a dot. This also precludes "@cell".
132 */
133 if (name[0] == '.')
134 return ERR_PTR(-EINVAL);
135 for (i = 0; i < namelen; i++) {
136 char ch = name[i];
137 if (!isprint(ch) || ch == '/' || ch == '@')
138 return ERR_PTR(-EINVAL);
139 }
140
141 _enter("%*.*s,%s", namelen, namelen, name, addresses);
142
143 cell = kzalloc(sizeof(struct afs_cell), GFP_KERNEL);
144 if (!cell) {
145 _leave(" = -ENOMEM");
146 return ERR_PTR(-ENOMEM);
147 }
148
149 cell->name = kmalloc(namelen + 1, GFP_KERNEL);
150 if (!cell->name) {
151 kfree(cell);
152 return ERR_PTR(-ENOMEM);
153 }
154
155 cell->net = net;
156 cell->name_len = namelen;
157 for (i = 0; i < namelen; i++)
158 cell->name[i] = tolower(name[i]);
159 cell->name[i] = 0;
160
161 refcount_set(&cell->ref, 1);
162 atomic_set(&cell->active, 0);
163 INIT_WORK(&cell->manager, afs_manage_cell_work);
164 spin_lock_init(&cell->vs_lock);
165 cell->volumes = RB_ROOT;
166 INIT_HLIST_HEAD(&cell->proc_volumes);
167 seqlock_init(&cell->volume_lock);
168 cell->fs_servers = RB_ROOT;
169 seqlock_init(&cell->fs_lock);
170 INIT_LIST_HEAD(&cell->fs_open_mmaps);
171 init_rwsem(&cell->fs_open_mmaps_lock);
172 rwlock_init(&cell->vl_servers_lock);
173 cell->flags = (1 << AFS_CELL_FL_CHECK_ALIAS);
174
175 /* Provide a VL server list, filling it in if we were given a list of
176 * addresses to use.
177 */
178 if (addresses) {
179 vllist = afs_parse_text_addrs(net,
180 addresses, strlen(addresses), ':',
181 VL_SERVICE, AFS_VL_PORT);
182 if (IS_ERR(vllist)) {
183 ret = PTR_ERR(vllist);
184 goto parse_failed;
185 }
186
187 vllist->source = DNS_RECORD_FROM_CONFIG;
188 vllist->status = DNS_LOOKUP_NOT_DONE;
189 cell->dns_expiry = TIME64_MAX;
190 } else {
191 ret = -ENOMEM;
192 vllist = afs_alloc_vlserver_list(0);
193 if (!vllist)
194 goto error;
195 vllist->source = DNS_RECORD_UNAVAILABLE;
196 vllist->status = DNS_LOOKUP_NOT_DONE;
197 cell->dns_expiry = ktime_get_real_seconds();
198 }
199
200 rcu_assign_pointer(cell->vl_servers, vllist);
201
202 cell->dns_source = vllist->source;
203 cell->dns_status = vllist->status;
204 smp_store_release(&cell->dns_lookup_count, 1); /* vs source/status */
205 atomic_inc(&net->cells_outstanding);
206 cell->debug_id = atomic_inc_return(&cell_debug_id);
207 trace_afs_cell(cell->debug_id, 1, 0, afs_cell_trace_alloc);
208
209 _leave(" = %p", cell);
210 return cell;
211
212 parse_failed:
213 if (ret == -EINVAL)
214 printk(KERN_ERR "kAFS: bad VL server IP address\n");
215 error:
216 kfree(cell->name);
217 kfree(cell);
218 _leave(" = %d", ret);
219 return ERR_PTR(ret);
220 }
221
222 /*
223 * afs_lookup_cell - Look up or create a cell record.
224 * @net: The network namespace
225 * @name: The name of the cell.
226 * @namesz: The strlen of the cell name.
227 * @vllist: A colon/comma separated list of numeric IP addresses or NULL.
228 * @excl: T if an error should be given if the cell name already exists.
229 *
230 * Look up a cell record by name and query the DNS for VL server addresses if
231 * needed. Note that that actual DNS query is punted off to the manager thread
232 * so that this function can return immediately if interrupted whilst allowing
233 * cell records to be shared even if not yet fully constructed.
234 */
afs_lookup_cell(struct afs_net * net,const char * name,unsigned int namesz,const char * vllist,bool excl)235 struct afs_cell *afs_lookup_cell(struct afs_net *net,
236 const char *name, unsigned int namesz,
237 const char *vllist, bool excl)
238 {
239 struct afs_cell *cell, *candidate, *cursor;
240 struct rb_node *parent, **pp;
241 enum afs_cell_state state;
242 int ret, n;
243
244 _enter("%s,%s", name, vllist);
245
246 if (!excl) {
247 cell = afs_find_cell(net, name, namesz, afs_cell_trace_use_lookup);
248 if (!IS_ERR(cell))
249 goto wait_for_cell;
250 }
251
252 /* Assume we're probably going to create a cell and preallocate and
253 * mostly set up a candidate record. We can then use this to stash the
254 * name, the net namespace and VL server addresses.
255 *
256 * We also want to do this before we hold any locks as it may involve
257 * upcalling to userspace to make DNS queries.
258 */
259 candidate = afs_alloc_cell(net, name, namesz, vllist);
260 if (IS_ERR(candidate)) {
261 _leave(" = %ld", PTR_ERR(candidate));
262 return candidate;
263 }
264
265 /* Find the insertion point and check to see if someone else added a
266 * cell whilst we were allocating.
267 */
268 down_write(&net->cells_lock);
269
270 pp = &net->cells.rb_node;
271 parent = NULL;
272 while (*pp) {
273 parent = *pp;
274 cursor = rb_entry(parent, struct afs_cell, net_node);
275
276 n = strncasecmp(cursor->name, name,
277 min_t(size_t, cursor->name_len, namesz));
278 if (n == 0)
279 n = cursor->name_len - namesz;
280 if (n < 0)
281 pp = &(*pp)->rb_left;
282 else if (n > 0)
283 pp = &(*pp)->rb_right;
284 else
285 goto cell_already_exists;
286 }
287
288 cell = candidate;
289 candidate = NULL;
290 atomic_set(&cell->active, 2);
291 trace_afs_cell(cell->debug_id, refcount_read(&cell->ref), 2, afs_cell_trace_insert);
292 rb_link_node_rcu(&cell->net_node, parent, pp);
293 rb_insert_color(&cell->net_node, &net->cells);
294 up_write(&net->cells_lock);
295
296 afs_queue_cell(cell, afs_cell_trace_get_queue_new);
297
298 wait_for_cell:
299 trace_afs_cell(cell->debug_id, refcount_read(&cell->ref), atomic_read(&cell->active),
300 afs_cell_trace_wait);
301 _debug("wait_for_cell");
302 wait_var_event(&cell->state,
303 ({
304 state = smp_load_acquire(&cell->state); /* vs error */
305 state == AFS_CELL_ACTIVE || state == AFS_CELL_REMOVED;
306 }));
307
308 /* Check the state obtained from the wait check. */
309 if (state == AFS_CELL_REMOVED) {
310 ret = cell->error;
311 goto error;
312 }
313
314 _leave(" = %p [cell]", cell);
315 return cell;
316
317 cell_already_exists:
318 _debug("cell exists");
319 cell = cursor;
320 if (excl) {
321 ret = -EEXIST;
322 } else {
323 afs_use_cell(cursor, afs_cell_trace_use_lookup);
324 ret = 0;
325 }
326 up_write(&net->cells_lock);
327 if (candidate)
328 afs_put_cell(candidate, afs_cell_trace_put_candidate);
329 if (ret == 0)
330 goto wait_for_cell;
331 goto error_noput;
332 error:
333 afs_unuse_cell(net, cell, afs_cell_trace_unuse_lookup);
334 error_noput:
335 _leave(" = %d [error]", ret);
336 return ERR_PTR(ret);
337 }
338
339 /*
340 * set the root cell information
341 * - can be called with a module parameter string
342 * - can be called from a write to /proc/fs/afs/rootcell
343 */
afs_cell_init(struct afs_net * net,const char * rootcell)344 int afs_cell_init(struct afs_net *net, const char *rootcell)
345 {
346 struct afs_cell *old_root, *new_root;
347 const char *cp, *vllist;
348 size_t len;
349
350 _enter("");
351
352 if (!rootcell) {
353 /* module is loaded with no parameters, or built statically.
354 * - in the future we might initialize cell DB here.
355 */
356 _leave(" = 0 [no root]");
357 return 0;
358 }
359
360 cp = strchr(rootcell, ':');
361 if (!cp) {
362 _debug("kAFS: no VL server IP addresses specified");
363 vllist = NULL;
364 len = strlen(rootcell);
365 } else {
366 vllist = cp + 1;
367 len = cp - rootcell;
368 }
369
370 /* allocate a cell record for the root cell */
371 new_root = afs_lookup_cell(net, rootcell, len, vllist, false);
372 if (IS_ERR(new_root)) {
373 _leave(" = %ld", PTR_ERR(new_root));
374 return PTR_ERR(new_root);
375 }
376
377 if (!test_and_set_bit(AFS_CELL_FL_NO_GC, &new_root->flags))
378 afs_use_cell(new_root, afs_cell_trace_use_pin);
379
380 /* install the new cell */
381 down_write(&net->cells_lock);
382 afs_see_cell(new_root, afs_cell_trace_see_ws);
383 old_root = net->ws_cell;
384 net->ws_cell = new_root;
385 up_write(&net->cells_lock);
386
387 afs_unuse_cell(net, old_root, afs_cell_trace_unuse_ws);
388 _leave(" = 0");
389 return 0;
390 }
391
392 /*
393 * Update a cell's VL server address list from the DNS.
394 */
afs_update_cell(struct afs_cell * cell)395 static int afs_update_cell(struct afs_cell *cell)
396 {
397 struct afs_vlserver_list *vllist, *old = NULL, *p;
398 unsigned int min_ttl = READ_ONCE(afs_cell_min_ttl);
399 unsigned int max_ttl = READ_ONCE(afs_cell_max_ttl);
400 time64_t now, expiry = 0;
401 int ret = 0;
402
403 _enter("%s", cell->name);
404
405 vllist = afs_dns_query(cell, &expiry);
406 if (IS_ERR(vllist)) {
407 ret = PTR_ERR(vllist);
408
409 _debug("%s: fail %d", cell->name, ret);
410 if (ret == -ENOMEM)
411 goto out_wake;
412
413 vllist = afs_alloc_vlserver_list(0);
414 if (!vllist) {
415 if (ret >= 0)
416 ret = -ENOMEM;
417 goto out_wake;
418 }
419
420 switch (ret) {
421 case -ENODATA:
422 case -EDESTADDRREQ:
423 vllist->status = DNS_LOOKUP_GOT_NOT_FOUND;
424 break;
425 case -EAGAIN:
426 case -ECONNREFUSED:
427 vllist->status = DNS_LOOKUP_GOT_TEMP_FAILURE;
428 break;
429 default:
430 vllist->status = DNS_LOOKUP_GOT_LOCAL_FAILURE;
431 break;
432 }
433 }
434
435 _debug("%s: got list %d %d", cell->name, vllist->source, vllist->status);
436 cell->dns_status = vllist->status;
437
438 now = ktime_get_real_seconds();
439 if (min_ttl > max_ttl)
440 max_ttl = min_ttl;
441 if (expiry < now + min_ttl)
442 expiry = now + min_ttl;
443 else if (expiry > now + max_ttl)
444 expiry = now + max_ttl;
445
446 _debug("%s: status %d", cell->name, vllist->status);
447 if (vllist->source == DNS_RECORD_UNAVAILABLE) {
448 switch (vllist->status) {
449 case DNS_LOOKUP_GOT_NOT_FOUND:
450 /* The DNS said that the cell does not exist or there
451 * weren't any addresses to be had.
452 */
453 cell->dns_expiry = expiry;
454 break;
455
456 case DNS_LOOKUP_BAD:
457 case DNS_LOOKUP_GOT_LOCAL_FAILURE:
458 case DNS_LOOKUP_GOT_TEMP_FAILURE:
459 case DNS_LOOKUP_GOT_NS_FAILURE:
460 default:
461 cell->dns_expiry = now + 10;
462 break;
463 }
464 } else {
465 cell->dns_expiry = expiry;
466 }
467
468 /* Replace the VL server list if the new record has servers or the old
469 * record doesn't.
470 */
471 write_lock(&cell->vl_servers_lock);
472 p = rcu_dereference_protected(cell->vl_servers, true);
473 if (vllist->nr_servers > 0 || p->nr_servers == 0) {
474 rcu_assign_pointer(cell->vl_servers, vllist);
475 cell->dns_source = vllist->source;
476 old = p;
477 }
478 write_unlock(&cell->vl_servers_lock);
479 afs_put_vlserverlist(cell->net, old);
480
481 out_wake:
482 smp_store_release(&cell->dns_lookup_count,
483 cell->dns_lookup_count + 1); /* vs source/status */
484 wake_up_var(&cell->dns_lookup_count);
485 _leave(" = %d", ret);
486 return ret;
487 }
488
489 /*
490 * Destroy a cell record
491 */
afs_cell_destroy(struct rcu_head * rcu)492 static void afs_cell_destroy(struct rcu_head *rcu)
493 {
494 struct afs_cell *cell = container_of(rcu, struct afs_cell, rcu);
495 struct afs_net *net = cell->net;
496 int r;
497
498 _enter("%p{%s}", cell, cell->name);
499
500 r = refcount_read(&cell->ref);
501 ASSERTCMP(r, ==, 0);
502 trace_afs_cell(cell->debug_id, r, atomic_read(&cell->active), afs_cell_trace_free);
503
504 afs_put_vlserverlist(net, rcu_access_pointer(cell->vl_servers));
505 afs_unuse_cell(net, cell->alias_of, afs_cell_trace_unuse_alias);
506 key_put(cell->anonymous_key);
507 kfree(cell->name);
508 kfree(cell);
509
510 afs_dec_cells_outstanding(net);
511 _leave(" [destroyed]");
512 }
513
514 /*
515 * Queue the cell manager.
516 */
afs_queue_cell_manager(struct afs_net * net)517 static void afs_queue_cell_manager(struct afs_net *net)
518 {
519 int outstanding = atomic_inc_return(&net->cells_outstanding);
520
521 _enter("%d", outstanding);
522
523 if (!queue_work(afs_wq, &net->cells_manager))
524 afs_dec_cells_outstanding(net);
525 }
526
527 /*
528 * Cell management timer. We have an increment on cells_outstanding that we
529 * need to pass along to the work item.
530 */
afs_cells_timer(struct timer_list * timer)531 void afs_cells_timer(struct timer_list *timer)
532 {
533 struct afs_net *net = container_of(timer, struct afs_net, cells_timer);
534
535 _enter("");
536 if (!queue_work(afs_wq, &net->cells_manager))
537 afs_dec_cells_outstanding(net);
538 }
539
540 /*
541 * Get a reference on a cell record.
542 */
afs_get_cell(struct afs_cell * cell,enum afs_cell_trace reason)543 struct afs_cell *afs_get_cell(struct afs_cell *cell, enum afs_cell_trace reason)
544 {
545 int r;
546
547 __refcount_inc(&cell->ref, &r);
548 trace_afs_cell(cell->debug_id, r + 1, atomic_read(&cell->active), reason);
549 return cell;
550 }
551
552 /*
553 * Drop a reference on a cell record.
554 */
afs_put_cell(struct afs_cell * cell,enum afs_cell_trace reason)555 void afs_put_cell(struct afs_cell *cell, enum afs_cell_trace reason)
556 {
557 if (cell) {
558 unsigned int debug_id = cell->debug_id;
559 unsigned int a;
560 bool zero;
561 int r;
562
563 a = atomic_read(&cell->active);
564 zero = __refcount_dec_and_test(&cell->ref, &r);
565 trace_afs_cell(debug_id, r - 1, a, reason);
566 if (zero) {
567 a = atomic_read(&cell->active);
568 WARN(a != 0, "Cell active count %u > 0\n", a);
569 call_rcu(&cell->rcu, afs_cell_destroy);
570 }
571 }
572 }
573
574 /*
575 * Note a cell becoming more active.
576 */
afs_use_cell(struct afs_cell * cell,enum afs_cell_trace reason)577 struct afs_cell *afs_use_cell(struct afs_cell *cell, enum afs_cell_trace reason)
578 {
579 int r, a;
580
581 r = refcount_read(&cell->ref);
582 WARN_ON(r == 0);
583 a = atomic_inc_return(&cell->active);
584 trace_afs_cell(cell->debug_id, r, a, reason);
585 return cell;
586 }
587
588 /*
589 * Record a cell becoming less active. When the active counter reaches 1, it
590 * is scheduled for destruction, but may get reactivated.
591 */
afs_unuse_cell(struct afs_net * net,struct afs_cell * cell,enum afs_cell_trace reason)592 void afs_unuse_cell(struct afs_net *net, struct afs_cell *cell, enum afs_cell_trace reason)
593 {
594 unsigned int debug_id;
595 time64_t now, expire_delay;
596 int r, a;
597
598 if (!cell)
599 return;
600
601 _enter("%s", cell->name);
602
603 now = ktime_get_real_seconds();
604 cell->last_inactive = now;
605 expire_delay = 0;
606 if (cell->vl_servers->nr_servers)
607 expire_delay = afs_cell_gc_delay;
608
609 debug_id = cell->debug_id;
610 r = refcount_read(&cell->ref);
611 a = atomic_dec_return(&cell->active);
612 trace_afs_cell(debug_id, r, a, reason);
613 WARN_ON(a == 0);
614 if (a == 1)
615 /* 'cell' may now be garbage collected. */
616 afs_set_cell_timer(net, expire_delay);
617 }
618
619 /*
620 * Note that a cell has been seen.
621 */
afs_see_cell(struct afs_cell * cell,enum afs_cell_trace reason)622 void afs_see_cell(struct afs_cell *cell, enum afs_cell_trace reason)
623 {
624 int r, a;
625
626 r = refcount_read(&cell->ref);
627 a = atomic_read(&cell->active);
628 trace_afs_cell(cell->debug_id, r, a, reason);
629 }
630
631 /*
632 * Queue a cell for management, giving the workqueue a ref to hold.
633 */
afs_queue_cell(struct afs_cell * cell,enum afs_cell_trace reason)634 void afs_queue_cell(struct afs_cell *cell, enum afs_cell_trace reason)
635 {
636 afs_get_cell(cell, reason);
637 if (!queue_work(afs_wq, &cell->manager))
638 afs_put_cell(cell, afs_cell_trace_put_queue_fail);
639 }
640
641 /*
642 * Allocate a key to use as a placeholder for anonymous user security.
643 */
afs_alloc_anon_key(struct afs_cell * cell)644 static int afs_alloc_anon_key(struct afs_cell *cell)
645 {
646 struct key *key;
647 char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp;
648
649 /* Create a key to represent an anonymous user. */
650 memcpy(keyname, "afs@", 4);
651 dp = keyname + 4;
652 cp = cell->name;
653 do {
654 *dp++ = tolower(*cp);
655 } while (*cp++);
656
657 key = rxrpc_get_null_key(keyname);
658 if (IS_ERR(key))
659 return PTR_ERR(key);
660
661 cell->anonymous_key = key;
662
663 _debug("anon key %p{%x}",
664 cell->anonymous_key, key_serial(cell->anonymous_key));
665 return 0;
666 }
667
668 /*
669 * Activate a cell.
670 */
afs_activate_cell(struct afs_net * net,struct afs_cell * cell)671 static int afs_activate_cell(struct afs_net *net, struct afs_cell *cell)
672 {
673 struct hlist_node **p;
674 struct afs_cell *pcell;
675 int ret;
676
677 if (!cell->anonymous_key) {
678 ret = afs_alloc_anon_key(cell);
679 if (ret < 0)
680 return ret;
681 }
682
683 ret = afs_proc_cell_setup(cell);
684 if (ret < 0)
685 return ret;
686
687 mutex_lock(&net->proc_cells_lock);
688 for (p = &net->proc_cells.first; *p; p = &(*p)->next) {
689 pcell = hlist_entry(*p, struct afs_cell, proc_link);
690 if (strcmp(cell->name, pcell->name) < 0)
691 break;
692 }
693
694 cell->proc_link.pprev = p;
695 cell->proc_link.next = *p;
696 rcu_assign_pointer(*p, &cell->proc_link.next);
697 if (cell->proc_link.next)
698 cell->proc_link.next->pprev = &cell->proc_link.next;
699
700 afs_dynroot_mkdir(net, cell);
701 mutex_unlock(&net->proc_cells_lock);
702 return 0;
703 }
704
705 /*
706 * Deactivate a cell.
707 */
afs_deactivate_cell(struct afs_net * net,struct afs_cell * cell)708 static void afs_deactivate_cell(struct afs_net *net, struct afs_cell *cell)
709 {
710 _enter("%s", cell->name);
711
712 afs_proc_cell_remove(cell);
713
714 mutex_lock(&net->proc_cells_lock);
715 hlist_del_rcu(&cell->proc_link);
716 afs_dynroot_rmdir(net, cell);
717 mutex_unlock(&net->proc_cells_lock);
718
719 _leave("");
720 }
721
722 /*
723 * Manage a cell record, initialising and destroying it, maintaining its DNS
724 * records.
725 */
afs_manage_cell(struct afs_cell * cell)726 static void afs_manage_cell(struct afs_cell *cell)
727 {
728 struct afs_net *net = cell->net;
729 int ret, active;
730
731 _enter("%s", cell->name);
732
733 again:
734 _debug("state %u", cell->state);
735 switch (cell->state) {
736 case AFS_CELL_INACTIVE:
737 case AFS_CELL_FAILED:
738 down_write(&net->cells_lock);
739 active = 1;
740 if (atomic_try_cmpxchg_relaxed(&cell->active, &active, 0)) {
741 rb_erase(&cell->net_node, &net->cells);
742 trace_afs_cell(cell->debug_id, refcount_read(&cell->ref), 0,
743 afs_cell_trace_unuse_delete);
744 smp_store_release(&cell->state, AFS_CELL_REMOVED);
745 }
746 up_write(&net->cells_lock);
747 if (cell->state == AFS_CELL_REMOVED) {
748 wake_up_var(&cell->state);
749 goto final_destruction;
750 }
751 if (cell->state == AFS_CELL_FAILED)
752 goto done;
753 smp_store_release(&cell->state, AFS_CELL_UNSET);
754 wake_up_var(&cell->state);
755 goto again;
756
757 case AFS_CELL_UNSET:
758 smp_store_release(&cell->state, AFS_CELL_ACTIVATING);
759 wake_up_var(&cell->state);
760 goto again;
761
762 case AFS_CELL_ACTIVATING:
763 ret = afs_activate_cell(net, cell);
764 if (ret < 0)
765 goto activation_failed;
766
767 smp_store_release(&cell->state, AFS_CELL_ACTIVE);
768 wake_up_var(&cell->state);
769 goto again;
770
771 case AFS_CELL_ACTIVE:
772 if (atomic_read(&cell->active) > 1) {
773 if (test_and_clear_bit(AFS_CELL_FL_DO_LOOKUP, &cell->flags)) {
774 ret = afs_update_cell(cell);
775 if (ret < 0)
776 cell->error = ret;
777 }
778 goto done;
779 }
780 smp_store_release(&cell->state, AFS_CELL_DEACTIVATING);
781 wake_up_var(&cell->state);
782 goto again;
783
784 case AFS_CELL_DEACTIVATING:
785 if (atomic_read(&cell->active) > 1)
786 goto reverse_deactivation;
787 afs_deactivate_cell(net, cell);
788 smp_store_release(&cell->state, AFS_CELL_INACTIVE);
789 wake_up_var(&cell->state);
790 goto again;
791
792 case AFS_CELL_REMOVED:
793 goto done;
794
795 default:
796 break;
797 }
798 _debug("bad state %u", cell->state);
799 BUG(); /* Unhandled state */
800
801 activation_failed:
802 cell->error = ret;
803 afs_deactivate_cell(net, cell);
804
805 smp_store_release(&cell->state, AFS_CELL_FAILED); /* vs error */
806 wake_up_var(&cell->state);
807 goto again;
808
809 reverse_deactivation:
810 smp_store_release(&cell->state, AFS_CELL_ACTIVE);
811 wake_up_var(&cell->state);
812 _leave(" [deact->act]");
813 return;
814
815 done:
816 _leave(" [done %u]", cell->state);
817 return;
818
819 final_destruction:
820 /* The root volume is pinning the cell */
821 afs_put_volume(cell->net, cell->root_volume, afs_volume_trace_put_cell_root);
822 cell->root_volume = NULL;
823 afs_put_cell(cell, afs_cell_trace_put_destroy);
824 }
825
afs_manage_cell_work(struct work_struct * work)826 static void afs_manage_cell_work(struct work_struct *work)
827 {
828 struct afs_cell *cell = container_of(work, struct afs_cell, manager);
829
830 afs_manage_cell(cell);
831 afs_put_cell(cell, afs_cell_trace_put_queue_work);
832 }
833
834 /*
835 * Manage the records of cells known to a network namespace. This includes
836 * updating the DNS records and garbage collecting unused cells that were
837 * automatically added.
838 *
839 * Note that constructed cell records may only be removed from net->cells by
840 * this work item, so it is safe for this work item to stash a cursor pointing
841 * into the tree and then return to caller (provided it skips cells that are
842 * still under construction).
843 *
844 * Note also that we were given an increment on net->cells_outstanding by
845 * whoever queued us that we need to deal with before returning.
846 */
afs_manage_cells(struct work_struct * work)847 void afs_manage_cells(struct work_struct *work)
848 {
849 struct afs_net *net = container_of(work, struct afs_net, cells_manager);
850 struct rb_node *cursor;
851 time64_t now = ktime_get_real_seconds(), next_manage = TIME64_MAX;
852 bool purging = !net->live;
853
854 _enter("");
855
856 /* Trawl the cell database looking for cells that have expired from
857 * lack of use and cells whose DNS results have expired and dispatch
858 * their managers.
859 */
860 down_read(&net->cells_lock);
861
862 for (cursor = rb_first(&net->cells); cursor; cursor = rb_next(cursor)) {
863 struct afs_cell *cell =
864 rb_entry(cursor, struct afs_cell, net_node);
865 unsigned active;
866 bool sched_cell = false;
867
868 active = atomic_read(&cell->active);
869 trace_afs_cell(cell->debug_id, refcount_read(&cell->ref),
870 active, afs_cell_trace_manage);
871
872 ASSERTCMP(active, >=, 1);
873
874 if (purging) {
875 if (test_and_clear_bit(AFS_CELL_FL_NO_GC, &cell->flags)) {
876 active = atomic_dec_return(&cell->active);
877 trace_afs_cell(cell->debug_id, refcount_read(&cell->ref),
878 active, afs_cell_trace_unuse_pin);
879 }
880 }
881
882 if (active == 1) {
883 struct afs_vlserver_list *vllist;
884 time64_t expire_at = cell->last_inactive;
885
886 read_lock(&cell->vl_servers_lock);
887 vllist = rcu_dereference_protected(
888 cell->vl_servers,
889 lockdep_is_held(&cell->vl_servers_lock));
890 if (vllist->nr_servers > 0)
891 expire_at += afs_cell_gc_delay;
892 read_unlock(&cell->vl_servers_lock);
893 if (purging || expire_at <= now)
894 sched_cell = true;
895 else if (expire_at < next_manage)
896 next_manage = expire_at;
897 }
898
899 if (!purging) {
900 if (test_bit(AFS_CELL_FL_DO_LOOKUP, &cell->flags))
901 sched_cell = true;
902 }
903
904 if (sched_cell)
905 afs_queue_cell(cell, afs_cell_trace_get_queue_manage);
906 }
907
908 up_read(&net->cells_lock);
909
910 /* Update the timer on the way out. We have to pass an increment on
911 * cells_outstanding in the namespace that we are in to the timer or
912 * the work scheduler.
913 */
914 if (!purging && next_manage < TIME64_MAX) {
915 now = ktime_get_real_seconds();
916
917 if (next_manage - now <= 0) {
918 if (queue_work(afs_wq, &net->cells_manager))
919 atomic_inc(&net->cells_outstanding);
920 } else {
921 afs_set_cell_timer(net, next_manage - now);
922 }
923 }
924
925 afs_dec_cells_outstanding(net);
926 _leave(" [%d]", atomic_read(&net->cells_outstanding));
927 }
928
929 /*
930 * Purge in-memory cell database.
931 */
afs_cell_purge(struct afs_net * net)932 void afs_cell_purge(struct afs_net *net)
933 {
934 struct afs_cell *ws;
935
936 _enter("");
937
938 down_write(&net->cells_lock);
939 ws = net->ws_cell;
940 net->ws_cell = NULL;
941 up_write(&net->cells_lock);
942 afs_unuse_cell(net, ws, afs_cell_trace_unuse_ws);
943
944 _debug("del timer");
945 if (del_timer_sync(&net->cells_timer))
946 atomic_dec(&net->cells_outstanding);
947
948 _debug("kick mgr");
949 afs_queue_cell_manager(net);
950
951 _debug("wait");
952 wait_var_event(&net->cells_outstanding,
953 !atomic_read(&net->cells_outstanding));
954 _leave("");
955 }
956