vserver 1.9.3
[linux-2.6.git] / fs / jffs2 / super.c
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright (C) 2001-2003 Red Hat, Inc.
5  *
6  * Created by David Woodhouse <dwmw2@redhat.com>
7  *
8  * For licensing information, see the file 'LICENCE' in this directory.
9  *
10  * $Id: super.c,v 1.99 2004/08/24 07:59:57 dwmw2 Exp $
11  *
12  */
13
14 #include <linux/config.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/init.h>
19 #include <linux/list.h>
20 #include <linux/fs.h>
21 #include <linux/mount.h>
22 #include <linux/jffs2.h>
23 #include <linux/pagemap.h>
24 #include <linux/mtd/mtd.h>
25 #include <linux/ctype.h>
26 #include <linux/namei.h>
27 #include "compr.h"
28 #include "nodelist.h"
29
30 static void jffs2_put_super(struct super_block *);
31
32 static kmem_cache_t *jffs2_inode_cachep;
33
34 static struct inode *jffs2_alloc_inode(struct super_block *sb)
35 {
36         struct jffs2_inode_info *ei;
37         ei = (struct jffs2_inode_info *)kmem_cache_alloc(jffs2_inode_cachep, SLAB_KERNEL);
38         if (!ei)
39                 return NULL;
40         return &ei->vfs_inode;
41 }
42
43 static void jffs2_destroy_inode(struct inode *inode)
44 {
45         kmem_cache_free(jffs2_inode_cachep, JFFS2_INODE_INFO(inode));
46 }
47
48 static void jffs2_i_init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
49 {
50         struct jffs2_inode_info *ei = (struct jffs2_inode_info *) foo;
51
52         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
53             SLAB_CTOR_CONSTRUCTOR) {
54                 init_MUTEX_LOCKED(&ei->sem);
55                 inode_init_once(&ei->vfs_inode);
56         }
57 }
58
59 static struct super_operations jffs2_super_operations =
60 {
61         .alloc_inode =  jffs2_alloc_inode,
62         .destroy_inode =jffs2_destroy_inode,
63         .read_inode =   jffs2_read_inode,
64         .put_super =    jffs2_put_super,
65         .write_super =  jffs2_write_super,
66         .statfs =       jffs2_statfs,
67         .remount_fs =   jffs2_remount_fs,
68         .clear_inode =  jffs2_clear_inode,
69         .dirty_inode =  jffs2_dirty_inode,
70 };
71
72 static int jffs2_sb_compare(struct super_block *sb, void *data)
73 {
74         struct jffs2_sb_info *p = data;
75         struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
76
77         /* The superblocks are considered to be equivalent if the underlying MTD
78            device is the same one */
79         if (c->mtd == p->mtd) {
80                 D1(printk(KERN_DEBUG "jffs2_sb_compare: match on device %d (\"%s\")\n", p->mtd->index, p->mtd->name));
81                 return 1;
82         } else {
83                 D1(printk(KERN_DEBUG "jffs2_sb_compare: No match, device %d (\"%s\"), device %d (\"%s\")\n",
84                           c->mtd->index, c->mtd->name, p->mtd->index, p->mtd->name));
85                 return 0;
86         }
87 }
88
89 static int jffs2_sb_set(struct super_block *sb, void *data)
90 {
91         struct jffs2_sb_info *p = data;
92
93         /* For persistence of NFS exports etc. we use the same s_dev
94            each time we mount the device, don't just use an anonymous
95            device */
96         sb->s_fs_info = p;
97         p->os_priv = sb;
98         sb->s_dev = MKDEV(MTD_BLOCK_MAJOR, p->mtd->index);
99
100         return 0;
101 }
102
103 static struct super_block *jffs2_get_sb_mtd(struct file_system_type *fs_type,
104                                               int flags, const char *dev_name, 
105                                               void *data, struct mtd_info *mtd)
106 {
107         struct super_block *sb;
108         struct jffs2_sb_info *c;
109         int ret;
110
111         c = kmalloc(sizeof(*c), GFP_KERNEL);
112         if (!c)
113                 return ERR_PTR(-ENOMEM);
114         memset(c, 0, sizeof(*c));
115         c->mtd = mtd;
116
117         sb = sget(fs_type, jffs2_sb_compare, jffs2_sb_set, c);
118
119         if (IS_ERR(sb))
120                 goto out_put;
121
122         if (sb->s_root) {
123                 /* New mountpoint for JFFS2 which is already mounted */
124                 D1(printk(KERN_DEBUG "jffs2_get_sb_mtd(): Device %d (\"%s\") is already mounted\n",
125                           mtd->index, mtd->name));
126                 goto out_put;
127         }
128
129         D1(printk(KERN_DEBUG "jffs2_get_sb_mtd(): New superblock for device %d (\"%s\")\n",
130                   mtd->index, mtd->name));
131
132         sb->s_op = &jffs2_super_operations;
133         sb->s_flags = flags | MS_NOATIME;
134
135         ret = jffs2_do_fill_super(sb, data, (flags&MS_VERBOSE)?1:0);
136
137         if (ret) {
138                 /* Failure case... */
139                 up_write(&sb->s_umount);
140                 deactivate_super(sb);
141                 return ERR_PTR(ret);
142         }
143
144         sb->s_flags |= MS_ACTIVE;
145         return sb;
146
147  out_put:
148         kfree(c);
149         put_mtd_device(mtd);
150
151         return sb;
152 }
153
154 static struct super_block *jffs2_get_sb_mtdnr(struct file_system_type *fs_type,
155                                               int flags, const char *dev_name, 
156                                               void *data, int mtdnr)
157 {
158         struct mtd_info *mtd;
159
160         mtd = get_mtd_device(NULL, mtdnr);
161         if (!mtd) {
162                 D1(printk(KERN_DEBUG "jffs2: MTD device #%u doesn't appear to exist\n", mtdnr));
163                 return ERR_PTR(-EINVAL);
164         }
165
166         return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd);
167 }
168
169 static struct super_block *jffs2_get_sb(struct file_system_type *fs_type,
170                                         int flags, const char *dev_name,
171                                         void *data)
172 {
173         int err;
174         struct nameidata nd;
175         int mtdnr;
176
177         if (!dev_name)
178                 return ERR_PTR(-EINVAL);
179
180         D1(printk(KERN_DEBUG "jffs2_get_sb(): dev_name \"%s\"\n", dev_name));
181
182         /* The preferred way of mounting in future; especially when
183            CONFIG_BLK_DEV is implemented - we specify the underlying
184            MTD device by number or by name, so that we don't require 
185            block device support to be present in the kernel. */
186
187         /* FIXME: How to do the root fs this way? */
188
189         if (dev_name[0] == 'm' && dev_name[1] == 't' && dev_name[2] == 'd') {
190                 /* Probably mounting without the blkdev crap */
191                 if (dev_name[3] == ':') {
192                         struct mtd_info *mtd;
193
194                         /* Mount by MTD device name */
195                         D1(printk(KERN_DEBUG "jffs2_get_sb(): mtd:%%s, name \"%s\"\n", dev_name+4));
196                         for (mtdnr = 0; mtdnr < MAX_MTD_DEVICES; mtdnr++) {
197                                 mtd = get_mtd_device(NULL, mtdnr);
198                                 if (mtd) {
199                                         if (!strcmp(mtd->name, dev_name+4))
200                                                 return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd);
201                                         put_mtd_device(mtd);
202                                 }
203                         }
204                         printk(KERN_NOTICE "jffs2_get_sb(): MTD device with name \"%s\" not found.\n", dev_name+4);
205                 } else if (isdigit(dev_name[3])) {
206                         /* Mount by MTD device number name */
207                         char *endptr;
208                         
209                         mtdnr = simple_strtoul(dev_name+3, &endptr, 0);
210                         if (!*endptr) {
211                                 /* It was a valid number */
212                                 D1(printk(KERN_DEBUG "jffs2_get_sb(): mtd%%d, mtdnr %d\n", mtdnr));
213                                 return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr);
214                         }
215                 }
216         }
217
218         /* Try the old way - the hack where we allowed users to mount 
219            /dev/mtdblock$(n) but didn't actually _use_ the blkdev */
220
221         err = path_lookup(dev_name, LOOKUP_FOLLOW, &nd);
222
223         D1(printk(KERN_DEBUG "jffs2_get_sb(): path_lookup() returned %d, inode %p\n",
224                   err, nd.dentry->d_inode));
225
226         if (err)
227                 return ERR_PTR(err);
228
229         err = -EINVAL;
230
231         if (!S_ISBLK(nd.dentry->d_inode->i_mode))
232                 goto out;
233
234         if (nd.mnt->mnt_flags & MNT_NODEV) {
235                 err = -EACCES;
236                 goto out;
237         }
238
239         if (imajor(nd.dentry->d_inode) != MTD_BLOCK_MAJOR) {
240                 if (!(flags & MS_VERBOSE)) /* Yes I mean this. Strangely */
241                         printk(KERN_NOTICE "Attempt to mount non-MTD device \"%s\" as JFFS2\n",
242                                dev_name);
243                 goto out;
244         }
245
246         mtdnr = iminor(nd.dentry->d_inode);
247         path_release(&nd);
248
249         return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr);
250
251 out:
252         path_release(&nd);
253         return ERR_PTR(err);
254 }
255
256 static void jffs2_put_super (struct super_block *sb)
257 {
258         struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
259
260         D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));
261
262         if (!(sb->s_flags & MS_RDONLY))
263                 jffs2_stop_garbage_collect_thread(c);
264         down(&c->alloc_sem);
265         jffs2_flush_wbuf_pad(c);
266         up(&c->alloc_sem);
267         jffs2_free_ino_caches(c);
268         jffs2_free_raw_node_refs(c);
269         kfree(c->blocks);
270         jffs2_flash_cleanup(c);
271         kfree(c->inocache_list);
272         if (c->mtd->sync)
273                 c->mtd->sync(c->mtd);
274
275         D1(printk(KERN_DEBUG "jffs2_put_super returning\n"));
276 }
277
278 static void jffs2_kill_sb(struct super_block *sb)
279 {
280         struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
281         generic_shutdown_super(sb);
282         put_mtd_device(c->mtd);
283         kfree(c);
284 }
285
286 static struct file_system_type jffs2_fs_type = {
287         .owner =        THIS_MODULE,
288         .name =         "jffs2",
289         .get_sb =       jffs2_get_sb,
290         .kill_sb =      jffs2_kill_sb,
291 };
292
293 static int __init init_jffs2_fs(void)
294 {
295         int ret;
296
297         printk(KERN_INFO "JFFS2 version 2.2."
298 #ifdef CONFIG_JFFS2_FS_NAND
299                " (NAND)"
300 #endif
301                " (C) 2001-2003 Red Hat, Inc.\n");
302
303         jffs2_inode_cachep = kmem_cache_create("jffs2_i",
304                                              sizeof(struct jffs2_inode_info),
305                                              0, SLAB_RECLAIM_ACCOUNT,
306                                              jffs2_i_init_once, NULL);
307         if (!jffs2_inode_cachep) {
308                 printk(KERN_ERR "JFFS2 error: Failed to initialise inode cache\n");
309                 return -ENOMEM;
310         }
311         ret = jffs2_compressors_init();
312         if (ret) {
313                 printk(KERN_ERR "JFFS2 error: Failed to initialise compressors\n");
314                 goto out;
315         }
316         ret = jffs2_create_slab_caches();
317         if (ret) {
318                 printk(KERN_ERR "JFFS2 error: Failed to initialise slab caches\n");
319                 goto out_compressors;
320         }
321         ret = register_filesystem(&jffs2_fs_type);
322         if (ret) {
323                 printk(KERN_ERR "JFFS2 error: Failed to register filesystem\n");
324                 goto out_slab;
325         }
326         return 0;
327
328  out_slab:
329         jffs2_destroy_slab_caches();
330  out_compressors:
331         jffs2_compressors_exit();
332  out:
333         kmem_cache_destroy(jffs2_inode_cachep);
334         return ret;
335 }
336
337 static void __exit exit_jffs2_fs(void)
338 {
339         unregister_filesystem(&jffs2_fs_type);
340         jffs2_destroy_slab_caches();
341         jffs2_compressors_exit();
342         kmem_cache_destroy(jffs2_inode_cachep);
343 }
344
345 module_init(init_jffs2_fs);
346 module_exit(exit_jffs2_fs);
347
348 MODULE_DESCRIPTION("The Journalling Flash File System, v2");
349 MODULE_AUTHOR("Red Hat, Inc.");
350 MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for 
351                        // the sake of this tag. It's Free Software.