more testing
[yaffs/.git] / yaffs_super.c
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright (C) 2001 Red Hat, Inc.
5  *
6  * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
7  *
8  * The original JFFS, from which the design for JFFS2 was derived,
9  * was designed and implemented by Axis Communications AB.
10  *
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/
15  *
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.
20  *
21  * The Original Code is JFFS2 - Journalling Flash File System, version 2
22  *
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.
33  *
34  * $Id: yaffs_super.c,v 1.1 2002-05-20 17:42:08 aleph1 Exp $
35  *
36  */
37
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>
45 #include <linux/fs.h>
46 #include <linux/jffs2.h>
47 #include <linux/pagemap.h>
48 #include <linux/mtd/mtd.h>
49 #include <linux/interrupt.h>
50 #include "nodelist.h"
51
52 #ifndef MTD_BLOCK_MAJOR
53 #define MTD_BLOCK_MAJOR 31
54 #endif
55
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 *);
62
63 static struct super_operations jffs2_super_operations =
64 {
65         read_inode:     jffs2_read_inode,
66 //      delete_inode:   jffs2_delete_inode,
67         put_super:      jffs2_put_super,
68         write_super:    jffs2_write_super,
69         statfs:         jffs2_statfs,
70         remount_fs:     jffs2_remount_fs,
71         clear_inode:    jffs2_clear_inode
72 };
73
74 static int jffs2_statfs(struct super_block *sb, struct statfs *buf)
75 {
76         struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
77         unsigned long avail;
78
79         buf->f_type = JFFS2_SUPER_MAGIC;
80         buf->f_bsize = 1 << PAGE_SHIFT;
81         buf->f_blocks = c->flash_size >> PAGE_SHIFT;
82         buf->f_files = 0;
83         buf->f_ffree = 0;
84         buf->f_namelen = JFFS2_MAX_NAME_LEN;
85
86         spin_lock_bh(&c->erase_completion_lock);
87
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;
91         else
92                 avail = 0;
93
94         buf->f_bavail = buf->f_bfree = avail >> PAGE_SHIFT;
95
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);
105
106         if (c->nextblock) {
107                 printk(KERN_DEBUG "nextblock: 0x%08x\n", c->nextblock->offset);
108         } else {
109                 printk(KERN_DEBUG "nextblock: NULL\n");
110         }
111         if (c->gcblock) {
112                 printk(KERN_DEBUG "gcblock: 0x%08x\n", c->gcblock->offset);
113         } else {
114                 printk(KERN_DEBUG "gcblock: NULL\n");
115         }
116         if (list_empty(&c->clean_list)) {
117                 printk(KERN_DEBUG "clean_list: empty\n");
118         } else {
119                 struct list_head *this;
120
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);
124                 }
125         }
126         if (list_empty(&c->dirty_list)) {
127                 printk(KERN_DEBUG "dirty_list: empty\n");
128         } else {
129                 struct list_head *this;
130
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);
134                 }
135         }
136         if (list_empty(&c->erasing_list)) {
137                 printk(KERN_DEBUG "erasing_list: empty\n");
138         } else {
139                 struct list_head *this;
140
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);
144                 }
145         }
146         if (list_empty(&c->erase_pending_list)) {
147                 printk(KERN_DEBUG "erase_pending_list: empty\n");
148         } else {
149                 struct list_head *this;
150
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);
154                 }
155         }
156         if (list_empty(&c->free_list)) {
157                 printk(KERN_DEBUG "free_list: empty\n");
158         } else {
159                 struct list_head *this;
160
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);
164                 }
165         }
166         if (list_empty(&c->bad_list)) {
167                 printk(KERN_DEBUG "bad_list: empty\n");
168         } else {
169                 struct list_head *this;
170
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);
174                 }
175         }
176         if (list_empty(&c->bad_used_list)) {
177                 printk(KERN_DEBUG "bad_used_list: empty\n");
178         } else {
179                 struct list_head *this;
180
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);
184                 }
185         }
186 #endif /* CONFIG_JFFS2_FS_DEBUG */
187
188         spin_unlock_bh(&c->erase_completion_lock);
189
190
191         return 0;
192 }
193
194 static struct super_block *jffs2_read_super(struct super_block *sb, void *data, int silent)
195 {
196         struct jffs2_sb_info *c;
197         struct inode *root_i;
198         int i;
199
200         D1(printk(KERN_DEBUG "jffs2: read_super for device %s\n", kdevname(sb->s_dev)));
201
202         if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) {
203                 if (!silent)
204                         printk(KERN_DEBUG "jffs2: attempt to mount non-MTD device %s\n", kdevname(sb->s_dev));
205                 return NULL;
206         }
207
208         c = JFFS2_SB_INFO(sb);
209         memset(c, 0, sizeof(*c));
210         
211         c->mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
212         if (!c->mtd) {
213                 D1(printk(KERN_DEBUG "jffs2: MTD device #%u doesn't appear to exist\n", MINOR(sb->s_dev)));
214                 return NULL;
215         }
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);
220         if (!c->blocks)
221                 goto out_mtd;
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;
230         }
231                 
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);
237
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);
246         c->highest_ino = 1;
247
248         if (jffs2_build_filesystem(c)) {
249                 D1(printk(KERN_DEBUG "build_fs failed\n"));
250                 goto out_nodes;
251         }
252         sb->s_op = &jffs2_super_operations;
253
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"));
258                 goto out_nodes;
259         }
260
261         D1(printk(KERN_DEBUG "jffs2_read_super(): d_alloc_root()\n"));
262         sb->s_root = d_alloc_root(root_i);
263         if (!sb->s_root)
264                 goto out_root_i;
265
266 #if LINUX_VERSION_CODE >= 0x20403
267         sb->s_maxbytes = 0xFFFFFFFF;
268 #endif
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);
274         return sb;
275
276  out_root_i:
277         iput(root_i);
278  out_nodes:
279         jffs2_free_ino_caches(c);
280         jffs2_free_raw_node_refs(c);
281         kfree(c->blocks);
282  out_mtd:
283         put_mtd_device(c->mtd);
284         return NULL;
285 }
286
287 void jffs2_put_super (struct super_block *sb)
288 {
289         struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
290
291         D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));
292
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);
297         kfree(c->blocks);
298         if (c->mtd->sync)
299                 c->mtd->sync(c->mtd);
300         put_mtd_device(c->mtd);
301         
302         D1(printk(KERN_DEBUG "jffs2_put_super returning\n"));
303 }
304
305 int jffs2_remount_fs (struct super_block *sb, int *flags, char *data)
306 {
307         struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
308
309         if (c->flags & JFFS2_SB_FLAG_RO && !(sb->s_flags & MS_RDONLY))
310                 return -EROFS;
311
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);
317
318         if (!(*flags & MS_RDONLY))
319                 jffs2_start_garbage_collect_thread(c);
320         
321         sb->s_flags = (sb->s_flags & ~MS_RDONLY)|(*flags & MS_RDONLY);
322
323         return 0;
324 }
325
326 void jffs2_write_super (struct super_block *sb)
327 {
328         struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
329         sb->s_dirt = 0;
330
331         if (sb->s_flags & MS_RDONLY)
332                 return;
333
334         jffs2_garbage_collect_trigger(c);
335         jffs2_erase_pending_blocks(c);
336         jffs2_mark_erased_blocks(c);
337 }
338
339
340 static DECLARE_FSTYPE_DEV(jffs2_fs_type, "jffs2", jffs2_read_super);
341
342 static int __init init_jffs2_fs(void)
343 {
344         int ret;
345
346         printk(KERN_NOTICE "JFFS2 version 2.1. (C) 2001 Red Hat, Inc., designed by Axis Communications AB.\n");
347
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));
353                 return -EIO;
354         }
355
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));
359                 return -EIO;
360         }
361 #endif
362
363         ret = jffs2_create_slab_caches();
364         if (ret) {
365                 printk(KERN_ERR "JFFS2 error: Failed to initialise slab caches\n");
366                 return ret;
367         }
368         ret = register_filesystem(&jffs2_fs_type);
369         if (ret) {
370                 printk(KERN_ERR "JFFS2 error: Failed to register filesystem\n");
371                 jffs2_destroy_slab_caches();
372         }
373         return ret;
374 }
375
376 static void __exit exit_jffs2_fs(void)
377 {
378         jffs2_destroy_slab_caches();
379         unregister_filesystem(&jffs2_fs_type);
380 }
381
382 module_init(init_jffs2_fs);
383 module_exit(exit_jffs2_fs);
384
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.
389 /*
390  * YAFFS: Yet another FFS. A NAND-flash specific file system. 
391  *
392  * Copyright (C) 2002 Aleph One Ltd.
393  *   for Toby Churchill Ltd and Brightstar Engineering
394  *
395  * Created by Charles Manning <charles@aleph1.co.uk>
396  *
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.
400  *
401  */