1 /* AFS superblock handling
3 * Copyright (c) 2002, 2007 Red Hat, Inc. All rights reserved.
5 * This software may be freely redistributed under the terms of the
6 * GNU General Public License.
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.
12 * Authors: David Howells <dhowells@redhat.com>
13 * David Woodhouse <dwmw2@redhat.com>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/slab.h>
22 #include <linux/pagemap.h>
25 #define AFS_FS_MAGIC 0x6B414653 /* 'kAFS' */
27 static void afs_i_init_once(void *foo, struct kmem_cache *cachep,
30 static int afs_get_sb(struct file_system_type *fs_type,
31 int flags, const char *dev_name,
32 void *data, struct vfsmount *mnt);
34 static struct inode *afs_alloc_inode(struct super_block *sb);
36 static void afs_put_super(struct super_block *sb);
38 static void afs_destroy_inode(struct inode *inode);
40 struct file_system_type afs_fs_type = {
44 .kill_sb = kill_anon_super,
45 .fs_flags = FS_BINARY_MOUNTDATA,
48 static const struct super_operations afs_super_ops = {
49 .statfs = simple_statfs,
50 .alloc_inode = afs_alloc_inode,
51 .drop_inode = generic_delete_inode,
52 .destroy_inode = afs_destroy_inode,
53 .clear_inode = afs_clear_inode,
54 .umount_begin = afs_umount_begin,
55 .put_super = afs_put_super,
58 static struct kmem_cache *afs_inode_cachep;
59 static atomic_t afs_count_active_inodes;
62 * initialise the filesystem
64 int __init afs_fs_init(void)
70 /* create ourselves an inode cache */
71 atomic_set(&afs_count_active_inodes, 0);
74 afs_inode_cachep = kmem_cache_create("afs_inode_cache",
75 sizeof(struct afs_vnode),
80 if (!afs_inode_cachep) {
81 printk(KERN_NOTICE "kAFS: Failed to allocate inode cache\n");
85 /* now export our filesystem to lesser mortals */
86 ret = register_filesystem(&afs_fs_type);
88 kmem_cache_destroy(afs_inode_cachep);
98 * clean up the filesystem
100 void __exit afs_fs_exit(void)
104 afs_mntpt_kill_timer();
105 unregister_filesystem(&afs_fs_type);
107 if (atomic_read(&afs_count_active_inodes) != 0) {
108 printk("kAFS: %d active inode objects still present\n",
109 atomic_read(&afs_count_active_inodes));
113 kmem_cache_destroy(afs_inode_cachep);
118 * check that an argument has a value
120 static int want_arg(char **_value, const char *option)
122 if (!_value || !*_value || !**_value) {
123 printk(KERN_NOTICE "kAFS: %s: argument missing\n", option);
130 * check that there's no subsequent value
132 static int want_no_value(char *const *_value, const char *option)
134 if (*_value && **_value) {
135 printk(KERN_NOTICE "kAFS: %s: Invalid argument: %s\n",
143 * parse the mount options
144 * - this function has been shamelessly adapted from the ext3 fs which
145 * shamelessly adapted it from the msdos fs
147 static int afs_parse_options(struct afs_mount_params *params,
148 char *options, const char **devname)
150 struct afs_cell *cell;
154 _enter("%s", options);
156 options[PAGE_SIZE - 1] = 0;
159 while ((key = strsep(&options, ","))) {
160 value = strchr(key, '=');
164 _debug("kAFS: KEY: %s, VAL:%s", key, value ?: "-");
166 if (strcmp(key, "rwpath") == 0) {
167 if (!want_no_value(&value, "rwpath"))
170 } else if (strcmp(key, "vol") == 0) {
171 if (!want_arg(&value, "vol"))
174 } else if (strcmp(key, "cell") == 0) {
175 if (!want_arg(&value, "cell"))
177 cell = afs_cell_lookup(value, strlen(value));
179 return PTR_ERR(cell);
180 afs_put_cell(params->cell);
183 printk("kAFS: Unknown mount option: '%s'\n", key);
191 _leave(" = %d", ret);
196 * parse a device name to get cell name, volume name, volume type and R/W
198 * - this can be one of the following:
199 * "%[cell:]volume[.]" R/W volume
200 * "#[cell:]volume[.]" R/O or R/W volume (rwpath=0),
201 * or R/W (rwpath=1) volume
202 * "%[cell:]volume.readonly" R/O volume
203 * "#[cell:]volume.readonly" R/O volume
204 * "%[cell:]volume.backup" Backup volume
205 * "#[cell:]volume.backup" Backup volume
207 static int afs_parse_device_name(struct afs_mount_params *params,
210 struct afs_cell *cell;
211 const char *cellname, *suffix;
217 printk(KERN_ERR "kAFS: no volume name specified\n");
221 if ((name[0] != '%' && name[0] != '#') || !name[1]) {
222 printk(KERN_ERR "kAFS: unparsable volume name\n");
226 /* determine the type of volume we're looking for */
227 params->type = AFSVL_ROVOL;
228 params->force = false;
229 if (params->rwpath || name[0] == '%') {
230 params->type = AFSVL_RWVOL;
231 params->force = true;
235 /* split the cell name out if there is one */
236 params->volname = strchr(name, ':');
237 if (params->volname) {
239 cellnamesz = params->volname - name;
242 params->volname = name;
247 /* the volume type is further affected by a possible suffix */
248 suffix = strrchr(params->volname, '.');
250 if (strcmp(suffix, ".readonly") == 0) {
251 params->type = AFSVL_ROVOL;
252 params->force = true;
253 } else if (strcmp(suffix, ".backup") == 0) {
254 params->type = AFSVL_BACKVOL;
255 params->force = true;
256 } else if (suffix[1] == 0) {
262 params->volnamesz = suffix ?
263 suffix - params->volname : strlen(params->volname);
265 _debug("cell %*.*s [%p]",
266 cellnamesz, cellnamesz, cellname ?: "", params->cell);
268 /* lookup the cell record */
269 if (cellname || !params->cell) {
270 cell = afs_cell_lookup(cellname, cellnamesz);
272 printk(KERN_ERR "kAFS: unable to lookup cell '%s'\n",
274 return PTR_ERR(cell);
276 afs_put_cell(params->cell);
280 _debug("CELL:%s [%p] VOLUME:%*.*s SUFFIX:%s TYPE:%d%s",
281 params->cell->name, params->cell,
282 params->volnamesz, params->volnamesz, params->volname,
283 suffix ?: "-", params->type, params->force ? " FORCE" : "");
289 * check a superblock to see if it's the one we're looking for
291 static int afs_test_super(struct super_block *sb, void *data)
293 struct afs_mount_params *params = data;
294 struct afs_super_info *as = sb->s_fs_info;
296 return as->volume == params->volume;
300 * fill in the superblock
302 static int afs_fill_super(struct super_block *sb, void *data)
304 struct afs_mount_params *params = data;
305 struct afs_super_info *as = NULL;
307 struct dentry *root = NULL;
308 struct inode *inode = NULL;
313 /* allocate a superblock info record */
314 as = kzalloc(sizeof(struct afs_super_info), GFP_KERNEL);
316 _leave(" = -ENOMEM");
320 afs_get_volume(params->volume);
321 as->volume = params->volume;
323 /* fill in the superblock */
324 sb->s_blocksize = PAGE_CACHE_SIZE;
325 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
326 sb->s_magic = AFS_FS_MAGIC;
327 sb->s_op = &afs_super_ops;
330 /* allocate the root inode and dentry */
331 fid.vid = as->volume->vid;
334 inode = afs_iget(sb, params->key, &fid, NULL, NULL);
339 root = d_alloc_root(inode);
349 ret = PTR_ERR(inode);
353 afs_put_volume(as->volume);
356 sb->s_fs_info = NULL;
358 _leave(" = %d", ret);
363 * get an AFS superblock
364 * - TODO: don't use get_sb_nodev(), but rather call sget() directly
366 static int afs_get_sb(struct file_system_type *fs_type,
368 const char *dev_name,
370 struct vfsmount *mnt)
372 struct afs_mount_params params;
373 struct super_block *sb;
374 struct afs_volume *vol;
378 _enter(",,%s,%p", dev_name, options);
380 memset(¶ms, 0, sizeof(params));
382 /* parse the options and device name */
384 ret = afs_parse_options(¶ms, options, &dev_name);
390 ret = afs_parse_device_name(¶ms, dev_name);
394 /* try and do the mount securely */
395 key = afs_request_key(params.cell);
397 _leave(" = %ld [key]", PTR_ERR(key));
403 /* parse the device name */
404 vol = afs_volume_lookup(¶ms);
411 /* allocate a deviceless superblock */
412 sb = sget(fs_type, afs_test_super, set_anon_super, ¶ms);
419 /* initial superblock/root creation */
422 ret = afs_fill_super(sb, ¶ms);
424 up_write(&sb->s_umount);
425 deactivate_super(sb);
428 sb->s_flags |= MS_ACTIVE;
431 ASSERTCMP(sb->s_flags, &, MS_ACTIVE);
434 simple_set_mnt(mnt, sb);
435 afs_put_volume(params.volume);
436 afs_put_cell(params.cell);
437 _leave(" = 0 [%p]", sb);
441 afs_put_volume(params.volume);
442 afs_put_cell(params.cell);
444 _leave(" = %d", ret);
449 * finish the unmounting process on the superblock
451 static void afs_put_super(struct super_block *sb)
453 struct afs_super_info *as = sb->s_fs_info;
457 afs_put_volume(as->volume);
463 * initialise an inode cache slab element prior to any use
465 static void afs_i_init_once(void *_vnode, struct kmem_cache *cachep,
468 struct afs_vnode *vnode = _vnode;
470 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
471 SLAB_CTOR_CONSTRUCTOR) {
472 memset(vnode, 0, sizeof(*vnode));
473 inode_init_once(&vnode->vfs_inode);
474 init_waitqueue_head(&vnode->update_waitq);
475 mutex_init(&vnode->permits_lock);
476 mutex_init(&vnode->validate_lock);
477 spin_lock_init(&vnode->lock);
478 INIT_WORK(&vnode->cb_broken_work, afs_broken_callback_work);
483 * allocate an AFS inode struct from our slab cache
485 static struct inode *afs_alloc_inode(struct super_block *sb)
487 struct afs_vnode *vnode;
489 vnode = kmem_cache_alloc(afs_inode_cachep, GFP_KERNEL);
493 atomic_inc(&afs_count_active_inodes);
495 memset(&vnode->fid, 0, sizeof(vnode->fid));
496 memset(&vnode->status, 0, sizeof(vnode->status));
498 vnode->volume = NULL;
499 vnode->update_cnt = 0;
500 vnode->flags = 1 << AFS_VNODE_UNSET;
501 vnode->cb_promised = false;
503 return &vnode->vfs_inode;
507 * destroy an AFS inode struct
509 static void afs_destroy_inode(struct inode *inode)
511 struct afs_vnode *vnode = AFS_FS_I(inode);
513 _enter("{%lu}", inode->i_ino);
515 _debug("DESTROY INODE %p", inode);
517 ASSERTCMP(vnode->server, ==, NULL);
519 kmem_cache_free(afs_inode_cachep, vnode);
520 atomic_dec(&afs_count_active_inodes);