]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/afs/volume.c
Fix "fs: convert core functions to zero_user_page"
[net-next-2.6.git] / fs / afs / volume.c
CommitLineData
ec26815a 1/* AFS volume management
1da177e4 2 *
08e0e7c8 3 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
1da177e4
LT
4 * Written by David Howells (dhowells@redhat.com)
5 *
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.
10 */
11
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/slab.h>
16#include <linux/fs.h>
17#include <linux/pagemap.h>
1da177e4
LT
18#include "internal.h"
19
1da177e4 20static const char *afs_voltypes[] = { "R/W", "R/O", "BAK" };
1da177e4 21
1da177e4
LT
22/*
23 * lookup a volume by name
24 * - this can be one of the following:
25 * "%[cell:]volume[.]" R/W volume
26 * "#[cell:]volume[.]" R/O or R/W volume (rwparent=0),
27 * or R/W (rwparent=1) volume
28 * "%[cell:]volume.readonly" R/O volume
29 * "#[cell:]volume.readonly" R/O volume
30 * "%[cell:]volume.backup" Backup volume
31 * "#[cell:]volume.backup" Backup volume
32 *
33 * The cell name is optional, and defaults to the current cell.
34 *
35 * See "The Rules of Mount Point Traversal" in Chapter 5 of the AFS SysAdmin
36 * Guide
37 * - Rule 1: Explicit type suffix forces access of that type or nothing
38 * (no suffix, then use Rule 2 & 3)
39 * - Rule 2: If parent volume is R/O, then mount R/O volume by preference, R/W
40 * if not available
41 * - Rule 3: If parent volume is R/W, then only mount R/W volume unless
42 * explicitly told otherwise
43 */
00d3b7a4 44struct afs_volume *afs_volume_lookup(struct afs_mount_params *params)
1da177e4
LT
45{
46 struct afs_vlocation *vlocation = NULL;
47 struct afs_volume *volume = NULL;
08e0e7c8 48 struct afs_server *server = NULL;
1da177e4 49 char srvtmask;
00d3b7a4 50 int ret, loop;
1da177e4 51
00d3b7a4
DH
52 _enter("{%*.*s,%d}",
53 params->volnamesz, params->volnamesz, params->volname, params->rwpath);
1da177e4
LT
54
55 /* lookup the volume location record */
00d3b7a4
DH
56 vlocation = afs_vlocation_lookup(params->cell, params->key,
57 params->volname, params->volnamesz);
08e0e7c8
DH
58 if (IS_ERR(vlocation)) {
59 ret = PTR_ERR(vlocation);
60 vlocation = NULL;
1da177e4 61 goto error;
08e0e7c8 62 }
1da177e4
LT
63
64 /* make the final decision on the type we want */
65 ret = -ENOMEDIUM;
00d3b7a4 66 if (params->force && !(vlocation->vldb.vidmask & (1 << params->type)))
1da177e4
LT
67 goto error;
68
69 srvtmask = 0;
70 for (loop = 0; loop < vlocation->vldb.nservers; loop++)
71 srvtmask |= vlocation->vldb.srvtmask[loop];
72
00d3b7a4
DH
73 if (params->force) {
74 if (!(srvtmask & (1 << params->type)))
1da177e4 75 goto error;
ec26815a 76 } else if (srvtmask & AFS_VOL_VTM_RO) {
00d3b7a4 77 params->type = AFSVL_ROVOL;
ec26815a 78 } else if (srvtmask & AFS_VOL_VTM_RW) {
00d3b7a4 79 params->type = AFSVL_RWVOL;
ec26815a 80 } else {
1da177e4
LT
81 goto error;
82 }
83
00d3b7a4 84 down_write(&params->cell->vl_sem);
1da177e4
LT
85
86 /* is the volume already active? */
00d3b7a4 87 if (vlocation->vols[params->type]) {
1da177e4 88 /* yes - re-use it */
00d3b7a4 89 volume = vlocation->vols[params->type];
1da177e4
LT
90 afs_get_volume(volume);
91 goto success;
92 }
93
94 /* create a new volume record */
95 _debug("creating new volume record");
96
97 ret = -ENOMEM;
f8314dc6 98 volume = kzalloc(sizeof(struct afs_volume), GFP_KERNEL);
1da177e4
LT
99 if (!volume)
100 goto error_up;
101
1da177e4 102 atomic_set(&volume->usage, 1);
00d3b7a4
DH
103 volume->type = params->type;
104 volume->type_force = params->force;
105 volume->cell = params->cell;
106 volume->vid = vlocation->vldb.vid[params->type];
1da177e4
LT
107
108 init_rwsem(&volume->server_sem);
109
110 /* look up all the applicable server records */
111 for (loop = 0; loop < 8; loop++) {
112 if (vlocation->vldb.srvtmask[loop] & (1 << volume->type)) {
08e0e7c8
DH
113 server = afs_lookup_server(
114 volume->cell, &vlocation->vldb.servers[loop]);
115 if (IS_ERR(server)) {
116 ret = PTR_ERR(server);
1da177e4 117 goto error_discard;
08e0e7c8 118 }
1da177e4 119
08e0e7c8 120 volume->servers[volume->nservers] = server;
1da177e4
LT
121 volume->nservers++;
122 }
123 }
124
125 /* attach the cache and volume location */
126#ifdef AFS_CACHING_SUPPORT
127 cachefs_acquire_cookie(vlocation->cache,
128 &afs_vnode_cache_index_def,
129 volume,
130 &volume->cache);
131#endif
132
133 afs_get_vlocation(vlocation);
134 volume->vlocation = vlocation;
135
00d3b7a4 136 vlocation->vols[volume->type] = volume;
1da177e4 137
ec26815a 138success:
1da177e4
LT
139 _debug("kAFS selected %s volume %08x",
140 afs_voltypes[volume->type], volume->vid);
00d3b7a4 141 up_write(&params->cell->vl_sem);
08e0e7c8 142 afs_put_vlocation(vlocation);
08e0e7c8
DH
143 _leave(" = %p", volume);
144 return volume;
1da177e4
LT
145
146 /* clean up */
ec26815a 147error_up:
00d3b7a4 148 up_write(&params->cell->vl_sem);
ec26815a 149error:
1da177e4 150 afs_put_vlocation(vlocation);
08e0e7c8
DH
151 _leave(" = %d", ret);
152 return ERR_PTR(ret);
1da177e4 153
ec26815a 154error_discard:
00d3b7a4 155 up_write(&params->cell->vl_sem);
1da177e4
LT
156
157 for (loop = volume->nservers - 1; loop >= 0; loop--)
158 afs_put_server(volume->servers[loop]);
159
160 kfree(volume);
161 goto error;
ec26815a 162}
1da177e4 163
1da177e4
LT
164/*
165 * destroy a volume record
166 */
167void afs_put_volume(struct afs_volume *volume)
168{
169 struct afs_vlocation *vlocation;
170 int loop;
171
172 if (!volume)
173 return;
174
175 _enter("%p", volume);
176
08e0e7c8 177 ASSERTCMP(atomic_read(&volume->usage), >, 0);
1da177e4 178
08e0e7c8 179 vlocation = volume->vlocation;
1da177e4
LT
180
181 /* to prevent a race, the decrement and the dequeue must be effectively
182 * atomic */
183 down_write(&vlocation->cell->vl_sem);
184
185 if (likely(!atomic_dec_and_test(&volume->usage))) {
186 up_write(&vlocation->cell->vl_sem);
187 _leave("");
188 return;
189 }
190
191 vlocation->vols[volume->type] = NULL;
192
193 up_write(&vlocation->cell->vl_sem);
194
195 /* finish cleaning up the volume */
196#ifdef AFS_CACHING_SUPPORT
197 cachefs_relinquish_cookie(volume->cache, 0);
198#endif
199 afs_put_vlocation(vlocation);
200
201 for (loop = volume->nservers - 1; loop >= 0; loop--)
202 afs_put_server(volume->servers[loop]);
203
204 kfree(volume);
205
206 _leave(" [destroyed]");
ec26815a 207}
1da177e4 208
1da177e4
LT
209/*
210 * pick a server to use to try accessing this volume
211 * - returns with an elevated usage count on the server chosen
212 */
08e0e7c8 213struct afs_server *afs_volume_pick_fileserver(struct afs_vnode *vnode)
1da177e4 214{
08e0e7c8 215 struct afs_volume *volume = vnode->volume;
1da177e4
LT
216 struct afs_server *server;
217 int ret, state, loop;
218
219 _enter("%s", volume->vlocation->vldb.name);
220
08e0e7c8
DH
221 /* stick with the server we're already using if we can */
222 if (vnode->server && vnode->server->fs_state == 0) {
223 afs_get_server(vnode->server);
224 _leave(" = %p [current]", vnode->server);
225 return vnode->server;
226 }
227
1da177e4
LT
228 down_read(&volume->server_sem);
229
230 /* handle the no-server case */
231 if (volume->nservers == 0) {
232 ret = volume->rjservers ? -ENOMEDIUM : -ESTALE;
233 up_read(&volume->server_sem);
234 _leave(" = %d [no servers]", ret);
08e0e7c8 235 return ERR_PTR(ret);
1da177e4
LT
236 }
237
238 /* basically, just search the list for the first live server and use
239 * that */
240 ret = 0;
241 for (loop = 0; loop < volume->nservers; loop++) {
242 server = volume->servers[loop];
243 state = server->fs_state;
244
08e0e7c8
DH
245 _debug("consider %d [%d]", loop, state);
246
1da177e4
LT
247 switch (state) {
248 /* found an apparently healthy server */
249 case 0:
250 afs_get_server(server);
251 up_read(&volume->server_sem);
08e0e7c8
DH
252 _leave(" = %p (picked %08x)",
253 server, ntohl(server->addr.s_addr));
254 return server;
1da177e4
LT
255
256 case -ENETUNREACH:
257 if (ret == 0)
258 ret = state;
259 break;
260
261 case -EHOSTUNREACH:
262 if (ret == 0 ||
263 ret == -ENETUNREACH)
264 ret = state;
265 break;
266
267 case -ECONNREFUSED:
268 if (ret == 0 ||
269 ret == -ENETUNREACH ||
270 ret == -EHOSTUNREACH)
271 ret = state;
272 break;
273
274 default:
275 case -EREMOTEIO:
276 if (ret == 0 ||
277 ret == -ENETUNREACH ||
278 ret == -EHOSTUNREACH ||
279 ret == -ECONNREFUSED)
280 ret = state;
281 break;
282 }
283 }
284
285 /* no available servers
286 * - TODO: handle the no active servers case better
287 */
288 up_read(&volume->server_sem);
289 _leave(" = %d", ret);
08e0e7c8 290 return ERR_PTR(ret);
ec26815a 291}
1da177e4 292
1da177e4
LT
293/*
294 * release a server after use
295 * - releases the ref on the server struct that was acquired by picking
296 * - records result of using a particular server to access a volume
297 * - return 0 to try again, 1 if okay or to issue error
260a9803 298 * - the caller must release the server struct if result was 0
1da177e4 299 */
08e0e7c8 300int afs_volume_release_fileserver(struct afs_vnode *vnode,
1da177e4
LT
301 struct afs_server *server,
302 int result)
303{
08e0e7c8 304 struct afs_volume *volume = vnode->volume;
1da177e4
LT
305 unsigned loop;
306
307 _enter("%s,%08x,%d",
308 volume->vlocation->vldb.name, ntohl(server->addr.s_addr),
309 result);
310
311 switch (result) {
312 /* success */
313 case 0:
314 server->fs_act_jif = jiffies;
08e0e7c8 315 server->fs_state = 0;
260a9803
DH
316 _leave("");
317 return 1;
1da177e4
LT
318
319 /* the fileserver denied all knowledge of the volume */
320 case -ENOMEDIUM:
321 server->fs_act_jif = jiffies;
322 down_write(&volume->server_sem);
323
08e0e7c8 324 /* firstly, find where the server is in the active list (if it
1da177e4
LT
325 * is) */
326 for (loop = 0; loop < volume->nservers; loop++)
327 if (volume->servers[loop] == server)
328 goto present;
329
330 /* no longer there - may have been discarded by another op */
331 goto try_next_server_upw;
332
333 present:
334 volume->nservers--;
335 memmove(&volume->servers[loop],
336 &volume->servers[loop + 1],
337 sizeof(volume->servers[loop]) *
338 (volume->nservers - loop));
339 volume->servers[volume->nservers] = NULL;
340 afs_put_server(server);
341 volume->rjservers++;
342
343 if (volume->nservers > 0)
344 /* another server might acknowledge its existence */
345 goto try_next_server_upw;
346
347 /* handle the case where all the fileservers have rejected the
348 * volume
349 * - TODO: try asking the fileservers for volume information
350 * - TODO: contact the VL server again to see if the volume is
351 * no longer registered
352 */
353 up_write(&volume->server_sem);
354 afs_put_server(server);
355 _leave(" [completely rejected]");
356 return 1;
357
358 /* problem reaching the server */
359 case -ENETUNREACH:
360 case -EHOSTUNREACH:
361 case -ECONNREFUSED:
08e0e7c8 362 case -ETIME:
1da177e4
LT
363 case -ETIMEDOUT:
364 case -EREMOTEIO:
365 /* mark the server as dead
366 * TODO: vary dead timeout depending on error
367 */
368 spin_lock(&server->fs_lock);
369 if (!server->fs_state) {
370 server->fs_dead_jif = jiffies + HZ * 10;
371 server->fs_state = result;
372 printk("kAFS: SERVER DEAD state=%d\n", result);
373 }
374 spin_unlock(&server->fs_lock);
375 goto try_next_server;
376
377 /* miscellaneous error */
378 default:
379 server->fs_act_jif = jiffies;
380 case -ENOMEM:
381 case -ENONET:
260a9803
DH
382 /* tell the caller to accept the result */
383 afs_put_server(server);
384 _leave(" [local failure]");
385 return 1;
1da177e4
LT
386 }
387
1da177e4 388 /* tell the caller to loop around and try the next server */
ec26815a 389try_next_server_upw:
1da177e4 390 up_write(&volume->server_sem);
ec26815a 391try_next_server:
1da177e4
LT
392 afs_put_server(server);
393 _leave(" [try next server]");
394 return 0;
ec26815a 395}