Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * JFFS2 -- Journalling Flash File System, Version 2. | |
3 | * | |
c00c310e | 4 | * Copyright © 2001-2007 Red Hat, Inc. |
1da177e4 LT |
5 | * |
6 | * Created by David Woodhouse <dwmw2@infradead.org> | |
7 | * | |
8 | * For licensing information, see the file 'LICENCE' in this directory. | |
9 | * | |
1da177e4 LT |
10 | */ |
11 | ||
1da177e4 LT |
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> | |
9c74034f | 18 | #include <linux/err.h> |
1da177e4 LT |
19 | #include <linux/mount.h> |
20 | #include <linux/jffs2.h> | |
21 | #include <linux/pagemap.h> | |
22 | #include <linux/mtd/mtd.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 | ||
e18b890b | 30 | static struct kmem_cache *jffs2_inode_cachep; |
1da177e4 LT |
31 | |
32 | static struct inode *jffs2_alloc_inode(struct super_block *sb) | |
33 | { | |
34 | struct jffs2_inode_info *ei; | |
e94b1766 | 35 | ei = (struct jffs2_inode_info *)kmem_cache_alloc(jffs2_inode_cachep, GFP_KERNEL); |
1da177e4 LT |
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 | ||
e18b890b | 46 | static void jffs2_i_init_once(void * foo, struct kmem_cache * cachep, unsigned long flags) |
1da177e4 LT |
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) { | |
21eeb7aa | 52 | init_MUTEX(&ei->sem); |
1da177e4 LT |
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); | |
182ec4ee | 63 | up(&c->alloc_sem); |
1da177e4 LT |
64 | return 0; |
65 | } | |
66 | ||
ee9b6d61 | 67 | static const struct super_operations jffs2_super_operations = |
1da177e4 LT |
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 | static int jffs2_sb_compare(struct super_block *sb, void *data) | |
82 | { | |
83 | struct jffs2_sb_info *p = data; | |
84 | struct jffs2_sb_info *c = JFFS2_SB_INFO(sb); | |
85 | ||
86 | /* The superblocks are considered to be equivalent if the underlying MTD | |
87 | device is the same one */ | |
88 | if (c->mtd == p->mtd) { | |
89 | D1(printk(KERN_DEBUG "jffs2_sb_compare: match on device %d (\"%s\")\n", p->mtd->index, p->mtd->name)); | |
90 | return 1; | |
91 | } else { | |
92 | D1(printk(KERN_DEBUG "jffs2_sb_compare: No match, device %d (\"%s\"), device %d (\"%s\")\n", | |
93 | c->mtd->index, c->mtd->name, p->mtd->index, p->mtd->name)); | |
94 | return 0; | |
95 | } | |
96 | } | |
97 | ||
98 | static int jffs2_sb_set(struct super_block *sb, void *data) | |
99 | { | |
100 | struct jffs2_sb_info *p = data; | |
101 | ||
102 | /* For persistence of NFS exports etc. we use the same s_dev | |
103 | each time we mount the device, don't just use an anonymous | |
104 | device */ | |
105 | sb->s_fs_info = p; | |
106 | p->os_priv = sb; | |
107 | sb->s_dev = MKDEV(MTD_BLOCK_MAJOR, p->mtd->index); | |
108 | ||
109 | return 0; | |
110 | } | |
111 | ||
454e2398 DH |
112 | static int jffs2_get_sb_mtd(struct file_system_type *fs_type, |
113 | int flags, const char *dev_name, | |
114 | void *data, struct mtd_info *mtd, | |
115 | struct vfsmount *mnt) | |
1da177e4 LT |
116 | { |
117 | struct super_block *sb; | |
118 | struct jffs2_sb_info *c; | |
119 | int ret; | |
120 | ||
f8314dc6 | 121 | c = kzalloc(sizeof(*c), GFP_KERNEL); |
1da177e4 | 122 | if (!c) |
454e2398 | 123 | return -ENOMEM; |
1da177e4 LT |
124 | c->mtd = mtd; |
125 | ||
126 | sb = sget(fs_type, jffs2_sb_compare, jffs2_sb_set, c); | |
127 | ||
128 | if (IS_ERR(sb)) | |
454e2398 | 129 | goto out_error; |
1da177e4 LT |
130 | |
131 | if (sb->s_root) { | |
132 | /* New mountpoint for JFFS2 which is already mounted */ | |
133 | D1(printk(KERN_DEBUG "jffs2_get_sb_mtd(): Device %d (\"%s\") is already mounted\n", | |
134 | mtd->index, mtd->name)); | |
454e2398 | 135 | ret = simple_set_mnt(mnt, sb); |
1da177e4 LT |
136 | goto out_put; |
137 | } | |
138 | ||
139 | D1(printk(KERN_DEBUG "jffs2_get_sb_mtd(): New superblock for device %d (\"%s\")\n", | |
140 | mtd->index, mtd->name)); | |
141 | ||
b6220598 AB |
142 | /* Initialize JFFS2 superblock locks, the further initialization will be |
143 | * done later */ | |
144 | init_MUTEX(&c->alloc_sem); | |
145 | init_MUTEX(&c->erase_free_sem); | |
146 | init_waitqueue_head(&c->erase_wait); | |
147 | init_waitqueue_head(&c->inocache_wq); | |
148 | spin_lock_init(&c->erase_completion_lock); | |
149 | spin_lock_init(&c->inocache_lock); | |
150 | ||
1da177e4 LT |
151 | sb->s_op = &jffs2_super_operations; |
152 | sb->s_flags = flags | MS_NOATIME; | |
aa98d7cf KK |
153 | sb->s_xattr = jffs2_xattr_handlers; |
154 | #ifdef CONFIG_JFFS2_FS_POSIX_ACL | |
155 | sb->s_flags |= MS_POSIXACL; | |
156 | #endif | |
9b04c997 | 157 | ret = jffs2_do_fill_super(sb, data, flags & MS_SILENT ? 1 : 0); |
1da177e4 LT |
158 | |
159 | if (ret) { | |
160 | /* Failure case... */ | |
161 | up_write(&sb->s_umount); | |
162 | deactivate_super(sb); | |
454e2398 | 163 | return ret; |
1da177e4 LT |
164 | } |
165 | ||
166 | sb->s_flags |= MS_ACTIVE; | |
454e2398 | 167 | return simple_set_mnt(mnt, sb); |
1da177e4 | 168 | |
454e2398 DH |
169 | out_error: |
170 | ret = PTR_ERR(sb); | |
1da177e4 LT |
171 | out_put: |
172 | kfree(c); | |
173 | put_mtd_device(mtd); | |
174 | ||
454e2398 | 175 | return ret; |
1da177e4 LT |
176 | } |
177 | ||
454e2398 DH |
178 | static int jffs2_get_sb_mtdnr(struct file_system_type *fs_type, |
179 | int flags, const char *dev_name, | |
180 | void *data, int mtdnr, | |
181 | struct vfsmount *mnt) | |
1da177e4 LT |
182 | { |
183 | struct mtd_info *mtd; | |
184 | ||
185 | mtd = get_mtd_device(NULL, mtdnr); | |
9c74034f | 186 | if (IS_ERR(mtd)) { |
1da177e4 | 187 | D1(printk(KERN_DEBUG "jffs2: MTD device #%u doesn't appear to exist\n", mtdnr)); |
9c74034f | 188 | return PTR_ERR(mtd); |
1da177e4 LT |
189 | } |
190 | ||
454e2398 | 191 | return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd, mnt); |
1da177e4 LT |
192 | } |
193 | ||
454e2398 DH |
194 | static int jffs2_get_sb(struct file_system_type *fs_type, |
195 | int flags, const char *dev_name, | |
196 | void *data, struct vfsmount *mnt) | |
1da177e4 LT |
197 | { |
198 | int err; | |
199 | struct nameidata nd; | |
200 | int mtdnr; | |
201 | ||
202 | if (!dev_name) | |
454e2398 | 203 | return -EINVAL; |
1da177e4 LT |
204 | |
205 | D1(printk(KERN_DEBUG "jffs2_get_sb(): dev_name \"%s\"\n", dev_name)); | |
206 | ||
207 | /* The preferred way of mounting in future; especially when | |
208 | CONFIG_BLK_DEV is implemented - we specify the underlying | |
182ec4ee | 209 | MTD device by number or by name, so that we don't require |
1da177e4 LT |
210 | block device support to be present in the kernel. */ |
211 | ||
212 | /* FIXME: How to do the root fs this way? */ | |
213 | ||
214 | if (dev_name[0] == 'm' && dev_name[1] == 't' && dev_name[2] == 'd') { | |
215 | /* Probably mounting without the blkdev crap */ | |
216 | if (dev_name[3] == ':') { | |
217 | struct mtd_info *mtd; | |
218 | ||
219 | /* Mount by MTD device name */ | |
220 | D1(printk(KERN_DEBUG "jffs2_get_sb(): mtd:%%s, name \"%s\"\n", dev_name+4)); | |
221 | for (mtdnr = 0; mtdnr < MAX_MTD_DEVICES; mtdnr++) { | |
222 | mtd = get_mtd_device(NULL, mtdnr); | |
9c74034f | 223 | if (!IS_ERR(mtd)) { |
1da177e4 | 224 | if (!strcmp(mtd->name, dev_name+4)) |
454e2398 | 225 | return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd, mnt); |
1da177e4 LT |
226 | put_mtd_device(mtd); |
227 | } | |
228 | } | |
229 | printk(KERN_NOTICE "jffs2_get_sb(): MTD device with name \"%s\" not found.\n", dev_name+4); | |
230 | } else if (isdigit(dev_name[3])) { | |
231 | /* Mount by MTD device number name */ | |
232 | char *endptr; | |
182ec4ee | 233 | |
1da177e4 LT |
234 | mtdnr = simple_strtoul(dev_name+3, &endptr, 0); |
235 | if (!*endptr) { | |
236 | /* It was a valid number */ | |
237 | D1(printk(KERN_DEBUG "jffs2_get_sb(): mtd%%d, mtdnr %d\n", mtdnr)); | |
454e2398 | 238 | return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr, mnt); |
1da177e4 LT |
239 | } |
240 | } | |
241 | } | |
242 | ||
182ec4ee | 243 | /* Try the old way - the hack where we allowed users to mount |
1da177e4 LT |
244 | /dev/mtdblock$(n) but didn't actually _use_ the blkdev */ |
245 | ||
246 | err = path_lookup(dev_name, LOOKUP_FOLLOW, &nd); | |
247 | ||
248 | D1(printk(KERN_DEBUG "jffs2_get_sb(): path_lookup() returned %d, inode %p\n", | |
249 | err, nd.dentry->d_inode)); | |
250 | ||
251 | if (err) | |
454e2398 | 252 | return err; |
1da177e4 LT |
253 | |
254 | err = -EINVAL; | |
255 | ||
256 | if (!S_ISBLK(nd.dentry->d_inode->i_mode)) | |
257 | goto out; | |
258 | ||
259 | if (nd.mnt->mnt_flags & MNT_NODEV) { | |
260 | err = -EACCES; | |
261 | goto out; | |
262 | } | |
263 | ||
264 | if (imajor(nd.dentry->d_inode) != MTD_BLOCK_MAJOR) { | |
9b04c997 | 265 | if (!(flags & MS_SILENT)) |
1da177e4 LT |
266 | printk(KERN_NOTICE "Attempt to mount non-MTD device \"%s\" as JFFS2\n", |
267 | dev_name); | |
268 | goto out; | |
269 | } | |
270 | ||
271 | mtdnr = iminor(nd.dentry->d_inode); | |
272 | path_release(&nd); | |
273 | ||
454e2398 | 274 | return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr, mnt); |
1da177e4 LT |
275 | |
276 | out: | |
277 | path_release(&nd); | |
454e2398 | 278 | return err; |
1da177e4 LT |
279 | } |
280 | ||
281 | static void jffs2_put_super (struct super_block *sb) | |
282 | { | |
283 | struct jffs2_sb_info *c = JFFS2_SB_INFO(sb); | |
284 | ||
285 | D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n")); | |
286 | ||
1da177e4 LT |
287 | down(&c->alloc_sem); |
288 | jffs2_flush_wbuf_pad(c); | |
289 | up(&c->alloc_sem); | |
e631ddba FH |
290 | |
291 | jffs2_sum_exit(c); | |
292 | ||
1da177e4 LT |
293 | jffs2_free_ino_caches(c); |
294 | jffs2_free_raw_node_refs(c); | |
4ce1f562 | 295 | if (jffs2_blocks_use_vmalloc(c)) |
1da177e4 LT |
296 | vfree(c->blocks); |
297 | else | |
298 | kfree(c->blocks); | |
299 | jffs2_flash_cleanup(c); | |
300 | kfree(c->inocache_list); | |
aa98d7cf | 301 | jffs2_clear_xattr_subsystem(c); |
1da177e4 LT |
302 | if (c->mtd->sync) |
303 | c->mtd->sync(c->mtd); | |
304 | ||
305 | D1(printk(KERN_DEBUG "jffs2_put_super returning\n")); | |
306 | } | |
307 | ||
308 | static void jffs2_kill_sb(struct super_block *sb) | |
309 | { | |
310 | struct jffs2_sb_info *c = JFFS2_SB_INFO(sb); | |
a69dde91 AB |
311 | if (!(sb->s_flags & MS_RDONLY)) |
312 | jffs2_stop_garbage_collect_thread(c); | |
1da177e4 LT |
313 | generic_shutdown_super(sb); |
314 | put_mtd_device(c->mtd); | |
315 | kfree(c); | |
316 | } | |
317 | ||
318 | static struct file_system_type jffs2_fs_type = { | |
319 | .owner = THIS_MODULE, | |
320 | .name = "jffs2", | |
321 | .get_sb = jffs2_get_sb, | |
322 | .kill_sb = jffs2_kill_sb, | |
323 | }; | |
324 | ||
325 | static int __init init_jffs2_fs(void) | |
326 | { | |
327 | int ret; | |
328 | ||
3e68fbb5 DW |
329 | /* Paranoia checks for on-medium structures. If we ask GCC |
330 | to pack them with __attribute__((packed)) then it _also_ | |
331 | assumes that they're not aligned -- so it emits crappy | |
332 | code on some architectures. Ideally we want an attribute | |
333 | which means just 'no padding', without the alignment | |
334 | thing. But GCC doesn't have that -- we have to just | |
335 | hope the structs are the right sizes, instead. */ | |
2ecd05ae AD |
336 | BUILD_BUG_ON(sizeof(struct jffs2_unknown_node) != 12); |
337 | BUILD_BUG_ON(sizeof(struct jffs2_raw_dirent) != 40); | |
338 | BUILD_BUG_ON(sizeof(struct jffs2_raw_inode) != 68); | |
339 | BUILD_BUG_ON(sizeof(struct jffs2_raw_summary) != 32); | |
3e68fbb5 | 340 | |
1da177e4 | 341 | printk(KERN_INFO "JFFS2 version 2.2." |
2f82ce1e | 342 | #ifdef CONFIG_JFFS2_FS_WRITEBUFFER |
1da177e4 | 343 | " (NAND)" |
e631ddba FH |
344 | #endif |
345 | #ifdef CONFIG_JFFS2_SUMMARY | |
346 | " (SUMMARY) " | |
1da177e4 | 347 | #endif |
c00c310e | 348 | " © 2001-2006 Red Hat, Inc.\n"); |
1da177e4 LT |
349 | |
350 | jffs2_inode_cachep = kmem_cache_create("jffs2_i", | |
351 | sizeof(struct jffs2_inode_info), | |
fffb60f9 PJ |
352 | 0, (SLAB_RECLAIM_ACCOUNT| |
353 | SLAB_MEM_SPREAD), | |
1da177e4 LT |
354 | jffs2_i_init_once, NULL); |
355 | if (!jffs2_inode_cachep) { | |
356 | printk(KERN_ERR "JFFS2 error: Failed to initialise inode cache\n"); | |
357 | return -ENOMEM; | |
358 | } | |
359 | ret = jffs2_compressors_init(); | |
360 | if (ret) { | |
361 | printk(KERN_ERR "JFFS2 error: Failed to initialise compressors\n"); | |
362 | goto out; | |
363 | } | |
364 | ret = jffs2_create_slab_caches(); | |
365 | if (ret) { | |
366 | printk(KERN_ERR "JFFS2 error: Failed to initialise slab caches\n"); | |
367 | goto out_compressors; | |
368 | } | |
369 | ret = register_filesystem(&jffs2_fs_type); | |
370 | if (ret) { | |
371 | printk(KERN_ERR "JFFS2 error: Failed to register filesystem\n"); | |
372 | goto out_slab; | |
373 | } | |
374 | return 0; | |
375 | ||
376 | out_slab: | |
377 | jffs2_destroy_slab_caches(); | |
378 | out_compressors: | |
379 | jffs2_compressors_exit(); | |
380 | out: | |
381 | kmem_cache_destroy(jffs2_inode_cachep); | |
382 | return ret; | |
383 | } | |
384 | ||
385 | static void __exit exit_jffs2_fs(void) | |
386 | { | |
387 | unregister_filesystem(&jffs2_fs_type); | |
388 | jffs2_destroy_slab_caches(); | |
389 | jffs2_compressors_exit(); | |
390 | kmem_cache_destroy(jffs2_inode_cachep); | |
391 | } | |
392 | ||
393 | module_init(init_jffs2_fs); | |
394 | module_exit(exit_jffs2_fs); | |
395 | ||
396 | MODULE_DESCRIPTION("The Journalling Flash File System, v2"); | |
397 | MODULE_AUTHOR("Red Hat, Inc."); | |
182ec4ee | 398 | MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for |
1da177e4 | 399 | // the sake of this tag. It's Free Software. |