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