2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright © 2001-2007 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@infradead.org>
8 * For licensing information, see the file 'LICENCE' in this directory.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/init.h>
16 #include <linux/list.h>
18 #include <linux/err.h>
19 #include <linux/mount.h>
20 #include <linux/jffs2.h>
21 #include <linux/pagemap.h>
22 #include <linux/mtd/mtd.h>
23 #include <linux/ctype.h>
24 #include <linux/namei.h>
28 static void jffs2_put_super(struct super_block *);
30 static struct kmem_cache *jffs2_inode_cachep;
32 static struct inode *jffs2_alloc_inode(struct super_block *sb)
34 struct jffs2_inode_info *ei;
35 ei = (struct jffs2_inode_info *)kmem_cache_alloc(jffs2_inode_cachep, GFP_KERNEL);
38 return &ei->vfs_inode;
41 static void jffs2_destroy_inode(struct inode *inode)
43 kmem_cache_free(jffs2_inode_cachep, JFFS2_INODE_INFO(inode));
46 static void jffs2_i_init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
48 struct jffs2_inode_info *ei = (struct jffs2_inode_info *) foo;
50 if (flags & SLAB_CTOR_CONSTRUCTOR) {
52 inode_init_once(&ei->vfs_inode);
56 static int jffs2_sync_fs(struct super_block *sb, int wait)
58 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
61 jffs2_flush_wbuf_pad(c);
66 static const struct super_operations jffs2_super_operations =
68 .alloc_inode = jffs2_alloc_inode,
69 .destroy_inode =jffs2_destroy_inode,
70 .read_inode = jffs2_read_inode,
71 .put_super = jffs2_put_super,
72 .write_super = jffs2_write_super,
73 .statfs = jffs2_statfs,
74 .remount_fs = jffs2_remount_fs,
75 .clear_inode = jffs2_clear_inode,
76 .dirty_inode = jffs2_dirty_inode,
77 .sync_fs = jffs2_sync_fs,
80 static int jffs2_sb_compare(struct super_block *sb, void *data)
82 struct jffs2_sb_info *p = data;
83 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
85 /* The superblocks are considered to be equivalent if the underlying MTD
86 device is the same one */
87 if (c->mtd == p->mtd) {
88 D1(printk(KERN_DEBUG "jffs2_sb_compare: match on device %d (\"%s\")\n", p->mtd->index, p->mtd->name));
91 D1(printk(KERN_DEBUG "jffs2_sb_compare: No match, device %d (\"%s\"), device %d (\"%s\")\n",
92 c->mtd->index, c->mtd->name, p->mtd->index, p->mtd->name));
97 static int jffs2_sb_set(struct super_block *sb, void *data)
99 struct jffs2_sb_info *p = data;
101 /* For persistence of NFS exports etc. we use the same s_dev
102 each time we mount the device, don't just use an anonymous
106 sb->s_dev = MKDEV(MTD_BLOCK_MAJOR, p->mtd->index);
111 static int jffs2_get_sb_mtd(struct file_system_type *fs_type,
112 int flags, const char *dev_name,
113 void *data, struct mtd_info *mtd,
114 struct vfsmount *mnt)
116 struct super_block *sb;
117 struct jffs2_sb_info *c;
120 c = kzalloc(sizeof(*c), GFP_KERNEL);
125 sb = sget(fs_type, jffs2_sb_compare, jffs2_sb_set, c);
131 /* New mountpoint for JFFS2 which is already mounted */
132 D1(printk(KERN_DEBUG "jffs2_get_sb_mtd(): Device %d (\"%s\") is already mounted\n",
133 mtd->index, mtd->name));
134 ret = simple_set_mnt(mnt, sb);
138 D1(printk(KERN_DEBUG "jffs2_get_sb_mtd(): New superblock for device %d (\"%s\")\n",
139 mtd->index, mtd->name));
141 /* Initialize JFFS2 superblock locks, the further initialization will be
143 init_MUTEX(&c->alloc_sem);
144 init_MUTEX(&c->erase_free_sem);
145 init_waitqueue_head(&c->erase_wait);
146 init_waitqueue_head(&c->inocache_wq);
147 spin_lock_init(&c->erase_completion_lock);
148 spin_lock_init(&c->inocache_lock);
150 sb->s_op = &jffs2_super_operations;
151 sb->s_flags = flags | MS_NOATIME;
152 sb->s_xattr = jffs2_xattr_handlers;
153 #ifdef CONFIG_JFFS2_FS_POSIX_ACL
154 sb->s_flags |= MS_POSIXACL;
156 ret = jffs2_do_fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
159 /* Failure case... */
160 up_write(&sb->s_umount);
161 deactivate_super(sb);
165 sb->s_flags |= MS_ACTIVE;
166 return simple_set_mnt(mnt, sb);
177 static int jffs2_get_sb_mtdnr(struct file_system_type *fs_type,
178 int flags, const char *dev_name,
179 void *data, int mtdnr,
180 struct vfsmount *mnt)
182 struct mtd_info *mtd;
184 mtd = get_mtd_device(NULL, mtdnr);
186 D1(printk(KERN_DEBUG "jffs2: MTD device #%u doesn't appear to exist\n", mtdnr));
190 return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd, mnt);
193 static int jffs2_get_sb(struct file_system_type *fs_type,
194 int flags, const char *dev_name,
195 void *data, struct vfsmount *mnt)
204 D1(printk(KERN_DEBUG "jffs2_get_sb(): dev_name \"%s\"\n", dev_name));
206 /* The preferred way of mounting in future; especially when
207 CONFIG_BLK_DEV is implemented - we specify the underlying
208 MTD device by number or by name, so that we don't require
209 block device support to be present in the kernel. */
211 /* FIXME: How to do the root fs this way? */
213 if (dev_name[0] == 'm' && dev_name[1] == 't' && dev_name[2] == 'd') {
214 /* Probably mounting without the blkdev crap */
215 if (dev_name[3] == ':') {
216 struct mtd_info *mtd;
218 /* Mount by MTD device name */
219 D1(printk(KERN_DEBUG "jffs2_get_sb(): mtd:%%s, name \"%s\"\n", dev_name+4));
220 for (mtdnr = 0; mtdnr < MAX_MTD_DEVICES; mtdnr++) {
221 mtd = get_mtd_device(NULL, mtdnr);
223 if (!strcmp(mtd->name, dev_name+4))
224 return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd, mnt);
228 printk(KERN_NOTICE "jffs2_get_sb(): MTD device with name \"%s\" not found.\n", dev_name+4);
229 } else if (isdigit(dev_name[3])) {
230 /* Mount by MTD device number name */
233 mtdnr = simple_strtoul(dev_name+3, &endptr, 0);
235 /* It was a valid number */
236 D1(printk(KERN_DEBUG "jffs2_get_sb(): mtd%%d, mtdnr %d\n", mtdnr));
237 return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr, mnt);
242 /* Try the old way - the hack where we allowed users to mount
243 /dev/mtdblock$(n) but didn't actually _use_ the blkdev */
245 err = path_lookup(dev_name, LOOKUP_FOLLOW, &nd);
247 D1(printk(KERN_DEBUG "jffs2_get_sb(): path_lookup() returned %d, inode %p\n",
248 err, nd.dentry->d_inode));
255 if (!S_ISBLK(nd.dentry->d_inode->i_mode))
258 if (nd.mnt->mnt_flags & MNT_NODEV) {
263 if (imajor(nd.dentry->d_inode) != MTD_BLOCK_MAJOR) {
264 if (!(flags & MS_SILENT))
265 printk(KERN_NOTICE "Attempt to mount non-MTD device \"%s\" as JFFS2\n",
270 mtdnr = iminor(nd.dentry->d_inode);
273 return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr, mnt);
280 static void jffs2_put_super (struct super_block *sb)
282 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
284 D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));
287 jffs2_flush_wbuf_pad(c);
292 jffs2_free_ino_caches(c);
293 jffs2_free_raw_node_refs(c);
294 if (jffs2_blocks_use_vmalloc(c))
298 jffs2_flash_cleanup(c);
299 kfree(c->inocache_list);
300 jffs2_clear_xattr_subsystem(c);
302 c->mtd->sync(c->mtd);
304 D1(printk(KERN_DEBUG "jffs2_put_super returning\n"));
307 static void jffs2_kill_sb(struct super_block *sb)
309 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
310 if (!(sb->s_flags & MS_RDONLY))
311 jffs2_stop_garbage_collect_thread(c);
312 generic_shutdown_super(sb);
313 put_mtd_device(c->mtd);
317 static struct file_system_type jffs2_fs_type = {
318 .owner = THIS_MODULE,
320 .get_sb = jffs2_get_sb,
321 .kill_sb = jffs2_kill_sb,
324 static int __init init_jffs2_fs(void)
328 /* Paranoia checks for on-medium structures. If we ask GCC
329 to pack them with __attribute__((packed)) then it _also_
330 assumes that they're not aligned -- so it emits crappy
331 code on some architectures. Ideally we want an attribute
332 which means just 'no padding', without the alignment
333 thing. But GCC doesn't have that -- we have to just
334 hope the structs are the right sizes, instead. */
335 BUILD_BUG_ON(sizeof(struct jffs2_unknown_node) != 12);
336 BUILD_BUG_ON(sizeof(struct jffs2_raw_dirent) != 40);
337 BUILD_BUG_ON(sizeof(struct jffs2_raw_inode) != 68);
338 BUILD_BUG_ON(sizeof(struct jffs2_raw_summary) != 32);
340 printk(KERN_INFO "JFFS2 version 2.2."
341 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
344 #ifdef CONFIG_JFFS2_SUMMARY
347 " © 2001-2006 Red Hat, Inc.\n");
349 jffs2_inode_cachep = kmem_cache_create("jffs2_i",
350 sizeof(struct jffs2_inode_info),
351 0, (SLAB_RECLAIM_ACCOUNT|
353 jffs2_i_init_once, NULL);
354 if (!jffs2_inode_cachep) {
355 printk(KERN_ERR "JFFS2 error: Failed to initialise inode cache\n");
358 ret = jffs2_compressors_init();
360 printk(KERN_ERR "JFFS2 error: Failed to initialise compressors\n");
363 ret = jffs2_create_slab_caches();
365 printk(KERN_ERR "JFFS2 error: Failed to initialise slab caches\n");
366 goto out_compressors;
368 ret = register_filesystem(&jffs2_fs_type);
370 printk(KERN_ERR "JFFS2 error: Failed to register filesystem\n");
376 jffs2_destroy_slab_caches();
378 jffs2_compressors_exit();
380 kmem_cache_destroy(jffs2_inode_cachep);
384 static void __exit exit_jffs2_fs(void)
386 unregister_filesystem(&jffs2_fs_type);
387 jffs2_destroy_slab_caches();
388 jffs2_compressors_exit();
389 kmem_cache_destroy(jffs2_inode_cachep);
392 module_init(init_jffs2_fs);
393 module_exit(exit_jffs2_fs);
395 MODULE_DESCRIPTION("The Journalling Flash File System, v2");
396 MODULE_AUTHOR("Red Hat, Inc.");
397 MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for
398 // the sake of this tag. It's Free Software.