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