2 * Copyright (c) 2002 Red Hat, Inc. All rights reserved.
4 * This software may be freely redistributed under the terms of the
5 * GNU General Public License.
7 * You should have received a copy of the GNU General Public License
8 * along with this program; if not, write to the Free Software
9 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
11 * Authors: David Howells <dhowells@redhat.com>
12 * David Woodhouse <dwmw2@cambridge.redhat.com>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
21 #include <linux/pagemap.h>
25 #include "cmservice.h"
30 #define AFS_FS_MAGIC 0x6B414653 /* 'kAFS' */
32 struct afs_mount_params {
34 struct afs_cell *default_cell;
35 struct afs_volume *volume;
38 static void afs_i_init_once(void *foo, kmem_cache_t *cachep,
41 static int afs_get_sb(struct file_system_type *fs_type,
42 int flags, const char *dev_name,
43 void *data, struct vfsmount *mnt);
45 static struct inode *afs_alloc_inode(struct super_block *sb);
47 static void afs_put_super(struct super_block *sb);
49 static void afs_destroy_inode(struct inode *inode);
51 struct file_system_type afs_fs_type = {
55 .kill_sb = kill_anon_super,
56 .fs_flags = FS_BINARY_MOUNTDATA,
59 static struct super_operations afs_super_ops = {
60 .statfs = simple_statfs,
61 .alloc_inode = afs_alloc_inode,
62 .drop_inode = generic_delete_inode,
63 .destroy_inode = afs_destroy_inode,
64 .clear_inode = afs_clear_inode,
65 .put_super = afs_put_super,
68 static kmem_cache_t *afs_inode_cachep;
69 static atomic_t afs_count_active_inodes;
71 /*****************************************************************************/
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);
108 } /* end afs_fs_init() */
110 /*****************************************************************************/
112 * clean up the filesystem
114 void __exit afs_fs_exit(void)
116 unregister_filesystem(&afs_fs_type);
118 if (atomic_read(&afs_count_active_inodes) != 0) {
119 printk("kAFS: %d active inode objects still present\n",
120 atomic_read(&afs_count_active_inodes));
124 kmem_cache_destroy(afs_inode_cachep);
126 } /* end afs_fs_exit() */
128 /*****************************************************************************/
130 * check that an argument has a value
132 static int want_arg(char **_value, const char *option)
134 if (!_value || !*_value || !**_value) {
135 printk(KERN_NOTICE "kAFS: %s: argument missing\n", option);
139 } /* end want_arg() */
141 /*****************************************************************************/
143 * check that there's no subsequent value
145 static int want_no_value(char *const *_value, const char *option)
147 if (*_value && **_value) {
148 printk(KERN_NOTICE "kAFS: %s: Invalid argument: %s\n",
153 } /* end want_no_value() */
155 /*****************************************************************************/
157 * parse the mount options
158 * - this function has been shamelessly adapted from the ext3 fs which
159 * shamelessly adapted it from the msdos fs
161 static int afs_super_parse_options(struct afs_mount_params *params,
163 const char **devname)
168 _enter("%s", options);
170 options[PAGE_SIZE - 1] = 0;
173 while ((key = strsep(&options, ",")) != 0)
175 value = strchr(key, '=');
179 printk("kAFS: KEY: %s, VAL:%s\n", key, value ?: "-");
181 if (strcmp(key, "rwpath") == 0) {
182 if (!want_no_value(&value, "rwpath"))
187 else if (strcmp(key, "vol") == 0) {
188 if (!want_arg(&value, "vol"))
193 else if (strcmp(key, "cell") == 0) {
194 if (!want_arg(&value, "cell"))
196 afs_put_cell(params->default_cell);
197 ret = afs_cell_lookup(value,
199 ¶ms->default_cell);
205 printk("kAFS: Unknown mount option: '%s'\n", key);
213 _leave(" = %d", ret);
215 } /* end afs_super_parse_options() */
217 /*****************************************************************************/
219 * check a superblock to see if it's the one we're looking for
221 static int afs_test_super(struct super_block *sb, void *data)
223 struct afs_mount_params *params = data;
224 struct afs_super_info *as = sb->s_fs_info;
226 return as->volume == params->volume;
227 } /* end afs_test_super() */
229 /*****************************************************************************/
231 * fill in the superblock
233 static int afs_fill_super(struct super_block *sb, void *data, int silent)
235 struct afs_mount_params *params = data;
236 struct afs_super_info *as = NULL;
238 struct dentry *root = NULL;
239 struct inode *inode = NULL;
244 /* allocate a superblock info record */
245 as = kmalloc(sizeof(struct afs_super_info), GFP_KERNEL);
247 _leave(" = -ENOMEM");
251 memset(as, 0, sizeof(struct afs_super_info));
253 afs_get_volume(params->volume);
254 as->volume = params->volume;
256 /* fill in the superblock */
257 sb->s_blocksize = PAGE_CACHE_SIZE;
258 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
259 sb->s_magic = AFS_FS_MAGIC;
260 sb->s_op = &afs_super_ops;
263 /* allocate the root inode and dentry */
264 fid.vid = as->volume->vid;
267 ret = afs_iget(sb, &fid, &inode);
272 root = d_alloc_root(inode);
283 afs_put_volume(as->volume);
286 sb->s_fs_info = NULL;
288 kleave(" = %d", ret);
290 } /* end afs_fill_super() */
292 /*****************************************************************************/
294 * get an AFS superblock
295 * - TODO: don't use get_sb_nodev(), but rather call sget() directly
297 static int afs_get_sb(struct file_system_type *fs_type,
299 const char *dev_name,
301 struct vfsmount *mnt)
303 struct afs_mount_params params;
304 struct super_block *sb;
307 _enter(",,%s,%p", dev_name, options);
309 memset(¶ms, 0, sizeof(params));
311 /* start the cache manager */
314 _leave(" = %d", ret);
318 /* parse the options */
320 ret = afs_super_parse_options(¶ms, options, &dev_name);
324 printk("kAFS: no volume name specified\n");
330 /* parse the device name */
331 ret = afs_volume_lookup(dev_name,
338 /* allocate a deviceless superblock */
339 sb = sget(fs_type, afs_test_super, set_anon_super, ¶ms);
345 ret = afs_fill_super(sb, ¶ms, flags & MS_SILENT ? 1 : 0);
347 up_write(&sb->s_umount);
348 deactivate_super(sb);
351 sb->s_flags |= MS_ACTIVE;
352 simple_set_mnt(mnt, sb);
354 afs_put_volume(params.volume);
355 afs_put_cell(params.default_cell);
356 _leave(" = 0 [%p]", 0, sb);
360 afs_put_volume(params.volume);
361 afs_put_cell(params.default_cell);
363 _leave(" = %d", ret);
365 } /* end afs_get_sb() */
367 /*****************************************************************************/
369 * finish the unmounting process on the superblock
371 static void afs_put_super(struct super_block *sb)
373 struct afs_super_info *as = sb->s_fs_info;
377 afs_put_volume(as->volume);
381 } /* end afs_put_super() */
383 /*****************************************************************************/
385 * initialise an inode cache slab element prior to any use
387 static void afs_i_init_once(void *_vnode, kmem_cache_t *cachep,
390 struct afs_vnode *vnode = (struct afs_vnode *) _vnode;
392 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
393 SLAB_CTOR_CONSTRUCTOR) {
394 memset(vnode, 0, sizeof(*vnode));
395 inode_init_once(&vnode->vfs_inode);
396 init_waitqueue_head(&vnode->update_waitq);
397 spin_lock_init(&vnode->lock);
398 INIT_LIST_HEAD(&vnode->cb_link);
399 INIT_LIST_HEAD(&vnode->cb_hash_link);
400 afs_timer_init(&vnode->cb_timeout,
401 &afs_vnode_cb_timed_out_ops);
404 } /* end afs_i_init_once() */
406 /*****************************************************************************/
408 * allocate an AFS inode struct from our slab cache
410 static struct inode *afs_alloc_inode(struct super_block *sb)
412 struct afs_vnode *vnode;
414 vnode = (struct afs_vnode *)
415 kmem_cache_alloc(afs_inode_cachep, SLAB_KERNEL);
419 atomic_inc(&afs_count_active_inodes);
421 memset(&vnode->fid, 0, sizeof(vnode->fid));
422 memset(&vnode->status, 0, sizeof(vnode->status));
424 vnode->volume = NULL;
425 vnode->update_cnt = 0;
428 return &vnode->vfs_inode;
429 } /* end afs_alloc_inode() */
431 /*****************************************************************************/
433 * destroy an AFS inode struct
435 static void afs_destroy_inode(struct inode *inode)
437 _enter("{%lu}", inode->i_ino);
439 kmem_cache_free(afs_inode_cachep, AFS_FS_I(inode));
441 atomic_dec(&afs_count_active_inodes);
443 } /* end afs_destroy_inode() */