super.c 10 KB

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