ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / afs / super.c
1 /*
2  * Copyright (c) 2002 Red Hat, Inc. All rights reserved.
3  *
4  * This software may be freely redistributed under the terms of the
5  * GNU General Public License.
6  *
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.
10  *
11  * Authors: David Howells <dhowells@redhat.com>
12  *          David Woodhouse <dwmw2@cambridge.redhat.com>
13  *
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/fs.h>
21 #include <linux/pagemap.h>
22 #include "vnode.h"
23 #include "volume.h"
24 #include "cell.h"
25 #include "cmservice.h"
26 #include "fsclient.h"
27 #include "super.h"
28 #include "internal.h"
29
30 #define AFS_FS_MAGIC 0x6B414653 /* 'kAFS' */
31
32 struct afs_mount_params {
33         int                     rwpath;
34         struct afs_cell         *default_cell;
35         struct afs_volume       *volume;
36 };
37
38 static void afs_i_init_once(void *foo, kmem_cache_t *cachep,
39                             unsigned long flags);
40
41 static struct super_block *afs_get_sb(struct file_system_type *fs_type,
42                                       int flags, const char *dev_name,
43                                       void *data);
44
45 static struct inode *afs_alloc_inode(struct super_block *sb);
46
47 static void afs_put_super(struct super_block *sb);
48
49 static void afs_destroy_inode(struct inode *inode);
50
51 static struct file_system_type afs_fs_type = {
52         .owner          = THIS_MODULE,
53         .name           = "afs",
54         .get_sb         = afs_get_sb,
55         .kill_sb        = kill_anon_super,
56         .fs_flags       = FS_BINARY_MOUNTDATA,
57 };
58
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,
66 };
67
68 static kmem_cache_t *afs_inode_cachep;
69 static atomic_t afs_count_active_inodes;
70
71 /*****************************************************************************/
72 /*
73  * initialise the filesystem
74  */
75 int __init afs_fs_init(void)
76 {
77         int ret;
78
79         _enter("");
80
81 #ifdef AFS_AUTOMOUNT_SUPPORT
82         afs_timer_init(&afs_mntpt_expiry_timer, &afs_mntpt_expiry_timer_ops);
83 #endif
84
85         /* create ourselves an inode cache */
86         atomic_set(&afs_count_active_inodes, 0);
87
88         ret = -ENOMEM;
89         afs_inode_cachep = kmem_cache_create("afs_inode_cache",
90                                              sizeof(struct afs_vnode),
91                                              0,
92                                              SLAB_HWCACHE_ALIGN,
93                                              afs_i_init_once,
94                                              NULL);
95         if (!afs_inode_cachep) {
96                 printk(KERN_NOTICE "kAFS: Failed to allocate inode cache\n");
97                 return ret;
98         }
99
100         /* now export our filesystem to lesser mortals */
101         ret = register_filesystem(&afs_fs_type);
102         if (ret < 0) {
103                 kmem_cache_destroy(afs_inode_cachep);
104                 kleave(" = %d", ret);
105                 return ret;
106         }
107
108         kleave(" = 0");
109         return 0;
110 } /* end afs_fs_init() */
111
112 /*****************************************************************************/
113 /*
114  * clean up the filesystem
115  */
116 void __exit afs_fs_exit(void)
117 {
118         unregister_filesystem(&afs_fs_type);
119
120         if (atomic_read(&afs_count_active_inodes) != 0) {
121                 printk("kAFS: %d active inode objects still present\n",
122                        atomic_read(&afs_count_active_inodes));
123                 BUG();
124         }
125
126         kmem_cache_destroy(afs_inode_cachep);
127
128 } /* end afs_fs_exit() */
129
130 /*****************************************************************************/
131 /*
132  * check that an argument has a value
133  */
134 static int want_arg(char **_value, const char *option)
135 {
136         if (!_value || !*_value || !**_value) {
137                 printk(KERN_NOTICE "kAFS: %s: argument missing\n", option);
138                 return 0;
139         }
140         return 1;
141 } /* end want_arg() */
142
143 /*****************************************************************************/
144 /*
145  * check that there's no subsequent value
146  */
147 static int want_no_value(char *const *_value, const char *option)
148 {
149         if (*_value && **_value) {
150                 printk(KERN_NOTICE "kAFS: %s: Invalid argument: %s\n",
151                        option, *_value);
152                 return 0;
153         }
154         return 1;
155 } /* end want_no_value() */
156
157 /*****************************************************************************/
158 /*
159  * parse the mount options
160  * - this function has been shamelessly adapted from the ext3 fs which
161  *   shamelessly adapted it from the msdos fs
162  */
163 static int afs_super_parse_options(struct afs_mount_params *params,
164                                    char *options,
165                                    const char **devname)
166 {
167         char *key, *value;
168         int ret;
169
170         _enter("%s", options);
171
172         options[PAGE_SIZE - 1] = 0;
173
174         ret = 0;
175         while ((key = strsep(&options, ",")))
176         {
177                 value = strchr(key, '=');
178                 if (value)
179                         *value++ = 0;
180
181                 printk("kAFS: KEY: %s, VAL:%s\n", key, value ?: "-");
182
183                 if (strcmp(key, "rwpath") == 0) {
184                         if (!want_no_value(&value, "rwpath"))
185                                 return -EINVAL;
186                         params->rwpath = 1;
187                         continue;
188                 }
189                 else if (strcmp(key, "vol") == 0) {
190                         if (!want_arg(&value, "vol"))
191                                 return -EINVAL;
192                         *devname = value;
193                         continue;
194                 }
195                 else if (strcmp(key, "cell") == 0) {
196                         if (!want_arg(&value, "cell"))
197                                 return -EINVAL;
198                         afs_put_cell(params->default_cell);
199                         ret = afs_cell_lookup(value,
200                                               strlen(value),
201                                               &params->default_cell);
202                         if (ret < 0)
203                                 return -EINVAL;
204                         continue;
205                 }
206
207                 printk("kAFS: Unknown mount option: '%s'\n",  key);
208                 ret = -EINVAL;
209                 goto error;
210         }
211
212         ret = 0;
213
214  error:
215         _leave(" = %d", ret);
216         return ret;
217 } /* end afs_super_parse_options() */
218
219 /*****************************************************************************/
220 /*
221  * check a superblock to see if it's the one we're looking for
222  */
223 static int afs_test_super(struct super_block *sb, void *data)
224 {
225         struct afs_mount_params *params = data;
226         struct afs_super_info *as = sb->s_fs_info;
227
228         return as->volume == params->volume;
229 } /* end afs_test_super() */
230
231 /*****************************************************************************/
232 /*
233  * fill in the superblock
234  */
235 static int afs_fill_super(struct super_block *sb, void *data, int silent)
236 {
237         struct afs_mount_params *params = data;
238         struct afs_super_info *as = NULL;
239         struct afs_fid fid;
240         struct dentry *root = NULL;
241         struct inode *inode = NULL;
242         int ret;
243
244         kenter("");
245
246         /* allocate a superblock info record */
247         as = kmalloc(sizeof(struct afs_super_info), GFP_KERNEL);
248         if (!as) {
249                 _leave(" = -ENOMEM");
250                 return -ENOMEM;
251         }
252
253         memset(as, 0, sizeof(struct afs_super_info));
254
255         afs_get_volume(params->volume);
256         as->volume = params->volume;
257
258         /* fill in the superblock */
259         sb->s_blocksize         = PAGE_CACHE_SIZE;
260         sb->s_blocksize_bits    = PAGE_CACHE_SHIFT;
261         sb->s_magic             = AFS_FS_MAGIC;
262         sb->s_op                = &afs_super_ops;
263         sb->s_fs_info           = as;
264
265         /* allocate the root inode and dentry */
266         fid.vid         = as->volume->vid;
267         fid.vnode       = 1;
268         fid.unique      = 1;
269         ret = afs_iget(sb, &fid, &inode);
270         if (ret < 0)
271                 goto error;
272
273         ret = -ENOMEM;
274         root = d_alloc_root(inode);
275         if (!root)
276                 goto error;
277
278         sb->s_root = root;
279
280         kleave(" = 0");
281         return 0;
282
283  error:
284         iput(inode);
285         afs_put_volume(as->volume);
286         kfree(as);
287
288         sb->s_fs_info = NULL;
289
290         kleave(" = %d", ret);
291         return ret;
292 } /* end afs_fill_super() */
293
294 /*****************************************************************************/
295 /*
296  * get an AFS superblock
297  * - TODO: don't use get_sb_nodev(), but rather call sget() directly
298  */
299 static struct super_block *afs_get_sb(struct file_system_type *fs_type,
300                                       int flags,
301                                       const char *dev_name,
302                                       void *options)
303 {
304         struct afs_mount_params params;
305         struct super_block *sb;
306         int ret;
307
308         _enter(",,%s,%p", dev_name, options);
309
310         memset(&params, 0, sizeof(params));
311
312         /* start the cache manager */
313         ret = afscm_start();
314         if (ret < 0) {
315                 _leave(" = %d", ret);
316                 return ERR_PTR(ret);
317         }
318
319         /* parse the options */
320         if (options) {
321                 ret = afs_super_parse_options(&params, options, &dev_name);
322                 if (ret < 0)
323                         goto error;
324                 if (!dev_name) {
325                         printk("kAFS: no volume name specified\n");
326                         ret = -EINVAL;
327                         goto error;
328                 }
329         }
330
331         /* parse the device name */
332         ret = afs_volume_lookup(dev_name,
333                                 params.default_cell,
334                                 params.rwpath,
335                                 &params.volume);
336         if (ret < 0)
337                 goto error;
338
339         /* allocate a deviceless superblock */
340         sb = sget(fs_type, afs_test_super, set_anon_super, &params);
341         if (IS_ERR(sb))
342                 goto error;
343
344         sb->s_flags = flags;
345
346         ret = afs_fill_super(sb, &params, flags & MS_VERBOSE ? 1 : 0);
347         if (ret < 0) {
348                 up_write(&sb->s_umount);
349                 deactivate_super(sb);
350                 goto error;
351         }
352         sb->s_flags |= MS_ACTIVE;
353
354         afs_put_volume(params.volume);
355         afs_put_cell(params.default_cell);
356         _leave(" = %p", sb);
357         return sb;
358
359  error:
360         afs_put_volume(params.volume);
361         afs_put_cell(params.default_cell);
362         afscm_stop();
363         _leave(" = %d", ret);
364         return ERR_PTR(ret);
365 } /* end afs_get_sb() */
366
367 /*****************************************************************************/
368 /*
369  * finish the unmounting process on the superblock
370  */
371 static void afs_put_super(struct super_block *sb)
372 {
373         struct afs_super_info *as = sb->s_fs_info;
374
375         _enter("");
376
377         afs_put_volume(as->volume);
378         afscm_stop();
379
380         _leave("");
381 } /* end afs_put_super() */
382
383 /*****************************************************************************/
384 /*
385  * initialise an inode cache slab element prior to any use
386  */
387 static void afs_i_init_once(void *_vnode, kmem_cache_t *cachep,
388                             unsigned long flags)
389 {
390         struct afs_vnode *vnode = (struct afs_vnode *) _vnode;
391
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);
402         }
403
404 } /* end afs_i_init_once() */
405
406 /*****************************************************************************/
407 /*
408  * allocate an AFS inode struct from our slab cache
409  */
410 static struct inode *afs_alloc_inode(struct super_block *sb)
411 {
412         struct afs_vnode *vnode;
413
414         vnode = (struct afs_vnode *)
415                 kmem_cache_alloc(afs_inode_cachep, SLAB_KERNEL);
416         if (!vnode)
417                 return NULL;
418
419         atomic_inc(&afs_count_active_inodes);
420
421         memset(&vnode->fid, 0, sizeof(vnode->fid));
422         memset(&vnode->status, 0, sizeof(vnode->status));
423
424         vnode->volume           = NULL;
425         vnode->update_cnt       = 0;
426         vnode->flags            = 0;
427
428         return &vnode->vfs_inode;
429 } /* end afs_alloc_inode() */
430
431 /*****************************************************************************/
432 /*
433  * destroy an AFS inode struct
434  */
435 static void afs_destroy_inode(struct inode *inode)
436 {
437         _enter("{%lu}", inode->i_ino);
438
439         kmem_cache_free(afs_inode_cachep, AFS_FS_I(inode));
440
441         atomic_dec(&afs_count_active_inodes);
442
443 } /* end afs_destroy_inode() */