super.c 7.8 KB

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