thinkpad-acpi: Fix procfs hotkey reset command
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ubifs / debug.c
blob510ffa0bbda4658559cf682a01145d4ff7f9231e
1 /*
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
13 * more details.
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 (Битюцкий Артём)
20 * Adrian Hunter
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
32 #include "ubifs.h"
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)
57 switch (fmt) {
58 case UBIFS_SIMPLE_KEY_FMT:
59 return "simple";
60 default:
61 return "unknown/invalid format";
65 static const char *get_key_hash(int hash)
67 switch (hash) {
68 case UBIFS_KEY_HASH_R5:
69 return "R5";
70 case UBIFS_KEY_HASH_TEST:
71 return "test";
72 default:
73 return "unknown/invalid name hash";
77 static const char *get_key_type(int type)
79 switch (type) {
80 case UBIFS_INO_KEY:
81 return "inode";
82 case UBIFS_DENT_KEY:
83 return "direntry";
84 case UBIFS_XENT_KEY:
85 return "xentry";
86 case UBIFS_DATA_KEY:
87 return "data";
88 case UBIFS_TRUN_KEY:
89 return "truncate";
90 default:
91 return "unknown/invalid key";
95 static void sprintf_key(const struct ubifs_info *c, const union ubifs_key *key,
96 char *buffer)
98 char *p = buffer;
99 int type = key_type(c, key);
101 if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
102 switch (type) {
103 case UBIFS_INO_KEY:
104 sprintf(p, "(%lu, %s)", (unsigned long)key_inum(c, key),
105 get_key_type(type));
106 break;
107 case UBIFS_DENT_KEY:
108 case UBIFS_XENT_KEY:
109 sprintf(p, "(%lu, %s, %#08x)",
110 (unsigned long)key_inum(c, key),
111 get_key_type(type), key_hash(c, key));
112 break;
113 case UBIFS_DATA_KEY:
114 sprintf(p, "(%lu, %s, %u)",
115 (unsigned long)key_inum(c, key),
116 get_key_type(type), key_block(c, key));
117 break;
118 case UBIFS_TRUN_KEY:
119 sprintf(p, "(%lu, %s)",
120 (unsigned long)key_inum(c, key),
121 get_key_type(type));
122 break;
123 default:
124 sprintf(p, "(bad key type: %#08x, %#08x)",
125 key->u32[0], key->u32[1]);
127 } else
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);
135 return 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);
142 return dbg_key_buf1;
145 const char *dbg_ntype(int type)
147 switch (type) {
148 case UBIFS_PAD_NODE:
149 return "padding node";
150 case UBIFS_SB_NODE:
151 return "superblock node";
152 case UBIFS_MST_NODE:
153 return "master node";
154 case UBIFS_REF_NODE:
155 return "reference node";
156 case UBIFS_INO_NODE:
157 return "inode node";
158 case UBIFS_DENT_NODE:
159 return "direntry node";
160 case UBIFS_XENT_NODE:
161 return "xentry node";
162 case UBIFS_DATA_NODE:
163 return "data node";
164 case UBIFS_TRUN_NODE:
165 return "truncate node";
166 case UBIFS_IDX_NODE:
167 return "indexing node";
168 case UBIFS_CS_NODE:
169 return "commit start node";
170 case UBIFS_ORPH_NODE:
171 return "orphan node";
172 default:
173 return "unknown node";
177 static const char *dbg_gtype(int type)
179 switch (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";
186 default:
187 return "unknown";
191 const char *dbg_cstate(int cmt_state)
193 switch (cmt_state) {
194 case COMMIT_RESTING:
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";
204 case COMMIT_BROKEN:
205 return "broken commit";
206 default:
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)
264 int i, n;
265 union ubifs_key key;
266 const struct ubifs_ch *ch = node;
268 if (dbg_failure_mode)
269 return;
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);
276 return;
279 spin_lock(&dbg_lock);
280 dump_ch(node);
282 switch (ch->node_type) {
283 case UBIFS_PAD_NODE:
285 const struct ubifs_pad_node *pad = node;
287 printk(KERN_DEBUG "\tpad_len %u\n",
288 le32_to_cpu(pad->pad_len));
289 break;
291 case UBIFS_SB_NODE:
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],
343 sup->uuid[15]);
344 break;
346 case UBIFS_MST_NODE:
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));
406 break;
408 case UBIFS_REF_NODE:
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));
418 break;
420 case UBIFS_INO_NODE:
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));
459 break;
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)");
478 else {
479 for (i = 0; i < nlen && dent->name[i]; i++)
480 printk("%c", dent->name[i]);
482 printk("\n");
484 break;
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",
498 dlen);
499 printk(KERN_DEBUG "\tdata:\n");
500 print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET, 32, 1,
501 (void *)&dn->data, dlen, 0);
502 break;
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));
514 break;
516 case UBIFS_IDX_NODE:
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));
535 break;
537 case UBIFS_CS_NODE:
538 break;
539 case UBIFS_ORPH_NODE:
541 const struct ubifs_orph_node *orph = node;
543 printk(KERN_DEBUG "\tcommit number %llu\n",
544 (unsigned long long)
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]));
553 break;
555 default:
556 printk(KERN_DEBUG "node type %d was not recognized\n",
557 (int)ch->node_type);
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,
586 lst->total_dirty);
587 printk(KERN_DEBUG "\ttotal_used %lld, total_dark %lld, "
588 "total_dead %lld\n", lst->total_used, lst->total_dark,
589 lst->total_dead);
590 spin_unlock(&dbg_lock);
593 void dbg_dump_budg(struct ubifs_info *c)
595 int i;
596 struct rb_node *rb;
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,
607 c->freeable_cnt);
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)
644 int lnum, err;
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);
654 if (err)
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)
663 int i;
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);
688 if (c->big_lpt)
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)
704 return;
706 printk(KERN_DEBUG "(pid %d) Dumping LEB %d\n", current->pid, lnum);
708 sleb = ubifs_scan(c, lnum, 0, c->dbg_buf);
709 if (IS_ERR(sleb)) {
710 ubifs_err("scan error %d", (int)PTR_ERR(sleb));
711 return;
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) {
718 cond_resched();
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);
725 return;
728 void dbg_dump_znode(const struct ubifs_info *c,
729 const struct ubifs_znode *znode)
731 int n;
732 const struct ubifs_zbranch *zbr;
734 spin_lock(&dbg_lock);
735 if (znode->parent)
736 zbr = &znode->parent->zbranch[znode->iip];
737 else
738 zbr = &c->zroot;
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);
747 return;
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,
756 zbr->offs, zbr->len,
757 DBGKEY(&zbr->key));
758 else
759 printk(KERN_DEBUG "\t%d: LNC %p LEB %d:%d len %d key "
760 "%s\n", n, zbr->znode, zbr->lnum,
761 zbr->offs, zbr->len,
762 DBGKEY(&zbr->key));
764 spin_unlock(&dbg_lock);
767 void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
769 int i;
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)
785 int i;
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;
803 int level;
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);
810 while (znode) {
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,
823 void *priv)
825 dbg_dump_znode(c, znode);
826 return 0;
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
848 * %-EINVAL if not.
850 int dbg_check_synced_i_size(struct inode *inode)
852 int err = 0;
853 struct ubifs_inode *ui = ubifs_inode(inode);
855 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
856 return 0;
857 if (!S_ISREG(inode->i_mode))
858 return 0;
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));
867 dbg_dump_stack();
868 err = -EINVAL;
870 spin_unlock(&ui->ui_lock);
871 mutex_unlock(&ui->ui_mutex);
872 return err;
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
883 * failure.
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;
891 union ubifs_key key;
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))
897 return 0;
899 if (!S_ISDIR(dir->i_mode))
900 return 0;
902 lowest_dent_key(c, &key, dir->i_ino);
903 while (1) {
904 int err;
906 dent = ubifs_tnc_next_ent(c, &key, &nm);
907 if (IS_ERR(dent)) {
908 err = PTR_ERR(dent);
909 if (err == -ENOENT)
910 break;
911 return err;
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)
918 nlink += 1;
919 kfree(pdent);
920 pdent = dent;
921 key_read(c, &dent->key, &key);
923 kfree(pdent);
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);
930 dump_stack();
931 return -EINVAL;
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);
936 dump_stack();
937 return -EINVAL;
940 return 0;
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;
961 union ubifs_key key;
963 ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
964 dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
965 if (!dent1)
966 return -ENOMEM;
967 dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
968 if (!dent2) {
969 err = -ENOMEM;
970 goto out_free;
973 err = ubifs_tnc_read_node(c, zbr1, dent1);
974 if (err)
975 goto out_free;
976 err = ubifs_validate_entry(c, dent1);
977 if (err)
978 goto out_free;
980 err = ubifs_tnc_read_node(c, zbr2, dent2);
981 if (err)
982 goto out_free;
983 err = ubifs_validate_entry(c, dent2);
984 if (err)
985 goto out_free;
987 /* Make sure node keys are the same as in zbranch */
988 err = 1;
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",
994 DBGKEY(&zbr1->key));
995 dbg_dump_node(c, dent1);
996 goto out_free;
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);
1006 goto out_free;
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)) {
1014 err = 0;
1015 goto out_free;
1017 if (cmp == 0 && nlen1 == nlen2)
1018 dbg_err("2 xent/dent nodes with the same name");
1019 else
1020 dbg_err("bad order of colliding key %s",
1021 DBGKEY(&key));
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);
1028 out_free:
1029 kfree(dent2);
1030 kfree(dent1);
1031 return err;
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;
1046 int n, err, cmp;
1048 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
1049 err = 1;
1050 goto out;
1052 if (znode->level < 0) {
1053 err = 2;
1054 goto out;
1056 if (znode->iip < 0 || znode->iip >= c->fanout) {
1057 err = 3;
1058 goto out;
1061 if (zbr->len == 0)
1062 /* Only dirty zbranch may have no on-flash nodes */
1063 if (!ubifs_zn_dirty(znode)) {
1064 err = 4;
1065 goto out;
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
1072 * memory barriers.
1074 smp_mb();
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.
1082 smp_mb();
1083 if (ubifs_zn_dirty(znode)) {
1084 err = 5;
1085 goto out;
1090 if (zp) {
1091 const union ubifs_key *min, *max;
1093 if (znode->level != zp->level - 1) {
1094 err = 6;
1095 goto out;
1098 /* Make sure the 'parent' pointer in our znode is correct */
1099 err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
1100 if (!err) {
1101 /* This zbranch does not exist in the parent */
1102 err = 7;
1103 goto out;
1106 if (znode->iip >= zp->child_cnt) {
1107 err = 8;
1108 goto out;
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)) {
1115 err = 9;
1116 goto out;
1118 n = znode->iip;
1122 * Make sure that the first key in our znode is greater than or
1123 * equal to the key in the pointing zbranch.
1125 min = &zbr->key;
1126 cmp = keys_cmp(c, min, &znode->zbranch[0].key);
1127 if (cmp == 1) {
1128 err = 10;
1129 goto out;
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);
1142 if (cmp == -1) {
1143 err = 11;
1144 goto out;
1147 } else {
1148 /* This may only be root znode */
1149 if (zbr != &c->zroot) {
1150 err = 12;
1151 goto out;
1156 * Make sure that next key is greater or equivalent then the previous
1157 * one.
1159 for (n = 1; n < znode->child_cnt; n++) {
1160 cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
1161 &znode->zbranch[n].key);
1162 if (cmp > 0) {
1163 err = 13;
1164 goto out;
1166 if (cmp == 0) {
1167 /* This can only be keys with colliding hash */
1168 if (!is_hash_key(c, &znode->zbranch[n].key)) {
1169 err = 14;
1170 goto out;
1173 if (znode->level != 0 || c->replaying)
1174 continue;
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]);
1182 if (err < 0)
1183 return err;
1184 if (err) {
1185 err = 15;
1186 goto out;
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)) {
1195 err = 16;
1196 goto out;
1199 if (znode->zbranch[n].lnum != 0 &&
1200 znode->zbranch[n].len == 0) {
1201 err = 17;
1202 goto out;
1205 if (znode->zbranch[n].lnum == 0 &&
1206 znode->zbranch[n].len != 0) {
1207 err = 18;
1208 goto out;
1211 if (znode->zbranch[n].lnum == 0 &&
1212 znode->zbranch[n].offs != 0) {
1213 err = 19;
1214 goto out;
1217 if (znode->level != 0 && znode->zbranch[n].znode)
1218 if (znode->zbranch[n].znode->parent != znode) {
1219 err = 20;
1220 goto out;
1224 return 0;
1226 out:
1227 ubifs_err("failed, error %d", err);
1228 ubifs_msg("dump of the znode");
1229 dbg_dump_znode(c, znode);
1230 if (zp) {
1231 ubifs_msg("dump of the parent znode");
1232 dbg_dump_znode(c, zp);
1234 dump_stack();
1235 return -EINVAL;
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;
1250 int err, last;
1252 if (!(ubifs_chk_flags & UBIFS_CHK_TNC))
1253 return 0;
1255 ubifs_assert(mutex_is_locked(&c->tnc_mutex));
1256 if (!c->zroot.znode)
1257 return 0;
1259 znode = ubifs_tnc_postorder_first(c->zroot.znode);
1260 while (1) {
1261 struct ubifs_znode *prev;
1262 struct ubifs_zbranch *zbr;
1264 if (!znode->parent)
1265 zbr = &c->zroot;
1266 else
1267 zbr = &znode->parent->zbranch[znode->iip];
1269 err = dbg_check_znode(c, zbr);
1270 if (err)
1271 return err;
1273 if (extra) {
1274 if (ubifs_zn_dirty(znode))
1275 dirty_cnt += 1;
1276 else
1277 clean_cnt += 1;
1280 prev = znode;
1281 znode = ubifs_tnc_postorder_next(znode);
1282 if (!znode)
1283 break;
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]);
1295 if (err < 0)
1296 return err;
1297 if (err) {
1298 ubifs_msg("first znode");
1299 dbg_dump_znode(c, prev);
1300 ubifs_msg("second znode");
1301 dbg_dump_znode(c, znode);
1302 return -EINVAL;
1307 if (extra) {
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),
1311 clean_cnt);
1312 return -EINVAL;
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),
1317 dirty_cnt);
1318 return -EINVAL;
1322 return 0;
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
1338 * behavior.
1340 int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1341 dbg_znode_callback znode_cb, void *priv)
1343 int err;
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;
1354 goto out_unlock;
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
1361 * start from.
1363 znode = c->zroot.znode;
1364 while (znode->level > 0) {
1365 zbr = &znode->zbranch[0];
1366 child = zbr->znode;
1367 if (!child) {
1368 child = ubifs_load_znode(c, zbr, znode, 0);
1369 if (IS_ERR(child)) {
1370 err = PTR_ERR(child);
1371 goto out_unlock;
1373 zbr->znode = child;
1376 znode = child;
1379 /* Iterate over all indexing nodes */
1380 while (1) {
1381 int idx;
1383 cond_resched();
1385 if (znode_cb) {
1386 err = znode_cb(c, znode, priv);
1387 if (err) {
1388 ubifs_err("znode checking function returned "
1389 "error %d", err);
1390 dbg_dump_znode(c, znode);
1391 goto out_dump;
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);
1398 if (err) {
1399 ubifs_err("leaf checking function "
1400 "returned error %d, for leaf "
1401 "at LEB %d:%d",
1402 err, zbr->lnum, zbr->offs);
1403 goto out_dump;
1408 if (!znode->parent)
1409 break;
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];
1416 child = zbr->znode;
1417 if (!child) {
1418 child = ubifs_load_znode(c, zbr, znode, idx);
1419 if (IS_ERR(child)) {
1420 err = PTR_ERR(child);
1421 goto out_unlock;
1423 zbr->znode = child;
1425 znode = child;
1426 } else
1428 * This is the last child, switch to the parent and
1429 * continue.
1431 continue;
1433 /* Go to the lowest leftmost znode in the new sub-tree */
1434 while (znode->level > 0) {
1435 zbr = &znode->zbranch[0];
1436 child = zbr->znode;
1437 if (!child) {
1438 child = ubifs_load_znode(c, zbr, znode, 0);
1439 if (IS_ERR(child)) {
1440 err = PTR_ERR(child);
1441 goto out_unlock;
1443 zbr->znode = child;
1445 znode = child;
1449 mutex_unlock(&c->tnc_mutex);
1450 return 0;
1452 out_dump:
1453 if (znode->parent)
1454 zbr = &znode->parent->zbranch[znode->iip];
1455 else
1456 zbr = &c->zroot;
1457 ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1458 dbg_dump_znode(c, znode);
1459 out_unlock:
1460 mutex_unlock(&c->tnc_mutex);
1461 return err;
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
1472 * by @priv.
1474 static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1476 long long *idx_size = priv;
1477 int add;
1479 add = ubifs_idx_node_sz(c, znode->child_cnt);
1480 add = ALIGN(add, 8);
1481 *idx_size += add;
1482 return 0;
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)
1496 int err;
1497 long long calc = 0;
1499 if (!(ubifs_chk_flags & UBIFS_CHK_IDX_SZ))
1500 return 0;
1502 err = dbg_walk_index(c, NULL, add_size, &calc);
1503 if (err) {
1504 ubifs_err("error %d while walking the index", err);
1505 return err;
1508 if (calc != idx_size) {
1509 ubifs_err("index size check failed: calculated size is %lld, "
1510 "should be %lld", calc, idx_size);
1511 dump_stack();
1512 return -EINVAL;
1515 return 0;
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
1530 * inode)
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
1538 struct fsck_inode {
1539 struct rb_node rb;
1540 ino_t inum;
1541 umode_t mode;
1542 unsigned int nlink;
1543 unsigned int xattr_cnt;
1544 int references;
1545 int calc_cnt;
1546 long long size;
1547 unsigned int xattr_sz;
1548 long long calc_sz;
1549 long long calc_xcnt;
1550 long long calc_xsz;
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)
1559 struct fsck_data {
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;
1582 while (*p) {
1583 parent = *p;
1584 fscki = rb_entry(parent, struct fsck_inode, rb);
1585 if (inum < fscki->inum)
1586 p = &(*p)->rb_left;
1587 else if (inum > fscki->inum)
1588 p = &(*p)->rb_right;
1589 else
1590 return fscki;
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);
1600 if (!fscki)
1601 return ERR_PTR(-ENOMEM);
1603 fscki->inum = inum;
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);
1616 return fscki;
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)
1630 struct rb_node *p;
1631 struct fsck_inode *fscki;
1633 p = fsckd->inodes.rb_node;
1634 while (p) {
1635 fscki = rb_entry(p, struct fsck_inode, rb);
1636 if (inum < fscki->inum)
1637 p = p->rb_left;
1638 else if (inum > fscki->inum)
1639 p = p->rb_right;
1640 else
1641 return fscki;
1643 return NULL;
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
1655 * failure.
1657 static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1658 struct fsck_data *fsckd, ino_t inum)
1660 int n, err;
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);
1668 if (fscki)
1669 return fscki;
1671 ino_key_init(c, &key, inum);
1672 err = ubifs_lookup_level0(c, &key, &znode, &n);
1673 if (!err) {
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);
1690 if (!ino)
1691 return ERR_PTR(-ENOMEM);
1693 err = ubifs_tnc_read_node(c, zbr, ino);
1694 if (err) {
1695 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
1696 zbr->lnum, zbr->offs, err);
1697 kfree(ino);
1698 return ERR_PTR(err);
1701 fscki = add_inode(c, fsckd, ino);
1702 kfree(ino);
1703 if (IS_ERR(fscki)) {
1704 ubifs_err("error %ld while adding inode %lu node",
1705 PTR_ERR(fscki), (unsigned long)inum);
1706 return fscki;
1709 return fscki;
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,
1729 void *priv)
1731 ino_t inum;
1732 void *node;
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);
1740 return -EINVAL;
1743 node = kmalloc(zbr->len, GFP_NOFS);
1744 if (!node)
1745 return -ENOMEM;
1747 err = ubifs_tnc_read_node(c, zbr, node);
1748 if (err) {
1749 ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
1750 zbr->lnum, zbr->offs, err);
1751 goto out_free;
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);
1760 goto out_dump;
1762 goto out;
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);
1769 err = -EINVAL;
1770 goto out_free;
1773 ch = node;
1774 if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
1775 ubifs_err("too high sequence number, max. is %llu",
1776 c->max_sqnum);
1777 err = -EINVAL;
1778 goto out_dump;
1781 if (type == UBIFS_DATA_KEY) {
1782 long long blk_offs;
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);
1796 goto out_dump;
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,
1806 fscki->size);
1807 err = -EINVAL;
1808 goto out_dump;
1810 } else {
1811 int nlen;
1812 struct ubifs_dent_node *dent = node;
1813 struct fsck_inode *fscki1;
1815 err = ubifs_validate_entry(c, dent);
1816 if (err)
1817 goto out_dump;
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);
1830 goto out_dump;
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);
1843 goto out_dump;
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;
1852 } else {
1853 fscki1->calc_sz += CALC_DENT_SIZE(nlen);
1854 if (dent->type == UBIFS_ITYPE_DIR)
1855 fscki1->calc_cnt += 1;
1859 out:
1860 kfree(node);
1861 return 0;
1863 out_dump:
1864 ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
1865 dbg_dump_node(c, node);
1866 out_free:
1867 kfree(node);
1868 return err;
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;
1880 while (this) {
1881 if (this->rb_left)
1882 this = this->rb_left;
1883 else if (this->rb_right)
1884 this = this->rb_right;
1885 else {
1886 fscki = rb_entry(this, struct fsck_inode, rb);
1887 this = rb_parent(this);
1888 if (this) {
1889 if (this->rb_left == &fscki->rb)
1890 this->rb_left = NULL;
1891 else
1892 this->rb_right = NULL;
1894 kfree(fscki);
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)
1911 int n, err;
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);
1919 while (this) {
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
1927 * exception.
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 "
1933 "should be 1",
1934 (unsigned long)fscki->inum,
1935 fscki->references);
1936 goto out_dump;
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,
1943 fscki->references);
1944 goto out_dump;
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);
1951 goto out_dump;
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);
1958 goto out_dump;
1960 } else {
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);
1966 goto out_dump;
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,
1973 fscki->calc_xsz);
1974 goto out_dump;
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);
1981 goto out_dump;
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,
1987 fscki->calc_xnms);
1988 goto out_dump;
1992 return 0;
1994 out_dump:
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);
1998 if (!err) {
1999 ubifs_err("inode %lu not found in index",
2000 (unsigned long)fscki->inum);
2001 return -ENOENT;
2002 } else if (err < 0) {
2003 ubifs_err("error %d while looking up inode %lu",
2004 err, (unsigned long)fscki->inum);
2005 return err;
2008 zbr = &znode->zbranch[n];
2009 ino = kmalloc(zbr->len, GFP_NOFS);
2010 if (!ino)
2011 return -ENOMEM;
2013 err = ubifs_tnc_read_node(c, zbr, ino);
2014 if (err) {
2015 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2016 zbr->lnum, zbr->offs, err);
2017 kfree(ino);
2018 return 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);
2024 kfree(ino);
2025 return -EINVAL;
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
2035 * inodes).
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)
2043 int err;
2044 struct fsck_data fsckd;
2046 if (!(ubifs_chk_flags & UBIFS_CHK_FS))
2047 return 0;
2049 fsckd.inodes = RB_ROOT;
2050 err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
2051 if (err)
2052 goto out_free;
2054 err = check_inodes(c, &fsckd);
2055 if (err)
2056 goto out_free;
2058 free_inodes(&fsckd);
2059 return 0;
2061 out_free:
2062 ubifs_err("file-system check failed with error %d", err);
2063 dump_stack();
2064 free_inodes(&fsckd);
2065 return err;
2068 static int invocation_cnt;
2070 int dbg_force_in_the_gaps(void)
2072 if (!dbg_force_in_the_gaps_enabled)
2073 return 0;
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)
2094 if (next == 0)
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);
2105 if (!fmi) {
2106 dbg_err("Failed to register failure mode - no memory");
2107 return;
2109 fmi->c = c;
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)
2121 if (fmi->c == c) {
2122 list_del(&fmi->list);
2123 kfree(fmi);
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);
2138 return c;
2140 spin_unlock(&fmi_lock);
2141 return NULL;
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;
2150 return 0;
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)
2158 return 0;
2159 if (c->failure_mode)
2160 return 1;
2161 if (!c->fail_cnt) {
2162 /* First call - decide delay to failure */
2163 if (chance(1, 2)) {
2164 unsigned int delay = 1 << (simple_rand() >> 11);
2166 if (chance(1, 2)) {
2167 c->fail_delay = 1;
2168 c->fail_timeout = jiffies +
2169 msecs_to_jiffies(delay);
2170 dbg_rcvry("failing after %ums", delay);
2171 } else {
2172 c->fail_delay = 2;
2173 c->fail_cnt_max = delay;
2174 dbg_rcvry("failing after %u calls", delay);
2177 c->fail_cnt += 1;
2179 /* Determine if failure delay has expired */
2180 if (c->fail_delay == 1) {
2181 if (time_before(jiffies, c->fail_timeout))
2182 return 0;
2183 } else if (c->fail_delay == 2)
2184 if (c->fail_cnt++ < c->fail_cnt_max)
2185 return 0;
2186 if (lnum == UBIFS_SB_LNUM) {
2187 if (write) {
2188 if (chance(1, 2))
2189 return 0;
2190 } else if (chance(19, 20))
2191 return 0;
2192 dbg_rcvry("failing in super block LEB %d", lnum);
2193 } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2194 if (chance(19, 20))
2195 return 0;
2196 dbg_rcvry("failing in master LEB %d", lnum);
2197 } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2198 if (write) {
2199 if (chance(99, 100))
2200 return 0;
2201 } else if (chance(399, 400))
2202 return 0;
2203 dbg_rcvry("failing in log LEB %d", lnum);
2204 } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2205 if (write) {
2206 if (chance(7, 8))
2207 return 0;
2208 } else if (chance(19, 20))
2209 return 0;
2210 dbg_rcvry("failing in LPT LEB %d", lnum);
2211 } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2212 if (write) {
2213 if (chance(1, 2))
2214 return 0;
2215 } else if (chance(9, 10))
2216 return 0;
2217 dbg_rcvry("failing in orphan LEB %d", lnum);
2218 } else if (lnum == c->ihead_lnum) {
2219 if (chance(99, 100))
2220 return 0;
2221 dbg_rcvry("failing in index head LEB %d", lnum);
2222 } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2223 if (chance(9, 10))
2224 return 0;
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)) {
2228 if (chance(19, 20))
2229 return 0;
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))
2234 return 0;
2235 dbg_rcvry("failing in bud LEB %d commit running", lnum);
2236 } else {
2237 if (chance(9999, 10000))
2238 return 0;
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;
2243 dump_stack();
2244 return 1;
2247 static void cut_data(const void *buf, int len)
2249 int flen, i;
2250 unsigned char *p = (void *)buf;
2252 flen = (len * (long long)simple_rand()) >> 15;
2253 for (i = flen; i < len; i++)
2254 p[i] = 0xff;
2257 int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
2258 int len, int check)
2260 if (in_failure_mode(desc))
2261 return -EIO;
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)
2268 int err, failing;
2270 if (in_failure_mode(desc))
2271 return -EIO;
2272 failing = do_fail(desc, lnum, 1);
2273 if (failing)
2274 cut_data(buf, len);
2275 err = ubi_leb_write(desc, lnum, buf, offset, len, dtype);
2276 if (err)
2277 return err;
2278 if (failing)
2279 return -EIO;
2280 return 0;
2283 int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
2284 int len, int dtype)
2286 int err;
2288 if (do_fail(desc, lnum, 1))
2289 return -EIO;
2290 err = ubi_leb_change(desc, lnum, buf, len, dtype);
2291 if (err)
2292 return err;
2293 if (do_fail(desc, lnum, 1))
2294 return -EIO;
2295 return 0;
2298 int dbg_leb_erase(struct ubi_volume_desc *desc, int lnum)
2300 int err;
2302 if (do_fail(desc, lnum, 0))
2303 return -EIO;
2304 err = ubi_leb_erase(desc, lnum);
2305 if (err)
2306 return err;
2307 if (do_fail(desc, lnum, 0))
2308 return -EIO;
2309 return 0;
2312 int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum)
2314 int err;
2316 if (do_fail(desc, lnum, 0))
2317 return -EIO;
2318 err = ubi_leb_unmap(desc, lnum);
2319 if (err)
2320 return err;
2321 if (do_fail(desc, lnum, 0))
2322 return -EIO;
2323 return 0;
2326 int dbg_is_mapped(struct ubi_volume_desc *desc, int lnum)
2328 if (in_failure_mode(desc))
2329 return -EIO;
2330 return ubi_is_mapped(desc, lnum);
2333 int dbg_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
2335 int err;
2337 if (do_fail(desc, lnum, 0))
2338 return -EIO;
2339 err = ubi_leb_map(desc, lnum, dtype);
2340 if (err)
2341 return err;
2342 if (do_fail(desc, lnum, 0))
2343 return -EIO;
2344 return 0;
2347 #endif /* CONFIG_UBIFS_FS_DEBUG */