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 <keys/rxrpc-type.h>
21 static unsigned __read_mostly afs_cell_gc_delay = 10;
23 static void afs_manage_cell(struct work_struct *);
25 static void afs_dec_cells_outstanding(struct afs_net *net)
27 if (atomic_dec_and_test(&net->cells_outstanding))
28 wake_up_var(&net->cells_outstanding);
32 * Set the cell timer to fire after a given delay, assuming it's not already
33 * set for an earlier time.
35 static void afs_set_cell_timer(struct afs_net *net, time64_t delay)
38 atomic_inc(&net->cells_outstanding);
39 if (timer_reduce(&net->cells_timer, jiffies + delay * HZ))
40 afs_dec_cells_outstanding(net);
45 * Look up and get an activation reference on a cell record under RCU
46 * conditions. The caller must hold the RCU read lock.
48 struct afs_cell *afs_lookup_cell_rcu(struct afs_net *net,
49 const char *name, unsigned int namesz)
51 struct afs_cell *cell = NULL;
53 int n, seq = 0, ret = 0;
55 _enter("%*.*s", namesz, namesz, name);
57 if (name && namesz == 0)
58 return ERR_PTR(-EINVAL);
59 if (namesz > AFS_MAXCELLNAME)
60 return ERR_PTR(-ENAMETOOLONG);
63 /* Unfortunately, rbtree walking doesn't give reliable results
64 * under just the RCU read lock, so we have to check for
68 afs_put_cell(net, cell);
72 read_seqbegin_or_lock(&net->cells_lock, &seq);
75 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 return ret == 0 ? cell : ERR_PTR(ret);
116 * Set up a cell record and fill in its name, VL server address list and
117 * allocate an anonymous key
119 static struct afs_cell *afs_alloc_cell(struct afs_net *net,
120 const char *name, unsigned int namelen,
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, vllist);
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 cell->flags = ((1 << AFS_CELL_FL_NOT_READY) |
152 (1 << AFS_CELL_FL_NO_LOOKUP_YET));
153 INIT_LIST_HEAD(&cell->proc_volumes);
154 rwlock_init(&cell->proc_lock);
155 rwlock_init(&cell->vl_addrs_lock);
157 /* Fill in the VL server list if we were given a list of addresses to
161 struct afs_addr_list *alist;
163 alist = afs_parse_text_addrs(vllist, strlen(vllist), ':',
164 VL_SERVICE, AFS_VL_PORT);
166 ret = PTR_ERR(alist);
170 rcu_assign_pointer(cell->vl_addrs, alist);
171 cell->dns_expiry = TIME64_MAX;
174 _leave(" = %p", cell);
179 printk(KERN_ERR "kAFS: bad VL server IP address\n");
181 _leave(" = %d", ret);
186 * afs_lookup_cell - Look up or create a cell record.
187 * @net: The network namespace
188 * @name: The name of the cell.
189 * @namesz: The strlen of the cell name.
190 * @vllist: A colon/comma separated list of numeric IP addresses or NULL.
191 * @excl: T if an error should be given if the cell name already exists.
193 * Look up a cell record by name and query the DNS for VL server addresses if
194 * needed. Note that that actual DNS query is punted off to the manager thread
195 * so that this function can return immediately if interrupted whilst allowing
196 * cell records to be shared even if not yet fully constructed.
198 struct afs_cell *afs_lookup_cell(struct afs_net *net,
199 const char *name, unsigned int namesz,
200 const char *vllist, bool excl)
202 struct afs_cell *cell, *candidate, *cursor;
203 struct rb_node *parent, **pp;
206 _enter("%s,%s", name, vllist);
210 cell = afs_lookup_cell_rcu(net, name, namesz);
216 /* Assume we're probably going to create a cell and preallocate and
217 * mostly set up a candidate record. We can then use this to stash the
218 * name, the net namespace and VL server addresses.
220 * We also want to do this before we hold any locks as it may involve
221 * upcalling to userspace to make DNS queries.
223 candidate = afs_alloc_cell(net, name, namesz, vllist);
224 if (IS_ERR(candidate)) {
225 _leave(" = %ld", PTR_ERR(candidate));
229 /* Find the insertion point and check to see if someone else added a
230 * cell whilst we were allocating.
232 write_seqlock(&net->cells_lock);
234 pp = &net->cells.rb_node;
238 cursor = rb_entry(parent, struct afs_cell, net_node);
240 n = strncasecmp(cursor->name, name,
241 min_t(size_t, cursor->name_len, namesz));
243 n = cursor->name_len - namesz;
245 pp = &(*pp)->rb_left;
247 pp = &(*pp)->rb_right;
249 goto cell_already_exists;
254 rb_link_node_rcu(&cell->net_node, parent, pp);
255 rb_insert_color(&cell->net_node, &net->cells);
256 atomic_inc(&net->cells_outstanding);
257 write_sequnlock(&net->cells_lock);
259 queue_work(afs_wq, &cell->manager);
262 _debug("wait_for_cell");
263 ret = wait_on_bit(&cell->flags, AFS_CELL_FL_NOT_READY, TASK_INTERRUPTIBLE);
266 switch (READ_ONCE(cell->state)) {
267 case AFS_CELL_FAILED:
271 _debug("weird %u %d", cell->state, cell->error);
273 case AFS_CELL_ACTIVE:
277 _leave(" = %p [cell]", cell);
281 _debug("cell exists");
286 afs_get_cell(cursor);
289 write_sequnlock(&net->cells_lock);
295 afs_put_cell(net, cell);
297 _leave(" = %d [error]", ret);
302 * set the root cell information
303 * - can be called with a module parameter string
304 * - can be called from a write to /proc/fs/afs/rootcell
306 int afs_cell_init(struct afs_net *net, const char *rootcell)
308 struct afs_cell *old_root, *new_root;
309 const char *cp, *vllist;
315 /* module is loaded with no parameters, or built statically.
316 * - in the future we might initialize cell DB here.
318 _leave(" = 0 [no root]");
322 cp = strchr(rootcell, ':');
324 _debug("kAFS: no VL server IP addresses specified");
326 len = strlen(rootcell);
332 /* allocate a cell record for the root cell */
333 new_root = afs_lookup_cell(net, rootcell, len, vllist, false);
334 if (IS_ERR(new_root)) {
335 _leave(" = %ld", PTR_ERR(new_root));
336 return PTR_ERR(new_root);
339 if (!test_and_set_bit(AFS_CELL_FL_NO_GC, &new_root->flags))
340 afs_get_cell(new_root);
342 /* install the new cell */
343 write_seqlock(&net->cells_lock);
344 old_root = net->ws_cell;
345 net->ws_cell = new_root;
346 write_sequnlock(&net->cells_lock);
348 afs_put_cell(net, old_root);
354 * Update a cell's VL server address list from the DNS.
356 static void afs_update_cell(struct afs_cell *cell)
358 struct afs_addr_list *alist, *old;
359 time64_t now, expiry;
361 _enter("%s", cell->name);
363 alist = afs_dns_query(cell, &expiry);
365 switch (PTR_ERR(alist)) {
367 /* The DNS said that the cell does not exist */
368 set_bit(AFS_CELL_FL_NOT_FOUND, &cell->flags);
369 clear_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags);
370 cell->dns_expiry = ktime_get_real_seconds() + 61;
376 set_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags);
377 cell->dns_expiry = ktime_get_real_seconds() + 10;
381 cell->error = -EDESTADDRREQ;
383 clear_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags);
384 clear_bit(AFS_CELL_FL_NOT_FOUND, &cell->flags);
386 /* Exclusion on changing vl_addrs is achieved by a
387 * non-reentrant work item.
389 old = rcu_dereference_protected(cell->vl_addrs, true);
390 rcu_assign_pointer(cell->vl_addrs, alist);
391 cell->dns_expiry = expiry;
394 afs_put_addrlist(old);
397 if (test_and_clear_bit(AFS_CELL_FL_NO_LOOKUP_YET, &cell->flags))
398 wake_up_bit(&cell->flags, AFS_CELL_FL_NO_LOOKUP_YET);
400 now = ktime_get_real_seconds();
401 afs_set_cell_timer(cell->net, cell->dns_expiry - now);
406 * Destroy a cell record
408 static void afs_cell_destroy(struct rcu_head *rcu)
410 struct afs_cell *cell = container_of(rcu, struct afs_cell, rcu);
412 _enter("%p{%s}", cell, cell->name);
414 ASSERTCMP(atomic_read(&cell->usage), ==, 0);
416 afs_put_addrlist(rcu_access_pointer(cell->vl_addrs));
417 key_put(cell->anonymous_key);
420 _leave(" [destroyed]");
424 * Queue the cell manager.
426 static void afs_queue_cell_manager(struct afs_net *net)
428 int outstanding = atomic_inc_return(&net->cells_outstanding);
430 _enter("%d", outstanding);
432 if (!queue_work(afs_wq, &net->cells_manager))
433 afs_dec_cells_outstanding(net);
437 * Cell management timer. We have an increment on cells_outstanding that we
438 * need to pass along to the work item.
440 void afs_cells_timer(struct timer_list *timer)
442 struct afs_net *net = container_of(timer, struct afs_net, cells_timer);
445 if (!queue_work(afs_wq, &net->cells_manager))
446 afs_dec_cells_outstanding(net);
450 * Get a reference on a cell record.
452 struct afs_cell *afs_get_cell(struct afs_cell *cell)
454 atomic_inc(&cell->usage);
459 * Drop a reference on a cell record.
461 void afs_put_cell(struct afs_net *net, struct afs_cell *cell)
463 time64_t now, expire_delay;
468 _enter("%s", cell->name);
470 now = ktime_get_real_seconds();
471 cell->last_inactive = now;
473 if (!test_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags) &&
474 !test_bit(AFS_CELL_FL_NOT_FOUND, &cell->flags))
475 expire_delay = afs_cell_gc_delay;
477 if (atomic_dec_return(&cell->usage) > 1)
480 /* 'cell' may now be garbage collected. */
481 afs_set_cell_timer(net, expire_delay);
485 * Allocate a key to use as a placeholder for anonymous user security.
487 static int afs_alloc_anon_key(struct afs_cell *cell)
490 char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp;
492 /* Create a key to represent an anonymous user. */
493 memcpy(keyname, "afs@", 4);
497 *dp++ = tolower(*cp);
500 key = rxrpc_get_null_key(keyname);
504 cell->anonymous_key = key;
506 _debug("anon key %p{%x}",
507 cell->anonymous_key, key_serial(cell->anonymous_key));
514 static int afs_activate_cell(struct afs_net *net, struct afs_cell *cell)
518 if (!cell->anonymous_key) {
519 ret = afs_alloc_anon_key(cell);
524 #ifdef CONFIG_AFS_FSCACHE
525 cell->cache = fscache_acquire_cookie(afs_cache_netfs.primary_index,
526 &afs_cell_cache_index_def,
527 cell->name, strlen(cell->name),
531 ret = afs_proc_cell_setup(net, cell);
534 spin_lock(&net->proc_cells_lock);
535 list_add_tail(&cell->proc_link, &net->proc_cells);
536 spin_unlock(&net->proc_cells_lock);
543 static void afs_deactivate_cell(struct afs_net *net, struct afs_cell *cell)
545 _enter("%s", cell->name);
547 afs_proc_cell_remove(net, cell);
549 spin_lock(&net->proc_cells_lock);
550 list_del_init(&cell->proc_link);
551 spin_unlock(&net->proc_cells_lock);
553 #ifdef CONFIG_AFS_FSCACHE
554 fscache_relinquish_cookie(cell->cache, NULL, false);
562 * Manage a cell record, initialising and destroying it, maintaining its DNS
565 static void afs_manage_cell(struct work_struct *work)
567 struct afs_cell *cell = container_of(work, struct afs_cell, manager);
568 struct afs_net *net = cell->net;
572 _enter("%s", cell->name);
575 _debug("state %u", cell->state);
576 switch (cell->state) {
577 case AFS_CELL_INACTIVE:
578 case AFS_CELL_FAILED:
579 write_seqlock(&net->cells_lock);
581 deleted = atomic_try_cmpxchg_relaxed(&cell->usage, &usage, 0);
583 rb_erase(&cell->net_node, &net->cells);
584 write_sequnlock(&net->cells_lock);
586 goto final_destruction;
587 if (cell->state == AFS_CELL_FAILED)
589 cell->state = AFS_CELL_UNSET;
593 cell->state = AFS_CELL_ACTIVATING;
596 case AFS_CELL_ACTIVATING:
597 ret = afs_activate_cell(net, cell);
599 goto activation_failed;
601 cell->state = AFS_CELL_ACTIVE;
603 clear_bit(AFS_CELL_FL_NOT_READY, &cell->flags);
604 wake_up_bit(&cell->flags, AFS_CELL_FL_NOT_READY);
607 case AFS_CELL_ACTIVE:
608 if (atomic_read(&cell->usage) > 1) {
609 time64_t now = ktime_get_real_seconds();
610 if (cell->dns_expiry <= now && net->live)
611 afs_update_cell(cell);
614 cell->state = AFS_CELL_DEACTIVATING;
617 case AFS_CELL_DEACTIVATING:
618 set_bit(AFS_CELL_FL_NOT_READY, &cell->flags);
619 if (atomic_read(&cell->usage) > 1)
620 goto reverse_deactivation;
621 afs_deactivate_cell(net, cell);
622 cell->state = AFS_CELL_INACTIVE;
628 _debug("bad state %u", cell->state);
629 BUG(); /* Unhandled state */
633 afs_deactivate_cell(net, cell);
635 cell->state = AFS_CELL_FAILED;
637 if (test_and_clear_bit(AFS_CELL_FL_NOT_READY, &cell->flags))
638 wake_up_bit(&cell->flags, AFS_CELL_FL_NOT_READY);
641 reverse_deactivation:
642 cell->state = AFS_CELL_ACTIVE;
644 clear_bit(AFS_CELL_FL_NOT_READY, &cell->flags);
645 wake_up_bit(&cell->flags, AFS_CELL_FL_NOT_READY);
646 _leave(" [deact->act]");
650 _leave(" [done %u]", cell->state);
654 call_rcu(&cell->rcu, afs_cell_destroy);
655 afs_dec_cells_outstanding(net);
656 _leave(" [destruct %d]", atomic_read(&net->cells_outstanding));
660 * Manage the records of cells known to a network namespace. This includes
661 * updating the DNS records and garbage collecting unused cells that were
662 * automatically added.
664 * Note that constructed cell records may only be removed from net->cells by
665 * this work item, so it is safe for this work item to stash a cursor pointing
666 * into the tree and then return to caller (provided it skips cells that are
667 * still under construction).
669 * Note also that we were given an increment on net->cells_outstanding by
670 * whoever queued us that we need to deal with before returning.
672 void afs_manage_cells(struct work_struct *work)
674 struct afs_net *net = container_of(work, struct afs_net, cells_manager);
675 struct rb_node *cursor;
676 time64_t now = ktime_get_real_seconds(), next_manage = TIME64_MAX;
677 bool purging = !net->live;
681 /* Trawl the cell database looking for cells that have expired from
682 * lack of use and cells whose DNS results have expired and dispatch
685 read_seqlock_excl(&net->cells_lock);
687 for (cursor = rb_first(&net->cells); cursor; cursor = rb_next(cursor)) {
688 struct afs_cell *cell =
689 rb_entry(cursor, struct afs_cell, net_node);
691 bool sched_cell = false;
693 usage = atomic_read(&cell->usage);
694 _debug("manage %s %u", cell->name, usage);
696 ASSERTCMP(usage, >=, 1);
699 if (test_and_clear_bit(AFS_CELL_FL_NO_GC, &cell->flags))
700 usage = atomic_dec_return(&cell->usage);
701 ASSERTCMP(usage, ==, 1);
705 time64_t expire_at = cell->last_inactive;
707 if (!test_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags) &&
708 !test_bit(AFS_CELL_FL_NOT_FOUND, &cell->flags))
709 expire_at += afs_cell_gc_delay;
710 if (purging || expire_at <= now)
712 else if (expire_at < next_manage)
713 next_manage = expire_at;
717 if (cell->dns_expiry <= now)
719 else if (cell->dns_expiry <= next_manage)
720 next_manage = cell->dns_expiry;
724 queue_work(afs_wq, &cell->manager);
727 read_sequnlock_excl(&net->cells_lock);
729 /* Update the timer on the way out. We have to pass an increment on
730 * cells_outstanding in the namespace that we are in to the timer or
731 * the work scheduler.
733 if (!purging && next_manage < TIME64_MAX) {
734 now = ktime_get_real_seconds();
736 if (next_manage - now <= 0) {
737 if (queue_work(afs_wq, &net->cells_manager))
738 atomic_inc(&net->cells_outstanding);
740 afs_set_cell_timer(net, next_manage - now);
744 afs_dec_cells_outstanding(net);
745 _leave(" [%d]", atomic_read(&net->cells_outstanding));
749 * Purge in-memory cell database.
751 void afs_cell_purge(struct afs_net *net)
757 write_seqlock(&net->cells_lock);
760 write_sequnlock(&net->cells_lock);
761 afs_put_cell(net, ws);
764 if (del_timer_sync(&net->cells_timer))
765 atomic_dec(&net->cells_outstanding);
768 afs_queue_cell_manager(net);
771 wait_var_event(&net->cells_outstanding,
772 !atomic_read(&net->cells_outstanding));