blob: f4909951667554b6e4263ebebedc6d2513599d48 [file] [log] [blame]
David Howellsec268152007-04-26 15:49:28 -07001/* AFS server record management
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
David Howells08e0e7c2007-04-26 15:55:03 -07003 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "internal.h"
15
Adrian Bunkc1206a22007-10-16 23:26:41 -070016static unsigned afs_server_timeout = 10; /* server timeout in seconds */
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
David Howells08e0e7c2007-04-26 15:55:03 -070018static void afs_reap_server(struct work_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
David Howells08e0e7c2007-04-26 15:55:03 -070020/* tree of all the servers, indexed by IP address */
21static struct rb_root afs_servers = RB_ROOT;
22static DEFINE_RWLOCK(afs_servers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
David Howells08e0e7c2007-04-26 15:55:03 -070024/* LRU list of all the servers not currently in use */
25static LIST_HEAD(afs_server_graveyard);
26static DEFINE_SPINLOCK(afs_server_graveyard_lock);
27static DECLARE_DELAYED_WORK(afs_server_reaper, afs_reap_server);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/*
David Howells08e0e7c2007-04-26 15:55:03 -070030 * install a server record in the master tree
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 */
David Howells08e0e7c2007-04-26 15:55:03 -070032static int afs_install_server(struct afs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
David Howells08e0e7c2007-04-26 15:55:03 -070034 struct afs_server *xserver;
35 struct rb_node **pp, *p;
36 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
David Howells08e0e7c2007-04-26 15:55:03 -070038 _enter("%p", server);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
David Howells08e0e7c2007-04-26 15:55:03 -070040 write_lock(&afs_servers_lock);
41
42 ret = -EEXIST;
43 pp = &afs_servers.rb_node;
44 p = NULL;
45 while (*pp) {
46 p = *pp;
47 _debug("- consider %p", p);
48 xserver = rb_entry(p, struct afs_server, master_rb);
49 if (server->addr.s_addr < xserver->addr.s_addr)
50 pp = &(*pp)->rb_left;
51 else if (server->addr.s_addr > xserver->addr.s_addr)
52 pp = &(*pp)->rb_right;
53 else
54 goto error;
55 }
56
57 rb_link_node(&server->master_rb, p, pp);
58 rb_insert_color(&server->master_rb, &afs_servers);
59 ret = 0;
60
61error:
62 write_unlock(&afs_servers_lock);
63 return ret;
64}
65
66/*
67 * allocate a new server record
68 */
69static struct afs_server *afs_alloc_server(struct afs_cell *cell,
70 const struct in_addr *addr)
71{
72 struct afs_server *server;
73
74 _enter("");
75
Yan Burmanb593e482006-12-06 20:40:32 -080076 server = kzalloc(sizeof(struct afs_server), GFP_KERNEL);
David Howells08e0e7c2007-04-26 15:55:03 -070077 if (server) {
78 atomic_set(&server->usage, 1);
79 server->cell = cell;
80
81 INIT_LIST_HEAD(&server->link);
82 INIT_LIST_HEAD(&server->grave);
83 init_rwsem(&server->sem);
84 spin_lock_init(&server->fs_lock);
85 server->fs_vnodes = RB_ROOT;
86 server->cb_promises = RB_ROOT;
87 spin_lock_init(&server->cb_lock);
88 init_waitqueue_head(&server->cb_break_waitq);
89 INIT_DELAYED_WORK(&server->cb_break_work,
90 afs_dispatch_give_up_callbacks);
91
92 memcpy(&server->addr, addr, sizeof(struct in_addr));
93 server->addr.s_addr = addr->s_addr;
94 }
95
96 _leave(" = %p{%d}", server, atomic_read(&server->usage));
97 return server;
98}
99
100/*
101 * get an FS-server record for a cell
102 */
103struct afs_server *afs_lookup_server(struct afs_cell *cell,
104 const struct in_addr *addr)
105{
106 struct afs_server *server, *candidate;
107
Harvey Harrisonbe859402008-10-31 00:56:28 -0700108 _enter("%p,%pI4", cell, &addr->s_addr);
David Howells08e0e7c2007-04-26 15:55:03 -0700109
110 /* quick scan of the list to see if we already have the server */
111 read_lock(&cell->servers_lock);
112
113 list_for_each_entry(server, &cell->servers, link) {
114 if (server->addr.s_addr == addr->s_addr)
115 goto found_server_quickly;
116 }
117 read_unlock(&cell->servers_lock);
118
119 candidate = afs_alloc_server(cell, addr);
120 if (!candidate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 _leave(" = -ENOMEM");
David Howells08e0e7c2007-04-26 15:55:03 -0700122 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
124
David Howells08e0e7c2007-04-26 15:55:03 -0700125 write_lock(&cell->servers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
David Howells08e0e7c2007-04-26 15:55:03 -0700127 /* check the cell's server list again */
128 list_for_each_entry(server, &cell->servers, link) {
129 if (server->addr.s_addr == addr->s_addr)
130 goto found_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
132
David Howells08e0e7c2007-04-26 15:55:03 -0700133 _debug("new");
134 server = candidate;
135 if (afs_install_server(server) < 0)
136 goto server_in_two_cells;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 afs_get_cell(cell);
David Howells08e0e7c2007-04-26 15:55:03 -0700139 list_add_tail(&server->link, &cell->servers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
David Howells08e0e7c2007-04-26 15:55:03 -0700141 write_unlock(&cell->servers_lock);
142 _leave(" = %p{%d}", server, atomic_read(&server->usage));
143 return server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
David Howells08e0e7c2007-04-26 15:55:03 -0700145 /* found a matching server quickly */
146found_server_quickly:
147 _debug("found quickly");
148 afs_get_server(server);
149 read_unlock(&cell->servers_lock);
150no_longer_unused:
151 if (!list_empty(&server->grave)) {
152 spin_lock(&afs_server_graveyard_lock);
153 list_del_init(&server->grave);
154 spin_unlock(&afs_server_graveyard_lock);
155 }
156 _leave(" = %p{%d}", server, atomic_read(&server->usage));
157 return server;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
David Howells08e0e7c2007-04-26 15:55:03 -0700159 /* found a matching server on the second pass */
160found_server:
161 _debug("found");
162 afs_get_server(server);
163 write_unlock(&cell->servers_lock);
164 kfree(candidate);
165 goto no_longer_unused;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
David Howells08e0e7c2007-04-26 15:55:03 -0700167 /* found a server that seems to be in two cells */
168server_in_two_cells:
169 write_unlock(&cell->servers_lock);
170 kfree(candidate);
Harvey Harrisonbe859402008-10-31 00:56:28 -0700171 printk(KERN_NOTICE "kAFS: Server %pI4 appears to be in two cells\n",
172 addr);
David Howells08e0e7c2007-04-26 15:55:03 -0700173 _leave(" = -EEXIST");
174 return ERR_PTR(-EEXIST);
175}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
David Howells08e0e7c2007-04-26 15:55:03 -0700177/*
178 * look up a server by its IP address
179 */
180struct afs_server *afs_find_server(const struct in_addr *_addr)
181{
182 struct afs_server *server = NULL;
183 struct rb_node *p;
184 struct in_addr addr = *_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Harvey Harrisonbe859402008-10-31 00:56:28 -0700186 _enter("%pI4", &addr.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
David Howells08e0e7c2007-04-26 15:55:03 -0700188 read_lock(&afs_servers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
David Howells08e0e7c2007-04-26 15:55:03 -0700190 p = afs_servers.rb_node;
191 while (p) {
192 server = rb_entry(p, struct afs_server, master_rb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
David Howells08e0e7c2007-04-26 15:55:03 -0700194 _debug("- consider %p", p);
195
196 if (addr.s_addr < server->addr.s_addr) {
197 p = p->rb_left;
198 } else if (addr.s_addr > server->addr.s_addr) {
199 p = p->rb_right;
200 } else {
201 afs_get_server(server);
202 goto found;
203 }
204 }
205
206 server = NULL;
207found:
208 read_unlock(&afs_servers_lock);
209 ASSERTIFCMP(server, server->addr.s_addr, ==, addr.s_addr);
210 _leave(" = %p", server);
211 return server;
David Howellsec268152007-04-26 15:49:28 -0700212}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214/*
215 * destroy a server record
216 * - removes from the cell list
217 */
218void afs_put_server(struct afs_server *server)
219{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (!server)
221 return;
222
David Howells08e0e7c2007-04-26 15:55:03 -0700223 _enter("%p{%d}", server, atomic_read(&server->usage));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
David Howells260a9802007-04-26 15:59:35 -0700225 _debug("PUT SERVER %d", atomic_read(&server->usage));
226
David Howells08e0e7c2007-04-26 15:55:03 -0700227 ASSERTCMP(atomic_read(&server->usage), >, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 if (likely(!atomic_dec_and_test(&server->usage))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 _leave("");
231 return;
232 }
233
David Howells08e0e7c2007-04-26 15:55:03 -0700234 afs_flush_callback_breaks(server);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
David Howells08e0e7c2007-04-26 15:55:03 -0700236 spin_lock(&afs_server_graveyard_lock);
237 if (atomic_read(&server->usage) == 0) {
238 list_move_tail(&server->grave, &afs_server_graveyard);
239 server->time_of_death = get_seconds();
240 schedule_delayed_work(&afs_server_reaper,
241 afs_server_timeout * HZ);
242 }
243 spin_unlock(&afs_server_graveyard_lock);
244 _leave(" [dead]");
David Howellsec268152007-04-26 15:49:28 -0700245}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247/*
David Howells08e0e7c2007-04-26 15:55:03 -0700248 * destroy a dead server
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 */
David Howells08e0e7c2007-04-26 15:55:03 -0700250static void afs_destroy_server(struct afs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 _enter("%p", server);
253
David Howells416351f2007-05-09 02:33:45 -0700254 ASSERTIF(server->cb_break_head != server->cb_break_tail,
255 delayed_work_pending(&server->cb_break_work));
256
David Howells08e0e7c2007-04-26 15:55:03 -0700257 ASSERTCMP(server->fs_vnodes.rb_node, ==, NULL);
258 ASSERTCMP(server->cb_promises.rb_node, ==, NULL);
259 ASSERTCMP(server->cb_break_head, ==, server->cb_break_tail);
260 ASSERTCMP(atomic_read(&server->cb_break_n), ==, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
David Howells08e0e7c2007-04-26 15:55:03 -0700262 afs_put_cell(server->cell);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 kfree(server);
David Howellsec268152007-04-26 15:49:28 -0700264}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266/*
David Howells08e0e7c2007-04-26 15:55:03 -0700267 * reap dead server records
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 */
David Howells08e0e7c2007-04-26 15:55:03 -0700269static void afs_reap_server(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
David Howells08e0e7c2007-04-26 15:55:03 -0700271 LIST_HEAD(corpses);
272 struct afs_server *server;
273 unsigned long delay, expiry;
274 time_t now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
David Howells08e0e7c2007-04-26 15:55:03 -0700276 now = get_seconds();
277 spin_lock(&afs_server_graveyard_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
David Howells08e0e7c2007-04-26 15:55:03 -0700279 while (!list_empty(&afs_server_graveyard)) {
280 server = list_entry(afs_server_graveyard.next,
281 struct afs_server, grave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
David Howells08e0e7c2007-04-26 15:55:03 -0700283 /* the queue is ordered most dead first */
284 expiry = server->time_of_death + afs_server_timeout;
285 if (expiry > now) {
286 delay = (expiry - now) * HZ;
287 if (!schedule_delayed_work(&afs_server_reaper, delay)) {
288 cancel_delayed_work(&afs_server_reaper);
289 schedule_delayed_work(&afs_server_reaper,
290 delay);
291 }
292 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294
David Howells08e0e7c2007-04-26 15:55:03 -0700295 write_lock(&server->cell->servers_lock);
296 write_lock(&afs_servers_lock);
297 if (atomic_read(&server->usage) > 0) {
298 list_del_init(&server->grave);
299 } else {
300 list_move_tail(&server->grave, &corpses);
301 list_del_init(&server->link);
302 rb_erase(&server->master_rb, &afs_servers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
David Howells08e0e7c2007-04-26 15:55:03 -0700304 write_unlock(&afs_servers_lock);
305 write_unlock(&server->cell->servers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
307
David Howells08e0e7c2007-04-26 15:55:03 -0700308 spin_unlock(&afs_server_graveyard_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
David Howells08e0e7c2007-04-26 15:55:03 -0700310 /* now reap the corpses we've extracted */
311 while (!list_empty(&corpses)) {
312 server = list_entry(corpses.next, struct afs_server, grave);
313 list_del(&server->grave);
314 afs_destroy_server(server);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
David Howellsec268152007-04-26 15:49:28 -0700316}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318/*
David Howells08e0e7c2007-04-26 15:55:03 -0700319 * discard all the server records for rmmod
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 */
David Howells08e0e7c2007-04-26 15:55:03 -0700321void __exit afs_purge_servers(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
David Howells08e0e7c2007-04-26 15:55:03 -0700323 afs_server_timeout = 0;
324 cancel_delayed_work(&afs_server_reaper);
325 schedule_delayed_work(&afs_server_reaper, 0);
David Howellsec268152007-04-26 15:49:28 -0700326}