super.c 10 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/parser.h>
  21. #include <linux/jffs2.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/mtd/super.h>
  24. #include <linux/ctype.h>
  25. #include <linux/namei.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/exportfs.h>
  28. #include "compr.h"
  29. #include "nodelist.h"
  30. static void jffs2_put_super(struct super_block *);
  31. static struct kmem_cache *jffs2_inode_cachep;
  32. static struct inode *jffs2_alloc_inode(struct super_block *sb)
  33. {
  34. struct jffs2_inode_info *f;
  35. f = kmem_cache_alloc(jffs2_inode_cachep, GFP_KERNEL);
  36. if (!f)
  37. return NULL;
  38. return &f->vfs_inode;
  39. }
  40. static void jffs2_i_callback(struct rcu_head *head)
  41. {
  42. struct inode *inode = container_of(head, struct inode, i_rcu);
  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 const char *jffs2_compr_name(unsigned int compr)
  56. {
  57. switch (compr) {
  58. case JFFS2_COMPR_MODE_NONE:
  59. return "none";
  60. #ifdef CONFIG_JFFS2_LZO
  61. case JFFS2_COMPR_MODE_FORCELZO:
  62. return "lzo";
  63. #endif
  64. #ifdef CONFIG_JFFS2_ZLIB
  65. case JFFS2_COMPR_MODE_FORCEZLIB:
  66. return "zlib";
  67. #endif
  68. default:
  69. /* should never happen; programmer error */
  70. WARN_ON(1);
  71. return "";
  72. }
  73. }
  74. static int jffs2_show_options(struct seq_file *s, struct dentry *root)
  75. {
  76. struct jffs2_sb_info *c = JFFS2_SB_INFO(root->d_sb);
  77. struct jffs2_mount_opts *opts = &c->mount_opts;
  78. if (opts->override_compr)
  79. seq_printf(s, ",compr=%s", jffs2_compr_name(opts->compr));
  80. if (opts->rp_size)
  81. seq_printf(s, ",rp_size=%u", opts->rp_size / 1024);
  82. return 0;
  83. }
  84. static int jffs2_sync_fs(struct super_block *sb, int wait)
  85. {
  86. struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
  87. #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
  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(d_inode(child)->i_sb, pino));
  125. }
  126. static const struct export_operations jffs2_export_ops = {
  127. .get_parent = jffs2_get_parent,
  128. .fh_to_dentry = jffs2_fh_to_dentry,
  129. .fh_to_parent = jffs2_fh_to_parent,
  130. };
  131. /*
  132. * JFFS2 mount options.
  133. *
  134. * Opt_override_compr: override default compressor
  135. * Opt_rp_size: size of reserved pool in KiB
  136. * Opt_err: just end of array marker
  137. */
  138. enum {
  139. Opt_override_compr,
  140. Opt_rp_size,
  141. Opt_err,
  142. };
  143. static const match_table_t tokens = {
  144. {Opt_override_compr, "compr=%s"},
  145. {Opt_rp_size, "rp_size=%u"},
  146. {Opt_err, NULL},
  147. };
  148. static int jffs2_parse_options(struct jffs2_sb_info *c, char *data)
  149. {
  150. substring_t args[MAX_OPT_ARGS];
  151. char *p, *name;
  152. unsigned int opt;
  153. if (!data)
  154. return 0;
  155. while ((p = strsep(&data, ","))) {
  156. int token;
  157. if (!*p)
  158. continue;
  159. token = match_token(p, tokens, args);
  160. switch (token) {
  161. case Opt_override_compr:
  162. name = match_strdup(&args[0]);
  163. if (!name)
  164. return -ENOMEM;
  165. if (!strcmp(name, "none"))
  166. c->mount_opts.compr = JFFS2_COMPR_MODE_NONE;
  167. #ifdef CONFIG_JFFS2_LZO
  168. else if (!strcmp(name, "lzo"))
  169. c->mount_opts.compr = JFFS2_COMPR_MODE_FORCELZO;
  170. #endif
  171. #ifdef CONFIG_JFFS2_ZLIB
  172. else if (!strcmp(name, "zlib"))
  173. c->mount_opts.compr =
  174. JFFS2_COMPR_MODE_FORCEZLIB;
  175. #endif
  176. else {
  177. pr_err("Error: unknown compressor \"%s\"\n",
  178. name);
  179. kfree(name);
  180. return -EINVAL;
  181. }
  182. kfree(name);
  183. c->mount_opts.override_compr = true;
  184. break;
  185. case Opt_rp_size:
  186. if (match_int(&args[0], &opt))
  187. return -EINVAL;
  188. opt *= 1024;
  189. if (opt > c->mtd->size) {
  190. pr_warn("Too large reserve pool specified, max "
  191. "is %llu KB\n", c->mtd->size / 1024);
  192. return -EINVAL;
  193. }
  194. c->mount_opts.rp_size = opt;
  195. break;
  196. default:
  197. pr_err("Error: unrecognized mount option '%s' or missing value\n",
  198. p);
  199. return -EINVAL;
  200. }
  201. }
  202. return 0;
  203. }
  204. static int jffs2_remount_fs(struct super_block *sb, int *flags, char *data)
  205. {
  206. struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
  207. int err;
  208. sync_filesystem(sb);
  209. err = jffs2_parse_options(c, data);
  210. if (err)
  211. return -EINVAL;
  212. return jffs2_do_remount_fs(sb, flags, data);
  213. }
  214. static const struct super_operations jffs2_super_operations =
  215. {
  216. .alloc_inode = jffs2_alloc_inode,
  217. .destroy_inode =jffs2_destroy_inode,
  218. .put_super = jffs2_put_super,
  219. .statfs = jffs2_statfs,
  220. .remount_fs = jffs2_remount_fs,
  221. .evict_inode = jffs2_evict_inode,
  222. .dirty_inode = jffs2_dirty_inode,
  223. .show_options = jffs2_show_options,
  224. .sync_fs = jffs2_sync_fs,
  225. };
  226. /*
  227. * fill in the superblock
  228. */
  229. static int jffs2_fill_super(struct super_block *sb, void *data, int silent)
  230. {
  231. struct jffs2_sb_info *c;
  232. int ret;
  233. jffs2_dbg(1, "jffs2_get_sb_mtd():"
  234. " New superblock for device %d (\"%s\")\n",
  235. sb->s_mtd->index, sb->s_mtd->name);
  236. c = kzalloc(sizeof(*c), GFP_KERNEL);
  237. if (!c)
  238. return -ENOMEM;
  239. c->mtd = sb->s_mtd;
  240. c->os_priv = sb;
  241. sb->s_fs_info = c;
  242. ret = jffs2_parse_options(c, data);
  243. if (ret) {
  244. kfree(c);
  245. return -EINVAL;
  246. }
  247. /* Initialize JFFS2 superblock locks, the further initialization will
  248. * be done later */
  249. mutex_init(&c->alloc_sem);
  250. mutex_init(&c->erase_free_sem);
  251. init_waitqueue_head(&c->erase_wait);
  252. init_waitqueue_head(&c->inocache_wq);
  253. spin_lock_init(&c->erase_completion_lock);
  254. spin_lock_init(&c->inocache_lock);
  255. sb->s_op = &jffs2_super_operations;
  256. sb->s_export_op = &jffs2_export_ops;
  257. sb->s_flags = sb->s_flags | MS_NOATIME;
  258. sb->s_xattr = jffs2_xattr_handlers;
  259. #ifdef CONFIG_JFFS2_FS_POSIX_ACL
  260. sb->s_flags |= MS_POSIXACL;
  261. #endif
  262. ret = jffs2_do_fill_super(sb, data, silent);
  263. return ret;
  264. }
  265. static struct dentry *jffs2_mount(struct file_system_type *fs_type,
  266. int flags, const char *dev_name,
  267. void *data)
  268. {
  269. return mount_mtd(fs_type, flags, dev_name, data, jffs2_fill_super);
  270. }
  271. static void jffs2_put_super (struct super_block *sb)
  272. {
  273. struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
  274. jffs2_dbg(2, "%s()\n", __func__);
  275. mutex_lock(&c->alloc_sem);
  276. jffs2_flush_wbuf_pad(c);
  277. mutex_unlock(&c->alloc_sem);
  278. jffs2_sum_exit(c);
  279. jffs2_free_ino_caches(c);
  280. jffs2_free_raw_node_refs(c);
  281. if (jffs2_blocks_use_vmalloc(c))
  282. vfree(c->blocks);
  283. else
  284. kfree(c->blocks);
  285. jffs2_flash_cleanup(c);
  286. kfree(c->inocache_list);
  287. jffs2_clear_xattr_subsystem(c);
  288. mtd_sync(c->mtd);
  289. jffs2_dbg(1, "%s(): returning\n", __func__);
  290. }
  291. static void jffs2_kill_sb(struct super_block *sb)
  292. {
  293. struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
  294. if (!(sb->s_flags & MS_RDONLY))
  295. jffs2_stop_garbage_collect_thread(c);
  296. kill_mtd_super(sb);
  297. kfree(c);
  298. }
  299. static struct file_system_type jffs2_fs_type = {
  300. .owner = THIS_MODULE,
  301. .name = "jffs2",
  302. .mount = jffs2_mount,
  303. .kill_sb = jffs2_kill_sb,
  304. };
  305. MODULE_ALIAS_FS("jffs2");
  306. static int __init init_jffs2_fs(void)
  307. {
  308. int ret;
  309. /* Paranoia checks for on-medium structures. If we ask GCC
  310. to pack them with __attribute__((packed)) then it _also_
  311. assumes that they're not aligned -- so it emits crappy
  312. code on some architectures. Ideally we want an attribute
  313. which means just 'no padding', without the alignment
  314. thing. But GCC doesn't have that -- we have to just
  315. hope the structs are the right sizes, instead. */
  316. BUILD_BUG_ON(sizeof(struct jffs2_unknown_node) != 12);
  317. BUILD_BUG_ON(sizeof(struct jffs2_raw_dirent) != 40);
  318. BUILD_BUG_ON(sizeof(struct jffs2_raw_inode) != 68);
  319. BUILD_BUG_ON(sizeof(struct jffs2_raw_summary) != 32);
  320. pr_info("version 2.2."
  321. #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
  322. " (NAND)"
  323. #endif
  324. #ifdef CONFIG_JFFS2_SUMMARY
  325. " (SUMMARY) "
  326. #endif
  327. " © 2001-2006 Red Hat, Inc.\n");
  328. jffs2_inode_cachep = kmem_cache_create("jffs2_i",
  329. sizeof(struct jffs2_inode_info),
  330. 0, (SLAB_RECLAIM_ACCOUNT|
  331. SLAB_MEM_SPREAD),
  332. jffs2_i_init_once);
  333. if (!jffs2_inode_cachep) {
  334. pr_err("error: Failed to initialise inode cache\n");
  335. return -ENOMEM;
  336. }
  337. ret = jffs2_compressors_init();
  338. if (ret) {
  339. pr_err("error: Failed to initialise compressors\n");
  340. goto out;
  341. }
  342. ret = jffs2_create_slab_caches();
  343. if (ret) {
  344. pr_err("error: Failed to initialise slab caches\n");
  345. goto out_compressors;
  346. }
  347. ret = register_filesystem(&jffs2_fs_type);
  348. if (ret) {
  349. pr_err("error: Failed to register filesystem\n");
  350. goto out_slab;
  351. }
  352. return 0;
  353. out_slab:
  354. jffs2_destroy_slab_caches();
  355. out_compressors:
  356. jffs2_compressors_exit();
  357. out:
  358. kmem_cache_destroy(jffs2_inode_cachep);
  359. return ret;
  360. }
  361. static void __exit exit_jffs2_fs(void)
  362. {
  363. unregister_filesystem(&jffs2_fs_type);
  364. jffs2_destroy_slab_caches();
  365. jffs2_compressors_exit();
  366. /*
  367. * Make sure all delayed rcu free inodes are flushed before we
  368. * destroy cache.
  369. */
  370. rcu_barrier();
  371. kmem_cache_destroy(jffs2_inode_cachep);
  372. }
  373. module_init(init_jffs2_fs);
  374. module_exit(exit_jffs2_fs);
  375. MODULE_DESCRIPTION("The Journalling Flash File System, v2");
  376. MODULE_AUTHOR("Red Hat, Inc.");
  377. MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for
  378. // the sake of this tag. It's Free Software.