blob: 6127f0fcd62c4e376bd2554c1003aedb40aab471 [file] [log] [blame]
David Howellsec268152007-04-26 15:49:28 -07001/* AFS cell and server record management
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
David Howells989782d2017-11-02 15:27:50 +00003 * Copyright (C) 2002, 2017 Red Hat, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Written by David Howells (dhowells@redhat.com)
5 *
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.
10 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/slab.h>
David Howells00d3b7a2007-04-26 15:57:07 -070013#include <linux/key.h>
14#include <linux/ctype.h>
Wang Lei07567a52010-08-04 15:16:38 +010015#include <linux/dns_resolver.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040016#include <linux/sched.h>
David Howells3838d3e2017-11-02 15:27:47 +000017#include <linux/inet.h>
David Howells0da0b7f2018-06-15 15:19:22 +010018#include <linux/namei.h>
David Howells00d3b7a2007-04-26 15:57:07 -070019#include <keys/rxrpc-type.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "internal.h"
21
David Howellsfe342cf2018-04-09 21:12:31 +010022static unsigned __read_mostly afs_cell_gc_delay = 10;
David Howells989782d2017-11-02 15:27:50 +000023
24static void afs_manage_cell(struct work_struct *);
25
26static void afs_dec_cells_outstanding(struct afs_net *net)
27{
28 if (atomic_dec_and_test(&net->cells_outstanding))
Peter Zijlstraab1fbe32018-03-15 11:42:28 +010029 wake_up_var(&net->cells_outstanding);
David Howells989782d2017-11-02 15:27:50 +000030}
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032/*
David Howells989782d2017-11-02 15:27:50 +000033 * Set the cell timer to fire after a given delay, assuming it's not already
34 * set for an earlier time.
35 */
36static void afs_set_cell_timer(struct afs_net *net, time64_t delay)
37{
38 if (net->live) {
39 atomic_inc(&net->cells_outstanding);
40 if (timer_reduce(&net->cells_timer, jiffies + delay * HZ))
41 afs_dec_cells_outstanding(net);
42 }
43}
44
45/*
46 * Look up and get an activation reference on a cell record under RCU
47 * conditions. The caller must hold the RCU read lock.
48 */
49struct afs_cell *afs_lookup_cell_rcu(struct afs_net *net,
50 const char *name, unsigned int namesz)
51{
52 struct afs_cell *cell = NULL;
53 struct rb_node *p;
54 int n, seq = 0, ret = 0;
55
56 _enter("%*.*s", namesz, namesz, name);
57
58 if (name && namesz == 0)
59 return ERR_PTR(-EINVAL);
60 if (namesz > AFS_MAXCELLNAME)
61 return ERR_PTR(-ENAMETOOLONG);
62
63 do {
64 /* Unfortunately, rbtree walking doesn't give reliable results
65 * under just the RCU read lock, so we have to check for
66 * changes.
67 */
68 if (cell)
69 afs_put_cell(net, cell);
70 cell = NULL;
71 ret = -ENOENT;
72
73 read_seqbegin_or_lock(&net->cells_lock, &seq);
74
75 if (!name) {
76 cell = rcu_dereference_raw(net->ws_cell);
77 if (cell) {
78 afs_get_cell(cell);
David Howellsfe342cf2018-04-09 21:12:31 +010079 break;
David Howells989782d2017-11-02 15:27:50 +000080 }
81 ret = -EDESTADDRREQ;
82 continue;
83 }
84
85 p = rcu_dereference_raw(net->cells.rb_node);
86 while (p) {
87 cell = rb_entry(p, struct afs_cell, net_node);
88
89 n = strncasecmp(cell->name, name,
90 min_t(size_t, cell->name_len, namesz));
91 if (n == 0)
92 n = cell->name_len - namesz;
93 if (n < 0) {
94 p = rcu_dereference_raw(p->rb_left);
95 } else if (n > 0) {
96 p = rcu_dereference_raw(p->rb_right);
97 } else {
98 if (atomic_inc_not_zero(&cell->usage)) {
99 ret = 0;
100 break;
101 }
102 /* We want to repeat the search, this time with
103 * the lock properly locked.
104 */
105 }
106 cell = NULL;
107 }
108
109 } while (need_seqretry(&net->cells_lock, seq));
110
111 done_seqretry(&net->cells_lock, seq);
112
113 return ret == 0 ? cell : ERR_PTR(ret);
114}
115
116/*
117 * Set up a cell record and fill in its name, VL server address list and
David Howells00d3b7a2007-04-26 15:57:07 -0700118 * allocate an anonymous key
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 */
David Howells989782d2017-11-02 15:27:50 +0000120static struct afs_cell *afs_alloc_cell(struct afs_net *net,
121 const char *name, unsigned int namelen,
122 const char *vllist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 struct afs_cell *cell;
David Howells989782d2017-11-02 15:27:50 +0000125 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
David Howells989782d2017-11-02 15:27:50 +0000127 ASSERT(name);
128 if (namelen == 0)
129 return ERR_PTR(-EINVAL);
Wang Lei07567a52010-08-04 15:16:38 +0100130 if (namelen > AFS_MAXCELLNAME) {
131 _leave(" = -ENAMETOOLONG");
David Howells00d3b7a2007-04-26 15:57:07 -0700132 return ERR_PTR(-ENAMETOOLONG);
Wang Lei07567a52010-08-04 15:16:38 +0100133 }
David Howells37ab6362018-04-06 14:17:23 +0100134 if (namelen == 5 && memcmp(name, "@cell", 5) == 0)
135 return ERR_PTR(-EINVAL);
David Howells00d3b7a2007-04-26 15:57:07 -0700136
David Howells989782d2017-11-02 15:27:50 +0000137 _enter("%*.*s,%s", namelen, namelen, name, vllist);
138
139 cell = kzalloc(sizeof(struct afs_cell), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (!cell) {
141 _leave(" = -ENOMEM");
David Howells08e0e7c2007-04-26 15:55:03 -0700142 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144
David Howellsf044c882017-11-02 15:27:45 +0000145 cell->net = net;
David Howells989782d2017-11-02 15:27:50 +0000146 cell->name_len = namelen;
147 for (i = 0; i < namelen; i++)
148 cell->name[i] = tolower(name[i]);
149
150 atomic_set(&cell->usage, 2);
151 INIT_WORK(&cell->manager, afs_manage_cell);
David Howells8b2a4642017-11-02 15:27:50 +0000152 cell->flags = ((1 << AFS_CELL_FL_NOT_READY) |
153 (1 << AFS_CELL_FL_NO_LOOKUP_YET));
David Howellsd2ddc772017-11-02 15:27:50 +0000154 INIT_LIST_HEAD(&cell->proc_volumes);
155 rwlock_init(&cell->proc_lock);
David Howells8b2a4642017-11-02 15:27:50 +0000156 rwlock_init(&cell->vl_addrs_lock);
David Howells4d9df982017-11-02 15:27:47 +0000157
David Howells989782d2017-11-02 15:27:50 +0000158 /* Fill in the VL server list if we were given a list of addresses to
159 * use.
160 */
161 if (vllist) {
David Howells8b2a4642017-11-02 15:27:50 +0000162 struct afs_addr_list *alist;
Wang Lei07567a52010-08-04 15:16:38 +0100163
David Howells8b2a4642017-11-02 15:27:50 +0000164 alist = afs_parse_text_addrs(vllist, strlen(vllist), ':',
165 VL_SERVICE, AFS_VL_PORT);
166 if (IS_ERR(alist)) {
167 ret = PTR_ERR(alist);
168 goto parse_failed;
169 }
David Howells989782d2017-11-02 15:27:50 +0000170
David Howells8b2a4642017-11-02 15:27:50 +0000171 rcu_assign_pointer(cell->vl_addrs, alist);
David Howells989782d2017-11-02 15:27:50 +0000172 cell->dns_expiry = TIME64_MAX;
Wang Lei07567a52010-08-04 15:16:38 +0100173 }
174
David Howells00d3b7a2007-04-26 15:57:07 -0700175 _leave(" = %p", cell);
176 return cell;
177
David Howells8b2a4642017-11-02 15:27:50 +0000178parse_failed:
179 if (ret == -EINVAL)
180 printk(KERN_ERR "kAFS: bad VL server IP address\n");
David Howells00d3b7a2007-04-26 15:57:07 -0700181 kfree(cell);
182 _leave(" = %d", ret);
183 return ERR_PTR(ret);
184}
185
186/*
David Howells989782d2017-11-02 15:27:50 +0000187 * afs_lookup_cell - Look up or create a cell record.
David Howellsf044c882017-11-02 15:27:45 +0000188 * @net: The network namespace
David Howells989782d2017-11-02 15:27:50 +0000189 * @name: The name of the cell.
190 * @namesz: The strlen of the cell name.
191 * @vllist: A colon/comma separated list of numeric IP addresses or NULL.
192 * @excl: T if an error should be given if the cell name already exists.
193 *
194 * Look up a cell record by name and query the DNS for VL server addresses if
195 * needed. Note that that actual DNS query is punted off to the manager thread
196 * so that this function can return immediately if interrupted whilst allowing
197 * cell records to be shared even if not yet fully constructed.
David Howells00d3b7a2007-04-26 15:57:07 -0700198 */
David Howells989782d2017-11-02 15:27:50 +0000199struct afs_cell *afs_lookup_cell(struct afs_net *net,
200 const char *name, unsigned int namesz,
201 const char *vllist, bool excl)
David Howells00d3b7a2007-04-26 15:57:07 -0700202{
David Howells989782d2017-11-02 15:27:50 +0000203 struct afs_cell *cell, *candidate, *cursor;
204 struct rb_node *parent, **pp;
205 int ret, n;
David Howells00d3b7a2007-04-26 15:57:07 -0700206
David Howells989782d2017-11-02 15:27:50 +0000207 _enter("%s,%s", name, vllist);
David Howells00d3b7a2007-04-26 15:57:07 -0700208
David Howells989782d2017-11-02 15:27:50 +0000209 if (!excl) {
210 rcu_read_lock();
211 cell = afs_lookup_cell_rcu(net, name, namesz);
212 rcu_read_unlock();
Gustavo A. R. Silva68327952017-11-17 16:40:32 -0600213 if (!IS_ERR(cell))
David Howells989782d2017-11-02 15:27:50 +0000214 goto wait_for_cell;
David Howells00d3b7a2007-04-26 15:57:07 -0700215 }
216
David Howells989782d2017-11-02 15:27:50 +0000217 /* Assume we're probably going to create a cell and preallocate and
218 * mostly set up a candidate record. We can then use this to stash the
219 * name, the net namespace and VL server addresses.
220 *
221 * We also want to do this before we hold any locks as it may involve
222 * upcalling to userspace to make DNS queries.
223 */
224 candidate = afs_alloc_cell(net, name, namesz, vllist);
225 if (IS_ERR(candidate)) {
226 _leave(" = %ld", PTR_ERR(candidate));
227 return candidate;
228 }
229
230 /* Find the insertion point and check to see if someone else added a
231 * cell whilst we were allocating.
232 */
233 write_seqlock(&net->cells_lock);
234
235 pp = &net->cells.rb_node;
236 parent = NULL;
237 while (*pp) {
238 parent = *pp;
239 cursor = rb_entry(parent, struct afs_cell, net_node);
240
241 n = strncasecmp(cursor->name, name,
242 min_t(size_t, cursor->name_len, namesz));
243 if (n == 0)
244 n = cursor->name_len - namesz;
245 if (n < 0)
246 pp = &(*pp)->rb_left;
247 else if (n > 0)
248 pp = &(*pp)->rb_right;
249 else
250 goto cell_already_exists;
251 }
252
253 cell = candidate;
254 candidate = NULL;
255 rb_link_node_rcu(&cell->net_node, parent, pp);
256 rb_insert_color(&cell->net_node, &net->cells);
257 atomic_inc(&net->cells_outstanding);
258 write_sequnlock(&net->cells_lock);
259
260 queue_work(afs_wq, &cell->manager);
261
262wait_for_cell:
263 _debug("wait_for_cell");
264 ret = wait_on_bit(&cell->flags, AFS_CELL_FL_NOT_READY, TASK_INTERRUPTIBLE);
265 smp_rmb();
266
267 switch (READ_ONCE(cell->state)) {
268 case AFS_CELL_FAILED:
269 ret = cell->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 goto error;
David Howells989782d2017-11-02 15:27:50 +0000271 default:
272 _debug("weird %u %d", cell->state, cell->error);
273 goto error;
274 case AFS_CELL_ACTIVE:
275 break;
276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
David Howells989782d2017-11-02 15:27:50 +0000278 _leave(" = %p [cell]", cell);
David Howells08e0e7c2007-04-26 15:55:03 -0700279 return cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
David Howells989782d2017-11-02 15:27:50 +0000281cell_already_exists:
282 _debug("cell exists");
283 cell = cursor;
284 if (excl) {
285 ret = -EEXIST;
286 } else {
David Howells989782d2017-11-02 15:27:50 +0000287 afs_get_cell(cursor);
288 ret = 0;
wangleibec5eb62010-08-11 09:38:04 +0100289 }
David Howells989782d2017-11-02 15:27:50 +0000290 write_sequnlock(&net->cells_lock);
291 kfree(candidate);
292 if (ret == 0)
293 goto wait_for_cell;
David Howells8b2a4642017-11-02 15:27:50 +0000294 goto error_noput;
David Howells989782d2017-11-02 15:27:50 +0000295error:
296 afs_put_cell(net, cell);
David Howells8b2a4642017-11-02 15:27:50 +0000297error_noput:
David Howells989782d2017-11-02 15:27:50 +0000298 _leave(" = %d [error]", ret);
299 return ERR_PTR(ret);
David Howellsec268152007-04-26 15:49:28 -0700300}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302/*
David Howells08e0e7c2007-04-26 15:55:03 -0700303 * set the root cell information
304 * - can be called with a module parameter string
305 * - can be called from a write to /proc/fs/afs/rootcell
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 */
David Howells989782d2017-11-02 15:27:50 +0000307int afs_cell_init(struct afs_net *net, const char *rootcell)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 struct afs_cell *old_root, *new_root;
David Howells989782d2017-11-02 15:27:50 +0000310 const char *cp, *vllist;
311 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 _enter("");
314
315 if (!rootcell) {
316 /* module is loaded with no parameters, or built statically.
317 * - in the future we might initialize cell DB here.
318 */
David Howells08e0e7c2007-04-26 15:55:03 -0700319 _leave(" = 0 [no root]");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return 0;
321 }
322
323 cp = strchr(rootcell, ':');
David Howells989782d2017-11-02 15:27:50 +0000324 if (!cp) {
Wang Lei07567a52010-08-04 15:16:38 +0100325 _debug("kAFS: no VL server IP addresses specified");
David Howells989782d2017-11-02 15:27:50 +0000326 vllist = NULL;
327 len = strlen(rootcell);
328 } else {
329 vllist = cp + 1;
330 len = cp - rootcell;
331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 /* allocate a cell record for the root cell */
David Howells989782d2017-11-02 15:27:50 +0000334 new_root = afs_lookup_cell(net, rootcell, len, vllist, false);
David Howells08e0e7c2007-04-26 15:55:03 -0700335 if (IS_ERR(new_root)) {
336 _leave(" = %ld", PTR_ERR(new_root));
337 return PTR_ERR(new_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339
David Howells17814ae2018-04-09 21:12:31 +0100340 if (!test_and_set_bit(AFS_CELL_FL_NO_GC, &new_root->flags))
341 afs_get_cell(new_root);
David Howells989782d2017-11-02 15:27:50 +0000342
David Howells08e0e7c2007-04-26 15:55:03 -0700343 /* install the new cell */
David Howells989782d2017-11-02 15:27:50 +0000344 write_seqlock(&net->cells_lock);
David Howells1588def2018-05-23 11:51:29 +0100345 old_root = rcu_access_pointer(net->ws_cell);
346 rcu_assign_pointer(net->ws_cell, new_root);
David Howells989782d2017-11-02 15:27:50 +0000347 write_sequnlock(&net->cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
David Howells989782d2017-11-02 15:27:50 +0000349 afs_put_cell(net, old_root);
David Howells08e0e7c2007-04-26 15:55:03 -0700350 _leave(" = 0");
351 return 0;
David Howellsec268152007-04-26 15:49:28 -0700352}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354/*
David Howells989782d2017-11-02 15:27:50 +0000355 * Update a cell's VL server address list from the DNS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 */
David Howells989782d2017-11-02 15:27:50 +0000357static void afs_update_cell(struct afs_cell *cell)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
David Howells8b2a4642017-11-02 15:27:50 +0000359 struct afs_addr_list *alist, *old;
David Howells989782d2017-11-02 15:27:50 +0000360 time64_t now, expiry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
David Howells989782d2017-11-02 15:27:50 +0000362 _enter("%s", cell->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
David Howells8b2a4642017-11-02 15:27:50 +0000364 alist = afs_dns_query(cell, &expiry);
365 if (IS_ERR(alist)) {
366 switch (PTR_ERR(alist)) {
367 case -ENODATA:
368 /* The DNS said that the cell does not exist */
369 set_bit(AFS_CELL_FL_NOT_FOUND, &cell->flags);
370 clear_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags);
371 cell->dns_expiry = ktime_get_real_seconds() + 61;
372 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
David Howells8b2a4642017-11-02 15:27:50 +0000374 case -EAGAIN:
375 case -ECONNREFUSED:
376 default:
377 set_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags);
378 cell->dns_expiry = ktime_get_real_seconds() + 10;
379 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381
David Howells8b2a4642017-11-02 15:27:50 +0000382 cell->error = -EDESTADDRREQ;
383 } else {
384 clear_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags);
385 clear_bit(AFS_CELL_FL_NOT_FOUND, &cell->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
David Howells8b2a4642017-11-02 15:27:50 +0000387 /* Exclusion on changing vl_addrs is achieved by a
388 * non-reentrant work item.
389 */
390 old = rcu_dereference_protected(cell->vl_addrs, true);
391 rcu_assign_pointer(cell->vl_addrs, alist);
392 cell->dns_expiry = expiry;
wangleibec5eb62010-08-11 09:38:04 +0100393
David Howells8b2a4642017-11-02 15:27:50 +0000394 if (old)
395 afs_put_addrlist(old);
396 }
397
398 if (test_and_clear_bit(AFS_CELL_FL_NO_LOOKUP_YET, &cell->flags))
399 wake_up_bit(&cell->flags, AFS_CELL_FL_NO_LOOKUP_YET);
wangleibec5eb62010-08-11 09:38:04 +0100400
David Howells989782d2017-11-02 15:27:50 +0000401 now = ktime_get_real_seconds();
David Howells8b2a4642017-11-02 15:27:50 +0000402 afs_set_cell_timer(cell->net, cell->dns_expiry - now);
David Howells989782d2017-11-02 15:27:50 +0000403 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700404}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406/*
David Howells989782d2017-11-02 15:27:50 +0000407 * Destroy a cell record
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 */
David Howells989782d2017-11-02 15:27:50 +0000409static void afs_cell_destroy(struct rcu_head *rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
David Howells989782d2017-11-02 15:27:50 +0000411 struct afs_cell *cell = container_of(rcu, struct afs_cell, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
David Howells989782d2017-11-02 15:27:50 +0000413 _enter("%p{%s}", cell, cell->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
David Howells08e0e7c2007-04-26 15:55:03 -0700415 ASSERTCMP(atomic_read(&cell->usage), ==, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
David Howellsfe342cf2018-04-09 21:12:31 +0100417 afs_put_addrlist(rcu_access_pointer(cell->vl_addrs));
David Howells00d3b7a2007-04-26 15:57:07 -0700418 key_put(cell->anonymous_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 kfree(cell);
420
421 _leave(" [destroyed]");
David Howellsec268152007-04-26 15:49:28 -0700422}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424/*
David Howells989782d2017-11-02 15:27:50 +0000425 * Queue the cell manager.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 */
David Howells989782d2017-11-02 15:27:50 +0000427static void afs_queue_cell_manager(struct afs_net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
David Howells989782d2017-11-02 15:27:50 +0000429 int outstanding = atomic_inc_return(&net->cells_outstanding);
430
431 _enter("%d", outstanding);
432
433 if (!queue_work(afs_wq, &net->cells_manager))
434 afs_dec_cells_outstanding(net);
435}
436
437/*
438 * Cell management timer. We have an increment on cells_outstanding that we
439 * need to pass along to the work item.
440 */
441void afs_cells_timer(struct timer_list *timer)
442{
443 struct afs_net *net = container_of(timer, struct afs_net, cells_timer);
444
445 _enter("");
446 if (!queue_work(afs_wq, &net->cells_manager))
447 afs_dec_cells_outstanding(net);
448}
449
450/*
David Howells8b2a4642017-11-02 15:27:50 +0000451 * Get a reference on a cell record.
452 */
453struct afs_cell *afs_get_cell(struct afs_cell *cell)
454{
455 atomic_inc(&cell->usage);
456 return cell;
457}
458
459/*
David Howells989782d2017-11-02 15:27:50 +0000460 * Drop a reference on a cell record.
461 */
462void afs_put_cell(struct afs_net *net, struct afs_cell *cell)
463{
464 time64_t now, expire_delay;
465
466 if (!cell)
467 return;
468
469 _enter("%s", cell->name);
470
471 now = ktime_get_real_seconds();
472 cell->last_inactive = now;
473 expire_delay = 0;
474 if (!test_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags) &&
475 !test_bit(AFS_CELL_FL_NOT_FOUND, &cell->flags))
476 expire_delay = afs_cell_gc_delay;
477
478 if (atomic_dec_return(&cell->usage) > 1)
479 return;
480
481 /* 'cell' may now be garbage collected. */
482 afs_set_cell_timer(net, expire_delay);
483}
484
485/*
486 * Allocate a key to use as a placeholder for anonymous user security.
487 */
488static int afs_alloc_anon_key(struct afs_cell *cell)
489{
490 struct key *key;
491 char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp;
492
493 /* Create a key to represent an anonymous user. */
494 memcpy(keyname, "afs@", 4);
495 dp = keyname + 4;
496 cp = cell->name;
497 do {
498 *dp++ = tolower(*cp);
499 } while (*cp++);
500
501 key = rxrpc_get_null_key(keyname);
502 if (IS_ERR(key))
503 return PTR_ERR(key);
504
505 cell->anonymous_key = key;
506
507 _debug("anon key %p{%x}",
508 cell->anonymous_key, key_serial(cell->anonymous_key));
509 return 0;
510}
511
512/*
513 * Activate a cell.
514 */
515static int afs_activate_cell(struct afs_net *net, struct afs_cell *cell)
516{
David Howells6b3944e2018-10-11 22:45:49 +0100517 struct hlist_node **p;
518 struct afs_cell *pcell;
David Howells989782d2017-11-02 15:27:50 +0000519 int ret;
520
521 if (!cell->anonymous_key) {
522 ret = afs_alloc_anon_key(cell);
523 if (ret < 0)
524 return ret;
525 }
526
527#ifdef CONFIG_AFS_FSCACHE
528 cell->cache = fscache_acquire_cookie(afs_cache_netfs.primary_index,
529 &afs_cell_cache_index_def,
David Howells402cb8d2018-04-04 13:41:28 +0100530 cell->name, strlen(cell->name),
531 NULL, 0,
David Howellsee1235a2018-04-04 13:41:28 +0100532 cell, 0, true);
David Howells989782d2017-11-02 15:27:50 +0000533#endif
David Howells5b86d4f2018-05-18 11:46:15 +0100534 ret = afs_proc_cell_setup(cell);
David Howells989782d2017-11-02 15:27:50 +0000535 if (ret < 0)
536 return ret;
David Howells0da0b7f2018-06-15 15:19:22 +0100537
538 mutex_lock(&net->proc_cells_lock);
David Howells6b3944e2018-10-11 22:45:49 +0100539 for (p = &net->proc_cells.first; *p; p = &(*p)->next) {
540 pcell = hlist_entry(*p, struct afs_cell, proc_link);
541 if (strcmp(cell->name, pcell->name) < 0)
542 break;
543 }
544
545 cell->proc_link.pprev = p;
546 cell->proc_link.next = *p;
547 rcu_assign_pointer(*p, &cell->proc_link.next);
548 if (cell->proc_link.next)
549 cell->proc_link.next->pprev = &cell->proc_link.next;
550
David Howells0da0b7f2018-06-15 15:19:22 +0100551 afs_dynroot_mkdir(net, cell);
552 mutex_unlock(&net->proc_cells_lock);
David Howells989782d2017-11-02 15:27:50 +0000553 return 0;
554}
555
556/*
557 * Deactivate a cell.
558 */
559static void afs_deactivate_cell(struct afs_net *net, struct afs_cell *cell)
560{
561 _enter("%s", cell->name);
562
David Howells5b86d4f2018-05-18 11:46:15 +0100563 afs_proc_cell_remove(cell);
David Howells989782d2017-11-02 15:27:50 +0000564
David Howells0da0b7f2018-06-15 15:19:22 +0100565 mutex_lock(&net->proc_cells_lock);
David Howells6b3944e2018-10-11 22:45:49 +0100566 hlist_del_rcu(&cell->proc_link);
David Howells0da0b7f2018-06-15 15:19:22 +0100567 afs_dynroot_rmdir(net, cell);
568 mutex_unlock(&net->proc_cells_lock);
David Howells989782d2017-11-02 15:27:50 +0000569
570#ifdef CONFIG_AFS_FSCACHE
David Howells402cb8d2018-04-04 13:41:28 +0100571 fscache_relinquish_cookie(cell->cache, NULL, false);
David Howells989782d2017-11-02 15:27:50 +0000572 cell->cache = NULL;
573#endif
574
575 _leave("");
576}
577
578/*
579 * Manage a cell record, initialising and destroying it, maintaining its DNS
580 * records.
581 */
582static void afs_manage_cell(struct work_struct *work)
583{
584 struct afs_cell *cell = container_of(work, struct afs_cell, manager);
585 struct afs_net *net = cell->net;
586 bool deleted;
587 int ret, usage;
588
589 _enter("%s", cell->name);
590
591again:
592 _debug("state %u", cell->state);
593 switch (cell->state) {
594 case AFS_CELL_INACTIVE:
595 case AFS_CELL_FAILED:
596 write_seqlock(&net->cells_lock);
597 usage = 1;
598 deleted = atomic_try_cmpxchg_relaxed(&cell->usage, &usage, 0);
599 if (deleted)
600 rb_erase(&cell->net_node, &net->cells);
601 write_sequnlock(&net->cells_lock);
602 if (deleted)
603 goto final_destruction;
604 if (cell->state == AFS_CELL_FAILED)
605 goto done;
606 cell->state = AFS_CELL_UNSET;
607 goto again;
608
609 case AFS_CELL_UNSET:
610 cell->state = AFS_CELL_ACTIVATING;
611 goto again;
612
613 case AFS_CELL_ACTIVATING:
614 ret = afs_activate_cell(net, cell);
615 if (ret < 0)
616 goto activation_failed;
617
618 cell->state = AFS_CELL_ACTIVE;
619 smp_wmb();
620 clear_bit(AFS_CELL_FL_NOT_READY, &cell->flags);
621 wake_up_bit(&cell->flags, AFS_CELL_FL_NOT_READY);
622 goto again;
623
624 case AFS_CELL_ACTIVE:
625 if (atomic_read(&cell->usage) > 1) {
626 time64_t now = ktime_get_real_seconds();
627 if (cell->dns_expiry <= now && net->live)
628 afs_update_cell(cell);
629 goto done;
630 }
631 cell->state = AFS_CELL_DEACTIVATING;
632 goto again;
633
634 case AFS_CELL_DEACTIVATING:
635 set_bit(AFS_CELL_FL_NOT_READY, &cell->flags);
636 if (atomic_read(&cell->usage) > 1)
637 goto reverse_deactivation;
638 afs_deactivate_cell(net, cell);
639 cell->state = AFS_CELL_INACTIVE;
640 goto again;
641
642 default:
643 break;
644 }
645 _debug("bad state %u", cell->state);
646 BUG(); /* Unhandled state */
647
648activation_failed:
649 cell->error = ret;
650 afs_deactivate_cell(net, cell);
651
652 cell->state = AFS_CELL_FAILED;
653 smp_wmb();
654 if (test_and_clear_bit(AFS_CELL_FL_NOT_READY, &cell->flags))
655 wake_up_bit(&cell->flags, AFS_CELL_FL_NOT_READY);
656 goto again;
657
658reverse_deactivation:
659 cell->state = AFS_CELL_ACTIVE;
660 smp_wmb();
661 clear_bit(AFS_CELL_FL_NOT_READY, &cell->flags);
662 wake_up_bit(&cell->flags, AFS_CELL_FL_NOT_READY);
663 _leave(" [deact->act]");
664 return;
665
666done:
667 _leave(" [done %u]", cell->state);
668 return;
669
670final_destruction:
671 call_rcu(&cell->rcu, afs_cell_destroy);
672 afs_dec_cells_outstanding(net);
673 _leave(" [destruct %d]", atomic_read(&net->cells_outstanding));
674}
675
676/*
677 * Manage the records of cells known to a network namespace. This includes
678 * updating the DNS records and garbage collecting unused cells that were
679 * automatically added.
680 *
681 * Note that constructed cell records may only be removed from net->cells by
682 * this work item, so it is safe for this work item to stash a cursor pointing
683 * into the tree and then return to caller (provided it skips cells that are
684 * still under construction).
685 *
686 * Note also that we were given an increment on net->cells_outstanding by
687 * whoever queued us that we need to deal with before returning.
688 */
689void afs_manage_cells(struct work_struct *work)
690{
691 struct afs_net *net = container_of(work, struct afs_net, cells_manager);
692 struct rb_node *cursor;
693 time64_t now = ktime_get_real_seconds(), next_manage = TIME64_MAX;
694 bool purging = !net->live;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 _enter("");
697
David Howells989782d2017-11-02 15:27:50 +0000698 /* Trawl the cell database looking for cells that have expired from
699 * lack of use and cells whose DNS results have expired and dispatch
700 * their managers.
701 */
702 read_seqlock_excl(&net->cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
David Howells989782d2017-11-02 15:27:50 +0000704 for (cursor = rb_first(&net->cells); cursor; cursor = rb_next(cursor)) {
705 struct afs_cell *cell =
706 rb_entry(cursor, struct afs_cell, net_node);
707 unsigned usage;
708 bool sched_cell = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700709
David Howells989782d2017-11-02 15:27:50 +0000710 usage = atomic_read(&cell->usage);
711 _debug("manage %s %u", cell->name, usage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
David Howells989782d2017-11-02 15:27:50 +0000713 ASSERTCMP(usage, >=, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
David Howells989782d2017-11-02 15:27:50 +0000715 if (purging) {
716 if (test_and_clear_bit(AFS_CELL_FL_NO_GC, &cell->flags))
717 usage = atomic_dec_return(&cell->usage);
718 ASSERTCMP(usage, ==, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720
David Howells989782d2017-11-02 15:27:50 +0000721 if (usage == 1) {
722 time64_t expire_at = cell->last_inactive;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
David Howells989782d2017-11-02 15:27:50 +0000724 if (!test_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags) &&
725 !test_bit(AFS_CELL_FL_NOT_FOUND, &cell->flags))
726 expire_at += afs_cell_gc_delay;
727 if (purging || expire_at <= now)
728 sched_cell = true;
729 else if (expire_at < next_manage)
730 next_manage = expire_at;
731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
David Howells989782d2017-11-02 15:27:50 +0000733 if (!purging) {
734 if (cell->dns_expiry <= now)
735 sched_cell = true;
736 else if (cell->dns_expiry <= next_manage)
737 next_manage = cell->dns_expiry;
738 }
739
740 if (sched_cell)
741 queue_work(afs_wq, &cell->manager);
742 }
743
744 read_sequnlock_excl(&net->cells_lock);
745
746 /* Update the timer on the way out. We have to pass an increment on
747 * cells_outstanding in the namespace that we are in to the timer or
748 * the work scheduler.
749 */
750 if (!purging && next_manage < TIME64_MAX) {
751 now = ktime_get_real_seconds();
752
753 if (next_manage - now <= 0) {
754 if (queue_work(afs_wq, &net->cells_manager))
755 atomic_inc(&net->cells_outstanding);
756 } else {
757 afs_set_cell_timer(net, next_manage - now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
759 }
760
David Howells989782d2017-11-02 15:27:50 +0000761 afs_dec_cells_outstanding(net);
762 _leave(" [%d]", atomic_read(&net->cells_outstanding));
763}
764
765/*
766 * Purge in-memory cell database.
767 */
768void afs_cell_purge(struct afs_net *net)
769{
770 struct afs_cell *ws;
771
772 _enter("");
773
774 write_seqlock(&net->cells_lock);
David Howells1588def2018-05-23 11:51:29 +0100775 ws = rcu_access_pointer(net->ws_cell);
776 RCU_INIT_POINTER(net->ws_cell, NULL);
David Howells989782d2017-11-02 15:27:50 +0000777 write_sequnlock(&net->cells_lock);
778 afs_put_cell(net, ws);
779
780 _debug("del timer");
781 if (del_timer_sync(&net->cells_timer))
782 atomic_dec(&net->cells_outstanding);
783
784 _debug("kick mgr");
785 afs_queue_cell_manager(net);
786
787 _debug("wait");
Peter Zijlstraab1fbe32018-03-15 11:42:28 +0100788 wait_var_event(&net->cells_outstanding,
789 !atomic_read(&net->cells_outstanding));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700791}