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