Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | * | |
182ec4ee | 10 | * $Id: super.c,v 1.110 2005/11/07 11:14:42 gleixner Exp $ |
1da177e4 LT |
11 | * |
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 | ||
30 | static void jffs2_put_super(struct super_block *); | |
31 | ||
32 | static kmem_cache_t *jffs2_inode_cachep; | |
33 | ||
34 | static 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 | ||
43 | static void jffs2_destroy_inode(struct inode *inode) | |
44 | { | |
45 | kmem_cache_free(jffs2_inode_cachep, JFFS2_INODE_INFO(inode)); | |
46 | } | |
47 | ||
48 | static 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) { | |
21eeb7aa | 54 | init_MUTEX(&ei->sem); |
1da177e4 LT |
55 | inode_init_once(&ei->vfs_inode); |
56 | } | |
57 | } | |
58 | ||
59 | static 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); | |
182ec4ee | 65 | up(&c->alloc_sem); |
1da177e4 LT |
66 | return 0; |
67 | } | |
68 | ||
69 | static 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 | ||
83 | static 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 | ||
100 | static 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 | ||
114 | static struct super_block *jffs2_get_sb_mtd(struct file_system_type *fs_type, | |
182ec4ee | 115 | int flags, const char *dev_name, |
1da177e4 LT |
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 | ||
b6220598 AB |
143 | /* 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 | ||
1da177e4 LT |
152 | sb->s_op = &jffs2_super_operations; |
153 | sb->s_flags = flags | MS_NOATIME; | |
aa98d7cf KK |
154 | sb->s_xattr = jffs2_xattr_handlers; |
155 | #ifdef CONFIG_JFFS2_FS_POSIX_ACL | |
156 | sb->s_flags |= MS_POSIXACL; | |
157 | #endif | |
9b04c997 | 158 | ret = jffs2_do_fill_super(sb, data, flags & MS_SILENT ? 1 : 0); |
1da177e4 LT |
159 | |
160 | if (ret) { | |
161 | /* Failure case... */ | |
162 | up_write(&sb->s_umount); | |
163 | deactivate_super(sb); | |
164 | return ERR_PTR(ret); | |
165 | } | |
166 | ||
167 | sb->s_flags |= MS_ACTIVE; | |
168 | return sb; | |
169 | ||
170 | out_put: | |
171 | kfree(c); | |
172 | put_mtd_device(mtd); | |
173 | ||
174 | return sb; | |
175 | } | |
176 | ||
177 | static struct super_block *jffs2_get_sb_mtdnr(struct file_system_type *fs_type, | |
182ec4ee | 178 | int flags, const char *dev_name, |
1da177e4 LT |
179 | void *data, int mtdnr) |
180 | { | |
181 | struct mtd_info *mtd; | |
182 | ||
183 | mtd = get_mtd_device(NULL, mtdnr); | |
184 | if (!mtd) { | |
185 | D1(printk(KERN_DEBUG "jffs2: MTD device #%u doesn't appear to exist\n", mtdnr)); | |
186 | return ERR_PTR(-EINVAL); | |
187 | } | |
188 | ||
189 | return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd); | |
190 | } | |
191 | ||
192 | static struct super_block *jffs2_get_sb(struct file_system_type *fs_type, | |
193 | int flags, const char *dev_name, | |
194 | void *data) | |
195 | { | |
196 | int err; | |
197 | struct nameidata nd; | |
198 | int mtdnr; | |
199 | ||
200 | if (!dev_name) | |
201 | return ERR_PTR(-EINVAL); | |
202 | ||
203 | D1(printk(KERN_DEBUG "jffs2_get_sb(): dev_name \"%s\"\n", dev_name)); | |
204 | ||
205 | /* The preferred way of mounting in future; especially when | |
206 | CONFIG_BLK_DEV is implemented - we specify the underlying | |
182ec4ee | 207 | MTD device by number or by name, so that we don't require |
1da177e4 LT |
208 | block device support to be present in the kernel. */ |
209 | ||
210 | /* FIXME: How to do the root fs this way? */ | |
211 | ||
212 | if (dev_name[0] == 'm' && dev_name[1] == 't' && dev_name[2] == 'd') { | |
213 | /* Probably mounting without the blkdev crap */ | |
214 | if (dev_name[3] == ':') { | |
215 | struct mtd_info *mtd; | |
216 | ||
217 | /* Mount by MTD device name */ | |
218 | D1(printk(KERN_DEBUG "jffs2_get_sb(): mtd:%%s, name \"%s\"\n", dev_name+4)); | |
219 | for (mtdnr = 0; mtdnr < MAX_MTD_DEVICES; mtdnr++) { | |
220 | mtd = get_mtd_device(NULL, mtdnr); | |
221 | if (mtd) { | |
222 | if (!strcmp(mtd->name, dev_name+4)) | |
223 | return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd); | |
224 | put_mtd_device(mtd); | |
225 | } | |
226 | } | |
227 | printk(KERN_NOTICE "jffs2_get_sb(): MTD device with name \"%s\" not found.\n", dev_name+4); | |
228 | } else if (isdigit(dev_name[3])) { | |
229 | /* Mount by MTD device number name */ | |
230 | char *endptr; | |
182ec4ee | 231 | |
1da177e4 LT |
232 | mtdnr = simple_strtoul(dev_name+3, &endptr, 0); |
233 | if (!*endptr) { | |
234 | /* It was a valid number */ | |
235 | D1(printk(KERN_DEBUG "jffs2_get_sb(): mtd%%d, mtdnr %d\n", mtdnr)); | |
236 | return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr); | |
237 | } | |
238 | } | |
239 | } | |
240 | ||
182ec4ee | 241 | /* Try the old way - the hack where we allowed users to mount |
1da177e4 LT |
242 | /dev/mtdblock$(n) but didn't actually _use_ the blkdev */ |
243 | ||
244 | err = path_lookup(dev_name, LOOKUP_FOLLOW, &nd); | |
245 | ||
246 | D1(printk(KERN_DEBUG "jffs2_get_sb(): path_lookup() returned %d, inode %p\n", | |
247 | err, nd.dentry->d_inode)); | |
248 | ||
249 | if (err) | |
250 | return ERR_PTR(err); | |
251 | ||
252 | err = -EINVAL; | |
253 | ||
254 | if (!S_ISBLK(nd.dentry->d_inode->i_mode)) | |
255 | goto out; | |
256 | ||
257 | if (nd.mnt->mnt_flags & MNT_NODEV) { | |
258 | err = -EACCES; | |
259 | goto out; | |
260 | } | |
261 | ||
262 | if (imajor(nd.dentry->d_inode) != MTD_BLOCK_MAJOR) { | |
9b04c997 | 263 | if (!(flags & MS_SILENT)) |
1da177e4 LT |
264 | printk(KERN_NOTICE "Attempt to mount non-MTD device \"%s\" as JFFS2\n", |
265 | dev_name); | |
266 | goto out; | |
267 | } | |
268 | ||
269 | mtdnr = iminor(nd.dentry->d_inode); | |
270 | path_release(&nd); | |
271 | ||
272 | return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr); | |
273 | ||
274 | out: | |
275 | path_release(&nd); | |
276 | return ERR_PTR(err); | |
277 | } | |
278 | ||
279 | static void jffs2_put_super (struct super_block *sb) | |
280 | { | |
281 | struct jffs2_sb_info *c = JFFS2_SB_INFO(sb); | |
282 | ||
283 | D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n")); | |
284 | ||
1da177e4 LT |
285 | down(&c->alloc_sem); |
286 | jffs2_flush_wbuf_pad(c); | |
287 | up(&c->alloc_sem); | |
e631ddba FH |
288 | |
289 | jffs2_sum_exit(c); | |
290 | ||
1da177e4 LT |
291 | jffs2_free_ino_caches(c); |
292 | jffs2_free_raw_node_refs(c); | |
4ce1f562 | 293 | if (jffs2_blocks_use_vmalloc(c)) |
1da177e4 LT |
294 | vfree(c->blocks); |
295 | else | |
296 | kfree(c->blocks); | |
297 | jffs2_flash_cleanup(c); | |
298 | kfree(c->inocache_list); | |
aa98d7cf | 299 | jffs2_clear_xattr_subsystem(c); |
1da177e4 LT |
300 | if (c->mtd->sync) |
301 | c->mtd->sync(c->mtd); | |
302 | ||
303 | D1(printk(KERN_DEBUG "jffs2_put_super returning\n")); | |
304 | } | |
305 | ||
306 | static void jffs2_kill_sb(struct super_block *sb) | |
307 | { | |
308 | struct jffs2_sb_info *c = JFFS2_SB_INFO(sb); | |
a69dde91 AB |
309 | if (!(sb->s_flags & MS_RDONLY)) |
310 | jffs2_stop_garbage_collect_thread(c); | |
1da177e4 LT |
311 | generic_shutdown_super(sb); |
312 | put_mtd_device(c->mtd); | |
313 | kfree(c); | |
314 | } | |
315 | ||
316 | static struct file_system_type jffs2_fs_type = { | |
317 | .owner = THIS_MODULE, | |
318 | .name = "jffs2", | |
319 | .get_sb = jffs2_get_sb, | |
320 | .kill_sb = jffs2_kill_sb, | |
321 | }; | |
322 | ||
323 | static int __init init_jffs2_fs(void) | |
324 | { | |
325 | int ret; | |
326 | ||
327 | printk(KERN_INFO "JFFS2 version 2.2." | |
2f82ce1e | 328 | #ifdef CONFIG_JFFS2_FS_WRITEBUFFER |
1da177e4 | 329 | " (NAND)" |
e631ddba FH |
330 | #endif |
331 | #ifdef CONFIG_JFFS2_SUMMARY | |
332 | " (SUMMARY) " | |
1da177e4 LT |
333 | #endif |
334 | " (C) 2001-2003 Red Hat, Inc.\n"); | |
335 | ||
336 | jffs2_inode_cachep = kmem_cache_create("jffs2_i", | |
337 | sizeof(struct jffs2_inode_info), | |
fffb60f9 PJ |
338 | 0, (SLAB_RECLAIM_ACCOUNT| |
339 | SLAB_MEM_SPREAD), | |
1da177e4 LT |
340 | jffs2_i_init_once, NULL); |
341 | if (!jffs2_inode_cachep) { | |
342 | printk(KERN_ERR "JFFS2 error: Failed to initialise inode cache\n"); | |
343 | return -ENOMEM; | |
344 | } | |
345 | ret = jffs2_compressors_init(); | |
346 | if (ret) { | |
347 | printk(KERN_ERR "JFFS2 error: Failed to initialise compressors\n"); | |
348 | goto out; | |
349 | } | |
350 | ret = jffs2_create_slab_caches(); | |
351 | if (ret) { | |
352 | printk(KERN_ERR "JFFS2 error: Failed to initialise slab caches\n"); | |
353 | goto out_compressors; | |
354 | } | |
355 | ret = register_filesystem(&jffs2_fs_type); | |
356 | if (ret) { | |
357 | printk(KERN_ERR "JFFS2 error: Failed to register filesystem\n"); | |
358 | goto out_slab; | |
359 | } | |
360 | return 0; | |
361 | ||
362 | out_slab: | |
363 | jffs2_destroy_slab_caches(); | |
364 | out_compressors: | |
365 | jffs2_compressors_exit(); | |
366 | out: | |
367 | kmem_cache_destroy(jffs2_inode_cachep); | |
368 | return ret; | |
369 | } | |
370 | ||
371 | static void __exit exit_jffs2_fs(void) | |
372 | { | |
373 | unregister_filesystem(&jffs2_fs_type); | |
374 | jffs2_destroy_slab_caches(); | |
375 | jffs2_compressors_exit(); | |
376 | kmem_cache_destroy(jffs2_inode_cachep); | |
377 | } | |
378 | ||
379 | module_init(init_jffs2_fs); | |
380 | module_exit(exit_jffs2_fs); | |
381 | ||
382 | MODULE_DESCRIPTION("The Journalling Flash File System, v2"); | |
383 | MODULE_AUTHOR("Red Hat, Inc."); | |
182ec4ee | 384 | MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for |
1da177e4 | 385 | // the sake of this tag. It's Free Software. |