build.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. * Copyright © 2001-2007 Red Hat, Inc.
  5. * Copyright © 2004-2010 David Woodhouse <dwmw2@infradead.org>
  6. *
  7. * Created by David Woodhouse <dwmw2@infradead.org>
  8. *
  9. * For licensing information, see the file 'LICENCE' in this directory.
  10. *
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/kernel.h>
  14. #include <linux/sched.h>
  15. #include <linux/slab.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/mtd/mtd.h>
  18. #include <linux/mm.h> /* kvfree() */
  19. #include "nodelist.h"
  20. static void jffs2_build_remove_unlinked_inode(struct jffs2_sb_info *,
  21. struct jffs2_inode_cache *, struct jffs2_full_dirent **);
  22. static inline struct jffs2_inode_cache *
  23. first_inode_chain(int *i, struct jffs2_sb_info *c)
  24. {
  25. for (; *i < c->inocache_hashsize; (*i)++) {
  26. if (c->inocache_list[*i])
  27. return c->inocache_list[*i];
  28. }
  29. return NULL;
  30. }
  31. static inline struct jffs2_inode_cache *
  32. next_inode(int *i, struct jffs2_inode_cache *ic, struct jffs2_sb_info *c)
  33. {
  34. /* More in this chain? */
  35. if (ic->next)
  36. return ic->next;
  37. (*i)++;
  38. return first_inode_chain(i, c);
  39. }
  40. #define for_each_inode(i, c, ic) \
  41. for (i = 0, ic = first_inode_chain(&i, (c)); \
  42. ic; \
  43. ic = next_inode(&i, ic, (c)))
  44. static void jffs2_build_inode_pass1(struct jffs2_sb_info *c,
  45. struct jffs2_inode_cache *ic)
  46. {
  47. struct jffs2_full_dirent *fd;
  48. dbg_fsbuild("building directory inode #%u\n", ic->ino);
  49. /* For each child, increase nlink */
  50. for(fd = ic->scan_dents; fd; fd = fd->next) {
  51. struct jffs2_inode_cache *child_ic;
  52. if (!fd->ino)
  53. continue;
  54. /* we can get high latency here with huge directories */
  55. child_ic = jffs2_get_ino_cache(c, fd->ino);
  56. if (!child_ic) {
  57. dbg_fsbuild("child \"%s\" (ino #%u) of dir ino #%u doesn't exist!\n",
  58. fd->name, fd->ino, ic->ino);
  59. jffs2_mark_node_obsolete(c, fd->raw);
  60. continue;
  61. }
  62. if (fd->type == DT_DIR) {
  63. if (child_ic->pino_nlink) {
  64. JFFS2_ERROR("child dir \"%s\" (ino #%u) of dir ino #%u appears to be a hard link\n",
  65. fd->name, fd->ino, ic->ino);
  66. /* TODO: What do we do about it? */
  67. } else {
  68. child_ic->pino_nlink = ic->ino;
  69. }
  70. } else
  71. child_ic->pino_nlink++;
  72. dbg_fsbuild("increased nlink for child \"%s\" (ino #%u)\n", fd->name, fd->ino);
  73. /* Can't free scan_dents so far. We might need them in pass 2 */
  74. }
  75. }
  76. /* Scan plan:
  77. - Scan physical nodes. Build map of inodes/dirents. Allocate inocaches as we go
  78. - Scan directory tree from top down, setting nlink in inocaches
  79. - Scan inocaches for inodes with nlink==0
  80. */
  81. static int jffs2_build_filesystem(struct jffs2_sb_info *c)
  82. {
  83. int ret;
  84. int i;
  85. struct jffs2_inode_cache *ic;
  86. struct jffs2_full_dirent *fd;
  87. struct jffs2_full_dirent *dead_fds = NULL;
  88. dbg_fsbuild("build FS data structures\n");
  89. /* First, scan the medium and build all the inode caches with
  90. lists of physical nodes */
  91. c->flags |= JFFS2_SB_FLAG_SCANNING;
  92. ret = jffs2_scan_medium(c);
  93. c->flags &= ~JFFS2_SB_FLAG_SCANNING;
  94. if (ret)
  95. goto exit;
  96. dbg_fsbuild("scanned flash completely\n");
  97. jffs2_dbg_dump_block_lists_nolock(c);
  98. dbg_fsbuild("pass 1 starting\n");
  99. c->flags |= JFFS2_SB_FLAG_BUILDING;
  100. /* Now scan the directory tree, increasing nlink according to every dirent found. */
  101. for_each_inode(i, c, ic) {
  102. if (ic->scan_dents) {
  103. jffs2_build_inode_pass1(c, ic);
  104. cond_resched();
  105. }
  106. }
  107. dbg_fsbuild("pass 1 complete\n");
  108. /* Next, scan for inodes with nlink == 0 and remove them. If
  109. they were directories, then decrement the nlink of their
  110. children too, and repeat the scan. As that's going to be
  111. a fairly uncommon occurrence, it's not so evil to do it this
  112. way. Recursion bad. */
  113. dbg_fsbuild("pass 2 starting\n");
  114. for_each_inode(i, c, ic) {
  115. if (ic->pino_nlink)
  116. continue;
  117. jffs2_build_remove_unlinked_inode(c, ic, &dead_fds);
  118. cond_resched();
  119. }
  120. dbg_fsbuild("pass 2a starting\n");
  121. while (dead_fds) {
  122. fd = dead_fds;
  123. dead_fds = fd->next;
  124. ic = jffs2_get_ino_cache(c, fd->ino);
  125. if (ic)
  126. jffs2_build_remove_unlinked_inode(c, ic, &dead_fds);
  127. jffs2_free_full_dirent(fd);
  128. }
  129. dbg_fsbuild("pass 2a complete\n");
  130. dbg_fsbuild("freeing temporary data structures\n");
  131. /* Finally, we can scan again and free the dirent structs */
  132. for_each_inode(i, c, ic) {
  133. while(ic->scan_dents) {
  134. fd = ic->scan_dents;
  135. ic->scan_dents = fd->next;
  136. jffs2_free_full_dirent(fd);
  137. }
  138. ic->scan_dents = NULL;
  139. cond_resched();
  140. }
  141. jffs2_build_xattr_subsystem(c);
  142. c->flags &= ~JFFS2_SB_FLAG_BUILDING;
  143. dbg_fsbuild("FS build complete\n");
  144. /* Rotate the lists by some number to ensure wear levelling */
  145. jffs2_rotate_lists(c);
  146. ret = 0;
  147. exit:
  148. if (ret) {
  149. for_each_inode(i, c, ic) {
  150. while(ic->scan_dents) {
  151. fd = ic->scan_dents;
  152. ic->scan_dents = fd->next;
  153. jffs2_free_full_dirent(fd);
  154. }
  155. }
  156. jffs2_clear_xattr_subsystem(c);
  157. }
  158. return ret;
  159. }
  160. static void jffs2_build_remove_unlinked_inode(struct jffs2_sb_info *c,
  161. struct jffs2_inode_cache *ic,
  162. struct jffs2_full_dirent **dead_fds)
  163. {
  164. struct jffs2_raw_node_ref *raw;
  165. struct jffs2_full_dirent *fd;
  166. dbg_fsbuild("removing ino #%u with nlink == zero.\n", ic->ino);
  167. raw = ic->nodes;
  168. while (raw != (void *)ic) {
  169. struct jffs2_raw_node_ref *next = raw->next_in_ino;
  170. dbg_fsbuild("obsoleting node at 0x%08x\n", ref_offset(raw));
  171. jffs2_mark_node_obsolete(c, raw);
  172. raw = next;
  173. }
  174. if (ic->scan_dents) {
  175. int whinged = 0;
  176. dbg_fsbuild("inode #%u was a directory which may have children...\n", ic->ino);
  177. while(ic->scan_dents) {
  178. struct jffs2_inode_cache *child_ic;
  179. fd = ic->scan_dents;
  180. ic->scan_dents = fd->next;
  181. if (!fd->ino) {
  182. /* It's a deletion dirent. Ignore it */
  183. dbg_fsbuild("child \"%s\" is a deletion dirent, skipping...\n", fd->name);
  184. jffs2_free_full_dirent(fd);
  185. continue;
  186. }
  187. if (!whinged)
  188. whinged = 1;
  189. dbg_fsbuild("removing child \"%s\", ino #%u\n", fd->name, fd->ino);
  190. child_ic = jffs2_get_ino_cache(c, fd->ino);
  191. if (!child_ic) {
  192. dbg_fsbuild("cannot remove child \"%s\", ino #%u, because it doesn't exist\n",
  193. fd->name, fd->ino);
  194. jffs2_free_full_dirent(fd);
  195. continue;
  196. }
  197. /* Reduce nlink of the child. If it's now zero, stick it on the
  198. dead_fds list to be cleaned up later. Else just free the fd */
  199. if (fd->type == DT_DIR)
  200. child_ic->pino_nlink = 0;
  201. else
  202. child_ic->pino_nlink--;
  203. if (!child_ic->pino_nlink) {
  204. dbg_fsbuild("inode #%u (\"%s\") now has no links; adding to dead_fds list.\n",
  205. fd->ino, fd->name);
  206. fd->next = *dead_fds;
  207. *dead_fds = fd;
  208. } else {
  209. dbg_fsbuild("inode #%u (\"%s\") has now got nlink %d. Ignoring.\n",
  210. fd->ino, fd->name, child_ic->pino_nlink);
  211. jffs2_free_full_dirent(fd);
  212. }
  213. }
  214. }
  215. /*
  216. We don't delete the inocache from the hash list and free it yet.
  217. The erase code will do that, when all the nodes are completely gone.
  218. */
  219. }
  220. static void jffs2_calc_trigger_levels(struct jffs2_sb_info *c)
  221. {
  222. uint32_t size;
  223. /* Deletion should almost _always_ be allowed. We're fairly
  224. buggered once we stop allowing people to delete stuff
  225. because there's not enough free space... */
  226. c->resv_blocks_deletion = 2;
  227. /* Be conservative about how much space we need before we allow writes.
  228. On top of that which is required for deletia, require an extra 2%
  229. of the medium to be available, for overhead caused by nodes being
  230. split across blocks, etc. */
  231. size = c->flash_size / 50; /* 2% of flash size */
  232. size += c->nr_blocks * 100; /* And 100 bytes per eraseblock */
  233. size += c->sector_size - 1; /* ... and round up */
  234. c->resv_blocks_write = c->resv_blocks_deletion + (size / c->sector_size);
  235. /* When do we let the GC thread run in the background */
  236. c->resv_blocks_gctrigger = c->resv_blocks_write + 1;
  237. /* When do we allow garbage collection to merge nodes to make
  238. long-term progress at the expense of short-term space exhaustion? */
  239. c->resv_blocks_gcmerge = c->resv_blocks_deletion + 1;
  240. /* When do we allow garbage collection to eat from bad blocks rather
  241. than actually making progress? */
  242. c->resv_blocks_gcbad = 0;//c->resv_blocks_deletion + 2;
  243. /* What number of 'very dirty' eraseblocks do we allow before we
  244. trigger the GC thread even if we don't _need_ the space. When we
  245. can't mark nodes obsolete on the medium, the old dirty nodes cause
  246. performance problems because we have to inspect and discard them. */
  247. c->vdirty_blocks_gctrigger = c->resv_blocks_gctrigger;
  248. if (jffs2_can_mark_obsolete(c))
  249. c->vdirty_blocks_gctrigger *= 10;
  250. /* If there's less than this amount of dirty space, don't bother
  251. trying to GC to make more space. It'll be a fruitless task */
  252. c->nospc_dirty_size = c->sector_size + (c->flash_size / 100);
  253. dbg_fsbuild("trigger levels (size %d KiB, block size %d KiB, %d blocks)\n",
  254. c->flash_size / 1024, c->sector_size / 1024, c->nr_blocks);
  255. dbg_fsbuild("Blocks required to allow deletion: %d (%d KiB)\n",
  256. c->resv_blocks_deletion, c->resv_blocks_deletion*c->sector_size/1024);
  257. dbg_fsbuild("Blocks required to allow writes: %d (%d KiB)\n",
  258. c->resv_blocks_write, c->resv_blocks_write*c->sector_size/1024);
  259. dbg_fsbuild("Blocks required to quiesce GC thread: %d (%d KiB)\n",
  260. c->resv_blocks_gctrigger, c->resv_blocks_gctrigger*c->sector_size/1024);
  261. dbg_fsbuild("Blocks required to allow GC merges: %d (%d KiB)\n",
  262. c->resv_blocks_gcmerge, c->resv_blocks_gcmerge*c->sector_size/1024);
  263. dbg_fsbuild("Blocks required to GC bad blocks: %d (%d KiB)\n",
  264. c->resv_blocks_gcbad, c->resv_blocks_gcbad*c->sector_size/1024);
  265. dbg_fsbuild("Amount of dirty space required to GC: %d bytes\n",
  266. c->nospc_dirty_size);
  267. dbg_fsbuild("Very dirty blocks before GC triggered: %d\n",
  268. c->vdirty_blocks_gctrigger);
  269. }
  270. int jffs2_do_mount_fs(struct jffs2_sb_info *c)
  271. {
  272. int ret;
  273. int i;
  274. int size;
  275. c->free_size = c->flash_size;
  276. c->nr_blocks = c->flash_size / c->sector_size;
  277. size = sizeof(struct jffs2_eraseblock) * c->nr_blocks;
  278. #ifndef __ECOS
  279. if (jffs2_blocks_use_vmalloc(c))
  280. c->blocks = vzalloc(size);
  281. else
  282. #endif
  283. c->blocks = kzalloc(size, GFP_KERNEL);
  284. if (!c->blocks)
  285. return -ENOMEM;
  286. for (i=0; i<c->nr_blocks; i++) {
  287. INIT_LIST_HEAD(&c->blocks[i].list);
  288. c->blocks[i].offset = i * c->sector_size;
  289. c->blocks[i].free_size = c->sector_size;
  290. }
  291. INIT_LIST_HEAD(&c->clean_list);
  292. INIT_LIST_HEAD(&c->very_dirty_list);
  293. INIT_LIST_HEAD(&c->dirty_list);
  294. INIT_LIST_HEAD(&c->erasable_list);
  295. INIT_LIST_HEAD(&c->erasing_list);
  296. INIT_LIST_HEAD(&c->erase_checking_list);
  297. INIT_LIST_HEAD(&c->erase_pending_list);
  298. INIT_LIST_HEAD(&c->erasable_pending_wbuf_list);
  299. INIT_LIST_HEAD(&c->erase_complete_list);
  300. INIT_LIST_HEAD(&c->free_list);
  301. INIT_LIST_HEAD(&c->bad_list);
  302. INIT_LIST_HEAD(&c->bad_used_list);
  303. c->highest_ino = 1;
  304. c->summary = NULL;
  305. ret = jffs2_sum_init(c);
  306. if (ret)
  307. goto out_free;
  308. if (jffs2_build_filesystem(c)) {
  309. dbg_fsbuild("build_fs failed\n");
  310. jffs2_free_ino_caches(c);
  311. jffs2_free_raw_node_refs(c);
  312. ret = -EIO;
  313. goto out_free;
  314. }
  315. jffs2_calc_trigger_levels(c);
  316. return 0;
  317. out_free:
  318. kvfree(c->blocks);
  319. return ret;
  320. }