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);
86 p = rcu_dereference_raw(net->cells.rb_node);
88 cell = rb_entry(p, struct afs_cell, net_node);
90 n = strncasecmp(cell->name, name,
91 min_t(size_t, cell->name_len, namesz));
93 n = cell->name_len - namesz;
95 p = rcu_dereference_raw(p->rb_left);
97 p = rcu_dereference_raw(p->rb_right);
99 if (atomic_inc_not_zero(&cell->usage)) {
103 /* We want to repeat the search, this time with
104 * the lock properly locked.
110 } while (need_seqretry(&net->cells_lock, seq));
112 done_seqretry(&net->cells_lock, seq);
114 if (ret != 0 && cell)
115 afs_put_cell(net, cell);
117 return ret == 0 ? cell : ERR_PTR(ret);
121 * Set up a cell record and fill in its name, VL server address list and
122 * allocate an anonymous key
124 static struct afs_cell *afs_alloc_cell(struct afs_net *net,
125 const char *name, unsigned int namelen,
128 struct afs_cell *cell;
133 return ERR_PTR(-EINVAL);
134 if (namelen > AFS_MAXCELLNAME) {
135 _leave(" = -ENAMETOOLONG");
136 return ERR_PTR(-ENAMETOOLONG);
139 /* Prohibit cell names that contain unprintable chars, '/' and '@' or
140 * that begin with a dot. This also precludes "@cell".
143 return ERR_PTR(-EINVAL);
144 for (i = 0; i < namelen; i++) {
146 if (!isprint(ch) || ch == '/' || ch == '@')
147 return ERR_PTR(-EINVAL);
150 _enter("%*.*s,%s", namelen, namelen, name, vllist);
152 cell = kzalloc(sizeof(struct afs_cell), GFP_KERNEL);
154 _leave(" = -ENOMEM");
155 return ERR_PTR(-ENOMEM);
159 cell->name_len = namelen;
160 for (i = 0; i < namelen; i++)
161 cell->name[i] = tolower(name[i]);
163 atomic_set(&cell->usage, 2);
164 INIT_WORK(&cell->manager, afs_manage_cell);
165 cell->flags = ((1 << AFS_CELL_FL_NOT_READY) |
166 (1 << AFS_CELL_FL_NO_LOOKUP_YET));
167 INIT_LIST_HEAD(&cell->proc_volumes);
168 rwlock_init(&cell->proc_lock);
169 rwlock_init(&cell->vl_addrs_lock);
171 /* Fill in the VL server list if we were given a list of addresses to
175 struct afs_addr_list *alist;
177 alist = afs_parse_text_addrs(vllist, strlen(vllist), ':',
178 VL_SERVICE, AFS_VL_PORT);
180 ret = PTR_ERR(alist);
184 rcu_assign_pointer(cell->vl_addrs, alist);
185 cell->dns_expiry = TIME64_MAX;
188 _leave(" = %p", cell);
193 printk(KERN_ERR "kAFS: bad VL server IP address\n");
195 _leave(" = %d", ret);
200 * afs_lookup_cell - Look up or create a cell record.
201 * @net: The network namespace
202 * @name: The name of the cell.
203 * @namesz: The strlen of the cell name.
204 * @vllist: A colon/comma separated list of numeric IP addresses or NULL.
205 * @excl: T if an error should be given if the cell name already exists.
207 * Look up a cell record by name and query the DNS for VL server addresses if
208 * needed. Note that that actual DNS query is punted off to the manager thread
209 * so that this function can return immediately if interrupted whilst allowing
210 * cell records to be shared even if not yet fully constructed.
212 struct afs_cell *afs_lookup_cell(struct afs_net *net,
213 const char *name, unsigned int namesz,
214 const char *vllist, bool excl)
216 struct afs_cell *cell, *candidate, *cursor;
217 struct rb_node *parent, **pp;
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 ret = wait_on_bit(&cell->flags, AFS_CELL_FL_NOT_READY, TASK_INTERRUPTIBLE);
280 switch (READ_ONCE(cell->state)) {
281 case AFS_CELL_FAILED:
285 _debug("weird %u %d", cell->state, cell->error);
287 case AFS_CELL_ACTIVE:
291 _leave(" = %p [cell]", cell);
295 _debug("cell exists");
300 afs_get_cell(cursor);
303 write_sequnlock(&net->cells_lock);
309 afs_put_cell(net, cell);
311 _leave(" = %d [error]", ret);
316 * set the root cell information
317 * - can be called with a module parameter string
318 * - can be called from a write to /proc/fs/afs/rootcell
320 int afs_cell_init(struct afs_net *net, const char *rootcell)
322 struct afs_cell *old_root, *new_root;
323 const char *cp, *vllist;
329 /* module is loaded with no parameters, or built statically.
330 * - in the future we might initialize cell DB here.
332 _leave(" = 0 [no root]");
336 cp = strchr(rootcell, ':');
338 _debug("kAFS: no VL server IP addresses specified");
340 len = strlen(rootcell);
346 /* allocate a cell record for the root cell */
347 new_root = afs_lookup_cell(net, rootcell, len, vllist, false);
348 if (IS_ERR(new_root)) {
349 _leave(" = %ld", PTR_ERR(new_root));
350 return PTR_ERR(new_root);
353 if (!test_and_set_bit(AFS_CELL_FL_NO_GC, &new_root->flags))
354 afs_get_cell(new_root);
356 /* install the new cell */
357 write_seqlock(&net->cells_lock);
358 old_root = rcu_access_pointer(net->ws_cell);
359 rcu_assign_pointer(net->ws_cell, new_root);
360 write_sequnlock(&net->cells_lock);
362 afs_put_cell(net, old_root);
368 * Update a cell's VL server address list from the DNS.
370 static void afs_update_cell(struct afs_cell *cell)
372 struct afs_addr_list *alist, *old;
373 time64_t now, expiry;
375 _enter("%s", cell->name);
377 alist = afs_dns_query(cell, &expiry);
379 switch (PTR_ERR(alist)) {
381 /* The DNS said that the cell does not exist */
382 set_bit(AFS_CELL_FL_NOT_FOUND, &cell->flags);
383 clear_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags);
384 cell->dns_expiry = ktime_get_real_seconds() + 61;
390 set_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags);
391 cell->dns_expiry = ktime_get_real_seconds() + 10;
395 cell->error = -EDESTADDRREQ;
397 clear_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags);
398 clear_bit(AFS_CELL_FL_NOT_FOUND, &cell->flags);
400 /* Exclusion on changing vl_addrs is achieved by a
401 * non-reentrant work item.
403 old = rcu_dereference_protected(cell->vl_addrs, true);
404 rcu_assign_pointer(cell->vl_addrs, alist);
405 cell->dns_expiry = expiry;
408 afs_put_addrlist(old);
411 if (test_and_clear_bit(AFS_CELL_FL_NO_LOOKUP_YET, &cell->flags))
412 wake_up_bit(&cell->flags, AFS_CELL_FL_NO_LOOKUP_YET);
414 now = ktime_get_real_seconds();
415 afs_set_cell_timer(cell->net, cell->dns_expiry - now);
420 * Destroy a cell record
422 static void afs_cell_destroy(struct rcu_head *rcu)
424 struct afs_cell *cell = container_of(rcu, struct afs_cell, rcu);
426 _enter("%p{%s}", cell, cell->name);
428 ASSERTCMP(atomic_read(&cell->usage), ==, 0);
430 afs_put_addrlist(rcu_access_pointer(cell->vl_addrs));
431 key_put(cell->anonymous_key);
434 _leave(" [destroyed]");
438 * Queue the cell manager.
440 static void afs_queue_cell_manager(struct afs_net *net)
442 int outstanding = atomic_inc_return(&net->cells_outstanding);
444 _enter("%d", outstanding);
446 if (!queue_work(afs_wq, &net->cells_manager))
447 afs_dec_cells_outstanding(net);
451 * Cell management timer. We have an increment on cells_outstanding that we
452 * need to pass along to the work item.
454 void afs_cells_timer(struct timer_list *timer)
456 struct afs_net *net = container_of(timer, struct afs_net, cells_timer);
459 if (!queue_work(afs_wq, &net->cells_manager))
460 afs_dec_cells_outstanding(net);
464 * Get a reference on a cell record.
466 struct afs_cell *afs_get_cell(struct afs_cell *cell)
468 atomic_inc(&cell->usage);
473 * Drop a reference on a cell record.
475 void afs_put_cell(struct afs_net *net, struct afs_cell *cell)
477 time64_t now, expire_delay;
482 _enter("%s", cell->name);
484 now = ktime_get_real_seconds();
485 cell->last_inactive = now;
487 if (!test_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags) &&
488 !test_bit(AFS_CELL_FL_NOT_FOUND, &cell->flags))
489 expire_delay = afs_cell_gc_delay;
491 if (atomic_dec_return(&cell->usage) > 1)
494 /* 'cell' may now be garbage collected. */
495 afs_set_cell_timer(net, expire_delay);
499 * Allocate a key to use as a placeholder for anonymous user security.
501 static int afs_alloc_anon_key(struct afs_cell *cell)
504 char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp;
506 /* Create a key to represent an anonymous user. */
507 memcpy(keyname, "afs@", 4);
511 *dp++ = tolower(*cp);
514 key = rxrpc_get_null_key(keyname);
518 cell->anonymous_key = key;
520 _debug("anon key %p{%x}",
521 cell->anonymous_key, key_serial(cell->anonymous_key));
528 static int afs_activate_cell(struct afs_net *net, struct afs_cell *cell)
530 struct hlist_node **p;
531 struct afs_cell *pcell;
534 if (!cell->anonymous_key) {
535 ret = afs_alloc_anon_key(cell);
540 #ifdef CONFIG_AFS_FSCACHE
541 cell->cache = fscache_acquire_cookie(afs_cache_netfs.primary_index,
542 &afs_cell_cache_index_def,
543 cell->name, strlen(cell->name),
547 ret = afs_proc_cell_setup(cell);
551 mutex_lock(&net->proc_cells_lock);
552 for (p = &net->proc_cells.first; *p; p = &(*p)->next) {
553 pcell = hlist_entry(*p, struct afs_cell, proc_link);
554 if (strcmp(cell->name, pcell->name) < 0)
558 cell->proc_link.pprev = p;
559 cell->proc_link.next = *p;
560 rcu_assign_pointer(*p, &cell->proc_link.next);
561 if (cell->proc_link.next)
562 cell->proc_link.next->pprev = &cell->proc_link.next;
564 afs_dynroot_mkdir(net, cell);
565 mutex_unlock(&net->proc_cells_lock);
572 static void afs_deactivate_cell(struct afs_net *net, struct afs_cell *cell)
574 _enter("%s", cell->name);
576 afs_proc_cell_remove(cell);
578 mutex_lock(&net->proc_cells_lock);
579 hlist_del_rcu(&cell->proc_link);
580 afs_dynroot_rmdir(net, cell);
581 mutex_unlock(&net->proc_cells_lock);
583 #ifdef CONFIG_AFS_FSCACHE
584 fscache_relinquish_cookie(cell->cache, NULL, false);
592 * Manage a cell record, initialising and destroying it, maintaining its DNS
595 static void afs_manage_cell(struct work_struct *work)
597 struct afs_cell *cell = container_of(work, struct afs_cell, manager);
598 struct afs_net *net = cell->net;
602 _enter("%s", cell->name);
605 _debug("state %u", cell->state);
606 switch (cell->state) {
607 case AFS_CELL_INACTIVE:
608 case AFS_CELL_FAILED:
609 write_seqlock(&net->cells_lock);
611 deleted = atomic_try_cmpxchg_relaxed(&cell->usage, &usage, 0);
613 rb_erase(&cell->net_node, &net->cells);
614 write_sequnlock(&net->cells_lock);
616 goto final_destruction;
617 if (cell->state == AFS_CELL_FAILED)
619 cell->state = AFS_CELL_UNSET;
623 cell->state = AFS_CELL_ACTIVATING;
626 case AFS_CELL_ACTIVATING:
627 ret = afs_activate_cell(net, cell);
629 goto activation_failed;
631 cell->state = AFS_CELL_ACTIVE;
633 clear_bit(AFS_CELL_FL_NOT_READY, &cell->flags);
634 wake_up_bit(&cell->flags, AFS_CELL_FL_NOT_READY);
637 case AFS_CELL_ACTIVE:
638 if (atomic_read(&cell->usage) > 1) {
639 time64_t now = ktime_get_real_seconds();
640 if (cell->dns_expiry <= now && net->live)
641 afs_update_cell(cell);
644 cell->state = AFS_CELL_DEACTIVATING;
647 case AFS_CELL_DEACTIVATING:
648 set_bit(AFS_CELL_FL_NOT_READY, &cell->flags);
649 if (atomic_read(&cell->usage) > 1)
650 goto reverse_deactivation;
651 afs_deactivate_cell(net, cell);
652 cell->state = AFS_CELL_INACTIVE;
658 _debug("bad state %u", cell->state);
659 BUG(); /* Unhandled state */
663 afs_deactivate_cell(net, cell);
665 cell->state = AFS_CELL_FAILED;
667 if (test_and_clear_bit(AFS_CELL_FL_NOT_READY, &cell->flags))
668 wake_up_bit(&cell->flags, AFS_CELL_FL_NOT_READY);
671 reverse_deactivation:
672 cell->state = AFS_CELL_ACTIVE;
674 clear_bit(AFS_CELL_FL_NOT_READY, &cell->flags);
675 wake_up_bit(&cell->flags, AFS_CELL_FL_NOT_READY);
676 _leave(" [deact->act]");
680 _leave(" [done %u]", cell->state);
684 call_rcu(&cell->rcu, afs_cell_destroy);
685 afs_dec_cells_outstanding(net);
686 _leave(" [destruct %d]", atomic_read(&net->cells_outstanding));
690 * Manage the records of cells known to a network namespace. This includes
691 * updating the DNS records and garbage collecting unused cells that were
692 * automatically added.
694 * Note that constructed cell records may only be removed from net->cells by
695 * this work item, so it is safe for this work item to stash a cursor pointing
696 * into the tree and then return to caller (provided it skips cells that are
697 * still under construction).
699 * Note also that we were given an increment on net->cells_outstanding by
700 * whoever queued us that we need to deal with before returning.
702 void afs_manage_cells(struct work_struct *work)
704 struct afs_net *net = container_of(work, struct afs_net, cells_manager);
705 struct rb_node *cursor;
706 time64_t now = ktime_get_real_seconds(), next_manage = TIME64_MAX;
707 bool purging = !net->live;
711 /* Trawl the cell database looking for cells that have expired from
712 * lack of use and cells whose DNS results have expired and dispatch
715 read_seqlock_excl(&net->cells_lock);
717 for (cursor = rb_first(&net->cells); cursor; cursor = rb_next(cursor)) {
718 struct afs_cell *cell =
719 rb_entry(cursor, struct afs_cell, net_node);
721 bool sched_cell = false;
723 usage = atomic_read(&cell->usage);
724 _debug("manage %s %u", cell->name, usage);
726 ASSERTCMP(usage, >=, 1);
729 if (test_and_clear_bit(AFS_CELL_FL_NO_GC, &cell->flags))
730 usage = atomic_dec_return(&cell->usage);
731 ASSERTCMP(usage, ==, 1);
735 time64_t expire_at = cell->last_inactive;
737 if (!test_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags) &&
738 !test_bit(AFS_CELL_FL_NOT_FOUND, &cell->flags))
739 expire_at += afs_cell_gc_delay;
740 if (purging || expire_at <= now)
742 else if (expire_at < next_manage)
743 next_manage = expire_at;
747 if (cell->dns_expiry <= now)
749 else if (cell->dns_expiry <= next_manage)
750 next_manage = cell->dns_expiry;
754 queue_work(afs_wq, &cell->manager);
757 read_sequnlock_excl(&net->cells_lock);
759 /* Update the timer on the way out. We have to pass an increment on
760 * cells_outstanding in the namespace that we are in to the timer or
761 * the work scheduler.
763 if (!purging && next_manage < TIME64_MAX) {
764 now = ktime_get_real_seconds();
766 if (next_manage - now <= 0) {
767 if (queue_work(afs_wq, &net->cells_manager))
768 atomic_inc(&net->cells_outstanding);
770 afs_set_cell_timer(net, next_manage - now);
774 afs_dec_cells_outstanding(net);
775 _leave(" [%d]", atomic_read(&net->cells_outstanding));
779 * Purge in-memory cell database.
781 void afs_cell_purge(struct afs_net *net)
787 write_seqlock(&net->cells_lock);
788 ws = rcu_access_pointer(net->ws_cell);
789 RCU_INIT_POINTER(net->ws_cell, NULL);
790 write_sequnlock(&net->cells_lock);
791 afs_put_cell(net, ws);
794 if (del_timer_sync(&net->cells_timer))
795 atomic_dec(&net->cells_outstanding);
798 afs_queue_cell_manager(net);
801 wait_var_event(&net->cells_outstanding,
802 !atomic_read(&net->cells_outstanding));