nodemgmt.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  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: nodemgmt.c,v 1.122 2005/05/06 09:30:27 dedekind Exp $
  11. *
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/mtd/mtd.h>
  16. #include <linux/compiler.h>
  17. #include <linux/sched.h> /* For cond_resched() */
  18. #include "nodelist.h"
  19. /**
  20. * jffs2_reserve_space - request physical space to write nodes to flash
  21. * @c: superblock info
  22. * @minsize: Minimum acceptable size of allocation
  23. * @ofs: Returned value of node offset
  24. * @len: Returned value of allocation length
  25. * @prio: Allocation type - ALLOC_{NORMAL,DELETION}
  26. *
  27. * Requests a block of physical space on the flash. Returns zero for success
  28. * and puts 'ofs' and 'len' into the appriopriate place, or returns -ENOSPC
  29. * or other error if appropriate.
  30. *
  31. * If it returns zero, jffs2_reserve_space() also downs the per-filesystem
  32. * allocation semaphore, to prevent more than one allocation from being
  33. * active at any time. The semaphore is later released by jffs2_commit_allocation()
  34. *
  35. * jffs2_reserve_space() may trigger garbage collection in order to make room
  36. * for the requested allocation.
  37. */
  38. static int jffs2_do_reserve_space(struct jffs2_sb_info *c, uint32_t minsize, uint32_t *ofs, uint32_t *len);
  39. int jffs2_reserve_space(struct jffs2_sb_info *c, uint32_t minsize, uint32_t *ofs, uint32_t *len, int prio)
  40. {
  41. int ret = -EAGAIN;
  42. int blocksneeded = c->resv_blocks_write;
  43. /* align it */
  44. minsize = PAD(minsize);
  45. D1(printk(KERN_DEBUG "jffs2_reserve_space(): Requested 0x%x bytes\n", minsize));
  46. down(&c->alloc_sem);
  47. D1(printk(KERN_DEBUG "jffs2_reserve_space(): alloc sem got\n"));
  48. spin_lock(&c->erase_completion_lock);
  49. /* this needs a little more thought (true <tglx> :)) */
  50. while(ret == -EAGAIN) {
  51. while(c->nr_free_blocks + c->nr_erasing_blocks < blocksneeded) {
  52. int ret;
  53. uint32_t dirty, avail;
  54. /* calculate real dirty size
  55. * dirty_size contains blocks on erase_pending_list
  56. * those blocks are counted in c->nr_erasing_blocks.
  57. * If one block is actually erased, it is not longer counted as dirty_space
  58. * but it is counted in c->nr_erasing_blocks, so we add it and subtract it
  59. * with c->nr_erasing_blocks * c->sector_size again.
  60. * Blocks on erasable_list are counted as dirty_size, but not in c->nr_erasing_blocks
  61. * This helps us to force gc and pick eventually a clean block to spread the load.
  62. * We add unchecked_size here, as we hopefully will find some space to use.
  63. * This will affect the sum only once, as gc first finishes checking
  64. * of nodes.
  65. */
  66. dirty = c->dirty_size + c->erasing_size - c->nr_erasing_blocks * c->sector_size + c->unchecked_size;
  67. if (dirty < c->nospc_dirty_size) {
  68. if (prio == ALLOC_DELETION && c->nr_free_blocks + c->nr_erasing_blocks >= c->resv_blocks_deletion) {
  69. D1(printk(KERN_NOTICE "jffs2_reserve_space(): Low on dirty space to GC, but it's a deletion. Allowing...\n"));
  70. break;
  71. }
  72. D1(printk(KERN_DEBUG "dirty size 0x%08x + unchecked_size 0x%08x < nospc_dirty_size 0x%08x, returning -ENOSPC\n",
  73. dirty, c->unchecked_size, c->sector_size));
  74. spin_unlock(&c->erase_completion_lock);
  75. up(&c->alloc_sem);
  76. return -ENOSPC;
  77. }
  78. /* Calc possibly available space. Possibly available means that we
  79. * don't know, if unchecked size contains obsoleted nodes, which could give us some
  80. * more usable space. This will affect the sum only once, as gc first finishes checking
  81. * of nodes.
  82. + Return -ENOSPC, if the maximum possibly available space is less or equal than
  83. * blocksneeded * sector_size.
  84. * This blocks endless gc looping on a filesystem, which is nearly full, even if
  85. * the check above passes.
  86. */
  87. avail = c->free_size + c->dirty_size + c->erasing_size + c->unchecked_size;
  88. if ( (avail / c->sector_size) <= blocksneeded) {
  89. if (prio == ALLOC_DELETION && c->nr_free_blocks + c->nr_erasing_blocks >= c->resv_blocks_deletion) {
  90. D1(printk(KERN_NOTICE "jffs2_reserve_space(): Low on possibly available space, but it's a deletion. Allowing...\n"));
  91. break;
  92. }
  93. D1(printk(KERN_DEBUG "max. available size 0x%08x < blocksneeded * sector_size 0x%08x, returning -ENOSPC\n",
  94. avail, blocksneeded * c->sector_size));
  95. spin_unlock(&c->erase_completion_lock);
  96. up(&c->alloc_sem);
  97. return -ENOSPC;
  98. }
  99. up(&c->alloc_sem);
  100. D1(printk(KERN_DEBUG "Triggering GC pass. nr_free_blocks %d, nr_erasing_blocks %d, free_size 0x%08x, dirty_size 0x%08x, wasted_size 0x%08x, used_size 0x%08x, erasing_size 0x%08x, bad_size 0x%08x (total 0x%08x of 0x%08x)\n",
  101. c->nr_free_blocks, c->nr_erasing_blocks, c->free_size, c->dirty_size, c->wasted_size, c->used_size, c->erasing_size, c->bad_size,
  102. c->free_size + c->dirty_size + c->wasted_size + c->used_size + c->erasing_size + c->bad_size, c->flash_size));
  103. spin_unlock(&c->erase_completion_lock);
  104. ret = jffs2_garbage_collect_pass(c);
  105. if (ret)
  106. return ret;
  107. cond_resched();
  108. if (signal_pending(current))
  109. return -EINTR;
  110. down(&c->alloc_sem);
  111. spin_lock(&c->erase_completion_lock);
  112. }
  113. ret = jffs2_do_reserve_space(c, minsize, ofs, len);
  114. if (ret) {
  115. D1(printk(KERN_DEBUG "jffs2_reserve_space: ret is %d\n", ret));
  116. }
  117. }
  118. spin_unlock(&c->erase_completion_lock);
  119. if (ret)
  120. up(&c->alloc_sem);
  121. return ret;
  122. }
  123. int jffs2_reserve_space_gc(struct jffs2_sb_info *c, uint32_t minsize, uint32_t *ofs, uint32_t *len)
  124. {
  125. int ret = -EAGAIN;
  126. minsize = PAD(minsize);
  127. D1(printk(KERN_DEBUG "jffs2_reserve_space_gc(): Requested 0x%x bytes\n", minsize));
  128. spin_lock(&c->erase_completion_lock);
  129. while(ret == -EAGAIN) {
  130. ret = jffs2_do_reserve_space(c, minsize, ofs, len);
  131. if (ret) {
  132. D1(printk(KERN_DEBUG "jffs2_reserve_space_gc: looping, ret is %d\n", ret));
  133. }
  134. }
  135. spin_unlock(&c->erase_completion_lock);
  136. return ret;
  137. }
  138. /* Called with alloc sem _and_ erase_completion_lock */
  139. static int jffs2_do_reserve_space(struct jffs2_sb_info *c, uint32_t minsize, uint32_t *ofs, uint32_t *len)
  140. {
  141. struct jffs2_eraseblock *jeb = c->nextblock;
  142. restart:
  143. if (jeb && minsize > jeb->free_size) {
  144. /* Skip the end of this block and file it as having some dirty space */
  145. /* If there's a pending write to it, flush now */
  146. if (jffs2_wbuf_dirty(c)) {
  147. spin_unlock(&c->erase_completion_lock);
  148. D1(printk(KERN_DEBUG "jffs2_do_reserve_space: Flushing write buffer\n"));
  149. jffs2_flush_wbuf_pad(c);
  150. spin_lock(&c->erase_completion_lock);
  151. jeb = c->nextblock;
  152. goto restart;
  153. }
  154. c->wasted_size += jeb->free_size;
  155. c->free_size -= jeb->free_size;
  156. jeb->wasted_size += jeb->free_size;
  157. jeb->free_size = 0;
  158. /* Check, if we have a dirty block now, or if it was dirty already */
  159. if (ISDIRTY (jeb->wasted_size + jeb->dirty_size)) {
  160. c->dirty_size += jeb->wasted_size;
  161. c->wasted_size -= jeb->wasted_size;
  162. jeb->dirty_size += jeb->wasted_size;
  163. jeb->wasted_size = 0;
  164. if (VERYDIRTY(c, jeb->dirty_size)) {
  165. D1(printk(KERN_DEBUG "Adding full erase block at 0x%08x to very_dirty_list (free 0x%08x, dirty 0x%08x, used 0x%08x\n",
  166. jeb->offset, jeb->free_size, jeb->dirty_size, jeb->used_size));
  167. list_add_tail(&jeb->list, &c->very_dirty_list);
  168. } else {
  169. D1(printk(KERN_DEBUG "Adding full erase block at 0x%08x to dirty_list (free 0x%08x, dirty 0x%08x, used 0x%08x\n",
  170. jeb->offset, jeb->free_size, jeb->dirty_size, jeb->used_size));
  171. list_add_tail(&jeb->list, &c->dirty_list);
  172. }
  173. } else {
  174. D1(printk(KERN_DEBUG "Adding full erase block at 0x%08x to clean_list (free 0x%08x, dirty 0x%08x, used 0x%08x\n",
  175. jeb->offset, jeb->free_size, jeb->dirty_size, jeb->used_size));
  176. list_add_tail(&jeb->list, &c->clean_list);
  177. }
  178. c->nextblock = jeb = NULL;
  179. }
  180. if (!jeb) {
  181. struct list_head *next;
  182. /* Take the next block off the 'free' list */
  183. if (list_empty(&c->free_list)) {
  184. if (!c->nr_erasing_blocks &&
  185. !list_empty(&c->erasable_list)) {
  186. struct jffs2_eraseblock *ejeb;
  187. ejeb = list_entry(c->erasable_list.next, struct jffs2_eraseblock, list);
  188. list_del(&ejeb->list);
  189. list_add_tail(&ejeb->list, &c->erase_pending_list);
  190. c->nr_erasing_blocks++;
  191. jffs2_erase_pending_trigger(c);
  192. D1(printk(KERN_DEBUG "jffs2_do_reserve_space: Triggering erase of erasable block at 0x%08x\n",
  193. ejeb->offset));
  194. }
  195. if (!c->nr_erasing_blocks &&
  196. !list_empty(&c->erasable_pending_wbuf_list)) {
  197. D1(printk(KERN_DEBUG "jffs2_do_reserve_space: Flushing write buffer\n"));
  198. /* c->nextblock is NULL, no update to c->nextblock allowed */
  199. spin_unlock(&c->erase_completion_lock);
  200. jffs2_flush_wbuf_pad(c);
  201. spin_lock(&c->erase_completion_lock);
  202. /* Have another go. It'll be on the erasable_list now */
  203. return -EAGAIN;
  204. }
  205. if (!c->nr_erasing_blocks) {
  206. /* Ouch. We're in GC, or we wouldn't have got here.
  207. And there's no space left. At all. */
  208. printk(KERN_CRIT "Argh. No free space left for GC. nr_erasing_blocks is %d. nr_free_blocks is %d. (erasableempty: %s, erasingempty: %s, erasependingempty: %s)\n",
  209. c->nr_erasing_blocks, c->nr_free_blocks, list_empty(&c->erasable_list)?"yes":"no",
  210. list_empty(&c->erasing_list)?"yes":"no", list_empty(&c->erase_pending_list)?"yes":"no");
  211. return -ENOSPC;
  212. }
  213. spin_unlock(&c->erase_completion_lock);
  214. /* Don't wait for it; just erase one right now */
  215. jffs2_erase_pending_blocks(c, 1);
  216. spin_lock(&c->erase_completion_lock);
  217. /* An erase may have failed, decreasing the
  218. amount of free space available. So we must
  219. restart from the beginning */
  220. return -EAGAIN;
  221. }
  222. next = c->free_list.next;
  223. list_del(next);
  224. c->nextblock = jeb = list_entry(next, struct jffs2_eraseblock, list);
  225. c->nr_free_blocks--;
  226. if (jeb->free_size != c->sector_size - c->cleanmarker_size) {
  227. printk(KERN_WARNING "Eep. Block 0x%08x taken from free_list had free_size of 0x%08x!!\n", jeb->offset, jeb->free_size);
  228. goto restart;
  229. }
  230. }
  231. /* OK, jeb (==c->nextblock) is now pointing at a block which definitely has
  232. enough space */
  233. *ofs = jeb->offset + (c->sector_size - jeb->free_size);
  234. *len = jeb->free_size;
  235. if (c->cleanmarker_size && jeb->used_size == c->cleanmarker_size &&
  236. !jeb->first_node->next_in_ino) {
  237. /* Only node in it beforehand was a CLEANMARKER node (we think).
  238. So mark it obsolete now that there's going to be another node
  239. in the block. This will reduce used_size to zero but We've
  240. already set c->nextblock so that jffs2_mark_node_obsolete()
  241. won't try to refile it to the dirty_list.
  242. */
  243. spin_unlock(&c->erase_completion_lock);
  244. jffs2_mark_node_obsolete(c, jeb->first_node);
  245. spin_lock(&c->erase_completion_lock);
  246. }
  247. D1(printk(KERN_DEBUG "jffs2_do_reserve_space(): Giving 0x%x bytes at 0x%x\n", *len, *ofs));
  248. return 0;
  249. }
  250. /**
  251. * jffs2_add_physical_node_ref - add a physical node reference to the list
  252. * @c: superblock info
  253. * @new: new node reference to add
  254. * @len: length of this physical node
  255. * @dirty: dirty flag for new node
  256. *
  257. * Should only be used to report nodes for which space has been allocated
  258. * by jffs2_reserve_space.
  259. *
  260. * Must be called with the alloc_sem held.
  261. */
  262. int jffs2_add_physical_node_ref(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *new)
  263. {
  264. struct jffs2_eraseblock *jeb;
  265. uint32_t len;
  266. jeb = &c->blocks[new->flash_offset / c->sector_size];
  267. len = ref_totlen(c, jeb, new);
  268. D1(printk(KERN_DEBUG "jffs2_add_physical_node_ref(): Node at 0x%x(%d), size 0x%x\n", ref_offset(new), ref_flags(new), len));
  269. #if 1
  270. /* we could get some obsolete nodes after nextblock was refiled
  271. in wbuf.c */
  272. if ((c->nextblock || !ref_obsolete(new))
  273. &&(jeb != c->nextblock || ref_offset(new) != jeb->offset + (c->sector_size - jeb->free_size))) {
  274. printk(KERN_WARNING "argh. node added in wrong place\n");
  275. jffs2_free_raw_node_ref(new);
  276. return -EINVAL;
  277. }
  278. #endif
  279. spin_lock(&c->erase_completion_lock);
  280. if (!jeb->first_node)
  281. jeb->first_node = new;
  282. if (jeb->last_node)
  283. jeb->last_node->next_phys = new;
  284. jeb->last_node = new;
  285. jeb->free_size -= len;
  286. c->free_size -= len;
  287. if (ref_obsolete(new)) {
  288. jeb->dirty_size += len;
  289. c->dirty_size += len;
  290. } else {
  291. jeb->used_size += len;
  292. c->used_size += len;
  293. }
  294. if (!jeb->free_size && !jeb->dirty_size && !ISDIRTY(jeb->wasted_size)) {
  295. /* If it lives on the dirty_list, jffs2_reserve_space will put it there */
  296. D1(printk(KERN_DEBUG "Adding full erase block at 0x%08x to clean_list (free 0x%08x, dirty 0x%08x, used 0x%08x\n",
  297. jeb->offset, jeb->free_size, jeb->dirty_size, jeb->used_size));
  298. if (jffs2_wbuf_dirty(c)) {
  299. /* Flush the last write in the block if it's outstanding */
  300. spin_unlock(&c->erase_completion_lock);
  301. jffs2_flush_wbuf_pad(c);
  302. spin_lock(&c->erase_completion_lock);
  303. }
  304. list_add_tail(&jeb->list, &c->clean_list);
  305. c->nextblock = NULL;
  306. }
  307. ACCT_SANITY_CHECK(c,jeb);
  308. D1(ACCT_PARANOIA_CHECK(jeb));
  309. spin_unlock(&c->erase_completion_lock);
  310. return 0;
  311. }
  312. void jffs2_complete_reservation(struct jffs2_sb_info *c)
  313. {
  314. D1(printk(KERN_DEBUG "jffs2_complete_reservation()\n"));
  315. jffs2_garbage_collect_trigger(c);
  316. up(&c->alloc_sem);
  317. }
  318. static inline int on_list(struct list_head *obj, struct list_head *head)
  319. {
  320. struct list_head *this;
  321. list_for_each(this, head) {
  322. if (this == obj) {
  323. D1(printk("%p is on list at %p\n", obj, head));
  324. return 1;
  325. }
  326. }
  327. return 0;
  328. }
  329. void jffs2_mark_node_obsolete(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref)
  330. {
  331. struct jffs2_eraseblock *jeb;
  332. int blocknr;
  333. struct jffs2_unknown_node n;
  334. int ret, addedsize;
  335. size_t retlen;
  336. if(!ref) {
  337. printk(KERN_NOTICE "EEEEEK. jffs2_mark_node_obsolete called with NULL node\n");
  338. return;
  339. }
  340. if (ref_obsolete(ref)) {
  341. D1(printk(KERN_DEBUG "jffs2_mark_node_obsolete called with already obsolete node at 0x%08x\n", ref_offset(ref)));
  342. return;
  343. }
  344. blocknr = ref->flash_offset / c->sector_size;
  345. if (blocknr >= c->nr_blocks) {
  346. printk(KERN_NOTICE "raw node at 0x%08x is off the end of device!\n", ref->flash_offset);
  347. BUG();
  348. }
  349. jeb = &c->blocks[blocknr];
  350. if (jffs2_can_mark_obsolete(c) && !jffs2_is_readonly(c) &&
  351. !(c->flags & (JFFS2_SB_FLAG_SCANNING | JFFS2_SB_FLAG_BUILDING))) {
  352. /* Hm. This may confuse static lock analysis. If any of the above
  353. three conditions is false, we're going to return from this
  354. function without actually obliterating any nodes or freeing
  355. any jffs2_raw_node_refs. So we don't need to stop erases from
  356. happening, or protect against people holding an obsolete
  357. jffs2_raw_node_ref without the erase_completion_lock. */
  358. down(&c->erase_free_sem);
  359. }
  360. spin_lock(&c->erase_completion_lock);
  361. if (ref_flags(ref) == REF_UNCHECKED) {
  362. D1(if (unlikely(jeb->unchecked_size < ref_totlen(c, jeb, ref))) {
  363. printk(KERN_NOTICE "raw unchecked node of size 0x%08x freed from erase block %d at 0x%08x, but unchecked_size was already 0x%08x\n",
  364. ref_totlen(c, jeb, ref), blocknr, ref->flash_offset, jeb->used_size);
  365. BUG();
  366. })
  367. D1(printk(KERN_DEBUG "Obsoleting previously unchecked node at 0x%08x of len %x: ", ref_offset(ref), ref_totlen(c, jeb, ref)));
  368. jeb->unchecked_size -= ref_totlen(c, jeb, ref);
  369. c->unchecked_size -= ref_totlen(c, jeb, ref);
  370. } else {
  371. D1(if (unlikely(jeb->used_size < ref_totlen(c, jeb, ref))) {
  372. printk(KERN_NOTICE "raw node of size 0x%08x freed from erase block %d at 0x%08x, but used_size was already 0x%08x\n",
  373. ref_totlen(c, jeb, ref), blocknr, ref->flash_offset, jeb->used_size);
  374. BUG();
  375. })
  376. D1(printk(KERN_DEBUG "Obsoleting node at 0x%08x of len %x: ", ref_offset(ref), ref_totlen(c, jeb, ref)));
  377. jeb->used_size -= ref_totlen(c, jeb, ref);
  378. c->used_size -= ref_totlen(c, jeb, ref);
  379. }
  380. // Take care, that wasted size is taken into concern
  381. if ((jeb->dirty_size || ISDIRTY(jeb->wasted_size + ref_totlen(c, jeb, ref))) && jeb != c->nextblock) {
  382. D1(printk(KERN_DEBUG "Dirtying\n"));
  383. addedsize = ref_totlen(c, jeb, ref);
  384. jeb->dirty_size += ref_totlen(c, jeb, ref);
  385. c->dirty_size += ref_totlen(c, jeb, ref);
  386. /* Convert wasted space to dirty, if not a bad block */
  387. if (jeb->wasted_size) {
  388. if (on_list(&jeb->list, &c->bad_used_list)) {
  389. D1(printk(KERN_DEBUG "Leaving block at %08x on the bad_used_list\n",
  390. jeb->offset));
  391. addedsize = 0; /* To fool the refiling code later */
  392. } else {
  393. D1(printk(KERN_DEBUG "Converting %d bytes of wasted space to dirty in block at %08x\n",
  394. jeb->wasted_size, jeb->offset));
  395. addedsize += jeb->wasted_size;
  396. jeb->dirty_size += jeb->wasted_size;
  397. c->dirty_size += jeb->wasted_size;
  398. c->wasted_size -= jeb->wasted_size;
  399. jeb->wasted_size = 0;
  400. }
  401. }
  402. } else {
  403. D1(printk(KERN_DEBUG "Wasting\n"));
  404. addedsize = 0;
  405. jeb->wasted_size += ref_totlen(c, jeb, ref);
  406. c->wasted_size += ref_totlen(c, jeb, ref);
  407. }
  408. ref->flash_offset = ref_offset(ref) | REF_OBSOLETE;
  409. ACCT_SANITY_CHECK(c, jeb);
  410. D1(ACCT_PARANOIA_CHECK(jeb));
  411. if (c->flags & JFFS2_SB_FLAG_SCANNING) {
  412. /* Flash scanning is in progress. Don't muck about with the block
  413. lists because they're not ready yet, and don't actually
  414. obliterate nodes that look obsolete. If they weren't
  415. marked obsolete on the flash at the time they _became_
  416. obsolete, there was probably a reason for that. */
  417. spin_unlock(&c->erase_completion_lock);
  418. /* We didn't lock the erase_free_sem */
  419. return;
  420. }
  421. if (jeb == c->nextblock) {
  422. D2(printk(KERN_DEBUG "Not moving nextblock 0x%08x to dirty/erase_pending list\n", jeb->offset));
  423. } else if (!jeb->used_size && !jeb->unchecked_size) {
  424. if (jeb == c->gcblock) {
  425. D1(printk(KERN_DEBUG "gcblock at 0x%08x completely dirtied. Clearing gcblock...\n", jeb->offset));
  426. c->gcblock = NULL;
  427. } else {
  428. D1(printk(KERN_DEBUG "Eraseblock at 0x%08x completely dirtied. Removing from (dirty?) list...\n", jeb->offset));
  429. list_del(&jeb->list);
  430. }
  431. if (jffs2_wbuf_dirty(c)) {
  432. D1(printk(KERN_DEBUG "...and adding to erasable_pending_wbuf_list\n"));
  433. list_add_tail(&jeb->list, &c->erasable_pending_wbuf_list);
  434. } else {
  435. if (jiffies & 127) {
  436. /* Most of the time, we just erase it immediately. Otherwise we
  437. spend ages scanning it on mount, etc. */
  438. D1(printk(KERN_DEBUG "...and adding to erase_pending_list\n"));
  439. list_add_tail(&jeb->list, &c->erase_pending_list);
  440. c->nr_erasing_blocks++;
  441. jffs2_erase_pending_trigger(c);
  442. } else {
  443. /* Sometimes, however, we leave it elsewhere so it doesn't get
  444. immediately reused, and we spread the load a bit. */
  445. D1(printk(KERN_DEBUG "...and adding to erasable_list\n"));
  446. list_add_tail(&jeb->list, &c->erasable_list);
  447. }
  448. }
  449. D1(printk(KERN_DEBUG "Done OK\n"));
  450. } else if (jeb == c->gcblock) {
  451. D2(printk(KERN_DEBUG "Not moving gcblock 0x%08x to dirty_list\n", jeb->offset));
  452. } else if (ISDIRTY(jeb->dirty_size) && !ISDIRTY(jeb->dirty_size - addedsize)) {
  453. D1(printk(KERN_DEBUG "Eraseblock at 0x%08x is freshly dirtied. Removing from clean list...\n", jeb->offset));
  454. list_del(&jeb->list);
  455. D1(printk(KERN_DEBUG "...and adding to dirty_list\n"));
  456. list_add_tail(&jeb->list, &c->dirty_list);
  457. } else if (VERYDIRTY(c, jeb->dirty_size) &&
  458. !VERYDIRTY(c, jeb->dirty_size - addedsize)) {
  459. D1(printk(KERN_DEBUG "Eraseblock at 0x%08x is now very dirty. Removing from dirty list...\n", jeb->offset));
  460. list_del(&jeb->list);
  461. D1(printk(KERN_DEBUG "...and adding to very_dirty_list\n"));
  462. list_add_tail(&jeb->list, &c->very_dirty_list);
  463. } else {
  464. D1(printk(KERN_DEBUG "Eraseblock at 0x%08x not moved anywhere. (free 0x%08x, dirty 0x%08x, used 0x%08x)\n",
  465. jeb->offset, jeb->free_size, jeb->dirty_size, jeb->used_size));
  466. }
  467. spin_unlock(&c->erase_completion_lock);
  468. if (!jffs2_can_mark_obsolete(c) || jffs2_is_readonly(c) ||
  469. (c->flags & JFFS2_SB_FLAG_BUILDING)) {
  470. /* We didn't lock the erase_free_sem */
  471. return;
  472. }
  473. /* The erase_free_sem is locked, and has been since before we marked the node obsolete
  474. and potentially put its eraseblock onto the erase_pending_list. Thus, we know that
  475. the block hasn't _already_ been erased, and that 'ref' itself hasn't been freed yet
  476. by jffs2_free_all_node_refs() in erase.c. Which is nice. */
  477. D1(printk(KERN_DEBUG "obliterating obsoleted node at 0x%08x\n", ref_offset(ref)));
  478. ret = jffs2_flash_read(c, ref_offset(ref), sizeof(n), &retlen, (char *)&n);
  479. if (ret) {
  480. printk(KERN_WARNING "Read error reading from obsoleted node at 0x%08x: %d\n", ref_offset(ref), ret);
  481. goto out_erase_sem;
  482. }
  483. if (retlen != sizeof(n)) {
  484. printk(KERN_WARNING "Short read from obsoleted node at 0x%08x: %zd\n", ref_offset(ref), retlen);
  485. goto out_erase_sem;
  486. }
  487. if (PAD(je32_to_cpu(n.totlen)) != PAD(ref_totlen(c, jeb, ref))) {
  488. printk(KERN_WARNING "Node totlen on flash (0x%08x) != totlen from node ref (0x%08x)\n", je32_to_cpu(n.totlen), ref_totlen(c, jeb, ref));
  489. goto out_erase_sem;
  490. }
  491. if (!(je16_to_cpu(n.nodetype) & JFFS2_NODE_ACCURATE)) {
  492. D1(printk(KERN_DEBUG "Node at 0x%08x was already marked obsolete (nodetype 0x%04x)\n", ref_offset(ref), je16_to_cpu(n.nodetype)));
  493. goto out_erase_sem;
  494. }
  495. /* XXX FIXME: This is ugly now */
  496. n.nodetype = cpu_to_je16(je16_to_cpu(n.nodetype) & ~JFFS2_NODE_ACCURATE);
  497. ret = jffs2_flash_write(c, ref_offset(ref), sizeof(n), &retlen, (char *)&n);
  498. if (ret) {
  499. printk(KERN_WARNING "Write error in obliterating obsoleted node at 0x%08x: %d\n", ref_offset(ref), ret);
  500. goto out_erase_sem;
  501. }
  502. if (retlen != sizeof(n)) {
  503. printk(KERN_WARNING "Short write in obliterating obsoleted node at 0x%08x: %zd\n", ref_offset(ref), retlen);
  504. goto out_erase_sem;
  505. }
  506. /* Nodes which have been marked obsolete no longer need to be
  507. associated with any inode. Remove them from the per-inode list.
  508. Note we can't do this for NAND at the moment because we need
  509. obsolete dirent nodes to stay on the lists, because of the
  510. horridness in jffs2_garbage_collect_deletion_dirent(). Also
  511. because we delete the inocache, and on NAND we need that to
  512. stay around until all the nodes are actually erased, in order
  513. to stop us from giving the same inode number to another newly
  514. created inode. */
  515. if (ref->next_in_ino) {
  516. struct jffs2_inode_cache *ic;
  517. struct jffs2_raw_node_ref **p;
  518. spin_lock(&c->erase_completion_lock);
  519. ic = jffs2_raw_ref_to_ic(ref);
  520. for (p = &ic->nodes; (*p) != ref; p = &((*p)->next_in_ino))
  521. ;
  522. *p = ref->next_in_ino;
  523. ref->next_in_ino = NULL;
  524. if (ic->nodes == (void *)ic && ic->nlink == 0)
  525. jffs2_del_ino_cache(c, ic);
  526. spin_unlock(&c->erase_completion_lock);
  527. }
  528. /* Merge with the next node in the physical list, if there is one
  529. and if it's also obsolete and if it doesn't belong to any inode */
  530. if (ref->next_phys && ref_obsolete(ref->next_phys) &&
  531. !ref->next_phys->next_in_ino) {
  532. struct jffs2_raw_node_ref *n = ref->next_phys;
  533. spin_lock(&c->erase_completion_lock);
  534. ref->__totlen += n->__totlen;
  535. ref->next_phys = n->next_phys;
  536. if (jeb->last_node == n) jeb->last_node = ref;
  537. if (jeb->gc_node == n) {
  538. /* gc will be happy continuing gc on this node */
  539. jeb->gc_node=ref;
  540. }
  541. spin_unlock(&c->erase_completion_lock);
  542. jffs2_free_raw_node_ref(n);
  543. }
  544. /* Also merge with the previous node in the list, if there is one
  545. and that one is obsolete */
  546. if (ref != jeb->first_node ) {
  547. struct jffs2_raw_node_ref *p = jeb->first_node;
  548. spin_lock(&c->erase_completion_lock);
  549. while (p->next_phys != ref)
  550. p = p->next_phys;
  551. if (ref_obsolete(p) && !ref->next_in_ino) {
  552. p->__totlen += ref->__totlen;
  553. if (jeb->last_node == ref) {
  554. jeb->last_node = p;
  555. }
  556. if (jeb->gc_node == ref) {
  557. /* gc will be happy continuing gc on this node */
  558. jeb->gc_node=p;
  559. }
  560. p->next_phys = ref->next_phys;
  561. jffs2_free_raw_node_ref(ref);
  562. }
  563. spin_unlock(&c->erase_completion_lock);
  564. }
  565. out_erase_sem:
  566. up(&c->erase_free_sem);
  567. }
  568. #if CONFIG_JFFS2_FS_DEBUG >= 2
  569. void jffs2_dump_block_lists(struct jffs2_sb_info *c)
  570. {
  571. printk(KERN_DEBUG "jffs2_dump_block_lists:\n");
  572. printk(KERN_DEBUG "flash_size: %08x\n", c->flash_size);
  573. printk(KERN_DEBUG "used_size: %08x\n", c->used_size);
  574. printk(KERN_DEBUG "dirty_size: %08x\n", c->dirty_size);
  575. printk(KERN_DEBUG "wasted_size: %08x\n", c->wasted_size);
  576. printk(KERN_DEBUG "unchecked_size: %08x\n", c->unchecked_size);
  577. printk(KERN_DEBUG "free_size: %08x\n", c->free_size);
  578. printk(KERN_DEBUG "erasing_size: %08x\n", c->erasing_size);
  579. printk(KERN_DEBUG "bad_size: %08x\n", c->bad_size);
  580. printk(KERN_DEBUG "sector_size: %08x\n", c->sector_size);
  581. printk(KERN_DEBUG "jffs2_reserved_blocks size: %08x\n",c->sector_size * c->resv_blocks_write);
  582. if (c->nextblock) {
  583. printk(KERN_DEBUG "nextblock: %08x (used %08x, dirty %08x, wasted %08x, unchecked %08x, free %08x)\n",
  584. c->nextblock->offset, c->nextblock->used_size, c->nextblock->dirty_size, c->nextblock->wasted_size, c->nextblock->unchecked_size, c->nextblock->free_size);
  585. } else {
  586. printk(KERN_DEBUG "nextblock: NULL\n");
  587. }
  588. if (c->gcblock) {
  589. printk(KERN_DEBUG "gcblock: %08x (used %08x, dirty %08x, wasted %08x, unchecked %08x, free %08x)\n",
  590. c->gcblock->offset, c->gcblock->used_size, c->gcblock->dirty_size, c->gcblock->wasted_size, c->gcblock->unchecked_size, c->gcblock->free_size);
  591. } else {
  592. printk(KERN_DEBUG "gcblock: NULL\n");
  593. }
  594. if (list_empty(&c->clean_list)) {
  595. printk(KERN_DEBUG "clean_list: empty\n");
  596. } else {
  597. struct list_head *this;
  598. int numblocks = 0;
  599. uint32_t dirty = 0;
  600. list_for_each(this, &c->clean_list) {
  601. struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
  602. numblocks ++;
  603. dirty += jeb->wasted_size;
  604. printk(KERN_DEBUG "clean_list: %08x (used %08x, dirty %08x, wasted %08x, unchecked %08x, free %08x)\n", jeb->offset, jeb->used_size, jeb->dirty_size, jeb->wasted_size, jeb->unchecked_size, jeb->free_size);
  605. }
  606. printk (KERN_DEBUG "Contains %d blocks with total wasted size %u, average wasted size: %u\n", numblocks, dirty, dirty / numblocks);
  607. }
  608. if (list_empty(&c->very_dirty_list)) {
  609. printk(KERN_DEBUG "very_dirty_list: empty\n");
  610. } else {
  611. struct list_head *this;
  612. int numblocks = 0;
  613. uint32_t dirty = 0;
  614. list_for_each(this, &c->very_dirty_list) {
  615. struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
  616. numblocks ++;
  617. dirty += jeb->dirty_size;
  618. printk(KERN_DEBUG "very_dirty_list: %08x (used %08x, dirty %08x, wasted %08x, unchecked %08x, free %08x)\n",
  619. jeb->offset, jeb->used_size, jeb->dirty_size, jeb->wasted_size, jeb->unchecked_size, jeb->free_size);
  620. }
  621. printk (KERN_DEBUG "Contains %d blocks with total dirty size %u, average dirty size: %u\n",
  622. numblocks, dirty, dirty / numblocks);
  623. }
  624. if (list_empty(&c->dirty_list)) {
  625. printk(KERN_DEBUG "dirty_list: empty\n");
  626. } else {
  627. struct list_head *this;
  628. int numblocks = 0;
  629. uint32_t dirty = 0;
  630. list_for_each(this, &c->dirty_list) {
  631. struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
  632. numblocks ++;
  633. dirty += jeb->dirty_size;
  634. printk(KERN_DEBUG "dirty_list: %08x (used %08x, dirty %08x, wasted %08x, unchecked %08x, free %08x)\n",
  635. jeb->offset, jeb->used_size, jeb->dirty_size, jeb->wasted_size, jeb->unchecked_size, jeb->free_size);
  636. }
  637. printk (KERN_DEBUG "Contains %d blocks with total dirty size %u, average dirty size: %u\n",
  638. numblocks, dirty, dirty / numblocks);
  639. }
  640. if (list_empty(&c->erasable_list)) {
  641. printk(KERN_DEBUG "erasable_list: empty\n");
  642. } else {
  643. struct list_head *this;
  644. list_for_each(this, &c->erasable_list) {
  645. struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
  646. printk(KERN_DEBUG "erasable_list: %08x (used %08x, dirty %08x, wasted %08x, unchecked %08x, free %08x)\n",
  647. jeb->offset, jeb->used_size, jeb->dirty_size, jeb->wasted_size, jeb->unchecked_size, jeb->free_size);
  648. }
  649. }
  650. if (list_empty(&c->erasing_list)) {
  651. printk(KERN_DEBUG "erasing_list: empty\n");
  652. } else {
  653. struct list_head *this;
  654. list_for_each(this, &c->erasing_list) {
  655. struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
  656. printk(KERN_DEBUG "erasing_list: %08x (used %08x, dirty %08x, wasted %08x, unchecked %08x, free %08x)\n",
  657. jeb->offset, jeb->used_size, jeb->dirty_size, jeb->wasted_size, jeb->unchecked_size, jeb->free_size);
  658. }
  659. }
  660. if (list_empty(&c->erase_pending_list)) {
  661. printk(KERN_DEBUG "erase_pending_list: empty\n");
  662. } else {
  663. struct list_head *this;
  664. list_for_each(this, &c->erase_pending_list) {
  665. struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
  666. printk(KERN_DEBUG "erase_pending_list: %08x (used %08x, dirty %08x, wasted %08x, unchecked %08x, free %08x)\n",
  667. jeb->offset, jeb->used_size, jeb->dirty_size, jeb->wasted_size, jeb->unchecked_size, jeb->free_size);
  668. }
  669. }
  670. if (list_empty(&c->erasable_pending_wbuf_list)) {
  671. printk(KERN_DEBUG "erasable_pending_wbuf_list: empty\n");
  672. } else {
  673. struct list_head *this;
  674. list_for_each(this, &c->erasable_pending_wbuf_list) {
  675. struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
  676. printk(KERN_DEBUG "erasable_pending_wbuf_list: %08x (used %08x, dirty %08x, wasted %08x, unchecked %08x, free %08x)\n",
  677. jeb->offset, jeb->used_size, jeb->dirty_size, jeb->wasted_size, jeb->unchecked_size, jeb->free_size);
  678. }
  679. }
  680. if (list_empty(&c->free_list)) {
  681. printk(KERN_DEBUG "free_list: empty\n");
  682. } else {
  683. struct list_head *this;
  684. list_for_each(this, &c->free_list) {
  685. struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
  686. printk(KERN_DEBUG "free_list: %08x (used %08x, dirty %08x, wasted %08x, unchecked %08x, free %08x)\n",
  687. jeb->offset, jeb->used_size, jeb->dirty_size, jeb->wasted_size, jeb->unchecked_size, jeb->free_size);
  688. }
  689. }
  690. if (list_empty(&c->bad_list)) {
  691. printk(KERN_DEBUG "bad_list: empty\n");
  692. } else {
  693. struct list_head *this;
  694. list_for_each(this, &c->bad_list) {
  695. struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
  696. printk(KERN_DEBUG "bad_list: %08x (used %08x, dirty %08x, wasted %08x, unchecked %08x, free %08x)\n",
  697. jeb->offset, jeb->used_size, jeb->dirty_size, jeb->wasted_size, jeb->unchecked_size, jeb->free_size);
  698. }
  699. }
  700. if (list_empty(&c->bad_used_list)) {
  701. printk(KERN_DEBUG "bad_used_list: empty\n");
  702. } else {
  703. struct list_head *this;
  704. list_for_each(this, &c->bad_used_list) {
  705. struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
  706. printk(KERN_DEBUG "bad_used_list: %08x (used %08x, dirty %08x, wasted %08x, unchecked %08x, free %08x)\n",
  707. jeb->offset, jeb->used_size, jeb->dirty_size, jeb->wasted_size, jeb->unchecked_size, jeb->free_size);
  708. }
  709. }
  710. }
  711. #endif /* CONFIG_JFFS2_FS_DEBUG */
  712. int jffs2_thread_should_wake(struct jffs2_sb_info *c)
  713. {
  714. int ret = 0;
  715. uint32_t dirty;
  716. if (c->unchecked_size) {
  717. D1(printk(KERN_DEBUG "jffs2_thread_should_wake(): unchecked_size %d, checked_ino #%d\n",
  718. c->unchecked_size, c->checked_ino));
  719. return 1;
  720. }
  721. /* dirty_size contains blocks on erase_pending_list
  722. * those blocks are counted in c->nr_erasing_blocks.
  723. * If one block is actually erased, it is not longer counted as dirty_space
  724. * but it is counted in c->nr_erasing_blocks, so we add it and subtract it
  725. * with c->nr_erasing_blocks * c->sector_size again.
  726. * Blocks on erasable_list are counted as dirty_size, but not in c->nr_erasing_blocks
  727. * This helps us to force gc and pick eventually a clean block to spread the load.
  728. */
  729. dirty = c->dirty_size + c->erasing_size - c->nr_erasing_blocks * c->sector_size;
  730. if (c->nr_free_blocks + c->nr_erasing_blocks < c->resv_blocks_gctrigger &&
  731. (dirty > c->nospc_dirty_size))
  732. ret = 1;
  733. D1(printk(KERN_DEBUG "jffs2_thread_should_wake(): nr_free_blocks %d, nr_erasing_blocks %d, dirty_size 0x%x: %s\n",
  734. c->nr_free_blocks, c->nr_erasing_blocks, c->dirty_size, ret?"yes":"no"));
  735. return ret;
  736. }