blob: 212098514ebfa572c0c41e6e4924dfa030b15efc [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 Howellsa5fb8e62019-08-22 13:28:43 +010077 ret = 0;
David Howellsfe342cf2018-04-09 21:12:31 +010078 break;
David Howells989782d2017-11-02 15:27:50 +000079 }
80 ret = -EDESTADDRREQ;
81 continue;
82 }
83
84 p = rcu_dereference_raw(net->cells.rb_node);
85 while (p) {
86 cell = rb_entry(p, struct afs_cell, net_node);
87
88 n = strncasecmp(cell->name, name,
89 min_t(size_t, cell->name_len, namesz));
90 if (n == 0)
91 n = cell->name_len - namesz;
92 if (n < 0) {
93 p = rcu_dereference_raw(p->rb_left);
94 } else if (n > 0) {
95 p = rcu_dereference_raw(p->rb_right);
96 } else {
97 if (atomic_inc_not_zero(&cell->usage)) {
98 ret = 0;
99 break;
100 }
101 /* We want to repeat the search, this time with
102 * the lock properly locked.
103 */
104 }
105 cell = NULL;
106 }
107
108 } while (need_seqretry(&net->cells_lock, seq));
109
110 done_seqretry(&net->cells_lock, seq);
111
David Howellsa5fb8e62019-08-22 13:28:43 +0100112 if (ret != 0 && cell)
113 afs_put_cell(net, cell);
114
David Howells989782d2017-11-02 15:27:50 +0000115 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 Howellsa45ea482020-01-26 01:02:53 +0000137
138 /* Prohibit cell names that contain unprintable chars, '/' and '@' or
139 * that begin with a dot. This also precludes "@cell".
140 */
141 if (name[0] == '.')
David Howells37ab6362018-04-06 14:17:23 +0100142 return ERR_PTR(-EINVAL);
David Howellsa45ea482020-01-26 01:02:53 +0000143 for (i = 0; i < namelen; i++) {
144 char ch = name[i];
145 if (!isprint(ch) || ch == '/' || ch == '@')
146 return ERR_PTR(-EINVAL);
147 }
David Howells00d3b7a2007-04-26 15:57:07 -0700148
David Howells0a5143f2018-10-20 00:57:57 +0100149 _enter("%*.*s,%s", namelen, namelen, name, addresses);
David Howells989782d2017-11-02 15:27:50 +0000150
151 cell = kzalloc(sizeof(struct afs_cell), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if (!cell) {
153 _leave(" = -ENOMEM");
David Howells08e0e7c2007-04-26 15:55:03 -0700154 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156
David Howellsf044c882017-11-02 15:27:45 +0000157 cell->net = net;
David Howells989782d2017-11-02 15:27:50 +0000158 cell->name_len = namelen;
159 for (i = 0; i < namelen; i++)
160 cell->name[i] = tolower(name[i]);
161
162 atomic_set(&cell->usage, 2);
163 INIT_WORK(&cell->manager, afs_manage_cell);
David Howellsd2ddc772017-11-02 15:27:50 +0000164 INIT_LIST_HEAD(&cell->proc_volumes);
165 rwlock_init(&cell->proc_lock);
David Howells0a5143f2018-10-20 00:57:57 +0100166 rwlock_init(&cell->vl_servers_lock);
David Howells8a070a92020-04-25 10:26:02 +0100167 cell->flags = (1 << AFS_CELL_FL_CHECK_ALIAS);
David Howells4d9df982017-11-02 15:27:47 +0000168
David Howellsca1cbbd2019-05-07 15:30:34 +0100169 /* Provide a VL server list, filling it in if we were given a list of
170 * addresses to use.
David Howells989782d2017-11-02 15:27:50 +0000171 */
David Howells0a5143f2018-10-20 00:57:57 +0100172 if (addresses) {
David Howells0a5143f2018-10-20 00:57:57 +0100173 vllist = afs_parse_text_addrs(net,
174 addresses, strlen(addresses), ':',
175 VL_SERVICE, AFS_VL_PORT);
176 if (IS_ERR(vllist)) {
177 ret = PTR_ERR(vllist);
David Howells8b2a4642017-11-02 15:27:50 +0000178 goto parse_failed;
179 }
David Howells989782d2017-11-02 15:27:50 +0000180
David Howellsd5c32c82019-05-07 15:06:36 +0100181 vllist->source = DNS_RECORD_FROM_CONFIG;
182 vllist->status = DNS_LOOKUP_NOT_DONE;
David Howells989782d2017-11-02 15:27:50 +0000183 cell->dns_expiry = TIME64_MAX;
David Howellsded2f4c2018-10-20 00:57:57 +0100184 } else {
David Howellsca1cbbd2019-05-07 15:30:34 +0100185 ret = -ENOMEM;
186 vllist = afs_alloc_vlserver_list(0);
187 if (!vllist)
188 goto error;
David Howellsd5c32c82019-05-07 15:06:36 +0100189 vllist->source = DNS_RECORD_UNAVAILABLE;
190 vllist->status = DNS_LOOKUP_NOT_DONE;
David Howellsded2f4c2018-10-20 00:57:57 +0100191 cell->dns_expiry = ktime_get_real_seconds();
Wang Lei07567a52010-08-04 15:16:38 +0100192 }
193
David Howellsca1cbbd2019-05-07 15:30:34 +0100194 rcu_assign_pointer(cell->vl_servers, vllist);
195
David Howellsd5c32c82019-05-07 15:06:36 +0100196 cell->dns_source = vllist->source;
197 cell->dns_status = vllist->status;
198 smp_store_release(&cell->dns_lookup_count, 1); /* vs source/status */
199
David Howells00d3b7a2007-04-26 15:57:07 -0700200 _leave(" = %p", cell);
201 return cell;
202
David Howells8b2a4642017-11-02 15:27:50 +0000203parse_failed:
204 if (ret == -EINVAL)
205 printk(KERN_ERR "kAFS: bad VL server IP address\n");
David Howellsca1cbbd2019-05-07 15:30:34 +0100206error:
David Howells00d3b7a2007-04-26 15:57:07 -0700207 kfree(cell);
208 _leave(" = %d", ret);
209 return ERR_PTR(ret);
210}
211
212/*
David Howells989782d2017-11-02 15:27:50 +0000213 * afs_lookup_cell - Look up or create a cell record.
David Howellsf044c882017-11-02 15:27:45 +0000214 * @net: The network namespace
David Howells989782d2017-11-02 15:27:50 +0000215 * @name: The name of the cell.
216 * @namesz: The strlen of the cell name.
217 * @vllist: A colon/comma separated list of numeric IP addresses or NULL.
218 * @excl: T if an error should be given if the cell name already exists.
219 *
220 * Look up a cell record by name and query the DNS for VL server addresses if
221 * needed. Note that that actual DNS query is punted off to the manager thread
222 * so that this function can return immediately if interrupted whilst allowing
223 * cell records to be shared even if not yet fully constructed.
David Howells00d3b7a2007-04-26 15:57:07 -0700224 */
David Howells989782d2017-11-02 15:27:50 +0000225struct afs_cell *afs_lookup_cell(struct afs_net *net,
226 const char *name, unsigned int namesz,
227 const char *vllist, bool excl)
David Howells00d3b7a2007-04-26 15:57:07 -0700228{
David Howells989782d2017-11-02 15:27:50 +0000229 struct afs_cell *cell, *candidate, *cursor;
230 struct rb_node *parent, **pp;
David Howellsd5c32c82019-05-07 15:06:36 +0100231 enum afs_cell_state state;
David Howells989782d2017-11-02 15:27:50 +0000232 int ret, n;
David Howells00d3b7a2007-04-26 15:57:07 -0700233
David Howells989782d2017-11-02 15:27:50 +0000234 _enter("%s,%s", name, vllist);
David Howells00d3b7a2007-04-26 15:57:07 -0700235
David Howells989782d2017-11-02 15:27:50 +0000236 if (!excl) {
237 rcu_read_lock();
238 cell = afs_lookup_cell_rcu(net, name, namesz);
239 rcu_read_unlock();
Gustavo A. R. Silva68327952017-11-17 16:40:32 -0600240 if (!IS_ERR(cell))
David Howells989782d2017-11-02 15:27:50 +0000241 goto wait_for_cell;
David Howells00d3b7a2007-04-26 15:57:07 -0700242 }
243
David Howells989782d2017-11-02 15:27:50 +0000244 /* Assume we're probably going to create a cell and preallocate and
245 * mostly set up a candidate record. We can then use this to stash the
246 * name, the net namespace and VL server addresses.
247 *
248 * We also want to do this before we hold any locks as it may involve
249 * upcalling to userspace to make DNS queries.
250 */
251 candidate = afs_alloc_cell(net, name, namesz, vllist);
252 if (IS_ERR(candidate)) {
253 _leave(" = %ld", PTR_ERR(candidate));
254 return candidate;
255 }
256
257 /* Find the insertion point and check to see if someone else added a
258 * cell whilst we were allocating.
259 */
260 write_seqlock(&net->cells_lock);
261
262 pp = &net->cells.rb_node;
263 parent = NULL;
264 while (*pp) {
265 parent = *pp;
266 cursor = rb_entry(parent, struct afs_cell, net_node);
267
268 n = strncasecmp(cursor->name, name,
269 min_t(size_t, cursor->name_len, namesz));
270 if (n == 0)
271 n = cursor->name_len - namesz;
272 if (n < 0)
273 pp = &(*pp)->rb_left;
274 else if (n > 0)
275 pp = &(*pp)->rb_right;
276 else
277 goto cell_already_exists;
278 }
279
280 cell = candidate;
281 candidate = NULL;
282 rb_link_node_rcu(&cell->net_node, parent, pp);
283 rb_insert_color(&cell->net_node, &net->cells);
284 atomic_inc(&net->cells_outstanding);
285 write_sequnlock(&net->cells_lock);
286
287 queue_work(afs_wq, &cell->manager);
288
289wait_for_cell:
290 _debug("wait_for_cell");
David Howellsd5c32c82019-05-07 15:06:36 +0100291 wait_var_event(&cell->state,
292 ({
293 state = smp_load_acquire(&cell->state); /* vs error */
294 state == AFS_CELL_ACTIVE || state == AFS_CELL_FAILED;
295 }));
David Howells989782d2017-11-02 15:27:50 +0000296
David Howellsd5c32c82019-05-07 15:06:36 +0100297 /* Check the state obtained from the wait check. */
298 if (state == AFS_CELL_FAILED) {
David Howells989782d2017-11-02 15:27:50 +0000299 ret = cell->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 goto error;
David Howells989782d2017-11-02 15:27:50 +0000301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
David Howells989782d2017-11-02 15:27:50 +0000303 _leave(" = %p [cell]", cell);
David Howells08e0e7c2007-04-26 15:55:03 -0700304 return cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
David Howells989782d2017-11-02 15:27:50 +0000306cell_already_exists:
307 _debug("cell exists");
308 cell = cursor;
309 if (excl) {
310 ret = -EEXIST;
311 } else {
David Howells989782d2017-11-02 15:27:50 +0000312 afs_get_cell(cursor);
313 ret = 0;
wangleibec5eb62010-08-11 09:38:04 +0100314 }
David Howells989782d2017-11-02 15:27:50 +0000315 write_sequnlock(&net->cells_lock);
316 kfree(candidate);
317 if (ret == 0)
318 goto wait_for_cell;
David Howells8b2a4642017-11-02 15:27:50 +0000319 goto error_noput;
David Howells989782d2017-11-02 15:27:50 +0000320error:
321 afs_put_cell(net, cell);
David Howells8b2a4642017-11-02 15:27:50 +0000322error_noput:
David Howells989782d2017-11-02 15:27:50 +0000323 _leave(" = %d [error]", ret);
324 return ERR_PTR(ret);
David Howellsec268152007-04-26 15:49:28 -0700325}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327/*
David Howells08e0e7c2007-04-26 15:55:03 -0700328 * set the root cell information
329 * - can be called with a module parameter string
330 * - can be called from a write to /proc/fs/afs/rootcell
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 */
David Howells989782d2017-11-02 15:27:50 +0000332int afs_cell_init(struct afs_net *net, const char *rootcell)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 struct afs_cell *old_root, *new_root;
David Howells989782d2017-11-02 15:27:50 +0000335 const char *cp, *vllist;
336 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 _enter("");
339
340 if (!rootcell) {
341 /* module is loaded with no parameters, or built statically.
342 * - in the future we might initialize cell DB here.
343 */
David Howells08e0e7c2007-04-26 15:55:03 -0700344 _leave(" = 0 [no root]");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return 0;
346 }
347
348 cp = strchr(rootcell, ':');
David Howells989782d2017-11-02 15:27:50 +0000349 if (!cp) {
Wang Lei07567a52010-08-04 15:16:38 +0100350 _debug("kAFS: no VL server IP addresses specified");
David Howells989782d2017-11-02 15:27:50 +0000351 vllist = NULL;
352 len = strlen(rootcell);
353 } else {
354 vllist = cp + 1;
355 len = cp - rootcell;
356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 /* allocate a cell record for the root cell */
David Howells989782d2017-11-02 15:27:50 +0000359 new_root = afs_lookup_cell(net, rootcell, len, vllist, false);
David Howells08e0e7c2007-04-26 15:55:03 -0700360 if (IS_ERR(new_root)) {
361 _leave(" = %ld", PTR_ERR(new_root));
362 return PTR_ERR(new_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
364
David Howells17814ae2018-04-09 21:12:31 +0100365 if (!test_and_set_bit(AFS_CELL_FL_NO_GC, &new_root->flags))
366 afs_get_cell(new_root);
David Howells989782d2017-11-02 15:27:50 +0000367
David Howells08e0e7c2007-04-26 15:55:03 -0700368 /* install the new cell */
David Howells989782d2017-11-02 15:27:50 +0000369 write_seqlock(&net->cells_lock);
David Howells1588def2018-05-23 11:51:29 +0100370 old_root = rcu_access_pointer(net->ws_cell);
371 rcu_assign_pointer(net->ws_cell, new_root);
David Howells989782d2017-11-02 15:27:50 +0000372 write_sequnlock(&net->cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
David Howells989782d2017-11-02 15:27:50 +0000374 afs_put_cell(net, old_root);
David Howells08e0e7c2007-04-26 15:55:03 -0700375 _leave(" = 0");
376 return 0;
David Howellsec268152007-04-26 15:49:28 -0700377}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379/*
David Howells989782d2017-11-02 15:27:50 +0000380 * Update a cell's VL server address list from the DNS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 */
David Howellsd5c32c82019-05-07 15:06:36 +0100382static int afs_update_cell(struct afs_cell *cell)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
David Howellsd5c32c82019-05-07 15:06:36 +0100384 struct afs_vlserver_list *vllist, *old = NULL, *p;
David Howellsded2f4c2018-10-20 00:57:57 +0100385 unsigned int min_ttl = READ_ONCE(afs_cell_min_ttl);
386 unsigned int max_ttl = READ_ONCE(afs_cell_max_ttl);
387 time64_t now, expiry = 0;
David Howellsd5c32c82019-05-07 15:06:36 +0100388 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
David Howells989782d2017-11-02 15:27:50 +0000390 _enter("%s", cell->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
David Howells0a5143f2018-10-20 00:57:57 +0100392 vllist = afs_dns_query(cell, &expiry);
David Howellsd5c32c82019-05-07 15:06:36 +0100393 if (IS_ERR(vllist)) {
394 ret = PTR_ERR(vllist);
395
396 _debug("%s: fail %d", cell->name, ret);
397 if (ret == -ENOMEM)
398 goto out_wake;
399
400 ret = -ENOMEM;
401 vllist = afs_alloc_vlserver_list(0);
402 if (!vllist)
403 goto out_wake;
404
405 switch (ret) {
406 case -ENODATA:
407 case -EDESTADDRREQ:
408 vllist->status = DNS_LOOKUP_GOT_NOT_FOUND;
409 break;
410 case -EAGAIN:
411 case -ECONNREFUSED:
412 vllist->status = DNS_LOOKUP_GOT_TEMP_FAILURE;
413 break;
414 default:
415 vllist->status = DNS_LOOKUP_GOT_LOCAL_FAILURE;
416 break;
417 }
418 }
419
420 _debug("%s: got list %d %d", cell->name, vllist->source, vllist->status);
421 cell->dns_status = vllist->status;
David Howellsded2f4c2018-10-20 00:57:57 +0100422
423 now = ktime_get_real_seconds();
424 if (min_ttl > max_ttl)
425 max_ttl = min_ttl;
426 if (expiry < now + min_ttl)
427 expiry = now + min_ttl;
428 else if (expiry > now + max_ttl)
429 expiry = now + max_ttl;
430
David Howellsd5c32c82019-05-07 15:06:36 +0100431 _debug("%s: status %d", cell->name, vllist->status);
432 if (vllist->source == DNS_RECORD_UNAVAILABLE) {
433 switch (vllist->status) {
434 case DNS_LOOKUP_GOT_NOT_FOUND:
David Howellsded2f4c2018-10-20 00:57:57 +0100435 /* The DNS said that the cell does not exist or there
436 * weren't any addresses to be had.
437 */
David Howellsded2f4c2018-10-20 00:57:57 +0100438 cell->dns_expiry = expiry;
David Howells8b2a4642017-11-02 15:27:50 +0000439 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
David Howellsd5c32c82019-05-07 15:06:36 +0100441 case DNS_LOOKUP_BAD:
442 case DNS_LOOKUP_GOT_LOCAL_FAILURE:
443 case DNS_LOOKUP_GOT_TEMP_FAILURE:
444 case DNS_LOOKUP_GOT_NS_FAILURE:
David Howells8b2a4642017-11-02 15:27:50 +0000445 default:
David Howellsded2f4c2018-10-20 00:57:57 +0100446 cell->dns_expiry = now + 10;
David Howells8b2a4642017-11-02 15:27:50 +0000447 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
David Howells8b2a4642017-11-02 15:27:50 +0000449 } else {
David Howells8b2a4642017-11-02 15:27:50 +0000450 cell->dns_expiry = expiry;
David Howells8b2a4642017-11-02 15:27:50 +0000451 }
452
David Howellsd5c32c82019-05-07 15:06:36 +0100453 /* Replace the VL server list if the new record has servers or the old
454 * record doesn't.
455 */
456 write_lock(&cell->vl_servers_lock);
457 p = rcu_dereference_protected(cell->vl_servers, true);
458 if (vllist->nr_servers > 0 || p->nr_servers == 0) {
459 rcu_assign_pointer(cell->vl_servers, vllist);
460 cell->dns_source = vllist->source;
461 old = p;
462 }
463 write_unlock(&cell->vl_servers_lock);
464 afs_put_vlserverlist(cell->net, old);
wangleibec5eb62010-08-11 09:38:04 +0100465
David Howellsd5c32c82019-05-07 15:06:36 +0100466out_wake:
467 smp_store_release(&cell->dns_lookup_count,
468 cell->dns_lookup_count + 1); /* vs source/status */
469 wake_up_var(&cell->dns_lookup_count);
470 _leave(" = %d", ret);
471 return ret;
David Howellsec268152007-04-26 15:49:28 -0700472}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474/*
David Howells989782d2017-11-02 15:27:50 +0000475 * Destroy a cell record
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 */
David Howells989782d2017-11-02 15:27:50 +0000477static void afs_cell_destroy(struct rcu_head *rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
David Howells989782d2017-11-02 15:27:50 +0000479 struct afs_cell *cell = container_of(rcu, struct afs_cell, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
David Howells989782d2017-11-02 15:27:50 +0000481 _enter("%p{%s}", cell, cell->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
David Howells08e0e7c2007-04-26 15:55:03 -0700483 ASSERTCMP(atomic_read(&cell->usage), ==, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
David Howells8a070a92020-04-25 10:26:02 +0100485 afs_put_volume(cell->net, cell->root_volume);
David Howells0a5143f2018-10-20 00:57:57 +0100486 afs_put_vlserverlist(cell->net, rcu_access_pointer(cell->vl_servers));
David Howells8a070a92020-04-25 10:26:02 +0100487 afs_put_cell(cell->net, cell->alias_of);
David Howells00d3b7a2007-04-26 15:57:07 -0700488 key_put(cell->anonymous_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 kfree(cell);
490
491 _leave(" [destroyed]");
David Howellsec268152007-04-26 15:49:28 -0700492}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494/*
David Howells989782d2017-11-02 15:27:50 +0000495 * Queue the cell manager.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 */
David Howells989782d2017-11-02 15:27:50 +0000497static void afs_queue_cell_manager(struct afs_net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
David Howells989782d2017-11-02 15:27:50 +0000499 int outstanding = atomic_inc_return(&net->cells_outstanding);
500
501 _enter("%d", outstanding);
502
503 if (!queue_work(afs_wq, &net->cells_manager))
504 afs_dec_cells_outstanding(net);
505}
506
507/*
508 * Cell management timer. We have an increment on cells_outstanding that we
509 * need to pass along to the work item.
510 */
511void afs_cells_timer(struct timer_list *timer)
512{
513 struct afs_net *net = container_of(timer, struct afs_net, cells_timer);
514
515 _enter("");
516 if (!queue_work(afs_wq, &net->cells_manager))
517 afs_dec_cells_outstanding(net);
518}
519
520/*
David Howells8b2a4642017-11-02 15:27:50 +0000521 * Get a reference on a cell record.
522 */
523struct afs_cell *afs_get_cell(struct afs_cell *cell)
524{
525 atomic_inc(&cell->usage);
526 return cell;
527}
528
529/*
David Howells989782d2017-11-02 15:27:50 +0000530 * Drop a reference on a cell record.
531 */
532void afs_put_cell(struct afs_net *net, struct afs_cell *cell)
533{
534 time64_t now, expire_delay;
535
536 if (!cell)
537 return;
538
539 _enter("%s", cell->name);
540
541 now = ktime_get_real_seconds();
542 cell->last_inactive = now;
543 expire_delay = 0;
David Howellsd5c32c82019-05-07 15:06:36 +0100544 if (cell->vl_servers->nr_servers)
David Howells989782d2017-11-02 15:27:50 +0000545 expire_delay = afs_cell_gc_delay;
546
547 if (atomic_dec_return(&cell->usage) > 1)
548 return;
549
550 /* 'cell' may now be garbage collected. */
551 afs_set_cell_timer(net, expire_delay);
552}
553
554/*
555 * Allocate a key to use as a placeholder for anonymous user security.
556 */
557static int afs_alloc_anon_key(struct afs_cell *cell)
558{
559 struct key *key;
560 char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp;
561
562 /* Create a key to represent an anonymous user. */
563 memcpy(keyname, "afs@", 4);
564 dp = keyname + 4;
565 cp = cell->name;
566 do {
567 *dp++ = tolower(*cp);
568 } while (*cp++);
569
570 key = rxrpc_get_null_key(keyname);
571 if (IS_ERR(key))
572 return PTR_ERR(key);
573
574 cell->anonymous_key = key;
575
576 _debug("anon key %p{%x}",
577 cell->anonymous_key, key_serial(cell->anonymous_key));
578 return 0;
579}
580
581/*
582 * Activate a cell.
583 */
584static int afs_activate_cell(struct afs_net *net, struct afs_cell *cell)
585{
David Howells6b3944e2018-10-11 22:45:49 +0100586 struct hlist_node **p;
587 struct afs_cell *pcell;
David Howells989782d2017-11-02 15:27:50 +0000588 int ret;
589
590 if (!cell->anonymous_key) {
591 ret = afs_alloc_anon_key(cell);
592 if (ret < 0)
593 return ret;
594 }
595
596#ifdef CONFIG_AFS_FSCACHE
597 cell->cache = fscache_acquire_cookie(afs_cache_netfs.primary_index,
598 &afs_cell_cache_index_def,
David Howells402cb8d2018-04-04 13:41:28 +0100599 cell->name, strlen(cell->name),
600 NULL, 0,
David Howellsee1235a2018-04-04 13:41:28 +0100601 cell, 0, true);
David Howells989782d2017-11-02 15:27:50 +0000602#endif
David Howells5b86d4f2018-05-18 11:46:15 +0100603 ret = afs_proc_cell_setup(cell);
David Howells989782d2017-11-02 15:27:50 +0000604 if (ret < 0)
605 return ret;
David Howells0da0b7f2018-06-15 15:19:22 +0100606
607 mutex_lock(&net->proc_cells_lock);
David Howells6b3944e2018-10-11 22:45:49 +0100608 for (p = &net->proc_cells.first; *p; p = &(*p)->next) {
609 pcell = hlist_entry(*p, struct afs_cell, proc_link);
610 if (strcmp(cell->name, pcell->name) < 0)
611 break;
612 }
613
614 cell->proc_link.pprev = p;
615 cell->proc_link.next = *p;
616 rcu_assign_pointer(*p, &cell->proc_link.next);
617 if (cell->proc_link.next)
618 cell->proc_link.next->pprev = &cell->proc_link.next;
619
David Howells0da0b7f2018-06-15 15:19:22 +0100620 afs_dynroot_mkdir(net, cell);
621 mutex_unlock(&net->proc_cells_lock);
David Howells989782d2017-11-02 15:27:50 +0000622 return 0;
623}
624
625/*
626 * Deactivate a cell.
627 */
628static void afs_deactivate_cell(struct afs_net *net, struct afs_cell *cell)
629{
630 _enter("%s", cell->name);
631
David Howells5b86d4f2018-05-18 11:46:15 +0100632 afs_proc_cell_remove(cell);
David Howells989782d2017-11-02 15:27:50 +0000633
David Howells0da0b7f2018-06-15 15:19:22 +0100634 mutex_lock(&net->proc_cells_lock);
David Howells6b3944e2018-10-11 22:45:49 +0100635 hlist_del_rcu(&cell->proc_link);
David Howells0da0b7f2018-06-15 15:19:22 +0100636 afs_dynroot_rmdir(net, cell);
637 mutex_unlock(&net->proc_cells_lock);
David Howells989782d2017-11-02 15:27:50 +0000638
639#ifdef CONFIG_AFS_FSCACHE
David Howells402cb8d2018-04-04 13:41:28 +0100640 fscache_relinquish_cookie(cell->cache, NULL, false);
David Howells989782d2017-11-02 15:27:50 +0000641 cell->cache = NULL;
642#endif
643
644 _leave("");
645}
646
647/*
648 * Manage a cell record, initialising and destroying it, maintaining its DNS
649 * records.
650 */
651static void afs_manage_cell(struct work_struct *work)
652{
653 struct afs_cell *cell = container_of(work, struct afs_cell, manager);
654 struct afs_net *net = cell->net;
655 bool deleted;
656 int ret, usage;
657
658 _enter("%s", cell->name);
659
660again:
661 _debug("state %u", cell->state);
662 switch (cell->state) {
663 case AFS_CELL_INACTIVE:
664 case AFS_CELL_FAILED:
665 write_seqlock(&net->cells_lock);
666 usage = 1;
667 deleted = atomic_try_cmpxchg_relaxed(&cell->usage, &usage, 0);
668 if (deleted)
669 rb_erase(&cell->net_node, &net->cells);
670 write_sequnlock(&net->cells_lock);
671 if (deleted)
672 goto final_destruction;
673 if (cell->state == AFS_CELL_FAILED)
674 goto done;
David Howellsd5c32c82019-05-07 15:06:36 +0100675 smp_store_release(&cell->state, AFS_CELL_UNSET);
676 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000677 goto again;
678
679 case AFS_CELL_UNSET:
David Howellsd5c32c82019-05-07 15:06:36 +0100680 smp_store_release(&cell->state, AFS_CELL_ACTIVATING);
681 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000682 goto again;
683
684 case AFS_CELL_ACTIVATING:
685 ret = afs_activate_cell(net, cell);
686 if (ret < 0)
687 goto activation_failed;
688
David Howellsd5c32c82019-05-07 15:06:36 +0100689 smp_store_release(&cell->state, AFS_CELL_ACTIVE);
690 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000691 goto again;
692
693 case AFS_CELL_ACTIVE:
694 if (atomic_read(&cell->usage) > 1) {
David Howellsd5c32c82019-05-07 15:06:36 +0100695 if (test_and_clear_bit(AFS_CELL_FL_DO_LOOKUP, &cell->flags)) {
696 ret = afs_update_cell(cell);
697 if (ret < 0)
698 cell->error = ret;
699 }
David Howells989782d2017-11-02 15:27:50 +0000700 goto done;
701 }
David Howellsd5c32c82019-05-07 15:06:36 +0100702 smp_store_release(&cell->state, AFS_CELL_DEACTIVATING);
703 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000704 goto again;
705
706 case AFS_CELL_DEACTIVATING:
David Howells989782d2017-11-02 15:27:50 +0000707 if (atomic_read(&cell->usage) > 1)
708 goto reverse_deactivation;
709 afs_deactivate_cell(net, cell);
David Howellsd5c32c82019-05-07 15:06:36 +0100710 smp_store_release(&cell->state, AFS_CELL_INACTIVE);
711 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000712 goto again;
713
714 default:
715 break;
716 }
717 _debug("bad state %u", cell->state);
718 BUG(); /* Unhandled state */
719
720activation_failed:
721 cell->error = ret;
722 afs_deactivate_cell(net, cell);
723
David Howellsd5c32c82019-05-07 15:06:36 +0100724 smp_store_release(&cell->state, AFS_CELL_FAILED); /* vs error */
725 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000726 goto again;
727
728reverse_deactivation:
David Howellsd5c32c82019-05-07 15:06:36 +0100729 smp_store_release(&cell->state, AFS_CELL_ACTIVE);
730 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000731 _leave(" [deact->act]");
732 return;
733
734done:
735 _leave(" [done %u]", cell->state);
736 return;
737
738final_destruction:
739 call_rcu(&cell->rcu, afs_cell_destroy);
740 afs_dec_cells_outstanding(net);
741 _leave(" [destruct %d]", atomic_read(&net->cells_outstanding));
742}
743
744/*
745 * Manage the records of cells known to a network namespace. This includes
746 * updating the DNS records and garbage collecting unused cells that were
747 * automatically added.
748 *
749 * Note that constructed cell records may only be removed from net->cells by
750 * this work item, so it is safe for this work item to stash a cursor pointing
751 * into the tree and then return to caller (provided it skips cells that are
752 * still under construction).
753 *
754 * Note also that we were given an increment on net->cells_outstanding by
755 * whoever queued us that we need to deal with before returning.
756 */
757void afs_manage_cells(struct work_struct *work)
758{
759 struct afs_net *net = container_of(work, struct afs_net, cells_manager);
760 struct rb_node *cursor;
761 time64_t now = ktime_get_real_seconds(), next_manage = TIME64_MAX;
762 bool purging = !net->live;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 _enter("");
765
David Howells989782d2017-11-02 15:27:50 +0000766 /* Trawl the cell database looking for cells that have expired from
767 * lack of use and cells whose DNS results have expired and dispatch
768 * their managers.
769 */
770 read_seqlock_excl(&net->cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
David Howells989782d2017-11-02 15:27:50 +0000772 for (cursor = rb_first(&net->cells); cursor; cursor = rb_next(cursor)) {
773 struct afs_cell *cell =
774 rb_entry(cursor, struct afs_cell, net_node);
775 unsigned usage;
776 bool sched_cell = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700777
David Howells989782d2017-11-02 15:27:50 +0000778 usage = atomic_read(&cell->usage);
779 _debug("manage %s %u", cell->name, usage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
David Howells989782d2017-11-02 15:27:50 +0000781 ASSERTCMP(usage, >=, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
David Howells989782d2017-11-02 15:27:50 +0000783 if (purging) {
784 if (test_and_clear_bit(AFS_CELL_FL_NO_GC, &cell->flags))
785 usage = atomic_dec_return(&cell->usage);
786 ASSERTCMP(usage, ==, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
788
David Howells989782d2017-11-02 15:27:50 +0000789 if (usage == 1) {
David Howellsd5c32c82019-05-07 15:06:36 +0100790 struct afs_vlserver_list *vllist;
David Howells989782d2017-11-02 15:27:50 +0000791 time64_t expire_at = cell->last_inactive;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
David Howellsd5c32c82019-05-07 15:06:36 +0100793 read_lock(&cell->vl_servers_lock);
794 vllist = rcu_dereference_protected(
795 cell->vl_servers,
796 lockdep_is_held(&cell->vl_servers_lock));
797 if (vllist->nr_servers > 0)
David Howells989782d2017-11-02 15:27:50 +0000798 expire_at += afs_cell_gc_delay;
David Howellsd5c32c82019-05-07 15:06:36 +0100799 read_unlock(&cell->vl_servers_lock);
David Howells989782d2017-11-02 15:27:50 +0000800 if (purging || expire_at <= now)
801 sched_cell = true;
802 else if (expire_at < next_manage)
803 next_manage = expire_at;
804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
David Howells989782d2017-11-02 15:27:50 +0000806 if (!purging) {
David Howellsd5c32c82019-05-07 15:06:36 +0100807 if (test_bit(AFS_CELL_FL_DO_LOOKUP, &cell->flags))
David Howells989782d2017-11-02 15:27:50 +0000808 sched_cell = true;
David Howells989782d2017-11-02 15:27:50 +0000809 }
810
811 if (sched_cell)
812 queue_work(afs_wq, &cell->manager);
813 }
814
815 read_sequnlock_excl(&net->cells_lock);
816
817 /* Update the timer on the way out. We have to pass an increment on
818 * cells_outstanding in the namespace that we are in to the timer or
819 * the work scheduler.
820 */
821 if (!purging && next_manage < TIME64_MAX) {
822 now = ktime_get_real_seconds();
823
824 if (next_manage - now <= 0) {
825 if (queue_work(afs_wq, &net->cells_manager))
826 atomic_inc(&net->cells_outstanding);
827 } else {
828 afs_set_cell_timer(net, next_manage - now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830 }
831
David Howells989782d2017-11-02 15:27:50 +0000832 afs_dec_cells_outstanding(net);
833 _leave(" [%d]", atomic_read(&net->cells_outstanding));
834}
835
836/*
837 * Purge in-memory cell database.
838 */
839void afs_cell_purge(struct afs_net *net)
840{
841 struct afs_cell *ws;
842
843 _enter("");
844
845 write_seqlock(&net->cells_lock);
David Howells1588def2018-05-23 11:51:29 +0100846 ws = rcu_access_pointer(net->ws_cell);
847 RCU_INIT_POINTER(net->ws_cell, NULL);
David Howells989782d2017-11-02 15:27:50 +0000848 write_sequnlock(&net->cells_lock);
849 afs_put_cell(net, ws);
850
851 _debug("del timer");
852 if (del_timer_sync(&net->cells_timer))
853 atomic_dec(&net->cells_outstanding);
854
855 _debug("kick mgr");
856 afs_queue_cell_manager(net);
857
858 _debug("wait");
Peter Zijlstraab1fbe32018-03-15 11:42:28 +0100859 wait_var_event(&net->cells_outstanding,
860 !atomic_read(&net->cells_outstanding));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700862}