blob: 45368f8bbe72d52e7dee1f300e8b13bd60139eb1 [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>
22#include <linux/mtd/mtd.h>
23#include <linux/ctype.h>
24#include <linux/namei.h>
25#include "compr.h"
26#include "nodelist.h"
27
28static void jffs2_put_super(struct super_block *);
29
Christoph Lametere18b8902006-12-06 20:33:20 -080030static struct kmem_cache *jffs2_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32static struct inode *jffs2_alloc_inode(struct super_block *sb)
33{
34 struct jffs2_inode_info *ei;
Christoph Lametere94b1762006-12-06 20:33:17 -080035 ei = (struct jffs2_inode_info *)kmem_cache_alloc(jffs2_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 if (!ei)
37 return NULL;
38 return &ei->vfs_inode;
39}
40
41static void jffs2_destroy_inode(struct inode *inode)
42{
43 kmem_cache_free(jffs2_inode_cachep, JFFS2_INODE_INFO(inode));
44}
45
Christoph Lametere18b8902006-12-06 20:33:20 -080046static void jffs2_i_init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 struct jffs2_inode_info *ei = (struct jffs2_inode_info *) foo;
49
Christoph Lameter50953fe2007-05-06 14:50:16 -070050 if (flags & SLAB_CTOR_CONSTRUCTOR) {
Thomas Gleixner21eeb7a2005-11-29 16:57:17 +010051 init_MUTEX(&ei->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 inode_init_once(&ei->vfs_inode);
53 }
54}
55
56static int jffs2_sync_fs(struct super_block *sb, int wait)
57{
58 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
59
60 down(&c->alloc_sem);
61 jffs2_flush_wbuf_pad(c);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000062 up(&c->alloc_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 return 0;
64}
65
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -080066static const struct super_operations jffs2_super_operations =
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
68 .alloc_inode = jffs2_alloc_inode,
69 .destroy_inode =jffs2_destroy_inode,
70 .read_inode = jffs2_read_inode,
71 .put_super = jffs2_put_super,
72 .write_super = jffs2_write_super,
73 .statfs = jffs2_statfs,
74 .remount_fs = jffs2_remount_fs,
75 .clear_inode = jffs2_clear_inode,
76 .dirty_inode = jffs2_dirty_inode,
77 .sync_fs = jffs2_sync_fs,
78};
79
80static int jffs2_sb_compare(struct super_block *sb, void *data)
81{
82 struct jffs2_sb_info *p = data;
83 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
84
85 /* The superblocks are considered to be equivalent if the underlying MTD
86 device is the same one */
87 if (c->mtd == p->mtd) {
88 D1(printk(KERN_DEBUG "jffs2_sb_compare: match on device %d (\"%s\")\n", p->mtd->index, p->mtd->name));
89 return 1;
90 } else {
91 D1(printk(KERN_DEBUG "jffs2_sb_compare: No match, device %d (\"%s\"), device %d (\"%s\")\n",
92 c->mtd->index, c->mtd->name, p->mtd->index, p->mtd->name));
93 return 0;
94 }
95}
96
97static int jffs2_sb_set(struct super_block *sb, void *data)
98{
99 struct jffs2_sb_info *p = data;
100
101 /* For persistence of NFS exports etc. we use the same s_dev
102 each time we mount the device, don't just use an anonymous
103 device */
104 sb->s_fs_info = p;
105 p->os_priv = sb;
106 sb->s_dev = MKDEV(MTD_BLOCK_MAJOR, p->mtd->index);
107
108 return 0;
109}
110
David Howells454e2392006-06-23 02:02:57 -0700111static int jffs2_get_sb_mtd(struct file_system_type *fs_type,
112 int flags, const char *dev_name,
113 void *data, struct mtd_info *mtd,
114 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 struct super_block *sb;
117 struct jffs2_sb_info *c;
118 int ret;
119
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700120 c = kzalloc(sizeof(*c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (!c)
David Howells454e2392006-06-23 02:02:57 -0700122 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 c->mtd = mtd;
124
125 sb = sget(fs_type, jffs2_sb_compare, jffs2_sb_set, c);
126
127 if (IS_ERR(sb))
David Howells454e2392006-06-23 02:02:57 -0700128 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 if (sb->s_root) {
131 /* New mountpoint for JFFS2 which is already mounted */
132 D1(printk(KERN_DEBUG "jffs2_get_sb_mtd(): Device %d (\"%s\") is already mounted\n",
133 mtd->index, mtd->name));
David Howells454e2392006-06-23 02:02:57 -0700134 ret = simple_set_mnt(mnt, sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 goto out_put;
136 }
137
138 D1(printk(KERN_DEBUG "jffs2_get_sb_mtd(): New superblock for device %d (\"%s\")\n",
139 mtd->index, mtd->name));
140
Artem B. Bityuckiyb6220592005-07-12 17:37:12 +0100141 /* Initialize JFFS2 superblock locks, the further initialization will be
142 * done later */
143 init_MUTEX(&c->alloc_sem);
144 init_MUTEX(&c->erase_free_sem);
145 init_waitqueue_head(&c->erase_wait);
146 init_waitqueue_head(&c->inocache_wq);
147 spin_lock_init(&c->erase_completion_lock);
148 spin_lock_init(&c->inocache_lock);
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 sb->s_op = &jffs2_super_operations;
151 sb->s_flags = flags | MS_NOATIME;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900152 sb->s_xattr = jffs2_xattr_handlers;
153#ifdef CONFIG_JFFS2_FS_POSIX_ACL
154 sb->s_flags |= MS_POSIXACL;
155#endif
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800156 ret = jffs2_do_fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 if (ret) {
159 /* Failure case... */
160 up_write(&sb->s_umount);
161 deactivate_super(sb);
David Howells454e2392006-06-23 02:02:57 -0700162 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
164
165 sb->s_flags |= MS_ACTIVE;
David Howells454e2392006-06-23 02:02:57 -0700166 return simple_set_mnt(mnt, sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
David Howells454e2392006-06-23 02:02:57 -0700168out_error:
169 ret = PTR_ERR(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 out_put:
171 kfree(c);
172 put_mtd_device(mtd);
173
David Howells454e2392006-06-23 02:02:57 -0700174 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
David Howells454e2392006-06-23 02:02:57 -0700177static int jffs2_get_sb_mtdnr(struct file_system_type *fs_type,
178 int flags, const char *dev_name,
179 void *data, int mtdnr,
180 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 struct mtd_info *mtd;
183
184 mtd = get_mtd_device(NULL, mtdnr);
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300185 if (IS_ERR(mtd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 D1(printk(KERN_DEBUG "jffs2: MTD device #%u doesn't appear to exist\n", mtdnr));
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300187 return PTR_ERR(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189
David Howells454e2392006-06-23 02:02:57 -0700190 return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
David Howells454e2392006-06-23 02:02:57 -0700193static int jffs2_get_sb(struct file_system_type *fs_type,
194 int flags, const char *dev_name,
195 void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 int err;
198 struct nameidata nd;
199 int mtdnr;
200
201 if (!dev_name)
David Howells454e2392006-06-23 02:02:57 -0700202 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 D1(printk(KERN_DEBUG "jffs2_get_sb(): dev_name \"%s\"\n", dev_name));
205
206 /* The preferred way of mounting in future; especially when
207 CONFIG_BLK_DEV is implemented - we specify the underlying
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000208 MTD device by number or by name, so that we don't require
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 block device support to be present in the kernel. */
210
211 /* FIXME: How to do the root fs this way? */
212
213 if (dev_name[0] == 'm' && dev_name[1] == 't' && dev_name[2] == 'd') {
214 /* Probably mounting without the blkdev crap */
215 if (dev_name[3] == ':') {
216 struct mtd_info *mtd;
217
218 /* Mount by MTD device name */
219 D1(printk(KERN_DEBUG "jffs2_get_sb(): mtd:%%s, name \"%s\"\n", dev_name+4));
220 for (mtdnr = 0; mtdnr < MAX_MTD_DEVICES; mtdnr++) {
221 mtd = get_mtd_device(NULL, mtdnr);
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300222 if (!IS_ERR(mtd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (!strcmp(mtd->name, dev_name+4))
David Howells454e2392006-06-23 02:02:57 -0700224 return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 put_mtd_device(mtd);
226 }
227 }
228 printk(KERN_NOTICE "jffs2_get_sb(): MTD device with name \"%s\" not found.\n", dev_name+4);
229 } else if (isdigit(dev_name[3])) {
230 /* Mount by MTD device number name */
231 char *endptr;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 mtdnr = simple_strtoul(dev_name+3, &endptr, 0);
234 if (!*endptr) {
235 /* It was a valid number */
236 D1(printk(KERN_DEBUG "jffs2_get_sb(): mtd%%d, mtdnr %d\n", mtdnr));
David Howells454e2392006-06-23 02:02:57 -0700237 return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239 }
240 }
241
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000242 /* Try the old way - the hack where we allowed users to mount
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 /dev/mtdblock$(n) but didn't actually _use_ the blkdev */
244
245 err = path_lookup(dev_name, LOOKUP_FOLLOW, &nd);
246
247 D1(printk(KERN_DEBUG "jffs2_get_sb(): path_lookup() returned %d, inode %p\n",
248 err, nd.dentry->d_inode));
249
250 if (err)
David Howells454e2392006-06-23 02:02:57 -0700251 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 err = -EINVAL;
254
255 if (!S_ISBLK(nd.dentry->d_inode->i_mode))
256 goto out;
257
258 if (nd.mnt->mnt_flags & MNT_NODEV) {
259 err = -EACCES;
260 goto out;
261 }
262
263 if (imajor(nd.dentry->d_inode) != MTD_BLOCK_MAJOR) {
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800264 if (!(flags & MS_SILENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 printk(KERN_NOTICE "Attempt to mount non-MTD device \"%s\" as JFFS2\n",
266 dev_name);
267 goto out;
268 }
269
270 mtdnr = iminor(nd.dentry->d_inode);
271 path_release(&nd);
272
David Howells454e2392006-06-23 02:02:57 -0700273 return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275out:
276 path_release(&nd);
David Howells454e2392006-06-23 02:02:57 -0700277 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280static void jffs2_put_super (struct super_block *sb)
281{
282 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
283
284 D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 down(&c->alloc_sem);
287 jffs2_flush_wbuf_pad(c);
288 up(&c->alloc_sem);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100289
290 jffs2_sum_exit(c);
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 jffs2_free_ino_caches(c);
293 jffs2_free_raw_node_refs(c);
Ferenc Havasi4ce1f562005-08-31 14:51:04 +0100294 if (jffs2_blocks_use_vmalloc(c))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 vfree(c->blocks);
296 else
297 kfree(c->blocks);
298 jffs2_flash_cleanup(c);
299 kfree(c->inocache_list);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900300 jffs2_clear_xattr_subsystem(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (c->mtd->sync)
302 c->mtd->sync(c->mtd);
303
304 D1(printk(KERN_DEBUG "jffs2_put_super returning\n"));
305}
306
307static void jffs2_kill_sb(struct super_block *sb)
308{
309 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
Artem B. Bityuckiya69dde92005-05-18 12:37:28 +0100310 if (!(sb->s_flags & MS_RDONLY))
311 jffs2_stop_garbage_collect_thread(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 generic_shutdown_super(sb);
313 put_mtd_device(c->mtd);
314 kfree(c);
315}
316
317static struct file_system_type jffs2_fs_type = {
318 .owner = THIS_MODULE,
319 .name = "jffs2",
320 .get_sb = jffs2_get_sb,
321 .kill_sb = jffs2_kill_sb,
322};
323
324static int __init init_jffs2_fs(void)
325{
326 int ret;
327
David Woodhouse3e68fbb2006-05-15 00:49:43 +0100328 /* Paranoia checks for on-medium structures. If we ask GCC
329 to pack them with __attribute__((packed)) then it _also_
330 assumes that they're not aligned -- so it emits crappy
331 code on some architectures. Ideally we want an attribute
332 which means just 'no padding', without the alignment
333 thing. But GCC doesn't have that -- we have to just
334 hope the structs are the right sizes, instead. */
Alexey Dobriyan2ecd05a2006-10-11 01:22:05 -0700335 BUILD_BUG_ON(sizeof(struct jffs2_unknown_node) != 12);
336 BUILD_BUG_ON(sizeof(struct jffs2_raw_dirent) != 40);
337 BUILD_BUG_ON(sizeof(struct jffs2_raw_inode) != 68);
338 BUILD_BUG_ON(sizeof(struct jffs2_raw_summary) != 32);
David Woodhouse3e68fbb2006-05-15 00:49:43 +0100339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 printk(KERN_INFO "JFFS2 version 2.2."
Andrew Victor2f82ce12005-02-09 09:24:26 +0000341#ifdef CONFIG_JFFS2_FS_WRITEBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 " (NAND)"
343#endif
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100344#ifdef CONFIG_JFFS2_SUMMARY
345 " (SUMMARY) "
346#endif
David Woodhousec00c3102007-04-25 14:16:47 +0100347 " © 2001-2006 Red Hat, Inc.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 jffs2_inode_cachep = kmem_cache_create("jffs2_i",
350 sizeof(struct jffs2_inode_info),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800351 0, (SLAB_RECLAIM_ACCOUNT|
352 SLAB_MEM_SPREAD),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 jffs2_i_init_once, NULL);
354 if (!jffs2_inode_cachep) {
355 printk(KERN_ERR "JFFS2 error: Failed to initialise inode cache\n");
356 return -ENOMEM;
357 }
358 ret = jffs2_compressors_init();
359 if (ret) {
360 printk(KERN_ERR "JFFS2 error: Failed to initialise compressors\n");
361 goto out;
362 }
363 ret = jffs2_create_slab_caches();
364 if (ret) {
365 printk(KERN_ERR "JFFS2 error: Failed to initialise slab caches\n");
366 goto out_compressors;
367 }
368 ret = register_filesystem(&jffs2_fs_type);
369 if (ret) {
370 printk(KERN_ERR "JFFS2 error: Failed to register filesystem\n");
371 goto out_slab;
372 }
373 return 0;
374
375 out_slab:
376 jffs2_destroy_slab_caches();
377 out_compressors:
378 jffs2_compressors_exit();
379 out:
380 kmem_cache_destroy(jffs2_inode_cachep);
381 return ret;
382}
383
384static void __exit exit_jffs2_fs(void)
385{
386 unregister_filesystem(&jffs2_fs_type);
387 jffs2_destroy_slab_caches();
388 jffs2_compressors_exit();
389 kmem_cache_destroy(jffs2_inode_cachep);
390}
391
392module_init(init_jffs2_fs);
393module_exit(exit_jffs2_fs);
394
395MODULE_DESCRIPTION("The Journalling Flash File System, v2");
396MODULE_AUTHOR("Red Hat, Inc.");
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000397MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 // the sake of this tag. It's Free Software.