build.c 11 KB

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