blob: 2378a662c2561660e10a592937f771bf74d8241e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
4 * Copyright (C) 2001-2003 Red Hat, Inc.
5 *
6 * Created by David Woodhouse <dwmw2@infradead.org>
7 *
8 * For licensing information, see the file 'LICENCE' in this directory.
9 *
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000010 * $Id: super.c,v 1.110 2005/11/07 11:14:42 gleixner Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 */
13
14#include <linux/config.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/slab.h>
18#include <linux/init.h>
19#include <linux/list.h>
20#include <linux/fs.h>
21#include <linux/mount.h>
22#include <linux/jffs2.h>
23#include <linux/pagemap.h>
24#include <linux/mtd/mtd.h>
25#include <linux/ctype.h>
26#include <linux/namei.h>
27#include "compr.h"
28#include "nodelist.h"
29
30static void jffs2_put_super(struct super_block *);
31
32static kmem_cache_t *jffs2_inode_cachep;
33
34static struct inode *jffs2_alloc_inode(struct super_block *sb)
35{
36 struct jffs2_inode_info *ei;
37 ei = (struct jffs2_inode_info *)kmem_cache_alloc(jffs2_inode_cachep, SLAB_KERNEL);
38 if (!ei)
39 return NULL;
40 return &ei->vfs_inode;
41}
42
43static void jffs2_destroy_inode(struct inode *inode)
44{
45 kmem_cache_free(jffs2_inode_cachep, JFFS2_INODE_INFO(inode));
46}
47
48static void jffs2_i_init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
49{
50 struct jffs2_inode_info *ei = (struct jffs2_inode_info *) foo;
51
52 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
53 SLAB_CTOR_CONSTRUCTOR) {
Thomas Gleixner21eeb7a2005-11-29 16:57:17 +010054 init_MUTEX(&ei->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 inode_init_once(&ei->vfs_inode);
56 }
57}
58
59static int jffs2_sync_fs(struct super_block *sb, int wait)
60{
61 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
62
63 down(&c->alloc_sem);
64 jffs2_flush_wbuf_pad(c);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000065 up(&c->alloc_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return 0;
67}
68
69static struct super_operations jffs2_super_operations =
70{
71 .alloc_inode = jffs2_alloc_inode,
72 .destroy_inode =jffs2_destroy_inode,
73 .read_inode = jffs2_read_inode,
74 .put_super = jffs2_put_super,
75 .write_super = jffs2_write_super,
76 .statfs = jffs2_statfs,
77 .remount_fs = jffs2_remount_fs,
78 .clear_inode = jffs2_clear_inode,
79 .dirty_inode = jffs2_dirty_inode,
80 .sync_fs = jffs2_sync_fs,
81};
82
83static int jffs2_sb_compare(struct super_block *sb, void *data)
84{
85 struct jffs2_sb_info *p = data;
86 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
87
88 /* The superblocks are considered to be equivalent if the underlying MTD
89 device is the same one */
90 if (c->mtd == p->mtd) {
91 D1(printk(KERN_DEBUG "jffs2_sb_compare: match on device %d (\"%s\")\n", p->mtd->index, p->mtd->name));
92 return 1;
93 } else {
94 D1(printk(KERN_DEBUG "jffs2_sb_compare: No match, device %d (\"%s\"), device %d (\"%s\")\n",
95 c->mtd->index, c->mtd->name, p->mtd->index, p->mtd->name));
96 return 0;
97 }
98}
99
100static int jffs2_sb_set(struct super_block *sb, void *data)
101{
102 struct jffs2_sb_info *p = data;
103
104 /* For persistence of NFS exports etc. we use the same s_dev
105 each time we mount the device, don't just use an anonymous
106 device */
107 sb->s_fs_info = p;
108 p->os_priv = sb;
109 sb->s_dev = MKDEV(MTD_BLOCK_MAJOR, p->mtd->index);
110
111 return 0;
112}
113
David Howells454e2392006-06-23 02:02:57 -0700114static int jffs2_get_sb_mtd(struct file_system_type *fs_type,
115 int flags, const char *dev_name,
116 void *data, struct mtd_info *mtd,
117 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 struct super_block *sb;
120 struct jffs2_sb_info *c;
121 int ret;
122
123 c = kmalloc(sizeof(*c), GFP_KERNEL);
124 if (!c)
David Howells454e2392006-06-23 02:02:57 -0700125 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 memset(c, 0, sizeof(*c));
127 c->mtd = mtd;
128
129 sb = sget(fs_type, jffs2_sb_compare, jffs2_sb_set, c);
130
131 if (IS_ERR(sb))
David Howells454e2392006-06-23 02:02:57 -0700132 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 if (sb->s_root) {
135 /* New mountpoint for JFFS2 which is already mounted */
136 D1(printk(KERN_DEBUG "jffs2_get_sb_mtd(): Device %d (\"%s\") is already mounted\n",
137 mtd->index, mtd->name));
David Howells454e2392006-06-23 02:02:57 -0700138 ret = simple_set_mnt(mnt, sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 goto out_put;
140 }
141
142 D1(printk(KERN_DEBUG "jffs2_get_sb_mtd(): New superblock for device %d (\"%s\")\n",
143 mtd->index, mtd->name));
144
Artem B. Bityuckiyb6220592005-07-12 17:37:12 +0100145 /* Initialize JFFS2 superblock locks, the further initialization will be
146 * done later */
147 init_MUTEX(&c->alloc_sem);
148 init_MUTEX(&c->erase_free_sem);
149 init_waitqueue_head(&c->erase_wait);
150 init_waitqueue_head(&c->inocache_wq);
151 spin_lock_init(&c->erase_completion_lock);
152 spin_lock_init(&c->inocache_lock);
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 sb->s_op = &jffs2_super_operations;
155 sb->s_flags = flags | MS_NOATIME;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900156 sb->s_xattr = jffs2_xattr_handlers;
157#ifdef CONFIG_JFFS2_FS_POSIX_ACL
158 sb->s_flags |= MS_POSIXACL;
159#endif
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800160 ret = jffs2_do_fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 if (ret) {
163 /* Failure case... */
164 up_write(&sb->s_umount);
165 deactivate_super(sb);
David Howells454e2392006-06-23 02:02:57 -0700166 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168
169 sb->s_flags |= MS_ACTIVE;
David Howells454e2392006-06-23 02:02:57 -0700170 return simple_set_mnt(mnt, sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
David Howells454e2392006-06-23 02:02:57 -0700172out_error:
173 ret = PTR_ERR(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 out_put:
175 kfree(c);
176 put_mtd_device(mtd);
177
David Howells454e2392006-06-23 02:02:57 -0700178 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
David Howells454e2392006-06-23 02:02:57 -0700181static int jffs2_get_sb_mtdnr(struct file_system_type *fs_type,
182 int flags, const char *dev_name,
183 void *data, int mtdnr,
184 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 struct mtd_info *mtd;
187
188 mtd = get_mtd_device(NULL, mtdnr);
189 if (!mtd) {
190 D1(printk(KERN_DEBUG "jffs2: MTD device #%u doesn't appear to exist\n", mtdnr));
David Howells454e2392006-06-23 02:02:57 -0700191 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
193
David Howells454e2392006-06-23 02:02:57 -0700194 return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
David Howells454e2392006-06-23 02:02:57 -0700197static int jffs2_get_sb(struct file_system_type *fs_type,
198 int flags, const char *dev_name,
199 void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 int err;
202 struct nameidata nd;
203 int mtdnr;
204
205 if (!dev_name)
David Howells454e2392006-06-23 02:02:57 -0700206 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 D1(printk(KERN_DEBUG "jffs2_get_sb(): dev_name \"%s\"\n", dev_name));
209
210 /* The preferred way of mounting in future; especially when
211 CONFIG_BLK_DEV is implemented - we specify the underlying
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000212 MTD device by number or by name, so that we don't require
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 block device support to be present in the kernel. */
214
215 /* FIXME: How to do the root fs this way? */
216
217 if (dev_name[0] == 'm' && dev_name[1] == 't' && dev_name[2] == 'd') {
218 /* Probably mounting without the blkdev crap */
219 if (dev_name[3] == ':') {
220 struct mtd_info *mtd;
221
222 /* Mount by MTD device name */
223 D1(printk(KERN_DEBUG "jffs2_get_sb(): mtd:%%s, name \"%s\"\n", dev_name+4));
224 for (mtdnr = 0; mtdnr < MAX_MTD_DEVICES; mtdnr++) {
225 mtd = get_mtd_device(NULL, mtdnr);
226 if (mtd) {
227 if (!strcmp(mtd->name, dev_name+4))
David Howells454e2392006-06-23 02:02:57 -0700228 return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 put_mtd_device(mtd);
230 }
231 }
232 printk(KERN_NOTICE "jffs2_get_sb(): MTD device with name \"%s\" not found.\n", dev_name+4);
233 } else if (isdigit(dev_name[3])) {
234 /* Mount by MTD device number name */
235 char *endptr;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 mtdnr = simple_strtoul(dev_name+3, &endptr, 0);
238 if (!*endptr) {
239 /* It was a valid number */
240 D1(printk(KERN_DEBUG "jffs2_get_sb(): mtd%%d, mtdnr %d\n", mtdnr));
David Howells454e2392006-06-23 02:02:57 -0700241 return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243 }
244 }
245
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000246 /* Try the old way - the hack where we allowed users to mount
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 /dev/mtdblock$(n) but didn't actually _use_ the blkdev */
248
249 err = path_lookup(dev_name, LOOKUP_FOLLOW, &nd);
250
251 D1(printk(KERN_DEBUG "jffs2_get_sb(): path_lookup() returned %d, inode %p\n",
252 err, nd.dentry->d_inode));
253
254 if (err)
David Howells454e2392006-06-23 02:02:57 -0700255 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 err = -EINVAL;
258
259 if (!S_ISBLK(nd.dentry->d_inode->i_mode))
260 goto out;
261
262 if (nd.mnt->mnt_flags & MNT_NODEV) {
263 err = -EACCES;
264 goto out;
265 }
266
267 if (imajor(nd.dentry->d_inode) != MTD_BLOCK_MAJOR) {
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800268 if (!(flags & MS_SILENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 printk(KERN_NOTICE "Attempt to mount non-MTD device \"%s\" as JFFS2\n",
270 dev_name);
271 goto out;
272 }
273
274 mtdnr = iminor(nd.dentry->d_inode);
275 path_release(&nd);
276
David Howells454e2392006-06-23 02:02:57 -0700277 return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279out:
280 path_release(&nd);
David Howells454e2392006-06-23 02:02:57 -0700281 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
284static void jffs2_put_super (struct super_block *sb)
285{
286 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
287
288 D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 down(&c->alloc_sem);
291 jffs2_flush_wbuf_pad(c);
292 up(&c->alloc_sem);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100293
294 jffs2_sum_exit(c);
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 jffs2_free_ino_caches(c);
297 jffs2_free_raw_node_refs(c);
Ferenc Havasi4ce1f562005-08-31 14:51:04 +0100298 if (jffs2_blocks_use_vmalloc(c))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 vfree(c->blocks);
300 else
301 kfree(c->blocks);
302 jffs2_flash_cleanup(c);
303 kfree(c->inocache_list);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900304 jffs2_clear_xattr_subsystem(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (c->mtd->sync)
306 c->mtd->sync(c->mtd);
307
308 D1(printk(KERN_DEBUG "jffs2_put_super returning\n"));
309}
310
311static void jffs2_kill_sb(struct super_block *sb)
312{
313 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
Artem B. Bityuckiya69dde92005-05-18 12:37:28 +0100314 if (!(sb->s_flags & MS_RDONLY))
315 jffs2_stop_garbage_collect_thread(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 generic_shutdown_super(sb);
317 put_mtd_device(c->mtd);
318 kfree(c);
319}
320
321static struct file_system_type jffs2_fs_type = {
322 .owner = THIS_MODULE,
323 .name = "jffs2",
324 .get_sb = jffs2_get_sb,
325 .kill_sb = jffs2_kill_sb,
326};
327
328static int __init init_jffs2_fs(void)
329{
330 int ret;
331
David Woodhouse3e68fbb2006-05-15 00:49:43 +0100332 /* Paranoia checks for on-medium structures. If we ask GCC
333 to pack them with __attribute__((packed)) then it _also_
334 assumes that they're not aligned -- so it emits crappy
335 code on some architectures. Ideally we want an attribute
336 which means just 'no padding', without the alignment
337 thing. But GCC doesn't have that -- we have to just
338 hope the structs are the right sizes, instead. */
339 BUG_ON(sizeof(struct jffs2_unknown_node) != 12);
340 BUG_ON(sizeof(struct jffs2_raw_dirent) != 40);
341 BUG_ON(sizeof(struct jffs2_raw_inode) != 68);
342 BUG_ON(sizeof(struct jffs2_raw_summary) != 32);
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 printk(KERN_INFO "JFFS2 version 2.2."
Andrew Victor2f82ce12005-02-09 09:24:26 +0000345#ifdef CONFIG_JFFS2_FS_WRITEBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 " (NAND)"
347#endif
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100348#ifdef CONFIG_JFFS2_SUMMARY
349 " (SUMMARY) "
350#endif
David Woodhouse3e68fbb2006-05-15 00:49:43 +0100351 " (C) 2001-2006 Red Hat, Inc.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 jffs2_inode_cachep = kmem_cache_create("jffs2_i",
354 sizeof(struct jffs2_inode_info),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800355 0, (SLAB_RECLAIM_ACCOUNT|
356 SLAB_MEM_SPREAD),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 jffs2_i_init_once, NULL);
358 if (!jffs2_inode_cachep) {
359 printk(KERN_ERR "JFFS2 error: Failed to initialise inode cache\n");
360 return -ENOMEM;
361 }
362 ret = jffs2_compressors_init();
363 if (ret) {
364 printk(KERN_ERR "JFFS2 error: Failed to initialise compressors\n");
365 goto out;
366 }
367 ret = jffs2_create_slab_caches();
368 if (ret) {
369 printk(KERN_ERR "JFFS2 error: Failed to initialise slab caches\n");
370 goto out_compressors;
371 }
372 ret = register_filesystem(&jffs2_fs_type);
373 if (ret) {
374 printk(KERN_ERR "JFFS2 error: Failed to register filesystem\n");
375 goto out_slab;
376 }
377 return 0;
378
379 out_slab:
380 jffs2_destroy_slab_caches();
381 out_compressors:
382 jffs2_compressors_exit();
383 out:
384 kmem_cache_destroy(jffs2_inode_cachep);
385 return ret;
386}
387
388static void __exit exit_jffs2_fs(void)
389{
390 unregister_filesystem(&jffs2_fs_type);
391 jffs2_destroy_slab_caches();
392 jffs2_compressors_exit();
393 kmem_cache_destroy(jffs2_inode_cachep);
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.");
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000401MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 // the sake of this tag. It's Free Software.