debug.c 21.9 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * JFFS2 -- Journalling Flash File System, Version 2.
 *
 * Copyright (C) 2001-2003 Red Hat, Inc.
 *
 * Created by David Woodhouse <dwmw2@infradead.org>
 *
 * For licensing information, see the file 'LICENCE' in this directory.
 *
10
 * $Id: debug.c,v 1.9 2005/08/05 10:42:24 dedekind Exp $
11 12 13
 *
 */
#include <linux/kernel.h>
14
#include <linux/types.h>
15
#include <linux/pagemap.h>
16 17
#include <linux/crc32.h>
#include <linux/jffs2.h>
18 19 20
#include "nodelist.h"
#include "debug.h"

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#ifdef JFFS2_DBG_SANITY_CHECKS

void
__jffs2_dbg_acct_sanity_check_nolock(struct jffs2_sb_info *c,
				     struct jffs2_eraseblock *jeb)
{
	if (unlikely(jeb && jeb->used_size + jeb->dirty_size +
			jeb->free_size + jeb->wasted_size +
			jeb->unchecked_size != c->sector_size)) {
		JFFS2_ERROR("eeep, space accounting for block at 0x%08x is screwed.\n", jeb->offset);
		JFFS2_ERROR("free %#08x + dirty %#08x + used %#08x + wasted %#08x + unchecked "
			"%#08x != total %#08x.\n", jeb->free_size, jeb->dirty_size, jeb->used_size,
			jeb->wasted_size, jeb->unchecked_size, c->sector_size);
		BUG();
	}

	if (unlikely(c->used_size + c->dirty_size + c->free_size + c->erasing_size + c->bad_size
				+ c->wasted_size + c->unchecked_size != c->flash_size)) {
		JFFS2_ERROR("eeep, space accounting superblock info is screwed.\n");
		JFFS2_ERROR("free %#08x + dirty %#08x + used %#08x + erasing %#08x + bad %#08x + "
			"wasted %#08x + unchecked %#08x != total %#08x.\n",
			c->free_size, c->dirty_size, c->used_size, c->erasing_size, c->bad_size,
			c->wasted_size, c->unchecked_size, c->flash_size);
		BUG();
	}
}

void
__jffs2_dbg_acct_sanity_check(struct jffs2_sb_info *c,
			      struct jffs2_eraseblock *jeb)
{
	spin_lock(&c->erase_completion_lock);
	jffs2_dbg_acct_sanity_check_nolock(c, jeb);
	spin_unlock(&c->erase_completion_lock);
}

#endif /* JFFS2_DBG_SANITY_CHECKS */

59
#ifdef JFFS2_DBG_PARANOIA_CHECKS
60 61 62
/*
 * Check the fragtree.
 */
63
void
64 65 66 67 68 69 70 71 72
__jffs2_dbg_fragtree_paranoia_check(struct jffs2_inode_info *f)
{
	down(&f->sem);
	__jffs2_dbg_fragtree_paranoia_check_nolock(f);
	up(&f->sem);
}
	
void
__jffs2_dbg_fragtree_paranoia_check_nolock(struct jffs2_inode_info *f)
73 74 75 76 77 78 79 80 81 82 83 84
{
	struct jffs2_node_frag *frag;
	int bitched = 0;

	for (frag = frag_first(&f->fragtree); frag; frag = frag_next(frag)) {
		struct jffs2_full_dnode *fn = frag->node;

		if (!fn || !fn->raw)
			continue;

		if (ref_flags(fn->raw) == REF_PRISTINE) {
			if (fn->frags > 1) {
85
				JFFS2_ERROR("REF_PRISTINE node at 0x%08x had %d frags. Tell dwmw2.\n",
86 87 88 89 90 91 92 93 94 95 96
						ref_offset(fn->raw), fn->frags);
				bitched = 1;
			}

			/* A hole node which isn't multi-page should be garbage-collected
			   and merged anyway, so we just check for the frag size here,
			   rather than mucking around with actually reading the node
			   and checking the compression type, which is the real way
			   to tell a hole node. */
			if (frag->ofs & (PAGE_CACHE_SIZE-1) && frag_prev(frag)
					&& frag_prev(frag)->size < PAGE_CACHE_SIZE && frag_prev(frag)->node) {
97 98
				JFFS2_ERROR("REF_PRISTINE node at 0x%08x had a previous non-hole frag "
						"in the same page. Tell dwmw2.\n", ref_offset(fn->raw));
99 100 101 102 103
				bitched = 1;
			}

			if ((frag->ofs+frag->size) & (PAGE_CACHE_SIZE-1) && frag_next(frag)
					&& frag_next(frag)->size < PAGE_CACHE_SIZE && frag_next(frag)->node) {
104 105
				JFFS2_ERROR("REF_PRISTINE node at 0x%08x (%08x-%08x) had a following "
						"non-hole frag in the same page. Tell dwmw2.\n",
106 107 108 109 110 111 112
					       ref_offset(fn->raw), frag->ofs, frag->ofs+frag->size);
				bitched = 1;
			}
		}
	}

	if (bitched) {
113 114
		JFFS2_ERROR("fragtree is corrupted.\n");
		__jffs2_dbg_dump_fragtree_nolock(f);
115 116 117 118 119 120 121 122
		BUG();
	}
}

/*
 * Check if the flash contains all 0xFF before we start writing.
 */
void
123 124
__jffs2_dbg_prewrite_paranoia_check(struct jffs2_sb_info *c,
				    uint32_t ofs, int len)
125 126 127 128 129 130 131 132 133 134 135
{
	size_t retlen;
	int ret, i;
	unsigned char *buf;

	buf = kmalloc(len, GFP_KERNEL);
	if (!buf)
		return;

	ret = jffs2_flash_read(c, ofs, len, &retlen, buf);
	if (ret || (retlen != len)) {
136 137
		JFFS2_WARNING("read %d bytes failed or short. ret %d, retlen %zd.\n",
				len, ret, retlen);
138 139 140 141 142 143 144 145 146 147
		kfree(buf);
		return;
	}

	ret = 0;
	for (i = 0; i < len; i++)
		if (buf[i] != 0xff)
			ret = 1;

	if (ret) {
148 149 150
		JFFS2_ERROR("argh, about to write node to %#08x on flash, but there are data "
			"already there. The first corrupted byte is at %#08x offset.\n", ofs, ofs + i);
		__jffs2_dbg_dump_buffer(buf, len, ofs);
151 152 153 154 155 156 157 158 159 160 161
		kfree(buf);
		BUG();
	}

	kfree(buf);
}

/*
 * Check the space accounting and node_ref list correctness for the JFFS2 erasable block 'jeb'.
 */
void
162 163 164 165 166 167 168 169 170 171 172
__jffs2_dbg_acct_paranoia_check(struct jffs2_sb_info *c,
				struct jffs2_eraseblock *jeb)
{
	spin_lock(&c->erase_completion_lock);
	__jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
	spin_unlock(&c->erase_completion_lock);
}
	
void
__jffs2_dbg_acct_paranoia_check_nolock(struct jffs2_sb_info *c,
				       struct jffs2_eraseblock *jeb)
173 174 175 176 177 178 179 180 181 182 183
{
	uint32_t my_used_size = 0;
	uint32_t my_unchecked_size = 0;
	uint32_t my_dirty_size = 0;
	struct jffs2_raw_node_ref *ref2 = jeb->first_node;

	while (ref2) {
		uint32_t totlen = ref_totlen(c, jeb, ref2);

		if (ref2->flash_offset < jeb->offset ||
				ref2->flash_offset > jeb->offset + c->sector_size) {
184
			JFFS2_ERROR("node_ref %#08x shouldn't be in block at %#08x.\n",
185
				ref_offset(ref2), jeb->offset);
186
			goto error;
187 188 189 190 191 192 193 194 195 196

		}
		if (ref_flags(ref2) == REF_UNCHECKED)
			my_unchecked_size += totlen;
		else if (!ref_obsolete(ref2))
			my_used_size += totlen;
		else
			my_dirty_size += totlen;

		if ((!ref2->next_phys) != (ref2 == jeb->last_node)) {
197 198 199 200 201
			JFFS2_ERROR("node_ref for node at %#08x (mem %p) has next_phys at %#08x (mem %p), "
				"last_node is at %#08x (mem %p).\n",
				ref_offset(ref2), ref2, ref_offset(ref2->next_phys), ref2->next_phys,
				ref_offset(jeb->last_node), jeb->last_node);
			goto error;
202 203 204 205 206
		}
		ref2 = ref2->next_phys;
	}

	if (my_used_size != jeb->used_size) {
207 208 209
		JFFS2_ERROR("Calculated used size %#08x != stored used size %#08x.\n",
			my_used_size, jeb->used_size);
		goto error;
210 211 212
	}

	if (my_unchecked_size != jeb->unchecked_size) {
213 214 215
		JFFS2_ERROR("Calculated unchecked size %#08x != stored unchecked size %#08x.\n",
			my_unchecked_size, jeb->unchecked_size);
		goto error;
216 217
	}

218 219
#if 0
	/* This should work when we implement ref->__totlen elemination */
220
	if (my_dirty_size != jeb->dirty_size + jeb->wasted_size) {
221
		JFFS2_ERROR("Calculated dirty+wasted size %#08x != stored dirty + wasted size %#08x\n",
222
			my_dirty_size, jeb->dirty_size + jeb->wasted_size);
223
		goto error;
224 225 226 227
	}

	if (jeb->free_size == 0
		&& my_used_size + my_unchecked_size + my_dirty_size != c->sector_size) {
228
		JFFS2_ERROR("The sum of all nodes in block (%#x) != size of block (%#x)\n",
229 230
			my_used_size + my_unchecked_size + my_dirty_size,
			c->sector_size);
231
		goto error;
232
	}
233 234 235 236 237 238 239 240 241 242
#endif

	return;

error:
	__jffs2_dbg_dump_node_refs_nolock(c, jeb);
	__jffs2_dbg_dump_jeb_nolock(jeb);
	__jffs2_dbg_dump_block_lists_nolock(c);
	BUG();
	
243
}
244
#endif /* JFFS2_DBG_PARANOIA_CHECKS */
245

246
#if defined(JFFS2_DBG_DUMPS) || defined(JFFS2_DBG_PARANOIA_CHECKS)
247 248 249 250
/*
 * Dump the node_refs of the 'jeb' JFFS2 eraseblock.
 */
void
251 252 253 254 255 256 257 258 259 260 261
__jffs2_dbg_dump_node_refs(struct jffs2_sb_info *c,
			   struct jffs2_eraseblock *jeb)
{
	spin_lock(&c->erase_completion_lock);
	__jffs2_dbg_dump_node_refs_nolock(c, jeb);
	spin_unlock(&c->erase_completion_lock);
}

void
__jffs2_dbg_dump_node_refs_nolock(struct jffs2_sb_info *c,
				  struct jffs2_eraseblock *jeb)
262 263 264 265
{
	struct jffs2_raw_node_ref *ref;
	int i = 0;

266
	JFFS2_DEBUG("Dump node_refs of the eraseblock %#08x\n", jeb->offset);
267
	if (!jeb->first_node) {
268
		JFFS2_DEBUG("no nodes in the eraseblock %#08x\n", jeb->offset);
269 270 271
		return;
	}

272
	printk(JFFS2_DBG_LVL);
273 274 275 276 277 278 279 280
	for (ref = jeb->first_node; ; ref = ref->next_phys) {
		printk("%#08x(%#x)", ref_offset(ref), ref->__totlen);
		if (ref->next_phys)
			printk("->");
		else
			break;
		if (++i == 4) {
			i = 0;
281
			printk("\n" JFFS2_DBG_LVL);
282 283 284 285 286
		}
	}
	printk("\n");
}

287 288 289 290 291 292 293 294 295 296 297
/*
 * Dump an eraseblock's space accounting.
 */
void
__jffs2_dbg_dump_jeb(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
{
	spin_lock(&c->erase_completion_lock);
	__jffs2_dbg_dump_jeb_nolock(jeb);
	spin_unlock(&c->erase_completion_lock);
}

298
void
299
__jffs2_dbg_dump_jeb_nolock(struct jffs2_eraseblock *jeb)
300
{
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
	if (!jeb)
		return;

	JFFS2_DEBUG("dump space accounting for the eraseblock at %#08x:\n",
			jeb->offset);

	printk(JFFS2_DBG_LVL "used_size: %#08x\n",	jeb->used_size);
	printk(JFFS2_DBG_LVL "dirty_size: %#08x\n",	jeb->dirty_size);
	printk(JFFS2_DBG_LVL "wasted_size: %#08x\n",	jeb->wasted_size);
	printk(JFFS2_DBG_LVL "unchecked_size: %#08x\n",	jeb->unchecked_size);
	printk(JFFS2_DBG_LVL "free_size: %#08x\n",	jeb->free_size);
}

void
__jffs2_dbg_dump_block_lists(struct jffs2_sb_info *c)
{
	spin_lock(&c->erase_completion_lock);
	__jffs2_dbg_dump_block_lists_nolock(c);
	spin_unlock(&c->erase_completion_lock);
}

void
__jffs2_dbg_dump_block_lists_nolock(struct jffs2_sb_info *c)
{
	JFFS2_DEBUG("dump JFFS2 blocks lists:\n");
	
	printk(JFFS2_DBG_LVL "flash_size: %#08x\n",	c->flash_size);
	printk(JFFS2_DBG_LVL "used_size: %#08x\n",	c->used_size);
	printk(JFFS2_DBG_LVL "dirty_size: %#08x\n",	c->dirty_size);
	printk(JFFS2_DBG_LVL "wasted_size: %#08x\n",	c->wasted_size);
	printk(JFFS2_DBG_LVL "unchecked_size: %#08x\n",	c->unchecked_size);
	printk(JFFS2_DBG_LVL "free_size: %#08x\n",	c->free_size);
	printk(JFFS2_DBG_LVL "erasing_size: %#08x\n",	c->erasing_size);
	printk(JFFS2_DBG_LVL "bad_size: %#08x\n",	c->bad_size);
	printk(JFFS2_DBG_LVL "sector_size: %#08x\n",	c->sector_size);
	printk(JFFS2_DBG_LVL "jffs2_reserved_blocks size: %#08x\n",
337 338 339
				c->sector_size * c->resv_blocks_write);

	if (c->nextblock)
340 341 342 343 344
		printk(JFFS2_DBG_LVL "nextblock: %#08x (used %#08x, dirty %#08x, wasted %#08x, "
			"unchecked %#08x, free %#08x)\n",
			c->nextblock->offset, c->nextblock->used_size,
			c->nextblock->dirty_size, c->nextblock->wasted_size,
			c->nextblock->unchecked_size, c->nextblock->free_size);
345
	else
346
		printk(JFFS2_DBG_LVL "nextblock: NULL\n");
347 348

	if (c->gcblock)
349 350 351 352
		printk(JFFS2_DBG_LVL "gcblock: %#08x (used %#08x, dirty %#08x, wasted %#08x, "
			"unchecked %#08x, free %#08x)\n",
			c->gcblock->offset, c->gcblock->used_size, c->gcblock->dirty_size,
			c->gcblock->wasted_size, c->gcblock->unchecked_size, c->gcblock->free_size);
353
	else
354
		printk(JFFS2_DBG_LVL "gcblock: NULL\n");
355 356

	if (list_empty(&c->clean_list)) {
357
		printk(JFFS2_DBG_LVL "clean_list: empty\n");
358 359 360 361 362 363 364 365 366 367
	} else {
		struct list_head *this;
		int numblocks = 0;
		uint32_t dirty = 0;

		list_for_each(this, &c->clean_list) {
			struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
			numblocks ++;
			dirty += jeb->wasted_size;
			if (!(jeb->used_size == 0 && jeb->dirty_size == 0 && jeb->wasted_size == 0)) {
368 369 370 371
				printk(JFFS2_DBG_LVL "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);
372 373 374
			}
		}

375 376
		printk (JFFS2_DBG_LVL "Contains %d blocks with total wasted size %u, average wasted size: %u\n",
			numblocks, dirty, dirty / numblocks);
377 378 379
	}

	if (list_empty(&c->very_dirty_list)) {
380
		printk(JFFS2_DBG_LVL "very_dirty_list: empty\n");
381 382 383 384 385 386 387 388 389 390 391
	} else {
		struct list_head *this;
		int numblocks = 0;
		uint32_t dirty = 0;

		list_for_each(this, &c->very_dirty_list) {
			struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);

			numblocks ++;
			dirty += jeb->dirty_size;
			if (!(jeb->used_size == 0 && jeb->dirty_size == 0 && jeb->wasted_size == 0)) {
392 393 394 395
				printk(JFFS2_DBG_LVL "very_dirty_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);
396 397 398
			}
		}

399 400
		printk (JFFS2_DBG_LVL "Contains %d blocks with total dirty size %u, average dirty size: %u\n",
			numblocks, dirty, dirty / numblocks);
401 402 403
	}

	if (list_empty(&c->dirty_list)) {
404
		printk(JFFS2_DBG_LVL "dirty_list: empty\n");
405 406 407 408 409 410 411 412 413 414 415
	} else {
		struct list_head *this;
		int numblocks = 0;
		uint32_t dirty = 0;

		list_for_each(this, &c->dirty_list) {
			struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);

			numblocks ++;
			dirty += jeb->dirty_size;
			if (!(jeb->used_size == 0 && jeb->dirty_size == 0 && jeb->wasted_size == 0)) {
416 417 418 419
				printk(JFFS2_DBG_LVL "dirty_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);
420 421 422
			}
		}

423
		printk (JFFS2_DBG_LVL "contains %d blocks with total dirty size %u, average dirty size: %u\n",
424 425 426 427
			numblocks, dirty, dirty / numblocks);
	}

	if (list_empty(&c->erasable_list)) {
428
		printk(JFFS2_DBG_LVL "erasable_list: empty\n");
429 430 431 432 433 434 435
	} else {
		struct list_head *this;

		list_for_each(this, &c->erasable_list) {
			struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);

			if (!(jeb->used_size == 0 && jeb->dirty_size == 0 && jeb->wasted_size == 0)) {
436 437 438 439
				printk(JFFS2_DBG_LVL "erasable_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);
440 441 442 443 444
			}
		}
	}

	if (list_empty(&c->erasing_list)) {
445
		printk(JFFS2_DBG_LVL "erasing_list: empty\n");
446 447 448 449 450 451 452
	} else {
		struct list_head *this;

		list_for_each(this, &c->erasing_list) {
			struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);

			if (!(jeb->used_size == 0 && jeb->dirty_size == 0 && jeb->wasted_size == 0)) {
453 454 455 456
				printk(JFFS2_DBG_LVL "erasing_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);
457 458 459 460 461
			}
		}
	}

	if (list_empty(&c->erase_pending_list)) {
462
		printk(JFFS2_DBG_LVL "erase_pending_list: empty\n");
463 464 465 466 467 468 469
	} else {
		struct list_head *this;

		list_for_each(this, &c->erase_pending_list) {
			struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);

			if (!(jeb->used_size == 0 && jeb->dirty_size == 0 && jeb->wasted_size == 0)) {
470 471 472 473
				printk(JFFS2_DBG_LVL "erase_pending_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);
474 475 476 477 478
			}
		}
	}

	if (list_empty(&c->erasable_pending_wbuf_list)) {
479
		printk(JFFS2_DBG_LVL "erasable_pending_wbuf_list: empty\n");
480 481 482 483 484 485 486
	} else {
		struct list_head *this;

		list_for_each(this, &c->erasable_pending_wbuf_list) {
			struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);

			if (!(jeb->used_size == 0 && jeb->dirty_size == 0 && jeb->wasted_size == 0)) {
487 488 489 490
				printk(JFFS2_DBG_LVL "erasable_pending_wbuf_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);
491 492 493 494 495
			}
		}
	}

	if (list_empty(&c->free_list)) {
496
		printk(JFFS2_DBG_LVL "free_list: empty\n");
497 498 499 500 501 502 503
	} else {
		struct list_head *this;

		list_for_each(this, &c->free_list) {
			struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);

			if (!(jeb->used_size == 0 && jeb->dirty_size == 0 && jeb->wasted_size == 0)) {
504 505 506 507
				printk(JFFS2_DBG_LVL "free_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);
508 509 510 511 512
			}
		}
	}

	if (list_empty(&c->bad_list)) {
513
		printk(JFFS2_DBG_LVL "bad_list: empty\n");
514 515 516 517 518 519 520
	} else {
		struct list_head *this;

		list_for_each(this, &c->bad_list) {
			struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);

			if (!(jeb->used_size == 0 && jeb->dirty_size == 0 && jeb->wasted_size == 0)) {
521 522 523 524
				printk(JFFS2_DBG_LVL "bad_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);
525 526 527 528 529
			}
		}
	}

	if (list_empty(&c->bad_used_list)) {
530
		printk(JFFS2_DBG_LVL "bad_used_list: empty\n");
531 532 533 534 535 536 537
	} else {
		struct list_head *this;

		list_for_each(this, &c->bad_used_list) {
			struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);

			if (!(jeb->used_size == 0 && jeb->dirty_size == 0 && jeb->wasted_size == 0)) {
538 539 540 541
				printk(JFFS2_DBG_LVL "bad_used_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);
542 543 544 545 546 547
			}
		}
	}
}

void
548 549 550 551 552 553 554 555 556
__jffs2_dbg_dump_fragtree(struct jffs2_inode_info *f)
{
	down(&f->sem);
	jffs2_dbg_dump_fragtree_nolock(f);
	up(&f->sem);
}

void
__jffs2_dbg_dump_fragtree_nolock(struct jffs2_inode_info *f)
557 558 559 560 561
{
	struct jffs2_node_frag *this = frag_first(&f->fragtree);
	uint32_t lastofs = 0;
	int buggy = 0;

562
	JFFS2_DEBUG("dump fragtree of ino #%u\n", f->inocache->ino);
563 564
	while(this) {
		if (this->node)
565 566 567 568 569
			printk(JFFS2_DBG_LVL "frag %#04x-%#04x: %#08x(%d) on flash (*%p), left (%p), "
				"right (%p), parent (%p)\n",
				this->ofs, this->ofs+this->size, ref_offset(this->node->raw),
				ref_flags(this->node->raw), this, frag_left(this), frag_right(this),
				frag_parent(this));
570
		else
571 572 573
			printk(JFFS2_DBG_LVL "frag %#04x-%#04x: hole (*%p). left (%p), right (%p), parent (%p)\n",
				this->ofs, this->ofs+this->size, this, frag_left(this),
				frag_right(this), frag_parent(this));
574 575 576 577 578 579 580
		if (this->ofs != lastofs)
			buggy = 1;
		lastofs = this->ofs + this->size;
		this = frag_next(this);
	}

	if (f->metadata)
581
		printk(JFFS2_DBG_LVL "metadata at 0x%08x\n", ref_offset(f->metadata->raw));
582 583

	if (buggy) {
584
		JFFS2_ERROR("frag tree got a hole in it.\n");
585 586 587 588
		BUG();
	}
}

589
#define JFFS2_BUFDUMP_BYTES_PER_LINE	32
590
void
591
__jffs2_dbg_dump_buffer(unsigned char *buf, int len, uint32_t offs)
592
{
593 594 595 596 597 598 599 600 601 602 603 604 605
	int skip;
	int i;
	
	JFFS2_DEBUG("dump from offset %#08x to offset %#08x (%x bytes).\n",
		offs, offs + len, len);
	i = skip = offs % JFFS2_BUFDUMP_BYTES_PER_LINE;
	offs = offs & ~(JFFS2_BUFDUMP_BYTES_PER_LINE - 1);
	
	if (skip != 0)
		printk(JFFS2_DBG_LVL "%#08x: ", offs);
	
	while (skip--)
		printk("   ");
606 607

	while (i < len) {
608 609 610 611 612
		if ((i % JFFS2_BUFDUMP_BYTES_PER_LINE) == 0 && i != len -1) {
			if (i != 0)
				printk("\n");
			offs += JFFS2_BUFDUMP_BYTES_PER_LINE;
			printk(JFFS2_DBG_LVL "%0#8x: ", offs);
613 614
		}

615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
		printk("%02x ", buf[i]);
		
		i += 1;
	}

	printk("\n");
}

/*
 * Dump a JFFS2 node.
 */
void
__jffs2_dbg_dump_node(struct jffs2_sb_info *c, uint32_t ofs)
{
	union jffs2_node_union node;
	int len = sizeof(union jffs2_node_union);
	size_t retlen;
	uint32_t crc;
	int ret;
	
	JFFS2_DEBUG("dump node at offset %#08x.\n", ofs);

	ret = jffs2_flash_read(c, ofs, len, &retlen, (unsigned char *)&node);
	if (ret || (retlen != len)) {
		JFFS2_ERROR("read %d bytes failed or short. ret %d, retlen %zd.\n",
			len, ret, retlen);
		return;
	}

	printk(JFFS2_DBG_LVL "magic:\t%#04x\n",
		je16_to_cpu(node.u.magic));
	printk(JFFS2_DBG_LVL "nodetype:\t%#04x\n",
		je16_to_cpu(node.u.nodetype));
	printk(JFFS2_DBG_LVL "totlen:\t%#08x\n",
		je32_to_cpu(node.u.totlen));
	printk(JFFS2_DBG_LVL "hdr_crc:\t%#08x\n",
		je32_to_cpu(node.u.hdr_crc));
	
	crc = crc32(0, &node.u, sizeof(node.u) - 4);
	if (crc != je32_to_cpu(node.u.hdr_crc)) {
		JFFS2_ERROR("wrong common header CRC.\n");
		return;
	}
	
	if (je16_to_cpu(node.u.magic) != JFFS2_MAGIC_BITMASK &&
		je16_to_cpu(node.u.magic) != JFFS2_OLD_MAGIC_BITMASK)
	{
		JFFS2_ERROR("wrong node magic: %#04x instead of %#04x.\n",
			je16_to_cpu(node.u.magic), JFFS2_MAGIC_BITMASK);
		return;
	}

	switch(je16_to_cpu(node.u.nodetype)) {

	case JFFS2_NODETYPE_INODE:

		printk(JFFS2_DBG_LVL "the node is inode node\n");
		printk(JFFS2_DBG_LVL "ino:\t%#08x\n",
				je32_to_cpu(node.i.ino));
		printk(JFFS2_DBG_LVL "version:\t%#08x\n",
				je32_to_cpu(node.i.version));
		printk(JFFS2_DBG_LVL "mode:\t%#08x\n",
				node.i.mode.m);
		printk(JFFS2_DBG_LVL "uid:\t%#04x\n",
				je16_to_cpu(node.i.uid));
		printk(JFFS2_DBG_LVL "gid:\t%#04x\n",
				je16_to_cpu(node.i.gid));
		printk(JFFS2_DBG_LVL "isize:\t%#08x\n",
				je32_to_cpu(node.i.isize));
		printk(JFFS2_DBG_LVL "atime:\t%#08x\n",
				je32_to_cpu(node.i.atime));
		printk(JFFS2_DBG_LVL "mtime:\t%#08x\n",
				je32_to_cpu(node.i.mtime));
		printk(JFFS2_DBG_LVL "ctime:\t%#08x\n",
				je32_to_cpu(node.i.ctime));
		printk(JFFS2_DBG_LVL "offset:\t%#08x\n",
				je32_to_cpu(node.i.offset));
		printk(JFFS2_DBG_LVL "csize:\t%#08x\n",
				je32_to_cpu(node.i.csize));
		printk(JFFS2_DBG_LVL "dsize:\t%#08x\n",
				je32_to_cpu(node.i.dsize));
		printk(JFFS2_DBG_LVL "compr:\t%#02x\n",
				node.i.compr);
		printk(JFFS2_DBG_LVL "usercompr:\t%#02x\n",
				node.i.usercompr);
		printk(JFFS2_DBG_LVL "flags:\t%#04x\n",
				je16_to_cpu(node.i.flags));
		printk(JFFS2_DBG_LVL "data_crc:\t%#08x\n",
				je32_to_cpu(node.i.data_crc));
		printk(JFFS2_DBG_LVL "node_crc:\t%#08x\n",
				je32_to_cpu(node.i.node_crc));
		crc = crc32(0, &node.i, sizeof(node.i) - 8); 
		if (crc != je32_to_cpu(node.i.node_crc)) {
			JFFS2_ERROR("wrong node header CRC.\n");
			return;
		}
		break;

	case JFFS2_NODETYPE_DIRENT:

		printk(JFFS2_DBG_LVL "the node is dirent node\n");
		printk(JFFS2_DBG_LVL "pino:\t%#08x\n",
				je32_to_cpu(node.d.pino));
		printk(JFFS2_DBG_LVL "version:\t%#08x\n",
				je32_to_cpu(node.d.version));
		printk(JFFS2_DBG_LVL "ino:\t%#08x\n",
				je32_to_cpu(node.d.ino));
		printk(JFFS2_DBG_LVL "mctime:\t%#08x\n",
				je32_to_cpu(node.d.mctime));
		printk(JFFS2_DBG_LVL "nsize:\t%#02x\n",
				node.d.nsize);
		printk(JFFS2_DBG_LVL "type:\t%#02x\n",
				node.d.type);
		printk(JFFS2_DBG_LVL "node_crc:\t%#08x\n",
				je32_to_cpu(node.d.node_crc));
		printk(JFFS2_DBG_LVL "name_crc:\t%#08x\n",
				je32_to_cpu(node.d.name_crc));
		
		node.d.name[node.d.nsize] = '\0';
		printk(JFFS2_DBG_LVL "name:\t\"%s\"\n", node.d.name);

		crc = crc32(0, &node.d, sizeof(node.d) - 8); 
		if (crc != je32_to_cpu(node.d.node_crc)) {
			JFFS2_ERROR("wrong node header CRC.\n");
			return;
740
		}
741
		break;
742

743 744 745
	default:
		printk(JFFS2_DBG_LVL "node type is unknown\n");
		break;
746 747
	}
}
748
#endif /* JFFS2_DBG_DUMPS || JFFS2_DBG_PARANOIA_CHECKS */