Merge branch 'master' into for_paulus
[linux-drm-fsl-dcu.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, struct kmem_cache *cachep,
39                             unsigned long flags);
40
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);
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 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 const 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 struct kmem_cache *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         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;
108 } /* end afs_fs_init() */
109
110 /*****************************************************************************/
111 /*
112  * clean up the filesystem
113  */
114 void __exit afs_fs_exit(void)
115 {
116         unregister_filesystem(&afs_fs_type);
117
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));
121                 BUG();
122         }
123
124         kmem_cache_destroy(afs_inode_cachep);
125
126 } /* end afs_fs_exit() */
127
128 /*****************************************************************************/
129 /*
130  * check that an argument has a value
131  */
132 static int want_arg(char **_value, const char *option)
133 {
134         if (!_value || !*_value || !**_value) {
135                 printk(KERN_NOTICE "kAFS: %s: argument missing\n", option);
136                 return 0;
137         }
138         return 1;
139 } /* end want_arg() */
140
141 /*****************************************************************************/
142 /*
143  * check that there's no subsequent value
144  */
145 static int want_no_value(char *const *_value, const char *option)
146 {
147         if (*_value && **_value) {
148                 printk(KERN_NOTICE "kAFS: %s: Invalid argument: %s\n",
149                        option, *_value);
150                 return 0;
151         }
152         return 1;
153 } /* end want_no_value() */
154
155 /*****************************************************************************/
156 /*
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
160  */
161 static int afs_super_parse_options(struct afs_mount_params *params,
162                                    char *options,
163                                    const char **devname)
164 {
165         char *key, *value;
166         int ret;
167
168         _enter("%s", options);
169
170         options[PAGE_SIZE - 1] = 0;
171
172         ret = 0;
173         while ((key = strsep(&options, ",")) != 0)
174         {
175                 value = strchr(key, '=');
176                 if (value)
177                         *value++ = 0;
178
179                 printk("kAFS: KEY: %s, VAL:%s\n", key, value ?: "-");
180
181                 if (strcmp(key, "rwpath") == 0) {
182                         if (!want_no_value(&value, "rwpath"))
183                                 return -EINVAL;
184                         params->rwpath = 1;
185                         continue;
186                 }
187                 else if (strcmp(key, "vol") == 0) {
188                         if (!want_arg(&value, "vol"))
189                                 return -EINVAL;
190                         *devname = value;
191                         continue;
192                 }
193                 else if (strcmp(key, "cell") == 0) {
194                         if (!want_arg(&value, "cell"))
195                                 return -EINVAL;
196                         afs_put_cell(params->default_cell);
197                         ret = afs_cell_lookup(value,
198                                               strlen(value),
199                                               &params->default_cell);
200                         if (ret < 0)
201                                 return -EINVAL;
202                         continue;
203                 }
204
205                 printk("kAFS: Unknown mount option: '%s'\n",  key);
206                 ret = -EINVAL;
207                 goto error;
208         }
209
210         ret = 0;
211
212  error:
213         _leave(" = %d", ret);
214         return ret;
215 } /* end afs_super_parse_options() */
216
217 /*****************************************************************************/
218 /*
219  * check a superblock to see if it's the one we're looking for
220  */
221 static int afs_test_super(struct super_block *sb, void *data)
222 {
223         struct afs_mount_params *params = data;
224         struct afs_super_info *as = sb->s_fs_info;
225
226         return as->volume == params->volume;
227 } /* end afs_test_super() */
228
229 /*****************************************************************************/
230 /*
231  * fill in the superblock
232  */
233 static int afs_fill_super(struct super_block *sb, void *data, int silent)
234 {
235         struct afs_mount_params *params = data;
236         struct afs_super_info *as = NULL;
237         struct afs_fid fid;
238         struct dentry *root = NULL;
239         struct inode *inode = NULL;
240         int ret;
241
242         kenter("");
243
244         /* allocate a superblock info record */
245         as = kzalloc(sizeof(struct afs_super_info), GFP_KERNEL);
246         if (!as) {
247                 _leave(" = -ENOMEM");
248                 return -ENOMEM;
249         }
250
251         afs_get_volume(params->volume);
252         as->volume = params->volume;
253
254         /* fill in the superblock */
255         sb->s_blocksize         = PAGE_CACHE_SIZE;
256         sb->s_blocksize_bits    = PAGE_CACHE_SHIFT;
257         sb->s_magic             = AFS_FS_MAGIC;
258         sb->s_op                = &afs_super_ops;
259         sb->s_fs_info           = as;
260
261         /* allocate the root inode and dentry */
262         fid.vid         = as->volume->vid;
263         fid.vnode       = 1;
264         fid.unique      = 1;
265         ret = afs_iget(sb, &fid, &inode);
266         if (ret < 0)
267                 goto error;
268
269         ret = -ENOMEM;
270         root = d_alloc_root(inode);
271         if (!root)
272                 goto error;
273
274         sb->s_root = root;
275
276         kleave(" = 0");
277         return 0;
278
279  error:
280         iput(inode);
281         afs_put_volume(as->volume);
282         kfree(as);
283
284         sb->s_fs_info = NULL;
285
286         kleave(" = %d", ret);
287         return ret;
288 } /* end afs_fill_super() */
289
290 /*****************************************************************************/
291 /*
292  * get an AFS superblock
293  * - TODO: don't use get_sb_nodev(), but rather call sget() directly
294  */
295 static int afs_get_sb(struct file_system_type *fs_type,
296                       int flags,
297                       const char *dev_name,
298                       void *options,
299                       struct vfsmount *mnt)
300 {
301         struct afs_mount_params params;
302         struct super_block *sb;
303         int ret;
304
305         _enter(",,%s,%p", dev_name, options);
306
307         memset(&params, 0, sizeof(params));
308
309         /* start the cache manager */
310         ret = afscm_start();
311         if (ret < 0) {
312                 _leave(" = %d", ret);
313                 return ret;
314         }
315
316         /* parse the options */
317         if (options) {
318                 ret = afs_super_parse_options(&params, options, &dev_name);
319                 if (ret < 0)
320                         goto error;
321                 if (!dev_name) {
322                         printk("kAFS: no volume name specified\n");
323                         ret = -EINVAL;
324                         goto error;
325                 }
326         }
327
328         /* parse the device name */
329         ret = afs_volume_lookup(dev_name,
330                                 params.default_cell,
331                                 params.rwpath,
332                                 &params.volume);
333         if (ret < 0)
334                 goto error;
335
336         /* allocate a deviceless superblock */
337         sb = sget(fs_type, afs_test_super, set_anon_super, &params);
338         if (IS_ERR(sb))
339                 goto error;
340
341         sb->s_flags = flags;
342
343         ret = afs_fill_super(sb, &params, flags & MS_SILENT ? 1 : 0);
344         if (ret < 0) {
345                 up_write(&sb->s_umount);
346                 deactivate_super(sb);
347                 goto error;
348         }
349         sb->s_flags |= MS_ACTIVE;
350         simple_set_mnt(mnt, sb);
351
352         afs_put_volume(params.volume);
353         afs_put_cell(params.default_cell);
354         _leave(" = 0 [%p]", 0, sb);
355         return 0;
356
357  error:
358         afs_put_volume(params.volume);
359         afs_put_cell(params.default_cell);
360         afscm_stop();
361         _leave(" = %d", ret);
362         return ret;
363 } /* end afs_get_sb() */
364
365 /*****************************************************************************/
366 /*
367  * finish the unmounting process on the superblock
368  */
369 static void afs_put_super(struct super_block *sb)
370 {
371         struct afs_super_info *as = sb->s_fs_info;
372
373         _enter("");
374
375         afs_put_volume(as->volume);
376         afscm_stop();
377
378         _leave("");
379 } /* end afs_put_super() */
380
381 /*****************************************************************************/
382 /*
383  * initialise an inode cache slab element prior to any use
384  */
385 static void afs_i_init_once(void *_vnode, struct kmem_cache *cachep,
386                             unsigned long flags)
387 {
388         struct afs_vnode *vnode = (struct afs_vnode *) _vnode;
389
390         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
391             SLAB_CTOR_CONSTRUCTOR) {
392                 memset(vnode, 0, sizeof(*vnode));
393                 inode_init_once(&vnode->vfs_inode);
394                 init_waitqueue_head(&vnode->update_waitq);
395                 spin_lock_init(&vnode->lock);
396                 INIT_LIST_HEAD(&vnode->cb_link);
397                 INIT_LIST_HEAD(&vnode->cb_hash_link);
398                 afs_timer_init(&vnode->cb_timeout,
399                                &afs_vnode_cb_timed_out_ops);
400         }
401
402 } /* end afs_i_init_once() */
403
404 /*****************************************************************************/
405 /*
406  * allocate an AFS inode struct from our slab cache
407  */
408 static struct inode *afs_alloc_inode(struct super_block *sb)
409 {
410         struct afs_vnode *vnode;
411
412         vnode = (struct afs_vnode *)
413                 kmem_cache_alloc(afs_inode_cachep, GFP_KERNEL);
414         if (!vnode)
415                 return NULL;
416
417         atomic_inc(&afs_count_active_inodes);
418
419         memset(&vnode->fid, 0, sizeof(vnode->fid));
420         memset(&vnode->status, 0, sizeof(vnode->status));
421
422         vnode->volume           = NULL;
423         vnode->update_cnt       = 0;
424         vnode->flags            = 0;
425
426         return &vnode->vfs_inode;
427 } /* end afs_alloc_inode() */
428
429 /*****************************************************************************/
430 /*
431  * destroy an AFS inode struct
432  */
433 static void afs_destroy_inode(struct inode *inode)
434 {
435         _enter("{%lu}", inode->i_ino);
436
437         kmem_cache_free(afs_inode_cachep, AFS_FS_I(inode));
438
439         atomic_dec(&afs_count_active_inodes);
440
441 } /* end afs_destroy_inode() */