12b27bdccSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21e51764aSArtem Bityutskiy /* 31e51764aSArtem Bityutskiy * This file is part of UBIFS. 41e51764aSArtem Bityutskiy * 51e51764aSArtem Bityutskiy * Copyright (C) 2006-2008 Nokia Corporation 61e51764aSArtem Bityutskiy * 71e51764aSArtem Bityutskiy * Authors: Artem Bityutskiy (Битюцкий Артём) 81e51764aSArtem Bityutskiy * Adrian Hunter 91e51764aSArtem Bityutskiy */ 101e51764aSArtem Bityutskiy 111e51764aSArtem Bityutskiy /* 121e51764aSArtem Bityutskiy * This file implements most of the debugging stuff which is compiled in only 131e51764aSArtem Bityutskiy * when it is enabled. But some debugging check functions are implemented in 141e51764aSArtem Bityutskiy * corresponding subsystem, just because they are closely related and utilize 151e51764aSArtem Bityutskiy * various local functions of those subsystems. 161e51764aSArtem Bityutskiy */ 171e51764aSArtem Bityutskiy 181e51764aSArtem Bityutskiy #include <linux/module.h> 19552ff317SArtem Bityutskiy #include <linux/debugfs.h> 204d61db4fSArtem Bityutskiy #include <linux/math64.h> 2181e79d38SArtem Bityutskiy #include <linux/uaccess.h> 22a7fa94a9SArtem Bityutskiy #include <linux/random.h> 23e328379aSHyunchul Lee #include <linux/ctype.h> 24a7fa94a9SArtem Bityutskiy #include "ubifs.h" 251e51764aSArtem Bityutskiy 26b06283c7SArtem Bityutskiy static DEFINE_SPINLOCK(dbg_lock); 271e51764aSArtem Bityutskiy 281e51764aSArtem Bityutskiy static const char *get_key_fmt(int fmt) 291e51764aSArtem Bityutskiy { 301e51764aSArtem Bityutskiy switch (fmt) { 311e51764aSArtem Bityutskiy case UBIFS_SIMPLE_KEY_FMT: 321e51764aSArtem Bityutskiy return "simple"; 331e51764aSArtem Bityutskiy default: 341e51764aSArtem Bityutskiy return "unknown/invalid format"; 351e51764aSArtem Bityutskiy } 361e51764aSArtem Bityutskiy } 371e51764aSArtem Bityutskiy 381e51764aSArtem Bityutskiy static const char *get_key_hash(int hash) 391e51764aSArtem Bityutskiy { 401e51764aSArtem Bityutskiy switch (hash) { 411e51764aSArtem Bityutskiy case UBIFS_KEY_HASH_R5: 421e51764aSArtem Bityutskiy return "R5"; 431e51764aSArtem Bityutskiy case UBIFS_KEY_HASH_TEST: 441e51764aSArtem Bityutskiy return "test"; 451e51764aSArtem Bityutskiy default: 461e51764aSArtem Bityutskiy return "unknown/invalid name hash"; 471e51764aSArtem Bityutskiy } 481e51764aSArtem Bityutskiy } 491e51764aSArtem Bityutskiy 501e51764aSArtem Bityutskiy static const char *get_key_type(int type) 511e51764aSArtem Bityutskiy { 521e51764aSArtem Bityutskiy switch (type) { 531e51764aSArtem Bityutskiy case UBIFS_INO_KEY: 541e51764aSArtem Bityutskiy return "inode"; 551e51764aSArtem Bityutskiy case UBIFS_DENT_KEY: 561e51764aSArtem Bityutskiy return "direntry"; 571e51764aSArtem Bityutskiy case UBIFS_XENT_KEY: 581e51764aSArtem Bityutskiy return "xentry"; 591e51764aSArtem Bityutskiy case UBIFS_DATA_KEY: 601e51764aSArtem Bityutskiy return "data"; 611e51764aSArtem Bityutskiy case UBIFS_TRUN_KEY: 621e51764aSArtem Bityutskiy return "truncate"; 631e51764aSArtem Bityutskiy default: 641e51764aSArtem Bityutskiy return "unknown/invalid key"; 651e51764aSArtem Bityutskiy } 661e51764aSArtem Bityutskiy } 671e51764aSArtem Bityutskiy 684315fb40SArtem Bityutskiy static const char *get_dent_type(int type) 694315fb40SArtem Bityutskiy { 704315fb40SArtem Bityutskiy switch (type) { 714315fb40SArtem Bityutskiy case UBIFS_ITYPE_REG: 724315fb40SArtem Bityutskiy return "file"; 734315fb40SArtem Bityutskiy case UBIFS_ITYPE_DIR: 744315fb40SArtem Bityutskiy return "dir"; 754315fb40SArtem Bityutskiy case UBIFS_ITYPE_LNK: 764315fb40SArtem Bityutskiy return "symlink"; 774315fb40SArtem Bityutskiy case UBIFS_ITYPE_BLK: 784315fb40SArtem Bityutskiy return "blkdev"; 794315fb40SArtem Bityutskiy case UBIFS_ITYPE_CHR: 804315fb40SArtem Bityutskiy return "char dev"; 814315fb40SArtem Bityutskiy case UBIFS_ITYPE_FIFO: 824315fb40SArtem Bityutskiy return "fifo"; 834315fb40SArtem Bityutskiy case UBIFS_ITYPE_SOCK: 844315fb40SArtem Bityutskiy return "socket"; 854315fb40SArtem Bityutskiy default: 864315fb40SArtem Bityutskiy return "unknown/invalid type"; 874315fb40SArtem Bityutskiy } 884315fb40SArtem Bityutskiy } 894315fb40SArtem Bityutskiy 90515315a1SArtem Bityutskiy const char *dbg_snprintf_key(const struct ubifs_info *c, 91515315a1SArtem Bityutskiy const union ubifs_key *key, char *buffer, int len) 921e51764aSArtem Bityutskiy { 931e51764aSArtem Bityutskiy char *p = buffer; 941e51764aSArtem Bityutskiy int type = key_type(c, key); 951e51764aSArtem Bityutskiy 961e51764aSArtem Bityutskiy if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) { 971e51764aSArtem Bityutskiy switch (type) { 981e51764aSArtem Bityutskiy case UBIFS_INO_KEY: 99beba0060SArtem Bityutskiy len -= snprintf(p, len, "(%lu, %s)", 100beba0060SArtem Bityutskiy (unsigned long)key_inum(c, key), 1011e51764aSArtem Bityutskiy get_key_type(type)); 1021e51764aSArtem Bityutskiy break; 1031e51764aSArtem Bityutskiy case UBIFS_DENT_KEY: 1041e51764aSArtem Bityutskiy case UBIFS_XENT_KEY: 105beba0060SArtem Bityutskiy len -= snprintf(p, len, "(%lu, %s, %#08x)", 106e84461adSArtem Bityutskiy (unsigned long)key_inum(c, key), 1071e51764aSArtem Bityutskiy get_key_type(type), key_hash(c, key)); 1081e51764aSArtem Bityutskiy break; 1091e51764aSArtem Bityutskiy case UBIFS_DATA_KEY: 110beba0060SArtem Bityutskiy len -= snprintf(p, len, "(%lu, %s, %u)", 111e84461adSArtem Bityutskiy (unsigned long)key_inum(c, key), 1121e51764aSArtem Bityutskiy get_key_type(type), key_block(c, key)); 1131e51764aSArtem Bityutskiy break; 1141e51764aSArtem Bityutskiy case UBIFS_TRUN_KEY: 115beba0060SArtem Bityutskiy len -= snprintf(p, len, "(%lu, %s)", 116e84461adSArtem Bityutskiy (unsigned long)key_inum(c, key), 117e84461adSArtem Bityutskiy get_key_type(type)); 1181e51764aSArtem Bityutskiy break; 1191e51764aSArtem Bityutskiy default: 120beba0060SArtem Bityutskiy len -= snprintf(p, len, "(bad key type: %#08x, %#08x)", 1211e51764aSArtem Bityutskiy key->u32[0], key->u32[1]); 1221e51764aSArtem Bityutskiy } 1231e51764aSArtem Bityutskiy } else 124beba0060SArtem Bityutskiy len -= snprintf(p, len, "bad key format %d", c->key_fmt); 1256eb61d58SRichard Weinberger ubifs_assert(c, len > 0); 126515315a1SArtem Bityutskiy return p; 1271e51764aSArtem Bityutskiy } 1281e51764aSArtem Bityutskiy 1291e51764aSArtem Bityutskiy const char *dbg_ntype(int type) 1301e51764aSArtem Bityutskiy { 1311e51764aSArtem Bityutskiy switch (type) { 1321e51764aSArtem Bityutskiy case UBIFS_PAD_NODE: 1331e51764aSArtem Bityutskiy return "padding node"; 1341e51764aSArtem Bityutskiy case UBIFS_SB_NODE: 1351e51764aSArtem Bityutskiy return "superblock node"; 1361e51764aSArtem Bityutskiy case UBIFS_MST_NODE: 1371e51764aSArtem Bityutskiy return "master node"; 1381e51764aSArtem Bityutskiy case UBIFS_REF_NODE: 1391e51764aSArtem Bityutskiy return "reference node"; 1401e51764aSArtem Bityutskiy case UBIFS_INO_NODE: 1411e51764aSArtem Bityutskiy return "inode node"; 1421e51764aSArtem Bityutskiy case UBIFS_DENT_NODE: 1431e51764aSArtem Bityutskiy return "direntry node"; 1441e51764aSArtem Bityutskiy case UBIFS_XENT_NODE: 1451e51764aSArtem Bityutskiy return "xentry node"; 1461e51764aSArtem Bityutskiy case UBIFS_DATA_NODE: 1471e51764aSArtem Bityutskiy return "data node"; 1481e51764aSArtem Bityutskiy case UBIFS_TRUN_NODE: 1491e51764aSArtem Bityutskiy return "truncate node"; 1501e51764aSArtem Bityutskiy case UBIFS_IDX_NODE: 1511e51764aSArtem Bityutskiy return "indexing node"; 1521e51764aSArtem Bityutskiy case UBIFS_CS_NODE: 1531e51764aSArtem Bityutskiy return "commit start node"; 1541e51764aSArtem Bityutskiy case UBIFS_ORPH_NODE: 1551e51764aSArtem Bityutskiy return "orphan node"; 1565125cfdfSSascha Hauer case UBIFS_AUTH_NODE: 1575125cfdfSSascha Hauer return "auth node"; 1581e51764aSArtem Bityutskiy default: 1591e51764aSArtem Bityutskiy return "unknown node"; 1601e51764aSArtem Bityutskiy } 1611e51764aSArtem Bityutskiy } 1621e51764aSArtem Bityutskiy 1631e51764aSArtem Bityutskiy static const char *dbg_gtype(int type) 1641e51764aSArtem Bityutskiy { 1651e51764aSArtem Bityutskiy switch (type) { 1661e51764aSArtem Bityutskiy case UBIFS_NO_NODE_GROUP: 1671e51764aSArtem Bityutskiy return "no node group"; 1681e51764aSArtem Bityutskiy case UBIFS_IN_NODE_GROUP: 1691e51764aSArtem Bityutskiy return "in node group"; 1701e51764aSArtem Bityutskiy case UBIFS_LAST_OF_NODE_GROUP: 1711e51764aSArtem Bityutskiy return "last of node group"; 1721e51764aSArtem Bityutskiy default: 1731e51764aSArtem Bityutskiy return "unknown"; 1741e51764aSArtem Bityutskiy } 1751e51764aSArtem Bityutskiy } 1761e51764aSArtem Bityutskiy 1771e51764aSArtem Bityutskiy const char *dbg_cstate(int cmt_state) 1781e51764aSArtem Bityutskiy { 1791e51764aSArtem Bityutskiy switch (cmt_state) { 1801e51764aSArtem Bityutskiy case COMMIT_RESTING: 1811e51764aSArtem Bityutskiy return "commit resting"; 1821e51764aSArtem Bityutskiy case COMMIT_BACKGROUND: 1831e51764aSArtem Bityutskiy return "background commit requested"; 1841e51764aSArtem Bityutskiy case COMMIT_REQUIRED: 1851e51764aSArtem Bityutskiy return "commit required"; 1861e51764aSArtem Bityutskiy case COMMIT_RUNNING_BACKGROUND: 1871e51764aSArtem Bityutskiy return "BACKGROUND commit running"; 1881e51764aSArtem Bityutskiy case COMMIT_RUNNING_REQUIRED: 1891e51764aSArtem Bityutskiy return "commit running and required"; 1901e51764aSArtem Bityutskiy case COMMIT_BROKEN: 1911e51764aSArtem Bityutskiy return "broken commit"; 1921e51764aSArtem Bityutskiy default: 1931e51764aSArtem Bityutskiy return "unknown commit state"; 1941e51764aSArtem Bityutskiy } 1951e51764aSArtem Bityutskiy } 1961e51764aSArtem Bityutskiy 19777a7ae58SArtem Bityutskiy const char *dbg_jhead(int jhead) 19877a7ae58SArtem Bityutskiy { 19977a7ae58SArtem Bityutskiy switch (jhead) { 20077a7ae58SArtem Bityutskiy case GCHD: 20177a7ae58SArtem Bityutskiy return "0 (GC)"; 20277a7ae58SArtem Bityutskiy case BASEHD: 20377a7ae58SArtem Bityutskiy return "1 (base)"; 20477a7ae58SArtem Bityutskiy case DATAHD: 20577a7ae58SArtem Bityutskiy return "2 (data)"; 20677a7ae58SArtem Bityutskiy default: 20777a7ae58SArtem Bityutskiy return "unknown journal head"; 20877a7ae58SArtem Bityutskiy } 20977a7ae58SArtem Bityutskiy } 21077a7ae58SArtem Bityutskiy 2111e51764aSArtem Bityutskiy static void dump_ch(const struct ubifs_ch *ch) 2121e51764aSArtem Bityutskiy { 2136b38d03fSArtem Bityutskiy pr_err("\tmagic %#x\n", le32_to_cpu(ch->magic)); 2146b38d03fSArtem Bityutskiy pr_err("\tcrc %#x\n", le32_to_cpu(ch->crc)); 2156b38d03fSArtem Bityutskiy pr_err("\tnode_type %d (%s)\n", ch->node_type, 2161e51764aSArtem Bityutskiy dbg_ntype(ch->node_type)); 2176b38d03fSArtem Bityutskiy pr_err("\tgroup_type %d (%s)\n", ch->group_type, 2181e51764aSArtem Bityutskiy dbg_gtype(ch->group_type)); 2196b38d03fSArtem Bityutskiy pr_err("\tsqnum %llu\n", 2201e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(ch->sqnum)); 2216b38d03fSArtem Bityutskiy pr_err("\tlen %u\n", le32_to_cpu(ch->len)); 2221e51764aSArtem Bityutskiy } 2231e51764aSArtem Bityutskiy 224edf6be24SArtem Bityutskiy void ubifs_dump_inode(struct ubifs_info *c, const struct inode *inode) 2251e51764aSArtem Bityutskiy { 2261e51764aSArtem Bityutskiy const struct ubifs_inode *ui = ubifs_inode(inode); 227f4f61d2cSRichard Weinberger struct fscrypt_name nm = {0}; 2284315fb40SArtem Bityutskiy union ubifs_key key; 2294315fb40SArtem Bityutskiy struct ubifs_dent_node *dent, *pdent = NULL; 2304315fb40SArtem Bityutskiy int count = 2; 2311e51764aSArtem Bityutskiy 2326b38d03fSArtem Bityutskiy pr_err("Dump in-memory inode:"); 2336b38d03fSArtem Bityutskiy pr_err("\tinode %lu\n", inode->i_ino); 2346b38d03fSArtem Bityutskiy pr_err("\tsize %llu\n", 2351e51764aSArtem Bityutskiy (unsigned long long)i_size_read(inode)); 2366b38d03fSArtem Bityutskiy pr_err("\tnlink %u\n", inode->i_nlink); 237782c3fb2SLinus Torvalds pr_err("\tuid %u\n", (unsigned int)i_uid_read(inode)); 238782c3fb2SLinus Torvalds pr_err("\tgid %u\n", (unsigned int)i_gid_read(inode)); 2396b38d03fSArtem Bityutskiy pr_err("\tatime %u.%u\n", 2401e51764aSArtem Bityutskiy (unsigned int)inode->i_atime.tv_sec, 2411e51764aSArtem Bityutskiy (unsigned int)inode->i_atime.tv_nsec); 2426b38d03fSArtem Bityutskiy pr_err("\tmtime %u.%u\n", 2431e51764aSArtem Bityutskiy (unsigned int)inode->i_mtime.tv_sec, 2441e51764aSArtem Bityutskiy (unsigned int)inode->i_mtime.tv_nsec); 2456b38d03fSArtem Bityutskiy pr_err("\tctime %u.%u\n", 2461e51764aSArtem Bityutskiy (unsigned int)inode->i_ctime.tv_sec, 2471e51764aSArtem Bityutskiy (unsigned int)inode->i_ctime.tv_nsec); 2486b38d03fSArtem Bityutskiy pr_err("\tcreat_sqnum %llu\n", ui->creat_sqnum); 2496b38d03fSArtem Bityutskiy pr_err("\txattr_size %u\n", ui->xattr_size); 2506b38d03fSArtem Bityutskiy pr_err("\txattr_cnt %u\n", ui->xattr_cnt); 2516b38d03fSArtem Bityutskiy pr_err("\txattr_names %u\n", ui->xattr_names); 2526b38d03fSArtem Bityutskiy pr_err("\tdirty %u\n", ui->dirty); 2536b38d03fSArtem Bityutskiy pr_err("\txattr %u\n", ui->xattr); 2541112018cSAndreas Gruenbacher pr_err("\tbulk_read %u\n", ui->bulk_read); 2556b38d03fSArtem Bityutskiy pr_err("\tsynced_i_size %llu\n", 256b5e426e9SArtem Bityutskiy (unsigned long long)ui->synced_i_size); 2576b38d03fSArtem Bityutskiy pr_err("\tui_size %llu\n", 258b5e426e9SArtem Bityutskiy (unsigned long long)ui->ui_size); 2596b38d03fSArtem Bityutskiy pr_err("\tflags %d\n", ui->flags); 2606b38d03fSArtem Bityutskiy pr_err("\tcompr_type %d\n", ui->compr_type); 2616b38d03fSArtem Bityutskiy pr_err("\tlast_page_read %lu\n", ui->last_page_read); 2626b38d03fSArtem Bityutskiy pr_err("\tread_in_a_row %lu\n", ui->read_in_a_row); 2636b38d03fSArtem Bityutskiy pr_err("\tdata_len %d\n", ui->data_len); 2644315fb40SArtem Bityutskiy 2654315fb40SArtem Bityutskiy if (!S_ISDIR(inode->i_mode)) 2664315fb40SArtem Bityutskiy return; 2674315fb40SArtem Bityutskiy 2686b38d03fSArtem Bityutskiy pr_err("List of directory entries:\n"); 2696eb61d58SRichard Weinberger ubifs_assert(c, !mutex_is_locked(&c->tnc_mutex)); 2704315fb40SArtem Bityutskiy 2714315fb40SArtem Bityutskiy lowest_dent_key(c, &key, inode->i_ino); 2724315fb40SArtem Bityutskiy while (1) { 2734315fb40SArtem Bityutskiy dent = ubifs_tnc_next_ent(c, &key, &nm); 2744315fb40SArtem Bityutskiy if (IS_ERR(dent)) { 2754315fb40SArtem Bityutskiy if (PTR_ERR(dent) != -ENOENT) 2766b38d03fSArtem Bityutskiy pr_err("error %ld\n", PTR_ERR(dent)); 2774315fb40SArtem Bityutskiy break; 2784315fb40SArtem Bityutskiy } 2794315fb40SArtem Bityutskiy 28033fda9faSHyunchul Lee pr_err("\t%d: inode %llu, type %s, len %d\n", 28133fda9faSHyunchul Lee count++, (unsigned long long) le64_to_cpu(dent->inum), 28233fda9faSHyunchul Lee get_dent_type(dent->type), 28333fda9faSHyunchul Lee le16_to_cpu(dent->nlen)); 2844315fb40SArtem Bityutskiy 285f4f61d2cSRichard Weinberger fname_name(&nm) = dent->name; 286f4f61d2cSRichard Weinberger fname_len(&nm) = le16_to_cpu(dent->nlen); 2874315fb40SArtem Bityutskiy kfree(pdent); 2884315fb40SArtem Bityutskiy pdent = dent; 2894315fb40SArtem Bityutskiy key_read(c, &dent->key, &key); 2904315fb40SArtem Bityutskiy } 2914315fb40SArtem Bityutskiy kfree(pdent); 2921e51764aSArtem Bityutskiy } 2931e51764aSArtem Bityutskiy 294c4c0d19dSZhihao Cheng void ubifs_dump_node(const struct ubifs_info *c, const void *node, int node_len) 2951e51764aSArtem Bityutskiy { 296c4c0d19dSZhihao Cheng int i, n, type, safe_len, max_node_len, min_node_len; 2971e51764aSArtem Bityutskiy union ubifs_key key; 2981e51764aSArtem Bityutskiy const struct ubifs_ch *ch = node; 299515315a1SArtem Bityutskiy char key_buf[DBG_KEY_BUF_LEN]; 3001e51764aSArtem Bityutskiy 3011e51764aSArtem Bityutskiy /* If the magic is incorrect, just hexdump the first bytes */ 3021e51764aSArtem Bityutskiy if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) { 3036b38d03fSArtem Bityutskiy pr_err("Not a node, first %zu bytes:", UBIFS_CH_SZ); 30416c395caSArtem Bityutskiy print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 32, 1, 3051e51764aSArtem Bityutskiy (void *)node, UBIFS_CH_SZ, 1); 3061e51764aSArtem Bityutskiy return; 3071e51764aSArtem Bityutskiy } 3081e51764aSArtem Bityutskiy 309c4c0d19dSZhihao Cheng /* Skip dumping unknown type node */ 310c4c0d19dSZhihao Cheng type = ch->node_type; 311c4c0d19dSZhihao Cheng if (type < 0 || type >= UBIFS_NODE_TYPES_CNT) { 312c4c0d19dSZhihao Cheng pr_err("node type %d was not recognized\n", type); 313c4c0d19dSZhihao Cheng return; 314c4c0d19dSZhihao Cheng } 315c4c0d19dSZhihao Cheng 3161e51764aSArtem Bityutskiy spin_lock(&dbg_lock); 3171e51764aSArtem Bityutskiy dump_ch(node); 3181e51764aSArtem Bityutskiy 319c4c0d19dSZhihao Cheng if (c->ranges[type].max_len == 0) { 320c4c0d19dSZhihao Cheng max_node_len = min_node_len = c->ranges[type].len; 321c4c0d19dSZhihao Cheng } else { 322c4c0d19dSZhihao Cheng max_node_len = c->ranges[type].max_len; 323c4c0d19dSZhihao Cheng min_node_len = c->ranges[type].min_len; 324c4c0d19dSZhihao Cheng } 325c4c0d19dSZhihao Cheng safe_len = le32_to_cpu(ch->len); 326c4c0d19dSZhihao Cheng safe_len = safe_len > 0 ? safe_len : 0; 327c4c0d19dSZhihao Cheng safe_len = min3(safe_len, max_node_len, node_len); 328c4c0d19dSZhihao Cheng if (safe_len < min_node_len) { 329c4c0d19dSZhihao Cheng pr_err("node len(%d) is too short for %s, left %d bytes:\n", 330c4c0d19dSZhihao Cheng safe_len, dbg_ntype(type), 331c4c0d19dSZhihao Cheng safe_len > UBIFS_CH_SZ ? 332c4c0d19dSZhihao Cheng safe_len - (int)UBIFS_CH_SZ : 0); 333c4c0d19dSZhihao Cheng if (safe_len > UBIFS_CH_SZ) 334c4c0d19dSZhihao Cheng print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 32, 1, 335c4c0d19dSZhihao Cheng (void *)node + UBIFS_CH_SZ, 336c4c0d19dSZhihao Cheng safe_len - UBIFS_CH_SZ, 0); 337c4c0d19dSZhihao Cheng goto out_unlock; 338c4c0d19dSZhihao Cheng } 339c4c0d19dSZhihao Cheng if (safe_len != le32_to_cpu(ch->len)) 340c4c0d19dSZhihao Cheng pr_err("\ttruncated node length %d\n", safe_len); 341c4c0d19dSZhihao Cheng 342c4c0d19dSZhihao Cheng switch (type) { 3431e51764aSArtem Bityutskiy case UBIFS_PAD_NODE: 3441e51764aSArtem Bityutskiy { 3451e51764aSArtem Bityutskiy const struct ubifs_pad_node *pad = node; 3461e51764aSArtem Bityutskiy 3476b38d03fSArtem Bityutskiy pr_err("\tpad_len %u\n", le32_to_cpu(pad->pad_len)); 3481e51764aSArtem Bityutskiy break; 3491e51764aSArtem Bityutskiy } 3501e51764aSArtem Bityutskiy case UBIFS_SB_NODE: 3511e51764aSArtem Bityutskiy { 3521e51764aSArtem Bityutskiy const struct ubifs_sb_node *sup = node; 3531e51764aSArtem Bityutskiy unsigned int sup_flags = le32_to_cpu(sup->flags); 3541e51764aSArtem Bityutskiy 3556b38d03fSArtem Bityutskiy pr_err("\tkey_hash %d (%s)\n", 3561e51764aSArtem Bityutskiy (int)sup->key_hash, get_key_hash(sup->key_hash)); 3576b38d03fSArtem Bityutskiy pr_err("\tkey_fmt %d (%s)\n", 3581e51764aSArtem Bityutskiy (int)sup->key_fmt, get_key_fmt(sup->key_fmt)); 3596b38d03fSArtem Bityutskiy pr_err("\tflags %#x\n", sup_flags); 3606b38d03fSArtem Bityutskiy pr_err("\tbig_lpt %u\n", 3611e51764aSArtem Bityutskiy !!(sup_flags & UBIFS_FLG_BIGLPT)); 3626b38d03fSArtem Bityutskiy pr_err("\tspace_fixup %u\n", 3639f58d350SMatthew L. Creech !!(sup_flags & UBIFS_FLG_SPACE_FIXUP)); 3646b38d03fSArtem Bityutskiy pr_err("\tmin_io_size %u\n", le32_to_cpu(sup->min_io_size)); 3656b38d03fSArtem Bityutskiy pr_err("\tleb_size %u\n", le32_to_cpu(sup->leb_size)); 3666b38d03fSArtem Bityutskiy pr_err("\tleb_cnt %u\n", le32_to_cpu(sup->leb_cnt)); 3676b38d03fSArtem Bityutskiy pr_err("\tmax_leb_cnt %u\n", le32_to_cpu(sup->max_leb_cnt)); 3686b38d03fSArtem Bityutskiy pr_err("\tmax_bud_bytes %llu\n", 3691e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(sup->max_bud_bytes)); 3706b38d03fSArtem Bityutskiy pr_err("\tlog_lebs %u\n", le32_to_cpu(sup->log_lebs)); 3716b38d03fSArtem Bityutskiy pr_err("\tlpt_lebs %u\n", le32_to_cpu(sup->lpt_lebs)); 3726b38d03fSArtem Bityutskiy pr_err("\torph_lebs %u\n", le32_to_cpu(sup->orph_lebs)); 3736b38d03fSArtem Bityutskiy pr_err("\tjhead_cnt %u\n", le32_to_cpu(sup->jhead_cnt)); 3746b38d03fSArtem Bityutskiy pr_err("\tfanout %u\n", le32_to_cpu(sup->fanout)); 3756b38d03fSArtem Bityutskiy pr_err("\tlsave_cnt %u\n", le32_to_cpu(sup->lsave_cnt)); 3766b38d03fSArtem Bityutskiy pr_err("\tdefault_compr %u\n", 3771e51764aSArtem Bityutskiy (int)le16_to_cpu(sup->default_compr)); 3786b38d03fSArtem Bityutskiy pr_err("\trp_size %llu\n", 3791e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(sup->rp_size)); 3806b38d03fSArtem Bityutskiy pr_err("\trp_uid %u\n", le32_to_cpu(sup->rp_uid)); 3816b38d03fSArtem Bityutskiy pr_err("\trp_gid %u\n", le32_to_cpu(sup->rp_gid)); 3826b38d03fSArtem Bityutskiy pr_err("\tfmt_version %u\n", le32_to_cpu(sup->fmt_version)); 3836b38d03fSArtem Bityutskiy pr_err("\ttime_gran %u\n", le32_to_cpu(sup->time_gran)); 3846b38d03fSArtem Bityutskiy pr_err("\tUUID %pUB\n", sup->uuid); 3851e51764aSArtem Bityutskiy break; 3861e51764aSArtem Bityutskiy } 3871e51764aSArtem Bityutskiy case UBIFS_MST_NODE: 3881e51764aSArtem Bityutskiy { 3891e51764aSArtem Bityutskiy const struct ubifs_mst_node *mst = node; 3901e51764aSArtem Bityutskiy 3916b38d03fSArtem Bityutskiy pr_err("\thighest_inum %llu\n", 3921e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->highest_inum)); 3936b38d03fSArtem Bityutskiy pr_err("\tcommit number %llu\n", 3941e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->cmt_no)); 3956b38d03fSArtem Bityutskiy pr_err("\tflags %#x\n", le32_to_cpu(mst->flags)); 3966b38d03fSArtem Bityutskiy pr_err("\tlog_lnum %u\n", le32_to_cpu(mst->log_lnum)); 3976b38d03fSArtem Bityutskiy pr_err("\troot_lnum %u\n", le32_to_cpu(mst->root_lnum)); 3986b38d03fSArtem Bityutskiy pr_err("\troot_offs %u\n", le32_to_cpu(mst->root_offs)); 3996b38d03fSArtem Bityutskiy pr_err("\troot_len %u\n", le32_to_cpu(mst->root_len)); 4006b38d03fSArtem Bityutskiy pr_err("\tgc_lnum %u\n", le32_to_cpu(mst->gc_lnum)); 4016b38d03fSArtem Bityutskiy pr_err("\tihead_lnum %u\n", le32_to_cpu(mst->ihead_lnum)); 4026b38d03fSArtem Bityutskiy pr_err("\tihead_offs %u\n", le32_to_cpu(mst->ihead_offs)); 4036b38d03fSArtem Bityutskiy pr_err("\tindex_size %llu\n", 4040ecb9529SHarvey Harrison (unsigned long long)le64_to_cpu(mst->index_size)); 4056b38d03fSArtem Bityutskiy pr_err("\tlpt_lnum %u\n", le32_to_cpu(mst->lpt_lnum)); 4066b38d03fSArtem Bityutskiy pr_err("\tlpt_offs %u\n", le32_to_cpu(mst->lpt_offs)); 4076b38d03fSArtem Bityutskiy pr_err("\tnhead_lnum %u\n", le32_to_cpu(mst->nhead_lnum)); 4086b38d03fSArtem Bityutskiy pr_err("\tnhead_offs %u\n", le32_to_cpu(mst->nhead_offs)); 4096b38d03fSArtem Bityutskiy pr_err("\tltab_lnum %u\n", le32_to_cpu(mst->ltab_lnum)); 4106b38d03fSArtem Bityutskiy pr_err("\tltab_offs %u\n", le32_to_cpu(mst->ltab_offs)); 4116b38d03fSArtem Bityutskiy pr_err("\tlsave_lnum %u\n", le32_to_cpu(mst->lsave_lnum)); 4126b38d03fSArtem Bityutskiy pr_err("\tlsave_offs %u\n", le32_to_cpu(mst->lsave_offs)); 4136b38d03fSArtem Bityutskiy pr_err("\tlscan_lnum %u\n", le32_to_cpu(mst->lscan_lnum)); 4146b38d03fSArtem Bityutskiy pr_err("\tleb_cnt %u\n", le32_to_cpu(mst->leb_cnt)); 4156b38d03fSArtem Bityutskiy pr_err("\tempty_lebs %u\n", le32_to_cpu(mst->empty_lebs)); 4166b38d03fSArtem Bityutskiy pr_err("\tidx_lebs %u\n", le32_to_cpu(mst->idx_lebs)); 4176b38d03fSArtem Bityutskiy pr_err("\ttotal_free %llu\n", 4181e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->total_free)); 4196b38d03fSArtem Bityutskiy pr_err("\ttotal_dirty %llu\n", 4201e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->total_dirty)); 4216b38d03fSArtem Bityutskiy pr_err("\ttotal_used %llu\n", 4221e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->total_used)); 4236b38d03fSArtem Bityutskiy pr_err("\ttotal_dead %llu\n", 4241e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->total_dead)); 4256b38d03fSArtem Bityutskiy pr_err("\ttotal_dark %llu\n", 4261e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->total_dark)); 4271e51764aSArtem Bityutskiy break; 4281e51764aSArtem Bityutskiy } 4291e51764aSArtem Bityutskiy case UBIFS_REF_NODE: 4301e51764aSArtem Bityutskiy { 4311e51764aSArtem Bityutskiy const struct ubifs_ref_node *ref = node; 4321e51764aSArtem Bityutskiy 4336b38d03fSArtem Bityutskiy pr_err("\tlnum %u\n", le32_to_cpu(ref->lnum)); 4346b38d03fSArtem Bityutskiy pr_err("\toffs %u\n", le32_to_cpu(ref->offs)); 4356b38d03fSArtem Bityutskiy pr_err("\tjhead %u\n", le32_to_cpu(ref->jhead)); 4361e51764aSArtem Bityutskiy break; 4371e51764aSArtem Bityutskiy } 4381e51764aSArtem Bityutskiy case UBIFS_INO_NODE: 4391e51764aSArtem Bityutskiy { 4401e51764aSArtem Bityutskiy const struct ubifs_ino_node *ino = node; 4411e51764aSArtem Bityutskiy 4421e51764aSArtem Bityutskiy key_read(c, &ino->key, &key); 4436b38d03fSArtem Bityutskiy pr_err("\tkey %s\n", 444515315a1SArtem Bityutskiy dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN)); 4456b38d03fSArtem Bityutskiy pr_err("\tcreat_sqnum %llu\n", 4461e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(ino->creat_sqnum)); 4476b38d03fSArtem Bityutskiy pr_err("\tsize %llu\n", 4481e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(ino->size)); 4496b38d03fSArtem Bityutskiy pr_err("\tnlink %u\n", le32_to_cpu(ino->nlink)); 4506b38d03fSArtem Bityutskiy pr_err("\tatime %lld.%u\n", 4511e51764aSArtem Bityutskiy (long long)le64_to_cpu(ino->atime_sec), 4521e51764aSArtem Bityutskiy le32_to_cpu(ino->atime_nsec)); 4536b38d03fSArtem Bityutskiy pr_err("\tmtime %lld.%u\n", 4541e51764aSArtem Bityutskiy (long long)le64_to_cpu(ino->mtime_sec), 4551e51764aSArtem Bityutskiy le32_to_cpu(ino->mtime_nsec)); 4566b38d03fSArtem Bityutskiy pr_err("\tctime %lld.%u\n", 4571e51764aSArtem Bityutskiy (long long)le64_to_cpu(ino->ctime_sec), 4581e51764aSArtem Bityutskiy le32_to_cpu(ino->ctime_nsec)); 4596b38d03fSArtem Bityutskiy pr_err("\tuid %u\n", le32_to_cpu(ino->uid)); 4606b38d03fSArtem Bityutskiy pr_err("\tgid %u\n", le32_to_cpu(ino->gid)); 4616b38d03fSArtem Bityutskiy pr_err("\tmode %u\n", le32_to_cpu(ino->mode)); 4626b38d03fSArtem Bityutskiy pr_err("\tflags %#x\n", le32_to_cpu(ino->flags)); 4636b38d03fSArtem Bityutskiy pr_err("\txattr_cnt %u\n", le32_to_cpu(ino->xattr_cnt)); 4646b38d03fSArtem Bityutskiy pr_err("\txattr_size %u\n", le32_to_cpu(ino->xattr_size)); 4656b38d03fSArtem Bityutskiy pr_err("\txattr_names %u\n", le32_to_cpu(ino->xattr_names)); 4666b38d03fSArtem Bityutskiy pr_err("\tcompr_type %#x\n", 4671e51764aSArtem Bityutskiy (int)le16_to_cpu(ino->compr_type)); 4686b38d03fSArtem Bityutskiy pr_err("\tdata len %u\n", le32_to_cpu(ino->data_len)); 4691e51764aSArtem Bityutskiy break; 4701e51764aSArtem Bityutskiy } 4711e51764aSArtem Bityutskiy case UBIFS_DENT_NODE: 4721e51764aSArtem Bityutskiy case UBIFS_XENT_NODE: 4731e51764aSArtem Bityutskiy { 4741e51764aSArtem Bityutskiy const struct ubifs_dent_node *dent = node; 4751e51764aSArtem Bityutskiy int nlen = le16_to_cpu(dent->nlen); 4761e51764aSArtem Bityutskiy 4771e51764aSArtem Bityutskiy key_read(c, &dent->key, &key); 4786b38d03fSArtem Bityutskiy pr_err("\tkey %s\n", 479515315a1SArtem Bityutskiy dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN)); 4806b38d03fSArtem Bityutskiy pr_err("\tinum %llu\n", 4811e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(dent->inum)); 4826b38d03fSArtem Bityutskiy pr_err("\ttype %d\n", (int)dent->type); 4836b38d03fSArtem Bityutskiy pr_err("\tnlen %d\n", nlen); 4846b38d03fSArtem Bityutskiy pr_err("\tname "); 4851e51764aSArtem Bityutskiy 486c4c0d19dSZhihao Cheng if (nlen > UBIFS_MAX_NLEN || 487c4c0d19dSZhihao Cheng nlen > safe_len - UBIFS_DENT_NODE_SZ) 4886b38d03fSArtem Bityutskiy pr_err("(bad name length, not printing, bad or corrupted node)"); 4891e51764aSArtem Bityutskiy else { 4901e51764aSArtem Bityutskiy for (i = 0; i < nlen && dent->name[i]; i++) 491e328379aSHyunchul Lee pr_cont("%c", isprint(dent->name[i]) ? 492e328379aSHyunchul Lee dent->name[i] : '?'); 4931e51764aSArtem Bityutskiy } 4946b38d03fSArtem Bityutskiy pr_cont("\n"); 4951e51764aSArtem Bityutskiy 4961e51764aSArtem Bityutskiy break; 4971e51764aSArtem Bityutskiy } 4981e51764aSArtem Bityutskiy case UBIFS_DATA_NODE: 4991e51764aSArtem Bityutskiy { 5001e51764aSArtem Bityutskiy const struct ubifs_data_node *dn = node; 5011e51764aSArtem Bityutskiy 5021e51764aSArtem Bityutskiy key_read(c, &dn->key, &key); 5036b38d03fSArtem Bityutskiy pr_err("\tkey %s\n", 504515315a1SArtem Bityutskiy dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN)); 5056b38d03fSArtem Bityutskiy pr_err("\tsize %u\n", le32_to_cpu(dn->size)); 5066b38d03fSArtem Bityutskiy pr_err("\tcompr_typ %d\n", 5071e51764aSArtem Bityutskiy (int)le16_to_cpu(dn->compr_type)); 508c4c0d19dSZhihao Cheng pr_err("\tdata size %u\n", 509c4c0d19dSZhihao Cheng le32_to_cpu(ch->len) - (unsigned int)UBIFS_DATA_NODE_SZ); 510c4c0d19dSZhihao Cheng pr_err("\tdata (length = %d):\n", 511c4c0d19dSZhihao Cheng safe_len - (int)UBIFS_DATA_NODE_SZ); 51216c395caSArtem Bityutskiy print_hex_dump(KERN_ERR, "\t", DUMP_PREFIX_OFFSET, 32, 1, 513c4c0d19dSZhihao Cheng (void *)&dn->data, 514c4c0d19dSZhihao Cheng safe_len - (int)UBIFS_DATA_NODE_SZ, 0); 5151e51764aSArtem Bityutskiy break; 5161e51764aSArtem Bityutskiy } 5171e51764aSArtem Bityutskiy case UBIFS_TRUN_NODE: 5181e51764aSArtem Bityutskiy { 5191e51764aSArtem Bityutskiy const struct ubifs_trun_node *trun = node; 5201e51764aSArtem Bityutskiy 5216b38d03fSArtem Bityutskiy pr_err("\tinum %u\n", le32_to_cpu(trun->inum)); 5226b38d03fSArtem Bityutskiy pr_err("\told_size %llu\n", 5231e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(trun->old_size)); 5246b38d03fSArtem Bityutskiy pr_err("\tnew_size %llu\n", 5251e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(trun->new_size)); 5261e51764aSArtem Bityutskiy break; 5271e51764aSArtem Bityutskiy } 5281e51764aSArtem Bityutskiy case UBIFS_IDX_NODE: 5291e51764aSArtem Bityutskiy { 5301e51764aSArtem Bityutskiy const struct ubifs_idx_node *idx = node; 531c4c0d19dSZhihao Cheng int max_child_cnt = (safe_len - UBIFS_IDX_NODE_SZ) / 532c4c0d19dSZhihao Cheng (ubifs_idx_node_sz(c, 1) - 533c4c0d19dSZhihao Cheng UBIFS_IDX_NODE_SZ); 5341e51764aSArtem Bityutskiy 535c4c0d19dSZhihao Cheng n = min_t(int, le16_to_cpu(idx->child_cnt), max_child_cnt); 536c4c0d19dSZhihao Cheng pr_err("\tchild_cnt %d\n", (int)le16_to_cpu(idx->child_cnt)); 5376b38d03fSArtem Bityutskiy pr_err("\tlevel %d\n", (int)le16_to_cpu(idx->level)); 5386b38d03fSArtem Bityutskiy pr_err("\tBranches:\n"); 5391e51764aSArtem Bityutskiy 540b80a974bSZhihao Cheng for (i = 0; i < n && i < c->fanout; i++) { 5411e51764aSArtem Bityutskiy const struct ubifs_branch *br; 5421e51764aSArtem Bityutskiy 5431e51764aSArtem Bityutskiy br = ubifs_idx_branch(c, idx, i); 5441e51764aSArtem Bityutskiy key_read(c, &br->key, &key); 5456b38d03fSArtem Bityutskiy pr_err("\t%d: LEB %d:%d len %d key %s\n", 5461e51764aSArtem Bityutskiy i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs), 547515315a1SArtem Bityutskiy le32_to_cpu(br->len), 548515315a1SArtem Bityutskiy dbg_snprintf_key(c, &key, key_buf, 549515315a1SArtem Bityutskiy DBG_KEY_BUF_LEN)); 5501e51764aSArtem Bityutskiy } 5511e51764aSArtem Bityutskiy break; 5521e51764aSArtem Bityutskiy } 5531e51764aSArtem Bityutskiy case UBIFS_CS_NODE: 5541e51764aSArtem Bityutskiy break; 5551e51764aSArtem Bityutskiy case UBIFS_ORPH_NODE: 5561e51764aSArtem Bityutskiy { 5571e51764aSArtem Bityutskiy const struct ubifs_orph_node *orph = node; 5581e51764aSArtem Bityutskiy 5596b38d03fSArtem Bityutskiy pr_err("\tcommit number %llu\n", 5601e51764aSArtem Bityutskiy (unsigned long long) 5611e51764aSArtem Bityutskiy le64_to_cpu(orph->cmt_no) & LLONG_MAX); 5626b38d03fSArtem Bityutskiy pr_err("\tlast node flag %llu\n", 5631e51764aSArtem Bityutskiy (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63); 564c4c0d19dSZhihao Cheng n = (safe_len - UBIFS_ORPH_NODE_SZ) >> 3; 5656b38d03fSArtem Bityutskiy pr_err("\t%d orphan inode numbers:\n", n); 5661e51764aSArtem Bityutskiy for (i = 0; i < n; i++) 5676b38d03fSArtem Bityutskiy pr_err("\t ino %llu\n", 5687424bac8SAlexander Beregalov (unsigned long long)le64_to_cpu(orph->inos[i])); 5691e51764aSArtem Bityutskiy break; 5701e51764aSArtem Bityutskiy } 5715125cfdfSSascha Hauer case UBIFS_AUTH_NODE: 5725125cfdfSSascha Hauer { 5735125cfdfSSascha Hauer break; 5745125cfdfSSascha Hauer } 5751e51764aSArtem Bityutskiy default: 576c4c0d19dSZhihao Cheng pr_err("node type %d was not recognized\n", type); 5771e51764aSArtem Bityutskiy } 578c4c0d19dSZhihao Cheng 579c4c0d19dSZhihao Cheng out_unlock: 5801e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 5811e51764aSArtem Bityutskiy } 5821e51764aSArtem Bityutskiy 583edf6be24SArtem Bityutskiy void ubifs_dump_budget_req(const struct ubifs_budget_req *req) 5841e51764aSArtem Bityutskiy { 5851e51764aSArtem Bityutskiy spin_lock(&dbg_lock); 5866b38d03fSArtem Bityutskiy pr_err("Budgeting request: new_ino %d, dirtied_ino %d\n", 5871e51764aSArtem Bityutskiy req->new_ino, req->dirtied_ino); 5886b38d03fSArtem Bityutskiy pr_err("\tnew_ino_d %d, dirtied_ino_d %d\n", 5891e51764aSArtem Bityutskiy req->new_ino_d, req->dirtied_ino_d); 5906b38d03fSArtem Bityutskiy pr_err("\tnew_page %d, dirtied_page %d\n", 5911e51764aSArtem Bityutskiy req->new_page, req->dirtied_page); 5926b38d03fSArtem Bityutskiy pr_err("\tnew_dent %d, mod_dent %d\n", 5931e51764aSArtem Bityutskiy req->new_dent, req->mod_dent); 5946b38d03fSArtem Bityutskiy pr_err("\tidx_growth %d\n", req->idx_growth); 5956b38d03fSArtem Bityutskiy pr_err("\tdata_growth %d dd_growth %d\n", 5961e51764aSArtem Bityutskiy req->data_growth, req->dd_growth); 5971e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 5981e51764aSArtem Bityutskiy } 5991e51764aSArtem Bityutskiy 600edf6be24SArtem Bityutskiy void ubifs_dump_lstats(const struct ubifs_lp_stats *lst) 6011e51764aSArtem Bityutskiy { 6021e51764aSArtem Bityutskiy spin_lock(&dbg_lock); 6036b38d03fSArtem Bityutskiy pr_err("(pid %d) Lprops statistics: empty_lebs %d, idx_lebs %d\n", 60479fda517SArtem Bityutskiy current->pid, lst->empty_lebs, lst->idx_lebs); 6056b38d03fSArtem Bityutskiy pr_err("\ttaken_empty_lebs %d, total_free %lld, total_dirty %lld\n", 60679fda517SArtem Bityutskiy lst->taken_empty_lebs, lst->total_free, lst->total_dirty); 6076b38d03fSArtem Bityutskiy pr_err("\ttotal_used %lld, total_dark %lld, total_dead %lld\n", 60879fda517SArtem Bityutskiy lst->total_used, lst->total_dark, lst->total_dead); 6091e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 6101e51764aSArtem Bityutskiy } 6111e51764aSArtem Bityutskiy 612edf6be24SArtem Bityutskiy void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi) 6131e51764aSArtem Bityutskiy { 6141e51764aSArtem Bityutskiy int i; 6151e51764aSArtem Bityutskiy struct rb_node *rb; 6161e51764aSArtem Bityutskiy struct ubifs_bud *bud; 6171e51764aSArtem Bityutskiy struct ubifs_gced_idx_leb *idx_gc; 61821a60258SArtem Bityutskiy long long available, outstanding, free; 6191e51764aSArtem Bityutskiy 6208ff83089SArtem Bityutskiy spin_lock(&c->space_lock); 6211e51764aSArtem Bityutskiy spin_lock(&dbg_lock); 6226b38d03fSArtem Bityutskiy pr_err("(pid %d) Budgeting info: data budget sum %lld, total budget sum %lld\n", 62379fda517SArtem Bityutskiy current->pid, bi->data_growth + bi->dd_growth, 624f1bd66afSArtem Bityutskiy bi->data_growth + bi->dd_growth + bi->idx_growth); 6256b38d03fSArtem Bityutskiy pr_err("\tbudg_data_growth %lld, budg_dd_growth %lld, budg_idx_growth %lld\n", 62679fda517SArtem Bityutskiy bi->data_growth, bi->dd_growth, bi->idx_growth); 6276b38d03fSArtem Bityutskiy pr_err("\tmin_idx_lebs %d, old_idx_sz %llu, uncommitted_idx %lld\n", 62879fda517SArtem Bityutskiy bi->min_idx_lebs, bi->old_idx_sz, bi->uncommitted_idx); 6296b38d03fSArtem Bityutskiy pr_err("\tpage_budget %d, inode_budget %d, dent_budget %d\n", 630f1bd66afSArtem Bityutskiy bi->page_budget, bi->inode_budget, bi->dent_budget); 6316b38d03fSArtem Bityutskiy pr_err("\tnospace %u, nospace_rp %u\n", bi->nospace, bi->nospace_rp); 6326b38d03fSArtem Bityutskiy pr_err("\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n", 6338c3067e4SArtem Bityutskiy c->dark_wm, c->dead_wm, c->max_idx_node_sz); 634f1bd66afSArtem Bityutskiy 635f1bd66afSArtem Bityutskiy if (bi != &c->bi) 636f1bd66afSArtem Bityutskiy /* 637f1bd66afSArtem Bityutskiy * If we are dumping saved budgeting data, do not print 638f1bd66afSArtem Bityutskiy * additional information which is about the current state, not 639f1bd66afSArtem Bityutskiy * the old one which corresponded to the saved budgeting data. 640f1bd66afSArtem Bityutskiy */ 641f1bd66afSArtem Bityutskiy goto out_unlock; 642f1bd66afSArtem Bityutskiy 6436b38d03fSArtem Bityutskiy pr_err("\tfreeable_cnt %d, calc_idx_sz %lld, idx_gc_cnt %d\n", 6448c3067e4SArtem Bityutskiy c->freeable_cnt, c->calc_idx_sz, c->idx_gc_cnt); 6456b38d03fSArtem Bityutskiy pr_err("\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, clean_zn_cnt %ld\n", 64679fda517SArtem Bityutskiy atomic_long_read(&c->dirty_pg_cnt), 6471e51764aSArtem Bityutskiy atomic_long_read(&c->dirty_zn_cnt), 6481e51764aSArtem Bityutskiy atomic_long_read(&c->clean_zn_cnt)); 6496b38d03fSArtem Bityutskiy pr_err("\tgc_lnum %d, ihead_lnum %d\n", c->gc_lnum, c->ihead_lnum); 650f1bd66afSArtem Bityutskiy 65184abf972SArtem Bityutskiy /* If we are in R/O mode, journal heads do not exist */ 65284abf972SArtem Bityutskiy if (c->jheads) 6531e51764aSArtem Bityutskiy for (i = 0; i < c->jhead_cnt; i++) 6546b38d03fSArtem Bityutskiy pr_err("\tjhead %s\t LEB %d\n", 65577a7ae58SArtem Bityutskiy dbg_jhead(c->jheads[i].wbuf.jhead), 65677a7ae58SArtem Bityutskiy c->jheads[i].wbuf.lnum); 6571e51764aSArtem Bityutskiy for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) { 6581e51764aSArtem Bityutskiy bud = rb_entry(rb, struct ubifs_bud, rb); 6596b38d03fSArtem Bityutskiy pr_err("\tbud LEB %d\n", bud->lnum); 6601e51764aSArtem Bityutskiy } 6611e51764aSArtem Bityutskiy list_for_each_entry(bud, &c->old_buds, list) 6626b38d03fSArtem Bityutskiy pr_err("\told bud LEB %d\n", bud->lnum); 6631e51764aSArtem Bityutskiy list_for_each_entry(idx_gc, &c->idx_gc, list) 6646b38d03fSArtem Bityutskiy pr_err("\tGC'ed idx LEB %d unmap %d\n", 6651e51764aSArtem Bityutskiy idx_gc->lnum, idx_gc->unmap); 6666b38d03fSArtem Bityutskiy pr_err("\tcommit state %d\n", c->cmt_state); 66721a60258SArtem Bityutskiy 66821a60258SArtem Bityutskiy /* Print budgeting predictions */ 669b137545cSArtem Bityutskiy available = ubifs_calc_available(c, c->bi.min_idx_lebs); 670b137545cSArtem Bityutskiy outstanding = c->bi.data_growth + c->bi.dd_growth; 67184abf972SArtem Bityutskiy free = ubifs_get_free_space_nolock(c); 6726b38d03fSArtem Bityutskiy pr_err("Budgeting predictions:\n"); 6736b38d03fSArtem Bityutskiy pr_err("\tavailable: %lld, outstanding %lld, free %lld\n", 67421a60258SArtem Bityutskiy available, outstanding, free); 675f1bd66afSArtem Bityutskiy out_unlock: 6761e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 6778ff83089SArtem Bityutskiy spin_unlock(&c->space_lock); 6781e51764aSArtem Bityutskiy } 6791e51764aSArtem Bityutskiy 680edf6be24SArtem Bityutskiy void ubifs_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp) 6811e51764aSArtem Bityutskiy { 682be9e62a7SArtem Bityutskiy int i, spc, dark = 0, dead = 0; 683be9e62a7SArtem Bityutskiy struct rb_node *rb; 684be9e62a7SArtem Bityutskiy struct ubifs_bud *bud; 685be9e62a7SArtem Bityutskiy 686be9e62a7SArtem Bityutskiy spc = lp->free + lp->dirty; 687be9e62a7SArtem Bityutskiy if (spc < c->dead_wm) 688be9e62a7SArtem Bityutskiy dead = spc; 689be9e62a7SArtem Bityutskiy else 690be9e62a7SArtem Bityutskiy dark = ubifs_calc_dark(c, spc); 691be9e62a7SArtem Bityutskiy 692be9e62a7SArtem Bityutskiy if (lp->flags & LPROPS_INDEX) 6936b38d03fSArtem Bityutskiy pr_err("LEB %-7d free %-8d dirty %-8d used %-8d free + dirty %-8d flags %#x (", 69479fda517SArtem Bityutskiy lp->lnum, lp->free, lp->dirty, c->leb_size - spc, spc, 69579fda517SArtem Bityutskiy lp->flags); 696be9e62a7SArtem Bityutskiy else 6976b38d03fSArtem Bityutskiy pr_err("LEB %-7d free %-8d dirty %-8d used %-8d free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d flags %#-4x (", 69879fda517SArtem Bityutskiy lp->lnum, lp->free, lp->dirty, c->leb_size - spc, spc, 69979fda517SArtem Bityutskiy dark, dead, (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags); 700be9e62a7SArtem Bityutskiy 701be9e62a7SArtem Bityutskiy if (lp->flags & LPROPS_TAKEN) { 702be9e62a7SArtem Bityutskiy if (lp->flags & LPROPS_INDEX) 7036b38d03fSArtem Bityutskiy pr_cont("index, taken"); 704be9e62a7SArtem Bityutskiy else 7056b38d03fSArtem Bityutskiy pr_cont("taken"); 706be9e62a7SArtem Bityutskiy } else { 707be9e62a7SArtem Bityutskiy const char *s; 708be9e62a7SArtem Bityutskiy 709be9e62a7SArtem Bityutskiy if (lp->flags & LPROPS_INDEX) { 710be9e62a7SArtem Bityutskiy switch (lp->flags & LPROPS_CAT_MASK) { 711be9e62a7SArtem Bityutskiy case LPROPS_DIRTY_IDX: 712be9e62a7SArtem Bityutskiy s = "dirty index"; 713be9e62a7SArtem Bityutskiy break; 714be9e62a7SArtem Bityutskiy case LPROPS_FRDI_IDX: 715be9e62a7SArtem Bityutskiy s = "freeable index"; 716be9e62a7SArtem Bityutskiy break; 717be9e62a7SArtem Bityutskiy default: 718be9e62a7SArtem Bityutskiy s = "index"; 719be9e62a7SArtem Bityutskiy } 720be9e62a7SArtem Bityutskiy } else { 721be9e62a7SArtem Bityutskiy switch (lp->flags & LPROPS_CAT_MASK) { 722be9e62a7SArtem Bityutskiy case LPROPS_UNCAT: 723be9e62a7SArtem Bityutskiy s = "not categorized"; 724be9e62a7SArtem Bityutskiy break; 725be9e62a7SArtem Bityutskiy case LPROPS_DIRTY: 726be9e62a7SArtem Bityutskiy s = "dirty"; 727be9e62a7SArtem Bityutskiy break; 728be9e62a7SArtem Bityutskiy case LPROPS_FREE: 729be9e62a7SArtem Bityutskiy s = "free"; 730be9e62a7SArtem Bityutskiy break; 731be9e62a7SArtem Bityutskiy case LPROPS_EMPTY: 732be9e62a7SArtem Bityutskiy s = "empty"; 733be9e62a7SArtem Bityutskiy break; 734be9e62a7SArtem Bityutskiy case LPROPS_FREEABLE: 735be9e62a7SArtem Bityutskiy s = "freeable"; 736be9e62a7SArtem Bityutskiy break; 737be9e62a7SArtem Bityutskiy default: 738be9e62a7SArtem Bityutskiy s = NULL; 739be9e62a7SArtem Bityutskiy break; 740be9e62a7SArtem Bityutskiy } 741be9e62a7SArtem Bityutskiy } 7426b38d03fSArtem Bityutskiy pr_cont("%s", s); 743be9e62a7SArtem Bityutskiy } 744be9e62a7SArtem Bityutskiy 745be9e62a7SArtem Bityutskiy for (rb = rb_first((struct rb_root *)&c->buds); rb; rb = rb_next(rb)) { 746be9e62a7SArtem Bityutskiy bud = rb_entry(rb, struct ubifs_bud, rb); 747be9e62a7SArtem Bityutskiy if (bud->lnum == lp->lnum) { 748be9e62a7SArtem Bityutskiy int head = 0; 749be9e62a7SArtem Bityutskiy for (i = 0; i < c->jhead_cnt; i++) { 7501321657dSArtem Bityutskiy /* 7511321657dSArtem Bityutskiy * Note, if we are in R/O mode or in the middle 7521321657dSArtem Bityutskiy * of mounting/re-mounting, the write-buffers do 7531321657dSArtem Bityutskiy * not exist. 7541321657dSArtem Bityutskiy */ 7551321657dSArtem Bityutskiy if (c->jheads && 7561321657dSArtem Bityutskiy lp->lnum == c->jheads[i].wbuf.lnum) { 7576b38d03fSArtem Bityutskiy pr_cont(", jhead %s", dbg_jhead(i)); 758be9e62a7SArtem Bityutskiy head = 1; 759be9e62a7SArtem Bityutskiy } 760be9e62a7SArtem Bityutskiy } 761be9e62a7SArtem Bityutskiy if (!head) 7626b38d03fSArtem Bityutskiy pr_cont(", bud of jhead %s", 763be9e62a7SArtem Bityutskiy dbg_jhead(bud->jhead)); 764be9e62a7SArtem Bityutskiy } 765be9e62a7SArtem Bityutskiy } 766be9e62a7SArtem Bityutskiy if (lp->lnum == c->gc_lnum) 7676b38d03fSArtem Bityutskiy pr_cont(", GC LEB"); 7686b38d03fSArtem Bityutskiy pr_cont(")\n"); 7691e51764aSArtem Bityutskiy } 7701e51764aSArtem Bityutskiy 771edf6be24SArtem Bityutskiy void ubifs_dump_lprops(struct ubifs_info *c) 7721e51764aSArtem Bityutskiy { 7731e51764aSArtem Bityutskiy int lnum, err; 7741e51764aSArtem Bityutskiy struct ubifs_lprops lp; 7751e51764aSArtem Bityutskiy struct ubifs_lp_stats lst; 7761e51764aSArtem Bityutskiy 7776b38d03fSArtem Bityutskiy pr_err("(pid %d) start dumping LEB properties\n", current->pid); 7781e51764aSArtem Bityutskiy ubifs_get_lp_stats(c, &lst); 779edf6be24SArtem Bityutskiy ubifs_dump_lstats(&lst); 7801e51764aSArtem Bityutskiy 7811e51764aSArtem Bityutskiy for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) { 7821e51764aSArtem Bityutskiy err = ubifs_read_one_lp(c, lnum, &lp); 783dac36981Shujianyang if (err) { 784235c362bSSheng Yong ubifs_err(c, "cannot read lprops for LEB %d", lnum); 785dac36981Shujianyang continue; 786dac36981Shujianyang } 7871e51764aSArtem Bityutskiy 788edf6be24SArtem Bityutskiy ubifs_dump_lprop(c, &lp); 7891e51764aSArtem Bityutskiy } 7906b38d03fSArtem Bityutskiy pr_err("(pid %d) finish dumping LEB properties\n", current->pid); 7911e51764aSArtem Bityutskiy } 7921e51764aSArtem Bityutskiy 793edf6be24SArtem Bityutskiy void ubifs_dump_lpt_info(struct ubifs_info *c) 79473944a6dSAdrian Hunter { 79573944a6dSAdrian Hunter int i; 79673944a6dSAdrian Hunter 79773944a6dSAdrian Hunter spin_lock(&dbg_lock); 7986b38d03fSArtem Bityutskiy pr_err("(pid %d) dumping LPT information\n", current->pid); 7996b38d03fSArtem Bityutskiy pr_err("\tlpt_sz: %lld\n", c->lpt_sz); 8006b38d03fSArtem Bityutskiy pr_err("\tpnode_sz: %d\n", c->pnode_sz); 8016b38d03fSArtem Bityutskiy pr_err("\tnnode_sz: %d\n", c->nnode_sz); 8026b38d03fSArtem Bityutskiy pr_err("\tltab_sz: %d\n", c->ltab_sz); 8036b38d03fSArtem Bityutskiy pr_err("\tlsave_sz: %d\n", c->lsave_sz); 804f2124007SChengsong Ke pr_err("\tbig_lpt: %u\n", c->big_lpt); 8056b38d03fSArtem Bityutskiy pr_err("\tlpt_hght: %d\n", c->lpt_hght); 8066b38d03fSArtem Bityutskiy pr_err("\tpnode_cnt: %d\n", c->pnode_cnt); 8076b38d03fSArtem Bityutskiy pr_err("\tnnode_cnt: %d\n", c->nnode_cnt); 8086b38d03fSArtem Bityutskiy pr_err("\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt); 8096b38d03fSArtem Bityutskiy pr_err("\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt); 8106b38d03fSArtem Bityutskiy pr_err("\tlsave_cnt: %d\n", c->lsave_cnt); 8116b38d03fSArtem Bityutskiy pr_err("\tspace_bits: %d\n", c->space_bits); 8126b38d03fSArtem Bityutskiy pr_err("\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits); 8136b38d03fSArtem Bityutskiy pr_err("\tlpt_offs_bits: %d\n", c->lpt_offs_bits); 8146b38d03fSArtem Bityutskiy pr_err("\tlpt_spc_bits: %d\n", c->lpt_spc_bits); 8156b38d03fSArtem Bityutskiy pr_err("\tpcnt_bits: %d\n", c->pcnt_bits); 8166b38d03fSArtem Bityutskiy pr_err("\tlnum_bits: %d\n", c->lnum_bits); 8176b38d03fSArtem Bityutskiy pr_err("\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs); 8186b38d03fSArtem Bityutskiy pr_err("\tLPT head is at %d:%d\n", 81973944a6dSAdrian Hunter c->nhead_lnum, c->nhead_offs); 8206b38d03fSArtem Bityutskiy pr_err("\tLPT ltab is at %d:%d\n", c->ltab_lnum, c->ltab_offs); 82173944a6dSAdrian Hunter if (c->big_lpt) 8226b38d03fSArtem Bityutskiy pr_err("\tLPT lsave is at %d:%d\n", 82373944a6dSAdrian Hunter c->lsave_lnum, c->lsave_offs); 82473944a6dSAdrian Hunter for (i = 0; i < c->lpt_lebs; i++) 8256b38d03fSArtem Bityutskiy pr_err("\tLPT LEB %d free %d dirty %d tgc %d cmt %d\n", 82679fda517SArtem Bityutskiy i + c->lpt_first, c->ltab[i].free, c->ltab[i].dirty, 82779fda517SArtem Bityutskiy c->ltab[i].tgc, c->ltab[i].cmt); 82873944a6dSAdrian Hunter spin_unlock(&dbg_lock); 82973944a6dSAdrian Hunter } 83073944a6dSAdrian Hunter 831edf6be24SArtem Bityutskiy void ubifs_dump_leb(const struct ubifs_info *c, int lnum) 8321e51764aSArtem Bityutskiy { 8331e51764aSArtem Bityutskiy struct ubifs_scan_leb *sleb; 8341e51764aSArtem Bityutskiy struct ubifs_scan_node *snod; 83573d9aec3SArtem Bityutskiy void *buf; 8361e51764aSArtem Bityutskiy 8376b38d03fSArtem Bityutskiy pr_err("(pid %d) start dumping LEB %d\n", current->pid, lnum); 83873d9aec3SArtem Bityutskiy 83988dca4caSChristoph Hellwig buf = __vmalloc(c->leb_size, GFP_NOFS); 84073d9aec3SArtem Bityutskiy if (!buf) { 841235c362bSSheng Yong ubifs_err(c, "cannot allocate memory for dumping LEB %d", lnum); 84273d9aec3SArtem Bityutskiy return; 84373d9aec3SArtem Bityutskiy } 84473d9aec3SArtem Bityutskiy 84573d9aec3SArtem Bityutskiy sleb = ubifs_scan(c, lnum, 0, buf, 0); 8461e51764aSArtem Bityutskiy if (IS_ERR(sleb)) { 847235c362bSSheng Yong ubifs_err(c, "scan error %d", (int)PTR_ERR(sleb)); 84873d9aec3SArtem Bityutskiy goto out; 8491e51764aSArtem Bityutskiy } 8501e51764aSArtem Bityutskiy 8516b38d03fSArtem Bityutskiy pr_err("LEB %d has %d nodes ending at %d\n", lnum, 8521e51764aSArtem Bityutskiy sleb->nodes_cnt, sleb->endpt); 8531e51764aSArtem Bityutskiy 8541e51764aSArtem Bityutskiy list_for_each_entry(snod, &sleb->nodes, list) { 8551e51764aSArtem Bityutskiy cond_resched(); 8566b38d03fSArtem Bityutskiy pr_err("Dumping node at LEB %d:%d len %d\n", lnum, 8571e51764aSArtem Bityutskiy snod->offs, snod->len); 858a33e30a0SZhihao Cheng ubifs_dump_node(c, snod->node, c->leb_size - snod->offs); 8591e51764aSArtem Bityutskiy } 8601e51764aSArtem Bityutskiy 8616b38d03fSArtem Bityutskiy pr_err("(pid %d) finish dumping LEB %d\n", current->pid, lnum); 8621e51764aSArtem Bityutskiy ubifs_scan_destroy(sleb); 86373d9aec3SArtem Bityutskiy 86473d9aec3SArtem Bityutskiy out: 86573d9aec3SArtem Bityutskiy vfree(buf); 8661e51764aSArtem Bityutskiy return; 8671e51764aSArtem Bityutskiy } 8681e51764aSArtem Bityutskiy 869edf6be24SArtem Bityutskiy void ubifs_dump_znode(const struct ubifs_info *c, 8701e51764aSArtem Bityutskiy const struct ubifs_znode *znode) 8711e51764aSArtem Bityutskiy { 8721e51764aSArtem Bityutskiy int n; 8731e51764aSArtem Bityutskiy const struct ubifs_zbranch *zbr; 874515315a1SArtem Bityutskiy char key_buf[DBG_KEY_BUF_LEN]; 8751e51764aSArtem Bityutskiy 8761e51764aSArtem Bityutskiy spin_lock(&dbg_lock); 8771e51764aSArtem Bityutskiy if (znode->parent) 8781e51764aSArtem Bityutskiy zbr = &znode->parent->zbranch[znode->iip]; 8791e51764aSArtem Bityutskiy else 8801e51764aSArtem Bityutskiy zbr = &c->zroot; 8811e51764aSArtem Bityutskiy 8826b38d03fSArtem Bityutskiy pr_err("znode %p, LEB %d:%d len %d parent %p iip %d level %d child_cnt %d flags %lx\n", 88379fda517SArtem Bityutskiy znode, zbr->lnum, zbr->offs, zbr->len, znode->parent, znode->iip, 88479fda517SArtem Bityutskiy znode->level, znode->child_cnt, znode->flags); 8851e51764aSArtem Bityutskiy 8861e51764aSArtem Bityutskiy if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) { 8871e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 8881e51764aSArtem Bityutskiy return; 8891e51764aSArtem Bityutskiy } 8901e51764aSArtem Bityutskiy 8916b38d03fSArtem Bityutskiy pr_err("zbranches:\n"); 8921e51764aSArtem Bityutskiy for (n = 0; n < znode->child_cnt; n++) { 8931e51764aSArtem Bityutskiy zbr = &znode->zbranch[n]; 8941e51764aSArtem Bityutskiy if (znode->level > 0) 8956b38d03fSArtem Bityutskiy pr_err("\t%d: znode %p LEB %d:%d len %d key %s\n", 89679fda517SArtem Bityutskiy n, zbr->znode, zbr->lnum, zbr->offs, zbr->len, 89779fda517SArtem Bityutskiy dbg_snprintf_key(c, &zbr->key, key_buf, 898515315a1SArtem Bityutskiy DBG_KEY_BUF_LEN)); 8991e51764aSArtem Bityutskiy else 9006b38d03fSArtem Bityutskiy pr_err("\t%d: LNC %p LEB %d:%d len %d key %s\n", 90179fda517SArtem Bityutskiy n, zbr->znode, zbr->lnum, zbr->offs, zbr->len, 90279fda517SArtem Bityutskiy dbg_snprintf_key(c, &zbr->key, key_buf, 903515315a1SArtem Bityutskiy DBG_KEY_BUF_LEN)); 9041e51764aSArtem Bityutskiy } 9051e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 9061e51764aSArtem Bityutskiy } 9071e51764aSArtem Bityutskiy 908edf6be24SArtem Bityutskiy void ubifs_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat) 9091e51764aSArtem Bityutskiy { 9101e51764aSArtem Bityutskiy int i; 9111e51764aSArtem Bityutskiy 9126b38d03fSArtem Bityutskiy pr_err("(pid %d) start dumping heap cat %d (%d elements)\n", 9131de94159SArtem Bityutskiy current->pid, cat, heap->cnt); 9141e51764aSArtem Bityutskiy for (i = 0; i < heap->cnt; i++) { 9151e51764aSArtem Bityutskiy struct ubifs_lprops *lprops = heap->arr[i]; 9161e51764aSArtem Bityutskiy 9176b38d03fSArtem Bityutskiy pr_err("\t%d. LEB %d hpos %d free %d dirty %d flags %d\n", 91879fda517SArtem Bityutskiy i, lprops->lnum, lprops->hpos, lprops->free, 91979fda517SArtem Bityutskiy lprops->dirty, lprops->flags); 9201e51764aSArtem Bityutskiy } 9216b38d03fSArtem Bityutskiy pr_err("(pid %d) finish dumping heap\n", current->pid); 9221e51764aSArtem Bityutskiy } 9231e51764aSArtem Bityutskiy 924edf6be24SArtem Bityutskiy void ubifs_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode, 9251e51764aSArtem Bityutskiy struct ubifs_nnode *parent, int iip) 9261e51764aSArtem Bityutskiy { 9271e51764aSArtem Bityutskiy int i; 9281e51764aSArtem Bityutskiy 9296b38d03fSArtem Bityutskiy pr_err("(pid %d) dumping pnode:\n", current->pid); 9306b38d03fSArtem Bityutskiy pr_err("\taddress %zx parent %zx cnext %zx\n", 9311e51764aSArtem Bityutskiy (size_t)pnode, (size_t)parent, (size_t)pnode->cnext); 9326b38d03fSArtem Bityutskiy pr_err("\tflags %lu iip %d level %d num %d\n", 9331e51764aSArtem Bityutskiy pnode->flags, iip, pnode->level, pnode->num); 9341e51764aSArtem Bityutskiy for (i = 0; i < UBIFS_LPT_FANOUT; i++) { 9351e51764aSArtem Bityutskiy struct ubifs_lprops *lp = &pnode->lprops[i]; 9361e51764aSArtem Bityutskiy 9376b38d03fSArtem Bityutskiy pr_err("\t%d: free %d dirty %d flags %d lnum %d\n", 9381e51764aSArtem Bityutskiy i, lp->free, lp->dirty, lp->flags, lp->lnum); 9391e51764aSArtem Bityutskiy } 9401e51764aSArtem Bityutskiy } 9411e51764aSArtem Bityutskiy 942edf6be24SArtem Bityutskiy void ubifs_dump_tnc(struct ubifs_info *c) 9431e51764aSArtem Bityutskiy { 9441e51764aSArtem Bityutskiy struct ubifs_znode *znode; 9451e51764aSArtem Bityutskiy int level; 9461e51764aSArtem Bityutskiy 9476b38d03fSArtem Bityutskiy pr_err("\n"); 9486b38d03fSArtem Bityutskiy pr_err("(pid %d) start dumping TNC tree\n", current->pid); 9496eb61d58SRichard Weinberger znode = ubifs_tnc_levelorder_next(c, c->zroot.znode, NULL); 9501e51764aSArtem Bityutskiy level = znode->level; 9516b38d03fSArtem Bityutskiy pr_err("== Level %d ==\n", level); 9521e51764aSArtem Bityutskiy while (znode) { 9531e51764aSArtem Bityutskiy if (level != znode->level) { 9541e51764aSArtem Bityutskiy level = znode->level; 9556b38d03fSArtem Bityutskiy pr_err("== Level %d ==\n", level); 9561e51764aSArtem Bityutskiy } 957edf6be24SArtem Bityutskiy ubifs_dump_znode(c, znode); 9586eb61d58SRichard Weinberger znode = ubifs_tnc_levelorder_next(c, c->zroot.znode, znode); 9591e51764aSArtem Bityutskiy } 9606b38d03fSArtem Bityutskiy pr_err("(pid %d) finish dumping TNC tree\n", current->pid); 9611e51764aSArtem Bityutskiy } 9621e51764aSArtem Bityutskiy 9631e51764aSArtem Bityutskiy static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode, 9641e51764aSArtem Bityutskiy void *priv) 9651e51764aSArtem Bityutskiy { 966edf6be24SArtem Bityutskiy ubifs_dump_znode(c, znode); 9671e51764aSArtem Bityutskiy return 0; 9681e51764aSArtem Bityutskiy } 9691e51764aSArtem Bityutskiy 9701e51764aSArtem Bityutskiy /** 971edf6be24SArtem Bityutskiy * ubifs_dump_index - dump the on-flash index. 9721e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 9731e51764aSArtem Bityutskiy * 974edf6be24SArtem Bityutskiy * This function dumps whole UBIFS indexing B-tree, unlike 'ubifs_dump_tnc()' 9751e51764aSArtem Bityutskiy * which dumps only in-memory znodes and does not read znodes which from flash. 9761e51764aSArtem Bityutskiy */ 977edf6be24SArtem Bityutskiy void ubifs_dump_index(struct ubifs_info *c) 9781e51764aSArtem Bityutskiy { 9791e51764aSArtem Bityutskiy dbg_walk_index(c, NULL, dump_znode, NULL); 9801e51764aSArtem Bityutskiy } 9811e51764aSArtem Bityutskiy 9821e51764aSArtem Bityutskiy /** 98384abf972SArtem Bityutskiy * dbg_save_space_info - save information about flash space. 98484abf972SArtem Bityutskiy * @c: UBIFS file-system description object 98584abf972SArtem Bityutskiy * 98684abf972SArtem Bityutskiy * This function saves information about UBIFS free space, dirty space, etc, in 98784abf972SArtem Bityutskiy * order to check it later. 98884abf972SArtem Bityutskiy */ 98984abf972SArtem Bityutskiy void dbg_save_space_info(struct ubifs_info *c) 99084abf972SArtem Bityutskiy { 99184abf972SArtem Bityutskiy struct ubifs_debug_info *d = c->dbg; 9927da6443aSArtem Bityutskiy int freeable_cnt; 99384abf972SArtem Bityutskiy 99484abf972SArtem Bityutskiy spin_lock(&c->space_lock); 9957da6443aSArtem Bityutskiy memcpy(&d->saved_lst, &c->lst, sizeof(struct ubifs_lp_stats)); 996f1bd66afSArtem Bityutskiy memcpy(&d->saved_bi, &c->bi, sizeof(struct ubifs_budg_info)); 997f1bd66afSArtem Bityutskiy d->saved_idx_gc_cnt = c->idx_gc_cnt; 9987da6443aSArtem Bityutskiy 9997da6443aSArtem Bityutskiy /* 10007da6443aSArtem Bityutskiy * We use a dirty hack here and zero out @c->freeable_cnt, because it 10017da6443aSArtem Bityutskiy * affects the free space calculations, and UBIFS might not know about 10027da6443aSArtem Bityutskiy * all freeable eraseblocks. Indeed, we know about freeable eraseblocks 10037da6443aSArtem Bityutskiy * only when we read their lprops, and we do this only lazily, upon the 10047da6443aSArtem Bityutskiy * need. So at any given point of time @c->freeable_cnt might be not 10057da6443aSArtem Bityutskiy * exactly accurate. 10067da6443aSArtem Bityutskiy * 10077da6443aSArtem Bityutskiy * Just one example about the issue we hit when we did not zero 10087da6443aSArtem Bityutskiy * @c->freeable_cnt. 10097da6443aSArtem Bityutskiy * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the 10107da6443aSArtem Bityutskiy * amount of free space in @d->saved_free 10117da6443aSArtem Bityutskiy * 2. We re-mount R/W, which makes UBIFS to read the "lsave" 10127da6443aSArtem Bityutskiy * information from flash, where we cache LEBs from various 10137da6443aSArtem Bityutskiy * categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()' 10147da6443aSArtem Bityutskiy * -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()' 10157da6443aSArtem Bityutskiy * -> 'ubifs_get_pnode()' -> 'update_cats()' 10167da6443aSArtem Bityutskiy * -> 'ubifs_add_to_cat()'). 10177da6443aSArtem Bityutskiy * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt 10187da6443aSArtem Bityutskiy * becomes %1. 10197da6443aSArtem Bityutskiy * 4. We calculate the amount of free space when the re-mount is 10207da6443aSArtem Bityutskiy * finished in 'dbg_check_space_info()' and it does not match 10217da6443aSArtem Bityutskiy * @d->saved_free. 10227da6443aSArtem Bityutskiy */ 10237da6443aSArtem Bityutskiy freeable_cnt = c->freeable_cnt; 10247da6443aSArtem Bityutskiy c->freeable_cnt = 0; 102584abf972SArtem Bityutskiy d->saved_free = ubifs_get_free_space_nolock(c); 10267da6443aSArtem Bityutskiy c->freeable_cnt = freeable_cnt; 102784abf972SArtem Bityutskiy spin_unlock(&c->space_lock); 102884abf972SArtem Bityutskiy } 102984abf972SArtem Bityutskiy 103084abf972SArtem Bityutskiy /** 103184abf972SArtem Bityutskiy * dbg_check_space_info - check flash space information. 103284abf972SArtem Bityutskiy * @c: UBIFS file-system description object 103384abf972SArtem Bityutskiy * 103484abf972SArtem Bityutskiy * This function compares current flash space information with the information 103584abf972SArtem Bityutskiy * which was saved when the 'dbg_save_space_info()' function was called. 1036b8f1da98SRandy Dunlap * Returns zero if the information has not changed, and %-EINVAL if it has 103784abf972SArtem Bityutskiy * changed. 103884abf972SArtem Bityutskiy */ 103984abf972SArtem Bityutskiy int dbg_check_space_info(struct ubifs_info *c) 104084abf972SArtem Bityutskiy { 104184abf972SArtem Bityutskiy struct ubifs_debug_info *d = c->dbg; 104284abf972SArtem Bityutskiy struct ubifs_lp_stats lst; 10437da6443aSArtem Bityutskiy long long free; 10447da6443aSArtem Bityutskiy int freeable_cnt; 104584abf972SArtem Bityutskiy 104684abf972SArtem Bityutskiy spin_lock(&c->space_lock); 10477da6443aSArtem Bityutskiy freeable_cnt = c->freeable_cnt; 10487da6443aSArtem Bityutskiy c->freeable_cnt = 0; 10497da6443aSArtem Bityutskiy free = ubifs_get_free_space_nolock(c); 10507da6443aSArtem Bityutskiy c->freeable_cnt = freeable_cnt; 105184abf972SArtem Bityutskiy spin_unlock(&c->space_lock); 105284abf972SArtem Bityutskiy 105384abf972SArtem Bityutskiy if (free != d->saved_free) { 1054235c362bSSheng Yong ubifs_err(c, "free space changed from %lld to %lld", 105584abf972SArtem Bityutskiy d->saved_free, free); 105684abf972SArtem Bityutskiy goto out; 105784abf972SArtem Bityutskiy } 105884abf972SArtem Bityutskiy 105984abf972SArtem Bityutskiy return 0; 106084abf972SArtem Bityutskiy 106184abf972SArtem Bityutskiy out: 1062235c362bSSheng Yong ubifs_msg(c, "saved lprops statistics dump"); 1063edf6be24SArtem Bityutskiy ubifs_dump_lstats(&d->saved_lst); 1064235c362bSSheng Yong ubifs_msg(c, "saved budgeting info dump"); 1065edf6be24SArtem Bityutskiy ubifs_dump_budg(c, &d->saved_bi); 1066235c362bSSheng Yong ubifs_msg(c, "saved idx_gc_cnt %d", d->saved_idx_gc_cnt); 1067235c362bSSheng Yong ubifs_msg(c, "current lprops statistics dump"); 1068f1bd66afSArtem Bityutskiy ubifs_get_lp_stats(c, &lst); 1069edf6be24SArtem Bityutskiy ubifs_dump_lstats(&lst); 1070235c362bSSheng Yong ubifs_msg(c, "current budgeting info dump"); 1071edf6be24SArtem Bityutskiy ubifs_dump_budg(c, &c->bi); 107284abf972SArtem Bityutskiy dump_stack(); 107384abf972SArtem Bityutskiy return -EINVAL; 107484abf972SArtem Bityutskiy } 107584abf972SArtem Bityutskiy 107684abf972SArtem Bityutskiy /** 10771e51764aSArtem Bityutskiy * dbg_check_synced_i_size - check synchronized inode size. 1078d808efb4SArtem Bityutskiy * @c: UBIFS file-system description object 10791e51764aSArtem Bityutskiy * @inode: inode to check 10801e51764aSArtem Bityutskiy * 10811e51764aSArtem Bityutskiy * If inode is clean, synchronized inode size has to be equivalent to current 10821e51764aSArtem Bityutskiy * inode size. This function has to be called only for locked inodes (@i_mutex 10831e51764aSArtem Bityutskiy * has to be locked). Returns %0 if synchronized inode size if correct, and 10841e51764aSArtem Bityutskiy * %-EINVAL if not. 10851e51764aSArtem Bityutskiy */ 1086d808efb4SArtem Bityutskiy int dbg_check_synced_i_size(const struct ubifs_info *c, struct inode *inode) 10871e51764aSArtem Bityutskiy { 10881e51764aSArtem Bityutskiy int err = 0; 10891e51764aSArtem Bityutskiy struct ubifs_inode *ui = ubifs_inode(inode); 10901e51764aSArtem Bityutskiy 10912b1844a8SArtem Bityutskiy if (!dbg_is_chk_gen(c)) 10921e51764aSArtem Bityutskiy return 0; 10931e51764aSArtem Bityutskiy if (!S_ISREG(inode->i_mode)) 10941e51764aSArtem Bityutskiy return 0; 10951e51764aSArtem Bityutskiy 10961e51764aSArtem Bityutskiy mutex_lock(&ui->ui_mutex); 10971e51764aSArtem Bityutskiy spin_lock(&ui->ui_lock); 10981e51764aSArtem Bityutskiy if (ui->ui_size != ui->synced_i_size && !ui->dirty) { 1099235c362bSSheng Yong ubifs_err(c, "ui_size is %lld, synced_i_size is %lld, but inode is clean", 110079fda517SArtem Bityutskiy ui->ui_size, ui->synced_i_size); 1101235c362bSSheng Yong ubifs_err(c, "i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino, 11021e51764aSArtem Bityutskiy inode->i_mode, i_size_read(inode)); 11037c46d0aeSArtem Bityutskiy dump_stack(); 11041e51764aSArtem Bityutskiy err = -EINVAL; 11051e51764aSArtem Bityutskiy } 11061e51764aSArtem Bityutskiy spin_unlock(&ui->ui_lock); 11071e51764aSArtem Bityutskiy mutex_unlock(&ui->ui_mutex); 11081e51764aSArtem Bityutskiy return err; 11091e51764aSArtem Bityutskiy } 11101e51764aSArtem Bityutskiy 11111e51764aSArtem Bityutskiy /* 11121e51764aSArtem Bityutskiy * dbg_check_dir - check directory inode size and link count. 11131e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 11141e51764aSArtem Bityutskiy * @dir: the directory to calculate size for 11151e51764aSArtem Bityutskiy * @size: the result is returned here 11161e51764aSArtem Bityutskiy * 11171e51764aSArtem Bityutskiy * This function makes sure that directory size and link count are correct. 11181e51764aSArtem Bityutskiy * Returns zero in case of success and a negative error code in case of 11191e51764aSArtem Bityutskiy * failure. 11201e51764aSArtem Bityutskiy * 11211e51764aSArtem Bityutskiy * Note, it is good idea to make sure the @dir->i_mutex is locked before 11221e51764aSArtem Bityutskiy * calling this function. 11231e51764aSArtem Bityutskiy */ 11241b51e983SArtem Bityutskiy int dbg_check_dir(struct ubifs_info *c, const struct inode *dir) 11251e51764aSArtem Bityutskiy { 11261e51764aSArtem Bityutskiy unsigned int nlink = 2; 11271e51764aSArtem Bityutskiy union ubifs_key key; 11281e51764aSArtem Bityutskiy struct ubifs_dent_node *dent, *pdent = NULL; 1129f4f61d2cSRichard Weinberger struct fscrypt_name nm = {0}; 11301e51764aSArtem Bityutskiy loff_t size = UBIFS_INO_NODE_SZ; 11311e51764aSArtem Bityutskiy 11322b1844a8SArtem Bityutskiy if (!dbg_is_chk_gen(c)) 11331e51764aSArtem Bityutskiy return 0; 11341e51764aSArtem Bityutskiy 11351e51764aSArtem Bityutskiy if (!S_ISDIR(dir->i_mode)) 11361e51764aSArtem Bityutskiy return 0; 11371e51764aSArtem Bityutskiy 11381e51764aSArtem Bityutskiy lowest_dent_key(c, &key, dir->i_ino); 11391e51764aSArtem Bityutskiy while (1) { 11401e51764aSArtem Bityutskiy int err; 11411e51764aSArtem Bityutskiy 11421e51764aSArtem Bityutskiy dent = ubifs_tnc_next_ent(c, &key, &nm); 11431e51764aSArtem Bityutskiy if (IS_ERR(dent)) { 11441e51764aSArtem Bityutskiy err = PTR_ERR(dent); 11451e51764aSArtem Bityutskiy if (err == -ENOENT) 11461e51764aSArtem Bityutskiy break; 114758f6e78aSZhihao Cheng kfree(pdent); 11481e51764aSArtem Bityutskiy return err; 11491e51764aSArtem Bityutskiy } 11501e51764aSArtem Bityutskiy 1151f4f61d2cSRichard Weinberger fname_name(&nm) = dent->name; 1152f4f61d2cSRichard Weinberger fname_len(&nm) = le16_to_cpu(dent->nlen); 1153f4f61d2cSRichard Weinberger size += CALC_DENT_SIZE(fname_len(&nm)); 11541e51764aSArtem Bityutskiy if (dent->type == UBIFS_ITYPE_DIR) 11551e51764aSArtem Bityutskiy nlink += 1; 11561e51764aSArtem Bityutskiy kfree(pdent); 11571e51764aSArtem Bityutskiy pdent = dent; 11581e51764aSArtem Bityutskiy key_read(c, &dent->key, &key); 11591e51764aSArtem Bityutskiy } 11601e51764aSArtem Bityutskiy kfree(pdent); 11611e51764aSArtem Bityutskiy 11621e51764aSArtem Bityutskiy if (i_size_read(dir) != size) { 1163235c362bSSheng Yong ubifs_err(c, "directory inode %lu has size %llu, but calculated size is %llu", 116479fda517SArtem Bityutskiy dir->i_ino, (unsigned long long)i_size_read(dir), 11651e51764aSArtem Bityutskiy (unsigned long long)size); 1166edf6be24SArtem Bityutskiy ubifs_dump_inode(c, dir); 11671e51764aSArtem Bityutskiy dump_stack(); 11681e51764aSArtem Bityutskiy return -EINVAL; 11691e51764aSArtem Bityutskiy } 11701e51764aSArtem Bityutskiy if (dir->i_nlink != nlink) { 1171235c362bSSheng Yong ubifs_err(c, "directory inode %lu has nlink %u, but calculated nlink is %u", 117279fda517SArtem Bityutskiy dir->i_ino, dir->i_nlink, nlink); 1173edf6be24SArtem Bityutskiy ubifs_dump_inode(c, dir); 11741e51764aSArtem Bityutskiy dump_stack(); 11751e51764aSArtem Bityutskiy return -EINVAL; 11761e51764aSArtem Bityutskiy } 11771e51764aSArtem Bityutskiy 11781e51764aSArtem Bityutskiy return 0; 11791e51764aSArtem Bityutskiy } 11801e51764aSArtem Bityutskiy 11811e51764aSArtem Bityutskiy /** 11821e51764aSArtem Bityutskiy * dbg_check_key_order - make sure that colliding keys are properly ordered. 11831e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 11841e51764aSArtem Bityutskiy * @zbr1: first zbranch 11851e51764aSArtem Bityutskiy * @zbr2: following zbranch 11861e51764aSArtem Bityutskiy * 11871e51764aSArtem Bityutskiy * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of 11881e51764aSArtem Bityutskiy * names of the direntries/xentries which are referred by the keys. This 11891e51764aSArtem Bityutskiy * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes 11901e51764aSArtem Bityutskiy * sure the name of direntry/xentry referred by @zbr1 is less than 11911e51764aSArtem Bityutskiy * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not, 11921e51764aSArtem Bityutskiy * and a negative error code in case of failure. 11931e51764aSArtem Bityutskiy */ 11941e51764aSArtem Bityutskiy static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1, 11951e51764aSArtem Bityutskiy struct ubifs_zbranch *zbr2) 11961e51764aSArtem Bityutskiy { 11971e51764aSArtem Bityutskiy int err, nlen1, nlen2, cmp; 11981e51764aSArtem Bityutskiy struct ubifs_dent_node *dent1, *dent2; 11991e51764aSArtem Bityutskiy union ubifs_key key; 1200515315a1SArtem Bityutskiy char key_buf[DBG_KEY_BUF_LEN]; 12011e51764aSArtem Bityutskiy 12026eb61d58SRichard Weinberger ubifs_assert(c, !keys_cmp(c, &zbr1->key, &zbr2->key)); 12031e51764aSArtem Bityutskiy dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS); 12041e51764aSArtem Bityutskiy if (!dent1) 12051e51764aSArtem Bityutskiy return -ENOMEM; 12061e51764aSArtem Bityutskiy dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS); 12071e51764aSArtem Bityutskiy if (!dent2) { 12081e51764aSArtem Bityutskiy err = -ENOMEM; 12091e51764aSArtem Bityutskiy goto out_free; 12101e51764aSArtem Bityutskiy } 12111e51764aSArtem Bityutskiy 12121e51764aSArtem Bityutskiy err = ubifs_tnc_read_node(c, zbr1, dent1); 12131e51764aSArtem Bityutskiy if (err) 12141e51764aSArtem Bityutskiy goto out_free; 12151e51764aSArtem Bityutskiy err = ubifs_validate_entry(c, dent1); 12161e51764aSArtem Bityutskiy if (err) 12171e51764aSArtem Bityutskiy goto out_free; 12181e51764aSArtem Bityutskiy 12191e51764aSArtem Bityutskiy err = ubifs_tnc_read_node(c, zbr2, dent2); 12201e51764aSArtem Bityutskiy if (err) 12211e51764aSArtem Bityutskiy goto out_free; 12221e51764aSArtem Bityutskiy err = ubifs_validate_entry(c, dent2); 12231e51764aSArtem Bityutskiy if (err) 12241e51764aSArtem Bityutskiy goto out_free; 12251e51764aSArtem Bityutskiy 12261e51764aSArtem Bityutskiy /* Make sure node keys are the same as in zbranch */ 12271e51764aSArtem Bityutskiy err = 1; 12281e51764aSArtem Bityutskiy key_read(c, &dent1->key, &key); 12291e51764aSArtem Bityutskiy if (keys_cmp(c, &zbr1->key, &key)) { 1230235c362bSSheng Yong ubifs_err(c, "1st entry at %d:%d has key %s", zbr1->lnum, 1231515315a1SArtem Bityutskiy zbr1->offs, dbg_snprintf_key(c, &key, key_buf, 1232515315a1SArtem Bityutskiy DBG_KEY_BUF_LEN)); 1233235c362bSSheng Yong ubifs_err(c, "but it should have key %s according to tnc", 1234515315a1SArtem Bityutskiy dbg_snprintf_key(c, &zbr1->key, key_buf, 1235515315a1SArtem Bityutskiy DBG_KEY_BUF_LEN)); 1236a33e30a0SZhihao Cheng ubifs_dump_node(c, dent1, UBIFS_MAX_DENT_NODE_SZ); 12371e51764aSArtem Bityutskiy goto out_free; 12381e51764aSArtem Bityutskiy } 12391e51764aSArtem Bityutskiy 12401e51764aSArtem Bityutskiy key_read(c, &dent2->key, &key); 12411e51764aSArtem Bityutskiy if (keys_cmp(c, &zbr2->key, &key)) { 1242235c362bSSheng Yong ubifs_err(c, "2nd entry at %d:%d has key %s", zbr1->lnum, 1243515315a1SArtem Bityutskiy zbr1->offs, dbg_snprintf_key(c, &key, key_buf, 1244515315a1SArtem Bityutskiy DBG_KEY_BUF_LEN)); 1245235c362bSSheng Yong ubifs_err(c, "but it should have key %s according to tnc", 1246515315a1SArtem Bityutskiy dbg_snprintf_key(c, &zbr2->key, key_buf, 1247515315a1SArtem Bityutskiy DBG_KEY_BUF_LEN)); 1248a33e30a0SZhihao Cheng ubifs_dump_node(c, dent2, UBIFS_MAX_DENT_NODE_SZ); 12491e51764aSArtem Bityutskiy goto out_free; 12501e51764aSArtem Bityutskiy } 12511e51764aSArtem Bityutskiy 12521e51764aSArtem Bityutskiy nlen1 = le16_to_cpu(dent1->nlen); 12531e51764aSArtem Bityutskiy nlen2 = le16_to_cpu(dent2->nlen); 12541e51764aSArtem Bityutskiy 12551e51764aSArtem Bityutskiy cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2)); 12561e51764aSArtem Bityutskiy if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) { 12571e51764aSArtem Bityutskiy err = 0; 12581e51764aSArtem Bityutskiy goto out_free; 12591e51764aSArtem Bityutskiy } 12601e51764aSArtem Bityutskiy if (cmp == 0 && nlen1 == nlen2) 1261235c362bSSheng Yong ubifs_err(c, "2 xent/dent nodes with the same name"); 12621e51764aSArtem Bityutskiy else 1263235c362bSSheng Yong ubifs_err(c, "bad order of colliding key %s", 1264515315a1SArtem Bityutskiy dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN)); 12651e51764aSArtem Bityutskiy 1266235c362bSSheng Yong ubifs_msg(c, "first node at %d:%d\n", zbr1->lnum, zbr1->offs); 1267a33e30a0SZhihao Cheng ubifs_dump_node(c, dent1, UBIFS_MAX_DENT_NODE_SZ); 1268235c362bSSheng Yong ubifs_msg(c, "second node at %d:%d\n", zbr2->lnum, zbr2->offs); 1269a33e30a0SZhihao Cheng ubifs_dump_node(c, dent2, UBIFS_MAX_DENT_NODE_SZ); 12701e51764aSArtem Bityutskiy 12711e51764aSArtem Bityutskiy out_free: 12721e51764aSArtem Bityutskiy kfree(dent2); 12731e51764aSArtem Bityutskiy kfree(dent1); 12741e51764aSArtem Bityutskiy return err; 12751e51764aSArtem Bityutskiy } 12761e51764aSArtem Bityutskiy 12771e51764aSArtem Bityutskiy /** 12781e51764aSArtem Bityutskiy * dbg_check_znode - check if znode is all right. 12791e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 12801e51764aSArtem Bityutskiy * @zbr: zbranch which points to this znode 12811e51764aSArtem Bityutskiy * 12821e51764aSArtem Bityutskiy * This function makes sure that znode referred to by @zbr is all right. 12831e51764aSArtem Bityutskiy * Returns zero if it is, and %-EINVAL if it is not. 12841e51764aSArtem Bityutskiy */ 12851e51764aSArtem Bityutskiy static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr) 12861e51764aSArtem Bityutskiy { 12871e51764aSArtem Bityutskiy struct ubifs_znode *znode = zbr->znode; 12881e51764aSArtem Bityutskiy struct ubifs_znode *zp = znode->parent; 12891e51764aSArtem Bityutskiy int n, err, cmp; 12901e51764aSArtem Bityutskiy 12911e51764aSArtem Bityutskiy if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) { 12921e51764aSArtem Bityutskiy err = 1; 12931e51764aSArtem Bityutskiy goto out; 12941e51764aSArtem Bityutskiy } 12951e51764aSArtem Bityutskiy if (znode->level < 0) { 12961e51764aSArtem Bityutskiy err = 2; 12971e51764aSArtem Bityutskiy goto out; 12981e51764aSArtem Bityutskiy } 12991e51764aSArtem Bityutskiy if (znode->iip < 0 || znode->iip >= c->fanout) { 13001e51764aSArtem Bityutskiy err = 3; 13011e51764aSArtem Bityutskiy goto out; 13021e51764aSArtem Bityutskiy } 13031e51764aSArtem Bityutskiy 13041e51764aSArtem Bityutskiy if (zbr->len == 0) 13051e51764aSArtem Bityutskiy /* Only dirty zbranch may have no on-flash nodes */ 13061e51764aSArtem Bityutskiy if (!ubifs_zn_dirty(znode)) { 13071e51764aSArtem Bityutskiy err = 4; 13081e51764aSArtem Bityutskiy goto out; 13091e51764aSArtem Bityutskiy } 13101e51764aSArtem Bityutskiy 13111e51764aSArtem Bityutskiy if (ubifs_zn_dirty(znode)) { 13121e51764aSArtem Bityutskiy /* 13131e51764aSArtem Bityutskiy * If znode is dirty, its parent has to be dirty as well. The 13141e51764aSArtem Bityutskiy * order of the operation is important, so we have to have 13151e51764aSArtem Bityutskiy * memory barriers. 13161e51764aSArtem Bityutskiy */ 13171e51764aSArtem Bityutskiy smp_mb(); 13181e51764aSArtem Bityutskiy if (zp && !ubifs_zn_dirty(zp)) { 13191e51764aSArtem Bityutskiy /* 13201e51764aSArtem Bityutskiy * The dirty flag is atomic and is cleared outside the 13211e51764aSArtem Bityutskiy * TNC mutex, so znode's dirty flag may now have 13221e51764aSArtem Bityutskiy * been cleared. The child is always cleared before the 13231e51764aSArtem Bityutskiy * parent, so we just need to check again. 13241e51764aSArtem Bityutskiy */ 13251e51764aSArtem Bityutskiy smp_mb(); 13261e51764aSArtem Bityutskiy if (ubifs_zn_dirty(znode)) { 13271e51764aSArtem Bityutskiy err = 5; 13281e51764aSArtem Bityutskiy goto out; 13291e51764aSArtem Bityutskiy } 13301e51764aSArtem Bityutskiy } 13311e51764aSArtem Bityutskiy } 13321e51764aSArtem Bityutskiy 13331e51764aSArtem Bityutskiy if (zp) { 13341e51764aSArtem Bityutskiy const union ubifs_key *min, *max; 13351e51764aSArtem Bityutskiy 13361e51764aSArtem Bityutskiy if (znode->level != zp->level - 1) { 13371e51764aSArtem Bityutskiy err = 6; 13381e51764aSArtem Bityutskiy goto out; 13391e51764aSArtem Bityutskiy } 13401e51764aSArtem Bityutskiy 13411e51764aSArtem Bityutskiy /* Make sure the 'parent' pointer in our znode is correct */ 13421e51764aSArtem Bityutskiy err = ubifs_search_zbranch(c, zp, &zbr->key, &n); 13431e51764aSArtem Bityutskiy if (!err) { 13441e51764aSArtem Bityutskiy /* This zbranch does not exist in the parent */ 13451e51764aSArtem Bityutskiy err = 7; 13461e51764aSArtem Bityutskiy goto out; 13471e51764aSArtem Bityutskiy } 13481e51764aSArtem Bityutskiy 13491e51764aSArtem Bityutskiy if (znode->iip >= zp->child_cnt) { 13501e51764aSArtem Bityutskiy err = 8; 13511e51764aSArtem Bityutskiy goto out; 13521e51764aSArtem Bityutskiy } 13531e51764aSArtem Bityutskiy 13541e51764aSArtem Bityutskiy if (znode->iip != n) { 13551e51764aSArtem Bityutskiy /* This may happen only in case of collisions */ 13561e51764aSArtem Bityutskiy if (keys_cmp(c, &zp->zbranch[n].key, 13571e51764aSArtem Bityutskiy &zp->zbranch[znode->iip].key)) { 13581e51764aSArtem Bityutskiy err = 9; 13591e51764aSArtem Bityutskiy goto out; 13601e51764aSArtem Bityutskiy } 13611e51764aSArtem Bityutskiy n = znode->iip; 13621e51764aSArtem Bityutskiy } 13631e51764aSArtem Bityutskiy 13641e51764aSArtem Bityutskiy /* 13651e51764aSArtem Bityutskiy * Make sure that the first key in our znode is greater than or 13661e51764aSArtem Bityutskiy * equal to the key in the pointing zbranch. 13671e51764aSArtem Bityutskiy */ 13681e51764aSArtem Bityutskiy min = &zbr->key; 13691e51764aSArtem Bityutskiy cmp = keys_cmp(c, min, &znode->zbranch[0].key); 13701e51764aSArtem Bityutskiy if (cmp == 1) { 13711e51764aSArtem Bityutskiy err = 10; 13721e51764aSArtem Bityutskiy goto out; 13731e51764aSArtem Bityutskiy } 13741e51764aSArtem Bityutskiy 13751e51764aSArtem Bityutskiy if (n + 1 < zp->child_cnt) { 13761e51764aSArtem Bityutskiy max = &zp->zbranch[n + 1].key; 13771e51764aSArtem Bityutskiy 13781e51764aSArtem Bityutskiy /* 13791e51764aSArtem Bityutskiy * Make sure the last key in our znode is less or 13807d4e9ccbSArtem Bityutskiy * equivalent than the key in the zbranch which goes 13811e51764aSArtem Bityutskiy * after our pointing zbranch. 13821e51764aSArtem Bityutskiy */ 13831e51764aSArtem Bityutskiy cmp = keys_cmp(c, max, 13841e51764aSArtem Bityutskiy &znode->zbranch[znode->child_cnt - 1].key); 13851e51764aSArtem Bityutskiy if (cmp == -1) { 13861e51764aSArtem Bityutskiy err = 11; 13871e51764aSArtem Bityutskiy goto out; 13881e51764aSArtem Bityutskiy } 13891e51764aSArtem Bityutskiy } 13901e51764aSArtem Bityutskiy } else { 13911e51764aSArtem Bityutskiy /* This may only be root znode */ 13921e51764aSArtem Bityutskiy if (zbr != &c->zroot) { 13931e51764aSArtem Bityutskiy err = 12; 13941e51764aSArtem Bityutskiy goto out; 13951e51764aSArtem Bityutskiy } 13961e51764aSArtem Bityutskiy } 13971e51764aSArtem Bityutskiy 13981e51764aSArtem Bityutskiy /* 13991e51764aSArtem Bityutskiy * Make sure that next key is greater or equivalent then the previous 14001e51764aSArtem Bityutskiy * one. 14011e51764aSArtem Bityutskiy */ 14021e51764aSArtem Bityutskiy for (n = 1; n < znode->child_cnt; n++) { 14031e51764aSArtem Bityutskiy cmp = keys_cmp(c, &znode->zbranch[n - 1].key, 14041e51764aSArtem Bityutskiy &znode->zbranch[n].key); 14051e51764aSArtem Bityutskiy if (cmp > 0) { 14061e51764aSArtem Bityutskiy err = 13; 14071e51764aSArtem Bityutskiy goto out; 14081e51764aSArtem Bityutskiy } 14091e51764aSArtem Bityutskiy if (cmp == 0) { 14101e51764aSArtem Bityutskiy /* This can only be keys with colliding hash */ 14111e51764aSArtem Bityutskiy if (!is_hash_key(c, &znode->zbranch[n].key)) { 14121e51764aSArtem Bityutskiy err = 14; 14131e51764aSArtem Bityutskiy goto out; 14141e51764aSArtem Bityutskiy } 14151e51764aSArtem Bityutskiy 14161e51764aSArtem Bityutskiy if (znode->level != 0 || c->replaying) 14171e51764aSArtem Bityutskiy continue; 14181e51764aSArtem Bityutskiy 14191e51764aSArtem Bityutskiy /* 14201e51764aSArtem Bityutskiy * Colliding keys should follow binary order of 14211e51764aSArtem Bityutskiy * corresponding xentry/dentry names. 14221e51764aSArtem Bityutskiy */ 14231e51764aSArtem Bityutskiy err = dbg_check_key_order(c, &znode->zbranch[n - 1], 14241e51764aSArtem Bityutskiy &znode->zbranch[n]); 14251e51764aSArtem Bityutskiy if (err < 0) 14261e51764aSArtem Bityutskiy return err; 14271e51764aSArtem Bityutskiy if (err) { 14281e51764aSArtem Bityutskiy err = 15; 14291e51764aSArtem Bityutskiy goto out; 14301e51764aSArtem Bityutskiy } 14311e51764aSArtem Bityutskiy } 14321e51764aSArtem Bityutskiy } 14331e51764aSArtem Bityutskiy 14341e51764aSArtem Bityutskiy for (n = 0; n < znode->child_cnt; n++) { 14351e51764aSArtem Bityutskiy if (!znode->zbranch[n].znode && 14361e51764aSArtem Bityutskiy (znode->zbranch[n].lnum == 0 || 14371e51764aSArtem Bityutskiy znode->zbranch[n].len == 0)) { 14381e51764aSArtem Bityutskiy err = 16; 14391e51764aSArtem Bityutskiy goto out; 14401e51764aSArtem Bityutskiy } 14411e51764aSArtem Bityutskiy 14421e51764aSArtem Bityutskiy if (znode->zbranch[n].lnum != 0 && 14431e51764aSArtem Bityutskiy znode->zbranch[n].len == 0) { 14441e51764aSArtem Bityutskiy err = 17; 14451e51764aSArtem Bityutskiy goto out; 14461e51764aSArtem Bityutskiy } 14471e51764aSArtem Bityutskiy 14481e51764aSArtem Bityutskiy if (znode->zbranch[n].lnum == 0 && 14491e51764aSArtem Bityutskiy znode->zbranch[n].len != 0) { 14501e51764aSArtem Bityutskiy err = 18; 14511e51764aSArtem Bityutskiy goto out; 14521e51764aSArtem Bityutskiy } 14531e51764aSArtem Bityutskiy 14541e51764aSArtem Bityutskiy if (znode->zbranch[n].lnum == 0 && 14551e51764aSArtem Bityutskiy znode->zbranch[n].offs != 0) { 14561e51764aSArtem Bityutskiy err = 19; 14571e51764aSArtem Bityutskiy goto out; 14581e51764aSArtem Bityutskiy } 14591e51764aSArtem Bityutskiy 14601e51764aSArtem Bityutskiy if (znode->level != 0 && znode->zbranch[n].znode) 14611e51764aSArtem Bityutskiy if (znode->zbranch[n].znode->parent != znode) { 14621e51764aSArtem Bityutskiy err = 20; 14631e51764aSArtem Bityutskiy goto out; 14641e51764aSArtem Bityutskiy } 14651e51764aSArtem Bityutskiy } 14661e51764aSArtem Bityutskiy 14671e51764aSArtem Bityutskiy return 0; 14681e51764aSArtem Bityutskiy 14691e51764aSArtem Bityutskiy out: 1470235c362bSSheng Yong ubifs_err(c, "failed, error %d", err); 1471235c362bSSheng Yong ubifs_msg(c, "dump of the znode"); 1472edf6be24SArtem Bityutskiy ubifs_dump_znode(c, znode); 14731e51764aSArtem Bityutskiy if (zp) { 1474235c362bSSheng Yong ubifs_msg(c, "dump of the parent znode"); 1475edf6be24SArtem Bityutskiy ubifs_dump_znode(c, zp); 14761e51764aSArtem Bityutskiy } 14771e51764aSArtem Bityutskiy dump_stack(); 14781e51764aSArtem Bityutskiy return -EINVAL; 14791e51764aSArtem Bityutskiy } 14801e51764aSArtem Bityutskiy 14811e51764aSArtem Bityutskiy /** 14821e51764aSArtem Bityutskiy * dbg_check_tnc - check TNC tree. 14831e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 14841e51764aSArtem Bityutskiy * @extra: do extra checks that are possible at start commit 14851e51764aSArtem Bityutskiy * 14861e51764aSArtem Bityutskiy * This function traverses whole TNC tree and checks every znode. Returns zero 14871e51764aSArtem Bityutskiy * if everything is all right and %-EINVAL if something is wrong with TNC. 14881e51764aSArtem Bityutskiy */ 14891e51764aSArtem Bityutskiy int dbg_check_tnc(struct ubifs_info *c, int extra) 14901e51764aSArtem Bityutskiy { 14911e51764aSArtem Bityutskiy struct ubifs_znode *znode; 14921e51764aSArtem Bityutskiy long clean_cnt = 0, dirty_cnt = 0; 14931e51764aSArtem Bityutskiy int err, last; 14941e51764aSArtem Bityutskiy 14958d7819b4SArtem Bityutskiy if (!dbg_is_chk_index(c)) 14961e51764aSArtem Bityutskiy return 0; 14971e51764aSArtem Bityutskiy 14986eb61d58SRichard Weinberger ubifs_assert(c, mutex_is_locked(&c->tnc_mutex)); 14991e51764aSArtem Bityutskiy if (!c->zroot.znode) 15001e51764aSArtem Bityutskiy return 0; 15011e51764aSArtem Bityutskiy 15021e51764aSArtem Bityutskiy znode = ubifs_tnc_postorder_first(c->zroot.znode); 15031e51764aSArtem Bityutskiy while (1) { 15041e51764aSArtem Bityutskiy struct ubifs_znode *prev; 15051e51764aSArtem Bityutskiy struct ubifs_zbranch *zbr; 15061e51764aSArtem Bityutskiy 15071e51764aSArtem Bityutskiy if (!znode->parent) 15081e51764aSArtem Bityutskiy zbr = &c->zroot; 15091e51764aSArtem Bityutskiy else 15101e51764aSArtem Bityutskiy zbr = &znode->parent->zbranch[znode->iip]; 15111e51764aSArtem Bityutskiy 15121e51764aSArtem Bityutskiy err = dbg_check_znode(c, zbr); 15131e51764aSArtem Bityutskiy if (err) 15141e51764aSArtem Bityutskiy return err; 15151e51764aSArtem Bityutskiy 15161e51764aSArtem Bityutskiy if (extra) { 15171e51764aSArtem Bityutskiy if (ubifs_zn_dirty(znode)) 15181e51764aSArtem Bityutskiy dirty_cnt += 1; 15191e51764aSArtem Bityutskiy else 15201e51764aSArtem Bityutskiy clean_cnt += 1; 15211e51764aSArtem Bityutskiy } 15221e51764aSArtem Bityutskiy 15231e51764aSArtem Bityutskiy prev = znode; 15246eb61d58SRichard Weinberger znode = ubifs_tnc_postorder_next(c, znode); 15251e51764aSArtem Bityutskiy if (!znode) 15261e51764aSArtem Bityutskiy break; 15271e51764aSArtem Bityutskiy 15281e51764aSArtem Bityutskiy /* 15291e51764aSArtem Bityutskiy * If the last key of this znode is equivalent to the first key 15301e51764aSArtem Bityutskiy * of the next znode (collision), then check order of the keys. 15311e51764aSArtem Bityutskiy */ 15321e51764aSArtem Bityutskiy last = prev->child_cnt - 1; 15331e51764aSArtem Bityutskiy if (prev->level == 0 && znode->level == 0 && !c->replaying && 15341e51764aSArtem Bityutskiy !keys_cmp(c, &prev->zbranch[last].key, 15351e51764aSArtem Bityutskiy &znode->zbranch[0].key)) { 15361e51764aSArtem Bityutskiy err = dbg_check_key_order(c, &prev->zbranch[last], 15371e51764aSArtem Bityutskiy &znode->zbranch[0]); 15381e51764aSArtem Bityutskiy if (err < 0) 15391e51764aSArtem Bityutskiy return err; 15401e51764aSArtem Bityutskiy if (err) { 1541235c362bSSheng Yong ubifs_msg(c, "first znode"); 1542edf6be24SArtem Bityutskiy ubifs_dump_znode(c, prev); 1543235c362bSSheng Yong ubifs_msg(c, "second znode"); 1544edf6be24SArtem Bityutskiy ubifs_dump_znode(c, znode); 15451e51764aSArtem Bityutskiy return -EINVAL; 15461e51764aSArtem Bityutskiy } 15471e51764aSArtem Bityutskiy } 15481e51764aSArtem Bityutskiy } 15491e51764aSArtem Bityutskiy 15501e51764aSArtem Bityutskiy if (extra) { 15511e51764aSArtem Bityutskiy if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) { 1552235c362bSSheng Yong ubifs_err(c, "incorrect clean_zn_cnt %ld, calculated %ld", 15531e51764aSArtem Bityutskiy atomic_long_read(&c->clean_zn_cnt), 15541e51764aSArtem Bityutskiy clean_cnt); 15551e51764aSArtem Bityutskiy return -EINVAL; 15561e51764aSArtem Bityutskiy } 15571e51764aSArtem Bityutskiy if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) { 1558235c362bSSheng Yong ubifs_err(c, "incorrect dirty_zn_cnt %ld, calculated %ld", 15591e51764aSArtem Bityutskiy atomic_long_read(&c->dirty_zn_cnt), 15601e51764aSArtem Bityutskiy dirty_cnt); 15611e51764aSArtem Bityutskiy return -EINVAL; 15621e51764aSArtem Bityutskiy } 15631e51764aSArtem Bityutskiy } 15641e51764aSArtem Bityutskiy 15651e51764aSArtem Bityutskiy return 0; 15661e51764aSArtem Bityutskiy } 15671e51764aSArtem Bityutskiy 15681e51764aSArtem Bityutskiy /** 15691e51764aSArtem Bityutskiy * dbg_walk_index - walk the on-flash index. 15701e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 15711e51764aSArtem Bityutskiy * @leaf_cb: called for each leaf node 15721e51764aSArtem Bityutskiy * @znode_cb: called for each indexing node 1573227c75c9SAdrian Hunter * @priv: private data which is passed to callbacks 15741e51764aSArtem Bityutskiy * 15751e51764aSArtem Bityutskiy * This function walks the UBIFS index and calls the @leaf_cb for each leaf 15761e51764aSArtem Bityutskiy * node and @znode_cb for each indexing node. Returns zero in case of success 15771e51764aSArtem Bityutskiy * and a negative error code in case of failure. 15781e51764aSArtem Bityutskiy * 15791e51764aSArtem Bityutskiy * It would be better if this function removed every znode it pulled to into 15801e51764aSArtem Bityutskiy * the TNC, so that the behavior more closely matched the non-debugging 15811e51764aSArtem Bityutskiy * behavior. 15821e51764aSArtem Bityutskiy */ 15831e51764aSArtem Bityutskiy int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb, 15841e51764aSArtem Bityutskiy dbg_znode_callback znode_cb, void *priv) 15851e51764aSArtem Bityutskiy { 15861e51764aSArtem Bityutskiy int err; 15871e51764aSArtem Bityutskiy struct ubifs_zbranch *zbr; 15881e51764aSArtem Bityutskiy struct ubifs_znode *znode, *child; 15891e51764aSArtem Bityutskiy 15901e51764aSArtem Bityutskiy mutex_lock(&c->tnc_mutex); 15911e51764aSArtem Bityutskiy /* If the root indexing node is not in TNC - pull it */ 15921e51764aSArtem Bityutskiy if (!c->zroot.znode) { 15931e51764aSArtem Bityutskiy c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0); 15941e51764aSArtem Bityutskiy if (IS_ERR(c->zroot.znode)) { 15951e51764aSArtem Bityutskiy err = PTR_ERR(c->zroot.znode); 15961e51764aSArtem Bityutskiy c->zroot.znode = NULL; 15971e51764aSArtem Bityutskiy goto out_unlock; 15981e51764aSArtem Bityutskiy } 15991e51764aSArtem Bityutskiy } 16001e51764aSArtem Bityutskiy 16011e51764aSArtem Bityutskiy /* 16021e51764aSArtem Bityutskiy * We are going to traverse the indexing tree in the postorder manner. 16031e51764aSArtem Bityutskiy * Go down and find the leftmost indexing node where we are going to 16041e51764aSArtem Bityutskiy * start from. 16051e51764aSArtem Bityutskiy */ 16061e51764aSArtem Bityutskiy znode = c->zroot.znode; 16071e51764aSArtem Bityutskiy while (znode->level > 0) { 16081e51764aSArtem Bityutskiy zbr = &znode->zbranch[0]; 16091e51764aSArtem Bityutskiy child = zbr->znode; 16101e51764aSArtem Bityutskiy if (!child) { 16111e51764aSArtem Bityutskiy child = ubifs_load_znode(c, zbr, znode, 0); 16121e51764aSArtem Bityutskiy if (IS_ERR(child)) { 16131e51764aSArtem Bityutskiy err = PTR_ERR(child); 16141e51764aSArtem Bityutskiy goto out_unlock; 16151e51764aSArtem Bityutskiy } 16161e51764aSArtem Bityutskiy } 16171e51764aSArtem Bityutskiy 16181e51764aSArtem Bityutskiy znode = child; 16191e51764aSArtem Bityutskiy } 16201e51764aSArtem Bityutskiy 16211e51764aSArtem Bityutskiy /* Iterate over all indexing nodes */ 16221e51764aSArtem Bityutskiy while (1) { 16231e51764aSArtem Bityutskiy int idx; 16241e51764aSArtem Bityutskiy 16251e51764aSArtem Bityutskiy cond_resched(); 16261e51764aSArtem Bityutskiy 16271e51764aSArtem Bityutskiy if (znode_cb) { 16281e51764aSArtem Bityutskiy err = znode_cb(c, znode, priv); 16291e51764aSArtem Bityutskiy if (err) { 1630235c362bSSheng Yong ubifs_err(c, "znode checking function returned error %d", 163179fda517SArtem Bityutskiy err); 1632edf6be24SArtem Bityutskiy ubifs_dump_znode(c, znode); 16331e51764aSArtem Bityutskiy goto out_dump; 16341e51764aSArtem Bityutskiy } 16351e51764aSArtem Bityutskiy } 16361e51764aSArtem Bityutskiy if (leaf_cb && znode->level == 0) { 16371e51764aSArtem Bityutskiy for (idx = 0; idx < znode->child_cnt; idx++) { 16381e51764aSArtem Bityutskiy zbr = &znode->zbranch[idx]; 16391e51764aSArtem Bityutskiy err = leaf_cb(c, zbr, priv); 16401e51764aSArtem Bityutskiy if (err) { 1641235c362bSSheng Yong ubifs_err(c, "leaf checking function returned error %d, for leaf at LEB %d:%d", 16421e51764aSArtem Bityutskiy err, zbr->lnum, zbr->offs); 16431e51764aSArtem Bityutskiy goto out_dump; 16441e51764aSArtem Bityutskiy } 16451e51764aSArtem Bityutskiy } 16461e51764aSArtem Bityutskiy } 16471e51764aSArtem Bityutskiy 16481e51764aSArtem Bityutskiy if (!znode->parent) 16491e51764aSArtem Bityutskiy break; 16501e51764aSArtem Bityutskiy 16511e51764aSArtem Bityutskiy idx = znode->iip + 1; 16521e51764aSArtem Bityutskiy znode = znode->parent; 16531e51764aSArtem Bityutskiy if (idx < znode->child_cnt) { 16541e51764aSArtem Bityutskiy /* Switch to the next index in the parent */ 16551e51764aSArtem Bityutskiy zbr = &znode->zbranch[idx]; 16561e51764aSArtem Bityutskiy child = zbr->znode; 16571e51764aSArtem Bityutskiy if (!child) { 16581e51764aSArtem Bityutskiy child = ubifs_load_znode(c, zbr, znode, idx); 16591e51764aSArtem Bityutskiy if (IS_ERR(child)) { 16601e51764aSArtem Bityutskiy err = PTR_ERR(child); 16611e51764aSArtem Bityutskiy goto out_unlock; 16621e51764aSArtem Bityutskiy } 16631e51764aSArtem Bityutskiy zbr->znode = child; 16641e51764aSArtem Bityutskiy } 16651e51764aSArtem Bityutskiy znode = child; 16661e51764aSArtem Bityutskiy } else 16671e51764aSArtem Bityutskiy /* 16681e51764aSArtem Bityutskiy * This is the last child, switch to the parent and 16691e51764aSArtem Bityutskiy * continue. 16701e51764aSArtem Bityutskiy */ 16711e51764aSArtem Bityutskiy continue; 16721e51764aSArtem Bityutskiy 16731e51764aSArtem Bityutskiy /* Go to the lowest leftmost znode in the new sub-tree */ 16741e51764aSArtem Bityutskiy while (znode->level > 0) { 16751e51764aSArtem Bityutskiy zbr = &znode->zbranch[0]; 16761e51764aSArtem Bityutskiy child = zbr->znode; 16771e51764aSArtem Bityutskiy if (!child) { 16781e51764aSArtem Bityutskiy child = ubifs_load_znode(c, zbr, znode, 0); 16791e51764aSArtem Bityutskiy if (IS_ERR(child)) { 16801e51764aSArtem Bityutskiy err = PTR_ERR(child); 16811e51764aSArtem Bityutskiy goto out_unlock; 16821e51764aSArtem Bityutskiy } 16831e51764aSArtem Bityutskiy zbr->znode = child; 16841e51764aSArtem Bityutskiy } 16851e51764aSArtem Bityutskiy znode = child; 16861e51764aSArtem Bityutskiy } 16871e51764aSArtem Bityutskiy } 16881e51764aSArtem Bityutskiy 16891e51764aSArtem Bityutskiy mutex_unlock(&c->tnc_mutex); 16901e51764aSArtem Bityutskiy return 0; 16911e51764aSArtem Bityutskiy 16921e51764aSArtem Bityutskiy out_dump: 16931e51764aSArtem Bityutskiy if (znode->parent) 16941e51764aSArtem Bityutskiy zbr = &znode->parent->zbranch[znode->iip]; 16951e51764aSArtem Bityutskiy else 16961e51764aSArtem Bityutskiy zbr = &c->zroot; 1697235c362bSSheng Yong ubifs_msg(c, "dump of znode at LEB %d:%d", zbr->lnum, zbr->offs); 1698edf6be24SArtem Bityutskiy ubifs_dump_znode(c, znode); 16991e51764aSArtem Bityutskiy out_unlock: 17001e51764aSArtem Bityutskiy mutex_unlock(&c->tnc_mutex); 17011e51764aSArtem Bityutskiy return err; 17021e51764aSArtem Bityutskiy } 17031e51764aSArtem Bityutskiy 17041e51764aSArtem Bityutskiy /** 17051e51764aSArtem Bityutskiy * add_size - add znode size to partially calculated index size. 17061e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 17071e51764aSArtem Bityutskiy * @znode: znode to add size for 17081e51764aSArtem Bityutskiy * @priv: partially calculated index size 17091e51764aSArtem Bityutskiy * 17101e51764aSArtem Bityutskiy * This is a helper function for 'dbg_check_idx_size()' which is called for 17111e51764aSArtem Bityutskiy * every indexing node and adds its size to the 'long long' variable pointed to 17121e51764aSArtem Bityutskiy * by @priv. 17131e51764aSArtem Bityutskiy */ 17141e51764aSArtem Bityutskiy static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv) 17151e51764aSArtem Bityutskiy { 17161e51764aSArtem Bityutskiy long long *idx_size = priv; 17171e51764aSArtem Bityutskiy int add; 17181e51764aSArtem Bityutskiy 17191e51764aSArtem Bityutskiy add = ubifs_idx_node_sz(c, znode->child_cnt); 17201e51764aSArtem Bityutskiy add = ALIGN(add, 8); 17211e51764aSArtem Bityutskiy *idx_size += add; 17221e51764aSArtem Bityutskiy return 0; 17231e51764aSArtem Bityutskiy } 17241e51764aSArtem Bityutskiy 17251e51764aSArtem Bityutskiy /** 17261e51764aSArtem Bityutskiy * dbg_check_idx_size - check index size. 17271e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 17281e51764aSArtem Bityutskiy * @idx_size: size to check 17291e51764aSArtem Bityutskiy * 17301e51764aSArtem Bityutskiy * This function walks the UBIFS index, calculates its size and checks that the 17311e51764aSArtem Bityutskiy * size is equivalent to @idx_size. Returns zero in case of success and a 17321e51764aSArtem Bityutskiy * negative error code in case of failure. 17331e51764aSArtem Bityutskiy */ 17341e51764aSArtem Bityutskiy int dbg_check_idx_size(struct ubifs_info *c, long long idx_size) 17351e51764aSArtem Bityutskiy { 17361e51764aSArtem Bityutskiy int err; 17371e51764aSArtem Bityutskiy long long calc = 0; 17381e51764aSArtem Bityutskiy 17398d7819b4SArtem Bityutskiy if (!dbg_is_chk_index(c)) 17401e51764aSArtem Bityutskiy return 0; 17411e51764aSArtem Bityutskiy 17421e51764aSArtem Bityutskiy err = dbg_walk_index(c, NULL, add_size, &calc); 17431e51764aSArtem Bityutskiy if (err) { 1744235c362bSSheng Yong ubifs_err(c, "error %d while walking the index", err); 17451e51764aSArtem Bityutskiy return err; 17461e51764aSArtem Bityutskiy } 17471e51764aSArtem Bityutskiy 17481e51764aSArtem Bityutskiy if (calc != idx_size) { 1749235c362bSSheng Yong ubifs_err(c, "index size check failed: calculated size is %lld, should be %lld", 175079fda517SArtem Bityutskiy calc, idx_size); 17511e51764aSArtem Bityutskiy dump_stack(); 17521e51764aSArtem Bityutskiy return -EINVAL; 17531e51764aSArtem Bityutskiy } 17541e51764aSArtem Bityutskiy 17551e51764aSArtem Bityutskiy return 0; 17561e51764aSArtem Bityutskiy } 17571e51764aSArtem Bityutskiy 17581e51764aSArtem Bityutskiy /** 17591e51764aSArtem Bityutskiy * struct fsck_inode - information about an inode used when checking the file-system. 17601e51764aSArtem Bityutskiy * @rb: link in the RB-tree of inodes 17611e51764aSArtem Bityutskiy * @inum: inode number 17621e51764aSArtem Bityutskiy * @mode: inode type, permissions, etc 17631e51764aSArtem Bityutskiy * @nlink: inode link count 17641e51764aSArtem Bityutskiy * @xattr_cnt: count of extended attributes 17651e51764aSArtem Bityutskiy * @references: how many directory/xattr entries refer this inode (calculated 17661e51764aSArtem Bityutskiy * while walking the index) 17671e51764aSArtem Bityutskiy * @calc_cnt: for directory inode count of child directories 17681e51764aSArtem Bityutskiy * @size: inode size (read from on-flash inode) 17691e51764aSArtem Bityutskiy * @xattr_sz: summary size of all extended attributes (read from on-flash 17701e51764aSArtem Bityutskiy * inode) 17711e51764aSArtem Bityutskiy * @calc_sz: for directories calculated directory size 17721e51764aSArtem Bityutskiy * @calc_xcnt: count of extended attributes 17731e51764aSArtem Bityutskiy * @calc_xsz: calculated summary size of all extended attributes 17741e51764aSArtem Bityutskiy * @xattr_nms: sum of lengths of all extended attribute names belonging to this 17751e51764aSArtem Bityutskiy * inode (read from on-flash inode) 17761e51764aSArtem Bityutskiy * @calc_xnms: calculated sum of lengths of all extended attribute names 17771e51764aSArtem Bityutskiy */ 17781e51764aSArtem Bityutskiy struct fsck_inode { 17791e51764aSArtem Bityutskiy struct rb_node rb; 17801e51764aSArtem Bityutskiy ino_t inum; 17811e51764aSArtem Bityutskiy umode_t mode; 17821e51764aSArtem Bityutskiy unsigned int nlink; 17831e51764aSArtem Bityutskiy unsigned int xattr_cnt; 17841e51764aSArtem Bityutskiy int references; 17851e51764aSArtem Bityutskiy int calc_cnt; 17861e51764aSArtem Bityutskiy long long size; 17871e51764aSArtem Bityutskiy unsigned int xattr_sz; 17881e51764aSArtem Bityutskiy long long calc_sz; 17891e51764aSArtem Bityutskiy long long calc_xcnt; 17901e51764aSArtem Bityutskiy long long calc_xsz; 17911e51764aSArtem Bityutskiy unsigned int xattr_nms; 17921e51764aSArtem Bityutskiy long long calc_xnms; 17931e51764aSArtem Bityutskiy }; 17941e51764aSArtem Bityutskiy 17951e51764aSArtem Bityutskiy /** 17961e51764aSArtem Bityutskiy * struct fsck_data - private FS checking information. 17971e51764aSArtem Bityutskiy * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects) 17981e51764aSArtem Bityutskiy */ 17991e51764aSArtem Bityutskiy struct fsck_data { 18001e51764aSArtem Bityutskiy struct rb_root inodes; 18011e51764aSArtem Bityutskiy }; 18021e51764aSArtem Bityutskiy 18031e51764aSArtem Bityutskiy /** 18041e51764aSArtem Bityutskiy * add_inode - add inode information to RB-tree of inodes. 18051e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 18061e51764aSArtem Bityutskiy * @fsckd: FS checking information 18071e51764aSArtem Bityutskiy * @ino: raw UBIFS inode to add 18081e51764aSArtem Bityutskiy * 18091e51764aSArtem Bityutskiy * This is a helper function for 'check_leaf()' which adds information about 18101e51764aSArtem Bityutskiy * inode @ino to the RB-tree of inodes. Returns inode information pointer in 18111e51764aSArtem Bityutskiy * case of success and a negative error code in case of failure. 18121e51764aSArtem Bityutskiy */ 18131e51764aSArtem Bityutskiy static struct fsck_inode *add_inode(struct ubifs_info *c, 18141e51764aSArtem Bityutskiy struct fsck_data *fsckd, 18151e51764aSArtem Bityutskiy struct ubifs_ino_node *ino) 18161e51764aSArtem Bityutskiy { 18171e51764aSArtem Bityutskiy struct rb_node **p, *parent = NULL; 18181e51764aSArtem Bityutskiy struct fsck_inode *fscki; 18191e51764aSArtem Bityutskiy ino_t inum = key_inum_flash(c, &ino->key); 182045cd5cddSArtem Bityutskiy struct inode *inode; 182145cd5cddSArtem Bityutskiy struct ubifs_inode *ui; 18221e51764aSArtem Bityutskiy 18231e51764aSArtem Bityutskiy p = &fsckd->inodes.rb_node; 18241e51764aSArtem Bityutskiy while (*p) { 18251e51764aSArtem Bityutskiy parent = *p; 18261e51764aSArtem Bityutskiy fscki = rb_entry(parent, struct fsck_inode, rb); 18271e51764aSArtem Bityutskiy if (inum < fscki->inum) 18281e51764aSArtem Bityutskiy p = &(*p)->rb_left; 18291e51764aSArtem Bityutskiy else if (inum > fscki->inum) 18301e51764aSArtem Bityutskiy p = &(*p)->rb_right; 18311e51764aSArtem Bityutskiy else 18321e51764aSArtem Bityutskiy return fscki; 18331e51764aSArtem Bityutskiy } 18341e51764aSArtem Bityutskiy 18351e51764aSArtem Bityutskiy if (inum > c->highest_inum) { 1836235c362bSSheng Yong ubifs_err(c, "too high inode number, max. is %lu", 1837e84461adSArtem Bityutskiy (unsigned long)c->highest_inum); 18381e51764aSArtem Bityutskiy return ERR_PTR(-EINVAL); 18391e51764aSArtem Bityutskiy } 18401e51764aSArtem Bityutskiy 18411e51764aSArtem Bityutskiy fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS); 18421e51764aSArtem Bityutskiy if (!fscki) 18431e51764aSArtem Bityutskiy return ERR_PTR(-ENOMEM); 18441e51764aSArtem Bityutskiy 184545cd5cddSArtem Bityutskiy inode = ilookup(c->vfs_sb, inum); 184645cd5cddSArtem Bityutskiy 18471e51764aSArtem Bityutskiy fscki->inum = inum; 184845cd5cddSArtem Bityutskiy /* 184945cd5cddSArtem Bityutskiy * If the inode is present in the VFS inode cache, use it instead of 185045cd5cddSArtem Bityutskiy * the on-flash inode which might be out-of-date. E.g., the size might 185145cd5cddSArtem Bityutskiy * be out-of-date. If we do not do this, the following may happen, for 185245cd5cddSArtem Bityutskiy * example: 185345cd5cddSArtem Bityutskiy * 1. A power cut happens 185445cd5cddSArtem Bityutskiy * 2. We mount the file-system R/O, the replay process fixes up the 185545cd5cddSArtem Bityutskiy * inode size in the VFS cache, but on on-flash. 185645cd5cddSArtem Bityutskiy * 3. 'check_leaf()' fails because it hits a data node beyond inode 185745cd5cddSArtem Bityutskiy * size. 185845cd5cddSArtem Bityutskiy */ 185945cd5cddSArtem Bityutskiy if (!inode) { 18601e51764aSArtem Bityutskiy fscki->nlink = le32_to_cpu(ino->nlink); 18611e51764aSArtem Bityutskiy fscki->size = le64_to_cpu(ino->size); 18621e51764aSArtem Bityutskiy fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt); 18631e51764aSArtem Bityutskiy fscki->xattr_sz = le32_to_cpu(ino->xattr_size); 18641e51764aSArtem Bityutskiy fscki->xattr_nms = le32_to_cpu(ino->xattr_names); 18651e51764aSArtem Bityutskiy fscki->mode = le32_to_cpu(ino->mode); 186645cd5cddSArtem Bityutskiy } else { 186745cd5cddSArtem Bityutskiy ui = ubifs_inode(inode); 186845cd5cddSArtem Bityutskiy fscki->nlink = inode->i_nlink; 186945cd5cddSArtem Bityutskiy fscki->size = inode->i_size; 187045cd5cddSArtem Bityutskiy fscki->xattr_cnt = ui->xattr_cnt; 187145cd5cddSArtem Bityutskiy fscki->xattr_sz = ui->xattr_size; 187245cd5cddSArtem Bityutskiy fscki->xattr_nms = ui->xattr_names; 187345cd5cddSArtem Bityutskiy fscki->mode = inode->i_mode; 187445cd5cddSArtem Bityutskiy iput(inode); 187545cd5cddSArtem Bityutskiy } 187645cd5cddSArtem Bityutskiy 18771e51764aSArtem Bityutskiy if (S_ISDIR(fscki->mode)) { 18781e51764aSArtem Bityutskiy fscki->calc_sz = UBIFS_INO_NODE_SZ; 18791e51764aSArtem Bityutskiy fscki->calc_cnt = 2; 18801e51764aSArtem Bityutskiy } 188145cd5cddSArtem Bityutskiy 18821e51764aSArtem Bityutskiy rb_link_node(&fscki->rb, parent, p); 18831e51764aSArtem Bityutskiy rb_insert_color(&fscki->rb, &fsckd->inodes); 188445cd5cddSArtem Bityutskiy 18851e51764aSArtem Bityutskiy return fscki; 18861e51764aSArtem Bityutskiy } 18871e51764aSArtem Bityutskiy 18881e51764aSArtem Bityutskiy /** 18891e51764aSArtem Bityutskiy * search_inode - search inode in the RB-tree of inodes. 18901e51764aSArtem Bityutskiy * @fsckd: FS checking information 18911e51764aSArtem Bityutskiy * @inum: inode number to search 18921e51764aSArtem Bityutskiy * 18931e51764aSArtem Bityutskiy * This is a helper function for 'check_leaf()' which searches inode @inum in 18941e51764aSArtem Bityutskiy * the RB-tree of inodes and returns an inode information pointer or %NULL if 18951e51764aSArtem Bityutskiy * the inode was not found. 18961e51764aSArtem Bityutskiy */ 18971e51764aSArtem Bityutskiy static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum) 18981e51764aSArtem Bityutskiy { 18991e51764aSArtem Bityutskiy struct rb_node *p; 19001e51764aSArtem Bityutskiy struct fsck_inode *fscki; 19011e51764aSArtem Bityutskiy 19021e51764aSArtem Bityutskiy p = fsckd->inodes.rb_node; 19031e51764aSArtem Bityutskiy while (p) { 19041e51764aSArtem Bityutskiy fscki = rb_entry(p, struct fsck_inode, rb); 19051e51764aSArtem Bityutskiy if (inum < fscki->inum) 19061e51764aSArtem Bityutskiy p = p->rb_left; 19071e51764aSArtem Bityutskiy else if (inum > fscki->inum) 19081e51764aSArtem Bityutskiy p = p->rb_right; 19091e51764aSArtem Bityutskiy else 19101e51764aSArtem Bityutskiy return fscki; 19111e51764aSArtem Bityutskiy } 19121e51764aSArtem Bityutskiy return NULL; 19131e51764aSArtem Bityutskiy } 19141e51764aSArtem Bityutskiy 19151e51764aSArtem Bityutskiy /** 19161e51764aSArtem Bityutskiy * read_add_inode - read inode node and add it to RB-tree of inodes. 19171e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 19181e51764aSArtem Bityutskiy * @fsckd: FS checking information 19191e51764aSArtem Bityutskiy * @inum: inode number to read 19201e51764aSArtem Bityutskiy * 19211e51764aSArtem Bityutskiy * This is a helper function for 'check_leaf()' which finds inode node @inum in 19221e51764aSArtem Bityutskiy * the index, reads it, and adds it to the RB-tree of inodes. Returns inode 19231e51764aSArtem Bityutskiy * information pointer in case of success and a negative error code in case of 19241e51764aSArtem Bityutskiy * failure. 19251e51764aSArtem Bityutskiy */ 19261e51764aSArtem Bityutskiy static struct fsck_inode *read_add_inode(struct ubifs_info *c, 19271e51764aSArtem Bityutskiy struct fsck_data *fsckd, ino_t inum) 19281e51764aSArtem Bityutskiy { 19291e51764aSArtem Bityutskiy int n, err; 19301e51764aSArtem Bityutskiy union ubifs_key key; 19311e51764aSArtem Bityutskiy struct ubifs_znode *znode; 19321e51764aSArtem Bityutskiy struct ubifs_zbranch *zbr; 19331e51764aSArtem Bityutskiy struct ubifs_ino_node *ino; 19341e51764aSArtem Bityutskiy struct fsck_inode *fscki; 19351e51764aSArtem Bityutskiy 19361e51764aSArtem Bityutskiy fscki = search_inode(fsckd, inum); 19371e51764aSArtem Bityutskiy if (fscki) 19381e51764aSArtem Bityutskiy return fscki; 19391e51764aSArtem Bityutskiy 19401e51764aSArtem Bityutskiy ino_key_init(c, &key, inum); 19411e51764aSArtem Bityutskiy err = ubifs_lookup_level0(c, &key, &znode, &n); 19421e51764aSArtem Bityutskiy if (!err) { 1943235c362bSSheng Yong ubifs_err(c, "inode %lu not found in index", (unsigned long)inum); 19441e51764aSArtem Bityutskiy return ERR_PTR(-ENOENT); 19451e51764aSArtem Bityutskiy } else if (err < 0) { 1946235c362bSSheng Yong ubifs_err(c, "error %d while looking up inode %lu", 1947e84461adSArtem Bityutskiy err, (unsigned long)inum); 19481e51764aSArtem Bityutskiy return ERR_PTR(err); 19491e51764aSArtem Bityutskiy } 19501e51764aSArtem Bityutskiy 19511e51764aSArtem Bityutskiy zbr = &znode->zbranch[n]; 19521e51764aSArtem Bityutskiy if (zbr->len < UBIFS_INO_NODE_SZ) { 1953235c362bSSheng Yong ubifs_err(c, "bad node %lu node length %d", 1954e84461adSArtem Bityutskiy (unsigned long)inum, zbr->len); 19551e51764aSArtem Bityutskiy return ERR_PTR(-EINVAL); 19561e51764aSArtem Bityutskiy } 19571e51764aSArtem Bityutskiy 19581e51764aSArtem Bityutskiy ino = kmalloc(zbr->len, GFP_NOFS); 19591e51764aSArtem Bityutskiy if (!ino) 19601e51764aSArtem Bityutskiy return ERR_PTR(-ENOMEM); 19611e51764aSArtem Bityutskiy 19621e51764aSArtem Bityutskiy err = ubifs_tnc_read_node(c, zbr, ino); 19631e51764aSArtem Bityutskiy if (err) { 1964235c362bSSheng Yong ubifs_err(c, "cannot read inode node at LEB %d:%d, error %d", 19651e51764aSArtem Bityutskiy zbr->lnum, zbr->offs, err); 19661e51764aSArtem Bityutskiy kfree(ino); 19671e51764aSArtem Bityutskiy return ERR_PTR(err); 19681e51764aSArtem Bityutskiy } 19691e51764aSArtem Bityutskiy 19701e51764aSArtem Bityutskiy fscki = add_inode(c, fsckd, ino); 19711e51764aSArtem Bityutskiy kfree(ino); 19721e51764aSArtem Bityutskiy if (IS_ERR(fscki)) { 1973235c362bSSheng Yong ubifs_err(c, "error %ld while adding inode %lu node", 1974e84461adSArtem Bityutskiy PTR_ERR(fscki), (unsigned long)inum); 19751e51764aSArtem Bityutskiy return fscki; 19761e51764aSArtem Bityutskiy } 19771e51764aSArtem Bityutskiy 19781e51764aSArtem Bityutskiy return fscki; 19791e51764aSArtem Bityutskiy } 19801e51764aSArtem Bityutskiy 19811e51764aSArtem Bityutskiy /** 19821e51764aSArtem Bityutskiy * check_leaf - check leaf node. 19831e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 19841e51764aSArtem Bityutskiy * @zbr: zbranch of the leaf node to check 19851e51764aSArtem Bityutskiy * @priv: FS checking information 19861e51764aSArtem Bityutskiy * 19871e51764aSArtem Bityutskiy * This is a helper function for 'dbg_check_filesystem()' which is called for 19881e51764aSArtem Bityutskiy * every single leaf node while walking the indexing tree. It checks that the 19891e51764aSArtem Bityutskiy * leaf node referred from the indexing tree exists, has correct CRC, and does 19901e51764aSArtem Bityutskiy * some other basic validation. This function is also responsible for building 19911e51764aSArtem Bityutskiy * an RB-tree of inodes - it adds all inodes into the RB-tree. It also 19921e51764aSArtem Bityutskiy * calculates reference count, size, etc for each inode in order to later 19931e51764aSArtem Bityutskiy * compare them to the information stored inside the inodes and detect possible 19941e51764aSArtem Bityutskiy * inconsistencies. Returns zero in case of success and a negative error code 19951e51764aSArtem Bityutskiy * in case of failure. 19961e51764aSArtem Bityutskiy */ 19971e51764aSArtem Bityutskiy static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr, 19981e51764aSArtem Bityutskiy void *priv) 19991e51764aSArtem Bityutskiy { 20001e51764aSArtem Bityutskiy ino_t inum; 20011e51764aSArtem Bityutskiy void *node; 20021e51764aSArtem Bityutskiy struct ubifs_ch *ch; 20031e51764aSArtem Bityutskiy int err, type = key_type(c, &zbr->key); 20041e51764aSArtem Bityutskiy struct fsck_inode *fscki; 20051e51764aSArtem Bityutskiy 20061e51764aSArtem Bityutskiy if (zbr->len < UBIFS_CH_SZ) { 2007235c362bSSheng Yong ubifs_err(c, "bad leaf length %d (LEB %d:%d)", 20081e51764aSArtem Bityutskiy zbr->len, zbr->lnum, zbr->offs); 20091e51764aSArtem Bityutskiy return -EINVAL; 20101e51764aSArtem Bityutskiy } 20111e51764aSArtem Bityutskiy 20121e51764aSArtem Bityutskiy node = kmalloc(zbr->len, GFP_NOFS); 20131e51764aSArtem Bityutskiy if (!node) 20141e51764aSArtem Bityutskiy return -ENOMEM; 20151e51764aSArtem Bityutskiy 20161e51764aSArtem Bityutskiy err = ubifs_tnc_read_node(c, zbr, node); 20171e51764aSArtem Bityutskiy if (err) { 2018235c362bSSheng Yong ubifs_err(c, "cannot read leaf node at LEB %d:%d, error %d", 20191e51764aSArtem Bityutskiy zbr->lnum, zbr->offs, err); 20201e51764aSArtem Bityutskiy goto out_free; 20211e51764aSArtem Bityutskiy } 20221e51764aSArtem Bityutskiy 20231e51764aSArtem Bityutskiy /* If this is an inode node, add it to RB-tree of inodes */ 20241e51764aSArtem Bityutskiy if (type == UBIFS_INO_KEY) { 20251e51764aSArtem Bityutskiy fscki = add_inode(c, priv, node); 20261e51764aSArtem Bityutskiy if (IS_ERR(fscki)) { 20271e51764aSArtem Bityutskiy err = PTR_ERR(fscki); 2028235c362bSSheng Yong ubifs_err(c, "error %d while adding inode node", err); 20291e51764aSArtem Bityutskiy goto out_dump; 20301e51764aSArtem Bityutskiy } 20311e51764aSArtem Bityutskiy goto out; 20321e51764aSArtem Bityutskiy } 20331e51764aSArtem Bityutskiy 20341e51764aSArtem Bityutskiy if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY && 20351e51764aSArtem Bityutskiy type != UBIFS_DATA_KEY) { 2036235c362bSSheng Yong ubifs_err(c, "unexpected node type %d at LEB %d:%d", 20371e51764aSArtem Bityutskiy type, zbr->lnum, zbr->offs); 20381e51764aSArtem Bityutskiy err = -EINVAL; 20391e51764aSArtem Bityutskiy goto out_free; 20401e51764aSArtem Bityutskiy } 20411e51764aSArtem Bityutskiy 20421e51764aSArtem Bityutskiy ch = node; 20431e51764aSArtem Bityutskiy if (le64_to_cpu(ch->sqnum) > c->max_sqnum) { 2044235c362bSSheng Yong ubifs_err(c, "too high sequence number, max. is %llu", 20451e51764aSArtem Bityutskiy c->max_sqnum); 20461e51764aSArtem Bityutskiy err = -EINVAL; 20471e51764aSArtem Bityutskiy goto out_dump; 20481e51764aSArtem Bityutskiy } 20491e51764aSArtem Bityutskiy 20501e51764aSArtem Bityutskiy if (type == UBIFS_DATA_KEY) { 20511e51764aSArtem Bityutskiy long long blk_offs; 20521e51764aSArtem Bityutskiy struct ubifs_data_node *dn = node; 20531e51764aSArtem Bityutskiy 20546eb61d58SRichard Weinberger ubifs_assert(c, zbr->len >= UBIFS_DATA_NODE_SZ); 2055fb4325a3SArtem Bityutskiy 20561e51764aSArtem Bityutskiy /* 20571e51764aSArtem Bityutskiy * Search the inode node this data node belongs to and insert 20581e51764aSArtem Bityutskiy * it to the RB-tree of inodes. 20591e51764aSArtem Bityutskiy */ 20601e51764aSArtem Bityutskiy inum = key_inum_flash(c, &dn->key); 20611e51764aSArtem Bityutskiy fscki = read_add_inode(c, priv, inum); 20621e51764aSArtem Bityutskiy if (IS_ERR(fscki)) { 20631e51764aSArtem Bityutskiy err = PTR_ERR(fscki); 2064235c362bSSheng Yong ubifs_err(c, "error %d while processing data node and trying to find inode node %lu", 2065e84461adSArtem Bityutskiy err, (unsigned long)inum); 20661e51764aSArtem Bityutskiy goto out_dump; 20671e51764aSArtem Bityutskiy } 20681e51764aSArtem Bityutskiy 20691e51764aSArtem Bityutskiy /* Make sure the data node is within inode size */ 20701e51764aSArtem Bityutskiy blk_offs = key_block_flash(c, &dn->key); 20711e51764aSArtem Bityutskiy blk_offs <<= UBIFS_BLOCK_SHIFT; 20721e51764aSArtem Bityutskiy blk_offs += le32_to_cpu(dn->size); 20731e51764aSArtem Bityutskiy if (blk_offs > fscki->size) { 2074235c362bSSheng Yong ubifs_err(c, "data node at LEB %d:%d is not within inode size %lld", 207579fda517SArtem Bityutskiy zbr->lnum, zbr->offs, fscki->size); 20761e51764aSArtem Bityutskiy err = -EINVAL; 20771e51764aSArtem Bityutskiy goto out_dump; 20781e51764aSArtem Bityutskiy } 20791e51764aSArtem Bityutskiy } else { 20801e51764aSArtem Bityutskiy int nlen; 20811e51764aSArtem Bityutskiy struct ubifs_dent_node *dent = node; 20821e51764aSArtem Bityutskiy struct fsck_inode *fscki1; 20831e51764aSArtem Bityutskiy 20846eb61d58SRichard Weinberger ubifs_assert(c, zbr->len >= UBIFS_DENT_NODE_SZ); 2085fb4325a3SArtem Bityutskiy 20861e51764aSArtem Bityutskiy err = ubifs_validate_entry(c, dent); 20871e51764aSArtem Bityutskiy if (err) 20881e51764aSArtem Bityutskiy goto out_dump; 20891e51764aSArtem Bityutskiy 20901e51764aSArtem Bityutskiy /* 20911e51764aSArtem Bityutskiy * Search the inode node this entry refers to and the parent 20921e51764aSArtem Bityutskiy * inode node and insert them to the RB-tree of inodes. 20931e51764aSArtem Bityutskiy */ 20941e51764aSArtem Bityutskiy inum = le64_to_cpu(dent->inum); 20951e51764aSArtem Bityutskiy fscki = read_add_inode(c, priv, inum); 20961e51764aSArtem Bityutskiy if (IS_ERR(fscki)) { 20971e51764aSArtem Bityutskiy err = PTR_ERR(fscki); 2098235c362bSSheng Yong ubifs_err(c, "error %d while processing entry node and trying to find inode node %lu", 2099e84461adSArtem Bityutskiy err, (unsigned long)inum); 21001e51764aSArtem Bityutskiy goto out_dump; 21011e51764aSArtem Bityutskiy } 21021e51764aSArtem Bityutskiy 21031e51764aSArtem Bityutskiy /* Count how many direntries or xentries refers this inode */ 21041e51764aSArtem Bityutskiy fscki->references += 1; 21051e51764aSArtem Bityutskiy 21061e51764aSArtem Bityutskiy inum = key_inum_flash(c, &dent->key); 21071e51764aSArtem Bityutskiy fscki1 = read_add_inode(c, priv, inum); 21081e51764aSArtem Bityutskiy if (IS_ERR(fscki1)) { 2109b38882f5SRoel Kluin err = PTR_ERR(fscki1); 2110235c362bSSheng Yong ubifs_err(c, "error %d while processing entry node and trying to find parent inode node %lu", 2111e84461adSArtem Bityutskiy err, (unsigned long)inum); 21121e51764aSArtem Bityutskiy goto out_dump; 21131e51764aSArtem Bityutskiy } 21141e51764aSArtem Bityutskiy 21151e51764aSArtem Bityutskiy nlen = le16_to_cpu(dent->nlen); 21161e51764aSArtem Bityutskiy if (type == UBIFS_XENT_KEY) { 21171e51764aSArtem Bityutskiy fscki1->calc_xcnt += 1; 21181e51764aSArtem Bityutskiy fscki1->calc_xsz += CALC_DENT_SIZE(nlen); 21191e51764aSArtem Bityutskiy fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size); 21201e51764aSArtem Bityutskiy fscki1->calc_xnms += nlen; 21211e51764aSArtem Bityutskiy } else { 21221e51764aSArtem Bityutskiy fscki1->calc_sz += CALC_DENT_SIZE(nlen); 21231e51764aSArtem Bityutskiy if (dent->type == UBIFS_ITYPE_DIR) 21241e51764aSArtem Bityutskiy fscki1->calc_cnt += 1; 21251e51764aSArtem Bityutskiy } 21261e51764aSArtem Bityutskiy } 21271e51764aSArtem Bityutskiy 21281e51764aSArtem Bityutskiy out: 21291e51764aSArtem Bityutskiy kfree(node); 21301e51764aSArtem Bityutskiy return 0; 21311e51764aSArtem Bityutskiy 21321e51764aSArtem Bityutskiy out_dump: 2133235c362bSSheng Yong ubifs_msg(c, "dump of node at LEB %d:%d", zbr->lnum, zbr->offs); 2134a33e30a0SZhihao Cheng ubifs_dump_node(c, node, zbr->len); 21351e51764aSArtem Bityutskiy out_free: 21361e51764aSArtem Bityutskiy kfree(node); 21371e51764aSArtem Bityutskiy return err; 21381e51764aSArtem Bityutskiy } 21391e51764aSArtem Bityutskiy 21401e51764aSArtem Bityutskiy /** 21411e51764aSArtem Bityutskiy * free_inodes - free RB-tree of inodes. 21421e51764aSArtem Bityutskiy * @fsckd: FS checking information 21431e51764aSArtem Bityutskiy */ 21441e51764aSArtem Bityutskiy static void free_inodes(struct fsck_data *fsckd) 21451e51764aSArtem Bityutskiy { 2146bb25e49fSCody P Schafer struct fsck_inode *fscki, *n; 21471e51764aSArtem Bityutskiy 2148bb25e49fSCody P Schafer rbtree_postorder_for_each_entry_safe(fscki, n, &fsckd->inodes, rb) 21491e51764aSArtem Bityutskiy kfree(fscki); 21501e51764aSArtem Bityutskiy } 21511e51764aSArtem Bityutskiy 21521e51764aSArtem Bityutskiy /** 21531e51764aSArtem Bityutskiy * check_inodes - checks all inodes. 21541e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 21551e51764aSArtem Bityutskiy * @fsckd: FS checking information 21561e51764aSArtem Bityutskiy * 21571e51764aSArtem Bityutskiy * This is a helper function for 'dbg_check_filesystem()' which walks the 21581e51764aSArtem Bityutskiy * RB-tree of inodes after the index scan has been finished, and checks that 21591e51764aSArtem Bityutskiy * inode nlink, size, etc are correct. Returns zero if inodes are fine, 21601e51764aSArtem Bityutskiy * %-EINVAL if not, and a negative error code in case of failure. 21611e51764aSArtem Bityutskiy */ 21621e51764aSArtem Bityutskiy static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd) 21631e51764aSArtem Bityutskiy { 21641e51764aSArtem Bityutskiy int n, err; 21651e51764aSArtem Bityutskiy union ubifs_key key; 21661e51764aSArtem Bityutskiy struct ubifs_znode *znode; 21671e51764aSArtem Bityutskiy struct ubifs_zbranch *zbr; 21681e51764aSArtem Bityutskiy struct ubifs_ino_node *ino; 21691e51764aSArtem Bityutskiy struct fsck_inode *fscki; 21701e51764aSArtem Bityutskiy struct rb_node *this = rb_first(&fsckd->inodes); 21711e51764aSArtem Bityutskiy 21721e51764aSArtem Bityutskiy while (this) { 21731e51764aSArtem Bityutskiy fscki = rb_entry(this, struct fsck_inode, rb); 21741e51764aSArtem Bityutskiy this = rb_next(this); 21751e51764aSArtem Bityutskiy 21761e51764aSArtem Bityutskiy if (S_ISDIR(fscki->mode)) { 21771e51764aSArtem Bityutskiy /* 21781e51764aSArtem Bityutskiy * Directories have to have exactly one reference (they 21791e51764aSArtem Bityutskiy * cannot have hardlinks), although root inode is an 21801e51764aSArtem Bityutskiy * exception. 21811e51764aSArtem Bityutskiy */ 21821e51764aSArtem Bityutskiy if (fscki->inum != UBIFS_ROOT_INO && 21831e51764aSArtem Bityutskiy fscki->references != 1) { 2184235c362bSSheng Yong ubifs_err(c, "directory inode %lu has %d direntries which refer it, but should be 1", 2185e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 21861e51764aSArtem Bityutskiy fscki->references); 21871e51764aSArtem Bityutskiy goto out_dump; 21881e51764aSArtem Bityutskiy } 21891e51764aSArtem Bityutskiy if (fscki->inum == UBIFS_ROOT_INO && 21901e51764aSArtem Bityutskiy fscki->references != 0) { 2191235c362bSSheng Yong ubifs_err(c, "root inode %lu has non-zero (%d) direntries which refer it", 2192e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 2193e84461adSArtem Bityutskiy fscki->references); 21941e51764aSArtem Bityutskiy goto out_dump; 21951e51764aSArtem Bityutskiy } 21961e51764aSArtem Bityutskiy if (fscki->calc_sz != fscki->size) { 2197235c362bSSheng Yong ubifs_err(c, "directory inode %lu size is %lld, but calculated size is %lld", 2198e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 2199e84461adSArtem Bityutskiy fscki->size, fscki->calc_sz); 22001e51764aSArtem Bityutskiy goto out_dump; 22011e51764aSArtem Bityutskiy } 22021e51764aSArtem Bityutskiy if (fscki->calc_cnt != fscki->nlink) { 2203235c362bSSheng Yong ubifs_err(c, "directory inode %lu nlink is %d, but calculated nlink is %d", 2204e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 2205e84461adSArtem Bityutskiy fscki->nlink, fscki->calc_cnt); 22061e51764aSArtem Bityutskiy goto out_dump; 22071e51764aSArtem Bityutskiy } 22081e51764aSArtem Bityutskiy } else { 22091e51764aSArtem Bityutskiy if (fscki->references != fscki->nlink) { 2210235c362bSSheng Yong ubifs_err(c, "inode %lu nlink is %d, but calculated nlink is %d", 2211e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 22121e51764aSArtem Bityutskiy fscki->nlink, fscki->references); 22131e51764aSArtem Bityutskiy goto out_dump; 22141e51764aSArtem Bityutskiy } 22151e51764aSArtem Bityutskiy } 22161e51764aSArtem Bityutskiy if (fscki->xattr_sz != fscki->calc_xsz) { 2217235c362bSSheng Yong ubifs_err(c, "inode %lu has xattr size %u, but calculated size is %lld", 2218e84461adSArtem Bityutskiy (unsigned long)fscki->inum, fscki->xattr_sz, 22191e51764aSArtem Bityutskiy fscki->calc_xsz); 22201e51764aSArtem Bityutskiy goto out_dump; 22211e51764aSArtem Bityutskiy } 22221e51764aSArtem Bityutskiy if (fscki->xattr_cnt != fscki->calc_xcnt) { 2223235c362bSSheng Yong ubifs_err(c, "inode %lu has %u xattrs, but calculated count is %lld", 2224e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 22251e51764aSArtem Bityutskiy fscki->xattr_cnt, fscki->calc_xcnt); 22261e51764aSArtem Bityutskiy goto out_dump; 22271e51764aSArtem Bityutskiy } 22281e51764aSArtem Bityutskiy if (fscki->xattr_nms != fscki->calc_xnms) { 2229235c362bSSheng Yong ubifs_err(c, "inode %lu has xattr names' size %u, but calculated names' size is %lld", 2230e84461adSArtem Bityutskiy (unsigned long)fscki->inum, fscki->xattr_nms, 22311e51764aSArtem Bityutskiy fscki->calc_xnms); 22321e51764aSArtem Bityutskiy goto out_dump; 22331e51764aSArtem Bityutskiy } 22341e51764aSArtem Bityutskiy } 22351e51764aSArtem Bityutskiy 22361e51764aSArtem Bityutskiy return 0; 22371e51764aSArtem Bityutskiy 22381e51764aSArtem Bityutskiy out_dump: 22391e51764aSArtem Bityutskiy /* Read the bad inode and dump it */ 22401e51764aSArtem Bityutskiy ino_key_init(c, &key, fscki->inum); 22411e51764aSArtem Bityutskiy err = ubifs_lookup_level0(c, &key, &znode, &n); 22421e51764aSArtem Bityutskiy if (!err) { 2243235c362bSSheng Yong ubifs_err(c, "inode %lu not found in index", 2244e84461adSArtem Bityutskiy (unsigned long)fscki->inum); 22451e51764aSArtem Bityutskiy return -ENOENT; 22461e51764aSArtem Bityutskiy } else if (err < 0) { 2247235c362bSSheng Yong ubifs_err(c, "error %d while looking up inode %lu", 2248e84461adSArtem Bityutskiy err, (unsigned long)fscki->inum); 22491e51764aSArtem Bityutskiy return err; 22501e51764aSArtem Bityutskiy } 22511e51764aSArtem Bityutskiy 22521e51764aSArtem Bityutskiy zbr = &znode->zbranch[n]; 22531e51764aSArtem Bityutskiy ino = kmalloc(zbr->len, GFP_NOFS); 22541e51764aSArtem Bityutskiy if (!ino) 22551e51764aSArtem Bityutskiy return -ENOMEM; 22561e51764aSArtem Bityutskiy 22571e51764aSArtem Bityutskiy err = ubifs_tnc_read_node(c, zbr, ino); 22581e51764aSArtem Bityutskiy if (err) { 2259235c362bSSheng Yong ubifs_err(c, "cannot read inode node at LEB %d:%d, error %d", 22601e51764aSArtem Bityutskiy zbr->lnum, zbr->offs, err); 22611e51764aSArtem Bityutskiy kfree(ino); 22621e51764aSArtem Bityutskiy return err; 22631e51764aSArtem Bityutskiy } 22641e51764aSArtem Bityutskiy 2265235c362bSSheng Yong ubifs_msg(c, "dump of the inode %lu sitting in LEB %d:%d", 2266e84461adSArtem Bityutskiy (unsigned long)fscki->inum, zbr->lnum, zbr->offs); 2267a33e30a0SZhihao Cheng ubifs_dump_node(c, ino, zbr->len); 22681e51764aSArtem Bityutskiy kfree(ino); 22691e51764aSArtem Bityutskiy return -EINVAL; 22701e51764aSArtem Bityutskiy } 22711e51764aSArtem Bityutskiy 22721e51764aSArtem Bityutskiy /** 22731e51764aSArtem Bityutskiy * dbg_check_filesystem - check the file-system. 22741e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 22751e51764aSArtem Bityutskiy * 22761e51764aSArtem Bityutskiy * This function checks the file system, namely: 22771e51764aSArtem Bityutskiy * o makes sure that all leaf nodes exist and their CRCs are correct; 22781e51764aSArtem Bityutskiy * o makes sure inode nlink, size, xattr size/count are correct (for all 22791e51764aSArtem Bityutskiy * inodes). 22801e51764aSArtem Bityutskiy * 22811e51764aSArtem Bityutskiy * The function reads whole indexing tree and all nodes, so it is pretty 22821e51764aSArtem Bityutskiy * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if 22831e51764aSArtem Bityutskiy * not, and a negative error code in case of failure. 22841e51764aSArtem Bityutskiy */ 22851e51764aSArtem Bityutskiy int dbg_check_filesystem(struct ubifs_info *c) 22861e51764aSArtem Bityutskiy { 22871e51764aSArtem Bityutskiy int err; 22881e51764aSArtem Bityutskiy struct fsck_data fsckd; 22891e51764aSArtem Bityutskiy 22902b1844a8SArtem Bityutskiy if (!dbg_is_chk_fs(c)) 22911e51764aSArtem Bityutskiy return 0; 22921e51764aSArtem Bityutskiy 22931e51764aSArtem Bityutskiy fsckd.inodes = RB_ROOT; 22941e51764aSArtem Bityutskiy err = dbg_walk_index(c, check_leaf, NULL, &fsckd); 22951e51764aSArtem Bityutskiy if (err) 22961e51764aSArtem Bityutskiy goto out_free; 22971e51764aSArtem Bityutskiy 22981e51764aSArtem Bityutskiy err = check_inodes(c, &fsckd); 22991e51764aSArtem Bityutskiy if (err) 23001e51764aSArtem Bityutskiy goto out_free; 23011e51764aSArtem Bityutskiy 23021e51764aSArtem Bityutskiy free_inodes(&fsckd); 23031e51764aSArtem Bityutskiy return 0; 23041e51764aSArtem Bityutskiy 23051e51764aSArtem Bityutskiy out_free: 2306235c362bSSheng Yong ubifs_err(c, "file-system check failed with error %d", err); 23071e51764aSArtem Bityutskiy dump_stack(); 23081e51764aSArtem Bityutskiy free_inodes(&fsckd); 23091e51764aSArtem Bityutskiy return err; 23101e51764aSArtem Bityutskiy } 23111e51764aSArtem Bityutskiy 23123bb66b47SArtem Bityutskiy /** 23133bb66b47SArtem Bityutskiy * dbg_check_data_nodes_order - check that list of data nodes is sorted. 23143bb66b47SArtem Bityutskiy * @c: UBIFS file-system description object 23153bb66b47SArtem Bityutskiy * @head: the list of nodes ('struct ubifs_scan_node' objects) 23163bb66b47SArtem Bityutskiy * 23173bb66b47SArtem Bityutskiy * This function returns zero if the list of data nodes is sorted correctly, 23183bb66b47SArtem Bityutskiy * and %-EINVAL if not. 23193bb66b47SArtem Bityutskiy */ 23203bb66b47SArtem Bityutskiy int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head) 23213bb66b47SArtem Bityutskiy { 23223bb66b47SArtem Bityutskiy struct list_head *cur; 23233bb66b47SArtem Bityutskiy struct ubifs_scan_node *sa, *sb; 23243bb66b47SArtem Bityutskiy 23252b1844a8SArtem Bityutskiy if (!dbg_is_chk_gen(c)) 23263bb66b47SArtem Bityutskiy return 0; 23273bb66b47SArtem Bityutskiy 23283bb66b47SArtem Bityutskiy for (cur = head->next; cur->next != head; cur = cur->next) { 23293bb66b47SArtem Bityutskiy ino_t inuma, inumb; 23303bb66b47SArtem Bityutskiy uint32_t blka, blkb; 23313bb66b47SArtem Bityutskiy 23323bb66b47SArtem Bityutskiy cond_resched(); 23333bb66b47SArtem Bityutskiy sa = container_of(cur, struct ubifs_scan_node, list); 23343bb66b47SArtem Bityutskiy sb = container_of(cur->next, struct ubifs_scan_node, list); 23353bb66b47SArtem Bityutskiy 23363bb66b47SArtem Bityutskiy if (sa->type != UBIFS_DATA_NODE) { 2337235c362bSSheng Yong ubifs_err(c, "bad node type %d", sa->type); 2338a33e30a0SZhihao Cheng ubifs_dump_node(c, sa->node, c->leb_size - sa->offs); 23393bb66b47SArtem Bityutskiy return -EINVAL; 23403bb66b47SArtem Bityutskiy } 23413bb66b47SArtem Bityutskiy if (sb->type != UBIFS_DATA_NODE) { 2342235c362bSSheng Yong ubifs_err(c, "bad node type %d", sb->type); 2343a33e30a0SZhihao Cheng ubifs_dump_node(c, sb->node, c->leb_size - sb->offs); 23443bb66b47SArtem Bityutskiy return -EINVAL; 23453bb66b47SArtem Bityutskiy } 23463bb66b47SArtem Bityutskiy 23473bb66b47SArtem Bityutskiy inuma = key_inum(c, &sa->key); 23483bb66b47SArtem Bityutskiy inumb = key_inum(c, &sb->key); 23493bb66b47SArtem Bityutskiy 23503bb66b47SArtem Bityutskiy if (inuma < inumb) 23513bb66b47SArtem Bityutskiy continue; 23523bb66b47SArtem Bityutskiy if (inuma > inumb) { 2353235c362bSSheng Yong ubifs_err(c, "larger inum %lu goes before inum %lu", 23543bb66b47SArtem Bityutskiy (unsigned long)inuma, (unsigned long)inumb); 23553bb66b47SArtem Bityutskiy goto error_dump; 23563bb66b47SArtem Bityutskiy } 23573bb66b47SArtem Bityutskiy 23583bb66b47SArtem Bityutskiy blka = key_block(c, &sa->key); 23593bb66b47SArtem Bityutskiy blkb = key_block(c, &sb->key); 23603bb66b47SArtem Bityutskiy 23613bb66b47SArtem Bityutskiy if (blka > blkb) { 2362235c362bSSheng Yong ubifs_err(c, "larger block %u goes before %u", blka, blkb); 23633bb66b47SArtem Bityutskiy goto error_dump; 23643bb66b47SArtem Bityutskiy } 23653bb66b47SArtem Bityutskiy if (blka == blkb) { 2366235c362bSSheng Yong ubifs_err(c, "two data nodes for the same block"); 23673bb66b47SArtem Bityutskiy goto error_dump; 23683bb66b47SArtem Bityutskiy } 23693bb66b47SArtem Bityutskiy } 23703bb66b47SArtem Bityutskiy 23713bb66b47SArtem Bityutskiy return 0; 23723bb66b47SArtem Bityutskiy 23733bb66b47SArtem Bityutskiy error_dump: 2374a33e30a0SZhihao Cheng ubifs_dump_node(c, sa->node, c->leb_size - sa->offs); 2375a33e30a0SZhihao Cheng ubifs_dump_node(c, sb->node, c->leb_size - sb->offs); 23763bb66b47SArtem Bityutskiy return -EINVAL; 23773bb66b47SArtem Bityutskiy } 23783bb66b47SArtem Bityutskiy 23793bb66b47SArtem Bityutskiy /** 23803bb66b47SArtem Bityutskiy * dbg_check_nondata_nodes_order - check that list of data nodes is sorted. 23813bb66b47SArtem Bityutskiy * @c: UBIFS file-system description object 23823bb66b47SArtem Bityutskiy * @head: the list of nodes ('struct ubifs_scan_node' objects) 23833bb66b47SArtem Bityutskiy * 23843bb66b47SArtem Bityutskiy * This function returns zero if the list of non-data nodes is sorted correctly, 23853bb66b47SArtem Bityutskiy * and %-EINVAL if not. 23863bb66b47SArtem Bityutskiy */ 23873bb66b47SArtem Bityutskiy int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head) 23883bb66b47SArtem Bityutskiy { 23893bb66b47SArtem Bityutskiy struct list_head *cur; 23903bb66b47SArtem Bityutskiy struct ubifs_scan_node *sa, *sb; 23913bb66b47SArtem Bityutskiy 23922b1844a8SArtem Bityutskiy if (!dbg_is_chk_gen(c)) 23933bb66b47SArtem Bityutskiy return 0; 23943bb66b47SArtem Bityutskiy 23953bb66b47SArtem Bityutskiy for (cur = head->next; cur->next != head; cur = cur->next) { 23963bb66b47SArtem Bityutskiy ino_t inuma, inumb; 23973bb66b47SArtem Bityutskiy uint32_t hasha, hashb; 23983bb66b47SArtem Bityutskiy 23993bb66b47SArtem Bityutskiy cond_resched(); 24003bb66b47SArtem Bityutskiy sa = container_of(cur, struct ubifs_scan_node, list); 24013bb66b47SArtem Bityutskiy sb = container_of(cur->next, struct ubifs_scan_node, list); 24023bb66b47SArtem Bityutskiy 24033bb66b47SArtem Bityutskiy if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE && 24043bb66b47SArtem Bityutskiy sa->type != UBIFS_XENT_NODE) { 2405235c362bSSheng Yong ubifs_err(c, "bad node type %d", sa->type); 2406a33e30a0SZhihao Cheng ubifs_dump_node(c, sa->node, c->leb_size - sa->offs); 24073bb66b47SArtem Bityutskiy return -EINVAL; 24083bb66b47SArtem Bityutskiy } 24096a258f7dSColin Ian King if (sb->type != UBIFS_INO_NODE && sb->type != UBIFS_DENT_NODE && 24106a258f7dSColin Ian King sb->type != UBIFS_XENT_NODE) { 2411235c362bSSheng Yong ubifs_err(c, "bad node type %d", sb->type); 2412a33e30a0SZhihao Cheng ubifs_dump_node(c, sb->node, c->leb_size - sb->offs); 24133bb66b47SArtem Bityutskiy return -EINVAL; 24143bb66b47SArtem Bityutskiy } 24153bb66b47SArtem Bityutskiy 24163bb66b47SArtem Bityutskiy if (sa->type != UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) { 2417235c362bSSheng Yong ubifs_err(c, "non-inode node goes before inode node"); 24183bb66b47SArtem Bityutskiy goto error_dump; 24193bb66b47SArtem Bityutskiy } 24203bb66b47SArtem Bityutskiy 24213bb66b47SArtem Bityutskiy if (sa->type == UBIFS_INO_NODE && sb->type != UBIFS_INO_NODE) 24223bb66b47SArtem Bityutskiy continue; 24233bb66b47SArtem Bityutskiy 24243bb66b47SArtem Bityutskiy if (sa->type == UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) { 24253bb66b47SArtem Bityutskiy /* Inode nodes are sorted in descending size order */ 24263bb66b47SArtem Bityutskiy if (sa->len < sb->len) { 2427235c362bSSheng Yong ubifs_err(c, "smaller inode node goes first"); 24283bb66b47SArtem Bityutskiy goto error_dump; 24293bb66b47SArtem Bityutskiy } 24303bb66b47SArtem Bityutskiy continue; 24313bb66b47SArtem Bityutskiy } 24323bb66b47SArtem Bityutskiy 24333bb66b47SArtem Bityutskiy /* 24343bb66b47SArtem Bityutskiy * This is either a dentry or xentry, which should be sorted in 24353bb66b47SArtem Bityutskiy * ascending (parent ino, hash) order. 24363bb66b47SArtem Bityutskiy */ 24373bb66b47SArtem Bityutskiy inuma = key_inum(c, &sa->key); 24383bb66b47SArtem Bityutskiy inumb = key_inum(c, &sb->key); 24393bb66b47SArtem Bityutskiy 24403bb66b47SArtem Bityutskiy if (inuma < inumb) 24413bb66b47SArtem Bityutskiy continue; 24423bb66b47SArtem Bityutskiy if (inuma > inumb) { 2443235c362bSSheng Yong ubifs_err(c, "larger inum %lu goes before inum %lu", 24443bb66b47SArtem Bityutskiy (unsigned long)inuma, (unsigned long)inumb); 24453bb66b47SArtem Bityutskiy goto error_dump; 24463bb66b47SArtem Bityutskiy } 24473bb66b47SArtem Bityutskiy 24483bb66b47SArtem Bityutskiy hasha = key_block(c, &sa->key); 24493bb66b47SArtem Bityutskiy hashb = key_block(c, &sb->key); 24503bb66b47SArtem Bityutskiy 24513bb66b47SArtem Bityutskiy if (hasha > hashb) { 2452235c362bSSheng Yong ubifs_err(c, "larger hash %u goes before %u", 2453c4361570SArtem Bityutskiy hasha, hashb); 24543bb66b47SArtem Bityutskiy goto error_dump; 24553bb66b47SArtem Bityutskiy } 24563bb66b47SArtem Bityutskiy } 24573bb66b47SArtem Bityutskiy 24583bb66b47SArtem Bityutskiy return 0; 24593bb66b47SArtem Bityutskiy 24603bb66b47SArtem Bityutskiy error_dump: 2461235c362bSSheng Yong ubifs_msg(c, "dumping first node"); 2462a33e30a0SZhihao Cheng ubifs_dump_node(c, sa->node, c->leb_size - sa->offs); 2463235c362bSSheng Yong ubifs_msg(c, "dumping second node"); 2464a33e30a0SZhihao Cheng ubifs_dump_node(c, sb->node, c->leb_size - sb->offs); 24653bb66b47SArtem Bityutskiy return -EINVAL; 24663bb66b47SArtem Bityutskiy } 24673bb66b47SArtem Bityutskiy 2468a7fa94a9SArtem Bityutskiy static inline int chance(unsigned int n, unsigned int out_of) 24691e51764aSArtem Bityutskiy { 2470*8032bf12SJason A. Donenfeld return !!(get_random_u32_below(out_of) + 1 <= n); 2471a7fa94a9SArtem Bityutskiy 24721e51764aSArtem Bityutskiy } 24731e51764aSArtem Bityutskiy 2474d27462a5SArtem Bityutskiy static int power_cut_emulated(struct ubifs_info *c, int lnum, int write) 24751e51764aSArtem Bityutskiy { 2476f57cb188SArtem Bityutskiy struct ubifs_debug_info *d = c->dbg; 24771e51764aSArtem Bityutskiy 24786eb61d58SRichard Weinberger ubifs_assert(c, dbg_is_tst_rcvry(c)); 24791e51764aSArtem Bityutskiy 2480d27462a5SArtem Bityutskiy if (!d->pc_cnt) { 2481d27462a5SArtem Bityutskiy /* First call - decide delay to the power cut */ 24821e51764aSArtem Bityutskiy if (chance(1, 2)) { 2483a7fa94a9SArtem Bityutskiy unsigned long delay; 24841e51764aSArtem Bityutskiy 24851e51764aSArtem Bityutskiy if (chance(1, 2)) { 2486d27462a5SArtem Bityutskiy d->pc_delay = 1; 2487443b39cdSRichard Weinberger /* Fail within 1 minute */ 2488*8032bf12SJason A. Donenfeld delay = get_random_u32_below(60000); 2489a7fa94a9SArtem Bityutskiy d->pc_timeout = jiffies; 2490a7fa94a9SArtem Bityutskiy d->pc_timeout += msecs_to_jiffies(delay); 2491235c362bSSheng Yong ubifs_warn(c, "failing after %lums", delay); 24921e51764aSArtem Bityutskiy } else { 2493d27462a5SArtem Bityutskiy d->pc_delay = 2; 2494*8032bf12SJason A. Donenfeld delay = get_random_u32_below(10000); 2495a7fa94a9SArtem Bityutskiy /* Fail within 10000 operations */ 2496d27462a5SArtem Bityutskiy d->pc_cnt_max = delay; 2497235c362bSSheng Yong ubifs_warn(c, "failing after %lu calls", delay); 24981e51764aSArtem Bityutskiy } 24991e51764aSArtem Bityutskiy } 2500a7fa94a9SArtem Bityutskiy 2501d27462a5SArtem Bityutskiy d->pc_cnt += 1; 25021e51764aSArtem Bityutskiy } 2503a7fa94a9SArtem Bityutskiy 25041e51764aSArtem Bityutskiy /* Determine if failure delay has expired */ 2505a7fa94a9SArtem Bityutskiy if (d->pc_delay == 1 && time_before(jiffies, d->pc_timeout)) 25061e51764aSArtem Bityutskiy return 0; 2507a7fa94a9SArtem Bityutskiy if (d->pc_delay == 2 && d->pc_cnt++ < d->pc_cnt_max) 25081e51764aSArtem Bityutskiy return 0; 2509a7fa94a9SArtem Bityutskiy 25101e51764aSArtem Bityutskiy if (lnum == UBIFS_SB_LNUM) { 2511a7fa94a9SArtem Bityutskiy if (write && chance(1, 2)) 25121e51764aSArtem Bityutskiy return 0; 2513a7fa94a9SArtem Bityutskiy if (chance(19, 20)) 25141e51764aSArtem Bityutskiy return 0; 2515235c362bSSheng Yong ubifs_warn(c, "failing in super block LEB %d", lnum); 25161e51764aSArtem Bityutskiy } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) { 25171e51764aSArtem Bityutskiy if (chance(19, 20)) 25181e51764aSArtem Bityutskiy return 0; 2519235c362bSSheng Yong ubifs_warn(c, "failing in master LEB %d", lnum); 25201e51764aSArtem Bityutskiy } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) { 2521a7fa94a9SArtem Bityutskiy if (write && chance(99, 100)) 25221e51764aSArtem Bityutskiy return 0; 2523a7fa94a9SArtem Bityutskiy if (chance(399, 400)) 25241e51764aSArtem Bityutskiy return 0; 2525235c362bSSheng Yong ubifs_warn(c, "failing in log LEB %d", lnum); 25261e51764aSArtem Bityutskiy } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) { 2527a7fa94a9SArtem Bityutskiy if (write && chance(7, 8)) 25281e51764aSArtem Bityutskiy return 0; 2529a7fa94a9SArtem Bityutskiy if (chance(19, 20)) 25301e51764aSArtem Bityutskiy return 0; 2531235c362bSSheng Yong ubifs_warn(c, "failing in LPT LEB %d", lnum); 25321e51764aSArtem Bityutskiy } else if (lnum >= c->orph_first && lnum <= c->orph_last) { 2533a7fa94a9SArtem Bityutskiy if (write && chance(1, 2)) 25341e51764aSArtem Bityutskiy return 0; 2535a7fa94a9SArtem Bityutskiy if (chance(9, 10)) 25361e51764aSArtem Bityutskiy return 0; 2537235c362bSSheng Yong ubifs_warn(c, "failing in orphan LEB %d", lnum); 25381e51764aSArtem Bityutskiy } else if (lnum == c->ihead_lnum) { 25391e51764aSArtem Bityutskiy if (chance(99, 100)) 25401e51764aSArtem Bityutskiy return 0; 2541235c362bSSheng Yong ubifs_warn(c, "failing in index head LEB %d", lnum); 25421e51764aSArtem Bityutskiy } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) { 25431e51764aSArtem Bityutskiy if (chance(9, 10)) 25441e51764aSArtem Bityutskiy return 0; 2545235c362bSSheng Yong ubifs_warn(c, "failing in GC head LEB %d", lnum); 25461e51764aSArtem Bityutskiy } else if (write && !RB_EMPTY_ROOT(&c->buds) && 25471e51764aSArtem Bityutskiy !ubifs_search_bud(c, lnum)) { 25481e51764aSArtem Bityutskiy if (chance(19, 20)) 25491e51764aSArtem Bityutskiy return 0; 2550235c362bSSheng Yong ubifs_warn(c, "failing in non-bud LEB %d", lnum); 25511e51764aSArtem Bityutskiy } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND || 25521e51764aSArtem Bityutskiy c->cmt_state == COMMIT_RUNNING_REQUIRED) { 25531e51764aSArtem Bityutskiy if (chance(999, 1000)) 25541e51764aSArtem Bityutskiy return 0; 2555235c362bSSheng Yong ubifs_warn(c, "failing in bud LEB %d commit running", lnum); 25561e51764aSArtem Bityutskiy } else { 25571e51764aSArtem Bityutskiy if (chance(9999, 10000)) 25581e51764aSArtem Bityutskiy return 0; 2559235c362bSSheng Yong ubifs_warn(c, "failing in bud LEB %d commit not running", lnum); 25601e51764aSArtem Bityutskiy } 256124a4f800SArtem Bityutskiy 2562d27462a5SArtem Bityutskiy d->pc_happened = 1; 2563235c362bSSheng Yong ubifs_warn(c, "========== Power cut emulated =========="); 25641e51764aSArtem Bityutskiy dump_stack(); 25651e51764aSArtem Bityutskiy return 1; 25661e51764aSArtem Bityutskiy } 25671e51764aSArtem Bityutskiy 25688089ed79SArtem Bityutskiy static int corrupt_data(const struct ubifs_info *c, const void *buf, 25698089ed79SArtem Bityutskiy unsigned int len) 25701e51764aSArtem Bityutskiy { 2571cdd9fa8dSAkinobu Mita unsigned int from, to, ffs = chance(1, 2); 25721e51764aSArtem Bityutskiy unsigned char *p = (void *)buf; 25731e51764aSArtem Bityutskiy 2574*8032bf12SJason A. Donenfeld from = get_random_u32_below(len); 257558a4e237SMats Kärrman /* Corruption span max to end of write unit */ 257658a4e237SMats Kärrman to = min(len, ALIGN(from + 1, c->max_write_size)); 2577a7fa94a9SArtem Bityutskiy 2578235c362bSSheng Yong ubifs_warn(c, "filled bytes %u-%u with %s", from, to - 1, 2579a7fa94a9SArtem Bityutskiy ffs ? "0xFFs" : "random data"); 2580a7fa94a9SArtem Bityutskiy 2581a7fa94a9SArtem Bityutskiy if (ffs) 2582cdd9fa8dSAkinobu Mita memset(p + from, 0xFF, to - from); 2583a7fa94a9SArtem Bityutskiy else 2584197173dbSJason A. Donenfeld get_random_bytes(p + from, to - from); 25858089ed79SArtem Bityutskiy 25868089ed79SArtem Bityutskiy return to; 25871e51764aSArtem Bityutskiy } 25881e51764aSArtem Bityutskiy 2589f57cb188SArtem Bityutskiy int dbg_leb_write(struct ubifs_info *c, int lnum, const void *buf, 2590b36a261eSRichard Weinberger int offs, int len) 25911e51764aSArtem Bityutskiy { 259216dfd804SAdrian Hunter int err, failing; 25931e51764aSArtem Bityutskiy 25948f6983abSshengyong if (dbg_is_power_cut(c)) 25951a29af8bSArtem Bityutskiy return -EROFS; 2596d27462a5SArtem Bityutskiy 2597d27462a5SArtem Bityutskiy failing = power_cut_emulated(c, lnum, 1); 2598c23e9b75SMats Kärrman if (failing) { 25998089ed79SArtem Bityutskiy len = corrupt_data(c, buf, len); 2600235c362bSSheng Yong ubifs_warn(c, "actually write %d bytes to LEB %d:%d (the buffer was corrupted)", 26018089ed79SArtem Bityutskiy len, lnum, offs); 2602c23e9b75SMats Kärrman } 2603b36a261eSRichard Weinberger err = ubi_leb_write(c->ubi, lnum, buf, offs, len); 26041e51764aSArtem Bityutskiy if (err) 26051e51764aSArtem Bityutskiy return err; 260616dfd804SAdrian Hunter if (failing) 26071a29af8bSArtem Bityutskiy return -EROFS; 26081e51764aSArtem Bityutskiy return 0; 26091e51764aSArtem Bityutskiy } 26101e51764aSArtem Bityutskiy 2611f57cb188SArtem Bityutskiy int dbg_leb_change(struct ubifs_info *c, int lnum, const void *buf, 2612b36a261eSRichard Weinberger int len) 26131e51764aSArtem Bityutskiy { 26141e51764aSArtem Bityutskiy int err; 26151e51764aSArtem Bityutskiy 26168f6983abSshengyong if (dbg_is_power_cut(c)) 2617d27462a5SArtem Bityutskiy return -EROFS; 2618d27462a5SArtem Bityutskiy if (power_cut_emulated(c, lnum, 1)) 26191a29af8bSArtem Bityutskiy return -EROFS; 2620b36a261eSRichard Weinberger err = ubi_leb_change(c->ubi, lnum, buf, len); 26211e51764aSArtem Bityutskiy if (err) 26221e51764aSArtem Bityutskiy return err; 2623d27462a5SArtem Bityutskiy if (power_cut_emulated(c, lnum, 1)) 26241a29af8bSArtem Bityutskiy return -EROFS; 26251e51764aSArtem Bityutskiy return 0; 26261e51764aSArtem Bityutskiy } 26271e51764aSArtem Bityutskiy 2628f57cb188SArtem Bityutskiy int dbg_leb_unmap(struct ubifs_info *c, int lnum) 26291e51764aSArtem Bityutskiy { 26301e51764aSArtem Bityutskiy int err; 26311e51764aSArtem Bityutskiy 26328f6983abSshengyong if (dbg_is_power_cut(c)) 2633d27462a5SArtem Bityutskiy return -EROFS; 2634d27462a5SArtem Bityutskiy if (power_cut_emulated(c, lnum, 0)) 26351a29af8bSArtem Bityutskiy return -EROFS; 2636f57cb188SArtem Bityutskiy err = ubi_leb_unmap(c->ubi, lnum); 26371e51764aSArtem Bityutskiy if (err) 26381e51764aSArtem Bityutskiy return err; 2639d27462a5SArtem Bityutskiy if (power_cut_emulated(c, lnum, 0)) 26401a29af8bSArtem Bityutskiy return -EROFS; 26411e51764aSArtem Bityutskiy return 0; 26421e51764aSArtem Bityutskiy } 26431e51764aSArtem Bityutskiy 2644b36a261eSRichard Weinberger int dbg_leb_map(struct ubifs_info *c, int lnum) 26451e51764aSArtem Bityutskiy { 26461e51764aSArtem Bityutskiy int err; 26471e51764aSArtem Bityutskiy 26488f6983abSshengyong if (dbg_is_power_cut(c)) 2649d27462a5SArtem Bityutskiy return -EROFS; 2650d27462a5SArtem Bityutskiy if (power_cut_emulated(c, lnum, 0)) 26511a29af8bSArtem Bityutskiy return -EROFS; 2652b36a261eSRichard Weinberger err = ubi_leb_map(c->ubi, lnum); 26531e51764aSArtem Bityutskiy if (err) 26541e51764aSArtem Bityutskiy return err; 2655d27462a5SArtem Bityutskiy if (power_cut_emulated(c, lnum, 0)) 26561a29af8bSArtem Bityutskiy return -EROFS; 26571e51764aSArtem Bityutskiy return 0; 26581e51764aSArtem Bityutskiy } 26591e51764aSArtem Bityutskiy 2660552ff317SArtem Bityutskiy /* 2661552ff317SArtem Bityutskiy * Root directory for UBIFS stuff in debugfs. Contains sub-directories which 2662552ff317SArtem Bityutskiy * contain the stuff specific to particular file-system mounts. 2663552ff317SArtem Bityutskiy */ 266484abf972SArtem Bityutskiy static struct dentry *dfs_rootdir; 2665552ff317SArtem Bityutskiy 26667dae997dSArtem Bityutskiy static int dfs_file_open(struct inode *inode, struct file *file) 2667552ff317SArtem Bityutskiy { 2668552ff317SArtem Bityutskiy file->private_data = inode->i_private; 26691bbfc848SArtem Bityutskiy return nonseekable_open(inode, file); 2670552ff317SArtem Bityutskiy } 2671552ff317SArtem Bityutskiy 267228488fc2SArtem Bityutskiy /** 267328488fc2SArtem Bityutskiy * provide_user_output - provide output to the user reading a debugfs file. 267428488fc2SArtem Bityutskiy * @val: boolean value for the answer 267528488fc2SArtem Bityutskiy * @u: the buffer to store the answer at 267628488fc2SArtem Bityutskiy * @count: size of the buffer 267728488fc2SArtem Bityutskiy * @ppos: position in the @u output buffer 267828488fc2SArtem Bityutskiy * 267928488fc2SArtem Bityutskiy * This is a simple helper function which stores @val boolean value in the user 268028488fc2SArtem Bityutskiy * buffer when the user reads one of UBIFS debugfs files. Returns amount of 268128488fc2SArtem Bityutskiy * bytes written to @u in case of success and a negative error code in case of 268228488fc2SArtem Bityutskiy * failure. 268328488fc2SArtem Bityutskiy */ 268428488fc2SArtem Bityutskiy static int provide_user_output(int val, char __user *u, size_t count, 268528488fc2SArtem Bityutskiy loff_t *ppos) 268628488fc2SArtem Bityutskiy { 268728488fc2SArtem Bityutskiy char buf[3]; 268828488fc2SArtem Bityutskiy 268928488fc2SArtem Bityutskiy if (val) 269028488fc2SArtem Bityutskiy buf[0] = '1'; 269128488fc2SArtem Bityutskiy else 269228488fc2SArtem Bityutskiy buf[0] = '0'; 269328488fc2SArtem Bityutskiy buf[1] = '\n'; 269428488fc2SArtem Bityutskiy buf[2] = 0x00; 269528488fc2SArtem Bityutskiy 269628488fc2SArtem Bityutskiy return simple_read_from_buffer(u, count, ppos, buf, 2); 269728488fc2SArtem Bityutskiy } 269828488fc2SArtem Bityutskiy 269981e79d38SArtem Bityutskiy static ssize_t dfs_file_read(struct file *file, char __user *u, size_t count, 270081e79d38SArtem Bityutskiy loff_t *ppos) 270181e79d38SArtem Bityutskiy { 270281e79d38SArtem Bityutskiy struct dentry *dent = file->f_path.dentry; 270381e79d38SArtem Bityutskiy struct ubifs_info *c = file->private_data; 270481e79d38SArtem Bityutskiy struct ubifs_debug_info *d = c->dbg; 270581e79d38SArtem Bityutskiy int val; 270681e79d38SArtem Bityutskiy 270781e79d38SArtem Bityutskiy if (dent == d->dfs_chk_gen) 270881e79d38SArtem Bityutskiy val = d->chk_gen; 270981e79d38SArtem Bityutskiy else if (dent == d->dfs_chk_index) 271081e79d38SArtem Bityutskiy val = d->chk_index; 271181e79d38SArtem Bityutskiy else if (dent == d->dfs_chk_orph) 271281e79d38SArtem Bityutskiy val = d->chk_orph; 271381e79d38SArtem Bityutskiy else if (dent == d->dfs_chk_lprops) 271481e79d38SArtem Bityutskiy val = d->chk_lprops; 271581e79d38SArtem Bityutskiy else if (dent == d->dfs_chk_fs) 271681e79d38SArtem Bityutskiy val = d->chk_fs; 271781e79d38SArtem Bityutskiy else if (dent == d->dfs_tst_rcvry) 271881e79d38SArtem Bityutskiy val = d->tst_rcvry; 271906bef945SArtem Bityutskiy else if (dent == d->dfs_ro_error) 272006bef945SArtem Bityutskiy val = c->ro_error; 272181e79d38SArtem Bityutskiy else 272281e79d38SArtem Bityutskiy return -EINVAL; 272381e79d38SArtem Bityutskiy 272428488fc2SArtem Bityutskiy return provide_user_output(val, u, count, ppos); 272528488fc2SArtem Bityutskiy } 272681e79d38SArtem Bityutskiy 272728488fc2SArtem Bityutskiy /** 272828488fc2SArtem Bityutskiy * interpret_user_input - interpret user debugfs file input. 272928488fc2SArtem Bityutskiy * @u: user-provided buffer with the input 273028488fc2SArtem Bityutskiy * @count: buffer size 273128488fc2SArtem Bityutskiy * 273228488fc2SArtem Bityutskiy * This is a helper function which interpret user input to a boolean UBIFS 273328488fc2SArtem Bityutskiy * debugfs file. Returns %0 or %1 in case of success and a negative error code 273428488fc2SArtem Bityutskiy * in case of failure. 273528488fc2SArtem Bityutskiy */ 273628488fc2SArtem Bityutskiy static int interpret_user_input(const char __user *u, size_t count) 273728488fc2SArtem Bityutskiy { 273828488fc2SArtem Bityutskiy size_t buf_size; 273928488fc2SArtem Bityutskiy char buf[8]; 274028488fc2SArtem Bityutskiy 274128488fc2SArtem Bityutskiy buf_size = min_t(size_t, count, (sizeof(buf) - 1)); 274228488fc2SArtem Bityutskiy if (copy_from_user(buf, u, buf_size)) 274328488fc2SArtem Bityutskiy return -EFAULT; 274428488fc2SArtem Bityutskiy 274528488fc2SArtem Bityutskiy if (buf[0] == '1') 274628488fc2SArtem Bityutskiy return 1; 274728488fc2SArtem Bityutskiy else if (buf[0] == '0') 274828488fc2SArtem Bityutskiy return 0; 274928488fc2SArtem Bityutskiy 275028488fc2SArtem Bityutskiy return -EINVAL; 275181e79d38SArtem Bityutskiy } 275281e79d38SArtem Bityutskiy 275381e79d38SArtem Bityutskiy static ssize_t dfs_file_write(struct file *file, const char __user *u, 2754552ff317SArtem Bityutskiy size_t count, loff_t *ppos) 2755552ff317SArtem Bityutskiy { 2756552ff317SArtem Bityutskiy struct ubifs_info *c = file->private_data; 2757552ff317SArtem Bityutskiy struct ubifs_debug_info *d = c->dbg; 275881e79d38SArtem Bityutskiy struct dentry *dent = file->f_path.dentry; 275981e79d38SArtem Bityutskiy int val; 2760552ff317SArtem Bityutskiy 276181e79d38SArtem Bityutskiy if (file->f_path.dentry == d->dfs_dump_lprops) { 2762edf6be24SArtem Bityutskiy ubifs_dump_lprops(c); 276381e79d38SArtem Bityutskiy return count; 276481e79d38SArtem Bityutskiy } 276581e79d38SArtem Bityutskiy if (file->f_path.dentry == d->dfs_dump_budg) { 2766edf6be24SArtem Bityutskiy ubifs_dump_budg(c, &c->bi); 276781e79d38SArtem Bityutskiy return count; 276881e79d38SArtem Bityutskiy } 276981e79d38SArtem Bityutskiy if (file->f_path.dentry == d->dfs_dump_tnc) { 2770552ff317SArtem Bityutskiy mutex_lock(&c->tnc_mutex); 2771edf6be24SArtem Bityutskiy ubifs_dump_tnc(c); 2772552ff317SArtem Bityutskiy mutex_unlock(&c->tnc_mutex); 277381e79d38SArtem Bityutskiy return count; 277481e79d38SArtem Bityutskiy } 277581e79d38SArtem Bityutskiy 277628488fc2SArtem Bityutskiy val = interpret_user_input(u, count); 277728488fc2SArtem Bityutskiy if (val < 0) 277828488fc2SArtem Bityutskiy return val; 277981e79d38SArtem Bityutskiy 278081e79d38SArtem Bityutskiy if (dent == d->dfs_chk_gen) 278181e79d38SArtem Bityutskiy d->chk_gen = val; 278281e79d38SArtem Bityutskiy else if (dent == d->dfs_chk_index) 278381e79d38SArtem Bityutskiy d->chk_index = val; 278481e79d38SArtem Bityutskiy else if (dent == d->dfs_chk_orph) 278581e79d38SArtem Bityutskiy d->chk_orph = val; 278681e79d38SArtem Bityutskiy else if (dent == d->dfs_chk_lprops) 278781e79d38SArtem Bityutskiy d->chk_lprops = val; 278881e79d38SArtem Bityutskiy else if (dent == d->dfs_chk_fs) 278981e79d38SArtem Bityutskiy d->chk_fs = val; 279081e79d38SArtem Bityutskiy else if (dent == d->dfs_tst_rcvry) 279181e79d38SArtem Bityutskiy d->tst_rcvry = val; 279206bef945SArtem Bityutskiy else if (dent == d->dfs_ro_error) 279306bef945SArtem Bityutskiy c->ro_error = !!val; 279481e79d38SArtem Bityutskiy else 2795552ff317SArtem Bityutskiy return -EINVAL; 2796552ff317SArtem Bityutskiy 2797552ff317SArtem Bityutskiy return count; 2798552ff317SArtem Bityutskiy } 2799552ff317SArtem Bityutskiy 280084abf972SArtem Bityutskiy static const struct file_operations dfs_fops = { 28017dae997dSArtem Bityutskiy .open = dfs_file_open, 280281e79d38SArtem Bityutskiy .read = dfs_file_read, 280381e79d38SArtem Bityutskiy .write = dfs_file_write, 2804552ff317SArtem Bityutskiy .owner = THIS_MODULE, 28051bbfc848SArtem Bityutskiy .llseek = no_llseek, 2806552ff317SArtem Bityutskiy }; 2807552ff317SArtem Bityutskiy 2808552ff317SArtem Bityutskiy /** 2809552ff317SArtem Bityutskiy * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance. 2810552ff317SArtem Bityutskiy * @c: UBIFS file-system description object 2811552ff317SArtem Bityutskiy * 2812702d6a83SGreg Kroah-Hartman * This function creates all debugfs files for this instance of UBIFS. 2813552ff317SArtem Bityutskiy * 2814552ff317SArtem Bityutskiy * Note, the only reason we have not merged this function with the 2815552ff317SArtem Bityutskiy * 'ubifs_debugging_init()' function is because it is better to initialize 2816552ff317SArtem Bityutskiy * debugfs interfaces at the very end of the mount process, and remove them at 2817552ff317SArtem Bityutskiy * the very beginning of the mount process. 2818552ff317SArtem Bityutskiy */ 2819702d6a83SGreg Kroah-Hartman void dbg_debugfs_init_fs(struct ubifs_info *c) 2820552ff317SArtem Bityutskiy { 2821d71cac59SGreg Kroah-Hartman int n; 2822552ff317SArtem Bityutskiy const char *fname; 2823552ff317SArtem Bityutskiy struct ubifs_debug_info *d = c->dbg; 2824552ff317SArtem Bityutskiy 2825ae380ce0SArtem Bityutskiy n = snprintf(d->dfs_dir_name, UBIFS_DFS_DIR_LEN + 1, UBIFS_DFS_DIR_NAME, 2826ae380ce0SArtem Bityutskiy c->vi.ubi_num, c->vi.vol_id); 2827be076fdfSDan Carpenter if (n > UBIFS_DFS_DIR_LEN) { 2828ae380ce0SArtem Bityutskiy /* The array size is too small */ 2829702d6a83SGreg Kroah-Hartman return; 2830ae380ce0SArtem Bityutskiy } 2831ae380ce0SArtem Bityutskiy 2832cc6a86b9SArtem Bityutskiy fname = d->dfs_dir_name; 2833702d6a83SGreg Kroah-Hartman d->dfs_dir = debugfs_create_dir(fname, dfs_rootdir); 2834552ff317SArtem Bityutskiy 2835552ff317SArtem Bityutskiy fname = "dump_lprops"; 2836702d6a83SGreg Kroah-Hartman d->dfs_dump_lprops = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, 2837702d6a83SGreg Kroah-Hartman &dfs_fops); 2838552ff317SArtem Bityutskiy 2839552ff317SArtem Bityutskiy fname = "dump_budg"; 2840702d6a83SGreg Kroah-Hartman d->dfs_dump_budg = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, 2841702d6a83SGreg Kroah-Hartman &dfs_fops); 2842552ff317SArtem Bityutskiy 2843552ff317SArtem Bityutskiy fname = "dump_tnc"; 2844702d6a83SGreg Kroah-Hartman d->dfs_dump_tnc = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, 2845702d6a83SGreg Kroah-Hartman &dfs_fops); 2846552ff317SArtem Bityutskiy 284781e79d38SArtem Bityutskiy fname = "chk_general"; 2848702d6a83SGreg Kroah-Hartman d->dfs_chk_gen = debugfs_create_file(fname, S_IRUSR | S_IWUSR, 2849702d6a83SGreg Kroah-Hartman d->dfs_dir, c, &dfs_fops); 285081e79d38SArtem Bityutskiy 285181e79d38SArtem Bityutskiy fname = "chk_index"; 2852702d6a83SGreg Kroah-Hartman d->dfs_chk_index = debugfs_create_file(fname, S_IRUSR | S_IWUSR, 2853702d6a83SGreg Kroah-Hartman d->dfs_dir, c, &dfs_fops); 285481e79d38SArtem Bityutskiy 285581e79d38SArtem Bityutskiy fname = "chk_orphans"; 2856702d6a83SGreg Kroah-Hartman d->dfs_chk_orph = debugfs_create_file(fname, S_IRUSR | S_IWUSR, 2857702d6a83SGreg Kroah-Hartman d->dfs_dir, c, &dfs_fops); 285881e79d38SArtem Bityutskiy 285981e79d38SArtem Bityutskiy fname = "chk_lprops"; 2860702d6a83SGreg Kroah-Hartman d->dfs_chk_lprops = debugfs_create_file(fname, S_IRUSR | S_IWUSR, 2861702d6a83SGreg Kroah-Hartman d->dfs_dir, c, &dfs_fops); 286281e79d38SArtem Bityutskiy 286381e79d38SArtem Bityutskiy fname = "chk_fs"; 2864702d6a83SGreg Kroah-Hartman d->dfs_chk_fs = debugfs_create_file(fname, S_IRUSR | S_IWUSR, 2865702d6a83SGreg Kroah-Hartman d->dfs_dir, c, &dfs_fops); 286681e79d38SArtem Bityutskiy 286781e79d38SArtem Bityutskiy fname = "tst_recovery"; 2868702d6a83SGreg Kroah-Hartman d->dfs_tst_rcvry = debugfs_create_file(fname, S_IRUSR | S_IWUSR, 2869702d6a83SGreg Kroah-Hartman d->dfs_dir, c, &dfs_fops); 287081e79d38SArtem Bityutskiy 287106bef945SArtem Bityutskiy fname = "ro_error"; 2872702d6a83SGreg Kroah-Hartman d->dfs_ro_error = debugfs_create_file(fname, S_IRUSR | S_IWUSR, 2873702d6a83SGreg Kroah-Hartman d->dfs_dir, c, &dfs_fops); 2874552ff317SArtem Bityutskiy } 2875552ff317SArtem Bityutskiy 2876552ff317SArtem Bityutskiy /** 2877552ff317SArtem Bityutskiy * dbg_debugfs_exit_fs - remove all debugfs files. 2878552ff317SArtem Bityutskiy * @c: UBIFS file-system description object 2879552ff317SArtem Bityutskiy */ 2880552ff317SArtem Bityutskiy void dbg_debugfs_exit_fs(struct ubifs_info *c) 2881552ff317SArtem Bityutskiy { 288284abf972SArtem Bityutskiy debugfs_remove_recursive(c->dbg->dfs_dir); 2883552ff317SArtem Bityutskiy } 2884552ff317SArtem Bityutskiy 2885e7717060SArtem Bityutskiy struct ubifs_global_debug_info ubifs_dbg; 2886e7717060SArtem Bityutskiy 2887e7717060SArtem Bityutskiy static struct dentry *dfs_chk_gen; 2888e7717060SArtem Bityutskiy static struct dentry *dfs_chk_index; 2889e7717060SArtem Bityutskiy static struct dentry *dfs_chk_orph; 2890e7717060SArtem Bityutskiy static struct dentry *dfs_chk_lprops; 2891e7717060SArtem Bityutskiy static struct dentry *dfs_chk_fs; 2892e7717060SArtem Bityutskiy static struct dentry *dfs_tst_rcvry; 2893e7717060SArtem Bityutskiy 2894e7717060SArtem Bityutskiy static ssize_t dfs_global_file_read(struct file *file, char __user *u, 2895e7717060SArtem Bityutskiy size_t count, loff_t *ppos) 2896e7717060SArtem Bityutskiy { 2897e7717060SArtem Bityutskiy struct dentry *dent = file->f_path.dentry; 2898e7717060SArtem Bityutskiy int val; 2899e7717060SArtem Bityutskiy 2900e7717060SArtem Bityutskiy if (dent == dfs_chk_gen) 2901e7717060SArtem Bityutskiy val = ubifs_dbg.chk_gen; 2902e7717060SArtem Bityutskiy else if (dent == dfs_chk_index) 2903e7717060SArtem Bityutskiy val = ubifs_dbg.chk_index; 2904e7717060SArtem Bityutskiy else if (dent == dfs_chk_orph) 2905e7717060SArtem Bityutskiy val = ubifs_dbg.chk_orph; 2906e7717060SArtem Bityutskiy else if (dent == dfs_chk_lprops) 2907e7717060SArtem Bityutskiy val = ubifs_dbg.chk_lprops; 2908e7717060SArtem Bityutskiy else if (dent == dfs_chk_fs) 2909e7717060SArtem Bityutskiy val = ubifs_dbg.chk_fs; 2910e7717060SArtem Bityutskiy else if (dent == dfs_tst_rcvry) 2911e7717060SArtem Bityutskiy val = ubifs_dbg.tst_rcvry; 2912e7717060SArtem Bityutskiy else 2913e7717060SArtem Bityutskiy return -EINVAL; 2914e7717060SArtem Bityutskiy 2915e7717060SArtem Bityutskiy return provide_user_output(val, u, count, ppos); 2916e7717060SArtem Bityutskiy } 2917e7717060SArtem Bityutskiy 2918e7717060SArtem Bityutskiy static ssize_t dfs_global_file_write(struct file *file, const char __user *u, 2919e7717060SArtem Bityutskiy size_t count, loff_t *ppos) 2920e7717060SArtem Bityutskiy { 2921e7717060SArtem Bityutskiy struct dentry *dent = file->f_path.dentry; 2922e7717060SArtem Bityutskiy int val; 2923e7717060SArtem Bityutskiy 2924e7717060SArtem Bityutskiy val = interpret_user_input(u, count); 2925e7717060SArtem Bityutskiy if (val < 0) 2926e7717060SArtem Bityutskiy return val; 2927e7717060SArtem Bityutskiy 2928e7717060SArtem Bityutskiy if (dent == dfs_chk_gen) 2929e7717060SArtem Bityutskiy ubifs_dbg.chk_gen = val; 2930e7717060SArtem Bityutskiy else if (dent == dfs_chk_index) 2931e7717060SArtem Bityutskiy ubifs_dbg.chk_index = val; 2932e7717060SArtem Bityutskiy else if (dent == dfs_chk_orph) 2933e7717060SArtem Bityutskiy ubifs_dbg.chk_orph = val; 2934e7717060SArtem Bityutskiy else if (dent == dfs_chk_lprops) 2935e7717060SArtem Bityutskiy ubifs_dbg.chk_lprops = val; 2936e7717060SArtem Bityutskiy else if (dent == dfs_chk_fs) 2937e7717060SArtem Bityutskiy ubifs_dbg.chk_fs = val; 2938e7717060SArtem Bityutskiy else if (dent == dfs_tst_rcvry) 2939e7717060SArtem Bityutskiy ubifs_dbg.tst_rcvry = val; 2940e7717060SArtem Bityutskiy else 2941e7717060SArtem Bityutskiy return -EINVAL; 2942e7717060SArtem Bityutskiy 2943e7717060SArtem Bityutskiy return count; 2944e7717060SArtem Bityutskiy } 2945e7717060SArtem Bityutskiy 2946e7717060SArtem Bityutskiy static const struct file_operations dfs_global_fops = { 2947e7717060SArtem Bityutskiy .read = dfs_global_file_read, 2948e7717060SArtem Bityutskiy .write = dfs_global_file_write, 2949e7717060SArtem Bityutskiy .owner = THIS_MODULE, 2950e7717060SArtem Bityutskiy .llseek = no_llseek, 2951e7717060SArtem Bityutskiy }; 2952e7717060SArtem Bityutskiy 29537dae997dSArtem Bityutskiy /** 29547dae997dSArtem Bityutskiy * dbg_debugfs_init - initialize debugfs file-system. 29557dae997dSArtem Bityutskiy * 29567dae997dSArtem Bityutskiy * UBIFS uses debugfs file-system to expose various debugging knobs to 29577dae997dSArtem Bityutskiy * user-space. This function creates "ubifs" directory in the debugfs 2958702d6a83SGreg Kroah-Hartman * file-system. 29597dae997dSArtem Bityutskiy */ 2960702d6a83SGreg Kroah-Hartman void dbg_debugfs_init(void) 29617dae997dSArtem Bityutskiy { 2962e7717060SArtem Bityutskiy const char *fname; 2963818039c7SArtem Bityutskiy 2964e7717060SArtem Bityutskiy fname = "ubifs"; 2965702d6a83SGreg Kroah-Hartman dfs_rootdir = debugfs_create_dir(fname, NULL); 2966e7717060SArtem Bityutskiy 2967e7717060SArtem Bityutskiy fname = "chk_general"; 2968702d6a83SGreg Kroah-Hartman dfs_chk_gen = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, 2969702d6a83SGreg Kroah-Hartman NULL, &dfs_global_fops); 2970e7717060SArtem Bityutskiy 2971e7717060SArtem Bityutskiy fname = "chk_index"; 2972702d6a83SGreg Kroah-Hartman dfs_chk_index = debugfs_create_file(fname, S_IRUSR | S_IWUSR, 2973702d6a83SGreg Kroah-Hartman dfs_rootdir, NULL, &dfs_global_fops); 2974e7717060SArtem Bityutskiy 2975e7717060SArtem Bityutskiy fname = "chk_orphans"; 2976702d6a83SGreg Kroah-Hartman dfs_chk_orph = debugfs_create_file(fname, S_IRUSR | S_IWUSR, 2977702d6a83SGreg Kroah-Hartman dfs_rootdir, NULL, &dfs_global_fops); 2978e7717060SArtem Bityutskiy 2979e7717060SArtem Bityutskiy fname = "chk_lprops"; 2980702d6a83SGreg Kroah-Hartman dfs_chk_lprops = debugfs_create_file(fname, S_IRUSR | S_IWUSR, 2981702d6a83SGreg Kroah-Hartman dfs_rootdir, NULL, &dfs_global_fops); 2982e7717060SArtem Bityutskiy 2983e7717060SArtem Bityutskiy fname = "chk_fs"; 2984702d6a83SGreg Kroah-Hartman dfs_chk_fs = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, 2985702d6a83SGreg Kroah-Hartman NULL, &dfs_global_fops); 2986e7717060SArtem Bityutskiy 2987e7717060SArtem Bityutskiy fname = "tst_recovery"; 2988702d6a83SGreg Kroah-Hartman dfs_tst_rcvry = debugfs_create_file(fname, S_IRUSR | S_IWUSR, 2989702d6a83SGreg Kroah-Hartman dfs_rootdir, NULL, &dfs_global_fops); 29907dae997dSArtem Bityutskiy } 29917dae997dSArtem Bityutskiy 29927dae997dSArtem Bityutskiy /** 29937dae997dSArtem Bityutskiy * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system. 29947dae997dSArtem Bityutskiy */ 29957dae997dSArtem Bityutskiy void dbg_debugfs_exit(void) 29967dae997dSArtem Bityutskiy { 2997e7717060SArtem Bityutskiy debugfs_remove_recursive(dfs_rootdir); 29987dae997dSArtem Bityutskiy } 29997dae997dSArtem Bityutskiy 30002e52eb74SRichard Weinberger void ubifs_assert_failed(struct ubifs_info *c, const char *expr, 30012e52eb74SRichard Weinberger const char *file, int line) 30022e52eb74SRichard Weinberger { 30032e52eb74SRichard Weinberger ubifs_err(c, "UBIFS assert failed: %s, in %s:%u", expr, file, line); 30042e52eb74SRichard Weinberger 30052e52eb74SRichard Weinberger switch (c->assert_action) { 30062e52eb74SRichard Weinberger case ASSACT_PANIC: 30072e52eb74SRichard Weinberger BUG(); 30082e52eb74SRichard Weinberger break; 30092e52eb74SRichard Weinberger 30102e52eb74SRichard Weinberger case ASSACT_RO: 30112e52eb74SRichard Weinberger ubifs_ro_mode(c, -EINVAL); 30122e52eb74SRichard Weinberger break; 30132e52eb74SRichard Weinberger 30142e52eb74SRichard Weinberger case ASSACT_REPORT: 30152e52eb74SRichard Weinberger default: 30162e52eb74SRichard Weinberger dump_stack(); 30172e52eb74SRichard Weinberger break; 30182e52eb74SRichard Weinberger 30192e52eb74SRichard Weinberger } 30202e52eb74SRichard Weinberger } 30212e52eb74SRichard Weinberger 30227dae997dSArtem Bityutskiy /** 30237dae997dSArtem Bityutskiy * ubifs_debugging_init - initialize UBIFS debugging. 30247dae997dSArtem Bityutskiy * @c: UBIFS file-system description object 30257dae997dSArtem Bityutskiy * 30267dae997dSArtem Bityutskiy * This function initializes debugging-related data for the file system. 30277dae997dSArtem Bityutskiy * Returns zero in case of success and a negative error code in case of 30287dae997dSArtem Bityutskiy * failure. 30297dae997dSArtem Bityutskiy */ 30307dae997dSArtem Bityutskiy int ubifs_debugging_init(struct ubifs_info *c) 30317dae997dSArtem Bityutskiy { 30327dae997dSArtem Bityutskiy c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL); 30337dae997dSArtem Bityutskiy if (!c->dbg) 30347dae997dSArtem Bityutskiy return -ENOMEM; 30357dae997dSArtem Bityutskiy 30367dae997dSArtem Bityutskiy return 0; 30377dae997dSArtem Bityutskiy } 30387dae997dSArtem Bityutskiy 30397dae997dSArtem Bityutskiy /** 30407dae997dSArtem Bityutskiy * ubifs_debugging_exit - free debugging data. 30417dae997dSArtem Bityutskiy * @c: UBIFS file-system description object 30427dae997dSArtem Bityutskiy */ 30437dae997dSArtem Bityutskiy void ubifs_debugging_exit(struct ubifs_info *c) 30447dae997dSArtem Bityutskiy { 30457dae997dSArtem Bityutskiy kfree(c->dbg); 30467dae997dSArtem Bityutskiy } 3047