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.104 2004/11/23 15:37:31 gleixner Exp $
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>
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>
30 static void jffs2_put_super(struct super_block *);
32 static kmem_cache_t *jffs2_inode_cachep;
34 static struct inode *jffs2_alloc_inode(struct super_block *sb)
36 struct jffs2_inode_info *ei;
37 ei = (struct jffs2_inode_info *)kmem_cache_alloc(jffs2_inode_cachep, SLAB_KERNEL);
40 return &ei->vfs_inode;
43 static void jffs2_destroy_inode(struct inode *inode)
45 kmem_cache_free(jffs2_inode_cachep, JFFS2_INODE_INFO(inode));
48 static void jffs2_i_init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
50 struct jffs2_inode_info *ei = (struct jffs2_inode_info *) foo;
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);
59 static int jffs2_sync_fs(struct super_block *sb, int wait)
61 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
64 jffs2_flush_wbuf_pad(c);
69 static struct super_operations jffs2_super_operations =
71 .alloc_inode = jffs2_alloc_inode,
72 .destroy_inode =jffs2_destroy_inode,
73 .read_inode = jffs2_read_inode,
74 .put_super = jffs2_put_super,
75 .write_super = jffs2_write_super,
76 .statfs = jffs2_statfs,
77 .remount_fs = jffs2_remount_fs,
78 .clear_inode = jffs2_clear_inode,
79 .dirty_inode = jffs2_dirty_inode,
80 .sync_fs = jffs2_sync_fs,
83 static int jffs2_sb_compare(struct super_block *sb, void *data)
85 struct jffs2_sb_info *p = data;
86 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
88 /* The superblocks are considered to be equivalent if the underlying MTD
89 device is the same one */
90 if (c->mtd == p->mtd) {
91 D1(printk(KERN_DEBUG "jffs2_sb_compare: match on device %d (\"%s\")\n", p->mtd->index, p->mtd->name));
94 D1(printk(KERN_DEBUG "jffs2_sb_compare: No match, device %d (\"%s\"), device %d (\"%s\")\n",
95 c->mtd->index, c->mtd->name, p->mtd->index, p->mtd->name));
100 static int jffs2_sb_set(struct super_block *sb, void *data)
102 struct jffs2_sb_info *p = data;
104 /* For persistence of NFS exports etc. we use the same s_dev
105 each time we mount the device, don't just use an anonymous
109 sb->s_dev = MKDEV(MTD_BLOCK_MAJOR, p->mtd->index);
114 static struct super_block *jffs2_get_sb_mtd(struct file_system_type *fs_type,
115 int flags, const char *dev_name,
116 void *data, struct mtd_info *mtd)
118 struct super_block *sb;
119 struct jffs2_sb_info *c;
122 c = kmalloc(sizeof(*c), GFP_KERNEL);
124 return ERR_PTR(-ENOMEM);
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));
140 D1(printk(KERN_DEBUG "jffs2_get_sb_mtd(): New superblock for device %d (\"%s\")\n",
141 mtd->index, mtd->name));
143 sb->s_op = &jffs2_super_operations;
144 sb->s_flags = flags | MS_NOATIME;
146 ret = jffs2_do_fill_super(sb, data, (flags&MS_VERBOSE)?1:0);
149 /* Failure case... */
150 up_write(&sb->s_umount);
151 deactivate_super(sb);
155 sb->s_flags |= MS_ACTIVE;
165 static struct super_block *jffs2_get_sb_mtdnr(struct file_system_type *fs_type,
166 int flags, const char *dev_name,
167 void *data, int mtdnr)
169 struct mtd_info *mtd;
171 mtd = get_mtd_device(NULL, mtdnr);
173 D1(printk(KERN_DEBUG "jffs2: MTD device #%u doesn't appear to exist\n", mtdnr));
174 return ERR_PTR(-EINVAL);
177 return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd);
180 static struct super_block *jffs2_get_sb(struct file_system_type *fs_type,
181 int flags, const char *dev_name,
189 return ERR_PTR(-EINVAL);
191 D1(printk(KERN_DEBUG "jffs2_get_sb(): dev_name \"%s\"\n", dev_name));
193 /* The preferred way of mounting in future; especially when
194 CONFIG_BLK_DEV is implemented - we specify the underlying
195 MTD device by number or by name, so that we don't require
196 block device support to be present in the kernel. */
198 /* FIXME: How to do the root fs this way? */
200 if (dev_name[0] == 'm' && dev_name[1] == 't' && dev_name[2] == 'd') {
201 /* Probably mounting without the blkdev crap */
202 if (dev_name[3] == ':') {
203 struct mtd_info *mtd;
205 /* Mount by MTD device name */
206 D1(printk(KERN_DEBUG "jffs2_get_sb(): mtd:%%s, name \"%s\"\n", dev_name+4));
207 for (mtdnr = 0; mtdnr < MAX_MTD_DEVICES; mtdnr++) {
208 mtd = get_mtd_device(NULL, mtdnr);
210 if (!strcmp(mtd->name, dev_name+4))
211 return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd);
215 printk(KERN_NOTICE "jffs2_get_sb(): MTD device with name \"%s\" not found.\n", dev_name+4);
216 } else if (isdigit(dev_name[3])) {
217 /* Mount by MTD device number name */
220 mtdnr = simple_strtoul(dev_name+3, &endptr, 0);
222 /* It was a valid number */
223 D1(printk(KERN_DEBUG "jffs2_get_sb(): mtd%%d, mtdnr %d\n", mtdnr));
224 return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr);
229 /* Try the old way - the hack where we allowed users to mount
230 /dev/mtdblock$(n) but didn't actually _use_ the blkdev */
232 err = path_lookup(dev_name, LOOKUP_FOLLOW, &nd);
234 D1(printk(KERN_DEBUG "jffs2_get_sb(): path_lookup() returned %d, inode %p\n",
235 err, nd.dentry->d_inode));
242 if (!S_ISBLK(nd.dentry->d_inode->i_mode))
245 if (nd.mnt->mnt_flags & MNT_NODEV) {
250 if (imajor(nd.dentry->d_inode) != MTD_BLOCK_MAJOR) {
251 if (!(flags & MS_VERBOSE)) /* Yes I mean this. Strangely */
252 printk(KERN_NOTICE "Attempt to mount non-MTD device \"%s\" as JFFS2\n",
257 mtdnr = iminor(nd.dentry->d_inode);
260 return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr);
267 static void jffs2_put_super (struct super_block *sb)
269 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
271 D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));
273 if (!(sb->s_flags & MS_RDONLY))
274 jffs2_stop_garbage_collect_thread(c);
276 jffs2_flush_wbuf_pad(c);
278 jffs2_free_ino_caches(c);
279 jffs2_free_raw_node_refs(c);
280 if (c->mtd->flags & MTD_NO_VIRTBLOCKS)
284 jffs2_flash_cleanup(c);
285 kfree(c->inocache_list);
287 c->mtd->sync(c->mtd);
289 D1(printk(KERN_DEBUG "jffs2_put_super returning\n"));
292 static void jffs2_kill_sb(struct super_block *sb)
294 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
295 generic_shutdown_super(sb);
296 put_mtd_device(c->mtd);
300 static struct file_system_type jffs2_fs_type = {
301 .owner = THIS_MODULE,
303 .get_sb = jffs2_get_sb,
304 .kill_sb = jffs2_kill_sb,
307 static int __init init_jffs2_fs(void)
311 printk(KERN_INFO "JFFS2 version 2.2."
312 #ifdef CONFIG_JFFS2_FS_NAND
315 " (C) 2001-2003 Red Hat, Inc.\n");
317 jffs2_inode_cachep = kmem_cache_create("jffs2_i",
318 sizeof(struct jffs2_inode_info),
319 0, SLAB_RECLAIM_ACCOUNT,
320 jffs2_i_init_once, NULL);
321 if (!jffs2_inode_cachep) {
322 printk(KERN_ERR "JFFS2 error: Failed to initialise inode cache\n");
325 ret = jffs2_compressors_init();
327 printk(KERN_ERR "JFFS2 error: Failed to initialise compressors\n");
330 ret = jffs2_create_slab_caches();
332 printk(KERN_ERR "JFFS2 error: Failed to initialise slab caches\n");
333 goto out_compressors;
335 ret = register_filesystem(&jffs2_fs_type);
337 printk(KERN_ERR "JFFS2 error: Failed to register filesystem\n");
343 jffs2_destroy_slab_caches();
345 jffs2_compressors_exit();
347 kmem_cache_destroy(jffs2_inode_cachep);
351 static void __exit exit_jffs2_fs(void)
353 unregister_filesystem(&jffs2_fs_type);
354 jffs2_destroy_slab_caches();
355 jffs2_compressors_exit();
356 kmem_cache_destroy(jffs2_inode_cachep);
359 module_init(init_jffs2_fs);
360 module_exit(exit_jffs2_fs);
362 MODULE_DESCRIPTION("The Journalling Flash File System, v2");
363 MODULE_AUTHOR("Red Hat, Inc.");
364 MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for
365 // the sake of this tag. It's Free Software.