]>
Commit | Line | Data |
---|---|---|
ec26815a | 1 | /* AFS cell and server record management |
1da177e4 | 2 | * |
989782dc | 3 | * Copyright (C) 2002, 2017 Red Hat, Inc. All Rights Reserved. |
1da177e4 LT |
4 | * Written by David Howells ([email protected]) |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * as published by the Free Software Foundation; either version | |
9 | * 2 of the License, or (at your option) any later version. | |
10 | */ | |
11 | ||
1da177e4 | 12 | #include <linux/slab.h> |
00d3b7a4 DH |
13 | #include <linux/key.h> |
14 | #include <linux/ctype.h> | |
07567a55 | 15 | #include <linux/dns_resolver.h> |
e8edc6e0 | 16 | #include <linux/sched.h> |
3838d3ec | 17 | #include <linux/inet.h> |
0da0b7fd | 18 | #include <linux/namei.h> |
00d3b7a4 | 19 | #include <keys/rxrpc-type.h> |
1da177e4 LT |
20 | #include "internal.h" |
21 | ||
fe342cf7 | 22 | static unsigned __read_mostly afs_cell_gc_delay = 10; |
ded2f4c5 DH |
23 | static unsigned __read_mostly afs_cell_min_ttl = 10 * 60; |
24 | static unsigned __read_mostly afs_cell_max_ttl = 24 * 60 * 60; | |
989782dc DH |
25 | |
26 | static void afs_manage_cell(struct work_struct *); | |
27 | ||
28 | static void afs_dec_cells_outstanding(struct afs_net *net) | |
29 | { | |
30 | if (atomic_dec_and_test(&net->cells_outstanding)) | |
ab1fbe32 | 31 | wake_up_var(&net->cells_outstanding); |
989782dc DH |
32 | } |
33 | ||
1da177e4 | 34 | /* |
989782dc DH |
35 | * Set the cell timer to fire after a given delay, assuming it's not already |
36 | * set for an earlier time. | |
1da177e4 | 37 | */ |
989782dc | 38 | static void afs_set_cell_timer(struct afs_net *net, time64_t delay) |
1da177e4 | 39 | { |
989782dc DH |
40 | if (net->live) { |
41 | atomic_inc(&net->cells_outstanding); | |
42 | if (timer_reduce(&net->cells_timer, jiffies + delay * HZ)) | |
43 | afs_dec_cells_outstanding(net); | |
44 | } | |
45 | } | |
46 | ||
47 | /* | |
48 | * Look up and get an activation reference on a cell record under RCU | |
49 | * conditions. The caller must hold the RCU read lock. | |
50 | */ | |
51 | struct afs_cell *afs_lookup_cell_rcu(struct afs_net *net, | |
52 | const char *name, unsigned int namesz) | |
53 | { | |
54 | struct afs_cell *cell = NULL; | |
55 | struct rb_node *p; | |
56 | int n, seq = 0, ret = 0; | |
57 | ||
58 | _enter("%*.*s", namesz, namesz, name); | |
59 | ||
60 | if (name && namesz == 0) | |
61 | return ERR_PTR(-EINVAL); | |
62 | if (namesz > AFS_MAXCELLNAME) | |
63 | return ERR_PTR(-ENAMETOOLONG); | |
64 | ||
65 | do { | |
66 | /* Unfortunately, rbtree walking doesn't give reliable results | |
67 | * under just the RCU read lock, so we have to check for | |
68 | * changes. | |
69 | */ | |
70 | if (cell) | |
71 | afs_put_cell(net, cell); | |
72 | cell = NULL; | |
73 | ret = -ENOENT; | |
74 | ||
75 | read_seqbegin_or_lock(&net->cells_lock, &seq); | |
76 | ||
77 | if (!name) { | |
78 | cell = rcu_dereference_raw(net->ws_cell); | |
79 | if (cell) { | |
80 | afs_get_cell(cell); | |
fe342cf7 | 81 | break; |
989782dc DH |
82 | } |
83 | ret = -EDESTADDRREQ; | |
84 | continue; | |
85 | } | |
86 | ||
87 | p = rcu_dereference_raw(net->cells.rb_node); | |
88 | while (p) { | |
89 | cell = rb_entry(p, struct afs_cell, net_node); | |
90 | ||
91 | n = strncasecmp(cell->name, name, | |
92 | min_t(size_t, cell->name_len, namesz)); | |
93 | if (n == 0) | |
94 | n = cell->name_len - namesz; | |
95 | if (n < 0) { | |
96 | p = rcu_dereference_raw(p->rb_left); | |
97 | } else if (n > 0) { | |
98 | p = rcu_dereference_raw(p->rb_right); | |
99 | } else { | |
100 | if (atomic_inc_not_zero(&cell->usage)) { | |
101 | ret = 0; | |
102 | break; | |
103 | } | |
104 | /* We want to repeat the search, this time with | |
105 | * the lock properly locked. | |
106 | */ | |
107 | } | |
108 | cell = NULL; | |
109 | } | |
1da177e4 | 110 | |
989782dc | 111 | } while (need_seqretry(&net->cells_lock, seq)); |
1da177e4 | 112 | |
989782dc | 113 | done_seqretry(&net->cells_lock, seq); |
1da177e4 | 114 | |
989782dc DH |
115 | return ret == 0 ? cell : ERR_PTR(ret); |
116 | } | |
117 | ||
118 | /* | |
119 | * Set up a cell record and fill in its name, VL server address list and | |
120 | * allocate an anonymous key | |
121 | */ | |
122 | static struct afs_cell *afs_alloc_cell(struct afs_net *net, | |
123 | const char *name, unsigned int namelen, | |
0a5143f2 | 124 | const char *addresses) |
989782dc | 125 | { |
ca1cbbdc | 126 | struct afs_vlserver_list *vllist; |
989782dc DH |
127 | struct afs_cell *cell; |
128 | int i, ret; | |
129 | ||
130 | ASSERT(name); | |
131 | if (namelen == 0) | |
132 | return ERR_PTR(-EINVAL); | |
07567a55 WL |
133 | if (namelen > AFS_MAXCELLNAME) { |
134 | _leave(" = -ENAMETOOLONG"); | |
00d3b7a4 | 135 | return ERR_PTR(-ENAMETOOLONG); |
07567a55 | 136 | } |
37ab6368 DH |
137 | if (namelen == 5 && memcmp(name, "@cell", 5) == 0) |
138 | return ERR_PTR(-EINVAL); | |
00d3b7a4 | 139 | |
0a5143f2 | 140 | _enter("%*.*s,%s", namelen, namelen, name, addresses); |
989782dc DH |
141 | |
142 | cell = kzalloc(sizeof(struct afs_cell), GFP_KERNEL); | |
1da177e4 LT |
143 | if (!cell) { |
144 | _leave(" = -ENOMEM"); | |
08e0e7c8 | 145 | return ERR_PTR(-ENOMEM); |
1da177e4 LT |
146 | } |
147 | ||
f044c884 | 148 | cell->net = net; |
989782dc DH |
149 | cell->name_len = namelen; |
150 | for (i = 0; i < namelen; i++) | |
151 | cell->name[i] = tolower(name[i]); | |
152 | ||
153 | atomic_set(&cell->usage, 2); | |
154 | INIT_WORK(&cell->manager, afs_manage_cell); | |
8b2a464c DH |
155 | cell->flags = ((1 << AFS_CELL_FL_NOT_READY) | |
156 | (1 << AFS_CELL_FL_NO_LOOKUP_YET)); | |
d2ddc776 DH |
157 | INIT_LIST_HEAD(&cell->proc_volumes); |
158 | rwlock_init(&cell->proc_lock); | |
0a5143f2 | 159 | rwlock_init(&cell->vl_servers_lock); |
4d9df986 | 160 | |
ca1cbbdc DH |
161 | /* Provide a VL server list, filling it in if we were given a list of |
162 | * addresses to use. | |
989782dc | 163 | */ |
0a5143f2 | 164 | if (addresses) { |
0a5143f2 DH |
165 | vllist = afs_parse_text_addrs(net, |
166 | addresses, strlen(addresses), ':', | |
167 | VL_SERVICE, AFS_VL_PORT); | |
168 | if (IS_ERR(vllist)) { | |
169 | ret = PTR_ERR(vllist); | |
8b2a464c DH |
170 | goto parse_failed; |
171 | } | |
00d3b7a4 | 172 | |
989782dc | 173 | cell->dns_expiry = TIME64_MAX; |
ded2f4c5 | 174 | } else { |
ca1cbbdc DH |
175 | ret = -ENOMEM; |
176 | vllist = afs_alloc_vlserver_list(0); | |
177 | if (!vllist) | |
178 | goto error; | |
ded2f4c5 | 179 | cell->dns_expiry = ktime_get_real_seconds(); |
00d3b7a4 | 180 | } |
00d3b7a4 | 181 | |
ca1cbbdc DH |
182 | rcu_assign_pointer(cell->vl_servers, vllist); |
183 | ||
00d3b7a4 DH |
184 | _leave(" = %p", cell); |
185 | return cell; | |
186 | ||
8b2a464c DH |
187 | parse_failed: |
188 | if (ret == -EINVAL) | |
189 | printk(KERN_ERR "kAFS: bad VL server IP address\n"); | |
ca1cbbdc | 190 | error: |
00d3b7a4 DH |
191 | kfree(cell); |
192 | _leave(" = %d", ret); | |
193 | return ERR_PTR(ret); | |
194 | } | |
1da177e4 | 195 | |
00d3b7a4 | 196 | /* |
989782dc | 197 | * afs_lookup_cell - Look up or create a cell record. |
f044c884 | 198 | * @net: The network namespace |
989782dc DH |
199 | * @name: The name of the cell. |
200 | * @namesz: The strlen of the cell name. | |
201 | * @vllist: A colon/comma separated list of numeric IP addresses or NULL. | |
202 | * @excl: T if an error should be given if the cell name already exists. | |
203 | * | |
204 | * Look up a cell record by name and query the DNS for VL server addresses if | |
205 | * needed. Note that that actual DNS query is punted off to the manager thread | |
206 | * so that this function can return immediately if interrupted whilst allowing | |
207 | * cell records to be shared even if not yet fully constructed. | |
00d3b7a4 | 208 | */ |
989782dc DH |
209 | struct afs_cell *afs_lookup_cell(struct afs_net *net, |
210 | const char *name, unsigned int namesz, | |
211 | const char *vllist, bool excl) | |
00d3b7a4 | 212 | { |
989782dc DH |
213 | struct afs_cell *cell, *candidate, *cursor; |
214 | struct rb_node *parent, **pp; | |
215 | int ret, n; | |
216 | ||
217 | _enter("%s,%s", name, vllist); | |
218 | ||
219 | if (!excl) { | |
220 | rcu_read_lock(); | |
221 | cell = afs_lookup_cell_rcu(net, name, namesz); | |
222 | rcu_read_unlock(); | |
68327951 | 223 | if (!IS_ERR(cell)) |
989782dc | 224 | goto wait_for_cell; |
989782dc | 225 | } |
00d3b7a4 | 226 | |
989782dc DH |
227 | /* Assume we're probably going to create a cell and preallocate and |
228 | * mostly set up a candidate record. We can then use this to stash the | |
229 | * name, the net namespace and VL server addresses. | |
230 | * | |
231 | * We also want to do this before we hold any locks as it may involve | |
232 | * upcalling to userspace to make DNS queries. | |
233 | */ | |
234 | candidate = afs_alloc_cell(net, name, namesz, vllist); | |
235 | if (IS_ERR(candidate)) { | |
236 | _leave(" = %ld", PTR_ERR(candidate)); | |
237 | return candidate; | |
5214b729 | 238 | } |
5214b729 | 239 | |
989782dc DH |
240 | /* Find the insertion point and check to see if someone else added a |
241 | * cell whilst we were allocating. | |
242 | */ | |
243 | write_seqlock(&net->cells_lock); | |
244 | ||
245 | pp = &net->cells.rb_node; | |
246 | parent = NULL; | |
247 | while (*pp) { | |
248 | parent = *pp; | |
249 | cursor = rb_entry(parent, struct afs_cell, net_node); | |
250 | ||
251 | n = strncasecmp(cursor->name, name, | |
252 | min_t(size_t, cursor->name_len, namesz)); | |
253 | if (n == 0) | |
254 | n = cursor->name_len - namesz; | |
255 | if (n < 0) | |
256 | pp = &(*pp)->rb_left; | |
257 | else if (n > 0) | |
258 | pp = &(*pp)->rb_right; | |
259 | else | |
260 | goto cell_already_exists; | |
00d3b7a4 DH |
261 | } |
262 | ||
989782dc DH |
263 | cell = candidate; |
264 | candidate = NULL; | |
265 | rb_link_node_rcu(&cell->net_node, parent, pp); | |
266 | rb_insert_color(&cell->net_node, &net->cells); | |
267 | atomic_inc(&net->cells_outstanding); | |
268 | write_sequnlock(&net->cells_lock); | |
1da177e4 | 269 | |
989782dc | 270 | queue_work(afs_wq, &cell->manager); |
1da177e4 | 271 | |
989782dc DH |
272 | wait_for_cell: |
273 | _debug("wait_for_cell"); | |
274 | ret = wait_on_bit(&cell->flags, AFS_CELL_FL_NOT_READY, TASK_INTERRUPTIBLE); | |
275 | smp_rmb(); | |
1da177e4 | 276 | |
989782dc DH |
277 | switch (READ_ONCE(cell->state)) { |
278 | case AFS_CELL_FAILED: | |
279 | ret = cell->error; | |
280 | goto error; | |
281 | default: | |
282 | _debug("weird %u %d", cell->state, cell->error); | |
283 | goto error; | |
284 | case AFS_CELL_ACTIVE: | |
285 | break; | |
286 | } | |
1da177e4 | 287 | |
989782dc | 288 | _leave(" = %p [cell]", cell); |
08e0e7c8 | 289 | return cell; |
1da177e4 | 290 | |
989782dc DH |
291 | cell_already_exists: |
292 | _debug("cell exists"); | |
293 | cell = cursor; | |
294 | if (excl) { | |
295 | ret = -EEXIST; | |
296 | } else { | |
989782dc DH |
297 | afs_get_cell(cursor); |
298 | ret = 0; | |
299 | } | |
300 | write_sequnlock(&net->cells_lock); | |
301 | kfree(candidate); | |
302 | if (ret == 0) | |
303 | goto wait_for_cell; | |
8b2a464c | 304 | goto error_noput; |
ec26815a | 305 | error: |
989782dc | 306 | afs_put_cell(net, cell); |
8b2a464c | 307 | error_noput: |
989782dc | 308 | _leave(" = %d [error]", ret); |
08e0e7c8 | 309 | return ERR_PTR(ret); |
ec26815a | 310 | } |
1da177e4 | 311 | |
1da177e4 | 312 | /* |
08e0e7c8 DH |
313 | * set the root cell information |
314 | * - can be called with a module parameter string | |
315 | * - can be called from a write to /proc/fs/afs/rootcell | |
1da177e4 | 316 | */ |
989782dc | 317 | int afs_cell_init(struct afs_net *net, const char *rootcell) |
1da177e4 LT |
318 | { |
319 | struct afs_cell *old_root, *new_root; | |
989782dc DH |
320 | const char *cp, *vllist; |
321 | size_t len; | |
1da177e4 LT |
322 | |
323 | _enter(""); | |
324 | ||
325 | if (!rootcell) { | |
326 | /* module is loaded with no parameters, or built statically. | |
327 | * - in the future we might initialize cell DB here. | |
328 | */ | |
08e0e7c8 | 329 | _leave(" = 0 [no root]"); |
1da177e4 LT |
330 | return 0; |
331 | } | |
332 | ||
333 | cp = strchr(rootcell, ':'); | |
989782dc | 334 | if (!cp) { |
07567a55 | 335 | _debug("kAFS: no VL server IP addresses specified"); |
989782dc DH |
336 | vllist = NULL; |
337 | len = strlen(rootcell); | |
338 | } else { | |
339 | vllist = cp + 1; | |
340 | len = cp - rootcell; | |
341 | } | |
1da177e4 LT |
342 | |
343 | /* allocate a cell record for the root cell */ | |
989782dc | 344 | new_root = afs_lookup_cell(net, rootcell, len, vllist, false); |
08e0e7c8 DH |
345 | if (IS_ERR(new_root)) { |
346 | _leave(" = %ld", PTR_ERR(new_root)); | |
347 | return PTR_ERR(new_root); | |
1da177e4 LT |
348 | } |
349 | ||
17814aef DH |
350 | if (!test_and_set_bit(AFS_CELL_FL_NO_GC, &new_root->flags)) |
351 | afs_get_cell(new_root); | |
989782dc | 352 | |
08e0e7c8 | 353 | /* install the new cell */ |
989782dc | 354 | write_seqlock(&net->cells_lock); |
1588def9 DH |
355 | old_root = rcu_access_pointer(net->ws_cell); |
356 | rcu_assign_pointer(net->ws_cell, new_root); | |
989782dc | 357 | write_sequnlock(&net->cells_lock); |
1da177e4 | 358 | |
989782dc | 359 | afs_put_cell(net, old_root); |
08e0e7c8 DH |
360 | _leave(" = 0"); |
361 | return 0; | |
ec26815a | 362 | } |
1da177e4 | 363 | |
1da177e4 | 364 | /* |
989782dc | 365 | * Update a cell's VL server address list from the DNS. |
1da177e4 | 366 | */ |
989782dc | 367 | static void afs_update_cell(struct afs_cell *cell) |
1da177e4 | 368 | { |
0a5143f2 | 369 | struct afs_vlserver_list *vllist, *old; |
ded2f4c5 DH |
370 | unsigned int min_ttl = READ_ONCE(afs_cell_min_ttl); |
371 | unsigned int max_ttl = READ_ONCE(afs_cell_max_ttl); | |
372 | time64_t now, expiry = 0; | |
1da177e4 | 373 | |
989782dc DH |
374 | _enter("%s", cell->name); |
375 | ||
0a5143f2 | 376 | vllist = afs_dns_query(cell, &expiry); |
ded2f4c5 DH |
377 | |
378 | now = ktime_get_real_seconds(); | |
379 | if (min_ttl > max_ttl) | |
380 | max_ttl = min_ttl; | |
381 | if (expiry < now + min_ttl) | |
382 | expiry = now + min_ttl; | |
383 | else if (expiry > now + max_ttl) | |
384 | expiry = now + max_ttl; | |
385 | ||
0a5143f2 DH |
386 | if (IS_ERR(vllist)) { |
387 | switch (PTR_ERR(vllist)) { | |
8b2a464c | 388 | case -ENODATA: |
ded2f4c5 DH |
389 | case -EDESTADDRREQ: |
390 | /* The DNS said that the cell does not exist or there | |
391 | * weren't any addresses to be had. | |
392 | */ | |
8b2a464c DH |
393 | set_bit(AFS_CELL_FL_NOT_FOUND, &cell->flags); |
394 | clear_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags); | |
ded2f4c5 | 395 | cell->dns_expiry = expiry; |
8b2a464c | 396 | break; |
989782dc | 397 | |
8b2a464c DH |
398 | case -EAGAIN: |
399 | case -ECONNREFUSED: | |
400 | default: | |
401 | set_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags); | |
ded2f4c5 | 402 | cell->dns_expiry = now + 10; |
8b2a464c DH |
403 | break; |
404 | } | |
989782dc | 405 | |
989782dc | 406 | cell->error = -EDESTADDRREQ; |
8b2a464c DH |
407 | } else { |
408 | clear_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags); | |
409 | clear_bit(AFS_CELL_FL_NOT_FOUND, &cell->flags); | |
1da177e4 | 410 | |
6b8812fc | 411 | write_lock(&cell->vl_servers_lock); |
0a5143f2 DH |
412 | old = rcu_dereference_protected(cell->vl_servers, true); |
413 | rcu_assign_pointer(cell->vl_servers, vllist); | |
8b2a464c | 414 | cell->dns_expiry = expiry; |
6b8812fc | 415 | write_unlock(&cell->vl_servers_lock); |
1da177e4 | 416 | |
ca1cbbdc | 417 | afs_put_vlserverlist(cell->net, old); |
8b2a464c | 418 | } |
bec5eb61 | 419 | |
8b2a464c DH |
420 | if (test_and_clear_bit(AFS_CELL_FL_NO_LOOKUP_YET, &cell->flags)) |
421 | wake_up_bit(&cell->flags, AFS_CELL_FL_NO_LOOKUP_YET); | |
bec5eb61 | 422 | |
989782dc | 423 | now = ktime_get_real_seconds(); |
8b2a464c | 424 | afs_set_cell_timer(cell->net, cell->dns_expiry - now); |
989782dc | 425 | _leave(""); |
ec26815a | 426 | } |
1da177e4 | 427 | |
1da177e4 | 428 | /* |
989782dc | 429 | * Destroy a cell record |
1da177e4 | 430 | */ |
989782dc | 431 | static void afs_cell_destroy(struct rcu_head *rcu) |
1da177e4 | 432 | { |
989782dc | 433 | struct afs_cell *cell = container_of(rcu, struct afs_cell, rcu); |
1da177e4 | 434 | |
989782dc | 435 | _enter("%p{%s}", cell, cell->name); |
1da177e4 | 436 | |
989782dc DH |
437 | ASSERTCMP(atomic_read(&cell->usage), ==, 0); |
438 | ||
0a5143f2 | 439 | afs_put_vlserverlist(cell->net, rcu_access_pointer(cell->vl_servers)); |
989782dc DH |
440 | key_put(cell->anonymous_key); |
441 | kfree(cell); | |
442 | ||
443 | _leave(" [destroyed]"); | |
ec26815a | 444 | } |
1da177e4 | 445 | |
1da177e4 | 446 | /* |
989782dc | 447 | * Queue the cell manager. |
1da177e4 | 448 | */ |
989782dc | 449 | static void afs_queue_cell_manager(struct afs_net *net) |
1da177e4 | 450 | { |
989782dc | 451 | int outstanding = atomic_inc_return(&net->cells_outstanding); |
1da177e4 | 452 | |
989782dc | 453 | _enter("%d", outstanding); |
1da177e4 | 454 | |
989782dc DH |
455 | if (!queue_work(afs_wq, &net->cells_manager)) |
456 | afs_dec_cells_outstanding(net); | |
457 | } | |
458 | ||
459 | /* | |
460 | * Cell management timer. We have an increment on cells_outstanding that we | |
461 | * need to pass along to the work item. | |
462 | */ | |
463 | void afs_cells_timer(struct timer_list *timer) | |
464 | { | |
465 | struct afs_net *net = container_of(timer, struct afs_net, cells_timer); | |
466 | ||
467 | _enter(""); | |
468 | if (!queue_work(afs_wq, &net->cells_manager)) | |
469 | afs_dec_cells_outstanding(net); | |
470 | } | |
1da177e4 | 471 | |
8b2a464c DH |
472 | /* |
473 | * Get a reference on a cell record. | |
474 | */ | |
475 | struct afs_cell *afs_get_cell(struct afs_cell *cell) | |
476 | { | |
477 | atomic_inc(&cell->usage); | |
478 | return cell; | |
479 | } | |
480 | ||
989782dc DH |
481 | /* |
482 | * Drop a reference on a cell record. | |
483 | */ | |
484 | void afs_put_cell(struct afs_net *net, struct afs_cell *cell) | |
485 | { | |
486 | time64_t now, expire_delay; | |
1da177e4 | 487 | |
989782dc | 488 | if (!cell) |
1da177e4 | 489 | return; |
1da177e4 | 490 | |
989782dc | 491 | _enter("%s", cell->name); |
08e0e7c8 | 492 | |
989782dc DH |
493 | now = ktime_get_real_seconds(); |
494 | cell->last_inactive = now; | |
495 | expire_delay = 0; | |
496 | if (!test_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags) && | |
497 | !test_bit(AFS_CELL_FL_NOT_FOUND, &cell->flags)) | |
498 | expire_delay = afs_cell_gc_delay; | |
1da177e4 | 499 | |
989782dc DH |
500 | if (atomic_dec_return(&cell->usage) > 1) |
501 | return; | |
1da177e4 | 502 | |
989782dc DH |
503 | /* 'cell' may now be garbage collected. */ |
504 | afs_set_cell_timer(net, expire_delay); | |
ec26815a | 505 | } |
1da177e4 | 506 | |
1da177e4 | 507 | /* |
989782dc | 508 | * Allocate a key to use as a placeholder for anonymous user security. |
1da177e4 | 509 | */ |
989782dc | 510 | static int afs_alloc_anon_key(struct afs_cell *cell) |
1da177e4 | 511 | { |
989782dc DH |
512 | struct key *key; |
513 | char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp; | |
1da177e4 | 514 | |
989782dc DH |
515 | /* Create a key to represent an anonymous user. */ |
516 | memcpy(keyname, "afs@", 4); | |
517 | dp = keyname + 4; | |
518 | cp = cell->name; | |
519 | do { | |
520 | *dp++ = tolower(*cp); | |
521 | } while (*cp++); | |
1da177e4 | 522 | |
989782dc DH |
523 | key = rxrpc_get_null_key(keyname); |
524 | if (IS_ERR(key)) | |
525 | return PTR_ERR(key); | |
1da177e4 | 526 | |
989782dc | 527 | cell->anonymous_key = key; |
1da177e4 | 528 | |
989782dc DH |
529 | _debug("anon key %p{%x}", |
530 | cell->anonymous_key, key_serial(cell->anonymous_key)); | |
531 | return 0; | |
532 | } | |
1da177e4 | 533 | |
989782dc DH |
534 | /* |
535 | * Activate a cell. | |
536 | */ | |
537 | static int afs_activate_cell(struct afs_net *net, struct afs_cell *cell) | |
538 | { | |
6b3944e4 DH |
539 | struct hlist_node **p; |
540 | struct afs_cell *pcell; | |
989782dc DH |
541 | int ret; |
542 | ||
543 | if (!cell->anonymous_key) { | |
544 | ret = afs_alloc_anon_key(cell); | |
545 | if (ret < 0) | |
546 | return ret; | |
08e0e7c8 DH |
547 | } |
548 | ||
989782dc DH |
549 | #ifdef CONFIG_AFS_FSCACHE |
550 | cell->cache = fscache_acquire_cookie(afs_cache_netfs.primary_index, | |
551 | &afs_cell_cache_index_def, | |
402cb8dd DH |
552 | cell->name, strlen(cell->name), |
553 | NULL, 0, | |
ee1235a9 | 554 | cell, 0, true); |
989782dc | 555 | #endif |
5b86d4ff | 556 | ret = afs_proc_cell_setup(cell); |
989782dc DH |
557 | if (ret < 0) |
558 | return ret; | |
0da0b7fd DH |
559 | |
560 | mutex_lock(&net->proc_cells_lock); | |
6b3944e4 DH |
561 | for (p = &net->proc_cells.first; *p; p = &(*p)->next) { |
562 | pcell = hlist_entry(*p, struct afs_cell, proc_link); | |
563 | if (strcmp(cell->name, pcell->name) < 0) | |
564 | break; | |
565 | } | |
566 | ||
567 | cell->proc_link.pprev = p; | |
568 | cell->proc_link.next = *p; | |
569 | rcu_assign_pointer(*p, &cell->proc_link.next); | |
570 | if (cell->proc_link.next) | |
571 | cell->proc_link.next->pprev = &cell->proc_link.next; | |
572 | ||
0da0b7fd DH |
573 | afs_dynroot_mkdir(net, cell); |
574 | mutex_unlock(&net->proc_cells_lock); | |
989782dc DH |
575 | return 0; |
576 | } | |
577 | ||
578 | /* | |
579 | * Deactivate a cell. | |
580 | */ | |
581 | static void afs_deactivate_cell(struct afs_net *net, struct afs_cell *cell) | |
582 | { | |
583 | _enter("%s", cell->name); | |
1da177e4 | 584 | |
5b86d4ff | 585 | afs_proc_cell_remove(cell); |
1da177e4 | 586 | |
0da0b7fd | 587 | mutex_lock(&net->proc_cells_lock); |
6b3944e4 | 588 | hlist_del_rcu(&cell->proc_link); |
0da0b7fd DH |
589 | afs_dynroot_rmdir(net, cell); |
590 | mutex_unlock(&net->proc_cells_lock); | |
1da177e4 | 591 | |
9b3f26c9 | 592 | #ifdef CONFIG_AFS_FSCACHE |
402cb8dd | 593 | fscache_relinquish_cookie(cell->cache, NULL, false); |
989782dc | 594 | cell->cache = NULL; |
1da177e4 | 595 | #endif |
1da177e4 | 596 | |
989782dc | 597 | _leave(""); |
ec26815a | 598 | } |
1da177e4 | 599 | |
1da177e4 | 600 | /* |
989782dc DH |
601 | * Manage a cell record, initialising and destroying it, maintaining its DNS |
602 | * records. | |
1da177e4 | 603 | */ |
989782dc | 604 | static void afs_manage_cell(struct work_struct *work) |
1da177e4 | 605 | { |
989782dc DH |
606 | struct afs_cell *cell = container_of(work, struct afs_cell, manager); |
607 | struct afs_net *net = cell->net; | |
608 | bool deleted; | |
609 | int ret, usage; | |
610 | ||
611 | _enter("%s", cell->name); | |
612 | ||
613 | again: | |
614 | _debug("state %u", cell->state); | |
615 | switch (cell->state) { | |
616 | case AFS_CELL_INACTIVE: | |
617 | case AFS_CELL_FAILED: | |
618 | write_seqlock(&net->cells_lock); | |
619 | usage = 1; | |
620 | deleted = atomic_try_cmpxchg_relaxed(&cell->usage, &usage, 0); | |
621 | if (deleted) | |
622 | rb_erase(&cell->net_node, &net->cells); | |
623 | write_sequnlock(&net->cells_lock); | |
624 | if (deleted) | |
625 | goto final_destruction; | |
626 | if (cell->state == AFS_CELL_FAILED) | |
627 | goto done; | |
628 | cell->state = AFS_CELL_UNSET; | |
629 | goto again; | |
630 | ||
631 | case AFS_CELL_UNSET: | |
632 | cell->state = AFS_CELL_ACTIVATING; | |
633 | goto again; | |
634 | ||
635 | case AFS_CELL_ACTIVATING: | |
636 | ret = afs_activate_cell(net, cell); | |
637 | if (ret < 0) | |
638 | goto activation_failed; | |
639 | ||
640 | cell->state = AFS_CELL_ACTIVE; | |
641 | smp_wmb(); | |
642 | clear_bit(AFS_CELL_FL_NOT_READY, &cell->flags); | |
643 | wake_up_bit(&cell->flags, AFS_CELL_FL_NOT_READY); | |
644 | goto again; | |
645 | ||
646 | case AFS_CELL_ACTIVE: | |
647 | if (atomic_read(&cell->usage) > 1) { | |
648 | time64_t now = ktime_get_real_seconds(); | |
649 | if (cell->dns_expiry <= now && net->live) | |
650 | afs_update_cell(cell); | |
651 | goto done; | |
652 | } | |
653 | cell->state = AFS_CELL_DEACTIVATING; | |
654 | goto again; | |
655 | ||
656 | case AFS_CELL_DEACTIVATING: | |
657 | set_bit(AFS_CELL_FL_NOT_READY, &cell->flags); | |
658 | if (atomic_read(&cell->usage) > 1) | |
659 | goto reverse_deactivation; | |
660 | afs_deactivate_cell(net, cell); | |
661 | cell->state = AFS_CELL_INACTIVE; | |
662 | goto again; | |
663 | ||
664 | default: | |
665 | break; | |
666 | } | |
667 | _debug("bad state %u", cell->state); | |
668 | BUG(); /* Unhandled state */ | |
669 | ||
670 | activation_failed: | |
671 | cell->error = ret; | |
672 | afs_deactivate_cell(net, cell); | |
673 | ||
674 | cell->state = AFS_CELL_FAILED; | |
675 | smp_wmb(); | |
676 | if (test_and_clear_bit(AFS_CELL_FL_NOT_READY, &cell->flags)) | |
677 | wake_up_bit(&cell->flags, AFS_CELL_FL_NOT_READY); | |
678 | goto again; | |
679 | ||
680 | reverse_deactivation: | |
681 | cell->state = AFS_CELL_ACTIVE; | |
682 | smp_wmb(); | |
683 | clear_bit(AFS_CELL_FL_NOT_READY, &cell->flags); | |
684 | wake_up_bit(&cell->flags, AFS_CELL_FL_NOT_READY); | |
685 | _leave(" [deact->act]"); | |
686 | return; | |
687 | ||
688 | done: | |
689 | _leave(" [done %u]", cell->state); | |
690 | return; | |
691 | ||
692 | final_destruction: | |
693 | call_rcu(&cell->rcu, afs_cell_destroy); | |
694 | afs_dec_cells_outstanding(net); | |
695 | _leave(" [destruct %d]", atomic_read(&net->cells_outstanding)); | |
696 | } | |
697 | ||
698 | /* | |
699 | * Manage the records of cells known to a network namespace. This includes | |
700 | * updating the DNS records and garbage collecting unused cells that were | |
701 | * automatically added. | |
702 | * | |
703 | * Note that constructed cell records may only be removed from net->cells by | |
704 | * this work item, so it is safe for this work item to stash a cursor pointing | |
705 | * into the tree and then return to caller (provided it skips cells that are | |
706 | * still under construction). | |
707 | * | |
708 | * Note also that we were given an increment on net->cells_outstanding by | |
709 | * whoever queued us that we need to deal with before returning. | |
710 | */ | |
711 | void afs_manage_cells(struct work_struct *work) | |
712 | { | |
713 | struct afs_net *net = container_of(work, struct afs_net, cells_manager); | |
714 | struct rb_node *cursor; | |
715 | time64_t now = ktime_get_real_seconds(), next_manage = TIME64_MAX; | |
716 | bool purging = !net->live; | |
1da177e4 LT |
717 | |
718 | _enter(""); | |
719 | ||
989782dc DH |
720 | /* Trawl the cell database looking for cells that have expired from |
721 | * lack of use and cells whose DNS results have expired and dispatch | |
722 | * their managers. | |
723 | */ | |
724 | read_seqlock_excl(&net->cells_lock); | |
1da177e4 | 725 | |
989782dc DH |
726 | for (cursor = rb_first(&net->cells); cursor; cursor = rb_next(cursor)) { |
727 | struct afs_cell *cell = | |
728 | rb_entry(cursor, struct afs_cell, net_node); | |
729 | unsigned usage; | |
730 | bool sched_cell = false; | |
08e0e7c8 | 731 | |
989782dc DH |
732 | usage = atomic_read(&cell->usage); |
733 | _debug("manage %s %u", cell->name, usage); | |
734 | ||
735 | ASSERTCMP(usage, >=, 1); | |
736 | ||
737 | if (purging) { | |
738 | if (test_and_clear_bit(AFS_CELL_FL_NO_GC, &cell->flags)) | |
739 | usage = atomic_dec_return(&cell->usage); | |
740 | ASSERTCMP(usage, ==, 1); | |
741 | } | |
1da177e4 | 742 | |
989782dc DH |
743 | if (usage == 1) { |
744 | time64_t expire_at = cell->last_inactive; | |
1da177e4 | 745 | |
989782dc DH |
746 | if (!test_bit(AFS_CELL_FL_DNS_FAIL, &cell->flags) && |
747 | !test_bit(AFS_CELL_FL_NOT_FOUND, &cell->flags)) | |
748 | expire_at += afs_cell_gc_delay; | |
749 | if (purging || expire_at <= now) | |
750 | sched_cell = true; | |
751 | else if (expire_at < next_manage) | |
752 | next_manage = expire_at; | |
1da177e4 LT |
753 | } |
754 | ||
989782dc DH |
755 | if (!purging) { |
756 | if (cell->dns_expiry <= now) | |
757 | sched_cell = true; | |
758 | else if (cell->dns_expiry <= next_manage) | |
759 | next_manage = cell->dns_expiry; | |
760 | } | |
761 | ||
762 | if (sched_cell) | |
763 | queue_work(afs_wq, &cell->manager); | |
764 | } | |
765 | ||
766 | read_sequnlock_excl(&net->cells_lock); | |
1da177e4 | 767 | |
989782dc DH |
768 | /* Update the timer on the way out. We have to pass an increment on |
769 | * cells_outstanding in the namespace that we are in to the timer or | |
770 | * the work scheduler. | |
771 | */ | |
772 | if (!purging && next_manage < TIME64_MAX) { | |
773 | now = ktime_get_real_seconds(); | |
1da177e4 | 774 | |
989782dc DH |
775 | if (next_manage - now <= 0) { |
776 | if (queue_work(afs_wq, &net->cells_manager)) | |
777 | atomic_inc(&net->cells_outstanding); | |
778 | } else { | |
779 | afs_set_cell_timer(net, next_manage - now); | |
1da177e4 LT |
780 | } |
781 | } | |
782 | ||
989782dc DH |
783 | afs_dec_cells_outstanding(net); |
784 | _leave(" [%d]", atomic_read(&net->cells_outstanding)); | |
785 | } | |
786 | ||
787 | /* | |
788 | * Purge in-memory cell database. | |
789 | */ | |
790 | void afs_cell_purge(struct afs_net *net) | |
791 | { | |
792 | struct afs_cell *ws; | |
793 | ||
794 | _enter(""); | |
795 | ||
796 | write_seqlock(&net->cells_lock); | |
1588def9 DH |
797 | ws = rcu_access_pointer(net->ws_cell); |
798 | RCU_INIT_POINTER(net->ws_cell, NULL); | |
989782dc DH |
799 | write_sequnlock(&net->cells_lock); |
800 | afs_put_cell(net, ws); | |
801 | ||
802 | _debug("del timer"); | |
803 | if (del_timer_sync(&net->cells_timer)) | |
804 | atomic_dec(&net->cells_outstanding); | |
805 | ||
806 | _debug("kick mgr"); | |
807 | afs_queue_cell_manager(net); | |
808 | ||
809 | _debug("wait"); | |
ab1fbe32 PZ |
810 | wait_var_event(&net->cells_outstanding, |
811 | !atomic_read(&net->cells_outstanding)); | |
1da177e4 | 812 | _leave(""); |
ec26815a | 813 | } |