1 /* volume.c: AFS volume management
3 * Copyright (C) 2002 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/slab.h>
17 #include <linux/pagemap.h>
22 #include "cmservice.h"
28 static const char *afs_voltypes
[] = { "R/W", "R/O", "BAK" };
31 #ifdef AFS_CACHING_SUPPORT
32 static cachefs_match_val_t
afs_volume_cache_match(void *target
,
34 static void afs_volume_cache_update(void *source
, void *entry
);
36 struct cachefs_index_def afs_volume_cache_index_def
= {
38 .data_size
= sizeof(struct afs_cache_vhash
),
39 .keys
[0] = { CACHEFS_INDEX_KEYS_BIN
, 1 },
40 .keys
[1] = { CACHEFS_INDEX_KEYS_BIN
, 1 },
41 .match
= afs_volume_cache_match
,
42 .update
= afs_volume_cache_update
,
46 /*****************************************************************************/
48 * lookup a volume by name
49 * - this can be one of the following:
50 * "%[cell:]volume[.]" R/W volume
51 * "#[cell:]volume[.]" R/O or R/W volume (rwparent=0),
52 * or R/W (rwparent=1) volume
53 * "%[cell:]volume.readonly" R/O volume
54 * "#[cell:]volume.readonly" R/O volume
55 * "%[cell:]volume.backup" Backup volume
56 * "#[cell:]volume.backup" Backup volume
58 * The cell name is optional, and defaults to the current cell.
60 * See "The Rules of Mount Point Traversal" in Chapter 5 of the AFS SysAdmin
62 * - Rule 1: Explicit type suffix forces access of that type or nothing
63 * (no suffix, then use Rule 2 & 3)
64 * - Rule 2: If parent volume is R/O, then mount R/O volume by preference, R/W
66 * - Rule 3: If parent volume is R/W, then only mount R/W volume unless
67 * explicitly told otherwise
69 int afs_volume_lookup(const char *name
, struct afs_cell
*cell
, int rwpath
,
70 struct afs_volume
**_volume
)
72 struct afs_vlocation
*vlocation
= NULL
;
73 struct afs_volume
*volume
= NULL
;
75 const char *cellname
, *volname
, *suffix
;
77 int force
, ret
, loop
, cellnamesz
, volnamesz
;
79 _enter("%s,,%d,", name
, rwpath
);
81 if (!name
|| (name
[0] != '%' && name
[0] != '#') || !name
[1]) {
82 printk("kAFS: unparsable volume name\n");
86 /* determine the type of volume we're looking for */
90 if (rwpath
|| name
[0] == '%') {
95 suffix
= strrchr(name
, '.');
97 if (strcmp(suffix
, ".readonly") == 0) {
101 else if (strcmp(suffix
, ".backup") == 0) {
102 type
= AFSVL_BACKVOL
;
105 else if (suffix
[1] == 0) {
112 /* split the cell and volume names */
114 volname
= strchr(name
, ':');
117 cellnamesz
= volname
- name
;
126 volnamesz
= suffix
? suffix
- volname
: strlen(volname
);
128 _debug("CELL:%*.*s [%p] VOLUME:%*.*s SUFFIX:%s TYPE:%d%s",
129 cellnamesz
, cellnamesz
, cellname
?: "", cell
,
130 volnamesz
, volnamesz
, volname
, suffix
?: "-",
132 force
? " FORCE" : "");
134 /* lookup the cell record */
135 if (cellname
|| !cell
) {
136 ret
= afs_cell_lookup(cellname
, cellnamesz
, &cell
);
138 printk("kAFS: unable to lookup cell '%s'\n",
147 /* lookup the volume location record */
148 ret
= afs_vlocation_lookup(cell
, volname
, volnamesz
, &vlocation
);
152 /* make the final decision on the type we want */
154 if (force
&& !(vlocation
->vldb
.vidmask
& (1 << type
)))
158 for (loop
= 0; loop
< vlocation
->vldb
.nservers
; loop
++)
159 srvtmask
|= vlocation
->vldb
.srvtmask
[loop
];
162 if (!(srvtmask
& (1 << type
)))
165 else if (srvtmask
& AFS_VOL_VTM_RO
) {
168 else if (srvtmask
& AFS_VOL_VTM_RW
) {
175 down_write(&cell
->vl_sem
);
177 /* is the volume already active? */
178 if (vlocation
->vols
[type
]) {
179 /* yes - re-use it */
180 volume
= vlocation
->vols
[type
];
181 afs_get_volume(volume
);
185 /* create a new volume record */
186 _debug("creating new volume record");
189 volume
= kmalloc(sizeof(struct afs_volume
), GFP_KERNEL
);
193 memset(volume
, 0, sizeof(struct afs_volume
));
194 atomic_set(&volume
->usage
, 1);
196 volume
->type_force
= force
;
198 volume
->vid
= vlocation
->vldb
.vid
[type
];
200 init_rwsem(&volume
->server_sem
);
202 /* look up all the applicable server records */
203 for (loop
= 0; loop
< 8; loop
++) {
204 if (vlocation
->vldb
.srvtmask
[loop
] & (1 << volume
->type
)) {
205 ret
= afs_server_lookup(
207 &vlocation
->vldb
.servers
[loop
],
208 &volume
->servers
[volume
->nservers
]);
216 /* attach the cache and volume location */
217 #ifdef AFS_CACHING_SUPPORT
218 cachefs_acquire_cookie(vlocation
->cache
,
219 &afs_vnode_cache_index_def
,
224 afs_get_vlocation(vlocation
);
225 volume
->vlocation
= vlocation
;
227 vlocation
->vols
[type
] = volume
;
230 _debug("kAFS selected %s volume %08x",
231 afs_voltypes
[volume
->type
], volume
->vid
);
237 up_write(&cell
->vl_sem
);
239 afs_put_vlocation(vlocation
);
242 _leave(" = %d (%p)", ret
, volume
);
246 up_write(&cell
->vl_sem
);
248 for (loop
= volume
->nservers
- 1; loop
>= 0; loop
--)
249 afs_put_server(volume
->servers
[loop
]);
253 } /* end afs_volume_lookup() */
255 /*****************************************************************************/
257 * destroy a volume record
259 void afs_put_volume(struct afs_volume
*volume
)
261 struct afs_vlocation
*vlocation
;
267 _enter("%p", volume
);
269 vlocation
= volume
->vlocation
;
272 BUG_ON(atomic_read(&volume
->usage
) <= 0);
274 /* to prevent a race, the decrement and the dequeue must be effectively
276 down_write(&vlocation
->cell
->vl_sem
);
278 if (likely(!atomic_dec_and_test(&volume
->usage
))) {
279 up_write(&vlocation
->cell
->vl_sem
);
284 vlocation
->vols
[volume
->type
] = NULL
;
286 up_write(&vlocation
->cell
->vl_sem
);
288 /* finish cleaning up the volume */
289 #ifdef AFS_CACHING_SUPPORT
290 cachefs_relinquish_cookie(volume
->cache
, 0);
292 afs_put_vlocation(vlocation
);
294 for (loop
= volume
->nservers
- 1; loop
>= 0; loop
--)
295 afs_put_server(volume
->servers
[loop
]);
299 _leave(" [destroyed]");
300 } /* end afs_put_volume() */
302 /*****************************************************************************/
304 * pick a server to use to try accessing this volume
305 * - returns with an elevated usage count on the server chosen
307 int afs_volume_pick_fileserver(struct afs_volume
*volume
,
308 struct afs_server
**_server
)
310 struct afs_server
*server
;
311 int ret
, state
, loop
;
313 _enter("%s", volume
->vlocation
->vldb
.name
);
315 down_read(&volume
->server_sem
);
317 /* handle the no-server case */
318 if (volume
->nservers
== 0) {
319 ret
= volume
->rjservers
? -ENOMEDIUM
: -ESTALE
;
320 up_read(&volume
->server_sem
);
321 _leave(" = %d [no servers]", ret
);
325 /* basically, just search the list for the first live server and use
328 for (loop
= 0; loop
< volume
->nservers
; loop
++) {
329 server
= volume
->servers
[loop
];
330 state
= server
->fs_state
;
333 /* found an apparently healthy server */
335 afs_get_server(server
);
336 up_read(&volume
->server_sem
);
338 _leave(" = 0 (picked %08x)",
339 ntohl(server
->addr
.s_addr
));
355 ret
== -ENETUNREACH
||
356 ret
== -EHOSTUNREACH
)
363 ret
== -ENETUNREACH
||
364 ret
== -EHOSTUNREACH
||
365 ret
== -ECONNREFUSED
)
371 /* no available servers
372 * - TODO: handle the no active servers case better
374 up_read(&volume
->server_sem
);
375 _leave(" = %d", ret
);
377 } /* end afs_volume_pick_fileserver() */
379 /*****************************************************************************/
381 * release a server after use
382 * - releases the ref on the server struct that was acquired by picking
383 * - records result of using a particular server to access a volume
384 * - return 0 to try again, 1 if okay or to issue error
386 int afs_volume_release_fileserver(struct afs_volume
*volume
,
387 struct afs_server
*server
,
393 volume
->vlocation
->vldb
.name
, ntohl(server
->addr
.s_addr
),
399 server
->fs_act_jif
= jiffies
;
402 /* the fileserver denied all knowledge of the volume */
404 server
->fs_act_jif
= jiffies
;
405 down_write(&volume
->server_sem
);
407 /* first, find where the server is in the active list (if it
409 for (loop
= 0; loop
< volume
->nservers
; loop
++)
410 if (volume
->servers
[loop
] == server
)
413 /* no longer there - may have been discarded by another op */
414 goto try_next_server_upw
;
418 memmove(&volume
->servers
[loop
],
419 &volume
->servers
[loop
+ 1],
420 sizeof(volume
->servers
[loop
]) *
421 (volume
->nservers
- loop
));
422 volume
->servers
[volume
->nservers
] = NULL
;
423 afs_put_server(server
);
426 if (volume
->nservers
> 0)
427 /* another server might acknowledge its existence */
428 goto try_next_server_upw
;
430 /* handle the case where all the fileservers have rejected the
432 * - TODO: try asking the fileservers for volume information
433 * - TODO: contact the VL server again to see if the volume is
434 * no longer registered
436 up_write(&volume
->server_sem
);
437 afs_put_server(server
);
438 _leave(" [completely rejected]");
441 /* problem reaching the server */
447 /* mark the server as dead
448 * TODO: vary dead timeout depending on error
450 spin_lock(&server
->fs_lock
);
451 if (!server
->fs_state
) {
452 server
->fs_dead_jif
= jiffies
+ HZ
* 10;
453 server
->fs_state
= result
;
454 printk("kAFS: SERVER DEAD state=%d\n", result
);
456 spin_unlock(&server
->fs_lock
);
457 goto try_next_server
;
459 /* miscellaneous error */
461 server
->fs_act_jif
= jiffies
;
467 /* tell the caller to accept the result */
468 afs_put_server(server
);
472 /* tell the caller to loop around and try the next server */
474 up_write(&volume
->server_sem
);
476 afs_put_server(server
);
477 _leave(" [try next server]");
480 } /* end afs_volume_release_fileserver() */
482 /*****************************************************************************/
484 * match a volume hash record stored in the cache
486 #ifdef AFS_CACHING_SUPPORT
487 static cachefs_match_val_t
afs_volume_cache_match(void *target
,
490 const struct afs_cache_vhash
*vhash
= entry
;
491 struct afs_volume
*volume
= target
;
493 _enter("{%u},{%u}", volume
->type
, vhash
->vtype
);
495 if (volume
->type
== vhash
->vtype
) {
496 _leave(" = SUCCESS");
497 return CACHEFS_MATCH_SUCCESS
;
501 return CACHEFS_MATCH_FAILED
;
502 } /* end afs_volume_cache_match() */
505 /*****************************************************************************/
507 * update a volume hash record stored in the cache
509 #ifdef AFS_CACHING_SUPPORT
510 static void afs_volume_cache_update(void *source
, void *entry
)
512 struct afs_cache_vhash
*vhash
= entry
;
513 struct afs_volume
*volume
= source
;
517 vhash
->vtype
= volume
->type
;
519 } /* end afs_volume_cache_update() */