11e51764aSArtem Bityutskiy /* 21e51764aSArtem Bityutskiy * This file is part of UBIFS. 31e51764aSArtem Bityutskiy * 41e51764aSArtem Bityutskiy * Copyright (C) 2006-2008 Nokia Corporation 51e51764aSArtem Bityutskiy * 61e51764aSArtem Bityutskiy * This program is free software; you can redistribute it and/or modify it 71e51764aSArtem Bityutskiy * under the terms of the GNU General Public License version 2 as published by 81e51764aSArtem Bityutskiy * the Free Software Foundation. 91e51764aSArtem Bityutskiy * 101e51764aSArtem Bityutskiy * This program is distributed in the hope that it will be useful, but WITHOUT 111e51764aSArtem Bityutskiy * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 121e51764aSArtem Bityutskiy * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 131e51764aSArtem Bityutskiy * more details. 141e51764aSArtem Bityutskiy * 151e51764aSArtem Bityutskiy * You should have received a copy of the GNU General Public License along with 161e51764aSArtem Bityutskiy * this program; if not, write to the Free Software Foundation, Inc., 51 171e51764aSArtem Bityutskiy * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 181e51764aSArtem Bityutskiy * 191e51764aSArtem Bityutskiy * Authors: Artem Bityutskiy (Битюцкий Артём) 201e51764aSArtem Bityutskiy * Adrian Hunter 211e51764aSArtem Bityutskiy */ 221e51764aSArtem Bityutskiy 231e51764aSArtem Bityutskiy /* 241e51764aSArtem Bityutskiy * This file implements most of the debugging stuff which is compiled in only 251e51764aSArtem Bityutskiy * when it is enabled. But some debugging check functions are implemented in 261e51764aSArtem Bityutskiy * corresponding subsystem, just because they are closely related and utilize 271e51764aSArtem Bityutskiy * various local functions of those subsystems. 281e51764aSArtem Bityutskiy */ 291e51764aSArtem Bityutskiy 301e51764aSArtem Bityutskiy #define UBIFS_DBG_PRESERVE_UBI 311e51764aSArtem Bityutskiy 321e51764aSArtem Bityutskiy #include "ubifs.h" 331e51764aSArtem Bityutskiy #include <linux/module.h> 341e51764aSArtem Bityutskiy #include <linux/moduleparam.h> 35552ff317SArtem Bityutskiy #include <linux/debugfs.h> 36*4d61db4fSArtem Bityutskiy #include <linux/math64.h> 371e51764aSArtem Bityutskiy 381e51764aSArtem Bityutskiy #ifdef CONFIG_UBIFS_FS_DEBUG 391e51764aSArtem Bityutskiy 401e51764aSArtem Bityutskiy DEFINE_SPINLOCK(dbg_lock); 411e51764aSArtem Bityutskiy 421e51764aSArtem Bityutskiy static char dbg_key_buf0[128]; 431e51764aSArtem Bityutskiy static char dbg_key_buf1[128]; 441e51764aSArtem Bityutskiy 451e51764aSArtem Bityutskiy unsigned int ubifs_msg_flags = UBIFS_MSG_FLAGS_DEFAULT; 461e51764aSArtem Bityutskiy unsigned int ubifs_chk_flags = UBIFS_CHK_FLAGS_DEFAULT; 471e51764aSArtem Bityutskiy unsigned int ubifs_tst_flags; 481e51764aSArtem Bityutskiy 491e51764aSArtem Bityutskiy module_param_named(debug_msgs, ubifs_msg_flags, uint, S_IRUGO | S_IWUSR); 501e51764aSArtem Bityutskiy module_param_named(debug_chks, ubifs_chk_flags, uint, S_IRUGO | S_IWUSR); 511e51764aSArtem Bityutskiy module_param_named(debug_tsts, ubifs_tst_flags, uint, S_IRUGO | S_IWUSR); 521e51764aSArtem Bityutskiy 531e51764aSArtem Bityutskiy MODULE_PARM_DESC(debug_msgs, "Debug message type flags"); 541e51764aSArtem Bityutskiy MODULE_PARM_DESC(debug_chks, "Debug check flags"); 551e51764aSArtem Bityutskiy MODULE_PARM_DESC(debug_tsts, "Debug special test flags"); 561e51764aSArtem Bityutskiy 571e51764aSArtem Bityutskiy static const char *get_key_fmt(int fmt) 581e51764aSArtem Bityutskiy { 591e51764aSArtem Bityutskiy switch (fmt) { 601e51764aSArtem Bityutskiy case UBIFS_SIMPLE_KEY_FMT: 611e51764aSArtem Bityutskiy return "simple"; 621e51764aSArtem Bityutskiy default: 631e51764aSArtem Bityutskiy return "unknown/invalid format"; 641e51764aSArtem Bityutskiy } 651e51764aSArtem Bityutskiy } 661e51764aSArtem Bityutskiy 671e51764aSArtem Bityutskiy static const char *get_key_hash(int hash) 681e51764aSArtem Bityutskiy { 691e51764aSArtem Bityutskiy switch (hash) { 701e51764aSArtem Bityutskiy case UBIFS_KEY_HASH_R5: 711e51764aSArtem Bityutskiy return "R5"; 721e51764aSArtem Bityutskiy case UBIFS_KEY_HASH_TEST: 731e51764aSArtem Bityutskiy return "test"; 741e51764aSArtem Bityutskiy default: 751e51764aSArtem Bityutskiy return "unknown/invalid name hash"; 761e51764aSArtem Bityutskiy } 771e51764aSArtem Bityutskiy } 781e51764aSArtem Bityutskiy 791e51764aSArtem Bityutskiy static const char *get_key_type(int type) 801e51764aSArtem Bityutskiy { 811e51764aSArtem Bityutskiy switch (type) { 821e51764aSArtem Bityutskiy case UBIFS_INO_KEY: 831e51764aSArtem Bityutskiy return "inode"; 841e51764aSArtem Bityutskiy case UBIFS_DENT_KEY: 851e51764aSArtem Bityutskiy return "direntry"; 861e51764aSArtem Bityutskiy case UBIFS_XENT_KEY: 871e51764aSArtem Bityutskiy return "xentry"; 881e51764aSArtem Bityutskiy case UBIFS_DATA_KEY: 891e51764aSArtem Bityutskiy return "data"; 901e51764aSArtem Bityutskiy case UBIFS_TRUN_KEY: 911e51764aSArtem Bityutskiy return "truncate"; 921e51764aSArtem Bityutskiy default: 931e51764aSArtem Bityutskiy return "unknown/invalid key"; 941e51764aSArtem Bityutskiy } 951e51764aSArtem Bityutskiy } 961e51764aSArtem Bityutskiy 971e51764aSArtem Bityutskiy static void sprintf_key(const struct ubifs_info *c, const union ubifs_key *key, 981e51764aSArtem Bityutskiy char *buffer) 991e51764aSArtem Bityutskiy { 1001e51764aSArtem Bityutskiy char *p = buffer; 1011e51764aSArtem Bityutskiy int type = key_type(c, key); 1021e51764aSArtem Bityutskiy 1031e51764aSArtem Bityutskiy if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) { 1041e51764aSArtem Bityutskiy switch (type) { 1051e51764aSArtem Bityutskiy case UBIFS_INO_KEY: 106e84461adSArtem Bityutskiy sprintf(p, "(%lu, %s)", (unsigned long)key_inum(c, key), 1071e51764aSArtem Bityutskiy get_key_type(type)); 1081e51764aSArtem Bityutskiy break; 1091e51764aSArtem Bityutskiy case UBIFS_DENT_KEY: 1101e51764aSArtem Bityutskiy case UBIFS_XENT_KEY: 111e84461adSArtem Bityutskiy sprintf(p, "(%lu, %s, %#08x)", 112e84461adSArtem Bityutskiy (unsigned long)key_inum(c, key), 1131e51764aSArtem Bityutskiy get_key_type(type), key_hash(c, key)); 1141e51764aSArtem Bityutskiy break; 1151e51764aSArtem Bityutskiy case UBIFS_DATA_KEY: 116e84461adSArtem Bityutskiy sprintf(p, "(%lu, %s, %u)", 117e84461adSArtem Bityutskiy (unsigned long)key_inum(c, key), 1181e51764aSArtem Bityutskiy get_key_type(type), key_block(c, key)); 1191e51764aSArtem Bityutskiy break; 1201e51764aSArtem Bityutskiy case UBIFS_TRUN_KEY: 1211e51764aSArtem Bityutskiy sprintf(p, "(%lu, %s)", 122e84461adSArtem Bityutskiy (unsigned long)key_inum(c, key), 123e84461adSArtem Bityutskiy get_key_type(type)); 1241e51764aSArtem Bityutskiy break; 1251e51764aSArtem Bityutskiy default: 1261e51764aSArtem Bityutskiy sprintf(p, "(bad key type: %#08x, %#08x)", 1271e51764aSArtem Bityutskiy key->u32[0], key->u32[1]); 1281e51764aSArtem Bityutskiy } 1291e51764aSArtem Bityutskiy } else 1301e51764aSArtem Bityutskiy sprintf(p, "bad key format %d", c->key_fmt); 1311e51764aSArtem Bityutskiy } 1321e51764aSArtem Bityutskiy 1331e51764aSArtem Bityutskiy const char *dbg_key_str0(const struct ubifs_info *c, const union ubifs_key *key) 1341e51764aSArtem Bityutskiy { 1351e51764aSArtem Bityutskiy /* dbg_lock must be held */ 1361e51764aSArtem Bityutskiy sprintf_key(c, key, dbg_key_buf0); 1371e51764aSArtem Bityutskiy return dbg_key_buf0; 1381e51764aSArtem Bityutskiy } 1391e51764aSArtem Bityutskiy 1401e51764aSArtem Bityutskiy const char *dbg_key_str1(const struct ubifs_info *c, const union ubifs_key *key) 1411e51764aSArtem Bityutskiy { 1421e51764aSArtem Bityutskiy /* dbg_lock must be held */ 1431e51764aSArtem Bityutskiy sprintf_key(c, key, dbg_key_buf1); 1441e51764aSArtem Bityutskiy return dbg_key_buf1; 1451e51764aSArtem Bityutskiy } 1461e51764aSArtem Bityutskiy 1471e51764aSArtem Bityutskiy const char *dbg_ntype(int type) 1481e51764aSArtem Bityutskiy { 1491e51764aSArtem Bityutskiy switch (type) { 1501e51764aSArtem Bityutskiy case UBIFS_PAD_NODE: 1511e51764aSArtem Bityutskiy return "padding node"; 1521e51764aSArtem Bityutskiy case UBIFS_SB_NODE: 1531e51764aSArtem Bityutskiy return "superblock node"; 1541e51764aSArtem Bityutskiy case UBIFS_MST_NODE: 1551e51764aSArtem Bityutskiy return "master node"; 1561e51764aSArtem Bityutskiy case UBIFS_REF_NODE: 1571e51764aSArtem Bityutskiy return "reference node"; 1581e51764aSArtem Bityutskiy case UBIFS_INO_NODE: 1591e51764aSArtem Bityutskiy return "inode node"; 1601e51764aSArtem Bityutskiy case UBIFS_DENT_NODE: 1611e51764aSArtem Bityutskiy return "direntry node"; 1621e51764aSArtem Bityutskiy case UBIFS_XENT_NODE: 1631e51764aSArtem Bityutskiy return "xentry node"; 1641e51764aSArtem Bityutskiy case UBIFS_DATA_NODE: 1651e51764aSArtem Bityutskiy return "data node"; 1661e51764aSArtem Bityutskiy case UBIFS_TRUN_NODE: 1671e51764aSArtem Bityutskiy return "truncate node"; 1681e51764aSArtem Bityutskiy case UBIFS_IDX_NODE: 1691e51764aSArtem Bityutskiy return "indexing node"; 1701e51764aSArtem Bityutskiy case UBIFS_CS_NODE: 1711e51764aSArtem Bityutskiy return "commit start node"; 1721e51764aSArtem Bityutskiy case UBIFS_ORPH_NODE: 1731e51764aSArtem Bityutskiy return "orphan node"; 1741e51764aSArtem Bityutskiy default: 1751e51764aSArtem Bityutskiy return "unknown node"; 1761e51764aSArtem Bityutskiy } 1771e51764aSArtem Bityutskiy } 1781e51764aSArtem Bityutskiy 1791e51764aSArtem Bityutskiy static const char *dbg_gtype(int type) 1801e51764aSArtem Bityutskiy { 1811e51764aSArtem Bityutskiy switch (type) { 1821e51764aSArtem Bityutskiy case UBIFS_NO_NODE_GROUP: 1831e51764aSArtem Bityutskiy return "no node group"; 1841e51764aSArtem Bityutskiy case UBIFS_IN_NODE_GROUP: 1851e51764aSArtem Bityutskiy return "in node group"; 1861e51764aSArtem Bityutskiy case UBIFS_LAST_OF_NODE_GROUP: 1871e51764aSArtem Bityutskiy return "last of node group"; 1881e51764aSArtem Bityutskiy default: 1891e51764aSArtem Bityutskiy return "unknown"; 1901e51764aSArtem Bityutskiy } 1911e51764aSArtem Bityutskiy } 1921e51764aSArtem Bityutskiy 1931e51764aSArtem Bityutskiy const char *dbg_cstate(int cmt_state) 1941e51764aSArtem Bityutskiy { 1951e51764aSArtem Bityutskiy switch (cmt_state) { 1961e51764aSArtem Bityutskiy case COMMIT_RESTING: 1971e51764aSArtem Bityutskiy return "commit resting"; 1981e51764aSArtem Bityutskiy case COMMIT_BACKGROUND: 1991e51764aSArtem Bityutskiy return "background commit requested"; 2001e51764aSArtem Bityutskiy case COMMIT_REQUIRED: 2011e51764aSArtem Bityutskiy return "commit required"; 2021e51764aSArtem Bityutskiy case COMMIT_RUNNING_BACKGROUND: 2031e51764aSArtem Bityutskiy return "BACKGROUND commit running"; 2041e51764aSArtem Bityutskiy case COMMIT_RUNNING_REQUIRED: 2051e51764aSArtem Bityutskiy return "commit running and required"; 2061e51764aSArtem Bityutskiy case COMMIT_BROKEN: 2071e51764aSArtem Bityutskiy return "broken commit"; 2081e51764aSArtem Bityutskiy default: 2091e51764aSArtem Bityutskiy return "unknown commit state"; 2101e51764aSArtem Bityutskiy } 2111e51764aSArtem Bityutskiy } 2121e51764aSArtem Bityutskiy 2131e51764aSArtem Bityutskiy static void dump_ch(const struct ubifs_ch *ch) 2141e51764aSArtem Bityutskiy { 2151e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tmagic %#x\n", le32_to_cpu(ch->magic)); 2161e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tcrc %#x\n", le32_to_cpu(ch->crc)); 2171e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tnode_type %d (%s)\n", ch->node_type, 2181e51764aSArtem Bityutskiy dbg_ntype(ch->node_type)); 2191e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tgroup_type %d (%s)\n", ch->group_type, 2201e51764aSArtem Bityutskiy dbg_gtype(ch->group_type)); 2211e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tsqnum %llu\n", 2221e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(ch->sqnum)); 2231e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlen %u\n", le32_to_cpu(ch->len)); 2241e51764aSArtem Bityutskiy } 2251e51764aSArtem Bityutskiy 2261e51764aSArtem Bityutskiy void dbg_dump_inode(const struct ubifs_info *c, const struct inode *inode) 2271e51764aSArtem Bityutskiy { 2281e51764aSArtem Bityutskiy const struct ubifs_inode *ui = ubifs_inode(inode); 2291e51764aSArtem Bityutskiy 230b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "Dump in-memory inode:"); 231b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tinode %lu\n", inode->i_ino); 232b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tsize %llu\n", 2331e51764aSArtem Bityutskiy (unsigned long long)i_size_read(inode)); 234b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tnlink %u\n", inode->i_nlink); 235b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tuid %u\n", (unsigned int)inode->i_uid); 236b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tgid %u\n", (unsigned int)inode->i_gid); 237b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tatime %u.%u\n", 2381e51764aSArtem Bityutskiy (unsigned int)inode->i_atime.tv_sec, 2391e51764aSArtem Bityutskiy (unsigned int)inode->i_atime.tv_nsec); 240b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tmtime %u.%u\n", 2411e51764aSArtem Bityutskiy (unsigned int)inode->i_mtime.tv_sec, 2421e51764aSArtem Bityutskiy (unsigned int)inode->i_mtime.tv_nsec); 243b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tctime %u.%u\n", 2441e51764aSArtem Bityutskiy (unsigned int)inode->i_ctime.tv_sec, 2451e51764aSArtem Bityutskiy (unsigned int)inode->i_ctime.tv_nsec); 246b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tcreat_sqnum %llu\n", ui->creat_sqnum); 247b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\txattr_size %u\n", ui->xattr_size); 248b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\txattr_cnt %u\n", ui->xattr_cnt); 249b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\txattr_names %u\n", ui->xattr_names); 250b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tdirty %u\n", ui->dirty); 251b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\txattr %u\n", ui->xattr); 252b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tbulk_read %u\n", ui->xattr); 253b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tsynced_i_size %llu\n", 254b5e426e9SArtem Bityutskiy (unsigned long long)ui->synced_i_size); 255b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tui_size %llu\n", 256b5e426e9SArtem Bityutskiy (unsigned long long)ui->ui_size); 257b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tflags %d\n", ui->flags); 258b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tcompr_type %d\n", ui->compr_type); 259b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tlast_page_read %lu\n", ui->last_page_read); 260b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tread_in_a_row %lu\n", ui->read_in_a_row); 261b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tdata_len %d\n", ui->data_len); 2621e51764aSArtem Bityutskiy } 2631e51764aSArtem Bityutskiy 2641e51764aSArtem Bityutskiy void dbg_dump_node(const struct ubifs_info *c, const void *node) 2651e51764aSArtem Bityutskiy { 2661e51764aSArtem Bityutskiy int i, n; 2671e51764aSArtem Bityutskiy union ubifs_key key; 2681e51764aSArtem Bityutskiy const struct ubifs_ch *ch = node; 2691e51764aSArtem Bityutskiy 2701e51764aSArtem Bityutskiy if (dbg_failure_mode) 2711e51764aSArtem Bityutskiy return; 2721e51764aSArtem Bityutskiy 2731e51764aSArtem Bityutskiy /* If the magic is incorrect, just hexdump the first bytes */ 2741e51764aSArtem Bityutskiy if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) { 2751e51764aSArtem Bityutskiy printk(KERN_DEBUG "Not a node, first %zu bytes:", UBIFS_CH_SZ); 2761e51764aSArtem Bityutskiy print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, 2771e51764aSArtem Bityutskiy (void *)node, UBIFS_CH_SZ, 1); 2781e51764aSArtem Bityutskiy return; 2791e51764aSArtem Bityutskiy } 2801e51764aSArtem Bityutskiy 2811e51764aSArtem Bityutskiy spin_lock(&dbg_lock); 2821e51764aSArtem Bityutskiy dump_ch(node); 2831e51764aSArtem Bityutskiy 2841e51764aSArtem Bityutskiy switch (ch->node_type) { 2851e51764aSArtem Bityutskiy case UBIFS_PAD_NODE: 2861e51764aSArtem Bityutskiy { 2871e51764aSArtem Bityutskiy const struct ubifs_pad_node *pad = node; 2881e51764aSArtem Bityutskiy 2891e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tpad_len %u\n", 2901e51764aSArtem Bityutskiy le32_to_cpu(pad->pad_len)); 2911e51764aSArtem Bityutskiy break; 2921e51764aSArtem Bityutskiy } 2931e51764aSArtem Bityutskiy case UBIFS_SB_NODE: 2941e51764aSArtem Bityutskiy { 2951e51764aSArtem Bityutskiy const struct ubifs_sb_node *sup = node; 2961e51764aSArtem Bityutskiy unsigned int sup_flags = le32_to_cpu(sup->flags); 2971e51764aSArtem Bityutskiy 2981e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tkey_hash %d (%s)\n", 2991e51764aSArtem Bityutskiy (int)sup->key_hash, get_key_hash(sup->key_hash)); 3001e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tkey_fmt %d (%s)\n", 3011e51764aSArtem Bityutskiy (int)sup->key_fmt, get_key_fmt(sup->key_fmt)); 3021e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tflags %#x\n", sup_flags); 3031e51764aSArtem Bityutskiy printk(KERN_DEBUG "\t big_lpt %u\n", 3041e51764aSArtem Bityutskiy !!(sup_flags & UBIFS_FLG_BIGLPT)); 3051e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tmin_io_size %u\n", 3061e51764aSArtem Bityutskiy le32_to_cpu(sup->min_io_size)); 3071e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tleb_size %u\n", 3081e51764aSArtem Bityutskiy le32_to_cpu(sup->leb_size)); 3091e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tleb_cnt %u\n", 3101e51764aSArtem Bityutskiy le32_to_cpu(sup->leb_cnt)); 3111e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tmax_leb_cnt %u\n", 3121e51764aSArtem Bityutskiy le32_to_cpu(sup->max_leb_cnt)); 3131e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tmax_bud_bytes %llu\n", 3141e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(sup->max_bud_bytes)); 3151e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlog_lebs %u\n", 3161e51764aSArtem Bityutskiy le32_to_cpu(sup->log_lebs)); 3171e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlpt_lebs %u\n", 3181e51764aSArtem Bityutskiy le32_to_cpu(sup->lpt_lebs)); 3191e51764aSArtem Bityutskiy printk(KERN_DEBUG "\torph_lebs %u\n", 3201e51764aSArtem Bityutskiy le32_to_cpu(sup->orph_lebs)); 3211e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tjhead_cnt %u\n", 3221e51764aSArtem Bityutskiy le32_to_cpu(sup->jhead_cnt)); 3231e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tfanout %u\n", 3241e51764aSArtem Bityutskiy le32_to_cpu(sup->fanout)); 3251e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlsave_cnt %u\n", 3261e51764aSArtem Bityutskiy le32_to_cpu(sup->lsave_cnt)); 3271e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tdefault_compr %u\n", 3281e51764aSArtem Bityutskiy (int)le16_to_cpu(sup->default_compr)); 3291e51764aSArtem Bityutskiy printk(KERN_DEBUG "\trp_size %llu\n", 3301e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(sup->rp_size)); 3311e51764aSArtem Bityutskiy printk(KERN_DEBUG "\trp_uid %u\n", 3321e51764aSArtem Bityutskiy le32_to_cpu(sup->rp_uid)); 3331e51764aSArtem Bityutskiy printk(KERN_DEBUG "\trp_gid %u\n", 3341e51764aSArtem Bityutskiy le32_to_cpu(sup->rp_gid)); 3351e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tfmt_version %u\n", 3361e51764aSArtem Bityutskiy le32_to_cpu(sup->fmt_version)); 3371e51764aSArtem Bityutskiy printk(KERN_DEBUG "\ttime_gran %u\n", 3381e51764aSArtem Bityutskiy le32_to_cpu(sup->time_gran)); 3391e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tUUID %02X%02X%02X%02X-%02X%02X" 3401e51764aSArtem Bityutskiy "-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X\n", 3411e51764aSArtem Bityutskiy sup->uuid[0], sup->uuid[1], sup->uuid[2], sup->uuid[3], 3421e51764aSArtem Bityutskiy sup->uuid[4], sup->uuid[5], sup->uuid[6], sup->uuid[7], 3431e51764aSArtem Bityutskiy sup->uuid[8], sup->uuid[9], sup->uuid[10], sup->uuid[11], 3441e51764aSArtem Bityutskiy sup->uuid[12], sup->uuid[13], sup->uuid[14], 3451e51764aSArtem Bityutskiy sup->uuid[15]); 3461e51764aSArtem Bityutskiy break; 3471e51764aSArtem Bityutskiy } 3481e51764aSArtem Bityutskiy case UBIFS_MST_NODE: 3491e51764aSArtem Bityutskiy { 3501e51764aSArtem Bityutskiy const struct ubifs_mst_node *mst = node; 3511e51764aSArtem Bityutskiy 3521e51764aSArtem Bityutskiy printk(KERN_DEBUG "\thighest_inum %llu\n", 3531e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->highest_inum)); 3541e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tcommit number %llu\n", 3551e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->cmt_no)); 3561e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tflags %#x\n", 3571e51764aSArtem Bityutskiy le32_to_cpu(mst->flags)); 3581e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlog_lnum %u\n", 3591e51764aSArtem Bityutskiy le32_to_cpu(mst->log_lnum)); 3601e51764aSArtem Bityutskiy printk(KERN_DEBUG "\troot_lnum %u\n", 3611e51764aSArtem Bityutskiy le32_to_cpu(mst->root_lnum)); 3621e51764aSArtem Bityutskiy printk(KERN_DEBUG "\troot_offs %u\n", 3631e51764aSArtem Bityutskiy le32_to_cpu(mst->root_offs)); 3641e51764aSArtem Bityutskiy printk(KERN_DEBUG "\troot_len %u\n", 3651e51764aSArtem Bityutskiy le32_to_cpu(mst->root_len)); 3661e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tgc_lnum %u\n", 3671e51764aSArtem Bityutskiy le32_to_cpu(mst->gc_lnum)); 3681e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tihead_lnum %u\n", 3691e51764aSArtem Bityutskiy le32_to_cpu(mst->ihead_lnum)); 3701e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tihead_offs %u\n", 3711e51764aSArtem Bityutskiy le32_to_cpu(mst->ihead_offs)); 3720ecb9529SHarvey Harrison printk(KERN_DEBUG "\tindex_size %llu\n", 3730ecb9529SHarvey Harrison (unsigned long long)le64_to_cpu(mst->index_size)); 3741e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlpt_lnum %u\n", 3751e51764aSArtem Bityutskiy le32_to_cpu(mst->lpt_lnum)); 3761e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlpt_offs %u\n", 3771e51764aSArtem Bityutskiy le32_to_cpu(mst->lpt_offs)); 3781e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tnhead_lnum %u\n", 3791e51764aSArtem Bityutskiy le32_to_cpu(mst->nhead_lnum)); 3801e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tnhead_offs %u\n", 3811e51764aSArtem Bityutskiy le32_to_cpu(mst->nhead_offs)); 3821e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tltab_lnum %u\n", 3831e51764aSArtem Bityutskiy le32_to_cpu(mst->ltab_lnum)); 3841e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tltab_offs %u\n", 3851e51764aSArtem Bityutskiy le32_to_cpu(mst->ltab_offs)); 3861e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlsave_lnum %u\n", 3871e51764aSArtem Bityutskiy le32_to_cpu(mst->lsave_lnum)); 3881e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlsave_offs %u\n", 3891e51764aSArtem Bityutskiy le32_to_cpu(mst->lsave_offs)); 3901e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlscan_lnum %u\n", 3911e51764aSArtem Bityutskiy le32_to_cpu(mst->lscan_lnum)); 3921e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tleb_cnt %u\n", 3931e51764aSArtem Bityutskiy le32_to_cpu(mst->leb_cnt)); 3941e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tempty_lebs %u\n", 3951e51764aSArtem Bityutskiy le32_to_cpu(mst->empty_lebs)); 3961e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tidx_lebs %u\n", 3971e51764aSArtem Bityutskiy le32_to_cpu(mst->idx_lebs)); 3981e51764aSArtem Bityutskiy printk(KERN_DEBUG "\ttotal_free %llu\n", 3991e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->total_free)); 4001e51764aSArtem Bityutskiy printk(KERN_DEBUG "\ttotal_dirty %llu\n", 4011e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->total_dirty)); 4021e51764aSArtem Bityutskiy printk(KERN_DEBUG "\ttotal_used %llu\n", 4031e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->total_used)); 4041e51764aSArtem Bityutskiy printk(KERN_DEBUG "\ttotal_dead %llu\n", 4051e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->total_dead)); 4061e51764aSArtem Bityutskiy printk(KERN_DEBUG "\ttotal_dark %llu\n", 4071e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->total_dark)); 4081e51764aSArtem Bityutskiy break; 4091e51764aSArtem Bityutskiy } 4101e51764aSArtem Bityutskiy case UBIFS_REF_NODE: 4111e51764aSArtem Bityutskiy { 4121e51764aSArtem Bityutskiy const struct ubifs_ref_node *ref = node; 4131e51764aSArtem Bityutskiy 4141e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlnum %u\n", 4151e51764aSArtem Bityutskiy le32_to_cpu(ref->lnum)); 4161e51764aSArtem Bityutskiy printk(KERN_DEBUG "\toffs %u\n", 4171e51764aSArtem Bityutskiy le32_to_cpu(ref->offs)); 4181e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tjhead %u\n", 4191e51764aSArtem Bityutskiy le32_to_cpu(ref->jhead)); 4201e51764aSArtem Bityutskiy break; 4211e51764aSArtem Bityutskiy } 4221e51764aSArtem Bityutskiy case UBIFS_INO_NODE: 4231e51764aSArtem Bityutskiy { 4241e51764aSArtem Bityutskiy const struct ubifs_ino_node *ino = node; 4251e51764aSArtem Bityutskiy 4261e51764aSArtem Bityutskiy key_read(c, &ino->key, &key); 4271e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key)); 4281e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tcreat_sqnum %llu\n", 4291e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(ino->creat_sqnum)); 4301e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tsize %llu\n", 4311e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(ino->size)); 4321e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tnlink %u\n", 4331e51764aSArtem Bityutskiy le32_to_cpu(ino->nlink)); 4341e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tatime %lld.%u\n", 4351e51764aSArtem Bityutskiy (long long)le64_to_cpu(ino->atime_sec), 4361e51764aSArtem Bityutskiy le32_to_cpu(ino->atime_nsec)); 4371e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tmtime %lld.%u\n", 4381e51764aSArtem Bityutskiy (long long)le64_to_cpu(ino->mtime_sec), 4391e51764aSArtem Bityutskiy le32_to_cpu(ino->mtime_nsec)); 4401e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tctime %lld.%u\n", 4411e51764aSArtem Bityutskiy (long long)le64_to_cpu(ino->ctime_sec), 4421e51764aSArtem Bityutskiy le32_to_cpu(ino->ctime_nsec)); 4431e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tuid %u\n", 4441e51764aSArtem Bityutskiy le32_to_cpu(ino->uid)); 4451e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tgid %u\n", 4461e51764aSArtem Bityutskiy le32_to_cpu(ino->gid)); 4471e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tmode %u\n", 4481e51764aSArtem Bityutskiy le32_to_cpu(ino->mode)); 4491e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tflags %#x\n", 4501e51764aSArtem Bityutskiy le32_to_cpu(ino->flags)); 4511e51764aSArtem Bityutskiy printk(KERN_DEBUG "\txattr_cnt %u\n", 4521e51764aSArtem Bityutskiy le32_to_cpu(ino->xattr_cnt)); 4531e51764aSArtem Bityutskiy printk(KERN_DEBUG "\txattr_size %u\n", 4541e51764aSArtem Bityutskiy le32_to_cpu(ino->xattr_size)); 4551e51764aSArtem Bityutskiy printk(KERN_DEBUG "\txattr_names %u\n", 4561e51764aSArtem Bityutskiy le32_to_cpu(ino->xattr_names)); 4571e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tcompr_type %#x\n", 4581e51764aSArtem Bityutskiy (int)le16_to_cpu(ino->compr_type)); 4591e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tdata len %u\n", 4601e51764aSArtem Bityutskiy le32_to_cpu(ino->data_len)); 4611e51764aSArtem Bityutskiy break; 4621e51764aSArtem Bityutskiy } 4631e51764aSArtem Bityutskiy case UBIFS_DENT_NODE: 4641e51764aSArtem Bityutskiy case UBIFS_XENT_NODE: 4651e51764aSArtem Bityutskiy { 4661e51764aSArtem Bityutskiy const struct ubifs_dent_node *dent = node; 4671e51764aSArtem Bityutskiy int nlen = le16_to_cpu(dent->nlen); 4681e51764aSArtem Bityutskiy 4691e51764aSArtem Bityutskiy key_read(c, &dent->key, &key); 4701e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key)); 4711e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tinum %llu\n", 4721e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(dent->inum)); 4731e51764aSArtem Bityutskiy printk(KERN_DEBUG "\ttype %d\n", (int)dent->type); 4741e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tnlen %d\n", nlen); 4751e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tname "); 4761e51764aSArtem Bityutskiy 4771e51764aSArtem Bityutskiy if (nlen > UBIFS_MAX_NLEN) 4781e51764aSArtem Bityutskiy printk(KERN_DEBUG "(bad name length, not printing, " 4791e51764aSArtem Bityutskiy "bad or corrupted node)"); 4801e51764aSArtem Bityutskiy else { 4811e51764aSArtem Bityutskiy for (i = 0; i < nlen && dent->name[i]; i++) 4821e51764aSArtem Bityutskiy printk("%c", dent->name[i]); 4831e51764aSArtem Bityutskiy } 4841e51764aSArtem Bityutskiy printk("\n"); 4851e51764aSArtem Bityutskiy 4861e51764aSArtem Bityutskiy break; 4871e51764aSArtem Bityutskiy } 4881e51764aSArtem Bityutskiy case UBIFS_DATA_NODE: 4891e51764aSArtem Bityutskiy { 4901e51764aSArtem Bityutskiy const struct ubifs_data_node *dn = node; 4911e51764aSArtem Bityutskiy int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ; 4921e51764aSArtem Bityutskiy 4931e51764aSArtem Bityutskiy key_read(c, &dn->key, &key); 4941e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key)); 4951e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tsize %u\n", 4961e51764aSArtem Bityutskiy le32_to_cpu(dn->size)); 4971e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tcompr_typ %d\n", 4981e51764aSArtem Bityutskiy (int)le16_to_cpu(dn->compr_type)); 4991e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tdata size %d\n", 5001e51764aSArtem Bityutskiy dlen); 5011e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tdata:\n"); 5021e51764aSArtem Bityutskiy print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET, 32, 1, 5031e51764aSArtem Bityutskiy (void *)&dn->data, dlen, 0); 5041e51764aSArtem Bityutskiy break; 5051e51764aSArtem Bityutskiy } 5061e51764aSArtem Bityutskiy case UBIFS_TRUN_NODE: 5071e51764aSArtem Bityutskiy { 5081e51764aSArtem Bityutskiy const struct ubifs_trun_node *trun = node; 5091e51764aSArtem Bityutskiy 5101e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tinum %u\n", 5111e51764aSArtem Bityutskiy le32_to_cpu(trun->inum)); 5121e51764aSArtem Bityutskiy printk(KERN_DEBUG "\told_size %llu\n", 5131e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(trun->old_size)); 5141e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tnew_size %llu\n", 5151e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(trun->new_size)); 5161e51764aSArtem Bityutskiy break; 5171e51764aSArtem Bityutskiy } 5181e51764aSArtem Bityutskiy case UBIFS_IDX_NODE: 5191e51764aSArtem Bityutskiy { 5201e51764aSArtem Bityutskiy const struct ubifs_idx_node *idx = node; 5211e51764aSArtem Bityutskiy 5221e51764aSArtem Bityutskiy n = le16_to_cpu(idx->child_cnt); 5231e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tchild_cnt %d\n", n); 5241e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlevel %d\n", 5251e51764aSArtem Bityutskiy (int)le16_to_cpu(idx->level)); 5261e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tBranches:\n"); 5271e51764aSArtem Bityutskiy 5281e51764aSArtem Bityutskiy for (i = 0; i < n && i < c->fanout - 1; i++) { 5291e51764aSArtem Bityutskiy const struct ubifs_branch *br; 5301e51764aSArtem Bityutskiy 5311e51764aSArtem Bityutskiy br = ubifs_idx_branch(c, idx, i); 5321e51764aSArtem Bityutskiy key_read(c, &br->key, &key); 5331e51764aSArtem Bityutskiy printk(KERN_DEBUG "\t%d: LEB %d:%d len %d key %s\n", 5341e51764aSArtem Bityutskiy i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs), 5351e51764aSArtem Bityutskiy le32_to_cpu(br->len), DBGKEY(&key)); 5361e51764aSArtem Bityutskiy } 5371e51764aSArtem Bityutskiy break; 5381e51764aSArtem Bityutskiy } 5391e51764aSArtem Bityutskiy case UBIFS_CS_NODE: 5401e51764aSArtem Bityutskiy break; 5411e51764aSArtem Bityutskiy case UBIFS_ORPH_NODE: 5421e51764aSArtem Bityutskiy { 5431e51764aSArtem Bityutskiy const struct ubifs_orph_node *orph = node; 5441e51764aSArtem Bityutskiy 5451e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tcommit number %llu\n", 5461e51764aSArtem Bityutskiy (unsigned long long) 5471e51764aSArtem Bityutskiy le64_to_cpu(orph->cmt_no) & LLONG_MAX); 5481e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlast node flag %llu\n", 5491e51764aSArtem Bityutskiy (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63); 5501e51764aSArtem Bityutskiy n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3; 5511e51764aSArtem Bityutskiy printk(KERN_DEBUG "\t%d orphan inode numbers:\n", n); 5521e51764aSArtem Bityutskiy for (i = 0; i < n; i++) 5531e51764aSArtem Bityutskiy printk(KERN_DEBUG "\t ino %llu\n", 5547424bac8SAlexander Beregalov (unsigned long long)le64_to_cpu(orph->inos[i])); 5551e51764aSArtem Bityutskiy break; 5561e51764aSArtem Bityutskiy } 5571e51764aSArtem Bityutskiy default: 5581e51764aSArtem Bityutskiy printk(KERN_DEBUG "node type %d was not recognized\n", 5591e51764aSArtem Bityutskiy (int)ch->node_type); 5601e51764aSArtem Bityutskiy } 5611e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 5621e51764aSArtem Bityutskiy } 5631e51764aSArtem Bityutskiy 5641e51764aSArtem Bityutskiy void dbg_dump_budget_req(const struct ubifs_budget_req *req) 5651e51764aSArtem Bityutskiy { 5661e51764aSArtem Bityutskiy spin_lock(&dbg_lock); 5671e51764aSArtem Bityutskiy printk(KERN_DEBUG "Budgeting request: new_ino %d, dirtied_ino %d\n", 5681e51764aSArtem Bityutskiy req->new_ino, req->dirtied_ino); 5691e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tnew_ino_d %d, dirtied_ino_d %d\n", 5701e51764aSArtem Bityutskiy req->new_ino_d, req->dirtied_ino_d); 5711e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tnew_page %d, dirtied_page %d\n", 5721e51764aSArtem Bityutskiy req->new_page, req->dirtied_page); 5731e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tnew_dent %d, mod_dent %d\n", 5741e51764aSArtem Bityutskiy req->new_dent, req->mod_dent); 5751e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tidx_growth %d\n", req->idx_growth); 5761e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tdata_growth %d dd_growth %d\n", 5771e51764aSArtem Bityutskiy req->data_growth, req->dd_growth); 5781e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 5791e51764aSArtem Bityutskiy } 5801e51764aSArtem Bityutskiy 5811e51764aSArtem Bityutskiy void dbg_dump_lstats(const struct ubifs_lp_stats *lst) 5821e51764aSArtem Bityutskiy { 5831e51764aSArtem Bityutskiy spin_lock(&dbg_lock); 5841de94159SArtem Bityutskiy printk(KERN_DEBUG "(pid %d) Lprops statistics: empty_lebs %d, " 5851de94159SArtem Bityutskiy "idx_lebs %d\n", current->pid, lst->empty_lebs, lst->idx_lebs); 5861e51764aSArtem Bityutskiy printk(KERN_DEBUG "\ttaken_empty_lebs %d, total_free %lld, " 5871e51764aSArtem Bityutskiy "total_dirty %lld\n", lst->taken_empty_lebs, lst->total_free, 5881e51764aSArtem Bityutskiy lst->total_dirty); 5891e51764aSArtem Bityutskiy printk(KERN_DEBUG "\ttotal_used %lld, total_dark %lld, " 5901e51764aSArtem Bityutskiy "total_dead %lld\n", lst->total_used, lst->total_dark, 5911e51764aSArtem Bityutskiy lst->total_dead); 5921e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 5931e51764aSArtem Bityutskiy } 5941e51764aSArtem Bityutskiy 5951e51764aSArtem Bityutskiy void dbg_dump_budg(struct ubifs_info *c) 5961e51764aSArtem Bityutskiy { 5971e51764aSArtem Bityutskiy int i; 5981e51764aSArtem Bityutskiy struct rb_node *rb; 5991e51764aSArtem Bityutskiy struct ubifs_bud *bud; 6001e51764aSArtem Bityutskiy struct ubifs_gced_idx_leb *idx_gc; 60121a60258SArtem Bityutskiy long long available, outstanding, free; 6021e51764aSArtem Bityutskiy 60321a60258SArtem Bityutskiy ubifs_assert(spin_is_locked(&c->space_lock)); 6041e51764aSArtem Bityutskiy spin_lock(&dbg_lock); 6051de94159SArtem Bityutskiy printk(KERN_DEBUG "(pid %d) Budgeting info: budg_data_growth %lld, " 6061de94159SArtem Bityutskiy "budg_dd_growth %lld, budg_idx_growth %lld\n", current->pid, 6071e51764aSArtem Bityutskiy c->budg_data_growth, c->budg_dd_growth, c->budg_idx_growth); 6081e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tdata budget sum %lld, total budget sum %lld, " 6091e51764aSArtem Bityutskiy "freeable_cnt %d\n", c->budg_data_growth + c->budg_dd_growth, 6101e51764aSArtem Bityutskiy c->budg_data_growth + c->budg_dd_growth + c->budg_idx_growth, 6111e51764aSArtem Bityutskiy c->freeable_cnt); 6121e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tmin_idx_lebs %d, old_idx_sz %lld, " 6131e51764aSArtem Bityutskiy "calc_idx_sz %lld, idx_gc_cnt %d\n", c->min_idx_lebs, 6141e51764aSArtem Bityutskiy c->old_idx_sz, c->calc_idx_sz, c->idx_gc_cnt); 6151e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, " 6161e51764aSArtem Bityutskiy "clean_zn_cnt %ld\n", atomic_long_read(&c->dirty_pg_cnt), 6171e51764aSArtem Bityutskiy atomic_long_read(&c->dirty_zn_cnt), 6181e51764aSArtem Bityutskiy atomic_long_read(&c->clean_zn_cnt)); 6191e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n", 6201e51764aSArtem Bityutskiy c->dark_wm, c->dead_wm, c->max_idx_node_sz); 6211e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tgc_lnum %d, ihead_lnum %d\n", 6221e51764aSArtem Bityutskiy c->gc_lnum, c->ihead_lnum); 6231e51764aSArtem Bityutskiy for (i = 0; i < c->jhead_cnt; i++) 6241e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tjhead %d\t LEB %d\n", 6251e51764aSArtem Bityutskiy c->jheads[i].wbuf.jhead, c->jheads[i].wbuf.lnum); 6261e51764aSArtem Bityutskiy for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) { 6271e51764aSArtem Bityutskiy bud = rb_entry(rb, struct ubifs_bud, rb); 6281e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tbud LEB %d\n", bud->lnum); 6291e51764aSArtem Bityutskiy } 6301e51764aSArtem Bityutskiy list_for_each_entry(bud, &c->old_buds, list) 6311e51764aSArtem Bityutskiy printk(KERN_DEBUG "\told bud LEB %d\n", bud->lnum); 6321e51764aSArtem Bityutskiy list_for_each_entry(idx_gc, &c->idx_gc, list) 6331e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tGC'ed idx LEB %d unmap %d\n", 6341e51764aSArtem Bityutskiy idx_gc->lnum, idx_gc->unmap); 6351e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tcommit state %d\n", c->cmt_state); 63621a60258SArtem Bityutskiy 63721a60258SArtem Bityutskiy /* Print budgeting predictions */ 63821a60258SArtem Bityutskiy available = ubifs_calc_available(c, c->min_idx_lebs); 63921a60258SArtem Bityutskiy outstanding = c->budg_data_growth + c->budg_dd_growth; 64021a60258SArtem Bityutskiy if (available > outstanding) 64121a60258SArtem Bityutskiy free = ubifs_reported_space(c, available - outstanding); 64221a60258SArtem Bityutskiy else 64321a60258SArtem Bityutskiy free = 0; 64421a60258SArtem Bityutskiy printk(KERN_DEBUG "Budgeting predictions:\n"); 64521a60258SArtem Bityutskiy printk(KERN_DEBUG "\tavailable: %lld, outstanding %lld, free %lld\n", 64621a60258SArtem Bityutskiy available, outstanding, free); 6471e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 6481e51764aSArtem Bityutskiy } 6491e51764aSArtem Bityutskiy 6501e51764aSArtem Bityutskiy void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp) 6511e51764aSArtem Bityutskiy { 6521e51764aSArtem Bityutskiy printk(KERN_DEBUG "LEB %d lprops: free %d, dirty %d (used %d), " 6531e51764aSArtem Bityutskiy "flags %#x\n", lp->lnum, lp->free, lp->dirty, 6541e51764aSArtem Bityutskiy c->leb_size - lp->free - lp->dirty, lp->flags); 6551e51764aSArtem Bityutskiy } 6561e51764aSArtem Bityutskiy 6571e51764aSArtem Bityutskiy void dbg_dump_lprops(struct ubifs_info *c) 6581e51764aSArtem Bityutskiy { 6591e51764aSArtem Bityutskiy int lnum, err; 6601e51764aSArtem Bityutskiy struct ubifs_lprops lp; 6611e51764aSArtem Bityutskiy struct ubifs_lp_stats lst; 6621e51764aSArtem Bityutskiy 6632ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) start dumping LEB properties\n", 6642ba5f7aeSArtem Bityutskiy current->pid); 6651e51764aSArtem Bityutskiy ubifs_get_lp_stats(c, &lst); 6661e51764aSArtem Bityutskiy dbg_dump_lstats(&lst); 6671e51764aSArtem Bityutskiy 6681e51764aSArtem Bityutskiy for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) { 6691e51764aSArtem Bityutskiy err = ubifs_read_one_lp(c, lnum, &lp); 6701e51764aSArtem Bityutskiy if (err) 6711e51764aSArtem Bityutskiy ubifs_err("cannot read lprops for LEB %d", lnum); 6721e51764aSArtem Bityutskiy 6731e51764aSArtem Bityutskiy dbg_dump_lprop(c, &lp); 6741e51764aSArtem Bityutskiy } 6752ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) finish dumping LEB properties\n", 6762ba5f7aeSArtem Bityutskiy current->pid); 6771e51764aSArtem Bityutskiy } 6781e51764aSArtem Bityutskiy 67973944a6dSAdrian Hunter void dbg_dump_lpt_info(struct ubifs_info *c) 68073944a6dSAdrian Hunter { 68173944a6dSAdrian Hunter int i; 68273944a6dSAdrian Hunter 68373944a6dSAdrian Hunter spin_lock(&dbg_lock); 6842ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) dumping LPT information\n", current->pid); 68573944a6dSAdrian Hunter printk(KERN_DEBUG "\tlpt_sz: %lld\n", c->lpt_sz); 68673944a6dSAdrian Hunter printk(KERN_DEBUG "\tpnode_sz: %d\n", c->pnode_sz); 68773944a6dSAdrian Hunter printk(KERN_DEBUG "\tnnode_sz: %d\n", c->nnode_sz); 68873944a6dSAdrian Hunter printk(KERN_DEBUG "\tltab_sz: %d\n", c->ltab_sz); 68973944a6dSAdrian Hunter printk(KERN_DEBUG "\tlsave_sz: %d\n", c->lsave_sz); 69073944a6dSAdrian Hunter printk(KERN_DEBUG "\tbig_lpt: %d\n", c->big_lpt); 69173944a6dSAdrian Hunter printk(KERN_DEBUG "\tlpt_hght: %d\n", c->lpt_hght); 69273944a6dSAdrian Hunter printk(KERN_DEBUG "\tpnode_cnt: %d\n", c->pnode_cnt); 69373944a6dSAdrian Hunter printk(KERN_DEBUG "\tnnode_cnt: %d\n", c->nnode_cnt); 69473944a6dSAdrian Hunter printk(KERN_DEBUG "\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt); 69573944a6dSAdrian Hunter printk(KERN_DEBUG "\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt); 69673944a6dSAdrian Hunter printk(KERN_DEBUG "\tlsave_cnt: %d\n", c->lsave_cnt); 69773944a6dSAdrian Hunter printk(KERN_DEBUG "\tspace_bits: %d\n", c->space_bits); 69873944a6dSAdrian Hunter printk(KERN_DEBUG "\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits); 69973944a6dSAdrian Hunter printk(KERN_DEBUG "\tlpt_offs_bits: %d\n", c->lpt_offs_bits); 70073944a6dSAdrian Hunter printk(KERN_DEBUG "\tlpt_spc_bits: %d\n", c->lpt_spc_bits); 70173944a6dSAdrian Hunter printk(KERN_DEBUG "\tpcnt_bits: %d\n", c->pcnt_bits); 70273944a6dSAdrian Hunter printk(KERN_DEBUG "\tlnum_bits: %d\n", c->lnum_bits); 70373944a6dSAdrian Hunter printk(KERN_DEBUG "\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs); 70473944a6dSAdrian Hunter printk(KERN_DEBUG "\tLPT head is at %d:%d\n", 70573944a6dSAdrian Hunter c->nhead_lnum, c->nhead_offs); 70673944a6dSAdrian Hunter printk(KERN_DEBUG "\tLPT ltab is at %d:%d\n", c->ltab_lnum, c->ltab_offs); 70773944a6dSAdrian Hunter if (c->big_lpt) 70873944a6dSAdrian Hunter printk(KERN_DEBUG "\tLPT lsave is at %d:%d\n", 70973944a6dSAdrian Hunter c->lsave_lnum, c->lsave_offs); 71073944a6dSAdrian Hunter for (i = 0; i < c->lpt_lebs; i++) 71173944a6dSAdrian Hunter printk(KERN_DEBUG "\tLPT LEB %d free %d dirty %d tgc %d " 71273944a6dSAdrian Hunter "cmt %d\n", i + c->lpt_first, c->ltab[i].free, 71373944a6dSAdrian Hunter c->ltab[i].dirty, c->ltab[i].tgc, c->ltab[i].cmt); 71473944a6dSAdrian Hunter spin_unlock(&dbg_lock); 71573944a6dSAdrian Hunter } 71673944a6dSAdrian Hunter 7171e51764aSArtem Bityutskiy void dbg_dump_leb(const struct ubifs_info *c, int lnum) 7181e51764aSArtem Bityutskiy { 7191e51764aSArtem Bityutskiy struct ubifs_scan_leb *sleb; 7201e51764aSArtem Bityutskiy struct ubifs_scan_node *snod; 7211e51764aSArtem Bityutskiy 7221e51764aSArtem Bityutskiy if (dbg_failure_mode) 7231e51764aSArtem Bityutskiy return; 7241e51764aSArtem Bityutskiy 7252ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n", 7262ba5f7aeSArtem Bityutskiy current->pid, lnum); 72717c2f9f8SArtem Bityutskiy sleb = ubifs_scan(c, lnum, 0, c->dbg->buf); 7281e51764aSArtem Bityutskiy if (IS_ERR(sleb)) { 7291e51764aSArtem Bityutskiy ubifs_err("scan error %d", (int)PTR_ERR(sleb)); 7301e51764aSArtem Bityutskiy return; 7311e51764aSArtem Bityutskiy } 7321e51764aSArtem Bityutskiy 7331e51764aSArtem Bityutskiy printk(KERN_DEBUG "LEB %d has %d nodes ending at %d\n", lnum, 7341e51764aSArtem Bityutskiy sleb->nodes_cnt, sleb->endpt); 7351e51764aSArtem Bityutskiy 7361e51764aSArtem Bityutskiy list_for_each_entry(snod, &sleb->nodes, list) { 7371e51764aSArtem Bityutskiy cond_resched(); 7381e51764aSArtem Bityutskiy printk(KERN_DEBUG "Dumping node at LEB %d:%d len %d\n", lnum, 7391e51764aSArtem Bityutskiy snod->offs, snod->len); 7401e51764aSArtem Bityutskiy dbg_dump_node(c, snod->node); 7411e51764aSArtem Bityutskiy } 7421e51764aSArtem Bityutskiy 7432ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n", 7442ba5f7aeSArtem Bityutskiy current->pid, lnum); 7451e51764aSArtem Bityutskiy ubifs_scan_destroy(sleb); 7461e51764aSArtem Bityutskiy return; 7471e51764aSArtem Bityutskiy } 7481e51764aSArtem Bityutskiy 7491e51764aSArtem Bityutskiy void dbg_dump_znode(const struct ubifs_info *c, 7501e51764aSArtem Bityutskiy const struct ubifs_znode *znode) 7511e51764aSArtem Bityutskiy { 7521e51764aSArtem Bityutskiy int n; 7531e51764aSArtem Bityutskiy const struct ubifs_zbranch *zbr; 7541e51764aSArtem Bityutskiy 7551e51764aSArtem Bityutskiy spin_lock(&dbg_lock); 7561e51764aSArtem Bityutskiy if (znode->parent) 7571e51764aSArtem Bityutskiy zbr = &znode->parent->zbranch[znode->iip]; 7581e51764aSArtem Bityutskiy else 7591e51764aSArtem Bityutskiy zbr = &c->zroot; 7601e51764aSArtem Bityutskiy 7611e51764aSArtem Bityutskiy printk(KERN_DEBUG "znode %p, LEB %d:%d len %d parent %p iip %d level %d" 7621e51764aSArtem Bityutskiy " child_cnt %d flags %lx\n", znode, zbr->lnum, zbr->offs, 7631e51764aSArtem Bityutskiy zbr->len, znode->parent, znode->iip, znode->level, 7641e51764aSArtem Bityutskiy znode->child_cnt, znode->flags); 7651e51764aSArtem Bityutskiy 7661e51764aSArtem Bityutskiy if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) { 7671e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 7681e51764aSArtem Bityutskiy return; 7691e51764aSArtem Bityutskiy } 7701e51764aSArtem Bityutskiy 7711e51764aSArtem Bityutskiy printk(KERN_DEBUG "zbranches:\n"); 7721e51764aSArtem Bityutskiy for (n = 0; n < znode->child_cnt; n++) { 7731e51764aSArtem Bityutskiy zbr = &znode->zbranch[n]; 7741e51764aSArtem Bityutskiy if (znode->level > 0) 7751e51764aSArtem Bityutskiy printk(KERN_DEBUG "\t%d: znode %p LEB %d:%d len %d key " 7761e51764aSArtem Bityutskiy "%s\n", n, zbr->znode, zbr->lnum, 7771e51764aSArtem Bityutskiy zbr->offs, zbr->len, 7781e51764aSArtem Bityutskiy DBGKEY(&zbr->key)); 7791e51764aSArtem Bityutskiy else 7801e51764aSArtem Bityutskiy printk(KERN_DEBUG "\t%d: LNC %p LEB %d:%d len %d key " 7811e51764aSArtem Bityutskiy "%s\n", n, zbr->znode, zbr->lnum, 7821e51764aSArtem Bityutskiy zbr->offs, zbr->len, 7831e51764aSArtem Bityutskiy DBGKEY(&zbr->key)); 7841e51764aSArtem Bityutskiy } 7851e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 7861e51764aSArtem Bityutskiy } 7871e51764aSArtem Bityutskiy 7881e51764aSArtem Bityutskiy void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat) 7891e51764aSArtem Bityutskiy { 7901e51764aSArtem Bityutskiy int i; 7911e51764aSArtem Bityutskiy 7922ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) start dumping heap cat %d (%d elements)\n", 7931de94159SArtem Bityutskiy current->pid, cat, heap->cnt); 7941e51764aSArtem Bityutskiy for (i = 0; i < heap->cnt; i++) { 7951e51764aSArtem Bityutskiy struct ubifs_lprops *lprops = heap->arr[i]; 7961e51764aSArtem Bityutskiy 7971e51764aSArtem Bityutskiy printk(KERN_DEBUG "\t%d. LEB %d hpos %d free %d dirty %d " 7981e51764aSArtem Bityutskiy "flags %d\n", i, lprops->lnum, lprops->hpos, 7991e51764aSArtem Bityutskiy lprops->free, lprops->dirty, lprops->flags); 8001e51764aSArtem Bityutskiy } 8012ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) finish dumping heap\n", current->pid); 8021e51764aSArtem Bityutskiy } 8031e51764aSArtem Bityutskiy 8041e51764aSArtem Bityutskiy void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode, 8051e51764aSArtem Bityutskiy struct ubifs_nnode *parent, int iip) 8061e51764aSArtem Bityutskiy { 8071e51764aSArtem Bityutskiy int i; 8081e51764aSArtem Bityutskiy 8092ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) dumping pnode:\n", current->pid); 8101e51764aSArtem Bityutskiy printk(KERN_DEBUG "\taddress %zx parent %zx cnext %zx\n", 8111e51764aSArtem Bityutskiy (size_t)pnode, (size_t)parent, (size_t)pnode->cnext); 8121e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tflags %lu iip %d level %d num %d\n", 8131e51764aSArtem Bityutskiy pnode->flags, iip, pnode->level, pnode->num); 8141e51764aSArtem Bityutskiy for (i = 0; i < UBIFS_LPT_FANOUT; i++) { 8151e51764aSArtem Bityutskiy struct ubifs_lprops *lp = &pnode->lprops[i]; 8161e51764aSArtem Bityutskiy 8171e51764aSArtem Bityutskiy printk(KERN_DEBUG "\t%d: free %d dirty %d flags %d lnum %d\n", 8181e51764aSArtem Bityutskiy i, lp->free, lp->dirty, lp->flags, lp->lnum); 8191e51764aSArtem Bityutskiy } 8201e51764aSArtem Bityutskiy } 8211e51764aSArtem Bityutskiy 8221e51764aSArtem Bityutskiy void dbg_dump_tnc(struct ubifs_info *c) 8231e51764aSArtem Bityutskiy { 8241e51764aSArtem Bityutskiy struct ubifs_znode *znode; 8251e51764aSArtem Bityutskiy int level; 8261e51764aSArtem Bityutskiy 8271e51764aSArtem Bityutskiy printk(KERN_DEBUG "\n"); 8282ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) start dumping TNC tree\n", current->pid); 8291e51764aSArtem Bityutskiy znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL); 8301e51764aSArtem Bityutskiy level = znode->level; 8311e51764aSArtem Bityutskiy printk(KERN_DEBUG "== Level %d ==\n", level); 8321e51764aSArtem Bityutskiy while (znode) { 8331e51764aSArtem Bityutskiy if (level != znode->level) { 8341e51764aSArtem Bityutskiy level = znode->level; 8351e51764aSArtem Bityutskiy printk(KERN_DEBUG "== Level %d ==\n", level); 8361e51764aSArtem Bityutskiy } 8371e51764aSArtem Bityutskiy dbg_dump_znode(c, znode); 8381e51764aSArtem Bityutskiy znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode); 8391e51764aSArtem Bityutskiy } 8402ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) finish dumping TNC tree\n", current->pid); 8411e51764aSArtem Bityutskiy } 8421e51764aSArtem Bityutskiy 8431e51764aSArtem Bityutskiy static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode, 8441e51764aSArtem Bityutskiy void *priv) 8451e51764aSArtem Bityutskiy { 8461e51764aSArtem Bityutskiy dbg_dump_znode(c, znode); 8471e51764aSArtem Bityutskiy return 0; 8481e51764aSArtem Bityutskiy } 8491e51764aSArtem Bityutskiy 8501e51764aSArtem Bityutskiy /** 8511e51764aSArtem Bityutskiy * dbg_dump_index - dump the on-flash index. 8521e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 8531e51764aSArtem Bityutskiy * 8541e51764aSArtem Bityutskiy * This function dumps whole UBIFS indexing B-tree, unlike 'dbg_dump_tnc()' 8551e51764aSArtem Bityutskiy * which dumps only in-memory znodes and does not read znodes which from flash. 8561e51764aSArtem Bityutskiy */ 8571e51764aSArtem Bityutskiy void dbg_dump_index(struct ubifs_info *c) 8581e51764aSArtem Bityutskiy { 8591e51764aSArtem Bityutskiy dbg_walk_index(c, NULL, dump_znode, NULL); 8601e51764aSArtem Bityutskiy } 8611e51764aSArtem Bityutskiy 8621e51764aSArtem Bityutskiy /** 8631e51764aSArtem Bityutskiy * dbg_check_synced_i_size - check synchronized inode size. 8641e51764aSArtem Bityutskiy * @inode: inode to check 8651e51764aSArtem Bityutskiy * 8661e51764aSArtem Bityutskiy * If inode is clean, synchronized inode size has to be equivalent to current 8671e51764aSArtem Bityutskiy * inode size. This function has to be called only for locked inodes (@i_mutex 8681e51764aSArtem Bityutskiy * has to be locked). Returns %0 if synchronized inode size if correct, and 8691e51764aSArtem Bityutskiy * %-EINVAL if not. 8701e51764aSArtem Bityutskiy */ 8711e51764aSArtem Bityutskiy int dbg_check_synced_i_size(struct inode *inode) 8721e51764aSArtem Bityutskiy { 8731e51764aSArtem Bityutskiy int err = 0; 8741e51764aSArtem Bityutskiy struct ubifs_inode *ui = ubifs_inode(inode); 8751e51764aSArtem Bityutskiy 8761e51764aSArtem Bityutskiy if (!(ubifs_chk_flags & UBIFS_CHK_GEN)) 8771e51764aSArtem Bityutskiy return 0; 8781e51764aSArtem Bityutskiy if (!S_ISREG(inode->i_mode)) 8791e51764aSArtem Bityutskiy return 0; 8801e51764aSArtem Bityutskiy 8811e51764aSArtem Bityutskiy mutex_lock(&ui->ui_mutex); 8821e51764aSArtem Bityutskiy spin_lock(&ui->ui_lock); 8831e51764aSArtem Bityutskiy if (ui->ui_size != ui->synced_i_size && !ui->dirty) { 8841e51764aSArtem Bityutskiy ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode " 8851e51764aSArtem Bityutskiy "is clean", ui->ui_size, ui->synced_i_size); 8861e51764aSArtem Bityutskiy ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino, 8871e51764aSArtem Bityutskiy inode->i_mode, i_size_read(inode)); 8881e51764aSArtem Bityutskiy dbg_dump_stack(); 8891e51764aSArtem Bityutskiy err = -EINVAL; 8901e51764aSArtem Bityutskiy } 8911e51764aSArtem Bityutskiy spin_unlock(&ui->ui_lock); 8921e51764aSArtem Bityutskiy mutex_unlock(&ui->ui_mutex); 8931e51764aSArtem Bityutskiy return err; 8941e51764aSArtem Bityutskiy } 8951e51764aSArtem Bityutskiy 8961e51764aSArtem Bityutskiy /* 8971e51764aSArtem Bityutskiy * dbg_check_dir - check directory inode size and link count. 8981e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 8991e51764aSArtem Bityutskiy * @dir: the directory to calculate size for 9001e51764aSArtem Bityutskiy * @size: the result is returned here 9011e51764aSArtem Bityutskiy * 9021e51764aSArtem Bityutskiy * This function makes sure that directory size and link count are correct. 9031e51764aSArtem Bityutskiy * Returns zero in case of success and a negative error code in case of 9041e51764aSArtem Bityutskiy * failure. 9051e51764aSArtem Bityutskiy * 9061e51764aSArtem Bityutskiy * Note, it is good idea to make sure the @dir->i_mutex is locked before 9071e51764aSArtem Bityutskiy * calling this function. 9081e51764aSArtem Bityutskiy */ 9091e51764aSArtem Bityutskiy int dbg_check_dir_size(struct ubifs_info *c, const struct inode *dir) 9101e51764aSArtem Bityutskiy { 9111e51764aSArtem Bityutskiy unsigned int nlink = 2; 9121e51764aSArtem Bityutskiy union ubifs_key key; 9131e51764aSArtem Bityutskiy struct ubifs_dent_node *dent, *pdent = NULL; 9141e51764aSArtem Bityutskiy struct qstr nm = { .name = NULL }; 9151e51764aSArtem Bityutskiy loff_t size = UBIFS_INO_NODE_SZ; 9161e51764aSArtem Bityutskiy 9171e51764aSArtem Bityutskiy if (!(ubifs_chk_flags & UBIFS_CHK_GEN)) 9181e51764aSArtem Bityutskiy return 0; 9191e51764aSArtem Bityutskiy 9201e51764aSArtem Bityutskiy if (!S_ISDIR(dir->i_mode)) 9211e51764aSArtem Bityutskiy return 0; 9221e51764aSArtem Bityutskiy 9231e51764aSArtem Bityutskiy lowest_dent_key(c, &key, dir->i_ino); 9241e51764aSArtem Bityutskiy while (1) { 9251e51764aSArtem Bityutskiy int err; 9261e51764aSArtem Bityutskiy 9271e51764aSArtem Bityutskiy dent = ubifs_tnc_next_ent(c, &key, &nm); 9281e51764aSArtem Bityutskiy if (IS_ERR(dent)) { 9291e51764aSArtem Bityutskiy err = PTR_ERR(dent); 9301e51764aSArtem Bityutskiy if (err == -ENOENT) 9311e51764aSArtem Bityutskiy break; 9321e51764aSArtem Bityutskiy return err; 9331e51764aSArtem Bityutskiy } 9341e51764aSArtem Bityutskiy 9351e51764aSArtem Bityutskiy nm.name = dent->name; 9361e51764aSArtem Bityutskiy nm.len = le16_to_cpu(dent->nlen); 9371e51764aSArtem Bityutskiy size += CALC_DENT_SIZE(nm.len); 9381e51764aSArtem Bityutskiy if (dent->type == UBIFS_ITYPE_DIR) 9391e51764aSArtem Bityutskiy nlink += 1; 9401e51764aSArtem Bityutskiy kfree(pdent); 9411e51764aSArtem Bityutskiy pdent = dent; 9421e51764aSArtem Bityutskiy key_read(c, &dent->key, &key); 9431e51764aSArtem Bityutskiy } 9441e51764aSArtem Bityutskiy kfree(pdent); 9451e51764aSArtem Bityutskiy 9461e51764aSArtem Bityutskiy if (i_size_read(dir) != size) { 9471e51764aSArtem Bityutskiy ubifs_err("directory inode %lu has size %llu, " 9481e51764aSArtem Bityutskiy "but calculated size is %llu", dir->i_ino, 9491e51764aSArtem Bityutskiy (unsigned long long)i_size_read(dir), 9501e51764aSArtem Bityutskiy (unsigned long long)size); 9511e51764aSArtem Bityutskiy dump_stack(); 9521e51764aSArtem Bityutskiy return -EINVAL; 9531e51764aSArtem Bityutskiy } 9541e51764aSArtem Bityutskiy if (dir->i_nlink != nlink) { 9551e51764aSArtem Bityutskiy ubifs_err("directory inode %lu has nlink %u, but calculated " 9561e51764aSArtem Bityutskiy "nlink is %u", dir->i_ino, dir->i_nlink, nlink); 9571e51764aSArtem Bityutskiy dump_stack(); 9581e51764aSArtem Bityutskiy return -EINVAL; 9591e51764aSArtem Bityutskiy } 9601e51764aSArtem Bityutskiy 9611e51764aSArtem Bityutskiy return 0; 9621e51764aSArtem Bityutskiy } 9631e51764aSArtem Bityutskiy 9641e51764aSArtem Bityutskiy /** 9651e51764aSArtem Bityutskiy * dbg_check_key_order - make sure that colliding keys are properly ordered. 9661e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 9671e51764aSArtem Bityutskiy * @zbr1: first zbranch 9681e51764aSArtem Bityutskiy * @zbr2: following zbranch 9691e51764aSArtem Bityutskiy * 9701e51764aSArtem Bityutskiy * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of 9711e51764aSArtem Bityutskiy * names of the direntries/xentries which are referred by the keys. This 9721e51764aSArtem Bityutskiy * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes 9731e51764aSArtem Bityutskiy * sure the name of direntry/xentry referred by @zbr1 is less than 9741e51764aSArtem Bityutskiy * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not, 9751e51764aSArtem Bityutskiy * and a negative error code in case of failure. 9761e51764aSArtem Bityutskiy */ 9771e51764aSArtem Bityutskiy static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1, 9781e51764aSArtem Bityutskiy struct ubifs_zbranch *zbr2) 9791e51764aSArtem Bityutskiy { 9801e51764aSArtem Bityutskiy int err, nlen1, nlen2, cmp; 9811e51764aSArtem Bityutskiy struct ubifs_dent_node *dent1, *dent2; 9821e51764aSArtem Bityutskiy union ubifs_key key; 9831e51764aSArtem Bityutskiy 9841e51764aSArtem Bityutskiy ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key)); 9851e51764aSArtem Bityutskiy dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS); 9861e51764aSArtem Bityutskiy if (!dent1) 9871e51764aSArtem Bityutskiy return -ENOMEM; 9881e51764aSArtem Bityutskiy dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS); 9891e51764aSArtem Bityutskiy if (!dent2) { 9901e51764aSArtem Bityutskiy err = -ENOMEM; 9911e51764aSArtem Bityutskiy goto out_free; 9921e51764aSArtem Bityutskiy } 9931e51764aSArtem Bityutskiy 9941e51764aSArtem Bityutskiy err = ubifs_tnc_read_node(c, zbr1, dent1); 9951e51764aSArtem Bityutskiy if (err) 9961e51764aSArtem Bityutskiy goto out_free; 9971e51764aSArtem Bityutskiy err = ubifs_validate_entry(c, dent1); 9981e51764aSArtem Bityutskiy if (err) 9991e51764aSArtem Bityutskiy goto out_free; 10001e51764aSArtem Bityutskiy 10011e51764aSArtem Bityutskiy err = ubifs_tnc_read_node(c, zbr2, dent2); 10021e51764aSArtem Bityutskiy if (err) 10031e51764aSArtem Bityutskiy goto out_free; 10041e51764aSArtem Bityutskiy err = ubifs_validate_entry(c, dent2); 10051e51764aSArtem Bityutskiy if (err) 10061e51764aSArtem Bityutskiy goto out_free; 10071e51764aSArtem Bityutskiy 10081e51764aSArtem Bityutskiy /* Make sure node keys are the same as in zbranch */ 10091e51764aSArtem Bityutskiy err = 1; 10101e51764aSArtem Bityutskiy key_read(c, &dent1->key, &key); 10111e51764aSArtem Bityutskiy if (keys_cmp(c, &zbr1->key, &key)) { 1012552ff317SArtem Bityutskiy ubifs_err("1st entry at %d:%d has key %s", zbr1->lnum, 10131e51764aSArtem Bityutskiy zbr1->offs, DBGKEY(&key)); 1014552ff317SArtem Bityutskiy ubifs_err("but it should have key %s according to tnc", 10152ba5f7aeSArtem Bityutskiy DBGKEY(&zbr1->key)); 10162ba5f7aeSArtem Bityutskiy dbg_dump_node(c, dent1); 10171e51764aSArtem Bityutskiy goto out_free; 10181e51764aSArtem Bityutskiy } 10191e51764aSArtem Bityutskiy 10201e51764aSArtem Bityutskiy key_read(c, &dent2->key, &key); 10211e51764aSArtem Bityutskiy if (keys_cmp(c, &zbr2->key, &key)) { 1022552ff317SArtem Bityutskiy ubifs_err("2nd entry at %d:%d has key %s", zbr1->lnum, 10231e51764aSArtem Bityutskiy zbr1->offs, DBGKEY(&key)); 1024552ff317SArtem Bityutskiy ubifs_err("but it should have key %s according to tnc", 10252ba5f7aeSArtem Bityutskiy DBGKEY(&zbr2->key)); 10262ba5f7aeSArtem Bityutskiy dbg_dump_node(c, dent2); 10271e51764aSArtem Bityutskiy goto out_free; 10281e51764aSArtem Bityutskiy } 10291e51764aSArtem Bityutskiy 10301e51764aSArtem Bityutskiy nlen1 = le16_to_cpu(dent1->nlen); 10311e51764aSArtem Bityutskiy nlen2 = le16_to_cpu(dent2->nlen); 10321e51764aSArtem Bityutskiy 10331e51764aSArtem Bityutskiy cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2)); 10341e51764aSArtem Bityutskiy if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) { 10351e51764aSArtem Bityutskiy err = 0; 10361e51764aSArtem Bityutskiy goto out_free; 10371e51764aSArtem Bityutskiy } 10381e51764aSArtem Bityutskiy if (cmp == 0 && nlen1 == nlen2) 1039552ff317SArtem Bityutskiy ubifs_err("2 xent/dent nodes with the same name"); 10401e51764aSArtem Bityutskiy else 1041552ff317SArtem Bityutskiy ubifs_err("bad order of colliding key %s", 10421e51764aSArtem Bityutskiy DBGKEY(&key)); 10431e51764aSArtem Bityutskiy 1044552ff317SArtem Bityutskiy ubifs_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs); 10451e51764aSArtem Bityutskiy dbg_dump_node(c, dent1); 1046552ff317SArtem Bityutskiy ubifs_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs); 10471e51764aSArtem Bityutskiy dbg_dump_node(c, dent2); 10481e51764aSArtem Bityutskiy 10491e51764aSArtem Bityutskiy out_free: 10501e51764aSArtem Bityutskiy kfree(dent2); 10511e51764aSArtem Bityutskiy kfree(dent1); 10521e51764aSArtem Bityutskiy return err; 10531e51764aSArtem Bityutskiy } 10541e51764aSArtem Bityutskiy 10551e51764aSArtem Bityutskiy /** 10561e51764aSArtem Bityutskiy * dbg_check_znode - check if znode is all right. 10571e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 10581e51764aSArtem Bityutskiy * @zbr: zbranch which points to this znode 10591e51764aSArtem Bityutskiy * 10601e51764aSArtem Bityutskiy * This function makes sure that znode referred to by @zbr is all right. 10611e51764aSArtem Bityutskiy * Returns zero if it is, and %-EINVAL if it is not. 10621e51764aSArtem Bityutskiy */ 10631e51764aSArtem Bityutskiy static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr) 10641e51764aSArtem Bityutskiy { 10651e51764aSArtem Bityutskiy struct ubifs_znode *znode = zbr->znode; 10661e51764aSArtem Bityutskiy struct ubifs_znode *zp = znode->parent; 10671e51764aSArtem Bityutskiy int n, err, cmp; 10681e51764aSArtem Bityutskiy 10691e51764aSArtem Bityutskiy if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) { 10701e51764aSArtem Bityutskiy err = 1; 10711e51764aSArtem Bityutskiy goto out; 10721e51764aSArtem Bityutskiy } 10731e51764aSArtem Bityutskiy if (znode->level < 0) { 10741e51764aSArtem Bityutskiy err = 2; 10751e51764aSArtem Bityutskiy goto out; 10761e51764aSArtem Bityutskiy } 10771e51764aSArtem Bityutskiy if (znode->iip < 0 || znode->iip >= c->fanout) { 10781e51764aSArtem Bityutskiy err = 3; 10791e51764aSArtem Bityutskiy goto out; 10801e51764aSArtem Bityutskiy } 10811e51764aSArtem Bityutskiy 10821e51764aSArtem Bityutskiy if (zbr->len == 0) 10831e51764aSArtem Bityutskiy /* Only dirty zbranch may have no on-flash nodes */ 10841e51764aSArtem Bityutskiy if (!ubifs_zn_dirty(znode)) { 10851e51764aSArtem Bityutskiy err = 4; 10861e51764aSArtem Bityutskiy goto out; 10871e51764aSArtem Bityutskiy } 10881e51764aSArtem Bityutskiy 10891e51764aSArtem Bityutskiy if (ubifs_zn_dirty(znode)) { 10901e51764aSArtem Bityutskiy /* 10911e51764aSArtem Bityutskiy * If znode is dirty, its parent has to be dirty as well. The 10921e51764aSArtem Bityutskiy * order of the operation is important, so we have to have 10931e51764aSArtem Bityutskiy * memory barriers. 10941e51764aSArtem Bityutskiy */ 10951e51764aSArtem Bityutskiy smp_mb(); 10961e51764aSArtem Bityutskiy if (zp && !ubifs_zn_dirty(zp)) { 10971e51764aSArtem Bityutskiy /* 10981e51764aSArtem Bityutskiy * The dirty flag is atomic and is cleared outside the 10991e51764aSArtem Bityutskiy * TNC mutex, so znode's dirty flag may now have 11001e51764aSArtem Bityutskiy * been cleared. The child is always cleared before the 11011e51764aSArtem Bityutskiy * parent, so we just need to check again. 11021e51764aSArtem Bityutskiy */ 11031e51764aSArtem Bityutskiy smp_mb(); 11041e51764aSArtem Bityutskiy if (ubifs_zn_dirty(znode)) { 11051e51764aSArtem Bityutskiy err = 5; 11061e51764aSArtem Bityutskiy goto out; 11071e51764aSArtem Bityutskiy } 11081e51764aSArtem Bityutskiy } 11091e51764aSArtem Bityutskiy } 11101e51764aSArtem Bityutskiy 11111e51764aSArtem Bityutskiy if (zp) { 11121e51764aSArtem Bityutskiy const union ubifs_key *min, *max; 11131e51764aSArtem Bityutskiy 11141e51764aSArtem Bityutskiy if (znode->level != zp->level - 1) { 11151e51764aSArtem Bityutskiy err = 6; 11161e51764aSArtem Bityutskiy goto out; 11171e51764aSArtem Bityutskiy } 11181e51764aSArtem Bityutskiy 11191e51764aSArtem Bityutskiy /* Make sure the 'parent' pointer in our znode is correct */ 11201e51764aSArtem Bityutskiy err = ubifs_search_zbranch(c, zp, &zbr->key, &n); 11211e51764aSArtem Bityutskiy if (!err) { 11221e51764aSArtem Bityutskiy /* This zbranch does not exist in the parent */ 11231e51764aSArtem Bityutskiy err = 7; 11241e51764aSArtem Bityutskiy goto out; 11251e51764aSArtem Bityutskiy } 11261e51764aSArtem Bityutskiy 11271e51764aSArtem Bityutskiy if (znode->iip >= zp->child_cnt) { 11281e51764aSArtem Bityutskiy err = 8; 11291e51764aSArtem Bityutskiy goto out; 11301e51764aSArtem Bityutskiy } 11311e51764aSArtem Bityutskiy 11321e51764aSArtem Bityutskiy if (znode->iip != n) { 11331e51764aSArtem Bityutskiy /* This may happen only in case of collisions */ 11341e51764aSArtem Bityutskiy if (keys_cmp(c, &zp->zbranch[n].key, 11351e51764aSArtem Bityutskiy &zp->zbranch[znode->iip].key)) { 11361e51764aSArtem Bityutskiy err = 9; 11371e51764aSArtem Bityutskiy goto out; 11381e51764aSArtem Bityutskiy } 11391e51764aSArtem Bityutskiy n = znode->iip; 11401e51764aSArtem Bityutskiy } 11411e51764aSArtem Bityutskiy 11421e51764aSArtem Bityutskiy /* 11431e51764aSArtem Bityutskiy * Make sure that the first key in our znode is greater than or 11441e51764aSArtem Bityutskiy * equal to the key in the pointing zbranch. 11451e51764aSArtem Bityutskiy */ 11461e51764aSArtem Bityutskiy min = &zbr->key; 11471e51764aSArtem Bityutskiy cmp = keys_cmp(c, min, &znode->zbranch[0].key); 11481e51764aSArtem Bityutskiy if (cmp == 1) { 11491e51764aSArtem Bityutskiy err = 10; 11501e51764aSArtem Bityutskiy goto out; 11511e51764aSArtem Bityutskiy } 11521e51764aSArtem Bityutskiy 11531e51764aSArtem Bityutskiy if (n + 1 < zp->child_cnt) { 11541e51764aSArtem Bityutskiy max = &zp->zbranch[n + 1].key; 11551e51764aSArtem Bityutskiy 11561e51764aSArtem Bityutskiy /* 11571e51764aSArtem Bityutskiy * Make sure the last key in our znode is less or 11581e51764aSArtem Bityutskiy * equivalent than the the key in zbranch which goes 11591e51764aSArtem Bityutskiy * after our pointing zbranch. 11601e51764aSArtem Bityutskiy */ 11611e51764aSArtem Bityutskiy cmp = keys_cmp(c, max, 11621e51764aSArtem Bityutskiy &znode->zbranch[znode->child_cnt - 1].key); 11631e51764aSArtem Bityutskiy if (cmp == -1) { 11641e51764aSArtem Bityutskiy err = 11; 11651e51764aSArtem Bityutskiy goto out; 11661e51764aSArtem Bityutskiy } 11671e51764aSArtem Bityutskiy } 11681e51764aSArtem Bityutskiy } else { 11691e51764aSArtem Bityutskiy /* This may only be root znode */ 11701e51764aSArtem Bityutskiy if (zbr != &c->zroot) { 11711e51764aSArtem Bityutskiy err = 12; 11721e51764aSArtem Bityutskiy goto out; 11731e51764aSArtem Bityutskiy } 11741e51764aSArtem Bityutskiy } 11751e51764aSArtem Bityutskiy 11761e51764aSArtem Bityutskiy /* 11771e51764aSArtem Bityutskiy * Make sure that next key is greater or equivalent then the previous 11781e51764aSArtem Bityutskiy * one. 11791e51764aSArtem Bityutskiy */ 11801e51764aSArtem Bityutskiy for (n = 1; n < znode->child_cnt; n++) { 11811e51764aSArtem Bityutskiy cmp = keys_cmp(c, &znode->zbranch[n - 1].key, 11821e51764aSArtem Bityutskiy &znode->zbranch[n].key); 11831e51764aSArtem Bityutskiy if (cmp > 0) { 11841e51764aSArtem Bityutskiy err = 13; 11851e51764aSArtem Bityutskiy goto out; 11861e51764aSArtem Bityutskiy } 11871e51764aSArtem Bityutskiy if (cmp == 0) { 11881e51764aSArtem Bityutskiy /* This can only be keys with colliding hash */ 11891e51764aSArtem Bityutskiy if (!is_hash_key(c, &znode->zbranch[n].key)) { 11901e51764aSArtem Bityutskiy err = 14; 11911e51764aSArtem Bityutskiy goto out; 11921e51764aSArtem Bityutskiy } 11931e51764aSArtem Bityutskiy 11941e51764aSArtem Bityutskiy if (znode->level != 0 || c->replaying) 11951e51764aSArtem Bityutskiy continue; 11961e51764aSArtem Bityutskiy 11971e51764aSArtem Bityutskiy /* 11981e51764aSArtem Bityutskiy * Colliding keys should follow binary order of 11991e51764aSArtem Bityutskiy * corresponding xentry/dentry names. 12001e51764aSArtem Bityutskiy */ 12011e51764aSArtem Bityutskiy err = dbg_check_key_order(c, &znode->zbranch[n - 1], 12021e51764aSArtem Bityutskiy &znode->zbranch[n]); 12031e51764aSArtem Bityutskiy if (err < 0) 12041e51764aSArtem Bityutskiy return err; 12051e51764aSArtem Bityutskiy if (err) { 12061e51764aSArtem Bityutskiy err = 15; 12071e51764aSArtem Bityutskiy goto out; 12081e51764aSArtem Bityutskiy } 12091e51764aSArtem Bityutskiy } 12101e51764aSArtem Bityutskiy } 12111e51764aSArtem Bityutskiy 12121e51764aSArtem Bityutskiy for (n = 0; n < znode->child_cnt; n++) { 12131e51764aSArtem Bityutskiy if (!znode->zbranch[n].znode && 12141e51764aSArtem Bityutskiy (znode->zbranch[n].lnum == 0 || 12151e51764aSArtem Bityutskiy znode->zbranch[n].len == 0)) { 12161e51764aSArtem Bityutskiy err = 16; 12171e51764aSArtem Bityutskiy goto out; 12181e51764aSArtem Bityutskiy } 12191e51764aSArtem Bityutskiy 12201e51764aSArtem Bityutskiy if (znode->zbranch[n].lnum != 0 && 12211e51764aSArtem Bityutskiy znode->zbranch[n].len == 0) { 12221e51764aSArtem Bityutskiy err = 17; 12231e51764aSArtem Bityutskiy goto out; 12241e51764aSArtem Bityutskiy } 12251e51764aSArtem Bityutskiy 12261e51764aSArtem Bityutskiy if (znode->zbranch[n].lnum == 0 && 12271e51764aSArtem Bityutskiy znode->zbranch[n].len != 0) { 12281e51764aSArtem Bityutskiy err = 18; 12291e51764aSArtem Bityutskiy goto out; 12301e51764aSArtem Bityutskiy } 12311e51764aSArtem Bityutskiy 12321e51764aSArtem Bityutskiy if (znode->zbranch[n].lnum == 0 && 12331e51764aSArtem Bityutskiy znode->zbranch[n].offs != 0) { 12341e51764aSArtem Bityutskiy err = 19; 12351e51764aSArtem Bityutskiy goto out; 12361e51764aSArtem Bityutskiy } 12371e51764aSArtem Bityutskiy 12381e51764aSArtem Bityutskiy if (znode->level != 0 && znode->zbranch[n].znode) 12391e51764aSArtem Bityutskiy if (znode->zbranch[n].znode->parent != znode) { 12401e51764aSArtem Bityutskiy err = 20; 12411e51764aSArtem Bityutskiy goto out; 12421e51764aSArtem Bityutskiy } 12431e51764aSArtem Bityutskiy } 12441e51764aSArtem Bityutskiy 12451e51764aSArtem Bityutskiy return 0; 12461e51764aSArtem Bityutskiy 12471e51764aSArtem Bityutskiy out: 12481e51764aSArtem Bityutskiy ubifs_err("failed, error %d", err); 12491e51764aSArtem Bityutskiy ubifs_msg("dump of the znode"); 12501e51764aSArtem Bityutskiy dbg_dump_znode(c, znode); 12511e51764aSArtem Bityutskiy if (zp) { 12521e51764aSArtem Bityutskiy ubifs_msg("dump of the parent znode"); 12531e51764aSArtem Bityutskiy dbg_dump_znode(c, zp); 12541e51764aSArtem Bityutskiy } 12551e51764aSArtem Bityutskiy dump_stack(); 12561e51764aSArtem Bityutskiy return -EINVAL; 12571e51764aSArtem Bityutskiy } 12581e51764aSArtem Bityutskiy 12591e51764aSArtem Bityutskiy /** 12601e51764aSArtem Bityutskiy * dbg_check_tnc - check TNC tree. 12611e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 12621e51764aSArtem Bityutskiy * @extra: do extra checks that are possible at start commit 12631e51764aSArtem Bityutskiy * 12641e51764aSArtem Bityutskiy * This function traverses whole TNC tree and checks every znode. Returns zero 12651e51764aSArtem Bityutskiy * if everything is all right and %-EINVAL if something is wrong with TNC. 12661e51764aSArtem Bityutskiy */ 12671e51764aSArtem Bityutskiy int dbg_check_tnc(struct ubifs_info *c, int extra) 12681e51764aSArtem Bityutskiy { 12691e51764aSArtem Bityutskiy struct ubifs_znode *znode; 12701e51764aSArtem Bityutskiy long clean_cnt = 0, dirty_cnt = 0; 12711e51764aSArtem Bityutskiy int err, last; 12721e51764aSArtem Bityutskiy 12731e51764aSArtem Bityutskiy if (!(ubifs_chk_flags & UBIFS_CHK_TNC)) 12741e51764aSArtem Bityutskiy return 0; 12751e51764aSArtem Bityutskiy 12761e51764aSArtem Bityutskiy ubifs_assert(mutex_is_locked(&c->tnc_mutex)); 12771e51764aSArtem Bityutskiy if (!c->zroot.znode) 12781e51764aSArtem Bityutskiy return 0; 12791e51764aSArtem Bityutskiy 12801e51764aSArtem Bityutskiy znode = ubifs_tnc_postorder_first(c->zroot.znode); 12811e51764aSArtem Bityutskiy while (1) { 12821e51764aSArtem Bityutskiy struct ubifs_znode *prev; 12831e51764aSArtem Bityutskiy struct ubifs_zbranch *zbr; 12841e51764aSArtem Bityutskiy 12851e51764aSArtem Bityutskiy if (!znode->parent) 12861e51764aSArtem Bityutskiy zbr = &c->zroot; 12871e51764aSArtem Bityutskiy else 12881e51764aSArtem Bityutskiy zbr = &znode->parent->zbranch[znode->iip]; 12891e51764aSArtem Bityutskiy 12901e51764aSArtem Bityutskiy err = dbg_check_znode(c, zbr); 12911e51764aSArtem Bityutskiy if (err) 12921e51764aSArtem Bityutskiy return err; 12931e51764aSArtem Bityutskiy 12941e51764aSArtem Bityutskiy if (extra) { 12951e51764aSArtem Bityutskiy if (ubifs_zn_dirty(znode)) 12961e51764aSArtem Bityutskiy dirty_cnt += 1; 12971e51764aSArtem Bityutskiy else 12981e51764aSArtem Bityutskiy clean_cnt += 1; 12991e51764aSArtem Bityutskiy } 13001e51764aSArtem Bityutskiy 13011e51764aSArtem Bityutskiy prev = znode; 13021e51764aSArtem Bityutskiy znode = ubifs_tnc_postorder_next(znode); 13031e51764aSArtem Bityutskiy if (!znode) 13041e51764aSArtem Bityutskiy break; 13051e51764aSArtem Bityutskiy 13061e51764aSArtem Bityutskiy /* 13071e51764aSArtem Bityutskiy * If the last key of this znode is equivalent to the first key 13081e51764aSArtem Bityutskiy * of the next znode (collision), then check order of the keys. 13091e51764aSArtem Bityutskiy */ 13101e51764aSArtem Bityutskiy last = prev->child_cnt - 1; 13111e51764aSArtem Bityutskiy if (prev->level == 0 && znode->level == 0 && !c->replaying && 13121e51764aSArtem Bityutskiy !keys_cmp(c, &prev->zbranch[last].key, 13131e51764aSArtem Bityutskiy &znode->zbranch[0].key)) { 13141e51764aSArtem Bityutskiy err = dbg_check_key_order(c, &prev->zbranch[last], 13151e51764aSArtem Bityutskiy &znode->zbranch[0]); 13161e51764aSArtem Bityutskiy if (err < 0) 13171e51764aSArtem Bityutskiy return err; 13181e51764aSArtem Bityutskiy if (err) { 13191e51764aSArtem Bityutskiy ubifs_msg("first znode"); 13201e51764aSArtem Bityutskiy dbg_dump_znode(c, prev); 13211e51764aSArtem Bityutskiy ubifs_msg("second znode"); 13221e51764aSArtem Bityutskiy dbg_dump_znode(c, znode); 13231e51764aSArtem Bityutskiy return -EINVAL; 13241e51764aSArtem Bityutskiy } 13251e51764aSArtem Bityutskiy } 13261e51764aSArtem Bityutskiy } 13271e51764aSArtem Bityutskiy 13281e51764aSArtem Bityutskiy if (extra) { 13291e51764aSArtem Bityutskiy if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) { 13301e51764aSArtem Bityutskiy ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld", 13311e51764aSArtem Bityutskiy atomic_long_read(&c->clean_zn_cnt), 13321e51764aSArtem Bityutskiy clean_cnt); 13331e51764aSArtem Bityutskiy return -EINVAL; 13341e51764aSArtem Bityutskiy } 13351e51764aSArtem Bityutskiy if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) { 13361e51764aSArtem Bityutskiy ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld", 13371e51764aSArtem Bityutskiy atomic_long_read(&c->dirty_zn_cnt), 13381e51764aSArtem Bityutskiy dirty_cnt); 13391e51764aSArtem Bityutskiy return -EINVAL; 13401e51764aSArtem Bityutskiy } 13411e51764aSArtem Bityutskiy } 13421e51764aSArtem Bityutskiy 13431e51764aSArtem Bityutskiy return 0; 13441e51764aSArtem Bityutskiy } 13451e51764aSArtem Bityutskiy 13461e51764aSArtem Bityutskiy /** 13471e51764aSArtem Bityutskiy * dbg_walk_index - walk the on-flash index. 13481e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 13491e51764aSArtem Bityutskiy * @leaf_cb: called for each leaf node 13501e51764aSArtem Bityutskiy * @znode_cb: called for each indexing node 13511e51764aSArtem Bityutskiy * @priv: private date which is passed to callbacks 13521e51764aSArtem Bityutskiy * 13531e51764aSArtem Bityutskiy * This function walks the UBIFS index and calls the @leaf_cb for each leaf 13541e51764aSArtem Bityutskiy * node and @znode_cb for each indexing node. Returns zero in case of success 13551e51764aSArtem Bityutskiy * and a negative error code in case of failure. 13561e51764aSArtem Bityutskiy * 13571e51764aSArtem Bityutskiy * It would be better if this function removed every znode it pulled to into 13581e51764aSArtem Bityutskiy * the TNC, so that the behavior more closely matched the non-debugging 13591e51764aSArtem Bityutskiy * behavior. 13601e51764aSArtem Bityutskiy */ 13611e51764aSArtem Bityutskiy int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb, 13621e51764aSArtem Bityutskiy dbg_znode_callback znode_cb, void *priv) 13631e51764aSArtem Bityutskiy { 13641e51764aSArtem Bityutskiy int err; 13651e51764aSArtem Bityutskiy struct ubifs_zbranch *zbr; 13661e51764aSArtem Bityutskiy struct ubifs_znode *znode, *child; 13671e51764aSArtem Bityutskiy 13681e51764aSArtem Bityutskiy mutex_lock(&c->tnc_mutex); 13691e51764aSArtem Bityutskiy /* If the root indexing node is not in TNC - pull it */ 13701e51764aSArtem Bityutskiy if (!c->zroot.znode) { 13711e51764aSArtem Bityutskiy c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0); 13721e51764aSArtem Bityutskiy if (IS_ERR(c->zroot.znode)) { 13731e51764aSArtem Bityutskiy err = PTR_ERR(c->zroot.znode); 13741e51764aSArtem Bityutskiy c->zroot.znode = NULL; 13751e51764aSArtem Bityutskiy goto out_unlock; 13761e51764aSArtem Bityutskiy } 13771e51764aSArtem Bityutskiy } 13781e51764aSArtem Bityutskiy 13791e51764aSArtem Bityutskiy /* 13801e51764aSArtem Bityutskiy * We are going to traverse the indexing tree in the postorder manner. 13811e51764aSArtem Bityutskiy * Go down and find the leftmost indexing node where we are going to 13821e51764aSArtem Bityutskiy * start from. 13831e51764aSArtem Bityutskiy */ 13841e51764aSArtem Bityutskiy znode = c->zroot.znode; 13851e51764aSArtem Bityutskiy while (znode->level > 0) { 13861e51764aSArtem Bityutskiy zbr = &znode->zbranch[0]; 13871e51764aSArtem Bityutskiy child = zbr->znode; 13881e51764aSArtem Bityutskiy if (!child) { 13891e51764aSArtem Bityutskiy child = ubifs_load_znode(c, zbr, znode, 0); 13901e51764aSArtem Bityutskiy if (IS_ERR(child)) { 13911e51764aSArtem Bityutskiy err = PTR_ERR(child); 13921e51764aSArtem Bityutskiy goto out_unlock; 13931e51764aSArtem Bityutskiy } 13941e51764aSArtem Bityutskiy zbr->znode = child; 13951e51764aSArtem Bityutskiy } 13961e51764aSArtem Bityutskiy 13971e51764aSArtem Bityutskiy znode = child; 13981e51764aSArtem Bityutskiy } 13991e51764aSArtem Bityutskiy 14001e51764aSArtem Bityutskiy /* Iterate over all indexing nodes */ 14011e51764aSArtem Bityutskiy while (1) { 14021e51764aSArtem Bityutskiy int idx; 14031e51764aSArtem Bityutskiy 14041e51764aSArtem Bityutskiy cond_resched(); 14051e51764aSArtem Bityutskiy 14061e51764aSArtem Bityutskiy if (znode_cb) { 14071e51764aSArtem Bityutskiy err = znode_cb(c, znode, priv); 14081e51764aSArtem Bityutskiy if (err) { 14091e51764aSArtem Bityutskiy ubifs_err("znode checking function returned " 14101e51764aSArtem Bityutskiy "error %d", err); 14111e51764aSArtem Bityutskiy dbg_dump_znode(c, znode); 14121e51764aSArtem Bityutskiy goto out_dump; 14131e51764aSArtem Bityutskiy } 14141e51764aSArtem Bityutskiy } 14151e51764aSArtem Bityutskiy if (leaf_cb && znode->level == 0) { 14161e51764aSArtem Bityutskiy for (idx = 0; idx < znode->child_cnt; idx++) { 14171e51764aSArtem Bityutskiy zbr = &znode->zbranch[idx]; 14181e51764aSArtem Bityutskiy err = leaf_cb(c, zbr, priv); 14191e51764aSArtem Bityutskiy if (err) { 14201e51764aSArtem Bityutskiy ubifs_err("leaf checking function " 14211e51764aSArtem Bityutskiy "returned error %d, for leaf " 14221e51764aSArtem Bityutskiy "at LEB %d:%d", 14231e51764aSArtem Bityutskiy err, zbr->lnum, zbr->offs); 14241e51764aSArtem Bityutskiy goto out_dump; 14251e51764aSArtem Bityutskiy } 14261e51764aSArtem Bityutskiy } 14271e51764aSArtem Bityutskiy } 14281e51764aSArtem Bityutskiy 14291e51764aSArtem Bityutskiy if (!znode->parent) 14301e51764aSArtem Bityutskiy break; 14311e51764aSArtem Bityutskiy 14321e51764aSArtem Bityutskiy idx = znode->iip + 1; 14331e51764aSArtem Bityutskiy znode = znode->parent; 14341e51764aSArtem Bityutskiy if (idx < znode->child_cnt) { 14351e51764aSArtem Bityutskiy /* Switch to the next index in the parent */ 14361e51764aSArtem Bityutskiy zbr = &znode->zbranch[idx]; 14371e51764aSArtem Bityutskiy child = zbr->znode; 14381e51764aSArtem Bityutskiy if (!child) { 14391e51764aSArtem Bityutskiy child = ubifs_load_znode(c, zbr, znode, idx); 14401e51764aSArtem Bityutskiy if (IS_ERR(child)) { 14411e51764aSArtem Bityutskiy err = PTR_ERR(child); 14421e51764aSArtem Bityutskiy goto out_unlock; 14431e51764aSArtem Bityutskiy } 14441e51764aSArtem Bityutskiy zbr->znode = child; 14451e51764aSArtem Bityutskiy } 14461e51764aSArtem Bityutskiy znode = child; 14471e51764aSArtem Bityutskiy } else 14481e51764aSArtem Bityutskiy /* 14491e51764aSArtem Bityutskiy * This is the last child, switch to the parent and 14501e51764aSArtem Bityutskiy * continue. 14511e51764aSArtem Bityutskiy */ 14521e51764aSArtem Bityutskiy continue; 14531e51764aSArtem Bityutskiy 14541e51764aSArtem Bityutskiy /* Go to the lowest leftmost znode in the new sub-tree */ 14551e51764aSArtem Bityutskiy while (znode->level > 0) { 14561e51764aSArtem Bityutskiy zbr = &znode->zbranch[0]; 14571e51764aSArtem Bityutskiy child = zbr->znode; 14581e51764aSArtem Bityutskiy if (!child) { 14591e51764aSArtem Bityutskiy child = ubifs_load_znode(c, zbr, znode, 0); 14601e51764aSArtem Bityutskiy if (IS_ERR(child)) { 14611e51764aSArtem Bityutskiy err = PTR_ERR(child); 14621e51764aSArtem Bityutskiy goto out_unlock; 14631e51764aSArtem Bityutskiy } 14641e51764aSArtem Bityutskiy zbr->znode = child; 14651e51764aSArtem Bityutskiy } 14661e51764aSArtem Bityutskiy znode = child; 14671e51764aSArtem Bityutskiy } 14681e51764aSArtem Bityutskiy } 14691e51764aSArtem Bityutskiy 14701e51764aSArtem Bityutskiy mutex_unlock(&c->tnc_mutex); 14711e51764aSArtem Bityutskiy return 0; 14721e51764aSArtem Bityutskiy 14731e51764aSArtem Bityutskiy out_dump: 14741e51764aSArtem Bityutskiy if (znode->parent) 14751e51764aSArtem Bityutskiy zbr = &znode->parent->zbranch[znode->iip]; 14761e51764aSArtem Bityutskiy else 14771e51764aSArtem Bityutskiy zbr = &c->zroot; 14781e51764aSArtem Bityutskiy ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs); 14791e51764aSArtem Bityutskiy dbg_dump_znode(c, znode); 14801e51764aSArtem Bityutskiy out_unlock: 14811e51764aSArtem Bityutskiy mutex_unlock(&c->tnc_mutex); 14821e51764aSArtem Bityutskiy return err; 14831e51764aSArtem Bityutskiy } 14841e51764aSArtem Bityutskiy 14851e51764aSArtem Bityutskiy /** 14861e51764aSArtem Bityutskiy * add_size - add znode size to partially calculated index size. 14871e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 14881e51764aSArtem Bityutskiy * @znode: znode to add size for 14891e51764aSArtem Bityutskiy * @priv: partially calculated index size 14901e51764aSArtem Bityutskiy * 14911e51764aSArtem Bityutskiy * This is a helper function for 'dbg_check_idx_size()' which is called for 14921e51764aSArtem Bityutskiy * every indexing node and adds its size to the 'long long' variable pointed to 14931e51764aSArtem Bityutskiy * by @priv. 14941e51764aSArtem Bityutskiy */ 14951e51764aSArtem Bityutskiy static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv) 14961e51764aSArtem Bityutskiy { 14971e51764aSArtem Bityutskiy long long *idx_size = priv; 14981e51764aSArtem Bityutskiy int add; 14991e51764aSArtem Bityutskiy 15001e51764aSArtem Bityutskiy add = ubifs_idx_node_sz(c, znode->child_cnt); 15011e51764aSArtem Bityutskiy add = ALIGN(add, 8); 15021e51764aSArtem Bityutskiy *idx_size += add; 15031e51764aSArtem Bityutskiy return 0; 15041e51764aSArtem Bityutskiy } 15051e51764aSArtem Bityutskiy 15061e51764aSArtem Bityutskiy /** 15071e51764aSArtem Bityutskiy * dbg_check_idx_size - check index size. 15081e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 15091e51764aSArtem Bityutskiy * @idx_size: size to check 15101e51764aSArtem Bityutskiy * 15111e51764aSArtem Bityutskiy * This function walks the UBIFS index, calculates its size and checks that the 15121e51764aSArtem Bityutskiy * size is equivalent to @idx_size. Returns zero in case of success and a 15131e51764aSArtem Bityutskiy * negative error code in case of failure. 15141e51764aSArtem Bityutskiy */ 15151e51764aSArtem Bityutskiy int dbg_check_idx_size(struct ubifs_info *c, long long idx_size) 15161e51764aSArtem Bityutskiy { 15171e51764aSArtem Bityutskiy int err; 15181e51764aSArtem Bityutskiy long long calc = 0; 15191e51764aSArtem Bityutskiy 15201e51764aSArtem Bityutskiy if (!(ubifs_chk_flags & UBIFS_CHK_IDX_SZ)) 15211e51764aSArtem Bityutskiy return 0; 15221e51764aSArtem Bityutskiy 15231e51764aSArtem Bityutskiy err = dbg_walk_index(c, NULL, add_size, &calc); 15241e51764aSArtem Bityutskiy if (err) { 15251e51764aSArtem Bityutskiy ubifs_err("error %d while walking the index", err); 15261e51764aSArtem Bityutskiy return err; 15271e51764aSArtem Bityutskiy } 15281e51764aSArtem Bityutskiy 15291e51764aSArtem Bityutskiy if (calc != idx_size) { 15301e51764aSArtem Bityutskiy ubifs_err("index size check failed: calculated size is %lld, " 15311e51764aSArtem Bityutskiy "should be %lld", calc, idx_size); 15321e51764aSArtem Bityutskiy dump_stack(); 15331e51764aSArtem Bityutskiy return -EINVAL; 15341e51764aSArtem Bityutskiy } 15351e51764aSArtem Bityutskiy 15361e51764aSArtem Bityutskiy return 0; 15371e51764aSArtem Bityutskiy } 15381e51764aSArtem Bityutskiy 15391e51764aSArtem Bityutskiy /** 15401e51764aSArtem Bityutskiy * struct fsck_inode - information about an inode used when checking the file-system. 15411e51764aSArtem Bityutskiy * @rb: link in the RB-tree of inodes 15421e51764aSArtem Bityutskiy * @inum: inode number 15431e51764aSArtem Bityutskiy * @mode: inode type, permissions, etc 15441e51764aSArtem Bityutskiy * @nlink: inode link count 15451e51764aSArtem Bityutskiy * @xattr_cnt: count of extended attributes 15461e51764aSArtem Bityutskiy * @references: how many directory/xattr entries refer this inode (calculated 15471e51764aSArtem Bityutskiy * while walking the index) 15481e51764aSArtem Bityutskiy * @calc_cnt: for directory inode count of child directories 15491e51764aSArtem Bityutskiy * @size: inode size (read from on-flash inode) 15501e51764aSArtem Bityutskiy * @xattr_sz: summary size of all extended attributes (read from on-flash 15511e51764aSArtem Bityutskiy * inode) 15521e51764aSArtem Bityutskiy * @calc_sz: for directories calculated directory size 15531e51764aSArtem Bityutskiy * @calc_xcnt: count of extended attributes 15541e51764aSArtem Bityutskiy * @calc_xsz: calculated summary size of all extended attributes 15551e51764aSArtem Bityutskiy * @xattr_nms: sum of lengths of all extended attribute names belonging to this 15561e51764aSArtem Bityutskiy * inode (read from on-flash inode) 15571e51764aSArtem Bityutskiy * @calc_xnms: calculated sum of lengths of all extended attribute names 15581e51764aSArtem Bityutskiy */ 15591e51764aSArtem Bityutskiy struct fsck_inode { 15601e51764aSArtem Bityutskiy struct rb_node rb; 15611e51764aSArtem Bityutskiy ino_t inum; 15621e51764aSArtem Bityutskiy umode_t mode; 15631e51764aSArtem Bityutskiy unsigned int nlink; 15641e51764aSArtem Bityutskiy unsigned int xattr_cnt; 15651e51764aSArtem Bityutskiy int references; 15661e51764aSArtem Bityutskiy int calc_cnt; 15671e51764aSArtem Bityutskiy long long size; 15681e51764aSArtem Bityutskiy unsigned int xattr_sz; 15691e51764aSArtem Bityutskiy long long calc_sz; 15701e51764aSArtem Bityutskiy long long calc_xcnt; 15711e51764aSArtem Bityutskiy long long calc_xsz; 15721e51764aSArtem Bityutskiy unsigned int xattr_nms; 15731e51764aSArtem Bityutskiy long long calc_xnms; 15741e51764aSArtem Bityutskiy }; 15751e51764aSArtem Bityutskiy 15761e51764aSArtem Bityutskiy /** 15771e51764aSArtem Bityutskiy * struct fsck_data - private FS checking information. 15781e51764aSArtem Bityutskiy * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects) 15791e51764aSArtem Bityutskiy */ 15801e51764aSArtem Bityutskiy struct fsck_data { 15811e51764aSArtem Bityutskiy struct rb_root inodes; 15821e51764aSArtem Bityutskiy }; 15831e51764aSArtem Bityutskiy 15841e51764aSArtem Bityutskiy /** 15851e51764aSArtem Bityutskiy * add_inode - add inode information to RB-tree of inodes. 15861e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 15871e51764aSArtem Bityutskiy * @fsckd: FS checking information 15881e51764aSArtem Bityutskiy * @ino: raw UBIFS inode to add 15891e51764aSArtem Bityutskiy * 15901e51764aSArtem Bityutskiy * This is a helper function for 'check_leaf()' which adds information about 15911e51764aSArtem Bityutskiy * inode @ino to the RB-tree of inodes. Returns inode information pointer in 15921e51764aSArtem Bityutskiy * case of success and a negative error code in case of failure. 15931e51764aSArtem Bityutskiy */ 15941e51764aSArtem Bityutskiy static struct fsck_inode *add_inode(struct ubifs_info *c, 15951e51764aSArtem Bityutskiy struct fsck_data *fsckd, 15961e51764aSArtem Bityutskiy struct ubifs_ino_node *ino) 15971e51764aSArtem Bityutskiy { 15981e51764aSArtem Bityutskiy struct rb_node **p, *parent = NULL; 15991e51764aSArtem Bityutskiy struct fsck_inode *fscki; 16001e51764aSArtem Bityutskiy ino_t inum = key_inum_flash(c, &ino->key); 16011e51764aSArtem Bityutskiy 16021e51764aSArtem Bityutskiy p = &fsckd->inodes.rb_node; 16031e51764aSArtem Bityutskiy while (*p) { 16041e51764aSArtem Bityutskiy parent = *p; 16051e51764aSArtem Bityutskiy fscki = rb_entry(parent, struct fsck_inode, rb); 16061e51764aSArtem Bityutskiy if (inum < fscki->inum) 16071e51764aSArtem Bityutskiy p = &(*p)->rb_left; 16081e51764aSArtem Bityutskiy else if (inum > fscki->inum) 16091e51764aSArtem Bityutskiy p = &(*p)->rb_right; 16101e51764aSArtem Bityutskiy else 16111e51764aSArtem Bityutskiy return fscki; 16121e51764aSArtem Bityutskiy } 16131e51764aSArtem Bityutskiy 16141e51764aSArtem Bityutskiy if (inum > c->highest_inum) { 16151e51764aSArtem Bityutskiy ubifs_err("too high inode number, max. is %lu", 1616e84461adSArtem Bityutskiy (unsigned long)c->highest_inum); 16171e51764aSArtem Bityutskiy return ERR_PTR(-EINVAL); 16181e51764aSArtem Bityutskiy } 16191e51764aSArtem Bityutskiy 16201e51764aSArtem Bityutskiy fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS); 16211e51764aSArtem Bityutskiy if (!fscki) 16221e51764aSArtem Bityutskiy return ERR_PTR(-ENOMEM); 16231e51764aSArtem Bityutskiy 16241e51764aSArtem Bityutskiy fscki->inum = inum; 16251e51764aSArtem Bityutskiy fscki->nlink = le32_to_cpu(ino->nlink); 16261e51764aSArtem Bityutskiy fscki->size = le64_to_cpu(ino->size); 16271e51764aSArtem Bityutskiy fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt); 16281e51764aSArtem Bityutskiy fscki->xattr_sz = le32_to_cpu(ino->xattr_size); 16291e51764aSArtem Bityutskiy fscki->xattr_nms = le32_to_cpu(ino->xattr_names); 16301e51764aSArtem Bityutskiy fscki->mode = le32_to_cpu(ino->mode); 16311e51764aSArtem Bityutskiy if (S_ISDIR(fscki->mode)) { 16321e51764aSArtem Bityutskiy fscki->calc_sz = UBIFS_INO_NODE_SZ; 16331e51764aSArtem Bityutskiy fscki->calc_cnt = 2; 16341e51764aSArtem Bityutskiy } 16351e51764aSArtem Bityutskiy rb_link_node(&fscki->rb, parent, p); 16361e51764aSArtem Bityutskiy rb_insert_color(&fscki->rb, &fsckd->inodes); 16371e51764aSArtem Bityutskiy return fscki; 16381e51764aSArtem Bityutskiy } 16391e51764aSArtem Bityutskiy 16401e51764aSArtem Bityutskiy /** 16411e51764aSArtem Bityutskiy * search_inode - search inode in the RB-tree of inodes. 16421e51764aSArtem Bityutskiy * @fsckd: FS checking information 16431e51764aSArtem Bityutskiy * @inum: inode number to search 16441e51764aSArtem Bityutskiy * 16451e51764aSArtem Bityutskiy * This is a helper function for 'check_leaf()' which searches inode @inum in 16461e51764aSArtem Bityutskiy * the RB-tree of inodes and returns an inode information pointer or %NULL if 16471e51764aSArtem Bityutskiy * the inode was not found. 16481e51764aSArtem Bityutskiy */ 16491e51764aSArtem Bityutskiy static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum) 16501e51764aSArtem Bityutskiy { 16511e51764aSArtem Bityutskiy struct rb_node *p; 16521e51764aSArtem Bityutskiy struct fsck_inode *fscki; 16531e51764aSArtem Bityutskiy 16541e51764aSArtem Bityutskiy p = fsckd->inodes.rb_node; 16551e51764aSArtem Bityutskiy while (p) { 16561e51764aSArtem Bityutskiy fscki = rb_entry(p, struct fsck_inode, rb); 16571e51764aSArtem Bityutskiy if (inum < fscki->inum) 16581e51764aSArtem Bityutskiy p = p->rb_left; 16591e51764aSArtem Bityutskiy else if (inum > fscki->inum) 16601e51764aSArtem Bityutskiy p = p->rb_right; 16611e51764aSArtem Bityutskiy else 16621e51764aSArtem Bityutskiy return fscki; 16631e51764aSArtem Bityutskiy } 16641e51764aSArtem Bityutskiy return NULL; 16651e51764aSArtem Bityutskiy } 16661e51764aSArtem Bityutskiy 16671e51764aSArtem Bityutskiy /** 16681e51764aSArtem Bityutskiy * read_add_inode - read inode node and add it to RB-tree of inodes. 16691e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 16701e51764aSArtem Bityutskiy * @fsckd: FS checking information 16711e51764aSArtem Bityutskiy * @inum: inode number to read 16721e51764aSArtem Bityutskiy * 16731e51764aSArtem Bityutskiy * This is a helper function for 'check_leaf()' which finds inode node @inum in 16741e51764aSArtem Bityutskiy * the index, reads it, and adds it to the RB-tree of inodes. Returns inode 16751e51764aSArtem Bityutskiy * information pointer in case of success and a negative error code in case of 16761e51764aSArtem Bityutskiy * failure. 16771e51764aSArtem Bityutskiy */ 16781e51764aSArtem Bityutskiy static struct fsck_inode *read_add_inode(struct ubifs_info *c, 16791e51764aSArtem Bityutskiy struct fsck_data *fsckd, ino_t inum) 16801e51764aSArtem Bityutskiy { 16811e51764aSArtem Bityutskiy int n, err; 16821e51764aSArtem Bityutskiy union ubifs_key key; 16831e51764aSArtem Bityutskiy struct ubifs_znode *znode; 16841e51764aSArtem Bityutskiy struct ubifs_zbranch *zbr; 16851e51764aSArtem Bityutskiy struct ubifs_ino_node *ino; 16861e51764aSArtem Bityutskiy struct fsck_inode *fscki; 16871e51764aSArtem Bityutskiy 16881e51764aSArtem Bityutskiy fscki = search_inode(fsckd, inum); 16891e51764aSArtem Bityutskiy if (fscki) 16901e51764aSArtem Bityutskiy return fscki; 16911e51764aSArtem Bityutskiy 16921e51764aSArtem Bityutskiy ino_key_init(c, &key, inum); 16931e51764aSArtem Bityutskiy err = ubifs_lookup_level0(c, &key, &znode, &n); 16941e51764aSArtem Bityutskiy if (!err) { 1695e84461adSArtem Bityutskiy ubifs_err("inode %lu not found in index", (unsigned long)inum); 16961e51764aSArtem Bityutskiy return ERR_PTR(-ENOENT); 16971e51764aSArtem Bityutskiy } else if (err < 0) { 1698e84461adSArtem Bityutskiy ubifs_err("error %d while looking up inode %lu", 1699e84461adSArtem Bityutskiy err, (unsigned long)inum); 17001e51764aSArtem Bityutskiy return ERR_PTR(err); 17011e51764aSArtem Bityutskiy } 17021e51764aSArtem Bityutskiy 17031e51764aSArtem Bityutskiy zbr = &znode->zbranch[n]; 17041e51764aSArtem Bityutskiy if (zbr->len < UBIFS_INO_NODE_SZ) { 1705e84461adSArtem Bityutskiy ubifs_err("bad node %lu node length %d", 1706e84461adSArtem Bityutskiy (unsigned long)inum, zbr->len); 17071e51764aSArtem Bityutskiy return ERR_PTR(-EINVAL); 17081e51764aSArtem Bityutskiy } 17091e51764aSArtem Bityutskiy 17101e51764aSArtem Bityutskiy ino = kmalloc(zbr->len, GFP_NOFS); 17111e51764aSArtem Bityutskiy if (!ino) 17121e51764aSArtem Bityutskiy return ERR_PTR(-ENOMEM); 17131e51764aSArtem Bityutskiy 17141e51764aSArtem Bityutskiy err = ubifs_tnc_read_node(c, zbr, ino); 17151e51764aSArtem Bityutskiy if (err) { 17161e51764aSArtem Bityutskiy ubifs_err("cannot read inode node at LEB %d:%d, error %d", 17171e51764aSArtem Bityutskiy zbr->lnum, zbr->offs, err); 17181e51764aSArtem Bityutskiy kfree(ino); 17191e51764aSArtem Bityutskiy return ERR_PTR(err); 17201e51764aSArtem Bityutskiy } 17211e51764aSArtem Bityutskiy 17221e51764aSArtem Bityutskiy fscki = add_inode(c, fsckd, ino); 17231e51764aSArtem Bityutskiy kfree(ino); 17241e51764aSArtem Bityutskiy if (IS_ERR(fscki)) { 17251e51764aSArtem Bityutskiy ubifs_err("error %ld while adding inode %lu node", 1726e84461adSArtem Bityutskiy PTR_ERR(fscki), (unsigned long)inum); 17271e51764aSArtem Bityutskiy return fscki; 17281e51764aSArtem Bityutskiy } 17291e51764aSArtem Bityutskiy 17301e51764aSArtem Bityutskiy return fscki; 17311e51764aSArtem Bityutskiy } 17321e51764aSArtem Bityutskiy 17331e51764aSArtem Bityutskiy /** 17341e51764aSArtem Bityutskiy * check_leaf - check leaf node. 17351e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 17361e51764aSArtem Bityutskiy * @zbr: zbranch of the leaf node to check 17371e51764aSArtem Bityutskiy * @priv: FS checking information 17381e51764aSArtem Bityutskiy * 17391e51764aSArtem Bityutskiy * This is a helper function for 'dbg_check_filesystem()' which is called for 17401e51764aSArtem Bityutskiy * every single leaf node while walking the indexing tree. It checks that the 17411e51764aSArtem Bityutskiy * leaf node referred from the indexing tree exists, has correct CRC, and does 17421e51764aSArtem Bityutskiy * some other basic validation. This function is also responsible for building 17431e51764aSArtem Bityutskiy * an RB-tree of inodes - it adds all inodes into the RB-tree. It also 17441e51764aSArtem Bityutskiy * calculates reference count, size, etc for each inode in order to later 17451e51764aSArtem Bityutskiy * compare them to the information stored inside the inodes and detect possible 17461e51764aSArtem Bityutskiy * inconsistencies. Returns zero in case of success and a negative error code 17471e51764aSArtem Bityutskiy * in case of failure. 17481e51764aSArtem Bityutskiy */ 17491e51764aSArtem Bityutskiy static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr, 17501e51764aSArtem Bityutskiy void *priv) 17511e51764aSArtem Bityutskiy { 17521e51764aSArtem Bityutskiy ino_t inum; 17531e51764aSArtem Bityutskiy void *node; 17541e51764aSArtem Bityutskiy struct ubifs_ch *ch; 17551e51764aSArtem Bityutskiy int err, type = key_type(c, &zbr->key); 17561e51764aSArtem Bityutskiy struct fsck_inode *fscki; 17571e51764aSArtem Bityutskiy 17581e51764aSArtem Bityutskiy if (zbr->len < UBIFS_CH_SZ) { 17591e51764aSArtem Bityutskiy ubifs_err("bad leaf length %d (LEB %d:%d)", 17601e51764aSArtem Bityutskiy zbr->len, zbr->lnum, zbr->offs); 17611e51764aSArtem Bityutskiy return -EINVAL; 17621e51764aSArtem Bityutskiy } 17631e51764aSArtem Bityutskiy 17641e51764aSArtem Bityutskiy node = kmalloc(zbr->len, GFP_NOFS); 17651e51764aSArtem Bityutskiy if (!node) 17661e51764aSArtem Bityutskiy return -ENOMEM; 17671e51764aSArtem Bityutskiy 17681e51764aSArtem Bityutskiy err = ubifs_tnc_read_node(c, zbr, node); 17691e51764aSArtem Bityutskiy if (err) { 17701e51764aSArtem Bityutskiy ubifs_err("cannot read leaf node at LEB %d:%d, error %d", 17711e51764aSArtem Bityutskiy zbr->lnum, zbr->offs, err); 17721e51764aSArtem Bityutskiy goto out_free; 17731e51764aSArtem Bityutskiy } 17741e51764aSArtem Bityutskiy 17751e51764aSArtem Bityutskiy /* If this is an inode node, add it to RB-tree of inodes */ 17761e51764aSArtem Bityutskiy if (type == UBIFS_INO_KEY) { 17771e51764aSArtem Bityutskiy fscki = add_inode(c, priv, node); 17781e51764aSArtem Bityutskiy if (IS_ERR(fscki)) { 17791e51764aSArtem Bityutskiy err = PTR_ERR(fscki); 17801e51764aSArtem Bityutskiy ubifs_err("error %d while adding inode node", err); 17811e51764aSArtem Bityutskiy goto out_dump; 17821e51764aSArtem Bityutskiy } 17831e51764aSArtem Bityutskiy goto out; 17841e51764aSArtem Bityutskiy } 17851e51764aSArtem Bityutskiy 17861e51764aSArtem Bityutskiy if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY && 17871e51764aSArtem Bityutskiy type != UBIFS_DATA_KEY) { 17881e51764aSArtem Bityutskiy ubifs_err("unexpected node type %d at LEB %d:%d", 17891e51764aSArtem Bityutskiy type, zbr->lnum, zbr->offs); 17901e51764aSArtem Bityutskiy err = -EINVAL; 17911e51764aSArtem Bityutskiy goto out_free; 17921e51764aSArtem Bityutskiy } 17931e51764aSArtem Bityutskiy 17941e51764aSArtem Bityutskiy ch = node; 17951e51764aSArtem Bityutskiy if (le64_to_cpu(ch->sqnum) > c->max_sqnum) { 17961e51764aSArtem Bityutskiy ubifs_err("too high sequence number, max. is %llu", 17971e51764aSArtem Bityutskiy c->max_sqnum); 17981e51764aSArtem Bityutskiy err = -EINVAL; 17991e51764aSArtem Bityutskiy goto out_dump; 18001e51764aSArtem Bityutskiy } 18011e51764aSArtem Bityutskiy 18021e51764aSArtem Bityutskiy if (type == UBIFS_DATA_KEY) { 18031e51764aSArtem Bityutskiy long long blk_offs; 18041e51764aSArtem Bityutskiy struct ubifs_data_node *dn = node; 18051e51764aSArtem Bityutskiy 18061e51764aSArtem Bityutskiy /* 18071e51764aSArtem Bityutskiy * Search the inode node this data node belongs to and insert 18081e51764aSArtem Bityutskiy * it to the RB-tree of inodes. 18091e51764aSArtem Bityutskiy */ 18101e51764aSArtem Bityutskiy inum = key_inum_flash(c, &dn->key); 18111e51764aSArtem Bityutskiy fscki = read_add_inode(c, priv, inum); 18121e51764aSArtem Bityutskiy if (IS_ERR(fscki)) { 18131e51764aSArtem Bityutskiy err = PTR_ERR(fscki); 18141e51764aSArtem Bityutskiy ubifs_err("error %d while processing data node and " 1815e84461adSArtem Bityutskiy "trying to find inode node %lu", 1816e84461adSArtem Bityutskiy err, (unsigned long)inum); 18171e51764aSArtem Bityutskiy goto out_dump; 18181e51764aSArtem Bityutskiy } 18191e51764aSArtem Bityutskiy 18201e51764aSArtem Bityutskiy /* Make sure the data node is within inode size */ 18211e51764aSArtem Bityutskiy blk_offs = key_block_flash(c, &dn->key); 18221e51764aSArtem Bityutskiy blk_offs <<= UBIFS_BLOCK_SHIFT; 18231e51764aSArtem Bityutskiy blk_offs += le32_to_cpu(dn->size); 18241e51764aSArtem Bityutskiy if (blk_offs > fscki->size) { 18251e51764aSArtem Bityutskiy ubifs_err("data node at LEB %d:%d is not within inode " 18261e51764aSArtem Bityutskiy "size %lld", zbr->lnum, zbr->offs, 18271e51764aSArtem Bityutskiy fscki->size); 18281e51764aSArtem Bityutskiy err = -EINVAL; 18291e51764aSArtem Bityutskiy goto out_dump; 18301e51764aSArtem Bityutskiy } 18311e51764aSArtem Bityutskiy } else { 18321e51764aSArtem Bityutskiy int nlen; 18331e51764aSArtem Bityutskiy struct ubifs_dent_node *dent = node; 18341e51764aSArtem Bityutskiy struct fsck_inode *fscki1; 18351e51764aSArtem Bityutskiy 18361e51764aSArtem Bityutskiy err = ubifs_validate_entry(c, dent); 18371e51764aSArtem Bityutskiy if (err) 18381e51764aSArtem Bityutskiy goto out_dump; 18391e51764aSArtem Bityutskiy 18401e51764aSArtem Bityutskiy /* 18411e51764aSArtem Bityutskiy * Search the inode node this entry refers to and the parent 18421e51764aSArtem Bityutskiy * inode node and insert them to the RB-tree of inodes. 18431e51764aSArtem Bityutskiy */ 18441e51764aSArtem Bityutskiy inum = le64_to_cpu(dent->inum); 18451e51764aSArtem Bityutskiy fscki = read_add_inode(c, priv, inum); 18461e51764aSArtem Bityutskiy if (IS_ERR(fscki)) { 18471e51764aSArtem Bityutskiy err = PTR_ERR(fscki); 18481e51764aSArtem Bityutskiy ubifs_err("error %d while processing entry node and " 1849e84461adSArtem Bityutskiy "trying to find inode node %lu", 1850e84461adSArtem Bityutskiy err, (unsigned long)inum); 18511e51764aSArtem Bityutskiy goto out_dump; 18521e51764aSArtem Bityutskiy } 18531e51764aSArtem Bityutskiy 18541e51764aSArtem Bityutskiy /* Count how many direntries or xentries refers this inode */ 18551e51764aSArtem Bityutskiy fscki->references += 1; 18561e51764aSArtem Bityutskiy 18571e51764aSArtem Bityutskiy inum = key_inum_flash(c, &dent->key); 18581e51764aSArtem Bityutskiy fscki1 = read_add_inode(c, priv, inum); 18591e51764aSArtem Bityutskiy if (IS_ERR(fscki1)) { 18601e51764aSArtem Bityutskiy err = PTR_ERR(fscki); 18611e51764aSArtem Bityutskiy ubifs_err("error %d while processing entry node and " 18621e51764aSArtem Bityutskiy "trying to find parent inode node %lu", 1863e84461adSArtem Bityutskiy err, (unsigned long)inum); 18641e51764aSArtem Bityutskiy goto out_dump; 18651e51764aSArtem Bityutskiy } 18661e51764aSArtem Bityutskiy 18671e51764aSArtem Bityutskiy nlen = le16_to_cpu(dent->nlen); 18681e51764aSArtem Bityutskiy if (type == UBIFS_XENT_KEY) { 18691e51764aSArtem Bityutskiy fscki1->calc_xcnt += 1; 18701e51764aSArtem Bityutskiy fscki1->calc_xsz += CALC_DENT_SIZE(nlen); 18711e51764aSArtem Bityutskiy fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size); 18721e51764aSArtem Bityutskiy fscki1->calc_xnms += nlen; 18731e51764aSArtem Bityutskiy } else { 18741e51764aSArtem Bityutskiy fscki1->calc_sz += CALC_DENT_SIZE(nlen); 18751e51764aSArtem Bityutskiy if (dent->type == UBIFS_ITYPE_DIR) 18761e51764aSArtem Bityutskiy fscki1->calc_cnt += 1; 18771e51764aSArtem Bityutskiy } 18781e51764aSArtem Bityutskiy } 18791e51764aSArtem Bityutskiy 18801e51764aSArtem Bityutskiy out: 18811e51764aSArtem Bityutskiy kfree(node); 18821e51764aSArtem Bityutskiy return 0; 18831e51764aSArtem Bityutskiy 18841e51764aSArtem Bityutskiy out_dump: 18851e51764aSArtem Bityutskiy ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs); 18861e51764aSArtem Bityutskiy dbg_dump_node(c, node); 18871e51764aSArtem Bityutskiy out_free: 18881e51764aSArtem Bityutskiy kfree(node); 18891e51764aSArtem Bityutskiy return err; 18901e51764aSArtem Bityutskiy } 18911e51764aSArtem Bityutskiy 18921e51764aSArtem Bityutskiy /** 18931e51764aSArtem Bityutskiy * free_inodes - free RB-tree of inodes. 18941e51764aSArtem Bityutskiy * @fsckd: FS checking information 18951e51764aSArtem Bityutskiy */ 18961e51764aSArtem Bityutskiy static void free_inodes(struct fsck_data *fsckd) 18971e51764aSArtem Bityutskiy { 18981e51764aSArtem Bityutskiy struct rb_node *this = fsckd->inodes.rb_node; 18991e51764aSArtem Bityutskiy struct fsck_inode *fscki; 19001e51764aSArtem Bityutskiy 19011e51764aSArtem Bityutskiy while (this) { 19021e51764aSArtem Bityutskiy if (this->rb_left) 19031e51764aSArtem Bityutskiy this = this->rb_left; 19041e51764aSArtem Bityutskiy else if (this->rb_right) 19051e51764aSArtem Bityutskiy this = this->rb_right; 19061e51764aSArtem Bityutskiy else { 19071e51764aSArtem Bityutskiy fscki = rb_entry(this, struct fsck_inode, rb); 19081e51764aSArtem Bityutskiy this = rb_parent(this); 19091e51764aSArtem Bityutskiy if (this) { 19101e51764aSArtem Bityutskiy if (this->rb_left == &fscki->rb) 19111e51764aSArtem Bityutskiy this->rb_left = NULL; 19121e51764aSArtem Bityutskiy else 19131e51764aSArtem Bityutskiy this->rb_right = NULL; 19141e51764aSArtem Bityutskiy } 19151e51764aSArtem Bityutskiy kfree(fscki); 19161e51764aSArtem Bityutskiy } 19171e51764aSArtem Bityutskiy } 19181e51764aSArtem Bityutskiy } 19191e51764aSArtem Bityutskiy 19201e51764aSArtem Bityutskiy /** 19211e51764aSArtem Bityutskiy * check_inodes - checks all inodes. 19221e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 19231e51764aSArtem Bityutskiy * @fsckd: FS checking information 19241e51764aSArtem Bityutskiy * 19251e51764aSArtem Bityutskiy * This is a helper function for 'dbg_check_filesystem()' which walks the 19261e51764aSArtem Bityutskiy * RB-tree of inodes after the index scan has been finished, and checks that 19271e51764aSArtem Bityutskiy * inode nlink, size, etc are correct. Returns zero if inodes are fine, 19281e51764aSArtem Bityutskiy * %-EINVAL if not, and a negative error code in case of failure. 19291e51764aSArtem Bityutskiy */ 19301e51764aSArtem Bityutskiy static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd) 19311e51764aSArtem Bityutskiy { 19321e51764aSArtem Bityutskiy int n, err; 19331e51764aSArtem Bityutskiy union ubifs_key key; 19341e51764aSArtem Bityutskiy struct ubifs_znode *znode; 19351e51764aSArtem Bityutskiy struct ubifs_zbranch *zbr; 19361e51764aSArtem Bityutskiy struct ubifs_ino_node *ino; 19371e51764aSArtem Bityutskiy struct fsck_inode *fscki; 19381e51764aSArtem Bityutskiy struct rb_node *this = rb_first(&fsckd->inodes); 19391e51764aSArtem Bityutskiy 19401e51764aSArtem Bityutskiy while (this) { 19411e51764aSArtem Bityutskiy fscki = rb_entry(this, struct fsck_inode, rb); 19421e51764aSArtem Bityutskiy this = rb_next(this); 19431e51764aSArtem Bityutskiy 19441e51764aSArtem Bityutskiy if (S_ISDIR(fscki->mode)) { 19451e51764aSArtem Bityutskiy /* 19461e51764aSArtem Bityutskiy * Directories have to have exactly one reference (they 19471e51764aSArtem Bityutskiy * cannot have hardlinks), although root inode is an 19481e51764aSArtem Bityutskiy * exception. 19491e51764aSArtem Bityutskiy */ 19501e51764aSArtem Bityutskiy if (fscki->inum != UBIFS_ROOT_INO && 19511e51764aSArtem Bityutskiy fscki->references != 1) { 19521e51764aSArtem Bityutskiy ubifs_err("directory inode %lu has %d " 19531e51764aSArtem Bityutskiy "direntries which refer it, but " 1954e84461adSArtem Bityutskiy "should be 1", 1955e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 19561e51764aSArtem Bityutskiy fscki->references); 19571e51764aSArtem Bityutskiy goto out_dump; 19581e51764aSArtem Bityutskiy } 19591e51764aSArtem Bityutskiy if (fscki->inum == UBIFS_ROOT_INO && 19601e51764aSArtem Bityutskiy fscki->references != 0) { 19611e51764aSArtem Bityutskiy ubifs_err("root inode %lu has non-zero (%d) " 19621e51764aSArtem Bityutskiy "direntries which refer it", 1963e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 1964e84461adSArtem Bityutskiy fscki->references); 19651e51764aSArtem Bityutskiy goto out_dump; 19661e51764aSArtem Bityutskiy } 19671e51764aSArtem Bityutskiy if (fscki->calc_sz != fscki->size) { 19681e51764aSArtem Bityutskiy ubifs_err("directory inode %lu size is %lld, " 19691e51764aSArtem Bityutskiy "but calculated size is %lld", 1970e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 1971e84461adSArtem Bityutskiy fscki->size, fscki->calc_sz); 19721e51764aSArtem Bityutskiy goto out_dump; 19731e51764aSArtem Bityutskiy } 19741e51764aSArtem Bityutskiy if (fscki->calc_cnt != fscki->nlink) { 19751e51764aSArtem Bityutskiy ubifs_err("directory inode %lu nlink is %d, " 19761e51764aSArtem Bityutskiy "but calculated nlink is %d", 1977e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 1978e84461adSArtem Bityutskiy fscki->nlink, fscki->calc_cnt); 19791e51764aSArtem Bityutskiy goto out_dump; 19801e51764aSArtem Bityutskiy } 19811e51764aSArtem Bityutskiy } else { 19821e51764aSArtem Bityutskiy if (fscki->references != fscki->nlink) { 19831e51764aSArtem Bityutskiy ubifs_err("inode %lu nlink is %d, but " 1984e84461adSArtem Bityutskiy "calculated nlink is %d", 1985e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 19861e51764aSArtem Bityutskiy fscki->nlink, fscki->references); 19871e51764aSArtem Bityutskiy goto out_dump; 19881e51764aSArtem Bityutskiy } 19891e51764aSArtem Bityutskiy } 19901e51764aSArtem Bityutskiy if (fscki->xattr_sz != fscki->calc_xsz) { 19911e51764aSArtem Bityutskiy ubifs_err("inode %lu has xattr size %u, but " 19921e51764aSArtem Bityutskiy "calculated size is %lld", 1993e84461adSArtem Bityutskiy (unsigned long)fscki->inum, fscki->xattr_sz, 19941e51764aSArtem Bityutskiy fscki->calc_xsz); 19951e51764aSArtem Bityutskiy goto out_dump; 19961e51764aSArtem Bityutskiy } 19971e51764aSArtem Bityutskiy if (fscki->xattr_cnt != fscki->calc_xcnt) { 19981e51764aSArtem Bityutskiy ubifs_err("inode %lu has %u xattrs, but " 1999e84461adSArtem Bityutskiy "calculated count is %lld", 2000e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 20011e51764aSArtem Bityutskiy fscki->xattr_cnt, fscki->calc_xcnt); 20021e51764aSArtem Bityutskiy goto out_dump; 20031e51764aSArtem Bityutskiy } 20041e51764aSArtem Bityutskiy if (fscki->xattr_nms != fscki->calc_xnms) { 20051e51764aSArtem Bityutskiy ubifs_err("inode %lu has xattr names' size %u, but " 20061e51764aSArtem Bityutskiy "calculated names' size is %lld", 2007e84461adSArtem Bityutskiy (unsigned long)fscki->inum, fscki->xattr_nms, 20081e51764aSArtem Bityutskiy fscki->calc_xnms); 20091e51764aSArtem Bityutskiy goto out_dump; 20101e51764aSArtem Bityutskiy } 20111e51764aSArtem Bityutskiy } 20121e51764aSArtem Bityutskiy 20131e51764aSArtem Bityutskiy return 0; 20141e51764aSArtem Bityutskiy 20151e51764aSArtem Bityutskiy out_dump: 20161e51764aSArtem Bityutskiy /* Read the bad inode and dump it */ 20171e51764aSArtem Bityutskiy ino_key_init(c, &key, fscki->inum); 20181e51764aSArtem Bityutskiy err = ubifs_lookup_level0(c, &key, &znode, &n); 20191e51764aSArtem Bityutskiy if (!err) { 2020e84461adSArtem Bityutskiy ubifs_err("inode %lu not found in index", 2021e84461adSArtem Bityutskiy (unsigned long)fscki->inum); 20221e51764aSArtem Bityutskiy return -ENOENT; 20231e51764aSArtem Bityutskiy } else if (err < 0) { 20241e51764aSArtem Bityutskiy ubifs_err("error %d while looking up inode %lu", 2025e84461adSArtem Bityutskiy err, (unsigned long)fscki->inum); 20261e51764aSArtem Bityutskiy return err; 20271e51764aSArtem Bityutskiy } 20281e51764aSArtem Bityutskiy 20291e51764aSArtem Bityutskiy zbr = &znode->zbranch[n]; 20301e51764aSArtem Bityutskiy ino = kmalloc(zbr->len, GFP_NOFS); 20311e51764aSArtem Bityutskiy if (!ino) 20321e51764aSArtem Bityutskiy return -ENOMEM; 20331e51764aSArtem Bityutskiy 20341e51764aSArtem Bityutskiy err = ubifs_tnc_read_node(c, zbr, ino); 20351e51764aSArtem Bityutskiy if (err) { 20361e51764aSArtem Bityutskiy ubifs_err("cannot read inode node at LEB %d:%d, error %d", 20371e51764aSArtem Bityutskiy zbr->lnum, zbr->offs, err); 20381e51764aSArtem Bityutskiy kfree(ino); 20391e51764aSArtem Bityutskiy return err; 20401e51764aSArtem Bityutskiy } 20411e51764aSArtem Bityutskiy 20421e51764aSArtem Bityutskiy ubifs_msg("dump of the inode %lu sitting in LEB %d:%d", 2043e84461adSArtem Bityutskiy (unsigned long)fscki->inum, zbr->lnum, zbr->offs); 20441e51764aSArtem Bityutskiy dbg_dump_node(c, ino); 20451e51764aSArtem Bityutskiy kfree(ino); 20461e51764aSArtem Bityutskiy return -EINVAL; 20471e51764aSArtem Bityutskiy } 20481e51764aSArtem Bityutskiy 20491e51764aSArtem Bityutskiy /** 20501e51764aSArtem Bityutskiy * dbg_check_filesystem - check the file-system. 20511e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 20521e51764aSArtem Bityutskiy * 20531e51764aSArtem Bityutskiy * This function checks the file system, namely: 20541e51764aSArtem Bityutskiy * o makes sure that all leaf nodes exist and their CRCs are correct; 20551e51764aSArtem Bityutskiy * o makes sure inode nlink, size, xattr size/count are correct (for all 20561e51764aSArtem Bityutskiy * inodes). 20571e51764aSArtem Bityutskiy * 20581e51764aSArtem Bityutskiy * The function reads whole indexing tree and all nodes, so it is pretty 20591e51764aSArtem Bityutskiy * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if 20601e51764aSArtem Bityutskiy * not, and a negative error code in case of failure. 20611e51764aSArtem Bityutskiy */ 20621e51764aSArtem Bityutskiy int dbg_check_filesystem(struct ubifs_info *c) 20631e51764aSArtem Bityutskiy { 20641e51764aSArtem Bityutskiy int err; 20651e51764aSArtem Bityutskiy struct fsck_data fsckd; 20661e51764aSArtem Bityutskiy 20671e51764aSArtem Bityutskiy if (!(ubifs_chk_flags & UBIFS_CHK_FS)) 20681e51764aSArtem Bityutskiy return 0; 20691e51764aSArtem Bityutskiy 20701e51764aSArtem Bityutskiy fsckd.inodes = RB_ROOT; 20711e51764aSArtem Bityutskiy err = dbg_walk_index(c, check_leaf, NULL, &fsckd); 20721e51764aSArtem Bityutskiy if (err) 20731e51764aSArtem Bityutskiy goto out_free; 20741e51764aSArtem Bityutskiy 20751e51764aSArtem Bityutskiy err = check_inodes(c, &fsckd); 20761e51764aSArtem Bityutskiy if (err) 20771e51764aSArtem Bityutskiy goto out_free; 20781e51764aSArtem Bityutskiy 20791e51764aSArtem Bityutskiy free_inodes(&fsckd); 20801e51764aSArtem Bityutskiy return 0; 20811e51764aSArtem Bityutskiy 20821e51764aSArtem Bityutskiy out_free: 20831e51764aSArtem Bityutskiy ubifs_err("file-system check failed with error %d", err); 20841e51764aSArtem Bityutskiy dump_stack(); 20851e51764aSArtem Bityutskiy free_inodes(&fsckd); 20861e51764aSArtem Bityutskiy return err; 20871e51764aSArtem Bityutskiy } 20881e51764aSArtem Bityutskiy 20891e51764aSArtem Bityutskiy static int invocation_cnt; 20901e51764aSArtem Bityutskiy 20911e51764aSArtem Bityutskiy int dbg_force_in_the_gaps(void) 20921e51764aSArtem Bityutskiy { 20931e51764aSArtem Bityutskiy if (!dbg_force_in_the_gaps_enabled) 20941e51764aSArtem Bityutskiy return 0; 20951e51764aSArtem Bityutskiy /* Force in-the-gaps every 8th commit */ 20961e51764aSArtem Bityutskiy return !((invocation_cnt++) & 0x7); 20971e51764aSArtem Bityutskiy } 20981e51764aSArtem Bityutskiy 20991e51764aSArtem Bityutskiy /* Failure mode for recovery testing */ 21001e51764aSArtem Bityutskiy 21011e51764aSArtem Bityutskiy #define chance(n, d) (simple_rand() <= (n) * 32768LL / (d)) 21021e51764aSArtem Bityutskiy 21031e51764aSArtem Bityutskiy struct failure_mode_info { 21041e51764aSArtem Bityutskiy struct list_head list; 21051e51764aSArtem Bityutskiy struct ubifs_info *c; 21061e51764aSArtem Bityutskiy }; 21071e51764aSArtem Bityutskiy 21081e51764aSArtem Bityutskiy static LIST_HEAD(fmi_list); 21091e51764aSArtem Bityutskiy static DEFINE_SPINLOCK(fmi_lock); 21101e51764aSArtem Bityutskiy 21111e51764aSArtem Bityutskiy static unsigned int next; 21121e51764aSArtem Bityutskiy 21131e51764aSArtem Bityutskiy static int simple_rand(void) 21141e51764aSArtem Bityutskiy { 21151e51764aSArtem Bityutskiy if (next == 0) 21161e51764aSArtem Bityutskiy next = current->pid; 21171e51764aSArtem Bityutskiy next = next * 1103515245 + 12345; 21181e51764aSArtem Bityutskiy return (next >> 16) & 32767; 21191e51764aSArtem Bityutskiy } 21201e51764aSArtem Bityutskiy 212117c2f9f8SArtem Bityutskiy static void failure_mode_init(struct ubifs_info *c) 21221e51764aSArtem Bityutskiy { 21231e51764aSArtem Bityutskiy struct failure_mode_info *fmi; 21241e51764aSArtem Bityutskiy 21251e51764aSArtem Bityutskiy fmi = kmalloc(sizeof(struct failure_mode_info), GFP_NOFS); 21261e51764aSArtem Bityutskiy if (!fmi) { 2127552ff317SArtem Bityutskiy ubifs_err("Failed to register failure mode - no memory"); 21281e51764aSArtem Bityutskiy return; 21291e51764aSArtem Bityutskiy } 21301e51764aSArtem Bityutskiy fmi->c = c; 21311e51764aSArtem Bityutskiy spin_lock(&fmi_lock); 21321e51764aSArtem Bityutskiy list_add_tail(&fmi->list, &fmi_list); 21331e51764aSArtem Bityutskiy spin_unlock(&fmi_lock); 21341e51764aSArtem Bityutskiy } 21351e51764aSArtem Bityutskiy 213617c2f9f8SArtem Bityutskiy static void failure_mode_exit(struct ubifs_info *c) 21371e51764aSArtem Bityutskiy { 21381e51764aSArtem Bityutskiy struct failure_mode_info *fmi, *tmp; 21391e51764aSArtem Bityutskiy 21401e51764aSArtem Bityutskiy spin_lock(&fmi_lock); 21411e51764aSArtem Bityutskiy list_for_each_entry_safe(fmi, tmp, &fmi_list, list) 21421e51764aSArtem Bityutskiy if (fmi->c == c) { 21431e51764aSArtem Bityutskiy list_del(&fmi->list); 21441e51764aSArtem Bityutskiy kfree(fmi); 21451e51764aSArtem Bityutskiy } 21461e51764aSArtem Bityutskiy spin_unlock(&fmi_lock); 21471e51764aSArtem Bityutskiy } 21481e51764aSArtem Bityutskiy 21491e51764aSArtem Bityutskiy static struct ubifs_info *dbg_find_info(struct ubi_volume_desc *desc) 21501e51764aSArtem Bityutskiy { 21511e51764aSArtem Bityutskiy struct failure_mode_info *fmi; 21521e51764aSArtem Bityutskiy 21531e51764aSArtem Bityutskiy spin_lock(&fmi_lock); 21541e51764aSArtem Bityutskiy list_for_each_entry(fmi, &fmi_list, list) 21551e51764aSArtem Bityutskiy if (fmi->c->ubi == desc) { 21561e51764aSArtem Bityutskiy struct ubifs_info *c = fmi->c; 21571e51764aSArtem Bityutskiy 21581e51764aSArtem Bityutskiy spin_unlock(&fmi_lock); 21591e51764aSArtem Bityutskiy return c; 21601e51764aSArtem Bityutskiy } 21611e51764aSArtem Bityutskiy spin_unlock(&fmi_lock); 21621e51764aSArtem Bityutskiy return NULL; 21631e51764aSArtem Bityutskiy } 21641e51764aSArtem Bityutskiy 21651e51764aSArtem Bityutskiy static int in_failure_mode(struct ubi_volume_desc *desc) 21661e51764aSArtem Bityutskiy { 21671e51764aSArtem Bityutskiy struct ubifs_info *c = dbg_find_info(desc); 21681e51764aSArtem Bityutskiy 21691e51764aSArtem Bityutskiy if (c && dbg_failure_mode) 217017c2f9f8SArtem Bityutskiy return c->dbg->failure_mode; 21711e51764aSArtem Bityutskiy return 0; 21721e51764aSArtem Bityutskiy } 21731e51764aSArtem Bityutskiy 21741e51764aSArtem Bityutskiy static int do_fail(struct ubi_volume_desc *desc, int lnum, int write) 21751e51764aSArtem Bityutskiy { 21761e51764aSArtem Bityutskiy struct ubifs_info *c = dbg_find_info(desc); 217717c2f9f8SArtem Bityutskiy struct ubifs_debug_info *d; 21781e51764aSArtem Bityutskiy 21791e51764aSArtem Bityutskiy if (!c || !dbg_failure_mode) 21801e51764aSArtem Bityutskiy return 0; 218117c2f9f8SArtem Bityutskiy d = c->dbg; 218217c2f9f8SArtem Bityutskiy if (d->failure_mode) 21831e51764aSArtem Bityutskiy return 1; 218417c2f9f8SArtem Bityutskiy if (!d->fail_cnt) { 21851e51764aSArtem Bityutskiy /* First call - decide delay to failure */ 21861e51764aSArtem Bityutskiy if (chance(1, 2)) { 21871e51764aSArtem Bityutskiy unsigned int delay = 1 << (simple_rand() >> 11); 21881e51764aSArtem Bityutskiy 21891e51764aSArtem Bityutskiy if (chance(1, 2)) { 219017c2f9f8SArtem Bityutskiy d->fail_delay = 1; 219117c2f9f8SArtem Bityutskiy d->fail_timeout = jiffies + 21921e51764aSArtem Bityutskiy msecs_to_jiffies(delay); 21931e51764aSArtem Bityutskiy dbg_rcvry("failing after %ums", delay); 21941e51764aSArtem Bityutskiy } else { 219517c2f9f8SArtem Bityutskiy d->fail_delay = 2; 219617c2f9f8SArtem Bityutskiy d->fail_cnt_max = delay; 21971e51764aSArtem Bityutskiy dbg_rcvry("failing after %u calls", delay); 21981e51764aSArtem Bityutskiy } 21991e51764aSArtem Bityutskiy } 220017c2f9f8SArtem Bityutskiy d->fail_cnt += 1; 22011e51764aSArtem Bityutskiy } 22021e51764aSArtem Bityutskiy /* Determine if failure delay has expired */ 220317c2f9f8SArtem Bityutskiy if (d->fail_delay == 1) { 220417c2f9f8SArtem Bityutskiy if (time_before(jiffies, d->fail_timeout)) 22051e51764aSArtem Bityutskiy return 0; 220617c2f9f8SArtem Bityutskiy } else if (d->fail_delay == 2) 220717c2f9f8SArtem Bityutskiy if (d->fail_cnt++ < d->fail_cnt_max) 22081e51764aSArtem Bityutskiy return 0; 22091e51764aSArtem Bityutskiy if (lnum == UBIFS_SB_LNUM) { 22101e51764aSArtem Bityutskiy if (write) { 22111e51764aSArtem Bityutskiy if (chance(1, 2)) 22121e51764aSArtem Bityutskiy return 0; 22131e51764aSArtem Bityutskiy } else if (chance(19, 20)) 22141e51764aSArtem Bityutskiy return 0; 22151e51764aSArtem Bityutskiy dbg_rcvry("failing in super block LEB %d", lnum); 22161e51764aSArtem Bityutskiy } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) { 22171e51764aSArtem Bityutskiy if (chance(19, 20)) 22181e51764aSArtem Bityutskiy return 0; 22191e51764aSArtem Bityutskiy dbg_rcvry("failing in master LEB %d", lnum); 22201e51764aSArtem Bityutskiy } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) { 22211e51764aSArtem Bityutskiy if (write) { 22221e51764aSArtem Bityutskiy if (chance(99, 100)) 22231e51764aSArtem Bityutskiy return 0; 22241e51764aSArtem Bityutskiy } else if (chance(399, 400)) 22251e51764aSArtem Bityutskiy return 0; 22261e51764aSArtem Bityutskiy dbg_rcvry("failing in log LEB %d", lnum); 22271e51764aSArtem Bityutskiy } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) { 22281e51764aSArtem Bityutskiy if (write) { 22291e51764aSArtem Bityutskiy if (chance(7, 8)) 22301e51764aSArtem Bityutskiy return 0; 22311e51764aSArtem Bityutskiy } else if (chance(19, 20)) 22321e51764aSArtem Bityutskiy return 0; 22331e51764aSArtem Bityutskiy dbg_rcvry("failing in LPT LEB %d", lnum); 22341e51764aSArtem Bityutskiy } else if (lnum >= c->orph_first && lnum <= c->orph_last) { 22351e51764aSArtem Bityutskiy if (write) { 22361e51764aSArtem Bityutskiy if (chance(1, 2)) 22371e51764aSArtem Bityutskiy return 0; 22381e51764aSArtem Bityutskiy } else if (chance(9, 10)) 22391e51764aSArtem Bityutskiy return 0; 22401e51764aSArtem Bityutskiy dbg_rcvry("failing in orphan LEB %d", lnum); 22411e51764aSArtem Bityutskiy } else if (lnum == c->ihead_lnum) { 22421e51764aSArtem Bityutskiy if (chance(99, 100)) 22431e51764aSArtem Bityutskiy return 0; 22441e51764aSArtem Bityutskiy dbg_rcvry("failing in index head LEB %d", lnum); 22451e51764aSArtem Bityutskiy } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) { 22461e51764aSArtem Bityutskiy if (chance(9, 10)) 22471e51764aSArtem Bityutskiy return 0; 22481e51764aSArtem Bityutskiy dbg_rcvry("failing in GC head LEB %d", lnum); 22491e51764aSArtem Bityutskiy } else if (write && !RB_EMPTY_ROOT(&c->buds) && 22501e51764aSArtem Bityutskiy !ubifs_search_bud(c, lnum)) { 22511e51764aSArtem Bityutskiy if (chance(19, 20)) 22521e51764aSArtem Bityutskiy return 0; 22531e51764aSArtem Bityutskiy dbg_rcvry("failing in non-bud LEB %d", lnum); 22541e51764aSArtem Bityutskiy } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND || 22551e51764aSArtem Bityutskiy c->cmt_state == COMMIT_RUNNING_REQUIRED) { 22561e51764aSArtem Bityutskiy if (chance(999, 1000)) 22571e51764aSArtem Bityutskiy return 0; 22581e51764aSArtem Bityutskiy dbg_rcvry("failing in bud LEB %d commit running", lnum); 22591e51764aSArtem Bityutskiy } else { 22601e51764aSArtem Bityutskiy if (chance(9999, 10000)) 22611e51764aSArtem Bityutskiy return 0; 22621e51764aSArtem Bityutskiy dbg_rcvry("failing in bud LEB %d commit not running", lnum); 22631e51764aSArtem Bityutskiy } 22641e51764aSArtem Bityutskiy ubifs_err("*** SETTING FAILURE MODE ON (LEB %d) ***", lnum); 226517c2f9f8SArtem Bityutskiy d->failure_mode = 1; 22661e51764aSArtem Bityutskiy dump_stack(); 22671e51764aSArtem Bityutskiy return 1; 22681e51764aSArtem Bityutskiy } 22691e51764aSArtem Bityutskiy 22701e51764aSArtem Bityutskiy static void cut_data(const void *buf, int len) 22711e51764aSArtem Bityutskiy { 22721e51764aSArtem Bityutskiy int flen, i; 22731e51764aSArtem Bityutskiy unsigned char *p = (void *)buf; 22741e51764aSArtem Bityutskiy 22751e51764aSArtem Bityutskiy flen = (len * (long long)simple_rand()) >> 15; 22761e51764aSArtem Bityutskiy for (i = flen; i < len; i++) 22771e51764aSArtem Bityutskiy p[i] = 0xff; 22781e51764aSArtem Bityutskiy } 22791e51764aSArtem Bityutskiy 22801e51764aSArtem Bityutskiy int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset, 22811e51764aSArtem Bityutskiy int len, int check) 22821e51764aSArtem Bityutskiy { 22831e51764aSArtem Bityutskiy if (in_failure_mode(desc)) 22841e51764aSArtem Bityutskiy return -EIO; 22851e51764aSArtem Bityutskiy return ubi_leb_read(desc, lnum, buf, offset, len, check); 22861e51764aSArtem Bityutskiy } 22871e51764aSArtem Bityutskiy 22881e51764aSArtem Bityutskiy int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf, 22891e51764aSArtem Bityutskiy int offset, int len, int dtype) 22901e51764aSArtem Bityutskiy { 229116dfd804SAdrian Hunter int err, failing; 22921e51764aSArtem Bityutskiy 22931e51764aSArtem Bityutskiy if (in_failure_mode(desc)) 22941e51764aSArtem Bityutskiy return -EIO; 229516dfd804SAdrian Hunter failing = do_fail(desc, lnum, 1); 229616dfd804SAdrian Hunter if (failing) 22971e51764aSArtem Bityutskiy cut_data(buf, len); 22981e51764aSArtem Bityutskiy err = ubi_leb_write(desc, lnum, buf, offset, len, dtype); 22991e51764aSArtem Bityutskiy if (err) 23001e51764aSArtem Bityutskiy return err; 230116dfd804SAdrian Hunter if (failing) 23021e51764aSArtem Bityutskiy return -EIO; 23031e51764aSArtem Bityutskiy return 0; 23041e51764aSArtem Bityutskiy } 23051e51764aSArtem Bityutskiy 23061e51764aSArtem Bityutskiy int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf, 23071e51764aSArtem Bityutskiy int len, int dtype) 23081e51764aSArtem Bityutskiy { 23091e51764aSArtem Bityutskiy int err; 23101e51764aSArtem Bityutskiy 23111e51764aSArtem Bityutskiy if (do_fail(desc, lnum, 1)) 23121e51764aSArtem Bityutskiy return -EIO; 23131e51764aSArtem Bityutskiy err = ubi_leb_change(desc, lnum, buf, len, dtype); 23141e51764aSArtem Bityutskiy if (err) 23151e51764aSArtem Bityutskiy return err; 23161e51764aSArtem Bityutskiy if (do_fail(desc, lnum, 1)) 23171e51764aSArtem Bityutskiy return -EIO; 23181e51764aSArtem Bityutskiy return 0; 23191e51764aSArtem Bityutskiy } 23201e51764aSArtem Bityutskiy 23211e51764aSArtem Bityutskiy int dbg_leb_erase(struct ubi_volume_desc *desc, int lnum) 23221e51764aSArtem Bityutskiy { 23231e51764aSArtem Bityutskiy int err; 23241e51764aSArtem Bityutskiy 23251e51764aSArtem Bityutskiy if (do_fail(desc, lnum, 0)) 23261e51764aSArtem Bityutskiy return -EIO; 23271e51764aSArtem Bityutskiy err = ubi_leb_erase(desc, lnum); 23281e51764aSArtem Bityutskiy if (err) 23291e51764aSArtem Bityutskiy return err; 23301e51764aSArtem Bityutskiy if (do_fail(desc, lnum, 0)) 23311e51764aSArtem Bityutskiy return -EIO; 23321e51764aSArtem Bityutskiy return 0; 23331e51764aSArtem Bityutskiy } 23341e51764aSArtem Bityutskiy 23351e51764aSArtem Bityutskiy int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum) 23361e51764aSArtem Bityutskiy { 23371e51764aSArtem Bityutskiy int err; 23381e51764aSArtem Bityutskiy 23391e51764aSArtem Bityutskiy if (do_fail(desc, lnum, 0)) 23401e51764aSArtem Bityutskiy return -EIO; 23411e51764aSArtem Bityutskiy err = ubi_leb_unmap(desc, lnum); 23421e51764aSArtem Bityutskiy if (err) 23431e51764aSArtem Bityutskiy return err; 23441e51764aSArtem Bityutskiy if (do_fail(desc, lnum, 0)) 23451e51764aSArtem Bityutskiy return -EIO; 23461e51764aSArtem Bityutskiy return 0; 23471e51764aSArtem Bityutskiy } 23481e51764aSArtem Bityutskiy 23491e51764aSArtem Bityutskiy int dbg_is_mapped(struct ubi_volume_desc *desc, int lnum) 23501e51764aSArtem Bityutskiy { 23511e51764aSArtem Bityutskiy if (in_failure_mode(desc)) 23521e51764aSArtem Bityutskiy return -EIO; 23531e51764aSArtem Bityutskiy return ubi_is_mapped(desc, lnum); 23541e51764aSArtem Bityutskiy } 23551e51764aSArtem Bityutskiy 23561e51764aSArtem Bityutskiy int dbg_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype) 23571e51764aSArtem Bityutskiy { 23581e51764aSArtem Bityutskiy int err; 23591e51764aSArtem Bityutskiy 23601e51764aSArtem Bityutskiy if (do_fail(desc, lnum, 0)) 23611e51764aSArtem Bityutskiy return -EIO; 23621e51764aSArtem Bityutskiy err = ubi_leb_map(desc, lnum, dtype); 23631e51764aSArtem Bityutskiy if (err) 23641e51764aSArtem Bityutskiy return err; 23651e51764aSArtem Bityutskiy if (do_fail(desc, lnum, 0)) 23661e51764aSArtem Bityutskiy return -EIO; 23671e51764aSArtem Bityutskiy return 0; 23681e51764aSArtem Bityutskiy } 23691e51764aSArtem Bityutskiy 237017c2f9f8SArtem Bityutskiy /** 237117c2f9f8SArtem Bityutskiy * ubifs_debugging_init - initialize UBIFS debugging. 237217c2f9f8SArtem Bityutskiy * @c: UBIFS file-system description object 237317c2f9f8SArtem Bityutskiy * 237417c2f9f8SArtem Bityutskiy * This function initializes debugging-related data for the file system. 237517c2f9f8SArtem Bityutskiy * Returns zero in case of success and a negative error code in case of 237617c2f9f8SArtem Bityutskiy * failure. 237717c2f9f8SArtem Bityutskiy */ 237817c2f9f8SArtem Bityutskiy int ubifs_debugging_init(struct ubifs_info *c) 237917c2f9f8SArtem Bityutskiy { 238017c2f9f8SArtem Bityutskiy c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL); 238117c2f9f8SArtem Bityutskiy if (!c->dbg) 238217c2f9f8SArtem Bityutskiy return -ENOMEM; 238317c2f9f8SArtem Bityutskiy 238417c2f9f8SArtem Bityutskiy c->dbg->buf = vmalloc(c->leb_size); 238517c2f9f8SArtem Bityutskiy if (!c->dbg->buf) 238617c2f9f8SArtem Bityutskiy goto out; 238717c2f9f8SArtem Bityutskiy 238817c2f9f8SArtem Bityutskiy failure_mode_init(c); 238917c2f9f8SArtem Bityutskiy return 0; 239017c2f9f8SArtem Bityutskiy 239117c2f9f8SArtem Bityutskiy out: 239217c2f9f8SArtem Bityutskiy kfree(c->dbg); 239317c2f9f8SArtem Bityutskiy return -ENOMEM; 239417c2f9f8SArtem Bityutskiy } 239517c2f9f8SArtem Bityutskiy 239617c2f9f8SArtem Bityutskiy /** 239717c2f9f8SArtem Bityutskiy * ubifs_debugging_exit - free debugging data. 239817c2f9f8SArtem Bityutskiy * @c: UBIFS file-system description object 239917c2f9f8SArtem Bityutskiy */ 240017c2f9f8SArtem Bityutskiy void ubifs_debugging_exit(struct ubifs_info *c) 240117c2f9f8SArtem Bityutskiy { 240217c2f9f8SArtem Bityutskiy failure_mode_exit(c); 240317c2f9f8SArtem Bityutskiy vfree(c->dbg->buf); 240417c2f9f8SArtem Bityutskiy kfree(c->dbg); 240517c2f9f8SArtem Bityutskiy } 240617c2f9f8SArtem Bityutskiy 2407552ff317SArtem Bityutskiy /* 2408552ff317SArtem Bityutskiy * Root directory for UBIFS stuff in debugfs. Contains sub-directories which 2409552ff317SArtem Bityutskiy * contain the stuff specific to particular file-system mounts. 2410552ff317SArtem Bityutskiy */ 2411552ff317SArtem Bityutskiy static struct dentry *debugfs_rootdir; 2412552ff317SArtem Bityutskiy 2413552ff317SArtem Bityutskiy /** 2414552ff317SArtem Bityutskiy * dbg_debugfs_init - initialize debugfs file-system. 2415552ff317SArtem Bityutskiy * 2416552ff317SArtem Bityutskiy * UBIFS uses debugfs file-system to expose various debugging knobs to 2417552ff317SArtem Bityutskiy * user-space. This function creates "ubifs" directory in the debugfs 2418552ff317SArtem Bityutskiy * file-system. Returns zero in case of success and a negative error code in 2419552ff317SArtem Bityutskiy * case of failure. 2420552ff317SArtem Bityutskiy */ 2421552ff317SArtem Bityutskiy int dbg_debugfs_init(void) 2422552ff317SArtem Bityutskiy { 2423552ff317SArtem Bityutskiy debugfs_rootdir = debugfs_create_dir("ubifs", NULL); 2424552ff317SArtem Bityutskiy if (IS_ERR(debugfs_rootdir)) { 2425552ff317SArtem Bityutskiy int err = PTR_ERR(debugfs_rootdir); 2426552ff317SArtem Bityutskiy ubifs_err("cannot create \"ubifs\" debugfs directory, " 2427552ff317SArtem Bityutskiy "error %d\n", err); 2428552ff317SArtem Bityutskiy return err; 2429552ff317SArtem Bityutskiy } 2430552ff317SArtem Bityutskiy 2431552ff317SArtem Bityutskiy return 0; 2432552ff317SArtem Bityutskiy } 2433552ff317SArtem Bityutskiy 2434552ff317SArtem Bityutskiy /** 2435552ff317SArtem Bityutskiy * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system. 2436552ff317SArtem Bityutskiy */ 2437552ff317SArtem Bityutskiy void dbg_debugfs_exit(void) 2438552ff317SArtem Bityutskiy { 2439552ff317SArtem Bityutskiy debugfs_remove(debugfs_rootdir); 2440552ff317SArtem Bityutskiy } 2441552ff317SArtem Bityutskiy 2442552ff317SArtem Bityutskiy static int open_debugfs_file(struct inode *inode, struct file *file) 2443552ff317SArtem Bityutskiy { 2444552ff317SArtem Bityutskiy file->private_data = inode->i_private; 2445552ff317SArtem Bityutskiy return 0; 2446552ff317SArtem Bityutskiy } 2447552ff317SArtem Bityutskiy 2448552ff317SArtem Bityutskiy static ssize_t write_debugfs_file(struct file *file, const char __user *buf, 2449552ff317SArtem Bityutskiy size_t count, loff_t *ppos) 2450552ff317SArtem Bityutskiy { 2451552ff317SArtem Bityutskiy struct ubifs_info *c = file->private_data; 2452552ff317SArtem Bityutskiy struct ubifs_debug_info *d = c->dbg; 2453552ff317SArtem Bityutskiy 2454552ff317SArtem Bityutskiy if (file->f_path.dentry == d->dump_lprops) 2455552ff317SArtem Bityutskiy dbg_dump_lprops(c); 2456552ff317SArtem Bityutskiy else if (file->f_path.dentry == d->dump_budg) { 2457552ff317SArtem Bityutskiy spin_lock(&c->space_lock); 2458552ff317SArtem Bityutskiy dbg_dump_budg(c); 2459552ff317SArtem Bityutskiy spin_unlock(&c->space_lock); 246024fa9e94SArtem Bityutskiy } else if (file->f_path.dentry == d->dump_tnc) { 2461552ff317SArtem Bityutskiy mutex_lock(&c->tnc_mutex); 2462552ff317SArtem Bityutskiy dbg_dump_tnc(c); 2463552ff317SArtem Bityutskiy mutex_unlock(&c->tnc_mutex); 2464552ff317SArtem Bityutskiy } else 2465552ff317SArtem Bityutskiy return -EINVAL; 2466552ff317SArtem Bityutskiy 2467552ff317SArtem Bityutskiy *ppos += count; 2468552ff317SArtem Bityutskiy return count; 2469552ff317SArtem Bityutskiy } 2470552ff317SArtem Bityutskiy 2471552ff317SArtem Bityutskiy static const struct file_operations debugfs_fops = { 2472552ff317SArtem Bityutskiy .open = open_debugfs_file, 2473552ff317SArtem Bityutskiy .write = write_debugfs_file, 2474552ff317SArtem Bityutskiy .owner = THIS_MODULE, 2475552ff317SArtem Bityutskiy }; 2476552ff317SArtem Bityutskiy 2477552ff317SArtem Bityutskiy /** 2478552ff317SArtem Bityutskiy * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance. 2479552ff317SArtem Bityutskiy * @c: UBIFS file-system description object 2480552ff317SArtem Bityutskiy * 2481552ff317SArtem Bityutskiy * This function creates all debugfs files for this instance of UBIFS. Returns 2482552ff317SArtem Bityutskiy * zero in case of success and a negative error code in case of failure. 2483552ff317SArtem Bityutskiy * 2484552ff317SArtem Bityutskiy * Note, the only reason we have not merged this function with the 2485552ff317SArtem Bityutskiy * 'ubifs_debugging_init()' function is because it is better to initialize 2486552ff317SArtem Bityutskiy * debugfs interfaces at the very end of the mount process, and remove them at 2487552ff317SArtem Bityutskiy * the very beginning of the mount process. 2488552ff317SArtem Bityutskiy */ 2489552ff317SArtem Bityutskiy int dbg_debugfs_init_fs(struct ubifs_info *c) 2490552ff317SArtem Bityutskiy { 2491552ff317SArtem Bityutskiy int err; 2492552ff317SArtem Bityutskiy const char *fname; 2493552ff317SArtem Bityutskiy struct dentry *dent; 2494552ff317SArtem Bityutskiy struct ubifs_debug_info *d = c->dbg; 2495552ff317SArtem Bityutskiy 2496552ff317SArtem Bityutskiy sprintf(d->debugfs_dir_name, "ubi%d_%d", c->vi.ubi_num, c->vi.vol_id); 2497552ff317SArtem Bityutskiy d->debugfs_dir = debugfs_create_dir(d->debugfs_dir_name, 2498552ff317SArtem Bityutskiy debugfs_rootdir); 2499552ff317SArtem Bityutskiy if (IS_ERR(d->debugfs_dir)) { 2500552ff317SArtem Bityutskiy err = PTR_ERR(d->debugfs_dir); 2501552ff317SArtem Bityutskiy ubifs_err("cannot create \"%s\" debugfs directory, error %d\n", 2502552ff317SArtem Bityutskiy d->debugfs_dir_name, err); 2503552ff317SArtem Bityutskiy goto out; 2504552ff317SArtem Bityutskiy } 2505552ff317SArtem Bityutskiy 2506552ff317SArtem Bityutskiy fname = "dump_lprops"; 2507552ff317SArtem Bityutskiy dent = debugfs_create_file(fname, S_IWUGO, d->debugfs_dir, c, 2508552ff317SArtem Bityutskiy &debugfs_fops); 2509552ff317SArtem Bityutskiy if (IS_ERR(dent)) 2510552ff317SArtem Bityutskiy goto out_remove; 2511552ff317SArtem Bityutskiy d->dump_lprops = dent; 2512552ff317SArtem Bityutskiy 2513552ff317SArtem Bityutskiy fname = "dump_budg"; 2514552ff317SArtem Bityutskiy dent = debugfs_create_file(fname, S_IWUGO, d->debugfs_dir, c, 2515552ff317SArtem Bityutskiy &debugfs_fops); 2516552ff317SArtem Bityutskiy if (IS_ERR(dent)) 2517552ff317SArtem Bityutskiy goto out_remove; 2518552ff317SArtem Bityutskiy d->dump_budg = dent; 2519552ff317SArtem Bityutskiy 2520552ff317SArtem Bityutskiy fname = "dump_tnc"; 2521552ff317SArtem Bityutskiy dent = debugfs_create_file(fname, S_IWUGO, d->debugfs_dir, c, 2522552ff317SArtem Bityutskiy &debugfs_fops); 2523552ff317SArtem Bityutskiy if (IS_ERR(dent)) 2524552ff317SArtem Bityutskiy goto out_remove; 2525552ff317SArtem Bityutskiy d->dump_tnc = dent; 2526552ff317SArtem Bityutskiy 2527552ff317SArtem Bityutskiy return 0; 2528552ff317SArtem Bityutskiy 2529552ff317SArtem Bityutskiy out_remove: 2530552ff317SArtem Bityutskiy err = PTR_ERR(dent); 2531552ff317SArtem Bityutskiy ubifs_err("cannot create \"%s\" debugfs directory, error %d\n", 2532552ff317SArtem Bityutskiy fname, err); 2533552ff317SArtem Bityutskiy debugfs_remove_recursive(d->debugfs_dir); 2534552ff317SArtem Bityutskiy out: 2535552ff317SArtem Bityutskiy return err; 2536552ff317SArtem Bityutskiy } 2537552ff317SArtem Bityutskiy 2538552ff317SArtem Bityutskiy /** 2539552ff317SArtem Bityutskiy * dbg_debugfs_exit_fs - remove all debugfs files. 2540552ff317SArtem Bityutskiy * @c: UBIFS file-system description object 2541552ff317SArtem Bityutskiy */ 2542552ff317SArtem Bityutskiy void dbg_debugfs_exit_fs(struct ubifs_info *c) 2543552ff317SArtem Bityutskiy { 2544552ff317SArtem Bityutskiy debugfs_remove_recursive(c->dbg->debugfs_dir); 2545552ff317SArtem Bityutskiy } 2546552ff317SArtem Bityutskiy 25471e51764aSArtem Bityutskiy #endif /* CONFIG_UBIFS_FS_DEBUG */ 2548