2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
24 * This file implements most of the debugging stuff which is compiled in only
25 * when it is enabled. But some debugging check functions are implemented in
26 * corresponding subsystem, just because they are closely related and utilize
27 * various local functions of those subsystems.
30 #include <linux/module.h>
31 #include <linux/debugfs.h>
32 #include <linux/math64.h>
33 #include <linux/uaccess.h>
34 #include <linux/random.h>
35 #include <linux/ctype.h>
38 static DEFINE_SPINLOCK(dbg_lock);
40 static const char *get_key_fmt(int fmt)
43 case UBIFS_SIMPLE_KEY_FMT:
46 return "unknown/invalid format";
50 static const char *get_key_hash(int hash)
53 case UBIFS_KEY_HASH_R5:
55 case UBIFS_KEY_HASH_TEST:
58 return "unknown/invalid name hash";
62 static const char *get_key_type(int type)
76 return "unknown/invalid key";
80 static const char *get_dent_type(int type)
93 case UBIFS_ITYPE_FIFO:
95 case UBIFS_ITYPE_SOCK:
98 return "unknown/invalid type";
102 const char *dbg_snprintf_key(const struct ubifs_info *c,
103 const union ubifs_key *key, char *buffer, int len)
106 int type = key_type(c, key);
108 if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
111 len -= snprintf(p, len, "(%lu, %s)",
112 (unsigned long)key_inum(c, key),
117 len -= snprintf(p, len, "(%lu, %s, %#08x)",
118 (unsigned long)key_inum(c, key),
119 get_key_type(type), key_hash(c, key));
122 len -= snprintf(p, len, "(%lu, %s, %u)",
123 (unsigned long)key_inum(c, key),
124 get_key_type(type), key_block(c, key));
127 len -= snprintf(p, len, "(%lu, %s)",
128 (unsigned long)key_inum(c, key),
132 len -= snprintf(p, len, "(bad key type: %#08x, %#08x)",
133 key->u32[0], key->u32[1]);
136 len -= snprintf(p, len, "bad key format %d", c->key_fmt);
137 ubifs_assert(len > 0);
141 const char *dbg_ntype(int type)
145 return "padding node";
147 return "superblock node";
149 return "master node";
151 return "reference node";
154 case UBIFS_DENT_NODE:
155 return "direntry node";
156 case UBIFS_XENT_NODE:
157 return "xentry node";
158 case UBIFS_DATA_NODE:
160 case UBIFS_TRUN_NODE:
161 return "truncate node";
163 return "indexing node";
165 return "commit start node";
166 case UBIFS_ORPH_NODE:
167 return "orphan node";
169 return "unknown node";
173 static const char *dbg_gtype(int type)
176 case UBIFS_NO_NODE_GROUP:
177 return "no node group";
178 case UBIFS_IN_NODE_GROUP:
179 return "in node group";
180 case UBIFS_LAST_OF_NODE_GROUP:
181 return "last of node group";
187 const char *dbg_cstate(int cmt_state)
191 return "commit resting";
192 case COMMIT_BACKGROUND:
193 return "background commit requested";
194 case COMMIT_REQUIRED:
195 return "commit required";
196 case COMMIT_RUNNING_BACKGROUND:
197 return "BACKGROUND commit running";
198 case COMMIT_RUNNING_REQUIRED:
199 return "commit running and required";
201 return "broken commit";
203 return "unknown commit state";
207 const char *dbg_jhead(int jhead)
217 return "unknown journal head";
221 static void dump_ch(const struct ubifs_ch *ch)
223 pr_err("\tmagic %#x\n", le32_to_cpu(ch->magic));
224 pr_err("\tcrc %#x\n", le32_to_cpu(ch->crc));
225 pr_err("\tnode_type %d (%s)\n", ch->node_type,
226 dbg_ntype(ch->node_type));
227 pr_err("\tgroup_type %d (%s)\n", ch->group_type,
228 dbg_gtype(ch->group_type));
229 pr_err("\tsqnum %llu\n",
230 (unsigned long long)le64_to_cpu(ch->sqnum));
231 pr_err("\tlen %u\n", le32_to_cpu(ch->len));
234 void ubifs_dump_inode(struct ubifs_info *c, const struct inode *inode)
236 const struct ubifs_inode *ui = ubifs_inode(inode);
237 struct fscrypt_name nm = {0};
239 struct ubifs_dent_node *dent, *pdent = NULL;
242 pr_err("Dump in-memory inode:");
243 pr_err("\tinode %lu\n", inode->i_ino);
244 pr_err("\tsize %llu\n",
245 (unsigned long long)i_size_read(inode));
246 pr_err("\tnlink %u\n", inode->i_nlink);
247 pr_err("\tuid %u\n", (unsigned int)i_uid_read(inode));
248 pr_err("\tgid %u\n", (unsigned int)i_gid_read(inode));
249 pr_err("\tatime %u.%u\n",
250 (unsigned int)inode->i_atime.tv_sec,
251 (unsigned int)inode->i_atime.tv_nsec);
252 pr_err("\tmtime %u.%u\n",
253 (unsigned int)inode->i_mtime.tv_sec,
254 (unsigned int)inode->i_mtime.tv_nsec);
255 pr_err("\tctime %u.%u\n",
256 (unsigned int)inode->i_ctime.tv_sec,
257 (unsigned int)inode->i_ctime.tv_nsec);
258 pr_err("\tcreat_sqnum %llu\n", ui->creat_sqnum);
259 pr_err("\txattr_size %u\n", ui->xattr_size);
260 pr_err("\txattr_cnt %u\n", ui->xattr_cnt);
261 pr_err("\txattr_names %u\n", ui->xattr_names);
262 pr_err("\tdirty %u\n", ui->dirty);
263 pr_err("\txattr %u\n", ui->xattr);
264 pr_err("\tbulk_read %u\n", ui->bulk_read);
265 pr_err("\tsynced_i_size %llu\n",
266 (unsigned long long)ui->synced_i_size);
267 pr_err("\tui_size %llu\n",
268 (unsigned long long)ui->ui_size);
269 pr_err("\tflags %d\n", ui->flags);
270 pr_err("\tcompr_type %d\n", ui->compr_type);
271 pr_err("\tlast_page_read %lu\n", ui->last_page_read);
272 pr_err("\tread_in_a_row %lu\n", ui->read_in_a_row);
273 pr_err("\tdata_len %d\n", ui->data_len);
275 if (!S_ISDIR(inode->i_mode))
278 pr_err("List of directory entries:\n");
279 ubifs_assert(!mutex_is_locked(&c->tnc_mutex));
281 lowest_dent_key(c, &key, inode->i_ino);
283 dent = ubifs_tnc_next_ent(c, &key, &nm);
285 if (PTR_ERR(dent) != -ENOENT)
286 pr_err("error %ld\n", PTR_ERR(dent));
290 pr_err("\t%d: inode %llu, type %s, len %d\n",
291 count++, (unsigned long long) le64_to_cpu(dent->inum),
292 get_dent_type(dent->type),
293 le16_to_cpu(dent->nlen));
295 fname_name(&nm) = dent->name;
296 fname_len(&nm) = le16_to_cpu(dent->nlen);
299 key_read(c, &dent->key, &key);
304 void ubifs_dump_node(const struct ubifs_info *c, const void *node)
308 const struct ubifs_ch *ch = node;
309 char key_buf[DBG_KEY_BUF_LEN];
311 /* If the magic is incorrect, just hexdump the first bytes */
312 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
313 pr_err("Not a node, first %zu bytes:", UBIFS_CH_SZ);
314 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 32, 1,
315 (void *)node, UBIFS_CH_SZ, 1);
319 spin_lock(&dbg_lock);
322 switch (ch->node_type) {
325 const struct ubifs_pad_node *pad = node;
327 pr_err("\tpad_len %u\n", le32_to_cpu(pad->pad_len));
332 const struct ubifs_sb_node *sup = node;
333 unsigned int sup_flags = le32_to_cpu(sup->flags);
335 pr_err("\tkey_hash %d (%s)\n",
336 (int)sup->key_hash, get_key_hash(sup->key_hash));
337 pr_err("\tkey_fmt %d (%s)\n",
338 (int)sup->key_fmt, get_key_fmt(sup->key_fmt));
339 pr_err("\tflags %#x\n", sup_flags);
340 pr_err("\tbig_lpt %u\n",
341 !!(sup_flags & UBIFS_FLG_BIGLPT));
342 pr_err("\tspace_fixup %u\n",
343 !!(sup_flags & UBIFS_FLG_SPACE_FIXUP));
344 pr_err("\tmin_io_size %u\n", le32_to_cpu(sup->min_io_size));
345 pr_err("\tleb_size %u\n", le32_to_cpu(sup->leb_size));
346 pr_err("\tleb_cnt %u\n", le32_to_cpu(sup->leb_cnt));
347 pr_err("\tmax_leb_cnt %u\n", le32_to_cpu(sup->max_leb_cnt));
348 pr_err("\tmax_bud_bytes %llu\n",
349 (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
350 pr_err("\tlog_lebs %u\n", le32_to_cpu(sup->log_lebs));
351 pr_err("\tlpt_lebs %u\n", le32_to_cpu(sup->lpt_lebs));
352 pr_err("\torph_lebs %u\n", le32_to_cpu(sup->orph_lebs));
353 pr_err("\tjhead_cnt %u\n", le32_to_cpu(sup->jhead_cnt));
354 pr_err("\tfanout %u\n", le32_to_cpu(sup->fanout));
355 pr_err("\tlsave_cnt %u\n", le32_to_cpu(sup->lsave_cnt));
356 pr_err("\tdefault_compr %u\n",
357 (int)le16_to_cpu(sup->default_compr));
358 pr_err("\trp_size %llu\n",
359 (unsigned long long)le64_to_cpu(sup->rp_size));
360 pr_err("\trp_uid %u\n", le32_to_cpu(sup->rp_uid));
361 pr_err("\trp_gid %u\n", le32_to_cpu(sup->rp_gid));
362 pr_err("\tfmt_version %u\n", le32_to_cpu(sup->fmt_version));
363 pr_err("\ttime_gran %u\n", le32_to_cpu(sup->time_gran));
364 pr_err("\tUUID %pUB\n", sup->uuid);
369 const struct ubifs_mst_node *mst = node;
371 pr_err("\thighest_inum %llu\n",
372 (unsigned long long)le64_to_cpu(mst->highest_inum));
373 pr_err("\tcommit number %llu\n",
374 (unsigned long long)le64_to_cpu(mst->cmt_no));
375 pr_err("\tflags %#x\n", le32_to_cpu(mst->flags));
376 pr_err("\tlog_lnum %u\n", le32_to_cpu(mst->log_lnum));
377 pr_err("\troot_lnum %u\n", le32_to_cpu(mst->root_lnum));
378 pr_err("\troot_offs %u\n", le32_to_cpu(mst->root_offs));
379 pr_err("\troot_len %u\n", le32_to_cpu(mst->root_len));
380 pr_err("\tgc_lnum %u\n", le32_to_cpu(mst->gc_lnum));
381 pr_err("\tihead_lnum %u\n", le32_to_cpu(mst->ihead_lnum));
382 pr_err("\tihead_offs %u\n", le32_to_cpu(mst->ihead_offs));
383 pr_err("\tindex_size %llu\n",
384 (unsigned long long)le64_to_cpu(mst->index_size));
385 pr_err("\tlpt_lnum %u\n", le32_to_cpu(mst->lpt_lnum));
386 pr_err("\tlpt_offs %u\n", le32_to_cpu(mst->lpt_offs));
387 pr_err("\tnhead_lnum %u\n", le32_to_cpu(mst->nhead_lnum));
388 pr_err("\tnhead_offs %u\n", le32_to_cpu(mst->nhead_offs));
389 pr_err("\tltab_lnum %u\n", le32_to_cpu(mst->ltab_lnum));
390 pr_err("\tltab_offs %u\n", le32_to_cpu(mst->ltab_offs));
391 pr_err("\tlsave_lnum %u\n", le32_to_cpu(mst->lsave_lnum));
392 pr_err("\tlsave_offs %u\n", le32_to_cpu(mst->lsave_offs));
393 pr_err("\tlscan_lnum %u\n", le32_to_cpu(mst->lscan_lnum));
394 pr_err("\tleb_cnt %u\n", le32_to_cpu(mst->leb_cnt));
395 pr_err("\tempty_lebs %u\n", le32_to_cpu(mst->empty_lebs));
396 pr_err("\tidx_lebs %u\n", le32_to_cpu(mst->idx_lebs));
397 pr_err("\ttotal_free %llu\n",
398 (unsigned long long)le64_to_cpu(mst->total_free));
399 pr_err("\ttotal_dirty %llu\n",
400 (unsigned long long)le64_to_cpu(mst->total_dirty));
401 pr_err("\ttotal_used %llu\n",
402 (unsigned long long)le64_to_cpu(mst->total_used));
403 pr_err("\ttotal_dead %llu\n",
404 (unsigned long long)le64_to_cpu(mst->total_dead));
405 pr_err("\ttotal_dark %llu\n",
406 (unsigned long long)le64_to_cpu(mst->total_dark));
411 const struct ubifs_ref_node *ref = node;
413 pr_err("\tlnum %u\n", le32_to_cpu(ref->lnum));
414 pr_err("\toffs %u\n", le32_to_cpu(ref->offs));
415 pr_err("\tjhead %u\n", le32_to_cpu(ref->jhead));
420 const struct ubifs_ino_node *ino = node;
422 key_read(c, &ino->key, &key);
424 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
425 pr_err("\tcreat_sqnum %llu\n",
426 (unsigned long long)le64_to_cpu(ino->creat_sqnum));
427 pr_err("\tsize %llu\n",
428 (unsigned long long)le64_to_cpu(ino->size));
429 pr_err("\tnlink %u\n", le32_to_cpu(ino->nlink));
430 pr_err("\tatime %lld.%u\n",
431 (long long)le64_to_cpu(ino->atime_sec),
432 le32_to_cpu(ino->atime_nsec));
433 pr_err("\tmtime %lld.%u\n",
434 (long long)le64_to_cpu(ino->mtime_sec),
435 le32_to_cpu(ino->mtime_nsec));
436 pr_err("\tctime %lld.%u\n",
437 (long long)le64_to_cpu(ino->ctime_sec),
438 le32_to_cpu(ino->ctime_nsec));
439 pr_err("\tuid %u\n", le32_to_cpu(ino->uid));
440 pr_err("\tgid %u\n", le32_to_cpu(ino->gid));
441 pr_err("\tmode %u\n", le32_to_cpu(ino->mode));
442 pr_err("\tflags %#x\n", le32_to_cpu(ino->flags));
443 pr_err("\txattr_cnt %u\n", le32_to_cpu(ino->xattr_cnt));
444 pr_err("\txattr_size %u\n", le32_to_cpu(ino->xattr_size));
445 pr_err("\txattr_names %u\n", le32_to_cpu(ino->xattr_names));
446 pr_err("\tcompr_type %#x\n",
447 (int)le16_to_cpu(ino->compr_type));
448 pr_err("\tdata len %u\n", le32_to_cpu(ino->data_len));
451 case UBIFS_DENT_NODE:
452 case UBIFS_XENT_NODE:
454 const struct ubifs_dent_node *dent = node;
455 int nlen = le16_to_cpu(dent->nlen);
457 key_read(c, &dent->key, &key);
459 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
460 pr_err("\tinum %llu\n",
461 (unsigned long long)le64_to_cpu(dent->inum));
462 pr_err("\ttype %d\n", (int)dent->type);
463 pr_err("\tnlen %d\n", nlen);
466 if (nlen > UBIFS_MAX_NLEN)
467 pr_err("(bad name length, not printing, bad or corrupted node)");
469 for (i = 0; i < nlen && dent->name[i]; i++)
470 pr_cont("%c", isprint(dent->name[i]) ?
471 dent->name[i] : '?');
477 case UBIFS_DATA_NODE:
479 const struct ubifs_data_node *dn = node;
480 int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
482 key_read(c, &dn->key, &key);
484 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
485 pr_err("\tsize %u\n", le32_to_cpu(dn->size));
486 pr_err("\tcompr_typ %d\n",
487 (int)le16_to_cpu(dn->compr_type));
488 pr_err("\tdata size %d\n", dlen);
490 print_hex_dump(KERN_ERR, "\t", DUMP_PREFIX_OFFSET, 32, 1,
491 (void *)&dn->data, dlen, 0);
494 case UBIFS_TRUN_NODE:
496 const struct ubifs_trun_node *trun = node;
498 pr_err("\tinum %u\n", le32_to_cpu(trun->inum));
499 pr_err("\told_size %llu\n",
500 (unsigned long long)le64_to_cpu(trun->old_size));
501 pr_err("\tnew_size %llu\n",
502 (unsigned long long)le64_to_cpu(trun->new_size));
507 const struct ubifs_idx_node *idx = node;
509 n = le16_to_cpu(idx->child_cnt);
510 pr_err("\tchild_cnt %d\n", n);
511 pr_err("\tlevel %d\n", (int)le16_to_cpu(idx->level));
512 pr_err("\tBranches:\n");
514 for (i = 0; i < n && i < c->fanout - 1; i++) {
515 const struct ubifs_branch *br;
517 br = ubifs_idx_branch(c, idx, i);
518 key_read(c, &br->key, &key);
519 pr_err("\t%d: LEB %d:%d len %d key %s\n",
520 i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
521 le32_to_cpu(br->len),
522 dbg_snprintf_key(c, &key, key_buf,
529 case UBIFS_ORPH_NODE:
531 const struct ubifs_orph_node *orph = node;
533 pr_err("\tcommit number %llu\n",
535 le64_to_cpu(orph->cmt_no) & LLONG_MAX);
536 pr_err("\tlast node flag %llu\n",
537 (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
538 n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
539 pr_err("\t%d orphan inode numbers:\n", n);
540 for (i = 0; i < n; i++)
541 pr_err("\t ino %llu\n",
542 (unsigned long long)le64_to_cpu(orph->inos[i]));
546 pr_err("node type %d was not recognized\n",
549 spin_unlock(&dbg_lock);
552 void ubifs_dump_budget_req(const struct ubifs_budget_req *req)
554 spin_lock(&dbg_lock);
555 pr_err("Budgeting request: new_ino %d, dirtied_ino %d\n",
556 req->new_ino, req->dirtied_ino);
557 pr_err("\tnew_ino_d %d, dirtied_ino_d %d\n",
558 req->new_ino_d, req->dirtied_ino_d);
559 pr_err("\tnew_page %d, dirtied_page %d\n",
560 req->new_page, req->dirtied_page);
561 pr_err("\tnew_dent %d, mod_dent %d\n",
562 req->new_dent, req->mod_dent);
563 pr_err("\tidx_growth %d\n", req->idx_growth);
564 pr_err("\tdata_growth %d dd_growth %d\n",
565 req->data_growth, req->dd_growth);
566 spin_unlock(&dbg_lock);
569 void ubifs_dump_lstats(const struct ubifs_lp_stats *lst)
571 spin_lock(&dbg_lock);
572 pr_err("(pid %d) Lprops statistics: empty_lebs %d, idx_lebs %d\n",
573 current->pid, lst->empty_lebs, lst->idx_lebs);
574 pr_err("\ttaken_empty_lebs %d, total_free %lld, total_dirty %lld\n",
575 lst->taken_empty_lebs, lst->total_free, lst->total_dirty);
576 pr_err("\ttotal_used %lld, total_dark %lld, total_dead %lld\n",
577 lst->total_used, lst->total_dark, lst->total_dead);
578 spin_unlock(&dbg_lock);
581 void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)
585 struct ubifs_bud *bud;
586 struct ubifs_gced_idx_leb *idx_gc;
587 long long available, outstanding, free;
589 spin_lock(&c->space_lock);
590 spin_lock(&dbg_lock);
591 pr_err("(pid %d) Budgeting info: data budget sum %lld, total budget sum %lld\n",
592 current->pid, bi->data_growth + bi->dd_growth,
593 bi->data_growth + bi->dd_growth + bi->idx_growth);
594 pr_err("\tbudg_data_growth %lld, budg_dd_growth %lld, budg_idx_growth %lld\n",
595 bi->data_growth, bi->dd_growth, bi->idx_growth);
596 pr_err("\tmin_idx_lebs %d, old_idx_sz %llu, uncommitted_idx %lld\n",
597 bi->min_idx_lebs, bi->old_idx_sz, bi->uncommitted_idx);
598 pr_err("\tpage_budget %d, inode_budget %d, dent_budget %d\n",
599 bi->page_budget, bi->inode_budget, bi->dent_budget);
600 pr_err("\tnospace %u, nospace_rp %u\n", bi->nospace, bi->nospace_rp);
601 pr_err("\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
602 c->dark_wm, c->dead_wm, c->max_idx_node_sz);
606 * If we are dumping saved budgeting data, do not print
607 * additional information which is about the current state, not
608 * the old one which corresponded to the saved budgeting data.
612 pr_err("\tfreeable_cnt %d, calc_idx_sz %lld, idx_gc_cnt %d\n",
613 c->freeable_cnt, c->calc_idx_sz, c->idx_gc_cnt);
614 pr_err("\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, clean_zn_cnt %ld\n",
615 atomic_long_read(&c->dirty_pg_cnt),
616 atomic_long_read(&c->dirty_zn_cnt),
617 atomic_long_read(&c->clean_zn_cnt));
618 pr_err("\tgc_lnum %d, ihead_lnum %d\n", c->gc_lnum, c->ihead_lnum);
620 /* If we are in R/O mode, journal heads do not exist */
622 for (i = 0; i < c->jhead_cnt; i++)
623 pr_err("\tjhead %s\t LEB %d\n",
624 dbg_jhead(c->jheads[i].wbuf.jhead),
625 c->jheads[i].wbuf.lnum);
626 for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
627 bud = rb_entry(rb, struct ubifs_bud, rb);
628 pr_err("\tbud LEB %d\n", bud->lnum);
630 list_for_each_entry(bud, &c->old_buds, list)
631 pr_err("\told bud LEB %d\n", bud->lnum);
632 list_for_each_entry(idx_gc, &c->idx_gc, list)
633 pr_err("\tGC'ed idx LEB %d unmap %d\n",
634 idx_gc->lnum, idx_gc->unmap);
635 pr_err("\tcommit state %d\n", c->cmt_state);
637 /* Print budgeting predictions */
638 available = ubifs_calc_available(c, c->bi.min_idx_lebs);
639 outstanding = c->bi.data_growth + c->bi.dd_growth;
640 free = ubifs_get_free_space_nolock(c);
641 pr_err("Budgeting predictions:\n");
642 pr_err("\tavailable: %lld, outstanding %lld, free %lld\n",
643 available, outstanding, free);
645 spin_unlock(&dbg_lock);
646 spin_unlock(&c->space_lock);
649 void ubifs_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
651 int i, spc, dark = 0, dead = 0;
653 struct ubifs_bud *bud;
655 spc = lp->free + lp->dirty;
656 if (spc < c->dead_wm)
659 dark = ubifs_calc_dark(c, spc);
661 if (lp->flags & LPROPS_INDEX)
662 pr_err("LEB %-7d free %-8d dirty %-8d used %-8d free + dirty %-8d flags %#x (",
663 lp->lnum, lp->free, lp->dirty, c->leb_size - spc, spc,
666 pr_err("LEB %-7d free %-8d dirty %-8d used %-8d free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d flags %#-4x (",
667 lp->lnum, lp->free, lp->dirty, c->leb_size - spc, spc,
668 dark, dead, (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags);
670 if (lp->flags & LPROPS_TAKEN) {
671 if (lp->flags & LPROPS_INDEX)
672 pr_cont("index, taken");
678 if (lp->flags & LPROPS_INDEX) {
679 switch (lp->flags & LPROPS_CAT_MASK) {
680 case LPROPS_DIRTY_IDX:
683 case LPROPS_FRDI_IDX:
684 s = "freeable index";
690 switch (lp->flags & LPROPS_CAT_MASK) {
692 s = "not categorized";
703 case LPROPS_FREEABLE:
714 for (rb = rb_first((struct rb_root *)&c->buds); rb; rb = rb_next(rb)) {
715 bud = rb_entry(rb, struct ubifs_bud, rb);
716 if (bud->lnum == lp->lnum) {
718 for (i = 0; i < c->jhead_cnt; i++) {
720 * Note, if we are in R/O mode or in the middle
721 * of mounting/re-mounting, the write-buffers do
725 lp->lnum == c->jheads[i].wbuf.lnum) {
726 pr_cont(", jhead %s", dbg_jhead(i));
731 pr_cont(", bud of jhead %s",
732 dbg_jhead(bud->jhead));
735 if (lp->lnum == c->gc_lnum)
740 void ubifs_dump_lprops(struct ubifs_info *c)
743 struct ubifs_lprops lp;
744 struct ubifs_lp_stats lst;
746 pr_err("(pid %d) start dumping LEB properties\n", current->pid);
747 ubifs_get_lp_stats(c, &lst);
748 ubifs_dump_lstats(&lst);
750 for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
751 err = ubifs_read_one_lp(c, lnum, &lp);
753 ubifs_err(c, "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(c, "cannot allocate memory for dumping LEB %d", lnum);
830 sleb = ubifs_scan(c, lnum, 0, buf, 0);
832 ubifs_err(c, "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);
968 * dbg_save_space_info - save information about flash space.
969 * @c: UBIFS file-system description object
971 * This function saves information about UBIFS free space, dirty space, etc, in
972 * order to check it later.
974 void dbg_save_space_info(struct ubifs_info *c)
976 struct ubifs_debug_info *d = c->dbg;
979 spin_lock(&c->space_lock);
980 memcpy(&d->saved_lst, &c->lst, sizeof(struct ubifs_lp_stats));
981 memcpy(&d->saved_bi, &c->bi, sizeof(struct ubifs_budg_info));
982 d->saved_idx_gc_cnt = c->idx_gc_cnt;
985 * We use a dirty hack here and zero out @c->freeable_cnt, because it
986 * affects the free space calculations, and UBIFS might not know about
987 * all freeable eraseblocks. Indeed, we know about freeable eraseblocks
988 * only when we read their lprops, and we do this only lazily, upon the
989 * need. So at any given point of time @c->freeable_cnt might be not
992 * Just one example about the issue we hit when we did not zero
994 * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the
995 * amount of free space in @d->saved_free
996 * 2. We re-mount R/W, which makes UBIFS to read the "lsave"
997 * information from flash, where we cache LEBs from various
998 * categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()'
999 * -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()'
1000 * -> 'ubifs_get_pnode()' -> 'update_cats()'
1001 * -> 'ubifs_add_to_cat()').
1002 * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt
1004 * 4. We calculate the amount of free space when the re-mount is
1005 * finished in 'dbg_check_space_info()' and it does not match
1008 freeable_cnt = c->freeable_cnt;
1009 c->freeable_cnt = 0;
1010 d->saved_free = ubifs_get_free_space_nolock(c);
1011 c->freeable_cnt = freeable_cnt;
1012 spin_unlock(&c->space_lock);
1016 * dbg_check_space_info - check flash space information.
1017 * @c: UBIFS file-system description object
1019 * This function compares current flash space information with the information
1020 * which was saved when the 'dbg_save_space_info()' function was called.
1021 * Returns zero if the information has not changed, and %-EINVAL it it has
1024 int dbg_check_space_info(struct ubifs_info *c)
1026 struct ubifs_debug_info *d = c->dbg;
1027 struct ubifs_lp_stats lst;
1031 spin_lock(&c->space_lock);
1032 freeable_cnt = c->freeable_cnt;
1033 c->freeable_cnt = 0;
1034 free = ubifs_get_free_space_nolock(c);
1035 c->freeable_cnt = freeable_cnt;
1036 spin_unlock(&c->space_lock);
1038 if (free != d->saved_free) {
1039 ubifs_err(c, "free space changed from %lld to %lld",
1040 d->saved_free, free);
1047 ubifs_msg(c, "saved lprops statistics dump");
1048 ubifs_dump_lstats(&d->saved_lst);
1049 ubifs_msg(c, "saved budgeting info dump");
1050 ubifs_dump_budg(c, &d->saved_bi);
1051 ubifs_msg(c, "saved idx_gc_cnt %d", d->saved_idx_gc_cnt);
1052 ubifs_msg(c, "current lprops statistics dump");
1053 ubifs_get_lp_stats(c, &lst);
1054 ubifs_dump_lstats(&lst);
1055 ubifs_msg(c, "current budgeting info dump");
1056 ubifs_dump_budg(c, &c->bi);
1062 * dbg_check_synced_i_size - check synchronized inode size.
1063 * @c: UBIFS file-system description object
1064 * @inode: inode to check
1066 * If inode is clean, synchronized inode size has to be equivalent to current
1067 * inode size. This function has to be called only for locked inodes (@i_mutex
1068 * has to be locked). Returns %0 if synchronized inode size if correct, and
1071 int dbg_check_synced_i_size(const struct ubifs_info *c, struct inode *inode)
1074 struct ubifs_inode *ui = ubifs_inode(inode);
1076 if (!dbg_is_chk_gen(c))
1078 if (!S_ISREG(inode->i_mode))
1081 mutex_lock(&ui->ui_mutex);
1082 spin_lock(&ui->ui_lock);
1083 if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
1084 ubifs_err(c, "ui_size is %lld, synced_i_size is %lld, but inode is clean",
1085 ui->ui_size, ui->synced_i_size);
1086 ubifs_err(c, "i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
1087 inode->i_mode, i_size_read(inode));
1091 spin_unlock(&ui->ui_lock);
1092 mutex_unlock(&ui->ui_mutex);
1097 * dbg_check_dir - check directory inode size and link count.
1098 * @c: UBIFS file-system description object
1099 * @dir: the directory to calculate size for
1100 * @size: the result is returned here
1102 * This function makes sure that directory size and link count are correct.
1103 * Returns zero in case of success and a negative error code in case of
1106 * Note, it is good idea to make sure the @dir->i_mutex is locked before
1107 * calling this function.
1109 int dbg_check_dir(struct ubifs_info *c, const struct inode *dir)
1111 unsigned int nlink = 2;
1112 union ubifs_key key;
1113 struct ubifs_dent_node *dent, *pdent = NULL;
1114 struct fscrypt_name nm = {0};
1115 loff_t size = UBIFS_INO_NODE_SZ;
1117 if (!dbg_is_chk_gen(c))
1120 if (!S_ISDIR(dir->i_mode))
1123 lowest_dent_key(c, &key, dir->i_ino);
1127 dent = ubifs_tnc_next_ent(c, &key, &nm);
1129 err = PTR_ERR(dent);
1136 fname_name(&nm) = dent->name;
1137 fname_len(&nm) = le16_to_cpu(dent->nlen);
1138 size += CALC_DENT_SIZE(fname_len(&nm));
1139 if (dent->type == UBIFS_ITYPE_DIR)
1143 key_read(c, &dent->key, &key);
1147 if (i_size_read(dir) != size) {
1148 ubifs_err(c, "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(c, "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(c, "1st entry at %d:%d has key %s", zbr1->lnum,
1216 zbr1->offs, dbg_snprintf_key(c, &key, key_buf,
1218 ubifs_err(c, "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(c, "2nd entry at %d:%d has key %s", zbr1->lnum,
1228 zbr1->offs, dbg_snprintf_key(c, &key, key_buf,
1230 ubifs_err(c, "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(c, "2 xent/dent nodes with the same name");
1248 ubifs_err(c, "bad order of colliding key %s",
1249 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
1251 ubifs_msg(c, "first node at %d:%d\n", zbr1->lnum, zbr1->offs);
1252 ubifs_dump_node(c, dent1);
1253 ubifs_msg(c, "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(c, "failed, error %d", err);
1456 ubifs_msg(c, "dump of the znode");
1457 ubifs_dump_znode(c, znode);
1459 ubifs_msg(c, "dump of the parent znode");
1460 ubifs_dump_znode(c, zp);
1467 * dbg_check_tnc - check TNC tree.
1468 * @c: UBIFS file-system description object
1469 * @extra: do extra checks that are possible at start commit
1471 * This function traverses whole TNC tree and checks every znode. Returns zero
1472 * if everything is all right and %-EINVAL if something is wrong with TNC.
1474 int dbg_check_tnc(struct ubifs_info *c, int extra)
1476 struct ubifs_znode *znode;
1477 long clean_cnt = 0, dirty_cnt = 0;
1480 if (!dbg_is_chk_index(c))
1483 ubifs_assert(mutex_is_locked(&c->tnc_mutex));
1484 if (!c->zroot.znode)
1487 znode = ubifs_tnc_postorder_first(c->zroot.znode);
1489 struct ubifs_znode *prev;
1490 struct ubifs_zbranch *zbr;
1495 zbr = &znode->parent->zbranch[znode->iip];
1497 err = dbg_check_znode(c, zbr);
1502 if (ubifs_zn_dirty(znode))
1509 znode = ubifs_tnc_postorder_next(znode);
1514 * If the last key of this znode is equivalent to the first key
1515 * of the next znode (collision), then check order of the keys.
1517 last = prev->child_cnt - 1;
1518 if (prev->level == 0 && znode->level == 0 && !c->replaying &&
1519 !keys_cmp(c, &prev->zbranch[last].key,
1520 &znode->zbranch[0].key)) {
1521 err = dbg_check_key_order(c, &prev->zbranch[last],
1522 &znode->zbranch[0]);
1526 ubifs_msg(c, "first znode");
1527 ubifs_dump_znode(c, prev);
1528 ubifs_msg(c, "second znode");
1529 ubifs_dump_znode(c, znode);
1536 if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
1537 ubifs_err(c, "incorrect clean_zn_cnt %ld, calculated %ld",
1538 atomic_long_read(&c->clean_zn_cnt),
1542 if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
1543 ubifs_err(c, "incorrect dirty_zn_cnt %ld, calculated %ld",
1544 atomic_long_read(&c->dirty_zn_cnt),
1554 * dbg_walk_index - walk the on-flash index.
1555 * @c: UBIFS file-system description object
1556 * @leaf_cb: called for each leaf node
1557 * @znode_cb: called for each indexing node
1558 * @priv: private data which is passed to callbacks
1560 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1561 * node and @znode_cb for each indexing node. Returns zero in case of success
1562 * and a negative error code in case of failure.
1564 * It would be better if this function removed every znode it pulled to into
1565 * the TNC, so that the behavior more closely matched the non-debugging
1568 int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1569 dbg_znode_callback znode_cb, void *priv)
1572 struct ubifs_zbranch *zbr;
1573 struct ubifs_znode *znode, *child;
1575 mutex_lock(&c->tnc_mutex);
1576 /* If the root indexing node is not in TNC - pull it */
1577 if (!c->zroot.znode) {
1578 c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1579 if (IS_ERR(c->zroot.znode)) {
1580 err = PTR_ERR(c->zroot.znode);
1581 c->zroot.znode = NULL;
1587 * We are going to traverse the indexing tree in the postorder manner.
1588 * Go down and find the leftmost indexing node where we are going to
1591 znode = c->zroot.znode;
1592 while (znode->level > 0) {
1593 zbr = &znode->zbranch[0];
1596 child = ubifs_load_znode(c, zbr, znode, 0);
1597 if (IS_ERR(child)) {
1598 err = PTR_ERR(child);
1607 /* Iterate over all indexing nodes */
1614 err = znode_cb(c, znode, priv);
1616 ubifs_err(c, "znode checking function returned error %d",
1618 ubifs_dump_znode(c, znode);
1622 if (leaf_cb && znode->level == 0) {
1623 for (idx = 0; idx < znode->child_cnt; idx++) {
1624 zbr = &znode->zbranch[idx];
1625 err = leaf_cb(c, zbr, priv);
1627 ubifs_err(c, "leaf checking function returned error %d, for leaf at LEB %d:%d",
1628 err, zbr->lnum, zbr->offs);
1637 idx = znode->iip + 1;
1638 znode = znode->parent;
1639 if (idx < znode->child_cnt) {
1640 /* Switch to the next index in the parent */
1641 zbr = &znode->zbranch[idx];
1644 child = ubifs_load_znode(c, zbr, znode, idx);
1645 if (IS_ERR(child)) {
1646 err = PTR_ERR(child);
1654 * This is the last child, switch to the parent and
1659 /* Go to the lowest leftmost znode in the new sub-tree */
1660 while (znode->level > 0) {
1661 zbr = &znode->zbranch[0];
1664 child = ubifs_load_znode(c, zbr, znode, 0);
1665 if (IS_ERR(child)) {
1666 err = PTR_ERR(child);
1675 mutex_unlock(&c->tnc_mutex);
1680 zbr = &znode->parent->zbranch[znode->iip];
1683 ubifs_msg(c, "dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1684 ubifs_dump_znode(c, znode);
1686 mutex_unlock(&c->tnc_mutex);
1691 * add_size - add znode size to partially calculated index size.
1692 * @c: UBIFS file-system description object
1693 * @znode: znode to add size for
1694 * @priv: partially calculated index size
1696 * This is a helper function for 'dbg_check_idx_size()' which is called for
1697 * every indexing node and adds its size to the 'long long' variable pointed to
1700 static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1702 long long *idx_size = priv;
1705 add = ubifs_idx_node_sz(c, znode->child_cnt);
1706 add = ALIGN(add, 8);
1712 * dbg_check_idx_size - check index size.
1713 * @c: UBIFS file-system description object
1714 * @idx_size: size to check
1716 * This function walks the UBIFS index, calculates its size and checks that the
1717 * size is equivalent to @idx_size. Returns zero in case of success and a
1718 * negative error code in case of failure.
1720 int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
1725 if (!dbg_is_chk_index(c))
1728 err = dbg_walk_index(c, NULL, add_size, &calc);
1730 ubifs_err(c, "error %d while walking the index", err);
1734 if (calc != idx_size) {
1735 ubifs_err(c, "index size check failed: calculated size is %lld, should be %lld",
1745 * struct fsck_inode - information about an inode used when checking the file-system.
1746 * @rb: link in the RB-tree of inodes
1747 * @inum: inode number
1748 * @mode: inode type, permissions, etc
1749 * @nlink: inode link count
1750 * @xattr_cnt: count of extended attributes
1751 * @references: how many directory/xattr entries refer this inode (calculated
1752 * while walking the index)
1753 * @calc_cnt: for directory inode count of child directories
1754 * @size: inode size (read from on-flash inode)
1755 * @xattr_sz: summary size of all extended attributes (read from on-flash
1757 * @calc_sz: for directories calculated directory size
1758 * @calc_xcnt: count of extended attributes
1759 * @calc_xsz: calculated summary size of all extended attributes
1760 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1761 * inode (read from on-flash inode)
1762 * @calc_xnms: calculated sum of lengths of all extended attribute names
1769 unsigned int xattr_cnt;
1773 unsigned int xattr_sz;
1775 long long calc_xcnt;
1777 unsigned int xattr_nms;
1778 long long calc_xnms;
1782 * struct fsck_data - private FS checking information.
1783 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1786 struct rb_root inodes;
1790 * add_inode - add inode information to RB-tree of inodes.
1791 * @c: UBIFS file-system description object
1792 * @fsckd: FS checking information
1793 * @ino: raw UBIFS inode to add
1795 * This is a helper function for 'check_leaf()' which adds information about
1796 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1797 * case of success and a negative error code in case of failure.
1799 static struct fsck_inode *add_inode(struct ubifs_info *c,
1800 struct fsck_data *fsckd,
1801 struct ubifs_ino_node *ino)
1803 struct rb_node **p, *parent = NULL;
1804 struct fsck_inode *fscki;
1805 ino_t inum = key_inum_flash(c, &ino->key);
1806 struct inode *inode;
1807 struct ubifs_inode *ui;
1809 p = &fsckd->inodes.rb_node;
1812 fscki = rb_entry(parent, struct fsck_inode, rb);
1813 if (inum < fscki->inum)
1815 else if (inum > fscki->inum)
1816 p = &(*p)->rb_right;
1821 if (inum > c->highest_inum) {
1822 ubifs_err(c, "too high inode number, max. is %lu",
1823 (unsigned long)c->highest_inum);
1824 return ERR_PTR(-EINVAL);
1827 fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
1829 return ERR_PTR(-ENOMEM);
1831 inode = ilookup(c->vfs_sb, inum);
1835 * If the inode is present in the VFS inode cache, use it instead of
1836 * the on-flash inode which might be out-of-date. E.g., the size might
1837 * be out-of-date. If we do not do this, the following may happen, for
1839 * 1. A power cut happens
1840 * 2. We mount the file-system R/O, the replay process fixes up the
1841 * inode size in the VFS cache, but on on-flash.
1842 * 3. 'check_leaf()' fails because it hits a data node beyond inode
1846 fscki->nlink = le32_to_cpu(ino->nlink);
1847 fscki->size = le64_to_cpu(ino->size);
1848 fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
1849 fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
1850 fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
1851 fscki->mode = le32_to_cpu(ino->mode);
1853 ui = ubifs_inode(inode);
1854 fscki->nlink = inode->i_nlink;
1855 fscki->size = inode->i_size;
1856 fscki->xattr_cnt = ui->xattr_cnt;
1857 fscki->xattr_sz = ui->xattr_size;
1858 fscki->xattr_nms = ui->xattr_names;
1859 fscki->mode = inode->i_mode;
1863 if (S_ISDIR(fscki->mode)) {
1864 fscki->calc_sz = UBIFS_INO_NODE_SZ;
1865 fscki->calc_cnt = 2;
1868 rb_link_node(&fscki->rb, parent, p);
1869 rb_insert_color(&fscki->rb, &fsckd->inodes);
1875 * search_inode - search inode in the RB-tree of inodes.
1876 * @fsckd: FS checking information
1877 * @inum: inode number to search
1879 * This is a helper function for 'check_leaf()' which searches inode @inum in
1880 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1881 * the inode was not found.
1883 static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
1886 struct fsck_inode *fscki;
1888 p = fsckd->inodes.rb_node;
1890 fscki = rb_entry(p, struct fsck_inode, rb);
1891 if (inum < fscki->inum)
1893 else if (inum > fscki->inum)
1902 * read_add_inode - read inode node and add it to RB-tree of inodes.
1903 * @c: UBIFS file-system description object
1904 * @fsckd: FS checking information
1905 * @inum: inode number to read
1907 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1908 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1909 * information pointer in case of success and a negative error code in case of
1912 static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1913 struct fsck_data *fsckd, ino_t inum)
1916 union ubifs_key key;
1917 struct ubifs_znode *znode;
1918 struct ubifs_zbranch *zbr;
1919 struct ubifs_ino_node *ino;
1920 struct fsck_inode *fscki;
1922 fscki = search_inode(fsckd, inum);
1926 ino_key_init(c, &key, inum);
1927 err = ubifs_lookup_level0(c, &key, &znode, &n);
1929 ubifs_err(c, "inode %lu not found in index", (unsigned long)inum);
1930 return ERR_PTR(-ENOENT);
1931 } else if (err < 0) {
1932 ubifs_err(c, "error %d while looking up inode %lu",
1933 err, (unsigned long)inum);
1934 return ERR_PTR(err);
1937 zbr = &znode->zbranch[n];
1938 if (zbr->len < UBIFS_INO_NODE_SZ) {
1939 ubifs_err(c, "bad node %lu node length %d",
1940 (unsigned long)inum, zbr->len);
1941 return ERR_PTR(-EINVAL);
1944 ino = kmalloc(zbr->len, GFP_NOFS);
1946 return ERR_PTR(-ENOMEM);
1948 err = ubifs_tnc_read_node(c, zbr, ino);
1950 ubifs_err(c, "cannot read inode node at LEB %d:%d, error %d",
1951 zbr->lnum, zbr->offs, err);
1953 return ERR_PTR(err);
1956 fscki = add_inode(c, fsckd, ino);
1958 if (IS_ERR(fscki)) {
1959 ubifs_err(c, "error %ld while adding inode %lu node",
1960 PTR_ERR(fscki), (unsigned long)inum);
1968 * check_leaf - check leaf node.
1969 * @c: UBIFS file-system description object
1970 * @zbr: zbranch of the leaf node to check
1971 * @priv: FS checking information
1973 * This is a helper function for 'dbg_check_filesystem()' which is called for
1974 * every single leaf node while walking the indexing tree. It checks that the
1975 * leaf node referred from the indexing tree exists, has correct CRC, and does
1976 * some other basic validation. This function is also responsible for building
1977 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
1978 * calculates reference count, size, etc for each inode in order to later
1979 * compare them to the information stored inside the inodes and detect possible
1980 * inconsistencies. Returns zero in case of success and a negative error code
1981 * in case of failure.
1983 static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
1988 struct ubifs_ch *ch;
1989 int err, type = key_type(c, &zbr->key);
1990 struct fsck_inode *fscki;
1992 if (zbr->len < UBIFS_CH_SZ) {
1993 ubifs_err(c, "bad leaf length %d (LEB %d:%d)",
1994 zbr->len, zbr->lnum, zbr->offs);
1998 node = kmalloc(zbr->len, GFP_NOFS);
2002 err = ubifs_tnc_read_node(c, zbr, node);
2004 ubifs_err(c, "cannot read leaf node at LEB %d:%d, error %d",
2005 zbr->lnum, zbr->offs, err);
2009 /* If this is an inode node, add it to RB-tree of inodes */
2010 if (type == UBIFS_INO_KEY) {
2011 fscki = add_inode(c, priv, node);
2012 if (IS_ERR(fscki)) {
2013 err = PTR_ERR(fscki);
2014 ubifs_err(c, "error %d while adding inode node", err);
2020 if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
2021 type != UBIFS_DATA_KEY) {
2022 ubifs_err(c, "unexpected node type %d at LEB %d:%d",
2023 type, zbr->lnum, zbr->offs);
2029 if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
2030 ubifs_err(c, "too high sequence number, max. is %llu",
2036 if (type == UBIFS_DATA_KEY) {
2038 struct ubifs_data_node *dn = node;
2040 ubifs_assert(zbr->len >= UBIFS_DATA_NODE_SZ);
2043 * Search the inode node this data node belongs to and insert
2044 * it to the RB-tree of inodes.
2046 inum = key_inum_flash(c, &dn->key);
2047 fscki = read_add_inode(c, priv, inum);
2048 if (IS_ERR(fscki)) {
2049 err = PTR_ERR(fscki);
2050 ubifs_err(c, "error %d while processing data node and trying to find inode node %lu",
2051 err, (unsigned long)inum);
2055 /* Make sure the data node is within inode size */
2056 blk_offs = key_block_flash(c, &dn->key);
2057 blk_offs <<= UBIFS_BLOCK_SHIFT;
2058 blk_offs += le32_to_cpu(dn->size);
2059 if (blk_offs > fscki->size) {
2060 ubifs_err(c, "data node at LEB %d:%d is not within inode size %lld",
2061 zbr->lnum, zbr->offs, fscki->size);
2067 struct ubifs_dent_node *dent = node;
2068 struct fsck_inode *fscki1;
2070 ubifs_assert(zbr->len >= UBIFS_DENT_NODE_SZ);
2072 err = ubifs_validate_entry(c, dent);
2077 * Search the inode node this entry refers to and the parent
2078 * inode node and insert them to the RB-tree of inodes.
2080 inum = le64_to_cpu(dent->inum);
2081 fscki = read_add_inode(c, priv, inum);
2082 if (IS_ERR(fscki)) {
2083 err = PTR_ERR(fscki);
2084 ubifs_err(c, "error %d while processing entry node and trying to find inode node %lu",
2085 err, (unsigned long)inum);
2089 /* Count how many direntries or xentries refers this inode */
2090 fscki->references += 1;
2092 inum = key_inum_flash(c, &dent->key);
2093 fscki1 = read_add_inode(c, priv, inum);
2094 if (IS_ERR(fscki1)) {
2095 err = PTR_ERR(fscki1);
2096 ubifs_err(c, "error %d while processing entry node and trying to find parent inode node %lu",
2097 err, (unsigned long)inum);
2101 nlen = le16_to_cpu(dent->nlen);
2102 if (type == UBIFS_XENT_KEY) {
2103 fscki1->calc_xcnt += 1;
2104 fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
2105 fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
2106 fscki1->calc_xnms += nlen;
2108 fscki1->calc_sz += CALC_DENT_SIZE(nlen);
2109 if (dent->type == UBIFS_ITYPE_DIR)
2110 fscki1->calc_cnt += 1;
2119 ubifs_msg(c, "dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
2120 ubifs_dump_node(c, node);
2127 * free_inodes - free RB-tree of inodes.
2128 * @fsckd: FS checking information
2130 static void free_inodes(struct fsck_data *fsckd)
2132 struct fsck_inode *fscki, *n;
2134 rbtree_postorder_for_each_entry_safe(fscki, n, &fsckd->inodes, rb)
2139 * check_inodes - checks all inodes.
2140 * @c: UBIFS file-system description object
2141 * @fsckd: FS checking information
2143 * This is a helper function for 'dbg_check_filesystem()' which walks the
2144 * RB-tree of inodes after the index scan has been finished, and checks that
2145 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
2146 * %-EINVAL if not, and a negative error code in case of failure.
2148 static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
2151 union ubifs_key key;
2152 struct ubifs_znode *znode;
2153 struct ubifs_zbranch *zbr;
2154 struct ubifs_ino_node *ino;
2155 struct fsck_inode *fscki;
2156 struct rb_node *this = rb_first(&fsckd->inodes);
2159 fscki = rb_entry(this, struct fsck_inode, rb);
2160 this = rb_next(this);
2162 if (S_ISDIR(fscki->mode)) {
2164 * Directories have to have exactly one reference (they
2165 * cannot have hardlinks), although root inode is an
2168 if (fscki->inum != UBIFS_ROOT_INO &&
2169 fscki->references != 1) {
2170 ubifs_err(c, "directory inode %lu has %d direntries which refer it, but should be 1",
2171 (unsigned long)fscki->inum,
2175 if (fscki->inum == UBIFS_ROOT_INO &&
2176 fscki->references != 0) {
2177 ubifs_err(c, "root inode %lu has non-zero (%d) direntries which refer it",
2178 (unsigned long)fscki->inum,
2182 if (fscki->calc_sz != fscki->size) {
2183 ubifs_err(c, "directory inode %lu size is %lld, but calculated size is %lld",
2184 (unsigned long)fscki->inum,
2185 fscki->size, fscki->calc_sz);
2188 if (fscki->calc_cnt != fscki->nlink) {
2189 ubifs_err(c, "directory inode %lu nlink is %d, but calculated nlink is %d",
2190 (unsigned long)fscki->inum,
2191 fscki->nlink, fscki->calc_cnt);
2195 if (fscki->references != fscki->nlink) {
2196 ubifs_err(c, "inode %lu nlink is %d, but calculated nlink is %d",
2197 (unsigned long)fscki->inum,
2198 fscki->nlink, fscki->references);
2202 if (fscki->xattr_sz != fscki->calc_xsz) {
2203 ubifs_err(c, "inode %lu has xattr size %u, but calculated size is %lld",
2204 (unsigned long)fscki->inum, fscki->xattr_sz,
2208 if (fscki->xattr_cnt != fscki->calc_xcnt) {
2209 ubifs_err(c, "inode %lu has %u xattrs, but calculated count is %lld",
2210 (unsigned long)fscki->inum,
2211 fscki->xattr_cnt, fscki->calc_xcnt);
2214 if (fscki->xattr_nms != fscki->calc_xnms) {
2215 ubifs_err(c, "inode %lu has xattr names' size %u, but calculated names' size is %lld",
2216 (unsigned long)fscki->inum, fscki->xattr_nms,
2225 /* Read the bad inode and dump it */
2226 ino_key_init(c, &key, fscki->inum);
2227 err = ubifs_lookup_level0(c, &key, &znode, &n);
2229 ubifs_err(c, "inode %lu not found in index",
2230 (unsigned long)fscki->inum);
2232 } else if (err < 0) {
2233 ubifs_err(c, "error %d while looking up inode %lu",
2234 err, (unsigned long)fscki->inum);
2238 zbr = &znode->zbranch[n];
2239 ino = kmalloc(zbr->len, GFP_NOFS);
2243 err = ubifs_tnc_read_node(c, zbr, ino);
2245 ubifs_err(c, "cannot read inode node at LEB %d:%d, error %d",
2246 zbr->lnum, zbr->offs, err);
2251 ubifs_msg(c, "dump of the inode %lu sitting in LEB %d:%d",
2252 (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
2253 ubifs_dump_node(c, ino);
2259 * dbg_check_filesystem - check the file-system.
2260 * @c: UBIFS file-system description object
2262 * This function checks the file system, namely:
2263 * o makes sure that all leaf nodes exist and their CRCs are correct;
2264 * o makes sure inode nlink, size, xattr size/count are correct (for all
2267 * The function reads whole indexing tree and all nodes, so it is pretty
2268 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2269 * not, and a negative error code in case of failure.
2271 int dbg_check_filesystem(struct ubifs_info *c)
2274 struct fsck_data fsckd;
2276 if (!dbg_is_chk_fs(c))
2279 fsckd.inodes = RB_ROOT;
2280 err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
2284 err = check_inodes(c, &fsckd);
2288 free_inodes(&fsckd);
2292 ubifs_err(c, "file-system check failed with error %d", err);
2294 free_inodes(&fsckd);
2299 * dbg_check_data_nodes_order - check that list of data nodes is sorted.
2300 * @c: UBIFS file-system description object
2301 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2303 * This function returns zero if the list of data nodes is sorted correctly,
2304 * and %-EINVAL if not.
2306 int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head)
2308 struct list_head *cur;
2309 struct ubifs_scan_node *sa, *sb;
2311 if (!dbg_is_chk_gen(c))
2314 for (cur = head->next; cur->next != head; cur = cur->next) {
2316 uint32_t blka, blkb;
2319 sa = container_of(cur, struct ubifs_scan_node, list);
2320 sb = container_of(cur->next, struct ubifs_scan_node, list);
2322 if (sa->type != UBIFS_DATA_NODE) {
2323 ubifs_err(c, "bad node type %d", sa->type);
2324 ubifs_dump_node(c, sa->node);
2327 if (sb->type != UBIFS_DATA_NODE) {
2328 ubifs_err(c, "bad node type %d", sb->type);
2329 ubifs_dump_node(c, sb->node);
2333 inuma = key_inum(c, &sa->key);
2334 inumb = key_inum(c, &sb->key);
2338 if (inuma > inumb) {
2339 ubifs_err(c, "larger inum %lu goes before inum %lu",
2340 (unsigned long)inuma, (unsigned long)inumb);
2344 blka = key_block(c, &sa->key);
2345 blkb = key_block(c, &sb->key);
2348 ubifs_err(c, "larger block %u goes before %u", blka, blkb);
2352 ubifs_err(c, "two data nodes for the same block");
2360 ubifs_dump_node(c, sa->node);
2361 ubifs_dump_node(c, sb->node);
2366 * dbg_check_nondata_nodes_order - check that list of data nodes is sorted.
2367 * @c: UBIFS file-system description object
2368 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2370 * This function returns zero if the list of non-data nodes is sorted correctly,
2371 * and %-EINVAL if not.
2373 int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head)
2375 struct list_head *cur;
2376 struct ubifs_scan_node *sa, *sb;
2378 if (!dbg_is_chk_gen(c))
2381 for (cur = head->next; cur->next != head; cur = cur->next) {
2383 uint32_t hasha, hashb;
2386 sa = container_of(cur, struct ubifs_scan_node, list);
2387 sb = container_of(cur->next, struct ubifs_scan_node, list);
2389 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2390 sa->type != UBIFS_XENT_NODE) {
2391 ubifs_err(c, "bad node type %d", sa->type);
2392 ubifs_dump_node(c, sa->node);
2395 if (sb->type != UBIFS_INO_NODE && sb->type != UBIFS_DENT_NODE &&
2396 sb->type != UBIFS_XENT_NODE) {
2397 ubifs_err(c, "bad node type %d", sb->type);
2398 ubifs_dump_node(c, sb->node);
2402 if (sa->type != UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2403 ubifs_err(c, "non-inode node goes before inode node");
2407 if (sa->type == UBIFS_INO_NODE && sb->type != UBIFS_INO_NODE)
2410 if (sa->type == UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2411 /* Inode nodes are sorted in descending size order */
2412 if (sa->len < sb->len) {
2413 ubifs_err(c, "smaller inode node goes first");
2420 * This is either a dentry or xentry, which should be sorted in
2421 * ascending (parent ino, hash) order.
2423 inuma = key_inum(c, &sa->key);
2424 inumb = key_inum(c, &sb->key);
2428 if (inuma > inumb) {
2429 ubifs_err(c, "larger inum %lu goes before inum %lu",
2430 (unsigned long)inuma, (unsigned long)inumb);
2434 hasha = key_block(c, &sa->key);
2435 hashb = key_block(c, &sb->key);
2437 if (hasha > hashb) {
2438 ubifs_err(c, "larger hash %u goes before %u",
2447 ubifs_msg(c, "dumping first node");
2448 ubifs_dump_node(c, sa->node);
2449 ubifs_msg(c, "dumping second node");
2450 ubifs_dump_node(c, sb->node);
2455 static inline int chance(unsigned int n, unsigned int out_of)
2457 return !!((prandom_u32() % out_of) + 1 <= n);
2461 static int power_cut_emulated(struct ubifs_info *c, int lnum, int write)
2463 struct ubifs_debug_info *d = c->dbg;
2465 ubifs_assert(dbg_is_tst_rcvry(c));
2468 /* First call - decide delay to the power cut */
2470 unsigned long delay;
2474 /* Fail within 1 minute */
2475 delay = prandom_u32() % 60000;
2476 d->pc_timeout = jiffies;
2477 d->pc_timeout += msecs_to_jiffies(delay);
2478 ubifs_warn(c, "failing after %lums", delay);
2481 delay = prandom_u32() % 10000;
2482 /* Fail within 10000 operations */
2483 d->pc_cnt_max = delay;
2484 ubifs_warn(c, "failing after %lu calls", delay);
2491 /* Determine if failure delay has expired */
2492 if (d->pc_delay == 1 && time_before(jiffies, d->pc_timeout))
2494 if (d->pc_delay == 2 && d->pc_cnt++ < d->pc_cnt_max)
2497 if (lnum == UBIFS_SB_LNUM) {
2498 if (write && chance(1, 2))
2502 ubifs_warn(c, "failing in super block LEB %d", lnum);
2503 } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2506 ubifs_warn(c, "failing in master LEB %d", lnum);
2507 } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2508 if (write && chance(99, 100))
2510 if (chance(399, 400))
2512 ubifs_warn(c, "failing in log LEB %d", lnum);
2513 } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2514 if (write && chance(7, 8))
2518 ubifs_warn(c, "failing in LPT LEB %d", lnum);
2519 } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2520 if (write && chance(1, 2))
2524 ubifs_warn(c, "failing in orphan LEB %d", lnum);
2525 } else if (lnum == c->ihead_lnum) {
2526 if (chance(99, 100))
2528 ubifs_warn(c, "failing in index head LEB %d", lnum);
2529 } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2532 ubifs_warn(c, "failing in GC head LEB %d", lnum);
2533 } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
2534 !ubifs_search_bud(c, lnum)) {
2537 ubifs_warn(c, "failing in non-bud LEB %d", lnum);
2538 } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
2539 c->cmt_state == COMMIT_RUNNING_REQUIRED) {
2540 if (chance(999, 1000))
2542 ubifs_warn(c, "failing in bud LEB %d commit running", lnum);
2544 if (chance(9999, 10000))
2546 ubifs_warn(c, "failing in bud LEB %d commit not running", lnum);
2550 ubifs_warn(c, "========== Power cut emulated ==========");
2555 static int corrupt_data(const struct ubifs_info *c, const void *buf,
2558 unsigned int from, to, ffs = chance(1, 2);
2559 unsigned char *p = (void *)buf;
2561 from = prandom_u32() % len;
2562 /* Corruption span max to end of write unit */
2563 to = min(len, ALIGN(from + 1, c->max_write_size));
2565 ubifs_warn(c, "filled bytes %u-%u with %s", from, to - 1,
2566 ffs ? "0xFFs" : "random data");
2569 memset(p + from, 0xFF, to - from);
2571 prandom_bytes(p + from, to - from);
2576 int dbg_leb_write(struct ubifs_info *c, int lnum, const void *buf,
2581 if (dbg_is_power_cut(c))
2584 failing = power_cut_emulated(c, lnum, 1);
2586 len = corrupt_data(c, buf, len);
2587 ubifs_warn(c, "actually write %d bytes to LEB %d:%d (the buffer was corrupted)",
2590 err = ubi_leb_write(c->ubi, lnum, buf, offs, len);
2598 int dbg_leb_change(struct ubifs_info *c, int lnum, const void *buf,
2603 if (dbg_is_power_cut(c))
2605 if (power_cut_emulated(c, lnum, 1))
2607 err = ubi_leb_change(c->ubi, lnum, buf, len);
2610 if (power_cut_emulated(c, lnum, 1))
2615 int dbg_leb_unmap(struct ubifs_info *c, int lnum)
2619 if (dbg_is_power_cut(c))
2621 if (power_cut_emulated(c, lnum, 0))
2623 err = ubi_leb_unmap(c->ubi, lnum);
2626 if (power_cut_emulated(c, lnum, 0))
2631 int dbg_leb_map(struct ubifs_info *c, int lnum)
2635 if (dbg_is_power_cut(c))
2637 if (power_cut_emulated(c, lnum, 0))
2639 err = ubi_leb_map(c->ubi, lnum);
2642 if (power_cut_emulated(c, lnum, 0))
2648 * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
2649 * contain the stuff specific to particular file-system mounts.
2651 static struct dentry *dfs_rootdir;
2653 static int dfs_file_open(struct inode *inode, struct file *file)
2655 file->private_data = inode->i_private;
2656 return nonseekable_open(inode, file);
2660 * provide_user_output - provide output to the user reading a debugfs file.
2661 * @val: boolean value for the answer
2662 * @u: the buffer to store the answer at
2663 * @count: size of the buffer
2664 * @ppos: position in the @u output buffer
2666 * This is a simple helper function which stores @val boolean value in the user
2667 * buffer when the user reads one of UBIFS debugfs files. Returns amount of
2668 * bytes written to @u in case of success and a negative error code in case of
2671 static int provide_user_output(int val, char __user *u, size_t count,
2683 return simple_read_from_buffer(u, count, ppos, buf, 2);
2686 static ssize_t dfs_file_read(struct file *file, char __user *u, size_t count,
2689 struct dentry *dent = file->f_path.dentry;
2690 struct ubifs_info *c = file->private_data;
2691 struct ubifs_debug_info *d = c->dbg;
2694 if (dent == d->dfs_chk_gen)
2696 else if (dent == d->dfs_chk_index)
2698 else if (dent == d->dfs_chk_orph)
2700 else if (dent == d->dfs_chk_lprops)
2701 val = d->chk_lprops;
2702 else if (dent == d->dfs_chk_fs)
2704 else if (dent == d->dfs_tst_rcvry)
2706 else if (dent == d->dfs_ro_error)
2711 return provide_user_output(val, u, count, ppos);
2715 * interpret_user_input - interpret user debugfs file input.
2716 * @u: user-provided buffer with the input
2717 * @count: buffer size
2719 * This is a helper function which interpret user input to a boolean UBIFS
2720 * debugfs file. Returns %0 or %1 in case of success and a negative error code
2721 * in case of failure.
2723 static int interpret_user_input(const char __user *u, size_t count)
2728 buf_size = min_t(size_t, count, (sizeof(buf) - 1));
2729 if (copy_from_user(buf, u, buf_size))
2734 else if (buf[0] == '0')
2740 static ssize_t dfs_file_write(struct file *file, const char __user *u,
2741 size_t count, loff_t *ppos)
2743 struct ubifs_info *c = file->private_data;
2744 struct ubifs_debug_info *d = c->dbg;
2745 struct dentry *dent = file->f_path.dentry;
2749 * TODO: this is racy - the file-system might have already been
2750 * unmounted and we'd oops in this case. The plan is to fix it with
2751 * help of 'iterate_supers_type()' which we should have in v3.0: when
2752 * a debugfs opened, we rember FS's UUID in file->private_data. Then
2753 * whenever we access the FS via a debugfs file, we iterate all UBIFS
2754 * superblocks and fine the one with the same UUID, and take the
2757 * The other way to go suggested by Al Viro is to create a separate
2758 * 'ubifs-debug' file-system instead.
2760 if (file->f_path.dentry == d->dfs_dump_lprops) {
2761 ubifs_dump_lprops(c);
2764 if (file->f_path.dentry == d->dfs_dump_budg) {
2765 ubifs_dump_budg(c, &c->bi);
2768 if (file->f_path.dentry == d->dfs_dump_tnc) {
2769 mutex_lock(&c->tnc_mutex);
2771 mutex_unlock(&c->tnc_mutex);
2775 val = interpret_user_input(u, count);
2779 if (dent == d->dfs_chk_gen)
2781 else if (dent == d->dfs_chk_index)
2783 else if (dent == d->dfs_chk_orph)
2785 else if (dent == d->dfs_chk_lprops)
2786 d->chk_lprops = val;
2787 else if (dent == d->dfs_chk_fs)
2789 else if (dent == d->dfs_tst_rcvry)
2791 else if (dent == d->dfs_ro_error)
2792 c->ro_error = !!val;
2799 static const struct file_operations dfs_fops = {
2800 .open = dfs_file_open,
2801 .read = dfs_file_read,
2802 .write = dfs_file_write,
2803 .owner = THIS_MODULE,
2804 .llseek = no_llseek,
2808 * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
2809 * @c: UBIFS file-system description object
2811 * This function creates all debugfs files for this instance of UBIFS. Returns
2812 * zero in case of success and a negative error code in case of failure.
2814 * Note, the only reason we have not merged this function with the
2815 * 'ubifs_debugging_init()' function is because it is better to initialize
2816 * debugfs interfaces at the very end of the mount process, and remove them at
2817 * the very beginning of the mount process.
2819 int dbg_debugfs_init_fs(struct ubifs_info *c)
2823 struct dentry *dent;
2824 struct ubifs_debug_info *d = c->dbg;
2826 if (!IS_ENABLED(CONFIG_DEBUG_FS))
2829 n = snprintf(d->dfs_dir_name, UBIFS_DFS_DIR_LEN + 1, UBIFS_DFS_DIR_NAME,
2830 c->vi.ubi_num, c->vi.vol_id);
2831 if (n == UBIFS_DFS_DIR_LEN) {
2832 /* The array size is too small */
2833 fname = UBIFS_DFS_DIR_NAME;
2834 dent = ERR_PTR(-EINVAL);
2838 fname = d->dfs_dir_name;
2839 dent = debugfs_create_dir(fname, dfs_rootdir);
2840 if (IS_ERR_OR_NULL(dent))
2844 fname = "dump_lprops";
2845 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2846 if (IS_ERR_OR_NULL(dent))
2848 d->dfs_dump_lprops = dent;
2850 fname = "dump_budg";
2851 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2852 if (IS_ERR_OR_NULL(dent))
2854 d->dfs_dump_budg = dent;
2857 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2858 if (IS_ERR_OR_NULL(dent))
2860 d->dfs_dump_tnc = dent;
2862 fname = "chk_general";
2863 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2865 if (IS_ERR_OR_NULL(dent))
2867 d->dfs_chk_gen = dent;
2869 fname = "chk_index";
2870 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2872 if (IS_ERR_OR_NULL(dent))
2874 d->dfs_chk_index = dent;
2876 fname = "chk_orphans";
2877 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2879 if (IS_ERR_OR_NULL(dent))
2881 d->dfs_chk_orph = dent;
2883 fname = "chk_lprops";
2884 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2886 if (IS_ERR_OR_NULL(dent))
2888 d->dfs_chk_lprops = dent;
2891 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2893 if (IS_ERR_OR_NULL(dent))
2895 d->dfs_chk_fs = dent;
2897 fname = "tst_recovery";
2898 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2900 if (IS_ERR_OR_NULL(dent))
2902 d->dfs_tst_rcvry = dent;
2905 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2907 if (IS_ERR_OR_NULL(dent))
2909 d->dfs_ro_error = dent;
2914 debugfs_remove_recursive(d->dfs_dir);
2916 err = dent ? PTR_ERR(dent) : -ENODEV;
2917 ubifs_err(c, "cannot create \"%s\" debugfs file or directory, error %d\n",
2923 * dbg_debugfs_exit_fs - remove all debugfs files.
2924 * @c: UBIFS file-system description object
2926 void dbg_debugfs_exit_fs(struct ubifs_info *c)
2928 if (IS_ENABLED(CONFIG_DEBUG_FS))
2929 debugfs_remove_recursive(c->dbg->dfs_dir);
2932 struct ubifs_global_debug_info ubifs_dbg;
2934 static struct dentry *dfs_chk_gen;
2935 static struct dentry *dfs_chk_index;
2936 static struct dentry *dfs_chk_orph;
2937 static struct dentry *dfs_chk_lprops;
2938 static struct dentry *dfs_chk_fs;
2939 static struct dentry *dfs_tst_rcvry;
2941 static ssize_t dfs_global_file_read(struct file *file, char __user *u,
2942 size_t count, loff_t *ppos)
2944 struct dentry *dent = file->f_path.dentry;
2947 if (dent == dfs_chk_gen)
2948 val = ubifs_dbg.chk_gen;
2949 else if (dent == dfs_chk_index)
2950 val = ubifs_dbg.chk_index;
2951 else if (dent == dfs_chk_orph)
2952 val = ubifs_dbg.chk_orph;
2953 else if (dent == dfs_chk_lprops)
2954 val = ubifs_dbg.chk_lprops;
2955 else if (dent == dfs_chk_fs)
2956 val = ubifs_dbg.chk_fs;
2957 else if (dent == dfs_tst_rcvry)
2958 val = ubifs_dbg.tst_rcvry;
2962 return provide_user_output(val, u, count, ppos);
2965 static ssize_t dfs_global_file_write(struct file *file, const char __user *u,
2966 size_t count, loff_t *ppos)
2968 struct dentry *dent = file->f_path.dentry;
2971 val = interpret_user_input(u, count);
2975 if (dent == dfs_chk_gen)
2976 ubifs_dbg.chk_gen = val;
2977 else if (dent == dfs_chk_index)
2978 ubifs_dbg.chk_index = val;
2979 else if (dent == dfs_chk_orph)
2980 ubifs_dbg.chk_orph = val;
2981 else if (dent == dfs_chk_lprops)
2982 ubifs_dbg.chk_lprops = val;
2983 else if (dent == dfs_chk_fs)
2984 ubifs_dbg.chk_fs = val;
2985 else if (dent == dfs_tst_rcvry)
2986 ubifs_dbg.tst_rcvry = val;
2993 static const struct file_operations dfs_global_fops = {
2994 .read = dfs_global_file_read,
2995 .write = dfs_global_file_write,
2996 .owner = THIS_MODULE,
2997 .llseek = no_llseek,
3001 * dbg_debugfs_init - initialize debugfs file-system.
3003 * UBIFS uses debugfs file-system to expose various debugging knobs to
3004 * user-space. This function creates "ubifs" directory in the debugfs
3005 * file-system. Returns zero in case of success and a negative error code in
3008 int dbg_debugfs_init(void)
3012 struct dentry *dent;
3014 if (!IS_ENABLED(CONFIG_DEBUG_FS))
3018 dent = debugfs_create_dir(fname, NULL);
3019 if (IS_ERR_OR_NULL(dent))
3023 fname = "chk_general";
3024 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3026 if (IS_ERR_OR_NULL(dent))
3030 fname = "chk_index";
3031 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3033 if (IS_ERR_OR_NULL(dent))
3035 dfs_chk_index = dent;
3037 fname = "chk_orphans";
3038 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3040 if (IS_ERR_OR_NULL(dent))
3042 dfs_chk_orph = dent;
3044 fname = "chk_lprops";
3045 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3047 if (IS_ERR_OR_NULL(dent))
3049 dfs_chk_lprops = dent;
3052 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3054 if (IS_ERR_OR_NULL(dent))
3058 fname = "tst_recovery";
3059 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3061 if (IS_ERR_OR_NULL(dent))
3063 dfs_tst_rcvry = dent;
3068 debugfs_remove_recursive(dfs_rootdir);
3070 err = dent ? PTR_ERR(dent) : -ENODEV;
3071 pr_err("UBIFS error (pid %d): cannot create \"%s\" debugfs file or directory, error %d\n",
3072 current->pid, fname, err);
3077 * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
3079 void dbg_debugfs_exit(void)
3081 if (IS_ENABLED(CONFIG_DEBUG_FS))
3082 debugfs_remove_recursive(dfs_rootdir);
3086 * ubifs_debugging_init - initialize UBIFS debugging.
3087 * @c: UBIFS file-system description object
3089 * This function initializes debugging-related data for the file system.
3090 * Returns zero in case of success and a negative error code in case of
3093 int ubifs_debugging_init(struct ubifs_info *c)
3095 c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
3103 * ubifs_debugging_exit - free debugging data.
3104 * @c: UBIFS file-system description object
3106 void ubifs_debugging_exit(struct ubifs_info *c)