xfs: use mnt_want_write in compat_attrmulti ioctl
[linux-2.6/linux-2.6-openrd.git] / fs / ubifs / debug.c
blob792c5a16c182e6b4f501e85041c7144e444370e8
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>
35 #include <linux/debugfs.h>
36 #include <linux/math64.h>
38 #ifdef CONFIG_UBIFS_FS_DEBUG
40 DEFINE_SPINLOCK(dbg_lock);
42 static char dbg_key_buf0[128];
43 static char dbg_key_buf1[128];
45 unsigned int ubifs_msg_flags = UBIFS_MSG_FLAGS_DEFAULT;
46 unsigned int ubifs_chk_flags = UBIFS_CHK_FLAGS_DEFAULT;
47 unsigned int ubifs_tst_flags;
49 module_param_named(debug_msgs, ubifs_msg_flags, uint, S_IRUGO | S_IWUSR);
50 module_param_named(debug_chks, ubifs_chk_flags, uint, S_IRUGO | S_IWUSR);
51 module_param_named(debug_tsts, ubifs_tst_flags, uint, S_IRUGO | S_IWUSR);
53 MODULE_PARM_DESC(debug_msgs, "Debug message type flags");
54 MODULE_PARM_DESC(debug_chks, "Debug check flags");
55 MODULE_PARM_DESC(debug_tsts, "Debug special test flags");
57 static const char *get_key_fmt(int fmt)
59 switch (fmt) {
60 case UBIFS_SIMPLE_KEY_FMT:
61 return "simple";
62 default:
63 return "unknown/invalid format";
67 static const char *get_key_hash(int hash)
69 switch (hash) {
70 case UBIFS_KEY_HASH_R5:
71 return "R5";
72 case UBIFS_KEY_HASH_TEST:
73 return "test";
74 default:
75 return "unknown/invalid name hash";
79 static const char *get_key_type(int type)
81 switch (type) {
82 case UBIFS_INO_KEY:
83 return "inode";
84 case UBIFS_DENT_KEY:
85 return "direntry";
86 case UBIFS_XENT_KEY:
87 return "xentry";
88 case UBIFS_DATA_KEY:
89 return "data";
90 case UBIFS_TRUN_KEY:
91 return "truncate";
92 default:
93 return "unknown/invalid key";
97 static void sprintf_key(const struct ubifs_info *c, const union ubifs_key *key,
98 char *buffer)
100 char *p = buffer;
101 int type = key_type(c, key);
103 if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
104 switch (type) {
105 case UBIFS_INO_KEY:
106 sprintf(p, "(%lu, %s)", (unsigned long)key_inum(c, key),
107 get_key_type(type));
108 break;
109 case UBIFS_DENT_KEY:
110 case UBIFS_XENT_KEY:
111 sprintf(p, "(%lu, %s, %#08x)",
112 (unsigned long)key_inum(c, key),
113 get_key_type(type), key_hash(c, key));
114 break;
115 case UBIFS_DATA_KEY:
116 sprintf(p, "(%lu, %s, %u)",
117 (unsigned long)key_inum(c, key),
118 get_key_type(type), key_block(c, key));
119 break;
120 case UBIFS_TRUN_KEY:
121 sprintf(p, "(%lu, %s)",
122 (unsigned long)key_inum(c, key),
123 get_key_type(type));
124 break;
125 default:
126 sprintf(p, "(bad key type: %#08x, %#08x)",
127 key->u32[0], key->u32[1]);
129 } else
130 sprintf(p, "bad key format %d", c->key_fmt);
133 const char *dbg_key_str0(const struct ubifs_info *c, const union ubifs_key *key)
135 /* dbg_lock must be held */
136 sprintf_key(c, key, dbg_key_buf0);
137 return dbg_key_buf0;
140 const char *dbg_key_str1(const struct ubifs_info *c, const union ubifs_key *key)
142 /* dbg_lock must be held */
143 sprintf_key(c, key, dbg_key_buf1);
144 return dbg_key_buf1;
147 const char *dbg_ntype(int type)
149 switch (type) {
150 case UBIFS_PAD_NODE:
151 return "padding node";
152 case UBIFS_SB_NODE:
153 return "superblock node";
154 case UBIFS_MST_NODE:
155 return "master node";
156 case UBIFS_REF_NODE:
157 return "reference node";
158 case UBIFS_INO_NODE:
159 return "inode node";
160 case UBIFS_DENT_NODE:
161 return "direntry node";
162 case UBIFS_XENT_NODE:
163 return "xentry node";
164 case UBIFS_DATA_NODE:
165 return "data node";
166 case UBIFS_TRUN_NODE:
167 return "truncate node";
168 case UBIFS_IDX_NODE:
169 return "indexing node";
170 case UBIFS_CS_NODE:
171 return "commit start node";
172 case UBIFS_ORPH_NODE:
173 return "orphan node";
174 default:
175 return "unknown node";
179 static const char *dbg_gtype(int type)
181 switch (type) {
182 case UBIFS_NO_NODE_GROUP:
183 return "no node group";
184 case UBIFS_IN_NODE_GROUP:
185 return "in node group";
186 case UBIFS_LAST_OF_NODE_GROUP:
187 return "last of node group";
188 default:
189 return "unknown";
193 const char *dbg_cstate(int cmt_state)
195 switch (cmt_state) {
196 case COMMIT_RESTING:
197 return "commit resting";
198 case COMMIT_BACKGROUND:
199 return "background commit requested";
200 case COMMIT_REQUIRED:
201 return "commit required";
202 case COMMIT_RUNNING_BACKGROUND:
203 return "BACKGROUND commit running";
204 case COMMIT_RUNNING_REQUIRED:
205 return "commit running and required";
206 case COMMIT_BROKEN:
207 return "broken commit";
208 default:
209 return "unknown commit state";
213 static void dump_ch(const struct ubifs_ch *ch)
215 printk(KERN_DEBUG "\tmagic %#x\n", le32_to_cpu(ch->magic));
216 printk(KERN_DEBUG "\tcrc %#x\n", le32_to_cpu(ch->crc));
217 printk(KERN_DEBUG "\tnode_type %d (%s)\n", ch->node_type,
218 dbg_ntype(ch->node_type));
219 printk(KERN_DEBUG "\tgroup_type %d (%s)\n", ch->group_type,
220 dbg_gtype(ch->group_type));
221 printk(KERN_DEBUG "\tsqnum %llu\n",
222 (unsigned long long)le64_to_cpu(ch->sqnum));
223 printk(KERN_DEBUG "\tlen %u\n", le32_to_cpu(ch->len));
226 void dbg_dump_inode(const struct ubifs_info *c, const struct inode *inode)
228 const struct ubifs_inode *ui = ubifs_inode(inode);
230 printk(KERN_DEBUG "Dump in-memory inode:");
231 printk(KERN_DEBUG "\tinode %lu\n", inode->i_ino);
232 printk(KERN_DEBUG "\tsize %llu\n",
233 (unsigned long long)i_size_read(inode));
234 printk(KERN_DEBUG "\tnlink %u\n", inode->i_nlink);
235 printk(KERN_DEBUG "\tuid %u\n", (unsigned int)inode->i_uid);
236 printk(KERN_DEBUG "\tgid %u\n", (unsigned int)inode->i_gid);
237 printk(KERN_DEBUG "\tatime %u.%u\n",
238 (unsigned int)inode->i_atime.tv_sec,
239 (unsigned int)inode->i_atime.tv_nsec);
240 printk(KERN_DEBUG "\tmtime %u.%u\n",
241 (unsigned int)inode->i_mtime.tv_sec,
242 (unsigned int)inode->i_mtime.tv_nsec);
243 printk(KERN_DEBUG "\tctime %u.%u\n",
244 (unsigned int)inode->i_ctime.tv_sec,
245 (unsigned int)inode->i_ctime.tv_nsec);
246 printk(KERN_DEBUG "\tcreat_sqnum %llu\n", ui->creat_sqnum);
247 printk(KERN_DEBUG "\txattr_size %u\n", ui->xattr_size);
248 printk(KERN_DEBUG "\txattr_cnt %u\n", ui->xattr_cnt);
249 printk(KERN_DEBUG "\txattr_names %u\n", ui->xattr_names);
250 printk(KERN_DEBUG "\tdirty %u\n", ui->dirty);
251 printk(KERN_DEBUG "\txattr %u\n", ui->xattr);
252 printk(KERN_DEBUG "\tbulk_read %u\n", ui->xattr);
253 printk(KERN_DEBUG "\tsynced_i_size %llu\n",
254 (unsigned long long)ui->synced_i_size);
255 printk(KERN_DEBUG "\tui_size %llu\n",
256 (unsigned long long)ui->ui_size);
257 printk(KERN_DEBUG "\tflags %d\n", ui->flags);
258 printk(KERN_DEBUG "\tcompr_type %d\n", ui->compr_type);
259 printk(KERN_DEBUG "\tlast_page_read %lu\n", ui->last_page_read);
260 printk(KERN_DEBUG "\tread_in_a_row %lu\n", ui->read_in_a_row);
261 printk(KERN_DEBUG "\tdata_len %d\n", ui->data_len);
264 void dbg_dump_node(const struct ubifs_info *c, const void *node)
266 int i, n;
267 union ubifs_key key;
268 const struct ubifs_ch *ch = node;
270 if (dbg_failure_mode)
271 return;
273 /* If the magic is incorrect, just hexdump the first bytes */
274 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
275 printk(KERN_DEBUG "Not a node, first %zu bytes:", UBIFS_CH_SZ);
276 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
277 (void *)node, UBIFS_CH_SZ, 1);
278 return;
281 spin_lock(&dbg_lock);
282 dump_ch(node);
284 switch (ch->node_type) {
285 case UBIFS_PAD_NODE:
287 const struct ubifs_pad_node *pad = node;
289 printk(KERN_DEBUG "\tpad_len %u\n",
290 le32_to_cpu(pad->pad_len));
291 break;
293 case UBIFS_SB_NODE:
295 const struct ubifs_sb_node *sup = node;
296 unsigned int sup_flags = le32_to_cpu(sup->flags);
298 printk(KERN_DEBUG "\tkey_hash %d (%s)\n",
299 (int)sup->key_hash, get_key_hash(sup->key_hash));
300 printk(KERN_DEBUG "\tkey_fmt %d (%s)\n",
301 (int)sup->key_fmt, get_key_fmt(sup->key_fmt));
302 printk(KERN_DEBUG "\tflags %#x\n", sup_flags);
303 printk(KERN_DEBUG "\t big_lpt %u\n",
304 !!(sup_flags & UBIFS_FLG_BIGLPT));
305 printk(KERN_DEBUG "\tmin_io_size %u\n",
306 le32_to_cpu(sup->min_io_size));
307 printk(KERN_DEBUG "\tleb_size %u\n",
308 le32_to_cpu(sup->leb_size));
309 printk(KERN_DEBUG "\tleb_cnt %u\n",
310 le32_to_cpu(sup->leb_cnt));
311 printk(KERN_DEBUG "\tmax_leb_cnt %u\n",
312 le32_to_cpu(sup->max_leb_cnt));
313 printk(KERN_DEBUG "\tmax_bud_bytes %llu\n",
314 (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
315 printk(KERN_DEBUG "\tlog_lebs %u\n",
316 le32_to_cpu(sup->log_lebs));
317 printk(KERN_DEBUG "\tlpt_lebs %u\n",
318 le32_to_cpu(sup->lpt_lebs));
319 printk(KERN_DEBUG "\torph_lebs %u\n",
320 le32_to_cpu(sup->orph_lebs));
321 printk(KERN_DEBUG "\tjhead_cnt %u\n",
322 le32_to_cpu(sup->jhead_cnt));
323 printk(KERN_DEBUG "\tfanout %u\n",
324 le32_to_cpu(sup->fanout));
325 printk(KERN_DEBUG "\tlsave_cnt %u\n",
326 le32_to_cpu(sup->lsave_cnt));
327 printk(KERN_DEBUG "\tdefault_compr %u\n",
328 (int)le16_to_cpu(sup->default_compr));
329 printk(KERN_DEBUG "\trp_size %llu\n",
330 (unsigned long long)le64_to_cpu(sup->rp_size));
331 printk(KERN_DEBUG "\trp_uid %u\n",
332 le32_to_cpu(sup->rp_uid));
333 printk(KERN_DEBUG "\trp_gid %u\n",
334 le32_to_cpu(sup->rp_gid));
335 printk(KERN_DEBUG "\tfmt_version %u\n",
336 le32_to_cpu(sup->fmt_version));
337 printk(KERN_DEBUG "\ttime_gran %u\n",
338 le32_to_cpu(sup->time_gran));
339 printk(KERN_DEBUG "\tUUID %02X%02X%02X%02X-%02X%02X"
340 "-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
341 sup->uuid[0], sup->uuid[1], sup->uuid[2], sup->uuid[3],
342 sup->uuid[4], sup->uuid[5], sup->uuid[6], sup->uuid[7],
343 sup->uuid[8], sup->uuid[9], sup->uuid[10], sup->uuid[11],
344 sup->uuid[12], sup->uuid[13], sup->uuid[14],
345 sup->uuid[15]);
346 break;
348 case UBIFS_MST_NODE:
350 const struct ubifs_mst_node *mst = node;
352 printk(KERN_DEBUG "\thighest_inum %llu\n",
353 (unsigned long long)le64_to_cpu(mst->highest_inum));
354 printk(KERN_DEBUG "\tcommit number %llu\n",
355 (unsigned long long)le64_to_cpu(mst->cmt_no));
356 printk(KERN_DEBUG "\tflags %#x\n",
357 le32_to_cpu(mst->flags));
358 printk(KERN_DEBUG "\tlog_lnum %u\n",
359 le32_to_cpu(mst->log_lnum));
360 printk(KERN_DEBUG "\troot_lnum %u\n",
361 le32_to_cpu(mst->root_lnum));
362 printk(KERN_DEBUG "\troot_offs %u\n",
363 le32_to_cpu(mst->root_offs));
364 printk(KERN_DEBUG "\troot_len %u\n",
365 le32_to_cpu(mst->root_len));
366 printk(KERN_DEBUG "\tgc_lnum %u\n",
367 le32_to_cpu(mst->gc_lnum));
368 printk(KERN_DEBUG "\tihead_lnum %u\n",
369 le32_to_cpu(mst->ihead_lnum));
370 printk(KERN_DEBUG "\tihead_offs %u\n",
371 le32_to_cpu(mst->ihead_offs));
372 printk(KERN_DEBUG "\tindex_size %llu\n",
373 (unsigned long long)le64_to_cpu(mst->index_size));
374 printk(KERN_DEBUG "\tlpt_lnum %u\n",
375 le32_to_cpu(mst->lpt_lnum));
376 printk(KERN_DEBUG "\tlpt_offs %u\n",
377 le32_to_cpu(mst->lpt_offs));
378 printk(KERN_DEBUG "\tnhead_lnum %u\n",
379 le32_to_cpu(mst->nhead_lnum));
380 printk(KERN_DEBUG "\tnhead_offs %u\n",
381 le32_to_cpu(mst->nhead_offs));
382 printk(KERN_DEBUG "\tltab_lnum %u\n",
383 le32_to_cpu(mst->ltab_lnum));
384 printk(KERN_DEBUG "\tltab_offs %u\n",
385 le32_to_cpu(mst->ltab_offs));
386 printk(KERN_DEBUG "\tlsave_lnum %u\n",
387 le32_to_cpu(mst->lsave_lnum));
388 printk(KERN_DEBUG "\tlsave_offs %u\n",
389 le32_to_cpu(mst->lsave_offs));
390 printk(KERN_DEBUG "\tlscan_lnum %u\n",
391 le32_to_cpu(mst->lscan_lnum));
392 printk(KERN_DEBUG "\tleb_cnt %u\n",
393 le32_to_cpu(mst->leb_cnt));
394 printk(KERN_DEBUG "\tempty_lebs %u\n",
395 le32_to_cpu(mst->empty_lebs));
396 printk(KERN_DEBUG "\tidx_lebs %u\n",
397 le32_to_cpu(mst->idx_lebs));
398 printk(KERN_DEBUG "\ttotal_free %llu\n",
399 (unsigned long long)le64_to_cpu(mst->total_free));
400 printk(KERN_DEBUG "\ttotal_dirty %llu\n",
401 (unsigned long long)le64_to_cpu(mst->total_dirty));
402 printk(KERN_DEBUG "\ttotal_used %llu\n",
403 (unsigned long long)le64_to_cpu(mst->total_used));
404 printk(KERN_DEBUG "\ttotal_dead %llu\n",
405 (unsigned long long)le64_to_cpu(mst->total_dead));
406 printk(KERN_DEBUG "\ttotal_dark %llu\n",
407 (unsigned long long)le64_to_cpu(mst->total_dark));
408 break;
410 case UBIFS_REF_NODE:
412 const struct ubifs_ref_node *ref = node;
414 printk(KERN_DEBUG "\tlnum %u\n",
415 le32_to_cpu(ref->lnum));
416 printk(KERN_DEBUG "\toffs %u\n",
417 le32_to_cpu(ref->offs));
418 printk(KERN_DEBUG "\tjhead %u\n",
419 le32_to_cpu(ref->jhead));
420 break;
422 case UBIFS_INO_NODE:
424 const struct ubifs_ino_node *ino = node;
426 key_read(c, &ino->key, &key);
427 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
428 printk(KERN_DEBUG "\tcreat_sqnum %llu\n",
429 (unsigned long long)le64_to_cpu(ino->creat_sqnum));
430 printk(KERN_DEBUG "\tsize %llu\n",
431 (unsigned long long)le64_to_cpu(ino->size));
432 printk(KERN_DEBUG "\tnlink %u\n",
433 le32_to_cpu(ino->nlink));
434 printk(KERN_DEBUG "\tatime %lld.%u\n",
435 (long long)le64_to_cpu(ino->atime_sec),
436 le32_to_cpu(ino->atime_nsec));
437 printk(KERN_DEBUG "\tmtime %lld.%u\n",
438 (long long)le64_to_cpu(ino->mtime_sec),
439 le32_to_cpu(ino->mtime_nsec));
440 printk(KERN_DEBUG "\tctime %lld.%u\n",
441 (long long)le64_to_cpu(ino->ctime_sec),
442 le32_to_cpu(ino->ctime_nsec));
443 printk(KERN_DEBUG "\tuid %u\n",
444 le32_to_cpu(ino->uid));
445 printk(KERN_DEBUG "\tgid %u\n",
446 le32_to_cpu(ino->gid));
447 printk(KERN_DEBUG "\tmode %u\n",
448 le32_to_cpu(ino->mode));
449 printk(KERN_DEBUG "\tflags %#x\n",
450 le32_to_cpu(ino->flags));
451 printk(KERN_DEBUG "\txattr_cnt %u\n",
452 le32_to_cpu(ino->xattr_cnt));
453 printk(KERN_DEBUG "\txattr_size %u\n",
454 le32_to_cpu(ino->xattr_size));
455 printk(KERN_DEBUG "\txattr_names %u\n",
456 le32_to_cpu(ino->xattr_names));
457 printk(KERN_DEBUG "\tcompr_type %#x\n",
458 (int)le16_to_cpu(ino->compr_type));
459 printk(KERN_DEBUG "\tdata len %u\n",
460 le32_to_cpu(ino->data_len));
461 break;
463 case UBIFS_DENT_NODE:
464 case UBIFS_XENT_NODE:
466 const struct ubifs_dent_node *dent = node;
467 int nlen = le16_to_cpu(dent->nlen);
469 key_read(c, &dent->key, &key);
470 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
471 printk(KERN_DEBUG "\tinum %llu\n",
472 (unsigned long long)le64_to_cpu(dent->inum));
473 printk(KERN_DEBUG "\ttype %d\n", (int)dent->type);
474 printk(KERN_DEBUG "\tnlen %d\n", nlen);
475 printk(KERN_DEBUG "\tname ");
477 if (nlen > UBIFS_MAX_NLEN)
478 printk(KERN_DEBUG "(bad name length, not printing, "
479 "bad or corrupted node)");
480 else {
481 for (i = 0; i < nlen && dent->name[i]; i++)
482 printk("%c", dent->name[i]);
484 printk("\n");
486 break;
488 case UBIFS_DATA_NODE:
490 const struct ubifs_data_node *dn = node;
491 int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
493 key_read(c, &dn->key, &key);
494 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
495 printk(KERN_DEBUG "\tsize %u\n",
496 le32_to_cpu(dn->size));
497 printk(KERN_DEBUG "\tcompr_typ %d\n",
498 (int)le16_to_cpu(dn->compr_type));
499 printk(KERN_DEBUG "\tdata size %d\n",
500 dlen);
501 printk(KERN_DEBUG "\tdata:\n");
502 print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET, 32, 1,
503 (void *)&dn->data, dlen, 0);
504 break;
506 case UBIFS_TRUN_NODE:
508 const struct ubifs_trun_node *trun = node;
510 printk(KERN_DEBUG "\tinum %u\n",
511 le32_to_cpu(trun->inum));
512 printk(KERN_DEBUG "\told_size %llu\n",
513 (unsigned long long)le64_to_cpu(trun->old_size));
514 printk(KERN_DEBUG "\tnew_size %llu\n",
515 (unsigned long long)le64_to_cpu(trun->new_size));
516 break;
518 case UBIFS_IDX_NODE:
520 const struct ubifs_idx_node *idx = node;
522 n = le16_to_cpu(idx->child_cnt);
523 printk(KERN_DEBUG "\tchild_cnt %d\n", n);
524 printk(KERN_DEBUG "\tlevel %d\n",
525 (int)le16_to_cpu(idx->level));
526 printk(KERN_DEBUG "\tBranches:\n");
528 for (i = 0; i < n && i < c->fanout - 1; i++) {
529 const struct ubifs_branch *br;
531 br = ubifs_idx_branch(c, idx, i);
532 key_read(c, &br->key, &key);
533 printk(KERN_DEBUG "\t%d: LEB %d:%d len %d key %s\n",
534 i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
535 le32_to_cpu(br->len), DBGKEY(&key));
537 break;
539 case UBIFS_CS_NODE:
540 break;
541 case UBIFS_ORPH_NODE:
543 const struct ubifs_orph_node *orph = node;
545 printk(KERN_DEBUG "\tcommit number %llu\n",
546 (unsigned long long)
547 le64_to_cpu(orph->cmt_no) & LLONG_MAX);
548 printk(KERN_DEBUG "\tlast node flag %llu\n",
549 (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
550 n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
551 printk(KERN_DEBUG "\t%d orphan inode numbers:\n", n);
552 for (i = 0; i < n; i++)
553 printk(KERN_DEBUG "\t ino %llu\n",
554 (unsigned long long)le64_to_cpu(orph->inos[i]));
555 break;
557 default:
558 printk(KERN_DEBUG "node type %d was not recognized\n",
559 (int)ch->node_type);
561 spin_unlock(&dbg_lock);
564 void dbg_dump_budget_req(const struct ubifs_budget_req *req)
566 spin_lock(&dbg_lock);
567 printk(KERN_DEBUG "Budgeting request: new_ino %d, dirtied_ino %d\n",
568 req->new_ino, req->dirtied_ino);
569 printk(KERN_DEBUG "\tnew_ino_d %d, dirtied_ino_d %d\n",
570 req->new_ino_d, req->dirtied_ino_d);
571 printk(KERN_DEBUG "\tnew_page %d, dirtied_page %d\n",
572 req->new_page, req->dirtied_page);
573 printk(KERN_DEBUG "\tnew_dent %d, mod_dent %d\n",
574 req->new_dent, req->mod_dent);
575 printk(KERN_DEBUG "\tidx_growth %d\n", req->idx_growth);
576 printk(KERN_DEBUG "\tdata_growth %d dd_growth %d\n",
577 req->data_growth, req->dd_growth);
578 spin_unlock(&dbg_lock);
581 void dbg_dump_lstats(const struct ubifs_lp_stats *lst)
583 spin_lock(&dbg_lock);
584 printk(KERN_DEBUG "(pid %d) Lprops statistics: empty_lebs %d, "
585 "idx_lebs %d\n", current->pid, lst->empty_lebs, lst->idx_lebs);
586 printk(KERN_DEBUG "\ttaken_empty_lebs %d, total_free %lld, "
587 "total_dirty %lld\n", lst->taken_empty_lebs, lst->total_free,
588 lst->total_dirty);
589 printk(KERN_DEBUG "\ttotal_used %lld, total_dark %lld, "
590 "total_dead %lld\n", lst->total_used, lst->total_dark,
591 lst->total_dead);
592 spin_unlock(&dbg_lock);
595 void dbg_dump_budg(struct ubifs_info *c)
597 int i;
598 struct rb_node *rb;
599 struct ubifs_bud *bud;
600 struct ubifs_gced_idx_leb *idx_gc;
601 long long available, outstanding, free;
603 ubifs_assert(spin_is_locked(&c->space_lock));
604 spin_lock(&dbg_lock);
605 printk(KERN_DEBUG "(pid %d) Budgeting info: budg_data_growth %lld, "
606 "budg_dd_growth %lld, budg_idx_growth %lld\n", current->pid,
607 c->budg_data_growth, c->budg_dd_growth, c->budg_idx_growth);
608 printk(KERN_DEBUG "\tdata budget sum %lld, total budget sum %lld, "
609 "freeable_cnt %d\n", c->budg_data_growth + c->budg_dd_growth,
610 c->budg_data_growth + c->budg_dd_growth + c->budg_idx_growth,
611 c->freeable_cnt);
612 printk(KERN_DEBUG "\tmin_idx_lebs %d, old_idx_sz %lld, "
613 "calc_idx_sz %lld, idx_gc_cnt %d\n", c->min_idx_lebs,
614 c->old_idx_sz, c->calc_idx_sz, c->idx_gc_cnt);
615 printk(KERN_DEBUG "\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, "
616 "clean_zn_cnt %ld\n", atomic_long_read(&c->dirty_pg_cnt),
617 atomic_long_read(&c->dirty_zn_cnt),
618 atomic_long_read(&c->clean_zn_cnt));
619 printk(KERN_DEBUG "\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
620 c->dark_wm, c->dead_wm, c->max_idx_node_sz);
621 printk(KERN_DEBUG "\tgc_lnum %d, ihead_lnum %d\n",
622 c->gc_lnum, c->ihead_lnum);
623 for (i = 0; i < c->jhead_cnt; i++)
624 printk(KERN_DEBUG "\tjhead %d\t LEB %d\n",
625 c->jheads[i].wbuf.jhead, c->jheads[i].wbuf.lnum);
626 for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
627 bud = rb_entry(rb, struct ubifs_bud, rb);
628 printk(KERN_DEBUG "\tbud LEB %d\n", bud->lnum);
630 list_for_each_entry(bud, &c->old_buds, list)
631 printk(KERN_DEBUG "\told bud LEB %d\n", bud->lnum);
632 list_for_each_entry(idx_gc, &c->idx_gc, list)
633 printk(KERN_DEBUG "\tGC'ed idx LEB %d unmap %d\n",
634 idx_gc->lnum, idx_gc->unmap);
635 printk(KERN_DEBUG "\tcommit state %d\n", c->cmt_state);
637 /* Print budgeting predictions */
638 available = ubifs_calc_available(c, c->min_idx_lebs);
639 outstanding = c->budg_data_growth + c->budg_dd_growth;
640 if (available > outstanding)
641 free = ubifs_reported_space(c, available - outstanding);
642 else
643 free = 0;
644 printk(KERN_DEBUG "Budgeting predictions:\n");
645 printk(KERN_DEBUG "\tavailable: %lld, outstanding %lld, free %lld\n",
646 available, outstanding, free);
647 spin_unlock(&dbg_lock);
650 void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
652 printk(KERN_DEBUG "LEB %d lprops: free %d, dirty %d (used %d), "
653 "flags %#x\n", lp->lnum, lp->free, lp->dirty,
654 c->leb_size - lp->free - lp->dirty, lp->flags);
657 void dbg_dump_lprops(struct ubifs_info *c)
659 int lnum, err;
660 struct ubifs_lprops lp;
661 struct ubifs_lp_stats lst;
663 printk(KERN_DEBUG "(pid %d) start dumping LEB properties\n",
664 current->pid);
665 ubifs_get_lp_stats(c, &lst);
666 dbg_dump_lstats(&lst);
668 for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
669 err = ubifs_read_one_lp(c, lnum, &lp);
670 if (err)
671 ubifs_err("cannot read lprops for LEB %d", lnum);
673 dbg_dump_lprop(c, &lp);
675 printk(KERN_DEBUG "(pid %d) finish dumping LEB properties\n",
676 current->pid);
679 void dbg_dump_lpt_info(struct ubifs_info *c)
681 int i;
683 spin_lock(&dbg_lock);
684 printk(KERN_DEBUG "(pid %d) dumping LPT information\n", current->pid);
685 printk(KERN_DEBUG "\tlpt_sz: %lld\n", c->lpt_sz);
686 printk(KERN_DEBUG "\tpnode_sz: %d\n", c->pnode_sz);
687 printk(KERN_DEBUG "\tnnode_sz: %d\n", c->nnode_sz);
688 printk(KERN_DEBUG "\tltab_sz: %d\n", c->ltab_sz);
689 printk(KERN_DEBUG "\tlsave_sz: %d\n", c->lsave_sz);
690 printk(KERN_DEBUG "\tbig_lpt: %d\n", c->big_lpt);
691 printk(KERN_DEBUG "\tlpt_hght: %d\n", c->lpt_hght);
692 printk(KERN_DEBUG "\tpnode_cnt: %d\n", c->pnode_cnt);
693 printk(KERN_DEBUG "\tnnode_cnt: %d\n", c->nnode_cnt);
694 printk(KERN_DEBUG "\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt);
695 printk(KERN_DEBUG "\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt);
696 printk(KERN_DEBUG "\tlsave_cnt: %d\n", c->lsave_cnt);
697 printk(KERN_DEBUG "\tspace_bits: %d\n", c->space_bits);
698 printk(KERN_DEBUG "\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
699 printk(KERN_DEBUG "\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
700 printk(KERN_DEBUG "\tlpt_spc_bits: %d\n", c->lpt_spc_bits);
701 printk(KERN_DEBUG "\tpcnt_bits: %d\n", c->pcnt_bits);
702 printk(KERN_DEBUG "\tlnum_bits: %d\n", c->lnum_bits);
703 printk(KERN_DEBUG "\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
704 printk(KERN_DEBUG "\tLPT head is at %d:%d\n",
705 c->nhead_lnum, c->nhead_offs);
706 printk(KERN_DEBUG "\tLPT ltab is at %d:%d\n",
707 c->ltab_lnum, c->ltab_offs);
708 if (c->big_lpt)
709 printk(KERN_DEBUG "\tLPT lsave is at %d:%d\n",
710 c->lsave_lnum, c->lsave_offs);
711 for (i = 0; i < c->lpt_lebs; i++)
712 printk(KERN_DEBUG "\tLPT LEB %d free %d dirty %d tgc %d "
713 "cmt %d\n", i + c->lpt_first, c->ltab[i].free,
714 c->ltab[i].dirty, c->ltab[i].tgc, c->ltab[i].cmt);
715 spin_unlock(&dbg_lock);
718 void dbg_dump_leb(const struct ubifs_info *c, int lnum)
720 struct ubifs_scan_leb *sleb;
721 struct ubifs_scan_node *snod;
723 if (dbg_failure_mode)
724 return;
726 printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n",
727 current->pid, lnum);
728 sleb = ubifs_scan(c, lnum, 0, c->dbg->buf);
729 if (IS_ERR(sleb)) {
730 ubifs_err("scan error %d", (int)PTR_ERR(sleb));
731 return;
734 printk(KERN_DEBUG "LEB %d has %d nodes ending at %d\n", lnum,
735 sleb->nodes_cnt, sleb->endpt);
737 list_for_each_entry(snod, &sleb->nodes, list) {
738 cond_resched();
739 printk(KERN_DEBUG "Dumping node at LEB %d:%d len %d\n", lnum,
740 snod->offs, snod->len);
741 dbg_dump_node(c, snod->node);
744 printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n",
745 current->pid, lnum);
746 ubifs_scan_destroy(sleb);
747 return;
750 void dbg_dump_znode(const struct ubifs_info *c,
751 const struct ubifs_znode *znode)
753 int n;
754 const struct ubifs_zbranch *zbr;
756 spin_lock(&dbg_lock);
757 if (znode->parent)
758 zbr = &znode->parent->zbranch[znode->iip];
759 else
760 zbr = &c->zroot;
762 printk(KERN_DEBUG "znode %p, LEB %d:%d len %d parent %p iip %d level %d"
763 " child_cnt %d flags %lx\n", znode, zbr->lnum, zbr->offs,
764 zbr->len, znode->parent, znode->iip, znode->level,
765 znode->child_cnt, znode->flags);
767 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
768 spin_unlock(&dbg_lock);
769 return;
772 printk(KERN_DEBUG "zbranches:\n");
773 for (n = 0; n < znode->child_cnt; n++) {
774 zbr = &znode->zbranch[n];
775 if (znode->level > 0)
776 printk(KERN_DEBUG "\t%d: znode %p LEB %d:%d len %d key "
777 "%s\n", n, zbr->znode, zbr->lnum,
778 zbr->offs, zbr->len,
779 DBGKEY(&zbr->key));
780 else
781 printk(KERN_DEBUG "\t%d: LNC %p LEB %d:%d len %d key "
782 "%s\n", n, zbr->znode, zbr->lnum,
783 zbr->offs, zbr->len,
784 DBGKEY(&zbr->key));
786 spin_unlock(&dbg_lock);
789 void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
791 int i;
793 printk(KERN_DEBUG "(pid %d) start dumping heap cat %d (%d elements)\n",
794 current->pid, cat, heap->cnt);
795 for (i = 0; i < heap->cnt; i++) {
796 struct ubifs_lprops *lprops = heap->arr[i];
798 printk(KERN_DEBUG "\t%d. LEB %d hpos %d free %d dirty %d "
799 "flags %d\n", i, lprops->lnum, lprops->hpos,
800 lprops->free, lprops->dirty, lprops->flags);
802 printk(KERN_DEBUG "(pid %d) finish dumping heap\n", current->pid);
805 void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
806 struct ubifs_nnode *parent, int iip)
808 int i;
810 printk(KERN_DEBUG "(pid %d) dumping pnode:\n", current->pid);
811 printk(KERN_DEBUG "\taddress %zx parent %zx cnext %zx\n",
812 (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
813 printk(KERN_DEBUG "\tflags %lu iip %d level %d num %d\n",
814 pnode->flags, iip, pnode->level, pnode->num);
815 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
816 struct ubifs_lprops *lp = &pnode->lprops[i];
818 printk(KERN_DEBUG "\t%d: free %d dirty %d flags %d lnum %d\n",
819 i, lp->free, lp->dirty, lp->flags, lp->lnum);
823 void dbg_dump_tnc(struct ubifs_info *c)
825 struct ubifs_znode *znode;
826 int level;
828 printk(KERN_DEBUG "\n");
829 printk(KERN_DEBUG "(pid %d) start dumping TNC tree\n", current->pid);
830 znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
831 level = znode->level;
832 printk(KERN_DEBUG "== Level %d ==\n", level);
833 while (znode) {
834 if (level != znode->level) {
835 level = znode->level;
836 printk(KERN_DEBUG "== Level %d ==\n", level);
838 dbg_dump_znode(c, znode);
839 znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
841 printk(KERN_DEBUG "(pid %d) finish dumping TNC tree\n", current->pid);
844 static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
845 void *priv)
847 dbg_dump_znode(c, znode);
848 return 0;
852 * dbg_dump_index - dump the on-flash index.
853 * @c: UBIFS file-system description object
855 * This function dumps whole UBIFS indexing B-tree, unlike 'dbg_dump_tnc()'
856 * which dumps only in-memory znodes and does not read znodes which from flash.
858 void dbg_dump_index(struct ubifs_info *c)
860 dbg_walk_index(c, NULL, dump_znode, NULL);
864 * dbg_check_synced_i_size - check synchronized inode size.
865 * @inode: inode to check
867 * If inode is clean, synchronized inode size has to be equivalent to current
868 * inode size. This function has to be called only for locked inodes (@i_mutex
869 * has to be locked). Returns %0 if synchronized inode size if correct, and
870 * %-EINVAL if not.
872 int dbg_check_synced_i_size(struct inode *inode)
874 int err = 0;
875 struct ubifs_inode *ui = ubifs_inode(inode);
877 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
878 return 0;
879 if (!S_ISREG(inode->i_mode))
880 return 0;
882 mutex_lock(&ui->ui_mutex);
883 spin_lock(&ui->ui_lock);
884 if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
885 ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode "
886 "is clean", ui->ui_size, ui->synced_i_size);
887 ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
888 inode->i_mode, i_size_read(inode));
889 dbg_dump_stack();
890 err = -EINVAL;
892 spin_unlock(&ui->ui_lock);
893 mutex_unlock(&ui->ui_mutex);
894 return err;
898 * dbg_check_dir - check directory inode size and link count.
899 * @c: UBIFS file-system description object
900 * @dir: the directory to calculate size for
901 * @size: the result is returned here
903 * This function makes sure that directory size and link count are correct.
904 * Returns zero in case of success and a negative error code in case of
905 * failure.
907 * Note, it is good idea to make sure the @dir->i_mutex is locked before
908 * calling this function.
910 int dbg_check_dir_size(struct ubifs_info *c, const struct inode *dir)
912 unsigned int nlink = 2;
913 union ubifs_key key;
914 struct ubifs_dent_node *dent, *pdent = NULL;
915 struct qstr nm = { .name = NULL };
916 loff_t size = UBIFS_INO_NODE_SZ;
918 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
919 return 0;
921 if (!S_ISDIR(dir->i_mode))
922 return 0;
924 lowest_dent_key(c, &key, dir->i_ino);
925 while (1) {
926 int err;
928 dent = ubifs_tnc_next_ent(c, &key, &nm);
929 if (IS_ERR(dent)) {
930 err = PTR_ERR(dent);
931 if (err == -ENOENT)
932 break;
933 return err;
936 nm.name = dent->name;
937 nm.len = le16_to_cpu(dent->nlen);
938 size += CALC_DENT_SIZE(nm.len);
939 if (dent->type == UBIFS_ITYPE_DIR)
940 nlink += 1;
941 kfree(pdent);
942 pdent = dent;
943 key_read(c, &dent->key, &key);
945 kfree(pdent);
947 if (i_size_read(dir) != size) {
948 ubifs_err("directory inode %lu has size %llu, "
949 "but calculated size is %llu", dir->i_ino,
950 (unsigned long long)i_size_read(dir),
951 (unsigned long long)size);
952 dump_stack();
953 return -EINVAL;
955 if (dir->i_nlink != nlink) {
956 ubifs_err("directory inode %lu has nlink %u, but calculated "
957 "nlink is %u", dir->i_ino, dir->i_nlink, nlink);
958 dump_stack();
959 return -EINVAL;
962 return 0;
966 * dbg_check_key_order - make sure that colliding keys are properly ordered.
967 * @c: UBIFS file-system description object
968 * @zbr1: first zbranch
969 * @zbr2: following zbranch
971 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
972 * names of the direntries/xentries which are referred by the keys. This
973 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
974 * sure the name of direntry/xentry referred by @zbr1 is less than
975 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
976 * and a negative error code in case of failure.
978 static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
979 struct ubifs_zbranch *zbr2)
981 int err, nlen1, nlen2, cmp;
982 struct ubifs_dent_node *dent1, *dent2;
983 union ubifs_key key;
985 ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
986 dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
987 if (!dent1)
988 return -ENOMEM;
989 dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
990 if (!dent2) {
991 err = -ENOMEM;
992 goto out_free;
995 err = ubifs_tnc_read_node(c, zbr1, dent1);
996 if (err)
997 goto out_free;
998 err = ubifs_validate_entry(c, dent1);
999 if (err)
1000 goto out_free;
1002 err = ubifs_tnc_read_node(c, zbr2, dent2);
1003 if (err)
1004 goto out_free;
1005 err = ubifs_validate_entry(c, dent2);
1006 if (err)
1007 goto out_free;
1009 /* Make sure node keys are the same as in zbranch */
1010 err = 1;
1011 key_read(c, &dent1->key, &key);
1012 if (keys_cmp(c, &zbr1->key, &key)) {
1013 dbg_err("1st entry at %d:%d has key %s", zbr1->lnum,
1014 zbr1->offs, DBGKEY(&key));
1015 dbg_err("but it should have key %s according to tnc",
1016 DBGKEY(&zbr1->key));
1017 dbg_dump_node(c, dent1);
1018 goto out_free;
1021 key_read(c, &dent2->key, &key);
1022 if (keys_cmp(c, &zbr2->key, &key)) {
1023 dbg_err("2nd entry at %d:%d has key %s", zbr1->lnum,
1024 zbr1->offs, DBGKEY(&key));
1025 dbg_err("but it should have key %s according to tnc",
1026 DBGKEY(&zbr2->key));
1027 dbg_dump_node(c, dent2);
1028 goto out_free;
1031 nlen1 = le16_to_cpu(dent1->nlen);
1032 nlen2 = le16_to_cpu(dent2->nlen);
1034 cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
1035 if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
1036 err = 0;
1037 goto out_free;
1039 if (cmp == 0 && nlen1 == nlen2)
1040 dbg_err("2 xent/dent nodes with the same name");
1041 else
1042 dbg_err("bad order of colliding key %s",
1043 DBGKEY(&key));
1045 ubifs_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs);
1046 dbg_dump_node(c, dent1);
1047 ubifs_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs);
1048 dbg_dump_node(c, dent2);
1050 out_free:
1051 kfree(dent2);
1052 kfree(dent1);
1053 return err;
1057 * dbg_check_znode - check if znode is all right.
1058 * @c: UBIFS file-system description object
1059 * @zbr: zbranch which points to this znode
1061 * This function makes sure that znode referred to by @zbr is all right.
1062 * Returns zero if it is, and %-EINVAL if it is not.
1064 static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
1066 struct ubifs_znode *znode = zbr->znode;
1067 struct ubifs_znode *zp = znode->parent;
1068 int n, err, cmp;
1070 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
1071 err = 1;
1072 goto out;
1074 if (znode->level < 0) {
1075 err = 2;
1076 goto out;
1078 if (znode->iip < 0 || znode->iip >= c->fanout) {
1079 err = 3;
1080 goto out;
1083 if (zbr->len == 0)
1084 /* Only dirty zbranch may have no on-flash nodes */
1085 if (!ubifs_zn_dirty(znode)) {
1086 err = 4;
1087 goto out;
1090 if (ubifs_zn_dirty(znode)) {
1092 * If znode is dirty, its parent has to be dirty as well. The
1093 * order of the operation is important, so we have to have
1094 * memory barriers.
1096 smp_mb();
1097 if (zp && !ubifs_zn_dirty(zp)) {
1099 * The dirty flag is atomic and is cleared outside the
1100 * TNC mutex, so znode's dirty flag may now have
1101 * been cleared. The child is always cleared before the
1102 * parent, so we just need to check again.
1104 smp_mb();
1105 if (ubifs_zn_dirty(znode)) {
1106 err = 5;
1107 goto out;
1112 if (zp) {
1113 const union ubifs_key *min, *max;
1115 if (znode->level != zp->level - 1) {
1116 err = 6;
1117 goto out;
1120 /* Make sure the 'parent' pointer in our znode is correct */
1121 err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
1122 if (!err) {
1123 /* This zbranch does not exist in the parent */
1124 err = 7;
1125 goto out;
1128 if (znode->iip >= zp->child_cnt) {
1129 err = 8;
1130 goto out;
1133 if (znode->iip != n) {
1134 /* This may happen only in case of collisions */
1135 if (keys_cmp(c, &zp->zbranch[n].key,
1136 &zp->zbranch[znode->iip].key)) {
1137 err = 9;
1138 goto out;
1140 n = znode->iip;
1144 * Make sure that the first key in our znode is greater than or
1145 * equal to the key in the pointing zbranch.
1147 min = &zbr->key;
1148 cmp = keys_cmp(c, min, &znode->zbranch[0].key);
1149 if (cmp == 1) {
1150 err = 10;
1151 goto out;
1154 if (n + 1 < zp->child_cnt) {
1155 max = &zp->zbranch[n + 1].key;
1158 * Make sure the last key in our znode is less or
1159 * equivalent than the the key in zbranch which goes
1160 * after our pointing zbranch.
1162 cmp = keys_cmp(c, max,
1163 &znode->zbranch[znode->child_cnt - 1].key);
1164 if (cmp == -1) {
1165 err = 11;
1166 goto out;
1169 } else {
1170 /* This may only be root znode */
1171 if (zbr != &c->zroot) {
1172 err = 12;
1173 goto out;
1178 * Make sure that next key is greater or equivalent then the previous
1179 * one.
1181 for (n = 1; n < znode->child_cnt; n++) {
1182 cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
1183 &znode->zbranch[n].key);
1184 if (cmp > 0) {
1185 err = 13;
1186 goto out;
1188 if (cmp == 0) {
1189 /* This can only be keys with colliding hash */
1190 if (!is_hash_key(c, &znode->zbranch[n].key)) {
1191 err = 14;
1192 goto out;
1195 if (znode->level != 0 || c->replaying)
1196 continue;
1199 * Colliding keys should follow binary order of
1200 * corresponding xentry/dentry names.
1202 err = dbg_check_key_order(c, &znode->zbranch[n - 1],
1203 &znode->zbranch[n]);
1204 if (err < 0)
1205 return err;
1206 if (err) {
1207 err = 15;
1208 goto out;
1213 for (n = 0; n < znode->child_cnt; n++) {
1214 if (!znode->zbranch[n].znode &&
1215 (znode->zbranch[n].lnum == 0 ||
1216 znode->zbranch[n].len == 0)) {
1217 err = 16;
1218 goto out;
1221 if (znode->zbranch[n].lnum != 0 &&
1222 znode->zbranch[n].len == 0) {
1223 err = 17;
1224 goto out;
1227 if (znode->zbranch[n].lnum == 0 &&
1228 znode->zbranch[n].len != 0) {
1229 err = 18;
1230 goto out;
1233 if (znode->zbranch[n].lnum == 0 &&
1234 znode->zbranch[n].offs != 0) {
1235 err = 19;
1236 goto out;
1239 if (znode->level != 0 && znode->zbranch[n].znode)
1240 if (znode->zbranch[n].znode->parent != znode) {
1241 err = 20;
1242 goto out;
1246 return 0;
1248 out:
1249 ubifs_err("failed, error %d", err);
1250 ubifs_msg("dump of the znode");
1251 dbg_dump_znode(c, znode);
1252 if (zp) {
1253 ubifs_msg("dump of the parent znode");
1254 dbg_dump_znode(c, zp);
1256 dump_stack();
1257 return -EINVAL;
1261 * dbg_check_tnc - check TNC tree.
1262 * @c: UBIFS file-system description object
1263 * @extra: do extra checks that are possible at start commit
1265 * This function traverses whole TNC tree and checks every znode. Returns zero
1266 * if everything is all right and %-EINVAL if something is wrong with TNC.
1268 int dbg_check_tnc(struct ubifs_info *c, int extra)
1270 struct ubifs_znode *znode;
1271 long clean_cnt = 0, dirty_cnt = 0;
1272 int err, last;
1274 if (!(ubifs_chk_flags & UBIFS_CHK_TNC))
1275 return 0;
1277 ubifs_assert(mutex_is_locked(&c->tnc_mutex));
1278 if (!c->zroot.znode)
1279 return 0;
1281 znode = ubifs_tnc_postorder_first(c->zroot.znode);
1282 while (1) {
1283 struct ubifs_znode *prev;
1284 struct ubifs_zbranch *zbr;
1286 if (!znode->parent)
1287 zbr = &c->zroot;
1288 else
1289 zbr = &znode->parent->zbranch[znode->iip];
1291 err = dbg_check_znode(c, zbr);
1292 if (err)
1293 return err;
1295 if (extra) {
1296 if (ubifs_zn_dirty(znode))
1297 dirty_cnt += 1;
1298 else
1299 clean_cnt += 1;
1302 prev = znode;
1303 znode = ubifs_tnc_postorder_next(znode);
1304 if (!znode)
1305 break;
1308 * If the last key of this znode is equivalent to the first key
1309 * of the next znode (collision), then check order of the keys.
1311 last = prev->child_cnt - 1;
1312 if (prev->level == 0 && znode->level == 0 && !c->replaying &&
1313 !keys_cmp(c, &prev->zbranch[last].key,
1314 &znode->zbranch[0].key)) {
1315 err = dbg_check_key_order(c, &prev->zbranch[last],
1316 &znode->zbranch[0]);
1317 if (err < 0)
1318 return err;
1319 if (err) {
1320 ubifs_msg("first znode");
1321 dbg_dump_znode(c, prev);
1322 ubifs_msg("second znode");
1323 dbg_dump_znode(c, znode);
1324 return -EINVAL;
1329 if (extra) {
1330 if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
1331 ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld",
1332 atomic_long_read(&c->clean_zn_cnt),
1333 clean_cnt);
1334 return -EINVAL;
1336 if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
1337 ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld",
1338 atomic_long_read(&c->dirty_zn_cnt),
1339 dirty_cnt);
1340 return -EINVAL;
1344 return 0;
1348 * dbg_walk_index - walk the on-flash index.
1349 * @c: UBIFS file-system description object
1350 * @leaf_cb: called for each leaf node
1351 * @znode_cb: called for each indexing node
1352 * @priv: private date which is passed to callbacks
1354 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1355 * node and @znode_cb for each indexing node. Returns zero in case of success
1356 * and a negative error code in case of failure.
1358 * It would be better if this function removed every znode it pulled to into
1359 * the TNC, so that the behavior more closely matched the non-debugging
1360 * behavior.
1362 int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1363 dbg_znode_callback znode_cb, void *priv)
1365 int err;
1366 struct ubifs_zbranch *zbr;
1367 struct ubifs_znode *znode, *child;
1369 mutex_lock(&c->tnc_mutex);
1370 /* If the root indexing node is not in TNC - pull it */
1371 if (!c->zroot.znode) {
1372 c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1373 if (IS_ERR(c->zroot.znode)) {
1374 err = PTR_ERR(c->zroot.znode);
1375 c->zroot.znode = NULL;
1376 goto out_unlock;
1381 * We are going to traverse the indexing tree in the postorder manner.
1382 * Go down and find the leftmost indexing node where we are going to
1383 * start from.
1385 znode = c->zroot.znode;
1386 while (znode->level > 0) {
1387 zbr = &znode->zbranch[0];
1388 child = zbr->znode;
1389 if (!child) {
1390 child = ubifs_load_znode(c, zbr, znode, 0);
1391 if (IS_ERR(child)) {
1392 err = PTR_ERR(child);
1393 goto out_unlock;
1395 zbr->znode = child;
1398 znode = child;
1401 /* Iterate over all indexing nodes */
1402 while (1) {
1403 int idx;
1405 cond_resched();
1407 if (znode_cb) {
1408 err = znode_cb(c, znode, priv);
1409 if (err) {
1410 ubifs_err("znode checking function returned "
1411 "error %d", err);
1412 dbg_dump_znode(c, znode);
1413 goto out_dump;
1416 if (leaf_cb && znode->level == 0) {
1417 for (idx = 0; idx < znode->child_cnt; idx++) {
1418 zbr = &znode->zbranch[idx];
1419 err = leaf_cb(c, zbr, priv);
1420 if (err) {
1421 ubifs_err("leaf checking function "
1422 "returned error %d, for leaf "
1423 "at LEB %d:%d",
1424 err, zbr->lnum, zbr->offs);
1425 goto out_dump;
1430 if (!znode->parent)
1431 break;
1433 idx = znode->iip + 1;
1434 znode = znode->parent;
1435 if (idx < znode->child_cnt) {
1436 /* Switch to the next index in the parent */
1437 zbr = &znode->zbranch[idx];
1438 child = zbr->znode;
1439 if (!child) {
1440 child = ubifs_load_znode(c, zbr, znode, idx);
1441 if (IS_ERR(child)) {
1442 err = PTR_ERR(child);
1443 goto out_unlock;
1445 zbr->znode = child;
1447 znode = child;
1448 } else
1450 * This is the last child, switch to the parent and
1451 * continue.
1453 continue;
1455 /* Go to the lowest leftmost znode in the new sub-tree */
1456 while (znode->level > 0) {
1457 zbr = &znode->zbranch[0];
1458 child = zbr->znode;
1459 if (!child) {
1460 child = ubifs_load_znode(c, zbr, znode, 0);
1461 if (IS_ERR(child)) {
1462 err = PTR_ERR(child);
1463 goto out_unlock;
1465 zbr->znode = child;
1467 znode = child;
1471 mutex_unlock(&c->tnc_mutex);
1472 return 0;
1474 out_dump:
1475 if (znode->parent)
1476 zbr = &znode->parent->zbranch[znode->iip];
1477 else
1478 zbr = &c->zroot;
1479 ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1480 dbg_dump_znode(c, znode);
1481 out_unlock:
1482 mutex_unlock(&c->tnc_mutex);
1483 return err;
1487 * add_size - add znode size to partially calculated index size.
1488 * @c: UBIFS file-system description object
1489 * @znode: znode to add size for
1490 * @priv: partially calculated index size
1492 * This is a helper function for 'dbg_check_idx_size()' which is called for
1493 * every indexing node and adds its size to the 'long long' variable pointed to
1494 * by @priv.
1496 static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1498 long long *idx_size = priv;
1499 int add;
1501 add = ubifs_idx_node_sz(c, znode->child_cnt);
1502 add = ALIGN(add, 8);
1503 *idx_size += add;
1504 return 0;
1508 * dbg_check_idx_size - check index size.
1509 * @c: UBIFS file-system description object
1510 * @idx_size: size to check
1512 * This function walks the UBIFS index, calculates its size and checks that the
1513 * size is equivalent to @idx_size. Returns zero in case of success and a
1514 * negative error code in case of failure.
1516 int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
1518 int err;
1519 long long calc = 0;
1521 if (!(ubifs_chk_flags & UBIFS_CHK_IDX_SZ))
1522 return 0;
1524 err = dbg_walk_index(c, NULL, add_size, &calc);
1525 if (err) {
1526 ubifs_err("error %d while walking the index", err);
1527 return err;
1530 if (calc != idx_size) {
1531 ubifs_err("index size check failed: calculated size is %lld, "
1532 "should be %lld", calc, idx_size);
1533 dump_stack();
1534 return -EINVAL;
1537 return 0;
1541 * struct fsck_inode - information about an inode used when checking the file-system.
1542 * @rb: link in the RB-tree of inodes
1543 * @inum: inode number
1544 * @mode: inode type, permissions, etc
1545 * @nlink: inode link count
1546 * @xattr_cnt: count of extended attributes
1547 * @references: how many directory/xattr entries refer this inode (calculated
1548 * while walking the index)
1549 * @calc_cnt: for directory inode count of child directories
1550 * @size: inode size (read from on-flash inode)
1551 * @xattr_sz: summary size of all extended attributes (read from on-flash
1552 * inode)
1553 * @calc_sz: for directories calculated directory size
1554 * @calc_xcnt: count of extended attributes
1555 * @calc_xsz: calculated summary size of all extended attributes
1556 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1557 * inode (read from on-flash inode)
1558 * @calc_xnms: calculated sum of lengths of all extended attribute names
1560 struct fsck_inode {
1561 struct rb_node rb;
1562 ino_t inum;
1563 umode_t mode;
1564 unsigned int nlink;
1565 unsigned int xattr_cnt;
1566 int references;
1567 int calc_cnt;
1568 long long size;
1569 unsigned int xattr_sz;
1570 long long calc_sz;
1571 long long calc_xcnt;
1572 long long calc_xsz;
1573 unsigned int xattr_nms;
1574 long long calc_xnms;
1578 * struct fsck_data - private FS checking information.
1579 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1581 struct fsck_data {
1582 struct rb_root inodes;
1586 * add_inode - add inode information to RB-tree of inodes.
1587 * @c: UBIFS file-system description object
1588 * @fsckd: FS checking information
1589 * @ino: raw UBIFS inode to add
1591 * This is a helper function for 'check_leaf()' which adds information about
1592 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1593 * case of success and a negative error code in case of failure.
1595 static struct fsck_inode *add_inode(struct ubifs_info *c,
1596 struct fsck_data *fsckd,
1597 struct ubifs_ino_node *ino)
1599 struct rb_node **p, *parent = NULL;
1600 struct fsck_inode *fscki;
1601 ino_t inum = key_inum_flash(c, &ino->key);
1603 p = &fsckd->inodes.rb_node;
1604 while (*p) {
1605 parent = *p;
1606 fscki = rb_entry(parent, struct fsck_inode, rb);
1607 if (inum < fscki->inum)
1608 p = &(*p)->rb_left;
1609 else if (inum > fscki->inum)
1610 p = &(*p)->rb_right;
1611 else
1612 return fscki;
1615 if (inum > c->highest_inum) {
1616 ubifs_err("too high inode number, max. is %lu",
1617 (unsigned long)c->highest_inum);
1618 return ERR_PTR(-EINVAL);
1621 fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
1622 if (!fscki)
1623 return ERR_PTR(-ENOMEM);
1625 fscki->inum = inum;
1626 fscki->nlink = le32_to_cpu(ino->nlink);
1627 fscki->size = le64_to_cpu(ino->size);
1628 fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
1629 fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
1630 fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
1631 fscki->mode = le32_to_cpu(ino->mode);
1632 if (S_ISDIR(fscki->mode)) {
1633 fscki->calc_sz = UBIFS_INO_NODE_SZ;
1634 fscki->calc_cnt = 2;
1636 rb_link_node(&fscki->rb, parent, p);
1637 rb_insert_color(&fscki->rb, &fsckd->inodes);
1638 return fscki;
1642 * search_inode - search inode in the RB-tree of inodes.
1643 * @fsckd: FS checking information
1644 * @inum: inode number to search
1646 * This is a helper function for 'check_leaf()' which searches inode @inum in
1647 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1648 * the inode was not found.
1650 static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
1652 struct rb_node *p;
1653 struct fsck_inode *fscki;
1655 p = fsckd->inodes.rb_node;
1656 while (p) {
1657 fscki = rb_entry(p, struct fsck_inode, rb);
1658 if (inum < fscki->inum)
1659 p = p->rb_left;
1660 else if (inum > fscki->inum)
1661 p = p->rb_right;
1662 else
1663 return fscki;
1665 return NULL;
1669 * read_add_inode - read inode node and add it to RB-tree of inodes.
1670 * @c: UBIFS file-system description object
1671 * @fsckd: FS checking information
1672 * @inum: inode number to read
1674 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1675 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1676 * information pointer in case of success and a negative error code in case of
1677 * failure.
1679 static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1680 struct fsck_data *fsckd, ino_t inum)
1682 int n, err;
1683 union ubifs_key key;
1684 struct ubifs_znode *znode;
1685 struct ubifs_zbranch *zbr;
1686 struct ubifs_ino_node *ino;
1687 struct fsck_inode *fscki;
1689 fscki = search_inode(fsckd, inum);
1690 if (fscki)
1691 return fscki;
1693 ino_key_init(c, &key, inum);
1694 err = ubifs_lookup_level0(c, &key, &znode, &n);
1695 if (!err) {
1696 ubifs_err("inode %lu not found in index", (unsigned long)inum);
1697 return ERR_PTR(-ENOENT);
1698 } else if (err < 0) {
1699 ubifs_err("error %d while looking up inode %lu",
1700 err, (unsigned long)inum);
1701 return ERR_PTR(err);
1704 zbr = &znode->zbranch[n];
1705 if (zbr->len < UBIFS_INO_NODE_SZ) {
1706 ubifs_err("bad node %lu node length %d",
1707 (unsigned long)inum, zbr->len);
1708 return ERR_PTR(-EINVAL);
1711 ino = kmalloc(zbr->len, GFP_NOFS);
1712 if (!ino)
1713 return ERR_PTR(-ENOMEM);
1715 err = ubifs_tnc_read_node(c, zbr, ino);
1716 if (err) {
1717 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
1718 zbr->lnum, zbr->offs, err);
1719 kfree(ino);
1720 return ERR_PTR(err);
1723 fscki = add_inode(c, fsckd, ino);
1724 kfree(ino);
1725 if (IS_ERR(fscki)) {
1726 ubifs_err("error %ld while adding inode %lu node",
1727 PTR_ERR(fscki), (unsigned long)inum);
1728 return fscki;
1731 return fscki;
1735 * check_leaf - check leaf node.
1736 * @c: UBIFS file-system description object
1737 * @zbr: zbranch of the leaf node to check
1738 * @priv: FS checking information
1740 * This is a helper function for 'dbg_check_filesystem()' which is called for
1741 * every single leaf node while walking the indexing tree. It checks that the
1742 * leaf node referred from the indexing tree exists, has correct CRC, and does
1743 * some other basic validation. This function is also responsible for building
1744 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
1745 * calculates reference count, size, etc for each inode in order to later
1746 * compare them to the information stored inside the inodes and detect possible
1747 * inconsistencies. Returns zero in case of success and a negative error code
1748 * in case of failure.
1750 static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
1751 void *priv)
1753 ino_t inum;
1754 void *node;
1755 struct ubifs_ch *ch;
1756 int err, type = key_type(c, &zbr->key);
1757 struct fsck_inode *fscki;
1759 if (zbr->len < UBIFS_CH_SZ) {
1760 ubifs_err("bad leaf length %d (LEB %d:%d)",
1761 zbr->len, zbr->lnum, zbr->offs);
1762 return -EINVAL;
1765 node = kmalloc(zbr->len, GFP_NOFS);
1766 if (!node)
1767 return -ENOMEM;
1769 err = ubifs_tnc_read_node(c, zbr, node);
1770 if (err) {
1771 ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
1772 zbr->lnum, zbr->offs, err);
1773 goto out_free;
1776 /* If this is an inode node, add it to RB-tree of inodes */
1777 if (type == UBIFS_INO_KEY) {
1778 fscki = add_inode(c, priv, node);
1779 if (IS_ERR(fscki)) {
1780 err = PTR_ERR(fscki);
1781 ubifs_err("error %d while adding inode node", err);
1782 goto out_dump;
1784 goto out;
1787 if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
1788 type != UBIFS_DATA_KEY) {
1789 ubifs_err("unexpected node type %d at LEB %d:%d",
1790 type, zbr->lnum, zbr->offs);
1791 err = -EINVAL;
1792 goto out_free;
1795 ch = node;
1796 if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
1797 ubifs_err("too high sequence number, max. is %llu",
1798 c->max_sqnum);
1799 err = -EINVAL;
1800 goto out_dump;
1803 if (type == UBIFS_DATA_KEY) {
1804 long long blk_offs;
1805 struct ubifs_data_node *dn = node;
1808 * Search the inode node this data node belongs to and insert
1809 * it to the RB-tree of inodes.
1811 inum = key_inum_flash(c, &dn->key);
1812 fscki = read_add_inode(c, priv, inum);
1813 if (IS_ERR(fscki)) {
1814 err = PTR_ERR(fscki);
1815 ubifs_err("error %d while processing data node and "
1816 "trying to find inode node %lu",
1817 err, (unsigned long)inum);
1818 goto out_dump;
1821 /* Make sure the data node is within inode size */
1822 blk_offs = key_block_flash(c, &dn->key);
1823 blk_offs <<= UBIFS_BLOCK_SHIFT;
1824 blk_offs += le32_to_cpu(dn->size);
1825 if (blk_offs > fscki->size) {
1826 ubifs_err("data node at LEB %d:%d is not within inode "
1827 "size %lld", zbr->lnum, zbr->offs,
1828 fscki->size);
1829 err = -EINVAL;
1830 goto out_dump;
1832 } else {
1833 int nlen;
1834 struct ubifs_dent_node *dent = node;
1835 struct fsck_inode *fscki1;
1837 err = ubifs_validate_entry(c, dent);
1838 if (err)
1839 goto out_dump;
1842 * Search the inode node this entry refers to and the parent
1843 * inode node and insert them to the RB-tree of inodes.
1845 inum = le64_to_cpu(dent->inum);
1846 fscki = read_add_inode(c, priv, inum);
1847 if (IS_ERR(fscki)) {
1848 err = PTR_ERR(fscki);
1849 ubifs_err("error %d while processing entry node and "
1850 "trying to find inode node %lu",
1851 err, (unsigned long)inum);
1852 goto out_dump;
1855 /* Count how many direntries or xentries refers this inode */
1856 fscki->references += 1;
1858 inum = key_inum_flash(c, &dent->key);
1859 fscki1 = read_add_inode(c, priv, inum);
1860 if (IS_ERR(fscki1)) {
1861 err = PTR_ERR(fscki);
1862 ubifs_err("error %d while processing entry node and "
1863 "trying to find parent inode node %lu",
1864 err, (unsigned long)inum);
1865 goto out_dump;
1868 nlen = le16_to_cpu(dent->nlen);
1869 if (type == UBIFS_XENT_KEY) {
1870 fscki1->calc_xcnt += 1;
1871 fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
1872 fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
1873 fscki1->calc_xnms += nlen;
1874 } else {
1875 fscki1->calc_sz += CALC_DENT_SIZE(nlen);
1876 if (dent->type == UBIFS_ITYPE_DIR)
1877 fscki1->calc_cnt += 1;
1881 out:
1882 kfree(node);
1883 return 0;
1885 out_dump:
1886 ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
1887 dbg_dump_node(c, node);
1888 out_free:
1889 kfree(node);
1890 return err;
1894 * free_inodes - free RB-tree of inodes.
1895 * @fsckd: FS checking information
1897 static void free_inodes(struct fsck_data *fsckd)
1899 struct rb_node *this = fsckd->inodes.rb_node;
1900 struct fsck_inode *fscki;
1902 while (this) {
1903 if (this->rb_left)
1904 this = this->rb_left;
1905 else if (this->rb_right)
1906 this = this->rb_right;
1907 else {
1908 fscki = rb_entry(this, struct fsck_inode, rb);
1909 this = rb_parent(this);
1910 if (this) {
1911 if (this->rb_left == &fscki->rb)
1912 this->rb_left = NULL;
1913 else
1914 this->rb_right = NULL;
1916 kfree(fscki);
1922 * check_inodes - checks all inodes.
1923 * @c: UBIFS file-system description object
1924 * @fsckd: FS checking information
1926 * This is a helper function for 'dbg_check_filesystem()' which walks the
1927 * RB-tree of inodes after the index scan has been finished, and checks that
1928 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
1929 * %-EINVAL if not, and a negative error code in case of failure.
1931 static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
1933 int n, err;
1934 union ubifs_key key;
1935 struct ubifs_znode *znode;
1936 struct ubifs_zbranch *zbr;
1937 struct ubifs_ino_node *ino;
1938 struct fsck_inode *fscki;
1939 struct rb_node *this = rb_first(&fsckd->inodes);
1941 while (this) {
1942 fscki = rb_entry(this, struct fsck_inode, rb);
1943 this = rb_next(this);
1945 if (S_ISDIR(fscki->mode)) {
1947 * Directories have to have exactly one reference (they
1948 * cannot have hardlinks), although root inode is an
1949 * exception.
1951 if (fscki->inum != UBIFS_ROOT_INO &&
1952 fscki->references != 1) {
1953 ubifs_err("directory inode %lu has %d "
1954 "direntries which refer it, but "
1955 "should be 1",
1956 (unsigned long)fscki->inum,
1957 fscki->references);
1958 goto out_dump;
1960 if (fscki->inum == UBIFS_ROOT_INO &&
1961 fscki->references != 0) {
1962 ubifs_err("root inode %lu has non-zero (%d) "
1963 "direntries which refer it",
1964 (unsigned long)fscki->inum,
1965 fscki->references);
1966 goto out_dump;
1968 if (fscki->calc_sz != fscki->size) {
1969 ubifs_err("directory inode %lu size is %lld, "
1970 "but calculated size is %lld",
1971 (unsigned long)fscki->inum,
1972 fscki->size, fscki->calc_sz);
1973 goto out_dump;
1975 if (fscki->calc_cnt != fscki->nlink) {
1976 ubifs_err("directory inode %lu nlink is %d, "
1977 "but calculated nlink is %d",
1978 (unsigned long)fscki->inum,
1979 fscki->nlink, fscki->calc_cnt);
1980 goto out_dump;
1982 } else {
1983 if (fscki->references != fscki->nlink) {
1984 ubifs_err("inode %lu nlink is %d, but "
1985 "calculated nlink is %d",
1986 (unsigned long)fscki->inum,
1987 fscki->nlink, fscki->references);
1988 goto out_dump;
1991 if (fscki->xattr_sz != fscki->calc_xsz) {
1992 ubifs_err("inode %lu has xattr size %u, but "
1993 "calculated size is %lld",
1994 (unsigned long)fscki->inum, fscki->xattr_sz,
1995 fscki->calc_xsz);
1996 goto out_dump;
1998 if (fscki->xattr_cnt != fscki->calc_xcnt) {
1999 ubifs_err("inode %lu has %u xattrs, but "
2000 "calculated count is %lld",
2001 (unsigned long)fscki->inum,
2002 fscki->xattr_cnt, fscki->calc_xcnt);
2003 goto out_dump;
2005 if (fscki->xattr_nms != fscki->calc_xnms) {
2006 ubifs_err("inode %lu has xattr names' size %u, but "
2007 "calculated names' size is %lld",
2008 (unsigned long)fscki->inum, fscki->xattr_nms,
2009 fscki->calc_xnms);
2010 goto out_dump;
2014 return 0;
2016 out_dump:
2017 /* Read the bad inode and dump it */
2018 ino_key_init(c, &key, fscki->inum);
2019 err = ubifs_lookup_level0(c, &key, &znode, &n);
2020 if (!err) {
2021 ubifs_err("inode %lu not found in index",
2022 (unsigned long)fscki->inum);
2023 return -ENOENT;
2024 } else if (err < 0) {
2025 ubifs_err("error %d while looking up inode %lu",
2026 err, (unsigned long)fscki->inum);
2027 return err;
2030 zbr = &znode->zbranch[n];
2031 ino = kmalloc(zbr->len, GFP_NOFS);
2032 if (!ino)
2033 return -ENOMEM;
2035 err = ubifs_tnc_read_node(c, zbr, ino);
2036 if (err) {
2037 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2038 zbr->lnum, zbr->offs, err);
2039 kfree(ino);
2040 return err;
2043 ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",
2044 (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
2045 dbg_dump_node(c, ino);
2046 kfree(ino);
2047 return -EINVAL;
2051 * dbg_check_filesystem - check the file-system.
2052 * @c: UBIFS file-system description object
2054 * This function checks the file system, namely:
2055 * o makes sure that all leaf nodes exist and their CRCs are correct;
2056 * o makes sure inode nlink, size, xattr size/count are correct (for all
2057 * inodes).
2059 * The function reads whole indexing tree and all nodes, so it is pretty
2060 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2061 * not, and a negative error code in case of failure.
2063 int dbg_check_filesystem(struct ubifs_info *c)
2065 int err;
2066 struct fsck_data fsckd;
2068 if (!(ubifs_chk_flags & UBIFS_CHK_FS))
2069 return 0;
2071 fsckd.inodes = RB_ROOT;
2072 err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
2073 if (err)
2074 goto out_free;
2076 err = check_inodes(c, &fsckd);
2077 if (err)
2078 goto out_free;
2080 free_inodes(&fsckd);
2081 return 0;
2083 out_free:
2084 ubifs_err("file-system check failed with error %d", err);
2085 dump_stack();
2086 free_inodes(&fsckd);
2087 return err;
2090 static int invocation_cnt;
2092 int dbg_force_in_the_gaps(void)
2094 if (!dbg_force_in_the_gaps_enabled)
2095 return 0;
2096 /* Force in-the-gaps every 8th commit */
2097 return !((invocation_cnt++) & 0x7);
2100 /* Failure mode for recovery testing */
2102 #define chance(n, d) (simple_rand() <= (n) * 32768LL / (d))
2104 struct failure_mode_info {
2105 struct list_head list;
2106 struct ubifs_info *c;
2109 static LIST_HEAD(fmi_list);
2110 static DEFINE_SPINLOCK(fmi_lock);
2112 static unsigned int next;
2114 static int simple_rand(void)
2116 if (next == 0)
2117 next = current->pid;
2118 next = next * 1103515245 + 12345;
2119 return (next >> 16) & 32767;
2122 static void failure_mode_init(struct ubifs_info *c)
2124 struct failure_mode_info *fmi;
2126 fmi = kmalloc(sizeof(struct failure_mode_info), GFP_NOFS);
2127 if (!fmi) {
2128 ubifs_err("Failed to register failure mode - no memory");
2129 return;
2131 fmi->c = c;
2132 spin_lock(&fmi_lock);
2133 list_add_tail(&fmi->list, &fmi_list);
2134 spin_unlock(&fmi_lock);
2137 static void failure_mode_exit(struct ubifs_info *c)
2139 struct failure_mode_info *fmi, *tmp;
2141 spin_lock(&fmi_lock);
2142 list_for_each_entry_safe(fmi, tmp, &fmi_list, list)
2143 if (fmi->c == c) {
2144 list_del(&fmi->list);
2145 kfree(fmi);
2147 spin_unlock(&fmi_lock);
2150 static struct ubifs_info *dbg_find_info(struct ubi_volume_desc *desc)
2152 struct failure_mode_info *fmi;
2154 spin_lock(&fmi_lock);
2155 list_for_each_entry(fmi, &fmi_list, list)
2156 if (fmi->c->ubi == desc) {
2157 struct ubifs_info *c = fmi->c;
2159 spin_unlock(&fmi_lock);
2160 return c;
2162 spin_unlock(&fmi_lock);
2163 return NULL;
2166 static int in_failure_mode(struct ubi_volume_desc *desc)
2168 struct ubifs_info *c = dbg_find_info(desc);
2170 if (c && dbg_failure_mode)
2171 return c->dbg->failure_mode;
2172 return 0;
2175 static int do_fail(struct ubi_volume_desc *desc, int lnum, int write)
2177 struct ubifs_info *c = dbg_find_info(desc);
2178 struct ubifs_debug_info *d;
2180 if (!c || !dbg_failure_mode)
2181 return 0;
2182 d = c->dbg;
2183 if (d->failure_mode)
2184 return 1;
2185 if (!d->fail_cnt) {
2186 /* First call - decide delay to failure */
2187 if (chance(1, 2)) {
2188 unsigned int delay = 1 << (simple_rand() >> 11);
2190 if (chance(1, 2)) {
2191 d->fail_delay = 1;
2192 d->fail_timeout = jiffies +
2193 msecs_to_jiffies(delay);
2194 dbg_rcvry("failing after %ums", delay);
2195 } else {
2196 d->fail_delay = 2;
2197 d->fail_cnt_max = delay;
2198 dbg_rcvry("failing after %u calls", delay);
2201 d->fail_cnt += 1;
2203 /* Determine if failure delay has expired */
2204 if (d->fail_delay == 1) {
2205 if (time_before(jiffies, d->fail_timeout))
2206 return 0;
2207 } else if (d->fail_delay == 2)
2208 if (d->fail_cnt++ < d->fail_cnt_max)
2209 return 0;
2210 if (lnum == UBIFS_SB_LNUM) {
2211 if (write) {
2212 if (chance(1, 2))
2213 return 0;
2214 } else if (chance(19, 20))
2215 return 0;
2216 dbg_rcvry("failing in super block LEB %d", lnum);
2217 } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2218 if (chance(19, 20))
2219 return 0;
2220 dbg_rcvry("failing in master LEB %d", lnum);
2221 } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2222 if (write) {
2223 if (chance(99, 100))
2224 return 0;
2225 } else if (chance(399, 400))
2226 return 0;
2227 dbg_rcvry("failing in log LEB %d", lnum);
2228 } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2229 if (write) {
2230 if (chance(7, 8))
2231 return 0;
2232 } else if (chance(19, 20))
2233 return 0;
2234 dbg_rcvry("failing in LPT LEB %d", lnum);
2235 } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2236 if (write) {
2237 if (chance(1, 2))
2238 return 0;
2239 } else if (chance(9, 10))
2240 return 0;
2241 dbg_rcvry("failing in orphan LEB %d", lnum);
2242 } else if (lnum == c->ihead_lnum) {
2243 if (chance(99, 100))
2244 return 0;
2245 dbg_rcvry("failing in index head LEB %d", lnum);
2246 } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2247 if (chance(9, 10))
2248 return 0;
2249 dbg_rcvry("failing in GC head LEB %d", lnum);
2250 } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
2251 !ubifs_search_bud(c, lnum)) {
2252 if (chance(19, 20))
2253 return 0;
2254 dbg_rcvry("failing in non-bud LEB %d", lnum);
2255 } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
2256 c->cmt_state == COMMIT_RUNNING_REQUIRED) {
2257 if (chance(999, 1000))
2258 return 0;
2259 dbg_rcvry("failing in bud LEB %d commit running", lnum);
2260 } else {
2261 if (chance(9999, 10000))
2262 return 0;
2263 dbg_rcvry("failing in bud LEB %d commit not running", lnum);
2265 ubifs_err("*** SETTING FAILURE MODE ON (LEB %d) ***", lnum);
2266 d->failure_mode = 1;
2267 dump_stack();
2268 return 1;
2271 static void cut_data(const void *buf, int len)
2273 int flen, i;
2274 unsigned char *p = (void *)buf;
2276 flen = (len * (long long)simple_rand()) >> 15;
2277 for (i = flen; i < len; i++)
2278 p[i] = 0xff;
2281 int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
2282 int len, int check)
2284 if (in_failure_mode(desc))
2285 return -EIO;
2286 return ubi_leb_read(desc, lnum, buf, offset, len, check);
2289 int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
2290 int offset, int len, int dtype)
2292 int err, failing;
2294 if (in_failure_mode(desc))
2295 return -EIO;
2296 failing = do_fail(desc, lnum, 1);
2297 if (failing)
2298 cut_data(buf, len);
2299 err = ubi_leb_write(desc, lnum, buf, offset, len, dtype);
2300 if (err)
2301 return err;
2302 if (failing)
2303 return -EIO;
2304 return 0;
2307 int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
2308 int len, int dtype)
2310 int err;
2312 if (do_fail(desc, lnum, 1))
2313 return -EIO;
2314 err = ubi_leb_change(desc, lnum, buf, len, dtype);
2315 if (err)
2316 return err;
2317 if (do_fail(desc, lnum, 1))
2318 return -EIO;
2319 return 0;
2322 int dbg_leb_erase(struct ubi_volume_desc *desc, int lnum)
2324 int err;
2326 if (do_fail(desc, lnum, 0))
2327 return -EIO;
2328 err = ubi_leb_erase(desc, lnum);
2329 if (err)
2330 return err;
2331 if (do_fail(desc, lnum, 0))
2332 return -EIO;
2333 return 0;
2336 int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum)
2338 int err;
2340 if (do_fail(desc, lnum, 0))
2341 return -EIO;
2342 err = ubi_leb_unmap(desc, lnum);
2343 if (err)
2344 return err;
2345 if (do_fail(desc, lnum, 0))
2346 return -EIO;
2347 return 0;
2350 int dbg_is_mapped(struct ubi_volume_desc *desc, int lnum)
2352 if (in_failure_mode(desc))
2353 return -EIO;
2354 return ubi_is_mapped(desc, lnum);
2357 int dbg_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
2359 int err;
2361 if (do_fail(desc, lnum, 0))
2362 return -EIO;
2363 err = ubi_leb_map(desc, lnum, dtype);
2364 if (err)
2365 return err;
2366 if (do_fail(desc, lnum, 0))
2367 return -EIO;
2368 return 0;
2372 * ubifs_debugging_init - initialize UBIFS debugging.
2373 * @c: UBIFS file-system description object
2375 * This function initializes debugging-related data for the file system.
2376 * Returns zero in case of success and a negative error code in case of
2377 * failure.
2379 int ubifs_debugging_init(struct ubifs_info *c)
2381 c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
2382 if (!c->dbg)
2383 return -ENOMEM;
2385 c->dbg->buf = vmalloc(c->leb_size);
2386 if (!c->dbg->buf)
2387 goto out;
2389 failure_mode_init(c);
2390 return 0;
2392 out:
2393 kfree(c->dbg);
2394 return -ENOMEM;
2398 * ubifs_debugging_exit - free debugging data.
2399 * @c: UBIFS file-system description object
2401 void ubifs_debugging_exit(struct ubifs_info *c)
2403 failure_mode_exit(c);
2404 vfree(c->dbg->buf);
2405 kfree(c->dbg);
2409 * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
2410 * contain the stuff specific to particular file-system mounts.
2412 static struct dentry *debugfs_rootdir;
2415 * dbg_debugfs_init - initialize debugfs file-system.
2417 * UBIFS uses debugfs file-system to expose various debugging knobs to
2418 * user-space. This function creates "ubifs" directory in the debugfs
2419 * file-system. Returns zero in case of success and a negative error code in
2420 * case of failure.
2422 int dbg_debugfs_init(void)
2424 debugfs_rootdir = debugfs_create_dir("ubifs", NULL);
2425 if (IS_ERR(debugfs_rootdir)) {
2426 int err = PTR_ERR(debugfs_rootdir);
2427 ubifs_err("cannot create \"ubifs\" debugfs directory, "
2428 "error %d\n", err);
2429 return err;
2432 return 0;
2436 * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
2438 void dbg_debugfs_exit(void)
2440 debugfs_remove(debugfs_rootdir);
2443 static int open_debugfs_file(struct inode *inode, struct file *file)
2445 file->private_data = inode->i_private;
2446 return 0;
2449 static ssize_t write_debugfs_file(struct file *file, const char __user *buf,
2450 size_t count, loff_t *ppos)
2452 struct ubifs_info *c = file->private_data;
2453 struct ubifs_debug_info *d = c->dbg;
2455 if (file->f_path.dentry == d->dump_lprops)
2456 dbg_dump_lprops(c);
2457 else if (file->f_path.dentry == d->dump_budg) {
2458 spin_lock(&c->space_lock);
2459 dbg_dump_budg(c);
2460 spin_unlock(&c->space_lock);
2461 } else if (file->f_path.dentry == d->dump_tnc) {
2462 mutex_lock(&c->tnc_mutex);
2463 dbg_dump_tnc(c);
2464 mutex_unlock(&c->tnc_mutex);
2465 } else
2466 return -EINVAL;
2468 *ppos += count;
2469 return count;
2472 static const struct file_operations debugfs_fops = {
2473 .open = open_debugfs_file,
2474 .write = write_debugfs_file,
2475 .owner = THIS_MODULE,
2479 * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
2480 * @c: UBIFS file-system description object
2482 * This function creates all debugfs files for this instance of UBIFS. Returns
2483 * zero in case of success and a negative error code in case of failure.
2485 * Note, the only reason we have not merged this function with the
2486 * 'ubifs_debugging_init()' function is because it is better to initialize
2487 * debugfs interfaces at the very end of the mount process, and remove them at
2488 * the very beginning of the mount process.
2490 int dbg_debugfs_init_fs(struct ubifs_info *c)
2492 int err;
2493 const char *fname;
2494 struct dentry *dent;
2495 struct ubifs_debug_info *d = c->dbg;
2497 sprintf(d->debugfs_dir_name, "ubi%d_%d", c->vi.ubi_num, c->vi.vol_id);
2498 d->debugfs_dir = debugfs_create_dir(d->debugfs_dir_name,
2499 debugfs_rootdir);
2500 if (IS_ERR(d->debugfs_dir)) {
2501 err = PTR_ERR(d->debugfs_dir);
2502 ubifs_err("cannot create \"%s\" debugfs directory, error %d\n",
2503 d->debugfs_dir_name, err);
2504 goto out;
2507 fname = "dump_lprops";
2508 dent = debugfs_create_file(fname, S_IWUGO, d->debugfs_dir, c,
2509 &debugfs_fops);
2510 if (IS_ERR(dent))
2511 goto out_remove;
2512 d->dump_lprops = dent;
2514 fname = "dump_budg";
2515 dent = debugfs_create_file(fname, S_IWUGO, d->debugfs_dir, c,
2516 &debugfs_fops);
2517 if (IS_ERR(dent))
2518 goto out_remove;
2519 d->dump_budg = dent;
2521 fname = "dump_tnc";
2522 dent = debugfs_create_file(fname, S_IWUGO, d->debugfs_dir, c,
2523 &debugfs_fops);
2524 if (IS_ERR(dent))
2525 goto out_remove;
2526 d->dump_tnc = dent;
2528 return 0;
2530 out_remove:
2531 err = PTR_ERR(dent);
2532 ubifs_err("cannot create \"%s\" debugfs directory, error %d\n",
2533 fname, err);
2534 debugfs_remove_recursive(d->debugfs_dir);
2535 out:
2536 return err;
2540 * dbg_debugfs_exit_fs - remove all debugfs files.
2541 * @c: UBIFS file-system description object
2543 void dbg_debugfs_exit_fs(struct ubifs_info *c)
2545 debugfs_remove_recursive(c->dbg->debugfs_dir);
2548 #endif /* CONFIG_UBIFS_FS_DEBUG */