1 /* AFS cell and server record management
3 * Copyright (C) 2002, 2017 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
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>
22 static unsigned __read_mostly afs_cell_gc_delay = 10;
24 static void afs_manage_cell(struct work_struct *);
26 static void afs_dec_cells_outstanding(struct afs_net *net)
28 if (atomic_dec_and_test(&net->cells_outstanding))
29 wake_up_var(&net->cells_outstanding);
33 * Set the cell timer to fire after a given delay, assuming it's not already
34 * set for an earlier time.
36 static void afs_set_cell_timer(struct afs_net *net, time64_t delay)
39 atomic_inc(&net->cells_outstanding);
40 if (timer_reduce(&net->cells_timer, jiffies + delay * HZ))
41 afs_dec_cells_outstanding(net);
46 * Look up and get an activation reference on a cell record under RCU
47 * conditions. The caller must hold the RCU read lock.
49 struct afs_cell *afs_lookup_cell_rcu(struct afs_net *net,
50 const char *name, unsigned int namesz)
52 struct afs_cell *cell = NULL;
54 int n, seq = 0, ret = 0;
56 _enter("%*.*s", namesz, namesz, name);
58 if (name && namesz == 0)
59 return ERR_PTR(-EINVAL);
60 if (namesz > AFS_MAXCELLNAME)
61 return ERR_PTR(-ENAMETOOLONG);
64 /* Unfortunately, rbtree walking doesn't give reliable results
65 * under just the RCU read lock, so we have to check for
69 afs_put_cell(net, cell);
73 read_seqbegin_or_lock(&net->cells_lock, &seq);
76 cell = rcu_dereference_raw(net->ws_cell);
85 p = rcu_dereference_raw(net->cells.rb_node);
87 cell = rb_entry(p, struct afs_cell, net_node);
89 n = strncasecmp(cell->name, name,
90 min_t(size_t, cell->name_len, namesz));
92 n = cell->name_len - namesz;
94 p = rcu_dereference_raw(p->rb_left);
96 p = rcu_dereference_raw(p->rb_right);
98 if (atomic_inc_not_zero(&cell->usage)) {
102 /* We want to repeat the search, this time with
103 * the lock properly locked.
109 } while (need_seqretry(&net->cells_lock, seq));
111 done_seqretry(&net->cells_lock, seq);
113 return ret == 0 ? cell : ERR_PTR(ret);
117 * Set up a cell record and fill in its name, VL server address list and
118 * allocate an anonymous key
120 static struct afs_cell *afs_alloc_cell(struct afs_net *net,
121 const char *name, unsigned int namelen,
122 const char *addresses)
124 struct afs_cell *cell;
129 return ERR_PTR(-EINVAL);
130 if (namelen > AFS_MAXCELLNAME) {
131 _leave(" = -ENAMETOOLONG");
132 return ERR_PTR(-ENAMETOOLONG);
134 if (namelen == 5 && memcmp(name, "@cell", 5) == 0)
135 return ERR_PTR(-EINVAL);
137 _enter("%*.*s,%s", namelen, namelen, name, addresses);
139 cell = kzalloc(sizeof(struct afs_cell), GFP_KERNEL);
141 _leave(" = -ENOMEM");
142 return ERR_PTR(-ENOMEM);
146 cell->name_len = namelen;
147 for (i = 0; i < namelen; i++)
148 cell->name[i] = tolower(name[i]);
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);
158 /* Fill in the VL server list if we were given a list of addresses to
162 struct afs_vlserver_list *vllist;
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);
172 rcu_assign_pointer(cell->vl_servers, vllist);
173 cell->dns_expiry = TIME64_MAX;
176 _leave(" = %p", cell);
181 printk(KERN_ERR "kAFS: bad VL server IP address\n");
183 _leave(" = %d", ret);
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.
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.
200 struct afs_cell *afs_lookup_cell(struct afs_net *net,
201 const char *name, unsigned int namesz,
202 const char *vllist, bool excl)
204 struct afs_cell *cell, *candidate, *cursor;
205 struct rb_node *parent, **pp;
208 _enter("%s,%s", name, vllist);
212 cell = afs_lookup_cell_rcu(net, name, namesz);
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.
222 * We also want to do this before we hold any locks as it may involve
223 * upcalling to userspace to make DNS queries.
225 candidate = afs_alloc_cell(net, name, namesz, vllist);
226 if (IS_ERR(candidate)) {
227 _leave(" = %ld", PTR_ERR(candidate));
231 /* Find the insertion point and check to see if someone else added a
232 * cell whilst we were allocating.
234 write_seqlock(&net->cells_lock);
236 pp = &net->cells.rb_node;
240 cursor = rb_entry(parent, struct afs_cell, net_node);
242 n = strncasecmp(cursor->name, name,
243 min_t(size_t, cursor->name_len, namesz));
245 n = cursor->name_len - namesz;
247 pp = &(*pp)->rb_left;
249 pp = &(*pp)->rb_right;
251 goto cell_already_exists;
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);
261 queue_work(afs_wq, &cell->manager);
264 _debug("wait_for_cell");
265 ret = wait_on_bit(&cell->flags, AFS_CELL_FL_NOT_READY, TASK_INTERRUPTIBLE);
268 switch (READ_ONCE(cell->state)) {
269 case AFS_CELL_FAILED:
273 _debug("weird %u %d", cell->state, cell->error);
275 case AFS_CELL_ACTIVE:
279 _leave(" = %p [cell]", cell);
283 _debug("cell exists");
288 afs_get_cell(cursor);
291 write_sequnlock(&net->cells_lock);
297 afs_put_cell(net, cell);
299 _leave(" = %d [error]", ret);
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
308 int afs_cell_init(struct afs_net *net, const char *rootcell)
310 struct afs_cell *old_root, *new_root;
311 const char *cp, *vllist;
317 /* module is loaded with no parameters, or built statically.
318 * - in the future we might initialize cell DB here.
320 _leave(" = 0 [no root]");
324 cp = strchr(rootcell, ':');
326 _debug("kAFS: no VL server IP addresses specified");
328 len = strlen(rootcell);
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);
341 if (!test_and_set_bit(AFS_CELL_FL_NO_GC, &new_root->flags))
342 afs_get_cell(new_root);
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);
350 afs_put_cell(net, old_root);
356 * Update a cell's VL server address list from the DNS.
358 static void afs_update_cell(struct afs_cell *cell)
360 struct afs_vlserver_list *vllist, *old;
361 time64_t now, expiry;
363 _enter("%s", cell->name);
365 vllist = afs_dns_query(cell, &expiry);
366 if (IS_ERR(vllist)) {
367 switch (PTR_ERR(vllist)) {
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;
378 set_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags);
379 cell->dns_expiry = ktime_get_real_seconds() + 10;
383 cell->error = -EDESTADDRREQ;
385 clear_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags);
386 clear_bit(AFS_CELL_FL_NOT_FOUND, &cell->flags);
388 /* Exclusion on changing vl_addrs is achieved by a
389 * non-reentrant work item.
391 old = rcu_dereference_protected(cell->vl_servers, true);
392 rcu_assign_pointer(cell->vl_servers, vllist);
393 cell->dns_expiry = expiry;
396 afs_put_vlserverlist(cell->net, old);
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);
402 now = ktime_get_real_seconds();
403 afs_set_cell_timer(cell->net, cell->dns_expiry - now);
408 * Destroy a cell record
410 static void afs_cell_destroy(struct rcu_head *rcu)
412 struct afs_cell *cell = container_of(rcu, struct afs_cell, rcu);
414 _enter("%p{%s}", cell, cell->name);
416 ASSERTCMP(atomic_read(&cell->usage), ==, 0);
418 afs_put_vlserverlist(cell->net, rcu_access_pointer(cell->vl_servers));
419 key_put(cell->anonymous_key);
422 _leave(" [destroyed]");
426 * Queue the cell manager.
428 static void afs_queue_cell_manager(struct afs_net *net)
430 int outstanding = atomic_inc_return(&net->cells_outstanding);
432 _enter("%d", outstanding);
434 if (!queue_work(afs_wq, &net->cells_manager))
435 afs_dec_cells_outstanding(net);
439 * Cell management timer. We have an increment on cells_outstanding that we
440 * need to pass along to the work item.
442 void afs_cells_timer(struct timer_list *timer)
444 struct afs_net *net = container_of(timer, struct afs_net, cells_timer);
447 if (!queue_work(afs_wq, &net->cells_manager))
448 afs_dec_cells_outstanding(net);
452 * Get a reference on a cell record.
454 struct afs_cell *afs_get_cell(struct afs_cell *cell)
456 atomic_inc(&cell->usage);
461 * Drop a reference on a cell record.
463 void afs_put_cell(struct afs_net *net, struct afs_cell *cell)
465 time64_t now, expire_delay;
470 _enter("%s", cell->name);
472 now = ktime_get_real_seconds();
473 cell->last_inactive = now;
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;
479 if (atomic_dec_return(&cell->usage) > 1)
482 /* 'cell' may now be garbage collected. */
483 afs_set_cell_timer(net, expire_delay);
487 * Allocate a key to use as a placeholder for anonymous user security.
489 static int afs_alloc_anon_key(struct afs_cell *cell)
492 char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp;
494 /* Create a key to represent an anonymous user. */
495 memcpy(keyname, "afs@", 4);
499 *dp++ = tolower(*cp);
502 key = rxrpc_get_null_key(keyname);
506 cell->anonymous_key = key;
508 _debug("anon key %p{%x}",
509 cell->anonymous_key, key_serial(cell->anonymous_key));
516 static int afs_activate_cell(struct afs_net *net, struct afs_cell *cell)
518 struct hlist_node **p;
519 struct afs_cell *pcell;
522 if (!cell->anonymous_key) {
523 ret = afs_alloc_anon_key(cell);
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),
535 ret = afs_proc_cell_setup(cell);
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)
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;
552 afs_dynroot_mkdir(net, cell);
553 mutex_unlock(&net->proc_cells_lock);
560 static void afs_deactivate_cell(struct afs_net *net, struct afs_cell *cell)
562 _enter("%s", cell->name);
564 afs_proc_cell_remove(cell);
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);
571 #ifdef CONFIG_AFS_FSCACHE
572 fscache_relinquish_cookie(cell->cache, NULL, false);
580 * Manage a cell record, initialising and destroying it, maintaining its DNS
583 static void afs_manage_cell(struct work_struct *work)
585 struct afs_cell *cell = container_of(work, struct afs_cell, manager);
586 struct afs_net *net = cell->net;
590 _enter("%s", cell->name);
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);
599 deleted = atomic_try_cmpxchg_relaxed(&cell->usage, &usage, 0);
601 rb_erase(&cell->net_node, &net->cells);
602 write_sequnlock(&net->cells_lock);
604 goto final_destruction;
605 if (cell->state == AFS_CELL_FAILED)
607 cell->state = AFS_CELL_UNSET;
611 cell->state = AFS_CELL_ACTIVATING;
614 case AFS_CELL_ACTIVATING:
615 ret = afs_activate_cell(net, cell);
617 goto activation_failed;
619 cell->state = AFS_CELL_ACTIVE;
621 clear_bit(AFS_CELL_FL_NOT_READY, &cell->flags);
622 wake_up_bit(&cell->flags, AFS_CELL_FL_NOT_READY);
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);
632 cell->state = AFS_CELL_DEACTIVATING;
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;
646 _debug("bad state %u", cell->state);
647 BUG(); /* Unhandled state */
651 afs_deactivate_cell(net, cell);
653 cell->state = AFS_CELL_FAILED;
655 if (test_and_clear_bit(AFS_CELL_FL_NOT_READY, &cell->flags))
656 wake_up_bit(&cell->flags, AFS_CELL_FL_NOT_READY);
659 reverse_deactivation:
660 cell->state = AFS_CELL_ACTIVE;
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]");
668 _leave(" [done %u]", cell->state);
672 call_rcu(&cell->rcu, afs_cell_destroy);
673 afs_dec_cells_outstanding(net);
674 _leave(" [destruct %d]", atomic_read(&net->cells_outstanding));
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.
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).
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.
690 void afs_manage_cells(struct work_struct *work)
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;
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
703 read_seqlock_excl(&net->cells_lock);
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);
709 bool sched_cell = false;
711 usage = atomic_read(&cell->usage);
712 _debug("manage %s %u", cell->name, usage);
714 ASSERTCMP(usage, >=, 1);
717 if (test_and_clear_bit(AFS_CELL_FL_NO_GC, &cell->flags))
718 usage = atomic_dec_return(&cell->usage);
719 ASSERTCMP(usage, ==, 1);
723 time64_t expire_at = cell->last_inactive;
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)
730 else if (expire_at < next_manage)
731 next_manage = expire_at;
735 if (cell->dns_expiry <= now)
737 else if (cell->dns_expiry <= next_manage)
738 next_manage = cell->dns_expiry;
742 queue_work(afs_wq, &cell->manager);
745 read_sequnlock_excl(&net->cells_lock);
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.
751 if (!purging && next_manage < TIME64_MAX) {
752 now = ktime_get_real_seconds();
754 if (next_manage - now <= 0) {
755 if (queue_work(afs_wq, &net->cells_manager))
756 atomic_inc(&net->cells_outstanding);
758 afs_set_cell_timer(net, next_manage - now);
762 afs_dec_cells_outstanding(net);
763 _leave(" [%d]", atomic_read(&net->cells_outstanding));
767 * Purge in-memory cell database.
769 void afs_cell_purge(struct afs_net *net)
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);
782 if (del_timer_sync(&net->cells_timer))
783 atomic_dec(&net->cells_outstanding);
786 afs_queue_cell_manager(net);
789 wait_var_event(&net->cells_outstanding,
790 !atomic_read(&net->cells_outstanding));