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: yaffs_super.c,v 1.1 2002-05-20 17:42:08 aleph1 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"));
252 sb->s_op = &jffs2_super_operations;
254 D1(printk(KERN_DEBUG "jffs2_read_super(): Getting root inode\n"));
255 root_i = iget(sb, 1);
256 if (is_bad_inode(root_i)) {
257 D1(printk(KERN_WARNING "get root inode failed\n"));
261 D1(printk(KERN_DEBUG "jffs2_read_super(): d_alloc_root()\n"));
262 sb->s_root = d_alloc_root(root_i);
266 #if LINUX_VERSION_CODE >= 0x20403
267 sb->s_maxbytes = 0xFFFFFFFF;
269 sb->s_blocksize = PAGE_CACHE_SIZE;
270 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
271 sb->s_magic = JFFS2_SUPER_MAGIC;
272 if (!(sb->s_flags & MS_RDONLY))
273 jffs2_start_garbage_collect_thread(c);
279 jffs2_free_ino_caches(c);
280 jffs2_free_raw_node_refs(c);
283 put_mtd_device(c->mtd);
287 void jffs2_put_super (struct super_block *sb)
289 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
291 D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));
293 if (!(sb->s_flags & MS_RDONLY))
294 jffs2_stop_garbage_collect_thread(c);
295 jffs2_free_ino_caches(c);
296 jffs2_free_raw_node_refs(c);
299 c->mtd->sync(c->mtd);
300 put_mtd_device(c->mtd);
302 D1(printk(KERN_DEBUG "jffs2_put_super returning\n"));
305 int jffs2_remount_fs (struct super_block *sb, int *flags, char *data)
307 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
309 if (c->flags & JFFS2_SB_FLAG_RO && !(sb->s_flags & MS_RDONLY))
312 /* We stop if it was running, then restart if it needs to.
313 This also catches the case where it was stopped and this
314 is just a remount to restart it */
315 if (!(sb->s_flags & MS_RDONLY))
316 jffs2_stop_garbage_collect_thread(c);
318 if (!(*flags & MS_RDONLY))
319 jffs2_start_garbage_collect_thread(c);
321 sb->s_flags = (sb->s_flags & ~MS_RDONLY)|(*flags & MS_RDONLY);
326 void jffs2_write_super (struct super_block *sb)
328 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
331 if (sb->s_flags & MS_RDONLY)
334 jffs2_garbage_collect_trigger(c);
335 jffs2_erase_pending_blocks(c);
336 jffs2_mark_erased_blocks(c);
340 static DECLARE_FSTYPE_DEV(jffs2_fs_type, "jffs2", jffs2_read_super);
342 static int __init init_jffs2_fs(void)
346 printk(KERN_NOTICE "JFFS2 version 2.1. (C) 2001 Red Hat, Inc., designed by Axis Communications AB.\n");
348 #ifdef JFFS2_OUT_OF_KERNEL
349 /* sanity checks. Could we do these at compile time? */
350 if (sizeof(struct jffs2_sb_info) > sizeof (((struct super_block *)NULL)->u)) {
351 printk(KERN_ERR "JFFS2 error: struct jffs2_sb_info (%d bytes) doesn't fit in the super_block union (%d bytes)\n",
352 sizeof(struct jffs2_sb_info), sizeof (((struct super_block *)NULL)->u));
356 if (sizeof(struct jffs2_inode_info) > sizeof (((struct inode *)NULL)->u)) {
357 printk(KERN_ERR "JFFS2 error: struct jffs2_inode_info (%d bytes) doesn't fit in the inode union (%d bytes)\n",
358 sizeof(struct jffs2_inode_info), sizeof (((struct inode *)NULL)->u));
363 ret = jffs2_create_slab_caches();
365 printk(KERN_ERR "JFFS2 error: Failed to initialise slab caches\n");
368 ret = register_filesystem(&jffs2_fs_type);
370 printk(KERN_ERR "JFFS2 error: Failed to register filesystem\n");
371 jffs2_destroy_slab_caches();
376 static void __exit exit_jffs2_fs(void)
378 jffs2_destroy_slab_caches();
379 unregister_filesystem(&jffs2_fs_type);
382 module_init(init_jffs2_fs);
383 module_exit(exit_jffs2_fs);
385 MODULE_DESCRIPTION("The Journalling Flash File System, v2");
386 MODULE_AUTHOR("Red Hat, Inc.");
387 MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for
388 // the sake of this tag. It's Free Software.
390 * YAFFS: Yet another FFS. A NAND-flash specific file system.
392 * Copyright (C) 2002 Aleph One Ltd.
393 * for Toby Churchill Ltd and Brightstar Engineering
395 * Created by Charles Manning <charles@aleph1.co.uk>
397 * This program is free software; you can redistribute it and/or modify
398 * it under the terms of the GNU General Public License version 2 as
399 * published by the Free Software Foundation.