2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation
6 * SPDX-License-Identifier: GPL-2.0+
8 * Authors: Artem Bityutskiy (Битюцкий Артём)
13 * This file implements most of the debugging stuff which is compiled in only
14 * when it is enabled. But some debugging check functions are implemented in
15 * corresponding subsystem, just because they are closely related and utilize
16 * various local functions of those subsystems.
20 #include <linux/module.h>
21 #include <linux/debugfs.h>
22 #include <linux/math64.h>
23 #include <linux/uaccess.h>
24 #include <linux/random.h>
26 #include <linux/compat.h>
27 #include <linux/err.h>
32 static DEFINE_SPINLOCK(dbg_lock);
35 static const char *get_key_fmt(int fmt)
38 case UBIFS_SIMPLE_KEY_FMT:
41 return "unknown/invalid format";
45 static const char *get_key_hash(int hash)
48 case UBIFS_KEY_HASH_R5:
50 case UBIFS_KEY_HASH_TEST:
53 return "unknown/invalid name hash";
57 static const char *get_key_type(int type)
71 return "unknown/invalid key";
76 static const char *get_dent_type(int type)
89 case UBIFS_ITYPE_FIFO:
91 case UBIFS_ITYPE_SOCK:
94 return "unknown/invalid type";
99 const char *dbg_snprintf_key(const struct ubifs_info *c,
100 const union ubifs_key *key, char *buffer, int len)
103 int type = key_type(c, key);
105 if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
108 len -= snprintf(p, len, "(%lu, %s)",
109 (unsigned long)key_inum(c, key),
114 len -= snprintf(p, len, "(%lu, %s, %#08x)",
115 (unsigned long)key_inum(c, key),
116 get_key_type(type), key_hash(c, key));
119 len -= snprintf(p, len, "(%lu, %s, %u)",
120 (unsigned long)key_inum(c, key),
121 get_key_type(type), key_block(c, key));
124 len -= snprintf(p, len, "(%lu, %s)",
125 (unsigned long)key_inum(c, key),
129 len -= snprintf(p, len, "(bad key type: %#08x, %#08x)",
130 key->u32[0], key->u32[1]);
133 len -= snprintf(p, len, "bad key format %d", c->key_fmt);
134 ubifs_assert(len > 0);
138 const char *dbg_ntype(int type)
142 return "padding node";
144 return "superblock node";
146 return "master node";
148 return "reference node";
151 case UBIFS_DENT_NODE:
152 return "direntry node";
153 case UBIFS_XENT_NODE:
154 return "xentry node";
155 case UBIFS_DATA_NODE:
157 case UBIFS_TRUN_NODE:
158 return "truncate node";
160 return "indexing node";
162 return "commit start node";
163 case UBIFS_ORPH_NODE:
164 return "orphan node";
166 return "unknown node";
170 static const char *dbg_gtype(int type)
173 case UBIFS_NO_NODE_GROUP:
174 return "no node group";
175 case UBIFS_IN_NODE_GROUP:
176 return "in node group";
177 case UBIFS_LAST_OF_NODE_GROUP:
178 return "last of node group";
184 const char *dbg_cstate(int cmt_state)
188 return "commit resting";
189 case COMMIT_BACKGROUND:
190 return "background commit requested";
191 case COMMIT_REQUIRED:
192 return "commit required";
193 case COMMIT_RUNNING_BACKGROUND:
194 return "BACKGROUND commit running";
195 case COMMIT_RUNNING_REQUIRED:
196 return "commit running and required";
198 return "broken commit";
200 return "unknown commit state";
204 const char *dbg_jhead(int jhead)
214 return "unknown journal head";
218 static void dump_ch(const struct ubifs_ch *ch)
220 pr_err("\tmagic %#x\n", le32_to_cpu(ch->magic));
221 pr_err("\tcrc %#x\n", le32_to_cpu(ch->crc));
222 pr_err("\tnode_type %d (%s)\n", ch->node_type,
223 dbg_ntype(ch->node_type));
224 pr_err("\tgroup_type %d (%s)\n", ch->group_type,
225 dbg_gtype(ch->group_type));
226 pr_err("\tsqnum %llu\n",
227 (unsigned long long)le64_to_cpu(ch->sqnum));
228 pr_err("\tlen %u\n", le32_to_cpu(ch->len));
231 void ubifs_dump_inode(struct ubifs_info *c, const struct inode *inode)
234 const struct ubifs_inode *ui = ubifs_inode(inode);
235 struct qstr nm = { .name = NULL };
237 struct ubifs_dent_node *dent, *pdent = NULL;
240 pr_err("Dump in-memory inode:");
241 pr_err("\tinode %lu\n", inode->i_ino);
242 pr_err("\tsize %llu\n",
243 (unsigned long long)i_size_read(inode));
244 pr_err("\tnlink %u\n", inode->i_nlink);
245 pr_err("\tuid %u\n", (unsigned int)i_uid_read(inode));
246 pr_err("\tgid %u\n", (unsigned int)i_gid_read(inode));
247 pr_err("\tatime %u.%u\n",
248 (unsigned int)inode->i_atime.tv_sec,
249 (unsigned int)inode->i_atime.tv_nsec);
250 pr_err("\tmtime %u.%u\n",
251 (unsigned int)inode->i_mtime.tv_sec,
252 (unsigned int)inode->i_mtime.tv_nsec);
253 pr_err("\tctime %u.%u\n",
254 (unsigned int)inode->i_ctime.tv_sec,
255 (unsigned int)inode->i_ctime.tv_nsec);
256 pr_err("\tcreat_sqnum %llu\n", ui->creat_sqnum);
257 pr_err("\txattr_size %u\n", ui->xattr_size);
258 pr_err("\txattr_cnt %u\n", ui->xattr_cnt);
259 pr_err("\txattr_names %u\n", ui->xattr_names);
260 pr_err("\tdirty %u\n", ui->dirty);
261 pr_err("\txattr %u\n", ui->xattr);
262 pr_err("\tbulk_read %u\n", ui->xattr);
263 pr_err("\tsynced_i_size %llu\n",
264 (unsigned long long)ui->synced_i_size);
265 pr_err("\tui_size %llu\n",
266 (unsigned long long)ui->ui_size);
267 pr_err("\tflags %d\n", ui->flags);
268 pr_err("\tcompr_type %d\n", ui->compr_type);
269 pr_err("\tlast_page_read %lu\n", ui->last_page_read);
270 pr_err("\tread_in_a_row %lu\n", ui->read_in_a_row);
271 pr_err("\tdata_len %d\n", ui->data_len);
273 if (!S_ISDIR(inode->i_mode))
276 pr_err("List of directory entries:\n");
277 ubifs_assert(!mutex_is_locked(&c->tnc_mutex));
279 lowest_dent_key(c, &key, inode->i_ino);
281 dent = ubifs_tnc_next_ent(c, &key, &nm);
283 if (PTR_ERR(dent) != -ENOENT)
284 pr_err("error %ld\n", PTR_ERR(dent));
288 pr_err("\t%d: %s (%s)\n",
289 count++, dent->name, get_dent_type(dent->type));
291 nm.name = dent->name;
292 nm.len = le16_to_cpu(dent->nlen);
295 key_read(c, &dent->key, &key);
301 void ubifs_dump_node(const struct ubifs_info *c, const void *node)
305 const struct ubifs_ch *ch = node;
306 char key_buf[DBG_KEY_BUF_LEN];
308 /* If the magic is incorrect, just hexdump the first bytes */
309 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
310 pr_err("Not a node, first %zu bytes:", UBIFS_CH_SZ);
311 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 32, 1,
312 (void *)node, UBIFS_CH_SZ, 1);
316 spin_lock(&dbg_lock);
319 switch (ch->node_type) {
322 const struct ubifs_pad_node *pad = node;
324 pr_err("\tpad_len %u\n", le32_to_cpu(pad->pad_len));
329 const struct ubifs_sb_node *sup = node;
330 unsigned int sup_flags = le32_to_cpu(sup->flags);
332 pr_err("\tkey_hash %d (%s)\n",
333 (int)sup->key_hash, get_key_hash(sup->key_hash));
334 pr_err("\tkey_fmt %d (%s)\n",
335 (int)sup->key_fmt, get_key_fmt(sup->key_fmt));
336 pr_err("\tflags %#x\n", sup_flags);
337 pr_err("\t big_lpt %u\n",
338 !!(sup_flags & UBIFS_FLG_BIGLPT));
339 pr_err("\t space_fixup %u\n",
340 !!(sup_flags & UBIFS_FLG_SPACE_FIXUP));
341 pr_err("\tmin_io_size %u\n", le32_to_cpu(sup->min_io_size));
342 pr_err("\tleb_size %u\n", le32_to_cpu(sup->leb_size));
343 pr_err("\tleb_cnt %u\n", le32_to_cpu(sup->leb_cnt));
344 pr_err("\tmax_leb_cnt %u\n", le32_to_cpu(sup->max_leb_cnt));
345 pr_err("\tmax_bud_bytes %llu\n",
346 (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
347 pr_err("\tlog_lebs %u\n", le32_to_cpu(sup->log_lebs));
348 pr_err("\tlpt_lebs %u\n", le32_to_cpu(sup->lpt_lebs));
349 pr_err("\torph_lebs %u\n", le32_to_cpu(sup->orph_lebs));
350 pr_err("\tjhead_cnt %u\n", le32_to_cpu(sup->jhead_cnt));
351 pr_err("\tfanout %u\n", le32_to_cpu(sup->fanout));
352 pr_err("\tlsave_cnt %u\n", le32_to_cpu(sup->lsave_cnt));
353 pr_err("\tdefault_compr %u\n",
354 (int)le16_to_cpu(sup->default_compr));
355 pr_err("\trp_size %llu\n",
356 (unsigned long long)le64_to_cpu(sup->rp_size));
357 pr_err("\trp_uid %u\n", le32_to_cpu(sup->rp_uid));
358 pr_err("\trp_gid %u\n", le32_to_cpu(sup->rp_gid));
359 pr_err("\tfmt_version %u\n", le32_to_cpu(sup->fmt_version));
360 pr_err("\ttime_gran %u\n", le32_to_cpu(sup->time_gran));
361 pr_err("\tUUID %pUB\n", sup->uuid);
366 const struct ubifs_mst_node *mst = node;
368 pr_err("\thighest_inum %llu\n",
369 (unsigned long long)le64_to_cpu(mst->highest_inum));
370 pr_err("\tcommit number %llu\n",
371 (unsigned long long)le64_to_cpu(mst->cmt_no));
372 pr_err("\tflags %#x\n", le32_to_cpu(mst->flags));
373 pr_err("\tlog_lnum %u\n", le32_to_cpu(mst->log_lnum));
374 pr_err("\troot_lnum %u\n", le32_to_cpu(mst->root_lnum));
375 pr_err("\troot_offs %u\n", le32_to_cpu(mst->root_offs));
376 pr_err("\troot_len %u\n", le32_to_cpu(mst->root_len));
377 pr_err("\tgc_lnum %u\n", le32_to_cpu(mst->gc_lnum));
378 pr_err("\tihead_lnum %u\n", le32_to_cpu(mst->ihead_lnum));
379 pr_err("\tihead_offs %u\n", le32_to_cpu(mst->ihead_offs));
380 pr_err("\tindex_size %llu\n",
381 (unsigned long long)le64_to_cpu(mst->index_size));
382 pr_err("\tlpt_lnum %u\n", le32_to_cpu(mst->lpt_lnum));
383 pr_err("\tlpt_offs %u\n", le32_to_cpu(mst->lpt_offs));
384 pr_err("\tnhead_lnum %u\n", le32_to_cpu(mst->nhead_lnum));
385 pr_err("\tnhead_offs %u\n", le32_to_cpu(mst->nhead_offs));
386 pr_err("\tltab_lnum %u\n", le32_to_cpu(mst->ltab_lnum));
387 pr_err("\tltab_offs %u\n", le32_to_cpu(mst->ltab_offs));
388 pr_err("\tlsave_lnum %u\n", le32_to_cpu(mst->lsave_lnum));
389 pr_err("\tlsave_offs %u\n", le32_to_cpu(mst->lsave_offs));
390 pr_err("\tlscan_lnum %u\n", le32_to_cpu(mst->lscan_lnum));
391 pr_err("\tleb_cnt %u\n", le32_to_cpu(mst->leb_cnt));
392 pr_err("\tempty_lebs %u\n", le32_to_cpu(mst->empty_lebs));
393 pr_err("\tidx_lebs %u\n", le32_to_cpu(mst->idx_lebs));
394 pr_err("\ttotal_free %llu\n",
395 (unsigned long long)le64_to_cpu(mst->total_free));
396 pr_err("\ttotal_dirty %llu\n",
397 (unsigned long long)le64_to_cpu(mst->total_dirty));
398 pr_err("\ttotal_used %llu\n",
399 (unsigned long long)le64_to_cpu(mst->total_used));
400 pr_err("\ttotal_dead %llu\n",
401 (unsigned long long)le64_to_cpu(mst->total_dead));
402 pr_err("\ttotal_dark %llu\n",
403 (unsigned long long)le64_to_cpu(mst->total_dark));
408 const struct ubifs_ref_node *ref = node;
410 pr_err("\tlnum %u\n", le32_to_cpu(ref->lnum));
411 pr_err("\toffs %u\n", le32_to_cpu(ref->offs));
412 pr_err("\tjhead %u\n", le32_to_cpu(ref->jhead));
417 const struct ubifs_ino_node *ino = node;
419 key_read(c, &ino->key, &key);
421 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
422 pr_err("\tcreat_sqnum %llu\n",
423 (unsigned long long)le64_to_cpu(ino->creat_sqnum));
424 pr_err("\tsize %llu\n",
425 (unsigned long long)le64_to_cpu(ino->size));
426 pr_err("\tnlink %u\n", le32_to_cpu(ino->nlink));
427 pr_err("\tatime %lld.%u\n",
428 (long long)le64_to_cpu(ino->atime_sec),
429 le32_to_cpu(ino->atime_nsec));
430 pr_err("\tmtime %lld.%u\n",
431 (long long)le64_to_cpu(ino->mtime_sec),
432 le32_to_cpu(ino->mtime_nsec));
433 pr_err("\tctime %lld.%u\n",
434 (long long)le64_to_cpu(ino->ctime_sec),
435 le32_to_cpu(ino->ctime_nsec));
436 pr_err("\tuid %u\n", le32_to_cpu(ino->uid));
437 pr_err("\tgid %u\n", le32_to_cpu(ino->gid));
438 pr_err("\tmode %u\n", le32_to_cpu(ino->mode));
439 pr_err("\tflags %#x\n", le32_to_cpu(ino->flags));
440 pr_err("\txattr_cnt %u\n", le32_to_cpu(ino->xattr_cnt));
441 pr_err("\txattr_size %u\n", le32_to_cpu(ino->xattr_size));
442 pr_err("\txattr_names %u\n", le32_to_cpu(ino->xattr_names));
443 pr_err("\tcompr_type %#x\n",
444 (int)le16_to_cpu(ino->compr_type));
445 pr_err("\tdata len %u\n", le32_to_cpu(ino->data_len));
448 case UBIFS_DENT_NODE:
449 case UBIFS_XENT_NODE:
451 const struct ubifs_dent_node *dent = node;
452 int nlen = le16_to_cpu(dent->nlen);
454 key_read(c, &dent->key, &key);
456 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
457 pr_err("\tinum %llu\n",
458 (unsigned long long)le64_to_cpu(dent->inum));
459 pr_err("\ttype %d\n", (int)dent->type);
460 pr_err("\tnlen %d\n", nlen);
463 if (nlen > UBIFS_MAX_NLEN)
464 pr_err("(bad name length, not printing, bad or corrupted node)");
466 for (i = 0; i < nlen && dent->name[i]; i++)
467 pr_cont("%c", dent->name[i]);
473 case UBIFS_DATA_NODE:
475 const struct ubifs_data_node *dn = node;
476 int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
478 key_read(c, &dn->key, &key);
480 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
481 pr_err("\tsize %u\n", le32_to_cpu(dn->size));
482 pr_err("\tcompr_typ %d\n",
483 (int)le16_to_cpu(dn->compr_type));
484 pr_err("\tdata size %d\n", dlen);
486 print_hex_dump(KERN_ERR, "\t", DUMP_PREFIX_OFFSET, 32, 1,
487 (void *)&dn->data, dlen, 0);
490 case UBIFS_TRUN_NODE:
492 const struct ubifs_trun_node *trun = node;
494 pr_err("\tinum %u\n", le32_to_cpu(trun->inum));
495 pr_err("\told_size %llu\n",
496 (unsigned long long)le64_to_cpu(trun->old_size));
497 pr_err("\tnew_size %llu\n",
498 (unsigned long long)le64_to_cpu(trun->new_size));
503 const struct ubifs_idx_node *idx = node;
505 n = le16_to_cpu(idx->child_cnt);
506 pr_err("\tchild_cnt %d\n", n);
507 pr_err("\tlevel %d\n", (int)le16_to_cpu(idx->level));
508 pr_err("\tBranches:\n");
510 for (i = 0; i < n && i < c->fanout - 1; i++) {
511 const struct ubifs_branch *br;
513 br = ubifs_idx_branch(c, idx, i);
514 key_read(c, &br->key, &key);
515 pr_err("\t%d: LEB %d:%d len %d key %s\n",
516 i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
517 le32_to_cpu(br->len),
518 dbg_snprintf_key(c, &key, key_buf,
525 case UBIFS_ORPH_NODE:
527 const struct ubifs_orph_node *orph = node;
529 pr_err("\tcommit number %llu\n",
531 le64_to_cpu(orph->cmt_no) & LLONG_MAX);
532 pr_err("\tlast node flag %llu\n",
533 (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
534 n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
535 pr_err("\t%d orphan inode numbers:\n", n);
536 for (i = 0; i < n; i++)
537 pr_err("\t ino %llu\n",
538 (unsigned long long)le64_to_cpu(orph->inos[i]));
542 pr_err("node type %d was not recognized\n",
545 spin_unlock(&dbg_lock);
548 void ubifs_dump_budget_req(const struct ubifs_budget_req *req)
550 spin_lock(&dbg_lock);
551 pr_err("Budgeting request: new_ino %d, dirtied_ino %d\n",
552 req->new_ino, req->dirtied_ino);
553 pr_err("\tnew_ino_d %d, dirtied_ino_d %d\n",
554 req->new_ino_d, req->dirtied_ino_d);
555 pr_err("\tnew_page %d, dirtied_page %d\n",
556 req->new_page, req->dirtied_page);
557 pr_err("\tnew_dent %d, mod_dent %d\n",
558 req->new_dent, req->mod_dent);
559 pr_err("\tidx_growth %d\n", req->idx_growth);
560 pr_err("\tdata_growth %d dd_growth %d\n",
561 req->data_growth, req->dd_growth);
562 spin_unlock(&dbg_lock);
565 void ubifs_dump_lstats(const struct ubifs_lp_stats *lst)
567 spin_lock(&dbg_lock);
568 pr_err("(pid %d) Lprops statistics: empty_lebs %d, idx_lebs %d\n",
569 current->pid, lst->empty_lebs, lst->idx_lebs);
570 pr_err("\ttaken_empty_lebs %d, total_free %lld, total_dirty %lld\n",
571 lst->taken_empty_lebs, lst->total_free, lst->total_dirty);
572 pr_err("\ttotal_used %lld, total_dark %lld, total_dead %lld\n",
573 lst->total_used, lst->total_dark, lst->total_dead);
574 spin_unlock(&dbg_lock);
578 void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)
582 struct ubifs_bud *bud;
583 struct ubifs_gced_idx_leb *idx_gc;
584 long long available, outstanding, free;
586 spin_lock(&c->space_lock);
587 spin_lock(&dbg_lock);
588 pr_err("(pid %d) Budgeting info: data budget sum %lld, total budget sum %lld\n",
589 current->pid, bi->data_growth + bi->dd_growth,
590 bi->data_growth + bi->dd_growth + bi->idx_growth);
591 pr_err("\tbudg_data_growth %lld, budg_dd_growth %lld, budg_idx_growth %lld\n",
592 bi->data_growth, bi->dd_growth, bi->idx_growth);
593 pr_err("\tmin_idx_lebs %d, old_idx_sz %llu, uncommitted_idx %lld\n",
594 bi->min_idx_lebs, bi->old_idx_sz, bi->uncommitted_idx);
595 pr_err("\tpage_budget %d, inode_budget %d, dent_budget %d\n",
596 bi->page_budget, bi->inode_budget, bi->dent_budget);
597 pr_err("\tnospace %u, nospace_rp %u\n", bi->nospace, bi->nospace_rp);
598 pr_err("\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
599 c->dark_wm, c->dead_wm, c->max_idx_node_sz);
603 * If we are dumping saved budgeting data, do not print
604 * additional information which is about the current state, not
605 * the old one which corresponded to the saved budgeting data.
609 pr_err("\tfreeable_cnt %d, calc_idx_sz %lld, idx_gc_cnt %d\n",
610 c->freeable_cnt, c->calc_idx_sz, c->idx_gc_cnt);
611 pr_err("\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, clean_zn_cnt %ld\n",
612 atomic_long_read(&c->dirty_pg_cnt),
613 atomic_long_read(&c->dirty_zn_cnt),
614 atomic_long_read(&c->clean_zn_cnt));
615 pr_err("\tgc_lnum %d, ihead_lnum %d\n", c->gc_lnum, c->ihead_lnum);
617 /* If we are in R/O mode, journal heads do not exist */
619 for (i = 0; i < c->jhead_cnt; i++)
620 pr_err("\tjhead %s\t LEB %d\n",
621 dbg_jhead(c->jheads[i].wbuf.jhead),
622 c->jheads[i].wbuf.lnum);
623 for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
624 bud = rb_entry(rb, struct ubifs_bud, rb);
625 pr_err("\tbud LEB %d\n", bud->lnum);
627 list_for_each_entry(bud, &c->old_buds, list)
628 pr_err("\told bud LEB %d\n", bud->lnum);
629 list_for_each_entry(idx_gc, &c->idx_gc, list)
630 pr_err("\tGC'ed idx LEB %d unmap %d\n",
631 idx_gc->lnum, idx_gc->unmap);
632 pr_err("\tcommit state %d\n", c->cmt_state);
634 /* Print budgeting predictions */
635 available = ubifs_calc_available(c, c->bi.min_idx_lebs);
636 outstanding = c->bi.data_growth + c->bi.dd_growth;
637 free = ubifs_get_free_space_nolock(c);
638 pr_err("Budgeting predictions:\n");
639 pr_err("\tavailable: %lld, outstanding %lld, free %lld\n",
640 available, outstanding, free);
642 spin_unlock(&dbg_lock);
643 spin_unlock(&c->space_lock);
646 void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)
651 void ubifs_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
653 int i, spc, dark = 0, dead = 0;
655 struct ubifs_bud *bud;
657 spc = lp->free + lp->dirty;
658 if (spc < c->dead_wm)
661 dark = ubifs_calc_dark(c, spc);
663 if (lp->flags & LPROPS_INDEX)
664 pr_err("LEB %-7d free %-8d dirty %-8d used %-8d free + dirty %-8d flags %#x (",
665 lp->lnum, lp->free, lp->dirty, c->leb_size - spc, spc,
668 pr_err("LEB %-7d free %-8d dirty %-8d used %-8d free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d flags %#-4x (",
669 lp->lnum, lp->free, lp->dirty, c->leb_size - spc, spc,
670 dark, dead, (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags);
672 if (lp->flags & LPROPS_TAKEN) {
673 if (lp->flags & LPROPS_INDEX)
674 pr_cont("index, taken");
680 if (lp->flags & LPROPS_INDEX) {
681 switch (lp->flags & LPROPS_CAT_MASK) {
682 case LPROPS_DIRTY_IDX:
685 case LPROPS_FRDI_IDX:
686 s = "freeable index";
692 switch (lp->flags & LPROPS_CAT_MASK) {
694 s = "not categorized";
705 case LPROPS_FREEABLE:
716 for (rb = rb_first((struct rb_root *)&c->buds); rb; rb = rb_next(rb)) {
717 bud = rb_entry(rb, struct ubifs_bud, rb);
718 if (bud->lnum == lp->lnum) {
720 for (i = 0; i < c->jhead_cnt; i++) {
722 * Note, if we are in R/O mode or in the middle
723 * of mounting/re-mounting, the write-buffers do
727 lp->lnum == c->jheads[i].wbuf.lnum) {
728 pr_cont(", jhead %s", dbg_jhead(i));
733 pr_cont(", bud of jhead %s",
734 dbg_jhead(bud->jhead));
737 if (lp->lnum == c->gc_lnum)
742 void ubifs_dump_lprops(struct ubifs_info *c)
745 struct ubifs_lprops lp;
746 struct ubifs_lp_stats lst;
748 pr_err("(pid %d) start dumping LEB properties\n", current->pid);
749 ubifs_get_lp_stats(c, &lst);
750 ubifs_dump_lstats(&lst);
752 for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
753 err = ubifs_read_one_lp(c, lnum, &lp);
755 ubifs_err("cannot read lprops for LEB %d", lnum);
757 ubifs_dump_lprop(c, &lp);
759 pr_err("(pid %d) finish dumping LEB properties\n", current->pid);
762 void ubifs_dump_lpt_info(struct ubifs_info *c)
766 spin_lock(&dbg_lock);
767 pr_err("(pid %d) dumping LPT information\n", current->pid);
768 pr_err("\tlpt_sz: %lld\n", c->lpt_sz);
769 pr_err("\tpnode_sz: %d\n", c->pnode_sz);
770 pr_err("\tnnode_sz: %d\n", c->nnode_sz);
771 pr_err("\tltab_sz: %d\n", c->ltab_sz);
772 pr_err("\tlsave_sz: %d\n", c->lsave_sz);
773 pr_err("\tbig_lpt: %d\n", c->big_lpt);
774 pr_err("\tlpt_hght: %d\n", c->lpt_hght);
775 pr_err("\tpnode_cnt: %d\n", c->pnode_cnt);
776 pr_err("\tnnode_cnt: %d\n", c->nnode_cnt);
777 pr_err("\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt);
778 pr_err("\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt);
779 pr_err("\tlsave_cnt: %d\n", c->lsave_cnt);
780 pr_err("\tspace_bits: %d\n", c->space_bits);
781 pr_err("\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
782 pr_err("\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
783 pr_err("\tlpt_spc_bits: %d\n", c->lpt_spc_bits);
784 pr_err("\tpcnt_bits: %d\n", c->pcnt_bits);
785 pr_err("\tlnum_bits: %d\n", c->lnum_bits);
786 pr_err("\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
787 pr_err("\tLPT head is at %d:%d\n",
788 c->nhead_lnum, c->nhead_offs);
789 pr_err("\tLPT ltab is at %d:%d\n", c->ltab_lnum, c->ltab_offs);
791 pr_err("\tLPT lsave is at %d:%d\n",
792 c->lsave_lnum, c->lsave_offs);
793 for (i = 0; i < c->lpt_lebs; i++)
794 pr_err("\tLPT LEB %d free %d dirty %d tgc %d cmt %d\n",
795 i + c->lpt_first, c->ltab[i].free, c->ltab[i].dirty,
796 c->ltab[i].tgc, c->ltab[i].cmt);
797 spin_unlock(&dbg_lock);
800 void ubifs_dump_sleb(const struct ubifs_info *c,
801 const struct ubifs_scan_leb *sleb, int offs)
803 struct ubifs_scan_node *snod;
805 pr_err("(pid %d) start dumping scanned data from LEB %d:%d\n",
806 current->pid, sleb->lnum, offs);
808 list_for_each_entry(snod, &sleb->nodes, list) {
810 pr_err("Dumping node at LEB %d:%d len %d\n",
811 sleb->lnum, snod->offs, snod->len);
812 ubifs_dump_node(c, snod->node);
816 void ubifs_dump_leb(const struct ubifs_info *c, int lnum)
818 struct ubifs_scan_leb *sleb;
819 struct ubifs_scan_node *snod;
822 pr_err("(pid %d) start dumping LEB %d\n", current->pid, lnum);
824 buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
826 ubifs_err("cannot allocate memory for dumping LEB %d", lnum);
830 sleb = ubifs_scan(c, lnum, 0, buf, 0);
832 ubifs_err("scan error %d", (int)PTR_ERR(sleb));
836 pr_err("LEB %d has %d nodes ending at %d\n", lnum,
837 sleb->nodes_cnt, sleb->endpt);
839 list_for_each_entry(snod, &sleb->nodes, list) {
841 pr_err("Dumping node at LEB %d:%d len %d\n", lnum,
842 snod->offs, snod->len);
843 ubifs_dump_node(c, snod->node);
846 pr_err("(pid %d) finish dumping LEB %d\n", current->pid, lnum);
847 ubifs_scan_destroy(sleb);
854 void ubifs_dump_znode(const struct ubifs_info *c,
855 const struct ubifs_znode *znode)
858 const struct ubifs_zbranch *zbr;
859 char key_buf[DBG_KEY_BUF_LEN];
861 spin_lock(&dbg_lock);
863 zbr = &znode->parent->zbranch[znode->iip];
867 pr_err("znode %p, LEB %d:%d len %d parent %p iip %d level %d child_cnt %d flags %lx\n",
868 znode, zbr->lnum, zbr->offs, zbr->len, znode->parent, znode->iip,
869 znode->level, znode->child_cnt, znode->flags);
871 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
872 spin_unlock(&dbg_lock);
876 pr_err("zbranches:\n");
877 for (n = 0; n < znode->child_cnt; n++) {
878 zbr = &znode->zbranch[n];
879 if (znode->level > 0)
880 pr_err("\t%d: znode %p LEB %d:%d len %d key %s\n",
881 n, zbr->znode, zbr->lnum, zbr->offs, zbr->len,
882 dbg_snprintf_key(c, &zbr->key, key_buf,
885 pr_err("\t%d: LNC %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 spin_unlock(&dbg_lock);
893 void ubifs_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
897 pr_err("(pid %d) start dumping heap cat %d (%d elements)\n",
898 current->pid, cat, heap->cnt);
899 for (i = 0; i < heap->cnt; i++) {
900 struct ubifs_lprops *lprops = heap->arr[i];
902 pr_err("\t%d. LEB %d hpos %d free %d dirty %d flags %d\n",
903 i, lprops->lnum, lprops->hpos, lprops->free,
904 lprops->dirty, lprops->flags);
906 pr_err("(pid %d) finish dumping heap\n", current->pid);
909 void ubifs_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
910 struct ubifs_nnode *parent, int iip)
914 pr_err("(pid %d) dumping pnode:\n", current->pid);
915 pr_err("\taddress %zx parent %zx cnext %zx\n",
916 (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
917 pr_err("\tflags %lu iip %d level %d num %d\n",
918 pnode->flags, iip, pnode->level, pnode->num);
919 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
920 struct ubifs_lprops *lp = &pnode->lprops[i];
922 pr_err("\t%d: free %d dirty %d flags %d lnum %d\n",
923 i, lp->free, lp->dirty, lp->flags, lp->lnum);
927 void ubifs_dump_tnc(struct ubifs_info *c)
929 struct ubifs_znode *znode;
933 pr_err("(pid %d) start dumping TNC tree\n", current->pid);
934 znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
935 level = znode->level;
936 pr_err("== Level %d ==\n", level);
938 if (level != znode->level) {
939 level = znode->level;
940 pr_err("== Level %d ==\n", level);
942 ubifs_dump_znode(c, znode);
943 znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
945 pr_err("(pid %d) finish dumping TNC tree\n", current->pid);
948 static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
951 ubifs_dump_znode(c, znode);
956 * ubifs_dump_index - dump the on-flash index.
957 * @c: UBIFS file-system description object
959 * This function dumps whole UBIFS indexing B-tree, unlike 'ubifs_dump_tnc()'
960 * which dumps only in-memory znodes and does not read znodes which from flash.
962 void ubifs_dump_index(struct ubifs_info *c)
964 dbg_walk_index(c, NULL, dump_znode, NULL);
969 * dbg_save_space_info - save information about flash space.
970 * @c: UBIFS file-system description object
972 * This function saves information about UBIFS free space, dirty space, etc, in
973 * order to check it later.
975 void dbg_save_space_info(struct ubifs_info *c)
977 struct ubifs_debug_info *d = c->dbg;
980 spin_lock(&c->space_lock);
981 memcpy(&d->saved_lst, &c->lst, sizeof(struct ubifs_lp_stats));
982 memcpy(&d->saved_bi, &c->bi, sizeof(struct ubifs_budg_info));
983 d->saved_idx_gc_cnt = c->idx_gc_cnt;
986 * We use a dirty hack here and zero out @c->freeable_cnt, because it
987 * affects the free space calculations, and UBIFS might not know about
988 * all freeable eraseblocks. Indeed, we know about freeable eraseblocks
989 * only when we read their lprops, and we do this only lazily, upon the
990 * need. So at any given point of time @c->freeable_cnt might be not
993 * Just one example about the issue we hit when we did not zero
995 * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the
996 * amount of free space in @d->saved_free
997 * 2. We re-mount R/W, which makes UBIFS to read the "lsave"
998 * information from flash, where we cache LEBs from various
999 * categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()'
1000 * -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()'
1001 * -> 'ubifs_get_pnode()' -> 'update_cats()'
1002 * -> 'ubifs_add_to_cat()').
1003 * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt
1005 * 4. We calculate the amount of free space when the re-mount is
1006 * finished in 'dbg_check_space_info()' and it does not match
1009 freeable_cnt = c->freeable_cnt;
1010 c->freeable_cnt = 0;
1011 d->saved_free = ubifs_get_free_space_nolock(c);
1012 c->freeable_cnt = freeable_cnt;
1013 spin_unlock(&c->space_lock);
1017 * dbg_check_space_info - check flash space information.
1018 * @c: UBIFS file-system description object
1020 * This function compares current flash space information with the information
1021 * which was saved when the 'dbg_save_space_info()' function was called.
1022 * Returns zero if the information has not changed, and %-EINVAL it it has
1025 int dbg_check_space_info(struct ubifs_info *c)
1027 struct ubifs_debug_info *d = c->dbg;
1028 struct ubifs_lp_stats lst;
1032 spin_lock(&c->space_lock);
1033 freeable_cnt = c->freeable_cnt;
1034 c->freeable_cnt = 0;
1035 free = ubifs_get_free_space_nolock(c);
1036 c->freeable_cnt = freeable_cnt;
1037 spin_unlock(&c->space_lock);
1039 if (free != d->saved_free) {
1040 ubifs_err("free space changed from %lld to %lld",
1041 d->saved_free, free);
1048 ubifs_msg("saved lprops statistics dump");
1049 ubifs_dump_lstats(&d->saved_lst);
1050 ubifs_msg("saved budgeting info dump");
1051 ubifs_dump_budg(c, &d->saved_bi);
1052 ubifs_msg("saved idx_gc_cnt %d", d->saved_idx_gc_cnt);
1053 ubifs_msg("current lprops statistics dump");
1054 ubifs_get_lp_stats(c, &lst);
1055 ubifs_dump_lstats(&lst);
1056 ubifs_msg("current budgeting info dump");
1057 ubifs_dump_budg(c, &c->bi);
1063 * dbg_check_synced_i_size - check synchronized inode size.
1064 * @c: UBIFS file-system description object
1065 * @inode: inode to check
1067 * If inode is clean, synchronized inode size has to be equivalent to current
1068 * inode size. This function has to be called only for locked inodes (@i_mutex
1069 * has to be locked). Returns %0 if synchronized inode size if correct, and
1072 int dbg_check_synced_i_size(const struct ubifs_info *c, struct inode *inode)
1075 struct ubifs_inode *ui = ubifs_inode(inode);
1077 if (!dbg_is_chk_gen(c))
1079 if (!S_ISREG(inode->i_mode))
1082 mutex_lock(&ui->ui_mutex);
1083 spin_lock(&ui->ui_lock);
1084 if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
1085 ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode is clean",
1086 ui->ui_size, ui->synced_i_size);
1087 ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
1088 inode->i_mode, i_size_read(inode));
1092 spin_unlock(&ui->ui_lock);
1093 mutex_unlock(&ui->ui_mutex);
1098 * dbg_check_dir - check directory inode size and link count.
1099 * @c: UBIFS file-system description object
1100 * @dir: the directory to calculate size for
1101 * @size: the result is returned here
1103 * This function makes sure that directory size and link count are correct.
1104 * Returns zero in case of success and a negative error code in case of
1107 * Note, it is good idea to make sure the @dir->i_mutex is locked before
1108 * calling this function.
1110 int dbg_check_dir(struct ubifs_info *c, const struct inode *dir)
1112 unsigned int nlink = 2;
1113 union ubifs_key key;
1114 struct ubifs_dent_node *dent, *pdent = NULL;
1115 struct qstr nm = { .name = NULL };
1116 loff_t size = UBIFS_INO_NODE_SZ;
1118 if (!dbg_is_chk_gen(c))
1121 if (!S_ISDIR(dir->i_mode))
1124 lowest_dent_key(c, &key, dir->i_ino);
1128 dent = ubifs_tnc_next_ent(c, &key, &nm);
1130 err = PTR_ERR(dent);
1136 nm.name = dent->name;
1137 nm.len = le16_to_cpu(dent->nlen);
1138 size += CALC_DENT_SIZE(nm.len);
1139 if (dent->type == UBIFS_ITYPE_DIR)
1143 key_read(c, &dent->key, &key);
1147 if (i_size_read(dir) != size) {
1148 ubifs_err("directory inode %lu has size %llu, but calculated size is %llu",
1149 dir->i_ino, (unsigned long long)i_size_read(dir),
1150 (unsigned long long)size);
1151 ubifs_dump_inode(c, dir);
1155 if (dir->i_nlink != nlink) {
1156 ubifs_err("directory inode %lu has nlink %u, but calculated nlink is %u",
1157 dir->i_ino, dir->i_nlink, nlink);
1158 ubifs_dump_inode(c, dir);
1167 * dbg_check_key_order - make sure that colliding keys are properly ordered.
1168 * @c: UBIFS file-system description object
1169 * @zbr1: first zbranch
1170 * @zbr2: following zbranch
1172 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
1173 * names of the direntries/xentries which are referred by the keys. This
1174 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
1175 * sure the name of direntry/xentry referred by @zbr1 is less than
1176 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
1177 * and a negative error code in case of failure.
1179 static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
1180 struct ubifs_zbranch *zbr2)
1182 int err, nlen1, nlen2, cmp;
1183 struct ubifs_dent_node *dent1, *dent2;
1184 union ubifs_key key;
1185 char key_buf[DBG_KEY_BUF_LEN];
1187 ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
1188 dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1191 dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1197 err = ubifs_tnc_read_node(c, zbr1, dent1);
1200 err = ubifs_validate_entry(c, dent1);
1204 err = ubifs_tnc_read_node(c, zbr2, dent2);
1207 err = ubifs_validate_entry(c, dent2);
1211 /* Make sure node keys are the same as in zbranch */
1213 key_read(c, &dent1->key, &key);
1214 if (keys_cmp(c, &zbr1->key, &key)) {
1215 ubifs_err("1st entry at %d:%d has key %s", zbr1->lnum,
1216 zbr1->offs, dbg_snprintf_key(c, &key, key_buf,
1218 ubifs_err("but it should have key %s according to tnc",
1219 dbg_snprintf_key(c, &zbr1->key, key_buf,
1221 ubifs_dump_node(c, dent1);
1225 key_read(c, &dent2->key, &key);
1226 if (keys_cmp(c, &zbr2->key, &key)) {
1227 ubifs_err("2nd entry at %d:%d has key %s", zbr1->lnum,
1228 zbr1->offs, dbg_snprintf_key(c, &key, key_buf,
1230 ubifs_err("but it should have key %s according to tnc",
1231 dbg_snprintf_key(c, &zbr2->key, key_buf,
1233 ubifs_dump_node(c, dent2);
1237 nlen1 = le16_to_cpu(dent1->nlen);
1238 nlen2 = le16_to_cpu(dent2->nlen);
1240 cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
1241 if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
1245 if (cmp == 0 && nlen1 == nlen2)
1246 ubifs_err("2 xent/dent nodes with the same name");
1248 ubifs_err("bad order of colliding key %s",
1249 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
1251 ubifs_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs);
1252 ubifs_dump_node(c, dent1);
1253 ubifs_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs);
1254 ubifs_dump_node(c, dent2);
1263 * dbg_check_znode - check if znode is all right.
1264 * @c: UBIFS file-system description object
1265 * @zbr: zbranch which points to this znode
1267 * This function makes sure that znode referred to by @zbr is all right.
1268 * Returns zero if it is, and %-EINVAL if it is not.
1270 static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
1272 struct ubifs_znode *znode = zbr->znode;
1273 struct ubifs_znode *zp = znode->parent;
1276 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
1280 if (znode->level < 0) {
1284 if (znode->iip < 0 || znode->iip >= c->fanout) {
1290 /* Only dirty zbranch may have no on-flash nodes */
1291 if (!ubifs_zn_dirty(znode)) {
1296 if (ubifs_zn_dirty(znode)) {
1298 * If znode is dirty, its parent has to be dirty as well. The
1299 * order of the operation is important, so we have to have
1303 if (zp && !ubifs_zn_dirty(zp)) {
1305 * The dirty flag is atomic and is cleared outside the
1306 * TNC mutex, so znode's dirty flag may now have
1307 * been cleared. The child is always cleared before the
1308 * parent, so we just need to check again.
1311 if (ubifs_zn_dirty(znode)) {
1319 const union ubifs_key *min, *max;
1321 if (znode->level != zp->level - 1) {
1326 /* Make sure the 'parent' pointer in our znode is correct */
1327 err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
1329 /* This zbranch does not exist in the parent */
1334 if (znode->iip >= zp->child_cnt) {
1339 if (znode->iip != n) {
1340 /* This may happen only in case of collisions */
1341 if (keys_cmp(c, &zp->zbranch[n].key,
1342 &zp->zbranch[znode->iip].key)) {
1350 * Make sure that the first key in our znode is greater than or
1351 * equal to the key in the pointing zbranch.
1354 cmp = keys_cmp(c, min, &znode->zbranch[0].key);
1360 if (n + 1 < zp->child_cnt) {
1361 max = &zp->zbranch[n + 1].key;
1364 * Make sure the last key in our znode is less or
1365 * equivalent than the key in the zbranch which goes
1366 * after our pointing zbranch.
1368 cmp = keys_cmp(c, max,
1369 &znode->zbranch[znode->child_cnt - 1].key);
1376 /* This may only be root znode */
1377 if (zbr != &c->zroot) {
1384 * Make sure that next key is greater or equivalent then the previous
1387 for (n = 1; n < znode->child_cnt; n++) {
1388 cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
1389 &znode->zbranch[n].key);
1395 /* This can only be keys with colliding hash */
1396 if (!is_hash_key(c, &znode->zbranch[n].key)) {
1401 if (znode->level != 0 || c->replaying)
1405 * Colliding keys should follow binary order of
1406 * corresponding xentry/dentry names.
1408 err = dbg_check_key_order(c, &znode->zbranch[n - 1],
1409 &znode->zbranch[n]);
1419 for (n = 0; n < znode->child_cnt; n++) {
1420 if (!znode->zbranch[n].znode &&
1421 (znode->zbranch[n].lnum == 0 ||
1422 znode->zbranch[n].len == 0)) {
1427 if (znode->zbranch[n].lnum != 0 &&
1428 znode->zbranch[n].len == 0) {
1433 if (znode->zbranch[n].lnum == 0 &&
1434 znode->zbranch[n].len != 0) {
1439 if (znode->zbranch[n].lnum == 0 &&
1440 znode->zbranch[n].offs != 0) {
1445 if (znode->level != 0 && znode->zbranch[n].znode)
1446 if (znode->zbranch[n].znode->parent != znode) {
1455 ubifs_err("failed, error %d", err);
1456 ubifs_msg("dump of the znode");
1457 ubifs_dump_znode(c, znode);
1459 ubifs_msg("dump of the parent znode");
1460 ubifs_dump_znode(c, zp);
1467 int dbg_check_dir(struct ubifs_info *c, const struct inode *dir)
1472 void dbg_debugfs_exit_fs(struct ubifs_info *c)
1477 int ubifs_debugging_init(struct ubifs_info *c)
1481 void ubifs_debugging_exit(struct ubifs_info *c)
1484 int dbg_check_filesystem(struct ubifs_info *c)
1488 int dbg_debugfs_init_fs(struct ubifs_info *c)
1496 * dbg_check_tnc - check TNC tree.
1497 * @c: UBIFS file-system description object
1498 * @extra: do extra checks that are possible at start commit
1500 * This function traverses whole TNC tree and checks every znode. Returns zero
1501 * if everything is all right and %-EINVAL if something is wrong with TNC.
1503 int dbg_check_tnc(struct ubifs_info *c, int extra)
1505 struct ubifs_znode *znode;
1506 long clean_cnt = 0, dirty_cnt = 0;
1509 if (!dbg_is_chk_index(c))
1512 ubifs_assert(mutex_is_locked(&c->tnc_mutex));
1513 if (!c->zroot.znode)
1516 znode = ubifs_tnc_postorder_first(c->zroot.znode);
1518 struct ubifs_znode *prev;
1519 struct ubifs_zbranch *zbr;
1524 zbr = &znode->parent->zbranch[znode->iip];
1526 err = dbg_check_znode(c, zbr);
1531 if (ubifs_zn_dirty(znode))
1538 znode = ubifs_tnc_postorder_next(znode);
1543 * If the last key of this znode is equivalent to the first key
1544 * of the next znode (collision), then check order of the keys.
1546 last = prev->child_cnt - 1;
1547 if (prev->level == 0 && znode->level == 0 && !c->replaying &&
1548 !keys_cmp(c, &prev->zbranch[last].key,
1549 &znode->zbranch[0].key)) {
1550 err = dbg_check_key_order(c, &prev->zbranch[last],
1551 &znode->zbranch[0]);
1555 ubifs_msg("first znode");
1556 ubifs_dump_znode(c, prev);
1557 ubifs_msg("second znode");
1558 ubifs_dump_znode(c, znode);
1565 if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
1566 ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld",
1567 atomic_long_read(&c->clean_zn_cnt),
1571 if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
1572 ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld",
1573 atomic_long_read(&c->dirty_zn_cnt),
1582 int dbg_check_tnc(struct ubifs_info *c, int extra)
1589 * dbg_walk_index - walk the on-flash index.
1590 * @c: UBIFS file-system description object
1591 * @leaf_cb: called for each leaf node
1592 * @znode_cb: called for each indexing node
1593 * @priv: private data which is passed to callbacks
1595 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1596 * node and @znode_cb for each indexing node. Returns zero in case of success
1597 * and a negative error code in case of failure.
1599 * It would be better if this function removed every znode it pulled to into
1600 * the TNC, so that the behavior more closely matched the non-debugging
1603 int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1604 dbg_znode_callback znode_cb, void *priv)
1607 struct ubifs_zbranch *zbr;
1608 struct ubifs_znode *znode, *child;
1610 mutex_lock(&c->tnc_mutex);
1611 /* If the root indexing node is not in TNC - pull it */
1612 if (!c->zroot.znode) {
1613 c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1614 if (IS_ERR(c->zroot.znode)) {
1615 err = PTR_ERR(c->zroot.znode);
1616 c->zroot.znode = NULL;
1622 * We are going to traverse the indexing tree in the postorder manner.
1623 * Go down and find the leftmost indexing node where we are going to
1626 znode = c->zroot.znode;
1627 while (znode->level > 0) {
1628 zbr = &znode->zbranch[0];
1631 child = ubifs_load_znode(c, zbr, znode, 0);
1632 if (IS_ERR(child)) {
1633 err = PTR_ERR(child);
1642 /* Iterate over all indexing nodes */
1649 err = znode_cb(c, znode, priv);
1651 ubifs_err("znode checking function returned error %d",
1653 ubifs_dump_znode(c, znode);
1657 if (leaf_cb && znode->level == 0) {
1658 for (idx = 0; idx < znode->child_cnt; idx++) {
1659 zbr = &znode->zbranch[idx];
1660 err = leaf_cb(c, zbr, priv);
1662 ubifs_err("leaf checking function returned error %d, for leaf at LEB %d:%d",
1663 err, zbr->lnum, zbr->offs);
1672 idx = znode->iip + 1;
1673 znode = znode->parent;
1674 if (idx < znode->child_cnt) {
1675 /* Switch to the next index in the parent */
1676 zbr = &znode->zbranch[idx];
1679 child = ubifs_load_znode(c, zbr, znode, idx);
1680 if (IS_ERR(child)) {
1681 err = PTR_ERR(child);
1689 * This is the last child, switch to the parent and
1694 /* Go to the lowest leftmost znode in the new sub-tree */
1695 while (znode->level > 0) {
1696 zbr = &znode->zbranch[0];
1699 child = ubifs_load_znode(c, zbr, znode, 0);
1700 if (IS_ERR(child)) {
1701 err = PTR_ERR(child);
1710 mutex_unlock(&c->tnc_mutex);
1715 zbr = &znode->parent->zbranch[znode->iip];
1718 ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1719 ubifs_dump_znode(c, znode);
1721 mutex_unlock(&c->tnc_mutex);
1726 * add_size - add znode size to partially calculated index size.
1727 * @c: UBIFS file-system description object
1728 * @znode: znode to add size for
1729 * @priv: partially calculated index size
1731 * This is a helper function for 'dbg_check_idx_size()' which is called for
1732 * every indexing node and adds its size to the 'long long' variable pointed to
1735 static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1737 long long *idx_size = priv;
1740 add = ubifs_idx_node_sz(c, znode->child_cnt);
1741 add = ALIGN(add, 8);
1747 * dbg_check_idx_size - check index size.
1748 * @c: UBIFS file-system description object
1749 * @idx_size: size to check
1751 * This function walks the UBIFS index, calculates its size and checks that the
1752 * size is equivalent to @idx_size. Returns zero in case of success and a
1753 * negative error code in case of failure.
1755 int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
1760 if (!dbg_is_chk_index(c))
1763 err = dbg_walk_index(c, NULL, add_size, &calc);
1765 ubifs_err("error %d while walking the index", err);
1769 if (calc != idx_size) {
1770 ubifs_err("index size check failed: calculated size is %lld, should be %lld",
1781 * struct fsck_inode - information about an inode used when checking the file-system.
1782 * @rb: link in the RB-tree of inodes
1783 * @inum: inode number
1784 * @mode: inode type, permissions, etc
1785 * @nlink: inode link count
1786 * @xattr_cnt: count of extended attributes
1787 * @references: how many directory/xattr entries refer this inode (calculated
1788 * while walking the index)
1789 * @calc_cnt: for directory inode count of child directories
1790 * @size: inode size (read from on-flash inode)
1791 * @xattr_sz: summary size of all extended attributes (read from on-flash
1793 * @calc_sz: for directories calculated directory size
1794 * @calc_xcnt: count of extended attributes
1795 * @calc_xsz: calculated summary size of all extended attributes
1796 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1797 * inode (read from on-flash inode)
1798 * @calc_xnms: calculated sum of lengths of all extended attribute names
1805 unsigned int xattr_cnt;
1809 unsigned int xattr_sz;
1811 long long calc_xcnt;
1813 unsigned int xattr_nms;
1814 long long calc_xnms;
1818 * struct fsck_data - private FS checking information.
1819 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1822 struct rb_root inodes;
1826 * add_inode - add inode information to RB-tree of inodes.
1827 * @c: UBIFS file-system description object
1828 * @fsckd: FS checking information
1829 * @ino: raw UBIFS inode to add
1831 * This is a helper function for 'check_leaf()' which adds information about
1832 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1833 * case of success and a negative error code in case of failure.
1835 static struct fsck_inode *add_inode(struct ubifs_info *c,
1836 struct fsck_data *fsckd,
1837 struct ubifs_ino_node *ino)
1839 struct rb_node **p, *parent = NULL;
1840 struct fsck_inode *fscki;
1841 ino_t inum = key_inum_flash(c, &ino->key);
1842 struct inode *inode;
1843 struct ubifs_inode *ui;
1845 p = &fsckd->inodes.rb_node;
1848 fscki = rb_entry(parent, struct fsck_inode, rb);
1849 if (inum < fscki->inum)
1851 else if (inum > fscki->inum)
1852 p = &(*p)->rb_right;
1857 if (inum > c->highest_inum) {
1858 ubifs_err("too high inode number, max. is %lu",
1859 (unsigned long)c->highest_inum);
1860 return ERR_PTR(-EINVAL);
1863 fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
1865 return ERR_PTR(-ENOMEM);
1867 inode = ilookup(c->vfs_sb, inum);
1871 * If the inode is present in the VFS inode cache, use it instead of
1872 * the on-flash inode which might be out-of-date. E.g., the size might
1873 * be out-of-date. If we do not do this, the following may happen, for
1875 * 1. A power cut happens
1876 * 2. We mount the file-system R/O, the replay process fixes up the
1877 * inode size in the VFS cache, but on on-flash.
1878 * 3. 'check_leaf()' fails because it hits a data node beyond inode
1882 fscki->nlink = le32_to_cpu(ino->nlink);
1883 fscki->size = le64_to_cpu(ino->size);
1884 fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
1885 fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
1886 fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
1887 fscki->mode = le32_to_cpu(ino->mode);
1889 ui = ubifs_inode(inode);
1890 fscki->nlink = inode->i_nlink;
1891 fscki->size = inode->i_size;
1892 fscki->xattr_cnt = ui->xattr_cnt;
1893 fscki->xattr_sz = ui->xattr_size;
1894 fscki->xattr_nms = ui->xattr_names;
1895 fscki->mode = inode->i_mode;
1899 if (S_ISDIR(fscki->mode)) {
1900 fscki->calc_sz = UBIFS_INO_NODE_SZ;
1901 fscki->calc_cnt = 2;
1904 rb_link_node(&fscki->rb, parent, p);
1905 rb_insert_color(&fscki->rb, &fsckd->inodes);
1911 * search_inode - search inode in the RB-tree of inodes.
1912 * @fsckd: FS checking information
1913 * @inum: inode number to search
1915 * This is a helper function for 'check_leaf()' which searches inode @inum in
1916 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1917 * the inode was not found.
1919 static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
1922 struct fsck_inode *fscki;
1924 p = fsckd->inodes.rb_node;
1926 fscki = rb_entry(p, struct fsck_inode, rb);
1927 if (inum < fscki->inum)
1929 else if (inum > fscki->inum)
1938 * read_add_inode - read inode node and add it to RB-tree of inodes.
1939 * @c: UBIFS file-system description object
1940 * @fsckd: FS checking information
1941 * @inum: inode number to read
1943 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1944 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1945 * information pointer in case of success and a negative error code in case of
1948 static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1949 struct fsck_data *fsckd, ino_t inum)
1952 union ubifs_key key;
1953 struct ubifs_znode *znode;
1954 struct ubifs_zbranch *zbr;
1955 struct ubifs_ino_node *ino;
1956 struct fsck_inode *fscki;
1958 fscki = search_inode(fsckd, inum);
1962 ino_key_init(c, &key, inum);
1963 err = ubifs_lookup_level0(c, &key, &znode, &n);
1965 ubifs_err("inode %lu not found in index", (unsigned long)inum);
1966 return ERR_PTR(-ENOENT);
1967 } else if (err < 0) {
1968 ubifs_err("error %d while looking up inode %lu",
1969 err, (unsigned long)inum);
1970 return ERR_PTR(err);
1973 zbr = &znode->zbranch[n];
1974 if (zbr->len < UBIFS_INO_NODE_SZ) {
1975 ubifs_err("bad node %lu node length %d",
1976 (unsigned long)inum, zbr->len);
1977 return ERR_PTR(-EINVAL);
1980 ino = kmalloc(zbr->len, GFP_NOFS);
1982 return ERR_PTR(-ENOMEM);
1984 err = ubifs_tnc_read_node(c, zbr, ino);
1986 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
1987 zbr->lnum, zbr->offs, err);
1989 return ERR_PTR(err);
1992 fscki = add_inode(c, fsckd, ino);
1994 if (IS_ERR(fscki)) {
1995 ubifs_err("error %ld while adding inode %lu node",
1996 PTR_ERR(fscki), (unsigned long)inum);
2004 * check_leaf - check leaf node.
2005 * @c: UBIFS file-system description object
2006 * @zbr: zbranch of the leaf node to check
2007 * @priv: FS checking information
2009 * This is a helper function for 'dbg_check_filesystem()' which is called for
2010 * every single leaf node while walking the indexing tree. It checks that the
2011 * leaf node referred from the indexing tree exists, has correct CRC, and does
2012 * some other basic validation. This function is also responsible for building
2013 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
2014 * calculates reference count, size, etc for each inode in order to later
2015 * compare them to the information stored inside the inodes and detect possible
2016 * inconsistencies. Returns zero in case of success and a negative error code
2017 * in case of failure.
2019 static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
2024 struct ubifs_ch *ch;
2025 int err, type = key_type(c, &zbr->key);
2026 struct fsck_inode *fscki;
2028 if (zbr->len < UBIFS_CH_SZ) {
2029 ubifs_err("bad leaf length %d (LEB %d:%d)",
2030 zbr->len, zbr->lnum, zbr->offs);
2034 node = kmalloc(zbr->len, GFP_NOFS);
2038 err = ubifs_tnc_read_node(c, zbr, node);
2040 ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
2041 zbr->lnum, zbr->offs, err);
2045 /* If this is an inode node, add it to RB-tree of inodes */
2046 if (type == UBIFS_INO_KEY) {
2047 fscki = add_inode(c, priv, node);
2048 if (IS_ERR(fscki)) {
2049 err = PTR_ERR(fscki);
2050 ubifs_err("error %d while adding inode node", err);
2056 if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
2057 type != UBIFS_DATA_KEY) {
2058 ubifs_err("unexpected node type %d at LEB %d:%d",
2059 type, zbr->lnum, zbr->offs);
2065 if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
2066 ubifs_err("too high sequence number, max. is %llu",
2072 if (type == UBIFS_DATA_KEY) {
2074 struct ubifs_data_node *dn = node;
2077 * Search the inode node this data node belongs to and insert
2078 * it to the RB-tree of inodes.
2080 inum = key_inum_flash(c, &dn->key);
2081 fscki = read_add_inode(c, priv, inum);
2082 if (IS_ERR(fscki)) {
2083 err = PTR_ERR(fscki);
2084 ubifs_err("error %d while processing data node and trying to find inode node %lu",
2085 err, (unsigned long)inum);
2089 /* Make sure the data node is within inode size */
2090 blk_offs = key_block_flash(c, &dn->key);
2091 blk_offs <<= UBIFS_BLOCK_SHIFT;
2092 blk_offs += le32_to_cpu(dn->size);
2093 if (blk_offs > fscki->size) {
2094 ubifs_err("data node at LEB %d:%d is not within inode size %lld",
2095 zbr->lnum, zbr->offs, fscki->size);
2101 struct ubifs_dent_node *dent = node;
2102 struct fsck_inode *fscki1;
2104 err = ubifs_validate_entry(c, dent);
2109 * Search the inode node this entry refers to and the parent
2110 * inode node and insert them to the RB-tree of inodes.
2112 inum = le64_to_cpu(dent->inum);
2113 fscki = read_add_inode(c, priv, inum);
2114 if (IS_ERR(fscki)) {
2115 err = PTR_ERR(fscki);
2116 ubifs_err("error %d while processing entry node and trying to find inode node %lu",
2117 err, (unsigned long)inum);
2121 /* Count how many direntries or xentries refers this inode */
2122 fscki->references += 1;
2124 inum = key_inum_flash(c, &dent->key);
2125 fscki1 = read_add_inode(c, priv, inum);
2126 if (IS_ERR(fscki1)) {
2127 err = PTR_ERR(fscki1);
2128 ubifs_err("error %d while processing entry node and trying to find parent inode node %lu",
2129 err, (unsigned long)inum);
2133 nlen = le16_to_cpu(dent->nlen);
2134 if (type == UBIFS_XENT_KEY) {
2135 fscki1->calc_xcnt += 1;
2136 fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
2137 fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
2138 fscki1->calc_xnms += nlen;
2140 fscki1->calc_sz += CALC_DENT_SIZE(nlen);
2141 if (dent->type == UBIFS_ITYPE_DIR)
2142 fscki1->calc_cnt += 1;
2151 ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
2152 ubifs_dump_node(c, node);
2159 * free_inodes - free RB-tree of inodes.
2160 * @fsckd: FS checking information
2162 static void free_inodes(struct fsck_data *fsckd)
2164 struct fsck_inode *fscki, *n;
2166 rbtree_postorder_for_each_entry_safe(fscki, n, &fsckd->inodes, rb)
2171 * check_inodes - checks all inodes.
2172 * @c: UBIFS file-system description object
2173 * @fsckd: FS checking information
2175 * This is a helper function for 'dbg_check_filesystem()' which walks the
2176 * RB-tree of inodes after the index scan has been finished, and checks that
2177 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
2178 * %-EINVAL if not, and a negative error code in case of failure.
2180 static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
2183 union ubifs_key key;
2184 struct ubifs_znode *znode;
2185 struct ubifs_zbranch *zbr;
2186 struct ubifs_ino_node *ino;
2187 struct fsck_inode *fscki;
2188 struct rb_node *this = rb_first(&fsckd->inodes);
2191 fscki = rb_entry(this, struct fsck_inode, rb);
2192 this = rb_next(this);
2194 if (S_ISDIR(fscki->mode)) {
2196 * Directories have to have exactly one reference (they
2197 * cannot have hardlinks), although root inode is an
2200 if (fscki->inum != UBIFS_ROOT_INO &&
2201 fscki->references != 1) {
2202 ubifs_err("directory inode %lu has %d direntries which refer it, but should be 1",
2203 (unsigned long)fscki->inum,
2207 if (fscki->inum == UBIFS_ROOT_INO &&
2208 fscki->references != 0) {
2209 ubifs_err("root inode %lu has non-zero (%d) direntries which refer it",
2210 (unsigned long)fscki->inum,
2214 if (fscki->calc_sz != fscki->size) {
2215 ubifs_err("directory inode %lu size is %lld, but calculated size is %lld",
2216 (unsigned long)fscki->inum,
2217 fscki->size, fscki->calc_sz);
2220 if (fscki->calc_cnt != fscki->nlink) {
2221 ubifs_err("directory inode %lu nlink is %d, but calculated nlink is %d",
2222 (unsigned long)fscki->inum,
2223 fscki->nlink, fscki->calc_cnt);
2227 if (fscki->references != fscki->nlink) {
2228 ubifs_err("inode %lu nlink is %d, but calculated nlink is %d",
2229 (unsigned long)fscki->inum,
2230 fscki->nlink, fscki->references);
2234 if (fscki->xattr_sz != fscki->calc_xsz) {
2235 ubifs_err("inode %lu has xattr size %u, but calculated size is %lld",
2236 (unsigned long)fscki->inum, fscki->xattr_sz,
2240 if (fscki->xattr_cnt != fscki->calc_xcnt) {
2241 ubifs_err("inode %lu has %u xattrs, but calculated count is %lld",
2242 (unsigned long)fscki->inum,
2243 fscki->xattr_cnt, fscki->calc_xcnt);
2246 if (fscki->xattr_nms != fscki->calc_xnms) {
2247 ubifs_err("inode %lu has xattr names' size %u, but calculated names' size is %lld",
2248 (unsigned long)fscki->inum, fscki->xattr_nms,
2257 /* Read the bad inode and dump it */
2258 ino_key_init(c, &key, fscki->inum);
2259 err = ubifs_lookup_level0(c, &key, &znode, &n);
2261 ubifs_err("inode %lu not found in index",
2262 (unsigned long)fscki->inum);
2264 } else if (err < 0) {
2265 ubifs_err("error %d while looking up inode %lu",
2266 err, (unsigned long)fscki->inum);
2270 zbr = &znode->zbranch[n];
2271 ino = kmalloc(zbr->len, GFP_NOFS);
2275 err = ubifs_tnc_read_node(c, zbr, ino);
2277 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2278 zbr->lnum, zbr->offs, err);
2283 ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",
2284 (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
2285 ubifs_dump_node(c, ino);
2291 * dbg_check_filesystem - check the file-system.
2292 * @c: UBIFS file-system description object
2294 * This function checks the file system, namely:
2295 * o makes sure that all leaf nodes exist and their CRCs are correct;
2296 * o makes sure inode nlink, size, xattr size/count are correct (for all
2299 * The function reads whole indexing tree and all nodes, so it is pretty
2300 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2301 * not, and a negative error code in case of failure.
2303 int dbg_check_filesystem(struct ubifs_info *c)
2306 struct fsck_data fsckd;
2308 if (!dbg_is_chk_fs(c))
2311 fsckd.inodes = RB_ROOT;
2312 err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
2316 err = check_inodes(c, &fsckd);
2320 free_inodes(&fsckd);
2324 ubifs_err("file-system check failed with error %d", err);
2326 free_inodes(&fsckd);
2331 * dbg_check_data_nodes_order - check that list of data nodes is sorted.
2332 * @c: UBIFS file-system description object
2333 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2335 * This function returns zero if the list of data nodes is sorted correctly,
2336 * and %-EINVAL if not.
2338 int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head)
2340 struct list_head *cur;
2341 struct ubifs_scan_node *sa, *sb;
2343 if (!dbg_is_chk_gen(c))
2346 for (cur = head->next; cur->next != head; cur = cur->next) {
2348 uint32_t blka, blkb;
2351 sa = container_of(cur, struct ubifs_scan_node, list);
2352 sb = container_of(cur->next, struct ubifs_scan_node, list);
2354 if (sa->type != UBIFS_DATA_NODE) {
2355 ubifs_err("bad node type %d", sa->type);
2356 ubifs_dump_node(c, sa->node);
2359 if (sb->type != UBIFS_DATA_NODE) {
2360 ubifs_err("bad node type %d", sb->type);
2361 ubifs_dump_node(c, sb->node);
2365 inuma = key_inum(c, &sa->key);
2366 inumb = key_inum(c, &sb->key);
2370 if (inuma > inumb) {
2371 ubifs_err("larger inum %lu goes before inum %lu",
2372 (unsigned long)inuma, (unsigned long)inumb);
2376 blka = key_block(c, &sa->key);
2377 blkb = key_block(c, &sb->key);
2380 ubifs_err("larger block %u goes before %u", blka, blkb);
2384 ubifs_err("two data nodes for the same block");
2392 ubifs_dump_node(c, sa->node);
2393 ubifs_dump_node(c, sb->node);
2398 * dbg_check_nondata_nodes_order - check that list of data nodes is sorted.
2399 * @c: UBIFS file-system description object
2400 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2402 * This function returns zero if the list of non-data nodes is sorted correctly,
2403 * and %-EINVAL if not.
2405 int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head)
2407 struct list_head *cur;
2408 struct ubifs_scan_node *sa, *sb;
2410 if (!dbg_is_chk_gen(c))
2413 for (cur = head->next; cur->next != head; cur = cur->next) {
2415 uint32_t hasha, hashb;
2418 sa = container_of(cur, struct ubifs_scan_node, list);
2419 sb = container_of(cur->next, struct ubifs_scan_node, list);
2421 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2422 sa->type != UBIFS_XENT_NODE) {
2423 ubifs_err("bad node type %d", sa->type);
2424 ubifs_dump_node(c, sa->node);
2427 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2428 sa->type != UBIFS_XENT_NODE) {
2429 ubifs_err("bad node type %d", sb->type);
2430 ubifs_dump_node(c, sb->node);
2434 if (sa->type != UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2435 ubifs_err("non-inode node goes before inode node");
2439 if (sa->type == UBIFS_INO_NODE && sb->type != UBIFS_INO_NODE)
2442 if (sa->type == UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2443 /* Inode nodes are sorted in descending size order */
2444 if (sa->len < sb->len) {
2445 ubifs_err("smaller inode node goes first");
2452 * This is either a dentry or xentry, which should be sorted in
2453 * ascending (parent ino, hash) order.
2455 inuma = key_inum(c, &sa->key);
2456 inumb = key_inum(c, &sb->key);
2460 if (inuma > inumb) {
2461 ubifs_err("larger inum %lu goes before inum %lu",
2462 (unsigned long)inuma, (unsigned long)inumb);
2466 hasha = key_block(c, &sa->key);
2467 hashb = key_block(c, &sb->key);
2469 if (hasha > hashb) {
2470 ubifs_err("larger hash %u goes before %u",
2479 ubifs_msg("dumping first node");
2480 ubifs_dump_node(c, sa->node);
2481 ubifs_msg("dumping second node");
2482 ubifs_dump_node(c, sb->node);
2487 static inline int chance(unsigned int n, unsigned int out_of)
2489 return !!((prandom_u32() % out_of) + 1 <= n);
2493 static int power_cut_emulated(struct ubifs_info *c, int lnum, int write)
2495 struct ubifs_debug_info *d = c->dbg;
2497 ubifs_assert(dbg_is_tst_rcvry(c));
2500 /* First call - decide delay to the power cut */
2502 unsigned long delay;
2506 /* Fail withing 1 minute */
2507 delay = prandom_u32() % 60000;
2508 d->pc_timeout = jiffies;
2509 d->pc_timeout += msecs_to_jiffies(delay);
2510 ubifs_warn("failing after %lums", delay);
2513 delay = prandom_u32() % 10000;
2514 /* Fail within 10000 operations */
2515 d->pc_cnt_max = delay;
2516 ubifs_warn("failing after %lu calls", delay);
2523 /* Determine if failure delay has expired */
2524 if (d->pc_delay == 1 && time_before(jiffies, d->pc_timeout))
2526 if (d->pc_delay == 2 && d->pc_cnt++ < d->pc_cnt_max)
2529 if (lnum == UBIFS_SB_LNUM) {
2530 if (write && chance(1, 2))
2534 ubifs_warn("failing in super block LEB %d", lnum);
2535 } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2538 ubifs_warn("failing in master LEB %d", lnum);
2539 } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2540 if (write && chance(99, 100))
2542 if (chance(399, 400))
2544 ubifs_warn("failing in log LEB %d", lnum);
2545 } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2546 if (write && chance(7, 8))
2550 ubifs_warn("failing in LPT LEB %d", lnum);
2551 } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2552 if (write && chance(1, 2))
2556 ubifs_warn("failing in orphan LEB %d", lnum);
2557 } else if (lnum == c->ihead_lnum) {
2558 if (chance(99, 100))
2560 ubifs_warn("failing in index head LEB %d", lnum);
2561 } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2564 ubifs_warn("failing in GC head LEB %d", lnum);
2565 } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
2566 !ubifs_search_bud(c, lnum)) {
2569 ubifs_warn("failing in non-bud LEB %d", lnum);
2570 } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
2571 c->cmt_state == COMMIT_RUNNING_REQUIRED) {
2572 if (chance(999, 1000))
2574 ubifs_warn("failing in bud LEB %d commit running", lnum);
2576 if (chance(9999, 10000))
2578 ubifs_warn("failing in bud LEB %d commit not running", lnum);
2582 ubifs_warn("========== Power cut emulated ==========");
2587 static int corrupt_data(const struct ubifs_info *c, const void *buf,
2590 unsigned int from, to, ffs = chance(1, 2);
2591 unsigned char *p = (void *)buf;
2593 from = prandom_u32() % len;
2594 /* Corruption span max to end of write unit */
2595 to = min(len, ALIGN(from + 1, c->max_write_size));
2597 ubifs_warn("filled bytes %u-%u with %s", from, to - 1,
2598 ffs ? "0xFFs" : "random data");
2601 memset(p + from, 0xFF, to - from);
2603 prandom_bytes(p + from, to - from);
2608 int dbg_leb_write(struct ubifs_info *c, int lnum, const void *buf,
2613 if (c->dbg->pc_happened)
2616 failing = power_cut_emulated(c, lnum, 1);
2618 len = corrupt_data(c, buf, len);
2619 ubifs_warn("actually write %d bytes to LEB %d:%d (the buffer was corrupted)",
2622 err = ubi_leb_write(c->ubi, lnum, buf, offs, len);
2630 int dbg_leb_change(struct ubifs_info *c, int lnum, const void *buf,
2635 if (c->dbg->pc_happened)
2637 if (power_cut_emulated(c, lnum, 1))
2639 err = ubi_leb_change(c->ubi, lnum, buf, len);
2642 if (power_cut_emulated(c, lnum, 1))
2647 int dbg_leb_unmap(struct ubifs_info *c, int lnum)
2651 if (c->dbg->pc_happened)
2653 if (power_cut_emulated(c, lnum, 0))
2655 err = ubi_leb_unmap(c->ubi, lnum);
2658 if (power_cut_emulated(c, lnum, 0))
2663 int dbg_leb_map(struct ubifs_info *c, int lnum)
2667 if (c->dbg->pc_happened)
2669 if (power_cut_emulated(c, lnum, 0))
2671 err = ubi_leb_map(c->ubi, lnum);
2674 if (power_cut_emulated(c, lnum, 0))
2680 * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
2681 * contain the stuff specific to particular file-system mounts.
2683 static struct dentry *dfs_rootdir;
2685 static int dfs_file_open(struct inode *inode, struct file *file)
2687 file->private_data = inode->i_private;
2688 return nonseekable_open(inode, file);
2692 * provide_user_output - provide output to the user reading a debugfs file.
2693 * @val: boolean value for the answer
2694 * @u: the buffer to store the answer at
2695 * @count: size of the buffer
2696 * @ppos: position in the @u output buffer
2698 * This is a simple helper function which stores @val boolean value in the user
2699 * buffer when the user reads one of UBIFS debugfs files. Returns amount of
2700 * bytes written to @u in case of success and a negative error code in case of
2703 static int provide_user_output(int val, char __user *u, size_t count,
2715 return simple_read_from_buffer(u, count, ppos, buf, 2);
2718 static ssize_t dfs_file_read(struct file *file, char __user *u, size_t count,
2721 struct dentry *dent = file->f_path.dentry;
2722 struct ubifs_info *c = file->private_data;
2723 struct ubifs_debug_info *d = c->dbg;
2726 if (dent == d->dfs_chk_gen)
2728 else if (dent == d->dfs_chk_index)
2730 else if (dent == d->dfs_chk_orph)
2732 else if (dent == d->dfs_chk_lprops)
2733 val = d->chk_lprops;
2734 else if (dent == d->dfs_chk_fs)
2736 else if (dent == d->dfs_tst_rcvry)
2738 else if (dent == d->dfs_ro_error)
2743 return provide_user_output(val, u, count, ppos);
2747 * interpret_user_input - interpret user debugfs file input.
2748 * @u: user-provided buffer with the input
2749 * @count: buffer size
2751 * This is a helper function which interpret user input to a boolean UBIFS
2752 * debugfs file. Returns %0 or %1 in case of success and a negative error code
2753 * in case of failure.
2755 static int interpret_user_input(const char __user *u, size_t count)
2760 buf_size = min_t(size_t, count, (sizeof(buf) - 1));
2761 if (copy_from_user(buf, u, buf_size))
2766 else if (buf[0] == '0')
2772 static ssize_t dfs_file_write(struct file *file, const char __user *u,
2773 size_t count, loff_t *ppos)
2775 struct ubifs_info *c = file->private_data;
2776 struct ubifs_debug_info *d = c->dbg;
2777 struct dentry *dent = file->f_path.dentry;
2781 * TODO: this is racy - the file-system might have already been
2782 * unmounted and we'd oops in this case. The plan is to fix it with
2783 * help of 'iterate_supers_type()' which we should have in v3.0: when
2784 * a debugfs opened, we rember FS's UUID in file->private_data. Then
2785 * whenever we access the FS via a debugfs file, we iterate all UBIFS
2786 * superblocks and fine the one with the same UUID, and take the
2789 * The other way to go suggested by Al Viro is to create a separate
2790 * 'ubifs-debug' file-system instead.
2792 if (file->f_path.dentry == d->dfs_dump_lprops) {
2793 ubifs_dump_lprops(c);
2796 if (file->f_path.dentry == d->dfs_dump_budg) {
2797 ubifs_dump_budg(c, &c->bi);
2800 if (file->f_path.dentry == d->dfs_dump_tnc) {
2801 mutex_lock(&c->tnc_mutex);
2803 mutex_unlock(&c->tnc_mutex);
2807 val = interpret_user_input(u, count);
2811 if (dent == d->dfs_chk_gen)
2813 else if (dent == d->dfs_chk_index)
2815 else if (dent == d->dfs_chk_orph)
2817 else if (dent == d->dfs_chk_lprops)
2818 d->chk_lprops = val;
2819 else if (dent == d->dfs_chk_fs)
2821 else if (dent == d->dfs_tst_rcvry)
2823 else if (dent == d->dfs_ro_error)
2824 c->ro_error = !!val;
2831 static const struct file_operations dfs_fops = {
2832 .open = dfs_file_open,
2833 .read = dfs_file_read,
2834 .write = dfs_file_write,
2835 .owner = THIS_MODULE,
2836 .llseek = no_llseek,
2840 * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
2841 * @c: UBIFS file-system description object
2843 * This function creates all debugfs files for this instance of UBIFS. Returns
2844 * zero in case of success and a negative error code in case of failure.
2846 * Note, the only reason we have not merged this function with the
2847 * 'ubifs_debugging_init()' function is because it is better to initialize
2848 * debugfs interfaces at the very end of the mount process, and remove them at
2849 * the very beginning of the mount process.
2851 int dbg_debugfs_init_fs(struct ubifs_info *c)
2855 struct dentry *dent;
2856 struct ubifs_debug_info *d = c->dbg;
2858 if (!IS_ENABLED(CONFIG_DEBUG_FS))
2861 n = snprintf(d->dfs_dir_name, UBIFS_DFS_DIR_LEN + 1, UBIFS_DFS_DIR_NAME,
2862 c->vi.ubi_num, c->vi.vol_id);
2863 if (n == UBIFS_DFS_DIR_LEN) {
2864 /* The array size is too small */
2865 fname = UBIFS_DFS_DIR_NAME;
2866 dent = ERR_PTR(-EINVAL);
2870 fname = d->dfs_dir_name;
2871 dent = debugfs_create_dir(fname, dfs_rootdir);
2872 if (IS_ERR_OR_NULL(dent))
2876 fname = "dump_lprops";
2877 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2878 if (IS_ERR_OR_NULL(dent))
2880 d->dfs_dump_lprops = dent;
2882 fname = "dump_budg";
2883 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2884 if (IS_ERR_OR_NULL(dent))
2886 d->dfs_dump_budg = dent;
2889 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2890 if (IS_ERR_OR_NULL(dent))
2892 d->dfs_dump_tnc = dent;
2894 fname = "chk_general";
2895 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2897 if (IS_ERR_OR_NULL(dent))
2899 d->dfs_chk_gen = dent;
2901 fname = "chk_index";
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_index = dent;
2908 fname = "chk_orphans";
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_orph = dent;
2915 fname = "chk_lprops";
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_lprops = dent;
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_fs = dent;
2929 fname = "tst_recovery";
2930 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2932 if (IS_ERR_OR_NULL(dent))
2934 d->dfs_tst_rcvry = dent;
2937 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2939 if (IS_ERR_OR_NULL(dent))
2941 d->dfs_ro_error = dent;
2946 debugfs_remove_recursive(d->dfs_dir);
2948 err = dent ? PTR_ERR(dent) : -ENODEV;
2949 ubifs_err("cannot create \"%s\" debugfs file or directory, error %d\n",
2955 * dbg_debugfs_exit_fs - remove all debugfs files.
2956 * @c: UBIFS file-system description object
2958 void dbg_debugfs_exit_fs(struct ubifs_info *c)
2960 if (IS_ENABLED(CONFIG_DEBUG_FS))
2961 debugfs_remove_recursive(c->dbg->dfs_dir);
2964 struct ubifs_global_debug_info ubifs_dbg;
2966 static struct dentry *dfs_chk_gen;
2967 static struct dentry *dfs_chk_index;
2968 static struct dentry *dfs_chk_orph;
2969 static struct dentry *dfs_chk_lprops;
2970 static struct dentry *dfs_chk_fs;
2971 static struct dentry *dfs_tst_rcvry;
2973 static ssize_t dfs_global_file_read(struct file *file, char __user *u,
2974 size_t count, loff_t *ppos)
2976 struct dentry *dent = file->f_path.dentry;
2979 if (dent == dfs_chk_gen)
2980 val = ubifs_dbg.chk_gen;
2981 else if (dent == dfs_chk_index)
2982 val = ubifs_dbg.chk_index;
2983 else if (dent == dfs_chk_orph)
2984 val = ubifs_dbg.chk_orph;
2985 else if (dent == dfs_chk_lprops)
2986 val = ubifs_dbg.chk_lprops;
2987 else if (dent == dfs_chk_fs)
2988 val = ubifs_dbg.chk_fs;
2989 else if (dent == dfs_tst_rcvry)
2990 val = ubifs_dbg.tst_rcvry;
2994 return provide_user_output(val, u, count, ppos);
2997 static ssize_t dfs_global_file_write(struct file *file, const char __user *u,
2998 size_t count, loff_t *ppos)
3000 struct dentry *dent = file->f_path.dentry;
3003 val = interpret_user_input(u, count);
3007 if (dent == dfs_chk_gen)
3008 ubifs_dbg.chk_gen = val;
3009 else if (dent == dfs_chk_index)
3010 ubifs_dbg.chk_index = val;
3011 else if (dent == dfs_chk_orph)
3012 ubifs_dbg.chk_orph = val;
3013 else if (dent == dfs_chk_lprops)
3014 ubifs_dbg.chk_lprops = val;
3015 else if (dent == dfs_chk_fs)
3016 ubifs_dbg.chk_fs = val;
3017 else if (dent == dfs_tst_rcvry)
3018 ubifs_dbg.tst_rcvry = val;
3025 static const struct file_operations dfs_global_fops = {
3026 .read = dfs_global_file_read,
3027 .write = dfs_global_file_write,
3028 .owner = THIS_MODULE,
3029 .llseek = no_llseek,
3033 * dbg_debugfs_init - initialize debugfs file-system.
3035 * UBIFS uses debugfs file-system to expose various debugging knobs to
3036 * user-space. This function creates "ubifs" directory in the debugfs
3037 * file-system. Returns zero in case of success and a negative error code in
3040 int dbg_debugfs_init(void)
3044 struct dentry *dent;
3046 if (!IS_ENABLED(CONFIG_DEBUG_FS))
3050 dent = debugfs_create_dir(fname, NULL);
3051 if (IS_ERR_OR_NULL(dent))
3055 fname = "chk_general";
3056 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3058 if (IS_ERR_OR_NULL(dent))
3062 fname = "chk_index";
3063 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3065 if (IS_ERR_OR_NULL(dent))
3067 dfs_chk_index = dent;
3069 fname = "chk_orphans";
3070 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3072 if (IS_ERR_OR_NULL(dent))
3074 dfs_chk_orph = dent;
3076 fname = "chk_lprops";
3077 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3079 if (IS_ERR_OR_NULL(dent))
3081 dfs_chk_lprops = dent;
3084 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3086 if (IS_ERR_OR_NULL(dent))
3090 fname = "tst_recovery";
3091 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3093 if (IS_ERR_OR_NULL(dent))
3095 dfs_tst_rcvry = dent;
3100 debugfs_remove_recursive(dfs_rootdir);
3102 err = dent ? PTR_ERR(dent) : -ENODEV;
3103 ubifs_err("cannot create \"%s\" debugfs file or directory, error %d\n",
3109 * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
3111 void dbg_debugfs_exit(void)
3113 if (IS_ENABLED(CONFIG_DEBUG_FS))
3114 debugfs_remove_recursive(dfs_rootdir);
3118 * ubifs_debugging_init - initialize UBIFS debugging.
3119 * @c: UBIFS file-system description object
3121 * This function initializes debugging-related data for the file system.
3122 * Returns zero in case of success and a negative error code in case of
3125 int ubifs_debugging_init(struct ubifs_info *c)
3127 c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
3135 * ubifs_debugging_exit - free debugging data.
3136 * @c: UBIFS file-system description object
3138 void ubifs_debugging_exit(struct ubifs_info *c)