uboot/fs/ubifs/debug.c
<<
>>
Prefs
   1/*
   2 * This file is part of UBIFS.
   3 *
   4 * Copyright (C) 2006-2008 Nokia Corporation
   5 *
   6 * SPDX-License-Identifier:     GPL-2.0+
   7 *
   8 * Authors: Artem Bityutskiy (Битюцкий Артём)
   9 *          Adrian Hunter
  10 */
  11
  12/*
  13 * This file implements most of the debugging stuff which is compiled in only
  14 * when it is enabled. But some debugging check functions are implemented in
  15 * corresponding subsystem, just because they are closely related and utilize
  16 * various local functions of those subsystems.
  17 */
  18
  19#ifndef __UBOOT__
  20#include <linux/module.h>
  21#include <linux/debugfs.h>
  22#include <linux/math64.h>
  23#include <linux/uaccess.h>
  24#include <linux/random.h>
  25#else
  26#include <linux/compat.h>
  27#include <linux/err.h>
  28#endif
  29#include "ubifs.h"
  30
  31#ifndef __UBOOT__
  32static DEFINE_SPINLOCK(dbg_lock);
  33#endif
  34
  35static const char *get_key_fmt(int fmt)
  36{
  37        switch (fmt) {
  38        case UBIFS_SIMPLE_KEY_FMT:
  39                return "simple";
  40        default:
  41                return "unknown/invalid format";
  42        }
  43}
  44
  45static const char *get_key_hash(int hash)
  46{
  47        switch (hash) {
  48        case UBIFS_KEY_HASH_R5:
  49                return "R5";
  50        case UBIFS_KEY_HASH_TEST:
  51                return "test";
  52        default:
  53                return "unknown/invalid name hash";
  54        }
  55}
  56
  57static const char *get_key_type(int type)
  58{
  59        switch (type) {
  60        case UBIFS_INO_KEY:
  61                return "inode";
  62        case UBIFS_DENT_KEY:
  63                return "direntry";
  64        case UBIFS_XENT_KEY:
  65                return "xentry";
  66        case UBIFS_DATA_KEY:
  67                return "data";
  68        case UBIFS_TRUN_KEY:
  69                return "truncate";
  70        default:
  71                return "unknown/invalid key";
  72        }
  73}
  74
  75#ifndef __UBOOT__
  76static const char *get_dent_type(int type)
  77{
  78        switch (type) {
  79        case UBIFS_ITYPE_REG:
  80                return "file";
  81        case UBIFS_ITYPE_DIR:
  82                return "dir";
  83        case UBIFS_ITYPE_LNK:
  84                return "symlink";
  85        case UBIFS_ITYPE_BLK:
  86                return "blkdev";
  87        case UBIFS_ITYPE_CHR:
  88                return "char dev";
  89        case UBIFS_ITYPE_FIFO:
  90                return "fifo";
  91        case UBIFS_ITYPE_SOCK:
  92                return "socket";
  93        default:
  94                return "unknown/invalid type";
  95        }
  96}
  97#endif
  98
  99const char *dbg_snprintf_key(const struct ubifs_info *c,
 100                             const union ubifs_key *key, char *buffer, int len)
 101{
 102        char *p = buffer;
 103        int type = key_type(c, key);
 104
 105        if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
 106                switch (type) {
 107                case UBIFS_INO_KEY:
 108                        len -= snprintf(p, len, "(%lu, %s)",
 109                                        (unsigned long)key_inum(c, key),
 110                                        get_key_type(type));
 111                        break;
 112                case UBIFS_DENT_KEY:
 113                case UBIFS_XENT_KEY:
 114                        len -= snprintf(p, len, "(%lu, %s, %#08x)",
 115                                        (unsigned long)key_inum(c, key),
 116                                        get_key_type(type), key_hash(c, key));
 117                        break;
 118                case UBIFS_DATA_KEY:
 119                        len -= snprintf(p, len, "(%lu, %s, %u)",
 120                                        (unsigned long)key_inum(c, key),
 121                                        get_key_type(type), key_block(c, key));
 122                        break;
 123                case UBIFS_TRUN_KEY:
 124                        len -= snprintf(p, len, "(%lu, %s)",
 125                                        (unsigned long)key_inum(c, key),
 126                                        get_key_type(type));
 127                        break;
 128                default:
 129                        len -= snprintf(p, len, "(bad key type: %#08x, %#08x)",
 130                                        key->u32[0], key->u32[1]);
 131                }
 132        } else
 133                len -= snprintf(p, len, "bad key format %d", c->key_fmt);
 134        ubifs_assert(len > 0);
 135        return p;
 136}
 137
 138const char *dbg_ntype(int type)
 139{
 140        switch (type) {
 141        case UBIFS_PAD_NODE:
 142                return "padding node";
 143        case UBIFS_SB_NODE:
 144                return "superblock node";
 145        case UBIFS_MST_NODE:
 146                return "master node";
 147        case UBIFS_REF_NODE:
 148                return "reference node";
 149        case UBIFS_INO_NODE:
 150                return "inode node";
 151        case UBIFS_DENT_NODE:
 152                return "direntry node";
 153        case UBIFS_XENT_NODE:
 154                return "xentry node";
 155        case UBIFS_DATA_NODE:
 156                return "data node";
 157        case UBIFS_TRUN_NODE:
 158                return "truncate node";
 159        case UBIFS_IDX_NODE:
 160                return "indexing node";
 161        case UBIFS_CS_NODE:
 162                return "commit start node";
 163        case UBIFS_ORPH_NODE:
 164                return "orphan node";
 165        default:
 166                return "unknown node";
 167        }
 168}
 169
 170static const char *dbg_gtype(int type)
 171{
 172        switch (type) {
 173        case UBIFS_NO_NODE_GROUP:
 174                return "no node group";
 175        case UBIFS_IN_NODE_GROUP:
 176                return "in node group";
 177        case UBIFS_LAST_OF_NODE_GROUP:
 178                return "last of node group";
 179        default:
 180                return "unknown";
 181        }
 182}
 183
 184const char *dbg_cstate(int cmt_state)
 185{
 186        switch (cmt_state) {
 187        case COMMIT_RESTING:
 188                return "commit resting";
 189        case COMMIT_BACKGROUND:
 190                return "background commit requested";
 191        case COMMIT_REQUIRED:
 192                return "commit required";
 193        case COMMIT_RUNNING_BACKGROUND:
 194                return "BACKGROUND commit running";
 195        case COMMIT_RUNNING_REQUIRED:
 196                return "commit running and required";
 197        case COMMIT_BROKEN:
 198                return "broken commit";
 199        default:
 200                return "unknown commit state";
 201        }
 202}
 203
 204const char *dbg_jhead(int jhead)
 205{
 206        switch (jhead) {
 207        case GCHD:
 208                return "0 (GC)";
 209        case BASEHD:
 210                return "1 (base)";
 211        case DATAHD:
 212                return "2 (data)";
 213        default:
 214                return "unknown journal head";
 215        }
 216}
 217
 218static void dump_ch(const struct ubifs_ch *ch)
 219{
 220        pr_err("\tmagic          %#x\n", le32_to_cpu(ch->magic));
 221        pr_err("\tcrc            %#x\n", le32_to_cpu(ch->crc));
 222        pr_err("\tnode_type      %d (%s)\n", ch->node_type,
 223               dbg_ntype(ch->node_type));
 224        pr_err("\tgroup_type     %d (%s)\n", ch->group_type,
 225               dbg_gtype(ch->group_type));
 226        pr_err("\tsqnum          %llu\n",
 227               (unsigned long long)le64_to_cpu(ch->sqnum));
 228        pr_err("\tlen            %u\n", le32_to_cpu(ch->len));
 229}
 230
 231void ubifs_dump_inode(struct ubifs_info *c, const struct inode *inode)
 232{
 233#ifndef __UBOOT__
 234        const struct ubifs_inode *ui = ubifs_inode(inode);
 235        struct qstr nm = { .name = NULL };
 236        union ubifs_key key;
 237        struct ubifs_dent_node *dent, *pdent = NULL;
 238        int count = 2;
 239
 240        pr_err("Dump in-memory inode:");
 241        pr_err("\tinode          %lu\n", inode->i_ino);
 242        pr_err("\tsize           %llu\n",
 243               (unsigned long long)i_size_read(inode));
 244        pr_err("\tnlink          %u\n", inode->i_nlink);
 245        pr_err("\tuid            %u\n", (unsigned int)i_uid_read(inode));
 246        pr_err("\tgid            %u\n", (unsigned int)i_gid_read(inode));
 247        pr_err("\tatime          %u.%u\n",
 248               (unsigned int)inode->i_atime.tv_sec,
 249               (unsigned int)inode->i_atime.tv_nsec);
 250        pr_err("\tmtime          %u.%u\n",
 251               (unsigned int)inode->i_mtime.tv_sec,
 252               (unsigned int)inode->i_mtime.tv_nsec);
 253        pr_err("\tctime          %u.%u\n",
 254               (unsigned int)inode->i_ctime.tv_sec,
 255               (unsigned int)inode->i_ctime.tv_nsec);
 256        pr_err("\tcreat_sqnum    %llu\n", ui->creat_sqnum);
 257        pr_err("\txattr_size     %u\n", ui->xattr_size);
 258        pr_err("\txattr_cnt      %u\n", ui->xattr_cnt);
 259        pr_err("\txattr_names    %u\n", ui->xattr_names);
 260        pr_err("\tdirty          %u\n", ui->dirty);
 261        pr_err("\txattr          %u\n", ui->xattr);
 262        pr_err("\tbulk_read      %u\n", ui->xattr);
 263        pr_err("\tsynced_i_size  %llu\n",
 264               (unsigned long long)ui->synced_i_size);
 265        pr_err("\tui_size        %llu\n",
 266               (unsigned long long)ui->ui_size);
 267        pr_err("\tflags          %d\n", ui->flags);
 268        pr_err("\tcompr_type     %d\n", ui->compr_type);
 269        pr_err("\tlast_page_read %lu\n", ui->last_page_read);
 270        pr_err("\tread_in_a_row  %lu\n", ui->read_in_a_row);
 271        pr_err("\tdata_len       %d\n", ui->data_len);
 272
 273        if (!S_ISDIR(inode->i_mode))
 274                return;
 275
 276        pr_err("List of directory entries:\n");
 277        ubifs_assert(!mutex_is_locked(&c->tnc_mutex));
 278
 279        lowest_dent_key(c, &key, inode->i_ino);
 280        while (1) {
 281                dent = ubifs_tnc_next_ent(c, &key, &nm);
 282                if (IS_ERR(dent)) {
 283                        if (PTR_ERR(dent) != -ENOENT)
 284                                pr_err("error %ld\n", PTR_ERR(dent));
 285                        break;
 286                }
 287
 288                pr_err("\t%d: %s (%s)\n",
 289                       count++, dent->name, get_dent_type(dent->type));
 290
 291                nm.name = dent->name;
 292                nm.len = le16_to_cpu(dent->nlen);
 293                kfree(pdent);
 294                pdent = dent;
 295                key_read(c, &dent->key, &key);
 296        }
 297        kfree(pdent);
 298#endif
 299}
 300
 301void ubifs_dump_node(const struct ubifs_info *c, const void *node)
 302{
 303        int i, n;
 304        union ubifs_key key;
 305        const struct ubifs_ch *ch = node;
 306        char key_buf[DBG_KEY_BUF_LEN];
 307
 308        /* If the magic is incorrect, just hexdump the first bytes */
 309        if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
 310                pr_err("Not a node, first %zu bytes:", UBIFS_CH_SZ);
 311                print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 32, 1,
 312                               (void *)node, UBIFS_CH_SZ, 1);
 313                return;
 314        }
 315
 316        spin_lock(&dbg_lock);
 317        dump_ch(node);
 318
 319        switch (ch->node_type) {
 320        case UBIFS_PAD_NODE:
 321        {
 322                const struct ubifs_pad_node *pad = node;
 323
 324                pr_err("\tpad_len        %u\n", le32_to_cpu(pad->pad_len));
 325                break;
 326        }
 327        case UBIFS_SB_NODE:
 328        {
 329                const struct ubifs_sb_node *sup = node;
 330                unsigned int sup_flags = le32_to_cpu(sup->flags);
 331
 332                pr_err("\tkey_hash       %d (%s)\n",
 333                       (int)sup->key_hash, get_key_hash(sup->key_hash));
 334                pr_err("\tkey_fmt        %d (%s)\n",
 335                       (int)sup->key_fmt, get_key_fmt(sup->key_fmt));
 336                pr_err("\tflags          %#x\n", sup_flags);
 337                pr_err("\tbig_lpt        %u\n",
 338                       !!(sup_flags & UBIFS_FLG_BIGLPT));
 339                pr_err("\tspace_fixup    %u\n",
 340                       !!(sup_flags & UBIFS_FLG_SPACE_FIXUP));
 341                pr_err("\tmin_io_size    %u\n", le32_to_cpu(sup->min_io_size));
 342                pr_err("\tleb_size       %u\n", le32_to_cpu(sup->leb_size));
 343                pr_err("\tleb_cnt        %u\n", le32_to_cpu(sup->leb_cnt));
 344                pr_err("\tmax_leb_cnt    %u\n", le32_to_cpu(sup->max_leb_cnt));
 345                pr_err("\tmax_bud_bytes  %llu\n",
 346                       (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
 347                pr_err("\tlog_lebs       %u\n", le32_to_cpu(sup->log_lebs));
 348                pr_err("\tlpt_lebs       %u\n", le32_to_cpu(sup->lpt_lebs));
 349                pr_err("\torph_lebs      %u\n", le32_to_cpu(sup->orph_lebs));
 350                pr_err("\tjhead_cnt      %u\n", le32_to_cpu(sup->jhead_cnt));
 351                pr_err("\tfanout         %u\n", le32_to_cpu(sup->fanout));
 352                pr_err("\tlsave_cnt      %u\n", le32_to_cpu(sup->lsave_cnt));
 353                pr_err("\tdefault_compr  %u\n",
 354                       (int)le16_to_cpu(sup->default_compr));
 355                pr_err("\trp_size        %llu\n",
 356                       (unsigned long long)le64_to_cpu(sup->rp_size));
 357                pr_err("\trp_uid         %u\n", le32_to_cpu(sup->rp_uid));
 358                pr_err("\trp_gid         %u\n", le32_to_cpu(sup->rp_gid));
 359                pr_err("\tfmt_version    %u\n", le32_to_cpu(sup->fmt_version));
 360                pr_err("\ttime_gran      %u\n", le32_to_cpu(sup->time_gran));
 361                pr_err("\tUUID           %pUB\n", sup->uuid);
 362                break;
 363        }
 364        case UBIFS_MST_NODE:
 365        {
 366                const struct ubifs_mst_node *mst = node;
 367
 368                pr_err("\thighest_inum   %llu\n",
 369                       (unsigned long long)le64_to_cpu(mst->highest_inum));
 370                pr_err("\tcommit number  %llu\n",
 371                       (unsigned long long)le64_to_cpu(mst->cmt_no));
 372                pr_err("\tflags          %#x\n", le32_to_cpu(mst->flags));
 373                pr_err("\tlog_lnum       %u\n", le32_to_cpu(mst->log_lnum));
 374                pr_err("\troot_lnum      %u\n", le32_to_cpu(mst->root_lnum));
 375                pr_err("\troot_offs      %u\n", le32_to_cpu(mst->root_offs));
 376                pr_err("\troot_len       %u\n", le32_to_cpu(mst->root_len));
 377                pr_err("\tgc_lnum        %u\n", le32_to_cpu(mst->gc_lnum));
 378                pr_err("\tihead_lnum     %u\n", le32_to_cpu(mst->ihead_lnum));
 379                pr_err("\tihead_offs     %u\n", le32_to_cpu(mst->ihead_offs));
 380                pr_err("\tindex_size     %llu\n",
 381                       (unsigned long long)le64_to_cpu(mst->index_size));
 382                pr_err("\tlpt_lnum       %u\n", le32_to_cpu(mst->lpt_lnum));
 383                pr_err("\tlpt_offs       %u\n", le32_to_cpu(mst->lpt_offs));
 384                pr_err("\tnhead_lnum     %u\n", le32_to_cpu(mst->nhead_lnum));
 385                pr_err("\tnhead_offs     %u\n", le32_to_cpu(mst->nhead_offs));
 386                pr_err("\tltab_lnum      %u\n", le32_to_cpu(mst->ltab_lnum));
 387                pr_err("\tltab_offs      %u\n", le32_to_cpu(mst->ltab_offs));
 388                pr_err("\tlsave_lnum     %u\n", le32_to_cpu(mst->lsave_lnum));
 389                pr_err("\tlsave_offs     %u\n", le32_to_cpu(mst->lsave_offs));
 390                pr_err("\tlscan_lnum     %u\n", le32_to_cpu(mst->lscan_lnum));
 391                pr_err("\tleb_cnt        %u\n", le32_to_cpu(mst->leb_cnt));
 392                pr_err("\tempty_lebs     %u\n", le32_to_cpu(mst->empty_lebs));
 393                pr_err("\tidx_lebs       %u\n", le32_to_cpu(mst->idx_lebs));
 394                pr_err("\ttotal_free     %llu\n",
 395                       (unsigned long long)le64_to_cpu(mst->total_free));
 396                pr_err("\ttotal_dirty    %llu\n",
 397                       (unsigned long long)le64_to_cpu(mst->total_dirty));
 398                pr_err("\ttotal_used     %llu\n",
 399                       (unsigned long long)le64_to_cpu(mst->total_used));
 400                pr_err("\ttotal_dead     %llu\n",
 401                       (unsigned long long)le64_to_cpu(mst->total_dead));
 402                pr_err("\ttotal_dark     %llu\n",
 403                       (unsigned long long)le64_to_cpu(mst->total_dark));
 404                break;
 405        }
 406        case UBIFS_REF_NODE:
 407        {
 408                const struct ubifs_ref_node *ref = node;
 409
 410                pr_err("\tlnum           %u\n", le32_to_cpu(ref->lnum));
 411                pr_err("\toffs           %u\n", le32_to_cpu(ref->offs));
 412                pr_err("\tjhead          %u\n", le32_to_cpu(ref->jhead));
 413                break;
 414        }
 415        case UBIFS_INO_NODE:
 416        {
 417                const struct ubifs_ino_node *ino = node;
 418
 419                key_read(c, &ino->key, &key);
 420                pr_err("\tkey            %s\n",
 421                       dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
 422                pr_err("\tcreat_sqnum    %llu\n",
 423                       (unsigned long long)le64_to_cpu(ino->creat_sqnum));
 424                pr_err("\tsize           %llu\n",
 425                       (unsigned long long)le64_to_cpu(ino->size));
 426                pr_err("\tnlink          %u\n", le32_to_cpu(ino->nlink));
 427                pr_err("\tatime          %lld.%u\n",
 428                       (long long)le64_to_cpu(ino->atime_sec),
 429                       le32_to_cpu(ino->atime_nsec));
 430                pr_err("\tmtime          %lld.%u\n",
 431                       (long long)le64_to_cpu(ino->mtime_sec),
 432                       le32_to_cpu(ino->mtime_nsec));
 433                pr_err("\tctime          %lld.%u\n",
 434                       (long long)le64_to_cpu(ino->ctime_sec),
 435                       le32_to_cpu(ino->ctime_nsec));
 436                pr_err("\tuid            %u\n", le32_to_cpu(ino->uid));
 437                pr_err("\tgid            %u\n", le32_to_cpu(ino->gid));
 438                pr_err("\tmode           %u\n", le32_to_cpu(ino->mode));
 439                pr_err("\tflags          %#x\n", le32_to_cpu(ino->flags));
 440                pr_err("\txattr_cnt      %u\n", le32_to_cpu(ino->xattr_cnt));
 441                pr_err("\txattr_size     %u\n", le32_to_cpu(ino->xattr_size));
 442                pr_err("\txattr_names    %u\n", le32_to_cpu(ino->xattr_names));
 443                pr_err("\tcompr_type     %#x\n",
 444                       (int)le16_to_cpu(ino->compr_type));
 445                pr_err("\tdata len       %u\n", le32_to_cpu(ino->data_len));
 446                break;
 447        }
 448        case UBIFS_DENT_NODE:
 449        case UBIFS_XENT_NODE:
 450        {
 451                const struct ubifs_dent_node *dent = node;
 452                int nlen = le16_to_cpu(dent->nlen);
 453
 454                key_read(c, &dent->key, &key);
 455                pr_err("\tkey            %s\n",
 456                       dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
 457                pr_err("\tinum           %llu\n",
 458                       (unsigned long long)le64_to_cpu(dent->inum));
 459                pr_err("\ttype           %d\n", (int)dent->type);
 460                pr_err("\tnlen           %d\n", nlen);
 461                pr_err("\tname           ");
 462
 463                if (nlen > UBIFS_MAX_NLEN)
 464                        pr_err("(bad name length, not printing, bad or corrupted node)");
 465                else {
 466                        for (i = 0; i < nlen && dent->name[i]; i++)
 467                                pr_cont("%c", dent->name[i]);
 468                }
 469                pr_cont("\n");
 470
 471                break;
 472        }
 473        case UBIFS_DATA_NODE:
 474        {
 475                const struct ubifs_data_node *dn = node;
 476                int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
 477
 478                key_read(c, &dn->key, &key);
 479                pr_err("\tkey            %s\n",
 480                       dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
 481                pr_err("\tsize           %u\n", le32_to_cpu(dn->size));
 482                pr_err("\tcompr_typ      %d\n",
 483                       (int)le16_to_cpu(dn->compr_type));
 484                pr_err("\tdata size      %d\n", dlen);
 485                pr_err("\tdata:\n");
 486                print_hex_dump(KERN_ERR, "\t", DUMP_PREFIX_OFFSET, 32, 1,
 487                               (void *)&dn->data, dlen, 0);
 488                break;
 489        }
 490        case UBIFS_TRUN_NODE:
 491        {
 492                const struct ubifs_trun_node *trun = node;
 493
 494                pr_err("\tinum           %u\n", le32_to_cpu(trun->inum));
 495                pr_err("\told_size       %llu\n",
 496                       (unsigned long long)le64_to_cpu(trun->old_size));
 497                pr_err("\tnew_size       %llu\n",
 498                       (unsigned long long)le64_to_cpu(trun->new_size));
 499                break;
 500        }
 501        case UBIFS_IDX_NODE:
 502        {
 503                const struct ubifs_idx_node *idx = node;
 504
 505                n = le16_to_cpu(idx->child_cnt);
 506                pr_err("\tchild_cnt      %d\n", n);
 507                pr_err("\tlevel          %d\n", (int)le16_to_cpu(idx->level));
 508                pr_err("\tBranches:\n");
 509
 510                for (i = 0; i < n && i < c->fanout - 1; i++) {
 511                        const struct ubifs_branch *br;
 512
 513                        br = ubifs_idx_branch(c, idx, i);
 514                        key_read(c, &br->key, &key);
 515                        pr_err("\t%d: LEB %d:%d len %d key %s\n",
 516                               i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
 517                               le32_to_cpu(br->len),
 518                               dbg_snprintf_key(c, &key, key_buf,
 519                                                DBG_KEY_BUF_LEN));
 520                }
 521                break;
 522        }
 523        case UBIFS_CS_NODE:
 524                break;
 525        case UBIFS_ORPH_NODE:
 526        {
 527                const struct ubifs_orph_node *orph = node;
 528
 529                pr_err("\tcommit number  %llu\n",
 530                       (unsigned long long)
 531                                le64_to_cpu(orph->cmt_no) & LLONG_MAX);
 532                pr_err("\tlast node flag %llu\n",
 533                       (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
 534                n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
 535                pr_err("\t%d orphan inode numbers:\n", n);
 536                for (i = 0; i < n; i++)
 537                        pr_err("\t  ino %llu\n",
 538                               (unsigned long long)le64_to_cpu(orph->inos[i]));
 539                break;
 540        }
 541        default:
 542                pr_err("node type %d was not recognized\n",
 543                       (int)ch->node_type);
 544        }
 545        spin_unlock(&dbg_lock);
 546}
 547
 548void ubifs_dump_budget_req(const struct ubifs_budget_req *req)
 549{
 550        spin_lock(&dbg_lock);
 551        pr_err("Budgeting request: new_ino %d, dirtied_ino %d\n",
 552               req->new_ino, req->dirtied_ino);
 553        pr_err("\tnew_ino_d   %d, dirtied_ino_d %d\n",
 554               req->new_ino_d, req->dirtied_ino_d);
 555        pr_err("\tnew_page    %d, dirtied_page %d\n",
 556               req->new_page, req->dirtied_page);
 557        pr_err("\tnew_dent    %d, mod_dent     %d\n",
 558               req->new_dent, req->mod_dent);
 559        pr_err("\tidx_growth  %d\n", req->idx_growth);
 560        pr_err("\tdata_growth %d dd_growth     %d\n",
 561               req->data_growth, req->dd_growth);
 562        spin_unlock(&dbg_lock);
 563}
 564
 565void ubifs_dump_lstats(const struct ubifs_lp_stats *lst)
 566{
 567        spin_lock(&dbg_lock);
 568        pr_err("(pid %d) Lprops statistics: empty_lebs %d, idx_lebs  %d\n",
 569               current->pid, lst->empty_lebs, lst->idx_lebs);
 570        pr_err("\ttaken_empty_lebs %d, total_free %lld, total_dirty %lld\n",
 571               lst->taken_empty_lebs, lst->total_free, lst->total_dirty);
 572        pr_err("\ttotal_used %lld, total_dark %lld, total_dead %lld\n",
 573               lst->total_used, lst->total_dark, lst->total_dead);
 574        spin_unlock(&dbg_lock);
 575}
 576
 577#ifndef __UBOOT__
 578void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)
 579{
 580        int i;
 581        struct rb_node *rb;
 582        struct ubifs_bud *bud;
 583        struct ubifs_gced_idx_leb *idx_gc;
 584        long long available, outstanding, free;
 585
 586        spin_lock(&c->space_lock);
 587        spin_lock(&dbg_lock);
 588        pr_err("(pid %d) Budgeting info: data budget sum %lld, total budget sum %lld\n",
 589               current->pid, bi->data_growth + bi->dd_growth,
 590               bi->data_growth + bi->dd_growth + bi->idx_growth);
 591        pr_err("\tbudg_data_growth %lld, budg_dd_growth %lld, budg_idx_growth %lld\n",
 592               bi->data_growth, bi->dd_growth, bi->idx_growth);
 593        pr_err("\tmin_idx_lebs %d, old_idx_sz %llu, uncommitted_idx %lld\n",
 594               bi->min_idx_lebs, bi->old_idx_sz, bi->uncommitted_idx);
 595        pr_err("\tpage_budget %d, inode_budget %d, dent_budget %d\n",
 596               bi->page_budget, bi->inode_budget, bi->dent_budget);
 597        pr_err("\tnospace %u, nospace_rp %u\n", bi->nospace, bi->nospace_rp);
 598        pr_err("\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
 599               c->dark_wm, c->dead_wm, c->max_idx_node_sz);
 600
 601        if (bi != &c->bi)
 602                /*
 603                 * If we are dumping saved budgeting data, do not print
 604                 * additional information which is about the current state, not
 605                 * the old one which corresponded to the saved budgeting data.
 606                 */
 607                goto out_unlock;
 608
 609        pr_err("\tfreeable_cnt %d, calc_idx_sz %lld, idx_gc_cnt %d\n",
 610               c->freeable_cnt, c->calc_idx_sz, c->idx_gc_cnt);
 611        pr_err("\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, clean_zn_cnt %ld\n",
 612               atomic_long_read(&c->dirty_pg_cnt),
 613               atomic_long_read(&c->dirty_zn_cnt),
 614               atomic_long_read(&c->clean_zn_cnt));
 615        pr_err("\tgc_lnum %d, ihead_lnum %d\n", c->gc_lnum, c->ihead_lnum);
 616
 617        /* If we are in R/O mode, journal heads do not exist */
 618        if (c->jheads)
 619                for (i = 0; i < c->jhead_cnt; i++)
 620                        pr_err("\tjhead %s\t LEB %d\n",
 621                               dbg_jhead(c->jheads[i].wbuf.jhead),
 622                               c->jheads[i].wbuf.lnum);
 623        for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
 624                bud = rb_entry(rb, struct ubifs_bud, rb);
 625                pr_err("\tbud LEB %d\n", bud->lnum);
 626        }
 627        list_for_each_entry(bud, &c->old_buds, list)
 628                pr_err("\told bud LEB %d\n", bud->lnum);
 629        list_for_each_entry(idx_gc, &c->idx_gc, list)
 630                pr_err("\tGC'ed idx LEB %d unmap %d\n",
 631                       idx_gc->lnum, idx_gc->unmap);
 632        pr_err("\tcommit state %d\n", c->cmt_state);
 633
 634        /* Print budgeting predictions */
 635        available = ubifs_calc_available(c, c->bi.min_idx_lebs);
 636        outstanding = c->bi.data_growth + c->bi.dd_growth;
 637        free = ubifs_get_free_space_nolock(c);
 638        pr_err("Budgeting predictions:\n");
 639        pr_err("\tavailable: %lld, outstanding %lld, free %lld\n",
 640               available, outstanding, free);
 641out_unlock:
 642        spin_unlock(&dbg_lock);
 643        spin_unlock(&c->space_lock);
 644}
 645#else
 646void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)
 647{
 648}
 649#endif
 650
 651void ubifs_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
 652{
 653        int i, spc, dark = 0, dead = 0;
 654        struct rb_node *rb;
 655        struct ubifs_bud *bud;
 656
 657        spc = lp->free + lp->dirty;
 658        if (spc < c->dead_wm)
 659                dead = spc;
 660        else
 661                dark = ubifs_calc_dark(c, spc);
 662
 663        if (lp->flags & LPROPS_INDEX)
 664                pr_err("LEB %-7d free %-8d dirty %-8d used %-8d free + dirty %-8d flags %#x (",
 665                       lp->lnum, lp->free, lp->dirty, c->leb_size - spc, spc,
 666                       lp->flags);
 667        else
 668                pr_err("LEB %-7d free %-8d dirty %-8d used %-8d free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d flags %#-4x (",
 669                       lp->lnum, lp->free, lp->dirty, c->leb_size - spc, spc,
 670                       dark, dead, (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags);
 671
 672        if (lp->flags & LPROPS_TAKEN) {
 673                if (lp->flags & LPROPS_INDEX)
 674                        pr_cont("index, taken");
 675                else
 676                        pr_cont("taken");
 677        } else {
 678                const char *s;
 679
 680                if (lp->flags & LPROPS_INDEX) {
 681                        switch (lp->flags & LPROPS_CAT_MASK) {
 682                        case LPROPS_DIRTY_IDX:
 683                                s = "dirty index";
 684                                break;
 685                        case LPROPS_FRDI_IDX:
 686                                s = "freeable index";
 687                                break;
 688                        default:
 689                                s = "index";
 690                        }
 691                } else {
 692                        switch (lp->flags & LPROPS_CAT_MASK) {
 693                        case LPROPS_UNCAT:
 694                                s = "not categorized";
 695                                break;
 696                        case LPROPS_DIRTY:
 697                                s = "dirty";
 698                                break;
 699                        case LPROPS_FREE:
 700                                s = "free";
 701                                break;
 702                        case LPROPS_EMPTY:
 703                                s = "empty";
 704                                break;
 705                        case LPROPS_FREEABLE:
 706                                s = "freeable";
 707                                break;
 708                        default:
 709                                s = NULL;
 710                                break;
 711                        }
 712                }
 713                pr_cont("%s", s);
 714        }
 715
 716        for (rb = rb_first((struct rb_root *)&c->buds); rb; rb = rb_next(rb)) {
 717                bud = rb_entry(rb, struct ubifs_bud, rb);
 718                if (bud->lnum == lp->lnum) {
 719                        int head = 0;
 720                        for (i = 0; i < c->jhead_cnt; i++) {
 721                                /*
 722                                 * Note, if we are in R/O mode or in the middle
 723                                 * of mounting/re-mounting, the write-buffers do
 724                                 * not exist.
 725                                 */
 726                                if (c->jheads &&
 727                                    lp->lnum == c->jheads[i].wbuf.lnum) {
 728                                        pr_cont(", jhead %s", dbg_jhead(i));
 729                                        head = 1;
 730                                }
 731                        }
 732                        if (!head)
 733                                pr_cont(", bud of jhead %s",
 734                                       dbg_jhead(bud->jhead));
 735                }
 736        }
 737        if (lp->lnum == c->gc_lnum)
 738                pr_cont(", GC LEB");
 739        pr_cont(")\n");
 740}
 741
 742void ubifs_dump_lprops(struct ubifs_info *c)
 743{
 744        int lnum, err;
 745        struct ubifs_lprops lp;
 746        struct ubifs_lp_stats lst;
 747
 748        pr_err("(pid %d) start dumping LEB properties\n", current->pid);
 749        ubifs_get_lp_stats(c, &lst);
 750        ubifs_dump_lstats(&lst);
 751
 752        for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
 753                err = ubifs_read_one_lp(c, lnum, &lp);
 754                if (err) {
 755                        ubifs_err(c, "cannot read lprops for LEB %d", lnum);
 756                        continue;
 757                }
 758
 759                ubifs_dump_lprop(c, &lp);
 760        }
 761        pr_err("(pid %d) finish dumping LEB properties\n", current->pid);
 762}
 763
 764void ubifs_dump_lpt_info(struct ubifs_info *c)
 765{
 766        int i;
 767
 768        spin_lock(&dbg_lock);
 769        pr_err("(pid %d) dumping LPT information\n", current->pid);
 770        pr_err("\tlpt_sz:        %lld\n", c->lpt_sz);
 771        pr_err("\tpnode_sz:      %d\n", c->pnode_sz);
 772        pr_err("\tnnode_sz:      %d\n", c->nnode_sz);
 773        pr_err("\tltab_sz:       %d\n", c->ltab_sz);
 774        pr_err("\tlsave_sz:      %d\n", c->lsave_sz);
 775        pr_err("\tbig_lpt:       %d\n", c->big_lpt);
 776        pr_err("\tlpt_hght:      %d\n", c->lpt_hght);
 777        pr_err("\tpnode_cnt:     %d\n", c->pnode_cnt);
 778        pr_err("\tnnode_cnt:     %d\n", c->nnode_cnt);
 779        pr_err("\tdirty_pn_cnt:  %d\n", c->dirty_pn_cnt);
 780        pr_err("\tdirty_nn_cnt:  %d\n", c->dirty_nn_cnt);
 781        pr_err("\tlsave_cnt:     %d\n", c->lsave_cnt);
 782        pr_err("\tspace_bits:    %d\n", c->space_bits);
 783        pr_err("\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
 784        pr_err("\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
 785        pr_err("\tlpt_spc_bits:  %d\n", c->lpt_spc_bits);
 786        pr_err("\tpcnt_bits:     %d\n", c->pcnt_bits);
 787        pr_err("\tlnum_bits:     %d\n", c->lnum_bits);
 788        pr_err("\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
 789        pr_err("\tLPT head is at %d:%d\n",
 790               c->nhead_lnum, c->nhead_offs);
 791        pr_err("\tLPT ltab is at %d:%d\n", c->ltab_lnum, c->ltab_offs);
 792        if (c->big_lpt)
 793                pr_err("\tLPT lsave is at %d:%d\n",
 794                       c->lsave_lnum, c->lsave_offs);
 795        for (i = 0; i < c->lpt_lebs; i++)
 796                pr_err("\tLPT LEB %d free %d dirty %d tgc %d cmt %d\n",
 797                       i + c->lpt_first, c->ltab[i].free, c->ltab[i].dirty,
 798                       c->ltab[i].tgc, c->ltab[i].cmt);
 799        spin_unlock(&dbg_lock);
 800}
 801
 802void ubifs_dump_sleb(const struct ubifs_info *c,
 803                     const struct ubifs_scan_leb *sleb, int offs)
 804{
 805        struct ubifs_scan_node *snod;
 806
 807        pr_err("(pid %d) start dumping scanned data from LEB %d:%d\n",
 808               current->pid, sleb->lnum, offs);
 809
 810        list_for_each_entry(snod, &sleb->nodes, list) {
 811                cond_resched();
 812                pr_err("Dumping node at LEB %d:%d len %d\n",
 813                       sleb->lnum, snod->offs, snod->len);
 814                ubifs_dump_node(c, snod->node);
 815        }
 816}
 817
 818void ubifs_dump_leb(const struct ubifs_info *c, int lnum)
 819{
 820        struct ubifs_scan_leb *sleb;
 821        struct ubifs_scan_node *snod;
 822        void *buf;
 823
 824        pr_err("(pid %d) start dumping LEB %d\n", current->pid, lnum);
 825
 826        buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
 827        if (!buf) {
 828                ubifs_err(c, "cannot allocate memory for dumping LEB %d", lnum);
 829                return;
 830        }
 831
 832        sleb = ubifs_scan(c, lnum, 0, buf, 0);
 833        if (IS_ERR(sleb)) {
 834                ubifs_err(c, "scan error %d", (int)PTR_ERR(sleb));
 835                goto out;
 836        }
 837
 838        pr_err("LEB %d has %d nodes ending at %d\n", lnum,
 839               sleb->nodes_cnt, sleb->endpt);
 840
 841        list_for_each_entry(snod, &sleb->nodes, list) {
 842                cond_resched();
 843                pr_err("Dumping node at LEB %d:%d len %d\n", lnum,
 844                       snod->offs, snod->len);
 845                ubifs_dump_node(c, snod->node);
 846        }
 847
 848        pr_err("(pid %d) finish dumping LEB %d\n", current->pid, lnum);
 849        ubifs_scan_destroy(sleb);
 850
 851out:
 852        vfree(buf);
 853        return;
 854}
 855
 856void ubifs_dump_znode(const struct ubifs_info *c,
 857                      const struct ubifs_znode *znode)
 858{
 859        int n;
 860        const struct ubifs_zbranch *zbr;
 861        char key_buf[DBG_KEY_BUF_LEN];
 862
 863        spin_lock(&dbg_lock);
 864        if (znode->parent)
 865                zbr = &znode->parent->zbranch[znode->iip];
 866        else
 867                zbr = &c->zroot;
 868
 869        pr_err("znode %p, LEB %d:%d len %d parent %p iip %d level %d child_cnt %d flags %lx\n",
 870               znode, zbr->lnum, zbr->offs, zbr->len, znode->parent, znode->iip,
 871               znode->level, znode->child_cnt, znode->flags);
 872
 873        if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
 874                spin_unlock(&dbg_lock);
 875                return;
 876        }
 877
 878        pr_err("zbranches:\n");
 879        for (n = 0; n < znode->child_cnt; n++) {
 880                zbr = &znode->zbranch[n];
 881                if (znode->level > 0)
 882                        pr_err("\t%d: znode %p LEB %d:%d len %d key %s\n",
 883                               n, zbr->znode, zbr->lnum, zbr->offs, zbr->len,
 884                               dbg_snprintf_key(c, &zbr->key, key_buf,
 885                                                DBG_KEY_BUF_LEN));
 886                else
 887                        pr_err("\t%d: LNC %p LEB %d:%d len %d key %s\n",
 888                               n, zbr->znode, zbr->lnum, zbr->offs, zbr->len,
 889                               dbg_snprintf_key(c, &zbr->key, key_buf,
 890                                                DBG_KEY_BUF_LEN));
 891        }
 892        spin_unlock(&dbg_lock);
 893}
 894
 895void ubifs_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
 896{
 897        int i;
 898
 899        pr_err("(pid %d) start dumping heap cat %d (%d elements)\n",
 900               current->pid, cat, heap->cnt);
 901        for (i = 0; i < heap->cnt; i++) {
 902                struct ubifs_lprops *lprops = heap->arr[i];
 903
 904                pr_err("\t%d. LEB %d hpos %d free %d dirty %d flags %d\n",
 905                       i, lprops->lnum, lprops->hpos, lprops->free,
 906                       lprops->dirty, lprops->flags);
 907        }
 908        pr_err("(pid %d) finish dumping heap\n", current->pid);
 909}
 910
 911void ubifs_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
 912                      struct ubifs_nnode *parent, int iip)
 913{
 914        int i;
 915
 916        pr_err("(pid %d) dumping pnode:\n", current->pid);
 917        pr_err("\taddress %zx parent %zx cnext %zx\n",
 918               (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
 919        pr_err("\tflags %lu iip %d level %d num %d\n",
 920               pnode->flags, iip, pnode->level, pnode->num);
 921        for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
 922                struct ubifs_lprops *lp = &pnode->lprops[i];
 923
 924                pr_err("\t%d: free %d dirty %d flags %d lnum %d\n",
 925                       i, lp->free, lp->dirty, lp->flags, lp->lnum);
 926        }
 927}
 928
 929void ubifs_dump_tnc(struct ubifs_info *c)
 930{
 931        struct ubifs_znode *znode;
 932        int level;
 933
 934        pr_err("\n");
 935        pr_err("(pid %d) start dumping TNC tree\n", current->pid);
 936        znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
 937        level = znode->level;
 938        pr_err("== Level %d ==\n", level);
 939        while (znode) {
 940                if (level != znode->level) {
 941                        level = znode->level;
 942                        pr_err("== Level %d ==\n", level);
 943                }
 944                ubifs_dump_znode(c, znode);
 945                znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
 946        }
 947        pr_err("(pid %d) finish dumping TNC tree\n", current->pid);
 948}
 949
 950static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
 951                      void *priv)
 952{
 953        ubifs_dump_znode(c, znode);
 954        return 0;
 955}
 956
 957/**
 958 * ubifs_dump_index - dump the on-flash index.
 959 * @c: UBIFS file-system description object
 960 *
 961 * This function dumps whole UBIFS indexing B-tree, unlike 'ubifs_dump_tnc()'
 962 * which dumps only in-memory znodes and does not read znodes which from flash.
 963 */
 964void ubifs_dump_index(struct ubifs_info *c)
 965{
 966        dbg_walk_index(c, NULL, dump_znode, NULL);
 967}
 968
 969#ifndef __UBOOT__
 970/**
 971 * dbg_save_space_info - save information about flash space.
 972 * @c: UBIFS file-system description object
 973 *
 974 * This function saves information about UBIFS free space, dirty space, etc, in
 975 * order to check it later.
 976 */
 977void dbg_save_space_info(struct ubifs_info *c)
 978{
 979        struct ubifs_debug_info *d = c->dbg;
 980        int freeable_cnt;
 981
 982        spin_lock(&c->space_lock);
 983        memcpy(&d->saved_lst, &c->lst, sizeof(struct ubifs_lp_stats));
 984        memcpy(&d->saved_bi, &c->bi, sizeof(struct ubifs_budg_info));
 985        d->saved_idx_gc_cnt = c->idx_gc_cnt;
 986
 987        /*
 988         * We use a dirty hack here and zero out @c->freeable_cnt, because it
 989         * affects the free space calculations, and UBIFS might not know about
 990         * all freeable eraseblocks. Indeed, we know about freeable eraseblocks
 991         * only when we read their lprops, and we do this only lazily, upon the
 992         * need. So at any given point of time @c->freeable_cnt might be not
 993         * exactly accurate.
 994         *
 995         * Just one example about the issue we hit when we did not zero
 996         * @c->freeable_cnt.
 997         * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the
 998         *    amount of free space in @d->saved_free
 999         * 2. We re-mount R/W, which makes UBIFS to read the "lsave"
1000         *    information from flash, where we cache LEBs from various
1001         *    categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()'
1002         *    -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()'
1003         *    -> 'ubifs_get_pnode()' -> 'update_cats()'
1004         *    -> 'ubifs_add_to_cat()').
1005         * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt
1006         *    becomes %1.
1007         * 4. We calculate the amount of free space when the re-mount is
1008         *    finished in 'dbg_check_space_info()' and it does not match
1009         *    @d->saved_free.
1010         */
1011        freeable_cnt = c->freeable_cnt;
1012        c->freeable_cnt = 0;
1013        d->saved_free = ubifs_get_free_space_nolock(c);
1014        c->freeable_cnt = freeable_cnt;
1015        spin_unlock(&c->space_lock);
1016}
1017
1018/**
1019 * dbg_check_space_info - check flash space information.
1020 * @c: UBIFS file-system description object
1021 *
1022 * This function compares current flash space information with the information
1023 * which was saved when the 'dbg_save_space_info()' function was called.
1024 * Returns zero if the information has not changed, and %-EINVAL it it has
1025 * changed.
1026 */
1027int dbg_check_space_info(struct ubifs_info *c)
1028{
1029        struct ubifs_debug_info *d = c->dbg;
1030        struct ubifs_lp_stats lst;
1031        long long free;
1032        int freeable_cnt;
1033
1034        spin_lock(&c->space_lock);
1035        freeable_cnt = c->freeable_cnt;
1036        c->freeable_cnt = 0;
1037        free = ubifs_get_free_space_nolock(c);
1038        c->freeable_cnt = freeable_cnt;
1039        spin_unlock(&c->space_lock);
1040
1041        if (free != d->saved_free) {
1042                ubifs_err(c, "free space changed from %lld to %lld",
1043                          d->saved_free, free);
1044                goto out;
1045        }
1046
1047        return 0;
1048
1049out:
1050        ubifs_msg(c, "saved lprops statistics dump");
1051        ubifs_dump_lstats(&d->saved_lst);
1052        ubifs_msg(c, "saved budgeting info dump");
1053        ubifs_dump_budg(c, &d->saved_bi);
1054        ubifs_msg(c, "saved idx_gc_cnt %d", d->saved_idx_gc_cnt);
1055        ubifs_msg(c, "current lprops statistics dump");
1056        ubifs_get_lp_stats(c, &lst);
1057        ubifs_dump_lstats(&lst);
1058        ubifs_msg(c, "current budgeting info dump");
1059        ubifs_dump_budg(c, &c->bi);
1060        dump_stack();
1061        return -EINVAL;
1062}
1063
1064/**
1065 * dbg_check_synced_i_size - check synchronized inode size.
1066 * @c: UBIFS file-system description object
1067 * @inode: inode to check
1068 *
1069 * If inode is clean, synchronized inode size has to be equivalent to current
1070 * inode size. This function has to be called only for locked inodes (@i_mutex
1071 * has to be locked). Returns %0 if synchronized inode size if correct, and
1072 * %-EINVAL if not.
1073 */
1074int dbg_check_synced_i_size(const struct ubifs_info *c, struct inode *inode)
1075{
1076        int err = 0;
1077        struct ubifs_inode *ui = ubifs_inode(inode);
1078
1079        if (!dbg_is_chk_gen(c))
1080                return 0;
1081        if (!S_ISREG(inode->i_mode))
1082                return 0;
1083
1084        mutex_lock(&ui->ui_mutex);
1085        spin_lock(&ui->ui_lock);
1086        if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
1087                ubifs_err(c, "ui_size is %lld, synced_i_size is %lld, but inode is clean",
1088                          ui->ui_size, ui->synced_i_size);
1089                ubifs_err(c, "i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
1090                          inode->i_mode, i_size_read(inode));
1091                dump_stack();
1092                err = -EINVAL;
1093        }
1094        spin_unlock(&ui->ui_lock);
1095        mutex_unlock(&ui->ui_mutex);
1096        return err;
1097}
1098
1099/*
1100 * dbg_check_dir - check directory inode size and link count.
1101 * @c: UBIFS file-system description object
1102 * @dir: the directory to calculate size for
1103 * @size: the result is returned here
1104 *
1105 * This function makes sure that directory size and link count are correct.
1106 * Returns zero in case of success and a negative error code in case of
1107 * failure.
1108 *
1109 * Note, it is good idea to make sure the @dir->i_mutex is locked before
1110 * calling this function.
1111 */
1112int dbg_check_dir(struct ubifs_info *c, const struct inode *dir)
1113{
1114        unsigned int nlink = 2;
1115        union ubifs_key key;
1116        struct ubifs_dent_node *dent, *pdent = NULL;
1117        struct qstr nm = { .name = NULL };
1118        loff_t size = UBIFS_INO_NODE_SZ;
1119
1120        if (!dbg_is_chk_gen(c))
1121                return 0;
1122
1123        if (!S_ISDIR(dir->i_mode))
1124                return 0;
1125
1126        lowest_dent_key(c, &key, dir->i_ino);
1127        while (1) {
1128                int err;
1129
1130                dent = ubifs_tnc_next_ent(c, &key, &nm);
1131                if (IS_ERR(dent)) {
1132                        err = PTR_ERR(dent);
1133                        if (err == -ENOENT)
1134                                break;
1135                        return err;
1136                }
1137
1138                nm.name = dent->name;
1139                nm.len = le16_to_cpu(dent->nlen);
1140                size += CALC_DENT_SIZE(nm.len);
1141                if (dent->type == UBIFS_ITYPE_DIR)
1142                        nlink += 1;
1143                kfree(pdent);
1144                pdent = dent;
1145                key_read(c, &dent->key, &key);
1146        }
1147        kfree(pdent);
1148
1149        if (i_size_read(dir) != size) {
1150                ubifs_err(c, "directory inode %lu has size %llu, but calculated size is %llu",
1151                          dir->i_ino, (unsigned long long)i_size_read(dir),
1152                          (unsigned long long)size);
1153                ubifs_dump_inode(c, dir);
1154                dump_stack();
1155                return -EINVAL;
1156        }
1157        if (dir->i_nlink != nlink) {
1158                ubifs_err(c, "directory inode %lu has nlink %u, but calculated nlink is %u",
1159                          dir->i_ino, dir->i_nlink, nlink);
1160                ubifs_dump_inode(c, dir);
1161                dump_stack();
1162                return -EINVAL;
1163        }
1164
1165        return 0;
1166}
1167
1168/**
1169 * dbg_check_key_order - make sure that colliding keys are properly ordered.
1170 * @c: UBIFS file-system description object
1171 * @zbr1: first zbranch
1172 * @zbr2: following zbranch
1173 *
1174 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
1175 * names of the direntries/xentries which are referred by the keys. This
1176 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
1177 * sure the name of direntry/xentry referred by @zbr1 is less than
1178 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
1179 * and a negative error code in case of failure.
1180 */
1181static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
1182                               struct ubifs_zbranch *zbr2)
1183{
1184        int err, nlen1, nlen2, cmp;
1185        struct ubifs_dent_node *dent1, *dent2;
1186        union ubifs_key key;
1187        char key_buf[DBG_KEY_BUF_LEN];
1188
1189        ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
1190        dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1191        if (!dent1)
1192                return -ENOMEM;
1193        dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1194        if (!dent2) {
1195                err = -ENOMEM;
1196                goto out_free;
1197        }
1198
1199        err = ubifs_tnc_read_node(c, zbr1, dent1);
1200        if (err)
1201                goto out_free;
1202        err = ubifs_validate_entry(c, dent1);
1203        if (err)
1204                goto out_free;
1205
1206        err = ubifs_tnc_read_node(c, zbr2, dent2);
1207        if (err)
1208                goto out_free;
1209        err = ubifs_validate_entry(c, dent2);
1210        if (err)
1211                goto out_free;
1212
1213        /* Make sure node keys are the same as in zbranch */
1214        err = 1;
1215        key_read(c, &dent1->key, &key);
1216        if (keys_cmp(c, &zbr1->key, &key)) {
1217                ubifs_err(c, "1st entry at %d:%d has key %s", zbr1->lnum,
1218                          zbr1->offs, dbg_snprintf_key(c, &key, key_buf,
1219                                                       DBG_KEY_BUF_LEN));
1220                ubifs_err(c, "but it should have key %s according to tnc",
1221                          dbg_snprintf_key(c, &zbr1->key, key_buf,
1222                                           DBG_KEY_BUF_LEN));
1223                ubifs_dump_node(c, dent1);
1224                goto out_free;
1225        }
1226
1227        key_read(c, &dent2->key, &key);
1228        if (keys_cmp(c, &zbr2->key, &key)) {
1229                ubifs_err(c, "2nd entry at %d:%d has key %s", zbr1->lnum,
1230                          zbr1->offs, dbg_snprintf_key(c, &key, key_buf,
1231                                                       DBG_KEY_BUF_LEN));
1232                ubifs_err(c, "but it should have key %s according to tnc",
1233                          dbg_snprintf_key(c, &zbr2->key, key_buf,
1234                                           DBG_KEY_BUF_LEN));
1235                ubifs_dump_node(c, dent2);
1236                goto out_free;
1237        }
1238
1239        nlen1 = le16_to_cpu(dent1->nlen);
1240        nlen2 = le16_to_cpu(dent2->nlen);
1241
1242        cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
1243        if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
1244                err = 0;
1245                goto out_free;
1246        }
1247        if (cmp == 0 && nlen1 == nlen2)
1248                ubifs_err(c, "2 xent/dent nodes with the same name");
1249        else
1250                ubifs_err(c, "bad order of colliding key %s",
1251                          dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
1252
1253        ubifs_msg(c, "first node at %d:%d\n", zbr1->lnum, zbr1->offs);
1254        ubifs_dump_node(c, dent1);
1255        ubifs_msg(c, "second node at %d:%d\n", zbr2->lnum, zbr2->offs);
1256        ubifs_dump_node(c, dent2);
1257
1258out_free:
1259        kfree(dent2);
1260        kfree(dent1);
1261        return err;
1262}
1263
1264/**
1265 * dbg_check_znode - check if znode is all right.
1266 * @c: UBIFS file-system description object
1267 * @zbr: zbranch which points to this znode
1268 *
1269 * This function makes sure that znode referred to by @zbr is all right.
1270 * Returns zero if it is, and %-EINVAL if it is not.
1271 */
1272static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
1273{
1274        struct ubifs_znode *znode = zbr->znode;
1275        struct ubifs_znode *zp = znode->parent;
1276        int n, err, cmp;
1277
1278        if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
1279                err = 1;
1280                goto out;
1281        }
1282        if (znode->level < 0) {
1283                err = 2;
1284                goto out;
1285        }
1286        if (znode->iip < 0 || znode->iip >= c->fanout) {
1287                err = 3;
1288                goto out;
1289        }
1290
1291        if (zbr->len == 0)
1292                /* Only dirty zbranch may have no on-flash nodes */
1293                if (!ubifs_zn_dirty(znode)) {
1294                        err = 4;
1295                        goto out;
1296                }
1297
1298        if (ubifs_zn_dirty(znode)) {
1299                /*
1300                 * If znode is dirty, its parent has to be dirty as well. The
1301                 * order of the operation is important, so we have to have
1302                 * memory barriers.
1303                 */
1304                smp_mb();
1305                if (zp && !ubifs_zn_dirty(zp)) {
1306                        /*
1307                         * The dirty flag is atomic and is cleared outside the
1308                         * TNC mutex, so znode's dirty flag may now have
1309                         * been cleared. The child is always cleared before the
1310                         * parent, so we just need to check again.
1311                         */
1312                        smp_mb();
1313                        if (ubifs_zn_dirty(znode)) {
1314                                err = 5;
1315                                goto out;
1316                        }
1317                }
1318        }
1319
1320        if (zp) {
1321                const union ubifs_key *min, *max;
1322
1323                if (znode->level != zp->level - 1) {
1324                        err = 6;
1325                        goto out;
1326                }
1327
1328                /* Make sure the 'parent' pointer in our znode is correct */
1329                err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
1330                if (!err) {
1331                        /* This zbranch does not exist in the parent */
1332                        err = 7;
1333                        goto out;
1334                }
1335
1336                if (znode->iip >= zp->child_cnt) {
1337                        err = 8;
1338                        goto out;
1339                }
1340
1341                if (znode->iip != n) {
1342                        /* This may happen only in case of collisions */
1343                        if (keys_cmp(c, &zp->zbranch[n].key,
1344                                     &zp->zbranch[znode->iip].key)) {
1345                                err = 9;
1346                                goto out;
1347                        }
1348                        n = znode->iip;
1349                }
1350
1351                /*
1352                 * Make sure that the first key in our znode is greater than or
1353                 * equal to the key in the pointing zbranch.
1354                 */
1355                min = &zbr->key;
1356                cmp = keys_cmp(c, min, &znode->zbranch[0].key);
1357                if (cmp == 1) {
1358                        err = 10;
1359                        goto out;
1360                }
1361
1362                if (n + 1 < zp->child_cnt) {
1363                        max = &zp->zbranch[n + 1].key;
1364
1365                        /*
1366                         * Make sure the last key in our znode is less or
1367                         * equivalent than the key in the zbranch which goes
1368                         * after our pointing zbranch.
1369                         */
1370                        cmp = keys_cmp(c, max,
1371                                &znode->zbranch[znode->child_cnt - 1].key);
1372                        if (cmp == -1) {
1373                                err = 11;
1374                                goto out;
1375                        }
1376                }
1377        } else {
1378                /* This may only be root znode */
1379                if (zbr != &c->zroot) {
1380                        err = 12;
1381                        goto out;
1382                }
1383        }
1384
1385        /*
1386         * Make sure that next key is greater or equivalent then the previous
1387         * one.
1388         */
1389        for (n = 1; n < znode->child_cnt; n++) {
1390                cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
1391                               &znode->zbranch[n].key);
1392                if (cmp > 0) {
1393                        err = 13;
1394                        goto out;
1395                }
1396                if (cmp == 0) {
1397                        /* This can only be keys with colliding hash */
1398                        if (!is_hash_key(c, &znode->zbranch[n].key)) {
1399                                err = 14;
1400                                goto out;
1401                        }
1402
1403                        if (znode->level != 0 || c->replaying)
1404                                continue;
1405
1406                        /*
1407                         * Colliding keys should follow binary order of
1408                         * corresponding xentry/dentry names.
1409                         */
1410                        err = dbg_check_key_order(c, &znode->zbranch[n - 1],
1411                                                  &znode->zbranch[n]);
1412                        if (err < 0)
1413                                return err;
1414                        if (err) {
1415                                err = 15;
1416                                goto out;
1417                        }
1418                }
1419        }
1420
1421        for (n = 0; n < znode->child_cnt; n++) {
1422                if (!znode->zbranch[n].znode &&
1423                    (znode->zbranch[n].lnum == 0 ||
1424                     znode->zbranch[n].len == 0)) {
1425                        err = 16;
1426                        goto out;
1427                }
1428
1429                if (znode->zbranch[n].lnum != 0 &&
1430                    znode->zbranch[n].len == 0) {
1431                        err = 17;
1432                        goto out;
1433                }
1434
1435                if (znode->zbranch[n].lnum == 0 &&
1436                    znode->zbranch[n].len != 0) {
1437                        err = 18;
1438                        goto out;
1439                }
1440
1441                if (znode->zbranch[n].lnum == 0 &&
1442                    znode->zbranch[n].offs != 0) {
1443                        err = 19;
1444                        goto out;
1445                }
1446
1447                if (znode->level != 0 && znode->zbranch[n].znode)
1448                        if (znode->zbranch[n].znode->parent != znode) {
1449                                err = 20;
1450                                goto out;
1451                        }
1452        }
1453
1454        return 0;
1455
1456out:
1457        ubifs_err(c, "failed, error %d", err);
1458        ubifs_msg(c, "dump of the znode");
1459        ubifs_dump_znode(c, znode);
1460        if (zp) {
1461                ubifs_msg(c, "dump of the parent znode");
1462                ubifs_dump_znode(c, zp);
1463        }
1464        dump_stack();
1465        return -EINVAL;
1466}
1467#else
1468
1469int dbg_check_dir(struct ubifs_info *c, const struct inode *dir)
1470{
1471        return 0;
1472}
1473
1474void dbg_debugfs_exit_fs(struct ubifs_info *c)
1475{
1476        return;
1477}
1478
1479int ubifs_debugging_init(struct ubifs_info *c)
1480{
1481        return 0;
1482}
1483void ubifs_debugging_exit(struct ubifs_info *c)
1484{
1485}
1486int dbg_check_filesystem(struct ubifs_info *c)
1487{
1488        return 0;
1489}
1490int dbg_debugfs_init_fs(struct ubifs_info *c)
1491{
1492        return 0;
1493}
1494#endif
1495
1496#ifndef __UBOOT__
1497/**
1498 * dbg_check_tnc - check TNC tree.
1499 * @c: UBIFS file-system description object
1500 * @extra: do extra checks that are possible at start commit
1501 *
1502 * This function traverses whole TNC tree and checks every znode. Returns zero
1503 * if everything is all right and %-EINVAL if something is wrong with TNC.
1504 */
1505int dbg_check_tnc(struct ubifs_info *c, int extra)
1506{
1507        struct ubifs_znode *znode;
1508        long clean_cnt = 0, dirty_cnt = 0;
1509        int err, last;
1510
1511        if (!dbg_is_chk_index(c))
1512                return 0;
1513
1514        ubifs_assert(mutex_is_locked(&c->tnc_mutex));
1515        if (!c->zroot.znode)
1516                return 0;
1517
1518        znode = ubifs_tnc_postorder_first(c->zroot.znode);
1519        while (1) {
1520                struct ubifs_znode *prev;
1521                struct ubifs_zbranch *zbr;
1522
1523                if (!znode->parent)
1524                        zbr = &c->zroot;
1525                else
1526                        zbr = &znode->parent->zbranch[znode->iip];
1527
1528                err = dbg_check_znode(c, zbr);
1529                if (err)
1530                        return err;
1531
1532                if (extra) {
1533                        if (ubifs_zn_dirty(znode))
1534                                dirty_cnt += 1;
1535                        else
1536                                clean_cnt += 1;
1537                }
1538
1539                prev = znode;
1540                znode = ubifs_tnc_postorder_next(znode);
1541                if (!znode)
1542                        break;
1543
1544                /*
1545                 * If the last key of this znode is equivalent to the first key
1546                 * of the next znode (collision), then check order of the keys.
1547                 */
1548                last = prev->child_cnt - 1;
1549                if (prev->level == 0 && znode->level == 0 && !c->replaying &&
1550                    !keys_cmp(c, &prev->zbranch[last].key,
1551                              &znode->zbranch[0].key)) {
1552                        err = dbg_check_key_order(c, &prev->zbranch[last],
1553                                                  &znode->zbranch[0]);
1554                        if (err < 0)
1555                                return err;
1556                        if (err) {
1557                                ubifs_msg(c, "first znode");
1558                                ubifs_dump_znode(c, prev);
1559                                ubifs_msg(c, "second znode");
1560                                ubifs_dump_znode(c, znode);
1561                                return -EINVAL;
1562                        }
1563                }
1564        }
1565
1566        if (extra) {
1567                if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
1568                        ubifs_err(c, "incorrect clean_zn_cnt %ld, calculated %ld",
1569                                  atomic_long_read(&c->clean_zn_cnt),
1570                                  clean_cnt);
1571                        return -EINVAL;
1572                }
1573                if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
1574                        ubifs_err(c, "incorrect dirty_zn_cnt %ld, calculated %ld",
1575                                  atomic_long_read(&c->dirty_zn_cnt),
1576                                  dirty_cnt);
1577                        return -EINVAL;
1578                }
1579        }
1580
1581        return 0;
1582}
1583#else
1584int dbg_check_tnc(struct ubifs_info *c, int extra)
1585{
1586        return 0;
1587}
1588#endif
1589
1590/**
1591 * dbg_walk_index - walk the on-flash index.
1592 * @c: UBIFS file-system description object
1593 * @leaf_cb: called for each leaf node
1594 * @znode_cb: called for each indexing node
1595 * @priv: private data which is passed to callbacks
1596 *
1597 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1598 * node and @znode_cb for each indexing node. Returns zero in case of success
1599 * and a negative error code in case of failure.
1600 *
1601 * It would be better if this function removed every znode it pulled to into
1602 * the TNC, so that the behavior more closely matched the non-debugging
1603 * behavior.
1604 */
1605int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1606                   dbg_znode_callback znode_cb, void *priv)
1607{
1608        int err;
1609        struct ubifs_zbranch *zbr;
1610        struct ubifs_znode *znode, *child;
1611
1612        mutex_lock(&c->tnc_mutex);
1613        /* If the root indexing node is not in TNC - pull it */
1614        if (!c->zroot.znode) {
1615                c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1616                if (IS_ERR(c->zroot.znode)) {
1617                        err = PTR_ERR(c->zroot.znode);
1618                        c->zroot.znode = NULL;
1619                        goto out_unlock;
1620                }
1621        }
1622
1623        /*
1624         * We are going to traverse the indexing tree in the postorder manner.
1625         * Go down and find the leftmost indexing node where we are going to
1626         * start from.
1627         */
1628        znode = c->zroot.znode;
1629        while (znode->level > 0) {
1630                zbr = &znode->zbranch[0];
1631                child = zbr->znode;
1632                if (!child) {
1633                        child = ubifs_load_znode(c, zbr, znode, 0);
1634                        if (IS_ERR(child)) {
1635                                err = PTR_ERR(child);
1636                                goto out_unlock;
1637                        }
1638                        zbr->znode = child;
1639                }
1640
1641                znode = child;
1642        }
1643
1644        /* Iterate over all indexing nodes */
1645        while (1) {
1646                int idx;
1647
1648                cond_resched();
1649
1650                if (znode_cb) {
1651                        err = znode_cb(c, znode, priv);
1652                        if (err) {
1653                                ubifs_err(c, "znode checking function returned error %d",
1654                                          err);
1655                                ubifs_dump_znode(c, znode);
1656                                goto out_dump;
1657                        }
1658                }
1659                if (leaf_cb && znode->level == 0) {
1660                        for (idx = 0; idx < znode->child_cnt; idx++) {
1661                                zbr = &znode->zbranch[idx];
1662                                err = leaf_cb(c, zbr, priv);
1663                                if (err) {
1664                                        ubifs_err(c, "leaf checking function returned error %d, for leaf at LEB %d:%d",
1665                                                  err, zbr->lnum, zbr->offs);
1666                                        goto out_dump;
1667                                }
1668                        }
1669                }
1670
1671                if (!znode->parent)
1672                        break;
1673
1674                idx = znode->iip + 1;
1675                znode = znode->parent;
1676                if (idx < znode->child_cnt) {
1677                        /* Switch to the next index in the parent */
1678                        zbr = &znode->zbranch[idx];
1679                        child = zbr->znode;
1680                        if (!child) {
1681                                child = ubifs_load_znode(c, zbr, znode, idx);
1682                                if (IS_ERR(child)) {
1683                                        err = PTR_ERR(child);
1684                                        goto out_unlock;
1685                                }
1686                                zbr->znode = child;
1687                        }
1688                        znode = child;
1689                } else
1690                        /*
1691                         * This is the last child, switch to the parent and
1692                         * continue.
1693                         */
1694                        continue;
1695
1696                /* Go to the lowest leftmost znode in the new sub-tree */
1697                while (znode->level > 0) {
1698                        zbr = &znode->zbranch[0];
1699                        child = zbr->znode;
1700                        if (!child) {
1701                                child = ubifs_load_znode(c, zbr, znode, 0);
1702                                if (IS_ERR(child)) {
1703                                        err = PTR_ERR(child);
1704                                        goto out_unlock;
1705                                }
1706                                zbr->znode = child;
1707                        }
1708                        znode = child;
1709                }
1710        }
1711
1712        mutex_unlock(&c->tnc_mutex);
1713        return 0;
1714
1715out_dump:
1716        if (znode->parent)
1717                zbr = &znode->parent->zbranch[znode->iip];
1718        else
1719                zbr = &c->zroot;
1720        ubifs_msg(c, "dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1721        ubifs_dump_znode(c, znode);
1722out_unlock:
1723        mutex_unlock(&c->tnc_mutex);
1724        return err;
1725}
1726
1727/**
1728 * add_size - add znode size to partially calculated index size.
1729 * @c: UBIFS file-system description object
1730 * @znode: znode to add size for
1731 * @priv: partially calculated index size
1732 *
1733 * This is a helper function for 'dbg_check_idx_size()' which is called for
1734 * every indexing node and adds its size to the 'long long' variable pointed to
1735 * by @priv.
1736 */
1737static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1738{
1739        long long *idx_size = priv;
1740        int add;
1741
1742        add = ubifs_idx_node_sz(c, znode->child_cnt);
1743        add = ALIGN(add, 8);
1744        *idx_size += add;
1745        return 0;
1746}
1747
1748/**
1749 * dbg_check_idx_size - check index size.
1750 * @c: UBIFS file-system description object
1751 * @idx_size: size to check
1752 *
1753 * This function walks the UBIFS index, calculates its size and checks that the
1754 * size is equivalent to @idx_size. Returns zero in case of success and a
1755 * negative error code in case of failure.
1756 */
1757int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
1758{
1759        int err;
1760        long long calc = 0;
1761
1762        if (!dbg_is_chk_index(c))
1763                return 0;
1764
1765        err = dbg_walk_index(c, NULL, add_size, &calc);
1766        if (err) {
1767                ubifs_err(c, "error %d while walking the index", err);
1768                return err;
1769        }
1770
1771        if (calc != idx_size) {
1772                ubifs_err(c, "index size check failed: calculated size is %lld, should be %lld",
1773                          calc, idx_size);
1774                dump_stack();
1775                return -EINVAL;
1776        }
1777
1778        return 0;
1779}
1780
1781#ifndef __UBOOT__
1782/**
1783 * struct fsck_inode - information about an inode used when checking the file-system.
1784 * @rb: link in the RB-tree of inodes
1785 * @inum: inode number
1786 * @mode: inode type, permissions, etc
1787 * @nlink: inode link count
1788 * @xattr_cnt: count of extended attributes
1789 * @references: how many directory/xattr entries refer this inode (calculated
1790 *              while walking the index)
1791 * @calc_cnt: for directory inode count of child directories
1792 * @size: inode size (read from on-flash inode)
1793 * @xattr_sz: summary size of all extended attributes (read from on-flash
1794 *            inode)
1795 * @calc_sz: for directories calculated directory size
1796 * @calc_xcnt: count of extended attributes
1797 * @calc_xsz: calculated summary size of all extended attributes
1798 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1799 *             inode (read from on-flash inode)
1800 * @calc_xnms: calculated sum of lengths of all extended attribute names
1801 */
1802struct fsck_inode {
1803        struct rb_node rb;
1804        ino_t inum;
1805        umode_t mode;
1806        unsigned int nlink;
1807        unsigned int xattr_cnt;
1808        int references;
1809        int calc_cnt;
1810        long long size;
1811        unsigned int xattr_sz;
1812        long long calc_sz;
1813        long long calc_xcnt;
1814        long long calc_xsz;
1815        unsigned int xattr_nms;
1816        long long calc_xnms;
1817};
1818
1819/**
1820 * struct fsck_data - private FS checking information.
1821 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1822 */
1823struct fsck_data {
1824        struct rb_root inodes;
1825};
1826
1827/**
1828 * add_inode - add inode information to RB-tree of inodes.
1829 * @c: UBIFS file-system description object
1830 * @fsckd: FS checking information
1831 * @ino: raw UBIFS inode to add
1832 *
1833 * This is a helper function for 'check_leaf()' which adds information about
1834 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1835 * case of success and a negative error code in case of failure.
1836 */
1837static struct fsck_inode *add_inode(struct ubifs_info *c,
1838                                    struct fsck_data *fsckd,
1839                                    struct ubifs_ino_node *ino)
1840{
1841        struct rb_node **p, *parent = NULL;
1842        struct fsck_inode *fscki;
1843        ino_t inum = key_inum_flash(c, &ino->key);
1844        struct inode *inode;
1845        struct ubifs_inode *ui;
1846
1847        p = &fsckd->inodes.rb_node;
1848        while (*p) {
1849                parent = *p;
1850                fscki = rb_entry(parent, struct fsck_inode, rb);
1851                if (inum < fscki->inum)
1852                        p = &(*p)->rb_left;
1853                else if (inum > fscki->inum)
1854                        p = &(*p)->rb_right;
1855                else
1856                        return fscki;
1857        }
1858
1859        if (inum > c->highest_inum) {
1860                ubifs_err(c, "too high inode number, max. is %lu",
1861                          (unsigned long)c->highest_inum);
1862                return ERR_PTR(-EINVAL);
1863        }
1864
1865        fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
1866        if (!fscki)
1867                return ERR_PTR(-ENOMEM);
1868
1869        inode = ilookup(c->vfs_sb, inum);
1870
1871        fscki->inum = inum;
1872        /*
1873         * If the inode is present in the VFS inode cache, use it instead of
1874         * the on-flash inode which might be out-of-date. E.g., the size might
1875         * be out-of-date. If we do not do this, the following may happen, for
1876         * example:
1877         *   1. A power cut happens
1878         *   2. We mount the file-system R/O, the replay process fixes up the
1879         *      inode size in the VFS cache, but on on-flash.
1880         *   3. 'check_leaf()' fails because it hits a data node beyond inode
1881         *      size.
1882         */
1883        if (!inode) {
1884                fscki->nlink = le32_to_cpu(ino->nlink);
1885                fscki->size = le64_to_cpu(ino->size);
1886                fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
1887                fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
1888                fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
1889                fscki->mode = le32_to_cpu(ino->mode);
1890        } else {
1891                ui = ubifs_inode(inode);
1892                fscki->nlink = inode->i_nlink;
1893                fscki->size = inode->i_size;
1894                fscki->xattr_cnt = ui->xattr_cnt;
1895                fscki->xattr_sz = ui->xattr_size;
1896                fscki->xattr_nms = ui->xattr_names;
1897                fscki->mode = inode->i_mode;
1898                iput(inode);
1899        }
1900
1901        if (S_ISDIR(fscki->mode)) {
1902                fscki->calc_sz = UBIFS_INO_NODE_SZ;
1903                fscki->calc_cnt = 2;
1904        }
1905
1906        rb_link_node(&fscki->rb, parent, p);
1907        rb_insert_color(&fscki->rb, &fsckd->inodes);
1908
1909        return fscki;
1910}
1911
1912/**
1913 * search_inode - search inode in the RB-tree of inodes.
1914 * @fsckd: FS checking information
1915 * @inum: inode number to search
1916 *
1917 * This is a helper function for 'check_leaf()' which searches inode @inum in
1918 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1919 * the inode was not found.
1920 */
1921static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
1922{
1923        struct rb_node *p;
1924        struct fsck_inode *fscki;
1925
1926        p = fsckd->inodes.rb_node;
1927        while (p) {
1928                fscki = rb_entry(p, struct fsck_inode, rb);
1929                if (inum < fscki->inum)
1930                        p = p->rb_left;
1931                else if (inum > fscki->inum)
1932                        p = p->rb_right;
1933                else
1934                        return fscki;
1935        }
1936        return NULL;
1937}
1938
1939/**
1940 * read_add_inode - read inode node and add it to RB-tree of inodes.
1941 * @c: UBIFS file-system description object
1942 * @fsckd: FS checking information
1943 * @inum: inode number to read
1944 *
1945 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1946 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1947 * information pointer in case of success and a negative error code in case of
1948 * failure.
1949 */
1950static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1951                                         struct fsck_data *fsckd, ino_t inum)
1952{
1953        int n, err;
1954        union ubifs_key key;
1955        struct ubifs_znode *znode;
1956        struct ubifs_zbranch *zbr;
1957        struct ubifs_ino_node *ino;
1958        struct fsck_inode *fscki;
1959
1960        fscki = search_inode(fsckd, inum);
1961        if (fscki)
1962                return fscki;
1963
1964        ino_key_init(c, &key, inum);
1965        err = ubifs_lookup_level0(c, &key, &znode, &n);
1966        if (!err) {
1967                ubifs_err(c, "inode %lu not found in index", (unsigned long)inum);
1968                return ERR_PTR(-ENOENT);
1969        } else if (err < 0) {
1970                ubifs_err(c, "error %d while looking up inode %lu",
1971                          err, (unsigned long)inum);
1972                return ERR_PTR(err);
1973        }
1974
1975        zbr = &znode->zbranch[n];
1976        if (zbr->len < UBIFS_INO_NODE_SZ) {
1977                ubifs_err(c, "bad node %lu node length %d",
1978                          (unsigned long)inum, zbr->len);
1979                return ERR_PTR(-EINVAL);
1980        }
1981
1982        ino = kmalloc(zbr->len, GFP_NOFS);
1983        if (!ino)
1984                return ERR_PTR(-ENOMEM);
1985
1986        err = ubifs_tnc_read_node(c, zbr, ino);
1987        if (err) {
1988                ubifs_err(c, "cannot read inode node at LEB %d:%d, error %d",
1989                          zbr->lnum, zbr->offs, err);
1990                kfree(ino);
1991                return ERR_PTR(err);
1992        }
1993
1994        fscki = add_inode(c, fsckd, ino);
1995        kfree(ino);
1996        if (IS_ERR(fscki)) {
1997                ubifs_err(c, "error %ld while adding inode %lu node",
1998                          PTR_ERR(fscki), (unsigned long)inum);
1999                return fscki;
2000        }
2001
2002        return fscki;
2003}
2004
2005/**
2006 * check_leaf - check leaf node.
2007 * @c: UBIFS file-system description object
2008 * @zbr: zbranch of the leaf node to check
2009 * @priv: FS checking information
2010 *
2011 * This is a helper function for 'dbg_check_filesystem()' which is called for
2012 * every single leaf node while walking the indexing tree. It checks that the
2013 * leaf node referred from the indexing tree exists, has correct CRC, and does
2014 * some other basic validation. This function is also responsible for building
2015 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
2016 * calculates reference count, size, etc for each inode in order to later
2017 * compare them to the information stored inside the inodes and detect possible
2018 * inconsistencies. Returns zero in case of success and a negative error code
2019 * in case of failure.
2020 */
2021static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
2022                      void *priv)
2023{
2024        ino_t inum;
2025        void *node;
2026        struct ubifs_ch *ch;
2027        int err, type = key_type(c, &zbr->key);
2028        struct fsck_inode *fscki;
2029
2030        if (zbr->len < UBIFS_CH_SZ) {
2031                ubifs_err(c, "bad leaf length %d (LEB %d:%d)",
2032                          zbr->len, zbr->lnum, zbr->offs);
2033                return -EINVAL;
2034        }
2035
2036        node = kmalloc(zbr->len, GFP_NOFS);
2037        if (!node)
2038                return -ENOMEM;
2039
2040        err = ubifs_tnc_read_node(c, zbr, node);
2041        if (err) {
2042                ubifs_err(c, "cannot read leaf node at LEB %d:%d, error %d",
2043                          zbr->lnum, zbr->offs, err);
2044                goto out_free;
2045        }
2046
2047        /* If this is an inode node, add it to RB-tree of inodes */
2048        if (type == UBIFS_INO_KEY) {
2049                fscki = add_inode(c, priv, node);
2050                if (IS_ERR(fscki)) {
2051                        err = PTR_ERR(fscki);
2052                        ubifs_err(c, "error %d while adding inode node", err);
2053                        goto out_dump;
2054                }
2055                goto out;
2056        }
2057
2058        if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
2059            type != UBIFS_DATA_KEY) {
2060                ubifs_err(c, "unexpected node type %d at LEB %d:%d",
2061                          type, zbr->lnum, zbr->offs);
2062                err = -EINVAL;
2063                goto out_free;
2064        }
2065
2066        ch = node;
2067        if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
2068                ubifs_err(c, "too high sequence number, max. is %llu",
2069                          c->max_sqnum);
2070                err = -EINVAL;
2071                goto out_dump;
2072        }
2073
2074        if (type == UBIFS_DATA_KEY) {
2075                long long blk_offs;
2076                struct ubifs_data_node *dn = node;
2077
2078                ubifs_assert(zbr->len >= UBIFS_DATA_NODE_SZ);
2079
2080                /*
2081                 * Search the inode node this data node belongs to and insert
2082                 * it to the RB-tree of inodes.
2083                 */
2084                inum = key_inum_flash(c, &dn->key);
2085                fscki = read_add_inode(c, priv, inum);
2086                if (IS_ERR(fscki)) {
2087                        err = PTR_ERR(fscki);
2088                        ubifs_err(c, "error %d while processing data node and trying to find inode node %lu",
2089                                  err, (unsigned long)inum);
2090                        goto out_dump;
2091                }
2092
2093                /* Make sure the data node is within inode size */
2094                blk_offs = key_block_flash(c, &dn->key);
2095                blk_offs <<= UBIFS_BLOCK_SHIFT;
2096                blk_offs += le32_to_cpu(dn->size);
2097                if (blk_offs > fscki->size) {
2098                        ubifs_err(c, "data node at LEB %d:%d is not within inode size %lld",
2099                                  zbr->lnum, zbr->offs, fscki->size);
2100                        err = -EINVAL;
2101                        goto out_dump;
2102                }
2103        } else {
2104                int nlen;
2105                struct ubifs_dent_node *dent = node;
2106                struct fsck_inode *fscki1;
2107
2108                ubifs_assert(zbr->len >= UBIFS_DENT_NODE_SZ);
2109
2110                err = ubifs_validate_entry(c, dent);
2111                if (err)
2112                        goto out_dump;
2113
2114                /*
2115                 * Search the inode node this entry refers to and the parent
2116                 * inode node and insert them to the RB-tree of inodes.
2117                 */
2118                inum = le64_to_cpu(dent->inum);
2119                fscki = read_add_inode(c, priv, inum);
2120                if (IS_ERR(fscki)) {
2121                        err = PTR_ERR(fscki);
2122                        ubifs_err(c, "error %d while processing entry node and trying to find inode node %lu",
2123                                  err, (unsigned long)inum);
2124                        goto out_dump;
2125                }
2126
2127                /* Count how many direntries or xentries refers this inode */
2128                fscki->references += 1;
2129
2130                inum = key_inum_flash(c, &dent->key);
2131                fscki1 = read_add_inode(c, priv, inum);
2132                if (IS_ERR(fscki1)) {
2133                        err = PTR_ERR(fscki1);
2134                        ubifs_err(c, "error %d while processing entry node and trying to find parent inode node %lu",
2135                                  err, (unsigned long)inum);
2136                        goto out_dump;
2137                }
2138
2139                nlen = le16_to_cpu(dent->nlen);
2140                if (type == UBIFS_XENT_KEY) {
2141                        fscki1->calc_xcnt += 1;
2142                        fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
2143                        fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
2144                        fscki1->calc_xnms += nlen;
2145                } else {
2146                        fscki1->calc_sz += CALC_DENT_SIZE(nlen);
2147                        if (dent->type == UBIFS_ITYPE_DIR)
2148                                fscki1->calc_cnt += 1;
2149                }
2150        }
2151
2152out:
2153        kfree(node);
2154        return 0;
2155
2156out_dump:
2157        ubifs_msg(c, "dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
2158        ubifs_dump_node(c, node);
2159out_free:
2160        kfree(node);
2161        return err;
2162}
2163
2164/**
2165 * free_inodes - free RB-tree of inodes.
2166 * @fsckd: FS checking information
2167 */
2168static void free_inodes(struct fsck_data *fsckd)
2169{
2170        struct fsck_inode *fscki, *n;
2171
2172        rbtree_postorder_for_each_entry_safe(fscki, n, &fsckd->inodes, rb)
2173                kfree(fscki);
2174}
2175
2176/**
2177 * check_inodes - checks all inodes.
2178 * @c: UBIFS file-system description object
2179 * @fsckd: FS checking information
2180 *
2181 * This is a helper function for 'dbg_check_filesystem()' which walks the
2182 * RB-tree of inodes after the index scan has been finished, and checks that
2183 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
2184 * %-EINVAL if not, and a negative error code in case of failure.
2185 */
2186static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
2187{
2188        int n, err;
2189        union ubifs_key key;
2190        struct ubifs_znode *znode;
2191        struct ubifs_zbranch *zbr;
2192        struct ubifs_ino_node *ino;
2193        struct fsck_inode *fscki;
2194        struct rb_node *this = rb_first(&fsckd->inodes);
2195
2196        while (this) {
2197                fscki = rb_entry(this, struct fsck_inode, rb);
2198                this = rb_next(this);
2199
2200                if (S_ISDIR(fscki->mode)) {
2201                        /*
2202                         * Directories have to have exactly one reference (they
2203                         * cannot have hardlinks), although root inode is an
2204                         * exception.
2205                         */
2206                        if (fscki->inum != UBIFS_ROOT_INO &&
2207                            fscki->references != 1) {
2208                                ubifs_err(c, "directory inode %lu has %d direntries which refer it, but should be 1",
2209                                          (unsigned long)fscki->inum,
2210                                          fscki->references);
2211                                goto out_dump;
2212                        }
2213                        if (fscki->inum == UBIFS_ROOT_INO &&
2214                            fscki->references != 0) {
2215                                ubifs_err(c, "root inode %lu has non-zero (%d) direntries which refer it",
2216                                          (unsigned long)fscki->inum,
2217                                          fscki->references);
2218                                goto out_dump;
2219                        }
2220                        if (fscki->calc_sz != fscki->size) {
2221                                ubifs_err(c, "directory inode %lu size is %lld, but calculated size is %lld",
2222                                          (unsigned long)fscki->inum,
2223                                          fscki->size, fscki->calc_sz);
2224                                goto out_dump;
2225                        }
2226                        if (fscki->calc_cnt != fscki->nlink) {
2227                                ubifs_err(c, "directory inode %lu nlink is %d, but calculated nlink is %d",
2228                                          (unsigned long)fscki->inum,
2229                                          fscki->nlink, fscki->calc_cnt);
2230                                goto out_dump;
2231                        }
2232                } else {
2233                        if (fscki->references != fscki->nlink) {
2234                                ubifs_err(c, "inode %lu nlink is %d, but calculated nlink is %d",
2235                                          (unsigned long)fscki->inum,
2236                                          fscki->nlink, fscki->references);
2237                                goto out_dump;
2238                        }
2239                }
2240                if (fscki->xattr_sz != fscki->calc_xsz) {
2241                        ubifs_err(c, "inode %lu has xattr size %u, but calculated size is %lld",
2242                                  (unsigned long)fscki->inum, fscki->xattr_sz,
2243                                  fscki->calc_xsz);
2244                        goto out_dump;
2245                }
2246                if (fscki->xattr_cnt != fscki->calc_xcnt) {
2247                        ubifs_err(c, "inode %lu has %u xattrs, but calculated count is %lld",
2248                                  (unsigned long)fscki->inum,
2249                                  fscki->xattr_cnt, fscki->calc_xcnt);
2250                        goto out_dump;
2251                }
2252                if (fscki->xattr_nms != fscki->calc_xnms) {
2253                        ubifs_err(c, "inode %lu has xattr names' size %u, but calculated names' size is %lld",
2254                                  (unsigned long)fscki->inum, fscki->xattr_nms,
2255                                  fscki->calc_xnms);
2256                        goto out_dump;
2257                }
2258        }
2259
2260        return 0;
2261
2262out_dump:
2263        /* Read the bad inode and dump it */
2264        ino_key_init(c, &key, fscki->inum);
2265        err = ubifs_lookup_level0(c, &key, &znode, &n);
2266        if (!err) {
2267                ubifs_err(c, "inode %lu not found in index",
2268                          (unsigned long)fscki->inum);
2269                return -ENOENT;
2270        } else if (err < 0) {
2271                ubifs_err(c, "error %d while looking up inode %lu",
2272                          err, (unsigned long)fscki->inum);
2273                return err;
2274        }
2275
2276        zbr = &znode->zbranch[n];
2277        ino = kmalloc(zbr->len, GFP_NOFS);
2278        if (!ino)
2279                return -ENOMEM;
2280
2281        err = ubifs_tnc_read_node(c, zbr, ino);
2282        if (err) {
2283                ubifs_err(c, "cannot read inode node at LEB %d:%d, error %d",
2284                          zbr->lnum, zbr->offs, err);
2285                kfree(ino);
2286                return err;
2287        }
2288
2289        ubifs_msg(c, "dump of the inode %lu sitting in LEB %d:%d",
2290                  (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
2291        ubifs_dump_node(c, ino);
2292        kfree(ino);
2293        return -EINVAL;
2294}
2295
2296/**
2297 * dbg_check_filesystem - check the file-system.
2298 * @c: UBIFS file-system description object
2299 *
2300 * This function checks the file system, namely:
2301 * o makes sure that all leaf nodes exist and their CRCs are correct;
2302 * o makes sure inode nlink, size, xattr size/count are correct (for all
2303 *   inodes).
2304 *
2305 * The function reads whole indexing tree and all nodes, so it is pretty
2306 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2307 * not, and a negative error code in case of failure.
2308 */
2309int dbg_check_filesystem(struct ubifs_info *c)
2310{
2311        int err;
2312        struct fsck_data fsckd;
2313
2314        if (!dbg_is_chk_fs(c))
2315                return 0;
2316
2317        fsckd.inodes = RB_ROOT;
2318        err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
2319        if (err)
2320                goto out_free;
2321
2322        err = check_inodes(c, &fsckd);
2323        if (err)
2324                goto out_free;
2325
2326        free_inodes(&fsckd);
2327        return 0;
2328
2329out_free:
2330        ubifs_err(c, "file-system check failed with error %d", err);
2331        dump_stack();
2332        free_inodes(&fsckd);
2333        return err;
2334}
2335
2336/**
2337 * dbg_check_data_nodes_order - check that list of data nodes is sorted.
2338 * @c: UBIFS file-system description object
2339 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2340 *
2341 * This function returns zero if the list of data nodes is sorted correctly,
2342 * and %-EINVAL if not.
2343 */
2344int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head)
2345{
2346        struct list_head *cur;
2347        struct ubifs_scan_node *sa, *sb;
2348
2349        if (!dbg_is_chk_gen(c))
2350                return 0;
2351
2352        for (cur = head->next; cur->next != head; cur = cur->next) {
2353                ino_t inuma, inumb;
2354                uint32_t blka, blkb;
2355
2356                cond_resched();
2357                sa = container_of(cur, struct ubifs_scan_node, list);
2358                sb = container_of(cur->next, struct ubifs_scan_node, list);
2359
2360                if (sa->type != UBIFS_DATA_NODE) {
2361                        ubifs_err(c, "bad node type %d", sa->type);
2362                        ubifs_dump_node(c, sa->node);
2363                        return -EINVAL;
2364                }
2365                if (sb->type != UBIFS_DATA_NODE) {
2366                        ubifs_err(c, "bad node type %d", sb->type);
2367                        ubifs_dump_node(c, sb->node);
2368                        return -EINVAL;
2369                }
2370
2371                inuma = key_inum(c, &sa->key);
2372                inumb = key_inum(c, &sb->key);
2373
2374                if (inuma < inumb)
2375                        continue;
2376                if (inuma > inumb) {
2377                        ubifs_err(c, "larger inum %lu goes before inum %lu",
2378                                  (unsigned long)inuma, (unsigned long)inumb);
2379                        goto error_dump;
2380                }
2381
2382                blka = key_block(c, &sa->key);
2383                blkb = key_block(c, &sb->key);
2384
2385                if (blka > blkb) {
2386                        ubifs_err(c, "larger block %u goes before %u", blka, blkb);
2387                        goto error_dump;
2388                }
2389                if (blka == blkb) {
2390                        ubifs_err(c, "two data nodes for the same block");
2391                        goto error_dump;
2392                }
2393        }
2394
2395        return 0;
2396
2397error_dump:
2398        ubifs_dump_node(c, sa->node);
2399        ubifs_dump_node(c, sb->node);
2400        return -EINVAL;
2401}
2402
2403/**
2404 * dbg_check_nondata_nodes_order - check that list of data nodes is sorted.
2405 * @c: UBIFS file-system description object
2406 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2407 *
2408 * This function returns zero if the list of non-data nodes is sorted correctly,
2409 * and %-EINVAL if not.
2410 */
2411int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head)
2412{
2413        struct list_head *cur;
2414        struct ubifs_scan_node *sa, *sb;
2415
2416        if (!dbg_is_chk_gen(c))
2417                return 0;
2418
2419        for (cur = head->next; cur->next != head; cur = cur->next) {
2420                ino_t inuma, inumb;
2421                uint32_t hasha, hashb;
2422
2423                cond_resched();
2424                sa = container_of(cur, struct ubifs_scan_node, list);
2425                sb = container_of(cur->next, struct ubifs_scan_node, list);
2426
2427                if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2428                    sa->type != UBIFS_XENT_NODE) {
2429                        ubifs_err(c, "bad node type %d", sa->type);
2430                        ubifs_dump_node(c, sa->node);
2431                        return -EINVAL;
2432                }
2433                if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2434                    sa->type != UBIFS_XENT_NODE) {
2435                        ubifs_err(c, "bad node type %d", sb->type);
2436                        ubifs_dump_node(c, sb->node);
2437                        return -EINVAL;
2438                }
2439
2440                if (sa->type != UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2441                        ubifs_err(c, "non-inode node goes before inode node");
2442                        goto error_dump;
2443                }
2444
2445                if (sa->type == UBIFS_INO_NODE && sb->type != UBIFS_INO_NODE)
2446                        continue;
2447
2448                if (sa->type == UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2449                        /* Inode nodes are sorted in descending size order */
2450                        if (sa->len < sb->len) {
2451                                ubifs_err(c, "smaller inode node goes first");
2452                                goto error_dump;
2453                        }
2454                        continue;
2455                }
2456
2457                /*
2458                 * This is either a dentry or xentry, which should be sorted in
2459                 * ascending (parent ino, hash) order.
2460                 */
2461                inuma = key_inum(c, &sa->key);
2462                inumb = key_inum(c, &sb->key);
2463
2464                if (inuma < inumb)
2465                        continue;
2466                if (inuma > inumb) {
2467                        ubifs_err(c, "larger inum %lu goes before inum %lu",
2468                                  (unsigned long)inuma, (unsigned long)inumb);
2469                        goto error_dump;
2470                }
2471
2472                hasha = key_block(c, &sa->key);
2473                hashb = key_block(c, &sb->key);
2474
2475                if (hasha > hashb) {
2476                        ubifs_err(c, "larger hash %u goes before %u",
2477                                  hasha, hashb);
2478                        goto error_dump;
2479                }
2480        }
2481
2482        return 0;
2483
2484error_dump:
2485        ubifs_msg(c, "dumping first node");
2486        ubifs_dump_node(c, sa->node);
2487        ubifs_msg(c, "dumping second node");
2488        ubifs_dump_node(c, sb->node);
2489        return -EINVAL;
2490        return 0;
2491}
2492
2493static inline int chance(unsigned int n, unsigned int out_of)
2494{
2495        return !!((prandom_u32() % out_of) + 1 <= n);
2496
2497}
2498
2499static int power_cut_emulated(struct ubifs_info *c, int lnum, int write)
2500{
2501        struct ubifs_debug_info *d = c->dbg;
2502
2503        ubifs_assert(dbg_is_tst_rcvry(c));
2504
2505        if (!d->pc_cnt) {
2506                /* First call - decide delay to the power cut */
2507                if (chance(1, 2)) {
2508                        unsigned long delay;
2509
2510                        if (chance(1, 2)) {
2511                                d->pc_delay = 1;
2512                                /* Fail within 1 minute */
2513                                delay = prandom_u32() % 60000;
2514                                d->pc_timeout = jiffies;
2515                                d->pc_timeout += msecs_to_jiffies(delay);
2516                                ubifs_warn(c, "failing after %lums", delay);
2517                        } else {
2518                                d->pc_delay = 2;
2519                                delay = prandom_u32() % 10000;
2520                                /* Fail within 10000 operations */
2521                                d->pc_cnt_max = delay;
2522                                ubifs_warn(c, "failing after %lu calls", delay);
2523                        }
2524                }
2525
2526                d->pc_cnt += 1;
2527        }
2528
2529        /* Determine if failure delay has expired */
2530        if (d->pc_delay == 1 && time_before(jiffies, d->pc_timeout))
2531                        return 0;
2532        if (d->pc_delay == 2 && d->pc_cnt++ < d->pc_cnt_max)
2533                        return 0;
2534
2535        if (lnum == UBIFS_SB_LNUM) {
2536                if (write && chance(1, 2))
2537                        return 0;
2538                if (chance(19, 20))
2539                        return 0;
2540                ubifs_warn(c, "failing in super block LEB %d", lnum);
2541        } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2542                if (chance(19, 20))
2543                        return 0;
2544                ubifs_warn(c, "failing in master LEB %d", lnum);
2545        } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2546                if (write && chance(99, 100))
2547                        return 0;
2548                if (chance(399, 400))
2549                        return 0;
2550                ubifs_warn(c, "failing in log LEB %d", lnum);
2551        } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2552                if (write && chance(7, 8))
2553                        return 0;
2554                if (chance(19, 20))
2555                        return 0;
2556                ubifs_warn(c, "failing in LPT LEB %d", lnum);
2557        } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2558                if (write && chance(1, 2))
2559                        return 0;
2560                if (chance(9, 10))
2561                        return 0;
2562                ubifs_warn(c, "failing in orphan LEB %d", lnum);
2563        } else if (lnum == c->ihead_lnum) {
2564                if (chance(99, 100))
2565                        return 0;
2566                ubifs_warn(c, "failing in index head LEB %d", lnum);
2567        } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2568                if (chance(9, 10))
2569                        return 0;
2570                ubifs_warn(c, "failing in GC head LEB %d", lnum);
2571        } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
2572                   !ubifs_search_bud(c, lnum)) {
2573                if (chance(19, 20))
2574                        return 0;
2575                ubifs_warn(c, "failing in non-bud LEB %d", lnum);
2576        } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
2577                   c->cmt_state == COMMIT_RUNNING_REQUIRED) {
2578                if (chance(999, 1000))
2579                        return 0;
2580                ubifs_warn(c, "failing in bud LEB %d commit running", lnum);
2581        } else {
2582                if (chance(9999, 10000))
2583                        return 0;
2584                ubifs_warn(c, "failing in bud LEB %d commit not running", lnum);
2585        }
2586
2587        d->pc_happened = 1;
2588        ubifs_warn(c, "========== Power cut emulated ==========");
2589        dump_stack();
2590        return 1;
2591}
2592
2593static int corrupt_data(const struct ubifs_info *c, const void *buf,
2594                        unsigned int len)
2595{
2596        unsigned int from, to, ffs = chance(1, 2);
2597        unsigned char *p = (void *)buf;
2598
2599        from = prandom_u32() % len;
2600        /* Corruption span max to end of write unit */
2601        to = min(len, ALIGN(from + 1, c->max_write_size));
2602
2603        ubifs_warn(c, "filled bytes %u-%u with %s", from, to - 1,
2604                   ffs ? "0xFFs" : "random data");
2605
2606        if (ffs)
2607                memset(p + from, 0xFF, to - from);
2608        else
2609                prandom_bytes(p + from, to - from);
2610
2611        return to;
2612}
2613
2614int dbg_leb_write(struct ubifs_info *c, int lnum, const void *buf,
2615                  int offs, int len)
2616{
2617        int err, failing;
2618
2619        if (c->dbg->pc_happened)
2620                return -EROFS;
2621
2622        failing = power_cut_emulated(c, lnum, 1);
2623        if (failing) {
2624                len = corrupt_data(c, buf, len);
2625                ubifs_warn(c, "actually write %d bytes to LEB %d:%d (the buffer was corrupted)",
2626                           len, lnum, offs);
2627        }
2628        err = ubi_leb_write(c->ubi, lnum, buf, offs, len);
2629        if (err)
2630                return err;
2631        if (failing)
2632                return -EROFS;
2633        return 0;
2634}
2635
2636int dbg_leb_change(struct ubifs_info *c, int lnum, const void *buf,
2637                   int len)
2638{
2639        int err;
2640
2641        if (c->dbg->pc_happened)
2642                return -EROFS;
2643        if (power_cut_emulated(c, lnum, 1))
2644                return -EROFS;
2645        err = ubi_leb_change(c->ubi, lnum, buf, len);
2646        if (err)
2647                return err;
2648        if (power_cut_emulated(c, lnum, 1))
2649                return -EROFS;
2650        return 0;
2651}
2652
2653int dbg_leb_unmap(struct ubifs_info *c, int lnum)
2654{
2655        int err;
2656
2657        if (c->dbg->pc_happened)
2658                return -EROFS;
2659        if (power_cut_emulated(c, lnum, 0))
2660                return -EROFS;
2661        err = ubi_leb_unmap(c->ubi, lnum);
2662        if (err)
2663                return err;
2664        if (power_cut_emulated(c, lnum, 0))
2665                return -EROFS;
2666        return 0;
2667}
2668
2669int dbg_leb_map(struct ubifs_info *c, int lnum)
2670{
2671        int err;
2672
2673        if (c->dbg->pc_happened)
2674                return -EROFS;
2675        if (power_cut_emulated(c, lnum, 0))
2676                return -EROFS;
2677        err = ubi_leb_map(c->ubi, lnum);
2678        if (err)
2679                return err;
2680        if (power_cut_emulated(c, lnum, 0))
2681                return -EROFS;
2682        return 0;
2683}
2684
2685/*
2686 * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
2687 * contain the stuff specific to particular file-system mounts.
2688 */
2689static struct dentry *dfs_rootdir;
2690
2691static int dfs_file_open(struct inode *inode, struct file *file)
2692{
2693        file->private_data = inode->i_private;
2694        return nonseekable_open(inode, file);
2695}
2696
2697/**
2698 * provide_user_output - provide output to the user reading a debugfs file.
2699 * @val: boolean value for the answer
2700 * @u: the buffer to store the answer at
2701 * @count: size of the buffer
2702 * @ppos: position in the @u output buffer
2703 *
2704 * This is a simple helper function which stores @val boolean value in the user
2705 * buffer when the user reads one of UBIFS debugfs files. Returns amount of
2706 * bytes written to @u in case of success and a negative error code in case of
2707 * failure.
2708 */
2709static int provide_user_output(int val, char __user *u, size_t count,
2710                               loff_t *ppos)
2711{
2712        char buf[3];
2713
2714        if (val)
2715                buf[0] = '1';
2716        else
2717                buf[0] = '0';
2718        buf[1] = '\n';
2719        buf[2] = 0x00;
2720
2721        return simple_read_from_buffer(u, count, ppos, buf, 2);
2722}
2723
2724static ssize_t dfs_file_read(struct file *file, char __user *u, size_t count,
2725                             loff_t *ppos)
2726{
2727        struct dentry *dent = file->f_path.dentry;
2728        struct ubifs_info *c = file->private_data;
2729        struct ubifs_debug_info *d = c->dbg;
2730        int val;
2731
2732        if (dent == d->dfs_chk_gen)
2733                val = d->chk_gen;
2734        else if (dent == d->dfs_chk_index)
2735                val = d->chk_index;
2736        else if (dent == d->dfs_chk_orph)
2737                val = d->chk_orph;
2738        else if (dent == d->dfs_chk_lprops)
2739                val = d->chk_lprops;
2740        else if (dent == d->dfs_chk_fs)
2741                val = d->chk_fs;
2742        else if (dent == d->dfs_tst_rcvry)
2743                val = d->tst_rcvry;
2744        else if (dent == d->dfs_ro_error)
2745                val = c->ro_error;
2746        else
2747                return -EINVAL;
2748
2749        return provide_user_output(val, u, count, ppos);
2750}
2751
2752/**
2753 * interpret_user_input - interpret user debugfs file input.
2754 * @u: user-provided buffer with the input
2755 * @count: buffer size
2756 *
2757 * This is a helper function which interpret user input to a boolean UBIFS
2758 * debugfs file. Returns %0 or %1 in case of success and a negative error code
2759 * in case of failure.
2760 */
2761static int interpret_user_input(const char __user *u, size_t count)
2762{
2763        size_t buf_size;
2764        char buf[8];
2765
2766        buf_size = min_t(size_t, count, (sizeof(buf) - 1));
2767        if (copy_from_user(buf, u, buf_size))
2768                return -EFAULT;
2769
2770        if (buf[0] == '1')
2771                return 1;
2772        else if (buf[0] == '0')
2773                return 0;
2774
2775        return -EINVAL;
2776}
2777
2778static ssize_t dfs_file_write(struct file *file, const char __user *u,
2779                              size_t count, loff_t *ppos)
2780{
2781        struct ubifs_info *c = file->private_data;
2782        struct ubifs_debug_info *d = c->dbg;
2783        struct dentry *dent = file->f_path.dentry;
2784        int val;
2785
2786        /*
2787         * TODO: this is racy - the file-system might have already been
2788         * unmounted and we'd oops in this case. The plan is to fix it with
2789         * help of 'iterate_supers_type()' which we should have in v3.0: when
2790         * a debugfs opened, we rember FS's UUID in file->private_data. Then
2791         * whenever we access the FS via a debugfs file, we iterate all UBIFS
2792         * superblocks and fine the one with the same UUID, and take the
2793         * locking right.
2794         *
2795         * The other way to go suggested by Al Viro is to create a separate
2796         * 'ubifs-debug' file-system instead.
2797         */
2798        if (file->f_path.dentry == d->dfs_dump_lprops) {
2799                ubifs_dump_lprops(c);
2800                return count;
2801        }
2802        if (file->f_path.dentry == d->dfs_dump_budg) {
2803                ubifs_dump_budg(c, &c->bi);
2804                return count;
2805        }
2806        if (file->f_path.dentry == d->dfs_dump_tnc) {
2807                mutex_lock(&c->tnc_mutex);
2808                ubifs_dump_tnc(c);
2809                mutex_unlock(&c->tnc_mutex);
2810                return count;
2811        }
2812
2813        val = interpret_user_input(u, count);
2814        if (val < 0)
2815                return val;
2816
2817        if (dent == d->dfs_chk_gen)
2818                d->chk_gen = val;
2819        else if (dent == d->dfs_chk_index)
2820                d->chk_index = val;
2821        else if (dent == d->dfs_chk_orph)
2822                d->chk_orph = val;
2823        else if (dent == d->dfs_chk_lprops)
2824                d->chk_lprops = val;
2825        else if (dent == d->dfs_chk_fs)
2826                d->chk_fs = val;
2827        else if (dent == d->dfs_tst_rcvry)
2828                d->tst_rcvry = val;
2829        else if (dent == d->dfs_ro_error)
2830                c->ro_error = !!val;
2831        else
2832                return -EINVAL;
2833
2834        return count;
2835}
2836
2837static const struct file_operations dfs_fops = {
2838        .open = dfs_file_open,
2839        .read = dfs_file_read,
2840        .write = dfs_file_write,
2841        .owner = THIS_MODULE,
2842        .llseek = no_llseek,
2843};
2844
2845/**
2846 * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
2847 * @c: UBIFS file-system description object
2848 *
2849 * This function creates all debugfs files for this instance of UBIFS. Returns
2850 * zero in case of success and a negative error code in case of failure.
2851 *
2852 * Note, the only reason we have not merged this function with the
2853 * 'ubifs_debugging_init()' function is because it is better to initialize
2854 * debugfs interfaces at the very end of the mount process, and remove them at
2855 * the very beginning of the mount process.
2856 */
2857int dbg_debugfs_init_fs(struct ubifs_info *c)
2858{
2859        int err, n;
2860        const char *fname;
2861        struct dentry *dent;
2862        struct ubifs_debug_info *d = c->dbg;
2863
2864        if (!IS_ENABLED(CONFIG_DEBUG_FS))
2865                return 0;
2866
2867        n = snprintf(d->dfs_dir_name, UBIFS_DFS_DIR_LEN + 1, UBIFS_DFS_DIR_NAME,
2868                     c->vi.ubi_num, c->vi.vol_id);
2869        if (n == UBIFS_DFS_DIR_LEN) {
2870                /* The array size is too small */
2871                fname = UBIFS_DFS_DIR_NAME;
2872                dent = ERR_PTR(-EINVAL);
2873                goto out;
2874        }
2875
2876        fname = d->dfs_dir_name;
2877        dent = debugfs_create_dir(fname, dfs_rootdir);
2878        if (IS_ERR_OR_NULL(dent))
2879                goto out;
2880        d->dfs_dir = dent;
2881
2882        fname = "dump_lprops";
2883        dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2884        if (IS_ERR_OR_NULL(dent))
2885                goto out_remove;
2886        d->dfs_dump_lprops = dent;
2887
2888        fname = "dump_budg";
2889        dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2890        if (IS_ERR_OR_NULL(dent))
2891                goto out_remove;
2892        d->dfs_dump_budg = dent;
2893
2894        fname = "dump_tnc";
2895        dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2896        if (IS_ERR_OR_NULL(dent))
2897                goto out_remove;
2898        d->dfs_dump_tnc = dent;
2899
2900        fname = "chk_general";
2901        dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2902                                   &dfs_fops);
2903        if (IS_ERR_OR_NULL(dent))
2904                goto out_remove;
2905        d->dfs_chk_gen = dent;
2906
2907        fname = "chk_index";
2908        dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2909                                   &dfs_fops);
2910        if (IS_ERR_OR_NULL(dent))
2911                goto out_remove;
2912        d->dfs_chk_index = dent;
2913
2914        fname = "chk_orphans";
2915        dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2916                                   &dfs_fops);
2917        if (IS_ERR_OR_NULL(dent))
2918                goto out_remove;
2919        d->dfs_chk_orph = dent;
2920
2921        fname = "chk_lprops";
2922        dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2923                                   &dfs_fops);
2924        if (IS_ERR_OR_NULL(dent))
2925                goto out_remove;
2926        d->dfs_chk_lprops = dent;
2927
2928        fname = "chk_fs";
2929        dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2930                                   &dfs_fops);
2931        if (IS_ERR_OR_NULL(dent))
2932                goto out_remove;
2933        d->dfs_chk_fs = dent;
2934
2935        fname = "tst_recovery";
2936        dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2937                                   &dfs_fops);
2938        if (IS_ERR_OR_NULL(dent))
2939                goto out_remove;
2940        d->dfs_tst_rcvry = dent;
2941
2942        fname = "ro_error";
2943        dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2944                                   &dfs_fops);
2945        if (IS_ERR_OR_NULL(dent))
2946                goto out_remove;
2947        d->dfs_ro_error = dent;
2948
2949        return 0;
2950
2951out_remove:
2952        debugfs_remove_recursive(d->dfs_dir);
2953out:
2954        err = dent ? PTR_ERR(dent) : -ENODEV;
2955        ubifs_err(c, "cannot create \"%s\" debugfs file or directory, error %d\n",
2956                  fname, err);
2957        return err;
2958}
2959
2960/**
2961 * dbg_debugfs_exit_fs - remove all debugfs files.
2962 * @c: UBIFS file-system description object
2963 */
2964void dbg_debugfs_exit_fs(struct ubifs_info *c)
2965{
2966        if (IS_ENABLED(CONFIG_DEBUG_FS))
2967                debugfs_remove_recursive(c->dbg->dfs_dir);
2968}
2969
2970struct ubifs_global_debug_info ubifs_dbg;
2971
2972static struct dentry *dfs_chk_gen;
2973static struct dentry *dfs_chk_index;
2974static struct dentry *dfs_chk_orph;
2975static struct dentry *dfs_chk_lprops;
2976static struct dentry *dfs_chk_fs;
2977static struct dentry *dfs_tst_rcvry;
2978
2979static ssize_t dfs_global_file_read(struct file *file, char __user *u,
2980                                    size_t count, loff_t *ppos)
2981{
2982        struct dentry *dent = file->f_path.dentry;
2983        int val;
2984
2985        if (dent == dfs_chk_gen)
2986                val = ubifs_dbg.chk_gen;
2987        else if (dent == dfs_chk_index)
2988                val = ubifs_dbg.chk_index;
2989        else if (dent == dfs_chk_orph)
2990                val = ubifs_dbg.chk_orph;
2991        else if (dent == dfs_chk_lprops)
2992                val = ubifs_dbg.chk_lprops;
2993        else if (dent == dfs_chk_fs)
2994                val = ubifs_dbg.chk_fs;
2995        else if (dent == dfs_tst_rcvry)
2996                val = ubifs_dbg.tst_rcvry;
2997        else
2998                return -EINVAL;
2999
3000        return provide_user_output(val, u, count, ppos);
3001}
3002
3003static ssize_t dfs_global_file_write(struct file *file, const char __user *u,
3004                                     size_t count, loff_t *ppos)
3005{
3006        struct dentry *dent = file->f_path.dentry;
3007        int val;
3008
3009        val = interpret_user_input(u, count);
3010        if (val < 0)
3011                return val;
3012
3013        if (dent == dfs_chk_gen)
3014                ubifs_dbg.chk_gen = val;
3015        else if (dent == dfs_chk_index)
3016                ubifs_dbg.chk_index = val;
3017        else if (dent == dfs_chk_orph)
3018                ubifs_dbg.chk_orph = val;
3019        else if (dent == dfs_chk_lprops)
3020                ubifs_dbg.chk_lprops = val;
3021        else if (dent == dfs_chk_fs)
3022                ubifs_dbg.chk_fs = val;
3023        else if (dent == dfs_tst_rcvry)
3024                ubifs_dbg.tst_rcvry = val;
3025        else
3026                return -EINVAL;
3027
3028        return count;
3029}
3030
3031static const struct file_operations dfs_global_fops = {
3032        .read = dfs_global_file_read,
3033        .write = dfs_global_file_write,
3034        .owner = THIS_MODULE,
3035        .llseek = no_llseek,
3036};
3037
3038/**
3039 * dbg_debugfs_init - initialize debugfs file-system.
3040 *
3041 * UBIFS uses debugfs file-system to expose various debugging knobs to
3042 * user-space. This function creates "ubifs" directory in the debugfs
3043 * file-system. Returns zero in case of success and a negative error code in
3044 * case of failure.
3045 */
3046int dbg_debugfs_init(void)
3047{
3048        int err;
3049        const char *fname;
3050        struct dentry *dent;
3051
3052        if (!IS_ENABLED(CONFIG_DEBUG_FS))
3053                return 0;
3054
3055        fname = "ubifs";
3056        dent = debugfs_create_dir(fname, NULL);
3057        if (IS_ERR_OR_NULL(dent))
3058                goto out;
3059        dfs_rootdir = dent;
3060
3061        fname = "chk_general";
3062        dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3063                                   &dfs_global_fops);
3064        if (IS_ERR_OR_NULL(dent))
3065                goto out_remove;
3066        dfs_chk_gen = dent;
3067
3068        fname = "chk_index";
3069        dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3070                                   &dfs_global_fops);
3071        if (IS_ERR_OR_NULL(dent))
3072                goto out_remove;
3073        dfs_chk_index = dent;
3074
3075        fname = "chk_orphans";
3076        dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3077                                   &dfs_global_fops);
3078        if (IS_ERR_OR_NULL(dent))
3079                goto out_remove;
3080        dfs_chk_orph = dent;
3081
3082        fname = "chk_lprops";
3083        dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3084                                   &dfs_global_fops);
3085        if (IS_ERR_OR_NULL(dent))
3086                goto out_remove;
3087        dfs_chk_lprops = dent;
3088
3089        fname = "chk_fs";
3090        dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3091                                   &dfs_global_fops);
3092        if (IS_ERR_OR_NULL(dent))
3093                goto out_remove;
3094        dfs_chk_fs = dent;
3095
3096        fname = "tst_recovery";
3097        dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3098                                   &dfs_global_fops);
3099        if (IS_ERR_OR_NULL(dent))
3100                goto out_remove;
3101        dfs_tst_rcvry = dent;
3102
3103        return 0;
3104
3105out_remove:
3106        debugfs_remove_recursive(dfs_rootdir);
3107out:
3108        err = dent ? PTR_ERR(dent) : -ENODEV;
3109        pr_err("UBIFS error (pid %d): cannot create \"%s\" debugfs file or directory, error %d\n",
3110               current->pid, fname, err);
3111        return err;
3112}
3113
3114/**
3115 * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
3116 */
3117void dbg_debugfs_exit(void)
3118{
3119        if (IS_ENABLED(CONFIG_DEBUG_FS))
3120                debugfs_remove_recursive(dfs_rootdir);
3121}
3122
3123/**
3124 * ubifs_debugging_init - initialize UBIFS debugging.
3125 * @c: UBIFS file-system description object
3126 *
3127 * This function initializes debugging-related data for the file system.
3128 * Returns zero in case of success and a negative error code in case of
3129 * failure.
3130 */
3131int ubifs_debugging_init(struct ubifs_info *c)
3132{
3133        c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
3134        if (!c->dbg)
3135                return -ENOMEM;
3136
3137        return 0;
3138}
3139
3140/**
3141 * ubifs_debugging_exit - free debugging data.
3142 * @c: UBIFS file-system description object
3143 */
3144void ubifs_debugging_exit(struct ubifs_info *c)
3145{
3146        kfree(c->dbg);
3147}
3148#endif
3149