]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/afs/cell.c
Merge tag 'tty-5.15-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty
[mirror_ubuntu-jammy-kernel.git] / fs / afs / cell.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
ec26815a 2/* AFS cell and server record management
1da177e4 3 *
989782dc 4 * Copyright (C) 2002, 2017 Red Hat, Inc. All Rights Reserved.
1da177e4 5 * Written by David Howells (dhowells@redhat.com)
1da177e4
LT
6 */
7
1da177e4 8#include <linux/slab.h>
00d3b7a4
DH
9#include <linux/key.h>
10#include <linux/ctype.h>
07567a55 11#include <linux/dns_resolver.h>
e8edc6e0 12#include <linux/sched.h>
3838d3ec 13#include <linux/inet.h>
0da0b7fd 14#include <linux/namei.h>
00d3b7a4 15#include <keys/rxrpc-type.h>
1da177e4
LT
16#include "internal.h"
17
fe342cf7 18static unsigned __read_mostly afs_cell_gc_delay = 10;
ded2f4c5
DH
19static unsigned __read_mostly afs_cell_min_ttl = 10 * 60;
20static unsigned __read_mostly afs_cell_max_ttl = 24 * 60 * 60;
dca54a7b 21static atomic_t cell_debug_id;
989782dc 22
286377f6 23static void afs_queue_cell_manager(struct afs_net *);
88c853c3 24static void afs_manage_cell_work(struct work_struct *);
989782dc
DH
25
26static void afs_dec_cells_outstanding(struct afs_net *net)
27{
28 if (atomic_dec_and_test(&net->cells_outstanding))
ab1fbe32 29 wake_up_var(&net->cells_outstanding);
989782dc
DH
30}
31
1da177e4 32/*
989782dc
DH
33 * Set the cell timer to fire after a given delay, assuming it's not already
34 * set for an earlier time.
1da177e4 35 */
989782dc 36static void afs_set_cell_timer(struct afs_net *net, time64_t delay)
1da177e4 37{
989782dc
DH
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);
286377f6
DH
42 } else {
43 afs_queue_cell_manager(net);
989782dc
DH
44 }
45}
46
47/*
92e3cc91
DH
48 * Look up and get an activation reference on a cell record. The caller must
49 * hold net->cells_lock at least read-locked.
989782dc 50 */
92e3cc91 51static struct afs_cell *afs_find_cell_locked(struct afs_net *net,
dca54a7b
DH
52 const char *name, unsigned int namesz,
53 enum afs_cell_trace reason)
989782dc
DH
54{
55 struct afs_cell *cell = NULL;
56 struct rb_node *p;
92e3cc91 57 int n;
989782dc
DH
58
59 _enter("%*.*s", namesz, namesz, name);
60
61 if (name && namesz == 0)
62 return ERR_PTR(-EINVAL);
63 if (namesz > AFS_MAXCELLNAME)
64 return ERR_PTR(-ENAMETOOLONG);
65
92e3cc91
DH
66 if (!name) {
67 cell = net->ws_cell;
68 if (!cell)
69 return ERR_PTR(-EDESTADDRREQ);
88c853c3 70 goto found;
92e3cc91 71 }
989782dc 72
92e3cc91
DH
73 p = net->cells.rb_node;
74 while (p) {
75 cell = rb_entry(p, struct afs_cell, net_node);
1da177e4 76
92e3cc91
DH
77 n = strncasecmp(cell->name, name,
78 min_t(size_t, cell->name_len, namesz));
79 if (n == 0)
80 n = cell->name_len - namesz;
81 if (n < 0)
82 p = p->rb_left;
83 else if (n > 0)
84 p = p->rb_right;
85 else
86 goto found;
87 }
88
89 return ERR_PTR(-ENOENT);
1da177e4 90
92e3cc91 91found:
dca54a7b 92 return afs_use_cell(cell, reason);
92e3cc91 93}
a5fb8e6c 94
88c853c3
DH
95/*
96 * Look up and get an activation reference on a cell record.
97 */
92e3cc91 98struct afs_cell *afs_find_cell(struct afs_net *net,
dca54a7b
DH
99 const char *name, unsigned int namesz,
100 enum afs_cell_trace reason)
92e3cc91
DH
101{
102 struct afs_cell *cell;
103
104 down_read(&net->cells_lock);
dca54a7b 105 cell = afs_find_cell_locked(net, name, namesz, reason);
92e3cc91
DH
106 up_read(&net->cells_lock);
107 return cell;
989782dc
DH
108}
109
110/*
111 * Set up a cell record and fill in its name, VL server address list and
112 * allocate an anonymous key
113 */
114static struct afs_cell *afs_alloc_cell(struct afs_net *net,
115 const char *name, unsigned int namelen,
0a5143f2 116 const char *addresses)
989782dc 117{
ca1cbbdc 118 struct afs_vlserver_list *vllist;
989782dc
DH
119 struct afs_cell *cell;
120 int i, ret;
121
122 ASSERT(name);
123 if (namelen == 0)
124 return ERR_PTR(-EINVAL);
07567a55
WL
125 if (namelen > AFS_MAXCELLNAME) {
126 _leave(" = -ENAMETOOLONG");
00d3b7a4 127 return ERR_PTR(-ENAMETOOLONG);
07567a55 128 }
a45ea48e
DH
129
130 /* Prohibit cell names that contain unprintable chars, '/' and '@' or
131 * that begin with a dot. This also precludes "@cell".
132 */
133 if (name[0] == '.')
37ab6368 134 return ERR_PTR(-EINVAL);
a45ea48e
DH
135 for (i = 0; i < namelen; i++) {
136 char ch = name[i];
137 if (!isprint(ch) || ch == '/' || ch == '@')
138 return ERR_PTR(-EINVAL);
139 }
00d3b7a4 140
0a5143f2 141 _enter("%*.*s,%s", namelen, namelen, name, addresses);
989782dc
DH
142
143 cell = kzalloc(sizeof(struct afs_cell), GFP_KERNEL);
1da177e4
LT
144 if (!cell) {
145 _leave(" = -ENOMEM");
08e0e7c8 146 return ERR_PTR(-ENOMEM);
1da177e4
LT
147 }
148
719fdd32
DH
149 cell->name = kmalloc(namelen + 1, GFP_KERNEL);
150 if (!cell->name) {
151 kfree(cell);
152 return ERR_PTR(-ENOMEM);
153 }
154
f044c884 155 cell->net = net;
989782dc
DH
156 cell->name_len = namelen;
157 for (i = 0; i < namelen; i++)
158 cell->name[i] = tolower(name[i]);
719fdd32 159 cell->name[i] = 0;
989782dc 160
88c853c3
DH
161 atomic_set(&cell->ref, 1);
162 atomic_set(&cell->active, 0);
163 INIT_WORK(&cell->manager, afs_manage_cell_work);
20325960
DH
164 cell->volumes = RB_ROOT;
165 INIT_HLIST_HEAD(&cell->proc_volumes);
166 seqlock_init(&cell->volume_lock);
167 cell->fs_servers = RB_ROOT;
168 seqlock_init(&cell->fs_lock);
6e0e99d5
DH
169 INIT_LIST_HEAD(&cell->fs_open_mmaps);
170 init_rwsem(&cell->fs_open_mmaps_lock);
0a5143f2 171 rwlock_init(&cell->vl_servers_lock);
8a070a96 172 cell->flags = (1 << AFS_CELL_FL_CHECK_ALIAS);
4d9df986 173
ca1cbbdc
DH
174 /* Provide a VL server list, filling it in if we were given a list of
175 * addresses to use.
989782dc 176 */
0a5143f2 177 if (addresses) {
0a5143f2
DH
178 vllist = afs_parse_text_addrs(net,
179 addresses, strlen(addresses), ':',
180 VL_SERVICE, AFS_VL_PORT);
181 if (IS_ERR(vllist)) {
182 ret = PTR_ERR(vllist);
8b2a464c
DH
183 goto parse_failed;
184 }
00d3b7a4 185
d5c32c89
DH
186 vllist->source = DNS_RECORD_FROM_CONFIG;
187 vllist->status = DNS_LOOKUP_NOT_DONE;
989782dc 188 cell->dns_expiry = TIME64_MAX;
ded2f4c5 189 } else {
ca1cbbdc
DH
190 ret = -ENOMEM;
191 vllist = afs_alloc_vlserver_list(0);
192 if (!vllist)
193 goto error;
d5c32c89
DH
194 vllist->source = DNS_RECORD_UNAVAILABLE;
195 vllist->status = DNS_LOOKUP_NOT_DONE;
ded2f4c5 196 cell->dns_expiry = ktime_get_real_seconds();
00d3b7a4 197 }
00d3b7a4 198
ca1cbbdc
DH
199 rcu_assign_pointer(cell->vl_servers, vllist);
200
d5c32c89
DH
201 cell->dns_source = vllist->source;
202 cell->dns_status = vllist->status;
203 smp_store_release(&cell->dns_lookup_count, 1); /* vs source/status */
88c853c3 204 atomic_inc(&net->cells_outstanding);
dca54a7b
DH
205 cell->debug_id = atomic_inc_return(&cell_debug_id);
206 trace_afs_cell(cell->debug_id, 1, 0, afs_cell_trace_alloc);
d5c32c89 207
00d3b7a4
DH
208 _leave(" = %p", cell);
209 return cell;
210
8b2a464c
DH
211parse_failed:
212 if (ret == -EINVAL)
213 printk(KERN_ERR "kAFS: bad VL server IP address\n");
ca1cbbdc 214error:
719fdd32 215 kfree(cell->name);
00d3b7a4
DH
216 kfree(cell);
217 _leave(" = %d", ret);
218 return ERR_PTR(ret);
219}
1da177e4 220
00d3b7a4 221/*
989782dc 222 * afs_lookup_cell - Look up or create a cell record.
f044c884 223 * @net: The network namespace
989782dc
DH
224 * @name: The name of the cell.
225 * @namesz: The strlen of the cell name.
226 * @vllist: A colon/comma separated list of numeric IP addresses or NULL.
227 * @excl: T if an error should be given if the cell name already exists.
228 *
229 * Look up a cell record by name and query the DNS for VL server addresses if
230 * needed. Note that that actual DNS query is punted off to the manager thread
231 * so that this function can return immediately if interrupted whilst allowing
232 * cell records to be shared even if not yet fully constructed.
00d3b7a4 233 */
989782dc
DH
234struct afs_cell *afs_lookup_cell(struct afs_net *net,
235 const char *name, unsigned int namesz,
236 const char *vllist, bool excl)
00d3b7a4 237{
989782dc
DH
238 struct afs_cell *cell, *candidate, *cursor;
239 struct rb_node *parent, **pp;
d5c32c89 240 enum afs_cell_state state;
989782dc
DH
241 int ret, n;
242
243 _enter("%s,%s", name, vllist);
244
245 if (!excl) {
dca54a7b 246 cell = afs_find_cell(net, name, namesz, afs_cell_trace_use_lookup);
68327951 247 if (!IS_ERR(cell))
989782dc 248 goto wait_for_cell;
989782dc 249 }
00d3b7a4 250
989782dc
DH
251 /* Assume we're probably going to create a cell and preallocate and
252 * mostly set up a candidate record. We can then use this to stash the
253 * name, the net namespace and VL server addresses.
254 *
255 * We also want to do this before we hold any locks as it may involve
256 * upcalling to userspace to make DNS queries.
257 */
258 candidate = afs_alloc_cell(net, name, namesz, vllist);
259 if (IS_ERR(candidate)) {
260 _leave(" = %ld", PTR_ERR(candidate));
261 return candidate;
5214b729 262 }
5214b729 263
989782dc
DH
264 /* Find the insertion point and check to see if someone else added a
265 * cell whilst we were allocating.
266 */
92e3cc91 267 down_write(&net->cells_lock);
989782dc
DH
268
269 pp = &net->cells.rb_node;
270 parent = NULL;
271 while (*pp) {
272 parent = *pp;
273 cursor = rb_entry(parent, struct afs_cell, net_node);
274
275 n = strncasecmp(cursor->name, name,
276 min_t(size_t, cursor->name_len, namesz));
277 if (n == 0)
278 n = cursor->name_len - namesz;
279 if (n < 0)
280 pp = &(*pp)->rb_left;
281 else if (n > 0)
282 pp = &(*pp)->rb_right;
283 else
284 goto cell_already_exists;
00d3b7a4
DH
285 }
286
989782dc
DH
287 cell = candidate;
288 candidate = NULL;
88c853c3 289 atomic_set(&cell->active, 2);
dca54a7b 290 trace_afs_cell(cell->debug_id, atomic_read(&cell->ref), 2, afs_cell_trace_insert);
989782dc
DH
291 rb_link_node_rcu(&cell->net_node, parent, pp);
292 rb_insert_color(&cell->net_node, &net->cells);
92e3cc91 293 up_write(&net->cells_lock);
1da177e4 294
dca54a7b 295 afs_queue_cell(cell, afs_cell_trace_get_queue_new);
1da177e4 296
989782dc 297wait_for_cell:
dca54a7b
DH
298 trace_afs_cell(cell->debug_id, atomic_read(&cell->ref), atomic_read(&cell->active),
299 afs_cell_trace_wait);
989782dc 300 _debug("wait_for_cell");
d5c32c89
DH
301 wait_var_event(&cell->state,
302 ({
303 state = smp_load_acquire(&cell->state); /* vs error */
1d0e850a 304 state == AFS_CELL_ACTIVE || state == AFS_CELL_REMOVED;
d5c32c89
DH
305 }));
306
307 /* Check the state obtained from the wait check. */
1d0e850a 308 if (state == AFS_CELL_REMOVED) {
989782dc
DH
309 ret = cell->error;
310 goto error;
989782dc 311 }
1da177e4 312
989782dc 313 _leave(" = %p [cell]", cell);
08e0e7c8 314 return cell;
1da177e4 315
989782dc
DH
316cell_already_exists:
317 _debug("cell exists");
318 cell = cursor;
319 if (excl) {
320 ret = -EEXIST;
321 } else {
dca54a7b 322 afs_use_cell(cursor, afs_cell_trace_use_lookup);
989782dc
DH
323 ret = 0;
324 }
92e3cc91 325 up_write(&net->cells_lock);
88c853c3 326 if (candidate)
dca54a7b 327 afs_put_cell(candidate, afs_cell_trace_put_candidate);
989782dc
DH
328 if (ret == 0)
329 goto wait_for_cell;
8b2a464c 330 goto error_noput;
ec26815a 331error:
dca54a7b 332 afs_unuse_cell(net, cell, afs_cell_trace_unuse_lookup);
8b2a464c 333error_noput:
989782dc 334 _leave(" = %d [error]", ret);
08e0e7c8 335 return ERR_PTR(ret);
ec26815a 336}
1da177e4 337
1da177e4 338/*
08e0e7c8
DH
339 * set the root cell information
340 * - can be called with a module parameter string
341 * - can be called from a write to /proc/fs/afs/rootcell
1da177e4 342 */
989782dc 343int afs_cell_init(struct afs_net *net, const char *rootcell)
1da177e4
LT
344{
345 struct afs_cell *old_root, *new_root;
989782dc
DH
346 const char *cp, *vllist;
347 size_t len;
1da177e4
LT
348
349 _enter("");
350
351 if (!rootcell) {
352 /* module is loaded with no parameters, or built statically.
353 * - in the future we might initialize cell DB here.
354 */
08e0e7c8 355 _leave(" = 0 [no root]");
1da177e4
LT
356 return 0;
357 }
358
359 cp = strchr(rootcell, ':');
989782dc 360 if (!cp) {
07567a55 361 _debug("kAFS: no VL server IP addresses specified");
989782dc
DH
362 vllist = NULL;
363 len = strlen(rootcell);
364 } else {
365 vllist = cp + 1;
366 len = cp - rootcell;
367 }
1da177e4
LT
368
369 /* allocate a cell record for the root cell */
989782dc 370 new_root = afs_lookup_cell(net, rootcell, len, vllist, false);
08e0e7c8
DH
371 if (IS_ERR(new_root)) {
372 _leave(" = %ld", PTR_ERR(new_root));
373 return PTR_ERR(new_root);
1da177e4
LT
374 }
375
17814aef 376 if (!test_and_set_bit(AFS_CELL_FL_NO_GC, &new_root->flags))
dca54a7b 377 afs_use_cell(new_root, afs_cell_trace_use_pin);
989782dc 378
08e0e7c8 379 /* install the new cell */
92e3cc91 380 down_write(&net->cells_lock);
dca54a7b 381 afs_see_cell(new_root, afs_cell_trace_see_ws);
92e3cc91
DH
382 old_root = net->ws_cell;
383 net->ws_cell = new_root;
384 up_write(&net->cells_lock);
1da177e4 385
dca54a7b 386 afs_unuse_cell(net, old_root, afs_cell_trace_unuse_ws);
08e0e7c8
DH
387 _leave(" = 0");
388 return 0;
ec26815a 389}
1da177e4 390
1da177e4 391/*
989782dc 392 * Update a cell's VL server address list from the DNS.
1da177e4 393 */
d5c32c89 394static int afs_update_cell(struct afs_cell *cell)
1da177e4 395{
d5c32c89 396 struct afs_vlserver_list *vllist, *old = NULL, *p;
ded2f4c5
DH
397 unsigned int min_ttl = READ_ONCE(afs_cell_min_ttl);
398 unsigned int max_ttl = READ_ONCE(afs_cell_max_ttl);
399 time64_t now, expiry = 0;
d5c32c89 400 int ret = 0;
1da177e4 401
989782dc
DH
402 _enter("%s", cell->name);
403
0a5143f2 404 vllist = afs_dns_query(cell, &expiry);
d5c32c89
DH
405 if (IS_ERR(vllist)) {
406 ret = PTR_ERR(vllist);
407
408 _debug("%s: fail %d", cell->name, ret);
409 if (ret == -ENOMEM)
410 goto out_wake;
411
412 ret = -ENOMEM;
413 vllist = afs_alloc_vlserver_list(0);
414 if (!vllist)
415 goto out_wake;
416
417 switch (ret) {
418 case -ENODATA:
419 case -EDESTADDRREQ:
420 vllist->status = DNS_LOOKUP_GOT_NOT_FOUND;
421 break;
422 case -EAGAIN:
423 case -ECONNREFUSED:
424 vllist->status = DNS_LOOKUP_GOT_TEMP_FAILURE;
425 break;
426 default:
427 vllist->status = DNS_LOOKUP_GOT_LOCAL_FAILURE;
428 break;
429 }
430 }
431
432 _debug("%s: got list %d %d", cell->name, vllist->source, vllist->status);
433 cell->dns_status = vllist->status;
ded2f4c5
DH
434
435 now = ktime_get_real_seconds();
436 if (min_ttl > max_ttl)
437 max_ttl = min_ttl;
438 if (expiry < now + min_ttl)
439 expiry = now + min_ttl;
440 else if (expiry > now + max_ttl)
441 expiry = now + max_ttl;
442
d5c32c89
DH
443 _debug("%s: status %d", cell->name, vllist->status);
444 if (vllist->source == DNS_RECORD_UNAVAILABLE) {
445 switch (vllist->status) {
446 case DNS_LOOKUP_GOT_NOT_FOUND:
ded2f4c5
DH
447 /* The DNS said that the cell does not exist or there
448 * weren't any addresses to be had.
449 */
ded2f4c5 450 cell->dns_expiry = expiry;
8b2a464c 451 break;
989782dc 452
d5c32c89
DH
453 case DNS_LOOKUP_BAD:
454 case DNS_LOOKUP_GOT_LOCAL_FAILURE:
455 case DNS_LOOKUP_GOT_TEMP_FAILURE:
456 case DNS_LOOKUP_GOT_NS_FAILURE:
8b2a464c 457 default:
ded2f4c5 458 cell->dns_expiry = now + 10;
8b2a464c
DH
459 break;
460 }
8b2a464c 461 } else {
8b2a464c 462 cell->dns_expiry = expiry;
8b2a464c 463 }
bec5eb61 464
d5c32c89
DH
465 /* Replace the VL server list if the new record has servers or the old
466 * record doesn't.
467 */
468 write_lock(&cell->vl_servers_lock);
469 p = rcu_dereference_protected(cell->vl_servers, true);
470 if (vllist->nr_servers > 0 || p->nr_servers == 0) {
471 rcu_assign_pointer(cell->vl_servers, vllist);
472 cell->dns_source = vllist->source;
473 old = p;
474 }
475 write_unlock(&cell->vl_servers_lock);
476 afs_put_vlserverlist(cell->net, old);
bec5eb61 477
d5c32c89
DH
478out_wake:
479 smp_store_release(&cell->dns_lookup_count,
480 cell->dns_lookup_count + 1); /* vs source/status */
481 wake_up_var(&cell->dns_lookup_count);
482 _leave(" = %d", ret);
483 return ret;
ec26815a 484}
1da177e4 485
1da177e4 486/*
989782dc 487 * Destroy a cell record
1da177e4 488 */
989782dc 489static void afs_cell_destroy(struct rcu_head *rcu)
1da177e4 490{
989782dc 491 struct afs_cell *cell = container_of(rcu, struct afs_cell, rcu);
88c853c3
DH
492 struct afs_net *net = cell->net;
493 int u;
1da177e4 494
989782dc 495 _enter("%p{%s}", cell, cell->name);
1da177e4 496
88c853c3
DH
497 u = atomic_read(&cell->ref);
498 ASSERTCMP(u, ==, 0);
dca54a7b 499 trace_afs_cell(cell->debug_id, u, atomic_read(&cell->active), afs_cell_trace_free);
989782dc 500
88c853c3 501 afs_put_vlserverlist(net, rcu_access_pointer(cell->vl_servers));
dca54a7b 502 afs_unuse_cell(net, cell->alias_of, afs_cell_trace_unuse_alias);
989782dc 503 key_put(cell->anonymous_key);
719fdd32 504 kfree(cell->name);
989782dc
DH
505 kfree(cell);
506
88c853c3 507 afs_dec_cells_outstanding(net);
989782dc 508 _leave(" [destroyed]");
ec26815a 509}
1da177e4 510
1da177e4 511/*
989782dc 512 * Queue the cell manager.
1da177e4 513 */
989782dc 514static void afs_queue_cell_manager(struct afs_net *net)
1da177e4 515{
989782dc 516 int outstanding = atomic_inc_return(&net->cells_outstanding);
1da177e4 517
989782dc 518 _enter("%d", outstanding);
1da177e4 519
989782dc
DH
520 if (!queue_work(afs_wq, &net->cells_manager))
521 afs_dec_cells_outstanding(net);
522}
523
524/*
525 * Cell management timer. We have an increment on cells_outstanding that we
526 * need to pass along to the work item.
527 */
528void afs_cells_timer(struct timer_list *timer)
529{
530 struct afs_net *net = container_of(timer, struct afs_net, cells_timer);
531
532 _enter("");
533 if (!queue_work(afs_wq, &net->cells_manager))
534 afs_dec_cells_outstanding(net);
535}
1da177e4 536
8b2a464c
DH
537/*
538 * Get a reference on a cell record.
539 */
dca54a7b 540struct afs_cell *afs_get_cell(struct afs_cell *cell, enum afs_cell_trace reason)
8b2a464c 541{
dca54a7b
DH
542 int u;
543
88c853c3
DH
544 if (atomic_read(&cell->ref) <= 0)
545 BUG();
546
dca54a7b
DH
547 u = atomic_inc_return(&cell->ref);
548 trace_afs_cell(cell->debug_id, u, atomic_read(&cell->active), reason);
8b2a464c
DH
549 return cell;
550}
551
989782dc
DH
552/*
553 * Drop a reference on a cell record.
554 */
dca54a7b 555void afs_put_cell(struct afs_cell *cell, enum afs_cell_trace reason)
88c853c3
DH
556{
557 if (cell) {
dca54a7b 558 unsigned int debug_id = cell->debug_id;
88c853c3
DH
559 unsigned int u, a;
560
dca54a7b 561 a = atomic_read(&cell->active);
88c853c3 562 u = atomic_dec_return(&cell->ref);
dca54a7b 563 trace_afs_cell(debug_id, u, a, reason);
88c853c3
DH
564 if (u == 0) {
565 a = atomic_read(&cell->active);
566 WARN(a != 0, "Cell active count %u > 0\n", a);
567 call_rcu(&cell->rcu, afs_cell_destroy);
568 }
569 }
570}
571
572/*
573 * Note a cell becoming more active.
574 */
dca54a7b 575struct afs_cell *afs_use_cell(struct afs_cell *cell, enum afs_cell_trace reason)
88c853c3 576{
dca54a7b
DH
577 int u, a;
578
88c853c3
DH
579 if (atomic_read(&cell->ref) <= 0)
580 BUG();
581
dca54a7b
DH
582 u = atomic_read(&cell->ref);
583 a = atomic_inc_return(&cell->active);
584 trace_afs_cell(cell->debug_id, u, a, reason);
88c853c3
DH
585 return cell;
586}
587
588/*
589 * Record a cell becoming less active. When the active counter reaches 1, it
590 * is scheduled for destruction, but may get reactivated.
591 */
dca54a7b 592void afs_unuse_cell(struct afs_net *net, struct afs_cell *cell, enum afs_cell_trace reason)
989782dc 593{
acc080d1 594 unsigned int debug_id;
989782dc 595 time64_t now, expire_delay;
dca54a7b 596 int u, a;
1da177e4 597
989782dc 598 if (!cell)
1da177e4 599 return;
1da177e4 600
989782dc 601 _enter("%s", cell->name);
08e0e7c8 602
989782dc
DH
603 now = ktime_get_real_seconds();
604 cell->last_inactive = now;
605 expire_delay = 0;
d5c32c89 606 if (cell->vl_servers->nr_servers)
989782dc 607 expire_delay = afs_cell_gc_delay;
1da177e4 608
acc080d1 609 debug_id = cell->debug_id;
dca54a7b 610 u = atomic_read(&cell->ref);
88c853c3 611 a = atomic_dec_return(&cell->active);
dca54a7b 612 trace_afs_cell(debug_id, u, a, reason);
88c853c3
DH
613 WARN_ON(a == 0);
614 if (a == 1)
615 /* 'cell' may now be garbage collected. */
616 afs_set_cell_timer(net, expire_delay);
617}
1da177e4 618
dca54a7b
DH
619/*
620 * Note that a cell has been seen.
621 */
622void afs_see_cell(struct afs_cell *cell, enum afs_cell_trace reason)
623{
624 int u, a;
625
626 u = atomic_read(&cell->ref);
627 a = atomic_read(&cell->active);
628 trace_afs_cell(cell->debug_id, u, a, reason);
629}
630
88c853c3
DH
631/*
632 * Queue a cell for management, giving the workqueue a ref to hold.
633 */
dca54a7b 634void afs_queue_cell(struct afs_cell *cell, enum afs_cell_trace reason)
88c853c3 635{
dca54a7b 636 afs_get_cell(cell, reason);
88c853c3 637 if (!queue_work(afs_wq, &cell->manager))
dca54a7b 638 afs_put_cell(cell, afs_cell_trace_put_queue_fail);
ec26815a 639}
1da177e4 640
1da177e4 641/*
989782dc 642 * Allocate a key to use as a placeholder for anonymous user security.
1da177e4 643 */
989782dc 644static int afs_alloc_anon_key(struct afs_cell *cell)
1da177e4 645{
989782dc
DH
646 struct key *key;
647 char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp;
1da177e4 648
989782dc
DH
649 /* Create a key to represent an anonymous user. */
650 memcpy(keyname, "afs@", 4);
651 dp = keyname + 4;
652 cp = cell->name;
653 do {
654 *dp++ = tolower(*cp);
655 } while (*cp++);
1da177e4 656
989782dc
DH
657 key = rxrpc_get_null_key(keyname);
658 if (IS_ERR(key))
659 return PTR_ERR(key);
1da177e4 660
989782dc 661 cell->anonymous_key = key;
1da177e4 662
989782dc
DH
663 _debug("anon key %p{%x}",
664 cell->anonymous_key, key_serial(cell->anonymous_key));
665 return 0;
666}
1da177e4 667
989782dc
DH
668/*
669 * Activate a cell.
670 */
671static int afs_activate_cell(struct afs_net *net, struct afs_cell *cell)
672{
6b3944e4
DH
673 struct hlist_node **p;
674 struct afs_cell *pcell;
989782dc
DH
675 int ret;
676
677 if (!cell->anonymous_key) {
678 ret = afs_alloc_anon_key(cell);
679 if (ret < 0)
680 return ret;
08e0e7c8
DH
681 }
682
989782dc
DH
683#ifdef CONFIG_AFS_FSCACHE
684 cell->cache = fscache_acquire_cookie(afs_cache_netfs.primary_index,
685 &afs_cell_cache_index_def,
402cb8dd
DH
686 cell->name, strlen(cell->name),
687 NULL, 0,
ee1235a9 688 cell, 0, true);
989782dc 689#endif
5b86d4ff 690 ret = afs_proc_cell_setup(cell);
989782dc
DH
691 if (ret < 0)
692 return ret;
0da0b7fd
DH
693
694 mutex_lock(&net->proc_cells_lock);
6b3944e4
DH
695 for (p = &net->proc_cells.first; *p; p = &(*p)->next) {
696 pcell = hlist_entry(*p, struct afs_cell, proc_link);
697 if (strcmp(cell->name, pcell->name) < 0)
698 break;
699 }
700
701 cell->proc_link.pprev = p;
702 cell->proc_link.next = *p;
703 rcu_assign_pointer(*p, &cell->proc_link.next);
704 if (cell->proc_link.next)
705 cell->proc_link.next->pprev = &cell->proc_link.next;
706
0da0b7fd
DH
707 afs_dynroot_mkdir(net, cell);
708 mutex_unlock(&net->proc_cells_lock);
989782dc
DH
709 return 0;
710}
711
712/*
713 * Deactivate a cell.
714 */
715static void afs_deactivate_cell(struct afs_net *net, struct afs_cell *cell)
716{
717 _enter("%s", cell->name);
1da177e4 718
5b86d4ff 719 afs_proc_cell_remove(cell);
1da177e4 720
0da0b7fd 721 mutex_lock(&net->proc_cells_lock);
6b3944e4 722 hlist_del_rcu(&cell->proc_link);
0da0b7fd
DH
723 afs_dynroot_rmdir(net, cell);
724 mutex_unlock(&net->proc_cells_lock);
1da177e4 725
9b3f26c9 726#ifdef CONFIG_AFS_FSCACHE
402cb8dd 727 fscache_relinquish_cookie(cell->cache, NULL, false);
989782dc 728 cell->cache = NULL;
1da177e4 729#endif
1da177e4 730
989782dc 731 _leave("");
ec26815a 732}
1da177e4 733
1da177e4 734/*
989782dc
DH
735 * Manage a cell record, initialising and destroying it, maintaining its DNS
736 * records.
1da177e4 737 */
88c853c3 738static void afs_manage_cell(struct afs_cell *cell)
1da177e4 739{
989782dc 740 struct afs_net *net = cell->net;
88c853c3 741 int ret, active;
989782dc
DH
742
743 _enter("%s", cell->name);
744
745again:
746 _debug("state %u", cell->state);
747 switch (cell->state) {
748 case AFS_CELL_INACTIVE:
749 case AFS_CELL_FAILED:
92e3cc91 750 down_write(&net->cells_lock);
88c853c3 751 active = 1;
1d0e850a 752 if (atomic_try_cmpxchg_relaxed(&cell->active, &active, 0)) {
989782dc 753 rb_erase(&cell->net_node, &net->cells);
dca54a7b
DH
754 trace_afs_cell(cell->debug_id, atomic_read(&cell->ref), 0,
755 afs_cell_trace_unuse_delete);
1d0e850a 756 smp_store_release(&cell->state, AFS_CELL_REMOVED);
88c853c3 757 }
92e3cc91 758 up_write(&net->cells_lock);
1d0e850a
DH
759 if (cell->state == AFS_CELL_REMOVED) {
760 wake_up_var(&cell->state);
989782dc 761 goto final_destruction;
1d0e850a 762 }
989782dc
DH
763 if (cell->state == AFS_CELL_FAILED)
764 goto done;
d5c32c89
DH
765 smp_store_release(&cell->state, AFS_CELL_UNSET);
766 wake_up_var(&cell->state);
989782dc
DH
767 goto again;
768
769 case AFS_CELL_UNSET:
d5c32c89
DH
770 smp_store_release(&cell->state, AFS_CELL_ACTIVATING);
771 wake_up_var(&cell->state);
989782dc
DH
772 goto again;
773
774 case AFS_CELL_ACTIVATING:
775 ret = afs_activate_cell(net, cell);
776 if (ret < 0)
777 goto activation_failed;
778
d5c32c89
DH
779 smp_store_release(&cell->state, AFS_CELL_ACTIVE);
780 wake_up_var(&cell->state);
989782dc
DH
781 goto again;
782
783 case AFS_CELL_ACTIVE:
88c853c3 784 if (atomic_read(&cell->active) > 1) {
d5c32c89
DH
785 if (test_and_clear_bit(AFS_CELL_FL_DO_LOOKUP, &cell->flags)) {
786 ret = afs_update_cell(cell);
787 if (ret < 0)
788 cell->error = ret;
789 }
989782dc
DH
790 goto done;
791 }
d5c32c89
DH
792 smp_store_release(&cell->state, AFS_CELL_DEACTIVATING);
793 wake_up_var(&cell->state);
989782dc
DH
794 goto again;
795
796 case AFS_CELL_DEACTIVATING:
88c853c3 797 if (atomic_read(&cell->active) > 1)
989782dc
DH
798 goto reverse_deactivation;
799 afs_deactivate_cell(net, cell);
d5c32c89
DH
800 smp_store_release(&cell->state, AFS_CELL_INACTIVE);
801 wake_up_var(&cell->state);
989782dc
DH
802 goto again;
803
1d0e850a
DH
804 case AFS_CELL_REMOVED:
805 goto done;
806
989782dc
DH
807 default:
808 break;
809 }
810 _debug("bad state %u", cell->state);
811 BUG(); /* Unhandled state */
812
813activation_failed:
814 cell->error = ret;
815 afs_deactivate_cell(net, cell);
816
d5c32c89
DH
817 smp_store_release(&cell->state, AFS_CELL_FAILED); /* vs error */
818 wake_up_var(&cell->state);
989782dc
DH
819 goto again;
820
821reverse_deactivation:
d5c32c89
DH
822 smp_store_release(&cell->state, AFS_CELL_ACTIVE);
823 wake_up_var(&cell->state);
989782dc
DH
824 _leave(" [deact->act]");
825 return;
826
827done:
828 _leave(" [done %u]", cell->state);
829 return;
830
831final_destruction:
88c853c3
DH
832 /* The root volume is pinning the cell */
833 afs_put_volume(cell->net, cell->root_volume, afs_volume_trace_put_cell_root);
834 cell->root_volume = NULL;
dca54a7b 835 afs_put_cell(cell, afs_cell_trace_put_destroy);
88c853c3
DH
836}
837
838static void afs_manage_cell_work(struct work_struct *work)
839{
840 struct afs_cell *cell = container_of(work, struct afs_cell, manager);
841
842 afs_manage_cell(cell);
dca54a7b 843 afs_put_cell(cell, afs_cell_trace_put_queue_work);
989782dc
DH
844}
845
846/*
847 * Manage the records of cells known to a network namespace. This includes
848 * updating the DNS records and garbage collecting unused cells that were
849 * automatically added.
850 *
851 * Note that constructed cell records may only be removed from net->cells by
852 * this work item, so it is safe for this work item to stash a cursor pointing
853 * into the tree and then return to caller (provided it skips cells that are
854 * still under construction).
855 *
856 * Note also that we were given an increment on net->cells_outstanding by
857 * whoever queued us that we need to deal with before returning.
858 */
859void afs_manage_cells(struct work_struct *work)
860{
861 struct afs_net *net = container_of(work, struct afs_net, cells_manager);
862 struct rb_node *cursor;
863 time64_t now = ktime_get_real_seconds(), next_manage = TIME64_MAX;
864 bool purging = !net->live;
1da177e4
LT
865
866 _enter("");
867
989782dc
DH
868 /* Trawl the cell database looking for cells that have expired from
869 * lack of use and cells whose DNS results have expired and dispatch
870 * their managers.
871 */
92e3cc91 872 down_read(&net->cells_lock);
1da177e4 873
989782dc
DH
874 for (cursor = rb_first(&net->cells); cursor; cursor = rb_next(cursor)) {
875 struct afs_cell *cell =
876 rb_entry(cursor, struct afs_cell, net_node);
88c853c3 877 unsigned active;
989782dc 878 bool sched_cell = false;
08e0e7c8 879
88c853c3 880 active = atomic_read(&cell->active);
dca54a7b
DH
881 trace_afs_cell(cell->debug_id, atomic_read(&cell->ref),
882 active, afs_cell_trace_manage);
989782dc 883
88c853c3 884 ASSERTCMP(active, >=, 1);
989782dc
DH
885
886 if (purging) {
dca54a7b
DH
887 if (test_and_clear_bit(AFS_CELL_FL_NO_GC, &cell->flags)) {
888 active = atomic_dec_return(&cell->active);
889 trace_afs_cell(cell->debug_id, atomic_read(&cell->ref),
890 active, afs_cell_trace_unuse_pin);
891 }
989782dc 892 }
1da177e4 893
88c853c3 894 if (active == 1) {
d5c32c89 895 struct afs_vlserver_list *vllist;
989782dc 896 time64_t expire_at = cell->last_inactive;
1da177e4 897
d5c32c89
DH
898 read_lock(&cell->vl_servers_lock);
899 vllist = rcu_dereference_protected(
900 cell->vl_servers,
901 lockdep_is_held(&cell->vl_servers_lock));
902 if (vllist->nr_servers > 0)
989782dc 903 expire_at += afs_cell_gc_delay;
d5c32c89 904 read_unlock(&cell->vl_servers_lock);
989782dc
DH
905 if (purging || expire_at <= now)
906 sched_cell = true;
907 else if (expire_at < next_manage)
908 next_manage = expire_at;
1da177e4
LT
909 }
910
989782dc 911 if (!purging) {
d5c32c89 912 if (test_bit(AFS_CELL_FL_DO_LOOKUP, &cell->flags))
989782dc 913 sched_cell = true;
989782dc
DH
914 }
915
916 if (sched_cell)
dca54a7b 917 afs_queue_cell(cell, afs_cell_trace_get_queue_manage);
989782dc
DH
918 }
919
92e3cc91 920 up_read(&net->cells_lock);
1da177e4 921
989782dc
DH
922 /* Update the timer on the way out. We have to pass an increment on
923 * cells_outstanding in the namespace that we are in to the timer or
924 * the work scheduler.
925 */
926 if (!purging && next_manage < TIME64_MAX) {
927 now = ktime_get_real_seconds();
1da177e4 928
989782dc
DH
929 if (next_manage - now <= 0) {
930 if (queue_work(afs_wq, &net->cells_manager))
931 atomic_inc(&net->cells_outstanding);
932 } else {
933 afs_set_cell_timer(net, next_manage - now);
1da177e4
LT
934 }
935 }
936
989782dc
DH
937 afs_dec_cells_outstanding(net);
938 _leave(" [%d]", atomic_read(&net->cells_outstanding));
939}
940
941/*
942 * Purge in-memory cell database.
943 */
944void afs_cell_purge(struct afs_net *net)
945{
946 struct afs_cell *ws;
947
948 _enter("");
949
92e3cc91
DH
950 down_write(&net->cells_lock);
951 ws = net->ws_cell;
952 net->ws_cell = NULL;
953 up_write(&net->cells_lock);
dca54a7b 954 afs_unuse_cell(net, ws, afs_cell_trace_unuse_ws);
989782dc
DH
955
956 _debug("del timer");
957 if (del_timer_sync(&net->cells_timer))
958 atomic_dec(&net->cells_outstanding);
959
960 _debug("kick mgr");
961 afs_queue_cell_manager(net);
962
963 _debug("wait");
ab1fbe32
PZ
964 wait_var_event(&net->cells_outstanding,
965 !atomic_read(&net->cells_outstanding));
1da177e4 966 _leave("");
ec26815a 967}