xref: /openbmc/linux/fs/afs/super.c (revision 677018a6)
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>
28f74f70f8SEric 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);
40677018a6SDavid Howells static int afs_show_devname(struct seq_file *m, struct dentry *root);
41677018a6SDavid Howells static int afs_show_options(struct seq_file *m, struct dentry *root);
421da177e4SLinus Torvalds 
431f5ce9e9STrond Myklebust struct file_system_type afs_fs_type = {
441da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
451da177e4SLinus Torvalds 	.name		= "afs",
46f7442b3bSAl Viro 	.mount		= afs_mount,
47dde194a6SAl Viro 	.kill_sb	= afs_kill_super,
4880c72fe4SDavid Howells 	.fs_flags	= 0,
491da177e4SLinus Torvalds };
507f78e035SEric W. Biederman MODULE_ALIAS_FS("afs");
511da177e4SLinus Torvalds 
52ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations afs_super_ops = {
5345222b9eSDavid Howells 	.statfs		= afs_statfs,
541da177e4SLinus Torvalds 	.alloc_inode	= afs_alloc_inode,
55bec5eb61Swanglei 	.drop_inode	= afs_drop_inode,
561da177e4SLinus Torvalds 	.destroy_inode	= afs_destroy_inode,
57b57922d9SAl Viro 	.evict_inode	= afs_evict_inode,
58677018a6SDavid Howells 	.show_devname	= afs_show_devname,
59677018a6SDavid Howells 	.show_options	= afs_show_options,
601da177e4SLinus Torvalds };
611da177e4SLinus Torvalds 
62e18b890bSChristoph Lameter static struct kmem_cache *afs_inode_cachep;
631da177e4SLinus Torvalds static atomic_t afs_count_active_inodes;
641da177e4SLinus Torvalds 
6580c72fe4SDavid Howells enum {
6680c72fe4SDavid Howells 	afs_no_opt,
6780c72fe4SDavid Howells 	afs_opt_cell,
6880c72fe4SDavid Howells 	afs_opt_rwpath,
6980c72fe4SDavid Howells 	afs_opt_vol,
70bec5eb61Swanglei 	afs_opt_autocell,
7180c72fe4SDavid Howells };
7280c72fe4SDavid Howells 
73a447c093SSteven Whitehouse static const match_table_t afs_options_list = {
7480c72fe4SDavid Howells 	{ afs_opt_cell,		"cell=%s"	},
7580c72fe4SDavid Howells 	{ afs_opt_rwpath,	"rwpath"	},
7680c72fe4SDavid Howells 	{ afs_opt_vol,		"vol=%s"	},
77bec5eb61Swanglei 	{ afs_opt_autocell,	"autocell"	},
7880c72fe4SDavid Howells 	{ afs_no_opt,		NULL		},
7980c72fe4SDavid Howells };
8080c72fe4SDavid Howells 
811da177e4SLinus Torvalds /*
821da177e4SLinus Torvalds  * initialise the filesystem
831da177e4SLinus Torvalds  */
841da177e4SLinus Torvalds int __init afs_fs_init(void)
851da177e4SLinus Torvalds {
861da177e4SLinus Torvalds 	int ret;
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds 	_enter("");
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 	/* create ourselves an inode cache */
911da177e4SLinus Torvalds 	atomic_set(&afs_count_active_inodes, 0);
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 	ret = -ENOMEM;
941da177e4SLinus Torvalds 	afs_inode_cachep = kmem_cache_create("afs_inode_cache",
951da177e4SLinus Torvalds 					     sizeof(struct afs_vnode),
961da177e4SLinus Torvalds 					     0,
975d097056SVladimir Davydov 					     SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT,
9820c2df83SPaul Mundt 					     afs_i_init_once);
991da177e4SLinus Torvalds 	if (!afs_inode_cachep) {
1001da177e4SLinus Torvalds 		printk(KERN_NOTICE "kAFS: Failed to allocate inode cache\n");
1011da177e4SLinus Torvalds 		return ret;
1021da177e4SLinus Torvalds 	}
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 	/* now export our filesystem to lesser mortals */
1051da177e4SLinus Torvalds 	ret = register_filesystem(&afs_fs_type);
1061da177e4SLinus Torvalds 	if (ret < 0) {
1071da177e4SLinus Torvalds 		kmem_cache_destroy(afs_inode_cachep);
10808e0e7c8SDavid Howells 		_leave(" = %d", ret);
1091da177e4SLinus Torvalds 		return ret;
1101da177e4SLinus Torvalds 	}
1111da177e4SLinus Torvalds 
11208e0e7c8SDavid Howells 	_leave(" = 0");
1131da177e4SLinus Torvalds 	return 0;
114ec26815aSDavid Howells }
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds /*
1171da177e4SLinus Torvalds  * clean up the filesystem
1181da177e4SLinus Torvalds  */
1191da177e4SLinus Torvalds void __exit afs_fs_exit(void)
1201da177e4SLinus Torvalds {
12108e0e7c8SDavid Howells 	_enter("");
12208e0e7c8SDavid Howells 
12308e0e7c8SDavid Howells 	afs_mntpt_kill_timer();
1241da177e4SLinus Torvalds 	unregister_filesystem(&afs_fs_type);
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 	if (atomic_read(&afs_count_active_inodes) != 0) {
1271da177e4SLinus Torvalds 		printk("kAFS: %d active inode objects still present\n",
1281da177e4SLinus Torvalds 		       atomic_read(&afs_count_active_inodes));
1291da177e4SLinus Torvalds 		BUG();
1301da177e4SLinus Torvalds 	}
1311da177e4SLinus Torvalds 
1328c0a8537SKirill A. Shutemov 	/*
1338c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
1348c0a8537SKirill A. Shutemov 	 * destroy cache.
1358c0a8537SKirill A. Shutemov 	 */
1368c0a8537SKirill A. Shutemov 	rcu_barrier();
1371da177e4SLinus Torvalds 	kmem_cache_destroy(afs_inode_cachep);
13808e0e7c8SDavid Howells 	_leave("");
139ec26815aSDavid Howells }
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds /*
142677018a6SDavid Howells  * Display the mount device name in /proc/mounts.
143677018a6SDavid Howells  */
144677018a6SDavid Howells static int afs_show_devname(struct seq_file *m, struct dentry *root)
145677018a6SDavid Howells {
146677018a6SDavid Howells 	struct afs_super_info *as = root->d_sb->s_fs_info;
147677018a6SDavid Howells 	struct afs_volume *volume = as->volume;
148677018a6SDavid Howells 	struct afs_cell *cell = volume->cell;
149677018a6SDavid Howells 	const char *suf = "";
150677018a6SDavid Howells 	char pref = '%';
151677018a6SDavid Howells 
152677018a6SDavid Howells 	switch (volume->type) {
153677018a6SDavid Howells 	case AFSVL_RWVOL:
154677018a6SDavid Howells 		break;
155677018a6SDavid Howells 	case AFSVL_ROVOL:
156677018a6SDavid Howells 		pref = '#';
157677018a6SDavid Howells 		if (volume->type_force)
158677018a6SDavid Howells 			suf = ".readonly";
159677018a6SDavid Howells 		break;
160677018a6SDavid Howells 	case AFSVL_BACKVOL:
161677018a6SDavid Howells 		pref = '#';
162677018a6SDavid Howells 		suf = ".backup";
163677018a6SDavid Howells 		break;
164677018a6SDavid Howells 	}
165677018a6SDavid Howells 
166677018a6SDavid Howells 	seq_printf(m, "%c%s:%s%s", pref, cell->name, volume->vlocation->vldb.name, suf);
167677018a6SDavid Howells 	return 0;
168677018a6SDavid Howells }
169677018a6SDavid Howells 
170677018a6SDavid Howells /*
171677018a6SDavid Howells  * Display the mount options in /proc/mounts.
172677018a6SDavid Howells  */
173677018a6SDavid Howells static int afs_show_options(struct seq_file *m, struct dentry *root)
174677018a6SDavid Howells {
175677018a6SDavid Howells 	if (test_bit(AFS_VNODE_AUTOCELL, &AFS_FS_I(d_inode(root))->flags))
176677018a6SDavid Howells 		seq_puts(m, "autocell");
177677018a6SDavid Howells 	return 0;
178677018a6SDavid Howells }
179677018a6SDavid Howells 
180677018a6SDavid Howells /*
1811da177e4SLinus Torvalds  * parse the mount options
1821da177e4SLinus Torvalds  * - this function has been shamelessly adapted from the ext3 fs which
1831da177e4SLinus Torvalds  *   shamelessly adapted it from the msdos fs
1841da177e4SLinus Torvalds  */
18500d3b7a4SDavid Howells static int afs_parse_options(struct afs_mount_params *params,
18608e0e7c8SDavid Howells 			     char *options, const char **devname)
1871da177e4SLinus Torvalds {
18808e0e7c8SDavid Howells 	struct afs_cell *cell;
18980c72fe4SDavid Howells 	substring_t args[MAX_OPT_ARGS];
19080c72fe4SDavid Howells 	char *p;
19180c72fe4SDavid Howells 	int token;
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	_enter("%s", options);
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	options[PAGE_SIZE - 1] = 0;
1961da177e4SLinus Torvalds 
19780c72fe4SDavid Howells 	while ((p = strsep(&options, ","))) {
19880c72fe4SDavid Howells 		if (!*p)
19980c72fe4SDavid Howells 			continue;
2001da177e4SLinus Torvalds 
20180c72fe4SDavid Howells 		token = match_token(p, afs_options_list, args);
20280c72fe4SDavid Howells 		switch (token) {
20380c72fe4SDavid Howells 		case afs_opt_cell:
20480c72fe4SDavid Howells 			cell = afs_cell_lookup(args[0].from,
205bec5eb61Swanglei 					       args[0].to - args[0].from,
206bec5eb61Swanglei 					       false);
20708e0e7c8SDavid Howells 			if (IS_ERR(cell))
20808e0e7c8SDavid Howells 				return PTR_ERR(cell);
20900d3b7a4SDavid Howells 			afs_put_cell(params->cell);
21000d3b7a4SDavid Howells 			params->cell = cell;
21180c72fe4SDavid Howells 			break;
21280c72fe4SDavid Howells 
21380c72fe4SDavid Howells 		case afs_opt_rwpath:
21480c72fe4SDavid Howells 			params->rwpath = 1;
21580c72fe4SDavid Howells 			break;
21680c72fe4SDavid Howells 
21780c72fe4SDavid Howells 		case afs_opt_vol:
21880c72fe4SDavid Howells 			*devname = args[0].from;
21980c72fe4SDavid Howells 			break;
22080c72fe4SDavid Howells 
221bec5eb61Swanglei 		case afs_opt_autocell:
222bec5eb61Swanglei 			params->autocell = 1;
223bec5eb61Swanglei 			break;
224bec5eb61Swanglei 
22580c72fe4SDavid Howells 		default:
22680c72fe4SDavid Howells 			printk(KERN_ERR "kAFS:"
22780c72fe4SDavid Howells 			       " Unknown or invalid mount option: '%s'\n", p);
22880c72fe4SDavid Howells 			return -EINVAL;
2291da177e4SLinus Torvalds 		}
23008e0e7c8SDavid Howells 	}
2311da177e4SLinus Torvalds 
23280c72fe4SDavid Howells 	_leave(" = 0");
23380c72fe4SDavid Howells 	return 0;
234ec26815aSDavid Howells }
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds /*
23700d3b7a4SDavid Howells  * parse a device name to get cell name, volume name, volume type and R/W
23800d3b7a4SDavid Howells  * selector
23900d3b7a4SDavid Howells  * - this can be one of the following:
24000d3b7a4SDavid Howells  *	"%[cell:]volume[.]"		R/W volume
24100d3b7a4SDavid Howells  *	"#[cell:]volume[.]"		R/O or R/W volume (rwpath=0),
24200d3b7a4SDavid Howells  *					 or R/W (rwpath=1) volume
24300d3b7a4SDavid Howells  *	"%[cell:]volume.readonly"	R/O volume
24400d3b7a4SDavid Howells  *	"#[cell:]volume.readonly"	R/O volume
24500d3b7a4SDavid Howells  *	"%[cell:]volume.backup"		Backup volume
24600d3b7a4SDavid Howells  *	"#[cell:]volume.backup"		Backup volume
24700d3b7a4SDavid Howells  */
24800d3b7a4SDavid Howells static int afs_parse_device_name(struct afs_mount_params *params,
24900d3b7a4SDavid Howells 				 const char *name)
25000d3b7a4SDavid Howells {
25100d3b7a4SDavid Howells 	struct afs_cell *cell;
25200d3b7a4SDavid Howells 	const char *cellname, *suffix;
25300d3b7a4SDavid Howells 	int cellnamesz;
25400d3b7a4SDavid Howells 
25500d3b7a4SDavid Howells 	_enter(",%s", name);
25600d3b7a4SDavid Howells 
25700d3b7a4SDavid Howells 	if (!name) {
25800d3b7a4SDavid Howells 		printk(KERN_ERR "kAFS: no volume name specified\n");
25900d3b7a4SDavid Howells 		return -EINVAL;
26000d3b7a4SDavid Howells 	}
26100d3b7a4SDavid Howells 
26200d3b7a4SDavid Howells 	if ((name[0] != '%' && name[0] != '#') || !name[1]) {
26300d3b7a4SDavid Howells 		printk(KERN_ERR "kAFS: unparsable volume name\n");
26400d3b7a4SDavid Howells 		return -EINVAL;
26500d3b7a4SDavid Howells 	}
26600d3b7a4SDavid Howells 
26700d3b7a4SDavid Howells 	/* determine the type of volume we're looking for */
26800d3b7a4SDavid Howells 	params->type = AFSVL_ROVOL;
26900d3b7a4SDavid Howells 	params->force = false;
27000d3b7a4SDavid Howells 	if (params->rwpath || name[0] == '%') {
27100d3b7a4SDavid Howells 		params->type = AFSVL_RWVOL;
27200d3b7a4SDavid Howells 		params->force = true;
27300d3b7a4SDavid Howells 	}
27400d3b7a4SDavid Howells 	name++;
27500d3b7a4SDavid Howells 
27600d3b7a4SDavid Howells 	/* split the cell name out if there is one */
27700d3b7a4SDavid Howells 	params->volname = strchr(name, ':');
27800d3b7a4SDavid Howells 	if (params->volname) {
27900d3b7a4SDavid Howells 		cellname = name;
28000d3b7a4SDavid Howells 		cellnamesz = params->volname - name;
28100d3b7a4SDavid Howells 		params->volname++;
28200d3b7a4SDavid Howells 	} else {
28300d3b7a4SDavid Howells 		params->volname = name;
28400d3b7a4SDavid Howells 		cellname = NULL;
28500d3b7a4SDavid Howells 		cellnamesz = 0;
28600d3b7a4SDavid Howells 	}
28700d3b7a4SDavid Howells 
28800d3b7a4SDavid Howells 	/* the volume type is further affected by a possible suffix */
28900d3b7a4SDavid Howells 	suffix = strrchr(params->volname, '.');
29000d3b7a4SDavid Howells 	if (suffix) {
29100d3b7a4SDavid Howells 		if (strcmp(suffix, ".readonly") == 0) {
29200d3b7a4SDavid Howells 			params->type = AFSVL_ROVOL;
29300d3b7a4SDavid Howells 			params->force = true;
29400d3b7a4SDavid Howells 		} else if (strcmp(suffix, ".backup") == 0) {
29500d3b7a4SDavid Howells 			params->type = AFSVL_BACKVOL;
29600d3b7a4SDavid Howells 			params->force = true;
29700d3b7a4SDavid Howells 		} else if (suffix[1] == 0) {
29800d3b7a4SDavid Howells 		} else {
29900d3b7a4SDavid Howells 			suffix = NULL;
30000d3b7a4SDavid Howells 		}
30100d3b7a4SDavid Howells 	}
30200d3b7a4SDavid Howells 
30300d3b7a4SDavid Howells 	params->volnamesz = suffix ?
30400d3b7a4SDavid Howells 		suffix - params->volname : strlen(params->volname);
30500d3b7a4SDavid Howells 
30600d3b7a4SDavid Howells 	_debug("cell %*.*s [%p]",
30700d3b7a4SDavid Howells 	       cellnamesz, cellnamesz, cellname ?: "", params->cell);
30800d3b7a4SDavid Howells 
30900d3b7a4SDavid Howells 	/* lookup the cell record */
31000d3b7a4SDavid Howells 	if (cellname || !params->cell) {
311bec5eb61Swanglei 		cell = afs_cell_lookup(cellname, cellnamesz, true);
31200d3b7a4SDavid Howells 		if (IS_ERR(cell)) {
313bec5eb61Swanglei 			printk(KERN_ERR "kAFS: unable to lookup cell '%*.*s'\n",
314bec5eb61Swanglei 			       cellnamesz, cellnamesz, cellname ?: "");
31500d3b7a4SDavid Howells 			return PTR_ERR(cell);
31600d3b7a4SDavid Howells 		}
31700d3b7a4SDavid Howells 		afs_put_cell(params->cell);
31800d3b7a4SDavid Howells 		params->cell = cell;
31900d3b7a4SDavid Howells 	}
32000d3b7a4SDavid Howells 
32100d3b7a4SDavid Howells 	_debug("CELL:%s [%p] VOLUME:%*.*s SUFFIX:%s TYPE:%d%s",
32200d3b7a4SDavid Howells 	       params->cell->name, params->cell,
32300d3b7a4SDavid Howells 	       params->volnamesz, params->volnamesz, params->volname,
32400d3b7a4SDavid Howells 	       suffix ?: "-", params->type, params->force ? " FORCE" : "");
32500d3b7a4SDavid Howells 
32600d3b7a4SDavid Howells 	return 0;
32700d3b7a4SDavid Howells }
32800d3b7a4SDavid Howells 
32900d3b7a4SDavid Howells /*
3301da177e4SLinus Torvalds  * check a superblock to see if it's the one we're looking for
3311da177e4SLinus Torvalds  */
3321da177e4SLinus Torvalds static int afs_test_super(struct super_block *sb, void *data)
3331da177e4SLinus Torvalds {
334dde194a6SAl Viro 	struct afs_super_info *as1 = data;
3351da177e4SLinus Torvalds 	struct afs_super_info *as = sb->s_fs_info;
3361da177e4SLinus Torvalds 
337dde194a6SAl Viro 	return as->volume == as1->volume;
338dde194a6SAl Viro }
339dde194a6SAl Viro 
340dde194a6SAl Viro static int afs_set_super(struct super_block *sb, void *data)
341dde194a6SAl Viro {
342dde194a6SAl Viro 	sb->s_fs_info = data;
343dde194a6SAl Viro 	return set_anon_super(sb, NULL);
344ec26815aSDavid Howells }
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds /*
3471da177e4SLinus Torvalds  * fill in the superblock
3481da177e4SLinus Torvalds  */
349dde194a6SAl Viro static int afs_fill_super(struct super_block *sb,
350dde194a6SAl Viro 			  struct afs_mount_params *params)
3511da177e4SLinus Torvalds {
352dde194a6SAl Viro 	struct afs_super_info *as = sb->s_fs_info;
3531da177e4SLinus Torvalds 	struct afs_fid fid;
3541da177e4SLinus Torvalds 	struct inode *inode = NULL;
3551da177e4SLinus Torvalds 	int ret;
3561da177e4SLinus Torvalds 
35708e0e7c8SDavid Howells 	_enter("");
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	/* fill in the superblock */
36009cbfeafSKirill A. Shutemov 	sb->s_blocksize		= PAGE_SIZE;
36109cbfeafSKirill A. Shutemov 	sb->s_blocksize_bits	= PAGE_SHIFT;
3621da177e4SLinus Torvalds 	sb->s_magic		= AFS_FS_MAGIC;
3631da177e4SLinus Torvalds 	sb->s_op		= &afs_super_ops;
364edd3ba94SJan Kara 	ret = super_setup_bdi(sb);
365edd3ba94SJan Kara 	if (ret)
366edd3ba94SJan Kara 		return ret;
367edd3ba94SJan Kara 	sb->s_bdi->ra_pages	= VM_MAX_READAHEAD * 1024 / PAGE_SIZE;
3682e41ae22SDavid Howells 	strlcpy(sb->s_id, as->volume->vlocation->vldb.name, sizeof(sb->s_id));
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	/* allocate the root inode and dentry */
3711da177e4SLinus Torvalds 	fid.vid		= as->volume->vid;
3721da177e4SLinus Torvalds 	fid.vnode	= 1;
3731da177e4SLinus Torvalds 	fid.unique	= 1;
374260a9803SDavid Howells 	inode = afs_iget(sb, params->key, &fid, NULL, NULL);
37508e0e7c8SDavid Howells 	if (IS_ERR(inode))
376dde194a6SAl Viro 		return PTR_ERR(inode);
3771da177e4SLinus Torvalds 
378bec5eb61Swanglei 	if (params->autocell)
379bec5eb61Swanglei 		set_bit(AFS_VNODE_AUTOCELL, &AFS_FS_I(inode)->flags);
380bec5eb61Swanglei 
3811da177e4SLinus Torvalds 	ret = -ENOMEM;
38248fde701SAl Viro 	sb->s_root = d_make_root(inode);
38348fde701SAl Viro 	if (!sb->s_root)
3841da177e4SLinus Torvalds 		goto error;
3851da177e4SLinus Torvalds 
386d61dcce2SAl Viro 	sb->s_d_op = &afs_fs_dentry_operations;
3871da177e4SLinus Torvalds 
38808e0e7c8SDavid Howells 	_leave(" = 0");
3891da177e4SLinus Torvalds 	return 0;
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds error:
39208e0e7c8SDavid Howells 	_leave(" = %d", ret);
3931da177e4SLinus Torvalds 	return ret;
394ec26815aSDavid Howells }
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds /*
3971da177e4SLinus Torvalds  * get an AFS superblock
3981da177e4SLinus Torvalds  */
399f7442b3bSAl Viro static struct dentry *afs_mount(struct file_system_type *fs_type,
400f7442b3bSAl Viro 		      int flags, const char *dev_name, void *options)
4011da177e4SLinus Torvalds {
4021da177e4SLinus Torvalds 	struct afs_mount_params params;
4031da177e4SLinus Torvalds 	struct super_block *sb;
40408e0e7c8SDavid Howells 	struct afs_volume *vol;
40500d3b7a4SDavid Howells 	struct key *key;
406969729d5SMiklos Szeredi 	char *new_opts = kstrdup(options, GFP_KERNEL);
407dde194a6SAl Viro 	struct afs_super_info *as;
4081da177e4SLinus Torvalds 	int ret;
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	_enter(",,%s,%p", dev_name, options);
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	memset(&params, 0, sizeof(params));
4131da177e4SLinus Torvalds 
414f74f70f8SEric W. Biederman 	ret = -EINVAL;
415f74f70f8SEric W. Biederman 	if (current->nsproxy->net_ns != &init_net)
416f74f70f8SEric W. Biederman 		goto error;
417f74f70f8SEric W. Biederman 
41800d3b7a4SDavid Howells 	/* parse the options and device name */
4191da177e4SLinus Torvalds 	if (options) {
42000d3b7a4SDavid Howells 		ret = afs_parse_options(&params, options, &dev_name);
4211da177e4SLinus Torvalds 		if (ret < 0)
4221da177e4SLinus Torvalds 			goto error;
4231da177e4SLinus Torvalds 	}
4241da177e4SLinus Torvalds 
42500d3b7a4SDavid Howells 	ret = afs_parse_device_name(&params, dev_name);
42600d3b7a4SDavid Howells 	if (ret < 0)
42700d3b7a4SDavid Howells 		goto error;
42800d3b7a4SDavid Howells 
42900d3b7a4SDavid Howells 	/* try and do the mount securely */
43000d3b7a4SDavid Howells 	key = afs_request_key(params.cell);
43100d3b7a4SDavid Howells 	if (IS_ERR(key)) {
43200d3b7a4SDavid Howells 		_leave(" = %ld [key]", PTR_ERR(key));
43300d3b7a4SDavid Howells 		ret = PTR_ERR(key);
43400d3b7a4SDavid Howells 		goto error;
43500d3b7a4SDavid Howells 	}
43600d3b7a4SDavid Howells 	params.key = key;
43700d3b7a4SDavid Howells 
4381da177e4SLinus Torvalds 	/* parse the device name */
43900d3b7a4SDavid Howells 	vol = afs_volume_lookup(&params);
44008e0e7c8SDavid Howells 	if (IS_ERR(vol)) {
44108e0e7c8SDavid Howells 		ret = PTR_ERR(vol);
4421da177e4SLinus Torvalds 		goto error;
44308e0e7c8SDavid Howells 	}
444dde194a6SAl Viro 
445dde194a6SAl Viro 	/* allocate a superblock info record */
446dde194a6SAl Viro 	as = kzalloc(sizeof(struct afs_super_info), GFP_KERNEL);
447dde194a6SAl Viro 	if (!as) {
448dde194a6SAl Viro 		ret = -ENOMEM;
449dde194a6SAl Viro 		afs_put_volume(vol);
450dde194a6SAl Viro 		goto error;
451dde194a6SAl Viro 	}
452dde194a6SAl Viro 	as->volume = vol;
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	/* allocate a deviceless superblock */
4559249e17fSDavid Howells 	sb = sget(fs_type, afs_test_super, afs_set_super, flags, as);
45608e0e7c8SDavid Howells 	if (IS_ERR(sb)) {
45708e0e7c8SDavid Howells 		ret = PTR_ERR(sb);
458dde194a6SAl Viro 		afs_put_volume(vol);
459dde194a6SAl Viro 		kfree(as);
4601da177e4SLinus Torvalds 		goto error;
46108e0e7c8SDavid Howells 	}
4621da177e4SLinus Torvalds 
463436058a4SDavid Howells 	if (!sb->s_root) {
464436058a4SDavid Howells 		/* initial superblock/root creation */
465436058a4SDavid Howells 		_debug("create");
466436058a4SDavid Howells 		ret = afs_fill_super(sb, &params);
4671da177e4SLinus Torvalds 		if (ret < 0) {
4686f5bbff9SAl Viro 			deactivate_locked_super(sb);
4691da177e4SLinus Torvalds 			goto error;
4701da177e4SLinus Torvalds 		}
4711da177e4SLinus Torvalds 		sb->s_flags |= MS_ACTIVE;
472436058a4SDavid Howells 	} else {
473436058a4SDavid Howells 		_debug("reuse");
474436058a4SDavid Howells 		ASSERTCMP(sb->s_flags, &, MS_ACTIVE);
475dde194a6SAl Viro 		afs_put_volume(vol);
476dde194a6SAl Viro 		kfree(as);
477436058a4SDavid Howells 	}
4781da177e4SLinus Torvalds 
47900d3b7a4SDavid Howells 	afs_put_cell(params.cell);
4802a32cebdSAl Viro 	kfree(new_opts);
48108e0e7c8SDavid Howells 	_leave(" = 0 [%p]", sb);
482f7442b3bSAl Viro 	return dget(sb->s_root);
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds error:
48500d3b7a4SDavid Howells 	afs_put_cell(params.cell);
48600d3b7a4SDavid Howells 	key_put(params.key);
487969729d5SMiklos Szeredi 	kfree(new_opts);
4881da177e4SLinus Torvalds 	_leave(" = %d", ret);
489f7442b3bSAl Viro 	return ERR_PTR(ret);
490ec26815aSDavid Howells }
4911da177e4SLinus Torvalds 
492dde194a6SAl Viro static void afs_kill_super(struct super_block *sb)
4931da177e4SLinus Torvalds {
4941da177e4SLinus Torvalds 	struct afs_super_info *as = sb->s_fs_info;
495dde194a6SAl Viro 	kill_anon_super(sb);
4961da177e4SLinus Torvalds 	afs_put_volume(as->volume);
497dde194a6SAl Viro 	kfree(as);
498ec26815aSDavid Howells }
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds /*
5011da177e4SLinus Torvalds  * initialise an inode cache slab element prior to any use
5021da177e4SLinus Torvalds  */
50351cc5068SAlexey Dobriyan static void afs_i_init_once(void *_vnode)
5041da177e4SLinus Torvalds {
505ec26815aSDavid Howells 	struct afs_vnode *vnode = _vnode;
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 	memset(vnode, 0, sizeof(*vnode));
5081da177e4SLinus Torvalds 	inode_init_once(&vnode->vfs_inode);
5091da177e4SLinus Torvalds 	init_waitqueue_head(&vnode->update_waitq);
51000d3b7a4SDavid Howells 	mutex_init(&vnode->permits_lock);
511260a9803SDavid Howells 	mutex_init(&vnode->validate_lock);
51231143d5dSDavid Howells 	spin_lock_init(&vnode->writeback_lock);
5131da177e4SLinus Torvalds 	spin_lock_init(&vnode->lock);
51431143d5dSDavid Howells 	INIT_LIST_HEAD(&vnode->writebacks);
515e8d6c554SDavid Howells 	INIT_LIST_HEAD(&vnode->pending_locks);
516e8d6c554SDavid Howells 	INIT_LIST_HEAD(&vnode->granted_locks);
517e8d6c554SDavid Howells 	INIT_DELAYED_WORK(&vnode->lock_work, afs_lock_work);
51808e0e7c8SDavid Howells 	INIT_WORK(&vnode->cb_broken_work, afs_broken_callback_work);
5191da177e4SLinus Torvalds }
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds /*
5221da177e4SLinus Torvalds  * allocate an AFS inode struct from our slab cache
5231da177e4SLinus Torvalds  */
5241da177e4SLinus Torvalds static struct inode *afs_alloc_inode(struct super_block *sb)
5251da177e4SLinus Torvalds {
5261da177e4SLinus Torvalds 	struct afs_vnode *vnode;
5271da177e4SLinus Torvalds 
528ec26815aSDavid Howells 	vnode = kmem_cache_alloc(afs_inode_cachep, GFP_KERNEL);
5291da177e4SLinus Torvalds 	if (!vnode)
5301da177e4SLinus Torvalds 		return NULL;
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds 	atomic_inc(&afs_count_active_inodes);
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 	memset(&vnode->fid, 0, sizeof(vnode->fid));
5351da177e4SLinus Torvalds 	memset(&vnode->status, 0, sizeof(vnode->status));
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds 	vnode->volume		= NULL;
5381da177e4SLinus Torvalds 	vnode->update_cnt	= 0;
539260a9803SDavid Howells 	vnode->flags		= 1 << AFS_VNODE_UNSET;
54008e0e7c8SDavid Howells 	vnode->cb_promised	= false;
5411da177e4SLinus Torvalds 
5420f300ca9SDavid Howells 	_leave(" = %p", &vnode->vfs_inode);
5431da177e4SLinus Torvalds 	return &vnode->vfs_inode;
544ec26815aSDavid Howells }
5451da177e4SLinus Torvalds 
546fa0d7e3dSNick Piggin static void afs_i_callback(struct rcu_head *head)
547fa0d7e3dSNick Piggin {
548fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
549fa0d7e3dSNick Piggin 	struct afs_vnode *vnode = AFS_FS_I(inode);
550fa0d7e3dSNick Piggin 	kmem_cache_free(afs_inode_cachep, vnode);
551fa0d7e3dSNick Piggin }
552fa0d7e3dSNick Piggin 
5531da177e4SLinus Torvalds /*
5541da177e4SLinus Torvalds  * destroy an AFS inode struct
5551da177e4SLinus Torvalds  */
5561da177e4SLinus Torvalds static void afs_destroy_inode(struct inode *inode)
5571da177e4SLinus Torvalds {
55808e0e7c8SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(inode);
55908e0e7c8SDavid Howells 
5600f300ca9SDavid Howells 	_enter("%p{%x:%u}", inode, vnode->fid.vid, vnode->fid.vnode);
5611da177e4SLinus Torvalds 
56208e0e7c8SDavid Howells 	_debug("DESTROY INODE %p", inode);
56308e0e7c8SDavid Howells 
56408e0e7c8SDavid Howells 	ASSERTCMP(vnode->server, ==, NULL);
56508e0e7c8SDavid Howells 
566fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, afs_i_callback);
5671da177e4SLinus Torvalds 	atomic_dec(&afs_count_active_inodes);
568ec26815aSDavid Howells }
56945222b9eSDavid Howells 
57045222b9eSDavid Howells /*
57145222b9eSDavid Howells  * return information about an AFS volume
57245222b9eSDavid Howells  */
57345222b9eSDavid Howells static int afs_statfs(struct dentry *dentry, struct kstatfs *buf)
57445222b9eSDavid Howells {
57545222b9eSDavid Howells 	struct afs_volume_status vs;
5762b0143b5SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
57745222b9eSDavid Howells 	struct key *key;
57845222b9eSDavid Howells 	int ret;
57945222b9eSDavid Howells 
58045222b9eSDavid Howells 	key = afs_request_key(vnode->volume->cell);
58145222b9eSDavid Howells 	if (IS_ERR(key))
58245222b9eSDavid Howells 		return PTR_ERR(key);
58345222b9eSDavid Howells 
58445222b9eSDavid Howells 	ret = afs_vnode_get_volume_status(vnode, key, &vs);
58545222b9eSDavid Howells 	key_put(key);
58645222b9eSDavid Howells 	if (ret < 0) {
58745222b9eSDavid Howells 		_leave(" = %d", ret);
58845222b9eSDavid Howells 		return ret;
58945222b9eSDavid Howells 	}
59045222b9eSDavid Howells 
59145222b9eSDavid Howells 	buf->f_type	= dentry->d_sb->s_magic;
59245222b9eSDavid Howells 	buf->f_bsize	= AFS_BLOCK_SIZE;
59345222b9eSDavid Howells 	buf->f_namelen	= AFSNAMEMAX - 1;
59445222b9eSDavid Howells 
59545222b9eSDavid Howells 	if (vs.max_quota == 0)
59645222b9eSDavid Howells 		buf->f_blocks = vs.part_max_blocks;
59745222b9eSDavid Howells 	else
59845222b9eSDavid Howells 		buf->f_blocks = vs.max_quota;
59945222b9eSDavid Howells 	buf->f_bavail = buf->f_bfree = buf->f_blocks - vs.blocks_in_use;
60045222b9eSDavid Howells 	return 0;
60145222b9eSDavid Howells }
602