blob: 47f96be05163cfba99eb335509b86f6fd3771805 [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 Howellsded2f4c2018-10-20 00:57:57 +010023static unsigned __read_mostly afs_cell_min_ttl = 10 * 60;
24static unsigned __read_mostly afs_cell_max_ttl = 24 * 60 * 60;
David Howells989782d2017-11-02 15:27:50 +000025
26static void afs_manage_cell(struct work_struct *);
27
28static void afs_dec_cells_outstanding(struct afs_net *net)
29{
30 if (atomic_dec_and_test(&net->cells_outstanding))
Peter Zijlstraab1fbe32018-03-15 11:42:28 +010031 wake_up_var(&net->cells_outstanding);
David Howells989782d2017-11-02 15:27:50 +000032}
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/*
David Howells989782d2017-11-02 15:27:50 +000035 * Set the cell timer to fire after a given delay, assuming it's not already
36 * set for an earlier time.
37 */
38static void afs_set_cell_timer(struct afs_net *net, time64_t delay)
39{
40 if (net->live) {
41 atomic_inc(&net->cells_outstanding);
42 if (timer_reduce(&net->cells_timer, jiffies + delay * HZ))
43 afs_dec_cells_outstanding(net);
44 }
45}
46
47/*
48 * Look up and get an activation reference on a cell record under RCU
49 * conditions. The caller must hold the RCU read lock.
50 */
51struct afs_cell *afs_lookup_cell_rcu(struct afs_net *net,
52 const char *name, unsigned int namesz)
53{
54 struct afs_cell *cell = NULL;
55 struct rb_node *p;
56 int n, seq = 0, ret = 0;
57
58 _enter("%*.*s", namesz, namesz, name);
59
60 if (name && namesz == 0)
61 return ERR_PTR(-EINVAL);
62 if (namesz > AFS_MAXCELLNAME)
63 return ERR_PTR(-ENAMETOOLONG);
64
65 do {
66 /* Unfortunately, rbtree walking doesn't give reliable results
67 * under just the RCU read lock, so we have to check for
68 * changes.
69 */
70 if (cell)
71 afs_put_cell(net, cell);
72 cell = NULL;
73 ret = -ENOENT;
74
75 read_seqbegin_or_lock(&net->cells_lock, &seq);
76
77 if (!name) {
78 cell = rcu_dereference_raw(net->ws_cell);
79 if (cell) {
80 afs_get_cell(cell);
David Howellsfe342cf2018-04-09 21:12:31 +010081 break;
David Howells989782d2017-11-02 15:27:50 +000082 }
83 ret = -EDESTADDRREQ;
84 continue;
85 }
86
87 p = rcu_dereference_raw(net->cells.rb_node);
88 while (p) {
89 cell = rb_entry(p, struct afs_cell, net_node);
90
91 n = strncasecmp(cell->name, name,
92 min_t(size_t, cell->name_len, namesz));
93 if (n == 0)
94 n = cell->name_len - namesz;
95 if (n < 0) {
96 p = rcu_dereference_raw(p->rb_left);
97 } else if (n > 0) {
98 p = rcu_dereference_raw(p->rb_right);
99 } else {
100 if (atomic_inc_not_zero(&cell->usage)) {
101 ret = 0;
102 break;
103 }
104 /* We want to repeat the search, this time with
105 * the lock properly locked.
106 */
107 }
108 cell = NULL;
109 }
110
111 } while (need_seqretry(&net->cells_lock, seq));
112
113 done_seqretry(&net->cells_lock, seq);
114
115 return ret == 0 ? cell : ERR_PTR(ret);
116}
117
118/*
119 * Set up a cell record and fill in its name, VL server address list and
David Howells00d3b7a2007-04-26 15:57:07 -0700120 * allocate an anonymous key
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 */
David Howells989782d2017-11-02 15:27:50 +0000122static struct afs_cell *afs_alloc_cell(struct afs_net *net,
123 const char *name, unsigned int namelen,
David Howells0a5143f2018-10-20 00:57:57 +0100124 const char *addresses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
David Howellsca1cbbd2019-05-07 15:30:34 +0100126 struct afs_vlserver_list *vllist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 struct afs_cell *cell;
David Howells989782d2017-11-02 15:27:50 +0000128 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
David Howells989782d2017-11-02 15:27:50 +0000130 ASSERT(name);
131 if (namelen == 0)
132 return ERR_PTR(-EINVAL);
Wang Lei07567a52010-08-04 15:16:38 +0100133 if (namelen > AFS_MAXCELLNAME) {
134 _leave(" = -ENAMETOOLONG");
David Howells00d3b7a2007-04-26 15:57:07 -0700135 return ERR_PTR(-ENAMETOOLONG);
Wang Lei07567a52010-08-04 15:16:38 +0100136 }
David Howells37ab6362018-04-06 14:17:23 +0100137 if (namelen == 5 && memcmp(name, "@cell", 5) == 0)
138 return ERR_PTR(-EINVAL);
David Howells00d3b7a2007-04-26 15:57:07 -0700139
David Howells0a5143f2018-10-20 00:57:57 +0100140 _enter("%*.*s,%s", namelen, namelen, name, addresses);
David Howells989782d2017-11-02 15:27:50 +0000141
142 cell = kzalloc(sizeof(struct afs_cell), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (!cell) {
144 _leave(" = -ENOMEM");
David Howells08e0e7c2007-04-26 15:55:03 -0700145 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
147
David Howellsf044c882017-11-02 15:27:45 +0000148 cell->net = net;
David Howells989782d2017-11-02 15:27:50 +0000149 cell->name_len = namelen;
150 for (i = 0; i < namelen; i++)
151 cell->name[i] = tolower(name[i]);
152
153 atomic_set(&cell->usage, 2);
154 INIT_WORK(&cell->manager, afs_manage_cell);
David Howells8b2a4642017-11-02 15:27:50 +0000155 cell->flags = ((1 << AFS_CELL_FL_NOT_READY) |
156 (1 << AFS_CELL_FL_NO_LOOKUP_YET));
David Howellsd2ddc772017-11-02 15:27:50 +0000157 INIT_LIST_HEAD(&cell->proc_volumes);
158 rwlock_init(&cell->proc_lock);
David Howells0a5143f2018-10-20 00:57:57 +0100159 rwlock_init(&cell->vl_servers_lock);
David Howells4d9df982017-11-02 15:27:47 +0000160
David Howellsca1cbbd2019-05-07 15:30:34 +0100161 /* Provide a VL server list, filling it in if we were given a list of
162 * addresses to use.
David Howells989782d2017-11-02 15:27:50 +0000163 */
David Howells0a5143f2018-10-20 00:57:57 +0100164 if (addresses) {
David Howells0a5143f2018-10-20 00:57:57 +0100165 vllist = afs_parse_text_addrs(net,
166 addresses, strlen(addresses), ':',
167 VL_SERVICE, AFS_VL_PORT);
168 if (IS_ERR(vllist)) {
169 ret = PTR_ERR(vllist);
David Howells8b2a4642017-11-02 15:27:50 +0000170 goto parse_failed;
171 }
David Howells989782d2017-11-02 15:27:50 +0000172
David Howells989782d2017-11-02 15:27:50 +0000173 cell->dns_expiry = TIME64_MAX;
David Howellsded2f4c2018-10-20 00:57:57 +0100174 } else {
David Howellsca1cbbd2019-05-07 15:30:34 +0100175 ret = -ENOMEM;
176 vllist = afs_alloc_vlserver_list(0);
177 if (!vllist)
178 goto error;
David Howellsded2f4c2018-10-20 00:57:57 +0100179 cell->dns_expiry = ktime_get_real_seconds();
Wang Lei07567a52010-08-04 15:16:38 +0100180 }
181
David Howellsca1cbbd2019-05-07 15:30:34 +0100182 rcu_assign_pointer(cell->vl_servers, vllist);
183
David Howells00d3b7a2007-04-26 15:57:07 -0700184 _leave(" = %p", cell);
185 return cell;
186
David Howells8b2a4642017-11-02 15:27:50 +0000187parse_failed:
188 if (ret == -EINVAL)
189 printk(KERN_ERR "kAFS: bad VL server IP address\n");
David Howellsca1cbbd2019-05-07 15:30:34 +0100190error:
David Howells00d3b7a2007-04-26 15:57:07 -0700191 kfree(cell);
192 _leave(" = %d", ret);
193 return ERR_PTR(ret);
194}
195
196/*
David Howells989782d2017-11-02 15:27:50 +0000197 * afs_lookup_cell - Look up or create a cell record.
David Howellsf044c882017-11-02 15:27:45 +0000198 * @net: The network namespace
David Howells989782d2017-11-02 15:27:50 +0000199 * @name: The name of the cell.
200 * @namesz: The strlen of the cell name.
201 * @vllist: A colon/comma separated list of numeric IP addresses or NULL.
202 * @excl: T if an error should be given if the cell name already exists.
203 *
204 * Look up a cell record by name and query the DNS for VL server addresses if
205 * needed. Note that that actual DNS query is punted off to the manager thread
206 * so that this function can return immediately if interrupted whilst allowing
207 * cell records to be shared even if not yet fully constructed.
David Howells00d3b7a2007-04-26 15:57:07 -0700208 */
David Howells989782d2017-11-02 15:27:50 +0000209struct afs_cell *afs_lookup_cell(struct afs_net *net,
210 const char *name, unsigned int namesz,
211 const char *vllist, bool excl)
David Howells00d3b7a2007-04-26 15:57:07 -0700212{
David Howells989782d2017-11-02 15:27:50 +0000213 struct afs_cell *cell, *candidate, *cursor;
214 struct rb_node *parent, **pp;
215 int ret, n;
David Howells00d3b7a2007-04-26 15:57:07 -0700216
David Howells989782d2017-11-02 15:27:50 +0000217 _enter("%s,%s", name, vllist);
David Howells00d3b7a2007-04-26 15:57:07 -0700218
David Howells989782d2017-11-02 15:27:50 +0000219 if (!excl) {
220 rcu_read_lock();
221 cell = afs_lookup_cell_rcu(net, name, namesz);
222 rcu_read_unlock();
Gustavo A. R. Silva68327952017-11-17 16:40:32 -0600223 if (!IS_ERR(cell))
David Howells989782d2017-11-02 15:27:50 +0000224 goto wait_for_cell;
David Howells00d3b7a2007-04-26 15:57:07 -0700225 }
226
David Howells989782d2017-11-02 15:27:50 +0000227 /* Assume we're probably going to create a cell and preallocate and
228 * mostly set up a candidate record. We can then use this to stash the
229 * name, the net namespace and VL server addresses.
230 *
231 * We also want to do this before we hold any locks as it may involve
232 * upcalling to userspace to make DNS queries.
233 */
234 candidate = afs_alloc_cell(net, name, namesz, vllist);
235 if (IS_ERR(candidate)) {
236 _leave(" = %ld", PTR_ERR(candidate));
237 return candidate;
238 }
239
240 /* Find the insertion point and check to see if someone else added a
241 * cell whilst we were allocating.
242 */
243 write_seqlock(&net->cells_lock);
244
245 pp = &net->cells.rb_node;
246 parent = NULL;
247 while (*pp) {
248 parent = *pp;
249 cursor = rb_entry(parent, struct afs_cell, net_node);
250
251 n = strncasecmp(cursor->name, name,
252 min_t(size_t, cursor->name_len, namesz));
253 if (n == 0)
254 n = cursor->name_len - namesz;
255 if (n < 0)
256 pp = &(*pp)->rb_left;
257 else if (n > 0)
258 pp = &(*pp)->rb_right;
259 else
260 goto cell_already_exists;
261 }
262
263 cell = candidate;
264 candidate = NULL;
265 rb_link_node_rcu(&cell->net_node, parent, pp);
266 rb_insert_color(&cell->net_node, &net->cells);
267 atomic_inc(&net->cells_outstanding);
268 write_sequnlock(&net->cells_lock);
269
270 queue_work(afs_wq, &cell->manager);
271
272wait_for_cell:
273 _debug("wait_for_cell");
274 ret = wait_on_bit(&cell->flags, AFS_CELL_FL_NOT_READY, TASK_INTERRUPTIBLE);
275 smp_rmb();
276
277 switch (READ_ONCE(cell->state)) {
278 case AFS_CELL_FAILED:
279 ret = cell->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 goto error;
David Howells989782d2017-11-02 15:27:50 +0000281 default:
282 _debug("weird %u %d", cell->state, cell->error);
283 goto error;
284 case AFS_CELL_ACTIVE:
285 break;
286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
David Howells989782d2017-11-02 15:27:50 +0000288 _leave(" = %p [cell]", cell);
David Howells08e0e7c2007-04-26 15:55:03 -0700289 return cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
David Howells989782d2017-11-02 15:27:50 +0000291cell_already_exists:
292 _debug("cell exists");
293 cell = cursor;
294 if (excl) {
295 ret = -EEXIST;
296 } else {
David Howells989782d2017-11-02 15:27:50 +0000297 afs_get_cell(cursor);
298 ret = 0;
wangleibec5eb62010-08-11 09:38:04 +0100299 }
David Howells989782d2017-11-02 15:27:50 +0000300 write_sequnlock(&net->cells_lock);
301 kfree(candidate);
302 if (ret == 0)
303 goto wait_for_cell;
David Howells8b2a4642017-11-02 15:27:50 +0000304 goto error_noput;
David Howells989782d2017-11-02 15:27:50 +0000305error:
306 afs_put_cell(net, cell);
David Howells8b2a4642017-11-02 15:27:50 +0000307error_noput:
David Howells989782d2017-11-02 15:27:50 +0000308 _leave(" = %d [error]", ret);
309 return ERR_PTR(ret);
David Howellsec268152007-04-26 15:49:28 -0700310}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312/*
David Howells08e0e7c2007-04-26 15:55:03 -0700313 * set the root cell information
314 * - can be called with a module parameter string
315 * - can be called from a write to /proc/fs/afs/rootcell
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 */
David Howells989782d2017-11-02 15:27:50 +0000317int afs_cell_init(struct afs_net *net, const char *rootcell)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 struct afs_cell *old_root, *new_root;
David Howells989782d2017-11-02 15:27:50 +0000320 const char *cp, *vllist;
321 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 _enter("");
324
325 if (!rootcell) {
326 /* module is loaded with no parameters, or built statically.
327 * - in the future we might initialize cell DB here.
328 */
David Howells08e0e7c2007-04-26 15:55:03 -0700329 _leave(" = 0 [no root]");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return 0;
331 }
332
333 cp = strchr(rootcell, ':');
David Howells989782d2017-11-02 15:27:50 +0000334 if (!cp) {
Wang Lei07567a52010-08-04 15:16:38 +0100335 _debug("kAFS: no VL server IP addresses specified");
David Howells989782d2017-11-02 15:27:50 +0000336 vllist = NULL;
337 len = strlen(rootcell);
338 } else {
339 vllist = cp + 1;
340 len = cp - rootcell;
341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 /* allocate a cell record for the root cell */
David Howells989782d2017-11-02 15:27:50 +0000344 new_root = afs_lookup_cell(net, rootcell, len, vllist, false);
David Howells08e0e7c2007-04-26 15:55:03 -0700345 if (IS_ERR(new_root)) {
346 _leave(" = %ld", PTR_ERR(new_root));
347 return PTR_ERR(new_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349
David Howells17814ae2018-04-09 21:12:31 +0100350 if (!test_and_set_bit(AFS_CELL_FL_NO_GC, &new_root->flags))
351 afs_get_cell(new_root);
David Howells989782d2017-11-02 15:27:50 +0000352
David Howells08e0e7c2007-04-26 15:55:03 -0700353 /* install the new cell */
David Howells989782d2017-11-02 15:27:50 +0000354 write_seqlock(&net->cells_lock);
David Howells1588def2018-05-23 11:51:29 +0100355 old_root = rcu_access_pointer(net->ws_cell);
356 rcu_assign_pointer(net->ws_cell, new_root);
David Howells989782d2017-11-02 15:27:50 +0000357 write_sequnlock(&net->cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
David Howells989782d2017-11-02 15:27:50 +0000359 afs_put_cell(net, old_root);
David Howells08e0e7c2007-04-26 15:55:03 -0700360 _leave(" = 0");
361 return 0;
David Howellsec268152007-04-26 15:49:28 -0700362}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364/*
David Howells989782d2017-11-02 15:27:50 +0000365 * Update a cell's VL server address list from the DNS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 */
David Howells989782d2017-11-02 15:27:50 +0000367static void afs_update_cell(struct afs_cell *cell)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
David Howells0a5143f2018-10-20 00:57:57 +0100369 struct afs_vlserver_list *vllist, *old;
David Howellsded2f4c2018-10-20 00:57:57 +0100370 unsigned int min_ttl = READ_ONCE(afs_cell_min_ttl);
371 unsigned int max_ttl = READ_ONCE(afs_cell_max_ttl);
372 time64_t now, expiry = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
David Howells989782d2017-11-02 15:27:50 +0000374 _enter("%s", cell->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
David Howells0a5143f2018-10-20 00:57:57 +0100376 vllist = afs_dns_query(cell, &expiry);
David Howellsded2f4c2018-10-20 00:57:57 +0100377
378 now = ktime_get_real_seconds();
379 if (min_ttl > max_ttl)
380 max_ttl = min_ttl;
381 if (expiry < now + min_ttl)
382 expiry = now + min_ttl;
383 else if (expiry > now + max_ttl)
384 expiry = now + max_ttl;
385
David Howells0a5143f2018-10-20 00:57:57 +0100386 if (IS_ERR(vllist)) {
387 switch (PTR_ERR(vllist)) {
David Howells8b2a4642017-11-02 15:27:50 +0000388 case -ENODATA:
David Howellsded2f4c2018-10-20 00:57:57 +0100389 case -EDESTADDRREQ:
390 /* The DNS said that the cell does not exist or there
391 * weren't any addresses to be had.
392 */
David Howells8b2a4642017-11-02 15:27:50 +0000393 set_bit(AFS_CELL_FL_NOT_FOUND, &cell->flags);
394 clear_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags);
David Howellsded2f4c2018-10-20 00:57:57 +0100395 cell->dns_expiry = expiry;
David Howells8b2a4642017-11-02 15:27:50 +0000396 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
David Howells8b2a4642017-11-02 15:27:50 +0000398 case -EAGAIN:
399 case -ECONNREFUSED:
400 default:
401 set_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags);
David Howellsded2f4c2018-10-20 00:57:57 +0100402 cell->dns_expiry = now + 10;
David Howells8b2a4642017-11-02 15:27:50 +0000403 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405
David Howells8b2a4642017-11-02 15:27:50 +0000406 cell->error = -EDESTADDRREQ;
407 } else {
408 clear_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags);
409 clear_bit(AFS_CELL_FL_NOT_FOUND, &cell->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
David Howells6b8812f2019-05-07 15:16:26 +0100411 write_lock(&cell->vl_servers_lock);
David Howells0a5143f2018-10-20 00:57:57 +0100412 old = rcu_dereference_protected(cell->vl_servers, true);
413 rcu_assign_pointer(cell->vl_servers, vllist);
David Howells8b2a4642017-11-02 15:27:50 +0000414 cell->dns_expiry = expiry;
David Howells6b8812f2019-05-07 15:16:26 +0100415 write_unlock(&cell->vl_servers_lock);
wangleibec5eb62010-08-11 09:38:04 +0100416
David Howellsca1cbbd2019-05-07 15:30:34 +0100417 afs_put_vlserverlist(cell->net, old);
David Howells8b2a4642017-11-02 15:27:50 +0000418 }
419
420 if (test_and_clear_bit(AFS_CELL_FL_NO_LOOKUP_YET, &cell->flags))
421 wake_up_bit(&cell->flags, AFS_CELL_FL_NO_LOOKUP_YET);
wangleibec5eb62010-08-11 09:38:04 +0100422
David Howells989782d2017-11-02 15:27:50 +0000423 now = ktime_get_real_seconds();
David Howells8b2a4642017-11-02 15:27:50 +0000424 afs_set_cell_timer(cell->net, cell->dns_expiry - now);
David Howells989782d2017-11-02 15:27:50 +0000425 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700426}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428/*
David Howells989782d2017-11-02 15:27:50 +0000429 * Destroy a cell record
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 */
David Howells989782d2017-11-02 15:27:50 +0000431static void afs_cell_destroy(struct rcu_head *rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
David Howells989782d2017-11-02 15:27:50 +0000433 struct afs_cell *cell = container_of(rcu, struct afs_cell, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
David Howells989782d2017-11-02 15:27:50 +0000435 _enter("%p{%s}", cell, cell->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
David Howells08e0e7c2007-04-26 15:55:03 -0700437 ASSERTCMP(atomic_read(&cell->usage), ==, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
David Howells0a5143f2018-10-20 00:57:57 +0100439 afs_put_vlserverlist(cell->net, rcu_access_pointer(cell->vl_servers));
David Howells00d3b7a2007-04-26 15:57:07 -0700440 key_put(cell->anonymous_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 kfree(cell);
442
443 _leave(" [destroyed]");
David Howellsec268152007-04-26 15:49:28 -0700444}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446/*
David Howells989782d2017-11-02 15:27:50 +0000447 * Queue the cell manager.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 */
David Howells989782d2017-11-02 15:27:50 +0000449static void afs_queue_cell_manager(struct afs_net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
David Howells989782d2017-11-02 15:27:50 +0000451 int outstanding = atomic_inc_return(&net->cells_outstanding);
452
453 _enter("%d", outstanding);
454
455 if (!queue_work(afs_wq, &net->cells_manager))
456 afs_dec_cells_outstanding(net);
457}
458
459/*
460 * Cell management timer. We have an increment on cells_outstanding that we
461 * need to pass along to the work item.
462 */
463void afs_cells_timer(struct timer_list *timer)
464{
465 struct afs_net *net = container_of(timer, struct afs_net, cells_timer);
466
467 _enter("");
468 if (!queue_work(afs_wq, &net->cells_manager))
469 afs_dec_cells_outstanding(net);
470}
471
472/*
David Howells8b2a4642017-11-02 15:27:50 +0000473 * Get a reference on a cell record.
474 */
475struct afs_cell *afs_get_cell(struct afs_cell *cell)
476{
477 atomic_inc(&cell->usage);
478 return cell;
479}
480
481/*
David Howells989782d2017-11-02 15:27:50 +0000482 * Drop a reference on a cell record.
483 */
484void afs_put_cell(struct afs_net *net, struct afs_cell *cell)
485{
486 time64_t now, expire_delay;
487
488 if (!cell)
489 return;
490
491 _enter("%s", cell->name);
492
493 now = ktime_get_real_seconds();
494 cell->last_inactive = now;
495 expire_delay = 0;
496 if (!test_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags) &&
497 !test_bit(AFS_CELL_FL_NOT_FOUND, &cell->flags))
498 expire_delay = afs_cell_gc_delay;
499
500 if (atomic_dec_return(&cell->usage) > 1)
501 return;
502
503 /* 'cell' may now be garbage collected. */
504 afs_set_cell_timer(net, expire_delay);
505}
506
507/*
508 * Allocate a key to use as a placeholder for anonymous user security.
509 */
510static int afs_alloc_anon_key(struct afs_cell *cell)
511{
512 struct key *key;
513 char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp;
514
515 /* Create a key to represent an anonymous user. */
516 memcpy(keyname, "afs@", 4);
517 dp = keyname + 4;
518 cp = cell->name;
519 do {
520 *dp++ = tolower(*cp);
521 } while (*cp++);
522
523 key = rxrpc_get_null_key(keyname);
524 if (IS_ERR(key))
525 return PTR_ERR(key);
526
527 cell->anonymous_key = key;
528
529 _debug("anon key %p{%x}",
530 cell->anonymous_key, key_serial(cell->anonymous_key));
531 return 0;
532}
533
534/*
535 * Activate a cell.
536 */
537static int afs_activate_cell(struct afs_net *net, struct afs_cell *cell)
538{
David Howells6b3944e2018-10-11 22:45:49 +0100539 struct hlist_node **p;
540 struct afs_cell *pcell;
David Howells989782d2017-11-02 15:27:50 +0000541 int ret;
542
543 if (!cell->anonymous_key) {
544 ret = afs_alloc_anon_key(cell);
545 if (ret < 0)
546 return ret;
547 }
548
549#ifdef CONFIG_AFS_FSCACHE
550 cell->cache = fscache_acquire_cookie(afs_cache_netfs.primary_index,
551 &afs_cell_cache_index_def,
David Howells402cb8d2018-04-04 13:41:28 +0100552 cell->name, strlen(cell->name),
553 NULL, 0,
David Howellsee1235a2018-04-04 13:41:28 +0100554 cell, 0, true);
David Howells989782d2017-11-02 15:27:50 +0000555#endif
David Howells5b86d4f2018-05-18 11:46:15 +0100556 ret = afs_proc_cell_setup(cell);
David Howells989782d2017-11-02 15:27:50 +0000557 if (ret < 0)
558 return ret;
David Howells0da0b7f2018-06-15 15:19:22 +0100559
560 mutex_lock(&net->proc_cells_lock);
David Howells6b3944e2018-10-11 22:45:49 +0100561 for (p = &net->proc_cells.first; *p; p = &(*p)->next) {
562 pcell = hlist_entry(*p, struct afs_cell, proc_link);
563 if (strcmp(cell->name, pcell->name) < 0)
564 break;
565 }
566
567 cell->proc_link.pprev = p;
568 cell->proc_link.next = *p;
569 rcu_assign_pointer(*p, &cell->proc_link.next);
570 if (cell->proc_link.next)
571 cell->proc_link.next->pprev = &cell->proc_link.next;
572
David Howells0da0b7f2018-06-15 15:19:22 +0100573 afs_dynroot_mkdir(net, cell);
574 mutex_unlock(&net->proc_cells_lock);
David Howells989782d2017-11-02 15:27:50 +0000575 return 0;
576}
577
578/*
579 * Deactivate a cell.
580 */
581static void afs_deactivate_cell(struct afs_net *net, struct afs_cell *cell)
582{
583 _enter("%s", cell->name);
584
David Howells5b86d4f2018-05-18 11:46:15 +0100585 afs_proc_cell_remove(cell);
David Howells989782d2017-11-02 15:27:50 +0000586
David Howells0da0b7f2018-06-15 15:19:22 +0100587 mutex_lock(&net->proc_cells_lock);
David Howells6b3944e2018-10-11 22:45:49 +0100588 hlist_del_rcu(&cell->proc_link);
David Howells0da0b7f2018-06-15 15:19:22 +0100589 afs_dynroot_rmdir(net, cell);
590 mutex_unlock(&net->proc_cells_lock);
David Howells989782d2017-11-02 15:27:50 +0000591
592#ifdef CONFIG_AFS_FSCACHE
David Howells402cb8d2018-04-04 13:41:28 +0100593 fscache_relinquish_cookie(cell->cache, NULL, false);
David Howells989782d2017-11-02 15:27:50 +0000594 cell->cache = NULL;
595#endif
596
597 _leave("");
598}
599
600/*
601 * Manage a cell record, initialising and destroying it, maintaining its DNS
602 * records.
603 */
604static void afs_manage_cell(struct work_struct *work)
605{
606 struct afs_cell *cell = container_of(work, struct afs_cell, manager);
607 struct afs_net *net = cell->net;
608 bool deleted;
609 int ret, usage;
610
611 _enter("%s", cell->name);
612
613again:
614 _debug("state %u", cell->state);
615 switch (cell->state) {
616 case AFS_CELL_INACTIVE:
617 case AFS_CELL_FAILED:
618 write_seqlock(&net->cells_lock);
619 usage = 1;
620 deleted = atomic_try_cmpxchg_relaxed(&cell->usage, &usage, 0);
621 if (deleted)
622 rb_erase(&cell->net_node, &net->cells);
623 write_sequnlock(&net->cells_lock);
624 if (deleted)
625 goto final_destruction;
626 if (cell->state == AFS_CELL_FAILED)
627 goto done;
628 cell->state = AFS_CELL_UNSET;
629 goto again;
630
631 case AFS_CELL_UNSET:
632 cell->state = AFS_CELL_ACTIVATING;
633 goto again;
634
635 case AFS_CELL_ACTIVATING:
636 ret = afs_activate_cell(net, cell);
637 if (ret < 0)
638 goto activation_failed;
639
640 cell->state = AFS_CELL_ACTIVE;
641 smp_wmb();
642 clear_bit(AFS_CELL_FL_NOT_READY, &cell->flags);
643 wake_up_bit(&cell->flags, AFS_CELL_FL_NOT_READY);
644 goto again;
645
646 case AFS_CELL_ACTIVE:
647 if (atomic_read(&cell->usage) > 1) {
648 time64_t now = ktime_get_real_seconds();
649 if (cell->dns_expiry <= now && net->live)
650 afs_update_cell(cell);
651 goto done;
652 }
653 cell->state = AFS_CELL_DEACTIVATING;
654 goto again;
655
656 case AFS_CELL_DEACTIVATING:
657 set_bit(AFS_CELL_FL_NOT_READY, &cell->flags);
658 if (atomic_read(&cell->usage) > 1)
659 goto reverse_deactivation;
660 afs_deactivate_cell(net, cell);
661 cell->state = AFS_CELL_INACTIVE;
662 goto again;
663
664 default:
665 break;
666 }
667 _debug("bad state %u", cell->state);
668 BUG(); /* Unhandled state */
669
670activation_failed:
671 cell->error = ret;
672 afs_deactivate_cell(net, cell);
673
674 cell->state = AFS_CELL_FAILED;
675 smp_wmb();
676 if (test_and_clear_bit(AFS_CELL_FL_NOT_READY, &cell->flags))
677 wake_up_bit(&cell->flags, AFS_CELL_FL_NOT_READY);
678 goto again;
679
680reverse_deactivation:
681 cell->state = AFS_CELL_ACTIVE;
682 smp_wmb();
683 clear_bit(AFS_CELL_FL_NOT_READY, &cell->flags);
684 wake_up_bit(&cell->flags, AFS_CELL_FL_NOT_READY);
685 _leave(" [deact->act]");
686 return;
687
688done:
689 _leave(" [done %u]", cell->state);
690 return;
691
692final_destruction:
693 call_rcu(&cell->rcu, afs_cell_destroy);
694 afs_dec_cells_outstanding(net);
695 _leave(" [destruct %d]", atomic_read(&net->cells_outstanding));
696}
697
698/*
699 * Manage the records of cells known to a network namespace. This includes
700 * updating the DNS records and garbage collecting unused cells that were
701 * automatically added.
702 *
703 * Note that constructed cell records may only be removed from net->cells by
704 * this work item, so it is safe for this work item to stash a cursor pointing
705 * into the tree and then return to caller (provided it skips cells that are
706 * still under construction).
707 *
708 * Note also that we were given an increment on net->cells_outstanding by
709 * whoever queued us that we need to deal with before returning.
710 */
711void afs_manage_cells(struct work_struct *work)
712{
713 struct afs_net *net = container_of(work, struct afs_net, cells_manager);
714 struct rb_node *cursor;
715 time64_t now = ktime_get_real_seconds(), next_manage = TIME64_MAX;
716 bool purging = !net->live;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 _enter("");
719
David Howells989782d2017-11-02 15:27:50 +0000720 /* Trawl the cell database looking for cells that have expired from
721 * lack of use and cells whose DNS results have expired and dispatch
722 * their managers.
723 */
724 read_seqlock_excl(&net->cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
David Howells989782d2017-11-02 15:27:50 +0000726 for (cursor = rb_first(&net->cells); cursor; cursor = rb_next(cursor)) {
727 struct afs_cell *cell =
728 rb_entry(cursor, struct afs_cell, net_node);
729 unsigned usage;
730 bool sched_cell = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700731
David Howells989782d2017-11-02 15:27:50 +0000732 usage = atomic_read(&cell->usage);
733 _debug("manage %s %u", cell->name, usage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
David Howells989782d2017-11-02 15:27:50 +0000735 ASSERTCMP(usage, >=, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
David Howells989782d2017-11-02 15:27:50 +0000737 if (purging) {
738 if (test_and_clear_bit(AFS_CELL_FL_NO_GC, &cell->flags))
739 usage = atomic_dec_return(&cell->usage);
740 ASSERTCMP(usage, ==, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
742
David Howells989782d2017-11-02 15:27:50 +0000743 if (usage == 1) {
744 time64_t expire_at = cell->last_inactive;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
David Howells989782d2017-11-02 15:27:50 +0000746 if (!test_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags) &&
747 !test_bit(AFS_CELL_FL_NOT_FOUND, &cell->flags))
748 expire_at += afs_cell_gc_delay;
749 if (purging || expire_at <= now)
750 sched_cell = true;
751 else if (expire_at < next_manage)
752 next_manage = expire_at;
753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
David Howells989782d2017-11-02 15:27:50 +0000755 if (!purging) {
756 if (cell->dns_expiry <= now)
757 sched_cell = true;
758 else if (cell->dns_expiry <= next_manage)
759 next_manage = cell->dns_expiry;
760 }
761
762 if (sched_cell)
763 queue_work(afs_wq, &cell->manager);
764 }
765
766 read_sequnlock_excl(&net->cells_lock);
767
768 /* Update the timer on the way out. We have to pass an increment on
769 * cells_outstanding in the namespace that we are in to the timer or
770 * the work scheduler.
771 */
772 if (!purging && next_manage < TIME64_MAX) {
773 now = ktime_get_real_seconds();
774
775 if (next_manage - now <= 0) {
776 if (queue_work(afs_wq, &net->cells_manager))
777 atomic_inc(&net->cells_outstanding);
778 } else {
779 afs_set_cell_timer(net, next_manage - now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 }
781 }
782
David Howells989782d2017-11-02 15:27:50 +0000783 afs_dec_cells_outstanding(net);
784 _leave(" [%d]", atomic_read(&net->cells_outstanding));
785}
786
787/*
788 * Purge in-memory cell database.
789 */
790void afs_cell_purge(struct afs_net *net)
791{
792 struct afs_cell *ws;
793
794 _enter("");
795
796 write_seqlock(&net->cells_lock);
David Howells1588def2018-05-23 11:51:29 +0100797 ws = rcu_access_pointer(net->ws_cell);
798 RCU_INIT_POINTER(net->ws_cell, NULL);
David Howells989782d2017-11-02 15:27:50 +0000799 write_sequnlock(&net->cells_lock);
800 afs_put_cell(net, ws);
801
802 _debug("del timer");
803 if (del_timer_sync(&net->cells_timer))
804 atomic_dec(&net->cells_outstanding);
805
806 _debug("kick mgr");
807 afs_queue_cell_manager(net);
808
809 _debug("wait");
Peter Zijlstraab1fbe32018-03-15 11:42:28 +0100810 wait_var_event(&net->cells_outstanding,
811 !atomic_read(&net->cells_outstanding));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700813}