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