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