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