2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation
6 * SPDX-License-Identifier: GPL-2.0+
8 * Authors: Artem Bityutskiy (Битюцкий Артём)
13 * This file implements most of the debugging stuff which is compiled in only
14 * when it is enabled. But some debugging check functions are implemented in
15 * corresponding subsystem, just because they are closely related and utilize
16 * various local functions of those subsystems.
20 #include <linux/module.h>
21 #include <linux/debugfs.h>
22 #include <linux/math64.h>
23 #include <linux/uaccess.h>
24 #include <linux/random.h>
26 #include <linux/compat.h>
27 #include <linux/err.h>
32 static DEFINE_SPINLOCK(dbg_lock);
35 static const char *get_key_fmt(int fmt)
38 case UBIFS_SIMPLE_KEY_FMT:
41 return "unknown/invalid format";
45 static const char *get_key_hash(int hash)
48 case UBIFS_KEY_HASH_R5:
50 case UBIFS_KEY_HASH_TEST:
53 return "unknown/invalid name hash";
57 static const char *get_key_type(int type)
71 return "unknown/invalid key";
76 static const char *get_dent_type(int type)
89 case UBIFS_ITYPE_FIFO:
91 case UBIFS_ITYPE_SOCK:
94 return "unknown/invalid type";
99 const char *dbg_snprintf_key(const struct ubifs_info *c,
100 const union ubifs_key *key, char *buffer, int len)
103 int type = key_type(c, key);
105 if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
108 len -= snprintf(p, len, "(%lu, %s)",
109 (unsigned long)key_inum(c, key),
114 len -= snprintf(p, len, "(%lu, %s, %#08x)",
115 (unsigned long)key_inum(c, key),
116 get_key_type(type), key_hash(c, key));
119 len -= snprintf(p, len, "(%lu, %s, %u)",
120 (unsigned long)key_inum(c, key),
121 get_key_type(type), key_block(c, key));
124 len -= snprintf(p, len, "(%lu, %s)",
125 (unsigned long)key_inum(c, key),
129 len -= snprintf(p, len, "(bad key type: %#08x, %#08x)",
130 key->u32[0], key->u32[1]);
133 len -= snprintf(p, len, "bad key format %d", c->key_fmt);
134 ubifs_assert(len > 0);
138 const char *dbg_ntype(int type)
142 return "padding node";
144 return "superblock node";
146 return "master node";
148 return "reference node";
151 case UBIFS_DENT_NODE:
152 return "direntry node";
153 case UBIFS_XENT_NODE:
154 return "xentry node";
155 case UBIFS_DATA_NODE:
157 case UBIFS_TRUN_NODE:
158 return "truncate node";
160 return "indexing node";
162 return "commit start node";
163 case UBIFS_ORPH_NODE:
164 return "orphan node";
166 return "unknown node";
170 static const char *dbg_gtype(int type)
173 case UBIFS_NO_NODE_GROUP:
174 return "no node group";
175 case UBIFS_IN_NODE_GROUP:
176 return "in node group";
177 case UBIFS_LAST_OF_NODE_GROUP:
178 return "last of node group";
184 const char *dbg_cstate(int cmt_state)
188 return "commit resting";
189 case COMMIT_BACKGROUND:
190 return "background commit requested";
191 case COMMIT_REQUIRED:
192 return "commit required";
193 case COMMIT_RUNNING_BACKGROUND:
194 return "BACKGROUND commit running";
195 case COMMIT_RUNNING_REQUIRED:
196 return "commit running and required";
198 return "broken commit";
200 return "unknown commit state";
204 const char *dbg_jhead(int jhead)
214 return "unknown journal head";
218 static void dump_ch(const struct ubifs_ch *ch)
220 pr_err("\tmagic %#x\n", le32_to_cpu(ch->magic));
221 pr_err("\tcrc %#x\n", le32_to_cpu(ch->crc));
222 pr_err("\tnode_type %d (%s)\n", ch->node_type,
223 dbg_ntype(ch->node_type));
224 pr_err("\tgroup_type %d (%s)\n", ch->group_type,
225 dbg_gtype(ch->group_type));
226 pr_err("\tsqnum %llu\n",
227 (unsigned long long)le64_to_cpu(ch->sqnum));
228 pr_err("\tlen %u\n", le32_to_cpu(ch->len));
231 void ubifs_dump_inode(struct ubifs_info *c, const struct inode *inode)
234 const struct ubifs_inode *ui = ubifs_inode(inode);
235 struct qstr nm = { .name = NULL };
237 struct ubifs_dent_node *dent, *pdent = NULL;
240 pr_err("Dump in-memory inode:");
241 pr_err("\tinode %lu\n", inode->i_ino);
242 pr_err("\tsize %llu\n",
243 (unsigned long long)i_size_read(inode));
244 pr_err("\tnlink %u\n", inode->i_nlink);
245 pr_err("\tuid %u\n", (unsigned int)i_uid_read(inode));
246 pr_err("\tgid %u\n", (unsigned int)i_gid_read(inode));
247 pr_err("\tatime %u.%u\n",
248 (unsigned int)inode->i_atime.tv_sec,
249 (unsigned int)inode->i_atime.tv_nsec);
250 pr_err("\tmtime %u.%u\n",
251 (unsigned int)inode->i_mtime.tv_sec,
252 (unsigned int)inode->i_mtime.tv_nsec);
253 pr_err("\tctime %u.%u\n",
254 (unsigned int)inode->i_ctime.tv_sec,
255 (unsigned int)inode->i_ctime.tv_nsec);
256 pr_err("\tcreat_sqnum %llu\n", ui->creat_sqnum);
257 pr_err("\txattr_size %u\n", ui->xattr_size);
258 pr_err("\txattr_cnt %u\n", ui->xattr_cnt);
259 pr_err("\txattr_names %u\n", ui->xattr_names);
260 pr_err("\tdirty %u\n", ui->dirty);
261 pr_err("\txattr %u\n", ui->xattr);
262 pr_err("\tbulk_read %u\n", ui->xattr);
263 pr_err("\tsynced_i_size %llu\n",
264 (unsigned long long)ui->synced_i_size);
265 pr_err("\tui_size %llu\n",
266 (unsigned long long)ui->ui_size);
267 pr_err("\tflags %d\n", ui->flags);
268 pr_err("\tcompr_type %d\n", ui->compr_type);
269 pr_err("\tlast_page_read %lu\n", ui->last_page_read);
270 pr_err("\tread_in_a_row %lu\n", ui->read_in_a_row);
271 pr_err("\tdata_len %d\n", ui->data_len);
273 if (!S_ISDIR(inode->i_mode))
276 pr_err("List of directory entries:\n");
277 ubifs_assert(!mutex_is_locked(&c->tnc_mutex));
279 lowest_dent_key(c, &key, inode->i_ino);
281 dent = ubifs_tnc_next_ent(c, &key, &nm);
283 if (PTR_ERR(dent) != -ENOENT)
284 pr_err("error %ld\n", PTR_ERR(dent));
288 pr_err("\t%d: %s (%s)\n",
289 count++, dent->name, get_dent_type(dent->type));
291 nm.name = dent->name;
292 nm.len = le16_to_cpu(dent->nlen);
295 key_read(c, &dent->key, &key);
301 void ubifs_dump_node(const struct ubifs_info *c, const void *node)
305 const struct ubifs_ch *ch = node;
306 char key_buf[DBG_KEY_BUF_LEN];
308 /* If the magic is incorrect, just hexdump the first bytes */
309 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
310 pr_err("Not a node, first %zu bytes:", UBIFS_CH_SZ);
311 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 32, 1,
312 (void *)node, UBIFS_CH_SZ, 1);
316 spin_lock(&dbg_lock);
319 switch (ch->node_type) {
322 const struct ubifs_pad_node *pad = node;
324 pr_err("\tpad_len %u\n", le32_to_cpu(pad->pad_len));
329 const struct ubifs_sb_node *sup = node;
330 unsigned int sup_flags = le32_to_cpu(sup->flags);
332 pr_err("\tkey_hash %d (%s)\n",
333 (int)sup->key_hash, get_key_hash(sup->key_hash));
334 pr_err("\tkey_fmt %d (%s)\n",
335 (int)sup->key_fmt, get_key_fmt(sup->key_fmt));
336 pr_err("\tflags %#x\n", sup_flags);
337 pr_err("\tbig_lpt %u\n",
338 !!(sup_flags & UBIFS_FLG_BIGLPT));
339 pr_err("\tspace_fixup %u\n",
340 !!(sup_flags & UBIFS_FLG_SPACE_FIXUP));
341 pr_err("\tmin_io_size %u\n", le32_to_cpu(sup->min_io_size));
342 pr_err("\tleb_size %u\n", le32_to_cpu(sup->leb_size));
343 pr_err("\tleb_cnt %u\n", le32_to_cpu(sup->leb_cnt));
344 pr_err("\tmax_leb_cnt %u\n", le32_to_cpu(sup->max_leb_cnt));
345 pr_err("\tmax_bud_bytes %llu\n",
346 (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
347 pr_err("\tlog_lebs %u\n", le32_to_cpu(sup->log_lebs));
348 pr_err("\tlpt_lebs %u\n", le32_to_cpu(sup->lpt_lebs));
349 pr_err("\torph_lebs %u\n", le32_to_cpu(sup->orph_lebs));
350 pr_err("\tjhead_cnt %u\n", le32_to_cpu(sup->jhead_cnt));
351 pr_err("\tfanout %u\n", le32_to_cpu(sup->fanout));
352 pr_err("\tlsave_cnt %u\n", le32_to_cpu(sup->lsave_cnt));
353 pr_err("\tdefault_compr %u\n",
354 (int)le16_to_cpu(sup->default_compr));
355 pr_err("\trp_size %llu\n",
356 (unsigned long long)le64_to_cpu(sup->rp_size));
357 pr_err("\trp_uid %u\n", le32_to_cpu(sup->rp_uid));
358 pr_err("\trp_gid %u\n", le32_to_cpu(sup->rp_gid));
359 pr_err("\tfmt_version %u\n", le32_to_cpu(sup->fmt_version));
360 pr_err("\ttime_gran %u\n", le32_to_cpu(sup->time_gran));
361 pr_err("\tUUID %pUB\n", sup->uuid);
366 const struct ubifs_mst_node *mst = node;
368 pr_err("\thighest_inum %llu\n",
369 (unsigned long long)le64_to_cpu(mst->highest_inum));
370 pr_err("\tcommit number %llu\n",
371 (unsigned long long)le64_to_cpu(mst->cmt_no));
372 pr_err("\tflags %#x\n", le32_to_cpu(mst->flags));
373 pr_err("\tlog_lnum %u\n", le32_to_cpu(mst->log_lnum));
374 pr_err("\troot_lnum %u\n", le32_to_cpu(mst->root_lnum));
375 pr_err("\troot_offs %u\n", le32_to_cpu(mst->root_offs));
376 pr_err("\troot_len %u\n", le32_to_cpu(mst->root_len));
377 pr_err("\tgc_lnum %u\n", le32_to_cpu(mst->gc_lnum));
378 pr_err("\tihead_lnum %u\n", le32_to_cpu(mst->ihead_lnum));
379 pr_err("\tihead_offs %u\n", le32_to_cpu(mst->ihead_offs));
380 pr_err("\tindex_size %llu\n",
381 (unsigned long long)le64_to_cpu(mst->index_size));
382 pr_err("\tlpt_lnum %u\n", le32_to_cpu(mst->lpt_lnum));
383 pr_err("\tlpt_offs %u\n", le32_to_cpu(mst->lpt_offs));
384 pr_err("\tnhead_lnum %u\n", le32_to_cpu(mst->nhead_lnum));
385 pr_err("\tnhead_offs %u\n", le32_to_cpu(mst->nhead_offs));
386 pr_err("\tltab_lnum %u\n", le32_to_cpu(mst->ltab_lnum));
387 pr_err("\tltab_offs %u\n", le32_to_cpu(mst->ltab_offs));
388 pr_err("\tlsave_lnum %u\n", le32_to_cpu(mst->lsave_lnum));
389 pr_err("\tlsave_offs %u\n", le32_to_cpu(mst->lsave_offs));
390 pr_err("\tlscan_lnum %u\n", le32_to_cpu(mst->lscan_lnum));
391 pr_err("\tleb_cnt %u\n", le32_to_cpu(mst->leb_cnt));
392 pr_err("\tempty_lebs %u\n", le32_to_cpu(mst->empty_lebs));
393 pr_err("\tidx_lebs %u\n", le32_to_cpu(mst->idx_lebs));
394 pr_err("\ttotal_free %llu\n",
395 (unsigned long long)le64_to_cpu(mst->total_free));
396 pr_err("\ttotal_dirty %llu\n",
397 (unsigned long long)le64_to_cpu(mst->total_dirty));
398 pr_err("\ttotal_used %llu\n",
399 (unsigned long long)le64_to_cpu(mst->total_used));
400 pr_err("\ttotal_dead %llu\n",
401 (unsigned long long)le64_to_cpu(mst->total_dead));
402 pr_err("\ttotal_dark %llu\n",
403 (unsigned long long)le64_to_cpu(mst->total_dark));
408 const struct ubifs_ref_node *ref = node;
410 pr_err("\tlnum %u\n", le32_to_cpu(ref->lnum));
411 pr_err("\toffs %u\n", le32_to_cpu(ref->offs));
412 pr_err("\tjhead %u\n", le32_to_cpu(ref->jhead));
417 const struct ubifs_ino_node *ino = node;
419 key_read(c, &ino->key, &key);
421 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
422 pr_err("\tcreat_sqnum %llu\n",
423 (unsigned long long)le64_to_cpu(ino->creat_sqnum));
424 pr_err("\tsize %llu\n",
425 (unsigned long long)le64_to_cpu(ino->size));
426 pr_err("\tnlink %u\n", le32_to_cpu(ino->nlink));
427 pr_err("\tatime %lld.%u\n",
428 (long long)le64_to_cpu(ino->atime_sec),
429 le32_to_cpu(ino->atime_nsec));
430 pr_err("\tmtime %lld.%u\n",
431 (long long)le64_to_cpu(ino->mtime_sec),
432 le32_to_cpu(ino->mtime_nsec));
433 pr_err("\tctime %lld.%u\n",
434 (long long)le64_to_cpu(ino->ctime_sec),
435 le32_to_cpu(ino->ctime_nsec));
436 pr_err("\tuid %u\n", le32_to_cpu(ino->uid));
437 pr_err("\tgid %u\n", le32_to_cpu(ino->gid));
438 pr_err("\tmode %u\n", le32_to_cpu(ino->mode));
439 pr_err("\tflags %#x\n", le32_to_cpu(ino->flags));
440 pr_err("\txattr_cnt %u\n", le32_to_cpu(ino->xattr_cnt));
441 pr_err("\txattr_size %u\n", le32_to_cpu(ino->xattr_size));
442 pr_err("\txattr_names %u\n", le32_to_cpu(ino->xattr_names));
443 pr_err("\tcompr_type %#x\n",
444 (int)le16_to_cpu(ino->compr_type));
445 pr_err("\tdata len %u\n", le32_to_cpu(ino->data_len));
448 case UBIFS_DENT_NODE:
449 case UBIFS_XENT_NODE:
451 const struct ubifs_dent_node *dent = node;
452 int nlen = le16_to_cpu(dent->nlen);
454 key_read(c, &dent->key, &key);
456 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
457 pr_err("\tinum %llu\n",
458 (unsigned long long)le64_to_cpu(dent->inum));
459 pr_err("\ttype %d\n", (int)dent->type);
460 pr_err("\tnlen %d\n", nlen);
463 if (nlen > UBIFS_MAX_NLEN)
464 pr_err("(bad name length, not printing, bad or corrupted node)");
466 for (i = 0; i < nlen && dent->name[i]; i++)
467 pr_cont("%c", dent->name[i]);
473 case UBIFS_DATA_NODE:
475 const struct ubifs_data_node *dn = node;
476 int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
478 key_read(c, &dn->key, &key);
480 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
481 pr_err("\tsize %u\n", le32_to_cpu(dn->size));
482 pr_err("\tcompr_typ %d\n",
483 (int)le16_to_cpu(dn->compr_type));
484 pr_err("\tdata size %d\n", dlen);
486 print_hex_dump(KERN_ERR, "\t", DUMP_PREFIX_OFFSET, 32, 1,
487 (void *)&dn->data, dlen, 0);
490 case UBIFS_TRUN_NODE:
492 const struct ubifs_trun_node *trun = node;
494 pr_err("\tinum %u\n", le32_to_cpu(trun->inum));
495 pr_err("\told_size %llu\n",
496 (unsigned long long)le64_to_cpu(trun->old_size));
497 pr_err("\tnew_size %llu\n",
498 (unsigned long long)le64_to_cpu(trun->new_size));
503 const struct ubifs_idx_node *idx = node;
505 n = le16_to_cpu(idx->child_cnt);
506 pr_err("\tchild_cnt %d\n", n);
507 pr_err("\tlevel %d\n", (int)le16_to_cpu(idx->level));
508 pr_err("\tBranches:\n");
510 for (i = 0; i < n && i < c->fanout - 1; i++) {
511 const struct ubifs_branch *br;
513 br = ubifs_idx_branch(c, idx, i);
514 key_read(c, &br->key, &key);
515 pr_err("\t%d: LEB %d:%d len %d key %s\n",
516 i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
517 le32_to_cpu(br->len),
518 dbg_snprintf_key(c, &key, key_buf,
525 case UBIFS_ORPH_NODE:
527 const struct ubifs_orph_node *orph = node;
529 pr_err("\tcommit number %llu\n",
531 le64_to_cpu(orph->cmt_no) & LLONG_MAX);
532 pr_err("\tlast node flag %llu\n",
533 (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
534 n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
535 pr_err("\t%d orphan inode numbers:\n", n);
536 for (i = 0; i < n; i++)
537 pr_err("\t ino %llu\n",
538 (unsigned long long)le64_to_cpu(orph->inos[i]));
542 pr_err("node type %d was not recognized\n",
545 spin_unlock(&dbg_lock);
548 void ubifs_dump_budget_req(const struct ubifs_budget_req *req)
550 spin_lock(&dbg_lock);
551 pr_err("Budgeting request: new_ino %d, dirtied_ino %d\n",
552 req->new_ino, req->dirtied_ino);
553 pr_err("\tnew_ino_d %d, dirtied_ino_d %d\n",
554 req->new_ino_d, req->dirtied_ino_d);
555 pr_err("\tnew_page %d, dirtied_page %d\n",
556 req->new_page, req->dirtied_page);
557 pr_err("\tnew_dent %d, mod_dent %d\n",
558 req->new_dent, req->mod_dent);
559 pr_err("\tidx_growth %d\n", req->idx_growth);
560 pr_err("\tdata_growth %d dd_growth %d\n",
561 req->data_growth, req->dd_growth);
562 spin_unlock(&dbg_lock);
565 void ubifs_dump_lstats(const struct ubifs_lp_stats *lst)
567 spin_lock(&dbg_lock);
568 pr_err("(pid %d) Lprops statistics: empty_lebs %d, idx_lebs %d\n",
569 current->pid, lst->empty_lebs, lst->idx_lebs);
570 pr_err("\ttaken_empty_lebs %d, total_free %lld, total_dirty %lld\n",
571 lst->taken_empty_lebs, lst->total_free, lst->total_dirty);
572 pr_err("\ttotal_used %lld, total_dark %lld, total_dead %lld\n",
573 lst->total_used, lst->total_dark, lst->total_dead);
574 spin_unlock(&dbg_lock);
578 void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)
582 struct ubifs_bud *bud;
583 struct ubifs_gced_idx_leb *idx_gc;
584 long long available, outstanding, free;
586 spin_lock(&c->space_lock);
587 spin_lock(&dbg_lock);
588 pr_err("(pid %d) Budgeting info: data budget sum %lld, total budget sum %lld\n",
589 current->pid, bi->data_growth + bi->dd_growth,
590 bi->data_growth + bi->dd_growth + bi->idx_growth);
591 pr_err("\tbudg_data_growth %lld, budg_dd_growth %lld, budg_idx_growth %lld\n",
592 bi->data_growth, bi->dd_growth, bi->idx_growth);
593 pr_err("\tmin_idx_lebs %d, old_idx_sz %llu, uncommitted_idx %lld\n",
594 bi->min_idx_lebs, bi->old_idx_sz, bi->uncommitted_idx);
595 pr_err("\tpage_budget %d, inode_budget %d, dent_budget %d\n",
596 bi->page_budget, bi->inode_budget, bi->dent_budget);
597 pr_err("\tnospace %u, nospace_rp %u\n", bi->nospace, bi->nospace_rp);
598 pr_err("\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
599 c->dark_wm, c->dead_wm, c->max_idx_node_sz);
603 * If we are dumping saved budgeting data, do not print
604 * additional information which is about the current state, not
605 * the old one which corresponded to the saved budgeting data.
609 pr_err("\tfreeable_cnt %d, calc_idx_sz %lld, idx_gc_cnt %d\n",
610 c->freeable_cnt, c->calc_idx_sz, c->idx_gc_cnt);
611 pr_err("\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, clean_zn_cnt %ld\n",
612 atomic_long_read(&c->dirty_pg_cnt),
613 atomic_long_read(&c->dirty_zn_cnt),
614 atomic_long_read(&c->clean_zn_cnt));
615 pr_err("\tgc_lnum %d, ihead_lnum %d\n", c->gc_lnum, c->ihead_lnum);
617 /* If we are in R/O mode, journal heads do not exist */
619 for (i = 0; i < c->jhead_cnt; i++)
620 pr_err("\tjhead %s\t LEB %d\n",
621 dbg_jhead(c->jheads[i].wbuf.jhead),
622 c->jheads[i].wbuf.lnum);
623 for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
624 bud = rb_entry(rb, struct ubifs_bud, rb);
625 pr_err("\tbud LEB %d\n", bud->lnum);
627 list_for_each_entry(bud, &c->old_buds, list)
628 pr_err("\told bud LEB %d\n", bud->lnum);
629 list_for_each_entry(idx_gc, &c->idx_gc, list)
630 pr_err("\tGC'ed idx LEB %d unmap %d\n",
631 idx_gc->lnum, idx_gc->unmap);
632 pr_err("\tcommit state %d\n", c->cmt_state);
634 /* Print budgeting predictions */
635 available = ubifs_calc_available(c, c->bi.min_idx_lebs);
636 outstanding = c->bi.data_growth + c->bi.dd_growth;
637 free = ubifs_get_free_space_nolock(c);
638 pr_err("Budgeting predictions:\n");
639 pr_err("\tavailable: %lld, outstanding %lld, free %lld\n",
640 available, outstanding, free);
642 spin_unlock(&dbg_lock);
643 spin_unlock(&c->space_lock);
646 void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)
651 void ubifs_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
653 int i, spc, dark = 0, dead = 0;
655 struct ubifs_bud *bud;
657 spc = lp->free + lp->dirty;
658 if (spc < c->dead_wm)
661 dark = ubifs_calc_dark(c, spc);
663 if (lp->flags & LPROPS_INDEX)
664 pr_err("LEB %-7d free %-8d dirty %-8d used %-8d free + dirty %-8d flags %#x (",
665 lp->lnum, lp->free, lp->dirty, c->leb_size - spc, spc,
668 pr_err("LEB %-7d free %-8d dirty %-8d used %-8d free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d flags %#-4x (",
669 lp->lnum, lp->free, lp->dirty, c->leb_size - spc, spc,
670 dark, dead, (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags);
672 if (lp->flags & LPROPS_TAKEN) {
673 if (lp->flags & LPROPS_INDEX)
674 pr_cont("index, taken");
680 if (lp->flags & LPROPS_INDEX) {
681 switch (lp->flags & LPROPS_CAT_MASK) {
682 case LPROPS_DIRTY_IDX:
685 case LPROPS_FRDI_IDX:
686 s = "freeable index";
692 switch (lp->flags & LPROPS_CAT_MASK) {
694 s = "not categorized";
705 case LPROPS_FREEABLE:
716 for (rb = rb_first((struct rb_root *)&c->buds); rb; rb = rb_next(rb)) {
717 bud = rb_entry(rb, struct ubifs_bud, rb);
718 if (bud->lnum == lp->lnum) {
720 for (i = 0; i < c->jhead_cnt; i++) {
722 * Note, if we are in R/O mode or in the middle
723 * of mounting/re-mounting, the write-buffers do
727 lp->lnum == c->jheads[i].wbuf.lnum) {
728 pr_cont(", jhead %s", dbg_jhead(i));
733 pr_cont(", bud of jhead %s",
734 dbg_jhead(bud->jhead));
737 if (lp->lnum == c->gc_lnum)
742 void ubifs_dump_lprops(struct ubifs_info *c)
745 struct ubifs_lprops lp;
746 struct ubifs_lp_stats lst;
748 pr_err("(pid %d) start dumping LEB properties\n", current->pid);
749 ubifs_get_lp_stats(c, &lst);
750 ubifs_dump_lstats(&lst);
752 for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
753 err = ubifs_read_one_lp(c, lnum, &lp);
755 ubifs_err(c, "cannot read lprops for LEB %d", lnum);
759 ubifs_dump_lprop(c, &lp);
761 pr_err("(pid %d) finish dumping LEB properties\n", current->pid);
764 void ubifs_dump_lpt_info(struct ubifs_info *c)
768 spin_lock(&dbg_lock);
769 pr_err("(pid %d) dumping LPT information\n", current->pid);
770 pr_err("\tlpt_sz: %lld\n", c->lpt_sz);
771 pr_err("\tpnode_sz: %d\n", c->pnode_sz);
772 pr_err("\tnnode_sz: %d\n", c->nnode_sz);
773 pr_err("\tltab_sz: %d\n", c->ltab_sz);
774 pr_err("\tlsave_sz: %d\n", c->lsave_sz);
775 pr_err("\tbig_lpt: %d\n", c->big_lpt);
776 pr_err("\tlpt_hght: %d\n", c->lpt_hght);
777 pr_err("\tpnode_cnt: %d\n", c->pnode_cnt);
778 pr_err("\tnnode_cnt: %d\n", c->nnode_cnt);
779 pr_err("\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt);
780 pr_err("\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt);
781 pr_err("\tlsave_cnt: %d\n", c->lsave_cnt);
782 pr_err("\tspace_bits: %d\n", c->space_bits);
783 pr_err("\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
784 pr_err("\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
785 pr_err("\tlpt_spc_bits: %d\n", c->lpt_spc_bits);
786 pr_err("\tpcnt_bits: %d\n", c->pcnt_bits);
787 pr_err("\tlnum_bits: %d\n", c->lnum_bits);
788 pr_err("\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
789 pr_err("\tLPT head is at %d:%d\n",
790 c->nhead_lnum, c->nhead_offs);
791 pr_err("\tLPT ltab is at %d:%d\n", c->ltab_lnum, c->ltab_offs);
793 pr_err("\tLPT lsave is at %d:%d\n",
794 c->lsave_lnum, c->lsave_offs);
795 for (i = 0; i < c->lpt_lebs; i++)
796 pr_err("\tLPT LEB %d free %d dirty %d tgc %d cmt %d\n",
797 i + c->lpt_first, c->ltab[i].free, c->ltab[i].dirty,
798 c->ltab[i].tgc, c->ltab[i].cmt);
799 spin_unlock(&dbg_lock);
802 void ubifs_dump_sleb(const struct ubifs_info *c,
803 const struct ubifs_scan_leb *sleb, int offs)
805 struct ubifs_scan_node *snod;
807 pr_err("(pid %d) start dumping scanned data from LEB %d:%d\n",
808 current->pid, sleb->lnum, offs);
810 list_for_each_entry(snod, &sleb->nodes, list) {
812 pr_err("Dumping node at LEB %d:%d len %d\n",
813 sleb->lnum, snod->offs, snod->len);
814 ubifs_dump_node(c, snod->node);
818 void ubifs_dump_leb(const struct ubifs_info *c, int lnum)
820 struct ubifs_scan_leb *sleb;
821 struct ubifs_scan_node *snod;
824 pr_err("(pid %d) start dumping LEB %d\n", current->pid, lnum);
826 buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
828 ubifs_err(c, "cannot allocate memory for dumping LEB %d", lnum);
832 sleb = ubifs_scan(c, lnum, 0, buf, 0);
834 ubifs_err(c, "scan error %d", (int)PTR_ERR(sleb));
838 pr_err("LEB %d has %d nodes ending at %d\n", lnum,
839 sleb->nodes_cnt, sleb->endpt);
841 list_for_each_entry(snod, &sleb->nodes, list) {
843 pr_err("Dumping node at LEB %d:%d len %d\n", lnum,
844 snod->offs, snod->len);
845 ubifs_dump_node(c, snod->node);
848 pr_err("(pid %d) finish dumping LEB %d\n", current->pid, lnum);
849 ubifs_scan_destroy(sleb);
856 void ubifs_dump_znode(const struct ubifs_info *c,
857 const struct ubifs_znode *znode)
860 const struct ubifs_zbranch *zbr;
861 char key_buf[DBG_KEY_BUF_LEN];
863 spin_lock(&dbg_lock);
865 zbr = &znode->parent->zbranch[znode->iip];
869 pr_err("znode %p, LEB %d:%d len %d parent %p iip %d level %d child_cnt %d flags %lx\n",
870 znode, zbr->lnum, zbr->offs, zbr->len, znode->parent, znode->iip,
871 znode->level, znode->child_cnt, znode->flags);
873 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
874 spin_unlock(&dbg_lock);
878 pr_err("zbranches:\n");
879 for (n = 0; n < znode->child_cnt; n++) {
880 zbr = &znode->zbranch[n];
881 if (znode->level > 0)
882 pr_err("\t%d: znode %p LEB %d:%d len %d key %s\n",
883 n, zbr->znode, zbr->lnum, zbr->offs, zbr->len,
884 dbg_snprintf_key(c, &zbr->key, key_buf,
887 pr_err("\t%d: LNC %p LEB %d:%d len %d key %s\n",
888 n, zbr->znode, zbr->lnum, zbr->offs, zbr->len,
889 dbg_snprintf_key(c, &zbr->key, key_buf,
892 spin_unlock(&dbg_lock);
895 void ubifs_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
899 pr_err("(pid %d) start dumping heap cat %d (%d elements)\n",
900 current->pid, cat, heap->cnt);
901 for (i = 0; i < heap->cnt; i++) {
902 struct ubifs_lprops *lprops = heap->arr[i];
904 pr_err("\t%d. LEB %d hpos %d free %d dirty %d flags %d\n",
905 i, lprops->lnum, lprops->hpos, lprops->free,
906 lprops->dirty, lprops->flags);
908 pr_err("(pid %d) finish dumping heap\n", current->pid);
911 void ubifs_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
912 struct ubifs_nnode *parent, int iip)
916 pr_err("(pid %d) dumping pnode:\n", current->pid);
917 pr_err("\taddress %zx parent %zx cnext %zx\n",
918 (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
919 pr_err("\tflags %lu iip %d level %d num %d\n",
920 pnode->flags, iip, pnode->level, pnode->num);
921 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
922 struct ubifs_lprops *lp = &pnode->lprops[i];
924 pr_err("\t%d: free %d dirty %d flags %d lnum %d\n",
925 i, lp->free, lp->dirty, lp->flags, lp->lnum);
929 void ubifs_dump_tnc(struct ubifs_info *c)
931 struct ubifs_znode *znode;
935 pr_err("(pid %d) start dumping TNC tree\n", current->pid);
936 znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
937 level = znode->level;
938 pr_err("== Level %d ==\n", level);
940 if (level != znode->level) {
941 level = znode->level;
942 pr_err("== Level %d ==\n", level);
944 ubifs_dump_znode(c, znode);
945 znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
947 pr_err("(pid %d) finish dumping TNC tree\n", current->pid);
950 static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
953 ubifs_dump_znode(c, znode);
958 * ubifs_dump_index - dump the on-flash index.
959 * @c: UBIFS file-system description object
961 * This function dumps whole UBIFS indexing B-tree, unlike 'ubifs_dump_tnc()'
962 * which dumps only in-memory znodes and does not read znodes which from flash.
964 void ubifs_dump_index(struct ubifs_info *c)
966 dbg_walk_index(c, NULL, dump_znode, NULL);
971 * dbg_save_space_info - save information about flash space.
972 * @c: UBIFS file-system description object
974 * This function saves information about UBIFS free space, dirty space, etc, in
975 * order to check it later.
977 void dbg_save_space_info(struct ubifs_info *c)
979 struct ubifs_debug_info *d = c->dbg;
982 spin_lock(&c->space_lock);
983 memcpy(&d->saved_lst, &c->lst, sizeof(struct ubifs_lp_stats));
984 memcpy(&d->saved_bi, &c->bi, sizeof(struct ubifs_budg_info));
985 d->saved_idx_gc_cnt = c->idx_gc_cnt;
988 * We use a dirty hack here and zero out @c->freeable_cnt, because it
989 * affects the free space calculations, and UBIFS might not know about
990 * all freeable eraseblocks. Indeed, we know about freeable eraseblocks
991 * only when we read their lprops, and we do this only lazily, upon the
992 * need. So at any given point of time @c->freeable_cnt might be not
995 * Just one example about the issue we hit when we did not zero
997 * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the
998 * amount of free space in @d->saved_free
999 * 2. We re-mount R/W, which makes UBIFS to read the "lsave"
1000 * information from flash, where we cache LEBs from various
1001 * categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()'
1002 * -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()'
1003 * -> 'ubifs_get_pnode()' -> 'update_cats()'
1004 * -> 'ubifs_add_to_cat()').
1005 * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt
1007 * 4. We calculate the amount of free space when the re-mount is
1008 * finished in 'dbg_check_space_info()' and it does not match
1011 freeable_cnt = c->freeable_cnt;
1012 c->freeable_cnt = 0;
1013 d->saved_free = ubifs_get_free_space_nolock(c);
1014 c->freeable_cnt = freeable_cnt;
1015 spin_unlock(&c->space_lock);
1019 * dbg_check_space_info - check flash space information.
1020 * @c: UBIFS file-system description object
1022 * This function compares current flash space information with the information
1023 * which was saved when the 'dbg_save_space_info()' function was called.
1024 * Returns zero if the information has not changed, and %-EINVAL it it has
1027 int dbg_check_space_info(struct ubifs_info *c)
1029 struct ubifs_debug_info *d = c->dbg;
1030 struct ubifs_lp_stats lst;
1034 spin_lock(&c->space_lock);
1035 freeable_cnt = c->freeable_cnt;
1036 c->freeable_cnt = 0;
1037 free = ubifs_get_free_space_nolock(c);
1038 c->freeable_cnt = freeable_cnt;
1039 spin_unlock(&c->space_lock);
1041 if (free != d->saved_free) {
1042 ubifs_err(c, "free space changed from %lld to %lld",
1043 d->saved_free, free);
1050 ubifs_msg(c, "saved lprops statistics dump");
1051 ubifs_dump_lstats(&d->saved_lst);
1052 ubifs_msg(c, "saved budgeting info dump");
1053 ubifs_dump_budg(c, &d->saved_bi);
1054 ubifs_msg(c, "saved idx_gc_cnt %d", d->saved_idx_gc_cnt);
1055 ubifs_msg(c, "current lprops statistics dump");
1056 ubifs_get_lp_stats(c, &lst);
1057 ubifs_dump_lstats(&lst);
1058 ubifs_msg(c, "current budgeting info dump");
1059 ubifs_dump_budg(c, &c->bi);
1065 * dbg_check_synced_i_size - check synchronized inode size.
1066 * @c: UBIFS file-system description object
1067 * @inode: inode to check
1069 * If inode is clean, synchronized inode size has to be equivalent to current
1070 * inode size. This function has to be called only for locked inodes (@i_mutex
1071 * has to be locked). Returns %0 if synchronized inode size if correct, and
1074 int dbg_check_synced_i_size(const struct ubifs_info *c, struct inode *inode)
1077 struct ubifs_inode *ui = ubifs_inode(inode);
1079 if (!dbg_is_chk_gen(c))
1081 if (!S_ISREG(inode->i_mode))
1084 mutex_lock(&ui->ui_mutex);
1085 spin_lock(&ui->ui_lock);
1086 if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
1087 ubifs_err(c, "ui_size is %lld, synced_i_size is %lld, but inode is clean",
1088 ui->ui_size, ui->synced_i_size);
1089 ubifs_err(c, "i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
1090 inode->i_mode, i_size_read(inode));
1094 spin_unlock(&ui->ui_lock);
1095 mutex_unlock(&ui->ui_mutex);
1100 * dbg_check_dir - check directory inode size and link count.
1101 * @c: UBIFS file-system description object
1102 * @dir: the directory to calculate size for
1103 * @size: the result is returned here
1105 * This function makes sure that directory size and link count are correct.
1106 * Returns zero in case of success and a negative error code in case of
1109 * Note, it is good idea to make sure the @dir->i_mutex is locked before
1110 * calling this function.
1112 int dbg_check_dir(struct ubifs_info *c, const struct inode *dir)
1114 unsigned int nlink = 2;
1115 union ubifs_key key;
1116 struct ubifs_dent_node *dent, *pdent = NULL;
1117 struct qstr nm = { .name = NULL };
1118 loff_t size = UBIFS_INO_NODE_SZ;
1120 if (!dbg_is_chk_gen(c))
1123 if (!S_ISDIR(dir->i_mode))
1126 lowest_dent_key(c, &key, dir->i_ino);
1130 dent = ubifs_tnc_next_ent(c, &key, &nm);
1132 err = PTR_ERR(dent);
1138 nm.name = dent->name;
1139 nm.len = le16_to_cpu(dent->nlen);
1140 size += CALC_DENT_SIZE(nm.len);
1141 if (dent->type == UBIFS_ITYPE_DIR)
1145 key_read(c, &dent->key, &key);
1149 if (i_size_read(dir) != size) {
1150 ubifs_err(c, "directory inode %lu has size %llu, but calculated size is %llu",
1151 dir->i_ino, (unsigned long long)i_size_read(dir),
1152 (unsigned long long)size);
1153 ubifs_dump_inode(c, dir);
1157 if (dir->i_nlink != nlink) {
1158 ubifs_err(c, "directory inode %lu has nlink %u, but calculated nlink is %u",
1159 dir->i_ino, dir->i_nlink, nlink);
1160 ubifs_dump_inode(c, dir);
1169 * dbg_check_key_order - make sure that colliding keys are properly ordered.
1170 * @c: UBIFS file-system description object
1171 * @zbr1: first zbranch
1172 * @zbr2: following zbranch
1174 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
1175 * names of the direntries/xentries which are referred by the keys. This
1176 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
1177 * sure the name of direntry/xentry referred by @zbr1 is less than
1178 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
1179 * and a negative error code in case of failure.
1181 static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
1182 struct ubifs_zbranch *zbr2)
1184 int err, nlen1, nlen2, cmp;
1185 struct ubifs_dent_node *dent1, *dent2;
1186 union ubifs_key key;
1187 char key_buf[DBG_KEY_BUF_LEN];
1189 ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
1190 dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1193 dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1199 err = ubifs_tnc_read_node(c, zbr1, dent1);
1202 err = ubifs_validate_entry(c, dent1);
1206 err = ubifs_tnc_read_node(c, zbr2, dent2);
1209 err = ubifs_validate_entry(c, dent2);
1213 /* Make sure node keys are the same as in zbranch */
1215 key_read(c, &dent1->key, &key);
1216 if (keys_cmp(c, &zbr1->key, &key)) {
1217 ubifs_err(c, "1st entry at %d:%d has key %s", zbr1->lnum,
1218 zbr1->offs, dbg_snprintf_key(c, &key, key_buf,
1220 ubifs_err(c, "but it should have key %s according to tnc",
1221 dbg_snprintf_key(c, &zbr1->key, key_buf,
1223 ubifs_dump_node(c, dent1);
1227 key_read(c, &dent2->key, &key);
1228 if (keys_cmp(c, &zbr2->key, &key)) {
1229 ubifs_err(c, "2nd entry at %d:%d has key %s", zbr1->lnum,
1230 zbr1->offs, dbg_snprintf_key(c, &key, key_buf,
1232 ubifs_err(c, "but it should have key %s according to tnc",
1233 dbg_snprintf_key(c, &zbr2->key, key_buf,
1235 ubifs_dump_node(c, dent2);
1239 nlen1 = le16_to_cpu(dent1->nlen);
1240 nlen2 = le16_to_cpu(dent2->nlen);
1242 cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
1243 if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
1247 if (cmp == 0 && nlen1 == nlen2)
1248 ubifs_err(c, "2 xent/dent nodes with the same name");
1250 ubifs_err(c, "bad order of colliding key %s",
1251 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
1253 ubifs_msg(c, "first node at %d:%d\n", zbr1->lnum, zbr1->offs);
1254 ubifs_dump_node(c, dent1);
1255 ubifs_msg(c, "second node at %d:%d\n", zbr2->lnum, zbr2->offs);
1256 ubifs_dump_node(c, dent2);
1265 * dbg_check_znode - check if znode is all right.
1266 * @c: UBIFS file-system description object
1267 * @zbr: zbranch which points to this znode
1269 * This function makes sure that znode referred to by @zbr is all right.
1270 * Returns zero if it is, and %-EINVAL if it is not.
1272 static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
1274 struct ubifs_znode *znode = zbr->znode;
1275 struct ubifs_znode *zp = znode->parent;
1278 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
1282 if (znode->level < 0) {
1286 if (znode->iip < 0 || znode->iip >= c->fanout) {
1292 /* Only dirty zbranch may have no on-flash nodes */
1293 if (!ubifs_zn_dirty(znode)) {
1298 if (ubifs_zn_dirty(znode)) {
1300 * If znode is dirty, its parent has to be dirty as well. The
1301 * order of the operation is important, so we have to have
1305 if (zp && !ubifs_zn_dirty(zp)) {
1307 * The dirty flag is atomic and is cleared outside the
1308 * TNC mutex, so znode's dirty flag may now have
1309 * been cleared. The child is always cleared before the
1310 * parent, so we just need to check again.
1313 if (ubifs_zn_dirty(znode)) {
1321 const union ubifs_key *min, *max;
1323 if (znode->level != zp->level - 1) {
1328 /* Make sure the 'parent' pointer in our znode is correct */
1329 err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
1331 /* This zbranch does not exist in the parent */
1336 if (znode->iip >= zp->child_cnt) {
1341 if (znode->iip != n) {
1342 /* This may happen only in case of collisions */
1343 if (keys_cmp(c, &zp->zbranch[n].key,
1344 &zp->zbranch[znode->iip].key)) {
1352 * Make sure that the first key in our znode is greater than or
1353 * equal to the key in the pointing zbranch.
1356 cmp = keys_cmp(c, min, &znode->zbranch[0].key);
1362 if (n + 1 < zp->child_cnt) {
1363 max = &zp->zbranch[n + 1].key;
1366 * Make sure the last key in our znode is less or
1367 * equivalent than the key in the zbranch which goes
1368 * after our pointing zbranch.
1370 cmp = keys_cmp(c, max,
1371 &znode->zbranch[znode->child_cnt - 1].key);
1378 /* This may only be root znode */
1379 if (zbr != &c->zroot) {
1386 * Make sure that next key is greater or equivalent then the previous
1389 for (n = 1; n < znode->child_cnt; n++) {
1390 cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
1391 &znode->zbranch[n].key);
1397 /* This can only be keys with colliding hash */
1398 if (!is_hash_key(c, &znode->zbranch[n].key)) {
1403 if (znode->level != 0 || c->replaying)
1407 * Colliding keys should follow binary order of
1408 * corresponding xentry/dentry names.
1410 err = dbg_check_key_order(c, &znode->zbranch[n - 1],
1411 &znode->zbranch[n]);
1421 for (n = 0; n < znode->child_cnt; n++) {
1422 if (!znode->zbranch[n].znode &&
1423 (znode->zbranch[n].lnum == 0 ||
1424 znode->zbranch[n].len == 0)) {
1429 if (znode->zbranch[n].lnum != 0 &&
1430 znode->zbranch[n].len == 0) {
1435 if (znode->zbranch[n].lnum == 0 &&
1436 znode->zbranch[n].len != 0) {
1441 if (znode->zbranch[n].lnum == 0 &&
1442 znode->zbranch[n].offs != 0) {
1447 if (znode->level != 0 && znode->zbranch[n].znode)
1448 if (znode->zbranch[n].znode->parent != znode) {
1457 ubifs_err(c, "failed, error %d", err);
1458 ubifs_msg(c, "dump of the znode");
1459 ubifs_dump_znode(c, znode);
1461 ubifs_msg(c, "dump of the parent znode");
1462 ubifs_dump_znode(c, zp);
1469 int dbg_check_dir(struct ubifs_info *c, const struct inode *dir)
1474 void dbg_debugfs_exit_fs(struct ubifs_info *c)
1479 int ubifs_debugging_init(struct ubifs_info *c)
1483 void ubifs_debugging_exit(struct ubifs_info *c)
1486 int dbg_check_filesystem(struct ubifs_info *c)
1490 int dbg_debugfs_init_fs(struct ubifs_info *c)
1498 * dbg_check_tnc - check TNC tree.
1499 * @c: UBIFS file-system description object
1500 * @extra: do extra checks that are possible at start commit
1502 * This function traverses whole TNC tree and checks every znode. Returns zero
1503 * if everything is all right and %-EINVAL if something is wrong with TNC.
1505 int dbg_check_tnc(struct ubifs_info *c, int extra)
1507 struct ubifs_znode *znode;
1508 long clean_cnt = 0, dirty_cnt = 0;
1511 if (!dbg_is_chk_index(c))
1514 ubifs_assert(mutex_is_locked(&c->tnc_mutex));
1515 if (!c->zroot.znode)
1518 znode = ubifs_tnc_postorder_first(c->zroot.znode);
1520 struct ubifs_znode *prev;
1521 struct ubifs_zbranch *zbr;
1526 zbr = &znode->parent->zbranch[znode->iip];
1528 err = dbg_check_znode(c, zbr);
1533 if (ubifs_zn_dirty(znode))
1540 znode = ubifs_tnc_postorder_next(znode);
1545 * If the last key of this znode is equivalent to the first key
1546 * of the next znode (collision), then check order of the keys.
1548 last = prev->child_cnt - 1;
1549 if (prev->level == 0 && znode->level == 0 && !c->replaying &&
1550 !keys_cmp(c, &prev->zbranch[last].key,
1551 &znode->zbranch[0].key)) {
1552 err = dbg_check_key_order(c, &prev->zbranch[last],
1553 &znode->zbranch[0]);
1557 ubifs_msg(c, "first znode");
1558 ubifs_dump_znode(c, prev);
1559 ubifs_msg(c, "second znode");
1560 ubifs_dump_znode(c, znode);
1567 if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
1568 ubifs_err(c, "incorrect clean_zn_cnt %ld, calculated %ld",
1569 atomic_long_read(&c->clean_zn_cnt),
1573 if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
1574 ubifs_err(c, "incorrect dirty_zn_cnt %ld, calculated %ld",
1575 atomic_long_read(&c->dirty_zn_cnt),
1584 int dbg_check_tnc(struct ubifs_info *c, int extra)
1591 * dbg_walk_index - walk the on-flash index.
1592 * @c: UBIFS file-system description object
1593 * @leaf_cb: called for each leaf node
1594 * @znode_cb: called for each indexing node
1595 * @priv: private data which is passed to callbacks
1597 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1598 * node and @znode_cb for each indexing node. Returns zero in case of success
1599 * and a negative error code in case of failure.
1601 * It would be better if this function removed every znode it pulled to into
1602 * the TNC, so that the behavior more closely matched the non-debugging
1605 int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1606 dbg_znode_callback znode_cb, void *priv)
1609 struct ubifs_zbranch *zbr;
1610 struct ubifs_znode *znode, *child;
1612 mutex_lock(&c->tnc_mutex);
1613 /* If the root indexing node is not in TNC - pull it */
1614 if (!c->zroot.znode) {
1615 c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1616 if (IS_ERR(c->zroot.znode)) {
1617 err = PTR_ERR(c->zroot.znode);
1618 c->zroot.znode = NULL;
1624 * We are going to traverse the indexing tree in the postorder manner.
1625 * Go down and find the leftmost indexing node where we are going to
1628 znode = c->zroot.znode;
1629 while (znode->level > 0) {
1630 zbr = &znode->zbranch[0];
1633 child = ubifs_load_znode(c, zbr, znode, 0);
1634 if (IS_ERR(child)) {
1635 err = PTR_ERR(child);
1644 /* Iterate over all indexing nodes */
1651 err = znode_cb(c, znode, priv);
1653 ubifs_err(c, "znode checking function returned error %d",
1655 ubifs_dump_znode(c, znode);
1659 if (leaf_cb && znode->level == 0) {
1660 for (idx = 0; idx < znode->child_cnt; idx++) {
1661 zbr = &znode->zbranch[idx];
1662 err = leaf_cb(c, zbr, priv);
1664 ubifs_err(c, "leaf checking function returned error %d, for leaf at LEB %d:%d",
1665 err, zbr->lnum, zbr->offs);
1674 idx = znode->iip + 1;
1675 znode = znode->parent;
1676 if (idx < znode->child_cnt) {
1677 /* Switch to the next index in the parent */
1678 zbr = &znode->zbranch[idx];
1681 child = ubifs_load_znode(c, zbr, znode, idx);
1682 if (IS_ERR(child)) {
1683 err = PTR_ERR(child);
1691 * This is the last child, switch to the parent and
1696 /* Go to the lowest leftmost znode in the new sub-tree */
1697 while (znode->level > 0) {
1698 zbr = &znode->zbranch[0];
1701 child = ubifs_load_znode(c, zbr, znode, 0);
1702 if (IS_ERR(child)) {
1703 err = PTR_ERR(child);
1712 mutex_unlock(&c->tnc_mutex);
1717 zbr = &znode->parent->zbranch[znode->iip];
1720 ubifs_msg(c, "dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1721 ubifs_dump_znode(c, znode);
1723 mutex_unlock(&c->tnc_mutex);
1728 * add_size - add znode size to partially calculated index size.
1729 * @c: UBIFS file-system description object
1730 * @znode: znode to add size for
1731 * @priv: partially calculated index size
1733 * This is a helper function for 'dbg_check_idx_size()' which is called for
1734 * every indexing node and adds its size to the 'long long' variable pointed to
1737 static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1739 long long *idx_size = priv;
1742 add = ubifs_idx_node_sz(c, znode->child_cnt);
1743 add = ALIGN(add, 8);
1749 * dbg_check_idx_size - check index size.
1750 * @c: UBIFS file-system description object
1751 * @idx_size: size to check
1753 * This function walks the UBIFS index, calculates its size and checks that the
1754 * size is equivalent to @idx_size. Returns zero in case of success and a
1755 * negative error code in case of failure.
1757 int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
1762 if (!dbg_is_chk_index(c))
1765 err = dbg_walk_index(c, NULL, add_size, &calc);
1767 ubifs_err(c, "error %d while walking the index", err);
1771 if (calc != idx_size) {
1772 ubifs_err(c, "index size check failed: calculated size is %lld, should be %lld",
1783 * struct fsck_inode - information about an inode used when checking the file-system.
1784 * @rb: link in the RB-tree of inodes
1785 * @inum: inode number
1786 * @mode: inode type, permissions, etc
1787 * @nlink: inode link count
1788 * @xattr_cnt: count of extended attributes
1789 * @references: how many directory/xattr entries refer this inode (calculated
1790 * while walking the index)
1791 * @calc_cnt: for directory inode count of child directories
1792 * @size: inode size (read from on-flash inode)
1793 * @xattr_sz: summary size of all extended attributes (read from on-flash
1795 * @calc_sz: for directories calculated directory size
1796 * @calc_xcnt: count of extended attributes
1797 * @calc_xsz: calculated summary size of all extended attributes
1798 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1799 * inode (read from on-flash inode)
1800 * @calc_xnms: calculated sum of lengths of all extended attribute names
1807 unsigned int xattr_cnt;
1811 unsigned int xattr_sz;
1813 long long calc_xcnt;
1815 unsigned int xattr_nms;
1816 long long calc_xnms;
1820 * struct fsck_data - private FS checking information.
1821 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1824 struct rb_root inodes;
1828 * add_inode - add inode information to RB-tree of inodes.
1829 * @c: UBIFS file-system description object
1830 * @fsckd: FS checking information
1831 * @ino: raw UBIFS inode to add
1833 * This is a helper function for 'check_leaf()' which adds information about
1834 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1835 * case of success and a negative error code in case of failure.
1837 static struct fsck_inode *add_inode(struct ubifs_info *c,
1838 struct fsck_data *fsckd,
1839 struct ubifs_ino_node *ino)
1841 struct rb_node **p, *parent = NULL;
1842 struct fsck_inode *fscki;
1843 ino_t inum = key_inum_flash(c, &ino->key);
1844 struct inode *inode;
1845 struct ubifs_inode *ui;
1847 p = &fsckd->inodes.rb_node;
1850 fscki = rb_entry(parent, struct fsck_inode, rb);
1851 if (inum < fscki->inum)
1853 else if (inum > fscki->inum)
1854 p = &(*p)->rb_right;
1859 if (inum > c->highest_inum) {
1860 ubifs_err(c, "too high inode number, max. is %lu",
1861 (unsigned long)c->highest_inum);
1862 return ERR_PTR(-EINVAL);
1865 fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
1867 return ERR_PTR(-ENOMEM);
1869 inode = ilookup(c->vfs_sb, inum);
1873 * If the inode is present in the VFS inode cache, use it instead of
1874 * the on-flash inode which might be out-of-date. E.g., the size might
1875 * be out-of-date. If we do not do this, the following may happen, for
1877 * 1. A power cut happens
1878 * 2. We mount the file-system R/O, the replay process fixes up the
1879 * inode size in the VFS cache, but on on-flash.
1880 * 3. 'check_leaf()' fails because it hits a data node beyond inode
1884 fscki->nlink = le32_to_cpu(ino->nlink);
1885 fscki->size = le64_to_cpu(ino->size);
1886 fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
1887 fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
1888 fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
1889 fscki->mode = le32_to_cpu(ino->mode);
1891 ui = ubifs_inode(inode);
1892 fscki->nlink = inode->i_nlink;
1893 fscki->size = inode->i_size;
1894 fscki->xattr_cnt = ui->xattr_cnt;
1895 fscki->xattr_sz = ui->xattr_size;
1896 fscki->xattr_nms = ui->xattr_names;
1897 fscki->mode = inode->i_mode;
1901 if (S_ISDIR(fscki->mode)) {
1902 fscki->calc_sz = UBIFS_INO_NODE_SZ;
1903 fscki->calc_cnt = 2;
1906 rb_link_node(&fscki->rb, parent, p);
1907 rb_insert_color(&fscki->rb, &fsckd->inodes);
1913 * search_inode - search inode in the RB-tree of inodes.
1914 * @fsckd: FS checking information
1915 * @inum: inode number to search
1917 * This is a helper function for 'check_leaf()' which searches inode @inum in
1918 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1919 * the inode was not found.
1921 static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
1924 struct fsck_inode *fscki;
1926 p = fsckd->inodes.rb_node;
1928 fscki = rb_entry(p, struct fsck_inode, rb);
1929 if (inum < fscki->inum)
1931 else if (inum > fscki->inum)
1940 * read_add_inode - read inode node and add it to RB-tree of inodes.
1941 * @c: UBIFS file-system description object
1942 * @fsckd: FS checking information
1943 * @inum: inode number to read
1945 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1946 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1947 * information pointer in case of success and a negative error code in case of
1950 static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1951 struct fsck_data *fsckd, ino_t inum)
1954 union ubifs_key key;
1955 struct ubifs_znode *znode;
1956 struct ubifs_zbranch *zbr;
1957 struct ubifs_ino_node *ino;
1958 struct fsck_inode *fscki;
1960 fscki = search_inode(fsckd, inum);
1964 ino_key_init(c, &key, inum);
1965 err = ubifs_lookup_level0(c, &key, &znode, &n);
1967 ubifs_err(c, "inode %lu not found in index", (unsigned long)inum);
1968 return ERR_PTR(-ENOENT);
1969 } else if (err < 0) {
1970 ubifs_err(c, "error %d while looking up inode %lu",
1971 err, (unsigned long)inum);
1972 return ERR_PTR(err);
1975 zbr = &znode->zbranch[n];
1976 if (zbr->len < UBIFS_INO_NODE_SZ) {
1977 ubifs_err(c, "bad node %lu node length %d",
1978 (unsigned long)inum, zbr->len);
1979 return ERR_PTR(-EINVAL);
1982 ino = kmalloc(zbr->len, GFP_NOFS);
1984 return ERR_PTR(-ENOMEM);
1986 err = ubifs_tnc_read_node(c, zbr, ino);
1988 ubifs_err(c, "cannot read inode node at LEB %d:%d, error %d",
1989 zbr->lnum, zbr->offs, err);
1991 return ERR_PTR(err);
1994 fscki = add_inode(c, fsckd, ino);
1996 if (IS_ERR(fscki)) {
1997 ubifs_err(c, "error %ld while adding inode %lu node",
1998 PTR_ERR(fscki), (unsigned long)inum);
2006 * check_leaf - check leaf node.
2007 * @c: UBIFS file-system description object
2008 * @zbr: zbranch of the leaf node to check
2009 * @priv: FS checking information
2011 * This is a helper function for 'dbg_check_filesystem()' which is called for
2012 * every single leaf node while walking the indexing tree. It checks that the
2013 * leaf node referred from the indexing tree exists, has correct CRC, and does
2014 * some other basic validation. This function is also responsible for building
2015 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
2016 * calculates reference count, size, etc for each inode in order to later
2017 * compare them to the information stored inside the inodes and detect possible
2018 * inconsistencies. Returns zero in case of success and a negative error code
2019 * in case of failure.
2021 static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
2026 struct ubifs_ch *ch;
2027 int err, type = key_type(c, &zbr->key);
2028 struct fsck_inode *fscki;
2030 if (zbr->len < UBIFS_CH_SZ) {
2031 ubifs_err(c, "bad leaf length %d (LEB %d:%d)",
2032 zbr->len, zbr->lnum, zbr->offs);
2036 node = kmalloc(zbr->len, GFP_NOFS);
2040 err = ubifs_tnc_read_node(c, zbr, node);
2042 ubifs_err(c, "cannot read leaf node at LEB %d:%d, error %d",
2043 zbr->lnum, zbr->offs, err);
2047 /* If this is an inode node, add it to RB-tree of inodes */
2048 if (type == UBIFS_INO_KEY) {
2049 fscki = add_inode(c, priv, node);
2050 if (IS_ERR(fscki)) {
2051 err = PTR_ERR(fscki);
2052 ubifs_err(c, "error %d while adding inode node", err);
2058 if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
2059 type != UBIFS_DATA_KEY) {
2060 ubifs_err(c, "unexpected node type %d at LEB %d:%d",
2061 type, zbr->lnum, zbr->offs);
2067 if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
2068 ubifs_err(c, "too high sequence number, max. is %llu",
2074 if (type == UBIFS_DATA_KEY) {
2076 struct ubifs_data_node *dn = node;
2078 ubifs_assert(zbr->len >= UBIFS_DATA_NODE_SZ);
2081 * Search the inode node this data node belongs to and insert
2082 * it to the RB-tree of inodes.
2084 inum = key_inum_flash(c, &dn->key);
2085 fscki = read_add_inode(c, priv, inum);
2086 if (IS_ERR(fscki)) {
2087 err = PTR_ERR(fscki);
2088 ubifs_err(c, "error %d while processing data node and trying to find inode node %lu",
2089 err, (unsigned long)inum);
2093 /* Make sure the data node is within inode size */
2094 blk_offs = key_block_flash(c, &dn->key);
2095 blk_offs <<= UBIFS_BLOCK_SHIFT;
2096 blk_offs += le32_to_cpu(dn->size);
2097 if (blk_offs > fscki->size) {
2098 ubifs_err(c, "data node at LEB %d:%d is not within inode size %lld",
2099 zbr->lnum, zbr->offs, fscki->size);
2105 struct ubifs_dent_node *dent = node;
2106 struct fsck_inode *fscki1;
2108 ubifs_assert(zbr->len >= UBIFS_DENT_NODE_SZ);
2110 err = ubifs_validate_entry(c, dent);
2115 * Search the inode node this entry refers to and the parent
2116 * inode node and insert them to the RB-tree of inodes.
2118 inum = le64_to_cpu(dent->inum);
2119 fscki = read_add_inode(c, priv, inum);
2120 if (IS_ERR(fscki)) {
2121 err = PTR_ERR(fscki);
2122 ubifs_err(c, "error %d while processing entry node and trying to find inode node %lu",
2123 err, (unsigned long)inum);
2127 /* Count how many direntries or xentries refers this inode */
2128 fscki->references += 1;
2130 inum = key_inum_flash(c, &dent->key);
2131 fscki1 = read_add_inode(c, priv, inum);
2132 if (IS_ERR(fscki1)) {
2133 err = PTR_ERR(fscki1);
2134 ubifs_err(c, "error %d while processing entry node and trying to find parent inode node %lu",
2135 err, (unsigned long)inum);
2139 nlen = le16_to_cpu(dent->nlen);
2140 if (type == UBIFS_XENT_KEY) {
2141 fscki1->calc_xcnt += 1;
2142 fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
2143 fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
2144 fscki1->calc_xnms += nlen;
2146 fscki1->calc_sz += CALC_DENT_SIZE(nlen);
2147 if (dent->type == UBIFS_ITYPE_DIR)
2148 fscki1->calc_cnt += 1;
2157 ubifs_msg(c, "dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
2158 ubifs_dump_node(c, node);
2165 * free_inodes - free RB-tree of inodes.
2166 * @fsckd: FS checking information
2168 static void free_inodes(struct fsck_data *fsckd)
2170 struct fsck_inode *fscki, *n;
2172 rbtree_postorder_for_each_entry_safe(fscki, n, &fsckd->inodes, rb)
2177 * check_inodes - checks all inodes.
2178 * @c: UBIFS file-system description object
2179 * @fsckd: FS checking information
2181 * This is a helper function for 'dbg_check_filesystem()' which walks the
2182 * RB-tree of inodes after the index scan has been finished, and checks that
2183 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
2184 * %-EINVAL if not, and a negative error code in case of failure.
2186 static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
2189 union ubifs_key key;
2190 struct ubifs_znode *znode;
2191 struct ubifs_zbranch *zbr;
2192 struct ubifs_ino_node *ino;
2193 struct fsck_inode *fscki;
2194 struct rb_node *this = rb_first(&fsckd->inodes);
2197 fscki = rb_entry(this, struct fsck_inode, rb);
2198 this = rb_next(this);
2200 if (S_ISDIR(fscki->mode)) {
2202 * Directories have to have exactly one reference (they
2203 * cannot have hardlinks), although root inode is an
2206 if (fscki->inum != UBIFS_ROOT_INO &&
2207 fscki->references != 1) {
2208 ubifs_err(c, "directory inode %lu has %d direntries which refer it, but should be 1",
2209 (unsigned long)fscki->inum,
2213 if (fscki->inum == UBIFS_ROOT_INO &&
2214 fscki->references != 0) {
2215 ubifs_err(c, "root inode %lu has non-zero (%d) direntries which refer it",
2216 (unsigned long)fscki->inum,
2220 if (fscki->calc_sz != fscki->size) {
2221 ubifs_err(c, "directory inode %lu size is %lld, but calculated size is %lld",
2222 (unsigned long)fscki->inum,
2223 fscki->size, fscki->calc_sz);
2226 if (fscki->calc_cnt != fscki->nlink) {
2227 ubifs_err(c, "directory inode %lu nlink is %d, but calculated nlink is %d",
2228 (unsigned long)fscki->inum,
2229 fscki->nlink, fscki->calc_cnt);
2233 if (fscki->references != fscki->nlink) {
2234 ubifs_err(c, "inode %lu nlink is %d, but calculated nlink is %d",
2235 (unsigned long)fscki->inum,
2236 fscki->nlink, fscki->references);
2240 if (fscki->xattr_sz != fscki->calc_xsz) {
2241 ubifs_err(c, "inode %lu has xattr size %u, but calculated size is %lld",
2242 (unsigned long)fscki->inum, fscki->xattr_sz,
2246 if (fscki->xattr_cnt != fscki->calc_xcnt) {
2247 ubifs_err(c, "inode %lu has %u xattrs, but calculated count is %lld",
2248 (unsigned long)fscki->inum,
2249 fscki->xattr_cnt, fscki->calc_xcnt);
2252 if (fscki->xattr_nms != fscki->calc_xnms) {
2253 ubifs_err(c, "inode %lu has xattr names' size %u, but calculated names' size is %lld",
2254 (unsigned long)fscki->inum, fscki->xattr_nms,
2263 /* Read the bad inode and dump it */
2264 ino_key_init(c, &key, fscki->inum);
2265 err = ubifs_lookup_level0(c, &key, &znode, &n);
2267 ubifs_err(c, "inode %lu not found in index",
2268 (unsigned long)fscki->inum);
2270 } else if (err < 0) {
2271 ubifs_err(c, "error %d while looking up inode %lu",
2272 err, (unsigned long)fscki->inum);
2276 zbr = &znode->zbranch[n];
2277 ino = kmalloc(zbr->len, GFP_NOFS);
2281 err = ubifs_tnc_read_node(c, zbr, ino);
2283 ubifs_err(c, "cannot read inode node at LEB %d:%d, error %d",
2284 zbr->lnum, zbr->offs, err);
2289 ubifs_msg(c, "dump of the inode %lu sitting in LEB %d:%d",
2290 (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
2291 ubifs_dump_node(c, ino);
2297 * dbg_check_filesystem - check the file-system.
2298 * @c: UBIFS file-system description object
2300 * This function checks the file system, namely:
2301 * o makes sure that all leaf nodes exist and their CRCs are correct;
2302 * o makes sure inode nlink, size, xattr size/count are correct (for all
2305 * The function reads whole indexing tree and all nodes, so it is pretty
2306 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2307 * not, and a negative error code in case of failure.
2309 int dbg_check_filesystem(struct ubifs_info *c)
2312 struct fsck_data fsckd;
2314 if (!dbg_is_chk_fs(c))
2317 fsckd.inodes = RB_ROOT;
2318 err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
2322 err = check_inodes(c, &fsckd);
2326 free_inodes(&fsckd);
2330 ubifs_err(c, "file-system check failed with error %d", err);
2332 free_inodes(&fsckd);
2337 * dbg_check_data_nodes_order - check that list of data nodes is sorted.
2338 * @c: UBIFS file-system description object
2339 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2341 * This function returns zero if the list of data nodes is sorted correctly,
2342 * and %-EINVAL if not.
2344 int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head)
2346 struct list_head *cur;
2347 struct ubifs_scan_node *sa, *sb;
2349 if (!dbg_is_chk_gen(c))
2352 for (cur = head->next; cur->next != head; cur = cur->next) {
2354 uint32_t blka, blkb;
2357 sa = container_of(cur, struct ubifs_scan_node, list);
2358 sb = container_of(cur->next, struct ubifs_scan_node, list);
2360 if (sa->type != UBIFS_DATA_NODE) {
2361 ubifs_err(c, "bad node type %d", sa->type);
2362 ubifs_dump_node(c, sa->node);
2365 if (sb->type != UBIFS_DATA_NODE) {
2366 ubifs_err(c, "bad node type %d", sb->type);
2367 ubifs_dump_node(c, sb->node);
2371 inuma = key_inum(c, &sa->key);
2372 inumb = key_inum(c, &sb->key);
2376 if (inuma > inumb) {
2377 ubifs_err(c, "larger inum %lu goes before inum %lu",
2378 (unsigned long)inuma, (unsigned long)inumb);
2382 blka = key_block(c, &sa->key);
2383 blkb = key_block(c, &sb->key);
2386 ubifs_err(c, "larger block %u goes before %u", blka, blkb);
2390 ubifs_err(c, "two data nodes for the same block");
2398 ubifs_dump_node(c, sa->node);
2399 ubifs_dump_node(c, sb->node);
2404 * dbg_check_nondata_nodes_order - check that list of data nodes is sorted.
2405 * @c: UBIFS file-system description object
2406 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2408 * This function returns zero if the list of non-data nodes is sorted correctly,
2409 * and %-EINVAL if not.
2411 int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head)
2413 struct list_head *cur;
2414 struct ubifs_scan_node *sa, *sb;
2416 if (!dbg_is_chk_gen(c))
2419 for (cur = head->next; cur->next != head; cur = cur->next) {
2421 uint32_t hasha, hashb;
2424 sa = container_of(cur, struct ubifs_scan_node, list);
2425 sb = container_of(cur->next, struct ubifs_scan_node, list);
2427 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2428 sa->type != UBIFS_XENT_NODE) {
2429 ubifs_err(c, "bad node type %d", sa->type);
2430 ubifs_dump_node(c, sa->node);
2433 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2434 sa->type != UBIFS_XENT_NODE) {
2435 ubifs_err(c, "bad node type %d", sb->type);
2436 ubifs_dump_node(c, sb->node);
2440 if (sa->type != UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2441 ubifs_err(c, "non-inode node goes before inode node");
2445 if (sa->type == UBIFS_INO_NODE && sb->type != UBIFS_INO_NODE)
2448 if (sa->type == UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2449 /* Inode nodes are sorted in descending size order */
2450 if (sa->len < sb->len) {
2451 ubifs_err(c, "smaller inode node goes first");
2458 * This is either a dentry or xentry, which should be sorted in
2459 * ascending (parent ino, hash) order.
2461 inuma = key_inum(c, &sa->key);
2462 inumb = key_inum(c, &sb->key);
2466 if (inuma > inumb) {
2467 ubifs_err(c, "larger inum %lu goes before inum %lu",
2468 (unsigned long)inuma, (unsigned long)inumb);
2472 hasha = key_block(c, &sa->key);
2473 hashb = key_block(c, &sb->key);
2475 if (hasha > hashb) {
2476 ubifs_err(c, "larger hash %u goes before %u",
2485 ubifs_msg(c, "dumping first node");
2486 ubifs_dump_node(c, sa->node);
2487 ubifs_msg(c, "dumping second node");
2488 ubifs_dump_node(c, sb->node);
2493 static inline int chance(unsigned int n, unsigned int out_of)
2495 return !!((prandom_u32() % out_of) + 1 <= n);
2499 static int power_cut_emulated(struct ubifs_info *c, int lnum, int write)
2501 struct ubifs_debug_info *d = c->dbg;
2503 ubifs_assert(dbg_is_tst_rcvry(c));
2506 /* First call - decide delay to the power cut */
2508 unsigned long delay;
2512 /* Fail within 1 minute */
2513 delay = prandom_u32() % 60000;
2514 d->pc_timeout = jiffies;
2515 d->pc_timeout += msecs_to_jiffies(delay);
2516 ubifs_warn(c, "failing after %lums", delay);
2519 delay = prandom_u32() % 10000;
2520 /* Fail within 10000 operations */
2521 d->pc_cnt_max = delay;
2522 ubifs_warn(c, "failing after %lu calls", delay);
2529 /* Determine if failure delay has expired */
2530 if (d->pc_delay == 1 && time_before(jiffies, d->pc_timeout))
2532 if (d->pc_delay == 2 && d->pc_cnt++ < d->pc_cnt_max)
2535 if (lnum == UBIFS_SB_LNUM) {
2536 if (write && chance(1, 2))
2540 ubifs_warn(c, "failing in super block LEB %d", lnum);
2541 } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2544 ubifs_warn(c, "failing in master LEB %d", lnum);
2545 } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2546 if (write && chance(99, 100))
2548 if (chance(399, 400))
2550 ubifs_warn(c, "failing in log LEB %d", lnum);
2551 } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2552 if (write && chance(7, 8))
2556 ubifs_warn(c, "failing in LPT LEB %d", lnum);
2557 } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2558 if (write && chance(1, 2))
2562 ubifs_warn(c, "failing in orphan LEB %d", lnum);
2563 } else if (lnum == c->ihead_lnum) {
2564 if (chance(99, 100))
2566 ubifs_warn(c, "failing in index head LEB %d", lnum);
2567 } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2570 ubifs_warn(c, "failing in GC head LEB %d", lnum);
2571 } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
2572 !ubifs_search_bud(c, lnum)) {
2575 ubifs_warn(c, "failing in non-bud LEB %d", lnum);
2576 } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
2577 c->cmt_state == COMMIT_RUNNING_REQUIRED) {
2578 if (chance(999, 1000))
2580 ubifs_warn(c, "failing in bud LEB %d commit running", lnum);
2582 if (chance(9999, 10000))
2584 ubifs_warn(c, "failing in bud LEB %d commit not running", lnum);
2588 ubifs_warn(c, "========== Power cut emulated ==========");
2593 static int corrupt_data(const struct ubifs_info *c, const void *buf,
2596 unsigned int from, to, ffs = chance(1, 2);
2597 unsigned char *p = (void *)buf;
2599 from = prandom_u32() % len;
2600 /* Corruption span max to end of write unit */
2601 to = min(len, ALIGN(from + 1, c->max_write_size));
2603 ubifs_warn(c, "filled bytes %u-%u with %s", from, to - 1,
2604 ffs ? "0xFFs" : "random data");
2607 memset(p + from, 0xFF, to - from);
2609 prandom_bytes(p + from, to - from);
2614 int dbg_leb_write(struct ubifs_info *c, int lnum, const void *buf,
2619 if (c->dbg->pc_happened)
2622 failing = power_cut_emulated(c, lnum, 1);
2624 len = corrupt_data(c, buf, len);
2625 ubifs_warn(c, "actually write %d bytes to LEB %d:%d (the buffer was corrupted)",
2628 err = ubi_leb_write(c->ubi, lnum, buf, offs, len);
2636 int dbg_leb_change(struct ubifs_info *c, int lnum, const void *buf,
2641 if (c->dbg->pc_happened)
2643 if (power_cut_emulated(c, lnum, 1))
2645 err = ubi_leb_change(c->ubi, lnum, buf, len);
2648 if (power_cut_emulated(c, lnum, 1))
2653 int dbg_leb_unmap(struct ubifs_info *c, int lnum)
2657 if (c->dbg->pc_happened)
2659 if (power_cut_emulated(c, lnum, 0))
2661 err = ubi_leb_unmap(c->ubi, lnum);
2664 if (power_cut_emulated(c, lnum, 0))
2669 int dbg_leb_map(struct ubifs_info *c, int lnum)
2673 if (c->dbg->pc_happened)
2675 if (power_cut_emulated(c, lnum, 0))
2677 err = ubi_leb_map(c->ubi, lnum);
2680 if (power_cut_emulated(c, lnum, 0))
2686 * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
2687 * contain the stuff specific to particular file-system mounts.
2689 static struct dentry *dfs_rootdir;
2691 static int dfs_file_open(struct inode *inode, struct file *file)
2693 file->private_data = inode->i_private;
2694 return nonseekable_open(inode, file);
2698 * provide_user_output - provide output to the user reading a debugfs file.
2699 * @val: boolean value for the answer
2700 * @u: the buffer to store the answer at
2701 * @count: size of the buffer
2702 * @ppos: position in the @u output buffer
2704 * This is a simple helper function which stores @val boolean value in the user
2705 * buffer when the user reads one of UBIFS debugfs files. Returns amount of
2706 * bytes written to @u in case of success and a negative error code in case of
2709 static int provide_user_output(int val, char __user *u, size_t count,
2721 return simple_read_from_buffer(u, count, ppos, buf, 2);
2724 static ssize_t dfs_file_read(struct file *file, char __user *u, size_t count,
2727 struct dentry *dent = file->f_path.dentry;
2728 struct ubifs_info *c = file->private_data;
2729 struct ubifs_debug_info *d = c->dbg;
2732 if (dent == d->dfs_chk_gen)
2734 else if (dent == d->dfs_chk_index)
2736 else if (dent == d->dfs_chk_orph)
2738 else if (dent == d->dfs_chk_lprops)
2739 val = d->chk_lprops;
2740 else if (dent == d->dfs_chk_fs)
2742 else if (dent == d->dfs_tst_rcvry)
2744 else if (dent == d->dfs_ro_error)
2749 return provide_user_output(val, u, count, ppos);
2753 * interpret_user_input - interpret user debugfs file input.
2754 * @u: user-provided buffer with the input
2755 * @count: buffer size
2757 * This is a helper function which interpret user input to a boolean UBIFS
2758 * debugfs file. Returns %0 or %1 in case of success and a negative error code
2759 * in case of failure.
2761 static int interpret_user_input(const char __user *u, size_t count)
2766 buf_size = min_t(size_t, count, (sizeof(buf) - 1));
2767 if (copy_from_user(buf, u, buf_size))
2772 else if (buf[0] == '0')
2778 static ssize_t dfs_file_write(struct file *file, const char __user *u,
2779 size_t count, loff_t *ppos)
2781 struct ubifs_info *c = file->private_data;
2782 struct ubifs_debug_info *d = c->dbg;
2783 struct dentry *dent = file->f_path.dentry;
2787 * TODO: this is racy - the file-system might have already been
2788 * unmounted and we'd oops in this case. The plan is to fix it with
2789 * help of 'iterate_supers_type()' which we should have in v3.0: when
2790 * a debugfs opened, we rember FS's UUID in file->private_data. Then
2791 * whenever we access the FS via a debugfs file, we iterate all UBIFS
2792 * superblocks and fine the one with the same UUID, and take the
2795 * The other way to go suggested by Al Viro is to create a separate
2796 * 'ubifs-debug' file-system instead.
2798 if (file->f_path.dentry == d->dfs_dump_lprops) {
2799 ubifs_dump_lprops(c);
2802 if (file->f_path.dentry == d->dfs_dump_budg) {
2803 ubifs_dump_budg(c, &c->bi);
2806 if (file->f_path.dentry == d->dfs_dump_tnc) {
2807 mutex_lock(&c->tnc_mutex);
2809 mutex_unlock(&c->tnc_mutex);
2813 val = interpret_user_input(u, count);
2817 if (dent == d->dfs_chk_gen)
2819 else if (dent == d->dfs_chk_index)
2821 else if (dent == d->dfs_chk_orph)
2823 else if (dent == d->dfs_chk_lprops)
2824 d->chk_lprops = val;
2825 else if (dent == d->dfs_chk_fs)
2827 else if (dent == d->dfs_tst_rcvry)
2829 else if (dent == d->dfs_ro_error)
2830 c->ro_error = !!val;
2837 static const struct file_operations dfs_fops = {
2838 .open = dfs_file_open,
2839 .read = dfs_file_read,
2840 .write = dfs_file_write,
2841 .owner = THIS_MODULE,
2842 .llseek = no_llseek,
2846 * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
2847 * @c: UBIFS file-system description object
2849 * This function creates all debugfs files for this instance of UBIFS. Returns
2850 * zero in case of success and a negative error code in case of failure.
2852 * Note, the only reason we have not merged this function with the
2853 * 'ubifs_debugging_init()' function is because it is better to initialize
2854 * debugfs interfaces at the very end of the mount process, and remove them at
2855 * the very beginning of the mount process.
2857 int dbg_debugfs_init_fs(struct ubifs_info *c)
2861 struct dentry *dent;
2862 struct ubifs_debug_info *d = c->dbg;
2864 if (!IS_ENABLED(CONFIG_DEBUG_FS))
2867 n = snprintf(d->dfs_dir_name, UBIFS_DFS_DIR_LEN + 1, UBIFS_DFS_DIR_NAME,
2868 c->vi.ubi_num, c->vi.vol_id);
2869 if (n == UBIFS_DFS_DIR_LEN) {
2870 /* The array size is too small */
2871 fname = UBIFS_DFS_DIR_NAME;
2872 dent = ERR_PTR(-EINVAL);
2876 fname = d->dfs_dir_name;
2877 dent = debugfs_create_dir(fname, dfs_rootdir);
2878 if (IS_ERR_OR_NULL(dent))
2882 fname = "dump_lprops";
2883 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2884 if (IS_ERR_OR_NULL(dent))
2886 d->dfs_dump_lprops = dent;
2888 fname = "dump_budg";
2889 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2890 if (IS_ERR_OR_NULL(dent))
2892 d->dfs_dump_budg = dent;
2895 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2896 if (IS_ERR_OR_NULL(dent))
2898 d->dfs_dump_tnc = dent;
2900 fname = "chk_general";
2901 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2903 if (IS_ERR_OR_NULL(dent))
2905 d->dfs_chk_gen = dent;
2907 fname = "chk_index";
2908 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2910 if (IS_ERR_OR_NULL(dent))
2912 d->dfs_chk_index = dent;
2914 fname = "chk_orphans";
2915 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2917 if (IS_ERR_OR_NULL(dent))
2919 d->dfs_chk_orph = dent;
2921 fname = "chk_lprops";
2922 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2924 if (IS_ERR_OR_NULL(dent))
2926 d->dfs_chk_lprops = dent;
2929 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2931 if (IS_ERR_OR_NULL(dent))
2933 d->dfs_chk_fs = dent;
2935 fname = "tst_recovery";
2936 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2938 if (IS_ERR_OR_NULL(dent))
2940 d->dfs_tst_rcvry = dent;
2943 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2945 if (IS_ERR_OR_NULL(dent))
2947 d->dfs_ro_error = dent;
2952 debugfs_remove_recursive(d->dfs_dir);
2954 err = dent ? PTR_ERR(dent) : -ENODEV;
2955 ubifs_err(c, "cannot create \"%s\" debugfs file or directory, error %d\n",
2961 * dbg_debugfs_exit_fs - remove all debugfs files.
2962 * @c: UBIFS file-system description object
2964 void dbg_debugfs_exit_fs(struct ubifs_info *c)
2966 if (IS_ENABLED(CONFIG_DEBUG_FS))
2967 debugfs_remove_recursive(c->dbg->dfs_dir);
2970 struct ubifs_global_debug_info ubifs_dbg;
2972 static struct dentry *dfs_chk_gen;
2973 static struct dentry *dfs_chk_index;
2974 static struct dentry *dfs_chk_orph;
2975 static struct dentry *dfs_chk_lprops;
2976 static struct dentry *dfs_chk_fs;
2977 static struct dentry *dfs_tst_rcvry;
2979 static ssize_t dfs_global_file_read(struct file *file, char __user *u,
2980 size_t count, loff_t *ppos)
2982 struct dentry *dent = file->f_path.dentry;
2985 if (dent == dfs_chk_gen)
2986 val = ubifs_dbg.chk_gen;
2987 else if (dent == dfs_chk_index)
2988 val = ubifs_dbg.chk_index;
2989 else if (dent == dfs_chk_orph)
2990 val = ubifs_dbg.chk_orph;
2991 else if (dent == dfs_chk_lprops)
2992 val = ubifs_dbg.chk_lprops;
2993 else if (dent == dfs_chk_fs)
2994 val = ubifs_dbg.chk_fs;
2995 else if (dent == dfs_tst_rcvry)
2996 val = ubifs_dbg.tst_rcvry;
3000 return provide_user_output(val, u, count, ppos);
3003 static ssize_t dfs_global_file_write(struct file *file, const char __user *u,
3004 size_t count, loff_t *ppos)
3006 struct dentry *dent = file->f_path.dentry;
3009 val = interpret_user_input(u, count);
3013 if (dent == dfs_chk_gen)
3014 ubifs_dbg.chk_gen = val;
3015 else if (dent == dfs_chk_index)
3016 ubifs_dbg.chk_index = val;
3017 else if (dent == dfs_chk_orph)
3018 ubifs_dbg.chk_orph = val;
3019 else if (dent == dfs_chk_lprops)
3020 ubifs_dbg.chk_lprops = val;
3021 else if (dent == dfs_chk_fs)
3022 ubifs_dbg.chk_fs = val;
3023 else if (dent == dfs_tst_rcvry)
3024 ubifs_dbg.tst_rcvry = val;
3031 static const struct file_operations dfs_global_fops = {
3032 .read = dfs_global_file_read,
3033 .write = dfs_global_file_write,
3034 .owner = THIS_MODULE,
3035 .llseek = no_llseek,
3039 * dbg_debugfs_init - initialize debugfs file-system.
3041 * UBIFS uses debugfs file-system to expose various debugging knobs to
3042 * user-space. This function creates "ubifs" directory in the debugfs
3043 * file-system. Returns zero in case of success and a negative error code in
3046 int dbg_debugfs_init(void)
3050 struct dentry *dent;
3052 if (!IS_ENABLED(CONFIG_DEBUG_FS))
3056 dent = debugfs_create_dir(fname, NULL);
3057 if (IS_ERR_OR_NULL(dent))
3061 fname = "chk_general";
3062 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3064 if (IS_ERR_OR_NULL(dent))
3068 fname = "chk_index";
3069 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3071 if (IS_ERR_OR_NULL(dent))
3073 dfs_chk_index = dent;
3075 fname = "chk_orphans";
3076 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3078 if (IS_ERR_OR_NULL(dent))
3080 dfs_chk_orph = dent;
3082 fname = "chk_lprops";
3083 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3085 if (IS_ERR_OR_NULL(dent))
3087 dfs_chk_lprops = dent;
3090 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3092 if (IS_ERR_OR_NULL(dent))
3096 fname = "tst_recovery";
3097 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3099 if (IS_ERR_OR_NULL(dent))
3101 dfs_tst_rcvry = dent;
3106 debugfs_remove_recursive(dfs_rootdir);
3108 err = dent ? PTR_ERR(dent) : -ENODEV;
3109 pr_err("UBIFS error (pid %d): cannot create \"%s\" debugfs file or directory, error %d\n",
3110 current->pid, fname, err);
3115 * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
3117 void dbg_debugfs_exit(void)
3119 if (IS_ENABLED(CONFIG_DEBUG_FS))
3120 debugfs_remove_recursive(dfs_rootdir);
3124 * ubifs_debugging_init - initialize UBIFS debugging.
3125 * @c: UBIFS file-system description object
3127 * This function initializes debugging-related data for the file system.
3128 * Returns zero in case of success and a negative error code in case of
3131 int ubifs_debugging_init(struct ubifs_info *c)
3133 c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
3141 * ubifs_debugging_exit - free debugging data.
3142 * @c: UBIFS file-system description object
3144 void ubifs_debugging_exit(struct ubifs_info *c)