super.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 <linux/exportfs.h>
  25. #include "compr.h"
  26. #include "nodelist.h"
  27. static void jffs2_put_super(struct super_block *);
  28. static struct kmem_cache *jffs2_inode_cachep;
  29. static struct inode *jffs2_alloc_inode(struct super_block *sb)
  30. {
  31. struct jffs2_inode_info *f;
  32. f = kmem_cache_alloc(jffs2_inode_cachep, GFP_KERNEL);
  33. if (!f)
  34. return NULL;
  35. return &f->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)
  42. {
  43. struct jffs2_inode_info *f = foo;
  44. mutex_init(&f->sem);
  45. inode_init_once(&f->vfs_inode);
  46. }
  47. static void jffs2_write_super(struct super_block *sb)
  48. {
  49. struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
  50. lock_super(sb);
  51. sb->s_dirt = 0;
  52. if (!(sb->s_flags & MS_RDONLY)) {
  53. D1(printk(KERN_DEBUG "jffs2_write_super()\n"));
  54. jffs2_garbage_collect_trigger(c);
  55. jffs2_erase_pending_blocks(c, 0);
  56. jffs2_flush_wbuf_gc(c, 0);
  57. }
  58. unlock_super(sb);
  59. }
  60. static int jffs2_sync_fs(struct super_block *sb, int wait)
  61. {
  62. struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
  63. jffs2_write_super(sb);
  64. mutex_lock(&c->alloc_sem);
  65. jffs2_flush_wbuf_pad(c);
  66. mutex_unlock(&c->alloc_sem);
  67. return 0;
  68. }
  69. static struct inode *jffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
  70. uint32_t generation)
  71. {
  72. /* We don't care about i_generation. We'll destroy the flash
  73. before we start re-using inode numbers anyway. And even
  74. if that wasn't true, we'd have other problems...*/
  75. return jffs2_iget(sb, ino);
  76. }
  77. static struct dentry *jffs2_fh_to_dentry(struct super_block *sb, struct fid *fid,
  78. int fh_len, int fh_type)
  79. {
  80. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  81. jffs2_nfs_get_inode);
  82. }
  83. static struct dentry *jffs2_fh_to_parent(struct super_block *sb, struct fid *fid,
  84. int fh_len, int fh_type)
  85. {
  86. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  87. jffs2_nfs_get_inode);
  88. }
  89. static struct dentry *jffs2_get_parent(struct dentry *child)
  90. {
  91. struct jffs2_inode_info *f;
  92. uint32_t pino;
  93. BUG_ON(!S_ISDIR(child->d_inode->i_mode));
  94. f = JFFS2_INODE_INFO(child->d_inode);
  95. pino = f->inocache->pino_nlink;
  96. JFFS2_DEBUG("Parent of directory ino #%u is #%u\n",
  97. f->inocache->ino, pino);
  98. return d_obtain_alias(jffs2_iget(child->d_inode->i_sb, pino));
  99. }
  100. static struct export_operations jffs2_export_ops = {
  101. .get_parent = jffs2_get_parent,
  102. .fh_to_dentry = jffs2_fh_to_dentry,
  103. .fh_to_parent = jffs2_fh_to_parent,
  104. };
  105. static const struct super_operations jffs2_super_operations =
  106. {
  107. .alloc_inode = jffs2_alloc_inode,
  108. .destroy_inode =jffs2_destroy_inode,
  109. .put_super = jffs2_put_super,
  110. .write_super = jffs2_write_super,
  111. .statfs = jffs2_statfs,
  112. .remount_fs = jffs2_remount_fs,
  113. .clear_inode = jffs2_clear_inode,
  114. .dirty_inode = jffs2_dirty_inode,
  115. .sync_fs = jffs2_sync_fs,
  116. };
  117. /*
  118. * fill in the superblock
  119. */
  120. static int jffs2_fill_super(struct super_block *sb, void *data, int silent)
  121. {
  122. struct jffs2_sb_info *c;
  123. D1(printk(KERN_DEBUG "jffs2_get_sb_mtd():"
  124. " New superblock for device %d (\"%s\")\n",
  125. sb->s_mtd->index, sb->s_mtd->name));
  126. c = kzalloc(sizeof(*c), GFP_KERNEL);
  127. if (!c)
  128. return -ENOMEM;
  129. c->mtd = sb->s_mtd;
  130. c->os_priv = sb;
  131. sb->s_fs_info = c;
  132. /* Initialize JFFS2 superblock locks, the further initialization will
  133. * be done later */
  134. mutex_init(&c->alloc_sem);
  135. mutex_init(&c->erase_free_sem);
  136. init_waitqueue_head(&c->erase_wait);
  137. init_waitqueue_head(&c->inocache_wq);
  138. spin_lock_init(&c->erase_completion_lock);
  139. spin_lock_init(&c->inocache_lock);
  140. sb->s_op = &jffs2_super_operations;
  141. sb->s_export_op = &jffs2_export_ops;
  142. sb->s_flags = sb->s_flags | MS_NOATIME;
  143. sb->s_xattr = jffs2_xattr_handlers;
  144. #ifdef CONFIG_JFFS2_FS_POSIX_ACL
  145. sb->s_flags |= MS_POSIXACL;
  146. #endif
  147. return jffs2_do_fill_super(sb, data, silent);
  148. }
  149. static int jffs2_get_sb(struct file_system_type *fs_type,
  150. int flags, const char *dev_name,
  151. void *data, struct vfsmount *mnt)
  152. {
  153. return get_sb_mtd(fs_type, flags, dev_name, data, jffs2_fill_super,
  154. mnt);
  155. }
  156. static void jffs2_put_super (struct super_block *sb)
  157. {
  158. struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
  159. D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));
  160. lock_kernel();
  161. if (sb->s_dirt)
  162. jffs2_write_super(sb);
  163. mutex_lock(&c->alloc_sem);
  164. jffs2_flush_wbuf_pad(c);
  165. mutex_unlock(&c->alloc_sem);
  166. jffs2_sum_exit(c);
  167. jffs2_free_ino_caches(c);
  168. jffs2_free_raw_node_refs(c);
  169. if (jffs2_blocks_use_vmalloc(c))
  170. vfree(c->blocks);
  171. else
  172. kfree(c->blocks);
  173. jffs2_flash_cleanup(c);
  174. kfree(c->inocache_list);
  175. jffs2_clear_xattr_subsystem(c);
  176. if (c->mtd->sync)
  177. c->mtd->sync(c->mtd);
  178. unlock_kernel();
  179. D1(printk(KERN_DEBUG "jffs2_put_super returning\n"));
  180. }
  181. static void jffs2_kill_sb(struct super_block *sb)
  182. {
  183. struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
  184. if (!(sb->s_flags & MS_RDONLY))
  185. jffs2_stop_garbage_collect_thread(c);
  186. kill_mtd_super(sb);
  187. kfree(c);
  188. }
  189. static struct file_system_type jffs2_fs_type = {
  190. .owner = THIS_MODULE,
  191. .name = "jffs2",
  192. .get_sb = jffs2_get_sb,
  193. .kill_sb = jffs2_kill_sb,
  194. };
  195. static int __init init_jffs2_fs(void)
  196. {
  197. int ret;
  198. /* Paranoia checks for on-medium structures. If we ask GCC
  199. to pack them with __attribute__((packed)) then it _also_
  200. assumes that they're not aligned -- so it emits crappy
  201. code on some architectures. Ideally we want an attribute
  202. which means just 'no padding', without the alignment
  203. thing. But GCC doesn't have that -- we have to just
  204. hope the structs are the right sizes, instead. */
  205. BUILD_BUG_ON(sizeof(struct jffs2_unknown_node) != 12);
  206. BUILD_BUG_ON(sizeof(struct jffs2_raw_dirent) != 40);
  207. BUILD_BUG_ON(sizeof(struct jffs2_raw_inode) != 68);
  208. BUILD_BUG_ON(sizeof(struct jffs2_raw_summary) != 32);
  209. printk(KERN_INFO "JFFS2 version 2.2."
  210. #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
  211. " (NAND)"
  212. #endif
  213. #ifdef CONFIG_JFFS2_SUMMARY
  214. " (SUMMARY) "
  215. #endif
  216. " © 2001-2006 Red Hat, Inc.\n");
  217. jffs2_inode_cachep = kmem_cache_create("jffs2_i",
  218. sizeof(struct jffs2_inode_info),
  219. 0, (SLAB_RECLAIM_ACCOUNT|
  220. SLAB_MEM_SPREAD),
  221. jffs2_i_init_once);
  222. if (!jffs2_inode_cachep) {
  223. printk(KERN_ERR "JFFS2 error: Failed to initialise inode cache\n");
  224. return -ENOMEM;
  225. }
  226. ret = jffs2_compressors_init();
  227. if (ret) {
  228. printk(KERN_ERR "JFFS2 error: Failed to initialise compressors\n");
  229. goto out;
  230. }
  231. ret = jffs2_create_slab_caches();
  232. if (ret) {
  233. printk(KERN_ERR "JFFS2 error: Failed to initialise slab caches\n");
  234. goto out_compressors;
  235. }
  236. ret = register_filesystem(&jffs2_fs_type);
  237. if (ret) {
  238. printk(KERN_ERR "JFFS2 error: Failed to register filesystem\n");
  239. goto out_slab;
  240. }
  241. return 0;
  242. out_slab:
  243. jffs2_destroy_slab_caches();
  244. out_compressors:
  245. jffs2_compressors_exit();
  246. out:
  247. kmem_cache_destroy(jffs2_inode_cachep);
  248. return ret;
  249. }
  250. static void __exit exit_jffs2_fs(void)
  251. {
  252. unregister_filesystem(&jffs2_fs_type);
  253. jffs2_destroy_slab_caches();
  254. jffs2_compressors_exit();
  255. kmem_cache_destroy(jffs2_inode_cachep);
  256. }
  257. module_init(init_jffs2_fs);
  258. module_exit(exit_jffs2_fs);
  259. MODULE_DESCRIPTION("The Journalling Flash File System, v2");
  260. MODULE_AUTHOR("Red Hat, Inc.");
  261. MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for
  262. // the sake of this tag. It's Free Software.