11e51764aSArtem Bityutskiy /* 21e51764aSArtem Bityutskiy * This file is part of UBIFS. 31e51764aSArtem Bityutskiy * 41e51764aSArtem Bityutskiy * Copyright (C) 2006-2008 Nokia Corporation 51e51764aSArtem Bityutskiy * 61e51764aSArtem Bityutskiy * This program is free software; you can redistribute it and/or modify it 71e51764aSArtem Bityutskiy * under the terms of the GNU General Public License version 2 as published by 81e51764aSArtem Bityutskiy * the Free Software Foundation. 91e51764aSArtem Bityutskiy * 101e51764aSArtem Bityutskiy * This program is distributed in the hope that it will be useful, but WITHOUT 111e51764aSArtem Bityutskiy * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 121e51764aSArtem Bityutskiy * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 131e51764aSArtem Bityutskiy * more details. 141e51764aSArtem Bityutskiy * 151e51764aSArtem Bityutskiy * You should have received a copy of the GNU General Public License along with 161e51764aSArtem Bityutskiy * this program; if not, write to the Free Software Foundation, Inc., 51 171e51764aSArtem Bityutskiy * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 181e51764aSArtem Bityutskiy * 191e51764aSArtem Bityutskiy * Authors: Artem Bityutskiy (Битюцкий Артём) 201e51764aSArtem Bityutskiy * Adrian Hunter 211e51764aSArtem Bityutskiy */ 221e51764aSArtem Bityutskiy 231e51764aSArtem Bityutskiy /* 241e51764aSArtem Bityutskiy * This file implements most of the debugging stuff which is compiled in only 251e51764aSArtem Bityutskiy * when it is enabled. But some debugging check functions are implemented in 261e51764aSArtem Bityutskiy * corresponding subsystem, just because they are closely related and utilize 271e51764aSArtem Bityutskiy * various local functions of those subsystems. 281e51764aSArtem Bityutskiy */ 291e51764aSArtem Bityutskiy 301e51764aSArtem Bityutskiy #include <linux/module.h> 31552ff317SArtem Bityutskiy #include <linux/debugfs.h> 324d61db4fSArtem Bityutskiy #include <linux/math64.h> 3381e79d38SArtem Bityutskiy #include <linux/uaccess.h> 34a7fa94a9SArtem Bityutskiy #include <linux/random.h> 35a7fa94a9SArtem Bityutskiy #include "ubifs.h" 361e51764aSArtem Bityutskiy 371e51764aSArtem Bityutskiy #ifdef CONFIG_UBIFS_FS_DEBUG 381e51764aSArtem Bityutskiy 39b06283c7SArtem Bityutskiy static DEFINE_SPINLOCK(dbg_lock); 401e51764aSArtem Bityutskiy 411e51764aSArtem Bityutskiy static const char *get_key_fmt(int fmt) 421e51764aSArtem Bityutskiy { 431e51764aSArtem Bityutskiy switch (fmt) { 441e51764aSArtem Bityutskiy case UBIFS_SIMPLE_KEY_FMT: 451e51764aSArtem Bityutskiy return "simple"; 461e51764aSArtem Bityutskiy default: 471e51764aSArtem Bityutskiy return "unknown/invalid format"; 481e51764aSArtem Bityutskiy } 491e51764aSArtem Bityutskiy } 501e51764aSArtem Bityutskiy 511e51764aSArtem Bityutskiy static const char *get_key_hash(int hash) 521e51764aSArtem Bityutskiy { 531e51764aSArtem Bityutskiy switch (hash) { 541e51764aSArtem Bityutskiy case UBIFS_KEY_HASH_R5: 551e51764aSArtem Bityutskiy return "R5"; 561e51764aSArtem Bityutskiy case UBIFS_KEY_HASH_TEST: 571e51764aSArtem Bityutskiy return "test"; 581e51764aSArtem Bityutskiy default: 591e51764aSArtem Bityutskiy return "unknown/invalid name hash"; 601e51764aSArtem Bityutskiy } 611e51764aSArtem Bityutskiy } 621e51764aSArtem Bityutskiy 631e51764aSArtem Bityutskiy static const char *get_key_type(int type) 641e51764aSArtem Bityutskiy { 651e51764aSArtem Bityutskiy switch (type) { 661e51764aSArtem Bityutskiy case UBIFS_INO_KEY: 671e51764aSArtem Bityutskiy return "inode"; 681e51764aSArtem Bityutskiy case UBIFS_DENT_KEY: 691e51764aSArtem Bityutskiy return "direntry"; 701e51764aSArtem Bityutskiy case UBIFS_XENT_KEY: 711e51764aSArtem Bityutskiy return "xentry"; 721e51764aSArtem Bityutskiy case UBIFS_DATA_KEY: 731e51764aSArtem Bityutskiy return "data"; 741e51764aSArtem Bityutskiy case UBIFS_TRUN_KEY: 751e51764aSArtem Bityutskiy return "truncate"; 761e51764aSArtem Bityutskiy default: 771e51764aSArtem Bityutskiy return "unknown/invalid key"; 781e51764aSArtem Bityutskiy } 791e51764aSArtem Bityutskiy } 801e51764aSArtem Bityutskiy 814315fb40SArtem Bityutskiy static const char *get_dent_type(int type) 824315fb40SArtem Bityutskiy { 834315fb40SArtem Bityutskiy switch (type) { 844315fb40SArtem Bityutskiy case UBIFS_ITYPE_REG: 854315fb40SArtem Bityutskiy return "file"; 864315fb40SArtem Bityutskiy case UBIFS_ITYPE_DIR: 874315fb40SArtem Bityutskiy return "dir"; 884315fb40SArtem Bityutskiy case UBIFS_ITYPE_LNK: 894315fb40SArtem Bityutskiy return "symlink"; 904315fb40SArtem Bityutskiy case UBIFS_ITYPE_BLK: 914315fb40SArtem Bityutskiy return "blkdev"; 924315fb40SArtem Bityutskiy case UBIFS_ITYPE_CHR: 934315fb40SArtem Bityutskiy return "char dev"; 944315fb40SArtem Bityutskiy case UBIFS_ITYPE_FIFO: 954315fb40SArtem Bityutskiy return "fifo"; 964315fb40SArtem Bityutskiy case UBIFS_ITYPE_SOCK: 974315fb40SArtem Bityutskiy return "socket"; 984315fb40SArtem Bityutskiy default: 994315fb40SArtem Bityutskiy return "unknown/invalid type"; 1004315fb40SArtem Bityutskiy } 1014315fb40SArtem Bityutskiy } 1024315fb40SArtem Bityutskiy 103515315a1SArtem Bityutskiy const char *dbg_snprintf_key(const struct ubifs_info *c, 104515315a1SArtem Bityutskiy const union ubifs_key *key, char *buffer, int len) 1051e51764aSArtem Bityutskiy { 1061e51764aSArtem Bityutskiy char *p = buffer; 1071e51764aSArtem Bityutskiy int type = key_type(c, key); 1081e51764aSArtem Bityutskiy 1091e51764aSArtem Bityutskiy if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) { 1101e51764aSArtem Bityutskiy switch (type) { 1111e51764aSArtem Bityutskiy case UBIFS_INO_KEY: 112beba0060SArtem Bityutskiy len -= snprintf(p, len, "(%lu, %s)", 113beba0060SArtem Bityutskiy (unsigned long)key_inum(c, key), 1141e51764aSArtem Bityutskiy get_key_type(type)); 1151e51764aSArtem Bityutskiy break; 1161e51764aSArtem Bityutskiy case UBIFS_DENT_KEY: 1171e51764aSArtem Bityutskiy case UBIFS_XENT_KEY: 118beba0060SArtem Bityutskiy len -= snprintf(p, len, "(%lu, %s, %#08x)", 119e84461adSArtem Bityutskiy (unsigned long)key_inum(c, key), 1201e51764aSArtem Bityutskiy get_key_type(type), key_hash(c, key)); 1211e51764aSArtem Bityutskiy break; 1221e51764aSArtem Bityutskiy case UBIFS_DATA_KEY: 123beba0060SArtem Bityutskiy len -= snprintf(p, len, "(%lu, %s, %u)", 124e84461adSArtem Bityutskiy (unsigned long)key_inum(c, key), 1251e51764aSArtem Bityutskiy get_key_type(type), key_block(c, key)); 1261e51764aSArtem Bityutskiy break; 1271e51764aSArtem Bityutskiy case UBIFS_TRUN_KEY: 128beba0060SArtem Bityutskiy len -= snprintf(p, len, "(%lu, %s)", 129e84461adSArtem Bityutskiy (unsigned long)key_inum(c, key), 130e84461adSArtem Bityutskiy get_key_type(type)); 1311e51764aSArtem Bityutskiy break; 1321e51764aSArtem Bityutskiy default: 133beba0060SArtem Bityutskiy len -= snprintf(p, len, "(bad key type: %#08x, %#08x)", 1341e51764aSArtem Bityutskiy key->u32[0], key->u32[1]); 1351e51764aSArtem Bityutskiy } 1361e51764aSArtem Bityutskiy } else 137beba0060SArtem Bityutskiy len -= snprintf(p, len, "bad key format %d", c->key_fmt); 138beba0060SArtem Bityutskiy ubifs_assert(len > 0); 139515315a1SArtem Bityutskiy return p; 1401e51764aSArtem Bityutskiy } 1411e51764aSArtem Bityutskiy 1421e51764aSArtem Bityutskiy const char *dbg_ntype(int type) 1431e51764aSArtem Bityutskiy { 1441e51764aSArtem Bityutskiy switch (type) { 1451e51764aSArtem Bityutskiy case UBIFS_PAD_NODE: 1461e51764aSArtem Bityutskiy return "padding node"; 1471e51764aSArtem Bityutskiy case UBIFS_SB_NODE: 1481e51764aSArtem Bityutskiy return "superblock node"; 1491e51764aSArtem Bityutskiy case UBIFS_MST_NODE: 1501e51764aSArtem Bityutskiy return "master node"; 1511e51764aSArtem Bityutskiy case UBIFS_REF_NODE: 1521e51764aSArtem Bityutskiy return "reference node"; 1531e51764aSArtem Bityutskiy case UBIFS_INO_NODE: 1541e51764aSArtem Bityutskiy return "inode node"; 1551e51764aSArtem Bityutskiy case UBIFS_DENT_NODE: 1561e51764aSArtem Bityutskiy return "direntry node"; 1571e51764aSArtem Bityutskiy case UBIFS_XENT_NODE: 1581e51764aSArtem Bityutskiy return "xentry node"; 1591e51764aSArtem Bityutskiy case UBIFS_DATA_NODE: 1601e51764aSArtem Bityutskiy return "data node"; 1611e51764aSArtem Bityutskiy case UBIFS_TRUN_NODE: 1621e51764aSArtem Bityutskiy return "truncate node"; 1631e51764aSArtem Bityutskiy case UBIFS_IDX_NODE: 1641e51764aSArtem Bityutskiy return "indexing node"; 1651e51764aSArtem Bityutskiy case UBIFS_CS_NODE: 1661e51764aSArtem Bityutskiy return "commit start node"; 1671e51764aSArtem Bityutskiy case UBIFS_ORPH_NODE: 1681e51764aSArtem Bityutskiy return "orphan node"; 1691e51764aSArtem Bityutskiy default: 1701e51764aSArtem Bityutskiy return "unknown node"; 1711e51764aSArtem Bityutskiy } 1721e51764aSArtem Bityutskiy } 1731e51764aSArtem Bityutskiy 1741e51764aSArtem Bityutskiy static const char *dbg_gtype(int type) 1751e51764aSArtem Bityutskiy { 1761e51764aSArtem Bityutskiy switch (type) { 1771e51764aSArtem Bityutskiy case UBIFS_NO_NODE_GROUP: 1781e51764aSArtem Bityutskiy return "no node group"; 1791e51764aSArtem Bityutskiy case UBIFS_IN_NODE_GROUP: 1801e51764aSArtem Bityutskiy return "in node group"; 1811e51764aSArtem Bityutskiy case UBIFS_LAST_OF_NODE_GROUP: 1821e51764aSArtem Bityutskiy return "last of node group"; 1831e51764aSArtem Bityutskiy default: 1841e51764aSArtem Bityutskiy return "unknown"; 1851e51764aSArtem Bityutskiy } 1861e51764aSArtem Bityutskiy } 1871e51764aSArtem Bityutskiy 1881e51764aSArtem Bityutskiy const char *dbg_cstate(int cmt_state) 1891e51764aSArtem Bityutskiy { 1901e51764aSArtem Bityutskiy switch (cmt_state) { 1911e51764aSArtem Bityutskiy case COMMIT_RESTING: 1921e51764aSArtem Bityutskiy return "commit resting"; 1931e51764aSArtem Bityutskiy case COMMIT_BACKGROUND: 1941e51764aSArtem Bityutskiy return "background commit requested"; 1951e51764aSArtem Bityutskiy case COMMIT_REQUIRED: 1961e51764aSArtem Bityutskiy return "commit required"; 1971e51764aSArtem Bityutskiy case COMMIT_RUNNING_BACKGROUND: 1981e51764aSArtem Bityutskiy return "BACKGROUND commit running"; 1991e51764aSArtem Bityutskiy case COMMIT_RUNNING_REQUIRED: 2001e51764aSArtem Bityutskiy return "commit running and required"; 2011e51764aSArtem Bityutskiy case COMMIT_BROKEN: 2021e51764aSArtem Bityutskiy return "broken commit"; 2031e51764aSArtem Bityutskiy default: 2041e51764aSArtem Bityutskiy return "unknown commit state"; 2051e51764aSArtem Bityutskiy } 2061e51764aSArtem Bityutskiy } 2071e51764aSArtem Bityutskiy 20877a7ae58SArtem Bityutskiy const char *dbg_jhead(int jhead) 20977a7ae58SArtem Bityutskiy { 21077a7ae58SArtem Bityutskiy switch (jhead) { 21177a7ae58SArtem Bityutskiy case GCHD: 21277a7ae58SArtem Bityutskiy return "0 (GC)"; 21377a7ae58SArtem Bityutskiy case BASEHD: 21477a7ae58SArtem Bityutskiy return "1 (base)"; 21577a7ae58SArtem Bityutskiy case DATAHD: 21677a7ae58SArtem Bityutskiy return "2 (data)"; 21777a7ae58SArtem Bityutskiy default: 21877a7ae58SArtem Bityutskiy return "unknown journal head"; 21977a7ae58SArtem Bityutskiy } 22077a7ae58SArtem Bityutskiy } 22177a7ae58SArtem Bityutskiy 2221e51764aSArtem Bityutskiy static void dump_ch(const struct ubifs_ch *ch) 2231e51764aSArtem Bityutskiy { 22416c395caSArtem Bityutskiy printk(KERN_ERR "\tmagic %#x\n", le32_to_cpu(ch->magic)); 22516c395caSArtem Bityutskiy printk(KERN_ERR "\tcrc %#x\n", le32_to_cpu(ch->crc)); 22616c395caSArtem Bityutskiy printk(KERN_ERR "\tnode_type %d (%s)\n", ch->node_type, 2271e51764aSArtem Bityutskiy dbg_ntype(ch->node_type)); 22816c395caSArtem Bityutskiy printk(KERN_ERR "\tgroup_type %d (%s)\n", ch->group_type, 2291e51764aSArtem Bityutskiy dbg_gtype(ch->group_type)); 23016c395caSArtem Bityutskiy printk(KERN_ERR "\tsqnum %llu\n", 2311e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(ch->sqnum)); 23216c395caSArtem Bityutskiy printk(KERN_ERR "\tlen %u\n", le32_to_cpu(ch->len)); 2331e51764aSArtem Bityutskiy } 2341e51764aSArtem Bityutskiy 235*edf6be24SArtem Bityutskiy void ubifs_dump_inode(struct ubifs_info *c, const struct inode *inode) 2361e51764aSArtem Bityutskiy { 2371e51764aSArtem Bityutskiy const struct ubifs_inode *ui = ubifs_inode(inode); 2384315fb40SArtem Bityutskiy struct qstr nm = { .name = NULL }; 2394315fb40SArtem Bityutskiy union ubifs_key key; 2404315fb40SArtem Bityutskiy struct ubifs_dent_node *dent, *pdent = NULL; 2414315fb40SArtem Bityutskiy int count = 2; 2421e51764aSArtem Bityutskiy 24316c395caSArtem Bityutskiy printk(KERN_ERR "Dump in-memory inode:"); 24416c395caSArtem Bityutskiy printk(KERN_ERR "\tinode %lu\n", inode->i_ino); 24516c395caSArtem Bityutskiy printk(KERN_ERR "\tsize %llu\n", 2461e51764aSArtem Bityutskiy (unsigned long long)i_size_read(inode)); 24716c395caSArtem Bityutskiy printk(KERN_ERR "\tnlink %u\n", inode->i_nlink); 24816c395caSArtem Bityutskiy printk(KERN_ERR "\tuid %u\n", (unsigned int)inode->i_uid); 24916c395caSArtem Bityutskiy printk(KERN_ERR "\tgid %u\n", (unsigned int)inode->i_gid); 25016c395caSArtem Bityutskiy printk(KERN_ERR "\tatime %u.%u\n", 2511e51764aSArtem Bityutskiy (unsigned int)inode->i_atime.tv_sec, 2521e51764aSArtem Bityutskiy (unsigned int)inode->i_atime.tv_nsec); 25316c395caSArtem Bityutskiy printk(KERN_ERR "\tmtime %u.%u\n", 2541e51764aSArtem Bityutskiy (unsigned int)inode->i_mtime.tv_sec, 2551e51764aSArtem Bityutskiy (unsigned int)inode->i_mtime.tv_nsec); 25616c395caSArtem Bityutskiy printk(KERN_ERR "\tctime %u.%u\n", 2571e51764aSArtem Bityutskiy (unsigned int)inode->i_ctime.tv_sec, 2581e51764aSArtem Bityutskiy (unsigned int)inode->i_ctime.tv_nsec); 25916c395caSArtem Bityutskiy printk(KERN_ERR "\tcreat_sqnum %llu\n", ui->creat_sqnum); 26016c395caSArtem Bityutskiy printk(KERN_ERR "\txattr_size %u\n", ui->xattr_size); 26116c395caSArtem Bityutskiy printk(KERN_ERR "\txattr_cnt %u\n", ui->xattr_cnt); 26216c395caSArtem Bityutskiy printk(KERN_ERR "\txattr_names %u\n", ui->xattr_names); 26316c395caSArtem Bityutskiy printk(KERN_ERR "\tdirty %u\n", ui->dirty); 26416c395caSArtem Bityutskiy printk(KERN_ERR "\txattr %u\n", ui->xattr); 26516c395caSArtem Bityutskiy printk(KERN_ERR "\tbulk_read %u\n", ui->xattr); 26616c395caSArtem Bityutskiy printk(KERN_ERR "\tsynced_i_size %llu\n", 267b5e426e9SArtem Bityutskiy (unsigned long long)ui->synced_i_size); 26816c395caSArtem Bityutskiy printk(KERN_ERR "\tui_size %llu\n", 269b5e426e9SArtem Bityutskiy (unsigned long long)ui->ui_size); 27016c395caSArtem Bityutskiy printk(KERN_ERR "\tflags %d\n", ui->flags); 27116c395caSArtem Bityutskiy printk(KERN_ERR "\tcompr_type %d\n", ui->compr_type); 27216c395caSArtem Bityutskiy printk(KERN_ERR "\tlast_page_read %lu\n", ui->last_page_read); 27316c395caSArtem Bityutskiy printk(KERN_ERR "\tread_in_a_row %lu\n", ui->read_in_a_row); 27416c395caSArtem Bityutskiy printk(KERN_ERR "\tdata_len %d\n", ui->data_len); 2754315fb40SArtem Bityutskiy 2764315fb40SArtem Bityutskiy if (!S_ISDIR(inode->i_mode)) 2774315fb40SArtem Bityutskiy return; 2784315fb40SArtem Bityutskiy 27916c395caSArtem Bityutskiy printk(KERN_ERR "List of directory entries:\n"); 2804315fb40SArtem Bityutskiy ubifs_assert(!mutex_is_locked(&c->tnc_mutex)); 2814315fb40SArtem Bityutskiy 2824315fb40SArtem Bityutskiy lowest_dent_key(c, &key, inode->i_ino); 2834315fb40SArtem Bityutskiy while (1) { 2844315fb40SArtem Bityutskiy dent = ubifs_tnc_next_ent(c, &key, &nm); 2854315fb40SArtem Bityutskiy if (IS_ERR(dent)) { 2864315fb40SArtem Bityutskiy if (PTR_ERR(dent) != -ENOENT) 28716c395caSArtem Bityutskiy printk(KERN_ERR "error %ld\n", PTR_ERR(dent)); 2884315fb40SArtem Bityutskiy break; 2894315fb40SArtem Bityutskiy } 2904315fb40SArtem Bityutskiy 29116c395caSArtem Bityutskiy printk(KERN_ERR "\t%d: %s (%s)\n", 2924315fb40SArtem Bityutskiy count++, dent->name, get_dent_type(dent->type)); 2934315fb40SArtem Bityutskiy 2944315fb40SArtem Bityutskiy nm.name = dent->name; 2954315fb40SArtem Bityutskiy nm.len = le16_to_cpu(dent->nlen); 2964315fb40SArtem Bityutskiy kfree(pdent); 2974315fb40SArtem Bityutskiy pdent = dent; 2984315fb40SArtem Bityutskiy key_read(c, &dent->key, &key); 2994315fb40SArtem Bityutskiy } 3004315fb40SArtem Bityutskiy kfree(pdent); 3011e51764aSArtem Bityutskiy } 3021e51764aSArtem Bityutskiy 303*edf6be24SArtem Bityutskiy void ubifs_dump_node(const struct ubifs_info *c, const void *node) 3041e51764aSArtem Bityutskiy { 3051e51764aSArtem Bityutskiy int i, n; 3061e51764aSArtem Bityutskiy union ubifs_key key; 3071e51764aSArtem Bityutskiy const struct ubifs_ch *ch = node; 308515315a1SArtem Bityutskiy char key_buf[DBG_KEY_BUF_LEN]; 3091e51764aSArtem Bityutskiy 3102b1844a8SArtem Bityutskiy if (dbg_is_tst_rcvry(c)) 3111e51764aSArtem Bityutskiy return; 3121e51764aSArtem Bityutskiy 3131e51764aSArtem Bityutskiy /* If the magic is incorrect, just hexdump the first bytes */ 3141e51764aSArtem Bityutskiy if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) { 31516c395caSArtem Bityutskiy printk(KERN_ERR "Not a node, first %zu bytes:", UBIFS_CH_SZ); 31616c395caSArtem Bityutskiy print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 32, 1, 3171e51764aSArtem Bityutskiy (void *)node, UBIFS_CH_SZ, 1); 3181e51764aSArtem Bityutskiy return; 3191e51764aSArtem Bityutskiy } 3201e51764aSArtem Bityutskiy 3211e51764aSArtem Bityutskiy spin_lock(&dbg_lock); 3221e51764aSArtem Bityutskiy dump_ch(node); 3231e51764aSArtem Bityutskiy 3241e51764aSArtem Bityutskiy switch (ch->node_type) { 3251e51764aSArtem Bityutskiy case UBIFS_PAD_NODE: 3261e51764aSArtem Bityutskiy { 3271e51764aSArtem Bityutskiy const struct ubifs_pad_node *pad = node; 3281e51764aSArtem Bityutskiy 32916c395caSArtem Bityutskiy printk(KERN_ERR "\tpad_len %u\n", 3301e51764aSArtem Bityutskiy le32_to_cpu(pad->pad_len)); 3311e51764aSArtem Bityutskiy break; 3321e51764aSArtem Bityutskiy } 3331e51764aSArtem Bityutskiy case UBIFS_SB_NODE: 3341e51764aSArtem Bityutskiy { 3351e51764aSArtem Bityutskiy const struct ubifs_sb_node *sup = node; 3361e51764aSArtem Bityutskiy unsigned int sup_flags = le32_to_cpu(sup->flags); 3371e51764aSArtem Bityutskiy 33816c395caSArtem Bityutskiy printk(KERN_ERR "\tkey_hash %d (%s)\n", 3391e51764aSArtem Bityutskiy (int)sup->key_hash, get_key_hash(sup->key_hash)); 34016c395caSArtem Bityutskiy printk(KERN_ERR "\tkey_fmt %d (%s)\n", 3411e51764aSArtem Bityutskiy (int)sup->key_fmt, get_key_fmt(sup->key_fmt)); 34216c395caSArtem Bityutskiy printk(KERN_ERR "\tflags %#x\n", sup_flags); 34316c395caSArtem Bityutskiy printk(KERN_ERR "\t big_lpt %u\n", 3441e51764aSArtem Bityutskiy !!(sup_flags & UBIFS_FLG_BIGLPT)); 34516c395caSArtem Bityutskiy printk(KERN_ERR "\t space_fixup %u\n", 3469f58d350SMatthew L. Creech !!(sup_flags & UBIFS_FLG_SPACE_FIXUP)); 34716c395caSArtem Bityutskiy printk(KERN_ERR "\tmin_io_size %u\n", 3481e51764aSArtem Bityutskiy le32_to_cpu(sup->min_io_size)); 34916c395caSArtem Bityutskiy printk(KERN_ERR "\tleb_size %u\n", 3501e51764aSArtem Bityutskiy le32_to_cpu(sup->leb_size)); 35116c395caSArtem Bityutskiy printk(KERN_ERR "\tleb_cnt %u\n", 3521e51764aSArtem Bityutskiy le32_to_cpu(sup->leb_cnt)); 35316c395caSArtem Bityutskiy printk(KERN_ERR "\tmax_leb_cnt %u\n", 3541e51764aSArtem Bityutskiy le32_to_cpu(sup->max_leb_cnt)); 35516c395caSArtem Bityutskiy printk(KERN_ERR "\tmax_bud_bytes %llu\n", 3561e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(sup->max_bud_bytes)); 35716c395caSArtem Bityutskiy printk(KERN_ERR "\tlog_lebs %u\n", 3581e51764aSArtem Bityutskiy le32_to_cpu(sup->log_lebs)); 35916c395caSArtem Bityutskiy printk(KERN_ERR "\tlpt_lebs %u\n", 3601e51764aSArtem Bityutskiy le32_to_cpu(sup->lpt_lebs)); 36116c395caSArtem Bityutskiy printk(KERN_ERR "\torph_lebs %u\n", 3621e51764aSArtem Bityutskiy le32_to_cpu(sup->orph_lebs)); 36316c395caSArtem Bityutskiy printk(KERN_ERR "\tjhead_cnt %u\n", 3641e51764aSArtem Bityutskiy le32_to_cpu(sup->jhead_cnt)); 36516c395caSArtem Bityutskiy printk(KERN_ERR "\tfanout %u\n", 3661e51764aSArtem Bityutskiy le32_to_cpu(sup->fanout)); 36716c395caSArtem Bityutskiy printk(KERN_ERR "\tlsave_cnt %u\n", 3681e51764aSArtem Bityutskiy le32_to_cpu(sup->lsave_cnt)); 36916c395caSArtem Bityutskiy printk(KERN_ERR "\tdefault_compr %u\n", 3701e51764aSArtem Bityutskiy (int)le16_to_cpu(sup->default_compr)); 37116c395caSArtem Bityutskiy printk(KERN_ERR "\trp_size %llu\n", 3721e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(sup->rp_size)); 37316c395caSArtem Bityutskiy printk(KERN_ERR "\trp_uid %u\n", 3741e51764aSArtem Bityutskiy le32_to_cpu(sup->rp_uid)); 37516c395caSArtem Bityutskiy printk(KERN_ERR "\trp_gid %u\n", 3761e51764aSArtem Bityutskiy le32_to_cpu(sup->rp_gid)); 37716c395caSArtem Bityutskiy printk(KERN_ERR "\tfmt_version %u\n", 3781e51764aSArtem Bityutskiy le32_to_cpu(sup->fmt_version)); 37916c395caSArtem Bityutskiy printk(KERN_ERR "\ttime_gran %u\n", 3801e51764aSArtem Bityutskiy le32_to_cpu(sup->time_gran)); 38116c395caSArtem Bityutskiy printk(KERN_ERR "\tUUID %pUB\n", 3827f2f4e72SJoe Perches sup->uuid); 3831e51764aSArtem Bityutskiy break; 3841e51764aSArtem Bityutskiy } 3851e51764aSArtem Bityutskiy case UBIFS_MST_NODE: 3861e51764aSArtem Bityutskiy { 3871e51764aSArtem Bityutskiy const struct ubifs_mst_node *mst = node; 3881e51764aSArtem Bityutskiy 38916c395caSArtem Bityutskiy printk(KERN_ERR "\thighest_inum %llu\n", 3901e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->highest_inum)); 39116c395caSArtem Bityutskiy printk(KERN_ERR "\tcommit number %llu\n", 3921e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->cmt_no)); 39316c395caSArtem Bityutskiy printk(KERN_ERR "\tflags %#x\n", 3941e51764aSArtem Bityutskiy le32_to_cpu(mst->flags)); 39516c395caSArtem Bityutskiy printk(KERN_ERR "\tlog_lnum %u\n", 3961e51764aSArtem Bityutskiy le32_to_cpu(mst->log_lnum)); 39716c395caSArtem Bityutskiy printk(KERN_ERR "\troot_lnum %u\n", 3981e51764aSArtem Bityutskiy le32_to_cpu(mst->root_lnum)); 39916c395caSArtem Bityutskiy printk(KERN_ERR "\troot_offs %u\n", 4001e51764aSArtem Bityutskiy le32_to_cpu(mst->root_offs)); 40116c395caSArtem Bityutskiy printk(KERN_ERR "\troot_len %u\n", 4021e51764aSArtem Bityutskiy le32_to_cpu(mst->root_len)); 40316c395caSArtem Bityutskiy printk(KERN_ERR "\tgc_lnum %u\n", 4041e51764aSArtem Bityutskiy le32_to_cpu(mst->gc_lnum)); 40516c395caSArtem Bityutskiy printk(KERN_ERR "\tihead_lnum %u\n", 4061e51764aSArtem Bityutskiy le32_to_cpu(mst->ihead_lnum)); 40716c395caSArtem Bityutskiy printk(KERN_ERR "\tihead_offs %u\n", 4081e51764aSArtem Bityutskiy le32_to_cpu(mst->ihead_offs)); 40916c395caSArtem Bityutskiy printk(KERN_ERR "\tindex_size %llu\n", 4100ecb9529SHarvey Harrison (unsigned long long)le64_to_cpu(mst->index_size)); 41116c395caSArtem Bityutskiy printk(KERN_ERR "\tlpt_lnum %u\n", 4121e51764aSArtem Bityutskiy le32_to_cpu(mst->lpt_lnum)); 41316c395caSArtem Bityutskiy printk(KERN_ERR "\tlpt_offs %u\n", 4141e51764aSArtem Bityutskiy le32_to_cpu(mst->lpt_offs)); 41516c395caSArtem Bityutskiy printk(KERN_ERR "\tnhead_lnum %u\n", 4161e51764aSArtem Bityutskiy le32_to_cpu(mst->nhead_lnum)); 41716c395caSArtem Bityutskiy printk(KERN_ERR "\tnhead_offs %u\n", 4181e51764aSArtem Bityutskiy le32_to_cpu(mst->nhead_offs)); 41916c395caSArtem Bityutskiy printk(KERN_ERR "\tltab_lnum %u\n", 4201e51764aSArtem Bityutskiy le32_to_cpu(mst->ltab_lnum)); 42116c395caSArtem Bityutskiy printk(KERN_ERR "\tltab_offs %u\n", 4221e51764aSArtem Bityutskiy le32_to_cpu(mst->ltab_offs)); 42316c395caSArtem Bityutskiy printk(KERN_ERR "\tlsave_lnum %u\n", 4241e51764aSArtem Bityutskiy le32_to_cpu(mst->lsave_lnum)); 42516c395caSArtem Bityutskiy printk(KERN_ERR "\tlsave_offs %u\n", 4261e51764aSArtem Bityutskiy le32_to_cpu(mst->lsave_offs)); 42716c395caSArtem Bityutskiy printk(KERN_ERR "\tlscan_lnum %u\n", 4281e51764aSArtem Bityutskiy le32_to_cpu(mst->lscan_lnum)); 42916c395caSArtem Bityutskiy printk(KERN_ERR "\tleb_cnt %u\n", 4301e51764aSArtem Bityutskiy le32_to_cpu(mst->leb_cnt)); 43116c395caSArtem Bityutskiy printk(KERN_ERR "\tempty_lebs %u\n", 4321e51764aSArtem Bityutskiy le32_to_cpu(mst->empty_lebs)); 43316c395caSArtem Bityutskiy printk(KERN_ERR "\tidx_lebs %u\n", 4341e51764aSArtem Bityutskiy le32_to_cpu(mst->idx_lebs)); 43516c395caSArtem Bityutskiy printk(KERN_ERR "\ttotal_free %llu\n", 4361e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->total_free)); 43716c395caSArtem Bityutskiy printk(KERN_ERR "\ttotal_dirty %llu\n", 4381e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->total_dirty)); 43916c395caSArtem Bityutskiy printk(KERN_ERR "\ttotal_used %llu\n", 4401e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->total_used)); 44116c395caSArtem Bityutskiy printk(KERN_ERR "\ttotal_dead %llu\n", 4421e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->total_dead)); 44316c395caSArtem Bityutskiy printk(KERN_ERR "\ttotal_dark %llu\n", 4441e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(mst->total_dark)); 4451e51764aSArtem Bityutskiy break; 4461e51764aSArtem Bityutskiy } 4471e51764aSArtem Bityutskiy case UBIFS_REF_NODE: 4481e51764aSArtem Bityutskiy { 4491e51764aSArtem Bityutskiy const struct ubifs_ref_node *ref = node; 4501e51764aSArtem Bityutskiy 45116c395caSArtem Bityutskiy printk(KERN_ERR "\tlnum %u\n", 4521e51764aSArtem Bityutskiy le32_to_cpu(ref->lnum)); 45316c395caSArtem Bityutskiy printk(KERN_ERR "\toffs %u\n", 4541e51764aSArtem Bityutskiy le32_to_cpu(ref->offs)); 45516c395caSArtem Bityutskiy printk(KERN_ERR "\tjhead %u\n", 4561e51764aSArtem Bityutskiy le32_to_cpu(ref->jhead)); 4571e51764aSArtem Bityutskiy break; 4581e51764aSArtem Bityutskiy } 4591e51764aSArtem Bityutskiy case UBIFS_INO_NODE: 4601e51764aSArtem Bityutskiy { 4611e51764aSArtem Bityutskiy const struct ubifs_ino_node *ino = node; 4621e51764aSArtem Bityutskiy 4631e51764aSArtem Bityutskiy key_read(c, &ino->key, &key); 46416c395caSArtem Bityutskiy printk(KERN_ERR "\tkey %s\n", 465515315a1SArtem Bityutskiy dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN)); 46616c395caSArtem Bityutskiy printk(KERN_ERR "\tcreat_sqnum %llu\n", 4671e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(ino->creat_sqnum)); 46816c395caSArtem Bityutskiy printk(KERN_ERR "\tsize %llu\n", 4691e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(ino->size)); 47016c395caSArtem Bityutskiy printk(KERN_ERR "\tnlink %u\n", 4711e51764aSArtem Bityutskiy le32_to_cpu(ino->nlink)); 47216c395caSArtem Bityutskiy printk(KERN_ERR "\tatime %lld.%u\n", 4731e51764aSArtem Bityutskiy (long long)le64_to_cpu(ino->atime_sec), 4741e51764aSArtem Bityutskiy le32_to_cpu(ino->atime_nsec)); 47516c395caSArtem Bityutskiy printk(KERN_ERR "\tmtime %lld.%u\n", 4761e51764aSArtem Bityutskiy (long long)le64_to_cpu(ino->mtime_sec), 4771e51764aSArtem Bityutskiy le32_to_cpu(ino->mtime_nsec)); 47816c395caSArtem Bityutskiy printk(KERN_ERR "\tctime %lld.%u\n", 4791e51764aSArtem Bityutskiy (long long)le64_to_cpu(ino->ctime_sec), 4801e51764aSArtem Bityutskiy le32_to_cpu(ino->ctime_nsec)); 48116c395caSArtem Bityutskiy printk(KERN_ERR "\tuid %u\n", 4821e51764aSArtem Bityutskiy le32_to_cpu(ino->uid)); 48316c395caSArtem Bityutskiy printk(KERN_ERR "\tgid %u\n", 4841e51764aSArtem Bityutskiy le32_to_cpu(ino->gid)); 48516c395caSArtem Bityutskiy printk(KERN_ERR "\tmode %u\n", 4861e51764aSArtem Bityutskiy le32_to_cpu(ino->mode)); 48716c395caSArtem Bityutskiy printk(KERN_ERR "\tflags %#x\n", 4881e51764aSArtem Bityutskiy le32_to_cpu(ino->flags)); 48916c395caSArtem Bityutskiy printk(KERN_ERR "\txattr_cnt %u\n", 4901e51764aSArtem Bityutskiy le32_to_cpu(ino->xattr_cnt)); 49116c395caSArtem Bityutskiy printk(KERN_ERR "\txattr_size %u\n", 4921e51764aSArtem Bityutskiy le32_to_cpu(ino->xattr_size)); 49316c395caSArtem Bityutskiy printk(KERN_ERR "\txattr_names %u\n", 4941e51764aSArtem Bityutskiy le32_to_cpu(ino->xattr_names)); 49516c395caSArtem Bityutskiy printk(KERN_ERR "\tcompr_type %#x\n", 4961e51764aSArtem Bityutskiy (int)le16_to_cpu(ino->compr_type)); 49716c395caSArtem Bityutskiy printk(KERN_ERR "\tdata len %u\n", 4981e51764aSArtem Bityutskiy le32_to_cpu(ino->data_len)); 4991e51764aSArtem Bityutskiy break; 5001e51764aSArtem Bityutskiy } 5011e51764aSArtem Bityutskiy case UBIFS_DENT_NODE: 5021e51764aSArtem Bityutskiy case UBIFS_XENT_NODE: 5031e51764aSArtem Bityutskiy { 5041e51764aSArtem Bityutskiy const struct ubifs_dent_node *dent = node; 5051e51764aSArtem Bityutskiy int nlen = le16_to_cpu(dent->nlen); 5061e51764aSArtem Bityutskiy 5071e51764aSArtem Bityutskiy key_read(c, &dent->key, &key); 50816c395caSArtem Bityutskiy printk(KERN_ERR "\tkey %s\n", 509515315a1SArtem Bityutskiy dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN)); 51016c395caSArtem Bityutskiy printk(KERN_ERR "\tinum %llu\n", 5111e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(dent->inum)); 51216c395caSArtem Bityutskiy printk(KERN_ERR "\ttype %d\n", (int)dent->type); 51316c395caSArtem Bityutskiy printk(KERN_ERR "\tnlen %d\n", nlen); 51416c395caSArtem Bityutskiy printk(KERN_ERR "\tname "); 5151e51764aSArtem Bityutskiy 5161e51764aSArtem Bityutskiy if (nlen > UBIFS_MAX_NLEN) 51716c395caSArtem Bityutskiy printk(KERN_ERR "(bad name length, not printing, " 5181e51764aSArtem Bityutskiy "bad or corrupted node)"); 5191e51764aSArtem Bityutskiy else { 5201e51764aSArtem Bityutskiy for (i = 0; i < nlen && dent->name[i]; i++) 521c9927c3eSArtem Bityutskiy printk(KERN_CONT "%c", dent->name[i]); 5221e51764aSArtem Bityutskiy } 523c9927c3eSArtem Bityutskiy printk(KERN_CONT "\n"); 5241e51764aSArtem Bityutskiy 5251e51764aSArtem Bityutskiy break; 5261e51764aSArtem Bityutskiy } 5271e51764aSArtem Bityutskiy case UBIFS_DATA_NODE: 5281e51764aSArtem Bityutskiy { 5291e51764aSArtem Bityutskiy const struct ubifs_data_node *dn = node; 5301e51764aSArtem Bityutskiy int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ; 5311e51764aSArtem Bityutskiy 5321e51764aSArtem Bityutskiy key_read(c, &dn->key, &key); 53316c395caSArtem Bityutskiy printk(KERN_ERR "\tkey %s\n", 534515315a1SArtem Bityutskiy dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN)); 53516c395caSArtem Bityutskiy printk(KERN_ERR "\tsize %u\n", 5361e51764aSArtem Bityutskiy le32_to_cpu(dn->size)); 53716c395caSArtem Bityutskiy printk(KERN_ERR "\tcompr_typ %d\n", 5381e51764aSArtem Bityutskiy (int)le16_to_cpu(dn->compr_type)); 53916c395caSArtem Bityutskiy printk(KERN_ERR "\tdata size %d\n", 5401e51764aSArtem Bityutskiy dlen); 54116c395caSArtem Bityutskiy printk(KERN_ERR "\tdata:\n"); 54216c395caSArtem Bityutskiy print_hex_dump(KERN_ERR, "\t", DUMP_PREFIX_OFFSET, 32, 1, 5431e51764aSArtem Bityutskiy (void *)&dn->data, dlen, 0); 5441e51764aSArtem Bityutskiy break; 5451e51764aSArtem Bityutskiy } 5461e51764aSArtem Bityutskiy case UBIFS_TRUN_NODE: 5471e51764aSArtem Bityutskiy { 5481e51764aSArtem Bityutskiy const struct ubifs_trun_node *trun = node; 5491e51764aSArtem Bityutskiy 55016c395caSArtem Bityutskiy printk(KERN_ERR "\tinum %u\n", 5511e51764aSArtem Bityutskiy le32_to_cpu(trun->inum)); 55216c395caSArtem Bityutskiy printk(KERN_ERR "\told_size %llu\n", 5531e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(trun->old_size)); 55416c395caSArtem Bityutskiy printk(KERN_ERR "\tnew_size %llu\n", 5551e51764aSArtem Bityutskiy (unsigned long long)le64_to_cpu(trun->new_size)); 5561e51764aSArtem Bityutskiy break; 5571e51764aSArtem Bityutskiy } 5581e51764aSArtem Bityutskiy case UBIFS_IDX_NODE: 5591e51764aSArtem Bityutskiy { 5601e51764aSArtem Bityutskiy const struct ubifs_idx_node *idx = node; 5611e51764aSArtem Bityutskiy 5621e51764aSArtem Bityutskiy n = le16_to_cpu(idx->child_cnt); 56316c395caSArtem Bityutskiy printk(KERN_ERR "\tchild_cnt %d\n", n); 56416c395caSArtem Bityutskiy printk(KERN_ERR "\tlevel %d\n", 5651e51764aSArtem Bityutskiy (int)le16_to_cpu(idx->level)); 56616c395caSArtem Bityutskiy printk(KERN_ERR "\tBranches:\n"); 5671e51764aSArtem Bityutskiy 5681e51764aSArtem Bityutskiy for (i = 0; i < n && i < c->fanout - 1; i++) { 5691e51764aSArtem Bityutskiy const struct ubifs_branch *br; 5701e51764aSArtem Bityutskiy 5711e51764aSArtem Bityutskiy br = ubifs_idx_branch(c, idx, i); 5721e51764aSArtem Bityutskiy key_read(c, &br->key, &key); 57316c395caSArtem Bityutskiy printk(KERN_ERR "\t%d: LEB %d:%d len %d key %s\n", 5741e51764aSArtem Bityutskiy i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs), 575515315a1SArtem Bityutskiy le32_to_cpu(br->len), 576515315a1SArtem Bityutskiy dbg_snprintf_key(c, &key, key_buf, 577515315a1SArtem Bityutskiy DBG_KEY_BUF_LEN)); 5781e51764aSArtem Bityutskiy } 5791e51764aSArtem Bityutskiy break; 5801e51764aSArtem Bityutskiy } 5811e51764aSArtem Bityutskiy case UBIFS_CS_NODE: 5821e51764aSArtem Bityutskiy break; 5831e51764aSArtem Bityutskiy case UBIFS_ORPH_NODE: 5841e51764aSArtem Bityutskiy { 5851e51764aSArtem Bityutskiy const struct ubifs_orph_node *orph = node; 5861e51764aSArtem Bityutskiy 58716c395caSArtem Bityutskiy printk(KERN_ERR "\tcommit number %llu\n", 5881e51764aSArtem Bityutskiy (unsigned long long) 5891e51764aSArtem Bityutskiy le64_to_cpu(orph->cmt_no) & LLONG_MAX); 59016c395caSArtem Bityutskiy printk(KERN_ERR "\tlast node flag %llu\n", 5911e51764aSArtem Bityutskiy (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63); 5921e51764aSArtem Bityutskiy n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3; 59316c395caSArtem Bityutskiy printk(KERN_ERR "\t%d orphan inode numbers:\n", n); 5941e51764aSArtem Bityutskiy for (i = 0; i < n; i++) 59516c395caSArtem Bityutskiy printk(KERN_ERR "\t ino %llu\n", 5967424bac8SAlexander Beregalov (unsigned long long)le64_to_cpu(orph->inos[i])); 5971e51764aSArtem Bityutskiy break; 5981e51764aSArtem Bityutskiy } 5991e51764aSArtem Bityutskiy default: 60016c395caSArtem Bityutskiy printk(KERN_ERR "node type %d was not recognized\n", 6011e51764aSArtem Bityutskiy (int)ch->node_type); 6021e51764aSArtem Bityutskiy } 6031e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 6041e51764aSArtem Bityutskiy } 6051e51764aSArtem Bityutskiy 606*edf6be24SArtem Bityutskiy void ubifs_dump_budget_req(const struct ubifs_budget_req *req) 6071e51764aSArtem Bityutskiy { 6081e51764aSArtem Bityutskiy spin_lock(&dbg_lock); 60916c395caSArtem Bityutskiy printk(KERN_ERR "Budgeting request: new_ino %d, dirtied_ino %d\n", 6101e51764aSArtem Bityutskiy req->new_ino, req->dirtied_ino); 61116c395caSArtem Bityutskiy printk(KERN_ERR "\tnew_ino_d %d, dirtied_ino_d %d\n", 6121e51764aSArtem Bityutskiy req->new_ino_d, req->dirtied_ino_d); 61316c395caSArtem Bityutskiy printk(KERN_ERR "\tnew_page %d, dirtied_page %d\n", 6141e51764aSArtem Bityutskiy req->new_page, req->dirtied_page); 61516c395caSArtem Bityutskiy printk(KERN_ERR "\tnew_dent %d, mod_dent %d\n", 6161e51764aSArtem Bityutskiy req->new_dent, req->mod_dent); 61716c395caSArtem Bityutskiy printk(KERN_ERR "\tidx_growth %d\n", req->idx_growth); 61816c395caSArtem Bityutskiy printk(KERN_ERR "\tdata_growth %d dd_growth %d\n", 6191e51764aSArtem Bityutskiy req->data_growth, req->dd_growth); 6201e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 6211e51764aSArtem Bityutskiy } 6221e51764aSArtem Bityutskiy 623*edf6be24SArtem Bityutskiy void ubifs_dump_lstats(const struct ubifs_lp_stats *lst) 6241e51764aSArtem Bityutskiy { 6251e51764aSArtem Bityutskiy spin_lock(&dbg_lock); 62616c395caSArtem Bityutskiy printk(KERN_ERR "(pid %d) Lprops statistics: empty_lebs %d, " 6271de94159SArtem Bityutskiy "idx_lebs %d\n", current->pid, lst->empty_lebs, lst->idx_lebs); 62816c395caSArtem Bityutskiy printk(KERN_ERR "\ttaken_empty_lebs %d, total_free %lld, " 6291e51764aSArtem Bityutskiy "total_dirty %lld\n", lst->taken_empty_lebs, lst->total_free, 6301e51764aSArtem Bityutskiy lst->total_dirty); 63116c395caSArtem Bityutskiy printk(KERN_ERR "\ttotal_used %lld, total_dark %lld, " 6321e51764aSArtem Bityutskiy "total_dead %lld\n", lst->total_used, lst->total_dark, 6331e51764aSArtem Bityutskiy lst->total_dead); 6341e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 6351e51764aSArtem Bityutskiy } 6361e51764aSArtem Bityutskiy 637*edf6be24SArtem Bityutskiy void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi) 6381e51764aSArtem Bityutskiy { 6391e51764aSArtem Bityutskiy int i; 6401e51764aSArtem Bityutskiy struct rb_node *rb; 6411e51764aSArtem Bityutskiy struct ubifs_bud *bud; 6421e51764aSArtem Bityutskiy struct ubifs_gced_idx_leb *idx_gc; 64321a60258SArtem Bityutskiy long long available, outstanding, free; 6441e51764aSArtem Bityutskiy 6458ff83089SArtem Bityutskiy spin_lock(&c->space_lock); 6461e51764aSArtem Bityutskiy spin_lock(&dbg_lock); 64716c395caSArtem Bityutskiy printk(KERN_ERR "(pid %d) Budgeting info: data budget sum %lld, " 6488c3067e4SArtem Bityutskiy "total budget sum %lld\n", current->pid, 649f1bd66afSArtem Bityutskiy bi->data_growth + bi->dd_growth, 650f1bd66afSArtem Bityutskiy bi->data_growth + bi->dd_growth + bi->idx_growth); 65116c395caSArtem Bityutskiy printk(KERN_ERR "\tbudg_data_growth %lld, budg_dd_growth %lld, " 652f1bd66afSArtem Bityutskiy "budg_idx_growth %lld\n", bi->data_growth, bi->dd_growth, 653f1bd66afSArtem Bityutskiy bi->idx_growth); 65416c395caSArtem Bityutskiy printk(KERN_ERR "\tmin_idx_lebs %d, old_idx_sz %llu, " 655f1bd66afSArtem Bityutskiy "uncommitted_idx %lld\n", bi->min_idx_lebs, bi->old_idx_sz, 656f1bd66afSArtem Bityutskiy bi->uncommitted_idx); 65716c395caSArtem Bityutskiy printk(KERN_ERR "\tpage_budget %d, inode_budget %d, dent_budget %d\n", 658f1bd66afSArtem Bityutskiy bi->page_budget, bi->inode_budget, bi->dent_budget); 65916c395caSArtem Bityutskiy printk(KERN_ERR "\tnospace %u, nospace_rp %u\n", 660f1bd66afSArtem Bityutskiy bi->nospace, bi->nospace_rp); 66116c395caSArtem Bityutskiy printk(KERN_ERR "\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n", 6628c3067e4SArtem Bityutskiy c->dark_wm, c->dead_wm, c->max_idx_node_sz); 663f1bd66afSArtem Bityutskiy 664f1bd66afSArtem Bityutskiy if (bi != &c->bi) 665f1bd66afSArtem Bityutskiy /* 666f1bd66afSArtem Bityutskiy * If we are dumping saved budgeting data, do not print 667f1bd66afSArtem Bityutskiy * additional information which is about the current state, not 668f1bd66afSArtem Bityutskiy * the old one which corresponded to the saved budgeting data. 669f1bd66afSArtem Bityutskiy */ 670f1bd66afSArtem Bityutskiy goto out_unlock; 671f1bd66afSArtem Bityutskiy 67216c395caSArtem Bityutskiy printk(KERN_ERR "\tfreeable_cnt %d, calc_idx_sz %lld, idx_gc_cnt %d\n", 6738c3067e4SArtem Bityutskiy c->freeable_cnt, c->calc_idx_sz, c->idx_gc_cnt); 67416c395caSArtem Bityutskiy printk(KERN_ERR "\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, " 6751e51764aSArtem Bityutskiy "clean_zn_cnt %ld\n", atomic_long_read(&c->dirty_pg_cnt), 6761e51764aSArtem Bityutskiy atomic_long_read(&c->dirty_zn_cnt), 6771e51764aSArtem Bityutskiy atomic_long_read(&c->clean_zn_cnt)); 67816c395caSArtem Bityutskiy printk(KERN_ERR "\tgc_lnum %d, ihead_lnum %d\n", 6791e51764aSArtem Bityutskiy c->gc_lnum, c->ihead_lnum); 680f1bd66afSArtem Bityutskiy 68184abf972SArtem Bityutskiy /* If we are in R/O mode, journal heads do not exist */ 68284abf972SArtem Bityutskiy if (c->jheads) 6831e51764aSArtem Bityutskiy for (i = 0; i < c->jhead_cnt; i++) 68416c395caSArtem Bityutskiy printk(KERN_ERR "\tjhead %s\t LEB %d\n", 68577a7ae58SArtem Bityutskiy dbg_jhead(c->jheads[i].wbuf.jhead), 68677a7ae58SArtem Bityutskiy c->jheads[i].wbuf.lnum); 6871e51764aSArtem Bityutskiy for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) { 6881e51764aSArtem Bityutskiy bud = rb_entry(rb, struct ubifs_bud, rb); 68916c395caSArtem Bityutskiy printk(KERN_ERR "\tbud LEB %d\n", bud->lnum); 6901e51764aSArtem Bityutskiy } 6911e51764aSArtem Bityutskiy list_for_each_entry(bud, &c->old_buds, list) 69216c395caSArtem Bityutskiy printk(KERN_ERR "\told bud LEB %d\n", bud->lnum); 6931e51764aSArtem Bityutskiy list_for_each_entry(idx_gc, &c->idx_gc, list) 69416c395caSArtem Bityutskiy printk(KERN_ERR "\tGC'ed idx LEB %d unmap %d\n", 6951e51764aSArtem Bityutskiy idx_gc->lnum, idx_gc->unmap); 69616c395caSArtem Bityutskiy printk(KERN_ERR "\tcommit state %d\n", c->cmt_state); 69721a60258SArtem Bityutskiy 69821a60258SArtem Bityutskiy /* Print budgeting predictions */ 699b137545cSArtem Bityutskiy available = ubifs_calc_available(c, c->bi.min_idx_lebs); 700b137545cSArtem Bityutskiy outstanding = c->bi.data_growth + c->bi.dd_growth; 70184abf972SArtem Bityutskiy free = ubifs_get_free_space_nolock(c); 70216c395caSArtem Bityutskiy printk(KERN_ERR "Budgeting predictions:\n"); 70316c395caSArtem Bityutskiy printk(KERN_ERR "\tavailable: %lld, outstanding %lld, free %lld\n", 70421a60258SArtem Bityutskiy available, outstanding, free); 705f1bd66afSArtem Bityutskiy out_unlock: 7061e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 7078ff83089SArtem Bityutskiy spin_unlock(&c->space_lock); 7081e51764aSArtem Bityutskiy } 7091e51764aSArtem Bityutskiy 710*edf6be24SArtem Bityutskiy void ubifs_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp) 7111e51764aSArtem Bityutskiy { 712be9e62a7SArtem Bityutskiy int i, spc, dark = 0, dead = 0; 713be9e62a7SArtem Bityutskiy struct rb_node *rb; 714be9e62a7SArtem Bityutskiy struct ubifs_bud *bud; 715be9e62a7SArtem Bityutskiy 716be9e62a7SArtem Bityutskiy spc = lp->free + lp->dirty; 717be9e62a7SArtem Bityutskiy if (spc < c->dead_wm) 718be9e62a7SArtem Bityutskiy dead = spc; 719be9e62a7SArtem Bityutskiy else 720be9e62a7SArtem Bityutskiy dark = ubifs_calc_dark(c, spc); 721be9e62a7SArtem Bityutskiy 722be9e62a7SArtem Bityutskiy if (lp->flags & LPROPS_INDEX) 72316c395caSArtem Bityutskiy printk(KERN_ERR "LEB %-7d free %-8d dirty %-8d used %-8d " 724be9e62a7SArtem Bityutskiy "free + dirty %-8d flags %#x (", lp->lnum, lp->free, 725be9e62a7SArtem Bityutskiy lp->dirty, c->leb_size - spc, spc, lp->flags); 726be9e62a7SArtem Bityutskiy else 72716c395caSArtem Bityutskiy printk(KERN_ERR "LEB %-7d free %-8d dirty %-8d used %-8d " 728be9e62a7SArtem Bityutskiy "free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d " 729be9e62a7SArtem Bityutskiy "flags %#-4x (", lp->lnum, lp->free, lp->dirty, 730be9e62a7SArtem Bityutskiy c->leb_size - spc, spc, dark, dead, 731be9e62a7SArtem Bityutskiy (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags); 732be9e62a7SArtem Bityutskiy 733be9e62a7SArtem Bityutskiy if (lp->flags & LPROPS_TAKEN) { 734be9e62a7SArtem Bityutskiy if (lp->flags & LPROPS_INDEX) 735be9e62a7SArtem Bityutskiy printk(KERN_CONT "index, taken"); 736be9e62a7SArtem Bityutskiy else 737be9e62a7SArtem Bityutskiy printk(KERN_CONT "taken"); 738be9e62a7SArtem Bityutskiy } else { 739be9e62a7SArtem Bityutskiy const char *s; 740be9e62a7SArtem Bityutskiy 741be9e62a7SArtem Bityutskiy if (lp->flags & LPROPS_INDEX) { 742be9e62a7SArtem Bityutskiy switch (lp->flags & LPROPS_CAT_MASK) { 743be9e62a7SArtem Bityutskiy case LPROPS_DIRTY_IDX: 744be9e62a7SArtem Bityutskiy s = "dirty index"; 745be9e62a7SArtem Bityutskiy break; 746be9e62a7SArtem Bityutskiy case LPROPS_FRDI_IDX: 747be9e62a7SArtem Bityutskiy s = "freeable index"; 748be9e62a7SArtem Bityutskiy break; 749be9e62a7SArtem Bityutskiy default: 750be9e62a7SArtem Bityutskiy s = "index"; 751be9e62a7SArtem Bityutskiy } 752be9e62a7SArtem Bityutskiy } else { 753be9e62a7SArtem Bityutskiy switch (lp->flags & LPROPS_CAT_MASK) { 754be9e62a7SArtem Bityutskiy case LPROPS_UNCAT: 755be9e62a7SArtem Bityutskiy s = "not categorized"; 756be9e62a7SArtem Bityutskiy break; 757be9e62a7SArtem Bityutskiy case LPROPS_DIRTY: 758be9e62a7SArtem Bityutskiy s = "dirty"; 759be9e62a7SArtem Bityutskiy break; 760be9e62a7SArtem Bityutskiy case LPROPS_FREE: 761be9e62a7SArtem Bityutskiy s = "free"; 762be9e62a7SArtem Bityutskiy break; 763be9e62a7SArtem Bityutskiy case LPROPS_EMPTY: 764be9e62a7SArtem Bityutskiy s = "empty"; 765be9e62a7SArtem Bityutskiy break; 766be9e62a7SArtem Bityutskiy case LPROPS_FREEABLE: 767be9e62a7SArtem Bityutskiy s = "freeable"; 768be9e62a7SArtem Bityutskiy break; 769be9e62a7SArtem Bityutskiy default: 770be9e62a7SArtem Bityutskiy s = NULL; 771be9e62a7SArtem Bityutskiy break; 772be9e62a7SArtem Bityutskiy } 773be9e62a7SArtem Bityutskiy } 774be9e62a7SArtem Bityutskiy printk(KERN_CONT "%s", s); 775be9e62a7SArtem Bityutskiy } 776be9e62a7SArtem Bityutskiy 777be9e62a7SArtem Bityutskiy for (rb = rb_first((struct rb_root *)&c->buds); rb; rb = rb_next(rb)) { 778be9e62a7SArtem Bityutskiy bud = rb_entry(rb, struct ubifs_bud, rb); 779be9e62a7SArtem Bityutskiy if (bud->lnum == lp->lnum) { 780be9e62a7SArtem Bityutskiy int head = 0; 781be9e62a7SArtem Bityutskiy for (i = 0; i < c->jhead_cnt; i++) { 7821321657dSArtem Bityutskiy /* 7831321657dSArtem Bityutskiy * Note, if we are in R/O mode or in the middle 7841321657dSArtem Bityutskiy * of mounting/re-mounting, the write-buffers do 7851321657dSArtem Bityutskiy * not exist. 7861321657dSArtem Bityutskiy */ 7871321657dSArtem Bityutskiy if (c->jheads && 7881321657dSArtem Bityutskiy lp->lnum == c->jheads[i].wbuf.lnum) { 789be9e62a7SArtem Bityutskiy printk(KERN_CONT ", jhead %s", 790be9e62a7SArtem Bityutskiy dbg_jhead(i)); 791be9e62a7SArtem Bityutskiy head = 1; 792be9e62a7SArtem Bityutskiy } 793be9e62a7SArtem Bityutskiy } 794be9e62a7SArtem Bityutskiy if (!head) 795be9e62a7SArtem Bityutskiy printk(KERN_CONT ", bud of jhead %s", 796be9e62a7SArtem Bityutskiy dbg_jhead(bud->jhead)); 797be9e62a7SArtem Bityutskiy } 798be9e62a7SArtem Bityutskiy } 799be9e62a7SArtem Bityutskiy if (lp->lnum == c->gc_lnum) 800be9e62a7SArtem Bityutskiy printk(KERN_CONT ", GC LEB"); 801be9e62a7SArtem Bityutskiy printk(KERN_CONT ")\n"); 8021e51764aSArtem Bityutskiy } 8031e51764aSArtem Bityutskiy 804*edf6be24SArtem Bityutskiy void ubifs_dump_lprops(struct ubifs_info *c) 8051e51764aSArtem Bityutskiy { 8061e51764aSArtem Bityutskiy int lnum, err; 8071e51764aSArtem Bityutskiy struct ubifs_lprops lp; 8081e51764aSArtem Bityutskiy struct ubifs_lp_stats lst; 8091e51764aSArtem Bityutskiy 81016c395caSArtem Bityutskiy printk(KERN_ERR "(pid %d) start dumping LEB properties\n", 8112ba5f7aeSArtem Bityutskiy current->pid); 8121e51764aSArtem Bityutskiy ubifs_get_lp_stats(c, &lst); 813*edf6be24SArtem Bityutskiy ubifs_dump_lstats(&lst); 8141e51764aSArtem Bityutskiy 8151e51764aSArtem Bityutskiy for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) { 8161e51764aSArtem Bityutskiy err = ubifs_read_one_lp(c, lnum, &lp); 8171e51764aSArtem Bityutskiy if (err) 8181e51764aSArtem Bityutskiy ubifs_err("cannot read lprops for LEB %d", lnum); 8191e51764aSArtem Bityutskiy 820*edf6be24SArtem Bityutskiy ubifs_dump_lprop(c, &lp); 8211e51764aSArtem Bityutskiy } 82216c395caSArtem Bityutskiy printk(KERN_ERR "(pid %d) finish dumping LEB properties\n", 8232ba5f7aeSArtem Bityutskiy current->pid); 8241e51764aSArtem Bityutskiy } 8251e51764aSArtem Bityutskiy 826*edf6be24SArtem Bityutskiy void ubifs_dump_lpt_info(struct ubifs_info *c) 82773944a6dSAdrian Hunter { 82873944a6dSAdrian Hunter int i; 82973944a6dSAdrian Hunter 83073944a6dSAdrian Hunter spin_lock(&dbg_lock); 83116c395caSArtem Bityutskiy printk(KERN_ERR "(pid %d) dumping LPT information\n", current->pid); 83216c395caSArtem Bityutskiy printk(KERN_ERR "\tlpt_sz: %lld\n", c->lpt_sz); 83316c395caSArtem Bityutskiy printk(KERN_ERR "\tpnode_sz: %d\n", c->pnode_sz); 83416c395caSArtem Bityutskiy printk(KERN_ERR "\tnnode_sz: %d\n", c->nnode_sz); 83516c395caSArtem Bityutskiy printk(KERN_ERR "\tltab_sz: %d\n", c->ltab_sz); 83616c395caSArtem Bityutskiy printk(KERN_ERR "\tlsave_sz: %d\n", c->lsave_sz); 83716c395caSArtem Bityutskiy printk(KERN_ERR "\tbig_lpt: %d\n", c->big_lpt); 83816c395caSArtem Bityutskiy printk(KERN_ERR "\tlpt_hght: %d\n", c->lpt_hght); 83916c395caSArtem Bityutskiy printk(KERN_ERR "\tpnode_cnt: %d\n", c->pnode_cnt); 84016c395caSArtem Bityutskiy printk(KERN_ERR "\tnnode_cnt: %d\n", c->nnode_cnt); 84116c395caSArtem Bityutskiy printk(KERN_ERR "\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt); 84216c395caSArtem Bityutskiy printk(KERN_ERR "\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt); 84316c395caSArtem Bityutskiy printk(KERN_ERR "\tlsave_cnt: %d\n", c->lsave_cnt); 84416c395caSArtem Bityutskiy printk(KERN_ERR "\tspace_bits: %d\n", c->space_bits); 84516c395caSArtem Bityutskiy printk(KERN_ERR "\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits); 84616c395caSArtem Bityutskiy printk(KERN_ERR "\tlpt_offs_bits: %d\n", c->lpt_offs_bits); 84716c395caSArtem Bityutskiy printk(KERN_ERR "\tlpt_spc_bits: %d\n", c->lpt_spc_bits); 84816c395caSArtem Bityutskiy printk(KERN_ERR "\tpcnt_bits: %d\n", c->pcnt_bits); 84916c395caSArtem Bityutskiy printk(KERN_ERR "\tlnum_bits: %d\n", c->lnum_bits); 85016c395caSArtem Bityutskiy printk(KERN_ERR "\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs); 85116c395caSArtem Bityutskiy printk(KERN_ERR "\tLPT head is at %d:%d\n", 85273944a6dSAdrian Hunter c->nhead_lnum, c->nhead_offs); 85316c395caSArtem Bityutskiy printk(KERN_ERR "\tLPT ltab is at %d:%d\n", 854f92b9826SArtem Bityutskiy c->ltab_lnum, c->ltab_offs); 85573944a6dSAdrian Hunter if (c->big_lpt) 85616c395caSArtem Bityutskiy printk(KERN_ERR "\tLPT lsave is at %d:%d\n", 85773944a6dSAdrian Hunter c->lsave_lnum, c->lsave_offs); 85873944a6dSAdrian Hunter for (i = 0; i < c->lpt_lebs; i++) 85916c395caSArtem Bityutskiy printk(KERN_ERR "\tLPT LEB %d free %d dirty %d tgc %d " 86073944a6dSAdrian Hunter "cmt %d\n", i + c->lpt_first, c->ltab[i].free, 86173944a6dSAdrian Hunter c->ltab[i].dirty, c->ltab[i].tgc, c->ltab[i].cmt); 86273944a6dSAdrian Hunter spin_unlock(&dbg_lock); 86373944a6dSAdrian Hunter } 86473944a6dSAdrian Hunter 865*edf6be24SArtem Bityutskiy void ubifs_dump_sleb(const struct ubifs_info *c, 866d37854cfSArtem Bityutskiy const struct ubifs_scan_leb *sleb, int offs) 867d37854cfSArtem Bityutskiy { 868d37854cfSArtem Bityutskiy struct ubifs_scan_node *snod; 869d37854cfSArtem Bityutskiy 87016c395caSArtem Bityutskiy printk(KERN_ERR "(pid %d) start dumping scanned data from LEB %d:%d\n", 871d37854cfSArtem Bityutskiy current->pid, sleb->lnum, offs); 872d37854cfSArtem Bityutskiy 873d37854cfSArtem Bityutskiy list_for_each_entry(snod, &sleb->nodes, list) { 874d37854cfSArtem Bityutskiy cond_resched(); 87516c395caSArtem Bityutskiy printk(KERN_ERR "Dumping node at LEB %d:%d len %d\n", sleb->lnum, 876d37854cfSArtem Bityutskiy snod->offs, snod->len); 877*edf6be24SArtem Bityutskiy ubifs_dump_node(c, snod->node); 878d37854cfSArtem Bityutskiy } 879d37854cfSArtem Bityutskiy } 880d37854cfSArtem Bityutskiy 881*edf6be24SArtem Bityutskiy void ubifs_dump_leb(const struct ubifs_info *c, int lnum) 8821e51764aSArtem Bityutskiy { 8831e51764aSArtem Bityutskiy struct ubifs_scan_leb *sleb; 8841e51764aSArtem Bityutskiy struct ubifs_scan_node *snod; 88573d9aec3SArtem Bityutskiy void *buf; 8861e51764aSArtem Bityutskiy 8872b1844a8SArtem Bityutskiy if (dbg_is_tst_rcvry(c)) 8881e51764aSArtem Bityutskiy return; 8891e51764aSArtem Bityutskiy 89016c395caSArtem Bityutskiy printk(KERN_ERR "(pid %d) start dumping LEB %d\n", 8912ba5f7aeSArtem Bityutskiy current->pid, lnum); 89273d9aec3SArtem Bityutskiy 893fc5e58c0SArtem Bityutskiy buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL); 89473d9aec3SArtem Bityutskiy if (!buf) { 89573d9aec3SArtem Bityutskiy ubifs_err("cannot allocate memory for dumping LEB %d", lnum); 89673d9aec3SArtem Bityutskiy return; 89773d9aec3SArtem Bityutskiy } 89873d9aec3SArtem Bityutskiy 89973d9aec3SArtem Bityutskiy sleb = ubifs_scan(c, lnum, 0, buf, 0); 9001e51764aSArtem Bityutskiy if (IS_ERR(sleb)) { 9011e51764aSArtem Bityutskiy ubifs_err("scan error %d", (int)PTR_ERR(sleb)); 90273d9aec3SArtem Bityutskiy goto out; 9031e51764aSArtem Bityutskiy } 9041e51764aSArtem Bityutskiy 90516c395caSArtem Bityutskiy printk(KERN_ERR "LEB %d has %d nodes ending at %d\n", lnum, 9061e51764aSArtem Bityutskiy sleb->nodes_cnt, sleb->endpt); 9071e51764aSArtem Bityutskiy 9081e51764aSArtem Bityutskiy list_for_each_entry(snod, &sleb->nodes, list) { 9091e51764aSArtem Bityutskiy cond_resched(); 91016c395caSArtem Bityutskiy printk(KERN_ERR "Dumping node at LEB %d:%d len %d\n", lnum, 9111e51764aSArtem Bityutskiy snod->offs, snod->len); 912*edf6be24SArtem Bityutskiy ubifs_dump_node(c, snod->node); 9131e51764aSArtem Bityutskiy } 9141e51764aSArtem Bityutskiy 91516c395caSArtem Bityutskiy printk(KERN_ERR "(pid %d) finish dumping LEB %d\n", 9162ba5f7aeSArtem Bityutskiy current->pid, lnum); 9171e51764aSArtem Bityutskiy ubifs_scan_destroy(sleb); 91873d9aec3SArtem Bityutskiy 91973d9aec3SArtem Bityutskiy out: 92073d9aec3SArtem Bityutskiy vfree(buf); 9211e51764aSArtem Bityutskiy return; 9221e51764aSArtem Bityutskiy } 9231e51764aSArtem Bityutskiy 924*edf6be24SArtem Bityutskiy void ubifs_dump_znode(const struct ubifs_info *c, 9251e51764aSArtem Bityutskiy const struct ubifs_znode *znode) 9261e51764aSArtem Bityutskiy { 9271e51764aSArtem Bityutskiy int n; 9281e51764aSArtem Bityutskiy const struct ubifs_zbranch *zbr; 929515315a1SArtem Bityutskiy char key_buf[DBG_KEY_BUF_LEN]; 9301e51764aSArtem Bityutskiy 9311e51764aSArtem Bityutskiy spin_lock(&dbg_lock); 9321e51764aSArtem Bityutskiy if (znode->parent) 9331e51764aSArtem Bityutskiy zbr = &znode->parent->zbranch[znode->iip]; 9341e51764aSArtem Bityutskiy else 9351e51764aSArtem Bityutskiy zbr = &c->zroot; 9361e51764aSArtem Bityutskiy 93716c395caSArtem Bityutskiy printk(KERN_ERR "znode %p, LEB %d:%d len %d parent %p iip %d level %d" 9381e51764aSArtem Bityutskiy " child_cnt %d flags %lx\n", znode, zbr->lnum, zbr->offs, 9391e51764aSArtem Bityutskiy zbr->len, znode->parent, znode->iip, znode->level, 9401e51764aSArtem Bityutskiy znode->child_cnt, znode->flags); 9411e51764aSArtem Bityutskiy 9421e51764aSArtem Bityutskiy if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) { 9431e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 9441e51764aSArtem Bityutskiy return; 9451e51764aSArtem Bityutskiy } 9461e51764aSArtem Bityutskiy 94716c395caSArtem Bityutskiy printk(KERN_ERR "zbranches:\n"); 9481e51764aSArtem Bityutskiy for (n = 0; n < znode->child_cnt; n++) { 9491e51764aSArtem Bityutskiy zbr = &znode->zbranch[n]; 9501e51764aSArtem Bityutskiy if (znode->level > 0) 95116c395caSArtem Bityutskiy printk(KERN_ERR "\t%d: znode %p LEB %d:%d len %d key " 9521e51764aSArtem Bityutskiy "%s\n", n, zbr->znode, zbr->lnum, 9531e51764aSArtem Bityutskiy zbr->offs, zbr->len, 954515315a1SArtem Bityutskiy dbg_snprintf_key(c, &zbr->key, 955515315a1SArtem Bityutskiy key_buf, 956515315a1SArtem Bityutskiy DBG_KEY_BUF_LEN)); 9571e51764aSArtem Bityutskiy else 95816c395caSArtem Bityutskiy printk(KERN_ERR "\t%d: LNC %p LEB %d:%d len %d key " 9591e51764aSArtem Bityutskiy "%s\n", n, zbr->znode, zbr->lnum, 9601e51764aSArtem Bityutskiy zbr->offs, zbr->len, 961515315a1SArtem Bityutskiy dbg_snprintf_key(c, &zbr->key, 962515315a1SArtem Bityutskiy key_buf, 963515315a1SArtem Bityutskiy DBG_KEY_BUF_LEN)); 9641e51764aSArtem Bityutskiy } 9651e51764aSArtem Bityutskiy spin_unlock(&dbg_lock); 9661e51764aSArtem Bityutskiy } 9671e51764aSArtem Bityutskiy 968*edf6be24SArtem Bityutskiy void ubifs_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat) 9691e51764aSArtem Bityutskiy { 9701e51764aSArtem Bityutskiy int i; 9711e51764aSArtem Bityutskiy 97216c395caSArtem Bityutskiy printk(KERN_ERR "(pid %d) start dumping heap cat %d (%d elements)\n", 9731de94159SArtem Bityutskiy current->pid, cat, heap->cnt); 9741e51764aSArtem Bityutskiy for (i = 0; i < heap->cnt; i++) { 9751e51764aSArtem Bityutskiy struct ubifs_lprops *lprops = heap->arr[i]; 9761e51764aSArtem Bityutskiy 97716c395caSArtem Bityutskiy printk(KERN_ERR "\t%d. LEB %d hpos %d free %d dirty %d " 9781e51764aSArtem Bityutskiy "flags %d\n", i, lprops->lnum, lprops->hpos, 9791e51764aSArtem Bityutskiy lprops->free, lprops->dirty, lprops->flags); 9801e51764aSArtem Bityutskiy } 98116c395caSArtem Bityutskiy printk(KERN_ERR "(pid %d) finish dumping heap\n", current->pid); 9821e51764aSArtem Bityutskiy } 9831e51764aSArtem Bityutskiy 984*edf6be24SArtem Bityutskiy void ubifs_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode, 9851e51764aSArtem Bityutskiy struct ubifs_nnode *parent, int iip) 9861e51764aSArtem Bityutskiy { 9871e51764aSArtem Bityutskiy int i; 9881e51764aSArtem Bityutskiy 98916c395caSArtem Bityutskiy printk(KERN_ERR "(pid %d) dumping pnode:\n", current->pid); 99016c395caSArtem Bityutskiy printk(KERN_ERR "\taddress %zx parent %zx cnext %zx\n", 9911e51764aSArtem Bityutskiy (size_t)pnode, (size_t)parent, (size_t)pnode->cnext); 99216c395caSArtem Bityutskiy printk(KERN_ERR "\tflags %lu iip %d level %d num %d\n", 9931e51764aSArtem Bityutskiy pnode->flags, iip, pnode->level, pnode->num); 9941e51764aSArtem Bityutskiy for (i = 0; i < UBIFS_LPT_FANOUT; i++) { 9951e51764aSArtem Bityutskiy struct ubifs_lprops *lp = &pnode->lprops[i]; 9961e51764aSArtem Bityutskiy 99716c395caSArtem Bityutskiy printk(KERN_ERR "\t%d: free %d dirty %d flags %d lnum %d\n", 9981e51764aSArtem Bityutskiy i, lp->free, lp->dirty, lp->flags, lp->lnum); 9991e51764aSArtem Bityutskiy } 10001e51764aSArtem Bityutskiy } 10011e51764aSArtem Bityutskiy 1002*edf6be24SArtem Bityutskiy void ubifs_dump_tnc(struct ubifs_info *c) 10031e51764aSArtem Bityutskiy { 10041e51764aSArtem Bityutskiy struct ubifs_znode *znode; 10051e51764aSArtem Bityutskiy int level; 10061e51764aSArtem Bityutskiy 100716c395caSArtem Bityutskiy printk(KERN_ERR "\n"); 100816c395caSArtem Bityutskiy printk(KERN_ERR "(pid %d) start dumping TNC tree\n", current->pid); 10091e51764aSArtem Bityutskiy znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL); 10101e51764aSArtem Bityutskiy level = znode->level; 101116c395caSArtem Bityutskiy printk(KERN_ERR "== Level %d ==\n", level); 10121e51764aSArtem Bityutskiy while (znode) { 10131e51764aSArtem Bityutskiy if (level != znode->level) { 10141e51764aSArtem Bityutskiy level = znode->level; 101516c395caSArtem Bityutskiy printk(KERN_ERR "== Level %d ==\n", level); 10161e51764aSArtem Bityutskiy } 1017*edf6be24SArtem Bityutskiy ubifs_dump_znode(c, znode); 10181e51764aSArtem Bityutskiy znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode); 10191e51764aSArtem Bityutskiy } 102016c395caSArtem Bityutskiy printk(KERN_ERR "(pid %d) finish dumping TNC tree\n", current->pid); 10211e51764aSArtem Bityutskiy } 10221e51764aSArtem Bityutskiy 10231e51764aSArtem Bityutskiy static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode, 10241e51764aSArtem Bityutskiy void *priv) 10251e51764aSArtem Bityutskiy { 1026*edf6be24SArtem Bityutskiy ubifs_dump_znode(c, znode); 10271e51764aSArtem Bityutskiy return 0; 10281e51764aSArtem Bityutskiy } 10291e51764aSArtem Bityutskiy 10301e51764aSArtem Bityutskiy /** 1031*edf6be24SArtem Bityutskiy * ubifs_dump_index - dump the on-flash index. 10321e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 10331e51764aSArtem Bityutskiy * 1034*edf6be24SArtem Bityutskiy * This function dumps whole UBIFS indexing B-tree, unlike 'ubifs_dump_tnc()' 10351e51764aSArtem Bityutskiy * which dumps only in-memory znodes and does not read znodes which from flash. 10361e51764aSArtem Bityutskiy */ 1037*edf6be24SArtem Bityutskiy void ubifs_dump_index(struct ubifs_info *c) 10381e51764aSArtem Bityutskiy { 10391e51764aSArtem Bityutskiy dbg_walk_index(c, NULL, dump_znode, NULL); 10401e51764aSArtem Bityutskiy } 10411e51764aSArtem Bityutskiy 10421e51764aSArtem Bityutskiy /** 104384abf972SArtem Bityutskiy * dbg_save_space_info - save information about flash space. 104484abf972SArtem Bityutskiy * @c: UBIFS file-system description object 104584abf972SArtem Bityutskiy * 104684abf972SArtem Bityutskiy * This function saves information about UBIFS free space, dirty space, etc, in 104784abf972SArtem Bityutskiy * order to check it later. 104884abf972SArtem Bityutskiy */ 104984abf972SArtem Bityutskiy void dbg_save_space_info(struct ubifs_info *c) 105084abf972SArtem Bityutskiy { 105184abf972SArtem Bityutskiy struct ubifs_debug_info *d = c->dbg; 10527da6443aSArtem Bityutskiy int freeable_cnt; 105384abf972SArtem Bityutskiy 105484abf972SArtem Bityutskiy spin_lock(&c->space_lock); 10557da6443aSArtem Bityutskiy memcpy(&d->saved_lst, &c->lst, sizeof(struct ubifs_lp_stats)); 1056f1bd66afSArtem Bityutskiy memcpy(&d->saved_bi, &c->bi, sizeof(struct ubifs_budg_info)); 1057f1bd66afSArtem Bityutskiy d->saved_idx_gc_cnt = c->idx_gc_cnt; 10587da6443aSArtem Bityutskiy 10597da6443aSArtem Bityutskiy /* 10607da6443aSArtem Bityutskiy * We use a dirty hack here and zero out @c->freeable_cnt, because it 10617da6443aSArtem Bityutskiy * affects the free space calculations, and UBIFS might not know about 10627da6443aSArtem Bityutskiy * all freeable eraseblocks. Indeed, we know about freeable eraseblocks 10637da6443aSArtem Bityutskiy * only when we read their lprops, and we do this only lazily, upon the 10647da6443aSArtem Bityutskiy * need. So at any given point of time @c->freeable_cnt might be not 10657da6443aSArtem Bityutskiy * exactly accurate. 10667da6443aSArtem Bityutskiy * 10677da6443aSArtem Bityutskiy * Just one example about the issue we hit when we did not zero 10687da6443aSArtem Bityutskiy * @c->freeable_cnt. 10697da6443aSArtem Bityutskiy * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the 10707da6443aSArtem Bityutskiy * amount of free space in @d->saved_free 10717da6443aSArtem Bityutskiy * 2. We re-mount R/W, which makes UBIFS to read the "lsave" 10727da6443aSArtem Bityutskiy * information from flash, where we cache LEBs from various 10737da6443aSArtem Bityutskiy * categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()' 10747da6443aSArtem Bityutskiy * -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()' 10757da6443aSArtem Bityutskiy * -> 'ubifs_get_pnode()' -> 'update_cats()' 10767da6443aSArtem Bityutskiy * -> 'ubifs_add_to_cat()'). 10777da6443aSArtem Bityutskiy * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt 10787da6443aSArtem Bityutskiy * becomes %1. 10797da6443aSArtem Bityutskiy * 4. We calculate the amount of free space when the re-mount is 10807da6443aSArtem Bityutskiy * finished in 'dbg_check_space_info()' and it does not match 10817da6443aSArtem Bityutskiy * @d->saved_free. 10827da6443aSArtem Bityutskiy */ 10837da6443aSArtem Bityutskiy freeable_cnt = c->freeable_cnt; 10847da6443aSArtem Bityutskiy c->freeable_cnt = 0; 108584abf972SArtem Bityutskiy d->saved_free = ubifs_get_free_space_nolock(c); 10867da6443aSArtem Bityutskiy c->freeable_cnt = freeable_cnt; 108784abf972SArtem Bityutskiy spin_unlock(&c->space_lock); 108884abf972SArtem Bityutskiy } 108984abf972SArtem Bityutskiy 109084abf972SArtem Bityutskiy /** 109184abf972SArtem Bityutskiy * dbg_check_space_info - check flash space information. 109284abf972SArtem Bityutskiy * @c: UBIFS file-system description object 109384abf972SArtem Bityutskiy * 109484abf972SArtem Bityutskiy * This function compares current flash space information with the information 109584abf972SArtem Bityutskiy * which was saved when the 'dbg_save_space_info()' function was called. 109684abf972SArtem Bityutskiy * Returns zero if the information has not changed, and %-EINVAL it it has 109784abf972SArtem Bityutskiy * changed. 109884abf972SArtem Bityutskiy */ 109984abf972SArtem Bityutskiy int dbg_check_space_info(struct ubifs_info *c) 110084abf972SArtem Bityutskiy { 110184abf972SArtem Bityutskiy struct ubifs_debug_info *d = c->dbg; 110284abf972SArtem Bityutskiy struct ubifs_lp_stats lst; 11037da6443aSArtem Bityutskiy long long free; 11047da6443aSArtem Bityutskiy int freeable_cnt; 110584abf972SArtem Bityutskiy 110684abf972SArtem Bityutskiy spin_lock(&c->space_lock); 11077da6443aSArtem Bityutskiy freeable_cnt = c->freeable_cnt; 11087da6443aSArtem Bityutskiy c->freeable_cnt = 0; 11097da6443aSArtem Bityutskiy free = ubifs_get_free_space_nolock(c); 11107da6443aSArtem Bityutskiy c->freeable_cnt = freeable_cnt; 111184abf972SArtem Bityutskiy spin_unlock(&c->space_lock); 111284abf972SArtem Bityutskiy 111384abf972SArtem Bityutskiy if (free != d->saved_free) { 111484abf972SArtem Bityutskiy ubifs_err("free space changed from %lld to %lld", 111584abf972SArtem Bityutskiy d->saved_free, free); 111684abf972SArtem Bityutskiy goto out; 111784abf972SArtem Bityutskiy } 111884abf972SArtem Bityutskiy 111984abf972SArtem Bityutskiy return 0; 112084abf972SArtem Bityutskiy 112184abf972SArtem Bityutskiy out: 112284abf972SArtem Bityutskiy ubifs_msg("saved lprops statistics dump"); 1123*edf6be24SArtem Bityutskiy ubifs_dump_lstats(&d->saved_lst); 1124f1bd66afSArtem Bityutskiy ubifs_msg("saved budgeting info dump"); 1125*edf6be24SArtem Bityutskiy ubifs_dump_budg(c, &d->saved_bi); 1126f1bd66afSArtem Bityutskiy ubifs_msg("saved idx_gc_cnt %d", d->saved_idx_gc_cnt); 112784abf972SArtem Bityutskiy ubifs_msg("current lprops statistics dump"); 1128f1bd66afSArtem Bityutskiy ubifs_get_lp_stats(c, &lst); 1129*edf6be24SArtem Bityutskiy ubifs_dump_lstats(&lst); 1130f1bd66afSArtem Bityutskiy ubifs_msg("current budgeting info dump"); 1131*edf6be24SArtem Bityutskiy ubifs_dump_budg(c, &c->bi); 113284abf972SArtem Bityutskiy dump_stack(); 113384abf972SArtem Bityutskiy return -EINVAL; 113484abf972SArtem Bityutskiy } 113584abf972SArtem Bityutskiy 113684abf972SArtem Bityutskiy /** 11371e51764aSArtem Bityutskiy * dbg_check_synced_i_size - check synchronized inode size. 1138d808efb4SArtem Bityutskiy * @c: UBIFS file-system description object 11391e51764aSArtem Bityutskiy * @inode: inode to check 11401e51764aSArtem Bityutskiy * 11411e51764aSArtem Bityutskiy * If inode is clean, synchronized inode size has to be equivalent to current 11421e51764aSArtem Bityutskiy * inode size. This function has to be called only for locked inodes (@i_mutex 11431e51764aSArtem Bityutskiy * has to be locked). Returns %0 if synchronized inode size if correct, and 11441e51764aSArtem Bityutskiy * %-EINVAL if not. 11451e51764aSArtem Bityutskiy */ 1146d808efb4SArtem Bityutskiy int dbg_check_synced_i_size(const struct ubifs_info *c, struct inode *inode) 11471e51764aSArtem Bityutskiy { 11481e51764aSArtem Bityutskiy int err = 0; 11491e51764aSArtem Bityutskiy struct ubifs_inode *ui = ubifs_inode(inode); 11501e51764aSArtem Bityutskiy 11512b1844a8SArtem Bityutskiy if (!dbg_is_chk_gen(c)) 11521e51764aSArtem Bityutskiy return 0; 11531e51764aSArtem Bityutskiy if (!S_ISREG(inode->i_mode)) 11541e51764aSArtem Bityutskiy return 0; 11551e51764aSArtem Bityutskiy 11561e51764aSArtem Bityutskiy mutex_lock(&ui->ui_mutex); 11571e51764aSArtem Bityutskiy spin_lock(&ui->ui_lock); 11581e51764aSArtem Bityutskiy if (ui->ui_size != ui->synced_i_size && !ui->dirty) { 11591e51764aSArtem Bityutskiy ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode " 11601e51764aSArtem Bityutskiy "is clean", ui->ui_size, ui->synced_i_size); 11611e51764aSArtem Bityutskiy ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino, 11621e51764aSArtem Bityutskiy inode->i_mode, i_size_read(inode)); 11637c46d0aeSArtem Bityutskiy dump_stack(); 11641e51764aSArtem Bityutskiy err = -EINVAL; 11651e51764aSArtem Bityutskiy } 11661e51764aSArtem Bityutskiy spin_unlock(&ui->ui_lock); 11671e51764aSArtem Bityutskiy mutex_unlock(&ui->ui_mutex); 11681e51764aSArtem Bityutskiy return err; 11691e51764aSArtem Bityutskiy } 11701e51764aSArtem Bityutskiy 11711e51764aSArtem Bityutskiy /* 11721e51764aSArtem Bityutskiy * dbg_check_dir - check directory inode size and link count. 11731e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 11741e51764aSArtem Bityutskiy * @dir: the directory to calculate size for 11751e51764aSArtem Bityutskiy * @size: the result is returned here 11761e51764aSArtem Bityutskiy * 11771e51764aSArtem Bityutskiy * This function makes sure that directory size and link count are correct. 11781e51764aSArtem Bityutskiy * Returns zero in case of success and a negative error code in case of 11791e51764aSArtem Bityutskiy * failure. 11801e51764aSArtem Bityutskiy * 11811e51764aSArtem Bityutskiy * Note, it is good idea to make sure the @dir->i_mutex is locked before 11821e51764aSArtem Bityutskiy * calling this function. 11831e51764aSArtem Bityutskiy */ 11841b51e983SArtem Bityutskiy int dbg_check_dir(struct ubifs_info *c, const struct inode *dir) 11851e51764aSArtem Bityutskiy { 11861e51764aSArtem Bityutskiy unsigned int nlink = 2; 11871e51764aSArtem Bityutskiy union ubifs_key key; 11881e51764aSArtem Bityutskiy struct ubifs_dent_node *dent, *pdent = NULL; 11891e51764aSArtem Bityutskiy struct qstr nm = { .name = NULL }; 11901e51764aSArtem Bityutskiy loff_t size = UBIFS_INO_NODE_SZ; 11911e51764aSArtem Bityutskiy 11922b1844a8SArtem Bityutskiy if (!dbg_is_chk_gen(c)) 11931e51764aSArtem Bityutskiy return 0; 11941e51764aSArtem Bityutskiy 11951e51764aSArtem Bityutskiy if (!S_ISDIR(dir->i_mode)) 11961e51764aSArtem Bityutskiy return 0; 11971e51764aSArtem Bityutskiy 11981e51764aSArtem Bityutskiy lowest_dent_key(c, &key, dir->i_ino); 11991e51764aSArtem Bityutskiy while (1) { 12001e51764aSArtem Bityutskiy int err; 12011e51764aSArtem Bityutskiy 12021e51764aSArtem Bityutskiy dent = ubifs_tnc_next_ent(c, &key, &nm); 12031e51764aSArtem Bityutskiy if (IS_ERR(dent)) { 12041e51764aSArtem Bityutskiy err = PTR_ERR(dent); 12051e51764aSArtem Bityutskiy if (err == -ENOENT) 12061e51764aSArtem Bityutskiy break; 12071e51764aSArtem Bityutskiy return err; 12081e51764aSArtem Bityutskiy } 12091e51764aSArtem Bityutskiy 12101e51764aSArtem Bityutskiy nm.name = dent->name; 12111e51764aSArtem Bityutskiy nm.len = le16_to_cpu(dent->nlen); 12121e51764aSArtem Bityutskiy size += CALC_DENT_SIZE(nm.len); 12131e51764aSArtem Bityutskiy if (dent->type == UBIFS_ITYPE_DIR) 12141e51764aSArtem Bityutskiy nlink += 1; 12151e51764aSArtem Bityutskiy kfree(pdent); 12161e51764aSArtem Bityutskiy pdent = dent; 12171e51764aSArtem Bityutskiy key_read(c, &dent->key, &key); 12181e51764aSArtem Bityutskiy } 12191e51764aSArtem Bityutskiy kfree(pdent); 12201e51764aSArtem Bityutskiy 12211e51764aSArtem Bityutskiy if (i_size_read(dir) != size) { 12221e51764aSArtem Bityutskiy ubifs_err("directory inode %lu has size %llu, " 12231e51764aSArtem Bityutskiy "but calculated size is %llu", dir->i_ino, 12241e51764aSArtem Bityutskiy (unsigned long long)i_size_read(dir), 12251e51764aSArtem Bityutskiy (unsigned long long)size); 1226*edf6be24SArtem Bityutskiy ubifs_dump_inode(c, dir); 12271e51764aSArtem Bityutskiy dump_stack(); 12281e51764aSArtem Bityutskiy return -EINVAL; 12291e51764aSArtem Bityutskiy } 12301e51764aSArtem Bityutskiy if (dir->i_nlink != nlink) { 12311e51764aSArtem Bityutskiy ubifs_err("directory inode %lu has nlink %u, but calculated " 12321e51764aSArtem Bityutskiy "nlink is %u", dir->i_ino, dir->i_nlink, nlink); 1233*edf6be24SArtem Bityutskiy ubifs_dump_inode(c, dir); 12341e51764aSArtem Bityutskiy dump_stack(); 12351e51764aSArtem Bityutskiy return -EINVAL; 12361e51764aSArtem Bityutskiy } 12371e51764aSArtem Bityutskiy 12381e51764aSArtem Bityutskiy return 0; 12391e51764aSArtem Bityutskiy } 12401e51764aSArtem Bityutskiy 12411e51764aSArtem Bityutskiy /** 12421e51764aSArtem Bityutskiy * dbg_check_key_order - make sure that colliding keys are properly ordered. 12431e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 12441e51764aSArtem Bityutskiy * @zbr1: first zbranch 12451e51764aSArtem Bityutskiy * @zbr2: following zbranch 12461e51764aSArtem Bityutskiy * 12471e51764aSArtem Bityutskiy * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of 12481e51764aSArtem Bityutskiy * names of the direntries/xentries which are referred by the keys. This 12491e51764aSArtem Bityutskiy * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes 12501e51764aSArtem Bityutskiy * sure the name of direntry/xentry referred by @zbr1 is less than 12511e51764aSArtem Bityutskiy * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not, 12521e51764aSArtem Bityutskiy * and a negative error code in case of failure. 12531e51764aSArtem Bityutskiy */ 12541e51764aSArtem Bityutskiy static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1, 12551e51764aSArtem Bityutskiy struct ubifs_zbranch *zbr2) 12561e51764aSArtem Bityutskiy { 12571e51764aSArtem Bityutskiy int err, nlen1, nlen2, cmp; 12581e51764aSArtem Bityutskiy struct ubifs_dent_node *dent1, *dent2; 12591e51764aSArtem Bityutskiy union ubifs_key key; 1260515315a1SArtem Bityutskiy char key_buf[DBG_KEY_BUF_LEN]; 12611e51764aSArtem Bityutskiy 12621e51764aSArtem Bityutskiy ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key)); 12631e51764aSArtem Bityutskiy dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS); 12641e51764aSArtem Bityutskiy if (!dent1) 12651e51764aSArtem Bityutskiy return -ENOMEM; 12661e51764aSArtem Bityutskiy dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS); 12671e51764aSArtem Bityutskiy if (!dent2) { 12681e51764aSArtem Bityutskiy err = -ENOMEM; 12691e51764aSArtem Bityutskiy goto out_free; 12701e51764aSArtem Bityutskiy } 12711e51764aSArtem Bityutskiy 12721e51764aSArtem Bityutskiy err = ubifs_tnc_read_node(c, zbr1, dent1); 12731e51764aSArtem Bityutskiy if (err) 12741e51764aSArtem Bityutskiy goto out_free; 12751e51764aSArtem Bityutskiy err = ubifs_validate_entry(c, dent1); 12761e51764aSArtem Bityutskiy if (err) 12771e51764aSArtem Bityutskiy goto out_free; 12781e51764aSArtem Bityutskiy 12791e51764aSArtem Bityutskiy err = ubifs_tnc_read_node(c, zbr2, dent2); 12801e51764aSArtem Bityutskiy if (err) 12811e51764aSArtem Bityutskiy goto out_free; 12821e51764aSArtem Bityutskiy err = ubifs_validate_entry(c, dent2); 12831e51764aSArtem Bityutskiy if (err) 12841e51764aSArtem Bityutskiy goto out_free; 12851e51764aSArtem Bityutskiy 12861e51764aSArtem Bityutskiy /* Make sure node keys are the same as in zbranch */ 12871e51764aSArtem Bityutskiy err = 1; 12881e51764aSArtem Bityutskiy key_read(c, &dent1->key, &key); 12891e51764aSArtem Bityutskiy if (keys_cmp(c, &zbr1->key, &key)) { 12905d38b3acSArtem Bityutskiy dbg_err("1st entry at %d:%d has key %s", zbr1->lnum, 1291515315a1SArtem Bityutskiy zbr1->offs, dbg_snprintf_key(c, &key, key_buf, 1292515315a1SArtem Bityutskiy DBG_KEY_BUF_LEN)); 12935d38b3acSArtem Bityutskiy dbg_err("but it should have key %s according to tnc", 1294515315a1SArtem Bityutskiy dbg_snprintf_key(c, &zbr1->key, key_buf, 1295515315a1SArtem Bityutskiy DBG_KEY_BUF_LEN)); 1296*edf6be24SArtem Bityutskiy ubifs_dump_node(c, dent1); 12971e51764aSArtem Bityutskiy goto out_free; 12981e51764aSArtem Bityutskiy } 12991e51764aSArtem Bityutskiy 13001e51764aSArtem Bityutskiy key_read(c, &dent2->key, &key); 13011e51764aSArtem Bityutskiy if (keys_cmp(c, &zbr2->key, &key)) { 13025d38b3acSArtem Bityutskiy dbg_err("2nd entry at %d:%d has key %s", zbr1->lnum, 1303515315a1SArtem Bityutskiy zbr1->offs, dbg_snprintf_key(c, &key, key_buf, 1304515315a1SArtem Bityutskiy DBG_KEY_BUF_LEN)); 13055d38b3acSArtem Bityutskiy dbg_err("but it should have key %s according to tnc", 1306515315a1SArtem Bityutskiy dbg_snprintf_key(c, &zbr2->key, key_buf, 1307515315a1SArtem Bityutskiy DBG_KEY_BUF_LEN)); 1308*edf6be24SArtem Bityutskiy ubifs_dump_node(c, dent2); 13091e51764aSArtem Bityutskiy goto out_free; 13101e51764aSArtem Bityutskiy } 13111e51764aSArtem Bityutskiy 13121e51764aSArtem Bityutskiy nlen1 = le16_to_cpu(dent1->nlen); 13131e51764aSArtem Bityutskiy nlen2 = le16_to_cpu(dent2->nlen); 13141e51764aSArtem Bityutskiy 13151e51764aSArtem Bityutskiy cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2)); 13161e51764aSArtem Bityutskiy if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) { 13171e51764aSArtem Bityutskiy err = 0; 13181e51764aSArtem Bityutskiy goto out_free; 13191e51764aSArtem Bityutskiy } 13201e51764aSArtem Bityutskiy if (cmp == 0 && nlen1 == nlen2) 13215d38b3acSArtem Bityutskiy dbg_err("2 xent/dent nodes with the same name"); 13221e51764aSArtem Bityutskiy else 13235d38b3acSArtem Bityutskiy dbg_err("bad order of colliding key %s", 1324515315a1SArtem Bityutskiy dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN)); 13251e51764aSArtem Bityutskiy 1326552ff317SArtem Bityutskiy ubifs_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs); 1327*edf6be24SArtem Bityutskiy ubifs_dump_node(c, dent1); 1328552ff317SArtem Bityutskiy ubifs_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs); 1329*edf6be24SArtem Bityutskiy ubifs_dump_node(c, dent2); 13301e51764aSArtem Bityutskiy 13311e51764aSArtem Bityutskiy out_free: 13321e51764aSArtem Bityutskiy kfree(dent2); 13331e51764aSArtem Bityutskiy kfree(dent1); 13341e51764aSArtem Bityutskiy return err; 13351e51764aSArtem Bityutskiy } 13361e51764aSArtem Bityutskiy 13371e51764aSArtem Bityutskiy /** 13381e51764aSArtem Bityutskiy * dbg_check_znode - check if znode is all right. 13391e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 13401e51764aSArtem Bityutskiy * @zbr: zbranch which points to this znode 13411e51764aSArtem Bityutskiy * 13421e51764aSArtem Bityutskiy * This function makes sure that znode referred to by @zbr is all right. 13431e51764aSArtem Bityutskiy * Returns zero if it is, and %-EINVAL if it is not. 13441e51764aSArtem Bityutskiy */ 13451e51764aSArtem Bityutskiy static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr) 13461e51764aSArtem Bityutskiy { 13471e51764aSArtem Bityutskiy struct ubifs_znode *znode = zbr->znode; 13481e51764aSArtem Bityutskiy struct ubifs_znode *zp = znode->parent; 13491e51764aSArtem Bityutskiy int n, err, cmp; 13501e51764aSArtem Bityutskiy 13511e51764aSArtem Bityutskiy if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) { 13521e51764aSArtem Bityutskiy err = 1; 13531e51764aSArtem Bityutskiy goto out; 13541e51764aSArtem Bityutskiy } 13551e51764aSArtem Bityutskiy if (znode->level < 0) { 13561e51764aSArtem Bityutskiy err = 2; 13571e51764aSArtem Bityutskiy goto out; 13581e51764aSArtem Bityutskiy } 13591e51764aSArtem Bityutskiy if (znode->iip < 0 || znode->iip >= c->fanout) { 13601e51764aSArtem Bityutskiy err = 3; 13611e51764aSArtem Bityutskiy goto out; 13621e51764aSArtem Bityutskiy } 13631e51764aSArtem Bityutskiy 13641e51764aSArtem Bityutskiy if (zbr->len == 0) 13651e51764aSArtem Bityutskiy /* Only dirty zbranch may have no on-flash nodes */ 13661e51764aSArtem Bityutskiy if (!ubifs_zn_dirty(znode)) { 13671e51764aSArtem Bityutskiy err = 4; 13681e51764aSArtem Bityutskiy goto out; 13691e51764aSArtem Bityutskiy } 13701e51764aSArtem Bityutskiy 13711e51764aSArtem Bityutskiy if (ubifs_zn_dirty(znode)) { 13721e51764aSArtem Bityutskiy /* 13731e51764aSArtem Bityutskiy * If znode is dirty, its parent has to be dirty as well. The 13741e51764aSArtem Bityutskiy * order of the operation is important, so we have to have 13751e51764aSArtem Bityutskiy * memory barriers. 13761e51764aSArtem Bityutskiy */ 13771e51764aSArtem Bityutskiy smp_mb(); 13781e51764aSArtem Bityutskiy if (zp && !ubifs_zn_dirty(zp)) { 13791e51764aSArtem Bityutskiy /* 13801e51764aSArtem Bityutskiy * The dirty flag is atomic and is cleared outside the 13811e51764aSArtem Bityutskiy * TNC mutex, so znode's dirty flag may now have 13821e51764aSArtem Bityutskiy * been cleared. The child is always cleared before the 13831e51764aSArtem Bityutskiy * parent, so we just need to check again. 13841e51764aSArtem Bityutskiy */ 13851e51764aSArtem Bityutskiy smp_mb(); 13861e51764aSArtem Bityutskiy if (ubifs_zn_dirty(znode)) { 13871e51764aSArtem Bityutskiy err = 5; 13881e51764aSArtem Bityutskiy goto out; 13891e51764aSArtem Bityutskiy } 13901e51764aSArtem Bityutskiy } 13911e51764aSArtem Bityutskiy } 13921e51764aSArtem Bityutskiy 13931e51764aSArtem Bityutskiy if (zp) { 13941e51764aSArtem Bityutskiy const union ubifs_key *min, *max; 13951e51764aSArtem Bityutskiy 13961e51764aSArtem Bityutskiy if (znode->level != zp->level - 1) { 13971e51764aSArtem Bityutskiy err = 6; 13981e51764aSArtem Bityutskiy goto out; 13991e51764aSArtem Bityutskiy } 14001e51764aSArtem Bityutskiy 14011e51764aSArtem Bityutskiy /* Make sure the 'parent' pointer in our znode is correct */ 14021e51764aSArtem Bityutskiy err = ubifs_search_zbranch(c, zp, &zbr->key, &n); 14031e51764aSArtem Bityutskiy if (!err) { 14041e51764aSArtem Bityutskiy /* This zbranch does not exist in the parent */ 14051e51764aSArtem Bityutskiy err = 7; 14061e51764aSArtem Bityutskiy goto out; 14071e51764aSArtem Bityutskiy } 14081e51764aSArtem Bityutskiy 14091e51764aSArtem Bityutskiy if (znode->iip >= zp->child_cnt) { 14101e51764aSArtem Bityutskiy err = 8; 14111e51764aSArtem Bityutskiy goto out; 14121e51764aSArtem Bityutskiy } 14131e51764aSArtem Bityutskiy 14141e51764aSArtem Bityutskiy if (znode->iip != n) { 14151e51764aSArtem Bityutskiy /* This may happen only in case of collisions */ 14161e51764aSArtem Bityutskiy if (keys_cmp(c, &zp->zbranch[n].key, 14171e51764aSArtem Bityutskiy &zp->zbranch[znode->iip].key)) { 14181e51764aSArtem Bityutskiy err = 9; 14191e51764aSArtem Bityutskiy goto out; 14201e51764aSArtem Bityutskiy } 14211e51764aSArtem Bityutskiy n = znode->iip; 14221e51764aSArtem Bityutskiy } 14231e51764aSArtem Bityutskiy 14241e51764aSArtem Bityutskiy /* 14251e51764aSArtem Bityutskiy * Make sure that the first key in our znode is greater than or 14261e51764aSArtem Bityutskiy * equal to the key in the pointing zbranch. 14271e51764aSArtem Bityutskiy */ 14281e51764aSArtem Bityutskiy min = &zbr->key; 14291e51764aSArtem Bityutskiy cmp = keys_cmp(c, min, &znode->zbranch[0].key); 14301e51764aSArtem Bityutskiy if (cmp == 1) { 14311e51764aSArtem Bityutskiy err = 10; 14321e51764aSArtem Bityutskiy goto out; 14331e51764aSArtem Bityutskiy } 14341e51764aSArtem Bityutskiy 14351e51764aSArtem Bityutskiy if (n + 1 < zp->child_cnt) { 14361e51764aSArtem Bityutskiy max = &zp->zbranch[n + 1].key; 14371e51764aSArtem Bityutskiy 14381e51764aSArtem Bityutskiy /* 14391e51764aSArtem Bityutskiy * Make sure the last key in our znode is less or 14407d4e9ccbSArtem Bityutskiy * equivalent than the key in the zbranch which goes 14411e51764aSArtem Bityutskiy * after our pointing zbranch. 14421e51764aSArtem Bityutskiy */ 14431e51764aSArtem Bityutskiy cmp = keys_cmp(c, max, 14441e51764aSArtem Bityutskiy &znode->zbranch[znode->child_cnt - 1].key); 14451e51764aSArtem Bityutskiy if (cmp == -1) { 14461e51764aSArtem Bityutskiy err = 11; 14471e51764aSArtem Bityutskiy goto out; 14481e51764aSArtem Bityutskiy } 14491e51764aSArtem Bityutskiy } 14501e51764aSArtem Bityutskiy } else { 14511e51764aSArtem Bityutskiy /* This may only be root znode */ 14521e51764aSArtem Bityutskiy if (zbr != &c->zroot) { 14531e51764aSArtem Bityutskiy err = 12; 14541e51764aSArtem Bityutskiy goto out; 14551e51764aSArtem Bityutskiy } 14561e51764aSArtem Bityutskiy } 14571e51764aSArtem Bityutskiy 14581e51764aSArtem Bityutskiy /* 14591e51764aSArtem Bityutskiy * Make sure that next key is greater or equivalent then the previous 14601e51764aSArtem Bityutskiy * one. 14611e51764aSArtem Bityutskiy */ 14621e51764aSArtem Bityutskiy for (n = 1; n < znode->child_cnt; n++) { 14631e51764aSArtem Bityutskiy cmp = keys_cmp(c, &znode->zbranch[n - 1].key, 14641e51764aSArtem Bityutskiy &znode->zbranch[n].key); 14651e51764aSArtem Bityutskiy if (cmp > 0) { 14661e51764aSArtem Bityutskiy err = 13; 14671e51764aSArtem Bityutskiy goto out; 14681e51764aSArtem Bityutskiy } 14691e51764aSArtem Bityutskiy if (cmp == 0) { 14701e51764aSArtem Bityutskiy /* This can only be keys with colliding hash */ 14711e51764aSArtem Bityutskiy if (!is_hash_key(c, &znode->zbranch[n].key)) { 14721e51764aSArtem Bityutskiy err = 14; 14731e51764aSArtem Bityutskiy goto out; 14741e51764aSArtem Bityutskiy } 14751e51764aSArtem Bityutskiy 14761e51764aSArtem Bityutskiy if (znode->level != 0 || c->replaying) 14771e51764aSArtem Bityutskiy continue; 14781e51764aSArtem Bityutskiy 14791e51764aSArtem Bityutskiy /* 14801e51764aSArtem Bityutskiy * Colliding keys should follow binary order of 14811e51764aSArtem Bityutskiy * corresponding xentry/dentry names. 14821e51764aSArtem Bityutskiy */ 14831e51764aSArtem Bityutskiy err = dbg_check_key_order(c, &znode->zbranch[n - 1], 14841e51764aSArtem Bityutskiy &znode->zbranch[n]); 14851e51764aSArtem Bityutskiy if (err < 0) 14861e51764aSArtem Bityutskiy return err; 14871e51764aSArtem Bityutskiy if (err) { 14881e51764aSArtem Bityutskiy err = 15; 14891e51764aSArtem Bityutskiy goto out; 14901e51764aSArtem Bityutskiy } 14911e51764aSArtem Bityutskiy } 14921e51764aSArtem Bityutskiy } 14931e51764aSArtem Bityutskiy 14941e51764aSArtem Bityutskiy for (n = 0; n < znode->child_cnt; n++) { 14951e51764aSArtem Bityutskiy if (!znode->zbranch[n].znode && 14961e51764aSArtem Bityutskiy (znode->zbranch[n].lnum == 0 || 14971e51764aSArtem Bityutskiy znode->zbranch[n].len == 0)) { 14981e51764aSArtem Bityutskiy err = 16; 14991e51764aSArtem Bityutskiy goto out; 15001e51764aSArtem Bityutskiy } 15011e51764aSArtem Bityutskiy 15021e51764aSArtem Bityutskiy if (znode->zbranch[n].lnum != 0 && 15031e51764aSArtem Bityutskiy znode->zbranch[n].len == 0) { 15041e51764aSArtem Bityutskiy err = 17; 15051e51764aSArtem Bityutskiy goto out; 15061e51764aSArtem Bityutskiy } 15071e51764aSArtem Bityutskiy 15081e51764aSArtem Bityutskiy if (znode->zbranch[n].lnum == 0 && 15091e51764aSArtem Bityutskiy znode->zbranch[n].len != 0) { 15101e51764aSArtem Bityutskiy err = 18; 15111e51764aSArtem Bityutskiy goto out; 15121e51764aSArtem Bityutskiy } 15131e51764aSArtem Bityutskiy 15141e51764aSArtem Bityutskiy if (znode->zbranch[n].lnum == 0 && 15151e51764aSArtem Bityutskiy znode->zbranch[n].offs != 0) { 15161e51764aSArtem Bityutskiy err = 19; 15171e51764aSArtem Bityutskiy goto out; 15181e51764aSArtem Bityutskiy } 15191e51764aSArtem Bityutskiy 15201e51764aSArtem Bityutskiy if (znode->level != 0 && znode->zbranch[n].znode) 15211e51764aSArtem Bityutskiy if (znode->zbranch[n].znode->parent != znode) { 15221e51764aSArtem Bityutskiy err = 20; 15231e51764aSArtem Bityutskiy goto out; 15241e51764aSArtem Bityutskiy } 15251e51764aSArtem Bityutskiy } 15261e51764aSArtem Bityutskiy 15271e51764aSArtem Bityutskiy return 0; 15281e51764aSArtem Bityutskiy 15291e51764aSArtem Bityutskiy out: 15301e51764aSArtem Bityutskiy ubifs_err("failed, error %d", err); 15311e51764aSArtem Bityutskiy ubifs_msg("dump of the znode"); 1532*edf6be24SArtem Bityutskiy ubifs_dump_znode(c, znode); 15331e51764aSArtem Bityutskiy if (zp) { 15341e51764aSArtem Bityutskiy ubifs_msg("dump of the parent znode"); 1535*edf6be24SArtem Bityutskiy ubifs_dump_znode(c, zp); 15361e51764aSArtem Bityutskiy } 15371e51764aSArtem Bityutskiy dump_stack(); 15381e51764aSArtem Bityutskiy return -EINVAL; 15391e51764aSArtem Bityutskiy } 15401e51764aSArtem Bityutskiy 15411e51764aSArtem Bityutskiy /** 15421e51764aSArtem Bityutskiy * dbg_check_tnc - check TNC tree. 15431e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 15441e51764aSArtem Bityutskiy * @extra: do extra checks that are possible at start commit 15451e51764aSArtem Bityutskiy * 15461e51764aSArtem Bityutskiy * This function traverses whole TNC tree and checks every znode. Returns zero 15471e51764aSArtem Bityutskiy * if everything is all right and %-EINVAL if something is wrong with TNC. 15481e51764aSArtem Bityutskiy */ 15491e51764aSArtem Bityutskiy int dbg_check_tnc(struct ubifs_info *c, int extra) 15501e51764aSArtem Bityutskiy { 15511e51764aSArtem Bityutskiy struct ubifs_znode *znode; 15521e51764aSArtem Bityutskiy long clean_cnt = 0, dirty_cnt = 0; 15531e51764aSArtem Bityutskiy int err, last; 15541e51764aSArtem Bityutskiy 15558d7819b4SArtem Bityutskiy if (!dbg_is_chk_index(c)) 15561e51764aSArtem Bityutskiy return 0; 15571e51764aSArtem Bityutskiy 15581e51764aSArtem Bityutskiy ubifs_assert(mutex_is_locked(&c->tnc_mutex)); 15591e51764aSArtem Bityutskiy if (!c->zroot.znode) 15601e51764aSArtem Bityutskiy return 0; 15611e51764aSArtem Bityutskiy 15621e51764aSArtem Bityutskiy znode = ubifs_tnc_postorder_first(c->zroot.znode); 15631e51764aSArtem Bityutskiy while (1) { 15641e51764aSArtem Bityutskiy struct ubifs_znode *prev; 15651e51764aSArtem Bityutskiy struct ubifs_zbranch *zbr; 15661e51764aSArtem Bityutskiy 15671e51764aSArtem Bityutskiy if (!znode->parent) 15681e51764aSArtem Bityutskiy zbr = &c->zroot; 15691e51764aSArtem Bityutskiy else 15701e51764aSArtem Bityutskiy zbr = &znode->parent->zbranch[znode->iip]; 15711e51764aSArtem Bityutskiy 15721e51764aSArtem Bityutskiy err = dbg_check_znode(c, zbr); 15731e51764aSArtem Bityutskiy if (err) 15741e51764aSArtem Bityutskiy return err; 15751e51764aSArtem Bityutskiy 15761e51764aSArtem Bityutskiy if (extra) { 15771e51764aSArtem Bityutskiy if (ubifs_zn_dirty(znode)) 15781e51764aSArtem Bityutskiy dirty_cnt += 1; 15791e51764aSArtem Bityutskiy else 15801e51764aSArtem Bityutskiy clean_cnt += 1; 15811e51764aSArtem Bityutskiy } 15821e51764aSArtem Bityutskiy 15831e51764aSArtem Bityutskiy prev = znode; 15841e51764aSArtem Bityutskiy znode = ubifs_tnc_postorder_next(znode); 15851e51764aSArtem Bityutskiy if (!znode) 15861e51764aSArtem Bityutskiy break; 15871e51764aSArtem Bityutskiy 15881e51764aSArtem Bityutskiy /* 15891e51764aSArtem Bityutskiy * If the last key of this znode is equivalent to the first key 15901e51764aSArtem Bityutskiy * of the next znode (collision), then check order of the keys. 15911e51764aSArtem Bityutskiy */ 15921e51764aSArtem Bityutskiy last = prev->child_cnt - 1; 15931e51764aSArtem Bityutskiy if (prev->level == 0 && znode->level == 0 && !c->replaying && 15941e51764aSArtem Bityutskiy !keys_cmp(c, &prev->zbranch[last].key, 15951e51764aSArtem Bityutskiy &znode->zbranch[0].key)) { 15961e51764aSArtem Bityutskiy err = dbg_check_key_order(c, &prev->zbranch[last], 15971e51764aSArtem Bityutskiy &znode->zbranch[0]); 15981e51764aSArtem Bityutskiy if (err < 0) 15991e51764aSArtem Bityutskiy return err; 16001e51764aSArtem Bityutskiy if (err) { 16011e51764aSArtem Bityutskiy ubifs_msg("first znode"); 1602*edf6be24SArtem Bityutskiy ubifs_dump_znode(c, prev); 16031e51764aSArtem Bityutskiy ubifs_msg("second znode"); 1604*edf6be24SArtem Bityutskiy ubifs_dump_znode(c, znode); 16051e51764aSArtem Bityutskiy return -EINVAL; 16061e51764aSArtem Bityutskiy } 16071e51764aSArtem Bityutskiy } 16081e51764aSArtem Bityutskiy } 16091e51764aSArtem Bityutskiy 16101e51764aSArtem Bityutskiy if (extra) { 16111e51764aSArtem Bityutskiy if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) { 16121e51764aSArtem Bityutskiy ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld", 16131e51764aSArtem Bityutskiy atomic_long_read(&c->clean_zn_cnt), 16141e51764aSArtem Bityutskiy clean_cnt); 16151e51764aSArtem Bityutskiy return -EINVAL; 16161e51764aSArtem Bityutskiy } 16171e51764aSArtem Bityutskiy if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) { 16181e51764aSArtem Bityutskiy ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld", 16191e51764aSArtem Bityutskiy atomic_long_read(&c->dirty_zn_cnt), 16201e51764aSArtem Bityutskiy dirty_cnt); 16211e51764aSArtem Bityutskiy return -EINVAL; 16221e51764aSArtem Bityutskiy } 16231e51764aSArtem Bityutskiy } 16241e51764aSArtem Bityutskiy 16251e51764aSArtem Bityutskiy return 0; 16261e51764aSArtem Bityutskiy } 16271e51764aSArtem Bityutskiy 16281e51764aSArtem Bityutskiy /** 16291e51764aSArtem Bityutskiy * dbg_walk_index - walk the on-flash index. 16301e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 16311e51764aSArtem Bityutskiy * @leaf_cb: called for each leaf node 16321e51764aSArtem Bityutskiy * @znode_cb: called for each indexing node 1633227c75c9SAdrian Hunter * @priv: private data which is passed to callbacks 16341e51764aSArtem Bityutskiy * 16351e51764aSArtem Bityutskiy * This function walks the UBIFS index and calls the @leaf_cb for each leaf 16361e51764aSArtem Bityutskiy * node and @znode_cb for each indexing node. Returns zero in case of success 16371e51764aSArtem Bityutskiy * and a negative error code in case of failure. 16381e51764aSArtem Bityutskiy * 16391e51764aSArtem Bityutskiy * It would be better if this function removed every znode it pulled to into 16401e51764aSArtem Bityutskiy * the TNC, so that the behavior more closely matched the non-debugging 16411e51764aSArtem Bityutskiy * behavior. 16421e51764aSArtem Bityutskiy */ 16431e51764aSArtem Bityutskiy int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb, 16441e51764aSArtem Bityutskiy dbg_znode_callback znode_cb, void *priv) 16451e51764aSArtem Bityutskiy { 16461e51764aSArtem Bityutskiy int err; 16471e51764aSArtem Bityutskiy struct ubifs_zbranch *zbr; 16481e51764aSArtem Bityutskiy struct ubifs_znode *znode, *child; 16491e51764aSArtem Bityutskiy 16501e51764aSArtem Bityutskiy mutex_lock(&c->tnc_mutex); 16511e51764aSArtem Bityutskiy /* If the root indexing node is not in TNC - pull it */ 16521e51764aSArtem Bityutskiy if (!c->zroot.znode) { 16531e51764aSArtem Bityutskiy c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0); 16541e51764aSArtem Bityutskiy if (IS_ERR(c->zroot.znode)) { 16551e51764aSArtem Bityutskiy err = PTR_ERR(c->zroot.znode); 16561e51764aSArtem Bityutskiy c->zroot.znode = NULL; 16571e51764aSArtem Bityutskiy goto out_unlock; 16581e51764aSArtem Bityutskiy } 16591e51764aSArtem Bityutskiy } 16601e51764aSArtem Bityutskiy 16611e51764aSArtem Bityutskiy /* 16621e51764aSArtem Bityutskiy * We are going to traverse the indexing tree in the postorder manner. 16631e51764aSArtem Bityutskiy * Go down and find the leftmost indexing node where we are going to 16641e51764aSArtem Bityutskiy * start from. 16651e51764aSArtem Bityutskiy */ 16661e51764aSArtem Bityutskiy znode = c->zroot.znode; 16671e51764aSArtem Bityutskiy while (znode->level > 0) { 16681e51764aSArtem Bityutskiy zbr = &znode->zbranch[0]; 16691e51764aSArtem Bityutskiy child = zbr->znode; 16701e51764aSArtem Bityutskiy if (!child) { 16711e51764aSArtem Bityutskiy child = ubifs_load_znode(c, zbr, znode, 0); 16721e51764aSArtem Bityutskiy if (IS_ERR(child)) { 16731e51764aSArtem Bityutskiy err = PTR_ERR(child); 16741e51764aSArtem Bityutskiy goto out_unlock; 16751e51764aSArtem Bityutskiy } 16761e51764aSArtem Bityutskiy zbr->znode = child; 16771e51764aSArtem Bityutskiy } 16781e51764aSArtem Bityutskiy 16791e51764aSArtem Bityutskiy znode = child; 16801e51764aSArtem Bityutskiy } 16811e51764aSArtem Bityutskiy 16821e51764aSArtem Bityutskiy /* Iterate over all indexing nodes */ 16831e51764aSArtem Bityutskiy while (1) { 16841e51764aSArtem Bityutskiy int idx; 16851e51764aSArtem Bityutskiy 16861e51764aSArtem Bityutskiy cond_resched(); 16871e51764aSArtem Bityutskiy 16881e51764aSArtem Bityutskiy if (znode_cb) { 16891e51764aSArtem Bityutskiy err = znode_cb(c, znode, priv); 16901e51764aSArtem Bityutskiy if (err) { 16911e51764aSArtem Bityutskiy ubifs_err("znode checking function returned " 16921e51764aSArtem Bityutskiy "error %d", err); 1693*edf6be24SArtem Bityutskiy ubifs_dump_znode(c, znode); 16941e51764aSArtem Bityutskiy goto out_dump; 16951e51764aSArtem Bityutskiy } 16961e51764aSArtem Bityutskiy } 16971e51764aSArtem Bityutskiy if (leaf_cb && znode->level == 0) { 16981e51764aSArtem Bityutskiy for (idx = 0; idx < znode->child_cnt; idx++) { 16991e51764aSArtem Bityutskiy zbr = &znode->zbranch[idx]; 17001e51764aSArtem Bityutskiy err = leaf_cb(c, zbr, priv); 17011e51764aSArtem Bityutskiy if (err) { 17021e51764aSArtem Bityutskiy ubifs_err("leaf checking function " 17031e51764aSArtem Bityutskiy "returned error %d, for leaf " 17041e51764aSArtem Bityutskiy "at LEB %d:%d", 17051e51764aSArtem Bityutskiy err, zbr->lnum, zbr->offs); 17061e51764aSArtem Bityutskiy goto out_dump; 17071e51764aSArtem Bityutskiy } 17081e51764aSArtem Bityutskiy } 17091e51764aSArtem Bityutskiy } 17101e51764aSArtem Bityutskiy 17111e51764aSArtem Bityutskiy if (!znode->parent) 17121e51764aSArtem Bityutskiy break; 17131e51764aSArtem Bityutskiy 17141e51764aSArtem Bityutskiy idx = znode->iip + 1; 17151e51764aSArtem Bityutskiy znode = znode->parent; 17161e51764aSArtem Bityutskiy if (idx < znode->child_cnt) { 17171e51764aSArtem Bityutskiy /* Switch to the next index in the parent */ 17181e51764aSArtem Bityutskiy zbr = &znode->zbranch[idx]; 17191e51764aSArtem Bityutskiy child = zbr->znode; 17201e51764aSArtem Bityutskiy if (!child) { 17211e51764aSArtem Bityutskiy child = ubifs_load_znode(c, zbr, znode, idx); 17221e51764aSArtem Bityutskiy if (IS_ERR(child)) { 17231e51764aSArtem Bityutskiy err = PTR_ERR(child); 17241e51764aSArtem Bityutskiy goto out_unlock; 17251e51764aSArtem Bityutskiy } 17261e51764aSArtem Bityutskiy zbr->znode = child; 17271e51764aSArtem Bityutskiy } 17281e51764aSArtem Bityutskiy znode = child; 17291e51764aSArtem Bityutskiy } else 17301e51764aSArtem Bityutskiy /* 17311e51764aSArtem Bityutskiy * This is the last child, switch to the parent and 17321e51764aSArtem Bityutskiy * continue. 17331e51764aSArtem Bityutskiy */ 17341e51764aSArtem Bityutskiy continue; 17351e51764aSArtem Bityutskiy 17361e51764aSArtem Bityutskiy /* Go to the lowest leftmost znode in the new sub-tree */ 17371e51764aSArtem Bityutskiy while (znode->level > 0) { 17381e51764aSArtem Bityutskiy zbr = &znode->zbranch[0]; 17391e51764aSArtem Bityutskiy child = zbr->znode; 17401e51764aSArtem Bityutskiy if (!child) { 17411e51764aSArtem Bityutskiy child = ubifs_load_znode(c, zbr, znode, 0); 17421e51764aSArtem Bityutskiy if (IS_ERR(child)) { 17431e51764aSArtem Bityutskiy err = PTR_ERR(child); 17441e51764aSArtem Bityutskiy goto out_unlock; 17451e51764aSArtem Bityutskiy } 17461e51764aSArtem Bityutskiy zbr->znode = child; 17471e51764aSArtem Bityutskiy } 17481e51764aSArtem Bityutskiy znode = child; 17491e51764aSArtem Bityutskiy } 17501e51764aSArtem Bityutskiy } 17511e51764aSArtem Bityutskiy 17521e51764aSArtem Bityutskiy mutex_unlock(&c->tnc_mutex); 17531e51764aSArtem Bityutskiy return 0; 17541e51764aSArtem Bityutskiy 17551e51764aSArtem Bityutskiy out_dump: 17561e51764aSArtem Bityutskiy if (znode->parent) 17571e51764aSArtem Bityutskiy zbr = &znode->parent->zbranch[znode->iip]; 17581e51764aSArtem Bityutskiy else 17591e51764aSArtem Bityutskiy zbr = &c->zroot; 17601e51764aSArtem Bityutskiy ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs); 1761*edf6be24SArtem Bityutskiy ubifs_dump_znode(c, znode); 17621e51764aSArtem Bityutskiy out_unlock: 17631e51764aSArtem Bityutskiy mutex_unlock(&c->tnc_mutex); 17641e51764aSArtem Bityutskiy return err; 17651e51764aSArtem Bityutskiy } 17661e51764aSArtem Bityutskiy 17671e51764aSArtem Bityutskiy /** 17681e51764aSArtem Bityutskiy * add_size - add znode size to partially calculated index size. 17691e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 17701e51764aSArtem Bityutskiy * @znode: znode to add size for 17711e51764aSArtem Bityutskiy * @priv: partially calculated index size 17721e51764aSArtem Bityutskiy * 17731e51764aSArtem Bityutskiy * This is a helper function for 'dbg_check_idx_size()' which is called for 17741e51764aSArtem Bityutskiy * every indexing node and adds its size to the 'long long' variable pointed to 17751e51764aSArtem Bityutskiy * by @priv. 17761e51764aSArtem Bityutskiy */ 17771e51764aSArtem Bityutskiy static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv) 17781e51764aSArtem Bityutskiy { 17791e51764aSArtem Bityutskiy long long *idx_size = priv; 17801e51764aSArtem Bityutskiy int add; 17811e51764aSArtem Bityutskiy 17821e51764aSArtem Bityutskiy add = ubifs_idx_node_sz(c, znode->child_cnt); 17831e51764aSArtem Bityutskiy add = ALIGN(add, 8); 17841e51764aSArtem Bityutskiy *idx_size += add; 17851e51764aSArtem Bityutskiy return 0; 17861e51764aSArtem Bityutskiy } 17871e51764aSArtem Bityutskiy 17881e51764aSArtem Bityutskiy /** 17891e51764aSArtem Bityutskiy * dbg_check_idx_size - check index size. 17901e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 17911e51764aSArtem Bityutskiy * @idx_size: size to check 17921e51764aSArtem Bityutskiy * 17931e51764aSArtem Bityutskiy * This function walks the UBIFS index, calculates its size and checks that the 17941e51764aSArtem Bityutskiy * size is equivalent to @idx_size. Returns zero in case of success and a 17951e51764aSArtem Bityutskiy * negative error code in case of failure. 17961e51764aSArtem Bityutskiy */ 17971e51764aSArtem Bityutskiy int dbg_check_idx_size(struct ubifs_info *c, long long idx_size) 17981e51764aSArtem Bityutskiy { 17991e51764aSArtem Bityutskiy int err; 18001e51764aSArtem Bityutskiy long long calc = 0; 18011e51764aSArtem Bityutskiy 18028d7819b4SArtem Bityutskiy if (!dbg_is_chk_index(c)) 18031e51764aSArtem Bityutskiy return 0; 18041e51764aSArtem Bityutskiy 18051e51764aSArtem Bityutskiy err = dbg_walk_index(c, NULL, add_size, &calc); 18061e51764aSArtem Bityutskiy if (err) { 18071e51764aSArtem Bityutskiy ubifs_err("error %d while walking the index", err); 18081e51764aSArtem Bityutskiy return err; 18091e51764aSArtem Bityutskiy } 18101e51764aSArtem Bityutskiy 18111e51764aSArtem Bityutskiy if (calc != idx_size) { 18121e51764aSArtem Bityutskiy ubifs_err("index size check failed: calculated size is %lld, " 18131e51764aSArtem Bityutskiy "should be %lld", calc, idx_size); 18141e51764aSArtem Bityutskiy dump_stack(); 18151e51764aSArtem Bityutskiy return -EINVAL; 18161e51764aSArtem Bityutskiy } 18171e51764aSArtem Bityutskiy 18181e51764aSArtem Bityutskiy return 0; 18191e51764aSArtem Bityutskiy } 18201e51764aSArtem Bityutskiy 18211e51764aSArtem Bityutskiy /** 18221e51764aSArtem Bityutskiy * struct fsck_inode - information about an inode used when checking the file-system. 18231e51764aSArtem Bityutskiy * @rb: link in the RB-tree of inodes 18241e51764aSArtem Bityutskiy * @inum: inode number 18251e51764aSArtem Bityutskiy * @mode: inode type, permissions, etc 18261e51764aSArtem Bityutskiy * @nlink: inode link count 18271e51764aSArtem Bityutskiy * @xattr_cnt: count of extended attributes 18281e51764aSArtem Bityutskiy * @references: how many directory/xattr entries refer this inode (calculated 18291e51764aSArtem Bityutskiy * while walking the index) 18301e51764aSArtem Bityutskiy * @calc_cnt: for directory inode count of child directories 18311e51764aSArtem Bityutskiy * @size: inode size (read from on-flash inode) 18321e51764aSArtem Bityutskiy * @xattr_sz: summary size of all extended attributes (read from on-flash 18331e51764aSArtem Bityutskiy * inode) 18341e51764aSArtem Bityutskiy * @calc_sz: for directories calculated directory size 18351e51764aSArtem Bityutskiy * @calc_xcnt: count of extended attributes 18361e51764aSArtem Bityutskiy * @calc_xsz: calculated summary size of all extended attributes 18371e51764aSArtem Bityutskiy * @xattr_nms: sum of lengths of all extended attribute names belonging to this 18381e51764aSArtem Bityutskiy * inode (read from on-flash inode) 18391e51764aSArtem Bityutskiy * @calc_xnms: calculated sum of lengths of all extended attribute names 18401e51764aSArtem Bityutskiy */ 18411e51764aSArtem Bityutskiy struct fsck_inode { 18421e51764aSArtem Bityutskiy struct rb_node rb; 18431e51764aSArtem Bityutskiy ino_t inum; 18441e51764aSArtem Bityutskiy umode_t mode; 18451e51764aSArtem Bityutskiy unsigned int nlink; 18461e51764aSArtem Bityutskiy unsigned int xattr_cnt; 18471e51764aSArtem Bityutskiy int references; 18481e51764aSArtem Bityutskiy int calc_cnt; 18491e51764aSArtem Bityutskiy long long size; 18501e51764aSArtem Bityutskiy unsigned int xattr_sz; 18511e51764aSArtem Bityutskiy long long calc_sz; 18521e51764aSArtem Bityutskiy long long calc_xcnt; 18531e51764aSArtem Bityutskiy long long calc_xsz; 18541e51764aSArtem Bityutskiy unsigned int xattr_nms; 18551e51764aSArtem Bityutskiy long long calc_xnms; 18561e51764aSArtem Bityutskiy }; 18571e51764aSArtem Bityutskiy 18581e51764aSArtem Bityutskiy /** 18591e51764aSArtem Bityutskiy * struct fsck_data - private FS checking information. 18601e51764aSArtem Bityutskiy * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects) 18611e51764aSArtem Bityutskiy */ 18621e51764aSArtem Bityutskiy struct fsck_data { 18631e51764aSArtem Bityutskiy struct rb_root inodes; 18641e51764aSArtem Bityutskiy }; 18651e51764aSArtem Bityutskiy 18661e51764aSArtem Bityutskiy /** 18671e51764aSArtem Bityutskiy * add_inode - add inode information to RB-tree of inodes. 18681e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 18691e51764aSArtem Bityutskiy * @fsckd: FS checking information 18701e51764aSArtem Bityutskiy * @ino: raw UBIFS inode to add 18711e51764aSArtem Bityutskiy * 18721e51764aSArtem Bityutskiy * This is a helper function for 'check_leaf()' which adds information about 18731e51764aSArtem Bityutskiy * inode @ino to the RB-tree of inodes. Returns inode information pointer in 18741e51764aSArtem Bityutskiy * case of success and a negative error code in case of failure. 18751e51764aSArtem Bityutskiy */ 18761e51764aSArtem Bityutskiy static struct fsck_inode *add_inode(struct ubifs_info *c, 18771e51764aSArtem Bityutskiy struct fsck_data *fsckd, 18781e51764aSArtem Bityutskiy struct ubifs_ino_node *ino) 18791e51764aSArtem Bityutskiy { 18801e51764aSArtem Bityutskiy struct rb_node **p, *parent = NULL; 18811e51764aSArtem Bityutskiy struct fsck_inode *fscki; 18821e51764aSArtem Bityutskiy ino_t inum = key_inum_flash(c, &ino->key); 188345cd5cddSArtem Bityutskiy struct inode *inode; 188445cd5cddSArtem Bityutskiy struct ubifs_inode *ui; 18851e51764aSArtem Bityutskiy 18861e51764aSArtem Bityutskiy p = &fsckd->inodes.rb_node; 18871e51764aSArtem Bityutskiy while (*p) { 18881e51764aSArtem Bityutskiy parent = *p; 18891e51764aSArtem Bityutskiy fscki = rb_entry(parent, struct fsck_inode, rb); 18901e51764aSArtem Bityutskiy if (inum < fscki->inum) 18911e51764aSArtem Bityutskiy p = &(*p)->rb_left; 18921e51764aSArtem Bityutskiy else if (inum > fscki->inum) 18931e51764aSArtem Bityutskiy p = &(*p)->rb_right; 18941e51764aSArtem Bityutskiy else 18951e51764aSArtem Bityutskiy return fscki; 18961e51764aSArtem Bityutskiy } 18971e51764aSArtem Bityutskiy 18981e51764aSArtem Bityutskiy if (inum > c->highest_inum) { 18991e51764aSArtem Bityutskiy ubifs_err("too high inode number, max. is %lu", 1900e84461adSArtem Bityutskiy (unsigned long)c->highest_inum); 19011e51764aSArtem Bityutskiy return ERR_PTR(-EINVAL); 19021e51764aSArtem Bityutskiy } 19031e51764aSArtem Bityutskiy 19041e51764aSArtem Bityutskiy fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS); 19051e51764aSArtem Bityutskiy if (!fscki) 19061e51764aSArtem Bityutskiy return ERR_PTR(-ENOMEM); 19071e51764aSArtem Bityutskiy 190845cd5cddSArtem Bityutskiy inode = ilookup(c->vfs_sb, inum); 190945cd5cddSArtem Bityutskiy 19101e51764aSArtem Bityutskiy fscki->inum = inum; 191145cd5cddSArtem Bityutskiy /* 191245cd5cddSArtem Bityutskiy * If the inode is present in the VFS inode cache, use it instead of 191345cd5cddSArtem Bityutskiy * the on-flash inode which might be out-of-date. E.g., the size might 191445cd5cddSArtem Bityutskiy * be out-of-date. If we do not do this, the following may happen, for 191545cd5cddSArtem Bityutskiy * example: 191645cd5cddSArtem Bityutskiy * 1. A power cut happens 191745cd5cddSArtem Bityutskiy * 2. We mount the file-system R/O, the replay process fixes up the 191845cd5cddSArtem Bityutskiy * inode size in the VFS cache, but on on-flash. 191945cd5cddSArtem Bityutskiy * 3. 'check_leaf()' fails because it hits a data node beyond inode 192045cd5cddSArtem Bityutskiy * size. 192145cd5cddSArtem Bityutskiy */ 192245cd5cddSArtem Bityutskiy if (!inode) { 19231e51764aSArtem Bityutskiy fscki->nlink = le32_to_cpu(ino->nlink); 19241e51764aSArtem Bityutskiy fscki->size = le64_to_cpu(ino->size); 19251e51764aSArtem Bityutskiy fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt); 19261e51764aSArtem Bityutskiy fscki->xattr_sz = le32_to_cpu(ino->xattr_size); 19271e51764aSArtem Bityutskiy fscki->xattr_nms = le32_to_cpu(ino->xattr_names); 19281e51764aSArtem Bityutskiy fscki->mode = le32_to_cpu(ino->mode); 192945cd5cddSArtem Bityutskiy } else { 193045cd5cddSArtem Bityutskiy ui = ubifs_inode(inode); 193145cd5cddSArtem Bityutskiy fscki->nlink = inode->i_nlink; 193245cd5cddSArtem Bityutskiy fscki->size = inode->i_size; 193345cd5cddSArtem Bityutskiy fscki->xattr_cnt = ui->xattr_cnt; 193445cd5cddSArtem Bityutskiy fscki->xattr_sz = ui->xattr_size; 193545cd5cddSArtem Bityutskiy fscki->xattr_nms = ui->xattr_names; 193645cd5cddSArtem Bityutskiy fscki->mode = inode->i_mode; 193745cd5cddSArtem Bityutskiy iput(inode); 193845cd5cddSArtem Bityutskiy } 193945cd5cddSArtem Bityutskiy 19401e51764aSArtem Bityutskiy if (S_ISDIR(fscki->mode)) { 19411e51764aSArtem Bityutskiy fscki->calc_sz = UBIFS_INO_NODE_SZ; 19421e51764aSArtem Bityutskiy fscki->calc_cnt = 2; 19431e51764aSArtem Bityutskiy } 194445cd5cddSArtem Bityutskiy 19451e51764aSArtem Bityutskiy rb_link_node(&fscki->rb, parent, p); 19461e51764aSArtem Bityutskiy rb_insert_color(&fscki->rb, &fsckd->inodes); 194745cd5cddSArtem Bityutskiy 19481e51764aSArtem Bityutskiy return fscki; 19491e51764aSArtem Bityutskiy } 19501e51764aSArtem Bityutskiy 19511e51764aSArtem Bityutskiy /** 19521e51764aSArtem Bityutskiy * search_inode - search inode in the RB-tree of inodes. 19531e51764aSArtem Bityutskiy * @fsckd: FS checking information 19541e51764aSArtem Bityutskiy * @inum: inode number to search 19551e51764aSArtem Bityutskiy * 19561e51764aSArtem Bityutskiy * This is a helper function for 'check_leaf()' which searches inode @inum in 19571e51764aSArtem Bityutskiy * the RB-tree of inodes and returns an inode information pointer or %NULL if 19581e51764aSArtem Bityutskiy * the inode was not found. 19591e51764aSArtem Bityutskiy */ 19601e51764aSArtem Bityutskiy static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum) 19611e51764aSArtem Bityutskiy { 19621e51764aSArtem Bityutskiy struct rb_node *p; 19631e51764aSArtem Bityutskiy struct fsck_inode *fscki; 19641e51764aSArtem Bityutskiy 19651e51764aSArtem Bityutskiy p = fsckd->inodes.rb_node; 19661e51764aSArtem Bityutskiy while (p) { 19671e51764aSArtem Bityutskiy fscki = rb_entry(p, struct fsck_inode, rb); 19681e51764aSArtem Bityutskiy if (inum < fscki->inum) 19691e51764aSArtem Bityutskiy p = p->rb_left; 19701e51764aSArtem Bityutskiy else if (inum > fscki->inum) 19711e51764aSArtem Bityutskiy p = p->rb_right; 19721e51764aSArtem Bityutskiy else 19731e51764aSArtem Bityutskiy return fscki; 19741e51764aSArtem Bityutskiy } 19751e51764aSArtem Bityutskiy return NULL; 19761e51764aSArtem Bityutskiy } 19771e51764aSArtem Bityutskiy 19781e51764aSArtem Bityutskiy /** 19791e51764aSArtem Bityutskiy * read_add_inode - read inode node and add it to RB-tree of inodes. 19801e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 19811e51764aSArtem Bityutskiy * @fsckd: FS checking information 19821e51764aSArtem Bityutskiy * @inum: inode number to read 19831e51764aSArtem Bityutskiy * 19841e51764aSArtem Bityutskiy * This is a helper function for 'check_leaf()' which finds inode node @inum in 19851e51764aSArtem Bityutskiy * the index, reads it, and adds it to the RB-tree of inodes. Returns inode 19861e51764aSArtem Bityutskiy * information pointer in case of success and a negative error code in case of 19871e51764aSArtem Bityutskiy * failure. 19881e51764aSArtem Bityutskiy */ 19891e51764aSArtem Bityutskiy static struct fsck_inode *read_add_inode(struct ubifs_info *c, 19901e51764aSArtem Bityutskiy struct fsck_data *fsckd, ino_t inum) 19911e51764aSArtem Bityutskiy { 19921e51764aSArtem Bityutskiy int n, err; 19931e51764aSArtem Bityutskiy union ubifs_key key; 19941e51764aSArtem Bityutskiy struct ubifs_znode *znode; 19951e51764aSArtem Bityutskiy struct ubifs_zbranch *zbr; 19961e51764aSArtem Bityutskiy struct ubifs_ino_node *ino; 19971e51764aSArtem Bityutskiy struct fsck_inode *fscki; 19981e51764aSArtem Bityutskiy 19991e51764aSArtem Bityutskiy fscki = search_inode(fsckd, inum); 20001e51764aSArtem Bityutskiy if (fscki) 20011e51764aSArtem Bityutskiy return fscki; 20021e51764aSArtem Bityutskiy 20031e51764aSArtem Bityutskiy ino_key_init(c, &key, inum); 20041e51764aSArtem Bityutskiy err = ubifs_lookup_level0(c, &key, &znode, &n); 20051e51764aSArtem Bityutskiy if (!err) { 2006e84461adSArtem Bityutskiy ubifs_err("inode %lu not found in index", (unsigned long)inum); 20071e51764aSArtem Bityutskiy return ERR_PTR(-ENOENT); 20081e51764aSArtem Bityutskiy } else if (err < 0) { 2009e84461adSArtem Bityutskiy ubifs_err("error %d while looking up inode %lu", 2010e84461adSArtem Bityutskiy err, (unsigned long)inum); 20111e51764aSArtem Bityutskiy return ERR_PTR(err); 20121e51764aSArtem Bityutskiy } 20131e51764aSArtem Bityutskiy 20141e51764aSArtem Bityutskiy zbr = &znode->zbranch[n]; 20151e51764aSArtem Bityutskiy if (zbr->len < UBIFS_INO_NODE_SZ) { 2016e84461adSArtem Bityutskiy ubifs_err("bad node %lu node length %d", 2017e84461adSArtem Bityutskiy (unsigned long)inum, zbr->len); 20181e51764aSArtem Bityutskiy return ERR_PTR(-EINVAL); 20191e51764aSArtem Bityutskiy } 20201e51764aSArtem Bityutskiy 20211e51764aSArtem Bityutskiy ino = kmalloc(zbr->len, GFP_NOFS); 20221e51764aSArtem Bityutskiy if (!ino) 20231e51764aSArtem Bityutskiy return ERR_PTR(-ENOMEM); 20241e51764aSArtem Bityutskiy 20251e51764aSArtem Bityutskiy err = ubifs_tnc_read_node(c, zbr, ino); 20261e51764aSArtem Bityutskiy if (err) { 20271e51764aSArtem Bityutskiy ubifs_err("cannot read inode node at LEB %d:%d, error %d", 20281e51764aSArtem Bityutskiy zbr->lnum, zbr->offs, err); 20291e51764aSArtem Bityutskiy kfree(ino); 20301e51764aSArtem Bityutskiy return ERR_PTR(err); 20311e51764aSArtem Bityutskiy } 20321e51764aSArtem Bityutskiy 20331e51764aSArtem Bityutskiy fscki = add_inode(c, fsckd, ino); 20341e51764aSArtem Bityutskiy kfree(ino); 20351e51764aSArtem Bityutskiy if (IS_ERR(fscki)) { 20361e51764aSArtem Bityutskiy ubifs_err("error %ld while adding inode %lu node", 2037e84461adSArtem Bityutskiy PTR_ERR(fscki), (unsigned long)inum); 20381e51764aSArtem Bityutskiy return fscki; 20391e51764aSArtem Bityutskiy } 20401e51764aSArtem Bityutskiy 20411e51764aSArtem Bityutskiy return fscki; 20421e51764aSArtem Bityutskiy } 20431e51764aSArtem Bityutskiy 20441e51764aSArtem Bityutskiy /** 20451e51764aSArtem Bityutskiy * check_leaf - check leaf node. 20461e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 20471e51764aSArtem Bityutskiy * @zbr: zbranch of the leaf node to check 20481e51764aSArtem Bityutskiy * @priv: FS checking information 20491e51764aSArtem Bityutskiy * 20501e51764aSArtem Bityutskiy * This is a helper function for 'dbg_check_filesystem()' which is called for 20511e51764aSArtem Bityutskiy * every single leaf node while walking the indexing tree. It checks that the 20521e51764aSArtem Bityutskiy * leaf node referred from the indexing tree exists, has correct CRC, and does 20531e51764aSArtem Bityutskiy * some other basic validation. This function is also responsible for building 20541e51764aSArtem Bityutskiy * an RB-tree of inodes - it adds all inodes into the RB-tree. It also 20551e51764aSArtem Bityutskiy * calculates reference count, size, etc for each inode in order to later 20561e51764aSArtem Bityutskiy * compare them to the information stored inside the inodes and detect possible 20571e51764aSArtem Bityutskiy * inconsistencies. Returns zero in case of success and a negative error code 20581e51764aSArtem Bityutskiy * in case of failure. 20591e51764aSArtem Bityutskiy */ 20601e51764aSArtem Bityutskiy static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr, 20611e51764aSArtem Bityutskiy void *priv) 20621e51764aSArtem Bityutskiy { 20631e51764aSArtem Bityutskiy ino_t inum; 20641e51764aSArtem Bityutskiy void *node; 20651e51764aSArtem Bityutskiy struct ubifs_ch *ch; 20661e51764aSArtem Bityutskiy int err, type = key_type(c, &zbr->key); 20671e51764aSArtem Bityutskiy struct fsck_inode *fscki; 20681e51764aSArtem Bityutskiy 20691e51764aSArtem Bityutskiy if (zbr->len < UBIFS_CH_SZ) { 20701e51764aSArtem Bityutskiy ubifs_err("bad leaf length %d (LEB %d:%d)", 20711e51764aSArtem Bityutskiy zbr->len, zbr->lnum, zbr->offs); 20721e51764aSArtem Bityutskiy return -EINVAL; 20731e51764aSArtem Bityutskiy } 20741e51764aSArtem Bityutskiy 20751e51764aSArtem Bityutskiy node = kmalloc(zbr->len, GFP_NOFS); 20761e51764aSArtem Bityutskiy if (!node) 20771e51764aSArtem Bityutskiy return -ENOMEM; 20781e51764aSArtem Bityutskiy 20791e51764aSArtem Bityutskiy err = ubifs_tnc_read_node(c, zbr, node); 20801e51764aSArtem Bityutskiy if (err) { 20811e51764aSArtem Bityutskiy ubifs_err("cannot read leaf node at LEB %d:%d, error %d", 20821e51764aSArtem Bityutskiy zbr->lnum, zbr->offs, err); 20831e51764aSArtem Bityutskiy goto out_free; 20841e51764aSArtem Bityutskiy } 20851e51764aSArtem Bityutskiy 20861e51764aSArtem Bityutskiy /* If this is an inode node, add it to RB-tree of inodes */ 20871e51764aSArtem Bityutskiy if (type == UBIFS_INO_KEY) { 20881e51764aSArtem Bityutskiy fscki = add_inode(c, priv, node); 20891e51764aSArtem Bityutskiy if (IS_ERR(fscki)) { 20901e51764aSArtem Bityutskiy err = PTR_ERR(fscki); 20911e51764aSArtem Bityutskiy ubifs_err("error %d while adding inode node", err); 20921e51764aSArtem Bityutskiy goto out_dump; 20931e51764aSArtem Bityutskiy } 20941e51764aSArtem Bityutskiy goto out; 20951e51764aSArtem Bityutskiy } 20961e51764aSArtem Bityutskiy 20971e51764aSArtem Bityutskiy if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY && 20981e51764aSArtem Bityutskiy type != UBIFS_DATA_KEY) { 20991e51764aSArtem Bityutskiy ubifs_err("unexpected node type %d at LEB %d:%d", 21001e51764aSArtem Bityutskiy type, zbr->lnum, zbr->offs); 21011e51764aSArtem Bityutskiy err = -EINVAL; 21021e51764aSArtem Bityutskiy goto out_free; 21031e51764aSArtem Bityutskiy } 21041e51764aSArtem Bityutskiy 21051e51764aSArtem Bityutskiy ch = node; 21061e51764aSArtem Bityutskiy if (le64_to_cpu(ch->sqnum) > c->max_sqnum) { 21071e51764aSArtem Bityutskiy ubifs_err("too high sequence number, max. is %llu", 21081e51764aSArtem Bityutskiy c->max_sqnum); 21091e51764aSArtem Bityutskiy err = -EINVAL; 21101e51764aSArtem Bityutskiy goto out_dump; 21111e51764aSArtem Bityutskiy } 21121e51764aSArtem Bityutskiy 21131e51764aSArtem Bityutskiy if (type == UBIFS_DATA_KEY) { 21141e51764aSArtem Bityutskiy long long blk_offs; 21151e51764aSArtem Bityutskiy struct ubifs_data_node *dn = node; 21161e51764aSArtem Bityutskiy 21171e51764aSArtem Bityutskiy /* 21181e51764aSArtem Bityutskiy * Search the inode node this data node belongs to and insert 21191e51764aSArtem Bityutskiy * it to the RB-tree of inodes. 21201e51764aSArtem Bityutskiy */ 21211e51764aSArtem Bityutskiy inum = key_inum_flash(c, &dn->key); 21221e51764aSArtem Bityutskiy fscki = read_add_inode(c, priv, inum); 21231e51764aSArtem Bityutskiy if (IS_ERR(fscki)) { 21241e51764aSArtem Bityutskiy err = PTR_ERR(fscki); 21251e51764aSArtem Bityutskiy ubifs_err("error %d while processing data node and " 2126e84461adSArtem Bityutskiy "trying to find inode node %lu", 2127e84461adSArtem Bityutskiy err, (unsigned long)inum); 21281e51764aSArtem Bityutskiy goto out_dump; 21291e51764aSArtem Bityutskiy } 21301e51764aSArtem Bityutskiy 21311e51764aSArtem Bityutskiy /* Make sure the data node is within inode size */ 21321e51764aSArtem Bityutskiy blk_offs = key_block_flash(c, &dn->key); 21331e51764aSArtem Bityutskiy blk_offs <<= UBIFS_BLOCK_SHIFT; 21341e51764aSArtem Bityutskiy blk_offs += le32_to_cpu(dn->size); 21351e51764aSArtem Bityutskiy if (blk_offs > fscki->size) { 21361e51764aSArtem Bityutskiy ubifs_err("data node at LEB %d:%d is not within inode " 21371e51764aSArtem Bityutskiy "size %lld", zbr->lnum, zbr->offs, 21381e51764aSArtem Bityutskiy fscki->size); 21391e51764aSArtem Bityutskiy err = -EINVAL; 21401e51764aSArtem Bityutskiy goto out_dump; 21411e51764aSArtem Bityutskiy } 21421e51764aSArtem Bityutskiy } else { 21431e51764aSArtem Bityutskiy int nlen; 21441e51764aSArtem Bityutskiy struct ubifs_dent_node *dent = node; 21451e51764aSArtem Bityutskiy struct fsck_inode *fscki1; 21461e51764aSArtem Bityutskiy 21471e51764aSArtem Bityutskiy err = ubifs_validate_entry(c, dent); 21481e51764aSArtem Bityutskiy if (err) 21491e51764aSArtem Bityutskiy goto out_dump; 21501e51764aSArtem Bityutskiy 21511e51764aSArtem Bityutskiy /* 21521e51764aSArtem Bityutskiy * Search the inode node this entry refers to and the parent 21531e51764aSArtem Bityutskiy * inode node and insert them to the RB-tree of inodes. 21541e51764aSArtem Bityutskiy */ 21551e51764aSArtem Bityutskiy inum = le64_to_cpu(dent->inum); 21561e51764aSArtem Bityutskiy fscki = read_add_inode(c, priv, inum); 21571e51764aSArtem Bityutskiy if (IS_ERR(fscki)) { 21581e51764aSArtem Bityutskiy err = PTR_ERR(fscki); 21591e51764aSArtem Bityutskiy ubifs_err("error %d while processing entry node and " 2160e84461adSArtem Bityutskiy "trying to find inode node %lu", 2161e84461adSArtem Bityutskiy err, (unsigned long)inum); 21621e51764aSArtem Bityutskiy goto out_dump; 21631e51764aSArtem Bityutskiy } 21641e51764aSArtem Bityutskiy 21651e51764aSArtem Bityutskiy /* Count how many direntries or xentries refers this inode */ 21661e51764aSArtem Bityutskiy fscki->references += 1; 21671e51764aSArtem Bityutskiy 21681e51764aSArtem Bityutskiy inum = key_inum_flash(c, &dent->key); 21691e51764aSArtem Bityutskiy fscki1 = read_add_inode(c, priv, inum); 21701e51764aSArtem Bityutskiy if (IS_ERR(fscki1)) { 2171b38882f5SRoel Kluin err = PTR_ERR(fscki1); 21721e51764aSArtem Bityutskiy ubifs_err("error %d while processing entry node and " 21731e51764aSArtem Bityutskiy "trying to find parent inode node %lu", 2174e84461adSArtem Bityutskiy err, (unsigned long)inum); 21751e51764aSArtem Bityutskiy goto out_dump; 21761e51764aSArtem Bityutskiy } 21771e51764aSArtem Bityutskiy 21781e51764aSArtem Bityutskiy nlen = le16_to_cpu(dent->nlen); 21791e51764aSArtem Bityutskiy if (type == UBIFS_XENT_KEY) { 21801e51764aSArtem Bityutskiy fscki1->calc_xcnt += 1; 21811e51764aSArtem Bityutskiy fscki1->calc_xsz += CALC_DENT_SIZE(nlen); 21821e51764aSArtem Bityutskiy fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size); 21831e51764aSArtem Bityutskiy fscki1->calc_xnms += nlen; 21841e51764aSArtem Bityutskiy } else { 21851e51764aSArtem Bityutskiy fscki1->calc_sz += CALC_DENT_SIZE(nlen); 21861e51764aSArtem Bityutskiy if (dent->type == UBIFS_ITYPE_DIR) 21871e51764aSArtem Bityutskiy fscki1->calc_cnt += 1; 21881e51764aSArtem Bityutskiy } 21891e51764aSArtem Bityutskiy } 21901e51764aSArtem Bityutskiy 21911e51764aSArtem Bityutskiy out: 21921e51764aSArtem Bityutskiy kfree(node); 21931e51764aSArtem Bityutskiy return 0; 21941e51764aSArtem Bityutskiy 21951e51764aSArtem Bityutskiy out_dump: 21961e51764aSArtem Bityutskiy ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs); 2197*edf6be24SArtem Bityutskiy ubifs_dump_node(c, node); 21981e51764aSArtem Bityutskiy out_free: 21991e51764aSArtem Bityutskiy kfree(node); 22001e51764aSArtem Bityutskiy return err; 22011e51764aSArtem Bityutskiy } 22021e51764aSArtem Bityutskiy 22031e51764aSArtem Bityutskiy /** 22041e51764aSArtem Bityutskiy * free_inodes - free RB-tree of inodes. 22051e51764aSArtem Bityutskiy * @fsckd: FS checking information 22061e51764aSArtem Bityutskiy */ 22071e51764aSArtem Bityutskiy static void free_inodes(struct fsck_data *fsckd) 22081e51764aSArtem Bityutskiy { 22091e51764aSArtem Bityutskiy struct rb_node *this = fsckd->inodes.rb_node; 22101e51764aSArtem Bityutskiy struct fsck_inode *fscki; 22111e51764aSArtem Bityutskiy 22121e51764aSArtem Bityutskiy while (this) { 22131e51764aSArtem Bityutskiy if (this->rb_left) 22141e51764aSArtem Bityutskiy this = this->rb_left; 22151e51764aSArtem Bityutskiy else if (this->rb_right) 22161e51764aSArtem Bityutskiy this = this->rb_right; 22171e51764aSArtem Bityutskiy else { 22181e51764aSArtem Bityutskiy fscki = rb_entry(this, struct fsck_inode, rb); 22191e51764aSArtem Bityutskiy this = rb_parent(this); 22201e51764aSArtem Bityutskiy if (this) { 22211e51764aSArtem Bityutskiy if (this->rb_left == &fscki->rb) 22221e51764aSArtem Bityutskiy this->rb_left = NULL; 22231e51764aSArtem Bityutskiy else 22241e51764aSArtem Bityutskiy this->rb_right = NULL; 22251e51764aSArtem Bityutskiy } 22261e51764aSArtem Bityutskiy kfree(fscki); 22271e51764aSArtem Bityutskiy } 22281e51764aSArtem Bityutskiy } 22291e51764aSArtem Bityutskiy } 22301e51764aSArtem Bityutskiy 22311e51764aSArtem Bityutskiy /** 22321e51764aSArtem Bityutskiy * check_inodes - checks all inodes. 22331e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 22341e51764aSArtem Bityutskiy * @fsckd: FS checking information 22351e51764aSArtem Bityutskiy * 22361e51764aSArtem Bityutskiy * This is a helper function for 'dbg_check_filesystem()' which walks the 22371e51764aSArtem Bityutskiy * RB-tree of inodes after the index scan has been finished, and checks that 22381e51764aSArtem Bityutskiy * inode nlink, size, etc are correct. Returns zero if inodes are fine, 22391e51764aSArtem Bityutskiy * %-EINVAL if not, and a negative error code in case of failure. 22401e51764aSArtem Bityutskiy */ 22411e51764aSArtem Bityutskiy static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd) 22421e51764aSArtem Bityutskiy { 22431e51764aSArtem Bityutskiy int n, err; 22441e51764aSArtem Bityutskiy union ubifs_key key; 22451e51764aSArtem Bityutskiy struct ubifs_znode *znode; 22461e51764aSArtem Bityutskiy struct ubifs_zbranch *zbr; 22471e51764aSArtem Bityutskiy struct ubifs_ino_node *ino; 22481e51764aSArtem Bityutskiy struct fsck_inode *fscki; 22491e51764aSArtem Bityutskiy struct rb_node *this = rb_first(&fsckd->inodes); 22501e51764aSArtem Bityutskiy 22511e51764aSArtem Bityutskiy while (this) { 22521e51764aSArtem Bityutskiy fscki = rb_entry(this, struct fsck_inode, rb); 22531e51764aSArtem Bityutskiy this = rb_next(this); 22541e51764aSArtem Bityutskiy 22551e51764aSArtem Bityutskiy if (S_ISDIR(fscki->mode)) { 22561e51764aSArtem Bityutskiy /* 22571e51764aSArtem Bityutskiy * Directories have to have exactly one reference (they 22581e51764aSArtem Bityutskiy * cannot have hardlinks), although root inode is an 22591e51764aSArtem Bityutskiy * exception. 22601e51764aSArtem Bityutskiy */ 22611e51764aSArtem Bityutskiy if (fscki->inum != UBIFS_ROOT_INO && 22621e51764aSArtem Bityutskiy fscki->references != 1) { 22631e51764aSArtem Bityutskiy ubifs_err("directory inode %lu has %d " 22641e51764aSArtem Bityutskiy "direntries which refer it, but " 2265e84461adSArtem Bityutskiy "should be 1", 2266e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 22671e51764aSArtem Bityutskiy fscki->references); 22681e51764aSArtem Bityutskiy goto out_dump; 22691e51764aSArtem Bityutskiy } 22701e51764aSArtem Bityutskiy if (fscki->inum == UBIFS_ROOT_INO && 22711e51764aSArtem Bityutskiy fscki->references != 0) { 22721e51764aSArtem Bityutskiy ubifs_err("root inode %lu has non-zero (%d) " 22731e51764aSArtem Bityutskiy "direntries which refer it", 2274e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 2275e84461adSArtem Bityutskiy fscki->references); 22761e51764aSArtem Bityutskiy goto out_dump; 22771e51764aSArtem Bityutskiy } 22781e51764aSArtem Bityutskiy if (fscki->calc_sz != fscki->size) { 22791e51764aSArtem Bityutskiy ubifs_err("directory inode %lu size is %lld, " 22801e51764aSArtem Bityutskiy "but calculated size is %lld", 2281e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 2282e84461adSArtem Bityutskiy fscki->size, fscki->calc_sz); 22831e51764aSArtem Bityutskiy goto out_dump; 22841e51764aSArtem Bityutskiy } 22851e51764aSArtem Bityutskiy if (fscki->calc_cnt != fscki->nlink) { 22861e51764aSArtem Bityutskiy ubifs_err("directory inode %lu nlink is %d, " 22871e51764aSArtem Bityutskiy "but calculated nlink is %d", 2288e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 2289e84461adSArtem Bityutskiy fscki->nlink, fscki->calc_cnt); 22901e51764aSArtem Bityutskiy goto out_dump; 22911e51764aSArtem Bityutskiy } 22921e51764aSArtem Bityutskiy } else { 22931e51764aSArtem Bityutskiy if (fscki->references != fscki->nlink) { 22941e51764aSArtem Bityutskiy ubifs_err("inode %lu nlink is %d, but " 2295e84461adSArtem Bityutskiy "calculated nlink is %d", 2296e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 22971e51764aSArtem Bityutskiy fscki->nlink, fscki->references); 22981e51764aSArtem Bityutskiy goto out_dump; 22991e51764aSArtem Bityutskiy } 23001e51764aSArtem Bityutskiy } 23011e51764aSArtem Bityutskiy if (fscki->xattr_sz != fscki->calc_xsz) { 23021e51764aSArtem Bityutskiy ubifs_err("inode %lu has xattr size %u, but " 23031e51764aSArtem Bityutskiy "calculated size is %lld", 2304e84461adSArtem Bityutskiy (unsigned long)fscki->inum, fscki->xattr_sz, 23051e51764aSArtem Bityutskiy fscki->calc_xsz); 23061e51764aSArtem Bityutskiy goto out_dump; 23071e51764aSArtem Bityutskiy } 23081e51764aSArtem Bityutskiy if (fscki->xattr_cnt != fscki->calc_xcnt) { 23091e51764aSArtem Bityutskiy ubifs_err("inode %lu has %u xattrs, but " 2310e84461adSArtem Bityutskiy "calculated count is %lld", 2311e84461adSArtem Bityutskiy (unsigned long)fscki->inum, 23121e51764aSArtem Bityutskiy fscki->xattr_cnt, fscki->calc_xcnt); 23131e51764aSArtem Bityutskiy goto out_dump; 23141e51764aSArtem Bityutskiy } 23151e51764aSArtem Bityutskiy if (fscki->xattr_nms != fscki->calc_xnms) { 23161e51764aSArtem Bityutskiy ubifs_err("inode %lu has xattr names' size %u, but " 23171e51764aSArtem Bityutskiy "calculated names' size is %lld", 2318e84461adSArtem Bityutskiy (unsigned long)fscki->inum, fscki->xattr_nms, 23191e51764aSArtem Bityutskiy fscki->calc_xnms); 23201e51764aSArtem Bityutskiy goto out_dump; 23211e51764aSArtem Bityutskiy } 23221e51764aSArtem Bityutskiy } 23231e51764aSArtem Bityutskiy 23241e51764aSArtem Bityutskiy return 0; 23251e51764aSArtem Bityutskiy 23261e51764aSArtem Bityutskiy out_dump: 23271e51764aSArtem Bityutskiy /* Read the bad inode and dump it */ 23281e51764aSArtem Bityutskiy ino_key_init(c, &key, fscki->inum); 23291e51764aSArtem Bityutskiy err = ubifs_lookup_level0(c, &key, &znode, &n); 23301e51764aSArtem Bityutskiy if (!err) { 2331e84461adSArtem Bityutskiy ubifs_err("inode %lu not found in index", 2332e84461adSArtem Bityutskiy (unsigned long)fscki->inum); 23331e51764aSArtem Bityutskiy return -ENOENT; 23341e51764aSArtem Bityutskiy } else if (err < 0) { 23351e51764aSArtem Bityutskiy ubifs_err("error %d while looking up inode %lu", 2336e84461adSArtem Bityutskiy err, (unsigned long)fscki->inum); 23371e51764aSArtem Bityutskiy return err; 23381e51764aSArtem Bityutskiy } 23391e51764aSArtem Bityutskiy 23401e51764aSArtem Bityutskiy zbr = &znode->zbranch[n]; 23411e51764aSArtem Bityutskiy ino = kmalloc(zbr->len, GFP_NOFS); 23421e51764aSArtem Bityutskiy if (!ino) 23431e51764aSArtem Bityutskiy return -ENOMEM; 23441e51764aSArtem Bityutskiy 23451e51764aSArtem Bityutskiy err = ubifs_tnc_read_node(c, zbr, ino); 23461e51764aSArtem Bityutskiy if (err) { 23471e51764aSArtem Bityutskiy ubifs_err("cannot read inode node at LEB %d:%d, error %d", 23481e51764aSArtem Bityutskiy zbr->lnum, zbr->offs, err); 23491e51764aSArtem Bityutskiy kfree(ino); 23501e51764aSArtem Bityutskiy return err; 23511e51764aSArtem Bityutskiy } 23521e51764aSArtem Bityutskiy 23531e51764aSArtem Bityutskiy ubifs_msg("dump of the inode %lu sitting in LEB %d:%d", 2354e84461adSArtem Bityutskiy (unsigned long)fscki->inum, zbr->lnum, zbr->offs); 2355*edf6be24SArtem Bityutskiy ubifs_dump_node(c, ino); 23561e51764aSArtem Bityutskiy kfree(ino); 23571e51764aSArtem Bityutskiy return -EINVAL; 23581e51764aSArtem Bityutskiy } 23591e51764aSArtem Bityutskiy 23601e51764aSArtem Bityutskiy /** 23611e51764aSArtem Bityutskiy * dbg_check_filesystem - check the file-system. 23621e51764aSArtem Bityutskiy * @c: UBIFS file-system description object 23631e51764aSArtem Bityutskiy * 23641e51764aSArtem Bityutskiy * This function checks the file system, namely: 23651e51764aSArtem Bityutskiy * o makes sure that all leaf nodes exist and their CRCs are correct; 23661e51764aSArtem Bityutskiy * o makes sure inode nlink, size, xattr size/count are correct (for all 23671e51764aSArtem Bityutskiy * inodes). 23681e51764aSArtem Bityutskiy * 23691e51764aSArtem Bityutskiy * The function reads whole indexing tree and all nodes, so it is pretty 23701e51764aSArtem Bityutskiy * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if 23711e51764aSArtem Bityutskiy * not, and a negative error code in case of failure. 23721e51764aSArtem Bityutskiy */ 23731e51764aSArtem Bityutskiy int dbg_check_filesystem(struct ubifs_info *c) 23741e51764aSArtem Bityutskiy { 23751e51764aSArtem Bityutskiy int err; 23761e51764aSArtem Bityutskiy struct fsck_data fsckd; 23771e51764aSArtem Bityutskiy 23782b1844a8SArtem Bityutskiy if (!dbg_is_chk_fs(c)) 23791e51764aSArtem Bityutskiy return 0; 23801e51764aSArtem Bityutskiy 23811e51764aSArtem Bityutskiy fsckd.inodes = RB_ROOT; 23821e51764aSArtem Bityutskiy err = dbg_walk_index(c, check_leaf, NULL, &fsckd); 23831e51764aSArtem Bityutskiy if (err) 23841e51764aSArtem Bityutskiy goto out_free; 23851e51764aSArtem Bityutskiy 23861e51764aSArtem Bityutskiy err = check_inodes(c, &fsckd); 23871e51764aSArtem Bityutskiy if (err) 23881e51764aSArtem Bityutskiy goto out_free; 23891e51764aSArtem Bityutskiy 23901e51764aSArtem Bityutskiy free_inodes(&fsckd); 23911e51764aSArtem Bityutskiy return 0; 23921e51764aSArtem Bityutskiy 23931e51764aSArtem Bityutskiy out_free: 23941e51764aSArtem Bityutskiy ubifs_err("file-system check failed with error %d", err); 23951e51764aSArtem Bityutskiy dump_stack(); 23961e51764aSArtem Bityutskiy free_inodes(&fsckd); 23971e51764aSArtem Bityutskiy return err; 23981e51764aSArtem Bityutskiy } 23991e51764aSArtem Bityutskiy 24003bb66b47SArtem Bityutskiy /** 24013bb66b47SArtem Bityutskiy * dbg_check_data_nodes_order - check that list of data nodes is sorted. 24023bb66b47SArtem Bityutskiy * @c: UBIFS file-system description object 24033bb66b47SArtem Bityutskiy * @head: the list of nodes ('struct ubifs_scan_node' objects) 24043bb66b47SArtem Bityutskiy * 24053bb66b47SArtem Bityutskiy * This function returns zero if the list of data nodes is sorted correctly, 24063bb66b47SArtem Bityutskiy * and %-EINVAL if not. 24073bb66b47SArtem Bityutskiy */ 24083bb66b47SArtem Bityutskiy int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head) 24093bb66b47SArtem Bityutskiy { 24103bb66b47SArtem Bityutskiy struct list_head *cur; 24113bb66b47SArtem Bityutskiy struct ubifs_scan_node *sa, *sb; 24123bb66b47SArtem Bityutskiy 24132b1844a8SArtem Bityutskiy if (!dbg_is_chk_gen(c)) 24143bb66b47SArtem Bityutskiy return 0; 24153bb66b47SArtem Bityutskiy 24163bb66b47SArtem Bityutskiy for (cur = head->next; cur->next != head; cur = cur->next) { 24173bb66b47SArtem Bityutskiy ino_t inuma, inumb; 24183bb66b47SArtem Bityutskiy uint32_t blka, blkb; 24193bb66b47SArtem Bityutskiy 24203bb66b47SArtem Bityutskiy cond_resched(); 24213bb66b47SArtem Bityutskiy sa = container_of(cur, struct ubifs_scan_node, list); 24223bb66b47SArtem Bityutskiy sb = container_of(cur->next, struct ubifs_scan_node, list); 24233bb66b47SArtem Bityutskiy 24243bb66b47SArtem Bityutskiy if (sa->type != UBIFS_DATA_NODE) { 24253bb66b47SArtem Bityutskiy ubifs_err("bad node type %d", sa->type); 2426*edf6be24SArtem Bityutskiy ubifs_dump_node(c, sa->node); 24273bb66b47SArtem Bityutskiy return -EINVAL; 24283bb66b47SArtem Bityutskiy } 24293bb66b47SArtem Bityutskiy if (sb->type != UBIFS_DATA_NODE) { 24303bb66b47SArtem Bityutskiy ubifs_err("bad node type %d", sb->type); 2431*edf6be24SArtem Bityutskiy ubifs_dump_node(c, sb->node); 24323bb66b47SArtem Bityutskiy return -EINVAL; 24333bb66b47SArtem Bityutskiy } 24343bb66b47SArtem Bityutskiy 24353bb66b47SArtem Bityutskiy inuma = key_inum(c, &sa->key); 24363bb66b47SArtem Bityutskiy inumb = key_inum(c, &sb->key); 24373bb66b47SArtem Bityutskiy 24383bb66b47SArtem Bityutskiy if (inuma < inumb) 24393bb66b47SArtem Bityutskiy continue; 24403bb66b47SArtem Bityutskiy if (inuma > inumb) { 24413bb66b47SArtem Bityutskiy ubifs_err("larger inum %lu goes before inum %lu", 24423bb66b47SArtem Bityutskiy (unsigned long)inuma, (unsigned long)inumb); 24433bb66b47SArtem Bityutskiy goto error_dump; 24443bb66b47SArtem Bityutskiy } 24453bb66b47SArtem Bityutskiy 24463bb66b47SArtem Bityutskiy blka = key_block(c, &sa->key); 24473bb66b47SArtem Bityutskiy blkb = key_block(c, &sb->key); 24483bb66b47SArtem Bityutskiy 24493bb66b47SArtem Bityutskiy if (blka > blkb) { 24503bb66b47SArtem Bityutskiy ubifs_err("larger block %u goes before %u", blka, blkb); 24513bb66b47SArtem Bityutskiy goto error_dump; 24523bb66b47SArtem Bityutskiy } 24533bb66b47SArtem Bityutskiy if (blka == blkb) { 24543bb66b47SArtem Bityutskiy ubifs_err("two data nodes for the same block"); 24553bb66b47SArtem Bityutskiy goto error_dump; 24563bb66b47SArtem Bityutskiy } 24573bb66b47SArtem Bityutskiy } 24583bb66b47SArtem Bityutskiy 24593bb66b47SArtem Bityutskiy return 0; 24603bb66b47SArtem Bityutskiy 24613bb66b47SArtem Bityutskiy error_dump: 2462*edf6be24SArtem Bityutskiy ubifs_dump_node(c, sa->node); 2463*edf6be24SArtem Bityutskiy ubifs_dump_node(c, sb->node); 24643bb66b47SArtem Bityutskiy return -EINVAL; 24653bb66b47SArtem Bityutskiy } 24663bb66b47SArtem Bityutskiy 24673bb66b47SArtem Bityutskiy /** 24683bb66b47SArtem Bityutskiy * dbg_check_nondata_nodes_order - check that list of data nodes is sorted. 24693bb66b47SArtem Bityutskiy * @c: UBIFS file-system description object 24703bb66b47SArtem Bityutskiy * @head: the list of nodes ('struct ubifs_scan_node' objects) 24713bb66b47SArtem Bityutskiy * 24723bb66b47SArtem Bityutskiy * This function returns zero if the list of non-data nodes is sorted correctly, 24733bb66b47SArtem Bityutskiy * and %-EINVAL if not. 24743bb66b47SArtem Bityutskiy */ 24753bb66b47SArtem Bityutskiy int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head) 24763bb66b47SArtem Bityutskiy { 24773bb66b47SArtem Bityutskiy struct list_head *cur; 24783bb66b47SArtem Bityutskiy struct ubifs_scan_node *sa, *sb; 24793bb66b47SArtem Bityutskiy 24802b1844a8SArtem Bityutskiy if (!dbg_is_chk_gen(c)) 24813bb66b47SArtem Bityutskiy return 0; 24823bb66b47SArtem Bityutskiy 24833bb66b47SArtem Bityutskiy for (cur = head->next; cur->next != head; cur = cur->next) { 24843bb66b47SArtem Bityutskiy ino_t inuma, inumb; 24853bb66b47SArtem Bityutskiy uint32_t hasha, hashb; 24863bb66b47SArtem Bityutskiy 24873bb66b47SArtem Bityutskiy cond_resched(); 24883bb66b47SArtem Bityutskiy sa = container_of(cur, struct ubifs_scan_node, list); 24893bb66b47SArtem Bityutskiy sb = container_of(cur->next, struct ubifs_scan_node, list); 24903bb66b47SArtem Bityutskiy 24913bb66b47SArtem Bityutskiy if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE && 24923bb66b47SArtem Bityutskiy sa->type != UBIFS_XENT_NODE) { 24933bb66b47SArtem Bityutskiy ubifs_err("bad node type %d", sa->type); 2494*edf6be24SArtem Bityutskiy ubifs_dump_node(c, sa->node); 24953bb66b47SArtem Bityutskiy return -EINVAL; 24963bb66b47SArtem Bityutskiy } 24973bb66b47SArtem Bityutskiy if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE && 24983bb66b47SArtem Bityutskiy sa->type != UBIFS_XENT_NODE) { 24993bb66b47SArtem Bityutskiy ubifs_err("bad node type %d", sb->type); 2500*edf6be24SArtem Bityutskiy ubifs_dump_node(c, sb->node); 25013bb66b47SArtem Bityutskiy return -EINVAL; 25023bb66b47SArtem Bityutskiy } 25033bb66b47SArtem Bityutskiy 25043bb66b47SArtem Bityutskiy if (sa->type != UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) { 25053bb66b47SArtem Bityutskiy ubifs_err("non-inode node goes before inode node"); 25063bb66b47SArtem Bityutskiy goto error_dump; 25073bb66b47SArtem Bityutskiy } 25083bb66b47SArtem Bityutskiy 25093bb66b47SArtem Bityutskiy if (sa->type == UBIFS_INO_NODE && sb->type != UBIFS_INO_NODE) 25103bb66b47SArtem Bityutskiy continue; 25113bb66b47SArtem Bityutskiy 25123bb66b47SArtem Bityutskiy if (sa->type == UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) { 25133bb66b47SArtem Bityutskiy /* Inode nodes are sorted in descending size order */ 25143bb66b47SArtem Bityutskiy if (sa->len < sb->len) { 25153bb66b47SArtem Bityutskiy ubifs_err("smaller inode node goes first"); 25163bb66b47SArtem Bityutskiy goto error_dump; 25173bb66b47SArtem Bityutskiy } 25183bb66b47SArtem Bityutskiy continue; 25193bb66b47SArtem Bityutskiy } 25203bb66b47SArtem Bityutskiy 25213bb66b47SArtem Bityutskiy /* 25223bb66b47SArtem Bityutskiy * This is either a dentry or xentry, which should be sorted in 25233bb66b47SArtem Bityutskiy * ascending (parent ino, hash) order. 25243bb66b47SArtem Bityutskiy */ 25253bb66b47SArtem Bityutskiy inuma = key_inum(c, &sa->key); 25263bb66b47SArtem Bityutskiy inumb = key_inum(c, &sb->key); 25273bb66b47SArtem Bityutskiy 25283bb66b47SArtem Bityutskiy if (inuma < inumb) 25293bb66b47SArtem Bityutskiy continue; 25303bb66b47SArtem Bityutskiy if (inuma > inumb) { 25313bb66b47SArtem Bityutskiy ubifs_err("larger inum %lu goes before inum %lu", 25323bb66b47SArtem Bityutskiy (unsigned long)inuma, (unsigned long)inumb); 25333bb66b47SArtem Bityutskiy goto error_dump; 25343bb66b47SArtem Bityutskiy } 25353bb66b47SArtem Bityutskiy 25363bb66b47SArtem Bityutskiy hasha = key_block(c, &sa->key); 25373bb66b47SArtem Bityutskiy hashb = key_block(c, &sb->key); 25383bb66b47SArtem Bityutskiy 25393bb66b47SArtem Bityutskiy if (hasha > hashb) { 2540c4361570SArtem Bityutskiy ubifs_err("larger hash %u goes before %u", 2541c4361570SArtem Bityutskiy hasha, hashb); 25423bb66b47SArtem Bityutskiy goto error_dump; 25433bb66b47SArtem Bityutskiy } 25443bb66b47SArtem Bityutskiy } 25453bb66b47SArtem Bityutskiy 25463bb66b47SArtem Bityutskiy return 0; 25473bb66b47SArtem Bityutskiy 25483bb66b47SArtem Bityutskiy error_dump: 25493bb66b47SArtem Bityutskiy ubifs_msg("dumping first node"); 2550*edf6be24SArtem Bityutskiy ubifs_dump_node(c, sa->node); 25513bb66b47SArtem Bityutskiy ubifs_msg("dumping second node"); 2552*edf6be24SArtem Bityutskiy ubifs_dump_node(c, sb->node); 25533bb66b47SArtem Bityutskiy return -EINVAL; 25543bb66b47SArtem Bityutskiy return 0; 25553bb66b47SArtem Bityutskiy } 25563bb66b47SArtem Bityutskiy 2557a7fa94a9SArtem Bityutskiy static inline int chance(unsigned int n, unsigned int out_of) 25581e51764aSArtem Bityutskiy { 2559a7fa94a9SArtem Bityutskiy return !!((random32() % out_of) + 1 <= n); 2560a7fa94a9SArtem Bityutskiy 25611e51764aSArtem Bityutskiy } 25621e51764aSArtem Bityutskiy 2563d27462a5SArtem Bityutskiy static int power_cut_emulated(struct ubifs_info *c, int lnum, int write) 25641e51764aSArtem Bityutskiy { 2565f57cb188SArtem Bityutskiy struct ubifs_debug_info *d = c->dbg; 25661e51764aSArtem Bityutskiy 2567f57cb188SArtem Bityutskiy ubifs_assert(dbg_is_tst_rcvry(c)); 25681e51764aSArtem Bityutskiy 2569d27462a5SArtem Bityutskiy if (!d->pc_cnt) { 2570d27462a5SArtem Bityutskiy /* First call - decide delay to the power cut */ 25711e51764aSArtem Bityutskiy if (chance(1, 2)) { 2572a7fa94a9SArtem Bityutskiy unsigned long delay; 25731e51764aSArtem Bityutskiy 25741e51764aSArtem Bityutskiy if (chance(1, 2)) { 2575d27462a5SArtem Bityutskiy d->pc_delay = 1; 2576a7fa94a9SArtem Bityutskiy /* Fail withing 1 minute */ 2577a7fa94a9SArtem Bityutskiy delay = random32() % 60000; 2578a7fa94a9SArtem Bityutskiy d->pc_timeout = jiffies; 2579a7fa94a9SArtem Bityutskiy d->pc_timeout += msecs_to_jiffies(delay); 2580a7fa94a9SArtem Bityutskiy ubifs_warn("failing after %lums", delay); 25811e51764aSArtem Bityutskiy } else { 2582d27462a5SArtem Bityutskiy d->pc_delay = 2; 2583a7fa94a9SArtem Bityutskiy delay = random32() % 10000; 2584a7fa94a9SArtem Bityutskiy /* Fail within 10000 operations */ 2585d27462a5SArtem Bityutskiy d->pc_cnt_max = delay; 2586a7fa94a9SArtem Bityutskiy ubifs_warn("failing after %lu calls", delay); 25871e51764aSArtem Bityutskiy } 25881e51764aSArtem Bityutskiy } 2589a7fa94a9SArtem Bityutskiy 2590d27462a5SArtem Bityutskiy d->pc_cnt += 1; 25911e51764aSArtem Bityutskiy } 2592a7fa94a9SArtem Bityutskiy 25931e51764aSArtem Bityutskiy /* Determine if failure delay has expired */ 2594a7fa94a9SArtem Bityutskiy if (d->pc_delay == 1 && time_before(jiffies, d->pc_timeout)) 25951e51764aSArtem Bityutskiy return 0; 2596a7fa94a9SArtem Bityutskiy if (d->pc_delay == 2 && d->pc_cnt++ < d->pc_cnt_max) 25971e51764aSArtem Bityutskiy return 0; 2598a7fa94a9SArtem Bityutskiy 25991e51764aSArtem Bityutskiy if (lnum == UBIFS_SB_LNUM) { 2600a7fa94a9SArtem Bityutskiy if (write && chance(1, 2)) 26011e51764aSArtem Bityutskiy return 0; 2602a7fa94a9SArtem Bityutskiy if (chance(19, 20)) 26031e51764aSArtem Bityutskiy return 0; 260424a4f800SArtem Bityutskiy ubifs_warn("failing in super block LEB %d", lnum); 26051e51764aSArtem Bityutskiy } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) { 26061e51764aSArtem Bityutskiy if (chance(19, 20)) 26071e51764aSArtem Bityutskiy return 0; 260824a4f800SArtem Bityutskiy ubifs_warn("failing in master LEB %d", lnum); 26091e51764aSArtem Bityutskiy } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) { 2610a7fa94a9SArtem Bityutskiy if (write && chance(99, 100)) 26111e51764aSArtem Bityutskiy return 0; 2612a7fa94a9SArtem Bityutskiy if (chance(399, 400)) 26131e51764aSArtem Bityutskiy return 0; 261424a4f800SArtem Bityutskiy ubifs_warn("failing in log LEB %d", lnum); 26151e51764aSArtem Bityutskiy } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) { 2616a7fa94a9SArtem Bityutskiy if (write && chance(7, 8)) 26171e51764aSArtem Bityutskiy return 0; 2618a7fa94a9SArtem Bityutskiy if (chance(19, 20)) 26191e51764aSArtem Bityutskiy return 0; 262024a4f800SArtem Bityutskiy ubifs_warn("failing in LPT LEB %d", lnum); 26211e51764aSArtem Bityutskiy } else if (lnum >= c->orph_first && lnum <= c->orph_last) { 2622a7fa94a9SArtem Bityutskiy if (write && chance(1, 2)) 26231e51764aSArtem Bityutskiy return 0; 2624a7fa94a9SArtem Bityutskiy if (chance(9, 10)) 26251e51764aSArtem Bityutskiy return 0; 262624a4f800SArtem Bityutskiy ubifs_warn("failing in orphan LEB %d", lnum); 26271e51764aSArtem Bityutskiy } else if (lnum == c->ihead_lnum) { 26281e51764aSArtem Bityutskiy if (chance(99, 100)) 26291e51764aSArtem Bityutskiy return 0; 263024a4f800SArtem Bityutskiy ubifs_warn("failing in index head LEB %d", lnum); 26311e51764aSArtem Bityutskiy } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) { 26321e51764aSArtem Bityutskiy if (chance(9, 10)) 26331e51764aSArtem Bityutskiy return 0; 263424a4f800SArtem Bityutskiy ubifs_warn("failing in GC head LEB %d", lnum); 26351e51764aSArtem Bityutskiy } else if (write && !RB_EMPTY_ROOT(&c->buds) && 26361e51764aSArtem Bityutskiy !ubifs_search_bud(c, lnum)) { 26371e51764aSArtem Bityutskiy if (chance(19, 20)) 26381e51764aSArtem Bityutskiy return 0; 263924a4f800SArtem Bityutskiy ubifs_warn("failing in non-bud LEB %d", lnum); 26401e51764aSArtem Bityutskiy } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND || 26411e51764aSArtem Bityutskiy c->cmt_state == COMMIT_RUNNING_REQUIRED) { 26421e51764aSArtem Bityutskiy if (chance(999, 1000)) 26431e51764aSArtem Bityutskiy return 0; 264424a4f800SArtem Bityutskiy ubifs_warn("failing in bud LEB %d commit running", lnum); 26451e51764aSArtem Bityutskiy } else { 26461e51764aSArtem Bityutskiy if (chance(9999, 10000)) 26471e51764aSArtem Bityutskiy return 0; 264824a4f800SArtem Bityutskiy ubifs_warn("failing in bud LEB %d commit not running", lnum); 26491e51764aSArtem Bityutskiy } 265024a4f800SArtem Bityutskiy 2651d27462a5SArtem Bityutskiy d->pc_happened = 1; 2652a7fa94a9SArtem Bityutskiy ubifs_warn("========== Power cut emulated =========="); 26531e51764aSArtem Bityutskiy dump_stack(); 26541e51764aSArtem Bityutskiy return 1; 26551e51764aSArtem Bityutskiy } 26561e51764aSArtem Bityutskiy 2657a7fa94a9SArtem Bityutskiy static void cut_data(const void *buf, unsigned int len) 26581e51764aSArtem Bityutskiy { 2659a7fa94a9SArtem Bityutskiy unsigned int from, to, i, ffs = chance(1, 2); 26601e51764aSArtem Bityutskiy unsigned char *p = (void *)buf; 26611e51764aSArtem Bityutskiy 2662a7fa94a9SArtem Bityutskiy from = random32() % (len + 1); 2663a7fa94a9SArtem Bityutskiy if (chance(1, 2)) 2664a7fa94a9SArtem Bityutskiy to = random32() % (len - from + 1); 2665a7fa94a9SArtem Bityutskiy else 2666a7fa94a9SArtem Bityutskiy to = len; 2667a7fa94a9SArtem Bityutskiy 2668a7fa94a9SArtem Bityutskiy if (from < to) 2669a7fa94a9SArtem Bityutskiy ubifs_warn("filled bytes %u-%u with %s", from, to - 1, 2670a7fa94a9SArtem Bityutskiy ffs ? "0xFFs" : "random data"); 2671a7fa94a9SArtem Bityutskiy 2672a7fa94a9SArtem Bityutskiy if (ffs) 2673a7fa94a9SArtem Bityutskiy for (i = from; i < to; i++) 2674a7fa94a9SArtem Bityutskiy p[i] = 0xFF; 2675a7fa94a9SArtem Bityutskiy else 2676a7fa94a9SArtem Bityutskiy for (i = from; i < to; i++) 2677a7fa94a9SArtem Bityutskiy p[i] = random32() % 0x100; 26781e51764aSArtem Bityutskiy } 26791e51764aSArtem Bityutskiy 2680f57cb188SArtem Bityutskiy int dbg_leb_write(struct ubifs_info *c, int lnum, const void *buf, 2681891a54a1SArtem Bityutskiy int offs, int len, int dtype) 26821e51764aSArtem Bityutskiy { 268316dfd804SAdrian Hunter int err, failing; 26841e51764aSArtem Bityutskiy 2685d27462a5SArtem Bityutskiy if (c->dbg->pc_happened) 26861a29af8bSArtem Bityutskiy return -EROFS; 2687d27462a5SArtem Bityutskiy 2688d27462a5SArtem Bityutskiy failing = power_cut_emulated(c, lnum, 1); 268916dfd804SAdrian Hunter if (failing) 26901e51764aSArtem Bityutskiy cut_data(buf, len); 2691f57cb188SArtem Bityutskiy err = ubi_leb_write(c->ubi, lnum, buf, offs, len, dtype); 26921e51764aSArtem Bityutskiy if (err) 26931e51764aSArtem Bityutskiy return err; 269416dfd804SAdrian Hunter if (failing) 26951a29af8bSArtem Bityutskiy return -EROFS; 26961e51764aSArtem Bityutskiy return 0; 26971e51764aSArtem Bityutskiy } 26981e51764aSArtem Bityutskiy 2699f57cb188SArtem Bityutskiy int dbg_leb_change(struct ubifs_info *c, int lnum, const void *buf, 27001e51764aSArtem Bityutskiy int len, int dtype) 27011e51764aSArtem Bityutskiy { 27021e51764aSArtem Bityutskiy int err; 27031e51764aSArtem Bityutskiy 2704d27462a5SArtem Bityutskiy if (c->dbg->pc_happened) 2705d27462a5SArtem Bityutskiy return -EROFS; 2706d27462a5SArtem Bityutskiy if (power_cut_emulated(c, lnum, 1)) 27071a29af8bSArtem Bityutskiy return -EROFS; 2708f57cb188SArtem Bityutskiy err = ubi_leb_change(c->ubi, lnum, buf, len, dtype); 27091e51764aSArtem Bityutskiy if (err) 27101e51764aSArtem Bityutskiy return err; 2711d27462a5SArtem Bityutskiy if (power_cut_emulated(c, lnum, 1)) 27121a29af8bSArtem Bityutskiy return -EROFS; 27131e51764aSArtem Bityutskiy return 0; 27141e51764aSArtem Bityutskiy } 27151e51764aSArtem Bityutskiy 2716f57cb188SArtem Bityutskiy int dbg_leb_unmap(struct ubifs_info *c, int lnum) 27171e51764aSArtem Bityutskiy { 27181e51764aSArtem Bityutskiy int err; 27191e51764aSArtem Bityutskiy 2720d27462a5SArtem Bityutskiy if (c->dbg->pc_happened) 2721d27462a5SArtem Bityutskiy return -EROFS; 2722d27462a5SArtem Bityutskiy if (power_cut_emulated(c, lnum, 0)) 27231a29af8bSArtem Bityutskiy return -EROFS; 2724f57cb188SArtem Bityutskiy err = ubi_leb_unmap(c->ubi, lnum); 27251e51764aSArtem Bityutskiy if (err) 27261e51764aSArtem Bityutskiy return err; 2727d27462a5SArtem Bityutskiy if (power_cut_emulated(c, lnum, 0)) 27281a29af8bSArtem Bityutskiy return -EROFS; 27291e51764aSArtem Bityutskiy return 0; 27301e51764aSArtem Bityutskiy } 27311e51764aSArtem Bityutskiy 2732f57cb188SArtem Bityutskiy int dbg_leb_map(struct ubifs_info *c, int lnum, int dtype) 27331e51764aSArtem Bityutskiy { 27341e51764aSArtem Bityutskiy int err; 27351e51764aSArtem Bityutskiy 2736d27462a5SArtem Bityutskiy if (c->dbg->pc_happened) 2737d27462a5SArtem Bityutskiy return -EROFS; 2738d27462a5SArtem Bityutskiy if (power_cut_emulated(c, lnum, 0)) 27391a29af8bSArtem Bityutskiy return -EROFS; 2740f57cb188SArtem Bityutskiy err = ubi_leb_map(c->ubi, lnum, dtype); 27411e51764aSArtem Bityutskiy if (err) 27421e51764aSArtem Bityutskiy return err; 2743d27462a5SArtem Bityutskiy if (power_cut_emulated(c, lnum, 0)) 27441a29af8bSArtem Bityutskiy return -EROFS; 27451e51764aSArtem Bityutskiy return 0; 27461e51764aSArtem Bityutskiy } 27471e51764aSArtem Bityutskiy 2748552ff317SArtem Bityutskiy /* 2749552ff317SArtem Bityutskiy * Root directory for UBIFS stuff in debugfs. Contains sub-directories which 2750552ff317SArtem Bityutskiy * contain the stuff specific to particular file-system mounts. 2751552ff317SArtem Bityutskiy */ 275284abf972SArtem Bityutskiy static struct dentry *dfs_rootdir; 2753552ff317SArtem Bityutskiy 27547dae997dSArtem Bityutskiy static int dfs_file_open(struct inode *inode, struct file *file) 2755552ff317SArtem Bityutskiy { 2756552ff317SArtem Bityutskiy file->private_data = inode->i_private; 27571bbfc848SArtem Bityutskiy return nonseekable_open(inode, file); 2758552ff317SArtem Bityutskiy } 2759552ff317SArtem Bityutskiy 276028488fc2SArtem Bityutskiy /** 276128488fc2SArtem Bityutskiy * provide_user_output - provide output to the user reading a debugfs file. 276228488fc2SArtem Bityutskiy * @val: boolean value for the answer 276328488fc2SArtem Bityutskiy * @u: the buffer to store the answer at 276428488fc2SArtem Bityutskiy * @count: size of the buffer 276528488fc2SArtem Bityutskiy * @ppos: position in the @u output buffer 276628488fc2SArtem Bityutskiy * 276728488fc2SArtem Bityutskiy * This is a simple helper function which stores @val boolean value in the user 276828488fc2SArtem Bityutskiy * buffer when the user reads one of UBIFS debugfs files. Returns amount of 276928488fc2SArtem Bityutskiy * bytes written to @u in case of success and a negative error code in case of 277028488fc2SArtem Bityutskiy * failure. 277128488fc2SArtem Bityutskiy */ 277228488fc2SArtem Bityutskiy static int provide_user_output(int val, char __user *u, size_t count, 277328488fc2SArtem Bityutskiy loff_t *ppos) 277428488fc2SArtem Bityutskiy { 277528488fc2SArtem Bityutskiy char buf[3]; 277628488fc2SArtem Bityutskiy 277728488fc2SArtem Bityutskiy if (val) 277828488fc2SArtem Bityutskiy buf[0] = '1'; 277928488fc2SArtem Bityutskiy else 278028488fc2SArtem Bityutskiy buf[0] = '0'; 278128488fc2SArtem Bityutskiy buf[1] = '\n'; 278228488fc2SArtem Bityutskiy buf[2] = 0x00; 278328488fc2SArtem Bityutskiy 278428488fc2SArtem Bityutskiy return simple_read_from_buffer(u, count, ppos, buf, 2); 278528488fc2SArtem Bityutskiy } 278628488fc2SArtem Bityutskiy 278781e79d38SArtem Bityutskiy static ssize_t dfs_file_read(struct file *file, char __user *u, size_t count, 278881e79d38SArtem Bityutskiy loff_t *ppos) 278981e79d38SArtem Bityutskiy { 279081e79d38SArtem Bityutskiy struct dentry *dent = file->f_path.dentry; 279181e79d38SArtem Bityutskiy struct ubifs_info *c = file->private_data; 279281e79d38SArtem Bityutskiy struct ubifs_debug_info *d = c->dbg; 279381e79d38SArtem Bityutskiy int val; 279481e79d38SArtem Bityutskiy 279581e79d38SArtem Bityutskiy if (dent == d->dfs_chk_gen) 279681e79d38SArtem Bityutskiy val = d->chk_gen; 279781e79d38SArtem Bityutskiy else if (dent == d->dfs_chk_index) 279881e79d38SArtem Bityutskiy val = d->chk_index; 279981e79d38SArtem Bityutskiy else if (dent == d->dfs_chk_orph) 280081e79d38SArtem Bityutskiy val = d->chk_orph; 280181e79d38SArtem Bityutskiy else if (dent == d->dfs_chk_lprops) 280281e79d38SArtem Bityutskiy val = d->chk_lprops; 280381e79d38SArtem Bityutskiy else if (dent == d->dfs_chk_fs) 280481e79d38SArtem Bityutskiy val = d->chk_fs; 280581e79d38SArtem Bityutskiy else if (dent == d->dfs_tst_rcvry) 280681e79d38SArtem Bityutskiy val = d->tst_rcvry; 280781e79d38SArtem Bityutskiy else 280881e79d38SArtem Bityutskiy return -EINVAL; 280981e79d38SArtem Bityutskiy 281028488fc2SArtem Bityutskiy return provide_user_output(val, u, count, ppos); 281128488fc2SArtem Bityutskiy } 281281e79d38SArtem Bityutskiy 281328488fc2SArtem Bityutskiy /** 281428488fc2SArtem Bityutskiy * interpret_user_input - interpret user debugfs file input. 281528488fc2SArtem Bityutskiy * @u: user-provided buffer with the input 281628488fc2SArtem Bityutskiy * @count: buffer size 281728488fc2SArtem Bityutskiy * 281828488fc2SArtem Bityutskiy * This is a helper function which interpret user input to a boolean UBIFS 281928488fc2SArtem Bityutskiy * debugfs file. Returns %0 or %1 in case of success and a negative error code 282028488fc2SArtem Bityutskiy * in case of failure. 282128488fc2SArtem Bityutskiy */ 282228488fc2SArtem Bityutskiy static int interpret_user_input(const char __user *u, size_t count) 282328488fc2SArtem Bityutskiy { 282428488fc2SArtem Bityutskiy size_t buf_size; 282528488fc2SArtem Bityutskiy char buf[8]; 282628488fc2SArtem Bityutskiy 282728488fc2SArtem Bityutskiy buf_size = min_t(size_t, count, (sizeof(buf) - 1)); 282828488fc2SArtem Bityutskiy if (copy_from_user(buf, u, buf_size)) 282928488fc2SArtem Bityutskiy return -EFAULT; 283028488fc2SArtem Bityutskiy 283128488fc2SArtem Bityutskiy if (buf[0] == '1') 283228488fc2SArtem Bityutskiy return 1; 283328488fc2SArtem Bityutskiy else if (buf[0] == '0') 283428488fc2SArtem Bityutskiy return 0; 283528488fc2SArtem Bityutskiy 283628488fc2SArtem Bityutskiy return -EINVAL; 283781e79d38SArtem Bityutskiy } 283881e79d38SArtem Bityutskiy 283981e79d38SArtem Bityutskiy static ssize_t dfs_file_write(struct file *file, const char __user *u, 2840552ff317SArtem Bityutskiy size_t count, loff_t *ppos) 2841552ff317SArtem Bityutskiy { 2842552ff317SArtem Bityutskiy struct ubifs_info *c = file->private_data; 2843552ff317SArtem Bityutskiy struct ubifs_debug_info *d = c->dbg; 284481e79d38SArtem Bityutskiy struct dentry *dent = file->f_path.dentry; 284581e79d38SArtem Bityutskiy int val; 2846552ff317SArtem Bityutskiy 284781e79d38SArtem Bityutskiy /* 284824a4f800SArtem Bityutskiy * TODO: this is racy - the file-system might have already been 284924a4f800SArtem Bityutskiy * unmounted and we'd oops in this case. The plan is to fix it with 285024a4f800SArtem Bityutskiy * help of 'iterate_supers_type()' which we should have in v3.0: when 285124a4f800SArtem Bityutskiy * a debugfs opened, we rember FS's UUID in file->private_data. Then 285224a4f800SArtem Bityutskiy * whenever we access the FS via a debugfs file, we iterate all UBIFS 285324a4f800SArtem Bityutskiy * superblocks and fine the one with the same UUID, and take the 285424a4f800SArtem Bityutskiy * locking right. 285524a4f800SArtem Bityutskiy * 285624a4f800SArtem Bityutskiy * The other way to go suggested by Al Viro is to create a separate 285724a4f800SArtem Bityutskiy * 'ubifs-debug' file-system instead. 285881e79d38SArtem Bityutskiy */ 285981e79d38SArtem Bityutskiy if (file->f_path.dentry == d->dfs_dump_lprops) { 2860*edf6be24SArtem Bityutskiy ubifs_dump_lprops(c); 286181e79d38SArtem Bityutskiy return count; 286281e79d38SArtem Bityutskiy } 286381e79d38SArtem Bityutskiy if (file->f_path.dentry == d->dfs_dump_budg) { 2864*edf6be24SArtem Bityutskiy ubifs_dump_budg(c, &c->bi); 286581e79d38SArtem Bityutskiy return count; 286681e79d38SArtem Bityutskiy } 286781e79d38SArtem Bityutskiy if (file->f_path.dentry == d->dfs_dump_tnc) { 2868552ff317SArtem Bityutskiy mutex_lock(&c->tnc_mutex); 2869*edf6be24SArtem Bityutskiy ubifs_dump_tnc(c); 2870552ff317SArtem Bityutskiy mutex_unlock(&c->tnc_mutex); 287181e79d38SArtem Bityutskiy return count; 287281e79d38SArtem Bityutskiy } 287381e79d38SArtem Bityutskiy 287428488fc2SArtem Bityutskiy val = interpret_user_input(u, count); 287528488fc2SArtem Bityutskiy if (val < 0) 287628488fc2SArtem Bityutskiy return val; 287781e79d38SArtem Bityutskiy 287881e79d38SArtem Bityutskiy if (dent == d->dfs_chk_gen) 287981e79d38SArtem Bityutskiy d->chk_gen = val; 288081e79d38SArtem Bityutskiy else if (dent == d->dfs_chk_index) 288181e79d38SArtem Bityutskiy d->chk_index = val; 288281e79d38SArtem Bityutskiy else if (dent == d->dfs_chk_orph) 288381e79d38SArtem Bityutskiy d->chk_orph = val; 288481e79d38SArtem Bityutskiy else if (dent == d->dfs_chk_lprops) 288581e79d38SArtem Bityutskiy d->chk_lprops = val; 288681e79d38SArtem Bityutskiy else if (dent == d->dfs_chk_fs) 288781e79d38SArtem Bityutskiy d->chk_fs = val; 288881e79d38SArtem Bityutskiy else if (dent == d->dfs_tst_rcvry) 288981e79d38SArtem Bityutskiy d->tst_rcvry = val; 289081e79d38SArtem Bityutskiy else 2891552ff317SArtem Bityutskiy return -EINVAL; 2892552ff317SArtem Bityutskiy 2893552ff317SArtem Bityutskiy return count; 2894552ff317SArtem Bityutskiy } 2895552ff317SArtem Bityutskiy 289684abf972SArtem Bityutskiy static const struct file_operations dfs_fops = { 28977dae997dSArtem Bityutskiy .open = dfs_file_open, 289881e79d38SArtem Bityutskiy .read = dfs_file_read, 289981e79d38SArtem Bityutskiy .write = dfs_file_write, 2900552ff317SArtem Bityutskiy .owner = THIS_MODULE, 29011bbfc848SArtem Bityutskiy .llseek = no_llseek, 2902552ff317SArtem Bityutskiy }; 2903552ff317SArtem Bityutskiy 2904552ff317SArtem Bityutskiy /** 2905552ff317SArtem Bityutskiy * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance. 2906552ff317SArtem Bityutskiy * @c: UBIFS file-system description object 2907552ff317SArtem Bityutskiy * 2908552ff317SArtem Bityutskiy * This function creates all debugfs files for this instance of UBIFS. Returns 2909552ff317SArtem Bityutskiy * zero in case of success and a negative error code in case of failure. 2910552ff317SArtem Bityutskiy * 2911552ff317SArtem Bityutskiy * Note, the only reason we have not merged this function with the 2912552ff317SArtem Bityutskiy * 'ubifs_debugging_init()' function is because it is better to initialize 2913552ff317SArtem Bityutskiy * debugfs interfaces at the very end of the mount process, and remove them at 2914552ff317SArtem Bityutskiy * the very beginning of the mount process. 2915552ff317SArtem Bityutskiy */ 2916552ff317SArtem Bityutskiy int dbg_debugfs_init_fs(struct ubifs_info *c) 2917552ff317SArtem Bityutskiy { 2918ae380ce0SArtem Bityutskiy int err, n; 2919552ff317SArtem Bityutskiy const char *fname; 2920552ff317SArtem Bityutskiy struct dentry *dent; 2921552ff317SArtem Bityutskiy struct ubifs_debug_info *d = c->dbg; 2922552ff317SArtem Bityutskiy 2923ae380ce0SArtem Bityutskiy n = snprintf(d->dfs_dir_name, UBIFS_DFS_DIR_LEN + 1, UBIFS_DFS_DIR_NAME, 2924ae380ce0SArtem Bityutskiy c->vi.ubi_num, c->vi.vol_id); 2925ae380ce0SArtem Bityutskiy if (n == UBIFS_DFS_DIR_LEN) { 2926ae380ce0SArtem Bityutskiy /* The array size is too small */ 2927ae380ce0SArtem Bityutskiy fname = UBIFS_DFS_DIR_NAME; 2928ae380ce0SArtem Bityutskiy dent = ERR_PTR(-EINVAL); 2929ae380ce0SArtem Bityutskiy goto out; 2930ae380ce0SArtem Bityutskiy } 2931ae380ce0SArtem Bityutskiy 2932cc6a86b9SArtem Bityutskiy fname = d->dfs_dir_name; 2933cc6a86b9SArtem Bityutskiy dent = debugfs_create_dir(fname, dfs_rootdir); 293495169535SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 2935552ff317SArtem Bityutskiy goto out; 2936cc6a86b9SArtem Bityutskiy d->dfs_dir = dent; 2937552ff317SArtem Bityutskiy 2938552ff317SArtem Bityutskiy fname = "dump_lprops"; 29398c559d30SVasiliy Kulikov dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops); 294095169535SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 2941552ff317SArtem Bityutskiy goto out_remove; 294284abf972SArtem Bityutskiy d->dfs_dump_lprops = dent; 2943552ff317SArtem Bityutskiy 2944552ff317SArtem Bityutskiy fname = "dump_budg"; 29458c559d30SVasiliy Kulikov dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops); 294695169535SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 2947552ff317SArtem Bityutskiy goto out_remove; 294884abf972SArtem Bityutskiy d->dfs_dump_budg = dent; 2949552ff317SArtem Bityutskiy 2950552ff317SArtem Bityutskiy fname = "dump_tnc"; 29518c559d30SVasiliy Kulikov dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops); 295295169535SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 2953552ff317SArtem Bityutskiy goto out_remove; 295484abf972SArtem Bityutskiy d->dfs_dump_tnc = dent; 2955552ff317SArtem Bityutskiy 295681e79d38SArtem Bityutskiy fname = "chk_general"; 295781e79d38SArtem Bityutskiy dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c, 295881e79d38SArtem Bityutskiy &dfs_fops); 295981e79d38SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 296081e79d38SArtem Bityutskiy goto out_remove; 296181e79d38SArtem Bityutskiy d->dfs_chk_gen = dent; 296281e79d38SArtem Bityutskiy 296381e79d38SArtem Bityutskiy fname = "chk_index"; 296481e79d38SArtem Bityutskiy dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c, 296581e79d38SArtem Bityutskiy &dfs_fops); 296681e79d38SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 296781e79d38SArtem Bityutskiy goto out_remove; 296881e79d38SArtem Bityutskiy d->dfs_chk_index = dent; 296981e79d38SArtem Bityutskiy 297081e79d38SArtem Bityutskiy fname = "chk_orphans"; 297181e79d38SArtem Bityutskiy dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c, 297281e79d38SArtem Bityutskiy &dfs_fops); 297381e79d38SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 297481e79d38SArtem Bityutskiy goto out_remove; 297581e79d38SArtem Bityutskiy d->dfs_chk_orph = dent; 297681e79d38SArtem Bityutskiy 297781e79d38SArtem Bityutskiy fname = "chk_lprops"; 297881e79d38SArtem Bityutskiy dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c, 297981e79d38SArtem Bityutskiy &dfs_fops); 298081e79d38SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 298181e79d38SArtem Bityutskiy goto out_remove; 298281e79d38SArtem Bityutskiy d->dfs_chk_lprops = dent; 298381e79d38SArtem Bityutskiy 298481e79d38SArtem Bityutskiy fname = "chk_fs"; 298581e79d38SArtem Bityutskiy dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c, 298681e79d38SArtem Bityutskiy &dfs_fops); 298781e79d38SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 298881e79d38SArtem Bityutskiy goto out_remove; 298981e79d38SArtem Bityutskiy d->dfs_chk_fs = dent; 299081e79d38SArtem Bityutskiy 299181e79d38SArtem Bityutskiy fname = "tst_recovery"; 299281e79d38SArtem Bityutskiy dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c, 299381e79d38SArtem Bityutskiy &dfs_fops); 299481e79d38SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 299581e79d38SArtem Bityutskiy goto out_remove; 299681e79d38SArtem Bityutskiy d->dfs_tst_rcvry = dent; 299781e79d38SArtem Bityutskiy 2998552ff317SArtem Bityutskiy return 0; 2999552ff317SArtem Bityutskiy 3000552ff317SArtem Bityutskiy out_remove: 3001cc6a86b9SArtem Bityutskiy debugfs_remove_recursive(d->dfs_dir); 3002cc6a86b9SArtem Bityutskiy out: 300395169535SArtem Bityutskiy err = dent ? PTR_ERR(dent) : -ENODEV; 3004e7717060SArtem Bityutskiy ubifs_err("cannot create \"%s\" debugfs file or directory, error %d\n", 3005552ff317SArtem Bityutskiy fname, err); 3006552ff317SArtem Bityutskiy return err; 3007552ff317SArtem Bityutskiy } 3008552ff317SArtem Bityutskiy 3009552ff317SArtem Bityutskiy /** 3010552ff317SArtem Bityutskiy * dbg_debugfs_exit_fs - remove all debugfs files. 3011552ff317SArtem Bityutskiy * @c: UBIFS file-system description object 3012552ff317SArtem Bityutskiy */ 3013552ff317SArtem Bityutskiy void dbg_debugfs_exit_fs(struct ubifs_info *c) 3014552ff317SArtem Bityutskiy { 301584abf972SArtem Bityutskiy debugfs_remove_recursive(c->dbg->dfs_dir); 3016552ff317SArtem Bityutskiy } 3017552ff317SArtem Bityutskiy 3018e7717060SArtem Bityutskiy struct ubifs_global_debug_info ubifs_dbg; 3019e7717060SArtem Bityutskiy 3020e7717060SArtem Bityutskiy static struct dentry *dfs_chk_gen; 3021e7717060SArtem Bityutskiy static struct dentry *dfs_chk_index; 3022e7717060SArtem Bityutskiy static struct dentry *dfs_chk_orph; 3023e7717060SArtem Bityutskiy static struct dentry *dfs_chk_lprops; 3024e7717060SArtem Bityutskiy static struct dentry *dfs_chk_fs; 3025e7717060SArtem Bityutskiy static struct dentry *dfs_tst_rcvry; 3026e7717060SArtem Bityutskiy 3027e7717060SArtem Bityutskiy static ssize_t dfs_global_file_read(struct file *file, char __user *u, 3028e7717060SArtem Bityutskiy size_t count, loff_t *ppos) 3029e7717060SArtem Bityutskiy { 3030e7717060SArtem Bityutskiy struct dentry *dent = file->f_path.dentry; 3031e7717060SArtem Bityutskiy int val; 3032e7717060SArtem Bityutskiy 3033e7717060SArtem Bityutskiy if (dent == dfs_chk_gen) 3034e7717060SArtem Bityutskiy val = ubifs_dbg.chk_gen; 3035e7717060SArtem Bityutskiy else if (dent == dfs_chk_index) 3036e7717060SArtem Bityutskiy val = ubifs_dbg.chk_index; 3037e7717060SArtem Bityutskiy else if (dent == dfs_chk_orph) 3038e7717060SArtem Bityutskiy val = ubifs_dbg.chk_orph; 3039e7717060SArtem Bityutskiy else if (dent == dfs_chk_lprops) 3040e7717060SArtem Bityutskiy val = ubifs_dbg.chk_lprops; 3041e7717060SArtem Bityutskiy else if (dent == dfs_chk_fs) 3042e7717060SArtem Bityutskiy val = ubifs_dbg.chk_fs; 3043e7717060SArtem Bityutskiy else if (dent == dfs_tst_rcvry) 3044e7717060SArtem Bityutskiy val = ubifs_dbg.tst_rcvry; 3045e7717060SArtem Bityutskiy else 3046e7717060SArtem Bityutskiy return -EINVAL; 3047e7717060SArtem Bityutskiy 3048e7717060SArtem Bityutskiy return provide_user_output(val, u, count, ppos); 3049e7717060SArtem Bityutskiy } 3050e7717060SArtem Bityutskiy 3051e7717060SArtem Bityutskiy static ssize_t dfs_global_file_write(struct file *file, const char __user *u, 3052e7717060SArtem Bityutskiy size_t count, loff_t *ppos) 3053e7717060SArtem Bityutskiy { 3054e7717060SArtem Bityutskiy struct dentry *dent = file->f_path.dentry; 3055e7717060SArtem Bityutskiy int val; 3056e7717060SArtem Bityutskiy 3057e7717060SArtem Bityutskiy val = interpret_user_input(u, count); 3058e7717060SArtem Bityutskiy if (val < 0) 3059e7717060SArtem Bityutskiy return val; 3060e7717060SArtem Bityutskiy 3061e7717060SArtem Bityutskiy if (dent == dfs_chk_gen) 3062e7717060SArtem Bityutskiy ubifs_dbg.chk_gen = val; 3063e7717060SArtem Bityutskiy else if (dent == dfs_chk_index) 3064e7717060SArtem Bityutskiy ubifs_dbg.chk_index = val; 3065e7717060SArtem Bityutskiy else if (dent == dfs_chk_orph) 3066e7717060SArtem Bityutskiy ubifs_dbg.chk_orph = val; 3067e7717060SArtem Bityutskiy else if (dent == dfs_chk_lprops) 3068e7717060SArtem Bityutskiy ubifs_dbg.chk_lprops = val; 3069e7717060SArtem Bityutskiy else if (dent == dfs_chk_fs) 3070e7717060SArtem Bityutskiy ubifs_dbg.chk_fs = val; 3071e7717060SArtem Bityutskiy else if (dent == dfs_tst_rcvry) 3072e7717060SArtem Bityutskiy ubifs_dbg.tst_rcvry = val; 3073e7717060SArtem Bityutskiy else 3074e7717060SArtem Bityutskiy return -EINVAL; 3075e7717060SArtem Bityutskiy 3076e7717060SArtem Bityutskiy return count; 3077e7717060SArtem Bityutskiy } 3078e7717060SArtem Bityutskiy 3079e7717060SArtem Bityutskiy static const struct file_operations dfs_global_fops = { 3080e7717060SArtem Bityutskiy .read = dfs_global_file_read, 3081e7717060SArtem Bityutskiy .write = dfs_global_file_write, 3082e7717060SArtem Bityutskiy .owner = THIS_MODULE, 3083e7717060SArtem Bityutskiy .llseek = no_llseek, 3084e7717060SArtem Bityutskiy }; 3085e7717060SArtem Bityutskiy 30867dae997dSArtem Bityutskiy /** 30877dae997dSArtem Bityutskiy * dbg_debugfs_init - initialize debugfs file-system. 30887dae997dSArtem Bityutskiy * 30897dae997dSArtem Bityutskiy * UBIFS uses debugfs file-system to expose various debugging knobs to 30907dae997dSArtem Bityutskiy * user-space. This function creates "ubifs" directory in the debugfs 30917dae997dSArtem Bityutskiy * file-system. Returns zero in case of success and a negative error code in 30927dae997dSArtem Bityutskiy * case of failure. 30937dae997dSArtem Bityutskiy */ 30947dae997dSArtem Bityutskiy int dbg_debugfs_init(void) 30957dae997dSArtem Bityutskiy { 3096e7717060SArtem Bityutskiy int err; 3097e7717060SArtem Bityutskiy const char *fname; 3098e7717060SArtem Bityutskiy struct dentry *dent; 3099e7717060SArtem Bityutskiy 3100e7717060SArtem Bityutskiy fname = "ubifs"; 3101e7717060SArtem Bityutskiy dent = debugfs_create_dir(fname, NULL); 3102e7717060SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 3103e7717060SArtem Bityutskiy goto out; 3104e7717060SArtem Bityutskiy dfs_rootdir = dent; 3105e7717060SArtem Bityutskiy 3106e7717060SArtem Bityutskiy fname = "chk_general"; 3107e7717060SArtem Bityutskiy dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL, 3108e7717060SArtem Bityutskiy &dfs_global_fops); 3109e7717060SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 3110e7717060SArtem Bityutskiy goto out_remove; 3111e7717060SArtem Bityutskiy dfs_chk_gen = dent; 3112e7717060SArtem Bityutskiy 3113e7717060SArtem Bityutskiy fname = "chk_index"; 3114e7717060SArtem Bityutskiy dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL, 3115e7717060SArtem Bityutskiy &dfs_global_fops); 3116e7717060SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 3117e7717060SArtem Bityutskiy goto out_remove; 3118e7717060SArtem Bityutskiy dfs_chk_index = dent; 3119e7717060SArtem Bityutskiy 3120e7717060SArtem Bityutskiy fname = "chk_orphans"; 3121e7717060SArtem Bityutskiy dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL, 3122e7717060SArtem Bityutskiy &dfs_global_fops); 3123e7717060SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 3124e7717060SArtem Bityutskiy goto out_remove; 3125e7717060SArtem Bityutskiy dfs_chk_orph = dent; 3126e7717060SArtem Bityutskiy 3127e7717060SArtem Bityutskiy fname = "chk_lprops"; 3128e7717060SArtem Bityutskiy dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL, 3129e7717060SArtem Bityutskiy &dfs_global_fops); 3130e7717060SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 3131e7717060SArtem Bityutskiy goto out_remove; 3132e7717060SArtem Bityutskiy dfs_chk_lprops = dent; 3133e7717060SArtem Bityutskiy 3134e7717060SArtem Bityutskiy fname = "chk_fs"; 3135e7717060SArtem Bityutskiy dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL, 3136e7717060SArtem Bityutskiy &dfs_global_fops); 3137e7717060SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 3138e7717060SArtem Bityutskiy goto out_remove; 3139e7717060SArtem Bityutskiy dfs_chk_fs = dent; 3140e7717060SArtem Bityutskiy 3141e7717060SArtem Bityutskiy fname = "tst_recovery"; 3142e7717060SArtem Bityutskiy dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL, 3143e7717060SArtem Bityutskiy &dfs_global_fops); 3144e7717060SArtem Bityutskiy if (IS_ERR_OR_NULL(dent)) 3145e7717060SArtem Bityutskiy goto out_remove; 3146e7717060SArtem Bityutskiy dfs_tst_rcvry = dent; 31477dae997dSArtem Bityutskiy 31487dae997dSArtem Bityutskiy return 0; 3149e7717060SArtem Bityutskiy 3150e7717060SArtem Bityutskiy out_remove: 3151e7717060SArtem Bityutskiy debugfs_remove_recursive(dfs_rootdir); 3152e7717060SArtem Bityutskiy out: 3153e7717060SArtem Bityutskiy err = dent ? PTR_ERR(dent) : -ENODEV; 3154e7717060SArtem Bityutskiy ubifs_err("cannot create \"%s\" debugfs file or directory, error %d\n", 3155e7717060SArtem Bityutskiy fname, err); 3156e7717060SArtem Bityutskiy return err; 31577dae997dSArtem Bityutskiy } 31587dae997dSArtem Bityutskiy 31597dae997dSArtem Bityutskiy /** 31607dae997dSArtem Bityutskiy * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system. 31617dae997dSArtem Bityutskiy */ 31627dae997dSArtem Bityutskiy void dbg_debugfs_exit(void) 31637dae997dSArtem Bityutskiy { 3164e7717060SArtem Bityutskiy debugfs_remove_recursive(dfs_rootdir); 31657dae997dSArtem Bityutskiy } 31667dae997dSArtem Bityutskiy 31677dae997dSArtem Bityutskiy /** 31687dae997dSArtem Bityutskiy * ubifs_debugging_init - initialize UBIFS debugging. 31697dae997dSArtem Bityutskiy * @c: UBIFS file-system description object 31707dae997dSArtem Bityutskiy * 31717dae997dSArtem Bityutskiy * This function initializes debugging-related data for the file system. 31727dae997dSArtem Bityutskiy * Returns zero in case of success and a negative error code in case of 31737dae997dSArtem Bityutskiy * failure. 31747dae997dSArtem Bityutskiy */ 31757dae997dSArtem Bityutskiy int ubifs_debugging_init(struct ubifs_info *c) 31767dae997dSArtem Bityutskiy { 31777dae997dSArtem Bityutskiy c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL); 31787dae997dSArtem Bityutskiy if (!c->dbg) 31797dae997dSArtem Bityutskiy return -ENOMEM; 31807dae997dSArtem Bityutskiy 31817dae997dSArtem Bityutskiy return 0; 31827dae997dSArtem Bityutskiy } 31837dae997dSArtem Bityutskiy 31847dae997dSArtem Bityutskiy /** 31857dae997dSArtem Bityutskiy * ubifs_debugging_exit - free debugging data. 31867dae997dSArtem Bityutskiy * @c: UBIFS file-system description object 31877dae997dSArtem Bityutskiy */ 31887dae997dSArtem Bityutskiy void ubifs_debugging_exit(struct ubifs_info *c) 31897dae997dSArtem Bityutskiy { 31907dae997dSArtem Bityutskiy kfree(c->dbg); 31917dae997dSArtem Bityutskiy } 31927dae997dSArtem Bityutskiy 31931e51764aSArtem Bityutskiy #endif /* CONFIG_UBIFS_FS_DEBUG */ 3194