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