[MTD] generalise the handling of MTD-specific superblocks
[deliverable/linux.git] / fs / jffs2 / super.c
1 /*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
4 * Copyright © 2001-2007 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 *
10 */
11
12 #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>
18 #include <linux/err.h>
19 #include <linux/mount.h>
20 #include <linux/jffs2.h>
21 #include <linux/pagemap.h>
22 #include <linux/mtd/super.h>
23 #include <linux/ctype.h>
24 #include <linux/namei.h>
25 #include "compr.h"
26 #include "nodelist.h"
27
28 static void jffs2_put_super(struct super_block *);
29
30 static struct kmem_cache *jffs2_inode_cachep;
31
32 static struct inode *jffs2_alloc_inode(struct super_block *sb)
33 {
34 struct jffs2_inode_info *ei;
35 ei = (struct jffs2_inode_info *)kmem_cache_alloc(jffs2_inode_cachep, GFP_KERNEL);
36 if (!ei)
37 return NULL;
38 return &ei->vfs_inode;
39 }
40
41 static void jffs2_destroy_inode(struct inode *inode)
42 {
43 kmem_cache_free(jffs2_inode_cachep, JFFS2_INODE_INFO(inode));
44 }
45
46 static void jffs2_i_init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
47 {
48 struct jffs2_inode_info *ei = (struct jffs2_inode_info *) foo;
49
50 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
51 SLAB_CTOR_CONSTRUCTOR) {
52 init_MUTEX(&ei->sem);
53 inode_init_once(&ei->vfs_inode);
54 }
55 }
56
57 static int jffs2_sync_fs(struct super_block *sb, int wait)
58 {
59 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
60
61 down(&c->alloc_sem);
62 jffs2_flush_wbuf_pad(c);
63 up(&c->alloc_sem);
64 return 0;
65 }
66
67 static const struct super_operations jffs2_super_operations =
68 {
69 .alloc_inode = jffs2_alloc_inode,
70 .destroy_inode =jffs2_destroy_inode,
71 .read_inode = jffs2_read_inode,
72 .put_super = jffs2_put_super,
73 .write_super = jffs2_write_super,
74 .statfs = jffs2_statfs,
75 .remount_fs = jffs2_remount_fs,
76 .clear_inode = jffs2_clear_inode,
77 .dirty_inode = jffs2_dirty_inode,
78 .sync_fs = jffs2_sync_fs,
79 };
80
81 /*
82 * fill in the superblock
83 */
84 static int jffs2_fill_super(struct super_block *sb, void *data, int silent)
85 {
86 struct jffs2_sb_info *c;
87
88 D1(printk(KERN_DEBUG "jffs2_get_sb_mtd():"
89 " New superblock for device %d (\"%s\")\n",
90 sb->s_mtd->index, sb->s_mtd->name));
91
92 c = kzalloc(sizeof(*c), GFP_KERNEL);
93 if (!c)
94 return -ENOMEM;
95
96 c->mtd = sb->s_mtd;
97 c->os_priv = sb;
98 sb->s_fs_info = c;
99
100 /* Initialize JFFS2 superblock locks, the further initialization will
101 * be done later */
102 init_MUTEX(&c->alloc_sem);
103 init_MUTEX(&c->erase_free_sem);
104 init_waitqueue_head(&c->erase_wait);
105 init_waitqueue_head(&c->inocache_wq);
106 spin_lock_init(&c->erase_completion_lock);
107 spin_lock_init(&c->inocache_lock);
108
109 sb->s_op = &jffs2_super_operations;
110 sb->s_flags = sb->s_flags | MS_NOATIME;
111 sb->s_xattr = jffs2_xattr_handlers;
112 #ifdef CONFIG_JFFS2_FS_POSIX_ACL
113 sb->s_flags |= MS_POSIXACL;
114 #endif
115 return jffs2_do_fill_super(sb, data, silent);
116 }
117
118 static int jffs2_get_sb(struct file_system_type *fs_type,
119 int flags, const char *dev_name,
120 void *data, struct vfsmount *mnt)
121 {
122 return get_sb_mtd(fs_type, flags, dev_name, data, jffs2_fill_super,
123 mnt);
124 }
125
126 static void jffs2_put_super (struct super_block *sb)
127 {
128 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
129
130 D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));
131
132 down(&c->alloc_sem);
133 jffs2_flush_wbuf_pad(c);
134 up(&c->alloc_sem);
135
136 jffs2_sum_exit(c);
137
138 jffs2_free_ino_caches(c);
139 jffs2_free_raw_node_refs(c);
140 if (jffs2_blocks_use_vmalloc(c))
141 vfree(c->blocks);
142 else
143 kfree(c->blocks);
144 jffs2_flash_cleanup(c);
145 kfree(c->inocache_list);
146 jffs2_clear_xattr_subsystem(c);
147 if (c->mtd->sync)
148 c->mtd->sync(c->mtd);
149
150 D1(printk(KERN_DEBUG "jffs2_put_super returning\n"));
151 }
152
153 static void jffs2_kill_sb(struct super_block *sb)
154 {
155 struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
156 if (!(sb->s_flags & MS_RDONLY))
157 jffs2_stop_garbage_collect_thread(c);
158 kill_mtd_super(sb);
159 kfree(c);
160 }
161
162 static struct file_system_type jffs2_fs_type = {
163 .owner = THIS_MODULE,
164 .name = "jffs2",
165 .get_sb = jffs2_get_sb,
166 .kill_sb = jffs2_kill_sb,
167 };
168
169 static int __init init_jffs2_fs(void)
170 {
171 int ret;
172
173 /* Paranoia checks for on-medium structures. If we ask GCC
174 to pack them with __attribute__((packed)) then it _also_
175 assumes that they're not aligned -- so it emits crappy
176 code on some architectures. Ideally we want an attribute
177 which means just 'no padding', without the alignment
178 thing. But GCC doesn't have that -- we have to just
179 hope the structs are the right sizes, instead. */
180 BUILD_BUG_ON(sizeof(struct jffs2_unknown_node) != 12);
181 BUILD_BUG_ON(sizeof(struct jffs2_raw_dirent) != 40);
182 BUILD_BUG_ON(sizeof(struct jffs2_raw_inode) != 68);
183 BUILD_BUG_ON(sizeof(struct jffs2_raw_summary) != 32);
184
185 printk(KERN_INFO "JFFS2 version 2.2."
186 #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
187 " (NAND)"
188 #endif
189 #ifdef CONFIG_JFFS2_SUMMARY
190 " (SUMMARY) "
191 #endif
192 " © 2001-2006 Red Hat, Inc.\n");
193
194 jffs2_inode_cachep = kmem_cache_create("jffs2_i",
195 sizeof(struct jffs2_inode_info),
196 0, (SLAB_RECLAIM_ACCOUNT|
197 SLAB_MEM_SPREAD),
198 jffs2_i_init_once, NULL);
199 if (!jffs2_inode_cachep) {
200 printk(KERN_ERR "JFFS2 error: Failed to initialise inode cache\n");
201 return -ENOMEM;
202 }
203 ret = jffs2_compressors_init();
204 if (ret) {
205 printk(KERN_ERR "JFFS2 error: Failed to initialise compressors\n");
206 goto out;
207 }
208 ret = jffs2_create_slab_caches();
209 if (ret) {
210 printk(KERN_ERR "JFFS2 error: Failed to initialise slab caches\n");
211 goto out_compressors;
212 }
213 ret = register_filesystem(&jffs2_fs_type);
214 if (ret) {
215 printk(KERN_ERR "JFFS2 error: Failed to register filesystem\n");
216 goto out_slab;
217 }
218 return 0;
219
220 out_slab:
221 jffs2_destroy_slab_caches();
222 out_compressors:
223 jffs2_compressors_exit();
224 out:
225 kmem_cache_destroy(jffs2_inode_cachep);
226 return ret;
227 }
228
229 static void __exit exit_jffs2_fs(void)
230 {
231 unregister_filesystem(&jffs2_fs_type);
232 jffs2_destroy_slab_caches();
233 jffs2_compressors_exit();
234 kmem_cache_destroy(jffs2_inode_cachep);
235 }
236
237 module_init(init_jffs2_fs);
238 module_exit(exit_jffs2_fs);
239
240 MODULE_DESCRIPTION("The Journalling Flash File System, v2");
241 MODULE_AUTHOR("Red Hat, Inc.");
242 MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for
243 // the sake of this tag. It's Free Software.
This page took 0.051435 seconds and 6 git commands to generate.