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