super.c 10 KB

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