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