2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001-2003 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@infradead.org>
8 * For licensing information, see the file 'LICENCE' in this directory.
10 * $Id: super.c,v 1.110 2005/11/07 11:14:42 gleixner Exp $
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/init.h>
18 #include <linux/list.h>
20 #include <linux/mount.h>
21 #include <linux/jffs2.h>
22 #include <linux/pagemap.h>
23 #include <linux/mtd/mtd.h>
24 #include <linux/ctype.h>
25 #include <linux/namei.h>
29 static void jffs2_put_super(struct super_block *);
31 static kmem_cache_t *jffs2_inode_cachep;
33 static struct inode *jffs2_alloc_inode(struct super_block *sb)
35 struct jffs2_inode_info *ei;
36 ei = (struct jffs2_inode_info *)kmem_cache_alloc(jffs2_inode_cachep, SLAB_KERNEL);
39 return &ei->vfs_inode;
42 static void jffs2_destroy_inode(struct inode *inode)
44 kmem_cache_free(jffs2_inode_cachep, JFFS2_INODE_INFO(inode));
47 static void jffs2_i_init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
49 struct jffs2_inode_info *ei = (struct jffs2_inode_info *) foo;
51 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
52 SLAB_CTOR_CONSTRUCTOR) {
54 inode_init_once(&ei->vfs_inode);
58 static int jffs2_sync_fs(struct super_block *sb, int wait)
60 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
63 jffs2_flush_wbuf_pad(c);
68 static struct super_operations jffs2_super_operations =
70 .alloc_inode = jffs2_alloc_inode,
71 .destroy_inode =jffs2_destroy_inode,
72 .read_inode = jffs2_read_inode,
73 .put_super = jffs2_put_super,
74 .write_super = jffs2_write_super,
75 .statfs = jffs2_statfs,
76 .remount_fs = jffs2_remount_fs,
77 .clear_inode = jffs2_clear_inode,
78 .dirty_inode = jffs2_dirty_inode,
79 .sync_fs = jffs2_sync_fs,
82 static int jffs2_sb_compare(struct super_block *sb, void *data)
84 struct jffs2_sb_info *p = data;
85 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
87 /* The superblocks are considered to be equivalent if the underlying MTD
88 device is the same one */
89 if (c->mtd == p->mtd) {
90 D1(printk(KERN_DEBUG "jffs2_sb_compare: match on device %d (\"%s\")\n", p->mtd->index, p->mtd->name));
93 D1(printk(KERN_DEBUG "jffs2_sb_compare: No match, device %d (\"%s\"), device %d (\"%s\")\n",
94 c->mtd->index, c->mtd->name, p->mtd->index, p->mtd->name));
99 static int jffs2_sb_set(struct super_block *sb, void *data)
101 struct jffs2_sb_info *p = data;
103 /* For persistence of NFS exports etc. we use the same s_dev
104 each time we mount the device, don't just use an anonymous
108 sb->s_dev = MKDEV(MTD_BLOCK_MAJOR, p->mtd->index);
113 static int jffs2_get_sb_mtd(struct file_system_type *fs_type,
114 int flags, const char *dev_name,
115 void *data, struct mtd_info *mtd,
116 struct vfsmount *mnt)
118 struct super_block *sb;
119 struct jffs2_sb_info *c;
122 c = kmalloc(sizeof(*c), GFP_KERNEL);
125 memset(c, 0, sizeof(*c));
128 sb = sget(fs_type, jffs2_sb_compare, jffs2_sb_set, c);
134 /* New mountpoint for JFFS2 which is already mounted */
135 D1(printk(KERN_DEBUG "jffs2_get_sb_mtd(): Device %d (\"%s\") is already mounted\n",
136 mtd->index, mtd->name));
137 ret = simple_set_mnt(mnt, sb);
141 D1(printk(KERN_DEBUG "jffs2_get_sb_mtd(): New superblock for device %d (\"%s\")\n",
142 mtd->index, mtd->name));
144 /* Initialize JFFS2 superblock locks, the further initialization will be
146 init_MUTEX(&c->alloc_sem);
147 init_MUTEX(&c->erase_free_sem);
148 init_waitqueue_head(&c->erase_wait);
149 init_waitqueue_head(&c->inocache_wq);
150 spin_lock_init(&c->erase_completion_lock);
151 spin_lock_init(&c->inocache_lock);
153 sb->s_op = &jffs2_super_operations;
154 sb->s_flags = flags | MS_NOATIME;
155 sb->s_xattr = jffs2_xattr_handlers;
156 #ifdef CONFIG_JFFS2_FS_POSIX_ACL
157 sb->s_flags |= MS_POSIXACL;
159 ret = jffs2_do_fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
162 /* Failure case... */
163 up_write(&sb->s_umount);
164 deactivate_super(sb);
168 sb->s_flags |= MS_ACTIVE;
169 return simple_set_mnt(mnt, sb);
180 static int jffs2_get_sb_mtdnr(struct file_system_type *fs_type,
181 int flags, const char *dev_name,
182 void *data, int mtdnr,
183 struct vfsmount *mnt)
185 struct mtd_info *mtd;
187 mtd = get_mtd_device(NULL, mtdnr);
189 D1(printk(KERN_DEBUG "jffs2: MTD device #%u doesn't appear to exist\n", mtdnr));
193 return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd, mnt);
196 static int jffs2_get_sb(struct file_system_type *fs_type,
197 int flags, const char *dev_name,
198 void *data, struct vfsmount *mnt)
207 D1(printk(KERN_DEBUG "jffs2_get_sb(): dev_name \"%s\"\n", dev_name));
209 /* The preferred way of mounting in future; especially when
210 CONFIG_BLK_DEV is implemented - we specify the underlying
211 MTD device by number or by name, so that we don't require
212 block device support to be present in the kernel. */
214 /* FIXME: How to do the root fs this way? */
216 if (dev_name[0] == 'm' && dev_name[1] == 't' && dev_name[2] == 'd') {
217 /* Probably mounting without the blkdev crap */
218 if (dev_name[3] == ':') {
219 struct mtd_info *mtd;
221 /* Mount by MTD device name */
222 D1(printk(KERN_DEBUG "jffs2_get_sb(): mtd:%%s, name \"%s\"\n", dev_name+4));
223 for (mtdnr = 0; mtdnr < MAX_MTD_DEVICES; mtdnr++) {
224 mtd = get_mtd_device(NULL, mtdnr);
226 if (!strcmp(mtd->name, dev_name+4))
227 return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd, mnt);
231 printk(KERN_NOTICE "jffs2_get_sb(): MTD device with name \"%s\" not found.\n", dev_name+4);
232 } else if (isdigit(dev_name[3])) {
233 /* Mount by MTD device number name */
236 mtdnr = simple_strtoul(dev_name+3, &endptr, 0);
238 /* It was a valid number */
239 D1(printk(KERN_DEBUG "jffs2_get_sb(): mtd%%d, mtdnr %d\n", mtdnr));
240 return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr, mnt);
245 /* Try the old way - the hack where we allowed users to mount
246 /dev/mtdblock$(n) but didn't actually _use_ the blkdev */
248 err = path_lookup(dev_name, LOOKUP_FOLLOW, &nd);
250 D1(printk(KERN_DEBUG "jffs2_get_sb(): path_lookup() returned %d, inode %p\n",
251 err, nd.dentry->d_inode));
258 if (!S_ISBLK(nd.dentry->d_inode->i_mode))
261 if (nd.mnt->mnt_flags & MNT_NODEV) {
266 if (imajor(nd.dentry->d_inode) != MTD_BLOCK_MAJOR) {
267 if (!(flags & MS_SILENT))
268 printk(KERN_NOTICE "Attempt to mount non-MTD device \"%s\" as JFFS2\n",
273 mtdnr = iminor(nd.dentry->d_inode);
276 return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr, mnt);
283 static void jffs2_put_super (struct super_block *sb)
285 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
287 D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));
290 jffs2_flush_wbuf_pad(c);
295 jffs2_free_ino_caches(c);
296 jffs2_free_raw_node_refs(c);
297 if (jffs2_blocks_use_vmalloc(c))
301 jffs2_flash_cleanup(c);
302 kfree(c->inocache_list);
303 jffs2_clear_xattr_subsystem(c);
305 c->mtd->sync(c->mtd);
307 D1(printk(KERN_DEBUG "jffs2_put_super returning\n"));
310 static void jffs2_kill_sb(struct super_block *sb)
312 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
313 if (!(sb->s_flags & MS_RDONLY))
314 jffs2_stop_garbage_collect_thread(c);
315 generic_shutdown_super(sb);
316 put_mtd_device(c->mtd);
320 static struct file_system_type jffs2_fs_type = {
321 .owner = THIS_MODULE,
323 .get_sb = jffs2_get_sb,
324 .kill_sb = jffs2_kill_sb,
327 static int __init init_jffs2_fs(void)
331 /* Paranoia checks for on-medium structures. If we ask GCC
332 to pack them with __attribute__((packed)) then it _also_
333 assumes that they're not aligned -- so it emits crappy
334 code on some architectures. Ideally we want an attribute
335 which means just 'no padding', without the alignment
336 thing. But GCC doesn't have that -- we have to just
337 hope the structs are the right sizes, instead. */
338 BUG_ON(sizeof(struct jffs2_unknown_node) != 12);
339 BUG_ON(sizeof(struct jffs2_raw_dirent) != 40);
340 BUG_ON(sizeof(struct jffs2_raw_inode) != 68);
341 BUG_ON(sizeof(struct jffs2_raw_summary) != 32);
343 printk(KERN_INFO "JFFS2 version 2.2."
344 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
347 #ifdef CONFIG_JFFS2_SUMMARY
350 " (C) 2001-2006 Red Hat, Inc.\n");
352 jffs2_inode_cachep = kmem_cache_create("jffs2_i",
353 sizeof(struct jffs2_inode_info),
354 0, (SLAB_RECLAIM_ACCOUNT|
356 jffs2_i_init_once, NULL);
357 if (!jffs2_inode_cachep) {
358 printk(KERN_ERR "JFFS2 error: Failed to initialise inode cache\n");
361 ret = jffs2_compressors_init();
363 printk(KERN_ERR "JFFS2 error: Failed to initialise compressors\n");
366 ret = jffs2_create_slab_caches();
368 printk(KERN_ERR "JFFS2 error: Failed to initialise slab caches\n");
369 goto out_compressors;
371 ret = register_filesystem(&jffs2_fs_type);
373 printk(KERN_ERR "JFFS2 error: Failed to register filesystem\n");
379 jffs2_destroy_slab_caches();
381 jffs2_compressors_exit();
383 kmem_cache_destroy(jffs2_inode_cachep);
387 static void __exit exit_jffs2_fs(void)
389 unregister_filesystem(&jffs2_fs_type);
390 jffs2_destroy_slab_caches();
391 jffs2_compressors_exit();
392 kmem_cache_destroy(jffs2_inode_cachep);
395 module_init(init_jffs2_fs);
396 module_exit(exit_jffs2_fs);
398 MODULE_DESCRIPTION("The Journalling Flash File System, v2");
399 MODULE_AUTHOR("Red Hat, Inc.");
400 MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for
401 // the sake of this tag. It's Free Software.