super.c 10 KB

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