1 /* AFS volume location management
3 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
17 unsigned afs_vlocation_timeout = 10; /* volume location timeout in seconds */
18 unsigned afs_vlocation_update_timeout = 10 * 60;
20 static void afs_vlocation_reaper(struct work_struct *);
21 static void afs_vlocation_updater(struct work_struct *);
23 static LIST_HEAD(afs_vlocation_updates);
24 static LIST_HEAD(afs_vlocation_graveyard);
25 static DEFINE_SPINLOCK(afs_vlocation_updates_lock);
26 static DEFINE_SPINLOCK(afs_vlocation_graveyard_lock);
27 static DECLARE_DELAYED_WORK(afs_vlocation_reap, afs_vlocation_reaper);
28 static DECLARE_DELAYED_WORK(afs_vlocation_update, afs_vlocation_updater);
29 static struct workqueue_struct *afs_vlocation_update_worker;
32 * iterate through the VL servers in a cell until one of them admits knowing
33 * about the volume in question
35 static int afs_vlocation_access_vl_by_name(struct afs_vlocation *vl,
37 struct afs_cache_vlocation *vldb)
39 struct afs_cell *cell = vl->cell;
43 _enter("%s,%s", cell->name, vl->vldb.name);
45 down_write(&vl->cell->vl_sem);
47 for (count = cell->vl_naddrs; count > 0; count--) {
48 addr = cell->vl_addrs[cell->vl_curr_svix];
50 _debug("CellServ[%hu]: %08x", cell->vl_curr_svix, addr.s_addr);
52 /* attempt to access the VL server */
53 ret = afs_vl_get_entry_by_name(&addr, key, vl->vldb.name, vldb,
63 if (ret == -ENOMEM || ret == -ENONET)
73 /* rotate the server records upon lookup failure */
76 cell->vl_curr_svix %= cell->vl_naddrs;
80 up_write(&vl->cell->vl_sem);
86 * iterate through the VL servers in a cell until one of them admits knowing
87 * about the volume in question
89 static int afs_vlocation_access_vl_by_id(struct afs_vlocation *vl,
92 afs_voltype_t voltype,
93 struct afs_cache_vlocation *vldb)
95 struct afs_cell *cell = vl->cell;
99 _enter("%s,%x,%d,", cell->name, volid, voltype);
101 down_write(&vl->cell->vl_sem);
103 for (count = cell->vl_naddrs; count > 0; count--) {
104 addr = cell->vl_addrs[cell->vl_curr_svix];
106 _debug("CellServ[%hu]: %08x", cell->vl_curr_svix, addr.s_addr);
108 /* attempt to access the VL server */
109 ret = afs_vl_get_entry_by_id(&addr, key, volid, voltype, vldb,
119 if (ret == -ENOMEM || ret == -ENONET)
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);
129 __set_current_state(TASK_RUNNING);
142 /* rotate the server records upon lookup failure */
144 cell->vl_curr_svix++;
145 cell->vl_curr_svix %= cell->vl_naddrs;
146 vl->upd_busy_cnt = 0;
150 if (ret < 0 && vl->upd_rej_cnt > 0) {
151 printk(KERN_NOTICE "kAFS:"
152 " Active volume no longer valid '%s'\n",
158 up_write(&vl->cell->vl_sem);
159 _leave(" = %d", ret);
164 * allocate a volume location record
166 static struct afs_vlocation *afs_vlocation_alloc(struct afs_cell *cell,
170 struct afs_vlocation *vl;
172 vl = kzalloc(sizeof(struct afs_vlocation), GFP_KERNEL);
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);
181 spin_lock_init(&vl->lock);
182 memcpy(vl->vldb.name, name, namesz);
190 * update record if we found it in the cache
192 static int afs_vlocation_update_record(struct afs_vlocation *vl,
194 struct afs_cache_vlocation *vldb)
196 afs_voltype_t voltype;
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) }",
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]);
211 _debug("Vids: %08x %08x %08x",
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;
231 /* contact the server to make sure the volume is still available
232 * - TODO: need to handle disconnected operation here
234 ret = afs_vlocation_access_vl_by_id(vl, key, vid, voltype, vldb);
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);
244 /* pulled from local cache into memory */
249 /* uh oh... looks like the volume got deleted */
251 printk(KERN_ERR "kAFS:"
252 " volume '%s' (%x) does not exist '%s'\n",
253 vl->vldb.name, vid, vl->cell->name);
255 /* TODO: make existing record unavailable */
256 _leave(" = %d", ret);
262 * apply the update to a VL record
264 static void afs_vlocation_apply_update(struct afs_vlocation *vl,
265 struct afs_cache_vlocation *vldb)
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]);
273 _debug("Vids: %08x %08x %08x",
274 vldb->vid[0], vldb->vid[1], vldb->vid[2]);
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);
283 #ifdef AFS_CACHING_SUPPORT
284 /* update volume entry in local cache */
285 cachefs_update_cookie(vl->cache);
290 * fill in a volume location record, consulting the cache and the VL server
293 static int afs_vlocation_fill_in_record(struct afs_vlocation *vl,
296 struct afs_cache_vlocation vldb;
301 ASSERTCMP(vl->valid, ==, 0);
303 memset(&vldb, 0, sizeof(vldb));
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,
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");
317 ret = afs_vlocation_update_record(vl, key, &vldb);
319 /* try to look up an unknown volume in the cell VL databases by
321 ret = afs_vlocation_access_vl_by_name(vl, key, &vldb);
323 printk("kAFS: failed to locate '%s' in cell '%s'\n",
324 vl->vldb.name, vl->cell->name);
329 afs_vlocation_apply_update(vl, &vldb);
335 * queue a vlocation record for updates
337 void afs_vlocation_queue_for_updates(struct afs_vlocation *vl)
339 struct afs_vlocation *xvl;
341 /* wait at least 10 minutes before updating... */
342 vl->update_at = get_seconds() + afs_vlocation_update_timeout;
344 spin_lock(&afs_vlocation_updates_lock);
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
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;
356 queue_delayed_work(afs_vlocation_update_worker,
357 &afs_vlocation_update,
358 afs_vlocation_update_timeout * HZ);
361 list_add_tail(&vl->update, &afs_vlocation_updates);
362 spin_unlock(&afs_vlocation_updates_lock);
366 * lookup volume location
367 * - 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
372 struct afs_vlocation *afs_vlocation_lookup(struct afs_cell *cell,
377 struct afs_vlocation *vl;
380 _enter("{%s},{%x},%*.*s,%zu",
381 cell->name, key_serial(key),
382 (int) namesz, (int) namesz, name, namesz);
384 if (namesz > sizeof(vl->vldb.name)) {
385 _leave(" = -ENAMETOOLONG");
386 return ERR_PTR(-ENAMETOOLONG);
389 /* 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')
395 if (memcmp(vl->vldb.name, name, namesz) == 0)
396 goto found_in_memory;
398 spin_unlock(&cell->vl_lock);
400 /* not in the cell's in-memory lists - create a new record */
401 vl = afs_vlocation_alloc(cell, name, namesz);
403 up_write(&cell->vl_sem);
404 return ERR_PTR(-ENOMEM);
409 list_add_tail(&vl->link, &cell->vl_list);
410 vl->state = AFS_VL_CREATING;
411 up_write(&cell->vl_sem);
414 ret = afs_vlocation_fill_in_record(vl, key);
417 spin_lock(&vl->lock);
418 vl->state = AFS_VL_VALID;
420 spin_unlock(&vl->lock);
422 /* schedule for regular updates */
423 afs_vlocation_queue_for_updates(vl);
427 /* found in memory */
428 _debug("found in memory");
429 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);
436 up_write(&cell->vl_sem);
438 /* see if it was an abandoned record that we might try filling in */
439 spin_lock(&vl->lock);
440 while (vl->state != AFS_VL_VALID) {
441 afs_vlocation_state_t state = vl->state;
443 _debug("invalid [state %d]", state);
445 if ((state == AFS_VL_NEW || state == AFS_VL_NO_VOLUME)) {
446 vl->state = AFS_VL_CREATING;
447 spin_unlock(&vl->lock);
451 /* must now wait for creation or update by someone else to
455 spin_unlock(&vl->lock);
456 ret = wait_event_interruptible(
458 vl->state == AFS_VL_NEW ||
459 vl->state == AFS_VL_VALID ||
460 vl->state == AFS_VL_NO_VOLUME);
463 spin_lock(&vl->lock);
465 spin_unlock(&vl->lock);
472 spin_lock(&vl->lock);
473 vl->state = AFS_VL_NEW;
475 spin_unlock(&vl->lock);
478 afs_put_vlocation(vl);
479 _leave(" = %d", ret);
484 * finish using a volume location record
486 void afs_put_vlocation(struct afs_vlocation *vl)
491 _enter("%s", vl->vldb.name);
493 ASSERTCMP(atomic_read(&vl->usage), >, 0);
495 if (likely(!atomic_dec_and_test(&vl->usage))) {
500 spin_lock(&afs_vlocation_graveyard_lock);
501 if (atomic_read(&vl->usage) == 0) {
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);
508 /* 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);
515 spin_unlock(&afs_vlocation_graveyard_lock);
516 _leave(" [killed?]");
520 * destroy a dead volume location record
522 static void afs_vlocation_destroy(struct afs_vlocation *vl)
526 #ifdef AFS_CACHING_SUPPORT
527 cachefs_relinquish_cookie(vl->cache, 0);
530 afs_put_cell(vl->cell);
535 * reap dead volume location records
537 static void afs_vlocation_reaper(struct work_struct *work)
540 struct afs_vlocation *vl;
541 unsigned long delay, expiry;
547 spin_lock(&afs_vlocation_graveyard_lock);
549 while (!list_empty(&afs_vlocation_graveyard)) {
550 vl = list_entry(afs_vlocation_graveyard.next,
551 struct afs_vlocation, grave);
553 _debug("check %p", vl);
555 /* the queue is ordered most dead first */
556 expiry = vl->time_of_death + afs_vlocation_timeout;
558 delay = (expiry - now) * HZ;
559 _debug("delay %lu", delay);
560 if (!schedule_delayed_work(&afs_vlocation_reap,
562 cancel_delayed_work(&afs_vlocation_reap);
563 schedule_delayed_work(&afs_vlocation_reap,
569 spin_lock(&vl->cell->vl_lock);
570 if (atomic_read(&vl->usage) > 0) {
572 list_del_init(&vl->grave);
575 list_move_tail(&vl->grave, &corpses);
576 list_del_init(&vl->link);
578 spin_unlock(&vl->cell->vl_lock);
581 spin_unlock(&afs_vlocation_graveyard_lock);
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);
594 * initialise the VL update process
596 int __init afs_vlocation_update_init(void)
598 afs_vlocation_update_worker =
599 create_singlethread_workqueue("kafs_vlupdated");
600 return afs_vlocation_update_worker ? 0 : -ENOMEM;
604 * discard all the volume location records for rmmod
606 void __exit afs_vlocation_purge(void)
608 afs_vlocation_timeout = 0;
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);
618 cancel_delayed_work(&afs_vlocation_reap);
619 schedule_delayed_work(&afs_vlocation_reap, 0);
623 * update a volume location
625 static void afs_vlocation_updater(struct work_struct *work)
627 struct afs_cache_vlocation vldb;
628 struct afs_vlocation *vl, *xvl;
637 /* find a record to update */
638 spin_lock(&afs_vlocation_updates_lock);
640 if (list_empty(&afs_vlocation_updates)) {
641 spin_unlock(&afs_vlocation_updates_lock);
642 _leave(" [nothing]");
646 vl = list_entry(afs_vlocation_updates.next,
647 struct afs_vlocation, update);
648 if (atomic_read(&vl->usage) > 0)
650 list_del_init(&vl->update);
653 timeout = vl->update_at - now;
655 queue_delayed_work(afs_vlocation_update_worker,
656 &afs_vlocation_update, timeout * HZ);
657 spin_unlock(&afs_vlocation_updates_lock);
658 _leave(" [nothing]");
662 list_del_init(&vl->update);
663 atomic_inc(&vl->usage);
664 spin_unlock(&afs_vlocation_updates_lock);
666 /* we can now perform the update */
667 _debug("update %s", vl->vldb.name);
668 vl->state = AFS_VL_UPDATING;
670 vl->upd_busy_cnt = 0;
672 ret = afs_vlocation_update_record(vl, NULL, &vldb);
673 spin_lock(&vl->lock);
676 afs_vlocation_apply_update(vl, &vldb);
677 vl->state = AFS_VL_VALID;
681 vl->state = AFS_VL_VOLUME_DELETED;
684 vl->state = AFS_VL_UNCERTAIN;
687 spin_unlock(&vl->lock);
689 /* and then reschedule */
690 _debug("reschedule");
691 vl->update_at = get_seconds() + afs_vlocation_update_timeout;
693 spin_lock(&afs_vlocation_updates_lock);
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
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;
710 timeout = afs_vlocation_update_timeout;
713 ASSERT(list_empty(&vl->update));
715 list_add_tail(&vl->update, &afs_vlocation_updates);
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);