build.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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.69 2004/12/16 20:22:18 dmarlin 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_MOUNTING;
  86. ret = jffs2_scan_medium(c);
  87. if (ret)
  88. goto exit;
  89. D1(printk(KERN_DEBUG "Scanned flash completely\n"));
  90. D2(jffs2_dump_block_lists(c));
  91. /* Now scan the directory tree, increasing nlink according to every dirent found. */
  92. for_each_inode(i, c, ic) {
  93. D1(printk(KERN_DEBUG "Pass 1: ino #%u\n", ic->ino));
  94. D1(BUG_ON(ic->ino > c->highest_ino));
  95. if (ic->scan_dents) {
  96. jffs2_build_inode_pass1(c, ic);
  97. cond_resched();
  98. }
  99. }
  100. c->flags &= ~JFFS2_SB_FLAG_MOUNTING;
  101. D1(printk(KERN_DEBUG "Pass 1 complete\n"));
  102. /* Next, scan for inodes with nlink == 0 and remove them. If
  103. they were directories, then decrement the nlink of their
  104. children too, and repeat the scan. As that's going to be
  105. a fairly uncommon occurrence, it's not so evil to do it this
  106. way. Recursion bad. */
  107. D1(printk(KERN_DEBUG "Pass 2 starting\n"));
  108. for_each_inode(i, c, ic) {
  109. D1(printk(KERN_DEBUG "Pass 2: ino #%u, nlink %d, ic %p, nodes %p\n", ic->ino, ic->nlink, ic, ic->nodes));
  110. if (ic->nlink)
  111. continue;
  112. jffs2_build_remove_unlinked_inode(c, ic, &dead_fds);
  113. cond_resched();
  114. }
  115. D1(printk(KERN_DEBUG "Pass 2a starting\n"));
  116. while (dead_fds) {
  117. fd = dead_fds;
  118. dead_fds = fd->next;
  119. ic = jffs2_get_ino_cache(c, fd->ino);
  120. D1(printk(KERN_DEBUG "Removing dead_fd ino #%u (\"%s\"), ic at %p\n", fd->ino, fd->name, ic));
  121. if (ic)
  122. jffs2_build_remove_unlinked_inode(c, ic, &dead_fds);
  123. jffs2_free_full_dirent(fd);
  124. }
  125. D1(printk(KERN_DEBUG "Pass 2 complete\n"));
  126. /* Finally, we can scan again and free the dirent structs */
  127. for_each_inode(i, c, ic) {
  128. D1(printk(KERN_DEBUG "Pass 3: ino #%u, ic %p, nodes %p\n", ic->ino, ic, ic->nodes));
  129. while(ic->scan_dents) {
  130. fd = ic->scan_dents;
  131. ic->scan_dents = fd->next;
  132. jffs2_free_full_dirent(fd);
  133. }
  134. ic->scan_dents = NULL;
  135. cond_resched();
  136. }
  137. D1(printk(KERN_DEBUG "Pass 3 complete\n"));
  138. D2(jffs2_dump_block_lists(c));
  139. /* Rotate the lists by some number to ensure wear levelling */
  140. jffs2_rotate_lists(c);
  141. ret = 0;
  142. exit:
  143. if (ret) {
  144. for_each_inode(i, c, ic) {
  145. while(ic->scan_dents) {
  146. fd = ic->scan_dents;
  147. ic->scan_dents = fd->next;
  148. jffs2_free_full_dirent(fd);
  149. }
  150. }
  151. }
  152. return ret;
  153. }
  154. static void jffs2_build_remove_unlinked_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic, struct jffs2_full_dirent **dead_fds)
  155. {
  156. struct jffs2_raw_node_ref *raw;
  157. struct jffs2_full_dirent *fd;
  158. D1(printk(KERN_DEBUG "JFFS2: Removing ino #%u with nlink == zero.\n", ic->ino));
  159. raw = ic->nodes;
  160. while (raw != (void *)ic) {
  161. struct jffs2_raw_node_ref *next = raw->next_in_ino;
  162. D1(printk(KERN_DEBUG "obsoleting node at 0x%08x\n", ref_offset(raw)));
  163. jffs2_mark_node_obsolete(c, raw);
  164. raw = next;
  165. }
  166. if (ic->scan_dents) {
  167. int whinged = 0;
  168. D1(printk(KERN_DEBUG "Inode #%u was a directory which may have children...\n", ic->ino));
  169. while(ic->scan_dents) {
  170. struct jffs2_inode_cache *child_ic;
  171. fd = ic->scan_dents;
  172. ic->scan_dents = fd->next;
  173. if (!fd->ino) {
  174. /* It's a deletion dirent. Ignore it */
  175. D1(printk(KERN_DEBUG "Child \"%s\" is a deletion dirent, skipping...\n", fd->name));
  176. jffs2_free_full_dirent(fd);
  177. continue;
  178. }
  179. if (!whinged) {
  180. whinged = 1;
  181. printk(KERN_NOTICE "Inode #%u was a directory with children - removing those too...\n", ic->ino);
  182. }
  183. D1(printk(KERN_DEBUG "Removing child \"%s\", ino #%u\n",
  184. fd->name, fd->ino));
  185. child_ic = jffs2_get_ino_cache(c, fd->ino);
  186. if (!child_ic) {
  187. printk(KERN_NOTICE "Cannot remove child \"%s\", ino #%u, because it doesn't exist\n", fd->name, fd->ino);
  188. jffs2_free_full_dirent(fd);
  189. continue;
  190. }
  191. /* Reduce nlink of the child. If it's now zero, stick it on the
  192. dead_fds list to be cleaned up later. Else just free the fd */
  193. child_ic->nlink--;
  194. if (!child_ic->nlink) {
  195. D1(printk(KERN_DEBUG "Inode #%u (\"%s\") has now got zero nlink. Adding to dead_fds list.\n",
  196. fd->ino, fd->name));
  197. fd->next = *dead_fds;
  198. *dead_fds = fd;
  199. } else {
  200. D1(printk(KERN_DEBUG "Inode #%u (\"%s\") has now got nlink %d. Ignoring.\n",
  201. fd->ino, fd->name, child_ic->nlink));
  202. jffs2_free_full_dirent(fd);
  203. }
  204. }
  205. }
  206. /*
  207. We don't delete the inocache from the hash list and free it yet.
  208. The erase code will do that, when all the nodes are completely gone.
  209. */
  210. }
  211. static void jffs2_calc_trigger_levels(struct jffs2_sb_info *c)
  212. {
  213. uint32_t size;
  214. /* Deletion should almost _always_ be allowed. We're fairly
  215. buggered once we stop allowing people to delete stuff
  216. because there's not enough free space... */
  217. c->resv_blocks_deletion = 2;
  218. /* Be conservative about how much space we need before we allow writes.
  219. On top of that which is required for deletia, require an extra 2%
  220. of the medium to be available, for overhead caused by nodes being
  221. split across blocks, etc. */
  222. size = c->flash_size / 50; /* 2% of flash size */
  223. size += c->nr_blocks * 100; /* And 100 bytes per eraseblock */
  224. size += c->sector_size - 1; /* ... and round up */
  225. c->resv_blocks_write = c->resv_blocks_deletion + (size / c->sector_size);
  226. /* When do we let the GC thread run in the background */
  227. c->resv_blocks_gctrigger = c->resv_blocks_write + 1;
  228. /* When do we allow garbage collection to merge nodes to make
  229. long-term progress at the expense of short-term space exhaustion? */
  230. c->resv_blocks_gcmerge = c->resv_blocks_deletion + 1;
  231. /* When do we allow garbage collection to eat from bad blocks rather
  232. than actually making progress? */
  233. c->resv_blocks_gcbad = 0;//c->resv_blocks_deletion + 2;
  234. /* If there's less than this amount of dirty space, don't bother
  235. trying to GC to make more space. It'll be a fruitless task */
  236. c->nospc_dirty_size = c->sector_size + (c->flash_size / 100);
  237. D1(printk(KERN_DEBUG "JFFS2 trigger levels (size %d KiB, block size %d KiB, %d blocks)\n",
  238. c->flash_size / 1024, c->sector_size / 1024, c->nr_blocks));
  239. D1(printk(KERN_DEBUG "Blocks required to allow deletion: %d (%d KiB)\n",
  240. c->resv_blocks_deletion, c->resv_blocks_deletion*c->sector_size/1024));
  241. D1(printk(KERN_DEBUG "Blocks required to allow writes: %d (%d KiB)\n",
  242. c->resv_blocks_write, c->resv_blocks_write*c->sector_size/1024));
  243. D1(printk(KERN_DEBUG "Blocks required to quiesce GC thread: %d (%d KiB)\n",
  244. c->resv_blocks_gctrigger, c->resv_blocks_gctrigger*c->sector_size/1024));
  245. D1(printk(KERN_DEBUG "Blocks required to allow GC merges: %d (%d KiB)\n",
  246. c->resv_blocks_gcmerge, c->resv_blocks_gcmerge*c->sector_size/1024));
  247. D1(printk(KERN_DEBUG "Blocks required to GC bad blocks: %d (%d KiB)\n",
  248. c->resv_blocks_gcbad, c->resv_blocks_gcbad*c->sector_size/1024));
  249. D1(printk(KERN_DEBUG "Amount of dirty space required to GC: %d bytes\n",
  250. c->nospc_dirty_size));
  251. }
  252. int jffs2_do_mount_fs(struct jffs2_sb_info *c)
  253. {
  254. int i;
  255. c->free_size = c->flash_size;
  256. c->nr_blocks = c->flash_size / c->sector_size;
  257. if (c->mtd->flags & MTD_NO_VIRTBLOCKS)
  258. c->blocks = vmalloc(sizeof(struct jffs2_eraseblock) * c->nr_blocks);
  259. else
  260. c->blocks = kmalloc(sizeof(struct jffs2_eraseblock) * c->nr_blocks, GFP_KERNEL);
  261. if (!c->blocks)
  262. return -ENOMEM;
  263. for (i=0; i<c->nr_blocks; i++) {
  264. INIT_LIST_HEAD(&c->blocks[i].list);
  265. c->blocks[i].offset = i * c->sector_size;
  266. c->blocks[i].free_size = c->sector_size;
  267. c->blocks[i].dirty_size = 0;
  268. c->blocks[i].wasted_size = 0;
  269. c->blocks[i].unchecked_size = 0;
  270. c->blocks[i].used_size = 0;
  271. c->blocks[i].first_node = NULL;
  272. c->blocks[i].last_node = NULL;
  273. c->blocks[i].bad_count = 0;
  274. }
  275. init_MUTEX(&c->alloc_sem);
  276. init_MUTEX(&c->erase_free_sem);
  277. init_waitqueue_head(&c->erase_wait);
  278. init_waitqueue_head(&c->inocache_wq);
  279. spin_lock_init(&c->erase_completion_lock);
  280. spin_lock_init(&c->inocache_lock);
  281. INIT_LIST_HEAD(&c->clean_list);
  282. INIT_LIST_HEAD(&c->very_dirty_list);
  283. INIT_LIST_HEAD(&c->dirty_list);
  284. INIT_LIST_HEAD(&c->erasable_list);
  285. INIT_LIST_HEAD(&c->erasing_list);
  286. INIT_LIST_HEAD(&c->erase_pending_list);
  287. INIT_LIST_HEAD(&c->erasable_pending_wbuf_list);
  288. INIT_LIST_HEAD(&c->erase_complete_list);
  289. INIT_LIST_HEAD(&c->free_list);
  290. INIT_LIST_HEAD(&c->bad_list);
  291. INIT_LIST_HEAD(&c->bad_used_list);
  292. c->highest_ino = 1;
  293. if (jffs2_build_filesystem(c)) {
  294. D1(printk(KERN_DEBUG "build_fs failed\n"));
  295. jffs2_free_ino_caches(c);
  296. jffs2_free_raw_node_refs(c);
  297. if (c->mtd->flags & MTD_NO_VIRTBLOCKS) {
  298. vfree(c->blocks);
  299. } else {
  300. kfree(c->blocks);
  301. }
  302. return -EIO;
  303. }
  304. jffs2_calc_trigger_levels(c);
  305. return 0;
  306. }