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>
15 #include <linux/sched.h>
18 static unsigned afs_vlocation_timeout
= 10; /* volume location timeout in seconds */
19 static unsigned afs_vlocation_update_timeout
= 10 * 60;
21 static void afs_vlocation_reaper(struct work_struct
*);
22 static void afs_vlocation_updater(struct work_struct
*);
24 static LIST_HEAD(afs_vlocation_updates
);
25 static LIST_HEAD(afs_vlocation_graveyard
);
26 static DEFINE_SPINLOCK(afs_vlocation_updates_lock
);
27 static DEFINE_SPINLOCK(afs_vlocation_graveyard_lock
);
28 static DECLARE_DELAYED_WORK(afs_vlocation_reap
, afs_vlocation_reaper
);
29 static DECLARE_DELAYED_WORK(afs_vlocation_update
, afs_vlocation_updater
);
30 static struct workqueue_struct
*afs_vlocation_update_worker
;
33 * iterate through the VL servers in a cell until one of them admits knowing
34 * about the volume in question
36 static int afs_vlocation_access_vl_by_name(struct afs_vlocation
*vl
,
38 struct afs_cache_vlocation
*vldb
)
40 struct afs_cell
*cell
= vl
->cell
;
44 _enter("%s,%s", cell
->name
, vl
->vldb
.name
);
46 down_write(&vl
->cell
->vl_sem
);
48 for (count
= cell
->vl_naddrs
; count
> 0; count
--) {
49 addr
= cell
->vl_addrs
[cell
->vl_curr_svix
];
51 _debug("CellServ[%hu]: %08x", cell
->vl_curr_svix
, addr
.s_addr
);
53 /* attempt to access the VL server */
54 ret
= afs_vl_get_entry_by_name(&addr
, key
, vl
->vldb
.name
, vldb
,
64 if (ret
== -ENOMEM
|| ret
== -ENONET
)
74 /* rotate the server records upon lookup failure */
77 cell
->vl_curr_svix
%= cell
->vl_naddrs
;
81 up_write(&vl
->cell
->vl_sem
);
87 * iterate through the VL servers in a cell until one of them admits knowing
88 * about the volume in question
90 static int afs_vlocation_access_vl_by_id(struct afs_vlocation
*vl
,
93 afs_voltype_t voltype
,
94 struct afs_cache_vlocation
*vldb
)
96 struct afs_cell
*cell
= vl
->cell
;
100 _enter("%s,%x,%d,", cell
->name
, volid
, voltype
);
102 down_write(&vl
->cell
->vl_sem
);
104 for (count
= cell
->vl_naddrs
; count
> 0; count
--) {
105 addr
= cell
->vl_addrs
[cell
->vl_curr_svix
];
107 _debug("CellServ[%hu]: %08x", cell
->vl_curr_svix
, addr
.s_addr
);
109 /* attempt to access the VL server */
110 ret
= afs_vl_get_entry_by_id(&addr
, key
, volid
, voltype
, vldb
,
120 if (ret
== -ENOMEM
|| ret
== -ENONET
)
125 if (vl
->upd_busy_cnt
<= 3) {
126 if (vl
->upd_busy_cnt
> 1) {
127 /* second+ BUSY - sleep a little bit */
128 set_current_state(TASK_UNINTERRUPTIBLE
);
130 __set_current_state(TASK_RUNNING
);
143 /* rotate the server records upon lookup failure */
145 cell
->vl_curr_svix
++;
146 cell
->vl_curr_svix
%= cell
->vl_naddrs
;
147 vl
->upd_busy_cnt
= 0;
151 if (ret
< 0 && vl
->upd_rej_cnt
> 0) {
152 printk(KERN_NOTICE
"kAFS:"
153 " Active volume no longer valid '%s'\n",
159 up_write(&vl
->cell
->vl_sem
);
160 _leave(" = %d", ret
);
165 * allocate a volume location record
167 static struct afs_vlocation
*afs_vlocation_alloc(struct afs_cell
*cell
,
171 struct afs_vlocation
*vl
;
173 vl
= kzalloc(sizeof(struct afs_vlocation
), GFP_KERNEL
);
176 vl
->state
= AFS_VL_NEW
;
177 atomic_set(&vl
->usage
, 1);
178 INIT_LIST_HEAD(&vl
->link
);
179 INIT_LIST_HEAD(&vl
->grave
);
180 INIT_LIST_HEAD(&vl
->update
);
181 init_waitqueue_head(&vl
->waitq
);
182 spin_lock_init(&vl
->lock
);
183 memcpy(vl
->vldb
.name
, name
, namesz
);
191 * update record if we found it in the cache
193 static int afs_vlocation_update_record(struct afs_vlocation
*vl
,
195 struct afs_cache_vlocation
*vldb
)
197 afs_voltype_t voltype
;
201 /* try to look up a cached volume in the cell VL databases by ID */
202 _debug("Locally Cached: %s %02x { %08x(%x) %08x(%x) %08x(%x) }",
205 ntohl(vl
->vldb
.servers
[0].s_addr
),
206 vl
->vldb
.srvtmask
[0],
207 ntohl(vl
->vldb
.servers
[1].s_addr
),
208 vl
->vldb
.srvtmask
[1],
209 ntohl(vl
->vldb
.servers
[2].s_addr
),
210 vl
->vldb
.srvtmask
[2]);
212 _debug("Vids: %08x %08x %08x",
217 if (vl
->vldb
.vidmask
& AFS_VOL_VTM_RW
) {
218 vid
= vl
->vldb
.vid
[0];
219 voltype
= AFSVL_RWVOL
;
220 } else if (vl
->vldb
.vidmask
& AFS_VOL_VTM_RO
) {
221 vid
= vl
->vldb
.vid
[1];
222 voltype
= AFSVL_ROVOL
;
223 } else if (vl
->vldb
.vidmask
& AFS_VOL_VTM_BAK
) {
224 vid
= vl
->vldb
.vid
[2];
225 voltype
= AFSVL_BACKVOL
;
232 /* contact the server to make sure the volume is still available
233 * - TODO: need to handle disconnected operation here
235 ret
= afs_vlocation_access_vl_by_id(vl
, key
, vid
, voltype
, vldb
);
239 printk(KERN_WARNING
"kAFS:"
240 " failed to update volume '%s' (%x) up in '%s': %d\n",
241 vl
->vldb
.name
, vid
, vl
->cell
->name
, ret
);
242 _leave(" = %d", ret
);
245 /* pulled from local cache into memory */
250 /* uh oh... looks like the volume got deleted */
252 printk(KERN_ERR
"kAFS:"
253 " volume '%s' (%x) does not exist '%s'\n",
254 vl
->vldb
.name
, vid
, vl
->cell
->name
);
256 /* TODO: make existing record unavailable */
257 _leave(" = %d", ret
);
263 * apply the update to a VL record
265 static void afs_vlocation_apply_update(struct afs_vlocation
*vl
,
266 struct afs_cache_vlocation
*vldb
)
268 _debug("Done VL Lookup: %s %02x { %08x(%x) %08x(%x) %08x(%x) }",
269 vldb
->name
, vldb
->vidmask
,
270 ntohl(vldb
->servers
[0].s_addr
), vldb
->srvtmask
[0],
271 ntohl(vldb
->servers
[1].s_addr
), vldb
->srvtmask
[1],
272 ntohl(vldb
->servers
[2].s_addr
), vldb
->srvtmask
[2]);
274 _debug("Vids: %08x %08x %08x",
275 vldb
->vid
[0], vldb
->vid
[1], vldb
->vid
[2]);
277 if (strcmp(vldb
->name
, vl
->vldb
.name
) != 0)
278 printk(KERN_NOTICE
"kAFS:"
279 " name of volume '%s' changed to '%s' on server\n",
280 vl
->vldb
.name
, vldb
->name
);
284 #ifdef CONFIG_AFS_FSCACHE
285 fscache_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 CONFIG_AFS_FSCACHE
307 vl
->cache
= fscache_acquire_cookie(vl
->cell
->cache
,
308 &afs_vlocation_cache_index_def
, vl
);
312 /* try to update a known volume in the cell VL databases by
313 * ID as the name may have changed */
314 _debug("found in cache");
315 ret
= afs_vlocation_update_record(vl
, key
, &vldb
);
317 /* try to look up an unknown volume in the cell VL databases by
319 ret
= afs_vlocation_access_vl_by_name(vl
, key
, &vldb
);
321 printk("kAFS: failed to locate '%s' in cell '%s'\n",
322 vl
->vldb
.name
, vl
->cell
->name
);
327 afs_vlocation_apply_update(vl
, &vldb
);
333 * queue a vlocation record for updates
335 static void afs_vlocation_queue_for_updates(struct afs_vlocation
*vl
)
337 struct afs_vlocation
*xvl
;
339 /* wait at least 10 minutes before updating... */
340 vl
->update_at
= get_seconds() + afs_vlocation_update_timeout
;
342 spin_lock(&afs_vlocation_updates_lock
);
344 if (!list_empty(&afs_vlocation_updates
)) {
345 /* ... but wait at least 1 second more than the newest record
346 * already queued so that we don't spam the VL server suddenly
347 * with lots of requests
349 xvl
= list_entry(afs_vlocation_updates
.prev
,
350 struct afs_vlocation
, update
);
351 if (vl
->update_at
<= xvl
->update_at
)
352 vl
->update_at
= xvl
->update_at
+ 1;
354 queue_delayed_work(afs_vlocation_update_worker
,
355 &afs_vlocation_update
,
356 afs_vlocation_update_timeout
* HZ
);
359 list_add_tail(&vl
->update
, &afs_vlocation_updates
);
360 spin_unlock(&afs_vlocation_updates_lock
);
364 * lookup volume location
365 * - iterate through the VL servers in a cell until one of them admits knowing
366 * about the volume in question
367 * - lookup in the local cache if not able to find on the VL server
368 * - insert/update in the local cache if did get a VL response
370 struct afs_vlocation
*afs_vlocation_lookup(struct afs_cell
*cell
,
375 struct afs_vlocation
*vl
;
378 _enter("{%s},{%x},%*.*s,%zu",
379 cell
->name
, key_serial(key
),
380 (int) namesz
, (int) namesz
, name
, namesz
);
382 if (namesz
>= sizeof(vl
->vldb
.name
)) {
383 _leave(" = -ENAMETOOLONG");
384 return ERR_PTR(-ENAMETOOLONG
);
387 /* see if we have an in-memory copy first */
388 down_write(&cell
->vl_sem
);
389 spin_lock(&cell
->vl_lock
);
390 list_for_each_entry(vl
, &cell
->vl_list
, link
) {
391 if (vl
->vldb
.name
[namesz
] != '\0')
393 if (memcmp(vl
->vldb
.name
, name
, namesz
) == 0)
394 goto found_in_memory
;
396 spin_unlock(&cell
->vl_lock
);
398 /* not in the cell's in-memory lists - create a new record */
399 vl
= afs_vlocation_alloc(cell
, name
, namesz
);
401 up_write(&cell
->vl_sem
);
402 return ERR_PTR(-ENOMEM
);
407 list_add_tail(&vl
->link
, &cell
->vl_list
);
408 vl
->state
= AFS_VL_CREATING
;
409 up_write(&cell
->vl_sem
);
412 ret
= afs_vlocation_fill_in_record(vl
, key
);
415 spin_lock(&vl
->lock
);
416 vl
->state
= AFS_VL_VALID
;
417 spin_unlock(&vl
->lock
);
420 /* update volume entry in local cache */
421 #ifdef CONFIG_AFS_FSCACHE
422 fscache_update_cookie(vl
->cache
);
425 /* schedule for regular updates */
426 afs_vlocation_queue_for_updates(vl
);
430 /* found in memory */
431 _debug("found in memory");
432 atomic_inc(&vl
->usage
);
433 spin_unlock(&cell
->vl_lock
);
434 if (!list_empty(&vl
->grave
)) {
435 spin_lock(&afs_vlocation_graveyard_lock
);
436 list_del_init(&vl
->grave
);
437 spin_unlock(&afs_vlocation_graveyard_lock
);
439 up_write(&cell
->vl_sem
);
441 /* see if it was an abandoned record that we might try filling in */
442 spin_lock(&vl
->lock
);
443 while (vl
->state
!= AFS_VL_VALID
) {
444 afs_vlocation_state_t state
= vl
->state
;
446 _debug("invalid [state %d]", state
);
448 if (state
== AFS_VL_NEW
|| state
== AFS_VL_NO_VOLUME
) {
449 vl
->state
= AFS_VL_CREATING
;
450 spin_unlock(&vl
->lock
);
454 /* must now wait for creation or update by someone else to
458 spin_unlock(&vl
->lock
);
459 ret
= wait_event_interruptible(vl
->waitq
,
460 vl
->state
== AFS_VL_NEW
||
461 vl
->state
== AFS_VL_VALID
||
462 vl
->state
== AFS_VL_NO_VOLUME
);
465 spin_lock(&vl
->lock
);
467 spin_unlock(&vl
->lock
);
474 spin_lock(&vl
->lock
);
475 vl
->state
= AFS_VL_NEW
;
476 spin_unlock(&vl
->lock
);
480 afs_put_vlocation(vl
);
481 _leave(" = %d", ret
);
486 * finish using a volume location record
488 void afs_put_vlocation(struct afs_vlocation
*vl
)
493 _enter("%s", vl
->vldb
.name
);
495 ASSERTCMP(atomic_read(&vl
->usage
), >, 0);
497 if (likely(!atomic_dec_and_test(&vl
->usage
))) {
502 spin_lock(&afs_vlocation_graveyard_lock
);
503 if (atomic_read(&vl
->usage
) == 0) {
505 list_move_tail(&vl
->grave
, &afs_vlocation_graveyard
);
506 vl
->time_of_death
= get_seconds();
507 schedule_delayed_work(&afs_vlocation_reap
,
508 afs_vlocation_timeout
* HZ
);
510 /* suspend updates on this record */
511 if (!list_empty(&vl
->update
)) {
512 spin_lock(&afs_vlocation_updates_lock
);
513 list_del_init(&vl
->update
);
514 spin_unlock(&afs_vlocation_updates_lock
);
517 spin_unlock(&afs_vlocation_graveyard_lock
);
518 _leave(" [killed?]");
522 * destroy a dead volume location record
524 static void afs_vlocation_destroy(struct afs_vlocation
*vl
)
528 #ifdef CONFIG_AFS_FSCACHE
529 fscache_relinquish_cookie(vl
->cache
, 0);
531 afs_put_cell(vl
->cell
);
536 * reap dead volume location records
538 static void afs_vlocation_reaper(struct work_struct
*work
)
541 struct afs_vlocation
*vl
;
542 unsigned long delay
, expiry
;
548 spin_lock(&afs_vlocation_graveyard_lock
);
550 while (!list_empty(&afs_vlocation_graveyard
)) {
551 vl
= list_entry(afs_vlocation_graveyard
.next
,
552 struct afs_vlocation
, grave
);
554 _debug("check %p", vl
);
556 /* the queue is ordered most dead first */
557 expiry
= vl
->time_of_death
+ afs_vlocation_timeout
;
559 delay
= (expiry
- now
) * HZ
;
560 _debug("delay %lu", delay
);
561 if (!schedule_delayed_work(&afs_vlocation_reap
,
563 cancel_delayed_work(&afs_vlocation_reap
);
564 schedule_delayed_work(&afs_vlocation_reap
,
570 spin_lock(&vl
->cell
->vl_lock
);
571 if (atomic_read(&vl
->usage
) > 0) {
573 list_del_init(&vl
->grave
);
576 list_move_tail(&vl
->grave
, &corpses
);
577 list_del_init(&vl
->link
);
579 spin_unlock(&vl
->cell
->vl_lock
);
582 spin_unlock(&afs_vlocation_graveyard_lock
);
584 /* now reap the corpses we've extracted */
585 while (!list_empty(&corpses
)) {
586 vl
= list_entry(corpses
.next
, struct afs_vlocation
, grave
);
587 list_del(&vl
->grave
);
588 afs_vlocation_destroy(vl
);
595 * initialise the VL update process
597 int __init
afs_vlocation_update_init(void)
599 afs_vlocation_update_worker
=
600 create_singlethread_workqueue("kafs_vlupdated");
601 return afs_vlocation_update_worker
? 0 : -ENOMEM
;
605 * discard all the volume location records for rmmod
607 void afs_vlocation_purge(void)
609 afs_vlocation_timeout
= 0;
611 spin_lock(&afs_vlocation_updates_lock
);
612 list_del_init(&afs_vlocation_updates
);
613 spin_unlock(&afs_vlocation_updates_lock
);
614 cancel_delayed_work(&afs_vlocation_update
);
615 queue_delayed_work(afs_vlocation_update_worker
,
616 &afs_vlocation_update
, 0);
617 destroy_workqueue(afs_vlocation_update_worker
);
619 cancel_delayed_work(&afs_vlocation_reap
);
620 schedule_delayed_work(&afs_vlocation_reap
, 0);
624 * update a volume location
626 static void afs_vlocation_updater(struct work_struct
*work
)
628 struct afs_cache_vlocation vldb
;
629 struct afs_vlocation
*vl
, *xvl
;
638 /* find a record to update */
639 spin_lock(&afs_vlocation_updates_lock
);
641 if (list_empty(&afs_vlocation_updates
)) {
642 spin_unlock(&afs_vlocation_updates_lock
);
643 _leave(" [nothing]");
647 vl
= list_entry(afs_vlocation_updates
.next
,
648 struct afs_vlocation
, update
);
649 if (atomic_read(&vl
->usage
) > 0)
651 list_del_init(&vl
->update
);
654 timeout
= vl
->update_at
- now
;
656 queue_delayed_work(afs_vlocation_update_worker
,
657 &afs_vlocation_update
, timeout
* HZ
);
658 spin_unlock(&afs_vlocation_updates_lock
);
659 _leave(" [nothing]");
663 list_del_init(&vl
->update
);
664 atomic_inc(&vl
->usage
);
665 spin_unlock(&afs_vlocation_updates_lock
);
667 /* we can now perform the update */
668 _debug("update %s", vl
->vldb
.name
);
669 vl
->state
= AFS_VL_UPDATING
;
671 vl
->upd_busy_cnt
= 0;
673 ret
= afs_vlocation_update_record(vl
, NULL
, &vldb
);
674 spin_lock(&vl
->lock
);
677 afs_vlocation_apply_update(vl
, &vldb
);
678 vl
->state
= AFS_VL_VALID
;
681 vl
->state
= AFS_VL_VOLUME_DELETED
;
684 vl
->state
= AFS_VL_UNCERTAIN
;
687 spin_unlock(&vl
->lock
);
690 /* and then reschedule */
691 _debug("reschedule");
692 vl
->update_at
= get_seconds() + afs_vlocation_update_timeout
;
694 spin_lock(&afs_vlocation_updates_lock
);
696 if (!list_empty(&afs_vlocation_updates
)) {
697 /* next update in 10 minutes, but wait at least 1 second more
698 * than the newest record already queued so that we don't spam
699 * the VL server suddenly with lots of requests
701 xvl
= list_entry(afs_vlocation_updates
.prev
,
702 struct afs_vlocation
, update
);
703 if (vl
->update_at
<= xvl
->update_at
)
704 vl
->update_at
= xvl
->update_at
+ 1;
705 xvl
= list_entry(afs_vlocation_updates
.next
,
706 struct afs_vlocation
, update
);
707 timeout
= xvl
->update_at
- now
;
711 timeout
= afs_vlocation_update_timeout
;
714 ASSERT(list_empty(&vl
->update
));
716 list_add_tail(&vl
->update
, &afs_vlocation_updates
);
718 _debug("timeout %ld", timeout
);
719 queue_delayed_work(afs_vlocation_update_worker
,
720 &afs_vlocation_update
, timeout
* HZ
);
721 spin_unlock(&afs_vlocation_updates_lock
);
722 afs_put_vlocation(vl
);