1 /* AFS superblock handling
3 * Copyright (c) 2002 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>
26 #include "cmservice.h"
31 #define AFS_FS_MAGIC 0x6B414653 /* 'kAFS' */
33 struct afs_mount_params {
35 struct afs_cell *default_cell;
36 struct afs_volume *volume;
39 static void afs_i_init_once(void *foo, struct kmem_cache *cachep,
42 static int afs_get_sb(struct file_system_type *fs_type,
43 int flags, const char *dev_name,
44 void *data, struct vfsmount *mnt);
46 static struct inode *afs_alloc_inode(struct super_block *sb);
48 static void afs_put_super(struct super_block *sb);
50 static void afs_destroy_inode(struct inode *inode);
52 struct file_system_type afs_fs_type = {
56 .kill_sb = kill_anon_super,
57 .fs_flags = FS_BINARY_MOUNTDATA,
60 static const struct super_operations afs_super_ops = {
61 .statfs = simple_statfs,
62 .alloc_inode = afs_alloc_inode,
63 .drop_inode = generic_delete_inode,
64 .destroy_inode = afs_destroy_inode,
65 .clear_inode = afs_clear_inode,
66 .put_super = afs_put_super,
69 static struct kmem_cache *afs_inode_cachep;
70 static atomic_t afs_count_active_inodes;
73 * initialise the filesystem
75 int __init afs_fs_init(void)
81 afs_timer_init(&afs_mntpt_expiry_timer, &afs_mntpt_expiry_timer_ops);
83 /* create ourselves an inode cache */
84 atomic_set(&afs_count_active_inodes, 0);
87 afs_inode_cachep = kmem_cache_create("afs_inode_cache",
88 sizeof(struct afs_vnode),
93 if (!afs_inode_cachep) {
94 printk(KERN_NOTICE "kAFS: Failed to allocate inode cache\n");
98 /* now export our filesystem to lesser mortals */
99 ret = register_filesystem(&afs_fs_type);
101 kmem_cache_destroy(afs_inode_cachep);
102 kleave(" = %d", ret);
111 * clean up the filesystem
113 void __exit afs_fs_exit(void)
115 unregister_filesystem(&afs_fs_type);
117 if (atomic_read(&afs_count_active_inodes) != 0) {
118 printk("kAFS: %d active inode objects still present\n",
119 atomic_read(&afs_count_active_inodes));
123 kmem_cache_destroy(afs_inode_cachep);
127 * check that an argument has a value
129 static int want_arg(char **_value, const char *option)
131 if (!_value || !*_value || !**_value) {
132 printk(KERN_NOTICE "kAFS: %s: argument missing\n", option);
139 * check that there's no subsequent value
141 static int want_no_value(char *const *_value, const char *option)
143 if (*_value && **_value) {
144 printk(KERN_NOTICE "kAFS: %s: Invalid argument: %s\n",
152 * parse the mount options
153 * - this function has been shamelessly adapted from the ext3 fs which
154 * shamelessly adapted it from the msdos fs
156 static int afs_super_parse_options(struct afs_mount_params *params,
158 const char **devname)
163 _enter("%s", options);
165 options[PAGE_SIZE - 1] = 0;
168 while ((key = strsep(&options, ",")) != 0)
170 value = strchr(key, '=');
174 printk("kAFS: KEY: %s, VAL:%s\n", key, value ?: "-");
176 if (strcmp(key, "rwpath") == 0) {
177 if (!want_no_value(&value, "rwpath"))
181 } else if (strcmp(key, "vol") == 0) {
182 if (!want_arg(&value, "vol"))
186 } else if (strcmp(key, "cell") == 0) {
187 if (!want_arg(&value, "cell"))
189 afs_put_cell(params->default_cell);
190 ret = afs_cell_lookup(value,
192 ¶ms->default_cell);
198 printk("kAFS: Unknown mount option: '%s'\n", key);
206 _leave(" = %d", ret);
211 * check a superblock to see if it's the one we're looking for
213 static int afs_test_super(struct super_block *sb, void *data)
215 struct afs_mount_params *params = data;
216 struct afs_super_info *as = sb->s_fs_info;
218 return as->volume == params->volume;
222 * fill in the superblock
224 static int afs_fill_super(struct super_block *sb, void *data, int silent)
226 struct afs_mount_params *params = data;
227 struct afs_super_info *as = NULL;
229 struct dentry *root = NULL;
230 struct inode *inode = NULL;
235 /* allocate a superblock info record */
236 as = kzalloc(sizeof(struct afs_super_info), GFP_KERNEL);
238 _leave(" = -ENOMEM");
242 afs_get_volume(params->volume);
243 as->volume = params->volume;
245 /* fill in the superblock */
246 sb->s_blocksize = PAGE_CACHE_SIZE;
247 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
248 sb->s_magic = AFS_FS_MAGIC;
249 sb->s_op = &afs_super_ops;
252 /* allocate the root inode and dentry */
253 fid.vid = as->volume->vid;
256 ret = afs_iget(sb, &fid, &inode);
261 root = d_alloc_root(inode);
272 afs_put_volume(as->volume);
275 sb->s_fs_info = NULL;
277 kleave(" = %d", ret);
282 * get an AFS superblock
283 * - TODO: don't use get_sb_nodev(), but rather call sget() directly
285 static int afs_get_sb(struct file_system_type *fs_type,
287 const char *dev_name,
289 struct vfsmount *mnt)
291 struct afs_mount_params params;
292 struct super_block *sb;
295 _enter(",,%s,%p", dev_name, options);
297 memset(¶ms, 0, sizeof(params));
299 /* start the cache manager */
302 _leave(" = %d", ret);
306 /* parse the options */
308 ret = afs_super_parse_options(¶ms, options, &dev_name);
312 printk("kAFS: no volume name specified\n");
318 /* parse the device name */
319 ret = afs_volume_lookup(dev_name,
326 /* allocate a deviceless superblock */
327 sb = sget(fs_type, afs_test_super, set_anon_super, ¶ms);
333 ret = afs_fill_super(sb, ¶ms, flags & MS_SILENT ? 1 : 0);
335 up_write(&sb->s_umount);
336 deactivate_super(sb);
339 sb->s_flags |= MS_ACTIVE;
340 simple_set_mnt(mnt, sb);
342 afs_put_volume(params.volume);
343 afs_put_cell(params.default_cell);
344 _leave(" = 0 [%p]", 0, sb);
348 afs_put_volume(params.volume);
349 afs_put_cell(params.default_cell);
351 _leave(" = %d", ret);
356 * finish the unmounting process on the superblock
358 static void afs_put_super(struct super_block *sb)
360 struct afs_super_info *as = sb->s_fs_info;
364 afs_put_volume(as->volume);
371 * initialise an inode cache slab element prior to any use
373 static void afs_i_init_once(void *_vnode, struct kmem_cache *cachep,
376 struct afs_vnode *vnode = _vnode;
378 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
379 SLAB_CTOR_CONSTRUCTOR) {
380 memset(vnode, 0, sizeof(*vnode));
381 inode_init_once(&vnode->vfs_inode);
382 init_waitqueue_head(&vnode->update_waitq);
383 spin_lock_init(&vnode->lock);
384 INIT_LIST_HEAD(&vnode->cb_link);
385 INIT_LIST_HEAD(&vnode->cb_hash_link);
386 afs_timer_init(&vnode->cb_timeout,
387 &afs_vnode_cb_timed_out_ops);
392 * allocate an AFS inode struct from our slab cache
394 static struct inode *afs_alloc_inode(struct super_block *sb)
396 struct afs_vnode *vnode;
398 vnode = kmem_cache_alloc(afs_inode_cachep, GFP_KERNEL);
402 atomic_inc(&afs_count_active_inodes);
404 memset(&vnode->fid, 0, sizeof(vnode->fid));
405 memset(&vnode->status, 0, sizeof(vnode->status));
407 vnode->volume = NULL;
408 vnode->update_cnt = 0;
411 return &vnode->vfs_inode;
415 * destroy an AFS inode struct
417 static void afs_destroy_inode(struct inode *inode)
419 _enter("{%lu}", inode->i_ino);
421 kmem_cache_free(afs_inode_cachep, AFS_FS_I(inode));
422 atomic_dec(&afs_count_active_inodes);