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/moduleparam.h>
36 #ifdef CONFIG_UBIFS_FS_DEBUG
38 DEFINE_SPINLOCK(dbg_lock);
40 static char dbg_key_buf0[128];
41 static char dbg_key_buf1[128];
43 unsigned int ubifs_msg_flags = UBIFS_MSG_FLAGS_DEFAULT;
44 unsigned int ubifs_chk_flags = UBIFS_CHK_FLAGS_DEFAULT;
45 unsigned int ubifs_tst_flags;
47 module_param_named(debug_msgs, ubifs_msg_flags, uint, S_IRUGO | S_IWUSR);
48 module_param_named(debug_chks, ubifs_chk_flags, uint, S_IRUGO | S_IWUSR);
49 module_param_named(debug_tsts, ubifs_tst_flags, uint, S_IRUGO | S_IWUSR);
51 MODULE_PARM_DESC(debug_msgs, "Debug message type flags");
52 MODULE_PARM_DESC(debug_chks, "Debug check flags");
53 MODULE_PARM_DESC(debug_tsts, "Debug special test flags");
55 static const char *get_key_fmt(int fmt)
58 case UBIFS_SIMPLE_KEY_FMT:
61 return "unknown/invalid format";
65 static const char *get_key_hash(int hash)
68 case UBIFS_KEY_HASH_R5:
70 case UBIFS_KEY_HASH_TEST:
73 return "unknown/invalid name hash";
77 static const char *get_key_type(int type)
91 return "unknown/invalid key";
95 static void sprintf_key(const struct ubifs_info *c, const union ubifs_key *key,
99 int type = key_type(c, key);
101 if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
104 sprintf(p, "(%lu, %s)", key_inum(c, key),
109 sprintf(p, "(%lu, %s, %#08x)", key_inum(c, key),
110 get_key_type(type), key_hash(c, key));
113 sprintf(p, "(%lu, %s, %u)", key_inum(c, key),
114 get_key_type(type), key_block(c, key));
117 sprintf(p, "(%lu, %s)",
118 key_inum(c, key), get_key_type(type));
121 sprintf(p, "(bad key type: %#08x, %#08x)",
122 key->u32[0], key->u32[1]);
125 sprintf(p, "bad key format %d", c->key_fmt);
128 const char *dbg_key_str0(const struct ubifs_info *c, const union ubifs_key *key)
130 /* dbg_lock must be held */
131 sprintf_key(c, key, dbg_key_buf0);
135 const char *dbg_key_str1(const struct ubifs_info *c, const union ubifs_key *key)
137 /* dbg_lock must be held */
138 sprintf_key(c, key, dbg_key_buf1);
142 const char *dbg_ntype(int type)
146 return "padding node";
148 return "superblock node";
150 return "master node";
152 return "reference node";
155 case UBIFS_DENT_NODE:
156 return "direntry node";
157 case UBIFS_XENT_NODE:
158 return "xentry node";
159 case UBIFS_DATA_NODE:
161 case UBIFS_TRUN_NODE:
162 return "truncate node";
164 return "indexing node";
166 return "commit start node";
167 case UBIFS_ORPH_NODE:
168 return "orphan node";
170 return "unknown node";
174 static const char *dbg_gtype(int type)
177 case UBIFS_NO_NODE_GROUP:
178 return "no node group";
179 case UBIFS_IN_NODE_GROUP:
180 return "in node group";
181 case UBIFS_LAST_OF_NODE_GROUP:
182 return "last of node group";
188 const char *dbg_cstate(int cmt_state)
192 return "commit resting";
193 case COMMIT_BACKGROUND:
194 return "background commit requested";
195 case COMMIT_REQUIRED:
196 return "commit required";
197 case COMMIT_RUNNING_BACKGROUND:
198 return "BACKGROUND commit running";
199 case COMMIT_RUNNING_REQUIRED:
200 return "commit running and required";
202 return "broken commit";
204 return "unknown commit state";
208 static void dump_ch(const struct ubifs_ch *ch)
210 printk(KERN_DEBUG "\tmagic %#x\n", le32_to_cpu(ch->magic));
211 printk(KERN_DEBUG "\tcrc %#x\n", le32_to_cpu(ch->crc));
212 printk(KERN_DEBUG "\tnode_type %d (%s)\n", ch->node_type,
213 dbg_ntype(ch->node_type));
214 printk(KERN_DEBUG "\tgroup_type %d (%s)\n", ch->group_type,
215 dbg_gtype(ch->group_type));
216 printk(KERN_DEBUG "\tsqnum %llu\n",
217 (unsigned long long)le64_to_cpu(ch->sqnum));
218 printk(KERN_DEBUG "\tlen %u\n", le32_to_cpu(ch->len));
221 void dbg_dump_inode(const struct ubifs_info *c, const struct inode *inode)
223 const struct ubifs_inode *ui = ubifs_inode(inode);
225 printk(KERN_DEBUG "Dump in-memory inode:");
226 printk(KERN_DEBUG "\tinode %lu\n", inode->i_ino);
227 printk(KERN_DEBUG "\tsize %llu\n",
228 (unsigned long long)i_size_read(inode));
229 printk(KERN_DEBUG "\tnlink %u\n", inode->i_nlink);
230 printk(KERN_DEBUG "\tuid %u\n", (unsigned int)inode->i_uid);
231 printk(KERN_DEBUG "\tgid %u\n", (unsigned int)inode->i_gid);
232 printk(KERN_DEBUG "\tatime %u.%u\n",
233 (unsigned int)inode->i_atime.tv_sec,
234 (unsigned int)inode->i_atime.tv_nsec);
235 printk(KERN_DEBUG "\tmtime %u.%u\n",
236 (unsigned int)inode->i_mtime.tv_sec,
237 (unsigned int)inode->i_mtime.tv_nsec);
238 printk(KERN_DEBUG "\tctime %u.%u\n",
239 (unsigned int)inode->i_ctime.tv_sec,
240 (unsigned int)inode->i_ctime.tv_nsec);
241 printk(KERN_DEBUG "\tcreat_sqnum %llu\n", ui->creat_sqnum);
242 printk(KERN_DEBUG "\txattr_size %u\n", ui->xattr_size);
243 printk(KERN_DEBUG "\txattr_cnt %u\n", ui->xattr_cnt);
244 printk(KERN_DEBUG "\txattr_names %u\n", ui->xattr_names);
245 printk(KERN_DEBUG "\tdirty %u\n", ui->dirty);
246 printk(KERN_DEBUG "\txattr %u\n", ui->xattr);
247 printk(KERN_DEBUG "\tbulk_read %u\n", ui->xattr);
248 printk(KERN_DEBUG "\tsynced_i_size %llu\n",
249 (unsigned long long)ui->synced_i_size);
250 printk(KERN_DEBUG "\tui_size %llu\n",
251 (unsigned long long)ui->ui_size);
252 printk(KERN_DEBUG "\tflags %d\n", ui->flags);
253 printk(KERN_DEBUG "\tcompr_type %d\n", ui->compr_type);
254 printk(KERN_DEBUG "\tlast_page_read %lu\n", ui->last_page_read);
255 printk(KERN_DEBUG "\tread_in_a_row %lu\n", ui->read_in_a_row);
256 printk(KERN_DEBUG "\tdata_len %d\n", ui->data_len);
259 void dbg_dump_node(const struct ubifs_info *c, const void *node)
263 const struct ubifs_ch *ch = node;
265 if (dbg_failure_mode)
268 /* If the magic is incorrect, just hexdump the first bytes */
269 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
270 printk(KERN_DEBUG "Not a node, first %zu bytes:", UBIFS_CH_SZ);
271 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
272 (void *)node, UBIFS_CH_SZ, 1);
276 spin_lock(&dbg_lock);
279 switch (ch->node_type) {
282 const struct ubifs_pad_node *pad = node;
284 printk(KERN_DEBUG "\tpad_len %u\n",
285 le32_to_cpu(pad->pad_len));
290 const struct ubifs_sb_node *sup = node;
291 unsigned int sup_flags = le32_to_cpu(sup->flags);
293 printk(KERN_DEBUG "\tkey_hash %d (%s)\n",
294 (int)sup->key_hash, get_key_hash(sup->key_hash));
295 printk(KERN_DEBUG "\tkey_fmt %d (%s)\n",
296 (int)sup->key_fmt, get_key_fmt(sup->key_fmt));
297 printk(KERN_DEBUG "\tflags %#x\n", sup_flags);
298 printk(KERN_DEBUG "\t big_lpt %u\n",
299 !!(sup_flags & UBIFS_FLG_BIGLPT));
300 printk(KERN_DEBUG "\tmin_io_size %u\n",
301 le32_to_cpu(sup->min_io_size));
302 printk(KERN_DEBUG "\tleb_size %u\n",
303 le32_to_cpu(sup->leb_size));
304 printk(KERN_DEBUG "\tleb_cnt %u\n",
305 le32_to_cpu(sup->leb_cnt));
306 printk(KERN_DEBUG "\tmax_leb_cnt %u\n",
307 le32_to_cpu(sup->max_leb_cnt));
308 printk(KERN_DEBUG "\tmax_bud_bytes %llu\n",
309 (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
310 printk(KERN_DEBUG "\tlog_lebs %u\n",
311 le32_to_cpu(sup->log_lebs));
312 printk(KERN_DEBUG "\tlpt_lebs %u\n",
313 le32_to_cpu(sup->lpt_lebs));
314 printk(KERN_DEBUG "\torph_lebs %u\n",
315 le32_to_cpu(sup->orph_lebs));
316 printk(KERN_DEBUG "\tjhead_cnt %u\n",
317 le32_to_cpu(sup->jhead_cnt));
318 printk(KERN_DEBUG "\tfanout %u\n",
319 le32_to_cpu(sup->fanout));
320 printk(KERN_DEBUG "\tlsave_cnt %u\n",
321 le32_to_cpu(sup->lsave_cnt));
322 printk(KERN_DEBUG "\tdefault_compr %u\n",
323 (int)le16_to_cpu(sup->default_compr));
324 printk(KERN_DEBUG "\trp_size %llu\n",
325 (unsigned long long)le64_to_cpu(sup->rp_size));
326 printk(KERN_DEBUG "\trp_uid %u\n",
327 le32_to_cpu(sup->rp_uid));
328 printk(KERN_DEBUG "\trp_gid %u\n",
329 le32_to_cpu(sup->rp_gid));
330 printk(KERN_DEBUG "\tfmt_version %u\n",
331 le32_to_cpu(sup->fmt_version));
332 printk(KERN_DEBUG "\ttime_gran %u\n",
333 le32_to_cpu(sup->time_gran));
334 printk(KERN_DEBUG "\tUUID %02X%02X%02X%02X-%02X%02X"
335 "-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
336 sup->uuid[0], sup->uuid[1], sup->uuid[2], sup->uuid[3],
337 sup->uuid[4], sup->uuid[5], sup->uuid[6], sup->uuid[7],
338 sup->uuid[8], sup->uuid[9], sup->uuid[10], sup->uuid[11],
339 sup->uuid[12], sup->uuid[13], sup->uuid[14],
345 const struct ubifs_mst_node *mst = node;
347 printk(KERN_DEBUG "\thighest_inum %llu\n",
348 (unsigned long long)le64_to_cpu(mst->highest_inum));
349 printk(KERN_DEBUG "\tcommit number %llu\n",
350 (unsigned long long)le64_to_cpu(mst->cmt_no));
351 printk(KERN_DEBUG "\tflags %#x\n",
352 le32_to_cpu(mst->flags));
353 printk(KERN_DEBUG "\tlog_lnum %u\n",
354 le32_to_cpu(mst->log_lnum));
355 printk(KERN_DEBUG "\troot_lnum %u\n",
356 le32_to_cpu(mst->root_lnum));
357 printk(KERN_DEBUG "\troot_offs %u\n",
358 le32_to_cpu(mst->root_offs));
359 printk(KERN_DEBUG "\troot_len %u\n",
360 le32_to_cpu(mst->root_len));
361 printk(KERN_DEBUG "\tgc_lnum %u\n",
362 le32_to_cpu(mst->gc_lnum));
363 printk(KERN_DEBUG "\tihead_lnum %u\n",
364 le32_to_cpu(mst->ihead_lnum));
365 printk(KERN_DEBUG "\tihead_offs %u\n",
366 le32_to_cpu(mst->ihead_offs));
367 printk(KERN_DEBUG "\tindex_size %u\n",
368 le32_to_cpu(mst->index_size));
369 printk(KERN_DEBUG "\tlpt_lnum %u\n",
370 le32_to_cpu(mst->lpt_lnum));
371 printk(KERN_DEBUG "\tlpt_offs %u\n",
372 le32_to_cpu(mst->lpt_offs));
373 printk(KERN_DEBUG "\tnhead_lnum %u\n",
374 le32_to_cpu(mst->nhead_lnum));
375 printk(KERN_DEBUG "\tnhead_offs %u\n",
376 le32_to_cpu(mst->nhead_offs));
377 printk(KERN_DEBUG "\tltab_lnum %u\n",
378 le32_to_cpu(mst->ltab_lnum));
379 printk(KERN_DEBUG "\tltab_offs %u\n",
380 le32_to_cpu(mst->ltab_offs));
381 printk(KERN_DEBUG "\tlsave_lnum %u\n",
382 le32_to_cpu(mst->lsave_lnum));
383 printk(KERN_DEBUG "\tlsave_offs %u\n",
384 le32_to_cpu(mst->lsave_offs));
385 printk(KERN_DEBUG "\tlscan_lnum %u\n",
386 le32_to_cpu(mst->lscan_lnum));
387 printk(KERN_DEBUG "\tleb_cnt %u\n",
388 le32_to_cpu(mst->leb_cnt));
389 printk(KERN_DEBUG "\tempty_lebs %u\n",
390 le32_to_cpu(mst->empty_lebs));
391 printk(KERN_DEBUG "\tidx_lebs %u\n",
392 le32_to_cpu(mst->idx_lebs));
393 printk(KERN_DEBUG "\ttotal_free %llu\n",
394 (unsigned long long)le64_to_cpu(mst->total_free));
395 printk(KERN_DEBUG "\ttotal_dirty %llu\n",
396 (unsigned long long)le64_to_cpu(mst->total_dirty));
397 printk(KERN_DEBUG "\ttotal_used %llu\n",
398 (unsigned long long)le64_to_cpu(mst->total_used));
399 printk(KERN_DEBUG "\ttotal_dead %llu\n",
400 (unsigned long long)le64_to_cpu(mst->total_dead));
401 printk(KERN_DEBUG "\ttotal_dark %llu\n",
402 (unsigned long long)le64_to_cpu(mst->total_dark));
407 const struct ubifs_ref_node *ref = node;
409 printk(KERN_DEBUG "\tlnum %u\n",
410 le32_to_cpu(ref->lnum));
411 printk(KERN_DEBUG "\toffs %u\n",
412 le32_to_cpu(ref->offs));
413 printk(KERN_DEBUG "\tjhead %u\n",
414 le32_to_cpu(ref->jhead));
419 const struct ubifs_ino_node *ino = node;
421 key_read(c, &ino->key, &key);
422 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
423 printk(KERN_DEBUG "\tcreat_sqnum %llu\n",
424 (unsigned long long)le64_to_cpu(ino->creat_sqnum));
425 printk(KERN_DEBUG "\tsize %llu\n",
426 (unsigned long long)le64_to_cpu(ino->size));
427 printk(KERN_DEBUG "\tnlink %u\n",
428 le32_to_cpu(ino->nlink));
429 printk(KERN_DEBUG "\tatime %lld.%u\n",
430 (long long)le64_to_cpu(ino->atime_sec),
431 le32_to_cpu(ino->atime_nsec));
432 printk(KERN_DEBUG "\tmtime %lld.%u\n",
433 (long long)le64_to_cpu(ino->mtime_sec),
434 le32_to_cpu(ino->mtime_nsec));
435 printk(KERN_DEBUG "\tctime %lld.%u\n",
436 (long long)le64_to_cpu(ino->ctime_sec),
437 le32_to_cpu(ino->ctime_nsec));
438 printk(KERN_DEBUG "\tuid %u\n",
439 le32_to_cpu(ino->uid));
440 printk(KERN_DEBUG "\tgid %u\n",
441 le32_to_cpu(ino->gid));
442 printk(KERN_DEBUG "\tmode %u\n",
443 le32_to_cpu(ino->mode));
444 printk(KERN_DEBUG "\tflags %#x\n",
445 le32_to_cpu(ino->flags));
446 printk(KERN_DEBUG "\txattr_cnt %u\n",
447 le32_to_cpu(ino->xattr_cnt));
448 printk(KERN_DEBUG "\txattr_size %u\n",
449 le32_to_cpu(ino->xattr_size));
450 printk(KERN_DEBUG "\txattr_names %u\n",
451 le32_to_cpu(ino->xattr_names));
452 printk(KERN_DEBUG "\tcompr_type %#x\n",
453 (int)le16_to_cpu(ino->compr_type));
454 printk(KERN_DEBUG "\tdata len %u\n",
455 le32_to_cpu(ino->data_len));
458 case UBIFS_DENT_NODE:
459 case UBIFS_XENT_NODE:
461 const struct ubifs_dent_node *dent = node;
462 int nlen = le16_to_cpu(dent->nlen);
464 key_read(c, &dent->key, &key);
465 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
466 printk(KERN_DEBUG "\tinum %llu\n",
467 (unsigned long long)le64_to_cpu(dent->inum));
468 printk(KERN_DEBUG "\ttype %d\n", (int)dent->type);
469 printk(KERN_DEBUG "\tnlen %d\n", nlen);
470 printk(KERN_DEBUG "\tname ");
472 if (nlen > UBIFS_MAX_NLEN)
473 printk(KERN_DEBUG "(bad name length, not printing, "
474 "bad or corrupted node)");
476 for (i = 0; i < nlen && dent->name[i]; i++)
477 printk("%c", dent->name[i]);
483 case UBIFS_DATA_NODE:
485 const struct ubifs_data_node *dn = node;
486 int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
488 key_read(c, &dn->key, &key);
489 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
490 printk(KERN_DEBUG "\tsize %u\n",
491 le32_to_cpu(dn->size));
492 printk(KERN_DEBUG "\tcompr_typ %d\n",
493 (int)le16_to_cpu(dn->compr_type));
494 printk(KERN_DEBUG "\tdata size %d\n",
496 printk(KERN_DEBUG "\tdata:\n");
497 print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET, 32, 1,
498 (void *)&dn->data, dlen, 0);
501 case UBIFS_TRUN_NODE:
503 const struct ubifs_trun_node *trun = node;
505 printk(KERN_DEBUG "\tinum %u\n",
506 le32_to_cpu(trun->inum));
507 printk(KERN_DEBUG "\told_size %llu\n",
508 (unsigned long long)le64_to_cpu(trun->old_size));
509 printk(KERN_DEBUG "\tnew_size %llu\n",
510 (unsigned long long)le64_to_cpu(trun->new_size));
515 const struct ubifs_idx_node *idx = node;
517 n = le16_to_cpu(idx->child_cnt);
518 printk(KERN_DEBUG "\tchild_cnt %d\n", n);
519 printk(KERN_DEBUG "\tlevel %d\n",
520 (int)le16_to_cpu(idx->level));
521 printk(KERN_DEBUG "\tBranches:\n");
523 for (i = 0; i < n && i < c->fanout - 1; i++) {
524 const struct ubifs_branch *br;
526 br = ubifs_idx_branch(c, idx, i);
527 key_read(c, &br->key, &key);
528 printk(KERN_DEBUG "\t%d: LEB %d:%d len %d key %s\n",
529 i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
530 le32_to_cpu(br->len), DBGKEY(&key));
536 case UBIFS_ORPH_NODE:
538 const struct ubifs_orph_node *orph = node;
540 printk(KERN_DEBUG "\tcommit number %llu\n",
542 le64_to_cpu(orph->cmt_no) & LLONG_MAX);
543 printk(KERN_DEBUG "\tlast node flag %llu\n",
544 (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
545 n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
546 printk(KERN_DEBUG "\t%d orphan inode numbers:\n", n);
547 for (i = 0; i < n; i++)
548 printk(KERN_DEBUG "\t ino %llu\n",
549 (unsigned long long)le64_to_cpu(orph->inos[i]));
553 printk(KERN_DEBUG "node type %d was not recognized\n",
556 spin_unlock(&dbg_lock);
559 void dbg_dump_budget_req(const struct ubifs_budget_req *req)
561 spin_lock(&dbg_lock);
562 printk(KERN_DEBUG "Budgeting request: new_ino %d, dirtied_ino %d\n",
563 req->new_ino, req->dirtied_ino);
564 printk(KERN_DEBUG "\tnew_ino_d %d, dirtied_ino_d %d\n",
565 req->new_ino_d, req->dirtied_ino_d);
566 printk(KERN_DEBUG "\tnew_page %d, dirtied_page %d\n",
567 req->new_page, req->dirtied_page);
568 printk(KERN_DEBUG "\tnew_dent %d, mod_dent %d\n",
569 req->new_dent, req->mod_dent);
570 printk(KERN_DEBUG "\tidx_growth %d\n", req->idx_growth);
571 printk(KERN_DEBUG "\tdata_growth %d dd_growth %d\n",
572 req->data_growth, req->dd_growth);
573 spin_unlock(&dbg_lock);
576 void dbg_dump_lstats(const struct ubifs_lp_stats *lst)
578 spin_lock(&dbg_lock);
579 printk(KERN_DEBUG "(pid %d) Lprops statistics: empty_lebs %d, "
580 "idx_lebs %d\n", current->pid, lst->empty_lebs, lst->idx_lebs);
581 printk(KERN_DEBUG "\ttaken_empty_lebs %d, total_free %lld, "
582 "total_dirty %lld\n", lst->taken_empty_lebs, lst->total_free,
584 printk(KERN_DEBUG "\ttotal_used %lld, total_dark %lld, "
585 "total_dead %lld\n", lst->total_used, lst->total_dark,
587 spin_unlock(&dbg_lock);
590 void dbg_dump_budg(struct ubifs_info *c)
594 struct ubifs_bud *bud;
595 struct ubifs_gced_idx_leb *idx_gc;
597 spin_lock(&dbg_lock);
598 printk(KERN_DEBUG "(pid %d) Budgeting info: budg_data_growth %lld, "
599 "budg_dd_growth %lld, budg_idx_growth %lld\n", current->pid,
600 c->budg_data_growth, c->budg_dd_growth, c->budg_idx_growth);
601 printk(KERN_DEBUG "\tdata budget sum %lld, total budget sum %lld, "
602 "freeable_cnt %d\n", c->budg_data_growth + c->budg_dd_growth,
603 c->budg_data_growth + c->budg_dd_growth + c->budg_idx_growth,
605 printk(KERN_DEBUG "\tmin_idx_lebs %d, old_idx_sz %lld, "
606 "calc_idx_sz %lld, idx_gc_cnt %d\n", c->min_idx_lebs,
607 c->old_idx_sz, c->calc_idx_sz, c->idx_gc_cnt);
608 printk(KERN_DEBUG "\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, "
609 "clean_zn_cnt %ld\n", atomic_long_read(&c->dirty_pg_cnt),
610 atomic_long_read(&c->dirty_zn_cnt),
611 atomic_long_read(&c->clean_zn_cnt));
612 printk(KERN_DEBUG "\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
613 c->dark_wm, c->dead_wm, c->max_idx_node_sz);
614 printk(KERN_DEBUG "\tgc_lnum %d, ihead_lnum %d\n",
615 c->gc_lnum, c->ihead_lnum);
616 for (i = 0; i < c->jhead_cnt; i++)
617 printk(KERN_DEBUG "\tjhead %d\t LEB %d\n",
618 c->jheads[i].wbuf.jhead, c->jheads[i].wbuf.lnum);
619 for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
620 bud = rb_entry(rb, struct ubifs_bud, rb);
621 printk(KERN_DEBUG "\tbud LEB %d\n", bud->lnum);
623 list_for_each_entry(bud, &c->old_buds, list)
624 printk(KERN_DEBUG "\told bud LEB %d\n", bud->lnum);
625 list_for_each_entry(idx_gc, &c->idx_gc, list)
626 printk(KERN_DEBUG "\tGC'ed idx LEB %d unmap %d\n",
627 idx_gc->lnum, idx_gc->unmap);
628 printk(KERN_DEBUG "\tcommit state %d\n", c->cmt_state);
629 spin_unlock(&dbg_lock);
632 void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
634 printk(KERN_DEBUG "LEB %d lprops: free %d, dirty %d (used %d), "
635 "flags %#x\n", lp->lnum, lp->free, lp->dirty,
636 c->leb_size - lp->free - lp->dirty, lp->flags);
639 void dbg_dump_lprops(struct ubifs_info *c)
642 struct ubifs_lprops lp;
643 struct ubifs_lp_stats lst;
645 printk(KERN_DEBUG "(pid %d) Dumping LEB properties\n", current->pid);
646 ubifs_get_lp_stats(c, &lst);
647 dbg_dump_lstats(&lst);
649 for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
650 err = ubifs_read_one_lp(c, lnum, &lp);
652 ubifs_err("cannot read lprops for LEB %d", lnum);
654 dbg_dump_lprop(c, &lp);
658 void dbg_dump_lpt_info(struct ubifs_info *c)
662 spin_lock(&dbg_lock);
663 printk(KERN_DEBUG "\tlpt_sz: %lld\n", c->lpt_sz);
664 printk(KERN_DEBUG "\tpnode_sz: %d\n", c->pnode_sz);
665 printk(KERN_DEBUG "\tnnode_sz: %d\n", c->nnode_sz);
666 printk(KERN_DEBUG "\tltab_sz: %d\n", c->ltab_sz);
667 printk(KERN_DEBUG "\tlsave_sz: %d\n", c->lsave_sz);
668 printk(KERN_DEBUG "\tbig_lpt: %d\n", c->big_lpt);
669 printk(KERN_DEBUG "\tlpt_hght: %d\n", c->lpt_hght);
670 printk(KERN_DEBUG "\tpnode_cnt: %d\n", c->pnode_cnt);
671 printk(KERN_DEBUG "\tnnode_cnt: %d\n", c->nnode_cnt);
672 printk(KERN_DEBUG "\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt);
673 printk(KERN_DEBUG "\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt);
674 printk(KERN_DEBUG "\tlsave_cnt: %d\n", c->lsave_cnt);
675 printk(KERN_DEBUG "\tspace_bits: %d\n", c->space_bits);
676 printk(KERN_DEBUG "\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
677 printk(KERN_DEBUG "\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
678 printk(KERN_DEBUG "\tlpt_spc_bits: %d\n", c->lpt_spc_bits);
679 printk(KERN_DEBUG "\tpcnt_bits: %d\n", c->pcnt_bits);
680 printk(KERN_DEBUG "\tlnum_bits: %d\n", c->lnum_bits);
681 printk(KERN_DEBUG "\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
682 printk(KERN_DEBUG "\tLPT head is at %d:%d\n",
683 c->nhead_lnum, c->nhead_offs);
684 printk(KERN_DEBUG "\tLPT ltab is at %d:%d\n", c->ltab_lnum, c->ltab_offs);
686 printk(KERN_DEBUG "\tLPT lsave is at %d:%d\n",
687 c->lsave_lnum, c->lsave_offs);
688 for (i = 0; i < c->lpt_lebs; i++)
689 printk(KERN_DEBUG "\tLPT LEB %d free %d dirty %d tgc %d "
690 "cmt %d\n", i + c->lpt_first, c->ltab[i].free,
691 c->ltab[i].dirty, c->ltab[i].tgc, c->ltab[i].cmt);
692 spin_unlock(&dbg_lock);
695 void dbg_dump_leb(const struct ubifs_info *c, int lnum)
697 struct ubifs_scan_leb *sleb;
698 struct ubifs_scan_node *snod;
700 if (dbg_failure_mode)
703 printk(KERN_DEBUG "(pid %d) Dumping LEB %d\n", current->pid, lnum);
705 sleb = ubifs_scan(c, lnum, 0, c->dbg_buf);
707 ubifs_err("scan error %d", (int)PTR_ERR(sleb));
711 printk(KERN_DEBUG "LEB %d has %d nodes ending at %d\n", lnum,
712 sleb->nodes_cnt, sleb->endpt);
714 list_for_each_entry(snod, &sleb->nodes, list) {
716 printk(KERN_DEBUG "Dumping node at LEB %d:%d len %d\n", lnum,
717 snod->offs, snod->len);
718 dbg_dump_node(c, snod->node);
721 ubifs_scan_destroy(sleb);
725 void dbg_dump_znode(const struct ubifs_info *c,
726 const struct ubifs_znode *znode)
729 const struct ubifs_zbranch *zbr;
731 spin_lock(&dbg_lock);
733 zbr = &znode->parent->zbranch[znode->iip];
737 printk(KERN_DEBUG "znode %p, LEB %d:%d len %d parent %p iip %d level %d"
738 " child_cnt %d flags %lx\n", znode, zbr->lnum, zbr->offs,
739 zbr->len, znode->parent, znode->iip, znode->level,
740 znode->child_cnt, znode->flags);
742 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
743 spin_unlock(&dbg_lock);
747 printk(KERN_DEBUG "zbranches:\n");
748 for (n = 0; n < znode->child_cnt; n++) {
749 zbr = &znode->zbranch[n];
750 if (znode->level > 0)
751 printk(KERN_DEBUG "\t%d: znode %p LEB %d:%d len %d key "
752 "%s\n", n, zbr->znode, zbr->lnum,
756 printk(KERN_DEBUG "\t%d: LNC %p LEB %d:%d len %d key "
757 "%s\n", n, zbr->znode, zbr->lnum,
761 spin_unlock(&dbg_lock);
764 void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
768 printk(KERN_DEBUG "(pid %d) Dumping heap cat %d (%d elements)\n",
769 current->pid, cat, heap->cnt);
770 for (i = 0; i < heap->cnt; i++) {
771 struct ubifs_lprops *lprops = heap->arr[i];
773 printk(KERN_DEBUG "\t%d. LEB %d hpos %d free %d dirty %d "
774 "flags %d\n", i, lprops->lnum, lprops->hpos,
775 lprops->free, lprops->dirty, lprops->flags);
779 void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
780 struct ubifs_nnode *parent, int iip)
784 printk(KERN_DEBUG "(pid %d) Dumping pnode:\n", current->pid);
785 printk(KERN_DEBUG "\taddress %zx parent %zx cnext %zx\n",
786 (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
787 printk(KERN_DEBUG "\tflags %lu iip %d level %d num %d\n",
788 pnode->flags, iip, pnode->level, pnode->num);
789 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
790 struct ubifs_lprops *lp = &pnode->lprops[i];
792 printk(KERN_DEBUG "\t%d: free %d dirty %d flags %d lnum %d\n",
793 i, lp->free, lp->dirty, lp->flags, lp->lnum);
797 void dbg_dump_tnc(struct ubifs_info *c)
799 struct ubifs_znode *znode;
802 printk(KERN_DEBUG "\n");
803 printk(KERN_DEBUG "(pid %d) Dumping the TNC tree\n", current->pid);
804 znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
805 level = znode->level;
806 printk(KERN_DEBUG "== Level %d ==\n", level);
808 if (level != znode->level) {
809 level = znode->level;
810 printk(KERN_DEBUG "== Level %d ==\n", level);
812 dbg_dump_znode(c, znode);
813 znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
816 printk(KERN_DEBUG "\n");
819 static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
822 dbg_dump_znode(c, znode);
827 * dbg_dump_index - dump the on-flash index.
828 * @c: UBIFS file-system description object
830 * This function dumps whole UBIFS indexing B-tree, unlike 'dbg_dump_tnc()'
831 * which dumps only in-memory znodes and does not read znodes which from flash.
833 void dbg_dump_index(struct ubifs_info *c)
835 dbg_walk_index(c, NULL, dump_znode, NULL);
839 * dbg_check_synced_i_size - check synchronized inode size.
840 * @inode: inode to check
842 * If inode is clean, synchronized inode size has to be equivalent to current
843 * inode size. This function has to be called only for locked inodes (@i_mutex
844 * has to be locked). Returns %0 if synchronized inode size if correct, and
847 int dbg_check_synced_i_size(struct inode *inode)
850 struct ubifs_inode *ui = ubifs_inode(inode);
852 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
854 if (!S_ISREG(inode->i_mode))
857 mutex_lock(&ui->ui_mutex);
858 spin_lock(&ui->ui_lock);
859 if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
860 ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode "
861 "is clean", ui->ui_size, ui->synced_i_size);
862 ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
863 inode->i_mode, i_size_read(inode));
867 spin_unlock(&ui->ui_lock);
868 mutex_unlock(&ui->ui_mutex);
873 * dbg_check_dir - check directory inode size and link count.
874 * @c: UBIFS file-system description object
875 * @dir: the directory to calculate size for
876 * @size: the result is returned here
878 * This function makes sure that directory size and link count are correct.
879 * Returns zero in case of success and a negative error code in case of
882 * Note, it is good idea to make sure the @dir->i_mutex is locked before
883 * calling this function.
885 int dbg_check_dir_size(struct ubifs_info *c, const struct inode *dir)
887 unsigned int nlink = 2;
889 struct ubifs_dent_node *dent, *pdent = NULL;
890 struct qstr nm = { .name = NULL };
891 loff_t size = UBIFS_INO_NODE_SZ;
893 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
896 if (!S_ISDIR(dir->i_mode))
899 lowest_dent_key(c, &key, dir->i_ino);
903 dent = ubifs_tnc_next_ent(c, &key, &nm);
911 nm.name = dent->name;
912 nm.len = le16_to_cpu(dent->nlen);
913 size += CALC_DENT_SIZE(nm.len);
914 if (dent->type == UBIFS_ITYPE_DIR)
918 key_read(c, &dent->key, &key);
922 if (i_size_read(dir) != size) {
923 ubifs_err("directory inode %lu has size %llu, "
924 "but calculated size is %llu", dir->i_ino,
925 (unsigned long long)i_size_read(dir),
926 (unsigned long long)size);
930 if (dir->i_nlink != nlink) {
931 ubifs_err("directory inode %lu has nlink %u, but calculated "
932 "nlink is %u", dir->i_ino, dir->i_nlink, nlink);
941 * dbg_check_key_order - make sure that colliding keys are properly ordered.
942 * @c: UBIFS file-system description object
943 * @zbr1: first zbranch
944 * @zbr2: following zbranch
946 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
947 * names of the direntries/xentries which are referred by the keys. This
948 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
949 * sure the name of direntry/xentry referred by @zbr1 is less than
950 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
951 * and a negative error code in case of failure.
953 static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
954 struct ubifs_zbranch *zbr2)
956 int err, nlen1, nlen2, cmp;
957 struct ubifs_dent_node *dent1, *dent2;
960 ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
961 dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
964 dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
970 err = ubifs_tnc_read_node(c, zbr1, dent1);
973 err = ubifs_validate_entry(c, dent1);
977 err = ubifs_tnc_read_node(c, zbr2, dent2);
980 err = ubifs_validate_entry(c, dent2);
984 /* Make sure node keys are the same as in zbranch */
986 key_read(c, &dent1->key, &key);
987 if (keys_cmp(c, &zbr1->key, &key)) {
988 dbg_err("1st entry at %d:%d has key %s", zbr1->lnum,
989 zbr1->offs, DBGKEY(&key));
990 dbg_err("but it should have key %s according to tnc",
992 dbg_dump_node(c, dent1);
996 key_read(c, &dent2->key, &key);
997 if (keys_cmp(c, &zbr2->key, &key)) {
998 dbg_err("2nd entry at %d:%d has key %s", zbr1->lnum,
999 zbr1->offs, DBGKEY(&key));
1000 dbg_err("but it should have key %s according to tnc",
1001 DBGKEY(&zbr2->key));
1002 dbg_dump_node(c, dent2);
1006 nlen1 = le16_to_cpu(dent1->nlen);
1007 nlen2 = le16_to_cpu(dent2->nlen);
1009 cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
1010 if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
1014 if (cmp == 0 && nlen1 == nlen2)
1015 dbg_err("2 xent/dent nodes with the same name");
1017 dbg_err("bad order of colliding key %s",
1020 dbg_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs);
1021 dbg_dump_node(c, dent1);
1022 dbg_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs);
1023 dbg_dump_node(c, dent2);
1032 * dbg_check_znode - check if znode is all right.
1033 * @c: UBIFS file-system description object
1034 * @zbr: zbranch which points to this znode
1036 * This function makes sure that znode referred to by @zbr is all right.
1037 * Returns zero if it is, and %-EINVAL if it is not.
1039 static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
1041 struct ubifs_znode *znode = zbr->znode;
1042 struct ubifs_znode *zp = znode->parent;
1045 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
1049 if (znode->level < 0) {
1053 if (znode->iip < 0 || znode->iip >= c->fanout) {
1059 /* Only dirty zbranch may have no on-flash nodes */
1060 if (!ubifs_zn_dirty(znode)) {
1065 if (ubifs_zn_dirty(znode)) {
1067 * If znode is dirty, its parent has to be dirty as well. The
1068 * order of the operation is important, so we have to have
1072 if (zp && !ubifs_zn_dirty(zp)) {
1074 * The dirty flag is atomic and is cleared outside the
1075 * TNC mutex, so znode's dirty flag may now have
1076 * been cleared. The child is always cleared before the
1077 * parent, so we just need to check again.
1080 if (ubifs_zn_dirty(znode)) {
1088 const union ubifs_key *min, *max;
1090 if (znode->level != zp->level - 1) {
1095 /* Make sure the 'parent' pointer in our znode is correct */
1096 err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
1098 /* This zbranch does not exist in the parent */
1103 if (znode->iip >= zp->child_cnt) {
1108 if (znode->iip != n) {
1109 /* This may happen only in case of collisions */
1110 if (keys_cmp(c, &zp->zbranch[n].key,
1111 &zp->zbranch[znode->iip].key)) {
1119 * Make sure that the first key in our znode is greater than or
1120 * equal to the key in the pointing zbranch.
1123 cmp = keys_cmp(c, min, &znode->zbranch[0].key);
1129 if (n + 1 < zp->child_cnt) {
1130 max = &zp->zbranch[n + 1].key;
1133 * Make sure the last key in our znode is less or
1134 * equivalent than the the key in zbranch which goes
1135 * after our pointing zbranch.
1137 cmp = keys_cmp(c, max,
1138 &znode->zbranch[znode->child_cnt - 1].key);
1145 /* This may only be root znode */
1146 if (zbr != &c->zroot) {
1153 * Make sure that next key is greater or equivalent then the previous
1156 for (n = 1; n < znode->child_cnt; n++) {
1157 cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
1158 &znode->zbranch[n].key);
1164 /* This can only be keys with colliding hash */
1165 if (!is_hash_key(c, &znode->zbranch[n].key)) {
1170 if (znode->level != 0 || c->replaying)
1174 * Colliding keys should follow binary order of
1175 * corresponding xentry/dentry names.
1177 err = dbg_check_key_order(c, &znode->zbranch[n - 1],
1178 &znode->zbranch[n]);
1188 for (n = 0; n < znode->child_cnt; n++) {
1189 if (!znode->zbranch[n].znode &&
1190 (znode->zbranch[n].lnum == 0 ||
1191 znode->zbranch[n].len == 0)) {
1196 if (znode->zbranch[n].lnum != 0 &&
1197 znode->zbranch[n].len == 0) {
1202 if (znode->zbranch[n].lnum == 0 &&
1203 znode->zbranch[n].len != 0) {
1208 if (znode->zbranch[n].lnum == 0 &&
1209 znode->zbranch[n].offs != 0) {
1214 if (znode->level != 0 && znode->zbranch[n].znode)
1215 if (znode->zbranch[n].znode->parent != znode) {
1224 ubifs_err("failed, error %d", err);
1225 ubifs_msg("dump of the znode");
1226 dbg_dump_znode(c, znode);
1228 ubifs_msg("dump of the parent znode");
1229 dbg_dump_znode(c, zp);
1236 * dbg_check_tnc - check TNC tree.
1237 * @c: UBIFS file-system description object
1238 * @extra: do extra checks that are possible at start commit
1240 * This function traverses whole TNC tree and checks every znode. Returns zero
1241 * if everything is all right and %-EINVAL if something is wrong with TNC.
1243 int dbg_check_tnc(struct ubifs_info *c, int extra)
1245 struct ubifs_znode *znode;
1246 long clean_cnt = 0, dirty_cnt = 0;
1249 if (!(ubifs_chk_flags & UBIFS_CHK_TNC))
1252 ubifs_assert(mutex_is_locked(&c->tnc_mutex));
1253 if (!c->zroot.znode)
1256 znode = ubifs_tnc_postorder_first(c->zroot.znode);
1258 struct ubifs_znode *prev;
1259 struct ubifs_zbranch *zbr;
1264 zbr = &znode->parent->zbranch[znode->iip];
1266 err = dbg_check_znode(c, zbr);
1271 if (ubifs_zn_dirty(znode))
1278 znode = ubifs_tnc_postorder_next(znode);
1283 * If the last key of this znode is equivalent to the first key
1284 * of the next znode (collision), then check order of the keys.
1286 last = prev->child_cnt - 1;
1287 if (prev->level == 0 && znode->level == 0 && !c->replaying &&
1288 !keys_cmp(c, &prev->zbranch[last].key,
1289 &znode->zbranch[0].key)) {
1290 err = dbg_check_key_order(c, &prev->zbranch[last],
1291 &znode->zbranch[0]);
1295 ubifs_msg("first znode");
1296 dbg_dump_znode(c, prev);
1297 ubifs_msg("second znode");
1298 dbg_dump_znode(c, znode);
1305 if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
1306 ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld",
1307 atomic_long_read(&c->clean_zn_cnt),
1311 if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
1312 ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld",
1313 atomic_long_read(&c->dirty_zn_cnt),
1323 * dbg_walk_index - walk the on-flash index.
1324 * @c: UBIFS file-system description object
1325 * @leaf_cb: called for each leaf node
1326 * @znode_cb: called for each indexing node
1327 * @priv: private date which is passed to callbacks
1329 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1330 * node and @znode_cb for each indexing node. Returns zero in case of success
1331 * and a negative error code in case of failure.
1333 * It would be better if this function removed every znode it pulled to into
1334 * the TNC, so that the behavior more closely matched the non-debugging
1337 int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1338 dbg_znode_callback znode_cb, void *priv)
1341 struct ubifs_zbranch *zbr;
1342 struct ubifs_znode *znode, *child;
1344 mutex_lock(&c->tnc_mutex);
1345 /* If the root indexing node is not in TNC - pull it */
1346 if (!c->zroot.znode) {
1347 c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1348 if (IS_ERR(c->zroot.znode)) {
1349 err = PTR_ERR(c->zroot.znode);
1350 c->zroot.znode = NULL;
1356 * We are going to traverse the indexing tree in the postorder manner.
1357 * Go down and find the leftmost indexing node where we are going to
1360 znode = c->zroot.znode;
1361 while (znode->level > 0) {
1362 zbr = &znode->zbranch[0];
1365 child = ubifs_load_znode(c, zbr, znode, 0);
1366 if (IS_ERR(child)) {
1367 err = PTR_ERR(child);
1376 /* Iterate over all indexing nodes */
1383 err = znode_cb(c, znode, priv);
1385 ubifs_err("znode checking function returned "
1387 dbg_dump_znode(c, znode);
1391 if (leaf_cb && znode->level == 0) {
1392 for (idx = 0; idx < znode->child_cnt; idx++) {
1393 zbr = &znode->zbranch[idx];
1394 err = leaf_cb(c, zbr, priv);
1396 ubifs_err("leaf checking function "
1397 "returned error %d, for leaf "
1399 err, zbr->lnum, zbr->offs);
1408 idx = znode->iip + 1;
1409 znode = znode->parent;
1410 if (idx < znode->child_cnt) {
1411 /* Switch to the next index in the parent */
1412 zbr = &znode->zbranch[idx];
1415 child = ubifs_load_znode(c, zbr, znode, idx);
1416 if (IS_ERR(child)) {
1417 err = PTR_ERR(child);
1425 * This is the last child, switch to the parent and
1430 /* Go to the lowest leftmost znode in the new sub-tree */
1431 while (znode->level > 0) {
1432 zbr = &znode->zbranch[0];
1435 child = ubifs_load_znode(c, zbr, znode, 0);
1436 if (IS_ERR(child)) {
1437 err = PTR_ERR(child);
1446 mutex_unlock(&c->tnc_mutex);
1451 zbr = &znode->parent->zbranch[znode->iip];
1454 ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1455 dbg_dump_znode(c, znode);
1457 mutex_unlock(&c->tnc_mutex);
1462 * add_size - add znode size to partially calculated index size.
1463 * @c: UBIFS file-system description object
1464 * @znode: znode to add size for
1465 * @priv: partially calculated index size
1467 * This is a helper function for 'dbg_check_idx_size()' which is called for
1468 * every indexing node and adds its size to the 'long long' variable pointed to
1471 static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1473 long long *idx_size = priv;
1476 add = ubifs_idx_node_sz(c, znode->child_cnt);
1477 add = ALIGN(add, 8);
1483 * dbg_check_idx_size - check index size.
1484 * @c: UBIFS file-system description object
1485 * @idx_size: size to check
1487 * This function walks the UBIFS index, calculates its size and checks that the
1488 * size is equivalent to @idx_size. Returns zero in case of success and a
1489 * negative error code in case of failure.
1491 int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
1496 if (!(ubifs_chk_flags & UBIFS_CHK_IDX_SZ))
1499 err = dbg_walk_index(c, NULL, add_size, &calc);
1501 ubifs_err("error %d while walking the index", err);
1505 if (calc != idx_size) {
1506 ubifs_err("index size check failed: calculated size is %lld, "
1507 "should be %lld", calc, idx_size);
1516 * struct fsck_inode - information about an inode used when checking the file-system.
1517 * @rb: link in the RB-tree of inodes
1518 * @inum: inode number
1519 * @mode: inode type, permissions, etc
1520 * @nlink: inode link count
1521 * @xattr_cnt: count of extended attributes
1522 * @references: how many directory/xattr entries refer this inode (calculated
1523 * while walking the index)
1524 * @calc_cnt: for directory inode count of child directories
1525 * @size: inode size (read from on-flash inode)
1526 * @xattr_sz: summary size of all extended attributes (read from on-flash
1528 * @calc_sz: for directories calculated directory size
1529 * @calc_xcnt: count of extended attributes
1530 * @calc_xsz: calculated summary size of all extended attributes
1531 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1532 * inode (read from on-flash inode)
1533 * @calc_xnms: calculated sum of lengths of all extended attribute names
1540 unsigned int xattr_cnt;
1544 unsigned int xattr_sz;
1546 long long calc_xcnt;
1548 unsigned int xattr_nms;
1549 long long calc_xnms;
1553 * struct fsck_data - private FS checking information.
1554 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1557 struct rb_root inodes;
1561 * add_inode - add inode information to RB-tree of inodes.
1562 * @c: UBIFS file-system description object
1563 * @fsckd: FS checking information
1564 * @ino: raw UBIFS inode to add
1566 * This is a helper function for 'check_leaf()' which adds information about
1567 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1568 * case of success and a negative error code in case of failure.
1570 static struct fsck_inode *add_inode(struct ubifs_info *c,
1571 struct fsck_data *fsckd,
1572 struct ubifs_ino_node *ino)
1574 struct rb_node **p, *parent = NULL;
1575 struct fsck_inode *fscki;
1576 ino_t inum = key_inum_flash(c, &ino->key);
1578 p = &fsckd->inodes.rb_node;
1581 fscki = rb_entry(parent, struct fsck_inode, rb);
1582 if (inum < fscki->inum)
1584 else if (inum > fscki->inum)
1585 p = &(*p)->rb_right;
1590 if (inum > c->highest_inum) {
1591 ubifs_err("too high inode number, max. is %lu",
1593 return ERR_PTR(-EINVAL);
1596 fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
1598 return ERR_PTR(-ENOMEM);
1601 fscki->nlink = le32_to_cpu(ino->nlink);
1602 fscki->size = le64_to_cpu(ino->size);
1603 fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
1604 fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
1605 fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
1606 fscki->mode = le32_to_cpu(ino->mode);
1607 if (S_ISDIR(fscki->mode)) {
1608 fscki->calc_sz = UBIFS_INO_NODE_SZ;
1609 fscki->calc_cnt = 2;
1611 rb_link_node(&fscki->rb, parent, p);
1612 rb_insert_color(&fscki->rb, &fsckd->inodes);
1617 * search_inode - search inode in the RB-tree of inodes.
1618 * @fsckd: FS checking information
1619 * @inum: inode number to search
1621 * This is a helper function for 'check_leaf()' which searches inode @inum in
1622 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1623 * the inode was not found.
1625 static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
1628 struct fsck_inode *fscki;
1630 p = fsckd->inodes.rb_node;
1632 fscki = rb_entry(p, struct fsck_inode, rb);
1633 if (inum < fscki->inum)
1635 else if (inum > fscki->inum)
1644 * read_add_inode - read inode node and add it to RB-tree of inodes.
1645 * @c: UBIFS file-system description object
1646 * @fsckd: FS checking information
1647 * @inum: inode number to read
1649 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1650 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1651 * information pointer in case of success and a negative error code in case of
1654 static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1655 struct fsck_data *fsckd, ino_t inum)
1658 union ubifs_key key;
1659 struct ubifs_znode *znode;
1660 struct ubifs_zbranch *zbr;
1661 struct ubifs_ino_node *ino;
1662 struct fsck_inode *fscki;
1664 fscki = search_inode(fsckd, inum);
1668 ino_key_init(c, &key, inum);
1669 err = ubifs_lookup_level0(c, &key, &znode, &n);
1671 ubifs_err("inode %lu not found in index", inum);
1672 return ERR_PTR(-ENOENT);
1673 } else if (err < 0) {
1674 ubifs_err("error %d while looking up inode %lu", err, inum);
1675 return ERR_PTR(err);
1678 zbr = &znode->zbranch[n];
1679 if (zbr->len < UBIFS_INO_NODE_SZ) {
1680 ubifs_err("bad node %lu node length %d", inum, zbr->len);
1681 return ERR_PTR(-EINVAL);
1684 ino = kmalloc(zbr->len, GFP_NOFS);
1686 return ERR_PTR(-ENOMEM);
1688 err = ubifs_tnc_read_node(c, zbr, ino);
1690 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
1691 zbr->lnum, zbr->offs, err);
1693 return ERR_PTR(err);
1696 fscki = add_inode(c, fsckd, ino);
1698 if (IS_ERR(fscki)) {
1699 ubifs_err("error %ld while adding inode %lu node",
1700 PTR_ERR(fscki), inum);
1708 * check_leaf - check leaf node.
1709 * @c: UBIFS file-system description object
1710 * @zbr: zbranch of the leaf node to check
1711 * @priv: FS checking information
1713 * This is a helper function for 'dbg_check_filesystem()' which is called for
1714 * every single leaf node while walking the indexing tree. It checks that the
1715 * leaf node referred from the indexing tree exists, has correct CRC, and does
1716 * some other basic validation. This function is also responsible for building
1717 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
1718 * calculates reference count, size, etc for each inode in order to later
1719 * compare them to the information stored inside the inodes and detect possible
1720 * inconsistencies. Returns zero in case of success and a negative error code
1721 * in case of failure.
1723 static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
1728 struct ubifs_ch *ch;
1729 int err, type = key_type(c, &zbr->key);
1730 struct fsck_inode *fscki;
1732 if (zbr->len < UBIFS_CH_SZ) {
1733 ubifs_err("bad leaf length %d (LEB %d:%d)",
1734 zbr->len, zbr->lnum, zbr->offs);
1738 node = kmalloc(zbr->len, GFP_NOFS);
1742 err = ubifs_tnc_read_node(c, zbr, node);
1744 ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
1745 zbr->lnum, zbr->offs, err);
1749 /* If this is an inode node, add it to RB-tree of inodes */
1750 if (type == UBIFS_INO_KEY) {
1751 fscki = add_inode(c, priv, node);
1752 if (IS_ERR(fscki)) {
1753 err = PTR_ERR(fscki);
1754 ubifs_err("error %d while adding inode node", err);
1760 if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
1761 type != UBIFS_DATA_KEY) {
1762 ubifs_err("unexpected node type %d at LEB %d:%d",
1763 type, zbr->lnum, zbr->offs);
1769 if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
1770 ubifs_err("too high sequence number, max. is %llu",
1776 if (type == UBIFS_DATA_KEY) {
1778 struct ubifs_data_node *dn = node;
1781 * Search the inode node this data node belongs to and insert
1782 * it to the RB-tree of inodes.
1784 inum = key_inum_flash(c, &dn->key);
1785 fscki = read_add_inode(c, priv, inum);
1786 if (IS_ERR(fscki)) {
1787 err = PTR_ERR(fscki);
1788 ubifs_err("error %d while processing data node and "
1789 "trying to find inode node %lu", err, inum);
1793 /* Make sure the data node is within inode size */
1794 blk_offs = key_block_flash(c, &dn->key);
1795 blk_offs <<= UBIFS_BLOCK_SHIFT;
1796 blk_offs += le32_to_cpu(dn->size);
1797 if (blk_offs > fscki->size) {
1798 ubifs_err("data node at LEB %d:%d is not within inode "
1799 "size %lld", zbr->lnum, zbr->offs,
1806 struct ubifs_dent_node *dent = node;
1807 struct fsck_inode *fscki1;
1809 err = ubifs_validate_entry(c, dent);
1814 * Search the inode node this entry refers to and the parent
1815 * inode node and insert them to the RB-tree of inodes.
1817 inum = le64_to_cpu(dent->inum);
1818 fscki = read_add_inode(c, priv, inum);
1819 if (IS_ERR(fscki)) {
1820 err = PTR_ERR(fscki);
1821 ubifs_err("error %d while processing entry node and "
1822 "trying to find inode node %lu", err, inum);
1826 /* Count how many direntries or xentries refers this inode */
1827 fscki->references += 1;
1829 inum = key_inum_flash(c, &dent->key);
1830 fscki1 = read_add_inode(c, priv, inum);
1831 if (IS_ERR(fscki1)) {
1832 err = PTR_ERR(fscki);
1833 ubifs_err("error %d while processing entry node and "
1834 "trying to find parent inode node %lu",
1839 nlen = le16_to_cpu(dent->nlen);
1840 if (type == UBIFS_XENT_KEY) {
1841 fscki1->calc_xcnt += 1;
1842 fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
1843 fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
1844 fscki1->calc_xnms += nlen;
1846 fscki1->calc_sz += CALC_DENT_SIZE(nlen);
1847 if (dent->type == UBIFS_ITYPE_DIR)
1848 fscki1->calc_cnt += 1;
1857 ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
1858 dbg_dump_node(c, node);
1865 * free_inodes - free RB-tree of inodes.
1866 * @fsckd: FS checking information
1868 static void free_inodes(struct fsck_data *fsckd)
1870 struct rb_node *this = fsckd->inodes.rb_node;
1871 struct fsck_inode *fscki;
1875 this = this->rb_left;
1876 else if (this->rb_right)
1877 this = this->rb_right;
1879 fscki = rb_entry(this, struct fsck_inode, rb);
1880 this = rb_parent(this);
1882 if (this->rb_left == &fscki->rb)
1883 this->rb_left = NULL;
1885 this->rb_right = NULL;
1893 * check_inodes - checks all inodes.
1894 * @c: UBIFS file-system description object
1895 * @fsckd: FS checking information
1897 * This is a helper function for 'dbg_check_filesystem()' which walks the
1898 * RB-tree of inodes after the index scan has been finished, and checks that
1899 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
1900 * %-EINVAL if not, and a negative error code in case of failure.
1902 static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
1905 union ubifs_key key;
1906 struct ubifs_znode *znode;
1907 struct ubifs_zbranch *zbr;
1908 struct ubifs_ino_node *ino;
1909 struct fsck_inode *fscki;
1910 struct rb_node *this = rb_first(&fsckd->inodes);
1913 fscki = rb_entry(this, struct fsck_inode, rb);
1914 this = rb_next(this);
1916 if (S_ISDIR(fscki->mode)) {
1918 * Directories have to have exactly one reference (they
1919 * cannot have hardlinks), although root inode is an
1922 if (fscki->inum != UBIFS_ROOT_INO &&
1923 fscki->references != 1) {
1924 ubifs_err("directory inode %lu has %d "
1925 "direntries which refer it, but "
1926 "should be 1", fscki->inum,
1930 if (fscki->inum == UBIFS_ROOT_INO &&
1931 fscki->references != 0) {
1932 ubifs_err("root inode %lu has non-zero (%d) "
1933 "direntries which refer it",
1934 fscki->inum, fscki->references);
1937 if (fscki->calc_sz != fscki->size) {
1938 ubifs_err("directory inode %lu size is %lld, "
1939 "but calculated size is %lld",
1940 fscki->inum, fscki->size,
1944 if (fscki->calc_cnt != fscki->nlink) {
1945 ubifs_err("directory inode %lu nlink is %d, "
1946 "but calculated nlink is %d",
1947 fscki->inum, fscki->nlink,
1952 if (fscki->references != fscki->nlink) {
1953 ubifs_err("inode %lu nlink is %d, but "
1954 "calculated nlink is %d", fscki->inum,
1955 fscki->nlink, fscki->references);
1959 if (fscki->xattr_sz != fscki->calc_xsz) {
1960 ubifs_err("inode %lu has xattr size %u, but "
1961 "calculated size is %lld",
1962 fscki->inum, fscki->xattr_sz,
1966 if (fscki->xattr_cnt != fscki->calc_xcnt) {
1967 ubifs_err("inode %lu has %u xattrs, but "
1968 "calculated count is %lld", fscki->inum,
1969 fscki->xattr_cnt, fscki->calc_xcnt);
1972 if (fscki->xattr_nms != fscki->calc_xnms) {
1973 ubifs_err("inode %lu has xattr names' size %u, but "
1974 "calculated names' size is %lld",
1975 fscki->inum, fscki->xattr_nms,
1984 /* Read the bad inode and dump it */
1985 ino_key_init(c, &key, fscki->inum);
1986 err = ubifs_lookup_level0(c, &key, &znode, &n);
1988 ubifs_err("inode %lu not found in index", fscki->inum);
1990 } else if (err < 0) {
1991 ubifs_err("error %d while looking up inode %lu",
1996 zbr = &znode->zbranch[n];
1997 ino = kmalloc(zbr->len, GFP_NOFS);
2001 err = ubifs_tnc_read_node(c, zbr, ino);
2003 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2004 zbr->lnum, zbr->offs, err);
2009 ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",
2010 fscki->inum, zbr->lnum, zbr->offs);
2011 dbg_dump_node(c, ino);
2017 * dbg_check_filesystem - check the file-system.
2018 * @c: UBIFS file-system description object
2020 * This function checks the file system, namely:
2021 * o makes sure that all leaf nodes exist and their CRCs are correct;
2022 * o makes sure inode nlink, size, xattr size/count are correct (for all
2025 * The function reads whole indexing tree and all nodes, so it is pretty
2026 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2027 * not, and a negative error code in case of failure.
2029 int dbg_check_filesystem(struct ubifs_info *c)
2032 struct fsck_data fsckd;
2034 if (!(ubifs_chk_flags & UBIFS_CHK_FS))
2037 fsckd.inodes = RB_ROOT;
2038 err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
2042 err = check_inodes(c, &fsckd);
2046 free_inodes(&fsckd);
2050 ubifs_err("file-system check failed with error %d", err);
2052 free_inodes(&fsckd);
2056 static int invocation_cnt;
2058 int dbg_force_in_the_gaps(void)
2060 if (!dbg_force_in_the_gaps_enabled)
2062 /* Force in-the-gaps every 8th commit */
2063 return !((invocation_cnt++) & 0x7);
2066 /* Failure mode for recovery testing */
2068 #define chance(n, d) (simple_rand() <= (n) * 32768LL / (d))
2070 struct failure_mode_info {
2071 struct list_head list;
2072 struct ubifs_info *c;
2075 static LIST_HEAD(fmi_list);
2076 static DEFINE_SPINLOCK(fmi_lock);
2078 static unsigned int next;
2080 static int simple_rand(void)
2083 next = current->pid;
2084 next = next * 1103515245 + 12345;
2085 return (next >> 16) & 32767;
2088 void dbg_failure_mode_registration(struct ubifs_info *c)
2090 struct failure_mode_info *fmi;
2092 fmi = kmalloc(sizeof(struct failure_mode_info), GFP_NOFS);
2094 dbg_err("Failed to register failure mode - no memory");
2098 spin_lock(&fmi_lock);
2099 list_add_tail(&fmi->list, &fmi_list);
2100 spin_unlock(&fmi_lock);
2103 void dbg_failure_mode_deregistration(struct ubifs_info *c)
2105 struct failure_mode_info *fmi, *tmp;
2107 spin_lock(&fmi_lock);
2108 list_for_each_entry_safe(fmi, tmp, &fmi_list, list)
2110 list_del(&fmi->list);
2113 spin_unlock(&fmi_lock);
2116 static struct ubifs_info *dbg_find_info(struct ubi_volume_desc *desc)
2118 struct failure_mode_info *fmi;
2120 spin_lock(&fmi_lock);
2121 list_for_each_entry(fmi, &fmi_list, list)
2122 if (fmi->c->ubi == desc) {
2123 struct ubifs_info *c = fmi->c;
2125 spin_unlock(&fmi_lock);
2128 spin_unlock(&fmi_lock);
2132 static int in_failure_mode(struct ubi_volume_desc *desc)
2134 struct ubifs_info *c = dbg_find_info(desc);
2136 if (c && dbg_failure_mode)
2137 return c->failure_mode;
2141 static int do_fail(struct ubi_volume_desc *desc, int lnum, int write)
2143 struct ubifs_info *c = dbg_find_info(desc);
2145 if (!c || !dbg_failure_mode)
2147 if (c->failure_mode)
2150 /* First call - decide delay to failure */
2152 unsigned int delay = 1 << (simple_rand() >> 11);
2156 c->fail_timeout = jiffies +
2157 msecs_to_jiffies(delay);
2158 dbg_rcvry("failing after %ums", delay);
2161 c->fail_cnt_max = delay;
2162 dbg_rcvry("failing after %u calls", delay);
2167 /* Determine if failure delay has expired */
2168 if (c->fail_delay == 1) {
2169 if (time_before(jiffies, c->fail_timeout))
2171 } else if (c->fail_delay == 2)
2172 if (c->fail_cnt++ < c->fail_cnt_max)
2174 if (lnum == UBIFS_SB_LNUM) {
2178 } else if (chance(19, 20))
2180 dbg_rcvry("failing in super block LEB %d", lnum);
2181 } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2184 dbg_rcvry("failing in master LEB %d", lnum);
2185 } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2187 if (chance(99, 100))
2189 } else if (chance(399, 400))
2191 dbg_rcvry("failing in log LEB %d", lnum);
2192 } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2196 } else if (chance(19, 20))
2198 dbg_rcvry("failing in LPT LEB %d", lnum);
2199 } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2203 } else if (chance(9, 10))
2205 dbg_rcvry("failing in orphan LEB %d", lnum);
2206 } else if (lnum == c->ihead_lnum) {
2207 if (chance(99, 100))
2209 dbg_rcvry("failing in index head LEB %d", lnum);
2210 } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2213 dbg_rcvry("failing in GC head LEB %d", lnum);
2214 } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
2215 !ubifs_search_bud(c, lnum)) {
2218 dbg_rcvry("failing in non-bud LEB %d", lnum);
2219 } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
2220 c->cmt_state == COMMIT_RUNNING_REQUIRED) {
2221 if (chance(999, 1000))
2223 dbg_rcvry("failing in bud LEB %d commit running", lnum);
2225 if (chance(9999, 10000))
2227 dbg_rcvry("failing in bud LEB %d commit not running", lnum);
2229 ubifs_err("*** SETTING FAILURE MODE ON (LEB %d) ***", lnum);
2230 c->failure_mode = 1;
2235 static void cut_data(const void *buf, int len)
2238 unsigned char *p = (void *)buf;
2240 flen = (len * (long long)simple_rand()) >> 15;
2241 for (i = flen; i < len; i++)
2245 int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
2248 if (in_failure_mode(desc))
2250 return ubi_leb_read(desc, lnum, buf, offset, len, check);
2253 int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
2254 int offset, int len, int dtype)
2258 if (in_failure_mode(desc))
2260 failing = do_fail(desc, lnum, 1);
2263 err = ubi_leb_write(desc, lnum, buf, offset, len, dtype);
2271 int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
2276 if (do_fail(desc, lnum, 1))
2278 err = ubi_leb_change(desc, lnum, buf, len, dtype);
2281 if (do_fail(desc, lnum, 1))
2286 int dbg_leb_erase(struct ubi_volume_desc *desc, int lnum)
2290 if (do_fail(desc, lnum, 0))
2292 err = ubi_leb_erase(desc, lnum);
2295 if (do_fail(desc, lnum, 0))
2300 int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum)
2304 if (do_fail(desc, lnum, 0))
2306 err = ubi_leb_unmap(desc, lnum);
2309 if (do_fail(desc, lnum, 0))
2314 int dbg_is_mapped(struct ubi_volume_desc *desc, int lnum)
2316 if (in_failure_mode(desc))
2318 return ubi_is_mapped(desc, lnum);
2321 int dbg_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
2325 if (do_fail(desc, lnum, 0))
2327 err = ubi_leb_map(desc, lnum, dtype);
2330 if (do_fail(desc, lnum, 0))
2335 #endif /* CONFIG_UBIFS_FS_DEBUG */