super.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.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/fs_context.h>
  21. #include <linux/fs_parser.h>
  22. #include <linux/jffs2.h>
  23. #include <linux/pagemap.h>
  24. #include <linux/mtd/super.h>
  25. #include <linux/ctype.h>
  26. #include <linux/namei.h>
  27. #include <linux/seq_file.h>
  28. #include <linux/exportfs.h>
  29. #include "compr.h"
  30. #include "nodelist.h"
  31. static void jffs2_put_super(struct super_block *);
  32. static struct kmem_cache *jffs2_inode_cachep;
  33. static struct inode *jffs2_alloc_inode(struct super_block *sb)
  34. {
  35. struct jffs2_inode_info *f;
  36. f = alloc_inode_sb(sb, jffs2_inode_cachep, GFP_KERNEL);
  37. if (!f)
  38. return NULL;
  39. return &f->vfs_inode;
  40. }
  41. static void jffs2_free_inode(struct inode *inode)
  42. {
  43. struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
  44. kfree(f->target);
  45. kmem_cache_free(jffs2_inode_cachep, f);
  46. }
  47. static void jffs2_i_init_once(void *foo)
  48. {
  49. struct jffs2_inode_info *f = foo;
  50. mutex_init(&f->sem);
  51. f->target = NULL;
  52. inode_init_once(&f->vfs_inode);
  53. }
  54. static const char *jffs2_compr_name(unsigned int compr)
  55. {
  56. switch (compr) {
  57. case JFFS2_COMPR_MODE_NONE:
  58. return "none";
  59. #ifdef CONFIG_JFFS2_LZO
  60. case JFFS2_COMPR_MODE_FORCELZO:
  61. return "lzo";
  62. #endif
  63. #ifdef CONFIG_JFFS2_ZLIB
  64. case JFFS2_COMPR_MODE_FORCEZLIB:
  65. return "zlib";
  66. #endif
  67. default:
  68. /* should never happen; programmer error */
  69. WARN_ON(1);
  70. return "";
  71. }
  72. }
  73. static int jffs2_show_options(struct seq_file *s, struct dentry *root)
  74. {
  75. struct jffs2_sb_info *c = JFFS2_SB_INFO(root->d_sb);
  76. struct jffs2_mount_opts *opts = &c->mount_opts;
  77. if (opts->override_compr)
  78. seq_printf(s, ",compr=%s", jffs2_compr_name(opts->compr));
  79. if (opts->set_rp_size)
  80. seq_printf(s, ",rp_size=%u", opts->rp_size / 1024);
  81. return 0;
  82. }
  83. static int jffs2_sync_fs(struct super_block *sb, int wait)
  84. {
  85. struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
  86. #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
  87. if (jffs2_is_writebuffered(c))
  88. cancel_delayed_work_sync(&c->wbuf_dwork);
  89. #endif
  90. mutex_lock(&c->alloc_sem);
  91. jffs2_flush_wbuf_pad(c);
  92. mutex_unlock(&c->alloc_sem);
  93. return 0;
  94. }
  95. static struct inode *jffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
  96. uint32_t generation)
  97. {
  98. /* We don't care about i_generation. We'll destroy the flash
  99. before we start re-using inode numbers anyway. And even
  100. if that wasn't true, we'd have other problems...*/
  101. return jffs2_iget(sb, ino);
  102. }
  103. static struct dentry *jffs2_fh_to_dentry(struct super_block *sb, struct fid *fid,
  104. int fh_len, int fh_type)
  105. {
  106. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  107. jffs2_nfs_get_inode);
  108. }
  109. static struct dentry *jffs2_fh_to_parent(struct super_block *sb, struct fid *fid,
  110. int fh_len, int fh_type)
  111. {
  112. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  113. jffs2_nfs_get_inode);
  114. }
  115. static struct dentry *jffs2_get_parent(struct dentry *child)
  116. {
  117. struct jffs2_inode_info *f;
  118. uint32_t pino;
  119. BUG_ON(!d_is_dir(child));
  120. f = JFFS2_INODE_INFO(d_inode(child));
  121. pino = f->inocache->pino_nlink;
  122. JFFS2_DEBUG("Parent of directory ino #%u is #%u\n",
  123. f->inocache->ino, pino);
  124. return d_obtain_alias(jffs2_iget(child->d_sb, pino));
  125. }
  126. static const struct export_operations jffs2_export_ops = {
  127. .encode_fh = generic_encode_ino32_fh,
  128. .get_parent = jffs2_get_parent,
  129. .fh_to_dentry = jffs2_fh_to_dentry,
  130. .fh_to_parent = jffs2_fh_to_parent,
  131. };
  132. /*
  133. * JFFS2 mount options.
  134. *
  135. * Opt_source: The source device
  136. * Opt_override_compr: override default compressor
  137. * Opt_rp_size: size of reserved pool in KiB
  138. */
  139. enum {
  140. Opt_override_compr,
  141. Opt_rp_size,
  142. };
  143. static const struct constant_table jffs2_param_compr[] = {
  144. {"none", JFFS2_COMPR_MODE_NONE },
  145. #ifdef CONFIG_JFFS2_LZO
  146. {"lzo", JFFS2_COMPR_MODE_FORCELZO },
  147. #endif
  148. #ifdef CONFIG_JFFS2_ZLIB
  149. {"zlib", JFFS2_COMPR_MODE_FORCEZLIB },
  150. #endif
  151. {}
  152. };
  153. static const struct fs_parameter_spec jffs2_fs_parameters[] = {
  154. fsparam_enum ("compr", Opt_override_compr, jffs2_param_compr),
  155. fsparam_u32 ("rp_size", Opt_rp_size),
  156. {}
  157. };
  158. static int jffs2_parse_param(struct fs_context *fc, struct fs_parameter *param)
  159. {
  160. struct fs_parse_result result;
  161. struct jffs2_sb_info *c = fc->s_fs_info;
  162. int opt;
  163. opt = fs_parse(fc, jffs2_fs_parameters, param, &result);
  164. if (opt < 0)
  165. return opt;
  166. switch (opt) {
  167. case Opt_override_compr:
  168. c->mount_opts.compr = result.uint_32;
  169. c->mount_opts.override_compr = true;
  170. break;
  171. case Opt_rp_size:
  172. if (result.uint_32 > UINT_MAX / 1024)
  173. return invalf(fc, "jffs2: rp_size unrepresentable");
  174. c->mount_opts.rp_size = result.uint_32 * 1024;
  175. c->mount_opts.set_rp_size = true;
  176. break;
  177. default:
  178. return -EINVAL;
  179. }
  180. return 0;
  181. }
  182. static inline void jffs2_update_mount_opts(struct fs_context *fc)
  183. {
  184. struct jffs2_sb_info *new_c = fc->s_fs_info;
  185. struct jffs2_sb_info *c = JFFS2_SB_INFO(fc->root->d_sb);
  186. mutex_lock(&c->alloc_sem);
  187. if (new_c->mount_opts.override_compr) {
  188. c->mount_opts.override_compr = new_c->mount_opts.override_compr;
  189. c->mount_opts.compr = new_c->mount_opts.compr;
  190. }
  191. if (new_c->mount_opts.set_rp_size) {
  192. c->mount_opts.set_rp_size = new_c->mount_opts.set_rp_size;
  193. c->mount_opts.rp_size = new_c->mount_opts.rp_size;
  194. }
  195. mutex_unlock(&c->alloc_sem);
  196. }
  197. static int jffs2_reconfigure(struct fs_context *fc)
  198. {
  199. struct super_block *sb = fc->root->d_sb;
  200. sync_filesystem(sb);
  201. jffs2_update_mount_opts(fc);
  202. return jffs2_do_remount_fs(sb, fc);
  203. }
  204. static const struct super_operations jffs2_super_operations =
  205. {
  206. .alloc_inode = jffs2_alloc_inode,
  207. .free_inode = jffs2_free_inode,
  208. .put_super = jffs2_put_super,
  209. .statfs = jffs2_statfs,
  210. .evict_inode = jffs2_evict_inode,
  211. .dirty_inode = jffs2_dirty_inode,
  212. .show_options = jffs2_show_options,
  213. .sync_fs = jffs2_sync_fs,
  214. };
  215. /*
  216. * fill in the superblock
  217. */
  218. static int jffs2_fill_super(struct super_block *sb, struct fs_context *fc)
  219. {
  220. struct jffs2_sb_info *c = sb->s_fs_info;
  221. jffs2_dbg(1, "jffs2_get_sb_mtd():"
  222. " New superblock for device %d (\"%s\")\n",
  223. sb->s_mtd->index, sb->s_mtd->name);
  224. c->mtd = sb->s_mtd;
  225. c->os_priv = sb;
  226. if (c->mount_opts.rp_size > c->mtd->size)
  227. return invalf(fc, "jffs2: Too large reserve pool specified, max is %llu KB",
  228. c->mtd->size / 1024);
  229. /* Initialize JFFS2 superblock locks, the further initialization will
  230. * be done later */
  231. mutex_init(&c->alloc_sem);
  232. mutex_init(&c->erase_free_sem);
  233. init_waitqueue_head(&c->erase_wait);
  234. init_waitqueue_head(&c->inocache_wq);
  235. spin_lock_init(&c->erase_completion_lock);
  236. spin_lock_init(&c->inocache_lock);
  237. sb->s_op = &jffs2_super_operations;
  238. sb->s_export_op = &jffs2_export_ops;
  239. sb->s_flags = sb->s_flags | SB_NOATIME;
  240. sb->s_xattr = jffs2_xattr_handlers;
  241. #ifdef CONFIG_JFFS2_FS_POSIX_ACL
  242. sb->s_flags |= SB_POSIXACL;
  243. #endif
  244. return jffs2_do_fill_super(sb, fc);
  245. }
  246. static int jffs2_get_tree(struct fs_context *fc)
  247. {
  248. return get_tree_mtd(fc, jffs2_fill_super);
  249. }
  250. static void jffs2_free_fc(struct fs_context *fc)
  251. {
  252. kfree(fc->s_fs_info);
  253. }
  254. static const struct fs_context_operations jffs2_context_ops = {
  255. .free = jffs2_free_fc,
  256. .parse_param = jffs2_parse_param,
  257. .get_tree = jffs2_get_tree,
  258. .reconfigure = jffs2_reconfigure,
  259. };
  260. static int jffs2_init_fs_context(struct fs_context *fc)
  261. {
  262. struct jffs2_sb_info *ctx;
  263. ctx = kzalloc_obj(struct jffs2_sb_info);
  264. if (!ctx)
  265. return -ENOMEM;
  266. fc->s_fs_info = ctx;
  267. fc->ops = &jffs2_context_ops;
  268. return 0;
  269. }
  270. static void jffs2_put_super (struct super_block *sb)
  271. {
  272. struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
  273. jffs2_dbg(2, "%s()\n", __func__);
  274. mutex_lock(&c->alloc_sem);
  275. jffs2_flush_wbuf_pad(c);
  276. mutex_unlock(&c->alloc_sem);
  277. jffs2_sum_exit(c);
  278. jffs2_free_ino_caches(c);
  279. jffs2_free_raw_node_refs(c);
  280. kvfree(c->blocks);
  281. jffs2_flash_cleanup(c);
  282. kfree(c->inocache_list);
  283. jffs2_clear_xattr_subsystem(c);
  284. mtd_sync(c->mtd);
  285. jffs2_dbg(1, "%s(): returning\n", __func__);
  286. }
  287. static void jffs2_kill_sb(struct super_block *sb)
  288. {
  289. struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
  290. if (c && !sb_rdonly(sb))
  291. jffs2_stop_garbage_collect_thread(c);
  292. kill_mtd_super(sb);
  293. kfree(c);
  294. }
  295. static struct file_system_type jffs2_fs_type = {
  296. .owner = THIS_MODULE,
  297. .name = "jffs2",
  298. .init_fs_context = jffs2_init_fs_context,
  299. .parameters = jffs2_fs_parameters,
  300. .kill_sb = jffs2_kill_sb,
  301. };
  302. MODULE_ALIAS_FS("jffs2");
  303. static int __init init_jffs2_fs(void)
  304. {
  305. int ret;
  306. /* Paranoia checks for on-medium structures. If we ask GCC
  307. to pack them with __attribute__((packed)) then it _also_
  308. assumes that they're not aligned -- so it emits crappy
  309. code on some architectures. Ideally we want an attribute
  310. which means just 'no padding', without the alignment
  311. thing. But GCC doesn't have that -- we have to just
  312. hope the structs are the right sizes, instead. */
  313. BUILD_BUG_ON(sizeof(struct jffs2_unknown_node) != 12);
  314. BUILD_BUG_ON(sizeof(struct jffs2_raw_dirent) != 40);
  315. BUILD_BUG_ON(sizeof(struct jffs2_raw_inode) != 68);
  316. BUILD_BUG_ON(sizeof(struct jffs2_raw_summary) != 32);
  317. pr_info("version 2.2."
  318. #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
  319. " (NAND)"
  320. #endif
  321. #ifdef CONFIG_JFFS2_SUMMARY
  322. " (SUMMARY) "
  323. #endif
  324. " © 2001-2006 Red Hat, Inc.\n");
  325. jffs2_inode_cachep = kmem_cache_create("jffs2_i",
  326. sizeof(struct jffs2_inode_info),
  327. 0, (SLAB_RECLAIM_ACCOUNT|
  328. SLAB_ACCOUNT),
  329. jffs2_i_init_once);
  330. if (!jffs2_inode_cachep) {
  331. pr_err("error: Failed to initialise inode cache\n");
  332. return -ENOMEM;
  333. }
  334. ret = jffs2_compressors_init();
  335. if (ret) {
  336. pr_err("error: Failed to initialise compressors\n");
  337. goto out;
  338. }
  339. ret = jffs2_create_slab_caches();
  340. if (ret) {
  341. pr_err("error: Failed to initialise slab caches\n");
  342. goto out_compressors;
  343. }
  344. ret = register_filesystem(&jffs2_fs_type);
  345. if (ret) {
  346. pr_err("error: Failed to register filesystem\n");
  347. goto out_slab;
  348. }
  349. return 0;
  350. out_slab:
  351. jffs2_destroy_slab_caches();
  352. out_compressors:
  353. jffs2_compressors_exit();
  354. out:
  355. kmem_cache_destroy(jffs2_inode_cachep);
  356. return ret;
  357. }
  358. static void __exit exit_jffs2_fs(void)
  359. {
  360. unregister_filesystem(&jffs2_fs_type);
  361. jffs2_destroy_slab_caches();
  362. jffs2_compressors_exit();
  363. /*
  364. * Make sure all delayed rcu free inodes are flushed before we
  365. * destroy cache.
  366. */
  367. rcu_barrier();
  368. kmem_cache_destroy(jffs2_inode_cachep);
  369. }
  370. module_init(init_jffs2_fs);
  371. module_exit(exit_jffs2_fs);
  372. MODULE_DESCRIPTION("The Journalling Flash File System, v2");
  373. MODULE_AUTHOR("Red Hat, Inc.");
  374. MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for
  375. // the sake of this tag. It's Free Software.