[AFS]: Clean up the AFS sources
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / afs / super.c
1 /* AFS superblock handling
2 *
3 * 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>
13 * David Woodhouse <dwmw2@redhat.com>
14 *
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
33 struct afs_mount_params {
34 int rwpath;
35 struct afs_cell *default_cell;
36 struct afs_volume *volume;
37 };
38
39 static void afs_i_init_once(void *foo, struct kmem_cache *cachep,
40 unsigned long flags);
41
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);
45
46 static struct inode *afs_alloc_inode(struct super_block *sb);
47
48 static void afs_put_super(struct super_block *sb);
49
50 static void afs_destroy_inode(struct inode *inode);
51
52 struct file_system_type afs_fs_type = {
53 .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
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,
67 };
68
69 static struct kmem_cache *afs_inode_cachep;
70 static atomic_t afs_count_active_inodes;
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 }
109
110 /*
111 * clean up the filesystem
112 */
113 void __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);
124 }
125
126 /*
127 * check that an argument has a value
128 */
129 static 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;
136 }
137
138 /*
139 * check that there's no subsequent value
140 */
141 static 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;
149 }
150
151 /*
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 */
156 static 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;
181 } else if (strcmp(key, "vol") == 0) {
182 if (!want_arg(&value, "vol"))
183 return -EINVAL;
184 *devname = value;
185 continue;
186 } else if (strcmp(key, "cell") == 0) {
187 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
205 error:
206 _leave(" = %d", ret);
207 return ret;
208 }
209
210 /*
211 * check a superblock to see if it's the one we're looking for
212 */
213 static 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;
219 }
220
221 /*
222 * fill in the superblock
223 */
224 static 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 */
236 as = kzalloc(sizeof(struct afs_super_info), GFP_KERNEL);
237 if (!as) {
238 _leave(" = -ENOMEM");
239 return -ENOMEM;
240 }
241
242 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
270 error:
271 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;
279 }
280
281 /*
282 * get an AFS superblock
283 * - TODO: don't use get_sb_nodev(), but rather call sget() directly
284 */
285 static 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)
290 {
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);
303 return ret;
304 }
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
333 ret = afs_fill_super(sb, &params, flags & MS_SILENT ? 1 : 0);
334 if (ret < 0) {
335 up_write(&sb->s_umount);
336 deactivate_super(sb);
337 goto error;
338 }
339 sb->s_flags |= MS_ACTIVE;
340 simple_set_mnt(mnt, sb);
341
342 afs_put_volume(params.volume);
343 afs_put_cell(params.default_cell);
344 _leave(" = 0 [%p]", 0, sb);
345 return 0;
346
347 error:
348 afs_put_volume(params.volume);
349 afs_put_cell(params.default_cell);
350 afscm_stop();
351 _leave(" = %d", ret);
352 return ret;
353 }
354
355 /*
356 * finish the unmounting process on the superblock
357 */
358 static 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("");
368 }
369
370 /*
371 * initialise an inode cache slab element prior to any use
372 */
373 static void afs_i_init_once(void *_vnode, struct kmem_cache *cachep,
374 unsigned long flags)
375 {
376 struct afs_vnode *vnode = _vnode;
377
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 }
389 }
390
391 /*
392 * allocate an AFS inode struct from our slab cache
393 */
394 static struct inode *afs_alloc_inode(struct super_block *sb)
395 {
396 struct afs_vnode *vnode;
397
398 vnode = kmem_cache_alloc(afs_inode_cachep, GFP_KERNEL);
399 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;
412 }
413
414 /*
415 * destroy an AFS inode struct
416 */
417 static 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));
422 atomic_dec(&afs_count_active_inodes);
423 }