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