debug.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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: debug.c,v 1.1 2005/07/17 06:56:20 dedekind Exp $
  11. *
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/pagemap.h>
  15. #include "nodelist.h"
  16. #include "debug.h"
  17. #ifdef JFFS2_DBG_PARANOIA_CHECKS
  18. void
  19. jffs2_dbg_fragtree_paranoia_check(struct jffs2_inode_info *f)
  20. {
  21. struct jffs2_node_frag *frag;
  22. int bitched = 0;
  23. for (frag = frag_first(&f->fragtree); frag; frag = frag_next(frag)) {
  24. struct jffs2_full_dnode *fn = frag->node;
  25. if (!fn || !fn->raw)
  26. continue;
  27. if (ref_flags(fn->raw) == REF_PRISTINE) {
  28. if (fn->frags > 1) {
  29. printk(KERN_ERR "REF_PRISTINE node at 0x%08x had %d frags. Tell dwmw2\n",
  30. ref_offset(fn->raw), fn->frags);
  31. bitched = 1;
  32. }
  33. /* A hole node which isn't multi-page should be garbage-collected
  34. and merged anyway, so we just check for the frag size here,
  35. rather than mucking around with actually reading the node
  36. and checking the compression type, which is the real way
  37. to tell a hole node. */
  38. if (frag->ofs & (PAGE_CACHE_SIZE-1) && frag_prev(frag)
  39. && frag_prev(frag)->size < PAGE_CACHE_SIZE && frag_prev(frag)->node) {
  40. printk(KERN_ERR "REF_PRISTINE node at 0x%08x had a previous non-hole frag "
  41. "in the same page. Tell dwmw2\n", ref_offset(fn->raw));
  42. bitched = 1;
  43. }
  44. if ((frag->ofs+frag->size) & (PAGE_CACHE_SIZE-1) && frag_next(frag)
  45. && frag_next(frag)->size < PAGE_CACHE_SIZE && frag_next(frag)->node) {
  46. printk(KERN_ERR "REF_PRISTINE node at 0x%08x (%08x-%08x) had a following "
  47. "non-hole frag in the same page. Tell dwmw2\n",
  48. ref_offset(fn->raw), frag->ofs, frag->ofs+frag->size);
  49. bitched = 1;
  50. }
  51. }
  52. }
  53. if (bitched) {
  54. printk(KERN_ERR "Fragtree is corrupted. Fragtree dump:\n");
  55. jffs2_dbg_dump_fragtree(f);
  56. BUG();
  57. }
  58. }
  59. /*
  60. * Check if the flash contains all 0xFF before we start writing.
  61. */
  62. void
  63. jffs2_dbg_prewrite_paranoia_check(struct jffs2_sb_info *c, uint32_t ofs, int len)
  64. {
  65. size_t retlen;
  66. int ret, i;
  67. unsigned char *buf;
  68. buf = kmalloc(len, GFP_KERNEL);
  69. if (!buf)
  70. return;
  71. ret = jffs2_flash_read(c, ofs, len, &retlen, buf);
  72. if (ret || (retlen != len)) {
  73. printk(KERN_WARNING "read %d bytes failed or short in %s(). ret %d, retlen %zd\n",
  74. len, __FUNCTION__, ret, retlen);
  75. kfree(buf);
  76. return;
  77. }
  78. ret = 0;
  79. for (i = 0; i < len; i++)
  80. if (buf[i] != 0xff)
  81. ret = 1;
  82. if (ret) {
  83. printk(KERN_ERR "ARGH. About to write node to %#08x on flash, but there are data "
  84. "already there. The first corrupted byte is at %#08x.\n", ofs, ofs + i);
  85. jffs2_dbg_dump_buffer(buf, len, ofs);
  86. kfree(buf);
  87. BUG();
  88. }
  89. kfree(buf);
  90. }
  91. /*
  92. * Check the space accounting and node_ref list correctness for the JFFS2 erasable block 'jeb'.
  93. */
  94. void
  95. jffs2_dbg_acct_paranoia_check(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
  96. {
  97. uint32_t my_used_size = 0;
  98. uint32_t my_unchecked_size = 0;
  99. uint32_t my_dirty_size = 0;
  100. struct jffs2_raw_node_ref *ref2 = jeb->first_node;
  101. while (ref2) {
  102. uint32_t totlen = ref_totlen(c, jeb, ref2);
  103. if (ref2->flash_offset < jeb->offset ||
  104. ref2->flash_offset > jeb->offset + c->sector_size) {
  105. printk(KERN_ERR "node_ref %#08x shouldn't be in block at %#08x!\n",
  106. ref_offset(ref2), jeb->offset);
  107. jffs2_dbg_dump_node_refs(c, jeb);
  108. jffs2_dbg_dump_block_lists(c);
  109. BUG();
  110. }
  111. if (ref_flags(ref2) == REF_UNCHECKED)
  112. my_unchecked_size += totlen;
  113. else if (!ref_obsolete(ref2))
  114. my_used_size += totlen;
  115. else
  116. my_dirty_size += totlen;
  117. if ((!ref2->next_phys) != (ref2 == jeb->last_node)) {
  118. printk(KERN_ERR "node_ref for node at %#08x (mem %p) has next_phys at %#08x (mem %p), "
  119. "last_node is at %#08x (mem %p)\n",
  120. ref_offset(ref2), ref2, ref_offset(ref2->next_phys), ref2->next_phys,
  121. ref_offset(jeb->last_node), jeb->last_node);
  122. jffs2_dbg_dump_node_refs(c, jeb);
  123. jffs2_dbg_dump_block_lists(c);
  124. BUG();
  125. }
  126. ref2 = ref2->next_phys;
  127. }
  128. if (my_used_size != jeb->used_size) {
  129. printk(KERN_ERR "Calculated used size %#08x != stored used size %#08x\n",
  130. my_used_size, jeb->used_size);
  131. jffs2_dbg_dump_node_refs(c, jeb);
  132. jffs2_dbg_dump_block_lists(c);
  133. BUG();
  134. }
  135. if (my_unchecked_size != jeb->unchecked_size) {
  136. printk(KERN_ERR "Calculated unchecked size %#08x != stored unchecked size %#08x\n",
  137. my_unchecked_size, jeb->unchecked_size);
  138. jffs2_dbg_dump_node_refs(c, jeb);
  139. jffs2_dbg_dump_block_lists(c);
  140. BUG();
  141. }
  142. if (my_dirty_size != jeb->dirty_size + jeb->wasted_size) {
  143. printk(KERN_ERR "Calculated dirty+wasted size %#08x != stored dirty + wasted size %#08x\n",
  144. my_dirty_size, jeb->dirty_size + jeb->wasted_size);
  145. jffs2_dbg_dump_node_refs(c, jeb);
  146. jffs2_dbg_dump_block_lists(c);
  147. BUG();
  148. }
  149. if (jeb->free_size == 0
  150. && my_used_size + my_unchecked_size + my_dirty_size != c->sector_size) {
  151. printk(KERN_ERR "The sum of all nodes in block (%#x) != size of block (%#x)\n",
  152. my_used_size + my_unchecked_size + my_dirty_size,
  153. c->sector_size);
  154. jffs2_dbg_dump_node_refs(c, jeb);
  155. jffs2_dbg_dump_block_lists(c);
  156. BUG();
  157. }
  158. }
  159. #endif /* JFFS2_PARANOIA_CHECKS */
  160. #if defined(JFFS2_PARANOIA_CHECKS) || (CONFIG_JFFS2_FS_DEBUG > 0)
  161. /*
  162. * Dump the node_refs of the 'jeb' JFFS2 eraseblock.
  163. */
  164. void
  165. jffs2_dbg_dump_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
  166. {
  167. struct jffs2_raw_node_ref *ref;
  168. int i = 0;
  169. if (!jeb->first_node) {
  170. printk(KERN_DEBUG "no nodes in block %#08x\n", jeb->offset);
  171. return;
  172. }
  173. printk(KERN_DEBUG);
  174. for (ref = jeb->first_node; ; ref = ref->next_phys) {
  175. printk("%#08x(%#x)", ref_offset(ref), ref->__totlen);
  176. if (ref->next_phys)
  177. printk("->");
  178. else
  179. break;
  180. if (++i == 4) {
  181. i = 0;
  182. printk("\n" KERN_DEBUG);
  183. }
  184. }
  185. printk("\n");
  186. }
  187. void
  188. jffs2_dbg_dump_block_lists(struct jffs2_sb_info *c)
  189. {
  190. printk(KERN_DEBUG "flash_size: %#08x\n", c->flash_size);
  191. printk(KERN_DEBUG "used_size: %#08x\n", c->used_size);
  192. printk(KERN_DEBUG "dirty_size: %#08x\n", c->dirty_size);
  193. printk(KERN_DEBUG "wasted_size: %#08x\n", c->wasted_size);
  194. printk(KERN_DEBUG "unchecked_size: %#08x\n", c->unchecked_size);
  195. printk(KERN_DEBUG "free_size: %#08x\n", c->free_size);
  196. printk(KERN_DEBUG "erasing_size: %#08x\n", c->erasing_size);
  197. printk(KERN_DEBUG "bad_size: %#08x\n", c->bad_size);
  198. printk(KERN_DEBUG "sector_size: %#08x\n", c->sector_size);
  199. printk(KERN_DEBUG "jffs2_reserved_blocks size: %#08x\n",
  200. c->sector_size * c->resv_blocks_write);
  201. if (c->nextblock)
  202. printk(KERN_DEBUG "nextblock: %#08x (used %#08x, dirty %#08x, wasted %#08x, "
  203. "unchecked %#08x, free %#08x)\n",
  204. c->nextblock->offset, c->nextblock->used_size,
  205. c->nextblock->dirty_size, c->nextblock->wasted_size,
  206. c->nextblock->unchecked_size, c->nextblock->free_size);
  207. else
  208. printk(KERN_DEBUG "nextblock: NULL\n");
  209. if (c->gcblock)
  210. printk(KERN_DEBUG "gcblock: %#08x (used %#08x, dirty %#08x, wasted %#08x, "
  211. "unchecked %#08x, free %#08x)\n",
  212. c->gcblock->offset, c->gcblock->used_size, c->gcblock->dirty_size,
  213. c->gcblock->wasted_size, c->gcblock->unchecked_size, c->gcblock->free_size);
  214. else
  215. printk(KERN_DEBUG "gcblock: NULL\n");
  216. if (list_empty(&c->clean_list)) {
  217. printk(KERN_DEBUG "clean_list: empty\n");
  218. } else {
  219. struct list_head *this;
  220. int numblocks = 0;
  221. uint32_t dirty = 0;
  222. list_for_each(this, &c->clean_list) {
  223. struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
  224. numblocks ++;
  225. dirty += jeb->wasted_size;
  226. if (!(jeb->used_size == 0 && jeb->dirty_size == 0 && jeb->wasted_size == 0)) {
  227. printk(KERN_DEBUG "clean_list: %#08x (used %#08x, dirty %#08x, wasted %#08x, "
  228. "unchecked %#08x, free %#08x)\n",
  229. jeb->offset, jeb->used_size, jeb->dirty_size, jeb->wasted_size,
  230. jeb->unchecked_size, jeb->free_size);
  231. }
  232. }
  233. printk (KERN_DEBUG "Contains %d blocks with total wasted size %u, average wasted size: %u\n",
  234. numblocks, dirty, dirty / numblocks);
  235. }
  236. if (list_empty(&c->very_dirty_list)) {
  237. printk(KERN_DEBUG "very_dirty_list: empty\n");
  238. } else {
  239. struct list_head *this;
  240. int numblocks = 0;
  241. uint32_t dirty = 0;
  242. list_for_each(this, &c->very_dirty_list) {
  243. struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
  244. numblocks ++;
  245. dirty += jeb->dirty_size;
  246. if (!(jeb->used_size == 0 && jeb->dirty_size == 0 && jeb->wasted_size == 0)) {
  247. printk(KERN_DEBUG "very_dirty_list: %#08x (used %#08x, dirty %#08x, wasted %#08x, "
  248. "unchecked %#08x, free %#08x)\n",
  249. jeb->offset, jeb->used_size, jeb->dirty_size, jeb->wasted_size,
  250. jeb->unchecked_size, jeb->free_size);
  251. }
  252. }
  253. printk (KERN_DEBUG "Contains %d blocks with total dirty size %u, average dirty size: %u\n",
  254. numblocks, dirty, dirty / numblocks);
  255. }
  256. if (list_empty(&c->dirty_list)) {
  257. printk(KERN_DEBUG "dirty_list: empty\n");
  258. } else {
  259. struct list_head *this;
  260. int numblocks = 0;
  261. uint32_t dirty = 0;
  262. list_for_each(this, &c->dirty_list) {
  263. struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
  264. numblocks ++;
  265. dirty += jeb->dirty_size;
  266. if (!(jeb->used_size == 0 && jeb->dirty_size == 0 && jeb->wasted_size == 0)) {
  267. printk(KERN_DEBUG "dirty_list: %#08x (used %#08x, dirty %#08x, wasted %#08x, "
  268. "unchecked %#08x, free %#08x)\n",
  269. jeb->offset, jeb->used_size, jeb->dirty_size, jeb->wasted_size,
  270. jeb->unchecked_size, jeb->free_size);
  271. }
  272. }
  273. printk (KERN_DEBUG "Contains %d blocks with total dirty size %u, average dirty size: %u\n",
  274. numblocks, dirty, dirty / numblocks);
  275. }
  276. if (list_empty(&c->erasable_list)) {
  277. printk(KERN_DEBUG "erasable_list: empty\n");
  278. } else {
  279. struct list_head *this;
  280. list_for_each(this, &c->erasable_list) {
  281. struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
  282. if (!(jeb->used_size == 0 && jeb->dirty_size == 0 && jeb->wasted_size == 0)) {
  283. printk(KERN_DEBUG "erasable_list: %#08x (used %#08x, dirty %#08x, wasted %#08x, "
  284. "unchecked %#08x, free %#08x)\n",
  285. jeb->offset, jeb->used_size, jeb->dirty_size, jeb->wasted_size,
  286. jeb->unchecked_size, jeb->free_size);
  287. }
  288. }
  289. }
  290. if (list_empty(&c->erasing_list)) {
  291. printk(KERN_DEBUG "erasing_list: empty\n");
  292. } else {
  293. struct list_head *this;
  294. list_for_each(this, &c->erasing_list) {
  295. struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
  296. if (!(jeb->used_size == 0 && jeb->dirty_size == 0 && jeb->wasted_size == 0)) {
  297. printk(KERN_DEBUG "erasing_list: %#08x (used %#08x, dirty %#08x, wasted %#08x, "
  298. "unchecked %#08x, free %#08x)\n",
  299. jeb->offset, jeb->used_size, jeb->dirty_size, jeb->wasted_size,
  300. jeb->unchecked_size, jeb->free_size);
  301. }
  302. }
  303. }
  304. if (list_empty(&c->erase_pending_list)) {
  305. printk(KERN_DEBUG "erase_pending_list: empty\n");
  306. } else {
  307. struct list_head *this;
  308. list_for_each(this, &c->erase_pending_list) {
  309. struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
  310. if (!(jeb->used_size == 0 && jeb->dirty_size == 0 && jeb->wasted_size == 0)) {
  311. printk(KERN_DEBUG "erase_pending_list: %#08x (used %#08x, dirty %#08x, wasted %#08x, "
  312. "unchecked %#08x, free %#08x)\n",
  313. jeb->offset, jeb->used_size, jeb->dirty_size, jeb->wasted_size,
  314. jeb->unchecked_size, jeb->free_size);
  315. }
  316. }
  317. }
  318. if (list_empty(&c->erasable_pending_wbuf_list)) {
  319. printk(KERN_DEBUG "erasable_pending_wbuf_list: empty\n");
  320. } else {
  321. struct list_head *this;
  322. list_for_each(this, &c->erasable_pending_wbuf_list) {
  323. struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
  324. if (!(jeb->used_size == 0 && jeb->dirty_size == 0 && jeb->wasted_size == 0)) {
  325. printk(KERN_DEBUG "erasable_pending_wbuf_list: %#08x (used %#08x, dirty %#08x, "
  326. "wasted %#08x, unchecked %#08x, free %#08x)\n",
  327. jeb->offset, jeb->used_size, jeb->dirty_size, jeb->wasted_size,
  328. jeb->unchecked_size, jeb->free_size);
  329. }
  330. }
  331. }
  332. if (list_empty(&c->free_list)) {
  333. printk(KERN_DEBUG "free_list: empty\n");
  334. } else {
  335. struct list_head *this;
  336. list_for_each(this, &c->free_list) {
  337. struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
  338. if (!(jeb->used_size == 0 && jeb->dirty_size == 0 && jeb->wasted_size == 0)) {
  339. printk(KERN_DEBUG "free_list: %#08x (used %#08x, dirty %#08x, wasted %#08x, "
  340. "unchecked %#08x, free %#08x)\n",
  341. jeb->offset, jeb->used_size, jeb->dirty_size, jeb->wasted_size,
  342. jeb->unchecked_size, jeb->free_size);
  343. }
  344. }
  345. }
  346. if (list_empty(&c->bad_list)) {
  347. printk(KERN_DEBUG "bad_list: empty\n");
  348. } else {
  349. struct list_head *this;
  350. list_for_each(this, &c->bad_list) {
  351. struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
  352. if (!(jeb->used_size == 0 && jeb->dirty_size == 0 && jeb->wasted_size == 0)) {
  353. printk(KERN_DEBUG "bad_list: %#08x (used %#08x, dirty %#08x, wasted %#08x, "
  354. "unchecked %#08x, free %#08x)\n",
  355. jeb->offset, jeb->used_size, jeb->dirty_size, jeb->wasted_size,
  356. jeb->unchecked_size, jeb->free_size);
  357. }
  358. }
  359. }
  360. if (list_empty(&c->bad_used_list)) {
  361. printk(KERN_DEBUG "bad_used_list: empty\n");
  362. } else {
  363. struct list_head *this;
  364. list_for_each(this, &c->bad_used_list) {
  365. struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
  366. if (!(jeb->used_size == 0 && jeb->dirty_size == 0 && jeb->wasted_size == 0)) {
  367. printk(KERN_DEBUG "bad_used_list: %#08x (used %#08x, dirty %#08x, wasted %#08x, "
  368. "unchecked %#08x, free %#08x)\n",
  369. jeb->offset, jeb->used_size, jeb->dirty_size, jeb->wasted_size,
  370. jeb->unchecked_size, jeb->free_size);
  371. }
  372. }
  373. }
  374. }
  375. void
  376. jffs2_dbg_dump_fragtree(struct jffs2_inode_info *f)
  377. {
  378. struct jffs2_node_frag *this = frag_first(&f->fragtree);
  379. uint32_t lastofs = 0;
  380. int buggy = 0;
  381. printk(KERN_DEBUG "inode is ino #%u\n", f->inocache->ino);
  382. while(this) {
  383. if (this->node)
  384. printk(KERN_DEBUG "frag %#04x-%#04x: %#08x(%d) on flash (*%p), left (%p), "
  385. "right (%p), parent (%p)\n",
  386. this->ofs, this->ofs+this->size, ref_offset(this->node->raw),
  387. ref_flags(this->node->raw), this, frag_left(this), frag_right(this),
  388. frag_parent(this));
  389. else
  390. printk(KERN_DEBUG "frag %#04x-%#04x: hole (*%p). left (%p), right (%p), parent (%p)\n",
  391. this->ofs, this->ofs+this->size, this, frag_left(this),
  392. frag_right(this), frag_parent(this));
  393. if (this->ofs != lastofs)
  394. buggy = 1;
  395. lastofs = this->ofs + this->size;
  396. this = frag_next(this);
  397. }
  398. if (f->metadata)
  399. printk(KERN_DEBUG "metadata at 0x%08x\n", ref_offset(f->metadata->raw));
  400. if (buggy) {
  401. printk(KERN_ERR "Error! %s(): Frag tree got a hole in it\n", __FUNCTION__);
  402. BUG();
  403. }
  404. }
  405. #define JFFS3_BUFDUMP_BYTES_PER_LINE 8
  406. void
  407. jffs2_dbg_dump_buffer(char *buf, int len, uint32_t offs)
  408. {
  409. int i = 0;
  410. int skip = offs & ~(JFFS3_BUFDUMP_BYTES_PER_LINE - 1);
  411. while (i < len) {
  412. int j = 0;
  413. printk(KERN_DEBUG "0x#x: \n");
  414. while (skip) {
  415. printk(" ");
  416. skip -= 1;
  417. }
  418. while (j < JFFS3_BUFDUMP_BYTES_PER_LINE) {
  419. if (i + j < len)
  420. printk(" %#02x", buf[i + j++]);
  421. }
  422. i += JFFS3_BUFDUMP_BYTES_PER_LINE;
  423. }
  424. }
  425. #endif /* JFFS2_PARANOIA_CHECKS || CONFIG_JFFS2_FS_DEBUG > 0 */