xref: /openbmc/linux/fs/afs/super.c (revision f74f70f8b10b435f5f20247e70d1d86b53a59685)
1ec26815aSDavid Howells /* AFS superblock handling
2ec26815aSDavid Howells  *
308e0e7c8SDavid Howells  * Copyright (c) 2002, 2007 Red Hat, Inc. All rights reserved.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * This software may be freely redistributed under the terms of the
61da177e4SLinus Torvalds  * GNU General Public License.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License
91da177e4SLinus Torvalds  * along with this program; if not, write to the Free Software
101da177e4SLinus Torvalds  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  * Authors: David Howells <dhowells@redhat.com>
1344d1b980SDavid Woodhouse  *          David Woodhouse <dwmw2@infradead.org>
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/kernel.h>
181da177e4SLinus Torvalds #include <linux/module.h>
19bec5eb61Swanglei #include <linux/mount.h>
201da177e4SLinus Torvalds #include <linux/init.h>
211da177e4SLinus Torvalds #include <linux/slab.h>
221da177e4SLinus Torvalds #include <linux/fs.h>
231da177e4SLinus Torvalds #include <linux/pagemap.h>
2480c72fe4SDavid Howells #include <linux/parser.h>
2545222b9eSDavid Howells #include <linux/statfs.h>
26e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
27*f74f70f8SEric W. Biederman #include <linux/nsproxy.h>
28*f74f70f8SEric W. Biederman #include <net/net_namespace.h>
291da177e4SLinus Torvalds #include "internal.h"
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds #define AFS_FS_MAGIC 0x6B414653 /* 'kAFS' */
321da177e4SLinus Torvalds 
3351cc5068SAlexey Dobriyan static void afs_i_init_once(void *foo);
34f7442b3bSAl Viro static struct dentry *afs_mount(struct file_system_type *fs_type,
35f7442b3bSAl Viro 		      int flags, const char *dev_name, void *data);
36dde194a6SAl Viro static void afs_kill_super(struct super_block *sb);
371da177e4SLinus Torvalds static struct inode *afs_alloc_inode(struct super_block *sb);
381da177e4SLinus Torvalds static void afs_destroy_inode(struct inode *inode);
3945222b9eSDavid Howells static int afs_statfs(struct dentry *dentry, struct kstatfs *buf);
401da177e4SLinus Torvalds 
411f5ce9e9STrond Myklebust struct file_system_type afs_fs_type = {
421da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
431da177e4SLinus Torvalds 	.name		= "afs",
44f7442b3bSAl Viro 	.mount		= afs_mount,
45dde194a6SAl Viro 	.kill_sb	= afs_kill_super,
4680c72fe4SDavid Howells 	.fs_flags	= 0,
471da177e4SLinus Torvalds };
481da177e4SLinus Torvalds 
49ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations afs_super_ops = {
5045222b9eSDavid Howells 	.statfs		= afs_statfs,
511da177e4SLinus Torvalds 	.alloc_inode	= afs_alloc_inode,
52bec5eb61Swanglei 	.drop_inode	= afs_drop_inode,
531da177e4SLinus Torvalds 	.destroy_inode	= afs_destroy_inode,
54b57922d9SAl Viro 	.evict_inode	= afs_evict_inode,
55969729d5SMiklos Szeredi 	.show_options	= generic_show_options,
561da177e4SLinus Torvalds };
571da177e4SLinus Torvalds 
58e18b890bSChristoph Lameter static struct kmem_cache *afs_inode_cachep;
591da177e4SLinus Torvalds static atomic_t afs_count_active_inodes;
601da177e4SLinus Torvalds 
6180c72fe4SDavid Howells enum {
6280c72fe4SDavid Howells 	afs_no_opt,
6380c72fe4SDavid Howells 	afs_opt_cell,
6480c72fe4SDavid Howells 	afs_opt_rwpath,
6580c72fe4SDavid Howells 	afs_opt_vol,
66bec5eb61Swanglei 	afs_opt_autocell,
6780c72fe4SDavid Howells };
6880c72fe4SDavid Howells 
69a447c093SSteven Whitehouse static const match_table_t afs_options_list = {
7080c72fe4SDavid Howells 	{ afs_opt_cell,		"cell=%s"	},
7180c72fe4SDavid Howells 	{ afs_opt_rwpath,	"rwpath"	},
7280c72fe4SDavid Howells 	{ afs_opt_vol,		"vol=%s"	},
73bec5eb61Swanglei 	{ afs_opt_autocell,	"autocell"	},
7480c72fe4SDavid Howells 	{ afs_no_opt,		NULL		},
7580c72fe4SDavid Howells };
7680c72fe4SDavid Howells 
771da177e4SLinus Torvalds /*
781da177e4SLinus Torvalds  * initialise the filesystem
791da177e4SLinus Torvalds  */
801da177e4SLinus Torvalds int __init afs_fs_init(void)
811da177e4SLinus Torvalds {
821da177e4SLinus Torvalds 	int ret;
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds 	_enter("");
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds 	/* create ourselves an inode cache */
871da177e4SLinus Torvalds 	atomic_set(&afs_count_active_inodes, 0);
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 	ret = -ENOMEM;
901da177e4SLinus Torvalds 	afs_inode_cachep = kmem_cache_create("afs_inode_cache",
911da177e4SLinus Torvalds 					     sizeof(struct afs_vnode),
921da177e4SLinus Torvalds 					     0,
931da177e4SLinus Torvalds 					     SLAB_HWCACHE_ALIGN,
9420c2df83SPaul Mundt 					     afs_i_init_once);
951da177e4SLinus Torvalds 	if (!afs_inode_cachep) {
961da177e4SLinus Torvalds 		printk(KERN_NOTICE "kAFS: Failed to allocate inode cache\n");
971da177e4SLinus Torvalds 		return ret;
981da177e4SLinus Torvalds 	}
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds 	/* now export our filesystem to lesser mortals */
1011da177e4SLinus Torvalds 	ret = register_filesystem(&afs_fs_type);
1021da177e4SLinus Torvalds 	if (ret < 0) {
1031da177e4SLinus Torvalds 		kmem_cache_destroy(afs_inode_cachep);
10408e0e7c8SDavid Howells 		_leave(" = %d", ret);
1051da177e4SLinus Torvalds 		return ret;
1061da177e4SLinus Torvalds 	}
1071da177e4SLinus Torvalds 
10808e0e7c8SDavid Howells 	_leave(" = 0");
1091da177e4SLinus Torvalds 	return 0;
110ec26815aSDavid Howells }
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds /*
1131da177e4SLinus Torvalds  * clean up the filesystem
1141da177e4SLinus Torvalds  */
1151da177e4SLinus Torvalds void __exit afs_fs_exit(void)
1161da177e4SLinus Torvalds {
11708e0e7c8SDavid Howells 	_enter("");
11808e0e7c8SDavid Howells 
11908e0e7c8SDavid Howells 	afs_mntpt_kill_timer();
1201da177e4SLinus Torvalds 	unregister_filesystem(&afs_fs_type);
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 	if (atomic_read(&afs_count_active_inodes) != 0) {
1231da177e4SLinus Torvalds 		printk("kAFS: %d active inode objects still present\n",
1241da177e4SLinus Torvalds 		       atomic_read(&afs_count_active_inodes));
1251da177e4SLinus Torvalds 		BUG();
1261da177e4SLinus Torvalds 	}
1271da177e4SLinus Torvalds 
1288c0a8537SKirill A. Shutemov 	/*
1298c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
1308c0a8537SKirill A. Shutemov 	 * destroy cache.
1318c0a8537SKirill A. Shutemov 	 */
1328c0a8537SKirill A. Shutemov 	rcu_barrier();
1331da177e4SLinus Torvalds 	kmem_cache_destroy(afs_inode_cachep);
13408e0e7c8SDavid Howells 	_leave("");
135ec26815aSDavid Howells }
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds /*
1381da177e4SLinus Torvalds  * parse the mount options
1391da177e4SLinus Torvalds  * - this function has been shamelessly adapted from the ext3 fs which
1401da177e4SLinus Torvalds  *   shamelessly adapted it from the msdos fs
1411da177e4SLinus Torvalds  */
14200d3b7a4SDavid Howells static int afs_parse_options(struct afs_mount_params *params,
14308e0e7c8SDavid Howells 			     char *options, const char **devname)
1441da177e4SLinus Torvalds {
14508e0e7c8SDavid Howells 	struct afs_cell *cell;
14680c72fe4SDavid Howells 	substring_t args[MAX_OPT_ARGS];
14780c72fe4SDavid Howells 	char *p;
14880c72fe4SDavid Howells 	int token;
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 	_enter("%s", options);
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	options[PAGE_SIZE - 1] = 0;
1531da177e4SLinus Torvalds 
15480c72fe4SDavid Howells 	while ((p = strsep(&options, ","))) {
15580c72fe4SDavid Howells 		if (!*p)
15680c72fe4SDavid Howells 			continue;
1571da177e4SLinus Torvalds 
15880c72fe4SDavid Howells 		token = match_token(p, afs_options_list, args);
15980c72fe4SDavid Howells 		switch (token) {
16080c72fe4SDavid Howells 		case afs_opt_cell:
16180c72fe4SDavid Howells 			cell = afs_cell_lookup(args[0].from,
162bec5eb61Swanglei 					       args[0].to - args[0].from,
163bec5eb61Swanglei 					       false);
16408e0e7c8SDavid Howells 			if (IS_ERR(cell))
16508e0e7c8SDavid Howells 				return PTR_ERR(cell);
16600d3b7a4SDavid Howells 			afs_put_cell(params->cell);
16700d3b7a4SDavid Howells 			params->cell = cell;
16880c72fe4SDavid Howells 			break;
16980c72fe4SDavid Howells 
17080c72fe4SDavid Howells 		case afs_opt_rwpath:
17180c72fe4SDavid Howells 			params->rwpath = 1;
17280c72fe4SDavid Howells 			break;
17380c72fe4SDavid Howells 
17480c72fe4SDavid Howells 		case afs_opt_vol:
17580c72fe4SDavid Howells 			*devname = args[0].from;
17680c72fe4SDavid Howells 			break;
17780c72fe4SDavid Howells 
178bec5eb61Swanglei 		case afs_opt_autocell:
179bec5eb61Swanglei 			params->autocell = 1;
180bec5eb61Swanglei 			break;
181bec5eb61Swanglei 
18280c72fe4SDavid Howells 		default:
18380c72fe4SDavid Howells 			printk(KERN_ERR "kAFS:"
18480c72fe4SDavid Howells 			       " Unknown or invalid mount option: '%s'\n", p);
18580c72fe4SDavid Howells 			return -EINVAL;
1861da177e4SLinus Torvalds 		}
18708e0e7c8SDavid Howells 	}
1881da177e4SLinus Torvalds 
18980c72fe4SDavid Howells 	_leave(" = 0");
19080c72fe4SDavid Howells 	return 0;
191ec26815aSDavid Howells }
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds /*
19400d3b7a4SDavid Howells  * parse a device name to get cell name, volume name, volume type and R/W
19500d3b7a4SDavid Howells  * selector
19600d3b7a4SDavid Howells  * - this can be one of the following:
19700d3b7a4SDavid Howells  *	"%[cell:]volume[.]"		R/W volume
19800d3b7a4SDavid Howells  *	"#[cell:]volume[.]"		R/O or R/W volume (rwpath=0),
19900d3b7a4SDavid Howells  *					 or R/W (rwpath=1) volume
20000d3b7a4SDavid Howells  *	"%[cell:]volume.readonly"	R/O volume
20100d3b7a4SDavid Howells  *	"#[cell:]volume.readonly"	R/O volume
20200d3b7a4SDavid Howells  *	"%[cell:]volume.backup"		Backup volume
20300d3b7a4SDavid Howells  *	"#[cell:]volume.backup"		Backup volume
20400d3b7a4SDavid Howells  */
20500d3b7a4SDavid Howells static int afs_parse_device_name(struct afs_mount_params *params,
20600d3b7a4SDavid Howells 				 const char *name)
20700d3b7a4SDavid Howells {
20800d3b7a4SDavid Howells 	struct afs_cell *cell;
20900d3b7a4SDavid Howells 	const char *cellname, *suffix;
21000d3b7a4SDavid Howells 	int cellnamesz;
21100d3b7a4SDavid Howells 
21200d3b7a4SDavid Howells 	_enter(",%s", name);
21300d3b7a4SDavid Howells 
21400d3b7a4SDavid Howells 	if (!name) {
21500d3b7a4SDavid Howells 		printk(KERN_ERR "kAFS: no volume name specified\n");
21600d3b7a4SDavid Howells 		return -EINVAL;
21700d3b7a4SDavid Howells 	}
21800d3b7a4SDavid Howells 
21900d3b7a4SDavid Howells 	if ((name[0] != '%' && name[0] != '#') || !name[1]) {
22000d3b7a4SDavid Howells 		printk(KERN_ERR "kAFS: unparsable volume name\n");
22100d3b7a4SDavid Howells 		return -EINVAL;
22200d3b7a4SDavid Howells 	}
22300d3b7a4SDavid Howells 
22400d3b7a4SDavid Howells 	/* determine the type of volume we're looking for */
22500d3b7a4SDavid Howells 	params->type = AFSVL_ROVOL;
22600d3b7a4SDavid Howells 	params->force = false;
22700d3b7a4SDavid Howells 	if (params->rwpath || name[0] == '%') {
22800d3b7a4SDavid Howells 		params->type = AFSVL_RWVOL;
22900d3b7a4SDavid Howells 		params->force = true;
23000d3b7a4SDavid Howells 	}
23100d3b7a4SDavid Howells 	name++;
23200d3b7a4SDavid Howells 
23300d3b7a4SDavid Howells 	/* split the cell name out if there is one */
23400d3b7a4SDavid Howells 	params->volname = strchr(name, ':');
23500d3b7a4SDavid Howells 	if (params->volname) {
23600d3b7a4SDavid Howells 		cellname = name;
23700d3b7a4SDavid Howells 		cellnamesz = params->volname - name;
23800d3b7a4SDavid Howells 		params->volname++;
23900d3b7a4SDavid Howells 	} else {
24000d3b7a4SDavid Howells 		params->volname = name;
24100d3b7a4SDavid Howells 		cellname = NULL;
24200d3b7a4SDavid Howells 		cellnamesz = 0;
24300d3b7a4SDavid Howells 	}
24400d3b7a4SDavid Howells 
24500d3b7a4SDavid Howells 	/* the volume type is further affected by a possible suffix */
24600d3b7a4SDavid Howells 	suffix = strrchr(params->volname, '.');
24700d3b7a4SDavid Howells 	if (suffix) {
24800d3b7a4SDavid Howells 		if (strcmp(suffix, ".readonly") == 0) {
24900d3b7a4SDavid Howells 			params->type = AFSVL_ROVOL;
25000d3b7a4SDavid Howells 			params->force = true;
25100d3b7a4SDavid Howells 		} else if (strcmp(suffix, ".backup") == 0) {
25200d3b7a4SDavid Howells 			params->type = AFSVL_BACKVOL;
25300d3b7a4SDavid Howells 			params->force = true;
25400d3b7a4SDavid Howells 		} else if (suffix[1] == 0) {
25500d3b7a4SDavid Howells 		} else {
25600d3b7a4SDavid Howells 			suffix = NULL;
25700d3b7a4SDavid Howells 		}
25800d3b7a4SDavid Howells 	}
25900d3b7a4SDavid Howells 
26000d3b7a4SDavid Howells 	params->volnamesz = suffix ?
26100d3b7a4SDavid Howells 		suffix - params->volname : strlen(params->volname);
26200d3b7a4SDavid Howells 
26300d3b7a4SDavid Howells 	_debug("cell %*.*s [%p]",
26400d3b7a4SDavid Howells 	       cellnamesz, cellnamesz, cellname ?: "", params->cell);
26500d3b7a4SDavid Howells 
26600d3b7a4SDavid Howells 	/* lookup the cell record */
26700d3b7a4SDavid Howells 	if (cellname || !params->cell) {
268bec5eb61Swanglei 		cell = afs_cell_lookup(cellname, cellnamesz, true);
26900d3b7a4SDavid Howells 		if (IS_ERR(cell)) {
270bec5eb61Swanglei 			printk(KERN_ERR "kAFS: unable to lookup cell '%*.*s'\n",
271bec5eb61Swanglei 			       cellnamesz, cellnamesz, cellname ?: "");
27200d3b7a4SDavid Howells 			return PTR_ERR(cell);
27300d3b7a4SDavid Howells 		}
27400d3b7a4SDavid Howells 		afs_put_cell(params->cell);
27500d3b7a4SDavid Howells 		params->cell = cell;
27600d3b7a4SDavid Howells 	}
27700d3b7a4SDavid Howells 
27800d3b7a4SDavid Howells 	_debug("CELL:%s [%p] VOLUME:%*.*s SUFFIX:%s TYPE:%d%s",
27900d3b7a4SDavid Howells 	       params->cell->name, params->cell,
28000d3b7a4SDavid Howells 	       params->volnamesz, params->volnamesz, params->volname,
28100d3b7a4SDavid Howells 	       suffix ?: "-", params->type, params->force ? " FORCE" : "");
28200d3b7a4SDavid Howells 
28300d3b7a4SDavid Howells 	return 0;
28400d3b7a4SDavid Howells }
28500d3b7a4SDavid Howells 
28600d3b7a4SDavid Howells /*
2871da177e4SLinus Torvalds  * check a superblock to see if it's the one we're looking for
2881da177e4SLinus Torvalds  */
2891da177e4SLinus Torvalds static int afs_test_super(struct super_block *sb, void *data)
2901da177e4SLinus Torvalds {
291dde194a6SAl Viro 	struct afs_super_info *as1 = data;
2921da177e4SLinus Torvalds 	struct afs_super_info *as = sb->s_fs_info;
2931da177e4SLinus Torvalds 
294dde194a6SAl Viro 	return as->volume == as1->volume;
295dde194a6SAl Viro }
296dde194a6SAl Viro 
297dde194a6SAl Viro static int afs_set_super(struct super_block *sb, void *data)
298dde194a6SAl Viro {
299dde194a6SAl Viro 	sb->s_fs_info = data;
300dde194a6SAl Viro 	return set_anon_super(sb, NULL);
301ec26815aSDavid Howells }
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds /*
3041da177e4SLinus Torvalds  * fill in the superblock
3051da177e4SLinus Torvalds  */
306dde194a6SAl Viro static int afs_fill_super(struct super_block *sb,
307dde194a6SAl Viro 			  struct afs_mount_params *params)
3081da177e4SLinus Torvalds {
309dde194a6SAl Viro 	struct afs_super_info *as = sb->s_fs_info;
3101da177e4SLinus Torvalds 	struct afs_fid fid;
3111da177e4SLinus Torvalds 	struct inode *inode = NULL;
3121da177e4SLinus Torvalds 	int ret;
3131da177e4SLinus Torvalds 
31408e0e7c8SDavid Howells 	_enter("");
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 	/* fill in the superblock */
3171da177e4SLinus Torvalds 	sb->s_blocksize		= PAGE_CACHE_SIZE;
3181da177e4SLinus Torvalds 	sb->s_blocksize_bits	= PAGE_CACHE_SHIFT;
3191da177e4SLinus Torvalds 	sb->s_magic		= AFS_FS_MAGIC;
3201da177e4SLinus Torvalds 	sb->s_op		= &afs_super_ops;
321e1da0222SJens Axboe 	sb->s_bdi		= &as->volume->bdi;
3222e41ae22SDavid Howells 	strlcpy(sb->s_id, as->volume->vlocation->vldb.name, sizeof(sb->s_id));
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	/* allocate the root inode and dentry */
3251da177e4SLinus Torvalds 	fid.vid		= as->volume->vid;
3261da177e4SLinus Torvalds 	fid.vnode	= 1;
3271da177e4SLinus Torvalds 	fid.unique	= 1;
328260a9803SDavid Howells 	inode = afs_iget(sb, params->key, &fid, NULL, NULL);
32908e0e7c8SDavid Howells 	if (IS_ERR(inode))
330dde194a6SAl Viro 		return PTR_ERR(inode);
3311da177e4SLinus Torvalds 
332bec5eb61Swanglei 	if (params->autocell)
333bec5eb61Swanglei 		set_bit(AFS_VNODE_AUTOCELL, &AFS_FS_I(inode)->flags);
334bec5eb61Swanglei 
3351da177e4SLinus Torvalds 	ret = -ENOMEM;
33648fde701SAl Viro 	sb->s_root = d_make_root(inode);
33748fde701SAl Viro 	if (!sb->s_root)
3381da177e4SLinus Torvalds 		goto error;
3391da177e4SLinus Torvalds 
340d61dcce2SAl Viro 	sb->s_d_op = &afs_fs_dentry_operations;
3411da177e4SLinus Torvalds 
34208e0e7c8SDavid Howells 	_leave(" = 0");
3431da177e4SLinus Torvalds 	return 0;
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds error:
34608e0e7c8SDavid Howells 	_leave(" = %d", ret);
3471da177e4SLinus Torvalds 	return ret;
348ec26815aSDavid Howells }
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds /*
3511da177e4SLinus Torvalds  * get an AFS superblock
3521da177e4SLinus Torvalds  */
353f7442b3bSAl Viro static struct dentry *afs_mount(struct file_system_type *fs_type,
354f7442b3bSAl Viro 		      int flags, const char *dev_name, void *options)
3551da177e4SLinus Torvalds {
3561da177e4SLinus Torvalds 	struct afs_mount_params params;
3571da177e4SLinus Torvalds 	struct super_block *sb;
35808e0e7c8SDavid Howells 	struct afs_volume *vol;
35900d3b7a4SDavid Howells 	struct key *key;
360969729d5SMiklos Szeredi 	char *new_opts = kstrdup(options, GFP_KERNEL);
361dde194a6SAl Viro 	struct afs_super_info *as;
3621da177e4SLinus Torvalds 	int ret;
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 	_enter(",,%s,%p", dev_name, options);
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 	memset(&params, 0, sizeof(params));
3671da177e4SLinus Torvalds 
368*f74f70f8SEric W. Biederman 	ret = -EINVAL;
369*f74f70f8SEric W. Biederman 	if (current->nsproxy->net_ns != &init_net)
370*f74f70f8SEric W. Biederman 		goto error;
371*f74f70f8SEric W. Biederman 
37200d3b7a4SDavid Howells 	/* parse the options and device name */
3731da177e4SLinus Torvalds 	if (options) {
37400d3b7a4SDavid Howells 		ret = afs_parse_options(&params, options, &dev_name);
3751da177e4SLinus Torvalds 		if (ret < 0)
3761da177e4SLinus Torvalds 			goto error;
3771da177e4SLinus Torvalds 	}
3781da177e4SLinus Torvalds 
37900d3b7a4SDavid Howells 	ret = afs_parse_device_name(&params, dev_name);
38000d3b7a4SDavid Howells 	if (ret < 0)
38100d3b7a4SDavid Howells 		goto error;
38200d3b7a4SDavid Howells 
38300d3b7a4SDavid Howells 	/* try and do the mount securely */
38400d3b7a4SDavid Howells 	key = afs_request_key(params.cell);
38500d3b7a4SDavid Howells 	if (IS_ERR(key)) {
38600d3b7a4SDavid Howells 		_leave(" = %ld [key]", PTR_ERR(key));
38700d3b7a4SDavid Howells 		ret = PTR_ERR(key);
38800d3b7a4SDavid Howells 		goto error;
38900d3b7a4SDavid Howells 	}
39000d3b7a4SDavid Howells 	params.key = key;
39100d3b7a4SDavid Howells 
3921da177e4SLinus Torvalds 	/* parse the device name */
39300d3b7a4SDavid Howells 	vol = afs_volume_lookup(&params);
39408e0e7c8SDavid Howells 	if (IS_ERR(vol)) {
39508e0e7c8SDavid Howells 		ret = PTR_ERR(vol);
3961da177e4SLinus Torvalds 		goto error;
39708e0e7c8SDavid Howells 	}
398dde194a6SAl Viro 
399dde194a6SAl Viro 	/* allocate a superblock info record */
400dde194a6SAl Viro 	as = kzalloc(sizeof(struct afs_super_info), GFP_KERNEL);
401dde194a6SAl Viro 	if (!as) {
402dde194a6SAl Viro 		ret = -ENOMEM;
403dde194a6SAl Viro 		afs_put_volume(vol);
404dde194a6SAl Viro 		goto error;
405dde194a6SAl Viro 	}
406dde194a6SAl Viro 	as->volume = vol;
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 	/* allocate a deviceless superblock */
4099249e17fSDavid Howells 	sb = sget(fs_type, afs_test_super, afs_set_super, flags, as);
41008e0e7c8SDavid Howells 	if (IS_ERR(sb)) {
41108e0e7c8SDavid Howells 		ret = PTR_ERR(sb);
412dde194a6SAl Viro 		afs_put_volume(vol);
413dde194a6SAl Viro 		kfree(as);
4141da177e4SLinus Torvalds 		goto error;
41508e0e7c8SDavid Howells 	}
4161da177e4SLinus Torvalds 
417436058a4SDavid Howells 	if (!sb->s_root) {
418436058a4SDavid Howells 		/* initial superblock/root creation */
419436058a4SDavid Howells 		_debug("create");
420436058a4SDavid Howells 		ret = afs_fill_super(sb, &params);
4211da177e4SLinus Torvalds 		if (ret < 0) {
4226f5bbff9SAl Viro 			deactivate_locked_super(sb);
4231da177e4SLinus Torvalds 			goto error;
4241da177e4SLinus Torvalds 		}
4252a32cebdSAl Viro 		save_mount_options(sb, new_opts);
4261da177e4SLinus Torvalds 		sb->s_flags |= MS_ACTIVE;
427436058a4SDavid Howells 	} else {
428436058a4SDavid Howells 		_debug("reuse");
429436058a4SDavid Howells 		ASSERTCMP(sb->s_flags, &, MS_ACTIVE);
430dde194a6SAl Viro 		afs_put_volume(vol);
431dde194a6SAl Viro 		kfree(as);
432436058a4SDavid Howells 	}
4331da177e4SLinus Torvalds 
43400d3b7a4SDavid Howells 	afs_put_cell(params.cell);
4352a32cebdSAl Viro 	kfree(new_opts);
43608e0e7c8SDavid Howells 	_leave(" = 0 [%p]", sb);
437f7442b3bSAl Viro 	return dget(sb->s_root);
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds error:
44000d3b7a4SDavid Howells 	afs_put_cell(params.cell);
44100d3b7a4SDavid Howells 	key_put(params.key);
442969729d5SMiklos Szeredi 	kfree(new_opts);
4431da177e4SLinus Torvalds 	_leave(" = %d", ret);
444f7442b3bSAl Viro 	return ERR_PTR(ret);
445ec26815aSDavid Howells }
4461da177e4SLinus Torvalds 
447dde194a6SAl Viro static void afs_kill_super(struct super_block *sb)
4481da177e4SLinus Torvalds {
4491da177e4SLinus Torvalds 	struct afs_super_info *as = sb->s_fs_info;
450dde194a6SAl Viro 	kill_anon_super(sb);
4511da177e4SLinus Torvalds 	afs_put_volume(as->volume);
452dde194a6SAl Viro 	kfree(as);
453ec26815aSDavid Howells }
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds /*
4561da177e4SLinus Torvalds  * initialise an inode cache slab element prior to any use
4571da177e4SLinus Torvalds  */
45851cc5068SAlexey Dobriyan static void afs_i_init_once(void *_vnode)
4591da177e4SLinus Torvalds {
460ec26815aSDavid Howells 	struct afs_vnode *vnode = _vnode;
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 	memset(vnode, 0, sizeof(*vnode));
4631da177e4SLinus Torvalds 	inode_init_once(&vnode->vfs_inode);
4641da177e4SLinus Torvalds 	init_waitqueue_head(&vnode->update_waitq);
46500d3b7a4SDavid Howells 	mutex_init(&vnode->permits_lock);
466260a9803SDavid Howells 	mutex_init(&vnode->validate_lock);
46731143d5dSDavid Howells 	spin_lock_init(&vnode->writeback_lock);
4681da177e4SLinus Torvalds 	spin_lock_init(&vnode->lock);
46931143d5dSDavid Howells 	INIT_LIST_HEAD(&vnode->writebacks);
470e8d6c554SDavid Howells 	INIT_LIST_HEAD(&vnode->pending_locks);
471e8d6c554SDavid Howells 	INIT_LIST_HEAD(&vnode->granted_locks);
472e8d6c554SDavid Howells 	INIT_DELAYED_WORK(&vnode->lock_work, afs_lock_work);
47308e0e7c8SDavid Howells 	INIT_WORK(&vnode->cb_broken_work, afs_broken_callback_work);
4741da177e4SLinus Torvalds }
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds /*
4771da177e4SLinus Torvalds  * allocate an AFS inode struct from our slab cache
4781da177e4SLinus Torvalds  */
4791da177e4SLinus Torvalds static struct inode *afs_alloc_inode(struct super_block *sb)
4801da177e4SLinus Torvalds {
4811da177e4SLinus Torvalds 	struct afs_vnode *vnode;
4821da177e4SLinus Torvalds 
483ec26815aSDavid Howells 	vnode = kmem_cache_alloc(afs_inode_cachep, GFP_KERNEL);
4841da177e4SLinus Torvalds 	if (!vnode)
4851da177e4SLinus Torvalds 		return NULL;
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 	atomic_inc(&afs_count_active_inodes);
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	memset(&vnode->fid, 0, sizeof(vnode->fid));
4901da177e4SLinus Torvalds 	memset(&vnode->status, 0, sizeof(vnode->status));
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 	vnode->volume		= NULL;
4931da177e4SLinus Torvalds 	vnode->update_cnt	= 0;
494260a9803SDavid Howells 	vnode->flags		= 1 << AFS_VNODE_UNSET;
49508e0e7c8SDavid Howells 	vnode->cb_promised	= false;
4961da177e4SLinus Torvalds 
4970f300ca9SDavid Howells 	_leave(" = %p", &vnode->vfs_inode);
4981da177e4SLinus Torvalds 	return &vnode->vfs_inode;
499ec26815aSDavid Howells }
5001da177e4SLinus Torvalds 
501fa0d7e3dSNick Piggin static void afs_i_callback(struct rcu_head *head)
502fa0d7e3dSNick Piggin {
503fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
504fa0d7e3dSNick Piggin 	struct afs_vnode *vnode = AFS_FS_I(inode);
505fa0d7e3dSNick Piggin 	kmem_cache_free(afs_inode_cachep, vnode);
506fa0d7e3dSNick Piggin }
507fa0d7e3dSNick Piggin 
5081da177e4SLinus Torvalds /*
5091da177e4SLinus Torvalds  * destroy an AFS inode struct
5101da177e4SLinus Torvalds  */
5111da177e4SLinus Torvalds static void afs_destroy_inode(struct inode *inode)
5121da177e4SLinus Torvalds {
51308e0e7c8SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(inode);
51408e0e7c8SDavid Howells 
5150f300ca9SDavid Howells 	_enter("%p{%x:%u}", inode, vnode->fid.vid, vnode->fid.vnode);
5161da177e4SLinus Torvalds 
51708e0e7c8SDavid Howells 	_debug("DESTROY INODE %p", inode);
51808e0e7c8SDavid Howells 
51908e0e7c8SDavid Howells 	ASSERTCMP(vnode->server, ==, NULL);
52008e0e7c8SDavid Howells 
521fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, afs_i_callback);
5221da177e4SLinus Torvalds 	atomic_dec(&afs_count_active_inodes);
523ec26815aSDavid Howells }
52445222b9eSDavid Howells 
52545222b9eSDavid Howells /*
52645222b9eSDavid Howells  * return information about an AFS volume
52745222b9eSDavid Howells  */
52845222b9eSDavid Howells static int afs_statfs(struct dentry *dentry, struct kstatfs *buf)
52945222b9eSDavid Howells {
53045222b9eSDavid Howells 	struct afs_volume_status vs;
53145222b9eSDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(dentry->d_inode);
53245222b9eSDavid Howells 	struct key *key;
53345222b9eSDavid Howells 	int ret;
53445222b9eSDavid Howells 
53545222b9eSDavid Howells 	key = afs_request_key(vnode->volume->cell);
53645222b9eSDavid Howells 	if (IS_ERR(key))
53745222b9eSDavid Howells 		return PTR_ERR(key);
53845222b9eSDavid Howells 
53945222b9eSDavid Howells 	ret = afs_vnode_get_volume_status(vnode, key, &vs);
54045222b9eSDavid Howells 	key_put(key);
54145222b9eSDavid Howells 	if (ret < 0) {
54245222b9eSDavid Howells 		_leave(" = %d", ret);
54345222b9eSDavid Howells 		return ret;
54445222b9eSDavid Howells 	}
54545222b9eSDavid Howells 
54645222b9eSDavid Howells 	buf->f_type	= dentry->d_sb->s_magic;
54745222b9eSDavid Howells 	buf->f_bsize	= AFS_BLOCK_SIZE;
54845222b9eSDavid Howells 	buf->f_namelen	= AFSNAMEMAX - 1;
54945222b9eSDavid Howells 
55045222b9eSDavid Howells 	if (vs.max_quota == 0)
55145222b9eSDavid Howells 		buf->f_blocks = vs.part_max_blocks;
55245222b9eSDavid Howells 	else
55345222b9eSDavid Howells 		buf->f_blocks = vs.max_quota;
55445222b9eSDavid Howells 	buf->f_bavail = buf->f_bfree = buf->f_blocks - vs.blocks_in_use;
55545222b9eSDavid Howells 	return 0;
55645222b9eSDavid Howells }
557