MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / fs / jffs2.org / super.c
blob1121a74a59e80699e5e9a0e59b8bbc3ae1ea3918
1 /*
2 * JFFS2 -- Journalling Flash File System, Version 2.
4 * Copyright (C) 2001-2003 Red Hat, Inc.
6 * Created by David Woodhouse <dwmw2@redhat.com>
8 * For licensing information, see the file 'LICENCE' in this directory.
10 * $Id: super.c,v 1.99 2004/08/24 07:59:57 dwmw2 Exp $
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"
30 static void jffs2_put_super(struct super_block *);
32 static kmem_cache_t *jffs2_inode_cachep;
34 static struct inode *jffs2_alloc_inode(struct super_block *sb)
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;
43 static void jffs2_destroy_inode(struct inode *inode)
45 kmem_cache_free(jffs2_inode_cachep, JFFS2_INODE_INFO(inode));
48 static void jffs2_i_init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
50 struct jffs2_inode_info *ei = (struct jffs2_inode_info *) foo;
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);
59 static struct super_operations jffs2_super_operations =
61 .alloc_inode = jffs2_alloc_inode,
62 .destroy_inode =jffs2_destroy_inode,
63 .read_inode = jffs2_read_inode,
64 .put_super = jffs2_put_super,
65 .write_super = jffs2_write_super,
66 .statfs = jffs2_statfs,
67 .remount_fs = jffs2_remount_fs,
68 .clear_inode = jffs2_clear_inode,
69 .dirty_inode = jffs2_dirty_inode,
72 static int jffs2_sb_compare(struct super_block *sb, void *data)
74 struct jffs2_sb_info *p = data;
75 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
77 /* The superblocks are considered to be equivalent if the underlying MTD
78 device is the same one */
79 if (c->mtd == p->mtd) {
80 D1(printk(KERN_DEBUG "jffs2_sb_compare: match on device %d (\"%s\")\n", p->mtd->index, p->mtd->name));
81 return 1;
82 } else {
83 D1(printk(KERN_DEBUG "jffs2_sb_compare: No match, device %d (\"%s\"), device %d (\"%s\")\n",
84 c->mtd->index, c->mtd->name, p->mtd->index, p->mtd->name));
85 return 0;
89 static int jffs2_sb_set(struct super_block *sb, void *data)
91 struct jffs2_sb_info *p = data;
93 /* For persistence of NFS exports etc. we use the same s_dev
94 each time we mount the device, don't just use an anonymous
95 device */
96 sb->s_fs_info = p;
97 p->os_priv = sb;
98 sb->s_dev = MKDEV(MTD_BLOCK_MAJOR, p->mtd->index);
100 return 0;
103 static struct super_block *jffs2_get_sb_mtd(struct file_system_type *fs_type,
104 int flags, const char *dev_name,
105 void *data, struct mtd_info *mtd)
107 struct super_block *sb;
108 struct jffs2_sb_info *c;
109 int ret;
111 c = kmalloc(sizeof(*c), GFP_KERNEL);
112 if (!c)
113 return ERR_PTR(-ENOMEM);
114 memset(c, 0, sizeof(*c));
115 c->mtd = mtd;
117 sb = sget(fs_type, jffs2_sb_compare, jffs2_sb_set, c);
119 if (IS_ERR(sb))
120 goto out_put;
122 if (sb->s_root) {
123 /* New mountpoint for JFFS2 which is already mounted */
124 D1(printk(KERN_DEBUG "jffs2_get_sb_mtd(): Device %d (\"%s\") is already mounted\n",
125 mtd->index, mtd->name));
126 goto out_put;
129 D1(printk(KERN_DEBUG "jffs2_get_sb_mtd(): New superblock for device %d (\"%s\")\n",
130 mtd->index, mtd->name));
132 sb->s_op = &jffs2_super_operations;
133 sb->s_flags = flags | MS_NOATIME;
135 ret = jffs2_do_fill_super(sb, data, (flags&MS_VERBOSE)?1:0);
137 if (ret) {
138 /* Failure case... */
139 up_write(&sb->s_umount);
140 deactivate_super(sb);
141 return ERR_PTR(ret);
144 sb->s_flags |= MS_ACTIVE;
145 return sb;
147 out_put:
148 kfree(c);
149 put_mtd_device(mtd);
151 return sb;
154 static struct super_block *jffs2_get_sb_mtdnr(struct file_system_type *fs_type,
155 int flags, const char *dev_name,
156 void *data, int mtdnr)
158 struct mtd_info *mtd;
160 mtd = get_mtd_device(NULL, mtdnr);
161 if (!mtd) {
162 D1(printk(KERN_DEBUG "jffs2: MTD device #%u doesn't appear to exist\n", mtdnr));
163 return ERR_PTR(-EINVAL);
166 return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd);
169 static struct super_block *jffs2_get_sb(struct file_system_type *fs_type,
170 int flags, const char *dev_name,
171 void *data)
173 int err;
174 struct nameidata nd;
175 int mtdnr;
177 if (!dev_name)
178 return ERR_PTR(-EINVAL);
180 D1(printk(KERN_DEBUG "jffs2_get_sb(): dev_name \"%s\"\n", dev_name));
182 /* The preferred way of mounting in future; especially when
183 CONFIG_BLK_DEV is implemented - we specify the underlying
184 MTD device by number or by name, so that we don't require
185 block device support to be present in the kernel. */
187 /* FIXME: How to do the root fs this way? */
189 if (dev_name[0] == 'm' && dev_name[1] == 't' && dev_name[2] == 'd') {
190 /* Probably mounting without the blkdev crap */
191 if (dev_name[3] == ':') {
192 struct mtd_info *mtd;
194 /* Mount by MTD device name */
195 D1(printk(KERN_DEBUG "jffs2_get_sb(): mtd:%%s, name \"%s\"\n", dev_name+4));
196 for (mtdnr = 0; mtdnr < MAX_MTD_DEVICES; mtdnr++) {
197 mtd = get_mtd_device(NULL, mtdnr);
198 if (mtd) {
199 if (!strcmp(mtd->name, dev_name+4))
200 return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd);
201 put_mtd_device(mtd);
204 printk(KERN_NOTICE "jffs2_get_sb(): MTD device with name \"%s\" not found.\n", dev_name+4);
205 } else if (isdigit(dev_name[3])) {
206 /* Mount by MTD device number name */
207 char *endptr;
209 mtdnr = simple_strtoul(dev_name+3, &endptr, 0);
210 if (!*endptr) {
211 /* It was a valid number */
212 D1(printk(KERN_DEBUG "jffs2_get_sb(): mtd%%d, mtdnr %d\n", mtdnr));
213 return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr);
218 /* Try the old way - the hack where we allowed users to mount
219 /dev/mtdblock$(n) but didn't actually _use_ the blkdev */
221 err = path_lookup(dev_name, LOOKUP_FOLLOW, &nd);
223 D1(printk(KERN_DEBUG "jffs2_get_sb(): path_lookup() returned %d, inode %p\n",
224 err, nd.dentry->d_inode));
226 if (err)
227 return ERR_PTR(err);
229 err = -EINVAL;
231 if (!S_ISBLK(nd.dentry->d_inode->i_mode))
232 goto out;
234 if (nd.mnt->mnt_flags & MNT_NODEV) {
235 err = -EACCES;
236 goto out;
239 if (imajor(nd.dentry->d_inode) != MTD_BLOCK_MAJOR) {
240 if (!(flags & MS_VERBOSE)) /* Yes I mean this. Strangely */
241 printk(KERN_NOTICE "Attempt to mount non-MTD device \"%s\" as JFFS2\n",
242 dev_name);
243 goto out;
246 mtdnr = iminor(nd.dentry->d_inode);
247 path_release(&nd);
249 return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr);
251 out:
252 path_release(&nd);
253 return ERR_PTR(err);
256 static void jffs2_put_super (struct super_block *sb)
258 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
260 D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));
262 if (!(sb->s_flags & MS_RDONLY))
263 jffs2_stop_garbage_collect_thread(c);
264 down(&c->alloc_sem);
265 jffs2_flush_wbuf_pad(c);
266 up(&c->alloc_sem);
267 jffs2_free_ino_caches(c);
268 jffs2_free_raw_node_refs(c);
269 kfree(c->blocks);
270 jffs2_flash_cleanup(c);
271 kfree(c->inocache_list);
272 if (c->mtd->sync)
273 c->mtd->sync(c->mtd);
275 D1(printk(KERN_DEBUG "jffs2_put_super returning\n"));
278 static void jffs2_kill_sb(struct super_block *sb)
280 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
281 generic_shutdown_super(sb);
282 put_mtd_device(c->mtd);
283 kfree(c);
286 static struct file_system_type jffs2_fs_type = {
287 .owner = THIS_MODULE,
288 .name = "jffs2",
289 .get_sb = jffs2_get_sb,
290 .kill_sb = jffs2_kill_sb,
291 .fs_flags = FS_REQUIRES_DEV,
294 static int __init init_jffs2_fs(void)
296 int ret;
298 printk(KERN_INFO "JFFS2 version 2.2."
299 #ifdef CONFIG_JFFS2_FS_NAND
300 " (NAND)"
301 #endif
302 " (C) 2001-2003 Red Hat, Inc.\n");
304 jffs2_inode_cachep = kmem_cache_create("jffs2_i",
305 sizeof(struct jffs2_inode_info),
306 0, SLAB_RECLAIM_ACCOUNT,
307 jffs2_i_init_once, NULL);
308 if (!jffs2_inode_cachep) {
309 printk(KERN_ERR "JFFS2 error: Failed to initialise inode cache\n");
310 return -ENOMEM;
312 ret = jffs2_compressors_init();
313 if (ret) {
314 printk(KERN_ERR "JFFS2 error: Failed to initialise compressors\n");
315 goto out;
317 ret = jffs2_create_slab_caches();
318 if (ret) {
319 printk(KERN_ERR "JFFS2 error: Failed to initialise slab caches\n");
320 goto out_compressors;
322 ret = register_filesystem(&jffs2_fs_type);
323 if (ret) {
324 printk(KERN_ERR "JFFS2 error: Failed to register filesystem\n");
325 goto out_slab;
327 return 0;
329 out_slab:
330 jffs2_destroy_slab_caches();
331 out_compressors:
332 jffs2_compressors_exit();
333 out:
334 kmem_cache_destroy(jffs2_inode_cachep);
335 return ret;
338 static void __exit exit_jffs2_fs(void)
340 unregister_filesystem(&jffs2_fs_type);
341 jffs2_destroy_slab_caches();
342 jffs2_compressors_exit();
343 kmem_cache_destroy(jffs2_inode_cachep);
346 module_init(init_jffs2_fs);
347 module_exit(exit_jffs2_fs);
349 MODULE_DESCRIPTION("The Journalling Flash File System, v2");
350 MODULE_AUTHOR("Red Hat, Inc.");
351 MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for
352 // the sake of this tag. It's Free Software.