super.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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/parser.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/seq_file.h>
  26. #include <linux/exportfs.h>
  27. #include "compr.h"
  28. #include "nodelist.h"
  29. static void jffs2_put_super(struct super_block *);
  30. static struct kmem_cache *jffs2_inode_cachep;
  31. static struct inode *jffs2_alloc_inode(struct super_block *sb)
  32. {
  33. struct jffs2_inode_info *f;
  34. f = kmem_cache_alloc(jffs2_inode_cachep, GFP_KERNEL);
  35. if (!f)
  36. return NULL;
  37. return &f->vfs_inode;
  38. }
  39. static void jffs2_i_callback(struct rcu_head *head)
  40. {
  41. struct inode *inode = container_of(head, struct inode, i_rcu);
  42. INIT_LIST_HEAD(&inode->i_dentry);
  43. kmem_cache_free(jffs2_inode_cachep, JFFS2_INODE_INFO(inode));
  44. }
  45. static void jffs2_destroy_inode(struct inode *inode)
  46. {
  47. call_rcu(&inode->i_rcu, jffs2_i_callback);
  48. }
  49. static void jffs2_i_init_once(void *foo)
  50. {
  51. struct jffs2_inode_info *f = foo;
  52. mutex_init(&f->sem);
  53. inode_init_once(&f->vfs_inode);
  54. }
  55. static void jffs2_write_super(struct super_block *sb)
  56. {
  57. struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
  58. lock_super(sb);
  59. sb->s_dirt = 0;
  60. if (!(sb->s_flags & MS_RDONLY)) {
  61. D1(printk(KERN_DEBUG "jffs2_write_super()\n"));
  62. jffs2_flush_wbuf_gc(c, 0);
  63. }
  64. unlock_super(sb);
  65. }
  66. static const char *jffs2_compr_name(unsigned int compr)
  67. {
  68. switch (compr) {
  69. case JFFS2_COMPR_MODE_NONE:
  70. return "none";
  71. default:
  72. /* should never happen; programmer error */
  73. WARN_ON(1);
  74. return "";
  75. }
  76. }
  77. static int jffs2_show_options(struct seq_file *s, struct vfsmount *mnt)
  78. {
  79. struct jffs2_sb_info *c = JFFS2_SB_INFO(mnt->mnt_sb);
  80. struct jffs2_mount_opts *opts = &c->mount_opts;
  81. if (opts->override_compr)
  82. seq_printf(s, ",compr=%s", jffs2_compr_name(opts->compr));
  83. return 0;
  84. }
  85. static int jffs2_sync_fs(struct super_block *sb, int wait)
  86. {
  87. struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
  88. jffs2_write_super(sb);
  89. mutex_lock(&c->alloc_sem);
  90. jffs2_flush_wbuf_pad(c);
  91. mutex_unlock(&c->alloc_sem);
  92. return 0;
  93. }
  94. static struct inode *jffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
  95. uint32_t generation)
  96. {
  97. /* We don't care about i_generation. We'll destroy the flash
  98. before we start re-using inode numbers anyway. And even
  99. if that wasn't true, we'd have other problems...*/
  100. return jffs2_iget(sb, ino);
  101. }
  102. static struct dentry *jffs2_fh_to_dentry(struct super_block *sb, struct fid *fid,
  103. int fh_len, int fh_type)
  104. {
  105. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  106. jffs2_nfs_get_inode);
  107. }
  108. static struct dentry *jffs2_fh_to_parent(struct super_block *sb, struct fid *fid,
  109. int fh_len, int fh_type)
  110. {
  111. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  112. jffs2_nfs_get_inode);
  113. }
  114. static struct dentry *jffs2_get_parent(struct dentry *child)
  115. {
  116. struct jffs2_inode_info *f;
  117. uint32_t pino;
  118. BUG_ON(!S_ISDIR(child->d_inode->i_mode));
  119. f = JFFS2_INODE_INFO(child->d_inode);
  120. pino = f->inocache->pino_nlink;
  121. JFFS2_DEBUG("Parent of directory ino #%u is #%u\n",
  122. f->inocache->ino, pino);
  123. return d_obtain_alias(jffs2_iget(child->d_inode->i_sb, pino));
  124. }
  125. static const struct export_operations jffs2_export_ops = {
  126. .get_parent = jffs2_get_parent,
  127. .fh_to_dentry = jffs2_fh_to_dentry,
  128. .fh_to_parent = jffs2_fh_to_parent,
  129. };
  130. /*
  131. * JFFS2 mount options.
  132. *
  133. * Opt_override_compr: override default compressor
  134. * Opt_err: just end of array marker
  135. */
  136. enum {
  137. Opt_override_compr,
  138. Opt_err,
  139. };
  140. static const match_table_t tokens = {
  141. {Opt_override_compr, "compr=%s"},
  142. {Opt_err, NULL},
  143. };
  144. static int jffs2_parse_options(struct jffs2_sb_info *c, char *data)
  145. {
  146. substring_t args[MAX_OPT_ARGS];
  147. char *p, *name;
  148. if (!data)
  149. return 0;
  150. while ((p = strsep(&data, ","))) {
  151. int token;
  152. if (!*p)
  153. continue;
  154. token = match_token(p, tokens, args);
  155. switch (token) {
  156. case Opt_override_compr:
  157. name = match_strdup(&args[0]);
  158. if (!name)
  159. return -ENOMEM;
  160. if (!strcmp(name, "none")) {
  161. c->mount_opts.compr = JFFS2_COMPR_MODE_NONE;
  162. c->mount_opts.override_compr = true;
  163. }
  164. kfree(name);
  165. break;
  166. default:
  167. printk(KERN_ERR "JFFS2 Error: unrecognized mount option '%s' or missing value\n",
  168. p);
  169. return -EINVAL;
  170. }
  171. }
  172. return 0;
  173. }
  174. static int jffs2_remount_fs(struct super_block *sb, int *flags, char *data)
  175. {
  176. struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
  177. int err;
  178. err = jffs2_parse_options(c, data);
  179. if (err)
  180. return -EINVAL;
  181. return jffs2_do_remount_fs(sb, flags, data);
  182. }
  183. static const struct super_operations jffs2_super_operations =
  184. {
  185. .alloc_inode = jffs2_alloc_inode,
  186. .destroy_inode =jffs2_destroy_inode,
  187. .put_super = jffs2_put_super,
  188. .write_super = jffs2_write_super,
  189. .statfs = jffs2_statfs,
  190. .remount_fs = jffs2_remount_fs,
  191. .evict_inode = jffs2_evict_inode,
  192. .dirty_inode = jffs2_dirty_inode,
  193. .show_options = jffs2_show_options,
  194. .sync_fs = jffs2_sync_fs,
  195. };
  196. /*
  197. * fill in the superblock
  198. */
  199. static int jffs2_fill_super(struct super_block *sb, void *data, int silent)
  200. {
  201. struct jffs2_sb_info *c;
  202. int ret;
  203. D1(printk(KERN_DEBUG "jffs2_get_sb_mtd():"
  204. " New superblock for device %d (\"%s\")\n",
  205. sb->s_mtd->index, sb->s_mtd->name));
  206. c = kzalloc(sizeof(*c), GFP_KERNEL);
  207. if (!c)
  208. return -ENOMEM;
  209. c->mtd = sb->s_mtd;
  210. c->os_priv = sb;
  211. sb->s_fs_info = c;
  212. ret = jffs2_parse_options(c, data);
  213. if (ret) {
  214. kfree(c);
  215. return -EINVAL;
  216. }
  217. /* Initialize JFFS2 superblock locks, the further initialization will
  218. * be done later */
  219. mutex_init(&c->alloc_sem);
  220. mutex_init(&c->erase_free_sem);
  221. init_waitqueue_head(&c->erase_wait);
  222. init_waitqueue_head(&c->inocache_wq);
  223. spin_lock_init(&c->erase_completion_lock);
  224. spin_lock_init(&c->inocache_lock);
  225. sb->s_op = &jffs2_super_operations;
  226. sb->s_export_op = &jffs2_export_ops;
  227. sb->s_flags = sb->s_flags | MS_NOATIME;
  228. sb->s_xattr = jffs2_xattr_handlers;
  229. #ifdef CONFIG_JFFS2_FS_POSIX_ACL
  230. sb->s_flags |= MS_POSIXACL;
  231. #endif
  232. ret = jffs2_do_fill_super(sb, data, silent);
  233. return ret;
  234. }
  235. static struct dentry *jffs2_mount(struct file_system_type *fs_type,
  236. int flags, const char *dev_name,
  237. void *data)
  238. {
  239. return mount_mtd(fs_type, flags, dev_name, data, jffs2_fill_super);
  240. }
  241. static void jffs2_put_super (struct super_block *sb)
  242. {
  243. struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
  244. D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));
  245. if (sb->s_dirt)
  246. jffs2_write_super(sb);
  247. mutex_lock(&c->alloc_sem);
  248. jffs2_flush_wbuf_pad(c);
  249. mutex_unlock(&c->alloc_sem);
  250. jffs2_sum_exit(c);
  251. jffs2_free_ino_caches(c);
  252. jffs2_free_raw_node_refs(c);
  253. if (jffs2_blocks_use_vmalloc(c))
  254. vfree(c->blocks);
  255. else
  256. kfree(c->blocks);
  257. jffs2_flash_cleanup(c);
  258. kfree(c->inocache_list);
  259. jffs2_clear_xattr_subsystem(c);
  260. if (c->mtd->sync)
  261. c->mtd->sync(c->mtd);
  262. D1(printk(KERN_DEBUG "jffs2_put_super returning\n"));
  263. }
  264. static void jffs2_kill_sb(struct super_block *sb)
  265. {
  266. struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
  267. if (!(sb->s_flags & MS_RDONLY))
  268. jffs2_stop_garbage_collect_thread(c);
  269. kill_mtd_super(sb);
  270. kfree(c);
  271. }
  272. static struct file_system_type jffs2_fs_type = {
  273. .owner = THIS_MODULE,
  274. .name = "jffs2",
  275. .mount = jffs2_mount,
  276. .kill_sb = jffs2_kill_sb,
  277. };
  278. static int __init init_jffs2_fs(void)
  279. {
  280. int ret;
  281. /* Paranoia checks for on-medium structures. If we ask GCC
  282. to pack them with __attribute__((packed)) then it _also_
  283. assumes that they're not aligned -- so it emits crappy
  284. code on some architectures. Ideally we want an attribute
  285. which means just 'no padding', without the alignment
  286. thing. But GCC doesn't have that -- we have to just
  287. hope the structs are the right sizes, instead. */
  288. BUILD_BUG_ON(sizeof(struct jffs2_unknown_node) != 12);
  289. BUILD_BUG_ON(sizeof(struct jffs2_raw_dirent) != 40);
  290. BUILD_BUG_ON(sizeof(struct jffs2_raw_inode) != 68);
  291. BUILD_BUG_ON(sizeof(struct jffs2_raw_summary) != 32);
  292. printk(KERN_INFO "JFFS2 version 2.2."
  293. #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
  294. " (NAND)"
  295. #endif
  296. #ifdef CONFIG_JFFS2_SUMMARY
  297. " (SUMMARY) "
  298. #endif
  299. " © 2001-2006 Red Hat, Inc.\n");
  300. jffs2_inode_cachep = kmem_cache_create("jffs2_i",
  301. sizeof(struct jffs2_inode_info),
  302. 0, (SLAB_RECLAIM_ACCOUNT|
  303. SLAB_MEM_SPREAD),
  304. jffs2_i_init_once);
  305. if (!jffs2_inode_cachep) {
  306. printk(KERN_ERR "JFFS2 error: Failed to initialise inode cache\n");
  307. return -ENOMEM;
  308. }
  309. ret = jffs2_compressors_init();
  310. if (ret) {
  311. printk(KERN_ERR "JFFS2 error: Failed to initialise compressors\n");
  312. goto out;
  313. }
  314. ret = jffs2_create_slab_caches();
  315. if (ret) {
  316. printk(KERN_ERR "JFFS2 error: Failed to initialise slab caches\n");
  317. goto out_compressors;
  318. }
  319. ret = register_filesystem(&jffs2_fs_type);
  320. if (ret) {
  321. printk(KERN_ERR "JFFS2 error: Failed to register filesystem\n");
  322. goto out_slab;
  323. }
  324. return 0;
  325. out_slab:
  326. jffs2_destroy_slab_caches();
  327. out_compressors:
  328. jffs2_compressors_exit();
  329. out:
  330. kmem_cache_destroy(jffs2_inode_cachep);
  331. return ret;
  332. }
  333. static void __exit exit_jffs2_fs(void)
  334. {
  335. unregister_filesystem(&jffs2_fs_type);
  336. jffs2_destroy_slab_caches();
  337. jffs2_compressors_exit();
  338. kmem_cache_destroy(jffs2_inode_cachep);
  339. }
  340. module_init(init_jffs2_fs);
  341. module_exit(exit_jffs2_fs);
  342. MODULE_DESCRIPTION("The Journalling Flash File System, v2");
  343. MODULE_AUTHOR("Red Hat, Inc.");
  344. MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for
  345. // the sake of this tag. It's Free Software.