2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
8 * The original JFFS, from which the design for JFFS2 was derived,
9 * was designed and implemented by Axis Communications AB.
11 * The contents of this file are subject to the Red Hat eCos Public
12 * License Version 1.1 (the "Licence"); you may not use this file
13 * except in compliance with the Licence. You may obtain a copy of
14 * the Licence at http://www.redhat.com/
16 * Software distributed under the Licence is distributed on an "AS IS"
17 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
18 * See the Licence for the specific language governing rights and
19 * limitations under the Licence.
21 * The Original Code is JFFS2 - Journalling Flash File System, version 2
23 * Alternatively, the contents of this file may be used under the
24 * terms of the GNU General Public License version 2 (the "GPL"), in
25 * which case the provisions of the GPL are applicable instead of the
26 * above. If you wish to allow the use of your version of this file
27 * only under the terms of the GPL and not to allow others to use your
28 * version of this file under the RHEPL, indicate your decision by
29 * deleting the provisions above and replace them with the notice and
30 * other provisions required by the GPL. If you do not delete the
31 * provisions above, a recipient may use your version of this file
32 * under either the RHEPL or the GPL.
34 * $Id: super.c,v 1.48.2.3 2002/10/11 09:04:44 dwmw2 Exp $
38 #include <linux/config.h>
39 #include <linux/kernel.h>
40 #include <linux/module.h>
41 #include <linux/version.h>
42 #include <linux/slab.h>
43 #include <linux/init.h>
44 #include <linux/list.h>
46 #include <linux/jffs2.h>
47 #include <linux/pagemap.h>
48 #include <linux/mtd/mtd.h>
49 #include <linux/interrupt.h>
52 #ifndef MTD_BLOCK_MAJOR
53 #define MTD_BLOCK_MAJOR 31
56 extern void jffs2_read_inode (struct inode *);
57 void jffs2_put_super (struct super_block *);
58 void jffs2_write_super (struct super_block *);
59 static int jffs2_statfs (struct super_block *, struct statfs *);
60 int jffs2_remount_fs (struct super_block *, int *, char *);
61 extern void jffs2_clear_inode (struct inode *);
63 static struct super_operations jffs2_super_operations =
65 read_inode: jffs2_read_inode,
66 // delete_inode: jffs2_delete_inode,
67 put_super: jffs2_put_super,
68 write_super: jffs2_write_super,
70 remount_fs: jffs2_remount_fs,
71 clear_inode: jffs2_clear_inode
74 static int jffs2_statfs(struct super_block *sb, struct statfs *buf)
76 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
79 buf->f_type = JFFS2_SUPER_MAGIC;
80 buf->f_bsize = 1 << PAGE_SHIFT;
81 buf->f_blocks = c->flash_size >> PAGE_SHIFT;
84 buf->f_namelen = JFFS2_MAX_NAME_LEN;
86 spin_lock_bh(&c->erase_completion_lock);
88 avail = c->dirty_size + c->free_size;
89 if (avail > c->sector_size * JFFS2_RESERVED_BLOCKS_WRITE)
90 avail -= c->sector_size * JFFS2_RESERVED_BLOCKS_WRITE;
94 buf->f_bavail = buf->f_bfree = avail >> PAGE_SHIFT;
96 #if CONFIG_JFFS2_FS_DEBUG > 0
97 printk(KERN_DEBUG "STATFS:\n");
98 printk(KERN_DEBUG "flash_size: %08x\n", c->flash_size);
99 printk(KERN_DEBUG "used_size: %08x\n", c->used_size);
100 printk(KERN_DEBUG "dirty_size: %08x\n", c->dirty_size);
101 printk(KERN_DEBUG "free_size: %08x\n", c->free_size);
102 printk(KERN_DEBUG "erasing_size: %08x\n", c->erasing_size);
103 printk(KERN_DEBUG "bad_size: %08x\n", c->bad_size);
104 printk(KERN_DEBUG "sector_size: %08x\n", c->sector_size);
107 printk(KERN_DEBUG "nextblock: 0x%08x\n", c->nextblock->offset);
109 printk(KERN_DEBUG "nextblock: NULL\n");
112 printk(KERN_DEBUG "gcblock: 0x%08x\n", c->gcblock->offset);
114 printk(KERN_DEBUG "gcblock: NULL\n");
116 if (list_empty(&c->clean_list)) {
117 printk(KERN_DEBUG "clean_list: empty\n");
119 struct list_head *this;
121 list_for_each(this, &c->clean_list) {
122 struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
123 printk(KERN_DEBUG "clean_list: %08x\n", jeb->offset);
126 if (list_empty(&c->dirty_list)) {
127 printk(KERN_DEBUG "dirty_list: empty\n");
129 struct list_head *this;
131 list_for_each(this, &c->dirty_list) {
132 struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
133 printk(KERN_DEBUG "dirty_list: %08x\n", jeb->offset);
136 if (list_empty(&c->erasing_list)) {
137 printk(KERN_DEBUG "erasing_list: empty\n");
139 struct list_head *this;
141 list_for_each(this, &c->erasing_list) {
142 struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
143 printk(KERN_DEBUG "erasing_list: %08x\n", jeb->offset);
146 if (list_empty(&c->erase_pending_list)) {
147 printk(KERN_DEBUG "erase_pending_list: empty\n");
149 struct list_head *this;
151 list_for_each(this, &c->erase_pending_list) {
152 struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
153 printk(KERN_DEBUG "erase_pending_list: %08x\n", jeb->offset);
156 if (list_empty(&c->free_list)) {
157 printk(KERN_DEBUG "free_list: empty\n");
159 struct list_head *this;
161 list_for_each(this, &c->free_list) {
162 struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
163 printk(KERN_DEBUG "free_list: %08x\n", jeb->offset);
166 if (list_empty(&c->bad_list)) {
167 printk(KERN_DEBUG "bad_list: empty\n");
169 struct list_head *this;
171 list_for_each(this, &c->bad_list) {
172 struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
173 printk(KERN_DEBUG "bad_list: %08x\n", jeb->offset);
176 if (list_empty(&c->bad_used_list)) {
177 printk(KERN_DEBUG "bad_used_list: empty\n");
179 struct list_head *this;
181 list_for_each(this, &c->bad_used_list) {
182 struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
183 printk(KERN_DEBUG "bad_used_list: %08x\n", jeb->offset);
186 #endif /* CONFIG_JFFS2_FS_DEBUG */
188 spin_unlock_bh(&c->erase_completion_lock);
194 static struct super_block *jffs2_read_super(struct super_block *sb, void *data, int silent)
196 struct jffs2_sb_info *c;
197 struct inode *root_i;
200 D1(printk(KERN_DEBUG "jffs2: read_super for device %s\n", kdevname(sb->s_dev)));
202 if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) {
204 printk(KERN_DEBUG "jffs2: attempt to mount non-MTD device %s\n", kdevname(sb->s_dev));
208 c = JFFS2_SB_INFO(sb);
209 memset(c, 0, sizeof(*c));
211 c->mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
213 D1(printk(KERN_DEBUG "jffs2: MTD device #%u doesn't appear to exist\n", MINOR(sb->s_dev)));
216 c->sector_size = c->mtd->erasesize;
217 c->free_size = c->flash_size = c->mtd->size;
218 c->nr_blocks = c->mtd->size / c->mtd->erasesize;
219 c->blocks = kmalloc(sizeof(struct jffs2_eraseblock) * c->nr_blocks, GFP_KERNEL);
222 for (i=0; i<c->nr_blocks; i++) {
223 INIT_LIST_HEAD(&c->blocks[i].list);
224 c->blocks[i].offset = i * c->sector_size;
225 c->blocks[i].free_size = c->sector_size;
226 c->blocks[i].dirty_size = 0;
227 c->blocks[i].used_size = 0;
228 c->blocks[i].first_node = NULL;
229 c->blocks[i].last_node = NULL;
232 spin_lock_init(&c->nodelist_lock);
233 init_MUTEX(&c->alloc_sem);
234 init_waitqueue_head(&c->erase_wait);
235 spin_lock_init(&c->erase_completion_lock);
236 spin_lock_init(&c->inocache_lock);
238 INIT_LIST_HEAD(&c->clean_list);
239 INIT_LIST_HEAD(&c->dirty_list);
240 INIT_LIST_HEAD(&c->erasing_list);
241 INIT_LIST_HEAD(&c->erase_pending_list);
242 INIT_LIST_HEAD(&c->erase_complete_list);
243 INIT_LIST_HEAD(&c->free_list);
244 INIT_LIST_HEAD(&c->bad_list);
245 INIT_LIST_HEAD(&c->bad_used_list);
248 if (jffs2_build_filesystem(c)) {
249 D1(printk(KERN_DEBUG "build_fs failed\n"));
253 sb->s_op = &jffs2_super_operations;
255 D1(printk(KERN_DEBUG "jffs2_read_super(): Getting root inode\n"));
256 root_i = iget(sb, 1);
257 if (is_bad_inode(root_i)) {
258 D1(printk(KERN_WARNING "get root inode failed\n"));
262 D1(printk(KERN_DEBUG "jffs2_read_super(): d_alloc_root()\n"));
263 sb->s_root = d_alloc_root(root_i);
267 #if LINUX_VERSION_CODE >= 0x20403
268 sb->s_maxbytes = 0xFFFFFFFF;
270 sb->s_blocksize = PAGE_CACHE_SIZE;
271 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
272 sb->s_magic = JFFS2_SUPER_MAGIC;
273 if (!(sb->s_flags & MS_RDONLY))
274 jffs2_start_garbage_collect_thread(c);
280 jffs2_free_ino_caches(c);
281 jffs2_free_raw_node_refs(c);
284 put_mtd_device(c->mtd);
288 void jffs2_put_super (struct super_block *sb)
290 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
292 D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));
294 if (!(sb->s_flags & MS_RDONLY))
295 jffs2_stop_garbage_collect_thread(c);
296 jffs2_free_ino_caches(c);
297 jffs2_free_raw_node_refs(c);
300 c->mtd->sync(c->mtd);
301 put_mtd_device(c->mtd);
303 D1(printk(KERN_DEBUG "jffs2_put_super returning\n"));
306 int jffs2_remount_fs (struct super_block *sb, int *flags, char *data)
308 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
310 if (c->flags & JFFS2_SB_FLAG_RO && !(sb->s_flags & MS_RDONLY))
313 /* We stop if it was running, then restart if it needs to.
314 This also catches the case where it was stopped and this
315 is just a remount to restart it */
316 if (!(sb->s_flags & MS_RDONLY))
317 jffs2_stop_garbage_collect_thread(c);
319 if (!(*flags & MS_RDONLY))
320 jffs2_start_garbage_collect_thread(c);
322 sb->s_flags = (sb->s_flags & ~MS_RDONLY)|(*flags & MS_RDONLY);
327 void jffs2_write_super (struct super_block *sb)
329 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
332 if (sb->s_flags & MS_RDONLY)
335 jffs2_garbage_collect_trigger(c);
336 jffs2_erase_pending_blocks(c);
337 jffs2_mark_erased_blocks(c);
341 static DECLARE_FSTYPE_DEV(jffs2_fs_type, "jffs2", jffs2_read_super);
343 static int __init init_jffs2_fs(void)
347 printk(KERN_NOTICE "JFFS2 version 2.1. (C) 2001 Red Hat, Inc., designed by Axis Communications AB.\n");
349 #ifdef JFFS2_OUT_OF_KERNEL
350 /* sanity checks. Could we do these at compile time? */
351 if (sizeof(struct jffs2_sb_info) > sizeof (((struct super_block *)NULL)->u)) {
352 printk(KERN_ERR "JFFS2 error: struct jffs2_sb_info (%d bytes) doesn't fit in the super_block union (%d bytes)\n",
353 sizeof(struct jffs2_sb_info), sizeof (((struct super_block *)NULL)->u));
357 if (sizeof(struct jffs2_inode_info) > sizeof (((struct inode *)NULL)->u)) {
358 printk(KERN_ERR "JFFS2 error: struct jffs2_inode_info (%d bytes) doesn't fit in the inode union (%d bytes)\n",
359 sizeof(struct jffs2_inode_info), sizeof (((struct inode *)NULL)->u));
364 ret = jffs2_zlib_init();
366 printk(KERN_ERR "JFFS2 error: Failed to initialise zlib workspaces\n");
369 ret = jffs2_create_slab_caches();
371 printk(KERN_ERR "JFFS2 error: Failed to initialise slab caches\n");
374 ret = register_filesystem(&jffs2_fs_type);
376 printk(KERN_ERR "JFFS2 error: Failed to register filesystem\n");
382 jffs2_destroy_slab_caches();
389 static void __exit exit_jffs2_fs(void)
391 jffs2_destroy_slab_caches();
393 unregister_filesystem(&jffs2_fs_type);
396 module_init(init_jffs2_fs);
397 module_exit(exit_jffs2_fs);
399 MODULE_DESCRIPTION("The Journalling Flash File System, v2");
400 MODULE_AUTHOR("Red Hat, Inc.");
401 MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for
402 // the sake of this tag. It's Free Software.