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.
21 #include <linux/module.h>
22 #include <linux/debugfs.h>
23 #include <linux/math64.h>
24 #include <linux/uaccess.h>
25 #include <linux/random.h>
27 #include <linux/compat.h>
28 #include <linux/err.h>
33 static DEFINE_SPINLOCK(dbg_lock);
36 static const char *get_key_fmt(int fmt)
39 case UBIFS_SIMPLE_KEY_FMT:
42 return "unknown/invalid format";
46 static const char *get_key_hash(int hash)
49 case UBIFS_KEY_HASH_R5:
51 case UBIFS_KEY_HASH_TEST:
54 return "unknown/invalid name hash";
58 static const char *get_key_type(int type)
72 return "unknown/invalid key";
77 static const char *get_dent_type(int type)
90 case UBIFS_ITYPE_FIFO:
92 case UBIFS_ITYPE_SOCK:
95 return "unknown/invalid type";
100 const char *dbg_snprintf_key(const struct ubifs_info *c,
101 const union ubifs_key *key, char *buffer, int len)
104 int type = key_type(c, key);
106 if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
109 len -= snprintf(p, len, "(%lu, %s)",
110 (unsigned long)key_inum(c, key),
115 len -= snprintf(p, len, "(%lu, %s, %#08x)",
116 (unsigned long)key_inum(c, key),
117 get_key_type(type), key_hash(c, key));
120 len -= snprintf(p, len, "(%lu, %s, %u)",
121 (unsigned long)key_inum(c, key),
122 get_key_type(type), key_block(c, key));
125 len -= snprintf(p, len, "(%lu, %s)",
126 (unsigned long)key_inum(c, key),
130 len -= snprintf(p, len, "(bad key type: %#08x, %#08x)",
131 key->u32[0], key->u32[1]);
134 len -= snprintf(p, len, "bad key format %d", c->key_fmt);
135 ubifs_assert(len > 0);
139 const char *dbg_ntype(int type)
143 return "padding node";
145 return "superblock node";
147 return "master node";
149 return "reference node";
152 case UBIFS_DENT_NODE:
153 return "direntry node";
154 case UBIFS_XENT_NODE:
155 return "xentry node";
156 case UBIFS_DATA_NODE:
158 case UBIFS_TRUN_NODE:
159 return "truncate node";
161 return "indexing node";
163 return "commit start node";
164 case UBIFS_ORPH_NODE:
165 return "orphan node";
167 return "unknown node";
171 static const char *dbg_gtype(int type)
174 case UBIFS_NO_NODE_GROUP:
175 return "no node group";
176 case UBIFS_IN_NODE_GROUP:
177 return "in node group";
178 case UBIFS_LAST_OF_NODE_GROUP:
179 return "last of node group";
185 const char *dbg_cstate(int cmt_state)
189 return "commit resting";
190 case COMMIT_BACKGROUND:
191 return "background commit requested";
192 case COMMIT_REQUIRED:
193 return "commit required";
194 case COMMIT_RUNNING_BACKGROUND:
195 return "BACKGROUND commit running";
196 case COMMIT_RUNNING_REQUIRED:
197 return "commit running and required";
199 return "broken commit";
201 return "unknown commit state";
205 const char *dbg_jhead(int jhead)
215 return "unknown journal head";
219 static void dump_ch(const struct ubifs_ch *ch)
221 pr_err("\tmagic %#x\n", le32_to_cpu(ch->magic));
222 pr_err("\tcrc %#x\n", le32_to_cpu(ch->crc));
223 pr_err("\tnode_type %d (%s)\n", ch->node_type,
224 dbg_ntype(ch->node_type));
225 pr_err("\tgroup_type %d (%s)\n", ch->group_type,
226 dbg_gtype(ch->group_type));
227 pr_err("\tsqnum %llu\n",
228 (unsigned long long)le64_to_cpu(ch->sqnum));
229 pr_err("\tlen %u\n", le32_to_cpu(ch->len));
232 void ubifs_dump_inode(struct ubifs_info *c, const struct inode *inode)
235 const struct ubifs_inode *ui = ubifs_inode(inode);
236 struct qstr nm = { .name = NULL };
238 struct ubifs_dent_node *dent, *pdent = NULL;
241 pr_err("Dump in-memory inode:");
242 pr_err("\tinode %lu\n", inode->i_ino);
243 pr_err("\tsize %llu\n",
244 (unsigned long long)i_size_read(inode));
245 pr_err("\tnlink %u\n", inode->i_nlink);
246 pr_err("\tuid %u\n", (unsigned int)i_uid_read(inode));
247 pr_err("\tgid %u\n", (unsigned int)i_gid_read(inode));
248 pr_err("\tatime %u.%u\n",
249 (unsigned int)inode->i_atime.tv_sec,
250 (unsigned int)inode->i_atime.tv_nsec);
251 pr_err("\tmtime %u.%u\n",
252 (unsigned int)inode->i_mtime.tv_sec,
253 (unsigned int)inode->i_mtime.tv_nsec);
254 pr_err("\tctime %u.%u\n",
255 (unsigned int)inode->i_ctime.tv_sec,
256 (unsigned int)inode->i_ctime.tv_nsec);
257 pr_err("\tcreat_sqnum %llu\n", ui->creat_sqnum);
258 pr_err("\txattr_size %u\n", ui->xattr_size);
259 pr_err("\txattr_cnt %u\n", ui->xattr_cnt);
260 pr_err("\txattr_names %u\n", ui->xattr_names);
261 pr_err("\tdirty %u\n", ui->dirty);
262 pr_err("\txattr %u\n", ui->xattr);
263 pr_err("\tbulk_read %u\n", ui->xattr);
264 pr_err("\tsynced_i_size %llu\n",
265 (unsigned long long)ui->synced_i_size);
266 pr_err("\tui_size %llu\n",
267 (unsigned long long)ui->ui_size);
268 pr_err("\tflags %d\n", ui->flags);
269 pr_err("\tcompr_type %d\n", ui->compr_type);
270 pr_err("\tlast_page_read %lu\n", ui->last_page_read);
271 pr_err("\tread_in_a_row %lu\n", ui->read_in_a_row);
272 pr_err("\tdata_len %d\n", ui->data_len);
274 if (!S_ISDIR(inode->i_mode))
277 pr_err("List of directory entries:\n");
278 ubifs_assert(!mutex_is_locked(&c->tnc_mutex));
280 lowest_dent_key(c, &key, inode->i_ino);
282 dent = ubifs_tnc_next_ent(c, &key, &nm);
284 if (PTR_ERR(dent) != -ENOENT)
285 pr_err("error %ld\n", PTR_ERR(dent));
289 pr_err("\t%d: %s (%s)\n",
290 count++, dent->name, get_dent_type(dent->type));
292 nm.name = dent->name;
293 nm.len = le16_to_cpu(dent->nlen);
296 key_read(c, &dent->key, &key);
302 void ubifs_dump_node(const struct ubifs_info *c, const void *node)
306 const struct ubifs_ch *ch = node;
307 char key_buf[DBG_KEY_BUF_LEN];
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);
317 spin_lock(&dbg_lock);
320 switch (ch->node_type) {
323 const struct ubifs_pad_node *pad = node;
325 pr_err("\tpad_len %u\n", le32_to_cpu(pad->pad_len));
330 const struct ubifs_sb_node *sup = node;
331 unsigned int sup_flags = le32_to_cpu(sup->flags);
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("\t big_lpt %u\n",
339 !!(sup_flags & UBIFS_FLG_BIGLPT));
340 pr_err("\t space_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);
367 const struct ubifs_mst_node *mst = node;
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));
409 const struct ubifs_ref_node *ref = node;
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));
418 const struct ubifs_ino_node *ino = node;
420 key_read(c, &ino->key, &key);
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));
449 case UBIFS_DENT_NODE:
450 case UBIFS_XENT_NODE:
452 const struct ubifs_dent_node *dent = node;
453 int nlen = le16_to_cpu(dent->nlen);
455 key_read(c, &dent->key, &key);
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);
464 if (nlen > UBIFS_MAX_NLEN)
465 pr_err("(bad name length, not printing, bad or corrupted node)");
467 for (i = 0; i < nlen && dent->name[i]; i++)
468 pr_cont("%c", dent->name[i]);
474 case UBIFS_DATA_NODE:
476 const struct ubifs_data_node *dn = node;
477 int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
479 key_read(c, &dn->key, &key);
481 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
482 pr_err("\tsize %u\n", le32_to_cpu(dn->size));
483 pr_err("\tcompr_typ %d\n",
484 (int)le16_to_cpu(dn->compr_type));
485 pr_err("\tdata size %d\n", dlen);
487 print_hex_dump(KERN_ERR, "\t", DUMP_PREFIX_OFFSET, 32, 1,
488 (void *)&dn->data, dlen, 0);
491 case UBIFS_TRUN_NODE:
493 const struct ubifs_trun_node *trun = node;
495 pr_err("\tinum %u\n", le32_to_cpu(trun->inum));
496 pr_err("\told_size %llu\n",
497 (unsigned long long)le64_to_cpu(trun->old_size));
498 pr_err("\tnew_size %llu\n",
499 (unsigned long long)le64_to_cpu(trun->new_size));
504 const struct ubifs_idx_node *idx = node;
506 n = le16_to_cpu(idx->child_cnt);
507 pr_err("\tchild_cnt %d\n", n);
508 pr_err("\tlevel %d\n", (int)le16_to_cpu(idx->level));
509 pr_err("\tBranches:\n");
511 for (i = 0; i < n && i < c->fanout - 1; i++) {
512 const struct ubifs_branch *br;
514 br = ubifs_idx_branch(c, idx, i);
515 key_read(c, &br->key, &key);
516 pr_err("\t%d: LEB %d:%d len %d key %s\n",
517 i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
518 le32_to_cpu(br->len),
519 dbg_snprintf_key(c, &key, key_buf,
526 case UBIFS_ORPH_NODE:
528 const struct ubifs_orph_node *orph = node;
530 pr_err("\tcommit number %llu\n",
532 le64_to_cpu(orph->cmt_no) & LLONG_MAX);
533 pr_err("\tlast node flag %llu\n",
534 (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
535 n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
536 pr_err("\t%d orphan inode numbers:\n", n);
537 for (i = 0; i < n; i++)
538 pr_err("\t ino %llu\n",
539 (unsigned long long)le64_to_cpu(orph->inos[i]));
543 pr_err("node type %d was not recognized\n",
546 spin_unlock(&dbg_lock);
549 void ubifs_dump_budget_req(const struct ubifs_budget_req *req)
551 spin_lock(&dbg_lock);
552 pr_err("Budgeting request: new_ino %d, dirtied_ino %d\n",
553 req->new_ino, req->dirtied_ino);
554 pr_err("\tnew_ino_d %d, dirtied_ino_d %d\n",
555 req->new_ino_d, req->dirtied_ino_d);
556 pr_err("\tnew_page %d, dirtied_page %d\n",
557 req->new_page, req->dirtied_page);
558 pr_err("\tnew_dent %d, mod_dent %d\n",
559 req->new_dent, req->mod_dent);
560 pr_err("\tidx_growth %d\n", req->idx_growth);
561 pr_err("\tdata_growth %d dd_growth %d\n",
562 req->data_growth, req->dd_growth);
563 spin_unlock(&dbg_lock);
566 void ubifs_dump_lstats(const struct ubifs_lp_stats *lst)
568 spin_lock(&dbg_lock);
569 pr_err("(pid %d) Lprops statistics: empty_lebs %d, idx_lebs %d\n",
570 current->pid, lst->empty_lebs, lst->idx_lebs);
571 pr_err("\ttaken_empty_lebs %d, total_free %lld, total_dirty %lld\n",
572 lst->taken_empty_lebs, lst->total_free, lst->total_dirty);
573 pr_err("\ttotal_used %lld, total_dark %lld, total_dead %lld\n",
574 lst->total_used, lst->total_dark, lst->total_dead);
575 spin_unlock(&dbg_lock);
579 void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)
583 struct ubifs_bud *bud;
584 struct ubifs_gced_idx_leb *idx_gc;
585 long long available, outstanding, free;
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);
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.
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);
618 /* If we are in R/O mode, journal heads do not exist */
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);
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);
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);
643 spin_unlock(&dbg_lock);
644 spin_unlock(&c->space_lock);
647 void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)
652 void ubifs_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
654 int i, spc, dark = 0, dead = 0;
656 struct ubifs_bud *bud;
658 spc = lp->free + lp->dirty;
659 if (spc < c->dead_wm)
662 dark = ubifs_calc_dark(c, spc);
664 if (lp->flags & LPROPS_INDEX)
665 pr_err("LEB %-7d free %-8d dirty %-8d used %-8d free + dirty %-8d flags %#x (",
666 lp->lnum, lp->free, lp->dirty, c->leb_size - spc, spc,
669 pr_err("LEB %-7d free %-8d dirty %-8d used %-8d free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d flags %#-4x (",
670 lp->lnum, lp->free, lp->dirty, c->leb_size - spc, spc,
671 dark, dead, (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags);
673 if (lp->flags & LPROPS_TAKEN) {
674 if (lp->flags & LPROPS_INDEX)
675 pr_cont("index, taken");
681 if (lp->flags & LPROPS_INDEX) {
682 switch (lp->flags & LPROPS_CAT_MASK) {
683 case LPROPS_DIRTY_IDX:
686 case LPROPS_FRDI_IDX:
687 s = "freeable index";
693 switch (lp->flags & LPROPS_CAT_MASK) {
695 s = "not categorized";
706 case LPROPS_FREEABLE:
717 for (rb = rb_first((struct rb_root *)&c->buds); rb; rb = rb_next(rb)) {
718 bud = rb_entry(rb, struct ubifs_bud, rb);
719 if (bud->lnum == lp->lnum) {
721 for (i = 0; i < c->jhead_cnt; i++) {
723 * Note, if we are in R/O mode or in the middle
724 * of mounting/re-mounting, the write-buffers do
728 lp->lnum == c->jheads[i].wbuf.lnum) {
729 pr_cont(", jhead %s", dbg_jhead(i));
734 pr_cont(", bud of jhead %s",
735 dbg_jhead(bud->jhead));
738 if (lp->lnum == c->gc_lnum)
743 void ubifs_dump_lprops(struct ubifs_info *c)
746 struct ubifs_lprops lp;
747 struct ubifs_lp_stats lst;
749 pr_err("(pid %d) start dumping LEB properties\n", current->pid);
750 ubifs_get_lp_stats(c, &lst);
751 ubifs_dump_lstats(&lst);
753 for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
754 err = ubifs_read_one_lp(c, lnum, &lp);
756 ubifs_err("cannot read lprops for LEB %d", lnum);
758 ubifs_dump_lprop(c, &lp);
760 pr_err("(pid %d) finish dumping LEB properties\n", current->pid);
763 void ubifs_dump_lpt_info(struct ubifs_info *c)
767 spin_lock(&dbg_lock);
768 pr_err("(pid %d) dumping LPT information\n", current->pid);
769 pr_err("\tlpt_sz: %lld\n", c->lpt_sz);
770 pr_err("\tpnode_sz: %d\n", c->pnode_sz);
771 pr_err("\tnnode_sz: %d\n", c->nnode_sz);
772 pr_err("\tltab_sz: %d\n", c->ltab_sz);
773 pr_err("\tlsave_sz: %d\n", c->lsave_sz);
774 pr_err("\tbig_lpt: %d\n", c->big_lpt);
775 pr_err("\tlpt_hght: %d\n", c->lpt_hght);
776 pr_err("\tpnode_cnt: %d\n", c->pnode_cnt);
777 pr_err("\tnnode_cnt: %d\n", c->nnode_cnt);
778 pr_err("\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt);
779 pr_err("\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt);
780 pr_err("\tlsave_cnt: %d\n", c->lsave_cnt);
781 pr_err("\tspace_bits: %d\n", c->space_bits);
782 pr_err("\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
783 pr_err("\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
784 pr_err("\tlpt_spc_bits: %d\n", c->lpt_spc_bits);
785 pr_err("\tpcnt_bits: %d\n", c->pcnt_bits);
786 pr_err("\tlnum_bits: %d\n", c->lnum_bits);
787 pr_err("\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
788 pr_err("\tLPT head is at %d:%d\n",
789 c->nhead_lnum, c->nhead_offs);
790 pr_err("\tLPT ltab is at %d:%d\n", c->ltab_lnum, c->ltab_offs);
792 pr_err("\tLPT lsave is at %d:%d\n",
793 c->lsave_lnum, c->lsave_offs);
794 for (i = 0; i < c->lpt_lebs; i++)
795 pr_err("\tLPT LEB %d free %d dirty %d tgc %d cmt %d\n",
796 i + c->lpt_first, c->ltab[i].free, c->ltab[i].dirty,
797 c->ltab[i].tgc, c->ltab[i].cmt);
798 spin_unlock(&dbg_lock);
801 void ubifs_dump_sleb(const struct ubifs_info *c,
802 const struct ubifs_scan_leb *sleb, int offs)
804 struct ubifs_scan_node *snod;
806 pr_err("(pid %d) start dumping scanned data from LEB %d:%d\n",
807 current->pid, sleb->lnum, offs);
809 list_for_each_entry(snod, &sleb->nodes, list) {
811 pr_err("Dumping node at LEB %d:%d len %d\n",
812 sleb->lnum, snod->offs, snod->len);
813 ubifs_dump_node(c, snod->node);
817 void ubifs_dump_leb(const struct ubifs_info *c, int lnum)
819 struct ubifs_scan_leb *sleb;
820 struct ubifs_scan_node *snod;
823 pr_err("(pid %d) start dumping LEB %d\n", current->pid, lnum);
825 buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
827 ubifs_err("cannot allocate memory for dumping LEB %d", lnum);
831 sleb = ubifs_scan(c, lnum, 0, buf, 0);
833 ubifs_err("scan error %d", (int)PTR_ERR(sleb));
837 pr_err("LEB %d has %d nodes ending at %d\n", lnum,
838 sleb->nodes_cnt, sleb->endpt);
840 list_for_each_entry(snod, &sleb->nodes, list) {
842 pr_err("Dumping node at LEB %d:%d len %d\n", lnum,
843 snod->offs, snod->len);
844 ubifs_dump_node(c, snod->node);
847 pr_err("(pid %d) finish dumping LEB %d\n", current->pid, lnum);
848 ubifs_scan_destroy(sleb);
855 void ubifs_dump_znode(const struct ubifs_info *c,
856 const struct ubifs_znode *znode)
859 const struct ubifs_zbranch *zbr;
860 char key_buf[DBG_KEY_BUF_LEN];
862 spin_lock(&dbg_lock);
864 zbr = &znode->parent->zbranch[znode->iip];
868 pr_err("znode %p, LEB %d:%d len %d parent %p iip %d level %d child_cnt %d flags %lx\n",
869 znode, zbr->lnum, zbr->offs, zbr->len, znode->parent, znode->iip,
870 znode->level, znode->child_cnt, znode->flags);
872 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
873 spin_unlock(&dbg_lock);
877 pr_err("zbranches:\n");
878 for (n = 0; n < znode->child_cnt; n++) {
879 zbr = &znode->zbranch[n];
880 if (znode->level > 0)
881 pr_err("\t%d: znode %p LEB %d:%d len %d key %s\n",
882 n, zbr->znode, zbr->lnum, zbr->offs, zbr->len,
883 dbg_snprintf_key(c, &zbr->key, key_buf,
886 pr_err("\t%d: LNC %p LEB %d:%d len %d key %s\n",
887 n, zbr->znode, zbr->lnum, zbr->offs, zbr->len,
888 dbg_snprintf_key(c, &zbr->key, key_buf,
891 spin_unlock(&dbg_lock);
894 void ubifs_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
898 pr_err("(pid %d) start dumping heap cat %d (%d elements)\n",
899 current->pid, cat, heap->cnt);
900 for (i = 0; i < heap->cnt; i++) {
901 struct ubifs_lprops *lprops = heap->arr[i];
903 pr_err("\t%d. LEB %d hpos %d free %d dirty %d flags %d\n",
904 i, lprops->lnum, lprops->hpos, lprops->free,
905 lprops->dirty, lprops->flags);
907 pr_err("(pid %d) finish dumping heap\n", current->pid);
910 void ubifs_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
911 struct ubifs_nnode *parent, int iip)
915 pr_err("(pid %d) dumping pnode:\n", current->pid);
916 pr_err("\taddress %zx parent %zx cnext %zx\n",
917 (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
918 pr_err("\tflags %lu iip %d level %d num %d\n",
919 pnode->flags, iip, pnode->level, pnode->num);
920 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
921 struct ubifs_lprops *lp = &pnode->lprops[i];
923 pr_err("\t%d: free %d dirty %d flags %d lnum %d\n",
924 i, lp->free, lp->dirty, lp->flags, lp->lnum);
928 void ubifs_dump_tnc(struct ubifs_info *c)
930 struct ubifs_znode *znode;
934 pr_err("(pid %d) start dumping TNC tree\n", current->pid);
935 znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
936 level = znode->level;
937 pr_err("== Level %d ==\n", level);
939 if (level != znode->level) {
940 level = znode->level;
941 pr_err("== Level %d ==\n", level);
943 ubifs_dump_znode(c, znode);
944 znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
946 pr_err("(pid %d) finish dumping TNC tree\n", current->pid);
949 static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
952 ubifs_dump_znode(c, znode);
957 * ubifs_dump_index - dump the on-flash index.
958 * @c: UBIFS file-system description object
960 * This function dumps whole UBIFS indexing B-tree, unlike 'ubifs_dump_tnc()'
961 * which dumps only in-memory znodes and does not read znodes which from flash.
963 void ubifs_dump_index(struct ubifs_info *c)
965 dbg_walk_index(c, NULL, dump_znode, NULL);
970 * dbg_save_space_info - save information about flash space.
971 * @c: UBIFS file-system description object
973 * This function saves information about UBIFS free space, dirty space, etc, in
974 * order to check it later.
976 void dbg_save_space_info(struct ubifs_info *c)
978 struct ubifs_debug_info *d = c->dbg;
981 spin_lock(&c->space_lock);
982 memcpy(&d->saved_lst, &c->lst, sizeof(struct ubifs_lp_stats));
983 memcpy(&d->saved_bi, &c->bi, sizeof(struct ubifs_budg_info));
984 d->saved_idx_gc_cnt = c->idx_gc_cnt;
987 * We use a dirty hack here and zero out @c->freeable_cnt, because it
988 * affects the free space calculations, and UBIFS might not know about
989 * all freeable eraseblocks. Indeed, we know about freeable eraseblocks
990 * only when we read their lprops, and we do this only lazily, upon the
991 * need. So at any given point of time @c->freeable_cnt might be not
994 * Just one example about the issue we hit when we did not zero
996 * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the
997 * amount of free space in @d->saved_free
998 * 2. We re-mount R/W, which makes UBIFS to read the "lsave"
999 * information from flash, where we cache LEBs from various
1000 * categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()'
1001 * -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()'
1002 * -> 'ubifs_get_pnode()' -> 'update_cats()'
1003 * -> 'ubifs_add_to_cat()').
1004 * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt
1006 * 4. We calculate the amount of free space when the re-mount is
1007 * finished in 'dbg_check_space_info()' and it does not match
1010 freeable_cnt = c->freeable_cnt;
1011 c->freeable_cnt = 0;
1012 d->saved_free = ubifs_get_free_space_nolock(c);
1013 c->freeable_cnt = freeable_cnt;
1014 spin_unlock(&c->space_lock);
1018 * dbg_check_space_info - check flash space information.
1019 * @c: UBIFS file-system description object
1021 * This function compares current flash space information with the information
1022 * which was saved when the 'dbg_save_space_info()' function was called.
1023 * Returns zero if the information has not changed, and %-EINVAL it it has
1026 int dbg_check_space_info(struct ubifs_info *c)
1028 struct ubifs_debug_info *d = c->dbg;
1029 struct ubifs_lp_stats lst;
1033 spin_lock(&c->space_lock);
1034 freeable_cnt = c->freeable_cnt;
1035 c->freeable_cnt = 0;
1036 free = ubifs_get_free_space_nolock(c);
1037 c->freeable_cnt = freeable_cnt;
1038 spin_unlock(&c->space_lock);
1040 if (free != d->saved_free) {
1041 ubifs_err("free space changed from %lld to %lld",
1042 d->saved_free, free);
1049 ubifs_msg("saved lprops statistics dump");
1050 ubifs_dump_lstats(&d->saved_lst);
1051 ubifs_msg("saved budgeting info dump");
1052 ubifs_dump_budg(c, &d->saved_bi);
1053 ubifs_msg("saved idx_gc_cnt %d", d->saved_idx_gc_cnt);
1054 ubifs_msg("current lprops statistics dump");
1055 ubifs_get_lp_stats(c, &lst);
1056 ubifs_dump_lstats(&lst);
1057 ubifs_msg("current budgeting info dump");
1058 ubifs_dump_budg(c, &c->bi);
1064 * dbg_check_synced_i_size - check synchronized inode size.
1065 * @c: UBIFS file-system description object
1066 * @inode: inode to check
1068 * If inode is clean, synchronized inode size has to be equivalent to current
1069 * inode size. This function has to be called only for locked inodes (@i_mutex
1070 * has to be locked). Returns %0 if synchronized inode size if correct, and
1073 int dbg_check_synced_i_size(const struct ubifs_info *c, struct inode *inode)
1076 struct ubifs_inode *ui = ubifs_inode(inode);
1078 if (!dbg_is_chk_gen(c))
1080 if (!S_ISREG(inode->i_mode))
1083 mutex_lock(&ui->ui_mutex);
1084 spin_lock(&ui->ui_lock);
1085 if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
1086 ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode is clean",
1087 ui->ui_size, ui->synced_i_size);
1088 ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
1089 inode->i_mode, i_size_read(inode));
1093 spin_unlock(&ui->ui_lock);
1094 mutex_unlock(&ui->ui_mutex);
1099 * dbg_check_dir - check directory inode size and link count.
1100 * @c: UBIFS file-system description object
1101 * @dir: the directory to calculate size for
1102 * @size: the result is returned here
1104 * This function makes sure that directory size and link count are correct.
1105 * Returns zero in case of success and a negative error code in case of
1108 * Note, it is good idea to make sure the @dir->i_mutex is locked before
1109 * calling this function.
1111 int dbg_check_dir(struct ubifs_info *c, const struct inode *dir)
1113 unsigned int nlink = 2;
1114 union ubifs_key key;
1115 struct ubifs_dent_node *dent, *pdent = NULL;
1116 struct qstr nm = { .name = NULL };
1117 loff_t size = UBIFS_INO_NODE_SZ;
1119 if (!dbg_is_chk_gen(c))
1122 if (!S_ISDIR(dir->i_mode))
1125 lowest_dent_key(c, &key, dir->i_ino);
1129 dent = ubifs_tnc_next_ent(c, &key, &nm);
1131 err = PTR_ERR(dent);
1137 nm.name = dent->name;
1138 nm.len = le16_to_cpu(dent->nlen);
1139 size += CALC_DENT_SIZE(nm.len);
1140 if (dent->type == UBIFS_ITYPE_DIR)
1144 key_read(c, &dent->key, &key);
1148 if (i_size_read(dir) != size) {
1149 ubifs_err("directory inode %lu has size %llu, but calculated size is %llu",
1150 dir->i_ino, (unsigned long long)i_size_read(dir),
1151 (unsigned long long)size);
1152 ubifs_dump_inode(c, dir);
1156 if (dir->i_nlink != nlink) {
1157 ubifs_err("directory inode %lu has nlink %u, but calculated nlink is %u",
1158 dir->i_ino, dir->i_nlink, nlink);
1159 ubifs_dump_inode(c, dir);
1168 * dbg_check_key_order - make sure that colliding keys are properly ordered.
1169 * @c: UBIFS file-system description object
1170 * @zbr1: first zbranch
1171 * @zbr2: following zbranch
1173 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
1174 * names of the direntries/xentries which are referred by the keys. This
1175 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
1176 * sure the name of direntry/xentry referred by @zbr1 is less than
1177 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
1178 * and a negative error code in case of failure.
1180 static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
1181 struct ubifs_zbranch *zbr2)
1183 int err, nlen1, nlen2, cmp;
1184 struct ubifs_dent_node *dent1, *dent2;
1185 union ubifs_key key;
1186 char key_buf[DBG_KEY_BUF_LEN];
1188 ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
1189 dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1192 dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1198 err = ubifs_tnc_read_node(c, zbr1, dent1);
1201 err = ubifs_validate_entry(c, dent1);
1205 err = ubifs_tnc_read_node(c, zbr2, dent2);
1208 err = ubifs_validate_entry(c, dent2);
1212 /* Make sure node keys are the same as in zbranch */
1214 key_read(c, &dent1->key, &key);
1215 if (keys_cmp(c, &zbr1->key, &key)) {
1216 ubifs_err("1st entry at %d:%d has key %s", zbr1->lnum,
1217 zbr1->offs, dbg_snprintf_key(c, &key, key_buf,
1219 ubifs_err("but it should have key %s according to tnc",
1220 dbg_snprintf_key(c, &zbr1->key, key_buf,
1222 ubifs_dump_node(c, dent1);
1226 key_read(c, &dent2->key, &key);
1227 if (keys_cmp(c, &zbr2->key, &key)) {
1228 ubifs_err("2nd entry at %d:%d has key %s", zbr1->lnum,
1229 zbr1->offs, dbg_snprintf_key(c, &key, key_buf,
1231 ubifs_err("but it should have key %s according to tnc",
1232 dbg_snprintf_key(c, &zbr2->key, key_buf,
1234 ubifs_dump_node(c, dent2);
1238 nlen1 = le16_to_cpu(dent1->nlen);
1239 nlen2 = le16_to_cpu(dent2->nlen);
1241 cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
1242 if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
1246 if (cmp == 0 && nlen1 == nlen2)
1247 ubifs_err("2 xent/dent nodes with the same name");
1249 ubifs_err("bad order of colliding key %s",
1250 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
1252 ubifs_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs);
1253 ubifs_dump_node(c, dent1);
1254 ubifs_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs);
1255 ubifs_dump_node(c, dent2);
1264 * dbg_check_znode - check if znode is all right.
1265 * @c: UBIFS file-system description object
1266 * @zbr: zbranch which points to this znode
1268 * This function makes sure that znode referred to by @zbr is all right.
1269 * Returns zero if it is, and %-EINVAL if it is not.
1271 static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
1273 struct ubifs_znode *znode = zbr->znode;
1274 struct ubifs_znode *zp = znode->parent;
1277 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
1281 if (znode->level < 0) {
1285 if (znode->iip < 0 || znode->iip >= c->fanout) {
1291 /* Only dirty zbranch may have no on-flash nodes */
1292 if (!ubifs_zn_dirty(znode)) {
1297 if (ubifs_zn_dirty(znode)) {
1299 * If znode is dirty, its parent has to be dirty as well. The
1300 * order of the operation is important, so we have to have
1304 if (zp && !ubifs_zn_dirty(zp)) {
1306 * The dirty flag is atomic and is cleared outside the
1307 * TNC mutex, so znode's dirty flag may now have
1308 * been cleared. The child is always cleared before the
1309 * parent, so we just need to check again.
1312 if (ubifs_zn_dirty(znode)) {
1320 const union ubifs_key *min, *max;
1322 if (znode->level != zp->level - 1) {
1327 /* Make sure the 'parent' pointer in our znode is correct */
1328 err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
1330 /* This zbranch does not exist in the parent */
1335 if (znode->iip >= zp->child_cnt) {
1340 if (znode->iip != n) {
1341 /* This may happen only in case of collisions */
1342 if (keys_cmp(c, &zp->zbranch[n].key,
1343 &zp->zbranch[znode->iip].key)) {
1351 * Make sure that the first key in our znode is greater than or
1352 * equal to the key in the pointing zbranch.
1355 cmp = keys_cmp(c, min, &znode->zbranch[0].key);
1361 if (n + 1 < zp->child_cnt) {
1362 max = &zp->zbranch[n + 1].key;
1365 * Make sure the last key in our znode is less or
1366 * equivalent than the key in the zbranch which goes
1367 * after our pointing zbranch.
1369 cmp = keys_cmp(c, max,
1370 &znode->zbranch[znode->child_cnt - 1].key);
1377 /* This may only be root znode */
1378 if (zbr != &c->zroot) {
1385 * Make sure that next key is greater or equivalent then the previous
1388 for (n = 1; n < znode->child_cnt; n++) {
1389 cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
1390 &znode->zbranch[n].key);
1396 /* This can only be keys with colliding hash */
1397 if (!is_hash_key(c, &znode->zbranch[n].key)) {
1402 if (znode->level != 0 || c->replaying)
1406 * Colliding keys should follow binary order of
1407 * corresponding xentry/dentry names.
1409 err = dbg_check_key_order(c, &znode->zbranch[n - 1],
1410 &znode->zbranch[n]);
1420 for (n = 0; n < znode->child_cnt; n++) {
1421 if (!znode->zbranch[n].znode &&
1422 (znode->zbranch[n].lnum == 0 ||
1423 znode->zbranch[n].len == 0)) {
1428 if (znode->zbranch[n].lnum != 0 &&
1429 znode->zbranch[n].len == 0) {
1434 if (znode->zbranch[n].lnum == 0 &&
1435 znode->zbranch[n].len != 0) {
1440 if (znode->zbranch[n].lnum == 0 &&
1441 znode->zbranch[n].offs != 0) {
1446 if (znode->level != 0 && znode->zbranch[n].znode)
1447 if (znode->zbranch[n].znode->parent != znode) {
1456 ubifs_err("failed, error %d", err);
1457 ubifs_msg("dump of the znode");
1458 ubifs_dump_znode(c, znode);
1460 ubifs_msg("dump of the parent znode");
1461 ubifs_dump_znode(c, zp);
1468 int dbg_check_dir(struct ubifs_info *c, const struct inode *dir)
1473 void dbg_debugfs_exit_fs(struct ubifs_info *c)
1478 int ubifs_debugging_init(struct ubifs_info *c)
1482 void ubifs_debugging_exit(struct ubifs_info *c)
1485 int dbg_check_filesystem(struct ubifs_info *c)
1489 int dbg_debugfs_init_fs(struct ubifs_info *c)
1497 * dbg_check_tnc - check TNC tree.
1498 * @c: UBIFS file-system description object
1499 * @extra: do extra checks that are possible at start commit
1501 * This function traverses whole TNC tree and checks every znode. Returns zero
1502 * if everything is all right and %-EINVAL if something is wrong with TNC.
1504 int dbg_check_tnc(struct ubifs_info *c, int extra)
1506 struct ubifs_znode *znode;
1507 long clean_cnt = 0, dirty_cnt = 0;
1510 if (!dbg_is_chk_index(c))
1513 ubifs_assert(mutex_is_locked(&c->tnc_mutex));
1514 if (!c->zroot.znode)
1517 znode = ubifs_tnc_postorder_first(c->zroot.znode);
1519 struct ubifs_znode *prev;
1520 struct ubifs_zbranch *zbr;
1525 zbr = &znode->parent->zbranch[znode->iip];
1527 err = dbg_check_znode(c, zbr);
1532 if (ubifs_zn_dirty(znode))
1539 znode = ubifs_tnc_postorder_next(znode);
1544 * If the last key of this znode is equivalent to the first key
1545 * of the next znode (collision), then check order of the keys.
1547 last = prev->child_cnt - 1;
1548 if (prev->level == 0 && znode->level == 0 && !c->replaying &&
1549 !keys_cmp(c, &prev->zbranch[last].key,
1550 &znode->zbranch[0].key)) {
1551 err = dbg_check_key_order(c, &prev->zbranch[last],
1552 &znode->zbranch[0]);
1556 ubifs_msg("first znode");
1557 ubifs_dump_znode(c, prev);
1558 ubifs_msg("second znode");
1559 ubifs_dump_znode(c, znode);
1566 if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
1567 ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld",
1568 atomic_long_read(&c->clean_zn_cnt),
1572 if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
1573 ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld",
1574 atomic_long_read(&c->dirty_zn_cnt),
1583 int dbg_check_tnc(struct ubifs_info *c, int extra)
1590 * dbg_walk_index - walk the on-flash index.
1591 * @c: UBIFS file-system description object
1592 * @leaf_cb: called for each leaf node
1593 * @znode_cb: called for each indexing node
1594 * @priv: private data which is passed to callbacks
1596 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1597 * node and @znode_cb for each indexing node. Returns zero in case of success
1598 * and a negative error code in case of failure.
1600 * It would be better if this function removed every znode it pulled to into
1601 * the TNC, so that the behavior more closely matched the non-debugging
1604 int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1605 dbg_znode_callback znode_cb, void *priv)
1608 struct ubifs_zbranch *zbr;
1609 struct ubifs_znode *znode, *child;
1611 mutex_lock(&c->tnc_mutex);
1612 /* If the root indexing node is not in TNC - pull it */
1613 if (!c->zroot.znode) {
1614 c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1615 if (IS_ERR(c->zroot.znode)) {
1616 err = PTR_ERR(c->zroot.znode);
1617 c->zroot.znode = NULL;
1623 * We are going to traverse the indexing tree in the postorder manner.
1624 * Go down and find the leftmost indexing node where we are going to
1627 znode = c->zroot.znode;
1628 while (znode->level > 0) {
1629 zbr = &znode->zbranch[0];
1632 child = ubifs_load_znode(c, zbr, znode, 0);
1633 if (IS_ERR(child)) {
1634 err = PTR_ERR(child);
1643 /* Iterate over all indexing nodes */
1650 err = znode_cb(c, znode, priv);
1652 ubifs_err("znode checking function returned error %d",
1654 ubifs_dump_znode(c, znode);
1658 if (leaf_cb && znode->level == 0) {
1659 for (idx = 0; idx < znode->child_cnt; idx++) {
1660 zbr = &znode->zbranch[idx];
1661 err = leaf_cb(c, zbr, priv);
1663 ubifs_err("leaf checking function returned error %d, for leaf at LEB %d:%d",
1664 err, zbr->lnum, zbr->offs);
1673 idx = znode->iip + 1;
1674 znode = znode->parent;
1675 if (idx < znode->child_cnt) {
1676 /* Switch to the next index in the parent */
1677 zbr = &znode->zbranch[idx];
1680 child = ubifs_load_znode(c, zbr, znode, idx);
1681 if (IS_ERR(child)) {
1682 err = PTR_ERR(child);
1690 * This is the last child, switch to the parent and
1695 /* Go to the lowest leftmost znode in the new sub-tree */
1696 while (znode->level > 0) {
1697 zbr = &znode->zbranch[0];
1700 child = ubifs_load_znode(c, zbr, znode, 0);
1701 if (IS_ERR(child)) {
1702 err = PTR_ERR(child);
1711 mutex_unlock(&c->tnc_mutex);
1716 zbr = &znode->parent->zbranch[znode->iip];
1719 ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1720 ubifs_dump_znode(c, znode);
1722 mutex_unlock(&c->tnc_mutex);
1727 * add_size - add znode size to partially calculated index size.
1728 * @c: UBIFS file-system description object
1729 * @znode: znode to add size for
1730 * @priv: partially calculated index size
1732 * This is a helper function for 'dbg_check_idx_size()' which is called for
1733 * every indexing node and adds its size to the 'long long' variable pointed to
1736 static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1738 long long *idx_size = priv;
1741 add = ubifs_idx_node_sz(c, znode->child_cnt);
1742 add = ALIGN(add, 8);
1748 * dbg_check_idx_size - check index size.
1749 * @c: UBIFS file-system description object
1750 * @idx_size: size to check
1752 * This function walks the UBIFS index, calculates its size and checks that the
1753 * size is equivalent to @idx_size. Returns zero in case of success and a
1754 * negative error code in case of failure.
1756 int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
1761 if (!dbg_is_chk_index(c))
1764 err = dbg_walk_index(c, NULL, add_size, &calc);
1766 ubifs_err("error %d while walking the index", err);
1770 if (calc != idx_size) {
1771 ubifs_err("index size check failed: calculated size is %lld, should be %lld",
1782 * struct fsck_inode - information about an inode used when checking the file-system.
1783 * @rb: link in the RB-tree of inodes
1784 * @inum: inode number
1785 * @mode: inode type, permissions, etc
1786 * @nlink: inode link count
1787 * @xattr_cnt: count of extended attributes
1788 * @references: how many directory/xattr entries refer this inode (calculated
1789 * while walking the index)
1790 * @calc_cnt: for directory inode count of child directories
1791 * @size: inode size (read from on-flash inode)
1792 * @xattr_sz: summary size of all extended attributes (read from on-flash
1794 * @calc_sz: for directories calculated directory size
1795 * @calc_xcnt: count of extended attributes
1796 * @calc_xsz: calculated summary size of all extended attributes
1797 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1798 * inode (read from on-flash inode)
1799 * @calc_xnms: calculated sum of lengths of all extended attribute names
1806 unsigned int xattr_cnt;
1810 unsigned int xattr_sz;
1812 long long calc_xcnt;
1814 unsigned int xattr_nms;
1815 long long calc_xnms;
1819 * struct fsck_data - private FS checking information.
1820 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1823 struct rb_root inodes;
1827 * add_inode - add inode information to RB-tree of inodes.
1828 * @c: UBIFS file-system description object
1829 * @fsckd: FS checking information
1830 * @ino: raw UBIFS inode to add
1832 * This is a helper function for 'check_leaf()' which adds information about
1833 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1834 * case of success and a negative error code in case of failure.
1836 static struct fsck_inode *add_inode(struct ubifs_info *c,
1837 struct fsck_data *fsckd,
1838 struct ubifs_ino_node *ino)
1840 struct rb_node **p, *parent = NULL;
1841 struct fsck_inode *fscki;
1842 ino_t inum = key_inum_flash(c, &ino->key);
1843 struct inode *inode;
1844 struct ubifs_inode *ui;
1846 p = &fsckd->inodes.rb_node;
1849 fscki = rb_entry(parent, struct fsck_inode, rb);
1850 if (inum < fscki->inum)
1852 else if (inum > fscki->inum)
1853 p = &(*p)->rb_right;
1858 if (inum > c->highest_inum) {
1859 ubifs_err("too high inode number, max. is %lu",
1860 (unsigned long)c->highest_inum);
1861 return ERR_PTR(-EINVAL);
1864 fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
1866 return ERR_PTR(-ENOMEM);
1868 inode = ilookup(c->vfs_sb, inum);
1872 * If the inode is present in the VFS inode cache, use it instead of
1873 * the on-flash inode which might be out-of-date. E.g., the size might
1874 * be out-of-date. If we do not do this, the following may happen, for
1876 * 1. A power cut happens
1877 * 2. We mount the file-system R/O, the replay process fixes up the
1878 * inode size in the VFS cache, but on on-flash.
1879 * 3. 'check_leaf()' fails because it hits a data node beyond inode
1883 fscki->nlink = le32_to_cpu(ino->nlink);
1884 fscki->size = le64_to_cpu(ino->size);
1885 fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
1886 fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
1887 fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
1888 fscki->mode = le32_to_cpu(ino->mode);
1890 ui = ubifs_inode(inode);
1891 fscki->nlink = inode->i_nlink;
1892 fscki->size = inode->i_size;
1893 fscki->xattr_cnt = ui->xattr_cnt;
1894 fscki->xattr_sz = ui->xattr_size;
1895 fscki->xattr_nms = ui->xattr_names;
1896 fscki->mode = inode->i_mode;
1900 if (S_ISDIR(fscki->mode)) {
1901 fscki->calc_sz = UBIFS_INO_NODE_SZ;
1902 fscki->calc_cnt = 2;
1905 rb_link_node(&fscki->rb, parent, p);
1906 rb_insert_color(&fscki->rb, &fsckd->inodes);
1912 * search_inode - search inode in the RB-tree of inodes.
1913 * @fsckd: FS checking information
1914 * @inum: inode number to search
1916 * This is a helper function for 'check_leaf()' which searches inode @inum in
1917 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1918 * the inode was not found.
1920 static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
1923 struct fsck_inode *fscki;
1925 p = fsckd->inodes.rb_node;
1927 fscki = rb_entry(p, struct fsck_inode, rb);
1928 if (inum < fscki->inum)
1930 else if (inum > fscki->inum)
1939 * read_add_inode - read inode node and add it to RB-tree of inodes.
1940 * @c: UBIFS file-system description object
1941 * @fsckd: FS checking information
1942 * @inum: inode number to read
1944 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1945 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1946 * information pointer in case of success and a negative error code in case of
1949 static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1950 struct fsck_data *fsckd, ino_t inum)
1953 union ubifs_key key;
1954 struct ubifs_znode *znode;
1955 struct ubifs_zbranch *zbr;
1956 struct ubifs_ino_node *ino;
1957 struct fsck_inode *fscki;
1959 fscki = search_inode(fsckd, inum);
1963 ino_key_init(c, &key, inum);
1964 err = ubifs_lookup_level0(c, &key, &znode, &n);
1966 ubifs_err("inode %lu not found in index", (unsigned long)inum);
1967 return ERR_PTR(-ENOENT);
1968 } else if (err < 0) {
1969 ubifs_err("error %d while looking up inode %lu",
1970 err, (unsigned long)inum);
1971 return ERR_PTR(err);
1974 zbr = &znode->zbranch[n];
1975 if (zbr->len < UBIFS_INO_NODE_SZ) {
1976 ubifs_err("bad node %lu node length %d",
1977 (unsigned long)inum, zbr->len);
1978 return ERR_PTR(-EINVAL);
1981 ino = kmalloc(zbr->len, GFP_NOFS);
1983 return ERR_PTR(-ENOMEM);
1985 err = ubifs_tnc_read_node(c, zbr, ino);
1987 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
1988 zbr->lnum, zbr->offs, err);
1990 return ERR_PTR(err);
1993 fscki = add_inode(c, fsckd, ino);
1995 if (IS_ERR(fscki)) {
1996 ubifs_err("error %ld while adding inode %lu node",
1997 PTR_ERR(fscki), (unsigned long)inum);
2005 * check_leaf - check leaf node.
2006 * @c: UBIFS file-system description object
2007 * @zbr: zbranch of the leaf node to check
2008 * @priv: FS checking information
2010 * This is a helper function for 'dbg_check_filesystem()' which is called for
2011 * every single leaf node while walking the indexing tree. It checks that the
2012 * leaf node referred from the indexing tree exists, has correct CRC, and does
2013 * some other basic validation. This function is also responsible for building
2014 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
2015 * calculates reference count, size, etc for each inode in order to later
2016 * compare them to the information stored inside the inodes and detect possible
2017 * inconsistencies. Returns zero in case of success and a negative error code
2018 * in case of failure.
2020 static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
2025 struct ubifs_ch *ch;
2026 int err, type = key_type(c, &zbr->key);
2027 struct fsck_inode *fscki;
2029 if (zbr->len < UBIFS_CH_SZ) {
2030 ubifs_err("bad leaf length %d (LEB %d:%d)",
2031 zbr->len, zbr->lnum, zbr->offs);
2035 node = kmalloc(zbr->len, GFP_NOFS);
2039 err = ubifs_tnc_read_node(c, zbr, node);
2041 ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
2042 zbr->lnum, zbr->offs, err);
2046 /* If this is an inode node, add it to RB-tree of inodes */
2047 if (type == UBIFS_INO_KEY) {
2048 fscki = add_inode(c, priv, node);
2049 if (IS_ERR(fscki)) {
2050 err = PTR_ERR(fscki);
2051 ubifs_err("error %d while adding inode node", err);
2057 if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
2058 type != UBIFS_DATA_KEY) {
2059 ubifs_err("unexpected node type %d at LEB %d:%d",
2060 type, zbr->lnum, zbr->offs);
2066 if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
2067 ubifs_err("too high sequence number, max. is %llu",
2073 if (type == UBIFS_DATA_KEY) {
2075 struct ubifs_data_node *dn = node;
2078 * Search the inode node this data node belongs to and insert
2079 * it to the RB-tree of inodes.
2081 inum = key_inum_flash(c, &dn->key);
2082 fscki = read_add_inode(c, priv, inum);
2083 if (IS_ERR(fscki)) {
2084 err = PTR_ERR(fscki);
2085 ubifs_err("error %d while processing data node and trying to find inode node %lu",
2086 err, (unsigned long)inum);
2090 /* Make sure the data node is within inode size */
2091 blk_offs = key_block_flash(c, &dn->key);
2092 blk_offs <<= UBIFS_BLOCK_SHIFT;
2093 blk_offs += le32_to_cpu(dn->size);
2094 if (blk_offs > fscki->size) {
2095 ubifs_err("data node at LEB %d:%d is not within inode size %lld",
2096 zbr->lnum, zbr->offs, fscki->size);
2102 struct ubifs_dent_node *dent = node;
2103 struct fsck_inode *fscki1;
2105 err = ubifs_validate_entry(c, dent);
2110 * Search the inode node this entry refers to and the parent
2111 * inode node and insert them to the RB-tree of inodes.
2113 inum = le64_to_cpu(dent->inum);
2114 fscki = read_add_inode(c, priv, inum);
2115 if (IS_ERR(fscki)) {
2116 err = PTR_ERR(fscki);
2117 ubifs_err("error %d while processing entry node and trying to find inode node %lu",
2118 err, (unsigned long)inum);
2122 /* Count how many direntries or xentries refers this inode */
2123 fscki->references += 1;
2125 inum = key_inum_flash(c, &dent->key);
2126 fscki1 = read_add_inode(c, priv, inum);
2127 if (IS_ERR(fscki1)) {
2128 err = PTR_ERR(fscki1);
2129 ubifs_err("error %d while processing entry node and trying to find parent inode node %lu",
2130 err, (unsigned long)inum);
2134 nlen = le16_to_cpu(dent->nlen);
2135 if (type == UBIFS_XENT_KEY) {
2136 fscki1->calc_xcnt += 1;
2137 fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
2138 fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
2139 fscki1->calc_xnms += nlen;
2141 fscki1->calc_sz += CALC_DENT_SIZE(nlen);
2142 if (dent->type == UBIFS_ITYPE_DIR)
2143 fscki1->calc_cnt += 1;
2152 ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
2153 ubifs_dump_node(c, node);
2160 * free_inodes - free RB-tree of inodes.
2161 * @fsckd: FS checking information
2163 static void free_inodes(struct fsck_data *fsckd)
2165 struct fsck_inode *fscki, *n;
2167 rbtree_postorder_for_each_entry_safe(fscki, n, &fsckd->inodes, rb)
2172 * check_inodes - checks all inodes.
2173 * @c: UBIFS file-system description object
2174 * @fsckd: FS checking information
2176 * This is a helper function for 'dbg_check_filesystem()' which walks the
2177 * RB-tree of inodes after the index scan has been finished, and checks that
2178 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
2179 * %-EINVAL if not, and a negative error code in case of failure.
2181 static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
2184 union ubifs_key key;
2185 struct ubifs_znode *znode;
2186 struct ubifs_zbranch *zbr;
2187 struct ubifs_ino_node *ino;
2188 struct fsck_inode *fscki;
2189 struct rb_node *this = rb_first(&fsckd->inodes);
2192 fscki = rb_entry(this, struct fsck_inode, rb);
2193 this = rb_next(this);
2195 if (S_ISDIR(fscki->mode)) {
2197 * Directories have to have exactly one reference (they
2198 * cannot have hardlinks), although root inode is an
2201 if (fscki->inum != UBIFS_ROOT_INO &&
2202 fscki->references != 1) {
2203 ubifs_err("directory inode %lu has %d direntries which refer it, but should be 1",
2204 (unsigned long)fscki->inum,
2208 if (fscki->inum == UBIFS_ROOT_INO &&
2209 fscki->references != 0) {
2210 ubifs_err("root inode %lu has non-zero (%d) direntries which refer it",
2211 (unsigned long)fscki->inum,
2215 if (fscki->calc_sz != fscki->size) {
2216 ubifs_err("directory inode %lu size is %lld, but calculated size is %lld",
2217 (unsigned long)fscki->inum,
2218 fscki->size, fscki->calc_sz);
2221 if (fscki->calc_cnt != fscki->nlink) {
2222 ubifs_err("directory inode %lu nlink is %d, but calculated nlink is %d",
2223 (unsigned long)fscki->inum,
2224 fscki->nlink, fscki->calc_cnt);
2228 if (fscki->references != fscki->nlink) {
2229 ubifs_err("inode %lu nlink is %d, but calculated nlink is %d",
2230 (unsigned long)fscki->inum,
2231 fscki->nlink, fscki->references);
2235 if (fscki->xattr_sz != fscki->calc_xsz) {
2236 ubifs_err("inode %lu has xattr size %u, but calculated size is %lld",
2237 (unsigned long)fscki->inum, fscki->xattr_sz,
2241 if (fscki->xattr_cnt != fscki->calc_xcnt) {
2242 ubifs_err("inode %lu has %u xattrs, but calculated count is %lld",
2243 (unsigned long)fscki->inum,
2244 fscki->xattr_cnt, fscki->calc_xcnt);
2247 if (fscki->xattr_nms != fscki->calc_xnms) {
2248 ubifs_err("inode %lu has xattr names' size %u, but calculated names' size is %lld",
2249 (unsigned long)fscki->inum, fscki->xattr_nms,
2258 /* Read the bad inode and dump it */
2259 ino_key_init(c, &key, fscki->inum);
2260 err = ubifs_lookup_level0(c, &key, &znode, &n);
2262 ubifs_err("inode %lu not found in index",
2263 (unsigned long)fscki->inum);
2265 } else if (err < 0) {
2266 ubifs_err("error %d while looking up inode %lu",
2267 err, (unsigned long)fscki->inum);
2271 zbr = &znode->zbranch[n];
2272 ino = kmalloc(zbr->len, GFP_NOFS);
2276 err = ubifs_tnc_read_node(c, zbr, ino);
2278 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2279 zbr->lnum, zbr->offs, err);
2284 ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",
2285 (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
2286 ubifs_dump_node(c, ino);
2292 * dbg_check_filesystem - check the file-system.
2293 * @c: UBIFS file-system description object
2295 * This function checks the file system, namely:
2296 * o makes sure that all leaf nodes exist and their CRCs are correct;
2297 * o makes sure inode nlink, size, xattr size/count are correct (for all
2300 * The function reads whole indexing tree and all nodes, so it is pretty
2301 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2302 * not, and a negative error code in case of failure.
2304 int dbg_check_filesystem(struct ubifs_info *c)
2307 struct fsck_data fsckd;
2309 if (!dbg_is_chk_fs(c))
2312 fsckd.inodes = RB_ROOT;
2313 err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
2317 err = check_inodes(c, &fsckd);
2321 free_inodes(&fsckd);
2325 ubifs_err("file-system check failed with error %d", err);
2327 free_inodes(&fsckd);
2332 * dbg_check_data_nodes_order - check that list of data nodes is sorted.
2333 * @c: UBIFS file-system description object
2334 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2336 * This function returns zero if the list of data nodes is sorted correctly,
2337 * and %-EINVAL if not.
2339 int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head)
2341 struct list_head *cur;
2342 struct ubifs_scan_node *sa, *sb;
2344 if (!dbg_is_chk_gen(c))
2347 for (cur = head->next; cur->next != head; cur = cur->next) {
2349 uint32_t blka, blkb;
2352 sa = container_of(cur, struct ubifs_scan_node, list);
2353 sb = container_of(cur->next, struct ubifs_scan_node, list);
2355 if (sa->type != UBIFS_DATA_NODE) {
2356 ubifs_err("bad node type %d", sa->type);
2357 ubifs_dump_node(c, sa->node);
2360 if (sb->type != UBIFS_DATA_NODE) {
2361 ubifs_err("bad node type %d", sb->type);
2362 ubifs_dump_node(c, sb->node);
2366 inuma = key_inum(c, &sa->key);
2367 inumb = key_inum(c, &sb->key);
2371 if (inuma > inumb) {
2372 ubifs_err("larger inum %lu goes before inum %lu",
2373 (unsigned long)inuma, (unsigned long)inumb);
2377 blka = key_block(c, &sa->key);
2378 blkb = key_block(c, &sb->key);
2381 ubifs_err("larger block %u goes before %u", blka, blkb);
2385 ubifs_err("two data nodes for the same block");
2393 ubifs_dump_node(c, sa->node);
2394 ubifs_dump_node(c, sb->node);
2399 * dbg_check_nondata_nodes_order - check that list of data nodes is sorted.
2400 * @c: UBIFS file-system description object
2401 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2403 * This function returns zero if the list of non-data nodes is sorted correctly,
2404 * and %-EINVAL if not.
2406 int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head)
2408 struct list_head *cur;
2409 struct ubifs_scan_node *sa, *sb;
2411 if (!dbg_is_chk_gen(c))
2414 for (cur = head->next; cur->next != head; cur = cur->next) {
2416 uint32_t hasha, hashb;
2419 sa = container_of(cur, struct ubifs_scan_node, list);
2420 sb = container_of(cur->next, struct ubifs_scan_node, list);
2422 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2423 sa->type != UBIFS_XENT_NODE) {
2424 ubifs_err("bad node type %d", sa->type);
2425 ubifs_dump_node(c, sa->node);
2428 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2429 sa->type != UBIFS_XENT_NODE) {
2430 ubifs_err("bad node type %d", sb->type);
2431 ubifs_dump_node(c, sb->node);
2435 if (sa->type != UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2436 ubifs_err("non-inode node goes before inode node");
2440 if (sa->type == UBIFS_INO_NODE && sb->type != UBIFS_INO_NODE)
2443 if (sa->type == UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2444 /* Inode nodes are sorted in descending size order */
2445 if (sa->len < sb->len) {
2446 ubifs_err("smaller inode node goes first");
2453 * This is either a dentry or xentry, which should be sorted in
2454 * ascending (parent ino, hash) order.
2456 inuma = key_inum(c, &sa->key);
2457 inumb = key_inum(c, &sb->key);
2461 if (inuma > inumb) {
2462 ubifs_err("larger inum %lu goes before inum %lu",
2463 (unsigned long)inuma, (unsigned long)inumb);
2467 hasha = key_block(c, &sa->key);
2468 hashb = key_block(c, &sb->key);
2470 if (hasha > hashb) {
2471 ubifs_err("larger hash %u goes before %u",
2480 ubifs_msg("dumping first node");
2481 ubifs_dump_node(c, sa->node);
2482 ubifs_msg("dumping second node");
2483 ubifs_dump_node(c, sb->node);
2488 static inline int chance(unsigned int n, unsigned int out_of)
2490 return !!((prandom_u32() % out_of) + 1 <= n);
2494 static int power_cut_emulated(struct ubifs_info *c, int lnum, int write)
2496 struct ubifs_debug_info *d = c->dbg;
2498 ubifs_assert(dbg_is_tst_rcvry(c));
2501 /* First call - decide delay to the power cut */
2503 unsigned long delay;
2507 /* Fail withing 1 minute */
2508 delay = prandom_u32() % 60000;
2509 d->pc_timeout = jiffies;
2510 d->pc_timeout += msecs_to_jiffies(delay);
2511 ubifs_warn("failing after %lums", delay);
2514 delay = prandom_u32() % 10000;
2515 /* Fail within 10000 operations */
2516 d->pc_cnt_max = delay;
2517 ubifs_warn("failing after %lu calls", delay);
2524 /* Determine if failure delay has expired */
2525 if (d->pc_delay == 1 && time_before(jiffies, d->pc_timeout))
2527 if (d->pc_delay == 2 && d->pc_cnt++ < d->pc_cnt_max)
2530 if (lnum == UBIFS_SB_LNUM) {
2531 if (write && chance(1, 2))
2535 ubifs_warn("failing in super block LEB %d", lnum);
2536 } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2539 ubifs_warn("failing in master LEB %d", lnum);
2540 } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2541 if (write && chance(99, 100))
2543 if (chance(399, 400))
2545 ubifs_warn("failing in log LEB %d", lnum);
2546 } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2547 if (write && chance(7, 8))
2551 ubifs_warn("failing in LPT LEB %d", lnum);
2552 } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2553 if (write && chance(1, 2))
2557 ubifs_warn("failing in orphan LEB %d", lnum);
2558 } else if (lnum == c->ihead_lnum) {
2559 if (chance(99, 100))
2561 ubifs_warn("failing in index head LEB %d", lnum);
2562 } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2565 ubifs_warn("failing in GC head LEB %d", lnum);
2566 } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
2567 !ubifs_search_bud(c, lnum)) {
2570 ubifs_warn("failing in non-bud LEB %d", lnum);
2571 } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
2572 c->cmt_state == COMMIT_RUNNING_REQUIRED) {
2573 if (chance(999, 1000))
2575 ubifs_warn("failing in bud LEB %d commit running", lnum);
2577 if (chance(9999, 10000))
2579 ubifs_warn("failing in bud LEB %d commit not running", lnum);
2583 ubifs_warn("========== Power cut emulated ==========");
2588 static int corrupt_data(const struct ubifs_info *c, const void *buf,
2591 unsigned int from, to, ffs = chance(1, 2);
2592 unsigned char *p = (void *)buf;
2594 from = prandom_u32() % len;
2595 /* Corruption span max to end of write unit */
2596 to = min(len, ALIGN(from + 1, c->max_write_size));
2598 ubifs_warn("filled bytes %u-%u with %s", from, to - 1,
2599 ffs ? "0xFFs" : "random data");
2602 memset(p + from, 0xFF, to - from);
2604 prandom_bytes(p + from, to - from);
2609 int dbg_leb_write(struct ubifs_info *c, int lnum, const void *buf,
2614 if (c->dbg->pc_happened)
2617 failing = power_cut_emulated(c, lnum, 1);
2619 len = corrupt_data(c, buf, len);
2620 ubifs_warn("actually write %d bytes to LEB %d:%d (the buffer was corrupted)",
2623 err = ubi_leb_write(c->ubi, lnum, buf, offs, len);
2631 int dbg_leb_change(struct ubifs_info *c, int lnum, const void *buf,
2636 if (c->dbg->pc_happened)
2638 if (power_cut_emulated(c, lnum, 1))
2640 err = ubi_leb_change(c->ubi, lnum, buf, len);
2643 if (power_cut_emulated(c, lnum, 1))
2648 int dbg_leb_unmap(struct ubifs_info *c, int lnum)
2652 if (c->dbg->pc_happened)
2654 if (power_cut_emulated(c, lnum, 0))
2656 err = ubi_leb_unmap(c->ubi, lnum);
2659 if (power_cut_emulated(c, lnum, 0))
2664 int dbg_leb_map(struct ubifs_info *c, int lnum)
2668 if (c->dbg->pc_happened)
2670 if (power_cut_emulated(c, lnum, 0))
2672 err = ubi_leb_map(c->ubi, lnum);
2675 if (power_cut_emulated(c, lnum, 0))
2681 * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
2682 * contain the stuff specific to particular file-system mounts.
2684 static struct dentry *dfs_rootdir;
2686 static int dfs_file_open(struct inode *inode, struct file *file)
2688 file->private_data = inode->i_private;
2689 return nonseekable_open(inode, file);
2693 * provide_user_output - provide output to the user reading a debugfs file.
2694 * @val: boolean value for the answer
2695 * @u: the buffer to store the answer at
2696 * @count: size of the buffer
2697 * @ppos: position in the @u output buffer
2699 * This is a simple helper function which stores @val boolean value in the user
2700 * buffer when the user reads one of UBIFS debugfs files. Returns amount of
2701 * bytes written to @u in case of success and a negative error code in case of
2704 static int provide_user_output(int val, char __user *u, size_t count,
2716 return simple_read_from_buffer(u, count, ppos, buf, 2);
2719 static ssize_t dfs_file_read(struct file *file, char __user *u, size_t count,
2722 struct dentry *dent = file->f_path.dentry;
2723 struct ubifs_info *c = file->private_data;
2724 struct ubifs_debug_info *d = c->dbg;
2727 if (dent == d->dfs_chk_gen)
2729 else if (dent == d->dfs_chk_index)
2731 else if (dent == d->dfs_chk_orph)
2733 else if (dent == d->dfs_chk_lprops)
2734 val = d->chk_lprops;
2735 else if (dent == d->dfs_chk_fs)
2737 else if (dent == d->dfs_tst_rcvry)
2739 else if (dent == d->dfs_ro_error)
2744 return provide_user_output(val, u, count, ppos);
2748 * interpret_user_input - interpret user debugfs file input.
2749 * @u: user-provided buffer with the input
2750 * @count: buffer size
2752 * This is a helper function which interpret user input to a boolean UBIFS
2753 * debugfs file. Returns %0 or %1 in case of success and a negative error code
2754 * in case of failure.
2756 static int interpret_user_input(const char __user *u, size_t count)
2761 buf_size = min_t(size_t, count, (sizeof(buf) - 1));
2762 if (copy_from_user(buf, u, buf_size))
2767 else if (buf[0] == '0')
2773 static ssize_t dfs_file_write(struct file *file, const char __user *u,
2774 size_t count, loff_t *ppos)
2776 struct ubifs_info *c = file->private_data;
2777 struct ubifs_debug_info *d = c->dbg;
2778 struct dentry *dent = file->f_path.dentry;
2782 * TODO: this is racy - the file-system might have already been
2783 * unmounted and we'd oops in this case. The plan is to fix it with
2784 * help of 'iterate_supers_type()' which we should have in v3.0: when
2785 * a debugfs opened, we rember FS's UUID in file->private_data. Then
2786 * whenever we access the FS via a debugfs file, we iterate all UBIFS
2787 * superblocks and fine the one with the same UUID, and take the
2790 * The other way to go suggested by Al Viro is to create a separate
2791 * 'ubifs-debug' file-system instead.
2793 if (file->f_path.dentry == d->dfs_dump_lprops) {
2794 ubifs_dump_lprops(c);
2797 if (file->f_path.dentry == d->dfs_dump_budg) {
2798 ubifs_dump_budg(c, &c->bi);
2801 if (file->f_path.dentry == d->dfs_dump_tnc) {
2802 mutex_lock(&c->tnc_mutex);
2804 mutex_unlock(&c->tnc_mutex);
2808 val = interpret_user_input(u, count);
2812 if (dent == d->dfs_chk_gen)
2814 else if (dent == d->dfs_chk_index)
2816 else if (dent == d->dfs_chk_orph)
2818 else if (dent == d->dfs_chk_lprops)
2819 d->chk_lprops = val;
2820 else if (dent == d->dfs_chk_fs)
2822 else if (dent == d->dfs_tst_rcvry)
2824 else if (dent == d->dfs_ro_error)
2825 c->ro_error = !!val;
2832 static const struct file_operations dfs_fops = {
2833 .open = dfs_file_open,
2834 .read = dfs_file_read,
2835 .write = dfs_file_write,
2836 .owner = THIS_MODULE,
2837 .llseek = no_llseek,
2841 * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
2842 * @c: UBIFS file-system description object
2844 * This function creates all debugfs files for this instance of UBIFS. Returns
2845 * zero in case of success and a negative error code in case of failure.
2847 * Note, the only reason we have not merged this function with the
2848 * 'ubifs_debugging_init()' function is because it is better to initialize
2849 * debugfs interfaces at the very end of the mount process, and remove them at
2850 * the very beginning of the mount process.
2852 int dbg_debugfs_init_fs(struct ubifs_info *c)
2856 struct dentry *dent;
2857 struct ubifs_debug_info *d = c->dbg;
2859 if (!IS_ENABLED(CONFIG_DEBUG_FS))
2862 n = snprintf(d->dfs_dir_name, UBIFS_DFS_DIR_LEN + 1, UBIFS_DFS_DIR_NAME,
2863 c->vi.ubi_num, c->vi.vol_id);
2864 if (n == UBIFS_DFS_DIR_LEN) {
2865 /* The array size is too small */
2866 fname = UBIFS_DFS_DIR_NAME;
2867 dent = ERR_PTR(-EINVAL);
2871 fname = d->dfs_dir_name;
2872 dent = debugfs_create_dir(fname, dfs_rootdir);
2873 if (IS_ERR_OR_NULL(dent))
2877 fname = "dump_lprops";
2878 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2879 if (IS_ERR_OR_NULL(dent))
2881 d->dfs_dump_lprops = dent;
2883 fname = "dump_budg";
2884 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2885 if (IS_ERR_OR_NULL(dent))
2887 d->dfs_dump_budg = dent;
2890 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2891 if (IS_ERR_OR_NULL(dent))
2893 d->dfs_dump_tnc = dent;
2895 fname = "chk_general";
2896 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2898 if (IS_ERR_OR_NULL(dent))
2900 d->dfs_chk_gen = dent;
2902 fname = "chk_index";
2903 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2905 if (IS_ERR_OR_NULL(dent))
2907 d->dfs_chk_index = dent;
2909 fname = "chk_orphans";
2910 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2912 if (IS_ERR_OR_NULL(dent))
2914 d->dfs_chk_orph = dent;
2916 fname = "chk_lprops";
2917 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2919 if (IS_ERR_OR_NULL(dent))
2921 d->dfs_chk_lprops = dent;
2924 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2926 if (IS_ERR_OR_NULL(dent))
2928 d->dfs_chk_fs = dent;
2930 fname = "tst_recovery";
2931 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2933 if (IS_ERR_OR_NULL(dent))
2935 d->dfs_tst_rcvry = dent;
2938 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2940 if (IS_ERR_OR_NULL(dent))
2942 d->dfs_ro_error = dent;
2947 debugfs_remove_recursive(d->dfs_dir);
2949 err = dent ? PTR_ERR(dent) : -ENODEV;
2950 ubifs_err("cannot create \"%s\" debugfs file or directory, error %d\n",
2956 * dbg_debugfs_exit_fs - remove all debugfs files.
2957 * @c: UBIFS file-system description object
2959 void dbg_debugfs_exit_fs(struct ubifs_info *c)
2961 if (IS_ENABLED(CONFIG_DEBUG_FS))
2962 debugfs_remove_recursive(c->dbg->dfs_dir);
2965 struct ubifs_global_debug_info ubifs_dbg;
2967 static struct dentry *dfs_chk_gen;
2968 static struct dentry *dfs_chk_index;
2969 static struct dentry *dfs_chk_orph;
2970 static struct dentry *dfs_chk_lprops;
2971 static struct dentry *dfs_chk_fs;
2972 static struct dentry *dfs_tst_rcvry;
2974 static ssize_t dfs_global_file_read(struct file *file, char __user *u,
2975 size_t count, loff_t *ppos)
2977 struct dentry *dent = file->f_path.dentry;
2980 if (dent == dfs_chk_gen)
2981 val = ubifs_dbg.chk_gen;
2982 else if (dent == dfs_chk_index)
2983 val = ubifs_dbg.chk_index;
2984 else if (dent == dfs_chk_orph)
2985 val = ubifs_dbg.chk_orph;
2986 else if (dent == dfs_chk_lprops)
2987 val = ubifs_dbg.chk_lprops;
2988 else if (dent == dfs_chk_fs)
2989 val = ubifs_dbg.chk_fs;
2990 else if (dent == dfs_tst_rcvry)
2991 val = ubifs_dbg.tst_rcvry;
2995 return provide_user_output(val, u, count, ppos);
2998 static ssize_t dfs_global_file_write(struct file *file, const char __user *u,
2999 size_t count, loff_t *ppos)
3001 struct dentry *dent = file->f_path.dentry;
3004 val = interpret_user_input(u, count);
3008 if (dent == dfs_chk_gen)
3009 ubifs_dbg.chk_gen = val;
3010 else if (dent == dfs_chk_index)
3011 ubifs_dbg.chk_index = val;
3012 else if (dent == dfs_chk_orph)
3013 ubifs_dbg.chk_orph = val;
3014 else if (dent == dfs_chk_lprops)
3015 ubifs_dbg.chk_lprops = val;
3016 else if (dent == dfs_chk_fs)
3017 ubifs_dbg.chk_fs = val;
3018 else if (dent == dfs_tst_rcvry)
3019 ubifs_dbg.tst_rcvry = val;
3026 static const struct file_operations dfs_global_fops = {
3027 .read = dfs_global_file_read,
3028 .write = dfs_global_file_write,
3029 .owner = THIS_MODULE,
3030 .llseek = no_llseek,
3034 * dbg_debugfs_init - initialize debugfs file-system.
3036 * UBIFS uses debugfs file-system to expose various debugging knobs to
3037 * user-space. This function creates "ubifs" directory in the debugfs
3038 * file-system. Returns zero in case of success and a negative error code in
3041 int dbg_debugfs_init(void)
3045 struct dentry *dent;
3047 if (!IS_ENABLED(CONFIG_DEBUG_FS))
3051 dent = debugfs_create_dir(fname, NULL);
3052 if (IS_ERR_OR_NULL(dent))
3056 fname = "chk_general";
3057 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3059 if (IS_ERR_OR_NULL(dent))
3063 fname = "chk_index";
3064 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3066 if (IS_ERR_OR_NULL(dent))
3068 dfs_chk_index = dent;
3070 fname = "chk_orphans";
3071 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3073 if (IS_ERR_OR_NULL(dent))
3075 dfs_chk_orph = dent;
3077 fname = "chk_lprops";
3078 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3080 if (IS_ERR_OR_NULL(dent))
3082 dfs_chk_lprops = dent;
3085 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3087 if (IS_ERR_OR_NULL(dent))
3091 fname = "tst_recovery";
3092 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3094 if (IS_ERR_OR_NULL(dent))
3096 dfs_tst_rcvry = dent;
3101 debugfs_remove_recursive(dfs_rootdir);
3103 err = dent ? PTR_ERR(dent) : -ENODEV;
3104 ubifs_err("cannot create \"%s\" debugfs file or directory, error %d\n",
3110 * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
3112 void dbg_debugfs_exit(void)
3114 if (IS_ENABLED(CONFIG_DEBUG_FS))
3115 debugfs_remove_recursive(dfs_rootdir);
3119 * ubifs_debugging_init - initialize UBIFS debugging.
3120 * @c: UBIFS file-system description object
3122 * This function initializes debugging-related data for the file system.
3123 * Returns zero in case of success and a negative error code in case of
3126 int ubifs_debugging_init(struct ubifs_info *c)
3128 c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
3136 * ubifs_debugging_exit - free debugging data.
3137 * @c: UBIFS file-system description object
3139 void ubifs_debugging_exit(struct ubifs_info *c)