nodemgmt.c 26 KB

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