blob: 0470a5c0b8a18829058a4d1d2327d75fc68af145 [file] [log] [blame]
David Howellsec268152007-04-26 15:49:28 -07001/* AFS superblock handling
2 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (c) 2002 Red Hat, Inc. All rights reserved.
4 *
5 * This software may be freely redistributed under the terms of the
6 * GNU General Public License.
7 *
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.
11 *
12 * Authors: David Howells <dhowells@redhat.com>
David Howellsec268152007-04-26 15:49:28 -070013 * David Woodhouse <dwmw2@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 */
16
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/slab.h>
21#include <linux/fs.h>
22#include <linux/pagemap.h>
23#include "vnode.h"
24#include "volume.h"
25#include "cell.h"
26#include "cmservice.h"
27#include "fsclient.h"
28#include "super.h"
29#include "internal.h"
30
31#define AFS_FS_MAGIC 0x6B414653 /* 'kAFS' */
32
33struct afs_mount_params {
34 int rwpath;
35 struct afs_cell *default_cell;
36 struct afs_volume *volume;
37};
38
Christoph Lametere18b8902006-12-06 20:33:20 -080039static void afs_i_init_once(void *foo, struct kmem_cache *cachep,
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 unsigned long flags);
41
David Howells454e2392006-06-23 02:02:57 -070042static int afs_get_sb(struct file_system_type *fs_type,
43 int flags, const char *dev_name,
44 void *data, struct vfsmount *mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46static struct inode *afs_alloc_inode(struct super_block *sb);
47
48static void afs_put_super(struct super_block *sb);
49
50static void afs_destroy_inode(struct inode *inode);
51
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -040052struct file_system_type afs_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 .owner = THIS_MODULE,
54 .name = "afs",
55 .get_sb = afs_get_sb,
56 .kill_sb = kill_anon_super,
57 .fs_flags = FS_BINARY_MOUNTDATA,
58};
59
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -080060static const struct super_operations afs_super_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 .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,
67};
68
Christoph Lametere18b8902006-12-06 20:33:20 -080069static struct kmem_cache *afs_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static atomic_t afs_count_active_inodes;
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/*
73 * initialise the filesystem
74 */
75int __init afs_fs_init(void)
76{
77 int ret;
78
79 _enter("");
80
81 afs_timer_init(&afs_mntpt_expiry_timer, &afs_mntpt_expiry_timer_ops);
82
83 /* create ourselves an inode cache */
84 atomic_set(&afs_count_active_inodes, 0);
85
86 ret = -ENOMEM;
87 afs_inode_cachep = kmem_cache_create("afs_inode_cache",
88 sizeof(struct afs_vnode),
89 0,
90 SLAB_HWCACHE_ALIGN,
91 afs_i_init_once,
92 NULL);
93 if (!afs_inode_cachep) {
94 printk(KERN_NOTICE "kAFS: Failed to allocate inode cache\n");
95 return ret;
96 }
97
98 /* now export our filesystem to lesser mortals */
99 ret = register_filesystem(&afs_fs_type);
100 if (ret < 0) {
101 kmem_cache_destroy(afs_inode_cachep);
102 kleave(" = %d", ret);
103 return ret;
104 }
105
106 kleave(" = 0");
107 return 0;
David Howellsec268152007-04-26 15:49:28 -0700108}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/*
111 * clean up the filesystem
112 */
113void __exit afs_fs_exit(void)
114{
115 unregister_filesystem(&afs_fs_type);
116
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));
120 BUG();
121 }
122
123 kmem_cache_destroy(afs_inode_cachep);
David Howellsec268152007-04-26 15:49:28 -0700124}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126/*
127 * check that an argument has a value
128 */
129static int want_arg(char **_value, const char *option)
130{
131 if (!_value || !*_value || !**_value) {
132 printk(KERN_NOTICE "kAFS: %s: argument missing\n", option);
133 return 0;
134 }
135 return 1;
David Howellsec268152007-04-26 15:49:28 -0700136}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/*
139 * check that there's no subsequent value
140 */
141static int want_no_value(char *const *_value, const char *option)
142{
143 if (*_value && **_value) {
144 printk(KERN_NOTICE "kAFS: %s: Invalid argument: %s\n",
145 option, *_value);
146 return 0;
147 }
148 return 1;
David Howellsec268152007-04-26 15:49:28 -0700149}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/*
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
155 */
156static int afs_super_parse_options(struct afs_mount_params *params,
157 char *options,
158 const char **devname)
159{
160 char *key, *value;
161 int ret;
162
163 _enter("%s", options);
164
165 options[PAGE_SIZE - 1] = 0;
166
167 ret = 0;
168 while ((key = strsep(&options, ",")) != 0)
169 {
170 value = strchr(key, '=');
171 if (value)
172 *value++ = 0;
173
174 printk("kAFS: KEY: %s, VAL:%s\n", key, value ?: "-");
175
176 if (strcmp(key, "rwpath") == 0) {
177 if (!want_no_value(&value, "rwpath"))
178 return -EINVAL;
179 params->rwpath = 1;
180 continue;
David Howellsec268152007-04-26 15:49:28 -0700181 } else if (strcmp(key, "vol") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (!want_arg(&value, "vol"))
183 return -EINVAL;
184 *devname = value;
185 continue;
David Howellsec268152007-04-26 15:49:28 -0700186 } else if (strcmp(key, "cell") == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (!want_arg(&value, "cell"))
188 return -EINVAL;
189 afs_put_cell(params->default_cell);
190 ret = afs_cell_lookup(value,
191 strlen(value),
192 &params->default_cell);
193 if (ret < 0)
194 return -EINVAL;
195 continue;
196 }
197
198 printk("kAFS: Unknown mount option: '%s'\n", key);
199 ret = -EINVAL;
200 goto error;
201 }
202
203 ret = 0;
204
David Howellsec268152007-04-26 15:49:28 -0700205error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 _leave(" = %d", ret);
207 return ret;
David Howellsec268152007-04-26 15:49:28 -0700208}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/*
211 * check a superblock to see if it's the one we're looking for
212 */
213static int afs_test_super(struct super_block *sb, void *data)
214{
215 struct afs_mount_params *params = data;
216 struct afs_super_info *as = sb->s_fs_info;
217
218 return as->volume == params->volume;
David Howellsec268152007-04-26 15:49:28 -0700219}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221/*
222 * fill in the superblock
223 */
224static int afs_fill_super(struct super_block *sb, void *data, int silent)
225{
226 struct afs_mount_params *params = data;
227 struct afs_super_info *as = NULL;
228 struct afs_fid fid;
229 struct dentry *root = NULL;
230 struct inode *inode = NULL;
231 int ret;
232
233 kenter("");
234
235 /* allocate a superblock info record */
Yan Burmanb593e482006-12-06 20:40:32 -0800236 as = kzalloc(sizeof(struct afs_super_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (!as) {
238 _leave(" = -ENOMEM");
239 return -ENOMEM;
240 }
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 afs_get_volume(params->volume);
243 as->volume = params->volume;
244
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;
250 sb->s_fs_info = as;
251
252 /* allocate the root inode and dentry */
253 fid.vid = as->volume->vid;
254 fid.vnode = 1;
255 fid.unique = 1;
256 ret = afs_iget(sb, &fid, &inode);
257 if (ret < 0)
258 goto error;
259
260 ret = -ENOMEM;
261 root = d_alloc_root(inode);
262 if (!root)
263 goto error;
264
265 sb->s_root = root;
266
267 kleave(" = 0");
268 return 0;
269
David Howellsec268152007-04-26 15:49:28 -0700270error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 iput(inode);
272 afs_put_volume(as->volume);
273 kfree(as);
274
275 sb->s_fs_info = NULL;
276
277 kleave(" = %d", ret);
278 return ret;
David Howellsec268152007-04-26 15:49:28 -0700279}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281/*
282 * get an AFS superblock
283 * - TODO: don't use get_sb_nodev(), but rather call sget() directly
284 */
David Howells454e2392006-06-23 02:02:57 -0700285static int afs_get_sb(struct file_system_type *fs_type,
286 int flags,
287 const char *dev_name,
288 void *options,
289 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 struct afs_mount_params params;
292 struct super_block *sb;
293 int ret;
294
295 _enter(",,%s,%p", dev_name, options);
296
297 memset(&params, 0, sizeof(params));
298
299 /* start the cache manager */
300 ret = afscm_start();
301 if (ret < 0) {
302 _leave(" = %d", ret);
David Howells454e2392006-06-23 02:02:57 -0700303 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305
306 /* parse the options */
307 if (options) {
308 ret = afs_super_parse_options(&params, options, &dev_name);
309 if (ret < 0)
310 goto error;
311 if (!dev_name) {
312 printk("kAFS: no volume name specified\n");
313 ret = -EINVAL;
314 goto error;
315 }
316 }
317
318 /* parse the device name */
319 ret = afs_volume_lookup(dev_name,
320 params.default_cell,
321 params.rwpath,
322 &params.volume);
323 if (ret < 0)
324 goto error;
325
326 /* allocate a deviceless superblock */
327 sb = sget(fs_type, afs_test_super, set_anon_super, &params);
328 if (IS_ERR(sb))
329 goto error;
330
331 sb->s_flags = flags;
332
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800333 ret = afs_fill_super(sb, &params, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (ret < 0) {
335 up_write(&sb->s_umount);
336 deactivate_super(sb);
337 goto error;
338 }
339 sb->s_flags |= MS_ACTIVE;
David Howells454e2392006-06-23 02:02:57 -0700340 simple_set_mnt(mnt, sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 afs_put_volume(params.volume);
343 afs_put_cell(params.default_cell);
David Howells454e2392006-06-23 02:02:57 -0700344 _leave(" = 0 [%p]", 0, sb);
345 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
David Howellsec268152007-04-26 15:49:28 -0700347error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 afs_put_volume(params.volume);
349 afs_put_cell(params.default_cell);
350 afscm_stop();
351 _leave(" = %d", ret);
David Howells454e2392006-06-23 02:02:57 -0700352 return ret;
David Howellsec268152007-04-26 15:49:28 -0700353}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355/*
356 * finish the unmounting process on the superblock
357 */
358static void afs_put_super(struct super_block *sb)
359{
360 struct afs_super_info *as = sb->s_fs_info;
361
362 _enter("");
363
364 afs_put_volume(as->volume);
365 afscm_stop();
366
367 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700368}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370/*
371 * initialise an inode cache slab element prior to any use
372 */
Christoph Lametere18b8902006-12-06 20:33:20 -0800373static void afs_i_init_once(void *_vnode, struct kmem_cache *cachep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 unsigned long flags)
375{
David Howellsec268152007-04-26 15:49:28 -0700376 struct afs_vnode *vnode = _vnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
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);
388 }
David Howellsec268152007-04-26 15:49:28 -0700389}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391/*
392 * allocate an AFS inode struct from our slab cache
393 */
394static struct inode *afs_alloc_inode(struct super_block *sb)
395{
396 struct afs_vnode *vnode;
397
David Howellsec268152007-04-26 15:49:28 -0700398 vnode = kmem_cache_alloc(afs_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (!vnode)
400 return NULL;
401
402 atomic_inc(&afs_count_active_inodes);
403
404 memset(&vnode->fid, 0, sizeof(vnode->fid));
405 memset(&vnode->status, 0, sizeof(vnode->status));
406
407 vnode->volume = NULL;
408 vnode->update_cnt = 0;
409 vnode->flags = 0;
410
411 return &vnode->vfs_inode;
David Howellsec268152007-04-26 15:49:28 -0700412}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414/*
415 * destroy an AFS inode struct
416 */
417static void afs_destroy_inode(struct inode *inode)
418{
419 _enter("{%lu}", inode->i_ino);
420
421 kmem_cache_free(afs_inode_cachep, AFS_FS_I(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 atomic_dec(&afs_count_active_inodes);
David Howellsec268152007-04-26 15:49:28 -0700423}