1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* AFS cell and server record management
4 * Copyright (C) 2002, 2017 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #include <linux/slab.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>
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;
22 static void afs_manage_cell(struct work_struct *);
24 static void afs_dec_cells_outstanding(struct afs_net *net)
26 if (atomic_dec_and_test(&net->cells_outstanding))
27 wake_up_var(&net->cells_outstanding);
31 * Set the cell timer to fire after a given delay, assuming it's not already
32 * set for an earlier time.
34 static void afs_set_cell_timer(struct afs_net *net, time64_t delay)
37 atomic_inc(&net->cells_outstanding);
38 if (timer_reduce(&net->cells_timer, jiffies + delay * HZ))
39 afs_dec_cells_outstanding(net);
44 * Look up and get an activation reference on a cell record under RCU
45 * conditions. The caller must hold the RCU read lock.
47 struct afs_cell *afs_lookup_cell_rcu(struct afs_net *net,
48 const char *name, unsigned int namesz)
50 struct afs_cell *cell = NULL;
52 int n, seq = 0, ret = 0;
54 _enter("%*.*s", namesz, namesz, name);
56 if (name && namesz == 0)
57 return ERR_PTR(-EINVAL);
58 if (namesz > AFS_MAXCELLNAME)
59 return ERR_PTR(-ENAMETOOLONG);
62 /* Unfortunately, rbtree walking doesn't give reliable results
63 * under just the RCU read lock, so we have to check for
67 afs_put_cell(net, cell);
71 read_seqbegin_or_lock(&net->cells_lock, &seq);
74 cell = rcu_dereference_raw(net->ws_cell);
84 p = rcu_dereference_raw(net->cells.rb_node);
86 cell = rb_entry(p, struct afs_cell, net_node);
88 n = strncasecmp(cell->name, name,
89 min_t(size_t, cell->name_len, namesz));
91 n = cell->name_len - namesz;
93 p = rcu_dereference_raw(p->rb_left);
95 p = rcu_dereference_raw(p->rb_right);
97 if (atomic_inc_not_zero(&cell->usage)) {
101 /* We want to repeat the search, this time with
102 * the lock properly locked.
108 } while (need_seqretry(&net->cells_lock, seq));
110 done_seqretry(&net->cells_lock, seq);
112 if (ret != 0 && cell)
113 afs_put_cell(net, cell);
115 return ret == 0 ? cell : ERR_PTR(ret);
119 * Set up a cell record and fill in its name, VL server address list and
120 * allocate an anonymous key
122 static struct afs_cell *afs_alloc_cell(struct afs_net *net,
123 const char *name, unsigned int namelen,
124 const char *addresses)
126 struct afs_vlserver_list *vllist;
127 struct afs_cell *cell;
132 return ERR_PTR(-EINVAL);
133 if (namelen > AFS_MAXCELLNAME) {
134 _leave(" = -ENAMETOOLONG");
135 return ERR_PTR(-ENAMETOOLONG);
138 /* Prohibit cell names that contain unprintable chars, '/' and '@' or
139 * that begin with a dot. This also precludes "@cell".
142 return ERR_PTR(-EINVAL);
143 for (i = 0; i < namelen; i++) {
145 if (!isprint(ch) || ch == '/' || ch == '@')
146 return ERR_PTR(-EINVAL);
149 _enter("%*.*s,%s", namelen, namelen, name, addresses);
151 cell = kzalloc(sizeof(struct afs_cell), GFP_KERNEL);
153 _leave(" = -ENOMEM");
154 return ERR_PTR(-ENOMEM);
157 cell->name = kmalloc(namelen + 1, GFP_KERNEL);
160 return ERR_PTR(-ENOMEM);
164 cell->name_len = namelen;
165 for (i = 0; i < namelen; i++)
166 cell->name[i] = tolower(name[i]);
169 atomic_set(&cell->usage, 2);
170 INIT_WORK(&cell->manager, afs_manage_cell);
171 cell->volumes = RB_ROOT;
172 INIT_HLIST_HEAD(&cell->proc_volumes);
173 seqlock_init(&cell->volume_lock);
174 cell->fs_servers = RB_ROOT;
175 seqlock_init(&cell->fs_lock);
176 rwlock_init(&cell->vl_servers_lock);
177 cell->flags = (1 << AFS_CELL_FL_CHECK_ALIAS);
179 /* Provide a VL server list, filling it in if we were given a list of
183 vllist = afs_parse_text_addrs(net,
184 addresses, strlen(addresses), ':',
185 VL_SERVICE, AFS_VL_PORT);
186 if (IS_ERR(vllist)) {
187 ret = PTR_ERR(vllist);
191 vllist->source = DNS_RECORD_FROM_CONFIG;
192 vllist->status = DNS_LOOKUP_NOT_DONE;
193 cell->dns_expiry = TIME64_MAX;
196 vllist = afs_alloc_vlserver_list(0);
199 vllist->source = DNS_RECORD_UNAVAILABLE;
200 vllist->status = DNS_LOOKUP_NOT_DONE;
201 cell->dns_expiry = ktime_get_real_seconds();
204 rcu_assign_pointer(cell->vl_servers, vllist);
206 cell->dns_source = vllist->source;
207 cell->dns_status = vllist->status;
208 smp_store_release(&cell->dns_lookup_count, 1); /* vs source/status */
210 _leave(" = %p", cell);
215 printk(KERN_ERR "kAFS: bad VL server IP address\n");
219 _leave(" = %d", ret);
224 * afs_lookup_cell - Look up or create a cell record.
225 * @net: The network namespace
226 * @name: The name of the cell.
227 * @namesz: The strlen of the cell name.
228 * @vllist: A colon/comma separated list of numeric IP addresses or NULL.
229 * @excl: T if an error should be given if the cell name already exists.
231 * Look up a cell record by name and query the DNS for VL server addresses if
232 * needed. Note that that actual DNS query is punted off to the manager thread
233 * so that this function can return immediately if interrupted whilst allowing
234 * cell records to be shared even if not yet fully constructed.
236 struct afs_cell *afs_lookup_cell(struct afs_net *net,
237 const char *name, unsigned int namesz,
238 const char *vllist, bool excl)
240 struct afs_cell *cell, *candidate, *cursor;
241 struct rb_node *parent, **pp;
242 enum afs_cell_state state;
245 _enter("%s,%s", name, vllist);
249 cell = afs_lookup_cell_rcu(net, name, namesz);
255 /* Assume we're probably going to create a cell and preallocate and
256 * mostly set up a candidate record. We can then use this to stash the
257 * name, the net namespace and VL server addresses.
259 * We also want to do this before we hold any locks as it may involve
260 * upcalling to userspace to make DNS queries.
262 candidate = afs_alloc_cell(net, name, namesz, vllist);
263 if (IS_ERR(candidate)) {
264 _leave(" = %ld", PTR_ERR(candidate));
268 /* Find the insertion point and check to see if someone else added a
269 * cell whilst we were allocating.
271 write_seqlock(&net->cells_lock);
273 pp = &net->cells.rb_node;
277 cursor = rb_entry(parent, struct afs_cell, net_node);
279 n = strncasecmp(cursor->name, name,
280 min_t(size_t, cursor->name_len, namesz));
282 n = cursor->name_len - namesz;
284 pp = &(*pp)->rb_left;
286 pp = &(*pp)->rb_right;
288 goto cell_already_exists;
293 rb_link_node_rcu(&cell->net_node, parent, pp);
294 rb_insert_color(&cell->net_node, &net->cells);
295 atomic_inc(&net->cells_outstanding);
296 write_sequnlock(&net->cells_lock);
298 queue_work(afs_wq, &cell->manager);
301 _debug("wait_for_cell");
302 wait_var_event(&cell->state,
304 state = smp_load_acquire(&cell->state); /* vs error */
305 state == AFS_CELL_ACTIVE || state == AFS_CELL_FAILED;
308 /* Check the state obtained from the wait check. */
309 if (state == AFS_CELL_FAILED) {
314 _leave(" = %p [cell]", cell);
318 _debug("cell exists");
323 afs_get_cell(cursor);
326 write_sequnlock(&net->cells_lock);
332 afs_put_cell(net, cell);
334 _leave(" = %d [error]", ret);
339 * set the root cell information
340 * - can be called with a module parameter string
341 * - can be called from a write to /proc/fs/afs/rootcell
343 int afs_cell_init(struct afs_net *net, const char *rootcell)
345 struct afs_cell *old_root, *new_root;
346 const char *cp, *vllist;
352 /* module is loaded with no parameters, or built statically.
353 * - in the future we might initialize cell DB here.
355 _leave(" = 0 [no root]");
359 cp = strchr(rootcell, ':');
361 _debug("kAFS: no VL server IP addresses specified");
363 len = strlen(rootcell);
369 /* allocate a cell record for the root cell */
370 new_root = afs_lookup_cell(net, rootcell, len, vllist, false);
371 if (IS_ERR(new_root)) {
372 _leave(" = %ld", PTR_ERR(new_root));
373 return PTR_ERR(new_root);
376 if (!test_and_set_bit(AFS_CELL_FL_NO_GC, &new_root->flags))
377 afs_get_cell(new_root);
379 /* install the new cell */
380 write_seqlock(&net->cells_lock);
381 old_root = rcu_access_pointer(net->ws_cell);
382 rcu_assign_pointer(net->ws_cell, new_root);
383 write_sequnlock(&net->cells_lock);
385 afs_put_cell(net, old_root);
391 * Update a cell's VL server address list from the DNS.
393 static int afs_update_cell(struct afs_cell *cell)
395 struct afs_vlserver_list *vllist, *old = NULL, *p;
396 unsigned int min_ttl = READ_ONCE(afs_cell_min_ttl);
397 unsigned int max_ttl = READ_ONCE(afs_cell_max_ttl);
398 time64_t now, expiry = 0;
401 _enter("%s", cell->name);
403 vllist = afs_dns_query(cell, &expiry);
404 if (IS_ERR(vllist)) {
405 ret = PTR_ERR(vllist);
407 _debug("%s: fail %d", cell->name, ret);
412 vllist = afs_alloc_vlserver_list(0);
419 vllist->status = DNS_LOOKUP_GOT_NOT_FOUND;
423 vllist->status = DNS_LOOKUP_GOT_TEMP_FAILURE;
426 vllist->status = DNS_LOOKUP_GOT_LOCAL_FAILURE;
431 _debug("%s: got list %d %d", cell->name, vllist->source, vllist->status);
432 cell->dns_status = vllist->status;
434 now = ktime_get_real_seconds();
435 if (min_ttl > max_ttl)
437 if (expiry < now + min_ttl)
438 expiry = now + min_ttl;
439 else if (expiry > now + max_ttl)
440 expiry = now + max_ttl;
442 _debug("%s: status %d", cell->name, vllist->status);
443 if (vllist->source == DNS_RECORD_UNAVAILABLE) {
444 switch (vllist->status) {
445 case DNS_LOOKUP_GOT_NOT_FOUND:
446 /* The DNS said that the cell does not exist or there
447 * weren't any addresses to be had.
449 cell->dns_expiry = expiry;
453 case DNS_LOOKUP_GOT_LOCAL_FAILURE:
454 case DNS_LOOKUP_GOT_TEMP_FAILURE:
455 case DNS_LOOKUP_GOT_NS_FAILURE:
457 cell->dns_expiry = now + 10;
461 cell->dns_expiry = expiry;
464 /* Replace the VL server list if the new record has servers or the old
467 write_lock(&cell->vl_servers_lock);
468 p = rcu_dereference_protected(cell->vl_servers, true);
469 if (vllist->nr_servers > 0 || p->nr_servers == 0) {
470 rcu_assign_pointer(cell->vl_servers, vllist);
471 cell->dns_source = vllist->source;
474 write_unlock(&cell->vl_servers_lock);
475 afs_put_vlserverlist(cell->net, old);
478 smp_store_release(&cell->dns_lookup_count,
479 cell->dns_lookup_count + 1); /* vs source/status */
480 wake_up_var(&cell->dns_lookup_count);
481 _leave(" = %d", ret);
486 * Destroy a cell record
488 static void afs_cell_destroy(struct rcu_head *rcu)
490 struct afs_cell *cell = container_of(rcu, struct afs_cell, rcu);
492 _enter("%p{%s}", cell, cell->name);
494 ASSERTCMP(atomic_read(&cell->usage), ==, 0);
496 afs_put_volume(cell->net, cell->root_volume, afs_volume_trace_put_cell_root);
497 afs_put_vlserverlist(cell->net, rcu_access_pointer(cell->vl_servers));
498 afs_put_cell(cell->net, cell->alias_of);
499 key_put(cell->anonymous_key);
503 _leave(" [destroyed]");
507 * Queue the cell manager.
509 static void afs_queue_cell_manager(struct afs_net *net)
511 int outstanding = atomic_inc_return(&net->cells_outstanding);
513 _enter("%d", outstanding);
515 if (!queue_work(afs_wq, &net->cells_manager))
516 afs_dec_cells_outstanding(net);
520 * Cell management timer. We have an increment on cells_outstanding that we
521 * need to pass along to the work item.
523 void afs_cells_timer(struct timer_list *timer)
525 struct afs_net *net = container_of(timer, struct afs_net, cells_timer);
528 if (!queue_work(afs_wq, &net->cells_manager))
529 afs_dec_cells_outstanding(net);
533 * Get a reference on a cell record.
535 struct afs_cell *afs_get_cell(struct afs_cell *cell)
537 atomic_inc(&cell->usage);
542 * Drop a reference on a cell record.
544 void afs_put_cell(struct afs_net *net, struct afs_cell *cell)
546 time64_t now, expire_delay;
551 _enter("%s", cell->name);
553 now = ktime_get_real_seconds();
554 cell->last_inactive = now;
556 if (cell->vl_servers->nr_servers)
557 expire_delay = afs_cell_gc_delay;
559 if (atomic_dec_return(&cell->usage) > 1)
562 /* 'cell' may now be garbage collected. */
563 afs_set_cell_timer(net, expire_delay);
567 * Allocate a key to use as a placeholder for anonymous user security.
569 static int afs_alloc_anon_key(struct afs_cell *cell)
572 char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp;
574 /* Create a key to represent an anonymous user. */
575 memcpy(keyname, "afs@", 4);
579 *dp++ = tolower(*cp);
582 key = rxrpc_get_null_key(keyname);
586 cell->anonymous_key = key;
588 _debug("anon key %p{%x}",
589 cell->anonymous_key, key_serial(cell->anonymous_key));
596 static int afs_activate_cell(struct afs_net *net, struct afs_cell *cell)
598 struct hlist_node **p;
599 struct afs_cell *pcell;
602 if (!cell->anonymous_key) {
603 ret = afs_alloc_anon_key(cell);
608 #ifdef CONFIG_AFS_FSCACHE
609 cell->cache = fscache_acquire_cookie(afs_cache_netfs.primary_index,
610 &afs_cell_cache_index_def,
611 cell->name, strlen(cell->name),
615 ret = afs_proc_cell_setup(cell);
619 mutex_lock(&net->proc_cells_lock);
620 for (p = &net->proc_cells.first; *p; p = &(*p)->next) {
621 pcell = hlist_entry(*p, struct afs_cell, proc_link);
622 if (strcmp(cell->name, pcell->name) < 0)
626 cell->proc_link.pprev = p;
627 cell->proc_link.next = *p;
628 rcu_assign_pointer(*p, &cell->proc_link.next);
629 if (cell->proc_link.next)
630 cell->proc_link.next->pprev = &cell->proc_link.next;
632 afs_dynroot_mkdir(net, cell);
633 mutex_unlock(&net->proc_cells_lock);
640 static void afs_deactivate_cell(struct afs_net *net, struct afs_cell *cell)
642 _enter("%s", cell->name);
644 afs_proc_cell_remove(cell);
646 mutex_lock(&net->proc_cells_lock);
647 hlist_del_rcu(&cell->proc_link);
648 afs_dynroot_rmdir(net, cell);
649 mutex_unlock(&net->proc_cells_lock);
651 #ifdef CONFIG_AFS_FSCACHE
652 fscache_relinquish_cookie(cell->cache, NULL, false);
660 * Manage a cell record, initialising and destroying it, maintaining its DNS
663 static void afs_manage_cell(struct work_struct *work)
665 struct afs_cell *cell = container_of(work, struct afs_cell, manager);
666 struct afs_net *net = cell->net;
670 _enter("%s", cell->name);
673 _debug("state %u", cell->state);
674 switch (cell->state) {
675 case AFS_CELL_INACTIVE:
676 case AFS_CELL_FAILED:
677 write_seqlock(&net->cells_lock);
679 deleted = atomic_try_cmpxchg_relaxed(&cell->usage, &usage, 0);
681 rb_erase(&cell->net_node, &net->cells);
682 write_sequnlock(&net->cells_lock);
684 goto final_destruction;
685 if (cell->state == AFS_CELL_FAILED)
687 smp_store_release(&cell->state, AFS_CELL_UNSET);
688 wake_up_var(&cell->state);
692 smp_store_release(&cell->state, AFS_CELL_ACTIVATING);
693 wake_up_var(&cell->state);
696 case AFS_CELL_ACTIVATING:
697 ret = afs_activate_cell(net, cell);
699 goto activation_failed;
701 smp_store_release(&cell->state, AFS_CELL_ACTIVE);
702 wake_up_var(&cell->state);
705 case AFS_CELL_ACTIVE:
706 if (atomic_read(&cell->usage) > 1) {
707 if (test_and_clear_bit(AFS_CELL_FL_DO_LOOKUP, &cell->flags)) {
708 ret = afs_update_cell(cell);
714 smp_store_release(&cell->state, AFS_CELL_DEACTIVATING);
715 wake_up_var(&cell->state);
718 case AFS_CELL_DEACTIVATING:
719 if (atomic_read(&cell->usage) > 1)
720 goto reverse_deactivation;
721 afs_deactivate_cell(net, cell);
722 smp_store_release(&cell->state, AFS_CELL_INACTIVE);
723 wake_up_var(&cell->state);
729 _debug("bad state %u", cell->state);
730 BUG(); /* Unhandled state */
734 afs_deactivate_cell(net, cell);
736 smp_store_release(&cell->state, AFS_CELL_FAILED); /* vs error */
737 wake_up_var(&cell->state);
740 reverse_deactivation:
741 smp_store_release(&cell->state, AFS_CELL_ACTIVE);
742 wake_up_var(&cell->state);
743 _leave(" [deact->act]");
747 _leave(" [done %u]", cell->state);
751 call_rcu(&cell->rcu, afs_cell_destroy);
752 afs_dec_cells_outstanding(net);
753 _leave(" [destruct %d]", atomic_read(&net->cells_outstanding));
757 * Manage the records of cells known to a network namespace. This includes
758 * updating the DNS records and garbage collecting unused cells that were
759 * automatically added.
761 * Note that constructed cell records may only be removed from net->cells by
762 * this work item, so it is safe for this work item to stash a cursor pointing
763 * into the tree and then return to caller (provided it skips cells that are
764 * still under construction).
766 * Note also that we were given an increment on net->cells_outstanding by
767 * whoever queued us that we need to deal with before returning.
769 void afs_manage_cells(struct work_struct *work)
771 struct afs_net *net = container_of(work, struct afs_net, cells_manager);
772 struct rb_node *cursor;
773 time64_t now = ktime_get_real_seconds(), next_manage = TIME64_MAX;
774 bool purging = !net->live;
778 /* Trawl the cell database looking for cells that have expired from
779 * lack of use and cells whose DNS results have expired and dispatch
782 read_seqlock_excl(&net->cells_lock);
784 for (cursor = rb_first(&net->cells); cursor; cursor = rb_next(cursor)) {
785 struct afs_cell *cell =
786 rb_entry(cursor, struct afs_cell, net_node);
788 bool sched_cell = false;
790 usage = atomic_read(&cell->usage);
791 _debug("manage %s %u", cell->name, usage);
793 ASSERTCMP(usage, >=, 1);
796 if (test_and_clear_bit(AFS_CELL_FL_NO_GC, &cell->flags))
797 usage = atomic_dec_return(&cell->usage);
798 ASSERTCMP(usage, ==, 1);
802 struct afs_vlserver_list *vllist;
803 time64_t expire_at = cell->last_inactive;
805 read_lock(&cell->vl_servers_lock);
806 vllist = rcu_dereference_protected(
808 lockdep_is_held(&cell->vl_servers_lock));
809 if (vllist->nr_servers > 0)
810 expire_at += afs_cell_gc_delay;
811 read_unlock(&cell->vl_servers_lock);
812 if (purging || expire_at <= now)
814 else if (expire_at < next_manage)
815 next_manage = expire_at;
819 if (test_bit(AFS_CELL_FL_DO_LOOKUP, &cell->flags))
824 queue_work(afs_wq, &cell->manager);
827 read_sequnlock_excl(&net->cells_lock);
829 /* Update the timer on the way out. We have to pass an increment on
830 * cells_outstanding in the namespace that we are in to the timer or
831 * the work scheduler.
833 if (!purging && next_manage < TIME64_MAX) {
834 now = ktime_get_real_seconds();
836 if (next_manage - now <= 0) {
837 if (queue_work(afs_wq, &net->cells_manager))
838 atomic_inc(&net->cells_outstanding);
840 afs_set_cell_timer(net, next_manage - now);
844 afs_dec_cells_outstanding(net);
845 _leave(" [%d]", atomic_read(&net->cells_outstanding));
849 * Purge in-memory cell database.
851 void afs_cell_purge(struct afs_net *net)
857 write_seqlock(&net->cells_lock);
858 ws = rcu_access_pointer(net->ws_cell);
859 RCU_INIT_POINTER(net->ws_cell, NULL);
860 write_sequnlock(&net->cells_lock);
861 afs_put_cell(net, ws);
864 if (del_timer_sync(&net->cells_timer))
865 atomic_dec(&net->cells_outstanding);
868 afs_queue_cell_manager(net);
871 wait_var_event(&net->cells_outstanding,
872 !atomic_read(&net->cells_outstanding));