1 /* 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,
47 * lookup a volume by name
48 * - this can be one of the following:
49 * "%[cell:]volume[.]" R/W volume
50 * "#[cell:]volume[.]" R/O or R/W volume (rwparent=0),
51 * or R/W (rwparent=1) volume
52 * "%[cell:]volume.readonly" R/O volume
53 * "#[cell:]volume.readonly" R/O volume
54 * "%[cell:]volume.backup" Backup volume
55 * "#[cell:]volume.backup" Backup volume
57 * The cell name is optional, and defaults to the current cell.
59 * See "The Rules of Mount Point Traversal" in Chapter 5 of the AFS SysAdmin
61 * - Rule 1: Explicit type suffix forces access of that type or nothing
62 * (no suffix, then use Rule 2 & 3)
63 * - Rule 2: If parent volume is R/O, then mount R/O volume by preference, R/W
65 * - Rule 3: If parent volume is R/W, then only mount R/W volume unless
66 * explicitly told otherwise
68 int afs_volume_lookup(const char *name, struct afs_cell *cell, int rwpath,
69 struct afs_volume **_volume)
71 struct afs_vlocation *vlocation = NULL;
72 struct afs_volume *volume = NULL;
74 const char *cellname, *volname, *suffix;
76 int force, ret, loop, cellnamesz, volnamesz;
78 _enter("%s,,%d,", name, rwpath);
80 if (!name || (name[0] != '%' && name[0] != '#') || !name[1]) {
81 printk("kAFS: unparsable volume name\n");
85 /* determine the type of volume we're looking for */
89 if (rwpath || name[0] == '%') {
94 suffix = strrchr(name, '.');
96 if (strcmp(suffix, ".readonly") == 0) {
99 } else if (strcmp(suffix, ".backup") == 0) {
100 type = AFSVL_BACKVOL;
102 } else if (suffix[1] == 0) {
108 /* split the cell and volume names */
110 volname = strchr(name, ':');
113 cellnamesz = volname - name;
121 volnamesz = suffix ? suffix - volname : strlen(volname);
123 _debug("CELL:%*.*s [%p] VOLUME:%*.*s SUFFIX:%s TYPE:%d%s",
124 cellnamesz, cellnamesz, cellname ?: "", cell,
125 volnamesz, volnamesz, volname, suffix ?: "-",
127 force ? " FORCE" : "");
129 /* lookup the cell record */
130 if (cellname || !cell) {
131 ret = afs_cell_lookup(cellname, cellnamesz, &cell);
133 printk("kAFS: unable to lookup cell '%s'\n",
141 /* lookup the volume location record */
142 ret = afs_vlocation_lookup(cell, volname, volnamesz, &vlocation);
146 /* make the final decision on the type we want */
148 if (force && !(vlocation->vldb.vidmask & (1 << type)))
152 for (loop = 0; loop < vlocation->vldb.nservers; loop++)
153 srvtmask |= vlocation->vldb.srvtmask[loop];
156 if (!(srvtmask & (1 << type)))
158 } else if (srvtmask & AFS_VOL_VTM_RO) {
160 } else if (srvtmask & AFS_VOL_VTM_RW) {
166 down_write(&cell->vl_sem);
168 /* is the volume already active? */
169 if (vlocation->vols[type]) {
170 /* yes - re-use it */
171 volume = vlocation->vols[type];
172 afs_get_volume(volume);
176 /* create a new volume record */
177 _debug("creating new volume record");
180 volume = kzalloc(sizeof(struct afs_volume), GFP_KERNEL);
184 atomic_set(&volume->usage, 1);
186 volume->type_force = force;
188 volume->vid = vlocation->vldb.vid[type];
190 init_rwsem(&volume->server_sem);
192 /* look up all the applicable server records */
193 for (loop = 0; loop < 8; loop++) {
194 if (vlocation->vldb.srvtmask[loop] & (1 << volume->type)) {
195 ret = afs_server_lookup(
197 &vlocation->vldb.servers[loop],
198 &volume->servers[volume->nservers]);
206 /* attach the cache and volume location */
207 #ifdef AFS_CACHING_SUPPORT
208 cachefs_acquire_cookie(vlocation->cache,
209 &afs_vnode_cache_index_def,
214 afs_get_vlocation(vlocation);
215 volume->vlocation = vlocation;
217 vlocation->vols[type] = volume;
220 _debug("kAFS selected %s volume %08x",
221 afs_voltypes[volume->type], volume->vid);
227 up_write(&cell->vl_sem);
229 afs_put_vlocation(vlocation);
232 _leave(" = %d (%p)", ret, volume);
236 up_write(&cell->vl_sem);
238 for (loop = volume->nservers - 1; loop >= 0; loop--)
239 afs_put_server(volume->servers[loop]);
246 * destroy a volume record
248 void afs_put_volume(struct afs_volume *volume)
250 struct afs_vlocation *vlocation;
256 _enter("%p", volume);
258 vlocation = volume->vlocation;
261 BUG_ON(atomic_read(&volume->usage) <= 0);
263 /* to prevent a race, the decrement and the dequeue must be effectively
265 down_write(&vlocation->cell->vl_sem);
267 if (likely(!atomic_dec_and_test(&volume->usage))) {
268 up_write(&vlocation->cell->vl_sem);
273 vlocation->vols[volume->type] = NULL;
275 up_write(&vlocation->cell->vl_sem);
277 /* finish cleaning up the volume */
278 #ifdef AFS_CACHING_SUPPORT
279 cachefs_relinquish_cookie(volume->cache, 0);
281 afs_put_vlocation(vlocation);
283 for (loop = volume->nservers - 1; loop >= 0; loop--)
284 afs_put_server(volume->servers[loop]);
288 _leave(" [destroyed]");
292 * pick a server to use to try accessing this volume
293 * - returns with an elevated usage count on the server chosen
295 int afs_volume_pick_fileserver(struct afs_volume *volume,
296 struct afs_server **_server)
298 struct afs_server *server;
299 int ret, state, loop;
301 _enter("%s", volume->vlocation->vldb.name);
303 down_read(&volume->server_sem);
305 /* handle the no-server case */
306 if (volume->nservers == 0) {
307 ret = volume->rjservers ? -ENOMEDIUM : -ESTALE;
308 up_read(&volume->server_sem);
309 _leave(" = %d [no servers]", ret);
313 /* basically, just search the list for the first live server and use
316 for (loop = 0; loop < volume->nservers; loop++) {
317 server = volume->servers[loop];
318 state = server->fs_state;
321 /* found an apparently healthy server */
323 afs_get_server(server);
324 up_read(&volume->server_sem);
326 _leave(" = 0 (picked %08x)",
327 ntohl(server->addr.s_addr));
343 ret == -ENETUNREACH ||
344 ret == -EHOSTUNREACH)
351 ret == -ENETUNREACH ||
352 ret == -EHOSTUNREACH ||
353 ret == -ECONNREFUSED)
359 /* no available servers
360 * - TODO: handle the no active servers case better
362 up_read(&volume->server_sem);
363 _leave(" = %d", ret);
368 * release a server after use
369 * - releases the ref on the server struct that was acquired by picking
370 * - records result of using a particular server to access a volume
371 * - return 0 to try again, 1 if okay or to issue error
373 int afs_volume_release_fileserver(struct afs_volume *volume,
374 struct afs_server *server,
380 volume->vlocation->vldb.name, ntohl(server->addr.s_addr),
386 server->fs_act_jif = jiffies;
389 /* the fileserver denied all knowledge of the volume */
391 server->fs_act_jif = jiffies;
392 down_write(&volume->server_sem);
394 /* first, find where the server is in the active list (if it
396 for (loop = 0; loop < volume->nservers; loop++)
397 if (volume->servers[loop] == server)
400 /* no longer there - may have been discarded by another op */
401 goto try_next_server_upw;
405 memmove(&volume->servers[loop],
406 &volume->servers[loop + 1],
407 sizeof(volume->servers[loop]) *
408 (volume->nservers - loop));
409 volume->servers[volume->nservers] = NULL;
410 afs_put_server(server);
413 if (volume->nservers > 0)
414 /* another server might acknowledge its existence */
415 goto try_next_server_upw;
417 /* handle the case where all the fileservers have rejected the
419 * - TODO: try asking the fileservers for volume information
420 * - TODO: contact the VL server again to see if the volume is
421 * no longer registered
423 up_write(&volume->server_sem);
424 afs_put_server(server);
425 _leave(" [completely rejected]");
428 /* problem reaching the server */
434 /* mark the server as dead
435 * TODO: vary dead timeout depending on error
437 spin_lock(&server->fs_lock);
438 if (!server->fs_state) {
439 server->fs_dead_jif = jiffies + HZ * 10;
440 server->fs_state = result;
441 printk("kAFS: SERVER DEAD state=%d\n", result);
443 spin_unlock(&server->fs_lock);
444 goto try_next_server;
446 /* miscellaneous error */
448 server->fs_act_jif = jiffies;
454 /* tell the caller to accept the result */
455 afs_put_server(server);
459 /* tell the caller to loop around and try the next server */
461 up_write(&volume->server_sem);
463 afs_put_server(server);
464 _leave(" [try next server]");
469 * match a volume hash record stored in the cache
471 #ifdef AFS_CACHING_SUPPORT
472 static cachefs_match_val_t afs_volume_cache_match(void *target,
475 const struct afs_cache_vhash *vhash = entry;
476 struct afs_volume *volume = target;
478 _enter("{%u},{%u}", volume->type, vhash->vtype);
480 if (volume->type == vhash->vtype) {
481 _leave(" = SUCCESS");
482 return CACHEFS_MATCH_SUCCESS;
486 return CACHEFS_MATCH_FAILED;
491 * update a volume hash record stored in the cache
493 #ifdef AFS_CACHING_SUPPORT
494 static void afs_volume_cache_update(void *source, void *entry)
496 struct afs_cache_vhash *vhash = entry;
497 struct afs_volume *volume = source;
501 vhash->vtype = volume->type;