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/module.h>
13 #include <linux/slab.h>
14 #include <linux/key.h>
15 #include <linux/ctype.h>
16 #include <linux/dns_resolver.h>
17 #include <linux/sched.h>
18 #include <linux/inet.h>
19 #include <keys/rxrpc-type.h>
22 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_atomic_t(&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
,
124 struct afs_cell
*cell
;
129 return ERR_PTR(-EINVAL
);
130 if (namelen
> AFS_MAXCELLNAME
) {
131 _leave(" = -ENAMETOOLONG");
132 return ERR_PTR(-ENAMETOOLONG
);
135 _enter("%*.*s,%s", namelen
, namelen
, name
, vllist
);
137 cell
= kzalloc(sizeof(struct afs_cell
), GFP_KERNEL
);
139 _leave(" = -ENOMEM");
140 return ERR_PTR(-ENOMEM
);
144 cell
->name_len
= namelen
;
145 for (i
= 0; i
< namelen
; i
++)
146 cell
->name
[i
] = tolower(name
[i
]);
148 atomic_set(&cell
->usage
, 2);
149 INIT_WORK(&cell
->manager
, afs_manage_cell
);
150 rwlock_init(&cell
->servers_lock
);
151 INIT_LIST_HEAD(&cell
->servers
);
152 init_rwsem(&cell
->vl_sem
);
153 INIT_LIST_HEAD(&cell
->vl_list
);
154 spin_lock_init(&cell
->vl_lock
);
155 seqlock_init(&cell
->vl_addrs_lock
);
156 cell
->flags
= (1 << AFS_CELL_FL_NOT_READY
);
158 for (i
= 0; i
< AFS_CELL_MAX_ADDRS
; i
++) {
159 struct sockaddr_rxrpc
*srx
= &cell
->vl_addrs
[i
];
160 srx
->srx_family
= AF_RXRPC
;
161 srx
->srx_service
= VL_SERVICE
;
162 srx
->transport_type
= SOCK_DGRAM
;
163 srx
->transport
.sin6
.sin6_family
= AF_INET6
;
164 srx
->transport
.sin6
.sin6_port
= htons(AFS_VL_PORT
);
167 /* Fill in the VL server list if we were given a list of addresses to
173 if (strchr(vllist
, ',') || !strchr(vllist
, '.'))
177 struct sockaddr_rxrpc
*srx
= &cell
->vl_addrs
[cell
->vl_naddrs
];
179 if (in4_pton(vllist
, -1,
180 (u8
*)&srx
->transport
.sin6
.sin6_addr
.s6_addr32
[3],
182 srx
->transport_len
= sizeof(struct sockaddr_in6
);
183 srx
->transport
.sin6
.sin6_addr
.s6_addr32
[0] = 0;
184 srx
->transport
.sin6
.sin6_addr
.s6_addr32
[1] = 0;
185 srx
->transport
.sin6
.sin6_addr
.s6_addr32
[2] = htonl(0xffff);
186 } else if (in6_pton(vllist
, -1,
187 srx
->transport
.sin6
.sin6_addr
.s6_addr
,
189 srx
->transport_len
= sizeof(struct sockaddr_in6
);
190 srx
->transport
.sin6
.sin6_family
= AF_INET6
;
200 } while (cell
->vl_naddrs
< AFS_CELL_MAX_ADDRS
&& vllist
);
202 /* Disable DNS refresh for manually-specified cells */
203 cell
->dns_expiry
= TIME64_MAX
;
205 /* We're going to need to 'refresh' this cell's VL server list
206 * from the DNS before we can use it.
208 cell
->dns_expiry
= S64_MIN
;
211 _leave(" = %p", cell
);
215 printk(KERN_ERR
"kAFS: bad VL server IP address\n");
218 _leave(" = %d", ret
);
223 * afs_lookup_cell - Look up or create a cell record.
224 * @net: The network namespace
225 * @name: The name of the cell.
226 * @namesz: The strlen of the cell name.
227 * @vllist: A colon/comma separated list of numeric IP addresses or NULL.
228 * @excl: T if an error should be given if the cell name already exists.
230 * Look up a cell record by name and query the DNS for VL server addresses if
231 * needed. Note that that actual DNS query is punted off to the manager thread
232 * so that this function can return immediately if interrupted whilst allowing
233 * cell records to be shared even if not yet fully constructed.
235 struct afs_cell
*afs_lookup_cell(struct afs_net
*net
,
236 const char *name
, unsigned int namesz
,
237 const char *vllist
, bool excl
)
239 struct afs_cell
*cell
, *candidate
, *cursor
;
240 struct rb_node
*parent
, **pp
;
243 _enter("%s,%s", name
, vllist
);
247 cell
= afs_lookup_cell_rcu(net
, name
, namesz
);
251 afs_put_cell(net
, cell
);
252 return ERR_PTR(-EEXIST
);
258 /* Assume we're probably going to create a cell and preallocate and
259 * mostly set up a candidate record. We can then use this to stash the
260 * name, the net namespace and VL server addresses.
262 * We also want to do this before we hold any locks as it may involve
263 * upcalling to userspace to make DNS queries.
265 candidate
= afs_alloc_cell(net
, name
, namesz
, vllist
);
266 if (IS_ERR(candidate
)) {
267 _leave(" = %ld", PTR_ERR(candidate
));
271 /* Find the insertion point and check to see if someone else added a
272 * cell whilst we were allocating.
274 write_seqlock(&net
->cells_lock
);
276 pp
= &net
->cells
.rb_node
;
280 cursor
= rb_entry(parent
, struct afs_cell
, net_node
);
282 n
= strncasecmp(cursor
->name
, name
,
283 min_t(size_t, cursor
->name_len
, namesz
));
285 n
= cursor
->name_len
- namesz
;
287 pp
= &(*pp
)->rb_left
;
289 pp
= &(*pp
)->rb_right
;
291 goto cell_already_exists
;
296 rb_link_node_rcu(&cell
->net_node
, parent
, pp
);
297 rb_insert_color(&cell
->net_node
, &net
->cells
);
298 atomic_inc(&net
->cells_outstanding
);
299 write_sequnlock(&net
->cells_lock
);
301 queue_work(afs_wq
, &cell
->manager
);
304 _debug("wait_for_cell");
305 ret
= wait_on_bit(&cell
->flags
, AFS_CELL_FL_NOT_READY
, TASK_INTERRUPTIBLE
);
308 switch (READ_ONCE(cell
->state
)) {
309 case AFS_CELL_FAILED
:
313 _debug("weird %u %d", cell
->state
, cell
->error
);
315 case AFS_CELL_ACTIVE
:
319 _leave(" = %p [cell]", cell
);
323 _debug("cell exists");
328 ASSERTCMP(atomic_read(&cursor
->usage
), >=, 1);
329 afs_get_cell(cursor
);
332 write_sequnlock(&net
->cells_lock
);
337 afs_put_cell(net
, cell
);
338 _leave(" = %d [error]", ret
);
343 * set the root cell information
344 * - can be called with a module parameter string
345 * - can be called from a write to /proc/fs/afs/rootcell
347 int afs_cell_init(struct afs_net
*net
, const char *rootcell
)
349 struct afs_cell
*old_root
, *new_root
;
350 const char *cp
, *vllist
;
356 /* module is loaded with no parameters, or built statically.
357 * - in the future we might initialize cell DB here.
359 _leave(" = 0 [no root]");
363 cp
= strchr(rootcell
, ':');
365 _debug("kAFS: no VL server IP addresses specified");
367 len
= strlen(rootcell
);
373 /* allocate a cell record for the root cell */
374 new_root
= afs_lookup_cell(net
, rootcell
, len
, vllist
, false);
375 if (IS_ERR(new_root
)) {
376 _leave(" = %ld", PTR_ERR(new_root
));
377 return PTR_ERR(new_root
);
380 set_bit(AFS_CELL_FL_NO_GC
, &new_root
->flags
);
381 afs_get_cell(new_root
);
383 /* install the new cell */
384 write_seqlock(&net
->cells_lock
);
385 old_root
= net
->ws_cell
;
386 net
->ws_cell
= new_root
;
387 write_sequnlock(&net
->cells_lock
);
389 afs_put_cell(net
, old_root
);
395 * Update a cell's VL server address list from the DNS.
397 static void afs_update_cell(struct afs_cell
*cell
)
399 time64_t now
, expiry
;
403 _enter("%s", cell
->name
);
405 ret
= dns_query("afsdb", cell
->name
, cell
->name_len
,
406 "ipv4", &vllist
, &expiry
);
407 _debug("query %d", ret
);
410 clear_bit(AFS_CELL_FL_DNS_FAIL
, &cell
->flags
);
411 clear_bit(AFS_CELL_FL_NOT_FOUND
, &cell
->flags
);
415 clear_bit(AFS_CELL_FL_DNS_FAIL
, &cell
->flags
);
416 set_bit(AFS_CELL_FL_NOT_FOUND
, &cell
->flags
);
417 cell
->dns_expiry
= ktime_get_real_seconds() + 61;
418 cell
->error
= -EDESTADDRREQ
;
424 /* Unable to query DNS. */
425 set_bit(AFS_CELL_FL_DNS_FAIL
, &cell
->flags
);
426 cell
->dns_expiry
= ktime_get_real_seconds() + 10;
427 cell
->error
= -EDESTADDRREQ
;
432 write_seqlock(&cell
->vl_addrs_lock
);
436 struct sockaddr_rxrpc
*srx
= &cell
->vl_addrs
[cell
->vl_naddrs
];
438 if (in4_pton(vllist
, -1,
439 (u8
*)&srx
->transport
.sin6
.sin6_addr
.s6_addr32
[3],
440 ',', (const char **)&vllist
)) {
441 srx
->transport_len
= sizeof(struct sockaddr_in6
);
442 srx
->transport
.sin6
.sin6_addr
.s6_addr32
[0] = 0;
443 srx
->transport
.sin6
.sin6_addr
.s6_addr32
[1] = 0;
444 srx
->transport
.sin6
.sin6_addr
.s6_addr32
[2] = htonl(0xffff);
445 } else if (in6_pton(vllist
, -1,
446 srx
->transport
.sin6
.sin6_addr
.s6_addr
,
447 ',', (const char **)&vllist
)) {
448 srx
->transport_len
= sizeof(struct sockaddr_in6
);
449 srx
->transport
.sin6
.sin6_family
= AF_INET6
;
459 } while (cell
->vl_naddrs
< AFS_CELL_MAX_ADDRS
);
461 if (cell
->vl_naddrs
< AFS_CELL_MAX_ADDRS
)
462 memset(cell
->vl_addrs
+ cell
->vl_naddrs
, 0,
463 (AFS_CELL_MAX_ADDRS
- cell
->vl_naddrs
) * sizeof(cell
->vl_addrs
[0]));
465 now
= ktime_get_real_seconds();
466 cell
->dns_expiry
= expiry
;
467 afs_set_cell_timer(cell
->net
, expiry
- now
);
469 write_sequnlock(&cell
->vl_addrs_lock
);
475 * Destroy a cell record
477 static void afs_cell_destroy(struct rcu_head
*rcu
)
479 struct afs_cell
*cell
= container_of(rcu
, struct afs_cell
, rcu
);
481 _enter("%p{%s}", cell
, cell
->name
);
483 ASSERTCMP(atomic_read(&cell
->usage
), ==, 0);
485 key_put(cell
->anonymous_key
);
488 _leave(" [destroyed]");
492 * Queue the cell manager.
494 static void afs_queue_cell_manager(struct afs_net
*net
)
496 int outstanding
= atomic_inc_return(&net
->cells_outstanding
);
498 _enter("%d", outstanding
);
500 if (!queue_work(afs_wq
, &net
->cells_manager
))
501 afs_dec_cells_outstanding(net
);
505 * Cell management timer. We have an increment on cells_outstanding that we
506 * need to pass along to the work item.
508 void afs_cells_timer(struct timer_list
*timer
)
510 struct afs_net
*net
= container_of(timer
, struct afs_net
, cells_timer
);
513 if (!queue_work(afs_wq
, &net
->cells_manager
))
514 afs_dec_cells_outstanding(net
);
518 * Drop a reference on a cell record.
520 void afs_put_cell(struct afs_net
*net
, struct afs_cell
*cell
)
522 time64_t now
, expire_delay
;
527 _enter("%s", cell
->name
);
529 now
= ktime_get_real_seconds();
530 cell
->last_inactive
= now
;
532 if (!test_bit(AFS_CELL_FL_DNS_FAIL
, &cell
->flags
) &&
533 !test_bit(AFS_CELL_FL_NOT_FOUND
, &cell
->flags
))
534 expire_delay
= afs_cell_gc_delay
;
536 if (atomic_dec_return(&cell
->usage
) > 1)
539 /* 'cell' may now be garbage collected. */
540 afs_set_cell_timer(net
, expire_delay
);
544 * Allocate a key to use as a placeholder for anonymous user security.
546 static int afs_alloc_anon_key(struct afs_cell
*cell
)
549 char keyname
[4 + AFS_MAXCELLNAME
+ 1], *cp
, *dp
;
551 /* Create a key to represent an anonymous user. */
552 memcpy(keyname
, "afs@", 4);
556 *dp
++ = tolower(*cp
);
559 key
= rxrpc_get_null_key(keyname
);
563 cell
->anonymous_key
= key
;
565 _debug("anon key %p{%x}",
566 cell
->anonymous_key
, key_serial(cell
->anonymous_key
));
573 static int afs_activate_cell(struct afs_net
*net
, struct afs_cell
*cell
)
577 if (!cell
->anonymous_key
) {
578 ret
= afs_alloc_anon_key(cell
);
583 #ifdef CONFIG_AFS_FSCACHE
584 cell
->cache
= fscache_acquire_cookie(afs_cache_netfs
.primary_index
,
585 &afs_cell_cache_index_def
,
588 ret
= afs_proc_cell_setup(net
, cell
);
591 spin_lock(&net
->proc_cells_lock
);
592 list_add_tail(&cell
->proc_link
, &net
->proc_cells
);
593 spin_unlock(&net
->proc_cells_lock
);
600 static void afs_deactivate_cell(struct afs_net
*net
, struct afs_cell
*cell
)
602 _enter("%s", cell
->name
);
604 afs_proc_cell_remove(net
, cell
);
606 spin_lock(&net
->proc_cells_lock
);
607 list_del_init(&cell
->proc_link
);
608 spin_unlock(&net
->proc_cells_lock
);
610 #ifdef CONFIG_AFS_FSCACHE
611 fscache_relinquish_cookie(cell
->cache
, 0);
619 * Manage a cell record, initialising and destroying it, maintaining its DNS
622 static void afs_manage_cell(struct work_struct
*work
)
624 struct afs_cell
*cell
= container_of(work
, struct afs_cell
, manager
);
625 struct afs_net
*net
= cell
->net
;
629 _enter("%s", cell
->name
);
632 _debug("state %u", cell
->state
);
633 switch (cell
->state
) {
634 case AFS_CELL_INACTIVE
:
635 case AFS_CELL_FAILED
:
636 write_seqlock(&net
->cells_lock
);
638 deleted
= atomic_try_cmpxchg_relaxed(&cell
->usage
, &usage
, 0);
640 rb_erase(&cell
->net_node
, &net
->cells
);
641 write_sequnlock(&net
->cells_lock
);
643 goto final_destruction
;
644 if (cell
->state
== AFS_CELL_FAILED
)
646 cell
->state
= AFS_CELL_UNSET
;
650 cell
->state
= AFS_CELL_ACTIVATING
;
653 case AFS_CELL_ACTIVATING
:
654 ret
= afs_activate_cell(net
, cell
);
656 goto activation_failed
;
658 cell
->state
= AFS_CELL_ACTIVE
;
660 clear_bit(AFS_CELL_FL_NOT_READY
, &cell
->flags
);
661 wake_up_bit(&cell
->flags
, AFS_CELL_FL_NOT_READY
);
664 case AFS_CELL_ACTIVE
:
665 if (atomic_read(&cell
->usage
) > 1) {
666 time64_t now
= ktime_get_real_seconds();
667 if (cell
->dns_expiry
<= now
&& net
->live
)
668 afs_update_cell(cell
);
671 cell
->state
= AFS_CELL_DEACTIVATING
;
674 case AFS_CELL_DEACTIVATING
:
675 set_bit(AFS_CELL_FL_NOT_READY
, &cell
->flags
);
676 if (atomic_read(&cell
->usage
) > 1)
677 goto reverse_deactivation
;
678 afs_deactivate_cell(net
, cell
);
679 cell
->state
= AFS_CELL_INACTIVE
;
685 _debug("bad state %u", cell
->state
);
686 BUG(); /* Unhandled state */
690 afs_deactivate_cell(net
, cell
);
692 cell
->state
= AFS_CELL_FAILED
;
694 if (test_and_clear_bit(AFS_CELL_FL_NOT_READY
, &cell
->flags
))
695 wake_up_bit(&cell
->flags
, AFS_CELL_FL_NOT_READY
);
698 reverse_deactivation
:
699 cell
->state
= AFS_CELL_ACTIVE
;
701 clear_bit(AFS_CELL_FL_NOT_READY
, &cell
->flags
);
702 wake_up_bit(&cell
->flags
, AFS_CELL_FL_NOT_READY
);
703 _leave(" [deact->act]");
707 _leave(" [done %u]", cell
->state
);
711 call_rcu(&cell
->rcu
, afs_cell_destroy
);
712 afs_dec_cells_outstanding(net
);
713 _leave(" [destruct %d]", atomic_read(&net
->cells_outstanding
));
717 * Manage the records of cells known to a network namespace. This includes
718 * updating the DNS records and garbage collecting unused cells that were
719 * automatically added.
721 * Note that constructed cell records may only be removed from net->cells by
722 * this work item, so it is safe for this work item to stash a cursor pointing
723 * into the tree and then return to caller (provided it skips cells that are
724 * still under construction).
726 * Note also that we were given an increment on net->cells_outstanding by
727 * whoever queued us that we need to deal with before returning.
729 void afs_manage_cells(struct work_struct
*work
)
731 struct afs_net
*net
= container_of(work
, struct afs_net
, cells_manager
);
732 struct rb_node
*cursor
;
733 time64_t now
= ktime_get_real_seconds(), next_manage
= TIME64_MAX
;
734 bool purging
= !net
->live
;
738 /* Trawl the cell database looking for cells that have expired from
739 * lack of use and cells whose DNS results have expired and dispatch
742 read_seqlock_excl(&net
->cells_lock
);
744 for (cursor
= rb_first(&net
->cells
); cursor
; cursor
= rb_next(cursor
)) {
745 struct afs_cell
*cell
=
746 rb_entry(cursor
, struct afs_cell
, net_node
);
748 bool sched_cell
= false;
750 usage
= atomic_read(&cell
->usage
);
751 _debug("manage %s %u", cell
->name
, usage
);
753 ASSERTCMP(usage
, >=, 1);
756 if (test_and_clear_bit(AFS_CELL_FL_NO_GC
, &cell
->flags
))
757 usage
= atomic_dec_return(&cell
->usage
);
758 ASSERTCMP(usage
, ==, 1);
762 time64_t expire_at
= cell
->last_inactive
;
764 if (!test_bit(AFS_CELL_FL_DNS_FAIL
, &cell
->flags
) &&
765 !test_bit(AFS_CELL_FL_NOT_FOUND
, &cell
->flags
))
766 expire_at
+= afs_cell_gc_delay
;
767 if (purging
|| expire_at
<= now
)
769 else if (expire_at
< next_manage
)
770 next_manage
= expire_at
;
774 if (cell
->dns_expiry
<= now
)
776 else if (cell
->dns_expiry
<= next_manage
)
777 next_manage
= cell
->dns_expiry
;
781 queue_work(afs_wq
, &cell
->manager
);
784 read_sequnlock_excl(&net
->cells_lock
);
786 /* Update the timer on the way out. We have to pass an increment on
787 * cells_outstanding in the namespace that we are in to the timer or
788 * the work scheduler.
790 if (!purging
&& next_manage
< TIME64_MAX
) {
791 now
= ktime_get_real_seconds();
793 if (next_manage
- now
<= 0) {
794 if (queue_work(afs_wq
, &net
->cells_manager
))
795 atomic_inc(&net
->cells_outstanding
);
797 afs_set_cell_timer(net
, next_manage
- now
);
801 afs_dec_cells_outstanding(net
);
802 _leave(" [%d]", atomic_read(&net
->cells_outstanding
));
806 * Purge in-memory cell database.
808 void afs_cell_purge(struct afs_net
*net
)
814 write_seqlock(&net
->cells_lock
);
817 write_sequnlock(&net
->cells_lock
);
818 afs_put_cell(net
, ws
);
821 if (del_timer_sync(&net
->cells_timer
))
822 atomic_dec(&net
->cells_outstanding
);
825 afs_queue_cell_manager(net
);
828 wait_on_atomic_t(&net
->cells_outstanding
, atomic_t_wait
,
829 TASK_UNINTERRUPTIBLE
);