linux/fs/ubifs/debug.c
<<
>>
Prefs
   1/*
   2 * This file is part of UBIFS.
   3 *
   4 * Copyright (C) 2006-2008 Nokia Corporation
   5 *
   6 * This program is free software; you can redistribute it and/or modify it
   7 * under the terms of the GNU General Public License version 2 as published by
   8 * the Free Software Foundation.
   9 *
  10 * This program is distributed in the hope that it will be useful, but WITHOUT
  11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  13 * more details.
  14 *
  15 * You should have received a copy of the GNU General Public License along with
  16 * this program; if not, write to the Free Software Foundation, Inc., 51
  17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18 *
  19 * Authors: Artem Bityutskiy (Битюцкий Артём)
  20 *          Adrian Hunter
  21 */
  22
  23/*
  24 * This file implements most of the debugging stuff which is compiled in only
  25 * when it is enabled. But some debugging check functions are implemented in
  26 * corresponding subsystem, just because they are closely related and utilize
  27 * various local functions of those subsystems.
  28 */
  29
  30#define UBIFS_DBG_PRESERVE_UBI
  31
  32#include "ubifs.h"
  33#include <linux/module.h>
  34#include <linux/moduleparam.h>
  35
  36#ifdef CONFIG_UBIFS_FS_DEBUG
  37
  38DEFINE_SPINLOCK(dbg_lock);
  39
  40static char dbg_key_buf0[128];
  41static char dbg_key_buf1[128];
  42
  43unsigned int ubifs_msg_flags = UBIFS_MSG_FLAGS_DEFAULT;
  44unsigned int ubifs_chk_flags = UBIFS_CHK_FLAGS_DEFAULT;
  45unsigned int ubifs_tst_flags;
  46
  47module_param_named(debug_msgs, ubifs_msg_flags, uint, S_IRUGO | S_IWUSR);
  48module_param_named(debug_chks, ubifs_chk_flags, uint, S_IRUGO | S_IWUSR);
  49module_param_named(debug_tsts, ubifs_tst_flags, uint, S_IRUGO | S_IWUSR);
  50
  51MODULE_PARM_DESC(debug_msgs, "Debug message type flags");
  52MODULE_PARM_DESC(debug_chks, "Debug check flags");
  53MODULE_PARM_DESC(debug_tsts, "Debug special test flags");
  54
  55static const char *get_key_fmt(int fmt)
  56{
  57        switch (fmt) {
  58        case UBIFS_SIMPLE_KEY_FMT:
  59                return "simple";
  60        default:
  61                return "unknown/invalid format";
  62        }
  63}
  64
  65static const char *get_key_hash(int hash)
  66{
  67        switch (hash) {
  68        case UBIFS_KEY_HASH_R5:
  69                return "R5";
  70        case UBIFS_KEY_HASH_TEST:
  71                return "test";
  72        default:
  73                return "unknown/invalid name hash";
  74        }
  75}
  76
  77static const char *get_key_type(int type)
  78{
  79        switch (type) {
  80        case UBIFS_INO_KEY:
  81                return "inode";
  82        case UBIFS_DENT_KEY:
  83                return "direntry";
  84        case UBIFS_XENT_KEY:
  85                return "xentry";
  86        case UBIFS_DATA_KEY:
  87                return "data";
  88        case UBIFS_TRUN_KEY:
  89                return "truncate";
  90        default:
  91                return "unknown/invalid key";
  92        }
  93}
  94
  95static void sprintf_key(const struct ubifs_info *c, const union ubifs_key *key,
  96                        char *buffer)
  97{
  98        char *p = buffer;
  99        int type = key_type(c, key);
 100
 101        if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
 102                switch (type) {
 103                case UBIFS_INO_KEY:
 104                        sprintf(p, "(%lu, %s)", (unsigned long)key_inum(c, key),
 105                               get_key_type(type));
 106                        break;
 107                case UBIFS_DENT_KEY:
 108                case UBIFS_XENT_KEY:
 109                        sprintf(p, "(%lu, %s, %#08x)",
 110                                (unsigned long)key_inum(c, key),
 111                                get_key_type(type), key_hash(c, key));
 112                        break;
 113                case UBIFS_DATA_KEY:
 114                        sprintf(p, "(%lu, %s, %u)",
 115                                (unsigned long)key_inum(c, key),
 116                                get_key_type(type), key_block(c, key));
 117                        break;
 118                case UBIFS_TRUN_KEY:
 119                        sprintf(p, "(%lu, %s)",
 120                                (unsigned long)key_inum(c, key),
 121                                get_key_type(type));
 122                        break;
 123                default:
 124                        sprintf(p, "(bad key type: %#08x, %#08x)",
 125                                key->u32[0], key->u32[1]);
 126                }
 127        } else
 128                sprintf(p, "bad key format %d", c->key_fmt);
 129}
 130
 131const char *dbg_key_str0(const struct ubifs_info *c, const union ubifs_key *key)
 132{
 133        /* dbg_lock must be held */
 134        sprintf_key(c, key, dbg_key_buf0);
 135        return dbg_key_buf0;
 136}
 137
 138const char *dbg_key_str1(const struct ubifs_info *c, const union ubifs_key *key)
 139{
 140        /* dbg_lock must be held */
 141        sprintf_key(c, key, dbg_key_buf1);
 142        return dbg_key_buf1;
 143}
 144
 145const char *dbg_ntype(int type)
 146{
 147        switch (type) {
 148        case UBIFS_PAD_NODE:
 149                return "padding node";
 150        case UBIFS_SB_NODE:
 151                return "superblock node";
 152        case UBIFS_MST_NODE:
 153                return "master node";
 154        case UBIFS_REF_NODE:
 155                return "reference node";
 156        case UBIFS_INO_NODE:
 157                return "inode node";
 158        case UBIFS_DENT_NODE:
 159                return "direntry node";
 160        case UBIFS_XENT_NODE:
 161                return "xentry node";
 162        case UBIFS_DATA_NODE:
 163                return "data node";
 164        case UBIFS_TRUN_NODE:
 165                return "truncate node";
 166        case UBIFS_IDX_NODE:
 167                return "indexing node";
 168        case UBIFS_CS_NODE:
 169                return "commit start node";
 170        case UBIFS_ORPH_NODE:
 171                return "orphan node";
 172        default:
 173                return "unknown node";
 174        }
 175}
 176
 177static const char *dbg_gtype(int type)
 178{
 179        switch (type) {
 180        case UBIFS_NO_NODE_GROUP:
 181                return "no node group";
 182        case UBIFS_IN_NODE_GROUP:
 183                return "in node group";
 184        case UBIFS_LAST_OF_NODE_GROUP:
 185                return "last of node group";
 186        default:
 187                return "unknown";
 188        }
 189}
 190
 191const char *dbg_cstate(int cmt_state)
 192{
 193        switch (cmt_state) {
 194        case COMMIT_RESTING:
 195                return "commit resting";
 196        case COMMIT_BACKGROUND:
 197                return "background commit requested";
 198        case COMMIT_REQUIRED:
 199                return "commit required";
 200        case COMMIT_RUNNING_BACKGROUND:
 201                return "BACKGROUND commit running";
 202        case COMMIT_RUNNING_REQUIRED:
 203                return "commit running and required";
 204        case COMMIT_BROKEN:
 205                return "broken commit";
 206        default:
 207                return "unknown commit state";
 208        }
 209}
 210
 211static void dump_ch(const struct ubifs_ch *ch)
 212{
 213        printk(KERN_DEBUG "\tmagic          %#x\n", le32_to_cpu(ch->magic));
 214        printk(KERN_DEBUG "\tcrc            %#x\n", le32_to_cpu(ch->crc));
 215        printk(KERN_DEBUG "\tnode_type      %d (%s)\n", ch->node_type,
 216               dbg_ntype(ch->node_type));
 217        printk(KERN_DEBUG "\tgroup_type     %d (%s)\n", ch->group_type,
 218               dbg_gtype(ch->group_type));
 219        printk(KERN_DEBUG "\tsqnum          %llu\n",
 220               (unsigned long long)le64_to_cpu(ch->sqnum));
 221        printk(KERN_DEBUG "\tlen            %u\n", le32_to_cpu(ch->len));
 222}
 223
 224void dbg_dump_inode(const struct ubifs_info *c, const struct inode *inode)
 225{
 226        const struct ubifs_inode *ui = ubifs_inode(inode);
 227
 228        printk(KERN_DEBUG "Dump in-memory inode:");
 229        printk(KERN_DEBUG "\tinode          %lu\n", inode->i_ino);
 230        printk(KERN_DEBUG "\tsize           %llu\n",
 231               (unsigned long long)i_size_read(inode));
 232        printk(KERN_DEBUG "\tnlink          %u\n", inode->i_nlink);
 233        printk(KERN_DEBUG "\tuid            %u\n", (unsigned int)inode->i_uid);
 234        printk(KERN_DEBUG "\tgid            %u\n", (unsigned int)inode->i_gid);
 235        printk(KERN_DEBUG "\tatime          %u.%u\n",
 236               (unsigned int)inode->i_atime.tv_sec,
 237               (unsigned int)inode->i_atime.tv_nsec);
 238        printk(KERN_DEBUG "\tmtime          %u.%u\n",
 239               (unsigned int)inode->i_mtime.tv_sec,
 240               (unsigned int)inode->i_mtime.tv_nsec);
 241        printk(KERN_DEBUG "\tctime          %u.%u\n",
 242               (unsigned int)inode->i_ctime.tv_sec,
 243               (unsigned int)inode->i_ctime.tv_nsec);
 244        printk(KERN_DEBUG "\tcreat_sqnum    %llu\n", ui->creat_sqnum);
 245        printk(KERN_DEBUG "\txattr_size     %u\n", ui->xattr_size);
 246        printk(KERN_DEBUG "\txattr_cnt      %u\n", ui->xattr_cnt);
 247        printk(KERN_DEBUG "\txattr_names    %u\n", ui->xattr_names);
 248        printk(KERN_DEBUG "\tdirty          %u\n", ui->dirty);
 249        printk(KERN_DEBUG "\txattr          %u\n", ui->xattr);
 250        printk(KERN_DEBUG "\tbulk_read      %u\n", ui->xattr);
 251        printk(KERN_DEBUG "\tsynced_i_size  %llu\n",
 252               (unsigned long long)ui->synced_i_size);
 253        printk(KERN_DEBUG "\tui_size        %llu\n",
 254               (unsigned long long)ui->ui_size);
 255        printk(KERN_DEBUG "\tflags          %d\n", ui->flags);
 256        printk(KERN_DEBUG "\tcompr_type     %d\n", ui->compr_type);
 257        printk(KERN_DEBUG "\tlast_page_read %lu\n", ui->last_page_read);
 258        printk(KERN_DEBUG "\tread_in_a_row  %lu\n", ui->read_in_a_row);
 259        printk(KERN_DEBUG "\tdata_len       %d\n", ui->data_len);
 260}
 261
 262void dbg_dump_node(const struct ubifs_info *c, const void *node)
 263{
 264        int i, n;
 265        union ubifs_key key;
 266        const struct ubifs_ch *ch = node;
 267
 268        if (dbg_failure_mode)
 269                return;
 270
 271        /* If the magic is incorrect, just hexdump the first bytes */
 272        if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
 273                printk(KERN_DEBUG "Not a node, first %zu bytes:", UBIFS_CH_SZ);
 274                print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
 275                               (void *)node, UBIFS_CH_SZ, 1);
 276                return;
 277        }
 278
 279        spin_lock(&dbg_lock);
 280        dump_ch(node);
 281
 282        switch (ch->node_type) {
 283        case UBIFS_PAD_NODE:
 284        {
 285                const struct ubifs_pad_node *pad = node;
 286
 287                printk(KERN_DEBUG "\tpad_len        %u\n",
 288                       le32_to_cpu(pad->pad_len));
 289                break;
 290        }
 291        case UBIFS_SB_NODE:
 292        {
 293                const struct ubifs_sb_node *sup = node;
 294                unsigned int sup_flags = le32_to_cpu(sup->flags);
 295
 296                printk(KERN_DEBUG "\tkey_hash       %d (%s)\n",
 297                       (int)sup->key_hash, get_key_hash(sup->key_hash));
 298                printk(KERN_DEBUG "\tkey_fmt        %d (%s)\n",
 299                       (int)sup->key_fmt, get_key_fmt(sup->key_fmt));
 300                printk(KERN_DEBUG "\tflags          %#x\n", sup_flags);
 301                printk(KERN_DEBUG "\t  big_lpt      %u\n",
 302                       !!(sup_flags & UBIFS_FLG_BIGLPT));
 303                printk(KERN_DEBUG "\tmin_io_size    %u\n",
 304                       le32_to_cpu(sup->min_io_size));
 305                printk(KERN_DEBUG "\tleb_size       %u\n",
 306                       le32_to_cpu(sup->leb_size));
 307                printk(KERN_DEBUG "\tleb_cnt        %u\n",
 308                       le32_to_cpu(sup->leb_cnt));
 309                printk(KERN_DEBUG "\tmax_leb_cnt    %u\n",
 310                       le32_to_cpu(sup->max_leb_cnt));
 311                printk(KERN_DEBUG "\tmax_bud_bytes  %llu\n",
 312                       (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
 313                printk(KERN_DEBUG "\tlog_lebs       %u\n",
 314                       le32_to_cpu(sup->log_lebs));
 315                printk(KERN_DEBUG "\tlpt_lebs       %u\n",
 316                       le32_to_cpu(sup->lpt_lebs));
 317                printk(KERN_DEBUG "\torph_lebs      %u\n",
 318                       le32_to_cpu(sup->orph_lebs));
 319                printk(KERN_DEBUG "\tjhead_cnt      %u\n",
 320                       le32_to_cpu(sup->jhead_cnt));
 321                printk(KERN_DEBUG "\tfanout         %u\n",
 322                       le32_to_cpu(sup->fanout));
 323                printk(KERN_DEBUG "\tlsave_cnt      %u\n",
 324                       le32_to_cpu(sup->lsave_cnt));
 325                printk(KERN_DEBUG "\tdefault_compr  %u\n",
 326                       (int)le16_to_cpu(sup->default_compr));
 327                printk(KERN_DEBUG "\trp_size        %llu\n",
 328                       (unsigned long long)le64_to_cpu(sup->rp_size));
 329                printk(KERN_DEBUG "\trp_uid         %u\n",
 330                       le32_to_cpu(sup->rp_uid));
 331                printk(KERN_DEBUG "\trp_gid         %u\n",
 332                       le32_to_cpu(sup->rp_gid));
 333                printk(KERN_DEBUG "\tfmt_version    %u\n",
 334                       le32_to_cpu(sup->fmt_version));
 335                printk(KERN_DEBUG "\ttime_gran      %u\n",
 336                       le32_to_cpu(sup->time_gran));
 337                printk(KERN_DEBUG "\tUUID           %02X%02X%02X%02X-%02X%02X"
 338                       "-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
 339                       sup->uuid[0], sup->uuid[1], sup->uuid[2], sup->uuid[3],
 340                       sup->uuid[4], sup->uuid[5], sup->uuid[6], sup->uuid[7],
 341                       sup->uuid[8], sup->uuid[9], sup->uuid[10], sup->uuid[11],
 342                       sup->uuid[12], sup->uuid[13], sup->uuid[14],
 343                       sup->uuid[15]);
 344                break;
 345        }
 346        case UBIFS_MST_NODE:
 347        {
 348                const struct ubifs_mst_node *mst = node;
 349
 350                printk(KERN_DEBUG "\thighest_inum   %llu\n",
 351                       (unsigned long long)le64_to_cpu(mst->highest_inum));
 352                printk(KERN_DEBUG "\tcommit number  %llu\n",
 353                       (unsigned long long)le64_to_cpu(mst->cmt_no));
 354                printk(KERN_DEBUG "\tflags          %#x\n",
 355                       le32_to_cpu(mst->flags));
 356                printk(KERN_DEBUG "\tlog_lnum       %u\n",
 357                       le32_to_cpu(mst->log_lnum));
 358                printk(KERN_DEBUG "\troot_lnum      %u\n",
 359                       le32_to_cpu(mst->root_lnum));
 360                printk(KERN_DEBUG "\troot_offs      %u\n",
 361                       le32_to_cpu(mst->root_offs));
 362                printk(KERN_DEBUG "\troot_len       %u\n",
 363                       le32_to_cpu(mst->root_len));
 364                printk(KERN_DEBUG "\tgc_lnum        %u\n",
 365                       le32_to_cpu(mst->gc_lnum));
 366                printk(KERN_DEBUG "\tihead_lnum     %u\n",
 367                       le32_to_cpu(mst->ihead_lnum));
 368                printk(KERN_DEBUG "\tihead_offs     %u\n",
 369                       le32_to_cpu(mst->ihead_offs));
 370                printk(KERN_DEBUG "\tindex_size     %llu\n",
 371                       (unsigned long long)le64_to_cpu(mst->index_size));
 372                printk(KERN_DEBUG "\tlpt_lnum       %u\n",
 373                       le32_to_cpu(mst->lpt_lnum));
 374                printk(KERN_DEBUG "\tlpt_offs       %u\n",
 375                       le32_to_cpu(mst->lpt_offs));
 376                printk(KERN_DEBUG "\tnhead_lnum     %u\n",
 377                       le32_to_cpu(mst->nhead_lnum));
 378                printk(KERN_DEBUG "\tnhead_offs     %u\n",
 379                       le32_to_cpu(mst->nhead_offs));
 380                printk(KERN_DEBUG "\tltab_lnum      %u\n",
 381                       le32_to_cpu(mst->ltab_lnum));
 382                printk(KERN_DEBUG "\tltab_offs      %u\n",
 383                       le32_to_cpu(mst->ltab_offs));
 384                printk(KERN_DEBUG "\tlsave_lnum     %u\n",
 385                       le32_to_cpu(mst->lsave_lnum));
 386                printk(KERN_DEBUG "\tlsave_offs     %u\n",
 387                       le32_to_cpu(mst->lsave_offs));
 388                printk(KERN_DEBUG "\tlscan_lnum     %u\n",
 389                       le32_to_cpu(mst->lscan_lnum));
 390                printk(KERN_DEBUG "\tleb_cnt        %u\n",
 391                       le32_to_cpu(mst->leb_cnt));
 392                printk(KERN_DEBUG "\tempty_lebs     %u\n",
 393                       le32_to_cpu(mst->empty_lebs));
 394                printk(KERN_DEBUG "\tidx_lebs       %u\n",
 395                       le32_to_cpu(mst->idx_lebs));
 396                printk(KERN_DEBUG "\ttotal_free     %llu\n",
 397                       (unsigned long long)le64_to_cpu(mst->total_free));
 398                printk(KERN_DEBUG "\ttotal_dirty    %llu\n",
 399                       (unsigned long long)le64_to_cpu(mst->total_dirty));
 400                printk(KERN_DEBUG "\ttotal_used     %llu\n",
 401                       (unsigned long long)le64_to_cpu(mst->total_used));
 402                printk(KERN_DEBUG "\ttotal_dead     %llu\n",
 403                       (unsigned long long)le64_to_cpu(mst->total_dead));
 404                printk(KERN_DEBUG "\ttotal_dark     %llu\n",
 405                       (unsigned long long)le64_to_cpu(mst->total_dark));
 406                break;
 407        }
 408        case UBIFS_REF_NODE:
 409        {
 410                const struct ubifs_ref_node *ref = node;
 411
 412                printk(KERN_DEBUG "\tlnum           %u\n",
 413                       le32_to_cpu(ref->lnum));
 414                printk(KERN_DEBUG "\toffs           %u\n",
 415                       le32_to_cpu(ref->offs));
 416                printk(KERN_DEBUG "\tjhead          %u\n",
 417                       le32_to_cpu(ref->jhead));
 418                break;
 419        }
 420        case UBIFS_INO_NODE:
 421        {
 422                const struct ubifs_ino_node *ino = node;
 423
 424                key_read(c, &ino->key, &key);
 425                printk(KERN_DEBUG "\tkey            %s\n", DBGKEY(&key));
 426                printk(KERN_DEBUG "\tcreat_sqnum    %llu\n",
 427                       (unsigned long long)le64_to_cpu(ino->creat_sqnum));
 428                printk(KERN_DEBUG "\tsize           %llu\n",
 429                       (unsigned long long)le64_to_cpu(ino->size));
 430                printk(KERN_DEBUG "\tnlink          %u\n",
 431                       le32_to_cpu(ino->nlink));
 432                printk(KERN_DEBUG "\tatime          %lld.%u\n",
 433                       (long long)le64_to_cpu(ino->atime_sec),
 434                       le32_to_cpu(ino->atime_nsec));
 435                printk(KERN_DEBUG "\tmtime          %lld.%u\n",
 436                       (long long)le64_to_cpu(ino->mtime_sec),
 437                       le32_to_cpu(ino->mtime_nsec));
 438                printk(KERN_DEBUG "\tctime          %lld.%u\n",
 439                       (long long)le64_to_cpu(ino->ctime_sec),
 440                       le32_to_cpu(ino->ctime_nsec));
 441                printk(KERN_DEBUG "\tuid            %u\n",
 442                       le32_to_cpu(ino->uid));
 443                printk(KERN_DEBUG "\tgid            %u\n",
 444                       le32_to_cpu(ino->gid));
 445                printk(KERN_DEBUG "\tmode           %u\n",
 446                       le32_to_cpu(ino->mode));
 447                printk(KERN_DEBUG "\tflags          %#x\n",
 448                       le32_to_cpu(ino->flags));
 449                printk(KERN_DEBUG "\txattr_cnt      %u\n",
 450                       le32_to_cpu(ino->xattr_cnt));
 451                printk(KERN_DEBUG "\txattr_size     %u\n",
 452                       le32_to_cpu(ino->xattr_size));
 453                printk(KERN_DEBUG "\txattr_names    %u\n",
 454                       le32_to_cpu(ino->xattr_names));
 455                printk(KERN_DEBUG "\tcompr_type     %#x\n",
 456                       (int)le16_to_cpu(ino->compr_type));
 457                printk(KERN_DEBUG "\tdata len       %u\n",
 458                       le32_to_cpu(ino->data_len));
 459                break;
 460        }
 461        case UBIFS_DENT_NODE:
 462        case UBIFS_XENT_NODE:
 463        {
 464                const struct ubifs_dent_node *dent = node;
 465                int nlen = le16_to_cpu(dent->nlen);
 466
 467                key_read(c, &dent->key, &key);
 468                printk(KERN_DEBUG "\tkey            %s\n", DBGKEY(&key));
 469                printk(KERN_DEBUG "\tinum           %llu\n",
 470                       (unsigned long long)le64_to_cpu(dent->inum));
 471                printk(KERN_DEBUG "\ttype           %d\n", (int)dent->type);
 472                printk(KERN_DEBUG "\tnlen           %d\n", nlen);
 473                printk(KERN_DEBUG "\tname           ");
 474
 475                if (nlen > UBIFS_MAX_NLEN)
 476                        printk(KERN_DEBUG "(bad name length, not printing, "
 477                                          "bad or corrupted node)");
 478                else {
 479                        for (i = 0; i < nlen && dent->name[i]; i++)
 480                                printk("%c", dent->name[i]);
 481                }
 482                printk("\n");
 483
 484                break;
 485        }
 486        case UBIFS_DATA_NODE:
 487        {
 488                const struct ubifs_data_node *dn = node;
 489                int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
 490
 491                key_read(c, &dn->key, &key);
 492                printk(KERN_DEBUG "\tkey            %s\n", DBGKEY(&key));
 493                printk(KERN_DEBUG "\tsize           %u\n",
 494                       le32_to_cpu(dn->size));
 495                printk(KERN_DEBUG "\tcompr_typ      %d\n",
 496                       (int)le16_to_cpu(dn->compr_type));
 497                printk(KERN_DEBUG "\tdata size      %d\n",
 498                       dlen);
 499                printk(KERN_DEBUG "\tdata:\n");
 500                print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET, 32, 1,
 501                               (void *)&dn->data, dlen, 0);
 502                break;
 503        }
 504        case UBIFS_TRUN_NODE:
 505        {
 506                const struct ubifs_trun_node *trun = node;
 507
 508                printk(KERN_DEBUG "\tinum           %u\n",
 509                       le32_to_cpu(trun->inum));
 510                printk(KERN_DEBUG "\told_size       %llu\n",
 511                       (unsigned long long)le64_to_cpu(trun->old_size));
 512                printk(KERN_DEBUG "\tnew_size       %llu\n",
 513                       (unsigned long long)le64_to_cpu(trun->new_size));
 514                break;
 515        }
 516        case UBIFS_IDX_NODE:
 517        {
 518                const struct ubifs_idx_node *idx = node;
 519
 520                n = le16_to_cpu(idx->child_cnt);
 521                printk(KERN_DEBUG "\tchild_cnt      %d\n", n);
 522                printk(KERN_DEBUG "\tlevel          %d\n",
 523                       (int)le16_to_cpu(idx->level));
 524                printk(KERN_DEBUG "\tBranches:\n");
 525
 526                for (i = 0; i < n && i < c->fanout - 1; i++) {
 527                        const struct ubifs_branch *br;
 528
 529                        br = ubifs_idx_branch(c, idx, i);
 530                        key_read(c, &br->key, &key);
 531                        printk(KERN_DEBUG "\t%d: LEB %d:%d len %d key %s\n",
 532                               i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
 533                               le32_to_cpu(br->len), DBGKEY(&key));
 534                }
 535                break;
 536        }
 537        case UBIFS_CS_NODE:
 538                break;
 539        case UBIFS_ORPH_NODE:
 540        {
 541                const struct ubifs_orph_node *orph = node;
 542
 543                printk(KERN_DEBUG "\tcommit number  %llu\n",
 544                       (unsigned long long)
 545                                le64_to_cpu(orph->cmt_no) & LLONG_MAX);
 546                printk(KERN_DEBUG "\tlast node flag %llu\n",
 547                       (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
 548                n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
 549                printk(KERN_DEBUG "\t%d orphan inode numbers:\n", n);
 550                for (i = 0; i < n; i++)
 551                        printk(KERN_DEBUG "\t  ino %llu\n",
 552                               (unsigned long long)le64_to_cpu(orph->inos[i]));
 553                break;
 554        }
 555        default:
 556                printk(KERN_DEBUG "node type %d was not recognized\n",
 557                       (int)ch->node_type);
 558        }
 559        spin_unlock(&dbg_lock);
 560}
 561
 562void dbg_dump_budget_req(const struct ubifs_budget_req *req)
 563{
 564        spin_lock(&dbg_lock);
 565        printk(KERN_DEBUG "Budgeting request: new_ino %d, dirtied_ino %d\n",
 566               req->new_ino, req->dirtied_ino);
 567        printk(KERN_DEBUG "\tnew_ino_d   %d, dirtied_ino_d %d\n",
 568               req->new_ino_d, req->dirtied_ino_d);
 569        printk(KERN_DEBUG "\tnew_page    %d, dirtied_page %d\n",
 570               req->new_page, req->dirtied_page);
 571        printk(KERN_DEBUG "\tnew_dent    %d, mod_dent     %d\n",
 572               req->new_dent, req->mod_dent);
 573        printk(KERN_DEBUG "\tidx_growth  %d\n", req->idx_growth);
 574        printk(KERN_DEBUG "\tdata_growth %d dd_growth     %d\n",
 575               req->data_growth, req->dd_growth);
 576        spin_unlock(&dbg_lock);
 577}
 578
 579void dbg_dump_lstats(const struct ubifs_lp_stats *lst)
 580{
 581        spin_lock(&dbg_lock);
 582        printk(KERN_DEBUG "(pid %d) Lprops statistics: empty_lebs %d, "
 583               "idx_lebs  %d\n", current->pid, lst->empty_lebs, lst->idx_lebs);
 584        printk(KERN_DEBUG "\ttaken_empty_lebs %d, total_free %lld, "
 585               "total_dirty %lld\n", lst->taken_empty_lebs, lst->total_free,
 586               lst->total_dirty);
 587        printk(KERN_DEBUG "\ttotal_used %lld, total_dark %lld, "
 588               "total_dead %lld\n", lst->total_used, lst->total_dark,
 589               lst->total_dead);
 590        spin_unlock(&dbg_lock);
 591}
 592
 593void dbg_dump_budg(struct ubifs_info *c)
 594{
 595        int i;
 596        struct rb_node *rb;
 597        struct ubifs_bud *bud;
 598        struct ubifs_gced_idx_leb *idx_gc;
 599
 600        spin_lock(&dbg_lock);
 601        printk(KERN_DEBUG "(pid %d) Budgeting info: budg_data_growth %lld, "
 602               "budg_dd_growth %lld, budg_idx_growth %lld\n", current->pid,
 603               c->budg_data_growth, c->budg_dd_growth, c->budg_idx_growth);
 604        printk(KERN_DEBUG "\tdata budget sum %lld, total budget sum %lld, "
 605               "freeable_cnt %d\n", c->budg_data_growth + c->budg_dd_growth,
 606               c->budg_data_growth + c->budg_dd_growth + c->budg_idx_growth,
 607               c->freeable_cnt);
 608        printk(KERN_DEBUG "\tmin_idx_lebs %d, old_idx_sz %lld, "
 609               "calc_idx_sz %lld, idx_gc_cnt %d\n", c->min_idx_lebs,
 610               c->old_idx_sz, c->calc_idx_sz, c->idx_gc_cnt);
 611        printk(KERN_DEBUG "\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, "
 612               "clean_zn_cnt %ld\n", atomic_long_read(&c->dirty_pg_cnt),
 613               atomic_long_read(&c->dirty_zn_cnt),
 614               atomic_long_read(&c->clean_zn_cnt));
 615        printk(KERN_DEBUG "\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
 616               c->dark_wm, c->dead_wm, c->max_idx_node_sz);
 617        printk(KERN_DEBUG "\tgc_lnum %d, ihead_lnum %d\n",
 618               c->gc_lnum, c->ihead_lnum);
 619        for (i = 0; i < c->jhead_cnt; i++)
 620                printk(KERN_DEBUG "\tjhead %d\t LEB %d\n",
 621                       c->jheads[i].wbuf.jhead, c->jheads[i].wbuf.lnum);
 622        for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
 623                bud = rb_entry(rb, struct ubifs_bud, rb);
 624                printk(KERN_DEBUG "\tbud LEB %d\n", bud->lnum);
 625        }
 626        list_for_each_entry(bud, &c->old_buds, list)
 627                printk(KERN_DEBUG "\told bud LEB %d\n", bud->lnum);
 628        list_for_each_entry(idx_gc, &c->idx_gc, list)
 629                printk(KERN_DEBUG "\tGC'ed idx LEB %d unmap %d\n",
 630                       idx_gc->lnum, idx_gc->unmap);
 631        printk(KERN_DEBUG "\tcommit state %d\n", c->cmt_state);
 632        spin_unlock(&dbg_lock);
 633}
 634
 635void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
 636{
 637        printk(KERN_DEBUG "LEB %d lprops: free %d, dirty %d (used %d), "
 638               "flags %#x\n", lp->lnum, lp->free, lp->dirty,
 639               c->leb_size - lp->free - lp->dirty, lp->flags);
 640}
 641
 642void dbg_dump_lprops(struct ubifs_info *c)
 643{
 644        int lnum, err;
 645        struct ubifs_lprops lp;
 646        struct ubifs_lp_stats lst;
 647
 648        printk(KERN_DEBUG "(pid %d) Dumping LEB properties\n", current->pid);
 649        ubifs_get_lp_stats(c, &lst);
 650        dbg_dump_lstats(&lst);
 651
 652        for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
 653                err = ubifs_read_one_lp(c, lnum, &lp);
 654                if (err)
 655                        ubifs_err("cannot read lprops for LEB %d", lnum);
 656
 657                dbg_dump_lprop(c, &lp);
 658        }
 659}
 660
 661void dbg_dump_lpt_info(struct ubifs_info *c)
 662{
 663        int i;
 664
 665        spin_lock(&dbg_lock);
 666        printk(KERN_DEBUG "\tlpt_sz:        %lld\n", c->lpt_sz);
 667        printk(KERN_DEBUG "\tpnode_sz:      %d\n", c->pnode_sz);
 668        printk(KERN_DEBUG "\tnnode_sz:      %d\n", c->nnode_sz);
 669        printk(KERN_DEBUG "\tltab_sz:       %d\n", c->ltab_sz);
 670        printk(KERN_DEBUG "\tlsave_sz:      %d\n", c->lsave_sz);
 671        printk(KERN_DEBUG "\tbig_lpt:       %d\n", c->big_lpt);
 672        printk(KERN_DEBUG "\tlpt_hght:      %d\n", c->lpt_hght);
 673        printk(KERN_DEBUG "\tpnode_cnt:     %d\n", c->pnode_cnt);
 674        printk(KERN_DEBUG "\tnnode_cnt:     %d\n", c->nnode_cnt);
 675        printk(KERN_DEBUG "\tdirty_pn_cnt:  %d\n", c->dirty_pn_cnt);
 676        printk(KERN_DEBUG "\tdirty_nn_cnt:  %d\n", c->dirty_nn_cnt);
 677        printk(KERN_DEBUG "\tlsave_cnt:     %d\n", c->lsave_cnt);
 678        printk(KERN_DEBUG "\tspace_bits:    %d\n", c->space_bits);
 679        printk(KERN_DEBUG "\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
 680        printk(KERN_DEBUG "\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
 681        printk(KERN_DEBUG "\tlpt_spc_bits:  %d\n", c->lpt_spc_bits);
 682        printk(KERN_DEBUG "\tpcnt_bits:     %d\n", c->pcnt_bits);
 683        printk(KERN_DEBUG "\tlnum_bits:     %d\n", c->lnum_bits);
 684        printk(KERN_DEBUG "\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
 685        printk(KERN_DEBUG "\tLPT head is at %d:%d\n",
 686               c->nhead_lnum, c->nhead_offs);
 687        printk(KERN_DEBUG "\tLPT ltab is at %d:%d\n", c->ltab_lnum, c->ltab_offs);
 688        if (c->big_lpt)
 689                printk(KERN_DEBUG "\tLPT lsave is at %d:%d\n",
 690                       c->lsave_lnum, c->lsave_offs);
 691        for (i = 0; i < c->lpt_lebs; i++)
 692                printk(KERN_DEBUG "\tLPT LEB %d free %d dirty %d tgc %d "
 693                       "cmt %d\n", i + c->lpt_first, c->ltab[i].free,
 694                       c->ltab[i].dirty, c->ltab[i].tgc, c->ltab[i].cmt);
 695        spin_unlock(&dbg_lock);
 696}
 697
 698void dbg_dump_leb(const struct ubifs_info *c, int lnum)
 699{
 700        struct ubifs_scan_leb *sleb;
 701        struct ubifs_scan_node *snod;
 702
 703        if (dbg_failure_mode)
 704                return;
 705
 706        printk(KERN_DEBUG "(pid %d) Dumping LEB %d\n", current->pid, lnum);
 707
 708        sleb = ubifs_scan(c, lnum, 0, c->dbg_buf);
 709        if (IS_ERR(sleb)) {
 710                ubifs_err("scan error %d", (int)PTR_ERR(sleb));
 711                return;
 712        }
 713
 714        printk(KERN_DEBUG "LEB %d has %d nodes ending at %d\n", lnum,
 715               sleb->nodes_cnt, sleb->endpt);
 716
 717        list_for_each_entry(snod, &sleb->nodes, list) {
 718                cond_resched();
 719                printk(KERN_DEBUG "Dumping node at LEB %d:%d len %d\n", lnum,
 720                       snod->offs, snod->len);
 721                dbg_dump_node(c, snod->node);
 722        }
 723
 724        ubifs_scan_destroy(sleb);
 725        return;
 726}
 727
 728void dbg_dump_znode(const struct ubifs_info *c,
 729                    const struct ubifs_znode *znode)
 730{
 731        int n;
 732        const struct ubifs_zbranch *zbr;
 733
 734        spin_lock(&dbg_lock);
 735        if (znode->parent)
 736                zbr = &znode->parent->zbranch[znode->iip];
 737        else
 738                zbr = &c->zroot;
 739
 740        printk(KERN_DEBUG "znode %p, LEB %d:%d len %d parent %p iip %d level %d"
 741               " child_cnt %d flags %lx\n", znode, zbr->lnum, zbr->offs,
 742               zbr->len, znode->parent, znode->iip, znode->level,
 743               znode->child_cnt, znode->flags);
 744
 745        if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
 746                spin_unlock(&dbg_lock);
 747                return;
 748        }
 749
 750        printk(KERN_DEBUG "zbranches:\n");
 751        for (n = 0; n < znode->child_cnt; n++) {
 752                zbr = &znode->zbranch[n];
 753                if (znode->level > 0)
 754                        printk(KERN_DEBUG "\t%d: znode %p LEB %d:%d len %d key "
 755                                          "%s\n", n, zbr->znode, zbr->lnum,
 756                                          zbr->offs, zbr->len,
 757                                          DBGKEY(&zbr->key));
 758                else
 759                        printk(KERN_DEBUG "\t%d: LNC %p LEB %d:%d len %d key "
 760                                          "%s\n", n, zbr->znode, zbr->lnum,
 761                                          zbr->offs, zbr->len,
 762                                          DBGKEY(&zbr->key));
 763        }
 764        spin_unlock(&dbg_lock);
 765}
 766
 767void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
 768{
 769        int i;
 770
 771        printk(KERN_DEBUG "(pid %d) Dumping heap cat %d (%d elements)\n",
 772               current->pid, cat, heap->cnt);
 773        for (i = 0; i < heap->cnt; i++) {
 774                struct ubifs_lprops *lprops = heap->arr[i];
 775
 776                printk(KERN_DEBUG "\t%d. LEB %d hpos %d free %d dirty %d "
 777                       "flags %d\n", i, lprops->lnum, lprops->hpos,
 778                       lprops->free, lprops->dirty, lprops->flags);
 779        }
 780}
 781
 782void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
 783                    struct ubifs_nnode *parent, int iip)
 784{
 785        int i;
 786
 787        printk(KERN_DEBUG "(pid %d) Dumping pnode:\n", current->pid);
 788        printk(KERN_DEBUG "\taddress %zx parent %zx cnext %zx\n",
 789               (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
 790        printk(KERN_DEBUG "\tflags %lu iip %d level %d num %d\n",
 791               pnode->flags, iip, pnode->level, pnode->num);
 792        for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
 793                struct ubifs_lprops *lp = &pnode->lprops[i];
 794
 795                printk(KERN_DEBUG "\t%d: free %d dirty %d flags %d lnum %d\n",
 796                       i, lp->free, lp->dirty, lp->flags, lp->lnum);
 797        }
 798}
 799
 800void dbg_dump_tnc(struct ubifs_info *c)
 801{
 802        struct ubifs_znode *znode;
 803        int level;
 804
 805        printk(KERN_DEBUG "\n");
 806        printk(KERN_DEBUG "(pid %d) Dumping the TNC tree\n", current->pid);
 807        znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
 808        level = znode->level;
 809        printk(KERN_DEBUG "== Level %d ==\n", level);
 810        while (znode) {
 811                if (level != znode->level) {
 812                        level = znode->level;
 813                        printk(KERN_DEBUG "== Level %d ==\n", level);
 814                }
 815                dbg_dump_znode(c, znode);
 816                znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
 817        }
 818
 819        printk(KERN_DEBUG "\n");
 820}
 821
 822static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
 823                      void *priv)
 824{
 825        dbg_dump_znode(c, znode);
 826        return 0;
 827}
 828
 829/**
 830 * dbg_dump_index - dump the on-flash index.
 831 * @c: UBIFS file-system description object
 832 *
 833 * This function dumps whole UBIFS indexing B-tree, unlike 'dbg_dump_tnc()'
 834 * which dumps only in-memory znodes and does not read znodes which from flash.
 835 */
 836void dbg_dump_index(struct ubifs_info *c)
 837{
 838        dbg_walk_index(c, NULL, dump_znode, NULL);
 839}
 840
 841/**
 842 * dbg_check_synced_i_size - check synchronized inode size.
 843 * @inode: inode to check
 844 *
 845 * If inode is clean, synchronized inode size has to be equivalent to current
 846 * inode size. This function has to be called only for locked inodes (@i_mutex
 847 * has to be locked). Returns %0 if synchronized inode size if correct, and
 848 * %-EINVAL if not.
 849 */
 850int dbg_check_synced_i_size(struct inode *inode)
 851{
 852        int err = 0;
 853        struct ubifs_inode *ui = ubifs_inode(inode);
 854
 855        if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
 856                return 0;
 857        if (!S_ISREG(inode->i_mode))
 858                return 0;
 859
 860        mutex_lock(&ui->ui_mutex);
 861        spin_lock(&ui->ui_lock);
 862        if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
 863                ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode "
 864                          "is clean", ui->ui_size, ui->synced_i_size);
 865                ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
 866                          inode->i_mode, i_size_read(inode));
 867                dbg_dump_stack();
 868                err = -EINVAL;
 869        }
 870        spin_unlock(&ui->ui_lock);
 871        mutex_unlock(&ui->ui_mutex);
 872        return err;
 873}
 874
 875/*
 876 * dbg_check_dir - check directory inode size and link count.
 877 * @c: UBIFS file-system description object
 878 * @dir: the directory to calculate size for
 879 * @size: the result is returned here
 880 *
 881 * This function makes sure that directory size and link count are correct.
 882 * Returns zero in case of success and a negative error code in case of
 883 * failure.
 884 *
 885 * Note, it is good idea to make sure the @dir->i_mutex is locked before
 886 * calling this function.
 887 */
 888int dbg_check_dir_size(struct ubifs_info *c, const struct inode *dir)
 889{
 890        unsigned int nlink = 2;
 891        union ubifs_key key;
 892        struct ubifs_dent_node *dent, *pdent = NULL;
 893        struct qstr nm = { .name = NULL };
 894        loff_t size = UBIFS_INO_NODE_SZ;
 895
 896        if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
 897                return 0;
 898
 899        if (!S_ISDIR(dir->i_mode))
 900                return 0;
 901
 902        lowest_dent_key(c, &key, dir->i_ino);
 903        while (1) {
 904                int err;
 905
 906                dent = ubifs_tnc_next_ent(c, &key, &nm);
 907                if (IS_ERR(dent)) {
 908                        err = PTR_ERR(dent);
 909                        if (err == -ENOENT)
 910                                break;
 911                        return err;
 912                }
 913
 914                nm.name = dent->name;
 915                nm.len = le16_to_cpu(dent->nlen);
 916                size += CALC_DENT_SIZE(nm.len);
 917                if (dent->type == UBIFS_ITYPE_DIR)
 918                        nlink += 1;
 919                kfree(pdent);
 920                pdent = dent;
 921                key_read(c, &dent->key, &key);
 922        }
 923        kfree(pdent);
 924
 925        if (i_size_read(dir) != size) {
 926                ubifs_err("directory inode %lu has size %llu, "
 927                          "but calculated size is %llu", dir->i_ino,
 928                          (unsigned long long)i_size_read(dir),
 929                          (unsigned long long)size);
 930                dump_stack();
 931                return -EINVAL;
 932        }
 933        if (dir->i_nlink != nlink) {
 934                ubifs_err("directory inode %lu has nlink %u, but calculated "
 935                          "nlink is %u", dir->i_ino, dir->i_nlink, nlink);
 936                dump_stack();
 937                return -EINVAL;
 938        }
 939
 940        return 0;
 941}
 942
 943/**
 944 * dbg_check_key_order - make sure that colliding keys are properly ordered.
 945 * @c: UBIFS file-system description object
 946 * @zbr1: first zbranch
 947 * @zbr2: following zbranch
 948 *
 949 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
 950 * names of the direntries/xentries which are referred by the keys. This
 951 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
 952 * sure the name of direntry/xentry referred by @zbr1 is less than
 953 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
 954 * and a negative error code in case of failure.
 955 */
 956static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
 957                               struct ubifs_zbranch *zbr2)
 958{
 959        int err, nlen1, nlen2, cmp;
 960        struct ubifs_dent_node *dent1, *dent2;
 961        union ubifs_key key;
 962
 963        ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
 964        dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
 965        if (!dent1)
 966                return -ENOMEM;
 967        dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
 968        if (!dent2) {
 969                err = -ENOMEM;
 970                goto out_free;
 971        }
 972
 973        err = ubifs_tnc_read_node(c, zbr1, dent1);
 974        if (err)
 975                goto out_free;
 976        err = ubifs_validate_entry(c, dent1);
 977        if (err)
 978                goto out_free;
 979
 980        err = ubifs_tnc_read_node(c, zbr2, dent2);
 981        if (err)
 982                goto out_free;
 983        err = ubifs_validate_entry(c, dent2);
 984        if (err)
 985                goto out_free;
 986
 987        /* Make sure node keys are the same as in zbranch */
 988        err = 1;
 989        key_read(c, &dent1->key, &key);
 990        if (keys_cmp(c, &zbr1->key, &key)) {
 991                dbg_err("1st entry at %d:%d has key %s", zbr1->lnum,
 992                        zbr1->offs, DBGKEY(&key));
 993                dbg_err("but it should have key %s according to tnc",
 994                        DBGKEY(&zbr1->key));
 995                        dbg_dump_node(c, dent1);
 996                        goto out_free;
 997        }
 998
 999        key_read(c, &dent2->key, &key);
1000        if (keys_cmp(c, &zbr2->key, &key)) {
1001                dbg_err("2nd entry at %d:%d has key %s", zbr1->lnum,
1002                        zbr1->offs, DBGKEY(&key));
1003                dbg_err("but it should have key %s according to tnc",
1004                        DBGKEY(&zbr2->key));
1005                        dbg_dump_node(c, dent2);
1006                        goto out_free;
1007        }
1008
1009        nlen1 = le16_to_cpu(dent1->nlen);
1010        nlen2 = le16_to_cpu(dent2->nlen);
1011
1012        cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
1013        if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
1014                err = 0;
1015                goto out_free;
1016        }
1017        if (cmp == 0 && nlen1 == nlen2)
1018                dbg_err("2 xent/dent nodes with the same name");
1019        else
1020                dbg_err("bad order of colliding key %s",
1021                        DBGKEY(&key));
1022
1023        dbg_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs);
1024        dbg_dump_node(c, dent1);
1025        dbg_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs);
1026        dbg_dump_node(c, dent2);
1027
1028out_free:
1029        kfree(dent2);
1030        kfree(dent1);
1031        return err;
1032}
1033
1034/**
1035 * dbg_check_znode - check if znode is all right.
1036 * @c: UBIFS file-system description object
1037 * @zbr: zbranch which points to this znode
1038 *
1039 * This function makes sure that znode referred to by @zbr is all right.
1040 * Returns zero if it is, and %-EINVAL if it is not.
1041 */
1042static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
1043{
1044        struct ubifs_znode *znode = zbr->znode;
1045        struct ubifs_znode *zp = znode->parent;
1046        int n, err, cmp;
1047
1048        if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
1049                err = 1;
1050                goto out;
1051        }
1052        if (znode->level < 0) {
1053                err = 2;
1054                goto out;
1055        }
1056        if (znode->iip < 0 || znode->iip >= c->fanout) {
1057                err = 3;
1058                goto out;
1059        }
1060
1061        if (zbr->len == 0)
1062                /* Only dirty zbranch may have no on-flash nodes */
1063                if (!ubifs_zn_dirty(znode)) {
1064                        err = 4;
1065                        goto out;
1066                }
1067
1068        if (ubifs_zn_dirty(znode)) {
1069                /*
1070                 * If znode is dirty, its parent has to be dirty as well. The
1071                 * order of the operation is important, so we have to have
1072                 * memory barriers.
1073                 */
1074                smp_mb();
1075                if (zp && !ubifs_zn_dirty(zp)) {
1076                        /*
1077                         * The dirty flag is atomic and is cleared outside the
1078                         * TNC mutex, so znode's dirty flag may now have
1079                         * been cleared. The child is always cleared before the
1080                         * parent, so we just need to check again.
1081                         */
1082                        smp_mb();
1083                        if (ubifs_zn_dirty(znode)) {
1084                                err = 5;
1085                                goto out;
1086                        }
1087                }
1088        }
1089
1090        if (zp) {
1091                const union ubifs_key *min, *max;
1092
1093                if (znode->level != zp->level - 1) {
1094                        err = 6;
1095                        goto out;
1096                }
1097
1098                /* Make sure the 'parent' pointer in our znode is correct */
1099                err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
1100                if (!err) {
1101                        /* This zbranch does not exist in the parent */
1102                        err = 7;
1103                        goto out;
1104                }
1105
1106                if (znode->iip >= zp->child_cnt) {
1107                        err = 8;
1108                        goto out;
1109                }
1110
1111                if (znode->iip != n) {
1112                        /* This may happen only in case of collisions */
1113                        if (keys_cmp(c, &zp->zbranch[n].key,
1114                                     &zp->zbranch[znode->iip].key)) {
1115                                err = 9;
1116                                goto out;
1117                        }
1118                        n = znode->iip;
1119                }
1120
1121                /*
1122                 * Make sure that the first key in our znode is greater than or
1123                 * equal to the key in the pointing zbranch.
1124                 */
1125                min = &zbr->key;
1126                cmp = keys_cmp(c, min, &znode->zbranch[0].key);
1127                if (cmp == 1) {
1128                        err = 10;
1129                        goto out;
1130                }
1131
1132                if (n + 1 < zp->child_cnt) {
1133                        max = &zp->zbranch[n + 1].key;
1134
1135                        /*
1136                         * Make sure the last key in our znode is less or
1137                         * equivalent than the the key in zbranch which goes
1138                         * after our pointing zbranch.
1139                         */
1140                        cmp = keys_cmp(c, max,
1141                                &znode->zbranch[znode->child_cnt - 1].key);
1142                        if (cmp == -1) {
1143                                err = 11;
1144                                goto out;
1145                        }
1146                }
1147        } else {
1148                /* This may only be root znode */
1149                if (zbr != &c->zroot) {
1150                        err = 12;
1151                        goto out;
1152                }
1153        }
1154
1155        /*
1156         * Make sure that next key is greater or equivalent then the previous
1157         * one.
1158         */
1159        for (n = 1; n < znode->child_cnt; n++) {
1160                cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
1161                               &znode->zbranch[n].key);
1162                if (cmp > 0) {
1163                        err = 13;
1164                        goto out;
1165                }
1166                if (cmp == 0) {
1167                        /* This can only be keys with colliding hash */
1168                        if (!is_hash_key(c, &znode->zbranch[n].key)) {
1169                                err = 14;
1170                                goto out;
1171                        }
1172
1173                        if (znode->level != 0 || c->replaying)
1174                                continue;
1175
1176                        /*
1177                         * Colliding keys should follow binary order of
1178                         * corresponding xentry/dentry names.
1179                         */
1180                        err = dbg_check_key_order(c, &znode->zbranch[n - 1],
1181                                                  &znode->zbranch[n]);
1182                        if (err < 0)
1183                                return err;
1184                        if (err) {
1185                                err = 15;
1186                                goto out;
1187                        }
1188                }
1189        }
1190
1191        for (n = 0; n < znode->child_cnt; n++) {
1192                if (!znode->zbranch[n].znode &&
1193                    (znode->zbranch[n].lnum == 0 ||
1194                     znode->zbranch[n].len == 0)) {
1195                        err = 16;
1196                        goto out;
1197                }
1198
1199                if (znode->zbranch[n].lnum != 0 &&
1200                    znode->zbranch[n].len == 0) {
1201                        err = 17;
1202                        goto out;
1203                }
1204
1205                if (znode->zbranch[n].lnum == 0 &&
1206                    znode->zbranch[n].len != 0) {
1207                        err = 18;
1208                        goto out;
1209                }
1210
1211                if (znode->zbranch[n].lnum == 0 &&
1212                    znode->zbranch[n].offs != 0) {
1213                        err = 19;
1214                        goto out;
1215                }
1216
1217                if (znode->level != 0 && znode->zbranch[n].znode)
1218                        if (znode->zbranch[n].znode->parent != znode) {
1219                                err = 20;
1220                                goto out;
1221                        }
1222        }
1223
1224        return 0;
1225
1226out:
1227        ubifs_err("failed, error %d", err);
1228        ubifs_msg("dump of the znode");
1229        dbg_dump_znode(c, znode);
1230        if (zp) {
1231                ubifs_msg("dump of the parent znode");
1232                dbg_dump_znode(c, zp);
1233        }
1234        dump_stack();
1235        return -EINVAL;
1236}
1237
1238/**
1239 * dbg_check_tnc - check TNC tree.
1240 * @c: UBIFS file-system description object
1241 * @extra: do extra checks that are possible at start commit
1242 *
1243 * This function traverses whole TNC tree and checks every znode. Returns zero
1244 * if everything is all right and %-EINVAL if something is wrong with TNC.
1245 */
1246int dbg_check_tnc(struct ubifs_info *c, int extra)
1247{
1248        struct ubifs_znode *znode;
1249        long clean_cnt = 0, dirty_cnt = 0;
1250        int err, last;
1251
1252        if (!(ubifs_chk_flags & UBIFS_CHK_TNC))
1253                return 0;
1254
1255        ubifs_assert(mutex_is_locked(&c->tnc_mutex));
1256        if (!c->zroot.znode)
1257                return 0;
1258
1259        znode = ubifs_tnc_postorder_first(c->zroot.znode);
1260        while (1) {
1261                struct ubifs_znode *prev;
1262                struct ubifs_zbranch *zbr;
1263
1264                if (!znode->parent)
1265                        zbr = &c->zroot;
1266                else
1267                        zbr = &znode->parent->zbranch[znode->iip];
1268
1269                err = dbg_check_znode(c, zbr);
1270                if (err)
1271                        return err;
1272
1273                if (extra) {
1274                        if (ubifs_zn_dirty(znode))
1275                                dirty_cnt += 1;
1276                        else
1277                                clean_cnt += 1;
1278                }
1279
1280                prev = znode;
1281                znode = ubifs_tnc_postorder_next(znode);
1282                if (!znode)
1283                        break;
1284
1285                /*
1286                 * If the last key of this znode is equivalent to the first key
1287                 * of the next znode (collision), then check order of the keys.
1288                 */
1289                last = prev->child_cnt - 1;
1290                if (prev->level == 0 && znode->level == 0 && !c->replaying &&
1291                    !keys_cmp(c, &prev->zbranch[last].key,
1292                              &znode->zbranch[0].key)) {
1293                        err = dbg_check_key_order(c, &prev->zbranch[last],
1294                                                  &znode->zbranch[0]);
1295                        if (err < 0)
1296                                return err;
1297                        if (err) {
1298                                ubifs_msg("first znode");
1299                                dbg_dump_znode(c, prev);
1300                                ubifs_msg("second znode");
1301                                dbg_dump_znode(c, znode);
1302                                return -EINVAL;
1303                        }
1304                }
1305        }
1306
1307        if (extra) {
1308                if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
1309                        ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld",
1310                                  atomic_long_read(&c->clean_zn_cnt),
1311                                  clean_cnt);
1312                        return -EINVAL;
1313                }
1314                if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
1315                        ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld",
1316                                  atomic_long_read(&c->dirty_zn_cnt),
1317                                  dirty_cnt);
1318                        return -EINVAL;
1319                }
1320        }
1321
1322        return 0;
1323}
1324
1325/**
1326 * dbg_walk_index - walk the on-flash index.
1327 * @c: UBIFS file-system description object
1328 * @leaf_cb: called for each leaf node
1329 * @znode_cb: called for each indexing node
1330 * @priv: private date which is passed to callbacks
1331 *
1332 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1333 * node and @znode_cb for each indexing node. Returns zero in case of success
1334 * and a negative error code in case of failure.
1335 *
1336 * It would be better if this function removed every znode it pulled to into
1337 * the TNC, so that the behavior more closely matched the non-debugging
1338 * behavior.
1339 */
1340int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1341                   dbg_znode_callback znode_cb, void *priv)
1342{
1343        int err;
1344        struct ubifs_zbranch *zbr;
1345        struct ubifs_znode *znode, *child;
1346
1347        mutex_lock(&c->tnc_mutex);
1348        /* If the root indexing node is not in TNC - pull it */
1349        if (!c->zroot.znode) {
1350                c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1351                if (IS_ERR(c->zroot.znode)) {
1352                        err = PTR_ERR(c->zroot.znode);
1353                        c->zroot.znode = NULL;
1354                        goto out_unlock;
1355                }
1356        }
1357
1358        /*
1359         * We are going to traverse the indexing tree in the postorder manner.
1360         * Go down and find the leftmost indexing node where we are going to
1361         * start from.
1362         */
1363        znode = c->zroot.znode;
1364        while (znode->level > 0) {
1365                zbr = &znode->zbranch[0];
1366                child = zbr->znode;
1367                if (!child) {
1368                        child = ubifs_load_znode(c, zbr, znode, 0);
1369                        if (IS_ERR(child)) {
1370                                err = PTR_ERR(child);
1371                                goto out_unlock;
1372                        }
1373                        zbr->znode = child;
1374                }
1375
1376                znode = child;
1377        }
1378
1379        /* Iterate over all indexing nodes */
1380        while (1) {
1381                int idx;
1382
1383                cond_resched();
1384
1385                if (znode_cb) {
1386                        err = znode_cb(c, znode, priv);
1387                        if (err) {
1388                                ubifs_err("znode checking function returned "
1389                                          "error %d", err);
1390                                dbg_dump_znode(c, znode);
1391                                goto out_dump;
1392                        }
1393                }
1394                if (leaf_cb && znode->level == 0) {
1395                        for (idx = 0; idx < znode->child_cnt; idx++) {
1396                                zbr = &znode->zbranch[idx];
1397                                err = leaf_cb(c, zbr, priv);
1398                                if (err) {
1399                                        ubifs_err("leaf checking function "
1400                                                  "returned error %d, for leaf "
1401                                                  "at LEB %d:%d",
1402                                                  err, zbr->lnum, zbr->offs);
1403                                        goto out_dump;
1404                                }
1405                        }
1406                }
1407
1408                if (!znode->parent)
1409                        break;
1410
1411                idx = znode->iip + 1;
1412                znode = znode->parent;
1413                if (idx < znode->child_cnt) {
1414                        /* Switch to the next index in the parent */
1415                        zbr = &znode->zbranch[idx];
1416                        child = zbr->znode;
1417                        if (!child) {
1418                                child = ubifs_load_znode(c, zbr, znode, idx);
1419                                if (IS_ERR(child)) {
1420                                        err = PTR_ERR(child);
1421                                        goto out_unlock;
1422                                }
1423                                zbr->znode = child;
1424                        }
1425                        znode = child;
1426                } else
1427                        /*
1428                         * This is the last child, switch to the parent and
1429                         * continue.
1430                         */
1431                        continue;
1432
1433                /* Go to the lowest leftmost znode in the new sub-tree */
1434                while (znode->level > 0) {
1435                        zbr = &znode->zbranch[0];
1436                        child = zbr->znode;
1437                        if (!child) {
1438                                child = ubifs_load_znode(c, zbr, znode, 0);
1439                                if (IS_ERR(child)) {
1440                                        err = PTR_ERR(child);
1441                                        goto out_unlock;
1442                                }
1443                                zbr->znode = child;
1444                        }
1445                        znode = child;
1446                }
1447        }
1448
1449        mutex_unlock(&c->tnc_mutex);
1450        return 0;
1451
1452out_dump:
1453        if (znode->parent)
1454                zbr = &znode->parent->zbranch[znode->iip];
1455        else
1456                zbr = &c->zroot;
1457        ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1458        dbg_dump_znode(c, znode);
1459out_unlock:
1460        mutex_unlock(&c->tnc_mutex);
1461        return err;
1462}
1463
1464/**
1465 * add_size - add znode size to partially calculated index size.
1466 * @c: UBIFS file-system description object
1467 * @znode: znode to add size for
1468 * @priv: partially calculated index size
1469 *
1470 * This is a helper function for 'dbg_check_idx_size()' which is called for
1471 * every indexing node and adds its size to the 'long long' variable pointed to
1472 * by @priv.
1473 */
1474static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1475{
1476        long long *idx_size = priv;
1477        int add;
1478
1479        add = ubifs_idx_node_sz(c, znode->child_cnt);
1480        add = ALIGN(add, 8);
1481        *idx_size += add;
1482        return 0;
1483}
1484
1485/**
1486 * dbg_check_idx_size - check index size.
1487 * @c: UBIFS file-system description object
1488 * @idx_size: size to check
1489 *
1490 * This function walks the UBIFS index, calculates its size and checks that the
1491 * size is equivalent to @idx_size. Returns zero in case of success and a
1492 * negative error code in case of failure.
1493 */
1494int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
1495{
1496        int err;
1497        long long calc = 0;
1498
1499        if (!(ubifs_chk_flags & UBIFS_CHK_IDX_SZ))
1500                return 0;
1501
1502        err = dbg_walk_index(c, NULL, add_size, &calc);
1503        if (err) {
1504                ubifs_err("error %d while walking the index", err);
1505                return err;
1506        }
1507
1508        if (calc != idx_size) {
1509                ubifs_err("index size check failed: calculated size is %lld, "
1510                          "should be %lld", calc, idx_size);
1511                dump_stack();
1512                return -EINVAL;
1513        }
1514
1515        return 0;
1516}
1517
1518/**
1519 * struct fsck_inode - information about an inode used when checking the file-system.
1520 * @rb: link in the RB-tree of inodes
1521 * @inum: inode number
1522 * @mode: inode type, permissions, etc
1523 * @nlink: inode link count
1524 * @xattr_cnt: count of extended attributes
1525 * @references: how many directory/xattr entries refer this inode (calculated
1526 *              while walking the index)
1527 * @calc_cnt: for directory inode count of child directories
1528 * @size: inode size (read from on-flash inode)
1529 * @xattr_sz: summary size of all extended attributes (read from on-flash
1530 *            inode)
1531 * @calc_sz: for directories calculated directory size
1532 * @calc_xcnt: count of extended attributes
1533 * @calc_xsz: calculated summary size of all extended attributes
1534 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1535 *             inode (read from on-flash inode)
1536 * @calc_xnms: calculated sum of lengths of all extended attribute names
1537 */
1538struct fsck_inode {
1539        struct rb_node rb;
1540        ino_t inum;
1541        umode_t mode;
1542        unsigned int nlink;
1543        unsigned int xattr_cnt;
1544        int references;
1545        int calc_cnt;
1546        long long size;
1547        unsigned int xattr_sz;
1548        long long calc_sz;
1549        long long calc_xcnt;
1550        long long calc_xsz;
1551        unsigned int xattr_nms;
1552        long long calc_xnms;
1553};
1554
1555/**
1556 * struct fsck_data - private FS checking information.
1557 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1558 */
1559struct fsck_data {
1560        struct rb_root inodes;
1561};
1562
1563/**
1564 * add_inode - add inode information to RB-tree of inodes.
1565 * @c: UBIFS file-system description object
1566 * @fsckd: FS checking information
1567 * @ino: raw UBIFS inode to add
1568 *
1569 * This is a helper function for 'check_leaf()' which adds information about
1570 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1571 * case of success and a negative error code in case of failure.
1572 */
1573static struct fsck_inode *add_inode(struct ubifs_info *c,
1574                                    struct fsck_data *fsckd,
1575                                    struct ubifs_ino_node *ino)
1576{
1577        struct rb_node **p, *parent = NULL;
1578        struct fsck_inode *fscki;
1579        ino_t inum = key_inum_flash(c, &ino->key);
1580
1581        p = &fsckd->inodes.rb_node;
1582        while (*p) {
1583                parent = *p;
1584                fscki = rb_entry(parent, struct fsck_inode, rb);
1585                if (inum < fscki->inum)
1586                        p = &(*p)->rb_left;
1587                else if (inum > fscki->inum)
1588                        p = &(*p)->rb_right;
1589                else
1590                        return fscki;
1591        }
1592
1593        if (inum > c->highest_inum) {
1594                ubifs_err("too high inode number, max. is %lu",
1595                          (unsigned long)c->highest_inum);
1596                return ERR_PTR(-EINVAL);
1597        }
1598
1599        fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
1600        if (!fscki)
1601                return ERR_PTR(-ENOMEM);
1602
1603        fscki->inum = inum;
1604        fscki->nlink = le32_to_cpu(ino->nlink);
1605        fscki->size = le64_to_cpu(ino->size);
1606        fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
1607        fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
1608        fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
1609        fscki->mode = le32_to_cpu(ino->mode);
1610        if (S_ISDIR(fscki->mode)) {
1611                fscki->calc_sz = UBIFS_INO_NODE_SZ;
1612                fscki->calc_cnt = 2;
1613        }
1614        rb_link_node(&fscki->rb, parent, p);
1615        rb_insert_color(&fscki->rb, &fsckd->inodes);
1616        return fscki;
1617}
1618
1619/**
1620 * search_inode - search inode in the RB-tree of inodes.
1621 * @fsckd: FS checking information
1622 * @inum: inode number to search
1623 *
1624 * This is a helper function for 'check_leaf()' which searches inode @inum in
1625 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1626 * the inode was not found.
1627 */
1628static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
1629{
1630        struct rb_node *p;
1631        struct fsck_inode *fscki;
1632
1633        p = fsckd->inodes.rb_node;
1634        while (p) {
1635                fscki = rb_entry(p, struct fsck_inode, rb);
1636                if (inum < fscki->inum)
1637                        p = p->rb_left;
1638                else if (inum > fscki->inum)
1639                        p = p->rb_right;
1640                else
1641                        return fscki;
1642        }
1643        return NULL;
1644}
1645
1646/**
1647 * read_add_inode - read inode node and add it to RB-tree of inodes.
1648 * @c: UBIFS file-system description object
1649 * @fsckd: FS checking information
1650 * @inum: inode number to read
1651 *
1652 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1653 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1654 * information pointer in case of success and a negative error code in case of
1655 * failure.
1656 */
1657static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1658                                         struct fsck_data *fsckd, ino_t inum)
1659{
1660        int n, err;
1661        union ubifs_key key;
1662        struct ubifs_znode *znode;
1663        struct ubifs_zbranch *zbr;
1664        struct ubifs_ino_node *ino;
1665        struct fsck_inode *fscki;
1666
1667        fscki = search_inode(fsckd, inum);
1668        if (fscki)
1669                return fscki;
1670
1671        ino_key_init(c, &key, inum);
1672        err = ubifs_lookup_level0(c, &key, &znode, &n);
1673        if (!err) {
1674                ubifs_err("inode %lu not found in index", (unsigned long)inum);
1675                return ERR_PTR(-ENOENT);
1676        } else if (err < 0) {
1677                ubifs_err("error %d while looking up inode %lu",
1678                          err, (unsigned long)inum);
1679                return ERR_PTR(err);
1680        }
1681
1682        zbr = &znode->zbranch[n];
1683        if (zbr->len < UBIFS_INO_NODE_SZ) {
1684                ubifs_err("bad node %lu node length %d",
1685                          (unsigned long)inum, zbr->len);
1686                return ERR_PTR(-EINVAL);
1687        }
1688
1689        ino = kmalloc(zbr->len, GFP_NOFS);
1690        if (!ino)
1691                return ERR_PTR(-ENOMEM);
1692
1693        err = ubifs_tnc_read_node(c, zbr, ino);
1694        if (err) {
1695                ubifs_err("cannot read inode node at LEB %d:%d, error %d",
1696                          zbr->lnum, zbr->offs, err);
1697                kfree(ino);
1698                return ERR_PTR(err);
1699        }
1700
1701        fscki = add_inode(c, fsckd, ino);
1702        kfree(ino);
1703        if (IS_ERR(fscki)) {
1704                ubifs_err("error %ld while adding inode %lu node",
1705                          PTR_ERR(fscki), (unsigned long)inum);
1706                return fscki;
1707        }
1708
1709        return fscki;
1710}
1711
1712/**
1713 * check_leaf - check leaf node.
1714 * @c: UBIFS file-system description object
1715 * @zbr: zbranch of the leaf node to check
1716 * @priv: FS checking information
1717 *
1718 * This is a helper function for 'dbg_check_filesystem()' which is called for
1719 * every single leaf node while walking the indexing tree. It checks that the
1720 * leaf node referred from the indexing tree exists, has correct CRC, and does
1721 * some other basic validation. This function is also responsible for building
1722 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
1723 * calculates reference count, size, etc for each inode in order to later
1724 * compare them to the information stored inside the inodes and detect possible
1725 * inconsistencies. Returns zero in case of success and a negative error code
1726 * in case of failure.
1727 */
1728static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
1729                      void *priv)
1730{
1731        ino_t inum;
1732        void *node;
1733        struct ubifs_ch *ch;
1734        int err, type = key_type(c, &zbr->key);
1735        struct fsck_inode *fscki;
1736
1737        if (zbr->len < UBIFS_CH_SZ) {
1738                ubifs_err("bad leaf length %d (LEB %d:%d)",
1739                          zbr->len, zbr->lnum, zbr->offs);
1740                return -EINVAL;
1741        }
1742
1743        node = kmalloc(zbr->len, GFP_NOFS);
1744        if (!node)
1745                return -ENOMEM;
1746
1747        err = ubifs_tnc_read_node(c, zbr, node);
1748        if (err) {
1749                ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
1750                          zbr->lnum, zbr->offs, err);
1751                goto out_free;
1752        }
1753
1754        /* If this is an inode node, add it to RB-tree of inodes */
1755        if (type == UBIFS_INO_KEY) {
1756                fscki = add_inode(c, priv, node);
1757                if (IS_ERR(fscki)) {
1758                        err = PTR_ERR(fscki);
1759                        ubifs_err("error %d while adding inode node", err);
1760                        goto out_dump;
1761                }
1762                goto out;
1763        }
1764
1765        if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
1766            type != UBIFS_DATA_KEY) {
1767                ubifs_err("unexpected node type %d at LEB %d:%d",
1768                          type, zbr->lnum, zbr->offs);
1769                err = -EINVAL;
1770                goto out_free;
1771        }
1772
1773        ch = node;
1774        if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
1775                ubifs_err("too high sequence number, max. is %llu",
1776                          c->max_sqnum);
1777                err = -EINVAL;
1778                goto out_dump;
1779        }
1780
1781        if (type == UBIFS_DATA_KEY) {
1782                long long blk_offs;
1783                struct ubifs_data_node *dn = node;
1784
1785                /*
1786                 * Search the inode node this data node belongs to and insert
1787                 * it to the RB-tree of inodes.
1788                 */
1789                inum = key_inum_flash(c, &dn->key);
1790                fscki = read_add_inode(c, priv, inum);
1791                if (IS_ERR(fscki)) {
1792                        err = PTR_ERR(fscki);
1793                        ubifs_err("error %d while processing data node and "
1794                                  "trying to find inode node %lu",
1795                                  err, (unsigned long)inum);
1796                        goto out_dump;
1797                }
1798
1799                /* Make sure the data node is within inode size */
1800                blk_offs = key_block_flash(c, &dn->key);
1801                blk_offs <<= UBIFS_BLOCK_SHIFT;
1802                blk_offs += le32_to_cpu(dn->size);
1803                if (blk_offs > fscki->size) {
1804                        ubifs_err("data node at LEB %d:%d is not within inode "
1805                                  "size %lld", zbr->lnum, zbr->offs,
1806                                  fscki->size);
1807                        err = -EINVAL;
1808                        goto out_dump;
1809                }
1810        } else {
1811                int nlen;
1812                struct ubifs_dent_node *dent = node;
1813                struct fsck_inode *fscki1;
1814
1815                err = ubifs_validate_entry(c, dent);
1816                if (err)
1817                        goto out_dump;
1818
1819                /*
1820                 * Search the inode node this entry refers to and the parent
1821                 * inode node and insert them to the RB-tree of inodes.
1822                 */
1823                inum = le64_to_cpu(dent->inum);
1824                fscki = read_add_inode(c, priv, inum);
1825                if (IS_ERR(fscki)) {
1826                        err = PTR_ERR(fscki);
1827                        ubifs_err("error %d while processing entry node and "
1828                                  "trying to find inode node %lu",
1829                                  err, (unsigned long)inum);
1830                        goto out_dump;
1831                }
1832
1833                /* Count how many direntries or xentries refers this inode */
1834                fscki->references += 1;
1835
1836                inum = key_inum_flash(c, &dent->key);
1837                fscki1 = read_add_inode(c, priv, inum);
1838                if (IS_ERR(fscki1)) {
1839                        err = PTR_ERR(fscki);
1840                        ubifs_err("error %d while processing entry node and "
1841                                  "trying to find parent inode node %lu",
1842                                  err, (unsigned long)inum);
1843                        goto out_dump;
1844                }
1845
1846                nlen = le16_to_cpu(dent->nlen);
1847                if (type == UBIFS_XENT_KEY) {
1848                        fscki1->calc_xcnt += 1;
1849                        fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
1850                        fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
1851                        fscki1->calc_xnms += nlen;
1852                } else {
1853                        fscki1->calc_sz += CALC_DENT_SIZE(nlen);
1854                        if (dent->type == UBIFS_ITYPE_DIR)
1855                                fscki1->calc_cnt += 1;
1856                }
1857        }
1858
1859out:
1860        kfree(node);
1861        return 0;
1862
1863out_dump:
1864        ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
1865        dbg_dump_node(c, node);
1866out_free:
1867        kfree(node);
1868        return err;
1869}
1870
1871/**
1872 * free_inodes - free RB-tree of inodes.
1873 * @fsckd: FS checking information
1874 */
1875static void free_inodes(struct fsck_data *fsckd)
1876{
1877        struct rb_node *this = fsckd->inodes.rb_node;
1878        struct fsck_inode *fscki;
1879
1880        while (this) {
1881                if (this->rb_left)
1882                        this = this->rb_left;
1883                else if (this->rb_right)
1884                        this = this->rb_right;
1885                else {
1886                        fscki = rb_entry(this, struct fsck_inode, rb);
1887                        this = rb_parent(this);
1888                        if (this) {
1889                                if (this->rb_left == &fscki->rb)
1890                                        this->rb_left = NULL;
1891                                else
1892                                        this->rb_right = NULL;
1893                        }
1894                        kfree(fscki);
1895                }
1896        }
1897}
1898
1899/**
1900 * check_inodes - checks all inodes.
1901 * @c: UBIFS file-system description object
1902 * @fsckd: FS checking information
1903 *
1904 * This is a helper function for 'dbg_check_filesystem()' which walks the
1905 * RB-tree of inodes after the index scan has been finished, and checks that
1906 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
1907 * %-EINVAL if not, and a negative error code in case of failure.
1908 */
1909static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
1910{
1911        int n, err;
1912        union ubifs_key key;
1913        struct ubifs_znode *znode;
1914        struct ubifs_zbranch *zbr;
1915        struct ubifs_ino_node *ino;
1916        struct fsck_inode *fscki;
1917        struct rb_node *this = rb_first(&fsckd->inodes);
1918
1919        while (this) {
1920                fscki = rb_entry(this, struct fsck_inode, rb);
1921                this = rb_next(this);
1922
1923                if (S_ISDIR(fscki->mode)) {
1924                        /*
1925                         * Directories have to have exactly one reference (they
1926                         * cannot have hardlinks), although root inode is an
1927                         * exception.
1928                         */
1929                        if (fscki->inum != UBIFS_ROOT_INO &&
1930                            fscki->references != 1) {
1931                                ubifs_err("directory inode %lu has %d "
1932                                          "direntries which refer it, but "
1933                                          "should be 1",
1934                                          (unsigned long)fscki->inum,
1935                                          fscki->references);
1936                                goto out_dump;
1937                        }
1938                        if (fscki->inum == UBIFS_ROOT_INO &&
1939                            fscki->references != 0) {
1940                                ubifs_err("root inode %lu has non-zero (%d) "
1941                                          "direntries which refer it",
1942                                          (unsigned long)fscki->inum,
1943                                          fscki->references);
1944                                goto out_dump;
1945                        }
1946                        if (fscki->calc_sz != fscki->size) {
1947                                ubifs_err("directory inode %lu size is %lld, "
1948                                          "but calculated size is %lld",
1949                                          (unsigned long)fscki->inum,
1950                                          fscki->size, fscki->calc_sz);
1951                                goto out_dump;
1952                        }
1953                        if (fscki->calc_cnt != fscki->nlink) {
1954                                ubifs_err("directory inode %lu nlink is %d, "
1955                                          "but calculated nlink is %d",
1956                                          (unsigned long)fscki->inum,
1957                                          fscki->nlink, fscki->calc_cnt);
1958                                goto out_dump;
1959                        }
1960                } else {
1961                        if (fscki->references != fscki->nlink) {
1962                                ubifs_err("inode %lu nlink is %d, but "
1963                                          "calculated nlink is %d",
1964                                          (unsigned long)fscki->inum,
1965                                          fscki->nlink, fscki->references);
1966                                goto out_dump;
1967                        }
1968                }
1969                if (fscki->xattr_sz != fscki->calc_xsz) {
1970                        ubifs_err("inode %lu has xattr size %u, but "
1971                                  "calculated size is %lld",
1972                                  (unsigned long)fscki->inum, fscki->xattr_sz,
1973                                  fscki->calc_xsz);
1974                        goto out_dump;
1975                }
1976                if (fscki->xattr_cnt != fscki->calc_xcnt) {
1977                        ubifs_err("inode %lu has %u xattrs, but "
1978                                  "calculated count is %lld",
1979                                  (unsigned long)fscki->inum,
1980                                  fscki->xattr_cnt, fscki->calc_xcnt);
1981                        goto out_dump;
1982                }
1983                if (fscki->xattr_nms != fscki->calc_xnms) {
1984                        ubifs_err("inode %lu has xattr names' size %u, but "
1985                                  "calculated names' size is %lld",
1986                                  (unsigned long)fscki->inum, fscki->xattr_nms,
1987                                  fscki->calc_xnms);
1988                        goto out_dump;
1989                }
1990        }
1991
1992        return 0;
1993
1994out_dump:
1995        /* Read the bad inode and dump it */
1996        ino_key_init(c, &key, fscki->inum);
1997        err = ubifs_lookup_level0(c, &key, &znode, &n);
1998        if (!err) {
1999                ubifs_err("inode %lu not found in index",
2000                          (unsigned long)fscki->inum);
2001                return -ENOENT;
2002        } else if (err < 0) {
2003                ubifs_err("error %d while looking up inode %lu",
2004                          err, (unsigned long)fscki->inum);
2005                return err;
2006        }
2007
2008        zbr = &znode->zbranch[n];
2009        ino = kmalloc(zbr->len, GFP_NOFS);
2010        if (!ino)
2011                return -ENOMEM;
2012
2013        err = ubifs_tnc_read_node(c, zbr, ino);
2014        if (err) {
2015                ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2016                          zbr->lnum, zbr->offs, err);
2017                kfree(ino);
2018                return err;
2019        }
2020
2021        ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",
2022                  (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
2023        dbg_dump_node(c, ino);
2024        kfree(ino);
2025        return -EINVAL;
2026}
2027
2028/**
2029 * dbg_check_filesystem - check the file-system.
2030 * @c: UBIFS file-system description object
2031 *
2032 * This function checks the file system, namely:
2033 * o makes sure that all leaf nodes exist and their CRCs are correct;
2034 * o makes sure inode nlink, size, xattr size/count are correct (for all
2035 *   inodes).
2036 *
2037 * The function reads whole indexing tree and all nodes, so it is pretty
2038 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2039 * not, and a negative error code in case of failure.
2040 */
2041int dbg_check_filesystem(struct ubifs_info *c)
2042{
2043        int err;
2044        struct fsck_data fsckd;
2045
2046        if (!(ubifs_chk_flags & UBIFS_CHK_FS))
2047                return 0;
2048
2049        fsckd.inodes = RB_ROOT;
2050        err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
2051        if (err)
2052                goto out_free;
2053
2054        err = check_inodes(c, &fsckd);
2055        if (err)
2056                goto out_free;
2057
2058        free_inodes(&fsckd);
2059        return 0;
2060
2061out_free:
2062        ubifs_err("file-system check failed with error %d", err);
2063        dump_stack();
2064        free_inodes(&fsckd);
2065        return err;
2066}
2067
2068static int invocation_cnt;
2069
2070int dbg_force_in_the_gaps(void)
2071{
2072        if (!dbg_force_in_the_gaps_enabled)
2073                return 0;
2074        /* Force in-the-gaps every 8th commit */
2075        return !((invocation_cnt++) & 0x7);
2076}
2077
2078/* Failure mode for recovery testing */
2079
2080#define chance(n, d) (simple_rand() <= (n) * 32768LL / (d))
2081
2082struct failure_mode_info {
2083        struct list_head list;
2084        struct ubifs_info *c;
2085};
2086
2087static LIST_HEAD(fmi_list);
2088static DEFINE_SPINLOCK(fmi_lock);
2089
2090static unsigned int next;
2091
2092static int simple_rand(void)
2093{
2094        if (next == 0)
2095                next = current->pid;
2096        next = next * 1103515245 + 12345;
2097        return (next >> 16) & 32767;
2098}
2099
2100void dbg_failure_mode_registration(struct ubifs_info *c)
2101{
2102        struct failure_mode_info *fmi;
2103
2104        fmi = kmalloc(sizeof(struct failure_mode_info), GFP_NOFS);
2105        if (!fmi) {
2106                dbg_err("Failed to register failure mode - no memory");
2107                return;
2108        }
2109        fmi->c = c;
2110        spin_lock(&fmi_lock);
2111        list_add_tail(&fmi->list, &fmi_list);
2112        spin_unlock(&fmi_lock);
2113}
2114
2115void dbg_failure_mode_deregistration(struct ubifs_info *c)
2116{
2117        struct failure_mode_info *fmi, *tmp;
2118
2119        spin_lock(&fmi_lock);
2120        list_for_each_entry_safe(fmi, tmp, &fmi_list, list)
2121                if (fmi->c == c) {
2122                        list_del(&fmi->list);
2123                        kfree(fmi);
2124                }
2125        spin_unlock(&fmi_lock);
2126}
2127
2128static struct ubifs_info *dbg_find_info(struct ubi_volume_desc *desc)
2129{
2130        struct failure_mode_info *fmi;
2131
2132        spin_lock(&fmi_lock);
2133        list_for_each_entry(fmi, &fmi_list, list)
2134                if (fmi->c->ubi == desc) {
2135                        struct ubifs_info *c = fmi->c;
2136
2137                        spin_unlock(&fmi_lock);
2138                        return c;
2139                }
2140        spin_unlock(&fmi_lock);
2141        return NULL;
2142}
2143
2144static int in_failure_mode(struct ubi_volume_desc *desc)
2145{
2146        struct ubifs_info *c = dbg_find_info(desc);
2147
2148        if (c && dbg_failure_mode)
2149                return c->failure_mode;
2150        return 0;
2151}
2152
2153static int do_fail(struct ubi_volume_desc *desc, int lnum, int write)
2154{
2155        struct ubifs_info *c = dbg_find_info(desc);
2156
2157        if (!c || !dbg_failure_mode)
2158                return 0;
2159        if (c->failure_mode)
2160                return 1;
2161        if (!c->fail_cnt) {
2162                /* First call - decide delay to failure */
2163                if (chance(1, 2)) {
2164                        unsigned int delay = 1 << (simple_rand() >> 11);
2165
2166                        if (chance(1, 2)) {
2167                                c->fail_delay = 1;
2168                                c->fail_timeout = jiffies +
2169                                                  msecs_to_jiffies(delay);
2170                                dbg_rcvry("failing after %ums", delay);
2171                        } else {
2172                                c->fail_delay = 2;
2173                                c->fail_cnt_max = delay;
2174                                dbg_rcvry("failing after %u calls", delay);
2175                        }
2176                }
2177                c->fail_cnt += 1;
2178        }
2179        /* Determine if failure delay has expired */
2180        if (c->fail_delay == 1) {
2181                if (time_before(jiffies, c->fail_timeout))
2182                        return 0;
2183        } else if (c->fail_delay == 2)
2184                if (c->fail_cnt++ < c->fail_cnt_max)
2185                        return 0;
2186        if (lnum == UBIFS_SB_LNUM) {
2187                if (write) {
2188                        if (chance(1, 2))
2189                                return 0;
2190                } else if (chance(19, 20))
2191                        return 0;
2192                dbg_rcvry("failing in super block LEB %d", lnum);
2193        } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2194                if (chance(19, 20))
2195                        return 0;
2196                dbg_rcvry("failing in master LEB %d", lnum);
2197        } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2198                if (write) {
2199                        if (chance(99, 100))
2200                                return 0;
2201                } else if (chance(399, 400))
2202                        return 0;
2203                dbg_rcvry("failing in log LEB %d", lnum);
2204        } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2205                if (write) {
2206                        if (chance(7, 8))
2207                                return 0;
2208                } else if (chance(19, 20))
2209                        return 0;
2210                dbg_rcvry("failing in LPT LEB %d", lnum);
2211        } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2212                if (write) {
2213                        if (chance(1, 2))
2214                                return 0;
2215                } else if (chance(9, 10))
2216                        return 0;
2217                dbg_rcvry("failing in orphan LEB %d", lnum);
2218        } else if (lnum == c->ihead_lnum) {
2219                if (chance(99, 100))
2220                        return 0;
2221                dbg_rcvry("failing in index head LEB %d", lnum);
2222        } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2223                if (chance(9, 10))
2224                        return 0;
2225                dbg_rcvry("failing in GC head LEB %d", lnum);
2226        } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
2227                   !ubifs_search_bud(c, lnum)) {
2228                if (chance(19, 20))
2229                        return 0;
2230                dbg_rcvry("failing in non-bud LEB %d", lnum);
2231        } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
2232                   c->cmt_state == COMMIT_RUNNING_REQUIRED) {
2233                if (chance(999, 1000))
2234                        return 0;
2235                dbg_rcvry("failing in bud LEB %d commit running", lnum);
2236        } else {
2237                if (chance(9999, 10000))
2238                        return 0;
2239                dbg_rcvry("failing in bud LEB %d commit not running", lnum);
2240        }
2241        ubifs_err("*** SETTING FAILURE MODE ON (LEB %d) ***", lnum);
2242        c->failure_mode = 1;
2243        dump_stack();
2244        return 1;
2245}
2246
2247static void cut_data(const void *buf, int len)
2248{
2249        int flen, i;
2250        unsigned char *p = (void *)buf;
2251
2252        flen = (len * (long long)simple_rand()) >> 15;
2253        for (i = flen; i < len; i++)
2254                p[i] = 0xff;
2255}
2256
2257int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
2258                 int len, int check)
2259{
2260        if (in_failure_mode(desc))
2261                return -EIO;
2262        return ubi_leb_read(desc, lnum, buf, offset, len, check);
2263}
2264
2265int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
2266                  int offset, int len, int dtype)
2267{
2268        int err, failing;
2269
2270        if (in_failure_mode(desc))
2271                return -EIO;
2272        failing = do_fail(desc, lnum, 1);
2273        if (failing)
2274                cut_data(buf, len);
2275        err = ubi_leb_write(desc, lnum, buf, offset, len, dtype);
2276        if (err)
2277                return err;
2278        if (failing)
2279                return -EIO;
2280        return 0;
2281}
2282
2283int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
2284                   int len, int dtype)
2285{
2286        int err;
2287
2288        if (do_fail(desc, lnum, 1))
2289                return -EIO;
2290        err = ubi_leb_change(desc, lnum, buf, len, dtype);
2291        if (err)
2292                return err;
2293        if (do_fail(desc, lnum, 1))
2294                return -EIO;
2295        return 0;
2296}
2297
2298int dbg_leb_erase(struct ubi_volume_desc *desc, int lnum)
2299{
2300        int err;
2301
2302        if (do_fail(desc, lnum, 0))
2303                return -EIO;
2304        err = ubi_leb_erase(desc, lnum);
2305        if (err)
2306                return err;
2307        if (do_fail(desc, lnum, 0))
2308                return -EIO;
2309        return 0;
2310}
2311
2312int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum)
2313{
2314        int err;
2315
2316        if (do_fail(desc, lnum, 0))
2317                return -EIO;
2318        err = ubi_leb_unmap(desc, lnum);
2319        if (err)
2320                return err;
2321        if (do_fail(desc, lnum, 0))
2322                return -EIO;
2323        return 0;
2324}
2325
2326int dbg_is_mapped(struct ubi_volume_desc *desc, int lnum)
2327{
2328        if (in_failure_mode(desc))
2329                return -EIO;
2330        return ubi_is_mapped(desc, lnum);
2331}
2332
2333int dbg_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
2334{
2335        int err;
2336
2337        if (do_fail(desc, lnum, 0))
2338                return -EIO;
2339        err = ubi_leb_map(desc, lnum, dtype);
2340        if (err)
2341                return err;
2342        if (do_fail(desc, lnum, 0))
2343                return -EIO;
2344        return 0;
2345}
2346
2347#endif /* CONFIG_UBIFS_FS_DEBUG */
2348
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.