xref: /openbmc/linux/fs/ubifs/debug.c (revision 6eb61d587f4515e4be5669eff383c0185009954f)
11e51764aSArtem Bityutskiy /*
21e51764aSArtem Bityutskiy  * This file is part of UBIFS.
31e51764aSArtem Bityutskiy  *
41e51764aSArtem Bityutskiy  * Copyright (C) 2006-2008 Nokia Corporation
51e51764aSArtem Bityutskiy  *
61e51764aSArtem Bityutskiy  * This program is free software; you can redistribute it and/or modify it
71e51764aSArtem Bityutskiy  * under the terms of the GNU General Public License version 2 as published by
81e51764aSArtem Bityutskiy  * the Free Software Foundation.
91e51764aSArtem Bityutskiy  *
101e51764aSArtem Bityutskiy  * This program is distributed in the hope that it will be useful, but WITHOUT
111e51764aSArtem Bityutskiy  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
121e51764aSArtem Bityutskiy  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
131e51764aSArtem Bityutskiy  * more details.
141e51764aSArtem Bityutskiy  *
151e51764aSArtem Bityutskiy  * You should have received a copy of the GNU General Public License along with
161e51764aSArtem Bityutskiy  * this program; if not, write to the Free Software Foundation, Inc., 51
171e51764aSArtem Bityutskiy  * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
181e51764aSArtem Bityutskiy  *
191e51764aSArtem Bityutskiy  * Authors: Artem Bityutskiy (Битюцкий Артём)
201e51764aSArtem Bityutskiy  *          Adrian Hunter
211e51764aSArtem Bityutskiy  */
221e51764aSArtem Bityutskiy 
231e51764aSArtem Bityutskiy /*
241e51764aSArtem Bityutskiy  * This file implements most of the debugging stuff which is compiled in only
251e51764aSArtem Bityutskiy  * when it is enabled. But some debugging check functions are implemented in
261e51764aSArtem Bityutskiy  * corresponding subsystem, just because they are closely related and utilize
271e51764aSArtem Bityutskiy  * various local functions of those subsystems.
281e51764aSArtem Bityutskiy  */
291e51764aSArtem Bityutskiy 
301e51764aSArtem Bityutskiy #include <linux/module.h>
31552ff317SArtem Bityutskiy #include <linux/debugfs.h>
324d61db4fSArtem Bityutskiy #include <linux/math64.h>
3381e79d38SArtem Bityutskiy #include <linux/uaccess.h>
34a7fa94a9SArtem Bityutskiy #include <linux/random.h>
35e328379aSHyunchul Lee #include <linux/ctype.h>
36a7fa94a9SArtem Bityutskiy #include "ubifs.h"
371e51764aSArtem Bityutskiy 
38b06283c7SArtem Bityutskiy static DEFINE_SPINLOCK(dbg_lock);
391e51764aSArtem Bityutskiy 
401e51764aSArtem Bityutskiy static const char *get_key_fmt(int fmt)
411e51764aSArtem Bityutskiy {
421e51764aSArtem Bityutskiy 	switch (fmt) {
431e51764aSArtem Bityutskiy 	case UBIFS_SIMPLE_KEY_FMT:
441e51764aSArtem Bityutskiy 		return "simple";
451e51764aSArtem Bityutskiy 	default:
461e51764aSArtem Bityutskiy 		return "unknown/invalid format";
471e51764aSArtem Bityutskiy 	}
481e51764aSArtem Bityutskiy }
491e51764aSArtem Bityutskiy 
501e51764aSArtem Bityutskiy static const char *get_key_hash(int hash)
511e51764aSArtem Bityutskiy {
521e51764aSArtem Bityutskiy 	switch (hash) {
531e51764aSArtem Bityutskiy 	case UBIFS_KEY_HASH_R5:
541e51764aSArtem Bityutskiy 		return "R5";
551e51764aSArtem Bityutskiy 	case UBIFS_KEY_HASH_TEST:
561e51764aSArtem Bityutskiy 		return "test";
571e51764aSArtem Bityutskiy 	default:
581e51764aSArtem Bityutskiy 		return "unknown/invalid name hash";
591e51764aSArtem Bityutskiy 	}
601e51764aSArtem Bityutskiy }
611e51764aSArtem Bityutskiy 
621e51764aSArtem Bityutskiy static const char *get_key_type(int type)
631e51764aSArtem Bityutskiy {
641e51764aSArtem Bityutskiy 	switch (type) {
651e51764aSArtem Bityutskiy 	case UBIFS_INO_KEY:
661e51764aSArtem Bityutskiy 		return "inode";
671e51764aSArtem Bityutskiy 	case UBIFS_DENT_KEY:
681e51764aSArtem Bityutskiy 		return "direntry";
691e51764aSArtem Bityutskiy 	case UBIFS_XENT_KEY:
701e51764aSArtem Bityutskiy 		return "xentry";
711e51764aSArtem Bityutskiy 	case UBIFS_DATA_KEY:
721e51764aSArtem Bityutskiy 		return "data";
731e51764aSArtem Bityutskiy 	case UBIFS_TRUN_KEY:
741e51764aSArtem Bityutskiy 		return "truncate";
751e51764aSArtem Bityutskiy 	default:
761e51764aSArtem Bityutskiy 		return "unknown/invalid key";
771e51764aSArtem Bityutskiy 	}
781e51764aSArtem Bityutskiy }
791e51764aSArtem Bityutskiy 
804315fb40SArtem Bityutskiy static const char *get_dent_type(int type)
814315fb40SArtem Bityutskiy {
824315fb40SArtem Bityutskiy 	switch (type) {
834315fb40SArtem Bityutskiy 	case UBIFS_ITYPE_REG:
844315fb40SArtem Bityutskiy 		return "file";
854315fb40SArtem Bityutskiy 	case UBIFS_ITYPE_DIR:
864315fb40SArtem Bityutskiy 		return "dir";
874315fb40SArtem Bityutskiy 	case UBIFS_ITYPE_LNK:
884315fb40SArtem Bityutskiy 		return "symlink";
894315fb40SArtem Bityutskiy 	case UBIFS_ITYPE_BLK:
904315fb40SArtem Bityutskiy 		return "blkdev";
914315fb40SArtem Bityutskiy 	case UBIFS_ITYPE_CHR:
924315fb40SArtem Bityutskiy 		return "char dev";
934315fb40SArtem Bityutskiy 	case UBIFS_ITYPE_FIFO:
944315fb40SArtem Bityutskiy 		return "fifo";
954315fb40SArtem Bityutskiy 	case UBIFS_ITYPE_SOCK:
964315fb40SArtem Bityutskiy 		return "socket";
974315fb40SArtem Bityutskiy 	default:
984315fb40SArtem Bityutskiy 		return "unknown/invalid type";
994315fb40SArtem Bityutskiy 	}
1004315fb40SArtem Bityutskiy }
1014315fb40SArtem Bityutskiy 
102515315a1SArtem Bityutskiy const char *dbg_snprintf_key(const struct ubifs_info *c,
103515315a1SArtem Bityutskiy 			     const union ubifs_key *key, char *buffer, int len)
1041e51764aSArtem Bityutskiy {
1051e51764aSArtem Bityutskiy 	char *p = buffer;
1061e51764aSArtem Bityutskiy 	int type = key_type(c, key);
1071e51764aSArtem Bityutskiy 
1081e51764aSArtem Bityutskiy 	if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
1091e51764aSArtem Bityutskiy 		switch (type) {
1101e51764aSArtem Bityutskiy 		case UBIFS_INO_KEY:
111beba0060SArtem Bityutskiy 			len -= snprintf(p, len, "(%lu, %s)",
112beba0060SArtem Bityutskiy 					(unsigned long)key_inum(c, key),
1131e51764aSArtem Bityutskiy 					get_key_type(type));
1141e51764aSArtem Bityutskiy 			break;
1151e51764aSArtem Bityutskiy 		case UBIFS_DENT_KEY:
1161e51764aSArtem Bityutskiy 		case UBIFS_XENT_KEY:
117beba0060SArtem Bityutskiy 			len -= snprintf(p, len, "(%lu, %s, %#08x)",
118e84461adSArtem Bityutskiy 					(unsigned long)key_inum(c, key),
1191e51764aSArtem Bityutskiy 					get_key_type(type), key_hash(c, key));
1201e51764aSArtem Bityutskiy 			break;
1211e51764aSArtem Bityutskiy 		case UBIFS_DATA_KEY:
122beba0060SArtem Bityutskiy 			len -= snprintf(p, len, "(%lu, %s, %u)",
123e84461adSArtem Bityutskiy 					(unsigned long)key_inum(c, key),
1241e51764aSArtem Bityutskiy 					get_key_type(type), key_block(c, key));
1251e51764aSArtem Bityutskiy 			break;
1261e51764aSArtem Bityutskiy 		case UBIFS_TRUN_KEY:
127beba0060SArtem Bityutskiy 			len -= snprintf(p, len, "(%lu, %s)",
128e84461adSArtem Bityutskiy 					(unsigned long)key_inum(c, key),
129e84461adSArtem Bityutskiy 					get_key_type(type));
1301e51764aSArtem Bityutskiy 			break;
1311e51764aSArtem Bityutskiy 		default:
132beba0060SArtem Bityutskiy 			len -= snprintf(p, len, "(bad key type: %#08x, %#08x)",
1331e51764aSArtem Bityutskiy 					key->u32[0], key->u32[1]);
1341e51764aSArtem Bityutskiy 		}
1351e51764aSArtem Bityutskiy 	} else
136beba0060SArtem Bityutskiy 		len -= snprintf(p, len, "bad key format %d", c->key_fmt);
137*6eb61d58SRichard Weinberger 	ubifs_assert(c, len > 0);
138515315a1SArtem Bityutskiy 	return p;
1391e51764aSArtem Bityutskiy }
1401e51764aSArtem Bityutskiy 
1411e51764aSArtem Bityutskiy const char *dbg_ntype(int type)
1421e51764aSArtem Bityutskiy {
1431e51764aSArtem Bityutskiy 	switch (type) {
1441e51764aSArtem Bityutskiy 	case UBIFS_PAD_NODE:
1451e51764aSArtem Bityutskiy 		return "padding node";
1461e51764aSArtem Bityutskiy 	case UBIFS_SB_NODE:
1471e51764aSArtem Bityutskiy 		return "superblock node";
1481e51764aSArtem Bityutskiy 	case UBIFS_MST_NODE:
1491e51764aSArtem Bityutskiy 		return "master node";
1501e51764aSArtem Bityutskiy 	case UBIFS_REF_NODE:
1511e51764aSArtem Bityutskiy 		return "reference node";
1521e51764aSArtem Bityutskiy 	case UBIFS_INO_NODE:
1531e51764aSArtem Bityutskiy 		return "inode node";
1541e51764aSArtem Bityutskiy 	case UBIFS_DENT_NODE:
1551e51764aSArtem Bityutskiy 		return "direntry node";
1561e51764aSArtem Bityutskiy 	case UBIFS_XENT_NODE:
1571e51764aSArtem Bityutskiy 		return "xentry node";
1581e51764aSArtem Bityutskiy 	case UBIFS_DATA_NODE:
1591e51764aSArtem Bityutskiy 		return "data node";
1601e51764aSArtem Bityutskiy 	case UBIFS_TRUN_NODE:
1611e51764aSArtem Bityutskiy 		return "truncate node";
1621e51764aSArtem Bityutskiy 	case UBIFS_IDX_NODE:
1631e51764aSArtem Bityutskiy 		return "indexing node";
1641e51764aSArtem Bityutskiy 	case UBIFS_CS_NODE:
1651e51764aSArtem Bityutskiy 		return "commit start node";
1661e51764aSArtem Bityutskiy 	case UBIFS_ORPH_NODE:
1671e51764aSArtem Bityutskiy 		return "orphan node";
1681e51764aSArtem Bityutskiy 	default:
1691e51764aSArtem Bityutskiy 		return "unknown node";
1701e51764aSArtem Bityutskiy 	}
1711e51764aSArtem Bityutskiy }
1721e51764aSArtem Bityutskiy 
1731e51764aSArtem Bityutskiy static const char *dbg_gtype(int type)
1741e51764aSArtem Bityutskiy {
1751e51764aSArtem Bityutskiy 	switch (type) {
1761e51764aSArtem Bityutskiy 	case UBIFS_NO_NODE_GROUP:
1771e51764aSArtem Bityutskiy 		return "no node group";
1781e51764aSArtem Bityutskiy 	case UBIFS_IN_NODE_GROUP:
1791e51764aSArtem Bityutskiy 		return "in node group";
1801e51764aSArtem Bityutskiy 	case UBIFS_LAST_OF_NODE_GROUP:
1811e51764aSArtem Bityutskiy 		return "last of node group";
1821e51764aSArtem Bityutskiy 	default:
1831e51764aSArtem Bityutskiy 		return "unknown";
1841e51764aSArtem Bityutskiy 	}
1851e51764aSArtem Bityutskiy }
1861e51764aSArtem Bityutskiy 
1871e51764aSArtem Bityutskiy const char *dbg_cstate(int cmt_state)
1881e51764aSArtem Bityutskiy {
1891e51764aSArtem Bityutskiy 	switch (cmt_state) {
1901e51764aSArtem Bityutskiy 	case COMMIT_RESTING:
1911e51764aSArtem Bityutskiy 		return "commit resting";
1921e51764aSArtem Bityutskiy 	case COMMIT_BACKGROUND:
1931e51764aSArtem Bityutskiy 		return "background commit requested";
1941e51764aSArtem Bityutskiy 	case COMMIT_REQUIRED:
1951e51764aSArtem Bityutskiy 		return "commit required";
1961e51764aSArtem Bityutskiy 	case COMMIT_RUNNING_BACKGROUND:
1971e51764aSArtem Bityutskiy 		return "BACKGROUND commit running";
1981e51764aSArtem Bityutskiy 	case COMMIT_RUNNING_REQUIRED:
1991e51764aSArtem Bityutskiy 		return "commit running and required";
2001e51764aSArtem Bityutskiy 	case COMMIT_BROKEN:
2011e51764aSArtem Bityutskiy 		return "broken commit";
2021e51764aSArtem Bityutskiy 	default:
2031e51764aSArtem Bityutskiy 		return "unknown commit state";
2041e51764aSArtem Bityutskiy 	}
2051e51764aSArtem Bityutskiy }
2061e51764aSArtem Bityutskiy 
20777a7ae58SArtem Bityutskiy const char *dbg_jhead(int jhead)
20877a7ae58SArtem Bityutskiy {
20977a7ae58SArtem Bityutskiy 	switch (jhead) {
21077a7ae58SArtem Bityutskiy 	case GCHD:
21177a7ae58SArtem Bityutskiy 		return "0 (GC)";
21277a7ae58SArtem Bityutskiy 	case BASEHD:
21377a7ae58SArtem Bityutskiy 		return "1 (base)";
21477a7ae58SArtem Bityutskiy 	case DATAHD:
21577a7ae58SArtem Bityutskiy 		return "2 (data)";
21677a7ae58SArtem Bityutskiy 	default:
21777a7ae58SArtem Bityutskiy 		return "unknown journal head";
21877a7ae58SArtem Bityutskiy 	}
21977a7ae58SArtem Bityutskiy }
22077a7ae58SArtem Bityutskiy 
2211e51764aSArtem Bityutskiy static void dump_ch(const struct ubifs_ch *ch)
2221e51764aSArtem Bityutskiy {
2236b38d03fSArtem Bityutskiy 	pr_err("\tmagic          %#x\n", le32_to_cpu(ch->magic));
2246b38d03fSArtem Bityutskiy 	pr_err("\tcrc            %#x\n", le32_to_cpu(ch->crc));
2256b38d03fSArtem Bityutskiy 	pr_err("\tnode_type      %d (%s)\n", ch->node_type,
2261e51764aSArtem Bityutskiy 	       dbg_ntype(ch->node_type));
2276b38d03fSArtem Bityutskiy 	pr_err("\tgroup_type     %d (%s)\n", ch->group_type,
2281e51764aSArtem Bityutskiy 	       dbg_gtype(ch->group_type));
2296b38d03fSArtem Bityutskiy 	pr_err("\tsqnum          %llu\n",
2301e51764aSArtem Bityutskiy 	       (unsigned long long)le64_to_cpu(ch->sqnum));
2316b38d03fSArtem Bityutskiy 	pr_err("\tlen            %u\n", le32_to_cpu(ch->len));
2321e51764aSArtem Bityutskiy }
2331e51764aSArtem Bityutskiy 
234edf6be24SArtem Bityutskiy void ubifs_dump_inode(struct ubifs_info *c, const struct inode *inode)
2351e51764aSArtem Bityutskiy {
2361e51764aSArtem Bityutskiy 	const struct ubifs_inode *ui = ubifs_inode(inode);
237f4f61d2cSRichard Weinberger 	struct fscrypt_name nm = {0};
2384315fb40SArtem Bityutskiy 	union ubifs_key key;
2394315fb40SArtem Bityutskiy 	struct ubifs_dent_node *dent, *pdent = NULL;
2404315fb40SArtem Bityutskiy 	int count = 2;
2411e51764aSArtem Bityutskiy 
2426b38d03fSArtem Bityutskiy 	pr_err("Dump in-memory inode:");
2436b38d03fSArtem Bityutskiy 	pr_err("\tinode          %lu\n", inode->i_ino);
2446b38d03fSArtem Bityutskiy 	pr_err("\tsize           %llu\n",
2451e51764aSArtem Bityutskiy 	       (unsigned long long)i_size_read(inode));
2466b38d03fSArtem Bityutskiy 	pr_err("\tnlink          %u\n", inode->i_nlink);
247782c3fb2SLinus Torvalds 	pr_err("\tuid            %u\n", (unsigned int)i_uid_read(inode));
248782c3fb2SLinus Torvalds 	pr_err("\tgid            %u\n", (unsigned int)i_gid_read(inode));
2496b38d03fSArtem Bityutskiy 	pr_err("\tatime          %u.%u\n",
2501e51764aSArtem Bityutskiy 	       (unsigned int)inode->i_atime.tv_sec,
2511e51764aSArtem Bityutskiy 	       (unsigned int)inode->i_atime.tv_nsec);
2526b38d03fSArtem Bityutskiy 	pr_err("\tmtime          %u.%u\n",
2531e51764aSArtem Bityutskiy 	       (unsigned int)inode->i_mtime.tv_sec,
2541e51764aSArtem Bityutskiy 	       (unsigned int)inode->i_mtime.tv_nsec);
2556b38d03fSArtem Bityutskiy 	pr_err("\tctime          %u.%u\n",
2561e51764aSArtem Bityutskiy 	       (unsigned int)inode->i_ctime.tv_sec,
2571e51764aSArtem Bityutskiy 	       (unsigned int)inode->i_ctime.tv_nsec);
2586b38d03fSArtem Bityutskiy 	pr_err("\tcreat_sqnum    %llu\n", ui->creat_sqnum);
2596b38d03fSArtem Bityutskiy 	pr_err("\txattr_size     %u\n", ui->xattr_size);
2606b38d03fSArtem Bityutskiy 	pr_err("\txattr_cnt      %u\n", ui->xattr_cnt);
2616b38d03fSArtem Bityutskiy 	pr_err("\txattr_names    %u\n", ui->xattr_names);
2626b38d03fSArtem Bityutskiy 	pr_err("\tdirty          %u\n", ui->dirty);
2636b38d03fSArtem Bityutskiy 	pr_err("\txattr          %u\n", ui->xattr);
2641112018cSAndreas Gruenbacher 	pr_err("\tbulk_read      %u\n", ui->bulk_read);
2656b38d03fSArtem Bityutskiy 	pr_err("\tsynced_i_size  %llu\n",
266b5e426e9SArtem Bityutskiy 	       (unsigned long long)ui->synced_i_size);
2676b38d03fSArtem Bityutskiy 	pr_err("\tui_size        %llu\n",
268b5e426e9SArtem Bityutskiy 	       (unsigned long long)ui->ui_size);
2696b38d03fSArtem Bityutskiy 	pr_err("\tflags          %d\n", ui->flags);
2706b38d03fSArtem Bityutskiy 	pr_err("\tcompr_type     %d\n", ui->compr_type);
2716b38d03fSArtem Bityutskiy 	pr_err("\tlast_page_read %lu\n", ui->last_page_read);
2726b38d03fSArtem Bityutskiy 	pr_err("\tread_in_a_row  %lu\n", ui->read_in_a_row);
2736b38d03fSArtem Bityutskiy 	pr_err("\tdata_len       %d\n", ui->data_len);
2744315fb40SArtem Bityutskiy 
2754315fb40SArtem Bityutskiy 	if (!S_ISDIR(inode->i_mode))
2764315fb40SArtem Bityutskiy 		return;
2774315fb40SArtem Bityutskiy 
2786b38d03fSArtem Bityutskiy 	pr_err("List of directory entries:\n");
279*6eb61d58SRichard Weinberger 	ubifs_assert(c, !mutex_is_locked(&c->tnc_mutex));
2804315fb40SArtem Bityutskiy 
2814315fb40SArtem Bityutskiy 	lowest_dent_key(c, &key, inode->i_ino);
2824315fb40SArtem Bityutskiy 	while (1) {
2834315fb40SArtem Bityutskiy 		dent = ubifs_tnc_next_ent(c, &key, &nm);
2844315fb40SArtem Bityutskiy 		if (IS_ERR(dent)) {
2854315fb40SArtem Bityutskiy 			if (PTR_ERR(dent) != -ENOENT)
2866b38d03fSArtem Bityutskiy 				pr_err("error %ld\n", PTR_ERR(dent));
2874315fb40SArtem Bityutskiy 			break;
2884315fb40SArtem Bityutskiy 		}
2894315fb40SArtem Bityutskiy 
29033fda9faSHyunchul Lee 		pr_err("\t%d: inode %llu, type %s, len %d\n",
29133fda9faSHyunchul Lee 		       count++, (unsigned long long) le64_to_cpu(dent->inum),
29233fda9faSHyunchul Lee 		       get_dent_type(dent->type),
29333fda9faSHyunchul Lee 		       le16_to_cpu(dent->nlen));
2944315fb40SArtem Bityutskiy 
295f4f61d2cSRichard Weinberger 		fname_name(&nm) = dent->name;
296f4f61d2cSRichard Weinberger 		fname_len(&nm) = le16_to_cpu(dent->nlen);
2974315fb40SArtem Bityutskiy 		kfree(pdent);
2984315fb40SArtem Bityutskiy 		pdent = dent;
2994315fb40SArtem Bityutskiy 		key_read(c, &dent->key, &key);
3004315fb40SArtem Bityutskiy 	}
3014315fb40SArtem Bityutskiy 	kfree(pdent);
3021e51764aSArtem Bityutskiy }
3031e51764aSArtem Bityutskiy 
304edf6be24SArtem Bityutskiy void ubifs_dump_node(const struct ubifs_info *c, const void *node)
3051e51764aSArtem Bityutskiy {
3061e51764aSArtem Bityutskiy 	int i, n;
3071e51764aSArtem Bityutskiy 	union ubifs_key key;
3081e51764aSArtem Bityutskiy 	const struct ubifs_ch *ch = node;
309515315a1SArtem Bityutskiy 	char key_buf[DBG_KEY_BUF_LEN];
3101e51764aSArtem Bityutskiy 
3111e51764aSArtem Bityutskiy 	/* If the magic is incorrect, just hexdump the first bytes */
3121e51764aSArtem Bityutskiy 	if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
3136b38d03fSArtem Bityutskiy 		pr_err("Not a node, first %zu bytes:", UBIFS_CH_SZ);
31416c395caSArtem Bityutskiy 		print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 32, 1,
3151e51764aSArtem Bityutskiy 			       (void *)node, UBIFS_CH_SZ, 1);
3161e51764aSArtem Bityutskiy 		return;
3171e51764aSArtem Bityutskiy 	}
3181e51764aSArtem Bityutskiy 
3191e51764aSArtem Bityutskiy 	spin_lock(&dbg_lock);
3201e51764aSArtem Bityutskiy 	dump_ch(node);
3211e51764aSArtem Bityutskiy 
3221e51764aSArtem Bityutskiy 	switch (ch->node_type) {
3231e51764aSArtem Bityutskiy 	case UBIFS_PAD_NODE:
3241e51764aSArtem Bityutskiy 	{
3251e51764aSArtem Bityutskiy 		const struct ubifs_pad_node *pad = node;
3261e51764aSArtem Bityutskiy 
3276b38d03fSArtem Bityutskiy 		pr_err("\tpad_len        %u\n", le32_to_cpu(pad->pad_len));
3281e51764aSArtem Bityutskiy 		break;
3291e51764aSArtem Bityutskiy 	}
3301e51764aSArtem Bityutskiy 	case UBIFS_SB_NODE:
3311e51764aSArtem Bityutskiy 	{
3321e51764aSArtem Bityutskiy 		const struct ubifs_sb_node *sup = node;
3331e51764aSArtem Bityutskiy 		unsigned int sup_flags = le32_to_cpu(sup->flags);
3341e51764aSArtem Bityutskiy 
3356b38d03fSArtem Bityutskiy 		pr_err("\tkey_hash       %d (%s)\n",
3361e51764aSArtem Bityutskiy 		       (int)sup->key_hash, get_key_hash(sup->key_hash));
3376b38d03fSArtem Bityutskiy 		pr_err("\tkey_fmt        %d (%s)\n",
3381e51764aSArtem Bityutskiy 		       (int)sup->key_fmt, get_key_fmt(sup->key_fmt));
3396b38d03fSArtem Bityutskiy 		pr_err("\tflags          %#x\n", sup_flags);
3406b38d03fSArtem Bityutskiy 		pr_err("\tbig_lpt        %u\n",
3411e51764aSArtem Bityutskiy 		       !!(sup_flags & UBIFS_FLG_BIGLPT));
3426b38d03fSArtem Bityutskiy 		pr_err("\tspace_fixup    %u\n",
3439f58d350SMatthew L. Creech 		       !!(sup_flags & UBIFS_FLG_SPACE_FIXUP));
3446b38d03fSArtem Bityutskiy 		pr_err("\tmin_io_size    %u\n", le32_to_cpu(sup->min_io_size));
3456b38d03fSArtem Bityutskiy 		pr_err("\tleb_size       %u\n", le32_to_cpu(sup->leb_size));
3466b38d03fSArtem Bityutskiy 		pr_err("\tleb_cnt        %u\n", le32_to_cpu(sup->leb_cnt));
3476b38d03fSArtem Bityutskiy 		pr_err("\tmax_leb_cnt    %u\n", le32_to_cpu(sup->max_leb_cnt));
3486b38d03fSArtem Bityutskiy 		pr_err("\tmax_bud_bytes  %llu\n",
3491e51764aSArtem Bityutskiy 		       (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
3506b38d03fSArtem Bityutskiy 		pr_err("\tlog_lebs       %u\n", le32_to_cpu(sup->log_lebs));
3516b38d03fSArtem Bityutskiy 		pr_err("\tlpt_lebs       %u\n", le32_to_cpu(sup->lpt_lebs));
3526b38d03fSArtem Bityutskiy 		pr_err("\torph_lebs      %u\n", le32_to_cpu(sup->orph_lebs));
3536b38d03fSArtem Bityutskiy 		pr_err("\tjhead_cnt      %u\n", le32_to_cpu(sup->jhead_cnt));
3546b38d03fSArtem Bityutskiy 		pr_err("\tfanout         %u\n", le32_to_cpu(sup->fanout));
3556b38d03fSArtem Bityutskiy 		pr_err("\tlsave_cnt      %u\n", le32_to_cpu(sup->lsave_cnt));
3566b38d03fSArtem Bityutskiy 		pr_err("\tdefault_compr  %u\n",
3571e51764aSArtem Bityutskiy 		       (int)le16_to_cpu(sup->default_compr));
3586b38d03fSArtem Bityutskiy 		pr_err("\trp_size        %llu\n",
3591e51764aSArtem Bityutskiy 		       (unsigned long long)le64_to_cpu(sup->rp_size));
3606b38d03fSArtem Bityutskiy 		pr_err("\trp_uid         %u\n", le32_to_cpu(sup->rp_uid));
3616b38d03fSArtem Bityutskiy 		pr_err("\trp_gid         %u\n", le32_to_cpu(sup->rp_gid));
3626b38d03fSArtem Bityutskiy 		pr_err("\tfmt_version    %u\n", le32_to_cpu(sup->fmt_version));
3636b38d03fSArtem Bityutskiy 		pr_err("\ttime_gran      %u\n", le32_to_cpu(sup->time_gran));
3646b38d03fSArtem Bityutskiy 		pr_err("\tUUID           %pUB\n", sup->uuid);
3651e51764aSArtem Bityutskiy 		break;
3661e51764aSArtem Bityutskiy 	}
3671e51764aSArtem Bityutskiy 	case UBIFS_MST_NODE:
3681e51764aSArtem Bityutskiy 	{
3691e51764aSArtem Bityutskiy 		const struct ubifs_mst_node *mst = node;
3701e51764aSArtem Bityutskiy 
3716b38d03fSArtem Bityutskiy 		pr_err("\thighest_inum   %llu\n",
3721e51764aSArtem Bityutskiy 		       (unsigned long long)le64_to_cpu(mst->highest_inum));
3736b38d03fSArtem Bityutskiy 		pr_err("\tcommit number  %llu\n",
3741e51764aSArtem Bityutskiy 		       (unsigned long long)le64_to_cpu(mst->cmt_no));
3756b38d03fSArtem Bityutskiy 		pr_err("\tflags          %#x\n", le32_to_cpu(mst->flags));
3766b38d03fSArtem Bityutskiy 		pr_err("\tlog_lnum       %u\n", le32_to_cpu(mst->log_lnum));
3776b38d03fSArtem Bityutskiy 		pr_err("\troot_lnum      %u\n", le32_to_cpu(mst->root_lnum));
3786b38d03fSArtem Bityutskiy 		pr_err("\troot_offs      %u\n", le32_to_cpu(mst->root_offs));
3796b38d03fSArtem Bityutskiy 		pr_err("\troot_len       %u\n", le32_to_cpu(mst->root_len));
3806b38d03fSArtem Bityutskiy 		pr_err("\tgc_lnum        %u\n", le32_to_cpu(mst->gc_lnum));
3816b38d03fSArtem Bityutskiy 		pr_err("\tihead_lnum     %u\n", le32_to_cpu(mst->ihead_lnum));
3826b38d03fSArtem Bityutskiy 		pr_err("\tihead_offs     %u\n", le32_to_cpu(mst->ihead_offs));
3836b38d03fSArtem Bityutskiy 		pr_err("\tindex_size     %llu\n",
3840ecb9529SHarvey Harrison 		       (unsigned long long)le64_to_cpu(mst->index_size));
3856b38d03fSArtem Bityutskiy 		pr_err("\tlpt_lnum       %u\n", le32_to_cpu(mst->lpt_lnum));
3866b38d03fSArtem Bityutskiy 		pr_err("\tlpt_offs       %u\n", le32_to_cpu(mst->lpt_offs));
3876b38d03fSArtem Bityutskiy 		pr_err("\tnhead_lnum     %u\n", le32_to_cpu(mst->nhead_lnum));
3886b38d03fSArtem Bityutskiy 		pr_err("\tnhead_offs     %u\n", le32_to_cpu(mst->nhead_offs));
3896b38d03fSArtem Bityutskiy 		pr_err("\tltab_lnum      %u\n", le32_to_cpu(mst->ltab_lnum));
3906b38d03fSArtem Bityutskiy 		pr_err("\tltab_offs      %u\n", le32_to_cpu(mst->ltab_offs));
3916b38d03fSArtem Bityutskiy 		pr_err("\tlsave_lnum     %u\n", le32_to_cpu(mst->lsave_lnum));
3926b38d03fSArtem Bityutskiy 		pr_err("\tlsave_offs     %u\n", le32_to_cpu(mst->lsave_offs));
3936b38d03fSArtem Bityutskiy 		pr_err("\tlscan_lnum     %u\n", le32_to_cpu(mst->lscan_lnum));
3946b38d03fSArtem Bityutskiy 		pr_err("\tleb_cnt        %u\n", le32_to_cpu(mst->leb_cnt));
3956b38d03fSArtem Bityutskiy 		pr_err("\tempty_lebs     %u\n", le32_to_cpu(mst->empty_lebs));
3966b38d03fSArtem Bityutskiy 		pr_err("\tidx_lebs       %u\n", le32_to_cpu(mst->idx_lebs));
3976b38d03fSArtem Bityutskiy 		pr_err("\ttotal_free     %llu\n",
3981e51764aSArtem Bityutskiy 		       (unsigned long long)le64_to_cpu(mst->total_free));
3996b38d03fSArtem Bityutskiy 		pr_err("\ttotal_dirty    %llu\n",
4001e51764aSArtem Bityutskiy 		       (unsigned long long)le64_to_cpu(mst->total_dirty));
4016b38d03fSArtem Bityutskiy 		pr_err("\ttotal_used     %llu\n",
4021e51764aSArtem Bityutskiy 		       (unsigned long long)le64_to_cpu(mst->total_used));
4036b38d03fSArtem Bityutskiy 		pr_err("\ttotal_dead     %llu\n",
4041e51764aSArtem Bityutskiy 		       (unsigned long long)le64_to_cpu(mst->total_dead));
4056b38d03fSArtem Bityutskiy 		pr_err("\ttotal_dark     %llu\n",
4061e51764aSArtem Bityutskiy 		       (unsigned long long)le64_to_cpu(mst->total_dark));
4071e51764aSArtem Bityutskiy 		break;
4081e51764aSArtem Bityutskiy 	}
4091e51764aSArtem Bityutskiy 	case UBIFS_REF_NODE:
4101e51764aSArtem Bityutskiy 	{
4111e51764aSArtem Bityutskiy 		const struct ubifs_ref_node *ref = node;
4121e51764aSArtem Bityutskiy 
4136b38d03fSArtem Bityutskiy 		pr_err("\tlnum           %u\n", le32_to_cpu(ref->lnum));
4146b38d03fSArtem Bityutskiy 		pr_err("\toffs           %u\n", le32_to_cpu(ref->offs));
4156b38d03fSArtem Bityutskiy 		pr_err("\tjhead          %u\n", le32_to_cpu(ref->jhead));
4161e51764aSArtem Bityutskiy 		break;
4171e51764aSArtem Bityutskiy 	}
4181e51764aSArtem Bityutskiy 	case UBIFS_INO_NODE:
4191e51764aSArtem Bityutskiy 	{
4201e51764aSArtem Bityutskiy 		const struct ubifs_ino_node *ino = node;
4211e51764aSArtem Bityutskiy 
4221e51764aSArtem Bityutskiy 		key_read(c, &ino->key, &key);
4236b38d03fSArtem Bityutskiy 		pr_err("\tkey            %s\n",
424515315a1SArtem Bityutskiy 		       dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
4256b38d03fSArtem Bityutskiy 		pr_err("\tcreat_sqnum    %llu\n",
4261e51764aSArtem Bityutskiy 		       (unsigned long long)le64_to_cpu(ino->creat_sqnum));
4276b38d03fSArtem Bityutskiy 		pr_err("\tsize           %llu\n",
4281e51764aSArtem Bityutskiy 		       (unsigned long long)le64_to_cpu(ino->size));
4296b38d03fSArtem Bityutskiy 		pr_err("\tnlink          %u\n", le32_to_cpu(ino->nlink));
4306b38d03fSArtem Bityutskiy 		pr_err("\tatime          %lld.%u\n",
4311e51764aSArtem Bityutskiy 		       (long long)le64_to_cpu(ino->atime_sec),
4321e51764aSArtem Bityutskiy 		       le32_to_cpu(ino->atime_nsec));
4336b38d03fSArtem Bityutskiy 		pr_err("\tmtime          %lld.%u\n",
4341e51764aSArtem Bityutskiy 		       (long long)le64_to_cpu(ino->mtime_sec),
4351e51764aSArtem Bityutskiy 		       le32_to_cpu(ino->mtime_nsec));
4366b38d03fSArtem Bityutskiy 		pr_err("\tctime          %lld.%u\n",
4371e51764aSArtem Bityutskiy 		       (long long)le64_to_cpu(ino->ctime_sec),
4381e51764aSArtem Bityutskiy 		       le32_to_cpu(ino->ctime_nsec));
4396b38d03fSArtem Bityutskiy 		pr_err("\tuid            %u\n", le32_to_cpu(ino->uid));
4406b38d03fSArtem Bityutskiy 		pr_err("\tgid            %u\n", le32_to_cpu(ino->gid));
4416b38d03fSArtem Bityutskiy 		pr_err("\tmode           %u\n", le32_to_cpu(ino->mode));
4426b38d03fSArtem Bityutskiy 		pr_err("\tflags          %#x\n", le32_to_cpu(ino->flags));
4436b38d03fSArtem Bityutskiy 		pr_err("\txattr_cnt      %u\n", le32_to_cpu(ino->xattr_cnt));
4446b38d03fSArtem Bityutskiy 		pr_err("\txattr_size     %u\n", le32_to_cpu(ino->xattr_size));
4456b38d03fSArtem Bityutskiy 		pr_err("\txattr_names    %u\n", le32_to_cpu(ino->xattr_names));
4466b38d03fSArtem Bityutskiy 		pr_err("\tcompr_type     %#x\n",
4471e51764aSArtem Bityutskiy 		       (int)le16_to_cpu(ino->compr_type));
4486b38d03fSArtem Bityutskiy 		pr_err("\tdata len       %u\n", le32_to_cpu(ino->data_len));
4491e51764aSArtem Bityutskiy 		break;
4501e51764aSArtem Bityutskiy 	}
4511e51764aSArtem Bityutskiy 	case UBIFS_DENT_NODE:
4521e51764aSArtem Bityutskiy 	case UBIFS_XENT_NODE:
4531e51764aSArtem Bityutskiy 	{
4541e51764aSArtem Bityutskiy 		const struct ubifs_dent_node *dent = node;
4551e51764aSArtem Bityutskiy 		int nlen = le16_to_cpu(dent->nlen);
4561e51764aSArtem Bityutskiy 
4571e51764aSArtem Bityutskiy 		key_read(c, &dent->key, &key);
4586b38d03fSArtem Bityutskiy 		pr_err("\tkey            %s\n",
459515315a1SArtem Bityutskiy 		       dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
4606b38d03fSArtem Bityutskiy 		pr_err("\tinum           %llu\n",
4611e51764aSArtem Bityutskiy 		       (unsigned long long)le64_to_cpu(dent->inum));
4626b38d03fSArtem Bityutskiy 		pr_err("\ttype           %d\n", (int)dent->type);
4636b38d03fSArtem Bityutskiy 		pr_err("\tnlen           %d\n", nlen);
4646b38d03fSArtem Bityutskiy 		pr_err("\tname           ");
4651e51764aSArtem Bityutskiy 
4661e51764aSArtem Bityutskiy 		if (nlen > UBIFS_MAX_NLEN)
4676b38d03fSArtem Bityutskiy 			pr_err("(bad name length, not printing, bad or corrupted node)");
4681e51764aSArtem Bityutskiy 		else {
4691e51764aSArtem Bityutskiy 			for (i = 0; i < nlen && dent->name[i]; i++)
470e328379aSHyunchul Lee 				pr_cont("%c", isprint(dent->name[i]) ?
471e328379aSHyunchul Lee 					dent->name[i] : '?');
4721e51764aSArtem Bityutskiy 		}
4736b38d03fSArtem Bityutskiy 		pr_cont("\n");
4741e51764aSArtem Bityutskiy 
4751e51764aSArtem Bityutskiy 		break;
4761e51764aSArtem Bityutskiy 	}
4771e51764aSArtem Bityutskiy 	case UBIFS_DATA_NODE:
4781e51764aSArtem Bityutskiy 	{
4791e51764aSArtem Bityutskiy 		const struct ubifs_data_node *dn = node;
4801e51764aSArtem Bityutskiy 		int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
4811e51764aSArtem Bityutskiy 
4821e51764aSArtem Bityutskiy 		key_read(c, &dn->key, &key);
4836b38d03fSArtem Bityutskiy 		pr_err("\tkey            %s\n",
484515315a1SArtem Bityutskiy 		       dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
4856b38d03fSArtem Bityutskiy 		pr_err("\tsize           %u\n", le32_to_cpu(dn->size));
4866b38d03fSArtem Bityutskiy 		pr_err("\tcompr_typ      %d\n",
4871e51764aSArtem Bityutskiy 		       (int)le16_to_cpu(dn->compr_type));
4886b38d03fSArtem Bityutskiy 		pr_err("\tdata size      %d\n", dlen);
4896b38d03fSArtem Bityutskiy 		pr_err("\tdata:\n");
49016c395caSArtem Bityutskiy 		print_hex_dump(KERN_ERR, "\t", DUMP_PREFIX_OFFSET, 32, 1,
4911e51764aSArtem Bityutskiy 			       (void *)&dn->data, dlen, 0);
4921e51764aSArtem Bityutskiy 		break;
4931e51764aSArtem Bityutskiy 	}
4941e51764aSArtem Bityutskiy 	case UBIFS_TRUN_NODE:
4951e51764aSArtem Bityutskiy 	{
4961e51764aSArtem Bityutskiy 		const struct ubifs_trun_node *trun = node;
4971e51764aSArtem Bityutskiy 
4986b38d03fSArtem Bityutskiy 		pr_err("\tinum           %u\n", le32_to_cpu(trun->inum));
4996b38d03fSArtem Bityutskiy 		pr_err("\told_size       %llu\n",
5001e51764aSArtem Bityutskiy 		       (unsigned long long)le64_to_cpu(trun->old_size));
5016b38d03fSArtem Bityutskiy 		pr_err("\tnew_size       %llu\n",
5021e51764aSArtem Bityutskiy 		       (unsigned long long)le64_to_cpu(trun->new_size));
5031e51764aSArtem Bityutskiy 		break;
5041e51764aSArtem Bityutskiy 	}
5051e51764aSArtem Bityutskiy 	case UBIFS_IDX_NODE:
5061e51764aSArtem Bityutskiy 	{
5071e51764aSArtem Bityutskiy 		const struct ubifs_idx_node *idx = node;
5081e51764aSArtem Bityutskiy 
5091e51764aSArtem Bityutskiy 		n = le16_to_cpu(idx->child_cnt);
5106b38d03fSArtem Bityutskiy 		pr_err("\tchild_cnt      %d\n", n);
5116b38d03fSArtem Bityutskiy 		pr_err("\tlevel          %d\n", (int)le16_to_cpu(idx->level));
5126b38d03fSArtem Bityutskiy 		pr_err("\tBranches:\n");
5131e51764aSArtem Bityutskiy 
5141e51764aSArtem Bityutskiy 		for (i = 0; i < n && i < c->fanout - 1; i++) {
5151e51764aSArtem Bityutskiy 			const struct ubifs_branch *br;
5161e51764aSArtem Bityutskiy 
5171e51764aSArtem Bityutskiy 			br = ubifs_idx_branch(c, idx, i);
5181e51764aSArtem Bityutskiy 			key_read(c, &br->key, &key);
5196b38d03fSArtem Bityutskiy 			pr_err("\t%d: LEB %d:%d len %d key %s\n",
5201e51764aSArtem Bityutskiy 			       i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
521515315a1SArtem Bityutskiy 			       le32_to_cpu(br->len),
522515315a1SArtem Bityutskiy 			       dbg_snprintf_key(c, &key, key_buf,
523515315a1SArtem Bityutskiy 						DBG_KEY_BUF_LEN));
5241e51764aSArtem Bityutskiy 		}
5251e51764aSArtem Bityutskiy 		break;
5261e51764aSArtem Bityutskiy 	}
5271e51764aSArtem Bityutskiy 	case UBIFS_CS_NODE:
5281e51764aSArtem Bityutskiy 		break;
5291e51764aSArtem Bityutskiy 	case UBIFS_ORPH_NODE:
5301e51764aSArtem Bityutskiy 	{
5311e51764aSArtem Bityutskiy 		const struct ubifs_orph_node *orph = node;
5321e51764aSArtem Bityutskiy 
5336b38d03fSArtem Bityutskiy 		pr_err("\tcommit number  %llu\n",
5341e51764aSArtem Bityutskiy 		       (unsigned long long)
5351e51764aSArtem Bityutskiy 				le64_to_cpu(orph->cmt_no) & LLONG_MAX);
5366b38d03fSArtem Bityutskiy 		pr_err("\tlast node flag %llu\n",
5371e51764aSArtem Bityutskiy 		       (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
5381e51764aSArtem Bityutskiy 		n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
5396b38d03fSArtem Bityutskiy 		pr_err("\t%d orphan inode numbers:\n", n);
5401e51764aSArtem Bityutskiy 		for (i = 0; i < n; i++)
5416b38d03fSArtem Bityutskiy 			pr_err("\t  ino %llu\n",
5427424bac8SAlexander Beregalov 			       (unsigned long long)le64_to_cpu(orph->inos[i]));
5431e51764aSArtem Bityutskiy 		break;
5441e51764aSArtem Bityutskiy 	}
5451e51764aSArtem Bityutskiy 	default:
5466b38d03fSArtem Bityutskiy 		pr_err("node type %d was not recognized\n",
5471e51764aSArtem Bityutskiy 		       (int)ch->node_type);
5481e51764aSArtem Bityutskiy 	}
5491e51764aSArtem Bityutskiy 	spin_unlock(&dbg_lock);
5501e51764aSArtem Bityutskiy }
5511e51764aSArtem Bityutskiy 
552edf6be24SArtem Bityutskiy void ubifs_dump_budget_req(const struct ubifs_budget_req *req)
5531e51764aSArtem Bityutskiy {
5541e51764aSArtem Bityutskiy 	spin_lock(&dbg_lock);
5556b38d03fSArtem Bityutskiy 	pr_err("Budgeting request: new_ino %d, dirtied_ino %d\n",
5561e51764aSArtem Bityutskiy 	       req->new_ino, req->dirtied_ino);
5576b38d03fSArtem Bityutskiy 	pr_err("\tnew_ino_d   %d, dirtied_ino_d %d\n",
5581e51764aSArtem Bityutskiy 	       req->new_ino_d, req->dirtied_ino_d);
5596b38d03fSArtem Bityutskiy 	pr_err("\tnew_page    %d, dirtied_page %d\n",
5601e51764aSArtem Bityutskiy 	       req->new_page, req->dirtied_page);
5616b38d03fSArtem Bityutskiy 	pr_err("\tnew_dent    %d, mod_dent     %d\n",
5621e51764aSArtem Bityutskiy 	       req->new_dent, req->mod_dent);
5636b38d03fSArtem Bityutskiy 	pr_err("\tidx_growth  %d\n", req->idx_growth);
5646b38d03fSArtem Bityutskiy 	pr_err("\tdata_growth %d dd_growth     %d\n",
5651e51764aSArtem Bityutskiy 	       req->data_growth, req->dd_growth);
5661e51764aSArtem Bityutskiy 	spin_unlock(&dbg_lock);
5671e51764aSArtem Bityutskiy }
5681e51764aSArtem Bityutskiy 
569edf6be24SArtem Bityutskiy void ubifs_dump_lstats(const struct ubifs_lp_stats *lst)
5701e51764aSArtem Bityutskiy {
5711e51764aSArtem Bityutskiy 	spin_lock(&dbg_lock);
5726b38d03fSArtem Bityutskiy 	pr_err("(pid %d) Lprops statistics: empty_lebs %d, idx_lebs  %d\n",
57379fda517SArtem Bityutskiy 	       current->pid, lst->empty_lebs, lst->idx_lebs);
5746b38d03fSArtem Bityutskiy 	pr_err("\ttaken_empty_lebs %d, total_free %lld, total_dirty %lld\n",
57579fda517SArtem Bityutskiy 	       lst->taken_empty_lebs, lst->total_free, lst->total_dirty);
5766b38d03fSArtem Bityutskiy 	pr_err("\ttotal_used %lld, total_dark %lld, total_dead %lld\n",
57779fda517SArtem Bityutskiy 	       lst->total_used, lst->total_dark, lst->total_dead);
5781e51764aSArtem Bityutskiy 	spin_unlock(&dbg_lock);
5791e51764aSArtem Bityutskiy }
5801e51764aSArtem Bityutskiy 
581edf6be24SArtem Bityutskiy void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)
5821e51764aSArtem Bityutskiy {
5831e51764aSArtem Bityutskiy 	int i;
5841e51764aSArtem Bityutskiy 	struct rb_node *rb;
5851e51764aSArtem Bityutskiy 	struct ubifs_bud *bud;
5861e51764aSArtem Bityutskiy 	struct ubifs_gced_idx_leb *idx_gc;
58721a60258SArtem Bityutskiy 	long long available, outstanding, free;
5881e51764aSArtem Bityutskiy 
5898ff83089SArtem Bityutskiy 	spin_lock(&c->space_lock);
5901e51764aSArtem Bityutskiy 	spin_lock(&dbg_lock);
5916b38d03fSArtem Bityutskiy 	pr_err("(pid %d) Budgeting info: data budget sum %lld, total budget sum %lld\n",
59279fda517SArtem Bityutskiy 	       current->pid, bi->data_growth + bi->dd_growth,
593f1bd66afSArtem Bityutskiy 	       bi->data_growth + bi->dd_growth + bi->idx_growth);
5946b38d03fSArtem Bityutskiy 	pr_err("\tbudg_data_growth %lld, budg_dd_growth %lld, budg_idx_growth %lld\n",
59579fda517SArtem Bityutskiy 	       bi->data_growth, bi->dd_growth, bi->idx_growth);
5966b38d03fSArtem Bityutskiy 	pr_err("\tmin_idx_lebs %d, old_idx_sz %llu, uncommitted_idx %lld\n",
59779fda517SArtem Bityutskiy 	       bi->min_idx_lebs, bi->old_idx_sz, bi->uncommitted_idx);
5986b38d03fSArtem Bityutskiy 	pr_err("\tpage_budget %d, inode_budget %d, dent_budget %d\n",
599f1bd66afSArtem Bityutskiy 	       bi->page_budget, bi->inode_budget, bi->dent_budget);
6006b38d03fSArtem Bityutskiy 	pr_err("\tnospace %u, nospace_rp %u\n", bi->nospace, bi->nospace_rp);
6016b38d03fSArtem Bityutskiy 	pr_err("\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
6028c3067e4SArtem Bityutskiy 	       c->dark_wm, c->dead_wm, c->max_idx_node_sz);
603f1bd66afSArtem Bityutskiy 
604f1bd66afSArtem Bityutskiy 	if (bi != &c->bi)
605f1bd66afSArtem Bityutskiy 		/*
606f1bd66afSArtem Bityutskiy 		 * If we are dumping saved budgeting data, do not print
607f1bd66afSArtem Bityutskiy 		 * additional information which is about the current state, not
608f1bd66afSArtem Bityutskiy 		 * the old one which corresponded to the saved budgeting data.
609f1bd66afSArtem Bityutskiy 		 */
610f1bd66afSArtem Bityutskiy 		goto out_unlock;
611f1bd66afSArtem Bityutskiy 
6126b38d03fSArtem Bityutskiy 	pr_err("\tfreeable_cnt %d, calc_idx_sz %lld, idx_gc_cnt %d\n",
6138c3067e4SArtem Bityutskiy 	       c->freeable_cnt, c->calc_idx_sz, c->idx_gc_cnt);
6146b38d03fSArtem Bityutskiy 	pr_err("\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, clean_zn_cnt %ld\n",
61579fda517SArtem Bityutskiy 	       atomic_long_read(&c->dirty_pg_cnt),
6161e51764aSArtem Bityutskiy 	       atomic_long_read(&c->dirty_zn_cnt),
6171e51764aSArtem Bityutskiy 	       atomic_long_read(&c->clean_zn_cnt));
6186b38d03fSArtem Bityutskiy 	pr_err("\tgc_lnum %d, ihead_lnum %d\n", c->gc_lnum, c->ihead_lnum);
619f1bd66afSArtem Bityutskiy 
62084abf972SArtem Bityutskiy 	/* If we are in R/O mode, journal heads do not exist */
62184abf972SArtem Bityutskiy 	if (c->jheads)
6221e51764aSArtem Bityutskiy 		for (i = 0; i < c->jhead_cnt; i++)
6236b38d03fSArtem Bityutskiy 			pr_err("\tjhead %s\t LEB %d\n",
62477a7ae58SArtem Bityutskiy 			       dbg_jhead(c->jheads[i].wbuf.jhead),
62577a7ae58SArtem Bityutskiy 			       c->jheads[i].wbuf.lnum);
6261e51764aSArtem Bityutskiy 	for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
6271e51764aSArtem Bityutskiy 		bud = rb_entry(rb, struct ubifs_bud, rb);
6286b38d03fSArtem Bityutskiy 		pr_err("\tbud LEB %d\n", bud->lnum);
6291e51764aSArtem Bityutskiy 	}
6301e51764aSArtem Bityutskiy 	list_for_each_entry(bud, &c->old_buds, list)
6316b38d03fSArtem Bityutskiy 		pr_err("\told bud LEB %d\n", bud->lnum);
6321e51764aSArtem Bityutskiy 	list_for_each_entry(idx_gc, &c->idx_gc, list)
6336b38d03fSArtem Bityutskiy 		pr_err("\tGC'ed idx LEB %d unmap %d\n",
6341e51764aSArtem Bityutskiy 		       idx_gc->lnum, idx_gc->unmap);
6356b38d03fSArtem Bityutskiy 	pr_err("\tcommit state %d\n", c->cmt_state);
63621a60258SArtem Bityutskiy 
63721a60258SArtem Bityutskiy 	/* Print budgeting predictions */
638b137545cSArtem Bityutskiy 	available = ubifs_calc_available(c, c->bi.min_idx_lebs);
639b137545cSArtem Bityutskiy 	outstanding = c->bi.data_growth + c->bi.dd_growth;
64084abf972SArtem Bityutskiy 	free = ubifs_get_free_space_nolock(c);
6416b38d03fSArtem Bityutskiy 	pr_err("Budgeting predictions:\n");
6426b38d03fSArtem Bityutskiy 	pr_err("\tavailable: %lld, outstanding %lld, free %lld\n",
64321a60258SArtem Bityutskiy 	       available, outstanding, free);
644f1bd66afSArtem Bityutskiy out_unlock:
6451e51764aSArtem Bityutskiy 	spin_unlock(&dbg_lock);
6468ff83089SArtem Bityutskiy 	spin_unlock(&c->space_lock);
6471e51764aSArtem Bityutskiy }
6481e51764aSArtem Bityutskiy 
649edf6be24SArtem Bityutskiy void ubifs_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
6501e51764aSArtem Bityutskiy {
651be9e62a7SArtem Bityutskiy 	int i, spc, dark = 0, dead = 0;
652be9e62a7SArtem Bityutskiy 	struct rb_node *rb;
653be9e62a7SArtem Bityutskiy 	struct ubifs_bud *bud;
654be9e62a7SArtem Bityutskiy 
655be9e62a7SArtem Bityutskiy 	spc = lp->free + lp->dirty;
656be9e62a7SArtem Bityutskiy 	if (spc < c->dead_wm)
657be9e62a7SArtem Bityutskiy 		dead = spc;
658be9e62a7SArtem Bityutskiy 	else
659be9e62a7SArtem Bityutskiy 		dark = ubifs_calc_dark(c, spc);
660be9e62a7SArtem Bityutskiy 
661be9e62a7SArtem Bityutskiy 	if (lp->flags & LPROPS_INDEX)
6626b38d03fSArtem Bityutskiy 		pr_err("LEB %-7d free %-8d dirty %-8d used %-8d free + dirty %-8d flags %#x (",
66379fda517SArtem Bityutskiy 		       lp->lnum, lp->free, lp->dirty, c->leb_size - spc, spc,
66479fda517SArtem Bityutskiy 		       lp->flags);
665be9e62a7SArtem Bityutskiy 	else
6666b38d03fSArtem Bityutskiy 		pr_err("LEB %-7d free %-8d dirty %-8d used %-8d free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d flags %#-4x (",
66779fda517SArtem Bityutskiy 		       lp->lnum, lp->free, lp->dirty, c->leb_size - spc, spc,
66879fda517SArtem Bityutskiy 		       dark, dead, (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags);
669be9e62a7SArtem Bityutskiy 
670be9e62a7SArtem Bityutskiy 	if (lp->flags & LPROPS_TAKEN) {
671be9e62a7SArtem Bityutskiy 		if (lp->flags & LPROPS_INDEX)
6726b38d03fSArtem Bityutskiy 			pr_cont("index, taken");
673be9e62a7SArtem Bityutskiy 		else
6746b38d03fSArtem Bityutskiy 			pr_cont("taken");
675be9e62a7SArtem Bityutskiy 	} else {
676be9e62a7SArtem Bityutskiy 		const char *s;
677be9e62a7SArtem Bityutskiy 
678be9e62a7SArtem Bityutskiy 		if (lp->flags & LPROPS_INDEX) {
679be9e62a7SArtem Bityutskiy 			switch (lp->flags & LPROPS_CAT_MASK) {
680be9e62a7SArtem Bityutskiy 			case LPROPS_DIRTY_IDX:
681be9e62a7SArtem Bityutskiy 				s = "dirty index";
682be9e62a7SArtem Bityutskiy 				break;
683be9e62a7SArtem Bityutskiy 			case LPROPS_FRDI_IDX:
684be9e62a7SArtem Bityutskiy 				s = "freeable index";
685be9e62a7SArtem Bityutskiy 				break;
686be9e62a7SArtem Bityutskiy 			default:
687be9e62a7SArtem Bityutskiy 				s = "index";
688be9e62a7SArtem Bityutskiy 			}
689be9e62a7SArtem Bityutskiy 		} else {
690be9e62a7SArtem Bityutskiy 			switch (lp->flags & LPROPS_CAT_MASK) {
691be9e62a7SArtem Bityutskiy 			case LPROPS_UNCAT:
692be9e62a7SArtem Bityutskiy 				s = "not categorized";
693be9e62a7SArtem Bityutskiy 				break;
694be9e62a7SArtem Bityutskiy 			case LPROPS_DIRTY:
695be9e62a7SArtem Bityutskiy 				s = "dirty";
696be9e62a7SArtem Bityutskiy 				break;
697be9e62a7SArtem Bityutskiy 			case LPROPS_FREE:
698be9e62a7SArtem Bityutskiy 				s = "free";
699be9e62a7SArtem Bityutskiy 				break;
700be9e62a7SArtem Bityutskiy 			case LPROPS_EMPTY:
701be9e62a7SArtem Bityutskiy 				s = "empty";
702be9e62a7SArtem Bityutskiy 				break;
703be9e62a7SArtem Bityutskiy 			case LPROPS_FREEABLE:
704be9e62a7SArtem Bityutskiy 				s = "freeable";
705be9e62a7SArtem Bityutskiy 				break;
706be9e62a7SArtem Bityutskiy 			default:
707be9e62a7SArtem Bityutskiy 				s = NULL;
708be9e62a7SArtem Bityutskiy 				break;
709be9e62a7SArtem Bityutskiy 			}
710be9e62a7SArtem Bityutskiy 		}
7116b38d03fSArtem Bityutskiy 		pr_cont("%s", s);
712be9e62a7SArtem Bityutskiy 	}
713be9e62a7SArtem Bityutskiy 
714be9e62a7SArtem Bityutskiy 	for (rb = rb_first((struct rb_root *)&c->buds); rb; rb = rb_next(rb)) {
715be9e62a7SArtem Bityutskiy 		bud = rb_entry(rb, struct ubifs_bud, rb);
716be9e62a7SArtem Bityutskiy 		if (bud->lnum == lp->lnum) {
717be9e62a7SArtem Bityutskiy 			int head = 0;
718be9e62a7SArtem Bityutskiy 			for (i = 0; i < c->jhead_cnt; i++) {
7191321657dSArtem Bityutskiy 				/*
7201321657dSArtem Bityutskiy 				 * Note, if we are in R/O mode or in the middle
7211321657dSArtem Bityutskiy 				 * of mounting/re-mounting, the write-buffers do
7221321657dSArtem Bityutskiy 				 * not exist.
7231321657dSArtem Bityutskiy 				 */
7241321657dSArtem Bityutskiy 				if (c->jheads &&
7251321657dSArtem Bityutskiy 				    lp->lnum == c->jheads[i].wbuf.lnum) {
7266b38d03fSArtem Bityutskiy 					pr_cont(", jhead %s", dbg_jhead(i));
727be9e62a7SArtem Bityutskiy 					head = 1;
728be9e62a7SArtem Bityutskiy 				}
729be9e62a7SArtem Bityutskiy 			}
730be9e62a7SArtem Bityutskiy 			if (!head)
7316b38d03fSArtem Bityutskiy 				pr_cont(", bud of jhead %s",
732be9e62a7SArtem Bityutskiy 				       dbg_jhead(bud->jhead));
733be9e62a7SArtem Bityutskiy 		}
734be9e62a7SArtem Bityutskiy 	}
735be9e62a7SArtem Bityutskiy 	if (lp->lnum == c->gc_lnum)
7366b38d03fSArtem Bityutskiy 		pr_cont(", GC LEB");
7376b38d03fSArtem Bityutskiy 	pr_cont(")\n");
7381e51764aSArtem Bityutskiy }
7391e51764aSArtem Bityutskiy 
740edf6be24SArtem Bityutskiy void ubifs_dump_lprops(struct ubifs_info *c)
7411e51764aSArtem Bityutskiy {
7421e51764aSArtem Bityutskiy 	int lnum, err;
7431e51764aSArtem Bityutskiy 	struct ubifs_lprops lp;
7441e51764aSArtem Bityutskiy 	struct ubifs_lp_stats lst;
7451e51764aSArtem Bityutskiy 
7466b38d03fSArtem Bityutskiy 	pr_err("(pid %d) start dumping LEB properties\n", current->pid);
7471e51764aSArtem Bityutskiy 	ubifs_get_lp_stats(c, &lst);
748edf6be24SArtem Bityutskiy 	ubifs_dump_lstats(&lst);
7491e51764aSArtem Bityutskiy 
7501e51764aSArtem Bityutskiy 	for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
7511e51764aSArtem Bityutskiy 		err = ubifs_read_one_lp(c, lnum, &lp);
752dac36981Shujianyang 		if (err) {
753235c362bSSheng Yong 			ubifs_err(c, "cannot read lprops for LEB %d", lnum);
754dac36981Shujianyang 			continue;
755dac36981Shujianyang 		}
7561e51764aSArtem Bityutskiy 
757edf6be24SArtem Bityutskiy 		ubifs_dump_lprop(c, &lp);
7581e51764aSArtem Bityutskiy 	}
7596b38d03fSArtem Bityutskiy 	pr_err("(pid %d) finish dumping LEB properties\n", current->pid);
7601e51764aSArtem Bityutskiy }
7611e51764aSArtem Bityutskiy 
762edf6be24SArtem Bityutskiy void ubifs_dump_lpt_info(struct ubifs_info *c)
76373944a6dSAdrian Hunter {
76473944a6dSAdrian Hunter 	int i;
76573944a6dSAdrian Hunter 
76673944a6dSAdrian Hunter 	spin_lock(&dbg_lock);
7676b38d03fSArtem Bityutskiy 	pr_err("(pid %d) dumping LPT information\n", current->pid);
7686b38d03fSArtem Bityutskiy 	pr_err("\tlpt_sz:        %lld\n", c->lpt_sz);
7696b38d03fSArtem Bityutskiy 	pr_err("\tpnode_sz:      %d\n", c->pnode_sz);
7706b38d03fSArtem Bityutskiy 	pr_err("\tnnode_sz:      %d\n", c->nnode_sz);
7716b38d03fSArtem Bityutskiy 	pr_err("\tltab_sz:       %d\n", c->ltab_sz);
7726b38d03fSArtem Bityutskiy 	pr_err("\tlsave_sz:      %d\n", c->lsave_sz);
7736b38d03fSArtem Bityutskiy 	pr_err("\tbig_lpt:       %d\n", c->big_lpt);
7746b38d03fSArtem Bityutskiy 	pr_err("\tlpt_hght:      %d\n", c->lpt_hght);
7756b38d03fSArtem Bityutskiy 	pr_err("\tpnode_cnt:     %d\n", c->pnode_cnt);
7766b38d03fSArtem Bityutskiy 	pr_err("\tnnode_cnt:     %d\n", c->nnode_cnt);
7776b38d03fSArtem Bityutskiy 	pr_err("\tdirty_pn_cnt:  %d\n", c->dirty_pn_cnt);
7786b38d03fSArtem Bityutskiy 	pr_err("\tdirty_nn_cnt:  %d\n", c->dirty_nn_cnt);
7796b38d03fSArtem Bityutskiy 	pr_err("\tlsave_cnt:     %d\n", c->lsave_cnt);
7806b38d03fSArtem Bityutskiy 	pr_err("\tspace_bits:    %d\n", c->space_bits);
7816b38d03fSArtem Bityutskiy 	pr_err("\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
7826b38d03fSArtem Bityutskiy 	pr_err("\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
7836b38d03fSArtem Bityutskiy 	pr_err("\tlpt_spc_bits:  %d\n", c->lpt_spc_bits);
7846b38d03fSArtem Bityutskiy 	pr_err("\tpcnt_bits:     %d\n", c->pcnt_bits);
7856b38d03fSArtem Bityutskiy 	pr_err("\tlnum_bits:     %d\n", c->lnum_bits);
7866b38d03fSArtem Bityutskiy 	pr_err("\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
7876b38d03fSArtem Bityutskiy 	pr_err("\tLPT head is at %d:%d\n",
78873944a6dSAdrian Hunter 	       c->nhead_lnum, c->nhead_offs);
7896b38d03fSArtem Bityutskiy 	pr_err("\tLPT ltab is at %d:%d\n", c->ltab_lnum, c->ltab_offs);
79073944a6dSAdrian Hunter 	if (c->big_lpt)
7916b38d03fSArtem Bityutskiy 		pr_err("\tLPT lsave is at %d:%d\n",
79273944a6dSAdrian Hunter 		       c->lsave_lnum, c->lsave_offs);
79373944a6dSAdrian Hunter 	for (i = 0; i < c->lpt_lebs; i++)
7946b38d03fSArtem Bityutskiy 		pr_err("\tLPT LEB %d free %d dirty %d tgc %d cmt %d\n",
79579fda517SArtem Bityutskiy 		       i + c->lpt_first, c->ltab[i].free, c->ltab[i].dirty,
79679fda517SArtem Bityutskiy 		       c->ltab[i].tgc, c->ltab[i].cmt);
79773944a6dSAdrian Hunter 	spin_unlock(&dbg_lock);
79873944a6dSAdrian Hunter }
79973944a6dSAdrian Hunter 
800edf6be24SArtem Bityutskiy void ubifs_dump_sleb(const struct ubifs_info *c,
801d37854cfSArtem Bityutskiy 		     const struct ubifs_scan_leb *sleb, int offs)
802d37854cfSArtem Bityutskiy {
803d37854cfSArtem Bityutskiy 	struct ubifs_scan_node *snod;
804d37854cfSArtem Bityutskiy 
8056b38d03fSArtem Bityutskiy 	pr_err("(pid %d) start dumping scanned data from LEB %d:%d\n",
806d37854cfSArtem Bityutskiy 	       current->pid, sleb->lnum, offs);
807d37854cfSArtem Bityutskiy 
808d37854cfSArtem Bityutskiy 	list_for_each_entry(snod, &sleb->nodes, list) {
809d37854cfSArtem Bityutskiy 		cond_resched();
8106b38d03fSArtem Bityutskiy 		pr_err("Dumping node at LEB %d:%d len %d\n",
81179fda517SArtem Bityutskiy 		       sleb->lnum, snod->offs, snod->len);
812edf6be24SArtem Bityutskiy 		ubifs_dump_node(c, snod->node);
813d37854cfSArtem Bityutskiy 	}
814d37854cfSArtem Bityutskiy }
815d37854cfSArtem Bityutskiy 
816edf6be24SArtem Bityutskiy void ubifs_dump_leb(const struct ubifs_info *c, int lnum)
8171e51764aSArtem Bityutskiy {
8181e51764aSArtem Bityutskiy 	struct ubifs_scan_leb *sleb;
8191e51764aSArtem Bityutskiy 	struct ubifs_scan_node *snod;
82073d9aec3SArtem Bityutskiy 	void *buf;
8211e51764aSArtem Bityutskiy 
8226b38d03fSArtem Bityutskiy 	pr_err("(pid %d) start dumping LEB %d\n", current->pid, lnum);
82373d9aec3SArtem Bityutskiy 
824fc5e58c0SArtem Bityutskiy 	buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
82573d9aec3SArtem Bityutskiy 	if (!buf) {
826235c362bSSheng Yong 		ubifs_err(c, "cannot allocate memory for dumping LEB %d", lnum);
82773d9aec3SArtem Bityutskiy 		return;
82873d9aec3SArtem Bityutskiy 	}
82973d9aec3SArtem Bityutskiy 
83073d9aec3SArtem Bityutskiy 	sleb = ubifs_scan(c, lnum, 0, buf, 0);
8311e51764aSArtem Bityutskiy 	if (IS_ERR(sleb)) {
832235c362bSSheng Yong 		ubifs_err(c, "scan error %d", (int)PTR_ERR(sleb));
83373d9aec3SArtem Bityutskiy 		goto out;
8341e51764aSArtem Bityutskiy 	}
8351e51764aSArtem Bityutskiy 
8366b38d03fSArtem Bityutskiy 	pr_err("LEB %d has %d nodes ending at %d\n", lnum,
8371e51764aSArtem Bityutskiy 	       sleb->nodes_cnt, sleb->endpt);
8381e51764aSArtem Bityutskiy 
8391e51764aSArtem Bityutskiy 	list_for_each_entry(snod, &sleb->nodes, list) {
8401e51764aSArtem Bityutskiy 		cond_resched();
8416b38d03fSArtem Bityutskiy 		pr_err("Dumping node at LEB %d:%d len %d\n", lnum,
8421e51764aSArtem Bityutskiy 		       snod->offs, snod->len);
843edf6be24SArtem Bityutskiy 		ubifs_dump_node(c, snod->node);
8441e51764aSArtem Bityutskiy 	}
8451e51764aSArtem Bityutskiy 
8466b38d03fSArtem Bityutskiy 	pr_err("(pid %d) finish dumping LEB %d\n", current->pid, lnum);
8471e51764aSArtem Bityutskiy 	ubifs_scan_destroy(sleb);
84873d9aec3SArtem Bityutskiy 
84973d9aec3SArtem Bityutskiy out:
85073d9aec3SArtem Bityutskiy 	vfree(buf);
8511e51764aSArtem Bityutskiy 	return;
8521e51764aSArtem Bityutskiy }
8531e51764aSArtem Bityutskiy 
854edf6be24SArtem Bityutskiy void ubifs_dump_znode(const struct ubifs_info *c,
8551e51764aSArtem Bityutskiy 		      const struct ubifs_znode *znode)
8561e51764aSArtem Bityutskiy {
8571e51764aSArtem Bityutskiy 	int n;
8581e51764aSArtem Bityutskiy 	const struct ubifs_zbranch *zbr;
859515315a1SArtem Bityutskiy 	char key_buf[DBG_KEY_BUF_LEN];
8601e51764aSArtem Bityutskiy 
8611e51764aSArtem Bityutskiy 	spin_lock(&dbg_lock);
8621e51764aSArtem Bityutskiy 	if (znode->parent)
8631e51764aSArtem Bityutskiy 		zbr = &znode->parent->zbranch[znode->iip];
8641e51764aSArtem Bityutskiy 	else
8651e51764aSArtem Bityutskiy 		zbr = &c->zroot;
8661e51764aSArtem Bityutskiy 
8676b38d03fSArtem Bityutskiy 	pr_err("znode %p, LEB %d:%d len %d parent %p iip %d level %d child_cnt %d flags %lx\n",
86879fda517SArtem Bityutskiy 	       znode, zbr->lnum, zbr->offs, zbr->len, znode->parent, znode->iip,
86979fda517SArtem Bityutskiy 	       znode->level, znode->child_cnt, znode->flags);
8701e51764aSArtem Bityutskiy 
8711e51764aSArtem Bityutskiy 	if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
8721e51764aSArtem Bityutskiy 		spin_unlock(&dbg_lock);
8731e51764aSArtem Bityutskiy 		return;
8741e51764aSArtem Bityutskiy 	}
8751e51764aSArtem Bityutskiy 
8766b38d03fSArtem Bityutskiy 	pr_err("zbranches:\n");
8771e51764aSArtem Bityutskiy 	for (n = 0; n < znode->child_cnt; n++) {
8781e51764aSArtem Bityutskiy 		zbr = &znode->zbranch[n];
8791e51764aSArtem Bityutskiy 		if (znode->level > 0)
8806b38d03fSArtem Bityutskiy 			pr_err("\t%d: znode %p LEB %d:%d len %d key %s\n",
88179fda517SArtem Bityutskiy 			       n, zbr->znode, zbr->lnum, zbr->offs, zbr->len,
88279fda517SArtem Bityutskiy 			       dbg_snprintf_key(c, &zbr->key, key_buf,
883515315a1SArtem Bityutskiy 						DBG_KEY_BUF_LEN));
8841e51764aSArtem Bityutskiy 		else
8856b38d03fSArtem Bityutskiy 			pr_err("\t%d: LNC %p LEB %d:%d len %d key %s\n",
88679fda517SArtem Bityutskiy 			       n, zbr->znode, zbr->lnum, zbr->offs, zbr->len,
88779fda517SArtem Bityutskiy 			       dbg_snprintf_key(c, &zbr->key, key_buf,
888515315a1SArtem Bityutskiy 						DBG_KEY_BUF_LEN));
8891e51764aSArtem Bityutskiy 	}
8901e51764aSArtem Bityutskiy 	spin_unlock(&dbg_lock);
8911e51764aSArtem Bityutskiy }
8921e51764aSArtem Bityutskiy 
893edf6be24SArtem Bityutskiy void ubifs_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
8941e51764aSArtem Bityutskiy {
8951e51764aSArtem Bityutskiy 	int i;
8961e51764aSArtem Bityutskiy 
8976b38d03fSArtem Bityutskiy 	pr_err("(pid %d) start dumping heap cat %d (%d elements)\n",
8981de94159SArtem Bityutskiy 	       current->pid, cat, heap->cnt);
8991e51764aSArtem Bityutskiy 	for (i = 0; i < heap->cnt; i++) {
9001e51764aSArtem Bityutskiy 		struct ubifs_lprops *lprops = heap->arr[i];
9011e51764aSArtem Bityutskiy 
9026b38d03fSArtem Bityutskiy 		pr_err("\t%d. LEB %d hpos %d free %d dirty %d flags %d\n",
90379fda517SArtem Bityutskiy 		       i, lprops->lnum, lprops->hpos, lprops->free,
90479fda517SArtem Bityutskiy 		       lprops->dirty, lprops->flags);
9051e51764aSArtem Bityutskiy 	}
9066b38d03fSArtem Bityutskiy 	pr_err("(pid %d) finish dumping heap\n", current->pid);
9071e51764aSArtem Bityutskiy }
9081e51764aSArtem Bityutskiy 
909edf6be24SArtem Bityutskiy void ubifs_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
9101e51764aSArtem Bityutskiy 		      struct ubifs_nnode *parent, int iip)
9111e51764aSArtem Bityutskiy {
9121e51764aSArtem Bityutskiy 	int i;
9131e51764aSArtem Bityutskiy 
9146b38d03fSArtem Bityutskiy 	pr_err("(pid %d) dumping pnode:\n", current->pid);
9156b38d03fSArtem Bityutskiy 	pr_err("\taddress %zx parent %zx cnext %zx\n",
9161e51764aSArtem Bityutskiy 	       (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
9176b38d03fSArtem Bityutskiy 	pr_err("\tflags %lu iip %d level %d num %d\n",
9181e51764aSArtem Bityutskiy 	       pnode->flags, iip, pnode->level, pnode->num);
9191e51764aSArtem Bityutskiy 	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
9201e51764aSArtem Bityutskiy 		struct ubifs_lprops *lp = &pnode->lprops[i];
9211e51764aSArtem Bityutskiy 
9226b38d03fSArtem Bityutskiy 		pr_err("\t%d: free %d dirty %d flags %d lnum %d\n",
9231e51764aSArtem Bityutskiy 		       i, lp->free, lp->dirty, lp->flags, lp->lnum);
9241e51764aSArtem Bityutskiy 	}
9251e51764aSArtem Bityutskiy }
9261e51764aSArtem Bityutskiy 
927edf6be24SArtem Bityutskiy void ubifs_dump_tnc(struct ubifs_info *c)
9281e51764aSArtem Bityutskiy {
9291e51764aSArtem Bityutskiy 	struct ubifs_znode *znode;
9301e51764aSArtem Bityutskiy 	int level;
9311e51764aSArtem Bityutskiy 
9326b38d03fSArtem Bityutskiy 	pr_err("\n");
9336b38d03fSArtem Bityutskiy 	pr_err("(pid %d) start dumping TNC tree\n", current->pid);
934*6eb61d58SRichard Weinberger 	znode = ubifs_tnc_levelorder_next(c, c->zroot.znode, NULL);
9351e51764aSArtem Bityutskiy 	level = znode->level;
9366b38d03fSArtem Bityutskiy 	pr_err("== Level %d ==\n", level);
9371e51764aSArtem Bityutskiy 	while (znode) {
9381e51764aSArtem Bityutskiy 		if (level != znode->level) {
9391e51764aSArtem Bityutskiy 			level = znode->level;
9406b38d03fSArtem Bityutskiy 			pr_err("== Level %d ==\n", level);
9411e51764aSArtem Bityutskiy 		}
942edf6be24SArtem Bityutskiy 		ubifs_dump_znode(c, znode);
943*6eb61d58SRichard Weinberger 		znode = ubifs_tnc_levelorder_next(c, c->zroot.znode, znode);
9441e51764aSArtem Bityutskiy 	}
9456b38d03fSArtem Bityutskiy 	pr_err("(pid %d) finish dumping TNC tree\n", current->pid);
9461e51764aSArtem Bityutskiy }
9471e51764aSArtem Bityutskiy 
9481e51764aSArtem Bityutskiy static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
9491e51764aSArtem Bityutskiy 		      void *priv)
9501e51764aSArtem Bityutskiy {
951edf6be24SArtem Bityutskiy 	ubifs_dump_znode(c, znode);
9521e51764aSArtem Bityutskiy 	return 0;
9531e51764aSArtem Bityutskiy }
9541e51764aSArtem Bityutskiy 
9551e51764aSArtem Bityutskiy /**
956edf6be24SArtem Bityutskiy  * ubifs_dump_index - dump the on-flash index.
9571e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
9581e51764aSArtem Bityutskiy  *
959edf6be24SArtem Bityutskiy  * This function dumps whole UBIFS indexing B-tree, unlike 'ubifs_dump_tnc()'
9601e51764aSArtem Bityutskiy  * which dumps only in-memory znodes and does not read znodes which from flash.
9611e51764aSArtem Bityutskiy  */
962edf6be24SArtem Bityutskiy void ubifs_dump_index(struct ubifs_info *c)
9631e51764aSArtem Bityutskiy {
9641e51764aSArtem Bityutskiy 	dbg_walk_index(c, NULL, dump_znode, NULL);
9651e51764aSArtem Bityutskiy }
9661e51764aSArtem Bityutskiy 
9671e51764aSArtem Bityutskiy /**
96884abf972SArtem Bityutskiy  * dbg_save_space_info - save information about flash space.
96984abf972SArtem Bityutskiy  * @c: UBIFS file-system description object
97084abf972SArtem Bityutskiy  *
97184abf972SArtem Bityutskiy  * This function saves information about UBIFS free space, dirty space, etc, in
97284abf972SArtem Bityutskiy  * order to check it later.
97384abf972SArtem Bityutskiy  */
97484abf972SArtem Bityutskiy void dbg_save_space_info(struct ubifs_info *c)
97584abf972SArtem Bityutskiy {
97684abf972SArtem Bityutskiy 	struct ubifs_debug_info *d = c->dbg;
9777da6443aSArtem Bityutskiy 	int freeable_cnt;
97884abf972SArtem Bityutskiy 
97984abf972SArtem Bityutskiy 	spin_lock(&c->space_lock);
9807da6443aSArtem Bityutskiy 	memcpy(&d->saved_lst, &c->lst, sizeof(struct ubifs_lp_stats));
981f1bd66afSArtem Bityutskiy 	memcpy(&d->saved_bi, &c->bi, sizeof(struct ubifs_budg_info));
982f1bd66afSArtem Bityutskiy 	d->saved_idx_gc_cnt = c->idx_gc_cnt;
9837da6443aSArtem Bityutskiy 
9847da6443aSArtem Bityutskiy 	/*
9857da6443aSArtem Bityutskiy 	 * We use a dirty hack here and zero out @c->freeable_cnt, because it
9867da6443aSArtem Bityutskiy 	 * affects the free space calculations, and UBIFS might not know about
9877da6443aSArtem Bityutskiy 	 * all freeable eraseblocks. Indeed, we know about freeable eraseblocks
9887da6443aSArtem Bityutskiy 	 * only when we read their lprops, and we do this only lazily, upon the
9897da6443aSArtem Bityutskiy 	 * need. So at any given point of time @c->freeable_cnt might be not
9907da6443aSArtem Bityutskiy 	 * exactly accurate.
9917da6443aSArtem Bityutskiy 	 *
9927da6443aSArtem Bityutskiy 	 * Just one example about the issue we hit when we did not zero
9937da6443aSArtem Bityutskiy 	 * @c->freeable_cnt.
9947da6443aSArtem Bityutskiy 	 * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the
9957da6443aSArtem Bityutskiy 	 *    amount of free space in @d->saved_free
9967da6443aSArtem Bityutskiy 	 * 2. We re-mount R/W, which makes UBIFS to read the "lsave"
9977da6443aSArtem Bityutskiy 	 *    information from flash, where we cache LEBs from various
9987da6443aSArtem Bityutskiy 	 *    categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()'
9997da6443aSArtem Bityutskiy 	 *    -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()'
10007da6443aSArtem Bityutskiy 	 *    -> 'ubifs_get_pnode()' -> 'update_cats()'
10017da6443aSArtem Bityutskiy 	 *    -> 'ubifs_add_to_cat()').
10027da6443aSArtem Bityutskiy 	 * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt
10037da6443aSArtem Bityutskiy 	 *    becomes %1.
10047da6443aSArtem Bityutskiy 	 * 4. We calculate the amount of free space when the re-mount is
10057da6443aSArtem Bityutskiy 	 *    finished in 'dbg_check_space_info()' and it does not match
10067da6443aSArtem Bityutskiy 	 *    @d->saved_free.
10077da6443aSArtem Bityutskiy 	 */
10087da6443aSArtem Bityutskiy 	freeable_cnt = c->freeable_cnt;
10097da6443aSArtem Bityutskiy 	c->freeable_cnt = 0;
101084abf972SArtem Bityutskiy 	d->saved_free = ubifs_get_free_space_nolock(c);
10117da6443aSArtem Bityutskiy 	c->freeable_cnt = freeable_cnt;
101284abf972SArtem Bityutskiy 	spin_unlock(&c->space_lock);
101384abf972SArtem Bityutskiy }
101484abf972SArtem Bityutskiy 
101584abf972SArtem Bityutskiy /**
101684abf972SArtem Bityutskiy  * dbg_check_space_info - check flash space information.
101784abf972SArtem Bityutskiy  * @c: UBIFS file-system description object
101884abf972SArtem Bityutskiy  *
101984abf972SArtem Bityutskiy  * This function compares current flash space information with the information
102084abf972SArtem Bityutskiy  * which was saved when the 'dbg_save_space_info()' function was called.
102184abf972SArtem Bityutskiy  * Returns zero if the information has not changed, and %-EINVAL it it has
102284abf972SArtem Bityutskiy  * changed.
102384abf972SArtem Bityutskiy  */
102484abf972SArtem Bityutskiy int dbg_check_space_info(struct ubifs_info *c)
102584abf972SArtem Bityutskiy {
102684abf972SArtem Bityutskiy 	struct ubifs_debug_info *d = c->dbg;
102784abf972SArtem Bityutskiy 	struct ubifs_lp_stats lst;
10287da6443aSArtem Bityutskiy 	long long free;
10297da6443aSArtem Bityutskiy 	int freeable_cnt;
103084abf972SArtem Bityutskiy 
103184abf972SArtem Bityutskiy 	spin_lock(&c->space_lock);
10327da6443aSArtem Bityutskiy 	freeable_cnt = c->freeable_cnt;
10337da6443aSArtem Bityutskiy 	c->freeable_cnt = 0;
10347da6443aSArtem Bityutskiy 	free = ubifs_get_free_space_nolock(c);
10357da6443aSArtem Bityutskiy 	c->freeable_cnt = freeable_cnt;
103684abf972SArtem Bityutskiy 	spin_unlock(&c->space_lock);
103784abf972SArtem Bityutskiy 
103884abf972SArtem Bityutskiy 	if (free != d->saved_free) {
1039235c362bSSheng Yong 		ubifs_err(c, "free space changed from %lld to %lld",
104084abf972SArtem Bityutskiy 			  d->saved_free, free);
104184abf972SArtem Bityutskiy 		goto out;
104284abf972SArtem Bityutskiy 	}
104384abf972SArtem Bityutskiy 
104484abf972SArtem Bityutskiy 	return 0;
104584abf972SArtem Bityutskiy 
104684abf972SArtem Bityutskiy out:
1047235c362bSSheng Yong 	ubifs_msg(c, "saved lprops statistics dump");
1048edf6be24SArtem Bityutskiy 	ubifs_dump_lstats(&d->saved_lst);
1049235c362bSSheng Yong 	ubifs_msg(c, "saved budgeting info dump");
1050edf6be24SArtem Bityutskiy 	ubifs_dump_budg(c, &d->saved_bi);
1051235c362bSSheng Yong 	ubifs_msg(c, "saved idx_gc_cnt %d", d->saved_idx_gc_cnt);
1052235c362bSSheng Yong 	ubifs_msg(c, "current lprops statistics dump");
1053f1bd66afSArtem Bityutskiy 	ubifs_get_lp_stats(c, &lst);
1054edf6be24SArtem Bityutskiy 	ubifs_dump_lstats(&lst);
1055235c362bSSheng Yong 	ubifs_msg(c, "current budgeting info dump");
1056edf6be24SArtem Bityutskiy 	ubifs_dump_budg(c, &c->bi);
105784abf972SArtem Bityutskiy 	dump_stack();
105884abf972SArtem Bityutskiy 	return -EINVAL;
105984abf972SArtem Bityutskiy }
106084abf972SArtem Bityutskiy 
106184abf972SArtem Bityutskiy /**
10621e51764aSArtem Bityutskiy  * dbg_check_synced_i_size - check synchronized inode size.
1063d808efb4SArtem Bityutskiy  * @c: UBIFS file-system description object
10641e51764aSArtem Bityutskiy  * @inode: inode to check
10651e51764aSArtem Bityutskiy  *
10661e51764aSArtem Bityutskiy  * If inode is clean, synchronized inode size has to be equivalent to current
10671e51764aSArtem Bityutskiy  * inode size. This function has to be called only for locked inodes (@i_mutex
10681e51764aSArtem Bityutskiy  * has to be locked). Returns %0 if synchronized inode size if correct, and
10691e51764aSArtem Bityutskiy  * %-EINVAL if not.
10701e51764aSArtem Bityutskiy  */
1071d808efb4SArtem Bityutskiy int dbg_check_synced_i_size(const struct ubifs_info *c, struct inode *inode)
10721e51764aSArtem Bityutskiy {
10731e51764aSArtem Bityutskiy 	int err = 0;
10741e51764aSArtem Bityutskiy 	struct ubifs_inode *ui = ubifs_inode(inode);
10751e51764aSArtem Bityutskiy 
10762b1844a8SArtem Bityutskiy 	if (!dbg_is_chk_gen(c))
10771e51764aSArtem Bityutskiy 		return 0;
10781e51764aSArtem Bityutskiy 	if (!S_ISREG(inode->i_mode))
10791e51764aSArtem Bityutskiy 		return 0;
10801e51764aSArtem Bityutskiy 
10811e51764aSArtem Bityutskiy 	mutex_lock(&ui->ui_mutex);
10821e51764aSArtem Bityutskiy 	spin_lock(&ui->ui_lock);
10831e51764aSArtem Bityutskiy 	if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
1084235c362bSSheng Yong 		ubifs_err(c, "ui_size is %lld, synced_i_size is %lld, but inode is clean",
108579fda517SArtem Bityutskiy 			  ui->ui_size, ui->synced_i_size);
1086235c362bSSheng Yong 		ubifs_err(c, "i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
10871e51764aSArtem Bityutskiy 			  inode->i_mode, i_size_read(inode));
10887c46d0aeSArtem Bityutskiy 		dump_stack();
10891e51764aSArtem Bityutskiy 		err = -EINVAL;
10901e51764aSArtem Bityutskiy 	}
10911e51764aSArtem Bityutskiy 	spin_unlock(&ui->ui_lock);
10921e51764aSArtem Bityutskiy 	mutex_unlock(&ui->ui_mutex);
10931e51764aSArtem Bityutskiy 	return err;
10941e51764aSArtem Bityutskiy }
10951e51764aSArtem Bityutskiy 
10961e51764aSArtem Bityutskiy /*
10971e51764aSArtem Bityutskiy  * dbg_check_dir - check directory inode size and link count.
10981e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
10991e51764aSArtem Bityutskiy  * @dir: the directory to calculate size for
11001e51764aSArtem Bityutskiy  * @size: the result is returned here
11011e51764aSArtem Bityutskiy  *
11021e51764aSArtem Bityutskiy  * This function makes sure that directory size and link count are correct.
11031e51764aSArtem Bityutskiy  * Returns zero in case of success and a negative error code in case of
11041e51764aSArtem Bityutskiy  * failure.
11051e51764aSArtem Bityutskiy  *
11061e51764aSArtem Bityutskiy  * Note, it is good idea to make sure the @dir->i_mutex is locked before
11071e51764aSArtem Bityutskiy  * calling this function.
11081e51764aSArtem Bityutskiy  */
11091b51e983SArtem Bityutskiy int dbg_check_dir(struct ubifs_info *c, const struct inode *dir)
11101e51764aSArtem Bityutskiy {
11111e51764aSArtem Bityutskiy 	unsigned int nlink = 2;
11121e51764aSArtem Bityutskiy 	union ubifs_key key;
11131e51764aSArtem Bityutskiy 	struct ubifs_dent_node *dent, *pdent = NULL;
1114f4f61d2cSRichard Weinberger 	struct fscrypt_name nm = {0};
11151e51764aSArtem Bityutskiy 	loff_t size = UBIFS_INO_NODE_SZ;
11161e51764aSArtem Bityutskiy 
11172b1844a8SArtem Bityutskiy 	if (!dbg_is_chk_gen(c))
11181e51764aSArtem Bityutskiy 		return 0;
11191e51764aSArtem Bityutskiy 
11201e51764aSArtem Bityutskiy 	if (!S_ISDIR(dir->i_mode))
11211e51764aSArtem Bityutskiy 		return 0;
11221e51764aSArtem Bityutskiy 
11231e51764aSArtem Bityutskiy 	lowest_dent_key(c, &key, dir->i_ino);
11241e51764aSArtem Bityutskiy 	while (1) {
11251e51764aSArtem Bityutskiy 		int err;
11261e51764aSArtem Bityutskiy 
11271e51764aSArtem Bityutskiy 		dent = ubifs_tnc_next_ent(c, &key, &nm);
11281e51764aSArtem Bityutskiy 		if (IS_ERR(dent)) {
11291e51764aSArtem Bityutskiy 			err = PTR_ERR(dent);
11301e51764aSArtem Bityutskiy 			if (err == -ENOENT)
11311e51764aSArtem Bityutskiy 				break;
11321e51764aSArtem Bityutskiy 			return err;
11331e51764aSArtem Bityutskiy 		}
11341e51764aSArtem Bityutskiy 
1135f4f61d2cSRichard Weinberger 		fname_name(&nm) = dent->name;
1136f4f61d2cSRichard Weinberger 		fname_len(&nm) = le16_to_cpu(dent->nlen);
1137f4f61d2cSRichard Weinberger 		size += CALC_DENT_SIZE(fname_len(&nm));
11381e51764aSArtem Bityutskiy 		if (dent->type == UBIFS_ITYPE_DIR)
11391e51764aSArtem Bityutskiy 			nlink += 1;
11401e51764aSArtem Bityutskiy 		kfree(pdent);
11411e51764aSArtem Bityutskiy 		pdent = dent;
11421e51764aSArtem Bityutskiy 		key_read(c, &dent->key, &key);
11431e51764aSArtem Bityutskiy 	}
11441e51764aSArtem Bityutskiy 	kfree(pdent);
11451e51764aSArtem Bityutskiy 
11461e51764aSArtem Bityutskiy 	if (i_size_read(dir) != size) {
1147235c362bSSheng Yong 		ubifs_err(c, "directory inode %lu has size %llu, but calculated size is %llu",
114879fda517SArtem Bityutskiy 			  dir->i_ino, (unsigned long long)i_size_read(dir),
11491e51764aSArtem Bityutskiy 			  (unsigned long long)size);
1150edf6be24SArtem Bityutskiy 		ubifs_dump_inode(c, dir);
11511e51764aSArtem Bityutskiy 		dump_stack();
11521e51764aSArtem Bityutskiy 		return -EINVAL;
11531e51764aSArtem Bityutskiy 	}
11541e51764aSArtem Bityutskiy 	if (dir->i_nlink != nlink) {
1155235c362bSSheng Yong 		ubifs_err(c, "directory inode %lu has nlink %u, but calculated nlink is %u",
115679fda517SArtem Bityutskiy 			  dir->i_ino, dir->i_nlink, nlink);
1157edf6be24SArtem Bityutskiy 		ubifs_dump_inode(c, dir);
11581e51764aSArtem Bityutskiy 		dump_stack();
11591e51764aSArtem Bityutskiy 		return -EINVAL;
11601e51764aSArtem Bityutskiy 	}
11611e51764aSArtem Bityutskiy 
11621e51764aSArtem Bityutskiy 	return 0;
11631e51764aSArtem Bityutskiy }
11641e51764aSArtem Bityutskiy 
11651e51764aSArtem Bityutskiy /**
11661e51764aSArtem Bityutskiy  * dbg_check_key_order - make sure that colliding keys are properly ordered.
11671e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
11681e51764aSArtem Bityutskiy  * @zbr1: first zbranch
11691e51764aSArtem Bityutskiy  * @zbr2: following zbranch
11701e51764aSArtem Bityutskiy  *
11711e51764aSArtem Bityutskiy  * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
11721e51764aSArtem Bityutskiy  * names of the direntries/xentries which are referred by the keys. This
11731e51764aSArtem Bityutskiy  * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
11741e51764aSArtem Bityutskiy  * sure the name of direntry/xentry referred by @zbr1 is less than
11751e51764aSArtem Bityutskiy  * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
11761e51764aSArtem Bityutskiy  * and a negative error code in case of failure.
11771e51764aSArtem Bityutskiy  */
11781e51764aSArtem Bityutskiy static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
11791e51764aSArtem Bityutskiy 			       struct ubifs_zbranch *zbr2)
11801e51764aSArtem Bityutskiy {
11811e51764aSArtem Bityutskiy 	int err, nlen1, nlen2, cmp;
11821e51764aSArtem Bityutskiy 	struct ubifs_dent_node *dent1, *dent2;
11831e51764aSArtem Bityutskiy 	union ubifs_key key;
1184515315a1SArtem Bityutskiy 	char key_buf[DBG_KEY_BUF_LEN];
11851e51764aSArtem Bityutskiy 
1186*6eb61d58SRichard Weinberger 	ubifs_assert(c, !keys_cmp(c, &zbr1->key, &zbr2->key));
11871e51764aSArtem Bityutskiy 	dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
11881e51764aSArtem Bityutskiy 	if (!dent1)
11891e51764aSArtem Bityutskiy 		return -ENOMEM;
11901e51764aSArtem Bityutskiy 	dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
11911e51764aSArtem Bityutskiy 	if (!dent2) {
11921e51764aSArtem Bityutskiy 		err = -ENOMEM;
11931e51764aSArtem Bityutskiy 		goto out_free;
11941e51764aSArtem Bityutskiy 	}
11951e51764aSArtem Bityutskiy 
11961e51764aSArtem Bityutskiy 	err = ubifs_tnc_read_node(c, zbr1, dent1);
11971e51764aSArtem Bityutskiy 	if (err)
11981e51764aSArtem Bityutskiy 		goto out_free;
11991e51764aSArtem Bityutskiy 	err = ubifs_validate_entry(c, dent1);
12001e51764aSArtem Bityutskiy 	if (err)
12011e51764aSArtem Bityutskiy 		goto out_free;
12021e51764aSArtem Bityutskiy 
12031e51764aSArtem Bityutskiy 	err = ubifs_tnc_read_node(c, zbr2, dent2);
12041e51764aSArtem Bityutskiy 	if (err)
12051e51764aSArtem Bityutskiy 		goto out_free;
12061e51764aSArtem Bityutskiy 	err = ubifs_validate_entry(c, dent2);
12071e51764aSArtem Bityutskiy 	if (err)
12081e51764aSArtem Bityutskiy 		goto out_free;
12091e51764aSArtem Bityutskiy 
12101e51764aSArtem Bityutskiy 	/* Make sure node keys are the same as in zbranch */
12111e51764aSArtem Bityutskiy 	err = 1;
12121e51764aSArtem Bityutskiy 	key_read(c, &dent1->key, &key);
12131e51764aSArtem Bityutskiy 	if (keys_cmp(c, &zbr1->key, &key)) {
1214235c362bSSheng Yong 		ubifs_err(c, "1st entry at %d:%d has key %s", zbr1->lnum,
1215515315a1SArtem Bityutskiy 			  zbr1->offs, dbg_snprintf_key(c, &key, key_buf,
1216515315a1SArtem Bityutskiy 						       DBG_KEY_BUF_LEN));
1217235c362bSSheng Yong 		ubifs_err(c, "but it should have key %s according to tnc",
1218515315a1SArtem Bityutskiy 			  dbg_snprintf_key(c, &zbr1->key, key_buf,
1219515315a1SArtem Bityutskiy 					   DBG_KEY_BUF_LEN));
1220edf6be24SArtem Bityutskiy 		ubifs_dump_node(c, dent1);
12211e51764aSArtem Bityutskiy 		goto out_free;
12221e51764aSArtem Bityutskiy 	}
12231e51764aSArtem Bityutskiy 
12241e51764aSArtem Bityutskiy 	key_read(c, &dent2->key, &key);
12251e51764aSArtem Bityutskiy 	if (keys_cmp(c, &zbr2->key, &key)) {
1226235c362bSSheng Yong 		ubifs_err(c, "2nd entry at %d:%d has key %s", zbr1->lnum,
1227515315a1SArtem Bityutskiy 			  zbr1->offs, dbg_snprintf_key(c, &key, key_buf,
1228515315a1SArtem Bityutskiy 						       DBG_KEY_BUF_LEN));
1229235c362bSSheng Yong 		ubifs_err(c, "but it should have key %s according to tnc",
1230515315a1SArtem Bityutskiy 			  dbg_snprintf_key(c, &zbr2->key, key_buf,
1231515315a1SArtem Bityutskiy 					   DBG_KEY_BUF_LEN));
1232edf6be24SArtem Bityutskiy 		ubifs_dump_node(c, dent2);
12331e51764aSArtem Bityutskiy 		goto out_free;
12341e51764aSArtem Bityutskiy 	}
12351e51764aSArtem Bityutskiy 
12361e51764aSArtem Bityutskiy 	nlen1 = le16_to_cpu(dent1->nlen);
12371e51764aSArtem Bityutskiy 	nlen2 = le16_to_cpu(dent2->nlen);
12381e51764aSArtem Bityutskiy 
12391e51764aSArtem Bityutskiy 	cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
12401e51764aSArtem Bityutskiy 	if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
12411e51764aSArtem Bityutskiy 		err = 0;
12421e51764aSArtem Bityutskiy 		goto out_free;
12431e51764aSArtem Bityutskiy 	}
12441e51764aSArtem Bityutskiy 	if (cmp == 0 && nlen1 == nlen2)
1245235c362bSSheng Yong 		ubifs_err(c, "2 xent/dent nodes with the same name");
12461e51764aSArtem Bityutskiy 	else
1247235c362bSSheng Yong 		ubifs_err(c, "bad order of colliding key %s",
1248515315a1SArtem Bityutskiy 			  dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
12491e51764aSArtem Bityutskiy 
1250235c362bSSheng Yong 	ubifs_msg(c, "first node at %d:%d\n", zbr1->lnum, zbr1->offs);
1251edf6be24SArtem Bityutskiy 	ubifs_dump_node(c, dent1);
1252235c362bSSheng Yong 	ubifs_msg(c, "second node at %d:%d\n", zbr2->lnum, zbr2->offs);
1253edf6be24SArtem Bityutskiy 	ubifs_dump_node(c, dent2);
12541e51764aSArtem Bityutskiy 
12551e51764aSArtem Bityutskiy out_free:
12561e51764aSArtem Bityutskiy 	kfree(dent2);
12571e51764aSArtem Bityutskiy 	kfree(dent1);
12581e51764aSArtem Bityutskiy 	return err;
12591e51764aSArtem Bityutskiy }
12601e51764aSArtem Bityutskiy 
12611e51764aSArtem Bityutskiy /**
12621e51764aSArtem Bityutskiy  * dbg_check_znode - check if znode is all right.
12631e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
12641e51764aSArtem Bityutskiy  * @zbr: zbranch which points to this znode
12651e51764aSArtem Bityutskiy  *
12661e51764aSArtem Bityutskiy  * This function makes sure that znode referred to by @zbr is all right.
12671e51764aSArtem Bityutskiy  * Returns zero if it is, and %-EINVAL if it is not.
12681e51764aSArtem Bityutskiy  */
12691e51764aSArtem Bityutskiy static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
12701e51764aSArtem Bityutskiy {
12711e51764aSArtem Bityutskiy 	struct ubifs_znode *znode = zbr->znode;
12721e51764aSArtem Bityutskiy 	struct ubifs_znode *zp = znode->parent;
12731e51764aSArtem Bityutskiy 	int n, err, cmp;
12741e51764aSArtem Bityutskiy 
12751e51764aSArtem Bityutskiy 	if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
12761e51764aSArtem Bityutskiy 		err = 1;
12771e51764aSArtem Bityutskiy 		goto out;
12781e51764aSArtem Bityutskiy 	}
12791e51764aSArtem Bityutskiy 	if (znode->level < 0) {
12801e51764aSArtem Bityutskiy 		err = 2;
12811e51764aSArtem Bityutskiy 		goto out;
12821e51764aSArtem Bityutskiy 	}
12831e51764aSArtem Bityutskiy 	if (znode->iip < 0 || znode->iip >= c->fanout) {
12841e51764aSArtem Bityutskiy 		err = 3;
12851e51764aSArtem Bityutskiy 		goto out;
12861e51764aSArtem Bityutskiy 	}
12871e51764aSArtem Bityutskiy 
12881e51764aSArtem Bityutskiy 	if (zbr->len == 0)
12891e51764aSArtem Bityutskiy 		/* Only dirty zbranch may have no on-flash nodes */
12901e51764aSArtem Bityutskiy 		if (!ubifs_zn_dirty(znode)) {
12911e51764aSArtem Bityutskiy 			err = 4;
12921e51764aSArtem Bityutskiy 			goto out;
12931e51764aSArtem Bityutskiy 		}
12941e51764aSArtem Bityutskiy 
12951e51764aSArtem Bityutskiy 	if (ubifs_zn_dirty(znode)) {
12961e51764aSArtem Bityutskiy 		/*
12971e51764aSArtem Bityutskiy 		 * If znode is dirty, its parent has to be dirty as well. The
12981e51764aSArtem Bityutskiy 		 * order of the operation is important, so we have to have
12991e51764aSArtem Bityutskiy 		 * memory barriers.
13001e51764aSArtem Bityutskiy 		 */
13011e51764aSArtem Bityutskiy 		smp_mb();
13021e51764aSArtem Bityutskiy 		if (zp && !ubifs_zn_dirty(zp)) {
13031e51764aSArtem Bityutskiy 			/*
13041e51764aSArtem Bityutskiy 			 * The dirty flag is atomic and is cleared outside the
13051e51764aSArtem Bityutskiy 			 * TNC mutex, so znode's dirty flag may now have
13061e51764aSArtem Bityutskiy 			 * been cleared. The child is always cleared before the
13071e51764aSArtem Bityutskiy 			 * parent, so we just need to check again.
13081e51764aSArtem Bityutskiy 			 */
13091e51764aSArtem Bityutskiy 			smp_mb();
13101e51764aSArtem Bityutskiy 			if (ubifs_zn_dirty(znode)) {
13111e51764aSArtem Bityutskiy 				err = 5;
13121e51764aSArtem Bityutskiy 				goto out;
13131e51764aSArtem Bityutskiy 			}
13141e51764aSArtem Bityutskiy 		}
13151e51764aSArtem Bityutskiy 	}
13161e51764aSArtem Bityutskiy 
13171e51764aSArtem Bityutskiy 	if (zp) {
13181e51764aSArtem Bityutskiy 		const union ubifs_key *min, *max;
13191e51764aSArtem Bityutskiy 
13201e51764aSArtem Bityutskiy 		if (znode->level != zp->level - 1) {
13211e51764aSArtem Bityutskiy 			err = 6;
13221e51764aSArtem Bityutskiy 			goto out;
13231e51764aSArtem Bityutskiy 		}
13241e51764aSArtem Bityutskiy 
13251e51764aSArtem Bityutskiy 		/* Make sure the 'parent' pointer in our znode is correct */
13261e51764aSArtem Bityutskiy 		err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
13271e51764aSArtem Bityutskiy 		if (!err) {
13281e51764aSArtem Bityutskiy 			/* This zbranch does not exist in the parent */
13291e51764aSArtem Bityutskiy 			err = 7;
13301e51764aSArtem Bityutskiy 			goto out;
13311e51764aSArtem Bityutskiy 		}
13321e51764aSArtem Bityutskiy 
13331e51764aSArtem Bityutskiy 		if (znode->iip >= zp->child_cnt) {
13341e51764aSArtem Bityutskiy 			err = 8;
13351e51764aSArtem Bityutskiy 			goto out;
13361e51764aSArtem Bityutskiy 		}
13371e51764aSArtem Bityutskiy 
13381e51764aSArtem Bityutskiy 		if (znode->iip != n) {
13391e51764aSArtem Bityutskiy 			/* This may happen only in case of collisions */
13401e51764aSArtem Bityutskiy 			if (keys_cmp(c, &zp->zbranch[n].key,
13411e51764aSArtem Bityutskiy 				     &zp->zbranch[znode->iip].key)) {
13421e51764aSArtem Bityutskiy 				err = 9;
13431e51764aSArtem Bityutskiy 				goto out;
13441e51764aSArtem Bityutskiy 			}
13451e51764aSArtem Bityutskiy 			n = znode->iip;
13461e51764aSArtem Bityutskiy 		}
13471e51764aSArtem Bityutskiy 
13481e51764aSArtem Bityutskiy 		/*
13491e51764aSArtem Bityutskiy 		 * Make sure that the first key in our znode is greater than or
13501e51764aSArtem Bityutskiy 		 * equal to the key in the pointing zbranch.
13511e51764aSArtem Bityutskiy 		 */
13521e51764aSArtem Bityutskiy 		min = &zbr->key;
13531e51764aSArtem Bityutskiy 		cmp = keys_cmp(c, min, &znode->zbranch[0].key);
13541e51764aSArtem Bityutskiy 		if (cmp == 1) {
13551e51764aSArtem Bityutskiy 			err = 10;
13561e51764aSArtem Bityutskiy 			goto out;
13571e51764aSArtem Bityutskiy 		}
13581e51764aSArtem Bityutskiy 
13591e51764aSArtem Bityutskiy 		if (n + 1 < zp->child_cnt) {
13601e51764aSArtem Bityutskiy 			max = &zp->zbranch[n + 1].key;
13611e51764aSArtem Bityutskiy 
13621e51764aSArtem Bityutskiy 			/*
13631e51764aSArtem Bityutskiy 			 * Make sure the last key in our znode is less or
13647d4e9ccbSArtem Bityutskiy 			 * equivalent than the key in the zbranch which goes
13651e51764aSArtem Bityutskiy 			 * after our pointing zbranch.
13661e51764aSArtem Bityutskiy 			 */
13671e51764aSArtem Bityutskiy 			cmp = keys_cmp(c, max,
13681e51764aSArtem Bityutskiy 				&znode->zbranch[znode->child_cnt - 1].key);
13691e51764aSArtem Bityutskiy 			if (cmp == -1) {
13701e51764aSArtem Bityutskiy 				err = 11;
13711e51764aSArtem Bityutskiy 				goto out;
13721e51764aSArtem Bityutskiy 			}
13731e51764aSArtem Bityutskiy 		}
13741e51764aSArtem Bityutskiy 	} else {
13751e51764aSArtem Bityutskiy 		/* This may only be root znode */
13761e51764aSArtem Bityutskiy 		if (zbr != &c->zroot) {
13771e51764aSArtem Bityutskiy 			err = 12;
13781e51764aSArtem Bityutskiy 			goto out;
13791e51764aSArtem Bityutskiy 		}
13801e51764aSArtem Bityutskiy 	}
13811e51764aSArtem Bityutskiy 
13821e51764aSArtem Bityutskiy 	/*
13831e51764aSArtem Bityutskiy 	 * Make sure that next key is greater or equivalent then the previous
13841e51764aSArtem Bityutskiy 	 * one.
13851e51764aSArtem Bityutskiy 	 */
13861e51764aSArtem Bityutskiy 	for (n = 1; n < znode->child_cnt; n++) {
13871e51764aSArtem Bityutskiy 		cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
13881e51764aSArtem Bityutskiy 			       &znode->zbranch[n].key);
13891e51764aSArtem Bityutskiy 		if (cmp > 0) {
13901e51764aSArtem Bityutskiy 			err = 13;
13911e51764aSArtem Bityutskiy 			goto out;
13921e51764aSArtem Bityutskiy 		}
13931e51764aSArtem Bityutskiy 		if (cmp == 0) {
13941e51764aSArtem Bityutskiy 			/* This can only be keys with colliding hash */
13951e51764aSArtem Bityutskiy 			if (!is_hash_key(c, &znode->zbranch[n].key)) {
13961e51764aSArtem Bityutskiy 				err = 14;
13971e51764aSArtem Bityutskiy 				goto out;
13981e51764aSArtem Bityutskiy 			}
13991e51764aSArtem Bityutskiy 
14001e51764aSArtem Bityutskiy 			if (znode->level != 0 || c->replaying)
14011e51764aSArtem Bityutskiy 				continue;
14021e51764aSArtem Bityutskiy 
14031e51764aSArtem Bityutskiy 			/*
14041e51764aSArtem Bityutskiy 			 * Colliding keys should follow binary order of
14051e51764aSArtem Bityutskiy 			 * corresponding xentry/dentry names.
14061e51764aSArtem Bityutskiy 			 */
14071e51764aSArtem Bityutskiy 			err = dbg_check_key_order(c, &znode->zbranch[n - 1],
14081e51764aSArtem Bityutskiy 						  &znode->zbranch[n]);
14091e51764aSArtem Bityutskiy 			if (err < 0)
14101e51764aSArtem Bityutskiy 				return err;
14111e51764aSArtem Bityutskiy 			if (err) {
14121e51764aSArtem Bityutskiy 				err = 15;
14131e51764aSArtem Bityutskiy 				goto out;
14141e51764aSArtem Bityutskiy 			}
14151e51764aSArtem Bityutskiy 		}
14161e51764aSArtem Bityutskiy 	}
14171e51764aSArtem Bityutskiy 
14181e51764aSArtem Bityutskiy 	for (n = 0; n < znode->child_cnt; n++) {
14191e51764aSArtem Bityutskiy 		if (!znode->zbranch[n].znode &&
14201e51764aSArtem Bityutskiy 		    (znode->zbranch[n].lnum == 0 ||
14211e51764aSArtem Bityutskiy 		     znode->zbranch[n].len == 0)) {
14221e51764aSArtem Bityutskiy 			err = 16;
14231e51764aSArtem Bityutskiy 			goto out;
14241e51764aSArtem Bityutskiy 		}
14251e51764aSArtem Bityutskiy 
14261e51764aSArtem Bityutskiy 		if (znode->zbranch[n].lnum != 0 &&
14271e51764aSArtem Bityutskiy 		    znode->zbranch[n].len == 0) {
14281e51764aSArtem Bityutskiy 			err = 17;
14291e51764aSArtem Bityutskiy 			goto out;
14301e51764aSArtem Bityutskiy 		}
14311e51764aSArtem Bityutskiy 
14321e51764aSArtem Bityutskiy 		if (znode->zbranch[n].lnum == 0 &&
14331e51764aSArtem Bityutskiy 		    znode->zbranch[n].len != 0) {
14341e51764aSArtem Bityutskiy 			err = 18;
14351e51764aSArtem Bityutskiy 			goto out;
14361e51764aSArtem Bityutskiy 		}
14371e51764aSArtem Bityutskiy 
14381e51764aSArtem Bityutskiy 		if (znode->zbranch[n].lnum == 0 &&
14391e51764aSArtem Bityutskiy 		    znode->zbranch[n].offs != 0) {
14401e51764aSArtem Bityutskiy 			err = 19;
14411e51764aSArtem Bityutskiy 			goto out;
14421e51764aSArtem Bityutskiy 		}
14431e51764aSArtem Bityutskiy 
14441e51764aSArtem Bityutskiy 		if (znode->level != 0 && znode->zbranch[n].znode)
14451e51764aSArtem Bityutskiy 			if (znode->zbranch[n].znode->parent != znode) {
14461e51764aSArtem Bityutskiy 				err = 20;
14471e51764aSArtem Bityutskiy 				goto out;
14481e51764aSArtem Bityutskiy 			}
14491e51764aSArtem Bityutskiy 	}
14501e51764aSArtem Bityutskiy 
14511e51764aSArtem Bityutskiy 	return 0;
14521e51764aSArtem Bityutskiy 
14531e51764aSArtem Bityutskiy out:
1454235c362bSSheng Yong 	ubifs_err(c, "failed, error %d", err);
1455235c362bSSheng Yong 	ubifs_msg(c, "dump of the znode");
1456edf6be24SArtem Bityutskiy 	ubifs_dump_znode(c, znode);
14571e51764aSArtem Bityutskiy 	if (zp) {
1458235c362bSSheng Yong 		ubifs_msg(c, "dump of the parent znode");
1459edf6be24SArtem Bityutskiy 		ubifs_dump_znode(c, zp);
14601e51764aSArtem Bityutskiy 	}
14611e51764aSArtem Bityutskiy 	dump_stack();
14621e51764aSArtem Bityutskiy 	return -EINVAL;
14631e51764aSArtem Bityutskiy }
14641e51764aSArtem Bityutskiy 
14651e51764aSArtem Bityutskiy /**
14661e51764aSArtem Bityutskiy  * dbg_check_tnc - check TNC tree.
14671e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
14681e51764aSArtem Bityutskiy  * @extra: do extra checks that are possible at start commit
14691e51764aSArtem Bityutskiy  *
14701e51764aSArtem Bityutskiy  * This function traverses whole TNC tree and checks every znode. Returns zero
14711e51764aSArtem Bityutskiy  * if everything is all right and %-EINVAL if something is wrong with TNC.
14721e51764aSArtem Bityutskiy  */
14731e51764aSArtem Bityutskiy int dbg_check_tnc(struct ubifs_info *c, int extra)
14741e51764aSArtem Bityutskiy {
14751e51764aSArtem Bityutskiy 	struct ubifs_znode *znode;
14761e51764aSArtem Bityutskiy 	long clean_cnt = 0, dirty_cnt = 0;
14771e51764aSArtem Bityutskiy 	int err, last;
14781e51764aSArtem Bityutskiy 
14798d7819b4SArtem Bityutskiy 	if (!dbg_is_chk_index(c))
14801e51764aSArtem Bityutskiy 		return 0;
14811e51764aSArtem Bityutskiy 
1482*6eb61d58SRichard Weinberger 	ubifs_assert(c, mutex_is_locked(&c->tnc_mutex));
14831e51764aSArtem Bityutskiy 	if (!c->zroot.znode)
14841e51764aSArtem Bityutskiy 		return 0;
14851e51764aSArtem Bityutskiy 
14861e51764aSArtem Bityutskiy 	znode = ubifs_tnc_postorder_first(c->zroot.znode);
14871e51764aSArtem Bityutskiy 	while (1) {
14881e51764aSArtem Bityutskiy 		struct ubifs_znode *prev;
14891e51764aSArtem Bityutskiy 		struct ubifs_zbranch *zbr;
14901e51764aSArtem Bityutskiy 
14911e51764aSArtem Bityutskiy 		if (!znode->parent)
14921e51764aSArtem Bityutskiy 			zbr = &c->zroot;
14931e51764aSArtem Bityutskiy 		else
14941e51764aSArtem Bityutskiy 			zbr = &znode->parent->zbranch[znode->iip];
14951e51764aSArtem Bityutskiy 
14961e51764aSArtem Bityutskiy 		err = dbg_check_znode(c, zbr);
14971e51764aSArtem Bityutskiy 		if (err)
14981e51764aSArtem Bityutskiy 			return err;
14991e51764aSArtem Bityutskiy 
15001e51764aSArtem Bityutskiy 		if (extra) {
15011e51764aSArtem Bityutskiy 			if (ubifs_zn_dirty(znode))
15021e51764aSArtem Bityutskiy 				dirty_cnt += 1;
15031e51764aSArtem Bityutskiy 			else
15041e51764aSArtem Bityutskiy 				clean_cnt += 1;
15051e51764aSArtem Bityutskiy 		}
15061e51764aSArtem Bityutskiy 
15071e51764aSArtem Bityutskiy 		prev = znode;
1508*6eb61d58SRichard Weinberger 		znode = ubifs_tnc_postorder_next(c, znode);
15091e51764aSArtem Bityutskiy 		if (!znode)
15101e51764aSArtem Bityutskiy 			break;
15111e51764aSArtem Bityutskiy 
15121e51764aSArtem Bityutskiy 		/*
15131e51764aSArtem Bityutskiy 		 * If the last key of this znode is equivalent to the first key
15141e51764aSArtem Bityutskiy 		 * of the next znode (collision), then check order of the keys.
15151e51764aSArtem Bityutskiy 		 */
15161e51764aSArtem Bityutskiy 		last = prev->child_cnt - 1;
15171e51764aSArtem Bityutskiy 		if (prev->level == 0 && znode->level == 0 && !c->replaying &&
15181e51764aSArtem Bityutskiy 		    !keys_cmp(c, &prev->zbranch[last].key,
15191e51764aSArtem Bityutskiy 			      &znode->zbranch[0].key)) {
15201e51764aSArtem Bityutskiy 			err = dbg_check_key_order(c, &prev->zbranch[last],
15211e51764aSArtem Bityutskiy 						  &znode->zbranch[0]);
15221e51764aSArtem Bityutskiy 			if (err < 0)
15231e51764aSArtem Bityutskiy 				return err;
15241e51764aSArtem Bityutskiy 			if (err) {
1525235c362bSSheng Yong 				ubifs_msg(c, "first znode");
1526edf6be24SArtem Bityutskiy 				ubifs_dump_znode(c, prev);
1527235c362bSSheng Yong 				ubifs_msg(c, "second znode");
1528edf6be24SArtem Bityutskiy 				ubifs_dump_znode(c, znode);
15291e51764aSArtem Bityutskiy 				return -EINVAL;
15301e51764aSArtem Bityutskiy 			}
15311e51764aSArtem Bityutskiy 		}
15321e51764aSArtem Bityutskiy 	}
15331e51764aSArtem Bityutskiy 
15341e51764aSArtem Bityutskiy 	if (extra) {
15351e51764aSArtem Bityutskiy 		if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
1536235c362bSSheng Yong 			ubifs_err(c, "incorrect clean_zn_cnt %ld, calculated %ld",
15371e51764aSArtem Bityutskiy 				  atomic_long_read(&c->clean_zn_cnt),
15381e51764aSArtem Bityutskiy 				  clean_cnt);
15391e51764aSArtem Bityutskiy 			return -EINVAL;
15401e51764aSArtem Bityutskiy 		}
15411e51764aSArtem Bityutskiy 		if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
1542235c362bSSheng Yong 			ubifs_err(c, "incorrect dirty_zn_cnt %ld, calculated %ld",
15431e51764aSArtem Bityutskiy 				  atomic_long_read(&c->dirty_zn_cnt),
15441e51764aSArtem Bityutskiy 				  dirty_cnt);
15451e51764aSArtem Bityutskiy 			return -EINVAL;
15461e51764aSArtem Bityutskiy 		}
15471e51764aSArtem Bityutskiy 	}
15481e51764aSArtem Bityutskiy 
15491e51764aSArtem Bityutskiy 	return 0;
15501e51764aSArtem Bityutskiy }
15511e51764aSArtem Bityutskiy 
15521e51764aSArtem Bityutskiy /**
15531e51764aSArtem Bityutskiy  * dbg_walk_index - walk the on-flash index.
15541e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
15551e51764aSArtem Bityutskiy  * @leaf_cb: called for each leaf node
15561e51764aSArtem Bityutskiy  * @znode_cb: called for each indexing node
1557227c75c9SAdrian Hunter  * @priv: private data which is passed to callbacks
15581e51764aSArtem Bityutskiy  *
15591e51764aSArtem Bityutskiy  * This function walks the UBIFS index and calls the @leaf_cb for each leaf
15601e51764aSArtem Bityutskiy  * node and @znode_cb for each indexing node. Returns zero in case of success
15611e51764aSArtem Bityutskiy  * and a negative error code in case of failure.
15621e51764aSArtem Bityutskiy  *
15631e51764aSArtem Bityutskiy  * It would be better if this function removed every znode it pulled to into
15641e51764aSArtem Bityutskiy  * the TNC, so that the behavior more closely matched the non-debugging
15651e51764aSArtem Bityutskiy  * behavior.
15661e51764aSArtem Bityutskiy  */
15671e51764aSArtem Bityutskiy int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
15681e51764aSArtem Bityutskiy 		   dbg_znode_callback znode_cb, void *priv)
15691e51764aSArtem Bityutskiy {
15701e51764aSArtem Bityutskiy 	int err;
15711e51764aSArtem Bityutskiy 	struct ubifs_zbranch *zbr;
15721e51764aSArtem Bityutskiy 	struct ubifs_znode *znode, *child;
15731e51764aSArtem Bityutskiy 
15741e51764aSArtem Bityutskiy 	mutex_lock(&c->tnc_mutex);
15751e51764aSArtem Bityutskiy 	/* If the root indexing node is not in TNC - pull it */
15761e51764aSArtem Bityutskiy 	if (!c->zroot.znode) {
15771e51764aSArtem Bityutskiy 		c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
15781e51764aSArtem Bityutskiy 		if (IS_ERR(c->zroot.znode)) {
15791e51764aSArtem Bityutskiy 			err = PTR_ERR(c->zroot.znode);
15801e51764aSArtem Bityutskiy 			c->zroot.znode = NULL;
15811e51764aSArtem Bityutskiy 			goto out_unlock;
15821e51764aSArtem Bityutskiy 		}
15831e51764aSArtem Bityutskiy 	}
15841e51764aSArtem Bityutskiy 
15851e51764aSArtem Bityutskiy 	/*
15861e51764aSArtem Bityutskiy 	 * We are going to traverse the indexing tree in the postorder manner.
15871e51764aSArtem Bityutskiy 	 * Go down and find the leftmost indexing node where we are going to
15881e51764aSArtem Bityutskiy 	 * start from.
15891e51764aSArtem Bityutskiy 	 */
15901e51764aSArtem Bityutskiy 	znode = c->zroot.znode;
15911e51764aSArtem Bityutskiy 	while (znode->level > 0) {
15921e51764aSArtem Bityutskiy 		zbr = &znode->zbranch[0];
15931e51764aSArtem Bityutskiy 		child = zbr->znode;
15941e51764aSArtem Bityutskiy 		if (!child) {
15951e51764aSArtem Bityutskiy 			child = ubifs_load_znode(c, zbr, znode, 0);
15961e51764aSArtem Bityutskiy 			if (IS_ERR(child)) {
15971e51764aSArtem Bityutskiy 				err = PTR_ERR(child);
15981e51764aSArtem Bityutskiy 				goto out_unlock;
15991e51764aSArtem Bityutskiy 			}
16001e51764aSArtem Bityutskiy 			zbr->znode = child;
16011e51764aSArtem Bityutskiy 		}
16021e51764aSArtem Bityutskiy 
16031e51764aSArtem Bityutskiy 		znode = child;
16041e51764aSArtem Bityutskiy 	}
16051e51764aSArtem Bityutskiy 
16061e51764aSArtem Bityutskiy 	/* Iterate over all indexing nodes */
16071e51764aSArtem Bityutskiy 	while (1) {
16081e51764aSArtem Bityutskiy 		int idx;
16091e51764aSArtem Bityutskiy 
16101e51764aSArtem Bityutskiy 		cond_resched();
16111e51764aSArtem Bityutskiy 
16121e51764aSArtem Bityutskiy 		if (znode_cb) {
16131e51764aSArtem Bityutskiy 			err = znode_cb(c, znode, priv);
16141e51764aSArtem Bityutskiy 			if (err) {
1615235c362bSSheng Yong 				ubifs_err(c, "znode checking function returned error %d",
161679fda517SArtem Bityutskiy 					  err);
1617edf6be24SArtem Bityutskiy 				ubifs_dump_znode(c, znode);
16181e51764aSArtem Bityutskiy 				goto out_dump;
16191e51764aSArtem Bityutskiy 			}
16201e51764aSArtem Bityutskiy 		}
16211e51764aSArtem Bityutskiy 		if (leaf_cb && znode->level == 0) {
16221e51764aSArtem Bityutskiy 			for (idx = 0; idx < znode->child_cnt; idx++) {
16231e51764aSArtem Bityutskiy 				zbr = &znode->zbranch[idx];
16241e51764aSArtem Bityutskiy 				err = leaf_cb(c, zbr, priv);
16251e51764aSArtem Bityutskiy 				if (err) {
1626235c362bSSheng Yong 					ubifs_err(c, "leaf checking function returned error %d, for leaf at LEB %d:%d",
16271e51764aSArtem Bityutskiy 						  err, zbr->lnum, zbr->offs);
16281e51764aSArtem Bityutskiy 					goto out_dump;
16291e51764aSArtem Bityutskiy 				}
16301e51764aSArtem Bityutskiy 			}
16311e51764aSArtem Bityutskiy 		}
16321e51764aSArtem Bityutskiy 
16331e51764aSArtem Bityutskiy 		if (!znode->parent)
16341e51764aSArtem Bityutskiy 			break;
16351e51764aSArtem Bityutskiy 
16361e51764aSArtem Bityutskiy 		idx = znode->iip + 1;
16371e51764aSArtem Bityutskiy 		znode = znode->parent;
16381e51764aSArtem Bityutskiy 		if (idx < znode->child_cnt) {
16391e51764aSArtem Bityutskiy 			/* Switch to the next index in the parent */
16401e51764aSArtem Bityutskiy 			zbr = &znode->zbranch[idx];
16411e51764aSArtem Bityutskiy 			child = zbr->znode;
16421e51764aSArtem Bityutskiy 			if (!child) {
16431e51764aSArtem Bityutskiy 				child = ubifs_load_znode(c, zbr, znode, idx);
16441e51764aSArtem Bityutskiy 				if (IS_ERR(child)) {
16451e51764aSArtem Bityutskiy 					err = PTR_ERR(child);
16461e51764aSArtem Bityutskiy 					goto out_unlock;
16471e51764aSArtem Bityutskiy 				}
16481e51764aSArtem Bityutskiy 				zbr->znode = child;
16491e51764aSArtem Bityutskiy 			}
16501e51764aSArtem Bityutskiy 			znode = child;
16511e51764aSArtem Bityutskiy 		} else
16521e51764aSArtem Bityutskiy 			/*
16531e51764aSArtem Bityutskiy 			 * This is the last child, switch to the parent and
16541e51764aSArtem Bityutskiy 			 * continue.
16551e51764aSArtem Bityutskiy 			 */
16561e51764aSArtem Bityutskiy 			continue;
16571e51764aSArtem Bityutskiy 
16581e51764aSArtem Bityutskiy 		/* Go to the lowest leftmost znode in the new sub-tree */
16591e51764aSArtem Bityutskiy 		while (znode->level > 0) {
16601e51764aSArtem Bityutskiy 			zbr = &znode->zbranch[0];
16611e51764aSArtem Bityutskiy 			child = zbr->znode;
16621e51764aSArtem Bityutskiy 			if (!child) {
16631e51764aSArtem Bityutskiy 				child = ubifs_load_znode(c, zbr, znode, 0);
16641e51764aSArtem Bityutskiy 				if (IS_ERR(child)) {
16651e51764aSArtem Bityutskiy 					err = PTR_ERR(child);
16661e51764aSArtem Bityutskiy 					goto out_unlock;
16671e51764aSArtem Bityutskiy 				}
16681e51764aSArtem Bityutskiy 				zbr->znode = child;
16691e51764aSArtem Bityutskiy 			}
16701e51764aSArtem Bityutskiy 			znode = child;
16711e51764aSArtem Bityutskiy 		}
16721e51764aSArtem Bityutskiy 	}
16731e51764aSArtem Bityutskiy 
16741e51764aSArtem Bityutskiy 	mutex_unlock(&c->tnc_mutex);
16751e51764aSArtem Bityutskiy 	return 0;
16761e51764aSArtem Bityutskiy 
16771e51764aSArtem Bityutskiy out_dump:
16781e51764aSArtem Bityutskiy 	if (znode->parent)
16791e51764aSArtem Bityutskiy 		zbr = &znode->parent->zbranch[znode->iip];
16801e51764aSArtem Bityutskiy 	else
16811e51764aSArtem Bityutskiy 		zbr = &c->zroot;
1682235c362bSSheng Yong 	ubifs_msg(c, "dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1683edf6be24SArtem Bityutskiy 	ubifs_dump_znode(c, znode);
16841e51764aSArtem Bityutskiy out_unlock:
16851e51764aSArtem Bityutskiy 	mutex_unlock(&c->tnc_mutex);
16861e51764aSArtem Bityutskiy 	return err;
16871e51764aSArtem Bityutskiy }
16881e51764aSArtem Bityutskiy 
16891e51764aSArtem Bityutskiy /**
16901e51764aSArtem Bityutskiy  * add_size - add znode size to partially calculated index size.
16911e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
16921e51764aSArtem Bityutskiy  * @znode: znode to add size for
16931e51764aSArtem Bityutskiy  * @priv: partially calculated index size
16941e51764aSArtem Bityutskiy  *
16951e51764aSArtem Bityutskiy  * This is a helper function for 'dbg_check_idx_size()' which is called for
16961e51764aSArtem Bityutskiy  * every indexing node and adds its size to the 'long long' variable pointed to
16971e51764aSArtem Bityutskiy  * by @priv.
16981e51764aSArtem Bityutskiy  */
16991e51764aSArtem Bityutskiy static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
17001e51764aSArtem Bityutskiy {
17011e51764aSArtem Bityutskiy 	long long *idx_size = priv;
17021e51764aSArtem Bityutskiy 	int add;
17031e51764aSArtem Bityutskiy 
17041e51764aSArtem Bityutskiy 	add = ubifs_idx_node_sz(c, znode->child_cnt);
17051e51764aSArtem Bityutskiy 	add = ALIGN(add, 8);
17061e51764aSArtem Bityutskiy 	*idx_size += add;
17071e51764aSArtem Bityutskiy 	return 0;
17081e51764aSArtem Bityutskiy }
17091e51764aSArtem Bityutskiy 
17101e51764aSArtem Bityutskiy /**
17111e51764aSArtem Bityutskiy  * dbg_check_idx_size - check index size.
17121e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
17131e51764aSArtem Bityutskiy  * @idx_size: size to check
17141e51764aSArtem Bityutskiy  *
17151e51764aSArtem Bityutskiy  * This function walks the UBIFS index, calculates its size and checks that the
17161e51764aSArtem Bityutskiy  * size is equivalent to @idx_size. Returns zero in case of success and a
17171e51764aSArtem Bityutskiy  * negative error code in case of failure.
17181e51764aSArtem Bityutskiy  */
17191e51764aSArtem Bityutskiy int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
17201e51764aSArtem Bityutskiy {
17211e51764aSArtem Bityutskiy 	int err;
17221e51764aSArtem Bityutskiy 	long long calc = 0;
17231e51764aSArtem Bityutskiy 
17248d7819b4SArtem Bityutskiy 	if (!dbg_is_chk_index(c))
17251e51764aSArtem Bityutskiy 		return 0;
17261e51764aSArtem Bityutskiy 
17271e51764aSArtem Bityutskiy 	err = dbg_walk_index(c, NULL, add_size, &calc);
17281e51764aSArtem Bityutskiy 	if (err) {
1729235c362bSSheng Yong 		ubifs_err(c, "error %d while walking the index", err);
17301e51764aSArtem Bityutskiy 		return err;
17311e51764aSArtem Bityutskiy 	}
17321e51764aSArtem Bityutskiy 
17331e51764aSArtem Bityutskiy 	if (calc != idx_size) {
1734235c362bSSheng Yong 		ubifs_err(c, "index size check failed: calculated size is %lld, should be %lld",
173579fda517SArtem Bityutskiy 			  calc, idx_size);
17361e51764aSArtem Bityutskiy 		dump_stack();
17371e51764aSArtem Bityutskiy 		return -EINVAL;
17381e51764aSArtem Bityutskiy 	}
17391e51764aSArtem Bityutskiy 
17401e51764aSArtem Bityutskiy 	return 0;
17411e51764aSArtem Bityutskiy }
17421e51764aSArtem Bityutskiy 
17431e51764aSArtem Bityutskiy /**
17441e51764aSArtem Bityutskiy  * struct fsck_inode - information about an inode used when checking the file-system.
17451e51764aSArtem Bityutskiy  * @rb: link in the RB-tree of inodes
17461e51764aSArtem Bityutskiy  * @inum: inode number
17471e51764aSArtem Bityutskiy  * @mode: inode type, permissions, etc
17481e51764aSArtem Bityutskiy  * @nlink: inode link count
17491e51764aSArtem Bityutskiy  * @xattr_cnt: count of extended attributes
17501e51764aSArtem Bityutskiy  * @references: how many directory/xattr entries refer this inode (calculated
17511e51764aSArtem Bityutskiy  *              while walking the index)
17521e51764aSArtem Bityutskiy  * @calc_cnt: for directory inode count of child directories
17531e51764aSArtem Bityutskiy  * @size: inode size (read from on-flash inode)
17541e51764aSArtem Bityutskiy  * @xattr_sz: summary size of all extended attributes (read from on-flash
17551e51764aSArtem Bityutskiy  *            inode)
17561e51764aSArtem Bityutskiy  * @calc_sz: for directories calculated directory size
17571e51764aSArtem Bityutskiy  * @calc_xcnt: count of extended attributes
17581e51764aSArtem Bityutskiy  * @calc_xsz: calculated summary size of all extended attributes
17591e51764aSArtem Bityutskiy  * @xattr_nms: sum of lengths of all extended attribute names belonging to this
17601e51764aSArtem Bityutskiy  *             inode (read from on-flash inode)
17611e51764aSArtem Bityutskiy  * @calc_xnms: calculated sum of lengths of all extended attribute names
17621e51764aSArtem Bityutskiy  */
17631e51764aSArtem Bityutskiy struct fsck_inode {
17641e51764aSArtem Bityutskiy 	struct rb_node rb;
17651e51764aSArtem Bityutskiy 	ino_t inum;
17661e51764aSArtem Bityutskiy 	umode_t mode;
17671e51764aSArtem Bityutskiy 	unsigned int nlink;
17681e51764aSArtem Bityutskiy 	unsigned int xattr_cnt;
17691e51764aSArtem Bityutskiy 	int references;
17701e51764aSArtem Bityutskiy 	int calc_cnt;
17711e51764aSArtem Bityutskiy 	long long size;
17721e51764aSArtem Bityutskiy 	unsigned int xattr_sz;
17731e51764aSArtem Bityutskiy 	long long calc_sz;
17741e51764aSArtem Bityutskiy 	long long calc_xcnt;
17751e51764aSArtem Bityutskiy 	long long calc_xsz;
17761e51764aSArtem Bityutskiy 	unsigned int xattr_nms;
17771e51764aSArtem Bityutskiy 	long long calc_xnms;
17781e51764aSArtem Bityutskiy };
17791e51764aSArtem Bityutskiy 
17801e51764aSArtem Bityutskiy /**
17811e51764aSArtem Bityutskiy  * struct fsck_data - private FS checking information.
17821e51764aSArtem Bityutskiy  * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
17831e51764aSArtem Bityutskiy  */
17841e51764aSArtem Bityutskiy struct fsck_data {
17851e51764aSArtem Bityutskiy 	struct rb_root inodes;
17861e51764aSArtem Bityutskiy };
17871e51764aSArtem Bityutskiy 
17881e51764aSArtem Bityutskiy /**
17891e51764aSArtem Bityutskiy  * add_inode - add inode information to RB-tree of inodes.
17901e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
17911e51764aSArtem Bityutskiy  * @fsckd: FS checking information
17921e51764aSArtem Bityutskiy  * @ino: raw UBIFS inode to add
17931e51764aSArtem Bityutskiy  *
17941e51764aSArtem Bityutskiy  * This is a helper function for 'check_leaf()' which adds information about
17951e51764aSArtem Bityutskiy  * inode @ino to the RB-tree of inodes. Returns inode information pointer in
17961e51764aSArtem Bityutskiy  * case of success and a negative error code in case of failure.
17971e51764aSArtem Bityutskiy  */
17981e51764aSArtem Bityutskiy static struct fsck_inode *add_inode(struct ubifs_info *c,
17991e51764aSArtem Bityutskiy 				    struct fsck_data *fsckd,
18001e51764aSArtem Bityutskiy 				    struct ubifs_ino_node *ino)
18011e51764aSArtem Bityutskiy {
18021e51764aSArtem Bityutskiy 	struct rb_node **p, *parent = NULL;
18031e51764aSArtem Bityutskiy 	struct fsck_inode *fscki;
18041e51764aSArtem Bityutskiy 	ino_t inum = key_inum_flash(c, &ino->key);
180545cd5cddSArtem Bityutskiy 	struct inode *inode;
180645cd5cddSArtem Bityutskiy 	struct ubifs_inode *ui;
18071e51764aSArtem Bityutskiy 
18081e51764aSArtem Bityutskiy 	p = &fsckd->inodes.rb_node;
18091e51764aSArtem Bityutskiy 	while (*p) {
18101e51764aSArtem Bityutskiy 		parent = *p;
18111e51764aSArtem Bityutskiy 		fscki = rb_entry(parent, struct fsck_inode, rb);
18121e51764aSArtem Bityutskiy 		if (inum < fscki->inum)
18131e51764aSArtem Bityutskiy 			p = &(*p)->rb_left;
18141e51764aSArtem Bityutskiy 		else if (inum > fscki->inum)
18151e51764aSArtem Bityutskiy 			p = &(*p)->rb_right;
18161e51764aSArtem Bityutskiy 		else
18171e51764aSArtem Bityutskiy 			return fscki;
18181e51764aSArtem Bityutskiy 	}
18191e51764aSArtem Bityutskiy 
18201e51764aSArtem Bityutskiy 	if (inum > c->highest_inum) {
1821235c362bSSheng Yong 		ubifs_err(c, "too high inode number, max. is %lu",
1822e84461adSArtem Bityutskiy 			  (unsigned long)c->highest_inum);
18231e51764aSArtem Bityutskiy 		return ERR_PTR(-EINVAL);
18241e51764aSArtem Bityutskiy 	}
18251e51764aSArtem Bityutskiy 
18261e51764aSArtem Bityutskiy 	fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
18271e51764aSArtem Bityutskiy 	if (!fscki)
18281e51764aSArtem Bityutskiy 		return ERR_PTR(-ENOMEM);
18291e51764aSArtem Bityutskiy 
183045cd5cddSArtem Bityutskiy 	inode = ilookup(c->vfs_sb, inum);
183145cd5cddSArtem Bityutskiy 
18321e51764aSArtem Bityutskiy 	fscki->inum = inum;
183345cd5cddSArtem Bityutskiy 	/*
183445cd5cddSArtem Bityutskiy 	 * If the inode is present in the VFS inode cache, use it instead of
183545cd5cddSArtem Bityutskiy 	 * the on-flash inode which might be out-of-date. E.g., the size might
183645cd5cddSArtem Bityutskiy 	 * be out-of-date. If we do not do this, the following may happen, for
183745cd5cddSArtem Bityutskiy 	 * example:
183845cd5cddSArtem Bityutskiy 	 *   1. A power cut happens
183945cd5cddSArtem Bityutskiy 	 *   2. We mount the file-system R/O, the replay process fixes up the
184045cd5cddSArtem Bityutskiy 	 *      inode size in the VFS cache, but on on-flash.
184145cd5cddSArtem Bityutskiy 	 *   3. 'check_leaf()' fails because it hits a data node beyond inode
184245cd5cddSArtem Bityutskiy 	 *      size.
184345cd5cddSArtem Bityutskiy 	 */
184445cd5cddSArtem Bityutskiy 	if (!inode) {
18451e51764aSArtem Bityutskiy 		fscki->nlink = le32_to_cpu(ino->nlink);
18461e51764aSArtem Bityutskiy 		fscki->size = le64_to_cpu(ino->size);
18471e51764aSArtem Bityutskiy 		fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
18481e51764aSArtem Bityutskiy 		fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
18491e51764aSArtem Bityutskiy 		fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
18501e51764aSArtem Bityutskiy 		fscki->mode = le32_to_cpu(ino->mode);
185145cd5cddSArtem Bityutskiy 	} else {
185245cd5cddSArtem Bityutskiy 		ui = ubifs_inode(inode);
185345cd5cddSArtem Bityutskiy 		fscki->nlink = inode->i_nlink;
185445cd5cddSArtem Bityutskiy 		fscki->size = inode->i_size;
185545cd5cddSArtem Bityutskiy 		fscki->xattr_cnt = ui->xattr_cnt;
185645cd5cddSArtem Bityutskiy 		fscki->xattr_sz = ui->xattr_size;
185745cd5cddSArtem Bityutskiy 		fscki->xattr_nms = ui->xattr_names;
185845cd5cddSArtem Bityutskiy 		fscki->mode = inode->i_mode;
185945cd5cddSArtem Bityutskiy 		iput(inode);
186045cd5cddSArtem Bityutskiy 	}
186145cd5cddSArtem Bityutskiy 
18621e51764aSArtem Bityutskiy 	if (S_ISDIR(fscki->mode)) {
18631e51764aSArtem Bityutskiy 		fscki->calc_sz = UBIFS_INO_NODE_SZ;
18641e51764aSArtem Bityutskiy 		fscki->calc_cnt = 2;
18651e51764aSArtem Bityutskiy 	}
186645cd5cddSArtem Bityutskiy 
18671e51764aSArtem Bityutskiy 	rb_link_node(&fscki->rb, parent, p);
18681e51764aSArtem Bityutskiy 	rb_insert_color(&fscki->rb, &fsckd->inodes);
186945cd5cddSArtem Bityutskiy 
18701e51764aSArtem Bityutskiy 	return fscki;
18711e51764aSArtem Bityutskiy }
18721e51764aSArtem Bityutskiy 
18731e51764aSArtem Bityutskiy /**
18741e51764aSArtem Bityutskiy  * search_inode - search inode in the RB-tree of inodes.
18751e51764aSArtem Bityutskiy  * @fsckd: FS checking information
18761e51764aSArtem Bityutskiy  * @inum: inode number to search
18771e51764aSArtem Bityutskiy  *
18781e51764aSArtem Bityutskiy  * This is a helper function for 'check_leaf()' which searches inode @inum in
18791e51764aSArtem Bityutskiy  * the RB-tree of inodes and returns an inode information pointer or %NULL if
18801e51764aSArtem Bityutskiy  * the inode was not found.
18811e51764aSArtem Bityutskiy  */
18821e51764aSArtem Bityutskiy static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
18831e51764aSArtem Bityutskiy {
18841e51764aSArtem Bityutskiy 	struct rb_node *p;
18851e51764aSArtem Bityutskiy 	struct fsck_inode *fscki;
18861e51764aSArtem Bityutskiy 
18871e51764aSArtem Bityutskiy 	p = fsckd->inodes.rb_node;
18881e51764aSArtem Bityutskiy 	while (p) {
18891e51764aSArtem Bityutskiy 		fscki = rb_entry(p, struct fsck_inode, rb);
18901e51764aSArtem Bityutskiy 		if (inum < fscki->inum)
18911e51764aSArtem Bityutskiy 			p = p->rb_left;
18921e51764aSArtem Bityutskiy 		else if (inum > fscki->inum)
18931e51764aSArtem Bityutskiy 			p = p->rb_right;
18941e51764aSArtem Bityutskiy 		else
18951e51764aSArtem Bityutskiy 			return fscki;
18961e51764aSArtem Bityutskiy 	}
18971e51764aSArtem Bityutskiy 	return NULL;
18981e51764aSArtem Bityutskiy }
18991e51764aSArtem Bityutskiy 
19001e51764aSArtem Bityutskiy /**
19011e51764aSArtem Bityutskiy  * read_add_inode - read inode node and add it to RB-tree of inodes.
19021e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
19031e51764aSArtem Bityutskiy  * @fsckd: FS checking information
19041e51764aSArtem Bityutskiy  * @inum: inode number to read
19051e51764aSArtem Bityutskiy  *
19061e51764aSArtem Bityutskiy  * This is a helper function for 'check_leaf()' which finds inode node @inum in
19071e51764aSArtem Bityutskiy  * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
19081e51764aSArtem Bityutskiy  * information pointer in case of success and a negative error code in case of
19091e51764aSArtem Bityutskiy  * failure.
19101e51764aSArtem Bityutskiy  */
19111e51764aSArtem Bityutskiy static struct fsck_inode *read_add_inode(struct ubifs_info *c,
19121e51764aSArtem Bityutskiy 					 struct fsck_data *fsckd, ino_t inum)
19131e51764aSArtem Bityutskiy {
19141e51764aSArtem Bityutskiy 	int n, err;
19151e51764aSArtem Bityutskiy 	union ubifs_key key;
19161e51764aSArtem Bityutskiy 	struct ubifs_znode *znode;
19171e51764aSArtem Bityutskiy 	struct ubifs_zbranch *zbr;
19181e51764aSArtem Bityutskiy 	struct ubifs_ino_node *ino;
19191e51764aSArtem Bityutskiy 	struct fsck_inode *fscki;
19201e51764aSArtem Bityutskiy 
19211e51764aSArtem Bityutskiy 	fscki = search_inode(fsckd, inum);
19221e51764aSArtem Bityutskiy 	if (fscki)
19231e51764aSArtem Bityutskiy 		return fscki;
19241e51764aSArtem Bityutskiy 
19251e51764aSArtem Bityutskiy 	ino_key_init(c, &key, inum);
19261e51764aSArtem Bityutskiy 	err = ubifs_lookup_level0(c, &key, &znode, &n);
19271e51764aSArtem Bityutskiy 	if (!err) {
1928235c362bSSheng Yong 		ubifs_err(c, "inode %lu not found in index", (unsigned long)inum);
19291e51764aSArtem Bityutskiy 		return ERR_PTR(-ENOENT);
19301e51764aSArtem Bityutskiy 	} else if (err < 0) {
1931235c362bSSheng Yong 		ubifs_err(c, "error %d while looking up inode %lu",
1932e84461adSArtem Bityutskiy 			  err, (unsigned long)inum);
19331e51764aSArtem Bityutskiy 		return ERR_PTR(err);
19341e51764aSArtem Bityutskiy 	}
19351e51764aSArtem Bityutskiy 
19361e51764aSArtem Bityutskiy 	zbr = &znode->zbranch[n];
19371e51764aSArtem Bityutskiy 	if (zbr->len < UBIFS_INO_NODE_SZ) {
1938235c362bSSheng Yong 		ubifs_err(c, "bad node %lu node length %d",
1939e84461adSArtem Bityutskiy 			  (unsigned long)inum, zbr->len);
19401e51764aSArtem Bityutskiy 		return ERR_PTR(-EINVAL);
19411e51764aSArtem Bityutskiy 	}
19421e51764aSArtem Bityutskiy 
19431e51764aSArtem Bityutskiy 	ino = kmalloc(zbr->len, GFP_NOFS);
19441e51764aSArtem Bityutskiy 	if (!ino)
19451e51764aSArtem Bityutskiy 		return ERR_PTR(-ENOMEM);
19461e51764aSArtem Bityutskiy 
19471e51764aSArtem Bityutskiy 	err = ubifs_tnc_read_node(c, zbr, ino);
19481e51764aSArtem Bityutskiy 	if (err) {
1949235c362bSSheng Yong 		ubifs_err(c, "cannot read inode node at LEB %d:%d, error %d",
19501e51764aSArtem Bityutskiy 			  zbr->lnum, zbr->offs, err);
19511e51764aSArtem Bityutskiy 		kfree(ino);
19521e51764aSArtem Bityutskiy 		return ERR_PTR(err);
19531e51764aSArtem Bityutskiy 	}
19541e51764aSArtem Bityutskiy 
19551e51764aSArtem Bityutskiy 	fscki = add_inode(c, fsckd, ino);
19561e51764aSArtem Bityutskiy 	kfree(ino);
19571e51764aSArtem Bityutskiy 	if (IS_ERR(fscki)) {
1958235c362bSSheng Yong 		ubifs_err(c, "error %ld while adding inode %lu node",
1959e84461adSArtem Bityutskiy 			  PTR_ERR(fscki), (unsigned long)inum);
19601e51764aSArtem Bityutskiy 		return fscki;
19611e51764aSArtem Bityutskiy 	}
19621e51764aSArtem Bityutskiy 
19631e51764aSArtem Bityutskiy 	return fscki;
19641e51764aSArtem Bityutskiy }
19651e51764aSArtem Bityutskiy 
19661e51764aSArtem Bityutskiy /**
19671e51764aSArtem Bityutskiy  * check_leaf - check leaf node.
19681e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
19691e51764aSArtem Bityutskiy  * @zbr: zbranch of the leaf node to check
19701e51764aSArtem Bityutskiy  * @priv: FS checking information
19711e51764aSArtem Bityutskiy  *
19721e51764aSArtem Bityutskiy  * This is a helper function for 'dbg_check_filesystem()' which is called for
19731e51764aSArtem Bityutskiy  * every single leaf node while walking the indexing tree. It checks that the
19741e51764aSArtem Bityutskiy  * leaf node referred from the indexing tree exists, has correct CRC, and does
19751e51764aSArtem Bityutskiy  * some other basic validation. This function is also responsible for building
19761e51764aSArtem Bityutskiy  * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
19771e51764aSArtem Bityutskiy  * calculates reference count, size, etc for each inode in order to later
19781e51764aSArtem Bityutskiy  * compare them to the information stored inside the inodes and detect possible
19791e51764aSArtem Bityutskiy  * inconsistencies. Returns zero in case of success and a negative error code
19801e51764aSArtem Bityutskiy  * in case of failure.
19811e51764aSArtem Bityutskiy  */
19821e51764aSArtem Bityutskiy static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
19831e51764aSArtem Bityutskiy 		      void *priv)
19841e51764aSArtem Bityutskiy {
19851e51764aSArtem Bityutskiy 	ino_t inum;
19861e51764aSArtem Bityutskiy 	void *node;
19871e51764aSArtem Bityutskiy 	struct ubifs_ch *ch;
19881e51764aSArtem Bityutskiy 	int err, type = key_type(c, &zbr->key);
19891e51764aSArtem Bityutskiy 	struct fsck_inode *fscki;
19901e51764aSArtem Bityutskiy 
19911e51764aSArtem Bityutskiy 	if (zbr->len < UBIFS_CH_SZ) {
1992235c362bSSheng Yong 		ubifs_err(c, "bad leaf length %d (LEB %d:%d)",
19931e51764aSArtem Bityutskiy 			  zbr->len, zbr->lnum, zbr->offs);
19941e51764aSArtem Bityutskiy 		return -EINVAL;
19951e51764aSArtem Bityutskiy 	}
19961e51764aSArtem Bityutskiy 
19971e51764aSArtem Bityutskiy 	node = kmalloc(zbr->len, GFP_NOFS);
19981e51764aSArtem Bityutskiy 	if (!node)
19991e51764aSArtem Bityutskiy 		return -ENOMEM;
20001e51764aSArtem Bityutskiy 
20011e51764aSArtem Bityutskiy 	err = ubifs_tnc_read_node(c, zbr, node);
20021e51764aSArtem Bityutskiy 	if (err) {
2003235c362bSSheng Yong 		ubifs_err(c, "cannot read leaf node at LEB %d:%d, error %d",
20041e51764aSArtem Bityutskiy 			  zbr->lnum, zbr->offs, err);
20051e51764aSArtem Bityutskiy 		goto out_free;
20061e51764aSArtem Bityutskiy 	}
20071e51764aSArtem Bityutskiy 
20081e51764aSArtem Bityutskiy 	/* If this is an inode node, add it to RB-tree of inodes */
20091e51764aSArtem Bityutskiy 	if (type == UBIFS_INO_KEY) {
20101e51764aSArtem Bityutskiy 		fscki = add_inode(c, priv, node);
20111e51764aSArtem Bityutskiy 		if (IS_ERR(fscki)) {
20121e51764aSArtem Bityutskiy 			err = PTR_ERR(fscki);
2013235c362bSSheng Yong 			ubifs_err(c, "error %d while adding inode node", err);
20141e51764aSArtem Bityutskiy 			goto out_dump;
20151e51764aSArtem Bityutskiy 		}
20161e51764aSArtem Bityutskiy 		goto out;
20171e51764aSArtem Bityutskiy 	}
20181e51764aSArtem Bityutskiy 
20191e51764aSArtem Bityutskiy 	if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
20201e51764aSArtem Bityutskiy 	    type != UBIFS_DATA_KEY) {
2021235c362bSSheng Yong 		ubifs_err(c, "unexpected node type %d at LEB %d:%d",
20221e51764aSArtem Bityutskiy 			  type, zbr->lnum, zbr->offs);
20231e51764aSArtem Bityutskiy 		err = -EINVAL;
20241e51764aSArtem Bityutskiy 		goto out_free;
20251e51764aSArtem Bityutskiy 	}
20261e51764aSArtem Bityutskiy 
20271e51764aSArtem Bityutskiy 	ch = node;
20281e51764aSArtem Bityutskiy 	if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
2029235c362bSSheng Yong 		ubifs_err(c, "too high sequence number, max. is %llu",
20301e51764aSArtem Bityutskiy 			  c->max_sqnum);
20311e51764aSArtem Bityutskiy 		err = -EINVAL;
20321e51764aSArtem Bityutskiy 		goto out_dump;
20331e51764aSArtem Bityutskiy 	}
20341e51764aSArtem Bityutskiy 
20351e51764aSArtem Bityutskiy 	if (type == UBIFS_DATA_KEY) {
20361e51764aSArtem Bityutskiy 		long long blk_offs;
20371e51764aSArtem Bityutskiy 		struct ubifs_data_node *dn = node;
20381e51764aSArtem Bityutskiy 
2039*6eb61d58SRichard Weinberger 		ubifs_assert(c, zbr->len >= UBIFS_DATA_NODE_SZ);
2040fb4325a3SArtem Bityutskiy 
20411e51764aSArtem Bityutskiy 		/*
20421e51764aSArtem Bityutskiy 		 * Search the inode node this data node belongs to and insert
20431e51764aSArtem Bityutskiy 		 * it to the RB-tree of inodes.
20441e51764aSArtem Bityutskiy 		 */
20451e51764aSArtem Bityutskiy 		inum = key_inum_flash(c, &dn->key);
20461e51764aSArtem Bityutskiy 		fscki = read_add_inode(c, priv, inum);
20471e51764aSArtem Bityutskiy 		if (IS_ERR(fscki)) {
20481e51764aSArtem Bityutskiy 			err = PTR_ERR(fscki);
2049235c362bSSheng Yong 			ubifs_err(c, "error %d while processing data node and trying to find inode node %lu",
2050e84461adSArtem Bityutskiy 				  err, (unsigned long)inum);
20511e51764aSArtem Bityutskiy 			goto out_dump;
20521e51764aSArtem Bityutskiy 		}
20531e51764aSArtem Bityutskiy 
20541e51764aSArtem Bityutskiy 		/* Make sure the data node is within inode size */
20551e51764aSArtem Bityutskiy 		blk_offs = key_block_flash(c, &dn->key);
20561e51764aSArtem Bityutskiy 		blk_offs <<= UBIFS_BLOCK_SHIFT;
20571e51764aSArtem Bityutskiy 		blk_offs += le32_to_cpu(dn->size);
20581e51764aSArtem Bityutskiy 		if (blk_offs > fscki->size) {
2059235c362bSSheng Yong 			ubifs_err(c, "data node at LEB %d:%d is not within inode size %lld",
206079fda517SArtem Bityutskiy 				  zbr->lnum, zbr->offs, fscki->size);
20611e51764aSArtem Bityutskiy 			err = -EINVAL;
20621e51764aSArtem Bityutskiy 			goto out_dump;
20631e51764aSArtem Bityutskiy 		}
20641e51764aSArtem Bityutskiy 	} else {
20651e51764aSArtem Bityutskiy 		int nlen;
20661e51764aSArtem Bityutskiy 		struct ubifs_dent_node *dent = node;
20671e51764aSArtem Bityutskiy 		struct fsck_inode *fscki1;
20681e51764aSArtem Bityutskiy 
2069*6eb61d58SRichard Weinberger 		ubifs_assert(c, zbr->len >= UBIFS_DENT_NODE_SZ);
2070fb4325a3SArtem Bityutskiy 
20711e51764aSArtem Bityutskiy 		err = ubifs_validate_entry(c, dent);
20721e51764aSArtem Bityutskiy 		if (err)
20731e51764aSArtem Bityutskiy 			goto out_dump;
20741e51764aSArtem Bityutskiy 
20751e51764aSArtem Bityutskiy 		/*
20761e51764aSArtem Bityutskiy 		 * Search the inode node this entry refers to and the parent
20771e51764aSArtem Bityutskiy 		 * inode node and insert them to the RB-tree of inodes.
20781e51764aSArtem Bityutskiy 		 */
20791e51764aSArtem Bityutskiy 		inum = le64_to_cpu(dent->inum);
20801e51764aSArtem Bityutskiy 		fscki = read_add_inode(c, priv, inum);
20811e51764aSArtem Bityutskiy 		if (IS_ERR(fscki)) {
20821e51764aSArtem Bityutskiy 			err = PTR_ERR(fscki);
2083235c362bSSheng Yong 			ubifs_err(c, "error %d while processing entry node and trying to find inode node %lu",
2084e84461adSArtem Bityutskiy 				  err, (unsigned long)inum);
20851e51764aSArtem Bityutskiy 			goto out_dump;
20861e51764aSArtem Bityutskiy 		}
20871e51764aSArtem Bityutskiy 
20881e51764aSArtem Bityutskiy 		/* Count how many direntries or xentries refers this inode */
20891e51764aSArtem Bityutskiy 		fscki->references += 1;
20901e51764aSArtem Bityutskiy 
20911e51764aSArtem Bityutskiy 		inum = key_inum_flash(c, &dent->key);
20921e51764aSArtem Bityutskiy 		fscki1 = read_add_inode(c, priv, inum);
20931e51764aSArtem Bityutskiy 		if (IS_ERR(fscki1)) {
2094b38882f5SRoel Kluin 			err = PTR_ERR(fscki1);
2095235c362bSSheng Yong 			ubifs_err(c, "error %d while processing entry node and trying to find parent inode node %lu",
2096e84461adSArtem Bityutskiy 				  err, (unsigned long)inum);
20971e51764aSArtem Bityutskiy 			goto out_dump;
20981e51764aSArtem Bityutskiy 		}
20991e51764aSArtem Bityutskiy 
21001e51764aSArtem Bityutskiy 		nlen = le16_to_cpu(dent->nlen);
21011e51764aSArtem Bityutskiy 		if (type == UBIFS_XENT_KEY) {
21021e51764aSArtem Bityutskiy 			fscki1->calc_xcnt += 1;
21031e51764aSArtem Bityutskiy 			fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
21041e51764aSArtem Bityutskiy 			fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
21051e51764aSArtem Bityutskiy 			fscki1->calc_xnms += nlen;
21061e51764aSArtem Bityutskiy 		} else {
21071e51764aSArtem Bityutskiy 			fscki1->calc_sz += CALC_DENT_SIZE(nlen);
21081e51764aSArtem Bityutskiy 			if (dent->type == UBIFS_ITYPE_DIR)
21091e51764aSArtem Bityutskiy 				fscki1->calc_cnt += 1;
21101e51764aSArtem Bityutskiy 		}
21111e51764aSArtem Bityutskiy 	}
21121e51764aSArtem Bityutskiy 
21131e51764aSArtem Bityutskiy out:
21141e51764aSArtem Bityutskiy 	kfree(node);
21151e51764aSArtem Bityutskiy 	return 0;
21161e51764aSArtem Bityutskiy 
21171e51764aSArtem Bityutskiy out_dump:
2118235c362bSSheng Yong 	ubifs_msg(c, "dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
2119edf6be24SArtem Bityutskiy 	ubifs_dump_node(c, node);
21201e51764aSArtem Bityutskiy out_free:
21211e51764aSArtem Bityutskiy 	kfree(node);
21221e51764aSArtem Bityutskiy 	return err;
21231e51764aSArtem Bityutskiy }
21241e51764aSArtem Bityutskiy 
21251e51764aSArtem Bityutskiy /**
21261e51764aSArtem Bityutskiy  * free_inodes - free RB-tree of inodes.
21271e51764aSArtem Bityutskiy  * @fsckd: FS checking information
21281e51764aSArtem Bityutskiy  */
21291e51764aSArtem Bityutskiy static void free_inodes(struct fsck_data *fsckd)
21301e51764aSArtem Bityutskiy {
2131bb25e49fSCody P Schafer 	struct fsck_inode *fscki, *n;
21321e51764aSArtem Bityutskiy 
2133bb25e49fSCody P Schafer 	rbtree_postorder_for_each_entry_safe(fscki, n, &fsckd->inodes, rb)
21341e51764aSArtem Bityutskiy 		kfree(fscki);
21351e51764aSArtem Bityutskiy }
21361e51764aSArtem Bityutskiy 
21371e51764aSArtem Bityutskiy /**
21381e51764aSArtem Bityutskiy  * check_inodes - checks all inodes.
21391e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
21401e51764aSArtem Bityutskiy  * @fsckd: FS checking information
21411e51764aSArtem Bityutskiy  *
21421e51764aSArtem Bityutskiy  * This is a helper function for 'dbg_check_filesystem()' which walks the
21431e51764aSArtem Bityutskiy  * RB-tree of inodes after the index scan has been finished, and checks that
21441e51764aSArtem Bityutskiy  * inode nlink, size, etc are correct. Returns zero if inodes are fine,
21451e51764aSArtem Bityutskiy  * %-EINVAL if not, and a negative error code in case of failure.
21461e51764aSArtem Bityutskiy  */
21471e51764aSArtem Bityutskiy static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
21481e51764aSArtem Bityutskiy {
21491e51764aSArtem Bityutskiy 	int n, err;
21501e51764aSArtem Bityutskiy 	union ubifs_key key;
21511e51764aSArtem Bityutskiy 	struct ubifs_znode *znode;
21521e51764aSArtem Bityutskiy 	struct ubifs_zbranch *zbr;
21531e51764aSArtem Bityutskiy 	struct ubifs_ino_node *ino;
21541e51764aSArtem Bityutskiy 	struct fsck_inode *fscki;
21551e51764aSArtem Bityutskiy 	struct rb_node *this = rb_first(&fsckd->inodes);
21561e51764aSArtem Bityutskiy 
21571e51764aSArtem Bityutskiy 	while (this) {
21581e51764aSArtem Bityutskiy 		fscki = rb_entry(this, struct fsck_inode, rb);
21591e51764aSArtem Bityutskiy 		this = rb_next(this);
21601e51764aSArtem Bityutskiy 
21611e51764aSArtem Bityutskiy 		if (S_ISDIR(fscki->mode)) {
21621e51764aSArtem Bityutskiy 			/*
21631e51764aSArtem Bityutskiy 			 * Directories have to have exactly one reference (they
21641e51764aSArtem Bityutskiy 			 * cannot have hardlinks), although root inode is an
21651e51764aSArtem Bityutskiy 			 * exception.
21661e51764aSArtem Bityutskiy 			 */
21671e51764aSArtem Bityutskiy 			if (fscki->inum != UBIFS_ROOT_INO &&
21681e51764aSArtem Bityutskiy 			    fscki->references != 1) {
2169235c362bSSheng Yong 				ubifs_err(c, "directory inode %lu has %d direntries which refer it, but should be 1",
2170e84461adSArtem Bityutskiy 					  (unsigned long)fscki->inum,
21711e51764aSArtem Bityutskiy 					  fscki->references);
21721e51764aSArtem Bityutskiy 				goto out_dump;
21731e51764aSArtem Bityutskiy 			}
21741e51764aSArtem Bityutskiy 			if (fscki->inum == UBIFS_ROOT_INO &&
21751e51764aSArtem Bityutskiy 			    fscki->references != 0) {
2176235c362bSSheng Yong 				ubifs_err(c, "root inode %lu has non-zero (%d) direntries which refer it",
2177e84461adSArtem Bityutskiy 					  (unsigned long)fscki->inum,
2178e84461adSArtem Bityutskiy 					  fscki->references);
21791e51764aSArtem Bityutskiy 				goto out_dump;
21801e51764aSArtem Bityutskiy 			}
21811e51764aSArtem Bityutskiy 			if (fscki->calc_sz != fscki->size) {
2182235c362bSSheng Yong 				ubifs_err(c, "directory inode %lu size is %lld, but calculated size is %lld",
2183e84461adSArtem Bityutskiy 					  (unsigned long)fscki->inum,
2184e84461adSArtem Bityutskiy 					  fscki->size, fscki->calc_sz);
21851e51764aSArtem Bityutskiy 				goto out_dump;
21861e51764aSArtem Bityutskiy 			}
21871e51764aSArtem Bityutskiy 			if (fscki->calc_cnt != fscki->nlink) {
2188235c362bSSheng Yong 				ubifs_err(c, "directory inode %lu nlink is %d, but calculated nlink is %d",
2189e84461adSArtem Bityutskiy 					  (unsigned long)fscki->inum,
2190e84461adSArtem Bityutskiy 					  fscki->nlink, fscki->calc_cnt);
21911e51764aSArtem Bityutskiy 				goto out_dump;
21921e51764aSArtem Bityutskiy 			}
21931e51764aSArtem Bityutskiy 		} else {
21941e51764aSArtem Bityutskiy 			if (fscki->references != fscki->nlink) {
2195235c362bSSheng Yong 				ubifs_err(c, "inode %lu nlink is %d, but calculated nlink is %d",
2196e84461adSArtem Bityutskiy 					  (unsigned long)fscki->inum,
21971e51764aSArtem Bityutskiy 					  fscki->nlink, fscki->references);
21981e51764aSArtem Bityutskiy 				goto out_dump;
21991e51764aSArtem Bityutskiy 			}
22001e51764aSArtem Bityutskiy 		}
22011e51764aSArtem Bityutskiy 		if (fscki->xattr_sz != fscki->calc_xsz) {
2202235c362bSSheng Yong 			ubifs_err(c, "inode %lu has xattr size %u, but calculated size is %lld",
2203e84461adSArtem Bityutskiy 				  (unsigned long)fscki->inum, fscki->xattr_sz,
22041e51764aSArtem Bityutskiy 				  fscki->calc_xsz);
22051e51764aSArtem Bityutskiy 			goto out_dump;
22061e51764aSArtem Bityutskiy 		}
22071e51764aSArtem Bityutskiy 		if (fscki->xattr_cnt != fscki->calc_xcnt) {
2208235c362bSSheng Yong 			ubifs_err(c, "inode %lu has %u xattrs, but calculated count is %lld",
2209e84461adSArtem Bityutskiy 				  (unsigned long)fscki->inum,
22101e51764aSArtem Bityutskiy 				  fscki->xattr_cnt, fscki->calc_xcnt);
22111e51764aSArtem Bityutskiy 			goto out_dump;
22121e51764aSArtem Bityutskiy 		}
22131e51764aSArtem Bityutskiy 		if (fscki->xattr_nms != fscki->calc_xnms) {
2214235c362bSSheng Yong 			ubifs_err(c, "inode %lu has xattr names' size %u, but calculated names' size is %lld",
2215e84461adSArtem Bityutskiy 				  (unsigned long)fscki->inum, fscki->xattr_nms,
22161e51764aSArtem Bityutskiy 				  fscki->calc_xnms);
22171e51764aSArtem Bityutskiy 			goto out_dump;
22181e51764aSArtem Bityutskiy 		}
22191e51764aSArtem Bityutskiy 	}
22201e51764aSArtem Bityutskiy 
22211e51764aSArtem Bityutskiy 	return 0;
22221e51764aSArtem Bityutskiy 
22231e51764aSArtem Bityutskiy out_dump:
22241e51764aSArtem Bityutskiy 	/* Read the bad inode and dump it */
22251e51764aSArtem Bityutskiy 	ino_key_init(c, &key, fscki->inum);
22261e51764aSArtem Bityutskiy 	err = ubifs_lookup_level0(c, &key, &znode, &n);
22271e51764aSArtem Bityutskiy 	if (!err) {
2228235c362bSSheng Yong 		ubifs_err(c, "inode %lu not found in index",
2229e84461adSArtem Bityutskiy 			  (unsigned long)fscki->inum);
22301e51764aSArtem Bityutskiy 		return -ENOENT;
22311e51764aSArtem Bityutskiy 	} else if (err < 0) {
2232235c362bSSheng Yong 		ubifs_err(c, "error %d while looking up inode %lu",
2233e84461adSArtem Bityutskiy 			  err, (unsigned long)fscki->inum);
22341e51764aSArtem Bityutskiy 		return err;
22351e51764aSArtem Bityutskiy 	}
22361e51764aSArtem Bityutskiy 
22371e51764aSArtem Bityutskiy 	zbr = &znode->zbranch[n];
22381e51764aSArtem Bityutskiy 	ino = kmalloc(zbr->len, GFP_NOFS);
22391e51764aSArtem Bityutskiy 	if (!ino)
22401e51764aSArtem Bityutskiy 		return -ENOMEM;
22411e51764aSArtem Bityutskiy 
22421e51764aSArtem Bityutskiy 	err = ubifs_tnc_read_node(c, zbr, ino);
22431e51764aSArtem Bityutskiy 	if (err) {
2244235c362bSSheng Yong 		ubifs_err(c, "cannot read inode node at LEB %d:%d, error %d",
22451e51764aSArtem Bityutskiy 			  zbr->lnum, zbr->offs, err);
22461e51764aSArtem Bityutskiy 		kfree(ino);
22471e51764aSArtem Bityutskiy 		return err;
22481e51764aSArtem Bityutskiy 	}
22491e51764aSArtem Bityutskiy 
2250235c362bSSheng Yong 	ubifs_msg(c, "dump of the inode %lu sitting in LEB %d:%d",
2251e84461adSArtem Bityutskiy 		  (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
2252edf6be24SArtem Bityutskiy 	ubifs_dump_node(c, ino);
22531e51764aSArtem Bityutskiy 	kfree(ino);
22541e51764aSArtem Bityutskiy 	return -EINVAL;
22551e51764aSArtem Bityutskiy }
22561e51764aSArtem Bityutskiy 
22571e51764aSArtem Bityutskiy /**
22581e51764aSArtem Bityutskiy  * dbg_check_filesystem - check the file-system.
22591e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
22601e51764aSArtem Bityutskiy  *
22611e51764aSArtem Bityutskiy  * This function checks the file system, namely:
22621e51764aSArtem Bityutskiy  * o makes sure that all leaf nodes exist and their CRCs are correct;
22631e51764aSArtem Bityutskiy  * o makes sure inode nlink, size, xattr size/count are correct (for all
22641e51764aSArtem Bityutskiy  *   inodes).
22651e51764aSArtem Bityutskiy  *
22661e51764aSArtem Bityutskiy  * The function reads whole indexing tree and all nodes, so it is pretty
22671e51764aSArtem Bityutskiy  * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
22681e51764aSArtem Bityutskiy  * not, and a negative error code in case of failure.
22691e51764aSArtem Bityutskiy  */
22701e51764aSArtem Bityutskiy int dbg_check_filesystem(struct ubifs_info *c)
22711e51764aSArtem Bityutskiy {
22721e51764aSArtem Bityutskiy 	int err;
22731e51764aSArtem Bityutskiy 	struct fsck_data fsckd;
22741e51764aSArtem Bityutskiy 
22752b1844a8SArtem Bityutskiy 	if (!dbg_is_chk_fs(c))
22761e51764aSArtem Bityutskiy 		return 0;
22771e51764aSArtem Bityutskiy 
22781e51764aSArtem Bityutskiy 	fsckd.inodes = RB_ROOT;
22791e51764aSArtem Bityutskiy 	err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
22801e51764aSArtem Bityutskiy 	if (err)
22811e51764aSArtem Bityutskiy 		goto out_free;
22821e51764aSArtem Bityutskiy 
22831e51764aSArtem Bityutskiy 	err = check_inodes(c, &fsckd);
22841e51764aSArtem Bityutskiy 	if (err)
22851e51764aSArtem Bityutskiy 		goto out_free;
22861e51764aSArtem Bityutskiy 
22871e51764aSArtem Bityutskiy 	free_inodes(&fsckd);
22881e51764aSArtem Bityutskiy 	return 0;
22891e51764aSArtem Bityutskiy 
22901e51764aSArtem Bityutskiy out_free:
2291235c362bSSheng Yong 	ubifs_err(c, "file-system check failed with error %d", err);
22921e51764aSArtem Bityutskiy 	dump_stack();
22931e51764aSArtem Bityutskiy 	free_inodes(&fsckd);
22941e51764aSArtem Bityutskiy 	return err;
22951e51764aSArtem Bityutskiy }
22961e51764aSArtem Bityutskiy 
22973bb66b47SArtem Bityutskiy /**
22983bb66b47SArtem Bityutskiy  * dbg_check_data_nodes_order - check that list of data nodes is sorted.
22993bb66b47SArtem Bityutskiy  * @c: UBIFS file-system description object
23003bb66b47SArtem Bityutskiy  * @head: the list of nodes ('struct ubifs_scan_node' objects)
23013bb66b47SArtem Bityutskiy  *
23023bb66b47SArtem Bityutskiy  * This function returns zero if the list of data nodes is sorted correctly,
23033bb66b47SArtem Bityutskiy  * and %-EINVAL if not.
23043bb66b47SArtem Bityutskiy  */
23053bb66b47SArtem Bityutskiy int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head)
23063bb66b47SArtem Bityutskiy {
23073bb66b47SArtem Bityutskiy 	struct list_head *cur;
23083bb66b47SArtem Bityutskiy 	struct ubifs_scan_node *sa, *sb;
23093bb66b47SArtem Bityutskiy 
23102b1844a8SArtem Bityutskiy 	if (!dbg_is_chk_gen(c))
23113bb66b47SArtem Bityutskiy 		return 0;
23123bb66b47SArtem Bityutskiy 
23133bb66b47SArtem Bityutskiy 	for (cur = head->next; cur->next != head; cur = cur->next) {
23143bb66b47SArtem Bityutskiy 		ino_t inuma, inumb;
23153bb66b47SArtem Bityutskiy 		uint32_t blka, blkb;
23163bb66b47SArtem Bityutskiy 
23173bb66b47SArtem Bityutskiy 		cond_resched();
23183bb66b47SArtem Bityutskiy 		sa = container_of(cur, struct ubifs_scan_node, list);
23193bb66b47SArtem Bityutskiy 		sb = container_of(cur->next, struct ubifs_scan_node, list);
23203bb66b47SArtem Bityutskiy 
23213bb66b47SArtem Bityutskiy 		if (sa->type != UBIFS_DATA_NODE) {
2322235c362bSSheng Yong 			ubifs_err(c, "bad node type %d", sa->type);
2323edf6be24SArtem Bityutskiy 			ubifs_dump_node(c, sa->node);
23243bb66b47SArtem Bityutskiy 			return -EINVAL;
23253bb66b47SArtem Bityutskiy 		}
23263bb66b47SArtem Bityutskiy 		if (sb->type != UBIFS_DATA_NODE) {
2327235c362bSSheng Yong 			ubifs_err(c, "bad node type %d", sb->type);
2328edf6be24SArtem Bityutskiy 			ubifs_dump_node(c, sb->node);
23293bb66b47SArtem Bityutskiy 			return -EINVAL;
23303bb66b47SArtem Bityutskiy 		}
23313bb66b47SArtem Bityutskiy 
23323bb66b47SArtem Bityutskiy 		inuma = key_inum(c, &sa->key);
23333bb66b47SArtem Bityutskiy 		inumb = key_inum(c, &sb->key);
23343bb66b47SArtem Bityutskiy 
23353bb66b47SArtem Bityutskiy 		if (inuma < inumb)
23363bb66b47SArtem Bityutskiy 			continue;
23373bb66b47SArtem Bityutskiy 		if (inuma > inumb) {
2338235c362bSSheng Yong 			ubifs_err(c, "larger inum %lu goes before inum %lu",
23393bb66b47SArtem Bityutskiy 				  (unsigned long)inuma, (unsigned long)inumb);
23403bb66b47SArtem Bityutskiy 			goto error_dump;
23413bb66b47SArtem Bityutskiy 		}
23423bb66b47SArtem Bityutskiy 
23433bb66b47SArtem Bityutskiy 		blka = key_block(c, &sa->key);
23443bb66b47SArtem Bityutskiy 		blkb = key_block(c, &sb->key);
23453bb66b47SArtem Bityutskiy 
23463bb66b47SArtem Bityutskiy 		if (blka > blkb) {
2347235c362bSSheng Yong 			ubifs_err(c, "larger block %u goes before %u", blka, blkb);
23483bb66b47SArtem Bityutskiy 			goto error_dump;
23493bb66b47SArtem Bityutskiy 		}
23503bb66b47SArtem Bityutskiy 		if (blka == blkb) {
2351235c362bSSheng Yong 			ubifs_err(c, "two data nodes for the same block");
23523bb66b47SArtem Bityutskiy 			goto error_dump;
23533bb66b47SArtem Bityutskiy 		}
23543bb66b47SArtem Bityutskiy 	}
23553bb66b47SArtem Bityutskiy 
23563bb66b47SArtem Bityutskiy 	return 0;
23573bb66b47SArtem Bityutskiy 
23583bb66b47SArtem Bityutskiy error_dump:
2359edf6be24SArtem Bityutskiy 	ubifs_dump_node(c, sa->node);
2360edf6be24SArtem Bityutskiy 	ubifs_dump_node(c, sb->node);
23613bb66b47SArtem Bityutskiy 	return -EINVAL;
23623bb66b47SArtem Bityutskiy }
23633bb66b47SArtem Bityutskiy 
23643bb66b47SArtem Bityutskiy /**
23653bb66b47SArtem Bityutskiy  * dbg_check_nondata_nodes_order - check that list of data nodes is sorted.
23663bb66b47SArtem Bityutskiy  * @c: UBIFS file-system description object
23673bb66b47SArtem Bityutskiy  * @head: the list of nodes ('struct ubifs_scan_node' objects)
23683bb66b47SArtem Bityutskiy  *
23693bb66b47SArtem Bityutskiy  * This function returns zero if the list of non-data nodes is sorted correctly,
23703bb66b47SArtem Bityutskiy  * and %-EINVAL if not.
23713bb66b47SArtem Bityutskiy  */
23723bb66b47SArtem Bityutskiy int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head)
23733bb66b47SArtem Bityutskiy {
23743bb66b47SArtem Bityutskiy 	struct list_head *cur;
23753bb66b47SArtem Bityutskiy 	struct ubifs_scan_node *sa, *sb;
23763bb66b47SArtem Bityutskiy 
23772b1844a8SArtem Bityutskiy 	if (!dbg_is_chk_gen(c))
23783bb66b47SArtem Bityutskiy 		return 0;
23793bb66b47SArtem Bityutskiy 
23803bb66b47SArtem Bityutskiy 	for (cur = head->next; cur->next != head; cur = cur->next) {
23813bb66b47SArtem Bityutskiy 		ino_t inuma, inumb;
23823bb66b47SArtem Bityutskiy 		uint32_t hasha, hashb;
23833bb66b47SArtem Bityutskiy 
23843bb66b47SArtem Bityutskiy 		cond_resched();
23853bb66b47SArtem Bityutskiy 		sa = container_of(cur, struct ubifs_scan_node, list);
23863bb66b47SArtem Bityutskiy 		sb = container_of(cur->next, struct ubifs_scan_node, list);
23873bb66b47SArtem Bityutskiy 
23883bb66b47SArtem Bityutskiy 		if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
23893bb66b47SArtem Bityutskiy 		    sa->type != UBIFS_XENT_NODE) {
2390235c362bSSheng Yong 			ubifs_err(c, "bad node type %d", sa->type);
2391edf6be24SArtem Bityutskiy 			ubifs_dump_node(c, sa->node);
23923bb66b47SArtem Bityutskiy 			return -EINVAL;
23933bb66b47SArtem Bityutskiy 		}
23946a258f7dSColin Ian King 		if (sb->type != UBIFS_INO_NODE && sb->type != UBIFS_DENT_NODE &&
23956a258f7dSColin Ian King 		    sb->type != UBIFS_XENT_NODE) {
2396235c362bSSheng Yong 			ubifs_err(c, "bad node type %d", sb->type);
2397edf6be24SArtem Bityutskiy 			ubifs_dump_node(c, sb->node);
23983bb66b47SArtem Bityutskiy 			return -EINVAL;
23993bb66b47SArtem Bityutskiy 		}
24003bb66b47SArtem Bityutskiy 
24013bb66b47SArtem Bityutskiy 		if (sa->type != UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2402235c362bSSheng Yong 			ubifs_err(c, "non-inode node goes before inode node");
24033bb66b47SArtem Bityutskiy 			goto error_dump;
24043bb66b47SArtem Bityutskiy 		}
24053bb66b47SArtem Bityutskiy 
24063bb66b47SArtem Bityutskiy 		if (sa->type == UBIFS_INO_NODE && sb->type != UBIFS_INO_NODE)
24073bb66b47SArtem Bityutskiy 			continue;
24083bb66b47SArtem Bityutskiy 
24093bb66b47SArtem Bityutskiy 		if (sa->type == UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
24103bb66b47SArtem Bityutskiy 			/* Inode nodes are sorted in descending size order */
24113bb66b47SArtem Bityutskiy 			if (sa->len < sb->len) {
2412235c362bSSheng Yong 				ubifs_err(c, "smaller inode node goes first");
24133bb66b47SArtem Bityutskiy 				goto error_dump;
24143bb66b47SArtem Bityutskiy 			}
24153bb66b47SArtem Bityutskiy 			continue;
24163bb66b47SArtem Bityutskiy 		}
24173bb66b47SArtem Bityutskiy 
24183bb66b47SArtem Bityutskiy 		/*
24193bb66b47SArtem Bityutskiy 		 * This is either a dentry or xentry, which should be sorted in
24203bb66b47SArtem Bityutskiy 		 * ascending (parent ino, hash) order.
24213bb66b47SArtem Bityutskiy 		 */
24223bb66b47SArtem Bityutskiy 		inuma = key_inum(c, &sa->key);
24233bb66b47SArtem Bityutskiy 		inumb = key_inum(c, &sb->key);
24243bb66b47SArtem Bityutskiy 
24253bb66b47SArtem Bityutskiy 		if (inuma < inumb)
24263bb66b47SArtem Bityutskiy 			continue;
24273bb66b47SArtem Bityutskiy 		if (inuma > inumb) {
2428235c362bSSheng Yong 			ubifs_err(c, "larger inum %lu goes before inum %lu",
24293bb66b47SArtem Bityutskiy 				  (unsigned long)inuma, (unsigned long)inumb);
24303bb66b47SArtem Bityutskiy 			goto error_dump;
24313bb66b47SArtem Bityutskiy 		}
24323bb66b47SArtem Bityutskiy 
24333bb66b47SArtem Bityutskiy 		hasha = key_block(c, &sa->key);
24343bb66b47SArtem Bityutskiy 		hashb = key_block(c, &sb->key);
24353bb66b47SArtem Bityutskiy 
24363bb66b47SArtem Bityutskiy 		if (hasha > hashb) {
2437235c362bSSheng Yong 			ubifs_err(c, "larger hash %u goes before %u",
2438c4361570SArtem Bityutskiy 				  hasha, hashb);
24393bb66b47SArtem Bityutskiy 			goto error_dump;
24403bb66b47SArtem Bityutskiy 		}
24413bb66b47SArtem Bityutskiy 	}
24423bb66b47SArtem Bityutskiy 
24433bb66b47SArtem Bityutskiy 	return 0;
24443bb66b47SArtem Bityutskiy 
24453bb66b47SArtem Bityutskiy error_dump:
2446235c362bSSheng Yong 	ubifs_msg(c, "dumping first node");
2447edf6be24SArtem Bityutskiy 	ubifs_dump_node(c, sa->node);
2448235c362bSSheng Yong 	ubifs_msg(c, "dumping second node");
2449edf6be24SArtem Bityutskiy 	ubifs_dump_node(c, sb->node);
24503bb66b47SArtem Bityutskiy 	return -EINVAL;
24513bb66b47SArtem Bityutskiy 	return 0;
24523bb66b47SArtem Bityutskiy }
24533bb66b47SArtem Bityutskiy 
2454a7fa94a9SArtem Bityutskiy static inline int chance(unsigned int n, unsigned int out_of)
24551e51764aSArtem Bityutskiy {
24563d251a5bSAkinobu Mita 	return !!((prandom_u32() % out_of) + 1 <= n);
2457a7fa94a9SArtem Bityutskiy 
24581e51764aSArtem Bityutskiy }
24591e51764aSArtem Bityutskiy 
2460d27462a5SArtem Bityutskiy static int power_cut_emulated(struct ubifs_info *c, int lnum, int write)
24611e51764aSArtem Bityutskiy {
2462f57cb188SArtem Bityutskiy 	struct ubifs_debug_info *d = c->dbg;
24631e51764aSArtem Bityutskiy 
2464*6eb61d58SRichard Weinberger 	ubifs_assert(c, dbg_is_tst_rcvry(c));
24651e51764aSArtem Bityutskiy 
2466d27462a5SArtem Bityutskiy 	if (!d->pc_cnt) {
2467d27462a5SArtem Bityutskiy 		/* First call - decide delay to the power cut */
24681e51764aSArtem Bityutskiy 		if (chance(1, 2)) {
2469a7fa94a9SArtem Bityutskiy 			unsigned long delay;
24701e51764aSArtem Bityutskiy 
24711e51764aSArtem Bityutskiy 			if (chance(1, 2)) {
2472d27462a5SArtem Bityutskiy 				d->pc_delay = 1;
2473443b39cdSRichard Weinberger 				/* Fail within 1 minute */
24743d251a5bSAkinobu Mita 				delay = prandom_u32() % 60000;
2475a7fa94a9SArtem Bityutskiy 				d->pc_timeout = jiffies;
2476a7fa94a9SArtem Bityutskiy 				d->pc_timeout += msecs_to_jiffies(delay);
2477235c362bSSheng Yong 				ubifs_warn(c, "failing after %lums", delay);
24781e51764aSArtem Bityutskiy 			} else {
2479d27462a5SArtem Bityutskiy 				d->pc_delay = 2;
24803d251a5bSAkinobu Mita 				delay = prandom_u32() % 10000;
2481a7fa94a9SArtem Bityutskiy 				/* Fail within 10000 operations */
2482d27462a5SArtem Bityutskiy 				d->pc_cnt_max = delay;
2483235c362bSSheng Yong 				ubifs_warn(c, "failing after %lu calls", delay);
24841e51764aSArtem Bityutskiy 			}
24851e51764aSArtem Bityutskiy 		}
2486a7fa94a9SArtem Bityutskiy 
2487d27462a5SArtem Bityutskiy 		d->pc_cnt += 1;
24881e51764aSArtem Bityutskiy 	}
2489a7fa94a9SArtem Bityutskiy 
24901e51764aSArtem Bityutskiy 	/* Determine if failure delay has expired */
2491a7fa94a9SArtem Bityutskiy 	if (d->pc_delay == 1 && time_before(jiffies, d->pc_timeout))
24921e51764aSArtem Bityutskiy 			return 0;
2493a7fa94a9SArtem Bityutskiy 	if (d->pc_delay == 2 && d->pc_cnt++ < d->pc_cnt_max)
24941e51764aSArtem Bityutskiy 			return 0;
2495a7fa94a9SArtem Bityutskiy 
24961e51764aSArtem Bityutskiy 	if (lnum == UBIFS_SB_LNUM) {
2497a7fa94a9SArtem Bityutskiy 		if (write && chance(1, 2))
24981e51764aSArtem Bityutskiy 			return 0;
2499a7fa94a9SArtem Bityutskiy 		if (chance(19, 20))
25001e51764aSArtem Bityutskiy 			return 0;
2501235c362bSSheng Yong 		ubifs_warn(c, "failing in super block LEB %d", lnum);
25021e51764aSArtem Bityutskiy 	} else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
25031e51764aSArtem Bityutskiy 		if (chance(19, 20))
25041e51764aSArtem Bityutskiy 			return 0;
2505235c362bSSheng Yong 		ubifs_warn(c, "failing in master LEB %d", lnum);
25061e51764aSArtem Bityutskiy 	} else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2507a7fa94a9SArtem Bityutskiy 		if (write && chance(99, 100))
25081e51764aSArtem Bityutskiy 			return 0;
2509a7fa94a9SArtem Bityutskiy 		if (chance(399, 400))
25101e51764aSArtem Bityutskiy 			return 0;
2511235c362bSSheng Yong 		ubifs_warn(c, "failing in log LEB %d", lnum);
25121e51764aSArtem Bityutskiy 	} else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2513a7fa94a9SArtem Bityutskiy 		if (write && chance(7, 8))
25141e51764aSArtem Bityutskiy 			return 0;
2515a7fa94a9SArtem Bityutskiy 		if (chance(19, 20))
25161e51764aSArtem Bityutskiy 			return 0;
2517235c362bSSheng Yong 		ubifs_warn(c, "failing in LPT LEB %d", lnum);
25181e51764aSArtem Bityutskiy 	} else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2519a7fa94a9SArtem Bityutskiy 		if (write && chance(1, 2))
25201e51764aSArtem Bityutskiy 			return 0;
2521a7fa94a9SArtem Bityutskiy 		if (chance(9, 10))
25221e51764aSArtem Bityutskiy 			return 0;
2523235c362bSSheng Yong 		ubifs_warn(c, "failing in orphan LEB %d", lnum);
25241e51764aSArtem Bityutskiy 	} else if (lnum == c->ihead_lnum) {
25251e51764aSArtem Bityutskiy 		if (chance(99, 100))
25261e51764aSArtem Bityutskiy 			return 0;
2527235c362bSSheng Yong 		ubifs_warn(c, "failing in index head LEB %d", lnum);
25281e51764aSArtem Bityutskiy 	} else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
25291e51764aSArtem Bityutskiy 		if (chance(9, 10))
25301e51764aSArtem Bityutskiy 			return 0;
2531235c362bSSheng Yong 		ubifs_warn(c, "failing in GC head LEB %d", lnum);
25321e51764aSArtem Bityutskiy 	} else if (write && !RB_EMPTY_ROOT(&c->buds) &&
25331e51764aSArtem Bityutskiy 		   !ubifs_search_bud(c, lnum)) {
25341e51764aSArtem Bityutskiy 		if (chance(19, 20))
25351e51764aSArtem Bityutskiy 			return 0;
2536235c362bSSheng Yong 		ubifs_warn(c, "failing in non-bud LEB %d", lnum);
25371e51764aSArtem Bityutskiy 	} else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
25381e51764aSArtem Bityutskiy 		   c->cmt_state == COMMIT_RUNNING_REQUIRED) {
25391e51764aSArtem Bityutskiy 		if (chance(999, 1000))
25401e51764aSArtem Bityutskiy 			return 0;
2541235c362bSSheng Yong 		ubifs_warn(c, "failing in bud LEB %d commit running", lnum);
25421e51764aSArtem Bityutskiy 	} else {
25431e51764aSArtem Bityutskiy 		if (chance(9999, 10000))
25441e51764aSArtem Bityutskiy 			return 0;
2545235c362bSSheng Yong 		ubifs_warn(c, "failing in bud LEB %d commit not running", lnum);
25461e51764aSArtem Bityutskiy 	}
254724a4f800SArtem Bityutskiy 
2548d27462a5SArtem Bityutskiy 	d->pc_happened = 1;
2549235c362bSSheng Yong 	ubifs_warn(c, "========== Power cut emulated ==========");
25501e51764aSArtem Bityutskiy 	dump_stack();
25511e51764aSArtem Bityutskiy 	return 1;
25521e51764aSArtem Bityutskiy }
25531e51764aSArtem Bityutskiy 
25548089ed79SArtem Bityutskiy static int corrupt_data(const struct ubifs_info *c, const void *buf,
25558089ed79SArtem Bityutskiy 			unsigned int len)
25561e51764aSArtem Bityutskiy {
2557cdd9fa8dSAkinobu Mita 	unsigned int from, to, ffs = chance(1, 2);
25581e51764aSArtem Bityutskiy 	unsigned char *p = (void *)buf;
25591e51764aSArtem Bityutskiy 
256058a4e237SMats Kärrman 	from = prandom_u32() % len;
256158a4e237SMats Kärrman 	/* Corruption span max to end of write unit */
256258a4e237SMats Kärrman 	to = min(len, ALIGN(from + 1, c->max_write_size));
2563a7fa94a9SArtem Bityutskiy 
2564235c362bSSheng Yong 	ubifs_warn(c, "filled bytes %u-%u with %s", from, to - 1,
2565a7fa94a9SArtem Bityutskiy 		   ffs ? "0xFFs" : "random data");
2566a7fa94a9SArtem Bityutskiy 
2567a7fa94a9SArtem Bityutskiy 	if (ffs)
2568cdd9fa8dSAkinobu Mita 		memset(p + from, 0xFF, to - from);
2569a7fa94a9SArtem Bityutskiy 	else
2570cdd9fa8dSAkinobu Mita 		prandom_bytes(p + from, to - from);
25718089ed79SArtem Bityutskiy 
25728089ed79SArtem Bityutskiy 	return to;
25731e51764aSArtem Bityutskiy }
25741e51764aSArtem Bityutskiy 
2575f57cb188SArtem Bityutskiy int dbg_leb_write(struct ubifs_info *c, int lnum, const void *buf,
2576b36a261eSRichard Weinberger 		  int offs, int len)
25771e51764aSArtem Bityutskiy {
257816dfd804SAdrian Hunter 	int err, failing;
25791e51764aSArtem Bityutskiy 
25808f6983abSshengyong 	if (dbg_is_power_cut(c))
25811a29af8bSArtem Bityutskiy 		return -EROFS;
2582d27462a5SArtem Bityutskiy 
2583d27462a5SArtem Bityutskiy 	failing = power_cut_emulated(c, lnum, 1);
2584c23e9b75SMats Kärrman 	if (failing) {
25858089ed79SArtem Bityutskiy 		len = corrupt_data(c, buf, len);
2586235c362bSSheng Yong 		ubifs_warn(c, "actually write %d bytes to LEB %d:%d (the buffer was corrupted)",
25878089ed79SArtem Bityutskiy 			   len, lnum, offs);
2588c23e9b75SMats Kärrman 	}
2589b36a261eSRichard Weinberger 	err = ubi_leb_write(c->ubi, lnum, buf, offs, len);
25901e51764aSArtem Bityutskiy 	if (err)
25911e51764aSArtem Bityutskiy 		return err;
259216dfd804SAdrian Hunter 	if (failing)
25931a29af8bSArtem Bityutskiy 		return -EROFS;
25941e51764aSArtem Bityutskiy 	return 0;
25951e51764aSArtem Bityutskiy }
25961e51764aSArtem Bityutskiy 
2597f57cb188SArtem Bityutskiy int dbg_leb_change(struct ubifs_info *c, int lnum, const void *buf,
2598b36a261eSRichard Weinberger 		   int len)
25991e51764aSArtem Bityutskiy {
26001e51764aSArtem Bityutskiy 	int err;
26011e51764aSArtem Bityutskiy 
26028f6983abSshengyong 	if (dbg_is_power_cut(c))
2603d27462a5SArtem Bityutskiy 		return -EROFS;
2604d27462a5SArtem Bityutskiy 	if (power_cut_emulated(c, lnum, 1))
26051a29af8bSArtem Bityutskiy 		return -EROFS;
2606b36a261eSRichard Weinberger 	err = ubi_leb_change(c->ubi, lnum, buf, len);
26071e51764aSArtem Bityutskiy 	if (err)
26081e51764aSArtem Bityutskiy 		return err;
2609d27462a5SArtem Bityutskiy 	if (power_cut_emulated(c, lnum, 1))
26101a29af8bSArtem Bityutskiy 		return -EROFS;
26111e51764aSArtem Bityutskiy 	return 0;
26121e51764aSArtem Bityutskiy }
26131e51764aSArtem Bityutskiy 
2614f57cb188SArtem Bityutskiy int dbg_leb_unmap(struct ubifs_info *c, int lnum)
26151e51764aSArtem Bityutskiy {
26161e51764aSArtem Bityutskiy 	int err;
26171e51764aSArtem Bityutskiy 
26188f6983abSshengyong 	if (dbg_is_power_cut(c))
2619d27462a5SArtem Bityutskiy 		return -EROFS;
2620d27462a5SArtem Bityutskiy 	if (power_cut_emulated(c, lnum, 0))
26211a29af8bSArtem Bityutskiy 		return -EROFS;
2622f57cb188SArtem Bityutskiy 	err = ubi_leb_unmap(c->ubi, lnum);
26231e51764aSArtem Bityutskiy 	if (err)
26241e51764aSArtem Bityutskiy 		return err;
2625d27462a5SArtem Bityutskiy 	if (power_cut_emulated(c, lnum, 0))
26261a29af8bSArtem Bityutskiy 		return -EROFS;
26271e51764aSArtem Bityutskiy 	return 0;
26281e51764aSArtem Bityutskiy }
26291e51764aSArtem Bityutskiy 
2630b36a261eSRichard Weinberger int dbg_leb_map(struct ubifs_info *c, int lnum)
26311e51764aSArtem Bityutskiy {
26321e51764aSArtem Bityutskiy 	int err;
26331e51764aSArtem Bityutskiy 
26348f6983abSshengyong 	if (dbg_is_power_cut(c))
2635d27462a5SArtem Bityutskiy 		return -EROFS;
2636d27462a5SArtem Bityutskiy 	if (power_cut_emulated(c, lnum, 0))
26371a29af8bSArtem Bityutskiy 		return -EROFS;
2638b36a261eSRichard Weinberger 	err = ubi_leb_map(c->ubi, lnum);
26391e51764aSArtem Bityutskiy 	if (err)
26401e51764aSArtem Bityutskiy 		return err;
2641d27462a5SArtem Bityutskiy 	if (power_cut_emulated(c, lnum, 0))
26421a29af8bSArtem Bityutskiy 		return -EROFS;
26431e51764aSArtem Bityutskiy 	return 0;
26441e51764aSArtem Bityutskiy }
26451e51764aSArtem Bityutskiy 
2646552ff317SArtem Bityutskiy /*
2647552ff317SArtem Bityutskiy  * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
2648552ff317SArtem Bityutskiy  * contain the stuff specific to particular file-system mounts.
2649552ff317SArtem Bityutskiy  */
265084abf972SArtem Bityutskiy static struct dentry *dfs_rootdir;
2651552ff317SArtem Bityutskiy 
26527dae997dSArtem Bityutskiy static int dfs_file_open(struct inode *inode, struct file *file)
2653552ff317SArtem Bityutskiy {
2654552ff317SArtem Bityutskiy 	file->private_data = inode->i_private;
26551bbfc848SArtem Bityutskiy 	return nonseekable_open(inode, file);
2656552ff317SArtem Bityutskiy }
2657552ff317SArtem Bityutskiy 
265828488fc2SArtem Bityutskiy /**
265928488fc2SArtem Bityutskiy  * provide_user_output - provide output to the user reading a debugfs file.
266028488fc2SArtem Bityutskiy  * @val: boolean value for the answer
266128488fc2SArtem Bityutskiy  * @u: the buffer to store the answer at
266228488fc2SArtem Bityutskiy  * @count: size of the buffer
266328488fc2SArtem Bityutskiy  * @ppos: position in the @u output buffer
266428488fc2SArtem Bityutskiy  *
266528488fc2SArtem Bityutskiy  * This is a simple helper function which stores @val boolean value in the user
266628488fc2SArtem Bityutskiy  * buffer when the user reads one of UBIFS debugfs files. Returns amount of
266728488fc2SArtem Bityutskiy  * bytes written to @u in case of success and a negative error code in case of
266828488fc2SArtem Bityutskiy  * failure.
266928488fc2SArtem Bityutskiy  */
267028488fc2SArtem Bityutskiy static int provide_user_output(int val, char __user *u, size_t count,
267128488fc2SArtem Bityutskiy 			       loff_t *ppos)
267228488fc2SArtem Bityutskiy {
267328488fc2SArtem Bityutskiy 	char buf[3];
267428488fc2SArtem Bityutskiy 
267528488fc2SArtem Bityutskiy 	if (val)
267628488fc2SArtem Bityutskiy 		buf[0] = '1';
267728488fc2SArtem Bityutskiy 	else
267828488fc2SArtem Bityutskiy 		buf[0] = '0';
267928488fc2SArtem Bityutskiy 	buf[1] = '\n';
268028488fc2SArtem Bityutskiy 	buf[2] = 0x00;
268128488fc2SArtem Bityutskiy 
268228488fc2SArtem Bityutskiy 	return simple_read_from_buffer(u, count, ppos, buf, 2);
268328488fc2SArtem Bityutskiy }
268428488fc2SArtem Bityutskiy 
268581e79d38SArtem Bityutskiy static ssize_t dfs_file_read(struct file *file, char __user *u, size_t count,
268681e79d38SArtem Bityutskiy 			     loff_t *ppos)
268781e79d38SArtem Bityutskiy {
268881e79d38SArtem Bityutskiy 	struct dentry *dent = file->f_path.dentry;
268981e79d38SArtem Bityutskiy 	struct ubifs_info *c = file->private_data;
269081e79d38SArtem Bityutskiy 	struct ubifs_debug_info *d = c->dbg;
269181e79d38SArtem Bityutskiy 	int val;
269281e79d38SArtem Bityutskiy 
269381e79d38SArtem Bityutskiy 	if (dent == d->dfs_chk_gen)
269481e79d38SArtem Bityutskiy 		val = d->chk_gen;
269581e79d38SArtem Bityutskiy 	else if (dent == d->dfs_chk_index)
269681e79d38SArtem Bityutskiy 		val = d->chk_index;
269781e79d38SArtem Bityutskiy 	else if (dent == d->dfs_chk_orph)
269881e79d38SArtem Bityutskiy 		val = d->chk_orph;
269981e79d38SArtem Bityutskiy 	else if (dent == d->dfs_chk_lprops)
270081e79d38SArtem Bityutskiy 		val = d->chk_lprops;
270181e79d38SArtem Bityutskiy 	else if (dent == d->dfs_chk_fs)
270281e79d38SArtem Bityutskiy 		val = d->chk_fs;
270381e79d38SArtem Bityutskiy 	else if (dent == d->dfs_tst_rcvry)
270481e79d38SArtem Bityutskiy 		val = d->tst_rcvry;
270506bef945SArtem Bityutskiy 	else if (dent == d->dfs_ro_error)
270606bef945SArtem Bityutskiy 		val = c->ro_error;
270781e79d38SArtem Bityutskiy 	else
270881e79d38SArtem Bityutskiy 		return -EINVAL;
270981e79d38SArtem Bityutskiy 
271028488fc2SArtem Bityutskiy 	return provide_user_output(val, u, count, ppos);
271128488fc2SArtem Bityutskiy }
271281e79d38SArtem Bityutskiy 
271328488fc2SArtem Bityutskiy /**
271428488fc2SArtem Bityutskiy  * interpret_user_input - interpret user debugfs file input.
271528488fc2SArtem Bityutskiy  * @u: user-provided buffer with the input
271628488fc2SArtem Bityutskiy  * @count: buffer size
271728488fc2SArtem Bityutskiy  *
271828488fc2SArtem Bityutskiy  * This is a helper function which interpret user input to a boolean UBIFS
271928488fc2SArtem Bityutskiy  * debugfs file. Returns %0 or %1 in case of success and a negative error code
272028488fc2SArtem Bityutskiy  * in case of failure.
272128488fc2SArtem Bityutskiy  */
272228488fc2SArtem Bityutskiy static int interpret_user_input(const char __user *u, size_t count)
272328488fc2SArtem Bityutskiy {
272428488fc2SArtem Bityutskiy 	size_t buf_size;
272528488fc2SArtem Bityutskiy 	char buf[8];
272628488fc2SArtem Bityutskiy 
272728488fc2SArtem Bityutskiy 	buf_size = min_t(size_t, count, (sizeof(buf) - 1));
272828488fc2SArtem Bityutskiy 	if (copy_from_user(buf, u, buf_size))
272928488fc2SArtem Bityutskiy 		return -EFAULT;
273028488fc2SArtem Bityutskiy 
273128488fc2SArtem Bityutskiy 	if (buf[0] == '1')
273228488fc2SArtem Bityutskiy 		return 1;
273328488fc2SArtem Bityutskiy 	else if (buf[0] == '0')
273428488fc2SArtem Bityutskiy 		return 0;
273528488fc2SArtem Bityutskiy 
273628488fc2SArtem Bityutskiy 	return -EINVAL;
273781e79d38SArtem Bityutskiy }
273881e79d38SArtem Bityutskiy 
273981e79d38SArtem Bityutskiy static ssize_t dfs_file_write(struct file *file, const char __user *u,
2740552ff317SArtem Bityutskiy 			      size_t count, loff_t *ppos)
2741552ff317SArtem Bityutskiy {
2742552ff317SArtem Bityutskiy 	struct ubifs_info *c = file->private_data;
2743552ff317SArtem Bityutskiy 	struct ubifs_debug_info *d = c->dbg;
274481e79d38SArtem Bityutskiy 	struct dentry *dent = file->f_path.dentry;
274581e79d38SArtem Bityutskiy 	int val;
2746552ff317SArtem Bityutskiy 
274781e79d38SArtem Bityutskiy 	/*
274824a4f800SArtem Bityutskiy 	 * TODO: this is racy - the file-system might have already been
274924a4f800SArtem Bityutskiy 	 * unmounted and we'd oops in this case. The plan is to fix it with
275024a4f800SArtem Bityutskiy 	 * help of 'iterate_supers_type()' which we should have in v3.0: when
275124a4f800SArtem Bityutskiy 	 * a debugfs opened, we rember FS's UUID in file->private_data. Then
275224a4f800SArtem Bityutskiy 	 * whenever we access the FS via a debugfs file, we iterate all UBIFS
275324a4f800SArtem Bityutskiy 	 * superblocks and fine the one with the same UUID, and take the
275424a4f800SArtem Bityutskiy 	 * locking right.
275524a4f800SArtem Bityutskiy 	 *
275624a4f800SArtem Bityutskiy 	 * The other way to go suggested by Al Viro is to create a separate
275724a4f800SArtem Bityutskiy 	 * 'ubifs-debug' file-system instead.
275881e79d38SArtem Bityutskiy 	 */
275981e79d38SArtem Bityutskiy 	if (file->f_path.dentry == d->dfs_dump_lprops) {
2760edf6be24SArtem Bityutskiy 		ubifs_dump_lprops(c);
276181e79d38SArtem Bityutskiy 		return count;
276281e79d38SArtem Bityutskiy 	}
276381e79d38SArtem Bityutskiy 	if (file->f_path.dentry == d->dfs_dump_budg) {
2764edf6be24SArtem Bityutskiy 		ubifs_dump_budg(c, &c->bi);
276581e79d38SArtem Bityutskiy 		return count;
276681e79d38SArtem Bityutskiy 	}
276781e79d38SArtem Bityutskiy 	if (file->f_path.dentry == d->dfs_dump_tnc) {
2768552ff317SArtem Bityutskiy 		mutex_lock(&c->tnc_mutex);
2769edf6be24SArtem Bityutskiy 		ubifs_dump_tnc(c);
2770552ff317SArtem Bityutskiy 		mutex_unlock(&c->tnc_mutex);
277181e79d38SArtem Bityutskiy 		return count;
277281e79d38SArtem Bityutskiy 	}
277381e79d38SArtem Bityutskiy 
277428488fc2SArtem Bityutskiy 	val = interpret_user_input(u, count);
277528488fc2SArtem Bityutskiy 	if (val < 0)
277628488fc2SArtem Bityutskiy 		return val;
277781e79d38SArtem Bityutskiy 
277881e79d38SArtem Bityutskiy 	if (dent == d->dfs_chk_gen)
277981e79d38SArtem Bityutskiy 		d->chk_gen = val;
278081e79d38SArtem Bityutskiy 	else if (dent == d->dfs_chk_index)
278181e79d38SArtem Bityutskiy 		d->chk_index = val;
278281e79d38SArtem Bityutskiy 	else if (dent == d->dfs_chk_orph)
278381e79d38SArtem Bityutskiy 		d->chk_orph = val;
278481e79d38SArtem Bityutskiy 	else if (dent == d->dfs_chk_lprops)
278581e79d38SArtem Bityutskiy 		d->chk_lprops = val;
278681e79d38SArtem Bityutskiy 	else if (dent == d->dfs_chk_fs)
278781e79d38SArtem Bityutskiy 		d->chk_fs = val;
278881e79d38SArtem Bityutskiy 	else if (dent == d->dfs_tst_rcvry)
278981e79d38SArtem Bityutskiy 		d->tst_rcvry = val;
279006bef945SArtem Bityutskiy 	else if (dent == d->dfs_ro_error)
279106bef945SArtem Bityutskiy 		c->ro_error = !!val;
279281e79d38SArtem Bityutskiy 	else
2793552ff317SArtem Bityutskiy 		return -EINVAL;
2794552ff317SArtem Bityutskiy 
2795552ff317SArtem Bityutskiy 	return count;
2796552ff317SArtem Bityutskiy }
2797552ff317SArtem Bityutskiy 
279884abf972SArtem Bityutskiy static const struct file_operations dfs_fops = {
27997dae997dSArtem Bityutskiy 	.open = dfs_file_open,
280081e79d38SArtem Bityutskiy 	.read = dfs_file_read,
280181e79d38SArtem Bityutskiy 	.write = dfs_file_write,
2802552ff317SArtem Bityutskiy 	.owner = THIS_MODULE,
28031bbfc848SArtem Bityutskiy 	.llseek = no_llseek,
2804552ff317SArtem Bityutskiy };
2805552ff317SArtem Bityutskiy 
2806552ff317SArtem Bityutskiy /**
2807552ff317SArtem Bityutskiy  * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
2808552ff317SArtem Bityutskiy  * @c: UBIFS file-system description object
2809552ff317SArtem Bityutskiy  *
2810552ff317SArtem Bityutskiy  * This function creates all debugfs files for this instance of UBIFS. Returns
2811552ff317SArtem Bityutskiy  * zero in case of success and a negative error code in case of failure.
2812552ff317SArtem Bityutskiy  *
2813552ff317SArtem Bityutskiy  * Note, the only reason we have not merged this function with the
2814552ff317SArtem Bityutskiy  * 'ubifs_debugging_init()' function is because it is better to initialize
2815552ff317SArtem Bityutskiy  * debugfs interfaces at the very end of the mount process, and remove them at
2816552ff317SArtem Bityutskiy  * the very beginning of the mount process.
2817552ff317SArtem Bityutskiy  */
2818552ff317SArtem Bityutskiy int dbg_debugfs_init_fs(struct ubifs_info *c)
2819552ff317SArtem Bityutskiy {
2820ae380ce0SArtem Bityutskiy 	int err, n;
2821552ff317SArtem Bityutskiy 	const char *fname;
2822552ff317SArtem Bityutskiy 	struct dentry *dent;
2823552ff317SArtem Bityutskiy 	struct ubifs_debug_info *d = c->dbg;
2824552ff317SArtem Bityutskiy 
28252d4cf5aeSBrian Norris 	if (!IS_ENABLED(CONFIG_DEBUG_FS))
2826818039c7SArtem Bityutskiy 		return 0;
2827818039c7SArtem Bityutskiy 
2828ae380ce0SArtem Bityutskiy 	n = snprintf(d->dfs_dir_name, UBIFS_DFS_DIR_LEN + 1, UBIFS_DFS_DIR_NAME,
2829ae380ce0SArtem Bityutskiy 		     c->vi.ubi_num, c->vi.vol_id);
2830ae380ce0SArtem Bityutskiy 	if (n == UBIFS_DFS_DIR_LEN) {
2831ae380ce0SArtem Bityutskiy 		/* The array size is too small */
2832ae380ce0SArtem Bityutskiy 		fname = UBIFS_DFS_DIR_NAME;
2833ae380ce0SArtem Bityutskiy 		dent = ERR_PTR(-EINVAL);
2834ae380ce0SArtem Bityutskiy 		goto out;
2835ae380ce0SArtem Bityutskiy 	}
2836ae380ce0SArtem Bityutskiy 
2837cc6a86b9SArtem Bityutskiy 	fname = d->dfs_dir_name;
2838cc6a86b9SArtem Bityutskiy 	dent = debugfs_create_dir(fname, dfs_rootdir);
283995169535SArtem Bityutskiy 	if (IS_ERR_OR_NULL(dent))
2840552ff317SArtem Bityutskiy 		goto out;
2841cc6a86b9SArtem Bityutskiy 	d->dfs_dir = dent;
2842552ff317SArtem Bityutskiy 
2843552ff317SArtem Bityutskiy 	fname = "dump_lprops";
28448c559d30SVasiliy Kulikov 	dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
284595169535SArtem Bityutskiy 	if (IS_ERR_OR_NULL(dent))
2846552ff317SArtem Bityutskiy 		goto out_remove;
284784abf972SArtem Bityutskiy 	d->dfs_dump_lprops = dent;
2848552ff317SArtem Bityutskiy 
2849552ff317SArtem Bityutskiy 	fname = "dump_budg";
28508c559d30SVasiliy Kulikov 	dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
285195169535SArtem Bityutskiy 	if (IS_ERR_OR_NULL(dent))
2852552ff317SArtem Bityutskiy 		goto out_remove;
285384abf972SArtem Bityutskiy 	d->dfs_dump_budg = dent;
2854552ff317SArtem Bityutskiy 
2855552ff317SArtem Bityutskiy 	fname = "dump_tnc";
28568c559d30SVasiliy Kulikov 	dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
285795169535SArtem Bityutskiy 	if (IS_ERR_OR_NULL(dent))
2858552ff317SArtem Bityutskiy 		goto out_remove;
285984abf972SArtem Bityutskiy 	d->dfs_dump_tnc = dent;
2860552ff317SArtem Bityutskiy 
286181e79d38SArtem Bityutskiy 	fname = "chk_general";
286281e79d38SArtem Bityutskiy 	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
286381e79d38SArtem Bityutskiy 				   &dfs_fops);
286481e79d38SArtem Bityutskiy 	if (IS_ERR_OR_NULL(dent))
286581e79d38SArtem Bityutskiy 		goto out_remove;
286681e79d38SArtem Bityutskiy 	d->dfs_chk_gen = dent;
286781e79d38SArtem Bityutskiy 
286881e79d38SArtem Bityutskiy 	fname = "chk_index";
286981e79d38SArtem Bityutskiy 	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
287081e79d38SArtem Bityutskiy 				   &dfs_fops);
287181e79d38SArtem Bityutskiy 	if (IS_ERR_OR_NULL(dent))
287281e79d38SArtem Bityutskiy 		goto out_remove;
287381e79d38SArtem Bityutskiy 	d->dfs_chk_index = dent;
287481e79d38SArtem Bityutskiy 
287581e79d38SArtem Bityutskiy 	fname = "chk_orphans";
287681e79d38SArtem Bityutskiy 	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
287781e79d38SArtem Bityutskiy 				   &dfs_fops);
287881e79d38SArtem Bityutskiy 	if (IS_ERR_OR_NULL(dent))
287981e79d38SArtem Bityutskiy 		goto out_remove;
288081e79d38SArtem Bityutskiy 	d->dfs_chk_orph = dent;
288181e79d38SArtem Bityutskiy 
288281e79d38SArtem Bityutskiy 	fname = "chk_lprops";
288381e79d38SArtem Bityutskiy 	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
288481e79d38SArtem Bityutskiy 				   &dfs_fops);
288581e79d38SArtem Bityutskiy 	if (IS_ERR_OR_NULL(dent))
288681e79d38SArtem Bityutskiy 		goto out_remove;
288781e79d38SArtem Bityutskiy 	d->dfs_chk_lprops = dent;
288881e79d38SArtem Bityutskiy 
288981e79d38SArtem Bityutskiy 	fname = "chk_fs";
289081e79d38SArtem Bityutskiy 	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
289181e79d38SArtem Bityutskiy 				   &dfs_fops);
289281e79d38SArtem Bityutskiy 	if (IS_ERR_OR_NULL(dent))
289381e79d38SArtem Bityutskiy 		goto out_remove;
289481e79d38SArtem Bityutskiy 	d->dfs_chk_fs = dent;
289581e79d38SArtem Bityutskiy 
289681e79d38SArtem Bityutskiy 	fname = "tst_recovery";
289781e79d38SArtem Bityutskiy 	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
289881e79d38SArtem Bityutskiy 				   &dfs_fops);
289981e79d38SArtem Bityutskiy 	if (IS_ERR_OR_NULL(dent))
290081e79d38SArtem Bityutskiy 		goto out_remove;
290181e79d38SArtem Bityutskiy 	d->dfs_tst_rcvry = dent;
290281e79d38SArtem Bityutskiy 
290306bef945SArtem Bityutskiy 	fname = "ro_error";
290406bef945SArtem Bityutskiy 	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
290506bef945SArtem Bityutskiy 				   &dfs_fops);
290606bef945SArtem Bityutskiy 	if (IS_ERR_OR_NULL(dent))
290706bef945SArtem Bityutskiy 		goto out_remove;
290806bef945SArtem Bityutskiy 	d->dfs_ro_error = dent;
290906bef945SArtem Bityutskiy 
2910552ff317SArtem Bityutskiy 	return 0;
2911552ff317SArtem Bityutskiy 
2912552ff317SArtem Bityutskiy out_remove:
2913cc6a86b9SArtem Bityutskiy 	debugfs_remove_recursive(d->dfs_dir);
2914cc6a86b9SArtem Bityutskiy out:
291595169535SArtem Bityutskiy 	err = dent ? PTR_ERR(dent) : -ENODEV;
2916235c362bSSheng Yong 	ubifs_err(c, "cannot create \"%s\" debugfs file or directory, error %d\n",
2917552ff317SArtem Bityutskiy 		  fname, err);
2918552ff317SArtem Bityutskiy 	return err;
2919552ff317SArtem Bityutskiy }
2920552ff317SArtem Bityutskiy 
2921552ff317SArtem Bityutskiy /**
2922552ff317SArtem Bityutskiy  * dbg_debugfs_exit_fs - remove all debugfs files.
2923552ff317SArtem Bityutskiy  * @c: UBIFS file-system description object
2924552ff317SArtem Bityutskiy  */
2925552ff317SArtem Bityutskiy void dbg_debugfs_exit_fs(struct ubifs_info *c)
2926552ff317SArtem Bityutskiy {
29272d4cf5aeSBrian Norris 	if (IS_ENABLED(CONFIG_DEBUG_FS))
292884abf972SArtem Bityutskiy 		debugfs_remove_recursive(c->dbg->dfs_dir);
2929552ff317SArtem Bityutskiy }
2930552ff317SArtem Bityutskiy 
2931e7717060SArtem Bityutskiy struct ubifs_global_debug_info ubifs_dbg;
2932e7717060SArtem Bityutskiy 
2933e7717060SArtem Bityutskiy static struct dentry *dfs_chk_gen;
2934e7717060SArtem Bityutskiy static struct dentry *dfs_chk_index;
2935e7717060SArtem Bityutskiy static struct dentry *dfs_chk_orph;
2936e7717060SArtem Bityutskiy static struct dentry *dfs_chk_lprops;
2937e7717060SArtem Bityutskiy static struct dentry *dfs_chk_fs;
2938e7717060SArtem Bityutskiy static struct dentry *dfs_tst_rcvry;
2939e7717060SArtem Bityutskiy 
2940e7717060SArtem Bityutskiy static ssize_t dfs_global_file_read(struct file *file, char __user *u,
2941e7717060SArtem Bityutskiy 				    size_t count, loff_t *ppos)
2942e7717060SArtem Bityutskiy {
2943e7717060SArtem Bityutskiy 	struct dentry *dent = file->f_path.dentry;
2944e7717060SArtem Bityutskiy 	int val;
2945e7717060SArtem Bityutskiy 
2946e7717060SArtem Bityutskiy 	if (dent == dfs_chk_gen)
2947e7717060SArtem Bityutskiy 		val = ubifs_dbg.chk_gen;
2948e7717060SArtem Bityutskiy 	else if (dent == dfs_chk_index)
2949e7717060SArtem Bityutskiy 		val = ubifs_dbg.chk_index;
2950e7717060SArtem Bityutskiy 	else if (dent == dfs_chk_orph)
2951e7717060SArtem Bityutskiy 		val = ubifs_dbg.chk_orph;
2952e7717060SArtem Bityutskiy 	else if (dent == dfs_chk_lprops)
2953e7717060SArtem Bityutskiy 		val = ubifs_dbg.chk_lprops;
2954e7717060SArtem Bityutskiy 	else if (dent == dfs_chk_fs)
2955e7717060SArtem Bityutskiy 		val = ubifs_dbg.chk_fs;
2956e7717060SArtem Bityutskiy 	else if (dent == dfs_tst_rcvry)
2957e7717060SArtem Bityutskiy 		val = ubifs_dbg.tst_rcvry;
2958e7717060SArtem Bityutskiy 	else
2959e7717060SArtem Bityutskiy 		return -EINVAL;
2960e7717060SArtem Bityutskiy 
2961e7717060SArtem Bityutskiy 	return provide_user_output(val, u, count, ppos);
2962e7717060SArtem Bityutskiy }
2963e7717060SArtem Bityutskiy 
2964e7717060SArtem Bityutskiy static ssize_t dfs_global_file_write(struct file *file, const char __user *u,
2965e7717060SArtem Bityutskiy 				     size_t count, loff_t *ppos)
2966e7717060SArtem Bityutskiy {
2967e7717060SArtem Bityutskiy 	struct dentry *dent = file->f_path.dentry;
2968e7717060SArtem Bityutskiy 	int val;
2969e7717060SArtem Bityutskiy 
2970e7717060SArtem Bityutskiy 	val = interpret_user_input(u, count);
2971e7717060SArtem Bityutskiy 	if (val < 0)
2972e7717060SArtem Bityutskiy 		return val;
2973e7717060SArtem Bityutskiy 
2974e7717060SArtem Bityutskiy 	if (dent == dfs_chk_gen)
2975e7717060SArtem Bityutskiy 		ubifs_dbg.chk_gen = val;
2976e7717060SArtem Bityutskiy 	else if (dent == dfs_chk_index)
2977e7717060SArtem Bityutskiy 		ubifs_dbg.chk_index = val;
2978e7717060SArtem Bityutskiy 	else if (dent == dfs_chk_orph)
2979e7717060SArtem Bityutskiy 		ubifs_dbg.chk_orph = val;
2980e7717060SArtem Bityutskiy 	else if (dent == dfs_chk_lprops)
2981e7717060SArtem Bityutskiy 		ubifs_dbg.chk_lprops = val;
2982e7717060SArtem Bityutskiy 	else if (dent == dfs_chk_fs)
2983e7717060SArtem Bityutskiy 		ubifs_dbg.chk_fs = val;
2984e7717060SArtem Bityutskiy 	else if (dent == dfs_tst_rcvry)
2985e7717060SArtem Bityutskiy 		ubifs_dbg.tst_rcvry = val;
2986e7717060SArtem Bityutskiy 	else
2987e7717060SArtem Bityutskiy 		return -EINVAL;
2988e7717060SArtem Bityutskiy 
2989e7717060SArtem Bityutskiy 	return count;
2990e7717060SArtem Bityutskiy }
2991e7717060SArtem Bityutskiy 
2992e7717060SArtem Bityutskiy static const struct file_operations dfs_global_fops = {
2993e7717060SArtem Bityutskiy 	.read = dfs_global_file_read,
2994e7717060SArtem Bityutskiy 	.write = dfs_global_file_write,
2995e7717060SArtem Bityutskiy 	.owner = THIS_MODULE,
2996e7717060SArtem Bityutskiy 	.llseek = no_llseek,
2997e7717060SArtem Bityutskiy };
2998e7717060SArtem Bityutskiy 
29997dae997dSArtem Bityutskiy /**
30007dae997dSArtem Bityutskiy  * dbg_debugfs_init - initialize debugfs file-system.
30017dae997dSArtem Bityutskiy  *
30027dae997dSArtem Bityutskiy  * UBIFS uses debugfs file-system to expose various debugging knobs to
30037dae997dSArtem Bityutskiy  * user-space. This function creates "ubifs" directory in the debugfs
30047dae997dSArtem Bityutskiy  * file-system. Returns zero in case of success and a negative error code in
30057dae997dSArtem Bityutskiy  * case of failure.
30067dae997dSArtem Bityutskiy  */
30077dae997dSArtem Bityutskiy int dbg_debugfs_init(void)
30087dae997dSArtem Bityutskiy {
3009e7717060SArtem Bityutskiy 	int err;
3010e7717060SArtem Bityutskiy 	const char *fname;
3011e7717060SArtem Bityutskiy 	struct dentry *dent;
3012e7717060SArtem Bityutskiy 
30132d4cf5aeSBrian Norris 	if (!IS_ENABLED(CONFIG_DEBUG_FS))
3014818039c7SArtem Bityutskiy 		return 0;
3015818039c7SArtem Bityutskiy 
3016e7717060SArtem Bityutskiy 	fname = "ubifs";
3017e7717060SArtem Bityutskiy 	dent = debugfs_create_dir(fname, NULL);
3018e7717060SArtem Bityutskiy 	if (IS_ERR_OR_NULL(dent))
3019e7717060SArtem Bityutskiy 		goto out;
3020e7717060SArtem Bityutskiy 	dfs_rootdir = dent;
3021e7717060SArtem Bityutskiy 
3022e7717060SArtem Bityutskiy 	fname = "chk_general";
3023e7717060SArtem Bityutskiy 	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3024e7717060SArtem Bityutskiy 				   &dfs_global_fops);
3025e7717060SArtem Bityutskiy 	if (IS_ERR_OR_NULL(dent))
3026e7717060SArtem Bityutskiy 		goto out_remove;
3027e7717060SArtem Bityutskiy 	dfs_chk_gen = dent;
3028e7717060SArtem Bityutskiy 
3029e7717060SArtem Bityutskiy 	fname = "chk_index";
3030e7717060SArtem Bityutskiy 	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3031e7717060SArtem Bityutskiy 				   &dfs_global_fops);
3032e7717060SArtem Bityutskiy 	if (IS_ERR_OR_NULL(dent))
3033e7717060SArtem Bityutskiy 		goto out_remove;
3034e7717060SArtem Bityutskiy 	dfs_chk_index = dent;
3035e7717060SArtem Bityutskiy 
3036e7717060SArtem Bityutskiy 	fname = "chk_orphans";
3037e7717060SArtem Bityutskiy 	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3038e7717060SArtem Bityutskiy 				   &dfs_global_fops);
3039e7717060SArtem Bityutskiy 	if (IS_ERR_OR_NULL(dent))
3040e7717060SArtem Bityutskiy 		goto out_remove;
3041e7717060SArtem Bityutskiy 	dfs_chk_orph = dent;
3042e7717060SArtem Bityutskiy 
3043e7717060SArtem Bityutskiy 	fname = "chk_lprops";
3044e7717060SArtem Bityutskiy 	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3045e7717060SArtem Bityutskiy 				   &dfs_global_fops);
3046e7717060SArtem Bityutskiy 	if (IS_ERR_OR_NULL(dent))
3047e7717060SArtem Bityutskiy 		goto out_remove;
3048e7717060SArtem Bityutskiy 	dfs_chk_lprops = dent;
3049e7717060SArtem Bityutskiy 
3050e7717060SArtem Bityutskiy 	fname = "chk_fs";
3051e7717060SArtem Bityutskiy 	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3052e7717060SArtem Bityutskiy 				   &dfs_global_fops);
3053e7717060SArtem Bityutskiy 	if (IS_ERR_OR_NULL(dent))
3054e7717060SArtem Bityutskiy 		goto out_remove;
3055e7717060SArtem Bityutskiy 	dfs_chk_fs = dent;
3056e7717060SArtem Bityutskiy 
3057e7717060SArtem Bityutskiy 	fname = "tst_recovery";
3058e7717060SArtem Bityutskiy 	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3059e7717060SArtem Bityutskiy 				   &dfs_global_fops);
3060e7717060SArtem Bityutskiy 	if (IS_ERR_OR_NULL(dent))
3061e7717060SArtem Bityutskiy 		goto out_remove;
3062e7717060SArtem Bityutskiy 	dfs_tst_rcvry = dent;
30637dae997dSArtem Bityutskiy 
30647dae997dSArtem Bityutskiy 	return 0;
3065e7717060SArtem Bityutskiy 
3066e7717060SArtem Bityutskiy out_remove:
3067e7717060SArtem Bityutskiy 	debugfs_remove_recursive(dfs_rootdir);
3068e7717060SArtem Bityutskiy out:
3069e7717060SArtem Bityutskiy 	err = dent ? PTR_ERR(dent) : -ENODEV;
3070235c362bSSheng Yong 	pr_err("UBIFS error (pid %d): cannot create \"%s\" debugfs file or directory, error %d\n",
3071235c362bSSheng Yong 	       current->pid, fname, err);
3072e7717060SArtem Bityutskiy 	return err;
30737dae997dSArtem Bityutskiy }
30747dae997dSArtem Bityutskiy 
30757dae997dSArtem Bityutskiy /**
30767dae997dSArtem Bityutskiy  * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
30777dae997dSArtem Bityutskiy  */
30787dae997dSArtem Bityutskiy void dbg_debugfs_exit(void)
30797dae997dSArtem Bityutskiy {
30802d4cf5aeSBrian Norris 	if (IS_ENABLED(CONFIG_DEBUG_FS))
3081e7717060SArtem Bityutskiy 		debugfs_remove_recursive(dfs_rootdir);
30827dae997dSArtem Bityutskiy }
30837dae997dSArtem Bityutskiy 
30847dae997dSArtem Bityutskiy /**
30857dae997dSArtem Bityutskiy  * ubifs_debugging_init - initialize UBIFS debugging.
30867dae997dSArtem Bityutskiy  * @c: UBIFS file-system description object
30877dae997dSArtem Bityutskiy  *
30887dae997dSArtem Bityutskiy  * This function initializes debugging-related data for the file system.
30897dae997dSArtem Bityutskiy  * Returns zero in case of success and a negative error code in case of
30907dae997dSArtem Bityutskiy  * failure.
30917dae997dSArtem Bityutskiy  */
30927dae997dSArtem Bityutskiy int ubifs_debugging_init(struct ubifs_info *c)
30937dae997dSArtem Bityutskiy {
30947dae997dSArtem Bityutskiy 	c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
30957dae997dSArtem Bityutskiy 	if (!c->dbg)
30967dae997dSArtem Bityutskiy 		return -ENOMEM;
30977dae997dSArtem Bityutskiy 
30987dae997dSArtem Bityutskiy 	return 0;
30997dae997dSArtem Bityutskiy }
31007dae997dSArtem Bityutskiy 
31017dae997dSArtem Bityutskiy /**
31027dae997dSArtem Bityutskiy  * ubifs_debugging_exit - free debugging data.
31037dae997dSArtem Bityutskiy  * @c: UBIFS file-system description object
31047dae997dSArtem Bityutskiy  */
31057dae997dSArtem Bityutskiy void ubifs_debugging_exit(struct ubifs_info *c)
31067dae997dSArtem Bityutskiy {
31077dae997dSArtem Bityutskiy 	kfree(c->dbg);
31087dae997dSArtem Bityutskiy }
3109