xref: /openbmc/linux/fs/afs/super.c (revision 80c72fe4)
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@redhat.com>
14  *
15  */
16 
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/slab.h>
21 #include <linux/fs.h>
22 #include <linux/pagemap.h>
23 #include <linux/parser.h>
24 #include "internal.h"
25 
26 #define AFS_FS_MAGIC 0x6B414653 /* 'kAFS' */
27 
28 static void afs_i_init_once(void *foo, struct kmem_cache *cachep,
29 			    unsigned long flags);
30 
31 static int afs_get_sb(struct file_system_type *fs_type,
32 		      int flags, const char *dev_name,
33 		      void *data, struct vfsmount *mnt);
34 
35 static struct inode *afs_alloc_inode(struct super_block *sb);
36 
37 static void afs_put_super(struct super_block *sb);
38 
39 static void afs_destroy_inode(struct inode *inode);
40 
41 struct file_system_type afs_fs_type = {
42 	.owner		= THIS_MODULE,
43 	.name		= "afs",
44 	.get_sb		= afs_get_sb,
45 	.kill_sb	= kill_anon_super,
46 	.fs_flags	= 0,
47 };
48 
49 static const struct super_operations afs_super_ops = {
50 	.statfs		= simple_statfs,
51 	.alloc_inode	= afs_alloc_inode,
52 	.drop_inode	= generic_delete_inode,
53 	.destroy_inode	= afs_destroy_inode,
54 	.clear_inode	= afs_clear_inode,
55 	.umount_begin	= afs_umount_begin,
56 	.put_super	= afs_put_super,
57 };
58 
59 static struct kmem_cache *afs_inode_cachep;
60 static atomic_t afs_count_active_inodes;
61 
62 enum {
63 	afs_no_opt,
64 	afs_opt_cell,
65 	afs_opt_rwpath,
66 	afs_opt_vol,
67 };
68 
69 static const match_table_t afs_options_list = {
70 	{ afs_opt_cell,		"cell=%s"	},
71 	{ afs_opt_rwpath,	"rwpath"	},
72 	{ afs_opt_vol,		"vol=%s"	},
73 	{ afs_no_opt,		NULL		},
74 };
75 
76 /*
77  * initialise the filesystem
78  */
79 int __init afs_fs_init(void)
80 {
81 	int ret;
82 
83 	_enter("");
84 
85 	/* create ourselves an inode cache */
86 	atomic_set(&afs_count_active_inodes, 0);
87 
88 	ret = -ENOMEM;
89 	afs_inode_cachep = kmem_cache_create("afs_inode_cache",
90 					     sizeof(struct afs_vnode),
91 					     0,
92 					     SLAB_HWCACHE_ALIGN,
93 					     afs_i_init_once,
94 					     NULL);
95 	if (!afs_inode_cachep) {
96 		printk(KERN_NOTICE "kAFS: Failed to allocate inode cache\n");
97 		return ret;
98 	}
99 
100 	/* now export our filesystem to lesser mortals */
101 	ret = register_filesystem(&afs_fs_type);
102 	if (ret < 0) {
103 		kmem_cache_destroy(afs_inode_cachep);
104 		_leave(" = %d", ret);
105 		return ret;
106 	}
107 
108 	_leave(" = 0");
109 	return 0;
110 }
111 
112 /*
113  * clean up the filesystem
114  */
115 void __exit afs_fs_exit(void)
116 {
117 	_enter("");
118 
119 	afs_mntpt_kill_timer();
120 	unregister_filesystem(&afs_fs_type);
121 
122 	if (atomic_read(&afs_count_active_inodes) != 0) {
123 		printk("kAFS: %d active inode objects still present\n",
124 		       atomic_read(&afs_count_active_inodes));
125 		BUG();
126 	}
127 
128 	kmem_cache_destroy(afs_inode_cachep);
129 	_leave("");
130 }
131 
132 /*
133  * parse the mount options
134  * - this function has been shamelessly adapted from the ext3 fs which
135  *   shamelessly adapted it from the msdos fs
136  */
137 static int afs_parse_options(struct afs_mount_params *params,
138 			     char *options, const char **devname)
139 {
140 	struct afs_cell *cell;
141 	substring_t args[MAX_OPT_ARGS];
142 	char *p;
143 	int token;
144 
145 	_enter("%s", options);
146 
147 	options[PAGE_SIZE - 1] = 0;
148 
149 	while ((p = strsep(&options, ","))) {
150 		if (!*p)
151 			continue;
152 
153 		token = match_token(p, afs_options_list, args);
154 		switch (token) {
155 		case afs_opt_cell:
156 			cell = afs_cell_lookup(args[0].from,
157 					       args[0].to - args[0].from);
158 			if (IS_ERR(cell))
159 				return PTR_ERR(cell);
160 			afs_put_cell(params->cell);
161 			params->cell = cell;
162 			break;
163 
164 		case afs_opt_rwpath:
165 			params->rwpath = 1;
166 			break;
167 
168 		case afs_opt_vol:
169 			*devname = args[0].from;
170 			break;
171 
172 		default:
173 			printk(KERN_ERR "kAFS:"
174 			       " Unknown or invalid mount option: '%s'\n", p);
175 			return -EINVAL;
176 		}
177 	}
178 
179 	_leave(" = 0");
180 	return 0;
181 }
182 
183 /*
184  * parse a device name to get cell name, volume name, volume type and R/W
185  * selector
186  * - this can be one of the following:
187  *	"%[cell:]volume[.]"		R/W volume
188  *	"#[cell:]volume[.]"		R/O or R/W volume (rwpath=0),
189  *					 or R/W (rwpath=1) volume
190  *	"%[cell:]volume.readonly"	R/O volume
191  *	"#[cell:]volume.readonly"	R/O volume
192  *	"%[cell:]volume.backup"		Backup volume
193  *	"#[cell:]volume.backup"		Backup volume
194  */
195 static int afs_parse_device_name(struct afs_mount_params *params,
196 				 const char *name)
197 {
198 	struct afs_cell *cell;
199 	const char *cellname, *suffix;
200 	int cellnamesz;
201 
202 	_enter(",%s", name);
203 
204 	if (!name) {
205 		printk(KERN_ERR "kAFS: no volume name specified\n");
206 		return -EINVAL;
207 	}
208 
209 	if ((name[0] != '%' && name[0] != '#') || !name[1]) {
210 		printk(KERN_ERR "kAFS: unparsable volume name\n");
211 		return -EINVAL;
212 	}
213 
214 	/* determine the type of volume we're looking for */
215 	params->type = AFSVL_ROVOL;
216 	params->force = false;
217 	if (params->rwpath || name[0] == '%') {
218 		params->type = AFSVL_RWVOL;
219 		params->force = true;
220 	}
221 	name++;
222 
223 	/* split the cell name out if there is one */
224 	params->volname = strchr(name, ':');
225 	if (params->volname) {
226 		cellname = name;
227 		cellnamesz = params->volname - name;
228 		params->volname++;
229 	} else {
230 		params->volname = name;
231 		cellname = NULL;
232 		cellnamesz = 0;
233 	}
234 
235 	/* the volume type is further affected by a possible suffix */
236 	suffix = strrchr(params->volname, '.');
237 	if (suffix) {
238 		if (strcmp(suffix, ".readonly") == 0) {
239 			params->type = AFSVL_ROVOL;
240 			params->force = true;
241 		} else if (strcmp(suffix, ".backup") == 0) {
242 			params->type = AFSVL_BACKVOL;
243 			params->force = true;
244 		} else if (suffix[1] == 0) {
245 		} else {
246 			suffix = NULL;
247 		}
248 	}
249 
250 	params->volnamesz = suffix ?
251 		suffix - params->volname : strlen(params->volname);
252 
253 	_debug("cell %*.*s [%p]",
254 	       cellnamesz, cellnamesz, cellname ?: "", params->cell);
255 
256 	/* lookup the cell record */
257 	if (cellname || !params->cell) {
258 		cell = afs_cell_lookup(cellname, cellnamesz);
259 		if (IS_ERR(cell)) {
260 			printk(KERN_ERR "kAFS: unable to lookup cell '%s'\n",
261 			       cellname ?: "");
262 			return PTR_ERR(cell);
263 		}
264 		afs_put_cell(params->cell);
265 		params->cell = cell;
266 	}
267 
268 	_debug("CELL:%s [%p] VOLUME:%*.*s SUFFIX:%s TYPE:%d%s",
269 	       params->cell->name, params->cell,
270 	       params->volnamesz, params->volnamesz, params->volname,
271 	       suffix ?: "-", params->type, params->force ? " FORCE" : "");
272 
273 	return 0;
274 }
275 
276 /*
277  * check a superblock to see if it's the one we're looking for
278  */
279 static int afs_test_super(struct super_block *sb, void *data)
280 {
281 	struct afs_mount_params *params = data;
282 	struct afs_super_info *as = sb->s_fs_info;
283 
284 	return as->volume == params->volume;
285 }
286 
287 /*
288  * fill in the superblock
289  */
290 static int afs_fill_super(struct super_block *sb, void *data)
291 {
292 	struct afs_mount_params *params = data;
293 	struct afs_super_info *as = NULL;
294 	struct afs_fid fid;
295 	struct dentry *root = NULL;
296 	struct inode *inode = NULL;
297 	int ret;
298 
299 	_enter("");
300 
301 	/* allocate a superblock info record */
302 	as = kzalloc(sizeof(struct afs_super_info), GFP_KERNEL);
303 	if (!as) {
304 		_leave(" = -ENOMEM");
305 		return -ENOMEM;
306 	}
307 
308 	afs_get_volume(params->volume);
309 	as->volume = params->volume;
310 
311 	/* fill in the superblock */
312 	sb->s_blocksize		= PAGE_CACHE_SIZE;
313 	sb->s_blocksize_bits	= PAGE_CACHE_SHIFT;
314 	sb->s_magic		= AFS_FS_MAGIC;
315 	sb->s_op		= &afs_super_ops;
316 	sb->s_fs_info		= as;
317 
318 	/* allocate the root inode and dentry */
319 	fid.vid		= as->volume->vid;
320 	fid.vnode	= 1;
321 	fid.unique	= 1;
322 	inode = afs_iget(sb, params->key, &fid, NULL, NULL);
323 	if (IS_ERR(inode))
324 		goto error_inode;
325 
326 	ret = -ENOMEM;
327 	root = d_alloc_root(inode);
328 	if (!root)
329 		goto error;
330 
331 	sb->s_root = root;
332 
333 	_leave(" = 0");
334 	return 0;
335 
336 error_inode:
337 	ret = PTR_ERR(inode);
338 	inode = NULL;
339 error:
340 	iput(inode);
341 	afs_put_volume(as->volume);
342 	kfree(as);
343 
344 	sb->s_fs_info = NULL;
345 
346 	_leave(" = %d", ret);
347 	return ret;
348 }
349 
350 /*
351  * get an AFS superblock
352  */
353 static int afs_get_sb(struct file_system_type *fs_type,
354 		      int flags,
355 		      const char *dev_name,
356 		      void *options,
357 		      struct vfsmount *mnt)
358 {
359 	struct afs_mount_params params;
360 	struct super_block *sb;
361 	struct afs_volume *vol;
362 	struct key *key;
363 	int ret;
364 
365 	_enter(",,%s,%p", dev_name, options);
366 
367 	memset(&params, 0, sizeof(params));
368 
369 	/* parse the options and device name */
370 	if (options) {
371 		ret = afs_parse_options(&params, options, &dev_name);
372 		if (ret < 0)
373 			goto error;
374 	}
375 
376 	ret = afs_parse_device_name(&params, dev_name);
377 	if (ret < 0)
378 		goto error;
379 
380 	/* try and do the mount securely */
381 	key = afs_request_key(params.cell);
382 	if (IS_ERR(key)) {
383 		_leave(" = %ld [key]", PTR_ERR(key));
384 		ret = PTR_ERR(key);
385 		goto error;
386 	}
387 	params.key = key;
388 
389 	/* parse the device name */
390 	vol = afs_volume_lookup(&params);
391 	if (IS_ERR(vol)) {
392 		ret = PTR_ERR(vol);
393 		goto error;
394 	}
395 	params.volume = vol;
396 
397 	/* allocate a deviceless superblock */
398 	sb = sget(fs_type, afs_test_super, set_anon_super, &params);
399 	if (IS_ERR(sb)) {
400 		ret = PTR_ERR(sb);
401 		goto error;
402 	}
403 
404 	if (!sb->s_root) {
405 		/* initial superblock/root creation */
406 		_debug("create");
407 		sb->s_flags = flags;
408 		ret = afs_fill_super(sb, &params);
409 		if (ret < 0) {
410 			up_write(&sb->s_umount);
411 			deactivate_super(sb);
412 			goto error;
413 		}
414 		sb->s_flags |= MS_ACTIVE;
415 	} else {
416 		_debug("reuse");
417 		ASSERTCMP(sb->s_flags, &, MS_ACTIVE);
418 	}
419 
420 	simple_set_mnt(mnt, sb);
421 	afs_put_volume(params.volume);
422 	afs_put_cell(params.cell);
423 	_leave(" = 0 [%p]", sb);
424 	return 0;
425 
426 error:
427 	afs_put_volume(params.volume);
428 	afs_put_cell(params.cell);
429 	key_put(params.key);
430 	_leave(" = %d", ret);
431 	return ret;
432 }
433 
434 /*
435  * finish the unmounting process on the superblock
436  */
437 static void afs_put_super(struct super_block *sb)
438 {
439 	struct afs_super_info *as = sb->s_fs_info;
440 
441 	_enter("");
442 
443 	afs_put_volume(as->volume);
444 
445 	_leave("");
446 }
447 
448 /*
449  * initialise an inode cache slab element prior to any use
450  */
451 static void afs_i_init_once(void *_vnode, struct kmem_cache *cachep,
452 			    unsigned long flags)
453 {
454 	struct afs_vnode *vnode = _vnode;
455 
456 	if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
457 	    SLAB_CTOR_CONSTRUCTOR) {
458 		memset(vnode, 0, sizeof(*vnode));
459 		inode_init_once(&vnode->vfs_inode);
460 		init_waitqueue_head(&vnode->update_waitq);
461 		mutex_init(&vnode->permits_lock);
462 		mutex_init(&vnode->validate_lock);
463 		spin_lock_init(&vnode->lock);
464 		INIT_WORK(&vnode->cb_broken_work, afs_broken_callback_work);
465 	}
466 }
467 
468 /*
469  * allocate an AFS inode struct from our slab cache
470  */
471 static struct inode *afs_alloc_inode(struct super_block *sb)
472 {
473 	struct afs_vnode *vnode;
474 
475 	vnode = kmem_cache_alloc(afs_inode_cachep, GFP_KERNEL);
476 	if (!vnode)
477 		return NULL;
478 
479 	atomic_inc(&afs_count_active_inodes);
480 
481 	memset(&vnode->fid, 0, sizeof(vnode->fid));
482 	memset(&vnode->status, 0, sizeof(vnode->status));
483 
484 	vnode->volume		= NULL;
485 	vnode->update_cnt	= 0;
486 	vnode->flags		= 1 << AFS_VNODE_UNSET;
487 	vnode->cb_promised	= false;
488 
489 	return &vnode->vfs_inode;
490 }
491 
492 /*
493  * destroy an AFS inode struct
494  */
495 static void afs_destroy_inode(struct inode *inode)
496 {
497 	struct afs_vnode *vnode = AFS_FS_I(inode);
498 
499 	_enter("{%lu}", inode->i_ino);
500 
501 	_debug("DESTROY INODE %p", inode);
502 
503 	ASSERTCMP(vnode->server, ==, NULL);
504 
505 	kmem_cache_free(afs_inode_cachep, vnode);
506 	atomic_dec(&afs_count_active_inodes);
507 }
508