Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / fs / afs / volume.c
1 /* volume.c: AFS volume management
2  *
3  * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
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>
18 #include <linux/fscache.h>
19 #include "volume.h"
20 #include "vnode.h"
21 #include "cell.h"
22 #include "cmservice.h"
23 #include "fsclient.h"
24 #include "vlclient.h"
25 #include "internal.h"
26
27 #ifdef __KDEBUG
28 static const char *afs_voltypes[] = { "R/W", "R/O", "BAK" };
29 #endif
30
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);
34
35 static struct fscache_cookie_def afs_volume_cache_index_def = {
36         .name           = "AFS.volume",
37         .type           = FSCACHE_COOKIE_TYPE_INDEX,
38         .get_key        = afs_volume_cache_get_key,
39 };
40 #endif
41
42 /*****************************************************************************/
43 /*
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
53  *
54  * The cell name is optional, and defaults to the current cell.
55  *
56  * See "The Rules of Mount Point Traversal" in Chapter 5 of the AFS SysAdmin
57  * Guide
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
61  *           if not available
62  * - Rule 3: If parent volume is R/W, then only mount R/W volume unless
63  *           explicitly told otherwise
64  */
65 int afs_volume_lookup(const char *name, struct afs_cell *cell, int rwpath,
66                       struct afs_volume **_volume)
67 {
68         struct afs_vlocation *vlocation = NULL;
69         struct afs_volume *volume = NULL;
70         afs_voltype_t type;
71         const char *cellname, *volname, *suffix;
72         char srvtmask;
73         int force, ret, loop, cellnamesz, volnamesz;
74
75         _enter("%s,,%d,", name, rwpath);
76
77         if (!name || (name[0] != '%' && name[0] != '#') || !name[1]) {
78                 printk("kAFS: unparsable volume name\n");
79                 return -EINVAL;
80         }
81
82         /* determine the type of volume we're looking for */
83         force = 0;
84         type = AFSVL_ROVOL;
85
86         if (rwpath || name[0] == '%') {
87                 type = AFSVL_RWVOL;
88                 force = 1;
89         }
90
91         suffix = strrchr(name, '.');
92         if (suffix) {
93                 if (strcmp(suffix, ".readonly") == 0) {
94                         type = AFSVL_ROVOL;
95                         force = 1;
96                 }
97                 else if (strcmp(suffix, ".backup") == 0) {
98                         type = AFSVL_BACKVOL;
99                         force = 1;
100                 }
101                 else if (suffix[1] == 0) {
102                 }
103                 else {
104                         suffix = NULL;
105                 }
106         }
107
108         /* split the cell and volume names */
109         name++;
110         volname = strchr(name, ':');
111         if (volname) {
112                 cellname = name;
113                 cellnamesz = volname - name;
114                 volname++;
115         }
116         else {
117                 volname = name;
118                 cellname = NULL;
119                 cellnamesz = 0;
120         }
121
122         volnamesz = suffix ? suffix - volname : strlen(volname);
123
124         _debug("CELL:%*.*s [%p] VOLUME:%*.*s SUFFIX:%s TYPE:%d%s",
125                cellnamesz, cellnamesz, cellname ?: "", cell,
126                volnamesz, volnamesz, volname, suffix ?: "-",
127                type,
128                force ? " FORCE" : "");
129
130         /* lookup the cell record */
131         if (cellname || !cell) {
132                 ret = afs_cell_lookup(cellname, cellnamesz, &cell);
133                 if (ret<0) {
134                         printk("kAFS: unable to lookup cell '%s'\n",
135                                cellname ?: "");
136                         goto error;
137                 }
138         }
139         else {
140                 afs_get_cell(cell);
141         }
142
143         /* lookup the volume location record */
144         ret = afs_vlocation_lookup(cell, volname, volnamesz, &vlocation);
145         if (ret < 0)
146                 goto error;
147
148         /* make the final decision on the type we want */
149         ret = -ENOMEDIUM;
150         if (force && !(vlocation->vldb.vidmask & (1 << type)))
151                 goto error;
152
153         srvtmask = 0;
154         for (loop = 0; loop < vlocation->vldb.nservers; loop++)
155                 srvtmask |= vlocation->vldb.srvtmask[loop];
156
157         if (force) {
158                 if (!(srvtmask & (1 << type)))
159                         goto error;
160         }
161         else if (srvtmask & AFS_VOL_VTM_RO) {
162                 type = AFSVL_ROVOL;
163         }
164         else if (srvtmask & AFS_VOL_VTM_RW) {
165                 type = AFSVL_RWVOL;
166         }
167         else {
168                 goto error;
169         }
170
171         down_write(&cell->vl_sem);
172
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);
178                 goto success;
179         }
180
181         /* create a new volume record */
182         _debug("creating new volume record");
183
184         ret = -ENOMEM;
185         volume = kmalloc(sizeof(struct afs_volume), GFP_KERNEL);
186         if (!volume)
187                 goto error_up;
188
189         memset(volume, 0, sizeof(struct afs_volume));
190         atomic_set(&volume->usage, 1);
191         volume->type            = type;
192         volume->type_force      = force;
193         volume->cell            = cell;
194         volume->vid             = vlocation->vldb.vid[type];
195
196         init_rwsem(&volume->server_sem);
197
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(
202                                 volume->cell,
203                                 &vlocation->vldb.servers[loop],
204                                 &volume->servers[volume->nservers]);
205                         if (ret < 0)
206                                 goto error_discard;
207
208                         volume->nservers++;
209                 }
210         }
211
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,
216                                                volume);
217 #endif
218
219         afs_get_vlocation(vlocation);
220         volume->vlocation = vlocation;
221
222         vlocation->vols[type] = volume;
223
224  success:
225         _debug("kAFS selected %s volume %08x",
226                afs_voltypes[volume->type], volume->vid);
227         *_volume = volume;
228         ret = 0;
229
230         /* clean up */
231  error_up:
232         up_write(&cell->vl_sem);
233  error:
234         afs_put_vlocation(vlocation);
235         afs_put_cell(cell);
236
237         _leave(" = %d (%p)", ret, volume);
238         return ret;
239
240  error_discard:
241         up_write(&cell->vl_sem);
242
243         for (loop = volume->nservers - 1; loop >= 0; loop--)
244                 afs_put_server(volume->servers[loop]);
245
246         kfree(volume);
247         goto error;
248 } /* end afs_volume_lookup() */
249
250 /*****************************************************************************/
251 /*
252  * destroy a volume record
253  */
254 void afs_put_volume(struct afs_volume *volume)
255 {
256         struct afs_vlocation *vlocation;
257         int loop;
258
259         if (!volume)
260                 return;
261
262         _enter("%p", volume);
263
264         vlocation = volume->vlocation;
265
266         /* sanity check */
267         BUG_ON(atomic_read(&volume->usage) <= 0);
268
269         /* to prevent a race, the decrement and the dequeue must be effectively
270          * atomic */
271         down_write(&vlocation->cell->vl_sem);
272
273         if (likely(!atomic_dec_and_test(&volume->usage))) {
274                 up_write(&vlocation->cell->vl_sem);
275                 _leave("");
276                 return;
277         }
278
279         vlocation->vols[volume->type] = NULL;
280
281         up_write(&vlocation->cell->vl_sem);
282
283         /* finish cleaning up the volume */
284 #ifdef CONFIG_AFS_FSCACHE
285         fscache_relinquish_cookie(volume->cache, 0);
286 #endif
287         afs_put_vlocation(vlocation);
288
289         for (loop = volume->nservers - 1; loop >= 0; loop--)
290                 afs_put_server(volume->servers[loop]);
291
292         kfree(volume);
293
294         _leave(" [destroyed]");
295 } /* end afs_put_volume() */
296
297 /*****************************************************************************/
298 /*
299  * pick a server to use to try accessing this volume
300  * - returns with an elevated usage count on the server chosen
301  */
302 int afs_volume_pick_fileserver(struct afs_volume *volume,
303                                struct afs_server **_server)
304 {
305         struct afs_server *server;
306         int ret, state, loop;
307
308         _enter("%s", volume->vlocation->vldb.name);
309
310         down_read(&volume->server_sem);
311
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);
317                 return ret;
318         }
319
320         /* basically, just search the list for the first live server and use
321          * that */
322         ret = 0;
323         for (loop = 0; loop < volume->nservers; loop++) {
324                 server = volume->servers[loop];
325                 state = server->fs_state;
326
327                 switch (state) {
328                         /* found an apparently healthy server */
329                 case 0:
330                         afs_get_server(server);
331                         up_read(&volume->server_sem);
332                         *_server = server;
333                         _leave(" = 0 (picked %08x)",
334                                ntohl(server->addr.s_addr));
335                         return 0;
336
337                 case -ENETUNREACH:
338                         if (ret == 0)
339                                 ret = state;
340                         break;
341
342                 case -EHOSTUNREACH:
343                         if (ret == 0 ||
344                             ret == -ENETUNREACH)
345                                 ret = state;
346                         break;
347
348                 case -ECONNREFUSED:
349                         if (ret == 0 ||
350                             ret == -ENETUNREACH ||
351                             ret == -EHOSTUNREACH)
352                                 ret = state;
353                         break;
354
355                 default:
356                 case -EREMOTEIO:
357                         if (ret == 0 ||
358                             ret == -ENETUNREACH ||
359                             ret == -EHOSTUNREACH ||
360                             ret == -ECONNREFUSED)
361                                 ret = state;
362                         break;
363                 }
364         }
365
366         /* no available servers
367          * - TODO: handle the no active servers case better
368          */
369         up_read(&volume->server_sem);
370         _leave(" = %d", ret);
371         return ret;
372 } /* end afs_volume_pick_fileserver() */
373
374 /*****************************************************************************/
375 /*
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
380  */
381 int afs_volume_release_fileserver(struct afs_volume *volume,
382                                   struct afs_server *server,
383                                   int result)
384 {
385         unsigned loop;
386
387         _enter("%s,%08x,%d",
388                volume->vlocation->vldb.name, ntohl(server->addr.s_addr),
389                result);
390
391         switch (result) {
392                 /* success */
393         case 0:
394                 server->fs_act_jif = jiffies;
395                 break;
396
397                 /* the fileserver denied all knowledge of the volume */
398         case -ENOMEDIUM:
399                 server->fs_act_jif = jiffies;
400                 down_write(&volume->server_sem);
401
402                 /* first, find where the server is in the active list (if it
403                  * is) */
404                 for (loop = 0; loop < volume->nservers; loop++)
405                         if (volume->servers[loop] == server)
406                                 goto present;
407
408                 /* no longer there - may have been discarded by another op */
409                 goto try_next_server_upw;
410
411         present:
412                 volume->nservers--;
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);
419                 volume->rjservers++;
420
421                 if (volume->nservers > 0)
422                         /* another server might acknowledge its existence */
423                         goto try_next_server_upw;
424
425                 /* handle the case where all the fileservers have rejected the
426                  * volume
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
430                  */
431                 up_write(&volume->server_sem);
432                 afs_put_server(server);
433                 _leave(" [completely rejected]");
434                 return 1;
435
436                 /* problem reaching the server */
437         case -ENETUNREACH:
438         case -EHOSTUNREACH:
439         case -ECONNREFUSED:
440         case -ETIMEDOUT:
441         case -EREMOTEIO:
442                 /* mark the server as dead
443                  * TODO: vary dead timeout depending on error
444                  */
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);
450                 }
451                 spin_unlock(&server->fs_lock);
452                 goto try_next_server;
453
454                 /* miscellaneous error */
455         default:
456                 server->fs_act_jif = jiffies;
457         case -ENOMEM:
458         case -ENONET:
459                 break;
460         }
461
462         /* tell the caller to accept the result */
463         afs_put_server(server);
464         _leave("");
465         return 1;
466
467         /* tell the caller to loop around and try the next server */
468  try_next_server_upw:
469         up_write(&volume->server_sem);
470  try_next_server:
471         afs_put_server(server);
472         _leave(" [try next server]");
473         return 0;
474
475 } /* end afs_volume_release_fileserver() */
476
477 /*****************************************************************************/
478 /*
479  * set the key for the index entry
480  */
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)
484 {
485         const struct afs_volume *volume = cookie_netfs_data;
486         uint16_t klen;
487
488         _enter("{%u},%p,%u", volume->type, buffer, bufmax);
489
490         klen = sizeof(volume->type);
491         if (klen > bufmax)
492                 return 0;
493
494         memcpy(buffer, &volume->type, sizeof(volume->type));
495
496         _leave(" = %u", klen);
497         return klen;
498
499 } /* end afs_volume_cache_get_key() */
500 #endif