2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
24 * This file implements most of the debugging stuff which is compiled in only
25 * when it is enabled. But some debugging check functions are implemented in
26 * corresponding subsystem, just because they are closely related and utilize
27 * various local functions of those subsystems.
30 #define UBIFS_DBG_PRESERVE_UBI
33 #include <linux/module.h>
34 #include <linux/moduleparam.h>
36 #ifdef CONFIG_UBIFS_FS_DEBUG
38 DEFINE_SPINLOCK(dbg_lock
);
40 static char dbg_key_buf0
[128];
41 static char dbg_key_buf1
[128];
43 unsigned int ubifs_msg_flags
= UBIFS_MSG_FLAGS_DEFAULT
;
44 unsigned int ubifs_chk_flags
= UBIFS_CHK_FLAGS_DEFAULT
;
45 unsigned int ubifs_tst_flags
;
47 module_param_named(debug_msgs
, ubifs_msg_flags
, uint
, S_IRUGO
| S_IWUSR
);
48 module_param_named(debug_chks
, ubifs_chk_flags
, uint
, S_IRUGO
| S_IWUSR
);
49 module_param_named(debug_tsts
, ubifs_tst_flags
, uint
, S_IRUGO
| S_IWUSR
);
51 MODULE_PARM_DESC(debug_msgs
, "Debug message type flags");
52 MODULE_PARM_DESC(debug_chks
, "Debug check flags");
53 MODULE_PARM_DESC(debug_tsts
, "Debug special test flags");
55 static const char *get_key_fmt(int fmt
)
58 case UBIFS_SIMPLE_KEY_FMT
:
61 return "unknown/invalid format";
65 static const char *get_key_hash(int hash
)
68 case UBIFS_KEY_HASH_R5
:
70 case UBIFS_KEY_HASH_TEST
:
73 return "unknown/invalid name hash";
77 static const char *get_key_type(int type
)
91 return "unknown/invalid key";
95 static void sprintf_key(const struct ubifs_info
*c
, const union ubifs_key
*key
,
99 int type
= key_type(c
, key
);
101 if (c
->key_fmt
== UBIFS_SIMPLE_KEY_FMT
) {
104 sprintf(p
, "(%lu, %s)", (unsigned long)key_inum(c
, key
),
109 sprintf(p
, "(%lu, %s, %#08x)",
110 (unsigned long)key_inum(c
, key
),
111 get_key_type(type
), key_hash(c
, key
));
114 sprintf(p
, "(%lu, %s, %u)",
115 (unsigned long)key_inum(c
, key
),
116 get_key_type(type
), key_block(c
, key
));
119 sprintf(p
, "(%lu, %s)",
120 (unsigned long)key_inum(c
, key
),
124 sprintf(p
, "(bad key type: %#08x, %#08x)",
125 key
->u32
[0], key
->u32
[1]);
128 sprintf(p
, "bad key format %d", c
->key_fmt
);
131 const char *dbg_key_str0(const struct ubifs_info
*c
, const union ubifs_key
*key
)
133 /* dbg_lock must be held */
134 sprintf_key(c
, key
, dbg_key_buf0
);
138 const char *dbg_key_str1(const struct ubifs_info
*c
, const union ubifs_key
*key
)
140 /* dbg_lock must be held */
141 sprintf_key(c
, key
, dbg_key_buf1
);
145 const char *dbg_ntype(int type
)
149 return "padding node";
151 return "superblock node";
153 return "master node";
155 return "reference node";
158 case UBIFS_DENT_NODE
:
159 return "direntry node";
160 case UBIFS_XENT_NODE
:
161 return "xentry node";
162 case UBIFS_DATA_NODE
:
164 case UBIFS_TRUN_NODE
:
165 return "truncate node";
167 return "indexing node";
169 return "commit start node";
170 case UBIFS_ORPH_NODE
:
171 return "orphan node";
173 return "unknown node";
177 static const char *dbg_gtype(int type
)
180 case UBIFS_NO_NODE_GROUP
:
181 return "no node group";
182 case UBIFS_IN_NODE_GROUP
:
183 return "in node group";
184 case UBIFS_LAST_OF_NODE_GROUP
:
185 return "last of node group";
191 const char *dbg_cstate(int cmt_state
)
195 return "commit resting";
196 case COMMIT_BACKGROUND
:
197 return "background commit requested";
198 case COMMIT_REQUIRED
:
199 return "commit required";
200 case COMMIT_RUNNING_BACKGROUND
:
201 return "BACKGROUND commit running";
202 case COMMIT_RUNNING_REQUIRED
:
203 return "commit running and required";
205 return "broken commit";
207 return "unknown commit state";
211 static void dump_ch(const struct ubifs_ch
*ch
)
213 printk(KERN_DEBUG
"\tmagic %#x\n", le32_to_cpu(ch
->magic
));
214 printk(KERN_DEBUG
"\tcrc %#x\n", le32_to_cpu(ch
->crc
));
215 printk(KERN_DEBUG
"\tnode_type %d (%s)\n", ch
->node_type
,
216 dbg_ntype(ch
->node_type
));
217 printk(KERN_DEBUG
"\tgroup_type %d (%s)\n", ch
->group_type
,
218 dbg_gtype(ch
->group_type
));
219 printk(KERN_DEBUG
"\tsqnum %llu\n",
220 (unsigned long long)le64_to_cpu(ch
->sqnum
));
221 printk(KERN_DEBUG
"\tlen %u\n", le32_to_cpu(ch
->len
));
224 void dbg_dump_inode(const struct ubifs_info
*c
, const struct inode
*inode
)
226 const struct ubifs_inode
*ui
= ubifs_inode(inode
);
228 printk(KERN_DEBUG
"Dump in-memory inode:");
229 printk(KERN_DEBUG
"\tinode %lu\n", inode
->i_ino
);
230 printk(KERN_DEBUG
"\tsize %llu\n",
231 (unsigned long long)i_size_read(inode
));
232 printk(KERN_DEBUG
"\tnlink %u\n", inode
->i_nlink
);
233 printk(KERN_DEBUG
"\tuid %u\n", (unsigned int)inode
->i_uid
);
234 printk(KERN_DEBUG
"\tgid %u\n", (unsigned int)inode
->i_gid
);
235 printk(KERN_DEBUG
"\tatime %u.%u\n",
236 (unsigned int)inode
->i_atime
.tv_sec
,
237 (unsigned int)inode
->i_atime
.tv_nsec
);
238 printk(KERN_DEBUG
"\tmtime %u.%u\n",
239 (unsigned int)inode
->i_mtime
.tv_sec
,
240 (unsigned int)inode
->i_mtime
.tv_nsec
);
241 printk(KERN_DEBUG
"\tctime %u.%u\n",
242 (unsigned int)inode
->i_ctime
.tv_sec
,
243 (unsigned int)inode
->i_ctime
.tv_nsec
);
244 printk(KERN_DEBUG
"\tcreat_sqnum %llu\n", ui
->creat_sqnum
);
245 printk(KERN_DEBUG
"\txattr_size %u\n", ui
->xattr_size
);
246 printk(KERN_DEBUG
"\txattr_cnt %u\n", ui
->xattr_cnt
);
247 printk(KERN_DEBUG
"\txattr_names %u\n", ui
->xattr_names
);
248 printk(KERN_DEBUG
"\tdirty %u\n", ui
->dirty
);
249 printk(KERN_DEBUG
"\txattr %u\n", ui
->xattr
);
250 printk(KERN_DEBUG
"\tbulk_read %u\n", ui
->xattr
);
251 printk(KERN_DEBUG
"\tsynced_i_size %llu\n",
252 (unsigned long long)ui
->synced_i_size
);
253 printk(KERN_DEBUG
"\tui_size %llu\n",
254 (unsigned long long)ui
->ui_size
);
255 printk(KERN_DEBUG
"\tflags %d\n", ui
->flags
);
256 printk(KERN_DEBUG
"\tcompr_type %d\n", ui
->compr_type
);
257 printk(KERN_DEBUG
"\tlast_page_read %lu\n", ui
->last_page_read
);
258 printk(KERN_DEBUG
"\tread_in_a_row %lu\n", ui
->read_in_a_row
);
259 printk(KERN_DEBUG
"\tdata_len %d\n", ui
->data_len
);
262 void dbg_dump_node(const struct ubifs_info
*c
, const void *node
)
266 const struct ubifs_ch
*ch
= node
;
268 if (dbg_failure_mode
)
271 /* If the magic is incorrect, just hexdump the first bytes */
272 if (le32_to_cpu(ch
->magic
) != UBIFS_NODE_MAGIC
) {
273 printk(KERN_DEBUG
"Not a node, first %zu bytes:", UBIFS_CH_SZ
);
274 print_hex_dump(KERN_DEBUG
, "", DUMP_PREFIX_OFFSET
, 32, 1,
275 (void *)node
, UBIFS_CH_SZ
, 1);
279 spin_lock(&dbg_lock
);
282 switch (ch
->node_type
) {
285 const struct ubifs_pad_node
*pad
= node
;
287 printk(KERN_DEBUG
"\tpad_len %u\n",
288 le32_to_cpu(pad
->pad_len
));
293 const struct ubifs_sb_node
*sup
= node
;
294 unsigned int sup_flags
= le32_to_cpu(sup
->flags
);
296 printk(KERN_DEBUG
"\tkey_hash %d (%s)\n",
297 (int)sup
->key_hash
, get_key_hash(sup
->key_hash
));
298 printk(KERN_DEBUG
"\tkey_fmt %d (%s)\n",
299 (int)sup
->key_fmt
, get_key_fmt(sup
->key_fmt
));
300 printk(KERN_DEBUG
"\tflags %#x\n", sup_flags
);
301 printk(KERN_DEBUG
"\t big_lpt %u\n",
302 !!(sup_flags
& UBIFS_FLG_BIGLPT
));
303 printk(KERN_DEBUG
"\tmin_io_size %u\n",
304 le32_to_cpu(sup
->min_io_size
));
305 printk(KERN_DEBUG
"\tleb_size %u\n",
306 le32_to_cpu(sup
->leb_size
));
307 printk(KERN_DEBUG
"\tleb_cnt %u\n",
308 le32_to_cpu(sup
->leb_cnt
));
309 printk(KERN_DEBUG
"\tmax_leb_cnt %u\n",
310 le32_to_cpu(sup
->max_leb_cnt
));
311 printk(KERN_DEBUG
"\tmax_bud_bytes %llu\n",
312 (unsigned long long)le64_to_cpu(sup
->max_bud_bytes
));
313 printk(KERN_DEBUG
"\tlog_lebs %u\n",
314 le32_to_cpu(sup
->log_lebs
));
315 printk(KERN_DEBUG
"\tlpt_lebs %u\n",
316 le32_to_cpu(sup
->lpt_lebs
));
317 printk(KERN_DEBUG
"\torph_lebs %u\n",
318 le32_to_cpu(sup
->orph_lebs
));
319 printk(KERN_DEBUG
"\tjhead_cnt %u\n",
320 le32_to_cpu(sup
->jhead_cnt
));
321 printk(KERN_DEBUG
"\tfanout %u\n",
322 le32_to_cpu(sup
->fanout
));
323 printk(KERN_DEBUG
"\tlsave_cnt %u\n",
324 le32_to_cpu(sup
->lsave_cnt
));
325 printk(KERN_DEBUG
"\tdefault_compr %u\n",
326 (int)le16_to_cpu(sup
->default_compr
));
327 printk(KERN_DEBUG
"\trp_size %llu\n",
328 (unsigned long long)le64_to_cpu(sup
->rp_size
));
329 printk(KERN_DEBUG
"\trp_uid %u\n",
330 le32_to_cpu(sup
->rp_uid
));
331 printk(KERN_DEBUG
"\trp_gid %u\n",
332 le32_to_cpu(sup
->rp_gid
));
333 printk(KERN_DEBUG
"\tfmt_version %u\n",
334 le32_to_cpu(sup
->fmt_version
));
335 printk(KERN_DEBUG
"\ttime_gran %u\n",
336 le32_to_cpu(sup
->time_gran
));
337 printk(KERN_DEBUG
"\tUUID %02X%02X%02X%02X-%02X%02X"
338 "-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
339 sup
->uuid
[0], sup
->uuid
[1], sup
->uuid
[2], sup
->uuid
[3],
340 sup
->uuid
[4], sup
->uuid
[5], sup
->uuid
[6], sup
->uuid
[7],
341 sup
->uuid
[8], sup
->uuid
[9], sup
->uuid
[10], sup
->uuid
[11],
342 sup
->uuid
[12], sup
->uuid
[13], sup
->uuid
[14],
348 const struct ubifs_mst_node
*mst
= node
;
350 printk(KERN_DEBUG
"\thighest_inum %llu\n",
351 (unsigned long long)le64_to_cpu(mst
->highest_inum
));
352 printk(KERN_DEBUG
"\tcommit number %llu\n",
353 (unsigned long long)le64_to_cpu(mst
->cmt_no
));
354 printk(KERN_DEBUG
"\tflags %#x\n",
355 le32_to_cpu(mst
->flags
));
356 printk(KERN_DEBUG
"\tlog_lnum %u\n",
357 le32_to_cpu(mst
->log_lnum
));
358 printk(KERN_DEBUG
"\troot_lnum %u\n",
359 le32_to_cpu(mst
->root_lnum
));
360 printk(KERN_DEBUG
"\troot_offs %u\n",
361 le32_to_cpu(mst
->root_offs
));
362 printk(KERN_DEBUG
"\troot_len %u\n",
363 le32_to_cpu(mst
->root_len
));
364 printk(KERN_DEBUG
"\tgc_lnum %u\n",
365 le32_to_cpu(mst
->gc_lnum
));
366 printk(KERN_DEBUG
"\tihead_lnum %u\n",
367 le32_to_cpu(mst
->ihead_lnum
));
368 printk(KERN_DEBUG
"\tihead_offs %u\n",
369 le32_to_cpu(mst
->ihead_offs
));
370 printk(KERN_DEBUG
"\tindex_size %llu\n",
371 (unsigned long long)le64_to_cpu(mst
->index_size
));
372 printk(KERN_DEBUG
"\tlpt_lnum %u\n",
373 le32_to_cpu(mst
->lpt_lnum
));
374 printk(KERN_DEBUG
"\tlpt_offs %u\n",
375 le32_to_cpu(mst
->lpt_offs
));
376 printk(KERN_DEBUG
"\tnhead_lnum %u\n",
377 le32_to_cpu(mst
->nhead_lnum
));
378 printk(KERN_DEBUG
"\tnhead_offs %u\n",
379 le32_to_cpu(mst
->nhead_offs
));
380 printk(KERN_DEBUG
"\tltab_lnum %u\n",
381 le32_to_cpu(mst
->ltab_lnum
));
382 printk(KERN_DEBUG
"\tltab_offs %u\n",
383 le32_to_cpu(mst
->ltab_offs
));
384 printk(KERN_DEBUG
"\tlsave_lnum %u\n",
385 le32_to_cpu(mst
->lsave_lnum
));
386 printk(KERN_DEBUG
"\tlsave_offs %u\n",
387 le32_to_cpu(mst
->lsave_offs
));
388 printk(KERN_DEBUG
"\tlscan_lnum %u\n",
389 le32_to_cpu(mst
->lscan_lnum
));
390 printk(KERN_DEBUG
"\tleb_cnt %u\n",
391 le32_to_cpu(mst
->leb_cnt
));
392 printk(KERN_DEBUG
"\tempty_lebs %u\n",
393 le32_to_cpu(mst
->empty_lebs
));
394 printk(KERN_DEBUG
"\tidx_lebs %u\n",
395 le32_to_cpu(mst
->idx_lebs
));
396 printk(KERN_DEBUG
"\ttotal_free %llu\n",
397 (unsigned long long)le64_to_cpu(mst
->total_free
));
398 printk(KERN_DEBUG
"\ttotal_dirty %llu\n",
399 (unsigned long long)le64_to_cpu(mst
->total_dirty
));
400 printk(KERN_DEBUG
"\ttotal_used %llu\n",
401 (unsigned long long)le64_to_cpu(mst
->total_used
));
402 printk(KERN_DEBUG
"\ttotal_dead %llu\n",
403 (unsigned long long)le64_to_cpu(mst
->total_dead
));
404 printk(KERN_DEBUG
"\ttotal_dark %llu\n",
405 (unsigned long long)le64_to_cpu(mst
->total_dark
));
410 const struct ubifs_ref_node
*ref
= node
;
412 printk(KERN_DEBUG
"\tlnum %u\n",
413 le32_to_cpu(ref
->lnum
));
414 printk(KERN_DEBUG
"\toffs %u\n",
415 le32_to_cpu(ref
->offs
));
416 printk(KERN_DEBUG
"\tjhead %u\n",
417 le32_to_cpu(ref
->jhead
));
422 const struct ubifs_ino_node
*ino
= node
;
424 key_read(c
, &ino
->key
, &key
);
425 printk(KERN_DEBUG
"\tkey %s\n", DBGKEY(&key
));
426 printk(KERN_DEBUG
"\tcreat_sqnum %llu\n",
427 (unsigned long long)le64_to_cpu(ino
->creat_sqnum
));
428 printk(KERN_DEBUG
"\tsize %llu\n",
429 (unsigned long long)le64_to_cpu(ino
->size
));
430 printk(KERN_DEBUG
"\tnlink %u\n",
431 le32_to_cpu(ino
->nlink
));
432 printk(KERN_DEBUG
"\tatime %lld.%u\n",
433 (long long)le64_to_cpu(ino
->atime_sec
),
434 le32_to_cpu(ino
->atime_nsec
));
435 printk(KERN_DEBUG
"\tmtime %lld.%u\n",
436 (long long)le64_to_cpu(ino
->mtime_sec
),
437 le32_to_cpu(ino
->mtime_nsec
));
438 printk(KERN_DEBUG
"\tctime %lld.%u\n",
439 (long long)le64_to_cpu(ino
->ctime_sec
),
440 le32_to_cpu(ino
->ctime_nsec
));
441 printk(KERN_DEBUG
"\tuid %u\n",
442 le32_to_cpu(ino
->uid
));
443 printk(KERN_DEBUG
"\tgid %u\n",
444 le32_to_cpu(ino
->gid
));
445 printk(KERN_DEBUG
"\tmode %u\n",
446 le32_to_cpu(ino
->mode
));
447 printk(KERN_DEBUG
"\tflags %#x\n",
448 le32_to_cpu(ino
->flags
));
449 printk(KERN_DEBUG
"\txattr_cnt %u\n",
450 le32_to_cpu(ino
->xattr_cnt
));
451 printk(KERN_DEBUG
"\txattr_size %u\n",
452 le32_to_cpu(ino
->xattr_size
));
453 printk(KERN_DEBUG
"\txattr_names %u\n",
454 le32_to_cpu(ino
->xattr_names
));
455 printk(KERN_DEBUG
"\tcompr_type %#x\n",
456 (int)le16_to_cpu(ino
->compr_type
));
457 printk(KERN_DEBUG
"\tdata len %u\n",
458 le32_to_cpu(ino
->data_len
));
461 case UBIFS_DENT_NODE
:
462 case UBIFS_XENT_NODE
:
464 const struct ubifs_dent_node
*dent
= node
;
465 int nlen
= le16_to_cpu(dent
->nlen
);
467 key_read(c
, &dent
->key
, &key
);
468 printk(KERN_DEBUG
"\tkey %s\n", DBGKEY(&key
));
469 printk(KERN_DEBUG
"\tinum %llu\n",
470 (unsigned long long)le64_to_cpu(dent
->inum
));
471 printk(KERN_DEBUG
"\ttype %d\n", (int)dent
->type
);
472 printk(KERN_DEBUG
"\tnlen %d\n", nlen
);
473 printk(KERN_DEBUG
"\tname ");
475 if (nlen
> UBIFS_MAX_NLEN
)
476 printk(KERN_DEBUG
"(bad name length, not printing, "
477 "bad or corrupted node)");
479 for (i
= 0; i
< nlen
&& dent
->name
[i
]; i
++)
480 printk("%c", dent
->name
[i
]);
486 case UBIFS_DATA_NODE
:
488 const struct ubifs_data_node
*dn
= node
;
489 int dlen
= le32_to_cpu(ch
->len
) - UBIFS_DATA_NODE_SZ
;
491 key_read(c
, &dn
->key
, &key
);
492 printk(KERN_DEBUG
"\tkey %s\n", DBGKEY(&key
));
493 printk(KERN_DEBUG
"\tsize %u\n",
494 le32_to_cpu(dn
->size
));
495 printk(KERN_DEBUG
"\tcompr_typ %d\n",
496 (int)le16_to_cpu(dn
->compr_type
));
497 printk(KERN_DEBUG
"\tdata size %d\n",
499 printk(KERN_DEBUG
"\tdata:\n");
500 print_hex_dump(KERN_DEBUG
, "\t", DUMP_PREFIX_OFFSET
, 32, 1,
501 (void *)&dn
->data
, dlen
, 0);
504 case UBIFS_TRUN_NODE
:
506 const struct ubifs_trun_node
*trun
= node
;
508 printk(KERN_DEBUG
"\tinum %u\n",
509 le32_to_cpu(trun
->inum
));
510 printk(KERN_DEBUG
"\told_size %llu\n",
511 (unsigned long long)le64_to_cpu(trun
->old_size
));
512 printk(KERN_DEBUG
"\tnew_size %llu\n",
513 (unsigned long long)le64_to_cpu(trun
->new_size
));
518 const struct ubifs_idx_node
*idx
= node
;
520 n
= le16_to_cpu(idx
->child_cnt
);
521 printk(KERN_DEBUG
"\tchild_cnt %d\n", n
);
522 printk(KERN_DEBUG
"\tlevel %d\n",
523 (int)le16_to_cpu(idx
->level
));
524 printk(KERN_DEBUG
"\tBranches:\n");
526 for (i
= 0; i
< n
&& i
< c
->fanout
- 1; i
++) {
527 const struct ubifs_branch
*br
;
529 br
= ubifs_idx_branch(c
, idx
, i
);
530 key_read(c
, &br
->key
, &key
);
531 printk(KERN_DEBUG
"\t%d: LEB %d:%d len %d key %s\n",
532 i
, le32_to_cpu(br
->lnum
), le32_to_cpu(br
->offs
),
533 le32_to_cpu(br
->len
), DBGKEY(&key
));
539 case UBIFS_ORPH_NODE
:
541 const struct ubifs_orph_node
*orph
= node
;
543 printk(KERN_DEBUG
"\tcommit number %llu\n",
545 le64_to_cpu(orph
->cmt_no
) & LLONG_MAX
);
546 printk(KERN_DEBUG
"\tlast node flag %llu\n",
547 (unsigned long long)(le64_to_cpu(orph
->cmt_no
)) >> 63);
548 n
= (le32_to_cpu(ch
->len
) - UBIFS_ORPH_NODE_SZ
) >> 3;
549 printk(KERN_DEBUG
"\t%d orphan inode numbers:\n", n
);
550 for (i
= 0; i
< n
; i
++)
551 printk(KERN_DEBUG
"\t ino %llu\n",
552 (unsigned long long)le64_to_cpu(orph
->inos
[i
]));
556 printk(KERN_DEBUG
"node type %d was not recognized\n",
559 spin_unlock(&dbg_lock
);
562 void dbg_dump_budget_req(const struct ubifs_budget_req
*req
)
564 spin_lock(&dbg_lock
);
565 printk(KERN_DEBUG
"Budgeting request: new_ino %d, dirtied_ino %d\n",
566 req
->new_ino
, req
->dirtied_ino
);
567 printk(KERN_DEBUG
"\tnew_ino_d %d, dirtied_ino_d %d\n",
568 req
->new_ino_d
, req
->dirtied_ino_d
);
569 printk(KERN_DEBUG
"\tnew_page %d, dirtied_page %d\n",
570 req
->new_page
, req
->dirtied_page
);
571 printk(KERN_DEBUG
"\tnew_dent %d, mod_dent %d\n",
572 req
->new_dent
, req
->mod_dent
);
573 printk(KERN_DEBUG
"\tidx_growth %d\n", req
->idx_growth
);
574 printk(KERN_DEBUG
"\tdata_growth %d dd_growth %d\n",
575 req
->data_growth
, req
->dd_growth
);
576 spin_unlock(&dbg_lock
);
579 void dbg_dump_lstats(const struct ubifs_lp_stats
*lst
)
581 spin_lock(&dbg_lock
);
582 printk(KERN_DEBUG
"(pid %d) Lprops statistics: empty_lebs %d, "
583 "idx_lebs %d\n", current
->pid
, lst
->empty_lebs
, lst
->idx_lebs
);
584 printk(KERN_DEBUG
"\ttaken_empty_lebs %d, total_free %lld, "
585 "total_dirty %lld\n", lst
->taken_empty_lebs
, lst
->total_free
,
587 printk(KERN_DEBUG
"\ttotal_used %lld, total_dark %lld, "
588 "total_dead %lld\n", lst
->total_used
, lst
->total_dark
,
590 spin_unlock(&dbg_lock
);
593 void dbg_dump_budg(struct ubifs_info
*c
)
597 struct ubifs_bud
*bud
;
598 struct ubifs_gced_idx_leb
*idx_gc
;
600 spin_lock(&dbg_lock
);
601 printk(KERN_DEBUG
"(pid %d) Budgeting info: budg_data_growth %lld, "
602 "budg_dd_growth %lld, budg_idx_growth %lld\n", current
->pid
,
603 c
->budg_data_growth
, c
->budg_dd_growth
, c
->budg_idx_growth
);
604 printk(KERN_DEBUG
"\tdata budget sum %lld, total budget sum %lld, "
605 "freeable_cnt %d\n", c
->budg_data_growth
+ c
->budg_dd_growth
,
606 c
->budg_data_growth
+ c
->budg_dd_growth
+ c
->budg_idx_growth
,
608 printk(KERN_DEBUG
"\tmin_idx_lebs %d, old_idx_sz %lld, "
609 "calc_idx_sz %lld, idx_gc_cnt %d\n", c
->min_idx_lebs
,
610 c
->old_idx_sz
, c
->calc_idx_sz
, c
->idx_gc_cnt
);
611 printk(KERN_DEBUG
"\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, "
612 "clean_zn_cnt %ld\n", atomic_long_read(&c
->dirty_pg_cnt
),
613 atomic_long_read(&c
->dirty_zn_cnt
),
614 atomic_long_read(&c
->clean_zn_cnt
));
615 printk(KERN_DEBUG
"\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
616 c
->dark_wm
, c
->dead_wm
, c
->max_idx_node_sz
);
617 printk(KERN_DEBUG
"\tgc_lnum %d, ihead_lnum %d\n",
618 c
->gc_lnum
, c
->ihead_lnum
);
619 for (i
= 0; i
< c
->jhead_cnt
; i
++)
620 printk(KERN_DEBUG
"\tjhead %d\t LEB %d\n",
621 c
->jheads
[i
].wbuf
.jhead
, c
->jheads
[i
].wbuf
.lnum
);
622 for (rb
= rb_first(&c
->buds
); rb
; rb
= rb_next(rb
)) {
623 bud
= rb_entry(rb
, struct ubifs_bud
, rb
);
624 printk(KERN_DEBUG
"\tbud LEB %d\n", bud
->lnum
);
626 list_for_each_entry(bud
, &c
->old_buds
, list
)
627 printk(KERN_DEBUG
"\told bud LEB %d\n", bud
->lnum
);
628 list_for_each_entry(idx_gc
, &c
->idx_gc
, list
)
629 printk(KERN_DEBUG
"\tGC'ed idx LEB %d unmap %d\n",
630 idx_gc
->lnum
, idx_gc
->unmap
);
631 printk(KERN_DEBUG
"\tcommit state %d\n", c
->cmt_state
);
632 spin_unlock(&dbg_lock
);
635 void dbg_dump_lprop(const struct ubifs_info
*c
, const struct ubifs_lprops
*lp
)
637 printk(KERN_DEBUG
"LEB %d lprops: free %d, dirty %d (used %d), "
638 "flags %#x\n", lp
->lnum
, lp
->free
, lp
->dirty
,
639 c
->leb_size
- lp
->free
- lp
->dirty
, lp
->flags
);
642 void dbg_dump_lprops(struct ubifs_info
*c
)
645 struct ubifs_lprops lp
;
646 struct ubifs_lp_stats lst
;
648 printk(KERN_DEBUG
"(pid %d) Dumping LEB properties\n", current
->pid
);
649 ubifs_get_lp_stats(c
, &lst
);
650 dbg_dump_lstats(&lst
);
652 for (lnum
= c
->main_first
; lnum
< c
->leb_cnt
; lnum
++) {
653 err
= ubifs_read_one_lp(c
, lnum
, &lp
);
655 ubifs_err("cannot read lprops for LEB %d", lnum
);
657 dbg_dump_lprop(c
, &lp
);
661 void dbg_dump_lpt_info(struct ubifs_info
*c
)
665 spin_lock(&dbg_lock
);
666 printk(KERN_DEBUG
"\tlpt_sz: %lld\n", c
->lpt_sz
);
667 printk(KERN_DEBUG
"\tpnode_sz: %d\n", c
->pnode_sz
);
668 printk(KERN_DEBUG
"\tnnode_sz: %d\n", c
->nnode_sz
);
669 printk(KERN_DEBUG
"\tltab_sz: %d\n", c
->ltab_sz
);
670 printk(KERN_DEBUG
"\tlsave_sz: %d\n", c
->lsave_sz
);
671 printk(KERN_DEBUG
"\tbig_lpt: %d\n", c
->big_lpt
);
672 printk(KERN_DEBUG
"\tlpt_hght: %d\n", c
->lpt_hght
);
673 printk(KERN_DEBUG
"\tpnode_cnt: %d\n", c
->pnode_cnt
);
674 printk(KERN_DEBUG
"\tnnode_cnt: %d\n", c
->nnode_cnt
);
675 printk(KERN_DEBUG
"\tdirty_pn_cnt: %d\n", c
->dirty_pn_cnt
);
676 printk(KERN_DEBUG
"\tdirty_nn_cnt: %d\n", c
->dirty_nn_cnt
);
677 printk(KERN_DEBUG
"\tlsave_cnt: %d\n", c
->lsave_cnt
);
678 printk(KERN_DEBUG
"\tspace_bits: %d\n", c
->space_bits
);
679 printk(KERN_DEBUG
"\tlpt_lnum_bits: %d\n", c
->lpt_lnum_bits
);
680 printk(KERN_DEBUG
"\tlpt_offs_bits: %d\n", c
->lpt_offs_bits
);
681 printk(KERN_DEBUG
"\tlpt_spc_bits: %d\n", c
->lpt_spc_bits
);
682 printk(KERN_DEBUG
"\tpcnt_bits: %d\n", c
->pcnt_bits
);
683 printk(KERN_DEBUG
"\tlnum_bits: %d\n", c
->lnum_bits
);
684 printk(KERN_DEBUG
"\tLPT root is at %d:%d\n", c
->lpt_lnum
, c
->lpt_offs
);
685 printk(KERN_DEBUG
"\tLPT head is at %d:%d\n",
686 c
->nhead_lnum
, c
->nhead_offs
);
687 printk(KERN_DEBUG
"\tLPT ltab is at %d:%d\n", c
->ltab_lnum
, c
->ltab_offs
);
689 printk(KERN_DEBUG
"\tLPT lsave is at %d:%d\n",
690 c
->lsave_lnum
, c
->lsave_offs
);
691 for (i
= 0; i
< c
->lpt_lebs
; i
++)
692 printk(KERN_DEBUG
"\tLPT LEB %d free %d dirty %d tgc %d "
693 "cmt %d\n", i
+ c
->lpt_first
, c
->ltab
[i
].free
,
694 c
->ltab
[i
].dirty
, c
->ltab
[i
].tgc
, c
->ltab
[i
].cmt
);
695 spin_unlock(&dbg_lock
);
698 void dbg_dump_leb(const struct ubifs_info
*c
, int lnum
)
700 struct ubifs_scan_leb
*sleb
;
701 struct ubifs_scan_node
*snod
;
703 if (dbg_failure_mode
)
706 printk(KERN_DEBUG
"(pid %d) Dumping LEB %d\n", current
->pid
, lnum
);
708 sleb
= ubifs_scan(c
, lnum
, 0, c
->dbg_buf
);
710 ubifs_err("scan error %d", (int)PTR_ERR(sleb
));
714 printk(KERN_DEBUG
"LEB %d has %d nodes ending at %d\n", lnum
,
715 sleb
->nodes_cnt
, sleb
->endpt
);
717 list_for_each_entry(snod
, &sleb
->nodes
, list
) {
719 printk(KERN_DEBUG
"Dumping node at LEB %d:%d len %d\n", lnum
,
720 snod
->offs
, snod
->len
);
721 dbg_dump_node(c
, snod
->node
);
724 ubifs_scan_destroy(sleb
);
728 void dbg_dump_znode(const struct ubifs_info
*c
,
729 const struct ubifs_znode
*znode
)
732 const struct ubifs_zbranch
*zbr
;
734 spin_lock(&dbg_lock
);
736 zbr
= &znode
->parent
->zbranch
[znode
->iip
];
740 printk(KERN_DEBUG
"znode %p, LEB %d:%d len %d parent %p iip %d level %d"
741 " child_cnt %d flags %lx\n", znode
, zbr
->lnum
, zbr
->offs
,
742 zbr
->len
, znode
->parent
, znode
->iip
, znode
->level
,
743 znode
->child_cnt
, znode
->flags
);
745 if (znode
->child_cnt
<= 0 || znode
->child_cnt
> c
->fanout
) {
746 spin_unlock(&dbg_lock
);
750 printk(KERN_DEBUG
"zbranches:\n");
751 for (n
= 0; n
< znode
->child_cnt
; n
++) {
752 zbr
= &znode
->zbranch
[n
];
753 if (znode
->level
> 0)
754 printk(KERN_DEBUG
"\t%d: znode %p LEB %d:%d len %d key "
755 "%s\n", n
, zbr
->znode
, zbr
->lnum
,
759 printk(KERN_DEBUG
"\t%d: LNC %p LEB %d:%d len %d key "
760 "%s\n", n
, zbr
->znode
, zbr
->lnum
,
764 spin_unlock(&dbg_lock
);
767 void dbg_dump_heap(struct ubifs_info
*c
, struct ubifs_lpt_heap
*heap
, int cat
)
771 printk(KERN_DEBUG
"(pid %d) Dumping heap cat %d (%d elements)\n",
772 current
->pid
, cat
, heap
->cnt
);
773 for (i
= 0; i
< heap
->cnt
; i
++) {
774 struct ubifs_lprops
*lprops
= heap
->arr
[i
];
776 printk(KERN_DEBUG
"\t%d. LEB %d hpos %d free %d dirty %d "
777 "flags %d\n", i
, lprops
->lnum
, lprops
->hpos
,
778 lprops
->free
, lprops
->dirty
, lprops
->flags
);
782 void dbg_dump_pnode(struct ubifs_info
*c
, struct ubifs_pnode
*pnode
,
783 struct ubifs_nnode
*parent
, int iip
)
787 printk(KERN_DEBUG
"(pid %d) Dumping pnode:\n", current
->pid
);
788 printk(KERN_DEBUG
"\taddress %zx parent %zx cnext %zx\n",
789 (size_t)pnode
, (size_t)parent
, (size_t)pnode
->cnext
);
790 printk(KERN_DEBUG
"\tflags %lu iip %d level %d num %d\n",
791 pnode
->flags
, iip
, pnode
->level
, pnode
->num
);
792 for (i
= 0; i
< UBIFS_LPT_FANOUT
; i
++) {
793 struct ubifs_lprops
*lp
= &pnode
->lprops
[i
];
795 printk(KERN_DEBUG
"\t%d: free %d dirty %d flags %d lnum %d\n",
796 i
, lp
->free
, lp
->dirty
, lp
->flags
, lp
->lnum
);
800 void dbg_dump_tnc(struct ubifs_info
*c
)
802 struct ubifs_znode
*znode
;
805 printk(KERN_DEBUG
"\n");
806 printk(KERN_DEBUG
"(pid %d) Dumping the TNC tree\n", current
->pid
);
807 znode
= ubifs_tnc_levelorder_next(c
->zroot
.znode
, NULL
);
808 level
= znode
->level
;
809 printk(KERN_DEBUG
"== Level %d ==\n", level
);
811 if (level
!= znode
->level
) {
812 level
= znode
->level
;
813 printk(KERN_DEBUG
"== Level %d ==\n", level
);
815 dbg_dump_znode(c
, znode
);
816 znode
= ubifs_tnc_levelorder_next(c
->zroot
.znode
, znode
);
819 printk(KERN_DEBUG
"\n");
822 static int dump_znode(struct ubifs_info
*c
, struct ubifs_znode
*znode
,
825 dbg_dump_znode(c
, znode
);
830 * dbg_dump_index - dump the on-flash index.
831 * @c: UBIFS file-system description object
833 * This function dumps whole UBIFS indexing B-tree, unlike 'dbg_dump_tnc()'
834 * which dumps only in-memory znodes and does not read znodes which from flash.
836 void dbg_dump_index(struct ubifs_info
*c
)
838 dbg_walk_index(c
, NULL
, dump_znode
, NULL
);
842 * dbg_check_synced_i_size - check synchronized inode size.
843 * @inode: inode to check
845 * If inode is clean, synchronized inode size has to be equivalent to current
846 * inode size. This function has to be called only for locked inodes (@i_mutex
847 * has to be locked). Returns %0 if synchronized inode size if correct, and
850 int dbg_check_synced_i_size(struct inode
*inode
)
853 struct ubifs_inode
*ui
= ubifs_inode(inode
);
855 if (!(ubifs_chk_flags
& UBIFS_CHK_GEN
))
857 if (!S_ISREG(inode
->i_mode
))
860 mutex_lock(&ui
->ui_mutex
);
861 spin_lock(&ui
->ui_lock
);
862 if (ui
->ui_size
!= ui
->synced_i_size
&& !ui
->dirty
) {
863 ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode "
864 "is clean", ui
->ui_size
, ui
->synced_i_size
);
865 ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode
->i_ino
,
866 inode
->i_mode
, i_size_read(inode
));
870 spin_unlock(&ui
->ui_lock
);
871 mutex_unlock(&ui
->ui_mutex
);
876 * dbg_check_dir - check directory inode size and link count.
877 * @c: UBIFS file-system description object
878 * @dir: the directory to calculate size for
879 * @size: the result is returned here
881 * This function makes sure that directory size and link count are correct.
882 * Returns zero in case of success and a negative error code in case of
885 * Note, it is good idea to make sure the @dir->i_mutex is locked before
886 * calling this function.
888 int dbg_check_dir_size(struct ubifs_info
*c
, const struct inode
*dir
)
890 unsigned int nlink
= 2;
892 struct ubifs_dent_node
*dent
, *pdent
= NULL
;
893 struct qstr nm
= { .name
= NULL
};
894 loff_t size
= UBIFS_INO_NODE_SZ
;
896 if (!(ubifs_chk_flags
& UBIFS_CHK_GEN
))
899 if (!S_ISDIR(dir
->i_mode
))
902 lowest_dent_key(c
, &key
, dir
->i_ino
);
906 dent
= ubifs_tnc_next_ent(c
, &key
, &nm
);
914 nm
.name
= dent
->name
;
915 nm
.len
= le16_to_cpu(dent
->nlen
);
916 size
+= CALC_DENT_SIZE(nm
.len
);
917 if (dent
->type
== UBIFS_ITYPE_DIR
)
921 key_read(c
, &dent
->key
, &key
);
925 if (i_size_read(dir
) != size
) {
926 ubifs_err("directory inode %lu has size %llu, "
927 "but calculated size is %llu", dir
->i_ino
,
928 (unsigned long long)i_size_read(dir
),
929 (unsigned long long)size
);
933 if (dir
->i_nlink
!= nlink
) {
934 ubifs_err("directory inode %lu has nlink %u, but calculated "
935 "nlink is %u", dir
->i_ino
, dir
->i_nlink
, nlink
);
944 * dbg_check_key_order - make sure that colliding keys are properly ordered.
945 * @c: UBIFS file-system description object
946 * @zbr1: first zbranch
947 * @zbr2: following zbranch
949 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
950 * names of the direntries/xentries which are referred by the keys. This
951 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
952 * sure the name of direntry/xentry referred by @zbr1 is less than
953 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
954 * and a negative error code in case of failure.
956 static int dbg_check_key_order(struct ubifs_info
*c
, struct ubifs_zbranch
*zbr1
,
957 struct ubifs_zbranch
*zbr2
)
959 int err
, nlen1
, nlen2
, cmp
;
960 struct ubifs_dent_node
*dent1
, *dent2
;
963 ubifs_assert(!keys_cmp(c
, &zbr1
->key
, &zbr2
->key
));
964 dent1
= kmalloc(UBIFS_MAX_DENT_NODE_SZ
, GFP_NOFS
);
967 dent2
= kmalloc(UBIFS_MAX_DENT_NODE_SZ
, GFP_NOFS
);
973 err
= ubifs_tnc_read_node(c
, zbr1
, dent1
);
976 err
= ubifs_validate_entry(c
, dent1
);
980 err
= ubifs_tnc_read_node(c
, zbr2
, dent2
);
983 err
= ubifs_validate_entry(c
, dent2
);
987 /* Make sure node keys are the same as in zbranch */
989 key_read(c
, &dent1
->key
, &key
);
990 if (keys_cmp(c
, &zbr1
->key
, &key
)) {
991 dbg_err("1st entry at %d:%d has key %s", zbr1
->lnum
,
992 zbr1
->offs
, DBGKEY(&key
));
993 dbg_err("but it should have key %s according to tnc",
995 dbg_dump_node(c
, dent1
);
999 key_read(c
, &dent2
->key
, &key
);
1000 if (keys_cmp(c
, &zbr2
->key
, &key
)) {
1001 dbg_err("2nd entry at %d:%d has key %s", zbr1
->lnum
,
1002 zbr1
->offs
, DBGKEY(&key
));
1003 dbg_err("but it should have key %s according to tnc",
1004 DBGKEY(&zbr2
->key
));
1005 dbg_dump_node(c
, dent2
);
1009 nlen1
= le16_to_cpu(dent1
->nlen
);
1010 nlen2
= le16_to_cpu(dent2
->nlen
);
1012 cmp
= memcmp(dent1
->name
, dent2
->name
, min_t(int, nlen1
, nlen2
));
1013 if (cmp
< 0 || (cmp
== 0 && nlen1
< nlen2
)) {
1017 if (cmp
== 0 && nlen1
== nlen2
)
1018 dbg_err("2 xent/dent nodes with the same name");
1020 dbg_err("bad order of colliding key %s",
1023 dbg_msg("first node at %d:%d\n", zbr1
->lnum
, zbr1
->offs
);
1024 dbg_dump_node(c
, dent1
);
1025 dbg_msg("second node at %d:%d\n", zbr2
->lnum
, zbr2
->offs
);
1026 dbg_dump_node(c
, dent2
);
1035 * dbg_check_znode - check if znode is all right.
1036 * @c: UBIFS file-system description object
1037 * @zbr: zbranch which points to this znode
1039 * This function makes sure that znode referred to by @zbr is all right.
1040 * Returns zero if it is, and %-EINVAL if it is not.
1042 static int dbg_check_znode(struct ubifs_info
*c
, struct ubifs_zbranch
*zbr
)
1044 struct ubifs_znode
*znode
= zbr
->znode
;
1045 struct ubifs_znode
*zp
= znode
->parent
;
1048 if (znode
->child_cnt
<= 0 || znode
->child_cnt
> c
->fanout
) {
1052 if (znode
->level
< 0) {
1056 if (znode
->iip
< 0 || znode
->iip
>= c
->fanout
) {
1062 /* Only dirty zbranch may have no on-flash nodes */
1063 if (!ubifs_zn_dirty(znode
)) {
1068 if (ubifs_zn_dirty(znode
)) {
1070 * If znode is dirty, its parent has to be dirty as well. The
1071 * order of the operation is important, so we have to have
1075 if (zp
&& !ubifs_zn_dirty(zp
)) {
1077 * The dirty flag is atomic and is cleared outside the
1078 * TNC mutex, so znode's dirty flag may now have
1079 * been cleared. The child is always cleared before the
1080 * parent, so we just need to check again.
1083 if (ubifs_zn_dirty(znode
)) {
1091 const union ubifs_key
*min
, *max
;
1093 if (znode
->level
!= zp
->level
- 1) {
1098 /* Make sure the 'parent' pointer in our znode is correct */
1099 err
= ubifs_search_zbranch(c
, zp
, &zbr
->key
, &n
);
1101 /* This zbranch does not exist in the parent */
1106 if (znode
->iip
>= zp
->child_cnt
) {
1111 if (znode
->iip
!= n
) {
1112 /* This may happen only in case of collisions */
1113 if (keys_cmp(c
, &zp
->zbranch
[n
].key
,
1114 &zp
->zbranch
[znode
->iip
].key
)) {
1122 * Make sure that the first key in our znode is greater than or
1123 * equal to the key in the pointing zbranch.
1126 cmp
= keys_cmp(c
, min
, &znode
->zbranch
[0].key
);
1132 if (n
+ 1 < zp
->child_cnt
) {
1133 max
= &zp
->zbranch
[n
+ 1].key
;
1136 * Make sure the last key in our znode is less or
1137 * equivalent than the the key in zbranch which goes
1138 * after our pointing zbranch.
1140 cmp
= keys_cmp(c
, max
,
1141 &znode
->zbranch
[znode
->child_cnt
- 1].key
);
1148 /* This may only be root znode */
1149 if (zbr
!= &c
->zroot
) {
1156 * Make sure that next key is greater or equivalent then the previous
1159 for (n
= 1; n
< znode
->child_cnt
; n
++) {
1160 cmp
= keys_cmp(c
, &znode
->zbranch
[n
- 1].key
,
1161 &znode
->zbranch
[n
].key
);
1167 /* This can only be keys with colliding hash */
1168 if (!is_hash_key(c
, &znode
->zbranch
[n
].key
)) {
1173 if (znode
->level
!= 0 || c
->replaying
)
1177 * Colliding keys should follow binary order of
1178 * corresponding xentry/dentry names.
1180 err
= dbg_check_key_order(c
, &znode
->zbranch
[n
- 1],
1181 &znode
->zbranch
[n
]);
1191 for (n
= 0; n
< znode
->child_cnt
; n
++) {
1192 if (!znode
->zbranch
[n
].znode
&&
1193 (znode
->zbranch
[n
].lnum
== 0 ||
1194 znode
->zbranch
[n
].len
== 0)) {
1199 if (znode
->zbranch
[n
].lnum
!= 0 &&
1200 znode
->zbranch
[n
].len
== 0) {
1205 if (znode
->zbranch
[n
].lnum
== 0 &&
1206 znode
->zbranch
[n
].len
!= 0) {
1211 if (znode
->zbranch
[n
].lnum
== 0 &&
1212 znode
->zbranch
[n
].offs
!= 0) {
1217 if (znode
->level
!= 0 && znode
->zbranch
[n
].znode
)
1218 if (znode
->zbranch
[n
].znode
->parent
!= znode
) {
1227 ubifs_err("failed, error %d", err
);
1228 ubifs_msg("dump of the znode");
1229 dbg_dump_znode(c
, znode
);
1231 ubifs_msg("dump of the parent znode");
1232 dbg_dump_znode(c
, zp
);
1239 * dbg_check_tnc - check TNC tree.
1240 * @c: UBIFS file-system description object
1241 * @extra: do extra checks that are possible at start commit
1243 * This function traverses whole TNC tree and checks every znode. Returns zero
1244 * if everything is all right and %-EINVAL if something is wrong with TNC.
1246 int dbg_check_tnc(struct ubifs_info
*c
, int extra
)
1248 struct ubifs_znode
*znode
;
1249 long clean_cnt
= 0, dirty_cnt
= 0;
1252 if (!(ubifs_chk_flags
& UBIFS_CHK_TNC
))
1255 ubifs_assert(mutex_is_locked(&c
->tnc_mutex
));
1256 if (!c
->zroot
.znode
)
1259 znode
= ubifs_tnc_postorder_first(c
->zroot
.znode
);
1261 struct ubifs_znode
*prev
;
1262 struct ubifs_zbranch
*zbr
;
1267 zbr
= &znode
->parent
->zbranch
[znode
->iip
];
1269 err
= dbg_check_znode(c
, zbr
);
1274 if (ubifs_zn_dirty(znode
))
1281 znode
= ubifs_tnc_postorder_next(znode
);
1286 * If the last key of this znode is equivalent to the first key
1287 * of the next znode (collision), then check order of the keys.
1289 last
= prev
->child_cnt
- 1;
1290 if (prev
->level
== 0 && znode
->level
== 0 && !c
->replaying
&&
1291 !keys_cmp(c
, &prev
->zbranch
[last
].key
,
1292 &znode
->zbranch
[0].key
)) {
1293 err
= dbg_check_key_order(c
, &prev
->zbranch
[last
],
1294 &znode
->zbranch
[0]);
1298 ubifs_msg("first znode");
1299 dbg_dump_znode(c
, prev
);
1300 ubifs_msg("second znode");
1301 dbg_dump_znode(c
, znode
);
1308 if (clean_cnt
!= atomic_long_read(&c
->clean_zn_cnt
)) {
1309 ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld",
1310 atomic_long_read(&c
->clean_zn_cnt
),
1314 if (dirty_cnt
!= atomic_long_read(&c
->dirty_zn_cnt
)) {
1315 ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld",
1316 atomic_long_read(&c
->dirty_zn_cnt
),
1326 * dbg_walk_index - walk the on-flash index.
1327 * @c: UBIFS file-system description object
1328 * @leaf_cb: called for each leaf node
1329 * @znode_cb: called for each indexing node
1330 * @priv: private date which is passed to callbacks
1332 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1333 * node and @znode_cb for each indexing node. Returns zero in case of success
1334 * and a negative error code in case of failure.
1336 * It would be better if this function removed every znode it pulled to into
1337 * the TNC, so that the behavior more closely matched the non-debugging
1340 int dbg_walk_index(struct ubifs_info
*c
, dbg_leaf_callback leaf_cb
,
1341 dbg_znode_callback znode_cb
, void *priv
)
1344 struct ubifs_zbranch
*zbr
;
1345 struct ubifs_znode
*znode
, *child
;
1347 mutex_lock(&c
->tnc_mutex
);
1348 /* If the root indexing node is not in TNC - pull it */
1349 if (!c
->zroot
.znode
) {
1350 c
->zroot
.znode
= ubifs_load_znode(c
, &c
->zroot
, NULL
, 0);
1351 if (IS_ERR(c
->zroot
.znode
)) {
1352 err
= PTR_ERR(c
->zroot
.znode
);
1353 c
->zroot
.znode
= NULL
;
1359 * We are going to traverse the indexing tree in the postorder manner.
1360 * Go down and find the leftmost indexing node where we are going to
1363 znode
= c
->zroot
.znode
;
1364 while (znode
->level
> 0) {
1365 zbr
= &znode
->zbranch
[0];
1368 child
= ubifs_load_znode(c
, zbr
, znode
, 0);
1369 if (IS_ERR(child
)) {
1370 err
= PTR_ERR(child
);
1379 /* Iterate over all indexing nodes */
1386 err
= znode_cb(c
, znode
, priv
);
1388 ubifs_err("znode checking function returned "
1390 dbg_dump_znode(c
, znode
);
1394 if (leaf_cb
&& znode
->level
== 0) {
1395 for (idx
= 0; idx
< znode
->child_cnt
; idx
++) {
1396 zbr
= &znode
->zbranch
[idx
];
1397 err
= leaf_cb(c
, zbr
, priv
);
1399 ubifs_err("leaf checking function "
1400 "returned error %d, for leaf "
1402 err
, zbr
->lnum
, zbr
->offs
);
1411 idx
= znode
->iip
+ 1;
1412 znode
= znode
->parent
;
1413 if (idx
< znode
->child_cnt
) {
1414 /* Switch to the next index in the parent */
1415 zbr
= &znode
->zbranch
[idx
];
1418 child
= ubifs_load_znode(c
, zbr
, znode
, idx
);
1419 if (IS_ERR(child
)) {
1420 err
= PTR_ERR(child
);
1428 * This is the last child, switch to the parent and
1433 /* Go to the lowest leftmost znode in the new sub-tree */
1434 while (znode
->level
> 0) {
1435 zbr
= &znode
->zbranch
[0];
1438 child
= ubifs_load_znode(c
, zbr
, znode
, 0);
1439 if (IS_ERR(child
)) {
1440 err
= PTR_ERR(child
);
1449 mutex_unlock(&c
->tnc_mutex
);
1454 zbr
= &znode
->parent
->zbranch
[znode
->iip
];
1457 ubifs_msg("dump of znode at LEB %d:%d", zbr
->lnum
, zbr
->offs
);
1458 dbg_dump_znode(c
, znode
);
1460 mutex_unlock(&c
->tnc_mutex
);
1465 * add_size - add znode size to partially calculated index size.
1466 * @c: UBIFS file-system description object
1467 * @znode: znode to add size for
1468 * @priv: partially calculated index size
1470 * This is a helper function for 'dbg_check_idx_size()' which is called for
1471 * every indexing node and adds its size to the 'long long' variable pointed to
1474 static int add_size(struct ubifs_info
*c
, struct ubifs_znode
*znode
, void *priv
)
1476 long long *idx_size
= priv
;
1479 add
= ubifs_idx_node_sz(c
, znode
->child_cnt
);
1480 add
= ALIGN(add
, 8);
1486 * dbg_check_idx_size - check index size.
1487 * @c: UBIFS file-system description object
1488 * @idx_size: size to check
1490 * This function walks the UBIFS index, calculates its size and checks that the
1491 * size is equivalent to @idx_size. Returns zero in case of success and a
1492 * negative error code in case of failure.
1494 int dbg_check_idx_size(struct ubifs_info
*c
, long long idx_size
)
1499 if (!(ubifs_chk_flags
& UBIFS_CHK_IDX_SZ
))
1502 err
= dbg_walk_index(c
, NULL
, add_size
, &calc
);
1504 ubifs_err("error %d while walking the index", err
);
1508 if (calc
!= idx_size
) {
1509 ubifs_err("index size check failed: calculated size is %lld, "
1510 "should be %lld", calc
, idx_size
);
1519 * struct fsck_inode - information about an inode used when checking the file-system.
1520 * @rb: link in the RB-tree of inodes
1521 * @inum: inode number
1522 * @mode: inode type, permissions, etc
1523 * @nlink: inode link count
1524 * @xattr_cnt: count of extended attributes
1525 * @references: how many directory/xattr entries refer this inode (calculated
1526 * while walking the index)
1527 * @calc_cnt: for directory inode count of child directories
1528 * @size: inode size (read from on-flash inode)
1529 * @xattr_sz: summary size of all extended attributes (read from on-flash
1531 * @calc_sz: for directories calculated directory size
1532 * @calc_xcnt: count of extended attributes
1533 * @calc_xsz: calculated summary size of all extended attributes
1534 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1535 * inode (read from on-flash inode)
1536 * @calc_xnms: calculated sum of lengths of all extended attribute names
1543 unsigned int xattr_cnt
;
1547 unsigned int xattr_sz
;
1549 long long calc_xcnt
;
1551 unsigned int xattr_nms
;
1552 long long calc_xnms
;
1556 * struct fsck_data - private FS checking information.
1557 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1560 struct rb_root inodes
;
1564 * add_inode - add inode information to RB-tree of inodes.
1565 * @c: UBIFS file-system description object
1566 * @fsckd: FS checking information
1567 * @ino: raw UBIFS inode to add
1569 * This is a helper function for 'check_leaf()' which adds information about
1570 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1571 * case of success and a negative error code in case of failure.
1573 static struct fsck_inode
*add_inode(struct ubifs_info
*c
,
1574 struct fsck_data
*fsckd
,
1575 struct ubifs_ino_node
*ino
)
1577 struct rb_node
**p
, *parent
= NULL
;
1578 struct fsck_inode
*fscki
;
1579 ino_t inum
= key_inum_flash(c
, &ino
->key
);
1581 p
= &fsckd
->inodes
.rb_node
;
1584 fscki
= rb_entry(parent
, struct fsck_inode
, rb
);
1585 if (inum
< fscki
->inum
)
1587 else if (inum
> fscki
->inum
)
1588 p
= &(*p
)->rb_right
;
1593 if (inum
> c
->highest_inum
) {
1594 ubifs_err("too high inode number, max. is %lu",
1595 (unsigned long)c
->highest_inum
);
1596 return ERR_PTR(-EINVAL
);
1599 fscki
= kzalloc(sizeof(struct fsck_inode
), GFP_NOFS
);
1601 return ERR_PTR(-ENOMEM
);
1604 fscki
->nlink
= le32_to_cpu(ino
->nlink
);
1605 fscki
->size
= le64_to_cpu(ino
->size
);
1606 fscki
->xattr_cnt
= le32_to_cpu(ino
->xattr_cnt
);
1607 fscki
->xattr_sz
= le32_to_cpu(ino
->xattr_size
);
1608 fscki
->xattr_nms
= le32_to_cpu(ino
->xattr_names
);
1609 fscki
->mode
= le32_to_cpu(ino
->mode
);
1610 if (S_ISDIR(fscki
->mode
)) {
1611 fscki
->calc_sz
= UBIFS_INO_NODE_SZ
;
1612 fscki
->calc_cnt
= 2;
1614 rb_link_node(&fscki
->rb
, parent
, p
);
1615 rb_insert_color(&fscki
->rb
, &fsckd
->inodes
);
1620 * search_inode - search inode in the RB-tree of inodes.
1621 * @fsckd: FS checking information
1622 * @inum: inode number to search
1624 * This is a helper function for 'check_leaf()' which searches inode @inum in
1625 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1626 * the inode was not found.
1628 static struct fsck_inode
*search_inode(struct fsck_data
*fsckd
, ino_t inum
)
1631 struct fsck_inode
*fscki
;
1633 p
= fsckd
->inodes
.rb_node
;
1635 fscki
= rb_entry(p
, struct fsck_inode
, rb
);
1636 if (inum
< fscki
->inum
)
1638 else if (inum
> fscki
->inum
)
1647 * read_add_inode - read inode node and add it to RB-tree of inodes.
1648 * @c: UBIFS file-system description object
1649 * @fsckd: FS checking information
1650 * @inum: inode number to read
1652 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1653 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1654 * information pointer in case of success and a negative error code in case of
1657 static struct fsck_inode
*read_add_inode(struct ubifs_info
*c
,
1658 struct fsck_data
*fsckd
, ino_t inum
)
1661 union ubifs_key key
;
1662 struct ubifs_znode
*znode
;
1663 struct ubifs_zbranch
*zbr
;
1664 struct ubifs_ino_node
*ino
;
1665 struct fsck_inode
*fscki
;
1667 fscki
= search_inode(fsckd
, inum
);
1671 ino_key_init(c
, &key
, inum
);
1672 err
= ubifs_lookup_level0(c
, &key
, &znode
, &n
);
1674 ubifs_err("inode %lu not found in index", (unsigned long)inum
);
1675 return ERR_PTR(-ENOENT
);
1676 } else if (err
< 0) {
1677 ubifs_err("error %d while looking up inode %lu",
1678 err
, (unsigned long)inum
);
1679 return ERR_PTR(err
);
1682 zbr
= &znode
->zbranch
[n
];
1683 if (zbr
->len
< UBIFS_INO_NODE_SZ
) {
1684 ubifs_err("bad node %lu node length %d",
1685 (unsigned long)inum
, zbr
->len
);
1686 return ERR_PTR(-EINVAL
);
1689 ino
= kmalloc(zbr
->len
, GFP_NOFS
);
1691 return ERR_PTR(-ENOMEM
);
1693 err
= ubifs_tnc_read_node(c
, zbr
, ino
);
1695 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
1696 zbr
->lnum
, zbr
->offs
, err
);
1698 return ERR_PTR(err
);
1701 fscki
= add_inode(c
, fsckd
, ino
);
1703 if (IS_ERR(fscki
)) {
1704 ubifs_err("error %ld while adding inode %lu node",
1705 PTR_ERR(fscki
), (unsigned long)inum
);
1713 * check_leaf - check leaf node.
1714 * @c: UBIFS file-system description object
1715 * @zbr: zbranch of the leaf node to check
1716 * @priv: FS checking information
1718 * This is a helper function for 'dbg_check_filesystem()' which is called for
1719 * every single leaf node while walking the indexing tree. It checks that the
1720 * leaf node referred from the indexing tree exists, has correct CRC, and does
1721 * some other basic validation. This function is also responsible for building
1722 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
1723 * calculates reference count, size, etc for each inode in order to later
1724 * compare them to the information stored inside the inodes and detect possible
1725 * inconsistencies. Returns zero in case of success and a negative error code
1726 * in case of failure.
1728 static int check_leaf(struct ubifs_info
*c
, struct ubifs_zbranch
*zbr
,
1733 struct ubifs_ch
*ch
;
1734 int err
, type
= key_type(c
, &zbr
->key
);
1735 struct fsck_inode
*fscki
;
1737 if (zbr
->len
< UBIFS_CH_SZ
) {
1738 ubifs_err("bad leaf length %d (LEB %d:%d)",
1739 zbr
->len
, zbr
->lnum
, zbr
->offs
);
1743 node
= kmalloc(zbr
->len
, GFP_NOFS
);
1747 err
= ubifs_tnc_read_node(c
, zbr
, node
);
1749 ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
1750 zbr
->lnum
, zbr
->offs
, err
);
1754 /* If this is an inode node, add it to RB-tree of inodes */
1755 if (type
== UBIFS_INO_KEY
) {
1756 fscki
= add_inode(c
, priv
, node
);
1757 if (IS_ERR(fscki
)) {
1758 err
= PTR_ERR(fscki
);
1759 ubifs_err("error %d while adding inode node", err
);
1765 if (type
!= UBIFS_DENT_KEY
&& type
!= UBIFS_XENT_KEY
&&
1766 type
!= UBIFS_DATA_KEY
) {
1767 ubifs_err("unexpected node type %d at LEB %d:%d",
1768 type
, zbr
->lnum
, zbr
->offs
);
1774 if (le64_to_cpu(ch
->sqnum
) > c
->max_sqnum
) {
1775 ubifs_err("too high sequence number, max. is %llu",
1781 if (type
== UBIFS_DATA_KEY
) {
1783 struct ubifs_data_node
*dn
= node
;
1786 * Search the inode node this data node belongs to and insert
1787 * it to the RB-tree of inodes.
1789 inum
= key_inum_flash(c
, &dn
->key
);
1790 fscki
= read_add_inode(c
, priv
, inum
);
1791 if (IS_ERR(fscki
)) {
1792 err
= PTR_ERR(fscki
);
1793 ubifs_err("error %d while processing data node and "
1794 "trying to find inode node %lu",
1795 err
, (unsigned long)inum
);
1799 /* Make sure the data node is within inode size */
1800 blk_offs
= key_block_flash(c
, &dn
->key
);
1801 blk_offs
<<= UBIFS_BLOCK_SHIFT
;
1802 blk_offs
+= le32_to_cpu(dn
->size
);
1803 if (blk_offs
> fscki
->size
) {
1804 ubifs_err("data node at LEB %d:%d is not within inode "
1805 "size %lld", zbr
->lnum
, zbr
->offs
,
1812 struct ubifs_dent_node
*dent
= node
;
1813 struct fsck_inode
*fscki1
;
1815 err
= ubifs_validate_entry(c
, dent
);
1820 * Search the inode node this entry refers to and the parent
1821 * inode node and insert them to the RB-tree of inodes.
1823 inum
= le64_to_cpu(dent
->inum
);
1824 fscki
= read_add_inode(c
, priv
, inum
);
1825 if (IS_ERR(fscki
)) {
1826 err
= PTR_ERR(fscki
);
1827 ubifs_err("error %d while processing entry node and "
1828 "trying to find inode node %lu",
1829 err
, (unsigned long)inum
);
1833 /* Count how many direntries or xentries refers this inode */
1834 fscki
->references
+= 1;
1836 inum
= key_inum_flash(c
, &dent
->key
);
1837 fscki1
= read_add_inode(c
, priv
, inum
);
1838 if (IS_ERR(fscki1
)) {
1839 err
= PTR_ERR(fscki
);
1840 ubifs_err("error %d while processing entry node and "
1841 "trying to find parent inode node %lu",
1842 err
, (unsigned long)inum
);
1846 nlen
= le16_to_cpu(dent
->nlen
);
1847 if (type
== UBIFS_XENT_KEY
) {
1848 fscki1
->calc_xcnt
+= 1;
1849 fscki1
->calc_xsz
+= CALC_DENT_SIZE(nlen
);
1850 fscki1
->calc_xsz
+= CALC_XATTR_BYTES(fscki
->size
);
1851 fscki1
->calc_xnms
+= nlen
;
1853 fscki1
->calc_sz
+= CALC_DENT_SIZE(nlen
);
1854 if (dent
->type
== UBIFS_ITYPE_DIR
)
1855 fscki1
->calc_cnt
+= 1;
1864 ubifs_msg("dump of node at LEB %d:%d", zbr
->lnum
, zbr
->offs
);
1865 dbg_dump_node(c
, node
);
1872 * free_inodes - free RB-tree of inodes.
1873 * @fsckd: FS checking information
1875 static void free_inodes(struct fsck_data
*fsckd
)
1877 struct rb_node
*this = fsckd
->inodes
.rb_node
;
1878 struct fsck_inode
*fscki
;
1882 this = this->rb_left
;
1883 else if (this->rb_right
)
1884 this = this->rb_right
;
1886 fscki
= rb_entry(this, struct fsck_inode
, rb
);
1887 this = rb_parent(this);
1889 if (this->rb_left
== &fscki
->rb
)
1890 this->rb_left
= NULL
;
1892 this->rb_right
= NULL
;
1900 * check_inodes - checks all inodes.
1901 * @c: UBIFS file-system description object
1902 * @fsckd: FS checking information
1904 * This is a helper function for 'dbg_check_filesystem()' which walks the
1905 * RB-tree of inodes after the index scan has been finished, and checks that
1906 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
1907 * %-EINVAL if not, and a negative error code in case of failure.
1909 static int check_inodes(struct ubifs_info
*c
, struct fsck_data
*fsckd
)
1912 union ubifs_key key
;
1913 struct ubifs_znode
*znode
;
1914 struct ubifs_zbranch
*zbr
;
1915 struct ubifs_ino_node
*ino
;
1916 struct fsck_inode
*fscki
;
1917 struct rb_node
*this = rb_first(&fsckd
->inodes
);
1920 fscki
= rb_entry(this, struct fsck_inode
, rb
);
1921 this = rb_next(this);
1923 if (S_ISDIR(fscki
->mode
)) {
1925 * Directories have to have exactly one reference (they
1926 * cannot have hardlinks), although root inode is an
1929 if (fscki
->inum
!= UBIFS_ROOT_INO
&&
1930 fscki
->references
!= 1) {
1931 ubifs_err("directory inode %lu has %d "
1932 "direntries which refer it, but "
1934 (unsigned long)fscki
->inum
,
1938 if (fscki
->inum
== UBIFS_ROOT_INO
&&
1939 fscki
->references
!= 0) {
1940 ubifs_err("root inode %lu has non-zero (%d) "
1941 "direntries which refer it",
1942 (unsigned long)fscki
->inum
,
1946 if (fscki
->calc_sz
!= fscki
->size
) {
1947 ubifs_err("directory inode %lu size is %lld, "
1948 "but calculated size is %lld",
1949 (unsigned long)fscki
->inum
,
1950 fscki
->size
, fscki
->calc_sz
);
1953 if (fscki
->calc_cnt
!= fscki
->nlink
) {
1954 ubifs_err("directory inode %lu nlink is %d, "
1955 "but calculated nlink is %d",
1956 (unsigned long)fscki
->inum
,
1957 fscki
->nlink
, fscki
->calc_cnt
);
1961 if (fscki
->references
!= fscki
->nlink
) {
1962 ubifs_err("inode %lu nlink is %d, but "
1963 "calculated nlink is %d",
1964 (unsigned long)fscki
->inum
,
1965 fscki
->nlink
, fscki
->references
);
1969 if (fscki
->xattr_sz
!= fscki
->calc_xsz
) {
1970 ubifs_err("inode %lu has xattr size %u, but "
1971 "calculated size is %lld",
1972 (unsigned long)fscki
->inum
, fscki
->xattr_sz
,
1976 if (fscki
->xattr_cnt
!= fscki
->calc_xcnt
) {
1977 ubifs_err("inode %lu has %u xattrs, but "
1978 "calculated count is %lld",
1979 (unsigned long)fscki
->inum
,
1980 fscki
->xattr_cnt
, fscki
->calc_xcnt
);
1983 if (fscki
->xattr_nms
!= fscki
->calc_xnms
) {
1984 ubifs_err("inode %lu has xattr names' size %u, but "
1985 "calculated names' size is %lld",
1986 (unsigned long)fscki
->inum
, fscki
->xattr_nms
,
1995 /* Read the bad inode and dump it */
1996 ino_key_init(c
, &key
, fscki
->inum
);
1997 err
= ubifs_lookup_level0(c
, &key
, &znode
, &n
);
1999 ubifs_err("inode %lu not found in index",
2000 (unsigned long)fscki
->inum
);
2002 } else if (err
< 0) {
2003 ubifs_err("error %d while looking up inode %lu",
2004 err
, (unsigned long)fscki
->inum
);
2008 zbr
= &znode
->zbranch
[n
];
2009 ino
= kmalloc(zbr
->len
, GFP_NOFS
);
2013 err
= ubifs_tnc_read_node(c
, zbr
, ino
);
2015 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2016 zbr
->lnum
, zbr
->offs
, err
);
2021 ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",
2022 (unsigned long)fscki
->inum
, zbr
->lnum
, zbr
->offs
);
2023 dbg_dump_node(c
, ino
);
2029 * dbg_check_filesystem - check the file-system.
2030 * @c: UBIFS file-system description object
2032 * This function checks the file system, namely:
2033 * o makes sure that all leaf nodes exist and their CRCs are correct;
2034 * o makes sure inode nlink, size, xattr size/count are correct (for all
2037 * The function reads whole indexing tree and all nodes, so it is pretty
2038 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2039 * not, and a negative error code in case of failure.
2041 int dbg_check_filesystem(struct ubifs_info
*c
)
2044 struct fsck_data fsckd
;
2046 if (!(ubifs_chk_flags
& UBIFS_CHK_FS
))
2049 fsckd
.inodes
= RB_ROOT
;
2050 err
= dbg_walk_index(c
, check_leaf
, NULL
, &fsckd
);
2054 err
= check_inodes(c
, &fsckd
);
2058 free_inodes(&fsckd
);
2062 ubifs_err("file-system check failed with error %d", err
);
2064 free_inodes(&fsckd
);
2068 static int invocation_cnt
;
2070 int dbg_force_in_the_gaps(void)
2072 if (!dbg_force_in_the_gaps_enabled
)
2074 /* Force in-the-gaps every 8th commit */
2075 return !((invocation_cnt
++) & 0x7);
2078 /* Failure mode for recovery testing */
2080 #define chance(n, d) (simple_rand() <= (n) * 32768LL / (d))
2082 struct failure_mode_info
{
2083 struct list_head list
;
2084 struct ubifs_info
*c
;
2087 static LIST_HEAD(fmi_list
);
2088 static DEFINE_SPINLOCK(fmi_lock
);
2090 static unsigned int next
;
2092 static int simple_rand(void)
2095 next
= current
->pid
;
2096 next
= next
* 1103515245 + 12345;
2097 return (next
>> 16) & 32767;
2100 void dbg_failure_mode_registration(struct ubifs_info
*c
)
2102 struct failure_mode_info
*fmi
;
2104 fmi
= kmalloc(sizeof(struct failure_mode_info
), GFP_NOFS
);
2106 dbg_err("Failed to register failure mode - no memory");
2110 spin_lock(&fmi_lock
);
2111 list_add_tail(&fmi
->list
, &fmi_list
);
2112 spin_unlock(&fmi_lock
);
2115 void dbg_failure_mode_deregistration(struct ubifs_info
*c
)
2117 struct failure_mode_info
*fmi
, *tmp
;
2119 spin_lock(&fmi_lock
);
2120 list_for_each_entry_safe(fmi
, tmp
, &fmi_list
, list
)
2122 list_del(&fmi
->list
);
2125 spin_unlock(&fmi_lock
);
2128 static struct ubifs_info
*dbg_find_info(struct ubi_volume_desc
*desc
)
2130 struct failure_mode_info
*fmi
;
2132 spin_lock(&fmi_lock
);
2133 list_for_each_entry(fmi
, &fmi_list
, list
)
2134 if (fmi
->c
->ubi
== desc
) {
2135 struct ubifs_info
*c
= fmi
->c
;
2137 spin_unlock(&fmi_lock
);
2140 spin_unlock(&fmi_lock
);
2144 static int in_failure_mode(struct ubi_volume_desc
*desc
)
2146 struct ubifs_info
*c
= dbg_find_info(desc
);
2148 if (c
&& dbg_failure_mode
)
2149 return c
->failure_mode
;
2153 static int do_fail(struct ubi_volume_desc
*desc
, int lnum
, int write
)
2155 struct ubifs_info
*c
= dbg_find_info(desc
);
2157 if (!c
|| !dbg_failure_mode
)
2159 if (c
->failure_mode
)
2162 /* First call - decide delay to failure */
2164 unsigned int delay
= 1 << (simple_rand() >> 11);
2168 c
->fail_timeout
= jiffies
+
2169 msecs_to_jiffies(delay
);
2170 dbg_rcvry("failing after %ums", delay
);
2173 c
->fail_cnt_max
= delay
;
2174 dbg_rcvry("failing after %u calls", delay
);
2179 /* Determine if failure delay has expired */
2180 if (c
->fail_delay
== 1) {
2181 if (time_before(jiffies
, c
->fail_timeout
))
2183 } else if (c
->fail_delay
== 2)
2184 if (c
->fail_cnt
++ < c
->fail_cnt_max
)
2186 if (lnum
== UBIFS_SB_LNUM
) {
2190 } else if (chance(19, 20))
2192 dbg_rcvry("failing in super block LEB %d", lnum
);
2193 } else if (lnum
== UBIFS_MST_LNUM
|| lnum
== UBIFS_MST_LNUM
+ 1) {
2196 dbg_rcvry("failing in master LEB %d", lnum
);
2197 } else if (lnum
>= UBIFS_LOG_LNUM
&& lnum
<= c
->log_last
) {
2199 if (chance(99, 100))
2201 } else if (chance(399, 400))
2203 dbg_rcvry("failing in log LEB %d", lnum
);
2204 } else if (lnum
>= c
->lpt_first
&& lnum
<= c
->lpt_last
) {
2208 } else if (chance(19, 20))
2210 dbg_rcvry("failing in LPT LEB %d", lnum
);
2211 } else if (lnum
>= c
->orph_first
&& lnum
<= c
->orph_last
) {
2215 } else if (chance(9, 10))
2217 dbg_rcvry("failing in orphan LEB %d", lnum
);
2218 } else if (lnum
== c
->ihead_lnum
) {
2219 if (chance(99, 100))
2221 dbg_rcvry("failing in index head LEB %d", lnum
);
2222 } else if (c
->jheads
&& lnum
== c
->jheads
[GCHD
].wbuf
.lnum
) {
2225 dbg_rcvry("failing in GC head LEB %d", lnum
);
2226 } else if (write
&& !RB_EMPTY_ROOT(&c
->buds
) &&
2227 !ubifs_search_bud(c
, lnum
)) {
2230 dbg_rcvry("failing in non-bud LEB %d", lnum
);
2231 } else if (c
->cmt_state
== COMMIT_RUNNING_BACKGROUND
||
2232 c
->cmt_state
== COMMIT_RUNNING_REQUIRED
) {
2233 if (chance(999, 1000))
2235 dbg_rcvry("failing in bud LEB %d commit running", lnum
);
2237 if (chance(9999, 10000))
2239 dbg_rcvry("failing in bud LEB %d commit not running", lnum
);
2241 ubifs_err("*** SETTING FAILURE MODE ON (LEB %d) ***", lnum
);
2242 c
->failure_mode
= 1;
2247 static void cut_data(const void *buf
, int len
)
2250 unsigned char *p
= (void *)buf
;
2252 flen
= (len
* (long long)simple_rand()) >> 15;
2253 for (i
= flen
; i
< len
; i
++)
2257 int dbg_leb_read(struct ubi_volume_desc
*desc
, int lnum
, char *buf
, int offset
,
2260 if (in_failure_mode(desc
))
2262 return ubi_leb_read(desc
, lnum
, buf
, offset
, len
, check
);
2265 int dbg_leb_write(struct ubi_volume_desc
*desc
, int lnum
, const void *buf
,
2266 int offset
, int len
, int dtype
)
2270 if (in_failure_mode(desc
))
2272 failing
= do_fail(desc
, lnum
, 1);
2275 err
= ubi_leb_write(desc
, lnum
, buf
, offset
, len
, dtype
);
2283 int dbg_leb_change(struct ubi_volume_desc
*desc
, int lnum
, const void *buf
,
2288 if (do_fail(desc
, lnum
, 1))
2290 err
= ubi_leb_change(desc
, lnum
, buf
, len
, dtype
);
2293 if (do_fail(desc
, lnum
, 1))
2298 int dbg_leb_erase(struct ubi_volume_desc
*desc
, int lnum
)
2302 if (do_fail(desc
, lnum
, 0))
2304 err
= ubi_leb_erase(desc
, lnum
);
2307 if (do_fail(desc
, lnum
, 0))
2312 int dbg_leb_unmap(struct ubi_volume_desc
*desc
, int lnum
)
2316 if (do_fail(desc
, lnum
, 0))
2318 err
= ubi_leb_unmap(desc
, lnum
);
2321 if (do_fail(desc
, lnum
, 0))
2326 int dbg_is_mapped(struct ubi_volume_desc
*desc
, int lnum
)
2328 if (in_failure_mode(desc
))
2330 return ubi_is_mapped(desc
, lnum
);
2333 int dbg_leb_map(struct ubi_volume_desc
*desc
, int lnum
, int dtype
)
2337 if (do_fail(desc
, lnum
, 0))
2339 err
= ubi_leb_map(desc
, lnum
, dtype
);
2342 if (do_fail(desc
, lnum
, 0))
2347 #endif /* CONFIG_UBIFS_FS_DEBUG */