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