nodemgmt.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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.127 2005/09/20 15:49:12 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. #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_del(&ejeb->list);
  181. list_add_tail(&ejeb->list, &c->erase_pending_list);
  182. c->nr_erasing_blocks++;
  183. jffs2_erase_pending_trigger(c);
  184. D1(printk(KERN_DEBUG "jffs2_find_nextblock: Triggering erase of erasable block at 0x%08x\n",
  185. ejeb->offset));
  186. }
  187. if (!c->nr_erasing_blocks &&
  188. !list_empty(&c->erasable_pending_wbuf_list)) {
  189. D1(printk(KERN_DEBUG "jffs2_find_nextblock: Flushing write buffer\n"));
  190. /* c->nextblock is NULL, no update to c->nextblock allowed */
  191. spin_unlock(&c->erase_completion_lock);
  192. jffs2_flush_wbuf_pad(c);
  193. spin_lock(&c->erase_completion_lock);
  194. /* Have another go. It'll be on the erasable_list now */
  195. return -EAGAIN;
  196. }
  197. if (!c->nr_erasing_blocks) {
  198. /* Ouch. We're in GC, or we wouldn't have got here.
  199. And there's no space left. At all. */
  200. 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",
  201. c->nr_erasing_blocks, c->nr_free_blocks, list_empty(&c->erasable_list)?"yes":"no",
  202. list_empty(&c->erasing_list)?"yes":"no", list_empty(&c->erase_pending_list)?"yes":"no");
  203. return -ENOSPC;
  204. }
  205. spin_unlock(&c->erase_completion_lock);
  206. /* Don't wait for it; just erase one right now */
  207. jffs2_erase_pending_blocks(c, 1);
  208. spin_lock(&c->erase_completion_lock);
  209. /* An erase may have failed, decreasing the
  210. amount of free space available. So we must
  211. restart from the beginning */
  212. return -EAGAIN;
  213. }
  214. next = c->free_list.next;
  215. list_del(next);
  216. c->nextblock = list_entry(next, struct jffs2_eraseblock, list);
  217. c->nr_free_blocks--;
  218. jffs2_sum_reset_collected(c->summary); /* reset collected summary */
  219. D1(printk(KERN_DEBUG "jffs2_find_nextblock(): new nextblock = 0x%08x\n", c->nextblock->offset));
  220. return 0;
  221. }
  222. /* Called with alloc sem _and_ erase_completion_lock */
  223. static int jffs2_do_reserve_space(struct jffs2_sb_info *c, uint32_t minsize,
  224. uint32_t *len, uint32_t sumsize)
  225. {
  226. struct jffs2_eraseblock *jeb = c->nextblock;
  227. uint32_t reserved_size; /* for summary information at the end of the jeb */
  228. int ret;
  229. restart:
  230. reserved_size = 0;
  231. if (jffs2_sum_active() && (sumsize != JFFS2_SUMMARY_NOSUM_SIZE)) {
  232. /* NOSUM_SIZE means not to generate summary */
  233. if (jeb) {
  234. reserved_size = PAD(sumsize + c->summary->sum_size + JFFS2_SUMMARY_FRAME_SIZE);
  235. dbg_summary("minsize=%d , jeb->free=%d ,"
  236. "summary->size=%d , sumsize=%d\n",
  237. minsize, jeb->free_size,
  238. c->summary->sum_size, sumsize);
  239. }
  240. /* Is there enough space for writing out the current node, or we have to
  241. write out summary information now, close this jeb and select new nextblock? */
  242. if (jeb && (PAD(minsize) + PAD(c->summary->sum_size + sumsize +
  243. JFFS2_SUMMARY_FRAME_SIZE) > jeb->free_size)) {
  244. /* Has summary been disabled for this jeb? */
  245. if (jffs2_sum_is_disabled(c->summary)) {
  246. sumsize = JFFS2_SUMMARY_NOSUM_SIZE;
  247. goto restart;
  248. }
  249. /* Writing out the collected summary information */
  250. dbg_summary("generating summary for 0x%08x.\n", jeb->offset);
  251. ret = jffs2_sum_write_sumnode(c);
  252. if (ret)
  253. return ret;
  254. if (jffs2_sum_is_disabled(c->summary)) {
  255. /* jffs2_write_sumnode() couldn't write out the summary information
  256. diabling summary for this jeb and free the collected information
  257. */
  258. sumsize = JFFS2_SUMMARY_NOSUM_SIZE;
  259. goto restart;
  260. }
  261. jffs2_close_nextblock(c, jeb);
  262. jeb = NULL;
  263. /* keep always valid value in reserved_size */
  264. reserved_size = PAD(sumsize + c->summary->sum_size + JFFS2_SUMMARY_FRAME_SIZE);
  265. }
  266. } else {
  267. if (jeb && minsize > jeb->free_size) {
  268. uint32_t waste;
  269. /* Skip the end of this block and file it as having some dirty space */
  270. /* If there's a pending write to it, flush now */
  271. if (jffs2_wbuf_dirty(c)) {
  272. spin_unlock(&c->erase_completion_lock);
  273. D1(printk(KERN_DEBUG "jffs2_do_reserve_space: Flushing write buffer\n"));
  274. jffs2_flush_wbuf_pad(c);
  275. spin_lock(&c->erase_completion_lock);
  276. jeb = c->nextblock;
  277. goto restart;
  278. }
  279. spin_unlock(&c->erase_completion_lock);
  280. ret = jffs2_prealloc_raw_node_refs(c, jeb, 1);
  281. if (ret)
  282. return ret;
  283. /* Just lock it again and continue. Nothing much can change because
  284. we hold c->alloc_sem anyway. In fact, it's not entirely clear why
  285. we hold c->erase_completion_lock in the majority of this function...
  286. but that's a question for another (more caffeine-rich) day. */
  287. spin_lock(&c->erase_completion_lock);
  288. waste = jeb->free_size;
  289. jffs2_link_node_ref(c, jeb,
  290. (jeb->offset + c->sector_size - waste) | REF_OBSOLETE,
  291. waste, NULL);
  292. /* FIXME: that made it count as dirty. Convert to wasted */
  293. jeb->dirty_size -= waste;
  294. c->dirty_size -= waste;
  295. jeb->wasted_size += waste;
  296. c->wasted_size += waste;
  297. jffs2_close_nextblock(c, jeb);
  298. jeb = NULL;
  299. }
  300. }
  301. if (!jeb) {
  302. ret = jffs2_find_nextblock(c);
  303. if (ret)
  304. return ret;
  305. jeb = c->nextblock;
  306. if (jeb->free_size != c->sector_size - c->cleanmarker_size) {
  307. printk(KERN_WARNING "Eep. Block 0x%08x taken from free_list had free_size of 0x%08x!!\n", jeb->offset, jeb->free_size);
  308. goto restart;
  309. }
  310. }
  311. /* OK, jeb (==c->nextblock) is now pointing at a block which definitely has
  312. enough space */
  313. *len = jeb->free_size - reserved_size;
  314. if (c->cleanmarker_size && jeb->used_size == c->cleanmarker_size &&
  315. !jeb->first_node->next_in_ino) {
  316. /* Only node in it beforehand was a CLEANMARKER node (we think).
  317. So mark it obsolete now that there's going to be another node
  318. in the block. This will reduce used_size to zero but We've
  319. already set c->nextblock so that jffs2_mark_node_obsolete()
  320. won't try to refile it to the dirty_list.
  321. */
  322. spin_unlock(&c->erase_completion_lock);
  323. jffs2_mark_node_obsolete(c, jeb->first_node);
  324. spin_lock(&c->erase_completion_lock);
  325. }
  326. D1(printk(KERN_DEBUG "jffs2_do_reserve_space(): Giving 0x%x bytes at 0x%x\n",
  327. *len, jeb->offset + (c->sector_size - jeb->free_size)));
  328. return 0;
  329. }
  330. /**
  331. * jffs2_add_physical_node_ref - add a physical node reference to the list
  332. * @c: superblock info
  333. * @new: new node reference to add
  334. * @len: length of this physical node
  335. *
  336. * Should only be used to report nodes for which space has been allocated
  337. * by jffs2_reserve_space.
  338. *
  339. * Must be called with the alloc_sem held.
  340. */
  341. struct jffs2_raw_node_ref *jffs2_add_physical_node_ref(struct jffs2_sb_info *c,
  342. uint32_t ofs, uint32_t len,
  343. struct jffs2_inode_cache *ic)
  344. {
  345. struct jffs2_eraseblock *jeb;
  346. struct jffs2_raw_node_ref *new;
  347. jeb = &c->blocks[ofs / c->sector_size];
  348. D1(printk(KERN_DEBUG "jffs2_add_physical_node_ref(): Node at 0x%x(%d), size 0x%x\n",
  349. ofs & ~3, ofs & 3, len));
  350. #if 1
  351. /* Allow non-obsolete nodes only to be added at the end of c->nextblock,
  352. if c->nextblock is set. Note that wbuf.c will file obsolete nodes
  353. even after refiling c->nextblock */
  354. if ((c->nextblock || ((ofs & 3) != REF_OBSOLETE))
  355. && (jeb != c->nextblock || (ofs & ~3) != jeb->offset + (c->sector_size - jeb->free_size))) {
  356. printk(KERN_WARNING "argh. node added in wrong place\n");
  357. return ERR_PTR(-EINVAL);
  358. }
  359. #endif
  360. spin_lock(&c->erase_completion_lock);
  361. new = jffs2_link_node_ref(c, jeb, ofs, len, ic);
  362. if (!jeb->free_size && !jeb->dirty_size && !ISDIRTY(jeb->wasted_size)) {
  363. /* If it lives on the dirty_list, jffs2_reserve_space will put it there */
  364. D1(printk(KERN_DEBUG "Adding full erase block at 0x%08x to clean_list (free 0x%08x, dirty 0x%08x, used 0x%08x\n",
  365. jeb->offset, jeb->free_size, jeb->dirty_size, jeb->used_size));
  366. if (jffs2_wbuf_dirty(c)) {
  367. /* Flush the last write in the block if it's outstanding */
  368. spin_unlock(&c->erase_completion_lock);
  369. jffs2_flush_wbuf_pad(c);
  370. spin_lock(&c->erase_completion_lock);
  371. }
  372. list_add_tail(&jeb->list, &c->clean_list);
  373. c->nextblock = NULL;
  374. }
  375. jffs2_dbg_acct_sanity_check_nolock(c,jeb);
  376. jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
  377. spin_unlock(&c->erase_completion_lock);
  378. return new;
  379. }
  380. void jffs2_complete_reservation(struct jffs2_sb_info *c)
  381. {
  382. D1(printk(KERN_DEBUG "jffs2_complete_reservation()\n"));
  383. jffs2_garbage_collect_trigger(c);
  384. up(&c->alloc_sem);
  385. }
  386. static inline int on_list(struct list_head *obj, struct list_head *head)
  387. {
  388. struct list_head *this;
  389. list_for_each(this, head) {
  390. if (this == obj) {
  391. D1(printk("%p is on list at %p\n", obj, head));
  392. return 1;
  393. }
  394. }
  395. return 0;
  396. }
  397. void jffs2_mark_node_obsolete(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref)
  398. {
  399. struct jffs2_eraseblock *jeb;
  400. int blocknr;
  401. struct jffs2_unknown_node n;
  402. int ret, addedsize;
  403. size_t retlen;
  404. uint32_t freed_len;
  405. if(unlikely(!ref)) {
  406. printk(KERN_NOTICE "EEEEEK. jffs2_mark_node_obsolete called with NULL node\n");
  407. return;
  408. }
  409. if (ref_obsolete(ref)) {
  410. D1(printk(KERN_DEBUG "jffs2_mark_node_obsolete called with already obsolete node at 0x%08x\n", ref_offset(ref)));
  411. return;
  412. }
  413. blocknr = ref->flash_offset / c->sector_size;
  414. if (blocknr >= c->nr_blocks) {
  415. printk(KERN_NOTICE "raw node at 0x%08x is off the end of device!\n", ref->flash_offset);
  416. BUG();
  417. }
  418. jeb = &c->blocks[blocknr];
  419. if (jffs2_can_mark_obsolete(c) && !jffs2_is_readonly(c) &&
  420. !(c->flags & (JFFS2_SB_FLAG_SCANNING | JFFS2_SB_FLAG_BUILDING))) {
  421. /* Hm. This may confuse static lock analysis. If any of the above
  422. three conditions is false, we're going to return from this
  423. function without actually obliterating any nodes or freeing
  424. any jffs2_raw_node_refs. So we don't need to stop erases from
  425. happening, or protect against people holding an obsolete
  426. jffs2_raw_node_ref without the erase_completion_lock. */
  427. down(&c->erase_free_sem);
  428. }
  429. spin_lock(&c->erase_completion_lock);
  430. freed_len = ref_totlen(c, jeb, ref);
  431. if (ref_flags(ref) == REF_UNCHECKED) {
  432. D1(if (unlikely(jeb->unchecked_size < freed_len)) {
  433. 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",
  434. freed_len, blocknr, ref->flash_offset, jeb->used_size);
  435. BUG();
  436. })
  437. D1(printk(KERN_DEBUG "Obsoleting previously unchecked node at 0x%08x of len %x: ", ref_offset(ref), freed_len));
  438. jeb->unchecked_size -= freed_len;
  439. c->unchecked_size -= freed_len;
  440. } else {
  441. D1(if (unlikely(jeb->used_size < freed_len)) {
  442. 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",
  443. freed_len, blocknr, ref->flash_offset, jeb->used_size);
  444. BUG();
  445. })
  446. D1(printk(KERN_DEBUG "Obsoleting node at 0x%08x of len %#x: ", ref_offset(ref), freed_len));
  447. jeb->used_size -= freed_len;
  448. c->used_size -= freed_len;
  449. }
  450. // Take care, that wasted size is taken into concern
  451. if ((jeb->dirty_size || ISDIRTY(jeb->wasted_size + freed_len)) && jeb != c->nextblock) {
  452. D1(printk("Dirtying\n"));
  453. addedsize = freed_len;
  454. jeb->dirty_size += freed_len;
  455. c->dirty_size += freed_len;
  456. /* Convert wasted space to dirty, if not a bad block */
  457. if (jeb->wasted_size) {
  458. if (on_list(&jeb->list, &c->bad_used_list)) {
  459. D1(printk(KERN_DEBUG "Leaving block at %08x on the bad_used_list\n",
  460. jeb->offset));
  461. addedsize = 0; /* To fool the refiling code later */
  462. } else {
  463. D1(printk(KERN_DEBUG "Converting %d bytes of wasted space to dirty in block at %08x\n",
  464. jeb->wasted_size, jeb->offset));
  465. addedsize += jeb->wasted_size;
  466. jeb->dirty_size += jeb->wasted_size;
  467. c->dirty_size += jeb->wasted_size;
  468. c->wasted_size -= jeb->wasted_size;
  469. jeb->wasted_size = 0;
  470. }
  471. }
  472. } else {
  473. D1(printk("Wasting\n"));
  474. addedsize = 0;
  475. jeb->wasted_size += freed_len;
  476. c->wasted_size += freed_len;
  477. }
  478. ref->flash_offset = ref_offset(ref) | REF_OBSOLETE;
  479. jffs2_dbg_acct_sanity_check_nolock(c, jeb);
  480. jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
  481. if (c->flags & JFFS2_SB_FLAG_SCANNING) {
  482. /* Flash scanning is in progress. Don't muck about with the block
  483. lists because they're not ready yet, and don't actually
  484. obliterate nodes that look obsolete. If they weren't
  485. marked obsolete on the flash at the time they _became_
  486. obsolete, there was probably a reason for that. */
  487. spin_unlock(&c->erase_completion_lock);
  488. /* We didn't lock the erase_free_sem */
  489. return;
  490. }
  491. if (jeb == c->nextblock) {
  492. D2(printk(KERN_DEBUG "Not moving nextblock 0x%08x to dirty/erase_pending list\n", jeb->offset));
  493. } else if (!jeb->used_size && !jeb->unchecked_size) {
  494. if (jeb == c->gcblock) {
  495. D1(printk(KERN_DEBUG "gcblock at 0x%08x completely dirtied. Clearing gcblock...\n", jeb->offset));
  496. c->gcblock = NULL;
  497. } else {
  498. D1(printk(KERN_DEBUG "Eraseblock at 0x%08x completely dirtied. Removing from (dirty?) list...\n", jeb->offset));
  499. list_del(&jeb->list);
  500. }
  501. if (jffs2_wbuf_dirty(c)) {
  502. D1(printk(KERN_DEBUG "...and adding to erasable_pending_wbuf_list\n"));
  503. list_add_tail(&jeb->list, &c->erasable_pending_wbuf_list);
  504. } else {
  505. if (jiffies & 127) {
  506. /* Most of the time, we just erase it immediately. Otherwise we
  507. spend ages scanning it on mount, etc. */
  508. D1(printk(KERN_DEBUG "...and adding to erase_pending_list\n"));
  509. list_add_tail(&jeb->list, &c->erase_pending_list);
  510. c->nr_erasing_blocks++;
  511. jffs2_erase_pending_trigger(c);
  512. } else {
  513. /* Sometimes, however, we leave it elsewhere so it doesn't get
  514. immediately reused, and we spread the load a bit. */
  515. D1(printk(KERN_DEBUG "...and adding to erasable_list\n"));
  516. list_add_tail(&jeb->list, &c->erasable_list);
  517. }
  518. }
  519. D1(printk(KERN_DEBUG "Done OK\n"));
  520. } else if (jeb == c->gcblock) {
  521. D2(printk(KERN_DEBUG "Not moving gcblock 0x%08x to dirty_list\n", jeb->offset));
  522. } else if (ISDIRTY(jeb->dirty_size) && !ISDIRTY(jeb->dirty_size - addedsize)) {
  523. D1(printk(KERN_DEBUG "Eraseblock at 0x%08x is freshly dirtied. Removing from clean list...\n", jeb->offset));
  524. list_del(&jeb->list);
  525. D1(printk(KERN_DEBUG "...and adding to dirty_list\n"));
  526. list_add_tail(&jeb->list, &c->dirty_list);
  527. } else if (VERYDIRTY(c, jeb->dirty_size) &&
  528. !VERYDIRTY(c, jeb->dirty_size - addedsize)) {
  529. D1(printk(KERN_DEBUG "Eraseblock at 0x%08x is now very dirty. Removing from dirty list...\n", jeb->offset));
  530. list_del(&jeb->list);
  531. D1(printk(KERN_DEBUG "...and adding to very_dirty_list\n"));
  532. list_add_tail(&jeb->list, &c->very_dirty_list);
  533. } else {
  534. D1(printk(KERN_DEBUG "Eraseblock at 0x%08x not moved anywhere. (free 0x%08x, dirty 0x%08x, used 0x%08x)\n",
  535. jeb->offset, jeb->free_size, jeb->dirty_size, jeb->used_size));
  536. }
  537. spin_unlock(&c->erase_completion_lock);
  538. if (!jffs2_can_mark_obsolete(c) || jffs2_is_readonly(c) ||
  539. (c->flags & JFFS2_SB_FLAG_BUILDING)) {
  540. /* We didn't lock the erase_free_sem */
  541. return;
  542. }
  543. /* The erase_free_sem is locked, and has been since before we marked the node obsolete
  544. and potentially put its eraseblock onto the erase_pending_list. Thus, we know that
  545. the block hasn't _already_ been erased, and that 'ref' itself hasn't been freed yet
  546. by jffs2_free_jeb_node_refs() in erase.c. Which is nice. */
  547. D1(printk(KERN_DEBUG "obliterating obsoleted node at 0x%08x\n", ref_offset(ref)));
  548. ret = jffs2_flash_read(c, ref_offset(ref), sizeof(n), &retlen, (char *)&n);
  549. if (ret) {
  550. printk(KERN_WARNING "Read error reading from obsoleted node at 0x%08x: %d\n", ref_offset(ref), ret);
  551. goto out_erase_sem;
  552. }
  553. if (retlen != sizeof(n)) {
  554. printk(KERN_WARNING "Short read from obsoleted node at 0x%08x: %zd\n", ref_offset(ref), retlen);
  555. goto out_erase_sem;
  556. }
  557. if (PAD(je32_to_cpu(n.totlen)) != PAD(freed_len)) {
  558. printk(KERN_WARNING "Node totlen on flash (0x%08x) != totlen from node ref (0x%08x)\n", je32_to_cpu(n.totlen), freed_len);
  559. goto out_erase_sem;
  560. }
  561. if (!(je16_to_cpu(n.nodetype) & JFFS2_NODE_ACCURATE)) {
  562. D1(printk(KERN_DEBUG "Node at 0x%08x was already marked obsolete (nodetype 0x%04x)\n", ref_offset(ref), je16_to_cpu(n.nodetype)));
  563. goto out_erase_sem;
  564. }
  565. /* XXX FIXME: This is ugly now */
  566. n.nodetype = cpu_to_je16(je16_to_cpu(n.nodetype) & ~JFFS2_NODE_ACCURATE);
  567. ret = jffs2_flash_write(c, ref_offset(ref), sizeof(n), &retlen, (char *)&n);
  568. if (ret) {
  569. printk(KERN_WARNING "Write error in obliterating obsoleted node at 0x%08x: %d\n", ref_offset(ref), ret);
  570. goto out_erase_sem;
  571. }
  572. if (retlen != sizeof(n)) {
  573. printk(KERN_WARNING "Short write in obliterating obsoleted node at 0x%08x: %zd\n", ref_offset(ref), retlen);
  574. goto out_erase_sem;
  575. }
  576. /* Nodes which have been marked obsolete no longer need to be
  577. associated with any inode. Remove them from the per-inode list.
  578. Note we can't do this for NAND at the moment because we need
  579. obsolete dirent nodes to stay on the lists, because of the
  580. horridness in jffs2_garbage_collect_deletion_dirent(). Also
  581. because we delete the inocache, and on NAND we need that to
  582. stay around until all the nodes are actually erased, in order
  583. to stop us from giving the same inode number to another newly
  584. created inode. */
  585. if (ref->next_in_ino) {
  586. struct jffs2_inode_cache *ic;
  587. struct jffs2_raw_node_ref **p;
  588. spin_lock(&c->erase_completion_lock);
  589. ic = jffs2_raw_ref_to_ic(ref);
  590. /* It seems we should never call jffs2_mark_node_obsolete() for
  591. XATTR nodes.... yet. Make sure we notice if/when we change
  592. that :) */
  593. BUG_ON(ic->class != RAWNODE_CLASS_INODE_CACHE);
  594. for (p = &ic->nodes; (*p) != ref; p = &((*p)->next_in_ino))
  595. ;
  596. *p = ref->next_in_ino;
  597. ref->next_in_ino = NULL;
  598. if (ic->nodes == (void *)ic && ic->nlink == 0)
  599. jffs2_del_ino_cache(c, ic);
  600. spin_unlock(&c->erase_completion_lock);
  601. }
  602. out_erase_sem:
  603. up(&c->erase_free_sem);
  604. }
  605. int jffs2_thread_should_wake(struct jffs2_sb_info *c)
  606. {
  607. int ret = 0;
  608. uint32_t dirty;
  609. if (c->unchecked_size) {
  610. D1(printk(KERN_DEBUG "jffs2_thread_should_wake(): unchecked_size %d, checked_ino #%d\n",
  611. c->unchecked_size, c->checked_ino));
  612. return 1;
  613. }
  614. /* dirty_size contains blocks on erase_pending_list
  615. * those blocks are counted in c->nr_erasing_blocks.
  616. * If one block is actually erased, it is not longer counted as dirty_space
  617. * but it is counted in c->nr_erasing_blocks, so we add it and subtract it
  618. * with c->nr_erasing_blocks * c->sector_size again.
  619. * Blocks on erasable_list are counted as dirty_size, but not in c->nr_erasing_blocks
  620. * This helps us to force gc and pick eventually a clean block to spread the load.
  621. */
  622. dirty = c->dirty_size + c->erasing_size - c->nr_erasing_blocks * c->sector_size;
  623. if (c->nr_free_blocks + c->nr_erasing_blocks < c->resv_blocks_gctrigger &&
  624. (dirty > c->nospc_dirty_size))
  625. ret = 1;
  626. D1(printk(KERN_DEBUG "jffs2_thread_should_wake(): nr_free_blocks %d, nr_erasing_blocks %d, dirty_size 0x%x: %s\n",
  627. c->nr_free_blocks, c->nr_erasing_blocks, c->dirty_size, ret?"yes":"no"));
  628. return ret;
  629. }