v2.4.9.9 -> v2.4.9.10
[opensuse:kernel.git] / fs / jffs2 / 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: super.c,v 1.43 2001/05/29 08:59:47 dwmw2 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
51 #include "nodelist.h"
52
53 #ifndef MTD_BLOCK_MAJOR
54 #define MTD_BLOCK_MAJOR 31
55 #endif
56
57 extern void jffs2_read_inode (struct inode *);
58 void jffs2_put_super (struct super_block *);
59 void jffs2_write_super (struct super_block *);
60 static int jffs2_statfs (struct super_block *, struct statfs *);
61 int jffs2_remount_fs (struct super_block *, int *, char *);
62 extern void jffs2_clear_inode (struct inode *);
63
64 static struct super_operations jffs2_super_operations =
65 {
66         read_inode:     jffs2_read_inode,
67 //      delete_inode:   jffs2_delete_inode,
68         put_super:      jffs2_put_super,
69         write_super:    jffs2_write_super,
70         statfs:         jffs2_statfs,
71 //      remount_fs:     jffs2_remount_fs,
72         clear_inode:    jffs2_clear_inode
73 };
74
75 static int jffs2_statfs(struct super_block *sb, struct statfs *buf)
76 {
77         struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
78         unsigned long avail;
79
80         buf->f_type = JFFS2_SUPER_MAGIC;
81         buf->f_bsize = 1 << PAGE_SHIFT;
82         buf->f_blocks = c->flash_size >> PAGE_SHIFT;
83         buf->f_files = 0;
84         buf->f_ffree = 0;
85         buf->f_namelen = JFFS2_MAX_NAME_LEN;
86
87         spin_lock_bh(&c->erase_completion_lock);
88
89         avail = c->dirty_size + c->free_size;
90         if (avail > c->sector_size * JFFS2_RESERVED_BLOCKS_WRITE)
91                 avail -= c->sector_size * JFFS2_RESERVED_BLOCKS_WRITE;
92         else
93                 avail = 0;
94
95         buf->f_bavail = buf->f_bfree = avail >> PAGE_SHIFT;
96
97 #if CONFIG_JFFS2_FS_DEBUG > 0
98         printk(KERN_DEBUG "STATFS:\n");
99         printk(KERN_DEBUG "flash_size: %08x\n", c->flash_size);
100         printk(KERN_DEBUG "used_size: %08x\n", c->used_size);
101         printk(KERN_DEBUG "dirty_size: %08x\n", c->dirty_size);
102         printk(KERN_DEBUG "free_size: %08x\n", c->free_size);
103         printk(KERN_DEBUG "erasing_size: %08x\n", c->erasing_size);
104         printk(KERN_DEBUG "bad_size: %08x\n", c->bad_size);
105         printk(KERN_DEBUG "sector_size: %08x\n", c->sector_size);
106
107         if (c->nextblock) {
108                 printk(KERN_DEBUG "nextblock: 0x%08x\n", c->nextblock->offset);
109         } else {
110                 printk(KERN_DEBUG "nextblock: NULL\n");
111         }
112         if (c->gcblock) {
113                 printk(KERN_DEBUG "gcblock: 0x%08x\n", c->gcblock->offset);
114         } else {
115                 printk(KERN_DEBUG "gcblock: NULL\n");
116         }
117         if (list_empty(&c->clean_list)) {
118                 printk(KERN_DEBUG "clean_list: empty\n");
119         } else {
120                 struct list_head *this;
121
122                 list_for_each(this, &c->clean_list) {
123                         struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
124                         printk(KERN_DEBUG "clean_list: %08x\n", jeb->offset);
125                 }
126         }
127         if (list_empty(&c->dirty_list)) {
128                 printk(KERN_DEBUG "dirty_list: empty\n");
129         } else {
130                 struct list_head *this;
131
132                 list_for_each(this, &c->dirty_list) {
133                         struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
134                         printk(KERN_DEBUG "dirty_list: %08x\n", jeb->offset);
135                 }
136         }
137         if (list_empty(&c->erasing_list)) {
138                 printk(KERN_DEBUG "erasing_list: empty\n");
139         } else {
140                 struct list_head *this;
141
142                 list_for_each(this, &c->erasing_list) {
143                         struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
144                         printk(KERN_DEBUG "erasing_list: %08x\n", jeb->offset);
145                 }
146         }
147         if (list_empty(&c->erase_pending_list)) {
148                 printk(KERN_DEBUG "erase_pending_list: empty\n");
149         } else {
150                 struct list_head *this;
151
152                 list_for_each(this, &c->erase_pending_list) {
153                         struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
154                         printk(KERN_DEBUG "erase_pending_list: %08x\n", jeb->offset);
155                 }
156         }
157         if (list_empty(&c->free_list)) {
158                 printk(KERN_DEBUG "free_list: empty\n");
159         } else {
160                 struct list_head *this;
161
162                 list_for_each(this, &c->free_list) {
163                         struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
164                         printk(KERN_DEBUG "free_list: %08x\n", jeb->offset);
165                 }
166         }
167         if (list_empty(&c->bad_list)) {
168                 printk(KERN_DEBUG "bad_list: empty\n");
169         } else {
170                 struct list_head *this;
171
172                 list_for_each(this, &c->bad_list) {
173                         struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
174                         printk(KERN_DEBUG "bad_list: %08x\n", jeb->offset);
175                 }
176         }
177         if (list_empty(&c->bad_used_list)) {
178                 printk(KERN_DEBUG "bad_used_list: empty\n");
179         } else {
180                 struct list_head *this;
181
182                 list_for_each(this, &c->bad_used_list) {
183                         struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
184                         printk(KERN_DEBUG "bad_used_list: %08x\n", jeb->offset);
185                 }
186         }
187 #endif /* CONFIG_JFFS2_FS_DEBUG */
188
189         spin_unlock_bh(&c->erase_completion_lock);
190
191
192         return 0;
193 }
194
195 static struct super_block *jffs2_read_super(struct super_block *sb, void *data, int silent)
196 {
197         struct jffs2_sb_info *c;
198         struct inode *root_i;
199         int i;
200
201         D1(printk(KERN_DEBUG "jffs2: read_super for device %s\n", kdevname(sb->s_dev)));
202
203         if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) {
204                 if (!silent)
205                         printk(KERN_DEBUG "jffs2: attempt to mount non-MTD device %s\n", kdevname(sb->s_dev));
206                 return NULL;
207         }
208
209         c = JFFS2_SB_INFO(sb);
210         memset(c, 0, sizeof(*c));
211         
212         c->mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
213         if (!c->mtd) {
214                 D1(printk(KERN_DEBUG "jffs2: MTD device #%u doesn't appear to exist\n", MINOR(sb->s_dev)));
215                 return NULL;
216         }
217         c->sector_size = c->mtd->erasesize;
218         c->free_size = c->flash_size = c->mtd->size;
219         c->nr_blocks = c->mtd->size / c->mtd->erasesize;
220         c->blocks = kmalloc(sizeof(struct jffs2_eraseblock) * c->nr_blocks, GFP_KERNEL);
221         if (!c->blocks)
222                 goto out_mtd;
223         for (i=0; i<c->nr_blocks; i++) {
224                 INIT_LIST_HEAD(&c->blocks[i].list);
225                 c->blocks[i].offset = i * c->sector_size;
226                 c->blocks[i].free_size = c->sector_size;
227                 c->blocks[i].dirty_size = 0;
228                 c->blocks[i].used_size = 0;
229                 c->blocks[i].first_node = NULL;
230                 c->blocks[i].last_node = NULL;
231         }
232                 
233         spin_lock_init(&c->nodelist_lock);
234         init_MUTEX(&c->alloc_sem);
235         init_waitqueue_head(&c->erase_wait);
236         spin_lock_init(&c->erase_completion_lock);
237         spin_lock_init(&c->inocache_lock);
238
239         INIT_LIST_HEAD(&c->clean_list);
240         INIT_LIST_HEAD(&c->dirty_list);
241         INIT_LIST_HEAD(&c->erasing_list);
242         INIT_LIST_HEAD(&c->erase_pending_list);
243         INIT_LIST_HEAD(&c->erase_complete_list);
244         INIT_LIST_HEAD(&c->free_list);
245         INIT_LIST_HEAD(&c->bad_list);
246         INIT_LIST_HEAD(&c->bad_used_list);
247         c->highest_ino = 1;
248
249         if (jffs2_build_filesystem(c)) {
250                 D1(printk(KERN_DEBUG "build_fs failed\n"));
251                 goto out_nodes;
252         }
253         sb->s_op = &jffs2_super_operations;
254
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"));
259                 goto out_nodes;
260         }
261
262         D1(printk(KERN_DEBUG "jffs2_read_super(): d_alloc_root()\n"));
263         sb->s_root = d_alloc_root(root_i);
264         if (!sb->s_root)
265                 goto out_root_i;
266
267 #if LINUX_VERSION_CODE >= 0x20403
268         sb->s_maxbytes = 0xFFFFFFFF;
269 #endif
270         sb->s_blocksize = PAGE_CACHE_SIZE;
271         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
272         sb->s_magic = JFFS2_SUPER_MAGIC;
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         jffs2_stop_garbage_collect_thread(c);
294         jffs2_free_ino_caches(c);
295         jffs2_free_raw_node_refs(c);
296         kfree(c->blocks);
297         put_mtd_device(c->mtd);
298         
299         D1(printk(KERN_DEBUG "jffs2_put_super returning\n"));
300 }
301
302 void jffs2_write_super (struct super_block *sb)
303 {
304         struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
305         sb->s_dirt = 0;
306         jffs2_garbage_collect_trigger(c);
307         jffs2_erase_pending_blocks(c);
308         jffs2_mark_erased_blocks(c);
309 }
310
311
312 static DECLARE_FSTYPE_DEV(jffs2_fs_type, "jffs2", jffs2_read_super);
313
314 static int __init init_jffs2_fs(void)
315 {
316         int ret;
317
318         printk(KERN_NOTICE "JFFS2 version 2.1. (C) 2001 Red Hat, Inc., designed by Axis Communications AB.\n");
319
320 #ifdef JFFS2_OUT_OF_KERNEL
321         /* sanity checks. Could we do these at compile time? */
322         if (sizeof(struct jffs2_sb_info) > sizeof (((struct super_block *)NULL)->u)) {
323                 printk(KERN_ERR "JFFS2 error: struct jffs2_sb_info (%d bytes) doesn't fit in the super_block union (%d bytes)\n", 
324                        sizeof(struct jffs2_sb_info), sizeof (((struct super_block *)NULL)->u));
325                 return -EIO;
326         }
327
328         if (sizeof(struct jffs2_inode_info) > sizeof (((struct inode *)NULL)->u)) {
329                 printk(KERN_ERR "JFFS2 error: struct jffs2_inode_info (%d bytes) doesn't fit in the inode union (%d bytes)\n", 
330                        sizeof(struct jffs2_inode_info), sizeof (((struct inode *)NULL)->u));
331                 return -EIO;
332         }
333 #endif
334
335         ret = jffs2_create_slab_caches();
336         if (ret) {
337                 printk(KERN_ERR "JFFS2 error: Failed to initialise slab caches\n");
338                 return ret;
339         }
340         ret = register_filesystem(&jffs2_fs_type);
341         if (ret) {
342                 printk(KERN_ERR "JFFS2 error: Failed to register filesystem\n");
343                 jffs2_destroy_slab_caches();
344         }
345         return ret;
346 }
347
348 static void __exit exit_jffs2_fs(void)
349 {
350         jffs2_destroy_slab_caches();
351         unregister_filesystem(&jffs2_fs_type);
352 }
353
354 module_init(init_jffs2_fs);
355 module_exit(exit_jffs2_fs);