super.c 10 KB

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