blob: 6de374513c010790ab15e40f4103781f669d93c8 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/slab.h>
17#include <linux/init.h>
18#include <linux/list.h>
19#include <linux/fs.h>
20#include <linux/mount.h>
21#include <linux/jffs2.h>
22#include <linux/pagemap.h>
23#include <linux/mtd/mtd.h>
24#include <linux/ctype.h>
25#include <linux/namei.h>
26#include "compr.h"
27#include "nodelist.h"
28
29static void jffs2_put_super(struct super_block *);
30
31static kmem_cache_t *jffs2_inode_cachep;
32
33static struct inode *jffs2_alloc_inode(struct super_block *sb)
34{
35 struct jffs2_inode_info *ei;
36 ei = (struct jffs2_inode_info *)kmem_cache_alloc(jffs2_inode_cachep, SLAB_KERNEL);
37 if (!ei)
38 return NULL;
39 return &ei->vfs_inode;
40}
41
42static void jffs2_destroy_inode(struct inode *inode)
43{
44 kmem_cache_free(jffs2_inode_cachep, JFFS2_INODE_INFO(inode));
45}
46
47static void jffs2_i_init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
48{
49 struct jffs2_inode_info *ei = (struct jffs2_inode_info *) foo;
50
51 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
52 SLAB_CTOR_CONSTRUCTOR) {
Thomas Gleixner21eeb7a2005-11-29 16:57:17 +010053 init_MUTEX(&ei->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 inode_init_once(&ei->vfs_inode);
55 }
56}
57
58static int jffs2_sync_fs(struct super_block *sb, int wait)
59{
60 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
61
62 down(&c->alloc_sem);
63 jffs2_flush_wbuf_pad(c);
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000064 up(&c->alloc_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return 0;
66}
67
68static struct super_operations jffs2_super_operations =
69{
70 .alloc_inode = jffs2_alloc_inode,
71 .destroy_inode =jffs2_destroy_inode,
72 .read_inode = jffs2_read_inode,
73 .put_super = jffs2_put_super,
74 .write_super = jffs2_write_super,
75 .statfs = jffs2_statfs,
76 .remount_fs = jffs2_remount_fs,
77 .clear_inode = jffs2_clear_inode,
78 .dirty_inode = jffs2_dirty_inode,
79 .sync_fs = jffs2_sync_fs,
80};
81
82static int jffs2_sb_compare(struct super_block *sb, void *data)
83{
84 struct jffs2_sb_info *p = data;
85 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
86
87 /* The superblocks are considered to be equivalent if the underlying MTD
88 device is the same one */
89 if (c->mtd == p->mtd) {
90 D1(printk(KERN_DEBUG "jffs2_sb_compare: match on device %d (\"%s\")\n", p->mtd->index, p->mtd->name));
91 return 1;
92 } else {
93 D1(printk(KERN_DEBUG "jffs2_sb_compare: No match, device %d (\"%s\"), device %d (\"%s\")\n",
94 c->mtd->index, c->mtd->name, p->mtd->index, p->mtd->name));
95 return 0;
96 }
97}
98
99static int jffs2_sb_set(struct super_block *sb, void *data)
100{
101 struct jffs2_sb_info *p = data;
102
103 /* For persistence of NFS exports etc. we use the same s_dev
104 each time we mount the device, don't just use an anonymous
105 device */
106 sb->s_fs_info = p;
107 p->os_priv = sb;
108 sb->s_dev = MKDEV(MTD_BLOCK_MAJOR, p->mtd->index);
109
110 return 0;
111}
112
David Howells454e2392006-06-23 02:02:57 -0700113static int jffs2_get_sb_mtd(struct file_system_type *fs_type,
114 int flags, const char *dev_name,
115 void *data, struct mtd_info *mtd,
116 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 struct super_block *sb;
119 struct jffs2_sb_info *c;
120 int ret;
121
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -0700122 c = kzalloc(sizeof(*c), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (!c)
David Howells454e2392006-06-23 02:02:57 -0700124 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 c->mtd = mtd;
126
127 sb = sget(fs_type, jffs2_sb_compare, jffs2_sb_set, c);
128
129 if (IS_ERR(sb))
David Howells454e2392006-06-23 02:02:57 -0700130 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 if (sb->s_root) {
133 /* New mountpoint for JFFS2 which is already mounted */
134 D1(printk(KERN_DEBUG "jffs2_get_sb_mtd(): Device %d (\"%s\") is already mounted\n",
135 mtd->index, mtd->name));
David Howells454e2392006-06-23 02:02:57 -0700136 ret = simple_set_mnt(mnt, sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 goto out_put;
138 }
139
140 D1(printk(KERN_DEBUG "jffs2_get_sb_mtd(): New superblock for device %d (\"%s\")\n",
141 mtd->index, mtd->name));
142
Artem B. Bityuckiyb6220592005-07-12 17:37:12 +0100143 /* Initialize JFFS2 superblock locks, the further initialization will be
144 * done later */
145 init_MUTEX(&c->alloc_sem);
146 init_MUTEX(&c->erase_free_sem);
147 init_waitqueue_head(&c->erase_wait);
148 init_waitqueue_head(&c->inocache_wq);
149 spin_lock_init(&c->erase_completion_lock);
150 spin_lock_init(&c->inocache_lock);
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 sb->s_op = &jffs2_super_operations;
153 sb->s_flags = flags | MS_NOATIME;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900154 sb->s_xattr = jffs2_xattr_handlers;
155#ifdef CONFIG_JFFS2_FS_POSIX_ACL
156 sb->s_flags |= MS_POSIXACL;
157#endif
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800158 ret = jffs2_do_fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 if (ret) {
161 /* Failure case... */
162 up_write(&sb->s_umount);
163 deactivate_super(sb);
David Howells454e2392006-06-23 02:02:57 -0700164 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166
167 sb->s_flags |= MS_ACTIVE;
David Howells454e2392006-06-23 02:02:57 -0700168 return simple_set_mnt(mnt, sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
David Howells454e2392006-06-23 02:02:57 -0700170out_error:
171 ret = PTR_ERR(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 out_put:
173 kfree(c);
174 put_mtd_device(mtd);
175
David Howells454e2392006-06-23 02:02:57 -0700176 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
David Howells454e2392006-06-23 02:02:57 -0700179static int jffs2_get_sb_mtdnr(struct file_system_type *fs_type,
180 int flags, const char *dev_name,
181 void *data, int mtdnr,
182 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 struct mtd_info *mtd;
185
186 mtd = get_mtd_device(NULL, mtdnr);
187 if (!mtd) {
188 D1(printk(KERN_DEBUG "jffs2: MTD device #%u doesn't appear to exist\n", mtdnr));
David Howells454e2392006-06-23 02:02:57 -0700189 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
191
David Howells454e2392006-06-23 02:02:57 -0700192 return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
David Howells454e2392006-06-23 02:02:57 -0700195static int jffs2_get_sb(struct file_system_type *fs_type,
196 int flags, const char *dev_name,
197 void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 int err;
200 struct nameidata nd;
201 int mtdnr;
202
203 if (!dev_name)
David Howells454e2392006-06-23 02:02:57 -0700204 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 D1(printk(KERN_DEBUG "jffs2_get_sb(): dev_name \"%s\"\n", dev_name));
207
208 /* The preferred way of mounting in future; especially when
209 CONFIG_BLK_DEV is implemented - we specify the underlying
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000210 MTD device by number or by name, so that we don't require
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 block device support to be present in the kernel. */
212
213 /* FIXME: How to do the root fs this way? */
214
215 if (dev_name[0] == 'm' && dev_name[1] == 't' && dev_name[2] == 'd') {
216 /* Probably mounting without the blkdev crap */
217 if (dev_name[3] == ':') {
218 struct mtd_info *mtd;
219
220 /* Mount by MTD device name */
221 D1(printk(KERN_DEBUG "jffs2_get_sb(): mtd:%%s, name \"%s\"\n", dev_name+4));
222 for (mtdnr = 0; mtdnr < MAX_MTD_DEVICES; mtdnr++) {
223 mtd = get_mtd_device(NULL, mtdnr);
224 if (mtd) {
225 if (!strcmp(mtd->name, dev_name+4))
David Howells454e2392006-06-23 02:02:57 -0700226 return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 put_mtd_device(mtd);
228 }
229 }
230 printk(KERN_NOTICE "jffs2_get_sb(): MTD device with name \"%s\" not found.\n", dev_name+4);
231 } else if (isdigit(dev_name[3])) {
232 /* Mount by MTD device number name */
233 char *endptr;
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 mtdnr = simple_strtoul(dev_name+3, &endptr, 0);
236 if (!*endptr) {
237 /* It was a valid number */
238 D1(printk(KERN_DEBUG "jffs2_get_sb(): mtd%%d, mtdnr %d\n", mtdnr));
David Howells454e2392006-06-23 02:02:57 -0700239 return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241 }
242 }
243
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000244 /* Try the old way - the hack where we allowed users to mount
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 /dev/mtdblock$(n) but didn't actually _use_ the blkdev */
246
247 err = path_lookup(dev_name, LOOKUP_FOLLOW, &nd);
248
249 D1(printk(KERN_DEBUG "jffs2_get_sb(): path_lookup() returned %d, inode %p\n",
250 err, nd.dentry->d_inode));
251
252 if (err)
David Howells454e2392006-06-23 02:02:57 -0700253 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 err = -EINVAL;
256
257 if (!S_ISBLK(nd.dentry->d_inode->i_mode))
258 goto out;
259
260 if (nd.mnt->mnt_flags & MNT_NODEV) {
261 err = -EACCES;
262 goto out;
263 }
264
265 if (imajor(nd.dentry->d_inode) != MTD_BLOCK_MAJOR) {
Theodore Ts'o9b04c992006-03-24 03:15:10 -0800266 if (!(flags & MS_SILENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 printk(KERN_NOTICE "Attempt to mount non-MTD device \"%s\" as JFFS2\n",
268 dev_name);
269 goto out;
270 }
271
272 mtdnr = iminor(nd.dentry->d_inode);
273 path_release(&nd);
274
David Howells454e2392006-06-23 02:02:57 -0700275 return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277out:
278 path_release(&nd);
David Howells454e2392006-06-23 02:02:57 -0700279 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282static void jffs2_put_super (struct super_block *sb)
283{
284 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
285
286 D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 down(&c->alloc_sem);
289 jffs2_flush_wbuf_pad(c);
290 up(&c->alloc_sem);
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100291
292 jffs2_sum_exit(c);
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 jffs2_free_ino_caches(c);
295 jffs2_free_raw_node_refs(c);
Ferenc Havasi4ce1f562005-08-31 14:51:04 +0100296 if (jffs2_blocks_use_vmalloc(c))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 vfree(c->blocks);
298 else
299 kfree(c->blocks);
300 jffs2_flash_cleanup(c);
301 kfree(c->inocache_list);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900302 jffs2_clear_xattr_subsystem(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (c->mtd->sync)
304 c->mtd->sync(c->mtd);
305
306 D1(printk(KERN_DEBUG "jffs2_put_super returning\n"));
307}
308
309static void jffs2_kill_sb(struct super_block *sb)
310{
311 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
Artem B. Bityuckiya69dde92005-05-18 12:37:28 +0100312 if (!(sb->s_flags & MS_RDONLY))
313 jffs2_stop_garbage_collect_thread(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 generic_shutdown_super(sb);
315 put_mtd_device(c->mtd);
316 kfree(c);
317}
318
319static struct file_system_type jffs2_fs_type = {
320 .owner = THIS_MODULE,
321 .name = "jffs2",
322 .get_sb = jffs2_get_sb,
323 .kill_sb = jffs2_kill_sb,
324};
325
326static int __init init_jffs2_fs(void)
327{
328 int ret;
329
David Woodhouse3e68fbb2006-05-15 00:49:43 +0100330 /* Paranoia checks for on-medium structures. If we ask GCC
331 to pack them with __attribute__((packed)) then it _also_
332 assumes that they're not aligned -- so it emits crappy
333 code on some architectures. Ideally we want an attribute
334 which means just 'no padding', without the alignment
335 thing. But GCC doesn't have that -- we have to just
336 hope the structs are the right sizes, instead. */
337 BUG_ON(sizeof(struct jffs2_unknown_node) != 12);
338 BUG_ON(sizeof(struct jffs2_raw_dirent) != 40);
339 BUG_ON(sizeof(struct jffs2_raw_inode) != 68);
340 BUG_ON(sizeof(struct jffs2_raw_summary) != 32);
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 printk(KERN_INFO "JFFS2 version 2.2."
Andrew Victor2f82ce12005-02-09 09:24:26 +0000343#ifdef CONFIG_JFFS2_FS_WRITEBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 " (NAND)"
345#endif
Ferenc Havasie631ddb2005-09-07 09:35:26 +0100346#ifdef CONFIG_JFFS2_SUMMARY
347 " (SUMMARY) "
348#endif
David Woodhouse3e68fbb2006-05-15 00:49:43 +0100349 " (C) 2001-2006 Red Hat, Inc.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 jffs2_inode_cachep = kmem_cache_create("jffs2_i",
352 sizeof(struct jffs2_inode_info),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800353 0, (SLAB_RECLAIM_ACCOUNT|
354 SLAB_MEM_SPREAD),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 jffs2_i_init_once, NULL);
356 if (!jffs2_inode_cachep) {
357 printk(KERN_ERR "JFFS2 error: Failed to initialise inode cache\n");
358 return -ENOMEM;
359 }
360 ret = jffs2_compressors_init();
361 if (ret) {
362 printk(KERN_ERR "JFFS2 error: Failed to initialise compressors\n");
363 goto out;
364 }
365 ret = jffs2_create_slab_caches();
366 if (ret) {
367 printk(KERN_ERR "JFFS2 error: Failed to initialise slab caches\n");
368 goto out_compressors;
369 }
370 ret = register_filesystem(&jffs2_fs_type);
371 if (ret) {
372 printk(KERN_ERR "JFFS2 error: Failed to register filesystem\n");
373 goto out_slab;
374 }
375 return 0;
376
377 out_slab:
378 jffs2_destroy_slab_caches();
379 out_compressors:
380 jffs2_compressors_exit();
381 out:
382 kmem_cache_destroy(jffs2_inode_cachep);
383 return ret;
384}
385
386static void __exit exit_jffs2_fs(void)
387{
388 unregister_filesystem(&jffs2_fs_type);
389 jffs2_destroy_slab_caches();
390 jffs2_compressors_exit();
391 kmem_cache_destroy(jffs2_inode_cachep);
392}
393
394module_init(init_jffs2_fs);
395module_exit(exit_jffs2_fs);
396
397MODULE_DESCRIPTION("The Journalling Flash File System, v2");
398MODULE_AUTHOR("Red Hat, Inc.");
Thomas Gleixner182ec4e2005-11-07 11:16:07 +0000399MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 // the sake of this tag. It's Free Software.