blob: aaf9475cfb6ad866e05660121fd1a516650345b0 [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 *
Artem B. Bityuckiyb6220592005-07-12 17:37:12 +010010 * $Id: super.c,v 1.107 2005/07/12 16:37:08 dedekind 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) {
54 init_MUTEX_LOCKED(&ei->sem);
55 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);
65 up(&c->alloc_sem);
66 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
114static struct super_block *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{
118 struct super_block *sb;
119 struct jffs2_sb_info *c;
120 int ret;
121
122 c = kmalloc(sizeof(*c), GFP_KERNEL);
123 if (!c)
124 return ERR_PTR(-ENOMEM);
125 memset(c, 0, sizeof(*c));
126 c->mtd = mtd;
127
128 sb = sget(fs_type, jffs2_sb_compare, jffs2_sb_set, c);
129
130 if (IS_ERR(sb))
131 goto out_put;
132
133 if (sb->s_root) {
134 /* New mountpoint for JFFS2 which is already mounted */
135 D1(printk(KERN_DEBUG "jffs2_get_sb_mtd(): Device %d (\"%s\") is already mounted\n",
136 mtd->index, mtd->name));
137 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;
154
155 ret = jffs2_do_fill_super(sb, data, (flags&MS_VERBOSE)?1:0);
156
157 if (ret) {
158 /* Failure case... */
159 up_write(&sb->s_umount);
160 deactivate_super(sb);
161 return ERR_PTR(ret);
162 }
163
164 sb->s_flags |= MS_ACTIVE;
165 return sb;
166
167 out_put:
168 kfree(c);
169 put_mtd_device(mtd);
170
171 return sb;
172}
173
174static struct super_block *jffs2_get_sb_mtdnr(struct file_system_type *fs_type,
175 int flags, const char *dev_name,
176 void *data, int mtdnr)
177{
178 struct mtd_info *mtd;
179
180 mtd = get_mtd_device(NULL, mtdnr);
181 if (!mtd) {
182 D1(printk(KERN_DEBUG "jffs2: MTD device #%u doesn't appear to exist\n", mtdnr));
183 return ERR_PTR(-EINVAL);
184 }
185
186 return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd);
187}
188
189static struct super_block *jffs2_get_sb(struct file_system_type *fs_type,
190 int flags, const char *dev_name,
191 void *data)
192{
193 int err;
194 struct nameidata nd;
195 int mtdnr;
196
197 if (!dev_name)
198 return ERR_PTR(-EINVAL);
199
200 D1(printk(KERN_DEBUG "jffs2_get_sb(): dev_name \"%s\"\n", dev_name));
201
202 /* The preferred way of mounting in future; especially when
203 CONFIG_BLK_DEV is implemented - we specify the underlying
204 MTD device by number or by name, so that we don't require
205 block device support to be present in the kernel. */
206
207 /* FIXME: How to do the root fs this way? */
208
209 if (dev_name[0] == 'm' && dev_name[1] == 't' && dev_name[2] == 'd') {
210 /* Probably mounting without the blkdev crap */
211 if (dev_name[3] == ':') {
212 struct mtd_info *mtd;
213
214 /* Mount by MTD device name */
215 D1(printk(KERN_DEBUG "jffs2_get_sb(): mtd:%%s, name \"%s\"\n", dev_name+4));
216 for (mtdnr = 0; mtdnr < MAX_MTD_DEVICES; mtdnr++) {
217 mtd = get_mtd_device(NULL, mtdnr);
218 if (mtd) {
219 if (!strcmp(mtd->name, dev_name+4))
220 return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd);
221 put_mtd_device(mtd);
222 }
223 }
224 printk(KERN_NOTICE "jffs2_get_sb(): MTD device with name \"%s\" not found.\n", dev_name+4);
225 } else if (isdigit(dev_name[3])) {
226 /* Mount by MTD device number name */
227 char *endptr;
228
229 mtdnr = simple_strtoul(dev_name+3, &endptr, 0);
230 if (!*endptr) {
231 /* It was a valid number */
232 D1(printk(KERN_DEBUG "jffs2_get_sb(): mtd%%d, mtdnr %d\n", mtdnr));
233 return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr);
234 }
235 }
236 }
237
238 /* Try the old way - the hack where we allowed users to mount
239 /dev/mtdblock$(n) but didn't actually _use_ the blkdev */
240
241 err = path_lookup(dev_name, LOOKUP_FOLLOW, &nd);
242
243 D1(printk(KERN_DEBUG "jffs2_get_sb(): path_lookup() returned %d, inode %p\n",
244 err, nd.dentry->d_inode));
245
246 if (err)
247 return ERR_PTR(err);
248
249 err = -EINVAL;
250
251 if (!S_ISBLK(nd.dentry->d_inode->i_mode))
252 goto out;
253
254 if (nd.mnt->mnt_flags & MNT_NODEV) {
255 err = -EACCES;
256 goto out;
257 }
258
259 if (imajor(nd.dentry->d_inode) != MTD_BLOCK_MAJOR) {
260 if (!(flags & MS_VERBOSE)) /* Yes I mean this. Strangely */
261 printk(KERN_NOTICE "Attempt to mount non-MTD device \"%s\" as JFFS2\n",
262 dev_name);
263 goto out;
264 }
265
266 mtdnr = iminor(nd.dentry->d_inode);
267 path_release(&nd);
268
269 return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr);
270
271out:
272 path_release(&nd);
273 return ERR_PTR(err);
274}
275
276static void jffs2_put_super (struct super_block *sb)
277{
278 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
279
280 D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 down(&c->alloc_sem);
283 jffs2_flush_wbuf_pad(c);
284 up(&c->alloc_sem);
285 jffs2_free_ino_caches(c);
286 jffs2_free_raw_node_refs(c);
287 if (c->mtd->flags & MTD_NO_VIRTBLOCKS)
288 vfree(c->blocks);
289 else
290 kfree(c->blocks);
291 jffs2_flash_cleanup(c);
292 kfree(c->inocache_list);
293 if (c->mtd->sync)
294 c->mtd->sync(c->mtd);
295
296 D1(printk(KERN_DEBUG "jffs2_put_super returning\n"));
297}
298
299static void jffs2_kill_sb(struct super_block *sb)
300{
301 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
Artem B. Bityuckiya69dde92005-05-18 12:37:28 +0100302 if (!(sb->s_flags & MS_RDONLY))
303 jffs2_stop_garbage_collect_thread(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 generic_shutdown_super(sb);
305 put_mtd_device(c->mtd);
306 kfree(c);
307}
308
309static struct file_system_type jffs2_fs_type = {
310 .owner = THIS_MODULE,
311 .name = "jffs2",
312 .get_sb = jffs2_get_sb,
313 .kill_sb = jffs2_kill_sb,
314};
315
316static int __init init_jffs2_fs(void)
317{
318 int ret;
319
320 printk(KERN_INFO "JFFS2 version 2.2."
Andrew Victor2f82ce12005-02-09 09:24:26 +0000321#ifdef CONFIG_JFFS2_FS_WRITEBUFFER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 " (NAND)"
323#endif
324 " (C) 2001-2003 Red Hat, Inc.\n");
325
326 jffs2_inode_cachep = kmem_cache_create("jffs2_i",
327 sizeof(struct jffs2_inode_info),
328 0, SLAB_RECLAIM_ACCOUNT,
329 jffs2_i_init_once, NULL);
330 if (!jffs2_inode_cachep) {
331 printk(KERN_ERR "JFFS2 error: Failed to initialise inode cache\n");
332 return -ENOMEM;
333 }
334 ret = jffs2_compressors_init();
335 if (ret) {
336 printk(KERN_ERR "JFFS2 error: Failed to initialise compressors\n");
337 goto out;
338 }
339 ret = jffs2_create_slab_caches();
340 if (ret) {
341 printk(KERN_ERR "JFFS2 error: Failed to initialise slab caches\n");
342 goto out_compressors;
343 }
344 ret = register_filesystem(&jffs2_fs_type);
345 if (ret) {
346 printk(KERN_ERR "JFFS2 error: Failed to register filesystem\n");
347 goto out_slab;
348 }
349 return 0;
350
351 out_slab:
352 jffs2_destroy_slab_caches();
353 out_compressors:
354 jffs2_compressors_exit();
355 out:
356 kmem_cache_destroy(jffs2_inode_cachep);
357 return ret;
358}
359
360static void __exit exit_jffs2_fs(void)
361{
362 unregister_filesystem(&jffs2_fs_type);
363 jffs2_destroy_slab_caches();
364 jffs2_compressors_exit();
365 kmem_cache_destroy(jffs2_inode_cachep);
366}
367
368module_init(init_jffs2_fs);
369module_exit(exit_jffs2_fs);
370
371MODULE_DESCRIPTION("The Journalling Flash File System, v2");
372MODULE_AUTHOR("Red Hat, Inc.");
373MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for
374 // the sake of this tag. It's Free Software.