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"
27 const char *afs_voltypes[] = { "R/W", "R/O", "BAK" };
29 #ifdef AFS_CACHING_SUPPORT
30 static cachefs_match_val_t afs_volume_cache_match(void *target,
32 static void afs_volume_cache_update(void *source, void *entry);
34 struct cachefs_index_def afs_volume_cache_index_def = {
36 .data_size = sizeof(struct afs_cache_vhash),
37 .keys[0] = { CACHEFS_INDEX_KEYS_BIN, 1 },
38 .keys[1] = { CACHEFS_INDEX_KEYS_BIN, 1 },
39 .match = afs_volume_cache_match,
40 .update = afs_volume_cache_update,
44 /*****************************************************************************/
46 * lookup a volume by name
47 * - this can be one of the following:
48 * "%[cell:]volume[.]" R/W volume
49 * "#[cell:]volume[.]" R/O or R/W volume (rwparent=0),
50 * or R/W (rwparent=1) volume
51 * "%[cell:]volume.readonly" R/O volume
52 * "#[cell:]volume.readonly" R/O volume
53 * "%[cell:]volume.backup" Backup volume
54 * "#[cell:]volume.backup" Backup volume
56 * The cell name is optional, and defaults to the current cell.
58 * See "The Rules of Mount Point Traversal" in Chapter 5 of the AFS SysAdmin
60 * - Rule 1: Explicit type suffix forces access of that type or nothing
61 * (no suffix, then use Rule 2 & 3)
62 * - Rule 2: If parent volume is R/O, then mount R/O volume by preference, R/W
64 * - Rule 3: If parent volume is R/W, then only mount R/W volume unless
65 * explicitly told otherwise
67 int afs_volume_lookup(const char *name, struct afs_cell *cell, int rwpath,
68 struct afs_volume **_volume)
70 struct afs_vlocation *vlocation = NULL;
71 struct afs_volume *volume = NULL;
73 const char *cellname, *volname, *suffix;
75 int force, ret, loop, cellnamesz, volnamesz;
77 _enter("%s,,%d,", name, rwpath);
79 if (!name || (name[0] != '%' && name[0] != '#') || !name[1]) {
80 printk("kAFS: unparsable volume name\n");
84 /* determine the type of volume we're looking for */
88 if (rwpath || name[0] == '%') {
93 suffix = strrchr(name, '.');
95 if (strcmp(suffix, ".readonly") == 0) {
99 else if (strcmp(suffix, ".backup") == 0) {
100 type = AFSVL_BACKVOL;
103 else if (suffix[1] == 0) {
110 /* split the cell and volume names */
112 volname = strchr(name, ':');
115 cellnamesz = volname - name;
124 volnamesz = suffix ? suffix - volname : strlen(volname);
126 _debug("CELL:%*.*s [%p] VOLUME:%*.*s SUFFIX:%s TYPE:%d%s",
127 cellnamesz, cellnamesz, cellname ?: "", cell,
128 volnamesz, volnamesz, volname, suffix ?: "-",
130 force ? " FORCE" : "");
132 /* lookup the cell record */
133 if (cellname || !cell) {
134 ret = afs_cell_lookup(cellname, cellnamesz, &cell);
136 printk("kAFS: unable to lookup cell '%s'\n",
145 /* lookup the volume location record */
146 ret = afs_vlocation_lookup(cell, volname, volnamesz, &vlocation);
150 /* make the final decision on the type we want */
152 if (force && !(vlocation->vldb.vidmask & (1 << type)))
156 for (loop = 0; loop < vlocation->vldb.nservers; loop++)
157 srvtmask |= vlocation->vldb.srvtmask[loop];
160 if (!(srvtmask & (1 << type)))
163 else if (srvtmask & AFS_VOL_VTM_RO) {
166 else if (srvtmask & AFS_VOL_VTM_RW) {
173 down_write(&cell->vl_sem);
175 /* is the volume already active? */
176 if (vlocation->vols[type]) {
177 /* yes - re-use it */
178 volume = vlocation->vols[type];
179 afs_get_volume(volume);
183 /* create a new volume record */
184 _debug("creating new volume record");
187 volume = kmalloc(sizeof(struct afs_volume), GFP_KERNEL);
191 memset(volume, 0, sizeof(struct afs_volume));
192 atomic_set(&volume->usage, 1);
194 volume->type_force = force;
196 volume->vid = vlocation->vldb.vid[type];
198 init_rwsem(&volume->server_sem);
200 /* look up all the applicable server records */
201 for (loop = 0; loop < 8; loop++) {
202 if (vlocation->vldb.srvtmask[loop] & (1 << volume->type)) {
203 ret = afs_server_lookup(
205 &vlocation->vldb.servers[loop],
206 &volume->servers[volume->nservers]);
214 /* attach the cache and volume location */
215 #ifdef AFS_CACHING_SUPPORT
216 cachefs_acquire_cookie(vlocation->cache,
217 &afs_vnode_cache_index_def,
222 afs_get_vlocation(vlocation);
223 volume->vlocation = vlocation;
225 vlocation->vols[type] = volume;
228 _debug("kAFS selected %s volume %08x",
229 afs_voltypes[volume->type], volume->vid);
235 up_write(&cell->vl_sem);
237 afs_put_vlocation(vlocation);
240 _leave(" = %d (%p)", ret, volume);
244 up_write(&cell->vl_sem);
246 for (loop = volume->nservers - 1; loop >= 0; loop--)
247 afs_put_server(volume->servers[loop]);
251 } /* end afs_volume_lookup() */
253 /*****************************************************************************/
255 * destroy a volume record
257 void afs_put_volume(struct afs_volume *volume)
259 struct afs_vlocation *vlocation;
265 _enter("%p", volume);
267 vlocation = volume->vlocation;
270 BUG_ON(atomic_read(&volume->usage) <= 0);
272 /* to prevent a race, the decrement and the dequeue must be effectively
274 down_write(&vlocation->cell->vl_sem);
276 if (likely(!atomic_dec_and_test(&volume->usage))) {
277 up_write(&vlocation->cell->vl_sem);
282 vlocation->vols[volume->type] = NULL;
284 up_write(&vlocation->cell->vl_sem);
286 /* finish cleaning up the volume */
287 #ifdef AFS_CACHING_SUPPORT
288 cachefs_relinquish_cookie(volume->cache, 0);
290 afs_put_vlocation(vlocation);
292 for (loop = volume->nservers - 1; loop >= 0; loop--)
293 afs_put_server(volume->servers[loop]);
297 _leave(" [destroyed]");
298 } /* end afs_put_volume() */
300 /*****************************************************************************/
302 * pick a server to use to try accessing this volume
303 * - returns with an elevated usage count on the server chosen
305 int afs_volume_pick_fileserver(struct afs_volume *volume,
306 struct afs_server **_server)
308 struct afs_server *server;
309 int ret, state, loop;
311 _enter("%s", volume->vlocation->vldb.name);
313 down_read(&volume->server_sem);
315 /* handle the no-server case */
316 if (volume->nservers == 0) {
317 ret = volume->rjservers ? -ENOMEDIUM : -ESTALE;
318 up_read(&volume->server_sem);
319 _leave(" = %d [no servers]", ret);
323 /* basically, just search the list for the first live server and use
326 for (loop = 0; loop < volume->nservers; loop++) {
327 server = volume->servers[loop];
328 state = server->fs_state;
331 /* found an apparently healthy server */
333 afs_get_server(server);
334 up_read(&volume->server_sem);
336 _leave(" = 0 (picked %08x)",
337 ntohl(server->addr.s_addr));
353 ret == -ENETUNREACH ||
354 ret == -EHOSTUNREACH)
361 ret == -ENETUNREACH ||
362 ret == -EHOSTUNREACH ||
363 ret == -ECONNREFUSED)
369 /* no available servers
370 * - TODO: handle the no active servers case better
372 up_read(&volume->server_sem);
373 _leave(" = %d", ret);
375 } /* end afs_volume_pick_fileserver() */
377 /*****************************************************************************/
379 * release a server after use
380 * - releases the ref on the server struct that was acquired by picking
381 * - records result of using a particular server to access a volume
382 * - return 0 to try again, 1 if okay or to issue error
384 int afs_volume_release_fileserver(struct afs_volume *volume,
385 struct afs_server *server,
391 volume->vlocation->vldb.name, ntohl(server->addr.s_addr),
397 server->fs_act_jif = jiffies;
400 /* the fileserver denied all knowledge of the volume */
402 server->fs_act_jif = jiffies;
403 down_write(&volume->server_sem);
405 /* first, find where the server is in the active list (if it
407 for (loop = 0; loop < volume->nservers; loop++)
408 if (volume->servers[loop] == server)
411 /* no longer there - may have been discarded by another op */
412 goto try_next_server_upw;
416 memmove(&volume->servers[loop],
417 &volume->servers[loop + 1],
418 sizeof(volume->servers[loop]) *
419 (volume->nservers - loop));
420 volume->servers[volume->nservers] = NULL;
421 afs_put_server(server);
424 if (volume->nservers > 0)
425 /* another server might acknowledge its existence */
426 goto try_next_server_upw;
428 /* handle the case where all the fileservers have rejected the
430 * - TODO: try asking the fileservers for volume information
431 * - TODO: contact the VL server again to see if the volume is
432 * no longer registered
434 up_write(&volume->server_sem);
435 afs_put_server(server);
436 _leave(" [completely rejected]");
439 /* problem reaching the server */
445 /* mark the server as dead
446 * TODO: vary dead timeout depending on error
448 spin_lock(&server->fs_lock);
449 if (!server->fs_state) {
450 server->fs_dead_jif = jiffies + HZ * 10;
451 server->fs_state = result;
452 printk("kAFS: SERVER DEAD state=%d\n", result);
454 spin_unlock(&server->fs_lock);
455 goto try_next_server;
457 /* miscellaneous error */
459 server->fs_act_jif = jiffies;
465 /* tell the caller to accept the result */
466 afs_put_server(server);
470 /* tell the caller to loop around and try the next server */
472 up_write(&volume->server_sem);
474 afs_put_server(server);
475 _leave(" [try next server]");
478 } /* end afs_volume_release_fileserver() */
480 /*****************************************************************************/
482 * match a volume hash record stored in the cache
484 #ifdef AFS_CACHING_SUPPORT
485 static cachefs_match_val_t afs_volume_cache_match(void *target,
488 const struct afs_cache_vhash *vhash = entry;
489 struct afs_volume *volume = target;
491 _enter("{%u},{%u}", volume->type, vhash->vtype);
493 if (volume->type == vhash->vtype) {
494 _leave(" = SUCCESS");
495 return CACHEFS_MATCH_SUCCESS;
499 return CACHEFS_MATCH_FAILED;
500 } /* end afs_volume_cache_match() */
503 /*****************************************************************************/
505 * update a volume hash record stored in the cache
507 #ifdef AFS_CACHING_SUPPORT
508 static void afs_volume_cache_update(void *source, void *entry)
510 struct afs_cache_vhash *vhash = entry;
511 struct afs_volume *volume = source;
515 vhash->vtype = volume->type;
517 } /* end afs_volume_cache_update() */