blob: e3c69e659efb100d5134adc579c08b731fbb5eab [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
David Woodhousec00c3102007-04-25 14:16:47 +01004 * Copyright © 2001-2007 Red Hat, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Created by David Woodhouse <dwmw2@infradead.org>
7 *
8 * For licensing information, see the file 'LICENCE' in this directory.
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/slab.h>
15#include <linux/init.h>
16#include <linux/list.h>
17#include <linux/fs.h>
Artem Bityutskiy9c740342006-10-11 14:52:47 +030018#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/mount.h>
20#include <linux/jffs2.h>
21#include <linux/pagemap.h>
David Howellsacaebfd2007-05-10 22:51:50 -070022#include <linux/mtd/super.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/ctype.h>
24#include <linux/namei.h>
25#include "compr.h"
26#include "nodelist.h"
27
28static void jffs2_put_super(struct super_block *);
29
Christoph Lametere18b8902006-12-06 20:33:20 -080030static struct kmem_cache *jffs2_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32static struct inode *jffs2_alloc_inode(struct super_block *sb)
33{
34 struct jffs2_inode_info *ei;
Christoph Lametere94b1762006-12-06 20:33:17 -080035 ei = (struct jffs2_inode_info *)kmem_cache_alloc(jffs2_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 if (!ei)
37 return NULL;
38 return &ei->vfs_inode;
39}
40
41static void jffs2_destroy_inode(struct inode *inode)
42{
43 kmem_cache_free(jffs2_inode_cachep, JFFS2_INODE_INFO(inode));
44}
45
Christoph Lametere18b8902006-12-06 20:33:20 -080046static void jffs2_i_init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 struct jffs2_inode_info *ei = (struct jffs2_inode_info *) foo;
49
50 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
51 SLAB_CTOR_CONSTRUCTOR) {
Thomas Gleixner21eeb7a2005-11-29 16:57:17 +010052 init_MUTEX(&ei->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 inode_init_once(&ei->vfs_inode);
54 }
55}
56
57static int jffs2_sync_fs(struct super_block *sb, int wait)
58{
59 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
60
61 down(&c->alloc_sem);
62 jffs2_flush_wbuf_pad(c);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000063 up(&c->alloc_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 return 0;
65}
66
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -080067static const struct super_operations jffs2_super_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 .alloc_inode = jffs2_alloc_inode,
70 .destroy_inode =jffs2_destroy_inode,
71 .read_inode = jffs2_read_inode,
72 .put_super = jffs2_put_super,
73 .write_super = jffs2_write_super,
74 .statfs = jffs2_statfs,
75 .remount_fs = jffs2_remount_fs,
76 .clear_inode = jffs2_clear_inode,
77 .dirty_inode = jffs2_dirty_inode,
78 .sync_fs = jffs2_sync_fs,
79};
80
David Howellsacaebfd2007-05-10 22:51:50 -070081/*
82 * fill in the superblock
83 */
84static int jffs2_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 struct jffs2_sb_info *c;
David Howellsacaebfd2007-05-10 22:51:50 -070087
88 D1(printk(KERN_DEBUG "jffs2_get_sb_mtd():"
89 " New superblock for device %d (\"%s\")\n",
90 sb->s_mtd->index, sb->s_mtd->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -070092 c = kzalloc(sizeof(*c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if (!c)
David Howells454e2392006-06-23 02:02:57 -070094 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
David Howellsacaebfd2007-05-10 22:51:50 -070096 c->mtd = sb->s_mtd;
97 c->os_priv = sb;
98 sb->s_fs_info = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
David Howellsacaebfd2007-05-10 22:51:50 -0700100 /* Initialize JFFS2 superblock locks, the further initialization will
101 * be done later */
Artem B. Bityuckiyb6220592005-07-12 17:37:12 +0100102 init_MUTEX(&c->alloc_sem);
103 init_MUTEX(&c->erase_free_sem);
104 init_waitqueue_head(&c->erase_wait);
105 init_waitqueue_head(&c->inocache_wq);
106 spin_lock_init(&c->erase_completion_lock);
107 spin_lock_init(&c->inocache_lock);
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 sb->s_op = &jffs2_super_operations;
David Howellsacaebfd2007-05-10 22:51:50 -0700110 sb->s_flags = sb->s_flags | MS_NOATIME;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900111 sb->s_xattr = jffs2_xattr_handlers;
112#ifdef CONFIG_JFFS2_FS_POSIX_ACL
113 sb->s_flags |= MS_POSIXACL;
114#endif
David Howellsacaebfd2007-05-10 22:51:50 -0700115 return jffs2_do_fill_super(sb, data, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
David Howells454e2392006-06-23 02:02:57 -0700118static int jffs2_get_sb(struct file_system_type *fs_type,
119 int flags, const char *dev_name,
120 void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
David Howellsacaebfd2007-05-10 22:51:50 -0700122 return get_sb_mtd(fs_type, flags, dev_name, data, jffs2_fill_super,
123 mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126static void jffs2_put_super (struct super_block *sb)
127{
128 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
129
130 D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 down(&c->alloc_sem);
133 jffs2_flush_wbuf_pad(c);
134 up(&c->alloc_sem);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100135
136 jffs2_sum_exit(c);
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 jffs2_free_ino_caches(c);
139 jffs2_free_raw_node_refs(c);
Ferenc Havasi4ce1f562005-08-31 14:51:04 +0100140 if (jffs2_blocks_use_vmalloc(c))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 vfree(c->blocks);
142 else
143 kfree(c->blocks);
144 jffs2_flash_cleanup(c);
145 kfree(c->inocache_list);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900146 jffs2_clear_xattr_subsystem(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (c->mtd->sync)
148 c->mtd->sync(c->mtd);
149
150 D1(printk(KERN_DEBUG "jffs2_put_super returning\n"));
151}
152
153static void jffs2_kill_sb(struct super_block *sb)
154{
155 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
Artem B. Bityuckiya69dde92005-05-18 12:37:28 +0100156 if (!(sb->s_flags & MS_RDONLY))
157 jffs2_stop_garbage_collect_thread(c);
David Howellsacaebfd2007-05-10 22:51:50 -0700158 kill_mtd_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 kfree(c);
160}
161
162static struct file_system_type jffs2_fs_type = {
163 .owner = THIS_MODULE,
164 .name = "jffs2",
165 .get_sb = jffs2_get_sb,
166 .kill_sb = jffs2_kill_sb,
167};
168
169static int __init init_jffs2_fs(void)
170{
171 int ret;
172
David Woodhouse3e68fbb2006-05-15 00:49:43 +0100173 /* Paranoia checks for on-medium structures. If we ask GCC
174 to pack them with __attribute__((packed)) then it _also_
175 assumes that they're not aligned -- so it emits crappy
176 code on some architectures. Ideally we want an attribute
177 which means just 'no padding', without the alignment
178 thing. But GCC doesn't have that -- we have to just
179 hope the structs are the right sizes, instead. */
Alexey Dobriyan2ecd05a2006-10-11 01:22:05 -0700180 BUILD_BUG_ON(sizeof(struct jffs2_unknown_node) != 12);
181 BUILD_BUG_ON(sizeof(struct jffs2_raw_dirent) != 40);
182 BUILD_BUG_ON(sizeof(struct jffs2_raw_inode) != 68);
183 BUILD_BUG_ON(sizeof(struct jffs2_raw_summary) != 32);
David Woodhouse3e68fbb2006-05-15 00:49:43 +0100184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 printk(KERN_INFO "JFFS2 version 2.2."
Andrew Victor2f82ce12005-02-09 09:24:26 +0000186#ifdef CONFIG_JFFS2_FS_WRITEBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 " (NAND)"
188#endif
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100189#ifdef CONFIG_JFFS2_SUMMARY
190 " (SUMMARY) "
191#endif
David Woodhousec00c3102007-04-25 14:16:47 +0100192 " © 2001-2006 Red Hat, Inc.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 jffs2_inode_cachep = kmem_cache_create("jffs2_i",
195 sizeof(struct jffs2_inode_info),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800196 0, (SLAB_RECLAIM_ACCOUNT|
197 SLAB_MEM_SPREAD),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 jffs2_i_init_once, NULL);
199 if (!jffs2_inode_cachep) {
200 printk(KERN_ERR "JFFS2 error: Failed to initialise inode cache\n");
201 return -ENOMEM;
202 }
203 ret = jffs2_compressors_init();
204 if (ret) {
205 printk(KERN_ERR "JFFS2 error: Failed to initialise compressors\n");
206 goto out;
207 }
208 ret = jffs2_create_slab_caches();
209 if (ret) {
210 printk(KERN_ERR "JFFS2 error: Failed to initialise slab caches\n");
211 goto out_compressors;
212 }
213 ret = register_filesystem(&jffs2_fs_type);
214 if (ret) {
215 printk(KERN_ERR "JFFS2 error: Failed to register filesystem\n");
216 goto out_slab;
217 }
218 return 0;
219
220 out_slab:
221 jffs2_destroy_slab_caches();
222 out_compressors:
223 jffs2_compressors_exit();
224 out:
225 kmem_cache_destroy(jffs2_inode_cachep);
226 return ret;
227}
228
229static void __exit exit_jffs2_fs(void)
230{
231 unregister_filesystem(&jffs2_fs_type);
232 jffs2_destroy_slab_caches();
233 jffs2_compressors_exit();
234 kmem_cache_destroy(jffs2_inode_cachep);
235}
236
237module_init(init_jffs2_fs);
238module_exit(exit_jffs2_fs);
239
240MODULE_DESCRIPTION("The Journalling Flash File System, v2");
241MODULE_AUTHOR("Red Hat, Inc.");
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000242MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 // the sake of this tag. It's Free Software.