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");
53 static const struct super_operations afs_super_ops
= {
55 .alloc_inode
= afs_alloc_inode
,
56 .drop_inode
= afs_drop_inode
,
57 .destroy_inode
= afs_destroy_inode
,
58 .evict_inode
= afs_evict_inode
,
59 .show_devname
= afs_show_devname
,
60 .show_options
= afs_show_options
,
63 static struct kmem_cache
*afs_inode_cachep
;
64 static atomic_t afs_count_active_inodes
;
75 static const match_table_t afs_options_list
= {
76 { afs_opt_cell
, "cell=%s" },
77 { afs_opt_dyn
, "dyn" },
78 { afs_opt_rwpath
, "rwpath" },
79 { afs_opt_vol
, "vol=%s" },
80 { afs_opt_autocell
, "autocell" },
85 * initialise the filesystem
87 int __init
afs_fs_init(void)
93 /* create ourselves an inode cache */
94 atomic_set(&afs_count_active_inodes
, 0);
97 afs_inode_cachep
= kmem_cache_create("afs_inode_cache",
98 sizeof(struct afs_vnode
),
100 SLAB_HWCACHE_ALIGN
|SLAB_ACCOUNT
,
102 if (!afs_inode_cachep
) {
103 printk(KERN_NOTICE
"kAFS: Failed to allocate inode cache\n");
107 /* now export our filesystem to lesser mortals */
108 ret
= register_filesystem(&afs_fs_type
);
110 kmem_cache_destroy(afs_inode_cachep
);
111 _leave(" = %d", ret
);
120 * clean up the filesystem
122 void afs_fs_exit(void)
126 afs_mntpt_kill_timer();
127 unregister_filesystem(&afs_fs_type
);
129 if (atomic_read(&afs_count_active_inodes
) != 0) {
130 printk("kAFS: %d active inode objects still present\n",
131 atomic_read(&afs_count_active_inodes
));
136 * Make sure all delayed rcu free inodes are flushed before we
140 kmem_cache_destroy(afs_inode_cachep
);
145 * Display the mount device name in /proc/mounts.
147 static int afs_show_devname(struct seq_file
*m
, struct dentry
*root
)
149 struct afs_super_info
*as
= AFS_FS_S(root
->d_sb
);
150 struct afs_volume
*volume
= as
->volume
;
151 struct afs_cell
*cell
= as
->cell
;
152 const char *suf
= "";
160 switch (volume
->type
) {
165 if (volume
->type_force
)
174 seq_printf(m
, "%c%s:%s%s", pref
, cell
->name
, volume
->name
, suf
);
179 * Display the mount options in /proc/mounts.
181 static int afs_show_options(struct seq_file
*m
, struct dentry
*root
)
183 struct afs_super_info
*as
= AFS_FS_S(root
->d_sb
);
187 if (test_bit(AFS_VNODE_AUTOCELL
, &AFS_FS_I(d_inode(root
))->flags
))
188 seq_puts(m
, ",autocell");
193 * parse the mount options
194 * - this function has been shamelessly adapted from the ext3 fs which
195 * shamelessly adapted it from the msdos fs
197 static int afs_parse_options(struct afs_mount_params
*params
,
198 char *options
, const char **devname
)
200 struct afs_cell
*cell
;
201 substring_t args
[MAX_OPT_ARGS
];
205 _enter("%s", options
);
207 options
[PAGE_SIZE
- 1] = 0;
209 while ((p
= strsep(&options
, ","))) {
213 token
= match_token(p
, afs_options_list
, args
);
217 cell
= afs_lookup_cell_rcu(params
->net
,
219 args
[0].to
- args
[0].from
);
222 return PTR_ERR(cell
);
223 afs_put_cell(params
->net
, params
->cell
);
228 params
->rwpath
= true;
232 *devname
= args
[0].from
;
235 case afs_opt_autocell
:
236 params
->autocell
= true;
240 params
->dyn_root
= true;
244 printk(KERN_ERR
"kAFS:"
245 " Unknown or invalid mount option: '%s'\n", p
);
255 * parse a device name to get cell name, volume name, volume type and R/W
257 * - this can be one of the following:
258 * "%[cell:]volume[.]" R/W volume
259 * "#[cell:]volume[.]" R/O or R/W volume (rwpath=0),
260 * or R/W (rwpath=1) volume
261 * "%[cell:]volume.readonly" R/O volume
262 * "#[cell:]volume.readonly" R/O volume
263 * "%[cell:]volume.backup" Backup volume
264 * "#[cell:]volume.backup" Backup volume
266 static int afs_parse_device_name(struct afs_mount_params
*params
,
269 struct afs_cell
*cell
;
270 const char *cellname
, *suffix
;
276 printk(KERN_ERR
"kAFS: no volume name specified\n");
280 if ((name
[0] != '%' && name
[0] != '#') || !name
[1]) {
281 printk(KERN_ERR
"kAFS: unparsable volume name\n");
285 /* determine the type of volume we're looking for */
286 params
->type
= AFSVL_ROVOL
;
287 params
->force
= false;
288 if (params
->rwpath
|| name
[0] == '%') {
289 params
->type
= AFSVL_RWVOL
;
290 params
->force
= true;
294 /* split the cell name out if there is one */
295 params
->volname
= strchr(name
, ':');
296 if (params
->volname
) {
298 cellnamesz
= params
->volname
- name
;
301 params
->volname
= name
;
306 /* the volume type is further affected by a possible suffix */
307 suffix
= strrchr(params
->volname
, '.');
309 if (strcmp(suffix
, ".readonly") == 0) {
310 params
->type
= AFSVL_ROVOL
;
311 params
->force
= true;
312 } else if (strcmp(suffix
, ".backup") == 0) {
313 params
->type
= AFSVL_BACKVOL
;
314 params
->force
= true;
315 } else if (suffix
[1] == 0) {
321 params
->volnamesz
= suffix
?
322 suffix
- params
->volname
: strlen(params
->volname
);
324 _debug("cell %*.*s [%p]",
325 cellnamesz
, cellnamesz
, cellname
?: "", params
->cell
);
327 /* lookup the cell record */
328 if (cellname
|| !params
->cell
) {
329 cell
= afs_lookup_cell(params
->net
, cellname
, cellnamesz
,
332 printk(KERN_ERR
"kAFS: unable to lookup cell '%*.*s'\n",
333 cellnamesz
, cellnamesz
, cellname
?: "");
334 return PTR_ERR(cell
);
336 afs_put_cell(params
->net
, params
->cell
);
340 _debug("CELL:%s [%p] VOLUME:%*.*s SUFFIX:%s TYPE:%d%s",
341 params
->cell
->name
, params
->cell
,
342 params
->volnamesz
, params
->volnamesz
, params
->volname
,
343 suffix
?: "-", params
->type
, params
->force
? " FORCE" : "");
349 * check a superblock to see if it's the one we're looking for
351 static int afs_test_super(struct super_block
*sb
, void *data
)
353 struct afs_super_info
*as1
= data
;
354 struct afs_super_info
*as
= AFS_FS_S(sb
);
356 return (as
->net_ns
== as1
->net_ns
&&
358 as
->volume
->vid
== as1
->volume
->vid
&&
362 static int afs_dynroot_test_super(struct super_block
*sb
, void *data
)
364 struct afs_super_info
*as1
= data
;
365 struct afs_super_info
*as
= AFS_FS_S(sb
);
367 return (as
->net_ns
== as1
->net_ns
&&
371 static int afs_set_super(struct super_block
*sb
, void *data
)
373 struct afs_super_info
*as
= data
;
376 return set_anon_super(sb
, NULL
);
380 * fill in the superblock
382 static int afs_fill_super(struct super_block
*sb
,
383 struct afs_mount_params
*params
)
385 struct afs_super_info
*as
= AFS_FS_S(sb
);
387 struct inode
*inode
= NULL
;
392 /* fill in the superblock */
393 sb
->s_blocksize
= PAGE_SIZE
;
394 sb
->s_blocksize_bits
= PAGE_SHIFT
;
395 sb
->s_magic
= AFS_FS_MAGIC
;
396 sb
->s_op
= &afs_super_ops
;
398 sb
->s_xattr
= afs_xattr_handlers
;
399 ret
= super_setup_bdi(sb
);
402 sb
->s_bdi
->ra_pages
= VM_MAX_READAHEAD
* 1024 / PAGE_SIZE
;
404 /* allocate the root inode and dentry */
406 inode
= afs_iget_pseudo_dir(sb
, true);
407 sb
->s_flags
|= SB_RDONLY
;
409 sprintf(sb
->s_id
, "%u", as
->volume
->vid
);
410 afs_activate_volume(as
->volume
);
411 fid
.vid
= as
->volume
->vid
;
414 inode
= afs_iget(sb
, params
->key
, &fid
, NULL
, NULL
, NULL
);
418 return PTR_ERR(inode
);
420 if (params
->autocell
|| params
->dyn_root
)
421 set_bit(AFS_VNODE_AUTOCELL
, &AFS_FS_I(inode
)->flags
);
424 sb
->s_root
= d_make_root(inode
);
429 sb
->s_d_op
= &afs_dynroot_dentry_operations
;
430 ret
= afs_dynroot_populate(sb
);
434 sb
->s_d_op
= &afs_fs_dentry_operations
;
441 _leave(" = %d", ret
);
445 static struct afs_super_info
*afs_alloc_sbi(struct afs_mount_params
*params
)
447 struct afs_super_info
*as
;
449 as
= kzalloc(sizeof(struct afs_super_info
), GFP_KERNEL
);
451 as
->net_ns
= get_net(params
->net_ns
);
452 if (params
->dyn_root
)
455 as
->cell
= afs_get_cell(params
->cell
);
460 static void afs_destroy_sbi(struct afs_super_info
*as
)
463 afs_put_volume(as
->cell
, as
->volume
);
464 afs_put_cell(afs_net(as
->net_ns
), as
->cell
);
470 static void afs_kill_super(struct super_block
*sb
)
472 struct afs_super_info
*as
= AFS_FS_S(sb
);
473 struct afs_net
*net
= afs_net(as
->net_ns
);
476 afs_dynroot_depopulate(sb
);
478 /* Clear the callback interests (which will do ilookup5) before
479 * deactivating the superblock.
482 afs_clear_callback_interests(net
, as
->volume
->servers
);
485 afs_deactivate_volume(as
->volume
);
490 * get an AFS superblock
492 static struct dentry
*afs_mount(struct file_system_type
*fs_type
,
493 int flags
, const char *dev_name
, void *options
)
495 struct afs_mount_params params
;
496 struct super_block
*sb
;
497 struct afs_volume
*candidate
;
499 struct afs_super_info
*as
;
502 _enter(",,%s,%p", dev_name
, options
);
504 memset(¶ms
, 0, sizeof(params
));
507 if (current
->nsproxy
->net_ns
!= &init_net
)
509 params
.net_ns
= current
->nsproxy
->net_ns
;
510 params
.net
= afs_net(params
.net_ns
);
512 /* parse the options and device name */
514 ret
= afs_parse_options(¶ms
, options
, &dev_name
);
519 if (!params
.dyn_root
) {
520 ret
= afs_parse_device_name(¶ms
, dev_name
);
524 /* try and do the mount securely */
525 key
= afs_request_key(params
.cell
);
527 _leave(" = %ld [key]", PTR_ERR(key
));
534 /* allocate a superblock info record */
536 as
= afs_alloc_sbi(¶ms
);
540 if (!params
.dyn_root
) {
541 /* Assume we're going to need a volume record; at the very
542 * least we can use it to update the volume record if we have
543 * one already. This checks that the volume exists within the
546 candidate
= afs_create_volume(¶ms
);
547 if (IS_ERR(candidate
)) {
548 ret
= PTR_ERR(candidate
);
552 as
->volume
= candidate
;
555 /* allocate a deviceless superblock */
557 as
->dyn_root
? afs_dynroot_test_super
: afs_test_super
,
558 afs_set_super
, flags
, as
);
565 /* initial superblock/root creation */
567 ret
= afs_fill_super(sb
, ¶ms
);
571 sb
->s_flags
|= SB_ACTIVE
;
574 ASSERTCMP(sb
->s_flags
, &, SB_ACTIVE
);
579 afs_put_cell(params
.net
, params
.cell
);
581 _leave(" = 0 [%p]", sb
);
582 return dget(sb
->s_root
);
585 deactivate_locked_super(sb
);
592 afs_put_cell(params
.net
, params
.cell
);
593 _leave(" = %d", ret
);
598 * Initialise an inode cache slab element prior to any use. Note that
599 * afs_alloc_inode() *must* reset anything that could incorrectly leak from one
602 static void afs_i_init_once(void *_vnode
)
604 struct afs_vnode
*vnode
= _vnode
;
606 memset(vnode
, 0, sizeof(*vnode
));
607 inode_init_once(&vnode
->vfs_inode
);
608 mutex_init(&vnode
->io_lock
);
609 init_rwsem(&vnode
->validate_lock
);
610 spin_lock_init(&vnode
->wb_lock
);
611 spin_lock_init(&vnode
->lock
);
612 INIT_LIST_HEAD(&vnode
->wb_keys
);
613 INIT_LIST_HEAD(&vnode
->pending_locks
);
614 INIT_LIST_HEAD(&vnode
->granted_locks
);
615 INIT_DELAYED_WORK(&vnode
->lock_work
, afs_lock_work
);
616 seqlock_init(&vnode
->cb_lock
);
620 * allocate an AFS inode struct from our slab cache
622 static struct inode
*afs_alloc_inode(struct super_block
*sb
)
624 struct afs_vnode
*vnode
;
626 vnode
= kmem_cache_alloc(afs_inode_cachep
, GFP_KERNEL
);
630 atomic_inc(&afs_count_active_inodes
);
632 /* Reset anything that shouldn't leak from one inode to the next. */
633 memset(&vnode
->fid
, 0, sizeof(vnode
->fid
));
634 memset(&vnode
->status
, 0, sizeof(vnode
->status
));
636 vnode
->volume
= NULL
;
637 vnode
->lock_key
= NULL
;
638 vnode
->permit_cache
= NULL
;
639 vnode
->cb_interest
= NULL
;
640 #ifdef CONFIG_AFS_FSCACHE
644 vnode
->flags
= 1 << AFS_VNODE_UNSET
;
646 vnode
->lock_state
= AFS_VNODE_LOCK_NONE
;
648 _leave(" = %p", &vnode
->vfs_inode
);
649 return &vnode
->vfs_inode
;
652 static void afs_i_callback(struct rcu_head
*head
)
654 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
655 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
656 kmem_cache_free(afs_inode_cachep
, vnode
);
660 * destroy an AFS inode struct
662 static void afs_destroy_inode(struct inode
*inode
)
664 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
666 _enter("%p{%x:%u}", inode
, vnode
->fid
.vid
, vnode
->fid
.vnode
);
668 _debug("DESTROY INODE %p", inode
);
670 ASSERTCMP(vnode
->cb_interest
, ==, NULL
);
672 call_rcu(&inode
->i_rcu
, afs_i_callback
);
673 atomic_dec(&afs_count_active_inodes
);
677 * return information about an AFS volume
679 static int afs_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
681 struct afs_super_info
*as
= AFS_FS_S(dentry
->d_sb
);
682 struct afs_fs_cursor fc
;
683 struct afs_volume_status vs
;
684 struct afs_vnode
*vnode
= AFS_FS_I(d_inode(dentry
));
688 buf
->f_type
= dentry
->d_sb
->s_magic
;
689 buf
->f_bsize
= AFS_BLOCK_SIZE
;
690 buf
->f_namelen
= AFSNAMEMAX
- 1;
699 key
= afs_request_key(vnode
->volume
->cell
);
704 if (afs_begin_vnode_operation(&fc
, vnode
, key
)) {
705 fc
.flags
|= AFS_FS_CURSOR_NO_VSLEEP
;
706 while (afs_select_fileserver(&fc
)) {
707 fc
.cb_break
= afs_calc_vnode_cb_break(vnode
);
708 afs_fs_get_volume_status(&fc
, &vs
);
711 afs_check_for_remote_deletion(&fc
, fc
.vnode
);
712 afs_vnode_commit_status(&fc
, vnode
, fc
.cb_break
);
713 ret
= afs_end_vnode_operation(&fc
);
719 if (vs
.max_quota
== 0)
720 buf
->f_blocks
= vs
.part_max_blocks
;
722 buf
->f_blocks
= vs
.max_quota
;
723 buf
->f_bavail
= buf
->f_bfree
= buf
->f_blocks
- vs
.blocks_in_use
;