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);
83 p = rcu_dereference_raw(net->cells.rb_node);
85 cell = rb_entry(p, struct afs_cell, net_node);
87 n = strncasecmp(cell->name, name,
88 min_t(size_t, cell->name_len, namesz));
90 n = cell->name_len - namesz;
92 p = rcu_dereference_raw(p->rb_left);
94 p = rcu_dereference_raw(p->rb_right);
96 if (atomic_inc_not_zero(&cell->usage)) {
100 /* We want to repeat the search, this time with
101 * the lock properly locked.
107 } while (need_seqretry(&net->cells_lock, seq));
109 done_seqretry(&net->cells_lock, seq);
111 return ret == 0 ? cell : ERR_PTR(ret);
115 * Set up a cell record and fill in its name, VL server address list and
116 * allocate an anonymous key
118 static struct afs_cell *afs_alloc_cell(struct afs_net *net,
119 const char *name, unsigned int namelen,
120 const char *addresses)
122 struct afs_vlserver_list *vllist;
123 struct afs_cell *cell;
128 return ERR_PTR(-EINVAL);
129 if (namelen > AFS_MAXCELLNAME) {
130 _leave(" = -ENAMETOOLONG");
131 return ERR_PTR(-ENAMETOOLONG);
133 if (namelen == 5 && memcmp(name, "@cell", 5) == 0)
134 return ERR_PTR(-EINVAL);
136 _enter("%*.*s,%s", namelen, namelen, name, addresses);
138 cell = kzalloc(sizeof(struct afs_cell), GFP_KERNEL);
140 _leave(" = -ENOMEM");
141 return ERR_PTR(-ENOMEM);
145 cell->name_len = namelen;
146 for (i = 0; i < namelen; i++)
147 cell->name[i] = tolower(name[i]);
149 atomic_set(&cell->usage, 2);
150 INIT_WORK(&cell->manager, afs_manage_cell);
151 INIT_LIST_HEAD(&cell->proc_volumes);
152 rwlock_init(&cell->proc_lock);
153 rwlock_init(&cell->vl_servers_lock);
155 /* Provide a VL server list, filling it in if we were given a list of
159 vllist = afs_parse_text_addrs(net,
160 addresses, strlen(addresses), ':',
161 VL_SERVICE, AFS_VL_PORT);
162 if (IS_ERR(vllist)) {
163 ret = PTR_ERR(vllist);
167 vllist->source = DNS_RECORD_FROM_CONFIG;
168 vllist->status = DNS_LOOKUP_NOT_DONE;
169 cell->dns_expiry = TIME64_MAX;
172 vllist = afs_alloc_vlserver_list(0);
175 vllist->source = DNS_RECORD_UNAVAILABLE;
176 vllist->status = DNS_LOOKUP_NOT_DONE;
177 cell->dns_expiry = ktime_get_real_seconds();
180 rcu_assign_pointer(cell->vl_servers, vllist);
182 cell->dns_source = vllist->source;
183 cell->dns_status = vllist->status;
184 smp_store_release(&cell->dns_lookup_count, 1); /* vs source/status */
186 _leave(" = %p", cell);
191 printk(KERN_ERR "kAFS: bad VL server IP address\n");
194 _leave(" = %d", ret);
199 * afs_lookup_cell - Look up or create a cell record.
200 * @net: The network namespace
201 * @name: The name of the cell.
202 * @namesz: The strlen of the cell name.
203 * @vllist: A colon/comma separated list of numeric IP addresses or NULL.
204 * @excl: T if an error should be given if the cell name already exists.
206 * Look up a cell record by name and query the DNS for VL server addresses if
207 * needed. Note that that actual DNS query is punted off to the manager thread
208 * so that this function can return immediately if interrupted whilst allowing
209 * cell records to be shared even if not yet fully constructed.
211 struct afs_cell *afs_lookup_cell(struct afs_net *net,
212 const char *name, unsigned int namesz,
213 const char *vllist, bool excl)
215 struct afs_cell *cell, *candidate, *cursor;
216 struct rb_node *parent, **pp;
217 enum afs_cell_state state;
220 _enter("%s,%s", name, vllist);
224 cell = afs_lookup_cell_rcu(net, name, namesz);
230 /* Assume we're probably going to create a cell and preallocate and
231 * mostly set up a candidate record. We can then use this to stash the
232 * name, the net namespace and VL server addresses.
234 * We also want to do this before we hold any locks as it may involve
235 * upcalling to userspace to make DNS queries.
237 candidate = afs_alloc_cell(net, name, namesz, vllist);
238 if (IS_ERR(candidate)) {
239 _leave(" = %ld", PTR_ERR(candidate));
243 /* Find the insertion point and check to see if someone else added a
244 * cell whilst we were allocating.
246 write_seqlock(&net->cells_lock);
248 pp = &net->cells.rb_node;
252 cursor = rb_entry(parent, struct afs_cell, net_node);
254 n = strncasecmp(cursor->name, name,
255 min_t(size_t, cursor->name_len, namesz));
257 n = cursor->name_len - namesz;
259 pp = &(*pp)->rb_left;
261 pp = &(*pp)->rb_right;
263 goto cell_already_exists;
268 rb_link_node_rcu(&cell->net_node, parent, pp);
269 rb_insert_color(&cell->net_node, &net->cells);
270 atomic_inc(&net->cells_outstanding);
271 write_sequnlock(&net->cells_lock);
273 queue_work(afs_wq, &cell->manager);
276 _debug("wait_for_cell");
277 wait_var_event(&cell->state,
279 state = smp_load_acquire(&cell->state); /* vs error */
280 state == AFS_CELL_ACTIVE || state == AFS_CELL_FAILED;
283 /* Check the state obtained from the wait check. */
284 if (state == AFS_CELL_FAILED) {
289 _leave(" = %p [cell]", cell);
293 _debug("cell exists");
298 afs_get_cell(cursor);
301 write_sequnlock(&net->cells_lock);
307 afs_put_cell(net, cell);
309 _leave(" = %d [error]", ret);
314 * set the root cell information
315 * - can be called with a module parameter string
316 * - can be called from a write to /proc/fs/afs/rootcell
318 int afs_cell_init(struct afs_net *net, const char *rootcell)
320 struct afs_cell *old_root, *new_root;
321 const char *cp, *vllist;
327 /* module is loaded with no parameters, or built statically.
328 * - in the future we might initialize cell DB here.
330 _leave(" = 0 [no root]");
334 cp = strchr(rootcell, ':');
336 _debug("kAFS: no VL server IP addresses specified");
338 len = strlen(rootcell);
344 /* allocate a cell record for the root cell */
345 new_root = afs_lookup_cell(net, rootcell, len, vllist, false);
346 if (IS_ERR(new_root)) {
347 _leave(" = %ld", PTR_ERR(new_root));
348 return PTR_ERR(new_root);
351 if (!test_and_set_bit(AFS_CELL_FL_NO_GC, &new_root->flags))
352 afs_get_cell(new_root);
354 /* install the new cell */
355 write_seqlock(&net->cells_lock);
356 old_root = rcu_access_pointer(net->ws_cell);
357 rcu_assign_pointer(net->ws_cell, new_root);
358 write_sequnlock(&net->cells_lock);
360 afs_put_cell(net, old_root);
366 * Update a cell's VL server address list from the DNS.
368 static int afs_update_cell(struct afs_cell *cell)
370 struct afs_vlserver_list *vllist, *old = NULL, *p;
371 unsigned int min_ttl = READ_ONCE(afs_cell_min_ttl);
372 unsigned int max_ttl = READ_ONCE(afs_cell_max_ttl);
373 time64_t now, expiry = 0;
376 _enter("%s", cell->name);
378 vllist = afs_dns_query(cell, &expiry);
379 if (IS_ERR(vllist)) {
380 ret = PTR_ERR(vllist);
382 _debug("%s: fail %d", cell->name, ret);
387 vllist = afs_alloc_vlserver_list(0);
394 vllist->status = DNS_LOOKUP_GOT_NOT_FOUND;
398 vllist->status = DNS_LOOKUP_GOT_TEMP_FAILURE;
401 vllist->status = DNS_LOOKUP_GOT_LOCAL_FAILURE;
406 _debug("%s: got list %d %d", cell->name, vllist->source, vllist->status);
407 cell->dns_status = vllist->status;
409 now = ktime_get_real_seconds();
410 if (min_ttl > max_ttl)
412 if (expiry < now + min_ttl)
413 expiry = now + min_ttl;
414 else if (expiry > now + max_ttl)
415 expiry = now + max_ttl;
417 _debug("%s: status %d", cell->name, vllist->status);
418 if (vllist->source == DNS_RECORD_UNAVAILABLE) {
419 switch (vllist->status) {
420 case DNS_LOOKUP_GOT_NOT_FOUND:
421 /* The DNS said that the cell does not exist or there
422 * weren't any addresses to be had.
424 cell->dns_expiry = expiry;
428 case DNS_LOOKUP_GOT_LOCAL_FAILURE:
429 case DNS_LOOKUP_GOT_TEMP_FAILURE:
430 case DNS_LOOKUP_GOT_NS_FAILURE:
432 cell->dns_expiry = now + 10;
436 cell->dns_expiry = expiry;
439 /* Replace the VL server list if the new record has servers or the old
442 write_lock(&cell->vl_servers_lock);
443 p = rcu_dereference_protected(cell->vl_servers, true);
444 if (vllist->nr_servers > 0 || p->nr_servers == 0) {
445 rcu_assign_pointer(cell->vl_servers, vllist);
446 cell->dns_source = vllist->source;
449 write_unlock(&cell->vl_servers_lock);
450 afs_put_vlserverlist(cell->net, old);
453 smp_store_release(&cell->dns_lookup_count,
454 cell->dns_lookup_count + 1); /* vs source/status */
455 wake_up_var(&cell->dns_lookup_count);
456 _leave(" = %d", ret);
461 * Destroy a cell record
463 static void afs_cell_destroy(struct rcu_head *rcu)
465 struct afs_cell *cell = container_of(rcu, struct afs_cell, rcu);
467 _enter("%p{%s}", cell, cell->name);
469 ASSERTCMP(atomic_read(&cell->usage), ==, 0);
471 afs_put_vlserverlist(cell->net, rcu_access_pointer(cell->vl_servers));
472 key_put(cell->anonymous_key);
475 _leave(" [destroyed]");
479 * Queue the cell manager.
481 static void afs_queue_cell_manager(struct afs_net *net)
483 int outstanding = atomic_inc_return(&net->cells_outstanding);
485 _enter("%d", outstanding);
487 if (!queue_work(afs_wq, &net->cells_manager))
488 afs_dec_cells_outstanding(net);
492 * Cell management timer. We have an increment on cells_outstanding that we
493 * need to pass along to the work item.
495 void afs_cells_timer(struct timer_list *timer)
497 struct afs_net *net = container_of(timer, struct afs_net, cells_timer);
500 if (!queue_work(afs_wq, &net->cells_manager))
501 afs_dec_cells_outstanding(net);
505 * Get a reference on a cell record.
507 struct afs_cell *afs_get_cell(struct afs_cell *cell)
509 atomic_inc(&cell->usage);
514 * Drop a reference on a cell record.
516 void afs_put_cell(struct afs_net *net, struct afs_cell *cell)
518 time64_t now, expire_delay;
523 _enter("%s", cell->name);
525 now = ktime_get_real_seconds();
526 cell->last_inactive = now;
528 if (cell->vl_servers->nr_servers)
529 expire_delay = afs_cell_gc_delay;
531 if (atomic_dec_return(&cell->usage) > 1)
534 /* 'cell' may now be garbage collected. */
535 afs_set_cell_timer(net, expire_delay);
539 * Allocate a key to use as a placeholder for anonymous user security.
541 static int afs_alloc_anon_key(struct afs_cell *cell)
544 char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp;
546 /* Create a key to represent an anonymous user. */
547 memcpy(keyname, "afs@", 4);
551 *dp++ = tolower(*cp);
554 key = rxrpc_get_null_key(keyname);
558 cell->anonymous_key = key;
560 _debug("anon key %p{%x}",
561 cell->anonymous_key, key_serial(cell->anonymous_key));
568 static int afs_activate_cell(struct afs_net *net, struct afs_cell *cell)
570 struct hlist_node **p;
571 struct afs_cell *pcell;
574 if (!cell->anonymous_key) {
575 ret = afs_alloc_anon_key(cell);
580 #ifdef CONFIG_AFS_FSCACHE
581 cell->cache = fscache_acquire_cookie(afs_cache_netfs.primary_index,
582 &afs_cell_cache_index_def,
583 cell->name, strlen(cell->name),
587 ret = afs_proc_cell_setup(cell);
591 mutex_lock(&net->proc_cells_lock);
592 for (p = &net->proc_cells.first; *p; p = &(*p)->next) {
593 pcell = hlist_entry(*p, struct afs_cell, proc_link);
594 if (strcmp(cell->name, pcell->name) < 0)
598 cell->proc_link.pprev = p;
599 cell->proc_link.next = *p;
600 rcu_assign_pointer(*p, &cell->proc_link.next);
601 if (cell->proc_link.next)
602 cell->proc_link.next->pprev = &cell->proc_link.next;
604 afs_dynroot_mkdir(net, cell);
605 mutex_unlock(&net->proc_cells_lock);
612 static void afs_deactivate_cell(struct afs_net *net, struct afs_cell *cell)
614 _enter("%s", cell->name);
616 afs_proc_cell_remove(cell);
618 mutex_lock(&net->proc_cells_lock);
619 hlist_del_rcu(&cell->proc_link);
620 afs_dynroot_rmdir(net, cell);
621 mutex_unlock(&net->proc_cells_lock);
623 #ifdef CONFIG_AFS_FSCACHE
624 fscache_relinquish_cookie(cell->cache, NULL, false);
632 * Manage a cell record, initialising and destroying it, maintaining its DNS
635 static void afs_manage_cell(struct work_struct *work)
637 struct afs_cell *cell = container_of(work, struct afs_cell, manager);
638 struct afs_net *net = cell->net;
642 _enter("%s", cell->name);
645 _debug("state %u", cell->state);
646 switch (cell->state) {
647 case AFS_CELL_INACTIVE:
648 case AFS_CELL_FAILED:
649 write_seqlock(&net->cells_lock);
651 deleted = atomic_try_cmpxchg_relaxed(&cell->usage, &usage, 0);
653 rb_erase(&cell->net_node, &net->cells);
654 write_sequnlock(&net->cells_lock);
656 goto final_destruction;
657 if (cell->state == AFS_CELL_FAILED)
659 smp_store_release(&cell->state, AFS_CELL_UNSET);
660 wake_up_var(&cell->state);
664 smp_store_release(&cell->state, AFS_CELL_ACTIVATING);
665 wake_up_var(&cell->state);
668 case AFS_CELL_ACTIVATING:
669 ret = afs_activate_cell(net, cell);
671 goto activation_failed;
673 smp_store_release(&cell->state, AFS_CELL_ACTIVE);
674 wake_up_var(&cell->state);
677 case AFS_CELL_ACTIVE:
678 if (atomic_read(&cell->usage) > 1) {
679 if (test_and_clear_bit(AFS_CELL_FL_DO_LOOKUP, &cell->flags)) {
680 ret = afs_update_cell(cell);
686 smp_store_release(&cell->state, AFS_CELL_DEACTIVATING);
687 wake_up_var(&cell->state);
690 case AFS_CELL_DEACTIVATING:
691 if (atomic_read(&cell->usage) > 1)
692 goto reverse_deactivation;
693 afs_deactivate_cell(net, cell);
694 smp_store_release(&cell->state, AFS_CELL_INACTIVE);
695 wake_up_var(&cell->state);
701 _debug("bad state %u", cell->state);
702 BUG(); /* Unhandled state */
706 afs_deactivate_cell(net, cell);
708 smp_store_release(&cell->state, AFS_CELL_FAILED); /* vs error */
709 wake_up_var(&cell->state);
712 reverse_deactivation:
713 smp_store_release(&cell->state, AFS_CELL_ACTIVE);
714 wake_up_var(&cell->state);
715 _leave(" [deact->act]");
719 _leave(" [done %u]", cell->state);
723 call_rcu(&cell->rcu, afs_cell_destroy);
724 afs_dec_cells_outstanding(net);
725 _leave(" [destruct %d]", atomic_read(&net->cells_outstanding));
729 * Manage the records of cells known to a network namespace. This includes
730 * updating the DNS records and garbage collecting unused cells that were
731 * automatically added.
733 * Note that constructed cell records may only be removed from net->cells by
734 * this work item, so it is safe for this work item to stash a cursor pointing
735 * into the tree and then return to caller (provided it skips cells that are
736 * still under construction).
738 * Note also that we were given an increment on net->cells_outstanding by
739 * whoever queued us that we need to deal with before returning.
741 void afs_manage_cells(struct work_struct *work)
743 struct afs_net *net = container_of(work, struct afs_net, cells_manager);
744 struct rb_node *cursor;
745 time64_t now = ktime_get_real_seconds(), next_manage = TIME64_MAX;
746 bool purging = !net->live;
750 /* Trawl the cell database looking for cells that have expired from
751 * lack of use and cells whose DNS results have expired and dispatch
754 read_seqlock_excl(&net->cells_lock);
756 for (cursor = rb_first(&net->cells); cursor; cursor = rb_next(cursor)) {
757 struct afs_cell *cell =
758 rb_entry(cursor, struct afs_cell, net_node);
760 bool sched_cell = false;
762 usage = atomic_read(&cell->usage);
763 _debug("manage %s %u", cell->name, usage);
765 ASSERTCMP(usage, >=, 1);
768 if (test_and_clear_bit(AFS_CELL_FL_NO_GC, &cell->flags))
769 usage = atomic_dec_return(&cell->usage);
770 ASSERTCMP(usage, ==, 1);
774 struct afs_vlserver_list *vllist;
775 time64_t expire_at = cell->last_inactive;
777 read_lock(&cell->vl_servers_lock);
778 vllist = rcu_dereference_protected(
780 lockdep_is_held(&cell->vl_servers_lock));
781 if (vllist->nr_servers > 0)
782 expire_at += afs_cell_gc_delay;
783 read_unlock(&cell->vl_servers_lock);
784 if (purging || expire_at <= now)
786 else if (expire_at < next_manage)
787 next_manage = expire_at;
791 if (test_bit(AFS_CELL_FL_DO_LOOKUP, &cell->flags))
796 queue_work(afs_wq, &cell->manager);
799 read_sequnlock_excl(&net->cells_lock);
801 /* Update the timer on the way out. We have to pass an increment on
802 * cells_outstanding in the namespace that we are in to the timer or
803 * the work scheduler.
805 if (!purging && next_manage < TIME64_MAX) {
806 now = ktime_get_real_seconds();
808 if (next_manage - now <= 0) {
809 if (queue_work(afs_wq, &net->cells_manager))
810 atomic_inc(&net->cells_outstanding);
812 afs_set_cell_timer(net, next_manage - now);
816 afs_dec_cells_outstanding(net);
817 _leave(" [%d]", atomic_read(&net->cells_outstanding));
821 * Purge in-memory cell database.
823 void afs_cell_purge(struct afs_net *net)
829 write_seqlock(&net->cells_lock);
830 ws = rcu_access_pointer(net->ws_cell);
831 RCU_INIT_POINTER(net->ws_cell, NULL);
832 write_sequnlock(&net->cells_lock);
833 afs_put_cell(net, ws);
836 if (del_timer_sync(&net->cells_timer))
837 atomic_dec(&net->cells_outstanding);
840 afs_queue_cell_manager(net);
843 wait_var_event(&net->cells_outstanding,
844 !atomic_read(&net->cells_outstanding));