super.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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/super.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(struct kmem_cache *cachep, void *foo)
  41. {
  42. struct jffs2_inode_info *ei = (struct jffs2_inode_info *) foo;
  43. mutex_init(&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. mutex_lock(&c->alloc_sem);
  50. jffs2_flush_wbuf_pad(c);
  51. mutex_unlock(&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. .put_super = jffs2_put_super,
  59. .write_super = jffs2_write_super,
  60. .statfs = jffs2_statfs,
  61. .remount_fs = jffs2_remount_fs,
  62. .clear_inode = jffs2_clear_inode,
  63. .dirty_inode = jffs2_dirty_inode,
  64. .sync_fs = jffs2_sync_fs,
  65. };
  66. /*
  67. * fill in the superblock
  68. */
  69. static int jffs2_fill_super(struct super_block *sb, void *data, int silent)
  70. {
  71. struct jffs2_sb_info *c;
  72. D1(printk(KERN_DEBUG "jffs2_get_sb_mtd():"
  73. " New superblock for device %d (\"%s\")\n",
  74. sb->s_mtd->index, sb->s_mtd->name));
  75. c = kzalloc(sizeof(*c), GFP_KERNEL);
  76. if (!c)
  77. return -ENOMEM;
  78. c->mtd = sb->s_mtd;
  79. c->os_priv = sb;
  80. sb->s_fs_info = c;
  81. /* Initialize JFFS2 superblock locks, the further initialization will
  82. * be done later */
  83. mutex_init(&c->alloc_sem);
  84. mutex_init(&c->erase_free_sem);
  85. init_waitqueue_head(&c->erase_wait);
  86. init_waitqueue_head(&c->inocache_wq);
  87. spin_lock_init(&c->erase_completion_lock);
  88. spin_lock_init(&c->inocache_lock);
  89. sb->s_op = &jffs2_super_operations;
  90. sb->s_flags = sb->s_flags | MS_NOATIME;
  91. sb->s_xattr = jffs2_xattr_handlers;
  92. #ifdef CONFIG_JFFS2_FS_POSIX_ACL
  93. sb->s_flags |= MS_POSIXACL;
  94. #endif
  95. return jffs2_do_fill_super(sb, data, silent);
  96. }
  97. static int jffs2_get_sb(struct file_system_type *fs_type,
  98. int flags, const char *dev_name,
  99. void *data, struct vfsmount *mnt)
  100. {
  101. return get_sb_mtd(fs_type, flags, dev_name, data, jffs2_fill_super,
  102. mnt);
  103. }
  104. static void jffs2_put_super (struct super_block *sb)
  105. {
  106. struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
  107. D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));
  108. mutex_lock(&c->alloc_sem);
  109. jffs2_flush_wbuf_pad(c);
  110. mutex_unlock(&c->alloc_sem);
  111. jffs2_sum_exit(c);
  112. jffs2_free_ino_caches(c);
  113. jffs2_free_raw_node_refs(c);
  114. if (jffs2_blocks_use_vmalloc(c))
  115. vfree(c->blocks);
  116. else
  117. kfree(c->blocks);
  118. jffs2_flash_cleanup(c);
  119. kfree(c->inocache_list);
  120. jffs2_clear_xattr_subsystem(c);
  121. if (c->mtd->sync)
  122. c->mtd->sync(c->mtd);
  123. D1(printk(KERN_DEBUG "jffs2_put_super returning\n"));
  124. }
  125. static void jffs2_kill_sb(struct super_block *sb)
  126. {
  127. struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
  128. if (!(sb->s_flags & MS_RDONLY))
  129. jffs2_stop_garbage_collect_thread(c);
  130. kill_mtd_super(sb);
  131. kfree(c);
  132. }
  133. static struct file_system_type jffs2_fs_type = {
  134. .owner = THIS_MODULE,
  135. .name = "jffs2",
  136. .get_sb = jffs2_get_sb,
  137. .kill_sb = jffs2_kill_sb,
  138. };
  139. static int __init init_jffs2_fs(void)
  140. {
  141. int ret;
  142. /* Paranoia checks for on-medium structures. If we ask GCC
  143. to pack them with __attribute__((packed)) then it _also_
  144. assumes that they're not aligned -- so it emits crappy
  145. code on some architectures. Ideally we want an attribute
  146. which means just 'no padding', without the alignment
  147. thing. But GCC doesn't have that -- we have to just
  148. hope the structs are the right sizes, instead. */
  149. BUILD_BUG_ON(sizeof(struct jffs2_unknown_node) != 12);
  150. BUILD_BUG_ON(sizeof(struct jffs2_raw_dirent) != 40);
  151. BUILD_BUG_ON(sizeof(struct jffs2_raw_inode) != 68);
  152. BUILD_BUG_ON(sizeof(struct jffs2_raw_summary) != 32);
  153. printk(KERN_INFO "JFFS2 version 2.2."
  154. #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
  155. " (NAND)"
  156. #endif
  157. #ifdef CONFIG_JFFS2_SUMMARY
  158. " (SUMMARY) "
  159. #endif
  160. " © 2001-2006 Red Hat, Inc.\n");
  161. jffs2_inode_cachep = kmem_cache_create("jffs2_i",
  162. sizeof(struct jffs2_inode_info),
  163. 0, (SLAB_RECLAIM_ACCOUNT|
  164. SLAB_MEM_SPREAD),
  165. jffs2_i_init_once);
  166. if (!jffs2_inode_cachep) {
  167. printk(KERN_ERR "JFFS2 error: Failed to initialise inode cache\n");
  168. return -ENOMEM;
  169. }
  170. ret = jffs2_compressors_init();
  171. if (ret) {
  172. printk(KERN_ERR "JFFS2 error: Failed to initialise compressors\n");
  173. goto out;
  174. }
  175. ret = jffs2_create_slab_caches();
  176. if (ret) {
  177. printk(KERN_ERR "JFFS2 error: Failed to initialise slab caches\n");
  178. goto out_compressors;
  179. }
  180. ret = register_filesystem(&jffs2_fs_type);
  181. if (ret) {
  182. printk(KERN_ERR "JFFS2 error: Failed to register filesystem\n");
  183. goto out_slab;
  184. }
  185. return 0;
  186. out_slab:
  187. jffs2_destroy_slab_caches();
  188. out_compressors:
  189. jffs2_compressors_exit();
  190. out:
  191. kmem_cache_destroy(jffs2_inode_cachep);
  192. return ret;
  193. }
  194. static void __exit exit_jffs2_fs(void)
  195. {
  196. unregister_filesystem(&jffs2_fs_type);
  197. jffs2_destroy_slab_caches();
  198. jffs2_compressors_exit();
  199. kmem_cache_destroy(jffs2_inode_cachep);
  200. }
  201. module_init(init_jffs2_fs);
  202. module_exit(exit_jffs2_fs);
  203. MODULE_DESCRIPTION("The Journalling Flash File System, v2");
  204. MODULE_AUTHOR("Red Hat, Inc.");
  205. MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for
  206. // the sake of this tag. It's Free Software.