blob: 41173f81ac47a7acf620f15ef45d2762c6750e40 [file] [log] [blame]
David Howellsec268152007-04-26 15:49:28 -07001/* AFS superblock handling
2 *
David Howells08e0e7c2007-04-26 15:55:03 -07003 * Copyright (c) 2002, 2007 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This software may be freely redistributed under the terms of the
6 * GNU General Public License.
7 *
8 * You should have received a copy of the GNU General Public License
9 * along with this program; if not, write to the Free Software
10 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
11 *
12 * Authors: David Howells <dhowells@redhat.com>
David Howellsec268152007-04-26 15:49:28 -070013 * David Woodhouse <dwmw2@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 */
16
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/slab.h>
21#include <linux/fs.h>
22#include <linux/pagemap.h>
David Howells80c72fe2007-05-03 03:11:29 -070023#include <linux/parser.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "internal.h"
25
26#define AFS_FS_MAGIC 0x6B414653 /* 'kAFS' */
27
Christoph Lametere18b8902006-12-06 20:33:20 -080028static void afs_i_init_once(void *foo, struct kmem_cache *cachep,
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 unsigned long flags);
30
David Howells454e2392006-06-23 02:02:57 -070031static int afs_get_sb(struct file_system_type *fs_type,
32 int flags, const char *dev_name,
33 void *data, struct vfsmount *mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35static struct inode *afs_alloc_inode(struct super_block *sb);
36
37static void afs_put_super(struct super_block *sb);
38
39static void afs_destroy_inode(struct inode *inode);
40
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -040041struct file_system_type afs_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 .owner = THIS_MODULE,
43 .name = "afs",
44 .get_sb = afs_get_sb,
45 .kill_sb = kill_anon_super,
David Howells80c72fe2007-05-03 03:11:29 -070046 .fs_flags = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070047};
48
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -080049static const struct super_operations afs_super_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 .statfs = simple_statfs,
51 .alloc_inode = afs_alloc_inode,
52 .drop_inode = generic_delete_inode,
53 .destroy_inode = afs_destroy_inode,
54 .clear_inode = afs_clear_inode,
David Howells08e0e7c2007-04-26 15:55:03 -070055 .umount_begin = afs_umount_begin,
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 .put_super = afs_put_super,
57};
58
Christoph Lametere18b8902006-12-06 20:33:20 -080059static struct kmem_cache *afs_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static atomic_t afs_count_active_inodes;
61
David Howells80c72fe2007-05-03 03:11:29 -070062enum {
63 afs_no_opt,
64 afs_opt_cell,
65 afs_opt_rwpath,
66 afs_opt_vol,
67};
68
69static const match_table_t afs_options_list = {
70 { afs_opt_cell, "cell=%s" },
71 { afs_opt_rwpath, "rwpath" },
72 { afs_opt_vol, "vol=%s" },
73 { afs_no_opt, NULL },
74};
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/*
77 * initialise the filesystem
78 */
79int __init afs_fs_init(void)
80{
81 int ret;
82
83 _enter("");
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 /* create ourselves an inode cache */
86 atomic_set(&afs_count_active_inodes, 0);
87
88 ret = -ENOMEM;
89 afs_inode_cachep = kmem_cache_create("afs_inode_cache",
90 sizeof(struct afs_vnode),
91 0,
92 SLAB_HWCACHE_ALIGN,
93 afs_i_init_once,
94 NULL);
95 if (!afs_inode_cachep) {
96 printk(KERN_NOTICE "kAFS: Failed to allocate inode cache\n");
97 return ret;
98 }
99
100 /* now export our filesystem to lesser mortals */
101 ret = register_filesystem(&afs_fs_type);
102 if (ret < 0) {
103 kmem_cache_destroy(afs_inode_cachep);
David Howells08e0e7c2007-04-26 15:55:03 -0700104 _leave(" = %d", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return ret;
106 }
107
David Howells08e0e7c2007-04-26 15:55:03 -0700108 _leave(" = 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return 0;
David Howellsec268152007-04-26 15:49:28 -0700110}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/*
113 * clean up the filesystem
114 */
115void __exit afs_fs_exit(void)
116{
David Howells08e0e7c2007-04-26 15:55:03 -0700117 _enter("");
118
119 afs_mntpt_kill_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 unregister_filesystem(&afs_fs_type);
121
122 if (atomic_read(&afs_count_active_inodes) != 0) {
123 printk("kAFS: %d active inode objects still present\n",
124 atomic_read(&afs_count_active_inodes));
125 BUG();
126 }
127
128 kmem_cache_destroy(afs_inode_cachep);
David Howells08e0e7c2007-04-26 15:55:03 -0700129 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700130}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 * parse the mount options
134 * - this function has been shamelessly adapted from the ext3 fs which
135 * shamelessly adapted it from the msdos fs
136 */
David Howells00d3b7a2007-04-26 15:57:07 -0700137static int afs_parse_options(struct afs_mount_params *params,
138 char *options, const char **devname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
David Howells08e0e7c2007-04-26 15:55:03 -0700140 struct afs_cell *cell;
David Howells80c72fe2007-05-03 03:11:29 -0700141 substring_t args[MAX_OPT_ARGS];
142 char *p;
143 int token;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 _enter("%s", options);
146
147 options[PAGE_SIZE - 1] = 0;
148
David Howells80c72fe2007-05-03 03:11:29 -0700149 while ((p = strsep(&options, ","))) {
150 if (!*p)
151 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
David Howells80c72fe2007-05-03 03:11:29 -0700153 token = match_token(p, afs_options_list, args);
154 switch (token) {
155 case afs_opt_cell:
156 cell = afs_cell_lookup(args[0].from,
157 args[0].to - args[0].from);
David Howells08e0e7c2007-04-26 15:55:03 -0700158 if (IS_ERR(cell))
159 return PTR_ERR(cell);
David Howells00d3b7a2007-04-26 15:57:07 -0700160 afs_put_cell(params->cell);
161 params->cell = cell;
David Howells80c72fe2007-05-03 03:11:29 -0700162 break;
163
164 case afs_opt_rwpath:
165 params->rwpath = 1;
166 break;
167
168 case afs_opt_vol:
169 *devname = args[0].from;
170 break;
171
172 default:
173 printk(KERN_ERR "kAFS:"
174 " Unknown or invalid mount option: '%s'\n", p);
175 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
178
David Howells80c72fe2007-05-03 03:11:29 -0700179 _leave(" = 0");
180 return 0;
David Howellsec268152007-04-26 15:49:28 -0700181}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/*
David Howells00d3b7a2007-04-26 15:57:07 -0700184 * parse a device name to get cell name, volume name, volume type and R/W
185 * selector
186 * - this can be one of the following:
187 * "%[cell:]volume[.]" R/W volume
188 * "#[cell:]volume[.]" R/O or R/W volume (rwpath=0),
189 * or R/W (rwpath=1) volume
190 * "%[cell:]volume.readonly" R/O volume
191 * "#[cell:]volume.readonly" R/O volume
192 * "%[cell:]volume.backup" Backup volume
193 * "#[cell:]volume.backup" Backup volume
194 */
195static int afs_parse_device_name(struct afs_mount_params *params,
196 const char *name)
197{
198 struct afs_cell *cell;
199 const char *cellname, *suffix;
200 int cellnamesz;
201
202 _enter(",%s", name);
203
204 if (!name) {
205 printk(KERN_ERR "kAFS: no volume name specified\n");
206 return -EINVAL;
207 }
208
209 if ((name[0] != '%' && name[0] != '#') || !name[1]) {
210 printk(KERN_ERR "kAFS: unparsable volume name\n");
211 return -EINVAL;
212 }
213
214 /* determine the type of volume we're looking for */
215 params->type = AFSVL_ROVOL;
216 params->force = false;
217 if (params->rwpath || name[0] == '%') {
218 params->type = AFSVL_RWVOL;
219 params->force = true;
220 }
221 name++;
222
223 /* split the cell name out if there is one */
224 params->volname = strchr(name, ':');
225 if (params->volname) {
226 cellname = name;
227 cellnamesz = params->volname - name;
228 params->volname++;
229 } else {
230 params->volname = name;
231 cellname = NULL;
232 cellnamesz = 0;
233 }
234
235 /* the volume type is further affected by a possible suffix */
236 suffix = strrchr(params->volname, '.');
237 if (suffix) {
238 if (strcmp(suffix, ".readonly") == 0) {
239 params->type = AFSVL_ROVOL;
240 params->force = true;
241 } else if (strcmp(suffix, ".backup") == 0) {
242 params->type = AFSVL_BACKVOL;
243 params->force = true;
244 } else if (suffix[1] == 0) {
245 } else {
246 suffix = NULL;
247 }
248 }
249
250 params->volnamesz = suffix ?
251 suffix - params->volname : strlen(params->volname);
252
253 _debug("cell %*.*s [%p]",
254 cellnamesz, cellnamesz, cellname ?: "", params->cell);
255
256 /* lookup the cell record */
257 if (cellname || !params->cell) {
258 cell = afs_cell_lookup(cellname, cellnamesz);
259 if (IS_ERR(cell)) {
260 printk(KERN_ERR "kAFS: unable to lookup cell '%s'\n",
261 cellname ?: "");
262 return PTR_ERR(cell);
263 }
264 afs_put_cell(params->cell);
265 params->cell = cell;
266 }
267
268 _debug("CELL:%s [%p] VOLUME:%*.*s SUFFIX:%s TYPE:%d%s",
269 params->cell->name, params->cell,
270 params->volnamesz, params->volnamesz, params->volname,
271 suffix ?: "-", params->type, params->force ? " FORCE" : "");
272
273 return 0;
274}
275
276/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 * check a superblock to see if it's the one we're looking for
278 */
279static int afs_test_super(struct super_block *sb, void *data)
280{
281 struct afs_mount_params *params = data;
282 struct afs_super_info *as = sb->s_fs_info;
283
284 return as->volume == params->volume;
David Howellsec268152007-04-26 15:49:28 -0700285}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287/*
288 * fill in the superblock
289 */
David Howells436058a2007-04-26 15:56:24 -0700290static int afs_fill_super(struct super_block *sb, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 struct afs_mount_params *params = data;
293 struct afs_super_info *as = NULL;
294 struct afs_fid fid;
295 struct dentry *root = NULL;
296 struct inode *inode = NULL;
297 int ret;
298
David Howells08e0e7c2007-04-26 15:55:03 -0700299 _enter("");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 /* allocate a superblock info record */
Yan Burmanb593e482006-12-06 20:40:32 -0800302 as = kzalloc(sizeof(struct afs_super_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (!as) {
304 _leave(" = -ENOMEM");
305 return -ENOMEM;
306 }
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 afs_get_volume(params->volume);
309 as->volume = params->volume;
310
311 /* fill in the superblock */
312 sb->s_blocksize = PAGE_CACHE_SIZE;
313 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
314 sb->s_magic = AFS_FS_MAGIC;
315 sb->s_op = &afs_super_ops;
316 sb->s_fs_info = as;
317
318 /* allocate the root inode and dentry */
319 fid.vid = as->volume->vid;
320 fid.vnode = 1;
321 fid.unique = 1;
David Howells260a9802007-04-26 15:59:35 -0700322 inode = afs_iget(sb, params->key, &fid, NULL, NULL);
David Howells08e0e7c2007-04-26 15:55:03 -0700323 if (IS_ERR(inode))
324 goto error_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 ret = -ENOMEM;
327 root = d_alloc_root(inode);
328 if (!root)
329 goto error;
330
331 sb->s_root = root;
332
David Howells08e0e7c2007-04-26 15:55:03 -0700333 _leave(" = 0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return 0;
335
David Howells08e0e7c2007-04-26 15:55:03 -0700336error_inode:
337 ret = PTR_ERR(inode);
338 inode = NULL;
David Howellsec268152007-04-26 15:49:28 -0700339error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 iput(inode);
341 afs_put_volume(as->volume);
342 kfree(as);
343
344 sb->s_fs_info = NULL;
345
David Howells08e0e7c2007-04-26 15:55:03 -0700346 _leave(" = %d", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return ret;
David Howellsec268152007-04-26 15:49:28 -0700348}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350/*
351 * get an AFS superblock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 */
David Howells454e2392006-06-23 02:02:57 -0700353static int afs_get_sb(struct file_system_type *fs_type,
354 int flags,
355 const char *dev_name,
356 void *options,
357 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 struct afs_mount_params params;
360 struct super_block *sb;
David Howells08e0e7c2007-04-26 15:55:03 -0700361 struct afs_volume *vol;
David Howells00d3b7a2007-04-26 15:57:07 -0700362 struct key *key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 int ret;
364
365 _enter(",,%s,%p", dev_name, options);
366
367 memset(&params, 0, sizeof(params));
368
David Howells00d3b7a2007-04-26 15:57:07 -0700369 /* parse the options and device name */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (options) {
David Howells00d3b7a2007-04-26 15:57:07 -0700371 ret = afs_parse_options(&params, options, &dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (ret < 0)
373 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375
David Howells00d3b7a2007-04-26 15:57:07 -0700376 ret = afs_parse_device_name(&params, dev_name);
377 if (ret < 0)
378 goto error;
379
380 /* try and do the mount securely */
381 key = afs_request_key(params.cell);
382 if (IS_ERR(key)) {
383 _leave(" = %ld [key]", PTR_ERR(key));
384 ret = PTR_ERR(key);
385 goto error;
386 }
387 params.key = key;
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 /* parse the device name */
David Howells00d3b7a2007-04-26 15:57:07 -0700390 vol = afs_volume_lookup(&params);
David Howells08e0e7c2007-04-26 15:55:03 -0700391 if (IS_ERR(vol)) {
392 ret = PTR_ERR(vol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 goto error;
David Howells08e0e7c2007-04-26 15:55:03 -0700394 }
David Howells08e0e7c2007-04-26 15:55:03 -0700395 params.volume = vol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 /* allocate a deviceless superblock */
398 sb = sget(fs_type, afs_test_super, set_anon_super, &params);
David Howells08e0e7c2007-04-26 15:55:03 -0700399 if (IS_ERR(sb)) {
400 ret = PTR_ERR(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 goto error;
David Howells08e0e7c2007-04-26 15:55:03 -0700402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
David Howells436058a2007-04-26 15:56:24 -0700404 if (!sb->s_root) {
405 /* initial superblock/root creation */
406 _debug("create");
407 sb->s_flags = flags;
408 ret = afs_fill_super(sb, &params);
409 if (ret < 0) {
410 up_write(&sb->s_umount);
411 deactivate_super(sb);
412 goto error;
413 }
414 sb->s_flags |= MS_ACTIVE;
415 } else {
416 _debug("reuse");
417 ASSERTCMP(sb->s_flags, &, MS_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
David Howells436058a2007-04-26 15:56:24 -0700420 simple_set_mnt(mnt, sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 afs_put_volume(params.volume);
David Howells00d3b7a2007-04-26 15:57:07 -0700422 afs_put_cell(params.cell);
David Howells08e0e7c2007-04-26 15:55:03 -0700423 _leave(" = 0 [%p]", sb);
David Howells454e2392006-06-23 02:02:57 -0700424 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
David Howellsec268152007-04-26 15:49:28 -0700426error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 afs_put_volume(params.volume);
David Howells00d3b7a2007-04-26 15:57:07 -0700428 afs_put_cell(params.cell);
429 key_put(params.key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 _leave(" = %d", ret);
David Howells454e2392006-06-23 02:02:57 -0700431 return ret;
David Howellsec268152007-04-26 15:49:28 -0700432}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434/*
435 * finish the unmounting process on the superblock
436 */
437static void afs_put_super(struct super_block *sb)
438{
439 struct afs_super_info *as = sb->s_fs_info;
440
441 _enter("");
442
443 afs_put_volume(as->volume);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700446}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448/*
449 * initialise an inode cache slab element prior to any use
450 */
Christoph Lametere18b8902006-12-06 20:33:20 -0800451static void afs_i_init_once(void *_vnode, struct kmem_cache *cachep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 unsigned long flags)
453{
David Howellsec268152007-04-26 15:49:28 -0700454 struct afs_vnode *vnode = _vnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
457 SLAB_CTOR_CONSTRUCTOR) {
458 memset(vnode, 0, sizeof(*vnode));
459 inode_init_once(&vnode->vfs_inode);
460 init_waitqueue_head(&vnode->update_waitq);
David Howells00d3b7a2007-04-26 15:57:07 -0700461 mutex_init(&vnode->permits_lock);
David Howells260a9802007-04-26 15:59:35 -0700462 mutex_init(&vnode->validate_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 spin_lock_init(&vnode->lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700464 INIT_WORK(&vnode->cb_broken_work, afs_broken_callback_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
David Howellsec268152007-04-26 15:49:28 -0700466}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468/*
469 * allocate an AFS inode struct from our slab cache
470 */
471static struct inode *afs_alloc_inode(struct super_block *sb)
472{
473 struct afs_vnode *vnode;
474
David Howellsec268152007-04-26 15:49:28 -0700475 vnode = kmem_cache_alloc(afs_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (!vnode)
477 return NULL;
478
479 atomic_inc(&afs_count_active_inodes);
480
481 memset(&vnode->fid, 0, sizeof(vnode->fid));
482 memset(&vnode->status, 0, sizeof(vnode->status));
483
484 vnode->volume = NULL;
485 vnode->update_cnt = 0;
David Howells260a9802007-04-26 15:59:35 -0700486 vnode->flags = 1 << AFS_VNODE_UNSET;
David Howells08e0e7c2007-04-26 15:55:03 -0700487 vnode->cb_promised = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 return &vnode->vfs_inode;
David Howellsec268152007-04-26 15:49:28 -0700490}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492/*
493 * destroy an AFS inode struct
494 */
495static void afs_destroy_inode(struct inode *inode)
496{
David Howells08e0e7c2007-04-26 15:55:03 -0700497 struct afs_vnode *vnode = AFS_FS_I(inode);
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 _enter("{%lu}", inode->i_ino);
500
David Howells08e0e7c2007-04-26 15:55:03 -0700501 _debug("DESTROY INODE %p", inode);
502
503 ASSERTCMP(vnode->server, ==, NULL);
504
505 kmem_cache_free(afs_inode_cachep, vnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 atomic_dec(&afs_count_active_inodes);
David Howellsec268152007-04-26 15:49:28 -0700507}