blob: 853b8e300084cf2bb3c9941055ebc5aa433203e3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
David Woodhousec00c3102007-04-25 14:16:47 +01004 * Copyright © 2001-2007 Red Hat, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Created by David Woodhouse <dwmw2@infradead.org>
7 *
8 * For licensing information, see the file 'LICENCE' in this directory.
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/slab.h>
15#include <linux/init.h>
16#include <linux/list.h>
17#include <linux/fs.h>
Artem Bityutskiy9c740342006-10-11 14:52:47 +030018#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/mount.h>
20#include <linux/jffs2.h>
21#include <linux/pagemap.h>
David Howellsacaebfd2007-05-10 22:51:50 -070022#include <linux/mtd/super.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/ctype.h>
24#include <linux/namei.h>
David Woodhouse5f556aa2008-07-31 20:39:25 +010025#include <linux/exportfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "compr.h"
27#include "nodelist.h"
28
29static void jffs2_put_super(struct super_block *);
30
Christoph Lametere18b8902006-12-06 20:33:20 -080031static struct kmem_cache *jffs2_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33static struct inode *jffs2_alloc_inode(struct super_block *sb)
34{
David Woodhouse4e571ab2008-05-01 12:28:04 +010035 struct jffs2_inode_info *f;
36
37 f = kmem_cache_alloc(jffs2_inode_cachep, GFP_KERNEL);
38 if (!f)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 return NULL;
David Woodhouse4e571ab2008-05-01 12:28:04 +010040 return &f->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041}
42
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +110043static void jffs2_i_callback(struct rcu_head *head)
44{
45 struct inode *inode = container_of(head, struct inode, i_rcu);
46 INIT_LIST_HEAD(&inode->i_dentry);
47 kmem_cache_free(jffs2_inode_cachep, JFFS2_INODE_INFO(inode));
48}
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static void jffs2_destroy_inode(struct inode *inode)
51{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +110052 call_rcu(&inode->i_rcu, jffs2_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
Alexey Dobriyan51cc5062008-07-25 19:45:34 -070055static void jffs2_i_init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
David Woodhouse4e571ab2008-05-01 12:28:04 +010057 struct jffs2_inode_info *f = foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
David Woodhouse4e571ab2008-05-01 12:28:04 +010059 mutex_init(&f->sem);
60 inode_init_once(&f->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
Christoph Hellwig01ba6872009-05-11 23:34:27 +020063static void jffs2_write_super(struct super_block *sb)
64{
65 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
Christoph Hellwigebc1ac12009-05-11 23:35:03 +020066
67 lock_super(sb);
Christoph Hellwig01ba6872009-05-11 23:34:27 +020068 sb->s_dirt = 0;
69
Christoph Hellwigebc1ac12009-05-11 23:35:03 +020070 if (!(sb->s_flags & MS_RDONLY)) {
71 D1(printk(KERN_DEBUG "jffs2_write_super()\n"));
Christoph Hellwigebc1ac12009-05-11 23:35:03 +020072 jffs2_flush_wbuf_gc(c, 0);
73 }
Christoph Hellwig01ba6872009-05-11 23:34:27 +020074
Christoph Hellwigebc1ac12009-05-11 23:35:03 +020075 unlock_super(sb);
Christoph Hellwig01ba6872009-05-11 23:34:27 +020076}
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static int jffs2_sync_fs(struct super_block *sb, int wait)
79{
80 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
81
Christoph Hellwigd579ed02009-06-08 10:08:21 +020082 jffs2_write_super(sb);
83
David Woodhouseced22072008-04-22 15:13:40 +010084 mutex_lock(&c->alloc_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 jffs2_flush_wbuf_pad(c);
David Woodhouseced22072008-04-22 15:13:40 +010086 mutex_unlock(&c->alloc_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return 0;
88}
89
David Woodhouse5f556aa2008-07-31 20:39:25 +010090static struct inode *jffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
91 uint32_t generation)
92{
93 /* We don't care about i_generation. We'll destroy the flash
94 before we start re-using inode numbers anyway. And even
95 if that wasn't true, we'd have other problems...*/
96 return jffs2_iget(sb, ino);
97}
98
99static struct dentry *jffs2_fh_to_dentry(struct super_block *sb, struct fid *fid,
100 int fh_len, int fh_type)
101{
102 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
103 jffs2_nfs_get_inode);
104}
105
106static struct dentry *jffs2_fh_to_parent(struct super_block *sb, struct fid *fid,
107 int fh_len, int fh_type)
108{
109 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
110 jffs2_nfs_get_inode);
111}
112
113static struct dentry *jffs2_get_parent(struct dentry *child)
114{
115 struct jffs2_inode_info *f;
116 uint32_t pino;
117
118 BUG_ON(!S_ISDIR(child->d_inode->i_mode));
119
120 f = JFFS2_INODE_INFO(child->d_inode);
121
122 pino = f->inocache->pino_nlink;
123
124 JFFS2_DEBUG("Parent of directory ino #%u is #%u\n",
125 f->inocache->ino, pino);
126
127 return d_obtain_alias(jffs2_iget(child->d_inode->i_sb, pino));
128}
129
Alexey Dobriyanac4cfdd2009-09-21 17:01:10 -0700130static const struct export_operations jffs2_export_ops = {
David Woodhouse5f556aa2008-07-31 20:39:25 +0100131 .get_parent = jffs2_get_parent,
132 .fh_to_dentry = jffs2_fh_to_dentry,
133 .fh_to_parent = jffs2_fh_to_parent,
134};
135
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800136static const struct super_operations jffs2_super_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 .alloc_inode = jffs2_alloc_inode,
139 .destroy_inode =jffs2_destroy_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 .put_super = jffs2_put_super,
141 .write_super = jffs2_write_super,
142 .statfs = jffs2_statfs,
143 .remount_fs = jffs2_remount_fs,
Al Virob57922d2010-06-07 14:34:48 -0400144 .evict_inode = jffs2_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 .dirty_inode = jffs2_dirty_inode,
146 .sync_fs = jffs2_sync_fs,
147};
148
David Howellsacaebfd2007-05-10 22:51:50 -0700149/*
150 * fill in the superblock
151 */
152static int jffs2_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 struct jffs2_sb_info *c;
Jan Blunckdb719222010-08-15 22:51:10 +0200155 int ret;
156
David Howellsacaebfd2007-05-10 22:51:50 -0700157 D1(printk(KERN_DEBUG "jffs2_get_sb_mtd():"
158 " New superblock for device %d (\"%s\")\n",
159 sb->s_mtd->index, sb->s_mtd->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700161 c = kzalloc(sizeof(*c), GFP_KERNEL);
Arnd Bergmann1a028dd2010-09-16 16:11:09 +0200162 if (!c)
David Howells454e2392006-06-23 02:02:57 -0700163 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
David Howellsacaebfd2007-05-10 22:51:50 -0700165 c->mtd = sb->s_mtd;
166 c->os_priv = sb;
167 sb->s_fs_info = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
David Howellsacaebfd2007-05-10 22:51:50 -0700169 /* Initialize JFFS2 superblock locks, the further initialization will
170 * be done later */
David Woodhouseced22072008-04-22 15:13:40 +0100171 mutex_init(&c->alloc_sem);
172 mutex_init(&c->erase_free_sem);
Artem B. Bityuckiyb6220592005-07-12 17:37:12 +0100173 init_waitqueue_head(&c->erase_wait);
174 init_waitqueue_head(&c->inocache_wq);
175 spin_lock_init(&c->erase_completion_lock);
176 spin_lock_init(&c->inocache_lock);
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 sb->s_op = &jffs2_super_operations;
David Woodhouse5f556aa2008-07-31 20:39:25 +0100179 sb->s_export_op = &jffs2_export_ops;
David Howellsacaebfd2007-05-10 22:51:50 -0700180 sb->s_flags = sb->s_flags | MS_NOATIME;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900181 sb->s_xattr = jffs2_xattr_handlers;
182#ifdef CONFIG_JFFS2_FS_POSIX_ACL
183 sb->s_flags |= MS_POSIXACL;
184#endif
Jan Blunckdb719222010-08-15 22:51:10 +0200185 ret = jffs2_do_fill_super(sb, data, silent);
Jan Blunckdb719222010-08-15 22:51:10 +0200186 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Al Viro848b83a2010-07-25 00:56:46 +0400189static struct dentry *jffs2_mount(struct file_system_type *fs_type,
David Howells454e2392006-06-23 02:02:57 -0700190 int flags, const char *dev_name,
Al Viro848b83a2010-07-25 00:56:46 +0400191 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Al Viro848b83a2010-07-25 00:56:46 +0400193 return mount_mtd(fs_type, flags, dev_name, data, jffs2_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196static void jffs2_put_super (struct super_block *sb)
197{
198 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
199
200 D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));
201
Christoph Hellwig8c85e122009-04-28 18:00:26 +0200202 if (sb->s_dirt)
203 jffs2_write_super(sb);
204
David Woodhouseced22072008-04-22 15:13:40 +0100205 mutex_lock(&c->alloc_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 jffs2_flush_wbuf_pad(c);
David Woodhouseced22072008-04-22 15:13:40 +0100207 mutex_unlock(&c->alloc_sem);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100208
209 jffs2_sum_exit(c);
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 jffs2_free_ino_caches(c);
212 jffs2_free_raw_node_refs(c);
Ferenc Havasi4ce1f562005-08-31 14:51:04 +0100213 if (jffs2_blocks_use_vmalloc(c))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 vfree(c->blocks);
215 else
216 kfree(c->blocks);
217 jffs2_flash_cleanup(c);
218 kfree(c->inocache_list);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900219 jffs2_clear_xattr_subsystem(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (c->mtd->sync)
221 c->mtd->sync(c->mtd);
222
223 D1(printk(KERN_DEBUG "jffs2_put_super returning\n"));
224}
225
226static void jffs2_kill_sb(struct super_block *sb)
227{
228 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
Artem B. Bityuckiya69dde92005-05-18 12:37:28 +0100229 if (!(sb->s_flags & MS_RDONLY))
230 jffs2_stop_garbage_collect_thread(c);
David Howellsacaebfd2007-05-10 22:51:50 -0700231 kill_mtd_super(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 kfree(c);
233}
234
235static struct file_system_type jffs2_fs_type = {
236 .owner = THIS_MODULE,
237 .name = "jffs2",
Al Viro848b83a2010-07-25 00:56:46 +0400238 .mount = jffs2_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 .kill_sb = jffs2_kill_sb,
240};
241
242static int __init init_jffs2_fs(void)
243{
244 int ret;
245
David Woodhouse3e68fbb2006-05-15 00:49:43 +0100246 /* Paranoia checks for on-medium structures. If we ask GCC
247 to pack them with __attribute__((packed)) then it _also_
248 assumes that they're not aligned -- so it emits crappy
249 code on some architectures. Ideally we want an attribute
250 which means just 'no padding', without the alignment
251 thing. But GCC doesn't have that -- we have to just
252 hope the structs are the right sizes, instead. */
Alexey Dobriyan2ecd05a2006-10-11 01:22:05 -0700253 BUILD_BUG_ON(sizeof(struct jffs2_unknown_node) != 12);
254 BUILD_BUG_ON(sizeof(struct jffs2_raw_dirent) != 40);
255 BUILD_BUG_ON(sizeof(struct jffs2_raw_inode) != 68);
256 BUILD_BUG_ON(sizeof(struct jffs2_raw_summary) != 32);
David Woodhouse3e68fbb2006-05-15 00:49:43 +0100257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 printk(KERN_INFO "JFFS2 version 2.2."
Andrew Victor2f82ce12005-02-09 09:24:26 +0000259#ifdef CONFIG_JFFS2_FS_WRITEBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 " (NAND)"
261#endif
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100262#ifdef CONFIG_JFFS2_SUMMARY
263 " (SUMMARY) "
264#endif
David Woodhousec00c3102007-04-25 14:16:47 +0100265 " © 2001-2006 Red Hat, Inc.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 jffs2_inode_cachep = kmem_cache_create("jffs2_i",
268 sizeof(struct jffs2_inode_info),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800269 0, (SLAB_RECLAIM_ACCOUNT|
270 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900271 jffs2_i_init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (!jffs2_inode_cachep) {
273 printk(KERN_ERR "JFFS2 error: Failed to initialise inode cache\n");
274 return -ENOMEM;
275 }
276 ret = jffs2_compressors_init();
277 if (ret) {
278 printk(KERN_ERR "JFFS2 error: Failed to initialise compressors\n");
279 goto out;
280 }
281 ret = jffs2_create_slab_caches();
282 if (ret) {
283 printk(KERN_ERR "JFFS2 error: Failed to initialise slab caches\n");
284 goto out_compressors;
285 }
286 ret = register_filesystem(&jffs2_fs_type);
287 if (ret) {
288 printk(KERN_ERR "JFFS2 error: Failed to register filesystem\n");
289 goto out_slab;
290 }
291 return 0;
292
293 out_slab:
294 jffs2_destroy_slab_caches();
295 out_compressors:
296 jffs2_compressors_exit();
297 out:
298 kmem_cache_destroy(jffs2_inode_cachep);
299 return ret;
300}
301
302static void __exit exit_jffs2_fs(void)
303{
304 unregister_filesystem(&jffs2_fs_type);
305 jffs2_destroy_slab_caches();
306 jffs2_compressors_exit();
307 kmem_cache_destroy(jffs2_inode_cachep);
308}
309
310module_init(init_jffs2_fs);
311module_exit(exit_jffs2_fs);
312
313MODULE_DESCRIPTION("The Journalling Flash File System, v2");
314MODULE_AUTHOR("Red Hat, Inc.");
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000315MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 // the sake of this tag. It's Free Software.