blob: 60cb2f408c75a1d70d4e66c763d1ed2481c76e0d [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,
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 struct afs_cache_vlocation *vldb)
37{
David Howells08e0e7c2007-04-26 15:55:03 -070038 struct afs_cell *cell = vl->cell;
39 struct in_addr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 int count, ret;
41
David Howells08e0e7c2007-04-26 15:55:03 -070042 _enter("%s,%s", cell->name, vl->vldb.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
David Howells08e0e7c2007-04-26 15:55:03 -070044 down_write(&vl->cell->vl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 ret = -ENOMEDIUM;
46 for (count = cell->vl_naddrs; count > 0; count--) {
David Howells08e0e7c2007-04-26 15:55:03 -070047 addr = cell->vl_addrs[cell->vl_curr_svix];
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
David Howells08e0e7c2007-04-26 15:55:03 -070049 _debug("CellServ[%hu]: %08x", cell->vl_curr_svix, addr.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51 /* attempt to access the VL server */
David Howells08e0e7c2007-04-26 15:55:03 -070052 ret = afs_vl_get_entry_by_name(&addr, vl->vldb.name, vldb,
53 &afs_sync_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 switch (ret) {
55 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 goto out;
57 case -ENOMEM:
58 case -ENONET:
59 case -ENETUNREACH:
60 case -EHOSTUNREACH:
61 case -ECONNREFUSED:
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 if (ret == -ENOMEM || ret == -ENONET)
63 goto out;
64 goto rotate;
65 case -ENOMEDIUM:
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 goto out;
67 default:
David Howells08e0e7c2007-04-26 15:55:03 -070068 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 goto rotate;
70 }
71
72 /* rotate the server records upon lookup failure */
73 rotate:
74 cell->vl_curr_svix++;
75 cell->vl_curr_svix %= cell->vl_naddrs;
76 }
77
David Howellsec268152007-04-26 15:49:28 -070078out:
David Howells08e0e7c2007-04-26 15:55:03 -070079 up_write(&vl->cell->vl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 _leave(" = %d", ret);
81 return ret;
David Howellsec268152007-04-26 15:49:28 -070082}
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/*
85 * iterate through the VL servers in a cell until one of them admits knowing
86 * about the volume in question
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 */
David Howells08e0e7c2007-04-26 15:55:03 -070088static int afs_vlocation_access_vl_by_id(struct afs_vlocation *vl,
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 afs_volid_t volid,
90 afs_voltype_t voltype,
91 struct afs_cache_vlocation *vldb)
92{
David Howells08e0e7c2007-04-26 15:55:03 -070093 struct afs_cell *cell = vl->cell;
94 struct in_addr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 int count, ret;
96
97 _enter("%s,%x,%d,", cell->name, volid, voltype);
98
David Howells08e0e7c2007-04-26 15:55:03 -070099 down_write(&vl->cell->vl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 ret = -ENOMEDIUM;
101 for (count = cell->vl_naddrs; count > 0; count--) {
David Howells08e0e7c2007-04-26 15:55:03 -0700102 addr = cell->vl_addrs[cell->vl_curr_svix];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
David Howells08e0e7c2007-04-26 15:55:03 -0700104 _debug("CellServ[%hu]: %08x", cell->vl_curr_svix, addr.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 /* attempt to access the VL server */
David Howells08e0e7c2007-04-26 15:55:03 -0700107 ret = afs_vl_get_entry_by_id(&addr, volid, voltype, vldb,
108 &afs_sync_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 switch (ret) {
110 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 goto out;
112 case -ENOMEM:
113 case -ENONET:
114 case -ENETUNREACH:
115 case -EHOSTUNREACH:
116 case -ECONNREFUSED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (ret == -ENOMEM || ret == -ENONET)
118 goto out;
119 goto rotate;
David Howells08e0e7c2007-04-26 15:55:03 -0700120 case -EBUSY:
121 vl->upd_busy_cnt++;
122 if (vl->upd_busy_cnt <= 3) {
123 if (vl->upd_busy_cnt > 1) {
124 /* second+ BUSY - sleep a little bit */
125 set_current_state(TASK_UNINTERRUPTIBLE);
126 schedule_timeout(1);
127 __set_current_state(TASK_RUNNING);
128 }
129 continue;
130 }
131 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 case -ENOMEDIUM:
David Howells08e0e7c2007-04-26 15:55:03 -0700133 vl->upd_rej_cnt++;
134 goto rotate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 default:
David Howells08e0e7c2007-04-26 15:55:03 -0700136 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 goto rotate;
138 }
139
140 /* rotate the server records upon lookup failure */
141 rotate:
142 cell->vl_curr_svix++;
143 cell->vl_curr_svix %= cell->vl_naddrs;
David Howells08e0e7c2007-04-26 15:55:03 -0700144 vl->upd_busy_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146
David Howellsec268152007-04-26 15:49:28 -0700147out:
David Howells08e0e7c2007-04-26 15:55:03 -0700148 if (ret < 0 && vl->upd_rej_cnt > 0) {
149 printk(KERN_NOTICE "kAFS:"
150 " Active volume no longer valid '%s'\n",
151 vl->vldb.name);
152 vl->valid = 0;
153 ret = -ENOMEDIUM;
154 }
155
156 up_write(&vl->cell->vl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 _leave(" = %d", ret);
158 return ret;
David Howellsec268152007-04-26 15:49:28 -0700159}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161/*
David Howells08e0e7c2007-04-26 15:55:03 -0700162 * allocate a volume location record
163 */
164static struct afs_vlocation *afs_vlocation_alloc(struct afs_cell *cell,
165 const char *name,
166 size_t namesz)
167{
168 struct afs_vlocation *vl;
169
170 vl = kzalloc(sizeof(struct afs_vlocation), GFP_KERNEL);
171 if (vl) {
172 vl->cell = cell;
173 vl->state = AFS_VL_NEW;
174 atomic_set(&vl->usage, 1);
175 INIT_LIST_HEAD(&vl->link);
176 INIT_LIST_HEAD(&vl->grave);
177 INIT_LIST_HEAD(&vl->update);
178 init_waitqueue_head(&vl->waitq);
179 rwlock_init(&vl->lock);
180 memcpy(vl->vldb.name, name, namesz);
181 }
182
183 _leave(" = %p", vl);
184 return vl;
185}
186
187/*
188 * update record if we found it in the cache
189 */
190static int afs_vlocation_update_record(struct afs_vlocation *vl,
191 struct afs_cache_vlocation *vldb)
192{
193 afs_voltype_t voltype;
194 afs_volid_t vid;
195 int ret;
196
197 /* try to look up a cached volume in the cell VL databases by ID */
198 _debug("Locally Cached: %s %02x { %08x(%x) %08x(%x) %08x(%x) }",
199 vl->vldb.name,
200 vl->vldb.vidmask,
201 ntohl(vl->vldb.servers[0].s_addr),
202 vl->vldb.srvtmask[0],
203 ntohl(vl->vldb.servers[1].s_addr),
204 vl->vldb.srvtmask[1],
205 ntohl(vl->vldb.servers[2].s_addr),
206 vl->vldb.srvtmask[2]);
207
208 _debug("Vids: %08x %08x %08x",
209 vl->vldb.vid[0],
210 vl->vldb.vid[1],
211 vl->vldb.vid[2]);
212
213 if (vl->vldb.vidmask & AFS_VOL_VTM_RW) {
214 vid = vl->vldb.vid[0];
215 voltype = AFSVL_RWVOL;
216 } else if (vl->vldb.vidmask & AFS_VOL_VTM_RO) {
217 vid = vl->vldb.vid[1];
218 voltype = AFSVL_ROVOL;
219 } else if (vl->vldb.vidmask & AFS_VOL_VTM_BAK) {
220 vid = vl->vldb.vid[2];
221 voltype = AFSVL_BACKVOL;
222 } else {
223 BUG();
224 vid = 0;
225 voltype = 0;
226 }
227
228 /* contact the server to make sure the volume is still available
229 * - TODO: need to handle disconnected operation here
230 */
231 ret = afs_vlocation_access_vl_by_id(vl, vid, voltype, vldb);
232 switch (ret) {
233 /* net error */
234 default:
235 printk(KERN_WARNING "kAFS:"
236 " failed to update volume '%s' (%x) up in '%s': %d\n",
237 vl->vldb.name, vid, vl->cell->name, ret);
238 _leave(" = %d", ret);
239 return ret;
240
241 /* pulled from local cache into memory */
242 case 0:
243 _leave(" = 0");
244 return 0;
245
246 /* uh oh... looks like the volume got deleted */
247 case -ENOMEDIUM:
248 printk(KERN_ERR "kAFS:"
249 " volume '%s' (%x) does not exist '%s'\n",
250 vl->vldb.name, vid, vl->cell->name);
251
252 /* TODO: make existing record unavailable */
253 _leave(" = %d", ret);
254 return ret;
255 }
256}
257
258/*
259 * apply the update to a VL record
260 */
261static void afs_vlocation_apply_update(struct afs_vlocation *vl,
262 struct afs_cache_vlocation *vldb)
263{
264 _debug("Done VL Lookup: %s %02x { %08x(%x) %08x(%x) %08x(%x) }",
265 vldb->name, vldb->vidmask,
266 ntohl(vldb->servers[0].s_addr), vldb->srvtmask[0],
267 ntohl(vldb->servers[1].s_addr), vldb->srvtmask[1],
268 ntohl(vldb->servers[2].s_addr), vldb->srvtmask[2]);
269
270 _debug("Vids: %08x %08x %08x",
271 vldb->vid[0], vldb->vid[1], vldb->vid[2]);
272
273 if (strcmp(vldb->name, vl->vldb.name) != 0)
274 printk(KERN_NOTICE "kAFS:"
275 " name of volume '%s' changed to '%s' on server\n",
276 vl->vldb.name, vldb->name);
277
278 vl->vldb = *vldb;
279
280#ifdef AFS_CACHING_SUPPORT
281 /* update volume entry in local cache */
282 cachefs_update_cookie(vl->cache);
283#endif
284}
285
286/*
287 * fill in a volume location record, consulting the cache and the VL server
288 * both
289 */
290static int afs_vlocation_fill_in_record(struct afs_vlocation *vl)
291{
292 struct afs_cache_vlocation vldb;
293 int ret;
294
295 _enter("");
296
297 ASSERTCMP(vl->valid, ==, 0);
298
299 memset(&vldb, 0, sizeof(vldb));
300
301 /* see if we have an in-cache copy (will set vl->valid if there is) */
302#ifdef AFS_CACHING_SUPPORT
303 cachefs_acquire_cookie(cell->cache,
304 &afs_volume_cache_index_def,
305 vlocation,
306 &vl->cache);
307#endif
308
309 if (vl->valid) {
310 /* try to update a known volume in the cell VL databases by
311 * ID as the name may have changed */
312 _debug("found in cache");
313 ret = afs_vlocation_update_record(vl, &vldb);
314 } else {
315 /* try to look up an unknown volume in the cell VL databases by
316 * name */
317 ret = afs_vlocation_access_vl_by_name(vl, &vldb);
318 if (ret < 0) {
319 printk("kAFS: failed to locate '%s' in cell '%s'\n",
320 vl->vldb.name, vl->cell->name);
321 return ret;
322 }
323 }
324
325 afs_vlocation_apply_update(vl, &vldb);
326 _leave(" = 0");
327 return 0;
328}
329
330/*
331 * queue a vlocation record for updates
332 */
333void afs_vlocation_queue_for_updates(struct afs_vlocation *vl)
334{
335 struct afs_vlocation *xvl;
336
337 /* wait at least 10 minutes before updating... */
338 vl->update_at = get_seconds() + afs_vlocation_update_timeout;
339
340 spin_lock(&afs_vlocation_updates_lock);
341
342 if (!list_empty(&afs_vlocation_updates)) {
343 /* ... but wait at least 1 second more than the newest record
344 * already queued so that we don't spam the VL server suddenly
345 * with lots of requests
346 */
347 xvl = list_entry(afs_vlocation_updates.prev,
348 struct afs_vlocation, update);
349 if (vl->update_at <= xvl->update_at)
350 vl->update_at = xvl->update_at + 1;
351 } else {
352 queue_delayed_work(afs_vlocation_update_worker,
353 &afs_vlocation_update,
354 afs_vlocation_update_timeout * HZ);
355 }
356
357 list_add_tail(&vl->update, &afs_vlocation_updates);
358 spin_unlock(&afs_vlocation_updates_lock);
359}
360
361/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 * lookup volume location
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 * - iterate through the VL servers in a cell until one of them admits knowing
364 * about the volume in question
365 * - lookup in the local cache if not able to find on the VL server
366 * - insert/update in the local cache if did get a VL response
367 */
David Howells08e0e7c2007-04-26 15:55:03 -0700368struct afs_vlocation *afs_vlocation_lookup(struct afs_cell *cell,
369 const char *name,
370 size_t namesz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
David Howells08e0e7c2007-04-26 15:55:03 -0700372 struct afs_vlocation *vl;
373 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
David Howells08e0e7c2007-04-26 15:55:03 -0700375 _enter("{%s},%*.*s,%zu",
376 cell->name, (int) namesz, (int) namesz, name, namesz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
David Howells08e0e7c2007-04-26 15:55:03 -0700378 if (namesz > sizeof(vl->vldb.name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 _leave(" = -ENAMETOOLONG");
David Howells08e0e7c2007-04-26 15:55:03 -0700380 return ERR_PTR(-ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382
David Howells08e0e7c2007-04-26 15:55:03 -0700383 /* see if we have an in-memory copy first */
384 down_write(&cell->vl_sem);
385 spin_lock(&cell->vl_lock);
386 list_for_each_entry(vl, &cell->vl_list, link) {
387 if (vl->vldb.name[namesz] != '\0')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 continue;
David Howells08e0e7c2007-04-26 15:55:03 -0700389 if (memcmp(vl->vldb.name, name, namesz) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 goto found_in_memory;
391 }
David Howells08e0e7c2007-04-26 15:55:03 -0700392 spin_unlock(&cell->vl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 /* not in the cell's in-memory lists - create a new record */
David Howells08e0e7c2007-04-26 15:55:03 -0700395 vl = afs_vlocation_alloc(cell, name, namesz);
396 if (!vl) {
397 up_write(&cell->vl_sem);
398 return ERR_PTR(-ENOMEM);
399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 afs_get_cell(cell);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
David Howells08e0e7c2007-04-26 15:55:03 -0700403 list_add_tail(&vl->link, &cell->vl_list);
404 vl->state = AFS_VL_CREATING;
405 up_write(&cell->vl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
David Howells08e0e7c2007-04-26 15:55:03 -0700407fill_in_record:
408 ret = afs_vlocation_fill_in_record(vl);
409 if (ret < 0)
410 goto error_abandon;
411 vl->state = AFS_VL_VALID;
412 wake_up(&vl->waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
David Howells08e0e7c2007-04-26 15:55:03 -0700414 /* schedule for regular updates */
415 afs_vlocation_queue_for_updates(vl);
416 goto success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
David Howellsec268152007-04-26 15:49:28 -0700418found_in_memory:
David Howells08e0e7c2007-04-26 15:55:03 -0700419 /* found in memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 _debug("found in memory");
David Howells08e0e7c2007-04-26 15:55:03 -0700421 atomic_inc(&vl->usage);
422 spin_unlock(&cell->vl_lock);
423 if (!list_empty(&vl->grave)) {
424 spin_lock(&afs_vlocation_graveyard_lock);
425 list_del_init(&vl->grave);
426 spin_unlock(&afs_vlocation_graveyard_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
David Howells08e0e7c2007-04-26 15:55:03 -0700428 up_write(&cell->vl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
David Howells08e0e7c2007-04-26 15:55:03 -0700430 /* see if it was an abandoned record that we might try filling in */
431 while (vl->state != AFS_VL_VALID) {
432 afs_vlocation_state_t state = vl->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
David Howells08e0e7c2007-04-26 15:55:03 -0700434 _debug("invalid [state %d]", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
David Howells08e0e7c2007-04-26 15:55:03 -0700436 if ((state == AFS_VL_NEW || state == AFS_VL_NO_VOLUME)) {
437 if (cmpxchg(&vl->state, state, AFS_VL_CREATING) ==
438 state)
439 goto fill_in_record;
440 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
David Howells08e0e7c2007-04-26 15:55:03 -0700443 /* must now wait for creation or update by someone else to
444 * complete */
445 _debug("wait");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
David Howells08e0e7c2007-04-26 15:55:03 -0700447 ret = wait_event_interruptible(
448 vl->waitq,
449 vl->state == AFS_VL_NEW ||
450 vl->state == AFS_VL_VALID ||
451 vl->state == AFS_VL_NO_VOLUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (ret < 0)
David Howells08e0e7c2007-04-26 15:55:03 -0700453 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455
David Howells08e0e7c2007-04-26 15:55:03 -0700456success:
457 _leave(" = %p",vl);
458 return vl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
David Howells08e0e7c2007-04-26 15:55:03 -0700460error_abandon:
461 vl->state = AFS_VL_NEW;
462 wake_up(&vl->waitq);
463error:
464 ASSERT(vl != NULL);
465 afs_put_vlocation(vl);
466 _leave(" = %d", ret);
467 return ERR_PTR(ret);
David Howellsec268152007-04-26 15:49:28 -0700468}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470/*
David Howells08e0e7c2007-04-26 15:55:03 -0700471 * finish using a volume location record
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 */
David Howells08e0e7c2007-04-26 15:55:03 -0700473void afs_put_vlocation(struct afs_vlocation *vl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
David Howells08e0e7c2007-04-26 15:55:03 -0700475 if (!vl)
476 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
David Howells08e0e7c2007-04-26 15:55:03 -0700478 _enter("%s", vl->vldb.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
David Howells08e0e7c2007-04-26 15:55:03 -0700480 ASSERTCMP(atomic_read(&vl->usage), >, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
David Howells08e0e7c2007-04-26 15:55:03 -0700482 if (likely(!atomic_dec_and_test(&vl->usage))) {
483 _leave("");
484 return;
485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
David Howells08e0e7c2007-04-26 15:55:03 -0700487 spin_lock(&afs_vlocation_graveyard_lock);
488 if (atomic_read(&vl->usage) == 0) {
489 _debug("buried");
490 list_move_tail(&vl->grave, &afs_vlocation_graveyard);
491 vl->time_of_death = get_seconds();
492 schedule_delayed_work(&afs_vlocation_reap,
493 afs_vlocation_timeout * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
David Howells08e0e7c2007-04-26 15:55:03 -0700495 /* suspend updates on this record */
496 if (!list_empty(&vl->update)) {
497 spin_lock(&afs_vlocation_updates_lock);
498 list_del_init(&vl->update);
499 spin_unlock(&afs_vlocation_updates_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501 }
David Howells08e0e7c2007-04-26 15:55:03 -0700502 spin_unlock(&afs_vlocation_graveyard_lock);
503 _leave(" [killed?]");
David Howellsec268152007-04-26 15:49:28 -0700504}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506/*
David Howells08e0e7c2007-04-26 15:55:03 -0700507 * destroy a dead volume location record
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 */
David Howells08e0e7c2007-04-26 15:55:03 -0700509static void afs_vlocation_destroy(struct afs_vlocation *vl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
David Howells08e0e7c2007-04-26 15:55:03 -0700511 _enter("%p", vl);
512
513#ifdef AFS_CACHING_SUPPORT
514 cachefs_relinquish_cookie(vl->cache, 0);
515#endif
516
517 afs_put_cell(vl->cell);
518 kfree(vl);
519}
520
521/*
522 * reap dead volume location records
523 */
524static void afs_vlocation_reaper(struct work_struct *work)
525{
526 LIST_HEAD(corpses);
527 struct afs_vlocation *vl;
528 unsigned long delay, expiry;
529 time_t now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 _enter("");
532
David Howells08e0e7c2007-04-26 15:55:03 -0700533 now = get_seconds();
534 spin_lock(&afs_vlocation_graveyard_lock);
535
536 while (!list_empty(&afs_vlocation_graveyard)) {
537 vl = list_entry(afs_vlocation_graveyard.next,
538 struct afs_vlocation, grave);
539
540 _debug("check %p", vl);
541
542 /* the queue is ordered most dead first */
543 expiry = vl->time_of_death + afs_vlocation_timeout;
544 if (expiry > now) {
545 delay = (expiry - now) * HZ;
546 _debug("delay %lu", delay);
547 if (!schedule_delayed_work(&afs_vlocation_reap,
548 delay)) {
549 cancel_delayed_work(&afs_vlocation_reap);
550 schedule_delayed_work(&afs_vlocation_reap,
551 delay);
552 }
553 break;
554 }
555
556 spin_lock(&vl->cell->vl_lock);
557 if (atomic_read(&vl->usage) > 0) {
558 _debug("no reap");
559 list_del_init(&vl->grave);
560 } else {
561 _debug("reap");
562 list_move_tail(&vl->grave, &corpses);
563 list_del_init(&vl->link);
564 }
565 spin_unlock(&vl->cell->vl_lock);
566 }
567
568 spin_unlock(&afs_vlocation_graveyard_lock);
569
570 /* now reap the corpses we've extracted */
571 while (!list_empty(&corpses)) {
572 vl = list_entry(corpses.next, struct afs_vlocation, grave);
573 list_del(&vl->grave);
574 afs_vlocation_destroy(vl);
575 }
576
577 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700578}
David Howells08e0e7c2007-04-26 15:55:03 -0700579
580/*
581 * initialise the VL update process
582 */
583int __init afs_vlocation_update_init(void)
584{
585 afs_vlocation_update_worker =
586 create_singlethread_workqueue("kafs_vlupdated");
587 return afs_vlocation_update_worker ? 0 : -ENOMEM;
588}
589
590/*
591 * discard all the volume location records for rmmod
592 */
593void __exit afs_vlocation_purge(void)
594{
595 afs_vlocation_timeout = 0;
596
597 spin_lock(&afs_vlocation_updates_lock);
598 list_del_init(&afs_vlocation_updates);
599 spin_unlock(&afs_vlocation_updates_lock);
600 cancel_delayed_work(&afs_vlocation_update);
601 queue_delayed_work(afs_vlocation_update_worker,
602 &afs_vlocation_update, 0);
603 destroy_workqueue(afs_vlocation_update_worker);
604
605 cancel_delayed_work(&afs_vlocation_reap);
606 schedule_delayed_work(&afs_vlocation_reap, 0);
607}
608
609/*
610 * update a volume location
611 */
612static void afs_vlocation_updater(struct work_struct *work)
613{
614 struct afs_cache_vlocation vldb;
615 struct afs_vlocation *vl, *xvl;
616 time_t now;
617 long timeout;
618 int ret;
619
620 _enter("");
621
622 now = get_seconds();
623
624 /* find a record to update */
625 spin_lock(&afs_vlocation_updates_lock);
626 for (;;) {
627 if (list_empty(&afs_vlocation_updates)) {
628 spin_unlock(&afs_vlocation_updates_lock);
629 _leave(" [nothing]");
630 return;
631 }
632
633 vl = list_entry(afs_vlocation_updates.next,
634 struct afs_vlocation, update);
635 if (atomic_read(&vl->usage) > 0)
636 break;
637 list_del_init(&vl->update);
638 }
639
640 timeout = vl->update_at - now;
641 if (timeout > 0) {
642 queue_delayed_work(afs_vlocation_update_worker,
643 &afs_vlocation_update, timeout * HZ);
644 spin_unlock(&afs_vlocation_updates_lock);
645 _leave(" [nothing]");
646 return;
647 }
648
649 list_del_init(&vl->update);
650 atomic_inc(&vl->usage);
651 spin_unlock(&afs_vlocation_updates_lock);
652
653 /* we can now perform the update */
654 _debug("update %s", vl->vldb.name);
655 vl->state = AFS_VL_UPDATING;
656 vl->upd_rej_cnt = 0;
657 vl->upd_busy_cnt = 0;
658
659 ret = afs_vlocation_update_record(vl, &vldb);
660 switch (ret) {
661 case 0:
662 afs_vlocation_apply_update(vl, &vldb);
663 vl->state = AFS_VL_VALID;
664 break;
665 case -ENOMEDIUM:
666 vl->state = AFS_VL_VOLUME_DELETED;
667 break;
668 default:
669 vl->state = AFS_VL_UNCERTAIN;
670 break;
671 }
672
673 /* and then reschedule */
674 _debug("reschedule");
675 vl->update_at = get_seconds() + afs_vlocation_update_timeout;
676
677 spin_lock(&afs_vlocation_updates_lock);
678
679 if (!list_empty(&afs_vlocation_updates)) {
680 /* next update in 10 minutes, but wait at least 1 second more
681 * than the newest record already queued so that we don't spam
682 * the VL server suddenly with lots of requests
683 */
684 xvl = list_entry(afs_vlocation_updates.prev,
685 struct afs_vlocation, update);
686 if (vl->update_at <= xvl->update_at)
687 vl->update_at = xvl->update_at + 1;
688 xvl = list_entry(afs_vlocation_updates.next,
689 struct afs_vlocation, update);
690 timeout = xvl->update_at - now;
691 if (timeout < 0)
692 timeout = 0;
693 } else {
694 timeout = afs_vlocation_update_timeout;
695 }
696
697 ASSERT(list_empty(&vl->update));
698
699 list_add_tail(&vl->update, &afs_vlocation_updates);
700
701 _debug("timeout %ld", timeout);
702 queue_delayed_work(afs_vlocation_update_worker,
703 &afs_vlocation_update, timeout * HZ);
704 spin_unlock(&afs_vlocation_updates_lock);
705 afs_put_vlocation(vl);
706}