xref: /openbmc/linux/fs/afs/super.c (revision f8de483e7440b0d23ce6372b3ef8358841c8827b)
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>
27f74f70f8SEric W. Biederman #include <linux/nsproxy.h>
28f044c884SDavid Howells #include <linux/magic.h>
29f74f70f8SEric W. Biederman #include <net/net_namespace.h>
301da177e4SLinus Torvalds #include "internal.h"
311da177e4SLinus Torvalds 
3251cc5068SAlexey Dobriyan static void afs_i_init_once(void *foo);
33f7442b3bSAl Viro static struct dentry *afs_mount(struct file_system_type *fs_type,
34f7442b3bSAl Viro 		      int flags, const char *dev_name, void *data);
35dde194a6SAl Viro static void afs_kill_super(struct super_block *sb);
361da177e4SLinus Torvalds static struct inode *afs_alloc_inode(struct super_block *sb);
371da177e4SLinus Torvalds static void afs_destroy_inode(struct inode *inode);
3845222b9eSDavid Howells static int afs_statfs(struct dentry *dentry, struct kstatfs *buf);
39677018a6SDavid Howells static int afs_show_devname(struct seq_file *m, struct dentry *root);
40677018a6SDavid Howells static int afs_show_options(struct seq_file *m, struct dentry *root);
411da177e4SLinus Torvalds 
421f5ce9e9STrond Myklebust struct file_system_type afs_fs_type = {
431da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
441da177e4SLinus Torvalds 	.name		= "afs",
45f7442b3bSAl Viro 	.mount		= afs_mount,
46dde194a6SAl Viro 	.kill_sb	= afs_kill_super,
4780c72fe4SDavid Howells 	.fs_flags	= 0,
481da177e4SLinus Torvalds };
497f78e035SEric W. Biederman MODULE_ALIAS_FS("afs");
501da177e4SLinus Torvalds 
51ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations afs_super_ops = {
5245222b9eSDavid Howells 	.statfs		= afs_statfs,
531da177e4SLinus Torvalds 	.alloc_inode	= afs_alloc_inode,
54bec5eb61Swanglei 	.drop_inode	= afs_drop_inode,
551da177e4SLinus Torvalds 	.destroy_inode	= afs_destroy_inode,
56b57922d9SAl Viro 	.evict_inode	= afs_evict_inode,
57677018a6SDavid Howells 	.show_devname	= afs_show_devname,
58677018a6SDavid Howells 	.show_options	= afs_show_options,
591da177e4SLinus Torvalds };
601da177e4SLinus Torvalds 
61e18b890bSChristoph Lameter static struct kmem_cache *afs_inode_cachep;
621da177e4SLinus Torvalds static atomic_t afs_count_active_inodes;
631da177e4SLinus Torvalds 
6480c72fe4SDavid Howells enum {
6580c72fe4SDavid Howells 	afs_no_opt,
6680c72fe4SDavid Howells 	afs_opt_cell,
6780c72fe4SDavid Howells 	afs_opt_rwpath,
6880c72fe4SDavid Howells 	afs_opt_vol,
69bec5eb61Swanglei 	afs_opt_autocell,
7080c72fe4SDavid Howells };
7180c72fe4SDavid Howells 
72a447c093SSteven Whitehouse static const match_table_t afs_options_list = {
7380c72fe4SDavid Howells 	{ afs_opt_cell,		"cell=%s"	},
7480c72fe4SDavid Howells 	{ afs_opt_rwpath,	"rwpath"	},
7580c72fe4SDavid Howells 	{ afs_opt_vol,		"vol=%s"	},
76bec5eb61Swanglei 	{ afs_opt_autocell,	"autocell"	},
7780c72fe4SDavid Howells 	{ afs_no_opt,		NULL		},
7880c72fe4SDavid Howells };
7980c72fe4SDavid Howells 
801da177e4SLinus Torvalds /*
811da177e4SLinus Torvalds  * initialise the filesystem
821da177e4SLinus Torvalds  */
831da177e4SLinus Torvalds int __init afs_fs_init(void)
841da177e4SLinus Torvalds {
851da177e4SLinus Torvalds 	int ret;
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 	_enter("");
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 	/* create ourselves an inode cache */
901da177e4SLinus Torvalds 	atomic_set(&afs_count_active_inodes, 0);
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds 	ret = -ENOMEM;
931da177e4SLinus Torvalds 	afs_inode_cachep = kmem_cache_create("afs_inode_cache",
941da177e4SLinus Torvalds 					     sizeof(struct afs_vnode),
951da177e4SLinus Torvalds 					     0,
965d097056SVladimir Davydov 					     SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT,
9720c2df83SPaul Mundt 					     afs_i_init_once);
981da177e4SLinus Torvalds 	if (!afs_inode_cachep) {
991da177e4SLinus Torvalds 		printk(KERN_NOTICE "kAFS: Failed to allocate inode cache\n");
1001da177e4SLinus Torvalds 		return ret;
1011da177e4SLinus Torvalds 	}
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds 	/* now export our filesystem to lesser mortals */
1041da177e4SLinus Torvalds 	ret = register_filesystem(&afs_fs_type);
1051da177e4SLinus Torvalds 	if (ret < 0) {
1061da177e4SLinus Torvalds 		kmem_cache_destroy(afs_inode_cachep);
10708e0e7c8SDavid Howells 		_leave(" = %d", ret);
1081da177e4SLinus Torvalds 		return ret;
1091da177e4SLinus Torvalds 	}
1101da177e4SLinus Torvalds 
11108e0e7c8SDavid Howells 	_leave(" = 0");
1121da177e4SLinus Torvalds 	return 0;
113ec26815aSDavid Howells }
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds /*
1161da177e4SLinus Torvalds  * clean up the filesystem
1171da177e4SLinus Torvalds  */
1181da177e4SLinus Torvalds void __exit afs_fs_exit(void)
1191da177e4SLinus Torvalds {
12008e0e7c8SDavid Howells 	_enter("");
12108e0e7c8SDavid Howells 
12208e0e7c8SDavid Howells 	afs_mntpt_kill_timer();
1231da177e4SLinus Torvalds 	unregister_filesystem(&afs_fs_type);
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 	if (atomic_read(&afs_count_active_inodes) != 0) {
1261da177e4SLinus Torvalds 		printk("kAFS: %d active inode objects still present\n",
1271da177e4SLinus Torvalds 		       atomic_read(&afs_count_active_inodes));
1281da177e4SLinus Torvalds 		BUG();
1291da177e4SLinus Torvalds 	}
1301da177e4SLinus Torvalds 
1318c0a8537SKirill A. Shutemov 	/*
1328c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
1338c0a8537SKirill A. Shutemov 	 * destroy cache.
1348c0a8537SKirill A. Shutemov 	 */
1358c0a8537SKirill A. Shutemov 	rcu_barrier();
1361da177e4SLinus Torvalds 	kmem_cache_destroy(afs_inode_cachep);
13708e0e7c8SDavid Howells 	_leave("");
138ec26815aSDavid Howells }
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds /*
141677018a6SDavid Howells  * Display the mount device name in /proc/mounts.
142677018a6SDavid Howells  */
143677018a6SDavid Howells static int afs_show_devname(struct seq_file *m, struct dentry *root)
144677018a6SDavid Howells {
145d2ddc776SDavid Howells 	struct afs_super_info *as = AFS_FS_S(root->d_sb);
146677018a6SDavid Howells 	struct afs_volume *volume = as->volume;
147d2ddc776SDavid Howells 	struct afs_cell *cell = as->cell;
148677018a6SDavid Howells 	const char *suf = "";
149677018a6SDavid Howells 	char pref = '%';
150677018a6SDavid Howells 
151677018a6SDavid Howells 	switch (volume->type) {
152677018a6SDavid Howells 	case AFSVL_RWVOL:
153677018a6SDavid Howells 		break;
154677018a6SDavid Howells 	case AFSVL_ROVOL:
155677018a6SDavid Howells 		pref = '#';
156677018a6SDavid Howells 		if (volume->type_force)
157677018a6SDavid Howells 			suf = ".readonly";
158677018a6SDavid Howells 		break;
159677018a6SDavid Howells 	case AFSVL_BACKVOL:
160677018a6SDavid Howells 		pref = '#';
161677018a6SDavid Howells 		suf = ".backup";
162677018a6SDavid Howells 		break;
163677018a6SDavid Howells 	}
164677018a6SDavid Howells 
165d2ddc776SDavid Howells 	seq_printf(m, "%c%s:%s%s", pref, cell->name, volume->name, suf);
166677018a6SDavid Howells 	return 0;
167677018a6SDavid Howells }
168677018a6SDavid Howells 
169677018a6SDavid Howells /*
170677018a6SDavid Howells  * Display the mount options in /proc/mounts.
171677018a6SDavid Howells  */
172677018a6SDavid Howells static int afs_show_options(struct seq_file *m, struct dentry *root)
173677018a6SDavid Howells {
174677018a6SDavid Howells 	if (test_bit(AFS_VNODE_AUTOCELL, &AFS_FS_I(d_inode(root))->flags))
175677018a6SDavid Howells 		seq_puts(m, "autocell");
176677018a6SDavid Howells 	return 0;
177677018a6SDavid Howells }
178677018a6SDavid Howells 
179677018a6SDavid Howells /*
1801da177e4SLinus Torvalds  * parse the mount options
1811da177e4SLinus Torvalds  * - this function has been shamelessly adapted from the ext3 fs which
1821da177e4SLinus Torvalds  *   shamelessly adapted it from the msdos fs
1831da177e4SLinus Torvalds  */
18400d3b7a4SDavid Howells static int afs_parse_options(struct afs_mount_params *params,
18508e0e7c8SDavid Howells 			     char *options, const char **devname)
1861da177e4SLinus Torvalds {
18708e0e7c8SDavid Howells 	struct afs_cell *cell;
18880c72fe4SDavid Howells 	substring_t args[MAX_OPT_ARGS];
18980c72fe4SDavid Howells 	char *p;
19080c72fe4SDavid Howells 	int token;
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	_enter("%s", options);
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	options[PAGE_SIZE - 1] = 0;
1951da177e4SLinus Torvalds 
19680c72fe4SDavid Howells 	while ((p = strsep(&options, ","))) {
19780c72fe4SDavid Howells 		if (!*p)
19880c72fe4SDavid Howells 			continue;
1991da177e4SLinus Torvalds 
20080c72fe4SDavid Howells 		token = match_token(p, afs_options_list, args);
20180c72fe4SDavid Howells 		switch (token) {
20280c72fe4SDavid Howells 		case afs_opt_cell:
203989782dcSDavid Howells 			rcu_read_lock();
204989782dcSDavid Howells 			cell = afs_lookup_cell_rcu(params->net,
205f044c884SDavid Howells 						   args[0].from,
206989782dcSDavid Howells 						   args[0].to - args[0].from);
207989782dcSDavid Howells 			rcu_read_unlock();
20808e0e7c8SDavid Howells 			if (IS_ERR(cell))
20908e0e7c8SDavid Howells 				return PTR_ERR(cell);
2109ed900b1SDavid Howells 			afs_put_cell(params->net, params->cell);
21100d3b7a4SDavid Howells 			params->cell = cell;
21280c72fe4SDavid Howells 			break;
21380c72fe4SDavid Howells 
21480c72fe4SDavid Howells 		case afs_opt_rwpath:
21580c72fe4SDavid Howells 			params->rwpath = 1;
21680c72fe4SDavid Howells 			break;
21780c72fe4SDavid Howells 
21880c72fe4SDavid Howells 		case afs_opt_vol:
21980c72fe4SDavid Howells 			*devname = args[0].from;
22080c72fe4SDavid Howells 			break;
22180c72fe4SDavid Howells 
222bec5eb61Swanglei 		case afs_opt_autocell:
223bec5eb61Swanglei 			params->autocell = 1;
224bec5eb61Swanglei 			break;
225bec5eb61Swanglei 
22680c72fe4SDavid Howells 		default:
22780c72fe4SDavid Howells 			printk(KERN_ERR "kAFS:"
22880c72fe4SDavid Howells 			       " Unknown or invalid mount option: '%s'\n", p);
22980c72fe4SDavid Howells 			return -EINVAL;
2301da177e4SLinus Torvalds 		}
23108e0e7c8SDavid Howells 	}
2321da177e4SLinus Torvalds 
23380c72fe4SDavid Howells 	_leave(" = 0");
23480c72fe4SDavid Howells 	return 0;
235ec26815aSDavid Howells }
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds /*
23800d3b7a4SDavid Howells  * parse a device name to get cell name, volume name, volume type and R/W
23900d3b7a4SDavid Howells  * selector
24000d3b7a4SDavid Howells  * - this can be one of the following:
24100d3b7a4SDavid Howells  *	"%[cell:]volume[.]"		R/W volume
24200d3b7a4SDavid Howells  *	"#[cell:]volume[.]"		R/O or R/W volume (rwpath=0),
24300d3b7a4SDavid Howells  *					 or R/W (rwpath=1) volume
24400d3b7a4SDavid Howells  *	"%[cell:]volume.readonly"	R/O volume
24500d3b7a4SDavid Howells  *	"#[cell:]volume.readonly"	R/O volume
24600d3b7a4SDavid Howells  *	"%[cell:]volume.backup"		Backup volume
24700d3b7a4SDavid Howells  *	"#[cell:]volume.backup"		Backup volume
24800d3b7a4SDavid Howells  */
24900d3b7a4SDavid Howells static int afs_parse_device_name(struct afs_mount_params *params,
25000d3b7a4SDavid Howells 				 const char *name)
25100d3b7a4SDavid Howells {
25200d3b7a4SDavid Howells 	struct afs_cell *cell;
25300d3b7a4SDavid Howells 	const char *cellname, *suffix;
25400d3b7a4SDavid Howells 	int cellnamesz;
25500d3b7a4SDavid Howells 
25600d3b7a4SDavid Howells 	_enter(",%s", name);
25700d3b7a4SDavid Howells 
25800d3b7a4SDavid Howells 	if (!name) {
25900d3b7a4SDavid Howells 		printk(KERN_ERR "kAFS: no volume name specified\n");
26000d3b7a4SDavid Howells 		return -EINVAL;
26100d3b7a4SDavid Howells 	}
26200d3b7a4SDavid Howells 
26300d3b7a4SDavid Howells 	if ((name[0] != '%' && name[0] != '#') || !name[1]) {
26400d3b7a4SDavid Howells 		printk(KERN_ERR "kAFS: unparsable volume name\n");
26500d3b7a4SDavid Howells 		return -EINVAL;
26600d3b7a4SDavid Howells 	}
26700d3b7a4SDavid Howells 
26800d3b7a4SDavid Howells 	/* determine the type of volume we're looking for */
26900d3b7a4SDavid Howells 	params->type = AFSVL_ROVOL;
27000d3b7a4SDavid Howells 	params->force = false;
27100d3b7a4SDavid Howells 	if (params->rwpath || name[0] == '%') {
27200d3b7a4SDavid Howells 		params->type = AFSVL_RWVOL;
27300d3b7a4SDavid Howells 		params->force = true;
27400d3b7a4SDavid Howells 	}
27500d3b7a4SDavid Howells 	name++;
27600d3b7a4SDavid Howells 
27700d3b7a4SDavid Howells 	/* split the cell name out if there is one */
27800d3b7a4SDavid Howells 	params->volname = strchr(name, ':');
27900d3b7a4SDavid Howells 	if (params->volname) {
28000d3b7a4SDavid Howells 		cellname = name;
28100d3b7a4SDavid Howells 		cellnamesz = params->volname - name;
28200d3b7a4SDavid Howells 		params->volname++;
28300d3b7a4SDavid Howells 	} else {
28400d3b7a4SDavid Howells 		params->volname = name;
28500d3b7a4SDavid Howells 		cellname = NULL;
28600d3b7a4SDavid Howells 		cellnamesz = 0;
28700d3b7a4SDavid Howells 	}
28800d3b7a4SDavid Howells 
28900d3b7a4SDavid Howells 	/* the volume type is further affected by a possible suffix */
29000d3b7a4SDavid Howells 	suffix = strrchr(params->volname, '.');
29100d3b7a4SDavid Howells 	if (suffix) {
29200d3b7a4SDavid Howells 		if (strcmp(suffix, ".readonly") == 0) {
29300d3b7a4SDavid Howells 			params->type = AFSVL_ROVOL;
29400d3b7a4SDavid Howells 			params->force = true;
29500d3b7a4SDavid Howells 		} else if (strcmp(suffix, ".backup") == 0) {
29600d3b7a4SDavid Howells 			params->type = AFSVL_BACKVOL;
29700d3b7a4SDavid Howells 			params->force = true;
29800d3b7a4SDavid Howells 		} else if (suffix[1] == 0) {
29900d3b7a4SDavid Howells 		} else {
30000d3b7a4SDavid Howells 			suffix = NULL;
30100d3b7a4SDavid Howells 		}
30200d3b7a4SDavid Howells 	}
30300d3b7a4SDavid Howells 
30400d3b7a4SDavid Howells 	params->volnamesz = suffix ?
30500d3b7a4SDavid Howells 		suffix - params->volname : strlen(params->volname);
30600d3b7a4SDavid Howells 
30700d3b7a4SDavid Howells 	_debug("cell %*.*s [%p]",
30800d3b7a4SDavid Howells 	       cellnamesz, cellnamesz, cellname ?: "", params->cell);
30900d3b7a4SDavid Howells 
31000d3b7a4SDavid Howells 	/* lookup the cell record */
31100d3b7a4SDavid Howells 	if (cellname || !params->cell) {
312989782dcSDavid Howells 		cell = afs_lookup_cell(params->net, cellname, cellnamesz,
313989782dcSDavid Howells 				       NULL, false);
31400d3b7a4SDavid Howells 		if (IS_ERR(cell)) {
315bec5eb61Swanglei 			printk(KERN_ERR "kAFS: unable to lookup cell '%*.*s'\n",
316bec5eb61Swanglei 			       cellnamesz, cellnamesz, cellname ?: "");
31700d3b7a4SDavid Howells 			return PTR_ERR(cell);
31800d3b7a4SDavid Howells 		}
3199ed900b1SDavid Howells 		afs_put_cell(params->net, params->cell);
32000d3b7a4SDavid Howells 		params->cell = cell;
32100d3b7a4SDavid Howells 	}
32200d3b7a4SDavid Howells 
32300d3b7a4SDavid Howells 	_debug("CELL:%s [%p] VOLUME:%*.*s SUFFIX:%s TYPE:%d%s",
32400d3b7a4SDavid Howells 	       params->cell->name, params->cell,
32500d3b7a4SDavid Howells 	       params->volnamesz, params->volnamesz, params->volname,
32600d3b7a4SDavid Howells 	       suffix ?: "-", params->type, params->force ? " FORCE" : "");
32700d3b7a4SDavid Howells 
32800d3b7a4SDavid Howells 	return 0;
32900d3b7a4SDavid Howells }
33000d3b7a4SDavid Howells 
33100d3b7a4SDavid Howells /*
3321da177e4SLinus Torvalds  * check a superblock to see if it's the one we're looking for
3331da177e4SLinus Torvalds  */
3341da177e4SLinus Torvalds static int afs_test_super(struct super_block *sb, void *data)
3351da177e4SLinus Torvalds {
336dde194a6SAl Viro 	struct afs_super_info *as1 = data;
337d2ddc776SDavid Howells 	struct afs_super_info *as = AFS_FS_S(sb);
3381da177e4SLinus Torvalds 
339d2ddc776SDavid Howells 	return as->net == as1->net && as->volume->vid == as1->volume->vid;
340dde194a6SAl Viro }
341dde194a6SAl Viro 
342dde194a6SAl Viro static int afs_set_super(struct super_block *sb, void *data)
343dde194a6SAl Viro {
344d2ddc776SDavid Howells 	struct afs_super_info *as = data;
345d2ddc776SDavid Howells 
346d2ddc776SDavid Howells 	sb->s_fs_info = as;
347dde194a6SAl Viro 	return set_anon_super(sb, NULL);
348ec26815aSDavid Howells }
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds /*
3511da177e4SLinus Torvalds  * fill in the superblock
3521da177e4SLinus Torvalds  */
353dde194a6SAl Viro static int afs_fill_super(struct super_block *sb,
354dde194a6SAl Viro 			  struct afs_mount_params *params)
3551da177e4SLinus Torvalds {
356d2ddc776SDavid Howells 	struct afs_super_info *as = AFS_FS_S(sb);
3571da177e4SLinus Torvalds 	struct afs_fid fid;
3581da177e4SLinus Torvalds 	struct inode *inode = NULL;
3591da177e4SLinus Torvalds 	int ret;
3601da177e4SLinus Torvalds 
36108e0e7c8SDavid Howells 	_enter("");
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	/* fill in the superblock */
36409cbfeafSKirill A. Shutemov 	sb->s_blocksize		= PAGE_SIZE;
36509cbfeafSKirill A. Shutemov 	sb->s_blocksize_bits	= PAGE_SHIFT;
3661da177e4SLinus Torvalds 	sb->s_magic		= AFS_FS_MAGIC;
3671da177e4SLinus Torvalds 	sb->s_op		= &afs_super_ops;
368d3e3b7eaSDavid Howells 	sb->s_xattr		= afs_xattr_handlers;
369edd3ba94SJan Kara 	ret = super_setup_bdi(sb);
370edd3ba94SJan Kara 	if (ret)
371edd3ba94SJan Kara 		return ret;
372edd3ba94SJan Kara 	sb->s_bdi->ra_pages	= VM_MAX_READAHEAD * 1024 / PAGE_SIZE;
373d2ddc776SDavid Howells 	sprintf(sb->s_id, "%u", as->volume->vid);
374d2ddc776SDavid Howells 
375d2ddc776SDavid Howells 	afs_activate_volume(as->volume);
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 	/* allocate the root inode and dentry */
3781da177e4SLinus Torvalds 	fid.vid		= as->volume->vid;
3791da177e4SLinus Torvalds 	fid.vnode	= 1;
3801da177e4SLinus Torvalds 	fid.unique	= 1;
381d2ddc776SDavid Howells 	inode = afs_iget(sb, params->key, &fid, NULL, NULL, NULL);
38208e0e7c8SDavid Howells 	if (IS_ERR(inode))
383dde194a6SAl Viro 		return PTR_ERR(inode);
3841da177e4SLinus Torvalds 
385bec5eb61Swanglei 	if (params->autocell)
386bec5eb61Swanglei 		set_bit(AFS_VNODE_AUTOCELL, &AFS_FS_I(inode)->flags);
387bec5eb61Swanglei 
3881da177e4SLinus Torvalds 	ret = -ENOMEM;
38948fde701SAl Viro 	sb->s_root = d_make_root(inode);
39048fde701SAl Viro 	if (!sb->s_root)
3911da177e4SLinus Torvalds 		goto error;
3921da177e4SLinus Torvalds 
393d61dcce2SAl Viro 	sb->s_d_op = &afs_fs_dentry_operations;
3941da177e4SLinus Torvalds 
39508e0e7c8SDavid Howells 	_leave(" = 0");
3961da177e4SLinus Torvalds 	return 0;
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds error:
39908e0e7c8SDavid Howells 	_leave(" = %d", ret);
4001da177e4SLinus Torvalds 	return ret;
401ec26815aSDavid Howells }
4021da177e4SLinus Torvalds 
40349566f6fSDavid Howells static struct afs_super_info *afs_alloc_sbi(struct afs_mount_params *params)
40449566f6fSDavid Howells {
40549566f6fSDavid Howells 	struct afs_super_info *as;
40649566f6fSDavid Howells 
40749566f6fSDavid Howells 	as = kzalloc(sizeof(struct afs_super_info), GFP_KERNEL);
40849566f6fSDavid Howells 	if (as) {
40949566f6fSDavid Howells 		as->net = afs_get_net(params->net);
41049566f6fSDavid Howells 		as->cell = afs_get_cell(params->cell);
41149566f6fSDavid Howells 	}
41249566f6fSDavid Howells 	return as;
41349566f6fSDavid Howells }
41449566f6fSDavid Howells 
41549566f6fSDavid Howells static void afs_destroy_sbi(struct afs_super_info *as)
41649566f6fSDavid Howells {
41749566f6fSDavid Howells 	if (as) {
4189ed900b1SDavid Howells 		afs_put_volume(as->cell, as->volume);
4199ed900b1SDavid Howells 		afs_put_cell(as->net, as->cell);
42049566f6fSDavid Howells 		afs_put_net(as->net);
42149566f6fSDavid Howells 		kfree(as);
42249566f6fSDavid Howells 	}
42349566f6fSDavid Howells }
42449566f6fSDavid Howells 
4251da177e4SLinus Torvalds /*
4261da177e4SLinus Torvalds  * get an AFS superblock
4271da177e4SLinus Torvalds  */
428f7442b3bSAl Viro static struct dentry *afs_mount(struct file_system_type *fs_type,
429f7442b3bSAl Viro 				int flags, const char *dev_name, void *options)
4301da177e4SLinus Torvalds {
4311da177e4SLinus Torvalds 	struct afs_mount_params params;
4321da177e4SLinus Torvalds 	struct super_block *sb;
433d2ddc776SDavid Howells 	struct afs_volume *candidate;
43400d3b7a4SDavid Howells 	struct key *key;
435dde194a6SAl Viro 	struct afs_super_info *as;
4361da177e4SLinus Torvalds 	int ret;
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds 	_enter(",,%s,%p", dev_name, options);
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	memset(&params, 0, sizeof(params));
441f044c884SDavid Howells 	params.net = &__afs_net;
4421da177e4SLinus Torvalds 
443f74f70f8SEric W. Biederman 	ret = -EINVAL;
444f74f70f8SEric W. Biederman 	if (current->nsproxy->net_ns != &init_net)
445f74f70f8SEric W. Biederman 		goto error;
446f74f70f8SEric W. Biederman 
44700d3b7a4SDavid Howells 	/* parse the options and device name */
4481da177e4SLinus Torvalds 	if (options) {
44900d3b7a4SDavid Howells 		ret = afs_parse_options(&params, options, &dev_name);
4501da177e4SLinus Torvalds 		if (ret < 0)
4511da177e4SLinus Torvalds 			goto error;
4521da177e4SLinus Torvalds 	}
4531da177e4SLinus Torvalds 
45400d3b7a4SDavid Howells 	ret = afs_parse_device_name(&params, dev_name);
45500d3b7a4SDavid Howells 	if (ret < 0)
45600d3b7a4SDavid Howells 		goto error;
45700d3b7a4SDavid Howells 
45800d3b7a4SDavid Howells 	/* try and do the mount securely */
45900d3b7a4SDavid Howells 	key = afs_request_key(params.cell);
46000d3b7a4SDavid Howells 	if (IS_ERR(key)) {
46100d3b7a4SDavid Howells 		_leave(" = %ld [key]", PTR_ERR(key));
46200d3b7a4SDavid Howells 		ret = PTR_ERR(key);
46300d3b7a4SDavid Howells 		goto error;
46400d3b7a4SDavid Howells 	}
46500d3b7a4SDavid Howells 	params.key = key;
46600d3b7a4SDavid Howells 
46749566f6fSDavid Howells 	/* allocate a superblock info record */
46849566f6fSDavid Howells 	ret = -ENOMEM;
46949566f6fSDavid Howells 	as = afs_alloc_sbi(&params);
47049566f6fSDavid Howells 	if (!as)
471d2ddc776SDavid Howells 		goto error_key;
47249566f6fSDavid Howells 
473d2ddc776SDavid Howells 	/* Assume we're going to need a volume record; at the very least we can
474d2ddc776SDavid Howells 	 * use it to update the volume record if we have one already.  This
475d2ddc776SDavid Howells 	 * checks that the volume exists within the cell.
476d2ddc776SDavid Howells 	 */
477d2ddc776SDavid Howells 	candidate = afs_create_volume(&params);
478d2ddc776SDavid Howells 	if (IS_ERR(candidate)) {
479d2ddc776SDavid Howells 		ret = PTR_ERR(candidate);
480d2ddc776SDavid Howells 		goto error_as;
48108e0e7c8SDavid Howells 	}
482d2ddc776SDavid Howells 
483d2ddc776SDavid Howells 	as->volume = candidate;
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	/* allocate a deviceless superblock */
4869249e17fSDavid Howells 	sb = sget(fs_type, afs_test_super, afs_set_super, flags, as);
48708e0e7c8SDavid Howells 	if (IS_ERR(sb)) {
48808e0e7c8SDavid Howells 		ret = PTR_ERR(sb);
489f044c884SDavid Howells 		goto error_as;
49008e0e7c8SDavid Howells 	}
4911da177e4SLinus Torvalds 
492436058a4SDavid Howells 	if (!sb->s_root) {
493436058a4SDavid Howells 		/* initial superblock/root creation */
494436058a4SDavid Howells 		_debug("create");
495436058a4SDavid Howells 		ret = afs_fill_super(sb, &params);
496f044c884SDavid Howells 		if (ret < 0)
497f044c884SDavid Howells 			goto error_sb;
49849566f6fSDavid Howells 		as = NULL;
4991751e8a6SLinus Torvalds 		sb->s_flags |= SB_ACTIVE;
500436058a4SDavid Howells 	} else {
501436058a4SDavid Howells 		_debug("reuse");
5021751e8a6SLinus Torvalds 		ASSERTCMP(sb->s_flags, &, SB_ACTIVE);
50349566f6fSDavid Howells 		afs_destroy_sbi(as);
50449566f6fSDavid Howells 		as = NULL;
505436058a4SDavid Howells 	}
5061da177e4SLinus Torvalds 
5079ed900b1SDavid Howells 	afs_put_cell(params.net, params.cell);
50849566f6fSDavid Howells 	key_put(params.key);
50908e0e7c8SDavid Howells 	_leave(" = 0 [%p]", sb);
510f7442b3bSAl Viro 	return dget(sb->s_root);
5111da177e4SLinus Torvalds 
512f044c884SDavid Howells error_sb:
513f044c884SDavid Howells 	deactivate_locked_super(sb);
514d2ddc776SDavid Howells 	goto error_key;
515f044c884SDavid Howells error_as:
51649566f6fSDavid Howells 	afs_destroy_sbi(as);
517d2ddc776SDavid Howells error_key:
518d2ddc776SDavid Howells 	key_put(params.key);
5191da177e4SLinus Torvalds error:
5209ed900b1SDavid Howells 	afs_put_cell(params.net, params.cell);
5211da177e4SLinus Torvalds 	_leave(" = %d", ret);
522f7442b3bSAl Viro 	return ERR_PTR(ret);
523ec26815aSDavid Howells }
5241da177e4SLinus Torvalds 
525dde194a6SAl Viro static void afs_kill_super(struct super_block *sb)
5261da177e4SLinus Torvalds {
527c435ee34SDavid Howells 	struct afs_super_info *as = AFS_FS_S(sb);
528f044c884SDavid Howells 
529c435ee34SDavid Howells 	/* Clear the callback interests (which will do ilookup5) before
530c435ee34SDavid Howells 	 * deactivating the superblock.
531c435ee34SDavid Howells 	 */
532d2ddc776SDavid Howells 	afs_clear_callback_interests(as->net, as->volume->servers);
533dde194a6SAl Viro 	kill_anon_super(sb);
534d2ddc776SDavid Howells 	afs_deactivate_volume(as->volume);
53549566f6fSDavid Howells 	afs_destroy_sbi(as);
536ec26815aSDavid Howells }
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds /*
539*f8de483eSDavid Howells  * Initialise an inode cache slab element prior to any use.  Note that
540*f8de483eSDavid Howells  * afs_alloc_inode() *must* reset anything that could incorrectly leak from one
541*f8de483eSDavid Howells  * inode to another.
5421da177e4SLinus Torvalds  */
54351cc5068SAlexey Dobriyan static void afs_i_init_once(void *_vnode)
5441da177e4SLinus Torvalds {
545ec26815aSDavid Howells 	struct afs_vnode *vnode = _vnode;
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	memset(vnode, 0, sizeof(*vnode));
5481da177e4SLinus Torvalds 	inode_init_once(&vnode->vfs_inode);
549d2ddc776SDavid Howells 	mutex_init(&vnode->io_lock);
550260a9803SDavid Howells 	mutex_init(&vnode->validate_lock);
5514343d008SDavid Howells 	spin_lock_init(&vnode->wb_lock);
5521da177e4SLinus Torvalds 	spin_lock_init(&vnode->lock);
5534343d008SDavid Howells 	INIT_LIST_HEAD(&vnode->wb_keys);
554e8d6c554SDavid Howells 	INIT_LIST_HEAD(&vnode->pending_locks);
555e8d6c554SDavid Howells 	INIT_LIST_HEAD(&vnode->granted_locks);
556e8d6c554SDavid Howells 	INIT_DELAYED_WORK(&vnode->lock_work, afs_lock_work);
557c435ee34SDavid Howells 	seqlock_init(&vnode->cb_lock);
5581da177e4SLinus Torvalds }
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds /*
5611da177e4SLinus Torvalds  * allocate an AFS inode struct from our slab cache
5621da177e4SLinus Torvalds  */
5631da177e4SLinus Torvalds static struct inode *afs_alloc_inode(struct super_block *sb)
5641da177e4SLinus Torvalds {
5651da177e4SLinus Torvalds 	struct afs_vnode *vnode;
5661da177e4SLinus Torvalds 
567ec26815aSDavid Howells 	vnode = kmem_cache_alloc(afs_inode_cachep, GFP_KERNEL);
5681da177e4SLinus Torvalds 	if (!vnode)
5691da177e4SLinus Torvalds 		return NULL;
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 	atomic_inc(&afs_count_active_inodes);
5721da177e4SLinus Torvalds 
573*f8de483eSDavid Howells 	/* Reset anything that shouldn't leak from one inode to the next. */
5741da177e4SLinus Torvalds 	memset(&vnode->fid, 0, sizeof(vnode->fid));
5751da177e4SLinus Torvalds 	memset(&vnode->status, 0, sizeof(vnode->status));
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 	vnode->volume		= NULL;
578*f8de483eSDavid Howells 	vnode->lock_key		= NULL;
579*f8de483eSDavid Howells 	vnode->permit_cache	= NULL;
580*f8de483eSDavid Howells 	vnode->cb_interest	= NULL;
581*f8de483eSDavid Howells #ifdef CONFIG_AFS_FSCACHE
582*f8de483eSDavid Howells 	vnode->cache		= NULL;
583*f8de483eSDavid Howells #endif
584*f8de483eSDavid Howells 
585260a9803SDavid Howells 	vnode->flags		= 1 << AFS_VNODE_UNSET;
586*f8de483eSDavid Howells 	vnode->cb_type		= 0;
587*f8de483eSDavid Howells 	vnode->lock_state	= AFS_VNODE_LOCK_NONE;
5881da177e4SLinus Torvalds 
5890f300ca9SDavid Howells 	_leave(" = %p", &vnode->vfs_inode);
5901da177e4SLinus Torvalds 	return &vnode->vfs_inode;
591ec26815aSDavid Howells }
5921da177e4SLinus Torvalds 
593fa0d7e3dSNick Piggin static void afs_i_callback(struct rcu_head *head)
594fa0d7e3dSNick Piggin {
595fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
596fa0d7e3dSNick Piggin 	struct afs_vnode *vnode = AFS_FS_I(inode);
597fa0d7e3dSNick Piggin 	kmem_cache_free(afs_inode_cachep, vnode);
598fa0d7e3dSNick Piggin }
599fa0d7e3dSNick Piggin 
6001da177e4SLinus Torvalds /*
6011da177e4SLinus Torvalds  * destroy an AFS inode struct
6021da177e4SLinus Torvalds  */
6031da177e4SLinus Torvalds static void afs_destroy_inode(struct inode *inode)
6041da177e4SLinus Torvalds {
60508e0e7c8SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(inode);
60608e0e7c8SDavid Howells 
6070f300ca9SDavid Howells 	_enter("%p{%x:%u}", inode, vnode->fid.vid, vnode->fid.vnode);
6081da177e4SLinus Torvalds 
60908e0e7c8SDavid Howells 	_debug("DESTROY INODE %p", inode);
61008e0e7c8SDavid Howells 
611c435ee34SDavid Howells 	ASSERTCMP(vnode->cb_interest, ==, NULL);
61208e0e7c8SDavid Howells 
613fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, afs_i_callback);
6141da177e4SLinus Torvalds 	atomic_dec(&afs_count_active_inodes);
615ec26815aSDavid Howells }
61645222b9eSDavid Howells 
61745222b9eSDavid Howells /*
61845222b9eSDavid Howells  * return information about an AFS volume
61945222b9eSDavid Howells  */
62045222b9eSDavid Howells static int afs_statfs(struct dentry *dentry, struct kstatfs *buf)
62145222b9eSDavid Howells {
622d2ddc776SDavid Howells 	struct afs_fs_cursor fc;
62345222b9eSDavid Howells 	struct afs_volume_status vs;
6242b0143b5SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
62545222b9eSDavid Howells 	struct key *key;
62645222b9eSDavid Howells 	int ret;
62745222b9eSDavid Howells 
62845222b9eSDavid Howells 	key = afs_request_key(vnode->volume->cell);
62945222b9eSDavid Howells 	if (IS_ERR(key))
63045222b9eSDavid Howells 		return PTR_ERR(key);
63145222b9eSDavid Howells 
632d2ddc776SDavid Howells 	ret = -ERESTARTSYS;
633d2ddc776SDavid Howells 	if (afs_begin_vnode_operation(&fc, vnode, key)) {
634d2ddc776SDavid Howells 		fc.flags |= AFS_FS_CURSOR_NO_VSLEEP;
635d2ddc776SDavid Howells 		while (afs_select_fileserver(&fc)) {
636d2ddc776SDavid Howells 			fc.cb_break = vnode->cb_break + vnode->cb_s_break;
637d2ddc776SDavid Howells 			afs_fs_get_volume_status(&fc, &vs);
63845222b9eSDavid Howells 		}
63945222b9eSDavid Howells 
640d2ddc776SDavid Howells 		afs_check_for_remote_deletion(&fc, fc.vnode);
641d2ddc776SDavid Howells 		afs_vnode_commit_status(&fc, vnode, fc.cb_break);
642d2ddc776SDavid Howells 		ret = afs_end_vnode_operation(&fc);
643d2ddc776SDavid Howells 	}
644d2ddc776SDavid Howells 
645d2ddc776SDavid Howells 	key_put(key);
646d2ddc776SDavid Howells 
647d2ddc776SDavid Howells 	if (ret == 0) {
64845222b9eSDavid Howells 		buf->f_type	= dentry->d_sb->s_magic;
64945222b9eSDavid Howells 		buf->f_bsize	= AFS_BLOCK_SIZE;
65045222b9eSDavid Howells 		buf->f_namelen	= AFSNAMEMAX - 1;
65145222b9eSDavid Howells 
65245222b9eSDavid Howells 		if (vs.max_quota == 0)
65345222b9eSDavid Howells 			buf->f_blocks = vs.part_max_blocks;
65445222b9eSDavid Howells 		else
65545222b9eSDavid Howells 			buf->f_blocks = vs.max_quota;
65645222b9eSDavid Howells 		buf->f_bavail = buf->f_bfree = buf->f_blocks - vs.blocks_in_use;
657d2ddc776SDavid Howells 	}
658d2ddc776SDavid Howells 
659d2ddc776SDavid Howells 	return ret;
66045222b9eSDavid Howells }
661