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.
31 #include <linux/module.h>
32 #include <linux/debugfs.h>
33 #include <linux/math64.h>
34 #include <linux/uaccess.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 static const char *get_key_fmt(int fmt
)
46 case UBIFS_SIMPLE_KEY_FMT
:
49 return "unknown/invalid format";
53 static const char *get_key_hash(int hash
)
56 case UBIFS_KEY_HASH_R5
:
58 case UBIFS_KEY_HASH_TEST
:
61 return "unknown/invalid name hash";
65 static const char *get_key_type(int type
)
79 return "unknown/invalid key";
83 static const char *get_dent_type(int type
)
96 case UBIFS_ITYPE_FIFO
:
98 case UBIFS_ITYPE_SOCK
:
101 return "unknown/invalid type";
105 static void sprintf_key(const struct ubifs_info
*c
, const union ubifs_key
*key
,
109 int type
= key_type(c
, key
);
111 if (c
->key_fmt
== UBIFS_SIMPLE_KEY_FMT
) {
114 sprintf(p
, "(%lu, %s)", (unsigned long)key_inum(c
, key
),
119 sprintf(p
, "(%lu, %s, %#08x)",
120 (unsigned long)key_inum(c
, key
),
121 get_key_type(type
), key_hash(c
, key
));
124 sprintf(p
, "(%lu, %s, %u)",
125 (unsigned long)key_inum(c
, key
),
126 get_key_type(type
), key_block(c
, key
));
129 sprintf(p
, "(%lu, %s)",
130 (unsigned long)key_inum(c
, key
),
134 sprintf(p
, "(bad key type: %#08x, %#08x)",
135 key
->u32
[0], key
->u32
[1]);
138 sprintf(p
, "bad key format %d", c
->key_fmt
);
141 const char *dbg_key_str0(const struct ubifs_info
*c
, const union ubifs_key
*key
)
143 /* dbg_lock must be held */
144 sprintf_key(c
, key
, dbg_key_buf0
);
148 const char *dbg_key_str1(const struct ubifs_info
*c
, const union ubifs_key
*key
)
150 /* dbg_lock must be held */
151 sprintf_key(c
, key
, dbg_key_buf1
);
155 const char *dbg_ntype(int type
)
159 return "padding node";
161 return "superblock node";
163 return "master node";
165 return "reference node";
168 case UBIFS_DENT_NODE
:
169 return "direntry node";
170 case UBIFS_XENT_NODE
:
171 return "xentry node";
172 case UBIFS_DATA_NODE
:
174 case UBIFS_TRUN_NODE
:
175 return "truncate node";
177 return "indexing node";
179 return "commit start node";
180 case UBIFS_ORPH_NODE
:
181 return "orphan node";
183 return "unknown node";
187 static const char *dbg_gtype(int type
)
190 case UBIFS_NO_NODE_GROUP
:
191 return "no node group";
192 case UBIFS_IN_NODE_GROUP
:
193 return "in node group";
194 case UBIFS_LAST_OF_NODE_GROUP
:
195 return "last of node group";
201 const char *dbg_cstate(int cmt_state
)
205 return "commit resting";
206 case COMMIT_BACKGROUND
:
207 return "background commit requested";
208 case COMMIT_REQUIRED
:
209 return "commit required";
210 case COMMIT_RUNNING_BACKGROUND
:
211 return "BACKGROUND commit running";
212 case COMMIT_RUNNING_REQUIRED
:
213 return "commit running and required";
215 return "broken commit";
217 return "unknown commit state";
221 const char *dbg_jhead(int jhead
)
231 return "unknown journal head";
235 static void dump_ch(const struct ubifs_ch
*ch
)
237 printk(KERN_DEBUG
"\tmagic %#x\n", le32_to_cpu(ch
->magic
));
238 printk(KERN_DEBUG
"\tcrc %#x\n", le32_to_cpu(ch
->crc
));
239 printk(KERN_DEBUG
"\tnode_type %d (%s)\n", ch
->node_type
,
240 dbg_ntype(ch
->node_type
));
241 printk(KERN_DEBUG
"\tgroup_type %d (%s)\n", ch
->group_type
,
242 dbg_gtype(ch
->group_type
));
243 printk(KERN_DEBUG
"\tsqnum %llu\n",
244 (unsigned long long)le64_to_cpu(ch
->sqnum
));
245 printk(KERN_DEBUG
"\tlen %u\n", le32_to_cpu(ch
->len
));
248 void dbg_dump_inode(struct ubifs_info
*c
, const struct inode
*inode
)
250 const struct ubifs_inode
*ui
= ubifs_inode(inode
);
251 struct qstr nm
= { .name
= NULL
};
253 struct ubifs_dent_node
*dent
, *pdent
= NULL
;
256 printk(KERN_DEBUG
"Dump in-memory inode:");
257 printk(KERN_DEBUG
"\tinode %lu\n", inode
->i_ino
);
258 printk(KERN_DEBUG
"\tsize %llu\n",
259 (unsigned long long)i_size_read(inode
));
260 printk(KERN_DEBUG
"\tnlink %u\n", inode
->i_nlink
);
261 printk(KERN_DEBUG
"\tuid %u\n", (unsigned int)inode
->i_uid
);
262 printk(KERN_DEBUG
"\tgid %u\n", (unsigned int)inode
->i_gid
);
263 printk(KERN_DEBUG
"\tatime %u.%u\n",
264 (unsigned int)inode
->i_atime
.tv_sec
,
265 (unsigned int)inode
->i_atime
.tv_nsec
);
266 printk(KERN_DEBUG
"\tmtime %u.%u\n",
267 (unsigned int)inode
->i_mtime
.tv_sec
,
268 (unsigned int)inode
->i_mtime
.tv_nsec
);
269 printk(KERN_DEBUG
"\tctime %u.%u\n",
270 (unsigned int)inode
->i_ctime
.tv_sec
,
271 (unsigned int)inode
->i_ctime
.tv_nsec
);
272 printk(KERN_DEBUG
"\tcreat_sqnum %llu\n", ui
->creat_sqnum
);
273 printk(KERN_DEBUG
"\txattr_size %u\n", ui
->xattr_size
);
274 printk(KERN_DEBUG
"\txattr_cnt %u\n", ui
->xattr_cnt
);
275 printk(KERN_DEBUG
"\txattr_names %u\n", ui
->xattr_names
);
276 printk(KERN_DEBUG
"\tdirty %u\n", ui
->dirty
);
277 printk(KERN_DEBUG
"\txattr %u\n", ui
->xattr
);
278 printk(KERN_DEBUG
"\tbulk_read %u\n", ui
->xattr
);
279 printk(KERN_DEBUG
"\tsynced_i_size %llu\n",
280 (unsigned long long)ui
->synced_i_size
);
281 printk(KERN_DEBUG
"\tui_size %llu\n",
282 (unsigned long long)ui
->ui_size
);
283 printk(KERN_DEBUG
"\tflags %d\n", ui
->flags
);
284 printk(KERN_DEBUG
"\tcompr_type %d\n", ui
->compr_type
);
285 printk(KERN_DEBUG
"\tlast_page_read %lu\n", ui
->last_page_read
);
286 printk(KERN_DEBUG
"\tread_in_a_row %lu\n", ui
->read_in_a_row
);
287 printk(KERN_DEBUG
"\tdata_len %d\n", ui
->data_len
);
289 if (!S_ISDIR(inode
->i_mode
))
292 printk(KERN_DEBUG
"List of directory entries:\n");
293 ubifs_assert(!mutex_is_locked(&c
->tnc_mutex
));
295 lowest_dent_key(c
, &key
, inode
->i_ino
);
297 dent
= ubifs_tnc_next_ent(c
, &key
, &nm
);
299 if (PTR_ERR(dent
) != -ENOENT
)
300 printk(KERN_DEBUG
"error %ld\n", PTR_ERR(dent
));
304 printk(KERN_DEBUG
"\t%d: %s (%s)\n",
305 count
++, dent
->name
, get_dent_type(dent
->type
));
307 nm
.name
= dent
->name
;
308 nm
.len
= le16_to_cpu(dent
->nlen
);
311 key_read(c
, &dent
->key
, &key
);
316 void dbg_dump_node(const struct ubifs_info
*c
, const void *node
)
320 const struct ubifs_ch
*ch
= node
;
322 if (dbg_is_tst_rcvry(c
))
325 /* If the magic is incorrect, just hexdump the first bytes */
326 if (le32_to_cpu(ch
->magic
) != UBIFS_NODE_MAGIC
) {
327 printk(KERN_DEBUG
"Not a node, first %zu bytes:", UBIFS_CH_SZ
);
328 print_hex_dump(KERN_DEBUG
, "", DUMP_PREFIX_OFFSET
, 32, 1,
329 (void *)node
, UBIFS_CH_SZ
, 1);
333 spin_lock(&dbg_lock
);
336 switch (ch
->node_type
) {
339 const struct ubifs_pad_node
*pad
= node
;
341 printk(KERN_DEBUG
"\tpad_len %u\n",
342 le32_to_cpu(pad
->pad_len
));
347 const struct ubifs_sb_node
*sup
= node
;
348 unsigned int sup_flags
= le32_to_cpu(sup
->flags
);
350 printk(KERN_DEBUG
"\tkey_hash %d (%s)\n",
351 (int)sup
->key_hash
, get_key_hash(sup
->key_hash
));
352 printk(KERN_DEBUG
"\tkey_fmt %d (%s)\n",
353 (int)sup
->key_fmt
, get_key_fmt(sup
->key_fmt
));
354 printk(KERN_DEBUG
"\tflags %#x\n", sup_flags
);
355 printk(KERN_DEBUG
"\t big_lpt %u\n",
356 !!(sup_flags
& UBIFS_FLG_BIGLPT
));
357 printk(KERN_DEBUG
"\t space_fixup %u\n",
358 !!(sup_flags
& UBIFS_FLG_SPACE_FIXUP
));
359 printk(KERN_DEBUG
"\tmin_io_size %u\n",
360 le32_to_cpu(sup
->min_io_size
));
361 printk(KERN_DEBUG
"\tleb_size %u\n",
362 le32_to_cpu(sup
->leb_size
));
363 printk(KERN_DEBUG
"\tleb_cnt %u\n",
364 le32_to_cpu(sup
->leb_cnt
));
365 printk(KERN_DEBUG
"\tmax_leb_cnt %u\n",
366 le32_to_cpu(sup
->max_leb_cnt
));
367 printk(KERN_DEBUG
"\tmax_bud_bytes %llu\n",
368 (unsigned long long)le64_to_cpu(sup
->max_bud_bytes
));
369 printk(KERN_DEBUG
"\tlog_lebs %u\n",
370 le32_to_cpu(sup
->log_lebs
));
371 printk(KERN_DEBUG
"\tlpt_lebs %u\n",
372 le32_to_cpu(sup
->lpt_lebs
));
373 printk(KERN_DEBUG
"\torph_lebs %u\n",
374 le32_to_cpu(sup
->orph_lebs
));
375 printk(KERN_DEBUG
"\tjhead_cnt %u\n",
376 le32_to_cpu(sup
->jhead_cnt
));
377 printk(KERN_DEBUG
"\tfanout %u\n",
378 le32_to_cpu(sup
->fanout
));
379 printk(KERN_DEBUG
"\tlsave_cnt %u\n",
380 le32_to_cpu(sup
->lsave_cnt
));
381 printk(KERN_DEBUG
"\tdefault_compr %u\n",
382 (int)le16_to_cpu(sup
->default_compr
));
383 printk(KERN_DEBUG
"\trp_size %llu\n",
384 (unsigned long long)le64_to_cpu(sup
->rp_size
));
385 printk(KERN_DEBUG
"\trp_uid %u\n",
386 le32_to_cpu(sup
->rp_uid
));
387 printk(KERN_DEBUG
"\trp_gid %u\n",
388 le32_to_cpu(sup
->rp_gid
));
389 printk(KERN_DEBUG
"\tfmt_version %u\n",
390 le32_to_cpu(sup
->fmt_version
));
391 printk(KERN_DEBUG
"\ttime_gran %u\n",
392 le32_to_cpu(sup
->time_gran
));
393 printk(KERN_DEBUG
"\tUUID %pUB\n",
399 const struct ubifs_mst_node
*mst
= node
;
401 printk(KERN_DEBUG
"\thighest_inum %llu\n",
402 (unsigned long long)le64_to_cpu(mst
->highest_inum
));
403 printk(KERN_DEBUG
"\tcommit number %llu\n",
404 (unsigned long long)le64_to_cpu(mst
->cmt_no
));
405 printk(KERN_DEBUG
"\tflags %#x\n",
406 le32_to_cpu(mst
->flags
));
407 printk(KERN_DEBUG
"\tlog_lnum %u\n",
408 le32_to_cpu(mst
->log_lnum
));
409 printk(KERN_DEBUG
"\troot_lnum %u\n",
410 le32_to_cpu(mst
->root_lnum
));
411 printk(KERN_DEBUG
"\troot_offs %u\n",
412 le32_to_cpu(mst
->root_offs
));
413 printk(KERN_DEBUG
"\troot_len %u\n",
414 le32_to_cpu(mst
->root_len
));
415 printk(KERN_DEBUG
"\tgc_lnum %u\n",
416 le32_to_cpu(mst
->gc_lnum
));
417 printk(KERN_DEBUG
"\tihead_lnum %u\n",
418 le32_to_cpu(mst
->ihead_lnum
));
419 printk(KERN_DEBUG
"\tihead_offs %u\n",
420 le32_to_cpu(mst
->ihead_offs
));
421 printk(KERN_DEBUG
"\tindex_size %llu\n",
422 (unsigned long long)le64_to_cpu(mst
->index_size
));
423 printk(KERN_DEBUG
"\tlpt_lnum %u\n",
424 le32_to_cpu(mst
->lpt_lnum
));
425 printk(KERN_DEBUG
"\tlpt_offs %u\n",
426 le32_to_cpu(mst
->lpt_offs
));
427 printk(KERN_DEBUG
"\tnhead_lnum %u\n",
428 le32_to_cpu(mst
->nhead_lnum
));
429 printk(KERN_DEBUG
"\tnhead_offs %u\n",
430 le32_to_cpu(mst
->nhead_offs
));
431 printk(KERN_DEBUG
"\tltab_lnum %u\n",
432 le32_to_cpu(mst
->ltab_lnum
));
433 printk(KERN_DEBUG
"\tltab_offs %u\n",
434 le32_to_cpu(mst
->ltab_offs
));
435 printk(KERN_DEBUG
"\tlsave_lnum %u\n",
436 le32_to_cpu(mst
->lsave_lnum
));
437 printk(KERN_DEBUG
"\tlsave_offs %u\n",
438 le32_to_cpu(mst
->lsave_offs
));
439 printk(KERN_DEBUG
"\tlscan_lnum %u\n",
440 le32_to_cpu(mst
->lscan_lnum
));
441 printk(KERN_DEBUG
"\tleb_cnt %u\n",
442 le32_to_cpu(mst
->leb_cnt
));
443 printk(KERN_DEBUG
"\tempty_lebs %u\n",
444 le32_to_cpu(mst
->empty_lebs
));
445 printk(KERN_DEBUG
"\tidx_lebs %u\n",
446 le32_to_cpu(mst
->idx_lebs
));
447 printk(KERN_DEBUG
"\ttotal_free %llu\n",
448 (unsigned long long)le64_to_cpu(mst
->total_free
));
449 printk(KERN_DEBUG
"\ttotal_dirty %llu\n",
450 (unsigned long long)le64_to_cpu(mst
->total_dirty
));
451 printk(KERN_DEBUG
"\ttotal_used %llu\n",
452 (unsigned long long)le64_to_cpu(mst
->total_used
));
453 printk(KERN_DEBUG
"\ttotal_dead %llu\n",
454 (unsigned long long)le64_to_cpu(mst
->total_dead
));
455 printk(KERN_DEBUG
"\ttotal_dark %llu\n",
456 (unsigned long long)le64_to_cpu(mst
->total_dark
));
461 const struct ubifs_ref_node
*ref
= node
;
463 printk(KERN_DEBUG
"\tlnum %u\n",
464 le32_to_cpu(ref
->lnum
));
465 printk(KERN_DEBUG
"\toffs %u\n",
466 le32_to_cpu(ref
->offs
));
467 printk(KERN_DEBUG
"\tjhead %u\n",
468 le32_to_cpu(ref
->jhead
));
473 const struct ubifs_ino_node
*ino
= node
;
475 key_read(c
, &ino
->key
, &key
);
476 printk(KERN_DEBUG
"\tkey %s\n", DBGKEY(&key
));
477 printk(KERN_DEBUG
"\tcreat_sqnum %llu\n",
478 (unsigned long long)le64_to_cpu(ino
->creat_sqnum
));
479 printk(KERN_DEBUG
"\tsize %llu\n",
480 (unsigned long long)le64_to_cpu(ino
->size
));
481 printk(KERN_DEBUG
"\tnlink %u\n",
482 le32_to_cpu(ino
->nlink
));
483 printk(KERN_DEBUG
"\tatime %lld.%u\n",
484 (long long)le64_to_cpu(ino
->atime_sec
),
485 le32_to_cpu(ino
->atime_nsec
));
486 printk(KERN_DEBUG
"\tmtime %lld.%u\n",
487 (long long)le64_to_cpu(ino
->mtime_sec
),
488 le32_to_cpu(ino
->mtime_nsec
));
489 printk(KERN_DEBUG
"\tctime %lld.%u\n",
490 (long long)le64_to_cpu(ino
->ctime_sec
),
491 le32_to_cpu(ino
->ctime_nsec
));
492 printk(KERN_DEBUG
"\tuid %u\n",
493 le32_to_cpu(ino
->uid
));
494 printk(KERN_DEBUG
"\tgid %u\n",
495 le32_to_cpu(ino
->gid
));
496 printk(KERN_DEBUG
"\tmode %u\n",
497 le32_to_cpu(ino
->mode
));
498 printk(KERN_DEBUG
"\tflags %#x\n",
499 le32_to_cpu(ino
->flags
));
500 printk(KERN_DEBUG
"\txattr_cnt %u\n",
501 le32_to_cpu(ino
->xattr_cnt
));
502 printk(KERN_DEBUG
"\txattr_size %u\n",
503 le32_to_cpu(ino
->xattr_size
));
504 printk(KERN_DEBUG
"\txattr_names %u\n",
505 le32_to_cpu(ino
->xattr_names
));
506 printk(KERN_DEBUG
"\tcompr_type %#x\n",
507 (int)le16_to_cpu(ino
->compr_type
));
508 printk(KERN_DEBUG
"\tdata len %u\n",
509 le32_to_cpu(ino
->data_len
));
512 case UBIFS_DENT_NODE
:
513 case UBIFS_XENT_NODE
:
515 const struct ubifs_dent_node
*dent
= node
;
516 int nlen
= le16_to_cpu(dent
->nlen
);
518 key_read(c
, &dent
->key
, &key
);
519 printk(KERN_DEBUG
"\tkey %s\n", DBGKEY(&key
));
520 printk(KERN_DEBUG
"\tinum %llu\n",
521 (unsigned long long)le64_to_cpu(dent
->inum
));
522 printk(KERN_DEBUG
"\ttype %d\n", (int)dent
->type
);
523 printk(KERN_DEBUG
"\tnlen %d\n", nlen
);
524 printk(KERN_DEBUG
"\tname ");
526 if (nlen
> UBIFS_MAX_NLEN
)
527 printk(KERN_DEBUG
"(bad name length, not printing, "
528 "bad or corrupted node)");
530 for (i
= 0; i
< nlen
&& dent
->name
[i
]; i
++)
531 printk(KERN_CONT
"%c", dent
->name
[i
]);
533 printk(KERN_CONT
"\n");
537 case UBIFS_DATA_NODE
:
539 const struct ubifs_data_node
*dn
= node
;
540 int dlen
= le32_to_cpu(ch
->len
) - UBIFS_DATA_NODE_SZ
;
542 key_read(c
, &dn
->key
, &key
);
543 printk(KERN_DEBUG
"\tkey %s\n", DBGKEY(&key
));
544 printk(KERN_DEBUG
"\tsize %u\n",
545 le32_to_cpu(dn
->size
));
546 printk(KERN_DEBUG
"\tcompr_typ %d\n",
547 (int)le16_to_cpu(dn
->compr_type
));
548 printk(KERN_DEBUG
"\tdata size %d\n",
550 printk(KERN_DEBUG
"\tdata:\n");
551 print_hex_dump(KERN_DEBUG
, "\t", DUMP_PREFIX_OFFSET
, 32, 1,
552 (void *)&dn
->data
, dlen
, 0);
555 case UBIFS_TRUN_NODE
:
557 const struct ubifs_trun_node
*trun
= node
;
559 printk(KERN_DEBUG
"\tinum %u\n",
560 le32_to_cpu(trun
->inum
));
561 printk(KERN_DEBUG
"\told_size %llu\n",
562 (unsigned long long)le64_to_cpu(trun
->old_size
));
563 printk(KERN_DEBUG
"\tnew_size %llu\n",
564 (unsigned long long)le64_to_cpu(trun
->new_size
));
569 const struct ubifs_idx_node
*idx
= node
;
571 n
= le16_to_cpu(idx
->child_cnt
);
572 printk(KERN_DEBUG
"\tchild_cnt %d\n", n
);
573 printk(KERN_DEBUG
"\tlevel %d\n",
574 (int)le16_to_cpu(idx
->level
));
575 printk(KERN_DEBUG
"\tBranches:\n");
577 for (i
= 0; i
< n
&& i
< c
->fanout
- 1; i
++) {
578 const struct ubifs_branch
*br
;
580 br
= ubifs_idx_branch(c
, idx
, i
);
581 key_read(c
, &br
->key
, &key
);
582 printk(KERN_DEBUG
"\t%d: LEB %d:%d len %d key %s\n",
583 i
, le32_to_cpu(br
->lnum
), le32_to_cpu(br
->offs
),
584 le32_to_cpu(br
->len
), DBGKEY(&key
));
590 case UBIFS_ORPH_NODE
:
592 const struct ubifs_orph_node
*orph
= node
;
594 printk(KERN_DEBUG
"\tcommit number %llu\n",
596 le64_to_cpu(orph
->cmt_no
) & LLONG_MAX
);
597 printk(KERN_DEBUG
"\tlast node flag %llu\n",
598 (unsigned long long)(le64_to_cpu(orph
->cmt_no
)) >> 63);
599 n
= (le32_to_cpu(ch
->len
) - UBIFS_ORPH_NODE_SZ
) >> 3;
600 printk(KERN_DEBUG
"\t%d orphan inode numbers:\n", n
);
601 for (i
= 0; i
< n
; i
++)
602 printk(KERN_DEBUG
"\t ino %llu\n",
603 (unsigned long long)le64_to_cpu(orph
->inos
[i
]));
607 printk(KERN_DEBUG
"node type %d was not recognized\n",
610 spin_unlock(&dbg_lock
);
613 void dbg_dump_budget_req(const struct ubifs_budget_req
*req
)
615 spin_lock(&dbg_lock
);
616 printk(KERN_DEBUG
"Budgeting request: new_ino %d, dirtied_ino %d\n",
617 req
->new_ino
, req
->dirtied_ino
);
618 printk(KERN_DEBUG
"\tnew_ino_d %d, dirtied_ino_d %d\n",
619 req
->new_ino_d
, req
->dirtied_ino_d
);
620 printk(KERN_DEBUG
"\tnew_page %d, dirtied_page %d\n",
621 req
->new_page
, req
->dirtied_page
);
622 printk(KERN_DEBUG
"\tnew_dent %d, mod_dent %d\n",
623 req
->new_dent
, req
->mod_dent
);
624 printk(KERN_DEBUG
"\tidx_growth %d\n", req
->idx_growth
);
625 printk(KERN_DEBUG
"\tdata_growth %d dd_growth %d\n",
626 req
->data_growth
, req
->dd_growth
);
627 spin_unlock(&dbg_lock
);
630 void dbg_dump_lstats(const struct ubifs_lp_stats
*lst
)
632 spin_lock(&dbg_lock
);
633 printk(KERN_DEBUG
"(pid %d) Lprops statistics: empty_lebs %d, "
634 "idx_lebs %d\n", current
->pid
, lst
->empty_lebs
, lst
->idx_lebs
);
635 printk(KERN_DEBUG
"\ttaken_empty_lebs %d, total_free %lld, "
636 "total_dirty %lld\n", lst
->taken_empty_lebs
, lst
->total_free
,
638 printk(KERN_DEBUG
"\ttotal_used %lld, total_dark %lld, "
639 "total_dead %lld\n", lst
->total_used
, lst
->total_dark
,
641 spin_unlock(&dbg_lock
);
644 void dbg_dump_budg(struct ubifs_info
*c
, const struct ubifs_budg_info
*bi
)
648 struct ubifs_bud
*bud
;
649 struct ubifs_gced_idx_leb
*idx_gc
;
650 long long available
, outstanding
, free
;
652 spin_lock(&c
->space_lock
);
653 spin_lock(&dbg_lock
);
654 printk(KERN_DEBUG
"(pid %d) Budgeting info: data budget sum %lld, "
655 "total budget sum %lld\n", current
->pid
,
656 bi
->data_growth
+ bi
->dd_growth
,
657 bi
->data_growth
+ bi
->dd_growth
+ bi
->idx_growth
);
658 printk(KERN_DEBUG
"\tbudg_data_growth %lld, budg_dd_growth %lld, "
659 "budg_idx_growth %lld\n", bi
->data_growth
, bi
->dd_growth
,
661 printk(KERN_DEBUG
"\tmin_idx_lebs %d, old_idx_sz %llu, "
662 "uncommitted_idx %lld\n", bi
->min_idx_lebs
, bi
->old_idx_sz
,
663 bi
->uncommitted_idx
);
664 printk(KERN_DEBUG
"\tpage_budget %d, inode_budget %d, dent_budget %d\n",
665 bi
->page_budget
, bi
->inode_budget
, bi
->dent_budget
);
666 printk(KERN_DEBUG
"\tnospace %u, nospace_rp %u\n",
667 bi
->nospace
, bi
->nospace_rp
);
668 printk(KERN_DEBUG
"\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
669 c
->dark_wm
, c
->dead_wm
, c
->max_idx_node_sz
);
673 * If we are dumping saved budgeting data, do not print
674 * additional information which is about the current state, not
675 * the old one which corresponded to the saved budgeting data.
679 printk(KERN_DEBUG
"\tfreeable_cnt %d, calc_idx_sz %lld, idx_gc_cnt %d\n",
680 c
->freeable_cnt
, c
->calc_idx_sz
, c
->idx_gc_cnt
);
681 printk(KERN_DEBUG
"\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, "
682 "clean_zn_cnt %ld\n", atomic_long_read(&c
->dirty_pg_cnt
),
683 atomic_long_read(&c
->dirty_zn_cnt
),
684 atomic_long_read(&c
->clean_zn_cnt
));
685 printk(KERN_DEBUG
"\tgc_lnum %d, ihead_lnum %d\n",
686 c
->gc_lnum
, c
->ihead_lnum
);
688 /* If we are in R/O mode, journal heads do not exist */
690 for (i
= 0; i
< c
->jhead_cnt
; i
++)
691 printk(KERN_DEBUG
"\tjhead %s\t LEB %d\n",
692 dbg_jhead(c
->jheads
[i
].wbuf
.jhead
),
693 c
->jheads
[i
].wbuf
.lnum
);
694 for (rb
= rb_first(&c
->buds
); rb
; rb
= rb_next(rb
)) {
695 bud
= rb_entry(rb
, struct ubifs_bud
, rb
);
696 printk(KERN_DEBUG
"\tbud LEB %d\n", bud
->lnum
);
698 list_for_each_entry(bud
, &c
->old_buds
, list
)
699 printk(KERN_DEBUG
"\told bud LEB %d\n", bud
->lnum
);
700 list_for_each_entry(idx_gc
, &c
->idx_gc
, list
)
701 printk(KERN_DEBUG
"\tGC'ed idx LEB %d unmap %d\n",
702 idx_gc
->lnum
, idx_gc
->unmap
);
703 printk(KERN_DEBUG
"\tcommit state %d\n", c
->cmt_state
);
705 /* Print budgeting predictions */
706 available
= ubifs_calc_available(c
, c
->bi
.min_idx_lebs
);
707 outstanding
= c
->bi
.data_growth
+ c
->bi
.dd_growth
;
708 free
= ubifs_get_free_space_nolock(c
);
709 printk(KERN_DEBUG
"Budgeting predictions:\n");
710 printk(KERN_DEBUG
"\tavailable: %lld, outstanding %lld, free %lld\n",
711 available
, outstanding
, free
);
713 spin_unlock(&dbg_lock
);
714 spin_unlock(&c
->space_lock
);
717 void dbg_dump_lprop(const struct ubifs_info
*c
, const struct ubifs_lprops
*lp
)
719 int i
, spc
, dark
= 0, dead
= 0;
721 struct ubifs_bud
*bud
;
723 spc
= lp
->free
+ lp
->dirty
;
724 if (spc
< c
->dead_wm
)
727 dark
= ubifs_calc_dark(c
, spc
);
729 if (lp
->flags
& LPROPS_INDEX
)
730 printk(KERN_DEBUG
"LEB %-7d free %-8d dirty %-8d used %-8d "
731 "free + dirty %-8d flags %#x (", lp
->lnum
, lp
->free
,
732 lp
->dirty
, c
->leb_size
- spc
, spc
, lp
->flags
);
734 printk(KERN_DEBUG
"LEB %-7d free %-8d dirty %-8d used %-8d "
735 "free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d "
736 "flags %#-4x (", lp
->lnum
, lp
->free
, lp
->dirty
,
737 c
->leb_size
- spc
, spc
, dark
, dead
,
738 (int)(spc
/ UBIFS_MAX_NODE_SZ
), lp
->flags
);
740 if (lp
->flags
& LPROPS_TAKEN
) {
741 if (lp
->flags
& LPROPS_INDEX
)
742 printk(KERN_CONT
"index, taken");
744 printk(KERN_CONT
"taken");
748 if (lp
->flags
& LPROPS_INDEX
) {
749 switch (lp
->flags
& LPROPS_CAT_MASK
) {
750 case LPROPS_DIRTY_IDX
:
753 case LPROPS_FRDI_IDX
:
754 s
= "freeable index";
760 switch (lp
->flags
& LPROPS_CAT_MASK
) {
762 s
= "not categorized";
773 case LPROPS_FREEABLE
:
781 printk(KERN_CONT
"%s", s
);
784 for (rb
= rb_first((struct rb_root
*)&c
->buds
); rb
; rb
= rb_next(rb
)) {
785 bud
= rb_entry(rb
, struct ubifs_bud
, rb
);
786 if (bud
->lnum
== lp
->lnum
) {
788 for (i
= 0; i
< c
->jhead_cnt
; i
++) {
790 * Note, if we are in R/O mode or in the middle
791 * of mounting/re-mounting, the write-buffers do
795 lp
->lnum
== c
->jheads
[i
].wbuf
.lnum
) {
796 printk(KERN_CONT
", jhead %s",
802 printk(KERN_CONT
", bud of jhead %s",
803 dbg_jhead(bud
->jhead
));
806 if (lp
->lnum
== c
->gc_lnum
)
807 printk(KERN_CONT
", GC LEB");
808 printk(KERN_CONT
")\n");
811 void dbg_dump_lprops(struct ubifs_info
*c
)
814 struct ubifs_lprops lp
;
815 struct ubifs_lp_stats lst
;
817 printk(KERN_DEBUG
"(pid %d) start dumping LEB properties\n",
819 ubifs_get_lp_stats(c
, &lst
);
820 dbg_dump_lstats(&lst
);
822 for (lnum
= c
->main_first
; lnum
< c
->leb_cnt
; lnum
++) {
823 err
= ubifs_read_one_lp(c
, lnum
, &lp
);
825 ubifs_err("cannot read lprops for LEB %d", lnum
);
827 dbg_dump_lprop(c
, &lp
);
829 printk(KERN_DEBUG
"(pid %d) finish dumping LEB properties\n",
833 void dbg_dump_lpt_info(struct ubifs_info
*c
)
837 spin_lock(&dbg_lock
);
838 printk(KERN_DEBUG
"(pid %d) dumping LPT information\n", current
->pid
);
839 printk(KERN_DEBUG
"\tlpt_sz: %lld\n", c
->lpt_sz
);
840 printk(KERN_DEBUG
"\tpnode_sz: %d\n", c
->pnode_sz
);
841 printk(KERN_DEBUG
"\tnnode_sz: %d\n", c
->nnode_sz
);
842 printk(KERN_DEBUG
"\tltab_sz: %d\n", c
->ltab_sz
);
843 printk(KERN_DEBUG
"\tlsave_sz: %d\n", c
->lsave_sz
);
844 printk(KERN_DEBUG
"\tbig_lpt: %d\n", c
->big_lpt
);
845 printk(KERN_DEBUG
"\tlpt_hght: %d\n", c
->lpt_hght
);
846 printk(KERN_DEBUG
"\tpnode_cnt: %d\n", c
->pnode_cnt
);
847 printk(KERN_DEBUG
"\tnnode_cnt: %d\n", c
->nnode_cnt
);
848 printk(KERN_DEBUG
"\tdirty_pn_cnt: %d\n", c
->dirty_pn_cnt
);
849 printk(KERN_DEBUG
"\tdirty_nn_cnt: %d\n", c
->dirty_nn_cnt
);
850 printk(KERN_DEBUG
"\tlsave_cnt: %d\n", c
->lsave_cnt
);
851 printk(KERN_DEBUG
"\tspace_bits: %d\n", c
->space_bits
);
852 printk(KERN_DEBUG
"\tlpt_lnum_bits: %d\n", c
->lpt_lnum_bits
);
853 printk(KERN_DEBUG
"\tlpt_offs_bits: %d\n", c
->lpt_offs_bits
);
854 printk(KERN_DEBUG
"\tlpt_spc_bits: %d\n", c
->lpt_spc_bits
);
855 printk(KERN_DEBUG
"\tpcnt_bits: %d\n", c
->pcnt_bits
);
856 printk(KERN_DEBUG
"\tlnum_bits: %d\n", c
->lnum_bits
);
857 printk(KERN_DEBUG
"\tLPT root is at %d:%d\n", c
->lpt_lnum
, c
->lpt_offs
);
858 printk(KERN_DEBUG
"\tLPT head is at %d:%d\n",
859 c
->nhead_lnum
, c
->nhead_offs
);
860 printk(KERN_DEBUG
"\tLPT ltab is at %d:%d\n",
861 c
->ltab_lnum
, c
->ltab_offs
);
863 printk(KERN_DEBUG
"\tLPT lsave is at %d:%d\n",
864 c
->lsave_lnum
, c
->lsave_offs
);
865 for (i
= 0; i
< c
->lpt_lebs
; i
++)
866 printk(KERN_DEBUG
"\tLPT LEB %d free %d dirty %d tgc %d "
867 "cmt %d\n", i
+ c
->lpt_first
, c
->ltab
[i
].free
,
868 c
->ltab
[i
].dirty
, c
->ltab
[i
].tgc
, c
->ltab
[i
].cmt
);
869 spin_unlock(&dbg_lock
);
872 void dbg_dump_leb(const struct ubifs_info
*c
, int lnum
)
874 struct ubifs_scan_leb
*sleb
;
875 struct ubifs_scan_node
*snod
;
878 if (dbg_is_tst_rcvry(c
))
881 printk(KERN_DEBUG
"(pid %d) start dumping LEB %d\n",
884 buf
= __vmalloc(c
->leb_size
, GFP_NOFS
, PAGE_KERNEL
);
886 ubifs_err("cannot allocate memory for dumping LEB %d", lnum
);
890 sleb
= ubifs_scan(c
, lnum
, 0, buf
, 0);
892 ubifs_err("scan error %d", (int)PTR_ERR(sleb
));
896 printk(KERN_DEBUG
"LEB %d has %d nodes ending at %d\n", lnum
,
897 sleb
->nodes_cnt
, sleb
->endpt
);
899 list_for_each_entry(snod
, &sleb
->nodes
, list
) {
901 printk(KERN_DEBUG
"Dumping node at LEB %d:%d len %d\n", lnum
,
902 snod
->offs
, snod
->len
);
903 dbg_dump_node(c
, snod
->node
);
906 printk(KERN_DEBUG
"(pid %d) finish dumping LEB %d\n",
908 ubifs_scan_destroy(sleb
);
915 void dbg_dump_znode(const struct ubifs_info
*c
,
916 const struct ubifs_znode
*znode
)
919 const struct ubifs_zbranch
*zbr
;
921 spin_lock(&dbg_lock
);
923 zbr
= &znode
->parent
->zbranch
[znode
->iip
];
927 printk(KERN_DEBUG
"znode %p, LEB %d:%d len %d parent %p iip %d level %d"
928 " child_cnt %d flags %lx\n", znode
, zbr
->lnum
, zbr
->offs
,
929 zbr
->len
, znode
->parent
, znode
->iip
, znode
->level
,
930 znode
->child_cnt
, znode
->flags
);
932 if (znode
->child_cnt
<= 0 || znode
->child_cnt
> c
->fanout
) {
933 spin_unlock(&dbg_lock
);
937 printk(KERN_DEBUG
"zbranches:\n");
938 for (n
= 0; n
< znode
->child_cnt
; n
++) {
939 zbr
= &znode
->zbranch
[n
];
940 if (znode
->level
> 0)
941 printk(KERN_DEBUG
"\t%d: znode %p LEB %d:%d len %d key "
942 "%s\n", n
, zbr
->znode
, zbr
->lnum
,
946 printk(KERN_DEBUG
"\t%d: LNC %p LEB %d:%d len %d key "
947 "%s\n", n
, zbr
->znode
, zbr
->lnum
,
951 spin_unlock(&dbg_lock
);
954 void dbg_dump_heap(struct ubifs_info
*c
, struct ubifs_lpt_heap
*heap
, int cat
)
958 printk(KERN_DEBUG
"(pid %d) start dumping heap cat %d (%d elements)\n",
959 current
->pid
, cat
, heap
->cnt
);
960 for (i
= 0; i
< heap
->cnt
; i
++) {
961 struct ubifs_lprops
*lprops
= heap
->arr
[i
];
963 printk(KERN_DEBUG
"\t%d. LEB %d hpos %d free %d dirty %d "
964 "flags %d\n", i
, lprops
->lnum
, lprops
->hpos
,
965 lprops
->free
, lprops
->dirty
, lprops
->flags
);
967 printk(KERN_DEBUG
"(pid %d) finish dumping heap\n", current
->pid
);
970 void dbg_dump_pnode(struct ubifs_info
*c
, struct ubifs_pnode
*pnode
,
971 struct ubifs_nnode
*parent
, int iip
)
975 printk(KERN_DEBUG
"(pid %d) dumping pnode:\n", current
->pid
);
976 printk(KERN_DEBUG
"\taddress %zx parent %zx cnext %zx\n",
977 (size_t)pnode
, (size_t)parent
, (size_t)pnode
->cnext
);
978 printk(KERN_DEBUG
"\tflags %lu iip %d level %d num %d\n",
979 pnode
->flags
, iip
, pnode
->level
, pnode
->num
);
980 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
981 struct ubifs_lprops
*lp
= &pnode
->lprops
[i
];
983 printk(KERN_DEBUG
"\t%d: free %d dirty %d flags %d lnum %d\n",
984 i
, lp
->free
, lp
->dirty
, lp
->flags
, lp
->lnum
);
988 void dbg_dump_tnc(struct ubifs_info
*c
)
990 struct ubifs_znode
*znode
;
993 printk(KERN_DEBUG
"\n");
994 printk(KERN_DEBUG
"(pid %d) start dumping TNC tree\n", current
->pid
);
995 znode
= ubifs_tnc_levelorder_next(c
->zroot
.znode
, NULL
);
996 level
= znode
->level
;
997 printk(KERN_DEBUG
"== Level %d ==\n", level
);
999 if (level
!= znode
->level
) {
1000 level
= znode
->level
;
1001 printk(KERN_DEBUG
"== Level %d ==\n", level
);
1003 dbg_dump_znode(c
, znode
);
1004 znode
= ubifs_tnc_levelorder_next(c
->zroot
.znode
, znode
);
1006 printk(KERN_DEBUG
"(pid %d) finish dumping TNC tree\n", current
->pid
);
1009 static int dump_znode(struct ubifs_info
*c
, struct ubifs_znode
*znode
,
1012 dbg_dump_znode(c
, znode
);
1017 * dbg_dump_index - dump the on-flash index.
1018 * @c: UBIFS file-system description object
1020 * This function dumps whole UBIFS indexing B-tree, unlike 'dbg_dump_tnc()'
1021 * which dumps only in-memory znodes and does not read znodes which from flash.
1023 void dbg_dump_index(struct ubifs_info
*c
)
1025 dbg_walk_index(c
, NULL
, dump_znode
, NULL
);
1029 * dbg_save_space_info - save information about flash space.
1030 * @c: UBIFS file-system description object
1032 * This function saves information about UBIFS free space, dirty space, etc, in
1033 * order to check it later.
1035 void dbg_save_space_info(struct ubifs_info
*c
)
1037 struct ubifs_debug_info
*d
= c
->dbg
;
1040 spin_lock(&c
->space_lock
);
1041 memcpy(&d
->saved_lst
, &c
->lst
, sizeof(struct ubifs_lp_stats
));
1042 memcpy(&d
->saved_bi
, &c
->bi
, sizeof(struct ubifs_budg_info
));
1043 d
->saved_idx_gc_cnt
= c
->idx_gc_cnt
;
1046 * We use a dirty hack here and zero out @c->freeable_cnt, because it
1047 * affects the free space calculations, and UBIFS might not know about
1048 * all freeable eraseblocks. Indeed, we know about freeable eraseblocks
1049 * only when we read their lprops, and we do this only lazily, upon the
1050 * need. So at any given point of time @c->freeable_cnt might be not
1053 * Just one example about the issue we hit when we did not zero
1055 * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the
1056 * amount of free space in @d->saved_free
1057 * 2. We re-mount R/W, which makes UBIFS to read the "lsave"
1058 * information from flash, where we cache LEBs from various
1059 * categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()'
1060 * -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()'
1061 * -> 'ubifs_get_pnode()' -> 'update_cats()'
1062 * -> 'ubifs_add_to_cat()').
1063 * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt
1065 * 4. We calculate the amount of free space when the re-mount is
1066 * finished in 'dbg_check_space_info()' and it does not match
1069 freeable_cnt
= c
->freeable_cnt
;
1070 c
->freeable_cnt
= 0;
1071 d
->saved_free
= ubifs_get_free_space_nolock(c
);
1072 c
->freeable_cnt
= freeable_cnt
;
1073 spin_unlock(&c
->space_lock
);
1077 * dbg_check_space_info - check flash space information.
1078 * @c: UBIFS file-system description object
1080 * This function compares current flash space information with the information
1081 * which was saved when the 'dbg_save_space_info()' function was called.
1082 * Returns zero if the information has not changed, and %-EINVAL it it has
1085 int dbg_check_space_info(struct ubifs_info
*c
)
1087 struct ubifs_debug_info
*d
= c
->dbg
;
1088 struct ubifs_lp_stats lst
;
1092 spin_lock(&c
->space_lock
);
1093 freeable_cnt
= c
->freeable_cnt
;
1094 c
->freeable_cnt
= 0;
1095 free
= ubifs_get_free_space_nolock(c
);
1096 c
->freeable_cnt
= freeable_cnt
;
1097 spin_unlock(&c
->space_lock
);
1099 if (free
!= d
->saved_free
) {
1100 ubifs_err("free space changed from %lld to %lld",
1101 d
->saved_free
, free
);
1108 ubifs_msg("saved lprops statistics dump");
1109 dbg_dump_lstats(&d
->saved_lst
);
1110 ubifs_msg("saved budgeting info dump");
1111 dbg_dump_budg(c
, &d
->saved_bi
);
1112 ubifs_msg("saved idx_gc_cnt %d", d
->saved_idx_gc_cnt
);
1113 ubifs_msg("current lprops statistics dump");
1114 ubifs_get_lp_stats(c
, &lst
);
1115 dbg_dump_lstats(&lst
);
1116 ubifs_msg("current budgeting info dump");
1117 dbg_dump_budg(c
, &c
->bi
);
1123 * dbg_check_synced_i_size - check synchronized inode size.
1124 * @c: UBIFS file-system description object
1125 * @inode: inode to check
1127 * If inode is clean, synchronized inode size has to be equivalent to current
1128 * inode size. This function has to be called only for locked inodes (@i_mutex
1129 * has to be locked). Returns %0 if synchronized inode size if correct, and
1132 int dbg_check_synced_i_size(const struct ubifs_info
*c
, struct inode
*inode
)
1135 struct ubifs_inode
*ui
= ubifs_inode(inode
);
1137 if (!dbg_is_chk_gen(c
))
1139 if (!S_ISREG(inode
->i_mode
))
1142 mutex_lock(&ui
->ui_mutex
);
1143 spin_lock(&ui
->ui_lock
);
1144 if (ui
->ui_size
!= ui
->synced_i_size
&& !ui
->dirty
) {
1145 ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode "
1146 "is clean", ui
->ui_size
, ui
->synced_i_size
);
1147 ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode
->i_ino
,
1148 inode
->i_mode
, i_size_read(inode
));
1152 spin_unlock(&ui
->ui_lock
);
1153 mutex_unlock(&ui
->ui_mutex
);
1158 * dbg_check_dir - check directory inode size and link count.
1159 * @c: UBIFS file-system description object
1160 * @dir: the directory to calculate size for
1161 * @size: the result is returned here
1163 * This function makes sure that directory size and link count are correct.
1164 * Returns zero in case of success and a negative error code in case of
1167 * Note, it is good idea to make sure the @dir->i_mutex is locked before
1168 * calling this function.
1170 int dbg_check_dir(struct ubifs_info
*c
, const struct inode
*dir
)
1172 unsigned int nlink
= 2;
1173 union ubifs_key key
;
1174 struct ubifs_dent_node
*dent
, *pdent
= NULL
;
1175 struct qstr nm
= { .name
= NULL
};
1176 loff_t size
= UBIFS_INO_NODE_SZ
;
1178 if (!dbg_is_chk_gen(c
))
1181 if (!S_ISDIR(dir
->i_mode
))
1184 lowest_dent_key(c
, &key
, dir
->i_ino
);
1188 dent
= ubifs_tnc_next_ent(c
, &key
, &nm
);
1190 err
= PTR_ERR(dent
);
1196 nm
.name
= dent
->name
;
1197 nm
.len
= le16_to_cpu(dent
->nlen
);
1198 size
+= CALC_DENT_SIZE(nm
.len
);
1199 if (dent
->type
== UBIFS_ITYPE_DIR
)
1203 key_read(c
, &dent
->key
, &key
);
1207 if (i_size_read(dir
) != size
) {
1208 ubifs_err("directory inode %lu has size %llu, "
1209 "but calculated size is %llu", dir
->i_ino
,
1210 (unsigned long long)i_size_read(dir
),
1211 (unsigned long long)size
);
1212 dbg_dump_inode(c
, dir
);
1216 if (dir
->i_nlink
!= nlink
) {
1217 ubifs_err("directory inode %lu has nlink %u, but calculated "
1218 "nlink is %u", dir
->i_ino
, dir
->i_nlink
, nlink
);
1219 dbg_dump_inode(c
, dir
);
1228 * dbg_check_key_order - make sure that colliding keys are properly ordered.
1229 * @c: UBIFS file-system description object
1230 * @zbr1: first zbranch
1231 * @zbr2: following zbranch
1233 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
1234 * names of the direntries/xentries which are referred by the keys. This
1235 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
1236 * sure the name of direntry/xentry referred by @zbr1 is less than
1237 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
1238 * and a negative error code in case of failure.
1240 static int dbg_check_key_order(struct ubifs_info
*c
, struct ubifs_zbranch
*zbr1
,
1241 struct ubifs_zbranch
*zbr2
)
1243 int err
, nlen1
, nlen2
, cmp
;
1244 struct ubifs_dent_node
*dent1
, *dent2
;
1245 union ubifs_key key
;
1247 ubifs_assert(!keys_cmp(c
, &zbr1
->key
, &zbr2
->key
));
1248 dent1
= kmalloc(UBIFS_MAX_DENT_NODE_SZ
, GFP_NOFS
);
1251 dent2
= kmalloc(UBIFS_MAX_DENT_NODE_SZ
, GFP_NOFS
);
1257 err
= ubifs_tnc_read_node(c
, zbr1
, dent1
);
1260 err
= ubifs_validate_entry(c
, dent1
);
1264 err
= ubifs_tnc_read_node(c
, zbr2
, dent2
);
1267 err
= ubifs_validate_entry(c
, dent2
);
1271 /* Make sure node keys are the same as in zbranch */
1273 key_read(c
, &dent1
->key
, &key
);
1274 if (keys_cmp(c
, &zbr1
->key
, &key
)) {
1275 dbg_err("1st entry at %d:%d has key %s", zbr1
->lnum
,
1276 zbr1
->offs
, DBGKEY(&key
));
1277 dbg_err("but it should have key %s according to tnc",
1278 DBGKEY(&zbr1
->key
));
1279 dbg_dump_node(c
, dent1
);
1283 key_read(c
, &dent2
->key
, &key
);
1284 if (keys_cmp(c
, &zbr2
->key
, &key
)) {
1285 dbg_err("2nd entry at %d:%d has key %s", zbr1
->lnum
,
1286 zbr1
->offs
, DBGKEY(&key
));
1287 dbg_err("but it should have key %s according to tnc",
1288 DBGKEY(&zbr2
->key
));
1289 dbg_dump_node(c
, dent2
);
1293 nlen1
= le16_to_cpu(dent1
->nlen
);
1294 nlen2
= le16_to_cpu(dent2
->nlen
);
1296 cmp
= memcmp(dent1
->name
, dent2
->name
, min_t(int, nlen1
, nlen2
));
1297 if (cmp
< 0 || (cmp
== 0 && nlen1
< nlen2
)) {
1301 if (cmp
== 0 && nlen1
== nlen2
)
1302 dbg_err("2 xent/dent nodes with the same name");
1304 dbg_err("bad order of colliding key %s",
1307 ubifs_msg("first node at %d:%d\n", zbr1
->lnum
, zbr1
->offs
);
1308 dbg_dump_node(c
, dent1
);
1309 ubifs_msg("second node at %d:%d\n", zbr2
->lnum
, zbr2
->offs
);
1310 dbg_dump_node(c
, dent2
);
1319 * dbg_check_znode - check if znode is all right.
1320 * @c: UBIFS file-system description object
1321 * @zbr: zbranch which points to this znode
1323 * This function makes sure that znode referred to by @zbr is all right.
1324 * Returns zero if it is, and %-EINVAL if it is not.
1326 static int dbg_check_znode(struct ubifs_info
*c
, struct ubifs_zbranch
*zbr
)
1328 struct ubifs_znode
*znode
= zbr
->znode
;
1329 struct ubifs_znode
*zp
= znode
->parent
;
1332 if (znode
->child_cnt
<= 0 || znode
->child_cnt
> c
->fanout
) {
1336 if (znode
->level
< 0) {
1340 if (znode
->iip
< 0 || znode
->iip
>= c
->fanout
) {
1346 /* Only dirty zbranch may have no on-flash nodes */
1347 if (!ubifs_zn_dirty(znode
)) {
1352 if (ubifs_zn_dirty(znode
)) {
1354 * If znode is dirty, its parent has to be dirty as well. The
1355 * order of the operation is important, so we have to have
1359 if (zp
&& !ubifs_zn_dirty(zp
)) {
1361 * The dirty flag is atomic and is cleared outside the
1362 * TNC mutex, so znode's dirty flag may now have
1363 * been cleared. The child is always cleared before the
1364 * parent, so we just need to check again.
1367 if (ubifs_zn_dirty(znode
)) {
1375 const union ubifs_key
*min
, *max
;
1377 if (znode
->level
!= zp
->level
- 1) {
1382 /* Make sure the 'parent' pointer in our znode is correct */
1383 err
= ubifs_search_zbranch(c
, zp
, &zbr
->key
, &n
);
1385 /* This zbranch does not exist in the parent */
1390 if (znode
->iip
>= zp
->child_cnt
) {
1395 if (znode
->iip
!= n
) {
1396 /* This may happen only in case of collisions */
1397 if (keys_cmp(c
, &zp
->zbranch
[n
].key
,
1398 &zp
->zbranch
[znode
->iip
].key
)) {
1406 * Make sure that the first key in our znode is greater than or
1407 * equal to the key in the pointing zbranch.
1410 cmp
= keys_cmp(c
, min
, &znode
->zbranch
[0].key
);
1416 if (n
+ 1 < zp
->child_cnt
) {
1417 max
= &zp
->zbranch
[n
+ 1].key
;
1420 * Make sure the last key in our znode is less or
1421 * equivalent than the key in the zbranch which goes
1422 * after our pointing zbranch.
1424 cmp
= keys_cmp(c
, max
,
1425 &znode
->zbranch
[znode
->child_cnt
- 1].key
);
1432 /* This may only be root znode */
1433 if (zbr
!= &c
->zroot
) {
1440 * Make sure that next key is greater or equivalent then the previous
1443 for (n
= 1; n
< znode
->child_cnt
; n
++) {
1444 cmp
= keys_cmp(c
, &znode
->zbranch
[n
- 1].key
,
1445 &znode
->zbranch
[n
].key
);
1451 /* This can only be keys with colliding hash */
1452 if (!is_hash_key(c
, &znode
->zbranch
[n
].key
)) {
1457 if (znode
->level
!= 0 || c
->replaying
)
1461 * Colliding keys should follow binary order of
1462 * corresponding xentry/dentry names.
1464 err
= dbg_check_key_order(c
, &znode
->zbranch
[n
- 1],
1465 &znode
->zbranch
[n
]);
1475 for (n
= 0; n
< znode
->child_cnt
; n
++) {
1476 if (!znode
->zbranch
[n
].znode
&&
1477 (znode
->zbranch
[n
].lnum
== 0 ||
1478 znode
->zbranch
[n
].len
== 0)) {
1483 if (znode
->zbranch
[n
].lnum
!= 0 &&
1484 znode
->zbranch
[n
].len
== 0) {
1489 if (znode
->zbranch
[n
].lnum
== 0 &&
1490 znode
->zbranch
[n
].len
!= 0) {
1495 if (znode
->zbranch
[n
].lnum
== 0 &&
1496 znode
->zbranch
[n
].offs
!= 0) {
1501 if (znode
->level
!= 0 && znode
->zbranch
[n
].znode
)
1502 if (znode
->zbranch
[n
].znode
->parent
!= znode
) {
1511 ubifs_err("failed, error %d", err
);
1512 ubifs_msg("dump of the znode");
1513 dbg_dump_znode(c
, znode
);
1515 ubifs_msg("dump of the parent znode");
1516 dbg_dump_znode(c
, zp
);
1523 * dbg_check_tnc - check TNC tree.
1524 * @c: UBIFS file-system description object
1525 * @extra: do extra checks that are possible at start commit
1527 * This function traverses whole TNC tree and checks every znode. Returns zero
1528 * if everything is all right and %-EINVAL if something is wrong with TNC.
1530 int dbg_check_tnc(struct ubifs_info
*c
, int extra
)
1532 struct ubifs_znode
*znode
;
1533 long clean_cnt
= 0, dirty_cnt
= 0;
1536 if (!dbg_is_chk_index(c
))
1539 ubifs_assert(mutex_is_locked(&c
->tnc_mutex
));
1540 if (!c
->zroot
.znode
)
1543 znode
= ubifs_tnc_postorder_first(c
->zroot
.znode
);
1545 struct ubifs_znode
*prev
;
1546 struct ubifs_zbranch
*zbr
;
1551 zbr
= &znode
->parent
->zbranch
[znode
->iip
];
1553 err
= dbg_check_znode(c
, zbr
);
1558 if (ubifs_zn_dirty(znode
))
1565 znode
= ubifs_tnc_postorder_next(znode
);
1570 * If the last key of this znode is equivalent to the first key
1571 * of the next znode (collision), then check order of the keys.
1573 last
= prev
->child_cnt
- 1;
1574 if (prev
->level
== 0 && znode
->level
== 0 && !c
->replaying
&&
1575 !keys_cmp(c
, &prev
->zbranch
[last
].key
,
1576 &znode
->zbranch
[0].key
)) {
1577 err
= dbg_check_key_order(c
, &prev
->zbranch
[last
],
1578 &znode
->zbranch
[0]);
1582 ubifs_msg("first znode");
1583 dbg_dump_znode(c
, prev
);
1584 ubifs_msg("second znode");
1585 dbg_dump_znode(c
, znode
);
1592 if (clean_cnt
!= atomic_long_read(&c
->clean_zn_cnt
)) {
1593 ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld",
1594 atomic_long_read(&c
->clean_zn_cnt
),
1598 if (dirty_cnt
!= atomic_long_read(&c
->dirty_zn_cnt
)) {
1599 ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld",
1600 atomic_long_read(&c
->dirty_zn_cnt
),
1610 * dbg_walk_index - walk the on-flash index.
1611 * @c: UBIFS file-system description object
1612 * @leaf_cb: called for each leaf node
1613 * @znode_cb: called for each indexing node
1614 * @priv: private data which is passed to callbacks
1616 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1617 * node and @znode_cb for each indexing node. Returns zero in case of success
1618 * and a negative error code in case of failure.
1620 * It would be better if this function removed every znode it pulled to into
1621 * the TNC, so that the behavior more closely matched the non-debugging
1624 int dbg_walk_index(struct ubifs_info
*c
, dbg_leaf_callback leaf_cb
,
1625 dbg_znode_callback znode_cb
, void *priv
)
1628 struct ubifs_zbranch
*zbr
;
1629 struct ubifs_znode
*znode
, *child
;
1631 mutex_lock(&c
->tnc_mutex
);
1632 /* If the root indexing node is not in TNC - pull it */
1633 if (!c
->zroot
.znode
) {
1634 c
->zroot
.znode
= ubifs_load_znode(c
, &c
->zroot
, NULL
, 0);
1635 if (IS_ERR(c
->zroot
.znode
)) {
1636 err
= PTR_ERR(c
->zroot
.znode
);
1637 c
->zroot
.znode
= NULL
;
1643 * We are going to traverse the indexing tree in the postorder manner.
1644 * Go down and find the leftmost indexing node where we are going to
1647 znode
= c
->zroot
.znode
;
1648 while (znode
->level
> 0) {
1649 zbr
= &znode
->zbranch
[0];
1652 child
= ubifs_load_znode(c
, zbr
, znode
, 0);
1653 if (IS_ERR(child
)) {
1654 err
= PTR_ERR(child
);
1663 /* Iterate over all indexing nodes */
1670 err
= znode_cb(c
, znode
, priv
);
1672 ubifs_err("znode checking function returned "
1674 dbg_dump_znode(c
, znode
);
1678 if (leaf_cb
&& znode
->level
== 0) {
1679 for (idx
= 0; idx
< znode
->child_cnt
; idx
++) {
1680 zbr
= &znode
->zbranch
[idx
];
1681 err
= leaf_cb(c
, zbr
, priv
);
1683 ubifs_err("leaf checking function "
1684 "returned error %d, for leaf "
1686 err
, zbr
->lnum
, zbr
->offs
);
1695 idx
= znode
->iip
+ 1;
1696 znode
= znode
->parent
;
1697 if (idx
< znode
->child_cnt
) {
1698 /* Switch to the next index in the parent */
1699 zbr
= &znode
->zbranch
[idx
];
1702 child
= ubifs_load_znode(c
, zbr
, znode
, idx
);
1703 if (IS_ERR(child
)) {
1704 err
= PTR_ERR(child
);
1712 * This is the last child, switch to the parent and
1717 /* Go to the lowest leftmost znode in the new sub-tree */
1718 while (znode
->level
> 0) {
1719 zbr
= &znode
->zbranch
[0];
1722 child
= ubifs_load_znode(c
, zbr
, znode
, 0);
1723 if (IS_ERR(child
)) {
1724 err
= PTR_ERR(child
);
1733 mutex_unlock(&c
->tnc_mutex
);
1738 zbr
= &znode
->parent
->zbranch
[znode
->iip
];
1741 ubifs_msg("dump of znode at LEB %d:%d", zbr
->lnum
, zbr
->offs
);
1742 dbg_dump_znode(c
, znode
);
1744 mutex_unlock(&c
->tnc_mutex
);
1749 * add_size - add znode size to partially calculated index size.
1750 * @c: UBIFS file-system description object
1751 * @znode: znode to add size for
1752 * @priv: partially calculated index size
1754 * This is a helper function for 'dbg_check_idx_size()' which is called for
1755 * every indexing node and adds its size to the 'long long' variable pointed to
1758 static int add_size(struct ubifs_info
*c
, struct ubifs_znode
*znode
, void *priv
)
1760 long long *idx_size
= priv
;
1763 add
= ubifs_idx_node_sz(c
, znode
->child_cnt
);
1764 add
= ALIGN(add
, 8);
1770 * dbg_check_idx_size - check index size.
1771 * @c: UBIFS file-system description object
1772 * @idx_size: size to check
1774 * This function walks the UBIFS index, calculates its size and checks that the
1775 * size is equivalent to @idx_size. Returns zero in case of success and a
1776 * negative error code in case of failure.
1778 int dbg_check_idx_size(struct ubifs_info
*c
, long long idx_size
)
1783 if (!dbg_is_chk_index(c
))
1786 err
= dbg_walk_index(c
, NULL
, add_size
, &calc
);
1788 ubifs_err("error %d while walking the index", err
);
1792 if (calc
!= idx_size
) {
1793 ubifs_err("index size check failed: calculated size is %lld, "
1794 "should be %lld", calc
, idx_size
);
1803 * struct fsck_inode - information about an inode used when checking the file-system.
1804 * @rb: link in the RB-tree of inodes
1805 * @inum: inode number
1806 * @mode: inode type, permissions, etc
1807 * @nlink: inode link count
1808 * @xattr_cnt: count of extended attributes
1809 * @references: how many directory/xattr entries refer this inode (calculated
1810 * while walking the index)
1811 * @calc_cnt: for directory inode count of child directories
1812 * @size: inode size (read from on-flash inode)
1813 * @xattr_sz: summary size of all extended attributes (read from on-flash
1815 * @calc_sz: for directories calculated directory size
1816 * @calc_xcnt: count of extended attributes
1817 * @calc_xsz: calculated summary size of all extended attributes
1818 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1819 * inode (read from on-flash inode)
1820 * @calc_xnms: calculated sum of lengths of all extended attribute names
1827 unsigned int xattr_cnt
;
1831 unsigned int xattr_sz
;
1833 long long calc_xcnt
;
1835 unsigned int xattr_nms
;
1836 long long calc_xnms
;
1840 * struct fsck_data - private FS checking information.
1841 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1844 struct rb_root inodes
;
1848 * add_inode - add inode information to RB-tree of inodes.
1849 * @c: UBIFS file-system description object
1850 * @fsckd: FS checking information
1851 * @ino: raw UBIFS inode to add
1853 * This is a helper function for 'check_leaf()' which adds information about
1854 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1855 * case of success and a negative error code in case of failure.
1857 static struct fsck_inode
*add_inode(struct ubifs_info
*c
,
1858 struct fsck_data
*fsckd
,
1859 struct ubifs_ino_node
*ino
)
1861 struct rb_node
**p
, *parent
= NULL
;
1862 struct fsck_inode
*fscki
;
1863 ino_t inum
= key_inum_flash(c
, &ino
->key
);
1864 struct inode
*inode
;
1865 struct ubifs_inode
*ui
;
1867 p
= &fsckd
->inodes
.rb_node
;
1870 fscki
= rb_entry(parent
, struct fsck_inode
, rb
);
1871 if (inum
< fscki
->inum
)
1873 else if (inum
> fscki
->inum
)
1874 p
= &(*p
)->rb_right
;
1879 if (inum
> c
->highest_inum
) {
1880 ubifs_err("too high inode number, max. is %lu",
1881 (unsigned long)c
->highest_inum
);
1882 return ERR_PTR(-EINVAL
);
1885 fscki
= kzalloc(sizeof(struct fsck_inode
), GFP_NOFS
);
1887 return ERR_PTR(-ENOMEM
);
1889 inode
= ilookup(c
->vfs_sb
, inum
);
1893 * If the inode is present in the VFS inode cache, use it instead of
1894 * the on-flash inode which might be out-of-date. E.g., the size might
1895 * be out-of-date. If we do not do this, the following may happen, for
1897 * 1. A power cut happens
1898 * 2. We mount the file-system R/O, the replay process fixes up the
1899 * inode size in the VFS cache, but on on-flash.
1900 * 3. 'check_leaf()' fails because it hits a data node beyond inode
1904 fscki
->nlink
= le32_to_cpu(ino
->nlink
);
1905 fscki
->size
= le64_to_cpu(ino
->size
);
1906 fscki
->xattr_cnt
= le32_to_cpu(ino
->xattr_cnt
);
1907 fscki
->xattr_sz
= le32_to_cpu(ino
->xattr_size
);
1908 fscki
->xattr_nms
= le32_to_cpu(ino
->xattr_names
);
1909 fscki
->mode
= le32_to_cpu(ino
->mode
);
1911 ui
= ubifs_inode(inode
);
1912 fscki
->nlink
= inode
->i_nlink
;
1913 fscki
->size
= inode
->i_size
;
1914 fscki
->xattr_cnt
= ui
->xattr_cnt
;
1915 fscki
->xattr_sz
= ui
->xattr_size
;
1916 fscki
->xattr_nms
= ui
->xattr_names
;
1917 fscki
->mode
= inode
->i_mode
;
1921 if (S_ISDIR(fscki
->mode
)) {
1922 fscki
->calc_sz
= UBIFS_INO_NODE_SZ
;
1923 fscki
->calc_cnt
= 2;
1926 rb_link_node(&fscki
->rb
, parent
, p
);
1927 rb_insert_color(&fscki
->rb
, &fsckd
->inodes
);
1933 * search_inode - search inode in the RB-tree of inodes.
1934 * @fsckd: FS checking information
1935 * @inum: inode number to search
1937 * This is a helper function for 'check_leaf()' which searches inode @inum in
1938 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1939 * the inode was not found.
1941 static struct fsck_inode
*search_inode(struct fsck_data
*fsckd
, ino_t inum
)
1944 struct fsck_inode
*fscki
;
1946 p
= fsckd
->inodes
.rb_node
;
1948 fscki
= rb_entry(p
, struct fsck_inode
, rb
);
1949 if (inum
< fscki
->inum
)
1951 else if (inum
> fscki
->inum
)
1960 * read_add_inode - read inode node and add it to RB-tree of inodes.
1961 * @c: UBIFS file-system description object
1962 * @fsckd: FS checking information
1963 * @inum: inode number to read
1965 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1966 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1967 * information pointer in case of success and a negative error code in case of
1970 static struct fsck_inode
*read_add_inode(struct ubifs_info
*c
,
1971 struct fsck_data
*fsckd
, ino_t inum
)
1974 union ubifs_key key
;
1975 struct ubifs_znode
*znode
;
1976 struct ubifs_zbranch
*zbr
;
1977 struct ubifs_ino_node
*ino
;
1978 struct fsck_inode
*fscki
;
1980 fscki
= search_inode(fsckd
, inum
);
1984 ino_key_init(c
, &key
, inum
);
1985 err
= ubifs_lookup_level0(c
, &key
, &znode
, &n
);
1987 ubifs_err("inode %lu not found in index", (unsigned long)inum
);
1988 return ERR_PTR(-ENOENT
);
1989 } else if (err
< 0) {
1990 ubifs_err("error %d while looking up inode %lu",
1991 err
, (unsigned long)inum
);
1992 return ERR_PTR(err
);
1995 zbr
= &znode
->zbranch
[n
];
1996 if (zbr
->len
< UBIFS_INO_NODE_SZ
) {
1997 ubifs_err("bad node %lu node length %d",
1998 (unsigned long)inum
, zbr
->len
);
1999 return ERR_PTR(-EINVAL
);
2002 ino
= kmalloc(zbr
->len
, GFP_NOFS
);
2004 return ERR_PTR(-ENOMEM
);
2006 err
= ubifs_tnc_read_node(c
, zbr
, ino
);
2008 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2009 zbr
->lnum
, zbr
->offs
, err
);
2011 return ERR_PTR(err
);
2014 fscki
= add_inode(c
, fsckd
, ino
);
2016 if (IS_ERR(fscki
)) {
2017 ubifs_err("error %ld while adding inode %lu node",
2018 PTR_ERR(fscki
), (unsigned long)inum
);
2026 * check_leaf - check leaf node.
2027 * @c: UBIFS file-system description object
2028 * @zbr: zbranch of the leaf node to check
2029 * @priv: FS checking information
2031 * This is a helper function for 'dbg_check_filesystem()' which is called for
2032 * every single leaf node while walking the indexing tree. It checks that the
2033 * leaf node referred from the indexing tree exists, has correct CRC, and does
2034 * some other basic validation. This function is also responsible for building
2035 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
2036 * calculates reference count, size, etc for each inode in order to later
2037 * compare them to the information stored inside the inodes and detect possible
2038 * inconsistencies. Returns zero in case of success and a negative error code
2039 * in case of failure.
2041 static int check_leaf(struct ubifs_info
*c
, struct ubifs_zbranch
*zbr
,
2046 struct ubifs_ch
*ch
;
2047 int err
, type
= key_type(c
, &zbr
->key
);
2048 struct fsck_inode
*fscki
;
2050 if (zbr
->len
< UBIFS_CH_SZ
) {
2051 ubifs_err("bad leaf length %d (LEB %d:%d)",
2052 zbr
->len
, zbr
->lnum
, zbr
->offs
);
2056 node
= kmalloc(zbr
->len
, GFP_NOFS
);
2060 err
= ubifs_tnc_read_node(c
, zbr
, node
);
2062 ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
2063 zbr
->lnum
, zbr
->offs
, err
);
2067 /* If this is an inode node, add it to RB-tree of inodes */
2068 if (type
== UBIFS_INO_KEY
) {
2069 fscki
= add_inode(c
, priv
, node
);
2070 if (IS_ERR(fscki
)) {
2071 err
= PTR_ERR(fscki
);
2072 ubifs_err("error %d while adding inode node", err
);
2078 if (type
!= UBIFS_DENT_KEY
&& type
!= UBIFS_XENT_KEY
&&
2079 type
!= UBIFS_DATA_KEY
) {
2080 ubifs_err("unexpected node type %d at LEB %d:%d",
2081 type
, zbr
->lnum
, zbr
->offs
);
2087 if (le64_to_cpu(ch
->sqnum
) > c
->max_sqnum
) {
2088 ubifs_err("too high sequence number, max. is %llu",
2094 if (type
== UBIFS_DATA_KEY
) {
2096 struct ubifs_data_node
*dn
= node
;
2099 * Search the inode node this data node belongs to and insert
2100 * it to the RB-tree of inodes.
2102 inum
= key_inum_flash(c
, &dn
->key
);
2103 fscki
= read_add_inode(c
, priv
, inum
);
2104 if (IS_ERR(fscki
)) {
2105 err
= PTR_ERR(fscki
);
2106 ubifs_err("error %d while processing data node and "
2107 "trying to find inode node %lu",
2108 err
, (unsigned long)inum
);
2112 /* Make sure the data node is within inode size */
2113 blk_offs
= key_block_flash(c
, &dn
->key
);
2114 blk_offs
<<= UBIFS_BLOCK_SHIFT
;
2115 blk_offs
+= le32_to_cpu(dn
->size
);
2116 if (blk_offs
> fscki
->size
) {
2117 ubifs_err("data node at LEB %d:%d is not within inode "
2118 "size %lld", zbr
->lnum
, zbr
->offs
,
2125 struct ubifs_dent_node
*dent
= node
;
2126 struct fsck_inode
*fscki1
;
2128 err
= ubifs_validate_entry(c
, dent
);
2133 * Search the inode node this entry refers to and the parent
2134 * inode node and insert them to the RB-tree of inodes.
2136 inum
= le64_to_cpu(dent
->inum
);
2137 fscki
= read_add_inode(c
, priv
, inum
);
2138 if (IS_ERR(fscki
)) {
2139 err
= PTR_ERR(fscki
);
2140 ubifs_err("error %d while processing entry node and "
2141 "trying to find inode node %lu",
2142 err
, (unsigned long)inum
);
2146 /* Count how many direntries or xentries refers this inode */
2147 fscki
->references
+= 1;
2149 inum
= key_inum_flash(c
, &dent
->key
);
2150 fscki1
= read_add_inode(c
, priv
, inum
);
2151 if (IS_ERR(fscki1
)) {
2152 err
= PTR_ERR(fscki1
);
2153 ubifs_err("error %d while processing entry node and "
2154 "trying to find parent inode node %lu",
2155 err
, (unsigned long)inum
);
2159 nlen
= le16_to_cpu(dent
->nlen
);
2160 if (type
== UBIFS_XENT_KEY
) {
2161 fscki1
->calc_xcnt
+= 1;
2162 fscki1
->calc_xsz
+= CALC_DENT_SIZE(nlen
);
2163 fscki1
->calc_xsz
+= CALC_XATTR_BYTES(fscki
->size
);
2164 fscki1
->calc_xnms
+= nlen
;
2166 fscki1
->calc_sz
+= CALC_DENT_SIZE(nlen
);
2167 if (dent
->type
== UBIFS_ITYPE_DIR
)
2168 fscki1
->calc_cnt
+= 1;
2177 ubifs_msg("dump of node at LEB %d:%d", zbr
->lnum
, zbr
->offs
);
2178 dbg_dump_node(c
, node
);
2185 * free_inodes - free RB-tree of inodes.
2186 * @fsckd: FS checking information
2188 static void free_inodes(struct fsck_data
*fsckd
)
2190 struct rb_node
*this = fsckd
->inodes
.rb_node
;
2191 struct fsck_inode
*fscki
;
2195 this = this->rb_left
;
2196 else if (this->rb_right
)
2197 this = this->rb_right
;
2199 fscki
= rb_entry(this, struct fsck_inode
, rb
);
2200 this = rb_parent(this);
2202 if (this->rb_left
== &fscki
->rb
)
2203 this->rb_left
= NULL
;
2205 this->rb_right
= NULL
;
2213 * check_inodes - checks all inodes.
2214 * @c: UBIFS file-system description object
2215 * @fsckd: FS checking information
2217 * This is a helper function for 'dbg_check_filesystem()' which walks the
2218 * RB-tree of inodes after the index scan has been finished, and checks that
2219 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
2220 * %-EINVAL if not, and a negative error code in case of failure.
2222 static int check_inodes(struct ubifs_info
*c
, struct fsck_data
*fsckd
)
2225 union ubifs_key key
;
2226 struct ubifs_znode
*znode
;
2227 struct ubifs_zbranch
*zbr
;
2228 struct ubifs_ino_node
*ino
;
2229 struct fsck_inode
*fscki
;
2230 struct rb_node
*this = rb_first(&fsckd
->inodes
);
2233 fscki
= rb_entry(this, struct fsck_inode
, rb
);
2234 this = rb_next(this);
2236 if (S_ISDIR(fscki
->mode
)) {
2238 * Directories have to have exactly one reference (they
2239 * cannot have hardlinks), although root inode is an
2242 if (fscki
->inum
!= UBIFS_ROOT_INO
&&
2243 fscki
->references
!= 1) {
2244 ubifs_err("directory inode %lu has %d "
2245 "direntries which refer it, but "
2247 (unsigned long)fscki
->inum
,
2251 if (fscki
->inum
== UBIFS_ROOT_INO
&&
2252 fscki
->references
!= 0) {
2253 ubifs_err("root inode %lu has non-zero (%d) "
2254 "direntries which refer it",
2255 (unsigned long)fscki
->inum
,
2259 if (fscki
->calc_sz
!= fscki
->size
) {
2260 ubifs_err("directory inode %lu size is %lld, "
2261 "but calculated size is %lld",
2262 (unsigned long)fscki
->inum
,
2263 fscki
->size
, fscki
->calc_sz
);
2266 if (fscki
->calc_cnt
!= fscki
->nlink
) {
2267 ubifs_err("directory inode %lu nlink is %d, "
2268 "but calculated nlink is %d",
2269 (unsigned long)fscki
->inum
,
2270 fscki
->nlink
, fscki
->calc_cnt
);
2274 if (fscki
->references
!= fscki
->nlink
) {
2275 ubifs_err("inode %lu nlink is %d, but "
2276 "calculated nlink is %d",
2277 (unsigned long)fscki
->inum
,
2278 fscki
->nlink
, fscki
->references
);
2282 if (fscki
->xattr_sz
!= fscki
->calc_xsz
) {
2283 ubifs_err("inode %lu has xattr size %u, but "
2284 "calculated size is %lld",
2285 (unsigned long)fscki
->inum
, fscki
->xattr_sz
,
2289 if (fscki
->xattr_cnt
!= fscki
->calc_xcnt
) {
2290 ubifs_err("inode %lu has %u xattrs, but "
2291 "calculated count is %lld",
2292 (unsigned long)fscki
->inum
,
2293 fscki
->xattr_cnt
, fscki
->calc_xcnt
);
2296 if (fscki
->xattr_nms
!= fscki
->calc_xnms
) {
2297 ubifs_err("inode %lu has xattr names' size %u, but "
2298 "calculated names' size is %lld",
2299 (unsigned long)fscki
->inum
, fscki
->xattr_nms
,
2308 /* Read the bad inode and dump it */
2309 ino_key_init(c
, &key
, fscki
->inum
);
2310 err
= ubifs_lookup_level0(c
, &key
, &znode
, &n
);
2312 ubifs_err("inode %lu not found in index",
2313 (unsigned long)fscki
->inum
);
2315 } else if (err
< 0) {
2316 ubifs_err("error %d while looking up inode %lu",
2317 err
, (unsigned long)fscki
->inum
);
2321 zbr
= &znode
->zbranch
[n
];
2322 ino
= kmalloc(zbr
->len
, GFP_NOFS
);
2326 err
= ubifs_tnc_read_node(c
, zbr
, ino
);
2328 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2329 zbr
->lnum
, zbr
->offs
, err
);
2334 ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",
2335 (unsigned long)fscki
->inum
, zbr
->lnum
, zbr
->offs
);
2336 dbg_dump_node(c
, ino
);
2342 * dbg_check_filesystem - check the file-system.
2343 * @c: UBIFS file-system description object
2345 * This function checks the file system, namely:
2346 * o makes sure that all leaf nodes exist and their CRCs are correct;
2347 * o makes sure inode nlink, size, xattr size/count are correct (for all
2350 * The function reads whole indexing tree and all nodes, so it is pretty
2351 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2352 * not, and a negative error code in case of failure.
2354 int dbg_check_filesystem(struct ubifs_info
*c
)
2357 struct fsck_data fsckd
;
2359 if (!dbg_is_chk_fs(c
))
2362 fsckd
.inodes
= RB_ROOT
;
2363 err
= dbg_walk_index(c
, check_leaf
, NULL
, &fsckd
);
2367 err
= check_inodes(c
, &fsckd
);
2371 free_inodes(&fsckd
);
2375 ubifs_err("file-system check failed with error %d", err
);
2377 free_inodes(&fsckd
);
2382 * dbg_check_data_nodes_order - check that list of data nodes is sorted.
2383 * @c: UBIFS file-system description object
2384 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2386 * This function returns zero if the list of data nodes is sorted correctly,
2387 * and %-EINVAL if not.
2389 int dbg_check_data_nodes_order(struct ubifs_info
*c
, struct list_head
*head
)
2391 struct list_head
*cur
;
2392 struct ubifs_scan_node
*sa
, *sb
;
2394 if (!dbg_is_chk_gen(c
))
2397 for (cur
= head
->next
; cur
->next
!= head
; cur
= cur
->next
) {
2399 uint32_t blka
, blkb
;
2402 sa
= container_of(cur
, struct ubifs_scan_node
, list
);
2403 sb
= container_of(cur
->next
, struct ubifs_scan_node
, list
);
2405 if (sa
->type
!= UBIFS_DATA_NODE
) {
2406 ubifs_err("bad node type %d", sa
->type
);
2407 dbg_dump_node(c
, sa
->node
);
2410 if (sb
->type
!= UBIFS_DATA_NODE
) {
2411 ubifs_err("bad node type %d", sb
->type
);
2412 dbg_dump_node(c
, sb
->node
);
2416 inuma
= key_inum(c
, &sa
->key
);
2417 inumb
= key_inum(c
, &sb
->key
);
2421 if (inuma
> inumb
) {
2422 ubifs_err("larger inum %lu goes before inum %lu",
2423 (unsigned long)inuma
, (unsigned long)inumb
);
2427 blka
= key_block(c
, &sa
->key
);
2428 blkb
= key_block(c
, &sb
->key
);
2431 ubifs_err("larger block %u goes before %u", blka
, blkb
);
2435 ubifs_err("two data nodes for the same block");
2443 dbg_dump_node(c
, sa
->node
);
2444 dbg_dump_node(c
, sb
->node
);
2449 * dbg_check_nondata_nodes_order - check that list of data nodes is sorted.
2450 * @c: UBIFS file-system description object
2451 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2453 * This function returns zero if the list of non-data nodes is sorted correctly,
2454 * and %-EINVAL if not.
2456 int dbg_check_nondata_nodes_order(struct ubifs_info
*c
, struct list_head
*head
)
2458 struct list_head
*cur
;
2459 struct ubifs_scan_node
*sa
, *sb
;
2461 if (!dbg_is_chk_gen(c
))
2464 for (cur
= head
->next
; cur
->next
!= head
; cur
= cur
->next
) {
2466 uint32_t hasha
, hashb
;
2469 sa
= container_of(cur
, struct ubifs_scan_node
, list
);
2470 sb
= container_of(cur
->next
, struct ubifs_scan_node
, list
);
2472 if (sa
->type
!= UBIFS_INO_NODE
&& sa
->type
!= UBIFS_DENT_NODE
&&
2473 sa
->type
!= UBIFS_XENT_NODE
) {
2474 ubifs_err("bad node type %d", sa
->type
);
2475 dbg_dump_node(c
, sa
->node
);
2478 if (sa
->type
!= UBIFS_INO_NODE
&& sa
->type
!= UBIFS_DENT_NODE
&&
2479 sa
->type
!= UBIFS_XENT_NODE
) {
2480 ubifs_err("bad node type %d", sb
->type
);
2481 dbg_dump_node(c
, sb
->node
);
2485 if (sa
->type
!= UBIFS_INO_NODE
&& sb
->type
== UBIFS_INO_NODE
) {
2486 ubifs_err("non-inode node goes before inode node");
2490 if (sa
->type
== UBIFS_INO_NODE
&& sb
->type
!= UBIFS_INO_NODE
)
2493 if (sa
->type
== UBIFS_INO_NODE
&& sb
->type
== UBIFS_INO_NODE
) {
2494 /* Inode nodes are sorted in descending size order */
2495 if (sa
->len
< sb
->len
) {
2496 ubifs_err("smaller inode node goes first");
2503 * This is either a dentry or xentry, which should be sorted in
2504 * ascending (parent ino, hash) order.
2506 inuma
= key_inum(c
, &sa
->key
);
2507 inumb
= key_inum(c
, &sb
->key
);
2511 if (inuma
> inumb
) {
2512 ubifs_err("larger inum %lu goes before inum %lu",
2513 (unsigned long)inuma
, (unsigned long)inumb
);
2517 hasha
= key_block(c
, &sa
->key
);
2518 hashb
= key_block(c
, &sb
->key
);
2520 if (hasha
> hashb
) {
2521 ubifs_err("larger hash %u goes before %u",
2530 ubifs_msg("dumping first node");
2531 dbg_dump_node(c
, sa
->node
);
2532 ubifs_msg("dumping second node");
2533 dbg_dump_node(c
, sb
->node
);
2538 /* Failure mode for recovery testing */
2540 #define chance(n, d) (simple_rand() <= (n) * 32768LL / (d))
2542 static unsigned int next
;
2543 static int simple_rand(void)
2546 next
= current
->pid
;
2547 next
= next
* 1103515245 + 12345;
2548 return (next
>> 16) & 32767;
2551 static int do_fail(struct ubifs_info
*c
, int lnum
, int write
)
2553 struct ubifs_debug_info
*d
= c
->dbg
;
2555 ubifs_assert(dbg_is_tst_rcvry(c
));
2557 if (d
->failure_mode
)
2561 /* First call - decide delay to failure */
2563 unsigned int delay
= 1 << (simple_rand() >> 11);
2567 d
->fail_timeout
= jiffies
+
2568 msecs_to_jiffies(delay
);
2569 ubifs_warn("failing after %ums", delay
);
2572 d
->fail_cnt_max
= delay
;
2573 ubifs_warn("failing after %u calls", delay
);
2578 /* Determine if failure delay has expired */
2579 if (d
->fail_delay
== 1) {
2580 if (time_before(jiffies
, d
->fail_timeout
))
2582 } else if (d
->fail_delay
== 2)
2583 if (d
->fail_cnt
++ < d
->fail_cnt_max
)
2585 if (lnum
== UBIFS_SB_LNUM
) {
2589 } else if (chance(19, 20))
2591 ubifs_warn("failing in super block LEB %d", lnum
);
2592 } else if (lnum
== UBIFS_MST_LNUM
|| lnum
== UBIFS_MST_LNUM
+ 1) {
2595 ubifs_warn("failing in master LEB %d", lnum
);
2596 } else if (lnum
>= UBIFS_LOG_LNUM
&& lnum
<= c
->log_last
) {
2598 if (chance(99, 100))
2600 } else if (chance(399, 400))
2602 ubifs_warn("failing in log LEB %d", lnum
);
2603 } else if (lnum
>= c
->lpt_first
&& lnum
<= c
->lpt_last
) {
2607 } else if (chance(19, 20))
2609 ubifs_warn("failing in LPT LEB %d", lnum
);
2610 } else if (lnum
>= c
->orph_first
&& lnum
<= c
->orph_last
) {
2614 } else if (chance(9, 10))
2616 ubifs_warn("failing in orphan LEB %d", lnum
);
2617 } else if (lnum
== c
->ihead_lnum
) {
2618 if (chance(99, 100))
2620 ubifs_warn("failing in index head LEB %d", lnum
);
2621 } else if (c
->jheads
&& lnum
== c
->jheads
[GCHD
].wbuf
.lnum
) {
2624 ubifs_warn("failing in GC head LEB %d", lnum
);
2625 } else if (write
&& !RB_EMPTY_ROOT(&c
->buds
) &&
2626 !ubifs_search_bud(c
, lnum
)) {
2629 ubifs_warn("failing in non-bud LEB %d", lnum
);
2630 } else if (c
->cmt_state
== COMMIT_RUNNING_BACKGROUND
||
2631 c
->cmt_state
== COMMIT_RUNNING_REQUIRED
) {
2632 if (chance(999, 1000))
2634 ubifs_warn("failing in bud LEB %d commit running", lnum
);
2636 if (chance(9999, 10000))
2638 ubifs_warn("failing in bud LEB %d commit not running", lnum
);
2641 d
->failure_mode
= 1;
2646 static void cut_data(const void *buf
, int len
)
2649 unsigned char *p
= (void *)buf
;
2651 flen
= (len
* (long long)simple_rand()) >> 15;
2652 for (i
= flen
; i
< len
; i
++)
2656 int dbg_leb_write(struct ubifs_info
*c
, int lnum
, const void *buf
,
2657 int offs
, int len
, int dtype
)
2661 if (c
->dbg
->failure_mode
)
2663 failing
= do_fail(c
, lnum
, 1);
2666 err
= ubi_leb_write(c
->ubi
, lnum
, buf
, offs
, len
, dtype
);
2674 int dbg_leb_change(struct ubifs_info
*c
, int lnum
, const void *buf
,
2679 if (do_fail(c
, lnum
, 1))
2681 err
= ubi_leb_change(c
->ubi
, lnum
, buf
, len
, dtype
);
2684 if (do_fail(c
, lnum
, 1))
2689 int dbg_leb_unmap(struct ubifs_info
*c
, int lnum
)
2693 if (do_fail(c
, lnum
, 0))
2695 err
= ubi_leb_unmap(c
->ubi
, lnum
);
2698 if (do_fail(c
, lnum
, 0))
2703 int dbg_leb_map(struct ubifs_info
*c
, int lnum
, int dtype
)
2707 if (do_fail(c
, lnum
, 0))
2709 err
= ubi_leb_map(c
->ubi
, lnum
, dtype
);
2712 if (do_fail(c
, lnum
, 0))
2718 * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
2719 * contain the stuff specific to particular file-system mounts.
2721 static struct dentry
*dfs_rootdir
;
2723 static int dfs_file_open(struct inode
*inode
, struct file
*file
)
2725 file
->private_data
= inode
->i_private
;
2726 return nonseekable_open(inode
, file
);
2730 * provide_user_output - provide output to the user reading a debugfs file.
2731 * @val: boolean value for the answer
2732 * @u: the buffer to store the answer at
2733 * @count: size of the buffer
2734 * @ppos: position in the @u output buffer
2736 * This is a simple helper function which stores @val boolean value in the user
2737 * buffer when the user reads one of UBIFS debugfs files. Returns amount of
2738 * bytes written to @u in case of success and a negative error code in case of
2741 static int provide_user_output(int val
, char __user
*u
, size_t count
,
2753 return simple_read_from_buffer(u
, count
, ppos
, buf
, 2);
2756 static ssize_t
dfs_file_read(struct file
*file
, char __user
*u
, size_t count
,
2759 struct dentry
*dent
= file
->f_path
.dentry
;
2760 struct ubifs_info
*c
= file
->private_data
;
2761 struct ubifs_debug_info
*d
= c
->dbg
;
2764 if (dent
== d
->dfs_chk_gen
)
2766 else if (dent
== d
->dfs_chk_index
)
2768 else if (dent
== d
->dfs_chk_orph
)
2770 else if (dent
== d
->dfs_chk_lprops
)
2771 val
= d
->chk_lprops
;
2772 else if (dent
== d
->dfs_chk_fs
)
2774 else if (dent
== d
->dfs_tst_rcvry
)
2779 return provide_user_output(val
, u
, count
, ppos
);
2783 * interpret_user_input - interpret user debugfs file input.
2784 * @u: user-provided buffer with the input
2785 * @count: buffer size
2787 * This is a helper function which interpret user input to a boolean UBIFS
2788 * debugfs file. Returns %0 or %1 in case of success and a negative error code
2789 * in case of failure.
2791 static int interpret_user_input(const char __user
*u
, size_t count
)
2796 buf_size
= min_t(size_t, count
, (sizeof(buf
) - 1));
2797 if (copy_from_user(buf
, u
, buf_size
))
2802 else if (buf
[0] == '0')
2808 static ssize_t
dfs_file_write(struct file
*file
, const char __user
*u
,
2809 size_t count
, loff_t
*ppos
)
2811 struct ubifs_info
*c
= file
->private_data
;
2812 struct ubifs_debug_info
*d
= c
->dbg
;
2813 struct dentry
*dent
= file
->f_path
.dentry
;
2817 * TODO: this is racy - the file-system might have already been
2818 * unmounted and we'd oops in this case. The plan is to fix it with
2819 * help of 'iterate_supers_type()' which we should have in v3.0: when
2820 * a debugfs opened, we rember FS's UUID in file->private_data. Then
2821 * whenever we access the FS via a debugfs file, we iterate all UBIFS
2822 * superblocks and fine the one with the same UUID, and take the
2825 * The other way to go suggested by Al Viro is to create a separate
2826 * 'ubifs-debug' file-system instead.
2828 if (file
->f_path
.dentry
== d
->dfs_dump_lprops
) {
2832 if (file
->f_path
.dentry
== d
->dfs_dump_budg
) {
2833 dbg_dump_budg(c
, &c
->bi
);
2836 if (file
->f_path
.dentry
== d
->dfs_dump_tnc
) {
2837 mutex_lock(&c
->tnc_mutex
);
2839 mutex_unlock(&c
->tnc_mutex
);
2843 val
= interpret_user_input(u
, count
);
2847 if (dent
== d
->dfs_chk_gen
)
2849 else if (dent
== d
->dfs_chk_index
)
2851 else if (dent
== d
->dfs_chk_orph
)
2853 else if (dent
== d
->dfs_chk_lprops
)
2854 d
->chk_lprops
= val
;
2855 else if (dent
== d
->dfs_chk_fs
)
2857 else if (dent
== d
->dfs_tst_rcvry
)
2865 static const struct file_operations dfs_fops
= {
2866 .open
= dfs_file_open
,
2867 .read
= dfs_file_read
,
2868 .write
= dfs_file_write
,
2869 .owner
= THIS_MODULE
,
2870 .llseek
= no_llseek
,
2874 * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
2875 * @c: UBIFS file-system description object
2877 * This function creates all debugfs files for this instance of UBIFS. Returns
2878 * zero in case of success and a negative error code in case of failure.
2880 * Note, the only reason we have not merged this function with the
2881 * 'ubifs_debugging_init()' function is because it is better to initialize
2882 * debugfs interfaces at the very end of the mount process, and remove them at
2883 * the very beginning of the mount process.
2885 int dbg_debugfs_init_fs(struct ubifs_info
*c
)
2889 struct dentry
*dent
;
2890 struct ubifs_debug_info
*d
= c
->dbg
;
2892 n
= snprintf(d
->dfs_dir_name
, UBIFS_DFS_DIR_LEN
+ 1, UBIFS_DFS_DIR_NAME
,
2893 c
->vi
.ubi_num
, c
->vi
.vol_id
);
2894 if (n
== UBIFS_DFS_DIR_LEN
) {
2895 /* The array size is too small */
2896 fname
= UBIFS_DFS_DIR_NAME
;
2897 dent
= ERR_PTR(-EINVAL
);
2901 fname
= d
->dfs_dir_name
;
2902 dent
= debugfs_create_dir(fname
, dfs_rootdir
);
2903 if (IS_ERR_OR_NULL(dent
))
2907 fname
= "dump_lprops";
2908 dent
= debugfs_create_file(fname
, S_IWUSR
, d
->dfs_dir
, c
, &dfs_fops
);
2909 if (IS_ERR_OR_NULL(dent
))
2911 d
->dfs_dump_lprops
= dent
;
2913 fname
= "dump_budg";
2914 dent
= debugfs_create_file(fname
, S_IWUSR
, d
->dfs_dir
, c
, &dfs_fops
);
2915 if (IS_ERR_OR_NULL(dent
))
2917 d
->dfs_dump_budg
= dent
;
2920 dent
= debugfs_create_file(fname
, S_IWUSR
, d
->dfs_dir
, c
, &dfs_fops
);
2921 if (IS_ERR_OR_NULL(dent
))
2923 d
->dfs_dump_tnc
= dent
;
2925 fname
= "chk_general";
2926 dent
= debugfs_create_file(fname
, S_IRUSR
| S_IWUSR
, d
->dfs_dir
, c
,
2928 if (IS_ERR_OR_NULL(dent
))
2930 d
->dfs_chk_gen
= dent
;
2932 fname
= "chk_index";
2933 dent
= debugfs_create_file(fname
, S_IRUSR
| S_IWUSR
, d
->dfs_dir
, c
,
2935 if (IS_ERR_OR_NULL(dent
))
2937 d
->dfs_chk_index
= dent
;
2939 fname
= "chk_orphans";
2940 dent
= debugfs_create_file(fname
, S_IRUSR
| S_IWUSR
, d
->dfs_dir
, c
,
2942 if (IS_ERR_OR_NULL(dent
))
2944 d
->dfs_chk_orph
= dent
;
2946 fname
= "chk_lprops";
2947 dent
= debugfs_create_file(fname
, S_IRUSR
| S_IWUSR
, d
->dfs_dir
, c
,
2949 if (IS_ERR_OR_NULL(dent
))
2951 d
->dfs_chk_lprops
= dent
;
2954 dent
= debugfs_create_file(fname
, S_IRUSR
| S_IWUSR
, d
->dfs_dir
, c
,
2956 if (IS_ERR_OR_NULL(dent
))
2958 d
->dfs_chk_fs
= dent
;
2960 fname
= "tst_recovery";
2961 dent
= debugfs_create_file(fname
, S_IRUSR
| S_IWUSR
, d
->dfs_dir
, c
,
2963 if (IS_ERR_OR_NULL(dent
))
2965 d
->dfs_tst_rcvry
= dent
;
2970 debugfs_remove_recursive(d
->dfs_dir
);
2972 err
= dent
? PTR_ERR(dent
) : -ENODEV
;
2973 ubifs_err("cannot create \"%s\" debugfs file or directory, error %d\n",
2979 * dbg_debugfs_exit_fs - remove all debugfs files.
2980 * @c: UBIFS file-system description object
2982 void dbg_debugfs_exit_fs(struct ubifs_info
*c
)
2984 debugfs_remove_recursive(c
->dbg
->dfs_dir
);
2987 struct ubifs_global_debug_info ubifs_dbg
;
2989 static struct dentry
*dfs_chk_gen
;
2990 static struct dentry
*dfs_chk_index
;
2991 static struct dentry
*dfs_chk_orph
;
2992 static struct dentry
*dfs_chk_lprops
;
2993 static struct dentry
*dfs_chk_fs
;
2994 static struct dentry
*dfs_tst_rcvry
;
2996 static ssize_t
dfs_global_file_read(struct file
*file
, char __user
*u
,
2997 size_t count
, loff_t
*ppos
)
2999 struct dentry
*dent
= file
->f_path
.dentry
;
3002 if (dent
== dfs_chk_gen
)
3003 val
= ubifs_dbg
.chk_gen
;
3004 else if (dent
== dfs_chk_index
)
3005 val
= ubifs_dbg
.chk_index
;
3006 else if (dent
== dfs_chk_orph
)
3007 val
= ubifs_dbg
.chk_orph
;
3008 else if (dent
== dfs_chk_lprops
)
3009 val
= ubifs_dbg
.chk_lprops
;
3010 else if (dent
== dfs_chk_fs
)
3011 val
= ubifs_dbg
.chk_fs
;
3012 else if (dent
== dfs_tst_rcvry
)
3013 val
= ubifs_dbg
.tst_rcvry
;
3017 return provide_user_output(val
, u
, count
, ppos
);
3020 static ssize_t
dfs_global_file_write(struct file
*file
, const char __user
*u
,
3021 size_t count
, loff_t
*ppos
)
3023 struct dentry
*dent
= file
->f_path
.dentry
;
3026 val
= interpret_user_input(u
, count
);
3030 if (dent
== dfs_chk_gen
)
3031 ubifs_dbg
.chk_gen
= val
;
3032 else if (dent
== dfs_chk_index
)
3033 ubifs_dbg
.chk_index
= val
;
3034 else if (dent
== dfs_chk_orph
)
3035 ubifs_dbg
.chk_orph
= val
;
3036 else if (dent
== dfs_chk_lprops
)
3037 ubifs_dbg
.chk_lprops
= val
;
3038 else if (dent
== dfs_chk_fs
)
3039 ubifs_dbg
.chk_fs
= val
;
3040 else if (dent
== dfs_tst_rcvry
)
3041 ubifs_dbg
.tst_rcvry
= val
;
3048 static const struct file_operations dfs_global_fops
= {
3049 .read
= dfs_global_file_read
,
3050 .write
= dfs_global_file_write
,
3051 .owner
= THIS_MODULE
,
3052 .llseek
= no_llseek
,
3056 * dbg_debugfs_init - initialize debugfs file-system.
3058 * UBIFS uses debugfs file-system to expose various debugging knobs to
3059 * user-space. This function creates "ubifs" directory in the debugfs
3060 * file-system. Returns zero in case of success and a negative error code in
3063 int dbg_debugfs_init(void)
3067 struct dentry
*dent
;
3070 dent
= debugfs_create_dir(fname
, NULL
);
3071 if (IS_ERR_OR_NULL(dent
))
3075 fname
= "chk_general";
3076 dent
= debugfs_create_file(fname
, S_IRUSR
| S_IWUSR
, dfs_rootdir
, NULL
,
3078 if (IS_ERR_OR_NULL(dent
))
3082 fname
= "chk_index";
3083 dent
= debugfs_create_file(fname
, S_IRUSR
| S_IWUSR
, dfs_rootdir
, NULL
,
3085 if (IS_ERR_OR_NULL(dent
))
3087 dfs_chk_index
= dent
;
3089 fname
= "chk_orphans";
3090 dent
= debugfs_create_file(fname
, S_IRUSR
| S_IWUSR
, dfs_rootdir
, NULL
,
3092 if (IS_ERR_OR_NULL(dent
))
3094 dfs_chk_orph
= dent
;
3096 fname
= "chk_lprops";
3097 dent
= debugfs_create_file(fname
, S_IRUSR
| S_IWUSR
, dfs_rootdir
, NULL
,
3099 if (IS_ERR_OR_NULL(dent
))
3101 dfs_chk_lprops
= dent
;
3104 dent
= debugfs_create_file(fname
, S_IRUSR
| S_IWUSR
, dfs_rootdir
, NULL
,
3106 if (IS_ERR_OR_NULL(dent
))
3110 fname
= "tst_recovery";
3111 dent
= debugfs_create_file(fname
, S_IRUSR
| S_IWUSR
, dfs_rootdir
, NULL
,
3113 if (IS_ERR_OR_NULL(dent
))
3115 dfs_tst_rcvry
= dent
;
3120 debugfs_remove_recursive(dfs_rootdir
);
3122 err
= dent
? PTR_ERR(dent
) : -ENODEV
;
3123 ubifs_err("cannot create \"%s\" debugfs file or directory, error %d\n",
3129 * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
3131 void dbg_debugfs_exit(void)
3133 debugfs_remove_recursive(dfs_rootdir
);
3137 * ubifs_debugging_init - initialize UBIFS debugging.
3138 * @c: UBIFS file-system description object
3140 * This function initializes debugging-related data for the file system.
3141 * Returns zero in case of success and a negative error code in case of
3144 int ubifs_debugging_init(struct ubifs_info
*c
)
3146 c
->dbg
= kzalloc(sizeof(struct ubifs_debug_info
), GFP_KERNEL
);
3154 * ubifs_debugging_exit - free debugging data.
3155 * @c: UBIFS file-system description object
3157 void ubifs_debugging_exit(struct ubifs_info
*c
)
3162 #endif /* CONFIG_UBIFS_FS_DEBUG */