2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001-2003 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@redhat.com>
8 * For licensing information, see the file 'LICENCE' in this directory.
10 * $Id: super.c,v 1.99 2004/08/24 07:59:57 dwmw2 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 struct super_operations jffs2_super_operations =
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,
72 static int jffs2_sb_compare(struct super_block *sb, void *data)
74 struct jffs2_sb_info *p = data;
75 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
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));
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));
89 static int jffs2_sb_set(struct super_block *sb, void *data)
91 struct jffs2_sb_info *p = data;
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
98 sb->s_dev = MKDEV(MTD_BLOCK_MAJOR, p->mtd->index);
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)
107 struct super_block *sb;
108 struct jffs2_sb_info *c;
111 c = kmalloc(sizeof(*c), GFP_KERNEL);
113 return ERR_PTR(-ENOMEM);
114 memset(c, 0, sizeof(*c));
117 sb = sget(fs_type, jffs2_sb_compare, jffs2_sb_set, c);
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));
129 D1(printk(KERN_DEBUG "jffs2_get_sb_mtd(): New superblock for device %d (\"%s\")\n",
130 mtd->index, mtd->name));
132 sb->s_op = &jffs2_super_operations;
133 sb->s_flags = flags | MS_NOATIME;
135 ret = jffs2_do_fill_super(sb, data, (flags&MS_VERBOSE)?1:0);
138 /* Failure case... */
139 up_write(&sb->s_umount);
140 deactivate_super(sb);
144 sb->s_flags |= MS_ACTIVE;
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)
158 struct mtd_info *mtd;
160 mtd = get_mtd_device(NULL, mtdnr);
162 D1(printk(KERN_DEBUG "jffs2: MTD device #%u doesn't appear to exist\n", mtdnr));
163 return ERR_PTR(-EINVAL);
166 return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd);
169 static struct super_block *jffs2_get_sb(struct file_system_type *fs_type,
170 int flags, const char *dev_name,
178 return ERR_PTR(-EINVAL);
180 D1(printk(KERN_DEBUG "jffs2_get_sb(): dev_name \"%s\"\n", dev_name));
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. */
187 /* FIXME: How to do the root fs this way? */
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;
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);
199 if (!strcmp(mtd->name, dev_name+4))
200 return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd);
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 */
209 mtdnr = simple_strtoul(dev_name+3, &endptr, 0);
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);
218 /* Try the old way - the hack where we allowed users to mount
219 /dev/mtdblock$(n) but didn't actually _use_ the blkdev */
221 err = path_lookup(dev_name, LOOKUP_FOLLOW, &nd);
223 D1(printk(KERN_DEBUG "jffs2_get_sb(): path_lookup() returned %d, inode %p\n",
224 err, nd.dentry->d_inode));
231 if (!S_ISBLK(nd.dentry->d_inode->i_mode))
234 if (nd.mnt->mnt_flags & MNT_NODEV) {
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",
246 mtdnr = iminor(nd.dentry->d_inode);
249 return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr);
256 static void jffs2_put_super (struct super_block *sb)
258 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
260 D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));
262 if (!(sb->s_flags & MS_RDONLY))
263 jffs2_stop_garbage_collect_thread(c);
265 jffs2_flush_wbuf_pad(c);
267 jffs2_free_ino_caches(c);
268 jffs2_free_raw_node_refs(c);
270 jffs2_flash_cleanup(c);
271 kfree(c->inocache_list);
273 c->mtd->sync(c->mtd);
275 D1(printk(KERN_DEBUG "jffs2_put_super returning\n"));
278 static void jffs2_kill_sb(struct super_block *sb)
280 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
281 generic_shutdown_super(sb);
282 put_mtd_device(c->mtd);
286 static struct file_system_type jffs2_fs_type = {
287 .owner = THIS_MODULE,
289 .get_sb = jffs2_get_sb,
290 .kill_sb = jffs2_kill_sb,
293 static int __init init_jffs2_fs(void)
297 printk(KERN_INFO "JFFS2 version 2.2."
298 #ifdef CONFIG_JFFS2_FS_NAND
301 " (C) 2001-2003 Red Hat, Inc.\n");
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");
311 ret = jffs2_compressors_init();
313 printk(KERN_ERR "JFFS2 error: Failed to initialise compressors\n");
316 ret = jffs2_create_slab_caches();
318 printk(KERN_ERR "JFFS2 error: Failed to initialise slab caches\n");
319 goto out_compressors;
321 ret = register_filesystem(&jffs2_fs_type);
323 printk(KERN_ERR "JFFS2 error: Failed to register filesystem\n");
329 jffs2_destroy_slab_caches();
331 jffs2_compressors_exit();
333 kmem_cache_destroy(jffs2_inode_cachep);
337 static void __exit exit_jffs2_fs(void)
339 unregister_filesystem(&jffs2_fs_type);
340 jffs2_destroy_slab_caches();
341 jffs2_compressors_exit();
342 kmem_cache_destroy(jffs2_inode_cachep);
345 module_init(init_jffs2_fs);
346 module_exit(exit_jffs2_fs);
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.