1/* AFS superblock handling
2 *
3 * Copyright (c) 2002, 2007 Red Hat, Inc. All rights reserved.
4 *
5 * This software may be freely redistributed under the terms of the
6 * GNU General Public License.
7 *
8 * You should have received a copy of the GNU General Public License
9 * along with this program; if not, write to the Free Software
10 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
11 *
12 * Authors: David Howells <dhowells@redhat.com>
13 *          David Woodhouse <dwmw2@infradead.org>
14 *
15 */
16
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/mount.h>
20#include <linux/init.h>
21#include <linux/slab.h>
22#include <linux/fs.h>
23#include <linux/pagemap.h>
24#include <linux/parser.h>
25#include <linux/statfs.h>
26#include <linux/sched.h>
27#include <linux/nsproxy.h>
28#include <net/net_namespace.h>
29#include "internal.h"
30
31#define AFS_FS_MAGIC 0x6B414653 /* 'kAFS' */
32
33static void afs_i_init_once(void *foo);
34static struct dentry *afs_mount(struct file_system_type *fs_type,
35		      int flags, const char *dev_name, void *data);
36static void afs_kill_super(struct super_block *sb);
37static struct inode *afs_alloc_inode(struct super_block *sb);
38static void afs_destroy_inode(struct inode *inode);
39static int afs_statfs(struct dentry *dentry, struct kstatfs *buf);
40
41struct file_system_type afs_fs_type = {
42	.owner		= THIS_MODULE,
43	.name		= "afs",
44	.mount		= afs_mount,
45	.kill_sb	= afs_kill_super,
46	.fs_flags	= 0,
47};
48MODULE_ALIAS_FS("afs");
49
50static const struct super_operations afs_super_ops = {
51	.statfs		= afs_statfs,
52	.alloc_inode	= afs_alloc_inode,
53	.drop_inode	= afs_drop_inode,
54	.destroy_inode	= afs_destroy_inode,
55	.evict_inode	= afs_evict_inode,
56	.show_options	= generic_show_options,
57};
58
59static struct kmem_cache *afs_inode_cachep;
60static atomic_t afs_count_active_inodes;
61
62enum {
63	afs_no_opt,
64	afs_opt_cell,
65	afs_opt_rwpath,
66	afs_opt_vol,
67	afs_opt_autocell,
68};
69
70static const match_table_t afs_options_list = {
71	{ afs_opt_cell,		"cell=%s"	},
72	{ afs_opt_rwpath,	"rwpath"	},
73	{ afs_opt_vol,		"vol=%s"	},
74	{ afs_opt_autocell,	"autocell"	},
75	{ afs_no_opt,		NULL		},
76};
77
78/*
79 * initialise the filesystem
80 */
81int __init afs_fs_init(void)
82{
83	int ret;
84
85	_enter("");
86
87	/* create ourselves an inode cache */
88	atomic_set(&afs_count_active_inodes, 0);
89
90	ret = -ENOMEM;
91	afs_inode_cachep = kmem_cache_create("afs_inode_cache",
92					     sizeof(struct afs_vnode),
93					     0,
94					     SLAB_HWCACHE_ALIGN,
95					     afs_i_init_once);
96	if (!afs_inode_cachep) {
97		printk(KERN_NOTICE "kAFS: Failed to allocate inode cache\n");
98		return ret;
99	}
100
101	/* now export our filesystem to lesser mortals */
102	ret = register_filesystem(&afs_fs_type);
103	if (ret < 0) {
104		kmem_cache_destroy(afs_inode_cachep);
105		_leave(" = %d", ret);
106		return ret;
107	}
108
109	_leave(" = 0");
110	return 0;
111}
112
113/*
114 * clean up the filesystem
115 */
116void __exit afs_fs_exit(void)
117{
118	_enter("");
119
120	afs_mntpt_kill_timer();
121	unregister_filesystem(&afs_fs_type);
122
123	if (atomic_read(&afs_count_active_inodes) != 0) {
124		printk("kAFS: %d active inode objects still present\n",
125		       atomic_read(&afs_count_active_inodes));
126		BUG();
127	}
128
129	/*
130	 * Make sure all delayed rcu free inodes are flushed before we
131	 * destroy cache.
132	 */
133	rcu_barrier();
134	kmem_cache_destroy(afs_inode_cachep);
135	_leave("");
136}
137
138/*
139 * parse the mount options
140 * - this function has been shamelessly adapted from the ext3 fs which
141 *   shamelessly adapted it from the msdos fs
142 */
143static int afs_parse_options(struct afs_mount_params *params,
144			     char *options, const char **devname)
145{
146	struct afs_cell *cell;
147	substring_t args[MAX_OPT_ARGS];
148	char *p;
149	int token;
150
151	_enter("%s", options);
152
153	options[PAGE_SIZE - 1] = 0;
154
155	while ((p = strsep(&options, ","))) {
156		if (!*p)
157			continue;
158
159		token = match_token(p, afs_options_list, args);
160		switch (token) {
161		case afs_opt_cell:
162			cell = afs_cell_lookup(args[0].from,
163					       args[0].to - args[0].from,
164					       false);
165			if (IS_ERR(cell))
166				return PTR_ERR(cell);
167			afs_put_cell(params->cell);
168			params->cell = cell;
169			break;
170
171		case afs_opt_rwpath:
172			params->rwpath = 1;
173			break;
174
175		case afs_opt_vol:
176			*devname = args[0].from;
177			break;
178
179		case afs_opt_autocell:
180			params->autocell = 1;
181			break;
182
183		default:
184			printk(KERN_ERR "kAFS:"
185			       " Unknown or invalid mount option: '%s'\n", p);
186			return -EINVAL;
187		}
188	}
189
190	_leave(" = 0");
191	return 0;
192}
193
194/*
195 * parse a device name to get cell name, volume name, volume type and R/W
196 * selector
197 * - this can be one of the following:
198 *	"%[cell:]volume[.]"		R/W volume
199 *	"#[cell:]volume[.]"		R/O or R/W volume (rwpath=0),
200 *					 or R/W (rwpath=1) volume
201 *	"%[cell:]volume.readonly"	R/O volume
202 *	"#[cell:]volume.readonly"	R/O volume
203 *	"%[cell:]volume.backup"		Backup volume
204 *	"#[cell:]volume.backup"		Backup volume
205 */
206static int afs_parse_device_name(struct afs_mount_params *params,
207				 const char *name)
208{
209	struct afs_cell *cell;
210	const char *cellname, *suffix;
211	int cellnamesz;
212
213	_enter(",%s", name);
214
215	if (!name) {
216		printk(KERN_ERR "kAFS: no volume name specified\n");
217		return -EINVAL;
218	}
219
220	if ((name[0] != '%' && name[0] != '#') || !name[1]) {
221		printk(KERN_ERR "kAFS: unparsable volume name\n");
222		return -EINVAL;
223	}
224
225	/* determine the type of volume we're looking for */
226	params->type = AFSVL_ROVOL;
227	params->force = false;
228	if (params->rwpath || name[0] == '%') {
229		params->type = AFSVL_RWVOL;
230		params->force = true;
231	}
232	name++;
233
234	/* split the cell name out if there is one */
235	params->volname = strchr(name, ':');
236	if (params->volname) {
237		cellname = name;
238		cellnamesz = params->volname - name;
239		params->volname++;
240	} else {
241		params->volname = name;
242		cellname = NULL;
243		cellnamesz = 0;
244	}
245
246	/* the volume type is further affected by a possible suffix */
247	suffix = strrchr(params->volname, '.');
248	if (suffix) {
249		if (strcmp(suffix, ".readonly") == 0) {
250			params->type = AFSVL_ROVOL;
251			params->force = true;
252		} else if (strcmp(suffix, ".backup") == 0) {
253			params->type = AFSVL_BACKVOL;
254			params->force = true;
255		} else if (suffix[1] == 0) {
256		} else {
257			suffix = NULL;
258		}
259	}
260
261	params->volnamesz = suffix ?
262		suffix - params->volname : strlen(params->volname);
263
264	_debug("cell %*.*s [%p]",
265	       cellnamesz, cellnamesz, cellname ?: "", params->cell);
266
267	/* lookup the cell record */
268	if (cellname || !params->cell) {
269		cell = afs_cell_lookup(cellname, cellnamesz, true);
270		if (IS_ERR(cell)) {
271			printk(KERN_ERR "kAFS: unable to lookup cell '%*.*s'\n",
272			       cellnamesz, cellnamesz, cellname ?: "");
273			return PTR_ERR(cell);
274		}
275		afs_put_cell(params->cell);
276		params->cell = cell;
277	}
278
279	_debug("CELL:%s [%p] VOLUME:%*.*s SUFFIX:%s TYPE:%d%s",
280	       params->cell->name, params->cell,
281	       params->volnamesz, params->volnamesz, params->volname,
282	       suffix ?: "-", params->type, params->force ? " FORCE" : "");
283
284	return 0;
285}
286
287/*
288 * check a superblock to see if it's the one we're looking for
289 */
290static int afs_test_super(struct super_block *sb, void *data)
291{
292	struct afs_super_info *as1 = data;
293	struct afs_super_info *as = sb->s_fs_info;
294
295	return as->volume == as1->volume;
296}
297
298static int afs_set_super(struct super_block *sb, void *data)
299{
300	sb->s_fs_info = data;
301	return set_anon_super(sb, NULL);
302}
303
304/*
305 * fill in the superblock
306 */
307static int afs_fill_super(struct super_block *sb,
308			  struct afs_mount_params *params)
309{
310	struct afs_super_info *as = sb->s_fs_info;
311	struct afs_fid fid;
312	struct inode *inode = NULL;
313	int ret;
314
315	_enter("");
316
317	/* fill in the superblock */
318	sb->s_blocksize		= PAGE_CACHE_SIZE;
319	sb->s_blocksize_bits	= PAGE_CACHE_SHIFT;
320	sb->s_magic		= AFS_FS_MAGIC;
321	sb->s_op		= &afs_super_ops;
322	sb->s_bdi		= &as->volume->bdi;
323	strlcpy(sb->s_id, as->volume->vlocation->vldb.name, sizeof(sb->s_id));
324
325	/* allocate the root inode and dentry */
326	fid.vid		= as->volume->vid;
327	fid.vnode	= 1;
328	fid.unique	= 1;
329	inode = afs_iget(sb, params->key, &fid, NULL, NULL);
330	if (IS_ERR(inode))
331		return PTR_ERR(inode);
332
333	if (params->autocell)
334		set_bit(AFS_VNODE_AUTOCELL, &AFS_FS_I(inode)->flags);
335
336	ret = -ENOMEM;
337	sb->s_root = d_make_root(inode);
338	if (!sb->s_root)
339		goto error;
340
341	sb->s_d_op = &afs_fs_dentry_operations;
342
343	_leave(" = 0");
344	return 0;
345
346error:
347	_leave(" = %d", ret);
348	return ret;
349}
350
351/*
352 * get an AFS superblock
353 */
354static struct dentry *afs_mount(struct file_system_type *fs_type,
355		      int flags, const char *dev_name, void *options)
356{
357	struct afs_mount_params params;
358	struct super_block *sb;
359	struct afs_volume *vol;
360	struct key *key;
361	char *new_opts = kstrdup(options, GFP_KERNEL);
362	struct afs_super_info *as;
363	int ret;
364
365	_enter(",,%s,%p", dev_name, options);
366
367	memset(&params, 0, sizeof(params));
368
369	ret = -EINVAL;
370	if (current->nsproxy->net_ns != &init_net)
371		goto error;
372
373	/* parse the options and device name */
374	if (options) {
375		ret = afs_parse_options(&params, options, &dev_name);
376		if (ret < 0)
377			goto error;
378	}
379
380	ret = afs_parse_device_name(&params, dev_name);
381	if (ret < 0)
382		goto error;
383
384	/* try and do the mount securely */
385	key = afs_request_key(params.cell);
386	if (IS_ERR(key)) {
387		_leave(" = %ld [key]", PTR_ERR(key));
388		ret = PTR_ERR(key);
389		goto error;
390	}
391	params.key = key;
392
393	/* parse the device name */
394	vol = afs_volume_lookup(&params);
395	if (IS_ERR(vol)) {
396		ret = PTR_ERR(vol);
397		goto error;
398	}
399
400	/* allocate a superblock info record */
401	as = kzalloc(sizeof(struct afs_super_info), GFP_KERNEL);
402	if (!as) {
403		ret = -ENOMEM;
404		afs_put_volume(vol);
405		goto error;
406	}
407	as->volume = vol;
408
409	/* allocate a deviceless superblock */
410	sb = sget(fs_type, afs_test_super, afs_set_super, flags, as);
411	if (IS_ERR(sb)) {
412		ret = PTR_ERR(sb);
413		afs_put_volume(vol);
414		kfree(as);
415		goto error;
416	}
417
418	if (!sb->s_root) {
419		/* initial superblock/root creation */
420		_debug("create");
421		ret = afs_fill_super(sb, &params);
422		if (ret < 0) {
423			deactivate_locked_super(sb);
424			goto error;
425		}
426		save_mount_options(sb, new_opts);
427		sb->s_flags |= MS_ACTIVE;
428	} else {
429		_debug("reuse");
430		ASSERTCMP(sb->s_flags, &, MS_ACTIVE);
431		afs_put_volume(vol);
432		kfree(as);
433	}
434
435	afs_put_cell(params.cell);
436	kfree(new_opts);
437	_leave(" = 0 [%p]", sb);
438	return dget(sb->s_root);
439
440error:
441	afs_put_cell(params.cell);
442	key_put(params.key);
443	kfree(new_opts);
444	_leave(" = %d", ret);
445	return ERR_PTR(ret);
446}
447
448static void afs_kill_super(struct super_block *sb)
449{
450	struct afs_super_info *as = sb->s_fs_info;
451	kill_anon_super(sb);
452	afs_put_volume(as->volume);
453	kfree(as);
454}
455
456/*
457 * initialise an inode cache slab element prior to any use
458 */
459static void afs_i_init_once(void *_vnode)
460{
461	struct afs_vnode *vnode = _vnode;
462
463	memset(vnode, 0, sizeof(*vnode));
464	inode_init_once(&vnode->vfs_inode);
465	init_waitqueue_head(&vnode->update_waitq);
466	mutex_init(&vnode->permits_lock);
467	mutex_init(&vnode->validate_lock);
468	spin_lock_init(&vnode->writeback_lock);
469	spin_lock_init(&vnode->lock);
470	INIT_LIST_HEAD(&vnode->writebacks);
471	INIT_LIST_HEAD(&vnode->pending_locks);
472	INIT_LIST_HEAD(&vnode->granted_locks);
473	INIT_DELAYED_WORK(&vnode->lock_work, afs_lock_work);
474	INIT_WORK(&vnode->cb_broken_work, afs_broken_callback_work);
475}
476
477/*
478 * allocate an AFS inode struct from our slab cache
479 */
480static struct inode *afs_alloc_inode(struct super_block *sb)
481{
482	struct afs_vnode *vnode;
483
484	vnode = kmem_cache_alloc(afs_inode_cachep, GFP_KERNEL);
485	if (!vnode)
486		return NULL;
487
488	atomic_inc(&afs_count_active_inodes);
489
490	memset(&vnode->fid, 0, sizeof(vnode->fid));
491	memset(&vnode->status, 0, sizeof(vnode->status));
492
493	vnode->volume		= NULL;
494	vnode->update_cnt	= 0;
495	vnode->flags		= 1 << AFS_VNODE_UNSET;
496	vnode->cb_promised	= false;
497
498	_leave(" = %p", &vnode->vfs_inode);
499	return &vnode->vfs_inode;
500}
501
502static void afs_i_callback(struct rcu_head *head)
503{
504	struct inode *inode = container_of(head, struct inode, i_rcu);
505	struct afs_vnode *vnode = AFS_FS_I(inode);
506	kmem_cache_free(afs_inode_cachep, vnode);
507}
508
509/*
510 * destroy an AFS inode struct
511 */
512static void afs_destroy_inode(struct inode *inode)
513{
514	struct afs_vnode *vnode = AFS_FS_I(inode);
515
516	_enter("%p{%x:%u}", inode, vnode->fid.vid, vnode->fid.vnode);
517
518	_debug("DESTROY INODE %p", inode);
519
520	ASSERTCMP(vnode->server, ==, NULL);
521
522	call_rcu(&inode->i_rcu, afs_i_callback);
523	atomic_dec(&afs_count_active_inodes);
524}
525
526/*
527 * return information about an AFS volume
528 */
529static int afs_statfs(struct dentry *dentry, struct kstatfs *buf)
530{
531	struct afs_volume_status vs;
532	struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
533	struct key *key;
534	int ret;
535
536	key = afs_request_key(vnode->volume->cell);
537	if (IS_ERR(key))
538		return PTR_ERR(key);
539
540	ret = afs_vnode_get_volume_status(vnode, key, &vs);
541	key_put(key);
542	if (ret < 0) {
543		_leave(" = %d", ret);
544		return ret;
545	}
546
547	buf->f_type	= dentry->d_sb->s_magic;
548	buf->f_bsize	= AFS_BLOCK_SIZE;
549	buf->f_namelen	= AFSNAMEMAX - 1;
550
551	if (vs.max_quota == 0)
552		buf->f_blocks = vs.part_max_blocks;
553	else
554		buf->f_blocks = vs.max_quota;
555	buf->f_bavail = buf->f_bfree = buf->f_blocks - vs.blocks_in_use;
556	return 0;
557}
558