debug.c 86.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * This file is part of UBIFS.
 *
 * Copyright (C) 2006-2008 Nokia Corporation
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * Authors: Artem Bityutskiy (Битюцкий Артём)
 *          Adrian Hunter
 */

/*
 * This file implements most of the debugging stuff which is compiled in only
 * when it is enabled. But some debugging check functions are implemented in
 * corresponding subsystem, just because they are closely related and utilize
 * various local functions of those subsystems.
 */

#include <linux/module.h>
A
Artem Bityutskiy 已提交
31
#include <linux/debugfs.h>
A
Artem Bityutskiy 已提交
32
#include <linux/math64.h>
33
#include <linux/uaccess.h>
34 35
#include <linux/random.h>
#include "ubifs.h"
36

37
static DEFINE_SPINLOCK(dbg_lock);
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

static const char *get_key_fmt(int fmt)
{
	switch (fmt) {
	case UBIFS_SIMPLE_KEY_FMT:
		return "simple";
	default:
		return "unknown/invalid format";
	}
}

static const char *get_key_hash(int hash)
{
	switch (hash) {
	case UBIFS_KEY_HASH_R5:
		return "R5";
	case UBIFS_KEY_HASH_TEST:
		return "test";
	default:
		return "unknown/invalid name hash";
	}
}

static const char *get_key_type(int type)
{
	switch (type) {
	case UBIFS_INO_KEY:
		return "inode";
	case UBIFS_DENT_KEY:
		return "direntry";
	case UBIFS_XENT_KEY:
		return "xentry";
	case UBIFS_DATA_KEY:
		return "data";
	case UBIFS_TRUN_KEY:
		return "truncate";
	default:
		return "unknown/invalid key";
	}
}

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
static const char *get_dent_type(int type)
{
	switch (type) {
	case UBIFS_ITYPE_REG:
		return "file";
	case UBIFS_ITYPE_DIR:
		return "dir";
	case UBIFS_ITYPE_LNK:
		return "symlink";
	case UBIFS_ITYPE_BLK:
		return "blkdev";
	case UBIFS_ITYPE_CHR:
		return "char dev";
	case UBIFS_ITYPE_FIFO:
		return "fifo";
	case UBIFS_ITYPE_SOCK:
		return "socket";
	default:
		return "unknown/invalid type";
	}
}

A
Artem Bityutskiy 已提交
101 102
const char *dbg_snprintf_key(const struct ubifs_info *c,
			     const union ubifs_key *key, char *buffer, int len)
103 104 105 106 107 108 109
{
	char *p = buffer;
	int type = key_type(c, key);

	if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
		switch (type) {
		case UBIFS_INO_KEY:
110 111 112
			len -= snprintf(p, len, "(%lu, %s)",
					(unsigned long)key_inum(c, key),
					get_key_type(type));
113 114 115
			break;
		case UBIFS_DENT_KEY:
		case UBIFS_XENT_KEY:
116 117 118
			len -= snprintf(p, len, "(%lu, %s, %#08x)",
					(unsigned long)key_inum(c, key),
					get_key_type(type), key_hash(c, key));
119 120
			break;
		case UBIFS_DATA_KEY:
121 122 123
			len -= snprintf(p, len, "(%lu, %s, %u)",
					(unsigned long)key_inum(c, key),
					get_key_type(type), key_block(c, key));
124 125
			break;
		case UBIFS_TRUN_KEY:
126 127 128
			len -= snprintf(p, len, "(%lu, %s)",
					(unsigned long)key_inum(c, key),
					get_key_type(type));
129 130
			break;
		default:
131 132
			len -= snprintf(p, len, "(bad key type: %#08x, %#08x)",
					key->u32[0], key->u32[1]);
133 134
		}
	} else
135 136
		len -= snprintf(p, len, "bad key format %d", c->key_fmt);
	ubifs_assert(len > 0);
A
Artem Bityutskiy 已提交
137
	return p;
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
}

const char *dbg_ntype(int type)
{
	switch (type) {
	case UBIFS_PAD_NODE:
		return "padding node";
	case UBIFS_SB_NODE:
		return "superblock node";
	case UBIFS_MST_NODE:
		return "master node";
	case UBIFS_REF_NODE:
		return "reference node";
	case UBIFS_INO_NODE:
		return "inode node";
	case UBIFS_DENT_NODE:
		return "direntry node";
	case UBIFS_XENT_NODE:
		return "xentry node";
	case UBIFS_DATA_NODE:
		return "data node";
	case UBIFS_TRUN_NODE:
		return "truncate node";
	case UBIFS_IDX_NODE:
		return "indexing node";
	case UBIFS_CS_NODE:
		return "commit start node";
	case UBIFS_ORPH_NODE:
		return "orphan node";
	default:
		return "unknown node";
	}
}

static const char *dbg_gtype(int type)
{
	switch (type) {
	case UBIFS_NO_NODE_GROUP:
		return "no node group";
	case UBIFS_IN_NODE_GROUP:
		return "in node group";
	case UBIFS_LAST_OF_NODE_GROUP:
		return "last of node group";
	default:
		return "unknown";
	}
}

const char *dbg_cstate(int cmt_state)
{
	switch (cmt_state) {
	case COMMIT_RESTING:
		return "commit resting";
	case COMMIT_BACKGROUND:
		return "background commit requested";
	case COMMIT_REQUIRED:
		return "commit required";
	case COMMIT_RUNNING_BACKGROUND:
		return "BACKGROUND commit running";
	case COMMIT_RUNNING_REQUIRED:
		return "commit running and required";
	case COMMIT_BROKEN:
		return "broken commit";
	default:
		return "unknown commit state";
	}
}

206 207 208 209 210 211 212 213 214 215 216 217 218 219
const char *dbg_jhead(int jhead)
{
	switch (jhead) {
	case GCHD:
		return "0 (GC)";
	case BASEHD:
		return "1 (base)";
	case DATAHD:
		return "2 (data)";
	default:
		return "unknown journal head";
	}
}

220 221
static void dump_ch(const struct ubifs_ch *ch)
{
222 223 224
	printk(KERN_ERR "\tmagic          %#x\n", le32_to_cpu(ch->magic));
	printk(KERN_ERR "\tcrc            %#x\n", le32_to_cpu(ch->crc));
	printk(KERN_ERR "\tnode_type      %d (%s)\n", ch->node_type,
225
	       dbg_ntype(ch->node_type));
226
	printk(KERN_ERR "\tgroup_type     %d (%s)\n", ch->group_type,
227
	       dbg_gtype(ch->group_type));
228
	printk(KERN_ERR "\tsqnum          %llu\n",
229
	       (unsigned long long)le64_to_cpu(ch->sqnum));
230
	printk(KERN_ERR "\tlen            %u\n", le32_to_cpu(ch->len));
231 232
}

233
void ubifs_dump_inode(struct ubifs_info *c, const struct inode *inode)
234 235
{
	const struct ubifs_inode *ui = ubifs_inode(inode);
236 237 238 239
	struct qstr nm = { .name = NULL };
	union ubifs_key key;
	struct ubifs_dent_node *dent, *pdent = NULL;
	int count = 2;
240

241 242 243
	printk(KERN_ERR "Dump in-memory inode:");
	printk(KERN_ERR "\tinode          %lu\n", inode->i_ino);
	printk(KERN_ERR "\tsize           %llu\n",
244
	       (unsigned long long)i_size_read(inode));
245 246 247 248
	printk(KERN_ERR "\tnlink          %u\n", inode->i_nlink);
	printk(KERN_ERR "\tuid            %u\n", (unsigned int)inode->i_uid);
	printk(KERN_ERR "\tgid            %u\n", (unsigned int)inode->i_gid);
	printk(KERN_ERR "\tatime          %u.%u\n",
249 250
	       (unsigned int)inode->i_atime.tv_sec,
	       (unsigned int)inode->i_atime.tv_nsec);
251
	printk(KERN_ERR "\tmtime          %u.%u\n",
252 253
	       (unsigned int)inode->i_mtime.tv_sec,
	       (unsigned int)inode->i_mtime.tv_nsec);
254
	printk(KERN_ERR "\tctime          %u.%u\n",
255 256
	       (unsigned int)inode->i_ctime.tv_sec,
	       (unsigned int)inode->i_ctime.tv_nsec);
257 258 259 260 261 262 263 264
	printk(KERN_ERR "\tcreat_sqnum    %llu\n", ui->creat_sqnum);
	printk(KERN_ERR "\txattr_size     %u\n", ui->xattr_size);
	printk(KERN_ERR "\txattr_cnt      %u\n", ui->xattr_cnt);
	printk(KERN_ERR "\txattr_names    %u\n", ui->xattr_names);
	printk(KERN_ERR "\tdirty          %u\n", ui->dirty);
	printk(KERN_ERR "\txattr          %u\n", ui->xattr);
	printk(KERN_ERR "\tbulk_read      %u\n", ui->xattr);
	printk(KERN_ERR "\tsynced_i_size  %llu\n",
A
Artem Bityutskiy 已提交
265
	       (unsigned long long)ui->synced_i_size);
266
	printk(KERN_ERR "\tui_size        %llu\n",
A
Artem Bityutskiy 已提交
267
	       (unsigned long long)ui->ui_size);
268 269 270 271 272
	printk(KERN_ERR "\tflags          %d\n", ui->flags);
	printk(KERN_ERR "\tcompr_type     %d\n", ui->compr_type);
	printk(KERN_ERR "\tlast_page_read %lu\n", ui->last_page_read);
	printk(KERN_ERR "\tread_in_a_row  %lu\n", ui->read_in_a_row);
	printk(KERN_ERR "\tdata_len       %d\n", ui->data_len);
273 274 275 276

	if (!S_ISDIR(inode->i_mode))
		return;

277
	printk(KERN_ERR "List of directory entries:\n");
278 279 280 281 282 283 284
	ubifs_assert(!mutex_is_locked(&c->tnc_mutex));

	lowest_dent_key(c, &key, inode->i_ino);
	while (1) {
		dent = ubifs_tnc_next_ent(c, &key, &nm);
		if (IS_ERR(dent)) {
			if (PTR_ERR(dent) != -ENOENT)
285
				printk(KERN_ERR "error %ld\n", PTR_ERR(dent));
286 287 288
			break;
		}

289
		printk(KERN_ERR "\t%d: %s (%s)\n",
290 291 292 293 294 295 296 297 298
		       count++, dent->name, get_dent_type(dent->type));

		nm.name = dent->name;
		nm.len = le16_to_cpu(dent->nlen);
		kfree(pdent);
		pdent = dent;
		key_read(c, &dent->key, &key);
	}
	kfree(pdent);
299 300
}

301
void ubifs_dump_node(const struct ubifs_info *c, const void *node)
302 303 304 305
{
	int i, n;
	union ubifs_key key;
	const struct ubifs_ch *ch = node;
A
Artem Bityutskiy 已提交
306
	char key_buf[DBG_KEY_BUF_LEN];
307

308
	if (dbg_is_tst_rcvry(c))
309 310 311 312
		return;

	/* If the magic is incorrect, just hexdump the first bytes */
	if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
313 314
		printk(KERN_ERR "Not a node, first %zu bytes:", UBIFS_CH_SZ);
		print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 32, 1,
315 316 317 318 319 320 321 322 323 324 325 326
			       (void *)node, UBIFS_CH_SZ, 1);
		return;
	}

	spin_lock(&dbg_lock);
	dump_ch(node);

	switch (ch->node_type) {
	case UBIFS_PAD_NODE:
	{
		const struct ubifs_pad_node *pad = node;

327
		printk(KERN_ERR "\tpad_len        %u\n",
328 329 330 331 332 333 334 335
		       le32_to_cpu(pad->pad_len));
		break;
	}
	case UBIFS_SB_NODE:
	{
		const struct ubifs_sb_node *sup = node;
		unsigned int sup_flags = le32_to_cpu(sup->flags);

336
		printk(KERN_ERR "\tkey_hash       %d (%s)\n",
337
		       (int)sup->key_hash, get_key_hash(sup->key_hash));
338
		printk(KERN_ERR "\tkey_fmt        %d (%s)\n",
339
		       (int)sup->key_fmt, get_key_fmt(sup->key_fmt));
340 341
		printk(KERN_ERR "\tflags          %#x\n", sup_flags);
		printk(KERN_ERR "\t  big_lpt      %u\n",
342
		       !!(sup_flags & UBIFS_FLG_BIGLPT));
343
		printk(KERN_ERR "\t  space_fixup  %u\n",
344
		       !!(sup_flags & UBIFS_FLG_SPACE_FIXUP));
345
		printk(KERN_ERR "\tmin_io_size    %u\n",
346
		       le32_to_cpu(sup->min_io_size));
347
		printk(KERN_ERR "\tleb_size       %u\n",
348
		       le32_to_cpu(sup->leb_size));
349
		printk(KERN_ERR "\tleb_cnt        %u\n",
350
		       le32_to_cpu(sup->leb_cnt));
351
		printk(KERN_ERR "\tmax_leb_cnt    %u\n",
352
		       le32_to_cpu(sup->max_leb_cnt));
353
		printk(KERN_ERR "\tmax_bud_bytes  %llu\n",
354
		       (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
355
		printk(KERN_ERR "\tlog_lebs       %u\n",
356
		       le32_to_cpu(sup->log_lebs));
357
		printk(KERN_ERR "\tlpt_lebs       %u\n",
358
		       le32_to_cpu(sup->lpt_lebs));
359
		printk(KERN_ERR "\torph_lebs      %u\n",
360
		       le32_to_cpu(sup->orph_lebs));
361
		printk(KERN_ERR "\tjhead_cnt      %u\n",
362
		       le32_to_cpu(sup->jhead_cnt));
363
		printk(KERN_ERR "\tfanout         %u\n",
364
		       le32_to_cpu(sup->fanout));
365
		printk(KERN_ERR "\tlsave_cnt      %u\n",
366
		       le32_to_cpu(sup->lsave_cnt));
367
		printk(KERN_ERR "\tdefault_compr  %u\n",
368
		       (int)le16_to_cpu(sup->default_compr));
369
		printk(KERN_ERR "\trp_size        %llu\n",
370
		       (unsigned long long)le64_to_cpu(sup->rp_size));
371
		printk(KERN_ERR "\trp_uid         %u\n",
372
		       le32_to_cpu(sup->rp_uid));
373
		printk(KERN_ERR "\trp_gid         %u\n",
374
		       le32_to_cpu(sup->rp_gid));
375
		printk(KERN_ERR "\tfmt_version    %u\n",
376
		       le32_to_cpu(sup->fmt_version));
377
		printk(KERN_ERR "\ttime_gran      %u\n",
378
		       le32_to_cpu(sup->time_gran));
379
		printk(KERN_ERR "\tUUID           %pUB\n",
J
Joe Perches 已提交
380
		       sup->uuid);
381 382 383 384 385 386
		break;
	}
	case UBIFS_MST_NODE:
	{
		const struct ubifs_mst_node *mst = node;

387
		printk(KERN_ERR "\thighest_inum   %llu\n",
388
		       (unsigned long long)le64_to_cpu(mst->highest_inum));
389
		printk(KERN_ERR "\tcommit number  %llu\n",
390
		       (unsigned long long)le64_to_cpu(mst->cmt_no));
391
		printk(KERN_ERR "\tflags          %#x\n",
392
		       le32_to_cpu(mst->flags));
393
		printk(KERN_ERR "\tlog_lnum       %u\n",
394
		       le32_to_cpu(mst->log_lnum));
395
		printk(KERN_ERR "\troot_lnum      %u\n",
396
		       le32_to_cpu(mst->root_lnum));
397
		printk(KERN_ERR "\troot_offs      %u\n",
398
		       le32_to_cpu(mst->root_offs));
399
		printk(KERN_ERR "\troot_len       %u\n",
400
		       le32_to_cpu(mst->root_len));
401
		printk(KERN_ERR "\tgc_lnum        %u\n",
402
		       le32_to_cpu(mst->gc_lnum));
403
		printk(KERN_ERR "\tihead_lnum     %u\n",
404
		       le32_to_cpu(mst->ihead_lnum));
405
		printk(KERN_ERR "\tihead_offs     %u\n",
406
		       le32_to_cpu(mst->ihead_offs));
407
		printk(KERN_ERR "\tindex_size     %llu\n",
408
		       (unsigned long long)le64_to_cpu(mst->index_size));
409
		printk(KERN_ERR "\tlpt_lnum       %u\n",
410
		       le32_to_cpu(mst->lpt_lnum));
411
		printk(KERN_ERR "\tlpt_offs       %u\n",
412
		       le32_to_cpu(mst->lpt_offs));
413
		printk(KERN_ERR "\tnhead_lnum     %u\n",
414
		       le32_to_cpu(mst->nhead_lnum));
415
		printk(KERN_ERR "\tnhead_offs     %u\n",
416
		       le32_to_cpu(mst->nhead_offs));
417
		printk(KERN_ERR "\tltab_lnum      %u\n",
418
		       le32_to_cpu(mst->ltab_lnum));
419
		printk(KERN_ERR "\tltab_offs      %u\n",
420
		       le32_to_cpu(mst->ltab_offs));
421
		printk(KERN_ERR "\tlsave_lnum     %u\n",
422
		       le32_to_cpu(mst->lsave_lnum));
423
		printk(KERN_ERR "\tlsave_offs     %u\n",
424
		       le32_to_cpu(mst->lsave_offs));
425
		printk(KERN_ERR "\tlscan_lnum     %u\n",
426
		       le32_to_cpu(mst->lscan_lnum));
427
		printk(KERN_ERR "\tleb_cnt        %u\n",
428
		       le32_to_cpu(mst->leb_cnt));
429
		printk(KERN_ERR "\tempty_lebs     %u\n",
430
		       le32_to_cpu(mst->empty_lebs));
431
		printk(KERN_ERR "\tidx_lebs       %u\n",
432
		       le32_to_cpu(mst->idx_lebs));
433
		printk(KERN_ERR "\ttotal_free     %llu\n",
434
		       (unsigned long long)le64_to_cpu(mst->total_free));
435
		printk(KERN_ERR "\ttotal_dirty    %llu\n",
436
		       (unsigned long long)le64_to_cpu(mst->total_dirty));
437
		printk(KERN_ERR "\ttotal_used     %llu\n",
438
		       (unsigned long long)le64_to_cpu(mst->total_used));
439
		printk(KERN_ERR "\ttotal_dead     %llu\n",
440
		       (unsigned long long)le64_to_cpu(mst->total_dead));
441
		printk(KERN_ERR "\ttotal_dark     %llu\n",
442 443 444 445 446 447 448
		       (unsigned long long)le64_to_cpu(mst->total_dark));
		break;
	}
	case UBIFS_REF_NODE:
	{
		const struct ubifs_ref_node *ref = node;

449
		printk(KERN_ERR "\tlnum           %u\n",
450
		       le32_to_cpu(ref->lnum));
451
		printk(KERN_ERR "\toffs           %u\n",
452
		       le32_to_cpu(ref->offs));
453
		printk(KERN_ERR "\tjhead          %u\n",
454 455 456 457 458 459 460 461
		       le32_to_cpu(ref->jhead));
		break;
	}
	case UBIFS_INO_NODE:
	{
		const struct ubifs_ino_node *ino = node;

		key_read(c, &ino->key, &key);
462
		printk(KERN_ERR "\tkey            %s\n",
A
Artem Bityutskiy 已提交
463
		       dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
464
		printk(KERN_ERR "\tcreat_sqnum    %llu\n",
465
		       (unsigned long long)le64_to_cpu(ino->creat_sqnum));
466
		printk(KERN_ERR "\tsize           %llu\n",
467
		       (unsigned long long)le64_to_cpu(ino->size));
468
		printk(KERN_ERR "\tnlink          %u\n",
469
		       le32_to_cpu(ino->nlink));
470
		printk(KERN_ERR "\tatime          %lld.%u\n",
471 472
		       (long long)le64_to_cpu(ino->atime_sec),
		       le32_to_cpu(ino->atime_nsec));
473
		printk(KERN_ERR "\tmtime          %lld.%u\n",
474 475
		       (long long)le64_to_cpu(ino->mtime_sec),
		       le32_to_cpu(ino->mtime_nsec));
476
		printk(KERN_ERR "\tctime          %lld.%u\n",
477 478
		       (long long)le64_to_cpu(ino->ctime_sec),
		       le32_to_cpu(ino->ctime_nsec));
479
		printk(KERN_ERR "\tuid            %u\n",
480
		       le32_to_cpu(ino->uid));
481
		printk(KERN_ERR "\tgid            %u\n",
482
		       le32_to_cpu(ino->gid));
483
		printk(KERN_ERR "\tmode           %u\n",
484
		       le32_to_cpu(ino->mode));
485
		printk(KERN_ERR "\tflags          %#x\n",
486
		       le32_to_cpu(ino->flags));
487
		printk(KERN_ERR "\txattr_cnt      %u\n",
488
		       le32_to_cpu(ino->xattr_cnt));
489
		printk(KERN_ERR "\txattr_size     %u\n",
490
		       le32_to_cpu(ino->xattr_size));
491
		printk(KERN_ERR "\txattr_names    %u\n",
492
		       le32_to_cpu(ino->xattr_names));
493
		printk(KERN_ERR "\tcompr_type     %#x\n",
494
		       (int)le16_to_cpu(ino->compr_type));
495
		printk(KERN_ERR "\tdata len       %u\n",
496 497 498 499 500 501 502 503 504 505
		       le32_to_cpu(ino->data_len));
		break;
	}
	case UBIFS_DENT_NODE:
	case UBIFS_XENT_NODE:
	{
		const struct ubifs_dent_node *dent = node;
		int nlen = le16_to_cpu(dent->nlen);

		key_read(c, &dent->key, &key);
506
		printk(KERN_ERR "\tkey            %s\n",
A
Artem Bityutskiy 已提交
507
		       dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
508
		printk(KERN_ERR "\tinum           %llu\n",
509
		       (unsigned long long)le64_to_cpu(dent->inum));
510 511 512
		printk(KERN_ERR "\ttype           %d\n", (int)dent->type);
		printk(KERN_ERR "\tnlen           %d\n", nlen);
		printk(KERN_ERR "\tname           ");
513 514

		if (nlen > UBIFS_MAX_NLEN)
515
			printk(KERN_ERR "(bad name length, not printing, "
516 517 518
					  "bad or corrupted node)");
		else {
			for (i = 0; i < nlen && dent->name[i]; i++)
A
Artem Bityutskiy 已提交
519
				printk(KERN_CONT "%c", dent->name[i]);
520
		}
A
Artem Bityutskiy 已提交
521
		printk(KERN_CONT "\n");
522 523 524 525 526 527 528 529 530

		break;
	}
	case UBIFS_DATA_NODE:
	{
		const struct ubifs_data_node *dn = node;
		int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;

		key_read(c, &dn->key, &key);
531
		printk(KERN_ERR "\tkey            %s\n",
A
Artem Bityutskiy 已提交
532
		       dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
533
		printk(KERN_ERR "\tsize           %u\n",
534
		       le32_to_cpu(dn->size));
535
		printk(KERN_ERR "\tcompr_typ      %d\n",
536
		       (int)le16_to_cpu(dn->compr_type));
537
		printk(KERN_ERR "\tdata size      %d\n",
538
		       dlen);
539 540
		printk(KERN_ERR "\tdata:\n");
		print_hex_dump(KERN_ERR, "\t", DUMP_PREFIX_OFFSET, 32, 1,
541 542 543 544 545 546 547
			       (void *)&dn->data, dlen, 0);
		break;
	}
	case UBIFS_TRUN_NODE:
	{
		const struct ubifs_trun_node *trun = node;

548
		printk(KERN_ERR "\tinum           %u\n",
549
		       le32_to_cpu(trun->inum));
550
		printk(KERN_ERR "\told_size       %llu\n",
551
		       (unsigned long long)le64_to_cpu(trun->old_size));
552
		printk(KERN_ERR "\tnew_size       %llu\n",
553 554 555 556 557 558 559 560
		       (unsigned long long)le64_to_cpu(trun->new_size));
		break;
	}
	case UBIFS_IDX_NODE:
	{
		const struct ubifs_idx_node *idx = node;

		n = le16_to_cpu(idx->child_cnt);
561 562
		printk(KERN_ERR "\tchild_cnt      %d\n", n);
		printk(KERN_ERR "\tlevel          %d\n",
563
		       (int)le16_to_cpu(idx->level));
564
		printk(KERN_ERR "\tBranches:\n");
565 566 567 568 569 570

		for (i = 0; i < n && i < c->fanout - 1; i++) {
			const struct ubifs_branch *br;

			br = ubifs_idx_branch(c, idx, i);
			key_read(c, &br->key, &key);
571
			printk(KERN_ERR "\t%d: LEB %d:%d len %d key %s\n",
572
			       i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
A
Artem Bityutskiy 已提交
573 574 575
			       le32_to_cpu(br->len),
			       dbg_snprintf_key(c, &key, key_buf,
						DBG_KEY_BUF_LEN));
576 577 578 579 580 581 582 583 584
		}
		break;
	}
	case UBIFS_CS_NODE:
		break;
	case UBIFS_ORPH_NODE:
	{
		const struct ubifs_orph_node *orph = node;

585
		printk(KERN_ERR "\tcommit number  %llu\n",
586 587
		       (unsigned long long)
				le64_to_cpu(orph->cmt_no) & LLONG_MAX);
588
		printk(KERN_ERR "\tlast node flag %llu\n",
589 590
		       (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
		n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
591
		printk(KERN_ERR "\t%d orphan inode numbers:\n", n);
592
		for (i = 0; i < n; i++)
593
			printk(KERN_ERR "\t  ino %llu\n",
594
			       (unsigned long long)le64_to_cpu(orph->inos[i]));
595 596 597
		break;
	}
	default:
598
		printk(KERN_ERR "node type %d was not recognized\n",
599 600 601 602 603
		       (int)ch->node_type);
	}
	spin_unlock(&dbg_lock);
}

604
void ubifs_dump_budget_req(const struct ubifs_budget_req *req)
605 606
{
	spin_lock(&dbg_lock);
607
	printk(KERN_ERR "Budgeting request: new_ino %d, dirtied_ino %d\n",
608
	       req->new_ino, req->dirtied_ino);
609
	printk(KERN_ERR "\tnew_ino_d   %d, dirtied_ino_d %d\n",
610
	       req->new_ino_d, req->dirtied_ino_d);
611
	printk(KERN_ERR "\tnew_page    %d, dirtied_page %d\n",
612
	       req->new_page, req->dirtied_page);
613
	printk(KERN_ERR "\tnew_dent    %d, mod_dent     %d\n",
614
	       req->new_dent, req->mod_dent);
615 616
	printk(KERN_ERR "\tidx_growth  %d\n", req->idx_growth);
	printk(KERN_ERR "\tdata_growth %d dd_growth     %d\n",
617 618 619 620
	       req->data_growth, req->dd_growth);
	spin_unlock(&dbg_lock);
}

621
void ubifs_dump_lstats(const struct ubifs_lp_stats *lst)
622 623
{
	spin_lock(&dbg_lock);
624
	printk(KERN_ERR "(pid %d) Lprops statistics: empty_lebs %d, "
625
	       "idx_lebs  %d\n", current->pid, lst->empty_lebs, lst->idx_lebs);
626
	printk(KERN_ERR "\ttaken_empty_lebs %d, total_free %lld, "
627 628
	       "total_dirty %lld\n", lst->taken_empty_lebs, lst->total_free,
	       lst->total_dirty);
629
	printk(KERN_ERR "\ttotal_used %lld, total_dark %lld, "
630 631 632 633 634
	       "total_dead %lld\n", lst->total_used, lst->total_dark,
	       lst->total_dead);
	spin_unlock(&dbg_lock);
}

635
void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)
636 637 638 639 640
{
	int i;
	struct rb_node *rb;
	struct ubifs_bud *bud;
	struct ubifs_gced_idx_leb *idx_gc;
A
Artem Bityutskiy 已提交
641
	long long available, outstanding, free;
642

643
	spin_lock(&c->space_lock);
644
	spin_lock(&dbg_lock);
645
	printk(KERN_ERR "(pid %d) Budgeting info: data budget sum %lld, "
646
	       "total budget sum %lld\n", current->pid,
647 648
	       bi->data_growth + bi->dd_growth,
	       bi->data_growth + bi->dd_growth + bi->idx_growth);
649
	printk(KERN_ERR "\tbudg_data_growth %lld, budg_dd_growth %lld, "
650 651
	       "budg_idx_growth %lld\n", bi->data_growth, bi->dd_growth,
	       bi->idx_growth);
652
	printk(KERN_ERR "\tmin_idx_lebs %d, old_idx_sz %llu, "
653 654
	       "uncommitted_idx %lld\n", bi->min_idx_lebs, bi->old_idx_sz,
	       bi->uncommitted_idx);
655
	printk(KERN_ERR "\tpage_budget %d, inode_budget %d, dent_budget %d\n",
656
	       bi->page_budget, bi->inode_budget, bi->dent_budget);
657
	printk(KERN_ERR "\tnospace %u, nospace_rp %u\n",
658
	       bi->nospace, bi->nospace_rp);
659
	printk(KERN_ERR "\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
660
	       c->dark_wm, c->dead_wm, c->max_idx_node_sz);
661 662 663 664 665 666 667 668 669

	if (bi != &c->bi)
		/*
		 * If we are dumping saved budgeting data, do not print
		 * additional information which is about the current state, not
		 * the old one which corresponded to the saved budgeting data.
		 */
		goto out_unlock;

670
	printk(KERN_ERR "\tfreeable_cnt %d, calc_idx_sz %lld, idx_gc_cnt %d\n",
671
	       c->freeable_cnt, c->calc_idx_sz, c->idx_gc_cnt);
672
	printk(KERN_ERR "\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, "
673 674 675
	       "clean_zn_cnt %ld\n", atomic_long_read(&c->dirty_pg_cnt),
	       atomic_long_read(&c->dirty_zn_cnt),
	       atomic_long_read(&c->clean_zn_cnt));
676
	printk(KERN_ERR "\tgc_lnum %d, ihead_lnum %d\n",
677
	       c->gc_lnum, c->ihead_lnum);
678

679 680 681
	/* If we are in R/O mode, journal heads do not exist */
	if (c->jheads)
		for (i = 0; i < c->jhead_cnt; i++)
682
			printk(KERN_ERR "\tjhead %s\t LEB %d\n",
683 684
			       dbg_jhead(c->jheads[i].wbuf.jhead),
			       c->jheads[i].wbuf.lnum);
685 686
	for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
		bud = rb_entry(rb, struct ubifs_bud, rb);
687
		printk(KERN_ERR "\tbud LEB %d\n", bud->lnum);
688 689
	}
	list_for_each_entry(bud, &c->old_buds, list)
690
		printk(KERN_ERR "\told bud LEB %d\n", bud->lnum);
691
	list_for_each_entry(idx_gc, &c->idx_gc, list)
692
		printk(KERN_ERR "\tGC'ed idx LEB %d unmap %d\n",
693
		       idx_gc->lnum, idx_gc->unmap);
694
	printk(KERN_ERR "\tcommit state %d\n", c->cmt_state);
A
Artem Bityutskiy 已提交
695 696

	/* Print budgeting predictions */
697 698
	available = ubifs_calc_available(c, c->bi.min_idx_lebs);
	outstanding = c->bi.data_growth + c->bi.dd_growth;
699
	free = ubifs_get_free_space_nolock(c);
700 701
	printk(KERN_ERR "Budgeting predictions:\n");
	printk(KERN_ERR "\tavailable: %lld, outstanding %lld, free %lld\n",
A
Artem Bityutskiy 已提交
702
	       available, outstanding, free);
703
out_unlock:
704
	spin_unlock(&dbg_lock);
705
	spin_unlock(&c->space_lock);
706 707
}

708
void ubifs_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
709
{
A
Artem Bityutskiy 已提交
710 711 712 713 714 715 716 717 718 719 720
	int i, spc, dark = 0, dead = 0;
	struct rb_node *rb;
	struct ubifs_bud *bud;

	spc = lp->free + lp->dirty;
	if (spc < c->dead_wm)
		dead = spc;
	else
		dark = ubifs_calc_dark(c, spc);

	if (lp->flags & LPROPS_INDEX)
721
		printk(KERN_ERR "LEB %-7d free %-8d dirty %-8d used %-8d "
A
Artem Bityutskiy 已提交
722 723 724
		       "free + dirty %-8d flags %#x (", lp->lnum, lp->free,
		       lp->dirty, c->leb_size - spc, spc, lp->flags);
	else
725
		printk(KERN_ERR "LEB %-7d free %-8d dirty %-8d used %-8d "
A
Artem Bityutskiy 已提交
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
		       "free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d "
		       "flags %#-4x (", lp->lnum, lp->free, lp->dirty,
		       c->leb_size - spc, spc, dark, dead,
		       (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags);

	if (lp->flags & LPROPS_TAKEN) {
		if (lp->flags & LPROPS_INDEX)
			printk(KERN_CONT "index, taken");
		else
			printk(KERN_CONT "taken");
	} else {
		const char *s;

		if (lp->flags & LPROPS_INDEX) {
			switch (lp->flags & LPROPS_CAT_MASK) {
			case LPROPS_DIRTY_IDX:
				s = "dirty index";
				break;
			case LPROPS_FRDI_IDX:
				s = "freeable index";
				break;
			default:
				s = "index";
			}
		} else {
			switch (lp->flags & LPROPS_CAT_MASK) {
			case LPROPS_UNCAT:
				s = "not categorized";
				break;
			case LPROPS_DIRTY:
				s = "dirty";
				break;
			case LPROPS_FREE:
				s = "free";
				break;
			case LPROPS_EMPTY:
				s = "empty";
				break;
			case LPROPS_FREEABLE:
				s = "freeable";
				break;
			default:
				s = NULL;
				break;
			}
		}
		printk(KERN_CONT "%s", s);
	}

	for (rb = rb_first((struct rb_root *)&c->buds); rb; rb = rb_next(rb)) {
		bud = rb_entry(rb, struct ubifs_bud, rb);
		if (bud->lnum == lp->lnum) {
			int head = 0;
			for (i = 0; i < c->jhead_cnt; i++) {
780 781 782 783 784 785 786
				/*
				 * Note, if we are in R/O mode or in the middle
				 * of mounting/re-mounting, the write-buffers do
				 * not exist.
				 */
				if (c->jheads &&
				    lp->lnum == c->jheads[i].wbuf.lnum) {
A
Artem Bityutskiy 已提交
787 788 789 790 791 792 793 794 795 796 797 798 799
					printk(KERN_CONT ", jhead %s",
					       dbg_jhead(i));
					head = 1;
				}
			}
			if (!head)
				printk(KERN_CONT ", bud of jhead %s",
				       dbg_jhead(bud->jhead));
		}
	}
	if (lp->lnum == c->gc_lnum)
		printk(KERN_CONT ", GC LEB");
	printk(KERN_CONT ")\n");
800 801
}

802
void ubifs_dump_lprops(struct ubifs_info *c)
803 804 805 806 807
{
	int lnum, err;
	struct ubifs_lprops lp;
	struct ubifs_lp_stats lst;

808
	printk(KERN_ERR "(pid %d) start dumping LEB properties\n",
809
	       current->pid);
810
	ubifs_get_lp_stats(c, &lst);
811
	ubifs_dump_lstats(&lst);
812 813 814 815 816 817

	for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
		err = ubifs_read_one_lp(c, lnum, &lp);
		if (err)
			ubifs_err("cannot read lprops for LEB %d", lnum);

818
		ubifs_dump_lprop(c, &lp);
819
	}
820
	printk(KERN_ERR "(pid %d) finish dumping LEB properties\n",
821
	       current->pid);
822 823
}

824
void ubifs_dump_lpt_info(struct ubifs_info *c)
825 826 827 828
{
	int i;

	spin_lock(&dbg_lock);
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
	printk(KERN_ERR "(pid %d) dumping LPT information\n", current->pid);
	printk(KERN_ERR "\tlpt_sz:        %lld\n", c->lpt_sz);
	printk(KERN_ERR "\tpnode_sz:      %d\n", c->pnode_sz);
	printk(KERN_ERR "\tnnode_sz:      %d\n", c->nnode_sz);
	printk(KERN_ERR "\tltab_sz:       %d\n", c->ltab_sz);
	printk(KERN_ERR "\tlsave_sz:      %d\n", c->lsave_sz);
	printk(KERN_ERR "\tbig_lpt:       %d\n", c->big_lpt);
	printk(KERN_ERR "\tlpt_hght:      %d\n", c->lpt_hght);
	printk(KERN_ERR "\tpnode_cnt:     %d\n", c->pnode_cnt);
	printk(KERN_ERR "\tnnode_cnt:     %d\n", c->nnode_cnt);
	printk(KERN_ERR "\tdirty_pn_cnt:  %d\n", c->dirty_pn_cnt);
	printk(KERN_ERR "\tdirty_nn_cnt:  %d\n", c->dirty_nn_cnt);
	printk(KERN_ERR "\tlsave_cnt:     %d\n", c->lsave_cnt);
	printk(KERN_ERR "\tspace_bits:    %d\n", c->space_bits);
	printk(KERN_ERR "\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
	printk(KERN_ERR "\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
	printk(KERN_ERR "\tlpt_spc_bits:  %d\n", c->lpt_spc_bits);
	printk(KERN_ERR "\tpcnt_bits:     %d\n", c->pcnt_bits);
	printk(KERN_ERR "\tlnum_bits:     %d\n", c->lnum_bits);
	printk(KERN_ERR "\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
	printk(KERN_ERR "\tLPT head is at %d:%d\n",
850
	       c->nhead_lnum, c->nhead_offs);
851
	printk(KERN_ERR "\tLPT ltab is at %d:%d\n",
852
	       c->ltab_lnum, c->ltab_offs);
853
	if (c->big_lpt)
854
		printk(KERN_ERR "\tLPT lsave is at %d:%d\n",
855 856
		       c->lsave_lnum, c->lsave_offs);
	for (i = 0; i < c->lpt_lebs; i++)
857
		printk(KERN_ERR "\tLPT LEB %d free %d dirty %d tgc %d "
858 859 860 861 862
		       "cmt %d\n", i + c->lpt_first, c->ltab[i].free,
		       c->ltab[i].dirty, c->ltab[i].tgc, c->ltab[i].cmt);
	spin_unlock(&dbg_lock);
}

863 864
void ubifs_dump_sleb(const struct ubifs_info *c,
		     const struct ubifs_scan_leb *sleb, int offs)
865 866 867
{
	struct ubifs_scan_node *snod;

868
	printk(KERN_ERR "(pid %d) start dumping scanned data from LEB %d:%d\n",
869 870 871 872
	       current->pid, sleb->lnum, offs);

	list_for_each_entry(snod, &sleb->nodes, list) {
		cond_resched();
873
		printk(KERN_ERR "Dumping node at LEB %d:%d len %d\n", sleb->lnum,
874
		       snod->offs, snod->len);
875
		ubifs_dump_node(c, snod->node);
876 877 878
	}
}

879
void ubifs_dump_leb(const struct ubifs_info *c, int lnum)
880 881 882
{
	struct ubifs_scan_leb *sleb;
	struct ubifs_scan_node *snod;
883
	void *buf;
884

885
	if (dbg_is_tst_rcvry(c))
886 887
		return;

888
	printk(KERN_ERR "(pid %d) start dumping LEB %d\n",
889
	       current->pid, lnum);
890

A
Artem Bityutskiy 已提交
891
	buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
892 893 894 895 896 897
	if (!buf) {
		ubifs_err("cannot allocate memory for dumping LEB %d", lnum);
		return;
	}

	sleb = ubifs_scan(c, lnum, 0, buf, 0);
898 899
	if (IS_ERR(sleb)) {
		ubifs_err("scan error %d", (int)PTR_ERR(sleb));
900
		goto out;
901 902
	}

903
	printk(KERN_ERR "LEB %d has %d nodes ending at %d\n", lnum,
904 905 906 907
	       sleb->nodes_cnt, sleb->endpt);

	list_for_each_entry(snod, &sleb->nodes, list) {
		cond_resched();
908
		printk(KERN_ERR "Dumping node at LEB %d:%d len %d\n", lnum,
909
		       snod->offs, snod->len);
910
		ubifs_dump_node(c, snod->node);
911 912
	}

913
	printk(KERN_ERR "(pid %d) finish dumping LEB %d\n",
914
	       current->pid, lnum);
915
	ubifs_scan_destroy(sleb);
916 917 918

out:
	vfree(buf);
919 920 921
	return;
}

922 923
void ubifs_dump_znode(const struct ubifs_info *c,
		      const struct ubifs_znode *znode)
924 925 926
{
	int n;
	const struct ubifs_zbranch *zbr;
A
Artem Bityutskiy 已提交
927
	char key_buf[DBG_KEY_BUF_LEN];
928 929 930 931 932 933 934

	spin_lock(&dbg_lock);
	if (znode->parent)
		zbr = &znode->parent->zbranch[znode->iip];
	else
		zbr = &c->zroot;

935
	printk(KERN_ERR "znode %p, LEB %d:%d len %d parent %p iip %d level %d"
936 937 938 939 940 941 942 943 944
	       " child_cnt %d flags %lx\n", znode, zbr->lnum, zbr->offs,
	       zbr->len, znode->parent, znode->iip, znode->level,
	       znode->child_cnt, znode->flags);

	if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
		spin_unlock(&dbg_lock);
		return;
	}

945
	printk(KERN_ERR "zbranches:\n");
946 947 948
	for (n = 0; n < znode->child_cnt; n++) {
		zbr = &znode->zbranch[n];
		if (znode->level > 0)
949
			printk(KERN_ERR "\t%d: znode %p LEB %d:%d len %d key "
950 951
					  "%s\n", n, zbr->znode, zbr->lnum,
					  zbr->offs, zbr->len,
A
Artem Bityutskiy 已提交
952 953 954
					  dbg_snprintf_key(c, &zbr->key,
							   key_buf,
							   DBG_KEY_BUF_LEN));
955
		else
956
			printk(KERN_ERR "\t%d: LNC %p LEB %d:%d len %d key "
957 958
					  "%s\n", n, zbr->znode, zbr->lnum,
					  zbr->offs, zbr->len,
A
Artem Bityutskiy 已提交
959 960 961
					  dbg_snprintf_key(c, &zbr->key,
							   key_buf,
							   DBG_KEY_BUF_LEN));
962 963 964 965
	}
	spin_unlock(&dbg_lock);
}

966
void ubifs_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
967 968 969
{
	int i;

970
	printk(KERN_ERR "(pid %d) start dumping heap cat %d (%d elements)\n",
971
	       current->pid, cat, heap->cnt);
972 973 974
	for (i = 0; i < heap->cnt; i++) {
		struct ubifs_lprops *lprops = heap->arr[i];

975
		printk(KERN_ERR "\t%d. LEB %d hpos %d free %d dirty %d "
976 977 978
		       "flags %d\n", i, lprops->lnum, lprops->hpos,
		       lprops->free, lprops->dirty, lprops->flags);
	}
979
	printk(KERN_ERR "(pid %d) finish dumping heap\n", current->pid);
980 981
}

982 983
void ubifs_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
		      struct ubifs_nnode *parent, int iip)
984 985 986
{
	int i;

987 988
	printk(KERN_ERR "(pid %d) dumping pnode:\n", current->pid);
	printk(KERN_ERR "\taddress %zx parent %zx cnext %zx\n",
989
	       (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
990
	printk(KERN_ERR "\tflags %lu iip %d level %d num %d\n",
991 992 993 994
	       pnode->flags, iip, pnode->level, pnode->num);
	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
		struct ubifs_lprops *lp = &pnode->lprops[i];

995
		printk(KERN_ERR "\t%d: free %d dirty %d flags %d lnum %d\n",
996 997 998 999
		       i, lp->free, lp->dirty, lp->flags, lp->lnum);
	}
}

1000
void ubifs_dump_tnc(struct ubifs_info *c)
1001 1002 1003 1004
{
	struct ubifs_znode *znode;
	int level;

1005 1006
	printk(KERN_ERR "\n");
	printk(KERN_ERR "(pid %d) start dumping TNC tree\n", current->pid);
1007 1008
	znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
	level = znode->level;
1009
	printk(KERN_ERR "== Level %d ==\n", level);
1010 1011 1012
	while (znode) {
		if (level != znode->level) {
			level = znode->level;
1013
			printk(KERN_ERR "== Level %d ==\n", level);
1014
		}
1015
		ubifs_dump_znode(c, znode);
1016 1017
		znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
	}
1018
	printk(KERN_ERR "(pid %d) finish dumping TNC tree\n", current->pid);
1019 1020 1021 1022 1023
}

static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
		      void *priv)
{
1024
	ubifs_dump_znode(c, znode);
1025 1026 1027 1028
	return 0;
}

/**
1029
 * ubifs_dump_index - dump the on-flash index.
1030 1031
 * @c: UBIFS file-system description object
 *
1032
 * This function dumps whole UBIFS indexing B-tree, unlike 'ubifs_dump_tnc()'
1033 1034
 * which dumps only in-memory znodes and does not read znodes which from flash.
 */
1035
void ubifs_dump_index(struct ubifs_info *c)
1036 1037 1038 1039
{
	dbg_walk_index(c, NULL, dump_znode, NULL);
}

1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
/**
 * dbg_save_space_info - save information about flash space.
 * @c: UBIFS file-system description object
 *
 * This function saves information about UBIFS free space, dirty space, etc, in
 * order to check it later.
 */
void dbg_save_space_info(struct ubifs_info *c)
{
	struct ubifs_debug_info *d = c->dbg;
1050
	int freeable_cnt;
1051 1052

	spin_lock(&c->space_lock);
1053
	memcpy(&d->saved_lst, &c->lst, sizeof(struct ubifs_lp_stats));
1054 1055
	memcpy(&d->saved_bi, &c->bi, sizeof(struct ubifs_budg_info));
	d->saved_idx_gc_cnt = c->idx_gc_cnt;
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082

	/*
	 * We use a dirty hack here and zero out @c->freeable_cnt, because it
	 * affects the free space calculations, and UBIFS might not know about
	 * all freeable eraseblocks. Indeed, we know about freeable eraseblocks
	 * only when we read their lprops, and we do this only lazily, upon the
	 * need. So at any given point of time @c->freeable_cnt might be not
	 * exactly accurate.
	 *
	 * Just one example about the issue we hit when we did not zero
	 * @c->freeable_cnt.
	 * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the
	 *    amount of free space in @d->saved_free
	 * 2. We re-mount R/W, which makes UBIFS to read the "lsave"
	 *    information from flash, where we cache LEBs from various
	 *    categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()'
	 *    -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()'
	 *    -> 'ubifs_get_pnode()' -> 'update_cats()'
	 *    -> 'ubifs_add_to_cat()').
	 * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt
	 *    becomes %1.
	 * 4. We calculate the amount of free space when the re-mount is
	 *    finished in 'dbg_check_space_info()' and it does not match
	 *    @d->saved_free.
	 */
	freeable_cnt = c->freeable_cnt;
	c->freeable_cnt = 0;
1083
	d->saved_free = ubifs_get_free_space_nolock(c);
1084
	c->freeable_cnt = freeable_cnt;
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
	spin_unlock(&c->space_lock);
}

/**
 * dbg_check_space_info - check flash space information.
 * @c: UBIFS file-system description object
 *
 * This function compares current flash space information with the information
 * which was saved when the 'dbg_save_space_info()' function was called.
 * Returns zero if the information has not changed, and %-EINVAL it it has
 * changed.
 */
int dbg_check_space_info(struct ubifs_info *c)
{
	struct ubifs_debug_info *d = c->dbg;
	struct ubifs_lp_stats lst;
1101 1102
	long long free;
	int freeable_cnt;
1103 1104

	spin_lock(&c->space_lock);
1105 1106 1107 1108
	freeable_cnt = c->freeable_cnt;
	c->freeable_cnt = 0;
	free = ubifs_get_free_space_nolock(c);
	c->freeable_cnt = freeable_cnt;
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
	spin_unlock(&c->space_lock);

	if (free != d->saved_free) {
		ubifs_err("free space changed from %lld to %lld",
			  d->saved_free, free);
		goto out;
	}

	return 0;

out:
	ubifs_msg("saved lprops statistics dump");
1121
	ubifs_dump_lstats(&d->saved_lst);
1122
	ubifs_msg("saved budgeting info dump");
1123
	ubifs_dump_budg(c, &d->saved_bi);
1124
	ubifs_msg("saved idx_gc_cnt %d", d->saved_idx_gc_cnt);
1125
	ubifs_msg("current lprops statistics dump");
1126
	ubifs_get_lp_stats(c, &lst);
1127
	ubifs_dump_lstats(&lst);
1128
	ubifs_msg("current budgeting info dump");
1129
	ubifs_dump_budg(c, &c->bi);
1130 1131 1132 1133
	dump_stack();
	return -EINVAL;
}

1134 1135
/**
 * dbg_check_synced_i_size - check synchronized inode size.
1136
 * @c: UBIFS file-system description object
1137 1138 1139 1140 1141 1142 1143
 * @inode: inode to check
 *
 * If inode is clean, synchronized inode size has to be equivalent to current
 * inode size. This function has to be called only for locked inodes (@i_mutex
 * has to be locked). Returns %0 if synchronized inode size if correct, and
 * %-EINVAL if not.
 */
1144
int dbg_check_synced_i_size(const struct ubifs_info *c, struct inode *inode)
1145 1146 1147 1148
{
	int err = 0;
	struct ubifs_inode *ui = ubifs_inode(inode);

1149
	if (!dbg_is_chk_gen(c))
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
		return 0;
	if (!S_ISREG(inode->i_mode))
		return 0;

	mutex_lock(&ui->ui_mutex);
	spin_lock(&ui->ui_lock);
	if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
		ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode "
			  "is clean", ui->ui_size, ui->synced_i_size);
		ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
			  inode->i_mode, i_size_read(inode));
1161
		dump_stack();
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
		err = -EINVAL;
	}
	spin_unlock(&ui->ui_lock);
	mutex_unlock(&ui->ui_mutex);
	return err;
}

/*
 * dbg_check_dir - check directory inode size and link count.
 * @c: UBIFS file-system description object
 * @dir: the directory to calculate size for
 * @size: the result is returned here
 *
 * This function makes sure that directory size and link count are correct.
 * Returns zero in case of success and a negative error code in case of
 * failure.
 *
 * Note, it is good idea to make sure the @dir->i_mutex is locked before
 * calling this function.
 */
1182
int dbg_check_dir(struct ubifs_info *c, const struct inode *dir)
1183 1184 1185 1186 1187 1188 1189
{
	unsigned int nlink = 2;
	union ubifs_key key;
	struct ubifs_dent_node *dent, *pdent = NULL;
	struct qstr nm = { .name = NULL };
	loff_t size = UBIFS_INO_NODE_SZ;

1190
	if (!dbg_is_chk_gen(c))
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
		return 0;

	if (!S_ISDIR(dir->i_mode))
		return 0;

	lowest_dent_key(c, &key, dir->i_ino);
	while (1) {
		int err;

		dent = ubifs_tnc_next_ent(c, &key, &nm);
		if (IS_ERR(dent)) {
			err = PTR_ERR(dent);
			if (err == -ENOENT)
				break;
			return err;
		}

		nm.name = dent->name;
		nm.len = le16_to_cpu(dent->nlen);
		size += CALC_DENT_SIZE(nm.len);
		if (dent->type == UBIFS_ITYPE_DIR)
			nlink += 1;
		kfree(pdent);
		pdent = dent;
		key_read(c, &dent->key, &key);
	}
	kfree(pdent);

	if (i_size_read(dir) != size) {
		ubifs_err("directory inode %lu has size %llu, "
			  "but calculated size is %llu", dir->i_ino,
			  (unsigned long long)i_size_read(dir),
			  (unsigned long long)size);
1224
		ubifs_dump_inode(c, dir);
1225 1226 1227 1228 1229 1230
		dump_stack();
		return -EINVAL;
	}
	if (dir->i_nlink != nlink) {
		ubifs_err("directory inode %lu has nlink %u, but calculated "
			  "nlink is %u", dir->i_ino, dir->i_nlink, nlink);
1231
		ubifs_dump_inode(c, dir);
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
		dump_stack();
		return -EINVAL;
	}

	return 0;
}

/**
 * dbg_check_key_order - make sure that colliding keys are properly ordered.
 * @c: UBIFS file-system description object
 * @zbr1: first zbranch
 * @zbr2: following zbranch
 *
 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
 * names of the direntries/xentries which are referred by the keys. This
 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
 * sure the name of direntry/xentry referred by @zbr1 is less than
 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
 * and a negative error code in case of failure.
 */
static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
			       struct ubifs_zbranch *zbr2)
{
	int err, nlen1, nlen2, cmp;
	struct ubifs_dent_node *dent1, *dent2;
	union ubifs_key key;
A
Artem Bityutskiy 已提交
1258
	char key_buf[DBG_KEY_BUF_LEN];
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287

	ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
	dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
	if (!dent1)
		return -ENOMEM;
	dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
	if (!dent2) {
		err = -ENOMEM;
		goto out_free;
	}

	err = ubifs_tnc_read_node(c, zbr1, dent1);
	if (err)
		goto out_free;
	err = ubifs_validate_entry(c, dent1);
	if (err)
		goto out_free;

	err = ubifs_tnc_read_node(c, zbr2, dent2);
	if (err)
		goto out_free;
	err = ubifs_validate_entry(c, dent2);
	if (err)
		goto out_free;

	/* Make sure node keys are the same as in zbranch */
	err = 1;
	key_read(c, &dent1->key, &key);
	if (keys_cmp(c, &zbr1->key, &key)) {
A
Artem Bityutskiy 已提交
1288 1289 1290 1291 1292 1293
		ubifs_err("1st entry at %d:%d has key %s", zbr1->lnum,
			  zbr1->offs, dbg_snprintf_key(c, &key, key_buf,
						       DBG_KEY_BUF_LEN));
		ubifs_err("but it should have key %s according to tnc",
			  dbg_snprintf_key(c, &zbr1->key, key_buf,
					   DBG_KEY_BUF_LEN));
1294
		ubifs_dump_node(c, dent1);
A
Artem Bityutskiy 已提交
1295
		goto out_free;
1296 1297 1298 1299
	}

	key_read(c, &dent2->key, &key);
	if (keys_cmp(c, &zbr2->key, &key)) {
A
Artem Bityutskiy 已提交
1300 1301 1302 1303 1304 1305
		ubifs_err("2nd entry at %d:%d has key %s", zbr1->lnum,
			  zbr1->offs, dbg_snprintf_key(c, &key, key_buf,
						       DBG_KEY_BUF_LEN));
		ubifs_err("but it should have key %s according to tnc",
			  dbg_snprintf_key(c, &zbr2->key, key_buf,
					   DBG_KEY_BUF_LEN));
1306
		ubifs_dump_node(c, dent2);
A
Artem Bityutskiy 已提交
1307
		goto out_free;
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
	}

	nlen1 = le16_to_cpu(dent1->nlen);
	nlen2 = le16_to_cpu(dent2->nlen);

	cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
	if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
		err = 0;
		goto out_free;
	}
	if (cmp == 0 && nlen1 == nlen2)
A
Artem Bityutskiy 已提交
1319
		ubifs_err("2 xent/dent nodes with the same name");
1320
	else
A
Artem Bityutskiy 已提交
1321 1322
		ubifs_err("bad order of colliding key %s",
			  dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
1323

A
Artem Bityutskiy 已提交
1324
	ubifs_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs);
1325
	ubifs_dump_node(c, dent1);
A
Artem Bityutskiy 已提交
1326
	ubifs_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs);
1327
	ubifs_dump_node(c, dent2);
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437

out_free:
	kfree(dent2);
	kfree(dent1);
	return err;
}

/**
 * dbg_check_znode - check if znode is all right.
 * @c: UBIFS file-system description object
 * @zbr: zbranch which points to this znode
 *
 * This function makes sure that znode referred to by @zbr is all right.
 * Returns zero if it is, and %-EINVAL if it is not.
 */
static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
{
	struct ubifs_znode *znode = zbr->znode;
	struct ubifs_znode *zp = znode->parent;
	int n, err, cmp;

	if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
		err = 1;
		goto out;
	}
	if (znode->level < 0) {
		err = 2;
		goto out;
	}
	if (znode->iip < 0 || znode->iip >= c->fanout) {
		err = 3;
		goto out;
	}

	if (zbr->len == 0)
		/* Only dirty zbranch may have no on-flash nodes */
		if (!ubifs_zn_dirty(znode)) {
			err = 4;
			goto out;
		}

	if (ubifs_zn_dirty(znode)) {
		/*
		 * If znode is dirty, its parent has to be dirty as well. The
		 * order of the operation is important, so we have to have
		 * memory barriers.
		 */
		smp_mb();
		if (zp && !ubifs_zn_dirty(zp)) {
			/*
			 * The dirty flag is atomic and is cleared outside the
			 * TNC mutex, so znode's dirty flag may now have
			 * been cleared. The child is always cleared before the
			 * parent, so we just need to check again.
			 */
			smp_mb();
			if (ubifs_zn_dirty(znode)) {
				err = 5;
				goto out;
			}
		}
	}

	if (zp) {
		const union ubifs_key *min, *max;

		if (znode->level != zp->level - 1) {
			err = 6;
			goto out;
		}

		/* Make sure the 'parent' pointer in our znode is correct */
		err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
		if (!err) {
			/* This zbranch does not exist in the parent */
			err = 7;
			goto out;
		}

		if (znode->iip >= zp->child_cnt) {
			err = 8;
			goto out;
		}

		if (znode->iip != n) {
			/* This may happen only in case of collisions */
			if (keys_cmp(c, &zp->zbranch[n].key,
				     &zp->zbranch[znode->iip].key)) {
				err = 9;
				goto out;
			}
			n = znode->iip;
		}

		/*
		 * Make sure that the first key in our znode is greater than or
		 * equal to the key in the pointing zbranch.
		 */
		min = &zbr->key;
		cmp = keys_cmp(c, min, &znode->zbranch[0].key);
		if (cmp == 1) {
			err = 10;
			goto out;
		}

		if (n + 1 < zp->child_cnt) {
			max = &zp->zbranch[n + 1].key;

			/*
			 * Make sure the last key in our znode is less or
A
Artem Bityutskiy 已提交
1438
			 * equivalent than the key in the zbranch which goes
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
			 * after our pointing zbranch.
			 */
			cmp = keys_cmp(c, max,
				&znode->zbranch[znode->child_cnt - 1].key);
			if (cmp == -1) {
				err = 11;
				goto out;
			}
		}
	} else {
		/* This may only be root znode */
		if (zbr != &c->zroot) {
			err = 12;
			goto out;
		}
	}

	/*
	 * Make sure that next key is greater or equivalent then the previous
	 * one.
	 */
	for (n = 1; n < znode->child_cnt; n++) {
		cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
			       &znode->zbranch[n].key);
		if (cmp > 0) {
			err = 13;
			goto out;
		}
		if (cmp == 0) {
			/* This can only be keys with colliding hash */
			if (!is_hash_key(c, &znode->zbranch[n].key)) {
				err = 14;
				goto out;
			}

			if (znode->level != 0 || c->replaying)
				continue;

			/*
			 * Colliding keys should follow binary order of
			 * corresponding xentry/dentry names.
			 */
			err = dbg_check_key_order(c, &znode->zbranch[n - 1],
						  &znode->zbranch[n]);
			if (err < 0)
				return err;
			if (err) {
				err = 15;
				goto out;
			}
		}
	}

	for (n = 0; n < znode->child_cnt; n++) {
		if (!znode->zbranch[n].znode &&
		    (znode->zbranch[n].lnum == 0 ||
		     znode->zbranch[n].len == 0)) {
			err = 16;
			goto out;
		}

		if (znode->zbranch[n].lnum != 0 &&
		    znode->zbranch[n].len == 0) {
			err = 17;
			goto out;
		}

		if (znode->zbranch[n].lnum == 0 &&
		    znode->zbranch[n].len != 0) {
			err = 18;
			goto out;
		}

		if (znode->zbranch[n].lnum == 0 &&
		    znode->zbranch[n].offs != 0) {
			err = 19;
			goto out;
		}

		if (znode->level != 0 && znode->zbranch[n].znode)
			if (znode->zbranch[n].znode->parent != znode) {
				err = 20;
				goto out;
			}
	}

	return 0;

out:
	ubifs_err("failed, error %d", err);
	ubifs_msg("dump of the znode");
1530
	ubifs_dump_znode(c, znode);
1531 1532
	if (zp) {
		ubifs_msg("dump of the parent znode");
1533
		ubifs_dump_znode(c, zp);
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
	}
	dump_stack();
	return -EINVAL;
}

/**
 * dbg_check_tnc - check TNC tree.
 * @c: UBIFS file-system description object
 * @extra: do extra checks that are possible at start commit
 *
 * This function traverses whole TNC tree and checks every znode. Returns zero
 * if everything is all right and %-EINVAL if something is wrong with TNC.
 */
int dbg_check_tnc(struct ubifs_info *c, int extra)
{
	struct ubifs_znode *znode;
	long clean_cnt = 0, dirty_cnt = 0;
	int err, last;

1553
	if (!dbg_is_chk_index(c))
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599
		return 0;

	ubifs_assert(mutex_is_locked(&c->tnc_mutex));
	if (!c->zroot.znode)
		return 0;

	znode = ubifs_tnc_postorder_first(c->zroot.znode);
	while (1) {
		struct ubifs_znode *prev;
		struct ubifs_zbranch *zbr;

		if (!znode->parent)
			zbr = &c->zroot;
		else
			zbr = &znode->parent->zbranch[znode->iip];

		err = dbg_check_znode(c, zbr);
		if (err)
			return err;

		if (extra) {
			if (ubifs_zn_dirty(znode))
				dirty_cnt += 1;
			else
				clean_cnt += 1;
		}

		prev = znode;
		znode = ubifs_tnc_postorder_next(znode);
		if (!znode)
			break;

		/*
		 * If the last key of this znode is equivalent to the first key
		 * of the next znode (collision), then check order of the keys.
		 */
		last = prev->child_cnt - 1;
		if (prev->level == 0 && znode->level == 0 && !c->replaying &&
		    !keys_cmp(c, &prev->zbranch[last].key,
			      &znode->zbranch[0].key)) {
			err = dbg_check_key_order(c, &prev->zbranch[last],
						  &znode->zbranch[0]);
			if (err < 0)
				return err;
			if (err) {
				ubifs_msg("first znode");
1600
				ubifs_dump_znode(c, prev);
1601
				ubifs_msg("second znode");
1602
				ubifs_dump_znode(c, znode);
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
				return -EINVAL;
			}
		}
	}

	if (extra) {
		if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
			ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld",
				  atomic_long_read(&c->clean_zn_cnt),
				  clean_cnt);
			return -EINVAL;
		}
		if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
			ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld",
				  atomic_long_read(&c->dirty_zn_cnt),
				  dirty_cnt);
			return -EINVAL;
		}
	}

	return 0;
}

/**
 * dbg_walk_index - walk the on-flash index.
 * @c: UBIFS file-system description object
 * @leaf_cb: called for each leaf node
 * @znode_cb: called for each indexing node
1631
 * @priv: private data which is passed to callbacks
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690
 *
 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
 * node and @znode_cb for each indexing node. Returns zero in case of success
 * and a negative error code in case of failure.
 *
 * It would be better if this function removed every znode it pulled to into
 * the TNC, so that the behavior more closely matched the non-debugging
 * behavior.
 */
int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
		   dbg_znode_callback znode_cb, void *priv)
{
	int err;
	struct ubifs_zbranch *zbr;
	struct ubifs_znode *znode, *child;

	mutex_lock(&c->tnc_mutex);
	/* If the root indexing node is not in TNC - pull it */
	if (!c->zroot.znode) {
		c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
		if (IS_ERR(c->zroot.znode)) {
			err = PTR_ERR(c->zroot.znode);
			c->zroot.znode = NULL;
			goto out_unlock;
		}
	}

	/*
	 * We are going to traverse the indexing tree in the postorder manner.
	 * Go down and find the leftmost indexing node where we are going to
	 * start from.
	 */
	znode = c->zroot.znode;
	while (znode->level > 0) {
		zbr = &znode->zbranch[0];
		child = zbr->znode;
		if (!child) {
			child = ubifs_load_znode(c, zbr, znode, 0);
			if (IS_ERR(child)) {
				err = PTR_ERR(child);
				goto out_unlock;
			}
			zbr->znode = child;
		}

		znode = child;
	}

	/* Iterate over all indexing nodes */
	while (1) {
		int idx;

		cond_resched();

		if (znode_cb) {
			err = znode_cb(c, znode, priv);
			if (err) {
				ubifs_err("znode checking function returned "
					  "error %d", err);
1691
				ubifs_dump_znode(c, znode);
1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758
				goto out_dump;
			}
		}
		if (leaf_cb && znode->level == 0) {
			for (idx = 0; idx < znode->child_cnt; idx++) {
				zbr = &znode->zbranch[idx];
				err = leaf_cb(c, zbr, priv);
				if (err) {
					ubifs_err("leaf checking function "
						  "returned error %d, for leaf "
						  "at LEB %d:%d",
						  err, zbr->lnum, zbr->offs);
					goto out_dump;
				}
			}
		}

		if (!znode->parent)
			break;

		idx = znode->iip + 1;
		znode = znode->parent;
		if (idx < znode->child_cnt) {
			/* Switch to the next index in the parent */
			zbr = &znode->zbranch[idx];
			child = zbr->znode;
			if (!child) {
				child = ubifs_load_znode(c, zbr, znode, idx);
				if (IS_ERR(child)) {
					err = PTR_ERR(child);
					goto out_unlock;
				}
				zbr->znode = child;
			}
			znode = child;
		} else
			/*
			 * This is the last child, switch to the parent and
			 * continue.
			 */
			continue;

		/* Go to the lowest leftmost znode in the new sub-tree */
		while (znode->level > 0) {
			zbr = &znode->zbranch[0];
			child = zbr->znode;
			if (!child) {
				child = ubifs_load_znode(c, zbr, znode, 0);
				if (IS_ERR(child)) {
					err = PTR_ERR(child);
					goto out_unlock;
				}
				zbr->znode = child;
			}
			znode = child;
		}
	}

	mutex_unlock(&c->tnc_mutex);
	return 0;

out_dump:
	if (znode->parent)
		zbr = &znode->parent->zbranch[znode->iip];
	else
		zbr = &c->zroot;
	ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1759
	ubifs_dump_znode(c, znode);
1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799
out_unlock:
	mutex_unlock(&c->tnc_mutex);
	return err;
}

/**
 * add_size - add znode size to partially calculated index size.
 * @c: UBIFS file-system description object
 * @znode: znode to add size for
 * @priv: partially calculated index size
 *
 * This is a helper function for 'dbg_check_idx_size()' which is called for
 * every indexing node and adds its size to the 'long long' variable pointed to
 * by @priv.
 */
static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
{
	long long *idx_size = priv;
	int add;

	add = ubifs_idx_node_sz(c, znode->child_cnt);
	add = ALIGN(add, 8);
	*idx_size += add;
	return 0;
}

/**
 * dbg_check_idx_size - check index size.
 * @c: UBIFS file-system description object
 * @idx_size: size to check
 *
 * This function walks the UBIFS index, calculates its size and checks that the
 * size is equivalent to @idx_size. Returns zero in case of success and a
 * negative error code in case of failure.
 */
int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
{
	int err;
	long long calc = 0;

1800
	if (!dbg_is_chk_index(c))
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880
		return 0;

	err = dbg_walk_index(c, NULL, add_size, &calc);
	if (err) {
		ubifs_err("error %d while walking the index", err);
		return err;
	}

	if (calc != idx_size) {
		ubifs_err("index size check failed: calculated size is %lld, "
			  "should be %lld", calc, idx_size);
		dump_stack();
		return -EINVAL;
	}

	return 0;
}

/**
 * struct fsck_inode - information about an inode used when checking the file-system.
 * @rb: link in the RB-tree of inodes
 * @inum: inode number
 * @mode: inode type, permissions, etc
 * @nlink: inode link count
 * @xattr_cnt: count of extended attributes
 * @references: how many directory/xattr entries refer this inode (calculated
 *              while walking the index)
 * @calc_cnt: for directory inode count of child directories
 * @size: inode size (read from on-flash inode)
 * @xattr_sz: summary size of all extended attributes (read from on-flash
 *            inode)
 * @calc_sz: for directories calculated directory size
 * @calc_xcnt: count of extended attributes
 * @calc_xsz: calculated summary size of all extended attributes
 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
 *             inode (read from on-flash inode)
 * @calc_xnms: calculated sum of lengths of all extended attribute names
 */
struct fsck_inode {
	struct rb_node rb;
	ino_t inum;
	umode_t mode;
	unsigned int nlink;
	unsigned int xattr_cnt;
	int references;
	int calc_cnt;
	long long size;
	unsigned int xattr_sz;
	long long calc_sz;
	long long calc_xcnt;
	long long calc_xsz;
	unsigned int xattr_nms;
	long long calc_xnms;
};

/**
 * struct fsck_data - private FS checking information.
 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
 */
struct fsck_data {
	struct rb_root inodes;
};

/**
 * add_inode - add inode information to RB-tree of inodes.
 * @c: UBIFS file-system description object
 * @fsckd: FS checking information
 * @ino: raw UBIFS inode to add
 *
 * This is a helper function for 'check_leaf()' which adds information about
 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
 * case of success and a negative error code in case of failure.
 */
static struct fsck_inode *add_inode(struct ubifs_info *c,
				    struct fsck_data *fsckd,
				    struct ubifs_ino_node *ino)
{
	struct rb_node **p, *parent = NULL;
	struct fsck_inode *fscki;
	ino_t inum = key_inum_flash(c, &ino->key);
1881 1882
	struct inode *inode;
	struct ubifs_inode *ui;
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897

	p = &fsckd->inodes.rb_node;
	while (*p) {
		parent = *p;
		fscki = rb_entry(parent, struct fsck_inode, rb);
		if (inum < fscki->inum)
			p = &(*p)->rb_left;
		else if (inum > fscki->inum)
			p = &(*p)->rb_right;
		else
			return fscki;
	}

	if (inum > c->highest_inum) {
		ubifs_err("too high inode number, max. is %lu",
A
Artem Bityutskiy 已提交
1898
			  (unsigned long)c->highest_inum);
1899 1900 1901 1902 1903 1904 1905
		return ERR_PTR(-EINVAL);
	}

	fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
	if (!fscki)
		return ERR_PTR(-ENOMEM);

1906 1907
	inode = ilookup(c->vfs_sb, inum);

1908
	fscki->inum = inum;
1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937
	/*
	 * If the inode is present in the VFS inode cache, use it instead of
	 * the on-flash inode which might be out-of-date. E.g., the size might
	 * be out-of-date. If we do not do this, the following may happen, for
	 * example:
	 *   1. A power cut happens
	 *   2. We mount the file-system R/O, the replay process fixes up the
	 *      inode size in the VFS cache, but on on-flash.
	 *   3. 'check_leaf()' fails because it hits a data node beyond inode
	 *      size.
	 */
	if (!inode) {
		fscki->nlink = le32_to_cpu(ino->nlink);
		fscki->size = le64_to_cpu(ino->size);
		fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
		fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
		fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
		fscki->mode = le32_to_cpu(ino->mode);
	} else {
		ui = ubifs_inode(inode);
		fscki->nlink = inode->i_nlink;
		fscki->size = inode->i_size;
		fscki->xattr_cnt = ui->xattr_cnt;
		fscki->xattr_sz = ui->xattr_size;
		fscki->xattr_nms = ui->xattr_names;
		fscki->mode = inode->i_mode;
		iput(inode);
	}

1938 1939 1940 1941
	if (S_ISDIR(fscki->mode)) {
		fscki->calc_sz = UBIFS_INO_NODE_SZ;
		fscki->calc_cnt = 2;
	}
1942

1943 1944
	rb_link_node(&fscki->rb, parent, p);
	rb_insert_color(&fscki->rb, &fsckd->inodes);
1945

1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003
	return fscki;
}

/**
 * search_inode - search inode in the RB-tree of inodes.
 * @fsckd: FS checking information
 * @inum: inode number to search
 *
 * This is a helper function for 'check_leaf()' which searches inode @inum in
 * the RB-tree of inodes and returns an inode information pointer or %NULL if
 * the inode was not found.
 */
static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
{
	struct rb_node *p;
	struct fsck_inode *fscki;

	p = fsckd->inodes.rb_node;
	while (p) {
		fscki = rb_entry(p, struct fsck_inode, rb);
		if (inum < fscki->inum)
			p = p->rb_left;
		else if (inum > fscki->inum)
			p = p->rb_right;
		else
			return fscki;
	}
	return NULL;
}

/**
 * read_add_inode - read inode node and add it to RB-tree of inodes.
 * @c: UBIFS file-system description object
 * @fsckd: FS checking information
 * @inum: inode number to read
 *
 * This is a helper function for 'check_leaf()' which finds inode node @inum in
 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
 * information pointer in case of success and a negative error code in case of
 * failure.
 */
static struct fsck_inode *read_add_inode(struct ubifs_info *c,
					 struct fsck_data *fsckd, ino_t inum)
{
	int n, err;
	union ubifs_key key;
	struct ubifs_znode *znode;
	struct ubifs_zbranch *zbr;
	struct ubifs_ino_node *ino;
	struct fsck_inode *fscki;

	fscki = search_inode(fsckd, inum);
	if (fscki)
		return fscki;

	ino_key_init(c, &key, inum);
	err = ubifs_lookup_level0(c, &key, &znode, &n);
	if (!err) {
A
Artem Bityutskiy 已提交
2004
		ubifs_err("inode %lu not found in index", (unsigned long)inum);
2005 2006
		return ERR_PTR(-ENOENT);
	} else if (err < 0) {
A
Artem Bityutskiy 已提交
2007 2008
		ubifs_err("error %d while looking up inode %lu",
			  err, (unsigned long)inum);
2009 2010 2011 2012 2013
		return ERR_PTR(err);
	}

	zbr = &znode->zbranch[n];
	if (zbr->len < UBIFS_INO_NODE_SZ) {
A
Artem Bityutskiy 已提交
2014 2015
		ubifs_err("bad node %lu node length %d",
			  (unsigned long)inum, zbr->len);
2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034
		return ERR_PTR(-EINVAL);
	}

	ino = kmalloc(zbr->len, GFP_NOFS);
	if (!ino)
		return ERR_PTR(-ENOMEM);

	err = ubifs_tnc_read_node(c, zbr, ino);
	if (err) {
		ubifs_err("cannot read inode node at LEB %d:%d, error %d",
			  zbr->lnum, zbr->offs, err);
		kfree(ino);
		return ERR_PTR(err);
	}

	fscki = add_inode(c, fsckd, ino);
	kfree(ino);
	if (IS_ERR(fscki)) {
		ubifs_err("error %ld while adding inode %lu node",
A
Artem Bityutskiy 已提交
2035
			  PTR_ERR(fscki), (unsigned long)inum);
2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123
		return fscki;
	}

	return fscki;
}

/**
 * check_leaf - check leaf node.
 * @c: UBIFS file-system description object
 * @zbr: zbranch of the leaf node to check
 * @priv: FS checking information
 *
 * This is a helper function for 'dbg_check_filesystem()' which is called for
 * every single leaf node while walking the indexing tree. It checks that the
 * leaf node referred from the indexing tree exists, has correct CRC, and does
 * some other basic validation. This function is also responsible for building
 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
 * calculates reference count, size, etc for each inode in order to later
 * compare them to the information stored inside the inodes and detect possible
 * inconsistencies. Returns zero in case of success and a negative error code
 * in case of failure.
 */
static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
		      void *priv)
{
	ino_t inum;
	void *node;
	struct ubifs_ch *ch;
	int err, type = key_type(c, &zbr->key);
	struct fsck_inode *fscki;

	if (zbr->len < UBIFS_CH_SZ) {
		ubifs_err("bad leaf length %d (LEB %d:%d)",
			  zbr->len, zbr->lnum, zbr->offs);
		return -EINVAL;
	}

	node = kmalloc(zbr->len, GFP_NOFS);
	if (!node)
		return -ENOMEM;

	err = ubifs_tnc_read_node(c, zbr, node);
	if (err) {
		ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
			  zbr->lnum, zbr->offs, err);
		goto out_free;
	}

	/* If this is an inode node, add it to RB-tree of inodes */
	if (type == UBIFS_INO_KEY) {
		fscki = add_inode(c, priv, node);
		if (IS_ERR(fscki)) {
			err = PTR_ERR(fscki);
			ubifs_err("error %d while adding inode node", err);
			goto out_dump;
		}
		goto out;
	}

	if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
	    type != UBIFS_DATA_KEY) {
		ubifs_err("unexpected node type %d at LEB %d:%d",
			  type, zbr->lnum, zbr->offs);
		err = -EINVAL;
		goto out_free;
	}

	ch = node;
	if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
		ubifs_err("too high sequence number, max. is %llu",
			  c->max_sqnum);
		err = -EINVAL;
		goto out_dump;
	}

	if (type == UBIFS_DATA_KEY) {
		long long blk_offs;
		struct ubifs_data_node *dn = node;

		/*
		 * Search the inode node this data node belongs to and insert
		 * it to the RB-tree of inodes.
		 */
		inum = key_inum_flash(c, &dn->key);
		fscki = read_add_inode(c, priv, inum);
		if (IS_ERR(fscki)) {
			err = PTR_ERR(fscki);
			ubifs_err("error %d while processing data node and "
A
Artem Bityutskiy 已提交
2124 2125
				  "trying to find inode node %lu",
				  err, (unsigned long)inum);
2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157
			goto out_dump;
		}

		/* Make sure the data node is within inode size */
		blk_offs = key_block_flash(c, &dn->key);
		blk_offs <<= UBIFS_BLOCK_SHIFT;
		blk_offs += le32_to_cpu(dn->size);
		if (blk_offs > fscki->size) {
			ubifs_err("data node at LEB %d:%d is not within inode "
				  "size %lld", zbr->lnum, zbr->offs,
				  fscki->size);
			err = -EINVAL;
			goto out_dump;
		}
	} else {
		int nlen;
		struct ubifs_dent_node *dent = node;
		struct fsck_inode *fscki1;

		err = ubifs_validate_entry(c, dent);
		if (err)
			goto out_dump;

		/*
		 * Search the inode node this entry refers to and the parent
		 * inode node and insert them to the RB-tree of inodes.
		 */
		inum = le64_to_cpu(dent->inum);
		fscki = read_add_inode(c, priv, inum);
		if (IS_ERR(fscki)) {
			err = PTR_ERR(fscki);
			ubifs_err("error %d while processing entry node and "
A
Artem Bityutskiy 已提交
2158 2159
				  "trying to find inode node %lu",
				  err, (unsigned long)inum);
2160 2161 2162 2163 2164 2165 2166 2167 2168
			goto out_dump;
		}

		/* Count how many direntries or xentries refers this inode */
		fscki->references += 1;

		inum = key_inum_flash(c, &dent->key);
		fscki1 = read_add_inode(c, priv, inum);
		if (IS_ERR(fscki1)) {
R
Roel Kluin 已提交
2169
			err = PTR_ERR(fscki1);
2170 2171
			ubifs_err("error %d while processing entry node and "
				  "trying to find parent inode node %lu",
A
Artem Bityutskiy 已提交
2172
				  err, (unsigned long)inum);
2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194
			goto out_dump;
		}

		nlen = le16_to_cpu(dent->nlen);
		if (type == UBIFS_XENT_KEY) {
			fscki1->calc_xcnt += 1;
			fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
			fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
			fscki1->calc_xnms += nlen;
		} else {
			fscki1->calc_sz += CALC_DENT_SIZE(nlen);
			if (dent->type == UBIFS_ITYPE_DIR)
				fscki1->calc_cnt += 1;
		}
	}

out:
	kfree(node);
	return 0;

out_dump:
	ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
2195
	ubifs_dump_node(c, node);
2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262
out_free:
	kfree(node);
	return err;
}

/**
 * free_inodes - free RB-tree of inodes.
 * @fsckd: FS checking information
 */
static void free_inodes(struct fsck_data *fsckd)
{
	struct rb_node *this = fsckd->inodes.rb_node;
	struct fsck_inode *fscki;

	while (this) {
		if (this->rb_left)
			this = this->rb_left;
		else if (this->rb_right)
			this = this->rb_right;
		else {
			fscki = rb_entry(this, struct fsck_inode, rb);
			this = rb_parent(this);
			if (this) {
				if (this->rb_left == &fscki->rb)
					this->rb_left = NULL;
				else
					this->rb_right = NULL;
			}
			kfree(fscki);
		}
	}
}

/**
 * check_inodes - checks all inodes.
 * @c: UBIFS file-system description object
 * @fsckd: FS checking information
 *
 * This is a helper function for 'dbg_check_filesystem()' which walks the
 * RB-tree of inodes after the index scan has been finished, and checks that
 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
 * %-EINVAL if not, and a negative error code in case of failure.
 */
static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
{
	int n, err;
	union ubifs_key key;
	struct ubifs_znode *znode;
	struct ubifs_zbranch *zbr;
	struct ubifs_ino_node *ino;
	struct fsck_inode *fscki;
	struct rb_node *this = rb_first(&fsckd->inodes);

	while (this) {
		fscki = rb_entry(this, struct fsck_inode, rb);
		this = rb_next(this);

		if (S_ISDIR(fscki->mode)) {
			/*
			 * Directories have to have exactly one reference (they
			 * cannot have hardlinks), although root inode is an
			 * exception.
			 */
			if (fscki->inum != UBIFS_ROOT_INO &&
			    fscki->references != 1) {
				ubifs_err("directory inode %lu has %d "
					  "direntries which refer it, but "
A
Artem Bityutskiy 已提交
2263 2264
					  "should be 1",
					  (unsigned long)fscki->inum,
2265 2266 2267 2268 2269 2270 2271
					  fscki->references);
				goto out_dump;
			}
			if (fscki->inum == UBIFS_ROOT_INO &&
			    fscki->references != 0) {
				ubifs_err("root inode %lu has non-zero (%d) "
					  "direntries which refer it",
A
Artem Bityutskiy 已提交
2272 2273
					  (unsigned long)fscki->inum,
					  fscki->references);
2274 2275 2276 2277 2278
				goto out_dump;
			}
			if (fscki->calc_sz != fscki->size) {
				ubifs_err("directory inode %lu size is %lld, "
					  "but calculated size is %lld",
A
Artem Bityutskiy 已提交
2279 2280
					  (unsigned long)fscki->inum,
					  fscki->size, fscki->calc_sz);
2281 2282 2283 2284 2285
				goto out_dump;
			}
			if (fscki->calc_cnt != fscki->nlink) {
				ubifs_err("directory inode %lu nlink is %d, "
					  "but calculated nlink is %d",
A
Artem Bityutskiy 已提交
2286 2287
					  (unsigned long)fscki->inum,
					  fscki->nlink, fscki->calc_cnt);
2288 2289 2290 2291 2292
				goto out_dump;
			}
		} else {
			if (fscki->references != fscki->nlink) {
				ubifs_err("inode %lu nlink is %d, but "
A
Artem Bityutskiy 已提交
2293 2294
					  "calculated nlink is %d",
					  (unsigned long)fscki->inum,
2295 2296 2297 2298 2299 2300 2301
					  fscki->nlink, fscki->references);
				goto out_dump;
			}
		}
		if (fscki->xattr_sz != fscki->calc_xsz) {
			ubifs_err("inode %lu has xattr size %u, but "
				  "calculated size is %lld",
A
Artem Bityutskiy 已提交
2302
				  (unsigned long)fscki->inum, fscki->xattr_sz,
2303 2304 2305 2306 2307
				  fscki->calc_xsz);
			goto out_dump;
		}
		if (fscki->xattr_cnt != fscki->calc_xcnt) {
			ubifs_err("inode %lu has %u xattrs, but "
A
Artem Bityutskiy 已提交
2308 2309
				  "calculated count is %lld",
				  (unsigned long)fscki->inum,
2310 2311 2312 2313 2314 2315
				  fscki->xattr_cnt, fscki->calc_xcnt);
			goto out_dump;
		}
		if (fscki->xattr_nms != fscki->calc_xnms) {
			ubifs_err("inode %lu has xattr names' size %u, but "
				  "calculated names' size is %lld",
A
Artem Bityutskiy 已提交
2316
				  (unsigned long)fscki->inum, fscki->xattr_nms,
2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328
				  fscki->calc_xnms);
			goto out_dump;
		}
	}

	return 0;

out_dump:
	/* Read the bad inode and dump it */
	ino_key_init(c, &key, fscki->inum);
	err = ubifs_lookup_level0(c, &key, &znode, &n);
	if (!err) {
A
Artem Bityutskiy 已提交
2329 2330
		ubifs_err("inode %lu not found in index",
			  (unsigned long)fscki->inum);
2331 2332 2333
		return -ENOENT;
	} else if (err < 0) {
		ubifs_err("error %d while looking up inode %lu",
A
Artem Bityutskiy 已提交
2334
			  err, (unsigned long)fscki->inum);
2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351
		return err;
	}

	zbr = &znode->zbranch[n];
	ino = kmalloc(zbr->len, GFP_NOFS);
	if (!ino)
		return -ENOMEM;

	err = ubifs_tnc_read_node(c, zbr, ino);
	if (err) {
		ubifs_err("cannot read inode node at LEB %d:%d, error %d",
			  zbr->lnum, zbr->offs, err);
		kfree(ino);
		return err;
	}

	ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",
A
Artem Bityutskiy 已提交
2352
		  (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
2353
	ubifs_dump_node(c, ino);
2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375
	kfree(ino);
	return -EINVAL;
}

/**
 * dbg_check_filesystem - check the file-system.
 * @c: UBIFS file-system description object
 *
 * This function checks the file system, namely:
 * o makes sure that all leaf nodes exist and their CRCs are correct;
 * o makes sure inode nlink, size, xattr size/count are correct (for all
 *   inodes).
 *
 * The function reads whole indexing tree and all nodes, so it is pretty
 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
 * not, and a negative error code in case of failure.
 */
int dbg_check_filesystem(struct ubifs_info *c)
{
	int err;
	struct fsck_data fsckd;

2376
	if (!dbg_is_chk_fs(c))
2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397
		return 0;

	fsckd.inodes = RB_ROOT;
	err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
	if (err)
		goto out_free;

	err = check_inodes(c, &fsckd);
	if (err)
		goto out_free;

	free_inodes(&fsckd);
	return 0;

out_free:
	ubifs_err("file-system check failed with error %d", err);
	dump_stack();
	free_inodes(&fsckd);
	return err;
}

2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410
/**
 * dbg_check_data_nodes_order - check that list of data nodes is sorted.
 * @c: UBIFS file-system description object
 * @head: the list of nodes ('struct ubifs_scan_node' objects)
 *
 * This function returns zero if the list of data nodes is sorted correctly,
 * and %-EINVAL if not.
 */
int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head)
{
	struct list_head *cur;
	struct ubifs_scan_node *sa, *sb;

2411
	if (!dbg_is_chk_gen(c))
2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423
		return 0;

	for (cur = head->next; cur->next != head; cur = cur->next) {
		ino_t inuma, inumb;
		uint32_t blka, blkb;

		cond_resched();
		sa = container_of(cur, struct ubifs_scan_node, list);
		sb = container_of(cur->next, struct ubifs_scan_node, list);

		if (sa->type != UBIFS_DATA_NODE) {
			ubifs_err("bad node type %d", sa->type);
2424
			ubifs_dump_node(c, sa->node);
2425 2426 2427 2428
			return -EINVAL;
		}
		if (sb->type != UBIFS_DATA_NODE) {
			ubifs_err("bad node type %d", sb->type);
2429
			ubifs_dump_node(c, sb->node);
2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459
			return -EINVAL;
		}

		inuma = key_inum(c, &sa->key);
		inumb = key_inum(c, &sb->key);

		if (inuma < inumb)
			continue;
		if (inuma > inumb) {
			ubifs_err("larger inum %lu goes before inum %lu",
				  (unsigned long)inuma, (unsigned long)inumb);
			goto error_dump;
		}

		blka = key_block(c, &sa->key);
		blkb = key_block(c, &sb->key);

		if (blka > blkb) {
			ubifs_err("larger block %u goes before %u", blka, blkb);
			goto error_dump;
		}
		if (blka == blkb) {
			ubifs_err("two data nodes for the same block");
			goto error_dump;
		}
	}

	return 0;

error_dump:
2460 2461
	ubifs_dump_node(c, sa->node);
	ubifs_dump_node(c, sb->node);
2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477
	return -EINVAL;
}

/**
 * dbg_check_nondata_nodes_order - check that list of data nodes is sorted.
 * @c: UBIFS file-system description object
 * @head: the list of nodes ('struct ubifs_scan_node' objects)
 *
 * This function returns zero if the list of non-data nodes is sorted correctly,
 * and %-EINVAL if not.
 */
int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head)
{
	struct list_head *cur;
	struct ubifs_scan_node *sa, *sb;

2478
	if (!dbg_is_chk_gen(c))
2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491
		return 0;

	for (cur = head->next; cur->next != head; cur = cur->next) {
		ino_t inuma, inumb;
		uint32_t hasha, hashb;

		cond_resched();
		sa = container_of(cur, struct ubifs_scan_node, list);
		sb = container_of(cur->next, struct ubifs_scan_node, list);

		if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
		    sa->type != UBIFS_XENT_NODE) {
			ubifs_err("bad node type %d", sa->type);
2492
			ubifs_dump_node(c, sa->node);
2493 2494 2495 2496 2497
			return -EINVAL;
		}
		if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
		    sa->type != UBIFS_XENT_NODE) {
			ubifs_err("bad node type %d", sb->type);
2498
			ubifs_dump_node(c, sb->node);
2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537
			return -EINVAL;
		}

		if (sa->type != UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
			ubifs_err("non-inode node goes before inode node");
			goto error_dump;
		}

		if (sa->type == UBIFS_INO_NODE && sb->type != UBIFS_INO_NODE)
			continue;

		if (sa->type == UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
			/* Inode nodes are sorted in descending size order */
			if (sa->len < sb->len) {
				ubifs_err("smaller inode node goes first");
				goto error_dump;
			}
			continue;
		}

		/*
		 * This is either a dentry or xentry, which should be sorted in
		 * ascending (parent ino, hash) order.
		 */
		inuma = key_inum(c, &sa->key);
		inumb = key_inum(c, &sb->key);

		if (inuma < inumb)
			continue;
		if (inuma > inumb) {
			ubifs_err("larger inum %lu goes before inum %lu",
				  (unsigned long)inuma, (unsigned long)inumb);
			goto error_dump;
		}

		hasha = key_block(c, &sa->key);
		hashb = key_block(c, &sb->key);

		if (hasha > hashb) {
2538 2539
			ubifs_err("larger hash %u goes before %u",
				  hasha, hashb);
2540 2541 2542 2543 2544 2545 2546 2547
			goto error_dump;
		}
	}

	return 0;

error_dump:
	ubifs_msg("dumping first node");
2548
	ubifs_dump_node(c, sa->node);
2549
	ubifs_msg("dumping second node");
2550
	ubifs_dump_node(c, sb->node);
2551 2552 2553 2554
	return -EINVAL;
	return 0;
}

2555
static inline int chance(unsigned int n, unsigned int out_of)
2556
{
2557 2558
	return !!((random32() % out_of) + 1 <= n);

2559 2560
}

2561
static int power_cut_emulated(struct ubifs_info *c, int lnum, int write)
2562
{
2563
	struct ubifs_debug_info *d = c->dbg;
2564

2565
	ubifs_assert(dbg_is_tst_rcvry(c));
2566

2567 2568
	if (!d->pc_cnt) {
		/* First call - decide delay to the power cut */
2569
		if (chance(1, 2)) {
2570
			unsigned long delay;
2571 2572

			if (chance(1, 2)) {
2573
				d->pc_delay = 1;
2574 2575 2576 2577 2578
				/* Fail withing 1 minute */
				delay = random32() % 60000;
				d->pc_timeout = jiffies;
				d->pc_timeout += msecs_to_jiffies(delay);
				ubifs_warn("failing after %lums", delay);
2579
			} else {
2580
				d->pc_delay = 2;
2581 2582
				delay = random32() % 10000;
				/* Fail within 10000 operations */
2583
				d->pc_cnt_max = delay;
2584
				ubifs_warn("failing after %lu calls", delay);
2585 2586
			}
		}
2587

2588
		d->pc_cnt += 1;
2589
	}
2590

2591
	/* Determine if failure delay has expired */
2592
	if (d->pc_delay == 1 && time_before(jiffies, d->pc_timeout))
2593
			return 0;
2594
	if (d->pc_delay == 2 && d->pc_cnt++ < d->pc_cnt_max)
2595
			return 0;
2596

2597
	if (lnum == UBIFS_SB_LNUM) {
2598 2599 2600
		if (write && chance(1, 2))
			return 0;
		if (chance(19, 20))
2601
			return 0;
2602
		ubifs_warn("failing in super block LEB %d", lnum);
2603 2604 2605
	} else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
		if (chance(19, 20))
			return 0;
2606
		ubifs_warn("failing in master LEB %d", lnum);
2607
	} else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2608 2609 2610
		if (write && chance(99, 100))
			return 0;
		if (chance(399, 400))
2611
			return 0;
2612
		ubifs_warn("failing in log LEB %d", lnum);
2613
	} else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2614 2615 2616
		if (write && chance(7, 8))
			return 0;
		if (chance(19, 20))
2617
			return 0;
2618
		ubifs_warn("failing in LPT LEB %d", lnum);
2619
	} else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2620 2621 2622
		if (write && chance(1, 2))
			return 0;
		if (chance(9, 10))
2623
			return 0;
2624
		ubifs_warn("failing in orphan LEB %d", lnum);
2625 2626 2627
	} else if (lnum == c->ihead_lnum) {
		if (chance(99, 100))
			return 0;
2628
		ubifs_warn("failing in index head LEB %d", lnum);
2629 2630 2631
	} else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
		if (chance(9, 10))
			return 0;
2632
		ubifs_warn("failing in GC head LEB %d", lnum);
2633 2634 2635 2636
	} else if (write && !RB_EMPTY_ROOT(&c->buds) &&
		   !ubifs_search_bud(c, lnum)) {
		if (chance(19, 20))
			return 0;
2637
		ubifs_warn("failing in non-bud LEB %d", lnum);
2638 2639 2640 2641
	} else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
		   c->cmt_state == COMMIT_RUNNING_REQUIRED) {
		if (chance(999, 1000))
			return 0;
2642
		ubifs_warn("failing in bud LEB %d commit running", lnum);
2643 2644 2645
	} else {
		if (chance(9999, 10000))
			return 0;
2646
		ubifs_warn("failing in bud LEB %d commit not running", lnum);
2647
	}
2648

2649
	d->pc_happened = 1;
2650
	ubifs_warn("========== Power cut emulated ==========");
2651 2652 2653 2654
	dump_stack();
	return 1;
}

2655
static void cut_data(const void *buf, unsigned int len)
2656
{
2657
	unsigned int from, to, i, ffs = chance(1, 2);
2658 2659
	unsigned char *p = (void *)buf;

2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675
	from = random32() % (len + 1);
	if (chance(1, 2))
		to = random32() % (len - from + 1);
	else
		to = len;

	if (from < to)
		ubifs_warn("filled bytes %u-%u with %s", from, to - 1,
			   ffs ? "0xFFs" : "random data");

	if (ffs)
		for (i = from; i < to; i++)
			p[i] = 0xFF;
	else
		for (i = from; i < to; i++)
			p[i] = random32() % 0x100;
2676 2677
}

2678
int dbg_leb_write(struct ubifs_info *c, int lnum, const void *buf,
R
Richard Weinberger 已提交
2679
		  int offs, int len)
2680
{
2681
	int err, failing;
2682

2683
	if (c->dbg->pc_happened)
2684
		return -EROFS;
2685 2686

	failing = power_cut_emulated(c, lnum, 1);
2687
	if (failing)
2688
		cut_data(buf, len);
R
Richard Weinberger 已提交
2689
	err = ubi_leb_write(c->ubi, lnum, buf, offs, len);
2690 2691
	if (err)
		return err;
2692
	if (failing)
2693
		return -EROFS;
2694 2695 2696
	return 0;
}

2697
int dbg_leb_change(struct ubifs_info *c, int lnum, const void *buf,
R
Richard Weinberger 已提交
2698
		   int len)
2699 2700 2701
{
	int err;

2702 2703 2704
	if (c->dbg->pc_happened)
		return -EROFS;
	if (power_cut_emulated(c, lnum, 1))
2705
		return -EROFS;
R
Richard Weinberger 已提交
2706
	err = ubi_leb_change(c->ubi, lnum, buf, len);
2707 2708
	if (err)
		return err;
2709
	if (power_cut_emulated(c, lnum, 1))
2710
		return -EROFS;
2711 2712 2713
	return 0;
}

2714
int dbg_leb_unmap(struct ubifs_info *c, int lnum)
2715 2716 2717
{
	int err;

2718 2719 2720
	if (c->dbg->pc_happened)
		return -EROFS;
	if (power_cut_emulated(c, lnum, 0))
2721
		return -EROFS;
2722
	err = ubi_leb_unmap(c->ubi, lnum);
2723 2724
	if (err)
		return err;
2725
	if (power_cut_emulated(c, lnum, 0))
2726
		return -EROFS;
2727 2728 2729
	return 0;
}

R
Richard Weinberger 已提交
2730
int dbg_leb_map(struct ubifs_info *c, int lnum)
2731 2732 2733
{
	int err;

2734 2735 2736
	if (c->dbg->pc_happened)
		return -EROFS;
	if (power_cut_emulated(c, lnum, 0))
2737
		return -EROFS;
R
Richard Weinberger 已提交
2738
	err = ubi_leb_map(c->ubi, lnum);
2739 2740
	if (err)
		return err;
2741
	if (power_cut_emulated(c, lnum, 0))
2742
		return -EROFS;
2743 2744 2745
	return 0;
}

A
Artem Bityutskiy 已提交
2746 2747 2748 2749
/*
 * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
 * contain the stuff specific to particular file-system mounts.
 */
2750
static struct dentry *dfs_rootdir;
A
Artem Bityutskiy 已提交
2751

2752
static int dfs_file_open(struct inode *inode, struct file *file)
A
Artem Bityutskiy 已提交
2753 2754
{
	file->private_data = inode->i_private;
2755
	return nonseekable_open(inode, file);
A
Artem Bityutskiy 已提交
2756 2757
}

2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784
/**
 * provide_user_output - provide output to the user reading a debugfs file.
 * @val: boolean value for the answer
 * @u: the buffer to store the answer at
 * @count: size of the buffer
 * @ppos: position in the @u output buffer
 *
 * This is a simple helper function which stores @val boolean value in the user
 * buffer when the user reads one of UBIFS debugfs files. Returns amount of
 * bytes written to @u in case of success and a negative error code in case of
 * failure.
 */
static int provide_user_output(int val, char __user *u, size_t count,
			       loff_t *ppos)
{
	char buf[3];

	if (val)
		buf[0] = '1';
	else
		buf[0] = '0';
	buf[1] = '\n';
	buf[2] = 0x00;

	return simple_read_from_buffer(u, count, ppos, buf, 2);
}

2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804
static ssize_t dfs_file_read(struct file *file, char __user *u, size_t count,
			     loff_t *ppos)
{
	struct dentry *dent = file->f_path.dentry;
	struct ubifs_info *c = file->private_data;
	struct ubifs_debug_info *d = c->dbg;
	int val;

	if (dent == d->dfs_chk_gen)
		val = d->chk_gen;
	else if (dent == d->dfs_chk_index)
		val = d->chk_index;
	else if (dent == d->dfs_chk_orph)
		val = d->chk_orph;
	else if (dent == d->dfs_chk_lprops)
		val = d->chk_lprops;
	else if (dent == d->dfs_chk_fs)
		val = d->chk_fs;
	else if (dent == d->dfs_tst_rcvry)
		val = d->tst_rcvry;
2805 2806
	else if (dent == d->dfs_ro_error)
		val = c->ro_error;
2807 2808 2809
	else
		return -EINVAL;

2810 2811
	return provide_user_output(val, u, count, ppos);
}
2812

2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836
/**
 * interpret_user_input - interpret user debugfs file input.
 * @u: user-provided buffer with the input
 * @count: buffer size
 *
 * This is a helper function which interpret user input to a boolean UBIFS
 * debugfs file. Returns %0 or %1 in case of success and a negative error code
 * in case of failure.
 */
static int interpret_user_input(const char __user *u, size_t count)
{
	size_t buf_size;
	char buf[8];

	buf_size = min_t(size_t, count, (sizeof(buf) - 1));
	if (copy_from_user(buf, u, buf_size))
		return -EFAULT;

	if (buf[0] == '1')
		return 1;
	else if (buf[0] == '0')
		return 0;

	return -EINVAL;
2837 2838 2839 2840
}

static ssize_t dfs_file_write(struct file *file, const char __user *u,
			      size_t count, loff_t *ppos)
A
Artem Bityutskiy 已提交
2841 2842 2843
{
	struct ubifs_info *c = file->private_data;
	struct ubifs_debug_info *d = c->dbg;
2844 2845
	struct dentry *dent = file->f_path.dentry;
	int val;
A
Artem Bityutskiy 已提交
2846

2847
	/*
2848 2849 2850 2851 2852 2853 2854 2855 2856 2857
	 * TODO: this is racy - the file-system might have already been
	 * unmounted and we'd oops in this case. The plan is to fix it with
	 * help of 'iterate_supers_type()' which we should have in v3.0: when
	 * a debugfs opened, we rember FS's UUID in file->private_data. Then
	 * whenever we access the FS via a debugfs file, we iterate all UBIFS
	 * superblocks and fine the one with the same UUID, and take the
	 * locking right.
	 *
	 * The other way to go suggested by Al Viro is to create a separate
	 * 'ubifs-debug' file-system instead.
2858 2859
	 */
	if (file->f_path.dentry == d->dfs_dump_lprops) {
2860
		ubifs_dump_lprops(c);
2861 2862 2863
		return count;
	}
	if (file->f_path.dentry == d->dfs_dump_budg) {
2864
		ubifs_dump_budg(c, &c->bi);
2865 2866 2867
		return count;
	}
	if (file->f_path.dentry == d->dfs_dump_tnc) {
A
Artem Bityutskiy 已提交
2868
		mutex_lock(&c->tnc_mutex);
2869
		ubifs_dump_tnc(c);
A
Artem Bityutskiy 已提交
2870
		mutex_unlock(&c->tnc_mutex);
2871 2872 2873
		return count;
	}

2874 2875 2876
	val = interpret_user_input(u, count);
	if (val < 0)
		return val;
2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889

	if (dent == d->dfs_chk_gen)
		d->chk_gen = val;
	else if (dent == d->dfs_chk_index)
		d->chk_index = val;
	else if (dent == d->dfs_chk_orph)
		d->chk_orph = val;
	else if (dent == d->dfs_chk_lprops)
		d->chk_lprops = val;
	else if (dent == d->dfs_chk_fs)
		d->chk_fs = val;
	else if (dent == d->dfs_tst_rcvry)
		d->tst_rcvry = val;
2890 2891
	else if (dent == d->dfs_ro_error)
		c->ro_error = !!val;
2892
	else
A
Artem Bityutskiy 已提交
2893 2894 2895 2896 2897
		return -EINVAL;

	return count;
}

2898
static const struct file_operations dfs_fops = {
2899
	.open = dfs_file_open,
2900 2901
	.read = dfs_file_read,
	.write = dfs_file_write,
A
Artem Bityutskiy 已提交
2902
	.owner = THIS_MODULE,
2903
	.llseek = no_llseek,
A
Artem Bityutskiy 已提交
2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919
};

/**
 * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
 * @c: UBIFS file-system description object
 *
 * This function creates all debugfs files for this instance of UBIFS. Returns
 * zero in case of success and a negative error code in case of failure.
 *
 * Note, the only reason we have not merged this function with the
 * 'ubifs_debugging_init()' function is because it is better to initialize
 * debugfs interfaces at the very end of the mount process, and remove them at
 * the very beginning of the mount process.
 */
int dbg_debugfs_init_fs(struct ubifs_info *c)
{
2920
	int err, n;
A
Artem Bityutskiy 已提交
2921 2922 2923 2924
	const char *fname;
	struct dentry *dent;
	struct ubifs_debug_info *d = c->dbg;

2925
	if (!IS_ENABLED(CONFIG_DEBUG_FS))
2926 2927
		return 0;

2928 2929 2930 2931 2932 2933 2934 2935 2936
	n = snprintf(d->dfs_dir_name, UBIFS_DFS_DIR_LEN + 1, UBIFS_DFS_DIR_NAME,
		     c->vi.ubi_num, c->vi.vol_id);
	if (n == UBIFS_DFS_DIR_LEN) {
		/* The array size is too small */
		fname = UBIFS_DFS_DIR_NAME;
		dent = ERR_PTR(-EINVAL);
		goto out;
	}

2937 2938
	fname = d->dfs_dir_name;
	dent = debugfs_create_dir(fname, dfs_rootdir);
2939
	if (IS_ERR_OR_NULL(dent))
A
Artem Bityutskiy 已提交
2940
		goto out;
2941
	d->dfs_dir = dent;
A
Artem Bityutskiy 已提交
2942 2943

	fname = "dump_lprops";
2944
	dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2945
	if (IS_ERR_OR_NULL(dent))
A
Artem Bityutskiy 已提交
2946
		goto out_remove;
2947
	d->dfs_dump_lprops = dent;
A
Artem Bityutskiy 已提交
2948 2949

	fname = "dump_budg";
2950
	dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2951
	if (IS_ERR_OR_NULL(dent))
A
Artem Bityutskiy 已提交
2952
		goto out_remove;
2953
	d->dfs_dump_budg = dent;
A
Artem Bityutskiy 已提交
2954 2955

	fname = "dump_tnc";
2956
	dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2957
	if (IS_ERR_OR_NULL(dent))
A
Artem Bityutskiy 已提交
2958
		goto out_remove;
2959
	d->dfs_dump_tnc = dent;
A
Artem Bityutskiy 已提交
2960

2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002
	fname = "chk_general";
	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
				   &dfs_fops);
	if (IS_ERR_OR_NULL(dent))
		goto out_remove;
	d->dfs_chk_gen = dent;

	fname = "chk_index";
	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
				   &dfs_fops);
	if (IS_ERR_OR_NULL(dent))
		goto out_remove;
	d->dfs_chk_index = dent;

	fname = "chk_orphans";
	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
				   &dfs_fops);
	if (IS_ERR_OR_NULL(dent))
		goto out_remove;
	d->dfs_chk_orph = dent;

	fname = "chk_lprops";
	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
				   &dfs_fops);
	if (IS_ERR_OR_NULL(dent))
		goto out_remove;
	d->dfs_chk_lprops = dent;

	fname = "chk_fs";
	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
				   &dfs_fops);
	if (IS_ERR_OR_NULL(dent))
		goto out_remove;
	d->dfs_chk_fs = dent;

	fname = "tst_recovery";
	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
				   &dfs_fops);
	if (IS_ERR_OR_NULL(dent))
		goto out_remove;
	d->dfs_tst_rcvry = dent;

3003 3004 3005 3006 3007 3008 3009
	fname = "ro_error";
	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
				   &dfs_fops);
	if (IS_ERR_OR_NULL(dent))
		goto out_remove;
	d->dfs_ro_error = dent;

A
Artem Bityutskiy 已提交
3010 3011 3012
	return 0;

out_remove:
3013 3014
	debugfs_remove_recursive(d->dfs_dir);
out:
3015
	err = dent ? PTR_ERR(dent) : -ENODEV;
3016
	ubifs_err("cannot create \"%s\" debugfs file or directory, error %d\n",
A
Artem Bityutskiy 已提交
3017 3018 3019 3020 3021 3022 3023 3024 3025 3026
		  fname, err);
	return err;
}

/**
 * dbg_debugfs_exit_fs - remove all debugfs files.
 * @c: UBIFS file-system description object
 */
void dbg_debugfs_exit_fs(struct ubifs_info *c)
{
3027
	if (IS_ENABLED(CONFIG_DEBUG_FS))
3028
		debugfs_remove_recursive(c->dbg->dfs_dir);
A
Artem Bityutskiy 已提交
3029 3030
}

3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098
struct ubifs_global_debug_info ubifs_dbg;

static struct dentry *dfs_chk_gen;
static struct dentry *dfs_chk_index;
static struct dentry *dfs_chk_orph;
static struct dentry *dfs_chk_lprops;
static struct dentry *dfs_chk_fs;
static struct dentry *dfs_tst_rcvry;

static ssize_t dfs_global_file_read(struct file *file, char __user *u,
				    size_t count, loff_t *ppos)
{
	struct dentry *dent = file->f_path.dentry;
	int val;

	if (dent == dfs_chk_gen)
		val = ubifs_dbg.chk_gen;
	else if (dent == dfs_chk_index)
		val = ubifs_dbg.chk_index;
	else if (dent == dfs_chk_orph)
		val = ubifs_dbg.chk_orph;
	else if (dent == dfs_chk_lprops)
		val = ubifs_dbg.chk_lprops;
	else if (dent == dfs_chk_fs)
		val = ubifs_dbg.chk_fs;
	else if (dent == dfs_tst_rcvry)
		val = ubifs_dbg.tst_rcvry;
	else
		return -EINVAL;

	return provide_user_output(val, u, count, ppos);
}

static ssize_t dfs_global_file_write(struct file *file, const char __user *u,
				     size_t count, loff_t *ppos)
{
	struct dentry *dent = file->f_path.dentry;
	int val;

	val = interpret_user_input(u, count);
	if (val < 0)
		return val;

	if (dent == dfs_chk_gen)
		ubifs_dbg.chk_gen = val;
	else if (dent == dfs_chk_index)
		ubifs_dbg.chk_index = val;
	else if (dent == dfs_chk_orph)
		ubifs_dbg.chk_orph = val;
	else if (dent == dfs_chk_lprops)
		ubifs_dbg.chk_lprops = val;
	else if (dent == dfs_chk_fs)
		ubifs_dbg.chk_fs = val;
	else if (dent == dfs_tst_rcvry)
		ubifs_dbg.tst_rcvry = val;
	else
		return -EINVAL;

	return count;
}

static const struct file_operations dfs_global_fops = {
	.read = dfs_global_file_read,
	.write = dfs_global_file_write,
	.owner = THIS_MODULE,
	.llseek = no_llseek,
};

3099 3100 3101 3102 3103 3104 3105 3106 3107 3108
/**
 * dbg_debugfs_init - initialize debugfs file-system.
 *
 * UBIFS uses debugfs file-system to expose various debugging knobs to
 * user-space. This function creates "ubifs" directory in the debugfs
 * file-system. Returns zero in case of success and a negative error code in
 * case of failure.
 */
int dbg_debugfs_init(void)
{
3109 3110 3111 3112
	int err;
	const char *fname;
	struct dentry *dent;

3113
	if (!IS_ENABLED(CONFIG_DEBUG_FS))
3114 3115
		return 0;

3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162
	fname = "ubifs";
	dent = debugfs_create_dir(fname, NULL);
	if (IS_ERR_OR_NULL(dent))
		goto out;
	dfs_rootdir = dent;

	fname = "chk_general";
	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
				   &dfs_global_fops);
	if (IS_ERR_OR_NULL(dent))
		goto out_remove;
	dfs_chk_gen = dent;

	fname = "chk_index";
	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
				   &dfs_global_fops);
	if (IS_ERR_OR_NULL(dent))
		goto out_remove;
	dfs_chk_index = dent;

	fname = "chk_orphans";
	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
				   &dfs_global_fops);
	if (IS_ERR_OR_NULL(dent))
		goto out_remove;
	dfs_chk_orph = dent;

	fname = "chk_lprops";
	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
				   &dfs_global_fops);
	if (IS_ERR_OR_NULL(dent))
		goto out_remove;
	dfs_chk_lprops = dent;

	fname = "chk_fs";
	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
				   &dfs_global_fops);
	if (IS_ERR_OR_NULL(dent))
		goto out_remove;
	dfs_chk_fs = dent;

	fname = "tst_recovery";
	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
				   &dfs_global_fops);
	if (IS_ERR_OR_NULL(dent))
		goto out_remove;
	dfs_tst_rcvry = dent;
3163 3164

	return 0;
3165 3166 3167 3168 3169 3170 3171 3172

out_remove:
	debugfs_remove_recursive(dfs_rootdir);
out:
	err = dent ? PTR_ERR(dent) : -ENODEV;
	ubifs_err("cannot create \"%s\" debugfs file or directory, error %d\n",
		  fname, err);
	return err;
3173 3174 3175 3176 3177 3178 3179
}

/**
 * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
 */
void dbg_debugfs_exit(void)
{
3180
	if (IS_ENABLED(CONFIG_DEBUG_FS))
3181
		debugfs_remove_recursive(dfs_rootdir);
3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208
}

/**
 * ubifs_debugging_init - initialize UBIFS debugging.
 * @c: UBIFS file-system description object
 *
 * This function initializes debugging-related data for the file system.
 * Returns zero in case of success and a negative error code in case of
 * failure.
 */
int ubifs_debugging_init(struct ubifs_info *c)
{
	c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
	if (!c->dbg)
		return -ENOMEM;

	return 0;
}

/**
 * ubifs_debugging_exit - free debugging data.
 * @c: UBIFS file-system description object
 */
void ubifs_debugging_exit(struct ubifs_info *c)
{
	kfree(c->dbg);
}