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> 34552ff317SArtem Bityutskiy #include <linux/debugfs.h> 354d61db4fSArtem Bityutskiy #include <linux/math64.h> 3681e79d38SArtem Bityutskiy #include <linux/uaccess.h> 371e51764aSArtem Bityutskiy 381e51764aSArtem Bityutskiy #ifdef CONFIG_UBIFS_FS_DEBUG 391e51764aSArtem Bityutskiy 401e51764aSArtem Bityutskiy DEFINE_SPINLOCK(dbg_lock); 411e51764aSArtem Bityutskiy 421e51764aSArtem Bityutskiy static char dbg_key_buf0[128]; 431e51764aSArtem Bityutskiy static char dbg_key_buf1[128]; 441e51764aSArtem Bityutskiy 451e51764aSArtem Bityutskiy static const char *get_key_fmt(int fmt) 461e51764aSArtem Bityutskiy { 471e51764aSArtem Bityutskiy switch (fmt) { 481e51764aSArtem Bityutskiy case UBIFS_SIMPLE_KEY_FMT: 491e51764aSArtem Bityutskiy return "simple"; 501e51764aSArtem Bityutskiy default: 511e51764aSArtem Bityutskiy return "unknown/invalid format"; 521e51764aSArtem Bityutskiy } 531e51764aSArtem Bityutskiy } 541e51764aSArtem Bityutskiy 551e51764aSArtem Bityutskiy static const char *get_key_hash(int hash) 561e51764aSArtem Bityutskiy { 571e51764aSArtem Bityutskiy switch (hash) { 581e51764aSArtem Bityutskiy case UBIFS_KEY_HASH_R5: 591e51764aSArtem Bityutskiy return "R5"; 601e51764aSArtem Bityutskiy case UBIFS_KEY_HASH_TEST: 611e51764aSArtem Bityutskiy return "test"; 621e51764aSArtem Bityutskiy default: 631e51764aSArtem Bityutskiy return "unknown/invalid name hash"; 641e51764aSArtem Bityutskiy } 651e51764aSArtem Bityutskiy } 661e51764aSArtem Bityutskiy 671e51764aSArtem Bityutskiy static const char *get_key_type(int type) 681e51764aSArtem Bityutskiy { 691e51764aSArtem Bityutskiy switch (type) { 701e51764aSArtem Bityutskiy case UBIFS_INO_KEY: 711e51764aSArtem Bityutskiy return "inode"; 721e51764aSArtem Bityutskiy case UBIFS_DENT_KEY: 731e51764aSArtem Bityutskiy return "direntry"; 741e51764aSArtem Bityutskiy case UBIFS_XENT_KEY: 751e51764aSArtem Bityutskiy return "xentry"; 761e51764aSArtem Bityutskiy case UBIFS_DATA_KEY: 771e51764aSArtem Bityutskiy return "data"; 781e51764aSArtem Bityutskiy case UBIFS_TRUN_KEY: 791e51764aSArtem Bityutskiy return "truncate"; 801e51764aSArtem Bityutskiy default: 811e51764aSArtem Bityutskiy return "unknown/invalid key"; 821e51764aSArtem Bityutskiy } 831e51764aSArtem Bityutskiy } 841e51764aSArtem Bityutskiy 854315fb40SArtem Bityutskiy static const char *get_dent_type(int type) 864315fb40SArtem Bityutskiy { 874315fb40SArtem Bityutskiy switch (type) { 884315fb40SArtem Bityutskiy case UBIFS_ITYPE_REG: 894315fb40SArtem Bityutskiy return "file"; 904315fb40SArtem Bityutskiy case UBIFS_ITYPE_DIR: 914315fb40SArtem Bityutskiy return "dir"; 924315fb40SArtem Bityutskiy case UBIFS_ITYPE_LNK: 934315fb40SArtem Bityutskiy return "symlink"; 944315fb40SArtem Bityutskiy case UBIFS_ITYPE_BLK: 954315fb40SArtem Bityutskiy return "blkdev"; 964315fb40SArtem Bityutskiy case UBIFS_ITYPE_CHR: 974315fb40SArtem Bityutskiy return "char dev"; 984315fb40SArtem Bityutskiy case UBIFS_ITYPE_FIFO: 994315fb40SArtem Bityutskiy return "fifo"; 1004315fb40SArtem Bityutskiy case UBIFS_ITYPE_SOCK: 1014315fb40SArtem Bityutskiy return "socket"; 1024315fb40SArtem Bityutskiy default: 1034315fb40SArtem Bityutskiy return "unknown/invalid type"; 1044315fb40SArtem Bityutskiy } 1054315fb40SArtem Bityutskiy } 1064315fb40SArtem Bityutskiy 1071e51764aSArtem Bityutskiy static void sprintf_key(const struct ubifs_info *c, const union ubifs_key *key, 1081e51764aSArtem Bityutskiy char *buffer) 1091e51764aSArtem Bityutskiy { 1101e51764aSArtem Bityutskiy char *p = buffer; 1111e51764aSArtem Bityutskiy int type = key_type(c, key); 1121e51764aSArtem Bityutskiy 1131e51764aSArtem Bityutskiy if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) { 1141e51764aSArtem Bityutskiy switch (type) { 1151e51764aSArtem Bityutskiy case UBIFS_INO_KEY: 116e84461adSArtem Bityutskiy sprintf(p, "(%lu, %s)", (unsigned long)key_inum(c, key), 1171e51764aSArtem Bityutskiy get_key_type(type)); 1181e51764aSArtem Bityutskiy break; 1191e51764aSArtem Bityutskiy case UBIFS_DENT_KEY: 1201e51764aSArtem Bityutskiy case UBIFS_XENT_KEY: 121e84461adSArtem Bityutskiy sprintf(p, "(%lu, %s, %#08x)", 122e84461adSArtem Bityutskiy (unsigned long)key_inum(c, key), 1231e51764aSArtem Bityutskiy get_key_type(type), key_hash(c, key)); 1241e51764aSArtem Bityutskiy break; 1251e51764aSArtem Bityutskiy case UBIFS_DATA_KEY: 126e84461adSArtem Bityutskiy sprintf(p, "(%lu, %s, %u)", 127e84461adSArtem Bityutskiy (unsigned long)key_inum(c, key), 1281e51764aSArtem Bityutskiy get_key_type(type), key_block(c, key)); 1291e51764aSArtem Bityutskiy break; 1301e51764aSArtem Bityutskiy case UBIFS_TRUN_KEY: 1311e51764aSArtem Bityutskiy sprintf(p, "(%lu, %s)", 132e84461adSArtem Bityutskiy (unsigned long)key_inum(c, key), 133e84461adSArtem Bityutskiy get_key_type(type)); 1341e51764aSArtem Bityutskiy break; 1351e51764aSArtem Bityutskiy default: 1361e51764aSArtem Bityutskiy sprintf(p, "(bad key type: %#08x, %#08x)", 1371e51764aSArtem Bityutskiy key->u32[0], key->u32[1]); 1381e51764aSArtem Bityutskiy } 1391e51764aSArtem Bityutskiy } else 1401e51764aSArtem Bityutskiy sprintf(p, "bad key format %d", c->key_fmt); 1411e51764aSArtem Bityutskiy } 1421e51764aSArtem Bityutskiy 1431e51764aSArtem Bityutskiy const char *dbg_key_str0(const struct ubifs_info *c, const union ubifs_key *key) 1441e51764aSArtem Bityutskiy { 1451e51764aSArtem Bityutskiy /* dbg_lock must be held */ 1461e51764aSArtem Bityutskiy sprintf_key(c, key, dbg_key_buf0); 1471e51764aSArtem Bityutskiy return dbg_key_buf0; 1481e51764aSArtem Bityutskiy } 1491e51764aSArtem Bityutskiy 1501e51764aSArtem Bityutskiy const char *dbg_key_str1(const struct ubifs_info *c, const union ubifs_key *key) 1511e51764aSArtem Bityutskiy { 1521e51764aSArtem Bityutskiy /* dbg_lock must be held */ 1531e51764aSArtem Bityutskiy sprintf_key(c, key, dbg_key_buf1); 1541e51764aSArtem Bityutskiy return dbg_key_buf1; 1551e51764aSArtem Bityutskiy } 1561e51764aSArtem Bityutskiy 1571e51764aSArtem Bityutskiy const char *dbg_ntype(int type) 1581e51764aSArtem Bityutskiy { 1591e51764aSArtem Bityutskiy switch (type) { 1601e51764aSArtem Bityutskiy case UBIFS_PAD_NODE: 1611e51764aSArtem Bityutskiy return "padding node"; 1621e51764aSArtem Bityutskiy case UBIFS_SB_NODE: 1631e51764aSArtem Bityutskiy return "superblock node"; 1641e51764aSArtem Bityutskiy case UBIFS_MST_NODE: 1651e51764aSArtem Bityutskiy return "master node"; 1661e51764aSArtem Bityutskiy case UBIFS_REF_NODE: 1671e51764aSArtem Bityutskiy return "reference node"; 1681e51764aSArtem Bityutskiy case UBIFS_INO_NODE: 1691e51764aSArtem Bityutskiy return "inode node"; 1701e51764aSArtem Bityutskiy case UBIFS_DENT_NODE: 1711e51764aSArtem Bityutskiy return "direntry node"; 1721e51764aSArtem Bityutskiy case UBIFS_XENT_NODE: 1731e51764aSArtem Bityutskiy return "xentry node"; 1741e51764aSArtem Bityutskiy case UBIFS_DATA_NODE: 1751e51764aSArtem Bityutskiy return "data node"; 1761e51764aSArtem Bityutskiy case UBIFS_TRUN_NODE: 1771e51764aSArtem Bityutskiy return "truncate node"; 1781e51764aSArtem Bityutskiy case UBIFS_IDX_NODE: 1791e51764aSArtem Bityutskiy return "indexing node"; 1801e51764aSArtem Bityutskiy case UBIFS_CS_NODE: 1811e51764aSArtem Bityutskiy return "commit start node"; 1821e51764aSArtem Bityutskiy case UBIFS_ORPH_NODE: 1831e51764aSArtem Bityutskiy return "orphan node"; 1841e51764aSArtem Bityutskiy default: 1851e51764aSArtem Bityutskiy return "unknown node"; 1861e51764aSArtem Bityutskiy } 1871e51764aSArtem Bityutskiy } 1881e51764aSArtem Bityutskiy 1891e51764aSArtem Bityutskiy static const char *dbg_gtype(int type) 1901e51764aSArtem Bityutskiy { 1911e51764aSArtem Bityutskiy switch (type) { 1921e51764aSArtem Bityutskiy case UBIFS_NO_NODE_GROUP: 1931e51764aSArtem Bityutskiy return "no node group"; 1941e51764aSArtem Bityutskiy case UBIFS_IN_NODE_GROUP: 1951e51764aSArtem Bityutskiy return "in node group"; 1961e51764aSArtem Bityutskiy case UBIFS_LAST_OF_NODE_GROUP: 1971e51764aSArtem Bityutskiy return "last of node group"; 1981e51764aSArtem Bityutskiy default: 1991e51764aSArtem Bityutskiy return "unknown"; 2001e51764aSArtem Bityutskiy } 2011e51764aSArtem Bityutskiy } 2021e51764aSArtem Bityutskiy 2031e51764aSArtem Bityutskiy const char *dbg_cstate(int cmt_state) 2041e51764aSArtem Bityutskiy { 2051e51764aSArtem Bityutskiy switch (cmt_state) { 2061e51764aSArtem Bityutskiy case COMMIT_RESTING: 2071e51764aSArtem Bityutskiy return "commit resting"; 2081e51764aSArtem Bityutskiy case COMMIT_BACKGROUND: 2091e51764aSArtem Bityutskiy return "background commit requested"; 2101e51764aSArtem Bityutskiy case COMMIT_REQUIRED: 2111e51764aSArtem Bityutskiy return "commit required"; 2121e51764aSArtem Bityutskiy case COMMIT_RUNNING_BACKGROUND: 2131e51764aSArtem Bityutskiy return "BACKGROUND commit running"; 2141e51764aSArtem Bityutskiy case COMMIT_RUNNING_REQUIRED: 2151e51764aSArtem Bityutskiy return "commit running and required"; 2161e51764aSArtem Bityutskiy case COMMIT_BROKEN: 2171e51764aSArtem Bityutskiy return "broken commit"; 2181e51764aSArtem Bityutskiy default: 2191e51764aSArtem Bityutskiy return "unknown commit state"; 2201e51764aSArtem Bityutskiy } 2211e51764aSArtem Bityutskiy } 2221e51764aSArtem Bityutskiy 22377a7ae58SArtem Bityutskiy const char *dbg_jhead(int jhead) 22477a7ae58SArtem Bityutskiy { 22577a7ae58SArtem Bityutskiy switch (jhead) { 22677a7ae58SArtem Bityutskiy case GCHD: 22777a7ae58SArtem Bityutskiy return "0 (GC)"; 22877a7ae58SArtem Bityutskiy case BASEHD: 22977a7ae58SArtem Bityutskiy return "1 (base)"; 23077a7ae58SArtem Bityutskiy case DATAHD: 23177a7ae58SArtem Bityutskiy return "2 (data)"; 23277a7ae58SArtem Bityutskiy default: 23377a7ae58SArtem Bityutskiy return "unknown journal head"; 23477a7ae58SArtem Bityutskiy } 23577a7ae58SArtem Bityutskiy } 23677a7ae58SArtem Bityutskiy 2371e51764aSArtem Bityutskiy static void dump_ch(const struct ubifs_ch *ch) 2381e51764aSArtem Bityutskiy { 2391e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tmagic %#x\n", le32_to_cpu(ch->magic)); 2401e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tcrc %#x\n", le32_to_cpu(ch->crc)); 2411e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tnode_type %d (%s)\n", ch->node_type, 2421e51764aSArtem Bityutskiy dbg_ntype(ch->node_type)); 2431e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tgroup_type %d (%s)\n", ch->group_type, 2441e51764aSArtem Bityutskiy dbg_gtype(ch->group_type)); 2451e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tsqnum %llu\n", 2461e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(ch->sqnum)); 2471e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlen %u\n", le32_to_cpu(ch->len)); 2481e51764aSArtem Bityutskiy } 2491e51764aSArtem Bityutskiy 2504315fb40SArtem Bityutskiy void dbg_dump_inode(struct ubifs_info *c, const struct inode *inode) 2511e51764aSArtem Bityutskiy { 2521e51764aSArtem Bityutskiy const struct ubifs_inode *ui = ubifs_inode(inode); 2534315fb40SArtem Bityutskiy struct qstr nm = { .name = NULL }; 2544315fb40SArtem Bityutskiy union ubifs_key key; 2554315fb40SArtem Bityutskiy struct ubifs_dent_node *dent, *pdent = NULL; 2564315fb40SArtem Bityutskiy int count = 2; 2571e51764aSArtem Bityutskiy 258b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "Dump in-memory inode:"); 259b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tinode %lu\n", inode->i_ino); 260b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tsize %llu\n", 2611e51764aSArtem Bityutskiy (unsigned long long)i_size_read(inode)); 262b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tnlink %u\n", inode->i_nlink); 263b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tuid %u\n", (unsigned int)inode->i_uid); 264b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tgid %u\n", (unsigned int)inode->i_gid); 265b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tatime %u.%u\n", 2661e51764aSArtem Bityutskiy (unsigned int)inode->i_atime.tv_sec, 2671e51764aSArtem Bityutskiy (unsigned int)inode->i_atime.tv_nsec); 268b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tmtime %u.%u\n", 2691e51764aSArtem Bityutskiy (unsigned int)inode->i_mtime.tv_sec, 2701e51764aSArtem Bityutskiy (unsigned int)inode->i_mtime.tv_nsec); 271b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tctime %u.%u\n", 2721e51764aSArtem Bityutskiy (unsigned int)inode->i_ctime.tv_sec, 2731e51764aSArtem Bityutskiy (unsigned int)inode->i_ctime.tv_nsec); 274b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tcreat_sqnum %llu\n", ui->creat_sqnum); 275b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\txattr_size %u\n", ui->xattr_size); 276b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\txattr_cnt %u\n", ui->xattr_cnt); 277b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\txattr_names %u\n", ui->xattr_names); 278b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tdirty %u\n", ui->dirty); 279b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\txattr %u\n", ui->xattr); 280b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tbulk_read %u\n", ui->xattr); 281b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tsynced_i_size %llu\n", 282b5e426e9SArtem Bityutskiy (unsigned long long)ui->synced_i_size); 283b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tui_size %llu\n", 284b5e426e9SArtem Bityutskiy (unsigned long long)ui->ui_size); 285b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tflags %d\n", ui->flags); 286b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tcompr_type %d\n", ui->compr_type); 287b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tlast_page_read %lu\n", ui->last_page_read); 288b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tread_in_a_row %lu\n", ui->read_in_a_row); 289b5e426e9SArtem Bityutskiy printk(KERN_DEBUG "\tdata_len %d\n", ui->data_len); 2904315fb40SArtem Bityutskiy 2914315fb40SArtem Bityutskiy if (!S_ISDIR(inode->i_mode)) 2924315fb40SArtem Bityutskiy return; 2934315fb40SArtem Bityutskiy 2944315fb40SArtem Bityutskiy printk(KERN_DEBUG "List of directory entries:\n"); 2954315fb40SArtem Bityutskiy ubifs_assert(!mutex_is_locked(&c->tnc_mutex)); 2964315fb40SArtem Bityutskiy 2974315fb40SArtem Bityutskiy lowest_dent_key(c, &key, inode->i_ino); 2984315fb40SArtem Bityutskiy while (1) { 2994315fb40SArtem Bityutskiy dent = ubifs_tnc_next_ent(c, &key, &nm); 3004315fb40SArtem Bityutskiy if (IS_ERR(dent)) { 3014315fb40SArtem Bityutskiy if (PTR_ERR(dent) != -ENOENT) 3024315fb40SArtem Bityutskiy printk(KERN_DEBUG "error %ld\n", PTR_ERR(dent)); 3034315fb40SArtem Bityutskiy break; 3044315fb40SArtem Bityutskiy } 3054315fb40SArtem Bityutskiy 3064315fb40SArtem Bityutskiy printk(KERN_DEBUG "\t%d: %s (%s)\n", 3074315fb40SArtem Bityutskiy count++, dent->name, get_dent_type(dent->type)); 3084315fb40SArtem Bityutskiy 3094315fb40SArtem Bityutskiy nm.name = dent->name; 3104315fb40SArtem Bityutskiy nm.len = le16_to_cpu(dent->nlen); 3114315fb40SArtem Bityutskiy kfree(pdent); 3124315fb40SArtem Bityutskiy pdent = dent; 3134315fb40SArtem Bityutskiy key_read(c, &dent->key, &key); 3144315fb40SArtem Bityutskiy } 3154315fb40SArtem Bityutskiy kfree(pdent); 3161e51764aSArtem Bityutskiy } 3171e51764aSArtem Bityutskiy 3181e51764aSArtem Bityutskiy void dbg_dump_node(const struct ubifs_info *c, const void *node) 3191e51764aSArtem Bityutskiy { 3201e51764aSArtem Bityutskiy int i, n; 3211e51764aSArtem Bityutskiy union ubifs_key key; 3221e51764aSArtem Bityutskiy const struct ubifs_ch *ch = node; 3231e51764aSArtem Bityutskiy 3242b1844a8SArtem Bityutskiy if (dbg_is_tst_rcvry(c)) 3251e51764aSArtem Bityutskiy return; 3261e51764aSArtem Bityutskiy 3271e51764aSArtem Bityutskiy /* If the magic is incorrect, just hexdump the first bytes */ 3281e51764aSArtem Bityutskiy if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) { 3291e51764aSArtem Bityutskiy printk(KERN_DEBUG "Not a node, first %zu bytes:", UBIFS_CH_SZ); 3301e51764aSArtem Bityutskiy print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, 3311e51764aSArtem Bityutskiy (void *)node, UBIFS_CH_SZ, 1); 3321e51764aSArtem Bityutskiy return; 3331e51764aSArtem Bityutskiy } 3341e51764aSArtem Bityutskiy 3351e51764aSArtem Bityutskiy spin_lock(&dbg_lock); 3361e51764aSArtem Bityutskiy dump_ch(node); 3371e51764aSArtem Bityutskiy 3381e51764aSArtem Bityutskiy switch (ch->node_type) { 3391e51764aSArtem Bityutskiy case UBIFS_PAD_NODE: 3401e51764aSArtem Bityutskiy { 3411e51764aSArtem Bityutskiy const struct ubifs_pad_node *pad = node; 3421e51764aSArtem Bityutskiy 3431e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tpad_len %u\n", 3441e51764aSArtem Bityutskiy le32_to_cpu(pad->pad_len)); 3451e51764aSArtem Bityutskiy break; 3461e51764aSArtem Bityutskiy } 3471e51764aSArtem Bityutskiy case UBIFS_SB_NODE: 3481e51764aSArtem Bityutskiy { 3491e51764aSArtem Bityutskiy const struct ubifs_sb_node *sup = node; 3501e51764aSArtem Bityutskiy unsigned int sup_flags = le32_to_cpu(sup->flags); 3511e51764aSArtem Bityutskiy 3521e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tkey_hash %d (%s)\n", 3531e51764aSArtem Bityutskiy (int)sup->key_hash, get_key_hash(sup->key_hash)); 3541e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tkey_fmt %d (%s)\n", 3551e51764aSArtem Bityutskiy (int)sup->key_fmt, get_key_fmt(sup->key_fmt)); 3561e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tflags %#x\n", sup_flags); 3571e51764aSArtem Bityutskiy printk(KERN_DEBUG "\t big_lpt %u\n", 3581e51764aSArtem Bityutskiy !!(sup_flags & UBIFS_FLG_BIGLPT)); 3599f58d350SMatthew L. Creech printk(KERN_DEBUG "\t space_fixup %u\n", 3609f58d350SMatthew L. Creech !!(sup_flags & UBIFS_FLG_SPACE_FIXUP)); 3611e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tmin_io_size %u\n", 3621e51764aSArtem Bityutskiy le32_to_cpu(sup->min_io_size)); 3631e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tleb_size %u\n", 3641e51764aSArtem Bityutskiy le32_to_cpu(sup->leb_size)); 3651e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tleb_cnt %u\n", 3661e51764aSArtem Bityutskiy le32_to_cpu(sup->leb_cnt)); 3671e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tmax_leb_cnt %u\n", 3681e51764aSArtem Bityutskiy le32_to_cpu(sup->max_leb_cnt)); 3691e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tmax_bud_bytes %llu\n", 3701e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(sup->max_bud_bytes)); 3711e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlog_lebs %u\n", 3721e51764aSArtem Bityutskiy le32_to_cpu(sup->log_lebs)); 3731e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlpt_lebs %u\n", 3741e51764aSArtem Bityutskiy le32_to_cpu(sup->lpt_lebs)); 3751e51764aSArtem Bityutskiy printk(KERN_DEBUG "\torph_lebs %u\n", 3761e51764aSArtem Bityutskiy le32_to_cpu(sup->orph_lebs)); 3771e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tjhead_cnt %u\n", 3781e51764aSArtem Bityutskiy le32_to_cpu(sup->jhead_cnt)); 3791e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tfanout %u\n", 3801e51764aSArtem Bityutskiy le32_to_cpu(sup->fanout)); 3811e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlsave_cnt %u\n", 3821e51764aSArtem Bityutskiy le32_to_cpu(sup->lsave_cnt)); 3831e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tdefault_compr %u\n", 3841e51764aSArtem Bityutskiy (int)le16_to_cpu(sup->default_compr)); 3851e51764aSArtem Bityutskiy printk(KERN_DEBUG "\trp_size %llu\n", 3861e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(sup->rp_size)); 3871e51764aSArtem Bityutskiy printk(KERN_DEBUG "\trp_uid %u\n", 3881e51764aSArtem Bityutskiy le32_to_cpu(sup->rp_uid)); 3891e51764aSArtem Bityutskiy printk(KERN_DEBUG "\trp_gid %u\n", 3901e51764aSArtem Bityutskiy le32_to_cpu(sup->rp_gid)); 3911e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tfmt_version %u\n", 3921e51764aSArtem Bityutskiy le32_to_cpu(sup->fmt_version)); 3931e51764aSArtem Bityutskiy printk(KERN_DEBUG "\ttime_gran %u\n", 3941e51764aSArtem Bityutskiy le32_to_cpu(sup->time_gran)); 3957f2f4e72SJoe Perches printk(KERN_DEBUG "\tUUID %pUB\n", 3967f2f4e72SJoe Perches sup->uuid); 3971e51764aSArtem Bityutskiy break; 3981e51764aSArtem Bityutskiy } 3991e51764aSArtem Bityutskiy case UBIFS_MST_NODE: 4001e51764aSArtem Bityutskiy { 4011e51764aSArtem Bityutskiy const struct ubifs_mst_node *mst = node; 4021e51764aSArtem Bityutskiy 4031e51764aSArtem Bityutskiy printk(KERN_DEBUG "\thighest_inum %llu\n", 4041e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->highest_inum)); 4051e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tcommit number %llu\n", 4061e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->cmt_no)); 4071e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tflags %#x\n", 4081e51764aSArtem Bityutskiy le32_to_cpu(mst->flags)); 4091e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlog_lnum %u\n", 4101e51764aSArtem Bityutskiy le32_to_cpu(mst->log_lnum)); 4111e51764aSArtem Bityutskiy printk(KERN_DEBUG "\troot_lnum %u\n", 4121e51764aSArtem Bityutskiy le32_to_cpu(mst->root_lnum)); 4131e51764aSArtem Bityutskiy printk(KERN_DEBUG "\troot_offs %u\n", 4141e51764aSArtem Bityutskiy le32_to_cpu(mst->root_offs)); 4151e51764aSArtem Bityutskiy printk(KERN_DEBUG "\troot_len %u\n", 4161e51764aSArtem Bityutskiy le32_to_cpu(mst->root_len)); 4171e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tgc_lnum %u\n", 4181e51764aSArtem Bityutskiy le32_to_cpu(mst->gc_lnum)); 4191e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tihead_lnum %u\n", 4201e51764aSArtem Bityutskiy le32_to_cpu(mst->ihead_lnum)); 4211e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tihead_offs %u\n", 4221e51764aSArtem Bityutskiy le32_to_cpu(mst->ihead_offs)); 4230ecb9529SHarvey Harrison printk(KERN_DEBUG "\tindex_size %llu\n", 4240ecb9529SHarvey Harrison (unsigned long long)le64_to_cpu(mst->index_size)); 4251e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlpt_lnum %u\n", 4261e51764aSArtem Bityutskiy le32_to_cpu(mst->lpt_lnum)); 4271e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlpt_offs %u\n", 4281e51764aSArtem Bityutskiy le32_to_cpu(mst->lpt_offs)); 4291e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tnhead_lnum %u\n", 4301e51764aSArtem Bityutskiy le32_to_cpu(mst->nhead_lnum)); 4311e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tnhead_offs %u\n", 4321e51764aSArtem Bityutskiy le32_to_cpu(mst->nhead_offs)); 4331e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tltab_lnum %u\n", 4341e51764aSArtem Bityutskiy le32_to_cpu(mst->ltab_lnum)); 4351e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tltab_offs %u\n", 4361e51764aSArtem Bityutskiy le32_to_cpu(mst->ltab_offs)); 4371e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlsave_lnum %u\n", 4381e51764aSArtem Bityutskiy le32_to_cpu(mst->lsave_lnum)); 4391e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlsave_offs %u\n", 4401e51764aSArtem Bityutskiy le32_to_cpu(mst->lsave_offs)); 4411e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlscan_lnum %u\n", 4421e51764aSArtem Bityutskiy le32_to_cpu(mst->lscan_lnum)); 4431e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tleb_cnt %u\n", 4441e51764aSArtem Bityutskiy le32_to_cpu(mst->leb_cnt)); 4451e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tempty_lebs %u\n", 4461e51764aSArtem Bityutskiy le32_to_cpu(mst->empty_lebs)); 4471e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tidx_lebs %u\n", 4481e51764aSArtem Bityutskiy le32_to_cpu(mst->idx_lebs)); 4491e51764aSArtem Bityutskiy printk(KERN_DEBUG "\ttotal_free %llu\n", 4501e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->total_free)); 4511e51764aSArtem Bityutskiy printk(KERN_DEBUG "\ttotal_dirty %llu\n", 4521e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->total_dirty)); 4531e51764aSArtem Bityutskiy printk(KERN_DEBUG "\ttotal_used %llu\n", 4541e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->total_used)); 4551e51764aSArtem Bityutskiy printk(KERN_DEBUG "\ttotal_dead %llu\n", 4561e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->total_dead)); 4571e51764aSArtem Bityutskiy printk(KERN_DEBUG "\ttotal_dark %llu\n", 4581e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->total_dark)); 4591e51764aSArtem Bityutskiy break; 4601e51764aSArtem Bityutskiy } 4611e51764aSArtem Bityutskiy case UBIFS_REF_NODE: 4621e51764aSArtem Bityutskiy { 4631e51764aSArtem Bityutskiy const struct ubifs_ref_node *ref = node; 4641e51764aSArtem Bityutskiy 4651e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlnum %u\n", 4661e51764aSArtem Bityutskiy le32_to_cpu(ref->lnum)); 4671e51764aSArtem Bityutskiy printk(KERN_DEBUG "\toffs %u\n", 4681e51764aSArtem Bityutskiy le32_to_cpu(ref->offs)); 4691e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tjhead %u\n", 4701e51764aSArtem Bityutskiy le32_to_cpu(ref->jhead)); 4711e51764aSArtem Bityutskiy break; 4721e51764aSArtem Bityutskiy } 4731e51764aSArtem Bityutskiy case UBIFS_INO_NODE: 4741e51764aSArtem Bityutskiy { 4751e51764aSArtem Bityutskiy const struct ubifs_ino_node *ino = node; 4761e51764aSArtem Bityutskiy 4771e51764aSArtem Bityutskiy key_read(c, &ino->key, &key); 4781e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key)); 4791e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tcreat_sqnum %llu\n", 4801e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(ino->creat_sqnum)); 4811e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tsize %llu\n", 4821e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(ino->size)); 4831e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tnlink %u\n", 4841e51764aSArtem Bityutskiy le32_to_cpu(ino->nlink)); 4851e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tatime %lld.%u\n", 4861e51764aSArtem Bityutskiy (long long)le64_to_cpu(ino->atime_sec), 4871e51764aSArtem Bityutskiy le32_to_cpu(ino->atime_nsec)); 4881e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tmtime %lld.%u\n", 4891e51764aSArtem Bityutskiy (long long)le64_to_cpu(ino->mtime_sec), 4901e51764aSArtem Bityutskiy le32_to_cpu(ino->mtime_nsec)); 4911e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tctime %lld.%u\n", 4921e51764aSArtem Bityutskiy (long long)le64_to_cpu(ino->ctime_sec), 4931e51764aSArtem Bityutskiy le32_to_cpu(ino->ctime_nsec)); 4941e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tuid %u\n", 4951e51764aSArtem Bityutskiy le32_to_cpu(ino->uid)); 4961e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tgid %u\n", 4971e51764aSArtem Bityutskiy le32_to_cpu(ino->gid)); 4981e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tmode %u\n", 4991e51764aSArtem Bityutskiy le32_to_cpu(ino->mode)); 5001e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tflags %#x\n", 5011e51764aSArtem Bityutskiy le32_to_cpu(ino->flags)); 5021e51764aSArtem Bityutskiy printk(KERN_DEBUG "\txattr_cnt %u\n", 5031e51764aSArtem Bityutskiy le32_to_cpu(ino->xattr_cnt)); 5041e51764aSArtem Bityutskiy printk(KERN_DEBUG "\txattr_size %u\n", 5051e51764aSArtem Bityutskiy le32_to_cpu(ino->xattr_size)); 5061e51764aSArtem Bityutskiy printk(KERN_DEBUG "\txattr_names %u\n", 5071e51764aSArtem Bityutskiy le32_to_cpu(ino->xattr_names)); 5081e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tcompr_type %#x\n", 5091e51764aSArtem Bityutskiy (int)le16_to_cpu(ino->compr_type)); 5101e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tdata len %u\n", 5111e51764aSArtem Bityutskiy le32_to_cpu(ino->data_len)); 5121e51764aSArtem Bityutskiy break; 5131e51764aSArtem Bityutskiy } 5141e51764aSArtem Bityutskiy case UBIFS_DENT_NODE: 5151e51764aSArtem Bityutskiy case UBIFS_XENT_NODE: 5161e51764aSArtem Bityutskiy { 5171e51764aSArtem Bityutskiy const struct ubifs_dent_node *dent = node; 5181e51764aSArtem Bityutskiy int nlen = le16_to_cpu(dent->nlen); 5191e51764aSArtem Bityutskiy 5201e51764aSArtem Bityutskiy key_read(c, &dent->key, &key); 5211e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key)); 5221e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tinum %llu\n", 5231e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(dent->inum)); 5241e51764aSArtem Bityutskiy printk(KERN_DEBUG "\ttype %d\n", (int)dent->type); 5251e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tnlen %d\n", nlen); 5261e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tname "); 5271e51764aSArtem Bityutskiy 5281e51764aSArtem Bityutskiy if (nlen > UBIFS_MAX_NLEN) 5291e51764aSArtem Bityutskiy printk(KERN_DEBUG "(bad name length, not printing, " 5301e51764aSArtem Bityutskiy "bad or corrupted node)"); 5311e51764aSArtem Bityutskiy else { 5321e51764aSArtem Bityutskiy for (i = 0; i < nlen && dent->name[i]; i++) 533c9927c3eSArtem Bityutskiy printk(KERN_CONT "%c", dent->name[i]); 5341e51764aSArtem Bityutskiy } 535c9927c3eSArtem Bityutskiy printk(KERN_CONT "\n"); 5361e51764aSArtem Bityutskiy 5371e51764aSArtem Bityutskiy break; 5381e51764aSArtem Bityutskiy } 5391e51764aSArtem Bityutskiy case UBIFS_DATA_NODE: 5401e51764aSArtem Bityutskiy { 5411e51764aSArtem Bityutskiy const struct ubifs_data_node *dn = node; 5421e51764aSArtem Bityutskiy int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ; 5431e51764aSArtem Bityutskiy 5441e51764aSArtem Bityutskiy key_read(c, &dn->key, &key); 5451e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key)); 5461e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tsize %u\n", 5471e51764aSArtem Bityutskiy le32_to_cpu(dn->size)); 5481e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tcompr_typ %d\n", 5491e51764aSArtem Bityutskiy (int)le16_to_cpu(dn->compr_type)); 5501e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tdata size %d\n", 5511e51764aSArtem Bityutskiy dlen); 5521e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tdata:\n"); 5531e51764aSArtem Bityutskiy print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET, 32, 1, 5541e51764aSArtem Bityutskiy (void *)&dn->data, dlen, 0); 5551e51764aSArtem Bityutskiy break; 5561e51764aSArtem Bityutskiy } 5571e51764aSArtem Bityutskiy case UBIFS_TRUN_NODE: 5581e51764aSArtem Bityutskiy { 5591e51764aSArtem Bityutskiy const struct ubifs_trun_node *trun = node; 5601e51764aSArtem Bityutskiy 5611e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tinum %u\n", 5621e51764aSArtem Bityutskiy le32_to_cpu(trun->inum)); 5631e51764aSArtem Bityutskiy printk(KERN_DEBUG "\told_size %llu\n", 5641e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(trun->old_size)); 5651e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tnew_size %llu\n", 5661e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(trun->new_size)); 5671e51764aSArtem Bityutskiy break; 5681e51764aSArtem Bityutskiy } 5691e51764aSArtem Bityutskiy case UBIFS_IDX_NODE: 5701e51764aSArtem Bityutskiy { 5711e51764aSArtem Bityutskiy const struct ubifs_idx_node *idx = node; 5721e51764aSArtem Bityutskiy 5731e51764aSArtem Bityutskiy n = le16_to_cpu(idx->child_cnt); 5741e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tchild_cnt %d\n", n); 5751e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlevel %d\n", 5761e51764aSArtem Bityutskiy (int)le16_to_cpu(idx->level)); 5771e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tBranches:\n"); 5781e51764aSArtem Bityutskiy 5791e51764aSArtem Bityutskiy for (i = 0; i < n && i < c->fanout - 1; i++) { 5801e51764aSArtem Bityutskiy const struct ubifs_branch *br; 5811e51764aSArtem Bityutskiy 5821e51764aSArtem Bityutskiy br = ubifs_idx_branch(c, idx, i); 5831e51764aSArtem Bityutskiy key_read(c, &br->key, &key); 5841e51764aSArtem Bityutskiy printk(KERN_DEBUG "\t%d: LEB %d:%d len %d key %s\n", 5851e51764aSArtem Bityutskiy i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs), 5861e51764aSArtem Bityutskiy le32_to_cpu(br->len), DBGKEY(&key)); 5871e51764aSArtem Bityutskiy } 5881e51764aSArtem Bityutskiy break; 5891e51764aSArtem Bityutskiy } 5901e51764aSArtem Bityutskiy case UBIFS_CS_NODE: 5911e51764aSArtem Bityutskiy break; 5921e51764aSArtem Bityutskiy case UBIFS_ORPH_NODE: 5931e51764aSArtem Bityutskiy { 5941e51764aSArtem Bityutskiy const struct ubifs_orph_node *orph = node; 5951e51764aSArtem Bityutskiy 5961e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tcommit number %llu\n", 5971e51764aSArtem Bityutskiy (unsigned long long) 5981e51764aSArtem Bityutskiy le64_to_cpu(orph->cmt_no) & LLONG_MAX); 5991e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tlast node flag %llu\n", 6001e51764aSArtem Bityutskiy (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63); 6011e51764aSArtem Bityutskiy n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3; 6021e51764aSArtem Bityutskiy printk(KERN_DEBUG "\t%d orphan inode numbers:\n", n); 6031e51764aSArtem Bityutskiy for (i = 0; i < n; i++) 6041e51764aSArtem Bityutskiy printk(KERN_DEBUG "\t ino %llu\n", 6057424bac8SAlexander Beregalov (unsigned long long)le64_to_cpu(orph->inos[i])); 6061e51764aSArtem Bityutskiy break; 6071e51764aSArtem Bityutskiy } 6081e51764aSArtem Bityutskiy default: 6091e51764aSArtem Bityutskiy printk(KERN_DEBUG "node type %d was not recognized\n", 6101e51764aSArtem Bityutskiy (int)ch->node_type); 6111e51764aSArtem Bityutskiy } 6121e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 6131e51764aSArtem Bityutskiy } 6141e51764aSArtem Bityutskiy 6151e51764aSArtem Bityutskiy void dbg_dump_budget_req(const struct ubifs_budget_req *req) 6161e51764aSArtem Bityutskiy { 6171e51764aSArtem Bityutskiy spin_lock(&dbg_lock); 6181e51764aSArtem Bityutskiy printk(KERN_DEBUG "Budgeting request: new_ino %d, dirtied_ino %d\n", 6191e51764aSArtem Bityutskiy req->new_ino, req->dirtied_ino); 6201e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tnew_ino_d %d, dirtied_ino_d %d\n", 6211e51764aSArtem Bityutskiy req->new_ino_d, req->dirtied_ino_d); 6221e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tnew_page %d, dirtied_page %d\n", 6231e51764aSArtem Bityutskiy req->new_page, req->dirtied_page); 6241e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tnew_dent %d, mod_dent %d\n", 6251e51764aSArtem Bityutskiy req->new_dent, req->mod_dent); 6261e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tidx_growth %d\n", req->idx_growth); 6271e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tdata_growth %d dd_growth %d\n", 6281e51764aSArtem Bityutskiy req->data_growth, req->dd_growth); 6291e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 6301e51764aSArtem Bityutskiy } 6311e51764aSArtem Bityutskiy 6321e51764aSArtem Bityutskiy void dbg_dump_lstats(const struct ubifs_lp_stats *lst) 6331e51764aSArtem Bityutskiy { 6341e51764aSArtem Bityutskiy spin_lock(&dbg_lock); 6351de94159SArtem Bityutskiy printk(KERN_DEBUG "(pid %d) Lprops statistics: empty_lebs %d, " 6361de94159SArtem Bityutskiy "idx_lebs %d\n", current->pid, lst->empty_lebs, lst->idx_lebs); 6371e51764aSArtem Bityutskiy printk(KERN_DEBUG "\ttaken_empty_lebs %d, total_free %lld, " 6381e51764aSArtem Bityutskiy "total_dirty %lld\n", lst->taken_empty_lebs, lst->total_free, 6391e51764aSArtem Bityutskiy lst->total_dirty); 6401e51764aSArtem Bityutskiy printk(KERN_DEBUG "\ttotal_used %lld, total_dark %lld, " 6411e51764aSArtem Bityutskiy "total_dead %lld\n", lst->total_used, lst->total_dark, 6421e51764aSArtem Bityutskiy lst->total_dead); 6431e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 6441e51764aSArtem Bityutskiy } 6451e51764aSArtem Bityutskiy 646f1bd66afSArtem Bityutskiy void dbg_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi) 6471e51764aSArtem Bityutskiy { 6481e51764aSArtem Bityutskiy int i; 6491e51764aSArtem Bityutskiy struct rb_node *rb; 6501e51764aSArtem Bityutskiy struct ubifs_bud *bud; 6511e51764aSArtem Bityutskiy struct ubifs_gced_idx_leb *idx_gc; 65221a60258SArtem Bityutskiy long long available, outstanding, free; 6531e51764aSArtem Bityutskiy 6548ff83089SArtem Bityutskiy spin_lock(&c->space_lock); 6551e51764aSArtem Bityutskiy spin_lock(&dbg_lock); 6568c3067e4SArtem Bityutskiy printk(KERN_DEBUG "(pid %d) Budgeting info: data budget sum %lld, " 6578c3067e4SArtem Bityutskiy "total budget sum %lld\n", current->pid, 658f1bd66afSArtem Bityutskiy bi->data_growth + bi->dd_growth, 659f1bd66afSArtem Bityutskiy bi->data_growth + bi->dd_growth + bi->idx_growth); 6608c3067e4SArtem Bityutskiy printk(KERN_DEBUG "\tbudg_data_growth %lld, budg_dd_growth %lld, " 661f1bd66afSArtem Bityutskiy "budg_idx_growth %lld\n", bi->data_growth, bi->dd_growth, 662f1bd66afSArtem Bityutskiy bi->idx_growth); 6638c3067e4SArtem Bityutskiy printk(KERN_DEBUG "\tmin_idx_lebs %d, old_idx_sz %llu, " 664f1bd66afSArtem Bityutskiy "uncommitted_idx %lld\n", bi->min_idx_lebs, bi->old_idx_sz, 665f1bd66afSArtem Bityutskiy bi->uncommitted_idx); 6668c3067e4SArtem Bityutskiy printk(KERN_DEBUG "\tpage_budget %d, inode_budget %d, dent_budget %d\n", 667f1bd66afSArtem Bityutskiy bi->page_budget, bi->inode_budget, bi->dent_budget); 6688c3067e4SArtem Bityutskiy printk(KERN_DEBUG "\tnospace %u, nospace_rp %u\n", 669f1bd66afSArtem Bityutskiy bi->nospace, bi->nospace_rp); 6708c3067e4SArtem Bityutskiy printk(KERN_DEBUG "\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n", 6718c3067e4SArtem Bityutskiy c->dark_wm, c->dead_wm, c->max_idx_node_sz); 672f1bd66afSArtem Bityutskiy 673f1bd66afSArtem Bityutskiy if (bi != &c->bi) 674f1bd66afSArtem Bityutskiy /* 675f1bd66afSArtem Bityutskiy * If we are dumping saved budgeting data, do not print 676f1bd66afSArtem Bityutskiy * additional information which is about the current state, not 677f1bd66afSArtem Bityutskiy * the old one which corresponded to the saved budgeting data. 678f1bd66afSArtem Bityutskiy */ 679f1bd66afSArtem Bityutskiy goto out_unlock; 680f1bd66afSArtem Bityutskiy 6818c3067e4SArtem Bityutskiy printk(KERN_DEBUG "\tfreeable_cnt %d, calc_idx_sz %lld, idx_gc_cnt %d\n", 6828c3067e4SArtem Bityutskiy c->freeable_cnt, c->calc_idx_sz, c->idx_gc_cnt); 6831e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, " 6841e51764aSArtem Bityutskiy "clean_zn_cnt %ld\n", atomic_long_read(&c->dirty_pg_cnt), 6851e51764aSArtem Bityutskiy atomic_long_read(&c->dirty_zn_cnt), 6861e51764aSArtem Bityutskiy atomic_long_read(&c->clean_zn_cnt)); 6871e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tgc_lnum %d, ihead_lnum %d\n", 6881e51764aSArtem Bityutskiy c->gc_lnum, c->ihead_lnum); 689f1bd66afSArtem Bityutskiy 69084abf972SArtem Bityutskiy /* If we are in R/O mode, journal heads do not exist */ 69184abf972SArtem Bityutskiy if (c->jheads) 6921e51764aSArtem Bityutskiy for (i = 0; i < c->jhead_cnt; i++) 69377a7ae58SArtem Bityutskiy printk(KERN_DEBUG "\tjhead %s\t LEB %d\n", 69477a7ae58SArtem Bityutskiy dbg_jhead(c->jheads[i].wbuf.jhead), 69577a7ae58SArtem Bityutskiy c->jheads[i].wbuf.lnum); 6961e51764aSArtem Bityutskiy for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) { 6971e51764aSArtem Bityutskiy bud = rb_entry(rb, struct ubifs_bud, rb); 6981e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tbud LEB %d\n", bud->lnum); 6991e51764aSArtem Bityutskiy } 7001e51764aSArtem Bityutskiy list_for_each_entry(bud, &c->old_buds, list) 7011e51764aSArtem Bityutskiy printk(KERN_DEBUG "\told bud LEB %d\n", bud->lnum); 7021e51764aSArtem Bityutskiy list_for_each_entry(idx_gc, &c->idx_gc, list) 7031e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tGC'ed idx LEB %d unmap %d\n", 7041e51764aSArtem Bityutskiy idx_gc->lnum, idx_gc->unmap); 7051e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tcommit state %d\n", c->cmt_state); 70621a60258SArtem Bityutskiy 70721a60258SArtem Bityutskiy /* Print budgeting predictions */ 708b137545cSArtem Bityutskiy available = ubifs_calc_available(c, c->bi.min_idx_lebs); 709b137545cSArtem Bityutskiy outstanding = c->bi.data_growth + c->bi.dd_growth; 71084abf972SArtem Bityutskiy free = ubifs_get_free_space_nolock(c); 71121a60258SArtem Bityutskiy printk(KERN_DEBUG "Budgeting predictions:\n"); 71221a60258SArtem Bityutskiy printk(KERN_DEBUG "\tavailable: %lld, outstanding %lld, free %lld\n", 71321a60258SArtem Bityutskiy available, outstanding, free); 714f1bd66afSArtem Bityutskiy out_unlock: 7151e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 7168ff83089SArtem Bityutskiy spin_unlock(&c->space_lock); 7171e51764aSArtem Bityutskiy } 7181e51764aSArtem Bityutskiy 7191e51764aSArtem Bityutskiy void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp) 7201e51764aSArtem Bityutskiy { 721be9e62a7SArtem Bityutskiy int i, spc, dark = 0, dead = 0; 722be9e62a7SArtem Bityutskiy struct rb_node *rb; 723be9e62a7SArtem Bityutskiy struct ubifs_bud *bud; 724be9e62a7SArtem Bityutskiy 725be9e62a7SArtem Bityutskiy spc = lp->free + lp->dirty; 726be9e62a7SArtem Bityutskiy if (spc < c->dead_wm) 727be9e62a7SArtem Bityutskiy dead = spc; 728be9e62a7SArtem Bityutskiy else 729be9e62a7SArtem Bityutskiy dark = ubifs_calc_dark(c, spc); 730be9e62a7SArtem Bityutskiy 731be9e62a7SArtem Bityutskiy if (lp->flags & LPROPS_INDEX) 732be9e62a7SArtem Bityutskiy printk(KERN_DEBUG "LEB %-7d free %-8d dirty %-8d used %-8d " 733be9e62a7SArtem Bityutskiy "free + dirty %-8d flags %#x (", lp->lnum, lp->free, 734be9e62a7SArtem Bityutskiy lp->dirty, c->leb_size - spc, spc, lp->flags); 735be9e62a7SArtem Bityutskiy else 736be9e62a7SArtem Bityutskiy printk(KERN_DEBUG "LEB %-7d free %-8d dirty %-8d used %-8d " 737be9e62a7SArtem Bityutskiy "free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d " 738be9e62a7SArtem Bityutskiy "flags %#-4x (", lp->lnum, lp->free, lp->dirty, 739be9e62a7SArtem Bityutskiy c->leb_size - spc, spc, dark, dead, 740be9e62a7SArtem Bityutskiy (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags); 741be9e62a7SArtem Bityutskiy 742be9e62a7SArtem Bityutskiy if (lp->flags & LPROPS_TAKEN) { 743be9e62a7SArtem Bityutskiy if (lp->flags & LPROPS_INDEX) 744be9e62a7SArtem Bityutskiy printk(KERN_CONT "index, taken"); 745be9e62a7SArtem Bityutskiy else 746be9e62a7SArtem Bityutskiy printk(KERN_CONT "taken"); 747be9e62a7SArtem Bityutskiy } else { 748be9e62a7SArtem Bityutskiy const char *s; 749be9e62a7SArtem Bityutskiy 750be9e62a7SArtem Bityutskiy if (lp->flags & LPROPS_INDEX) { 751be9e62a7SArtem Bityutskiy switch (lp->flags & LPROPS_CAT_MASK) { 752be9e62a7SArtem Bityutskiy case LPROPS_DIRTY_IDX: 753be9e62a7SArtem Bityutskiy s = "dirty index"; 754be9e62a7SArtem Bityutskiy break; 755be9e62a7SArtem Bityutskiy case LPROPS_FRDI_IDX: 756be9e62a7SArtem Bityutskiy s = "freeable index"; 757be9e62a7SArtem Bityutskiy break; 758be9e62a7SArtem Bityutskiy default: 759be9e62a7SArtem Bityutskiy s = "index"; 760be9e62a7SArtem Bityutskiy } 761be9e62a7SArtem Bityutskiy } else { 762be9e62a7SArtem Bityutskiy switch (lp->flags & LPROPS_CAT_MASK) { 763be9e62a7SArtem Bityutskiy case LPROPS_UNCAT: 764be9e62a7SArtem Bityutskiy s = "not categorized"; 765be9e62a7SArtem Bityutskiy break; 766be9e62a7SArtem Bityutskiy case LPROPS_DIRTY: 767be9e62a7SArtem Bityutskiy s = "dirty"; 768be9e62a7SArtem Bityutskiy break; 769be9e62a7SArtem Bityutskiy case LPROPS_FREE: 770be9e62a7SArtem Bityutskiy s = "free"; 771be9e62a7SArtem Bityutskiy break; 772be9e62a7SArtem Bityutskiy case LPROPS_EMPTY: 773be9e62a7SArtem Bityutskiy s = "empty"; 774be9e62a7SArtem Bityutskiy break; 775be9e62a7SArtem Bityutskiy case LPROPS_FREEABLE: 776be9e62a7SArtem Bityutskiy s = "freeable"; 777be9e62a7SArtem Bityutskiy break; 778be9e62a7SArtem Bityutskiy default: 779be9e62a7SArtem Bityutskiy s = NULL; 780be9e62a7SArtem Bityutskiy break; 781be9e62a7SArtem Bityutskiy } 782be9e62a7SArtem Bityutskiy } 783be9e62a7SArtem Bityutskiy printk(KERN_CONT "%s", s); 784be9e62a7SArtem Bityutskiy } 785be9e62a7SArtem Bityutskiy 786be9e62a7SArtem Bityutskiy for (rb = rb_first((struct rb_root *)&c->buds); rb; rb = rb_next(rb)) { 787be9e62a7SArtem Bityutskiy bud = rb_entry(rb, struct ubifs_bud, rb); 788be9e62a7SArtem Bityutskiy if (bud->lnum == lp->lnum) { 789be9e62a7SArtem Bityutskiy int head = 0; 790be9e62a7SArtem Bityutskiy for (i = 0; i < c->jhead_cnt; i++) { 7911321657dSArtem Bityutskiy /* 7921321657dSArtem Bityutskiy * Note, if we are in R/O mode or in the middle 7931321657dSArtem Bityutskiy * of mounting/re-mounting, the write-buffers do 7941321657dSArtem Bityutskiy * not exist. 7951321657dSArtem Bityutskiy */ 7961321657dSArtem Bityutskiy if (c->jheads && 7971321657dSArtem Bityutskiy lp->lnum == c->jheads[i].wbuf.lnum) { 798be9e62a7SArtem Bityutskiy printk(KERN_CONT ", jhead %s", 799be9e62a7SArtem Bityutskiy dbg_jhead(i)); 800be9e62a7SArtem Bityutskiy head = 1; 801be9e62a7SArtem Bityutskiy } 802be9e62a7SArtem Bityutskiy } 803be9e62a7SArtem Bityutskiy if (!head) 804be9e62a7SArtem Bityutskiy printk(KERN_CONT ", bud of jhead %s", 805be9e62a7SArtem Bityutskiy dbg_jhead(bud->jhead)); 806be9e62a7SArtem Bityutskiy } 807be9e62a7SArtem Bityutskiy } 808be9e62a7SArtem Bityutskiy if (lp->lnum == c->gc_lnum) 809be9e62a7SArtem Bityutskiy printk(KERN_CONT ", GC LEB"); 810be9e62a7SArtem Bityutskiy printk(KERN_CONT ")\n"); 8111e51764aSArtem Bityutskiy } 8121e51764aSArtem Bityutskiy 8131e51764aSArtem Bityutskiy void dbg_dump_lprops(struct ubifs_info *c) 8141e51764aSArtem Bityutskiy { 8151e51764aSArtem Bityutskiy int lnum, err; 8161e51764aSArtem Bityutskiy struct ubifs_lprops lp; 8171e51764aSArtem Bityutskiy struct ubifs_lp_stats lst; 8181e51764aSArtem Bityutskiy 8192ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) start dumping LEB properties\n", 8202ba5f7aeSArtem Bityutskiy current->pid); 8211e51764aSArtem Bityutskiy ubifs_get_lp_stats(c, &lst); 8221e51764aSArtem Bityutskiy dbg_dump_lstats(&lst); 8231e51764aSArtem Bityutskiy 8241e51764aSArtem Bityutskiy for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) { 8251e51764aSArtem Bityutskiy err = ubifs_read_one_lp(c, lnum, &lp); 8261e51764aSArtem Bityutskiy if (err) 8271e51764aSArtem Bityutskiy ubifs_err("cannot read lprops for LEB %d", lnum); 8281e51764aSArtem Bityutskiy 8291e51764aSArtem Bityutskiy dbg_dump_lprop(c, &lp); 8301e51764aSArtem Bityutskiy } 8312ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) finish dumping LEB properties\n", 8322ba5f7aeSArtem Bityutskiy current->pid); 8331e51764aSArtem Bityutskiy } 8341e51764aSArtem Bityutskiy 83573944a6dSAdrian Hunter void dbg_dump_lpt_info(struct ubifs_info *c) 83673944a6dSAdrian Hunter { 83773944a6dSAdrian Hunter int i; 83873944a6dSAdrian Hunter 83973944a6dSAdrian Hunter spin_lock(&dbg_lock); 8402ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) dumping LPT information\n", current->pid); 84173944a6dSAdrian Hunter printk(KERN_DEBUG "\tlpt_sz: %lld\n", c->lpt_sz); 84273944a6dSAdrian Hunter printk(KERN_DEBUG "\tpnode_sz: %d\n", c->pnode_sz); 84373944a6dSAdrian Hunter printk(KERN_DEBUG "\tnnode_sz: %d\n", c->nnode_sz); 84473944a6dSAdrian Hunter printk(KERN_DEBUG "\tltab_sz: %d\n", c->ltab_sz); 84573944a6dSAdrian Hunter printk(KERN_DEBUG "\tlsave_sz: %d\n", c->lsave_sz); 84673944a6dSAdrian Hunter printk(KERN_DEBUG "\tbig_lpt: %d\n", c->big_lpt); 84773944a6dSAdrian Hunter printk(KERN_DEBUG "\tlpt_hght: %d\n", c->lpt_hght); 84873944a6dSAdrian Hunter printk(KERN_DEBUG "\tpnode_cnt: %d\n", c->pnode_cnt); 84973944a6dSAdrian Hunter printk(KERN_DEBUG "\tnnode_cnt: %d\n", c->nnode_cnt); 85073944a6dSAdrian Hunter printk(KERN_DEBUG "\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt); 85173944a6dSAdrian Hunter printk(KERN_DEBUG "\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt); 85273944a6dSAdrian Hunter printk(KERN_DEBUG "\tlsave_cnt: %d\n", c->lsave_cnt); 85373944a6dSAdrian Hunter printk(KERN_DEBUG "\tspace_bits: %d\n", c->space_bits); 85473944a6dSAdrian Hunter printk(KERN_DEBUG "\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits); 85573944a6dSAdrian Hunter printk(KERN_DEBUG "\tlpt_offs_bits: %d\n", c->lpt_offs_bits); 85673944a6dSAdrian Hunter printk(KERN_DEBUG "\tlpt_spc_bits: %d\n", c->lpt_spc_bits); 85773944a6dSAdrian Hunter printk(KERN_DEBUG "\tpcnt_bits: %d\n", c->pcnt_bits); 85873944a6dSAdrian Hunter printk(KERN_DEBUG "\tlnum_bits: %d\n", c->lnum_bits); 85973944a6dSAdrian Hunter printk(KERN_DEBUG "\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs); 86073944a6dSAdrian Hunter printk(KERN_DEBUG "\tLPT head is at %d:%d\n", 86173944a6dSAdrian Hunter c->nhead_lnum, c->nhead_offs); 862f92b9826SArtem Bityutskiy printk(KERN_DEBUG "\tLPT ltab is at %d:%d\n", 863f92b9826SArtem Bityutskiy c->ltab_lnum, c->ltab_offs); 86473944a6dSAdrian Hunter if (c->big_lpt) 86573944a6dSAdrian Hunter printk(KERN_DEBUG "\tLPT lsave is at %d:%d\n", 86673944a6dSAdrian Hunter c->lsave_lnum, c->lsave_offs); 86773944a6dSAdrian Hunter for (i = 0; i < c->lpt_lebs; i++) 86873944a6dSAdrian Hunter printk(KERN_DEBUG "\tLPT LEB %d free %d dirty %d tgc %d " 86973944a6dSAdrian Hunter "cmt %d\n", i + c->lpt_first, c->ltab[i].free, 87073944a6dSAdrian Hunter c->ltab[i].dirty, c->ltab[i].tgc, c->ltab[i].cmt); 87173944a6dSAdrian Hunter spin_unlock(&dbg_lock); 87273944a6dSAdrian Hunter } 87373944a6dSAdrian Hunter 8741e51764aSArtem Bityutskiy void dbg_dump_leb(const struct ubifs_info *c, int lnum) 8751e51764aSArtem Bityutskiy { 8761e51764aSArtem Bityutskiy struct ubifs_scan_leb *sleb; 8771e51764aSArtem Bityutskiy struct ubifs_scan_node *snod; 87873d9aec3SArtem Bityutskiy void *buf; 8791e51764aSArtem Bityutskiy 8802b1844a8SArtem Bityutskiy if (dbg_is_tst_rcvry(c)) 8811e51764aSArtem Bityutskiy return; 8821e51764aSArtem Bityutskiy 8832ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n", 8842ba5f7aeSArtem Bityutskiy current->pid, lnum); 88573d9aec3SArtem Bityutskiy 886fc5e58c0SArtem Bityutskiy buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL); 88773d9aec3SArtem Bityutskiy if (!buf) { 88873d9aec3SArtem Bityutskiy ubifs_err("cannot allocate memory for dumping LEB %d", lnum); 88973d9aec3SArtem Bityutskiy return; 89073d9aec3SArtem Bityutskiy } 89173d9aec3SArtem Bityutskiy 89273d9aec3SArtem Bityutskiy sleb = ubifs_scan(c, lnum, 0, buf, 0); 8931e51764aSArtem Bityutskiy if (IS_ERR(sleb)) { 8941e51764aSArtem Bityutskiy ubifs_err("scan error %d", (int)PTR_ERR(sleb)); 89573d9aec3SArtem Bityutskiy goto out; 8961e51764aSArtem Bityutskiy } 8971e51764aSArtem Bityutskiy 8981e51764aSArtem Bityutskiy printk(KERN_DEBUG "LEB %d has %d nodes ending at %d\n", lnum, 8991e51764aSArtem Bityutskiy sleb->nodes_cnt, sleb->endpt); 9001e51764aSArtem Bityutskiy 9011e51764aSArtem Bityutskiy list_for_each_entry(snod, &sleb->nodes, list) { 9021e51764aSArtem Bityutskiy cond_resched(); 9031e51764aSArtem Bityutskiy printk(KERN_DEBUG "Dumping node at LEB %d:%d len %d\n", lnum, 9041e51764aSArtem Bityutskiy snod->offs, snod->len); 9051e51764aSArtem Bityutskiy dbg_dump_node(c, snod->node); 9061e51764aSArtem Bityutskiy } 9071e51764aSArtem Bityutskiy 9082ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n", 9092ba5f7aeSArtem Bityutskiy current->pid, lnum); 9101e51764aSArtem Bityutskiy ubifs_scan_destroy(sleb); 91173d9aec3SArtem Bityutskiy 91273d9aec3SArtem Bityutskiy out: 91373d9aec3SArtem Bityutskiy vfree(buf); 9141e51764aSArtem Bityutskiy return; 9151e51764aSArtem Bityutskiy } 9161e51764aSArtem Bityutskiy 9171e51764aSArtem Bityutskiy void dbg_dump_znode(const struct ubifs_info *c, 9181e51764aSArtem Bityutskiy const struct ubifs_znode *znode) 9191e51764aSArtem Bityutskiy { 9201e51764aSArtem Bityutskiy int n; 9211e51764aSArtem Bityutskiy const struct ubifs_zbranch *zbr; 9221e51764aSArtem Bityutskiy 9231e51764aSArtem Bityutskiy spin_lock(&dbg_lock); 9241e51764aSArtem Bityutskiy if (znode->parent) 9251e51764aSArtem Bityutskiy zbr = &znode->parent->zbranch[znode->iip]; 9261e51764aSArtem Bityutskiy else 9271e51764aSArtem Bityutskiy zbr = &c->zroot; 9281e51764aSArtem Bityutskiy 9291e51764aSArtem Bityutskiy printk(KERN_DEBUG "znode %p, LEB %d:%d len %d parent %p iip %d level %d" 9301e51764aSArtem Bityutskiy " child_cnt %d flags %lx\n", znode, zbr->lnum, zbr->offs, 9311e51764aSArtem Bityutskiy zbr->len, znode->parent, znode->iip, znode->level, 9321e51764aSArtem Bityutskiy znode->child_cnt, znode->flags); 9331e51764aSArtem Bityutskiy 9341e51764aSArtem Bityutskiy if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) { 9351e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 9361e51764aSArtem Bityutskiy return; 9371e51764aSArtem Bityutskiy } 9381e51764aSArtem Bityutskiy 9391e51764aSArtem Bityutskiy printk(KERN_DEBUG "zbranches:\n"); 9401e51764aSArtem Bityutskiy for (n = 0; n < znode->child_cnt; n++) { 9411e51764aSArtem Bityutskiy zbr = &znode->zbranch[n]; 9421e51764aSArtem Bityutskiy if (znode->level > 0) 9431e51764aSArtem Bityutskiy printk(KERN_DEBUG "\t%d: znode %p LEB %d:%d len %d key " 9441e51764aSArtem Bityutskiy "%s\n", n, zbr->znode, zbr->lnum, 9451e51764aSArtem Bityutskiy zbr->offs, zbr->len, 9461e51764aSArtem Bityutskiy DBGKEY(&zbr->key)); 9471e51764aSArtem Bityutskiy else 9481e51764aSArtem Bityutskiy printk(KERN_DEBUG "\t%d: LNC %p LEB %d:%d len %d key " 9491e51764aSArtem Bityutskiy "%s\n", n, zbr->znode, zbr->lnum, 9501e51764aSArtem Bityutskiy zbr->offs, zbr->len, 9511e51764aSArtem Bityutskiy DBGKEY(&zbr->key)); 9521e51764aSArtem Bityutskiy } 9531e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 9541e51764aSArtem Bityutskiy } 9551e51764aSArtem Bityutskiy 9561e51764aSArtem Bityutskiy void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat) 9571e51764aSArtem Bityutskiy { 9581e51764aSArtem Bityutskiy int i; 9591e51764aSArtem Bityutskiy 9602ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) start dumping heap cat %d (%d elements)\n", 9611de94159SArtem Bityutskiy current->pid, cat, heap->cnt); 9621e51764aSArtem Bityutskiy for (i = 0; i < heap->cnt; i++) { 9631e51764aSArtem Bityutskiy struct ubifs_lprops *lprops = heap->arr[i]; 9641e51764aSArtem Bityutskiy 9651e51764aSArtem Bityutskiy printk(KERN_DEBUG "\t%d. LEB %d hpos %d free %d dirty %d " 9661e51764aSArtem Bityutskiy "flags %d\n", i, lprops->lnum, lprops->hpos, 9671e51764aSArtem Bityutskiy lprops->free, lprops->dirty, lprops->flags); 9681e51764aSArtem Bityutskiy } 9692ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) finish dumping heap\n", current->pid); 9701e51764aSArtem Bityutskiy } 9711e51764aSArtem Bityutskiy 9721e51764aSArtem Bityutskiy void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode, 9731e51764aSArtem Bityutskiy struct ubifs_nnode *parent, int iip) 9741e51764aSArtem Bityutskiy { 9751e51764aSArtem Bityutskiy int i; 9761e51764aSArtem Bityutskiy 9772ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) dumping pnode:\n", current->pid); 9781e51764aSArtem Bityutskiy printk(KERN_DEBUG "\taddress %zx parent %zx cnext %zx\n", 9791e51764aSArtem Bityutskiy (size_t)pnode, (size_t)parent, (size_t)pnode->cnext); 9801e51764aSArtem Bityutskiy printk(KERN_DEBUG "\tflags %lu iip %d level %d num %d\n", 9811e51764aSArtem Bityutskiy pnode->flags, iip, pnode->level, pnode->num); 9821e51764aSArtem Bityutskiy for (i = 0; i < UBIFS_LPT_FANOUT; i++) { 9831e51764aSArtem Bityutskiy struct ubifs_lprops *lp = &pnode->lprops[i]; 9841e51764aSArtem Bityutskiy 9851e51764aSArtem Bityutskiy printk(KERN_DEBUG "\t%d: free %d dirty %d flags %d lnum %d\n", 9861e51764aSArtem Bityutskiy i, lp->free, lp->dirty, lp->flags, lp->lnum); 9871e51764aSArtem Bityutskiy } 9881e51764aSArtem Bityutskiy } 9891e51764aSArtem Bityutskiy 9901e51764aSArtem Bityutskiy void dbg_dump_tnc(struct ubifs_info *c) 9911e51764aSArtem Bityutskiy { 9921e51764aSArtem Bityutskiy struct ubifs_znode *znode; 9931e51764aSArtem Bityutskiy int level; 9941e51764aSArtem Bityutskiy 9951e51764aSArtem Bityutskiy printk(KERN_DEBUG "\n"); 9962ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) start dumping TNC tree\n", current->pid); 9971e51764aSArtem Bityutskiy znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL); 9981e51764aSArtem Bityutskiy level = znode->level; 9991e51764aSArtem Bityutskiy printk(KERN_DEBUG "== Level %d ==\n", level); 10001e51764aSArtem Bityutskiy while (znode) { 10011e51764aSArtem Bityutskiy if (level != znode->level) { 10021e51764aSArtem Bityutskiy level = znode->level; 10031e51764aSArtem Bityutskiy printk(KERN_DEBUG "== Level %d ==\n", level); 10041e51764aSArtem Bityutskiy } 10051e51764aSArtem Bityutskiy dbg_dump_znode(c, znode); 10061e51764aSArtem Bityutskiy znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode); 10071e51764aSArtem Bityutskiy } 10082ba5f7aeSArtem Bityutskiy printk(KERN_DEBUG "(pid %d) finish dumping TNC tree\n", current->pid); 10091e51764aSArtem Bityutskiy } 10101e51764aSArtem Bityutskiy 10111e51764aSArtem Bityutskiy static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode, 10121e51764aSArtem Bityutskiy void *priv) 10131e51764aSArtem Bityutskiy { 10141e51764aSArtem Bityutskiy dbg_dump_znode(c, znode); 10151e51764aSArtem Bityutskiy return 0; 10161e51764aSArtem Bityutskiy } 10171e51764aSArtem Bityutskiy 10181e51764aSArtem Bityutskiy /** 10191e51764aSArtem Bityutskiy * dbg_dump_index - dump the on-flash index. 10201e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 10211e51764aSArtem Bityutskiy * 10221e51764aSArtem Bityutskiy * This function dumps whole UBIFS indexing B-tree, unlike 'dbg_dump_tnc()' 10231e51764aSArtem Bityutskiy * which dumps only in-memory znodes and does not read znodes which from flash. 10241e51764aSArtem Bityutskiy */ 10251e51764aSArtem Bityutskiy void dbg_dump_index(struct ubifs_info *c) 10261e51764aSArtem Bityutskiy { 10271e51764aSArtem Bityutskiy dbg_walk_index(c, NULL, dump_znode, NULL); 10281e51764aSArtem Bityutskiy } 10291e51764aSArtem Bityutskiy 10301e51764aSArtem Bityutskiy /** 103184abf972SArtem Bityutskiy * dbg_save_space_info - save information about flash space. 103284abf972SArtem Bityutskiy * @c: UBIFS file-system description object 103384abf972SArtem Bityutskiy * 103484abf972SArtem Bityutskiy * This function saves information about UBIFS free space, dirty space, etc, in 103584abf972SArtem Bityutskiy * order to check it later. 103684abf972SArtem Bityutskiy */ 103784abf972SArtem Bityutskiy void dbg_save_space_info(struct ubifs_info *c) 103884abf972SArtem Bityutskiy { 103984abf972SArtem Bityutskiy struct ubifs_debug_info *d = c->dbg; 10407da6443aSArtem Bityutskiy int freeable_cnt; 104184abf972SArtem Bityutskiy 104284abf972SArtem Bityutskiy spin_lock(&c->space_lock); 10437da6443aSArtem Bityutskiy memcpy(&d->saved_lst, &c->lst, sizeof(struct ubifs_lp_stats)); 1044f1bd66afSArtem Bityutskiy memcpy(&d->saved_bi, &c->bi, sizeof(struct ubifs_budg_info)); 1045f1bd66afSArtem Bityutskiy d->saved_idx_gc_cnt = c->idx_gc_cnt; 10467da6443aSArtem Bityutskiy 10477da6443aSArtem Bityutskiy /* 10487da6443aSArtem Bityutskiy * We use a dirty hack here and zero out @c->freeable_cnt, because it 10497da6443aSArtem Bityutskiy * affects the free space calculations, and UBIFS might not know about 10507da6443aSArtem Bityutskiy * all freeable eraseblocks. Indeed, we know about freeable eraseblocks 10517da6443aSArtem Bityutskiy * only when we read their lprops, and we do this only lazily, upon the 10527da6443aSArtem Bityutskiy * need. So at any given point of time @c->freeable_cnt might be not 10537da6443aSArtem Bityutskiy * exactly accurate. 10547da6443aSArtem Bityutskiy * 10557da6443aSArtem Bityutskiy * Just one example about the issue we hit when we did not zero 10567da6443aSArtem Bityutskiy * @c->freeable_cnt. 10577da6443aSArtem Bityutskiy * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the 10587da6443aSArtem Bityutskiy * amount of free space in @d->saved_free 10597da6443aSArtem Bityutskiy * 2. We re-mount R/W, which makes UBIFS to read the "lsave" 10607da6443aSArtem Bityutskiy * information from flash, where we cache LEBs from various 10617da6443aSArtem Bityutskiy * categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()' 10627da6443aSArtem Bityutskiy * -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()' 10637da6443aSArtem Bityutskiy * -> 'ubifs_get_pnode()' -> 'update_cats()' 10647da6443aSArtem Bityutskiy * -> 'ubifs_add_to_cat()'). 10657da6443aSArtem Bityutskiy * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt 10667da6443aSArtem Bityutskiy * becomes %1. 10677da6443aSArtem Bityutskiy * 4. We calculate the amount of free space when the re-mount is 10687da6443aSArtem Bityutskiy * finished in 'dbg_check_space_info()' and it does not match 10697da6443aSArtem Bityutskiy * @d->saved_free. 10707da6443aSArtem Bityutskiy */ 10717da6443aSArtem Bityutskiy freeable_cnt = c->freeable_cnt; 10727da6443aSArtem Bityutskiy c->freeable_cnt = 0; 107384abf972SArtem Bityutskiy d->saved_free = ubifs_get_free_space_nolock(c); 10747da6443aSArtem Bityutskiy c->freeable_cnt = freeable_cnt; 107584abf972SArtem Bityutskiy spin_unlock(&c->space_lock); 107684abf972SArtem Bityutskiy } 107784abf972SArtem Bityutskiy 107884abf972SArtem Bityutskiy /** 107984abf972SArtem Bityutskiy * dbg_check_space_info - check flash space information. 108084abf972SArtem Bityutskiy * @c: UBIFS file-system description object 108184abf972SArtem Bityutskiy * 108284abf972SArtem Bityutskiy * This function compares current flash space information with the information 108384abf972SArtem Bityutskiy * which was saved when the 'dbg_save_space_info()' function was called. 108484abf972SArtem Bityutskiy * Returns zero if the information has not changed, and %-EINVAL it it has 108584abf972SArtem Bityutskiy * changed. 108684abf972SArtem Bityutskiy */ 108784abf972SArtem Bityutskiy int dbg_check_space_info(struct ubifs_info *c) 108884abf972SArtem Bityutskiy { 108984abf972SArtem Bityutskiy struct ubifs_debug_info *d = c->dbg; 109084abf972SArtem Bityutskiy struct ubifs_lp_stats lst; 10917da6443aSArtem Bityutskiy long long free; 10927da6443aSArtem Bityutskiy int freeable_cnt; 109384abf972SArtem Bityutskiy 109484abf972SArtem Bityutskiy spin_lock(&c->space_lock); 10957da6443aSArtem Bityutskiy freeable_cnt = c->freeable_cnt; 10967da6443aSArtem Bityutskiy c->freeable_cnt = 0; 10977da6443aSArtem Bityutskiy free = ubifs_get_free_space_nolock(c); 10987da6443aSArtem Bityutskiy c->freeable_cnt = freeable_cnt; 109984abf972SArtem Bityutskiy spin_unlock(&c->space_lock); 110084abf972SArtem Bityutskiy 110184abf972SArtem Bityutskiy if (free != d->saved_free) { 110284abf972SArtem Bityutskiy ubifs_err("free space changed from %lld to %lld", 110384abf972SArtem Bityutskiy d->saved_free, free); 110484abf972SArtem Bityutskiy goto out; 110584abf972SArtem Bityutskiy } 110684abf972SArtem Bityutskiy 110784abf972SArtem Bityutskiy return 0; 110884abf972SArtem Bityutskiy 110984abf972SArtem Bityutskiy out: 111084abf972SArtem Bityutskiy ubifs_msg("saved lprops statistics dump"); 111184abf972SArtem Bityutskiy dbg_dump_lstats(&d->saved_lst); 1112f1bd66afSArtem Bityutskiy ubifs_msg("saved budgeting info dump"); 1113f1bd66afSArtem Bityutskiy dbg_dump_budg(c, &d->saved_bi); 1114f1bd66afSArtem Bityutskiy ubifs_msg("saved idx_gc_cnt %d", d->saved_idx_gc_cnt); 111584abf972SArtem Bityutskiy ubifs_msg("current lprops statistics dump"); 1116f1bd66afSArtem Bityutskiy ubifs_get_lp_stats(c, &lst); 1117e055f7e8SArtem Bityutskiy dbg_dump_lstats(&lst); 1118f1bd66afSArtem Bityutskiy ubifs_msg("current budgeting info dump"); 1119f1bd66afSArtem Bityutskiy dbg_dump_budg(c, &c->bi); 112084abf972SArtem Bityutskiy dump_stack(); 112184abf972SArtem Bityutskiy return -EINVAL; 112284abf972SArtem Bityutskiy } 112384abf972SArtem Bityutskiy 112484abf972SArtem Bityutskiy /** 11251e51764aSArtem Bityutskiy * dbg_check_synced_i_size - check synchronized inode size. 1126d808efb4SArtem Bityutskiy * @c: UBIFS file-system description object 11271e51764aSArtem Bityutskiy * @inode: inode to check 11281e51764aSArtem Bityutskiy * 11291e51764aSArtem Bityutskiy * If inode is clean, synchronized inode size has to be equivalent to current 11301e51764aSArtem Bityutskiy * inode size. This function has to be called only for locked inodes (@i_mutex 11311e51764aSArtem Bityutskiy * has to be locked). Returns %0 if synchronized inode size if correct, and 11321e51764aSArtem Bityutskiy * %-EINVAL if not. 11331e51764aSArtem Bityutskiy */ 1134d808efb4SArtem Bityutskiy int dbg_check_synced_i_size(const struct ubifs_info *c, struct inode *inode) 11351e51764aSArtem Bityutskiy { 11361e51764aSArtem Bityutskiy int err = 0; 11371e51764aSArtem Bityutskiy struct ubifs_inode *ui = ubifs_inode(inode); 11381e51764aSArtem Bityutskiy 11392b1844a8SArtem Bityutskiy if (!dbg_is_chk_gen(c)) 11401e51764aSArtem Bityutskiy return 0; 11411e51764aSArtem Bityutskiy if (!S_ISREG(inode->i_mode)) 11421e51764aSArtem Bityutskiy return 0; 11431e51764aSArtem Bityutskiy 11441e51764aSArtem Bityutskiy mutex_lock(&ui->ui_mutex); 11451e51764aSArtem Bityutskiy spin_lock(&ui->ui_lock); 11461e51764aSArtem Bityutskiy if (ui->ui_size != ui->synced_i_size && !ui->dirty) { 11471e51764aSArtem Bityutskiy ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode " 11481e51764aSArtem Bityutskiy "is clean", ui->ui_size, ui->synced_i_size); 11491e51764aSArtem Bityutskiy ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino, 11501e51764aSArtem Bityutskiy inode->i_mode, i_size_read(inode)); 11511e51764aSArtem Bityutskiy dbg_dump_stack(); 11521e51764aSArtem Bityutskiy err = -EINVAL; 11531e51764aSArtem Bityutskiy } 11541e51764aSArtem Bityutskiy spin_unlock(&ui->ui_lock); 11551e51764aSArtem Bityutskiy mutex_unlock(&ui->ui_mutex); 11561e51764aSArtem Bityutskiy return err; 11571e51764aSArtem Bityutskiy } 11581e51764aSArtem Bityutskiy 11591e51764aSArtem Bityutskiy /* 11601e51764aSArtem Bityutskiy * dbg_check_dir - check directory inode size and link count. 11611e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 11621e51764aSArtem Bityutskiy * @dir: the directory to calculate size for 11631e51764aSArtem Bityutskiy * @size: the result is returned here 11641e51764aSArtem Bityutskiy * 11651e51764aSArtem Bityutskiy * This function makes sure that directory size and link count are correct. 11661e51764aSArtem Bityutskiy * Returns zero in case of success and a negative error code in case of 11671e51764aSArtem Bityutskiy * failure. 11681e51764aSArtem Bityutskiy * 11691e51764aSArtem Bityutskiy * Note, it is good idea to make sure the @dir->i_mutex is locked before 11701e51764aSArtem Bityutskiy * calling this function. 11711e51764aSArtem Bityutskiy */ 11721b51e983SArtem Bityutskiy int dbg_check_dir(struct ubifs_info *c, const struct inode *dir) 11731e51764aSArtem Bityutskiy { 11741e51764aSArtem Bityutskiy unsigned int nlink = 2; 11751e51764aSArtem Bityutskiy union ubifs_key key; 11761e51764aSArtem Bityutskiy struct ubifs_dent_node *dent, *pdent = NULL; 11771e51764aSArtem Bityutskiy struct qstr nm = { .name = NULL }; 11781e51764aSArtem Bityutskiy loff_t size = UBIFS_INO_NODE_SZ; 11791e51764aSArtem Bityutskiy 11802b1844a8SArtem Bityutskiy if (!dbg_is_chk_gen(c)) 11811e51764aSArtem Bityutskiy return 0; 11821e51764aSArtem Bityutskiy 11831e51764aSArtem Bityutskiy if (!S_ISDIR(dir->i_mode)) 11841e51764aSArtem Bityutskiy return 0; 11851e51764aSArtem Bityutskiy 11861e51764aSArtem Bityutskiy lowest_dent_key(c, &key, dir->i_ino); 11871e51764aSArtem Bityutskiy while (1) { 11881e51764aSArtem Bityutskiy int err; 11891e51764aSArtem Bityutskiy 11901e51764aSArtem Bityutskiy dent = ubifs_tnc_next_ent(c, &key, &nm); 11911e51764aSArtem Bityutskiy if (IS_ERR(dent)) { 11921e51764aSArtem Bityutskiy err = PTR_ERR(dent); 11931e51764aSArtem Bityutskiy if (err == -ENOENT) 11941e51764aSArtem Bityutskiy break; 11951e51764aSArtem Bityutskiy return err; 11961e51764aSArtem Bityutskiy } 11971e51764aSArtem Bityutskiy 11981e51764aSArtem Bityutskiy nm.name = dent->name; 11991e51764aSArtem Bityutskiy nm.len = le16_to_cpu(dent->nlen); 12001e51764aSArtem Bityutskiy size += CALC_DENT_SIZE(nm.len); 12011e51764aSArtem Bityutskiy if (dent->type == UBIFS_ITYPE_DIR) 12021e51764aSArtem Bityutskiy nlink += 1; 12031e51764aSArtem Bityutskiy kfree(pdent); 12041e51764aSArtem Bityutskiy pdent = dent; 12051e51764aSArtem Bityutskiy key_read(c, &dent->key, &key); 12061e51764aSArtem Bityutskiy } 12071e51764aSArtem Bityutskiy kfree(pdent); 12081e51764aSArtem Bityutskiy 12091e51764aSArtem Bityutskiy if (i_size_read(dir) != size) { 12101e51764aSArtem Bityutskiy ubifs_err("directory inode %lu has size %llu, " 12111e51764aSArtem Bityutskiy "but calculated size is %llu", dir->i_ino, 12121e51764aSArtem Bityutskiy (unsigned long long)i_size_read(dir), 12131e51764aSArtem Bityutskiy (unsigned long long)size); 12144315fb40SArtem Bityutskiy dbg_dump_inode(c, dir); 12151e51764aSArtem Bityutskiy dump_stack(); 12161e51764aSArtem Bityutskiy return -EINVAL; 12171e51764aSArtem Bityutskiy } 12181e51764aSArtem Bityutskiy if (dir->i_nlink != nlink) { 12191e51764aSArtem Bityutskiy ubifs_err("directory inode %lu has nlink %u, but calculated " 12201e51764aSArtem Bityutskiy "nlink is %u", dir->i_ino, dir->i_nlink, nlink); 12214315fb40SArtem Bityutskiy dbg_dump_inode(c, dir); 12221e51764aSArtem Bityutskiy dump_stack(); 12231e51764aSArtem Bityutskiy return -EINVAL; 12241e51764aSArtem Bityutskiy } 12251e51764aSArtem Bityutskiy 12261e51764aSArtem Bityutskiy return 0; 12271e51764aSArtem Bityutskiy } 12281e51764aSArtem Bityutskiy 12291e51764aSArtem Bityutskiy /** 12301e51764aSArtem Bityutskiy * dbg_check_key_order - make sure that colliding keys are properly ordered. 12311e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 12321e51764aSArtem Bityutskiy * @zbr1: first zbranch 12331e51764aSArtem Bityutskiy * @zbr2: following zbranch 12341e51764aSArtem Bityutskiy * 12351e51764aSArtem Bityutskiy * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of 12361e51764aSArtem Bityutskiy * names of the direntries/xentries which are referred by the keys. This 12371e51764aSArtem Bityutskiy * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes 12381e51764aSArtem Bityutskiy * sure the name of direntry/xentry referred by @zbr1 is less than 12391e51764aSArtem Bityutskiy * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not, 12401e51764aSArtem Bityutskiy * and a negative error code in case of failure. 12411e51764aSArtem Bityutskiy */ 12421e51764aSArtem Bityutskiy static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1, 12431e51764aSArtem Bityutskiy struct ubifs_zbranch *zbr2) 12441e51764aSArtem Bityutskiy { 12451e51764aSArtem Bityutskiy int err, nlen1, nlen2, cmp; 12461e51764aSArtem Bityutskiy struct ubifs_dent_node *dent1, *dent2; 12471e51764aSArtem Bityutskiy union ubifs_key key; 12481e51764aSArtem Bityutskiy 12491e51764aSArtem Bityutskiy ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key)); 12501e51764aSArtem Bityutskiy dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS); 12511e51764aSArtem Bityutskiy if (!dent1) 12521e51764aSArtem Bityutskiy return -ENOMEM; 12531e51764aSArtem Bityutskiy dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS); 12541e51764aSArtem Bityutskiy if (!dent2) { 12551e51764aSArtem Bityutskiy err = -ENOMEM; 12561e51764aSArtem Bityutskiy goto out_free; 12571e51764aSArtem Bityutskiy } 12581e51764aSArtem Bityutskiy 12591e51764aSArtem Bityutskiy err = ubifs_tnc_read_node(c, zbr1, dent1); 12601e51764aSArtem Bityutskiy if (err) 12611e51764aSArtem Bityutskiy goto out_free; 12621e51764aSArtem Bityutskiy err = ubifs_validate_entry(c, dent1); 12631e51764aSArtem Bityutskiy if (err) 12641e51764aSArtem Bityutskiy goto out_free; 12651e51764aSArtem Bityutskiy 12661e51764aSArtem Bityutskiy err = ubifs_tnc_read_node(c, zbr2, dent2); 12671e51764aSArtem Bityutskiy if (err) 12681e51764aSArtem Bityutskiy goto out_free; 12691e51764aSArtem Bityutskiy err = ubifs_validate_entry(c, dent2); 12701e51764aSArtem Bityutskiy if (err) 12711e51764aSArtem Bityutskiy goto out_free; 12721e51764aSArtem Bityutskiy 12731e51764aSArtem Bityutskiy /* Make sure node keys are the same as in zbranch */ 12741e51764aSArtem Bityutskiy err = 1; 12751e51764aSArtem Bityutskiy key_read(c, &dent1->key, &key); 12761e51764aSArtem Bityutskiy if (keys_cmp(c, &zbr1->key, &key)) { 12775d38b3acSArtem Bityutskiy dbg_err("1st entry at %d:%d has key %s", zbr1->lnum, 12781e51764aSArtem Bityutskiy zbr1->offs, DBGKEY(&key)); 12795d38b3acSArtem Bityutskiy dbg_err("but it should have key %s according to tnc", 12802ba5f7aeSArtem Bityutskiy DBGKEY(&zbr1->key)); 12812ba5f7aeSArtem Bityutskiy dbg_dump_node(c, dent1); 12821e51764aSArtem Bityutskiy goto out_free; 12831e51764aSArtem Bityutskiy } 12841e51764aSArtem Bityutskiy 12851e51764aSArtem Bityutskiy key_read(c, &dent2->key, &key); 12861e51764aSArtem Bityutskiy if (keys_cmp(c, &zbr2->key, &key)) { 12875d38b3acSArtem Bityutskiy dbg_err("2nd entry at %d:%d has key %s", zbr1->lnum, 12881e51764aSArtem Bityutskiy zbr1->offs, DBGKEY(&key)); 12895d38b3acSArtem Bityutskiy dbg_err("but it should have key %s according to tnc", 12902ba5f7aeSArtem Bityutskiy DBGKEY(&zbr2->key)); 12912ba5f7aeSArtem Bityutskiy dbg_dump_node(c, dent2); 12921e51764aSArtem Bityutskiy goto out_free; 12931e51764aSArtem Bityutskiy } 12941e51764aSArtem Bityutskiy 12951e51764aSArtem Bityutskiy nlen1 = le16_to_cpu(dent1->nlen); 12961e51764aSArtem Bityutskiy nlen2 = le16_to_cpu(dent2->nlen); 12971e51764aSArtem Bityutskiy 12981e51764aSArtem Bityutskiy cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2)); 12991e51764aSArtem Bityutskiy if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) { 13001e51764aSArtem Bityutskiy err = 0; 13011e51764aSArtem Bityutskiy goto out_free; 13021e51764aSArtem Bityutskiy } 13031e51764aSArtem Bityutskiy if (cmp == 0 && nlen1 == nlen2) 13045d38b3acSArtem Bityutskiy dbg_err("2 xent/dent nodes with the same name"); 13051e51764aSArtem Bityutskiy else 13065d38b3acSArtem Bityutskiy dbg_err("bad order of colliding key %s", 13071e51764aSArtem Bityutskiy DBGKEY(&key)); 13081e51764aSArtem Bityutskiy 1309552ff317SArtem Bityutskiy ubifs_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs); 13101e51764aSArtem Bityutskiy dbg_dump_node(c, dent1); 1311552ff317SArtem Bityutskiy ubifs_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs); 13121e51764aSArtem Bityutskiy dbg_dump_node(c, dent2); 13131e51764aSArtem Bityutskiy 13141e51764aSArtem Bityutskiy out_free: 13151e51764aSArtem Bityutskiy kfree(dent2); 13161e51764aSArtem Bityutskiy kfree(dent1); 13171e51764aSArtem Bityutskiy return err; 13181e51764aSArtem Bityutskiy } 13191e51764aSArtem Bityutskiy 13201e51764aSArtem Bityutskiy /** 13211e51764aSArtem Bityutskiy * dbg_check_znode - check if znode is all right. 13221e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 13231e51764aSArtem Bityutskiy * @zbr: zbranch which points to this znode 13241e51764aSArtem Bityutskiy * 13251e51764aSArtem Bityutskiy * This function makes sure that znode referred to by @zbr is all right. 13261e51764aSArtem Bityutskiy * Returns zero if it is, and %-EINVAL if it is not. 13271e51764aSArtem Bityutskiy */ 13281e51764aSArtem Bityutskiy static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr) 13291e51764aSArtem Bityutskiy { 13301e51764aSArtem Bityutskiy struct ubifs_znode *znode = zbr->znode; 13311e51764aSArtem Bityutskiy struct ubifs_znode *zp = znode->parent; 13321e51764aSArtem Bityutskiy int n, err, cmp; 13331e51764aSArtem Bityutskiy 13341e51764aSArtem Bityutskiy if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) { 13351e51764aSArtem Bityutskiy err = 1; 13361e51764aSArtem Bityutskiy goto out; 13371e51764aSArtem Bityutskiy } 13381e51764aSArtem Bityutskiy if (znode->level < 0) { 13391e51764aSArtem Bityutskiy err = 2; 13401e51764aSArtem Bityutskiy goto out; 13411e51764aSArtem Bityutskiy } 13421e51764aSArtem Bityutskiy if (znode->iip < 0 || znode->iip >= c->fanout) { 13431e51764aSArtem Bityutskiy err = 3; 13441e51764aSArtem Bityutskiy goto out; 13451e51764aSArtem Bityutskiy } 13461e51764aSArtem Bityutskiy 13471e51764aSArtem Bityutskiy if (zbr->len == 0) 13481e51764aSArtem Bityutskiy /* Only dirty zbranch may have no on-flash nodes */ 13491e51764aSArtem Bityutskiy if (!ubifs_zn_dirty(znode)) { 13501e51764aSArtem Bityutskiy err = 4; 13511e51764aSArtem Bityutskiy goto out; 13521e51764aSArtem Bityutskiy } 13531e51764aSArtem Bityutskiy 13541e51764aSArtem Bityutskiy if (ubifs_zn_dirty(znode)) { 13551e51764aSArtem Bityutskiy /* 13561e51764aSArtem Bityutskiy * If znode is dirty, its parent has to be dirty as well. The 13571e51764aSArtem Bityutskiy * order of the operation is important, so we have to have 13581e51764aSArtem Bityutskiy * memory barriers. 13591e51764aSArtem Bityutskiy */ 13601e51764aSArtem Bityutskiy smp_mb(); 13611e51764aSArtem Bityutskiy if (zp && !ubifs_zn_dirty(zp)) { 13621e51764aSArtem Bityutskiy /* 13631e51764aSArtem Bityutskiy * The dirty flag is atomic and is cleared outside the 13641e51764aSArtem Bityutskiy * TNC mutex, so znode's dirty flag may now have 13651e51764aSArtem Bityutskiy * been cleared. The child is always cleared before the 13661e51764aSArtem Bityutskiy * parent, so we just need to check again. 13671e51764aSArtem Bityutskiy */ 13681e51764aSArtem Bityutskiy smp_mb(); 13691e51764aSArtem Bityutskiy if (ubifs_zn_dirty(znode)) { 13701e51764aSArtem Bityutskiy err = 5; 13711e51764aSArtem Bityutskiy goto out; 13721e51764aSArtem Bityutskiy } 13731e51764aSArtem Bityutskiy } 13741e51764aSArtem Bityutskiy } 13751e51764aSArtem Bityutskiy 13761e51764aSArtem Bityutskiy if (zp) { 13771e51764aSArtem Bityutskiy const union ubifs_key *min, *max; 13781e51764aSArtem Bityutskiy 13791e51764aSArtem Bityutskiy if (znode->level != zp->level - 1) { 13801e51764aSArtem Bityutskiy err = 6; 13811e51764aSArtem Bityutskiy goto out; 13821e51764aSArtem Bityutskiy } 13831e51764aSArtem Bityutskiy 13841e51764aSArtem Bityutskiy /* Make sure the 'parent' pointer in our znode is correct */ 13851e51764aSArtem Bityutskiy err = ubifs_search_zbranch(c, zp, &zbr->key, &n); 13861e51764aSArtem Bityutskiy if (!err) { 13871e51764aSArtem Bityutskiy /* This zbranch does not exist in the parent */ 13881e51764aSArtem Bityutskiy err = 7; 13891e51764aSArtem Bityutskiy goto out; 13901e51764aSArtem Bityutskiy } 13911e51764aSArtem Bityutskiy 13921e51764aSArtem Bityutskiy if (znode->iip >= zp->child_cnt) { 13931e51764aSArtem Bityutskiy err = 8; 13941e51764aSArtem Bityutskiy goto out; 13951e51764aSArtem Bityutskiy } 13961e51764aSArtem Bityutskiy 13971e51764aSArtem Bityutskiy if (znode->iip != n) { 13981e51764aSArtem Bityutskiy /* This may happen only in case of collisions */ 13991e51764aSArtem Bityutskiy if (keys_cmp(c, &zp->zbranch[n].key, 14001e51764aSArtem Bityutskiy &zp->zbranch[znode->iip].key)) { 14011e51764aSArtem Bityutskiy err = 9; 14021e51764aSArtem Bityutskiy goto out; 14031e51764aSArtem Bityutskiy } 14041e51764aSArtem Bityutskiy n = znode->iip; 14051e51764aSArtem Bityutskiy } 14061e51764aSArtem Bityutskiy 14071e51764aSArtem Bityutskiy /* 14081e51764aSArtem Bityutskiy * Make sure that the first key in our znode is greater than or 14091e51764aSArtem Bityutskiy * equal to the key in the pointing zbranch. 14101e51764aSArtem Bityutskiy */ 14111e51764aSArtem Bityutskiy min = &zbr->key; 14121e51764aSArtem Bityutskiy cmp = keys_cmp(c, min, &znode->zbranch[0].key); 14131e51764aSArtem Bityutskiy if (cmp == 1) { 14141e51764aSArtem Bityutskiy err = 10; 14151e51764aSArtem Bityutskiy goto out; 14161e51764aSArtem Bityutskiy } 14171e51764aSArtem Bityutskiy 14181e51764aSArtem Bityutskiy if (n + 1 < zp->child_cnt) { 14191e51764aSArtem Bityutskiy max = &zp->zbranch[n + 1].key; 14201e51764aSArtem Bityutskiy 14211e51764aSArtem Bityutskiy /* 14221e51764aSArtem Bityutskiy * Make sure the last key in our znode is less or 14237d4e9ccbSArtem Bityutskiy * equivalent than the key in the zbranch which goes 14241e51764aSArtem Bityutskiy * after our pointing zbranch. 14251e51764aSArtem Bityutskiy */ 14261e51764aSArtem Bityutskiy cmp = keys_cmp(c, max, 14271e51764aSArtem Bityutskiy &znode->zbranch[znode->child_cnt - 1].key); 14281e51764aSArtem Bityutskiy if (cmp == -1) { 14291e51764aSArtem Bityutskiy err = 11; 14301e51764aSArtem Bityutskiy goto out; 14311e51764aSArtem Bityutskiy } 14321e51764aSArtem Bityutskiy } 14331e51764aSArtem Bityutskiy } else { 14341e51764aSArtem Bityutskiy /* This may only be root znode */ 14351e51764aSArtem Bityutskiy if (zbr != &c->zroot) { 14361e51764aSArtem Bityutskiy err = 12; 14371e51764aSArtem Bityutskiy goto out; 14381e51764aSArtem Bityutskiy } 14391e51764aSArtem Bityutskiy } 14401e51764aSArtem Bityutskiy 14411e51764aSArtem Bityutskiy /* 14421e51764aSArtem Bityutskiy * Make sure that next key is greater or equivalent then the previous 14431e51764aSArtem Bityutskiy * one. 14441e51764aSArtem Bityutskiy */ 14451e51764aSArtem Bityutskiy for (n = 1; n < znode->child_cnt; n++) { 14461e51764aSArtem Bityutskiy cmp = keys_cmp(c, &znode->zbranch[n - 1].key, 14471e51764aSArtem Bityutskiy &znode->zbranch[n].key); 14481e51764aSArtem Bityutskiy if (cmp > 0) { 14491e51764aSArtem Bityutskiy err = 13; 14501e51764aSArtem Bityutskiy goto out; 14511e51764aSArtem Bityutskiy } 14521e51764aSArtem Bityutskiy if (cmp == 0) { 14531e51764aSArtem Bityutskiy /* This can only be keys with colliding hash */ 14541e51764aSArtem Bityutskiy if (!is_hash_key(c, &znode->zbranch[n].key)) { 14551e51764aSArtem Bityutskiy err = 14; 14561e51764aSArtem Bityutskiy goto out; 14571e51764aSArtem Bityutskiy } 14581e51764aSArtem Bityutskiy 14591e51764aSArtem Bityutskiy if (znode->level != 0 || c->replaying) 14601e51764aSArtem Bityutskiy continue; 14611e51764aSArtem Bityutskiy 14621e51764aSArtem Bityutskiy /* 14631e51764aSArtem Bityutskiy * Colliding keys should follow binary order of 14641e51764aSArtem Bityutskiy * corresponding xentry/dentry names. 14651e51764aSArtem Bityutskiy */ 14661e51764aSArtem Bityutskiy err = dbg_check_key_order(c, &znode->zbranch[n - 1], 14671e51764aSArtem Bityutskiy &znode->zbranch[n]); 14681e51764aSArtem Bityutskiy if (err < 0) 14691e51764aSArtem Bityutskiy return err; 14701e51764aSArtem Bityutskiy if (err) { 14711e51764aSArtem Bityutskiy err = 15; 14721e51764aSArtem Bityutskiy goto out; 14731e51764aSArtem Bityutskiy } 14741e51764aSArtem Bityutskiy } 14751e51764aSArtem Bityutskiy } 14761e51764aSArtem Bityutskiy 14771e51764aSArtem Bityutskiy for (n = 0; n < znode->child_cnt; n++) { 14781e51764aSArtem Bityutskiy if (!znode->zbranch[n].znode && 14791e51764aSArtem Bityutskiy (znode->zbranch[n].lnum == 0 || 14801e51764aSArtem Bityutskiy znode->zbranch[n].len == 0)) { 14811e51764aSArtem Bityutskiy err = 16; 14821e51764aSArtem Bityutskiy goto out; 14831e51764aSArtem Bityutskiy } 14841e51764aSArtem Bityutskiy 14851e51764aSArtem Bityutskiy if (znode->zbranch[n].lnum != 0 && 14861e51764aSArtem Bityutskiy znode->zbranch[n].len == 0) { 14871e51764aSArtem Bityutskiy err = 17; 14881e51764aSArtem Bityutskiy goto out; 14891e51764aSArtem Bityutskiy } 14901e51764aSArtem Bityutskiy 14911e51764aSArtem Bityutskiy if (znode->zbranch[n].lnum == 0 && 14921e51764aSArtem Bityutskiy znode->zbranch[n].len != 0) { 14931e51764aSArtem Bityutskiy err = 18; 14941e51764aSArtem Bityutskiy goto out; 14951e51764aSArtem Bityutskiy } 14961e51764aSArtem Bityutskiy 14971e51764aSArtem Bityutskiy if (znode->zbranch[n].lnum == 0 && 14981e51764aSArtem Bityutskiy znode->zbranch[n].offs != 0) { 14991e51764aSArtem Bityutskiy err = 19; 15001e51764aSArtem Bityutskiy goto out; 15011e51764aSArtem Bityutskiy } 15021e51764aSArtem Bityutskiy 15031e51764aSArtem Bityutskiy if (znode->level != 0 && znode->zbranch[n].znode) 15041e51764aSArtem Bityutskiy if (znode->zbranch[n].znode->parent != znode) { 15051e51764aSArtem Bityutskiy err = 20; 15061e51764aSArtem Bityutskiy goto out; 15071e51764aSArtem Bityutskiy } 15081e51764aSArtem Bityutskiy } 15091e51764aSArtem Bityutskiy 15101e51764aSArtem Bityutskiy return 0; 15111e51764aSArtem Bityutskiy 15121e51764aSArtem Bityutskiy out: 15131e51764aSArtem Bityutskiy ubifs_err("failed, error %d", err); 15141e51764aSArtem Bityutskiy ubifs_msg("dump of the znode"); 15151e51764aSArtem Bityutskiy dbg_dump_znode(c, znode); 15161e51764aSArtem Bityutskiy if (zp) { 15171e51764aSArtem Bityutskiy ubifs_msg("dump of the parent znode"); 15181e51764aSArtem Bityutskiy dbg_dump_znode(c, zp); 15191e51764aSArtem Bityutskiy } 15201e51764aSArtem Bityutskiy dump_stack(); 15211e51764aSArtem Bityutskiy return -EINVAL; 15221e51764aSArtem Bityutskiy } 15231e51764aSArtem Bityutskiy 15241e51764aSArtem Bityutskiy /** 15251e51764aSArtem Bityutskiy * dbg_check_tnc - check TNC tree. 15261e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 15271e51764aSArtem Bityutskiy * @extra: do extra checks that are possible at start commit 15281e51764aSArtem Bityutskiy * 15291e51764aSArtem Bityutskiy * This function traverses whole TNC tree and checks every znode. Returns zero 15301e51764aSArtem Bityutskiy * if everything is all right and %-EINVAL if something is wrong with TNC. 15311e51764aSArtem Bityutskiy */ 15321e51764aSArtem Bityutskiy int dbg_check_tnc(struct ubifs_info *c, int extra) 15331e51764aSArtem Bityutskiy { 15341e51764aSArtem Bityutskiy struct ubifs_znode *znode; 15351e51764aSArtem Bityutskiy long clean_cnt = 0, dirty_cnt = 0; 15361e51764aSArtem Bityutskiy int err, last; 15371e51764aSArtem Bityutskiy 15388d7819b4SArtem Bityutskiy if (!dbg_is_chk_index(c)) 15391e51764aSArtem Bityutskiy return 0; 15401e51764aSArtem Bityutskiy 15411e51764aSArtem Bityutskiy ubifs_assert(mutex_is_locked(&c->tnc_mutex)); 15421e51764aSArtem Bityutskiy if (!c->zroot.znode) 15431e51764aSArtem Bityutskiy return 0; 15441e51764aSArtem Bityutskiy 15451e51764aSArtem Bityutskiy znode = ubifs_tnc_postorder_first(c->zroot.znode); 15461e51764aSArtem Bityutskiy while (1) { 15471e51764aSArtem Bityutskiy struct ubifs_znode *prev; 15481e51764aSArtem Bityutskiy struct ubifs_zbranch *zbr; 15491e51764aSArtem Bityutskiy 15501e51764aSArtem Bityutskiy if (!znode->parent) 15511e51764aSArtem Bityutskiy zbr = &c->zroot; 15521e51764aSArtem Bityutskiy else 15531e51764aSArtem Bityutskiy zbr = &znode->parent->zbranch[znode->iip]; 15541e51764aSArtem Bityutskiy 15551e51764aSArtem Bityutskiy err = dbg_check_znode(c, zbr); 15561e51764aSArtem Bityutskiy if (err) 15571e51764aSArtem Bityutskiy return err; 15581e51764aSArtem Bityutskiy 15591e51764aSArtem Bityutskiy if (extra) { 15601e51764aSArtem Bityutskiy if (ubifs_zn_dirty(znode)) 15611e51764aSArtem Bityutskiy dirty_cnt += 1; 15621e51764aSArtem Bityutskiy else 15631e51764aSArtem Bityutskiy clean_cnt += 1; 15641e51764aSArtem Bityutskiy } 15651e51764aSArtem Bityutskiy 15661e51764aSArtem Bityutskiy prev = znode; 15671e51764aSArtem Bityutskiy znode = ubifs_tnc_postorder_next(znode); 15681e51764aSArtem Bityutskiy if (!znode) 15691e51764aSArtem Bityutskiy break; 15701e51764aSArtem Bityutskiy 15711e51764aSArtem Bityutskiy /* 15721e51764aSArtem Bityutskiy * If the last key of this znode is equivalent to the first key 15731e51764aSArtem Bityutskiy * of the next znode (collision), then check order of the keys. 15741e51764aSArtem Bityutskiy */ 15751e51764aSArtem Bityutskiy last = prev->child_cnt - 1; 15761e51764aSArtem Bityutskiy if (prev->level == 0 && znode->level == 0 && !c->replaying && 15771e51764aSArtem Bityutskiy !keys_cmp(c, &prev->zbranch[last].key, 15781e51764aSArtem Bityutskiy &znode->zbranch[0].key)) { 15791e51764aSArtem Bityutskiy err = dbg_check_key_order(c, &prev->zbranch[last], 15801e51764aSArtem Bityutskiy &znode->zbranch[0]); 15811e51764aSArtem Bityutskiy if (err < 0) 15821e51764aSArtem Bityutskiy return err; 15831e51764aSArtem Bityutskiy if (err) { 15841e51764aSArtem Bityutskiy ubifs_msg("first znode"); 15851e51764aSArtem Bityutskiy dbg_dump_znode(c, prev); 15861e51764aSArtem Bityutskiy ubifs_msg("second znode"); 15871e51764aSArtem Bityutskiy dbg_dump_znode(c, znode); 15881e51764aSArtem Bityutskiy return -EINVAL; 15891e51764aSArtem Bityutskiy } 15901e51764aSArtem Bityutskiy } 15911e51764aSArtem Bityutskiy } 15921e51764aSArtem Bityutskiy 15931e51764aSArtem Bityutskiy if (extra) { 15941e51764aSArtem Bityutskiy if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) { 15951e51764aSArtem Bityutskiy ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld", 15961e51764aSArtem Bityutskiy atomic_long_read(&c->clean_zn_cnt), 15971e51764aSArtem Bityutskiy clean_cnt); 15981e51764aSArtem Bityutskiy return -EINVAL; 15991e51764aSArtem Bityutskiy } 16001e51764aSArtem Bityutskiy if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) { 16011e51764aSArtem Bityutskiy ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld", 16021e51764aSArtem Bityutskiy atomic_long_read(&c->dirty_zn_cnt), 16031e51764aSArtem Bityutskiy dirty_cnt); 16041e51764aSArtem Bityutskiy return -EINVAL; 16051e51764aSArtem Bityutskiy } 16061e51764aSArtem Bityutskiy } 16071e51764aSArtem Bityutskiy 16081e51764aSArtem Bityutskiy return 0; 16091e51764aSArtem Bityutskiy } 16101e51764aSArtem Bityutskiy 16111e51764aSArtem Bityutskiy /** 16121e51764aSArtem Bityutskiy * dbg_walk_index - walk the on-flash index. 16131e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 16141e51764aSArtem Bityutskiy * @leaf_cb: called for each leaf node 16151e51764aSArtem Bityutskiy * @znode_cb: called for each indexing node 1616227c75c9SAdrian Hunter * @priv: private data which is passed to callbacks 16171e51764aSArtem Bityutskiy * 16181e51764aSArtem Bityutskiy * This function walks the UBIFS index and calls the @leaf_cb for each leaf 16191e51764aSArtem Bityutskiy * node and @znode_cb for each indexing node. Returns zero in case of success 16201e51764aSArtem Bityutskiy * and a negative error code in case of failure. 16211e51764aSArtem Bityutskiy * 16221e51764aSArtem Bityutskiy * It would be better if this function removed every znode it pulled to into 16231e51764aSArtem Bityutskiy * the TNC, so that the behavior more closely matched the non-debugging 16241e51764aSArtem Bityutskiy * behavior. 16251e51764aSArtem Bityutskiy */ 16261e51764aSArtem Bityutskiy int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb, 16271e51764aSArtem Bityutskiy dbg_znode_callback znode_cb, void *priv) 16281e51764aSArtem Bityutskiy { 16291e51764aSArtem Bityutskiy int err; 16301e51764aSArtem Bityutskiy struct ubifs_zbranch *zbr; 16311e51764aSArtem Bityutskiy struct ubifs_znode *znode, *child; 16321e51764aSArtem Bityutskiy 16331e51764aSArtem Bityutskiy mutex_lock(&c->tnc_mutex); 16341e51764aSArtem Bityutskiy /* If the root indexing node is not in TNC - pull it */ 16351e51764aSArtem Bityutskiy if (!c->zroot.znode) { 16361e51764aSArtem Bityutskiy c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0); 16371e51764aSArtem Bityutskiy if (IS_ERR(c->zroot.znode)) { 16381e51764aSArtem Bityutskiy err = PTR_ERR(c->zroot.znode); 16391e51764aSArtem Bityutskiy c->zroot.znode = NULL; 16401e51764aSArtem Bityutskiy goto out_unlock; 16411e51764aSArtem Bityutskiy } 16421e51764aSArtem Bityutskiy } 16431e51764aSArtem Bityutskiy 16441e51764aSArtem Bityutskiy /* 16451e51764aSArtem Bityutskiy * We are going to traverse the indexing tree in the postorder manner. 16461e51764aSArtem Bityutskiy * Go down and find the leftmost indexing node where we are going to 16471e51764aSArtem Bityutskiy * start from. 16481e51764aSArtem Bityutskiy */ 16491e51764aSArtem Bityutskiy znode = c->zroot.znode; 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 16621e51764aSArtem Bityutskiy znode = child; 16631e51764aSArtem Bityutskiy } 16641e51764aSArtem Bityutskiy 16651e51764aSArtem Bityutskiy /* Iterate over all indexing nodes */ 16661e51764aSArtem Bityutskiy while (1) { 16671e51764aSArtem Bityutskiy int idx; 16681e51764aSArtem Bityutskiy 16691e51764aSArtem Bityutskiy cond_resched(); 16701e51764aSArtem Bityutskiy 16711e51764aSArtem Bityutskiy if (znode_cb) { 16721e51764aSArtem Bityutskiy err = znode_cb(c, znode, priv); 16731e51764aSArtem Bityutskiy if (err) { 16741e51764aSArtem Bityutskiy ubifs_err("znode checking function returned " 16751e51764aSArtem Bityutskiy "error %d", err); 16761e51764aSArtem Bityutskiy dbg_dump_znode(c, znode); 16771e51764aSArtem Bityutskiy goto out_dump; 16781e51764aSArtem Bityutskiy } 16791e51764aSArtem Bityutskiy } 16801e51764aSArtem Bityutskiy if (leaf_cb && znode->level == 0) { 16811e51764aSArtem Bityutskiy for (idx = 0; idx < znode->child_cnt; idx++) { 16821e51764aSArtem Bityutskiy zbr = &znode->zbranch[idx]; 16831e51764aSArtem Bityutskiy err = leaf_cb(c, zbr, priv); 16841e51764aSArtem Bityutskiy if (err) { 16851e51764aSArtem Bityutskiy ubifs_err("leaf checking function " 16861e51764aSArtem Bityutskiy "returned error %d, for leaf " 16871e51764aSArtem Bityutskiy "at LEB %d:%d", 16881e51764aSArtem Bityutskiy err, zbr->lnum, zbr->offs); 16891e51764aSArtem Bityutskiy goto out_dump; 16901e51764aSArtem Bityutskiy } 16911e51764aSArtem Bityutskiy } 16921e51764aSArtem Bityutskiy } 16931e51764aSArtem Bityutskiy 16941e51764aSArtem Bityutskiy if (!znode->parent) 16951e51764aSArtem Bityutskiy break; 16961e51764aSArtem Bityutskiy 16971e51764aSArtem Bityutskiy idx = znode->iip + 1; 16981e51764aSArtem Bityutskiy znode = znode->parent; 16991e51764aSArtem Bityutskiy if (idx < znode->child_cnt) { 17001e51764aSArtem Bityutskiy /* Switch to the next index in the parent */ 17011e51764aSArtem Bityutskiy zbr = &znode->zbranch[idx]; 17021e51764aSArtem Bityutskiy child = zbr->znode; 17031e51764aSArtem Bityutskiy if (!child) { 17041e51764aSArtem Bityutskiy child = ubifs_load_znode(c, zbr, znode, idx); 17051e51764aSArtem Bityutskiy if (IS_ERR(child)) { 17061e51764aSArtem Bityutskiy err = PTR_ERR(child); 17071e51764aSArtem Bityutskiy goto out_unlock; 17081e51764aSArtem Bityutskiy } 17091e51764aSArtem Bityutskiy zbr->znode = child; 17101e51764aSArtem Bityutskiy } 17111e51764aSArtem Bityutskiy znode = child; 17121e51764aSArtem Bityutskiy } else 17131e51764aSArtem Bityutskiy /* 17141e51764aSArtem Bityutskiy * This is the last child, switch to the parent and 17151e51764aSArtem Bityutskiy * continue. 17161e51764aSArtem Bityutskiy */ 17171e51764aSArtem Bityutskiy continue; 17181e51764aSArtem Bityutskiy 17191e51764aSArtem Bityutskiy /* Go to the lowest leftmost znode in the new sub-tree */ 17201e51764aSArtem Bityutskiy while (znode->level > 0) { 17211e51764aSArtem Bityutskiy zbr = &znode->zbranch[0]; 17221e51764aSArtem Bityutskiy child = zbr->znode; 17231e51764aSArtem Bityutskiy if (!child) { 17241e51764aSArtem Bityutskiy child = ubifs_load_znode(c, zbr, znode, 0); 17251e51764aSArtem Bityutskiy if (IS_ERR(child)) { 17261e51764aSArtem Bityutskiy err = PTR_ERR(child); 17271e51764aSArtem Bityutskiy goto out_unlock; 17281e51764aSArtem Bityutskiy } 17291e51764aSArtem Bityutskiy zbr->znode = child; 17301e51764aSArtem Bityutskiy } 17311e51764aSArtem Bityutskiy znode = child; 17321e51764aSArtem Bityutskiy } 17331e51764aSArtem Bityutskiy } 17341e51764aSArtem Bityutskiy 17351e51764aSArtem Bityutskiy mutex_unlock(&c->tnc_mutex); 17361e51764aSArtem Bityutskiy return 0; 17371e51764aSArtem Bityutskiy 17381e51764aSArtem Bityutskiy out_dump: 17391e51764aSArtem Bityutskiy if (znode->parent) 17401e51764aSArtem Bityutskiy zbr = &znode->parent->zbranch[znode->iip]; 17411e51764aSArtem Bityutskiy else 17421e51764aSArtem Bityutskiy zbr = &c->zroot; 17431e51764aSArtem Bityutskiy ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs); 17441e51764aSArtem Bityutskiy dbg_dump_znode(c, znode); 17451e51764aSArtem Bityutskiy out_unlock: 17461e51764aSArtem Bityutskiy mutex_unlock(&c->tnc_mutex); 17471e51764aSArtem Bityutskiy return err; 17481e51764aSArtem Bityutskiy } 17491e51764aSArtem Bityutskiy 17501e51764aSArtem Bityutskiy /** 17511e51764aSArtem Bityutskiy * add_size - add znode size to partially calculated index size. 17521e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 17531e51764aSArtem Bityutskiy * @znode: znode to add size for 17541e51764aSArtem Bityutskiy * @priv: partially calculated index size 17551e51764aSArtem Bityutskiy * 17561e51764aSArtem Bityutskiy * This is a helper function for 'dbg_check_idx_size()' which is called for 17571e51764aSArtem Bityutskiy * every indexing node and adds its size to the 'long long' variable pointed to 17581e51764aSArtem Bityutskiy * by @priv. 17591e51764aSArtem Bityutskiy */ 17601e51764aSArtem Bityutskiy static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv) 17611e51764aSArtem Bityutskiy { 17621e51764aSArtem Bityutskiy long long *idx_size = priv; 17631e51764aSArtem Bityutskiy int add; 17641e51764aSArtem Bityutskiy 17651e51764aSArtem Bityutskiy add = ubifs_idx_node_sz(c, znode->child_cnt); 17661e51764aSArtem Bityutskiy add = ALIGN(add, 8); 17671e51764aSArtem Bityutskiy *idx_size += add; 17681e51764aSArtem Bityutskiy return 0; 17691e51764aSArtem Bityutskiy } 17701e51764aSArtem Bityutskiy 17711e51764aSArtem Bityutskiy /** 17721e51764aSArtem Bityutskiy * dbg_check_idx_size - check index size. 17731e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 17741e51764aSArtem Bityutskiy * @idx_size: size to check 17751e51764aSArtem Bityutskiy * 17761e51764aSArtem Bityutskiy * This function walks the UBIFS index, calculates its size and checks that the 17771e51764aSArtem Bityutskiy * size is equivalent to @idx_size. Returns zero in case of success and a 17781e51764aSArtem Bityutskiy * negative error code in case of failure. 17791e51764aSArtem Bityutskiy */ 17801e51764aSArtem Bityutskiy int dbg_check_idx_size(struct ubifs_info *c, long long idx_size) 17811e51764aSArtem Bityutskiy { 17821e51764aSArtem Bityutskiy int err; 17831e51764aSArtem Bityutskiy long long calc = 0; 17841e51764aSArtem Bityutskiy 17858d7819b4SArtem Bityutskiy if (!dbg_is_chk_index(c)) 17861e51764aSArtem Bityutskiy return 0; 17871e51764aSArtem Bityutskiy 17881e51764aSArtem Bityutskiy err = dbg_walk_index(c, NULL, add_size, &calc); 17891e51764aSArtem Bityutskiy if (err) { 17901e51764aSArtem Bityutskiy ubifs_err("error %d while walking the index", err); 17911e51764aSArtem Bityutskiy return err; 17921e51764aSArtem Bityutskiy } 17931e51764aSArtem Bityutskiy 17941e51764aSArtem Bityutskiy if (calc != idx_size) { 17951e51764aSArtem Bityutskiy ubifs_err("index size check failed: calculated size is %lld, " 17961e51764aSArtem Bityutskiy "should be %lld", calc, idx_size); 17971e51764aSArtem Bityutskiy dump_stack(); 17981e51764aSArtem Bityutskiy return -EINVAL; 17991e51764aSArtem Bityutskiy } 18001e51764aSArtem Bityutskiy 18011e51764aSArtem Bityutskiy return 0; 18021e51764aSArtem Bityutskiy } 18031e51764aSArtem Bityutskiy 18041e51764aSArtem Bityutskiy /** 18051e51764aSArtem Bityutskiy * struct fsck_inode - information about an inode used when checking the file-system. 18061e51764aSArtem Bityutskiy * @rb: link in the RB-tree of inodes 18071e51764aSArtem Bityutskiy * @inum: inode number 18081e51764aSArtem Bityutskiy * @mode: inode type, permissions, etc 18091e51764aSArtem Bityutskiy * @nlink: inode link count 18101e51764aSArtem Bityutskiy * @xattr_cnt: count of extended attributes 18111e51764aSArtem Bityutskiy * @references: how many directory/xattr entries refer this inode (calculated 18121e51764aSArtem Bityutskiy * while walking the index) 18131e51764aSArtem Bityutskiy * @calc_cnt: for directory inode count of child directories 18141e51764aSArtem Bityutskiy * @size: inode size (read from on-flash inode) 18151e51764aSArtem Bityutskiy * @xattr_sz: summary size of all extended attributes (read from on-flash 18161e51764aSArtem Bityutskiy * inode) 18171e51764aSArtem Bityutskiy * @calc_sz: for directories calculated directory size 18181e51764aSArtem Bityutskiy * @calc_xcnt: count of extended attributes 18191e51764aSArtem Bityutskiy * @calc_xsz: calculated summary size of all extended attributes 18201e51764aSArtem Bityutskiy * @xattr_nms: sum of lengths of all extended attribute names belonging to this 18211e51764aSArtem Bityutskiy * inode (read from on-flash inode) 18221e51764aSArtem Bityutskiy * @calc_xnms: calculated sum of lengths of all extended attribute names 18231e51764aSArtem Bityutskiy */ 18241e51764aSArtem Bityutskiy struct fsck_inode { 18251e51764aSArtem Bityutskiy struct rb_node rb; 18261e51764aSArtem Bityutskiy ino_t inum; 18271e51764aSArtem Bityutskiy umode_t mode; 18281e51764aSArtem Bityutskiy unsigned int nlink; 18291e51764aSArtem Bityutskiy unsigned int xattr_cnt; 18301e51764aSArtem Bityutskiy int references; 18311e51764aSArtem Bityutskiy int calc_cnt; 18321e51764aSArtem Bityutskiy long long size; 18331e51764aSArtem Bityutskiy unsigned int xattr_sz; 18341e51764aSArtem Bityutskiy long long calc_sz; 18351e51764aSArtem Bityutskiy long long calc_xcnt; 18361e51764aSArtem Bityutskiy long long calc_xsz; 18371e51764aSArtem Bityutskiy unsigned int xattr_nms; 18381e51764aSArtem Bityutskiy long long calc_xnms; 18391e51764aSArtem Bityutskiy }; 18401e51764aSArtem Bityutskiy 18411e51764aSArtem Bityutskiy /** 18421e51764aSArtem Bityutskiy * struct fsck_data - private FS checking information. 18431e51764aSArtem Bityutskiy * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects) 18441e51764aSArtem Bityutskiy */ 18451e51764aSArtem Bityutskiy struct fsck_data { 18461e51764aSArtem Bityutskiy struct rb_root inodes; 18471e51764aSArtem Bityutskiy }; 18481e51764aSArtem Bityutskiy 18491e51764aSArtem Bityutskiy /** 18501e51764aSArtem Bityutskiy * add_inode - add inode information to RB-tree of inodes. 18511e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 18521e51764aSArtem Bityutskiy * @fsckd: FS checking information 18531e51764aSArtem Bityutskiy * @ino: raw UBIFS inode to add 18541e51764aSArtem Bityutskiy * 18551e51764aSArtem Bityutskiy * This is a helper function for 'check_leaf()' which adds information about 18561e51764aSArtem Bityutskiy * inode @ino to the RB-tree of inodes. Returns inode information pointer in 18571e51764aSArtem Bityutskiy * case of success and a negative error code in case of failure. 18581e51764aSArtem Bityutskiy */ 18591e51764aSArtem Bityutskiy static struct fsck_inode *add_inode(struct ubifs_info *c, 18601e51764aSArtem Bityutskiy struct fsck_data *fsckd, 18611e51764aSArtem Bityutskiy struct ubifs_ino_node *ino) 18621e51764aSArtem Bityutskiy { 18631e51764aSArtem Bityutskiy struct rb_node **p, *parent = NULL; 18641e51764aSArtem Bityutskiy struct fsck_inode *fscki; 18651e51764aSArtem Bityutskiy ino_t inum = key_inum_flash(c, &ino->key); 186645cd5cddSArtem Bityutskiy struct inode *inode; 186745cd5cddSArtem Bityutskiy struct ubifs_inode *ui; 18681e51764aSArtem Bityutskiy 18691e51764aSArtem Bityutskiy p = &fsckd->inodes.rb_node; 18701e51764aSArtem Bityutskiy while (*p) { 18711e51764aSArtem Bityutskiy parent = *p; 18721e51764aSArtem Bityutskiy fscki = rb_entry(parent, struct fsck_inode, rb); 18731e51764aSArtem Bityutskiy if (inum < fscki->inum) 18741e51764aSArtem Bityutskiy p = &(*p)->rb_left; 18751e51764aSArtem Bityutskiy else if (inum > fscki->inum) 18761e51764aSArtem Bityutskiy p = &(*p)->rb_right; 18771e51764aSArtem Bityutskiy else 18781e51764aSArtem Bityutskiy return fscki; 18791e51764aSArtem Bityutskiy } 18801e51764aSArtem Bityutskiy 18811e51764aSArtem Bityutskiy if (inum > c->highest_inum) { 18821e51764aSArtem Bityutskiy ubifs_err("too high inode number, max. is %lu", 1883e84461adSArtem Bityutskiy (unsigned long)c->highest_inum); 18841e51764aSArtem Bityutskiy return ERR_PTR(-EINVAL); 18851e51764aSArtem Bityutskiy } 18861e51764aSArtem Bityutskiy 18871e51764aSArtem Bityutskiy fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS); 18881e51764aSArtem Bityutskiy if (!fscki) 18891e51764aSArtem Bityutskiy return ERR_PTR(-ENOMEM); 18901e51764aSArtem Bityutskiy 189145cd5cddSArtem Bityutskiy inode = ilookup(c->vfs_sb, inum); 189245cd5cddSArtem Bityutskiy 18931e51764aSArtem Bityutskiy fscki->inum = inum; 189445cd5cddSArtem Bityutskiy /* 189545cd5cddSArtem Bityutskiy * If the inode is present in the VFS inode cache, use it instead of 189645cd5cddSArtem Bityutskiy * the on-flash inode which might be out-of-date. E.g., the size might 189745cd5cddSArtem Bityutskiy * be out-of-date. If we do not do this, the following may happen, for 189845cd5cddSArtem Bityutskiy * example: 189945cd5cddSArtem Bityutskiy * 1. A power cut happens 190045cd5cddSArtem Bityutskiy * 2. We mount the file-system R/O, the replay process fixes up the 190145cd5cddSArtem Bityutskiy * inode size in the VFS cache, but on on-flash. 190245cd5cddSArtem Bityutskiy * 3. 'check_leaf()' fails because it hits a data node beyond inode 190345cd5cddSArtem Bityutskiy * size. 190445cd5cddSArtem Bityutskiy */ 190545cd5cddSArtem Bityutskiy if (!inode) { 19061e51764aSArtem Bityutskiy fscki->nlink = le32_to_cpu(ino->nlink); 19071e51764aSArtem Bityutskiy fscki->size = le64_to_cpu(ino->size); 19081e51764aSArtem Bityutskiy fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt); 19091e51764aSArtem Bityutskiy fscki->xattr_sz = le32_to_cpu(ino->xattr_size); 19101e51764aSArtem Bityutskiy fscki->xattr_nms = le32_to_cpu(ino->xattr_names); 19111e51764aSArtem Bityutskiy fscki->mode = le32_to_cpu(ino->mode); 191245cd5cddSArtem Bityutskiy } else { 191345cd5cddSArtem Bityutskiy ui = ubifs_inode(inode); 191445cd5cddSArtem Bityutskiy fscki->nlink = inode->i_nlink; 191545cd5cddSArtem Bityutskiy fscki->size = inode->i_size; 191645cd5cddSArtem Bityutskiy fscki->xattr_cnt = ui->xattr_cnt; 191745cd5cddSArtem Bityutskiy fscki->xattr_sz = ui->xattr_size; 191845cd5cddSArtem Bityutskiy fscki->xattr_nms = ui->xattr_names; 191945cd5cddSArtem Bityutskiy fscki->mode = inode->i_mode; 192045cd5cddSArtem Bityutskiy iput(inode); 192145cd5cddSArtem Bityutskiy } 192245cd5cddSArtem Bityutskiy 19231e51764aSArtem Bityutskiy if (S_ISDIR(fscki->mode)) { 19241e51764aSArtem Bityutskiy fscki->calc_sz = UBIFS_INO_NODE_SZ; 19251e51764aSArtem Bityutskiy fscki->calc_cnt = 2; 19261e51764aSArtem Bityutskiy } 192745cd5cddSArtem Bityutskiy 19281e51764aSArtem Bityutskiy rb_link_node(&fscki->rb, parent, p); 19291e51764aSArtem Bityutskiy rb_insert_color(&fscki->rb, &fsckd->inodes); 193045cd5cddSArtem Bityutskiy 19311e51764aSArtem Bityutskiy return fscki; 19321e51764aSArtem Bityutskiy } 19331e51764aSArtem Bityutskiy 19341e51764aSArtem Bityutskiy /** 19351e51764aSArtem Bityutskiy * search_inode - search inode in the RB-tree of inodes. 19361e51764aSArtem Bityutskiy * @fsckd: FS checking information 19371e51764aSArtem Bityutskiy * @inum: inode number to search 19381e51764aSArtem Bityutskiy * 19391e51764aSArtem Bityutskiy * This is a helper function for 'check_leaf()' which searches inode @inum in 19401e51764aSArtem Bityutskiy * the RB-tree of inodes and returns an inode information pointer or %NULL if 19411e51764aSArtem Bityutskiy * the inode was not found. 19421e51764aSArtem Bityutskiy */ 19431e51764aSArtem Bityutskiy static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum) 19441e51764aSArtem Bityutskiy { 19451e51764aSArtem Bityutskiy struct rb_node *p; 19461e51764aSArtem Bityutskiy struct fsck_inode *fscki; 19471e51764aSArtem Bityutskiy 19481e51764aSArtem Bityutskiy p = fsckd->inodes.rb_node; 19491e51764aSArtem Bityutskiy while (p) { 19501e51764aSArtem Bityutskiy fscki = rb_entry(p, struct fsck_inode, rb); 19511e51764aSArtem Bityutskiy if (inum < fscki->inum) 19521e51764aSArtem Bityutskiy p = p->rb_left; 19531e51764aSArtem Bityutskiy else if (inum > fscki->inum) 19541e51764aSArtem Bityutskiy p = p->rb_right; 19551e51764aSArtem Bityutskiy else 19561e51764aSArtem Bityutskiy return fscki; 19571e51764aSArtem Bityutskiy } 19581e51764aSArtem Bityutskiy return NULL; 19591e51764aSArtem Bityutskiy } 19601e51764aSArtem Bityutskiy 19611e51764aSArtem Bityutskiy /** 19621e51764aSArtem Bityutskiy * read_add_inode - read inode node and add it to RB-tree of inodes. 19631e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 19641e51764aSArtem Bityutskiy * @fsckd: FS checking information 19651e51764aSArtem Bityutskiy * @inum: inode number to read 19661e51764aSArtem Bityutskiy * 19671e51764aSArtem Bityutskiy * This is a helper function for 'check_leaf()' which finds inode node @inum in 19681e51764aSArtem Bityutskiy * the index, reads it, and adds it to the RB-tree of inodes. Returns inode 19691e51764aSArtem Bityutskiy * information pointer in case of success and a negative error code in case of 19701e51764aSArtem Bityutskiy * failure. 19711e51764aSArtem Bityutskiy */ 19721e51764aSArtem Bityutskiy static struct fsck_inode *read_add_inode(struct ubifs_info *c, 19731e51764aSArtem Bityutskiy struct fsck_data *fsckd, ino_t inum) 19741e51764aSArtem Bityutskiy { 19751e51764aSArtem Bityutskiy int n, err; 19761e51764aSArtem Bityutskiy union ubifs_key key; 19771e51764aSArtem Bityutskiy struct ubifs_znode *znode; 19781e51764aSArtem Bityutskiy struct ubifs_zbranch *zbr; 19791e51764aSArtem Bityutskiy struct ubifs_ino_node *ino; 19801e51764aSArtem Bityutskiy struct fsck_inode *fscki; 19811e51764aSArtem Bityutskiy 19821e51764aSArtem Bityutskiy fscki = search_inode(fsckd, inum); 19831e51764aSArtem Bityutskiy if (fscki) 19841e51764aSArtem Bityutskiy return fscki; 19851e51764aSArtem Bityutskiy 19861e51764aSArtem Bityutskiy ino_key_init(c, &key, inum); 19871e51764aSArtem Bityutskiy err = ubifs_lookup_level0(c, &key, &znode, &n); 19881e51764aSArtem Bityutskiy if (!err) { 1989e84461adSArtem Bityutskiy ubifs_err("inode %lu not found in index", (unsigned long)inum); 19901e51764aSArtem Bityutskiy return ERR_PTR(-ENOENT); 19911e51764aSArtem Bityutskiy } else if (err < 0) { 1992e84461adSArtem Bityutskiy ubifs_err("error %d while looking up inode %lu", 1993e84461adSArtem Bityutskiy err, (unsigned long)inum); 19941e51764aSArtem Bityutskiy return ERR_PTR(err); 19951e51764aSArtem Bityutskiy } 19961e51764aSArtem Bityutskiy 19971e51764aSArtem Bityutskiy zbr = &znode->zbranch[n]; 19981e51764aSArtem Bityutskiy if (zbr->len < UBIFS_INO_NODE_SZ) { 1999e84461adSArtem Bityutskiy ubifs_err("bad node %lu node length %d", 2000e84461adSArtem Bityutskiy (unsigned long)inum, zbr->len); 20011e51764aSArtem Bityutskiy return ERR_PTR(-EINVAL); 20021e51764aSArtem Bityutskiy } 20031e51764aSArtem Bityutskiy 20041e51764aSArtem Bityutskiy ino = kmalloc(zbr->len, GFP_NOFS); 20051e51764aSArtem Bityutskiy if (!ino) 20061e51764aSArtem Bityutskiy return ERR_PTR(-ENOMEM); 20071e51764aSArtem Bityutskiy 20081e51764aSArtem Bityutskiy err = ubifs_tnc_read_node(c, zbr, ino); 20091e51764aSArtem Bityutskiy if (err) { 20101e51764aSArtem Bityutskiy ubifs_err("cannot read inode node at LEB %d:%d, error %d", 20111e51764aSArtem Bityutskiy zbr->lnum, zbr->offs, err); 20121e51764aSArtem Bityutskiy kfree(ino); 20131e51764aSArtem Bityutskiy return ERR_PTR(err); 20141e51764aSArtem Bityutskiy } 20151e51764aSArtem Bityutskiy 20161e51764aSArtem Bityutskiy fscki = add_inode(c, fsckd, ino); 20171e51764aSArtem Bityutskiy kfree(ino); 20181e51764aSArtem Bityutskiy if (IS_ERR(fscki)) { 20191e51764aSArtem Bityutskiy ubifs_err("error %ld while adding inode %lu node", 2020e84461adSArtem Bityutskiy PTR_ERR(fscki), (unsigned long)inum); 20211e51764aSArtem Bityutskiy return fscki; 20221e51764aSArtem Bityutskiy } 20231e51764aSArtem Bityutskiy 20241e51764aSArtem Bityutskiy return fscki; 20251e51764aSArtem Bityutskiy } 20261e51764aSArtem Bityutskiy 20271e51764aSArtem Bityutskiy /** 20281e51764aSArtem Bityutskiy * check_leaf - check leaf node. 20291e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 20301e51764aSArtem Bityutskiy * @zbr: zbranch of the leaf node to check 20311e51764aSArtem Bityutskiy * @priv: FS checking information 20321e51764aSArtem Bityutskiy * 20331e51764aSArtem Bityutskiy * This is a helper function for 'dbg_check_filesystem()' which is called for 20341e51764aSArtem Bityutskiy * every single leaf node while walking the indexing tree. It checks that the 20351e51764aSArtem Bityutskiy * leaf node referred from the indexing tree exists, has correct CRC, and does 20361e51764aSArtem Bityutskiy * some other basic validation. This function is also responsible for building 20371e51764aSArtem Bityutskiy * an RB-tree of inodes - it adds all inodes into the RB-tree. It also 20381e51764aSArtem Bityutskiy * calculates reference count, size, etc for each inode in order to later 20391e51764aSArtem Bityutskiy * compare them to the information stored inside the inodes and detect possible 20401e51764aSArtem Bityutskiy * inconsistencies. Returns zero in case of success and a negative error code 20411e51764aSArtem Bityutskiy * in case of failure. 20421e51764aSArtem Bityutskiy */ 20431e51764aSArtem Bityutskiy static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr, 20441e51764aSArtem Bityutskiy void *priv) 20451e51764aSArtem Bityutskiy { 20461e51764aSArtem Bityutskiy ino_t inum; 20471e51764aSArtem Bityutskiy void *node; 20481e51764aSArtem Bityutskiy struct ubifs_ch *ch; 20491e51764aSArtem Bityutskiy int err, type = key_type(c, &zbr->key); 20501e51764aSArtem Bityutskiy struct fsck_inode *fscki; 20511e51764aSArtem Bityutskiy 20521e51764aSArtem Bityutskiy if (zbr->len < UBIFS_CH_SZ) { 20531e51764aSArtem Bityutskiy ubifs_err("bad leaf length %d (LEB %d:%d)", 20541e51764aSArtem Bityutskiy zbr->len, zbr->lnum, zbr->offs); 20551e51764aSArtem Bityutskiy return -EINVAL; 20561e51764aSArtem Bityutskiy } 20571e51764aSArtem Bityutskiy 20581e51764aSArtem Bityutskiy node = kmalloc(zbr->len, GFP_NOFS); 20591e51764aSArtem Bityutskiy if (!node) 20601e51764aSArtem Bityutskiy return -ENOMEM; 20611e51764aSArtem Bityutskiy 20621e51764aSArtem Bityutskiy err = ubifs_tnc_read_node(c, zbr, node); 20631e51764aSArtem Bityutskiy if (err) { 20641e51764aSArtem Bityutskiy ubifs_err("cannot read leaf node at LEB %d:%d, error %d", 20651e51764aSArtem Bityutskiy zbr->lnum, zbr->offs, err); 20661e51764aSArtem Bityutskiy goto out_free; 20671e51764aSArtem Bityutskiy } 20681e51764aSArtem Bityutskiy 20691e51764aSArtem Bityutskiy /* If this is an inode node, add it to RB-tree of inodes */ 20701e51764aSArtem Bityutskiy if (type == UBIFS_INO_KEY) { 20711e51764aSArtem Bityutskiy fscki = add_inode(c, priv, node); 20721e51764aSArtem Bityutskiy if (IS_ERR(fscki)) { 20731e51764aSArtem Bityutskiy err = PTR_ERR(fscki); 20741e51764aSArtem Bityutskiy ubifs_err("error %d while adding inode node", err); 20751e51764aSArtem Bityutskiy goto out_dump; 20761e51764aSArtem Bityutskiy } 20771e51764aSArtem Bityutskiy goto out; 20781e51764aSArtem Bityutskiy } 20791e51764aSArtem Bityutskiy 20801e51764aSArtem Bityutskiy if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY && 20811e51764aSArtem Bityutskiy type != UBIFS_DATA_KEY) { 20821e51764aSArtem Bityutskiy ubifs_err("unexpected node type %d at LEB %d:%d", 20831e51764aSArtem Bityutskiy type, zbr->lnum, zbr->offs); 20841e51764aSArtem Bityutskiy err = -EINVAL; 20851e51764aSArtem Bityutskiy goto out_free; 20861e51764aSArtem Bityutskiy } 20871e51764aSArtem Bityutskiy 20881e51764aSArtem Bityutskiy ch = node; 20891e51764aSArtem Bityutskiy if (le64_to_cpu(ch->sqnum) > c->max_sqnum) { 20901e51764aSArtem Bityutskiy ubifs_err("too high sequence number, max. is %llu", 20911e51764aSArtem Bityutskiy c->max_sqnum); 20921e51764aSArtem Bityutskiy err = -EINVAL; 20931e51764aSArtem Bityutskiy goto out_dump; 20941e51764aSArtem Bityutskiy } 20951e51764aSArtem Bityutskiy 20961e51764aSArtem Bityutskiy if (type == UBIFS_DATA_KEY) { 20971e51764aSArtem Bityutskiy long long blk_offs; 20981e51764aSArtem Bityutskiy struct ubifs_data_node *dn = node; 20991e51764aSArtem Bityutskiy 21001e51764aSArtem Bityutskiy /* 21011e51764aSArtem Bityutskiy * Search the inode node this data node belongs to and insert 21021e51764aSArtem Bityutskiy * it to the RB-tree of inodes. 21031e51764aSArtem Bityutskiy */ 21041e51764aSArtem Bityutskiy inum = key_inum_flash(c, &dn->key); 21051e51764aSArtem Bityutskiy fscki = read_add_inode(c, priv, inum); 21061e51764aSArtem Bityutskiy if (IS_ERR(fscki)) { 21071e51764aSArtem Bityutskiy err = PTR_ERR(fscki); 21081e51764aSArtem Bityutskiy ubifs_err("error %d while processing data node and " 2109e84461adSArtem Bityutskiy "trying to find inode node %lu", 2110e84461adSArtem Bityutskiy err, (unsigned long)inum); 21111e51764aSArtem Bityutskiy goto out_dump; 21121e51764aSArtem Bityutskiy } 21131e51764aSArtem Bityutskiy 21141e51764aSArtem Bityutskiy /* Make sure the data node is within inode size */ 21151e51764aSArtem Bityutskiy blk_offs = key_block_flash(c, &dn->key); 21161e51764aSArtem Bityutskiy blk_offs <<= UBIFS_BLOCK_SHIFT; 21171e51764aSArtem Bityutskiy blk_offs += le32_to_cpu(dn->size); 21181e51764aSArtem Bityutskiy if (blk_offs > fscki->size) { 21191e51764aSArtem Bityutskiy ubifs_err("data node at LEB %d:%d is not within inode " 21201e51764aSArtem Bityutskiy "size %lld", zbr->lnum, zbr->offs, 21211e51764aSArtem Bityutskiy fscki->size); 21221e51764aSArtem Bityutskiy err = -EINVAL; 21231e51764aSArtem Bityutskiy goto out_dump; 21241e51764aSArtem Bityutskiy } 21251e51764aSArtem Bityutskiy } else { 21261e51764aSArtem Bityutskiy int nlen; 21271e51764aSArtem Bityutskiy struct ubifs_dent_node *dent = node; 21281e51764aSArtem Bityutskiy struct fsck_inode *fscki1; 21291e51764aSArtem Bityutskiy 21301e51764aSArtem Bityutskiy err = ubifs_validate_entry(c, dent); 21311e51764aSArtem Bityutskiy if (err) 21321e51764aSArtem Bityutskiy goto out_dump; 21331e51764aSArtem Bityutskiy 21341e51764aSArtem Bityutskiy /* 21351e51764aSArtem Bityutskiy * Search the inode node this entry refers to and the parent 21361e51764aSArtem Bityutskiy * inode node and insert them to the RB-tree of inodes. 21371e51764aSArtem Bityutskiy */ 21381e51764aSArtem Bityutskiy inum = le64_to_cpu(dent->inum); 21391e51764aSArtem Bityutskiy fscki = read_add_inode(c, priv, inum); 21401e51764aSArtem Bityutskiy if (IS_ERR(fscki)) { 21411e51764aSArtem Bityutskiy err = PTR_ERR(fscki); 21421e51764aSArtem Bityutskiy ubifs_err("error %d while processing entry node and " 2143e84461adSArtem Bityutskiy "trying to find inode node %lu", 2144e84461adSArtem Bityutskiy err, (unsigned long)inum); 21451e51764aSArtem Bityutskiy goto out_dump; 21461e51764aSArtem Bityutskiy } 21471e51764aSArtem Bityutskiy 21481e51764aSArtem Bityutskiy /* Count how many direntries or xentries refers this inode */ 21491e51764aSArtem Bityutskiy fscki->references += 1; 21501e51764aSArtem Bityutskiy 21511e51764aSArtem Bityutskiy inum = key_inum_flash(c, &dent->key); 21521e51764aSArtem Bityutskiy fscki1 = read_add_inode(c, priv, inum); 21531e51764aSArtem Bityutskiy if (IS_ERR(fscki1)) { 2154b38882f5SRoel Kluin err = PTR_ERR(fscki1); 21551e51764aSArtem Bityutskiy ubifs_err("error %d while processing entry node and " 21561e51764aSArtem Bityutskiy "trying to find parent inode node %lu", 2157e84461adSArtem Bityutskiy err, (unsigned long)inum); 21581e51764aSArtem Bityutskiy goto out_dump; 21591e51764aSArtem Bityutskiy } 21601e51764aSArtem Bityutskiy 21611e51764aSArtem Bityutskiy nlen = le16_to_cpu(dent->nlen); 21621e51764aSArtem Bityutskiy if (type == UBIFS_XENT_KEY) { 21631e51764aSArtem Bityutskiy fscki1->calc_xcnt += 1; 21641e51764aSArtem Bityutskiy fscki1->calc_xsz += CALC_DENT_SIZE(nlen); 21651e51764aSArtem Bityutskiy fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size); 21661e51764aSArtem Bityutskiy fscki1->calc_xnms += nlen; 21671e51764aSArtem Bityutskiy } else { 21681e51764aSArtem Bityutskiy fscki1->calc_sz += CALC_DENT_SIZE(nlen); 21691e51764aSArtem Bityutskiy if (dent->type == UBIFS_ITYPE_DIR) 21701e51764aSArtem Bityutskiy fscki1->calc_cnt += 1; 21711e51764aSArtem Bityutskiy } 21721e51764aSArtem Bityutskiy } 21731e51764aSArtem Bityutskiy 21741e51764aSArtem Bityutskiy out: 21751e51764aSArtem Bityutskiy kfree(node); 21761e51764aSArtem Bityutskiy return 0; 21771e51764aSArtem Bityutskiy 21781e51764aSArtem Bityutskiy out_dump: 21791e51764aSArtem Bityutskiy ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs); 21801e51764aSArtem Bityutskiy dbg_dump_node(c, node); 21811e51764aSArtem Bityutskiy out_free: 21821e51764aSArtem Bityutskiy kfree(node); 21831e51764aSArtem Bityutskiy return err; 21841e51764aSArtem Bityutskiy } 21851e51764aSArtem Bityutskiy 21861e51764aSArtem Bityutskiy /** 21871e51764aSArtem Bityutskiy * free_inodes - free RB-tree of inodes. 21881e51764aSArtem Bityutskiy * @fsckd: FS checking information 21891e51764aSArtem Bityutskiy */ 21901e51764aSArtem Bityutskiy static void free_inodes(struct fsck_data *fsckd) 21911e51764aSArtem Bityutskiy { 21921e51764aSArtem Bityutskiy struct rb_node *this = fsckd->inodes.rb_node; 21931e51764aSArtem Bityutskiy struct fsck_inode *fscki; 21941e51764aSArtem Bityutskiy 21951e51764aSArtem Bityutskiy while (this) { 21961e51764aSArtem Bityutskiy if (this->rb_left) 21971e51764aSArtem Bityutskiy this = this->rb_left; 21981e51764aSArtem Bityutskiy else if (this->rb_right) 21991e51764aSArtem Bityutskiy this = this->rb_right; 22001e51764aSArtem Bityutskiy else { 22011e51764aSArtem Bityutskiy fscki = rb_entry(this, struct fsck_inode, rb); 22021e51764aSArtem Bityutskiy this = rb_parent(this); 22031e51764aSArtem Bityutskiy if (this) { 22041e51764aSArtem Bityutskiy if (this->rb_left == &fscki->rb) 22051e51764aSArtem Bityutskiy this->rb_left = NULL; 22061e51764aSArtem Bityutskiy else 22071e51764aSArtem Bityutskiy this->rb_right = NULL; 22081e51764aSArtem Bityutskiy } 22091e51764aSArtem Bityutskiy kfree(fscki); 22101e51764aSArtem Bityutskiy } 22111e51764aSArtem Bityutskiy } 22121e51764aSArtem Bityutskiy } 22131e51764aSArtem Bityutskiy 22141e51764aSArtem Bityutskiy /** 22151e51764aSArtem Bityutskiy * check_inodes - checks all inodes. 22161e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 22171e51764aSArtem Bityutskiy * @fsckd: FS checking information 22181e51764aSArtem Bityutskiy * 22191e51764aSArtem Bityutskiy * This is a helper function for 'dbg_check_filesystem()' which walks the 22201e51764aSArtem Bityutskiy * RB-tree of inodes after the index scan has been finished, and checks that 22211e51764aSArtem Bityutskiy * inode nlink, size, etc are correct. Returns zero if inodes are fine, 22221e51764aSArtem Bityutskiy * %-EINVAL if not, and a negative error code in case of failure. 22231e51764aSArtem Bityutskiy */ 22241e51764aSArtem Bityutskiy static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd) 22251e51764aSArtem Bityutskiy { 22261e51764aSArtem Bityutskiy int n, err; 22271e51764aSArtem Bityutskiy union ubifs_key key; 22281e51764aSArtem Bityutskiy struct ubifs_znode *znode; 22291e51764aSArtem Bityutskiy struct ubifs_zbranch *zbr; 22301e51764aSArtem Bityutskiy struct ubifs_ino_node *ino; 22311e51764aSArtem Bityutskiy struct fsck_inode *fscki; 22321e51764aSArtem Bityutskiy struct rb_node *this = rb_first(&fsckd->inodes); 22331e51764aSArtem Bityutskiy 22341e51764aSArtem Bityutskiy while (this) { 22351e51764aSArtem Bityutskiy fscki = rb_entry(this, struct fsck_inode, rb); 22361e51764aSArtem Bityutskiy this = rb_next(this); 22371e51764aSArtem Bityutskiy 22381e51764aSArtem Bityutskiy if (S_ISDIR(fscki->mode)) { 22391e51764aSArtem Bityutskiy /* 22401e51764aSArtem Bityutskiy * Directories have to have exactly one reference (they 22411e51764aSArtem Bityutskiy * cannot have hardlinks), although root inode is an 22421e51764aSArtem Bityutskiy * exception. 22431e51764aSArtem Bityutskiy */ 22441e51764aSArtem Bityutskiy if (fscki->inum != UBIFS_ROOT_INO && 22451e51764aSArtem Bityutskiy fscki->references != 1) { 22461e51764aSArtem Bityutskiy ubifs_err("directory inode %lu has %d " 22471e51764aSArtem Bityutskiy "direntries which refer it, but " 2248e84461adSArtem Bityutskiy "should be 1", 2249e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 22501e51764aSArtem Bityutskiy fscki->references); 22511e51764aSArtem Bityutskiy goto out_dump; 22521e51764aSArtem Bityutskiy } 22531e51764aSArtem Bityutskiy if (fscki->inum == UBIFS_ROOT_INO && 22541e51764aSArtem Bityutskiy fscki->references != 0) { 22551e51764aSArtem Bityutskiy ubifs_err("root inode %lu has non-zero (%d) " 22561e51764aSArtem Bityutskiy "direntries which refer it", 2257e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 2258e84461adSArtem Bityutskiy fscki->references); 22591e51764aSArtem Bityutskiy goto out_dump; 22601e51764aSArtem Bityutskiy } 22611e51764aSArtem Bityutskiy if (fscki->calc_sz != fscki->size) { 22621e51764aSArtem Bityutskiy ubifs_err("directory inode %lu size is %lld, " 22631e51764aSArtem Bityutskiy "but calculated size is %lld", 2264e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 2265e84461adSArtem Bityutskiy fscki->size, fscki->calc_sz); 22661e51764aSArtem Bityutskiy goto out_dump; 22671e51764aSArtem Bityutskiy } 22681e51764aSArtem Bityutskiy if (fscki->calc_cnt != fscki->nlink) { 22691e51764aSArtem Bityutskiy ubifs_err("directory inode %lu nlink is %d, " 22701e51764aSArtem Bityutskiy "but calculated nlink is %d", 2271e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 2272e84461adSArtem Bityutskiy fscki->nlink, fscki->calc_cnt); 22731e51764aSArtem Bityutskiy goto out_dump; 22741e51764aSArtem Bityutskiy } 22751e51764aSArtem Bityutskiy } else { 22761e51764aSArtem Bityutskiy if (fscki->references != fscki->nlink) { 22771e51764aSArtem Bityutskiy ubifs_err("inode %lu nlink is %d, but " 2278e84461adSArtem Bityutskiy "calculated nlink is %d", 2279e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 22801e51764aSArtem Bityutskiy fscki->nlink, fscki->references); 22811e51764aSArtem Bityutskiy goto out_dump; 22821e51764aSArtem Bityutskiy } 22831e51764aSArtem Bityutskiy } 22841e51764aSArtem Bityutskiy if (fscki->xattr_sz != fscki->calc_xsz) { 22851e51764aSArtem Bityutskiy ubifs_err("inode %lu has xattr size %u, but " 22861e51764aSArtem Bityutskiy "calculated size is %lld", 2287e84461adSArtem Bityutskiy (unsigned long)fscki->inum, fscki->xattr_sz, 22881e51764aSArtem Bityutskiy fscki->calc_xsz); 22891e51764aSArtem Bityutskiy goto out_dump; 22901e51764aSArtem Bityutskiy } 22911e51764aSArtem Bityutskiy if (fscki->xattr_cnt != fscki->calc_xcnt) { 22921e51764aSArtem Bityutskiy ubifs_err("inode %lu has %u xattrs, but " 2293e84461adSArtem Bityutskiy "calculated count is %lld", 2294e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 22951e51764aSArtem Bityutskiy fscki->xattr_cnt, fscki->calc_xcnt); 22961e51764aSArtem Bityutskiy goto out_dump; 22971e51764aSArtem Bityutskiy } 22981e51764aSArtem Bityutskiy if (fscki->xattr_nms != fscki->calc_xnms) { 22991e51764aSArtem Bityutskiy ubifs_err("inode %lu has xattr names' size %u, but " 23001e51764aSArtem Bityutskiy "calculated names' size is %lld", 2301e84461adSArtem Bityutskiy (unsigned long)fscki->inum, fscki->xattr_nms, 23021e51764aSArtem Bityutskiy fscki->calc_xnms); 23031e51764aSArtem Bityutskiy goto out_dump; 23041e51764aSArtem Bityutskiy } 23051e51764aSArtem Bityutskiy } 23061e51764aSArtem Bityutskiy 23071e51764aSArtem Bityutskiy return 0; 23081e51764aSArtem Bityutskiy 23091e51764aSArtem Bityutskiy out_dump: 23101e51764aSArtem Bityutskiy /* Read the bad inode and dump it */ 23111e51764aSArtem Bityutskiy ino_key_init(c, &key, fscki->inum); 23121e51764aSArtem Bityutskiy err = ubifs_lookup_level0(c, &key, &znode, &n); 23131e51764aSArtem Bityutskiy if (!err) { 2314e84461adSArtem Bityutskiy ubifs_err("inode %lu not found in index", 2315e84461adSArtem Bityutskiy (unsigned long)fscki->inum); 23161e51764aSArtem Bityutskiy return -ENOENT; 23171e51764aSArtem Bityutskiy } else if (err < 0) { 23181e51764aSArtem Bityutskiy ubifs_err("error %d while looking up inode %lu", 2319e84461adSArtem Bityutskiy err, (unsigned long)fscki->inum); 23201e51764aSArtem Bityutskiy return err; 23211e51764aSArtem Bityutskiy } 23221e51764aSArtem Bityutskiy 23231e51764aSArtem Bityutskiy zbr = &znode->zbranch[n]; 23241e51764aSArtem Bityutskiy ino = kmalloc(zbr->len, GFP_NOFS); 23251e51764aSArtem Bityutskiy if (!ino) 23261e51764aSArtem Bityutskiy return -ENOMEM; 23271e51764aSArtem Bityutskiy 23281e51764aSArtem Bityutskiy err = ubifs_tnc_read_node(c, zbr, ino); 23291e51764aSArtem Bityutskiy if (err) { 23301e51764aSArtem Bityutskiy ubifs_err("cannot read inode node at LEB %d:%d, error %d", 23311e51764aSArtem Bityutskiy zbr->lnum, zbr->offs, err); 23321e51764aSArtem Bityutskiy kfree(ino); 23331e51764aSArtem Bityutskiy return err; 23341e51764aSArtem Bityutskiy } 23351e51764aSArtem Bityutskiy 23361e51764aSArtem Bityutskiy ubifs_msg("dump of the inode %lu sitting in LEB %d:%d", 2337e84461adSArtem Bityutskiy (unsigned long)fscki->inum, zbr->lnum, zbr->offs); 23381e51764aSArtem Bityutskiy dbg_dump_node(c, ino); 23391e51764aSArtem Bityutskiy kfree(ino); 23401e51764aSArtem Bityutskiy return -EINVAL; 23411e51764aSArtem Bityutskiy } 23421e51764aSArtem Bityutskiy 23431e51764aSArtem Bityutskiy /** 23441e51764aSArtem Bityutskiy * dbg_check_filesystem - check the file-system. 23451e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 23461e51764aSArtem Bityutskiy * 23471e51764aSArtem Bityutskiy * This function checks the file system, namely: 23481e51764aSArtem Bityutskiy * o makes sure that all leaf nodes exist and their CRCs are correct; 23491e51764aSArtem Bityutskiy * o makes sure inode nlink, size, xattr size/count are correct (for all 23501e51764aSArtem Bityutskiy * inodes). 23511e51764aSArtem Bityutskiy * 23521e51764aSArtem Bityutskiy * The function reads whole indexing tree and all nodes, so it is pretty 23531e51764aSArtem Bityutskiy * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if 23541e51764aSArtem Bityutskiy * not, and a negative error code in case of failure. 23551e51764aSArtem Bityutskiy */ 23561e51764aSArtem Bityutskiy int dbg_check_filesystem(struct ubifs_info *c) 23571e51764aSArtem Bityutskiy { 23581e51764aSArtem Bityutskiy int err; 23591e51764aSArtem Bityutskiy struct fsck_data fsckd; 23601e51764aSArtem Bityutskiy 23612b1844a8SArtem Bityutskiy if (!dbg_is_chk_fs(c)) 23621e51764aSArtem Bityutskiy return 0; 23631e51764aSArtem Bityutskiy 23641e51764aSArtem Bityutskiy fsckd.inodes = RB_ROOT; 23651e51764aSArtem Bityutskiy err = dbg_walk_index(c, check_leaf, NULL, &fsckd); 23661e51764aSArtem Bityutskiy if (err) 23671e51764aSArtem Bityutskiy goto out_free; 23681e51764aSArtem Bityutskiy 23691e51764aSArtem Bityutskiy err = check_inodes(c, &fsckd); 23701e51764aSArtem Bityutskiy if (err) 23711e51764aSArtem Bityutskiy goto out_free; 23721e51764aSArtem Bityutskiy 23731e51764aSArtem Bityutskiy free_inodes(&fsckd); 23741e51764aSArtem Bityutskiy return 0; 23751e51764aSArtem Bityutskiy 23761e51764aSArtem Bityutskiy out_free: 23771e51764aSArtem Bityutskiy ubifs_err("file-system check failed with error %d", err); 23781e51764aSArtem Bityutskiy dump_stack(); 23791e51764aSArtem Bityutskiy free_inodes(&fsckd); 23801e51764aSArtem Bityutskiy return err; 23811e51764aSArtem Bityutskiy } 23821e51764aSArtem Bityutskiy 23833bb66b47SArtem Bityutskiy /** 23843bb66b47SArtem Bityutskiy * dbg_check_data_nodes_order - check that list of data nodes is sorted. 23853bb66b47SArtem Bityutskiy * @c: UBIFS file-system description object 23863bb66b47SArtem Bityutskiy * @head: the list of nodes ('struct ubifs_scan_node' objects) 23873bb66b47SArtem Bityutskiy * 23883bb66b47SArtem Bityutskiy * This function returns zero if the list of data nodes is sorted correctly, 23893bb66b47SArtem Bityutskiy * and %-EINVAL if not. 23903bb66b47SArtem Bityutskiy */ 23913bb66b47SArtem Bityutskiy int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head) 23923bb66b47SArtem Bityutskiy { 23933bb66b47SArtem Bityutskiy struct list_head *cur; 23943bb66b47SArtem Bityutskiy struct ubifs_scan_node *sa, *sb; 23953bb66b47SArtem Bityutskiy 23962b1844a8SArtem Bityutskiy if (!dbg_is_chk_gen(c)) 23973bb66b47SArtem Bityutskiy return 0; 23983bb66b47SArtem Bityutskiy 23993bb66b47SArtem Bityutskiy for (cur = head->next; cur->next != head; cur = cur->next) { 24003bb66b47SArtem Bityutskiy ino_t inuma, inumb; 24013bb66b47SArtem Bityutskiy uint32_t blka, blkb; 24023bb66b47SArtem Bityutskiy 24033bb66b47SArtem Bityutskiy cond_resched(); 24043bb66b47SArtem Bityutskiy sa = container_of(cur, struct ubifs_scan_node, list); 24053bb66b47SArtem Bityutskiy sb = container_of(cur->next, struct ubifs_scan_node, list); 24063bb66b47SArtem Bityutskiy 24073bb66b47SArtem Bityutskiy if (sa->type != UBIFS_DATA_NODE) { 24083bb66b47SArtem Bityutskiy ubifs_err("bad node type %d", sa->type); 24093bb66b47SArtem Bityutskiy dbg_dump_node(c, sa->node); 24103bb66b47SArtem Bityutskiy return -EINVAL; 24113bb66b47SArtem Bityutskiy } 24123bb66b47SArtem Bityutskiy if (sb->type != UBIFS_DATA_NODE) { 24133bb66b47SArtem Bityutskiy ubifs_err("bad node type %d", sb->type); 24143bb66b47SArtem Bityutskiy dbg_dump_node(c, sb->node); 24153bb66b47SArtem Bityutskiy return -EINVAL; 24163bb66b47SArtem Bityutskiy } 24173bb66b47SArtem Bityutskiy 24183bb66b47SArtem Bityutskiy inuma = key_inum(c, &sa->key); 24193bb66b47SArtem Bityutskiy inumb = key_inum(c, &sb->key); 24203bb66b47SArtem Bityutskiy 24213bb66b47SArtem Bityutskiy if (inuma < inumb) 24223bb66b47SArtem Bityutskiy continue; 24233bb66b47SArtem Bityutskiy if (inuma > inumb) { 24243bb66b47SArtem Bityutskiy ubifs_err("larger inum %lu goes before inum %lu", 24253bb66b47SArtem Bityutskiy (unsigned long)inuma, (unsigned long)inumb); 24263bb66b47SArtem Bityutskiy goto error_dump; 24273bb66b47SArtem Bityutskiy } 24283bb66b47SArtem Bityutskiy 24293bb66b47SArtem Bityutskiy blka = key_block(c, &sa->key); 24303bb66b47SArtem Bityutskiy blkb = key_block(c, &sb->key); 24313bb66b47SArtem Bityutskiy 24323bb66b47SArtem Bityutskiy if (blka > blkb) { 24333bb66b47SArtem Bityutskiy ubifs_err("larger block %u goes before %u", blka, blkb); 24343bb66b47SArtem Bityutskiy goto error_dump; 24353bb66b47SArtem Bityutskiy } 24363bb66b47SArtem Bityutskiy if (blka == blkb) { 24373bb66b47SArtem Bityutskiy ubifs_err("two data nodes for the same block"); 24383bb66b47SArtem Bityutskiy goto error_dump; 24393bb66b47SArtem Bityutskiy } 24403bb66b47SArtem Bityutskiy } 24413bb66b47SArtem Bityutskiy 24423bb66b47SArtem Bityutskiy return 0; 24433bb66b47SArtem Bityutskiy 24443bb66b47SArtem Bityutskiy error_dump: 24453bb66b47SArtem Bityutskiy dbg_dump_node(c, sa->node); 24463bb66b47SArtem Bityutskiy dbg_dump_node(c, sb->node); 24473bb66b47SArtem Bityutskiy return -EINVAL; 24483bb66b47SArtem Bityutskiy } 24493bb66b47SArtem Bityutskiy 24503bb66b47SArtem Bityutskiy /** 24513bb66b47SArtem Bityutskiy * dbg_check_nondata_nodes_order - check that list of data nodes is sorted. 24523bb66b47SArtem Bityutskiy * @c: UBIFS file-system description object 24533bb66b47SArtem Bityutskiy * @head: the list of nodes ('struct ubifs_scan_node' objects) 24543bb66b47SArtem Bityutskiy * 24553bb66b47SArtem Bityutskiy * This function returns zero if the list of non-data nodes is sorted correctly, 24563bb66b47SArtem Bityutskiy * and %-EINVAL if not. 24573bb66b47SArtem Bityutskiy */ 24583bb66b47SArtem Bityutskiy int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head) 24593bb66b47SArtem Bityutskiy { 24603bb66b47SArtem Bityutskiy struct list_head *cur; 24613bb66b47SArtem Bityutskiy struct ubifs_scan_node *sa, *sb; 24623bb66b47SArtem Bityutskiy 24632b1844a8SArtem Bityutskiy if (!dbg_is_chk_gen(c)) 24643bb66b47SArtem Bityutskiy return 0; 24653bb66b47SArtem Bityutskiy 24663bb66b47SArtem Bityutskiy for (cur = head->next; cur->next != head; cur = cur->next) { 24673bb66b47SArtem Bityutskiy ino_t inuma, inumb; 24683bb66b47SArtem Bityutskiy uint32_t hasha, hashb; 24693bb66b47SArtem Bityutskiy 24703bb66b47SArtem Bityutskiy cond_resched(); 24713bb66b47SArtem Bityutskiy sa = container_of(cur, struct ubifs_scan_node, list); 24723bb66b47SArtem Bityutskiy sb = container_of(cur->next, struct ubifs_scan_node, list); 24733bb66b47SArtem Bityutskiy 24743bb66b47SArtem Bityutskiy if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE && 24753bb66b47SArtem Bityutskiy sa->type != UBIFS_XENT_NODE) { 24763bb66b47SArtem Bityutskiy ubifs_err("bad node type %d", sa->type); 24773bb66b47SArtem Bityutskiy dbg_dump_node(c, sa->node); 24783bb66b47SArtem Bityutskiy return -EINVAL; 24793bb66b47SArtem Bityutskiy } 24803bb66b47SArtem Bityutskiy if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE && 24813bb66b47SArtem Bityutskiy sa->type != UBIFS_XENT_NODE) { 24823bb66b47SArtem Bityutskiy ubifs_err("bad node type %d", sb->type); 24833bb66b47SArtem Bityutskiy dbg_dump_node(c, sb->node); 24843bb66b47SArtem Bityutskiy return -EINVAL; 24853bb66b47SArtem Bityutskiy } 24863bb66b47SArtem Bityutskiy 24873bb66b47SArtem Bityutskiy if (sa->type != UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) { 24883bb66b47SArtem Bityutskiy ubifs_err("non-inode node goes before inode node"); 24893bb66b47SArtem Bityutskiy goto error_dump; 24903bb66b47SArtem Bityutskiy } 24913bb66b47SArtem Bityutskiy 24923bb66b47SArtem Bityutskiy if (sa->type == UBIFS_INO_NODE && sb->type != UBIFS_INO_NODE) 24933bb66b47SArtem Bityutskiy continue; 24943bb66b47SArtem Bityutskiy 24953bb66b47SArtem Bityutskiy if (sa->type == UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) { 24963bb66b47SArtem Bityutskiy /* Inode nodes are sorted in descending size order */ 24973bb66b47SArtem Bityutskiy if (sa->len < sb->len) { 24983bb66b47SArtem Bityutskiy ubifs_err("smaller inode node goes first"); 24993bb66b47SArtem Bityutskiy goto error_dump; 25003bb66b47SArtem Bityutskiy } 25013bb66b47SArtem Bityutskiy continue; 25023bb66b47SArtem Bityutskiy } 25033bb66b47SArtem Bityutskiy 25043bb66b47SArtem Bityutskiy /* 25053bb66b47SArtem Bityutskiy * This is either a dentry or xentry, which should be sorted in 25063bb66b47SArtem Bityutskiy * ascending (parent ino, hash) order. 25073bb66b47SArtem Bityutskiy */ 25083bb66b47SArtem Bityutskiy inuma = key_inum(c, &sa->key); 25093bb66b47SArtem Bityutskiy inumb = key_inum(c, &sb->key); 25103bb66b47SArtem Bityutskiy 25113bb66b47SArtem Bityutskiy if (inuma < inumb) 25123bb66b47SArtem Bityutskiy continue; 25133bb66b47SArtem Bityutskiy if (inuma > inumb) { 25143bb66b47SArtem Bityutskiy ubifs_err("larger inum %lu goes before inum %lu", 25153bb66b47SArtem Bityutskiy (unsigned long)inuma, (unsigned long)inumb); 25163bb66b47SArtem Bityutskiy goto error_dump; 25173bb66b47SArtem Bityutskiy } 25183bb66b47SArtem Bityutskiy 25193bb66b47SArtem Bityutskiy hasha = key_block(c, &sa->key); 25203bb66b47SArtem Bityutskiy hashb = key_block(c, &sb->key); 25213bb66b47SArtem Bityutskiy 25223bb66b47SArtem Bityutskiy if (hasha > hashb) { 2523c4361570SArtem Bityutskiy ubifs_err("larger hash %u goes before %u", 2524c4361570SArtem Bityutskiy hasha, hashb); 25253bb66b47SArtem Bityutskiy goto error_dump; 25263bb66b47SArtem Bityutskiy } 25273bb66b47SArtem Bityutskiy } 25283bb66b47SArtem Bityutskiy 25293bb66b47SArtem Bityutskiy return 0; 25303bb66b47SArtem Bityutskiy 25313bb66b47SArtem Bityutskiy error_dump: 25323bb66b47SArtem Bityutskiy ubifs_msg("dumping first node"); 25333bb66b47SArtem Bityutskiy dbg_dump_node(c, sa->node); 25343bb66b47SArtem Bityutskiy ubifs_msg("dumping second node"); 25353bb66b47SArtem Bityutskiy dbg_dump_node(c, sb->node); 25363bb66b47SArtem Bityutskiy return -EINVAL; 25373bb66b47SArtem Bityutskiy return 0; 25383bb66b47SArtem Bityutskiy } 25393bb66b47SArtem Bityutskiy 25401e51764aSArtem Bityutskiy /* Failure mode for recovery testing */ 25411e51764aSArtem Bityutskiy 25421e51764aSArtem Bityutskiy #define chance(n, d) (simple_rand() <= (n) * 32768LL / (d)) 25431e51764aSArtem Bityutskiy 25441e51764aSArtem Bityutskiy struct failure_mode_info { 25451e51764aSArtem Bityutskiy struct list_head list; 25461e51764aSArtem Bityutskiy struct ubifs_info *c; 25471e51764aSArtem Bityutskiy }; 25481e51764aSArtem Bityutskiy 25491e51764aSArtem Bityutskiy static LIST_HEAD(fmi_list); 25501e51764aSArtem Bityutskiy static DEFINE_SPINLOCK(fmi_lock); 25511e51764aSArtem Bityutskiy 25521e51764aSArtem Bityutskiy static unsigned int next; 25531e51764aSArtem Bityutskiy 25541e51764aSArtem Bityutskiy static int simple_rand(void) 25551e51764aSArtem Bityutskiy { 25561e51764aSArtem Bityutskiy if (next == 0) 25571e51764aSArtem Bityutskiy next = current->pid; 25581e51764aSArtem Bityutskiy next = next * 1103515245 + 12345; 25591e51764aSArtem Bityutskiy return (next >> 16) & 32767; 25601e51764aSArtem Bityutskiy } 25611e51764aSArtem Bityutskiy 256217c2f9f8SArtem Bityutskiy static void failure_mode_init(struct ubifs_info *c) 25631e51764aSArtem Bityutskiy { 25641e51764aSArtem Bityutskiy struct failure_mode_info *fmi; 25651e51764aSArtem Bityutskiy 25661e51764aSArtem Bityutskiy fmi = kmalloc(sizeof(struct failure_mode_info), GFP_NOFS); 25671e51764aSArtem Bityutskiy if (!fmi) { 2568552ff317SArtem Bityutskiy ubifs_err("Failed to register failure mode - no memory"); 25691e51764aSArtem Bityutskiy return; 25701e51764aSArtem Bityutskiy } 25711e51764aSArtem Bityutskiy fmi->c = c; 25721e51764aSArtem Bityutskiy spin_lock(&fmi_lock); 25731e51764aSArtem Bityutskiy list_add_tail(&fmi->list, &fmi_list); 25741e51764aSArtem Bityutskiy spin_unlock(&fmi_lock); 25751e51764aSArtem Bityutskiy } 25761e51764aSArtem Bityutskiy 257717c2f9f8SArtem Bityutskiy static void failure_mode_exit(struct ubifs_info *c) 25781e51764aSArtem Bityutskiy { 25791e51764aSArtem Bityutskiy struct failure_mode_info *fmi, *tmp; 25801e51764aSArtem Bityutskiy 25811e51764aSArtem Bityutskiy spin_lock(&fmi_lock); 25821e51764aSArtem Bityutskiy list_for_each_entry_safe(fmi, tmp, &fmi_list, list) 25831e51764aSArtem Bityutskiy if (fmi->c == c) { 25841e51764aSArtem Bityutskiy list_del(&fmi->list); 25851e51764aSArtem Bityutskiy kfree(fmi); 25861e51764aSArtem Bityutskiy } 25871e51764aSArtem Bityutskiy spin_unlock(&fmi_lock); 25881e51764aSArtem Bityutskiy } 25891e51764aSArtem Bityutskiy 25901e51764aSArtem Bityutskiy static struct ubifs_info *dbg_find_info(struct ubi_volume_desc *desc) 25911e51764aSArtem Bityutskiy { 25921e51764aSArtem Bityutskiy struct failure_mode_info *fmi; 25931e51764aSArtem Bityutskiy 25941e51764aSArtem Bityutskiy spin_lock(&fmi_lock); 25951e51764aSArtem Bityutskiy list_for_each_entry(fmi, &fmi_list, list) 25961e51764aSArtem Bityutskiy if (fmi->c->ubi == desc) { 25971e51764aSArtem Bityutskiy struct ubifs_info *c = fmi->c; 25981e51764aSArtem Bityutskiy 25991e51764aSArtem Bityutskiy spin_unlock(&fmi_lock); 26001e51764aSArtem Bityutskiy return c; 26011e51764aSArtem Bityutskiy } 26021e51764aSArtem Bityutskiy spin_unlock(&fmi_lock); 26031e51764aSArtem Bityutskiy return NULL; 26041e51764aSArtem Bityutskiy } 26051e51764aSArtem Bityutskiy 26061e51764aSArtem Bityutskiy static int in_failure_mode(struct ubi_volume_desc *desc) 26071e51764aSArtem Bityutskiy { 26081e51764aSArtem Bityutskiy struct ubifs_info *c = dbg_find_info(desc); 26091e51764aSArtem Bityutskiy 26102b1844a8SArtem Bityutskiy if (c && dbg_is_tst_rcvry(c)) 261117c2f9f8SArtem Bityutskiy return c->dbg->failure_mode; 26121e51764aSArtem Bityutskiy return 0; 26131e51764aSArtem Bityutskiy } 26141e51764aSArtem Bityutskiy 26151e51764aSArtem Bityutskiy static int do_fail(struct ubi_volume_desc *desc, int lnum, int write) 26161e51764aSArtem Bityutskiy { 26171e51764aSArtem Bityutskiy struct ubifs_info *c = dbg_find_info(desc); 261817c2f9f8SArtem Bityutskiy struct ubifs_debug_info *d; 26191e51764aSArtem Bityutskiy 26202b1844a8SArtem Bityutskiy if (!c || !dbg_is_tst_rcvry(c)) 26211e51764aSArtem Bityutskiy return 0; 262217c2f9f8SArtem Bityutskiy d = c->dbg; 262317c2f9f8SArtem Bityutskiy if (d->failure_mode) 26241e51764aSArtem Bityutskiy return 1; 262517c2f9f8SArtem Bityutskiy if (!d->fail_cnt) { 26261e51764aSArtem Bityutskiy /* First call - decide delay to failure */ 26271e51764aSArtem Bityutskiy if (chance(1, 2)) { 26281e51764aSArtem Bityutskiy unsigned int delay = 1 << (simple_rand() >> 11); 26291e51764aSArtem Bityutskiy 26301e51764aSArtem Bityutskiy if (chance(1, 2)) { 263117c2f9f8SArtem Bityutskiy d->fail_delay = 1; 263217c2f9f8SArtem Bityutskiy d->fail_timeout = jiffies + 26331e51764aSArtem Bityutskiy msecs_to_jiffies(delay); 263424a4f800SArtem Bityutskiy ubifs_warn("failing after %ums", delay); 26351e51764aSArtem Bityutskiy } else { 263617c2f9f8SArtem Bityutskiy d->fail_delay = 2; 263717c2f9f8SArtem Bityutskiy d->fail_cnt_max = delay; 263824a4f800SArtem Bityutskiy ubifs_warn("failing after %u calls", delay); 26391e51764aSArtem Bityutskiy } 26401e51764aSArtem Bityutskiy } 264117c2f9f8SArtem Bityutskiy d->fail_cnt += 1; 26421e51764aSArtem Bityutskiy } 26431e51764aSArtem Bityutskiy /* Determine if failure delay has expired */ 264417c2f9f8SArtem Bityutskiy if (d->fail_delay == 1) { 264517c2f9f8SArtem Bityutskiy if (time_before(jiffies, d->fail_timeout)) 26461e51764aSArtem Bityutskiy return 0; 264717c2f9f8SArtem Bityutskiy } else if (d->fail_delay == 2) 264817c2f9f8SArtem Bityutskiy if (d->fail_cnt++ < d->fail_cnt_max) 26491e51764aSArtem Bityutskiy return 0; 26501e51764aSArtem Bityutskiy if (lnum == UBIFS_SB_LNUM) { 26511e51764aSArtem Bityutskiy if (write) { 26521e51764aSArtem Bityutskiy if (chance(1, 2)) 26531e51764aSArtem Bityutskiy return 0; 26541e51764aSArtem Bityutskiy } else if (chance(19, 20)) 26551e51764aSArtem Bityutskiy return 0; 265624a4f800SArtem Bityutskiy ubifs_warn("failing in super block LEB %d", lnum); 26571e51764aSArtem Bityutskiy } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) { 26581e51764aSArtem Bityutskiy if (chance(19, 20)) 26591e51764aSArtem Bityutskiy return 0; 266024a4f800SArtem Bityutskiy ubifs_warn("failing in master LEB %d", lnum); 26611e51764aSArtem Bityutskiy } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) { 26621e51764aSArtem Bityutskiy if (write) { 26631e51764aSArtem Bityutskiy if (chance(99, 100)) 26641e51764aSArtem Bityutskiy return 0; 26651e51764aSArtem Bityutskiy } else if (chance(399, 400)) 26661e51764aSArtem Bityutskiy return 0; 266724a4f800SArtem Bityutskiy ubifs_warn("failing in log LEB %d", lnum); 26681e51764aSArtem Bityutskiy } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) { 26691e51764aSArtem Bityutskiy if (write) { 26701e51764aSArtem Bityutskiy if (chance(7, 8)) 26711e51764aSArtem Bityutskiy return 0; 26721e51764aSArtem Bityutskiy } else if (chance(19, 20)) 26731e51764aSArtem Bityutskiy return 0; 267424a4f800SArtem Bityutskiy ubifs_warn("failing in LPT LEB %d", lnum); 26751e51764aSArtem Bityutskiy } else if (lnum >= c->orph_first && lnum <= c->orph_last) { 26761e51764aSArtem Bityutskiy if (write) { 26771e51764aSArtem Bityutskiy if (chance(1, 2)) 26781e51764aSArtem Bityutskiy return 0; 26791e51764aSArtem Bityutskiy } else if (chance(9, 10)) 26801e51764aSArtem Bityutskiy return 0; 268124a4f800SArtem Bityutskiy ubifs_warn("failing in orphan LEB %d", lnum); 26821e51764aSArtem Bityutskiy } else if (lnum == c->ihead_lnum) { 26831e51764aSArtem Bityutskiy if (chance(99, 100)) 26841e51764aSArtem Bityutskiy return 0; 268524a4f800SArtem Bityutskiy ubifs_warn("failing in index head LEB %d", lnum); 26861e51764aSArtem Bityutskiy } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) { 26871e51764aSArtem Bityutskiy if (chance(9, 10)) 26881e51764aSArtem Bityutskiy return 0; 268924a4f800SArtem Bityutskiy ubifs_warn("failing in GC head LEB %d", lnum); 26901e51764aSArtem Bityutskiy } else if (write && !RB_EMPTY_ROOT(&c->buds) && 26911e51764aSArtem Bityutskiy !ubifs_search_bud(c, lnum)) { 26921e51764aSArtem Bityutskiy if (chance(19, 20)) 26931e51764aSArtem Bityutskiy return 0; 269424a4f800SArtem Bityutskiy ubifs_warn("failing in non-bud LEB %d", lnum); 26951e51764aSArtem Bityutskiy } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND || 26961e51764aSArtem Bityutskiy c->cmt_state == COMMIT_RUNNING_REQUIRED) { 26971e51764aSArtem Bityutskiy if (chance(999, 1000)) 26981e51764aSArtem Bityutskiy return 0; 269924a4f800SArtem Bityutskiy ubifs_warn("failing in bud LEB %d commit running", lnum); 27001e51764aSArtem Bityutskiy } else { 27011e51764aSArtem Bityutskiy if (chance(9999, 10000)) 27021e51764aSArtem Bityutskiy return 0; 270324a4f800SArtem Bityutskiy ubifs_warn("failing in bud LEB %d commit not running", lnum); 27041e51764aSArtem Bityutskiy } 270524a4f800SArtem Bityutskiy 270617c2f9f8SArtem Bityutskiy d->failure_mode = 1; 27071e51764aSArtem Bityutskiy dump_stack(); 27081e51764aSArtem Bityutskiy return 1; 27091e51764aSArtem Bityutskiy } 27101e51764aSArtem Bityutskiy 27111e51764aSArtem Bityutskiy static void cut_data(const void *buf, int len) 27121e51764aSArtem Bityutskiy { 27131e51764aSArtem Bityutskiy int flen, i; 27141e51764aSArtem Bityutskiy unsigned char *p = (void *)buf; 27151e51764aSArtem Bityutskiy 27161e51764aSArtem Bityutskiy flen = (len * (long long)simple_rand()) >> 15; 27171e51764aSArtem Bityutskiy for (i = flen; i < len; i++) 27181e51764aSArtem Bityutskiy p[i] = 0xff; 27191e51764aSArtem Bityutskiy } 27201e51764aSArtem Bityutskiy 27211e51764aSArtem Bityutskiy int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset, 27221e51764aSArtem Bityutskiy int len, int check) 27231e51764aSArtem Bityutskiy { 27241e51764aSArtem Bityutskiy if (in_failure_mode(desc)) 27251a29af8bSArtem Bityutskiy return -EROFS; 27261e51764aSArtem Bityutskiy return ubi_leb_read(desc, lnum, buf, offset, len, check); 27271e51764aSArtem Bityutskiy } 27281e51764aSArtem Bityutskiy 27291e51764aSArtem Bityutskiy int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf, 27301e51764aSArtem Bityutskiy int offset, int len, int dtype) 27311e51764aSArtem Bityutskiy { 273216dfd804SAdrian Hunter int err, failing; 27331e51764aSArtem Bityutskiy 27341e51764aSArtem Bityutskiy if (in_failure_mode(desc)) 27351a29af8bSArtem Bityutskiy return -EROFS; 273616dfd804SAdrian Hunter failing = do_fail(desc, lnum, 1); 273716dfd804SAdrian Hunter if (failing) 27381e51764aSArtem Bityutskiy cut_data(buf, len); 27391e51764aSArtem Bityutskiy err = ubi_leb_write(desc, lnum, buf, offset, len, dtype); 27401e51764aSArtem Bityutskiy if (err) 27411e51764aSArtem Bityutskiy return err; 274216dfd804SAdrian Hunter if (failing) 27431a29af8bSArtem Bityutskiy return -EROFS; 27441e51764aSArtem Bityutskiy return 0; 27451e51764aSArtem Bityutskiy } 27461e51764aSArtem Bityutskiy 27471e51764aSArtem Bityutskiy int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf, 27481e51764aSArtem Bityutskiy int len, int dtype) 27491e51764aSArtem Bityutskiy { 27501e51764aSArtem Bityutskiy int err; 27511e51764aSArtem Bityutskiy 27521e51764aSArtem Bityutskiy if (do_fail(desc, lnum, 1)) 27531a29af8bSArtem Bityutskiy return -EROFS; 27541e51764aSArtem Bityutskiy err = ubi_leb_change(desc, lnum, buf, len, dtype); 27551e51764aSArtem Bityutskiy if (err) 27561e51764aSArtem Bityutskiy return err; 27571e51764aSArtem Bityutskiy if (do_fail(desc, lnum, 1)) 27581a29af8bSArtem Bityutskiy return -EROFS; 27591e51764aSArtem Bityutskiy return 0; 27601e51764aSArtem Bityutskiy } 27611e51764aSArtem Bityutskiy 27621e51764aSArtem Bityutskiy int dbg_leb_erase(struct ubi_volume_desc *desc, int lnum) 27631e51764aSArtem Bityutskiy { 27641e51764aSArtem Bityutskiy int err; 27651e51764aSArtem Bityutskiy 27661e51764aSArtem Bityutskiy if (do_fail(desc, lnum, 0)) 27671a29af8bSArtem Bityutskiy return -EROFS; 27681e51764aSArtem Bityutskiy err = ubi_leb_erase(desc, lnum); 27691e51764aSArtem Bityutskiy if (err) 27701e51764aSArtem Bityutskiy return err; 27711e51764aSArtem Bityutskiy if (do_fail(desc, lnum, 0)) 27721a29af8bSArtem Bityutskiy return -EROFS; 27731e51764aSArtem Bityutskiy return 0; 27741e51764aSArtem Bityutskiy } 27751e51764aSArtem Bityutskiy 27761e51764aSArtem Bityutskiy int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum) 27771e51764aSArtem Bityutskiy { 27781e51764aSArtem Bityutskiy int err; 27791e51764aSArtem Bityutskiy 27801e51764aSArtem Bityutskiy if (do_fail(desc, lnum, 0)) 27811a29af8bSArtem Bityutskiy return -EROFS; 27821e51764aSArtem Bityutskiy err = ubi_leb_unmap(desc, lnum); 27831e51764aSArtem Bityutskiy if (err) 27841e51764aSArtem Bityutskiy return err; 27851e51764aSArtem Bityutskiy if (do_fail(desc, lnum, 0)) 27861a29af8bSArtem Bityutskiy return -EROFS; 27871e51764aSArtem Bityutskiy return 0; 27881e51764aSArtem Bityutskiy } 27891e51764aSArtem Bityutskiy 27901e51764aSArtem Bityutskiy int dbg_is_mapped(struct ubi_volume_desc *desc, int lnum) 27911e51764aSArtem Bityutskiy { 27921e51764aSArtem Bityutskiy if (in_failure_mode(desc)) 27931a29af8bSArtem Bityutskiy return -EROFS; 27941e51764aSArtem Bityutskiy return ubi_is_mapped(desc, lnum); 27951e51764aSArtem Bityutskiy } 27961e51764aSArtem Bityutskiy 27971e51764aSArtem Bityutskiy int dbg_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype) 27981e51764aSArtem Bityutskiy { 27991e51764aSArtem Bityutskiy int err; 28001e51764aSArtem Bityutskiy 28011e51764aSArtem Bityutskiy if (do_fail(desc, lnum, 0)) 28021a29af8bSArtem Bityutskiy return -EROFS; 28031e51764aSArtem Bityutskiy err = ubi_leb_map(desc, lnum, dtype); 28041e51764aSArtem Bityutskiy if (err) 28051e51764aSArtem Bityutskiy return err; 28061e51764aSArtem Bityutskiy if (do_fail(desc, lnum, 0)) 28071a29af8bSArtem Bityutskiy return -EROFS; 28081e51764aSArtem Bityutskiy return 0; 28091e51764aSArtem Bityutskiy } 28101e51764aSArtem Bityutskiy 2811552ff317SArtem Bityutskiy /* 2812552ff317SArtem Bityutskiy * Root directory for UBIFS stuff in debugfs. Contains sub-directories which 2813552ff317SArtem Bityutskiy * contain the stuff specific to particular file-system mounts. 2814552ff317SArtem Bityutskiy */ 281584abf972SArtem Bityutskiy static struct dentry *dfs_rootdir; 2816552ff317SArtem Bityutskiy 2817*7dae997dSArtem Bityutskiy static int dfs_file_open(struct inode *inode, struct file *file) 2818552ff317SArtem Bityutskiy { 2819552ff317SArtem Bityutskiy file->private_data = inode->i_private; 28201bbfc848SArtem Bityutskiy return nonseekable_open(inode, file); 2821552ff317SArtem Bityutskiy } 2822552ff317SArtem Bityutskiy 282381e79d38SArtem Bityutskiy static ssize_t dfs_file_read(struct file *file, char __user *u, size_t count, 282481e79d38SArtem Bityutskiy loff_t *ppos) 282581e79d38SArtem Bityutskiy { 282681e79d38SArtem Bityutskiy struct dentry *dent = file->f_path.dentry; 282781e79d38SArtem Bityutskiy struct ubifs_info *c = file->private_data; 282881e79d38SArtem Bityutskiy struct ubifs_debug_info *d = c->dbg; 282981e79d38SArtem Bityutskiy char buf[3]; 283081e79d38SArtem Bityutskiy int val; 283181e79d38SArtem Bityutskiy 283281e79d38SArtem Bityutskiy if (dent == d->dfs_chk_gen) 283381e79d38SArtem Bityutskiy val = d->chk_gen; 283481e79d38SArtem Bityutskiy else if (dent == d->dfs_chk_index) 283581e79d38SArtem Bityutskiy val = d->chk_index; 283681e79d38SArtem Bityutskiy else if (dent == d->dfs_chk_orph) 283781e79d38SArtem Bityutskiy val = d->chk_orph; 283881e79d38SArtem Bityutskiy else if (dent == d->dfs_chk_lprops) 283981e79d38SArtem Bityutskiy val = d->chk_lprops; 284081e79d38SArtem Bityutskiy else if (dent == d->dfs_chk_fs) 284181e79d38SArtem Bityutskiy val = d->chk_fs; 284281e79d38SArtem Bityutskiy else if (dent == d->dfs_tst_rcvry) 284381e79d38SArtem Bityutskiy val = d->tst_rcvry; 284481e79d38SArtem Bityutskiy else 284581e79d38SArtem Bityutskiy return -EINVAL; 284681e79d38SArtem Bityutskiy 284781e79d38SArtem Bityutskiy if (val) 284881e79d38SArtem Bityutskiy buf[0] = '1'; 284981e79d38SArtem Bityutskiy else 285081e79d38SArtem Bityutskiy buf[0] = '0'; 285181e79d38SArtem Bityutskiy buf[1] = '\n'; 285281e79d38SArtem Bityutskiy buf[2] = 0x00; 285381e79d38SArtem Bityutskiy 285481e79d38SArtem Bityutskiy return simple_read_from_buffer(u, count, ppos, buf, 2); 285581e79d38SArtem Bityutskiy } 285681e79d38SArtem Bityutskiy 285781e79d38SArtem Bityutskiy static ssize_t dfs_file_write(struct file *file, const char __user *u, 2858552ff317SArtem Bityutskiy size_t count, loff_t *ppos) 2859552ff317SArtem Bityutskiy { 2860552ff317SArtem Bityutskiy struct ubifs_info *c = file->private_data; 2861552ff317SArtem Bityutskiy struct ubifs_debug_info *d = c->dbg; 286281e79d38SArtem Bityutskiy struct dentry *dent = file->f_path.dentry; 286381e79d38SArtem Bityutskiy size_t buf_size; 286481e79d38SArtem Bityutskiy char buf[8]; 286581e79d38SArtem Bityutskiy int val; 2866552ff317SArtem Bityutskiy 286781e79d38SArtem Bityutskiy /* 286824a4f800SArtem Bityutskiy * TODO: this is racy - the file-system might have already been 286924a4f800SArtem Bityutskiy * unmounted and we'd oops in this case. The plan is to fix it with 287024a4f800SArtem Bityutskiy * help of 'iterate_supers_type()' which we should have in v3.0: when 287124a4f800SArtem Bityutskiy * a debugfs opened, we rember FS's UUID in file->private_data. Then 287224a4f800SArtem Bityutskiy * whenever we access the FS via a debugfs file, we iterate all UBIFS 287324a4f800SArtem Bityutskiy * superblocks and fine the one with the same UUID, and take the 287424a4f800SArtem Bityutskiy * locking right. 287524a4f800SArtem Bityutskiy * 287624a4f800SArtem Bityutskiy * The other way to go suggested by Al Viro is to create a separate 287724a4f800SArtem Bityutskiy * 'ubifs-debug' file-system instead. 287881e79d38SArtem Bityutskiy */ 287981e79d38SArtem Bityutskiy if (file->f_path.dentry == d->dfs_dump_lprops) { 2880552ff317SArtem Bityutskiy dbg_dump_lprops(c); 288181e79d38SArtem Bityutskiy return count; 288281e79d38SArtem Bityutskiy } 288381e79d38SArtem Bityutskiy if (file->f_path.dentry == d->dfs_dump_budg) { 2884f1bd66afSArtem Bityutskiy dbg_dump_budg(c, &c->bi); 288581e79d38SArtem Bityutskiy return count; 288681e79d38SArtem Bityutskiy } 288781e79d38SArtem Bityutskiy if (file->f_path.dentry == d->dfs_dump_tnc) { 2888552ff317SArtem Bityutskiy mutex_lock(&c->tnc_mutex); 2889552ff317SArtem Bityutskiy dbg_dump_tnc(c); 2890552ff317SArtem Bityutskiy mutex_unlock(&c->tnc_mutex); 289181e79d38SArtem Bityutskiy return count; 289281e79d38SArtem Bityutskiy } 289381e79d38SArtem Bityutskiy 289481e79d38SArtem Bityutskiy buf_size = min_t(size_t, count, (sizeof(buf) - 1)); 289581e79d38SArtem Bityutskiy if (copy_from_user(buf, u, buf_size)) 289681e79d38SArtem Bityutskiy return -EFAULT; 289781e79d38SArtem Bityutskiy 289881e79d38SArtem Bityutskiy if (buf[0] == '1') 289981e79d38SArtem Bityutskiy val = 1; 290081e79d38SArtem Bityutskiy else if (buf[0] == '0') 290181e79d38SArtem Bityutskiy val = 0; 290281e79d38SArtem Bityutskiy else 290381e79d38SArtem Bityutskiy return -EINVAL; 290481e79d38SArtem Bityutskiy 290581e79d38SArtem Bityutskiy if (dent == d->dfs_chk_gen) 290681e79d38SArtem Bityutskiy d->chk_gen = val; 290781e79d38SArtem Bityutskiy else if (dent == d->dfs_chk_index) 290881e79d38SArtem Bityutskiy d->chk_index = val; 290981e79d38SArtem Bityutskiy else if (dent == d->dfs_chk_orph) 291081e79d38SArtem Bityutskiy d->chk_orph = val; 291181e79d38SArtem Bityutskiy else if (dent == d->dfs_chk_lprops) 291281e79d38SArtem Bityutskiy d->chk_lprops = val; 291381e79d38SArtem Bityutskiy else if (dent == d->dfs_chk_fs) 291481e79d38SArtem Bityutskiy d->chk_fs = val; 291581e79d38SArtem Bityutskiy else if (dent == d->dfs_tst_rcvry) 291681e79d38SArtem Bityutskiy d->tst_rcvry = val; 291781e79d38SArtem Bityutskiy else 2918552ff317SArtem Bityutskiy return -EINVAL; 2919552ff317SArtem Bityutskiy 2920552ff317SArtem Bityutskiy return count; 2921552ff317SArtem Bityutskiy } 2922552ff317SArtem Bityutskiy 292384abf972SArtem Bityutskiy static const struct file_operations dfs_fops = { 2924*7dae997dSArtem Bityutskiy .open = dfs_file_open, 292581e79d38SArtem Bityutskiy .read = dfs_file_read, 292681e79d38SArtem Bityutskiy .write = dfs_file_write, 2927552ff317SArtem Bityutskiy .owner = THIS_MODULE, 29281bbfc848SArtem Bityutskiy .llseek = no_llseek, 2929552ff317SArtem Bityutskiy }; 2930552ff317SArtem Bityutskiy 2931552ff317SArtem Bityutskiy /** 2932552ff317SArtem Bityutskiy * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance. 2933552ff317SArtem Bityutskiy * @c: UBIFS file-system description object 2934552ff317SArtem Bityutskiy * 2935552ff317SArtem Bityutskiy * This function creates all debugfs files for this instance of UBIFS. Returns 2936552ff317SArtem Bityutskiy * zero in case of success and a negative error code in case of failure. 2937552ff317SArtem Bityutskiy * 2938552ff317SArtem Bityutskiy * Note, the only reason we have not merged this function with the 2939552ff317SArtem Bityutskiy * 'ubifs_debugging_init()' function is because it is better to initialize 2940552ff317SArtem Bityutskiy * debugfs interfaces at the very end of the mount process, and remove them at 2941552ff317SArtem Bityutskiy * the very beginning of the mount process. 2942552ff317SArtem Bityutskiy */ 2943552ff317SArtem Bityutskiy int dbg_debugfs_init_fs(struct ubifs_info *c) 2944552ff317SArtem Bityutskiy { 2945ae380ce0SArtem Bityutskiy int err, n; 2946552ff317SArtem Bityutskiy const char *fname; 2947552ff317SArtem Bityutskiy struct dentry *dent; 2948552ff317SArtem Bityutskiy struct ubifs_debug_info *d = c->dbg; 2949552ff317SArtem Bityutskiy 2950ae380ce0SArtem Bityutskiy n = snprintf(d->dfs_dir_name, UBIFS_DFS_DIR_LEN + 1, UBIFS_DFS_DIR_NAME, 2951ae380ce0SArtem Bityutskiy c->vi.ubi_num, c->vi.vol_id); 2952ae380ce0SArtem Bityutskiy if (n == UBIFS_DFS_DIR_LEN) { 2953ae380ce0SArtem Bityutskiy /* The array size is too small */ 2954ae380ce0SArtem Bityutskiy fname = UBIFS_DFS_DIR_NAME; 2955ae380ce0SArtem Bityutskiy dent = ERR_PTR(-EINVAL); 2956ae380ce0SArtem Bityutskiy goto out; 2957ae380ce0SArtem Bityutskiy } 2958ae380ce0SArtem Bityutskiy 2959cc6a86b9SArtem Bityutskiy fname = d->dfs_dir_name; 2960cc6a86b9SArtem Bityutskiy dent = debugfs_create_dir(fname, dfs_rootdir); 296195169535SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 2962552ff317SArtem Bityutskiy goto out; 2963cc6a86b9SArtem Bityutskiy d->dfs_dir = dent; 2964552ff317SArtem Bityutskiy 2965552ff317SArtem Bityutskiy fname = "dump_lprops"; 29668c559d30SVasiliy Kulikov dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops); 296795169535SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 2968552ff317SArtem Bityutskiy goto out_remove; 296984abf972SArtem Bityutskiy d->dfs_dump_lprops = dent; 2970552ff317SArtem Bityutskiy 2971552ff317SArtem Bityutskiy fname = "dump_budg"; 29728c559d30SVasiliy Kulikov dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops); 297395169535SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 2974552ff317SArtem Bityutskiy goto out_remove; 297584abf972SArtem Bityutskiy d->dfs_dump_budg = dent; 2976552ff317SArtem Bityutskiy 2977552ff317SArtem Bityutskiy fname = "dump_tnc"; 29788c559d30SVasiliy Kulikov dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops); 297995169535SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 2980552ff317SArtem Bityutskiy goto out_remove; 298184abf972SArtem Bityutskiy d->dfs_dump_tnc = dent; 2982552ff317SArtem Bityutskiy 298381e79d38SArtem Bityutskiy fname = "chk_general"; 298481e79d38SArtem Bityutskiy dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c, 298581e79d38SArtem Bityutskiy &dfs_fops); 298681e79d38SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 298781e79d38SArtem Bityutskiy goto out_remove; 298881e79d38SArtem Bityutskiy d->dfs_chk_gen = dent; 298981e79d38SArtem Bityutskiy 299081e79d38SArtem Bityutskiy fname = "chk_index"; 299181e79d38SArtem Bityutskiy dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c, 299281e79d38SArtem Bityutskiy &dfs_fops); 299381e79d38SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 299481e79d38SArtem Bityutskiy goto out_remove; 299581e79d38SArtem Bityutskiy d->dfs_chk_index = dent; 299681e79d38SArtem Bityutskiy 299781e79d38SArtem Bityutskiy fname = "chk_orphans"; 299881e79d38SArtem Bityutskiy dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c, 299981e79d38SArtem Bityutskiy &dfs_fops); 300081e79d38SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 300181e79d38SArtem Bityutskiy goto out_remove; 300281e79d38SArtem Bityutskiy d->dfs_chk_orph = dent; 300381e79d38SArtem Bityutskiy 300481e79d38SArtem Bityutskiy fname = "chk_lprops"; 300581e79d38SArtem Bityutskiy dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c, 300681e79d38SArtem Bityutskiy &dfs_fops); 300781e79d38SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 300881e79d38SArtem Bityutskiy goto out_remove; 300981e79d38SArtem Bityutskiy d->dfs_chk_lprops = dent; 301081e79d38SArtem Bityutskiy 301181e79d38SArtem Bityutskiy fname = "chk_fs"; 301281e79d38SArtem Bityutskiy dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c, 301381e79d38SArtem Bityutskiy &dfs_fops); 301481e79d38SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 301581e79d38SArtem Bityutskiy goto out_remove; 301681e79d38SArtem Bityutskiy d->dfs_chk_fs = dent; 301781e79d38SArtem Bityutskiy 301881e79d38SArtem Bityutskiy fname = "tst_recovery"; 301981e79d38SArtem Bityutskiy dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c, 302081e79d38SArtem Bityutskiy &dfs_fops); 302181e79d38SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 302281e79d38SArtem Bityutskiy goto out_remove; 302381e79d38SArtem Bityutskiy d->dfs_tst_rcvry = dent; 302481e79d38SArtem Bityutskiy 3025552ff317SArtem Bityutskiy return 0; 3026552ff317SArtem Bityutskiy 3027552ff317SArtem Bityutskiy out_remove: 3028cc6a86b9SArtem Bityutskiy debugfs_remove_recursive(d->dfs_dir); 3029cc6a86b9SArtem Bityutskiy out: 303095169535SArtem Bityutskiy err = dent ? PTR_ERR(dent) : -ENODEV; 3031ae380ce0SArtem Bityutskiy ubifs_err("cannot create \"%s\" debugfs filr or directory, error %d\n", 3032552ff317SArtem Bityutskiy fname, err); 3033552ff317SArtem Bityutskiy return err; 3034552ff317SArtem Bityutskiy } 3035552ff317SArtem Bityutskiy 3036552ff317SArtem Bityutskiy /** 3037552ff317SArtem Bityutskiy * dbg_debugfs_exit_fs - remove all debugfs files. 3038552ff317SArtem Bityutskiy * @c: UBIFS file-system description object 3039552ff317SArtem Bityutskiy */ 3040552ff317SArtem Bityutskiy void dbg_debugfs_exit_fs(struct ubifs_info *c) 3041552ff317SArtem Bityutskiy { 304284abf972SArtem Bityutskiy debugfs_remove_recursive(c->dbg->dfs_dir); 3043552ff317SArtem Bityutskiy } 3044552ff317SArtem Bityutskiy 3045*7dae997dSArtem Bityutskiy /** 3046*7dae997dSArtem Bityutskiy * dbg_debugfs_init - initialize debugfs file-system. 3047*7dae997dSArtem Bityutskiy * 3048*7dae997dSArtem Bityutskiy * UBIFS uses debugfs file-system to expose various debugging knobs to 3049*7dae997dSArtem Bityutskiy * user-space. This function creates "ubifs" directory in the debugfs 3050*7dae997dSArtem Bityutskiy * file-system. Returns zero in case of success and a negative error code in 3051*7dae997dSArtem Bityutskiy * case of failure. 3052*7dae997dSArtem Bityutskiy */ 3053*7dae997dSArtem Bityutskiy int dbg_debugfs_init(void) 3054*7dae997dSArtem Bityutskiy { 3055*7dae997dSArtem Bityutskiy dfs_rootdir = debugfs_create_dir("ubifs", NULL); 3056*7dae997dSArtem Bityutskiy if (IS_ERR_OR_NULL(dfs_rootdir)) { 3057*7dae997dSArtem Bityutskiy int err = dfs_rootdir ? PTR_ERR(dfs_rootdir) : -ENODEV; 3058*7dae997dSArtem Bityutskiy ubifs_err("cannot create \"ubifs\" debugfs directory, " 3059*7dae997dSArtem Bityutskiy "error %d\n", err); 3060*7dae997dSArtem Bityutskiy return err; 3061*7dae997dSArtem Bityutskiy } 3062*7dae997dSArtem Bityutskiy 3063*7dae997dSArtem Bityutskiy return 0; 3064*7dae997dSArtem Bityutskiy } 3065*7dae997dSArtem Bityutskiy 3066*7dae997dSArtem Bityutskiy /** 3067*7dae997dSArtem Bityutskiy * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system. 3068*7dae997dSArtem Bityutskiy */ 3069*7dae997dSArtem Bityutskiy void dbg_debugfs_exit(void) 3070*7dae997dSArtem Bityutskiy { 3071*7dae997dSArtem Bityutskiy debugfs_remove(dfs_rootdir); 3072*7dae997dSArtem Bityutskiy } 3073*7dae997dSArtem Bityutskiy 3074*7dae997dSArtem Bityutskiy /** 3075*7dae997dSArtem Bityutskiy * ubifs_debugging_init - initialize UBIFS debugging. 3076*7dae997dSArtem Bityutskiy * @c: UBIFS file-system description object 3077*7dae997dSArtem Bityutskiy * 3078*7dae997dSArtem Bityutskiy * This function initializes debugging-related data for the file system. 3079*7dae997dSArtem Bityutskiy * Returns zero in case of success and a negative error code in case of 3080*7dae997dSArtem Bityutskiy * failure. 3081*7dae997dSArtem Bityutskiy */ 3082*7dae997dSArtem Bityutskiy int ubifs_debugging_init(struct ubifs_info *c) 3083*7dae997dSArtem Bityutskiy { 3084*7dae997dSArtem Bityutskiy c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL); 3085*7dae997dSArtem Bityutskiy if (!c->dbg) 3086*7dae997dSArtem Bityutskiy return -ENOMEM; 3087*7dae997dSArtem Bityutskiy 3088*7dae997dSArtem Bityutskiy failure_mode_init(c); 3089*7dae997dSArtem Bityutskiy return 0; 3090*7dae997dSArtem Bityutskiy } 3091*7dae997dSArtem Bityutskiy 3092*7dae997dSArtem Bityutskiy /** 3093*7dae997dSArtem Bityutskiy * ubifs_debugging_exit - free debugging data. 3094*7dae997dSArtem Bityutskiy * @c: UBIFS file-system description object 3095*7dae997dSArtem Bityutskiy */ 3096*7dae997dSArtem Bityutskiy void ubifs_debugging_exit(struct ubifs_info *c) 3097*7dae997dSArtem Bityutskiy { 3098*7dae997dSArtem Bityutskiy failure_mode_exit(c); 3099*7dae997dSArtem Bityutskiy kfree(c->dbg); 3100*7dae997dSArtem Bityutskiy } 3101*7dae997dSArtem Bityutskiy 31021e51764aSArtem Bityutskiy #endif /* CONFIG_UBIFS_FS_DEBUG */ 3103