super.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. ret = jffs2_do_fill_super(sb, data, (flags&MS_VERBOSE)?1:0);
  132. if (ret) {
  133. /* Failure case... */
  134. up_write(&sb->s_umount);
  135. deactivate_super(sb);
  136. return ERR_PTR(ret);
  137. }
  138. sb->s_flags |= MS_ACTIVE;
  139. return sb;
  140. out_put:
  141. kfree(c);
  142. put_mtd_device(mtd);
  143. return sb;
  144. }
  145. static struct super_block *jffs2_get_sb_mtdnr(struct file_system_type *fs_type,
  146. int flags, const char *dev_name,
  147. void *data, int mtdnr)
  148. {
  149. struct mtd_info *mtd;
  150. mtd = get_mtd_device(NULL, mtdnr);
  151. if (!mtd) {
  152. D1(printk(KERN_DEBUG "jffs2: MTD device #%u doesn't appear to exist\n", mtdnr));
  153. return ERR_PTR(-EINVAL);
  154. }
  155. return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd);
  156. }
  157. static struct super_block *jffs2_get_sb(struct file_system_type *fs_type,
  158. int flags, const char *dev_name,
  159. void *data)
  160. {
  161. int err;
  162. struct nameidata nd;
  163. int mtdnr;
  164. if (!dev_name)
  165. return ERR_PTR(-EINVAL);
  166. D1(printk(KERN_DEBUG "jffs2_get_sb(): dev_name \"%s\"\n", dev_name));
  167. /* The preferred way of mounting in future; especially when
  168. CONFIG_BLK_DEV is implemented - we specify the underlying
  169. MTD device by number or by name, so that we don't require
  170. block device support to be present in the kernel. */
  171. /* FIXME: How to do the root fs this way? */
  172. if (dev_name[0] == 'm' && dev_name[1] == 't' && dev_name[2] == 'd') {
  173. /* Probably mounting without the blkdev crap */
  174. if (dev_name[3] == ':') {
  175. struct mtd_info *mtd;
  176. /* Mount by MTD device name */
  177. D1(printk(KERN_DEBUG "jffs2_get_sb(): mtd:%%s, name \"%s\"\n", dev_name+4));
  178. for (mtdnr = 0; mtdnr < MAX_MTD_DEVICES; mtdnr++) {
  179. mtd = get_mtd_device(NULL, mtdnr);
  180. if (mtd) {
  181. if (!strcmp(mtd->name, dev_name+4))
  182. return jffs2_get_sb_mtd(fs_type, flags, dev_name, data, mtd);
  183. put_mtd_device(mtd);
  184. }
  185. }
  186. printk(KERN_NOTICE "jffs2_get_sb(): MTD device with name \"%s\" not found.\n", dev_name+4);
  187. } else if (isdigit(dev_name[3])) {
  188. /* Mount by MTD device number name */
  189. char *endptr;
  190. mtdnr = simple_strtoul(dev_name+3, &endptr, 0);
  191. if (!*endptr) {
  192. /* It was a valid number */
  193. D1(printk(KERN_DEBUG "jffs2_get_sb(): mtd%%d, mtdnr %d\n", mtdnr));
  194. return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr);
  195. }
  196. }
  197. }
  198. /* Try the old way - the hack where we allowed users to mount
  199. /dev/mtdblock$(n) but didn't actually _use_ the blkdev */
  200. err = path_lookup(dev_name, LOOKUP_FOLLOW, &nd);
  201. D1(printk(KERN_DEBUG "jffs2_get_sb(): path_lookup() returned %d, inode %p\n",
  202. err, nd.dentry->d_inode));
  203. if (err)
  204. return ERR_PTR(err);
  205. err = -EINVAL;
  206. if (!S_ISBLK(nd.dentry->d_inode->i_mode))
  207. goto out;
  208. if (nd.mnt->mnt_flags & MNT_NODEV) {
  209. err = -EACCES;
  210. goto out;
  211. }
  212. if (imajor(nd.dentry->d_inode) != MTD_BLOCK_MAJOR) {
  213. if (!(flags & MS_VERBOSE)) /* Yes I mean this. Strangely */
  214. printk(KERN_NOTICE "Attempt to mount non-MTD device \"%s\" as JFFS2\n",
  215. dev_name);
  216. goto out;
  217. }
  218. mtdnr = iminor(nd.dentry->d_inode);
  219. path_release(&nd);
  220. return jffs2_get_sb_mtdnr(fs_type, flags, dev_name, data, mtdnr);
  221. out:
  222. path_release(&nd);
  223. return ERR_PTR(err);
  224. }
  225. static void jffs2_put_super (struct super_block *sb)
  226. {
  227. struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
  228. D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));
  229. down(&c->alloc_sem);
  230. jffs2_flush_wbuf_pad(c);
  231. up(&c->alloc_sem);
  232. jffs2_sum_exit(c);
  233. jffs2_free_ino_caches(c);
  234. jffs2_free_raw_node_refs(c);
  235. if (jffs2_blocks_use_vmalloc(c))
  236. vfree(c->blocks);
  237. else
  238. kfree(c->blocks);
  239. jffs2_flash_cleanup(c);
  240. kfree(c->inocache_list);
  241. if (c->mtd->sync)
  242. c->mtd->sync(c->mtd);
  243. D1(printk(KERN_DEBUG "jffs2_put_super returning\n"));
  244. }
  245. static void jffs2_kill_sb(struct super_block *sb)
  246. {
  247. struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
  248. if (!(sb->s_flags & MS_RDONLY))
  249. jffs2_stop_garbage_collect_thread(c);
  250. generic_shutdown_super(sb);
  251. put_mtd_device(c->mtd);
  252. kfree(c);
  253. }
  254. static struct file_system_type jffs2_fs_type = {
  255. .owner = THIS_MODULE,
  256. .name = "jffs2",
  257. .get_sb = jffs2_get_sb,
  258. .kill_sb = jffs2_kill_sb,
  259. };
  260. static int __init init_jffs2_fs(void)
  261. {
  262. int ret;
  263. printk(KERN_INFO "JFFS2 version 2.2."
  264. #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
  265. " (NAND)"
  266. #endif
  267. #ifdef CONFIG_JFFS2_SUMMARY
  268. " (SUMMARY) "
  269. #endif
  270. " (C) 2001-2003 Red Hat, Inc.\n");
  271. jffs2_inode_cachep = kmem_cache_create("jffs2_i",
  272. sizeof(struct jffs2_inode_info),
  273. 0, SLAB_RECLAIM_ACCOUNT,
  274. jffs2_i_init_once, NULL);
  275. if (!jffs2_inode_cachep) {
  276. printk(KERN_ERR "JFFS2 error: Failed to initialise inode cache\n");
  277. return -ENOMEM;
  278. }
  279. ret = jffs2_compressors_init();
  280. if (ret) {
  281. printk(KERN_ERR "JFFS2 error: Failed to initialise compressors\n");
  282. goto out;
  283. }
  284. ret = jffs2_create_slab_caches();
  285. if (ret) {
  286. printk(KERN_ERR "JFFS2 error: Failed to initialise slab caches\n");
  287. goto out_compressors;
  288. }
  289. ret = register_filesystem(&jffs2_fs_type);
  290. if (ret) {
  291. printk(KERN_ERR "JFFS2 error: Failed to register filesystem\n");
  292. goto out_slab;
  293. }
  294. return 0;
  295. out_slab:
  296. jffs2_destroy_slab_caches();
  297. out_compressors:
  298. jffs2_compressors_exit();
  299. out:
  300. kmem_cache_destroy(jffs2_inode_cachep);
  301. return ret;
  302. }
  303. static void __exit exit_jffs2_fs(void)
  304. {
  305. unregister_filesystem(&jffs2_fs_type);
  306. jffs2_destroy_slab_caches();
  307. jffs2_compressors_exit();
  308. kmem_cache_destroy(jffs2_inode_cachep);
  309. }
  310. module_init(init_jffs2_fs);
  311. module_exit(exit_jffs2_fs);
  312. MODULE_DESCRIPTION("The Journalling Flash File System, v2");
  313. MODULE_AUTHOR("Red Hat, Inc.");
  314. MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for
  315. // the sake of this tag. It's Free Software.