1 /* AFS superblock handling
3 * Copyright (c) 2002, 2007 Red Hat, Inc. All rights reserved.
5 * This software may be freely redistributed under the terms of the
6 * GNU General Public License.
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.
12 * Authors: David Howells <dhowells@redhat.com>
13 * David Woodhouse <dwmw2@infradead.org>
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>
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 <linux/magic.h>
29 #include <net/net_namespace.h>
32 static void afs_i_init_once(void *foo
);
33 static struct dentry
*afs_mount(struct file_system_type
*fs_type
,
34 int flags
, const char *dev_name
, void *data
);
35 static void afs_kill_super(struct super_block
*sb
);
36 static struct inode
*afs_alloc_inode(struct super_block
*sb
);
37 static void afs_destroy_inode(struct inode
*inode
);
38 static int afs_statfs(struct dentry
*dentry
, struct kstatfs
*buf
);
39 static int afs_show_devname(struct seq_file
*m
, struct dentry
*root
);
40 static int afs_show_options(struct seq_file
*m
, struct dentry
*root
);
42 struct file_system_type afs_fs_type
= {
46 .kill_sb
= afs_kill_super
,
49 MODULE_ALIAS_FS("afs");
51 static const struct super_operations afs_super_ops
= {
53 .alloc_inode
= afs_alloc_inode
,
54 .drop_inode
= afs_drop_inode
,
55 .destroy_inode
= afs_destroy_inode
,
56 .evict_inode
= afs_evict_inode
,
57 .show_devname
= afs_show_devname
,
58 .show_options
= afs_show_options
,
61 static struct kmem_cache
*afs_inode_cachep
;
62 static atomic_t afs_count_active_inodes
;
72 static const match_table_t afs_options_list
= {
73 { afs_opt_cell
, "cell=%s" },
74 { afs_opt_rwpath
, "rwpath" },
75 { afs_opt_vol
, "vol=%s" },
76 { afs_opt_autocell
, "autocell" },
81 * initialise the filesystem
83 int __init
afs_fs_init(void)
89 /* create ourselves an inode cache */
90 atomic_set(&afs_count_active_inodes
, 0);
93 afs_inode_cachep
= kmem_cache_create("afs_inode_cache",
94 sizeof(struct afs_vnode
),
96 SLAB_HWCACHE_ALIGN
|SLAB_ACCOUNT
,
98 if (!afs_inode_cachep
) {
99 printk(KERN_NOTICE
"kAFS: Failed to allocate inode cache\n");
103 /* now export our filesystem to lesser mortals */
104 ret
= register_filesystem(&afs_fs_type
);
106 kmem_cache_destroy(afs_inode_cachep
);
107 _leave(" = %d", ret
);
116 * clean up the filesystem
118 void __exit
afs_fs_exit(void)
122 afs_mntpt_kill_timer();
123 unregister_filesystem(&afs_fs_type
);
125 if (atomic_read(&afs_count_active_inodes
) != 0) {
126 printk("kAFS: %d active inode objects still present\n",
127 atomic_read(&afs_count_active_inodes
));
132 * Make sure all delayed rcu free inodes are flushed before we
136 kmem_cache_destroy(afs_inode_cachep
);
141 * Display the mount device name in /proc/mounts.
143 static int afs_show_devname(struct seq_file
*m
, struct dentry
*root
)
145 struct afs_super_info
*as
= AFS_FS_S(root
->d_sb
);
146 struct afs_volume
*volume
= as
->volume
;
147 struct afs_cell
*cell
= as
->cell
;
148 const char *suf
= "";
151 switch (volume
->type
) {
156 if (volume
->type_force
)
165 seq_printf(m
, "%c%s:%s%s", pref
, cell
->name
, volume
->name
, suf
);
170 * Display the mount options in /proc/mounts.
172 static int afs_show_options(struct seq_file
*m
, struct dentry
*root
)
174 if (test_bit(AFS_VNODE_AUTOCELL
, &AFS_FS_I(d_inode(root
))->flags
))
175 seq_puts(m
, "autocell");
180 * parse the mount options
181 * - this function has been shamelessly adapted from the ext3 fs which
182 * shamelessly adapted it from the msdos fs
184 static int afs_parse_options(struct afs_mount_params
*params
,
185 char *options
, const char **devname
)
187 struct afs_cell
*cell
;
188 substring_t args
[MAX_OPT_ARGS
];
192 _enter("%s", options
);
194 options
[PAGE_SIZE
- 1] = 0;
196 while ((p
= strsep(&options
, ","))) {
200 token
= match_token(p
, afs_options_list
, args
);
204 cell
= afs_lookup_cell_rcu(params
->net
,
206 args
[0].to
- args
[0].from
);
209 return PTR_ERR(cell
);
210 afs_put_cell(params
->net
, params
->cell
);
219 *devname
= args
[0].from
;
222 case afs_opt_autocell
:
223 params
->autocell
= 1;
227 printk(KERN_ERR
"kAFS:"
228 " Unknown or invalid mount option: '%s'\n", p
);
238 * parse a device name to get cell name, volume name, volume type and R/W
240 * - this can be one of the following:
241 * "%[cell:]volume[.]" R/W volume
242 * "#[cell:]volume[.]" R/O or R/W volume (rwpath=0),
243 * or R/W (rwpath=1) volume
244 * "%[cell:]volume.readonly" R/O volume
245 * "#[cell:]volume.readonly" R/O volume
246 * "%[cell:]volume.backup" Backup volume
247 * "#[cell:]volume.backup" Backup volume
249 static int afs_parse_device_name(struct afs_mount_params
*params
,
252 struct afs_cell
*cell
;
253 const char *cellname
, *suffix
;
259 printk(KERN_ERR
"kAFS: no volume name specified\n");
263 if ((name
[0] != '%' && name
[0] != '#') || !name
[1]) {
264 printk(KERN_ERR
"kAFS: unparsable volume name\n");
268 /* determine the type of volume we're looking for */
269 params
->type
= AFSVL_ROVOL
;
270 params
->force
= false;
271 if (params
->rwpath
|| name
[0] == '%') {
272 params
->type
= AFSVL_RWVOL
;
273 params
->force
= true;
277 /* split the cell name out if there is one */
278 params
->volname
= strchr(name
, ':');
279 if (params
->volname
) {
281 cellnamesz
= params
->volname
- name
;
284 params
->volname
= name
;
289 /* the volume type is further affected by a possible suffix */
290 suffix
= strrchr(params
->volname
, '.');
292 if (strcmp(suffix
, ".readonly") == 0) {
293 params
->type
= AFSVL_ROVOL
;
294 params
->force
= true;
295 } else if (strcmp(suffix
, ".backup") == 0) {
296 params
->type
= AFSVL_BACKVOL
;
297 params
->force
= true;
298 } else if (suffix
[1] == 0) {
304 params
->volnamesz
= suffix
?
305 suffix
- params
->volname
: strlen(params
->volname
);
307 _debug("cell %*.*s [%p]",
308 cellnamesz
, cellnamesz
, cellname
?: "", params
->cell
);
310 /* lookup the cell record */
311 if (cellname
|| !params
->cell
) {
312 cell
= afs_lookup_cell(params
->net
, cellname
, cellnamesz
,
315 printk(KERN_ERR
"kAFS: unable to lookup cell '%*.*s'\n",
316 cellnamesz
, cellnamesz
, cellname
?: "");
317 return PTR_ERR(cell
);
319 afs_put_cell(params
->net
, params
->cell
);
323 _debug("CELL:%s [%p] VOLUME:%*.*s SUFFIX:%s TYPE:%d%s",
324 params
->cell
->name
, params
->cell
,
325 params
->volnamesz
, params
->volnamesz
, params
->volname
,
326 suffix
?: "-", params
->type
, params
->force
? " FORCE" : "");
332 * check a superblock to see if it's the one we're looking for
334 static int afs_test_super(struct super_block
*sb
, void *data
)
336 struct afs_super_info
*as1
= data
;
337 struct afs_super_info
*as
= AFS_FS_S(sb
);
339 return as
->net
== as1
->net
&& as
->volume
->vid
== as1
->volume
->vid
;
342 static int afs_set_super(struct super_block
*sb
, void *data
)
344 struct afs_super_info
*as
= data
;
347 return set_anon_super(sb
, NULL
);
351 * fill in the superblock
353 static int afs_fill_super(struct super_block
*sb
,
354 struct afs_mount_params
*params
)
356 struct afs_super_info
*as
= AFS_FS_S(sb
);
358 struct inode
*inode
= NULL
;
363 /* fill in the superblock */
364 sb
->s_blocksize
= PAGE_SIZE
;
365 sb
->s_blocksize_bits
= PAGE_SHIFT
;
366 sb
->s_magic
= AFS_FS_MAGIC
;
367 sb
->s_op
= &afs_super_ops
;
368 sb
->s_xattr
= afs_xattr_handlers
;
369 ret
= super_setup_bdi(sb
);
372 sb
->s_bdi
->ra_pages
= VM_MAX_READAHEAD
* 1024 / PAGE_SIZE
;
373 sprintf(sb
->s_id
, "%u", as
->volume
->vid
);
375 afs_activate_volume(as
->volume
);
377 /* allocate the root inode and dentry */
378 fid
.vid
= as
->volume
->vid
;
381 inode
= afs_iget(sb
, params
->key
, &fid
, NULL
, NULL
, NULL
);
383 return PTR_ERR(inode
);
385 if (params
->autocell
)
386 set_bit(AFS_VNODE_AUTOCELL
, &AFS_FS_I(inode
)->flags
);
389 sb
->s_root
= d_make_root(inode
);
393 sb
->s_d_op
= &afs_fs_dentry_operations
;
399 _leave(" = %d", ret
);
403 static struct afs_super_info
*afs_alloc_sbi(struct afs_mount_params
*params
)
405 struct afs_super_info
*as
;
407 as
= kzalloc(sizeof(struct afs_super_info
), GFP_KERNEL
);
409 as
->net
= afs_get_net(params
->net
);
410 as
->cell
= afs_get_cell(params
->cell
);
415 static void afs_destroy_sbi(struct afs_super_info
*as
)
418 afs_put_volume(as
->cell
, as
->volume
);
419 afs_put_cell(as
->net
, as
->cell
);
420 afs_put_net(as
->net
);
426 * get an AFS superblock
428 static struct dentry
*afs_mount(struct file_system_type
*fs_type
,
429 int flags
, const char *dev_name
, void *options
)
431 struct afs_mount_params params
;
432 struct super_block
*sb
;
433 struct afs_volume
*candidate
;
435 struct afs_super_info
*as
;
438 _enter(",,%s,%p", dev_name
, options
);
440 memset(¶ms
, 0, sizeof(params
));
441 params
.net
= &__afs_net
;
444 if (current
->nsproxy
->net_ns
!= &init_net
)
447 /* parse the options and device name */
449 ret
= afs_parse_options(¶ms
, options
, &dev_name
);
454 ret
= afs_parse_device_name(¶ms
, dev_name
);
458 /* try and do the mount securely */
459 key
= afs_request_key(params
.cell
);
461 _leave(" = %ld [key]", PTR_ERR(key
));
467 /* allocate a superblock info record */
469 as
= afs_alloc_sbi(¶ms
);
473 /* Assume we're going to need a volume record; at the very least we can
474 * use it to update the volume record if we have one already. This
475 * checks that the volume exists within the cell.
477 candidate
= afs_create_volume(¶ms
);
478 if (IS_ERR(candidate
)) {
479 ret
= PTR_ERR(candidate
);
483 as
->volume
= candidate
;
485 /* allocate a deviceless superblock */
486 sb
= sget(fs_type
, afs_test_super
, afs_set_super
, flags
, as
);
493 /* initial superblock/root creation */
495 ret
= afs_fill_super(sb
, ¶ms
);
499 sb
->s_flags
|= SB_ACTIVE
;
502 ASSERTCMP(sb
->s_flags
, &, SB_ACTIVE
);
507 afs_put_cell(params
.net
, params
.cell
);
509 _leave(" = 0 [%p]", sb
);
510 return dget(sb
->s_root
);
513 deactivate_locked_super(sb
);
520 afs_put_cell(params
.net
, params
.cell
);
521 _leave(" = %d", ret
);
525 static void afs_kill_super(struct super_block
*sb
)
527 struct afs_super_info
*as
= AFS_FS_S(sb
);
529 /* Clear the callback interests (which will do ilookup5) before
530 * deactivating the superblock.
532 afs_clear_callback_interests(as
->net
, as
->volume
->servers
);
534 afs_deactivate_volume(as
->volume
);
539 * Initialise an inode cache slab element prior to any use. Note that
540 * afs_alloc_inode() *must* reset anything that could incorrectly leak from one
543 static void afs_i_init_once(void *_vnode
)
545 struct afs_vnode
*vnode
= _vnode
;
547 memset(vnode
, 0, sizeof(*vnode
));
548 inode_init_once(&vnode
->vfs_inode
);
549 mutex_init(&vnode
->io_lock
);
550 mutex_init(&vnode
->validate_lock
);
551 spin_lock_init(&vnode
->wb_lock
);
552 spin_lock_init(&vnode
->lock
);
553 INIT_LIST_HEAD(&vnode
->wb_keys
);
554 INIT_LIST_HEAD(&vnode
->pending_locks
);
555 INIT_LIST_HEAD(&vnode
->granted_locks
);
556 INIT_DELAYED_WORK(&vnode
->lock_work
, afs_lock_work
);
557 seqlock_init(&vnode
->cb_lock
);
561 * allocate an AFS inode struct from our slab cache
563 static struct inode
*afs_alloc_inode(struct super_block
*sb
)
565 struct afs_vnode
*vnode
;
567 vnode
= kmem_cache_alloc(afs_inode_cachep
, GFP_KERNEL
);
571 atomic_inc(&afs_count_active_inodes
);
573 /* Reset anything that shouldn't leak from one inode to the next. */
574 memset(&vnode
->fid
, 0, sizeof(vnode
->fid
));
575 memset(&vnode
->status
, 0, sizeof(vnode
->status
));
577 vnode
->volume
= NULL
;
578 vnode
->lock_key
= NULL
;
579 vnode
->permit_cache
= NULL
;
580 vnode
->cb_interest
= NULL
;
581 #ifdef CONFIG_AFS_FSCACHE
585 vnode
->flags
= 1 << AFS_VNODE_UNSET
;
587 vnode
->lock_state
= AFS_VNODE_LOCK_NONE
;
589 _leave(" = %p", &vnode
->vfs_inode
);
590 return &vnode
->vfs_inode
;
593 static void afs_i_callback(struct rcu_head
*head
)
595 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
596 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
597 kmem_cache_free(afs_inode_cachep
, vnode
);
601 * destroy an AFS inode struct
603 static void afs_destroy_inode(struct inode
*inode
)
605 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
607 _enter("%p{%x:%u}", inode
, vnode
->fid
.vid
, vnode
->fid
.vnode
);
609 _debug("DESTROY INODE %p", inode
);
611 ASSERTCMP(vnode
->cb_interest
, ==, NULL
);
613 call_rcu(&inode
->i_rcu
, afs_i_callback
);
614 atomic_dec(&afs_count_active_inodes
);
618 * return information about an AFS volume
620 static int afs_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
622 struct afs_fs_cursor fc
;
623 struct afs_volume_status vs
;
624 struct afs_vnode
*vnode
= AFS_FS_I(d_inode(dentry
));
628 key
= afs_request_key(vnode
->volume
->cell
);
633 if (afs_begin_vnode_operation(&fc
, vnode
, key
)) {
634 fc
.flags
|= AFS_FS_CURSOR_NO_VSLEEP
;
635 while (afs_select_fileserver(&fc
)) {
636 fc
.cb_break
= vnode
->cb_break
+ vnode
->cb_s_break
;
637 afs_fs_get_volume_status(&fc
, &vs
);
640 afs_check_for_remote_deletion(&fc
, fc
.vnode
);
641 afs_vnode_commit_status(&fc
, vnode
, fc
.cb_break
);
642 ret
= afs_end_vnode_operation(&fc
);
648 buf
->f_type
= dentry
->d_sb
->s_magic
;
649 buf
->f_bsize
= AFS_BLOCK_SIZE
;
650 buf
->f_namelen
= AFSNAMEMAX
- 1;
652 if (vs
.max_quota
== 0)
653 buf
->f_blocks
= vs
.part_max_blocks
;
655 buf
->f_blocks
= vs
.max_quota
;
656 buf
->f_bavail
= buf
->f_bfree
= buf
->f_blocks
- vs
.blocks_in_use
;