blob: 74cce174882a4ca14d18ceb3d58f0314e398dbcd [file] [log] [blame]
David Howells08e0e7c2007-04-26 15:55:03 -07001/* AFS volume location 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/kernel.h>
13#include <linux/module.h>
14#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "internal.h"
16
David Howells08e0e7c2007-04-26 15:55:03 -070017unsigned afs_vlocation_timeout = 10; /* volume location timeout in seconds */
18unsigned afs_vlocation_update_timeout = 10 * 60;
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
David Howells08e0e7c2007-04-26 15:55:03 -070020static void afs_vlocation_reaper(struct work_struct *);
21static void afs_vlocation_updater(struct work_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
David Howells08e0e7c2007-04-26 15:55:03 -070023static LIST_HEAD(afs_vlocation_updates);
24static LIST_HEAD(afs_vlocation_graveyard);
25static DEFINE_SPINLOCK(afs_vlocation_updates_lock);
26static DEFINE_SPINLOCK(afs_vlocation_graveyard_lock);
27static DECLARE_DELAYED_WORK(afs_vlocation_reap, afs_vlocation_reaper);
28static DECLARE_DELAYED_WORK(afs_vlocation_update, afs_vlocation_updater);
29static struct workqueue_struct *afs_vlocation_update_worker;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031/*
32 * iterate through the VL servers in a cell until one of them admits knowing
33 * about the volume in question
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 */
David Howells08e0e7c2007-04-26 15:55:03 -070035static int afs_vlocation_access_vl_by_name(struct afs_vlocation *vl,
David Howells00d3b7a2007-04-26 15:57:07 -070036 struct key *key,
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 struct afs_cache_vlocation *vldb)
38{
David Howells08e0e7c2007-04-26 15:55:03 -070039 struct afs_cell *cell = vl->cell;
40 struct in_addr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 int count, ret;
42
David Howells08e0e7c2007-04-26 15:55:03 -070043 _enter("%s,%s", cell->name, vl->vldb.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
David Howells08e0e7c2007-04-26 15:55:03 -070045 down_write(&vl->cell->vl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 ret = -ENOMEDIUM;
47 for (count = cell->vl_naddrs; count > 0; count--) {
David Howells08e0e7c2007-04-26 15:55:03 -070048 addr = cell->vl_addrs[cell->vl_curr_svix];
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
David Howells08e0e7c2007-04-26 15:55:03 -070050 _debug("CellServ[%hu]: %08x", cell->vl_curr_svix, addr.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52 /* attempt to access the VL server */
David Howells00d3b7a2007-04-26 15:57:07 -070053 ret = afs_vl_get_entry_by_name(&addr, key, vl->vldb.name, vldb,
David Howells08e0e7c2007-04-26 15:55:03 -070054 &afs_sync_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 switch (ret) {
56 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 goto out;
58 case -ENOMEM:
59 case -ENONET:
60 case -ENETUNREACH:
61 case -EHOSTUNREACH:
62 case -ECONNREFUSED:
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 if (ret == -ENOMEM || ret == -ENONET)
64 goto out;
65 goto rotate;
66 case -ENOMEDIUM:
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 goto out;
68 default:
David Howells08e0e7c2007-04-26 15:55:03 -070069 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 goto rotate;
71 }
72
73 /* rotate the server records upon lookup failure */
74 rotate:
75 cell->vl_curr_svix++;
76 cell->vl_curr_svix %= cell->vl_naddrs;
77 }
78
David Howellsec268152007-04-26 15:49:28 -070079out:
David Howells08e0e7c2007-04-26 15:55:03 -070080 up_write(&vl->cell->vl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 _leave(" = %d", ret);
82 return ret;
David Howellsec268152007-04-26 15:49:28 -070083}
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/*
86 * iterate through the VL servers in a cell until one of them admits knowing
87 * about the volume in question
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 */
David Howells08e0e7c2007-04-26 15:55:03 -070089static int afs_vlocation_access_vl_by_id(struct afs_vlocation *vl,
David Howells00d3b7a2007-04-26 15:57:07 -070090 struct key *key,
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 afs_volid_t volid,
92 afs_voltype_t voltype,
93 struct afs_cache_vlocation *vldb)
94{
David Howells08e0e7c2007-04-26 15:55:03 -070095 struct afs_cell *cell = vl->cell;
96 struct in_addr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 int count, ret;
98
99 _enter("%s,%x,%d,", cell->name, volid, voltype);
100
David Howells08e0e7c2007-04-26 15:55:03 -0700101 down_write(&vl->cell->vl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 ret = -ENOMEDIUM;
103 for (count = cell->vl_naddrs; count > 0; count--) {
David Howells08e0e7c2007-04-26 15:55:03 -0700104 addr = cell->vl_addrs[cell->vl_curr_svix];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
David Howells08e0e7c2007-04-26 15:55:03 -0700106 _debug("CellServ[%hu]: %08x", cell->vl_curr_svix, addr.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 /* attempt to access the VL server */
David Howells00d3b7a2007-04-26 15:57:07 -0700109 ret = afs_vl_get_entry_by_id(&addr, key, volid, voltype, vldb,
David Howells08e0e7c2007-04-26 15:55:03 -0700110 &afs_sync_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 switch (ret) {
112 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 goto out;
114 case -ENOMEM:
115 case -ENONET:
116 case -ENETUNREACH:
117 case -EHOSTUNREACH:
118 case -ECONNREFUSED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (ret == -ENOMEM || ret == -ENONET)
120 goto out;
121 goto rotate;
David Howells08e0e7c2007-04-26 15:55:03 -0700122 case -EBUSY:
123 vl->upd_busy_cnt++;
124 if (vl->upd_busy_cnt <= 3) {
125 if (vl->upd_busy_cnt > 1) {
126 /* second+ BUSY - sleep a little bit */
127 set_current_state(TASK_UNINTERRUPTIBLE);
128 schedule_timeout(1);
129 __set_current_state(TASK_RUNNING);
130 }
131 continue;
132 }
133 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 case -ENOMEDIUM:
David Howells08e0e7c2007-04-26 15:55:03 -0700135 vl->upd_rej_cnt++;
136 goto rotate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 default:
David Howells08e0e7c2007-04-26 15:55:03 -0700138 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 goto rotate;
140 }
141
142 /* rotate the server records upon lookup failure */
143 rotate:
144 cell->vl_curr_svix++;
145 cell->vl_curr_svix %= cell->vl_naddrs;
David Howells08e0e7c2007-04-26 15:55:03 -0700146 vl->upd_busy_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
148
David Howellsec268152007-04-26 15:49:28 -0700149out:
David Howells08e0e7c2007-04-26 15:55:03 -0700150 if (ret < 0 && vl->upd_rej_cnt > 0) {
151 printk(KERN_NOTICE "kAFS:"
152 " Active volume no longer valid '%s'\n",
153 vl->vldb.name);
154 vl->valid = 0;
155 ret = -ENOMEDIUM;
156 }
157
158 up_write(&vl->cell->vl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 _leave(" = %d", ret);
160 return ret;
David Howellsec268152007-04-26 15:49:28 -0700161}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/*
David Howells08e0e7c2007-04-26 15:55:03 -0700164 * allocate a volume location record
165 */
166static struct afs_vlocation *afs_vlocation_alloc(struct afs_cell *cell,
167 const char *name,
168 size_t namesz)
169{
170 struct afs_vlocation *vl;
171
172 vl = kzalloc(sizeof(struct afs_vlocation), GFP_KERNEL);
173 if (vl) {
174 vl->cell = cell;
175 vl->state = AFS_VL_NEW;
176 atomic_set(&vl->usage, 1);
177 INIT_LIST_HEAD(&vl->link);
178 INIT_LIST_HEAD(&vl->grave);
179 INIT_LIST_HEAD(&vl->update);
180 init_waitqueue_head(&vl->waitq);
David S. Miller39bf0942007-04-26 20:39:14 -0700181 spin_lock_init(&vl->lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700182 memcpy(vl->vldb.name, name, namesz);
183 }
184
185 _leave(" = %p", vl);
186 return vl;
187}
188
189/*
190 * update record if we found it in the cache
191 */
192static int afs_vlocation_update_record(struct afs_vlocation *vl,
David Howells00d3b7a2007-04-26 15:57:07 -0700193 struct key *key,
David Howells08e0e7c2007-04-26 15:55:03 -0700194 struct afs_cache_vlocation *vldb)
195{
196 afs_voltype_t voltype;
197 afs_volid_t vid;
198 int ret;
199
200 /* try to look up a cached volume in the cell VL databases by ID */
201 _debug("Locally Cached: %s %02x { %08x(%x) %08x(%x) %08x(%x) }",
202 vl->vldb.name,
203 vl->vldb.vidmask,
204 ntohl(vl->vldb.servers[0].s_addr),
205 vl->vldb.srvtmask[0],
206 ntohl(vl->vldb.servers[1].s_addr),
207 vl->vldb.srvtmask[1],
208 ntohl(vl->vldb.servers[2].s_addr),
209 vl->vldb.srvtmask[2]);
210
211 _debug("Vids: %08x %08x %08x",
212 vl->vldb.vid[0],
213 vl->vldb.vid[1],
214 vl->vldb.vid[2]);
215
216 if (vl->vldb.vidmask & AFS_VOL_VTM_RW) {
217 vid = vl->vldb.vid[0];
218 voltype = AFSVL_RWVOL;
219 } else if (vl->vldb.vidmask & AFS_VOL_VTM_RO) {
220 vid = vl->vldb.vid[1];
221 voltype = AFSVL_ROVOL;
222 } else if (vl->vldb.vidmask & AFS_VOL_VTM_BAK) {
223 vid = vl->vldb.vid[2];
224 voltype = AFSVL_BACKVOL;
225 } else {
226 BUG();
227 vid = 0;
228 voltype = 0;
229 }
230
231 /* contact the server to make sure the volume is still available
232 * - TODO: need to handle disconnected operation here
233 */
David Howells00d3b7a2007-04-26 15:57:07 -0700234 ret = afs_vlocation_access_vl_by_id(vl, key, vid, voltype, vldb);
David Howells08e0e7c2007-04-26 15:55:03 -0700235 switch (ret) {
236 /* net error */
237 default:
238 printk(KERN_WARNING "kAFS:"
239 " failed to update volume '%s' (%x) up in '%s': %d\n",
240 vl->vldb.name, vid, vl->cell->name, ret);
241 _leave(" = %d", ret);
242 return ret;
243
244 /* pulled from local cache into memory */
245 case 0:
246 _leave(" = 0");
247 return 0;
248
249 /* uh oh... looks like the volume got deleted */
250 case -ENOMEDIUM:
251 printk(KERN_ERR "kAFS:"
252 " volume '%s' (%x) does not exist '%s'\n",
253 vl->vldb.name, vid, vl->cell->name);
254
255 /* TODO: make existing record unavailable */
256 _leave(" = %d", ret);
257 return ret;
258 }
259}
260
261/*
262 * apply the update to a VL record
263 */
264static void afs_vlocation_apply_update(struct afs_vlocation *vl,
265 struct afs_cache_vlocation *vldb)
266{
267 _debug("Done VL Lookup: %s %02x { %08x(%x) %08x(%x) %08x(%x) }",
268 vldb->name, vldb->vidmask,
269 ntohl(vldb->servers[0].s_addr), vldb->srvtmask[0],
270 ntohl(vldb->servers[1].s_addr), vldb->srvtmask[1],
271 ntohl(vldb->servers[2].s_addr), vldb->srvtmask[2]);
272
273 _debug("Vids: %08x %08x %08x",
274 vldb->vid[0], vldb->vid[1], vldb->vid[2]);
275
276 if (strcmp(vldb->name, vl->vldb.name) != 0)
277 printk(KERN_NOTICE "kAFS:"
278 " name of volume '%s' changed to '%s' on server\n",
279 vl->vldb.name, vldb->name);
280
281 vl->vldb = *vldb;
282
283#ifdef AFS_CACHING_SUPPORT
284 /* update volume entry in local cache */
285 cachefs_update_cookie(vl->cache);
286#endif
287}
288
289/*
290 * fill in a volume location record, consulting the cache and the VL server
291 * both
292 */
David Howells00d3b7a2007-04-26 15:57:07 -0700293static int afs_vlocation_fill_in_record(struct afs_vlocation *vl,
294 struct key *key)
David Howells08e0e7c2007-04-26 15:55:03 -0700295{
296 struct afs_cache_vlocation vldb;
297 int ret;
298
299 _enter("");
300
301 ASSERTCMP(vl->valid, ==, 0);
302
303 memset(&vldb, 0, sizeof(vldb));
304
305 /* see if we have an in-cache copy (will set vl->valid if there is) */
306#ifdef AFS_CACHING_SUPPORT
307 cachefs_acquire_cookie(cell->cache,
308 &afs_volume_cache_index_def,
309 vlocation,
310 &vl->cache);
311#endif
312
313 if (vl->valid) {
314 /* try to update a known volume in the cell VL databases by
315 * ID as the name may have changed */
316 _debug("found in cache");
David Howells00d3b7a2007-04-26 15:57:07 -0700317 ret = afs_vlocation_update_record(vl, key, &vldb);
David Howells08e0e7c2007-04-26 15:55:03 -0700318 } else {
319 /* try to look up an unknown volume in the cell VL databases by
320 * name */
David Howells00d3b7a2007-04-26 15:57:07 -0700321 ret = afs_vlocation_access_vl_by_name(vl, key, &vldb);
David Howells08e0e7c2007-04-26 15:55:03 -0700322 if (ret < 0) {
323 printk("kAFS: failed to locate '%s' in cell '%s'\n",
324 vl->vldb.name, vl->cell->name);
325 return ret;
326 }
327 }
328
329 afs_vlocation_apply_update(vl, &vldb);
330 _leave(" = 0");
331 return 0;
332}
333
334/*
335 * queue a vlocation record for updates
336 */
337void afs_vlocation_queue_for_updates(struct afs_vlocation *vl)
338{
339 struct afs_vlocation *xvl;
340
341 /* wait at least 10 minutes before updating... */
342 vl->update_at = get_seconds() + afs_vlocation_update_timeout;
343
344 spin_lock(&afs_vlocation_updates_lock);
345
346 if (!list_empty(&afs_vlocation_updates)) {
347 /* ... but wait at least 1 second more than the newest record
348 * already queued so that we don't spam the VL server suddenly
349 * with lots of requests
350 */
351 xvl = list_entry(afs_vlocation_updates.prev,
352 struct afs_vlocation, update);
353 if (vl->update_at <= xvl->update_at)
354 vl->update_at = xvl->update_at + 1;
355 } else {
356 queue_delayed_work(afs_vlocation_update_worker,
357 &afs_vlocation_update,
358 afs_vlocation_update_timeout * HZ);
359 }
360
361 list_add_tail(&vl->update, &afs_vlocation_updates);
362 spin_unlock(&afs_vlocation_updates_lock);
363}
364
365/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 * lookup volume location
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 * - iterate through the VL servers in a cell until one of them admits knowing
368 * about the volume in question
369 * - lookup in the local cache if not able to find on the VL server
370 * - insert/update in the local cache if did get a VL response
371 */
David Howells08e0e7c2007-04-26 15:55:03 -0700372struct afs_vlocation *afs_vlocation_lookup(struct afs_cell *cell,
David Howells00d3b7a2007-04-26 15:57:07 -0700373 struct key *key,
David Howells08e0e7c2007-04-26 15:55:03 -0700374 const char *name,
375 size_t namesz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
David Howells08e0e7c2007-04-26 15:55:03 -0700377 struct afs_vlocation *vl;
378 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
David Howells00d3b7a2007-04-26 15:57:07 -0700380 _enter("{%s},{%x},%*.*s,%zu",
381 cell->name, key_serial(key),
382 (int) namesz, (int) namesz, name, namesz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
David Howells08e0e7c2007-04-26 15:55:03 -0700384 if (namesz > sizeof(vl->vldb.name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 _leave(" = -ENAMETOOLONG");
David Howells08e0e7c2007-04-26 15:55:03 -0700386 return ERR_PTR(-ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388
David Howells08e0e7c2007-04-26 15:55:03 -0700389 /* see if we have an in-memory copy first */
390 down_write(&cell->vl_sem);
391 spin_lock(&cell->vl_lock);
392 list_for_each_entry(vl, &cell->vl_list, link) {
393 if (vl->vldb.name[namesz] != '\0')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 continue;
David Howells08e0e7c2007-04-26 15:55:03 -0700395 if (memcmp(vl->vldb.name, name, namesz) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 goto found_in_memory;
397 }
David Howells08e0e7c2007-04-26 15:55:03 -0700398 spin_unlock(&cell->vl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 /* not in the cell's in-memory lists - create a new record */
David Howells08e0e7c2007-04-26 15:55:03 -0700401 vl = afs_vlocation_alloc(cell, name, namesz);
402 if (!vl) {
403 up_write(&cell->vl_sem);
404 return ERR_PTR(-ENOMEM);
405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 afs_get_cell(cell);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
David Howells08e0e7c2007-04-26 15:55:03 -0700409 list_add_tail(&vl->link, &cell->vl_list);
410 vl->state = AFS_VL_CREATING;
411 up_write(&cell->vl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
David Howells08e0e7c2007-04-26 15:55:03 -0700413fill_in_record:
David Howells00d3b7a2007-04-26 15:57:07 -0700414 ret = afs_vlocation_fill_in_record(vl, key);
David Howells08e0e7c2007-04-26 15:55:03 -0700415 if (ret < 0)
416 goto error_abandon;
David S. Miller39bf0942007-04-26 20:39:14 -0700417 spin_lock(&vl->lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700418 vl->state = AFS_VL_VALID;
419 wake_up(&vl->waitq);
David S. Miller39bf0942007-04-26 20:39:14 -0700420 spin_unlock(&vl->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
David Howells08e0e7c2007-04-26 15:55:03 -0700422 /* schedule for regular updates */
423 afs_vlocation_queue_for_updates(vl);
424 goto success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
David Howellsec268152007-04-26 15:49:28 -0700426found_in_memory:
David Howells08e0e7c2007-04-26 15:55:03 -0700427 /* found in memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 _debug("found in memory");
David Howells08e0e7c2007-04-26 15:55:03 -0700429 atomic_inc(&vl->usage);
430 spin_unlock(&cell->vl_lock);
431 if (!list_empty(&vl->grave)) {
432 spin_lock(&afs_vlocation_graveyard_lock);
433 list_del_init(&vl->grave);
434 spin_unlock(&afs_vlocation_graveyard_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
David Howells08e0e7c2007-04-26 15:55:03 -0700436 up_write(&cell->vl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
David Howells08e0e7c2007-04-26 15:55:03 -0700438 /* see if it was an abandoned record that we might try filling in */
David S. Miller39bf0942007-04-26 20:39:14 -0700439 spin_lock(&vl->lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700440 while (vl->state != AFS_VL_VALID) {
441 afs_vlocation_state_t state = vl->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
David Howells08e0e7c2007-04-26 15:55:03 -0700443 _debug("invalid [state %d]", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
David Howells08e0e7c2007-04-26 15:55:03 -0700445 if ((state == AFS_VL_NEW || state == AFS_VL_NO_VOLUME)) {
David S. Miller39bf0942007-04-26 20:39:14 -0700446 vl->state = AFS_VL_CREATING;
447 spin_unlock(&vl->lock);
448 goto fill_in_record;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
David Howells08e0e7c2007-04-26 15:55:03 -0700451 /* must now wait for creation or update by someone else to
452 * complete */
453 _debug("wait");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
David S. Miller39bf0942007-04-26 20:39:14 -0700455 spin_unlock(&vl->lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700456 ret = wait_event_interruptible(
457 vl->waitq,
458 vl->state == AFS_VL_NEW ||
459 vl->state == AFS_VL_VALID ||
460 vl->state == AFS_VL_NO_VOLUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (ret < 0)
David Howells08e0e7c2007-04-26 15:55:03 -0700462 goto error;
David S. Miller39bf0942007-04-26 20:39:14 -0700463 spin_lock(&vl->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
David S. Miller39bf0942007-04-26 20:39:14 -0700465 spin_unlock(&vl->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
David Howells08e0e7c2007-04-26 15:55:03 -0700467success:
468 _leave(" = %p",vl);
469 return vl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
David Howells08e0e7c2007-04-26 15:55:03 -0700471error_abandon:
David S. Miller39bf0942007-04-26 20:39:14 -0700472 spin_lock(&vl->lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700473 vl->state = AFS_VL_NEW;
474 wake_up(&vl->waitq);
David S. Miller39bf0942007-04-26 20:39:14 -0700475 spin_unlock(&vl->lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700476error:
477 ASSERT(vl != NULL);
478 afs_put_vlocation(vl);
479 _leave(" = %d", ret);
480 return ERR_PTR(ret);
David Howellsec268152007-04-26 15:49:28 -0700481}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483/*
David Howells08e0e7c2007-04-26 15:55:03 -0700484 * finish using a volume location record
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 */
David Howells08e0e7c2007-04-26 15:55:03 -0700486void afs_put_vlocation(struct afs_vlocation *vl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
David Howells08e0e7c2007-04-26 15:55:03 -0700488 if (!vl)
489 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
David Howells08e0e7c2007-04-26 15:55:03 -0700491 _enter("%s", vl->vldb.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
David Howells08e0e7c2007-04-26 15:55:03 -0700493 ASSERTCMP(atomic_read(&vl->usage), >, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
David Howells08e0e7c2007-04-26 15:55:03 -0700495 if (likely(!atomic_dec_and_test(&vl->usage))) {
496 _leave("");
497 return;
498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
David Howells08e0e7c2007-04-26 15:55:03 -0700500 spin_lock(&afs_vlocation_graveyard_lock);
501 if (atomic_read(&vl->usage) == 0) {
502 _debug("buried");
503 list_move_tail(&vl->grave, &afs_vlocation_graveyard);
504 vl->time_of_death = get_seconds();
505 schedule_delayed_work(&afs_vlocation_reap,
506 afs_vlocation_timeout * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
David Howells08e0e7c2007-04-26 15:55:03 -0700508 /* suspend updates on this record */
509 if (!list_empty(&vl->update)) {
510 spin_lock(&afs_vlocation_updates_lock);
511 list_del_init(&vl->update);
512 spin_unlock(&afs_vlocation_updates_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514 }
David Howells08e0e7c2007-04-26 15:55:03 -0700515 spin_unlock(&afs_vlocation_graveyard_lock);
516 _leave(" [killed?]");
David Howellsec268152007-04-26 15:49:28 -0700517}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519/*
David Howells08e0e7c2007-04-26 15:55:03 -0700520 * destroy a dead volume location record
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 */
David Howells08e0e7c2007-04-26 15:55:03 -0700522static void afs_vlocation_destroy(struct afs_vlocation *vl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
David Howells08e0e7c2007-04-26 15:55:03 -0700524 _enter("%p", vl);
525
526#ifdef AFS_CACHING_SUPPORT
527 cachefs_relinquish_cookie(vl->cache, 0);
528#endif
529
530 afs_put_cell(vl->cell);
531 kfree(vl);
532}
533
534/*
535 * reap dead volume location records
536 */
537static void afs_vlocation_reaper(struct work_struct *work)
538{
539 LIST_HEAD(corpses);
540 struct afs_vlocation *vl;
541 unsigned long delay, expiry;
542 time_t now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 _enter("");
545
David Howells08e0e7c2007-04-26 15:55:03 -0700546 now = get_seconds();
547 spin_lock(&afs_vlocation_graveyard_lock);
548
549 while (!list_empty(&afs_vlocation_graveyard)) {
550 vl = list_entry(afs_vlocation_graveyard.next,
551 struct afs_vlocation, grave);
552
553 _debug("check %p", vl);
554
555 /* the queue is ordered most dead first */
556 expiry = vl->time_of_death + afs_vlocation_timeout;
557 if (expiry > now) {
558 delay = (expiry - now) * HZ;
559 _debug("delay %lu", delay);
560 if (!schedule_delayed_work(&afs_vlocation_reap,
561 delay)) {
562 cancel_delayed_work(&afs_vlocation_reap);
563 schedule_delayed_work(&afs_vlocation_reap,
564 delay);
565 }
566 break;
567 }
568
569 spin_lock(&vl->cell->vl_lock);
570 if (atomic_read(&vl->usage) > 0) {
571 _debug("no reap");
572 list_del_init(&vl->grave);
573 } else {
574 _debug("reap");
575 list_move_tail(&vl->grave, &corpses);
576 list_del_init(&vl->link);
577 }
578 spin_unlock(&vl->cell->vl_lock);
579 }
580
581 spin_unlock(&afs_vlocation_graveyard_lock);
582
583 /* now reap the corpses we've extracted */
584 while (!list_empty(&corpses)) {
585 vl = list_entry(corpses.next, struct afs_vlocation, grave);
586 list_del(&vl->grave);
587 afs_vlocation_destroy(vl);
588 }
589
590 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700591}
David Howells08e0e7c2007-04-26 15:55:03 -0700592
593/*
594 * initialise the VL update process
595 */
596int __init afs_vlocation_update_init(void)
597{
598 afs_vlocation_update_worker =
599 create_singlethread_workqueue("kafs_vlupdated");
600 return afs_vlocation_update_worker ? 0 : -ENOMEM;
601}
602
603/*
604 * discard all the volume location records for rmmod
605 */
606void __exit afs_vlocation_purge(void)
607{
608 afs_vlocation_timeout = 0;
609
610 spin_lock(&afs_vlocation_updates_lock);
611 list_del_init(&afs_vlocation_updates);
612 spin_unlock(&afs_vlocation_updates_lock);
613 cancel_delayed_work(&afs_vlocation_update);
614 queue_delayed_work(afs_vlocation_update_worker,
615 &afs_vlocation_update, 0);
616 destroy_workqueue(afs_vlocation_update_worker);
617
618 cancel_delayed_work(&afs_vlocation_reap);
619 schedule_delayed_work(&afs_vlocation_reap, 0);
620}
621
622/*
623 * update a volume location
624 */
625static void afs_vlocation_updater(struct work_struct *work)
626{
627 struct afs_cache_vlocation vldb;
628 struct afs_vlocation *vl, *xvl;
629 time_t now;
630 long timeout;
631 int ret;
632
633 _enter("");
634
635 now = get_seconds();
636
637 /* find a record to update */
638 spin_lock(&afs_vlocation_updates_lock);
639 for (;;) {
640 if (list_empty(&afs_vlocation_updates)) {
641 spin_unlock(&afs_vlocation_updates_lock);
642 _leave(" [nothing]");
643 return;
644 }
645
646 vl = list_entry(afs_vlocation_updates.next,
647 struct afs_vlocation, update);
648 if (atomic_read(&vl->usage) > 0)
649 break;
650 list_del_init(&vl->update);
651 }
652
653 timeout = vl->update_at - now;
654 if (timeout > 0) {
655 queue_delayed_work(afs_vlocation_update_worker,
656 &afs_vlocation_update, timeout * HZ);
657 spin_unlock(&afs_vlocation_updates_lock);
658 _leave(" [nothing]");
659 return;
660 }
661
662 list_del_init(&vl->update);
663 atomic_inc(&vl->usage);
664 spin_unlock(&afs_vlocation_updates_lock);
665
666 /* we can now perform the update */
667 _debug("update %s", vl->vldb.name);
668 vl->state = AFS_VL_UPDATING;
669 vl->upd_rej_cnt = 0;
670 vl->upd_busy_cnt = 0;
671
David Howells00d3b7a2007-04-26 15:57:07 -0700672 ret = afs_vlocation_update_record(vl, NULL, &vldb);
David S. Miller39bf0942007-04-26 20:39:14 -0700673 spin_lock(&vl->lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700674 switch (ret) {
675 case 0:
676 afs_vlocation_apply_update(vl, &vldb);
677 vl->state = AFS_VL_VALID;
David S. Miller39bf0942007-04-26 20:39:14 -0700678 wake_up(&vl->waitq);
David Howells08e0e7c2007-04-26 15:55:03 -0700679 break;
680 case -ENOMEDIUM:
681 vl->state = AFS_VL_VOLUME_DELETED;
682 break;
683 default:
684 vl->state = AFS_VL_UNCERTAIN;
685 break;
686 }
David S. Miller39bf0942007-04-26 20:39:14 -0700687 spin_unlock(&vl->lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700688
689 /* and then reschedule */
690 _debug("reschedule");
691 vl->update_at = get_seconds() + afs_vlocation_update_timeout;
692
693 spin_lock(&afs_vlocation_updates_lock);
694
695 if (!list_empty(&afs_vlocation_updates)) {
696 /* next update in 10 minutes, but wait at least 1 second more
697 * than the newest record already queued so that we don't spam
698 * the VL server suddenly with lots of requests
699 */
700 xvl = list_entry(afs_vlocation_updates.prev,
701 struct afs_vlocation, update);
702 if (vl->update_at <= xvl->update_at)
703 vl->update_at = xvl->update_at + 1;
704 xvl = list_entry(afs_vlocation_updates.next,
705 struct afs_vlocation, update);
706 timeout = xvl->update_at - now;
707 if (timeout < 0)
708 timeout = 0;
709 } else {
710 timeout = afs_vlocation_update_timeout;
711 }
712
713 ASSERT(list_empty(&vl->update));
714
715 list_add_tail(&vl->update, &afs_vlocation_updates);
716
717 _debug("timeout %ld", timeout);
718 queue_delayed_work(afs_vlocation_update_worker,
719 &afs_vlocation_update, timeout * HZ);
720 spin_unlock(&afs_vlocation_updates_lock);
721 afs_put_vlocation(vl);
722}