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.1.1.1 2008/10/15 03:27:07 james26_jang 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
56extern void jffs2_read_inode (struct inode *);
57void jffs2_put_super (struct super_block *);
58void jffs2_write_super (struct super_block *);
59static int jffs2_statfs (struct super_block *, struct statfs *);
60int jffs2_remount_fs (struct super_block *, int *, char *);
61extern void jffs2_clear_inode (struct inode *);
62
63static 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
74static 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
194static 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
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	if (!(sb->s_flags & MS_RDONLY))
274		jffs2_start_garbage_collect_thread(c);
275	return sb;
276
277 out_root_i:
278	iput(root_i);
279 out_nodes:
280	jffs2_free_ino_caches(c);
281	jffs2_free_raw_node_refs(c);
282	kfree(c->blocks);
283 out_mtd:
284	put_mtd_device(c->mtd);
285	return NULL;
286}
287
288void jffs2_put_super (struct super_block *sb)
289{
290	struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
291
292	D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));
293
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);
298	kfree(c->blocks);
299	if (c->mtd->sync)
300		c->mtd->sync(c->mtd);
301	put_mtd_device(c->mtd);
302
303	D1(printk(KERN_DEBUG "jffs2_put_super returning\n"));
304}
305
306int jffs2_remount_fs (struct super_block *sb, int *flags, char *data)
307{
308	struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
309
310	if (c->flags & JFFS2_SB_FLAG_RO && !(sb->s_flags & MS_RDONLY))
311		return -EROFS;
312
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);
318
319	if (!(*flags & MS_RDONLY))
320		jffs2_start_garbage_collect_thread(c);
321
322	sb->s_flags = (sb->s_flags & ~MS_RDONLY)|(*flags & MS_RDONLY);
323
324	return 0;
325}
326
327void jffs2_write_super (struct super_block *sb)
328{
329	struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
330	sb->s_dirt = 0;
331
332	if (sb->s_flags & MS_RDONLY)
333		return;
334
335	jffs2_garbage_collect_trigger(c);
336	jffs2_erase_pending_blocks(c);
337	jffs2_mark_erased_blocks(c);
338}
339
340
341static DECLARE_FSTYPE_DEV(jffs2_fs_type, "jffs2", jffs2_read_super);
342
343static int __init init_jffs2_fs(void)
344{
345	int ret;
346
347	printk(KERN_NOTICE "JFFS2 version 2.1. (C) 2001 Red Hat, Inc., designed by Axis Communications AB.\n");
348
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));
354		return -EIO;
355	}
356
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));
360		return -EIO;
361	}
362#endif
363
364	ret = jffs2_zlib_init();
365	if (ret) {
366		printk(KERN_ERR "JFFS2 error: Failed to initialise zlib workspaces\n");
367		goto out;
368	}
369	ret = jffs2_create_slab_caches();
370	if (ret) {
371		printk(KERN_ERR "JFFS2 error: Failed to initialise slab caches\n");
372		goto out_zlib;
373	}
374	ret = register_filesystem(&jffs2_fs_type);
375	if (ret) {
376		printk(KERN_ERR "JFFS2 error: Failed to register filesystem\n");
377		goto out_slab;
378	}
379	return 0;
380
381 out_slab:
382	jffs2_destroy_slab_caches();
383 out_zlib:
384	jffs2_zlib_exit();
385 out:
386	return ret;
387}
388
389static void __exit exit_jffs2_fs(void)
390{
391	jffs2_destroy_slab_caches();
392	jffs2_zlib_exit();
393	unregister_filesystem(&jffs2_fs_type);
394}
395
396module_init(init_jffs2_fs);
397module_exit(exit_jffs2_fs);
398
399MODULE_DESCRIPTION("The Journalling Flash File System, v2");
400MODULE_AUTHOR("Red Hat, Inc.");
401MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for
402		       // the sake of this tag. It's Free Software.
403