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