blob: a2a87117d2626220719cd10e71572c0e5f1b385d [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howellsec268152007-04-26 15:49:28 -07002/* AFS cell and server record management
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
David Howells989782d2017-11-02 15:27:50 +00004 * Copyright (C) 2002, 2017 Red Hat, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Written by David Howells (dhowells@redhat.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/slab.h>
David Howells00d3b7a2007-04-26 15:57:07 -07009#include <linux/key.h>
10#include <linux/ctype.h>
Wang Lei07567a52010-08-04 15:16:38 +010011#include <linux/dns_resolver.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040012#include <linux/sched.h>
David Howells3838d3e2017-11-02 15:27:47 +000013#include <linux/inet.h>
David Howells0da0b7f2018-06-15 15:19:22 +010014#include <linux/namei.h>
David Howells00d3b7a2007-04-26 15:57:07 -070015#include <keys/rxrpc-type.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "internal.h"
17
David Howellsfe342cf2018-04-09 21:12:31 +010018static unsigned __read_mostly afs_cell_gc_delay = 10;
David Howellsded2f4c2018-10-20 00:57:57 +010019static unsigned __read_mostly afs_cell_min_ttl = 10 * 60;
20static unsigned __read_mostly afs_cell_max_ttl = 24 * 60 * 60;
David Howells989782d2017-11-02 15:27:50 +000021
22static void afs_manage_cell(struct work_struct *);
23
24static void afs_dec_cells_outstanding(struct afs_net *net)
25{
26 if (atomic_dec_and_test(&net->cells_outstanding))
Peter Zijlstraab1fbe32018-03-15 11:42:28 +010027 wake_up_var(&net->cells_outstanding);
David Howells989782d2017-11-02 15:27:50 +000028}
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/*
David Howells989782d2017-11-02 15:27:50 +000031 * Set the cell timer to fire after a given delay, assuming it's not already
32 * set for an earlier time.
33 */
34static void afs_set_cell_timer(struct afs_net *net, time64_t delay)
35{
36 if (net->live) {
37 atomic_inc(&net->cells_outstanding);
38 if (timer_reduce(&net->cells_timer, jiffies + delay * HZ))
39 afs_dec_cells_outstanding(net);
40 }
41}
42
43/*
44 * Look up and get an activation reference on a cell record under RCU
45 * conditions. The caller must hold the RCU read lock.
46 */
47struct afs_cell *afs_lookup_cell_rcu(struct afs_net *net,
48 const char *name, unsigned int namesz)
49{
50 struct afs_cell *cell = NULL;
51 struct rb_node *p;
52 int n, seq = 0, ret = 0;
53
54 _enter("%*.*s", namesz, namesz, name);
55
56 if (name && namesz == 0)
57 return ERR_PTR(-EINVAL);
58 if (namesz > AFS_MAXCELLNAME)
59 return ERR_PTR(-ENAMETOOLONG);
60
61 do {
62 /* Unfortunately, rbtree walking doesn't give reliable results
63 * under just the RCU read lock, so we have to check for
64 * changes.
65 */
66 if (cell)
67 afs_put_cell(net, cell);
68 cell = NULL;
69 ret = -ENOENT;
70
71 read_seqbegin_or_lock(&net->cells_lock, &seq);
72
73 if (!name) {
74 cell = rcu_dereference_raw(net->ws_cell);
75 if (cell) {
76 afs_get_cell(cell);
David Howellsfe342cf2018-04-09 21:12:31 +010077 break;
David Howells989782d2017-11-02 15:27:50 +000078 }
79 ret = -EDESTADDRREQ;
80 continue;
81 }
82
83 p = rcu_dereference_raw(net->cells.rb_node);
84 while (p) {
85 cell = rb_entry(p, struct afs_cell, net_node);
86
87 n = strncasecmp(cell->name, name,
88 min_t(size_t, cell->name_len, namesz));
89 if (n == 0)
90 n = cell->name_len - namesz;
91 if (n < 0) {
92 p = rcu_dereference_raw(p->rb_left);
93 } else if (n > 0) {
94 p = rcu_dereference_raw(p->rb_right);
95 } else {
96 if (atomic_inc_not_zero(&cell->usage)) {
97 ret = 0;
98 break;
99 }
100 /* We want to repeat the search, this time with
101 * the lock properly locked.
102 */
103 }
104 cell = NULL;
105 }
106
107 } while (need_seqretry(&net->cells_lock, seq));
108
109 done_seqretry(&net->cells_lock, seq);
110
111 return ret == 0 ? cell : ERR_PTR(ret);
112}
113
114/*
115 * Set up a cell record and fill in its name, VL server address list and
David Howells00d3b7a2007-04-26 15:57:07 -0700116 * allocate an anonymous key
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
David Howells989782d2017-11-02 15:27:50 +0000118static struct afs_cell *afs_alloc_cell(struct afs_net *net,
119 const char *name, unsigned int namelen,
David Howells0a5143f2018-10-20 00:57:57 +0100120 const char *addresses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
David Howellsca1cbbd2019-05-07 15:30:34 +0100122 struct afs_vlserver_list *vllist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 struct afs_cell *cell;
David Howells989782d2017-11-02 15:27:50 +0000124 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
David Howells989782d2017-11-02 15:27:50 +0000126 ASSERT(name);
127 if (namelen == 0)
128 return ERR_PTR(-EINVAL);
Wang Lei07567a52010-08-04 15:16:38 +0100129 if (namelen > AFS_MAXCELLNAME) {
130 _leave(" = -ENAMETOOLONG");
David Howells00d3b7a2007-04-26 15:57:07 -0700131 return ERR_PTR(-ENAMETOOLONG);
Wang Lei07567a52010-08-04 15:16:38 +0100132 }
David Howells37ab6362018-04-06 14:17:23 +0100133 if (namelen == 5 && memcmp(name, "@cell", 5) == 0)
134 return ERR_PTR(-EINVAL);
David Howells00d3b7a2007-04-26 15:57:07 -0700135
David Howells0a5143f2018-10-20 00:57:57 +0100136 _enter("%*.*s,%s", namelen, namelen, name, addresses);
David Howells989782d2017-11-02 15:27:50 +0000137
138 cell = kzalloc(sizeof(struct afs_cell), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if (!cell) {
140 _leave(" = -ENOMEM");
David Howells08e0e7c2007-04-26 15:55:03 -0700141 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
143
David Howellsf044c882017-11-02 15:27:45 +0000144 cell->net = net;
David Howells989782d2017-11-02 15:27:50 +0000145 cell->name_len = namelen;
146 for (i = 0; i < namelen; i++)
147 cell->name[i] = tolower(name[i]);
148
149 atomic_set(&cell->usage, 2);
150 INIT_WORK(&cell->manager, afs_manage_cell);
David Howellsd2ddc772017-11-02 15:27:50 +0000151 INIT_LIST_HEAD(&cell->proc_volumes);
152 rwlock_init(&cell->proc_lock);
David Howells0a5143f2018-10-20 00:57:57 +0100153 rwlock_init(&cell->vl_servers_lock);
David Howells4d9df982017-11-02 15:27:47 +0000154
David Howellsca1cbbd2019-05-07 15:30:34 +0100155 /* Provide a VL server list, filling it in if we were given a list of
156 * addresses to use.
David Howells989782d2017-11-02 15:27:50 +0000157 */
David Howells0a5143f2018-10-20 00:57:57 +0100158 if (addresses) {
David Howells0a5143f2018-10-20 00:57:57 +0100159 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);
David Howells8b2a4642017-11-02 15:27:50 +0000164 goto parse_failed;
165 }
David Howells989782d2017-11-02 15:27:50 +0000166
David Howellsd5c32c82019-05-07 15:06:36 +0100167 vllist->source = DNS_RECORD_FROM_CONFIG;
168 vllist->status = DNS_LOOKUP_NOT_DONE;
David Howells989782d2017-11-02 15:27:50 +0000169 cell->dns_expiry = TIME64_MAX;
David Howellsded2f4c2018-10-20 00:57:57 +0100170 } else {
David Howellsca1cbbd2019-05-07 15:30:34 +0100171 ret = -ENOMEM;
172 vllist = afs_alloc_vlserver_list(0);
173 if (!vllist)
174 goto error;
David Howellsd5c32c82019-05-07 15:06:36 +0100175 vllist->source = DNS_RECORD_UNAVAILABLE;
176 vllist->status = DNS_LOOKUP_NOT_DONE;
David Howellsded2f4c2018-10-20 00:57:57 +0100177 cell->dns_expiry = ktime_get_real_seconds();
Wang Lei07567a52010-08-04 15:16:38 +0100178 }
179
David Howellsca1cbbd2019-05-07 15:30:34 +0100180 rcu_assign_pointer(cell->vl_servers, vllist);
181
David Howellsd5c32c82019-05-07 15:06:36 +0100182 cell->dns_source = vllist->source;
183 cell->dns_status = vllist->status;
184 smp_store_release(&cell->dns_lookup_count, 1); /* vs source/status */
185
David Howells00d3b7a2007-04-26 15:57:07 -0700186 _leave(" = %p", cell);
187 return cell;
188
David Howells8b2a4642017-11-02 15:27:50 +0000189parse_failed:
190 if (ret == -EINVAL)
191 printk(KERN_ERR "kAFS: bad VL server IP address\n");
David Howellsca1cbbd2019-05-07 15:30:34 +0100192error:
David Howells00d3b7a2007-04-26 15:57:07 -0700193 kfree(cell);
194 _leave(" = %d", ret);
195 return ERR_PTR(ret);
196}
197
198/*
David Howells989782d2017-11-02 15:27:50 +0000199 * afs_lookup_cell - Look up or create a cell record.
David Howellsf044c882017-11-02 15:27:45 +0000200 * @net: The network namespace
David Howells989782d2017-11-02 15:27:50 +0000201 * @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.
205 *
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.
David Howells00d3b7a2007-04-26 15:57:07 -0700210 */
David Howells989782d2017-11-02 15:27:50 +0000211struct afs_cell *afs_lookup_cell(struct afs_net *net,
212 const char *name, unsigned int namesz,
213 const char *vllist, bool excl)
David Howells00d3b7a2007-04-26 15:57:07 -0700214{
David Howells989782d2017-11-02 15:27:50 +0000215 struct afs_cell *cell, *candidate, *cursor;
216 struct rb_node *parent, **pp;
David Howellsd5c32c82019-05-07 15:06:36 +0100217 enum afs_cell_state state;
David Howells989782d2017-11-02 15:27:50 +0000218 int ret, n;
David Howells00d3b7a2007-04-26 15:57:07 -0700219
David Howells989782d2017-11-02 15:27:50 +0000220 _enter("%s,%s", name, vllist);
David Howells00d3b7a2007-04-26 15:57:07 -0700221
David Howells989782d2017-11-02 15:27:50 +0000222 if (!excl) {
223 rcu_read_lock();
224 cell = afs_lookup_cell_rcu(net, name, namesz);
225 rcu_read_unlock();
Gustavo A. R. Silva68327952017-11-17 16:40:32 -0600226 if (!IS_ERR(cell))
David Howells989782d2017-11-02 15:27:50 +0000227 goto wait_for_cell;
David Howells00d3b7a2007-04-26 15:57:07 -0700228 }
229
David Howells989782d2017-11-02 15:27:50 +0000230 /* 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.
233 *
234 * We also want to do this before we hold any locks as it may involve
235 * upcalling to userspace to make DNS queries.
236 */
237 candidate = afs_alloc_cell(net, name, namesz, vllist);
238 if (IS_ERR(candidate)) {
239 _leave(" = %ld", PTR_ERR(candidate));
240 return candidate;
241 }
242
243 /* Find the insertion point and check to see if someone else added a
244 * cell whilst we were allocating.
245 */
246 write_seqlock(&net->cells_lock);
247
248 pp = &net->cells.rb_node;
249 parent = NULL;
250 while (*pp) {
251 parent = *pp;
252 cursor = rb_entry(parent, struct afs_cell, net_node);
253
254 n = strncasecmp(cursor->name, name,
255 min_t(size_t, cursor->name_len, namesz));
256 if (n == 0)
257 n = cursor->name_len - namesz;
258 if (n < 0)
259 pp = &(*pp)->rb_left;
260 else if (n > 0)
261 pp = &(*pp)->rb_right;
262 else
263 goto cell_already_exists;
264 }
265
266 cell = candidate;
267 candidate = NULL;
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);
272
273 queue_work(afs_wq, &cell->manager);
274
275wait_for_cell:
276 _debug("wait_for_cell");
David Howellsd5c32c82019-05-07 15:06:36 +0100277 wait_var_event(&cell->state,
278 ({
279 state = smp_load_acquire(&cell->state); /* vs error */
280 state == AFS_CELL_ACTIVE || state == AFS_CELL_FAILED;
281 }));
David Howells989782d2017-11-02 15:27:50 +0000282
David Howellsd5c32c82019-05-07 15:06:36 +0100283 /* Check the state obtained from the wait check. */
284 if (state == AFS_CELL_FAILED) {
David Howells989782d2017-11-02 15:27:50 +0000285 ret = cell->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 goto error;
David Howells989782d2017-11-02 15:27:50 +0000287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
David Howells989782d2017-11-02 15:27:50 +0000289 _leave(" = %p [cell]", cell);
David Howells08e0e7c2007-04-26 15:55:03 -0700290 return cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
David Howells989782d2017-11-02 15:27:50 +0000292cell_already_exists:
293 _debug("cell exists");
294 cell = cursor;
295 if (excl) {
296 ret = -EEXIST;
297 } else {
David Howells989782d2017-11-02 15:27:50 +0000298 afs_get_cell(cursor);
299 ret = 0;
wangleibec5eb62010-08-11 09:38:04 +0100300 }
David Howells989782d2017-11-02 15:27:50 +0000301 write_sequnlock(&net->cells_lock);
302 kfree(candidate);
303 if (ret == 0)
304 goto wait_for_cell;
David Howells8b2a4642017-11-02 15:27:50 +0000305 goto error_noput;
David Howells989782d2017-11-02 15:27:50 +0000306error:
307 afs_put_cell(net, cell);
David Howells8b2a4642017-11-02 15:27:50 +0000308error_noput:
David Howells989782d2017-11-02 15:27:50 +0000309 _leave(" = %d [error]", ret);
310 return ERR_PTR(ret);
David Howellsec268152007-04-26 15:49:28 -0700311}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313/*
David Howells08e0e7c2007-04-26 15:55:03 -0700314 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 */
David Howells989782d2017-11-02 15:27:50 +0000318int afs_cell_init(struct afs_net *net, const char *rootcell)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 struct afs_cell *old_root, *new_root;
David Howells989782d2017-11-02 15:27:50 +0000321 const char *cp, *vllist;
322 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 _enter("");
325
326 if (!rootcell) {
327 /* module is loaded with no parameters, or built statically.
328 * - in the future we might initialize cell DB here.
329 */
David Howells08e0e7c2007-04-26 15:55:03 -0700330 _leave(" = 0 [no root]");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return 0;
332 }
333
334 cp = strchr(rootcell, ':');
David Howells989782d2017-11-02 15:27:50 +0000335 if (!cp) {
Wang Lei07567a52010-08-04 15:16:38 +0100336 _debug("kAFS: no VL server IP addresses specified");
David Howells989782d2017-11-02 15:27:50 +0000337 vllist = NULL;
338 len = strlen(rootcell);
339 } else {
340 vllist = cp + 1;
341 len = cp - rootcell;
342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 /* allocate a cell record for the root cell */
David Howells989782d2017-11-02 15:27:50 +0000345 new_root = afs_lookup_cell(net, rootcell, len, vllist, false);
David Howells08e0e7c2007-04-26 15:55:03 -0700346 if (IS_ERR(new_root)) {
347 _leave(" = %ld", PTR_ERR(new_root));
348 return PTR_ERR(new_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350
David Howells17814ae2018-04-09 21:12:31 +0100351 if (!test_and_set_bit(AFS_CELL_FL_NO_GC, &new_root->flags))
352 afs_get_cell(new_root);
David Howells989782d2017-11-02 15:27:50 +0000353
David Howells08e0e7c2007-04-26 15:55:03 -0700354 /* install the new cell */
David Howells989782d2017-11-02 15:27:50 +0000355 write_seqlock(&net->cells_lock);
David Howells1588def2018-05-23 11:51:29 +0100356 old_root = rcu_access_pointer(net->ws_cell);
357 rcu_assign_pointer(net->ws_cell, new_root);
David Howells989782d2017-11-02 15:27:50 +0000358 write_sequnlock(&net->cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
David Howells989782d2017-11-02 15:27:50 +0000360 afs_put_cell(net, old_root);
David Howells08e0e7c2007-04-26 15:55:03 -0700361 _leave(" = 0");
362 return 0;
David Howellsec268152007-04-26 15:49:28 -0700363}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365/*
David Howells989782d2017-11-02 15:27:50 +0000366 * Update a cell's VL server address list from the DNS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 */
David Howellsd5c32c82019-05-07 15:06:36 +0100368static int afs_update_cell(struct afs_cell *cell)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
David Howellsd5c32c82019-05-07 15:06:36 +0100370 struct afs_vlserver_list *vllist, *old = NULL, *p;
David Howellsded2f4c2018-10-20 00:57:57 +0100371 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;
David Howellsd5c32c82019-05-07 15:06:36 +0100374 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
David Howells989782d2017-11-02 15:27:50 +0000376 _enter("%s", cell->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
David Howells0a5143f2018-10-20 00:57:57 +0100378 vllist = afs_dns_query(cell, &expiry);
David Howellsd5c32c82019-05-07 15:06:36 +0100379 if (IS_ERR(vllist)) {
380 ret = PTR_ERR(vllist);
381
382 _debug("%s: fail %d", cell->name, ret);
383 if (ret == -ENOMEM)
384 goto out_wake;
385
386 ret = -ENOMEM;
387 vllist = afs_alloc_vlserver_list(0);
388 if (!vllist)
389 goto out_wake;
390
391 switch (ret) {
392 case -ENODATA:
393 case -EDESTADDRREQ:
394 vllist->status = DNS_LOOKUP_GOT_NOT_FOUND;
395 break;
396 case -EAGAIN:
397 case -ECONNREFUSED:
398 vllist->status = DNS_LOOKUP_GOT_TEMP_FAILURE;
399 break;
400 default:
401 vllist->status = DNS_LOOKUP_GOT_LOCAL_FAILURE;
402 break;
403 }
404 }
405
406 _debug("%s: got list %d %d", cell->name, vllist->source, vllist->status);
407 cell->dns_status = vllist->status;
David Howellsded2f4c2018-10-20 00:57:57 +0100408
409 now = ktime_get_real_seconds();
410 if (min_ttl > max_ttl)
411 max_ttl = min_ttl;
412 if (expiry < now + min_ttl)
413 expiry = now + min_ttl;
414 else if (expiry > now + max_ttl)
415 expiry = now + max_ttl;
416
David Howellsd5c32c82019-05-07 15:06:36 +0100417 _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:
David Howellsded2f4c2018-10-20 00:57:57 +0100421 /* The DNS said that the cell does not exist or there
422 * weren't any addresses to be had.
423 */
David Howellsded2f4c2018-10-20 00:57:57 +0100424 cell->dns_expiry = expiry;
David Howells8b2a4642017-11-02 15:27:50 +0000425 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
David Howellsd5c32c82019-05-07 15:06:36 +0100427 case DNS_LOOKUP_BAD:
428 case DNS_LOOKUP_GOT_LOCAL_FAILURE:
429 case DNS_LOOKUP_GOT_TEMP_FAILURE:
430 case DNS_LOOKUP_GOT_NS_FAILURE:
David Howells8b2a4642017-11-02 15:27:50 +0000431 default:
David Howellsded2f4c2018-10-20 00:57:57 +0100432 cell->dns_expiry = now + 10;
David Howells8b2a4642017-11-02 15:27:50 +0000433 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
David Howells8b2a4642017-11-02 15:27:50 +0000435 } else {
David Howells8b2a4642017-11-02 15:27:50 +0000436 cell->dns_expiry = expiry;
David Howells8b2a4642017-11-02 15:27:50 +0000437 }
438
David Howellsd5c32c82019-05-07 15:06:36 +0100439 /* Replace the VL server list if the new record has servers or the old
440 * record doesn't.
441 */
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;
447 old = p;
448 }
449 write_unlock(&cell->vl_servers_lock);
450 afs_put_vlserverlist(cell->net, old);
wangleibec5eb62010-08-11 09:38:04 +0100451
David Howellsd5c32c82019-05-07 15:06:36 +0100452out_wake:
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);
457 return ret;
David Howellsec268152007-04-26 15:49:28 -0700458}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460/*
David Howells989782d2017-11-02 15:27:50 +0000461 * Destroy a cell record
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 */
David Howells989782d2017-11-02 15:27:50 +0000463static void afs_cell_destroy(struct rcu_head *rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
David Howells989782d2017-11-02 15:27:50 +0000465 struct afs_cell *cell = container_of(rcu, struct afs_cell, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
David Howells989782d2017-11-02 15:27:50 +0000467 _enter("%p{%s}", cell, cell->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
David Howells08e0e7c2007-04-26 15:55:03 -0700469 ASSERTCMP(atomic_read(&cell->usage), ==, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
David Howells0a5143f2018-10-20 00:57:57 +0100471 afs_put_vlserverlist(cell->net, rcu_access_pointer(cell->vl_servers));
David Howells00d3b7a2007-04-26 15:57:07 -0700472 key_put(cell->anonymous_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 kfree(cell);
474
475 _leave(" [destroyed]");
David Howellsec268152007-04-26 15:49:28 -0700476}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478/*
David Howells989782d2017-11-02 15:27:50 +0000479 * Queue the cell manager.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 */
David Howells989782d2017-11-02 15:27:50 +0000481static void afs_queue_cell_manager(struct afs_net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
David Howells989782d2017-11-02 15:27:50 +0000483 int outstanding = atomic_inc_return(&net->cells_outstanding);
484
485 _enter("%d", outstanding);
486
487 if (!queue_work(afs_wq, &net->cells_manager))
488 afs_dec_cells_outstanding(net);
489}
490
491/*
492 * Cell management timer. We have an increment on cells_outstanding that we
493 * need to pass along to the work item.
494 */
495void afs_cells_timer(struct timer_list *timer)
496{
497 struct afs_net *net = container_of(timer, struct afs_net, cells_timer);
498
499 _enter("");
500 if (!queue_work(afs_wq, &net->cells_manager))
501 afs_dec_cells_outstanding(net);
502}
503
504/*
David Howells8b2a4642017-11-02 15:27:50 +0000505 * Get a reference on a cell record.
506 */
507struct afs_cell *afs_get_cell(struct afs_cell *cell)
508{
509 atomic_inc(&cell->usage);
510 return cell;
511}
512
513/*
David Howells989782d2017-11-02 15:27:50 +0000514 * Drop a reference on a cell record.
515 */
516void afs_put_cell(struct afs_net *net, struct afs_cell *cell)
517{
518 time64_t now, expire_delay;
519
520 if (!cell)
521 return;
522
523 _enter("%s", cell->name);
524
525 now = ktime_get_real_seconds();
526 cell->last_inactive = now;
527 expire_delay = 0;
David Howellsd5c32c82019-05-07 15:06:36 +0100528 if (cell->vl_servers->nr_servers)
David Howells989782d2017-11-02 15:27:50 +0000529 expire_delay = afs_cell_gc_delay;
530
531 if (atomic_dec_return(&cell->usage) > 1)
532 return;
533
534 /* 'cell' may now be garbage collected. */
535 afs_set_cell_timer(net, expire_delay);
536}
537
538/*
539 * Allocate a key to use as a placeholder for anonymous user security.
540 */
541static int afs_alloc_anon_key(struct afs_cell *cell)
542{
543 struct key *key;
544 char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp;
545
546 /* Create a key to represent an anonymous user. */
547 memcpy(keyname, "afs@", 4);
548 dp = keyname + 4;
549 cp = cell->name;
550 do {
551 *dp++ = tolower(*cp);
552 } while (*cp++);
553
554 key = rxrpc_get_null_key(keyname);
555 if (IS_ERR(key))
556 return PTR_ERR(key);
557
558 cell->anonymous_key = key;
559
560 _debug("anon key %p{%x}",
561 cell->anonymous_key, key_serial(cell->anonymous_key));
562 return 0;
563}
564
565/*
566 * Activate a cell.
567 */
568static int afs_activate_cell(struct afs_net *net, struct afs_cell *cell)
569{
David Howells6b3944e2018-10-11 22:45:49 +0100570 struct hlist_node **p;
571 struct afs_cell *pcell;
David Howells989782d2017-11-02 15:27:50 +0000572 int ret;
573
574 if (!cell->anonymous_key) {
575 ret = afs_alloc_anon_key(cell);
576 if (ret < 0)
577 return ret;
578 }
579
580#ifdef CONFIG_AFS_FSCACHE
581 cell->cache = fscache_acquire_cookie(afs_cache_netfs.primary_index,
582 &afs_cell_cache_index_def,
David Howells402cb8d2018-04-04 13:41:28 +0100583 cell->name, strlen(cell->name),
584 NULL, 0,
David Howellsee1235a2018-04-04 13:41:28 +0100585 cell, 0, true);
David Howells989782d2017-11-02 15:27:50 +0000586#endif
David Howells5b86d4f2018-05-18 11:46:15 +0100587 ret = afs_proc_cell_setup(cell);
David Howells989782d2017-11-02 15:27:50 +0000588 if (ret < 0)
589 return ret;
David Howells0da0b7f2018-06-15 15:19:22 +0100590
591 mutex_lock(&net->proc_cells_lock);
David Howells6b3944e2018-10-11 22:45:49 +0100592 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)
595 break;
596 }
597
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;
603
David Howells0da0b7f2018-06-15 15:19:22 +0100604 afs_dynroot_mkdir(net, cell);
605 mutex_unlock(&net->proc_cells_lock);
David Howells989782d2017-11-02 15:27:50 +0000606 return 0;
607}
608
609/*
610 * Deactivate a cell.
611 */
612static void afs_deactivate_cell(struct afs_net *net, struct afs_cell *cell)
613{
614 _enter("%s", cell->name);
615
David Howells5b86d4f2018-05-18 11:46:15 +0100616 afs_proc_cell_remove(cell);
David Howells989782d2017-11-02 15:27:50 +0000617
David Howells0da0b7f2018-06-15 15:19:22 +0100618 mutex_lock(&net->proc_cells_lock);
David Howells6b3944e2018-10-11 22:45:49 +0100619 hlist_del_rcu(&cell->proc_link);
David Howells0da0b7f2018-06-15 15:19:22 +0100620 afs_dynroot_rmdir(net, cell);
621 mutex_unlock(&net->proc_cells_lock);
David Howells989782d2017-11-02 15:27:50 +0000622
623#ifdef CONFIG_AFS_FSCACHE
David Howells402cb8d2018-04-04 13:41:28 +0100624 fscache_relinquish_cookie(cell->cache, NULL, false);
David Howells989782d2017-11-02 15:27:50 +0000625 cell->cache = NULL;
626#endif
627
628 _leave("");
629}
630
631/*
632 * Manage a cell record, initialising and destroying it, maintaining its DNS
633 * records.
634 */
635static void afs_manage_cell(struct work_struct *work)
636{
637 struct afs_cell *cell = container_of(work, struct afs_cell, manager);
638 struct afs_net *net = cell->net;
639 bool deleted;
640 int ret, usage;
641
642 _enter("%s", cell->name);
643
644again:
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);
650 usage = 1;
651 deleted = atomic_try_cmpxchg_relaxed(&cell->usage, &usage, 0);
652 if (deleted)
653 rb_erase(&cell->net_node, &net->cells);
654 write_sequnlock(&net->cells_lock);
655 if (deleted)
656 goto final_destruction;
657 if (cell->state == AFS_CELL_FAILED)
658 goto done;
David Howellsd5c32c82019-05-07 15:06:36 +0100659 smp_store_release(&cell->state, AFS_CELL_UNSET);
660 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000661 goto again;
662
663 case AFS_CELL_UNSET:
David Howellsd5c32c82019-05-07 15:06:36 +0100664 smp_store_release(&cell->state, AFS_CELL_ACTIVATING);
665 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000666 goto again;
667
668 case AFS_CELL_ACTIVATING:
669 ret = afs_activate_cell(net, cell);
670 if (ret < 0)
671 goto activation_failed;
672
David Howellsd5c32c82019-05-07 15:06:36 +0100673 smp_store_release(&cell->state, AFS_CELL_ACTIVE);
674 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000675 goto again;
676
677 case AFS_CELL_ACTIVE:
678 if (atomic_read(&cell->usage) > 1) {
David Howellsd5c32c82019-05-07 15:06:36 +0100679 if (test_and_clear_bit(AFS_CELL_FL_DO_LOOKUP, &cell->flags)) {
680 ret = afs_update_cell(cell);
681 if (ret < 0)
682 cell->error = ret;
683 }
David Howells989782d2017-11-02 15:27:50 +0000684 goto done;
685 }
David Howellsd5c32c82019-05-07 15:06:36 +0100686 smp_store_release(&cell->state, AFS_CELL_DEACTIVATING);
687 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000688 goto again;
689
690 case AFS_CELL_DEACTIVATING:
David Howells989782d2017-11-02 15:27:50 +0000691 if (atomic_read(&cell->usage) > 1)
692 goto reverse_deactivation;
693 afs_deactivate_cell(net, cell);
David Howellsd5c32c82019-05-07 15:06:36 +0100694 smp_store_release(&cell->state, AFS_CELL_INACTIVE);
695 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000696 goto again;
697
698 default:
699 break;
700 }
701 _debug("bad state %u", cell->state);
702 BUG(); /* Unhandled state */
703
704activation_failed:
705 cell->error = ret;
706 afs_deactivate_cell(net, cell);
707
David Howellsd5c32c82019-05-07 15:06:36 +0100708 smp_store_release(&cell->state, AFS_CELL_FAILED); /* vs error */
709 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000710 goto again;
711
712reverse_deactivation:
David Howellsd5c32c82019-05-07 15:06:36 +0100713 smp_store_release(&cell->state, AFS_CELL_ACTIVE);
714 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000715 _leave(" [deact->act]");
716 return;
717
718done:
719 _leave(" [done %u]", cell->state);
720 return;
721
722final_destruction:
723 call_rcu(&cell->rcu, afs_cell_destroy);
724 afs_dec_cells_outstanding(net);
725 _leave(" [destruct %d]", atomic_read(&net->cells_outstanding));
726}
727
728/*
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.
732 *
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).
737 *
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.
740 */
741void afs_manage_cells(struct work_struct *work)
742{
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 _enter("");
749
David Howells989782d2017-11-02 15:27:50 +0000750 /* Trawl the cell database looking for cells that have expired from
751 * lack of use and cells whose DNS results have expired and dispatch
752 * their managers.
753 */
754 read_seqlock_excl(&net->cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
David Howells989782d2017-11-02 15:27:50 +0000756 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);
759 unsigned usage;
760 bool sched_cell = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700761
David Howells989782d2017-11-02 15:27:50 +0000762 usage = atomic_read(&cell->usage);
763 _debug("manage %s %u", cell->name, usage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
David Howells989782d2017-11-02 15:27:50 +0000765 ASSERTCMP(usage, >=, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
David Howells989782d2017-11-02 15:27:50 +0000767 if (purging) {
768 if (test_and_clear_bit(AFS_CELL_FL_NO_GC, &cell->flags))
769 usage = atomic_dec_return(&cell->usage);
770 ASSERTCMP(usage, ==, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
772
David Howells989782d2017-11-02 15:27:50 +0000773 if (usage == 1) {
David Howellsd5c32c82019-05-07 15:06:36 +0100774 struct afs_vlserver_list *vllist;
David Howells989782d2017-11-02 15:27:50 +0000775 time64_t expire_at = cell->last_inactive;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
David Howellsd5c32c82019-05-07 15:06:36 +0100777 read_lock(&cell->vl_servers_lock);
778 vllist = rcu_dereference_protected(
779 cell->vl_servers,
780 lockdep_is_held(&cell->vl_servers_lock));
781 if (vllist->nr_servers > 0)
David Howells989782d2017-11-02 15:27:50 +0000782 expire_at += afs_cell_gc_delay;
David Howellsd5c32c82019-05-07 15:06:36 +0100783 read_unlock(&cell->vl_servers_lock);
David Howells989782d2017-11-02 15:27:50 +0000784 if (purging || expire_at <= now)
785 sched_cell = true;
786 else if (expire_at < next_manage)
787 next_manage = expire_at;
788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
David Howells989782d2017-11-02 15:27:50 +0000790 if (!purging) {
David Howellsd5c32c82019-05-07 15:06:36 +0100791 if (test_bit(AFS_CELL_FL_DO_LOOKUP, &cell->flags))
David Howells989782d2017-11-02 15:27:50 +0000792 sched_cell = true;
David Howells989782d2017-11-02 15:27:50 +0000793 }
794
795 if (sched_cell)
796 queue_work(afs_wq, &cell->manager);
797 }
798
799 read_sequnlock_excl(&net->cells_lock);
800
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.
804 */
805 if (!purging && next_manage < TIME64_MAX) {
806 now = ktime_get_real_seconds();
807
808 if (next_manage - now <= 0) {
809 if (queue_work(afs_wq, &net->cells_manager))
810 atomic_inc(&net->cells_outstanding);
811 } else {
812 afs_set_cell_timer(net, next_manage - now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
814 }
815
David Howells989782d2017-11-02 15:27:50 +0000816 afs_dec_cells_outstanding(net);
817 _leave(" [%d]", atomic_read(&net->cells_outstanding));
818}
819
820/*
821 * Purge in-memory cell database.
822 */
823void afs_cell_purge(struct afs_net *net)
824{
825 struct afs_cell *ws;
826
827 _enter("");
828
829 write_seqlock(&net->cells_lock);
David Howells1588def2018-05-23 11:51:29 +0100830 ws = rcu_access_pointer(net->ws_cell);
831 RCU_INIT_POINTER(net->ws_cell, NULL);
David Howells989782d2017-11-02 15:27:50 +0000832 write_sequnlock(&net->cells_lock);
833 afs_put_cell(net, ws);
834
835 _debug("del timer");
836 if (del_timer_sync(&net->cells_timer))
837 atomic_dec(&net->cells_outstanding);
838
839 _debug("kick mgr");
840 afs_queue_cell_manager(net);
841
842 _debug("wait");
Peter Zijlstraab1fbe32018-03-15 11:42:28 +0100843 wait_var_event(&net->cells_outstanding,
844 !atomic_read(&net->cells_outstanding));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700846}