prints.c 20.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
 */

#include <linux/time.h>
#include <linux/fs.h>
#include <linux/reiserfs_fs.h>
#include <linux/string.h>
#include <linux/buffer_head.h>

#include <stdarg.h>

static char error_buf[1024];
static char fmt_buf[1024];
static char off_buf[80];

17
static char *reiserfs_cpu_offset(struct cpu_key *key)
L
Linus Torvalds 已提交
18
{
19 20 21 22 23 24 25 26 27 28
	if (cpu_key_k_type(key) == TYPE_DIRENTRY)
		sprintf(off_buf, "%Lu(%Lu)",
			(unsigned long long)
			GET_HASH_VALUE(cpu_key_k_offset(key)),
			(unsigned long long)
			GET_GENERATION_NUMBER(cpu_key_k_offset(key)));
	else
		sprintf(off_buf, "0x%Lx",
			(unsigned long long)cpu_key_k_offset(key));
	return off_buf;
L
Linus Torvalds 已提交
29 30
}

31
static char *le_offset(struct reiserfs_key *key)
L
Linus Torvalds 已提交
32
{
33
	int version;
L
Linus Torvalds 已提交
34

35 36 37 38 39 40 41 42 43 44 45
	version = le_key_version(key);
	if (le_key_k_type(version, key) == TYPE_DIRENTRY)
		sprintf(off_buf, "%Lu(%Lu)",
			(unsigned long long)
			GET_HASH_VALUE(le_key_k_offset(version, key)),
			(unsigned long long)
			GET_GENERATION_NUMBER(le_key_k_offset(version, key)));
	else
		sprintf(off_buf, "0x%Lx",
			(unsigned long long)le_key_k_offset(version, key));
	return off_buf;
L
Linus Torvalds 已提交
46 47
}

48
static char *cpu_type(struct cpu_key *key)
L
Linus Torvalds 已提交
49
{
50 51 52 53 54 55 56 57 58
	if (cpu_key_k_type(key) == TYPE_STAT_DATA)
		return "SD";
	if (cpu_key_k_type(key) == TYPE_DIRENTRY)
		return "DIR";
	if (cpu_key_k_type(key) == TYPE_DIRECT)
		return "DIRECT";
	if (cpu_key_k_type(key) == TYPE_INDIRECT)
		return "IND";
	return "UNKNOWN";
L
Linus Torvalds 已提交
59 60
}

61
static char *le_type(struct reiserfs_key *key)
L
Linus Torvalds 已提交
62
{
63
	int version;
L
Linus Torvalds 已提交
64

65
	version = le_key_version(key);
L
Linus Torvalds 已提交
66

67 68 69 70 71 72 73 74 75 76
	if (le_key_k_type(version, key) == TYPE_STAT_DATA)
		return "SD";
	if (le_key_k_type(version, key) == TYPE_DIRENTRY)
		return "DIR";
	if (le_key_k_type(version, key) == TYPE_DIRECT)
		return "DIRECT";
	if (le_key_k_type(version, key) == TYPE_INDIRECT)
		return "IND";
	return "UNKNOWN";
}
L
Linus Torvalds 已提交
77 78

/* %k */
79
static void sprintf_le_key(char *buf, struct reiserfs_key *key)
L
Linus Torvalds 已提交
80
{
81 82 83 84 85 86
	if (key)
		sprintf(buf, "[%d %d %s %s]", le32_to_cpu(key->k_dir_id),
			le32_to_cpu(key->k_objectid), le_offset(key),
			le_type(key));
	else
		sprintf(buf, "[NULL]");
L
Linus Torvalds 已提交
87 88 89
}

/* %K */
90
static void sprintf_cpu_key(char *buf, struct cpu_key *key)
L
Linus Torvalds 已提交
91
{
92 93 94 95 96 97
	if (key)
		sprintf(buf, "[%d %d %s %s]", key->on_disk_key.k_dir_id,
			key->on_disk_key.k_objectid, reiserfs_cpu_offset(key),
			cpu_type(key));
	else
		sprintf(buf, "[NULL]");
L
Linus Torvalds 已提交
98 99
}

100
static void sprintf_de_head(char *buf, struct reiserfs_de_head *deh)
L
Linus Torvalds 已提交
101
{
102 103 104 105 106 107 108
	if (deh)
		sprintf(buf,
			"[offset=%d dir_id=%d objectid=%d location=%d state=%04x]",
			deh_offset(deh), deh_dir_id(deh), deh_objectid(deh),
			deh_location(deh), deh_state(deh));
	else
		sprintf(buf, "[NULL]");
L
Linus Torvalds 已提交
109 110 111

}

112
static void sprintf_item_head(char *buf, struct item_head *ih)
L
Linus Torvalds 已提交
113
{
114 115 116 117 118 119 120 121 122
	if (ih) {
		strcpy(buf,
		       (ih_version(ih) == KEY_FORMAT_3_6) ? "*3.6* " : "*3.5*");
		sprintf_le_key(buf + strlen(buf), &(ih->ih_key));
		sprintf(buf + strlen(buf), ", item_len %d, item_location %d, "
			"free_space(entry_count) %d",
			ih_item_len(ih), ih_location(ih), ih_free_space(ih));
	} else
		sprintf(buf, "[NULL]");
L
Linus Torvalds 已提交
123 124
}

125
static void sprintf_direntry(char *buf, struct reiserfs_dir_entry *de)
L
Linus Torvalds 已提交
126
{
127
	char name[20];
L
Linus Torvalds 已提交
128

129 130 131
	memcpy(name, de->de_name, de->de_namelen > 19 ? 19 : de->de_namelen);
	name[de->de_namelen > 19 ? 19 : de->de_namelen] = 0;
	sprintf(buf, "\"%s\"==>[%d %d]", name, de->de_dir_id, de->de_objectid);
L
Linus Torvalds 已提交
132 133
}

134
static void sprintf_block_head(char *buf, struct buffer_head *bh)
L
Linus Torvalds 已提交
135
{
136 137
	sprintf(buf, "level=%d, nr_items=%d, free_space=%d rdkey ",
		B_LEVEL(bh), B_NR_ITEMS(bh), B_FREE_SPACE(bh));
L
Linus Torvalds 已提交
138 139
}

140
static void sprintf_buffer_head(char *buf, struct buffer_head *bh)
L
Linus Torvalds 已提交
141
{
142
	char b[BDEVNAME_SIZE];
L
Linus Torvalds 已提交
143

144
	sprintf(buf,
145
		"dev %s, size %zd, blocknr %llu, count %d, state 0x%lx, page %p, (%s, %s, %s)",
146 147 148 149 150 151
		bdevname(bh->b_bdev, b), bh->b_size,
		(unsigned long long)bh->b_blocknr, atomic_read(&(bh->b_count)),
		bh->b_state, bh->b_page,
		buffer_uptodate(bh) ? "UPTODATE" : "!UPTODATE",
		buffer_dirty(bh) ? "DIRTY" : "CLEAN",
		buffer_locked(bh) ? "LOCKED" : "UNLOCKED");
L
Linus Torvalds 已提交
152 153
}

154
static void sprintf_disk_child(char *buf, struct disk_child *dc)
L
Linus Torvalds 已提交
155
{
156 157
	sprintf(buf, "[dc_number=%d, dc_size=%u]", dc_block_number(dc),
		dc_size(dc));
L
Linus Torvalds 已提交
158 159
}

160
static char *is_there_reiserfs_struct(char *fmt, int *what)
L
Linus Torvalds 已提交
161
{
162
	char *k = fmt;
L
Linus Torvalds 已提交
163

164 165 166 167 168 169 170 171 172 173
	while ((k = strchr(k, '%')) != NULL) {
		if (k[1] == 'k' || k[1] == 'K' || k[1] == 'h' || k[1] == 't' ||
		    k[1] == 'z' || k[1] == 'b' || k[1] == 'y' || k[1] == 'a') {
			*what = k[1];
			break;
		}
		k++;
	}
	return k;
}
L
Linus Torvalds 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186

/* debugging reiserfs we used to print out a lot of different
   variables, like keys, item headers, buffer heads etc. Values of
   most fields matter. So it took a long time just to write
   appropriative printk. With this reiserfs_warning you can use format
   specification for complex structures like you used to do with
   printfs for integers, doubles and pointers. For instance, to print
   out key structure you have to write just: 
   reiserfs_warning ("bad key %k", key); 
   instead of 
   printk ("bad key %lu %lu %lu %lu", key->k_dir_id, key->k_objectid, 
           key->k_offset, key->k_uniqueness); 
*/
187
static DEFINE_SPINLOCK(error_lock);
188 189 190 191 192
static void prepare_error_buf(const char *fmt, va_list args)
{
	char *fmt1 = fmt_buf;
	char *k;
	char *p = error_buf;
193
	int what;
194

195 196
	spin_lock(&error_lock);

197 198
	strcpy(fmt1, fmt);

199
	while ((k = is_there_reiserfs_struct(fmt1, &what)) != NULL) {
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
		*k = 0;

		p += vsprintf(p, fmt1, args);

		switch (what) {
		case 'k':
			sprintf_le_key(p, va_arg(args, struct reiserfs_key *));
			break;
		case 'K':
			sprintf_cpu_key(p, va_arg(args, struct cpu_key *));
			break;
		case 'h':
			sprintf_item_head(p, va_arg(args, struct item_head *));
			break;
		case 't':
			sprintf_direntry(p,
					 va_arg(args,
						struct reiserfs_dir_entry *));
			break;
		case 'y':
			sprintf_disk_child(p,
					   va_arg(args, struct disk_child *));
			break;
		case 'z':
			sprintf_block_head(p,
					   va_arg(args, struct buffer_head *));
			break;
		case 'b':
			sprintf_buffer_head(p,
					    va_arg(args, struct buffer_head *));
			break;
		case 'a':
			sprintf_de_head(p,
					va_arg(args,
					       struct reiserfs_de_head *));
			break;
		}

		p += strlen(p);
		fmt1 = k + 2;
	}
	vsprintf(p, fmt1, args);
242
	spin_unlock(&error_lock);
L
Linus Torvalds 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263

}

/* in addition to usual conversion specifiers this accepts reiserfs
   specific conversion specifiers: 
   %k to print little endian key, 
   %K to print cpu key, 
   %h to print item_head,
   %t to print directory entry 
   %z to print block head (arg must be struct buffer_head *
   %b to print buffer_head
*/

#define do_reiserfs_warning(fmt)\
{\
    va_list args;\
    va_start( args, fmt );\
    prepare_error_buf( fmt, args );\
    va_end( args );\
}

J
Jeff Mahoney 已提交
264 265
void __reiserfs_warning(struct super_block *sb, const char *id,
			 const char *function, const char *fmt, ...)
L
Linus Torvalds 已提交
266
{
267 268
	do_reiserfs_warning(fmt);
	if (sb)
J
Jeff Mahoney 已提交
269 270 271
		printk(KERN_WARNING "REISERFS warning (device %s): %s%s%s: "
		       "%s\n", sb->s_id, id ? id : "", id ? " " : "",
		       function, error_buf);
272
	else
J
Jeff Mahoney 已提交
273 274
		printk(KERN_WARNING "REISERFS warning: %s%s%s: %s\n",
		       id ? id : "", id ? " " : "", function, error_buf);
L
Linus Torvalds 已提交
275 276 277
}

/* No newline.. reiserfs_info calls can be followed by printk's */
278
void reiserfs_info(struct super_block *sb, const char *fmt, ...)
L
Linus Torvalds 已提交
279
{
280 281
	do_reiserfs_warning(fmt);
	if (sb)
282 283
		printk(KERN_NOTICE "REISERFS (device %s): %s",
		       sb->s_id, error_buf);
284
	else
285
		printk(KERN_NOTICE "REISERFS %s:", error_buf);
L
Linus Torvalds 已提交
286 287 288
}

/* No newline.. reiserfs_printk calls can be followed by printk's */
289
static void reiserfs_printk(const char *fmt, ...)
L
Linus Torvalds 已提交
290
{
291 292
	do_reiserfs_warning(fmt);
	printk(error_buf);
L
Linus Torvalds 已提交
293 294
}

295
void reiserfs_debug(struct super_block *s, int level, const char *fmt, ...)
L
Linus Torvalds 已提交
296 297
{
#ifdef CONFIG_REISERFS_CHECK
298 299
	do_reiserfs_warning(fmt);
	if (s)
300 301
		printk(KERN_DEBUG "REISERFS debug (device %s): %s\n",
		       s->s_id, error_buf);
302
	else
303
		printk(KERN_DEBUG "REISERFS debug: %s\n", error_buf);
L
Linus Torvalds 已提交
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 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
#endif
}

/* The format:

           maintainer-errorid: [function-name:] message

    where errorid is unique to the maintainer and function-name is
    optional, is recommended, so that anyone can easily find the bug
    with a simple grep for the short to type string
    maintainer-errorid.  Don't bother with reusing errorids, there are
    lots of numbers out there.

    Example: 
    
    reiserfs_panic(
	p_sb, "reiser-29: reiserfs_new_blocknrs: "
	"one of search_start or rn(%d) is equal to MAX_B_NUM,"
	"which means that we are optimizing location based on the bogus location of a temp buffer (%p).", 
	rn, bh
    );

    Regular panic()s sometimes clear the screen before the message can
    be read, thus the need for the while loop.  

    Numbering scheme for panic used by Vladimir and Anatoly( Hans completely ignores this scheme, and considers it
    pointless complexity):

    panics in reiserfs_fs.h have numbers from 1000 to 1999
    super.c				        2000 to 2999
    preserve.c (unused)			    3000 to 3999
    bitmap.c				    4000 to 4999
    stree.c				        5000 to 5999
    prints.c				    6000 to 6999
    namei.c                     7000 to 7999
    fix_nodes.c                 8000 to 8999
    dir.c                       9000 to 9999
	lbalance.c					10000 to 10999
	ibalance.c		11000 to 11999 not ready
	do_balan.c		12000 to 12999
	inode.c			13000 to 13999
	file.c			14000 to 14999
    objectid.c                       15000 - 15999
    buffer.c                         16000 - 16999
    symlink.c                        17000 - 17999

   .  */

#ifdef CONFIG_REISERFS_CHECK
353
extern struct tree_balance *cur_tb;
L
Linus Torvalds 已提交
354 355
#endif

J
Jeff Mahoney 已提交
356 357
void __reiserfs_panic(struct super_block *sb, const char *id,
		      const char *function, const char *fmt, ...)
L
Linus Torvalds 已提交
358
{
359
	do_reiserfs_warning(fmt);
L
Linus Torvalds 已提交
360

J
Jeff Mahoney 已提交
361
#ifdef CONFIG_REISERFS_CHECK
362
	dump_stack();
J
Jeff Mahoney 已提交
363 364 365 366 367 368 369 370
#endif
	if (sb)
		panic(KERN_WARNING "REISERFS panic (device %s): %s%s%s: %s\n",
		      sb->s_id, id ? id : "", id ? " " : "",
		      function, error_buf);
	else
		panic(KERN_WARNING "REISERFS panic: %s%s%s: %s\n",
		      id ? id : "", id ? " " : "", function, error_buf);
L
Linus Torvalds 已提交
371 372
}

373
void reiserfs_abort(struct super_block *sb, int errno, const char *fmt, ...)
L
Linus Torvalds 已提交
374
{
375
	do_reiserfs_warning(fmt);
L
Linus Torvalds 已提交
376

377
	if (reiserfs_error_panic(sb)) {
378 379
		panic(KERN_CRIT "REISERFS panic (device %s): %s\n", sb->s_id,
		      error_buf);
380
	}
L
Linus Torvalds 已提交
381

382
	if (reiserfs_is_journal_aborted(SB_JOURNAL(sb)))
383
		return;
L
Linus Torvalds 已提交
384

385 386
	printk(KERN_CRIT "REISERFS abort (device %s): %s\n", sb->s_id,
	       error_buf);
L
Linus Torvalds 已提交
387

388 389
	sb->s_flags |= MS_RDONLY;
	reiserfs_journal_abort(sb, errno);
L
Linus Torvalds 已提交
390 391 392 393 394
}

/* this prints internal nodes (4 keys/items in line) (dc_number,
   dc_size)[k_dirid, k_objectid, k_offset, k_uniqueness](dc_number,
   dc_size)...*/
395
static int print_internal(struct buffer_head *bh, int first, int last)
L
Linus Torvalds 已提交
396
{
397 398 399 400
	struct reiserfs_key *key;
	struct disk_child *dc;
	int i;
	int from, to;
L
Linus Torvalds 已提交
401

402 403
	if (!B_IS_KEYS_LEVEL(bh))
		return 1;
L
Linus Torvalds 已提交
404

405
	check_internal(bh);
L
Linus Torvalds 已提交
406

407 408 409 410 411 412 413
	if (first == -1) {
		from = 0;
		to = B_NR_ITEMS(bh);
	} else {
		from = first;
		to = last < B_NR_ITEMS(bh) ? last : B_NR_ITEMS(bh);
	}
L
Linus Torvalds 已提交
414

415
	reiserfs_printk("INTERNAL NODE (%ld) contains %z\n", bh->b_blocknr, bh);
L
Linus Torvalds 已提交
416

417 418
	dc = B_N_CHILD(bh, from);
	reiserfs_printk("PTR %d: %y ", from, dc);
L
Linus Torvalds 已提交
419

420 421 422 423 424 425 426 427 428
	for (i = from, key = B_N_PDELIM_KEY(bh, from), dc++; i < to;
	     i++, key++, dc++) {
		reiserfs_printk("KEY %d: %k PTR %d: %y ", i, key, i + 1, dc);
		if (i && i % 4 == 0)
			printk("\n");
	}
	printk("\n");
	return 0;
}
L
Linus Torvalds 已提交
429

430 431 432 433 434 435 436
static int print_leaf(struct buffer_head *bh, int print_mode, int first,
		      int last)
{
	struct block_head *blkh;
	struct item_head *ih;
	int i, nr;
	int from, to;
L
Linus Torvalds 已提交
437

438 439
	if (!B_IS_ITEMS_LEVEL(bh))
		return 1;
L
Linus Torvalds 已提交
440

441
	check_leaf(bh);
L
Linus Torvalds 已提交
442

443 444 445
	blkh = B_BLK_HEAD(bh);
	ih = B_N_PITEM_HEAD(bh, 0);
	nr = blkh_nr_item(blkh);
L
Linus Torvalds 已提交
446

447 448 449
	printk
	    ("\n===================================================================\n");
	reiserfs_printk("LEAF NODE (%ld) contains %z\n", bh->b_blocknr, bh);
L
Linus Torvalds 已提交
450

451 452 453 454 455
	if (!(print_mode & PRINT_LEAF_ITEMS)) {
		reiserfs_printk("FIRST ITEM_KEY: %k, LAST ITEM KEY: %k\n",
				&(ih->ih_key), &((ih + nr - 1)->ih_key));
		return 0;
	}
L
Linus Torvalds 已提交
456

457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
	if (first < 0 || first > nr - 1)
		from = 0;
	else
		from = first;

	if (last < 0 || last > nr)
		to = nr;
	else
		to = last;

	ih += from;
	printk
	    ("-------------------------------------------------------------------------------\n");
	printk
	    ("|##|   type    |           key           | ilen | free_space | version | loc  |\n");
	for (i = from; i < to; i++, ih++) {
		printk
		    ("-------------------------------------------------------------------------------\n");
		reiserfs_printk("|%2d| %h |\n", i, ih);
		if (print_mode & PRINT_LEAF_ITEMS)
			op_print_item(ih, B_I_PITEM(bh, ih));
	}
L
Linus Torvalds 已提交
479

480 481
	printk
	    ("===================================================================\n");
L
Linus Torvalds 已提交
482

483
	return 0;
L
Linus Torvalds 已提交
484 485
}

486
char *reiserfs_hashname(int code)
L
Linus Torvalds 已提交
487
{
488 489 490 491 492 493
	if (code == YURA_HASH)
		return "rupasov";
	if (code == TEA_HASH)
		return "tea";
	if (code == R5_HASH)
		return "r5";
L
Linus Torvalds 已提交
494

495
	return "unknown";
L
Linus Torvalds 已提交
496 497 498
}

/* return 1 if this is not super block */
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
static int print_super_block(struct buffer_head *bh)
{
	struct reiserfs_super_block *rs =
	    (struct reiserfs_super_block *)(bh->b_data);
	int skipped, data_blocks;
	char *version;
	char b[BDEVNAME_SIZE];

	if (is_reiserfs_3_5(rs)) {
		version = "3.5";
	} else if (is_reiserfs_3_6(rs)) {
		version = "3.6";
	} else if (is_reiserfs_jr(rs)) {
		version = ((sb_version(rs) == REISERFS_VERSION_2) ?
			   "3.6" : "3.5");
	} else {
		return 1;
	}

	printk("%s\'s super block is in block %llu\n", bdevname(bh->b_bdev, b),
	       (unsigned long long)bh->b_blocknr);
	printk("Reiserfs version %s\n", version);
	printk("Block count %u\n", sb_block_count(rs));
	printk("Blocksize %d\n", sb_blocksize(rs));
	printk("Free blocks %u\n", sb_free_blocks(rs));
	// FIXME: this would be confusing if
	// someone stores reiserfs super block in some data block ;)
L
Linus Torvalds 已提交
526
//    skipped = (bh->b_blocknr * bh->b_size) / sb_blocksize(rs);
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
	skipped = bh->b_blocknr;
	data_blocks = sb_block_count(rs) - skipped - 1 - sb_bmap_nr(rs) -
	    (!is_reiserfs_jr(rs) ? sb_jp_journal_size(rs) +
	     1 : sb_reserved_for_journal(rs)) - sb_free_blocks(rs);
	printk
	    ("Busy blocks (skipped %d, bitmaps - %d, journal (or reserved) blocks - %d\n"
	     "1 super block, %d data blocks\n", skipped, sb_bmap_nr(rs),
	     (!is_reiserfs_jr(rs) ? (sb_jp_journal_size(rs) + 1) :
	      sb_reserved_for_journal(rs)), data_blocks);
	printk("Root block %u\n", sb_root_block(rs));
	printk("Journal block (first) %d\n", sb_jp_journal_1st_block(rs));
	printk("Journal dev %d\n", sb_jp_journal_dev(rs));
	printk("Journal orig size %d\n", sb_jp_journal_size(rs));
	printk("FS state %d\n", sb_fs_state(rs));
	printk("Hash function \"%s\"\n",
	       reiserfs_hashname(sb_hash_function_code(rs)));

	printk("Tree height %d\n", sb_tree_height(rs));
	return 0;
L
Linus Torvalds 已提交
546 547
}

548
static int print_desc_block(struct buffer_head *bh)
L
Linus Torvalds 已提交
549
{
550
	struct reiserfs_journal_desc *desc;
L
Linus Torvalds 已提交
551

552 553
	if (memcmp(get_journal_desc_magic(bh), JOURNAL_DESC_MAGIC, 8))
		return 1;
L
Linus Torvalds 已提交
554

555 556 557 558
	desc = (struct reiserfs_journal_desc *)(bh->b_data);
	printk("Desc block %llu (j_trans_id %d, j_mount_id %d, j_len %d)",
	       (unsigned long long)bh->b_blocknr, get_desc_trans_id(desc),
	       get_desc_mount_id(desc), get_desc_trans_len(desc));
L
Linus Torvalds 已提交
559

560
	return 0;
L
Linus Torvalds 已提交
561 562
}

563
void print_block(struct buffer_head *bh, ...)	//int print_mode, int first, int last)
L
Linus Torvalds 已提交
564
{
565 566
	va_list args;
	int mode, first, last;
L
Linus Torvalds 已提交
567

568
	va_start(args, bh);
L
Linus Torvalds 已提交
569

570 571 572 573
	if (!bh) {
		printk("print_block: buffer is NULL\n");
		return;
	}
L
Linus Torvalds 已提交
574

575 576 577 578 579 580 581 582 583 584
	mode = va_arg(args, int);
	first = va_arg(args, int);
	last = va_arg(args, int);
	if (print_leaf(bh, mode, first, last))
		if (print_internal(bh, first, last))
			if (print_super_block(bh))
				if (print_desc_block(bh))
					printk
					    ("Block %llu contains unformatted data\n",
					     (unsigned long long)bh->b_blocknr);
585 586

	va_end(args);
L
Linus Torvalds 已提交
587 588 589 590 591
}

static char print_tb_buf[2048];

/* this stores initial state of tree balance in the print_tb_buf */
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
void store_print_tb(struct tree_balance *tb)
{
	int h = 0;
	int i;
	struct buffer_head *tbSh, *tbFh;

	if (!tb)
		return;

	sprintf(print_tb_buf, "\n"
		"BALANCING %d\n"
		"MODE=%c, ITEM_POS=%d POS_IN_ITEM=%d\n"
		"=====================================================================\n"
		"* h *    S    *    L    *    R    *   F   *   FL  *   FR  *  CFL  *  CFR  *\n",
		REISERFS_SB(tb->tb_sb)->s_do_balance,
		tb->tb_mode, PATH_LAST_POSITION(tb->tb_path),
		tb->tb_path->pos_in_item);

610
	for (h = 0; h < ARRAY_SIZE(tb->insert_size); h++) {
611 612 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
		if (PATH_H_PATH_OFFSET(tb->tb_path, h) <=
		    tb->tb_path->path_length
		    && PATH_H_PATH_OFFSET(tb->tb_path,
					  h) > ILLEGAL_PATH_ELEMENT_OFFSET) {
			tbSh = PATH_H_PBUFFER(tb->tb_path, h);
			tbFh = PATH_H_PPARENT(tb->tb_path, h);
		} else {
			tbSh = NULL;
			tbFh = NULL;
		}
		sprintf(print_tb_buf + strlen(print_tb_buf),
			"* %d * %3lld(%2d) * %3lld(%2d) * %3lld(%2d) * %5lld * %5lld * %5lld * %5lld * %5lld *\n",
			h,
			(tbSh) ? (long long)(tbSh->b_blocknr) : (-1LL),
			(tbSh) ? atomic_read(&(tbSh->b_count)) : -1,
			(tb->L[h]) ? (long long)(tb->L[h]->b_blocknr) : (-1LL),
			(tb->L[h]) ? atomic_read(&(tb->L[h]->b_count)) : -1,
			(tb->R[h]) ? (long long)(tb->R[h]->b_blocknr) : (-1LL),
			(tb->R[h]) ? atomic_read(&(tb->R[h]->b_count)) : -1,
			(tbFh) ? (long long)(tbFh->b_blocknr) : (-1LL),
			(tb->FL[h]) ? (long long)(tb->FL[h]->
						  b_blocknr) : (-1LL),
			(tb->FR[h]) ? (long long)(tb->FR[h]->
						  b_blocknr) : (-1LL),
			(tb->CFL[h]) ? (long long)(tb->CFL[h]->
						   b_blocknr) : (-1LL),
			(tb->CFR[h]) ? (long long)(tb->CFR[h]->
						   b_blocknr) : (-1LL));
L
Linus Torvalds 已提交
639 640
	}

641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
	sprintf(print_tb_buf + strlen(print_tb_buf),
		"=====================================================================\n"
		"* h * size * ln * lb * rn * rb * blkn * s0 * s1 * s1b * s2 * s2b * curb * lk * rk *\n"
		"* 0 * %4d * %2d * %2d * %2d * %2d * %4d * %2d * %2d * %3d * %2d * %3d * %4d * %2d * %2d *\n",
		tb->insert_size[0], tb->lnum[0], tb->lbytes, tb->rnum[0],
		tb->rbytes, tb->blknum[0], tb->s0num, tb->s1num, tb->s1bytes,
		tb->s2num, tb->s2bytes, tb->cur_blknum, tb->lkey[0],
		tb->rkey[0]);

	/* this prints balance parameters for non-leaf levels */
	h = 0;
	do {
		h++;
		sprintf(print_tb_buf + strlen(print_tb_buf),
			"* %d * %4d * %2d *    * %2d *    * %2d *\n",
			h, tb->insert_size[h], tb->lnum[h], tb->rnum[h],
			tb->blknum[h]);
	} while (tb->insert_size[h]);
L
Linus Torvalds 已提交
659

660 661 662
	sprintf(print_tb_buf + strlen(print_tb_buf),
		"=====================================================================\n"
		"FEB list: ");
L
Linus Torvalds 已提交
663

664 665
	/* print FEB list (list of buffers in form (bh (b_blocknr, b_count), that will be used for new nodes) */
	h = 0;
666
	for (i = 0; i < ARRAY_SIZE(tb->FEB); i++)
667 668 669 670 671
		sprintf(print_tb_buf + strlen(print_tb_buf),
			"%p (%llu %d)%s", tb->FEB[i],
			tb->FEB[i] ? (unsigned long long)tb->FEB[i]->
			b_blocknr : 0ULL,
			tb->FEB[i] ? atomic_read(&(tb->FEB[i]->b_count)) : 0,
672
			(i == ARRAY_SIZE(tb->FEB) - 1) ? "\n" : ", ");
L
Linus Torvalds 已提交
673

674 675
	sprintf(print_tb_buf + strlen(print_tb_buf),
		"======================== the end ====================================\n");
L
Linus Torvalds 已提交
676 677
}

678 679 680 681
void print_cur_tb(char *mes)
{
	printk("%s\n%s", mes, print_tb_buf);
}
L
Linus Torvalds 已提交
682

683
static void check_leaf_block_head(struct buffer_head *bh)
L
Linus Torvalds 已提交
684
{
685 686 687 688 689 690
	struct block_head *blkh;
	int nr;

	blkh = B_BLK_HEAD(bh);
	nr = blkh_nr_item(blkh);
	if (nr > (bh->b_size - BLKH_SIZE) / IH_SIZE)
J
Jeff Mahoney 已提交
691
		reiserfs_panic(NULL, "vs-6010", "invalid item number %z",
692 693
			       bh);
	if (blkh_free_space(blkh) > bh->b_size - BLKH_SIZE - IH_SIZE * nr)
J
Jeff Mahoney 已提交
694
		reiserfs_panic(NULL, "vs-6020", "invalid free space %z",
695
			       bh);
L
Linus Torvalds 已提交
696 697 698

}

699 700 701 702 703 704
static void check_internal_block_head(struct buffer_head *bh)
{
	struct block_head *blkh;

	blkh = B_BLK_HEAD(bh);
	if (!(B_LEVEL(bh) > DISK_LEAF_NODE_LEVEL && B_LEVEL(bh) <= MAX_HEIGHT))
J
Jeff Mahoney 已提交
705
		reiserfs_panic(NULL, "vs-6025", "invalid level %z", bh);
706 707

	if (B_NR_ITEMS(bh) > (bh->b_size - BLKH_SIZE) / IH_SIZE)
J
Jeff Mahoney 已提交
708
		reiserfs_panic(NULL, "vs-6030", "invalid item number %z", bh);
709 710 711 712

	if (B_FREE_SPACE(bh) !=
	    bh->b_size - BLKH_SIZE - KEY_SIZE * B_NR_ITEMS(bh) -
	    DC_SIZE * (B_NR_ITEMS(bh) + 1))
J
Jeff Mahoney 已提交
713
		reiserfs_panic(NULL, "vs-6040", "invalid free space %z", bh);
714 715

}
L
Linus Torvalds 已提交
716

717
void check_leaf(struct buffer_head *bh)
L
Linus Torvalds 已提交
718
{
719 720 721 722 723 724 725 726
	int i;
	struct item_head *ih;

	if (!bh)
		return;
	check_leaf_block_head(bh);
	for (i = 0, ih = B_N_PITEM_HEAD(bh, 0); i < B_NR_ITEMS(bh); i++, ih++)
		op_check_item(ih, B_I_PITEM(bh, ih));
L
Linus Torvalds 已提交
727 728
}

729 730 731 732 733 734
void check_internal(struct buffer_head *bh)
{
	if (!bh)
		return;
	check_internal_block_head(bh);
}
L
Linus Torvalds 已提交
735

736
void print_statistics(struct super_block *s)
L
Linus Torvalds 已提交
737 738
{

739 740 741 742 743 744 745
	/*
	   printk ("reiserfs_put_super: session statistics: balances %d, fix_nodes %d, \
	   bmap with search %d, without %d, dir2ind %d, ind2dir %d\n",
	   REISERFS_SB(s)->s_do_balance, REISERFS_SB(s)->s_fix_nodes,
	   REISERFS_SB(s)->s_bmaps, REISERFS_SB(s)->s_bmaps_without_search,
	   REISERFS_SB(s)->s_direct2indirect, REISERFS_SB(s)->s_indirect2direct);
	 */
L
Linus Torvalds 已提交
746 747

}