super.c 6.0 KB

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