1 // SPDX-License-Identifier: GPL-2.0+
3 * This file is part of UBIFS.
5 * Copyright (C) 2006-2008 Nokia Corporation
7 * Authors: Artem Bityutskiy (Битюцкий Артём)
12 * This file implements most of the debugging stuff which is compiled in only
13 * when it is enabled. But some debugging check functions are implemented in
14 * corresponding subsystem, just because they are closely related and utilize
15 * various local functions of those subsystems.
20 #include <dm/devres.h>
21 #include <linux/printk.h>
24 #include <linux/module.h>
25 #include <linux/debugfs.h>
26 #include <linux/math64.h>
27 #include <linux/uaccess.h>
28 #include <linux/random.h>
30 #include <linux/compat.h>
31 #include <linux/err.h>
36 static DEFINE_SPINLOCK(dbg_lock);
39 #ifndef CONFIG_UBIFS_SILENCE_DEBUG_DUMP
40 static const char *get_key_fmt(int fmt)
43 case UBIFS_SIMPLE_KEY_FMT:
46 return "unknown/invalid format";
50 static const char *get_key_hash(int hash)
53 case UBIFS_KEY_HASH_R5:
55 case UBIFS_KEY_HASH_TEST:
58 return "unknown/invalid name hash";
62 static const char *get_key_type(int type)
76 return "unknown/invalid key";
81 static const char *get_dent_type(int type)
94 case UBIFS_ITYPE_FIFO:
96 case UBIFS_ITYPE_SOCK:
99 return "unknown/invalid type";
104 const char *dbg_snprintf_key(const struct ubifs_info *c,
105 const union ubifs_key *key, char *buffer, int len)
108 int type = key_type(c, key);
110 if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
113 len -= snprintf(p, len, "(%lu, %s)",
114 (unsigned long)key_inum(c, key),
119 len -= snprintf(p, len, "(%lu, %s, %#08x)",
120 (unsigned long)key_inum(c, key),
121 get_key_type(type), key_hash(c, key));
124 len -= snprintf(p, len, "(%lu, %s, %u)",
125 (unsigned long)key_inum(c, key),
126 get_key_type(type), key_block(c, key));
129 len -= snprintf(p, len, "(%lu, %s)",
130 (unsigned long)key_inum(c, key),
134 len -= snprintf(p, len, "(bad key type: %#08x, %#08x)",
135 key->u32[0], key->u32[1]);
138 len -= snprintf(p, len, "bad key format %d", c->key_fmt);
139 ubifs_assert(len > 0);
143 const char *dbg_ntype(int type)
147 return "padding node";
149 return "superblock node";
151 return "master node";
153 return "reference node";
156 case UBIFS_DENT_NODE:
157 return "direntry node";
158 case UBIFS_XENT_NODE:
159 return "xentry node";
160 case UBIFS_DATA_NODE:
162 case UBIFS_TRUN_NODE:
163 return "truncate node";
165 return "indexing node";
167 return "commit start node";
168 case UBIFS_ORPH_NODE:
169 return "orphan node";
171 return "unknown node";
175 static const char *dbg_gtype(int type)
178 case UBIFS_NO_NODE_GROUP:
179 return "no node group";
180 case UBIFS_IN_NODE_GROUP:
181 return "in node group";
182 case UBIFS_LAST_OF_NODE_GROUP:
183 return "last of node group";
189 const char *dbg_cstate(int cmt_state)
193 return "commit resting";
194 case COMMIT_BACKGROUND:
195 return "background commit requested";
196 case COMMIT_REQUIRED:
197 return "commit required";
198 case COMMIT_RUNNING_BACKGROUND:
199 return "BACKGROUND commit running";
200 case COMMIT_RUNNING_REQUIRED:
201 return "commit running and required";
203 return "broken commit";
205 return "unknown commit state";
209 const char *dbg_jhead(int jhead)
219 return "unknown journal head";
223 static void dump_ch(const struct ubifs_ch *ch)
225 pr_err("\tmagic %#x\n", le32_to_cpu(ch->magic));
226 pr_err("\tcrc %#x\n", le32_to_cpu(ch->crc));
227 pr_err("\tnode_type %d (%s)\n", ch->node_type,
228 dbg_ntype(ch->node_type));
229 pr_err("\tgroup_type %d (%s)\n", ch->group_type,
230 dbg_gtype(ch->group_type));
231 pr_err("\tsqnum %llu\n",
232 (unsigned long long)le64_to_cpu(ch->sqnum));
233 pr_err("\tlen %u\n", le32_to_cpu(ch->len));
237 void ubifs_dump_inode(struct ubifs_info *c, const struct inode *inode)
240 const struct ubifs_inode *ui = ubifs_inode(inode);
241 struct qstr nm = { .name = NULL };
243 struct ubifs_dent_node *dent, *pdent = NULL;
246 pr_err("Dump in-memory inode:");
247 pr_err("\tinode %lu\n", inode->i_ino);
248 pr_err("\tsize %llu\n",
249 (unsigned long long)i_size_read(inode));
250 pr_err("\tnlink %u\n", inode->i_nlink);
251 pr_err("\tuid %u\n", (unsigned int)i_uid_read(inode));
252 pr_err("\tgid %u\n", (unsigned int)i_gid_read(inode));
253 pr_err("\tatime %u.%u\n",
254 (unsigned int)inode->i_atime.tv_sec,
255 (unsigned int)inode->i_atime.tv_nsec);
256 pr_err("\tmtime %u.%u\n",
257 (unsigned int)inode->i_mtime.tv_sec,
258 (unsigned int)inode->i_mtime.tv_nsec);
259 pr_err("\tctime %u.%u\n",
260 (unsigned int)inode->i_ctime.tv_sec,
261 (unsigned int)inode->i_ctime.tv_nsec);
262 pr_err("\tcreat_sqnum %llu\n", ui->creat_sqnum);
263 pr_err("\txattr_size %u\n", ui->xattr_size);
264 pr_err("\txattr_cnt %u\n", ui->xattr_cnt);
265 pr_err("\txattr_names %u\n", ui->xattr_names);
266 pr_err("\tdirty %u\n", ui->dirty);
267 pr_err("\txattr %u\n", ui->xattr);
268 pr_err("\tbulk_read %u\n", ui->xattr);
269 pr_err("\tsynced_i_size %llu\n",
270 (unsigned long long)ui->synced_i_size);
271 pr_err("\tui_size %llu\n",
272 (unsigned long long)ui->ui_size);
273 pr_err("\tflags %d\n", ui->flags);
274 pr_err("\tcompr_type %d\n", ui->compr_type);
275 pr_err("\tlast_page_read %lu\n", ui->last_page_read);
276 pr_err("\tread_in_a_row %lu\n", ui->read_in_a_row);
277 pr_err("\tdata_len %d\n", ui->data_len);
279 if (!S_ISDIR(inode->i_mode))
282 pr_err("List of directory entries:\n");
283 ubifs_assert(!mutex_is_locked(&c->tnc_mutex));
285 lowest_dent_key(c, &key, inode->i_ino);
287 dent = ubifs_tnc_next_ent(c, &key, &nm);
289 if (PTR_ERR(dent) != -ENOENT)
290 pr_err("error %ld\n", PTR_ERR(dent));
294 pr_err("\t%d: %s (%s)\n",
295 count++, dent->name, get_dent_type(dent->type));
297 nm.name = dent->name;
298 nm.len = le16_to_cpu(dent->nlen);
301 key_read(c, &dent->key, &key);
307 void ubifs_dump_node(const struct ubifs_info *c, const void *node)
309 #ifndef CONFIG_UBIFS_SILENCE_DEBUG_DUMP
312 const struct ubifs_ch *ch = node;
313 char key_buf[DBG_KEY_BUF_LEN];
315 /* If the magic is incorrect, just hexdump the first bytes */
316 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
317 pr_err("Not a node, first %zu bytes:", UBIFS_CH_SZ);
318 print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1,
319 (void *)node, UBIFS_CH_SZ, 1);
323 spin_lock(&dbg_lock);
326 switch (ch->node_type) {
329 const struct ubifs_pad_node *pad = node;
331 pr_err("\tpad_len %u\n", le32_to_cpu(pad->pad_len));
336 const struct ubifs_sb_node *sup = node;
337 unsigned int sup_flags = le32_to_cpu(sup->flags);
339 pr_err("\tkey_hash %d (%s)\n",
340 (int)sup->key_hash, get_key_hash(sup->key_hash));
341 pr_err("\tkey_fmt %d (%s)\n",
342 (int)sup->key_fmt, get_key_fmt(sup->key_fmt));
343 pr_err("\tflags %#x\n", sup_flags);
344 pr_err("\tbig_lpt %u\n",
345 !!(sup_flags & UBIFS_FLG_BIGLPT));
346 pr_err("\tspace_fixup %u\n",
347 !!(sup_flags & UBIFS_FLG_SPACE_FIXUP));
348 pr_err("\tmin_io_size %u\n", le32_to_cpu(sup->min_io_size));
349 pr_err("\tleb_size %u\n", le32_to_cpu(sup->leb_size));
350 pr_err("\tleb_cnt %u\n", le32_to_cpu(sup->leb_cnt));
351 pr_err("\tmax_leb_cnt %u\n", le32_to_cpu(sup->max_leb_cnt));
352 pr_err("\tmax_bud_bytes %llu\n",
353 (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
354 pr_err("\tlog_lebs %u\n", le32_to_cpu(sup->log_lebs));
355 pr_err("\tlpt_lebs %u\n", le32_to_cpu(sup->lpt_lebs));
356 pr_err("\torph_lebs %u\n", le32_to_cpu(sup->orph_lebs));
357 pr_err("\tjhead_cnt %u\n", le32_to_cpu(sup->jhead_cnt));
358 pr_err("\tfanout %u\n", le32_to_cpu(sup->fanout));
359 pr_err("\tlsave_cnt %u\n", le32_to_cpu(sup->lsave_cnt));
360 pr_err("\tdefault_compr %u\n",
361 (int)le16_to_cpu(sup->default_compr));
362 pr_err("\trp_size %llu\n",
363 (unsigned long long)le64_to_cpu(sup->rp_size));
364 pr_err("\trp_uid %u\n", le32_to_cpu(sup->rp_uid));
365 pr_err("\trp_gid %u\n", le32_to_cpu(sup->rp_gid));
366 pr_err("\tfmt_version %u\n", le32_to_cpu(sup->fmt_version));
367 pr_err("\ttime_gran %u\n", le32_to_cpu(sup->time_gran));
368 pr_err("\tUUID %pUB\n", sup->uuid);
373 const struct ubifs_mst_node *mst = node;
375 pr_err("\thighest_inum %llu\n",
376 (unsigned long long)le64_to_cpu(mst->highest_inum));
377 pr_err("\tcommit number %llu\n",
378 (unsigned long long)le64_to_cpu(mst->cmt_no));
379 pr_err("\tflags %#x\n", le32_to_cpu(mst->flags));
380 pr_err("\tlog_lnum %u\n", le32_to_cpu(mst->log_lnum));
381 pr_err("\troot_lnum %u\n", le32_to_cpu(mst->root_lnum));
382 pr_err("\troot_offs %u\n", le32_to_cpu(mst->root_offs));
383 pr_err("\troot_len %u\n", le32_to_cpu(mst->root_len));
384 pr_err("\tgc_lnum %u\n", le32_to_cpu(mst->gc_lnum));
385 pr_err("\tihead_lnum %u\n", le32_to_cpu(mst->ihead_lnum));
386 pr_err("\tihead_offs %u\n", le32_to_cpu(mst->ihead_offs));
387 pr_err("\tindex_size %llu\n",
388 (unsigned long long)le64_to_cpu(mst->index_size));
389 pr_err("\tlpt_lnum %u\n", le32_to_cpu(mst->lpt_lnum));
390 pr_err("\tlpt_offs %u\n", le32_to_cpu(mst->lpt_offs));
391 pr_err("\tnhead_lnum %u\n", le32_to_cpu(mst->nhead_lnum));
392 pr_err("\tnhead_offs %u\n", le32_to_cpu(mst->nhead_offs));
393 pr_err("\tltab_lnum %u\n", le32_to_cpu(mst->ltab_lnum));
394 pr_err("\tltab_offs %u\n", le32_to_cpu(mst->ltab_offs));
395 pr_err("\tlsave_lnum %u\n", le32_to_cpu(mst->lsave_lnum));
396 pr_err("\tlsave_offs %u\n", le32_to_cpu(mst->lsave_offs));
397 pr_err("\tlscan_lnum %u\n", le32_to_cpu(mst->lscan_lnum));
398 pr_err("\tleb_cnt %u\n", le32_to_cpu(mst->leb_cnt));
399 pr_err("\tempty_lebs %u\n", le32_to_cpu(mst->empty_lebs));
400 pr_err("\tidx_lebs %u\n", le32_to_cpu(mst->idx_lebs));
401 pr_err("\ttotal_free %llu\n",
402 (unsigned long long)le64_to_cpu(mst->total_free));
403 pr_err("\ttotal_dirty %llu\n",
404 (unsigned long long)le64_to_cpu(mst->total_dirty));
405 pr_err("\ttotal_used %llu\n",
406 (unsigned long long)le64_to_cpu(mst->total_used));
407 pr_err("\ttotal_dead %llu\n",
408 (unsigned long long)le64_to_cpu(mst->total_dead));
409 pr_err("\ttotal_dark %llu\n",
410 (unsigned long long)le64_to_cpu(mst->total_dark));
415 const struct ubifs_ref_node *ref = node;
417 pr_err("\tlnum %u\n", le32_to_cpu(ref->lnum));
418 pr_err("\toffs %u\n", le32_to_cpu(ref->offs));
419 pr_err("\tjhead %u\n", le32_to_cpu(ref->jhead));
424 const struct ubifs_ino_node *ino = node;
426 key_read(c, &ino->key, &key);
428 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
429 pr_err("\tcreat_sqnum %llu\n",
430 (unsigned long long)le64_to_cpu(ino->creat_sqnum));
431 pr_err("\tsize %llu\n",
432 (unsigned long long)le64_to_cpu(ino->size));
433 pr_err("\tnlink %u\n", le32_to_cpu(ino->nlink));
434 pr_err("\tatime %lld.%u\n",
435 (long long)le64_to_cpu(ino->atime_sec),
436 le32_to_cpu(ino->atime_nsec));
437 pr_err("\tmtime %lld.%u\n",
438 (long long)le64_to_cpu(ino->mtime_sec),
439 le32_to_cpu(ino->mtime_nsec));
440 pr_err("\tctime %lld.%u\n",
441 (long long)le64_to_cpu(ino->ctime_sec),
442 le32_to_cpu(ino->ctime_nsec));
443 pr_err("\tuid %u\n", le32_to_cpu(ino->uid));
444 pr_err("\tgid %u\n", le32_to_cpu(ino->gid));
445 pr_err("\tmode %u\n", le32_to_cpu(ino->mode));
446 pr_err("\tflags %#x\n", le32_to_cpu(ino->flags));
447 pr_err("\txattr_cnt %u\n", le32_to_cpu(ino->xattr_cnt));
448 pr_err("\txattr_size %u\n", le32_to_cpu(ino->xattr_size));
449 pr_err("\txattr_names %u\n", le32_to_cpu(ino->xattr_names));
450 pr_err("\tcompr_type %#x\n",
451 (int)le16_to_cpu(ino->compr_type));
452 pr_err("\tdata len %u\n", le32_to_cpu(ino->data_len));
455 case UBIFS_DENT_NODE:
456 case UBIFS_XENT_NODE:
458 const struct ubifs_dent_node *dent = node;
459 int nlen = le16_to_cpu(dent->nlen);
461 key_read(c, &dent->key, &key);
463 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
464 pr_err("\tinum %llu\n",
465 (unsigned long long)le64_to_cpu(dent->inum));
466 pr_err("\ttype %d\n", (int)dent->type);
467 pr_err("\tnlen %d\n", nlen);
470 if (nlen > UBIFS_MAX_NLEN)
471 pr_err("(bad name length, not printing, bad or corrupted node)");
473 for (i = 0; i < nlen && dent->name[i]; i++)
474 pr_cont("%c", dent->name[i]);
480 case UBIFS_DATA_NODE:
482 const struct ubifs_data_node *dn = node;
483 int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
485 key_read(c, &dn->key, &key);
487 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
488 pr_err("\tsize %u\n", le32_to_cpu(dn->size));
489 pr_err("\tcompr_typ %d\n",
490 (int)le16_to_cpu(dn->compr_type));
491 pr_err("\tdata size %d\n", dlen);
493 print_hex_dump("\t", DUMP_PREFIX_OFFSET, 32, 1,
494 (void *)&dn->data, dlen, 0);
497 case UBIFS_TRUN_NODE:
499 const struct ubifs_trun_node *trun = node;
501 pr_err("\tinum %u\n", le32_to_cpu(trun->inum));
502 pr_err("\told_size %llu\n",
503 (unsigned long long)le64_to_cpu(trun->old_size));
504 pr_err("\tnew_size %llu\n",
505 (unsigned long long)le64_to_cpu(trun->new_size));
510 const struct ubifs_idx_node *idx = node;
512 n = le16_to_cpu(idx->child_cnt);
513 pr_err("\tchild_cnt %d\n", n);
514 pr_err("\tlevel %d\n", (int)le16_to_cpu(idx->level));
515 pr_err("\tBranches:\n");
517 for (i = 0; i < n && i < c->fanout - 1; i++) {
518 const struct ubifs_branch *br;
520 br = ubifs_idx_branch(c, idx, i);
521 key_read(c, &br->key, &key);
522 pr_err("\t%d: LEB %d:%d len %d key %s\n",
523 i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
524 le32_to_cpu(br->len),
525 dbg_snprintf_key(c, &key, key_buf,
532 case UBIFS_ORPH_NODE:
534 const struct ubifs_orph_node *orph = node;
536 pr_err("\tcommit number %llu\n",
538 le64_to_cpu(orph->cmt_no) & LLONG_MAX);
539 pr_err("\tlast node flag %llu\n",
540 (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
541 n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
542 pr_err("\t%d orphan inode numbers:\n", n);
543 for (i = 0; i < n; i++)
544 pr_err("\t ino %llu\n",
545 (unsigned long long)le64_to_cpu(orph->inos[i]));
549 pr_err("node type %d was not recognized\n",
552 spin_unlock(&dbg_lock);
556 void ubifs_dump_budget_req(const struct ubifs_budget_req *req)
558 #ifndef CONFIG_UBIFS_SILENCE_DEBUG_DUMP
559 spin_lock(&dbg_lock);
560 pr_err("Budgeting request: new_ino %d, dirtied_ino %d\n",
561 req->new_ino, req->dirtied_ino);
562 pr_err("\tnew_ino_d %d, dirtied_ino_d %d\n",
563 req->new_ino_d, req->dirtied_ino_d);
564 pr_err("\tnew_page %d, dirtied_page %d\n",
565 req->new_page, req->dirtied_page);
566 pr_err("\tnew_dent %d, mod_dent %d\n",
567 req->new_dent, req->mod_dent);
568 pr_err("\tidx_growth %d\n", req->idx_growth);
569 pr_err("\tdata_growth %d dd_growth %d\n",
570 req->data_growth, req->dd_growth);
571 spin_unlock(&dbg_lock);
575 void ubifs_dump_lstats(const struct ubifs_lp_stats *lst)
577 #ifndef CONFIG_UBIFS_SILENCE_DEBUG_DUMP
578 spin_lock(&dbg_lock);
579 pr_err("(pid %d) Lprops statistics: empty_lebs %d, idx_lebs %d\n",
580 current->pid, lst->empty_lebs, lst->idx_lebs);
581 pr_err("\ttaken_empty_lebs %d, total_free %lld, total_dirty %lld\n",
582 lst->taken_empty_lebs, lst->total_free, lst->total_dirty);
583 pr_err("\ttotal_used %lld, total_dark %lld, total_dead %lld\n",
584 lst->total_used, lst->total_dark, lst->total_dead);
585 spin_unlock(&dbg_lock);
590 void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)
594 struct ubifs_bud *bud;
595 struct ubifs_gced_idx_leb *idx_gc;
596 long long available, outstanding, free;
598 spin_lock(&c->space_lock);
599 spin_lock(&dbg_lock);
600 pr_err("(pid %d) Budgeting info: data budget sum %lld, total budget sum %lld\n",
601 current->pid, bi->data_growth + bi->dd_growth,
602 bi->data_growth + bi->dd_growth + bi->idx_growth);
603 pr_err("\tbudg_data_growth %lld, budg_dd_growth %lld, budg_idx_growth %lld\n",
604 bi->data_growth, bi->dd_growth, bi->idx_growth);
605 pr_err("\tmin_idx_lebs %d, old_idx_sz %llu, uncommitted_idx %lld\n",
606 bi->min_idx_lebs, bi->old_idx_sz, bi->uncommitted_idx);
607 pr_err("\tpage_budget %d, inode_budget %d, dent_budget %d\n",
608 bi->page_budget, bi->inode_budget, bi->dent_budget);
609 pr_err("\tnospace %u, nospace_rp %u\n", bi->nospace, bi->nospace_rp);
610 pr_err("\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
611 c->dark_wm, c->dead_wm, c->max_idx_node_sz);
615 * If we are dumping saved budgeting data, do not print
616 * additional information which is about the current state, not
617 * the old one which corresponded to the saved budgeting data.
621 pr_err("\tfreeable_cnt %d, calc_idx_sz %lld, idx_gc_cnt %d\n",
622 c->freeable_cnt, c->calc_idx_sz, c->idx_gc_cnt);
623 pr_err("\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, clean_zn_cnt %ld\n",
624 atomic_long_read(&c->dirty_pg_cnt),
625 atomic_long_read(&c->dirty_zn_cnt),
626 atomic_long_read(&c->clean_zn_cnt));
627 pr_err("\tgc_lnum %d, ihead_lnum %d\n", c->gc_lnum, c->ihead_lnum);
629 /* If we are in R/O mode, journal heads do not exist */
631 for (i = 0; i < c->jhead_cnt; i++)
632 pr_err("\tjhead %s\t LEB %d\n",
633 dbg_jhead(c->jheads[i].wbuf.jhead),
634 c->jheads[i].wbuf.lnum);
635 for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
636 bud = rb_entry(rb, struct ubifs_bud, rb);
637 pr_err("\tbud LEB %d\n", bud->lnum);
639 list_for_each_entry(bud, &c->old_buds, list)
640 pr_err("\told bud LEB %d\n", bud->lnum);
641 list_for_each_entry(idx_gc, &c->idx_gc, list)
642 pr_err("\tGC'ed idx LEB %d unmap %d\n",
643 idx_gc->lnum, idx_gc->unmap);
644 pr_err("\tcommit state %d\n", c->cmt_state);
646 /* Print budgeting predictions */
647 available = ubifs_calc_available(c, c->bi.min_idx_lebs);
648 outstanding = c->bi.data_growth + c->bi.dd_growth;
649 free = ubifs_get_free_space_nolock(c);
650 pr_err("Budgeting predictions:\n");
651 pr_err("\tavailable: %lld, outstanding %lld, free %lld\n",
652 available, outstanding, free);
654 spin_unlock(&dbg_lock);
655 spin_unlock(&c->space_lock);
658 void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)
663 void ubifs_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
665 #ifndef CONFIG_UBIFS_SILENCE_DEBUG_DUMP
666 int i, spc, dark = 0, dead = 0;
668 struct ubifs_bud *bud;
670 spc = lp->free + lp->dirty;
671 if (spc < c->dead_wm)
674 dark = ubifs_calc_dark(c, spc);
676 if (lp->flags & LPROPS_INDEX)
677 pr_err("LEB %-7d free %-8d dirty %-8d used %-8d free + dirty %-8d flags %#x (",
678 lp->lnum, lp->free, lp->dirty, c->leb_size - spc, spc,
681 pr_err("LEB %-7d free %-8d dirty %-8d used %-8d free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d flags %#-4x (",
682 lp->lnum, lp->free, lp->dirty, c->leb_size - spc, spc,
683 dark, dead, (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags);
685 if (lp->flags & LPROPS_TAKEN) {
686 if (lp->flags & LPROPS_INDEX)
687 pr_cont("index, taken");
693 if (lp->flags & LPROPS_INDEX) {
694 switch (lp->flags & LPROPS_CAT_MASK) {
695 case LPROPS_DIRTY_IDX:
698 case LPROPS_FRDI_IDX:
699 s = "freeable index";
705 switch (lp->flags & LPROPS_CAT_MASK) {
707 s = "not categorized";
718 case LPROPS_FREEABLE:
729 for (rb = rb_first((struct rb_root *)&c->buds); rb; rb = rb_next(rb)) {
730 bud = rb_entry(rb, struct ubifs_bud, rb);
731 if (bud->lnum == lp->lnum) {
733 for (i = 0; i < c->jhead_cnt; i++) {
735 * Note, if we are in R/O mode or in the middle
736 * of mounting/re-mounting, the write-buffers do
740 lp->lnum == c->jheads[i].wbuf.lnum) {
741 pr_cont(", jhead %s", dbg_jhead(i));
746 pr_cont(", bud of jhead %s",
747 dbg_jhead(bud->jhead));
750 if (lp->lnum == c->gc_lnum)
756 void ubifs_dump_lprops(struct ubifs_info *c)
759 struct ubifs_lprops lp;
760 struct ubifs_lp_stats lst;
762 pr_err("(pid %d) start dumping LEB properties\n", current->pid);
763 ubifs_get_lp_stats(c, &lst);
764 ubifs_dump_lstats(&lst);
766 for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
767 err = ubifs_read_one_lp(c, lnum, &lp);
769 ubifs_err(c, "cannot read lprops for LEB %d", lnum);
773 ubifs_dump_lprop(c, &lp);
775 pr_err("(pid %d) finish dumping LEB properties\n", current->pid);
778 void ubifs_dump_lpt_info(struct ubifs_info *c)
780 #ifndef CONFIG_UBIFS_SILENCE_DEBUG_DUMP
783 spin_lock(&dbg_lock);
784 pr_err("(pid %d) dumping LPT information\n", current->pid);
785 pr_err("\tlpt_sz: %lld\n", c->lpt_sz);
786 pr_err("\tpnode_sz: %d\n", c->pnode_sz);
787 pr_err("\tnnode_sz: %d\n", c->nnode_sz);
788 pr_err("\tltab_sz: %d\n", c->ltab_sz);
789 pr_err("\tlsave_sz: %d\n", c->lsave_sz);
790 pr_err("\tbig_lpt: %d\n", c->big_lpt);
791 pr_err("\tlpt_hght: %d\n", c->lpt_hght);
792 pr_err("\tpnode_cnt: %d\n", c->pnode_cnt);
793 pr_err("\tnnode_cnt: %d\n", c->nnode_cnt);
794 pr_err("\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt);
795 pr_err("\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt);
796 pr_err("\tlsave_cnt: %d\n", c->lsave_cnt);
797 pr_err("\tspace_bits: %d\n", c->space_bits);
798 pr_err("\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
799 pr_err("\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
800 pr_err("\tlpt_spc_bits: %d\n", c->lpt_spc_bits);
801 pr_err("\tpcnt_bits: %d\n", c->pcnt_bits);
802 pr_err("\tlnum_bits: %d\n", c->lnum_bits);
803 pr_err("\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
804 pr_err("\tLPT head is at %d:%d\n",
805 c->nhead_lnum, c->nhead_offs);
806 pr_err("\tLPT ltab is at %d:%d\n", c->ltab_lnum, c->ltab_offs);
808 pr_err("\tLPT lsave is at %d:%d\n",
809 c->lsave_lnum, c->lsave_offs);
810 for (i = 0; i < c->lpt_lebs; i++)
811 pr_err("\tLPT LEB %d free %d dirty %d tgc %d cmt %d\n",
812 i + c->lpt_first, c->ltab[i].free, c->ltab[i].dirty,
813 c->ltab[i].tgc, c->ltab[i].cmt);
814 spin_unlock(&dbg_lock);
818 void ubifs_dump_sleb(const struct ubifs_info *c,
819 const struct ubifs_scan_leb *sleb, int offs)
821 #ifndef CONFIG_UBIFS_SILENCE_DEBUG_DUMP
822 struct ubifs_scan_node *snod;
824 pr_err("(pid %d) start dumping scanned data from LEB %d:%d\n",
825 current->pid, sleb->lnum, offs);
827 list_for_each_entry(snod, &sleb->nodes, list) {
829 pr_err("Dumping node at LEB %d:%d len %d\n",
830 sleb->lnum, snod->offs, snod->len);
831 ubifs_dump_node(c, snod->node);
836 void ubifs_dump_leb(const struct ubifs_info *c, int lnum)
838 #ifndef CONFIG_UBIFS_SILENCE_DEBUG_DUMP
839 struct ubifs_scan_leb *sleb;
840 struct ubifs_scan_node *snod;
843 pr_err("(pid %d) start dumping LEB %d\n", current->pid, lnum);
845 buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
847 ubifs_err(c, "cannot allocate memory for dumping LEB %d", lnum);
851 sleb = ubifs_scan(c, lnum, 0, buf, 0);
853 ubifs_err(c, "scan error %d", (int)PTR_ERR(sleb));
857 pr_err("LEB %d has %d nodes ending at %d\n", lnum,
858 sleb->nodes_cnt, sleb->endpt);
860 list_for_each_entry(snod, &sleb->nodes, list) {
862 pr_err("Dumping node at LEB %d:%d len %d\n", lnum,
863 snod->offs, snod->len);
864 ubifs_dump_node(c, snod->node);
867 pr_err("(pid %d) finish dumping LEB %d\n", current->pid, lnum);
868 ubifs_scan_destroy(sleb);
876 void ubifs_dump_znode(const struct ubifs_info *c,
877 const struct ubifs_znode *znode)
879 #ifndef CONFIG_UBIFS_SILENCE_DEBUG_DUMP
881 const struct ubifs_zbranch *zbr;
882 char key_buf[DBG_KEY_BUF_LEN];
884 spin_lock(&dbg_lock);
886 zbr = &znode->parent->zbranch[znode->iip];
890 pr_err("znode %p, LEB %d:%d len %d parent %p iip %d level %d child_cnt %d flags %lx\n",
891 znode, zbr->lnum, zbr->offs, zbr->len, znode->parent, znode->iip,
892 znode->level, znode->child_cnt, znode->flags);
894 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
895 spin_unlock(&dbg_lock);
899 pr_err("zbranches:\n");
900 for (n = 0; n < znode->child_cnt; n++) {
901 zbr = &znode->zbranch[n];
902 if (znode->level > 0)
903 pr_err("\t%d: znode %p LEB %d:%d len %d key %s\n",
904 n, zbr->znode, zbr->lnum, zbr->offs, zbr->len,
905 dbg_snprintf_key(c, &zbr->key, key_buf,
908 pr_err("\t%d: LNC %p LEB %d:%d len %d key %s\n",
909 n, zbr->znode, zbr->lnum, zbr->offs, zbr->len,
910 dbg_snprintf_key(c, &zbr->key, key_buf,
913 spin_unlock(&dbg_lock);
917 void ubifs_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
919 #ifndef CONFIG_UBIFS_SILENCE_DEBUG_DUMP
922 pr_err("(pid %d) start dumping heap cat %d (%d elements)\n",
923 current->pid, cat, heap->cnt);
924 for (i = 0; i < heap->cnt; i++) {
925 struct ubifs_lprops *lprops = heap->arr[i];
927 pr_err("\t%d. LEB %d hpos %d free %d dirty %d flags %d\n",
928 i, lprops->lnum, lprops->hpos, lprops->free,
929 lprops->dirty, lprops->flags);
931 pr_err("(pid %d) finish dumping heap\n", current->pid);
935 void ubifs_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
936 struct ubifs_nnode *parent, int iip)
938 #ifndef CONFIG_UBIFS_SILENCE_DEBUG_DUMP
941 pr_err("(pid %d) dumping pnode:\n", current->pid);
942 pr_err("\taddress %zx parent %zx cnext %zx\n",
943 (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
944 pr_err("\tflags %lu iip %d level %d num %d\n",
945 pnode->flags, iip, pnode->level, pnode->num);
946 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
947 struct ubifs_lprops *lp = &pnode->lprops[i];
949 pr_err("\t%d: free %d dirty %d flags %d lnum %d\n",
950 i, lp->free, lp->dirty, lp->flags, lp->lnum);
955 void ubifs_dump_tnc(struct ubifs_info *c)
957 #ifndef CONFIG_UBIFS_SILENCE_DEBUG_DUMP
958 struct ubifs_znode *znode;
962 pr_err("(pid %d) start dumping TNC tree\n", current->pid);
963 znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
964 level = znode->level;
965 pr_err("== Level %d ==\n", level);
967 if (level != znode->level) {
968 level = znode->level;
969 pr_err("== Level %d ==\n", level);
971 ubifs_dump_znode(c, znode);
972 znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
974 pr_err("(pid %d) finish dumping TNC tree\n", current->pid);
978 #ifndef CONFIG_UBIFS_SILENCE_DEBUG_DUMP
979 static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
982 ubifs_dump_znode(c, znode);
988 * ubifs_dump_index - dump the on-flash index.
989 * @c: UBIFS file-system description object
991 * This function dumps whole UBIFS indexing B-tree, unlike 'ubifs_dump_tnc()'
992 * which dumps only in-memory znodes and does not read znodes which from flash.
994 void ubifs_dump_index(struct ubifs_info *c)
996 #ifndef CONFIG_UBIFS_SILENCE_DEBUG_DUMP
997 dbg_walk_index(c, NULL, dump_znode, NULL);
1003 * dbg_save_space_info - save information about flash space.
1004 * @c: UBIFS file-system description object
1006 * This function saves information about UBIFS free space, dirty space, etc, in
1007 * order to check it later.
1009 void dbg_save_space_info(struct ubifs_info *c)
1011 struct ubifs_debug_info *d = c->dbg;
1014 spin_lock(&c->space_lock);
1015 memcpy(&d->saved_lst, &c->lst, sizeof(struct ubifs_lp_stats));
1016 memcpy(&d->saved_bi, &c->bi, sizeof(struct ubifs_budg_info));
1017 d->saved_idx_gc_cnt = c->idx_gc_cnt;
1020 * We use a dirty hack here and zero out @c->freeable_cnt, because it
1021 * affects the free space calculations, and UBIFS might not know about
1022 * all freeable eraseblocks. Indeed, we know about freeable eraseblocks
1023 * only when we read their lprops, and we do this only lazily, upon the
1024 * need. So at any given point of time @c->freeable_cnt might be not
1027 * Just one example about the issue we hit when we did not zero
1029 * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the
1030 * amount of free space in @d->saved_free
1031 * 2. We re-mount R/W, which makes UBIFS to read the "lsave"
1032 * information from flash, where we cache LEBs from various
1033 * categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()'
1034 * -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()'
1035 * -> 'ubifs_get_pnode()' -> 'update_cats()'
1036 * -> 'ubifs_add_to_cat()').
1037 * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt
1039 * 4. We calculate the amount of free space when the re-mount is
1040 * finished in 'dbg_check_space_info()' and it does not match
1043 freeable_cnt = c->freeable_cnt;
1044 c->freeable_cnt = 0;
1045 d->saved_free = ubifs_get_free_space_nolock(c);
1046 c->freeable_cnt = freeable_cnt;
1047 spin_unlock(&c->space_lock);
1051 * dbg_check_space_info - check flash space information.
1052 * @c: UBIFS file-system description object
1054 * This function compares current flash space information with the information
1055 * which was saved when the 'dbg_save_space_info()' function was called.
1056 * Returns zero if the information has not changed, and %-EINVAL it it has
1059 int dbg_check_space_info(struct ubifs_info *c)
1061 struct ubifs_debug_info *d = c->dbg;
1062 struct ubifs_lp_stats lst;
1066 spin_lock(&c->space_lock);
1067 freeable_cnt = c->freeable_cnt;
1068 c->freeable_cnt = 0;
1069 free = ubifs_get_free_space_nolock(c);
1070 c->freeable_cnt = freeable_cnt;
1071 spin_unlock(&c->space_lock);
1073 if (free != d->saved_free) {
1074 ubifs_err(c, "free space changed from %lld to %lld",
1075 d->saved_free, free);
1082 ubifs_msg(c, "saved lprops statistics dump");
1083 ubifs_dump_lstats(&d->saved_lst);
1084 ubifs_msg(c, "saved budgeting info dump");
1085 ubifs_dump_budg(c, &d->saved_bi);
1086 ubifs_msg(c, "saved idx_gc_cnt %d", d->saved_idx_gc_cnt);
1087 ubifs_msg(c, "current lprops statistics dump");
1088 ubifs_get_lp_stats(c, &lst);
1089 ubifs_dump_lstats(&lst);
1090 ubifs_msg(c, "current budgeting info dump");
1091 ubifs_dump_budg(c, &c->bi);
1097 * dbg_check_synced_i_size - check synchronized inode size.
1098 * @c: UBIFS file-system description object
1099 * @inode: inode to check
1101 * If inode is clean, synchronized inode size has to be equivalent to current
1102 * inode size. This function has to be called only for locked inodes (@i_mutex
1103 * has to be locked). Returns %0 if synchronized inode size if correct, and
1106 int dbg_check_synced_i_size(const struct ubifs_info *c, struct inode *inode)
1109 struct ubifs_inode *ui = ubifs_inode(inode);
1111 if (!dbg_is_chk_gen(c))
1113 if (!S_ISREG(inode->i_mode))
1116 mutex_lock(&ui->ui_mutex);
1117 spin_lock(&ui->ui_lock);
1118 if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
1119 ubifs_err(c, "ui_size is %lld, synced_i_size is %lld, but inode is clean",
1120 ui->ui_size, ui->synced_i_size);
1121 ubifs_err(c, "i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
1122 inode->i_mode, i_size_read(inode));
1126 spin_unlock(&ui->ui_lock);
1127 mutex_unlock(&ui->ui_mutex);
1132 * dbg_check_dir - check directory inode size and link count.
1133 * @c: UBIFS file-system description object
1134 * @dir: the directory to calculate size for
1135 * @size: the result is returned here
1137 * This function makes sure that directory size and link count are correct.
1138 * Returns zero in case of success and a negative error code in case of
1141 * Note, it is good idea to make sure the @dir->i_mutex is locked before
1142 * calling this function.
1144 int dbg_check_dir(struct ubifs_info *c, const struct inode *dir)
1146 unsigned int nlink = 2;
1147 union ubifs_key key;
1148 struct ubifs_dent_node *dent, *pdent = NULL;
1149 struct qstr nm = { .name = NULL };
1150 loff_t size = UBIFS_INO_NODE_SZ;
1152 if (!dbg_is_chk_gen(c))
1155 if (!S_ISDIR(dir->i_mode))
1158 lowest_dent_key(c, &key, dir->i_ino);
1162 dent = ubifs_tnc_next_ent(c, &key, &nm);
1164 err = PTR_ERR(dent);
1170 nm.name = dent->name;
1171 nm.len = le16_to_cpu(dent->nlen);
1172 size += CALC_DENT_SIZE(nm.len);
1173 if (dent->type == UBIFS_ITYPE_DIR)
1177 key_read(c, &dent->key, &key);
1181 if (i_size_read(dir) != size) {
1182 ubifs_err(c, "directory inode %lu has size %llu, but calculated size is %llu",
1183 dir->i_ino, (unsigned long long)i_size_read(dir),
1184 (unsigned long long)size);
1185 ubifs_dump_inode(c, dir);
1189 if (dir->i_nlink != nlink) {
1190 ubifs_err(c, "directory inode %lu has nlink %u, but calculated nlink is %u",
1191 dir->i_ino, dir->i_nlink, nlink);
1192 ubifs_dump_inode(c, dir);
1201 * dbg_check_key_order - make sure that colliding keys are properly ordered.
1202 * @c: UBIFS file-system description object
1203 * @zbr1: first zbranch
1204 * @zbr2: following zbranch
1206 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
1207 * names of the direntries/xentries which are referred by the keys. This
1208 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
1209 * sure the name of direntry/xentry referred by @zbr1 is less than
1210 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
1211 * and a negative error code in case of failure.
1213 static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
1214 struct ubifs_zbranch *zbr2)
1216 int err, nlen1, nlen2, cmp;
1217 struct ubifs_dent_node *dent1, *dent2;
1218 union ubifs_key key;
1219 char key_buf[DBG_KEY_BUF_LEN];
1221 ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
1222 dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1225 dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1231 err = ubifs_tnc_read_node(c, zbr1, dent1);
1234 err = ubifs_validate_entry(c, dent1);
1238 err = ubifs_tnc_read_node(c, zbr2, dent2);
1241 err = ubifs_validate_entry(c, dent2);
1245 /* Make sure node keys are the same as in zbranch */
1247 key_read(c, &dent1->key, &key);
1248 if (keys_cmp(c, &zbr1->key, &key)) {
1249 ubifs_err(c, "1st entry at %d:%d has key %s", zbr1->lnum,
1250 zbr1->offs, dbg_snprintf_key(c, &key, key_buf,
1252 ubifs_err(c, "but it should have key %s according to tnc",
1253 dbg_snprintf_key(c, &zbr1->key, key_buf,
1255 ubifs_dump_node(c, dent1);
1259 key_read(c, &dent2->key, &key);
1260 if (keys_cmp(c, &zbr2->key, &key)) {
1261 ubifs_err(c, "2nd entry at %d:%d has key %s", zbr1->lnum,
1262 zbr1->offs, dbg_snprintf_key(c, &key, key_buf,
1264 ubifs_err(c, "but it should have key %s according to tnc",
1265 dbg_snprintf_key(c, &zbr2->key, key_buf,
1267 ubifs_dump_node(c, dent2);
1271 nlen1 = le16_to_cpu(dent1->nlen);
1272 nlen2 = le16_to_cpu(dent2->nlen);
1274 cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
1275 if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
1279 if (cmp == 0 && nlen1 == nlen2)
1280 ubifs_err(c, "2 xent/dent nodes with the same name");
1282 ubifs_err(c, "bad order of colliding key %s",
1283 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
1285 ubifs_msg(c, "first node at %d:%d\n", zbr1->lnum, zbr1->offs);
1286 ubifs_dump_node(c, dent1);
1287 ubifs_msg(c, "second node at %d:%d\n", zbr2->lnum, zbr2->offs);
1288 ubifs_dump_node(c, dent2);
1297 * dbg_check_znode - check if znode is all right.
1298 * @c: UBIFS file-system description object
1299 * @zbr: zbranch which points to this znode
1301 * This function makes sure that znode referred to by @zbr is all right.
1302 * Returns zero if it is, and %-EINVAL if it is not.
1304 static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
1306 struct ubifs_znode *znode = zbr->znode;
1307 struct ubifs_znode *zp = znode->parent;
1310 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
1314 if (znode->level < 0) {
1318 if (znode->iip < 0 || znode->iip >= c->fanout) {
1324 /* Only dirty zbranch may have no on-flash nodes */
1325 if (!ubifs_zn_dirty(znode)) {
1330 if (ubifs_zn_dirty(znode)) {
1332 * If znode is dirty, its parent has to be dirty as well. The
1333 * order of the operation is important, so we have to have
1337 if (zp && !ubifs_zn_dirty(zp)) {
1339 * The dirty flag is atomic and is cleared outside the
1340 * TNC mutex, so znode's dirty flag may now have
1341 * been cleared. The child is always cleared before the
1342 * parent, so we just need to check again.
1345 if (ubifs_zn_dirty(znode)) {
1353 const union ubifs_key *min, *max;
1355 if (znode->level != zp->level - 1) {
1360 /* Make sure the 'parent' pointer in our znode is correct */
1361 err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
1363 /* This zbranch does not exist in the parent */
1368 if (znode->iip >= zp->child_cnt) {
1373 if (znode->iip != n) {
1374 /* This may happen only in case of collisions */
1375 if (keys_cmp(c, &zp->zbranch[n].key,
1376 &zp->zbranch[znode->iip].key)) {
1384 * Make sure that the first key in our znode is greater than or
1385 * equal to the key in the pointing zbranch.
1388 cmp = keys_cmp(c, min, &znode->zbranch[0].key);
1394 if (n + 1 < zp->child_cnt) {
1395 max = &zp->zbranch[n + 1].key;
1398 * Make sure the last key in our znode is less or
1399 * equivalent than the key in the zbranch which goes
1400 * after our pointing zbranch.
1402 cmp = keys_cmp(c, max,
1403 &znode->zbranch[znode->child_cnt - 1].key);
1410 /* This may only be root znode */
1411 if (zbr != &c->zroot) {
1418 * Make sure that next key is greater or equivalent then the previous
1421 for (n = 1; n < znode->child_cnt; n++) {
1422 cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
1423 &znode->zbranch[n].key);
1429 /* This can only be keys with colliding hash */
1430 if (!is_hash_key(c, &znode->zbranch[n].key)) {
1435 if (znode->level != 0 || c->replaying)
1439 * Colliding keys should follow binary order of
1440 * corresponding xentry/dentry names.
1442 err = dbg_check_key_order(c, &znode->zbranch[n - 1],
1443 &znode->zbranch[n]);
1453 for (n = 0; n < znode->child_cnt; n++) {
1454 if (!znode->zbranch[n].znode &&
1455 (znode->zbranch[n].lnum == 0 ||
1456 znode->zbranch[n].len == 0)) {
1461 if (znode->zbranch[n].lnum != 0 &&
1462 znode->zbranch[n].len == 0) {
1467 if (znode->zbranch[n].lnum == 0 &&
1468 znode->zbranch[n].len != 0) {
1473 if (znode->zbranch[n].lnum == 0 &&
1474 znode->zbranch[n].offs != 0) {
1479 if (znode->level != 0 && znode->zbranch[n].znode)
1480 if (znode->zbranch[n].znode->parent != znode) {
1489 ubifs_err(c, "failed, error %d", err);
1490 ubifs_msg(c, "dump of the znode");
1491 ubifs_dump_znode(c, znode);
1493 ubifs_msg(c, "dump of the parent znode");
1494 ubifs_dump_znode(c, zp);
1501 int dbg_check_dir(struct ubifs_info *c, const struct inode *dir)
1506 void dbg_debugfs_exit_fs(struct ubifs_info *c)
1511 int ubifs_debugging_init(struct ubifs_info *c)
1515 void ubifs_debugging_exit(struct ubifs_info *c)
1518 int dbg_check_filesystem(struct ubifs_info *c)
1522 int dbg_debugfs_init_fs(struct ubifs_info *c)
1530 * dbg_check_tnc - check TNC tree.
1531 * @c: UBIFS file-system description object
1532 * @extra: do extra checks that are possible at start commit
1534 * This function traverses whole TNC tree and checks every znode. Returns zero
1535 * if everything is all right and %-EINVAL if something is wrong with TNC.
1537 int dbg_check_tnc(struct ubifs_info *c, int extra)
1539 struct ubifs_znode *znode;
1540 long clean_cnt = 0, dirty_cnt = 0;
1543 if (!dbg_is_chk_index(c))
1546 ubifs_assert(mutex_is_locked(&c->tnc_mutex));
1547 if (!c->zroot.znode)
1550 znode = ubifs_tnc_postorder_first(c->zroot.znode);
1552 struct ubifs_znode *prev;
1553 struct ubifs_zbranch *zbr;
1558 zbr = &znode->parent->zbranch[znode->iip];
1560 err = dbg_check_znode(c, zbr);
1565 if (ubifs_zn_dirty(znode))
1572 znode = ubifs_tnc_postorder_next(znode);
1577 * If the last key of this znode is equivalent to the first key
1578 * of the next znode (collision), then check order of the keys.
1580 last = prev->child_cnt - 1;
1581 if (prev->level == 0 && znode->level == 0 && !c->replaying &&
1582 !keys_cmp(c, &prev->zbranch[last].key,
1583 &znode->zbranch[0].key)) {
1584 err = dbg_check_key_order(c, &prev->zbranch[last],
1585 &znode->zbranch[0]);
1589 ubifs_msg(c, "first znode");
1590 ubifs_dump_znode(c, prev);
1591 ubifs_msg(c, "second znode");
1592 ubifs_dump_znode(c, znode);
1599 if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
1600 ubifs_err(c, "incorrect clean_zn_cnt %ld, calculated %ld",
1601 atomic_long_read(&c->clean_zn_cnt),
1605 if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
1606 ubifs_err(c, "incorrect dirty_zn_cnt %ld, calculated %ld",
1607 atomic_long_read(&c->dirty_zn_cnt),
1616 int dbg_check_tnc(struct ubifs_info *c, int extra)
1623 * dbg_walk_index - walk the on-flash index.
1624 * @c: UBIFS file-system description object
1625 * @leaf_cb: called for each leaf node
1626 * @znode_cb: called for each indexing node
1627 * @priv: private data which is passed to callbacks
1629 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1630 * node and @znode_cb for each indexing node. Returns zero in case of success
1631 * and a negative error code in case of failure.
1633 * It would be better if this function removed every znode it pulled to into
1634 * the TNC, so that the behavior more closely matched the non-debugging
1637 int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1638 dbg_znode_callback znode_cb, void *priv)
1641 struct ubifs_zbranch *zbr;
1642 struct ubifs_znode *znode, *child;
1644 mutex_lock(&c->tnc_mutex);
1645 /* If the root indexing node is not in TNC - pull it */
1646 if (!c->zroot.znode) {
1647 c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1648 if (IS_ERR(c->zroot.znode)) {
1649 err = PTR_ERR(c->zroot.znode);
1650 c->zroot.znode = NULL;
1656 * We are going to traverse the indexing tree in the postorder manner.
1657 * Go down and find the leftmost indexing node where we are going to
1660 znode = c->zroot.znode;
1661 while (znode->level > 0) {
1662 zbr = &znode->zbranch[0];
1665 child = ubifs_load_znode(c, zbr, znode, 0);
1666 if (IS_ERR(child)) {
1667 err = PTR_ERR(child);
1676 /* Iterate over all indexing nodes */
1683 err = znode_cb(c, znode, priv);
1685 ubifs_err(c, "znode checking function returned error %d",
1687 ubifs_dump_znode(c, znode);
1691 if (leaf_cb && znode->level == 0) {
1692 for (idx = 0; idx < znode->child_cnt; idx++) {
1693 zbr = &znode->zbranch[idx];
1694 err = leaf_cb(c, zbr, priv);
1696 ubifs_err(c, "leaf checking function returned error %d, for leaf at LEB %d:%d",
1697 err, zbr->lnum, zbr->offs);
1706 idx = znode->iip + 1;
1707 znode = znode->parent;
1708 if (idx < znode->child_cnt) {
1709 /* Switch to the next index in the parent */
1710 zbr = &znode->zbranch[idx];
1713 child = ubifs_load_znode(c, zbr, znode, idx);
1714 if (IS_ERR(child)) {
1715 err = PTR_ERR(child);
1723 * This is the last child, switch to the parent and
1728 /* Go to the lowest leftmost znode in the new sub-tree */
1729 while (znode->level > 0) {
1730 zbr = &znode->zbranch[0];
1733 child = ubifs_load_znode(c, zbr, znode, 0);
1734 if (IS_ERR(child)) {
1735 err = PTR_ERR(child);
1744 mutex_unlock(&c->tnc_mutex);
1749 zbr = &znode->parent->zbranch[znode->iip];
1752 ubifs_msg(c, "dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1753 ubifs_dump_znode(c, znode);
1755 mutex_unlock(&c->tnc_mutex);
1760 * add_size - add znode size to partially calculated index size.
1761 * @c: UBIFS file-system description object
1762 * @znode: znode to add size for
1763 * @priv: partially calculated index size
1765 * This is a helper function for 'dbg_check_idx_size()' which is called for
1766 * every indexing node and adds its size to the 'long long' variable pointed to
1769 static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1771 long long *idx_size = priv;
1774 add = ubifs_idx_node_sz(c, znode->child_cnt);
1775 add = ALIGN(add, 8);
1781 * dbg_check_idx_size - check index size.
1782 * @c: UBIFS file-system description object
1783 * @idx_size: size to check
1785 * This function walks the UBIFS index, calculates its size and checks that the
1786 * size is equivalent to @idx_size. Returns zero in case of success and a
1787 * negative error code in case of failure.
1789 int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
1794 if (!dbg_is_chk_index(c))
1797 err = dbg_walk_index(c, NULL, add_size, &calc);
1799 ubifs_err(c, "error %d while walking the index", err);
1803 if (calc != idx_size) {
1804 ubifs_err(c, "index size check failed: calculated size is %lld, should be %lld",
1815 * struct fsck_inode - information about an inode used when checking the file-system.
1816 * @rb: link in the RB-tree of inodes
1817 * @inum: inode number
1818 * @mode: inode type, permissions, etc
1819 * @nlink: inode link count
1820 * @xattr_cnt: count of extended attributes
1821 * @references: how many directory/xattr entries refer this inode (calculated
1822 * while walking the index)
1823 * @calc_cnt: for directory inode count of child directories
1824 * @size: inode size (read from on-flash inode)
1825 * @xattr_sz: summary size of all extended attributes (read from on-flash
1827 * @calc_sz: for directories calculated directory size
1828 * @calc_xcnt: count of extended attributes
1829 * @calc_xsz: calculated summary size of all extended attributes
1830 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1831 * inode (read from on-flash inode)
1832 * @calc_xnms: calculated sum of lengths of all extended attribute names
1839 unsigned int xattr_cnt;
1843 unsigned int xattr_sz;
1845 long long calc_xcnt;
1847 unsigned int xattr_nms;
1848 long long calc_xnms;
1852 * struct fsck_data - private FS checking information.
1853 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1856 struct rb_root inodes;
1860 * add_inode - add inode information to RB-tree of inodes.
1861 * @c: UBIFS file-system description object
1862 * @fsckd: FS checking information
1863 * @ino: raw UBIFS inode to add
1865 * This is a helper function for 'check_leaf()' which adds information about
1866 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1867 * case of success and a negative error code in case of failure.
1869 static struct fsck_inode *add_inode(struct ubifs_info *c,
1870 struct fsck_data *fsckd,
1871 struct ubifs_ino_node *ino)
1873 struct rb_node **p, *parent = NULL;
1874 struct fsck_inode *fscki;
1875 ino_t inum = key_inum_flash(c, &ino->key);
1876 struct inode *inode;
1877 struct ubifs_inode *ui;
1879 p = &fsckd->inodes.rb_node;
1882 fscki = rb_entry(parent, struct fsck_inode, rb);
1883 if (inum < fscki->inum)
1885 else if (inum > fscki->inum)
1886 p = &(*p)->rb_right;
1891 if (inum > c->highest_inum) {
1892 ubifs_err(c, "too high inode number, max. is %lu",
1893 (unsigned long)c->highest_inum);
1894 return ERR_PTR(-EINVAL);
1897 fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
1899 return ERR_PTR(-ENOMEM);
1901 inode = ilookup(c->vfs_sb, inum);
1905 * If the inode is present in the VFS inode cache, use it instead of
1906 * the on-flash inode which might be out-of-date. E.g., the size might
1907 * be out-of-date. If we do not do this, the following may happen, for
1909 * 1. A power cut happens
1910 * 2. We mount the file-system R/O, the replay process fixes up the
1911 * inode size in the VFS cache, but on on-flash.
1912 * 3. 'check_leaf()' fails because it hits a data node beyond inode
1916 fscki->nlink = le32_to_cpu(ino->nlink);
1917 fscki->size = le64_to_cpu(ino->size);
1918 fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
1919 fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
1920 fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
1921 fscki->mode = le32_to_cpu(ino->mode);
1923 ui = ubifs_inode(inode);
1924 fscki->nlink = inode->i_nlink;
1925 fscki->size = inode->i_size;
1926 fscki->xattr_cnt = ui->xattr_cnt;
1927 fscki->xattr_sz = ui->xattr_size;
1928 fscki->xattr_nms = ui->xattr_names;
1929 fscki->mode = inode->i_mode;
1933 if (S_ISDIR(fscki->mode)) {
1934 fscki->calc_sz = UBIFS_INO_NODE_SZ;
1935 fscki->calc_cnt = 2;
1938 rb_link_node(&fscki->rb, parent, p);
1939 rb_insert_color(&fscki->rb, &fsckd->inodes);
1945 * search_inode - search inode in the RB-tree of inodes.
1946 * @fsckd: FS checking information
1947 * @inum: inode number to search
1949 * This is a helper function for 'check_leaf()' which searches inode @inum in
1950 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1951 * the inode was not found.
1953 static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
1956 struct fsck_inode *fscki;
1958 p = fsckd->inodes.rb_node;
1960 fscki = rb_entry(p, struct fsck_inode, rb);
1961 if (inum < fscki->inum)
1963 else if (inum > fscki->inum)
1972 * read_add_inode - read inode node and add it to RB-tree of inodes.
1973 * @c: UBIFS file-system description object
1974 * @fsckd: FS checking information
1975 * @inum: inode number to read
1977 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1978 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1979 * information pointer in case of success and a negative error code in case of
1982 static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1983 struct fsck_data *fsckd, ino_t inum)
1986 union ubifs_key key;
1987 struct ubifs_znode *znode;
1988 struct ubifs_zbranch *zbr;
1989 struct ubifs_ino_node *ino;
1990 struct fsck_inode *fscki;
1992 fscki = search_inode(fsckd, inum);
1996 ino_key_init(c, &key, inum);
1997 err = ubifs_lookup_level0(c, &key, &znode, &n);
1999 ubifs_err(c, "inode %lu not found in index", (unsigned long)inum);
2000 return ERR_PTR(-ENOENT);
2001 } else if (err < 0) {
2002 ubifs_err(c, "error %d while looking up inode %lu",
2003 err, (unsigned long)inum);
2004 return ERR_PTR(err);
2007 zbr = &znode->zbranch[n];
2008 if (zbr->len < UBIFS_INO_NODE_SZ) {
2009 ubifs_err(c, "bad node %lu node length %d",
2010 (unsigned long)inum, zbr->len);
2011 return ERR_PTR(-EINVAL);
2014 ino = kmalloc(zbr->len, GFP_NOFS);
2016 return ERR_PTR(-ENOMEM);
2018 err = ubifs_tnc_read_node(c, zbr, ino);
2020 ubifs_err(c, "cannot read inode node at LEB %d:%d, error %d",
2021 zbr->lnum, zbr->offs, err);
2023 return ERR_PTR(err);
2026 fscki = add_inode(c, fsckd, ino);
2028 if (IS_ERR(fscki)) {
2029 ubifs_err(c, "error %ld while adding inode %lu node",
2030 PTR_ERR(fscki), (unsigned long)inum);
2038 * check_leaf - check leaf node.
2039 * @c: UBIFS file-system description object
2040 * @zbr: zbranch of the leaf node to check
2041 * @priv: FS checking information
2043 * This is a helper function for 'dbg_check_filesystem()' which is called for
2044 * every single leaf node while walking the indexing tree. It checks that the
2045 * leaf node referred from the indexing tree exists, has correct CRC, and does
2046 * some other basic validation. This function is also responsible for building
2047 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
2048 * calculates reference count, size, etc for each inode in order to later
2049 * compare them to the information stored inside the inodes and detect possible
2050 * inconsistencies. Returns zero in case of success and a negative error code
2051 * in case of failure.
2053 static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
2058 struct ubifs_ch *ch;
2059 int err, type = key_type(c, &zbr->key);
2060 struct fsck_inode *fscki;
2062 if (zbr->len < UBIFS_CH_SZ) {
2063 ubifs_err(c, "bad leaf length %d (LEB %d:%d)",
2064 zbr->len, zbr->lnum, zbr->offs);
2068 node = kmalloc(zbr->len, GFP_NOFS);
2072 err = ubifs_tnc_read_node(c, zbr, node);
2074 ubifs_err(c, "cannot read leaf node at LEB %d:%d, error %d",
2075 zbr->lnum, zbr->offs, err);
2079 /* If this is an inode node, add it to RB-tree of inodes */
2080 if (type == UBIFS_INO_KEY) {
2081 fscki = add_inode(c, priv, node);
2082 if (IS_ERR(fscki)) {
2083 err = PTR_ERR(fscki);
2084 ubifs_err(c, "error %d while adding inode node", err);
2090 if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
2091 type != UBIFS_DATA_KEY) {
2092 ubifs_err(c, "unexpected node type %d at LEB %d:%d",
2093 type, zbr->lnum, zbr->offs);
2099 if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
2100 ubifs_err(c, "too high sequence number, max. is %llu",
2106 if (type == UBIFS_DATA_KEY) {
2108 struct ubifs_data_node *dn = node;
2110 ubifs_assert(zbr->len >= UBIFS_DATA_NODE_SZ);
2113 * Search the inode node this data node belongs to and insert
2114 * it to the RB-tree of inodes.
2116 inum = key_inum_flash(c, &dn->key);
2117 fscki = read_add_inode(c, priv, inum);
2118 if (IS_ERR(fscki)) {
2119 err = PTR_ERR(fscki);
2120 ubifs_err(c, "error %d while processing data node and trying to find inode node %lu",
2121 err, (unsigned long)inum);
2125 /* Make sure the data node is within inode size */
2126 blk_offs = key_block_flash(c, &dn->key);
2127 blk_offs <<= UBIFS_BLOCK_SHIFT;
2128 blk_offs += le32_to_cpu(dn->size);
2129 if (blk_offs > fscki->size) {
2130 ubifs_err(c, "data node at LEB %d:%d is not within inode size %lld",
2131 zbr->lnum, zbr->offs, fscki->size);
2137 struct ubifs_dent_node *dent = node;
2138 struct fsck_inode *fscki1;
2140 ubifs_assert(zbr->len >= UBIFS_DENT_NODE_SZ);
2142 err = ubifs_validate_entry(c, dent);
2147 * Search the inode node this entry refers to and the parent
2148 * inode node and insert them to the RB-tree of inodes.
2150 inum = le64_to_cpu(dent->inum);
2151 fscki = read_add_inode(c, priv, inum);
2152 if (IS_ERR(fscki)) {
2153 err = PTR_ERR(fscki);
2154 ubifs_err(c, "error %d while processing entry node and trying to find inode node %lu",
2155 err, (unsigned long)inum);
2159 /* Count how many direntries or xentries refers this inode */
2160 fscki->references += 1;
2162 inum = key_inum_flash(c, &dent->key);
2163 fscki1 = read_add_inode(c, priv, inum);
2164 if (IS_ERR(fscki1)) {
2165 err = PTR_ERR(fscki1);
2166 ubifs_err(c, "error %d while processing entry node and trying to find parent inode node %lu",
2167 err, (unsigned long)inum);
2171 nlen = le16_to_cpu(dent->nlen);
2172 if (type == UBIFS_XENT_KEY) {
2173 fscki1->calc_xcnt += 1;
2174 fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
2175 fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
2176 fscki1->calc_xnms += nlen;
2178 fscki1->calc_sz += CALC_DENT_SIZE(nlen);
2179 if (dent->type == UBIFS_ITYPE_DIR)
2180 fscki1->calc_cnt += 1;
2189 ubifs_msg(c, "dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
2190 ubifs_dump_node(c, node);
2197 * free_inodes - free RB-tree of inodes.
2198 * @fsckd: FS checking information
2200 static void free_inodes(struct fsck_data *fsckd)
2202 struct fsck_inode *fscki, *n;
2204 rbtree_postorder_for_each_entry_safe(fscki, n, &fsckd->inodes, rb)
2209 * check_inodes - checks all inodes.
2210 * @c: UBIFS file-system description object
2211 * @fsckd: FS checking information
2213 * This is a helper function for 'dbg_check_filesystem()' which walks the
2214 * RB-tree of inodes after the index scan has been finished, and checks that
2215 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
2216 * %-EINVAL if not, and a negative error code in case of failure.
2218 static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
2221 union ubifs_key key;
2222 struct ubifs_znode *znode;
2223 struct ubifs_zbranch *zbr;
2224 struct ubifs_ino_node *ino;
2225 struct fsck_inode *fscki;
2226 struct rb_node *this = rb_first(&fsckd->inodes);
2229 fscki = rb_entry(this, struct fsck_inode, rb);
2230 this = rb_next(this);
2232 if (S_ISDIR(fscki->mode)) {
2234 * Directories have to have exactly one reference (they
2235 * cannot have hardlinks), although root inode is an
2238 if (fscki->inum != UBIFS_ROOT_INO &&
2239 fscki->references != 1) {
2240 ubifs_err(c, "directory inode %lu has %d direntries which refer it, but should be 1",
2241 (unsigned long)fscki->inum,
2245 if (fscki->inum == UBIFS_ROOT_INO &&
2246 fscki->references != 0) {
2247 ubifs_err(c, "root inode %lu has non-zero (%d) direntries which refer it",
2248 (unsigned long)fscki->inum,
2252 if (fscki->calc_sz != fscki->size) {
2253 ubifs_err(c, "directory inode %lu size is %lld, but calculated size is %lld",
2254 (unsigned long)fscki->inum,
2255 fscki->size, fscki->calc_sz);
2258 if (fscki->calc_cnt != fscki->nlink) {
2259 ubifs_err(c, "directory inode %lu nlink is %d, but calculated nlink is %d",
2260 (unsigned long)fscki->inum,
2261 fscki->nlink, fscki->calc_cnt);
2265 if (fscki->references != fscki->nlink) {
2266 ubifs_err(c, "inode %lu nlink is %d, but calculated nlink is %d",
2267 (unsigned long)fscki->inum,
2268 fscki->nlink, fscki->references);
2272 if (fscki->xattr_sz != fscki->calc_xsz) {
2273 ubifs_err(c, "inode %lu has xattr size %u, but calculated size is %lld",
2274 (unsigned long)fscki->inum, fscki->xattr_sz,
2278 if (fscki->xattr_cnt != fscki->calc_xcnt) {
2279 ubifs_err(c, "inode %lu has %u xattrs, but calculated count is %lld",
2280 (unsigned long)fscki->inum,
2281 fscki->xattr_cnt, fscki->calc_xcnt);
2284 if (fscki->xattr_nms != fscki->calc_xnms) {
2285 ubifs_err(c, "inode %lu has xattr names' size %u, but calculated names' size is %lld",
2286 (unsigned long)fscki->inum, fscki->xattr_nms,
2295 /* Read the bad inode and dump it */
2296 ino_key_init(c, &key, fscki->inum);
2297 err = ubifs_lookup_level0(c, &key, &znode, &n);
2299 ubifs_err(c, "inode %lu not found in index",
2300 (unsigned long)fscki->inum);
2302 } else if (err < 0) {
2303 ubifs_err(c, "error %d while looking up inode %lu",
2304 err, (unsigned long)fscki->inum);
2308 zbr = &znode->zbranch[n];
2309 ino = kmalloc(zbr->len, GFP_NOFS);
2313 err = ubifs_tnc_read_node(c, zbr, ino);
2315 ubifs_err(c, "cannot read inode node at LEB %d:%d, error %d",
2316 zbr->lnum, zbr->offs, err);
2321 ubifs_msg(c, "dump of the inode %lu sitting in LEB %d:%d",
2322 (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
2323 ubifs_dump_node(c, ino);
2329 * dbg_check_filesystem - check the file-system.
2330 * @c: UBIFS file-system description object
2332 * This function checks the file system, namely:
2333 * o makes sure that all leaf nodes exist and their CRCs are correct;
2334 * o makes sure inode nlink, size, xattr size/count are correct (for all
2337 * The function reads whole indexing tree and all nodes, so it is pretty
2338 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2339 * not, and a negative error code in case of failure.
2341 int dbg_check_filesystem(struct ubifs_info *c)
2344 struct fsck_data fsckd;
2346 if (!dbg_is_chk_fs(c))
2349 fsckd.inodes = RB_ROOT;
2350 err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
2354 err = check_inodes(c, &fsckd);
2358 free_inodes(&fsckd);
2362 ubifs_err(c, "file-system check failed with error %d", err);
2364 free_inodes(&fsckd);
2369 * dbg_check_data_nodes_order - check that list of data nodes is sorted.
2370 * @c: UBIFS file-system description object
2371 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2373 * This function returns zero if the list of data nodes is sorted correctly,
2374 * and %-EINVAL if not.
2376 int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head)
2378 struct list_head *cur;
2379 struct ubifs_scan_node *sa, *sb;
2381 if (!dbg_is_chk_gen(c))
2384 for (cur = head->next; cur->next != head; cur = cur->next) {
2386 uint32_t blka, blkb;
2389 sa = container_of(cur, struct ubifs_scan_node, list);
2390 sb = container_of(cur->next, struct ubifs_scan_node, list);
2392 if (sa->type != UBIFS_DATA_NODE) {
2393 ubifs_err(c, "bad node type %d", sa->type);
2394 ubifs_dump_node(c, sa->node);
2397 if (sb->type != UBIFS_DATA_NODE) {
2398 ubifs_err(c, "bad node type %d", sb->type);
2399 ubifs_dump_node(c, sb->node);
2403 inuma = key_inum(c, &sa->key);
2404 inumb = key_inum(c, &sb->key);
2408 if (inuma > inumb) {
2409 ubifs_err(c, "larger inum %lu goes before inum %lu",
2410 (unsigned long)inuma, (unsigned long)inumb);
2414 blka = key_block(c, &sa->key);
2415 blkb = key_block(c, &sb->key);
2418 ubifs_err(c, "larger block %u goes before %u", blka, blkb);
2422 ubifs_err(c, "two data nodes for the same block");
2430 ubifs_dump_node(c, sa->node);
2431 ubifs_dump_node(c, sb->node);
2436 * dbg_check_nondata_nodes_order - check that list of data nodes is sorted.
2437 * @c: UBIFS file-system description object
2438 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2440 * This function returns zero if the list of non-data nodes is sorted correctly,
2441 * and %-EINVAL if not.
2443 int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head)
2445 struct list_head *cur;
2446 struct ubifs_scan_node *sa, *sb;
2448 if (!dbg_is_chk_gen(c))
2451 for (cur = head->next; cur->next != head; cur = cur->next) {
2453 uint32_t hasha, hashb;
2456 sa = container_of(cur, struct ubifs_scan_node, list);
2457 sb = container_of(cur->next, struct ubifs_scan_node, list);
2459 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2460 sa->type != UBIFS_XENT_NODE) {
2461 ubifs_err(c, "bad node type %d", sa->type);
2462 ubifs_dump_node(c, sa->node);
2465 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2466 sa->type != UBIFS_XENT_NODE) {
2467 ubifs_err(c, "bad node type %d", sb->type);
2468 ubifs_dump_node(c, sb->node);
2472 if (sa->type != UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2473 ubifs_err(c, "non-inode node goes before inode node");
2477 if (sa->type == UBIFS_INO_NODE && sb->type != UBIFS_INO_NODE)
2480 if (sa->type == UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2481 /* Inode nodes are sorted in descending size order */
2482 if (sa->len < sb->len) {
2483 ubifs_err(c, "smaller inode node goes first");
2490 * This is either a dentry or xentry, which should be sorted in
2491 * ascending (parent ino, hash) order.
2493 inuma = key_inum(c, &sa->key);
2494 inumb = key_inum(c, &sb->key);
2498 if (inuma > inumb) {
2499 ubifs_err(c, "larger inum %lu goes before inum %lu",
2500 (unsigned long)inuma, (unsigned long)inumb);
2504 hasha = key_block(c, &sa->key);
2505 hashb = key_block(c, &sb->key);
2507 if (hasha > hashb) {
2508 ubifs_err(c, "larger hash %u goes before %u",
2517 ubifs_msg(c, "dumping first node");
2518 ubifs_dump_node(c, sa->node);
2519 ubifs_msg(c, "dumping second node");
2520 ubifs_dump_node(c, sb->node);
2525 static inline int chance(unsigned int n, unsigned int out_of)
2527 return !!((prandom_u32() % out_of) + 1 <= n);
2531 static int power_cut_emulated(struct ubifs_info *c, int lnum, int write)
2533 struct ubifs_debug_info *d = c->dbg;
2535 ubifs_assert(dbg_is_tst_rcvry(c));
2538 /* First call - decide delay to the power cut */
2540 unsigned long delay;
2544 /* Fail within 1 minute */
2545 delay = prandom_u32() % 60000;
2546 d->pc_timeout = jiffies;
2547 d->pc_timeout += msecs_to_jiffies(delay);
2548 ubifs_warn(c, "failing after %lums", delay);
2551 delay = prandom_u32() % 10000;
2552 /* Fail within 10000 operations */
2553 d->pc_cnt_max = delay;
2554 ubifs_warn(c, "failing after %lu calls", delay);
2561 /* Determine if failure delay has expired */
2562 if (d->pc_delay == 1 && time_before(jiffies, d->pc_timeout))
2564 if (d->pc_delay == 2 && d->pc_cnt++ < d->pc_cnt_max)
2567 if (lnum == UBIFS_SB_LNUM) {
2568 if (write && chance(1, 2))
2572 ubifs_warn(c, "failing in super block LEB %d", lnum);
2573 } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2576 ubifs_warn(c, "failing in master LEB %d", lnum);
2577 } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2578 if (write && chance(99, 100))
2580 if (chance(399, 400))
2582 ubifs_warn(c, "failing in log LEB %d", lnum);
2583 } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2584 if (write && chance(7, 8))
2588 ubifs_warn(c, "failing in LPT LEB %d", lnum);
2589 } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2590 if (write && chance(1, 2))
2594 ubifs_warn(c, "failing in orphan LEB %d", lnum);
2595 } else if (lnum == c->ihead_lnum) {
2596 if (chance(99, 100))
2598 ubifs_warn(c, "failing in index head LEB %d", lnum);
2599 } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2602 ubifs_warn(c, "failing in GC head LEB %d", lnum);
2603 } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
2604 !ubifs_search_bud(c, lnum)) {
2607 ubifs_warn(c, "failing in non-bud LEB %d", lnum);
2608 } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
2609 c->cmt_state == COMMIT_RUNNING_REQUIRED) {
2610 if (chance(999, 1000))
2612 ubifs_warn(c, "failing in bud LEB %d commit running", lnum);
2614 if (chance(9999, 10000))
2616 ubifs_warn(c, "failing in bud LEB %d commit not running", lnum);
2620 ubifs_warn(c, "========== Power cut emulated ==========");
2625 static int corrupt_data(const struct ubifs_info *c, const void *buf,
2628 unsigned int from, to, ffs = chance(1, 2);
2629 unsigned char *p = (void *)buf;
2631 from = prandom_u32() % len;
2632 /* Corruption span max to end of write unit */
2633 to = min(len, ALIGN(from + 1, c->max_write_size));
2635 ubifs_warn(c, "filled bytes %u-%u with %s", from, to - 1,
2636 ffs ? "0xFFs" : "random data");
2639 memset(p + from, 0xFF, to - from);
2641 prandom_bytes(p + from, to - from);
2646 int dbg_leb_write(struct ubifs_info *c, int lnum, const void *buf,
2651 if (c->dbg->pc_happened)
2654 failing = power_cut_emulated(c, lnum, 1);
2656 len = corrupt_data(c, buf, len);
2657 ubifs_warn(c, "actually write %d bytes to LEB %d:%d (the buffer was corrupted)",
2660 err = ubi_leb_write(c->ubi, lnum, buf, offs, len);
2668 int dbg_leb_change(struct ubifs_info *c, int lnum, const void *buf,
2673 if (c->dbg->pc_happened)
2675 if (power_cut_emulated(c, lnum, 1))
2677 err = ubi_leb_change(c->ubi, lnum, buf, len);
2680 if (power_cut_emulated(c, lnum, 1))
2685 int dbg_leb_unmap(struct ubifs_info *c, int lnum)
2689 if (c->dbg->pc_happened)
2691 if (power_cut_emulated(c, lnum, 0))
2693 err = ubi_leb_unmap(c->ubi, lnum);
2696 if (power_cut_emulated(c, lnum, 0))
2701 int dbg_leb_map(struct ubifs_info *c, int lnum)
2705 if (c->dbg->pc_happened)
2707 if (power_cut_emulated(c, lnum, 0))
2709 err = ubi_leb_map(c->ubi, lnum);
2712 if (power_cut_emulated(c, lnum, 0))
2718 * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
2719 * contain the stuff specific to particular file-system mounts.
2721 static struct dentry *dfs_rootdir;
2723 static int dfs_file_open(struct inode *inode, struct file *file)
2725 file->private_data = inode->i_private;
2726 return nonseekable_open(inode, file);
2730 * provide_user_output - provide output to the user reading a debugfs file.
2731 * @val: boolean value for the answer
2732 * @u: the buffer to store the answer at
2733 * @count: size of the buffer
2734 * @ppos: position in the @u output buffer
2736 * This is a simple helper function which stores @val boolean value in the user
2737 * buffer when the user reads one of UBIFS debugfs files. Returns amount of
2738 * bytes written to @u in case of success and a negative error code in case of
2741 static int provide_user_output(int val, char __user *u, size_t count,
2753 return simple_read_from_buffer(u, count, ppos, buf, 2);
2756 static ssize_t dfs_file_read(struct file *file, char __user *u, size_t count,
2759 struct dentry *dent = file->f_path.dentry;
2760 struct ubifs_info *c = file->private_data;
2761 struct ubifs_debug_info *d = c->dbg;
2764 if (dent == d->dfs_chk_gen)
2766 else if (dent == d->dfs_chk_index)
2768 else if (dent == d->dfs_chk_orph)
2770 else if (dent == d->dfs_chk_lprops)
2771 val = d->chk_lprops;
2772 else if (dent == d->dfs_chk_fs)
2774 else if (dent == d->dfs_tst_rcvry)
2776 else if (dent == d->dfs_ro_error)
2781 return provide_user_output(val, u, count, ppos);
2785 * interpret_user_input - interpret user debugfs file input.
2786 * @u: user-provided buffer with the input
2787 * @count: buffer size
2789 * This is a helper function which interpret user input to a boolean UBIFS
2790 * debugfs file. Returns %0 or %1 in case of success and a negative error code
2791 * in case of failure.
2793 static int interpret_user_input(const char __user *u, size_t count)
2798 buf_size = min_t(size_t, count, (sizeof(buf) - 1));
2799 if (copy_from_user(buf, u, buf_size))
2804 else if (buf[0] == '0')
2810 static ssize_t dfs_file_write(struct file *file, const char __user *u,
2811 size_t count, loff_t *ppos)
2813 struct ubifs_info *c = file->private_data;
2814 struct ubifs_debug_info *d = c->dbg;
2815 struct dentry *dent = file->f_path.dentry;
2819 * TODO: this is racy - the file-system might have already been
2820 * unmounted and we'd oops in this case. The plan is to fix it with
2821 * help of 'iterate_supers_type()' which we should have in v3.0: when
2822 * a debugfs opened, we rember FS's UUID in file->private_data. Then
2823 * whenever we access the FS via a debugfs file, we iterate all UBIFS
2824 * superblocks and fine the one with the same UUID, and take the
2827 * The other way to go suggested by Al Viro is to create a separate
2828 * 'ubifs-debug' file-system instead.
2830 if (file->f_path.dentry == d->dfs_dump_lprops) {
2831 ubifs_dump_lprops(c);
2834 if (file->f_path.dentry == d->dfs_dump_budg) {
2835 ubifs_dump_budg(c, &c->bi);
2838 if (file->f_path.dentry == d->dfs_dump_tnc) {
2839 mutex_lock(&c->tnc_mutex);
2841 mutex_unlock(&c->tnc_mutex);
2845 val = interpret_user_input(u, count);
2849 if (dent == d->dfs_chk_gen)
2851 else if (dent == d->dfs_chk_index)
2853 else if (dent == d->dfs_chk_orph)
2855 else if (dent == d->dfs_chk_lprops)
2856 d->chk_lprops = val;
2857 else if (dent == d->dfs_chk_fs)
2859 else if (dent == d->dfs_tst_rcvry)
2861 else if (dent == d->dfs_ro_error)
2862 c->ro_error = !!val;
2869 static const struct file_operations dfs_fops = {
2870 .open = dfs_file_open,
2871 .read = dfs_file_read,
2872 .write = dfs_file_write,
2873 .owner = THIS_MODULE,
2874 .llseek = no_llseek,
2878 * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
2879 * @c: UBIFS file-system description object
2881 * This function creates all debugfs files for this instance of UBIFS. Returns
2882 * zero in case of success and a negative error code in case of failure.
2884 * Note, the only reason we have not merged this function with the
2885 * 'ubifs_debugging_init()' function is because it is better to initialize
2886 * debugfs interfaces at the very end of the mount process, and remove them at
2887 * the very beginning of the mount process.
2889 int dbg_debugfs_init_fs(struct ubifs_info *c)
2893 struct dentry *dent;
2894 struct ubifs_debug_info *d = c->dbg;
2896 if (!IS_ENABLED(CONFIG_DEBUG_FS))
2899 n = snprintf(d->dfs_dir_name, UBIFS_DFS_DIR_LEN + 1, UBIFS_DFS_DIR_NAME,
2900 c->vi.ubi_num, c->vi.vol_id);
2901 if (n == UBIFS_DFS_DIR_LEN) {
2902 /* The array size is too small */
2903 fname = UBIFS_DFS_DIR_NAME;
2904 dent = ERR_PTR(-EINVAL);
2908 fname = d->dfs_dir_name;
2909 dent = debugfs_create_dir(fname, dfs_rootdir);
2910 if (IS_ERR_OR_NULL(dent))
2914 fname = "dump_lprops";
2915 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2916 if (IS_ERR_OR_NULL(dent))
2918 d->dfs_dump_lprops = dent;
2920 fname = "dump_budg";
2921 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2922 if (IS_ERR_OR_NULL(dent))
2924 d->dfs_dump_budg = dent;
2927 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2928 if (IS_ERR_OR_NULL(dent))
2930 d->dfs_dump_tnc = dent;
2932 fname = "chk_general";
2933 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2935 if (IS_ERR_OR_NULL(dent))
2937 d->dfs_chk_gen = dent;
2939 fname = "chk_index";
2940 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2942 if (IS_ERR_OR_NULL(dent))
2944 d->dfs_chk_index = dent;
2946 fname = "chk_orphans";
2947 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2949 if (IS_ERR_OR_NULL(dent))
2951 d->dfs_chk_orph = dent;
2953 fname = "chk_lprops";
2954 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2956 if (IS_ERR_OR_NULL(dent))
2958 d->dfs_chk_lprops = dent;
2961 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2963 if (IS_ERR_OR_NULL(dent))
2965 d->dfs_chk_fs = dent;
2967 fname = "tst_recovery";
2968 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2970 if (IS_ERR_OR_NULL(dent))
2972 d->dfs_tst_rcvry = dent;
2975 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2977 if (IS_ERR_OR_NULL(dent))
2979 d->dfs_ro_error = dent;
2984 debugfs_remove_recursive(d->dfs_dir);
2986 err = dent ? PTR_ERR(dent) : -ENODEV;
2987 ubifs_err(c, "cannot create \"%s\" debugfs file or directory, error %d\n",
2993 * dbg_debugfs_exit_fs - remove all debugfs files.
2994 * @c: UBIFS file-system description object
2996 void dbg_debugfs_exit_fs(struct ubifs_info *c)
2998 if (IS_ENABLED(CONFIG_DEBUG_FS))
2999 debugfs_remove_recursive(c->dbg->dfs_dir);
3002 struct ubifs_global_debug_info ubifs_dbg;
3004 static struct dentry *dfs_chk_gen;
3005 static struct dentry *dfs_chk_index;
3006 static struct dentry *dfs_chk_orph;
3007 static struct dentry *dfs_chk_lprops;
3008 static struct dentry *dfs_chk_fs;
3009 static struct dentry *dfs_tst_rcvry;
3011 static ssize_t dfs_global_file_read(struct file *file, char __user *u,
3012 size_t count, loff_t *ppos)
3014 struct dentry *dent = file->f_path.dentry;
3017 if (dent == dfs_chk_gen)
3018 val = ubifs_dbg.chk_gen;
3019 else if (dent == dfs_chk_index)
3020 val = ubifs_dbg.chk_index;
3021 else if (dent == dfs_chk_orph)
3022 val = ubifs_dbg.chk_orph;
3023 else if (dent == dfs_chk_lprops)
3024 val = ubifs_dbg.chk_lprops;
3025 else if (dent == dfs_chk_fs)
3026 val = ubifs_dbg.chk_fs;
3027 else if (dent == dfs_tst_rcvry)
3028 val = ubifs_dbg.tst_rcvry;
3032 return provide_user_output(val, u, count, ppos);
3035 static ssize_t dfs_global_file_write(struct file *file, const char __user *u,
3036 size_t count, loff_t *ppos)
3038 struct dentry *dent = file->f_path.dentry;
3041 val = interpret_user_input(u, count);
3045 if (dent == dfs_chk_gen)
3046 ubifs_dbg.chk_gen = val;
3047 else if (dent == dfs_chk_index)
3048 ubifs_dbg.chk_index = val;
3049 else if (dent == dfs_chk_orph)
3050 ubifs_dbg.chk_orph = val;
3051 else if (dent == dfs_chk_lprops)
3052 ubifs_dbg.chk_lprops = val;
3053 else if (dent == dfs_chk_fs)
3054 ubifs_dbg.chk_fs = val;
3055 else if (dent == dfs_tst_rcvry)
3056 ubifs_dbg.tst_rcvry = val;
3063 static const struct file_operations dfs_global_fops = {
3064 .read = dfs_global_file_read,
3065 .write = dfs_global_file_write,
3066 .owner = THIS_MODULE,
3067 .llseek = no_llseek,
3071 * dbg_debugfs_init - initialize debugfs file-system.
3073 * UBIFS uses debugfs file-system to expose various debugging knobs to
3074 * user-space. This function creates "ubifs" directory in the debugfs
3075 * file-system. Returns zero in case of success and a negative error code in
3078 int dbg_debugfs_init(void)
3082 struct dentry *dent;
3084 if (!IS_ENABLED(CONFIG_DEBUG_FS))
3088 dent = debugfs_create_dir(fname, NULL);
3089 if (IS_ERR_OR_NULL(dent))
3093 fname = "chk_general";
3094 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3096 if (IS_ERR_OR_NULL(dent))
3100 fname = "chk_index";
3101 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3103 if (IS_ERR_OR_NULL(dent))
3105 dfs_chk_index = dent;
3107 fname = "chk_orphans";
3108 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3110 if (IS_ERR_OR_NULL(dent))
3112 dfs_chk_orph = dent;
3114 fname = "chk_lprops";
3115 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3117 if (IS_ERR_OR_NULL(dent))
3119 dfs_chk_lprops = dent;
3122 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3124 if (IS_ERR_OR_NULL(dent))
3128 fname = "tst_recovery";
3129 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3131 if (IS_ERR_OR_NULL(dent))
3133 dfs_tst_rcvry = dent;
3138 debugfs_remove_recursive(dfs_rootdir);
3140 err = dent ? PTR_ERR(dent) : -ENODEV;
3141 pr_err("UBIFS error (pid %d): cannot create \"%s\" debugfs file or directory, error %d\n",
3142 current->pid, fname, err);
3147 * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
3149 void dbg_debugfs_exit(void)
3151 if (IS_ENABLED(CONFIG_DEBUG_FS))
3152 debugfs_remove_recursive(dfs_rootdir);
3156 * ubifs_debugging_init - initialize UBIFS debugging.
3157 * @c: UBIFS file-system description object
3159 * This function initializes debugging-related data for the file system.
3160 * Returns zero in case of success and a negative error code in case of
3163 int ubifs_debugging_init(struct ubifs_info *c)
3165 c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
3173 * ubifs_debugging_exit - free debugging data.
3174 * @c: UBIFS file-system description object
3176 void ubifs_debugging_exit(struct ubifs_info *c)