super.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/init.h>
  15. #include <linux/list.h>
  16. #include <linux/fs.h>
  17. #include <linux/err.h>
  18. #include <linux/mount.h>
  19. #include <linux/jffs2.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/mtd/mtd.h>
  22. #include <linux/ctype.h>
  23. #include <linux/namei.h>
  24. #include "compr.h"
  25. #include "nodelist.h"
  26. static void jffs2_put_super(struct super_block *);
  27. static struct kmem_cache *jffs2_inode_cachep;
  28. static struct inode *jffs2_alloc_inode(struct super_block *sb)
  29. {
  30. struct jffs2_inode_info *ei;
  31. ei = (struct jffs2_inode_info *)kmem_cache_alloc(jffs2_inode_cachep, GFP_KERNEL);
  32. if (!ei)
  33. return NULL;
  34. return &ei->vfs_inode;
  35. }
  36. static void jffs2_destroy_inode(struct inode *inode)
  37. {
  38. kmem_cache_free(jffs2_inode_cachep, JFFS2_INODE_INFO(inode));
  39. }
  40. static void jffs2_i_init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
  41. {
  42. struct jffs2_inode_info *ei = (struct jffs2_inode_info *) foo;
  43. init_MUTEX(&ei->sem);
  44. inode_init_once(&ei->vfs_inode);
  45. }
  46. static int jffs2_sync_fs(struct super_block *sb, int wait)
  47. {
  48. struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
  49. down(&c->alloc_sem);
  50. jffs2_flush_wbuf_pad(c);
  51. up(&c->alloc_sem);
  52. return 0;
  53. }
  54. static const struct super_operations jffs2_super_operations =
  55. {
  56. .alloc_inode = jffs2_alloc_inode,
  57. .destroy_inode =jffs2_destroy_inode,
  58. .read_inode = jffs2_read_inode,
  59. .put_super = jffs2_put_super,
  60. .write_super = jffs2_write_super,
  61. .statfs = jffs2_statfs,
  62. .remount_fs = jffs2_remount_fs,
  63. .clear_inode = jffs2_clear_inode,
  64. .dirty_inode = jffs2_dirty_inode,
  65. .sync_fs = jffs2_sync_fs,
  66. };
  67. static int jffs2_sb_compare(struct super_block *sb, void *data)
  68. {
  69. struct jffs2_sb_info *p = data;
  70. struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
  71. /* The superblocks are considered to be equivalent if the underlying MTD
  72. device is the same one */
  73. if (c->mtd == p->mtd) {
  74. D1(printk(KERN_DEBUG "jffs2_sb_compare: match on device %d (\"%s\")\n", p->mtd->index, p->mtd->name));
  75. return 1;
  76. } else {
  77. D1(printk(KERN_DEBUG "jffs2_sb_compare: No match, device %d (\"%s\"), device %d (\"%s\")\n",
  78. c->mtd->index, c->mtd->name, p->mtd->index, p->mtd->name));
  79. return 0;
  80. }
  81. }
  82. static int jffs2_sb_set(struct super_block *sb, void *data)
  83. {
  84. struct jffs2_sb_info *p = data;
  85. /* For persistence of NFS exports etc. we use the same s_dev
  86. each time we mount the device, don't just use an anonymous
  87. device */
  88. sb->s_fs_info = p;
  89. p->os_priv = sb;
  90. sb->s_dev = MKDEV(MTD_BLOCK_MAJOR, p->mtd->index);
  91. return 0;
  92. }
  93. static int jffs2_get_sb_mtd(struct file_system_type *fs_type,
  94. int flags, const char *dev_name,
  95. void *data, struct mtd_info *mtd,
  96. struct vfsmount *mnt)
  97. {
  98. struct super_block *sb;
  99. struct jffs2_sb_info *c;
  100. int ret;
  101. c = kzalloc(sizeof(*c), GFP_KERNEL);
  102. if (!c)
  103. return -ENOMEM;
  104. c->mtd = mtd;
  105. sb = sget(fs_type, jffs2_sb_compare, jffs2_sb_set, c);
  106. if (IS_ERR(sb))
  107. goto out_error;
  108. if (sb->s_root) {
  109. /* New mountpoint for JFFS2 which is already mounted */
  110. D1(printk(KERN_DEBUG "jffs2_get_sb_mtd(): Device %d (\"%s\") is already mounted\n",
  111. mtd->index, mtd->name));
  112. ret = simple_set_mnt(mnt, sb);
  113. goto out_put;
  114. }
  115. D1(printk(KERN_DEBUG "jffs2_get_sb_mtd(): New superblock for device %d (\"%s\")\n",
  116. mtd->index, mtd->name));
  117. /* Initialize JFFS2 superblock locks, the further initialization will be
  118. * done later */
  119. init_MUTEX(&c->alloc_sem);
  120. init_MUTEX(&c->erase_free_sem);
  121. init_waitqueue_head(&c->erase_wait);
  122. init_waitqueue_head(&c->inocache_wq);
  123. spin_lock_init(&c->erase_completion_lock);
  124. spin_lock_init(&c->inocache_lock);
  125. sb->s_op = &jffs2_super_operations;
  126. sb->s_flags = flags | MS_NOATIME;
  127. sb->s_xattr = jffs2_xattr_handlers;
  128. #ifdef CONFIG_JFFS2_FS_POSIX_ACL
  129. sb->s_flags |= MS_POSIXACL;
  130. #endif
  131. ret = jffs2_do_fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
  132. if (ret) {
  133. /* Failure case... */
  134. up_write(&sb->s_umount);
  135. deactivate_super(sb);
  136. return ret;
  137. }
  138. sb->s_flags |= MS_ACTIVE;
  139. return simple_set_mnt(mnt, sb);
  140. out_error:
  141. ret = PTR_ERR(sb);
  142. out_put:
  143. kfree(c);
  144. put_mtd_device(mtd);
  145. return ret;
  146. }
  147. static int jffs2_get_sb_mtdnr(struct file_system_type *fs_type,
  148. int flags, const char *dev_name,
  149. void *data, int mtdnr,
  150. struct vfsmount *mnt)
  151. {
  152. struct mtd_info *mtd;
  153. mtd = get_mtd_device(NULL, mtdnr);
  154. if (IS_ERR(mtd)) {
  155. D1(printk(KERN_DEBUG "jffs2: MTD device #%u doesn't appear to exist\n", mtdnr));
  156. return PTR_ERR(mtd);
  157. }
  158. return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd, mnt);
  159. }
  160. static int jffs2_get_sb(struct file_system_type *fs_type,
  161. int flags, const char *dev_name,
  162. void *data, struct vfsmount *mnt)
  163. {
  164. int err;
  165. struct nameidata nd;
  166. int mtdnr;
  167. if (!dev_name)
  168. return -EINVAL;
  169. D1(printk(KERN_DEBUG "jffs2_get_sb(): dev_name \"%s\"\n", dev_name));
  170. /* The preferred way of mounting in future; especially when
  171. CONFIG_BLK_DEV is implemented - we specify the underlying
  172. MTD device by number or by name, so that we don't require
  173. block device support to be present in the kernel. */
  174. /* FIXME: How to do the root fs this way? */
  175. if (dev_name[0] == 'm' && dev_name[1] == 't' && dev_name[2] == 'd') {
  176. /* Probably mounting without the blkdev crap */
  177. if (dev_name[3] == ':') {
  178. struct mtd_info *mtd;
  179. /* Mount by MTD device name */
  180. D1(printk(KERN_DEBUG "jffs2_get_sb(): mtd:%%s, name \"%s\"\n", dev_name+4));
  181. for (mtdnr = 0; mtdnr < MAX_MTD_DEVICES; mtdnr++) {
  182. mtd = get_mtd_device(NULL, mtdnr);
  183. if (!IS_ERR(mtd)) {
  184. if (!strcmp(mtd->name, dev_name+4))
  185. return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd, mnt);
  186. put_mtd_device(mtd);
  187. }
  188. }
  189. printk(KERN_NOTICE "jffs2_get_sb(): MTD device with name \"%s\" not found.\n", dev_name+4);
  190. } else if (isdigit(dev_name[3])) {
  191. /* Mount by MTD device number name */
  192. char *endptr;
  193. mtdnr = simple_strtoul(dev_name+3, &endptr, 0);
  194. if (!*endptr) {
  195. /* It was a valid number */
  196. D1(printk(KERN_DEBUG "jffs2_get_sb(): mtd%%d, mtdnr %d\n", mtdnr));
  197. return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr, mnt);
  198. }
  199. }
  200. }
  201. /* Try the old way - the hack where we allowed users to mount
  202. /dev/mtdblock$(n) but didn't actually _use_ the blkdev */
  203. err = path_lookup(dev_name, LOOKUP_FOLLOW, &nd);
  204. D1(printk(KERN_DEBUG "jffs2_get_sb(): path_lookup() returned %d, inode %p\n",
  205. err, nd.dentry->d_inode));
  206. if (err)
  207. return err;
  208. err = -EINVAL;
  209. if (!S_ISBLK(nd.dentry->d_inode->i_mode))
  210. goto out;
  211. if (nd.mnt->mnt_flags & MNT_NODEV) {
  212. err = -EACCES;
  213. goto out;
  214. }
  215. if (imajor(nd.dentry->d_inode) != MTD_BLOCK_MAJOR) {
  216. if (!(flags & MS_SILENT))
  217. printk(KERN_NOTICE "Attempt to mount non-MTD device \"%s\" as JFFS2\n",
  218. dev_name);
  219. goto out;
  220. }
  221. mtdnr = iminor(nd.dentry->d_inode);
  222. path_release(&nd);
  223. return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr, mnt);
  224. out:
  225. path_release(&nd);
  226. return err;
  227. }
  228. static void jffs2_put_super (struct super_block *sb)
  229. {
  230. struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
  231. D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));
  232. down(&c->alloc_sem);
  233. jffs2_flush_wbuf_pad(c);
  234. up(&c->alloc_sem);
  235. jffs2_sum_exit(c);
  236. jffs2_free_ino_caches(c);
  237. jffs2_free_raw_node_refs(c);
  238. if (jffs2_blocks_use_vmalloc(c))
  239. vfree(c->blocks);
  240. else
  241. kfree(c->blocks);
  242. jffs2_flash_cleanup(c);
  243. kfree(c->inocache_list);
  244. jffs2_clear_xattr_subsystem(c);
  245. if (c->mtd->sync)
  246. c->mtd->sync(c->mtd);
  247. D1(printk(KERN_DEBUG "jffs2_put_super returning\n"));
  248. }
  249. static void jffs2_kill_sb(struct super_block *sb)
  250. {
  251. struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
  252. if (!(sb->s_flags & MS_RDONLY))
  253. jffs2_stop_garbage_collect_thread(c);
  254. generic_shutdown_super(sb);
  255. put_mtd_device(c->mtd);
  256. kfree(c);
  257. }
  258. static struct file_system_type jffs2_fs_type = {
  259. .owner = THIS_MODULE,
  260. .name = "jffs2",
  261. .get_sb = jffs2_get_sb,
  262. .kill_sb = jffs2_kill_sb,
  263. };
  264. static int __init init_jffs2_fs(void)
  265. {
  266. int ret;
  267. /* Paranoia checks for on-medium structures. If we ask GCC
  268. to pack them with __attribute__((packed)) then it _also_
  269. assumes that they're not aligned -- so it emits crappy
  270. code on some architectures. Ideally we want an attribute
  271. which means just 'no padding', without the alignment
  272. thing. But GCC doesn't have that -- we have to just
  273. hope the structs are the right sizes, instead. */
  274. BUILD_BUG_ON(sizeof(struct jffs2_unknown_node) != 12);
  275. BUILD_BUG_ON(sizeof(struct jffs2_raw_dirent) != 40);
  276. BUILD_BUG_ON(sizeof(struct jffs2_raw_inode) != 68);
  277. BUILD_BUG_ON(sizeof(struct jffs2_raw_summary) != 32);
  278. printk(KERN_INFO "JFFS2 version 2.2."
  279. #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
  280. " (NAND)"
  281. #endif
  282. #ifdef CONFIG_JFFS2_SUMMARY
  283. " (SUMMARY) "
  284. #endif
  285. " © 2001-2006 Red Hat, Inc.\n");
  286. jffs2_inode_cachep = kmem_cache_create("jffs2_i",
  287. sizeof(struct jffs2_inode_info),
  288. 0, (SLAB_RECLAIM_ACCOUNT|
  289. SLAB_MEM_SPREAD),
  290. jffs2_i_init_once, NULL);
  291. if (!jffs2_inode_cachep) {
  292. printk(KERN_ERR "JFFS2 error: Failed to initialise inode cache\n");
  293. return -ENOMEM;
  294. }
  295. ret = jffs2_compressors_init();
  296. if (ret) {
  297. printk(KERN_ERR "JFFS2 error: Failed to initialise compressors\n");
  298. goto out;
  299. }
  300. ret = jffs2_create_slab_caches();
  301. if (ret) {
  302. printk(KERN_ERR "JFFS2 error: Failed to initialise slab caches\n");
  303. goto out_compressors;
  304. }
  305. ret = register_filesystem(&jffs2_fs_type);
  306. if (ret) {
  307. printk(KERN_ERR "JFFS2 error: Failed to register filesystem\n");
  308. goto out_slab;
  309. }
  310. return 0;
  311. out_slab:
  312. jffs2_destroy_slab_caches();
  313. out_compressors:
  314. jffs2_compressors_exit();
  315. out:
  316. kmem_cache_destroy(jffs2_inode_cachep);
  317. return ret;
  318. }
  319. static void __exit exit_jffs2_fs(void)
  320. {
  321. unregister_filesystem(&jffs2_fs_type);
  322. jffs2_destroy_slab_caches();
  323. jffs2_compressors_exit();
  324. kmem_cache_destroy(jffs2_inode_cachep);
  325. }
  326. module_init(init_jffs2_fs);
  327. module_exit(exit_jffs2_fs);
  328. MODULE_DESCRIPTION("The Journalling Flash File System, v2");
  329. MODULE_AUTHOR("Red Hat, Inc.");
  330. MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for
  331. // the sake of this tag. It's Free Software.