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> 364d61db4fSArtem Bityutskiy #include <linux/math64.h> 375a0e3ad6STejun Heo #include <linux/slab.h> 381e51764aSArtem Bityutskiy 391e51764aSArtem Bityutskiy #ifdef CONFIG_UBIFS_FS_DEBUG 401e51764aSArtem Bityutskiy 411e51764aSArtem Bityutskiy DEFINE_SPINLOCK(dbg_lock); 421e51764aSArtem Bityutskiy 431e51764aSArtem Bityutskiy static char dbg_key_buf0[128]; 441e51764aSArtem Bityutskiy static char dbg_key_buf1[128]; 451e51764aSArtem Bityutskiy 46cce3f612SArtem Bityutskiy unsigned int ubifs_msg_flags; 47cce3f612SArtem Bityutskiy unsigned int ubifs_chk_flags; 481e51764aSArtem Bityutskiy unsigned int ubifs_tst_flags; 491e51764aSArtem Bityutskiy 501e51764aSArtem Bityutskiy module_param_named(debug_msgs, ubifs_msg_flags, uint, S_IRUGO | S_IWUSR); 511e51764aSArtem Bityutskiy module_param_named(debug_chks, ubifs_chk_flags, uint, S_IRUGO | S_IWUSR); 521e51764aSArtem Bityutskiy module_param_named(debug_tsts, ubifs_tst_flags, uint, S_IRUGO | S_IWUSR); 531e51764aSArtem Bityutskiy 541e51764aSArtem Bityutskiy MODULE_PARM_DESC(debug_msgs, "Debug message type flags"); 551e51764aSArtem Bityutskiy MODULE_PARM_DESC(debug_chks, "Debug check flags"); 561e51764aSArtem Bityutskiy MODULE_PARM_DESC(debug_tsts, "Debug special test flags"); 571e51764aSArtem Bityutskiy 581e51764aSArtem Bityutskiy static const char *get_key_fmt(int fmt) 591e51764aSArtem Bityutskiy { 601e51764aSArtem Bityutskiy switch (fmt) { 611e51764aSArtem Bityutskiy case UBIFS_SIMPLE_KEY_FMT: 621e51764aSArtem Bityutskiy return "simple"; 631e51764aSArtem Bityutskiy default: 641e51764aSArtem Bityutskiy return "unknown/invalid format"; 651e51764aSArtem Bityutskiy } 661e51764aSArtem Bityutskiy } 671e51764aSArtem Bityutskiy 681e51764aSArtem Bityutskiy static const char *get_key_hash(int hash) 691e51764aSArtem Bityutskiy { 701e51764aSArtem Bityutskiy switch (hash) { 711e51764aSArtem Bityutskiy case UBIFS_KEY_HASH_R5: 721e51764aSArtem Bityutskiy return "R5"; 731e51764aSArtem Bityutskiy case UBIFS_KEY_HASH_TEST: 741e51764aSArtem Bityutskiy return "test"; 751e51764aSArtem Bityutskiy default: 761e51764aSArtem Bityutskiy return "unknown/invalid name hash"; 771e51764aSArtem Bityutskiy } 781e51764aSArtem Bityutskiy } 791e51764aSArtem Bityutskiy 801e51764aSArtem Bityutskiy static const char *get_key_type(int type) 811e51764aSArtem Bityutskiy { 821e51764aSArtem Bityutskiy switch (type) { 831e51764aSArtem Bityutskiy case UBIFS_INO_KEY: 841e51764aSArtem Bityutskiy return "inode"; 851e51764aSArtem Bityutskiy case UBIFS_DENT_KEY: 861e51764aSArtem Bityutskiy return "direntry"; 871e51764aSArtem Bityutskiy case UBIFS_XENT_KEY: 881e51764aSArtem Bityutskiy return "xentry"; 891e51764aSArtem Bityutskiy case UBIFS_DATA_KEY: 901e51764aSArtem Bityutskiy return "data"; 911e51764aSArtem Bityutskiy case UBIFS_TRUN_KEY: 921e51764aSArtem Bityutskiy return "truncate"; 931e51764aSArtem Bityutskiy default: 941e51764aSArtem Bityutskiy return "unknown/invalid key"; 951e51764aSArtem Bityutskiy } 961e51764aSArtem Bityutskiy } 971e51764aSArtem Bityutskiy 981e51764aSArtem Bityutskiy static void sprintf_key(const struct ubifs_info *c, const union ubifs_key *key, 991e51764aSArtem Bityutskiy char *buffer) 1001e51764aSArtem Bityutskiy { 1011e51764aSArtem Bityutskiy char *p = buffer; 1021e51764aSArtem Bityutskiy int type = key_type(c, key); 1031e51764aSArtem Bityutskiy 1041e51764aSArtem Bityutskiy if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) { 1051e51764aSArtem Bityutskiy switch (type) { 1061e51764aSArtem Bityutskiy case UBIFS_INO_KEY: 107e84461adSArtem Bityutskiy sprintf(p, "(%lu, %s)", (unsigned long)key_inum(c, key), 1081e51764aSArtem Bityutskiy get_key_type(type)); 1091e51764aSArtem Bityutskiy break; 1101e51764aSArtem Bityutskiy case UBIFS_DENT_KEY: 1111e51764aSArtem Bityutskiy case UBIFS_XENT_KEY: 112e84461adSArtem Bityutskiy sprintf(p, "(%lu, %s, %#08x)", 113e84461adSArtem Bityutskiy (unsigned long)key_inum(c, key), 1141e51764aSArtem Bityutskiy get_key_type(type), key_hash(c, key)); 1151e51764aSArtem Bityutskiy break; 1161e51764aSArtem Bityutskiy case UBIFS_DATA_KEY: 117e84461adSArtem Bityutskiy sprintf(p, "(%lu, %s, %u)", 118e84461adSArtem Bityutskiy (unsigned long)key_inum(c, key), 1191e51764aSArtem Bityutskiy get_key_type(type), key_block(c, key)); 1201e51764aSArtem Bityutskiy break; 1211e51764aSArtem Bityutskiy case UBIFS_TRUN_KEY: 1221e51764aSArtem Bityutskiy sprintf(p, "(%lu, %s)", 123e84461adSArtem Bityutskiy (unsigned long)key_inum(c, key), 124e84461adSArtem Bityutskiy get_key_type(type)); 1251e51764aSArtem Bityutskiy break; 1261e51764aSArtem Bityutskiy default: 1271e51764aSArtem Bityutskiy sprintf(p, "(bad key type: %#08x, %#08x)", 1281e51764aSArtem Bityutskiy key->u32[0], key->u32[1]); 1291e51764aSArtem Bityutskiy } 1301e51764aSArtem Bityutskiy } else 1311e51764aSArtem Bityutskiy sprintf(p, "bad key format %d", c->key_fmt); 1321e51764aSArtem Bityutskiy } 1331e51764aSArtem Bityutskiy 1341e51764aSArtem Bityutskiy const char *dbg_key_str0(const struct ubifs_info *c, const union ubifs_key *key) 1351e51764aSArtem Bityutskiy { 1361e51764aSArtem Bityutskiy /* dbg_lock must be held */ 1371e51764aSArtem Bityutskiy sprintf_key(c, key, dbg_key_buf0); 1381e51764aSArtem Bityutskiy return dbg_key_buf0; 1391e51764aSArtem Bityutskiy } 1401e51764aSArtem Bityutskiy 1411e51764aSArtem Bityutskiy const char *dbg_key_str1(const struct ubifs_info *c, const union ubifs_key *key) 1421e51764aSArtem Bityutskiy { 1431e51764aSArtem Bityutskiy /* dbg_lock must be held */ 1441e51764aSArtem Bityutskiy sprintf_key(c, key, dbg_key_buf1); 1451e51764aSArtem Bityutskiy return dbg_key_buf1; 1461e51764aSArtem Bityutskiy } 1471e51764aSArtem Bityutskiy 1481e51764aSArtem Bityutskiy const char *dbg_ntype(int type) 1491e51764aSArtem Bityutskiy { 1501e51764aSArtem Bityutskiy switch (type) { 1511e51764aSArtem Bityutskiy case UBIFS_PAD_NODE: 1521e51764aSArtem Bityutskiy return "padding node"; 1531e51764aSArtem Bityutskiy case UBIFS_SB_NODE: 1541e51764aSArtem Bityutskiy return "superblock node"; 1551e51764aSArtem Bityutskiy case UBIFS_MST_NODE: 1561e51764aSArtem Bityutskiy return "master node"; 1571e51764aSArtem Bityutskiy case UBIFS_REF_NODE: 1581e51764aSArtem Bityutskiy return "reference node"; 1591e51764aSArtem Bityutskiy case UBIFS_INO_NODE: 1601e51764aSArtem Bityutskiy return "inode node"; 1611e51764aSArtem Bityutskiy case UBIFS_DENT_NODE: 1621e51764aSArtem Bityutskiy return "direntry node"; 1631e51764aSArtem Bityutskiy case UBIFS_XENT_NODE: 1641e51764aSArtem Bityutskiy return "xentry node"; 1651e51764aSArtem Bityutskiy case UBIFS_DATA_NODE: 1661e51764aSArtem Bityutskiy return "data node"; 1671e51764aSArtem Bityutskiy case UBIFS_TRUN_NODE: 1681e51764aSArtem Bityutskiy return "truncate node"; 1691e51764aSArtem Bityutskiy case UBIFS_IDX_NODE: 1701e51764aSArtem Bityutskiy return "indexing node"; 1711e51764aSArtem Bityutskiy case UBIFS_CS_NODE: 1721e51764aSArtem Bityutskiy return "commit start node"; 1731e51764aSArtem Bityutskiy case UBIFS_ORPH_NODE: 1741e51764aSArtem Bityutskiy return "orphan node"; 1751e51764aSArtem Bityutskiy default: 1761e51764aSArtem Bityutskiy return "unknown node"; 1771e51764aSArtem Bityutskiy } 1781e51764aSArtem Bityutskiy } 1791e51764aSArtem Bityutskiy 1801e51764aSArtem Bityutskiy static const char *dbg_gtype(int type) 1811e51764aSArtem Bityutskiy { 1821e51764aSArtem Bityutskiy switch (type) { 1831e51764aSArtem Bityutskiy case UBIFS_NO_NODE_GROUP: 1841e51764aSArtem Bityutskiy return "no node group"; 1851e51764aSArtem Bityutskiy case UBIFS_IN_NODE_GROUP: 1861e51764aSArtem Bityutskiy return "in node group"; 1871e51764aSArtem Bityutskiy case UBIFS_LAST_OF_NODE_GROUP: 1881e51764aSArtem Bityutskiy return "last of node group"; 1891e51764aSArtem Bityutskiy default: 1901e51764aSArtem Bityutskiy return "unknown"; 1911e51764aSArtem Bityutskiy } 1921e51764aSArtem Bityutskiy } 1931e51764aSArtem Bityutskiy 1941e51764aSArtem Bityutskiy const char *dbg_cstate(int cmt_state) 1951e51764aSArtem Bityutskiy { 1961e51764aSArtem Bityutskiy switch (cmt_state) { 1971e51764aSArtem Bityutskiy case COMMIT_RESTING: 1981e51764aSArtem Bityutskiy return "commit resting"; 1991e51764aSArtem Bityutskiy case COMMIT_BACKGROUND: 2001e51764aSArtem Bityutskiy return "background commit requested"; 2011e51764aSArtem Bityutskiy case COMMIT_REQUIRED: 2021e51764aSArtem Bityutskiy return "commit required"; 2031e51764aSArtem Bityutskiy case COMMIT_RUNNING_BACKGROUND: 2041e51764aSArtem Bityutskiy return "BACKGROUND commit running"; 2051e51764aSArtem Bityutskiy case COMMIT_RUNNING_REQUIRED: 2061e51764aSArtem Bityutskiy return "commit running and required"; 2071e51764aSArtem Bityutskiy case COMMIT_BROKEN: 2081e51764aSArtem Bityutskiy return "broken commit"; 2091e51764aSArtem Bityutskiy default: 2101e51764aSArtem Bityutskiy return "unknown commit state"; 2111e51764aSArtem Bityutskiy } 2121e51764aSArtem Bityutskiy } 2131e51764aSArtem Bityutskiy 21477a7ae58SArtem Bityutskiy const char *dbg_jhead(int jhead) 21577a7ae58SArtem Bityutskiy { 21677a7ae58SArtem Bityutskiy switch (jhead) { 21777a7ae58SArtem Bityutskiy case GCHD: 21877a7ae58SArtem Bityutskiy return "0 (GC)"; 21977a7ae58SArtem Bityutskiy case BASEHD: 22077a7ae58SArtem Bityutskiy return "1 (base)"; 22177a7ae58SArtem Bityutskiy case DATAHD: 22277a7ae58SArtem Bityutskiy return "2 (data)"; 22377a7ae58SArtem Bityutskiy default: 22477a7ae58SArtem Bityutskiy return "unknown journal head"; 22577a7ae58SArtem Bityutskiy } 22677a7ae58SArtem Bityutskiy } 22777a7ae58SArtem Bityutskiy 2281e51764aSArtem Bityutskiy static void dump_ch(const struct ubifs_ch *ch) 2291e51764aSArtem Bityutskiy { 2301e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tmagic %#x\n", le32_to_cpu(ch->magic)); 2311e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tcrc %#x\n", le32_to_cpu(ch->crc)); 2321e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tnode_type %d (%s)\n", ch->node_type, 2331e51764aSArtem Bityutskiy dbg_ntype(ch->node_type)); 2341e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tgroup_type %d (%s)\n", ch->group_type, 2351e51764aSArtem Bityutskiy dbg_gtype(ch->group_type)); 2361e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tsqnum %llu\n", 2371e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(ch->sqnum)); 2381e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlen %u\n", le32_to_cpu(ch->len)); 2391e51764aSArtem Bityutskiy } 2401e51764aSArtem Bityutskiy 2411e51764aSArtem Bityutskiy void dbg_dump_inode(const struct ubifs_info *c, const struct inode *inode) 2421e51764aSArtem Bityutskiy { 2431e51764aSArtem Bityutskiy const struct ubifs_inode *ui = ubifs_inode(inode); 2441e51764aSArtem Bityutskiy 245b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "Dump in-memory inode:"); 246b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tinode %lu\n", inode->i_ino); 247b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tsize %llu\n", 2481e51764aSArtem Bityutskiy (unsigned long long)i_size_read(inode)); 249b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tnlink %u\n", inode->i_nlink); 250b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tuid %u\n", (unsigned int)inode->i_uid); 251b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tgid %u\n", (unsigned int)inode->i_gid); 252b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tatime %u.%u\n", 2531e51764aSArtem Bityutskiy (unsigned int)inode->i_atime.tv_sec, 2541e51764aSArtem Bityutskiy (unsigned int)inode->i_atime.tv_nsec); 255b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tmtime %u.%u\n", 2561e51764aSArtem Bityutskiy (unsigned int)inode->i_mtime.tv_sec, 2571e51764aSArtem Bityutskiy (unsigned int)inode->i_mtime.tv_nsec); 258b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tctime %u.%u\n", 2591e51764aSArtem Bityutskiy (unsigned int)inode->i_ctime.tv_sec, 2601e51764aSArtem Bityutskiy (unsigned int)inode->i_ctime.tv_nsec); 261b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tcreat_sqnum %llu\n", ui->creat_sqnum); 262b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\txattr_size %u\n", ui->xattr_size); 263b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\txattr_cnt %u\n", ui->xattr_cnt); 264b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\txattr_names %u\n", ui->xattr_names); 265b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tdirty %u\n", ui->dirty); 266b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\txattr %u\n", ui->xattr); 267b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tbulk_read %u\n", ui->xattr); 268b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tsynced_i_size %llu\n", 269b5e426e9SArtem Bityutskiy (unsigned long long)ui->synced_i_size); 270b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tui_size %llu\n", 271b5e426e9SArtem Bityutskiy (unsigned long long)ui->ui_size); 272b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tflags %d\n", ui->flags); 273b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tcompr_type %d\n", ui->compr_type); 274b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tlast_page_read %lu\n", ui->last_page_read); 275b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tread_in_a_row %lu\n", ui->read_in_a_row); 276b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tdata_len %d\n", ui->data_len); 2771e51764aSArtem Bityutskiy } 2781e51764aSArtem Bityutskiy 2791e51764aSArtem Bityutskiy void dbg_dump_node(const struct ubifs_info *c, const void *node) 2801e51764aSArtem Bityutskiy { 2811e51764aSArtem Bityutskiy int i, n; 2821e51764aSArtem Bityutskiy union ubifs_key key; 2831e51764aSArtem Bityutskiy const struct ubifs_ch *ch = node; 2841e51764aSArtem Bityutskiy 2851e51764aSArtem Bityutskiy if (dbg_failure_mode) 2861e51764aSArtem Bityutskiy return; 2871e51764aSArtem Bityutskiy 2881e51764aSArtem Bityutskiy /* If the magic is incorrect, just hexdump the first bytes */ 2891e51764aSArtem Bityutskiy if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) { 2901e51764aSArtem Bityutskiy printk(KERN_DEBUG "Not a node, first %zu bytes:", UBIFS_CH_SZ); 2911e51764aSArtem Bityutskiy print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, 2921e51764aSArtem Bityutskiy (void *)node, UBIFS_CH_SZ, 1); 2931e51764aSArtem Bityutskiy return; 2941e51764aSArtem Bityutskiy } 2951e51764aSArtem Bityutskiy 2961e51764aSArtem Bityutskiy spin_lock(&dbg_lock); 2971e51764aSArtem Bityutskiy dump_ch(node); 2981e51764aSArtem Bityutskiy 2991e51764aSArtem Bityutskiy switch (ch->node_type) { 3001e51764aSArtem Bityutskiy case UBIFS_PAD_NODE: 3011e51764aSArtem Bityutskiy { 3021e51764aSArtem Bityutskiy const struct ubifs_pad_node *pad = node; 3031e51764aSArtem Bityutskiy 3041e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tpad_len %u\n", 3051e51764aSArtem Bityutskiy le32_to_cpu(pad->pad_len)); 3061e51764aSArtem Bityutskiy break; 3071e51764aSArtem Bityutskiy } 3081e51764aSArtem Bityutskiy case UBIFS_SB_NODE: 3091e51764aSArtem Bityutskiy { 3101e51764aSArtem Bityutskiy const struct ubifs_sb_node *sup = node; 3111e51764aSArtem Bityutskiy unsigned int sup_flags = le32_to_cpu(sup->flags); 3121e51764aSArtem Bityutskiy 3131e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tkey_hash %d (%s)\n", 3141e51764aSArtem Bityutskiy (int)sup->key_hash, get_key_hash(sup->key_hash)); 3151e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tkey_fmt %d (%s)\n", 3161e51764aSArtem Bityutskiy (int)sup->key_fmt, get_key_fmt(sup->key_fmt)); 3171e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tflags %#x\n", sup_flags); 3181e51764aSArtem Bityutskiy printk(KERN_DEBUG "\t big_lpt %u\n", 3191e51764aSArtem Bityutskiy !!(sup_flags & UBIFS_FLG_BIGLPT)); 3201e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tmin_io_size %u\n", 3211e51764aSArtem Bityutskiy le32_to_cpu(sup->min_io_size)); 3221e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tleb_size %u\n", 3231e51764aSArtem Bityutskiy le32_to_cpu(sup->leb_size)); 3241e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tleb_cnt %u\n", 3251e51764aSArtem Bityutskiy le32_to_cpu(sup->leb_cnt)); 3261e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tmax_leb_cnt %u\n", 3271e51764aSArtem Bityutskiy le32_to_cpu(sup->max_leb_cnt)); 3281e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tmax_bud_bytes %llu\n", 3291e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(sup->max_bud_bytes)); 3301e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlog_lebs %u\n", 3311e51764aSArtem Bityutskiy le32_to_cpu(sup->log_lebs)); 3321e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlpt_lebs %u\n", 3331e51764aSArtem Bityutskiy le32_to_cpu(sup->lpt_lebs)); 3341e51764aSArtem Bityutskiy printk(KERN_DEBUG "\torph_lebs %u\n", 3351e51764aSArtem Bityutskiy le32_to_cpu(sup->orph_lebs)); 3361e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tjhead_cnt %u\n", 3371e51764aSArtem Bityutskiy le32_to_cpu(sup->jhead_cnt)); 3381e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tfanout %u\n", 3391e51764aSArtem Bityutskiy le32_to_cpu(sup->fanout)); 3401e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlsave_cnt %u\n", 3411e51764aSArtem Bityutskiy le32_to_cpu(sup->lsave_cnt)); 3421e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tdefault_compr %u\n", 3431e51764aSArtem Bityutskiy (int)le16_to_cpu(sup->default_compr)); 3441e51764aSArtem Bityutskiy printk(KERN_DEBUG "\trp_size %llu\n", 3451e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(sup->rp_size)); 3461e51764aSArtem Bityutskiy printk(KERN_DEBUG "\trp_uid %u\n", 3471e51764aSArtem Bityutskiy le32_to_cpu(sup->rp_uid)); 3481e51764aSArtem Bityutskiy printk(KERN_DEBUG "\trp_gid %u\n", 3491e51764aSArtem Bityutskiy le32_to_cpu(sup->rp_gid)); 3501e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tfmt_version %u\n", 3511e51764aSArtem Bityutskiy le32_to_cpu(sup->fmt_version)); 3521e51764aSArtem Bityutskiy printk(KERN_DEBUG "\ttime_gran %u\n", 3531e51764aSArtem Bityutskiy le32_to_cpu(sup->time_gran)); 3547f2f4e72SJoe Perches printk(KERN_DEBUG "\tUUID %pUB\n", 3557f2f4e72SJoe Perches sup->uuid); 3561e51764aSArtem Bityutskiy break; 3571e51764aSArtem Bityutskiy } 3581e51764aSArtem Bityutskiy case UBIFS_MST_NODE: 3591e51764aSArtem Bityutskiy { 3601e51764aSArtem Bityutskiy const struct ubifs_mst_node *mst = node; 3611e51764aSArtem Bityutskiy 3621e51764aSArtem Bityutskiy printk(KERN_DEBUG "\thighest_inum %llu\n", 3631e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->highest_inum)); 3641e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tcommit number %llu\n", 3651e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->cmt_no)); 3661e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tflags %#x\n", 3671e51764aSArtem Bityutskiy le32_to_cpu(mst->flags)); 3681e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlog_lnum %u\n", 3691e51764aSArtem Bityutskiy le32_to_cpu(mst->log_lnum)); 3701e51764aSArtem Bityutskiy printk(KERN_DEBUG "\troot_lnum %u\n", 3711e51764aSArtem Bityutskiy le32_to_cpu(mst->root_lnum)); 3721e51764aSArtem Bityutskiy printk(KERN_DEBUG "\troot_offs %u\n", 3731e51764aSArtem Bityutskiy le32_to_cpu(mst->root_offs)); 3741e51764aSArtem Bityutskiy printk(KERN_DEBUG "\troot_len %u\n", 3751e51764aSArtem Bityutskiy le32_to_cpu(mst->root_len)); 3761e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tgc_lnum %u\n", 3771e51764aSArtem Bityutskiy le32_to_cpu(mst->gc_lnum)); 3781e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tihead_lnum %u\n", 3791e51764aSArtem Bityutskiy le32_to_cpu(mst->ihead_lnum)); 3801e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tihead_offs %u\n", 3811e51764aSArtem Bityutskiy le32_to_cpu(mst->ihead_offs)); 3820ecb9529SHarvey Harrison printk(KERN_DEBUG "\tindex_size %llu\n", 3830ecb9529SHarvey Harrison (unsigned long long)le64_to_cpu(mst->index_size)); 3841e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlpt_lnum %u\n", 3851e51764aSArtem Bityutskiy le32_to_cpu(mst->lpt_lnum)); 3861e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlpt_offs %u\n", 3871e51764aSArtem Bityutskiy le32_to_cpu(mst->lpt_offs)); 3881e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tnhead_lnum %u\n", 3891e51764aSArtem Bityutskiy le32_to_cpu(mst->nhead_lnum)); 3901e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tnhead_offs %u\n", 3911e51764aSArtem Bityutskiy le32_to_cpu(mst->nhead_offs)); 3921e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tltab_lnum %u\n", 3931e51764aSArtem Bityutskiy le32_to_cpu(mst->ltab_lnum)); 3941e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tltab_offs %u\n", 3951e51764aSArtem Bityutskiy le32_to_cpu(mst->ltab_offs)); 3961e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlsave_lnum %u\n", 3971e51764aSArtem Bityutskiy le32_to_cpu(mst->lsave_lnum)); 3981e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlsave_offs %u\n", 3991e51764aSArtem Bityutskiy le32_to_cpu(mst->lsave_offs)); 4001e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlscan_lnum %u\n", 4011e51764aSArtem Bityutskiy le32_to_cpu(mst->lscan_lnum)); 4021e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tleb_cnt %u\n", 4031e51764aSArtem Bityutskiy le32_to_cpu(mst->leb_cnt)); 4041e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tempty_lebs %u\n", 4051e51764aSArtem Bityutskiy le32_to_cpu(mst->empty_lebs)); 4061e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tidx_lebs %u\n", 4071e51764aSArtem Bityutskiy le32_to_cpu(mst->idx_lebs)); 4081e51764aSArtem Bityutskiy printk(KERN_DEBUG "\ttotal_free %llu\n", 4091e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->total_free)); 4101e51764aSArtem Bityutskiy printk(KERN_DEBUG "\ttotal_dirty %llu\n", 4111e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->total_dirty)); 4121e51764aSArtem Bityutskiy printk(KERN_DEBUG "\ttotal_used %llu\n", 4131e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->total_used)); 4141e51764aSArtem Bityutskiy printk(KERN_DEBUG "\ttotal_dead %llu\n", 4151e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->total_dead)); 4161e51764aSArtem Bityutskiy printk(KERN_DEBUG "\ttotal_dark %llu\n", 4171e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->total_dark)); 4181e51764aSArtem Bityutskiy break; 4191e51764aSArtem Bityutskiy } 4201e51764aSArtem Bityutskiy case UBIFS_REF_NODE: 4211e51764aSArtem Bityutskiy { 4221e51764aSArtem Bityutskiy const struct ubifs_ref_node *ref = node; 4231e51764aSArtem Bityutskiy 4241e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlnum %u\n", 4251e51764aSArtem Bityutskiy le32_to_cpu(ref->lnum)); 4261e51764aSArtem Bityutskiy printk(KERN_DEBUG "\toffs %u\n", 4271e51764aSArtem Bityutskiy le32_to_cpu(ref->offs)); 4281e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tjhead %u\n", 4291e51764aSArtem Bityutskiy le32_to_cpu(ref->jhead)); 4301e51764aSArtem Bityutskiy break; 4311e51764aSArtem Bityutskiy } 4321e51764aSArtem Bityutskiy case UBIFS_INO_NODE: 4331e51764aSArtem Bityutskiy { 4341e51764aSArtem Bityutskiy const struct ubifs_ino_node *ino = node; 4351e51764aSArtem Bityutskiy 4361e51764aSArtem Bityutskiy key_read(c, &ino->key, &key); 4371e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key)); 4381e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tcreat_sqnum %llu\n", 4391e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(ino->creat_sqnum)); 4401e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tsize %llu\n", 4411e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(ino->size)); 4421e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tnlink %u\n", 4431e51764aSArtem Bityutskiy le32_to_cpu(ino->nlink)); 4441e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tatime %lld.%u\n", 4451e51764aSArtem Bityutskiy (long long)le64_to_cpu(ino->atime_sec), 4461e51764aSArtem Bityutskiy le32_to_cpu(ino->atime_nsec)); 4471e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tmtime %lld.%u\n", 4481e51764aSArtem Bityutskiy (long long)le64_to_cpu(ino->mtime_sec), 4491e51764aSArtem Bityutskiy le32_to_cpu(ino->mtime_nsec)); 4501e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tctime %lld.%u\n", 4511e51764aSArtem Bityutskiy (long long)le64_to_cpu(ino->ctime_sec), 4521e51764aSArtem Bityutskiy le32_to_cpu(ino->ctime_nsec)); 4531e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tuid %u\n", 4541e51764aSArtem Bityutskiy le32_to_cpu(ino->uid)); 4551e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tgid %u\n", 4561e51764aSArtem Bityutskiy le32_to_cpu(ino->gid)); 4571e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tmode %u\n", 4581e51764aSArtem Bityutskiy le32_to_cpu(ino->mode)); 4591e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tflags %#x\n", 4601e51764aSArtem Bityutskiy le32_to_cpu(ino->flags)); 4611e51764aSArtem Bityutskiy printk(KERN_DEBUG "\txattr_cnt %u\n", 4621e51764aSArtem Bityutskiy le32_to_cpu(ino->xattr_cnt)); 4631e51764aSArtem Bityutskiy printk(KERN_DEBUG "\txattr_size %u\n", 4641e51764aSArtem Bityutskiy le32_to_cpu(ino->xattr_size)); 4651e51764aSArtem Bityutskiy printk(KERN_DEBUG "\txattr_names %u\n", 4661e51764aSArtem Bityutskiy le32_to_cpu(ino->xattr_names)); 4671e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tcompr_type %#x\n", 4681e51764aSArtem Bityutskiy (int)le16_to_cpu(ino->compr_type)); 4691e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tdata len %u\n", 4701e51764aSArtem Bityutskiy le32_to_cpu(ino->data_len)); 4711e51764aSArtem Bityutskiy break; 4721e51764aSArtem Bityutskiy } 4731e51764aSArtem Bityutskiy case UBIFS_DENT_NODE: 4741e51764aSArtem Bityutskiy case UBIFS_XENT_NODE: 4751e51764aSArtem Bityutskiy { 4761e51764aSArtem Bityutskiy const struct ubifs_dent_node *dent = node; 4771e51764aSArtem Bityutskiy int nlen = le16_to_cpu(dent->nlen); 4781e51764aSArtem Bityutskiy 4791e51764aSArtem Bityutskiy key_read(c, &dent->key, &key); 4801e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key)); 4811e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tinum %llu\n", 4821e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(dent->inum)); 4831e51764aSArtem Bityutskiy printk(KERN_DEBUG "\ttype %d\n", (int)dent->type); 4841e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tnlen %d\n", nlen); 4851e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tname "); 4861e51764aSArtem Bityutskiy 4871e51764aSArtem Bityutskiy if (nlen > UBIFS_MAX_NLEN) 4881e51764aSArtem Bityutskiy printk(KERN_DEBUG "(bad name length, not printing, " 4891e51764aSArtem Bityutskiy "bad or corrupted node)"); 4901e51764aSArtem Bityutskiy else { 4911e51764aSArtem Bityutskiy for (i = 0; i < nlen && dent->name[i]; i++) 492c9927c3eSArtem Bityutskiy printk(KERN_CONT "%c", dent->name[i]); 4931e51764aSArtem Bityutskiy } 494c9927c3eSArtem Bityutskiy printk(KERN_CONT "\n"); 4951e51764aSArtem Bityutskiy 4961e51764aSArtem Bityutskiy break; 4971e51764aSArtem Bityutskiy } 4981e51764aSArtem Bityutskiy case UBIFS_DATA_NODE: 4991e51764aSArtem Bityutskiy { 5001e51764aSArtem Bityutskiy const struct ubifs_data_node *dn = node; 5011e51764aSArtem Bityutskiy int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ; 5021e51764aSArtem Bityutskiy 5031e51764aSArtem Bityutskiy key_read(c, &dn->key, &key); 5041e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key)); 5051e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tsize %u\n", 5061e51764aSArtem Bityutskiy le32_to_cpu(dn->size)); 5071e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tcompr_typ %d\n", 5081e51764aSArtem Bityutskiy (int)le16_to_cpu(dn->compr_type)); 5091e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tdata size %d\n", 5101e51764aSArtem Bityutskiy dlen); 5111e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tdata:\n"); 5121e51764aSArtem Bityutskiy print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET, 32, 1, 5131e51764aSArtem Bityutskiy (void *)&dn->data, dlen, 0); 5141e51764aSArtem Bityutskiy break; 5151e51764aSArtem Bityutskiy } 5161e51764aSArtem Bityutskiy case UBIFS_TRUN_NODE: 5171e51764aSArtem Bityutskiy { 5181e51764aSArtem Bityutskiy const struct ubifs_trun_node *trun = node; 5191e51764aSArtem Bityutskiy 5201e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tinum %u\n", 5211e51764aSArtem Bityutskiy le32_to_cpu(trun->inum)); 5221e51764aSArtem Bityutskiy printk(KERN_DEBUG "\told_size %llu\n", 5231e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(trun->old_size)); 5241e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tnew_size %llu\n", 5251e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(trun->new_size)); 5261e51764aSArtem Bityutskiy break; 5271e51764aSArtem Bityutskiy } 5281e51764aSArtem Bityutskiy case UBIFS_IDX_NODE: 5291e51764aSArtem Bityutskiy { 5301e51764aSArtem Bityutskiy const struct ubifs_idx_node *idx = node; 5311e51764aSArtem Bityutskiy 5321e51764aSArtem Bityutskiy n = le16_to_cpu(idx->child_cnt); 5331e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tchild_cnt %d\n", n); 5341e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlevel %d\n", 5351e51764aSArtem Bityutskiy (int)le16_to_cpu(idx->level)); 5361e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tBranches:\n"); 5371e51764aSArtem Bityutskiy 5381e51764aSArtem Bityutskiy for (i = 0; i < n && i < c->fanout - 1; i++) { 5391e51764aSArtem Bityutskiy const struct ubifs_branch *br; 5401e51764aSArtem Bityutskiy 5411e51764aSArtem Bityutskiy br = ubifs_idx_branch(c, idx, i); 5421e51764aSArtem Bityutskiy key_read(c, &br->key, &key); 5431e51764aSArtem Bityutskiy printk(KERN_DEBUG "\t%d: LEB %d:%d len %d key %s\n", 5441e51764aSArtem Bityutskiy i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs), 5451e51764aSArtem Bityutskiy le32_to_cpu(br->len), DBGKEY(&key)); 5461e51764aSArtem Bityutskiy } 5471e51764aSArtem Bityutskiy break; 5481e51764aSArtem Bityutskiy } 5491e51764aSArtem Bityutskiy case UBIFS_CS_NODE: 5501e51764aSArtem Bityutskiy break; 5511e51764aSArtem Bityutskiy case UBIFS_ORPH_NODE: 5521e51764aSArtem Bityutskiy { 5531e51764aSArtem Bityutskiy const struct ubifs_orph_node *orph = node; 5541e51764aSArtem Bityutskiy 5551e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tcommit number %llu\n", 5561e51764aSArtem Bityutskiy (unsigned long long) 5571e51764aSArtem Bityutskiy le64_to_cpu(orph->cmt_no) & LLONG_MAX); 5581e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlast node flag %llu\n", 5591e51764aSArtem Bityutskiy (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63); 5601e51764aSArtem Bityutskiy n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3; 5611e51764aSArtem Bityutskiy printk(KERN_DEBUG "\t%d orphan inode numbers:\n", n); 5621e51764aSArtem Bityutskiy for (i = 0; i < n; i++) 5631e51764aSArtem Bityutskiy printk(KERN_DEBUG "\t ino %llu\n", 5647424bac8SAlexander Beregalov (unsigned long long)le64_to_cpu(orph->inos[i])); 5651e51764aSArtem Bityutskiy break; 5661e51764aSArtem Bityutskiy } 5671e51764aSArtem Bityutskiy default: 5681e51764aSArtem Bityutskiy printk(KERN_DEBUG "node type %d was not recognized\n", 5691e51764aSArtem Bityutskiy (int)ch->node_type); 5701e51764aSArtem Bityutskiy } 5711e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 5721e51764aSArtem Bityutskiy } 5731e51764aSArtem Bityutskiy 5741e51764aSArtem Bityutskiy void dbg_dump_budget_req(const struct ubifs_budget_req *req) 5751e51764aSArtem Bityutskiy { 5761e51764aSArtem Bityutskiy spin_lock(&dbg_lock); 5771e51764aSArtem Bityutskiy printk(KERN_DEBUG "Budgeting request: new_ino %d, dirtied_ino %d\n", 5781e51764aSArtem Bityutskiy req->new_ino, req->dirtied_ino); 5791e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tnew_ino_d %d, dirtied_ino_d %d\n", 5801e51764aSArtem Bityutskiy req->new_ino_d, req->dirtied_ino_d); 5811e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tnew_page %d, dirtied_page %d\n", 5821e51764aSArtem Bityutskiy req->new_page, req->dirtied_page); 5831e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tnew_dent %d, mod_dent %d\n", 5841e51764aSArtem Bityutskiy req->new_dent, req->mod_dent); 5851e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tidx_growth %d\n", req->idx_growth); 5861e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tdata_growth %d dd_growth %d\n", 5871e51764aSArtem Bityutskiy req->data_growth, req->dd_growth); 5881e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 5891e51764aSArtem Bityutskiy } 5901e51764aSArtem Bityutskiy 5911e51764aSArtem Bityutskiy void dbg_dump_lstats(const struct ubifs_lp_stats *lst) 5921e51764aSArtem Bityutskiy { 5931e51764aSArtem Bityutskiy spin_lock(&dbg_lock); 5941de94159SArtem Bityutskiy printk(KERN_DEBUG "(pid %d) Lprops statistics: empty_lebs %d, " 5951de94159SArtem Bityutskiy "idx_lebs %d\n", current->pid, lst->empty_lebs, lst->idx_lebs); 5961e51764aSArtem Bityutskiy printk(KERN_DEBUG "\ttaken_empty_lebs %d, total_free %lld, " 5971e51764aSArtem Bityutskiy "total_dirty %lld\n", lst->taken_empty_lebs, lst->total_free, 5981e51764aSArtem Bityutskiy lst->total_dirty); 5991e51764aSArtem Bityutskiy printk(KERN_DEBUG "\ttotal_used %lld, total_dark %lld, " 6001e51764aSArtem Bityutskiy "total_dead %lld\n", lst->total_used, lst->total_dark, 6011e51764aSArtem Bityutskiy lst->total_dead); 6021e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 6031e51764aSArtem Bityutskiy } 6041e51764aSArtem Bityutskiy 6051e51764aSArtem Bityutskiy void dbg_dump_budg(struct ubifs_info *c) 6061e51764aSArtem Bityutskiy { 6071e51764aSArtem Bityutskiy int i; 6081e51764aSArtem Bityutskiy struct rb_node *rb; 6091e51764aSArtem Bityutskiy struct ubifs_bud *bud; 6101e51764aSArtem Bityutskiy struct ubifs_gced_idx_leb *idx_gc; 61121a60258SArtem Bityutskiy long long available, outstanding, free; 6121e51764aSArtem Bityutskiy 61321a60258SArtem Bityutskiy ubifs_assert(spin_is_locked(&c->space_lock)); 6141e51764aSArtem Bityutskiy spin_lock(&dbg_lock); 6151de94159SArtem Bityutskiy printk(KERN_DEBUG "(pid %d) Budgeting info: budg_data_growth %lld, " 6161de94159SArtem Bityutskiy "budg_dd_growth %lld, budg_idx_growth %lld\n", current->pid, 617*b137545cSArtem Bityutskiy c->bi.data_growth, c->bi.dd_growth, c->bi.idx_growth); 6181e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tdata budget sum %lld, total budget sum %lld, " 619*b137545cSArtem Bityutskiy "freeable_cnt %d\n", c->bi.data_growth + c->bi.dd_growth, 620*b137545cSArtem Bityutskiy c->bi.data_growth + c->bi.dd_growth + c->bi.idx_growth, 6211e51764aSArtem Bityutskiy c->freeable_cnt); 6221e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tmin_idx_lebs %d, old_idx_sz %lld, " 623*b137545cSArtem Bityutskiy "calc_idx_sz %lld, idx_gc_cnt %d\n", c->bi.min_idx_lebs, 624*b137545cSArtem Bityutskiy c->bi.old_idx_sz, c->calc_idx_sz, c->idx_gc_cnt); 6251e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, " 6261e51764aSArtem Bityutskiy "clean_zn_cnt %ld\n", atomic_long_read(&c->dirty_pg_cnt), 6271e51764aSArtem Bityutskiy atomic_long_read(&c->dirty_zn_cnt), 6281e51764aSArtem Bityutskiy atomic_long_read(&c->clean_zn_cnt)); 6291e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n", 6301e51764aSArtem Bityutskiy c->dark_wm, c->dead_wm, c->max_idx_node_sz); 6311e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tgc_lnum %d, ihead_lnum %d\n", 6321e51764aSArtem Bityutskiy c->gc_lnum, c->ihead_lnum); 63384abf972SArtem Bityutskiy /* If we are in R/O mode, journal heads do not exist */ 63484abf972SArtem Bityutskiy if (c->jheads) 6351e51764aSArtem Bityutskiy for (i = 0; i < c->jhead_cnt; i++) 63677a7ae58SArtem Bityutskiy printk(KERN_DEBUG "\tjhead %s\t LEB %d\n", 63777a7ae58SArtem Bityutskiy dbg_jhead(c->jheads[i].wbuf.jhead), 63877a7ae58SArtem Bityutskiy c->jheads[i].wbuf.lnum); 6391e51764aSArtem Bityutskiy for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) { 6401e51764aSArtem Bityutskiy bud = rb_entry(rb, struct ubifs_bud, rb); 6411e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tbud LEB %d\n", bud->lnum); 6421e51764aSArtem Bityutskiy } 6431e51764aSArtem Bityutskiy list_for_each_entry(bud, &c->old_buds, list) 6441e51764aSArtem Bityutskiy printk(KERN_DEBUG "\told bud LEB %d\n", bud->lnum); 6451e51764aSArtem Bityutskiy list_for_each_entry(idx_gc, &c->idx_gc, list) 6461e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tGC'ed idx LEB %d unmap %d\n", 6471e51764aSArtem Bityutskiy idx_gc->lnum, idx_gc->unmap); 6481e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tcommit state %d\n", c->cmt_state); 64921a60258SArtem Bityutskiy 65021a60258SArtem Bityutskiy /* Print budgeting predictions */ 651*b137545cSArtem Bityutskiy available = ubifs_calc_available(c, c->bi.min_idx_lebs); 652*b137545cSArtem Bityutskiy outstanding = c->bi.data_growth + c->bi.dd_growth; 65384abf972SArtem Bityutskiy free = ubifs_get_free_space_nolock(c); 65421a60258SArtem Bityutskiy printk(KERN_DEBUG "Budgeting predictions:\n"); 65521a60258SArtem Bityutskiy printk(KERN_DEBUG "\tavailable: %lld, outstanding %lld, free %lld\n", 65621a60258SArtem Bityutskiy available, outstanding, free); 6571e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 6581e51764aSArtem Bityutskiy } 6591e51764aSArtem Bityutskiy 6601e51764aSArtem Bityutskiy void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp) 6611e51764aSArtem Bityutskiy { 662be9e62a7SArtem Bityutskiy int i, spc, dark = 0, dead = 0; 663be9e62a7SArtem Bityutskiy struct rb_node *rb; 664be9e62a7SArtem Bityutskiy struct ubifs_bud *bud; 665be9e62a7SArtem Bityutskiy 666be9e62a7SArtem Bityutskiy spc = lp->free + lp->dirty; 667be9e62a7SArtem Bityutskiy if (spc < c->dead_wm) 668be9e62a7SArtem Bityutskiy dead = spc; 669be9e62a7SArtem Bityutskiy else 670be9e62a7SArtem Bityutskiy dark = ubifs_calc_dark(c, spc); 671be9e62a7SArtem Bityutskiy 672be9e62a7SArtem Bityutskiy if (lp->flags & LPROPS_INDEX) 673be9e62a7SArtem Bityutskiy printk(KERN_DEBUG "LEB %-7d free %-8d dirty %-8d used %-8d " 674be9e62a7SArtem Bityutskiy "free + dirty %-8d flags %#x (", lp->lnum, lp->free, 675be9e62a7SArtem Bityutskiy lp->dirty, c->leb_size - spc, spc, lp->flags); 676be9e62a7SArtem Bityutskiy else 677be9e62a7SArtem Bityutskiy printk(KERN_DEBUG "LEB %-7d free %-8d dirty %-8d used %-8d " 678be9e62a7SArtem Bityutskiy "free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d " 679be9e62a7SArtem Bityutskiy "flags %#-4x (", lp->lnum, lp->free, lp->dirty, 680be9e62a7SArtem Bityutskiy c->leb_size - spc, spc, dark, dead, 681be9e62a7SArtem Bityutskiy (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags); 682be9e62a7SArtem Bityutskiy 683be9e62a7SArtem Bityutskiy if (lp->flags & LPROPS_TAKEN) { 684be9e62a7SArtem Bityutskiy if (lp->flags & LPROPS_INDEX) 685be9e62a7SArtem Bityutskiy printk(KERN_CONT "index, taken"); 686be9e62a7SArtem Bityutskiy else 687be9e62a7SArtem Bityutskiy printk(KERN_CONT "taken"); 688be9e62a7SArtem Bityutskiy } else { 689be9e62a7SArtem Bityutskiy const char *s; 690be9e62a7SArtem Bityutskiy 691be9e62a7SArtem Bityutskiy if (lp->flags & LPROPS_INDEX) { 692be9e62a7SArtem Bityutskiy switch (lp->flags & LPROPS_CAT_MASK) { 693be9e62a7SArtem Bityutskiy case LPROPS_DIRTY_IDX: 694be9e62a7SArtem Bityutskiy s = "dirty index"; 695be9e62a7SArtem Bityutskiy break; 696be9e62a7SArtem Bityutskiy case LPROPS_FRDI_IDX: 697be9e62a7SArtem Bityutskiy s = "freeable index"; 698be9e62a7SArtem Bityutskiy break; 699be9e62a7SArtem Bityutskiy default: 700be9e62a7SArtem Bityutskiy s = "index"; 701be9e62a7SArtem Bityutskiy } 702be9e62a7SArtem Bityutskiy } else { 703be9e62a7SArtem Bityutskiy switch (lp->flags & LPROPS_CAT_MASK) { 704be9e62a7SArtem Bityutskiy case LPROPS_UNCAT: 705be9e62a7SArtem Bityutskiy s = "not categorized"; 706be9e62a7SArtem Bityutskiy break; 707be9e62a7SArtem Bityutskiy case LPROPS_DIRTY: 708be9e62a7SArtem Bityutskiy s = "dirty"; 709be9e62a7SArtem Bityutskiy break; 710be9e62a7SArtem Bityutskiy case LPROPS_FREE: 711be9e62a7SArtem Bityutskiy s = "free"; 712be9e62a7SArtem Bityutskiy break; 713be9e62a7SArtem Bityutskiy case LPROPS_EMPTY: 714be9e62a7SArtem Bityutskiy s = "empty"; 715be9e62a7SArtem Bityutskiy break; 716be9e62a7SArtem Bityutskiy case LPROPS_FREEABLE: 717be9e62a7SArtem Bityutskiy s = "freeable"; 718be9e62a7SArtem Bityutskiy break; 719be9e62a7SArtem Bityutskiy default: 720be9e62a7SArtem Bityutskiy s = NULL; 721be9e62a7SArtem Bityutskiy break; 722be9e62a7SArtem Bityutskiy } 723be9e62a7SArtem Bityutskiy } 724be9e62a7SArtem Bityutskiy printk(KERN_CONT "%s", s); 725be9e62a7SArtem Bityutskiy } 726be9e62a7SArtem Bityutskiy 727be9e62a7SArtem Bityutskiy for (rb = rb_first((struct rb_root *)&c->buds); rb; rb = rb_next(rb)) { 728be9e62a7SArtem Bityutskiy bud = rb_entry(rb, struct ubifs_bud, rb); 729be9e62a7SArtem Bityutskiy if (bud->lnum == lp->lnum) { 730be9e62a7SArtem Bityutskiy int head = 0; 731be9e62a7SArtem Bityutskiy for (i = 0; i < c->jhead_cnt; i++) { 732be9e62a7SArtem Bityutskiy if (lp->lnum == c->jheads[i].wbuf.lnum) { 733be9e62a7SArtem Bityutskiy printk(KERN_CONT ", jhead %s", 734be9e62a7SArtem Bityutskiy dbg_jhead(i)); 735be9e62a7SArtem Bityutskiy head = 1; 736be9e62a7SArtem Bityutskiy } 737be9e62a7SArtem Bityutskiy } 738be9e62a7SArtem Bityutskiy if (!head) 739be9e62a7SArtem Bityutskiy printk(KERN_CONT ", bud of jhead %s", 740be9e62a7SArtem Bityutskiy dbg_jhead(bud->jhead)); 741be9e62a7SArtem Bityutskiy } 742be9e62a7SArtem Bityutskiy } 743be9e62a7SArtem Bityutskiy if (lp->lnum == c->gc_lnum) 744be9e62a7SArtem Bityutskiy printk(KERN_CONT ", GC LEB"); 745be9e62a7SArtem Bityutskiy printk(KERN_CONT ")\n"); 7461e51764aSArtem Bityutskiy } 7471e51764aSArtem Bityutskiy 7481e51764aSArtem Bityutskiy void dbg_dump_lprops(struct ubifs_info *c) 7491e51764aSArtem Bityutskiy { 7501e51764aSArtem Bityutskiy int lnum, err; 7511e51764aSArtem Bityutskiy struct ubifs_lprops lp; 7521e51764aSArtem Bityutskiy struct ubifs_lp_stats lst; 7531e51764aSArtem Bityutskiy 7542ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) start dumping LEB properties\n", 7552ba5f7aeSArtem Bityutskiy current->pid); 7561e51764aSArtem Bityutskiy ubifs_get_lp_stats(c, &lst); 7571e51764aSArtem Bityutskiy dbg_dump_lstats(&lst); 7581e51764aSArtem Bityutskiy 7591e51764aSArtem Bityutskiy for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) { 7601e51764aSArtem Bityutskiy err = ubifs_read_one_lp(c, lnum, &lp); 7611e51764aSArtem Bityutskiy if (err) 7621e51764aSArtem Bityutskiy ubifs_err("cannot read lprops for LEB %d", lnum); 7631e51764aSArtem Bityutskiy 7641e51764aSArtem Bityutskiy dbg_dump_lprop(c, &lp); 7651e51764aSArtem Bityutskiy } 7662ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) finish dumping LEB properties\n", 7672ba5f7aeSArtem Bityutskiy current->pid); 7681e51764aSArtem Bityutskiy } 7691e51764aSArtem Bityutskiy 77073944a6dSAdrian Hunter void dbg_dump_lpt_info(struct ubifs_info *c) 77173944a6dSAdrian Hunter { 77273944a6dSAdrian Hunter int i; 77373944a6dSAdrian Hunter 77473944a6dSAdrian Hunter spin_lock(&dbg_lock); 7752ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) dumping LPT information\n", current->pid); 77673944a6dSAdrian Hunter printk(KERN_DEBUG "\tlpt_sz: %lld\n", c->lpt_sz); 77773944a6dSAdrian Hunter printk(KERN_DEBUG "\tpnode_sz: %d\n", c->pnode_sz); 77873944a6dSAdrian Hunter printk(KERN_DEBUG "\tnnode_sz: %d\n", c->nnode_sz); 77973944a6dSAdrian Hunter printk(KERN_DEBUG "\tltab_sz: %d\n", c->ltab_sz); 78073944a6dSAdrian Hunter printk(KERN_DEBUG "\tlsave_sz: %d\n", c->lsave_sz); 78173944a6dSAdrian Hunter printk(KERN_DEBUG "\tbig_lpt: %d\n", c->big_lpt); 78273944a6dSAdrian Hunter printk(KERN_DEBUG "\tlpt_hght: %d\n", c->lpt_hght); 78373944a6dSAdrian Hunter printk(KERN_DEBUG "\tpnode_cnt: %d\n", c->pnode_cnt); 78473944a6dSAdrian Hunter printk(KERN_DEBUG "\tnnode_cnt: %d\n", c->nnode_cnt); 78573944a6dSAdrian Hunter printk(KERN_DEBUG "\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt); 78673944a6dSAdrian Hunter printk(KERN_DEBUG "\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt); 78773944a6dSAdrian Hunter printk(KERN_DEBUG "\tlsave_cnt: %d\n", c->lsave_cnt); 78873944a6dSAdrian Hunter printk(KERN_DEBUG "\tspace_bits: %d\n", c->space_bits); 78973944a6dSAdrian Hunter printk(KERN_DEBUG "\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits); 79073944a6dSAdrian Hunter printk(KERN_DEBUG "\tlpt_offs_bits: %d\n", c->lpt_offs_bits); 79173944a6dSAdrian Hunter printk(KERN_DEBUG "\tlpt_spc_bits: %d\n", c->lpt_spc_bits); 79273944a6dSAdrian Hunter printk(KERN_DEBUG "\tpcnt_bits: %d\n", c->pcnt_bits); 79373944a6dSAdrian Hunter printk(KERN_DEBUG "\tlnum_bits: %d\n", c->lnum_bits); 79473944a6dSAdrian Hunter printk(KERN_DEBUG "\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs); 79573944a6dSAdrian Hunter printk(KERN_DEBUG "\tLPT head is at %d:%d\n", 79673944a6dSAdrian Hunter c->nhead_lnum, c->nhead_offs); 797f92b9826SArtem Bityutskiy printk(KERN_DEBUG "\tLPT ltab is at %d:%d\n", 798f92b9826SArtem Bityutskiy c->ltab_lnum, c->ltab_offs); 79973944a6dSAdrian Hunter if (c->big_lpt) 80073944a6dSAdrian Hunter printk(KERN_DEBUG "\tLPT lsave is at %d:%d\n", 80173944a6dSAdrian Hunter c->lsave_lnum, c->lsave_offs); 80273944a6dSAdrian Hunter for (i = 0; i < c->lpt_lebs; i++) 80373944a6dSAdrian Hunter printk(KERN_DEBUG "\tLPT LEB %d free %d dirty %d tgc %d " 80473944a6dSAdrian Hunter "cmt %d\n", i + c->lpt_first, c->ltab[i].free, 80573944a6dSAdrian Hunter c->ltab[i].dirty, c->ltab[i].tgc, c->ltab[i].cmt); 80673944a6dSAdrian Hunter spin_unlock(&dbg_lock); 80773944a6dSAdrian Hunter } 80873944a6dSAdrian Hunter 8091e51764aSArtem Bityutskiy void dbg_dump_leb(const struct ubifs_info *c, int lnum) 8101e51764aSArtem Bityutskiy { 8111e51764aSArtem Bityutskiy struct ubifs_scan_leb *sleb; 8121e51764aSArtem Bityutskiy struct ubifs_scan_node *snod; 81373d9aec3SArtem Bityutskiy void *buf; 8141e51764aSArtem Bityutskiy 8151e51764aSArtem Bityutskiy if (dbg_failure_mode) 8161e51764aSArtem Bityutskiy return; 8171e51764aSArtem Bityutskiy 8182ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n", 8192ba5f7aeSArtem Bityutskiy current->pid, lnum); 82073d9aec3SArtem Bityutskiy 821fc5e58c0SArtem Bityutskiy buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL); 82273d9aec3SArtem Bityutskiy if (!buf) { 82373d9aec3SArtem Bityutskiy ubifs_err("cannot allocate memory for dumping LEB %d", lnum); 82473d9aec3SArtem Bityutskiy return; 82573d9aec3SArtem Bityutskiy } 82673d9aec3SArtem Bityutskiy 82773d9aec3SArtem Bityutskiy sleb = ubifs_scan(c, lnum, 0, buf, 0); 8281e51764aSArtem Bityutskiy if (IS_ERR(sleb)) { 8291e51764aSArtem Bityutskiy ubifs_err("scan error %d", (int)PTR_ERR(sleb)); 83073d9aec3SArtem Bityutskiy goto out; 8311e51764aSArtem Bityutskiy } 8321e51764aSArtem Bityutskiy 8331e51764aSArtem Bityutskiy printk(KERN_DEBUG "LEB %d has %d nodes ending at %d\n", lnum, 8341e51764aSArtem Bityutskiy sleb->nodes_cnt, sleb->endpt); 8351e51764aSArtem Bityutskiy 8361e51764aSArtem Bityutskiy list_for_each_entry(snod, &sleb->nodes, list) { 8371e51764aSArtem Bityutskiy cond_resched(); 8381e51764aSArtem Bityutskiy printk(KERN_DEBUG "Dumping node at LEB %d:%d len %d\n", lnum, 8391e51764aSArtem Bityutskiy snod->offs, snod->len); 8401e51764aSArtem Bityutskiy dbg_dump_node(c, snod->node); 8411e51764aSArtem Bityutskiy } 8421e51764aSArtem Bityutskiy 8432ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n", 8442ba5f7aeSArtem Bityutskiy current->pid, lnum); 8451e51764aSArtem Bityutskiy ubifs_scan_destroy(sleb); 84673d9aec3SArtem Bityutskiy 84773d9aec3SArtem Bityutskiy out: 84873d9aec3SArtem Bityutskiy vfree(buf); 8491e51764aSArtem Bityutskiy return; 8501e51764aSArtem Bityutskiy } 8511e51764aSArtem Bityutskiy 8521e51764aSArtem Bityutskiy void dbg_dump_znode(const struct ubifs_info *c, 8531e51764aSArtem Bityutskiy const struct ubifs_znode *znode) 8541e51764aSArtem Bityutskiy { 8551e51764aSArtem Bityutskiy int n; 8561e51764aSArtem Bityutskiy const struct ubifs_zbranch *zbr; 8571e51764aSArtem Bityutskiy 8581e51764aSArtem Bityutskiy spin_lock(&dbg_lock); 8591e51764aSArtem Bityutskiy if (znode->parent) 8601e51764aSArtem Bityutskiy zbr = &znode->parent->zbranch[znode->iip]; 8611e51764aSArtem Bityutskiy else 8621e51764aSArtem Bityutskiy zbr = &c->zroot; 8631e51764aSArtem Bityutskiy 8641e51764aSArtem Bityutskiy printk(KERN_DEBUG "znode %p, LEB %d:%d len %d parent %p iip %d level %d" 8651e51764aSArtem Bityutskiy " child_cnt %d flags %lx\n", znode, zbr->lnum, zbr->offs, 8661e51764aSArtem Bityutskiy zbr->len, znode->parent, znode->iip, znode->level, 8671e51764aSArtem Bityutskiy znode->child_cnt, znode->flags); 8681e51764aSArtem Bityutskiy 8691e51764aSArtem Bityutskiy if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) { 8701e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 8711e51764aSArtem Bityutskiy return; 8721e51764aSArtem Bityutskiy } 8731e51764aSArtem Bityutskiy 8741e51764aSArtem Bityutskiy printk(KERN_DEBUG "zbranches:\n"); 8751e51764aSArtem Bityutskiy for (n = 0; n < znode->child_cnt; n++) { 8761e51764aSArtem Bityutskiy zbr = &znode->zbranch[n]; 8771e51764aSArtem Bityutskiy if (znode->level > 0) 8781e51764aSArtem Bityutskiy printk(KERN_DEBUG "\t%d: znode %p LEB %d:%d len %d key " 8791e51764aSArtem Bityutskiy "%s\n", n, zbr->znode, zbr->lnum, 8801e51764aSArtem Bityutskiy zbr->offs, zbr->len, 8811e51764aSArtem Bityutskiy DBGKEY(&zbr->key)); 8821e51764aSArtem Bityutskiy else 8831e51764aSArtem Bityutskiy printk(KERN_DEBUG "\t%d: LNC %p LEB %d:%d len %d key " 8841e51764aSArtem Bityutskiy "%s\n", n, zbr->znode, zbr->lnum, 8851e51764aSArtem Bityutskiy zbr->offs, zbr->len, 8861e51764aSArtem Bityutskiy DBGKEY(&zbr->key)); 8871e51764aSArtem Bityutskiy } 8881e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 8891e51764aSArtem Bityutskiy } 8901e51764aSArtem Bityutskiy 8911e51764aSArtem Bityutskiy void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat) 8921e51764aSArtem Bityutskiy { 8931e51764aSArtem Bityutskiy int i; 8941e51764aSArtem Bityutskiy 8952ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) start dumping heap cat %d (%d elements)\n", 8961de94159SArtem Bityutskiy current->pid, cat, heap->cnt); 8971e51764aSArtem Bityutskiy for (i = 0; i < heap->cnt; i++) { 8981e51764aSArtem Bityutskiy struct ubifs_lprops *lprops = heap->arr[i]; 8991e51764aSArtem Bityutskiy 9001e51764aSArtem Bityutskiy printk(KERN_DEBUG "\t%d. LEB %d hpos %d free %d dirty %d " 9011e51764aSArtem Bityutskiy "flags %d\n", i, lprops->lnum, lprops->hpos, 9021e51764aSArtem Bityutskiy lprops->free, lprops->dirty, lprops->flags); 9031e51764aSArtem Bityutskiy } 9042ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) finish dumping heap\n", current->pid); 9051e51764aSArtem Bityutskiy } 9061e51764aSArtem Bityutskiy 9071e51764aSArtem Bityutskiy void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode, 9081e51764aSArtem Bityutskiy struct ubifs_nnode *parent, int iip) 9091e51764aSArtem Bityutskiy { 9101e51764aSArtem Bityutskiy int i; 9111e51764aSArtem Bityutskiy 9122ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) dumping pnode:\n", current->pid); 9131e51764aSArtem Bityutskiy printk(KERN_DEBUG "\taddress %zx parent %zx cnext %zx\n", 9141e51764aSArtem Bityutskiy (size_t)pnode, (size_t)parent, (size_t)pnode->cnext); 9151e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tflags %lu iip %d level %d num %d\n", 9161e51764aSArtem Bityutskiy pnode->flags, iip, pnode->level, pnode->num); 9171e51764aSArtem Bityutskiy for (i = 0; i < UBIFS_LPT_FANOUT; i++) { 9181e51764aSArtem Bityutskiy struct ubifs_lprops *lp = &pnode->lprops[i]; 9191e51764aSArtem Bityutskiy 9201e51764aSArtem Bityutskiy printk(KERN_DEBUG "\t%d: free %d dirty %d flags %d lnum %d\n", 9211e51764aSArtem Bityutskiy i, lp->free, lp->dirty, lp->flags, lp->lnum); 9221e51764aSArtem Bityutskiy } 9231e51764aSArtem Bityutskiy } 9241e51764aSArtem Bityutskiy 9251e51764aSArtem Bityutskiy void dbg_dump_tnc(struct ubifs_info *c) 9261e51764aSArtem Bityutskiy { 9271e51764aSArtem Bityutskiy struct ubifs_znode *znode; 9281e51764aSArtem Bityutskiy int level; 9291e51764aSArtem Bityutskiy 9301e51764aSArtem Bityutskiy printk(KERN_DEBUG "\n"); 9312ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) start dumping TNC tree\n", current->pid); 9321e51764aSArtem Bityutskiy znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL); 9331e51764aSArtem Bityutskiy level = znode->level; 9341e51764aSArtem Bityutskiy printk(KERN_DEBUG "== Level %d ==\n", level); 9351e51764aSArtem Bityutskiy while (znode) { 9361e51764aSArtem Bityutskiy if (level != znode->level) { 9371e51764aSArtem Bityutskiy level = znode->level; 9381e51764aSArtem Bityutskiy printk(KERN_DEBUG "== Level %d ==\n", level); 9391e51764aSArtem Bityutskiy } 9401e51764aSArtem Bityutskiy dbg_dump_znode(c, znode); 9411e51764aSArtem Bityutskiy znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode); 9421e51764aSArtem Bityutskiy } 9432ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) finish dumping TNC tree\n", current->pid); 9441e51764aSArtem Bityutskiy } 9451e51764aSArtem Bityutskiy 9461e51764aSArtem Bityutskiy static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode, 9471e51764aSArtem Bityutskiy void *priv) 9481e51764aSArtem Bityutskiy { 9491e51764aSArtem Bityutskiy dbg_dump_znode(c, znode); 9501e51764aSArtem Bityutskiy return 0; 9511e51764aSArtem Bityutskiy } 9521e51764aSArtem Bityutskiy 9531e51764aSArtem Bityutskiy /** 9541e51764aSArtem Bityutskiy * dbg_dump_index - dump the on-flash index. 9551e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 9561e51764aSArtem Bityutskiy * 9571e51764aSArtem Bityutskiy * This function dumps whole UBIFS indexing B-tree, unlike 'dbg_dump_tnc()' 9581e51764aSArtem Bityutskiy * which dumps only in-memory znodes and does not read znodes which from flash. 9591e51764aSArtem Bityutskiy */ 9601e51764aSArtem Bityutskiy void dbg_dump_index(struct ubifs_info *c) 9611e51764aSArtem Bityutskiy { 9621e51764aSArtem Bityutskiy dbg_walk_index(c, NULL, dump_znode, NULL); 9631e51764aSArtem Bityutskiy } 9641e51764aSArtem Bityutskiy 9651e51764aSArtem Bityutskiy /** 96684abf972SArtem Bityutskiy * dbg_save_space_info - save information about flash space. 96784abf972SArtem Bityutskiy * @c: UBIFS file-system description object 96884abf972SArtem Bityutskiy * 96984abf972SArtem Bityutskiy * This function saves information about UBIFS free space, dirty space, etc, in 97084abf972SArtem Bityutskiy * order to check it later. 97184abf972SArtem Bityutskiy */ 97284abf972SArtem Bityutskiy void dbg_save_space_info(struct ubifs_info *c) 97384abf972SArtem Bityutskiy { 97484abf972SArtem Bityutskiy struct ubifs_debug_info *d = c->dbg; 9757da6443aSArtem Bityutskiy int freeable_cnt; 97684abf972SArtem Bityutskiy 97784abf972SArtem Bityutskiy spin_lock(&c->space_lock); 9787da6443aSArtem Bityutskiy memcpy(&d->saved_lst, &c->lst, sizeof(struct ubifs_lp_stats)); 9797da6443aSArtem Bityutskiy 9807da6443aSArtem Bityutskiy /* 9817da6443aSArtem Bityutskiy * We use a dirty hack here and zero out @c->freeable_cnt, because it 9827da6443aSArtem Bityutskiy * affects the free space calculations, and UBIFS might not know about 9837da6443aSArtem Bityutskiy * all freeable eraseblocks. Indeed, we know about freeable eraseblocks 9847da6443aSArtem Bityutskiy * only when we read their lprops, and we do this only lazily, upon the 9857da6443aSArtem Bityutskiy * need. So at any given point of time @c->freeable_cnt might be not 9867da6443aSArtem Bityutskiy * exactly accurate. 9877da6443aSArtem Bityutskiy * 9887da6443aSArtem Bityutskiy * Just one example about the issue we hit when we did not zero 9897da6443aSArtem Bityutskiy * @c->freeable_cnt. 9907da6443aSArtem Bityutskiy * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the 9917da6443aSArtem Bityutskiy * amount of free space in @d->saved_free 9927da6443aSArtem Bityutskiy * 2. We re-mount R/W, which makes UBIFS to read the "lsave" 9937da6443aSArtem Bityutskiy * information from flash, where we cache LEBs from various 9947da6443aSArtem Bityutskiy * categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()' 9957da6443aSArtem Bityutskiy * -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()' 9967da6443aSArtem Bityutskiy * -> 'ubifs_get_pnode()' -> 'update_cats()' 9977da6443aSArtem Bityutskiy * -> 'ubifs_add_to_cat()'). 9987da6443aSArtem Bityutskiy * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt 9997da6443aSArtem Bityutskiy * becomes %1. 10007da6443aSArtem Bityutskiy * 4. We calculate the amount of free space when the re-mount is 10017da6443aSArtem Bityutskiy * finished in 'dbg_check_space_info()' and it does not match 10027da6443aSArtem Bityutskiy * @d->saved_free. 10037da6443aSArtem Bityutskiy */ 10047da6443aSArtem Bityutskiy freeable_cnt = c->freeable_cnt; 10057da6443aSArtem Bityutskiy c->freeable_cnt = 0; 100684abf972SArtem Bityutskiy d->saved_free = ubifs_get_free_space_nolock(c); 10077da6443aSArtem Bityutskiy c->freeable_cnt = freeable_cnt; 100884abf972SArtem Bityutskiy spin_unlock(&c->space_lock); 100984abf972SArtem Bityutskiy } 101084abf972SArtem Bityutskiy 101184abf972SArtem Bityutskiy /** 101284abf972SArtem Bityutskiy * dbg_check_space_info - check flash space information. 101384abf972SArtem Bityutskiy * @c: UBIFS file-system description object 101484abf972SArtem Bityutskiy * 101584abf972SArtem Bityutskiy * This function compares current flash space information with the information 101684abf972SArtem Bityutskiy * which was saved when the 'dbg_save_space_info()' function was called. 101784abf972SArtem Bityutskiy * Returns zero if the information has not changed, and %-EINVAL it it has 101884abf972SArtem Bityutskiy * changed. 101984abf972SArtem Bityutskiy */ 102084abf972SArtem Bityutskiy int dbg_check_space_info(struct ubifs_info *c) 102184abf972SArtem Bityutskiy { 102284abf972SArtem Bityutskiy struct ubifs_debug_info *d = c->dbg; 102384abf972SArtem Bityutskiy struct ubifs_lp_stats lst; 10247da6443aSArtem Bityutskiy long long free; 10257da6443aSArtem Bityutskiy int freeable_cnt; 102684abf972SArtem Bityutskiy 102784abf972SArtem Bityutskiy spin_lock(&c->space_lock); 10287da6443aSArtem Bityutskiy freeable_cnt = c->freeable_cnt; 10297da6443aSArtem Bityutskiy c->freeable_cnt = 0; 10307da6443aSArtem Bityutskiy free = ubifs_get_free_space_nolock(c); 10317da6443aSArtem Bityutskiy c->freeable_cnt = freeable_cnt; 103284abf972SArtem Bityutskiy spin_unlock(&c->space_lock); 103384abf972SArtem Bityutskiy 103484abf972SArtem Bityutskiy if (free != d->saved_free) { 103584abf972SArtem Bityutskiy ubifs_err("free space changed from %lld to %lld", 103684abf972SArtem Bityutskiy d->saved_free, free); 103784abf972SArtem Bityutskiy goto out; 103884abf972SArtem Bityutskiy } 103984abf972SArtem Bityutskiy 104084abf972SArtem Bityutskiy return 0; 104184abf972SArtem Bityutskiy 104284abf972SArtem Bityutskiy out: 104384abf972SArtem Bityutskiy ubifs_msg("saved lprops statistics dump"); 104484abf972SArtem Bityutskiy dbg_dump_lstats(&d->saved_lst); 104584abf972SArtem Bityutskiy ubifs_get_lp_stats(c, &lst); 1046e055f7e8SArtem Bityutskiy 104784abf972SArtem Bityutskiy ubifs_msg("current lprops statistics dump"); 1048e055f7e8SArtem Bityutskiy dbg_dump_lstats(&lst); 1049e055f7e8SArtem Bityutskiy 105084abf972SArtem Bityutskiy spin_lock(&c->space_lock); 105184abf972SArtem Bityutskiy dbg_dump_budg(c); 105284abf972SArtem Bityutskiy spin_unlock(&c->space_lock); 105384abf972SArtem Bityutskiy dump_stack(); 105484abf972SArtem Bityutskiy return -EINVAL; 105584abf972SArtem Bityutskiy } 105684abf972SArtem Bityutskiy 105784abf972SArtem Bityutskiy /** 10581e51764aSArtem Bityutskiy * dbg_check_synced_i_size - check synchronized inode size. 10591e51764aSArtem Bityutskiy * @inode: inode to check 10601e51764aSArtem Bityutskiy * 10611e51764aSArtem Bityutskiy * If inode is clean, synchronized inode size has to be equivalent to current 10621e51764aSArtem Bityutskiy * inode size. This function has to be called only for locked inodes (@i_mutex 10631e51764aSArtem Bityutskiy * has to be locked). Returns %0 if synchronized inode size if correct, and 10641e51764aSArtem Bityutskiy * %-EINVAL if not. 10651e51764aSArtem Bityutskiy */ 10661e51764aSArtem Bityutskiy int dbg_check_synced_i_size(struct inode *inode) 10671e51764aSArtem Bityutskiy { 10681e51764aSArtem Bityutskiy int err = 0; 10691e51764aSArtem Bityutskiy struct ubifs_inode *ui = ubifs_inode(inode); 10701e51764aSArtem Bityutskiy 10711e51764aSArtem Bityutskiy if (!(ubifs_chk_flags & UBIFS_CHK_GEN)) 10721e51764aSArtem Bityutskiy return 0; 10731e51764aSArtem Bityutskiy if (!S_ISREG(inode->i_mode)) 10741e51764aSArtem Bityutskiy return 0; 10751e51764aSArtem Bityutskiy 10761e51764aSArtem Bityutskiy mutex_lock(&ui->ui_mutex); 10771e51764aSArtem Bityutskiy spin_lock(&ui->ui_lock); 10781e51764aSArtem Bityutskiy if (ui->ui_size != ui->synced_i_size && !ui->dirty) { 10791e51764aSArtem Bityutskiy ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode " 10801e51764aSArtem Bityutskiy "is clean", ui->ui_size, ui->synced_i_size); 10811e51764aSArtem Bityutskiy ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino, 10821e51764aSArtem Bityutskiy inode->i_mode, i_size_read(inode)); 10831e51764aSArtem Bityutskiy dbg_dump_stack(); 10841e51764aSArtem Bityutskiy err = -EINVAL; 10851e51764aSArtem Bityutskiy } 10861e51764aSArtem Bityutskiy spin_unlock(&ui->ui_lock); 10871e51764aSArtem Bityutskiy mutex_unlock(&ui->ui_mutex); 10881e51764aSArtem Bityutskiy return err; 10891e51764aSArtem Bityutskiy } 10901e51764aSArtem Bityutskiy 10911e51764aSArtem Bityutskiy /* 10921e51764aSArtem Bityutskiy * dbg_check_dir - check directory inode size and link count. 10931e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 10941e51764aSArtem Bityutskiy * @dir: the directory to calculate size for 10951e51764aSArtem Bityutskiy * @size: the result is returned here 10961e51764aSArtem Bityutskiy * 10971e51764aSArtem Bityutskiy * This function makes sure that directory size and link count are correct. 10981e51764aSArtem Bityutskiy * Returns zero in case of success and a negative error code in case of 10991e51764aSArtem Bityutskiy * failure. 11001e51764aSArtem Bityutskiy * 11011e51764aSArtem Bityutskiy * Note, it is good idea to make sure the @dir->i_mutex is locked before 11021e51764aSArtem Bityutskiy * calling this function. 11031e51764aSArtem Bityutskiy */ 11041e51764aSArtem Bityutskiy int dbg_check_dir_size(struct ubifs_info *c, const struct inode *dir) 11051e51764aSArtem Bityutskiy { 11061e51764aSArtem Bityutskiy unsigned int nlink = 2; 11071e51764aSArtem Bityutskiy union ubifs_key key; 11081e51764aSArtem Bityutskiy struct ubifs_dent_node *dent, *pdent = NULL; 11091e51764aSArtem Bityutskiy struct qstr nm = { .name = NULL }; 11101e51764aSArtem Bityutskiy loff_t size = UBIFS_INO_NODE_SZ; 11111e51764aSArtem Bityutskiy 11121e51764aSArtem Bityutskiy if (!(ubifs_chk_flags & UBIFS_CHK_GEN)) 11131e51764aSArtem Bityutskiy return 0; 11141e51764aSArtem Bityutskiy 11151e51764aSArtem Bityutskiy if (!S_ISDIR(dir->i_mode)) 11161e51764aSArtem Bityutskiy return 0; 11171e51764aSArtem Bityutskiy 11181e51764aSArtem Bityutskiy lowest_dent_key(c, &key, dir->i_ino); 11191e51764aSArtem Bityutskiy while (1) { 11201e51764aSArtem Bityutskiy int err; 11211e51764aSArtem Bityutskiy 11221e51764aSArtem Bityutskiy dent = ubifs_tnc_next_ent(c, &key, &nm); 11231e51764aSArtem Bityutskiy if (IS_ERR(dent)) { 11241e51764aSArtem Bityutskiy err = PTR_ERR(dent); 11251e51764aSArtem Bityutskiy if (err == -ENOENT) 11261e51764aSArtem Bityutskiy break; 11271e51764aSArtem Bityutskiy return err; 11281e51764aSArtem Bityutskiy } 11291e51764aSArtem Bityutskiy 11301e51764aSArtem Bityutskiy nm.name = dent->name; 11311e51764aSArtem Bityutskiy nm.len = le16_to_cpu(dent->nlen); 11321e51764aSArtem Bityutskiy size += CALC_DENT_SIZE(nm.len); 11331e51764aSArtem Bityutskiy if (dent->type == UBIFS_ITYPE_DIR) 11341e51764aSArtem Bityutskiy nlink += 1; 11351e51764aSArtem Bityutskiy kfree(pdent); 11361e51764aSArtem Bityutskiy pdent = dent; 11371e51764aSArtem Bityutskiy key_read(c, &dent->key, &key); 11381e51764aSArtem Bityutskiy } 11391e51764aSArtem Bityutskiy kfree(pdent); 11401e51764aSArtem Bityutskiy 11411e51764aSArtem Bityutskiy if (i_size_read(dir) != size) { 11421e51764aSArtem Bityutskiy ubifs_err("directory inode %lu has size %llu, " 11431e51764aSArtem Bityutskiy "but calculated size is %llu", dir->i_ino, 11441e51764aSArtem Bityutskiy (unsigned long long)i_size_read(dir), 11451e51764aSArtem Bityutskiy (unsigned long long)size); 11461e51764aSArtem Bityutskiy dump_stack(); 11471e51764aSArtem Bityutskiy return -EINVAL; 11481e51764aSArtem Bityutskiy } 11491e51764aSArtem Bityutskiy if (dir->i_nlink != nlink) { 11501e51764aSArtem Bityutskiy ubifs_err("directory inode %lu has nlink %u, but calculated " 11511e51764aSArtem Bityutskiy "nlink is %u", dir->i_ino, dir->i_nlink, nlink); 11521e51764aSArtem Bityutskiy dump_stack(); 11531e51764aSArtem Bityutskiy return -EINVAL; 11541e51764aSArtem Bityutskiy } 11551e51764aSArtem Bityutskiy 11561e51764aSArtem Bityutskiy return 0; 11571e51764aSArtem Bityutskiy } 11581e51764aSArtem Bityutskiy 11591e51764aSArtem Bityutskiy /** 11601e51764aSArtem Bityutskiy * dbg_check_key_order - make sure that colliding keys are properly ordered. 11611e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 11621e51764aSArtem Bityutskiy * @zbr1: first zbranch 11631e51764aSArtem Bityutskiy * @zbr2: following zbranch 11641e51764aSArtem Bityutskiy * 11651e51764aSArtem Bityutskiy * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of 11661e51764aSArtem Bityutskiy * names of the direntries/xentries which are referred by the keys. This 11671e51764aSArtem Bityutskiy * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes 11681e51764aSArtem Bityutskiy * sure the name of direntry/xentry referred by @zbr1 is less than 11691e51764aSArtem Bityutskiy * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not, 11701e51764aSArtem Bityutskiy * and a negative error code in case of failure. 11711e51764aSArtem Bityutskiy */ 11721e51764aSArtem Bityutskiy static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1, 11731e51764aSArtem Bityutskiy struct ubifs_zbranch *zbr2) 11741e51764aSArtem Bityutskiy { 11751e51764aSArtem Bityutskiy int err, nlen1, nlen2, cmp; 11761e51764aSArtem Bityutskiy struct ubifs_dent_node *dent1, *dent2; 11771e51764aSArtem Bityutskiy union ubifs_key key; 11781e51764aSArtem Bityutskiy 11791e51764aSArtem Bityutskiy ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key)); 11801e51764aSArtem Bityutskiy dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS); 11811e51764aSArtem Bityutskiy if (!dent1) 11821e51764aSArtem Bityutskiy return -ENOMEM; 11831e51764aSArtem Bityutskiy dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS); 11841e51764aSArtem Bityutskiy if (!dent2) { 11851e51764aSArtem Bityutskiy err = -ENOMEM; 11861e51764aSArtem Bityutskiy goto out_free; 11871e51764aSArtem Bityutskiy } 11881e51764aSArtem Bityutskiy 11891e51764aSArtem Bityutskiy err = ubifs_tnc_read_node(c, zbr1, dent1); 11901e51764aSArtem Bityutskiy if (err) 11911e51764aSArtem Bityutskiy goto out_free; 11921e51764aSArtem Bityutskiy err = ubifs_validate_entry(c, dent1); 11931e51764aSArtem Bityutskiy if (err) 11941e51764aSArtem Bityutskiy goto out_free; 11951e51764aSArtem Bityutskiy 11961e51764aSArtem Bityutskiy err = ubifs_tnc_read_node(c, zbr2, dent2); 11971e51764aSArtem Bityutskiy if (err) 11981e51764aSArtem Bityutskiy goto out_free; 11991e51764aSArtem Bityutskiy err = ubifs_validate_entry(c, dent2); 12001e51764aSArtem Bityutskiy if (err) 12011e51764aSArtem Bityutskiy goto out_free; 12021e51764aSArtem Bityutskiy 12031e51764aSArtem Bityutskiy /* Make sure node keys are the same as in zbranch */ 12041e51764aSArtem Bityutskiy err = 1; 12051e51764aSArtem Bityutskiy key_read(c, &dent1->key, &key); 12061e51764aSArtem Bityutskiy if (keys_cmp(c, &zbr1->key, &key)) { 12075d38b3acSArtem Bityutskiy dbg_err("1st entry at %d:%d has key %s", zbr1->lnum, 12081e51764aSArtem Bityutskiy zbr1->offs, DBGKEY(&key)); 12095d38b3acSArtem Bityutskiy dbg_err("but it should have key %s according to tnc", 12102ba5f7aeSArtem Bityutskiy DBGKEY(&zbr1->key)); 12112ba5f7aeSArtem Bityutskiy dbg_dump_node(c, dent1); 12121e51764aSArtem Bityutskiy goto out_free; 12131e51764aSArtem Bityutskiy } 12141e51764aSArtem Bityutskiy 12151e51764aSArtem Bityutskiy key_read(c, &dent2->key, &key); 12161e51764aSArtem Bityutskiy if (keys_cmp(c, &zbr2->key, &key)) { 12175d38b3acSArtem Bityutskiy dbg_err("2nd entry at %d:%d has key %s", zbr1->lnum, 12181e51764aSArtem Bityutskiy zbr1->offs, DBGKEY(&key)); 12195d38b3acSArtem Bityutskiy dbg_err("but it should have key %s according to tnc", 12202ba5f7aeSArtem Bityutskiy DBGKEY(&zbr2->key)); 12212ba5f7aeSArtem Bityutskiy dbg_dump_node(c, dent2); 12221e51764aSArtem Bityutskiy goto out_free; 12231e51764aSArtem Bityutskiy } 12241e51764aSArtem Bityutskiy 12251e51764aSArtem Bityutskiy nlen1 = le16_to_cpu(dent1->nlen); 12261e51764aSArtem Bityutskiy nlen2 = le16_to_cpu(dent2->nlen); 12271e51764aSArtem Bityutskiy 12281e51764aSArtem Bityutskiy cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2)); 12291e51764aSArtem Bityutskiy if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) { 12301e51764aSArtem Bityutskiy err = 0; 12311e51764aSArtem Bityutskiy goto out_free; 12321e51764aSArtem Bityutskiy } 12331e51764aSArtem Bityutskiy if (cmp == 0 && nlen1 == nlen2) 12345d38b3acSArtem Bityutskiy dbg_err("2 xent/dent nodes with the same name"); 12351e51764aSArtem Bityutskiy else 12365d38b3acSArtem Bityutskiy dbg_err("bad order of colliding key %s", 12371e51764aSArtem Bityutskiy DBGKEY(&key)); 12381e51764aSArtem Bityutskiy 1239552ff317SArtem Bityutskiy ubifs_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs); 12401e51764aSArtem Bityutskiy dbg_dump_node(c, dent1); 1241552ff317SArtem Bityutskiy ubifs_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs); 12421e51764aSArtem Bityutskiy dbg_dump_node(c, dent2); 12431e51764aSArtem Bityutskiy 12441e51764aSArtem Bityutskiy out_free: 12451e51764aSArtem Bityutskiy kfree(dent2); 12461e51764aSArtem Bityutskiy kfree(dent1); 12471e51764aSArtem Bityutskiy return err; 12481e51764aSArtem Bityutskiy } 12491e51764aSArtem Bityutskiy 12501e51764aSArtem Bityutskiy /** 12511e51764aSArtem Bityutskiy * dbg_check_znode - check if znode is all right. 12521e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 12531e51764aSArtem Bityutskiy * @zbr: zbranch which points to this znode 12541e51764aSArtem Bityutskiy * 12551e51764aSArtem Bityutskiy * This function makes sure that znode referred to by @zbr is all right. 12561e51764aSArtem Bityutskiy * Returns zero if it is, and %-EINVAL if it is not. 12571e51764aSArtem Bityutskiy */ 12581e51764aSArtem Bityutskiy static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr) 12591e51764aSArtem Bityutskiy { 12601e51764aSArtem Bityutskiy struct ubifs_znode *znode = zbr->znode; 12611e51764aSArtem Bityutskiy struct ubifs_znode *zp = znode->parent; 12621e51764aSArtem Bityutskiy int n, err, cmp; 12631e51764aSArtem Bityutskiy 12641e51764aSArtem Bityutskiy if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) { 12651e51764aSArtem Bityutskiy err = 1; 12661e51764aSArtem Bityutskiy goto out; 12671e51764aSArtem Bityutskiy } 12681e51764aSArtem Bityutskiy if (znode->level < 0) { 12691e51764aSArtem Bityutskiy err = 2; 12701e51764aSArtem Bityutskiy goto out; 12711e51764aSArtem Bityutskiy } 12721e51764aSArtem Bityutskiy if (znode->iip < 0 || znode->iip >= c->fanout) { 12731e51764aSArtem Bityutskiy err = 3; 12741e51764aSArtem Bityutskiy goto out; 12751e51764aSArtem Bityutskiy } 12761e51764aSArtem Bityutskiy 12771e51764aSArtem Bityutskiy if (zbr->len == 0) 12781e51764aSArtem Bityutskiy /* Only dirty zbranch may have no on-flash nodes */ 12791e51764aSArtem Bityutskiy if (!ubifs_zn_dirty(znode)) { 12801e51764aSArtem Bityutskiy err = 4; 12811e51764aSArtem Bityutskiy goto out; 12821e51764aSArtem Bityutskiy } 12831e51764aSArtem Bityutskiy 12841e51764aSArtem Bityutskiy if (ubifs_zn_dirty(znode)) { 12851e51764aSArtem Bityutskiy /* 12861e51764aSArtem Bityutskiy * If znode is dirty, its parent has to be dirty as well. The 12871e51764aSArtem Bityutskiy * order of the operation is important, so we have to have 12881e51764aSArtem Bityutskiy * memory barriers. 12891e51764aSArtem Bityutskiy */ 12901e51764aSArtem Bityutskiy smp_mb(); 12911e51764aSArtem Bityutskiy if (zp && !ubifs_zn_dirty(zp)) { 12921e51764aSArtem Bityutskiy /* 12931e51764aSArtem Bityutskiy * The dirty flag is atomic and is cleared outside the 12941e51764aSArtem Bityutskiy * TNC mutex, so znode's dirty flag may now have 12951e51764aSArtem Bityutskiy * been cleared. The child is always cleared before the 12961e51764aSArtem Bityutskiy * parent, so we just need to check again. 12971e51764aSArtem Bityutskiy */ 12981e51764aSArtem Bityutskiy smp_mb(); 12991e51764aSArtem Bityutskiy if (ubifs_zn_dirty(znode)) { 13001e51764aSArtem Bityutskiy err = 5; 13011e51764aSArtem Bityutskiy goto out; 13021e51764aSArtem Bityutskiy } 13031e51764aSArtem Bityutskiy } 13041e51764aSArtem Bityutskiy } 13051e51764aSArtem Bityutskiy 13061e51764aSArtem Bityutskiy if (zp) { 13071e51764aSArtem Bityutskiy const union ubifs_key *min, *max; 13081e51764aSArtem Bityutskiy 13091e51764aSArtem Bityutskiy if (znode->level != zp->level - 1) { 13101e51764aSArtem Bityutskiy err = 6; 13111e51764aSArtem Bityutskiy goto out; 13121e51764aSArtem Bityutskiy } 13131e51764aSArtem Bityutskiy 13141e51764aSArtem Bityutskiy /* Make sure the 'parent' pointer in our znode is correct */ 13151e51764aSArtem Bityutskiy err = ubifs_search_zbranch(c, zp, &zbr->key, &n); 13161e51764aSArtem Bityutskiy if (!err) { 13171e51764aSArtem Bityutskiy /* This zbranch does not exist in the parent */ 13181e51764aSArtem Bityutskiy err = 7; 13191e51764aSArtem Bityutskiy goto out; 13201e51764aSArtem Bityutskiy } 13211e51764aSArtem Bityutskiy 13221e51764aSArtem Bityutskiy if (znode->iip >= zp->child_cnt) { 13231e51764aSArtem Bityutskiy err = 8; 13241e51764aSArtem Bityutskiy goto out; 13251e51764aSArtem Bityutskiy } 13261e51764aSArtem Bityutskiy 13271e51764aSArtem Bityutskiy if (znode->iip != n) { 13281e51764aSArtem Bityutskiy /* This may happen only in case of collisions */ 13291e51764aSArtem Bityutskiy if (keys_cmp(c, &zp->zbranch[n].key, 13301e51764aSArtem Bityutskiy &zp->zbranch[znode->iip].key)) { 13311e51764aSArtem Bityutskiy err = 9; 13321e51764aSArtem Bityutskiy goto out; 13331e51764aSArtem Bityutskiy } 13341e51764aSArtem Bityutskiy n = znode->iip; 13351e51764aSArtem Bityutskiy } 13361e51764aSArtem Bityutskiy 13371e51764aSArtem Bityutskiy /* 13381e51764aSArtem Bityutskiy * Make sure that the first key in our znode is greater than or 13391e51764aSArtem Bityutskiy * equal to the key in the pointing zbranch. 13401e51764aSArtem Bityutskiy */ 13411e51764aSArtem Bityutskiy min = &zbr->key; 13421e51764aSArtem Bityutskiy cmp = keys_cmp(c, min, &znode->zbranch[0].key); 13431e51764aSArtem Bityutskiy if (cmp == 1) { 13441e51764aSArtem Bityutskiy err = 10; 13451e51764aSArtem Bityutskiy goto out; 13461e51764aSArtem Bityutskiy } 13471e51764aSArtem Bityutskiy 13481e51764aSArtem Bityutskiy if (n + 1 < zp->child_cnt) { 13491e51764aSArtem Bityutskiy max = &zp->zbranch[n + 1].key; 13501e51764aSArtem Bityutskiy 13511e51764aSArtem Bityutskiy /* 13521e51764aSArtem Bityutskiy * Make sure the last key in our znode is less or 13537d4e9ccbSArtem Bityutskiy * equivalent than the key in the zbranch which goes 13541e51764aSArtem Bityutskiy * after our pointing zbranch. 13551e51764aSArtem Bityutskiy */ 13561e51764aSArtem Bityutskiy cmp = keys_cmp(c, max, 13571e51764aSArtem Bityutskiy &znode->zbranch[znode->child_cnt - 1].key); 13581e51764aSArtem Bityutskiy if (cmp == -1) { 13591e51764aSArtem Bityutskiy err = 11; 13601e51764aSArtem Bityutskiy goto out; 13611e51764aSArtem Bityutskiy } 13621e51764aSArtem Bityutskiy } 13631e51764aSArtem Bityutskiy } else { 13641e51764aSArtem Bityutskiy /* This may only be root znode */ 13651e51764aSArtem Bityutskiy if (zbr != &c->zroot) { 13661e51764aSArtem Bityutskiy err = 12; 13671e51764aSArtem Bityutskiy goto out; 13681e51764aSArtem Bityutskiy } 13691e51764aSArtem Bityutskiy } 13701e51764aSArtem Bityutskiy 13711e51764aSArtem Bityutskiy /* 13721e51764aSArtem Bityutskiy * Make sure that next key is greater or equivalent then the previous 13731e51764aSArtem Bityutskiy * one. 13741e51764aSArtem Bityutskiy */ 13751e51764aSArtem Bityutskiy for (n = 1; n < znode->child_cnt; n++) { 13761e51764aSArtem Bityutskiy cmp = keys_cmp(c, &znode->zbranch[n - 1].key, 13771e51764aSArtem Bityutskiy &znode->zbranch[n].key); 13781e51764aSArtem Bityutskiy if (cmp > 0) { 13791e51764aSArtem Bityutskiy err = 13; 13801e51764aSArtem Bityutskiy goto out; 13811e51764aSArtem Bityutskiy } 13821e51764aSArtem Bityutskiy if (cmp == 0) { 13831e51764aSArtem Bityutskiy /* This can only be keys with colliding hash */ 13841e51764aSArtem Bityutskiy if (!is_hash_key(c, &znode->zbranch[n].key)) { 13851e51764aSArtem Bityutskiy err = 14; 13861e51764aSArtem Bityutskiy goto out; 13871e51764aSArtem Bityutskiy } 13881e51764aSArtem Bityutskiy 13891e51764aSArtem Bityutskiy if (znode->level != 0 || c->replaying) 13901e51764aSArtem Bityutskiy continue; 13911e51764aSArtem Bityutskiy 13921e51764aSArtem Bityutskiy /* 13931e51764aSArtem Bityutskiy * Colliding keys should follow binary order of 13941e51764aSArtem Bityutskiy * corresponding xentry/dentry names. 13951e51764aSArtem Bityutskiy */ 13961e51764aSArtem Bityutskiy err = dbg_check_key_order(c, &znode->zbranch[n - 1], 13971e51764aSArtem Bityutskiy &znode->zbranch[n]); 13981e51764aSArtem Bityutskiy if (err < 0) 13991e51764aSArtem Bityutskiy return err; 14001e51764aSArtem Bityutskiy if (err) { 14011e51764aSArtem Bityutskiy err = 15; 14021e51764aSArtem Bityutskiy goto out; 14031e51764aSArtem Bityutskiy } 14041e51764aSArtem Bityutskiy } 14051e51764aSArtem Bityutskiy } 14061e51764aSArtem Bityutskiy 14071e51764aSArtem Bityutskiy for (n = 0; n < znode->child_cnt; n++) { 14081e51764aSArtem Bityutskiy if (!znode->zbranch[n].znode && 14091e51764aSArtem Bityutskiy (znode->zbranch[n].lnum == 0 || 14101e51764aSArtem Bityutskiy znode->zbranch[n].len == 0)) { 14111e51764aSArtem Bityutskiy err = 16; 14121e51764aSArtem Bityutskiy goto out; 14131e51764aSArtem Bityutskiy } 14141e51764aSArtem Bityutskiy 14151e51764aSArtem Bityutskiy if (znode->zbranch[n].lnum != 0 && 14161e51764aSArtem Bityutskiy znode->zbranch[n].len == 0) { 14171e51764aSArtem Bityutskiy err = 17; 14181e51764aSArtem Bityutskiy goto out; 14191e51764aSArtem Bityutskiy } 14201e51764aSArtem Bityutskiy 14211e51764aSArtem Bityutskiy if (znode->zbranch[n].lnum == 0 && 14221e51764aSArtem Bityutskiy znode->zbranch[n].len != 0) { 14231e51764aSArtem Bityutskiy err = 18; 14241e51764aSArtem Bityutskiy goto out; 14251e51764aSArtem Bityutskiy } 14261e51764aSArtem Bityutskiy 14271e51764aSArtem Bityutskiy if (znode->zbranch[n].lnum == 0 && 14281e51764aSArtem Bityutskiy znode->zbranch[n].offs != 0) { 14291e51764aSArtem Bityutskiy err = 19; 14301e51764aSArtem Bityutskiy goto out; 14311e51764aSArtem Bityutskiy } 14321e51764aSArtem Bityutskiy 14331e51764aSArtem Bityutskiy if (znode->level != 0 && znode->zbranch[n].znode) 14341e51764aSArtem Bityutskiy if (znode->zbranch[n].znode->parent != znode) { 14351e51764aSArtem Bityutskiy err = 20; 14361e51764aSArtem Bityutskiy goto out; 14371e51764aSArtem Bityutskiy } 14381e51764aSArtem Bityutskiy } 14391e51764aSArtem Bityutskiy 14401e51764aSArtem Bityutskiy return 0; 14411e51764aSArtem Bityutskiy 14421e51764aSArtem Bityutskiy out: 14431e51764aSArtem Bityutskiy ubifs_err("failed, error %d", err); 14441e51764aSArtem Bityutskiy ubifs_msg("dump of the znode"); 14451e51764aSArtem Bityutskiy dbg_dump_znode(c, znode); 14461e51764aSArtem Bityutskiy if (zp) { 14471e51764aSArtem Bityutskiy ubifs_msg("dump of the parent znode"); 14481e51764aSArtem Bityutskiy dbg_dump_znode(c, zp); 14491e51764aSArtem Bityutskiy } 14501e51764aSArtem Bityutskiy dump_stack(); 14511e51764aSArtem Bityutskiy return -EINVAL; 14521e51764aSArtem Bityutskiy } 14531e51764aSArtem Bityutskiy 14541e51764aSArtem Bityutskiy /** 14551e51764aSArtem Bityutskiy * dbg_check_tnc - check TNC tree. 14561e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 14571e51764aSArtem Bityutskiy * @extra: do extra checks that are possible at start commit 14581e51764aSArtem Bityutskiy * 14591e51764aSArtem Bityutskiy * This function traverses whole TNC tree and checks every znode. Returns zero 14601e51764aSArtem Bityutskiy * if everything is all right and %-EINVAL if something is wrong with TNC. 14611e51764aSArtem Bityutskiy */ 14621e51764aSArtem Bityutskiy int dbg_check_tnc(struct ubifs_info *c, int extra) 14631e51764aSArtem Bityutskiy { 14641e51764aSArtem Bityutskiy struct ubifs_znode *znode; 14651e51764aSArtem Bityutskiy long clean_cnt = 0, dirty_cnt = 0; 14661e51764aSArtem Bityutskiy int err, last; 14671e51764aSArtem Bityutskiy 14681e51764aSArtem Bityutskiy if (!(ubifs_chk_flags & UBIFS_CHK_TNC)) 14691e51764aSArtem Bityutskiy return 0; 14701e51764aSArtem Bityutskiy 14711e51764aSArtem Bityutskiy ubifs_assert(mutex_is_locked(&c->tnc_mutex)); 14721e51764aSArtem Bityutskiy if (!c->zroot.znode) 14731e51764aSArtem Bityutskiy return 0; 14741e51764aSArtem Bityutskiy 14751e51764aSArtem Bityutskiy znode = ubifs_tnc_postorder_first(c->zroot.znode); 14761e51764aSArtem Bityutskiy while (1) { 14771e51764aSArtem Bityutskiy struct ubifs_znode *prev; 14781e51764aSArtem Bityutskiy struct ubifs_zbranch *zbr; 14791e51764aSArtem Bityutskiy 14801e51764aSArtem Bityutskiy if (!znode->parent) 14811e51764aSArtem Bityutskiy zbr = &c->zroot; 14821e51764aSArtem Bityutskiy else 14831e51764aSArtem Bityutskiy zbr = &znode->parent->zbranch[znode->iip]; 14841e51764aSArtem Bityutskiy 14851e51764aSArtem Bityutskiy err = dbg_check_znode(c, zbr); 14861e51764aSArtem Bityutskiy if (err) 14871e51764aSArtem Bityutskiy return err; 14881e51764aSArtem Bityutskiy 14891e51764aSArtem Bityutskiy if (extra) { 14901e51764aSArtem Bityutskiy if (ubifs_zn_dirty(znode)) 14911e51764aSArtem Bityutskiy dirty_cnt += 1; 14921e51764aSArtem Bityutskiy else 14931e51764aSArtem Bityutskiy clean_cnt += 1; 14941e51764aSArtem Bityutskiy } 14951e51764aSArtem Bityutskiy 14961e51764aSArtem Bityutskiy prev = znode; 14971e51764aSArtem Bityutskiy znode = ubifs_tnc_postorder_next(znode); 14981e51764aSArtem Bityutskiy if (!znode) 14991e51764aSArtem Bityutskiy break; 15001e51764aSArtem Bityutskiy 15011e51764aSArtem Bityutskiy /* 15021e51764aSArtem Bityutskiy * If the last key of this znode is equivalent to the first key 15031e51764aSArtem Bityutskiy * of the next znode (collision), then check order of the keys. 15041e51764aSArtem Bityutskiy */ 15051e51764aSArtem Bityutskiy last = prev->child_cnt - 1; 15061e51764aSArtem Bityutskiy if (prev->level == 0 && znode->level == 0 && !c->replaying && 15071e51764aSArtem Bityutskiy !keys_cmp(c, &prev->zbranch[last].key, 15081e51764aSArtem Bityutskiy &znode->zbranch[0].key)) { 15091e51764aSArtem Bityutskiy err = dbg_check_key_order(c, &prev->zbranch[last], 15101e51764aSArtem Bityutskiy &znode->zbranch[0]); 15111e51764aSArtem Bityutskiy if (err < 0) 15121e51764aSArtem Bityutskiy return err; 15131e51764aSArtem Bityutskiy if (err) { 15141e51764aSArtem Bityutskiy ubifs_msg("first znode"); 15151e51764aSArtem Bityutskiy dbg_dump_znode(c, prev); 15161e51764aSArtem Bityutskiy ubifs_msg("second znode"); 15171e51764aSArtem Bityutskiy dbg_dump_znode(c, znode); 15181e51764aSArtem Bityutskiy return -EINVAL; 15191e51764aSArtem Bityutskiy } 15201e51764aSArtem Bityutskiy } 15211e51764aSArtem Bityutskiy } 15221e51764aSArtem Bityutskiy 15231e51764aSArtem Bityutskiy if (extra) { 15241e51764aSArtem Bityutskiy if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) { 15251e51764aSArtem Bityutskiy ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld", 15261e51764aSArtem Bityutskiy atomic_long_read(&c->clean_zn_cnt), 15271e51764aSArtem Bityutskiy clean_cnt); 15281e51764aSArtem Bityutskiy return -EINVAL; 15291e51764aSArtem Bityutskiy } 15301e51764aSArtem Bityutskiy if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) { 15311e51764aSArtem Bityutskiy ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld", 15321e51764aSArtem Bityutskiy atomic_long_read(&c->dirty_zn_cnt), 15331e51764aSArtem Bityutskiy dirty_cnt); 15341e51764aSArtem Bityutskiy return -EINVAL; 15351e51764aSArtem Bityutskiy } 15361e51764aSArtem Bityutskiy } 15371e51764aSArtem Bityutskiy 15381e51764aSArtem Bityutskiy return 0; 15391e51764aSArtem Bityutskiy } 15401e51764aSArtem Bityutskiy 15411e51764aSArtem Bityutskiy /** 15421e51764aSArtem Bityutskiy * dbg_walk_index - walk the on-flash index. 15431e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 15441e51764aSArtem Bityutskiy * @leaf_cb: called for each leaf node 15451e51764aSArtem Bityutskiy * @znode_cb: called for each indexing node 1546227c75c9SAdrian Hunter * @priv: private data which is passed to callbacks 15471e51764aSArtem Bityutskiy * 15481e51764aSArtem Bityutskiy * This function walks the UBIFS index and calls the @leaf_cb for each leaf 15491e51764aSArtem Bityutskiy * node and @znode_cb for each indexing node. Returns zero in case of success 15501e51764aSArtem Bityutskiy * and a negative error code in case of failure. 15511e51764aSArtem Bityutskiy * 15521e51764aSArtem Bityutskiy * It would be better if this function removed every znode it pulled to into 15531e51764aSArtem Bityutskiy * the TNC, so that the behavior more closely matched the non-debugging 15541e51764aSArtem Bityutskiy * behavior. 15551e51764aSArtem Bityutskiy */ 15561e51764aSArtem Bityutskiy int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb, 15571e51764aSArtem Bityutskiy dbg_znode_callback znode_cb, void *priv) 15581e51764aSArtem Bityutskiy { 15591e51764aSArtem Bityutskiy int err; 15601e51764aSArtem Bityutskiy struct ubifs_zbranch *zbr; 15611e51764aSArtem Bityutskiy struct ubifs_znode *znode, *child; 15621e51764aSArtem Bityutskiy 15631e51764aSArtem Bityutskiy mutex_lock(&c->tnc_mutex); 15641e51764aSArtem Bityutskiy /* If the root indexing node is not in TNC - pull it */ 15651e51764aSArtem Bityutskiy if (!c->zroot.znode) { 15661e51764aSArtem Bityutskiy c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0); 15671e51764aSArtem Bityutskiy if (IS_ERR(c->zroot.znode)) { 15681e51764aSArtem Bityutskiy err = PTR_ERR(c->zroot.znode); 15691e51764aSArtem Bityutskiy c->zroot.znode = NULL; 15701e51764aSArtem Bityutskiy goto out_unlock; 15711e51764aSArtem Bityutskiy } 15721e51764aSArtem Bityutskiy } 15731e51764aSArtem Bityutskiy 15741e51764aSArtem Bityutskiy /* 15751e51764aSArtem Bityutskiy * We are going to traverse the indexing tree in the postorder manner. 15761e51764aSArtem Bityutskiy * Go down and find the leftmost indexing node where we are going to 15771e51764aSArtem Bityutskiy * start from. 15781e51764aSArtem Bityutskiy */ 15791e51764aSArtem Bityutskiy znode = c->zroot.znode; 15801e51764aSArtem Bityutskiy while (znode->level > 0) { 15811e51764aSArtem Bityutskiy zbr = &znode->zbranch[0]; 15821e51764aSArtem Bityutskiy child = zbr->znode; 15831e51764aSArtem Bityutskiy if (!child) { 15841e51764aSArtem Bityutskiy child = ubifs_load_znode(c, zbr, znode, 0); 15851e51764aSArtem Bityutskiy if (IS_ERR(child)) { 15861e51764aSArtem Bityutskiy err = PTR_ERR(child); 15871e51764aSArtem Bityutskiy goto out_unlock; 15881e51764aSArtem Bityutskiy } 15891e51764aSArtem Bityutskiy zbr->znode = child; 15901e51764aSArtem Bityutskiy } 15911e51764aSArtem Bityutskiy 15921e51764aSArtem Bityutskiy znode = child; 15931e51764aSArtem Bityutskiy } 15941e51764aSArtem Bityutskiy 15951e51764aSArtem Bityutskiy /* Iterate over all indexing nodes */ 15961e51764aSArtem Bityutskiy while (1) { 15971e51764aSArtem Bityutskiy int idx; 15981e51764aSArtem Bityutskiy 15991e51764aSArtem Bityutskiy cond_resched(); 16001e51764aSArtem Bityutskiy 16011e51764aSArtem Bityutskiy if (znode_cb) { 16021e51764aSArtem Bityutskiy err = znode_cb(c, znode, priv); 16031e51764aSArtem Bityutskiy if (err) { 16041e51764aSArtem Bityutskiy ubifs_err("znode checking function returned " 16051e51764aSArtem Bityutskiy "error %d", err); 16061e51764aSArtem Bityutskiy dbg_dump_znode(c, znode); 16071e51764aSArtem Bityutskiy goto out_dump; 16081e51764aSArtem Bityutskiy } 16091e51764aSArtem Bityutskiy } 16101e51764aSArtem Bityutskiy if (leaf_cb && znode->level == 0) { 16111e51764aSArtem Bityutskiy for (idx = 0; idx < znode->child_cnt; idx++) { 16121e51764aSArtem Bityutskiy zbr = &znode->zbranch[idx]; 16131e51764aSArtem Bityutskiy err = leaf_cb(c, zbr, priv); 16141e51764aSArtem Bityutskiy if (err) { 16151e51764aSArtem Bityutskiy ubifs_err("leaf checking function " 16161e51764aSArtem Bityutskiy "returned error %d, for leaf " 16171e51764aSArtem Bityutskiy "at LEB %d:%d", 16181e51764aSArtem Bityutskiy err, zbr->lnum, zbr->offs); 16191e51764aSArtem Bityutskiy goto out_dump; 16201e51764aSArtem Bityutskiy } 16211e51764aSArtem Bityutskiy } 16221e51764aSArtem Bityutskiy } 16231e51764aSArtem Bityutskiy 16241e51764aSArtem Bityutskiy if (!znode->parent) 16251e51764aSArtem Bityutskiy break; 16261e51764aSArtem Bityutskiy 16271e51764aSArtem Bityutskiy idx = znode->iip + 1; 16281e51764aSArtem Bityutskiy znode = znode->parent; 16291e51764aSArtem Bityutskiy if (idx < znode->child_cnt) { 16301e51764aSArtem Bityutskiy /* Switch to the next index in the parent */ 16311e51764aSArtem Bityutskiy zbr = &znode->zbranch[idx]; 16321e51764aSArtem Bityutskiy child = zbr->znode; 16331e51764aSArtem Bityutskiy if (!child) { 16341e51764aSArtem Bityutskiy child = ubifs_load_znode(c, zbr, znode, idx); 16351e51764aSArtem Bityutskiy if (IS_ERR(child)) { 16361e51764aSArtem Bityutskiy err = PTR_ERR(child); 16371e51764aSArtem Bityutskiy goto out_unlock; 16381e51764aSArtem Bityutskiy } 16391e51764aSArtem Bityutskiy zbr->znode = child; 16401e51764aSArtem Bityutskiy } 16411e51764aSArtem Bityutskiy znode = child; 16421e51764aSArtem Bityutskiy } else 16431e51764aSArtem Bityutskiy /* 16441e51764aSArtem Bityutskiy * This is the last child, switch to the parent and 16451e51764aSArtem Bityutskiy * continue. 16461e51764aSArtem Bityutskiy */ 16471e51764aSArtem Bityutskiy continue; 16481e51764aSArtem Bityutskiy 16491e51764aSArtem Bityutskiy /* Go to the lowest leftmost znode in the new sub-tree */ 16501e51764aSArtem Bityutskiy while (znode->level > 0) { 16511e51764aSArtem Bityutskiy zbr = &znode->zbranch[0]; 16521e51764aSArtem Bityutskiy child = zbr->znode; 16531e51764aSArtem Bityutskiy if (!child) { 16541e51764aSArtem Bityutskiy child = ubifs_load_znode(c, zbr, znode, 0); 16551e51764aSArtem Bityutskiy if (IS_ERR(child)) { 16561e51764aSArtem Bityutskiy err = PTR_ERR(child); 16571e51764aSArtem Bityutskiy goto out_unlock; 16581e51764aSArtem Bityutskiy } 16591e51764aSArtem Bityutskiy zbr->znode = child; 16601e51764aSArtem Bityutskiy } 16611e51764aSArtem Bityutskiy znode = child; 16621e51764aSArtem Bityutskiy } 16631e51764aSArtem Bityutskiy } 16641e51764aSArtem Bityutskiy 16651e51764aSArtem Bityutskiy mutex_unlock(&c->tnc_mutex); 16661e51764aSArtem Bityutskiy return 0; 16671e51764aSArtem Bityutskiy 16681e51764aSArtem Bityutskiy out_dump: 16691e51764aSArtem Bityutskiy if (znode->parent) 16701e51764aSArtem Bityutskiy zbr = &znode->parent->zbranch[znode->iip]; 16711e51764aSArtem Bityutskiy else 16721e51764aSArtem Bityutskiy zbr = &c->zroot; 16731e51764aSArtem Bityutskiy ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs); 16741e51764aSArtem Bityutskiy dbg_dump_znode(c, znode); 16751e51764aSArtem Bityutskiy out_unlock: 16761e51764aSArtem Bityutskiy mutex_unlock(&c->tnc_mutex); 16771e51764aSArtem Bityutskiy return err; 16781e51764aSArtem Bityutskiy } 16791e51764aSArtem Bityutskiy 16801e51764aSArtem Bityutskiy /** 16811e51764aSArtem Bityutskiy * add_size - add znode size to partially calculated index size. 16821e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 16831e51764aSArtem Bityutskiy * @znode: znode to add size for 16841e51764aSArtem Bityutskiy * @priv: partially calculated index size 16851e51764aSArtem Bityutskiy * 16861e51764aSArtem Bityutskiy * This is a helper function for 'dbg_check_idx_size()' which is called for 16871e51764aSArtem Bityutskiy * every indexing node and adds its size to the 'long long' variable pointed to 16881e51764aSArtem Bityutskiy * by @priv. 16891e51764aSArtem Bityutskiy */ 16901e51764aSArtem Bityutskiy static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv) 16911e51764aSArtem Bityutskiy { 16921e51764aSArtem Bityutskiy long long *idx_size = priv; 16931e51764aSArtem Bityutskiy int add; 16941e51764aSArtem Bityutskiy 16951e51764aSArtem Bityutskiy add = ubifs_idx_node_sz(c, znode->child_cnt); 16961e51764aSArtem Bityutskiy add = ALIGN(add, 8); 16971e51764aSArtem Bityutskiy *idx_size += add; 16981e51764aSArtem Bityutskiy return 0; 16991e51764aSArtem Bityutskiy } 17001e51764aSArtem Bityutskiy 17011e51764aSArtem Bityutskiy /** 17021e51764aSArtem Bityutskiy * dbg_check_idx_size - check index size. 17031e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 17041e51764aSArtem Bityutskiy * @idx_size: size to check 17051e51764aSArtem Bityutskiy * 17061e51764aSArtem Bityutskiy * This function walks the UBIFS index, calculates its size and checks that the 17071e51764aSArtem Bityutskiy * size is equivalent to @idx_size. Returns zero in case of success and a 17081e51764aSArtem Bityutskiy * negative error code in case of failure. 17091e51764aSArtem Bityutskiy */ 17101e51764aSArtem Bityutskiy int dbg_check_idx_size(struct ubifs_info *c, long long idx_size) 17111e51764aSArtem Bityutskiy { 17121e51764aSArtem Bityutskiy int err; 17131e51764aSArtem Bityutskiy long long calc = 0; 17141e51764aSArtem Bityutskiy 17151e51764aSArtem Bityutskiy if (!(ubifs_chk_flags & UBIFS_CHK_IDX_SZ)) 17161e51764aSArtem Bityutskiy return 0; 17171e51764aSArtem Bityutskiy 17181e51764aSArtem Bityutskiy err = dbg_walk_index(c, NULL, add_size, &calc); 17191e51764aSArtem Bityutskiy if (err) { 17201e51764aSArtem Bityutskiy ubifs_err("error %d while walking the index", err); 17211e51764aSArtem Bityutskiy return err; 17221e51764aSArtem Bityutskiy } 17231e51764aSArtem Bityutskiy 17241e51764aSArtem Bityutskiy if (calc != idx_size) { 17251e51764aSArtem Bityutskiy ubifs_err("index size check failed: calculated size is %lld, " 17261e51764aSArtem Bityutskiy "should be %lld", calc, idx_size); 17271e51764aSArtem Bityutskiy dump_stack(); 17281e51764aSArtem Bityutskiy return -EINVAL; 17291e51764aSArtem Bityutskiy } 17301e51764aSArtem Bityutskiy 17311e51764aSArtem Bityutskiy return 0; 17321e51764aSArtem Bityutskiy } 17331e51764aSArtem Bityutskiy 17341e51764aSArtem Bityutskiy /** 17351e51764aSArtem Bityutskiy * struct fsck_inode - information about an inode used when checking the file-system. 17361e51764aSArtem Bityutskiy * @rb: link in the RB-tree of inodes 17371e51764aSArtem Bityutskiy * @inum: inode number 17381e51764aSArtem Bityutskiy * @mode: inode type, permissions, etc 17391e51764aSArtem Bityutskiy * @nlink: inode link count 17401e51764aSArtem Bityutskiy * @xattr_cnt: count of extended attributes 17411e51764aSArtem Bityutskiy * @references: how many directory/xattr entries refer this inode (calculated 17421e51764aSArtem Bityutskiy * while walking the index) 17431e51764aSArtem Bityutskiy * @calc_cnt: for directory inode count of child directories 17441e51764aSArtem Bityutskiy * @size: inode size (read from on-flash inode) 17451e51764aSArtem Bityutskiy * @xattr_sz: summary size of all extended attributes (read from on-flash 17461e51764aSArtem Bityutskiy * inode) 17471e51764aSArtem Bityutskiy * @calc_sz: for directories calculated directory size 17481e51764aSArtem Bityutskiy * @calc_xcnt: count of extended attributes 17491e51764aSArtem Bityutskiy * @calc_xsz: calculated summary size of all extended attributes 17501e51764aSArtem Bityutskiy * @xattr_nms: sum of lengths of all extended attribute names belonging to this 17511e51764aSArtem Bityutskiy * inode (read from on-flash inode) 17521e51764aSArtem Bityutskiy * @calc_xnms: calculated sum of lengths of all extended attribute names 17531e51764aSArtem Bityutskiy */ 17541e51764aSArtem Bityutskiy struct fsck_inode { 17551e51764aSArtem Bityutskiy struct rb_node rb; 17561e51764aSArtem Bityutskiy ino_t inum; 17571e51764aSArtem Bityutskiy umode_t mode; 17581e51764aSArtem Bityutskiy unsigned int nlink; 17591e51764aSArtem Bityutskiy unsigned int xattr_cnt; 17601e51764aSArtem Bityutskiy int references; 17611e51764aSArtem Bityutskiy int calc_cnt; 17621e51764aSArtem Bityutskiy long long size; 17631e51764aSArtem Bityutskiy unsigned int xattr_sz; 17641e51764aSArtem Bityutskiy long long calc_sz; 17651e51764aSArtem Bityutskiy long long calc_xcnt; 17661e51764aSArtem Bityutskiy long long calc_xsz; 17671e51764aSArtem Bityutskiy unsigned int xattr_nms; 17681e51764aSArtem Bityutskiy long long calc_xnms; 17691e51764aSArtem Bityutskiy }; 17701e51764aSArtem Bityutskiy 17711e51764aSArtem Bityutskiy /** 17721e51764aSArtem Bityutskiy * struct fsck_data - private FS checking information. 17731e51764aSArtem Bityutskiy * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects) 17741e51764aSArtem Bityutskiy */ 17751e51764aSArtem Bityutskiy struct fsck_data { 17761e51764aSArtem Bityutskiy struct rb_root inodes; 17771e51764aSArtem Bityutskiy }; 17781e51764aSArtem Bityutskiy 17791e51764aSArtem Bityutskiy /** 17801e51764aSArtem Bityutskiy * add_inode - add inode information to RB-tree of inodes. 17811e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 17821e51764aSArtem Bityutskiy * @fsckd: FS checking information 17831e51764aSArtem Bityutskiy * @ino: raw UBIFS inode to add 17841e51764aSArtem Bityutskiy * 17851e51764aSArtem Bityutskiy * This is a helper function for 'check_leaf()' which adds information about 17861e51764aSArtem Bityutskiy * inode @ino to the RB-tree of inodes. Returns inode information pointer in 17871e51764aSArtem Bityutskiy * case of success and a negative error code in case of failure. 17881e51764aSArtem Bityutskiy */ 17891e51764aSArtem Bityutskiy static struct fsck_inode *add_inode(struct ubifs_info *c, 17901e51764aSArtem Bityutskiy struct fsck_data *fsckd, 17911e51764aSArtem Bityutskiy struct ubifs_ino_node *ino) 17921e51764aSArtem Bityutskiy { 17931e51764aSArtem Bityutskiy struct rb_node **p, *parent = NULL; 17941e51764aSArtem Bityutskiy struct fsck_inode *fscki; 17951e51764aSArtem Bityutskiy ino_t inum = key_inum_flash(c, &ino->key); 17961e51764aSArtem Bityutskiy 17971e51764aSArtem Bityutskiy p = &fsckd->inodes.rb_node; 17981e51764aSArtem Bityutskiy while (*p) { 17991e51764aSArtem Bityutskiy parent = *p; 18001e51764aSArtem Bityutskiy fscki = rb_entry(parent, struct fsck_inode, rb); 18011e51764aSArtem Bityutskiy if (inum < fscki->inum) 18021e51764aSArtem Bityutskiy p = &(*p)->rb_left; 18031e51764aSArtem Bityutskiy else if (inum > fscki->inum) 18041e51764aSArtem Bityutskiy p = &(*p)->rb_right; 18051e51764aSArtem Bityutskiy else 18061e51764aSArtem Bityutskiy return fscki; 18071e51764aSArtem Bityutskiy } 18081e51764aSArtem Bityutskiy 18091e51764aSArtem Bityutskiy if (inum > c->highest_inum) { 18101e51764aSArtem Bityutskiy ubifs_err("too high inode number, max. is %lu", 1811e84461adSArtem Bityutskiy (unsigned long)c->highest_inum); 18121e51764aSArtem Bityutskiy return ERR_PTR(-EINVAL); 18131e51764aSArtem Bityutskiy } 18141e51764aSArtem Bityutskiy 18151e51764aSArtem Bityutskiy fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS); 18161e51764aSArtem Bityutskiy if (!fscki) 18171e51764aSArtem Bityutskiy return ERR_PTR(-ENOMEM); 18181e51764aSArtem Bityutskiy 18191e51764aSArtem Bityutskiy fscki->inum = inum; 18201e51764aSArtem Bityutskiy fscki->nlink = le32_to_cpu(ino->nlink); 18211e51764aSArtem Bityutskiy fscki->size = le64_to_cpu(ino->size); 18221e51764aSArtem Bityutskiy fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt); 18231e51764aSArtem Bityutskiy fscki->xattr_sz = le32_to_cpu(ino->xattr_size); 18241e51764aSArtem Bityutskiy fscki->xattr_nms = le32_to_cpu(ino->xattr_names); 18251e51764aSArtem Bityutskiy fscki->mode = le32_to_cpu(ino->mode); 18261e51764aSArtem Bityutskiy if (S_ISDIR(fscki->mode)) { 18271e51764aSArtem Bityutskiy fscki->calc_sz = UBIFS_INO_NODE_SZ; 18281e51764aSArtem Bityutskiy fscki->calc_cnt = 2; 18291e51764aSArtem Bityutskiy } 18301e51764aSArtem Bityutskiy rb_link_node(&fscki->rb, parent, p); 18311e51764aSArtem Bityutskiy rb_insert_color(&fscki->rb, &fsckd->inodes); 18321e51764aSArtem Bityutskiy return fscki; 18331e51764aSArtem Bityutskiy } 18341e51764aSArtem Bityutskiy 18351e51764aSArtem Bityutskiy /** 18361e51764aSArtem Bityutskiy * search_inode - search inode in the RB-tree of inodes. 18371e51764aSArtem Bityutskiy * @fsckd: FS checking information 18381e51764aSArtem Bityutskiy * @inum: inode number to search 18391e51764aSArtem Bityutskiy * 18401e51764aSArtem Bityutskiy * This is a helper function for 'check_leaf()' which searches inode @inum in 18411e51764aSArtem Bityutskiy * the RB-tree of inodes and returns an inode information pointer or %NULL if 18421e51764aSArtem Bityutskiy * the inode was not found. 18431e51764aSArtem Bityutskiy */ 18441e51764aSArtem Bityutskiy static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum) 18451e51764aSArtem Bityutskiy { 18461e51764aSArtem Bityutskiy struct rb_node *p; 18471e51764aSArtem Bityutskiy struct fsck_inode *fscki; 18481e51764aSArtem Bityutskiy 18491e51764aSArtem Bityutskiy p = fsckd->inodes.rb_node; 18501e51764aSArtem Bityutskiy while (p) { 18511e51764aSArtem Bityutskiy fscki = rb_entry(p, struct fsck_inode, rb); 18521e51764aSArtem Bityutskiy if (inum < fscki->inum) 18531e51764aSArtem Bityutskiy p = p->rb_left; 18541e51764aSArtem Bityutskiy else if (inum > fscki->inum) 18551e51764aSArtem Bityutskiy p = p->rb_right; 18561e51764aSArtem Bityutskiy else 18571e51764aSArtem Bityutskiy return fscki; 18581e51764aSArtem Bityutskiy } 18591e51764aSArtem Bityutskiy return NULL; 18601e51764aSArtem Bityutskiy } 18611e51764aSArtem Bityutskiy 18621e51764aSArtem Bityutskiy /** 18631e51764aSArtem Bityutskiy * read_add_inode - read inode node and add it to RB-tree of inodes. 18641e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 18651e51764aSArtem Bityutskiy * @fsckd: FS checking information 18661e51764aSArtem Bityutskiy * @inum: inode number to read 18671e51764aSArtem Bityutskiy * 18681e51764aSArtem Bityutskiy * This is a helper function for 'check_leaf()' which finds inode node @inum in 18691e51764aSArtem Bityutskiy * the index, reads it, and adds it to the RB-tree of inodes. Returns inode 18701e51764aSArtem Bityutskiy * information pointer in case of success and a negative error code in case of 18711e51764aSArtem Bityutskiy * failure. 18721e51764aSArtem Bityutskiy */ 18731e51764aSArtem Bityutskiy static struct fsck_inode *read_add_inode(struct ubifs_info *c, 18741e51764aSArtem Bityutskiy struct fsck_data *fsckd, ino_t inum) 18751e51764aSArtem Bityutskiy { 18761e51764aSArtem Bityutskiy int n, err; 18771e51764aSArtem Bityutskiy union ubifs_key key; 18781e51764aSArtem Bityutskiy struct ubifs_znode *znode; 18791e51764aSArtem Bityutskiy struct ubifs_zbranch *zbr; 18801e51764aSArtem Bityutskiy struct ubifs_ino_node *ino; 18811e51764aSArtem Bityutskiy struct fsck_inode *fscki; 18821e51764aSArtem Bityutskiy 18831e51764aSArtem Bityutskiy fscki = search_inode(fsckd, inum); 18841e51764aSArtem Bityutskiy if (fscki) 18851e51764aSArtem Bityutskiy return fscki; 18861e51764aSArtem Bityutskiy 18871e51764aSArtem Bityutskiy ino_key_init(c, &key, inum); 18881e51764aSArtem Bityutskiy err = ubifs_lookup_level0(c, &key, &znode, &n); 18891e51764aSArtem Bityutskiy if (!err) { 1890e84461adSArtem Bityutskiy ubifs_err("inode %lu not found in index", (unsigned long)inum); 18911e51764aSArtem Bityutskiy return ERR_PTR(-ENOENT); 18921e51764aSArtem Bityutskiy } else if (err < 0) { 1893e84461adSArtem Bityutskiy ubifs_err("error %d while looking up inode %lu", 1894e84461adSArtem Bityutskiy err, (unsigned long)inum); 18951e51764aSArtem Bityutskiy return ERR_PTR(err); 18961e51764aSArtem Bityutskiy } 18971e51764aSArtem Bityutskiy 18981e51764aSArtem Bityutskiy zbr = &znode->zbranch[n]; 18991e51764aSArtem Bityutskiy if (zbr->len < UBIFS_INO_NODE_SZ) { 1900e84461adSArtem Bityutskiy ubifs_err("bad node %lu node length %d", 1901e84461adSArtem Bityutskiy (unsigned long)inum, zbr->len); 19021e51764aSArtem Bityutskiy return ERR_PTR(-EINVAL); 19031e51764aSArtem Bityutskiy } 19041e51764aSArtem Bityutskiy 19051e51764aSArtem Bityutskiy ino = kmalloc(zbr->len, GFP_NOFS); 19061e51764aSArtem Bityutskiy if (!ino) 19071e51764aSArtem Bityutskiy return ERR_PTR(-ENOMEM); 19081e51764aSArtem Bityutskiy 19091e51764aSArtem Bityutskiy err = ubifs_tnc_read_node(c, zbr, ino); 19101e51764aSArtem Bityutskiy if (err) { 19111e51764aSArtem Bityutskiy ubifs_err("cannot read inode node at LEB %d:%d, error %d", 19121e51764aSArtem Bityutskiy zbr->lnum, zbr->offs, err); 19131e51764aSArtem Bityutskiy kfree(ino); 19141e51764aSArtem Bityutskiy return ERR_PTR(err); 19151e51764aSArtem Bityutskiy } 19161e51764aSArtem Bityutskiy 19171e51764aSArtem Bityutskiy fscki = add_inode(c, fsckd, ino); 19181e51764aSArtem Bityutskiy kfree(ino); 19191e51764aSArtem Bityutskiy if (IS_ERR(fscki)) { 19201e51764aSArtem Bityutskiy ubifs_err("error %ld while adding inode %lu node", 1921e84461adSArtem Bityutskiy PTR_ERR(fscki), (unsigned long)inum); 19221e51764aSArtem Bityutskiy return fscki; 19231e51764aSArtem Bityutskiy } 19241e51764aSArtem Bityutskiy 19251e51764aSArtem Bityutskiy return fscki; 19261e51764aSArtem Bityutskiy } 19271e51764aSArtem Bityutskiy 19281e51764aSArtem Bityutskiy /** 19291e51764aSArtem Bityutskiy * check_leaf - check leaf node. 19301e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 19311e51764aSArtem Bityutskiy * @zbr: zbranch of the leaf node to check 19321e51764aSArtem Bityutskiy * @priv: FS checking information 19331e51764aSArtem Bityutskiy * 19341e51764aSArtem Bityutskiy * This is a helper function for 'dbg_check_filesystem()' which is called for 19351e51764aSArtem Bityutskiy * every single leaf node while walking the indexing tree. It checks that the 19361e51764aSArtem Bityutskiy * leaf node referred from the indexing tree exists, has correct CRC, and does 19371e51764aSArtem Bityutskiy * some other basic validation. This function is also responsible for building 19381e51764aSArtem Bityutskiy * an RB-tree of inodes - it adds all inodes into the RB-tree. It also 19391e51764aSArtem Bityutskiy * calculates reference count, size, etc for each inode in order to later 19401e51764aSArtem Bityutskiy * compare them to the information stored inside the inodes and detect possible 19411e51764aSArtem Bityutskiy * inconsistencies. Returns zero in case of success and a negative error code 19421e51764aSArtem Bityutskiy * in case of failure. 19431e51764aSArtem Bityutskiy */ 19441e51764aSArtem Bityutskiy static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr, 19451e51764aSArtem Bityutskiy void *priv) 19461e51764aSArtem Bityutskiy { 19471e51764aSArtem Bityutskiy ino_t inum; 19481e51764aSArtem Bityutskiy void *node; 19491e51764aSArtem Bityutskiy struct ubifs_ch *ch; 19501e51764aSArtem Bityutskiy int err, type = key_type(c, &zbr->key); 19511e51764aSArtem Bityutskiy struct fsck_inode *fscki; 19521e51764aSArtem Bityutskiy 19531e51764aSArtem Bityutskiy if (zbr->len < UBIFS_CH_SZ) { 19541e51764aSArtem Bityutskiy ubifs_err("bad leaf length %d (LEB %d:%d)", 19551e51764aSArtem Bityutskiy zbr->len, zbr->lnum, zbr->offs); 19561e51764aSArtem Bityutskiy return -EINVAL; 19571e51764aSArtem Bityutskiy } 19581e51764aSArtem Bityutskiy 19591e51764aSArtem Bityutskiy node = kmalloc(zbr->len, GFP_NOFS); 19601e51764aSArtem Bityutskiy if (!node) 19611e51764aSArtem Bityutskiy return -ENOMEM; 19621e51764aSArtem Bityutskiy 19631e51764aSArtem Bityutskiy err = ubifs_tnc_read_node(c, zbr, node); 19641e51764aSArtem Bityutskiy if (err) { 19651e51764aSArtem Bityutskiy ubifs_err("cannot read leaf node at LEB %d:%d, error %d", 19661e51764aSArtem Bityutskiy zbr->lnum, zbr->offs, err); 19671e51764aSArtem Bityutskiy goto out_free; 19681e51764aSArtem Bityutskiy } 19691e51764aSArtem Bityutskiy 19701e51764aSArtem Bityutskiy /* If this is an inode node, add it to RB-tree of inodes */ 19711e51764aSArtem Bityutskiy if (type == UBIFS_INO_KEY) { 19721e51764aSArtem Bityutskiy fscki = add_inode(c, priv, node); 19731e51764aSArtem Bityutskiy if (IS_ERR(fscki)) { 19741e51764aSArtem Bityutskiy err = PTR_ERR(fscki); 19751e51764aSArtem Bityutskiy ubifs_err("error %d while adding inode node", err); 19761e51764aSArtem Bityutskiy goto out_dump; 19771e51764aSArtem Bityutskiy } 19781e51764aSArtem Bityutskiy goto out; 19791e51764aSArtem Bityutskiy } 19801e51764aSArtem Bityutskiy 19811e51764aSArtem Bityutskiy if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY && 19821e51764aSArtem Bityutskiy type != UBIFS_DATA_KEY) { 19831e51764aSArtem Bityutskiy ubifs_err("unexpected node type %d at LEB %d:%d", 19841e51764aSArtem Bityutskiy type, zbr->lnum, zbr->offs); 19851e51764aSArtem Bityutskiy err = -EINVAL; 19861e51764aSArtem Bityutskiy goto out_free; 19871e51764aSArtem Bityutskiy } 19881e51764aSArtem Bityutskiy 19891e51764aSArtem Bityutskiy ch = node; 19901e51764aSArtem Bityutskiy if (le64_to_cpu(ch->sqnum) > c->max_sqnum) { 19911e51764aSArtem Bityutskiy ubifs_err("too high sequence number, max. is %llu", 19921e51764aSArtem Bityutskiy c->max_sqnum); 19931e51764aSArtem Bityutskiy err = -EINVAL; 19941e51764aSArtem Bityutskiy goto out_dump; 19951e51764aSArtem Bityutskiy } 19961e51764aSArtem Bityutskiy 19971e51764aSArtem Bityutskiy if (type == UBIFS_DATA_KEY) { 19981e51764aSArtem Bityutskiy long long blk_offs; 19991e51764aSArtem Bityutskiy struct ubifs_data_node *dn = node; 20001e51764aSArtem Bityutskiy 20011e51764aSArtem Bityutskiy /* 20021e51764aSArtem Bityutskiy * Search the inode node this data node belongs to and insert 20031e51764aSArtem Bityutskiy * it to the RB-tree of inodes. 20041e51764aSArtem Bityutskiy */ 20051e51764aSArtem Bityutskiy inum = key_inum_flash(c, &dn->key); 20061e51764aSArtem Bityutskiy fscki = read_add_inode(c, priv, inum); 20071e51764aSArtem Bityutskiy if (IS_ERR(fscki)) { 20081e51764aSArtem Bityutskiy err = PTR_ERR(fscki); 20091e51764aSArtem Bityutskiy ubifs_err("error %d while processing data node and " 2010e84461adSArtem Bityutskiy "trying to find inode node %lu", 2011e84461adSArtem Bityutskiy err, (unsigned long)inum); 20121e51764aSArtem Bityutskiy goto out_dump; 20131e51764aSArtem Bityutskiy } 20141e51764aSArtem Bityutskiy 20151e51764aSArtem Bityutskiy /* Make sure the data node is within inode size */ 20161e51764aSArtem Bityutskiy blk_offs = key_block_flash(c, &dn->key); 20171e51764aSArtem Bityutskiy blk_offs <<= UBIFS_BLOCK_SHIFT; 20181e51764aSArtem Bityutskiy blk_offs += le32_to_cpu(dn->size); 20191e51764aSArtem Bityutskiy if (blk_offs > fscki->size) { 20201e51764aSArtem Bityutskiy ubifs_err("data node at LEB %d:%d is not within inode " 20211e51764aSArtem Bityutskiy "size %lld", zbr->lnum, zbr->offs, 20221e51764aSArtem Bityutskiy fscki->size); 20231e51764aSArtem Bityutskiy err = -EINVAL; 20241e51764aSArtem Bityutskiy goto out_dump; 20251e51764aSArtem Bityutskiy } 20261e51764aSArtem Bityutskiy } else { 20271e51764aSArtem Bityutskiy int nlen; 20281e51764aSArtem Bityutskiy struct ubifs_dent_node *dent = node; 20291e51764aSArtem Bityutskiy struct fsck_inode *fscki1; 20301e51764aSArtem Bityutskiy 20311e51764aSArtem Bityutskiy err = ubifs_validate_entry(c, dent); 20321e51764aSArtem Bityutskiy if (err) 20331e51764aSArtem Bityutskiy goto out_dump; 20341e51764aSArtem Bityutskiy 20351e51764aSArtem Bityutskiy /* 20361e51764aSArtem Bityutskiy * Search the inode node this entry refers to and the parent 20371e51764aSArtem Bityutskiy * inode node and insert them to the RB-tree of inodes. 20381e51764aSArtem Bityutskiy */ 20391e51764aSArtem Bityutskiy inum = le64_to_cpu(dent->inum); 20401e51764aSArtem Bityutskiy fscki = read_add_inode(c, priv, inum); 20411e51764aSArtem Bityutskiy if (IS_ERR(fscki)) { 20421e51764aSArtem Bityutskiy err = PTR_ERR(fscki); 20431e51764aSArtem Bityutskiy ubifs_err("error %d while processing entry node and " 2044e84461adSArtem Bityutskiy "trying to find inode node %lu", 2045e84461adSArtem Bityutskiy err, (unsigned long)inum); 20461e51764aSArtem Bityutskiy goto out_dump; 20471e51764aSArtem Bityutskiy } 20481e51764aSArtem Bityutskiy 20491e51764aSArtem Bityutskiy /* Count how many direntries or xentries refers this inode */ 20501e51764aSArtem Bityutskiy fscki->references += 1; 20511e51764aSArtem Bityutskiy 20521e51764aSArtem Bityutskiy inum = key_inum_flash(c, &dent->key); 20531e51764aSArtem Bityutskiy fscki1 = read_add_inode(c, priv, inum); 20541e51764aSArtem Bityutskiy if (IS_ERR(fscki1)) { 2055b38882f5SRoel Kluin err = PTR_ERR(fscki1); 20561e51764aSArtem Bityutskiy ubifs_err("error %d while processing entry node and " 20571e51764aSArtem Bityutskiy "trying to find parent inode node %lu", 2058e84461adSArtem Bityutskiy err, (unsigned long)inum); 20591e51764aSArtem Bityutskiy goto out_dump; 20601e51764aSArtem Bityutskiy } 20611e51764aSArtem Bityutskiy 20621e51764aSArtem Bityutskiy nlen = le16_to_cpu(dent->nlen); 20631e51764aSArtem Bityutskiy if (type == UBIFS_XENT_KEY) { 20641e51764aSArtem Bityutskiy fscki1->calc_xcnt += 1; 20651e51764aSArtem Bityutskiy fscki1->calc_xsz += CALC_DENT_SIZE(nlen); 20661e51764aSArtem Bityutskiy fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size); 20671e51764aSArtem Bityutskiy fscki1->calc_xnms += nlen; 20681e51764aSArtem Bityutskiy } else { 20691e51764aSArtem Bityutskiy fscki1->calc_sz += CALC_DENT_SIZE(nlen); 20701e51764aSArtem Bityutskiy if (dent->type == UBIFS_ITYPE_DIR) 20711e51764aSArtem Bityutskiy fscki1->calc_cnt += 1; 20721e51764aSArtem Bityutskiy } 20731e51764aSArtem Bityutskiy } 20741e51764aSArtem Bityutskiy 20751e51764aSArtem Bityutskiy out: 20761e51764aSArtem Bityutskiy kfree(node); 20771e51764aSArtem Bityutskiy return 0; 20781e51764aSArtem Bityutskiy 20791e51764aSArtem Bityutskiy out_dump: 20801e51764aSArtem Bityutskiy ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs); 20811e51764aSArtem Bityutskiy dbg_dump_node(c, node); 20821e51764aSArtem Bityutskiy out_free: 20831e51764aSArtem Bityutskiy kfree(node); 20841e51764aSArtem Bityutskiy return err; 20851e51764aSArtem Bityutskiy } 20861e51764aSArtem Bityutskiy 20871e51764aSArtem Bityutskiy /** 20881e51764aSArtem Bityutskiy * free_inodes - free RB-tree of inodes. 20891e51764aSArtem Bityutskiy * @fsckd: FS checking information 20901e51764aSArtem Bityutskiy */ 20911e51764aSArtem Bityutskiy static void free_inodes(struct fsck_data *fsckd) 20921e51764aSArtem Bityutskiy { 20931e51764aSArtem Bityutskiy struct rb_node *this = fsckd->inodes.rb_node; 20941e51764aSArtem Bityutskiy struct fsck_inode *fscki; 20951e51764aSArtem Bityutskiy 20961e51764aSArtem Bityutskiy while (this) { 20971e51764aSArtem Bityutskiy if (this->rb_left) 20981e51764aSArtem Bityutskiy this = this->rb_left; 20991e51764aSArtem Bityutskiy else if (this->rb_right) 21001e51764aSArtem Bityutskiy this = this->rb_right; 21011e51764aSArtem Bityutskiy else { 21021e51764aSArtem Bityutskiy fscki = rb_entry(this, struct fsck_inode, rb); 21031e51764aSArtem Bityutskiy this = rb_parent(this); 21041e51764aSArtem Bityutskiy if (this) { 21051e51764aSArtem Bityutskiy if (this->rb_left == &fscki->rb) 21061e51764aSArtem Bityutskiy this->rb_left = NULL; 21071e51764aSArtem Bityutskiy else 21081e51764aSArtem Bityutskiy this->rb_right = NULL; 21091e51764aSArtem Bityutskiy } 21101e51764aSArtem Bityutskiy kfree(fscki); 21111e51764aSArtem Bityutskiy } 21121e51764aSArtem Bityutskiy } 21131e51764aSArtem Bityutskiy } 21141e51764aSArtem Bityutskiy 21151e51764aSArtem Bityutskiy /** 21161e51764aSArtem Bityutskiy * check_inodes - checks all inodes. 21171e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 21181e51764aSArtem Bityutskiy * @fsckd: FS checking information 21191e51764aSArtem Bityutskiy * 21201e51764aSArtem Bityutskiy * This is a helper function for 'dbg_check_filesystem()' which walks the 21211e51764aSArtem Bityutskiy * RB-tree of inodes after the index scan has been finished, and checks that 21221e51764aSArtem Bityutskiy * inode nlink, size, etc are correct. Returns zero if inodes are fine, 21231e51764aSArtem Bityutskiy * %-EINVAL if not, and a negative error code in case of failure. 21241e51764aSArtem Bityutskiy */ 21251e51764aSArtem Bityutskiy static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd) 21261e51764aSArtem Bityutskiy { 21271e51764aSArtem Bityutskiy int n, err; 21281e51764aSArtem Bityutskiy union ubifs_key key; 21291e51764aSArtem Bityutskiy struct ubifs_znode *znode; 21301e51764aSArtem Bityutskiy struct ubifs_zbranch *zbr; 21311e51764aSArtem Bityutskiy struct ubifs_ino_node *ino; 21321e51764aSArtem Bityutskiy struct fsck_inode *fscki; 21331e51764aSArtem Bityutskiy struct rb_node *this = rb_first(&fsckd->inodes); 21341e51764aSArtem Bityutskiy 21351e51764aSArtem Bityutskiy while (this) { 21361e51764aSArtem Bityutskiy fscki = rb_entry(this, struct fsck_inode, rb); 21371e51764aSArtem Bityutskiy this = rb_next(this); 21381e51764aSArtem Bityutskiy 21391e51764aSArtem Bityutskiy if (S_ISDIR(fscki->mode)) { 21401e51764aSArtem Bityutskiy /* 21411e51764aSArtem Bityutskiy * Directories have to have exactly one reference (they 21421e51764aSArtem Bityutskiy * cannot have hardlinks), although root inode is an 21431e51764aSArtem Bityutskiy * exception. 21441e51764aSArtem Bityutskiy */ 21451e51764aSArtem Bityutskiy if (fscki->inum != UBIFS_ROOT_INO && 21461e51764aSArtem Bityutskiy fscki->references != 1) { 21471e51764aSArtem Bityutskiy ubifs_err("directory inode %lu has %d " 21481e51764aSArtem Bityutskiy "direntries which refer it, but " 2149e84461adSArtem Bityutskiy "should be 1", 2150e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 21511e51764aSArtem Bityutskiy fscki->references); 21521e51764aSArtem Bityutskiy goto out_dump; 21531e51764aSArtem Bityutskiy } 21541e51764aSArtem Bityutskiy if (fscki->inum == UBIFS_ROOT_INO && 21551e51764aSArtem Bityutskiy fscki->references != 0) { 21561e51764aSArtem Bityutskiy ubifs_err("root inode %lu has non-zero (%d) " 21571e51764aSArtem Bityutskiy "direntries which refer it", 2158e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 2159e84461adSArtem Bityutskiy fscki->references); 21601e51764aSArtem Bityutskiy goto out_dump; 21611e51764aSArtem Bityutskiy } 21621e51764aSArtem Bityutskiy if (fscki->calc_sz != fscki->size) { 21631e51764aSArtem Bityutskiy ubifs_err("directory inode %lu size is %lld, " 21641e51764aSArtem Bityutskiy "but calculated size is %lld", 2165e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 2166e84461adSArtem Bityutskiy fscki->size, fscki->calc_sz); 21671e51764aSArtem Bityutskiy goto out_dump; 21681e51764aSArtem Bityutskiy } 21691e51764aSArtem Bityutskiy if (fscki->calc_cnt != fscki->nlink) { 21701e51764aSArtem Bityutskiy ubifs_err("directory inode %lu nlink is %d, " 21711e51764aSArtem Bityutskiy "but calculated nlink is %d", 2172e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 2173e84461adSArtem Bityutskiy fscki->nlink, fscki->calc_cnt); 21741e51764aSArtem Bityutskiy goto out_dump; 21751e51764aSArtem Bityutskiy } 21761e51764aSArtem Bityutskiy } else { 21771e51764aSArtem Bityutskiy if (fscki->references != fscki->nlink) { 21781e51764aSArtem Bityutskiy ubifs_err("inode %lu nlink is %d, but " 2179e84461adSArtem Bityutskiy "calculated nlink is %d", 2180e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 21811e51764aSArtem Bityutskiy fscki->nlink, fscki->references); 21821e51764aSArtem Bityutskiy goto out_dump; 21831e51764aSArtem Bityutskiy } 21841e51764aSArtem Bityutskiy } 21851e51764aSArtem Bityutskiy if (fscki->xattr_sz != fscki->calc_xsz) { 21861e51764aSArtem Bityutskiy ubifs_err("inode %lu has xattr size %u, but " 21871e51764aSArtem Bityutskiy "calculated size is %lld", 2188e84461adSArtem Bityutskiy (unsigned long)fscki->inum, fscki->xattr_sz, 21891e51764aSArtem Bityutskiy fscki->calc_xsz); 21901e51764aSArtem Bityutskiy goto out_dump; 21911e51764aSArtem Bityutskiy } 21921e51764aSArtem Bityutskiy if (fscki->xattr_cnt != fscki->calc_xcnt) { 21931e51764aSArtem Bityutskiy ubifs_err("inode %lu has %u xattrs, but " 2194e84461adSArtem Bityutskiy "calculated count is %lld", 2195e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 21961e51764aSArtem Bityutskiy fscki->xattr_cnt, fscki->calc_xcnt); 21971e51764aSArtem Bityutskiy goto out_dump; 21981e51764aSArtem Bityutskiy } 21991e51764aSArtem Bityutskiy if (fscki->xattr_nms != fscki->calc_xnms) { 22001e51764aSArtem Bityutskiy ubifs_err("inode %lu has xattr names' size %u, but " 22011e51764aSArtem Bityutskiy "calculated names' size is %lld", 2202e84461adSArtem Bityutskiy (unsigned long)fscki->inum, fscki->xattr_nms, 22031e51764aSArtem Bityutskiy fscki->calc_xnms); 22041e51764aSArtem Bityutskiy goto out_dump; 22051e51764aSArtem Bityutskiy } 22061e51764aSArtem Bityutskiy } 22071e51764aSArtem Bityutskiy 22081e51764aSArtem Bityutskiy return 0; 22091e51764aSArtem Bityutskiy 22101e51764aSArtem Bityutskiy out_dump: 22111e51764aSArtem Bityutskiy /* Read the bad inode and dump it */ 22121e51764aSArtem Bityutskiy ino_key_init(c, &key, fscki->inum); 22131e51764aSArtem Bityutskiy err = ubifs_lookup_level0(c, &key, &znode, &n); 22141e51764aSArtem Bityutskiy if (!err) { 2215e84461adSArtem Bityutskiy ubifs_err("inode %lu not found in index", 2216e84461adSArtem Bityutskiy (unsigned long)fscki->inum); 22171e51764aSArtem Bityutskiy return -ENOENT; 22181e51764aSArtem Bityutskiy } else if (err < 0) { 22191e51764aSArtem Bityutskiy ubifs_err("error %d while looking up inode %lu", 2220e84461adSArtem Bityutskiy err, (unsigned long)fscki->inum); 22211e51764aSArtem Bityutskiy return err; 22221e51764aSArtem Bityutskiy } 22231e51764aSArtem Bityutskiy 22241e51764aSArtem Bityutskiy zbr = &znode->zbranch[n]; 22251e51764aSArtem Bityutskiy ino = kmalloc(zbr->len, GFP_NOFS); 22261e51764aSArtem Bityutskiy if (!ino) 22271e51764aSArtem Bityutskiy return -ENOMEM; 22281e51764aSArtem Bityutskiy 22291e51764aSArtem Bityutskiy err = ubifs_tnc_read_node(c, zbr, ino); 22301e51764aSArtem Bityutskiy if (err) { 22311e51764aSArtem Bityutskiy ubifs_err("cannot read inode node at LEB %d:%d, error %d", 22321e51764aSArtem Bityutskiy zbr->lnum, zbr->offs, err); 22331e51764aSArtem Bityutskiy kfree(ino); 22341e51764aSArtem Bityutskiy return err; 22351e51764aSArtem Bityutskiy } 22361e51764aSArtem Bityutskiy 22371e51764aSArtem Bityutskiy ubifs_msg("dump of the inode %lu sitting in LEB %d:%d", 2238e84461adSArtem Bityutskiy (unsigned long)fscki->inum, zbr->lnum, zbr->offs); 22391e51764aSArtem Bityutskiy dbg_dump_node(c, ino); 22401e51764aSArtem Bityutskiy kfree(ino); 22411e51764aSArtem Bityutskiy return -EINVAL; 22421e51764aSArtem Bityutskiy } 22431e51764aSArtem Bityutskiy 22441e51764aSArtem Bityutskiy /** 22451e51764aSArtem Bityutskiy * dbg_check_filesystem - check the file-system. 22461e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 22471e51764aSArtem Bityutskiy * 22481e51764aSArtem Bityutskiy * This function checks the file system, namely: 22491e51764aSArtem Bityutskiy * o makes sure that all leaf nodes exist and their CRCs are correct; 22501e51764aSArtem Bityutskiy * o makes sure inode nlink, size, xattr size/count are correct (for all 22511e51764aSArtem Bityutskiy * inodes). 22521e51764aSArtem Bityutskiy * 22531e51764aSArtem Bityutskiy * The function reads whole indexing tree and all nodes, so it is pretty 22541e51764aSArtem Bityutskiy * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if 22551e51764aSArtem Bityutskiy * not, and a negative error code in case of failure. 22561e51764aSArtem Bityutskiy */ 22571e51764aSArtem Bityutskiy int dbg_check_filesystem(struct ubifs_info *c) 22581e51764aSArtem Bityutskiy { 22591e51764aSArtem Bityutskiy int err; 22601e51764aSArtem Bityutskiy struct fsck_data fsckd; 22611e51764aSArtem Bityutskiy 22621e51764aSArtem Bityutskiy if (!(ubifs_chk_flags & UBIFS_CHK_FS)) 22631e51764aSArtem Bityutskiy return 0; 22641e51764aSArtem Bityutskiy 22651e51764aSArtem Bityutskiy fsckd.inodes = RB_ROOT; 22661e51764aSArtem Bityutskiy err = dbg_walk_index(c, check_leaf, NULL, &fsckd); 22671e51764aSArtem Bityutskiy if (err) 22681e51764aSArtem Bityutskiy goto out_free; 22691e51764aSArtem Bityutskiy 22701e51764aSArtem Bityutskiy err = check_inodes(c, &fsckd); 22711e51764aSArtem Bityutskiy if (err) 22721e51764aSArtem Bityutskiy goto out_free; 22731e51764aSArtem Bityutskiy 22741e51764aSArtem Bityutskiy free_inodes(&fsckd); 22751e51764aSArtem Bityutskiy return 0; 22761e51764aSArtem Bityutskiy 22771e51764aSArtem Bityutskiy out_free: 22781e51764aSArtem Bityutskiy ubifs_err("file-system check failed with error %d", err); 22791e51764aSArtem Bityutskiy dump_stack(); 22801e51764aSArtem Bityutskiy free_inodes(&fsckd); 22811e51764aSArtem Bityutskiy return err; 22821e51764aSArtem Bityutskiy } 22831e51764aSArtem Bityutskiy 22843bb66b47SArtem Bityutskiy /** 22853bb66b47SArtem Bityutskiy * dbg_check_data_nodes_order - check that list of data nodes is sorted. 22863bb66b47SArtem Bityutskiy * @c: UBIFS file-system description object 22873bb66b47SArtem Bityutskiy * @head: the list of nodes ('struct ubifs_scan_node' objects) 22883bb66b47SArtem Bityutskiy * 22893bb66b47SArtem Bityutskiy * This function returns zero if the list of data nodes is sorted correctly, 22903bb66b47SArtem Bityutskiy * and %-EINVAL if not. 22913bb66b47SArtem Bityutskiy */ 22923bb66b47SArtem Bityutskiy int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head) 22933bb66b47SArtem Bityutskiy { 22943bb66b47SArtem Bityutskiy struct list_head *cur; 22953bb66b47SArtem Bityutskiy struct ubifs_scan_node *sa, *sb; 22963bb66b47SArtem Bityutskiy 22973bb66b47SArtem Bityutskiy if (!(ubifs_chk_flags & UBIFS_CHK_GEN)) 22983bb66b47SArtem Bityutskiy return 0; 22993bb66b47SArtem Bityutskiy 23003bb66b47SArtem Bityutskiy for (cur = head->next; cur->next != head; cur = cur->next) { 23013bb66b47SArtem Bityutskiy ino_t inuma, inumb; 23023bb66b47SArtem Bityutskiy uint32_t blka, blkb; 23033bb66b47SArtem Bityutskiy 23043bb66b47SArtem Bityutskiy cond_resched(); 23053bb66b47SArtem Bityutskiy sa = container_of(cur, struct ubifs_scan_node, list); 23063bb66b47SArtem Bityutskiy sb = container_of(cur->next, struct ubifs_scan_node, list); 23073bb66b47SArtem Bityutskiy 23083bb66b47SArtem Bityutskiy if (sa->type != UBIFS_DATA_NODE) { 23093bb66b47SArtem Bityutskiy ubifs_err("bad node type %d", sa->type); 23103bb66b47SArtem Bityutskiy dbg_dump_node(c, sa->node); 23113bb66b47SArtem Bityutskiy return -EINVAL; 23123bb66b47SArtem Bityutskiy } 23133bb66b47SArtem Bityutskiy if (sb->type != UBIFS_DATA_NODE) { 23143bb66b47SArtem Bityutskiy ubifs_err("bad node type %d", sb->type); 23153bb66b47SArtem Bityutskiy dbg_dump_node(c, sb->node); 23163bb66b47SArtem Bityutskiy return -EINVAL; 23173bb66b47SArtem Bityutskiy } 23183bb66b47SArtem Bityutskiy 23193bb66b47SArtem Bityutskiy inuma = key_inum(c, &sa->key); 23203bb66b47SArtem Bityutskiy inumb = key_inum(c, &sb->key); 23213bb66b47SArtem Bityutskiy 23223bb66b47SArtem Bityutskiy if (inuma < inumb) 23233bb66b47SArtem Bityutskiy continue; 23243bb66b47SArtem Bityutskiy if (inuma > inumb) { 23253bb66b47SArtem Bityutskiy ubifs_err("larger inum %lu goes before inum %lu", 23263bb66b47SArtem Bityutskiy (unsigned long)inuma, (unsigned long)inumb); 23273bb66b47SArtem Bityutskiy goto error_dump; 23283bb66b47SArtem Bityutskiy } 23293bb66b47SArtem Bityutskiy 23303bb66b47SArtem Bityutskiy blka = key_block(c, &sa->key); 23313bb66b47SArtem Bityutskiy blkb = key_block(c, &sb->key); 23323bb66b47SArtem Bityutskiy 23333bb66b47SArtem Bityutskiy if (blka > blkb) { 23343bb66b47SArtem Bityutskiy ubifs_err("larger block %u goes before %u", blka, blkb); 23353bb66b47SArtem Bityutskiy goto error_dump; 23363bb66b47SArtem Bityutskiy } 23373bb66b47SArtem Bityutskiy if (blka == blkb) { 23383bb66b47SArtem Bityutskiy ubifs_err("two data nodes for the same block"); 23393bb66b47SArtem Bityutskiy goto error_dump; 23403bb66b47SArtem Bityutskiy } 23413bb66b47SArtem Bityutskiy } 23423bb66b47SArtem Bityutskiy 23433bb66b47SArtem Bityutskiy return 0; 23443bb66b47SArtem Bityutskiy 23453bb66b47SArtem Bityutskiy error_dump: 23463bb66b47SArtem Bityutskiy dbg_dump_node(c, sa->node); 23473bb66b47SArtem Bityutskiy dbg_dump_node(c, sb->node); 23483bb66b47SArtem Bityutskiy return -EINVAL; 23493bb66b47SArtem Bityutskiy } 23503bb66b47SArtem Bityutskiy 23513bb66b47SArtem Bityutskiy /** 23523bb66b47SArtem Bityutskiy * dbg_check_nondata_nodes_order - check that list of data nodes is sorted. 23533bb66b47SArtem Bityutskiy * @c: UBIFS file-system description object 23543bb66b47SArtem Bityutskiy * @head: the list of nodes ('struct ubifs_scan_node' objects) 23553bb66b47SArtem Bityutskiy * 23563bb66b47SArtem Bityutskiy * This function returns zero if the list of non-data nodes is sorted correctly, 23573bb66b47SArtem Bityutskiy * and %-EINVAL if not. 23583bb66b47SArtem Bityutskiy */ 23593bb66b47SArtem Bityutskiy int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head) 23603bb66b47SArtem Bityutskiy { 23613bb66b47SArtem Bityutskiy struct list_head *cur; 23623bb66b47SArtem Bityutskiy struct ubifs_scan_node *sa, *sb; 23633bb66b47SArtem Bityutskiy 23643bb66b47SArtem Bityutskiy if (!(ubifs_chk_flags & UBIFS_CHK_GEN)) 23653bb66b47SArtem Bityutskiy return 0; 23663bb66b47SArtem Bityutskiy 23673bb66b47SArtem Bityutskiy for (cur = head->next; cur->next != head; cur = cur->next) { 23683bb66b47SArtem Bityutskiy ino_t inuma, inumb; 23693bb66b47SArtem Bityutskiy uint32_t hasha, hashb; 23703bb66b47SArtem Bityutskiy 23713bb66b47SArtem Bityutskiy cond_resched(); 23723bb66b47SArtem Bityutskiy sa = container_of(cur, struct ubifs_scan_node, list); 23733bb66b47SArtem Bityutskiy sb = container_of(cur->next, struct ubifs_scan_node, list); 23743bb66b47SArtem Bityutskiy 23753bb66b47SArtem Bityutskiy if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE && 23763bb66b47SArtem Bityutskiy sa->type != UBIFS_XENT_NODE) { 23773bb66b47SArtem Bityutskiy ubifs_err("bad node type %d", sa->type); 23783bb66b47SArtem Bityutskiy dbg_dump_node(c, sa->node); 23793bb66b47SArtem Bityutskiy return -EINVAL; 23803bb66b47SArtem Bityutskiy } 23813bb66b47SArtem Bityutskiy if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE && 23823bb66b47SArtem Bityutskiy sa->type != UBIFS_XENT_NODE) { 23833bb66b47SArtem Bityutskiy ubifs_err("bad node type %d", sb->type); 23843bb66b47SArtem Bityutskiy dbg_dump_node(c, sb->node); 23853bb66b47SArtem Bityutskiy return -EINVAL; 23863bb66b47SArtem Bityutskiy } 23873bb66b47SArtem Bityutskiy 23883bb66b47SArtem Bityutskiy if (sa->type != UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) { 23893bb66b47SArtem Bityutskiy ubifs_err("non-inode node goes before inode node"); 23903bb66b47SArtem Bityutskiy goto error_dump; 23913bb66b47SArtem Bityutskiy } 23923bb66b47SArtem Bityutskiy 23933bb66b47SArtem Bityutskiy if (sa->type == UBIFS_INO_NODE && sb->type != UBIFS_INO_NODE) 23943bb66b47SArtem Bityutskiy continue; 23953bb66b47SArtem Bityutskiy 23963bb66b47SArtem Bityutskiy if (sa->type == UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) { 23973bb66b47SArtem Bityutskiy /* Inode nodes are sorted in descending size order */ 23983bb66b47SArtem Bityutskiy if (sa->len < sb->len) { 23993bb66b47SArtem Bityutskiy ubifs_err("smaller inode node goes first"); 24003bb66b47SArtem Bityutskiy goto error_dump; 24013bb66b47SArtem Bityutskiy } 24023bb66b47SArtem Bityutskiy continue; 24033bb66b47SArtem Bityutskiy } 24043bb66b47SArtem Bityutskiy 24053bb66b47SArtem Bityutskiy /* 24063bb66b47SArtem Bityutskiy * This is either a dentry or xentry, which should be sorted in 24073bb66b47SArtem Bityutskiy * ascending (parent ino, hash) order. 24083bb66b47SArtem Bityutskiy */ 24093bb66b47SArtem Bityutskiy inuma = key_inum(c, &sa->key); 24103bb66b47SArtem Bityutskiy inumb = key_inum(c, &sb->key); 24113bb66b47SArtem Bityutskiy 24123bb66b47SArtem Bityutskiy if (inuma < inumb) 24133bb66b47SArtem Bityutskiy continue; 24143bb66b47SArtem Bityutskiy if (inuma > inumb) { 24153bb66b47SArtem Bityutskiy ubifs_err("larger inum %lu goes before inum %lu", 24163bb66b47SArtem Bityutskiy (unsigned long)inuma, (unsigned long)inumb); 24173bb66b47SArtem Bityutskiy goto error_dump; 24183bb66b47SArtem Bityutskiy } 24193bb66b47SArtem Bityutskiy 24203bb66b47SArtem Bityutskiy hasha = key_block(c, &sa->key); 24213bb66b47SArtem Bityutskiy hashb = key_block(c, &sb->key); 24223bb66b47SArtem Bityutskiy 24233bb66b47SArtem Bityutskiy if (hasha > hashb) { 2424c4361570SArtem Bityutskiy ubifs_err("larger hash %u goes before %u", 2425c4361570SArtem Bityutskiy hasha, hashb); 24263bb66b47SArtem Bityutskiy goto error_dump; 24273bb66b47SArtem Bityutskiy } 24283bb66b47SArtem Bityutskiy } 24293bb66b47SArtem Bityutskiy 24303bb66b47SArtem Bityutskiy return 0; 24313bb66b47SArtem Bityutskiy 24323bb66b47SArtem Bityutskiy error_dump: 24333bb66b47SArtem Bityutskiy ubifs_msg("dumping first node"); 24343bb66b47SArtem Bityutskiy dbg_dump_node(c, sa->node); 24353bb66b47SArtem Bityutskiy ubifs_msg("dumping second node"); 24363bb66b47SArtem Bityutskiy dbg_dump_node(c, sb->node); 24373bb66b47SArtem Bityutskiy return -EINVAL; 24383bb66b47SArtem Bityutskiy return 0; 24393bb66b47SArtem Bityutskiy } 24403bb66b47SArtem Bityutskiy 24411e51764aSArtem Bityutskiy static int invocation_cnt; 24421e51764aSArtem Bityutskiy 24431e51764aSArtem Bityutskiy int dbg_force_in_the_gaps(void) 24441e51764aSArtem Bityutskiy { 24451e51764aSArtem Bityutskiy if (!dbg_force_in_the_gaps_enabled) 24461e51764aSArtem Bityutskiy return 0; 24471e51764aSArtem Bityutskiy /* Force in-the-gaps every 8th commit */ 24481e51764aSArtem Bityutskiy return !((invocation_cnt++) & 0x7); 24491e51764aSArtem Bityutskiy } 24501e51764aSArtem Bityutskiy 24511e51764aSArtem Bityutskiy /* Failure mode for recovery testing */ 24521e51764aSArtem Bityutskiy 24531e51764aSArtem Bityutskiy #define chance(n, d) (simple_rand() <= (n) * 32768LL / (d)) 24541e51764aSArtem Bityutskiy 24551e51764aSArtem Bityutskiy struct failure_mode_info { 24561e51764aSArtem Bityutskiy struct list_head list; 24571e51764aSArtem Bityutskiy struct ubifs_info *c; 24581e51764aSArtem Bityutskiy }; 24591e51764aSArtem Bityutskiy 24601e51764aSArtem Bityutskiy static LIST_HEAD(fmi_list); 24611e51764aSArtem Bityutskiy static DEFINE_SPINLOCK(fmi_lock); 24621e51764aSArtem Bityutskiy 24631e51764aSArtem Bityutskiy static unsigned int next; 24641e51764aSArtem Bityutskiy 24651e51764aSArtem Bityutskiy static int simple_rand(void) 24661e51764aSArtem Bityutskiy { 24671e51764aSArtem Bityutskiy if (next == 0) 24681e51764aSArtem Bityutskiy next = current->pid; 24691e51764aSArtem Bityutskiy next = next * 1103515245 + 12345; 24701e51764aSArtem Bityutskiy return (next >> 16) & 32767; 24711e51764aSArtem Bityutskiy } 24721e51764aSArtem Bityutskiy 247317c2f9f8SArtem Bityutskiy static void failure_mode_init(struct ubifs_info *c) 24741e51764aSArtem Bityutskiy { 24751e51764aSArtem Bityutskiy struct failure_mode_info *fmi; 24761e51764aSArtem Bityutskiy 24771e51764aSArtem Bityutskiy fmi = kmalloc(sizeof(struct failure_mode_info), GFP_NOFS); 24781e51764aSArtem Bityutskiy if (!fmi) { 2479552ff317SArtem Bityutskiy ubifs_err("Failed to register failure mode - no memory"); 24801e51764aSArtem Bityutskiy return; 24811e51764aSArtem Bityutskiy } 24821e51764aSArtem Bityutskiy fmi->c = c; 24831e51764aSArtem Bityutskiy spin_lock(&fmi_lock); 24841e51764aSArtem Bityutskiy list_add_tail(&fmi->list, &fmi_list); 24851e51764aSArtem Bityutskiy spin_unlock(&fmi_lock); 24861e51764aSArtem Bityutskiy } 24871e51764aSArtem Bityutskiy 248817c2f9f8SArtem Bityutskiy static void failure_mode_exit(struct ubifs_info *c) 24891e51764aSArtem Bityutskiy { 24901e51764aSArtem Bityutskiy struct failure_mode_info *fmi, *tmp; 24911e51764aSArtem Bityutskiy 24921e51764aSArtem Bityutskiy spin_lock(&fmi_lock); 24931e51764aSArtem Bityutskiy list_for_each_entry_safe(fmi, tmp, &fmi_list, list) 24941e51764aSArtem Bityutskiy if (fmi->c == c) { 24951e51764aSArtem Bityutskiy list_del(&fmi->list); 24961e51764aSArtem Bityutskiy kfree(fmi); 24971e51764aSArtem Bityutskiy } 24981e51764aSArtem Bityutskiy spin_unlock(&fmi_lock); 24991e51764aSArtem Bityutskiy } 25001e51764aSArtem Bityutskiy 25011e51764aSArtem Bityutskiy static struct ubifs_info *dbg_find_info(struct ubi_volume_desc *desc) 25021e51764aSArtem Bityutskiy { 25031e51764aSArtem Bityutskiy struct failure_mode_info *fmi; 25041e51764aSArtem Bityutskiy 25051e51764aSArtem Bityutskiy spin_lock(&fmi_lock); 25061e51764aSArtem Bityutskiy list_for_each_entry(fmi, &fmi_list, list) 25071e51764aSArtem Bityutskiy if (fmi->c->ubi == desc) { 25081e51764aSArtem Bityutskiy struct ubifs_info *c = fmi->c; 25091e51764aSArtem Bityutskiy 25101e51764aSArtem Bityutskiy spin_unlock(&fmi_lock); 25111e51764aSArtem Bityutskiy return c; 25121e51764aSArtem Bityutskiy } 25131e51764aSArtem Bityutskiy spin_unlock(&fmi_lock); 25141e51764aSArtem Bityutskiy return NULL; 25151e51764aSArtem Bityutskiy } 25161e51764aSArtem Bityutskiy 25171e51764aSArtem Bityutskiy static int in_failure_mode(struct ubi_volume_desc *desc) 25181e51764aSArtem Bityutskiy { 25191e51764aSArtem Bityutskiy struct ubifs_info *c = dbg_find_info(desc); 25201e51764aSArtem Bityutskiy 25211e51764aSArtem Bityutskiy if (c && dbg_failure_mode) 252217c2f9f8SArtem Bityutskiy return c->dbg->failure_mode; 25231e51764aSArtem Bityutskiy return 0; 25241e51764aSArtem Bityutskiy } 25251e51764aSArtem Bityutskiy 25261e51764aSArtem Bityutskiy static int do_fail(struct ubi_volume_desc *desc, int lnum, int write) 25271e51764aSArtem Bityutskiy { 25281e51764aSArtem Bityutskiy struct ubifs_info *c = dbg_find_info(desc); 252917c2f9f8SArtem Bityutskiy struct ubifs_debug_info *d; 25301e51764aSArtem Bityutskiy 25311e51764aSArtem Bityutskiy if (!c || !dbg_failure_mode) 25321e51764aSArtem Bityutskiy return 0; 253317c2f9f8SArtem Bityutskiy d = c->dbg; 253417c2f9f8SArtem Bityutskiy if (d->failure_mode) 25351e51764aSArtem Bityutskiy return 1; 253617c2f9f8SArtem Bityutskiy if (!d->fail_cnt) { 25371e51764aSArtem Bityutskiy /* First call - decide delay to failure */ 25381e51764aSArtem Bityutskiy if (chance(1, 2)) { 25391e51764aSArtem Bityutskiy unsigned int delay = 1 << (simple_rand() >> 11); 25401e51764aSArtem Bityutskiy 25411e51764aSArtem Bityutskiy if (chance(1, 2)) { 254217c2f9f8SArtem Bityutskiy d->fail_delay = 1; 254317c2f9f8SArtem Bityutskiy d->fail_timeout = jiffies + 25441e51764aSArtem Bityutskiy msecs_to_jiffies(delay); 25451e51764aSArtem Bityutskiy dbg_rcvry("failing after %ums", delay); 25461e51764aSArtem Bityutskiy } else { 254717c2f9f8SArtem Bityutskiy d->fail_delay = 2; 254817c2f9f8SArtem Bityutskiy d->fail_cnt_max = delay; 25491e51764aSArtem Bityutskiy dbg_rcvry("failing after %u calls", delay); 25501e51764aSArtem Bityutskiy } 25511e51764aSArtem Bityutskiy } 255217c2f9f8SArtem Bityutskiy d->fail_cnt += 1; 25531e51764aSArtem Bityutskiy } 25541e51764aSArtem Bityutskiy /* Determine if failure delay has expired */ 255517c2f9f8SArtem Bityutskiy if (d->fail_delay == 1) { 255617c2f9f8SArtem Bityutskiy if (time_before(jiffies, d->fail_timeout)) 25571e51764aSArtem Bityutskiy return 0; 255817c2f9f8SArtem Bityutskiy } else if (d->fail_delay == 2) 255917c2f9f8SArtem Bityutskiy if (d->fail_cnt++ < d->fail_cnt_max) 25601e51764aSArtem Bityutskiy return 0; 25611e51764aSArtem Bityutskiy if (lnum == UBIFS_SB_LNUM) { 25621e51764aSArtem Bityutskiy if (write) { 25631e51764aSArtem Bityutskiy if (chance(1, 2)) 25641e51764aSArtem Bityutskiy return 0; 25651e51764aSArtem Bityutskiy } else if (chance(19, 20)) 25661e51764aSArtem Bityutskiy return 0; 25671e51764aSArtem Bityutskiy dbg_rcvry("failing in super block LEB %d", lnum); 25681e51764aSArtem Bityutskiy } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) { 25691e51764aSArtem Bityutskiy if (chance(19, 20)) 25701e51764aSArtem Bityutskiy return 0; 25711e51764aSArtem Bityutskiy dbg_rcvry("failing in master LEB %d", lnum); 25721e51764aSArtem Bityutskiy } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) { 25731e51764aSArtem Bityutskiy if (write) { 25741e51764aSArtem Bityutskiy if (chance(99, 100)) 25751e51764aSArtem Bityutskiy return 0; 25761e51764aSArtem Bityutskiy } else if (chance(399, 400)) 25771e51764aSArtem Bityutskiy return 0; 25781e51764aSArtem Bityutskiy dbg_rcvry("failing in log LEB %d", lnum); 25791e51764aSArtem Bityutskiy } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) { 25801e51764aSArtem Bityutskiy if (write) { 25811e51764aSArtem Bityutskiy if (chance(7, 8)) 25821e51764aSArtem Bityutskiy return 0; 25831e51764aSArtem Bityutskiy } else if (chance(19, 20)) 25841e51764aSArtem Bityutskiy return 0; 25851e51764aSArtem Bityutskiy dbg_rcvry("failing in LPT LEB %d", lnum); 25861e51764aSArtem Bityutskiy } else if (lnum >= c->orph_first && lnum <= c->orph_last) { 25871e51764aSArtem Bityutskiy if (write) { 25881e51764aSArtem Bityutskiy if (chance(1, 2)) 25891e51764aSArtem Bityutskiy return 0; 25901e51764aSArtem Bityutskiy } else if (chance(9, 10)) 25911e51764aSArtem Bityutskiy return 0; 25921e51764aSArtem Bityutskiy dbg_rcvry("failing in orphan LEB %d", lnum); 25931e51764aSArtem Bityutskiy } else if (lnum == c->ihead_lnum) { 25941e51764aSArtem Bityutskiy if (chance(99, 100)) 25951e51764aSArtem Bityutskiy return 0; 25961e51764aSArtem Bityutskiy dbg_rcvry("failing in index head LEB %d", lnum); 25971e51764aSArtem Bityutskiy } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) { 25981e51764aSArtem Bityutskiy if (chance(9, 10)) 25991e51764aSArtem Bityutskiy return 0; 26001e51764aSArtem Bityutskiy dbg_rcvry("failing in GC head LEB %d", lnum); 26011e51764aSArtem Bityutskiy } else if (write && !RB_EMPTY_ROOT(&c->buds) && 26021e51764aSArtem Bityutskiy !ubifs_search_bud(c, lnum)) { 26031e51764aSArtem Bityutskiy if (chance(19, 20)) 26041e51764aSArtem Bityutskiy return 0; 26051e51764aSArtem Bityutskiy dbg_rcvry("failing in non-bud LEB %d", lnum); 26061e51764aSArtem Bityutskiy } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND || 26071e51764aSArtem Bityutskiy c->cmt_state == COMMIT_RUNNING_REQUIRED) { 26081e51764aSArtem Bityutskiy if (chance(999, 1000)) 26091e51764aSArtem Bityutskiy return 0; 26101e51764aSArtem Bityutskiy dbg_rcvry("failing in bud LEB %d commit running", lnum); 26111e51764aSArtem Bityutskiy } else { 26121e51764aSArtem Bityutskiy if (chance(9999, 10000)) 26131e51764aSArtem Bityutskiy return 0; 26141e51764aSArtem Bityutskiy dbg_rcvry("failing in bud LEB %d commit not running", lnum); 26151e51764aSArtem Bityutskiy } 26161e51764aSArtem Bityutskiy ubifs_err("*** SETTING FAILURE MODE ON (LEB %d) ***", lnum); 261717c2f9f8SArtem Bityutskiy d->failure_mode = 1; 26181e51764aSArtem Bityutskiy dump_stack(); 26191e51764aSArtem Bityutskiy return 1; 26201e51764aSArtem Bityutskiy } 26211e51764aSArtem Bityutskiy 26221e51764aSArtem Bityutskiy static void cut_data(const void *buf, int len) 26231e51764aSArtem Bityutskiy { 26241e51764aSArtem Bityutskiy int flen, i; 26251e51764aSArtem Bityutskiy unsigned char *p = (void *)buf; 26261e51764aSArtem Bityutskiy 26271e51764aSArtem Bityutskiy flen = (len * (long long)simple_rand()) >> 15; 26281e51764aSArtem Bityutskiy for (i = flen; i < len; i++) 26291e51764aSArtem Bityutskiy p[i] = 0xff; 26301e51764aSArtem Bityutskiy } 26311e51764aSArtem Bityutskiy 26321e51764aSArtem Bityutskiy int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset, 26331e51764aSArtem Bityutskiy int len, int check) 26341e51764aSArtem Bityutskiy { 26351e51764aSArtem Bityutskiy if (in_failure_mode(desc)) 26361e51764aSArtem Bityutskiy return -EIO; 26371e51764aSArtem Bityutskiy return ubi_leb_read(desc, lnum, buf, offset, len, check); 26381e51764aSArtem Bityutskiy } 26391e51764aSArtem Bityutskiy 26401e51764aSArtem Bityutskiy int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf, 26411e51764aSArtem Bityutskiy int offset, int len, int dtype) 26421e51764aSArtem Bityutskiy { 264316dfd804SAdrian Hunter int err, failing; 26441e51764aSArtem Bityutskiy 26451e51764aSArtem Bityutskiy if (in_failure_mode(desc)) 26461e51764aSArtem Bityutskiy return -EIO; 264716dfd804SAdrian Hunter failing = do_fail(desc, lnum, 1); 264816dfd804SAdrian Hunter if (failing) 26491e51764aSArtem Bityutskiy cut_data(buf, len); 26501e51764aSArtem Bityutskiy err = ubi_leb_write(desc, lnum, buf, offset, len, dtype); 26511e51764aSArtem Bityutskiy if (err) 26521e51764aSArtem Bityutskiy return err; 265316dfd804SAdrian Hunter if (failing) 26541e51764aSArtem Bityutskiy return -EIO; 26551e51764aSArtem Bityutskiy return 0; 26561e51764aSArtem Bityutskiy } 26571e51764aSArtem Bityutskiy 26581e51764aSArtem Bityutskiy int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf, 26591e51764aSArtem Bityutskiy int len, int dtype) 26601e51764aSArtem Bityutskiy { 26611e51764aSArtem Bityutskiy int err; 26621e51764aSArtem Bityutskiy 26631e51764aSArtem Bityutskiy if (do_fail(desc, lnum, 1)) 26641e51764aSArtem Bityutskiy return -EIO; 26651e51764aSArtem Bityutskiy err = ubi_leb_change(desc, lnum, buf, len, dtype); 26661e51764aSArtem Bityutskiy if (err) 26671e51764aSArtem Bityutskiy return err; 26681e51764aSArtem Bityutskiy if (do_fail(desc, lnum, 1)) 26691e51764aSArtem Bityutskiy return -EIO; 26701e51764aSArtem Bityutskiy return 0; 26711e51764aSArtem Bityutskiy } 26721e51764aSArtem Bityutskiy 26731e51764aSArtem Bityutskiy int dbg_leb_erase(struct ubi_volume_desc *desc, int lnum) 26741e51764aSArtem Bityutskiy { 26751e51764aSArtem Bityutskiy int err; 26761e51764aSArtem Bityutskiy 26771e51764aSArtem Bityutskiy if (do_fail(desc, lnum, 0)) 26781e51764aSArtem Bityutskiy return -EIO; 26791e51764aSArtem Bityutskiy err = ubi_leb_erase(desc, lnum); 26801e51764aSArtem Bityutskiy if (err) 26811e51764aSArtem Bityutskiy return err; 26821e51764aSArtem Bityutskiy if (do_fail(desc, lnum, 0)) 26831e51764aSArtem Bityutskiy return -EIO; 26841e51764aSArtem Bityutskiy return 0; 26851e51764aSArtem Bityutskiy } 26861e51764aSArtem Bityutskiy 26871e51764aSArtem Bityutskiy int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum) 26881e51764aSArtem Bityutskiy { 26891e51764aSArtem Bityutskiy int err; 26901e51764aSArtem Bityutskiy 26911e51764aSArtem Bityutskiy if (do_fail(desc, lnum, 0)) 26921e51764aSArtem Bityutskiy return -EIO; 26931e51764aSArtem Bityutskiy err = ubi_leb_unmap(desc, lnum); 26941e51764aSArtem Bityutskiy if (err) 26951e51764aSArtem Bityutskiy return err; 26961e51764aSArtem Bityutskiy if (do_fail(desc, lnum, 0)) 26971e51764aSArtem Bityutskiy return -EIO; 26981e51764aSArtem Bityutskiy return 0; 26991e51764aSArtem Bityutskiy } 27001e51764aSArtem Bityutskiy 27011e51764aSArtem Bityutskiy int dbg_is_mapped(struct ubi_volume_desc *desc, int lnum) 27021e51764aSArtem Bityutskiy { 27031e51764aSArtem Bityutskiy if (in_failure_mode(desc)) 27041e51764aSArtem Bityutskiy return -EIO; 27051e51764aSArtem Bityutskiy return ubi_is_mapped(desc, lnum); 27061e51764aSArtem Bityutskiy } 27071e51764aSArtem Bityutskiy 27081e51764aSArtem Bityutskiy int dbg_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype) 27091e51764aSArtem Bityutskiy { 27101e51764aSArtem Bityutskiy int err; 27111e51764aSArtem Bityutskiy 27121e51764aSArtem Bityutskiy if (do_fail(desc, lnum, 0)) 27131e51764aSArtem Bityutskiy return -EIO; 27141e51764aSArtem Bityutskiy err = ubi_leb_map(desc, lnum, dtype); 27151e51764aSArtem Bityutskiy if (err) 27161e51764aSArtem Bityutskiy return err; 27171e51764aSArtem Bityutskiy if (do_fail(desc, lnum, 0)) 27181e51764aSArtem Bityutskiy return -EIO; 27191e51764aSArtem Bityutskiy return 0; 27201e51764aSArtem Bityutskiy } 27211e51764aSArtem Bityutskiy 272217c2f9f8SArtem Bityutskiy /** 272317c2f9f8SArtem Bityutskiy * ubifs_debugging_init - initialize UBIFS debugging. 272417c2f9f8SArtem Bityutskiy * @c: UBIFS file-system description object 272517c2f9f8SArtem Bityutskiy * 272617c2f9f8SArtem Bityutskiy * This function initializes debugging-related data for the file system. 272717c2f9f8SArtem Bityutskiy * Returns zero in case of success and a negative error code in case of 272817c2f9f8SArtem Bityutskiy * failure. 272917c2f9f8SArtem Bityutskiy */ 273017c2f9f8SArtem Bityutskiy int ubifs_debugging_init(struct ubifs_info *c) 273117c2f9f8SArtem Bityutskiy { 273217c2f9f8SArtem Bityutskiy c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL); 273317c2f9f8SArtem Bityutskiy if (!c->dbg) 273417c2f9f8SArtem Bityutskiy return -ENOMEM; 273517c2f9f8SArtem Bityutskiy 273617c2f9f8SArtem Bityutskiy failure_mode_init(c); 273717c2f9f8SArtem Bityutskiy return 0; 273817c2f9f8SArtem Bityutskiy } 273917c2f9f8SArtem Bityutskiy 274017c2f9f8SArtem Bityutskiy /** 274117c2f9f8SArtem Bityutskiy * ubifs_debugging_exit - free debugging data. 274217c2f9f8SArtem Bityutskiy * @c: UBIFS file-system description object 274317c2f9f8SArtem Bityutskiy */ 274417c2f9f8SArtem Bityutskiy void ubifs_debugging_exit(struct ubifs_info *c) 274517c2f9f8SArtem Bityutskiy { 274617c2f9f8SArtem Bityutskiy failure_mode_exit(c); 274717c2f9f8SArtem Bityutskiy kfree(c->dbg); 274817c2f9f8SArtem Bityutskiy } 274917c2f9f8SArtem Bityutskiy 2750552ff317SArtem Bityutskiy /* 2751552ff317SArtem Bityutskiy * Root directory for UBIFS stuff in debugfs. Contains sub-directories which 2752552ff317SArtem Bityutskiy * contain the stuff specific to particular file-system mounts. 2753552ff317SArtem Bityutskiy */ 275484abf972SArtem Bityutskiy static struct dentry *dfs_rootdir; 2755552ff317SArtem Bityutskiy 2756552ff317SArtem Bityutskiy /** 2757552ff317SArtem Bityutskiy * dbg_debugfs_init - initialize debugfs file-system. 2758552ff317SArtem Bityutskiy * 2759552ff317SArtem Bityutskiy * UBIFS uses debugfs file-system to expose various debugging knobs to 2760552ff317SArtem Bityutskiy * user-space. This function creates "ubifs" directory in the debugfs 2761552ff317SArtem Bityutskiy * file-system. Returns zero in case of success and a negative error code in 2762552ff317SArtem Bityutskiy * case of failure. 2763552ff317SArtem Bityutskiy */ 2764552ff317SArtem Bityutskiy int dbg_debugfs_init(void) 2765552ff317SArtem Bityutskiy { 276684abf972SArtem Bityutskiy dfs_rootdir = debugfs_create_dir("ubifs", NULL); 276784abf972SArtem Bityutskiy if (IS_ERR(dfs_rootdir)) { 276884abf972SArtem Bityutskiy int err = PTR_ERR(dfs_rootdir); 2769552ff317SArtem Bityutskiy ubifs_err("cannot create \"ubifs\" debugfs directory, " 2770552ff317SArtem Bityutskiy "error %d\n", err); 2771552ff317SArtem Bityutskiy return err; 2772552ff317SArtem Bityutskiy } 2773552ff317SArtem Bityutskiy 2774552ff317SArtem Bityutskiy return 0; 2775552ff317SArtem Bityutskiy } 2776552ff317SArtem Bityutskiy 2777552ff317SArtem Bityutskiy /** 2778552ff317SArtem Bityutskiy * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system. 2779552ff317SArtem Bityutskiy */ 2780552ff317SArtem Bityutskiy void dbg_debugfs_exit(void) 2781552ff317SArtem Bityutskiy { 278284abf972SArtem Bityutskiy debugfs_remove(dfs_rootdir); 2783552ff317SArtem Bityutskiy } 2784552ff317SArtem Bityutskiy 2785552ff317SArtem Bityutskiy static int open_debugfs_file(struct inode *inode, struct file *file) 2786552ff317SArtem Bityutskiy { 2787552ff317SArtem Bityutskiy file->private_data = inode->i_private; 27881bbfc848SArtem Bityutskiy return nonseekable_open(inode, file); 2789552ff317SArtem Bityutskiy } 2790552ff317SArtem Bityutskiy 2791552ff317SArtem Bityutskiy static ssize_t write_debugfs_file(struct file *file, const char __user *buf, 2792552ff317SArtem Bityutskiy size_t count, loff_t *ppos) 2793552ff317SArtem Bityutskiy { 2794552ff317SArtem Bityutskiy struct ubifs_info *c = file->private_data; 2795552ff317SArtem Bityutskiy struct ubifs_debug_info *d = c->dbg; 2796552ff317SArtem Bityutskiy 279784abf972SArtem Bityutskiy if (file->f_path.dentry == d->dfs_dump_lprops) 2798552ff317SArtem Bityutskiy dbg_dump_lprops(c); 279984abf972SArtem Bityutskiy else if (file->f_path.dentry == d->dfs_dump_budg) { 2800552ff317SArtem Bityutskiy spin_lock(&c->space_lock); 2801552ff317SArtem Bityutskiy dbg_dump_budg(c); 2802552ff317SArtem Bityutskiy spin_unlock(&c->space_lock); 280384abf972SArtem Bityutskiy } else if (file->f_path.dentry == d->dfs_dump_tnc) { 2804552ff317SArtem Bityutskiy mutex_lock(&c->tnc_mutex); 2805552ff317SArtem Bityutskiy dbg_dump_tnc(c); 2806552ff317SArtem Bityutskiy mutex_unlock(&c->tnc_mutex); 2807552ff317SArtem Bityutskiy } else 2808552ff317SArtem Bityutskiy return -EINVAL; 2809552ff317SArtem Bityutskiy 2810552ff317SArtem Bityutskiy *ppos += count; 2811552ff317SArtem Bityutskiy return count; 2812552ff317SArtem Bityutskiy } 2813552ff317SArtem Bityutskiy 281484abf972SArtem Bityutskiy static const struct file_operations dfs_fops = { 2815552ff317SArtem Bityutskiy .open = open_debugfs_file, 2816552ff317SArtem Bityutskiy .write = write_debugfs_file, 2817552ff317SArtem Bityutskiy .owner = THIS_MODULE, 28181bbfc848SArtem Bityutskiy .llseek = no_llseek, 2819552ff317SArtem Bityutskiy }; 2820552ff317SArtem Bityutskiy 2821552ff317SArtem Bityutskiy /** 2822552ff317SArtem Bityutskiy * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance. 2823552ff317SArtem Bityutskiy * @c: UBIFS file-system description object 2824552ff317SArtem Bityutskiy * 2825552ff317SArtem Bityutskiy * This function creates all debugfs files for this instance of UBIFS. Returns 2826552ff317SArtem Bityutskiy * zero in case of success and a negative error code in case of failure. 2827552ff317SArtem Bityutskiy * 2828552ff317SArtem Bityutskiy * Note, the only reason we have not merged this function with the 2829552ff317SArtem Bityutskiy * 'ubifs_debugging_init()' function is because it is better to initialize 2830552ff317SArtem Bityutskiy * debugfs interfaces at the very end of the mount process, and remove them at 2831552ff317SArtem Bityutskiy * the very beginning of the mount process. 2832552ff317SArtem Bityutskiy */ 2833552ff317SArtem Bityutskiy int dbg_debugfs_init_fs(struct ubifs_info *c) 2834552ff317SArtem Bityutskiy { 2835552ff317SArtem Bityutskiy int err; 2836552ff317SArtem Bityutskiy const char *fname; 2837552ff317SArtem Bityutskiy struct dentry *dent; 2838552ff317SArtem Bityutskiy struct ubifs_debug_info *d = c->dbg; 2839552ff317SArtem Bityutskiy 284084abf972SArtem Bityutskiy sprintf(d->dfs_dir_name, "ubi%d_%d", c->vi.ubi_num, c->vi.vol_id); 2841cc6a86b9SArtem Bityutskiy fname = d->dfs_dir_name; 2842cc6a86b9SArtem Bityutskiy dent = debugfs_create_dir(fname, dfs_rootdir); 284395169535SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 2844552ff317SArtem Bityutskiy goto out; 2845cc6a86b9SArtem Bityutskiy d->dfs_dir = dent; 2846552ff317SArtem Bityutskiy 2847552ff317SArtem Bityutskiy fname = "dump_lprops"; 28488c559d30SVasiliy Kulikov dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops); 284995169535SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 2850552ff317SArtem Bityutskiy goto out_remove; 285184abf972SArtem Bityutskiy d->dfs_dump_lprops = dent; 2852552ff317SArtem Bityutskiy 2853552ff317SArtem Bityutskiy fname = "dump_budg"; 28548c559d30SVasiliy Kulikov dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops); 285595169535SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 2856552ff317SArtem Bityutskiy goto out_remove; 285784abf972SArtem Bityutskiy d->dfs_dump_budg = dent; 2858552ff317SArtem Bityutskiy 2859552ff317SArtem Bityutskiy fname = "dump_tnc"; 28608c559d30SVasiliy Kulikov dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops); 286195169535SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 2862552ff317SArtem Bityutskiy goto out_remove; 286384abf972SArtem Bityutskiy d->dfs_dump_tnc = dent; 2864552ff317SArtem Bityutskiy 2865552ff317SArtem Bityutskiy return 0; 2866552ff317SArtem Bityutskiy 2867552ff317SArtem Bityutskiy out_remove: 2868cc6a86b9SArtem Bityutskiy debugfs_remove_recursive(d->dfs_dir); 2869cc6a86b9SArtem Bityutskiy out: 287095169535SArtem Bityutskiy err = dent ? PTR_ERR(dent) : -ENODEV; 2871552ff317SArtem Bityutskiy ubifs_err("cannot create \"%s\" debugfs directory, error %d\n", 2872552ff317SArtem Bityutskiy fname, err); 2873552ff317SArtem Bityutskiy return err; 2874552ff317SArtem Bityutskiy } 2875552ff317SArtem Bityutskiy 2876552ff317SArtem Bityutskiy /** 2877552ff317SArtem Bityutskiy * dbg_debugfs_exit_fs - remove all debugfs files. 2878552ff317SArtem Bityutskiy * @c: UBIFS file-system description object 2879552ff317SArtem Bityutskiy */ 2880552ff317SArtem Bityutskiy void dbg_debugfs_exit_fs(struct ubifs_info *c) 2881552ff317SArtem Bityutskiy { 288284abf972SArtem Bityutskiy debugfs_remove_recursive(c->dbg->dfs_dir); 2883552ff317SArtem Bityutskiy } 2884552ff317SArtem Bityutskiy 28851e51764aSArtem Bityutskiy #endif /* CONFIG_UBIFS_FS_DEBUG */ 2886