David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 1 | /* AFS server record management |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 3 | * Copyright (C) 2002, 2007 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 | |
| 12 | #include <linux/sched.h> |
| 13 | #include <linux/slab.h> |
David Howells | 4d9df98 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 14 | #include "afs_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include "internal.h" |
| 16 | |
Adrian Bunk | c1206a2 | 2007-10-16 23:26:41 -0700 | [diff] [blame] | 17 | static unsigned afs_server_timeout = 10; /* server timeout in seconds */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 19 | static void afs_inc_servers_outstanding(struct afs_net *net) |
| 20 | { |
| 21 | atomic_inc(&net->servers_outstanding); |
| 22 | } |
| 23 | |
| 24 | static void afs_dec_servers_outstanding(struct afs_net *net) |
| 25 | { |
| 26 | if (atomic_dec_and_test(&net->servers_outstanding)) |
| 27 | wake_up_atomic_t(&net->servers_outstanding); |
| 28 | } |
| 29 | |
| 30 | void afs_server_timer(struct timer_list *timer) |
| 31 | { |
| 32 | struct afs_net *net = container_of(timer, struct afs_net, server_timer); |
| 33 | |
| 34 | if (!queue_work(afs_wq, &net->server_reaper)) |
| 35 | afs_dec_servers_outstanding(net); |
| 36 | } |
| 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | /* |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 39 | * install a server record in the master tree |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | */ |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 41 | static int afs_install_server(struct afs_server *server) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 43 | struct afs_server *xserver; |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 44 | struct afs_net *net = server->cell->net; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 45 | struct rb_node **pp, *p; |
David Howells | 4d9df98 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 46 | int ret, diff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 48 | _enter("%p", server); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 50 | write_lock(&net->servers_lock); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 51 | |
| 52 | ret = -EEXIST; |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 53 | pp = &net->servers.rb_node; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 54 | p = NULL; |
| 55 | while (*pp) { |
| 56 | p = *pp; |
| 57 | _debug("- consider %p", p); |
| 58 | xserver = rb_entry(p, struct afs_server, master_rb); |
David Howells | 8b2a464 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 59 | diff = memcmp(&server->addrs->addrs[0], |
| 60 | &xserver->addrs->addrs[0], |
| 61 | sizeof(sizeof(server->addrs->addrs[0]))); |
David Howells | 4d9df98 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 62 | if (diff < 0) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 63 | pp = &(*pp)->rb_left; |
David Howells | 4d9df98 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 64 | else if (diff > 0) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 65 | pp = &(*pp)->rb_right; |
| 66 | else |
| 67 | goto error; |
| 68 | } |
| 69 | |
| 70 | rb_link_node(&server->master_rb, p, pp); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 71 | rb_insert_color(&server->master_rb, &net->servers); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 72 | ret = 0; |
| 73 | |
| 74 | error: |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 75 | write_unlock(&net->servers_lock); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 76 | return ret; |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * allocate a new server record |
| 81 | */ |
| 82 | static struct afs_server *afs_alloc_server(struct afs_cell *cell, |
David Howells | 4d9df98 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 83 | const struct sockaddr_rxrpc *addr) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 84 | { |
| 85 | struct afs_server *server; |
| 86 | |
| 87 | _enter(""); |
| 88 | |
Yan Burman | b593e48 | 2006-12-06 20:40:32 -0800 | [diff] [blame] | 89 | server = kzalloc(sizeof(struct afs_server), GFP_KERNEL); |
David Howells | 8b2a464 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 90 | if (!server) |
| 91 | goto enomem; |
| 92 | server->addrs = kzalloc(sizeof(struct afs_addr_list) + |
| 93 | sizeof(struct sockaddr_rxrpc), |
| 94 | GFP_KERNEL); |
| 95 | if (!server->addrs) |
| 96 | goto enomem_server; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 97 | |
David Howells | 8b2a464 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 98 | atomic_set(&server->usage, 1); |
| 99 | server->net = cell->net; |
| 100 | server->cell = cell; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 101 | |
David Howells | 8b2a464 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 102 | INIT_LIST_HEAD(&server->link); |
| 103 | INIT_LIST_HEAD(&server->grave); |
| 104 | init_rwsem(&server->sem); |
| 105 | spin_lock_init(&server->fs_lock); |
| 106 | INIT_LIST_HEAD(&server->cb_interests); |
| 107 | rwlock_init(&server->cb_break_lock); |
| 108 | |
| 109 | refcount_set(&server->addrs->usage, 1); |
| 110 | server->addrs->nr_addrs = 1; |
| 111 | server->addrs->addrs[0] = *addr; |
| 112 | afs_inc_servers_outstanding(cell->net); |
| 113 | |
| 114 | _leave(" = %p{%d}", server, atomic_read(&server->usage)); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 115 | return server; |
David Howells | 8b2a464 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 116 | |
| 117 | enomem_server: |
| 118 | kfree(server); |
| 119 | enomem: |
| 120 | _leave(" = NULL [nomem]"); |
| 121 | return NULL; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* |
| 125 | * get an FS-server record for a cell |
| 126 | */ |
| 127 | struct afs_server *afs_lookup_server(struct afs_cell *cell, |
David Howells | 4d9df98 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 128 | struct sockaddr_rxrpc *addr) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 129 | { |
| 130 | struct afs_server *server, *candidate; |
| 131 | |
David Howells | 4d9df98 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 132 | _enter("%p,%pIS", cell, &addr->transport); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 133 | |
| 134 | /* quick scan of the list to see if we already have the server */ |
| 135 | read_lock(&cell->servers_lock); |
| 136 | |
| 137 | list_for_each_entry(server, &cell->servers, link) { |
David Howells | 8b2a464 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 138 | if (memcmp(&server->addrs->addrs[0], addr, sizeof(*addr)) == 0) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 139 | goto found_server_quickly; |
| 140 | } |
| 141 | read_unlock(&cell->servers_lock); |
| 142 | |
| 143 | candidate = afs_alloc_server(cell, addr); |
| 144 | if (!candidate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | _leave(" = -ENOMEM"); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 146 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
| 148 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 149 | write_lock(&cell->servers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 151 | /* check the cell's server list again */ |
| 152 | list_for_each_entry(server, &cell->servers, link) { |
David Howells | 8b2a464 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 153 | if (memcmp(&server->addrs->addrs[0], addr, sizeof(*addr)) == 0) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 154 | goto found_server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | } |
| 156 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 157 | _debug("new"); |
| 158 | server = candidate; |
| 159 | if (afs_install_server(server) < 0) |
| 160 | goto server_in_two_cells; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
| 162 | afs_get_cell(cell); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 163 | list_add_tail(&server->link, &cell->servers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 165 | write_unlock(&cell->servers_lock); |
| 166 | _leave(" = %p{%d}", server, atomic_read(&server->usage)); |
| 167 | return server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 169 | /* found a matching server quickly */ |
| 170 | found_server_quickly: |
| 171 | _debug("found quickly"); |
| 172 | afs_get_server(server); |
| 173 | read_unlock(&cell->servers_lock); |
| 174 | no_longer_unused: |
| 175 | if (!list_empty(&server->grave)) { |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 176 | spin_lock(&cell->net->server_graveyard_lock); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 177 | list_del_init(&server->grave); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 178 | spin_unlock(&cell->net->server_graveyard_lock); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 179 | } |
| 180 | _leave(" = %p{%d}", server, atomic_read(&server->usage)); |
| 181 | return server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 183 | /* found a matching server on the second pass */ |
| 184 | found_server: |
| 185 | _debug("found"); |
| 186 | afs_get_server(server); |
| 187 | write_unlock(&cell->servers_lock); |
| 188 | kfree(candidate); |
| 189 | goto no_longer_unused; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 191 | /* found a server that seems to be in two cells */ |
| 192 | server_in_two_cells: |
| 193 | write_unlock(&cell->servers_lock); |
| 194 | kfree(candidate); |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 195 | afs_dec_servers_outstanding(cell->net); |
Harvey Harrison | be85940 | 2008-10-31 00:56:28 -0700 | [diff] [blame] | 196 | printk(KERN_NOTICE "kAFS: Server %pI4 appears to be in two cells\n", |
| 197 | addr); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 198 | _leave(" = -EEXIST"); |
| 199 | return ERR_PTR(-EEXIST); |
| 200 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 202 | /* |
| 203 | * look up a server by its IP address |
| 204 | */ |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 205 | struct afs_server *afs_find_server(struct afs_net *net, |
| 206 | const struct sockaddr_rxrpc *srx) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 207 | { |
| 208 | struct afs_server *server = NULL; |
| 209 | struct rb_node *p; |
David Howells | 4d9df98 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 210 | int diff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
David Howells | 4d9df98 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 212 | _enter("{%d,%pIS}", srx->transport.family, &srx->transport); |
David Howells | 8324f0b | 2016-08-30 09:49:29 +0100 | [diff] [blame] | 213 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 214 | read_lock(&net->servers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 216 | p = net->servers.rb_node; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 217 | while (p) { |
| 218 | server = rb_entry(p, struct afs_server, master_rb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 220 | _debug("- consider %p", p); |
| 221 | |
David Howells | 8b2a464 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 222 | diff = memcmp(srx, &server->addrs->addrs[0], sizeof(*srx)); |
David Howells | 4d9df98 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 223 | if (diff < 0) { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 224 | p = p->rb_left; |
David Howells | 4d9df98 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 225 | } else if (diff > 0) { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 226 | p = p->rb_right; |
| 227 | } else { |
| 228 | afs_get_server(server); |
| 229 | goto found; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | server = NULL; |
| 234 | found: |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 235 | read_unlock(&net->servers_lock); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 236 | _leave(" = %p", server); |
| 237 | return server; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 238 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 240 | static void afs_set_server_timer(struct afs_net *net, time64_t delay) |
| 241 | { |
| 242 | afs_inc_servers_outstanding(net); |
| 243 | if (net->live) { |
| 244 | if (timer_reduce(&net->server_timer, jiffies + delay * HZ)) |
| 245 | afs_dec_servers_outstanding(net); |
| 246 | } else { |
| 247 | if (!queue_work(afs_wq, &net->server_reaper)) |
| 248 | afs_dec_servers_outstanding(net); |
| 249 | } |
| 250 | } |
| 251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | /* |
| 253 | * destroy a server record |
| 254 | * - removes from the cell list |
| 255 | */ |
David Howells | 9ed900b | 2017-11-02 15:27:46 +0000 | [diff] [blame] | 256 | void afs_put_server(struct afs_net *net, struct afs_server *server) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | if (!server) |
| 259 | return; |
| 260 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 261 | _enter("%p{%d}", server, atomic_read(&server->usage)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 263 | _debug("PUT SERVER %d", atomic_read(&server->usage)); |
| 264 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 265 | ASSERTCMP(atomic_read(&server->usage), >, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
| 267 | if (likely(!atomic_dec_and_test(&server->usage))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | _leave(""); |
| 269 | return; |
| 270 | } |
| 271 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 272 | spin_lock(&net->server_graveyard_lock); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 273 | if (atomic_read(&server->usage) == 0) { |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 274 | list_move_tail(&server->grave, &net->server_graveyard); |
Tina Ruchandani | 8a79790 | 2017-03-16 16:27:46 +0000 | [diff] [blame] | 275 | server->time_of_death = ktime_get_real_seconds(); |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 276 | afs_set_server_timer(net, afs_server_timeout); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 277 | } |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 278 | spin_unlock(&net->server_graveyard_lock); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 279 | _leave(" [dead]"); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 280 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | /* |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 283 | * destroy a dead server |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | */ |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 285 | static void afs_destroy_server(struct afs_net *net, struct afs_server *server) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | { |
David Howells | 8b2a464 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 287 | struct afs_addr_list *alist = server->addrs; |
| 288 | struct afs_addr_cursor ac = { |
| 289 | .alist = alist, |
| 290 | .addr = &alist->addrs[0], |
| 291 | .start = alist->index, |
| 292 | .index = alist->index, |
| 293 | .error = 0, |
| 294 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | _enter("%p", server); |
| 296 | |
David Howells | 8b2a464 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 297 | afs_fs_give_up_all_callbacks(server, &ac, NULL, false); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 298 | afs_put_cell(net, server->cell); |
David Howells | 8b2a464 | 2017-11-02 15:27:50 +0000 | [diff] [blame^] | 299 | afs_put_addrlist(server->addrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | kfree(server); |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 301 | afs_dec_servers_outstanding(net); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 302 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | /* |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 305 | * reap dead server records |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | */ |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 307 | void afs_reap_server(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 309 | LIST_HEAD(corpses); |
| 310 | struct afs_server *server; |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 311 | struct afs_net *net = container_of(work, struct afs_net, server_reaper); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 312 | unsigned long delay, expiry; |
Tina Ruchandani | 8a79790 | 2017-03-16 16:27:46 +0000 | [diff] [blame] | 313 | time64_t now; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | |
Tina Ruchandani | 8a79790 | 2017-03-16 16:27:46 +0000 | [diff] [blame] | 315 | now = ktime_get_real_seconds(); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 316 | spin_lock(&net->server_graveyard_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 318 | while (!list_empty(&net->server_graveyard)) { |
| 319 | server = list_entry(net->server_graveyard.next, |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 320 | struct afs_server, grave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 322 | /* the queue is ordered most dead first */ |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 323 | if (net->live) { |
| 324 | expiry = server->time_of_death + afs_server_timeout; |
| 325 | if (expiry > now) { |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 326 | delay = (expiry - now); |
| 327 | afs_set_server_timer(net, delay); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 328 | break; |
| 329 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } |
| 331 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 332 | write_lock(&server->cell->servers_lock); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 333 | write_lock(&net->servers_lock); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 334 | if (atomic_read(&server->usage) > 0) { |
| 335 | list_del_init(&server->grave); |
| 336 | } else { |
| 337 | list_move_tail(&server->grave, &corpses); |
| 338 | list_del_init(&server->link); |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 339 | rb_erase(&server->master_rb, &net->servers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | } |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 341 | write_unlock(&net->servers_lock); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 342 | write_unlock(&server->cell->servers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | } |
| 344 | |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 345 | spin_unlock(&net->server_graveyard_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 347 | /* now reap the corpses we've extracted */ |
| 348 | while (!list_empty(&corpses)) { |
| 349 | server = list_entry(corpses.next, struct afs_server, grave); |
| 350 | list_del(&server->grave); |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 351 | afs_destroy_server(net, server); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | } |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 353 | |
| 354 | afs_dec_servers_outstanding(net); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 355 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | /* |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 358 | * Discard all the server records from a net namespace when it is destroyed or |
| 359 | * the afs module is removed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | */ |
David Howells | f044c88 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 361 | void __net_exit afs_purge_servers(struct afs_net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | { |
David Howells | 59fa1c4 | 2017-11-02 15:27:45 +0000 | [diff] [blame] | 363 | if (del_timer_sync(&net->server_timer)) |
| 364 | atomic_dec(&net->servers_outstanding); |
| 365 | |
| 366 | afs_inc_servers_outstanding(net); |
| 367 | if (!queue_work(afs_wq, &net->server_reaper)) |
| 368 | afs_dec_servers_outstanding(net); |
| 369 | |
| 370 | wait_on_atomic_t(&net->servers_outstanding, atomic_t_wait, |
| 371 | TASK_UNINTERRUPTIBLE); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 372 | } |