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/slab.h>
15 #include <linux/init.h>
16 #include <linux/sched.h>
19 static unsigned afs_vlocation_timeout
= 10; /* volume location timeout in seconds */
20 static unsigned afs_vlocation_update_timeout
= 10 * 60;
22 static void afs_vlocation_reaper(struct work_struct
*);
23 static void afs_vlocation_updater(struct work_struct
*);
25 static LIST_HEAD(afs_vlocation_updates
);
26 static LIST_HEAD(afs_vlocation_graveyard
);
27 static DEFINE_SPINLOCK(afs_vlocation_updates_lock
);
28 static DEFINE_SPINLOCK(afs_vlocation_graveyard_lock
);
29 static DECLARE_DELAYED_WORK(afs_vlocation_reap
, afs_vlocation_reaper
);
30 static DECLARE_DELAYED_WORK(afs_vlocation_update
, afs_vlocation_updater
);
31 static struct workqueue_struct
*afs_vlocation_update_worker
;
34 * iterate through the VL servers in a cell until one of them admits knowing
35 * about the volume in question
37 static int afs_vlocation_access_vl_by_name(struct afs_vlocation
*vl
,
39 struct afs_cache_vlocation
*vldb
)
41 struct afs_cell
*cell
= vl
->cell
;
45 _enter("%s,%s", cell
->name
, vl
->vldb
.name
);
47 down_write(&vl
->cell
->vl_sem
);
49 for (count
= cell
->vl_naddrs
; count
> 0; count
--) {
50 addr
= cell
->vl_addrs
[cell
->vl_curr_svix
];
52 _debug("CellServ[%hu]: %08x", cell
->vl_curr_svix
, addr
.s_addr
);
54 /* attempt to access the VL server */
55 ret
= afs_vl_get_entry_by_name(&addr
, key
, vl
->vldb
.name
, vldb
,
65 if (ret
== -ENOMEM
|| ret
== -ENONET
)
77 /* rotate the server records upon lookup failure */
80 cell
->vl_curr_svix
%= cell
->vl_naddrs
;
84 up_write(&vl
->cell
->vl_sem
);
90 * iterate through the VL servers in a cell until one of them admits knowing
91 * about the volume in question
93 static int afs_vlocation_access_vl_by_id(struct afs_vlocation
*vl
,
96 afs_voltype_t voltype
,
97 struct afs_cache_vlocation
*vldb
)
99 struct afs_cell
*cell
= vl
->cell
;
103 _enter("%s,%x,%d,", cell
->name
, volid
, voltype
);
105 down_write(&vl
->cell
->vl_sem
);
107 for (count
= cell
->vl_naddrs
; count
> 0; count
--) {
108 addr
= cell
->vl_addrs
[cell
->vl_curr_svix
];
110 _debug("CellServ[%hu]: %08x", cell
->vl_curr_svix
, addr
.s_addr
);
112 /* attempt to access the VL server */
113 ret
= afs_vl_get_entry_by_id(&addr
, key
, volid
, voltype
, vldb
,
123 if (ret
== -ENOMEM
|| ret
== -ENONET
)
128 if (vl
->upd_busy_cnt
<= 3) {
129 if (vl
->upd_busy_cnt
> 1) {
130 /* second+ BUSY - sleep a little bit */
131 set_current_state(TASK_UNINTERRUPTIBLE
);
145 /* rotate the server records upon lookup failure */
147 cell
->vl_curr_svix
++;
148 cell
->vl_curr_svix
%= cell
->vl_naddrs
;
149 vl
->upd_busy_cnt
= 0;
153 if (ret
< 0 && vl
->upd_rej_cnt
> 0) {
154 printk(KERN_NOTICE
"kAFS:"
155 " Active volume no longer valid '%s'\n",
161 up_write(&vl
->cell
->vl_sem
);
162 _leave(" = %d", ret
);
167 * allocate a volume location record
169 static struct afs_vlocation
*afs_vlocation_alloc(struct afs_cell
*cell
,
173 struct afs_vlocation
*vl
;
175 vl
= kzalloc(sizeof(struct afs_vlocation
), GFP_KERNEL
);
178 vl
->state
= AFS_VL_NEW
;
179 atomic_set(&vl
->usage
, 1);
180 INIT_LIST_HEAD(&vl
->link
);
181 INIT_LIST_HEAD(&vl
->grave
);
182 INIT_LIST_HEAD(&vl
->update
);
183 init_waitqueue_head(&vl
->waitq
);
184 spin_lock_init(&vl
->lock
);
185 memcpy(vl
->vldb
.name
, name
, namesz
);
193 * update record if we found it in the cache
195 static int afs_vlocation_update_record(struct afs_vlocation
*vl
,
197 struct afs_cache_vlocation
*vldb
)
199 afs_voltype_t voltype
;
203 /* try to look up a cached volume in the cell VL databases by ID */
204 _debug("Locally Cached: %s %02x { %08x(%x) %08x(%x) %08x(%x) }",
207 ntohl(vl
->vldb
.servers
[0].s_addr
),
208 vl
->vldb
.srvtmask
[0],
209 ntohl(vl
->vldb
.servers
[1].s_addr
),
210 vl
->vldb
.srvtmask
[1],
211 ntohl(vl
->vldb
.servers
[2].s_addr
),
212 vl
->vldb
.srvtmask
[2]);
214 _debug("Vids: %08x %08x %08x",
219 if (vl
->vldb
.vidmask
& AFS_VOL_VTM_RW
) {
220 vid
= vl
->vldb
.vid
[0];
221 voltype
= AFSVL_RWVOL
;
222 } else if (vl
->vldb
.vidmask
& AFS_VOL_VTM_RO
) {
223 vid
= vl
->vldb
.vid
[1];
224 voltype
= AFSVL_ROVOL
;
225 } else if (vl
->vldb
.vidmask
& AFS_VOL_VTM_BAK
) {
226 vid
= vl
->vldb
.vid
[2];
227 voltype
= AFSVL_BACKVOL
;
234 /* contact the server to make sure the volume is still available
235 * - TODO: need to handle disconnected operation here
237 ret
= afs_vlocation_access_vl_by_id(vl
, key
, vid
, voltype
, vldb
);
241 printk(KERN_WARNING
"kAFS:"
242 " failed to update volume '%s' (%x) up in '%s': %d\n",
243 vl
->vldb
.name
, vid
, vl
->cell
->name
, ret
);
244 _leave(" = %d", ret
);
247 /* pulled from local cache into memory */
252 /* uh oh... looks like the volume got deleted */
254 printk(KERN_ERR
"kAFS:"
255 " volume '%s' (%x) does not exist '%s'\n",
256 vl
->vldb
.name
, vid
, vl
->cell
->name
);
258 /* TODO: make existing record unavailable */
259 _leave(" = %d", ret
);
265 * apply the update to a VL record
267 static void afs_vlocation_apply_update(struct afs_vlocation
*vl
,
268 struct afs_cache_vlocation
*vldb
)
270 _debug("Done VL Lookup: %s %02x { %08x(%x) %08x(%x) %08x(%x) }",
271 vldb
->name
, vldb
->vidmask
,
272 ntohl(vldb
->servers
[0].s_addr
), vldb
->srvtmask
[0],
273 ntohl(vldb
->servers
[1].s_addr
), vldb
->srvtmask
[1],
274 ntohl(vldb
->servers
[2].s_addr
), vldb
->srvtmask
[2]);
276 _debug("Vids: %08x %08x %08x",
277 vldb
->vid
[0], vldb
->vid
[1], vldb
->vid
[2]);
279 if (strcmp(vldb
->name
, vl
->vldb
.name
) != 0)
280 printk(KERN_NOTICE
"kAFS:"
281 " name of volume '%s' changed to '%s' on server\n",
282 vl
->vldb
.name
, vldb
->name
);
286 #ifdef CONFIG_AFS_FSCACHE
287 fscache_update_cookie(vl
->cache
);
292 * fill in a volume location record, consulting the cache and the VL server
295 static int afs_vlocation_fill_in_record(struct afs_vlocation
*vl
,
298 struct afs_cache_vlocation vldb
;
303 ASSERTCMP(vl
->valid
, ==, 0);
305 memset(&vldb
, 0, sizeof(vldb
));
307 /* see if we have an in-cache copy (will set vl->valid if there is) */
308 #ifdef CONFIG_AFS_FSCACHE
309 vl
->cache
= fscache_acquire_cookie(vl
->cell
->cache
,
310 &afs_vlocation_cache_index_def
, vl
,
315 /* try to update a known volume in the cell VL databases by
316 * ID as the name may have changed */
317 _debug("found in cache");
318 ret
= afs_vlocation_update_record(vl
, key
, &vldb
);
320 /* try to look up an unknown volume in the cell VL databases by
322 ret
= afs_vlocation_access_vl_by_name(vl
, key
, &vldb
);
324 printk("kAFS: failed to locate '%s' in cell '%s'\n",
325 vl
->vldb
.name
, vl
->cell
->name
);
330 afs_vlocation_apply_update(vl
, &vldb
);
336 * queue a vlocation record for updates
338 static void afs_vlocation_queue_for_updates(struct afs_vlocation
*vl
)
340 struct afs_vlocation
*xvl
;
342 /* wait at least 10 minutes before updating... */
343 vl
->update_at
= get_seconds() + afs_vlocation_update_timeout
;
345 spin_lock(&afs_vlocation_updates_lock
);
347 if (!list_empty(&afs_vlocation_updates
)) {
348 /* ... but wait at least 1 second more than the newest record
349 * already queued so that we don't spam the VL server suddenly
350 * with lots of requests
352 xvl
= list_entry(afs_vlocation_updates
.prev
,
353 struct afs_vlocation
, update
);
354 if (vl
->update_at
<= xvl
->update_at
)
355 vl
->update_at
= xvl
->update_at
+ 1;
357 queue_delayed_work(afs_vlocation_update_worker
,
358 &afs_vlocation_update
,
359 afs_vlocation_update_timeout
* HZ
);
362 list_add_tail(&vl
->update
, &afs_vlocation_updates
);
363 spin_unlock(&afs_vlocation_updates_lock
);
367 * lookup volume location
368 * - iterate through the VL servers in a cell until one of them admits knowing
369 * about the volume in question
370 * - lookup in the local cache if not able to find on the VL server
371 * - insert/update in the local cache if did get a VL response
373 struct afs_vlocation
*afs_vlocation_lookup(struct afs_cell
*cell
,
378 struct afs_vlocation
*vl
;
381 _enter("{%s},{%x},%*.*s,%zu",
382 cell
->name
, key_serial(key
),
383 (int) namesz
, (int) namesz
, name
, namesz
);
385 if (namesz
>= sizeof(vl
->vldb
.name
)) {
386 _leave(" = -ENAMETOOLONG");
387 return ERR_PTR(-ENAMETOOLONG
);
390 /* see if we have an in-memory copy first */
391 down_write(&cell
->vl_sem
);
392 spin_lock(&cell
->vl_lock
);
393 list_for_each_entry(vl
, &cell
->vl_list
, link
) {
394 if (vl
->vldb
.name
[namesz
] != '\0')
396 if (memcmp(vl
->vldb
.name
, name
, namesz
) == 0)
397 goto found_in_memory
;
399 spin_unlock(&cell
->vl_lock
);
401 /* not in the cell's in-memory lists - create a new record */
402 vl
= afs_vlocation_alloc(cell
, name
, namesz
);
404 up_write(&cell
->vl_sem
);
405 return ERR_PTR(-ENOMEM
);
410 list_add_tail(&vl
->link
, &cell
->vl_list
);
411 vl
->state
= AFS_VL_CREATING
;
412 up_write(&cell
->vl_sem
);
415 ret
= afs_vlocation_fill_in_record(vl
, key
);
418 spin_lock(&vl
->lock
);
419 vl
->state
= AFS_VL_VALID
;
420 spin_unlock(&vl
->lock
);
423 /* update volume entry in local cache */
424 #ifdef CONFIG_AFS_FSCACHE
425 fscache_update_cookie(vl
->cache
);
428 /* schedule for regular updates */
429 afs_vlocation_queue_for_updates(vl
);
433 /* found in memory */
434 _debug("found in memory");
435 atomic_inc(&vl
->usage
);
436 spin_unlock(&cell
->vl_lock
);
437 if (!list_empty(&vl
->grave
)) {
438 spin_lock(&afs_vlocation_graveyard_lock
);
439 list_del_init(&vl
->grave
);
440 spin_unlock(&afs_vlocation_graveyard_lock
);
442 up_write(&cell
->vl_sem
);
444 /* see if it was an abandoned record that we might try filling in */
445 spin_lock(&vl
->lock
);
446 while (vl
->state
!= AFS_VL_VALID
) {
447 afs_vlocation_state_t state
= vl
->state
;
449 _debug("invalid [state %d]", state
);
451 if (state
== AFS_VL_NEW
|| state
== AFS_VL_NO_VOLUME
) {
452 vl
->state
= AFS_VL_CREATING
;
453 spin_unlock(&vl
->lock
);
457 /* must now wait for creation or update by someone else to
461 spin_unlock(&vl
->lock
);
462 ret
= wait_event_interruptible(vl
->waitq
,
463 vl
->state
== AFS_VL_NEW
||
464 vl
->state
== AFS_VL_VALID
||
465 vl
->state
== AFS_VL_NO_VOLUME
);
468 spin_lock(&vl
->lock
);
470 spin_unlock(&vl
->lock
);
477 spin_lock(&vl
->lock
);
478 vl
->state
= AFS_VL_NEW
;
479 spin_unlock(&vl
->lock
);
483 afs_put_vlocation(vl
);
484 _leave(" = %d", ret
);
489 * finish using a volume location record
491 void afs_put_vlocation(struct afs_vlocation
*vl
)
496 _enter("%s", vl
->vldb
.name
);
498 ASSERTCMP(atomic_read(&vl
->usage
), >, 0);
500 if (likely(!atomic_dec_and_test(&vl
->usage
))) {
505 spin_lock(&afs_vlocation_graveyard_lock
);
506 if (atomic_read(&vl
->usage
) == 0) {
508 list_move_tail(&vl
->grave
, &afs_vlocation_graveyard
);
509 vl
->time_of_death
= get_seconds();
510 queue_delayed_work(afs_wq
, &afs_vlocation_reap
,
511 afs_vlocation_timeout
* HZ
);
513 /* suspend updates on this record */
514 if (!list_empty(&vl
->update
)) {
515 spin_lock(&afs_vlocation_updates_lock
);
516 list_del_init(&vl
->update
);
517 spin_unlock(&afs_vlocation_updates_lock
);
520 spin_unlock(&afs_vlocation_graveyard_lock
);
521 _leave(" [killed?]");
525 * destroy a dead volume location record
527 static void afs_vlocation_destroy(struct afs_vlocation
*vl
)
531 #ifdef CONFIG_AFS_FSCACHE
532 fscache_relinquish_cookie(vl
->cache
, 0);
534 afs_put_cell(vl
->cell
);
539 * reap dead volume location records
541 static void afs_vlocation_reaper(struct work_struct
*work
)
544 struct afs_vlocation
*vl
;
545 unsigned long delay
, expiry
;
551 spin_lock(&afs_vlocation_graveyard_lock
);
553 while (!list_empty(&afs_vlocation_graveyard
)) {
554 vl
= list_entry(afs_vlocation_graveyard
.next
,
555 struct afs_vlocation
, grave
);
557 _debug("check %p", vl
);
559 /* the queue is ordered most dead first */
560 expiry
= vl
->time_of_death
+ afs_vlocation_timeout
;
562 delay
= (expiry
- now
) * HZ
;
563 _debug("delay %lu", delay
);
564 mod_delayed_work(afs_wq
, &afs_vlocation_reap
, delay
);
568 spin_lock(&vl
->cell
->vl_lock
);
569 if (atomic_read(&vl
->usage
) > 0) {
571 list_del_init(&vl
->grave
);
574 list_move_tail(&vl
->grave
, &corpses
);
575 list_del_init(&vl
->link
);
577 spin_unlock(&vl
->cell
->vl_lock
);
580 spin_unlock(&afs_vlocation_graveyard_lock
);
582 /* now reap the corpses we've extracted */
583 while (!list_empty(&corpses
)) {
584 vl
= list_entry(corpses
.next
, struct afs_vlocation
, grave
);
585 list_del(&vl
->grave
);
586 afs_vlocation_destroy(vl
);
593 * initialise the VL update process
595 int __init
afs_vlocation_update_init(void)
597 afs_vlocation_update_worker
=
598 create_singlethread_workqueue("kafs_vlupdated");
599 return afs_vlocation_update_worker
? 0 : -ENOMEM
;
603 * discard all the volume location records for rmmod
605 void afs_vlocation_purge(void)
607 afs_vlocation_timeout
= 0;
609 spin_lock(&afs_vlocation_updates_lock
);
610 list_del_init(&afs_vlocation_updates
);
611 spin_unlock(&afs_vlocation_updates_lock
);
612 mod_delayed_work(afs_vlocation_update_worker
, &afs_vlocation_update
, 0);
613 destroy_workqueue(afs_vlocation_update_worker
);
615 mod_delayed_work(afs_wq
, &afs_vlocation_reap
, 0);
619 * update a volume location
621 static void afs_vlocation_updater(struct work_struct
*work
)
623 struct afs_cache_vlocation vldb
;
624 struct afs_vlocation
*vl
, *xvl
;
633 /* find a record to update */
634 spin_lock(&afs_vlocation_updates_lock
);
636 if (list_empty(&afs_vlocation_updates
)) {
637 spin_unlock(&afs_vlocation_updates_lock
);
638 _leave(" [nothing]");
642 vl
= list_entry(afs_vlocation_updates
.next
,
643 struct afs_vlocation
, update
);
644 if (atomic_read(&vl
->usage
) > 0)
646 list_del_init(&vl
->update
);
649 timeout
= vl
->update_at
- now
;
651 queue_delayed_work(afs_vlocation_update_worker
,
652 &afs_vlocation_update
, timeout
* HZ
);
653 spin_unlock(&afs_vlocation_updates_lock
);
654 _leave(" [nothing]");
658 list_del_init(&vl
->update
);
659 atomic_inc(&vl
->usage
);
660 spin_unlock(&afs_vlocation_updates_lock
);
662 /* we can now perform the update */
663 _debug("update %s", vl
->vldb
.name
);
664 vl
->state
= AFS_VL_UPDATING
;
666 vl
->upd_busy_cnt
= 0;
668 ret
= afs_vlocation_update_record(vl
, NULL
, &vldb
);
669 spin_lock(&vl
->lock
);
672 afs_vlocation_apply_update(vl
, &vldb
);
673 vl
->state
= AFS_VL_VALID
;
676 vl
->state
= AFS_VL_VOLUME_DELETED
;
679 vl
->state
= AFS_VL_UNCERTAIN
;
682 spin_unlock(&vl
->lock
);
685 /* and then reschedule */
686 _debug("reschedule");
687 vl
->update_at
= get_seconds() + afs_vlocation_update_timeout
;
689 spin_lock(&afs_vlocation_updates_lock
);
691 if (!list_empty(&afs_vlocation_updates
)) {
692 /* next update in 10 minutes, but wait at least 1 second more
693 * than the newest record already queued so that we don't spam
694 * the VL server suddenly with lots of requests
696 xvl
= list_entry(afs_vlocation_updates
.prev
,
697 struct afs_vlocation
, update
);
698 if (vl
->update_at
<= xvl
->update_at
)
699 vl
->update_at
= xvl
->update_at
+ 1;
700 xvl
= list_entry(afs_vlocation_updates
.next
,
701 struct afs_vlocation
, update
);
702 timeout
= xvl
->update_at
- now
;
706 timeout
= afs_vlocation_update_timeout
;
709 ASSERT(list_empty(&vl
->update
));
711 list_add_tail(&vl
->update
, &afs_vlocation_updates
);
713 _debug("timeout %ld", timeout
);
714 queue_delayed_work(afs_vlocation_update_worker
,
715 &afs_vlocation_update
, timeout
* HZ
);
716 spin_unlock(&afs_vlocation_updates_lock
);
717 afs_put_vlocation(vl
);