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@redhat.com>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/slab.h>
22 #include <linux/pagemap.h>
25 #define AFS_FS_MAGIC 0x6B414653 /* 'kAFS' */
27 struct afs_mount_params
{
29 struct afs_cell
*default_cell
;
30 struct afs_volume
*volume
;
33 static void afs_i_init_once(void *foo
, struct kmem_cache
*cachep
,
36 static int afs_get_sb(struct file_system_type
*fs_type
,
37 int flags
, const char *dev_name
,
38 void *data
, struct vfsmount
*mnt
);
40 static struct inode
*afs_alloc_inode(struct super_block
*sb
);
42 static void afs_put_super(struct super_block
*sb
);
44 static void afs_destroy_inode(struct inode
*inode
);
46 struct file_system_type afs_fs_type
= {
50 .kill_sb
= kill_anon_super
,
51 .fs_flags
= FS_BINARY_MOUNTDATA
,
54 static const struct super_operations afs_super_ops
= {
55 .statfs
= simple_statfs
,
56 .alloc_inode
= afs_alloc_inode
,
57 .drop_inode
= generic_delete_inode
,
58 .destroy_inode
= afs_destroy_inode
,
59 .clear_inode
= afs_clear_inode
,
60 .umount_begin
= afs_umount_begin
,
61 .put_super
= afs_put_super
,
64 static struct kmem_cache
*afs_inode_cachep
;
65 static atomic_t afs_count_active_inodes
;
68 * initialise the filesystem
70 int __init
afs_fs_init(void)
76 /* create ourselves an inode cache */
77 atomic_set(&afs_count_active_inodes
, 0);
80 afs_inode_cachep
= kmem_cache_create("afs_inode_cache",
81 sizeof(struct afs_vnode
),
86 if (!afs_inode_cachep
) {
87 printk(KERN_NOTICE
"kAFS: Failed to allocate inode cache\n");
91 /* now export our filesystem to lesser mortals */
92 ret
= register_filesystem(&afs_fs_type
);
94 kmem_cache_destroy(afs_inode_cachep
);
104 * clean up the filesystem
106 void __exit
afs_fs_exit(void)
110 afs_mntpt_kill_timer();
111 unregister_filesystem(&afs_fs_type
);
113 if (atomic_read(&afs_count_active_inodes
) != 0) {
114 printk("kAFS: %d active inode objects still present\n",
115 atomic_read(&afs_count_active_inodes
));
119 kmem_cache_destroy(afs_inode_cachep
);
124 * check that an argument has a value
126 static int want_arg(char **_value
, const char *option
)
128 if (!_value
|| !*_value
|| !**_value
) {
129 printk(KERN_NOTICE
"kAFS: %s: argument missing\n", option
);
136 * check that there's no subsequent value
138 static int want_no_value(char *const *_value
, const char *option
)
140 if (*_value
&& **_value
) {
141 printk(KERN_NOTICE
"kAFS: %s: Invalid argument: %s\n",
149 * parse the mount options
150 * - this function has been shamelessly adapted from the ext3 fs which
151 * shamelessly adapted it from the msdos fs
153 static int afs_super_parse_options(struct afs_mount_params
*params
,
154 char *options
, const char **devname
)
156 struct afs_cell
*cell
;
160 _enter("%s", options
);
162 options
[PAGE_SIZE
- 1] = 0;
165 while ((key
= strsep(&options
, ","))) {
166 value
= strchr(key
, '=');
170 _debug("kAFS: KEY: %s, VAL:%s", key
, value
?: "-");
172 if (strcmp(key
, "rwpath") == 0) {
173 if (!want_no_value(&value
, "rwpath"))
176 } else if (strcmp(key
, "vol") == 0) {
177 if (!want_arg(&value
, "vol"))
180 } else if (strcmp(key
, "cell") == 0) {
181 if (!want_arg(&value
, "cell"))
183 cell
= afs_cell_lookup(value
, strlen(value
));
185 return PTR_ERR(cell
);
186 afs_put_cell(params
->default_cell
);
187 params
->default_cell
= cell
;
189 printk("kAFS: Unknown mount option: '%s'\n", key
);
197 _leave(" = %d", ret
);
202 * check a superblock to see if it's the one we're looking for
204 static int afs_test_super(struct super_block
*sb
, void *data
)
206 struct afs_mount_params
*params
= data
;
207 struct afs_super_info
*as
= sb
->s_fs_info
;
209 return as
->volume
== params
->volume
;
213 * fill in the superblock
215 static int afs_fill_super(struct super_block
*sb
, void *data
)
217 struct afs_mount_params
*params
= data
;
218 struct afs_super_info
*as
= NULL
;
220 struct dentry
*root
= NULL
;
221 struct inode
*inode
= NULL
;
226 /* allocate a superblock info record */
227 as
= kzalloc(sizeof(struct afs_super_info
), GFP_KERNEL
);
229 _leave(" = -ENOMEM");
233 afs_get_volume(params
->volume
);
234 as
->volume
= params
->volume
;
236 /* fill in the superblock */
237 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
238 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
239 sb
->s_magic
= AFS_FS_MAGIC
;
240 sb
->s_op
= &afs_super_ops
;
243 /* allocate the root inode and dentry */
244 fid
.vid
= as
->volume
->vid
;
247 inode
= afs_iget(sb
, &fid
);
252 root
= d_alloc_root(inode
);
262 ret
= PTR_ERR(inode
);
266 afs_put_volume(as
->volume
);
269 sb
->s_fs_info
= NULL
;
271 _leave(" = %d", ret
);
276 * get an AFS superblock
277 * - TODO: don't use get_sb_nodev(), but rather call sget() directly
279 static int afs_get_sb(struct file_system_type
*fs_type
,
281 const char *dev_name
,
283 struct vfsmount
*mnt
)
285 struct afs_mount_params params
;
286 struct super_block
*sb
;
287 struct afs_volume
*vol
;
290 _enter(",,%s,%p", dev_name
, options
);
292 memset(¶ms
, 0, sizeof(params
));
294 /* parse the options */
296 ret
= afs_super_parse_options(¶ms
, options
, &dev_name
);
300 printk("kAFS: no volume name specified\n");
306 /* parse the device name */
307 vol
= afs_volume_lookup(dev_name
, params
.default_cell
, params
.rwpath
);
315 /* allocate a deviceless superblock */
316 sb
= sget(fs_type
, afs_test_super
, set_anon_super
, ¶ms
);
323 /* initial superblock/root creation */
326 ret
= afs_fill_super(sb
, ¶ms
);
328 up_write(&sb
->s_umount
);
329 deactivate_super(sb
);
332 sb
->s_flags
|= MS_ACTIVE
;
335 ASSERTCMP(sb
->s_flags
, &, MS_ACTIVE
);
338 simple_set_mnt(mnt
, sb
);
339 afs_put_volume(params
.volume
);
340 afs_put_cell(params
.default_cell
);
341 _leave(" = 0 [%p]", sb
);
345 afs_put_volume(params
.volume
);
346 afs_put_cell(params
.default_cell
);
347 _leave(" = %d", ret
);
352 * finish the unmounting process on the superblock
354 static void afs_put_super(struct super_block
*sb
)
356 struct afs_super_info
*as
= sb
->s_fs_info
;
360 afs_put_volume(as
->volume
);
366 * initialise an inode cache slab element prior to any use
368 static void afs_i_init_once(void *_vnode
, struct kmem_cache
*cachep
,
371 struct afs_vnode
*vnode
= _vnode
;
373 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
374 SLAB_CTOR_CONSTRUCTOR
) {
375 memset(vnode
, 0, sizeof(*vnode
));
376 inode_init_once(&vnode
->vfs_inode
);
377 init_waitqueue_head(&vnode
->update_waitq
);
378 spin_lock_init(&vnode
->lock
);
379 INIT_WORK(&vnode
->cb_broken_work
, afs_broken_callback_work
);
380 mutex_init(&vnode
->cb_broken_lock
);
385 * allocate an AFS inode struct from our slab cache
387 static struct inode
*afs_alloc_inode(struct super_block
*sb
)
389 struct afs_vnode
*vnode
;
391 vnode
= kmem_cache_alloc(afs_inode_cachep
, GFP_KERNEL
);
395 atomic_inc(&afs_count_active_inodes
);
397 memset(&vnode
->fid
, 0, sizeof(vnode
->fid
));
398 memset(&vnode
->status
, 0, sizeof(vnode
->status
));
400 vnode
->volume
= NULL
;
401 vnode
->update_cnt
= 0;
403 vnode
->cb_promised
= false;
405 return &vnode
->vfs_inode
;
409 * destroy an AFS inode struct
411 static void afs_destroy_inode(struct inode
*inode
)
413 struct afs_vnode
*vnode
= AFS_FS_I(inode
);
415 _enter("{%lu}", inode
->i_ino
);
417 _debug("DESTROY INODE %p", inode
);
419 ASSERTCMP(vnode
->server
, ==, NULL
);
421 kmem_cache_free(afs_inode_cachep
, vnode
);
422 atomic_dec(&afs_count_active_inodes
);