volume.c 5.3 KB
Newer Older
D
David Howells 已提交
1
/* AFS volume management
L
Linus Torvalds 已提交
2
 *
3
 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
L
Linus Torvalds 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 * Written by David Howells (dhowells@redhat.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

#include <linux/kernel.h>
#include <linux/slab.h>
#include "internal.h"

static const char *afs_voltypes[] = { "R/W", "R/O", "BAK" };

/*
 * lookup a volume by name
 * - this can be one of the following:
 *	"%[cell:]volume[.]"		R/W volume
 *	"#[cell:]volume[.]"		R/O or R/W volume (rwparent=0),
 *					 or R/W (rwparent=1) volume
 *	"%[cell:]volume.readonly"	R/O volume
 *	"#[cell:]volume.readonly"	R/O volume
 *	"%[cell:]volume.backup"		Backup volume
 *	"#[cell:]volume.backup"		Backup volume
 *
 * The cell name is optional, and defaults to the current cell.
 *
 * See "The Rules of Mount Point Traversal" in Chapter 5 of the AFS SysAdmin
 * Guide
 * - Rule 1: Explicit type suffix forces access of that type or nothing
 *           (no suffix, then use Rule 2 & 3)
 * - Rule 2: If parent volume is R/O, then mount R/O volume by preference, R/W
 *           if not available
 * - Rule 3: If parent volume is R/W, then only mount R/W volume unless
 *           explicitly told otherwise
 */
D
David Howells 已提交
40
struct afs_volume *afs_volume_lookup(struct afs_mount_params *params)
L
Linus Torvalds 已提交
41 42 43
{
	struct afs_vlocation *vlocation = NULL;
	struct afs_volume *volume = NULL;
44
	struct afs_server *server = NULL;
L
Linus Torvalds 已提交
45
	char srvtmask;
D
David Howells 已提交
46
	int ret, loop;
L
Linus Torvalds 已提交
47

D
David Howells 已提交
48 49
	_enter("{%*.*s,%d}",
	       params->volnamesz, params->volnamesz, params->volname, params->rwpath);
L
Linus Torvalds 已提交
50 51

	/* lookup the volume location record */
52
	vlocation = afs_vlocation_lookup(params->net, params->cell, params->key,
D
David Howells 已提交
53
					 params->volname, params->volnamesz);
54 55 56
	if (IS_ERR(vlocation)) {
		ret = PTR_ERR(vlocation);
		vlocation = NULL;
L
Linus Torvalds 已提交
57
		goto error;
58
	}
L
Linus Torvalds 已提交
59 60 61

	/* make the final decision on the type we want */
	ret = -ENOMEDIUM;
D
David Howells 已提交
62
	if (params->force && !(vlocation->vldb.vidmask & (1 << params->type)))
L
Linus Torvalds 已提交
63 64 65 66 67 68
		goto error;

	srvtmask = 0;
	for (loop = 0; loop < vlocation->vldb.nservers; loop++)
		srvtmask |= vlocation->vldb.srvtmask[loop];

D
David Howells 已提交
69 70
	if (params->force) {
		if (!(srvtmask & (1 << params->type)))
L
Linus Torvalds 已提交
71
			goto error;
D
David Howells 已提交
72
	} else if (srvtmask & AFS_VOL_VTM_RO) {
D
David Howells 已提交
73
		params->type = AFSVL_ROVOL;
D
David Howells 已提交
74
	} else if (srvtmask & AFS_VOL_VTM_RW) {
D
David Howells 已提交
75
		params->type = AFSVL_RWVOL;
D
David Howells 已提交
76
	} else {
L
Linus Torvalds 已提交
77 78 79
		goto error;
	}

D
David Howells 已提交
80
	down_write(&params->cell->vl_sem);
L
Linus Torvalds 已提交
81 82

	/* is the volume already active? */
D
David Howells 已提交
83
	if (vlocation->vols[params->type]) {
L
Linus Torvalds 已提交
84
		/* yes - re-use it */
D
David Howells 已提交
85
		volume = vlocation->vols[params->type];
L
Linus Torvalds 已提交
86 87 88 89 90 91 92 93
		afs_get_volume(volume);
		goto success;
	}

	/* create a new volume record */
	_debug("creating new volume record");

	ret = -ENOMEM;
94
	volume = kzalloc(sizeof(struct afs_volume), GFP_KERNEL);
L
Linus Torvalds 已提交
95 96 97 98
	if (!volume)
		goto error_up;

	atomic_set(&volume->usage, 1);
D
David Howells 已提交
99 100 101 102
	volume->type		= params->type;
	volume->type_force	= params->force;
	volume->cell		= params->cell;
	volume->vid		= vlocation->vldb.vid[params->type];
L
Linus Torvalds 已提交
103 104 105 106 107 108

	init_rwsem(&volume->server_sem);

	/* look up all the applicable server records */
	for (loop = 0; loop < 8; loop++) {
		if (vlocation->vldb.srvtmask[loop] & (1 << volume->type)) {
109 110 111 112
			server = afs_lookup_server(
			       volume->cell, &vlocation->vldb.servers[loop]);
			if (IS_ERR(server)) {
				ret = PTR_ERR(server);
L
Linus Torvalds 已提交
113
				goto error_discard;
114
			}
L
Linus Torvalds 已提交
115

116
			volume->servers[volume->nservers] = server;
L
Linus Torvalds 已提交
117 118 119 120 121
			volume->nservers++;
		}
	}

	/* attach the cache and volume location */
D
David Howells 已提交
122
#ifdef CONFIG_AFS_FSCACHE
123
	volume->cache = fscache_acquire_cookie(volume->cell->cache,
D
David Howells 已提交
124
					       &afs_volume_cache_index_def,
125
					       volume, true);
L
Linus Torvalds 已提交
126 127 128 129
#endif
	afs_get_vlocation(vlocation);
	volume->vlocation = vlocation;

D
David Howells 已提交
130
	vlocation->vols[volume->type] = volume;
L
Linus Torvalds 已提交
131

D
David Howells 已提交
132
success:
L
Linus Torvalds 已提交
133 134
	_debug("kAFS selected %s volume %08x",
	       afs_voltypes[volume->type], volume->vid);
D
David Howells 已提交
135
	up_write(&params->cell->vl_sem);
136
	afs_put_vlocation(params->net, vlocation);
137 138
	_leave(" = %p", volume);
	return volume;
L
Linus Torvalds 已提交
139 140

	/* clean up */
D
David Howells 已提交
141
error_up:
D
David Howells 已提交
142
	up_write(&params->cell->vl_sem);
D
David Howells 已提交
143
error:
144
	afs_put_vlocation(params->net, vlocation);
145 146
	_leave(" = %d", ret);
	return ERR_PTR(ret);
L
Linus Torvalds 已提交
147

D
David Howells 已提交
148
error_discard:
D
David Howells 已提交
149
	up_write(&params->cell->vl_sem);
L
Linus Torvalds 已提交
150

151 152
	for (loop = volume->nservers - 1; loop >= 0; loop--) {
		afs_put_cb_interest(params->net, volume->cb_interests[loop]);
153
		afs_put_server(params->net, volume->servers[loop]);
154
	}
L
Linus Torvalds 已提交
155 156 157

	kfree(volume);
	goto error;
D
David Howells 已提交
158
}
L
Linus Torvalds 已提交
159 160 161 162

/*
 * destroy a volume record
 */
163
void afs_put_volume(struct afs_cell *cell, struct afs_volume *volume)
L
Linus Torvalds 已提交
164 165 166 167 168 169 170 171 172
{
	struct afs_vlocation *vlocation;
	int loop;

	if (!volume)
		return;

	_enter("%p", volume);

173
	ASSERTCMP(atomic_read(&volume->usage), >, 0);
L
Linus Torvalds 已提交
174

175
	vlocation = volume->vlocation;
L
Linus Torvalds 已提交
176 177 178

	/* to prevent a race, the decrement and the dequeue must be effectively
	 * atomic */
179
	down_write(&cell->vl_sem);
L
Linus Torvalds 已提交
180 181 182 183 184 185 186 187 188

	if (likely(!atomic_dec_and_test(&volume->usage))) {
		up_write(&vlocation->cell->vl_sem);
		_leave("");
		return;
	}

	vlocation->vols[volume->type] = NULL;

189
	up_write(&cell->vl_sem);
L
Linus Torvalds 已提交
190 191

	/* finish cleaning up the volume */
D
David Howells 已提交
192 193
#ifdef CONFIG_AFS_FSCACHE
	fscache_relinquish_cookie(volume->cache, 0);
L
Linus Torvalds 已提交
194
#endif
195
	afs_put_vlocation(cell->net, vlocation);
L
Linus Torvalds 已提交
196

197 198
	for (loop = volume->nservers - 1; loop >= 0; loop--) {
		afs_put_cb_interest(cell->net, volume->cb_interests[loop]);
199
		afs_put_server(cell->net, volume->servers[loop]);
200
	}
L
Linus Torvalds 已提交
201 202 203 204

	kfree(volume);

	_leave(" [destroyed]");
D
David Howells 已提交
205
}