super.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. if (jffs2_is_writebuffered(c))
  89. cancel_delayed_work_sync(&c->wbuf_dwork);
  90. #endif
  91. mutex_lock(&c->alloc_sem);
  92. jffs2_flush_wbuf_pad(c);
  93. mutex_unlock(&c->alloc_sem);
  94. return 0;
  95. }
  96. static struct inode *jffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
  97. uint32_t generation)
  98. {
  99. /* We don't care about i_generation. We'll destroy the flash
  100. before we start re-using inode numbers anyway. And even
  101. if that wasn't true, we'd have other problems...*/
  102. return jffs2_iget(sb, ino);
  103. }
  104. static struct dentry *jffs2_fh_to_dentry(struct super_block *sb, struct fid *fid,
  105. int fh_len, int fh_type)
  106. {
  107. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  108. jffs2_nfs_get_inode);
  109. }
  110. static struct dentry *jffs2_fh_to_parent(struct super_block *sb, struct fid *fid,
  111. int fh_len, int fh_type)
  112. {
  113. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  114. jffs2_nfs_get_inode);
  115. }
  116. static struct dentry *jffs2_get_parent(struct dentry *child)
  117. {
  118. struct jffs2_inode_info *f;
  119. uint32_t pino;
  120. BUG_ON(!d_is_dir(child));
  121. f = JFFS2_INODE_INFO(d_inode(child));
  122. pino = f->inocache->pino_nlink;
  123. JFFS2_DEBUG("Parent of directory ino #%u is #%u\n",
  124. f->inocache->ino, pino);
  125. return d_obtain_alias(jffs2_iget(d_inode(child)->i_sb, pino));
  126. }
  127. static const struct export_operations jffs2_export_ops = {
  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_override_compr: override default compressor
  136. * Opt_rp_size: size of reserved pool in KiB
  137. * Opt_err: just end of array marker
  138. */
  139. enum {
  140. Opt_override_compr,
  141. Opt_rp_size,
  142. Opt_err,
  143. };
  144. static const match_table_t tokens = {
  145. {Opt_override_compr, "compr=%s"},
  146. {Opt_rp_size, "rp_size=%u"},
  147. {Opt_err, NULL},
  148. };
  149. static int jffs2_parse_options(struct jffs2_sb_info *c, char *data)
  150. {
  151. substring_t args[MAX_OPT_ARGS];
  152. char *p, *name;
  153. unsigned int opt;
  154. if (!data)
  155. return 0;
  156. while ((p = strsep(&data, ","))) {
  157. int token;
  158. if (!*p)
  159. continue;
  160. token = match_token(p, tokens, args);
  161. switch (token) {
  162. case Opt_override_compr:
  163. name = match_strdup(&args[0]);
  164. if (!name)
  165. return -ENOMEM;
  166. if (!strcmp(name, "none"))
  167. c->mount_opts.compr = JFFS2_COMPR_MODE_NONE;
  168. #ifdef CONFIG_JFFS2_LZO
  169. else if (!strcmp(name, "lzo"))
  170. c->mount_opts.compr = JFFS2_COMPR_MODE_FORCELZO;
  171. #endif
  172. #ifdef CONFIG_JFFS2_ZLIB
  173. else if (!strcmp(name, "zlib"))
  174. c->mount_opts.compr =
  175. JFFS2_COMPR_MODE_FORCEZLIB;
  176. #endif
  177. else {
  178. pr_err("Error: unknown compressor \"%s\"\n",
  179. name);
  180. kfree(name);
  181. return -EINVAL;
  182. }
  183. kfree(name);
  184. c->mount_opts.override_compr = true;
  185. break;
  186. case Opt_rp_size:
  187. if (match_int(&args[0], &opt))
  188. return -EINVAL;
  189. opt *= 1024;
  190. if (opt > c->mtd->size) {
  191. pr_warn("Too large reserve pool specified, max "
  192. "is %llu KB\n", c->mtd->size / 1024);
  193. return -EINVAL;
  194. }
  195. c->mount_opts.rp_size = opt;
  196. break;
  197. default:
  198. pr_err("Error: unrecognized mount option '%s' or missing value\n",
  199. p);
  200. return -EINVAL;
  201. }
  202. }
  203. return 0;
  204. }
  205. static int jffs2_remount_fs(struct super_block *sb, int *flags, char *data)
  206. {
  207. struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
  208. int err;
  209. sync_filesystem(sb);
  210. err = jffs2_parse_options(c, data);
  211. if (err)
  212. return -EINVAL;
  213. return jffs2_do_remount_fs(sb, flags, data);
  214. }
  215. static const struct super_operations jffs2_super_operations =
  216. {
  217. .alloc_inode = jffs2_alloc_inode,
  218. .destroy_inode =jffs2_destroy_inode,
  219. .put_super = jffs2_put_super,
  220. .statfs = jffs2_statfs,
  221. .remount_fs = jffs2_remount_fs,
  222. .evict_inode = jffs2_evict_inode,
  223. .dirty_inode = jffs2_dirty_inode,
  224. .show_options = jffs2_show_options,
  225. .sync_fs = jffs2_sync_fs,
  226. };
  227. /*
  228. * fill in the superblock
  229. */
  230. static int jffs2_fill_super(struct super_block *sb, void *data, int silent)
  231. {
  232. struct jffs2_sb_info *c;
  233. int ret;
  234. jffs2_dbg(1, "jffs2_get_sb_mtd():"
  235. " New superblock for device %d (\"%s\")\n",
  236. sb->s_mtd->index, sb->s_mtd->name);
  237. c = kzalloc(sizeof(*c), GFP_KERNEL);
  238. if (!c)
  239. return -ENOMEM;
  240. c->mtd = sb->s_mtd;
  241. c->os_priv = sb;
  242. sb->s_fs_info = c;
  243. ret = jffs2_parse_options(c, data);
  244. if (ret)
  245. return -EINVAL;
  246. /* Initialize JFFS2 superblock locks, the further initialization will
  247. * be done later */
  248. mutex_init(&c->alloc_sem);
  249. mutex_init(&c->erase_free_sem);
  250. init_waitqueue_head(&c->erase_wait);
  251. init_waitqueue_head(&c->inocache_wq);
  252. spin_lock_init(&c->erase_completion_lock);
  253. spin_lock_init(&c->inocache_lock);
  254. sb->s_op = &jffs2_super_operations;
  255. sb->s_export_op = &jffs2_export_ops;
  256. sb->s_flags = sb->s_flags | MS_NOATIME;
  257. sb->s_xattr = jffs2_xattr_handlers;
  258. #ifdef CONFIG_JFFS2_FS_POSIX_ACL
  259. sb->s_flags |= MS_POSIXACL;
  260. #endif
  261. ret = jffs2_do_fill_super(sb, data, silent);
  262. return ret;
  263. }
  264. static struct dentry *jffs2_mount(struct file_system_type *fs_type,
  265. int flags, const char *dev_name,
  266. void *data)
  267. {
  268. return mount_mtd(fs_type, flags, dev_name, data, jffs2_fill_super);
  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. if (jffs2_blocks_use_vmalloc(c))
  281. vfree(c->blocks);
  282. else
  283. kfree(c->blocks);
  284. jffs2_flash_cleanup(c);
  285. kfree(c->inocache_list);
  286. jffs2_clear_xattr_subsystem(c);
  287. mtd_sync(c->mtd);
  288. jffs2_dbg(1, "%s(): returning\n", __func__);
  289. }
  290. static void jffs2_kill_sb(struct super_block *sb)
  291. {
  292. struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
  293. if (c && !(sb->s_flags & MS_RDONLY))
  294. jffs2_stop_garbage_collect_thread(c);
  295. kill_mtd_super(sb);
  296. kfree(c);
  297. }
  298. static struct file_system_type jffs2_fs_type = {
  299. .owner = THIS_MODULE,
  300. .name = "jffs2",
  301. .mount = jffs2_mount,
  302. .kill_sb = jffs2_kill_sb,
  303. };
  304. MODULE_ALIAS_FS("jffs2");
  305. static int __init init_jffs2_fs(void)
  306. {
  307. int ret;
  308. /* Paranoia checks for on-medium structures. If we ask GCC
  309. to pack them with __attribute__((packed)) then it _also_
  310. assumes that they're not aligned -- so it emits crappy
  311. code on some architectures. Ideally we want an attribute
  312. which means just 'no padding', without the alignment
  313. thing. But GCC doesn't have that -- we have to just
  314. hope the structs are the right sizes, instead. */
  315. BUILD_BUG_ON(sizeof(struct jffs2_unknown_node) != 12);
  316. BUILD_BUG_ON(sizeof(struct jffs2_raw_dirent) != 40);
  317. BUILD_BUG_ON(sizeof(struct jffs2_raw_inode) != 68);
  318. BUILD_BUG_ON(sizeof(struct jffs2_raw_summary) != 32);
  319. pr_info("version 2.2."
  320. #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
  321. " (NAND)"
  322. #endif
  323. #ifdef CONFIG_JFFS2_SUMMARY
  324. " (SUMMARY) "
  325. #endif
  326. " © 2001-2006 Red Hat, Inc.\n");
  327. jffs2_inode_cachep = kmem_cache_create("jffs2_i",
  328. sizeof(struct jffs2_inode_info),
  329. 0, (SLAB_RECLAIM_ACCOUNT|
  330. SLAB_MEM_SPREAD),
  331. jffs2_i_init_once);
  332. if (!jffs2_inode_cachep) {
  333. pr_err("error: Failed to initialise inode cache\n");
  334. return -ENOMEM;
  335. }
  336. ret = jffs2_compressors_init();
  337. if (ret) {
  338. pr_err("error: Failed to initialise compressors\n");
  339. goto out;
  340. }
  341. ret = jffs2_create_slab_caches();
  342. if (ret) {
  343. pr_err("error: Failed to initialise slab caches\n");
  344. goto out_compressors;
  345. }
  346. ret = register_filesystem(&jffs2_fs_type);
  347. if (ret) {
  348. pr_err("error: Failed to register filesystem\n");
  349. goto out_slab;
  350. }
  351. return 0;
  352. out_slab:
  353. jffs2_destroy_slab_caches();
  354. out_compressors:
  355. jffs2_compressors_exit();
  356. out:
  357. kmem_cache_destroy(jffs2_inode_cachep);
  358. return ret;
  359. }
  360. static void __exit exit_jffs2_fs(void)
  361. {
  362. unregister_filesystem(&jffs2_fs_type);
  363. jffs2_destroy_slab_caches();
  364. jffs2_compressors_exit();
  365. /*
  366. * Make sure all delayed rcu free inodes are flushed before we
  367. * destroy cache.
  368. */
  369. rcu_barrier();
  370. kmem_cache_destroy(jffs2_inode_cachep);
  371. }
  372. module_init(init_jffs2_fs);
  373. module_exit(exit_jffs2_fs);
  374. MODULE_DESCRIPTION("The Journalling Flash File System, v2");
  375. MODULE_AUTHOR("Red Hat, Inc.");
  376. MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for
  377. // the sake of this tag. It's Free Software.