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>
23 #include <linux/module.h>
24 #include <linux/debugfs.h>
25 #include <linux/math64.h>
26 #include <linux/uaccess.h>
27 #include <linux/random.h>
29 #include <linux/compat.h>
30 #include <linux/err.h>
35 static DEFINE_SPINLOCK(dbg_lock);
38 static const char *get_key_fmt(int fmt)
41 case UBIFS_SIMPLE_KEY_FMT:
44 return "unknown/invalid format";
48 static const char *get_key_hash(int hash)
51 case UBIFS_KEY_HASH_R5:
53 case UBIFS_KEY_HASH_TEST:
56 return "unknown/invalid name hash";
60 static const char *get_key_type(int type)
74 return "unknown/invalid key";
79 static const char *get_dent_type(int type)
92 case UBIFS_ITYPE_FIFO:
94 case UBIFS_ITYPE_SOCK:
97 return "unknown/invalid type";
102 const char *dbg_snprintf_key(const struct ubifs_info *c,
103 const union ubifs_key *key, char *buffer, int len)
106 int type = key_type(c, key);
108 if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
111 len -= snprintf(p, len, "(%lu, %s)",
112 (unsigned long)key_inum(c, key),
117 len -= snprintf(p, len, "(%lu, %s, %#08x)",
118 (unsigned long)key_inum(c, key),
119 get_key_type(type), key_hash(c, key));
122 len -= snprintf(p, len, "(%lu, %s, %u)",
123 (unsigned long)key_inum(c, key),
124 get_key_type(type), key_block(c, key));
127 len -= snprintf(p, len, "(%lu, %s)",
128 (unsigned long)key_inum(c, key),
132 len -= snprintf(p, len, "(bad key type: %#08x, %#08x)",
133 key->u32[0], key->u32[1]);
136 len -= snprintf(p, len, "bad key format %d", c->key_fmt);
137 ubifs_assert(len > 0);
141 const char *dbg_ntype(int type)
145 return "padding node";
147 return "superblock node";
149 return "master node";
151 return "reference node";
154 case UBIFS_DENT_NODE:
155 return "direntry node";
156 case UBIFS_XENT_NODE:
157 return "xentry node";
158 case UBIFS_DATA_NODE:
160 case UBIFS_TRUN_NODE:
161 return "truncate node";
163 return "indexing node";
165 return "commit start node";
166 case UBIFS_ORPH_NODE:
167 return "orphan node";
169 return "unknown node";
173 static const char *dbg_gtype(int type)
176 case UBIFS_NO_NODE_GROUP:
177 return "no node group";
178 case UBIFS_IN_NODE_GROUP:
179 return "in node group";
180 case UBIFS_LAST_OF_NODE_GROUP:
181 return "last of node group";
187 const char *dbg_cstate(int cmt_state)
191 return "commit resting";
192 case COMMIT_BACKGROUND:
193 return "background commit requested";
194 case COMMIT_REQUIRED:
195 return "commit required";
196 case COMMIT_RUNNING_BACKGROUND:
197 return "BACKGROUND commit running";
198 case COMMIT_RUNNING_REQUIRED:
199 return "commit running and required";
201 return "broken commit";
203 return "unknown commit state";
207 const char *dbg_jhead(int jhead)
217 return "unknown journal head";
221 static void dump_ch(const struct ubifs_ch *ch)
223 pr_err("\tmagic %#x\n", le32_to_cpu(ch->magic));
224 pr_err("\tcrc %#x\n", le32_to_cpu(ch->crc));
225 pr_err("\tnode_type %d (%s)\n", ch->node_type,
226 dbg_ntype(ch->node_type));
227 pr_err("\tgroup_type %d (%s)\n", ch->group_type,
228 dbg_gtype(ch->group_type));
229 pr_err("\tsqnum %llu\n",
230 (unsigned long long)le64_to_cpu(ch->sqnum));
231 pr_err("\tlen %u\n", le32_to_cpu(ch->len));
234 void ubifs_dump_inode(struct ubifs_info *c, const struct inode *inode)
237 const struct ubifs_inode *ui = ubifs_inode(inode);
238 struct qstr nm = { .name = NULL };
240 struct ubifs_dent_node *dent, *pdent = NULL;
243 pr_err("Dump in-memory inode:");
244 pr_err("\tinode %lu\n", inode->i_ino);
245 pr_err("\tsize %llu\n",
246 (unsigned long long)i_size_read(inode));
247 pr_err("\tnlink %u\n", inode->i_nlink);
248 pr_err("\tuid %u\n", (unsigned int)i_uid_read(inode));
249 pr_err("\tgid %u\n", (unsigned int)i_gid_read(inode));
250 pr_err("\tatime %u.%u\n",
251 (unsigned int)inode->i_atime.tv_sec,
252 (unsigned int)inode->i_atime.tv_nsec);
253 pr_err("\tmtime %u.%u\n",
254 (unsigned int)inode->i_mtime.tv_sec,
255 (unsigned int)inode->i_mtime.tv_nsec);
256 pr_err("\tctime %u.%u\n",
257 (unsigned int)inode->i_ctime.tv_sec,
258 (unsigned int)inode->i_ctime.tv_nsec);
259 pr_err("\tcreat_sqnum %llu\n", ui->creat_sqnum);
260 pr_err("\txattr_size %u\n", ui->xattr_size);
261 pr_err("\txattr_cnt %u\n", ui->xattr_cnt);
262 pr_err("\txattr_names %u\n", ui->xattr_names);
263 pr_err("\tdirty %u\n", ui->dirty);
264 pr_err("\txattr %u\n", ui->xattr);
265 pr_err("\tbulk_read %u\n", ui->xattr);
266 pr_err("\tsynced_i_size %llu\n",
267 (unsigned long long)ui->synced_i_size);
268 pr_err("\tui_size %llu\n",
269 (unsigned long long)ui->ui_size);
270 pr_err("\tflags %d\n", ui->flags);
271 pr_err("\tcompr_type %d\n", ui->compr_type);
272 pr_err("\tlast_page_read %lu\n", ui->last_page_read);
273 pr_err("\tread_in_a_row %lu\n", ui->read_in_a_row);
274 pr_err("\tdata_len %d\n", ui->data_len);
276 if (!S_ISDIR(inode->i_mode))
279 pr_err("List of directory entries:\n");
280 ubifs_assert(!mutex_is_locked(&c->tnc_mutex));
282 lowest_dent_key(c, &key, inode->i_ino);
284 dent = ubifs_tnc_next_ent(c, &key, &nm);
286 if (PTR_ERR(dent) != -ENOENT)
287 pr_err("error %ld\n", PTR_ERR(dent));
291 pr_err("\t%d: %s (%s)\n",
292 count++, dent->name, get_dent_type(dent->type));
294 nm.name = dent->name;
295 nm.len = le16_to_cpu(dent->nlen);
298 key_read(c, &dent->key, &key);
304 void ubifs_dump_node(const struct ubifs_info *c, const void *node)
308 const struct ubifs_ch *ch = node;
309 char key_buf[DBG_KEY_BUF_LEN];
311 /* If the magic is incorrect, just hexdump the first bytes */
312 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
313 pr_err("Not a node, first %zu bytes:", UBIFS_CH_SZ);
314 print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1,
315 (void *)node, UBIFS_CH_SZ, 1);
319 spin_lock(&dbg_lock);
322 switch (ch->node_type) {
325 const struct ubifs_pad_node *pad = node;
327 pr_err("\tpad_len %u\n", le32_to_cpu(pad->pad_len));
332 const struct ubifs_sb_node *sup = node;
333 unsigned int sup_flags = le32_to_cpu(sup->flags);
335 pr_err("\tkey_hash %d (%s)\n",
336 (int)sup->key_hash, get_key_hash(sup->key_hash));
337 pr_err("\tkey_fmt %d (%s)\n",
338 (int)sup->key_fmt, get_key_fmt(sup->key_fmt));
339 pr_err("\tflags %#x\n", sup_flags);
340 pr_err("\tbig_lpt %u\n",
341 !!(sup_flags & UBIFS_FLG_BIGLPT));
342 pr_err("\tspace_fixup %u\n",
343 !!(sup_flags & UBIFS_FLG_SPACE_FIXUP));
344 pr_err("\tmin_io_size %u\n", le32_to_cpu(sup->min_io_size));
345 pr_err("\tleb_size %u\n", le32_to_cpu(sup->leb_size));
346 pr_err("\tleb_cnt %u\n", le32_to_cpu(sup->leb_cnt));
347 pr_err("\tmax_leb_cnt %u\n", le32_to_cpu(sup->max_leb_cnt));
348 pr_err("\tmax_bud_bytes %llu\n",
349 (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
350 pr_err("\tlog_lebs %u\n", le32_to_cpu(sup->log_lebs));
351 pr_err("\tlpt_lebs %u\n", le32_to_cpu(sup->lpt_lebs));
352 pr_err("\torph_lebs %u\n", le32_to_cpu(sup->orph_lebs));
353 pr_err("\tjhead_cnt %u\n", le32_to_cpu(sup->jhead_cnt));
354 pr_err("\tfanout %u\n", le32_to_cpu(sup->fanout));
355 pr_err("\tlsave_cnt %u\n", le32_to_cpu(sup->lsave_cnt));
356 pr_err("\tdefault_compr %u\n",
357 (int)le16_to_cpu(sup->default_compr));
358 pr_err("\trp_size %llu\n",
359 (unsigned long long)le64_to_cpu(sup->rp_size));
360 pr_err("\trp_uid %u\n", le32_to_cpu(sup->rp_uid));
361 pr_err("\trp_gid %u\n", le32_to_cpu(sup->rp_gid));
362 pr_err("\tfmt_version %u\n", le32_to_cpu(sup->fmt_version));
363 pr_err("\ttime_gran %u\n", le32_to_cpu(sup->time_gran));
364 pr_err("\tUUID %pUB\n", sup->uuid);
369 const struct ubifs_mst_node *mst = node;
371 pr_err("\thighest_inum %llu\n",
372 (unsigned long long)le64_to_cpu(mst->highest_inum));
373 pr_err("\tcommit number %llu\n",
374 (unsigned long long)le64_to_cpu(mst->cmt_no));
375 pr_err("\tflags %#x\n", le32_to_cpu(mst->flags));
376 pr_err("\tlog_lnum %u\n", le32_to_cpu(mst->log_lnum));
377 pr_err("\troot_lnum %u\n", le32_to_cpu(mst->root_lnum));
378 pr_err("\troot_offs %u\n", le32_to_cpu(mst->root_offs));
379 pr_err("\troot_len %u\n", le32_to_cpu(mst->root_len));
380 pr_err("\tgc_lnum %u\n", le32_to_cpu(mst->gc_lnum));
381 pr_err("\tihead_lnum %u\n", le32_to_cpu(mst->ihead_lnum));
382 pr_err("\tihead_offs %u\n", le32_to_cpu(mst->ihead_offs));
383 pr_err("\tindex_size %llu\n",
384 (unsigned long long)le64_to_cpu(mst->index_size));
385 pr_err("\tlpt_lnum %u\n", le32_to_cpu(mst->lpt_lnum));
386 pr_err("\tlpt_offs %u\n", le32_to_cpu(mst->lpt_offs));
387 pr_err("\tnhead_lnum %u\n", le32_to_cpu(mst->nhead_lnum));
388 pr_err("\tnhead_offs %u\n", le32_to_cpu(mst->nhead_offs));
389 pr_err("\tltab_lnum %u\n", le32_to_cpu(mst->ltab_lnum));
390 pr_err("\tltab_offs %u\n", le32_to_cpu(mst->ltab_offs));
391 pr_err("\tlsave_lnum %u\n", le32_to_cpu(mst->lsave_lnum));
392 pr_err("\tlsave_offs %u\n", le32_to_cpu(mst->lsave_offs));
393 pr_err("\tlscan_lnum %u\n", le32_to_cpu(mst->lscan_lnum));
394 pr_err("\tleb_cnt %u\n", le32_to_cpu(mst->leb_cnt));
395 pr_err("\tempty_lebs %u\n", le32_to_cpu(mst->empty_lebs));
396 pr_err("\tidx_lebs %u\n", le32_to_cpu(mst->idx_lebs));
397 pr_err("\ttotal_free %llu\n",
398 (unsigned long long)le64_to_cpu(mst->total_free));
399 pr_err("\ttotal_dirty %llu\n",
400 (unsigned long long)le64_to_cpu(mst->total_dirty));
401 pr_err("\ttotal_used %llu\n",
402 (unsigned long long)le64_to_cpu(mst->total_used));
403 pr_err("\ttotal_dead %llu\n",
404 (unsigned long long)le64_to_cpu(mst->total_dead));
405 pr_err("\ttotal_dark %llu\n",
406 (unsigned long long)le64_to_cpu(mst->total_dark));
411 const struct ubifs_ref_node *ref = node;
413 pr_err("\tlnum %u\n", le32_to_cpu(ref->lnum));
414 pr_err("\toffs %u\n", le32_to_cpu(ref->offs));
415 pr_err("\tjhead %u\n", le32_to_cpu(ref->jhead));
420 const struct ubifs_ino_node *ino = node;
422 key_read(c, &ino->key, &key);
424 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
425 pr_err("\tcreat_sqnum %llu\n",
426 (unsigned long long)le64_to_cpu(ino->creat_sqnum));
427 pr_err("\tsize %llu\n",
428 (unsigned long long)le64_to_cpu(ino->size));
429 pr_err("\tnlink %u\n", le32_to_cpu(ino->nlink));
430 pr_err("\tatime %lld.%u\n",
431 (long long)le64_to_cpu(ino->atime_sec),
432 le32_to_cpu(ino->atime_nsec));
433 pr_err("\tmtime %lld.%u\n",
434 (long long)le64_to_cpu(ino->mtime_sec),
435 le32_to_cpu(ino->mtime_nsec));
436 pr_err("\tctime %lld.%u\n",
437 (long long)le64_to_cpu(ino->ctime_sec),
438 le32_to_cpu(ino->ctime_nsec));
439 pr_err("\tuid %u\n", le32_to_cpu(ino->uid));
440 pr_err("\tgid %u\n", le32_to_cpu(ino->gid));
441 pr_err("\tmode %u\n", le32_to_cpu(ino->mode));
442 pr_err("\tflags %#x\n", le32_to_cpu(ino->flags));
443 pr_err("\txattr_cnt %u\n", le32_to_cpu(ino->xattr_cnt));
444 pr_err("\txattr_size %u\n", le32_to_cpu(ino->xattr_size));
445 pr_err("\txattr_names %u\n", le32_to_cpu(ino->xattr_names));
446 pr_err("\tcompr_type %#x\n",
447 (int)le16_to_cpu(ino->compr_type));
448 pr_err("\tdata len %u\n", le32_to_cpu(ino->data_len));
451 case UBIFS_DENT_NODE:
452 case UBIFS_XENT_NODE:
454 const struct ubifs_dent_node *dent = node;
455 int nlen = le16_to_cpu(dent->nlen);
457 key_read(c, &dent->key, &key);
459 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
460 pr_err("\tinum %llu\n",
461 (unsigned long long)le64_to_cpu(dent->inum));
462 pr_err("\ttype %d\n", (int)dent->type);
463 pr_err("\tnlen %d\n", nlen);
466 if (nlen > UBIFS_MAX_NLEN)
467 pr_err("(bad name length, not printing, bad or corrupted node)");
469 for (i = 0; i < nlen && dent->name[i]; i++)
470 pr_cont("%c", dent->name[i]);
476 case UBIFS_DATA_NODE:
478 const struct ubifs_data_node *dn = node;
479 int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
481 key_read(c, &dn->key, &key);
483 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
484 pr_err("\tsize %u\n", le32_to_cpu(dn->size));
485 pr_err("\tcompr_typ %d\n",
486 (int)le16_to_cpu(dn->compr_type));
487 pr_err("\tdata size %d\n", dlen);
489 print_hex_dump("\t", DUMP_PREFIX_OFFSET, 32, 1,
490 (void *)&dn->data, dlen, 0);
493 case UBIFS_TRUN_NODE:
495 const struct ubifs_trun_node *trun = node;
497 pr_err("\tinum %u\n", le32_to_cpu(trun->inum));
498 pr_err("\told_size %llu\n",
499 (unsigned long long)le64_to_cpu(trun->old_size));
500 pr_err("\tnew_size %llu\n",
501 (unsigned long long)le64_to_cpu(trun->new_size));
506 const struct ubifs_idx_node *idx = node;
508 n = le16_to_cpu(idx->child_cnt);
509 pr_err("\tchild_cnt %d\n", n);
510 pr_err("\tlevel %d\n", (int)le16_to_cpu(idx->level));
511 pr_err("\tBranches:\n");
513 for (i = 0; i < n && i < c->fanout - 1; i++) {
514 const struct ubifs_branch *br;
516 br = ubifs_idx_branch(c, idx, i);
517 key_read(c, &br->key, &key);
518 pr_err("\t%d: LEB %d:%d len %d key %s\n",
519 i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
520 le32_to_cpu(br->len),
521 dbg_snprintf_key(c, &key, key_buf,
528 case UBIFS_ORPH_NODE:
530 const struct ubifs_orph_node *orph = node;
532 pr_err("\tcommit number %llu\n",
534 le64_to_cpu(orph->cmt_no) & LLONG_MAX);
535 pr_err("\tlast node flag %llu\n",
536 (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
537 n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
538 pr_err("\t%d orphan inode numbers:\n", n);
539 for (i = 0; i < n; i++)
540 pr_err("\t ino %llu\n",
541 (unsigned long long)le64_to_cpu(orph->inos[i]));
545 pr_err("node type %d was not recognized\n",
548 spin_unlock(&dbg_lock);
551 void ubifs_dump_budget_req(const struct ubifs_budget_req *req)
553 spin_lock(&dbg_lock);
554 pr_err("Budgeting request: new_ino %d, dirtied_ino %d\n",
555 req->new_ino, req->dirtied_ino);
556 pr_err("\tnew_ino_d %d, dirtied_ino_d %d\n",
557 req->new_ino_d, req->dirtied_ino_d);
558 pr_err("\tnew_page %d, dirtied_page %d\n",
559 req->new_page, req->dirtied_page);
560 pr_err("\tnew_dent %d, mod_dent %d\n",
561 req->new_dent, req->mod_dent);
562 pr_err("\tidx_growth %d\n", req->idx_growth);
563 pr_err("\tdata_growth %d dd_growth %d\n",
564 req->data_growth, req->dd_growth);
565 spin_unlock(&dbg_lock);
568 void ubifs_dump_lstats(const struct ubifs_lp_stats *lst)
570 spin_lock(&dbg_lock);
571 pr_err("(pid %d) Lprops statistics: empty_lebs %d, idx_lebs %d\n",
572 current->pid, lst->empty_lebs, lst->idx_lebs);
573 pr_err("\ttaken_empty_lebs %d, total_free %lld, total_dirty %lld\n",
574 lst->taken_empty_lebs, lst->total_free, lst->total_dirty);
575 pr_err("\ttotal_used %lld, total_dark %lld, total_dead %lld\n",
576 lst->total_used, lst->total_dark, lst->total_dead);
577 spin_unlock(&dbg_lock);
581 void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)
585 struct ubifs_bud *bud;
586 struct ubifs_gced_idx_leb *idx_gc;
587 long long available, outstanding, free;
589 spin_lock(&c->space_lock);
590 spin_lock(&dbg_lock);
591 pr_err("(pid %d) Budgeting info: data budget sum %lld, total budget sum %lld\n",
592 current->pid, bi->data_growth + bi->dd_growth,
593 bi->data_growth + bi->dd_growth + bi->idx_growth);
594 pr_err("\tbudg_data_growth %lld, budg_dd_growth %lld, budg_idx_growth %lld\n",
595 bi->data_growth, bi->dd_growth, bi->idx_growth);
596 pr_err("\tmin_idx_lebs %d, old_idx_sz %llu, uncommitted_idx %lld\n",
597 bi->min_idx_lebs, bi->old_idx_sz, bi->uncommitted_idx);
598 pr_err("\tpage_budget %d, inode_budget %d, dent_budget %d\n",
599 bi->page_budget, bi->inode_budget, bi->dent_budget);
600 pr_err("\tnospace %u, nospace_rp %u\n", bi->nospace, bi->nospace_rp);
601 pr_err("\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
602 c->dark_wm, c->dead_wm, c->max_idx_node_sz);
606 * If we are dumping saved budgeting data, do not print
607 * additional information which is about the current state, not
608 * the old one which corresponded to the saved budgeting data.
612 pr_err("\tfreeable_cnt %d, calc_idx_sz %lld, idx_gc_cnt %d\n",
613 c->freeable_cnt, c->calc_idx_sz, c->idx_gc_cnt);
614 pr_err("\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, clean_zn_cnt %ld\n",
615 atomic_long_read(&c->dirty_pg_cnt),
616 atomic_long_read(&c->dirty_zn_cnt),
617 atomic_long_read(&c->clean_zn_cnt));
618 pr_err("\tgc_lnum %d, ihead_lnum %d\n", c->gc_lnum, c->ihead_lnum);
620 /* If we are in R/O mode, journal heads do not exist */
622 for (i = 0; i < c->jhead_cnt; i++)
623 pr_err("\tjhead %s\t LEB %d\n",
624 dbg_jhead(c->jheads[i].wbuf.jhead),
625 c->jheads[i].wbuf.lnum);
626 for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
627 bud = rb_entry(rb, struct ubifs_bud, rb);
628 pr_err("\tbud LEB %d\n", bud->lnum);
630 list_for_each_entry(bud, &c->old_buds, list)
631 pr_err("\told bud LEB %d\n", bud->lnum);
632 list_for_each_entry(idx_gc, &c->idx_gc, list)
633 pr_err("\tGC'ed idx LEB %d unmap %d\n",
634 idx_gc->lnum, idx_gc->unmap);
635 pr_err("\tcommit state %d\n", c->cmt_state);
637 /* Print budgeting predictions */
638 available = ubifs_calc_available(c, c->bi.min_idx_lebs);
639 outstanding = c->bi.data_growth + c->bi.dd_growth;
640 free = ubifs_get_free_space_nolock(c);
641 pr_err("Budgeting predictions:\n");
642 pr_err("\tavailable: %lld, outstanding %lld, free %lld\n",
643 available, outstanding, free);
645 spin_unlock(&dbg_lock);
646 spin_unlock(&c->space_lock);
649 void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)
654 void ubifs_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
656 int i, spc, dark = 0, dead = 0;
658 struct ubifs_bud *bud;
660 spc = lp->free + lp->dirty;
661 if (spc < c->dead_wm)
664 dark = ubifs_calc_dark(c, spc);
666 if (lp->flags & LPROPS_INDEX)
667 pr_err("LEB %-7d free %-8d dirty %-8d used %-8d free + dirty %-8d flags %#x (",
668 lp->lnum, lp->free, lp->dirty, c->leb_size - spc, spc,
671 pr_err("LEB %-7d free %-8d dirty %-8d used %-8d free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d flags %#-4x (",
672 lp->lnum, lp->free, lp->dirty, c->leb_size - spc, spc,
673 dark, dead, (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags);
675 if (lp->flags & LPROPS_TAKEN) {
676 if (lp->flags & LPROPS_INDEX)
677 pr_cont("index, taken");
683 if (lp->flags & LPROPS_INDEX) {
684 switch (lp->flags & LPROPS_CAT_MASK) {
685 case LPROPS_DIRTY_IDX:
688 case LPROPS_FRDI_IDX:
689 s = "freeable index";
695 switch (lp->flags & LPROPS_CAT_MASK) {
697 s = "not categorized";
708 case LPROPS_FREEABLE:
719 for (rb = rb_first((struct rb_root *)&c->buds); rb; rb = rb_next(rb)) {
720 bud = rb_entry(rb, struct ubifs_bud, rb);
721 if (bud->lnum == lp->lnum) {
723 for (i = 0; i < c->jhead_cnt; i++) {
725 * Note, if we are in R/O mode or in the middle
726 * of mounting/re-mounting, the write-buffers do
730 lp->lnum == c->jheads[i].wbuf.lnum) {
731 pr_cont(", jhead %s", dbg_jhead(i));
736 pr_cont(", bud of jhead %s",
737 dbg_jhead(bud->jhead));
740 if (lp->lnum == c->gc_lnum)
745 void ubifs_dump_lprops(struct ubifs_info *c)
748 struct ubifs_lprops lp;
749 struct ubifs_lp_stats lst;
751 pr_err("(pid %d) start dumping LEB properties\n", current->pid);
752 ubifs_get_lp_stats(c, &lst);
753 ubifs_dump_lstats(&lst);
755 for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
756 err = ubifs_read_one_lp(c, lnum, &lp);
758 ubifs_err(c, "cannot read lprops for LEB %d", lnum);
762 ubifs_dump_lprop(c, &lp);
764 pr_err("(pid %d) finish dumping LEB properties\n", current->pid);
767 void ubifs_dump_lpt_info(struct ubifs_info *c)
771 spin_lock(&dbg_lock);
772 pr_err("(pid %d) dumping LPT information\n", current->pid);
773 pr_err("\tlpt_sz: %lld\n", c->lpt_sz);
774 pr_err("\tpnode_sz: %d\n", c->pnode_sz);
775 pr_err("\tnnode_sz: %d\n", c->nnode_sz);
776 pr_err("\tltab_sz: %d\n", c->ltab_sz);
777 pr_err("\tlsave_sz: %d\n", c->lsave_sz);
778 pr_err("\tbig_lpt: %d\n", c->big_lpt);
779 pr_err("\tlpt_hght: %d\n", c->lpt_hght);
780 pr_err("\tpnode_cnt: %d\n", c->pnode_cnt);
781 pr_err("\tnnode_cnt: %d\n", c->nnode_cnt);
782 pr_err("\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt);
783 pr_err("\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt);
784 pr_err("\tlsave_cnt: %d\n", c->lsave_cnt);
785 pr_err("\tspace_bits: %d\n", c->space_bits);
786 pr_err("\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
787 pr_err("\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
788 pr_err("\tlpt_spc_bits: %d\n", c->lpt_spc_bits);
789 pr_err("\tpcnt_bits: %d\n", c->pcnt_bits);
790 pr_err("\tlnum_bits: %d\n", c->lnum_bits);
791 pr_err("\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
792 pr_err("\tLPT head is at %d:%d\n",
793 c->nhead_lnum, c->nhead_offs);
794 pr_err("\tLPT ltab is at %d:%d\n", c->ltab_lnum, c->ltab_offs);
796 pr_err("\tLPT lsave is at %d:%d\n",
797 c->lsave_lnum, c->lsave_offs);
798 for (i = 0; i < c->lpt_lebs; i++)
799 pr_err("\tLPT LEB %d free %d dirty %d tgc %d cmt %d\n",
800 i + c->lpt_first, c->ltab[i].free, c->ltab[i].dirty,
801 c->ltab[i].tgc, c->ltab[i].cmt);
802 spin_unlock(&dbg_lock);
805 void ubifs_dump_sleb(const struct ubifs_info *c,
806 const struct ubifs_scan_leb *sleb, int offs)
808 struct ubifs_scan_node *snod;
810 pr_err("(pid %d) start dumping scanned data from LEB %d:%d\n",
811 current->pid, sleb->lnum, offs);
813 list_for_each_entry(snod, &sleb->nodes, list) {
815 pr_err("Dumping node at LEB %d:%d len %d\n",
816 sleb->lnum, snod->offs, snod->len);
817 ubifs_dump_node(c, snod->node);
821 void ubifs_dump_leb(const struct ubifs_info *c, int lnum)
823 struct ubifs_scan_leb *sleb;
824 struct ubifs_scan_node *snod;
827 pr_err("(pid %d) start dumping LEB %d\n", current->pid, lnum);
829 buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
831 ubifs_err(c, "cannot allocate memory for dumping LEB %d", lnum);
835 sleb = ubifs_scan(c, lnum, 0, buf, 0);
837 ubifs_err(c, "scan error %d", (int)PTR_ERR(sleb));
841 pr_err("LEB %d has %d nodes ending at %d\n", lnum,
842 sleb->nodes_cnt, sleb->endpt);
844 list_for_each_entry(snod, &sleb->nodes, list) {
846 pr_err("Dumping node at LEB %d:%d len %d\n", lnum,
847 snod->offs, snod->len);
848 ubifs_dump_node(c, snod->node);
851 pr_err("(pid %d) finish dumping LEB %d\n", current->pid, lnum);
852 ubifs_scan_destroy(sleb);
859 void ubifs_dump_znode(const struct ubifs_info *c,
860 const struct ubifs_znode *znode)
863 const struct ubifs_zbranch *zbr;
864 char key_buf[DBG_KEY_BUF_LEN];
866 spin_lock(&dbg_lock);
868 zbr = &znode->parent->zbranch[znode->iip];
872 pr_err("znode %p, LEB %d:%d len %d parent %p iip %d level %d child_cnt %d flags %lx\n",
873 znode, zbr->lnum, zbr->offs, zbr->len, znode->parent, znode->iip,
874 znode->level, znode->child_cnt, znode->flags);
876 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
877 spin_unlock(&dbg_lock);
881 pr_err("zbranches:\n");
882 for (n = 0; n < znode->child_cnt; n++) {
883 zbr = &znode->zbranch[n];
884 if (znode->level > 0)
885 pr_err("\t%d: znode %p LEB %d:%d len %d key %s\n",
886 n, zbr->znode, zbr->lnum, zbr->offs, zbr->len,
887 dbg_snprintf_key(c, &zbr->key, key_buf,
890 pr_err("\t%d: LNC %p LEB %d:%d len %d key %s\n",
891 n, zbr->znode, zbr->lnum, zbr->offs, zbr->len,
892 dbg_snprintf_key(c, &zbr->key, key_buf,
895 spin_unlock(&dbg_lock);
898 void ubifs_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
902 pr_err("(pid %d) start dumping heap cat %d (%d elements)\n",
903 current->pid, cat, heap->cnt);
904 for (i = 0; i < heap->cnt; i++) {
905 struct ubifs_lprops *lprops = heap->arr[i];
907 pr_err("\t%d. LEB %d hpos %d free %d dirty %d flags %d\n",
908 i, lprops->lnum, lprops->hpos, lprops->free,
909 lprops->dirty, lprops->flags);
911 pr_err("(pid %d) finish dumping heap\n", current->pid);
914 void ubifs_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
915 struct ubifs_nnode *parent, int iip)
919 pr_err("(pid %d) dumping pnode:\n", current->pid);
920 pr_err("\taddress %zx parent %zx cnext %zx\n",
921 (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
922 pr_err("\tflags %lu iip %d level %d num %d\n",
923 pnode->flags, iip, pnode->level, pnode->num);
924 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
925 struct ubifs_lprops *lp = &pnode->lprops[i];
927 pr_err("\t%d: free %d dirty %d flags %d lnum %d\n",
928 i, lp->free, lp->dirty, lp->flags, lp->lnum);
932 void ubifs_dump_tnc(struct ubifs_info *c)
934 struct ubifs_znode *znode;
938 pr_err("(pid %d) start dumping TNC tree\n", current->pid);
939 znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
940 level = znode->level;
941 pr_err("== Level %d ==\n", level);
943 if (level != znode->level) {
944 level = znode->level;
945 pr_err("== Level %d ==\n", level);
947 ubifs_dump_znode(c, znode);
948 znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
950 pr_err("(pid %d) finish dumping TNC tree\n", current->pid);
953 static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
956 ubifs_dump_znode(c, znode);
961 * ubifs_dump_index - dump the on-flash index.
962 * @c: UBIFS file-system description object
964 * This function dumps whole UBIFS indexing B-tree, unlike 'ubifs_dump_tnc()'
965 * which dumps only in-memory znodes and does not read znodes which from flash.
967 void ubifs_dump_index(struct ubifs_info *c)
969 dbg_walk_index(c, NULL, dump_znode, NULL);
974 * dbg_save_space_info - save information about flash space.
975 * @c: UBIFS file-system description object
977 * This function saves information about UBIFS free space, dirty space, etc, in
978 * order to check it later.
980 void dbg_save_space_info(struct ubifs_info *c)
982 struct ubifs_debug_info *d = c->dbg;
985 spin_lock(&c->space_lock);
986 memcpy(&d->saved_lst, &c->lst, sizeof(struct ubifs_lp_stats));
987 memcpy(&d->saved_bi, &c->bi, sizeof(struct ubifs_budg_info));
988 d->saved_idx_gc_cnt = c->idx_gc_cnt;
991 * We use a dirty hack here and zero out @c->freeable_cnt, because it
992 * affects the free space calculations, and UBIFS might not know about
993 * all freeable eraseblocks. Indeed, we know about freeable eraseblocks
994 * only when we read their lprops, and we do this only lazily, upon the
995 * need. So at any given point of time @c->freeable_cnt might be not
998 * Just one example about the issue we hit when we did not zero
1000 * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the
1001 * amount of free space in @d->saved_free
1002 * 2. We re-mount R/W, which makes UBIFS to read the "lsave"
1003 * information from flash, where we cache LEBs from various
1004 * categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()'
1005 * -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()'
1006 * -> 'ubifs_get_pnode()' -> 'update_cats()'
1007 * -> 'ubifs_add_to_cat()').
1008 * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt
1010 * 4. We calculate the amount of free space when the re-mount is
1011 * finished in 'dbg_check_space_info()' and it does not match
1014 freeable_cnt = c->freeable_cnt;
1015 c->freeable_cnt = 0;
1016 d->saved_free = ubifs_get_free_space_nolock(c);
1017 c->freeable_cnt = freeable_cnt;
1018 spin_unlock(&c->space_lock);
1022 * dbg_check_space_info - check flash space information.
1023 * @c: UBIFS file-system description object
1025 * This function compares current flash space information with the information
1026 * which was saved when the 'dbg_save_space_info()' function was called.
1027 * Returns zero if the information has not changed, and %-EINVAL it it has
1030 int dbg_check_space_info(struct ubifs_info *c)
1032 struct ubifs_debug_info *d = c->dbg;
1033 struct ubifs_lp_stats lst;
1037 spin_lock(&c->space_lock);
1038 freeable_cnt = c->freeable_cnt;
1039 c->freeable_cnt = 0;
1040 free = ubifs_get_free_space_nolock(c);
1041 c->freeable_cnt = freeable_cnt;
1042 spin_unlock(&c->space_lock);
1044 if (free != d->saved_free) {
1045 ubifs_err(c, "free space changed from %lld to %lld",
1046 d->saved_free, free);
1053 ubifs_msg(c, "saved lprops statistics dump");
1054 ubifs_dump_lstats(&d->saved_lst);
1055 ubifs_msg(c, "saved budgeting info dump");
1056 ubifs_dump_budg(c, &d->saved_bi);
1057 ubifs_msg(c, "saved idx_gc_cnt %d", d->saved_idx_gc_cnt);
1058 ubifs_msg(c, "current lprops statistics dump");
1059 ubifs_get_lp_stats(c, &lst);
1060 ubifs_dump_lstats(&lst);
1061 ubifs_msg(c, "current budgeting info dump");
1062 ubifs_dump_budg(c, &c->bi);
1068 * dbg_check_synced_i_size - check synchronized inode size.
1069 * @c: UBIFS file-system description object
1070 * @inode: inode to check
1072 * If inode is clean, synchronized inode size has to be equivalent to current
1073 * inode size. This function has to be called only for locked inodes (@i_mutex
1074 * has to be locked). Returns %0 if synchronized inode size if correct, and
1077 int dbg_check_synced_i_size(const struct ubifs_info *c, struct inode *inode)
1080 struct ubifs_inode *ui = ubifs_inode(inode);
1082 if (!dbg_is_chk_gen(c))
1084 if (!S_ISREG(inode->i_mode))
1087 mutex_lock(&ui->ui_mutex);
1088 spin_lock(&ui->ui_lock);
1089 if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
1090 ubifs_err(c, "ui_size is %lld, synced_i_size is %lld, but inode is clean",
1091 ui->ui_size, ui->synced_i_size);
1092 ubifs_err(c, "i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
1093 inode->i_mode, i_size_read(inode));
1097 spin_unlock(&ui->ui_lock);
1098 mutex_unlock(&ui->ui_mutex);
1103 * dbg_check_dir - check directory inode size and link count.
1104 * @c: UBIFS file-system description object
1105 * @dir: the directory to calculate size for
1106 * @size: the result is returned here
1108 * This function makes sure that directory size and link count are correct.
1109 * Returns zero in case of success and a negative error code in case of
1112 * Note, it is good idea to make sure the @dir->i_mutex is locked before
1113 * calling this function.
1115 int dbg_check_dir(struct ubifs_info *c, const struct inode *dir)
1117 unsigned int nlink = 2;
1118 union ubifs_key key;
1119 struct ubifs_dent_node *dent, *pdent = NULL;
1120 struct qstr nm = { .name = NULL };
1121 loff_t size = UBIFS_INO_NODE_SZ;
1123 if (!dbg_is_chk_gen(c))
1126 if (!S_ISDIR(dir->i_mode))
1129 lowest_dent_key(c, &key, dir->i_ino);
1133 dent = ubifs_tnc_next_ent(c, &key, &nm);
1135 err = PTR_ERR(dent);
1141 nm.name = dent->name;
1142 nm.len = le16_to_cpu(dent->nlen);
1143 size += CALC_DENT_SIZE(nm.len);
1144 if (dent->type == UBIFS_ITYPE_DIR)
1148 key_read(c, &dent->key, &key);
1152 if (i_size_read(dir) != size) {
1153 ubifs_err(c, "directory inode %lu has size %llu, but calculated size is %llu",
1154 dir->i_ino, (unsigned long long)i_size_read(dir),
1155 (unsigned long long)size);
1156 ubifs_dump_inode(c, dir);
1160 if (dir->i_nlink != nlink) {
1161 ubifs_err(c, "directory inode %lu has nlink %u, but calculated nlink is %u",
1162 dir->i_ino, dir->i_nlink, nlink);
1163 ubifs_dump_inode(c, dir);
1172 * dbg_check_key_order - make sure that colliding keys are properly ordered.
1173 * @c: UBIFS file-system description object
1174 * @zbr1: first zbranch
1175 * @zbr2: following zbranch
1177 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
1178 * names of the direntries/xentries which are referred by the keys. This
1179 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
1180 * sure the name of direntry/xentry referred by @zbr1 is less than
1181 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
1182 * and a negative error code in case of failure.
1184 static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
1185 struct ubifs_zbranch *zbr2)
1187 int err, nlen1, nlen2, cmp;
1188 struct ubifs_dent_node *dent1, *dent2;
1189 union ubifs_key key;
1190 char key_buf[DBG_KEY_BUF_LEN];
1192 ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
1193 dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1196 dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1202 err = ubifs_tnc_read_node(c, zbr1, dent1);
1205 err = ubifs_validate_entry(c, dent1);
1209 err = ubifs_tnc_read_node(c, zbr2, dent2);
1212 err = ubifs_validate_entry(c, dent2);
1216 /* Make sure node keys are the same as in zbranch */
1218 key_read(c, &dent1->key, &key);
1219 if (keys_cmp(c, &zbr1->key, &key)) {
1220 ubifs_err(c, "1st entry at %d:%d has key %s", zbr1->lnum,
1221 zbr1->offs, dbg_snprintf_key(c, &key, key_buf,
1223 ubifs_err(c, "but it should have key %s according to tnc",
1224 dbg_snprintf_key(c, &zbr1->key, key_buf,
1226 ubifs_dump_node(c, dent1);
1230 key_read(c, &dent2->key, &key);
1231 if (keys_cmp(c, &zbr2->key, &key)) {
1232 ubifs_err(c, "2nd entry at %d:%d has key %s", zbr1->lnum,
1233 zbr1->offs, dbg_snprintf_key(c, &key, key_buf,
1235 ubifs_err(c, "but it should have key %s according to tnc",
1236 dbg_snprintf_key(c, &zbr2->key, key_buf,
1238 ubifs_dump_node(c, dent2);
1242 nlen1 = le16_to_cpu(dent1->nlen);
1243 nlen2 = le16_to_cpu(dent2->nlen);
1245 cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
1246 if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
1250 if (cmp == 0 && nlen1 == nlen2)
1251 ubifs_err(c, "2 xent/dent nodes with the same name");
1253 ubifs_err(c, "bad order of colliding key %s",
1254 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
1256 ubifs_msg(c, "first node at %d:%d\n", zbr1->lnum, zbr1->offs);
1257 ubifs_dump_node(c, dent1);
1258 ubifs_msg(c, "second node at %d:%d\n", zbr2->lnum, zbr2->offs);
1259 ubifs_dump_node(c, dent2);
1268 * dbg_check_znode - check if znode is all right.
1269 * @c: UBIFS file-system description object
1270 * @zbr: zbranch which points to this znode
1272 * This function makes sure that znode referred to by @zbr is all right.
1273 * Returns zero if it is, and %-EINVAL if it is not.
1275 static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
1277 struct ubifs_znode *znode = zbr->znode;
1278 struct ubifs_znode *zp = znode->parent;
1281 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
1285 if (znode->level < 0) {
1289 if (znode->iip < 0 || znode->iip >= c->fanout) {
1295 /* Only dirty zbranch may have no on-flash nodes */
1296 if (!ubifs_zn_dirty(znode)) {
1301 if (ubifs_zn_dirty(znode)) {
1303 * If znode is dirty, its parent has to be dirty as well. The
1304 * order of the operation is important, so we have to have
1308 if (zp && !ubifs_zn_dirty(zp)) {
1310 * The dirty flag is atomic and is cleared outside the
1311 * TNC mutex, so znode's dirty flag may now have
1312 * been cleared. The child is always cleared before the
1313 * parent, so we just need to check again.
1316 if (ubifs_zn_dirty(znode)) {
1324 const union ubifs_key *min, *max;
1326 if (znode->level != zp->level - 1) {
1331 /* Make sure the 'parent' pointer in our znode is correct */
1332 err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
1334 /* This zbranch does not exist in the parent */
1339 if (znode->iip >= zp->child_cnt) {
1344 if (znode->iip != n) {
1345 /* This may happen only in case of collisions */
1346 if (keys_cmp(c, &zp->zbranch[n].key,
1347 &zp->zbranch[znode->iip].key)) {
1355 * Make sure that the first key in our znode is greater than or
1356 * equal to the key in the pointing zbranch.
1359 cmp = keys_cmp(c, min, &znode->zbranch[0].key);
1365 if (n + 1 < zp->child_cnt) {
1366 max = &zp->zbranch[n + 1].key;
1369 * Make sure the last key in our znode is less or
1370 * equivalent than the key in the zbranch which goes
1371 * after our pointing zbranch.
1373 cmp = keys_cmp(c, max,
1374 &znode->zbranch[znode->child_cnt - 1].key);
1381 /* This may only be root znode */
1382 if (zbr != &c->zroot) {
1389 * Make sure that next key is greater or equivalent then the previous
1392 for (n = 1; n < znode->child_cnt; n++) {
1393 cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
1394 &znode->zbranch[n].key);
1400 /* This can only be keys with colliding hash */
1401 if (!is_hash_key(c, &znode->zbranch[n].key)) {
1406 if (znode->level != 0 || c->replaying)
1410 * Colliding keys should follow binary order of
1411 * corresponding xentry/dentry names.
1413 err = dbg_check_key_order(c, &znode->zbranch[n - 1],
1414 &znode->zbranch[n]);
1424 for (n = 0; n < znode->child_cnt; n++) {
1425 if (!znode->zbranch[n].znode &&
1426 (znode->zbranch[n].lnum == 0 ||
1427 znode->zbranch[n].len == 0)) {
1432 if (znode->zbranch[n].lnum != 0 &&
1433 znode->zbranch[n].len == 0) {
1438 if (znode->zbranch[n].lnum == 0 &&
1439 znode->zbranch[n].len != 0) {
1444 if (znode->zbranch[n].lnum == 0 &&
1445 znode->zbranch[n].offs != 0) {
1450 if (znode->level != 0 && znode->zbranch[n].znode)
1451 if (znode->zbranch[n].znode->parent != znode) {
1460 ubifs_err(c, "failed, error %d", err);
1461 ubifs_msg(c, "dump of the znode");
1462 ubifs_dump_znode(c, znode);
1464 ubifs_msg(c, "dump of the parent znode");
1465 ubifs_dump_znode(c, zp);
1472 int dbg_check_dir(struct ubifs_info *c, const struct inode *dir)
1477 void dbg_debugfs_exit_fs(struct ubifs_info *c)
1482 int ubifs_debugging_init(struct ubifs_info *c)
1486 void ubifs_debugging_exit(struct ubifs_info *c)
1489 int dbg_check_filesystem(struct ubifs_info *c)
1493 int dbg_debugfs_init_fs(struct ubifs_info *c)
1501 * dbg_check_tnc - check TNC tree.
1502 * @c: UBIFS file-system description object
1503 * @extra: do extra checks that are possible at start commit
1505 * This function traverses whole TNC tree and checks every znode. Returns zero
1506 * if everything is all right and %-EINVAL if something is wrong with TNC.
1508 int dbg_check_tnc(struct ubifs_info *c, int extra)
1510 struct ubifs_znode *znode;
1511 long clean_cnt = 0, dirty_cnt = 0;
1514 if (!dbg_is_chk_index(c))
1517 ubifs_assert(mutex_is_locked(&c->tnc_mutex));
1518 if (!c->zroot.znode)
1521 znode = ubifs_tnc_postorder_first(c->zroot.znode);
1523 struct ubifs_znode *prev;
1524 struct ubifs_zbranch *zbr;
1529 zbr = &znode->parent->zbranch[znode->iip];
1531 err = dbg_check_znode(c, zbr);
1536 if (ubifs_zn_dirty(znode))
1543 znode = ubifs_tnc_postorder_next(znode);
1548 * If the last key of this znode is equivalent to the first key
1549 * of the next znode (collision), then check order of the keys.
1551 last = prev->child_cnt - 1;
1552 if (prev->level == 0 && znode->level == 0 && !c->replaying &&
1553 !keys_cmp(c, &prev->zbranch[last].key,
1554 &znode->zbranch[0].key)) {
1555 err = dbg_check_key_order(c, &prev->zbranch[last],
1556 &znode->zbranch[0]);
1560 ubifs_msg(c, "first znode");
1561 ubifs_dump_znode(c, prev);
1562 ubifs_msg(c, "second znode");
1563 ubifs_dump_znode(c, znode);
1570 if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
1571 ubifs_err(c, "incorrect clean_zn_cnt %ld, calculated %ld",
1572 atomic_long_read(&c->clean_zn_cnt),
1576 if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
1577 ubifs_err(c, "incorrect dirty_zn_cnt %ld, calculated %ld",
1578 atomic_long_read(&c->dirty_zn_cnt),
1587 int dbg_check_tnc(struct ubifs_info *c, int extra)
1594 * dbg_walk_index - walk the on-flash index.
1595 * @c: UBIFS file-system description object
1596 * @leaf_cb: called for each leaf node
1597 * @znode_cb: called for each indexing node
1598 * @priv: private data which is passed to callbacks
1600 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1601 * node and @znode_cb for each indexing node. Returns zero in case of success
1602 * and a negative error code in case of failure.
1604 * It would be better if this function removed every znode it pulled to into
1605 * the TNC, so that the behavior more closely matched the non-debugging
1608 int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1609 dbg_znode_callback znode_cb, void *priv)
1612 struct ubifs_zbranch *zbr;
1613 struct ubifs_znode *znode, *child;
1615 mutex_lock(&c->tnc_mutex);
1616 /* If the root indexing node is not in TNC - pull it */
1617 if (!c->zroot.znode) {
1618 c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1619 if (IS_ERR(c->zroot.znode)) {
1620 err = PTR_ERR(c->zroot.znode);
1621 c->zroot.znode = NULL;
1627 * We are going to traverse the indexing tree in the postorder manner.
1628 * Go down and find the leftmost indexing node where we are going to
1631 znode = c->zroot.znode;
1632 while (znode->level > 0) {
1633 zbr = &znode->zbranch[0];
1636 child = ubifs_load_znode(c, zbr, znode, 0);
1637 if (IS_ERR(child)) {
1638 err = PTR_ERR(child);
1647 /* Iterate over all indexing nodes */
1654 err = znode_cb(c, znode, priv);
1656 ubifs_err(c, "znode checking function returned error %d",
1658 ubifs_dump_znode(c, znode);
1662 if (leaf_cb && znode->level == 0) {
1663 for (idx = 0; idx < znode->child_cnt; idx++) {
1664 zbr = &znode->zbranch[idx];
1665 err = leaf_cb(c, zbr, priv);
1667 ubifs_err(c, "leaf checking function returned error %d, for leaf at LEB %d:%d",
1668 err, zbr->lnum, zbr->offs);
1677 idx = znode->iip + 1;
1678 znode = znode->parent;
1679 if (idx < znode->child_cnt) {
1680 /* Switch to the next index in the parent */
1681 zbr = &znode->zbranch[idx];
1684 child = ubifs_load_znode(c, zbr, znode, idx);
1685 if (IS_ERR(child)) {
1686 err = PTR_ERR(child);
1694 * This is the last child, switch to the parent and
1699 /* Go to the lowest leftmost znode in the new sub-tree */
1700 while (znode->level > 0) {
1701 zbr = &znode->zbranch[0];
1704 child = ubifs_load_znode(c, zbr, znode, 0);
1705 if (IS_ERR(child)) {
1706 err = PTR_ERR(child);
1715 mutex_unlock(&c->tnc_mutex);
1720 zbr = &znode->parent->zbranch[znode->iip];
1723 ubifs_msg(c, "dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1724 ubifs_dump_znode(c, znode);
1726 mutex_unlock(&c->tnc_mutex);
1731 * add_size - add znode size to partially calculated index size.
1732 * @c: UBIFS file-system description object
1733 * @znode: znode to add size for
1734 * @priv: partially calculated index size
1736 * This is a helper function for 'dbg_check_idx_size()' which is called for
1737 * every indexing node and adds its size to the 'long long' variable pointed to
1740 static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1742 long long *idx_size = priv;
1745 add = ubifs_idx_node_sz(c, znode->child_cnt);
1746 add = ALIGN(add, 8);
1752 * dbg_check_idx_size - check index size.
1753 * @c: UBIFS file-system description object
1754 * @idx_size: size to check
1756 * This function walks the UBIFS index, calculates its size and checks that the
1757 * size is equivalent to @idx_size. Returns zero in case of success and a
1758 * negative error code in case of failure.
1760 int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
1765 if (!dbg_is_chk_index(c))
1768 err = dbg_walk_index(c, NULL, add_size, &calc);
1770 ubifs_err(c, "error %d while walking the index", err);
1774 if (calc != idx_size) {
1775 ubifs_err(c, "index size check failed: calculated size is %lld, should be %lld",
1786 * struct fsck_inode - information about an inode used when checking the file-system.
1787 * @rb: link in the RB-tree of inodes
1788 * @inum: inode number
1789 * @mode: inode type, permissions, etc
1790 * @nlink: inode link count
1791 * @xattr_cnt: count of extended attributes
1792 * @references: how many directory/xattr entries refer this inode (calculated
1793 * while walking the index)
1794 * @calc_cnt: for directory inode count of child directories
1795 * @size: inode size (read from on-flash inode)
1796 * @xattr_sz: summary size of all extended attributes (read from on-flash
1798 * @calc_sz: for directories calculated directory size
1799 * @calc_xcnt: count of extended attributes
1800 * @calc_xsz: calculated summary size of all extended attributes
1801 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1802 * inode (read from on-flash inode)
1803 * @calc_xnms: calculated sum of lengths of all extended attribute names
1810 unsigned int xattr_cnt;
1814 unsigned int xattr_sz;
1816 long long calc_xcnt;
1818 unsigned int xattr_nms;
1819 long long calc_xnms;
1823 * struct fsck_data - private FS checking information.
1824 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1827 struct rb_root inodes;
1831 * add_inode - add inode information to RB-tree of inodes.
1832 * @c: UBIFS file-system description object
1833 * @fsckd: FS checking information
1834 * @ino: raw UBIFS inode to add
1836 * This is a helper function for 'check_leaf()' which adds information about
1837 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1838 * case of success and a negative error code in case of failure.
1840 static struct fsck_inode *add_inode(struct ubifs_info *c,
1841 struct fsck_data *fsckd,
1842 struct ubifs_ino_node *ino)
1844 struct rb_node **p, *parent = NULL;
1845 struct fsck_inode *fscki;
1846 ino_t inum = key_inum_flash(c, &ino->key);
1847 struct inode *inode;
1848 struct ubifs_inode *ui;
1850 p = &fsckd->inodes.rb_node;
1853 fscki = rb_entry(parent, struct fsck_inode, rb);
1854 if (inum < fscki->inum)
1856 else if (inum > fscki->inum)
1857 p = &(*p)->rb_right;
1862 if (inum > c->highest_inum) {
1863 ubifs_err(c, "too high inode number, max. is %lu",
1864 (unsigned long)c->highest_inum);
1865 return ERR_PTR(-EINVAL);
1868 fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
1870 return ERR_PTR(-ENOMEM);
1872 inode = ilookup(c->vfs_sb, inum);
1876 * If the inode is present in the VFS inode cache, use it instead of
1877 * the on-flash inode which might be out-of-date. E.g., the size might
1878 * be out-of-date. If we do not do this, the following may happen, for
1880 * 1. A power cut happens
1881 * 2. We mount the file-system R/O, the replay process fixes up the
1882 * inode size in the VFS cache, but on on-flash.
1883 * 3. 'check_leaf()' fails because it hits a data node beyond inode
1887 fscki->nlink = le32_to_cpu(ino->nlink);
1888 fscki->size = le64_to_cpu(ino->size);
1889 fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
1890 fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
1891 fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
1892 fscki->mode = le32_to_cpu(ino->mode);
1894 ui = ubifs_inode(inode);
1895 fscki->nlink = inode->i_nlink;
1896 fscki->size = inode->i_size;
1897 fscki->xattr_cnt = ui->xattr_cnt;
1898 fscki->xattr_sz = ui->xattr_size;
1899 fscki->xattr_nms = ui->xattr_names;
1900 fscki->mode = inode->i_mode;
1904 if (S_ISDIR(fscki->mode)) {
1905 fscki->calc_sz = UBIFS_INO_NODE_SZ;
1906 fscki->calc_cnt = 2;
1909 rb_link_node(&fscki->rb, parent, p);
1910 rb_insert_color(&fscki->rb, &fsckd->inodes);
1916 * search_inode - search inode in the RB-tree of inodes.
1917 * @fsckd: FS checking information
1918 * @inum: inode number to search
1920 * This is a helper function for 'check_leaf()' which searches inode @inum in
1921 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1922 * the inode was not found.
1924 static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
1927 struct fsck_inode *fscki;
1929 p = fsckd->inodes.rb_node;
1931 fscki = rb_entry(p, struct fsck_inode, rb);
1932 if (inum < fscki->inum)
1934 else if (inum > fscki->inum)
1943 * read_add_inode - read inode node and add it to RB-tree of inodes.
1944 * @c: UBIFS file-system description object
1945 * @fsckd: FS checking information
1946 * @inum: inode number to read
1948 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1949 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1950 * information pointer in case of success and a negative error code in case of
1953 static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1954 struct fsck_data *fsckd, ino_t inum)
1957 union ubifs_key key;
1958 struct ubifs_znode *znode;
1959 struct ubifs_zbranch *zbr;
1960 struct ubifs_ino_node *ino;
1961 struct fsck_inode *fscki;
1963 fscki = search_inode(fsckd, inum);
1967 ino_key_init(c, &key, inum);
1968 err = ubifs_lookup_level0(c, &key, &znode, &n);
1970 ubifs_err(c, "inode %lu not found in index", (unsigned long)inum);
1971 return ERR_PTR(-ENOENT);
1972 } else if (err < 0) {
1973 ubifs_err(c, "error %d while looking up inode %lu",
1974 err, (unsigned long)inum);
1975 return ERR_PTR(err);
1978 zbr = &znode->zbranch[n];
1979 if (zbr->len < UBIFS_INO_NODE_SZ) {
1980 ubifs_err(c, "bad node %lu node length %d",
1981 (unsigned long)inum, zbr->len);
1982 return ERR_PTR(-EINVAL);
1985 ino = kmalloc(zbr->len, GFP_NOFS);
1987 return ERR_PTR(-ENOMEM);
1989 err = ubifs_tnc_read_node(c, zbr, ino);
1991 ubifs_err(c, "cannot read inode node at LEB %d:%d, error %d",
1992 zbr->lnum, zbr->offs, err);
1994 return ERR_PTR(err);
1997 fscki = add_inode(c, fsckd, ino);
1999 if (IS_ERR(fscki)) {
2000 ubifs_err(c, "error %ld while adding inode %lu node",
2001 PTR_ERR(fscki), (unsigned long)inum);
2009 * check_leaf - check leaf node.
2010 * @c: UBIFS file-system description object
2011 * @zbr: zbranch of the leaf node to check
2012 * @priv: FS checking information
2014 * This is a helper function for 'dbg_check_filesystem()' which is called for
2015 * every single leaf node while walking the indexing tree. It checks that the
2016 * leaf node referred from the indexing tree exists, has correct CRC, and does
2017 * some other basic validation. This function is also responsible for building
2018 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
2019 * calculates reference count, size, etc for each inode in order to later
2020 * compare them to the information stored inside the inodes and detect possible
2021 * inconsistencies. Returns zero in case of success and a negative error code
2022 * in case of failure.
2024 static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
2029 struct ubifs_ch *ch;
2030 int err, type = key_type(c, &zbr->key);
2031 struct fsck_inode *fscki;
2033 if (zbr->len < UBIFS_CH_SZ) {
2034 ubifs_err(c, "bad leaf length %d (LEB %d:%d)",
2035 zbr->len, zbr->lnum, zbr->offs);
2039 node = kmalloc(zbr->len, GFP_NOFS);
2043 err = ubifs_tnc_read_node(c, zbr, node);
2045 ubifs_err(c, "cannot read leaf node at LEB %d:%d, error %d",
2046 zbr->lnum, zbr->offs, err);
2050 /* If this is an inode node, add it to RB-tree of inodes */
2051 if (type == UBIFS_INO_KEY) {
2052 fscki = add_inode(c, priv, node);
2053 if (IS_ERR(fscki)) {
2054 err = PTR_ERR(fscki);
2055 ubifs_err(c, "error %d while adding inode node", err);
2061 if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
2062 type != UBIFS_DATA_KEY) {
2063 ubifs_err(c, "unexpected node type %d at LEB %d:%d",
2064 type, zbr->lnum, zbr->offs);
2070 if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
2071 ubifs_err(c, "too high sequence number, max. is %llu",
2077 if (type == UBIFS_DATA_KEY) {
2079 struct ubifs_data_node *dn = node;
2081 ubifs_assert(zbr->len >= UBIFS_DATA_NODE_SZ);
2084 * Search the inode node this data node belongs to and insert
2085 * it to the RB-tree of inodes.
2087 inum = key_inum_flash(c, &dn->key);
2088 fscki = read_add_inode(c, priv, inum);
2089 if (IS_ERR(fscki)) {
2090 err = PTR_ERR(fscki);
2091 ubifs_err(c, "error %d while processing data node and trying to find inode node %lu",
2092 err, (unsigned long)inum);
2096 /* Make sure the data node is within inode size */
2097 blk_offs = key_block_flash(c, &dn->key);
2098 blk_offs <<= UBIFS_BLOCK_SHIFT;
2099 blk_offs += le32_to_cpu(dn->size);
2100 if (blk_offs > fscki->size) {
2101 ubifs_err(c, "data node at LEB %d:%d is not within inode size %lld",
2102 zbr->lnum, zbr->offs, fscki->size);
2108 struct ubifs_dent_node *dent = node;
2109 struct fsck_inode *fscki1;
2111 ubifs_assert(zbr->len >= UBIFS_DENT_NODE_SZ);
2113 err = ubifs_validate_entry(c, dent);
2118 * Search the inode node this entry refers to and the parent
2119 * inode node and insert them to the RB-tree of inodes.
2121 inum = le64_to_cpu(dent->inum);
2122 fscki = read_add_inode(c, priv, inum);
2123 if (IS_ERR(fscki)) {
2124 err = PTR_ERR(fscki);
2125 ubifs_err(c, "error %d while processing entry node and trying to find inode node %lu",
2126 err, (unsigned long)inum);
2130 /* Count how many direntries or xentries refers this inode */
2131 fscki->references += 1;
2133 inum = key_inum_flash(c, &dent->key);
2134 fscki1 = read_add_inode(c, priv, inum);
2135 if (IS_ERR(fscki1)) {
2136 err = PTR_ERR(fscki1);
2137 ubifs_err(c, "error %d while processing entry node and trying to find parent inode node %lu",
2138 err, (unsigned long)inum);
2142 nlen = le16_to_cpu(dent->nlen);
2143 if (type == UBIFS_XENT_KEY) {
2144 fscki1->calc_xcnt += 1;
2145 fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
2146 fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
2147 fscki1->calc_xnms += nlen;
2149 fscki1->calc_sz += CALC_DENT_SIZE(nlen);
2150 if (dent->type == UBIFS_ITYPE_DIR)
2151 fscki1->calc_cnt += 1;
2160 ubifs_msg(c, "dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
2161 ubifs_dump_node(c, node);
2168 * free_inodes - free RB-tree of inodes.
2169 * @fsckd: FS checking information
2171 static void free_inodes(struct fsck_data *fsckd)
2173 struct fsck_inode *fscki, *n;
2175 rbtree_postorder_for_each_entry_safe(fscki, n, &fsckd->inodes, rb)
2180 * check_inodes - checks all inodes.
2181 * @c: UBIFS file-system description object
2182 * @fsckd: FS checking information
2184 * This is a helper function for 'dbg_check_filesystem()' which walks the
2185 * RB-tree of inodes after the index scan has been finished, and checks that
2186 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
2187 * %-EINVAL if not, and a negative error code in case of failure.
2189 static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
2192 union ubifs_key key;
2193 struct ubifs_znode *znode;
2194 struct ubifs_zbranch *zbr;
2195 struct ubifs_ino_node *ino;
2196 struct fsck_inode *fscki;
2197 struct rb_node *this = rb_first(&fsckd->inodes);
2200 fscki = rb_entry(this, struct fsck_inode, rb);
2201 this = rb_next(this);
2203 if (S_ISDIR(fscki->mode)) {
2205 * Directories have to have exactly one reference (they
2206 * cannot have hardlinks), although root inode is an
2209 if (fscki->inum != UBIFS_ROOT_INO &&
2210 fscki->references != 1) {
2211 ubifs_err(c, "directory inode %lu has %d direntries which refer it, but should be 1",
2212 (unsigned long)fscki->inum,
2216 if (fscki->inum == UBIFS_ROOT_INO &&
2217 fscki->references != 0) {
2218 ubifs_err(c, "root inode %lu has non-zero (%d) direntries which refer it",
2219 (unsigned long)fscki->inum,
2223 if (fscki->calc_sz != fscki->size) {
2224 ubifs_err(c, "directory inode %lu size is %lld, but calculated size is %lld",
2225 (unsigned long)fscki->inum,
2226 fscki->size, fscki->calc_sz);
2229 if (fscki->calc_cnt != fscki->nlink) {
2230 ubifs_err(c, "directory inode %lu nlink is %d, but calculated nlink is %d",
2231 (unsigned long)fscki->inum,
2232 fscki->nlink, fscki->calc_cnt);
2236 if (fscki->references != fscki->nlink) {
2237 ubifs_err(c, "inode %lu nlink is %d, but calculated nlink is %d",
2238 (unsigned long)fscki->inum,
2239 fscki->nlink, fscki->references);
2243 if (fscki->xattr_sz != fscki->calc_xsz) {
2244 ubifs_err(c, "inode %lu has xattr size %u, but calculated size is %lld",
2245 (unsigned long)fscki->inum, fscki->xattr_sz,
2249 if (fscki->xattr_cnt != fscki->calc_xcnt) {
2250 ubifs_err(c, "inode %lu has %u xattrs, but calculated count is %lld",
2251 (unsigned long)fscki->inum,
2252 fscki->xattr_cnt, fscki->calc_xcnt);
2255 if (fscki->xattr_nms != fscki->calc_xnms) {
2256 ubifs_err(c, "inode %lu has xattr names' size %u, but calculated names' size is %lld",
2257 (unsigned long)fscki->inum, fscki->xattr_nms,
2266 /* Read the bad inode and dump it */
2267 ino_key_init(c, &key, fscki->inum);
2268 err = ubifs_lookup_level0(c, &key, &znode, &n);
2270 ubifs_err(c, "inode %lu not found in index",
2271 (unsigned long)fscki->inum);
2273 } else if (err < 0) {
2274 ubifs_err(c, "error %d while looking up inode %lu",
2275 err, (unsigned long)fscki->inum);
2279 zbr = &znode->zbranch[n];
2280 ino = kmalloc(zbr->len, GFP_NOFS);
2284 err = ubifs_tnc_read_node(c, zbr, ino);
2286 ubifs_err(c, "cannot read inode node at LEB %d:%d, error %d",
2287 zbr->lnum, zbr->offs, err);
2292 ubifs_msg(c, "dump of the inode %lu sitting in LEB %d:%d",
2293 (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
2294 ubifs_dump_node(c, ino);
2300 * dbg_check_filesystem - check the file-system.
2301 * @c: UBIFS file-system description object
2303 * This function checks the file system, namely:
2304 * o makes sure that all leaf nodes exist and their CRCs are correct;
2305 * o makes sure inode nlink, size, xattr size/count are correct (for all
2308 * The function reads whole indexing tree and all nodes, so it is pretty
2309 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2310 * not, and a negative error code in case of failure.
2312 int dbg_check_filesystem(struct ubifs_info *c)
2315 struct fsck_data fsckd;
2317 if (!dbg_is_chk_fs(c))
2320 fsckd.inodes = RB_ROOT;
2321 err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
2325 err = check_inodes(c, &fsckd);
2329 free_inodes(&fsckd);
2333 ubifs_err(c, "file-system check failed with error %d", err);
2335 free_inodes(&fsckd);
2340 * dbg_check_data_nodes_order - check that list of data nodes is sorted.
2341 * @c: UBIFS file-system description object
2342 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2344 * This function returns zero if the list of data nodes is sorted correctly,
2345 * and %-EINVAL if not.
2347 int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head)
2349 struct list_head *cur;
2350 struct ubifs_scan_node *sa, *sb;
2352 if (!dbg_is_chk_gen(c))
2355 for (cur = head->next; cur->next != head; cur = cur->next) {
2357 uint32_t blka, blkb;
2360 sa = container_of(cur, struct ubifs_scan_node, list);
2361 sb = container_of(cur->next, struct ubifs_scan_node, list);
2363 if (sa->type != UBIFS_DATA_NODE) {
2364 ubifs_err(c, "bad node type %d", sa->type);
2365 ubifs_dump_node(c, sa->node);
2368 if (sb->type != UBIFS_DATA_NODE) {
2369 ubifs_err(c, "bad node type %d", sb->type);
2370 ubifs_dump_node(c, sb->node);
2374 inuma = key_inum(c, &sa->key);
2375 inumb = key_inum(c, &sb->key);
2379 if (inuma > inumb) {
2380 ubifs_err(c, "larger inum %lu goes before inum %lu",
2381 (unsigned long)inuma, (unsigned long)inumb);
2385 blka = key_block(c, &sa->key);
2386 blkb = key_block(c, &sb->key);
2389 ubifs_err(c, "larger block %u goes before %u", blka, blkb);
2393 ubifs_err(c, "two data nodes for the same block");
2401 ubifs_dump_node(c, sa->node);
2402 ubifs_dump_node(c, sb->node);
2407 * dbg_check_nondata_nodes_order - check that list of data nodes is sorted.
2408 * @c: UBIFS file-system description object
2409 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2411 * This function returns zero if the list of non-data nodes is sorted correctly,
2412 * and %-EINVAL if not.
2414 int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head)
2416 struct list_head *cur;
2417 struct ubifs_scan_node *sa, *sb;
2419 if (!dbg_is_chk_gen(c))
2422 for (cur = head->next; cur->next != head; cur = cur->next) {
2424 uint32_t hasha, hashb;
2427 sa = container_of(cur, struct ubifs_scan_node, list);
2428 sb = container_of(cur->next, struct ubifs_scan_node, list);
2430 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2431 sa->type != UBIFS_XENT_NODE) {
2432 ubifs_err(c, "bad node type %d", sa->type);
2433 ubifs_dump_node(c, sa->node);
2436 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2437 sa->type != UBIFS_XENT_NODE) {
2438 ubifs_err(c, "bad node type %d", sb->type);
2439 ubifs_dump_node(c, sb->node);
2443 if (sa->type != UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2444 ubifs_err(c, "non-inode node goes before inode node");
2448 if (sa->type == UBIFS_INO_NODE && sb->type != UBIFS_INO_NODE)
2451 if (sa->type == UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2452 /* Inode nodes are sorted in descending size order */
2453 if (sa->len < sb->len) {
2454 ubifs_err(c, "smaller inode node goes first");
2461 * This is either a dentry or xentry, which should be sorted in
2462 * ascending (parent ino, hash) order.
2464 inuma = key_inum(c, &sa->key);
2465 inumb = key_inum(c, &sb->key);
2469 if (inuma > inumb) {
2470 ubifs_err(c, "larger inum %lu goes before inum %lu",
2471 (unsigned long)inuma, (unsigned long)inumb);
2475 hasha = key_block(c, &sa->key);
2476 hashb = key_block(c, &sb->key);
2478 if (hasha > hashb) {
2479 ubifs_err(c, "larger hash %u goes before %u",
2488 ubifs_msg(c, "dumping first node");
2489 ubifs_dump_node(c, sa->node);
2490 ubifs_msg(c, "dumping second node");
2491 ubifs_dump_node(c, sb->node);
2496 static inline int chance(unsigned int n, unsigned int out_of)
2498 return !!((prandom_u32() % out_of) + 1 <= n);
2502 static int power_cut_emulated(struct ubifs_info *c, int lnum, int write)
2504 struct ubifs_debug_info *d = c->dbg;
2506 ubifs_assert(dbg_is_tst_rcvry(c));
2509 /* First call - decide delay to the power cut */
2511 unsigned long delay;
2515 /* Fail within 1 minute */
2516 delay = prandom_u32() % 60000;
2517 d->pc_timeout = jiffies;
2518 d->pc_timeout += msecs_to_jiffies(delay);
2519 ubifs_warn(c, "failing after %lums", delay);
2522 delay = prandom_u32() % 10000;
2523 /* Fail within 10000 operations */
2524 d->pc_cnt_max = delay;
2525 ubifs_warn(c, "failing after %lu calls", delay);
2532 /* Determine if failure delay has expired */
2533 if (d->pc_delay == 1 && time_before(jiffies, d->pc_timeout))
2535 if (d->pc_delay == 2 && d->pc_cnt++ < d->pc_cnt_max)
2538 if (lnum == UBIFS_SB_LNUM) {
2539 if (write && chance(1, 2))
2543 ubifs_warn(c, "failing in super block LEB %d", lnum);
2544 } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2547 ubifs_warn(c, "failing in master LEB %d", lnum);
2548 } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2549 if (write && chance(99, 100))
2551 if (chance(399, 400))
2553 ubifs_warn(c, "failing in log LEB %d", lnum);
2554 } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2555 if (write && chance(7, 8))
2559 ubifs_warn(c, "failing in LPT LEB %d", lnum);
2560 } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2561 if (write && chance(1, 2))
2565 ubifs_warn(c, "failing in orphan LEB %d", lnum);
2566 } else if (lnum == c->ihead_lnum) {
2567 if (chance(99, 100))
2569 ubifs_warn(c, "failing in index head LEB %d", lnum);
2570 } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2573 ubifs_warn(c, "failing in GC head LEB %d", lnum);
2574 } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
2575 !ubifs_search_bud(c, lnum)) {
2578 ubifs_warn(c, "failing in non-bud LEB %d", lnum);
2579 } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
2580 c->cmt_state == COMMIT_RUNNING_REQUIRED) {
2581 if (chance(999, 1000))
2583 ubifs_warn(c, "failing in bud LEB %d commit running", lnum);
2585 if (chance(9999, 10000))
2587 ubifs_warn(c, "failing in bud LEB %d commit not running", lnum);
2591 ubifs_warn(c, "========== Power cut emulated ==========");
2596 static int corrupt_data(const struct ubifs_info *c, const void *buf,
2599 unsigned int from, to, ffs = chance(1, 2);
2600 unsigned char *p = (void *)buf;
2602 from = prandom_u32() % len;
2603 /* Corruption span max to end of write unit */
2604 to = min(len, ALIGN(from + 1, c->max_write_size));
2606 ubifs_warn(c, "filled bytes %u-%u with %s", from, to - 1,
2607 ffs ? "0xFFs" : "random data");
2610 memset(p + from, 0xFF, to - from);
2612 prandom_bytes(p + from, to - from);
2617 int dbg_leb_write(struct ubifs_info *c, int lnum, const void *buf,
2622 if (c->dbg->pc_happened)
2625 failing = power_cut_emulated(c, lnum, 1);
2627 len = corrupt_data(c, buf, len);
2628 ubifs_warn(c, "actually write %d bytes to LEB %d:%d (the buffer was corrupted)",
2631 err = ubi_leb_write(c->ubi, lnum, buf, offs, len);
2639 int dbg_leb_change(struct ubifs_info *c, int lnum, const void *buf,
2644 if (c->dbg->pc_happened)
2646 if (power_cut_emulated(c, lnum, 1))
2648 err = ubi_leb_change(c->ubi, lnum, buf, len);
2651 if (power_cut_emulated(c, lnum, 1))
2656 int dbg_leb_unmap(struct ubifs_info *c, int lnum)
2660 if (c->dbg->pc_happened)
2662 if (power_cut_emulated(c, lnum, 0))
2664 err = ubi_leb_unmap(c->ubi, lnum);
2667 if (power_cut_emulated(c, lnum, 0))
2672 int dbg_leb_map(struct ubifs_info *c, int lnum)
2676 if (c->dbg->pc_happened)
2678 if (power_cut_emulated(c, lnum, 0))
2680 err = ubi_leb_map(c->ubi, lnum);
2683 if (power_cut_emulated(c, lnum, 0))
2689 * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
2690 * contain the stuff specific to particular file-system mounts.
2692 static struct dentry *dfs_rootdir;
2694 static int dfs_file_open(struct inode *inode, struct file *file)
2696 file->private_data = inode->i_private;
2697 return nonseekable_open(inode, file);
2701 * provide_user_output - provide output to the user reading a debugfs file.
2702 * @val: boolean value for the answer
2703 * @u: the buffer to store the answer at
2704 * @count: size of the buffer
2705 * @ppos: position in the @u output buffer
2707 * This is a simple helper function which stores @val boolean value in the user
2708 * buffer when the user reads one of UBIFS debugfs files. Returns amount of
2709 * bytes written to @u in case of success and a negative error code in case of
2712 static int provide_user_output(int val, char __user *u, size_t count,
2724 return simple_read_from_buffer(u, count, ppos, buf, 2);
2727 static ssize_t dfs_file_read(struct file *file, char __user *u, size_t count,
2730 struct dentry *dent = file->f_path.dentry;
2731 struct ubifs_info *c = file->private_data;
2732 struct ubifs_debug_info *d = c->dbg;
2735 if (dent == d->dfs_chk_gen)
2737 else if (dent == d->dfs_chk_index)
2739 else if (dent == d->dfs_chk_orph)
2741 else if (dent == d->dfs_chk_lprops)
2742 val = d->chk_lprops;
2743 else if (dent == d->dfs_chk_fs)
2745 else if (dent == d->dfs_tst_rcvry)
2747 else if (dent == d->dfs_ro_error)
2752 return provide_user_output(val, u, count, ppos);
2756 * interpret_user_input - interpret user debugfs file input.
2757 * @u: user-provided buffer with the input
2758 * @count: buffer size
2760 * This is a helper function which interpret user input to a boolean UBIFS
2761 * debugfs file. Returns %0 or %1 in case of success and a negative error code
2762 * in case of failure.
2764 static int interpret_user_input(const char __user *u, size_t count)
2769 buf_size = min_t(size_t, count, (sizeof(buf) - 1));
2770 if (copy_from_user(buf, u, buf_size))
2775 else if (buf[0] == '0')
2781 static ssize_t dfs_file_write(struct file *file, const char __user *u,
2782 size_t count, loff_t *ppos)
2784 struct ubifs_info *c = file->private_data;
2785 struct ubifs_debug_info *d = c->dbg;
2786 struct dentry *dent = file->f_path.dentry;
2790 * TODO: this is racy - the file-system might have already been
2791 * unmounted and we'd oops in this case. The plan is to fix it with
2792 * help of 'iterate_supers_type()' which we should have in v3.0: when
2793 * a debugfs opened, we rember FS's UUID in file->private_data. Then
2794 * whenever we access the FS via a debugfs file, we iterate all UBIFS
2795 * superblocks and fine the one with the same UUID, and take the
2798 * The other way to go suggested by Al Viro is to create a separate
2799 * 'ubifs-debug' file-system instead.
2801 if (file->f_path.dentry == d->dfs_dump_lprops) {
2802 ubifs_dump_lprops(c);
2805 if (file->f_path.dentry == d->dfs_dump_budg) {
2806 ubifs_dump_budg(c, &c->bi);
2809 if (file->f_path.dentry == d->dfs_dump_tnc) {
2810 mutex_lock(&c->tnc_mutex);
2812 mutex_unlock(&c->tnc_mutex);
2816 val = interpret_user_input(u, count);
2820 if (dent == d->dfs_chk_gen)
2822 else if (dent == d->dfs_chk_index)
2824 else if (dent == d->dfs_chk_orph)
2826 else if (dent == d->dfs_chk_lprops)
2827 d->chk_lprops = val;
2828 else if (dent == d->dfs_chk_fs)
2830 else if (dent == d->dfs_tst_rcvry)
2832 else if (dent == d->dfs_ro_error)
2833 c->ro_error = !!val;
2840 static const struct file_operations dfs_fops = {
2841 .open = dfs_file_open,
2842 .read = dfs_file_read,
2843 .write = dfs_file_write,
2844 .owner = THIS_MODULE,
2845 .llseek = no_llseek,
2849 * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
2850 * @c: UBIFS file-system description object
2852 * This function creates all debugfs files for this instance of UBIFS. Returns
2853 * zero in case of success and a negative error code in case of failure.
2855 * Note, the only reason we have not merged this function with the
2856 * 'ubifs_debugging_init()' function is because it is better to initialize
2857 * debugfs interfaces at the very end of the mount process, and remove them at
2858 * the very beginning of the mount process.
2860 int dbg_debugfs_init_fs(struct ubifs_info *c)
2864 struct dentry *dent;
2865 struct ubifs_debug_info *d = c->dbg;
2867 if (!IS_ENABLED(CONFIG_DEBUG_FS))
2870 n = snprintf(d->dfs_dir_name, UBIFS_DFS_DIR_LEN + 1, UBIFS_DFS_DIR_NAME,
2871 c->vi.ubi_num, c->vi.vol_id);
2872 if (n == UBIFS_DFS_DIR_LEN) {
2873 /* The array size is too small */
2874 fname = UBIFS_DFS_DIR_NAME;
2875 dent = ERR_PTR(-EINVAL);
2879 fname = d->dfs_dir_name;
2880 dent = debugfs_create_dir(fname, dfs_rootdir);
2881 if (IS_ERR_OR_NULL(dent))
2885 fname = "dump_lprops";
2886 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2887 if (IS_ERR_OR_NULL(dent))
2889 d->dfs_dump_lprops = dent;
2891 fname = "dump_budg";
2892 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2893 if (IS_ERR_OR_NULL(dent))
2895 d->dfs_dump_budg = dent;
2898 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2899 if (IS_ERR_OR_NULL(dent))
2901 d->dfs_dump_tnc = dent;
2903 fname = "chk_general";
2904 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2906 if (IS_ERR_OR_NULL(dent))
2908 d->dfs_chk_gen = dent;
2910 fname = "chk_index";
2911 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2913 if (IS_ERR_OR_NULL(dent))
2915 d->dfs_chk_index = dent;
2917 fname = "chk_orphans";
2918 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2920 if (IS_ERR_OR_NULL(dent))
2922 d->dfs_chk_orph = dent;
2924 fname = "chk_lprops";
2925 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2927 if (IS_ERR_OR_NULL(dent))
2929 d->dfs_chk_lprops = dent;
2932 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2934 if (IS_ERR_OR_NULL(dent))
2936 d->dfs_chk_fs = dent;
2938 fname = "tst_recovery";
2939 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2941 if (IS_ERR_OR_NULL(dent))
2943 d->dfs_tst_rcvry = dent;
2946 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2948 if (IS_ERR_OR_NULL(dent))
2950 d->dfs_ro_error = dent;
2955 debugfs_remove_recursive(d->dfs_dir);
2957 err = dent ? PTR_ERR(dent) : -ENODEV;
2958 ubifs_err(c, "cannot create \"%s\" debugfs file or directory, error %d\n",
2964 * dbg_debugfs_exit_fs - remove all debugfs files.
2965 * @c: UBIFS file-system description object
2967 void dbg_debugfs_exit_fs(struct ubifs_info *c)
2969 if (IS_ENABLED(CONFIG_DEBUG_FS))
2970 debugfs_remove_recursive(c->dbg->dfs_dir);
2973 struct ubifs_global_debug_info ubifs_dbg;
2975 static struct dentry *dfs_chk_gen;
2976 static struct dentry *dfs_chk_index;
2977 static struct dentry *dfs_chk_orph;
2978 static struct dentry *dfs_chk_lprops;
2979 static struct dentry *dfs_chk_fs;
2980 static struct dentry *dfs_tst_rcvry;
2982 static ssize_t dfs_global_file_read(struct file *file, char __user *u,
2983 size_t count, loff_t *ppos)
2985 struct dentry *dent = file->f_path.dentry;
2988 if (dent == dfs_chk_gen)
2989 val = ubifs_dbg.chk_gen;
2990 else if (dent == dfs_chk_index)
2991 val = ubifs_dbg.chk_index;
2992 else if (dent == dfs_chk_orph)
2993 val = ubifs_dbg.chk_orph;
2994 else if (dent == dfs_chk_lprops)
2995 val = ubifs_dbg.chk_lprops;
2996 else if (dent == dfs_chk_fs)
2997 val = ubifs_dbg.chk_fs;
2998 else if (dent == dfs_tst_rcvry)
2999 val = ubifs_dbg.tst_rcvry;
3003 return provide_user_output(val, u, count, ppos);
3006 static ssize_t dfs_global_file_write(struct file *file, const char __user *u,
3007 size_t count, loff_t *ppos)
3009 struct dentry *dent = file->f_path.dentry;
3012 val = interpret_user_input(u, count);
3016 if (dent == dfs_chk_gen)
3017 ubifs_dbg.chk_gen = val;
3018 else if (dent == dfs_chk_index)
3019 ubifs_dbg.chk_index = val;
3020 else if (dent == dfs_chk_orph)
3021 ubifs_dbg.chk_orph = val;
3022 else if (dent == dfs_chk_lprops)
3023 ubifs_dbg.chk_lprops = val;
3024 else if (dent == dfs_chk_fs)
3025 ubifs_dbg.chk_fs = val;
3026 else if (dent == dfs_tst_rcvry)
3027 ubifs_dbg.tst_rcvry = val;
3034 static const struct file_operations dfs_global_fops = {
3035 .read = dfs_global_file_read,
3036 .write = dfs_global_file_write,
3037 .owner = THIS_MODULE,
3038 .llseek = no_llseek,
3042 * dbg_debugfs_init - initialize debugfs file-system.
3044 * UBIFS uses debugfs file-system to expose various debugging knobs to
3045 * user-space. This function creates "ubifs" directory in the debugfs
3046 * file-system. Returns zero in case of success and a negative error code in
3049 int dbg_debugfs_init(void)
3053 struct dentry *dent;
3055 if (!IS_ENABLED(CONFIG_DEBUG_FS))
3059 dent = debugfs_create_dir(fname, NULL);
3060 if (IS_ERR_OR_NULL(dent))
3064 fname = "chk_general";
3065 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3067 if (IS_ERR_OR_NULL(dent))
3071 fname = "chk_index";
3072 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3074 if (IS_ERR_OR_NULL(dent))
3076 dfs_chk_index = dent;
3078 fname = "chk_orphans";
3079 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3081 if (IS_ERR_OR_NULL(dent))
3083 dfs_chk_orph = dent;
3085 fname = "chk_lprops";
3086 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3088 if (IS_ERR_OR_NULL(dent))
3090 dfs_chk_lprops = dent;
3093 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3095 if (IS_ERR_OR_NULL(dent))
3099 fname = "tst_recovery";
3100 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3102 if (IS_ERR_OR_NULL(dent))
3104 dfs_tst_rcvry = dent;
3109 debugfs_remove_recursive(dfs_rootdir);
3111 err = dent ? PTR_ERR(dent) : -ENODEV;
3112 pr_err("UBIFS error (pid %d): cannot create \"%s\" debugfs file or directory, error %d\n",
3113 current->pid, fname, err);
3118 * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
3120 void dbg_debugfs_exit(void)
3122 if (IS_ENABLED(CONFIG_DEBUG_FS))
3123 debugfs_remove_recursive(dfs_rootdir);
3127 * ubifs_debugging_init - initialize UBIFS debugging.
3128 * @c: UBIFS file-system description object
3130 * This function initializes debugging-related data for the file system.
3131 * Returns zero in case of success and a negative error code in case of
3134 int ubifs_debugging_init(struct ubifs_info *c)
3136 c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
3144 * ubifs_debugging_exit - free debugging data.
3145 * @c: UBIFS file-system description object
3147 void ubifs_debugging_exit(struct ubifs_info *c)