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 #define UBIFS_DBG_PRESERVE_UBI
33 #include <linux/module.h>
34 #include <linux/debugfs.h>
35 #include <linux/math64.h>
36 #include <linux/uaccess.h>
38 #ifdef CONFIG_UBIFS_FS_DEBUG
40 DEFINE_SPINLOCK(dbg_lock);
42 static char dbg_key_buf0[128];
43 static char dbg_key_buf1[128];
45 static const char *get_key_fmt(int fmt)
48 case UBIFS_SIMPLE_KEY_FMT:
51 return "unknown/invalid format";
55 static const char *get_key_hash(int hash)
58 case UBIFS_KEY_HASH_R5:
60 case UBIFS_KEY_HASH_TEST:
63 return "unknown/invalid name hash";
67 static const char *get_key_type(int type)
81 return "unknown/invalid key";
85 static const char *get_dent_type(int type)
98 case UBIFS_ITYPE_FIFO:
100 case UBIFS_ITYPE_SOCK:
103 return "unknown/invalid type";
107 static void sprintf_key(const struct ubifs_info *c, const union ubifs_key *key,
111 int type = key_type(c, key);
113 if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
116 sprintf(p, "(%lu, %s)", (unsigned long)key_inum(c, key),
121 sprintf(p, "(%lu, %s, %#08x)",
122 (unsigned long)key_inum(c, key),
123 get_key_type(type), key_hash(c, key));
126 sprintf(p, "(%lu, %s, %u)",
127 (unsigned long)key_inum(c, key),
128 get_key_type(type), key_block(c, key));
131 sprintf(p, "(%lu, %s)",
132 (unsigned long)key_inum(c, key),
136 sprintf(p, "(bad key type: %#08x, %#08x)",
137 key->u32[0], key->u32[1]);
140 sprintf(p, "bad key format %d", c->key_fmt);
143 const char *dbg_key_str0(const struct ubifs_info *c, const union ubifs_key *key)
145 /* dbg_lock must be held */
146 sprintf_key(c, key, dbg_key_buf0);
150 const char *dbg_key_str1(const struct ubifs_info *c, const union ubifs_key *key)
152 /* dbg_lock must be held */
153 sprintf_key(c, key, dbg_key_buf1);
157 const char *dbg_ntype(int type)
161 return "padding node";
163 return "superblock node";
165 return "master node";
167 return "reference node";
170 case UBIFS_DENT_NODE:
171 return "direntry node";
172 case UBIFS_XENT_NODE:
173 return "xentry node";
174 case UBIFS_DATA_NODE:
176 case UBIFS_TRUN_NODE:
177 return "truncate node";
179 return "indexing node";
181 return "commit start node";
182 case UBIFS_ORPH_NODE:
183 return "orphan node";
185 return "unknown node";
189 static const char *dbg_gtype(int type)
192 case UBIFS_NO_NODE_GROUP:
193 return "no node group";
194 case UBIFS_IN_NODE_GROUP:
195 return "in node group";
196 case UBIFS_LAST_OF_NODE_GROUP:
197 return "last of node group";
203 const char *dbg_cstate(int cmt_state)
207 return "commit resting";
208 case COMMIT_BACKGROUND:
209 return "background commit requested";
210 case COMMIT_REQUIRED:
211 return "commit required";
212 case COMMIT_RUNNING_BACKGROUND:
213 return "BACKGROUND commit running";
214 case COMMIT_RUNNING_REQUIRED:
215 return "commit running and required";
217 return "broken commit";
219 return "unknown commit state";
223 const char *dbg_jhead(int jhead)
233 return "unknown journal head";
237 static void dump_ch(const struct ubifs_ch *ch)
239 printk(KERN_DEBUG "\tmagic %#x\n", le32_to_cpu(ch->magic));
240 printk(KERN_DEBUG "\tcrc %#x\n", le32_to_cpu(ch->crc));
241 printk(KERN_DEBUG "\tnode_type %d (%s)\n", ch->node_type,
242 dbg_ntype(ch->node_type));
243 printk(KERN_DEBUG "\tgroup_type %d (%s)\n", ch->group_type,
244 dbg_gtype(ch->group_type));
245 printk(KERN_DEBUG "\tsqnum %llu\n",
246 (unsigned long long)le64_to_cpu(ch->sqnum));
247 printk(KERN_DEBUG "\tlen %u\n", le32_to_cpu(ch->len));
250 void dbg_dump_inode(struct ubifs_info *c, const struct inode *inode)
252 const struct ubifs_inode *ui = ubifs_inode(inode);
253 struct qstr nm = { .name = NULL };
255 struct ubifs_dent_node *dent, *pdent = NULL;
258 printk(KERN_DEBUG "Dump in-memory inode:");
259 printk(KERN_DEBUG "\tinode %lu\n", inode->i_ino);
260 printk(KERN_DEBUG "\tsize %llu\n",
261 (unsigned long long)i_size_read(inode));
262 printk(KERN_DEBUG "\tnlink %u\n", inode->i_nlink);
263 printk(KERN_DEBUG "\tuid %u\n", (unsigned int)inode->i_uid);
264 printk(KERN_DEBUG "\tgid %u\n", (unsigned int)inode->i_gid);
265 printk(KERN_DEBUG "\tatime %u.%u\n",
266 (unsigned int)inode->i_atime.tv_sec,
267 (unsigned int)inode->i_atime.tv_nsec);
268 printk(KERN_DEBUG "\tmtime %u.%u\n",
269 (unsigned int)inode->i_mtime.tv_sec,
270 (unsigned int)inode->i_mtime.tv_nsec);
271 printk(KERN_DEBUG "\tctime %u.%u\n",
272 (unsigned int)inode->i_ctime.tv_sec,
273 (unsigned int)inode->i_ctime.tv_nsec);
274 printk(KERN_DEBUG "\tcreat_sqnum %llu\n", ui->creat_sqnum);
275 printk(KERN_DEBUG "\txattr_size %u\n", ui->xattr_size);
276 printk(KERN_DEBUG "\txattr_cnt %u\n", ui->xattr_cnt);
277 printk(KERN_DEBUG "\txattr_names %u\n", ui->xattr_names);
278 printk(KERN_DEBUG "\tdirty %u\n", ui->dirty);
279 printk(KERN_DEBUG "\txattr %u\n", ui->xattr);
280 printk(KERN_DEBUG "\tbulk_read %u\n", ui->xattr);
281 printk(KERN_DEBUG "\tsynced_i_size %llu\n",
282 (unsigned long long)ui->synced_i_size);
283 printk(KERN_DEBUG "\tui_size %llu\n",
284 (unsigned long long)ui->ui_size);
285 printk(KERN_DEBUG "\tflags %d\n", ui->flags);
286 printk(KERN_DEBUG "\tcompr_type %d\n", ui->compr_type);
287 printk(KERN_DEBUG "\tlast_page_read %lu\n", ui->last_page_read);
288 printk(KERN_DEBUG "\tread_in_a_row %lu\n", ui->read_in_a_row);
289 printk(KERN_DEBUG "\tdata_len %d\n", ui->data_len);
291 if (!S_ISDIR(inode->i_mode))
294 printk(KERN_DEBUG "List of directory entries:\n");
295 ubifs_assert(!mutex_is_locked(&c->tnc_mutex));
297 lowest_dent_key(c, &key, inode->i_ino);
299 dent = ubifs_tnc_next_ent(c, &key, &nm);
301 if (PTR_ERR(dent) != -ENOENT)
302 printk(KERN_DEBUG "error %ld\n", PTR_ERR(dent));
306 printk(KERN_DEBUG "\t%d: %s (%s)\n",
307 count++, dent->name, get_dent_type(dent->type));
309 nm.name = dent->name;
310 nm.len = le16_to_cpu(dent->nlen);
313 key_read(c, &dent->key, &key);
318 void dbg_dump_node(const struct ubifs_info *c, const void *node)
322 const struct ubifs_ch *ch = node;
324 if (dbg_is_tst_rcvry(c))
327 /* If the magic is incorrect, just hexdump the first bytes */
328 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
329 printk(KERN_DEBUG "Not a node, first %zu bytes:", UBIFS_CH_SZ);
330 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
331 (void *)node, UBIFS_CH_SZ, 1);
335 spin_lock(&dbg_lock);
338 switch (ch->node_type) {
341 const struct ubifs_pad_node *pad = node;
343 printk(KERN_DEBUG "\tpad_len %u\n",
344 le32_to_cpu(pad->pad_len));
349 const struct ubifs_sb_node *sup = node;
350 unsigned int sup_flags = le32_to_cpu(sup->flags);
352 printk(KERN_DEBUG "\tkey_hash %d (%s)\n",
353 (int)sup->key_hash, get_key_hash(sup->key_hash));
354 printk(KERN_DEBUG "\tkey_fmt %d (%s)\n",
355 (int)sup->key_fmt, get_key_fmt(sup->key_fmt));
356 printk(KERN_DEBUG "\tflags %#x\n", sup_flags);
357 printk(KERN_DEBUG "\t big_lpt %u\n",
358 !!(sup_flags & UBIFS_FLG_BIGLPT));
359 printk(KERN_DEBUG "\t space_fixup %u\n",
360 !!(sup_flags & UBIFS_FLG_SPACE_FIXUP));
361 printk(KERN_DEBUG "\tmin_io_size %u\n",
362 le32_to_cpu(sup->min_io_size));
363 printk(KERN_DEBUG "\tleb_size %u\n",
364 le32_to_cpu(sup->leb_size));
365 printk(KERN_DEBUG "\tleb_cnt %u\n",
366 le32_to_cpu(sup->leb_cnt));
367 printk(KERN_DEBUG "\tmax_leb_cnt %u\n",
368 le32_to_cpu(sup->max_leb_cnt));
369 printk(KERN_DEBUG "\tmax_bud_bytes %llu\n",
370 (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
371 printk(KERN_DEBUG "\tlog_lebs %u\n",
372 le32_to_cpu(sup->log_lebs));
373 printk(KERN_DEBUG "\tlpt_lebs %u\n",
374 le32_to_cpu(sup->lpt_lebs));
375 printk(KERN_DEBUG "\torph_lebs %u\n",
376 le32_to_cpu(sup->orph_lebs));
377 printk(KERN_DEBUG "\tjhead_cnt %u\n",
378 le32_to_cpu(sup->jhead_cnt));
379 printk(KERN_DEBUG "\tfanout %u\n",
380 le32_to_cpu(sup->fanout));
381 printk(KERN_DEBUG "\tlsave_cnt %u\n",
382 le32_to_cpu(sup->lsave_cnt));
383 printk(KERN_DEBUG "\tdefault_compr %u\n",
384 (int)le16_to_cpu(sup->default_compr));
385 printk(KERN_DEBUG "\trp_size %llu\n",
386 (unsigned long long)le64_to_cpu(sup->rp_size));
387 printk(KERN_DEBUG "\trp_uid %u\n",
388 le32_to_cpu(sup->rp_uid));
389 printk(KERN_DEBUG "\trp_gid %u\n",
390 le32_to_cpu(sup->rp_gid));
391 printk(KERN_DEBUG "\tfmt_version %u\n",
392 le32_to_cpu(sup->fmt_version));
393 printk(KERN_DEBUG "\ttime_gran %u\n",
394 le32_to_cpu(sup->time_gran));
395 printk(KERN_DEBUG "\tUUID %pUB\n",
401 const struct ubifs_mst_node *mst = node;
403 printk(KERN_DEBUG "\thighest_inum %llu\n",
404 (unsigned long long)le64_to_cpu(mst->highest_inum));
405 printk(KERN_DEBUG "\tcommit number %llu\n",
406 (unsigned long long)le64_to_cpu(mst->cmt_no));
407 printk(KERN_DEBUG "\tflags %#x\n",
408 le32_to_cpu(mst->flags));
409 printk(KERN_DEBUG "\tlog_lnum %u\n",
410 le32_to_cpu(mst->log_lnum));
411 printk(KERN_DEBUG "\troot_lnum %u\n",
412 le32_to_cpu(mst->root_lnum));
413 printk(KERN_DEBUG "\troot_offs %u\n",
414 le32_to_cpu(mst->root_offs));
415 printk(KERN_DEBUG "\troot_len %u\n",
416 le32_to_cpu(mst->root_len));
417 printk(KERN_DEBUG "\tgc_lnum %u\n",
418 le32_to_cpu(mst->gc_lnum));
419 printk(KERN_DEBUG "\tihead_lnum %u\n",
420 le32_to_cpu(mst->ihead_lnum));
421 printk(KERN_DEBUG "\tihead_offs %u\n",
422 le32_to_cpu(mst->ihead_offs));
423 printk(KERN_DEBUG "\tindex_size %llu\n",
424 (unsigned long long)le64_to_cpu(mst->index_size));
425 printk(KERN_DEBUG "\tlpt_lnum %u\n",
426 le32_to_cpu(mst->lpt_lnum));
427 printk(KERN_DEBUG "\tlpt_offs %u\n",
428 le32_to_cpu(mst->lpt_offs));
429 printk(KERN_DEBUG "\tnhead_lnum %u\n",
430 le32_to_cpu(mst->nhead_lnum));
431 printk(KERN_DEBUG "\tnhead_offs %u\n",
432 le32_to_cpu(mst->nhead_offs));
433 printk(KERN_DEBUG "\tltab_lnum %u\n",
434 le32_to_cpu(mst->ltab_lnum));
435 printk(KERN_DEBUG "\tltab_offs %u\n",
436 le32_to_cpu(mst->ltab_offs));
437 printk(KERN_DEBUG "\tlsave_lnum %u\n",
438 le32_to_cpu(mst->lsave_lnum));
439 printk(KERN_DEBUG "\tlsave_offs %u\n",
440 le32_to_cpu(mst->lsave_offs));
441 printk(KERN_DEBUG "\tlscan_lnum %u\n",
442 le32_to_cpu(mst->lscan_lnum));
443 printk(KERN_DEBUG "\tleb_cnt %u\n",
444 le32_to_cpu(mst->leb_cnt));
445 printk(KERN_DEBUG "\tempty_lebs %u\n",
446 le32_to_cpu(mst->empty_lebs));
447 printk(KERN_DEBUG "\tidx_lebs %u\n",
448 le32_to_cpu(mst->idx_lebs));
449 printk(KERN_DEBUG "\ttotal_free %llu\n",
450 (unsigned long long)le64_to_cpu(mst->total_free));
451 printk(KERN_DEBUG "\ttotal_dirty %llu\n",
452 (unsigned long long)le64_to_cpu(mst->total_dirty));
453 printk(KERN_DEBUG "\ttotal_used %llu\n",
454 (unsigned long long)le64_to_cpu(mst->total_used));
455 printk(KERN_DEBUG "\ttotal_dead %llu\n",
456 (unsigned long long)le64_to_cpu(mst->total_dead));
457 printk(KERN_DEBUG "\ttotal_dark %llu\n",
458 (unsigned long long)le64_to_cpu(mst->total_dark));
463 const struct ubifs_ref_node *ref = node;
465 printk(KERN_DEBUG "\tlnum %u\n",
466 le32_to_cpu(ref->lnum));
467 printk(KERN_DEBUG "\toffs %u\n",
468 le32_to_cpu(ref->offs));
469 printk(KERN_DEBUG "\tjhead %u\n",
470 le32_to_cpu(ref->jhead));
475 const struct ubifs_ino_node *ino = node;
477 key_read(c, &ino->key, &key);
478 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
479 printk(KERN_DEBUG "\tcreat_sqnum %llu\n",
480 (unsigned long long)le64_to_cpu(ino->creat_sqnum));
481 printk(KERN_DEBUG "\tsize %llu\n",
482 (unsigned long long)le64_to_cpu(ino->size));
483 printk(KERN_DEBUG "\tnlink %u\n",
484 le32_to_cpu(ino->nlink));
485 printk(KERN_DEBUG "\tatime %lld.%u\n",
486 (long long)le64_to_cpu(ino->atime_sec),
487 le32_to_cpu(ino->atime_nsec));
488 printk(KERN_DEBUG "\tmtime %lld.%u\n",
489 (long long)le64_to_cpu(ino->mtime_sec),
490 le32_to_cpu(ino->mtime_nsec));
491 printk(KERN_DEBUG "\tctime %lld.%u\n",
492 (long long)le64_to_cpu(ino->ctime_sec),
493 le32_to_cpu(ino->ctime_nsec));
494 printk(KERN_DEBUG "\tuid %u\n",
495 le32_to_cpu(ino->uid));
496 printk(KERN_DEBUG "\tgid %u\n",
497 le32_to_cpu(ino->gid));
498 printk(KERN_DEBUG "\tmode %u\n",
499 le32_to_cpu(ino->mode));
500 printk(KERN_DEBUG "\tflags %#x\n",
501 le32_to_cpu(ino->flags));
502 printk(KERN_DEBUG "\txattr_cnt %u\n",
503 le32_to_cpu(ino->xattr_cnt));
504 printk(KERN_DEBUG "\txattr_size %u\n",
505 le32_to_cpu(ino->xattr_size));
506 printk(KERN_DEBUG "\txattr_names %u\n",
507 le32_to_cpu(ino->xattr_names));
508 printk(KERN_DEBUG "\tcompr_type %#x\n",
509 (int)le16_to_cpu(ino->compr_type));
510 printk(KERN_DEBUG "\tdata len %u\n",
511 le32_to_cpu(ino->data_len));
514 case UBIFS_DENT_NODE:
515 case UBIFS_XENT_NODE:
517 const struct ubifs_dent_node *dent = node;
518 int nlen = le16_to_cpu(dent->nlen);
520 key_read(c, &dent->key, &key);
521 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
522 printk(KERN_DEBUG "\tinum %llu\n",
523 (unsigned long long)le64_to_cpu(dent->inum));
524 printk(KERN_DEBUG "\ttype %d\n", (int)dent->type);
525 printk(KERN_DEBUG "\tnlen %d\n", nlen);
526 printk(KERN_DEBUG "\tname ");
528 if (nlen > UBIFS_MAX_NLEN)
529 printk(KERN_DEBUG "(bad name length, not printing, "
530 "bad or corrupted node)");
532 for (i = 0; i < nlen && dent->name[i]; i++)
533 printk(KERN_CONT "%c", dent->name[i]);
535 printk(KERN_CONT "\n");
539 case UBIFS_DATA_NODE:
541 const struct ubifs_data_node *dn = node;
542 int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
544 key_read(c, &dn->key, &key);
545 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
546 printk(KERN_DEBUG "\tsize %u\n",
547 le32_to_cpu(dn->size));
548 printk(KERN_DEBUG "\tcompr_typ %d\n",
549 (int)le16_to_cpu(dn->compr_type));
550 printk(KERN_DEBUG "\tdata size %d\n",
552 printk(KERN_DEBUG "\tdata:\n");
553 print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET, 32, 1,
554 (void *)&dn->data, dlen, 0);
557 case UBIFS_TRUN_NODE:
559 const struct ubifs_trun_node *trun = node;
561 printk(KERN_DEBUG "\tinum %u\n",
562 le32_to_cpu(trun->inum));
563 printk(KERN_DEBUG "\told_size %llu\n",
564 (unsigned long long)le64_to_cpu(trun->old_size));
565 printk(KERN_DEBUG "\tnew_size %llu\n",
566 (unsigned long long)le64_to_cpu(trun->new_size));
571 const struct ubifs_idx_node *idx = node;
573 n = le16_to_cpu(idx->child_cnt);
574 printk(KERN_DEBUG "\tchild_cnt %d\n", n);
575 printk(KERN_DEBUG "\tlevel %d\n",
576 (int)le16_to_cpu(idx->level));
577 printk(KERN_DEBUG "\tBranches:\n");
579 for (i = 0; i < n && i < c->fanout - 1; i++) {
580 const struct ubifs_branch *br;
582 br = ubifs_idx_branch(c, idx, i);
583 key_read(c, &br->key, &key);
584 printk(KERN_DEBUG "\t%d: LEB %d:%d len %d key %s\n",
585 i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
586 le32_to_cpu(br->len), DBGKEY(&key));
592 case UBIFS_ORPH_NODE:
594 const struct ubifs_orph_node *orph = node;
596 printk(KERN_DEBUG "\tcommit number %llu\n",
598 le64_to_cpu(orph->cmt_no) & LLONG_MAX);
599 printk(KERN_DEBUG "\tlast node flag %llu\n",
600 (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
601 n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
602 printk(KERN_DEBUG "\t%d orphan inode numbers:\n", n);
603 for (i = 0; i < n; i++)
604 printk(KERN_DEBUG "\t ino %llu\n",
605 (unsigned long long)le64_to_cpu(orph->inos[i]));
609 printk(KERN_DEBUG "node type %d was not recognized\n",
612 spin_unlock(&dbg_lock);
615 void dbg_dump_budget_req(const struct ubifs_budget_req *req)
617 spin_lock(&dbg_lock);
618 printk(KERN_DEBUG "Budgeting request: new_ino %d, dirtied_ino %d\n",
619 req->new_ino, req->dirtied_ino);
620 printk(KERN_DEBUG "\tnew_ino_d %d, dirtied_ino_d %d\n",
621 req->new_ino_d, req->dirtied_ino_d);
622 printk(KERN_DEBUG "\tnew_page %d, dirtied_page %d\n",
623 req->new_page, req->dirtied_page);
624 printk(KERN_DEBUG "\tnew_dent %d, mod_dent %d\n",
625 req->new_dent, req->mod_dent);
626 printk(KERN_DEBUG "\tidx_growth %d\n", req->idx_growth);
627 printk(KERN_DEBUG "\tdata_growth %d dd_growth %d\n",
628 req->data_growth, req->dd_growth);
629 spin_unlock(&dbg_lock);
632 void dbg_dump_lstats(const struct ubifs_lp_stats *lst)
634 spin_lock(&dbg_lock);
635 printk(KERN_DEBUG "(pid %d) Lprops statistics: empty_lebs %d, "
636 "idx_lebs %d\n", current->pid, lst->empty_lebs, lst->idx_lebs);
637 printk(KERN_DEBUG "\ttaken_empty_lebs %d, total_free %lld, "
638 "total_dirty %lld\n", lst->taken_empty_lebs, lst->total_free,
640 printk(KERN_DEBUG "\ttotal_used %lld, total_dark %lld, "
641 "total_dead %lld\n", lst->total_used, lst->total_dark,
643 spin_unlock(&dbg_lock);
646 void dbg_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)
650 struct ubifs_bud *bud;
651 struct ubifs_gced_idx_leb *idx_gc;
652 long long available, outstanding, free;
654 spin_lock(&c->space_lock);
655 spin_lock(&dbg_lock);
656 printk(KERN_DEBUG "(pid %d) Budgeting info: data budget sum %lld, "
657 "total budget sum %lld\n", current->pid,
658 bi->data_growth + bi->dd_growth,
659 bi->data_growth + bi->dd_growth + bi->idx_growth);
660 printk(KERN_DEBUG "\tbudg_data_growth %lld, budg_dd_growth %lld, "
661 "budg_idx_growth %lld\n", bi->data_growth, bi->dd_growth,
663 printk(KERN_DEBUG "\tmin_idx_lebs %d, old_idx_sz %llu, "
664 "uncommitted_idx %lld\n", bi->min_idx_lebs, bi->old_idx_sz,
665 bi->uncommitted_idx);
666 printk(KERN_DEBUG "\tpage_budget %d, inode_budget %d, dent_budget %d\n",
667 bi->page_budget, bi->inode_budget, bi->dent_budget);
668 printk(KERN_DEBUG "\tnospace %u, nospace_rp %u\n",
669 bi->nospace, bi->nospace_rp);
670 printk(KERN_DEBUG "\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
671 c->dark_wm, c->dead_wm, c->max_idx_node_sz);
675 * If we are dumping saved budgeting data, do not print
676 * additional information which is about the current state, not
677 * the old one which corresponded to the saved budgeting data.
681 printk(KERN_DEBUG "\tfreeable_cnt %d, calc_idx_sz %lld, idx_gc_cnt %d\n",
682 c->freeable_cnt, c->calc_idx_sz, c->idx_gc_cnt);
683 printk(KERN_DEBUG "\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, "
684 "clean_zn_cnt %ld\n", atomic_long_read(&c->dirty_pg_cnt),
685 atomic_long_read(&c->dirty_zn_cnt),
686 atomic_long_read(&c->clean_zn_cnt));
687 printk(KERN_DEBUG "\tgc_lnum %d, ihead_lnum %d\n",
688 c->gc_lnum, c->ihead_lnum);
690 /* If we are in R/O mode, journal heads do not exist */
692 for (i = 0; i < c->jhead_cnt; i++)
693 printk(KERN_DEBUG "\tjhead %s\t LEB %d\n",
694 dbg_jhead(c->jheads[i].wbuf.jhead),
695 c->jheads[i].wbuf.lnum);
696 for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
697 bud = rb_entry(rb, struct ubifs_bud, rb);
698 printk(KERN_DEBUG "\tbud LEB %d\n", bud->lnum);
700 list_for_each_entry(bud, &c->old_buds, list)
701 printk(KERN_DEBUG "\told bud LEB %d\n", bud->lnum);
702 list_for_each_entry(idx_gc, &c->idx_gc, list)
703 printk(KERN_DEBUG "\tGC'ed idx LEB %d unmap %d\n",
704 idx_gc->lnum, idx_gc->unmap);
705 printk(KERN_DEBUG "\tcommit state %d\n", c->cmt_state);
707 /* Print budgeting predictions */
708 available = ubifs_calc_available(c, c->bi.min_idx_lebs);
709 outstanding = c->bi.data_growth + c->bi.dd_growth;
710 free = ubifs_get_free_space_nolock(c);
711 printk(KERN_DEBUG "Budgeting predictions:\n");
712 printk(KERN_DEBUG "\tavailable: %lld, outstanding %lld, free %lld\n",
713 available, outstanding, free);
715 spin_unlock(&dbg_lock);
716 spin_unlock(&c->space_lock);
719 void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
721 int i, spc, dark = 0, dead = 0;
723 struct ubifs_bud *bud;
725 spc = lp->free + lp->dirty;
726 if (spc < c->dead_wm)
729 dark = ubifs_calc_dark(c, spc);
731 if (lp->flags & LPROPS_INDEX)
732 printk(KERN_DEBUG "LEB %-7d free %-8d dirty %-8d used %-8d "
733 "free + dirty %-8d flags %#x (", lp->lnum, lp->free,
734 lp->dirty, c->leb_size - spc, spc, lp->flags);
736 printk(KERN_DEBUG "LEB %-7d free %-8d dirty %-8d used %-8d "
737 "free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d "
738 "flags %#-4x (", lp->lnum, lp->free, lp->dirty,
739 c->leb_size - spc, spc, dark, dead,
740 (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags);
742 if (lp->flags & LPROPS_TAKEN) {
743 if (lp->flags & LPROPS_INDEX)
744 printk(KERN_CONT "index, taken");
746 printk(KERN_CONT "taken");
750 if (lp->flags & LPROPS_INDEX) {
751 switch (lp->flags & LPROPS_CAT_MASK) {
752 case LPROPS_DIRTY_IDX:
755 case LPROPS_FRDI_IDX:
756 s = "freeable index";
762 switch (lp->flags & LPROPS_CAT_MASK) {
764 s = "not categorized";
775 case LPROPS_FREEABLE:
783 printk(KERN_CONT "%s", s);
786 for (rb = rb_first((struct rb_root *)&c->buds); rb; rb = rb_next(rb)) {
787 bud = rb_entry(rb, struct ubifs_bud, rb);
788 if (bud->lnum == lp->lnum) {
790 for (i = 0; i < c->jhead_cnt; i++) {
792 * Note, if we are in R/O mode or in the middle
793 * of mounting/re-mounting, the write-buffers do
797 lp->lnum == c->jheads[i].wbuf.lnum) {
798 printk(KERN_CONT ", jhead %s",
804 printk(KERN_CONT ", bud of jhead %s",
805 dbg_jhead(bud->jhead));
808 if (lp->lnum == c->gc_lnum)
809 printk(KERN_CONT ", GC LEB");
810 printk(KERN_CONT ")\n");
813 void dbg_dump_lprops(struct ubifs_info *c)
816 struct ubifs_lprops lp;
817 struct ubifs_lp_stats lst;
819 printk(KERN_DEBUG "(pid %d) start dumping LEB properties\n",
821 ubifs_get_lp_stats(c, &lst);
822 dbg_dump_lstats(&lst);
824 for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
825 err = ubifs_read_one_lp(c, lnum, &lp);
827 ubifs_err("cannot read lprops for LEB %d", lnum);
829 dbg_dump_lprop(c, &lp);
831 printk(KERN_DEBUG "(pid %d) finish dumping LEB properties\n",
835 void dbg_dump_lpt_info(struct ubifs_info *c)
839 spin_lock(&dbg_lock);
840 printk(KERN_DEBUG "(pid %d) dumping LPT information\n", current->pid);
841 printk(KERN_DEBUG "\tlpt_sz: %lld\n", c->lpt_sz);
842 printk(KERN_DEBUG "\tpnode_sz: %d\n", c->pnode_sz);
843 printk(KERN_DEBUG "\tnnode_sz: %d\n", c->nnode_sz);
844 printk(KERN_DEBUG "\tltab_sz: %d\n", c->ltab_sz);
845 printk(KERN_DEBUG "\tlsave_sz: %d\n", c->lsave_sz);
846 printk(KERN_DEBUG "\tbig_lpt: %d\n", c->big_lpt);
847 printk(KERN_DEBUG "\tlpt_hght: %d\n", c->lpt_hght);
848 printk(KERN_DEBUG "\tpnode_cnt: %d\n", c->pnode_cnt);
849 printk(KERN_DEBUG "\tnnode_cnt: %d\n", c->nnode_cnt);
850 printk(KERN_DEBUG "\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt);
851 printk(KERN_DEBUG "\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt);
852 printk(KERN_DEBUG "\tlsave_cnt: %d\n", c->lsave_cnt);
853 printk(KERN_DEBUG "\tspace_bits: %d\n", c->space_bits);
854 printk(KERN_DEBUG "\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
855 printk(KERN_DEBUG "\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
856 printk(KERN_DEBUG "\tlpt_spc_bits: %d\n", c->lpt_spc_bits);
857 printk(KERN_DEBUG "\tpcnt_bits: %d\n", c->pcnt_bits);
858 printk(KERN_DEBUG "\tlnum_bits: %d\n", c->lnum_bits);
859 printk(KERN_DEBUG "\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
860 printk(KERN_DEBUG "\tLPT head is at %d:%d\n",
861 c->nhead_lnum, c->nhead_offs);
862 printk(KERN_DEBUG "\tLPT ltab is at %d:%d\n",
863 c->ltab_lnum, c->ltab_offs);
865 printk(KERN_DEBUG "\tLPT lsave is at %d:%d\n",
866 c->lsave_lnum, c->lsave_offs);
867 for (i = 0; i < c->lpt_lebs; i++)
868 printk(KERN_DEBUG "\tLPT LEB %d free %d dirty %d tgc %d "
869 "cmt %d\n", i + c->lpt_first, c->ltab[i].free,
870 c->ltab[i].dirty, c->ltab[i].tgc, c->ltab[i].cmt);
871 spin_unlock(&dbg_lock);
874 void dbg_dump_leb(const struct ubifs_info *c, int lnum)
876 struct ubifs_scan_leb *sleb;
877 struct ubifs_scan_node *snod;
880 if (dbg_is_tst_rcvry(c))
883 printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n",
886 buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
888 ubifs_err("cannot allocate memory for dumping LEB %d", lnum);
892 sleb = ubifs_scan(c, lnum, 0, buf, 0);
894 ubifs_err("scan error %d", (int)PTR_ERR(sleb));
898 printk(KERN_DEBUG "LEB %d has %d nodes ending at %d\n", lnum,
899 sleb->nodes_cnt, sleb->endpt);
901 list_for_each_entry(snod, &sleb->nodes, list) {
903 printk(KERN_DEBUG "Dumping node at LEB %d:%d len %d\n", lnum,
904 snod->offs, snod->len);
905 dbg_dump_node(c, snod->node);
908 printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n",
910 ubifs_scan_destroy(sleb);
917 void dbg_dump_znode(const struct ubifs_info *c,
918 const struct ubifs_znode *znode)
921 const struct ubifs_zbranch *zbr;
923 spin_lock(&dbg_lock);
925 zbr = &znode->parent->zbranch[znode->iip];
929 printk(KERN_DEBUG "znode %p, LEB %d:%d len %d parent %p iip %d level %d"
930 " child_cnt %d flags %lx\n", znode, zbr->lnum, zbr->offs,
931 zbr->len, znode->parent, znode->iip, znode->level,
932 znode->child_cnt, znode->flags);
934 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
935 spin_unlock(&dbg_lock);
939 printk(KERN_DEBUG "zbranches:\n");
940 for (n = 0; n < znode->child_cnt; n++) {
941 zbr = &znode->zbranch[n];
942 if (znode->level > 0)
943 printk(KERN_DEBUG "\t%d: znode %p LEB %d:%d len %d key "
944 "%s\n", n, zbr->znode, zbr->lnum,
948 printk(KERN_DEBUG "\t%d: LNC %p LEB %d:%d len %d key "
949 "%s\n", n, zbr->znode, zbr->lnum,
953 spin_unlock(&dbg_lock);
956 void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
960 printk(KERN_DEBUG "(pid %d) start dumping heap cat %d (%d elements)\n",
961 current->pid, cat, heap->cnt);
962 for (i = 0; i < heap->cnt; i++) {
963 struct ubifs_lprops *lprops = heap->arr[i];
965 printk(KERN_DEBUG "\t%d. LEB %d hpos %d free %d dirty %d "
966 "flags %d\n", i, lprops->lnum, lprops->hpos,
967 lprops->free, lprops->dirty, lprops->flags);
969 printk(KERN_DEBUG "(pid %d) finish dumping heap\n", current->pid);
972 void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
973 struct ubifs_nnode *parent, int iip)
977 printk(KERN_DEBUG "(pid %d) dumping pnode:\n", current->pid);
978 printk(KERN_DEBUG "\taddress %zx parent %zx cnext %zx\n",
979 (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
980 printk(KERN_DEBUG "\tflags %lu iip %d level %d num %d\n",
981 pnode->flags, iip, pnode->level, pnode->num);
982 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
983 struct ubifs_lprops *lp = &pnode->lprops[i];
985 printk(KERN_DEBUG "\t%d: free %d dirty %d flags %d lnum %d\n",
986 i, lp->free, lp->dirty, lp->flags, lp->lnum);
990 void dbg_dump_tnc(struct ubifs_info *c)
992 struct ubifs_znode *znode;
995 printk(KERN_DEBUG "\n");
996 printk(KERN_DEBUG "(pid %d) start dumping TNC tree\n", current->pid);
997 znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
998 level = znode->level;
999 printk(KERN_DEBUG "== Level %d ==\n", level);
1001 if (level != znode->level) {
1002 level = znode->level;
1003 printk(KERN_DEBUG "== Level %d ==\n", level);
1005 dbg_dump_znode(c, znode);
1006 znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
1008 printk(KERN_DEBUG "(pid %d) finish dumping TNC tree\n", current->pid);
1011 static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
1014 dbg_dump_znode(c, znode);
1019 * dbg_dump_index - dump the on-flash index.
1020 * @c: UBIFS file-system description object
1022 * This function dumps whole UBIFS indexing B-tree, unlike 'dbg_dump_tnc()'
1023 * which dumps only in-memory znodes and does not read znodes which from flash.
1025 void dbg_dump_index(struct ubifs_info *c)
1027 dbg_walk_index(c, NULL, dump_znode, NULL);
1031 * dbg_save_space_info - save information about flash space.
1032 * @c: UBIFS file-system description object
1034 * This function saves information about UBIFS free space, dirty space, etc, in
1035 * order to check it later.
1037 void dbg_save_space_info(struct ubifs_info *c)
1039 struct ubifs_debug_info *d = c->dbg;
1042 spin_lock(&c->space_lock);
1043 memcpy(&d->saved_lst, &c->lst, sizeof(struct ubifs_lp_stats));
1044 memcpy(&d->saved_bi, &c->bi, sizeof(struct ubifs_budg_info));
1045 d->saved_idx_gc_cnt = c->idx_gc_cnt;
1048 * We use a dirty hack here and zero out @c->freeable_cnt, because it
1049 * affects the free space calculations, and UBIFS might not know about
1050 * all freeable eraseblocks. Indeed, we know about freeable eraseblocks
1051 * only when we read their lprops, and we do this only lazily, upon the
1052 * need. So at any given point of time @c->freeable_cnt might be not
1055 * Just one example about the issue we hit when we did not zero
1057 * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the
1058 * amount of free space in @d->saved_free
1059 * 2. We re-mount R/W, which makes UBIFS to read the "lsave"
1060 * information from flash, where we cache LEBs from various
1061 * categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()'
1062 * -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()'
1063 * -> 'ubifs_get_pnode()' -> 'update_cats()'
1064 * -> 'ubifs_add_to_cat()').
1065 * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt
1067 * 4. We calculate the amount of free space when the re-mount is
1068 * finished in 'dbg_check_space_info()' and it does not match
1071 freeable_cnt = c->freeable_cnt;
1072 c->freeable_cnt = 0;
1073 d->saved_free = ubifs_get_free_space_nolock(c);
1074 c->freeable_cnt = freeable_cnt;
1075 spin_unlock(&c->space_lock);
1079 * dbg_check_space_info - check flash space information.
1080 * @c: UBIFS file-system description object
1082 * This function compares current flash space information with the information
1083 * which was saved when the 'dbg_save_space_info()' function was called.
1084 * Returns zero if the information has not changed, and %-EINVAL it it has
1087 int dbg_check_space_info(struct ubifs_info *c)
1089 struct ubifs_debug_info *d = c->dbg;
1090 struct ubifs_lp_stats lst;
1094 spin_lock(&c->space_lock);
1095 freeable_cnt = c->freeable_cnt;
1096 c->freeable_cnt = 0;
1097 free = ubifs_get_free_space_nolock(c);
1098 c->freeable_cnt = freeable_cnt;
1099 spin_unlock(&c->space_lock);
1101 if (free != d->saved_free) {
1102 ubifs_err("free space changed from %lld to %lld",
1103 d->saved_free, free);
1110 ubifs_msg("saved lprops statistics dump");
1111 dbg_dump_lstats(&d->saved_lst);
1112 ubifs_msg("saved budgeting info dump");
1113 dbg_dump_budg(c, &d->saved_bi);
1114 ubifs_msg("saved idx_gc_cnt %d", d->saved_idx_gc_cnt);
1115 ubifs_msg("current lprops statistics dump");
1116 ubifs_get_lp_stats(c, &lst);
1117 dbg_dump_lstats(&lst);
1118 ubifs_msg("current budgeting info dump");
1119 dbg_dump_budg(c, &c->bi);
1125 * dbg_check_synced_i_size - check synchronized inode size.
1126 * @c: UBIFS file-system description object
1127 * @inode: inode to check
1129 * If inode is clean, synchronized inode size has to be equivalent to current
1130 * inode size. This function has to be called only for locked inodes (@i_mutex
1131 * has to be locked). Returns %0 if synchronized inode size if correct, and
1134 int dbg_check_synced_i_size(const struct ubifs_info *c, struct inode *inode)
1137 struct ubifs_inode *ui = ubifs_inode(inode);
1139 if (!dbg_is_chk_gen(c))
1141 if (!S_ISREG(inode->i_mode))
1144 mutex_lock(&ui->ui_mutex);
1145 spin_lock(&ui->ui_lock);
1146 if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
1147 ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode "
1148 "is clean", ui->ui_size, ui->synced_i_size);
1149 ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
1150 inode->i_mode, i_size_read(inode));
1154 spin_unlock(&ui->ui_lock);
1155 mutex_unlock(&ui->ui_mutex);
1160 * dbg_check_dir - check directory inode size and link count.
1161 * @c: UBIFS file-system description object
1162 * @dir: the directory to calculate size for
1163 * @size: the result is returned here
1165 * This function makes sure that directory size and link count are correct.
1166 * Returns zero in case of success and a negative error code in case of
1169 * Note, it is good idea to make sure the @dir->i_mutex is locked before
1170 * calling this function.
1172 int dbg_check_dir(struct ubifs_info *c, const struct inode *dir)
1174 unsigned int nlink = 2;
1175 union ubifs_key key;
1176 struct ubifs_dent_node *dent, *pdent = NULL;
1177 struct qstr nm = { .name = NULL };
1178 loff_t size = UBIFS_INO_NODE_SZ;
1180 if (!dbg_is_chk_gen(c))
1183 if (!S_ISDIR(dir->i_mode))
1186 lowest_dent_key(c, &key, dir->i_ino);
1190 dent = ubifs_tnc_next_ent(c, &key, &nm);
1192 err = PTR_ERR(dent);
1198 nm.name = dent->name;
1199 nm.len = le16_to_cpu(dent->nlen);
1200 size += CALC_DENT_SIZE(nm.len);
1201 if (dent->type == UBIFS_ITYPE_DIR)
1205 key_read(c, &dent->key, &key);
1209 if (i_size_read(dir) != size) {
1210 ubifs_err("directory inode %lu has size %llu, "
1211 "but calculated size is %llu", dir->i_ino,
1212 (unsigned long long)i_size_read(dir),
1213 (unsigned long long)size);
1214 dbg_dump_inode(c, dir);
1218 if (dir->i_nlink != nlink) {
1219 ubifs_err("directory inode %lu has nlink %u, but calculated "
1220 "nlink is %u", dir->i_ino, dir->i_nlink, nlink);
1221 dbg_dump_inode(c, dir);
1230 * dbg_check_key_order - make sure that colliding keys are properly ordered.
1231 * @c: UBIFS file-system description object
1232 * @zbr1: first zbranch
1233 * @zbr2: following zbranch
1235 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
1236 * names of the direntries/xentries which are referred by the keys. This
1237 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
1238 * sure the name of direntry/xentry referred by @zbr1 is less than
1239 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
1240 * and a negative error code in case of failure.
1242 static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
1243 struct ubifs_zbranch *zbr2)
1245 int err, nlen1, nlen2, cmp;
1246 struct ubifs_dent_node *dent1, *dent2;
1247 union ubifs_key key;
1249 ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
1250 dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1253 dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1259 err = ubifs_tnc_read_node(c, zbr1, dent1);
1262 err = ubifs_validate_entry(c, dent1);
1266 err = ubifs_tnc_read_node(c, zbr2, dent2);
1269 err = ubifs_validate_entry(c, dent2);
1273 /* Make sure node keys are the same as in zbranch */
1275 key_read(c, &dent1->key, &key);
1276 if (keys_cmp(c, &zbr1->key, &key)) {
1277 dbg_err("1st entry at %d:%d has key %s", zbr1->lnum,
1278 zbr1->offs, DBGKEY(&key));
1279 dbg_err("but it should have key %s according to tnc",
1280 DBGKEY(&zbr1->key));
1281 dbg_dump_node(c, dent1);
1285 key_read(c, &dent2->key, &key);
1286 if (keys_cmp(c, &zbr2->key, &key)) {
1287 dbg_err("2nd entry at %d:%d has key %s", zbr1->lnum,
1288 zbr1->offs, DBGKEY(&key));
1289 dbg_err("but it should have key %s according to tnc",
1290 DBGKEY(&zbr2->key));
1291 dbg_dump_node(c, dent2);
1295 nlen1 = le16_to_cpu(dent1->nlen);
1296 nlen2 = le16_to_cpu(dent2->nlen);
1298 cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
1299 if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
1303 if (cmp == 0 && nlen1 == nlen2)
1304 dbg_err("2 xent/dent nodes with the same name");
1306 dbg_err("bad order of colliding key %s",
1309 ubifs_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs);
1310 dbg_dump_node(c, dent1);
1311 ubifs_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs);
1312 dbg_dump_node(c, dent2);
1321 * dbg_check_znode - check if znode is all right.
1322 * @c: UBIFS file-system description object
1323 * @zbr: zbranch which points to this znode
1325 * This function makes sure that znode referred to by @zbr is all right.
1326 * Returns zero if it is, and %-EINVAL if it is not.
1328 static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
1330 struct ubifs_znode *znode = zbr->znode;
1331 struct ubifs_znode *zp = znode->parent;
1334 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
1338 if (znode->level < 0) {
1342 if (znode->iip < 0 || znode->iip >= c->fanout) {
1348 /* Only dirty zbranch may have no on-flash nodes */
1349 if (!ubifs_zn_dirty(znode)) {
1354 if (ubifs_zn_dirty(znode)) {
1356 * If znode is dirty, its parent has to be dirty as well. The
1357 * order of the operation is important, so we have to have
1361 if (zp && !ubifs_zn_dirty(zp)) {
1363 * The dirty flag is atomic and is cleared outside the
1364 * TNC mutex, so znode's dirty flag may now have
1365 * been cleared. The child is always cleared before the
1366 * parent, so we just need to check again.
1369 if (ubifs_zn_dirty(znode)) {
1377 const union ubifs_key *min, *max;
1379 if (znode->level != zp->level - 1) {
1384 /* Make sure the 'parent' pointer in our znode is correct */
1385 err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
1387 /* This zbranch does not exist in the parent */
1392 if (znode->iip >= zp->child_cnt) {
1397 if (znode->iip != n) {
1398 /* This may happen only in case of collisions */
1399 if (keys_cmp(c, &zp->zbranch[n].key,
1400 &zp->zbranch[znode->iip].key)) {
1408 * Make sure that the first key in our znode is greater than or
1409 * equal to the key in the pointing zbranch.
1412 cmp = keys_cmp(c, min, &znode->zbranch[0].key);
1418 if (n + 1 < zp->child_cnt) {
1419 max = &zp->zbranch[n + 1].key;
1422 * Make sure the last key in our znode is less or
1423 * equivalent than the key in the zbranch which goes
1424 * after our pointing zbranch.
1426 cmp = keys_cmp(c, max,
1427 &znode->zbranch[znode->child_cnt - 1].key);
1434 /* This may only be root znode */
1435 if (zbr != &c->zroot) {
1442 * Make sure that next key is greater or equivalent then the previous
1445 for (n = 1; n < znode->child_cnt; n++) {
1446 cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
1447 &znode->zbranch[n].key);
1453 /* This can only be keys with colliding hash */
1454 if (!is_hash_key(c, &znode->zbranch[n].key)) {
1459 if (znode->level != 0 || c->replaying)
1463 * Colliding keys should follow binary order of
1464 * corresponding xentry/dentry names.
1466 err = dbg_check_key_order(c, &znode->zbranch[n - 1],
1467 &znode->zbranch[n]);
1477 for (n = 0; n < znode->child_cnt; n++) {
1478 if (!znode->zbranch[n].znode &&
1479 (znode->zbranch[n].lnum == 0 ||
1480 znode->zbranch[n].len == 0)) {
1485 if (znode->zbranch[n].lnum != 0 &&
1486 znode->zbranch[n].len == 0) {
1491 if (znode->zbranch[n].lnum == 0 &&
1492 znode->zbranch[n].len != 0) {
1497 if (znode->zbranch[n].lnum == 0 &&
1498 znode->zbranch[n].offs != 0) {
1503 if (znode->level != 0 && znode->zbranch[n].znode)
1504 if (znode->zbranch[n].znode->parent != znode) {
1513 ubifs_err("failed, error %d", err);
1514 ubifs_msg("dump of the znode");
1515 dbg_dump_znode(c, znode);
1517 ubifs_msg("dump of the parent znode");
1518 dbg_dump_znode(c, zp);
1525 * dbg_check_tnc - check TNC tree.
1526 * @c: UBIFS file-system description object
1527 * @extra: do extra checks that are possible at start commit
1529 * This function traverses whole TNC tree and checks every znode. Returns zero
1530 * if everything is all right and %-EINVAL if something is wrong with TNC.
1532 int dbg_check_tnc(struct ubifs_info *c, int extra)
1534 struct ubifs_znode *znode;
1535 long clean_cnt = 0, dirty_cnt = 0;
1538 if (!dbg_is_chk_index(c))
1541 ubifs_assert(mutex_is_locked(&c->tnc_mutex));
1542 if (!c->zroot.znode)
1545 znode = ubifs_tnc_postorder_first(c->zroot.znode);
1547 struct ubifs_znode *prev;
1548 struct ubifs_zbranch *zbr;
1553 zbr = &znode->parent->zbranch[znode->iip];
1555 err = dbg_check_znode(c, zbr);
1560 if (ubifs_zn_dirty(znode))
1567 znode = ubifs_tnc_postorder_next(znode);
1572 * If the last key of this znode is equivalent to the first key
1573 * of the next znode (collision), then check order of the keys.
1575 last = prev->child_cnt - 1;
1576 if (prev->level == 0 && znode->level == 0 && !c->replaying &&
1577 !keys_cmp(c, &prev->zbranch[last].key,
1578 &znode->zbranch[0].key)) {
1579 err = dbg_check_key_order(c, &prev->zbranch[last],
1580 &znode->zbranch[0]);
1584 ubifs_msg("first znode");
1585 dbg_dump_znode(c, prev);
1586 ubifs_msg("second znode");
1587 dbg_dump_znode(c, znode);
1594 if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
1595 ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld",
1596 atomic_long_read(&c->clean_zn_cnt),
1600 if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
1601 ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld",
1602 atomic_long_read(&c->dirty_zn_cnt),
1612 * dbg_walk_index - walk the on-flash index.
1613 * @c: UBIFS file-system description object
1614 * @leaf_cb: called for each leaf node
1615 * @znode_cb: called for each indexing node
1616 * @priv: private data which is passed to callbacks
1618 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1619 * node and @znode_cb for each indexing node. Returns zero in case of success
1620 * and a negative error code in case of failure.
1622 * It would be better if this function removed every znode it pulled to into
1623 * the TNC, so that the behavior more closely matched the non-debugging
1626 int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1627 dbg_znode_callback znode_cb, void *priv)
1630 struct ubifs_zbranch *zbr;
1631 struct ubifs_znode *znode, *child;
1633 mutex_lock(&c->tnc_mutex);
1634 /* If the root indexing node is not in TNC - pull it */
1635 if (!c->zroot.znode) {
1636 c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1637 if (IS_ERR(c->zroot.znode)) {
1638 err = PTR_ERR(c->zroot.znode);
1639 c->zroot.znode = NULL;
1645 * We are going to traverse the indexing tree in the postorder manner.
1646 * Go down and find the leftmost indexing node where we are going to
1649 znode = c->zroot.znode;
1650 while (znode->level > 0) {
1651 zbr = &znode->zbranch[0];
1654 child = ubifs_load_znode(c, zbr, znode, 0);
1655 if (IS_ERR(child)) {
1656 err = PTR_ERR(child);
1665 /* Iterate over all indexing nodes */
1672 err = znode_cb(c, znode, priv);
1674 ubifs_err("znode checking function returned "
1676 dbg_dump_znode(c, znode);
1680 if (leaf_cb && znode->level == 0) {
1681 for (idx = 0; idx < znode->child_cnt; idx++) {
1682 zbr = &znode->zbranch[idx];
1683 err = leaf_cb(c, zbr, priv);
1685 ubifs_err("leaf checking function "
1686 "returned error %d, for leaf "
1688 err, zbr->lnum, zbr->offs);
1697 idx = znode->iip + 1;
1698 znode = znode->parent;
1699 if (idx < znode->child_cnt) {
1700 /* Switch to the next index in the parent */
1701 zbr = &znode->zbranch[idx];
1704 child = ubifs_load_znode(c, zbr, znode, idx);
1705 if (IS_ERR(child)) {
1706 err = PTR_ERR(child);
1714 * This is the last child, switch to the parent and
1719 /* Go to the lowest leftmost znode in the new sub-tree */
1720 while (znode->level > 0) {
1721 zbr = &znode->zbranch[0];
1724 child = ubifs_load_znode(c, zbr, znode, 0);
1725 if (IS_ERR(child)) {
1726 err = PTR_ERR(child);
1735 mutex_unlock(&c->tnc_mutex);
1740 zbr = &znode->parent->zbranch[znode->iip];
1743 ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1744 dbg_dump_znode(c, znode);
1746 mutex_unlock(&c->tnc_mutex);
1751 * add_size - add znode size to partially calculated index size.
1752 * @c: UBIFS file-system description object
1753 * @znode: znode to add size for
1754 * @priv: partially calculated index size
1756 * This is a helper function for 'dbg_check_idx_size()' which is called for
1757 * every indexing node and adds its size to the 'long long' variable pointed to
1760 static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1762 long long *idx_size = priv;
1765 add = ubifs_idx_node_sz(c, znode->child_cnt);
1766 add = ALIGN(add, 8);
1772 * dbg_check_idx_size - check index size.
1773 * @c: UBIFS file-system description object
1774 * @idx_size: size to check
1776 * This function walks the UBIFS index, calculates its size and checks that the
1777 * size is equivalent to @idx_size. Returns zero in case of success and a
1778 * negative error code in case of failure.
1780 int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
1785 if (!dbg_is_chk_index(c))
1788 err = dbg_walk_index(c, NULL, add_size, &calc);
1790 ubifs_err("error %d while walking the index", err);
1794 if (calc != idx_size) {
1795 ubifs_err("index size check failed: calculated size is %lld, "
1796 "should be %lld", calc, idx_size);
1805 * struct fsck_inode - information about an inode used when checking the file-system.
1806 * @rb: link in the RB-tree of inodes
1807 * @inum: inode number
1808 * @mode: inode type, permissions, etc
1809 * @nlink: inode link count
1810 * @xattr_cnt: count of extended attributes
1811 * @references: how many directory/xattr entries refer this inode (calculated
1812 * while walking the index)
1813 * @calc_cnt: for directory inode count of child directories
1814 * @size: inode size (read from on-flash inode)
1815 * @xattr_sz: summary size of all extended attributes (read from on-flash
1817 * @calc_sz: for directories calculated directory size
1818 * @calc_xcnt: count of extended attributes
1819 * @calc_xsz: calculated summary size of all extended attributes
1820 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1821 * inode (read from on-flash inode)
1822 * @calc_xnms: calculated sum of lengths of all extended attribute names
1829 unsigned int xattr_cnt;
1833 unsigned int xattr_sz;
1835 long long calc_xcnt;
1837 unsigned int xattr_nms;
1838 long long calc_xnms;
1842 * struct fsck_data - private FS checking information.
1843 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1846 struct rb_root inodes;
1850 * add_inode - add inode information to RB-tree of inodes.
1851 * @c: UBIFS file-system description object
1852 * @fsckd: FS checking information
1853 * @ino: raw UBIFS inode to add
1855 * This is a helper function for 'check_leaf()' which adds information about
1856 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1857 * case of success and a negative error code in case of failure.
1859 static struct fsck_inode *add_inode(struct ubifs_info *c,
1860 struct fsck_data *fsckd,
1861 struct ubifs_ino_node *ino)
1863 struct rb_node **p, *parent = NULL;
1864 struct fsck_inode *fscki;
1865 ino_t inum = key_inum_flash(c, &ino->key);
1866 struct inode *inode;
1867 struct ubifs_inode *ui;
1869 p = &fsckd->inodes.rb_node;
1872 fscki = rb_entry(parent, struct fsck_inode, rb);
1873 if (inum < fscki->inum)
1875 else if (inum > fscki->inum)
1876 p = &(*p)->rb_right;
1881 if (inum > c->highest_inum) {
1882 ubifs_err("too high inode number, max. is %lu",
1883 (unsigned long)c->highest_inum);
1884 return ERR_PTR(-EINVAL);
1887 fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
1889 return ERR_PTR(-ENOMEM);
1891 inode = ilookup(c->vfs_sb, inum);
1895 * If the inode is present in the VFS inode cache, use it instead of
1896 * the on-flash inode which might be out-of-date. E.g., the size might
1897 * be out-of-date. If we do not do this, the following may happen, for
1899 * 1. A power cut happens
1900 * 2. We mount the file-system R/O, the replay process fixes up the
1901 * inode size in the VFS cache, but on on-flash.
1902 * 3. 'check_leaf()' fails because it hits a data node beyond inode
1906 fscki->nlink = le32_to_cpu(ino->nlink);
1907 fscki->size = le64_to_cpu(ino->size);
1908 fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
1909 fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
1910 fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
1911 fscki->mode = le32_to_cpu(ino->mode);
1913 ui = ubifs_inode(inode);
1914 fscki->nlink = inode->i_nlink;
1915 fscki->size = inode->i_size;
1916 fscki->xattr_cnt = ui->xattr_cnt;
1917 fscki->xattr_sz = ui->xattr_size;
1918 fscki->xattr_nms = ui->xattr_names;
1919 fscki->mode = inode->i_mode;
1923 if (S_ISDIR(fscki->mode)) {
1924 fscki->calc_sz = UBIFS_INO_NODE_SZ;
1925 fscki->calc_cnt = 2;
1928 rb_link_node(&fscki->rb, parent, p);
1929 rb_insert_color(&fscki->rb, &fsckd->inodes);
1935 * search_inode - search inode in the RB-tree of inodes.
1936 * @fsckd: FS checking information
1937 * @inum: inode number to search
1939 * This is a helper function for 'check_leaf()' which searches inode @inum in
1940 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1941 * the inode was not found.
1943 static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
1946 struct fsck_inode *fscki;
1948 p = fsckd->inodes.rb_node;
1950 fscki = rb_entry(p, struct fsck_inode, rb);
1951 if (inum < fscki->inum)
1953 else if (inum > fscki->inum)
1962 * read_add_inode - read inode node and add it to RB-tree of inodes.
1963 * @c: UBIFS file-system description object
1964 * @fsckd: FS checking information
1965 * @inum: inode number to read
1967 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1968 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1969 * information pointer in case of success and a negative error code in case of
1972 static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1973 struct fsck_data *fsckd, ino_t inum)
1976 union ubifs_key key;
1977 struct ubifs_znode *znode;
1978 struct ubifs_zbranch *zbr;
1979 struct ubifs_ino_node *ino;
1980 struct fsck_inode *fscki;
1982 fscki = search_inode(fsckd, inum);
1986 ino_key_init(c, &key, inum);
1987 err = ubifs_lookup_level0(c, &key, &znode, &n);
1989 ubifs_err("inode %lu not found in index", (unsigned long)inum);
1990 return ERR_PTR(-ENOENT);
1991 } else if (err < 0) {
1992 ubifs_err("error %d while looking up inode %lu",
1993 err, (unsigned long)inum);
1994 return ERR_PTR(err);
1997 zbr = &znode->zbranch[n];
1998 if (zbr->len < UBIFS_INO_NODE_SZ) {
1999 ubifs_err("bad node %lu node length %d",
2000 (unsigned long)inum, zbr->len);
2001 return ERR_PTR(-EINVAL);
2004 ino = kmalloc(zbr->len, GFP_NOFS);
2006 return ERR_PTR(-ENOMEM);
2008 err = ubifs_tnc_read_node(c, zbr, ino);
2010 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2011 zbr->lnum, zbr->offs, err);
2013 return ERR_PTR(err);
2016 fscki = add_inode(c, fsckd, ino);
2018 if (IS_ERR(fscki)) {
2019 ubifs_err("error %ld while adding inode %lu node",
2020 PTR_ERR(fscki), (unsigned long)inum);
2028 * check_leaf - check leaf node.
2029 * @c: UBIFS file-system description object
2030 * @zbr: zbranch of the leaf node to check
2031 * @priv: FS checking information
2033 * This is a helper function for 'dbg_check_filesystem()' which is called for
2034 * every single leaf node while walking the indexing tree. It checks that the
2035 * leaf node referred from the indexing tree exists, has correct CRC, and does
2036 * some other basic validation. This function is also responsible for building
2037 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
2038 * calculates reference count, size, etc for each inode in order to later
2039 * compare them to the information stored inside the inodes and detect possible
2040 * inconsistencies. Returns zero in case of success and a negative error code
2041 * in case of failure.
2043 static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
2048 struct ubifs_ch *ch;
2049 int err, type = key_type(c, &zbr->key);
2050 struct fsck_inode *fscki;
2052 if (zbr->len < UBIFS_CH_SZ) {
2053 ubifs_err("bad leaf length %d (LEB %d:%d)",
2054 zbr->len, zbr->lnum, zbr->offs);
2058 node = kmalloc(zbr->len, GFP_NOFS);
2062 err = ubifs_tnc_read_node(c, zbr, node);
2064 ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
2065 zbr->lnum, zbr->offs, err);
2069 /* If this is an inode node, add it to RB-tree of inodes */
2070 if (type == UBIFS_INO_KEY) {
2071 fscki = add_inode(c, priv, node);
2072 if (IS_ERR(fscki)) {
2073 err = PTR_ERR(fscki);
2074 ubifs_err("error %d while adding inode node", err);
2080 if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
2081 type != UBIFS_DATA_KEY) {
2082 ubifs_err("unexpected node type %d at LEB %d:%d",
2083 type, zbr->lnum, zbr->offs);
2089 if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
2090 ubifs_err("too high sequence number, max. is %llu",
2096 if (type == UBIFS_DATA_KEY) {
2098 struct ubifs_data_node *dn = node;
2101 * Search the inode node this data node belongs to and insert
2102 * it to the RB-tree of inodes.
2104 inum = key_inum_flash(c, &dn->key);
2105 fscki = read_add_inode(c, priv, inum);
2106 if (IS_ERR(fscki)) {
2107 err = PTR_ERR(fscki);
2108 ubifs_err("error %d while processing data node and "
2109 "trying to find inode node %lu",
2110 err, (unsigned long)inum);
2114 /* Make sure the data node is within inode size */
2115 blk_offs = key_block_flash(c, &dn->key);
2116 blk_offs <<= UBIFS_BLOCK_SHIFT;
2117 blk_offs += le32_to_cpu(dn->size);
2118 if (blk_offs > fscki->size) {
2119 ubifs_err("data node at LEB %d:%d is not within inode "
2120 "size %lld", zbr->lnum, zbr->offs,
2127 struct ubifs_dent_node *dent = node;
2128 struct fsck_inode *fscki1;
2130 err = ubifs_validate_entry(c, dent);
2135 * Search the inode node this entry refers to and the parent
2136 * inode node and insert them to the RB-tree of inodes.
2138 inum = le64_to_cpu(dent->inum);
2139 fscki = read_add_inode(c, priv, inum);
2140 if (IS_ERR(fscki)) {
2141 err = PTR_ERR(fscki);
2142 ubifs_err("error %d while processing entry node and "
2143 "trying to find inode node %lu",
2144 err, (unsigned long)inum);
2148 /* Count how many direntries or xentries refers this inode */
2149 fscki->references += 1;
2151 inum = key_inum_flash(c, &dent->key);
2152 fscki1 = read_add_inode(c, priv, inum);
2153 if (IS_ERR(fscki1)) {
2154 err = PTR_ERR(fscki1);
2155 ubifs_err("error %d while processing entry node and "
2156 "trying to find parent inode node %lu",
2157 err, (unsigned long)inum);
2161 nlen = le16_to_cpu(dent->nlen);
2162 if (type == UBIFS_XENT_KEY) {
2163 fscki1->calc_xcnt += 1;
2164 fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
2165 fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
2166 fscki1->calc_xnms += nlen;
2168 fscki1->calc_sz += CALC_DENT_SIZE(nlen);
2169 if (dent->type == UBIFS_ITYPE_DIR)
2170 fscki1->calc_cnt += 1;
2179 ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
2180 dbg_dump_node(c, node);
2187 * free_inodes - free RB-tree of inodes.
2188 * @fsckd: FS checking information
2190 static void free_inodes(struct fsck_data *fsckd)
2192 struct rb_node *this = fsckd->inodes.rb_node;
2193 struct fsck_inode *fscki;
2197 this = this->rb_left;
2198 else if (this->rb_right)
2199 this = this->rb_right;
2201 fscki = rb_entry(this, struct fsck_inode, rb);
2202 this = rb_parent(this);
2204 if (this->rb_left == &fscki->rb)
2205 this->rb_left = NULL;
2207 this->rb_right = NULL;
2215 * check_inodes - checks all inodes.
2216 * @c: UBIFS file-system description object
2217 * @fsckd: FS checking information
2219 * This is a helper function for 'dbg_check_filesystem()' which walks the
2220 * RB-tree of inodes after the index scan has been finished, and checks that
2221 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
2222 * %-EINVAL if not, and a negative error code in case of failure.
2224 static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
2227 union ubifs_key key;
2228 struct ubifs_znode *znode;
2229 struct ubifs_zbranch *zbr;
2230 struct ubifs_ino_node *ino;
2231 struct fsck_inode *fscki;
2232 struct rb_node *this = rb_first(&fsckd->inodes);
2235 fscki = rb_entry(this, struct fsck_inode, rb);
2236 this = rb_next(this);
2238 if (S_ISDIR(fscki->mode)) {
2240 * Directories have to have exactly one reference (they
2241 * cannot have hardlinks), although root inode is an
2244 if (fscki->inum != UBIFS_ROOT_INO &&
2245 fscki->references != 1) {
2246 ubifs_err("directory inode %lu has %d "
2247 "direntries which refer it, but "
2249 (unsigned long)fscki->inum,
2253 if (fscki->inum == UBIFS_ROOT_INO &&
2254 fscki->references != 0) {
2255 ubifs_err("root inode %lu has non-zero (%d) "
2256 "direntries which refer it",
2257 (unsigned long)fscki->inum,
2261 if (fscki->calc_sz != fscki->size) {
2262 ubifs_err("directory inode %lu size is %lld, "
2263 "but calculated size is %lld",
2264 (unsigned long)fscki->inum,
2265 fscki->size, fscki->calc_sz);
2268 if (fscki->calc_cnt != fscki->nlink) {
2269 ubifs_err("directory inode %lu nlink is %d, "
2270 "but calculated nlink is %d",
2271 (unsigned long)fscki->inum,
2272 fscki->nlink, fscki->calc_cnt);
2276 if (fscki->references != fscki->nlink) {
2277 ubifs_err("inode %lu nlink is %d, but "
2278 "calculated nlink is %d",
2279 (unsigned long)fscki->inum,
2280 fscki->nlink, fscki->references);
2284 if (fscki->xattr_sz != fscki->calc_xsz) {
2285 ubifs_err("inode %lu has xattr size %u, but "
2286 "calculated size is %lld",
2287 (unsigned long)fscki->inum, fscki->xattr_sz,
2291 if (fscki->xattr_cnt != fscki->calc_xcnt) {
2292 ubifs_err("inode %lu has %u xattrs, but "
2293 "calculated count is %lld",
2294 (unsigned long)fscki->inum,
2295 fscki->xattr_cnt, fscki->calc_xcnt);
2298 if (fscki->xattr_nms != fscki->calc_xnms) {
2299 ubifs_err("inode %lu has xattr names' size %u, but "
2300 "calculated names' size is %lld",
2301 (unsigned long)fscki->inum, fscki->xattr_nms,
2310 /* Read the bad inode and dump it */
2311 ino_key_init(c, &key, fscki->inum);
2312 err = ubifs_lookup_level0(c, &key, &znode, &n);
2314 ubifs_err("inode %lu not found in index",
2315 (unsigned long)fscki->inum);
2317 } else if (err < 0) {
2318 ubifs_err("error %d while looking up inode %lu",
2319 err, (unsigned long)fscki->inum);
2323 zbr = &znode->zbranch[n];
2324 ino = kmalloc(zbr->len, GFP_NOFS);
2328 err = ubifs_tnc_read_node(c, zbr, ino);
2330 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2331 zbr->lnum, zbr->offs, err);
2336 ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",
2337 (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
2338 dbg_dump_node(c, ino);
2344 * dbg_check_filesystem - check the file-system.
2345 * @c: UBIFS file-system description object
2347 * This function checks the file system, namely:
2348 * o makes sure that all leaf nodes exist and their CRCs are correct;
2349 * o makes sure inode nlink, size, xattr size/count are correct (for all
2352 * The function reads whole indexing tree and all nodes, so it is pretty
2353 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2354 * not, and a negative error code in case of failure.
2356 int dbg_check_filesystem(struct ubifs_info *c)
2359 struct fsck_data fsckd;
2361 if (!dbg_is_chk_fs(c))
2364 fsckd.inodes = RB_ROOT;
2365 err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
2369 err = check_inodes(c, &fsckd);
2373 free_inodes(&fsckd);
2377 ubifs_err("file-system check failed with error %d", err);
2379 free_inodes(&fsckd);
2384 * dbg_check_data_nodes_order - check that list of data nodes is sorted.
2385 * @c: UBIFS file-system description object
2386 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2388 * This function returns zero if the list of data nodes is sorted correctly,
2389 * and %-EINVAL if not.
2391 int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head)
2393 struct list_head *cur;
2394 struct ubifs_scan_node *sa, *sb;
2396 if (!dbg_is_chk_gen(c))
2399 for (cur = head->next; cur->next != head; cur = cur->next) {
2401 uint32_t blka, blkb;
2404 sa = container_of(cur, struct ubifs_scan_node, list);
2405 sb = container_of(cur->next, struct ubifs_scan_node, list);
2407 if (sa->type != UBIFS_DATA_NODE) {
2408 ubifs_err("bad node type %d", sa->type);
2409 dbg_dump_node(c, sa->node);
2412 if (sb->type != UBIFS_DATA_NODE) {
2413 ubifs_err("bad node type %d", sb->type);
2414 dbg_dump_node(c, sb->node);
2418 inuma = key_inum(c, &sa->key);
2419 inumb = key_inum(c, &sb->key);
2423 if (inuma > inumb) {
2424 ubifs_err("larger inum %lu goes before inum %lu",
2425 (unsigned long)inuma, (unsigned long)inumb);
2429 blka = key_block(c, &sa->key);
2430 blkb = key_block(c, &sb->key);
2433 ubifs_err("larger block %u goes before %u", blka, blkb);
2437 ubifs_err("two data nodes for the same block");
2445 dbg_dump_node(c, sa->node);
2446 dbg_dump_node(c, sb->node);
2451 * dbg_check_nondata_nodes_order - check that list of data nodes is sorted.
2452 * @c: UBIFS file-system description object
2453 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2455 * This function returns zero if the list of non-data nodes is sorted correctly,
2456 * and %-EINVAL if not.
2458 int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head)
2460 struct list_head *cur;
2461 struct ubifs_scan_node *sa, *sb;
2463 if (!dbg_is_chk_gen(c))
2466 for (cur = head->next; cur->next != head; cur = cur->next) {
2468 uint32_t hasha, hashb;
2471 sa = container_of(cur, struct ubifs_scan_node, list);
2472 sb = container_of(cur->next, struct ubifs_scan_node, list);
2474 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2475 sa->type != UBIFS_XENT_NODE) {
2476 ubifs_err("bad node type %d", sa->type);
2477 dbg_dump_node(c, sa->node);
2480 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2481 sa->type != UBIFS_XENT_NODE) {
2482 ubifs_err("bad node type %d", sb->type);
2483 dbg_dump_node(c, sb->node);
2487 if (sa->type != UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2488 ubifs_err("non-inode node goes before inode node");
2492 if (sa->type == UBIFS_INO_NODE && sb->type != UBIFS_INO_NODE)
2495 if (sa->type == UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2496 /* Inode nodes are sorted in descending size order */
2497 if (sa->len < sb->len) {
2498 ubifs_err("smaller inode node goes first");
2505 * This is either a dentry or xentry, which should be sorted in
2506 * ascending (parent ino, hash) order.
2508 inuma = key_inum(c, &sa->key);
2509 inumb = key_inum(c, &sb->key);
2513 if (inuma > inumb) {
2514 ubifs_err("larger inum %lu goes before inum %lu",
2515 (unsigned long)inuma, (unsigned long)inumb);
2519 hasha = key_block(c, &sa->key);
2520 hashb = key_block(c, &sb->key);
2522 if (hasha > hashb) {
2523 ubifs_err("larger hash %u goes before %u",
2532 ubifs_msg("dumping first node");
2533 dbg_dump_node(c, sa->node);
2534 ubifs_msg("dumping second node");
2535 dbg_dump_node(c, sb->node);
2540 /* Failure mode for recovery testing */
2542 #define chance(n, d) (simple_rand() <= (n) * 32768LL / (d))
2544 struct failure_mode_info {
2545 struct list_head list;
2546 struct ubifs_info *c;
2549 static LIST_HEAD(fmi_list);
2550 static DEFINE_SPINLOCK(fmi_lock);
2552 static unsigned int next;
2554 static int simple_rand(void)
2557 next = current->pid;
2558 next = next * 1103515245 + 12345;
2559 return (next >> 16) & 32767;
2562 static void failure_mode_init(struct ubifs_info *c)
2564 struct failure_mode_info *fmi;
2566 fmi = kmalloc(sizeof(struct failure_mode_info), GFP_NOFS);
2568 ubifs_err("Failed to register failure mode - no memory");
2572 spin_lock(&fmi_lock);
2573 list_add_tail(&fmi->list, &fmi_list);
2574 spin_unlock(&fmi_lock);
2577 static void failure_mode_exit(struct ubifs_info *c)
2579 struct failure_mode_info *fmi, *tmp;
2581 spin_lock(&fmi_lock);
2582 list_for_each_entry_safe(fmi, tmp, &fmi_list, list)
2584 list_del(&fmi->list);
2587 spin_unlock(&fmi_lock);
2590 static struct ubifs_info *dbg_find_info(struct ubi_volume_desc *desc)
2592 struct failure_mode_info *fmi;
2594 spin_lock(&fmi_lock);
2595 list_for_each_entry(fmi, &fmi_list, list)
2596 if (fmi->c->ubi == desc) {
2597 struct ubifs_info *c = fmi->c;
2599 spin_unlock(&fmi_lock);
2602 spin_unlock(&fmi_lock);
2606 static int in_failure_mode(struct ubi_volume_desc *desc)
2608 struct ubifs_info *c = dbg_find_info(desc);
2610 if (c && dbg_is_tst_rcvry(c))
2611 return c->dbg->failure_mode;
2615 static int do_fail(struct ubi_volume_desc *desc, int lnum, int write)
2617 struct ubifs_info *c = dbg_find_info(desc);
2618 struct ubifs_debug_info *d;
2620 if (!c || !dbg_is_tst_rcvry(c))
2623 if (d->failure_mode)
2626 /* First call - decide delay to failure */
2628 unsigned int delay = 1 << (simple_rand() >> 11);
2632 d->fail_timeout = jiffies +
2633 msecs_to_jiffies(delay);
2634 ubifs_warn("failing after %ums", delay);
2637 d->fail_cnt_max = delay;
2638 ubifs_warn("failing after %u calls", delay);
2643 /* Determine if failure delay has expired */
2644 if (d->fail_delay == 1) {
2645 if (time_before(jiffies, d->fail_timeout))
2647 } else if (d->fail_delay == 2)
2648 if (d->fail_cnt++ < d->fail_cnt_max)
2650 if (lnum == UBIFS_SB_LNUM) {
2654 } else if (chance(19, 20))
2656 ubifs_warn("failing in super block LEB %d", lnum);
2657 } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2660 ubifs_warn("failing in master LEB %d", lnum);
2661 } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2663 if (chance(99, 100))
2665 } else if (chance(399, 400))
2667 ubifs_warn("failing in log LEB %d", lnum);
2668 } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2672 } else if (chance(19, 20))
2674 ubifs_warn("failing in LPT LEB %d", lnum);
2675 } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2679 } else if (chance(9, 10))
2681 ubifs_warn("failing in orphan LEB %d", lnum);
2682 } else if (lnum == c->ihead_lnum) {
2683 if (chance(99, 100))
2685 ubifs_warn("failing in index head LEB %d", lnum);
2686 } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2689 ubifs_warn("failing in GC head LEB %d", lnum);
2690 } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
2691 !ubifs_search_bud(c, lnum)) {
2694 ubifs_warn("failing in non-bud LEB %d", lnum);
2695 } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
2696 c->cmt_state == COMMIT_RUNNING_REQUIRED) {
2697 if (chance(999, 1000))
2699 ubifs_warn("failing in bud LEB %d commit running", lnum);
2701 if (chance(9999, 10000))
2703 ubifs_warn("failing in bud LEB %d commit not running", lnum);
2706 d->failure_mode = 1;
2711 static void cut_data(const void *buf, int len)
2714 unsigned char *p = (void *)buf;
2716 flen = (len * (long long)simple_rand()) >> 15;
2717 for (i = flen; i < len; i++)
2721 int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
2724 if (in_failure_mode(desc))
2726 return ubi_leb_read(desc, lnum, buf, offset, len, check);
2729 int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
2730 int offset, int len, int dtype)
2734 if (in_failure_mode(desc))
2736 failing = do_fail(desc, lnum, 1);
2739 err = ubi_leb_write(desc, lnum, buf, offset, len, dtype);
2747 int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
2752 if (do_fail(desc, lnum, 1))
2754 err = ubi_leb_change(desc, lnum, buf, len, dtype);
2757 if (do_fail(desc, lnum, 1))
2762 int dbg_leb_erase(struct ubi_volume_desc *desc, int lnum)
2766 if (do_fail(desc, lnum, 0))
2768 err = ubi_leb_erase(desc, lnum);
2771 if (do_fail(desc, lnum, 0))
2776 int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum)
2780 if (do_fail(desc, lnum, 0))
2782 err = ubi_leb_unmap(desc, lnum);
2785 if (do_fail(desc, lnum, 0))
2790 int dbg_is_mapped(struct ubi_volume_desc *desc, int lnum)
2792 if (in_failure_mode(desc))
2794 return ubi_is_mapped(desc, lnum);
2797 int dbg_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
2801 if (do_fail(desc, lnum, 0))
2803 err = ubi_leb_map(desc, lnum, dtype);
2806 if (do_fail(desc, lnum, 0))
2812 * ubifs_debugging_init - initialize UBIFS debugging.
2813 * @c: UBIFS file-system description object
2815 * This function initializes debugging-related data for the file system.
2816 * Returns zero in case of success and a negative error code in case of
2819 int ubifs_debugging_init(struct ubifs_info *c)
2821 c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
2825 failure_mode_init(c);
2830 * ubifs_debugging_exit - free debugging data.
2831 * @c: UBIFS file-system description object
2833 void ubifs_debugging_exit(struct ubifs_info *c)
2835 failure_mode_exit(c);
2840 * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
2841 * contain the stuff specific to particular file-system mounts.
2843 static struct dentry *dfs_rootdir;
2846 * dbg_debugfs_init - initialize debugfs file-system.
2848 * UBIFS uses debugfs file-system to expose various debugging knobs to
2849 * user-space. This function creates "ubifs" directory in the debugfs
2850 * file-system. Returns zero in case of success and a negative error code in
2853 int dbg_debugfs_init(void)
2855 dfs_rootdir = debugfs_create_dir("ubifs", NULL);
2856 if (IS_ERR_OR_NULL(dfs_rootdir)) {
2857 int err = dfs_rootdir ? PTR_ERR(dfs_rootdir) : -ENODEV;
2858 ubifs_err("cannot create \"ubifs\" debugfs directory, "
2867 * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
2869 void dbg_debugfs_exit(void)
2871 debugfs_remove(dfs_rootdir);
2874 static int open_debugfs_file(struct inode *inode, struct file *file)
2876 file->private_data = inode->i_private;
2877 return nonseekable_open(inode, file);
2880 static ssize_t dfs_file_read(struct file *file, char __user *u, size_t count,
2883 struct dentry *dent = file->f_path.dentry;
2884 struct ubifs_info *c = file->private_data;
2885 struct ubifs_debug_info *d = c->dbg;
2889 if (dent == d->dfs_chk_gen)
2891 else if (dent == d->dfs_chk_index)
2893 else if (dent == d->dfs_chk_orph)
2895 else if (dent == d->dfs_chk_lprops)
2896 val = d->chk_lprops;
2897 else if (dent == d->dfs_chk_fs)
2899 else if (dent == d->dfs_tst_rcvry)
2911 return simple_read_from_buffer(u, count, ppos, buf, 2);
2914 static ssize_t dfs_file_write(struct file *file, const char __user *u,
2915 size_t count, loff_t *ppos)
2917 struct ubifs_info *c = file->private_data;
2918 struct ubifs_debug_info *d = c->dbg;
2919 struct dentry *dent = file->f_path.dentry;
2925 * TODO: this is racy - the file-system might have already been
2926 * unmounted and we'd oops in this case. The plan is to fix it with
2927 * help of 'iterate_supers_type()' which we should have in v3.0: when
2928 * a debugfs opened, we rember FS's UUID in file->private_data. Then
2929 * whenever we access the FS via a debugfs file, we iterate all UBIFS
2930 * superblocks and fine the one with the same UUID, and take the
2933 * The other way to go suggested by Al Viro is to create a separate
2934 * 'ubifs-debug' file-system instead.
2936 if (file->f_path.dentry == d->dfs_dump_lprops) {
2940 if (file->f_path.dentry == d->dfs_dump_budg) {
2941 dbg_dump_budg(c, &c->bi);
2944 if (file->f_path.dentry == d->dfs_dump_tnc) {
2945 mutex_lock(&c->tnc_mutex);
2947 mutex_unlock(&c->tnc_mutex);
2951 buf_size = min_t(size_t, count, (sizeof(buf) - 1));
2952 if (copy_from_user(buf, u, buf_size))
2957 else if (buf[0] == '0')
2962 if (dent == d->dfs_chk_gen)
2964 else if (dent == d->dfs_chk_index)
2966 else if (dent == d->dfs_chk_orph)
2968 else if (dent == d->dfs_chk_lprops)
2969 d->chk_lprops = val;
2970 else if (dent == d->dfs_chk_fs)
2972 else if (dent == d->dfs_tst_rcvry)
2980 static const struct file_operations dfs_fops = {
2981 .open = open_debugfs_file,
2982 .read = dfs_file_read,
2983 .write = dfs_file_write,
2984 .owner = THIS_MODULE,
2985 .llseek = no_llseek,
2989 * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
2990 * @c: UBIFS file-system description object
2992 * This function creates all debugfs files for this instance of UBIFS. Returns
2993 * zero in case of success and a negative error code in case of failure.
2995 * Note, the only reason we have not merged this function with the
2996 * 'ubifs_debugging_init()' function is because it is better to initialize
2997 * debugfs interfaces at the very end of the mount process, and remove them at
2998 * the very beginning of the mount process.
3000 int dbg_debugfs_init_fs(struct ubifs_info *c)
3004 struct dentry *dent;
3005 struct ubifs_debug_info *d = c->dbg;
3007 n = snprintf(d->dfs_dir_name, UBIFS_DFS_DIR_LEN + 1, UBIFS_DFS_DIR_NAME,
3008 c->vi.ubi_num, c->vi.vol_id);
3009 if (n == UBIFS_DFS_DIR_LEN) {
3010 /* The array size is too small */
3011 fname = UBIFS_DFS_DIR_NAME;
3012 dent = ERR_PTR(-EINVAL);
3016 fname = d->dfs_dir_name;
3017 dent = debugfs_create_dir(fname, dfs_rootdir);
3018 if (IS_ERR_OR_NULL(dent))
3022 fname = "dump_lprops";
3023 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
3024 if (IS_ERR_OR_NULL(dent))
3026 d->dfs_dump_lprops = dent;
3028 fname = "dump_budg";
3029 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
3030 if (IS_ERR_OR_NULL(dent))
3032 d->dfs_dump_budg = dent;
3035 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
3036 if (IS_ERR_OR_NULL(dent))
3038 d->dfs_dump_tnc = dent;
3040 fname = "chk_general";
3041 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
3043 if (IS_ERR_OR_NULL(dent))
3045 d->dfs_chk_gen = dent;
3047 fname = "chk_index";
3048 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
3050 if (IS_ERR_OR_NULL(dent))
3052 d->dfs_chk_index = dent;
3054 fname = "chk_orphans";
3055 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
3057 if (IS_ERR_OR_NULL(dent))
3059 d->dfs_chk_orph = dent;
3061 fname = "chk_lprops";
3062 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
3064 if (IS_ERR_OR_NULL(dent))
3066 d->dfs_chk_lprops = dent;
3069 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
3071 if (IS_ERR_OR_NULL(dent))
3073 d->dfs_chk_fs = dent;
3075 fname = "tst_recovery";
3076 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
3078 if (IS_ERR_OR_NULL(dent))
3080 d->dfs_tst_rcvry = dent;
3085 debugfs_remove_recursive(d->dfs_dir);
3087 err = dent ? PTR_ERR(dent) : -ENODEV;
3088 ubifs_err("cannot create \"%s\" debugfs filr or directory, error %d\n",
3094 * dbg_debugfs_exit_fs - remove all debugfs files.
3095 * @c: UBIFS file-system description object
3097 void dbg_debugfs_exit_fs(struct ubifs_info *c)
3099 debugfs_remove_recursive(c->dbg->dfs_dir);
3102 #endif /* CONFIG_UBIFS_FS_DEBUG */