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>
18 #include <linux/fscache.h>
22 #include "cmservice.h"
28 static const char *afs_voltypes[] = { "R/W", "R/O", "BAK" };
31 #ifdef CONFIG_AFS_FSCACHE
32 static uint16_t afs_volume_cache_get_key(const void *cookie_netfs_data,
33 void *buffer, uint16_t buflen);
35 static struct fscache_cookie_def afs_volume_cache_index_def = {
37 .type = FSCACHE_COOKIE_TYPE_INDEX,
38 .get_key = afs_volume_cache_get_key,
42 /*****************************************************************************/
44 * lookup a volume by name
45 * - this can be one of the following:
46 * "%[cell:]volume[.]" R/W volume
47 * "#[cell:]volume[.]" R/O or R/W volume (rwparent=0),
48 * or R/W (rwparent=1) volume
49 * "%[cell:]volume.readonly" R/O volume
50 * "#[cell:]volume.readonly" R/O volume
51 * "%[cell:]volume.backup" Backup volume
52 * "#[cell:]volume.backup" Backup volume
54 * The cell name is optional, and defaults to the current cell.
56 * See "The Rules of Mount Point Traversal" in Chapter 5 of the AFS SysAdmin
58 * - Rule 1: Explicit type suffix forces access of that type or nothing
59 * (no suffix, then use Rule 2 & 3)
60 * - Rule 2: If parent volume is R/O, then mount R/O volume by preference, R/W
62 * - Rule 3: If parent volume is R/W, then only mount R/W volume unless
63 * explicitly told otherwise
65 int afs_volume_lookup(const char *name, struct afs_cell *cell, int rwpath,
66 struct afs_volume **_volume)
68 struct afs_vlocation *vlocation = NULL;
69 struct afs_volume *volume = NULL;
71 const char *cellname, *volname, *suffix;
73 int force, ret, loop, cellnamesz, volnamesz;
75 _enter("%s,,%d,", name, rwpath);
77 if (!name || (name[0] != '%' && name[0] != '#') || !name[1]) {
78 printk("kAFS: unparsable volume name\n");
82 /* determine the type of volume we're looking for */
86 if (rwpath || name[0] == '%') {
91 suffix = strrchr(name, '.');
93 if (strcmp(suffix, ".readonly") == 0) {
97 else if (strcmp(suffix, ".backup") == 0) {
101 else if (suffix[1] == 0) {
108 /* split the cell and volume names */
110 volname = strchr(name, ':');
113 cellnamesz = volname - name;
122 volnamesz = suffix ? suffix - volname : strlen(volname);
124 _debug("CELL:%*.*s [%p] VOLUME:%*.*s SUFFIX:%s TYPE:%d%s",
125 cellnamesz, cellnamesz, cellname ?: "", cell,
126 volnamesz, volnamesz, volname, suffix ?: "-",
128 force ? " FORCE" : "");
130 /* lookup the cell record */
131 if (cellname || !cell) {
132 ret = afs_cell_lookup(cellname, cellnamesz, &cell);
134 printk("kAFS: unable to lookup cell '%s'\n",
143 /* lookup the volume location record */
144 ret = afs_vlocation_lookup(cell, volname, volnamesz, &vlocation);
148 /* make the final decision on the type we want */
150 if (force && !(vlocation->vldb.vidmask & (1 << type)))
154 for (loop = 0; loop < vlocation->vldb.nservers; loop++)
155 srvtmask |= vlocation->vldb.srvtmask[loop];
158 if (!(srvtmask & (1 << type)))
161 else if (srvtmask & AFS_VOL_VTM_RO) {
164 else if (srvtmask & AFS_VOL_VTM_RW) {
171 down_write(&cell->vl_sem);
173 /* is the volume already active? */
174 if (vlocation->vols[type]) {
175 /* yes - re-use it */
176 volume = vlocation->vols[type];
177 afs_get_volume(volume);
181 /* create a new volume record */
182 _debug("creating new volume record");
185 volume = kmalloc(sizeof(struct afs_volume), GFP_KERNEL);
189 memset(volume, 0, sizeof(struct afs_volume));
190 atomic_set(&volume->usage, 1);
192 volume->type_force = force;
194 volume->vid = vlocation->vldb.vid[type];
196 init_rwsem(&volume->server_sem);
198 /* look up all the applicable server records */
199 for (loop = 0; loop < 8; loop++) {
200 if (vlocation->vldb.srvtmask[loop] & (1 << volume->type)) {
201 ret = afs_server_lookup(
203 &vlocation->vldb.servers[loop],
204 &volume->servers[volume->nservers]);
212 /* attach the cache and volume location */
213 #ifdef CONFIG_AFS_FSCACHE
214 volume->cache = fscache_acquire_cookie(vlocation->cache,
215 &afs_volume_cache_index_def,
219 afs_get_vlocation(vlocation);
220 volume->vlocation = vlocation;
222 vlocation->vols[type] = volume;
225 _debug("kAFS selected %s volume %08x",
226 afs_voltypes[volume->type], volume->vid);
232 up_write(&cell->vl_sem);
234 afs_put_vlocation(vlocation);
237 _leave(" = %d (%p)", ret, volume);
241 up_write(&cell->vl_sem);
243 for (loop = volume->nservers - 1; loop >= 0; loop--)
244 afs_put_server(volume->servers[loop]);
248 } /* end afs_volume_lookup() */
250 /*****************************************************************************/
252 * destroy a volume record
254 void afs_put_volume(struct afs_volume *volume)
256 struct afs_vlocation *vlocation;
262 _enter("%p", volume);
264 vlocation = volume->vlocation;
267 BUG_ON(atomic_read(&volume->usage) <= 0);
269 /* to prevent a race, the decrement and the dequeue must be effectively
271 down_write(&vlocation->cell->vl_sem);
273 if (likely(!atomic_dec_and_test(&volume->usage))) {
274 up_write(&vlocation->cell->vl_sem);
279 vlocation->vols[volume->type] = NULL;
281 up_write(&vlocation->cell->vl_sem);
283 /* finish cleaning up the volume */
284 #ifdef CONFIG_AFS_FSCACHE
285 fscache_relinquish_cookie(volume->cache, 0);
287 afs_put_vlocation(vlocation);
289 for (loop = volume->nservers - 1; loop >= 0; loop--)
290 afs_put_server(volume->servers[loop]);
294 _leave(" [destroyed]");
295 } /* end afs_put_volume() */
297 /*****************************************************************************/
299 * pick a server to use to try accessing this volume
300 * - returns with an elevated usage count on the server chosen
302 int afs_volume_pick_fileserver(struct afs_volume *volume,
303 struct afs_server **_server)
305 struct afs_server *server;
306 int ret, state, loop;
308 _enter("%s", volume->vlocation->vldb.name);
310 down_read(&volume->server_sem);
312 /* handle the no-server case */
313 if (volume->nservers == 0) {
314 ret = volume->rjservers ? -ENOMEDIUM : -ESTALE;
315 up_read(&volume->server_sem);
316 _leave(" = %d [no servers]", ret);
320 /* basically, just search the list for the first live server and use
323 for (loop = 0; loop < volume->nservers; loop++) {
324 server = volume->servers[loop];
325 state = server->fs_state;
328 /* found an apparently healthy server */
330 afs_get_server(server);
331 up_read(&volume->server_sem);
333 _leave(" = 0 (picked %08x)",
334 ntohl(server->addr.s_addr));
350 ret == -ENETUNREACH ||
351 ret == -EHOSTUNREACH)
358 ret == -ENETUNREACH ||
359 ret == -EHOSTUNREACH ||
360 ret == -ECONNREFUSED)
366 /* no available servers
367 * - TODO: handle the no active servers case better
369 up_read(&volume->server_sem);
370 _leave(" = %d", ret);
372 } /* end afs_volume_pick_fileserver() */
374 /*****************************************************************************/
376 * release a server after use
377 * - releases the ref on the server struct that was acquired by picking
378 * - records result of using a particular server to access a volume
379 * - return 0 to try again, 1 if okay or to issue error
381 int afs_volume_release_fileserver(struct afs_volume *volume,
382 struct afs_server *server,
388 volume->vlocation->vldb.name, ntohl(server->addr.s_addr),
394 server->fs_act_jif = jiffies;
397 /* the fileserver denied all knowledge of the volume */
399 server->fs_act_jif = jiffies;
400 down_write(&volume->server_sem);
402 /* first, find where the server is in the active list (if it
404 for (loop = 0; loop < volume->nservers; loop++)
405 if (volume->servers[loop] == server)
408 /* no longer there - may have been discarded by another op */
409 goto try_next_server_upw;
413 memmove(&volume->servers[loop],
414 &volume->servers[loop + 1],
415 sizeof(volume->servers[loop]) *
416 (volume->nservers - loop));
417 volume->servers[volume->nservers] = NULL;
418 afs_put_server(server);
421 if (volume->nservers > 0)
422 /* another server might acknowledge its existence */
423 goto try_next_server_upw;
425 /* handle the case where all the fileservers have rejected the
427 * - TODO: try asking the fileservers for volume information
428 * - TODO: contact the VL server again to see if the volume is
429 * no longer registered
431 up_write(&volume->server_sem);
432 afs_put_server(server);
433 _leave(" [completely rejected]");
436 /* problem reaching the server */
442 /* mark the server as dead
443 * TODO: vary dead timeout depending on error
445 spin_lock(&server->fs_lock);
446 if (!server->fs_state) {
447 server->fs_dead_jif = jiffies + HZ * 10;
448 server->fs_state = result;
449 printk("kAFS: SERVER DEAD state=%d\n", result);
451 spin_unlock(&server->fs_lock);
452 goto try_next_server;
454 /* miscellaneous error */
456 server->fs_act_jif = jiffies;
462 /* tell the caller to accept the result */
463 afs_put_server(server);
467 /* tell the caller to loop around and try the next server */
469 up_write(&volume->server_sem);
471 afs_put_server(server);
472 _leave(" [try next server]");
475 } /* end afs_volume_release_fileserver() */
477 /*****************************************************************************/
479 * set the key for the index entry
481 #ifdef CONFIG_AFS_FSCACHE
482 static uint16_t afs_volume_cache_get_key(const void *cookie_netfs_data,
483 void *buffer, uint16_t bufmax)
485 const struct afs_volume *volume = cookie_netfs_data;
488 _enter("{%u},%p,%u", volume->type, buffer, bufmax);
490 klen = sizeof(volume->type);
494 memcpy(buffer, &volume->type, sizeof(volume->type));
496 _leave(" = %u", klen);
499 } /* end afs_volume_cache_get_key() */