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>
35 #include <linux/debugfs.h>
36 #include <linux/math64.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 unsigned int ubifs_msg_flags
= UBIFS_MSG_FLAGS_DEFAULT
;
46 unsigned int ubifs_chk_flags
= UBIFS_CHK_FLAGS_DEFAULT
;
47 unsigned int ubifs_tst_flags
;
49 module_param_named(debug_msgs
, ubifs_msg_flags
, uint
, S_IRUGO
| S_IWUSR
);
50 module_param_named(debug_chks
, ubifs_chk_flags
, uint
, S_IRUGO
| S_IWUSR
);
51 module_param_named(debug_tsts
, ubifs_tst_flags
, uint
, S_IRUGO
| S_IWUSR
);
53 MODULE_PARM_DESC(debug_msgs
, "Debug message type flags");
54 MODULE_PARM_DESC(debug_chks
, "Debug check flags");
55 MODULE_PARM_DESC(debug_tsts
, "Debug special test flags");
57 static const char *get_key_fmt(int fmt
)
60 case UBIFS_SIMPLE_KEY_FMT
:
63 return "unknown/invalid format";
67 static const char *get_key_hash(int hash
)
70 case UBIFS_KEY_HASH_R5
:
72 case UBIFS_KEY_HASH_TEST
:
75 return "unknown/invalid name hash";
79 static const char *get_key_type(int type
)
93 return "unknown/invalid key";
97 static void sprintf_key(const struct ubifs_info
*c
, const union ubifs_key
*key
,
101 int type
= key_type(c
, key
);
103 if (c
->key_fmt
== UBIFS_SIMPLE_KEY_FMT
) {
106 sprintf(p
, "(%lu, %s)", (unsigned long)key_inum(c
, key
),
111 sprintf(p
, "(%lu, %s, %#08x)",
112 (unsigned long)key_inum(c
, key
),
113 get_key_type(type
), key_hash(c
, key
));
116 sprintf(p
, "(%lu, %s, %u)",
117 (unsigned long)key_inum(c
, key
),
118 get_key_type(type
), key_block(c
, key
));
121 sprintf(p
, "(%lu, %s)",
122 (unsigned long)key_inum(c
, key
),
126 sprintf(p
, "(bad key type: %#08x, %#08x)",
127 key
->u32
[0], key
->u32
[1]);
130 sprintf(p
, "bad key format %d", c
->key_fmt
);
133 const char *dbg_key_str0(const struct ubifs_info
*c
, const union ubifs_key
*key
)
135 /* dbg_lock must be held */
136 sprintf_key(c
, key
, dbg_key_buf0
);
140 const char *dbg_key_str1(const struct ubifs_info
*c
, const union ubifs_key
*key
)
142 /* dbg_lock must be held */
143 sprintf_key(c
, key
, dbg_key_buf1
);
147 const char *dbg_ntype(int type
)
151 return "padding node";
153 return "superblock node";
155 return "master node";
157 return "reference node";
160 case UBIFS_DENT_NODE
:
161 return "direntry node";
162 case UBIFS_XENT_NODE
:
163 return "xentry node";
164 case UBIFS_DATA_NODE
:
166 case UBIFS_TRUN_NODE
:
167 return "truncate node";
169 return "indexing node";
171 return "commit start node";
172 case UBIFS_ORPH_NODE
:
173 return "orphan node";
175 return "unknown node";
179 static const char *dbg_gtype(int type
)
182 case UBIFS_NO_NODE_GROUP
:
183 return "no node group";
184 case UBIFS_IN_NODE_GROUP
:
185 return "in node group";
186 case UBIFS_LAST_OF_NODE_GROUP
:
187 return "last of node group";
193 const char *dbg_cstate(int cmt_state
)
197 return "commit resting";
198 case COMMIT_BACKGROUND
:
199 return "background commit requested";
200 case COMMIT_REQUIRED
:
201 return "commit required";
202 case COMMIT_RUNNING_BACKGROUND
:
203 return "BACKGROUND commit running";
204 case COMMIT_RUNNING_REQUIRED
:
205 return "commit running and required";
207 return "broken commit";
209 return "unknown commit state";
213 const char *dbg_jhead(int jhead
)
223 return "unknown journal head";
227 static void dump_ch(const struct ubifs_ch
*ch
)
229 printk(KERN_DEBUG
"\tmagic %#x\n", le32_to_cpu(ch
->magic
));
230 printk(KERN_DEBUG
"\tcrc %#x\n", le32_to_cpu(ch
->crc
));
231 printk(KERN_DEBUG
"\tnode_type %d (%s)\n", ch
->node_type
,
232 dbg_ntype(ch
->node_type
));
233 printk(KERN_DEBUG
"\tgroup_type %d (%s)\n", ch
->group_type
,
234 dbg_gtype(ch
->group_type
));
235 printk(KERN_DEBUG
"\tsqnum %llu\n",
236 (unsigned long long)le64_to_cpu(ch
->sqnum
));
237 printk(KERN_DEBUG
"\tlen %u\n", le32_to_cpu(ch
->len
));
240 void dbg_dump_inode(const struct ubifs_info
*c
, const struct inode
*inode
)
242 const struct ubifs_inode
*ui
= ubifs_inode(inode
);
244 printk(KERN_DEBUG
"Dump in-memory inode:");
245 printk(KERN_DEBUG
"\tinode %lu\n", inode
->i_ino
);
246 printk(KERN_DEBUG
"\tsize %llu\n",
247 (unsigned long long)i_size_read(inode
));
248 printk(KERN_DEBUG
"\tnlink %u\n", inode
->i_nlink
);
249 printk(KERN_DEBUG
"\tuid %u\n", (unsigned int)inode
->i_uid
);
250 printk(KERN_DEBUG
"\tgid %u\n", (unsigned int)inode
->i_gid
);
251 printk(KERN_DEBUG
"\tatime %u.%u\n",
252 (unsigned int)inode
->i_atime
.tv_sec
,
253 (unsigned int)inode
->i_atime
.tv_nsec
);
254 printk(KERN_DEBUG
"\tmtime %u.%u\n",
255 (unsigned int)inode
->i_mtime
.tv_sec
,
256 (unsigned int)inode
->i_mtime
.tv_nsec
);
257 printk(KERN_DEBUG
"\tctime %u.%u\n",
258 (unsigned int)inode
->i_ctime
.tv_sec
,
259 (unsigned int)inode
->i_ctime
.tv_nsec
);
260 printk(KERN_DEBUG
"\tcreat_sqnum %llu\n", ui
->creat_sqnum
);
261 printk(KERN_DEBUG
"\txattr_size %u\n", ui
->xattr_size
);
262 printk(KERN_DEBUG
"\txattr_cnt %u\n", ui
->xattr_cnt
);
263 printk(KERN_DEBUG
"\txattr_names %u\n", ui
->xattr_names
);
264 printk(KERN_DEBUG
"\tdirty %u\n", ui
->dirty
);
265 printk(KERN_DEBUG
"\txattr %u\n", ui
->xattr
);
266 printk(KERN_DEBUG
"\tbulk_read %u\n", ui
->xattr
);
267 printk(KERN_DEBUG
"\tsynced_i_size %llu\n",
268 (unsigned long long)ui
->synced_i_size
);
269 printk(KERN_DEBUG
"\tui_size %llu\n",
270 (unsigned long long)ui
->ui_size
);
271 printk(KERN_DEBUG
"\tflags %d\n", ui
->flags
);
272 printk(KERN_DEBUG
"\tcompr_type %d\n", ui
->compr_type
);
273 printk(KERN_DEBUG
"\tlast_page_read %lu\n", ui
->last_page_read
);
274 printk(KERN_DEBUG
"\tread_in_a_row %lu\n", ui
->read_in_a_row
);
275 printk(KERN_DEBUG
"\tdata_len %d\n", ui
->data_len
);
278 void dbg_dump_node(const struct ubifs_info
*c
, const void *node
)
282 const struct ubifs_ch
*ch
= node
;
284 if (dbg_failure_mode
)
287 /* If the magic is incorrect, just hexdump the first bytes */
288 if (le32_to_cpu(ch
->magic
) != UBIFS_NODE_MAGIC
) {
289 printk(KERN_DEBUG
"Not a node, first %zu bytes:", UBIFS_CH_SZ
);
290 print_hex_dump(KERN_DEBUG
, "", DUMP_PREFIX_OFFSET
, 32, 1,
291 (void *)node
, UBIFS_CH_SZ
, 1);
295 spin_lock(&dbg_lock
);
298 switch (ch
->node_type
) {
301 const struct ubifs_pad_node
*pad
= node
;
303 printk(KERN_DEBUG
"\tpad_len %u\n",
304 le32_to_cpu(pad
->pad_len
));
309 const struct ubifs_sb_node
*sup
= node
;
310 unsigned int sup_flags
= le32_to_cpu(sup
->flags
);
312 printk(KERN_DEBUG
"\tkey_hash %d (%s)\n",
313 (int)sup
->key_hash
, get_key_hash(sup
->key_hash
));
314 printk(KERN_DEBUG
"\tkey_fmt %d (%s)\n",
315 (int)sup
->key_fmt
, get_key_fmt(sup
->key_fmt
));
316 printk(KERN_DEBUG
"\tflags %#x\n", sup_flags
);
317 printk(KERN_DEBUG
"\t big_lpt %u\n",
318 !!(sup_flags
& UBIFS_FLG_BIGLPT
));
319 printk(KERN_DEBUG
"\tmin_io_size %u\n",
320 le32_to_cpu(sup
->min_io_size
));
321 printk(KERN_DEBUG
"\tleb_size %u\n",
322 le32_to_cpu(sup
->leb_size
));
323 printk(KERN_DEBUG
"\tleb_cnt %u\n",
324 le32_to_cpu(sup
->leb_cnt
));
325 printk(KERN_DEBUG
"\tmax_leb_cnt %u\n",
326 le32_to_cpu(sup
->max_leb_cnt
));
327 printk(KERN_DEBUG
"\tmax_bud_bytes %llu\n",
328 (unsigned long long)le64_to_cpu(sup
->max_bud_bytes
));
329 printk(KERN_DEBUG
"\tlog_lebs %u\n",
330 le32_to_cpu(sup
->log_lebs
));
331 printk(KERN_DEBUG
"\tlpt_lebs %u\n",
332 le32_to_cpu(sup
->lpt_lebs
));
333 printk(KERN_DEBUG
"\torph_lebs %u\n",
334 le32_to_cpu(sup
->orph_lebs
));
335 printk(KERN_DEBUG
"\tjhead_cnt %u\n",
336 le32_to_cpu(sup
->jhead_cnt
));
337 printk(KERN_DEBUG
"\tfanout %u\n",
338 le32_to_cpu(sup
->fanout
));
339 printk(KERN_DEBUG
"\tlsave_cnt %u\n",
340 le32_to_cpu(sup
->lsave_cnt
));
341 printk(KERN_DEBUG
"\tdefault_compr %u\n",
342 (int)le16_to_cpu(sup
->default_compr
));
343 printk(KERN_DEBUG
"\trp_size %llu\n",
344 (unsigned long long)le64_to_cpu(sup
->rp_size
));
345 printk(KERN_DEBUG
"\trp_uid %u\n",
346 le32_to_cpu(sup
->rp_uid
));
347 printk(KERN_DEBUG
"\trp_gid %u\n",
348 le32_to_cpu(sup
->rp_gid
));
349 printk(KERN_DEBUG
"\tfmt_version %u\n",
350 le32_to_cpu(sup
->fmt_version
));
351 printk(KERN_DEBUG
"\ttime_gran %u\n",
352 le32_to_cpu(sup
->time_gran
));
353 printk(KERN_DEBUG
"\tUUID %02X%02X%02X%02X-%02X%02X"
354 "-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
355 sup
->uuid
[0], sup
->uuid
[1], sup
->uuid
[2], sup
->uuid
[3],
356 sup
->uuid
[4], sup
->uuid
[5], sup
->uuid
[6], sup
->uuid
[7],
357 sup
->uuid
[8], sup
->uuid
[9], sup
->uuid
[10], sup
->uuid
[11],
358 sup
->uuid
[12], sup
->uuid
[13], sup
->uuid
[14],
364 const struct ubifs_mst_node
*mst
= node
;
366 printk(KERN_DEBUG
"\thighest_inum %llu\n",
367 (unsigned long long)le64_to_cpu(mst
->highest_inum
));
368 printk(KERN_DEBUG
"\tcommit number %llu\n",
369 (unsigned long long)le64_to_cpu(mst
->cmt_no
));
370 printk(KERN_DEBUG
"\tflags %#x\n",
371 le32_to_cpu(mst
->flags
));
372 printk(KERN_DEBUG
"\tlog_lnum %u\n",
373 le32_to_cpu(mst
->log_lnum
));
374 printk(KERN_DEBUG
"\troot_lnum %u\n",
375 le32_to_cpu(mst
->root_lnum
));
376 printk(KERN_DEBUG
"\troot_offs %u\n",
377 le32_to_cpu(mst
->root_offs
));
378 printk(KERN_DEBUG
"\troot_len %u\n",
379 le32_to_cpu(mst
->root_len
));
380 printk(KERN_DEBUG
"\tgc_lnum %u\n",
381 le32_to_cpu(mst
->gc_lnum
));
382 printk(KERN_DEBUG
"\tihead_lnum %u\n",
383 le32_to_cpu(mst
->ihead_lnum
));
384 printk(KERN_DEBUG
"\tihead_offs %u\n",
385 le32_to_cpu(mst
->ihead_offs
));
386 printk(KERN_DEBUG
"\tindex_size %llu\n",
387 (unsigned long long)le64_to_cpu(mst
->index_size
));
388 printk(KERN_DEBUG
"\tlpt_lnum %u\n",
389 le32_to_cpu(mst
->lpt_lnum
));
390 printk(KERN_DEBUG
"\tlpt_offs %u\n",
391 le32_to_cpu(mst
->lpt_offs
));
392 printk(KERN_DEBUG
"\tnhead_lnum %u\n",
393 le32_to_cpu(mst
->nhead_lnum
));
394 printk(KERN_DEBUG
"\tnhead_offs %u\n",
395 le32_to_cpu(mst
->nhead_offs
));
396 printk(KERN_DEBUG
"\tltab_lnum %u\n",
397 le32_to_cpu(mst
->ltab_lnum
));
398 printk(KERN_DEBUG
"\tltab_offs %u\n",
399 le32_to_cpu(mst
->ltab_offs
));
400 printk(KERN_DEBUG
"\tlsave_lnum %u\n",
401 le32_to_cpu(mst
->lsave_lnum
));
402 printk(KERN_DEBUG
"\tlsave_offs %u\n",
403 le32_to_cpu(mst
->lsave_offs
));
404 printk(KERN_DEBUG
"\tlscan_lnum %u\n",
405 le32_to_cpu(mst
->lscan_lnum
));
406 printk(KERN_DEBUG
"\tleb_cnt %u\n",
407 le32_to_cpu(mst
->leb_cnt
));
408 printk(KERN_DEBUG
"\tempty_lebs %u\n",
409 le32_to_cpu(mst
->empty_lebs
));
410 printk(KERN_DEBUG
"\tidx_lebs %u\n",
411 le32_to_cpu(mst
->idx_lebs
));
412 printk(KERN_DEBUG
"\ttotal_free %llu\n",
413 (unsigned long long)le64_to_cpu(mst
->total_free
));
414 printk(KERN_DEBUG
"\ttotal_dirty %llu\n",
415 (unsigned long long)le64_to_cpu(mst
->total_dirty
));
416 printk(KERN_DEBUG
"\ttotal_used %llu\n",
417 (unsigned long long)le64_to_cpu(mst
->total_used
));
418 printk(KERN_DEBUG
"\ttotal_dead %llu\n",
419 (unsigned long long)le64_to_cpu(mst
->total_dead
));
420 printk(KERN_DEBUG
"\ttotal_dark %llu\n",
421 (unsigned long long)le64_to_cpu(mst
->total_dark
));
426 const struct ubifs_ref_node
*ref
= node
;
428 printk(KERN_DEBUG
"\tlnum %u\n",
429 le32_to_cpu(ref
->lnum
));
430 printk(KERN_DEBUG
"\toffs %u\n",
431 le32_to_cpu(ref
->offs
));
432 printk(KERN_DEBUG
"\tjhead %u\n",
433 le32_to_cpu(ref
->jhead
));
438 const struct ubifs_ino_node
*ino
= node
;
440 key_read(c
, &ino
->key
, &key
);
441 printk(KERN_DEBUG
"\tkey %s\n", DBGKEY(&key
));
442 printk(KERN_DEBUG
"\tcreat_sqnum %llu\n",
443 (unsigned long long)le64_to_cpu(ino
->creat_sqnum
));
444 printk(KERN_DEBUG
"\tsize %llu\n",
445 (unsigned long long)le64_to_cpu(ino
->size
));
446 printk(KERN_DEBUG
"\tnlink %u\n",
447 le32_to_cpu(ino
->nlink
));
448 printk(KERN_DEBUG
"\tatime %lld.%u\n",
449 (long long)le64_to_cpu(ino
->atime_sec
),
450 le32_to_cpu(ino
->atime_nsec
));
451 printk(KERN_DEBUG
"\tmtime %lld.%u\n",
452 (long long)le64_to_cpu(ino
->mtime_sec
),
453 le32_to_cpu(ino
->mtime_nsec
));
454 printk(KERN_DEBUG
"\tctime %lld.%u\n",
455 (long long)le64_to_cpu(ino
->ctime_sec
),
456 le32_to_cpu(ino
->ctime_nsec
));
457 printk(KERN_DEBUG
"\tuid %u\n",
458 le32_to_cpu(ino
->uid
));
459 printk(KERN_DEBUG
"\tgid %u\n",
460 le32_to_cpu(ino
->gid
));
461 printk(KERN_DEBUG
"\tmode %u\n",
462 le32_to_cpu(ino
->mode
));
463 printk(KERN_DEBUG
"\tflags %#x\n",
464 le32_to_cpu(ino
->flags
));
465 printk(KERN_DEBUG
"\txattr_cnt %u\n",
466 le32_to_cpu(ino
->xattr_cnt
));
467 printk(KERN_DEBUG
"\txattr_size %u\n",
468 le32_to_cpu(ino
->xattr_size
));
469 printk(KERN_DEBUG
"\txattr_names %u\n",
470 le32_to_cpu(ino
->xattr_names
));
471 printk(KERN_DEBUG
"\tcompr_type %#x\n",
472 (int)le16_to_cpu(ino
->compr_type
));
473 printk(KERN_DEBUG
"\tdata len %u\n",
474 le32_to_cpu(ino
->data_len
));
477 case UBIFS_DENT_NODE
:
478 case UBIFS_XENT_NODE
:
480 const struct ubifs_dent_node
*dent
= node
;
481 int nlen
= le16_to_cpu(dent
->nlen
);
483 key_read(c
, &dent
->key
, &key
);
484 printk(KERN_DEBUG
"\tkey %s\n", DBGKEY(&key
));
485 printk(KERN_DEBUG
"\tinum %llu\n",
486 (unsigned long long)le64_to_cpu(dent
->inum
));
487 printk(KERN_DEBUG
"\ttype %d\n", (int)dent
->type
);
488 printk(KERN_DEBUG
"\tnlen %d\n", nlen
);
489 printk(KERN_DEBUG
"\tname ");
491 if (nlen
> UBIFS_MAX_NLEN
)
492 printk(KERN_DEBUG
"(bad name length, not printing, "
493 "bad or corrupted node)");
495 for (i
= 0; i
< nlen
&& dent
->name
[i
]; i
++)
496 printk(KERN_CONT
"%c", dent
->name
[i
]);
498 printk(KERN_CONT
"\n");
502 case UBIFS_DATA_NODE
:
504 const struct ubifs_data_node
*dn
= node
;
505 int dlen
= le32_to_cpu(ch
->len
) - UBIFS_DATA_NODE_SZ
;
507 key_read(c
, &dn
->key
, &key
);
508 printk(KERN_DEBUG
"\tkey %s\n", DBGKEY(&key
));
509 printk(KERN_DEBUG
"\tsize %u\n",
510 le32_to_cpu(dn
->size
));
511 printk(KERN_DEBUG
"\tcompr_typ %d\n",
512 (int)le16_to_cpu(dn
->compr_type
));
513 printk(KERN_DEBUG
"\tdata size %d\n",
515 printk(KERN_DEBUG
"\tdata:\n");
516 print_hex_dump(KERN_DEBUG
, "\t", DUMP_PREFIX_OFFSET
, 32, 1,
517 (void *)&dn
->data
, dlen
, 0);
520 case UBIFS_TRUN_NODE
:
522 const struct ubifs_trun_node
*trun
= node
;
524 printk(KERN_DEBUG
"\tinum %u\n",
525 le32_to_cpu(trun
->inum
));
526 printk(KERN_DEBUG
"\told_size %llu\n",
527 (unsigned long long)le64_to_cpu(trun
->old_size
));
528 printk(KERN_DEBUG
"\tnew_size %llu\n",
529 (unsigned long long)le64_to_cpu(trun
->new_size
));
534 const struct ubifs_idx_node
*idx
= node
;
536 n
= le16_to_cpu(idx
->child_cnt
);
537 printk(KERN_DEBUG
"\tchild_cnt %d\n", n
);
538 printk(KERN_DEBUG
"\tlevel %d\n",
539 (int)le16_to_cpu(idx
->level
));
540 printk(KERN_DEBUG
"\tBranches:\n");
542 for (i
= 0; i
< n
&& i
< c
->fanout
- 1; i
++) {
543 const struct ubifs_branch
*br
;
545 br
= ubifs_idx_branch(c
, idx
, i
);
546 key_read(c
, &br
->key
, &key
);
547 printk(KERN_DEBUG
"\t%d: LEB %d:%d len %d key %s\n",
548 i
, le32_to_cpu(br
->lnum
), le32_to_cpu(br
->offs
),
549 le32_to_cpu(br
->len
), DBGKEY(&key
));
555 case UBIFS_ORPH_NODE
:
557 const struct ubifs_orph_node
*orph
= node
;
559 printk(KERN_DEBUG
"\tcommit number %llu\n",
561 le64_to_cpu(orph
->cmt_no
) & LLONG_MAX
);
562 printk(KERN_DEBUG
"\tlast node flag %llu\n",
563 (unsigned long long)(le64_to_cpu(orph
->cmt_no
)) >> 63);
564 n
= (le32_to_cpu(ch
->len
) - UBIFS_ORPH_NODE_SZ
) >> 3;
565 printk(KERN_DEBUG
"\t%d orphan inode numbers:\n", n
);
566 for (i
= 0; i
< n
; i
++)
567 printk(KERN_DEBUG
"\t ino %llu\n",
568 (unsigned long long)le64_to_cpu(orph
->inos
[i
]));
572 printk(KERN_DEBUG
"node type %d was not recognized\n",
575 spin_unlock(&dbg_lock
);
578 void dbg_dump_budget_req(const struct ubifs_budget_req
*req
)
580 spin_lock(&dbg_lock
);
581 printk(KERN_DEBUG
"Budgeting request: new_ino %d, dirtied_ino %d\n",
582 req
->new_ino
, req
->dirtied_ino
);
583 printk(KERN_DEBUG
"\tnew_ino_d %d, dirtied_ino_d %d\n",
584 req
->new_ino_d
, req
->dirtied_ino_d
);
585 printk(KERN_DEBUG
"\tnew_page %d, dirtied_page %d\n",
586 req
->new_page
, req
->dirtied_page
);
587 printk(KERN_DEBUG
"\tnew_dent %d, mod_dent %d\n",
588 req
->new_dent
, req
->mod_dent
);
589 printk(KERN_DEBUG
"\tidx_growth %d\n", req
->idx_growth
);
590 printk(KERN_DEBUG
"\tdata_growth %d dd_growth %d\n",
591 req
->data_growth
, req
->dd_growth
);
592 spin_unlock(&dbg_lock
);
595 void dbg_dump_lstats(const struct ubifs_lp_stats
*lst
)
597 spin_lock(&dbg_lock
);
598 printk(KERN_DEBUG
"(pid %d) Lprops statistics: empty_lebs %d, "
599 "idx_lebs %d\n", current
->pid
, lst
->empty_lebs
, lst
->idx_lebs
);
600 printk(KERN_DEBUG
"\ttaken_empty_lebs %d, total_free %lld, "
601 "total_dirty %lld\n", lst
->taken_empty_lebs
, lst
->total_free
,
603 printk(KERN_DEBUG
"\ttotal_used %lld, total_dark %lld, "
604 "total_dead %lld\n", lst
->total_used
, lst
->total_dark
,
606 spin_unlock(&dbg_lock
);
609 void dbg_dump_budg(struct ubifs_info
*c
)
613 struct ubifs_bud
*bud
;
614 struct ubifs_gced_idx_leb
*idx_gc
;
615 long long available
, outstanding
, free
;
617 ubifs_assert(spin_is_locked(&c
->space_lock
));
618 spin_lock(&dbg_lock
);
619 printk(KERN_DEBUG
"(pid %d) Budgeting info: budg_data_growth %lld, "
620 "budg_dd_growth %lld, budg_idx_growth %lld\n", current
->pid
,
621 c
->budg_data_growth
, c
->budg_dd_growth
, c
->budg_idx_growth
);
622 printk(KERN_DEBUG
"\tdata budget sum %lld, total budget sum %lld, "
623 "freeable_cnt %d\n", c
->budg_data_growth
+ c
->budg_dd_growth
,
624 c
->budg_data_growth
+ c
->budg_dd_growth
+ c
->budg_idx_growth
,
626 printk(KERN_DEBUG
"\tmin_idx_lebs %d, old_idx_sz %lld, "
627 "calc_idx_sz %lld, idx_gc_cnt %d\n", c
->min_idx_lebs
,
628 c
->old_idx_sz
, c
->calc_idx_sz
, c
->idx_gc_cnt
);
629 printk(KERN_DEBUG
"\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, "
630 "clean_zn_cnt %ld\n", atomic_long_read(&c
->dirty_pg_cnt
),
631 atomic_long_read(&c
->dirty_zn_cnt
),
632 atomic_long_read(&c
->clean_zn_cnt
));
633 printk(KERN_DEBUG
"\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
634 c
->dark_wm
, c
->dead_wm
, c
->max_idx_node_sz
);
635 printk(KERN_DEBUG
"\tgc_lnum %d, ihead_lnum %d\n",
636 c
->gc_lnum
, c
->ihead_lnum
);
637 /* If we are in R/O mode, journal heads do not exist */
639 for (i
= 0; i
< c
->jhead_cnt
; i
++)
640 printk(KERN_DEBUG
"\tjhead %s\t LEB %d\n",
641 dbg_jhead(c
->jheads
[i
].wbuf
.jhead
),
642 c
->jheads
[i
].wbuf
.lnum
);
643 for (rb
= rb_first(&c
->buds
); rb
; rb
= rb_next(rb
)) {
644 bud
= rb_entry(rb
, struct ubifs_bud
, rb
);
645 printk(KERN_DEBUG
"\tbud LEB %d\n", bud
->lnum
);
647 list_for_each_entry(bud
, &c
->old_buds
, list
)
648 printk(KERN_DEBUG
"\told bud LEB %d\n", bud
->lnum
);
649 list_for_each_entry(idx_gc
, &c
->idx_gc
, list
)
650 printk(KERN_DEBUG
"\tGC'ed idx LEB %d unmap %d\n",
651 idx_gc
->lnum
, idx_gc
->unmap
);
652 printk(KERN_DEBUG
"\tcommit state %d\n", c
->cmt_state
);
654 /* Print budgeting predictions */
655 available
= ubifs_calc_available(c
, c
->min_idx_lebs
);
656 outstanding
= c
->budg_data_growth
+ c
->budg_dd_growth
;
657 free
= ubifs_get_free_space_nolock(c
);
658 printk(KERN_DEBUG
"Budgeting predictions:\n");
659 printk(KERN_DEBUG
"\tavailable: %lld, outstanding %lld, free %lld\n",
660 available
, outstanding
, free
);
661 spin_unlock(&dbg_lock
);
664 void dbg_dump_lprop(const struct ubifs_info
*c
, const struct ubifs_lprops
*lp
)
666 int i
, spc
, dark
= 0, dead
= 0;
668 struct ubifs_bud
*bud
;
670 spc
= lp
->free
+ lp
->dirty
;
671 if (spc
< c
->dead_wm
)
674 dark
= ubifs_calc_dark(c
, spc
);
676 if (lp
->flags
& LPROPS_INDEX
)
677 printk(KERN_DEBUG
"LEB %-7d free %-8d dirty %-8d used %-8d "
678 "free + dirty %-8d flags %#x (", lp
->lnum
, lp
->free
,
679 lp
->dirty
, c
->leb_size
- spc
, spc
, lp
->flags
);
681 printk(KERN_DEBUG
"LEB %-7d free %-8d dirty %-8d used %-8d "
682 "free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d "
683 "flags %#-4x (", lp
->lnum
, lp
->free
, lp
->dirty
,
684 c
->leb_size
- spc
, spc
, dark
, dead
,
685 (int)(spc
/ UBIFS_MAX_NODE_SZ
), lp
->flags
);
687 if (lp
->flags
& LPROPS_TAKEN
) {
688 if (lp
->flags
& LPROPS_INDEX
)
689 printk(KERN_CONT
"index, taken");
691 printk(KERN_CONT
"taken");
695 if (lp
->flags
& LPROPS_INDEX
) {
696 switch (lp
->flags
& LPROPS_CAT_MASK
) {
697 case LPROPS_DIRTY_IDX
:
700 case LPROPS_FRDI_IDX
:
701 s
= "freeable index";
707 switch (lp
->flags
& LPROPS_CAT_MASK
) {
709 s
= "not categorized";
720 case LPROPS_FREEABLE
:
728 printk(KERN_CONT
"%s", s
);
731 for (rb
= rb_first((struct rb_root
*)&c
->buds
); rb
; rb
= rb_next(rb
)) {
732 bud
= rb_entry(rb
, struct ubifs_bud
, rb
);
733 if (bud
->lnum
== lp
->lnum
) {
735 for (i
= 0; i
< c
->jhead_cnt
; i
++) {
736 if (lp
->lnum
== c
->jheads
[i
].wbuf
.lnum
) {
737 printk(KERN_CONT
", jhead %s",
743 printk(KERN_CONT
", bud of jhead %s",
744 dbg_jhead(bud
->jhead
));
747 if (lp
->lnum
== c
->gc_lnum
)
748 printk(KERN_CONT
", GC LEB");
749 printk(KERN_CONT
")\n");
752 void dbg_dump_lprops(struct ubifs_info
*c
)
755 struct ubifs_lprops lp
;
756 struct ubifs_lp_stats lst
;
758 printk(KERN_DEBUG
"(pid %d) start dumping LEB properties\n",
760 ubifs_get_lp_stats(c
, &lst
);
761 dbg_dump_lstats(&lst
);
763 for (lnum
= c
->main_first
; lnum
< c
->leb_cnt
; lnum
++) {
764 err
= ubifs_read_one_lp(c
, lnum
, &lp
);
766 ubifs_err("cannot read lprops for LEB %d", lnum
);
768 dbg_dump_lprop(c
, &lp
);
770 printk(KERN_DEBUG
"(pid %d) finish dumping LEB properties\n",
774 void dbg_dump_lpt_info(struct ubifs_info
*c
)
778 spin_lock(&dbg_lock
);
779 printk(KERN_DEBUG
"(pid %d) dumping LPT information\n", current
->pid
);
780 printk(KERN_DEBUG
"\tlpt_sz: %lld\n", c
->lpt_sz
);
781 printk(KERN_DEBUG
"\tpnode_sz: %d\n", c
->pnode_sz
);
782 printk(KERN_DEBUG
"\tnnode_sz: %d\n", c
->nnode_sz
);
783 printk(KERN_DEBUG
"\tltab_sz: %d\n", c
->ltab_sz
);
784 printk(KERN_DEBUG
"\tlsave_sz: %d\n", c
->lsave_sz
);
785 printk(KERN_DEBUG
"\tbig_lpt: %d\n", c
->big_lpt
);
786 printk(KERN_DEBUG
"\tlpt_hght: %d\n", c
->lpt_hght
);
787 printk(KERN_DEBUG
"\tpnode_cnt: %d\n", c
->pnode_cnt
);
788 printk(KERN_DEBUG
"\tnnode_cnt: %d\n", c
->nnode_cnt
);
789 printk(KERN_DEBUG
"\tdirty_pn_cnt: %d\n", c
->dirty_pn_cnt
);
790 printk(KERN_DEBUG
"\tdirty_nn_cnt: %d\n", c
->dirty_nn_cnt
);
791 printk(KERN_DEBUG
"\tlsave_cnt: %d\n", c
->lsave_cnt
);
792 printk(KERN_DEBUG
"\tspace_bits: %d\n", c
->space_bits
);
793 printk(KERN_DEBUG
"\tlpt_lnum_bits: %d\n", c
->lpt_lnum_bits
);
794 printk(KERN_DEBUG
"\tlpt_offs_bits: %d\n", c
->lpt_offs_bits
);
795 printk(KERN_DEBUG
"\tlpt_spc_bits: %d\n", c
->lpt_spc_bits
);
796 printk(KERN_DEBUG
"\tpcnt_bits: %d\n", c
->pcnt_bits
);
797 printk(KERN_DEBUG
"\tlnum_bits: %d\n", c
->lnum_bits
);
798 printk(KERN_DEBUG
"\tLPT root is at %d:%d\n", c
->lpt_lnum
, c
->lpt_offs
);
799 printk(KERN_DEBUG
"\tLPT head is at %d:%d\n",
800 c
->nhead_lnum
, c
->nhead_offs
);
801 printk(KERN_DEBUG
"\tLPT ltab is at %d:%d\n",
802 c
->ltab_lnum
, c
->ltab_offs
);
804 printk(KERN_DEBUG
"\tLPT lsave is at %d:%d\n",
805 c
->lsave_lnum
, c
->lsave_offs
);
806 for (i
= 0; i
< c
->lpt_lebs
; i
++)
807 printk(KERN_DEBUG
"\tLPT LEB %d free %d dirty %d tgc %d "
808 "cmt %d\n", i
+ c
->lpt_first
, c
->ltab
[i
].free
,
809 c
->ltab
[i
].dirty
, c
->ltab
[i
].tgc
, c
->ltab
[i
].cmt
);
810 spin_unlock(&dbg_lock
);
813 void dbg_dump_leb(const struct ubifs_info
*c
, int lnum
)
815 struct ubifs_scan_leb
*sleb
;
816 struct ubifs_scan_node
*snod
;
818 if (dbg_failure_mode
)
821 printk(KERN_DEBUG
"(pid %d) start dumping LEB %d\n",
823 sleb
= ubifs_scan(c
, lnum
, 0, c
->dbg
->buf
, 0);
825 ubifs_err("scan error %d", (int)PTR_ERR(sleb
));
829 printk(KERN_DEBUG
"LEB %d has %d nodes ending at %d\n", lnum
,
830 sleb
->nodes_cnt
, sleb
->endpt
);
832 list_for_each_entry(snod
, &sleb
->nodes
, list
) {
834 printk(KERN_DEBUG
"Dumping node at LEB %d:%d len %d\n", lnum
,
835 snod
->offs
, snod
->len
);
836 dbg_dump_node(c
, snod
->node
);
839 printk(KERN_DEBUG
"(pid %d) finish dumping LEB %d\n",
841 ubifs_scan_destroy(sleb
);
845 void dbg_dump_znode(const struct ubifs_info
*c
,
846 const struct ubifs_znode
*znode
)
849 const struct ubifs_zbranch
*zbr
;
851 spin_lock(&dbg_lock
);
853 zbr
= &znode
->parent
->zbranch
[znode
->iip
];
857 printk(KERN_DEBUG
"znode %p, LEB %d:%d len %d parent %p iip %d level %d"
858 " child_cnt %d flags %lx\n", znode
, zbr
->lnum
, zbr
->offs
,
859 zbr
->len
, znode
->parent
, znode
->iip
, znode
->level
,
860 znode
->child_cnt
, znode
->flags
);
862 if (znode
->child_cnt
<= 0 || znode
->child_cnt
> c
->fanout
) {
863 spin_unlock(&dbg_lock
);
867 printk(KERN_DEBUG
"zbranches:\n");
868 for (n
= 0; n
< znode
->child_cnt
; n
++) {
869 zbr
= &znode
->zbranch
[n
];
870 if (znode
->level
> 0)
871 printk(KERN_DEBUG
"\t%d: znode %p LEB %d:%d len %d key "
872 "%s\n", n
, zbr
->znode
, zbr
->lnum
,
876 printk(KERN_DEBUG
"\t%d: LNC %p LEB %d:%d len %d key "
877 "%s\n", n
, zbr
->znode
, zbr
->lnum
,
881 spin_unlock(&dbg_lock
);
884 void dbg_dump_heap(struct ubifs_info
*c
, struct ubifs_lpt_heap
*heap
, int cat
)
888 printk(KERN_DEBUG
"(pid %d) start dumping heap cat %d (%d elements)\n",
889 current
->pid
, cat
, heap
->cnt
);
890 for (i
= 0; i
< heap
->cnt
; i
++) {
891 struct ubifs_lprops
*lprops
= heap
->arr
[i
];
893 printk(KERN_DEBUG
"\t%d. LEB %d hpos %d free %d dirty %d "
894 "flags %d\n", i
, lprops
->lnum
, lprops
->hpos
,
895 lprops
->free
, lprops
->dirty
, lprops
->flags
);
897 printk(KERN_DEBUG
"(pid %d) finish dumping heap\n", current
->pid
);
900 void dbg_dump_pnode(struct ubifs_info
*c
, struct ubifs_pnode
*pnode
,
901 struct ubifs_nnode
*parent
, int iip
)
905 printk(KERN_DEBUG
"(pid %d) dumping pnode:\n", current
->pid
);
906 printk(KERN_DEBUG
"\taddress %zx parent %zx cnext %zx\n",
907 (size_t)pnode
, (size_t)parent
, (size_t)pnode
->cnext
);
908 printk(KERN_DEBUG
"\tflags %lu iip %d level %d num %d\n",
909 pnode
->flags
, iip
, pnode
->level
, pnode
->num
);
910 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
911 struct ubifs_lprops
*lp
= &pnode
->lprops
[i
];
913 printk(KERN_DEBUG
"\t%d: free %d dirty %d flags %d lnum %d\n",
914 i
, lp
->free
, lp
->dirty
, lp
->flags
, lp
->lnum
);
918 void dbg_dump_tnc(struct ubifs_info
*c
)
920 struct ubifs_znode
*znode
;
923 printk(KERN_DEBUG
"\n");
924 printk(KERN_DEBUG
"(pid %d) start dumping TNC tree\n", current
->pid
);
925 znode
= ubifs_tnc_levelorder_next(c
->zroot
.znode
, NULL
);
926 level
= znode
->level
;
927 printk(KERN_DEBUG
"== Level %d ==\n", level
);
929 if (level
!= znode
->level
) {
930 level
= znode
->level
;
931 printk(KERN_DEBUG
"== Level %d ==\n", level
);
933 dbg_dump_znode(c
, znode
);
934 znode
= ubifs_tnc_levelorder_next(c
->zroot
.znode
, znode
);
936 printk(KERN_DEBUG
"(pid %d) finish dumping TNC tree\n", current
->pid
);
939 static int dump_znode(struct ubifs_info
*c
, struct ubifs_znode
*znode
,
942 dbg_dump_znode(c
, znode
);
947 * dbg_dump_index - dump the on-flash index.
948 * @c: UBIFS file-system description object
950 * This function dumps whole UBIFS indexing B-tree, unlike 'dbg_dump_tnc()'
951 * which dumps only in-memory znodes and does not read znodes which from flash.
953 void dbg_dump_index(struct ubifs_info
*c
)
955 dbg_walk_index(c
, NULL
, dump_znode
, NULL
);
959 * dbg_save_space_info - save information about flash space.
960 * @c: UBIFS file-system description object
962 * This function saves information about UBIFS free space, dirty space, etc, in
963 * order to check it later.
965 void dbg_save_space_info(struct ubifs_info
*c
)
967 struct ubifs_debug_info
*d
= c
->dbg
;
969 ubifs_get_lp_stats(c
, &d
->saved_lst
);
971 spin_lock(&c
->space_lock
);
972 d
->saved_free
= ubifs_get_free_space_nolock(c
);
973 spin_unlock(&c
->space_lock
);
977 * dbg_check_space_info - check flash space information.
978 * @c: UBIFS file-system description object
980 * This function compares current flash space information with the information
981 * which was saved when the 'dbg_save_space_info()' function was called.
982 * Returns zero if the information has not changed, and %-EINVAL it it has
985 int dbg_check_space_info(struct ubifs_info
*c
)
987 struct ubifs_debug_info
*d
= c
->dbg
;
988 struct ubifs_lp_stats lst
;
989 long long avail
, free
;
991 spin_lock(&c
->space_lock
);
992 avail
= ubifs_calc_available(c
, c
->min_idx_lebs
);
993 spin_unlock(&c
->space_lock
);
994 free
= ubifs_get_free_space(c
);
996 if (free
!= d
->saved_free
) {
997 ubifs_err("free space changed from %lld to %lld",
998 d
->saved_free
, free
);
1005 ubifs_msg("saved lprops statistics dump");
1006 dbg_dump_lstats(&d
->saved_lst
);
1007 ubifs_get_lp_stats(c
, &lst
);
1009 ubifs_msg("current lprops statistics dump");
1010 dbg_dump_lstats(&lst
);
1012 spin_lock(&c
->space_lock
);
1014 spin_unlock(&c
->space_lock
);
1020 * dbg_check_synced_i_size - check synchronized inode size.
1021 * @inode: inode to check
1023 * If inode is clean, synchronized inode size has to be equivalent to current
1024 * inode size. This function has to be called only for locked inodes (@i_mutex
1025 * has to be locked). Returns %0 if synchronized inode size if correct, and
1028 int dbg_check_synced_i_size(struct inode
*inode
)
1031 struct ubifs_inode
*ui
= ubifs_inode(inode
);
1033 if (!(ubifs_chk_flags
& UBIFS_CHK_GEN
))
1035 if (!S_ISREG(inode
->i_mode
))
1038 mutex_lock(&ui
->ui_mutex
);
1039 spin_lock(&ui
->ui_lock
);
1040 if (ui
->ui_size
!= ui
->synced_i_size
&& !ui
->dirty
) {
1041 ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode "
1042 "is clean", ui
->ui_size
, ui
->synced_i_size
);
1043 ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode
->i_ino
,
1044 inode
->i_mode
, i_size_read(inode
));
1048 spin_unlock(&ui
->ui_lock
);
1049 mutex_unlock(&ui
->ui_mutex
);
1054 * dbg_check_dir - check directory inode size and link count.
1055 * @c: UBIFS file-system description object
1056 * @dir: the directory to calculate size for
1057 * @size: the result is returned here
1059 * This function makes sure that directory size and link count are correct.
1060 * Returns zero in case of success and a negative error code in case of
1063 * Note, it is good idea to make sure the @dir->i_mutex is locked before
1064 * calling this function.
1066 int dbg_check_dir_size(struct ubifs_info
*c
, const struct inode
*dir
)
1068 unsigned int nlink
= 2;
1069 union ubifs_key key
;
1070 struct ubifs_dent_node
*dent
, *pdent
= NULL
;
1071 struct qstr nm
= { .name
= NULL
};
1072 loff_t size
= UBIFS_INO_NODE_SZ
;
1074 if (!(ubifs_chk_flags
& UBIFS_CHK_GEN
))
1077 if (!S_ISDIR(dir
->i_mode
))
1080 lowest_dent_key(c
, &key
, dir
->i_ino
);
1084 dent
= ubifs_tnc_next_ent(c
, &key
, &nm
);
1086 err
= PTR_ERR(dent
);
1092 nm
.name
= dent
->name
;
1093 nm
.len
= le16_to_cpu(dent
->nlen
);
1094 size
+= CALC_DENT_SIZE(nm
.len
);
1095 if (dent
->type
== UBIFS_ITYPE_DIR
)
1099 key_read(c
, &dent
->key
, &key
);
1103 if (i_size_read(dir
) != size
) {
1104 ubifs_err("directory inode %lu has size %llu, "
1105 "but calculated size is %llu", dir
->i_ino
,
1106 (unsigned long long)i_size_read(dir
),
1107 (unsigned long long)size
);
1111 if (dir
->i_nlink
!= nlink
) {
1112 ubifs_err("directory inode %lu has nlink %u, but calculated "
1113 "nlink is %u", dir
->i_ino
, dir
->i_nlink
, nlink
);
1122 * dbg_check_key_order - make sure that colliding keys are properly ordered.
1123 * @c: UBIFS file-system description object
1124 * @zbr1: first zbranch
1125 * @zbr2: following zbranch
1127 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
1128 * names of the direntries/xentries which are referred by the keys. This
1129 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
1130 * sure the name of direntry/xentry referred by @zbr1 is less than
1131 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
1132 * and a negative error code in case of failure.
1134 static int dbg_check_key_order(struct ubifs_info
*c
, struct ubifs_zbranch
*zbr1
,
1135 struct ubifs_zbranch
*zbr2
)
1137 int err
, nlen1
, nlen2
, cmp
;
1138 struct ubifs_dent_node
*dent1
, *dent2
;
1139 union ubifs_key key
;
1141 ubifs_assert(!keys_cmp(c
, &zbr1
->key
, &zbr2
->key
));
1142 dent1
= kmalloc(UBIFS_MAX_DENT_NODE_SZ
, GFP_NOFS
);
1145 dent2
= kmalloc(UBIFS_MAX_DENT_NODE_SZ
, GFP_NOFS
);
1151 err
= ubifs_tnc_read_node(c
, zbr1
, dent1
);
1154 err
= ubifs_validate_entry(c
, dent1
);
1158 err
= ubifs_tnc_read_node(c
, zbr2
, dent2
);
1161 err
= ubifs_validate_entry(c
, dent2
);
1165 /* Make sure node keys are the same as in zbranch */
1167 key_read(c
, &dent1
->key
, &key
);
1168 if (keys_cmp(c
, &zbr1
->key
, &key
)) {
1169 dbg_err("1st entry at %d:%d has key %s", zbr1
->lnum
,
1170 zbr1
->offs
, DBGKEY(&key
));
1171 dbg_err("but it should have key %s according to tnc",
1172 DBGKEY(&zbr1
->key
));
1173 dbg_dump_node(c
, dent1
);
1177 key_read(c
, &dent2
->key
, &key
);
1178 if (keys_cmp(c
, &zbr2
->key
, &key
)) {
1179 dbg_err("2nd entry at %d:%d has key %s", zbr1
->lnum
,
1180 zbr1
->offs
, DBGKEY(&key
));
1181 dbg_err("but it should have key %s according to tnc",
1182 DBGKEY(&zbr2
->key
));
1183 dbg_dump_node(c
, dent2
);
1187 nlen1
= le16_to_cpu(dent1
->nlen
);
1188 nlen2
= le16_to_cpu(dent2
->nlen
);
1190 cmp
= memcmp(dent1
->name
, dent2
->name
, min_t(int, nlen1
, nlen2
));
1191 if (cmp
< 0 || (cmp
== 0 && nlen1
< nlen2
)) {
1195 if (cmp
== 0 && nlen1
== nlen2
)
1196 dbg_err("2 xent/dent nodes with the same name");
1198 dbg_err("bad order of colliding key %s",
1201 ubifs_msg("first node at %d:%d\n", zbr1
->lnum
, zbr1
->offs
);
1202 dbg_dump_node(c
, dent1
);
1203 ubifs_msg("second node at %d:%d\n", zbr2
->lnum
, zbr2
->offs
);
1204 dbg_dump_node(c
, dent2
);
1213 * dbg_check_znode - check if znode is all right.
1214 * @c: UBIFS file-system description object
1215 * @zbr: zbranch which points to this znode
1217 * This function makes sure that znode referred to by @zbr is all right.
1218 * Returns zero if it is, and %-EINVAL if it is not.
1220 static int dbg_check_znode(struct ubifs_info
*c
, struct ubifs_zbranch
*zbr
)
1222 struct ubifs_znode
*znode
= zbr
->znode
;
1223 struct ubifs_znode
*zp
= znode
->parent
;
1226 if (znode
->child_cnt
<= 0 || znode
->child_cnt
> c
->fanout
) {
1230 if (znode
->level
< 0) {
1234 if (znode
->iip
< 0 || znode
->iip
>= c
->fanout
) {
1240 /* Only dirty zbranch may have no on-flash nodes */
1241 if (!ubifs_zn_dirty(znode
)) {
1246 if (ubifs_zn_dirty(znode
)) {
1248 * If znode is dirty, its parent has to be dirty as well. The
1249 * order of the operation is important, so we have to have
1253 if (zp
&& !ubifs_zn_dirty(zp
)) {
1255 * The dirty flag is atomic and is cleared outside the
1256 * TNC mutex, so znode's dirty flag may now have
1257 * been cleared. The child is always cleared before the
1258 * parent, so we just need to check again.
1261 if (ubifs_zn_dirty(znode
)) {
1269 const union ubifs_key
*min
, *max
;
1271 if (znode
->level
!= zp
->level
- 1) {
1276 /* Make sure the 'parent' pointer in our znode is correct */
1277 err
= ubifs_search_zbranch(c
, zp
, &zbr
->key
, &n
);
1279 /* This zbranch does not exist in the parent */
1284 if (znode
->iip
>= zp
->child_cnt
) {
1289 if (znode
->iip
!= n
) {
1290 /* This may happen only in case of collisions */
1291 if (keys_cmp(c
, &zp
->zbranch
[n
].key
,
1292 &zp
->zbranch
[znode
->iip
].key
)) {
1300 * Make sure that the first key in our znode is greater than or
1301 * equal to the key in the pointing zbranch.
1304 cmp
= keys_cmp(c
, min
, &znode
->zbranch
[0].key
);
1310 if (n
+ 1 < zp
->child_cnt
) {
1311 max
= &zp
->zbranch
[n
+ 1].key
;
1314 * Make sure the last key in our znode is less or
1315 * equivalent than the key in the zbranch which goes
1316 * after our pointing zbranch.
1318 cmp
= keys_cmp(c
, max
,
1319 &znode
->zbranch
[znode
->child_cnt
- 1].key
);
1326 /* This may only be root znode */
1327 if (zbr
!= &c
->zroot
) {
1334 * Make sure that next key is greater or equivalent then the previous
1337 for (n
= 1; n
< znode
->child_cnt
; n
++) {
1338 cmp
= keys_cmp(c
, &znode
->zbranch
[n
- 1].key
,
1339 &znode
->zbranch
[n
].key
);
1345 /* This can only be keys with colliding hash */
1346 if (!is_hash_key(c
, &znode
->zbranch
[n
].key
)) {
1351 if (znode
->level
!= 0 || c
->replaying
)
1355 * Colliding keys should follow binary order of
1356 * corresponding xentry/dentry names.
1358 err
= dbg_check_key_order(c
, &znode
->zbranch
[n
- 1],
1359 &znode
->zbranch
[n
]);
1369 for (n
= 0; n
< znode
->child_cnt
; n
++) {
1370 if (!znode
->zbranch
[n
].znode
&&
1371 (znode
->zbranch
[n
].lnum
== 0 ||
1372 znode
->zbranch
[n
].len
== 0)) {
1377 if (znode
->zbranch
[n
].lnum
!= 0 &&
1378 znode
->zbranch
[n
].len
== 0) {
1383 if (znode
->zbranch
[n
].lnum
== 0 &&
1384 znode
->zbranch
[n
].len
!= 0) {
1389 if (znode
->zbranch
[n
].lnum
== 0 &&
1390 znode
->zbranch
[n
].offs
!= 0) {
1395 if (znode
->level
!= 0 && znode
->zbranch
[n
].znode
)
1396 if (znode
->zbranch
[n
].znode
->parent
!= znode
) {
1405 ubifs_err("failed, error %d", err
);
1406 ubifs_msg("dump of the znode");
1407 dbg_dump_znode(c
, znode
);
1409 ubifs_msg("dump of the parent znode");
1410 dbg_dump_znode(c
, zp
);
1417 * dbg_check_tnc - check TNC tree.
1418 * @c: UBIFS file-system description object
1419 * @extra: do extra checks that are possible at start commit
1421 * This function traverses whole TNC tree and checks every znode. Returns zero
1422 * if everything is all right and %-EINVAL if something is wrong with TNC.
1424 int dbg_check_tnc(struct ubifs_info
*c
, int extra
)
1426 struct ubifs_znode
*znode
;
1427 long clean_cnt
= 0, dirty_cnt
= 0;
1430 if (!(ubifs_chk_flags
& UBIFS_CHK_TNC
))
1433 ubifs_assert(mutex_is_locked(&c
->tnc_mutex
));
1434 if (!c
->zroot
.znode
)
1437 znode
= ubifs_tnc_postorder_first(c
->zroot
.znode
);
1439 struct ubifs_znode
*prev
;
1440 struct ubifs_zbranch
*zbr
;
1445 zbr
= &znode
->parent
->zbranch
[znode
->iip
];
1447 err
= dbg_check_znode(c
, zbr
);
1452 if (ubifs_zn_dirty(znode
))
1459 znode
= ubifs_tnc_postorder_next(znode
);
1464 * If the last key of this znode is equivalent to the first key
1465 * of the next znode (collision), then check order of the keys.
1467 last
= prev
->child_cnt
- 1;
1468 if (prev
->level
== 0 && znode
->level
== 0 && !c
->replaying
&&
1469 !keys_cmp(c
, &prev
->zbranch
[last
].key
,
1470 &znode
->zbranch
[0].key
)) {
1471 err
= dbg_check_key_order(c
, &prev
->zbranch
[last
],
1472 &znode
->zbranch
[0]);
1476 ubifs_msg("first znode");
1477 dbg_dump_znode(c
, prev
);
1478 ubifs_msg("second znode");
1479 dbg_dump_znode(c
, znode
);
1486 if (clean_cnt
!= atomic_long_read(&c
->clean_zn_cnt
)) {
1487 ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld",
1488 atomic_long_read(&c
->clean_zn_cnt
),
1492 if (dirty_cnt
!= atomic_long_read(&c
->dirty_zn_cnt
)) {
1493 ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld",
1494 atomic_long_read(&c
->dirty_zn_cnt
),
1504 * dbg_walk_index - walk the on-flash index.
1505 * @c: UBIFS file-system description object
1506 * @leaf_cb: called for each leaf node
1507 * @znode_cb: called for each indexing node
1508 * @priv: private data which is passed to callbacks
1510 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1511 * node and @znode_cb for each indexing node. Returns zero in case of success
1512 * and a negative error code in case of failure.
1514 * It would be better if this function removed every znode it pulled to into
1515 * the TNC, so that the behavior more closely matched the non-debugging
1518 int dbg_walk_index(struct ubifs_info
*c
, dbg_leaf_callback leaf_cb
,
1519 dbg_znode_callback znode_cb
, void *priv
)
1522 struct ubifs_zbranch
*zbr
;
1523 struct ubifs_znode
*znode
, *child
;
1525 mutex_lock(&c
->tnc_mutex
);
1526 /* If the root indexing node is not in TNC - pull it */
1527 if (!c
->zroot
.znode
) {
1528 c
->zroot
.znode
= ubifs_load_znode(c
, &c
->zroot
, NULL
, 0);
1529 if (IS_ERR(c
->zroot
.znode
)) {
1530 err
= PTR_ERR(c
->zroot
.znode
);
1531 c
->zroot
.znode
= NULL
;
1537 * We are going to traverse the indexing tree in the postorder manner.
1538 * Go down and find the leftmost indexing node where we are going to
1541 znode
= c
->zroot
.znode
;
1542 while (znode
->level
> 0) {
1543 zbr
= &znode
->zbranch
[0];
1546 child
= ubifs_load_znode(c
, zbr
, znode
, 0);
1547 if (IS_ERR(child
)) {
1548 err
= PTR_ERR(child
);
1557 /* Iterate over all indexing nodes */
1564 err
= znode_cb(c
, znode
, priv
);
1566 ubifs_err("znode checking function returned "
1568 dbg_dump_znode(c
, znode
);
1572 if (leaf_cb
&& znode
->level
== 0) {
1573 for (idx
= 0; idx
< znode
->child_cnt
; idx
++) {
1574 zbr
= &znode
->zbranch
[idx
];
1575 err
= leaf_cb(c
, zbr
, priv
);
1577 ubifs_err("leaf checking function "
1578 "returned error %d, for leaf "
1580 err
, zbr
->lnum
, zbr
->offs
);
1589 idx
= znode
->iip
+ 1;
1590 znode
= znode
->parent
;
1591 if (idx
< znode
->child_cnt
) {
1592 /* Switch to the next index in the parent */
1593 zbr
= &znode
->zbranch
[idx
];
1596 child
= ubifs_load_znode(c
, zbr
, znode
, idx
);
1597 if (IS_ERR(child
)) {
1598 err
= PTR_ERR(child
);
1606 * This is the last child, switch to the parent and
1611 /* Go to the lowest leftmost znode in the new sub-tree */
1612 while (znode
->level
> 0) {
1613 zbr
= &znode
->zbranch
[0];
1616 child
= ubifs_load_znode(c
, zbr
, znode
, 0);
1617 if (IS_ERR(child
)) {
1618 err
= PTR_ERR(child
);
1627 mutex_unlock(&c
->tnc_mutex
);
1632 zbr
= &znode
->parent
->zbranch
[znode
->iip
];
1635 ubifs_msg("dump of znode at LEB %d:%d", zbr
->lnum
, zbr
->offs
);
1636 dbg_dump_znode(c
, znode
);
1638 mutex_unlock(&c
->tnc_mutex
);
1643 * add_size - add znode size to partially calculated index size.
1644 * @c: UBIFS file-system description object
1645 * @znode: znode to add size for
1646 * @priv: partially calculated index size
1648 * This is a helper function for 'dbg_check_idx_size()' which is called for
1649 * every indexing node and adds its size to the 'long long' variable pointed to
1652 static int add_size(struct ubifs_info
*c
, struct ubifs_znode
*znode
, void *priv
)
1654 long long *idx_size
= priv
;
1657 add
= ubifs_idx_node_sz(c
, znode
->child_cnt
);
1658 add
= ALIGN(add
, 8);
1664 * dbg_check_idx_size - check index size.
1665 * @c: UBIFS file-system description object
1666 * @idx_size: size to check
1668 * This function walks the UBIFS index, calculates its size and checks that the
1669 * size is equivalent to @idx_size. Returns zero in case of success and a
1670 * negative error code in case of failure.
1672 int dbg_check_idx_size(struct ubifs_info
*c
, long long idx_size
)
1677 if (!(ubifs_chk_flags
& UBIFS_CHK_IDX_SZ
))
1680 err
= dbg_walk_index(c
, NULL
, add_size
, &calc
);
1682 ubifs_err("error %d while walking the index", err
);
1686 if (calc
!= idx_size
) {
1687 ubifs_err("index size check failed: calculated size is %lld, "
1688 "should be %lld", calc
, idx_size
);
1697 * struct fsck_inode - information about an inode used when checking the file-system.
1698 * @rb: link in the RB-tree of inodes
1699 * @inum: inode number
1700 * @mode: inode type, permissions, etc
1701 * @nlink: inode link count
1702 * @xattr_cnt: count of extended attributes
1703 * @references: how many directory/xattr entries refer this inode (calculated
1704 * while walking the index)
1705 * @calc_cnt: for directory inode count of child directories
1706 * @size: inode size (read from on-flash inode)
1707 * @xattr_sz: summary size of all extended attributes (read from on-flash
1709 * @calc_sz: for directories calculated directory size
1710 * @calc_xcnt: count of extended attributes
1711 * @calc_xsz: calculated summary size of all extended attributes
1712 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1713 * inode (read from on-flash inode)
1714 * @calc_xnms: calculated sum of lengths of all extended attribute names
1721 unsigned int xattr_cnt
;
1725 unsigned int xattr_sz
;
1727 long long calc_xcnt
;
1729 unsigned int xattr_nms
;
1730 long long calc_xnms
;
1734 * struct fsck_data - private FS checking information.
1735 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1738 struct rb_root inodes
;
1742 * add_inode - add inode information to RB-tree of inodes.
1743 * @c: UBIFS file-system description object
1744 * @fsckd: FS checking information
1745 * @ino: raw UBIFS inode to add
1747 * This is a helper function for 'check_leaf()' which adds information about
1748 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1749 * case of success and a negative error code in case of failure.
1751 static struct fsck_inode
*add_inode(struct ubifs_info
*c
,
1752 struct fsck_data
*fsckd
,
1753 struct ubifs_ino_node
*ino
)
1755 struct rb_node
**p
, *parent
= NULL
;
1756 struct fsck_inode
*fscki
;
1757 ino_t inum
= key_inum_flash(c
, &ino
->key
);
1759 p
= &fsckd
->inodes
.rb_node
;
1762 fscki
= rb_entry(parent
, struct fsck_inode
, rb
);
1763 if (inum
< fscki
->inum
)
1765 else if (inum
> fscki
->inum
)
1766 p
= &(*p
)->rb_right
;
1771 if (inum
> c
->highest_inum
) {
1772 ubifs_err("too high inode number, max. is %lu",
1773 (unsigned long)c
->highest_inum
);
1774 return ERR_PTR(-EINVAL
);
1777 fscki
= kzalloc(sizeof(struct fsck_inode
), GFP_NOFS
);
1779 return ERR_PTR(-ENOMEM
);
1782 fscki
->nlink
= le32_to_cpu(ino
->nlink
);
1783 fscki
->size
= le64_to_cpu(ino
->size
);
1784 fscki
->xattr_cnt
= le32_to_cpu(ino
->xattr_cnt
);
1785 fscki
->xattr_sz
= le32_to_cpu(ino
->xattr_size
);
1786 fscki
->xattr_nms
= le32_to_cpu(ino
->xattr_names
);
1787 fscki
->mode
= le32_to_cpu(ino
->mode
);
1788 if (S_ISDIR(fscki
->mode
)) {
1789 fscki
->calc_sz
= UBIFS_INO_NODE_SZ
;
1790 fscki
->calc_cnt
= 2;
1792 rb_link_node(&fscki
->rb
, parent
, p
);
1793 rb_insert_color(&fscki
->rb
, &fsckd
->inodes
);
1798 * search_inode - search inode in the RB-tree of inodes.
1799 * @fsckd: FS checking information
1800 * @inum: inode number to search
1802 * This is a helper function for 'check_leaf()' which searches inode @inum in
1803 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1804 * the inode was not found.
1806 static struct fsck_inode
*search_inode(struct fsck_data
*fsckd
, ino_t inum
)
1809 struct fsck_inode
*fscki
;
1811 p
= fsckd
->inodes
.rb_node
;
1813 fscki
= rb_entry(p
, struct fsck_inode
, rb
);
1814 if (inum
< fscki
->inum
)
1816 else if (inum
> fscki
->inum
)
1825 * read_add_inode - read inode node and add it to RB-tree of inodes.
1826 * @c: UBIFS file-system description object
1827 * @fsckd: FS checking information
1828 * @inum: inode number to read
1830 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1831 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1832 * information pointer in case of success and a negative error code in case of
1835 static struct fsck_inode
*read_add_inode(struct ubifs_info
*c
,
1836 struct fsck_data
*fsckd
, ino_t inum
)
1839 union ubifs_key key
;
1840 struct ubifs_znode
*znode
;
1841 struct ubifs_zbranch
*zbr
;
1842 struct ubifs_ino_node
*ino
;
1843 struct fsck_inode
*fscki
;
1845 fscki
= search_inode(fsckd
, inum
);
1849 ino_key_init(c
, &key
, inum
);
1850 err
= ubifs_lookup_level0(c
, &key
, &znode
, &n
);
1852 ubifs_err("inode %lu not found in index", (unsigned long)inum
);
1853 return ERR_PTR(-ENOENT
);
1854 } else if (err
< 0) {
1855 ubifs_err("error %d while looking up inode %lu",
1856 err
, (unsigned long)inum
);
1857 return ERR_PTR(err
);
1860 zbr
= &znode
->zbranch
[n
];
1861 if (zbr
->len
< UBIFS_INO_NODE_SZ
) {
1862 ubifs_err("bad node %lu node length %d",
1863 (unsigned long)inum
, zbr
->len
);
1864 return ERR_PTR(-EINVAL
);
1867 ino
= kmalloc(zbr
->len
, GFP_NOFS
);
1869 return ERR_PTR(-ENOMEM
);
1871 err
= ubifs_tnc_read_node(c
, zbr
, ino
);
1873 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
1874 zbr
->lnum
, zbr
->offs
, err
);
1876 return ERR_PTR(err
);
1879 fscki
= add_inode(c
, fsckd
, ino
);
1881 if (IS_ERR(fscki
)) {
1882 ubifs_err("error %ld while adding inode %lu node",
1883 PTR_ERR(fscki
), (unsigned long)inum
);
1891 * check_leaf - check leaf node.
1892 * @c: UBIFS file-system description object
1893 * @zbr: zbranch of the leaf node to check
1894 * @priv: FS checking information
1896 * This is a helper function for 'dbg_check_filesystem()' which is called for
1897 * every single leaf node while walking the indexing tree. It checks that the
1898 * leaf node referred from the indexing tree exists, has correct CRC, and does
1899 * some other basic validation. This function is also responsible for building
1900 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
1901 * calculates reference count, size, etc for each inode in order to later
1902 * compare them to the information stored inside the inodes and detect possible
1903 * inconsistencies. Returns zero in case of success and a negative error code
1904 * in case of failure.
1906 static int check_leaf(struct ubifs_info
*c
, struct ubifs_zbranch
*zbr
,
1911 struct ubifs_ch
*ch
;
1912 int err
, type
= key_type(c
, &zbr
->key
);
1913 struct fsck_inode
*fscki
;
1915 if (zbr
->len
< UBIFS_CH_SZ
) {
1916 ubifs_err("bad leaf length %d (LEB %d:%d)",
1917 zbr
->len
, zbr
->lnum
, zbr
->offs
);
1921 node
= kmalloc(zbr
->len
, GFP_NOFS
);
1925 err
= ubifs_tnc_read_node(c
, zbr
, node
);
1927 ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
1928 zbr
->lnum
, zbr
->offs
, err
);
1932 /* If this is an inode node, add it to RB-tree of inodes */
1933 if (type
== UBIFS_INO_KEY
) {
1934 fscki
= add_inode(c
, priv
, node
);
1935 if (IS_ERR(fscki
)) {
1936 err
= PTR_ERR(fscki
);
1937 ubifs_err("error %d while adding inode node", err
);
1943 if (type
!= UBIFS_DENT_KEY
&& type
!= UBIFS_XENT_KEY
&&
1944 type
!= UBIFS_DATA_KEY
) {
1945 ubifs_err("unexpected node type %d at LEB %d:%d",
1946 type
, zbr
->lnum
, zbr
->offs
);
1952 if (le64_to_cpu(ch
->sqnum
) > c
->max_sqnum
) {
1953 ubifs_err("too high sequence number, max. is %llu",
1959 if (type
== UBIFS_DATA_KEY
) {
1961 struct ubifs_data_node
*dn
= node
;
1964 * Search the inode node this data node belongs to and insert
1965 * it to the RB-tree of inodes.
1967 inum
= key_inum_flash(c
, &dn
->key
);
1968 fscki
= read_add_inode(c
, priv
, inum
);
1969 if (IS_ERR(fscki
)) {
1970 err
= PTR_ERR(fscki
);
1971 ubifs_err("error %d while processing data node and "
1972 "trying to find inode node %lu",
1973 err
, (unsigned long)inum
);
1977 /* Make sure the data node is within inode size */
1978 blk_offs
= key_block_flash(c
, &dn
->key
);
1979 blk_offs
<<= UBIFS_BLOCK_SHIFT
;
1980 blk_offs
+= le32_to_cpu(dn
->size
);
1981 if (blk_offs
> fscki
->size
) {
1982 ubifs_err("data node at LEB %d:%d is not within inode "
1983 "size %lld", zbr
->lnum
, zbr
->offs
,
1990 struct ubifs_dent_node
*dent
= node
;
1991 struct fsck_inode
*fscki1
;
1993 err
= ubifs_validate_entry(c
, dent
);
1998 * Search the inode node this entry refers to and the parent
1999 * inode node and insert them to the RB-tree of inodes.
2001 inum
= le64_to_cpu(dent
->inum
);
2002 fscki
= read_add_inode(c
, priv
, inum
);
2003 if (IS_ERR(fscki
)) {
2004 err
= PTR_ERR(fscki
);
2005 ubifs_err("error %d while processing entry node and "
2006 "trying to find inode node %lu",
2007 err
, (unsigned long)inum
);
2011 /* Count how many direntries or xentries refers this inode */
2012 fscki
->references
+= 1;
2014 inum
= key_inum_flash(c
, &dent
->key
);
2015 fscki1
= read_add_inode(c
, priv
, inum
);
2016 if (IS_ERR(fscki1
)) {
2017 err
= PTR_ERR(fscki
);
2018 ubifs_err("error %d while processing entry node and "
2019 "trying to find parent inode node %lu",
2020 err
, (unsigned long)inum
);
2024 nlen
= le16_to_cpu(dent
->nlen
);
2025 if (type
== UBIFS_XENT_KEY
) {
2026 fscki1
->calc_xcnt
+= 1;
2027 fscki1
->calc_xsz
+= CALC_DENT_SIZE(nlen
);
2028 fscki1
->calc_xsz
+= CALC_XATTR_BYTES(fscki
->size
);
2029 fscki1
->calc_xnms
+= nlen
;
2031 fscki1
->calc_sz
+= CALC_DENT_SIZE(nlen
);
2032 if (dent
->type
== UBIFS_ITYPE_DIR
)
2033 fscki1
->calc_cnt
+= 1;
2042 ubifs_msg("dump of node at LEB %d:%d", zbr
->lnum
, zbr
->offs
);
2043 dbg_dump_node(c
, node
);
2050 * free_inodes - free RB-tree of inodes.
2051 * @fsckd: FS checking information
2053 static void free_inodes(struct fsck_data
*fsckd
)
2055 struct rb_node
*this = fsckd
->inodes
.rb_node
;
2056 struct fsck_inode
*fscki
;
2060 this = this->rb_left
;
2061 else if (this->rb_right
)
2062 this = this->rb_right
;
2064 fscki
= rb_entry(this, struct fsck_inode
, rb
);
2065 this = rb_parent(this);
2067 if (this->rb_left
== &fscki
->rb
)
2068 this->rb_left
= NULL
;
2070 this->rb_right
= NULL
;
2078 * check_inodes - checks all inodes.
2079 * @c: UBIFS file-system description object
2080 * @fsckd: FS checking information
2082 * This is a helper function for 'dbg_check_filesystem()' which walks the
2083 * RB-tree of inodes after the index scan has been finished, and checks that
2084 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
2085 * %-EINVAL if not, and a negative error code in case of failure.
2087 static int check_inodes(struct ubifs_info
*c
, struct fsck_data
*fsckd
)
2090 union ubifs_key key
;
2091 struct ubifs_znode
*znode
;
2092 struct ubifs_zbranch
*zbr
;
2093 struct ubifs_ino_node
*ino
;
2094 struct fsck_inode
*fscki
;
2095 struct rb_node
*this = rb_first(&fsckd
->inodes
);
2098 fscki
= rb_entry(this, struct fsck_inode
, rb
);
2099 this = rb_next(this);
2101 if (S_ISDIR(fscki
->mode
)) {
2103 * Directories have to have exactly one reference (they
2104 * cannot have hardlinks), although root inode is an
2107 if (fscki
->inum
!= UBIFS_ROOT_INO
&&
2108 fscki
->references
!= 1) {
2109 ubifs_err("directory inode %lu has %d "
2110 "direntries which refer it, but "
2112 (unsigned long)fscki
->inum
,
2116 if (fscki
->inum
== UBIFS_ROOT_INO
&&
2117 fscki
->references
!= 0) {
2118 ubifs_err("root inode %lu has non-zero (%d) "
2119 "direntries which refer it",
2120 (unsigned long)fscki
->inum
,
2124 if (fscki
->calc_sz
!= fscki
->size
) {
2125 ubifs_err("directory inode %lu size is %lld, "
2126 "but calculated size is %lld",
2127 (unsigned long)fscki
->inum
,
2128 fscki
->size
, fscki
->calc_sz
);
2131 if (fscki
->calc_cnt
!= fscki
->nlink
) {
2132 ubifs_err("directory inode %lu nlink is %d, "
2133 "but calculated nlink is %d",
2134 (unsigned long)fscki
->inum
,
2135 fscki
->nlink
, fscki
->calc_cnt
);
2139 if (fscki
->references
!= fscki
->nlink
) {
2140 ubifs_err("inode %lu nlink is %d, but "
2141 "calculated nlink is %d",
2142 (unsigned long)fscki
->inum
,
2143 fscki
->nlink
, fscki
->references
);
2147 if (fscki
->xattr_sz
!= fscki
->calc_xsz
) {
2148 ubifs_err("inode %lu has xattr size %u, but "
2149 "calculated size is %lld",
2150 (unsigned long)fscki
->inum
, fscki
->xattr_sz
,
2154 if (fscki
->xattr_cnt
!= fscki
->calc_xcnt
) {
2155 ubifs_err("inode %lu has %u xattrs, but "
2156 "calculated count is %lld",
2157 (unsigned long)fscki
->inum
,
2158 fscki
->xattr_cnt
, fscki
->calc_xcnt
);
2161 if (fscki
->xattr_nms
!= fscki
->calc_xnms
) {
2162 ubifs_err("inode %lu has xattr names' size %u, but "
2163 "calculated names' size is %lld",
2164 (unsigned long)fscki
->inum
, fscki
->xattr_nms
,
2173 /* Read the bad inode and dump it */
2174 ino_key_init(c
, &key
, fscki
->inum
);
2175 err
= ubifs_lookup_level0(c
, &key
, &znode
, &n
);
2177 ubifs_err("inode %lu not found in index",
2178 (unsigned long)fscki
->inum
);
2180 } else if (err
< 0) {
2181 ubifs_err("error %d while looking up inode %lu",
2182 err
, (unsigned long)fscki
->inum
);
2186 zbr
= &znode
->zbranch
[n
];
2187 ino
= kmalloc(zbr
->len
, GFP_NOFS
);
2191 err
= ubifs_tnc_read_node(c
, zbr
, ino
);
2193 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2194 zbr
->lnum
, zbr
->offs
, err
);
2199 ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",
2200 (unsigned long)fscki
->inum
, zbr
->lnum
, zbr
->offs
);
2201 dbg_dump_node(c
, ino
);
2207 * dbg_check_filesystem - check the file-system.
2208 * @c: UBIFS file-system description object
2210 * This function checks the file system, namely:
2211 * o makes sure that all leaf nodes exist and their CRCs are correct;
2212 * o makes sure inode nlink, size, xattr size/count are correct (for all
2215 * The function reads whole indexing tree and all nodes, so it is pretty
2216 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2217 * not, and a negative error code in case of failure.
2219 int dbg_check_filesystem(struct ubifs_info
*c
)
2222 struct fsck_data fsckd
;
2224 if (!(ubifs_chk_flags
& UBIFS_CHK_FS
))
2227 fsckd
.inodes
= RB_ROOT
;
2228 err
= dbg_walk_index(c
, check_leaf
, NULL
, &fsckd
);
2232 err
= check_inodes(c
, &fsckd
);
2236 free_inodes(&fsckd
);
2240 ubifs_err("file-system check failed with error %d", err
);
2242 free_inodes(&fsckd
);
2246 static int invocation_cnt
;
2248 int dbg_force_in_the_gaps(void)
2250 if (!dbg_force_in_the_gaps_enabled
)
2252 /* Force in-the-gaps every 8th commit */
2253 return !((invocation_cnt
++) & 0x7);
2256 /* Failure mode for recovery testing */
2258 #define chance(n, d) (simple_rand() <= (n) * 32768LL / (d))
2260 struct failure_mode_info
{
2261 struct list_head list
;
2262 struct ubifs_info
*c
;
2265 static LIST_HEAD(fmi_list
);
2266 static DEFINE_SPINLOCK(fmi_lock
);
2268 static unsigned int next
;
2270 static int simple_rand(void)
2273 next
= current
->pid
;
2274 next
= next
* 1103515245 + 12345;
2275 return (next
>> 16) & 32767;
2278 static void failure_mode_init(struct ubifs_info
*c
)
2280 struct failure_mode_info
*fmi
;
2282 fmi
= kmalloc(sizeof(struct failure_mode_info
), GFP_NOFS
);
2284 ubifs_err("Failed to register failure mode - no memory");
2288 spin_lock(&fmi_lock
);
2289 list_add_tail(&fmi
->list
, &fmi_list
);
2290 spin_unlock(&fmi_lock
);
2293 static void failure_mode_exit(struct ubifs_info
*c
)
2295 struct failure_mode_info
*fmi
, *tmp
;
2297 spin_lock(&fmi_lock
);
2298 list_for_each_entry_safe(fmi
, tmp
, &fmi_list
, list
)
2300 list_del(&fmi
->list
);
2303 spin_unlock(&fmi_lock
);
2306 static struct ubifs_info
*dbg_find_info(struct ubi_volume_desc
*desc
)
2308 struct failure_mode_info
*fmi
;
2310 spin_lock(&fmi_lock
);
2311 list_for_each_entry(fmi
, &fmi_list
, list
)
2312 if (fmi
->c
->ubi
== desc
) {
2313 struct ubifs_info
*c
= fmi
->c
;
2315 spin_unlock(&fmi_lock
);
2318 spin_unlock(&fmi_lock
);
2322 static int in_failure_mode(struct ubi_volume_desc
*desc
)
2324 struct ubifs_info
*c
= dbg_find_info(desc
);
2326 if (c
&& dbg_failure_mode
)
2327 return c
->dbg
->failure_mode
;
2331 static int do_fail(struct ubi_volume_desc
*desc
, int lnum
, int write
)
2333 struct ubifs_info
*c
= dbg_find_info(desc
);
2334 struct ubifs_debug_info
*d
;
2336 if (!c
|| !dbg_failure_mode
)
2339 if (d
->failure_mode
)
2342 /* First call - decide delay to failure */
2344 unsigned int delay
= 1 << (simple_rand() >> 11);
2348 d
->fail_timeout
= jiffies
+
2349 msecs_to_jiffies(delay
);
2350 dbg_rcvry("failing after %ums", delay
);
2353 d
->fail_cnt_max
= delay
;
2354 dbg_rcvry("failing after %u calls", delay
);
2359 /* Determine if failure delay has expired */
2360 if (d
->fail_delay
== 1) {
2361 if (time_before(jiffies
, d
->fail_timeout
))
2363 } else if (d
->fail_delay
== 2)
2364 if (d
->fail_cnt
++ < d
->fail_cnt_max
)
2366 if (lnum
== UBIFS_SB_LNUM
) {
2370 } else if (chance(19, 20))
2372 dbg_rcvry("failing in super block LEB %d", lnum
);
2373 } else if (lnum
== UBIFS_MST_LNUM
|| lnum
== UBIFS_MST_LNUM
+ 1) {
2376 dbg_rcvry("failing in master LEB %d", lnum
);
2377 } else if (lnum
>= UBIFS_LOG_LNUM
&& lnum
<= c
->log_last
) {
2379 if (chance(99, 100))
2381 } else if (chance(399, 400))
2383 dbg_rcvry("failing in log LEB %d", lnum
);
2384 } else if (lnum
>= c
->lpt_first
&& lnum
<= c
->lpt_last
) {
2388 } else if (chance(19, 20))
2390 dbg_rcvry("failing in LPT LEB %d", lnum
);
2391 } else if (lnum
>= c
->orph_first
&& lnum
<= c
->orph_last
) {
2395 } else if (chance(9, 10))
2397 dbg_rcvry("failing in orphan LEB %d", lnum
);
2398 } else if (lnum
== c
->ihead_lnum
) {
2399 if (chance(99, 100))
2401 dbg_rcvry("failing in index head LEB %d", lnum
);
2402 } else if (c
->jheads
&& lnum
== c
->jheads
[GCHD
].wbuf
.lnum
) {
2405 dbg_rcvry("failing in GC head LEB %d", lnum
);
2406 } else if (write
&& !RB_EMPTY_ROOT(&c
->buds
) &&
2407 !ubifs_search_bud(c
, lnum
)) {
2410 dbg_rcvry("failing in non-bud LEB %d", lnum
);
2411 } else if (c
->cmt_state
== COMMIT_RUNNING_BACKGROUND
||
2412 c
->cmt_state
== COMMIT_RUNNING_REQUIRED
) {
2413 if (chance(999, 1000))
2415 dbg_rcvry("failing in bud LEB %d commit running", lnum
);
2417 if (chance(9999, 10000))
2419 dbg_rcvry("failing in bud LEB %d commit not running", lnum
);
2421 ubifs_err("*** SETTING FAILURE MODE ON (LEB %d) ***", lnum
);
2422 d
->failure_mode
= 1;
2427 static void cut_data(const void *buf
, int len
)
2430 unsigned char *p
= (void *)buf
;
2432 flen
= (len
* (long long)simple_rand()) >> 15;
2433 for (i
= flen
; i
< len
; i
++)
2437 int dbg_leb_read(struct ubi_volume_desc
*desc
, int lnum
, char *buf
, int offset
,
2440 if (in_failure_mode(desc
))
2442 return ubi_leb_read(desc
, lnum
, buf
, offset
, len
, check
);
2445 int dbg_leb_write(struct ubi_volume_desc
*desc
, int lnum
, const void *buf
,
2446 int offset
, int len
, int dtype
)
2450 if (in_failure_mode(desc
))
2452 failing
= do_fail(desc
, lnum
, 1);
2455 err
= ubi_leb_write(desc
, lnum
, buf
, offset
, len
, dtype
);
2463 int dbg_leb_change(struct ubi_volume_desc
*desc
, int lnum
, const void *buf
,
2468 if (do_fail(desc
, lnum
, 1))
2470 err
= ubi_leb_change(desc
, lnum
, buf
, len
, dtype
);
2473 if (do_fail(desc
, lnum
, 1))
2478 int dbg_leb_erase(struct ubi_volume_desc
*desc
, int lnum
)
2482 if (do_fail(desc
, lnum
, 0))
2484 err
= ubi_leb_erase(desc
, lnum
);
2487 if (do_fail(desc
, lnum
, 0))
2492 int dbg_leb_unmap(struct ubi_volume_desc
*desc
, int lnum
)
2496 if (do_fail(desc
, lnum
, 0))
2498 err
= ubi_leb_unmap(desc
, lnum
);
2501 if (do_fail(desc
, lnum
, 0))
2506 int dbg_is_mapped(struct ubi_volume_desc
*desc
, int lnum
)
2508 if (in_failure_mode(desc
))
2510 return ubi_is_mapped(desc
, lnum
);
2513 int dbg_leb_map(struct ubi_volume_desc
*desc
, int lnum
, int dtype
)
2517 if (do_fail(desc
, lnum
, 0))
2519 err
= ubi_leb_map(desc
, lnum
, dtype
);
2522 if (do_fail(desc
, lnum
, 0))
2528 * ubifs_debugging_init - initialize UBIFS debugging.
2529 * @c: UBIFS file-system description object
2531 * This function initializes debugging-related data for the file system.
2532 * Returns zero in case of success and a negative error code in case of
2535 int ubifs_debugging_init(struct ubifs_info
*c
)
2537 c
->dbg
= kzalloc(sizeof(struct ubifs_debug_info
), GFP_KERNEL
);
2541 c
->dbg
->buf
= vmalloc(c
->leb_size
);
2545 failure_mode_init(c
);
2554 * ubifs_debugging_exit - free debugging data.
2555 * @c: UBIFS file-system description object
2557 void ubifs_debugging_exit(struct ubifs_info
*c
)
2559 failure_mode_exit(c
);
2565 * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
2566 * contain the stuff specific to particular file-system mounts.
2568 static struct dentry
*dfs_rootdir
;
2571 * dbg_debugfs_init - initialize debugfs file-system.
2573 * UBIFS uses debugfs file-system to expose various debugging knobs to
2574 * user-space. This function creates "ubifs" directory in the debugfs
2575 * file-system. Returns zero in case of success and a negative error code in
2578 int dbg_debugfs_init(void)
2580 dfs_rootdir
= debugfs_create_dir("ubifs", NULL
);
2581 if (IS_ERR(dfs_rootdir
)) {
2582 int err
= PTR_ERR(dfs_rootdir
);
2583 ubifs_err("cannot create \"ubifs\" debugfs directory, "
2592 * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
2594 void dbg_debugfs_exit(void)
2596 debugfs_remove(dfs_rootdir
);
2599 static int open_debugfs_file(struct inode
*inode
, struct file
*file
)
2601 file
->private_data
= inode
->i_private
;
2605 static ssize_t
write_debugfs_file(struct file
*file
, const char __user
*buf
,
2606 size_t count
, loff_t
*ppos
)
2608 struct ubifs_info
*c
= file
->private_data
;
2609 struct ubifs_debug_info
*d
= c
->dbg
;
2611 if (file
->f_path
.dentry
== d
->dfs_dump_lprops
)
2613 else if (file
->f_path
.dentry
== d
->dfs_dump_budg
) {
2614 spin_lock(&c
->space_lock
);
2616 spin_unlock(&c
->space_lock
);
2617 } else if (file
->f_path
.dentry
== d
->dfs_dump_tnc
) {
2618 mutex_lock(&c
->tnc_mutex
);
2620 mutex_unlock(&c
->tnc_mutex
);
2628 static const struct file_operations dfs_fops
= {
2629 .open
= open_debugfs_file
,
2630 .write
= write_debugfs_file
,
2631 .owner
= THIS_MODULE
,
2635 * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
2636 * @c: UBIFS file-system description object
2638 * This function creates all debugfs files for this instance of UBIFS. Returns
2639 * zero in case of success and a negative error code in case of failure.
2641 * Note, the only reason we have not merged this function with the
2642 * 'ubifs_debugging_init()' function is because it is better to initialize
2643 * debugfs interfaces at the very end of the mount process, and remove them at
2644 * the very beginning of the mount process.
2646 int dbg_debugfs_init_fs(struct ubifs_info
*c
)
2650 struct dentry
*dent
;
2651 struct ubifs_debug_info
*d
= c
->dbg
;
2653 sprintf(d
->dfs_dir_name
, "ubi%d_%d", c
->vi
.ubi_num
, c
->vi
.vol_id
);
2654 d
->dfs_dir
= debugfs_create_dir(d
->dfs_dir_name
, dfs_rootdir
);
2655 if (IS_ERR(d
->dfs_dir
)) {
2656 err
= PTR_ERR(d
->dfs_dir
);
2657 ubifs_err("cannot create \"%s\" debugfs directory, error %d\n",
2658 d
->dfs_dir_name
, err
);
2662 fname
= "dump_lprops";
2663 dent
= debugfs_create_file(fname
, S_IWUGO
, d
->dfs_dir
, c
, &dfs_fops
);
2666 d
->dfs_dump_lprops
= dent
;
2668 fname
= "dump_budg";
2669 dent
= debugfs_create_file(fname
, S_IWUGO
, d
->dfs_dir
, c
, &dfs_fops
);
2672 d
->dfs_dump_budg
= dent
;
2675 dent
= debugfs_create_file(fname
, S_IWUGO
, d
->dfs_dir
, c
, &dfs_fops
);
2678 d
->dfs_dump_tnc
= dent
;
2683 err
= PTR_ERR(dent
);
2684 ubifs_err("cannot create \"%s\" debugfs directory, error %d\n",
2686 debugfs_remove_recursive(d
->dfs_dir
);
2692 * dbg_debugfs_exit_fs - remove all debugfs files.
2693 * @c: UBIFS file-system description object
2695 void dbg_debugfs_exit_fs(struct ubifs_info
*c
)
2697 debugfs_remove_recursive(c
->dbg
->dfs_dir
);
2700 #endif /* CONFIG_UBIFS_FS_DEBUG */