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.
21 #include <linux/module.h>
22 #include <linux/debugfs.h>
23 #include <linux/math64.h>
24 #include <linux/uaccess.h>
25 #include <linux/random.h>
27 #include <linux/compat.h>
28 #include <linux/err.h>
33 static DEFINE_SPINLOCK(dbg_lock);
36 static const char *get_key_fmt(int fmt)
39 case UBIFS_SIMPLE_KEY_FMT:
42 return "unknown/invalid format";
46 static const char *get_key_hash(int hash)
49 case UBIFS_KEY_HASH_R5:
51 case UBIFS_KEY_HASH_TEST:
54 return "unknown/invalid name hash";
58 static const char *get_key_type(int type)
72 return "unknown/invalid key";
77 static const char *get_dent_type(int type)
90 case UBIFS_ITYPE_FIFO:
92 case UBIFS_ITYPE_SOCK:
95 return "unknown/invalid type";
100 const char *dbg_snprintf_key(const struct ubifs_info *c,
101 const union ubifs_key *key, char *buffer, int len)
104 int type = key_type(c, key);
106 if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
109 len -= snprintf(p, len, "(%lu, %s)",
110 (unsigned long)key_inum(c, key),
115 len -= snprintf(p, len, "(%lu, %s, %#08x)",
116 (unsigned long)key_inum(c, key),
117 get_key_type(type), key_hash(c, key));
120 len -= snprintf(p, len, "(%lu, %s, %u)",
121 (unsigned long)key_inum(c, key),
122 get_key_type(type), key_block(c, key));
125 len -= snprintf(p, len, "(%lu, %s)",
126 (unsigned long)key_inum(c, key),
130 len -= snprintf(p, len, "(bad key type: %#08x, %#08x)",
131 key->u32[0], key->u32[1]);
134 len -= snprintf(p, len, "bad key format %d", c->key_fmt);
135 ubifs_assert(len > 0);
139 const char *dbg_ntype(int type)
143 return "padding node";
145 return "superblock node";
147 return "master node";
149 return "reference node";
152 case UBIFS_DENT_NODE:
153 return "direntry node";
154 case UBIFS_XENT_NODE:
155 return "xentry node";
156 case UBIFS_DATA_NODE:
158 case UBIFS_TRUN_NODE:
159 return "truncate node";
161 return "indexing node";
163 return "commit start node";
164 case UBIFS_ORPH_NODE:
165 return "orphan node";
167 return "unknown node";
171 static const char *dbg_gtype(int type)
174 case UBIFS_NO_NODE_GROUP:
175 return "no node group";
176 case UBIFS_IN_NODE_GROUP:
177 return "in node group";
178 case UBIFS_LAST_OF_NODE_GROUP:
179 return "last of node group";
185 const char *dbg_cstate(int cmt_state)
189 return "commit resting";
190 case COMMIT_BACKGROUND:
191 return "background commit requested";
192 case COMMIT_REQUIRED:
193 return "commit required";
194 case COMMIT_RUNNING_BACKGROUND:
195 return "BACKGROUND commit running";
196 case COMMIT_RUNNING_REQUIRED:
197 return "commit running and required";
199 return "broken commit";
201 return "unknown commit state";
205 const char *dbg_jhead(int jhead)
215 return "unknown journal head";
219 static void dump_ch(const struct ubifs_ch *ch)
221 pr_err("\tmagic %#x\n", le32_to_cpu(ch->magic));
222 pr_err("\tcrc %#x\n", le32_to_cpu(ch->crc));
223 pr_err("\tnode_type %d (%s)\n", ch->node_type,
224 dbg_ntype(ch->node_type));
225 pr_err("\tgroup_type %d (%s)\n", ch->group_type,
226 dbg_gtype(ch->group_type));
227 pr_err("\tsqnum %llu\n",
228 (unsigned long long)le64_to_cpu(ch->sqnum));
229 pr_err("\tlen %u\n", le32_to_cpu(ch->len));
232 void ubifs_dump_inode(struct ubifs_info *c, const struct inode *inode)
235 const struct ubifs_inode *ui = ubifs_inode(inode);
236 struct qstr nm = { .name = NULL };
238 struct ubifs_dent_node *dent, *pdent = NULL;
241 pr_err("Dump in-memory inode:");
242 pr_err("\tinode %lu\n", inode->i_ino);
243 pr_err("\tsize %llu\n",
244 (unsigned long long)i_size_read(inode));
245 pr_err("\tnlink %u\n", inode->i_nlink);
246 pr_err("\tuid %u\n", (unsigned int)i_uid_read(inode));
247 pr_err("\tgid %u\n", (unsigned int)i_gid_read(inode));
248 pr_err("\tatime %u.%u\n",
249 (unsigned int)inode->i_atime.tv_sec,
250 (unsigned int)inode->i_atime.tv_nsec);
251 pr_err("\tmtime %u.%u\n",
252 (unsigned int)inode->i_mtime.tv_sec,
253 (unsigned int)inode->i_mtime.tv_nsec);
254 pr_err("\tctime %u.%u\n",
255 (unsigned int)inode->i_ctime.tv_sec,
256 (unsigned int)inode->i_ctime.tv_nsec);
257 pr_err("\tcreat_sqnum %llu\n", ui->creat_sqnum);
258 pr_err("\txattr_size %u\n", ui->xattr_size);
259 pr_err("\txattr_cnt %u\n", ui->xattr_cnt);
260 pr_err("\txattr_names %u\n", ui->xattr_names);
261 pr_err("\tdirty %u\n", ui->dirty);
262 pr_err("\txattr %u\n", ui->xattr);
263 pr_err("\tbulk_read %u\n", ui->xattr);
264 pr_err("\tsynced_i_size %llu\n",
265 (unsigned long long)ui->synced_i_size);
266 pr_err("\tui_size %llu\n",
267 (unsigned long long)ui->ui_size);
268 pr_err("\tflags %d\n", ui->flags);
269 pr_err("\tcompr_type %d\n", ui->compr_type);
270 pr_err("\tlast_page_read %lu\n", ui->last_page_read);
271 pr_err("\tread_in_a_row %lu\n", ui->read_in_a_row);
272 pr_err("\tdata_len %d\n", ui->data_len);
274 if (!S_ISDIR(inode->i_mode))
277 pr_err("List of directory entries:\n");
278 ubifs_assert(!mutex_is_locked(&c->tnc_mutex));
280 lowest_dent_key(c, &key, inode->i_ino);
282 dent = ubifs_tnc_next_ent(c, &key, &nm);
284 if (PTR_ERR(dent) != -ENOENT)
285 pr_err("error %ld\n", PTR_ERR(dent));
289 pr_err("\t%d: %s (%s)\n",
290 count++, dent->name, get_dent_type(dent->type));
292 nm.name = dent->name;
293 nm.len = le16_to_cpu(dent->nlen);
296 key_read(c, &dent->key, &key);
302 void ubifs_dump_node(const struct ubifs_info *c, const void *node)
306 const struct ubifs_ch *ch = node;
307 char key_buf[DBG_KEY_BUF_LEN];
309 /* If the magic is incorrect, just hexdump the first bytes */
310 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
311 pr_err("Not a node, first %zu bytes:", UBIFS_CH_SZ);
312 print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1,
313 (void *)node, UBIFS_CH_SZ, 1);
317 spin_lock(&dbg_lock);
320 switch (ch->node_type) {
323 const struct ubifs_pad_node *pad = node;
325 pr_err("\tpad_len %u\n", le32_to_cpu(pad->pad_len));
330 const struct ubifs_sb_node *sup = node;
331 unsigned int sup_flags = le32_to_cpu(sup->flags);
333 pr_err("\tkey_hash %d (%s)\n",
334 (int)sup->key_hash, get_key_hash(sup->key_hash));
335 pr_err("\tkey_fmt %d (%s)\n",
336 (int)sup->key_fmt, get_key_fmt(sup->key_fmt));
337 pr_err("\tflags %#x\n", sup_flags);
338 pr_err("\tbig_lpt %u\n",
339 !!(sup_flags & UBIFS_FLG_BIGLPT));
340 pr_err("\tspace_fixup %u\n",
341 !!(sup_flags & UBIFS_FLG_SPACE_FIXUP));
342 pr_err("\tmin_io_size %u\n", le32_to_cpu(sup->min_io_size));
343 pr_err("\tleb_size %u\n", le32_to_cpu(sup->leb_size));
344 pr_err("\tleb_cnt %u\n", le32_to_cpu(sup->leb_cnt));
345 pr_err("\tmax_leb_cnt %u\n", le32_to_cpu(sup->max_leb_cnt));
346 pr_err("\tmax_bud_bytes %llu\n",
347 (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
348 pr_err("\tlog_lebs %u\n", le32_to_cpu(sup->log_lebs));
349 pr_err("\tlpt_lebs %u\n", le32_to_cpu(sup->lpt_lebs));
350 pr_err("\torph_lebs %u\n", le32_to_cpu(sup->orph_lebs));
351 pr_err("\tjhead_cnt %u\n", le32_to_cpu(sup->jhead_cnt));
352 pr_err("\tfanout %u\n", le32_to_cpu(sup->fanout));
353 pr_err("\tlsave_cnt %u\n", le32_to_cpu(sup->lsave_cnt));
354 pr_err("\tdefault_compr %u\n",
355 (int)le16_to_cpu(sup->default_compr));
356 pr_err("\trp_size %llu\n",
357 (unsigned long long)le64_to_cpu(sup->rp_size));
358 pr_err("\trp_uid %u\n", le32_to_cpu(sup->rp_uid));
359 pr_err("\trp_gid %u\n", le32_to_cpu(sup->rp_gid));
360 pr_err("\tfmt_version %u\n", le32_to_cpu(sup->fmt_version));
361 pr_err("\ttime_gran %u\n", le32_to_cpu(sup->time_gran));
362 pr_err("\tUUID %pUB\n", sup->uuid);
367 const struct ubifs_mst_node *mst = node;
369 pr_err("\thighest_inum %llu\n",
370 (unsigned long long)le64_to_cpu(mst->highest_inum));
371 pr_err("\tcommit number %llu\n",
372 (unsigned long long)le64_to_cpu(mst->cmt_no));
373 pr_err("\tflags %#x\n", le32_to_cpu(mst->flags));
374 pr_err("\tlog_lnum %u\n", le32_to_cpu(mst->log_lnum));
375 pr_err("\troot_lnum %u\n", le32_to_cpu(mst->root_lnum));
376 pr_err("\troot_offs %u\n", le32_to_cpu(mst->root_offs));
377 pr_err("\troot_len %u\n", le32_to_cpu(mst->root_len));
378 pr_err("\tgc_lnum %u\n", le32_to_cpu(mst->gc_lnum));
379 pr_err("\tihead_lnum %u\n", le32_to_cpu(mst->ihead_lnum));
380 pr_err("\tihead_offs %u\n", le32_to_cpu(mst->ihead_offs));
381 pr_err("\tindex_size %llu\n",
382 (unsigned long long)le64_to_cpu(mst->index_size));
383 pr_err("\tlpt_lnum %u\n", le32_to_cpu(mst->lpt_lnum));
384 pr_err("\tlpt_offs %u\n", le32_to_cpu(mst->lpt_offs));
385 pr_err("\tnhead_lnum %u\n", le32_to_cpu(mst->nhead_lnum));
386 pr_err("\tnhead_offs %u\n", le32_to_cpu(mst->nhead_offs));
387 pr_err("\tltab_lnum %u\n", le32_to_cpu(mst->ltab_lnum));
388 pr_err("\tltab_offs %u\n", le32_to_cpu(mst->ltab_offs));
389 pr_err("\tlsave_lnum %u\n", le32_to_cpu(mst->lsave_lnum));
390 pr_err("\tlsave_offs %u\n", le32_to_cpu(mst->lsave_offs));
391 pr_err("\tlscan_lnum %u\n", le32_to_cpu(mst->lscan_lnum));
392 pr_err("\tleb_cnt %u\n", le32_to_cpu(mst->leb_cnt));
393 pr_err("\tempty_lebs %u\n", le32_to_cpu(mst->empty_lebs));
394 pr_err("\tidx_lebs %u\n", le32_to_cpu(mst->idx_lebs));
395 pr_err("\ttotal_free %llu\n",
396 (unsigned long long)le64_to_cpu(mst->total_free));
397 pr_err("\ttotal_dirty %llu\n",
398 (unsigned long long)le64_to_cpu(mst->total_dirty));
399 pr_err("\ttotal_used %llu\n",
400 (unsigned long long)le64_to_cpu(mst->total_used));
401 pr_err("\ttotal_dead %llu\n",
402 (unsigned long long)le64_to_cpu(mst->total_dead));
403 pr_err("\ttotal_dark %llu\n",
404 (unsigned long long)le64_to_cpu(mst->total_dark));
409 const struct ubifs_ref_node *ref = node;
411 pr_err("\tlnum %u\n", le32_to_cpu(ref->lnum));
412 pr_err("\toffs %u\n", le32_to_cpu(ref->offs));
413 pr_err("\tjhead %u\n", le32_to_cpu(ref->jhead));
418 const struct ubifs_ino_node *ino = node;
420 key_read(c, &ino->key, &key);
422 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
423 pr_err("\tcreat_sqnum %llu\n",
424 (unsigned long long)le64_to_cpu(ino->creat_sqnum));
425 pr_err("\tsize %llu\n",
426 (unsigned long long)le64_to_cpu(ino->size));
427 pr_err("\tnlink %u\n", le32_to_cpu(ino->nlink));
428 pr_err("\tatime %lld.%u\n",
429 (long long)le64_to_cpu(ino->atime_sec),
430 le32_to_cpu(ino->atime_nsec));
431 pr_err("\tmtime %lld.%u\n",
432 (long long)le64_to_cpu(ino->mtime_sec),
433 le32_to_cpu(ino->mtime_nsec));
434 pr_err("\tctime %lld.%u\n",
435 (long long)le64_to_cpu(ino->ctime_sec),
436 le32_to_cpu(ino->ctime_nsec));
437 pr_err("\tuid %u\n", le32_to_cpu(ino->uid));
438 pr_err("\tgid %u\n", le32_to_cpu(ino->gid));
439 pr_err("\tmode %u\n", le32_to_cpu(ino->mode));
440 pr_err("\tflags %#x\n", le32_to_cpu(ino->flags));
441 pr_err("\txattr_cnt %u\n", le32_to_cpu(ino->xattr_cnt));
442 pr_err("\txattr_size %u\n", le32_to_cpu(ino->xattr_size));
443 pr_err("\txattr_names %u\n", le32_to_cpu(ino->xattr_names));
444 pr_err("\tcompr_type %#x\n",
445 (int)le16_to_cpu(ino->compr_type));
446 pr_err("\tdata len %u\n", le32_to_cpu(ino->data_len));
449 case UBIFS_DENT_NODE:
450 case UBIFS_XENT_NODE:
452 const struct ubifs_dent_node *dent = node;
453 int nlen = le16_to_cpu(dent->nlen);
455 key_read(c, &dent->key, &key);
457 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
458 pr_err("\tinum %llu\n",
459 (unsigned long long)le64_to_cpu(dent->inum));
460 pr_err("\ttype %d\n", (int)dent->type);
461 pr_err("\tnlen %d\n", nlen);
464 if (nlen > UBIFS_MAX_NLEN)
465 pr_err("(bad name length, not printing, bad or corrupted node)");
467 for (i = 0; i < nlen && dent->name[i]; i++)
468 pr_cont("%c", dent->name[i]);
474 case UBIFS_DATA_NODE:
476 const struct ubifs_data_node *dn = node;
477 int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
479 key_read(c, &dn->key, &key);
481 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
482 pr_err("\tsize %u\n", le32_to_cpu(dn->size));
483 pr_err("\tcompr_typ %d\n",
484 (int)le16_to_cpu(dn->compr_type));
485 pr_err("\tdata size %d\n", dlen);
487 print_hex_dump("\t", DUMP_PREFIX_OFFSET, 32, 1,
488 (void *)&dn->data, dlen, 0);
491 case UBIFS_TRUN_NODE:
493 const struct ubifs_trun_node *trun = node;
495 pr_err("\tinum %u\n", le32_to_cpu(trun->inum));
496 pr_err("\told_size %llu\n",
497 (unsigned long long)le64_to_cpu(trun->old_size));
498 pr_err("\tnew_size %llu\n",
499 (unsigned long long)le64_to_cpu(trun->new_size));
504 const struct ubifs_idx_node *idx = node;
506 n = le16_to_cpu(idx->child_cnt);
507 pr_err("\tchild_cnt %d\n", n);
508 pr_err("\tlevel %d\n", (int)le16_to_cpu(idx->level));
509 pr_err("\tBranches:\n");
511 for (i = 0; i < n && i < c->fanout - 1; i++) {
512 const struct ubifs_branch *br;
514 br = ubifs_idx_branch(c, idx, i);
515 key_read(c, &br->key, &key);
516 pr_err("\t%d: LEB %d:%d len %d key %s\n",
517 i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
518 le32_to_cpu(br->len),
519 dbg_snprintf_key(c, &key, key_buf,
526 case UBIFS_ORPH_NODE:
528 const struct ubifs_orph_node *orph = node;
530 pr_err("\tcommit number %llu\n",
532 le64_to_cpu(orph->cmt_no) & LLONG_MAX);
533 pr_err("\tlast node flag %llu\n",
534 (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
535 n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
536 pr_err("\t%d orphan inode numbers:\n", n);
537 for (i = 0; i < n; i++)
538 pr_err("\t ino %llu\n",
539 (unsigned long long)le64_to_cpu(orph->inos[i]));
543 pr_err("node type %d was not recognized\n",
546 spin_unlock(&dbg_lock);
549 void ubifs_dump_budget_req(const struct ubifs_budget_req *req)
551 spin_lock(&dbg_lock);
552 pr_err("Budgeting request: new_ino %d, dirtied_ino %d\n",
553 req->new_ino, req->dirtied_ino);
554 pr_err("\tnew_ino_d %d, dirtied_ino_d %d\n",
555 req->new_ino_d, req->dirtied_ino_d);
556 pr_err("\tnew_page %d, dirtied_page %d\n",
557 req->new_page, req->dirtied_page);
558 pr_err("\tnew_dent %d, mod_dent %d\n",
559 req->new_dent, req->mod_dent);
560 pr_err("\tidx_growth %d\n", req->idx_growth);
561 pr_err("\tdata_growth %d dd_growth %d\n",
562 req->data_growth, req->dd_growth);
563 spin_unlock(&dbg_lock);
566 void ubifs_dump_lstats(const struct ubifs_lp_stats *lst)
568 spin_lock(&dbg_lock);
569 pr_err("(pid %d) Lprops statistics: empty_lebs %d, idx_lebs %d\n",
570 current->pid, lst->empty_lebs, lst->idx_lebs);
571 pr_err("\ttaken_empty_lebs %d, total_free %lld, total_dirty %lld\n",
572 lst->taken_empty_lebs, lst->total_free, lst->total_dirty);
573 pr_err("\ttotal_used %lld, total_dark %lld, total_dead %lld\n",
574 lst->total_used, lst->total_dark, lst->total_dead);
575 spin_unlock(&dbg_lock);
579 void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)
583 struct ubifs_bud *bud;
584 struct ubifs_gced_idx_leb *idx_gc;
585 long long available, outstanding, free;
587 spin_lock(&c->space_lock);
588 spin_lock(&dbg_lock);
589 pr_err("(pid %d) Budgeting info: data budget sum %lld, total budget sum %lld\n",
590 current->pid, bi->data_growth + bi->dd_growth,
591 bi->data_growth + bi->dd_growth + bi->idx_growth);
592 pr_err("\tbudg_data_growth %lld, budg_dd_growth %lld, budg_idx_growth %lld\n",
593 bi->data_growth, bi->dd_growth, bi->idx_growth);
594 pr_err("\tmin_idx_lebs %d, old_idx_sz %llu, uncommitted_idx %lld\n",
595 bi->min_idx_lebs, bi->old_idx_sz, bi->uncommitted_idx);
596 pr_err("\tpage_budget %d, inode_budget %d, dent_budget %d\n",
597 bi->page_budget, bi->inode_budget, bi->dent_budget);
598 pr_err("\tnospace %u, nospace_rp %u\n", bi->nospace, bi->nospace_rp);
599 pr_err("\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
600 c->dark_wm, c->dead_wm, c->max_idx_node_sz);
604 * If we are dumping saved budgeting data, do not print
605 * additional information which is about the current state, not
606 * the old one which corresponded to the saved budgeting data.
610 pr_err("\tfreeable_cnt %d, calc_idx_sz %lld, idx_gc_cnt %d\n",
611 c->freeable_cnt, c->calc_idx_sz, c->idx_gc_cnt);
612 pr_err("\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, clean_zn_cnt %ld\n",
613 atomic_long_read(&c->dirty_pg_cnt),
614 atomic_long_read(&c->dirty_zn_cnt),
615 atomic_long_read(&c->clean_zn_cnt));
616 pr_err("\tgc_lnum %d, ihead_lnum %d\n", c->gc_lnum, c->ihead_lnum);
618 /* If we are in R/O mode, journal heads do not exist */
620 for (i = 0; i < c->jhead_cnt; i++)
621 pr_err("\tjhead %s\t LEB %d\n",
622 dbg_jhead(c->jheads[i].wbuf.jhead),
623 c->jheads[i].wbuf.lnum);
624 for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
625 bud = rb_entry(rb, struct ubifs_bud, rb);
626 pr_err("\tbud LEB %d\n", bud->lnum);
628 list_for_each_entry(bud, &c->old_buds, list)
629 pr_err("\told bud LEB %d\n", bud->lnum);
630 list_for_each_entry(idx_gc, &c->idx_gc, list)
631 pr_err("\tGC'ed idx LEB %d unmap %d\n",
632 idx_gc->lnum, idx_gc->unmap);
633 pr_err("\tcommit state %d\n", c->cmt_state);
635 /* Print budgeting predictions */
636 available = ubifs_calc_available(c, c->bi.min_idx_lebs);
637 outstanding = c->bi.data_growth + c->bi.dd_growth;
638 free = ubifs_get_free_space_nolock(c);
639 pr_err("Budgeting predictions:\n");
640 pr_err("\tavailable: %lld, outstanding %lld, free %lld\n",
641 available, outstanding, free);
643 spin_unlock(&dbg_lock);
644 spin_unlock(&c->space_lock);
647 void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)
652 void ubifs_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
654 int i, spc, dark = 0, dead = 0;
656 struct ubifs_bud *bud;
658 spc = lp->free + lp->dirty;
659 if (spc < c->dead_wm)
662 dark = ubifs_calc_dark(c, spc);
664 if (lp->flags & LPROPS_INDEX)
665 pr_err("LEB %-7d free %-8d dirty %-8d used %-8d free + dirty %-8d flags %#x (",
666 lp->lnum, lp->free, lp->dirty, c->leb_size - spc, spc,
669 pr_err("LEB %-7d free %-8d dirty %-8d used %-8d free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d flags %#-4x (",
670 lp->lnum, lp->free, lp->dirty, c->leb_size - spc, spc,
671 dark, dead, (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags);
673 if (lp->flags & LPROPS_TAKEN) {
674 if (lp->flags & LPROPS_INDEX)
675 pr_cont("index, taken");
681 if (lp->flags & LPROPS_INDEX) {
682 switch (lp->flags & LPROPS_CAT_MASK) {
683 case LPROPS_DIRTY_IDX:
686 case LPROPS_FRDI_IDX:
687 s = "freeable index";
693 switch (lp->flags & LPROPS_CAT_MASK) {
695 s = "not categorized";
706 case LPROPS_FREEABLE:
717 for (rb = rb_first((struct rb_root *)&c->buds); rb; rb = rb_next(rb)) {
718 bud = rb_entry(rb, struct ubifs_bud, rb);
719 if (bud->lnum == lp->lnum) {
721 for (i = 0; i < c->jhead_cnt; i++) {
723 * Note, if we are in R/O mode or in the middle
724 * of mounting/re-mounting, the write-buffers do
728 lp->lnum == c->jheads[i].wbuf.lnum) {
729 pr_cont(", jhead %s", dbg_jhead(i));
734 pr_cont(", bud of jhead %s",
735 dbg_jhead(bud->jhead));
738 if (lp->lnum == c->gc_lnum)
743 void ubifs_dump_lprops(struct ubifs_info *c)
746 struct ubifs_lprops lp;
747 struct ubifs_lp_stats lst;
749 pr_err("(pid %d) start dumping LEB properties\n", current->pid);
750 ubifs_get_lp_stats(c, &lst);
751 ubifs_dump_lstats(&lst);
753 for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
754 err = ubifs_read_one_lp(c, lnum, &lp);
756 ubifs_err(c, "cannot read lprops for LEB %d", lnum);
760 ubifs_dump_lprop(c, &lp);
762 pr_err("(pid %d) finish dumping LEB properties\n", current->pid);
765 void ubifs_dump_lpt_info(struct ubifs_info *c)
769 spin_lock(&dbg_lock);
770 pr_err("(pid %d) dumping LPT information\n", current->pid);
771 pr_err("\tlpt_sz: %lld\n", c->lpt_sz);
772 pr_err("\tpnode_sz: %d\n", c->pnode_sz);
773 pr_err("\tnnode_sz: %d\n", c->nnode_sz);
774 pr_err("\tltab_sz: %d\n", c->ltab_sz);
775 pr_err("\tlsave_sz: %d\n", c->lsave_sz);
776 pr_err("\tbig_lpt: %d\n", c->big_lpt);
777 pr_err("\tlpt_hght: %d\n", c->lpt_hght);
778 pr_err("\tpnode_cnt: %d\n", c->pnode_cnt);
779 pr_err("\tnnode_cnt: %d\n", c->nnode_cnt);
780 pr_err("\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt);
781 pr_err("\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt);
782 pr_err("\tlsave_cnt: %d\n", c->lsave_cnt);
783 pr_err("\tspace_bits: %d\n", c->space_bits);
784 pr_err("\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
785 pr_err("\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
786 pr_err("\tlpt_spc_bits: %d\n", c->lpt_spc_bits);
787 pr_err("\tpcnt_bits: %d\n", c->pcnt_bits);
788 pr_err("\tlnum_bits: %d\n", c->lnum_bits);
789 pr_err("\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
790 pr_err("\tLPT head is at %d:%d\n",
791 c->nhead_lnum, c->nhead_offs);
792 pr_err("\tLPT ltab is at %d:%d\n", c->ltab_lnum, c->ltab_offs);
794 pr_err("\tLPT lsave is at %d:%d\n",
795 c->lsave_lnum, c->lsave_offs);
796 for (i = 0; i < c->lpt_lebs; i++)
797 pr_err("\tLPT LEB %d free %d dirty %d tgc %d cmt %d\n",
798 i + c->lpt_first, c->ltab[i].free, c->ltab[i].dirty,
799 c->ltab[i].tgc, c->ltab[i].cmt);
800 spin_unlock(&dbg_lock);
803 void ubifs_dump_sleb(const struct ubifs_info *c,
804 const struct ubifs_scan_leb *sleb, int offs)
806 struct ubifs_scan_node *snod;
808 pr_err("(pid %d) start dumping scanned data from LEB %d:%d\n",
809 current->pid, sleb->lnum, offs);
811 list_for_each_entry(snod, &sleb->nodes, list) {
813 pr_err("Dumping node at LEB %d:%d len %d\n",
814 sleb->lnum, snod->offs, snod->len);
815 ubifs_dump_node(c, snod->node);
819 void ubifs_dump_leb(const struct ubifs_info *c, int lnum)
821 struct ubifs_scan_leb *sleb;
822 struct ubifs_scan_node *snod;
825 pr_err("(pid %d) start dumping LEB %d\n", current->pid, lnum);
827 buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
829 ubifs_err(c, "cannot allocate memory for dumping LEB %d", lnum);
833 sleb = ubifs_scan(c, lnum, 0, buf, 0);
835 ubifs_err(c, "scan error %d", (int)PTR_ERR(sleb));
839 pr_err("LEB %d has %d nodes ending at %d\n", lnum,
840 sleb->nodes_cnt, sleb->endpt);
842 list_for_each_entry(snod, &sleb->nodes, list) {
844 pr_err("Dumping node at LEB %d:%d len %d\n", lnum,
845 snod->offs, snod->len);
846 ubifs_dump_node(c, snod->node);
849 pr_err("(pid %d) finish dumping LEB %d\n", current->pid, lnum);
850 ubifs_scan_destroy(sleb);
857 void ubifs_dump_znode(const struct ubifs_info *c,
858 const struct ubifs_znode *znode)
861 const struct ubifs_zbranch *zbr;
862 char key_buf[DBG_KEY_BUF_LEN];
864 spin_lock(&dbg_lock);
866 zbr = &znode->parent->zbranch[znode->iip];
870 pr_err("znode %p, LEB %d:%d len %d parent %p iip %d level %d child_cnt %d flags %lx\n",
871 znode, zbr->lnum, zbr->offs, zbr->len, znode->parent, znode->iip,
872 znode->level, znode->child_cnt, znode->flags);
874 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
875 spin_unlock(&dbg_lock);
879 pr_err("zbranches:\n");
880 for (n = 0; n < znode->child_cnt; n++) {
881 zbr = &znode->zbranch[n];
882 if (znode->level > 0)
883 pr_err("\t%d: znode %p LEB %d:%d len %d key %s\n",
884 n, zbr->znode, zbr->lnum, zbr->offs, zbr->len,
885 dbg_snprintf_key(c, &zbr->key, key_buf,
888 pr_err("\t%d: LNC %p LEB %d:%d len %d key %s\n",
889 n, zbr->znode, zbr->lnum, zbr->offs, zbr->len,
890 dbg_snprintf_key(c, &zbr->key, key_buf,
893 spin_unlock(&dbg_lock);
896 void ubifs_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
900 pr_err("(pid %d) start dumping heap cat %d (%d elements)\n",
901 current->pid, cat, heap->cnt);
902 for (i = 0; i < heap->cnt; i++) {
903 struct ubifs_lprops *lprops = heap->arr[i];
905 pr_err("\t%d. LEB %d hpos %d free %d dirty %d flags %d\n",
906 i, lprops->lnum, lprops->hpos, lprops->free,
907 lprops->dirty, lprops->flags);
909 pr_err("(pid %d) finish dumping heap\n", current->pid);
912 void ubifs_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
913 struct ubifs_nnode *parent, int iip)
917 pr_err("(pid %d) dumping pnode:\n", current->pid);
918 pr_err("\taddress %zx parent %zx cnext %zx\n",
919 (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
920 pr_err("\tflags %lu iip %d level %d num %d\n",
921 pnode->flags, iip, pnode->level, pnode->num);
922 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
923 struct ubifs_lprops *lp = &pnode->lprops[i];
925 pr_err("\t%d: free %d dirty %d flags %d lnum %d\n",
926 i, lp->free, lp->dirty, lp->flags, lp->lnum);
930 void ubifs_dump_tnc(struct ubifs_info *c)
932 struct ubifs_znode *znode;
936 pr_err("(pid %d) start dumping TNC tree\n", current->pid);
937 znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
938 level = znode->level;
939 pr_err("== Level %d ==\n", level);
941 if (level != znode->level) {
942 level = znode->level;
943 pr_err("== Level %d ==\n", level);
945 ubifs_dump_znode(c, znode);
946 znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
948 pr_err("(pid %d) finish dumping TNC tree\n", current->pid);
951 static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
954 ubifs_dump_znode(c, znode);
959 * ubifs_dump_index - dump the on-flash index.
960 * @c: UBIFS file-system description object
962 * This function dumps whole UBIFS indexing B-tree, unlike 'ubifs_dump_tnc()'
963 * which dumps only in-memory znodes and does not read znodes which from flash.
965 void ubifs_dump_index(struct ubifs_info *c)
967 dbg_walk_index(c, NULL, dump_znode, NULL);
972 * dbg_save_space_info - save information about flash space.
973 * @c: UBIFS file-system description object
975 * This function saves information about UBIFS free space, dirty space, etc, in
976 * order to check it later.
978 void dbg_save_space_info(struct ubifs_info *c)
980 struct ubifs_debug_info *d = c->dbg;
983 spin_lock(&c->space_lock);
984 memcpy(&d->saved_lst, &c->lst, sizeof(struct ubifs_lp_stats));
985 memcpy(&d->saved_bi, &c->bi, sizeof(struct ubifs_budg_info));
986 d->saved_idx_gc_cnt = c->idx_gc_cnt;
989 * We use a dirty hack here and zero out @c->freeable_cnt, because it
990 * affects the free space calculations, and UBIFS might not know about
991 * all freeable eraseblocks. Indeed, we know about freeable eraseblocks
992 * only when we read their lprops, and we do this only lazily, upon the
993 * need. So at any given point of time @c->freeable_cnt might be not
996 * Just one example about the issue we hit when we did not zero
998 * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the
999 * amount of free space in @d->saved_free
1000 * 2. We re-mount R/W, which makes UBIFS to read the "lsave"
1001 * information from flash, where we cache LEBs from various
1002 * categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()'
1003 * -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()'
1004 * -> 'ubifs_get_pnode()' -> 'update_cats()'
1005 * -> 'ubifs_add_to_cat()').
1006 * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt
1008 * 4. We calculate the amount of free space when the re-mount is
1009 * finished in 'dbg_check_space_info()' and it does not match
1012 freeable_cnt = c->freeable_cnt;
1013 c->freeable_cnt = 0;
1014 d->saved_free = ubifs_get_free_space_nolock(c);
1015 c->freeable_cnt = freeable_cnt;
1016 spin_unlock(&c->space_lock);
1020 * dbg_check_space_info - check flash space information.
1021 * @c: UBIFS file-system description object
1023 * This function compares current flash space information with the information
1024 * which was saved when the 'dbg_save_space_info()' function was called.
1025 * Returns zero if the information has not changed, and %-EINVAL it it has
1028 int dbg_check_space_info(struct ubifs_info *c)
1030 struct ubifs_debug_info *d = c->dbg;
1031 struct ubifs_lp_stats lst;
1035 spin_lock(&c->space_lock);
1036 freeable_cnt = c->freeable_cnt;
1037 c->freeable_cnt = 0;
1038 free = ubifs_get_free_space_nolock(c);
1039 c->freeable_cnt = freeable_cnt;
1040 spin_unlock(&c->space_lock);
1042 if (free != d->saved_free) {
1043 ubifs_err(c, "free space changed from %lld to %lld",
1044 d->saved_free, free);
1051 ubifs_msg(c, "saved lprops statistics dump");
1052 ubifs_dump_lstats(&d->saved_lst);
1053 ubifs_msg(c, "saved budgeting info dump");
1054 ubifs_dump_budg(c, &d->saved_bi);
1055 ubifs_msg(c, "saved idx_gc_cnt %d", d->saved_idx_gc_cnt);
1056 ubifs_msg(c, "current lprops statistics dump");
1057 ubifs_get_lp_stats(c, &lst);
1058 ubifs_dump_lstats(&lst);
1059 ubifs_msg(c, "current budgeting info dump");
1060 ubifs_dump_budg(c, &c->bi);
1066 * dbg_check_synced_i_size - check synchronized inode size.
1067 * @c: UBIFS file-system description object
1068 * @inode: inode to check
1070 * If inode is clean, synchronized inode size has to be equivalent to current
1071 * inode size. This function has to be called only for locked inodes (@i_mutex
1072 * has to be locked). Returns %0 if synchronized inode size if correct, and
1075 int dbg_check_synced_i_size(const struct ubifs_info *c, struct inode *inode)
1078 struct ubifs_inode *ui = ubifs_inode(inode);
1080 if (!dbg_is_chk_gen(c))
1082 if (!S_ISREG(inode->i_mode))
1085 mutex_lock(&ui->ui_mutex);
1086 spin_lock(&ui->ui_lock);
1087 if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
1088 ubifs_err(c, "ui_size is %lld, synced_i_size is %lld, but inode is clean",
1089 ui->ui_size, ui->synced_i_size);
1090 ubifs_err(c, "i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
1091 inode->i_mode, i_size_read(inode));
1095 spin_unlock(&ui->ui_lock);
1096 mutex_unlock(&ui->ui_mutex);
1101 * dbg_check_dir - check directory inode size and link count.
1102 * @c: UBIFS file-system description object
1103 * @dir: the directory to calculate size for
1104 * @size: the result is returned here
1106 * This function makes sure that directory size and link count are correct.
1107 * Returns zero in case of success and a negative error code in case of
1110 * Note, it is good idea to make sure the @dir->i_mutex is locked before
1111 * calling this function.
1113 int dbg_check_dir(struct ubifs_info *c, const struct inode *dir)
1115 unsigned int nlink = 2;
1116 union ubifs_key key;
1117 struct ubifs_dent_node *dent, *pdent = NULL;
1118 struct qstr nm = { .name = NULL };
1119 loff_t size = UBIFS_INO_NODE_SZ;
1121 if (!dbg_is_chk_gen(c))
1124 if (!S_ISDIR(dir->i_mode))
1127 lowest_dent_key(c, &key, dir->i_ino);
1131 dent = ubifs_tnc_next_ent(c, &key, &nm);
1133 err = PTR_ERR(dent);
1139 nm.name = dent->name;
1140 nm.len = le16_to_cpu(dent->nlen);
1141 size += CALC_DENT_SIZE(nm.len);
1142 if (dent->type == UBIFS_ITYPE_DIR)
1146 key_read(c, &dent->key, &key);
1150 if (i_size_read(dir) != size) {
1151 ubifs_err(c, "directory inode %lu has size %llu, but calculated size is %llu",
1152 dir->i_ino, (unsigned long long)i_size_read(dir),
1153 (unsigned long long)size);
1154 ubifs_dump_inode(c, dir);
1158 if (dir->i_nlink != nlink) {
1159 ubifs_err(c, "directory inode %lu has nlink %u, but calculated nlink is %u",
1160 dir->i_ino, dir->i_nlink, nlink);
1161 ubifs_dump_inode(c, dir);
1170 * dbg_check_key_order - make sure that colliding keys are properly ordered.
1171 * @c: UBIFS file-system description object
1172 * @zbr1: first zbranch
1173 * @zbr2: following zbranch
1175 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
1176 * names of the direntries/xentries which are referred by the keys. This
1177 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
1178 * sure the name of direntry/xentry referred by @zbr1 is less than
1179 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
1180 * and a negative error code in case of failure.
1182 static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
1183 struct ubifs_zbranch *zbr2)
1185 int err, nlen1, nlen2, cmp;
1186 struct ubifs_dent_node *dent1, *dent2;
1187 union ubifs_key key;
1188 char key_buf[DBG_KEY_BUF_LEN];
1190 ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
1191 dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1194 dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1200 err = ubifs_tnc_read_node(c, zbr1, dent1);
1203 err = ubifs_validate_entry(c, dent1);
1207 err = ubifs_tnc_read_node(c, zbr2, dent2);
1210 err = ubifs_validate_entry(c, dent2);
1214 /* Make sure node keys are the same as in zbranch */
1216 key_read(c, &dent1->key, &key);
1217 if (keys_cmp(c, &zbr1->key, &key)) {
1218 ubifs_err(c, "1st entry at %d:%d has key %s", zbr1->lnum,
1219 zbr1->offs, dbg_snprintf_key(c, &key, key_buf,
1221 ubifs_err(c, "but it should have key %s according to tnc",
1222 dbg_snprintf_key(c, &zbr1->key, key_buf,
1224 ubifs_dump_node(c, dent1);
1228 key_read(c, &dent2->key, &key);
1229 if (keys_cmp(c, &zbr2->key, &key)) {
1230 ubifs_err(c, "2nd entry at %d:%d has key %s", zbr1->lnum,
1231 zbr1->offs, dbg_snprintf_key(c, &key, key_buf,
1233 ubifs_err(c, "but it should have key %s according to tnc",
1234 dbg_snprintf_key(c, &zbr2->key, key_buf,
1236 ubifs_dump_node(c, dent2);
1240 nlen1 = le16_to_cpu(dent1->nlen);
1241 nlen2 = le16_to_cpu(dent2->nlen);
1243 cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
1244 if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
1248 if (cmp == 0 && nlen1 == nlen2)
1249 ubifs_err(c, "2 xent/dent nodes with the same name");
1251 ubifs_err(c, "bad order of colliding key %s",
1252 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
1254 ubifs_msg(c, "first node at %d:%d\n", zbr1->lnum, zbr1->offs);
1255 ubifs_dump_node(c, dent1);
1256 ubifs_msg(c, "second node at %d:%d\n", zbr2->lnum, zbr2->offs);
1257 ubifs_dump_node(c, dent2);
1266 * dbg_check_znode - check if znode is all right.
1267 * @c: UBIFS file-system description object
1268 * @zbr: zbranch which points to this znode
1270 * This function makes sure that znode referred to by @zbr is all right.
1271 * Returns zero if it is, and %-EINVAL if it is not.
1273 static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
1275 struct ubifs_znode *znode = zbr->znode;
1276 struct ubifs_znode *zp = znode->parent;
1279 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
1283 if (znode->level < 0) {
1287 if (znode->iip < 0 || znode->iip >= c->fanout) {
1293 /* Only dirty zbranch may have no on-flash nodes */
1294 if (!ubifs_zn_dirty(znode)) {
1299 if (ubifs_zn_dirty(znode)) {
1301 * If znode is dirty, its parent has to be dirty as well. The
1302 * order of the operation is important, so we have to have
1306 if (zp && !ubifs_zn_dirty(zp)) {
1308 * The dirty flag is atomic and is cleared outside the
1309 * TNC mutex, so znode's dirty flag may now have
1310 * been cleared. The child is always cleared before the
1311 * parent, so we just need to check again.
1314 if (ubifs_zn_dirty(znode)) {
1322 const union ubifs_key *min, *max;
1324 if (znode->level != zp->level - 1) {
1329 /* Make sure the 'parent' pointer in our znode is correct */
1330 err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
1332 /* This zbranch does not exist in the parent */
1337 if (znode->iip >= zp->child_cnt) {
1342 if (znode->iip != n) {
1343 /* This may happen only in case of collisions */
1344 if (keys_cmp(c, &zp->zbranch[n].key,
1345 &zp->zbranch[znode->iip].key)) {
1353 * Make sure that the first key in our znode is greater than or
1354 * equal to the key in the pointing zbranch.
1357 cmp = keys_cmp(c, min, &znode->zbranch[0].key);
1363 if (n + 1 < zp->child_cnt) {
1364 max = &zp->zbranch[n + 1].key;
1367 * Make sure the last key in our znode is less or
1368 * equivalent than the key in the zbranch which goes
1369 * after our pointing zbranch.
1371 cmp = keys_cmp(c, max,
1372 &znode->zbranch[znode->child_cnt - 1].key);
1379 /* This may only be root znode */
1380 if (zbr != &c->zroot) {
1387 * Make sure that next key is greater or equivalent then the previous
1390 for (n = 1; n < znode->child_cnt; n++) {
1391 cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
1392 &znode->zbranch[n].key);
1398 /* This can only be keys with colliding hash */
1399 if (!is_hash_key(c, &znode->zbranch[n].key)) {
1404 if (znode->level != 0 || c->replaying)
1408 * Colliding keys should follow binary order of
1409 * corresponding xentry/dentry names.
1411 err = dbg_check_key_order(c, &znode->zbranch[n - 1],
1412 &znode->zbranch[n]);
1422 for (n = 0; n < znode->child_cnt; n++) {
1423 if (!znode->zbranch[n].znode &&
1424 (znode->zbranch[n].lnum == 0 ||
1425 znode->zbranch[n].len == 0)) {
1430 if (znode->zbranch[n].lnum != 0 &&
1431 znode->zbranch[n].len == 0) {
1436 if (znode->zbranch[n].lnum == 0 &&
1437 znode->zbranch[n].len != 0) {
1442 if (znode->zbranch[n].lnum == 0 &&
1443 znode->zbranch[n].offs != 0) {
1448 if (znode->level != 0 && znode->zbranch[n].znode)
1449 if (znode->zbranch[n].znode->parent != znode) {
1458 ubifs_err(c, "failed, error %d", err);
1459 ubifs_msg(c, "dump of the znode");
1460 ubifs_dump_znode(c, znode);
1462 ubifs_msg(c, "dump of the parent znode");
1463 ubifs_dump_znode(c, zp);
1470 int dbg_check_dir(struct ubifs_info *c, const struct inode *dir)
1475 void dbg_debugfs_exit_fs(struct ubifs_info *c)
1480 int ubifs_debugging_init(struct ubifs_info *c)
1484 void ubifs_debugging_exit(struct ubifs_info *c)
1487 int dbg_check_filesystem(struct ubifs_info *c)
1491 int dbg_debugfs_init_fs(struct ubifs_info *c)
1499 * dbg_check_tnc - check TNC tree.
1500 * @c: UBIFS file-system description object
1501 * @extra: do extra checks that are possible at start commit
1503 * This function traverses whole TNC tree and checks every znode. Returns zero
1504 * if everything is all right and %-EINVAL if something is wrong with TNC.
1506 int dbg_check_tnc(struct ubifs_info *c, int extra)
1508 struct ubifs_znode *znode;
1509 long clean_cnt = 0, dirty_cnt = 0;
1512 if (!dbg_is_chk_index(c))
1515 ubifs_assert(mutex_is_locked(&c->tnc_mutex));
1516 if (!c->zroot.znode)
1519 znode = ubifs_tnc_postorder_first(c->zroot.znode);
1521 struct ubifs_znode *prev;
1522 struct ubifs_zbranch *zbr;
1527 zbr = &znode->parent->zbranch[znode->iip];
1529 err = dbg_check_znode(c, zbr);
1534 if (ubifs_zn_dirty(znode))
1541 znode = ubifs_tnc_postorder_next(znode);
1546 * If the last key of this znode is equivalent to the first key
1547 * of the next znode (collision), then check order of the keys.
1549 last = prev->child_cnt - 1;
1550 if (prev->level == 0 && znode->level == 0 && !c->replaying &&
1551 !keys_cmp(c, &prev->zbranch[last].key,
1552 &znode->zbranch[0].key)) {
1553 err = dbg_check_key_order(c, &prev->zbranch[last],
1554 &znode->zbranch[0]);
1558 ubifs_msg(c, "first znode");
1559 ubifs_dump_znode(c, prev);
1560 ubifs_msg(c, "second znode");
1561 ubifs_dump_znode(c, znode);
1568 if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
1569 ubifs_err(c, "incorrect clean_zn_cnt %ld, calculated %ld",
1570 atomic_long_read(&c->clean_zn_cnt),
1574 if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
1575 ubifs_err(c, "incorrect dirty_zn_cnt %ld, calculated %ld",
1576 atomic_long_read(&c->dirty_zn_cnt),
1585 int dbg_check_tnc(struct ubifs_info *c, int extra)
1592 * dbg_walk_index - walk the on-flash index.
1593 * @c: UBIFS file-system description object
1594 * @leaf_cb: called for each leaf node
1595 * @znode_cb: called for each indexing node
1596 * @priv: private data which is passed to callbacks
1598 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1599 * node and @znode_cb for each indexing node. Returns zero in case of success
1600 * and a negative error code in case of failure.
1602 * It would be better if this function removed every znode it pulled to into
1603 * the TNC, so that the behavior more closely matched the non-debugging
1606 int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1607 dbg_znode_callback znode_cb, void *priv)
1610 struct ubifs_zbranch *zbr;
1611 struct ubifs_znode *znode, *child;
1613 mutex_lock(&c->tnc_mutex);
1614 /* If the root indexing node is not in TNC - pull it */
1615 if (!c->zroot.znode) {
1616 c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1617 if (IS_ERR(c->zroot.znode)) {
1618 err = PTR_ERR(c->zroot.znode);
1619 c->zroot.znode = NULL;
1625 * We are going to traverse the indexing tree in the postorder manner.
1626 * Go down and find the leftmost indexing node where we are going to
1629 znode = c->zroot.znode;
1630 while (znode->level > 0) {
1631 zbr = &znode->zbranch[0];
1634 child = ubifs_load_znode(c, zbr, znode, 0);
1635 if (IS_ERR(child)) {
1636 err = PTR_ERR(child);
1645 /* Iterate over all indexing nodes */
1652 err = znode_cb(c, znode, priv);
1654 ubifs_err(c, "znode checking function returned error %d",
1656 ubifs_dump_znode(c, znode);
1660 if (leaf_cb && znode->level == 0) {
1661 for (idx = 0; idx < znode->child_cnt; idx++) {
1662 zbr = &znode->zbranch[idx];
1663 err = leaf_cb(c, zbr, priv);
1665 ubifs_err(c, "leaf checking function returned error %d, for leaf at LEB %d:%d",
1666 err, zbr->lnum, zbr->offs);
1675 idx = znode->iip + 1;
1676 znode = znode->parent;
1677 if (idx < znode->child_cnt) {
1678 /* Switch to the next index in the parent */
1679 zbr = &znode->zbranch[idx];
1682 child = ubifs_load_znode(c, zbr, znode, idx);
1683 if (IS_ERR(child)) {
1684 err = PTR_ERR(child);
1692 * This is the last child, switch to the parent and
1697 /* Go to the lowest leftmost znode in the new sub-tree */
1698 while (znode->level > 0) {
1699 zbr = &znode->zbranch[0];
1702 child = ubifs_load_znode(c, zbr, znode, 0);
1703 if (IS_ERR(child)) {
1704 err = PTR_ERR(child);
1713 mutex_unlock(&c->tnc_mutex);
1718 zbr = &znode->parent->zbranch[znode->iip];
1721 ubifs_msg(c, "dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1722 ubifs_dump_znode(c, znode);
1724 mutex_unlock(&c->tnc_mutex);
1729 * add_size - add znode size to partially calculated index size.
1730 * @c: UBIFS file-system description object
1731 * @znode: znode to add size for
1732 * @priv: partially calculated index size
1734 * This is a helper function for 'dbg_check_idx_size()' which is called for
1735 * every indexing node and adds its size to the 'long long' variable pointed to
1738 static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1740 long long *idx_size = priv;
1743 add = ubifs_idx_node_sz(c, znode->child_cnt);
1744 add = ALIGN(add, 8);
1750 * dbg_check_idx_size - check index size.
1751 * @c: UBIFS file-system description object
1752 * @idx_size: size to check
1754 * This function walks the UBIFS index, calculates its size and checks that the
1755 * size is equivalent to @idx_size. Returns zero in case of success and a
1756 * negative error code in case of failure.
1758 int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
1763 if (!dbg_is_chk_index(c))
1766 err = dbg_walk_index(c, NULL, add_size, &calc);
1768 ubifs_err(c, "error %d while walking the index", err);
1772 if (calc != idx_size) {
1773 ubifs_err(c, "index size check failed: calculated size is %lld, should be %lld",
1784 * struct fsck_inode - information about an inode used when checking the file-system.
1785 * @rb: link in the RB-tree of inodes
1786 * @inum: inode number
1787 * @mode: inode type, permissions, etc
1788 * @nlink: inode link count
1789 * @xattr_cnt: count of extended attributes
1790 * @references: how many directory/xattr entries refer this inode (calculated
1791 * while walking the index)
1792 * @calc_cnt: for directory inode count of child directories
1793 * @size: inode size (read from on-flash inode)
1794 * @xattr_sz: summary size of all extended attributes (read from on-flash
1796 * @calc_sz: for directories calculated directory size
1797 * @calc_xcnt: count of extended attributes
1798 * @calc_xsz: calculated summary size of all extended attributes
1799 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1800 * inode (read from on-flash inode)
1801 * @calc_xnms: calculated sum of lengths of all extended attribute names
1808 unsigned int xattr_cnt;
1812 unsigned int xattr_sz;
1814 long long calc_xcnt;
1816 unsigned int xattr_nms;
1817 long long calc_xnms;
1821 * struct fsck_data - private FS checking information.
1822 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1825 struct rb_root inodes;
1829 * add_inode - add inode information to RB-tree of inodes.
1830 * @c: UBIFS file-system description object
1831 * @fsckd: FS checking information
1832 * @ino: raw UBIFS inode to add
1834 * This is a helper function for 'check_leaf()' which adds information about
1835 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1836 * case of success and a negative error code in case of failure.
1838 static struct fsck_inode *add_inode(struct ubifs_info *c,
1839 struct fsck_data *fsckd,
1840 struct ubifs_ino_node *ino)
1842 struct rb_node **p, *parent = NULL;
1843 struct fsck_inode *fscki;
1844 ino_t inum = key_inum_flash(c, &ino->key);
1845 struct inode *inode;
1846 struct ubifs_inode *ui;
1848 p = &fsckd->inodes.rb_node;
1851 fscki = rb_entry(parent, struct fsck_inode, rb);
1852 if (inum < fscki->inum)
1854 else if (inum > fscki->inum)
1855 p = &(*p)->rb_right;
1860 if (inum > c->highest_inum) {
1861 ubifs_err(c, "too high inode number, max. is %lu",
1862 (unsigned long)c->highest_inum);
1863 return ERR_PTR(-EINVAL);
1866 fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
1868 return ERR_PTR(-ENOMEM);
1870 inode = ilookup(c->vfs_sb, inum);
1874 * If the inode is present in the VFS inode cache, use it instead of
1875 * the on-flash inode which might be out-of-date. E.g., the size might
1876 * be out-of-date. If we do not do this, the following may happen, for
1878 * 1. A power cut happens
1879 * 2. We mount the file-system R/O, the replay process fixes up the
1880 * inode size in the VFS cache, but on on-flash.
1881 * 3. 'check_leaf()' fails because it hits a data node beyond inode
1885 fscki->nlink = le32_to_cpu(ino->nlink);
1886 fscki->size = le64_to_cpu(ino->size);
1887 fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
1888 fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
1889 fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
1890 fscki->mode = le32_to_cpu(ino->mode);
1892 ui = ubifs_inode(inode);
1893 fscki->nlink = inode->i_nlink;
1894 fscki->size = inode->i_size;
1895 fscki->xattr_cnt = ui->xattr_cnt;
1896 fscki->xattr_sz = ui->xattr_size;
1897 fscki->xattr_nms = ui->xattr_names;
1898 fscki->mode = inode->i_mode;
1902 if (S_ISDIR(fscki->mode)) {
1903 fscki->calc_sz = UBIFS_INO_NODE_SZ;
1904 fscki->calc_cnt = 2;
1907 rb_link_node(&fscki->rb, parent, p);
1908 rb_insert_color(&fscki->rb, &fsckd->inodes);
1914 * search_inode - search inode in the RB-tree of inodes.
1915 * @fsckd: FS checking information
1916 * @inum: inode number to search
1918 * This is a helper function for 'check_leaf()' which searches inode @inum in
1919 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1920 * the inode was not found.
1922 static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
1925 struct fsck_inode *fscki;
1927 p = fsckd->inodes.rb_node;
1929 fscki = rb_entry(p, struct fsck_inode, rb);
1930 if (inum < fscki->inum)
1932 else if (inum > fscki->inum)
1941 * read_add_inode - read inode node and add it to RB-tree of inodes.
1942 * @c: UBIFS file-system description object
1943 * @fsckd: FS checking information
1944 * @inum: inode number to read
1946 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1947 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1948 * information pointer in case of success and a negative error code in case of
1951 static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1952 struct fsck_data *fsckd, ino_t inum)
1955 union ubifs_key key;
1956 struct ubifs_znode *znode;
1957 struct ubifs_zbranch *zbr;
1958 struct ubifs_ino_node *ino;
1959 struct fsck_inode *fscki;
1961 fscki = search_inode(fsckd, inum);
1965 ino_key_init(c, &key, inum);
1966 err = ubifs_lookup_level0(c, &key, &znode, &n);
1968 ubifs_err(c, "inode %lu not found in index", (unsigned long)inum);
1969 return ERR_PTR(-ENOENT);
1970 } else if (err < 0) {
1971 ubifs_err(c, "error %d while looking up inode %lu",
1972 err, (unsigned long)inum);
1973 return ERR_PTR(err);
1976 zbr = &znode->zbranch[n];
1977 if (zbr->len < UBIFS_INO_NODE_SZ) {
1978 ubifs_err(c, "bad node %lu node length %d",
1979 (unsigned long)inum, zbr->len);
1980 return ERR_PTR(-EINVAL);
1983 ino = kmalloc(zbr->len, GFP_NOFS);
1985 return ERR_PTR(-ENOMEM);
1987 err = ubifs_tnc_read_node(c, zbr, ino);
1989 ubifs_err(c, "cannot read inode node at LEB %d:%d, error %d",
1990 zbr->lnum, zbr->offs, err);
1992 return ERR_PTR(err);
1995 fscki = add_inode(c, fsckd, ino);
1997 if (IS_ERR(fscki)) {
1998 ubifs_err(c, "error %ld while adding inode %lu node",
1999 PTR_ERR(fscki), (unsigned long)inum);
2007 * check_leaf - check leaf node.
2008 * @c: UBIFS file-system description object
2009 * @zbr: zbranch of the leaf node to check
2010 * @priv: FS checking information
2012 * This is a helper function for 'dbg_check_filesystem()' which is called for
2013 * every single leaf node while walking the indexing tree. It checks that the
2014 * leaf node referred from the indexing tree exists, has correct CRC, and does
2015 * some other basic validation. This function is also responsible for building
2016 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
2017 * calculates reference count, size, etc for each inode in order to later
2018 * compare them to the information stored inside the inodes and detect possible
2019 * inconsistencies. Returns zero in case of success and a negative error code
2020 * in case of failure.
2022 static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
2027 struct ubifs_ch *ch;
2028 int err, type = key_type(c, &zbr->key);
2029 struct fsck_inode *fscki;
2031 if (zbr->len < UBIFS_CH_SZ) {
2032 ubifs_err(c, "bad leaf length %d (LEB %d:%d)",
2033 zbr->len, zbr->lnum, zbr->offs);
2037 node = kmalloc(zbr->len, GFP_NOFS);
2041 err = ubifs_tnc_read_node(c, zbr, node);
2043 ubifs_err(c, "cannot read leaf node at LEB %d:%d, error %d",
2044 zbr->lnum, zbr->offs, err);
2048 /* If this is an inode node, add it to RB-tree of inodes */
2049 if (type == UBIFS_INO_KEY) {
2050 fscki = add_inode(c, priv, node);
2051 if (IS_ERR(fscki)) {
2052 err = PTR_ERR(fscki);
2053 ubifs_err(c, "error %d while adding inode node", err);
2059 if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
2060 type != UBIFS_DATA_KEY) {
2061 ubifs_err(c, "unexpected node type %d at LEB %d:%d",
2062 type, zbr->lnum, zbr->offs);
2068 if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
2069 ubifs_err(c, "too high sequence number, max. is %llu",
2075 if (type == UBIFS_DATA_KEY) {
2077 struct ubifs_data_node *dn = node;
2079 ubifs_assert(zbr->len >= UBIFS_DATA_NODE_SZ);
2082 * Search the inode node this data node belongs to and insert
2083 * it to the RB-tree of inodes.
2085 inum = key_inum_flash(c, &dn->key);
2086 fscki = read_add_inode(c, priv, inum);
2087 if (IS_ERR(fscki)) {
2088 err = PTR_ERR(fscki);
2089 ubifs_err(c, "error %d while processing data node and trying to find inode node %lu",
2090 err, (unsigned long)inum);
2094 /* Make sure the data node is within inode size */
2095 blk_offs = key_block_flash(c, &dn->key);
2096 blk_offs <<= UBIFS_BLOCK_SHIFT;
2097 blk_offs += le32_to_cpu(dn->size);
2098 if (blk_offs > fscki->size) {
2099 ubifs_err(c, "data node at LEB %d:%d is not within inode size %lld",
2100 zbr->lnum, zbr->offs, fscki->size);
2106 struct ubifs_dent_node *dent = node;
2107 struct fsck_inode *fscki1;
2109 ubifs_assert(zbr->len >= UBIFS_DENT_NODE_SZ);
2111 err = ubifs_validate_entry(c, dent);
2116 * Search the inode node this entry refers to and the parent
2117 * inode node and insert them to the RB-tree of inodes.
2119 inum = le64_to_cpu(dent->inum);
2120 fscki = read_add_inode(c, priv, inum);
2121 if (IS_ERR(fscki)) {
2122 err = PTR_ERR(fscki);
2123 ubifs_err(c, "error %d while processing entry node and trying to find inode node %lu",
2124 err, (unsigned long)inum);
2128 /* Count how many direntries or xentries refers this inode */
2129 fscki->references += 1;
2131 inum = key_inum_flash(c, &dent->key);
2132 fscki1 = read_add_inode(c, priv, inum);
2133 if (IS_ERR(fscki1)) {
2134 err = PTR_ERR(fscki1);
2135 ubifs_err(c, "error %d while processing entry node and trying to find parent inode node %lu",
2136 err, (unsigned long)inum);
2140 nlen = le16_to_cpu(dent->nlen);
2141 if (type == UBIFS_XENT_KEY) {
2142 fscki1->calc_xcnt += 1;
2143 fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
2144 fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
2145 fscki1->calc_xnms += nlen;
2147 fscki1->calc_sz += CALC_DENT_SIZE(nlen);
2148 if (dent->type == UBIFS_ITYPE_DIR)
2149 fscki1->calc_cnt += 1;
2158 ubifs_msg(c, "dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
2159 ubifs_dump_node(c, node);
2166 * free_inodes - free RB-tree of inodes.
2167 * @fsckd: FS checking information
2169 static void free_inodes(struct fsck_data *fsckd)
2171 struct fsck_inode *fscki, *n;
2173 rbtree_postorder_for_each_entry_safe(fscki, n, &fsckd->inodes, rb)
2178 * check_inodes - checks all inodes.
2179 * @c: UBIFS file-system description object
2180 * @fsckd: FS checking information
2182 * This is a helper function for 'dbg_check_filesystem()' which walks the
2183 * RB-tree of inodes after the index scan has been finished, and checks that
2184 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
2185 * %-EINVAL if not, and a negative error code in case of failure.
2187 static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
2190 union ubifs_key key;
2191 struct ubifs_znode *znode;
2192 struct ubifs_zbranch *zbr;
2193 struct ubifs_ino_node *ino;
2194 struct fsck_inode *fscki;
2195 struct rb_node *this = rb_first(&fsckd->inodes);
2198 fscki = rb_entry(this, struct fsck_inode, rb);
2199 this = rb_next(this);
2201 if (S_ISDIR(fscki->mode)) {
2203 * Directories have to have exactly one reference (they
2204 * cannot have hardlinks), although root inode is an
2207 if (fscki->inum != UBIFS_ROOT_INO &&
2208 fscki->references != 1) {
2209 ubifs_err(c, "directory inode %lu has %d direntries which refer it, but should be 1",
2210 (unsigned long)fscki->inum,
2214 if (fscki->inum == UBIFS_ROOT_INO &&
2215 fscki->references != 0) {
2216 ubifs_err(c, "root inode %lu has non-zero (%d) direntries which refer it",
2217 (unsigned long)fscki->inum,
2221 if (fscki->calc_sz != fscki->size) {
2222 ubifs_err(c, "directory inode %lu size is %lld, but calculated size is %lld",
2223 (unsigned long)fscki->inum,
2224 fscki->size, fscki->calc_sz);
2227 if (fscki->calc_cnt != fscki->nlink) {
2228 ubifs_err(c, "directory inode %lu nlink is %d, but calculated nlink is %d",
2229 (unsigned long)fscki->inum,
2230 fscki->nlink, fscki->calc_cnt);
2234 if (fscki->references != fscki->nlink) {
2235 ubifs_err(c, "inode %lu nlink is %d, but calculated nlink is %d",
2236 (unsigned long)fscki->inum,
2237 fscki->nlink, fscki->references);
2241 if (fscki->xattr_sz != fscki->calc_xsz) {
2242 ubifs_err(c, "inode %lu has xattr size %u, but calculated size is %lld",
2243 (unsigned long)fscki->inum, fscki->xattr_sz,
2247 if (fscki->xattr_cnt != fscki->calc_xcnt) {
2248 ubifs_err(c, "inode %lu has %u xattrs, but calculated count is %lld",
2249 (unsigned long)fscki->inum,
2250 fscki->xattr_cnt, fscki->calc_xcnt);
2253 if (fscki->xattr_nms != fscki->calc_xnms) {
2254 ubifs_err(c, "inode %lu has xattr names' size %u, but calculated names' size is %lld",
2255 (unsigned long)fscki->inum, fscki->xattr_nms,
2264 /* Read the bad inode and dump it */
2265 ino_key_init(c, &key, fscki->inum);
2266 err = ubifs_lookup_level0(c, &key, &znode, &n);
2268 ubifs_err(c, "inode %lu not found in index",
2269 (unsigned long)fscki->inum);
2271 } else if (err < 0) {
2272 ubifs_err(c, "error %d while looking up inode %lu",
2273 err, (unsigned long)fscki->inum);
2277 zbr = &znode->zbranch[n];
2278 ino = kmalloc(zbr->len, GFP_NOFS);
2282 err = ubifs_tnc_read_node(c, zbr, ino);
2284 ubifs_err(c, "cannot read inode node at LEB %d:%d, error %d",
2285 zbr->lnum, zbr->offs, err);
2290 ubifs_msg(c, "dump of the inode %lu sitting in LEB %d:%d",
2291 (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
2292 ubifs_dump_node(c, ino);
2298 * dbg_check_filesystem - check the file-system.
2299 * @c: UBIFS file-system description object
2301 * This function checks the file system, namely:
2302 * o makes sure that all leaf nodes exist and their CRCs are correct;
2303 * o makes sure inode nlink, size, xattr size/count are correct (for all
2306 * The function reads whole indexing tree and all nodes, so it is pretty
2307 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2308 * not, and a negative error code in case of failure.
2310 int dbg_check_filesystem(struct ubifs_info *c)
2313 struct fsck_data fsckd;
2315 if (!dbg_is_chk_fs(c))
2318 fsckd.inodes = RB_ROOT;
2319 err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
2323 err = check_inodes(c, &fsckd);
2327 free_inodes(&fsckd);
2331 ubifs_err(c, "file-system check failed with error %d", err);
2333 free_inodes(&fsckd);
2338 * dbg_check_data_nodes_order - check that list of data nodes is sorted.
2339 * @c: UBIFS file-system description object
2340 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2342 * This function returns zero if the list of data nodes is sorted correctly,
2343 * and %-EINVAL if not.
2345 int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head)
2347 struct list_head *cur;
2348 struct ubifs_scan_node *sa, *sb;
2350 if (!dbg_is_chk_gen(c))
2353 for (cur = head->next; cur->next != head; cur = cur->next) {
2355 uint32_t blka, blkb;
2358 sa = container_of(cur, struct ubifs_scan_node, list);
2359 sb = container_of(cur->next, struct ubifs_scan_node, list);
2361 if (sa->type != UBIFS_DATA_NODE) {
2362 ubifs_err(c, "bad node type %d", sa->type);
2363 ubifs_dump_node(c, sa->node);
2366 if (sb->type != UBIFS_DATA_NODE) {
2367 ubifs_err(c, "bad node type %d", sb->type);
2368 ubifs_dump_node(c, sb->node);
2372 inuma = key_inum(c, &sa->key);
2373 inumb = key_inum(c, &sb->key);
2377 if (inuma > inumb) {
2378 ubifs_err(c, "larger inum %lu goes before inum %lu",
2379 (unsigned long)inuma, (unsigned long)inumb);
2383 blka = key_block(c, &sa->key);
2384 blkb = key_block(c, &sb->key);
2387 ubifs_err(c, "larger block %u goes before %u", blka, blkb);
2391 ubifs_err(c, "two data nodes for the same block");
2399 ubifs_dump_node(c, sa->node);
2400 ubifs_dump_node(c, sb->node);
2405 * dbg_check_nondata_nodes_order - check that list of data nodes is sorted.
2406 * @c: UBIFS file-system description object
2407 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2409 * This function returns zero if the list of non-data nodes is sorted correctly,
2410 * and %-EINVAL if not.
2412 int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head)
2414 struct list_head *cur;
2415 struct ubifs_scan_node *sa, *sb;
2417 if (!dbg_is_chk_gen(c))
2420 for (cur = head->next; cur->next != head; cur = cur->next) {
2422 uint32_t hasha, hashb;
2425 sa = container_of(cur, struct ubifs_scan_node, list);
2426 sb = container_of(cur->next, struct ubifs_scan_node, list);
2428 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2429 sa->type != UBIFS_XENT_NODE) {
2430 ubifs_err(c, "bad node type %d", sa->type);
2431 ubifs_dump_node(c, sa->node);
2434 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2435 sa->type != UBIFS_XENT_NODE) {
2436 ubifs_err(c, "bad node type %d", sb->type);
2437 ubifs_dump_node(c, sb->node);
2441 if (sa->type != UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2442 ubifs_err(c, "non-inode node goes before inode node");
2446 if (sa->type == UBIFS_INO_NODE && sb->type != UBIFS_INO_NODE)
2449 if (sa->type == UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2450 /* Inode nodes are sorted in descending size order */
2451 if (sa->len < sb->len) {
2452 ubifs_err(c, "smaller inode node goes first");
2459 * This is either a dentry or xentry, which should be sorted in
2460 * ascending (parent ino, hash) order.
2462 inuma = key_inum(c, &sa->key);
2463 inumb = key_inum(c, &sb->key);
2467 if (inuma > inumb) {
2468 ubifs_err(c, "larger inum %lu goes before inum %lu",
2469 (unsigned long)inuma, (unsigned long)inumb);
2473 hasha = key_block(c, &sa->key);
2474 hashb = key_block(c, &sb->key);
2476 if (hasha > hashb) {
2477 ubifs_err(c, "larger hash %u goes before %u",
2486 ubifs_msg(c, "dumping first node");
2487 ubifs_dump_node(c, sa->node);
2488 ubifs_msg(c, "dumping second node");
2489 ubifs_dump_node(c, sb->node);
2494 static inline int chance(unsigned int n, unsigned int out_of)
2496 return !!((prandom_u32() % out_of) + 1 <= n);
2500 static int power_cut_emulated(struct ubifs_info *c, int lnum, int write)
2502 struct ubifs_debug_info *d = c->dbg;
2504 ubifs_assert(dbg_is_tst_rcvry(c));
2507 /* First call - decide delay to the power cut */
2509 unsigned long delay;
2513 /* Fail within 1 minute */
2514 delay = prandom_u32() % 60000;
2515 d->pc_timeout = jiffies;
2516 d->pc_timeout += msecs_to_jiffies(delay);
2517 ubifs_warn(c, "failing after %lums", delay);
2520 delay = prandom_u32() % 10000;
2521 /* Fail within 10000 operations */
2522 d->pc_cnt_max = delay;
2523 ubifs_warn(c, "failing after %lu calls", delay);
2530 /* Determine if failure delay has expired */
2531 if (d->pc_delay == 1 && time_before(jiffies, d->pc_timeout))
2533 if (d->pc_delay == 2 && d->pc_cnt++ < d->pc_cnt_max)
2536 if (lnum == UBIFS_SB_LNUM) {
2537 if (write && chance(1, 2))
2541 ubifs_warn(c, "failing in super block LEB %d", lnum);
2542 } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2545 ubifs_warn(c, "failing in master LEB %d", lnum);
2546 } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2547 if (write && chance(99, 100))
2549 if (chance(399, 400))
2551 ubifs_warn(c, "failing in log LEB %d", lnum);
2552 } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2553 if (write && chance(7, 8))
2557 ubifs_warn(c, "failing in LPT LEB %d", lnum);
2558 } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2559 if (write && chance(1, 2))
2563 ubifs_warn(c, "failing in orphan LEB %d", lnum);
2564 } else if (lnum == c->ihead_lnum) {
2565 if (chance(99, 100))
2567 ubifs_warn(c, "failing in index head LEB %d", lnum);
2568 } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2571 ubifs_warn(c, "failing in GC head LEB %d", lnum);
2572 } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
2573 !ubifs_search_bud(c, lnum)) {
2576 ubifs_warn(c, "failing in non-bud LEB %d", lnum);
2577 } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
2578 c->cmt_state == COMMIT_RUNNING_REQUIRED) {
2579 if (chance(999, 1000))
2581 ubifs_warn(c, "failing in bud LEB %d commit running", lnum);
2583 if (chance(9999, 10000))
2585 ubifs_warn(c, "failing in bud LEB %d commit not running", lnum);
2589 ubifs_warn(c, "========== Power cut emulated ==========");
2594 static int corrupt_data(const struct ubifs_info *c, const void *buf,
2597 unsigned int from, to, ffs = chance(1, 2);
2598 unsigned char *p = (void *)buf;
2600 from = prandom_u32() % len;
2601 /* Corruption span max to end of write unit */
2602 to = min(len, ALIGN(from + 1, c->max_write_size));
2604 ubifs_warn(c, "filled bytes %u-%u with %s", from, to - 1,
2605 ffs ? "0xFFs" : "random data");
2608 memset(p + from, 0xFF, to - from);
2610 prandom_bytes(p + from, to - from);
2615 int dbg_leb_write(struct ubifs_info *c, int lnum, const void *buf,
2620 if (c->dbg->pc_happened)
2623 failing = power_cut_emulated(c, lnum, 1);
2625 len = corrupt_data(c, buf, len);
2626 ubifs_warn(c, "actually write %d bytes to LEB %d:%d (the buffer was corrupted)",
2629 err = ubi_leb_write(c->ubi, lnum, buf, offs, len);
2637 int dbg_leb_change(struct ubifs_info *c, int lnum, const void *buf,
2642 if (c->dbg->pc_happened)
2644 if (power_cut_emulated(c, lnum, 1))
2646 err = ubi_leb_change(c->ubi, lnum, buf, len);
2649 if (power_cut_emulated(c, lnum, 1))
2654 int dbg_leb_unmap(struct ubifs_info *c, int lnum)
2658 if (c->dbg->pc_happened)
2660 if (power_cut_emulated(c, lnum, 0))
2662 err = ubi_leb_unmap(c->ubi, lnum);
2665 if (power_cut_emulated(c, lnum, 0))
2670 int dbg_leb_map(struct ubifs_info *c, int lnum)
2674 if (c->dbg->pc_happened)
2676 if (power_cut_emulated(c, lnum, 0))
2678 err = ubi_leb_map(c->ubi, lnum);
2681 if (power_cut_emulated(c, lnum, 0))
2687 * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
2688 * contain the stuff specific to particular file-system mounts.
2690 static struct dentry *dfs_rootdir;
2692 static int dfs_file_open(struct inode *inode, struct file *file)
2694 file->private_data = inode->i_private;
2695 return nonseekable_open(inode, file);
2699 * provide_user_output - provide output to the user reading a debugfs file.
2700 * @val: boolean value for the answer
2701 * @u: the buffer to store the answer at
2702 * @count: size of the buffer
2703 * @ppos: position in the @u output buffer
2705 * This is a simple helper function which stores @val boolean value in the user
2706 * buffer when the user reads one of UBIFS debugfs files. Returns amount of
2707 * bytes written to @u in case of success and a negative error code in case of
2710 static int provide_user_output(int val, char __user *u, size_t count,
2722 return simple_read_from_buffer(u, count, ppos, buf, 2);
2725 static ssize_t dfs_file_read(struct file *file, char __user *u, size_t count,
2728 struct dentry *dent = file->f_path.dentry;
2729 struct ubifs_info *c = file->private_data;
2730 struct ubifs_debug_info *d = c->dbg;
2733 if (dent == d->dfs_chk_gen)
2735 else if (dent == d->dfs_chk_index)
2737 else if (dent == d->dfs_chk_orph)
2739 else if (dent == d->dfs_chk_lprops)
2740 val = d->chk_lprops;
2741 else if (dent == d->dfs_chk_fs)
2743 else if (dent == d->dfs_tst_rcvry)
2745 else if (dent == d->dfs_ro_error)
2750 return provide_user_output(val, u, count, ppos);
2754 * interpret_user_input - interpret user debugfs file input.
2755 * @u: user-provided buffer with the input
2756 * @count: buffer size
2758 * This is a helper function which interpret user input to a boolean UBIFS
2759 * debugfs file. Returns %0 or %1 in case of success and a negative error code
2760 * in case of failure.
2762 static int interpret_user_input(const char __user *u, size_t count)
2767 buf_size = min_t(size_t, count, (sizeof(buf) - 1));
2768 if (copy_from_user(buf, u, buf_size))
2773 else if (buf[0] == '0')
2779 static ssize_t dfs_file_write(struct file *file, const char __user *u,
2780 size_t count, loff_t *ppos)
2782 struct ubifs_info *c = file->private_data;
2783 struct ubifs_debug_info *d = c->dbg;
2784 struct dentry *dent = file->f_path.dentry;
2788 * TODO: this is racy - the file-system might have already been
2789 * unmounted and we'd oops in this case. The plan is to fix it with
2790 * help of 'iterate_supers_type()' which we should have in v3.0: when
2791 * a debugfs opened, we rember FS's UUID in file->private_data. Then
2792 * whenever we access the FS via a debugfs file, we iterate all UBIFS
2793 * superblocks and fine the one with the same UUID, and take the
2796 * The other way to go suggested by Al Viro is to create a separate
2797 * 'ubifs-debug' file-system instead.
2799 if (file->f_path.dentry == d->dfs_dump_lprops) {
2800 ubifs_dump_lprops(c);
2803 if (file->f_path.dentry == d->dfs_dump_budg) {
2804 ubifs_dump_budg(c, &c->bi);
2807 if (file->f_path.dentry == d->dfs_dump_tnc) {
2808 mutex_lock(&c->tnc_mutex);
2810 mutex_unlock(&c->tnc_mutex);
2814 val = interpret_user_input(u, count);
2818 if (dent == d->dfs_chk_gen)
2820 else if (dent == d->dfs_chk_index)
2822 else if (dent == d->dfs_chk_orph)
2824 else if (dent == d->dfs_chk_lprops)
2825 d->chk_lprops = val;
2826 else if (dent == d->dfs_chk_fs)
2828 else if (dent == d->dfs_tst_rcvry)
2830 else if (dent == d->dfs_ro_error)
2831 c->ro_error = !!val;
2838 static const struct file_operations dfs_fops = {
2839 .open = dfs_file_open,
2840 .read = dfs_file_read,
2841 .write = dfs_file_write,
2842 .owner = THIS_MODULE,
2843 .llseek = no_llseek,
2847 * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
2848 * @c: UBIFS file-system description object
2850 * This function creates all debugfs files for this instance of UBIFS. Returns
2851 * zero in case of success and a negative error code in case of failure.
2853 * Note, the only reason we have not merged this function with the
2854 * 'ubifs_debugging_init()' function is because it is better to initialize
2855 * debugfs interfaces at the very end of the mount process, and remove them at
2856 * the very beginning of the mount process.
2858 int dbg_debugfs_init_fs(struct ubifs_info *c)
2862 struct dentry *dent;
2863 struct ubifs_debug_info *d = c->dbg;
2865 if (!IS_ENABLED(CONFIG_DEBUG_FS))
2868 n = snprintf(d->dfs_dir_name, UBIFS_DFS_DIR_LEN + 1, UBIFS_DFS_DIR_NAME,
2869 c->vi.ubi_num, c->vi.vol_id);
2870 if (n == UBIFS_DFS_DIR_LEN) {
2871 /* The array size is too small */
2872 fname = UBIFS_DFS_DIR_NAME;
2873 dent = ERR_PTR(-EINVAL);
2877 fname = d->dfs_dir_name;
2878 dent = debugfs_create_dir(fname, dfs_rootdir);
2879 if (IS_ERR_OR_NULL(dent))
2883 fname = "dump_lprops";
2884 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2885 if (IS_ERR_OR_NULL(dent))
2887 d->dfs_dump_lprops = dent;
2889 fname = "dump_budg";
2890 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2891 if (IS_ERR_OR_NULL(dent))
2893 d->dfs_dump_budg = dent;
2896 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2897 if (IS_ERR_OR_NULL(dent))
2899 d->dfs_dump_tnc = dent;
2901 fname = "chk_general";
2902 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2904 if (IS_ERR_OR_NULL(dent))
2906 d->dfs_chk_gen = dent;
2908 fname = "chk_index";
2909 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2911 if (IS_ERR_OR_NULL(dent))
2913 d->dfs_chk_index = dent;
2915 fname = "chk_orphans";
2916 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2918 if (IS_ERR_OR_NULL(dent))
2920 d->dfs_chk_orph = dent;
2922 fname = "chk_lprops";
2923 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2925 if (IS_ERR_OR_NULL(dent))
2927 d->dfs_chk_lprops = dent;
2930 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2932 if (IS_ERR_OR_NULL(dent))
2934 d->dfs_chk_fs = dent;
2936 fname = "tst_recovery";
2937 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2939 if (IS_ERR_OR_NULL(dent))
2941 d->dfs_tst_rcvry = dent;
2944 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2946 if (IS_ERR_OR_NULL(dent))
2948 d->dfs_ro_error = dent;
2953 debugfs_remove_recursive(d->dfs_dir);
2955 err = dent ? PTR_ERR(dent) : -ENODEV;
2956 ubifs_err(c, "cannot create \"%s\" debugfs file or directory, error %d\n",
2962 * dbg_debugfs_exit_fs - remove all debugfs files.
2963 * @c: UBIFS file-system description object
2965 void dbg_debugfs_exit_fs(struct ubifs_info *c)
2967 if (IS_ENABLED(CONFIG_DEBUG_FS))
2968 debugfs_remove_recursive(c->dbg->dfs_dir);
2971 struct ubifs_global_debug_info ubifs_dbg;
2973 static struct dentry *dfs_chk_gen;
2974 static struct dentry *dfs_chk_index;
2975 static struct dentry *dfs_chk_orph;
2976 static struct dentry *dfs_chk_lprops;
2977 static struct dentry *dfs_chk_fs;
2978 static struct dentry *dfs_tst_rcvry;
2980 static ssize_t dfs_global_file_read(struct file *file, char __user *u,
2981 size_t count, loff_t *ppos)
2983 struct dentry *dent = file->f_path.dentry;
2986 if (dent == dfs_chk_gen)
2987 val = ubifs_dbg.chk_gen;
2988 else if (dent == dfs_chk_index)
2989 val = ubifs_dbg.chk_index;
2990 else if (dent == dfs_chk_orph)
2991 val = ubifs_dbg.chk_orph;
2992 else if (dent == dfs_chk_lprops)
2993 val = ubifs_dbg.chk_lprops;
2994 else if (dent == dfs_chk_fs)
2995 val = ubifs_dbg.chk_fs;
2996 else if (dent == dfs_tst_rcvry)
2997 val = ubifs_dbg.tst_rcvry;
3001 return provide_user_output(val, u, count, ppos);
3004 static ssize_t dfs_global_file_write(struct file *file, const char __user *u,
3005 size_t count, loff_t *ppos)
3007 struct dentry *dent = file->f_path.dentry;
3010 val = interpret_user_input(u, count);
3014 if (dent == dfs_chk_gen)
3015 ubifs_dbg.chk_gen = val;
3016 else if (dent == dfs_chk_index)
3017 ubifs_dbg.chk_index = val;
3018 else if (dent == dfs_chk_orph)
3019 ubifs_dbg.chk_orph = val;
3020 else if (dent == dfs_chk_lprops)
3021 ubifs_dbg.chk_lprops = val;
3022 else if (dent == dfs_chk_fs)
3023 ubifs_dbg.chk_fs = val;
3024 else if (dent == dfs_tst_rcvry)
3025 ubifs_dbg.tst_rcvry = val;
3032 static const struct file_operations dfs_global_fops = {
3033 .read = dfs_global_file_read,
3034 .write = dfs_global_file_write,
3035 .owner = THIS_MODULE,
3036 .llseek = no_llseek,
3040 * dbg_debugfs_init - initialize debugfs file-system.
3042 * UBIFS uses debugfs file-system to expose various debugging knobs to
3043 * user-space. This function creates "ubifs" directory in the debugfs
3044 * file-system. Returns zero in case of success and a negative error code in
3047 int dbg_debugfs_init(void)
3051 struct dentry *dent;
3053 if (!IS_ENABLED(CONFIG_DEBUG_FS))
3057 dent = debugfs_create_dir(fname, NULL);
3058 if (IS_ERR_OR_NULL(dent))
3062 fname = "chk_general";
3063 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3065 if (IS_ERR_OR_NULL(dent))
3069 fname = "chk_index";
3070 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3072 if (IS_ERR_OR_NULL(dent))
3074 dfs_chk_index = dent;
3076 fname = "chk_orphans";
3077 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3079 if (IS_ERR_OR_NULL(dent))
3081 dfs_chk_orph = dent;
3083 fname = "chk_lprops";
3084 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3086 if (IS_ERR_OR_NULL(dent))
3088 dfs_chk_lprops = dent;
3091 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3093 if (IS_ERR_OR_NULL(dent))
3097 fname = "tst_recovery";
3098 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3100 if (IS_ERR_OR_NULL(dent))
3102 dfs_tst_rcvry = dent;
3107 debugfs_remove_recursive(dfs_rootdir);
3109 err = dent ? PTR_ERR(dent) : -ENODEV;
3110 pr_err("UBIFS error (pid %d): cannot create \"%s\" debugfs file or directory, error %d\n",
3111 current->pid, fname, err);
3116 * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
3118 void dbg_debugfs_exit(void)
3120 if (IS_ENABLED(CONFIG_DEBUG_FS))
3121 debugfs_remove_recursive(dfs_rootdir);
3125 * ubifs_debugging_init - initialize UBIFS debugging.
3126 * @c: UBIFS file-system description object
3128 * This function initializes debugging-related data for the file system.
3129 * Returns zero in case of success and a negative error code in case of
3132 int ubifs_debugging_init(struct ubifs_info *c)
3134 c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
3142 * ubifs_debugging_exit - free debugging data.
3143 * @c: UBIFS file-system description object
3145 void ubifs_debugging_exit(struct ubifs_info *c)