UBIFS: rearrange the budget dump
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ubifs / debug.c
blob8e8bba937331839cf6059c196002b291fe35dff5
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>
37 #include <linux/slab.h>
39 #ifdef CONFIG_UBIFS_FS_DEBUG
41 DEFINE_SPINLOCK(dbg_lock);
43 static char dbg_key_buf0[128];
44 static char dbg_key_buf1[128];
46 unsigned int ubifs_msg_flags;
47 unsigned int ubifs_chk_flags;
48 unsigned int ubifs_tst_flags;
50 module_param_named(debug_msgs, ubifs_msg_flags, uint, S_IRUGO | S_IWUSR);
51 module_param_named(debug_chks, ubifs_chk_flags, uint, S_IRUGO | S_IWUSR);
52 module_param_named(debug_tsts, ubifs_tst_flags, uint, S_IRUGO | S_IWUSR);
54 MODULE_PARM_DESC(debug_msgs, "Debug message type flags");
55 MODULE_PARM_DESC(debug_chks, "Debug check flags");
56 MODULE_PARM_DESC(debug_tsts, "Debug special test flags");
58 static const char *get_key_fmt(int fmt)
60 switch (fmt) {
61 case UBIFS_SIMPLE_KEY_FMT:
62 return "simple";
63 default:
64 return "unknown/invalid format";
68 static const char *get_key_hash(int hash)
70 switch (hash) {
71 case UBIFS_KEY_HASH_R5:
72 return "R5";
73 case UBIFS_KEY_HASH_TEST:
74 return "test";
75 default:
76 return "unknown/invalid name hash";
80 static const char *get_key_type(int type)
82 switch (type) {
83 case UBIFS_INO_KEY:
84 return "inode";
85 case UBIFS_DENT_KEY:
86 return "direntry";
87 case UBIFS_XENT_KEY:
88 return "xentry";
89 case UBIFS_DATA_KEY:
90 return "data";
91 case UBIFS_TRUN_KEY:
92 return "truncate";
93 default:
94 return "unknown/invalid key";
98 static void sprintf_key(const struct ubifs_info *c, const union ubifs_key *key,
99 char *buffer)
101 char *p = buffer;
102 int type = key_type(c, key);
104 if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
105 switch (type) {
106 case UBIFS_INO_KEY:
107 sprintf(p, "(%lu, %s)", (unsigned long)key_inum(c, key),
108 get_key_type(type));
109 break;
110 case UBIFS_DENT_KEY:
111 case UBIFS_XENT_KEY:
112 sprintf(p, "(%lu, %s, %#08x)",
113 (unsigned long)key_inum(c, key),
114 get_key_type(type), key_hash(c, key));
115 break;
116 case UBIFS_DATA_KEY:
117 sprintf(p, "(%lu, %s, %u)",
118 (unsigned long)key_inum(c, key),
119 get_key_type(type), key_block(c, key));
120 break;
121 case UBIFS_TRUN_KEY:
122 sprintf(p, "(%lu, %s)",
123 (unsigned long)key_inum(c, key),
124 get_key_type(type));
125 break;
126 default:
127 sprintf(p, "(bad key type: %#08x, %#08x)",
128 key->u32[0], key->u32[1]);
130 } else
131 sprintf(p, "bad key format %d", c->key_fmt);
134 const char *dbg_key_str0(const struct ubifs_info *c, const union ubifs_key *key)
136 /* dbg_lock must be held */
137 sprintf_key(c, key, dbg_key_buf0);
138 return dbg_key_buf0;
141 const char *dbg_key_str1(const struct ubifs_info *c, const union ubifs_key *key)
143 /* dbg_lock must be held */
144 sprintf_key(c, key, dbg_key_buf1);
145 return dbg_key_buf1;
148 const char *dbg_ntype(int type)
150 switch (type) {
151 case UBIFS_PAD_NODE:
152 return "padding node";
153 case UBIFS_SB_NODE:
154 return "superblock node";
155 case UBIFS_MST_NODE:
156 return "master node";
157 case UBIFS_REF_NODE:
158 return "reference node";
159 case UBIFS_INO_NODE:
160 return "inode node";
161 case UBIFS_DENT_NODE:
162 return "direntry node";
163 case UBIFS_XENT_NODE:
164 return "xentry node";
165 case UBIFS_DATA_NODE:
166 return "data node";
167 case UBIFS_TRUN_NODE:
168 return "truncate node";
169 case UBIFS_IDX_NODE:
170 return "indexing node";
171 case UBIFS_CS_NODE:
172 return "commit start node";
173 case UBIFS_ORPH_NODE:
174 return "orphan node";
175 default:
176 return "unknown node";
180 static const char *dbg_gtype(int type)
182 switch (type) {
183 case UBIFS_NO_NODE_GROUP:
184 return "no node group";
185 case UBIFS_IN_NODE_GROUP:
186 return "in node group";
187 case UBIFS_LAST_OF_NODE_GROUP:
188 return "last of node group";
189 default:
190 return "unknown";
194 const char *dbg_cstate(int cmt_state)
196 switch (cmt_state) {
197 case COMMIT_RESTING:
198 return "commit resting";
199 case COMMIT_BACKGROUND:
200 return "background commit requested";
201 case COMMIT_REQUIRED:
202 return "commit required";
203 case COMMIT_RUNNING_BACKGROUND:
204 return "BACKGROUND commit running";
205 case COMMIT_RUNNING_REQUIRED:
206 return "commit running and required";
207 case COMMIT_BROKEN:
208 return "broken commit";
209 default:
210 return "unknown commit state";
214 const char *dbg_jhead(int jhead)
216 switch (jhead) {
217 case GCHD:
218 return "0 (GC)";
219 case BASEHD:
220 return "1 (base)";
221 case DATAHD:
222 return "2 (data)";
223 default:
224 return "unknown journal head";
228 static void dump_ch(const struct ubifs_ch *ch)
230 printk(KERN_DEBUG "\tmagic %#x\n", le32_to_cpu(ch->magic));
231 printk(KERN_DEBUG "\tcrc %#x\n", le32_to_cpu(ch->crc));
232 printk(KERN_DEBUG "\tnode_type %d (%s)\n", ch->node_type,
233 dbg_ntype(ch->node_type));
234 printk(KERN_DEBUG "\tgroup_type %d (%s)\n", ch->group_type,
235 dbg_gtype(ch->group_type));
236 printk(KERN_DEBUG "\tsqnum %llu\n",
237 (unsigned long long)le64_to_cpu(ch->sqnum));
238 printk(KERN_DEBUG "\tlen %u\n", le32_to_cpu(ch->len));
241 void dbg_dump_inode(const struct ubifs_info *c, const struct inode *inode)
243 const struct ubifs_inode *ui = ubifs_inode(inode);
245 printk(KERN_DEBUG "Dump in-memory inode:");
246 printk(KERN_DEBUG "\tinode %lu\n", inode->i_ino);
247 printk(KERN_DEBUG "\tsize %llu\n",
248 (unsigned long long)i_size_read(inode));
249 printk(KERN_DEBUG "\tnlink %u\n", inode->i_nlink);
250 printk(KERN_DEBUG "\tuid %u\n", (unsigned int)inode->i_uid);
251 printk(KERN_DEBUG "\tgid %u\n", (unsigned int)inode->i_gid);
252 printk(KERN_DEBUG "\tatime %u.%u\n",
253 (unsigned int)inode->i_atime.tv_sec,
254 (unsigned int)inode->i_atime.tv_nsec);
255 printk(KERN_DEBUG "\tmtime %u.%u\n",
256 (unsigned int)inode->i_mtime.tv_sec,
257 (unsigned int)inode->i_mtime.tv_nsec);
258 printk(KERN_DEBUG "\tctime %u.%u\n",
259 (unsigned int)inode->i_ctime.tv_sec,
260 (unsigned int)inode->i_ctime.tv_nsec);
261 printk(KERN_DEBUG "\tcreat_sqnum %llu\n", ui->creat_sqnum);
262 printk(KERN_DEBUG "\txattr_size %u\n", ui->xattr_size);
263 printk(KERN_DEBUG "\txattr_cnt %u\n", ui->xattr_cnt);
264 printk(KERN_DEBUG "\txattr_names %u\n", ui->xattr_names);
265 printk(KERN_DEBUG "\tdirty %u\n", ui->dirty);
266 printk(KERN_DEBUG "\txattr %u\n", ui->xattr);
267 printk(KERN_DEBUG "\tbulk_read %u\n", ui->xattr);
268 printk(KERN_DEBUG "\tsynced_i_size %llu\n",
269 (unsigned long long)ui->synced_i_size);
270 printk(KERN_DEBUG "\tui_size %llu\n",
271 (unsigned long long)ui->ui_size);
272 printk(KERN_DEBUG "\tflags %d\n", ui->flags);
273 printk(KERN_DEBUG "\tcompr_type %d\n", ui->compr_type);
274 printk(KERN_DEBUG "\tlast_page_read %lu\n", ui->last_page_read);
275 printk(KERN_DEBUG "\tread_in_a_row %lu\n", ui->read_in_a_row);
276 printk(KERN_DEBUG "\tdata_len %d\n", ui->data_len);
279 void dbg_dump_node(const struct ubifs_info *c, const void *node)
281 int i, n;
282 union ubifs_key key;
283 const struct ubifs_ch *ch = node;
285 if (dbg_failure_mode)
286 return;
288 /* If the magic is incorrect, just hexdump the first bytes */
289 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
290 printk(KERN_DEBUG "Not a node, first %zu bytes:", UBIFS_CH_SZ);
291 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
292 (void *)node, UBIFS_CH_SZ, 1);
293 return;
296 spin_lock(&dbg_lock);
297 dump_ch(node);
299 switch (ch->node_type) {
300 case UBIFS_PAD_NODE:
302 const struct ubifs_pad_node *pad = node;
304 printk(KERN_DEBUG "\tpad_len %u\n",
305 le32_to_cpu(pad->pad_len));
306 break;
308 case UBIFS_SB_NODE:
310 const struct ubifs_sb_node *sup = node;
311 unsigned int sup_flags = le32_to_cpu(sup->flags);
313 printk(KERN_DEBUG "\tkey_hash %d (%s)\n",
314 (int)sup->key_hash, get_key_hash(sup->key_hash));
315 printk(KERN_DEBUG "\tkey_fmt %d (%s)\n",
316 (int)sup->key_fmt, get_key_fmt(sup->key_fmt));
317 printk(KERN_DEBUG "\tflags %#x\n", sup_flags);
318 printk(KERN_DEBUG "\t big_lpt %u\n",
319 !!(sup_flags & UBIFS_FLG_BIGLPT));
320 printk(KERN_DEBUG "\tmin_io_size %u\n",
321 le32_to_cpu(sup->min_io_size));
322 printk(KERN_DEBUG "\tleb_size %u\n",
323 le32_to_cpu(sup->leb_size));
324 printk(KERN_DEBUG "\tleb_cnt %u\n",
325 le32_to_cpu(sup->leb_cnt));
326 printk(KERN_DEBUG "\tmax_leb_cnt %u\n",
327 le32_to_cpu(sup->max_leb_cnt));
328 printk(KERN_DEBUG "\tmax_bud_bytes %llu\n",
329 (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
330 printk(KERN_DEBUG "\tlog_lebs %u\n",
331 le32_to_cpu(sup->log_lebs));
332 printk(KERN_DEBUG "\tlpt_lebs %u\n",
333 le32_to_cpu(sup->lpt_lebs));
334 printk(KERN_DEBUG "\torph_lebs %u\n",
335 le32_to_cpu(sup->orph_lebs));
336 printk(KERN_DEBUG "\tjhead_cnt %u\n",
337 le32_to_cpu(sup->jhead_cnt));
338 printk(KERN_DEBUG "\tfanout %u\n",
339 le32_to_cpu(sup->fanout));
340 printk(KERN_DEBUG "\tlsave_cnt %u\n",
341 le32_to_cpu(sup->lsave_cnt));
342 printk(KERN_DEBUG "\tdefault_compr %u\n",
343 (int)le16_to_cpu(sup->default_compr));
344 printk(KERN_DEBUG "\trp_size %llu\n",
345 (unsigned long long)le64_to_cpu(sup->rp_size));
346 printk(KERN_DEBUG "\trp_uid %u\n",
347 le32_to_cpu(sup->rp_uid));
348 printk(KERN_DEBUG "\trp_gid %u\n",
349 le32_to_cpu(sup->rp_gid));
350 printk(KERN_DEBUG "\tfmt_version %u\n",
351 le32_to_cpu(sup->fmt_version));
352 printk(KERN_DEBUG "\ttime_gran %u\n",
353 le32_to_cpu(sup->time_gran));
354 printk(KERN_DEBUG "\tUUID %pUB\n",
355 sup->uuid);
356 break;
358 case UBIFS_MST_NODE:
360 const struct ubifs_mst_node *mst = node;
362 printk(KERN_DEBUG "\thighest_inum %llu\n",
363 (unsigned long long)le64_to_cpu(mst->highest_inum));
364 printk(KERN_DEBUG "\tcommit number %llu\n",
365 (unsigned long long)le64_to_cpu(mst->cmt_no));
366 printk(KERN_DEBUG "\tflags %#x\n",
367 le32_to_cpu(mst->flags));
368 printk(KERN_DEBUG "\tlog_lnum %u\n",
369 le32_to_cpu(mst->log_lnum));
370 printk(KERN_DEBUG "\troot_lnum %u\n",
371 le32_to_cpu(mst->root_lnum));
372 printk(KERN_DEBUG "\troot_offs %u\n",
373 le32_to_cpu(mst->root_offs));
374 printk(KERN_DEBUG "\troot_len %u\n",
375 le32_to_cpu(mst->root_len));
376 printk(KERN_DEBUG "\tgc_lnum %u\n",
377 le32_to_cpu(mst->gc_lnum));
378 printk(KERN_DEBUG "\tihead_lnum %u\n",
379 le32_to_cpu(mst->ihead_lnum));
380 printk(KERN_DEBUG "\tihead_offs %u\n",
381 le32_to_cpu(mst->ihead_offs));
382 printk(KERN_DEBUG "\tindex_size %llu\n",
383 (unsigned long long)le64_to_cpu(mst->index_size));
384 printk(KERN_DEBUG "\tlpt_lnum %u\n",
385 le32_to_cpu(mst->lpt_lnum));
386 printk(KERN_DEBUG "\tlpt_offs %u\n",
387 le32_to_cpu(mst->lpt_offs));
388 printk(KERN_DEBUG "\tnhead_lnum %u\n",
389 le32_to_cpu(mst->nhead_lnum));
390 printk(KERN_DEBUG "\tnhead_offs %u\n",
391 le32_to_cpu(mst->nhead_offs));
392 printk(KERN_DEBUG "\tltab_lnum %u\n",
393 le32_to_cpu(mst->ltab_lnum));
394 printk(KERN_DEBUG "\tltab_offs %u\n",
395 le32_to_cpu(mst->ltab_offs));
396 printk(KERN_DEBUG "\tlsave_lnum %u\n",
397 le32_to_cpu(mst->lsave_lnum));
398 printk(KERN_DEBUG "\tlsave_offs %u\n",
399 le32_to_cpu(mst->lsave_offs));
400 printk(KERN_DEBUG "\tlscan_lnum %u\n",
401 le32_to_cpu(mst->lscan_lnum));
402 printk(KERN_DEBUG "\tleb_cnt %u\n",
403 le32_to_cpu(mst->leb_cnt));
404 printk(KERN_DEBUG "\tempty_lebs %u\n",
405 le32_to_cpu(mst->empty_lebs));
406 printk(KERN_DEBUG "\tidx_lebs %u\n",
407 le32_to_cpu(mst->idx_lebs));
408 printk(KERN_DEBUG "\ttotal_free %llu\n",
409 (unsigned long long)le64_to_cpu(mst->total_free));
410 printk(KERN_DEBUG "\ttotal_dirty %llu\n",
411 (unsigned long long)le64_to_cpu(mst->total_dirty));
412 printk(KERN_DEBUG "\ttotal_used %llu\n",
413 (unsigned long long)le64_to_cpu(mst->total_used));
414 printk(KERN_DEBUG "\ttotal_dead %llu\n",
415 (unsigned long long)le64_to_cpu(mst->total_dead));
416 printk(KERN_DEBUG "\ttotal_dark %llu\n",
417 (unsigned long long)le64_to_cpu(mst->total_dark));
418 break;
420 case UBIFS_REF_NODE:
422 const struct ubifs_ref_node *ref = node;
424 printk(KERN_DEBUG "\tlnum %u\n",
425 le32_to_cpu(ref->lnum));
426 printk(KERN_DEBUG "\toffs %u\n",
427 le32_to_cpu(ref->offs));
428 printk(KERN_DEBUG "\tjhead %u\n",
429 le32_to_cpu(ref->jhead));
430 break;
432 case UBIFS_INO_NODE:
434 const struct ubifs_ino_node *ino = node;
436 key_read(c, &ino->key, &key);
437 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
438 printk(KERN_DEBUG "\tcreat_sqnum %llu\n",
439 (unsigned long long)le64_to_cpu(ino->creat_sqnum));
440 printk(KERN_DEBUG "\tsize %llu\n",
441 (unsigned long long)le64_to_cpu(ino->size));
442 printk(KERN_DEBUG "\tnlink %u\n",
443 le32_to_cpu(ino->nlink));
444 printk(KERN_DEBUG "\tatime %lld.%u\n",
445 (long long)le64_to_cpu(ino->atime_sec),
446 le32_to_cpu(ino->atime_nsec));
447 printk(KERN_DEBUG "\tmtime %lld.%u\n",
448 (long long)le64_to_cpu(ino->mtime_sec),
449 le32_to_cpu(ino->mtime_nsec));
450 printk(KERN_DEBUG "\tctime %lld.%u\n",
451 (long long)le64_to_cpu(ino->ctime_sec),
452 le32_to_cpu(ino->ctime_nsec));
453 printk(KERN_DEBUG "\tuid %u\n",
454 le32_to_cpu(ino->uid));
455 printk(KERN_DEBUG "\tgid %u\n",
456 le32_to_cpu(ino->gid));
457 printk(KERN_DEBUG "\tmode %u\n",
458 le32_to_cpu(ino->mode));
459 printk(KERN_DEBUG "\tflags %#x\n",
460 le32_to_cpu(ino->flags));
461 printk(KERN_DEBUG "\txattr_cnt %u\n",
462 le32_to_cpu(ino->xattr_cnt));
463 printk(KERN_DEBUG "\txattr_size %u\n",
464 le32_to_cpu(ino->xattr_size));
465 printk(KERN_DEBUG "\txattr_names %u\n",
466 le32_to_cpu(ino->xattr_names));
467 printk(KERN_DEBUG "\tcompr_type %#x\n",
468 (int)le16_to_cpu(ino->compr_type));
469 printk(KERN_DEBUG "\tdata len %u\n",
470 le32_to_cpu(ino->data_len));
471 break;
473 case UBIFS_DENT_NODE:
474 case UBIFS_XENT_NODE:
476 const struct ubifs_dent_node *dent = node;
477 int nlen = le16_to_cpu(dent->nlen);
479 key_read(c, &dent->key, &key);
480 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
481 printk(KERN_DEBUG "\tinum %llu\n",
482 (unsigned long long)le64_to_cpu(dent->inum));
483 printk(KERN_DEBUG "\ttype %d\n", (int)dent->type);
484 printk(KERN_DEBUG "\tnlen %d\n", nlen);
485 printk(KERN_DEBUG "\tname ");
487 if (nlen > UBIFS_MAX_NLEN)
488 printk(KERN_DEBUG "(bad name length, not printing, "
489 "bad or corrupted node)");
490 else {
491 for (i = 0; i < nlen && dent->name[i]; i++)
492 printk(KERN_CONT "%c", dent->name[i]);
494 printk(KERN_CONT "\n");
496 break;
498 case UBIFS_DATA_NODE:
500 const struct ubifs_data_node *dn = node;
501 int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
503 key_read(c, &dn->key, &key);
504 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
505 printk(KERN_DEBUG "\tsize %u\n",
506 le32_to_cpu(dn->size));
507 printk(KERN_DEBUG "\tcompr_typ %d\n",
508 (int)le16_to_cpu(dn->compr_type));
509 printk(KERN_DEBUG "\tdata size %d\n",
510 dlen);
511 printk(KERN_DEBUG "\tdata:\n");
512 print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET, 32, 1,
513 (void *)&dn->data, dlen, 0);
514 break;
516 case UBIFS_TRUN_NODE:
518 const struct ubifs_trun_node *trun = node;
520 printk(KERN_DEBUG "\tinum %u\n",
521 le32_to_cpu(trun->inum));
522 printk(KERN_DEBUG "\told_size %llu\n",
523 (unsigned long long)le64_to_cpu(trun->old_size));
524 printk(KERN_DEBUG "\tnew_size %llu\n",
525 (unsigned long long)le64_to_cpu(trun->new_size));
526 break;
528 case UBIFS_IDX_NODE:
530 const struct ubifs_idx_node *idx = node;
532 n = le16_to_cpu(idx->child_cnt);
533 printk(KERN_DEBUG "\tchild_cnt %d\n", n);
534 printk(KERN_DEBUG "\tlevel %d\n",
535 (int)le16_to_cpu(idx->level));
536 printk(KERN_DEBUG "\tBranches:\n");
538 for (i = 0; i < n && i < c->fanout - 1; i++) {
539 const struct ubifs_branch *br;
541 br = ubifs_idx_branch(c, idx, i);
542 key_read(c, &br->key, &key);
543 printk(KERN_DEBUG "\t%d: LEB %d:%d len %d key %s\n",
544 i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
545 le32_to_cpu(br->len), DBGKEY(&key));
547 break;
549 case UBIFS_CS_NODE:
550 break;
551 case UBIFS_ORPH_NODE:
553 const struct ubifs_orph_node *orph = node;
555 printk(KERN_DEBUG "\tcommit number %llu\n",
556 (unsigned long long)
557 le64_to_cpu(orph->cmt_no) & LLONG_MAX);
558 printk(KERN_DEBUG "\tlast node flag %llu\n",
559 (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
560 n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
561 printk(KERN_DEBUG "\t%d orphan inode numbers:\n", n);
562 for (i = 0; i < n; i++)
563 printk(KERN_DEBUG "\t ino %llu\n",
564 (unsigned long long)le64_to_cpu(orph->inos[i]));
565 break;
567 default:
568 printk(KERN_DEBUG "node type %d was not recognized\n",
569 (int)ch->node_type);
571 spin_unlock(&dbg_lock);
574 void dbg_dump_budget_req(const struct ubifs_budget_req *req)
576 spin_lock(&dbg_lock);
577 printk(KERN_DEBUG "Budgeting request: new_ino %d, dirtied_ino %d\n",
578 req->new_ino, req->dirtied_ino);
579 printk(KERN_DEBUG "\tnew_ino_d %d, dirtied_ino_d %d\n",
580 req->new_ino_d, req->dirtied_ino_d);
581 printk(KERN_DEBUG "\tnew_page %d, dirtied_page %d\n",
582 req->new_page, req->dirtied_page);
583 printk(KERN_DEBUG "\tnew_dent %d, mod_dent %d\n",
584 req->new_dent, req->mod_dent);
585 printk(KERN_DEBUG "\tidx_growth %d\n", req->idx_growth);
586 printk(KERN_DEBUG "\tdata_growth %d dd_growth %d\n",
587 req->data_growth, req->dd_growth);
588 spin_unlock(&dbg_lock);
591 void dbg_dump_lstats(const struct ubifs_lp_stats *lst)
593 spin_lock(&dbg_lock);
594 printk(KERN_DEBUG "(pid %d) Lprops statistics: empty_lebs %d, "
595 "idx_lebs %d\n", current->pid, lst->empty_lebs, lst->idx_lebs);
596 printk(KERN_DEBUG "\ttaken_empty_lebs %d, total_free %lld, "
597 "total_dirty %lld\n", lst->taken_empty_lebs, lst->total_free,
598 lst->total_dirty);
599 printk(KERN_DEBUG "\ttotal_used %lld, total_dark %lld, "
600 "total_dead %lld\n", lst->total_used, lst->total_dark,
601 lst->total_dead);
602 spin_unlock(&dbg_lock);
605 void dbg_dump_budg(struct ubifs_info *c)
607 int i;
608 struct rb_node *rb;
609 struct ubifs_bud *bud;
610 struct ubifs_gced_idx_leb *idx_gc;
611 long long available, outstanding, free;
613 spin_lock(&c->space_lock);
614 spin_lock(&dbg_lock);
615 printk(KERN_DEBUG "(pid %d) Budgeting info: data budget sum %lld, "
616 "total budget sum %lld\n", current->pid,
617 c->bi.data_growth + c->bi.dd_growth,
618 c->bi.data_growth + c->bi.dd_growth + c->bi.idx_growth);
619 printk(KERN_DEBUG "\tbudg_data_growth %lld, budg_dd_growth %lld, "
620 "budg_idx_growth %lld\n", c->bi.data_growth, c->bi.dd_growth,
621 c->bi.idx_growth);
622 printk(KERN_DEBUG "\tmin_idx_lebs %d, old_idx_sz %llu, "
623 "uncommitted_idx %lld\n", c->bi.min_idx_lebs, c->bi.old_idx_sz,
624 c->bi.uncommitted_idx);
625 printk(KERN_DEBUG "\tpage_budget %d, inode_budget %d, dent_budget %d\n",
626 c->bi.page_budget, c->bi.inode_budget, c->bi.dent_budget);
627 printk(KERN_DEBUG "\tnospace %u, nospace_rp %u\n",
628 c->bi.nospace, c->bi.nospace_rp);
629 printk(KERN_DEBUG "\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
630 c->dark_wm, c->dead_wm, c->max_idx_node_sz);
631 printk(KERN_DEBUG "\tfreeable_cnt %d, calc_idx_sz %lld, idx_gc_cnt %d\n",
632 c->freeable_cnt, c->calc_idx_sz, c->idx_gc_cnt);
633 printk(KERN_DEBUG "\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, "
634 "clean_zn_cnt %ld\n", atomic_long_read(&c->dirty_pg_cnt),
635 atomic_long_read(&c->dirty_zn_cnt),
636 atomic_long_read(&c->clean_zn_cnt));
637 printk(KERN_DEBUG "\tgc_lnum %d, ihead_lnum %d\n",
638 c->gc_lnum, c->ihead_lnum);
639 /* If we are in R/O mode, journal heads do not exist */
640 if (c->jheads)
641 for (i = 0; i < c->jhead_cnt; i++)
642 printk(KERN_DEBUG "\tjhead %s\t LEB %d\n",
643 dbg_jhead(c->jheads[i].wbuf.jhead),
644 c->jheads[i].wbuf.lnum);
645 for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
646 bud = rb_entry(rb, struct ubifs_bud, rb);
647 printk(KERN_DEBUG "\tbud LEB %d\n", bud->lnum);
649 list_for_each_entry(bud, &c->old_buds, list)
650 printk(KERN_DEBUG "\told bud LEB %d\n", bud->lnum);
651 list_for_each_entry(idx_gc, &c->idx_gc, list)
652 printk(KERN_DEBUG "\tGC'ed idx LEB %d unmap %d\n",
653 idx_gc->lnum, idx_gc->unmap);
654 printk(KERN_DEBUG "\tcommit state %d\n", c->cmt_state);
656 /* Print budgeting predictions */
657 available = ubifs_calc_available(c, c->bi.min_idx_lebs);
658 outstanding = c->bi.data_growth + c->bi.dd_growth;
659 free = ubifs_get_free_space_nolock(c);
660 printk(KERN_DEBUG "Budgeting predictions:\n");
661 printk(KERN_DEBUG "\tavailable: %lld, outstanding %lld, free %lld\n",
662 available, outstanding, free);
663 spin_unlock(&dbg_lock);
664 spin_unlock(&c->space_lock);
667 void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
669 int i, spc, dark = 0, dead = 0;
670 struct rb_node *rb;
671 struct ubifs_bud *bud;
673 spc = lp->free + lp->dirty;
674 if (spc < c->dead_wm)
675 dead = spc;
676 else
677 dark = ubifs_calc_dark(c, spc);
679 if (lp->flags & LPROPS_INDEX)
680 printk(KERN_DEBUG "LEB %-7d free %-8d dirty %-8d used %-8d "
681 "free + dirty %-8d flags %#x (", lp->lnum, lp->free,
682 lp->dirty, c->leb_size - spc, spc, lp->flags);
683 else
684 printk(KERN_DEBUG "LEB %-7d free %-8d dirty %-8d used %-8d "
685 "free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d "
686 "flags %#-4x (", lp->lnum, lp->free, lp->dirty,
687 c->leb_size - spc, spc, dark, dead,
688 (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags);
690 if (lp->flags & LPROPS_TAKEN) {
691 if (lp->flags & LPROPS_INDEX)
692 printk(KERN_CONT "index, taken");
693 else
694 printk(KERN_CONT "taken");
695 } else {
696 const char *s;
698 if (lp->flags & LPROPS_INDEX) {
699 switch (lp->flags & LPROPS_CAT_MASK) {
700 case LPROPS_DIRTY_IDX:
701 s = "dirty index";
702 break;
703 case LPROPS_FRDI_IDX:
704 s = "freeable index";
705 break;
706 default:
707 s = "index";
709 } else {
710 switch (lp->flags & LPROPS_CAT_MASK) {
711 case LPROPS_UNCAT:
712 s = "not categorized";
713 break;
714 case LPROPS_DIRTY:
715 s = "dirty";
716 break;
717 case LPROPS_FREE:
718 s = "free";
719 break;
720 case LPROPS_EMPTY:
721 s = "empty";
722 break;
723 case LPROPS_FREEABLE:
724 s = "freeable";
725 break;
726 default:
727 s = NULL;
728 break;
731 printk(KERN_CONT "%s", s);
734 for (rb = rb_first((struct rb_root *)&c->buds); rb; rb = rb_next(rb)) {
735 bud = rb_entry(rb, struct ubifs_bud, rb);
736 if (bud->lnum == lp->lnum) {
737 int head = 0;
738 for (i = 0; i < c->jhead_cnt; i++) {
739 if (lp->lnum == c->jheads[i].wbuf.lnum) {
740 printk(KERN_CONT ", jhead %s",
741 dbg_jhead(i));
742 head = 1;
745 if (!head)
746 printk(KERN_CONT ", bud of jhead %s",
747 dbg_jhead(bud->jhead));
750 if (lp->lnum == c->gc_lnum)
751 printk(KERN_CONT ", GC LEB");
752 printk(KERN_CONT ")\n");
755 void dbg_dump_lprops(struct ubifs_info *c)
757 int lnum, err;
758 struct ubifs_lprops lp;
759 struct ubifs_lp_stats lst;
761 printk(KERN_DEBUG "(pid %d) start dumping LEB properties\n",
762 current->pid);
763 ubifs_get_lp_stats(c, &lst);
764 dbg_dump_lstats(&lst);
766 for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
767 err = ubifs_read_one_lp(c, lnum, &lp);
768 if (err)
769 ubifs_err("cannot read lprops for LEB %d", lnum);
771 dbg_dump_lprop(c, &lp);
773 printk(KERN_DEBUG "(pid %d) finish dumping LEB properties\n",
774 current->pid);
777 void dbg_dump_lpt_info(struct ubifs_info *c)
779 int i;
781 spin_lock(&dbg_lock);
782 printk(KERN_DEBUG "(pid %d) dumping LPT information\n", current->pid);
783 printk(KERN_DEBUG "\tlpt_sz: %lld\n", c->lpt_sz);
784 printk(KERN_DEBUG "\tpnode_sz: %d\n", c->pnode_sz);
785 printk(KERN_DEBUG "\tnnode_sz: %d\n", c->nnode_sz);
786 printk(KERN_DEBUG "\tltab_sz: %d\n", c->ltab_sz);
787 printk(KERN_DEBUG "\tlsave_sz: %d\n", c->lsave_sz);
788 printk(KERN_DEBUG "\tbig_lpt: %d\n", c->big_lpt);
789 printk(KERN_DEBUG "\tlpt_hght: %d\n", c->lpt_hght);
790 printk(KERN_DEBUG "\tpnode_cnt: %d\n", c->pnode_cnt);
791 printk(KERN_DEBUG "\tnnode_cnt: %d\n", c->nnode_cnt);
792 printk(KERN_DEBUG "\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt);
793 printk(KERN_DEBUG "\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt);
794 printk(KERN_DEBUG "\tlsave_cnt: %d\n", c->lsave_cnt);
795 printk(KERN_DEBUG "\tspace_bits: %d\n", c->space_bits);
796 printk(KERN_DEBUG "\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
797 printk(KERN_DEBUG "\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
798 printk(KERN_DEBUG "\tlpt_spc_bits: %d\n", c->lpt_spc_bits);
799 printk(KERN_DEBUG "\tpcnt_bits: %d\n", c->pcnt_bits);
800 printk(KERN_DEBUG "\tlnum_bits: %d\n", c->lnum_bits);
801 printk(KERN_DEBUG "\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
802 printk(KERN_DEBUG "\tLPT head is at %d:%d\n",
803 c->nhead_lnum, c->nhead_offs);
804 printk(KERN_DEBUG "\tLPT ltab is at %d:%d\n",
805 c->ltab_lnum, c->ltab_offs);
806 if (c->big_lpt)
807 printk(KERN_DEBUG "\tLPT lsave is at %d:%d\n",
808 c->lsave_lnum, c->lsave_offs);
809 for (i = 0; i < c->lpt_lebs; i++)
810 printk(KERN_DEBUG "\tLPT LEB %d free %d dirty %d tgc %d "
811 "cmt %d\n", i + c->lpt_first, c->ltab[i].free,
812 c->ltab[i].dirty, c->ltab[i].tgc, c->ltab[i].cmt);
813 spin_unlock(&dbg_lock);
816 void dbg_dump_leb(const struct ubifs_info *c, int lnum)
818 struct ubifs_scan_leb *sleb;
819 struct ubifs_scan_node *snod;
820 void *buf;
822 if (dbg_failure_mode)
823 return;
825 printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n",
826 current->pid, lnum);
828 buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
829 if (!buf) {
830 ubifs_err("cannot allocate memory for dumping LEB %d", lnum);
831 return;
834 sleb = ubifs_scan(c, lnum, 0, buf, 0);
835 if (IS_ERR(sleb)) {
836 ubifs_err("scan error %d", (int)PTR_ERR(sleb));
837 goto out;
840 printk(KERN_DEBUG "LEB %d has %d nodes ending at %d\n", lnum,
841 sleb->nodes_cnt, sleb->endpt);
843 list_for_each_entry(snod, &sleb->nodes, list) {
844 cond_resched();
845 printk(KERN_DEBUG "Dumping node at LEB %d:%d len %d\n", lnum,
846 snod->offs, snod->len);
847 dbg_dump_node(c, snod->node);
850 printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n",
851 current->pid, lnum);
852 ubifs_scan_destroy(sleb);
854 out:
855 vfree(buf);
856 return;
859 void dbg_dump_znode(const struct ubifs_info *c,
860 const struct ubifs_znode *znode)
862 int n;
863 const struct ubifs_zbranch *zbr;
865 spin_lock(&dbg_lock);
866 if (znode->parent)
867 zbr = &znode->parent->zbranch[znode->iip];
868 else
869 zbr = &c->zroot;
871 printk(KERN_DEBUG "znode %p, LEB %d:%d len %d parent %p iip %d level %d"
872 " child_cnt %d flags %lx\n", znode, zbr->lnum, zbr->offs,
873 zbr->len, znode->parent, znode->iip, znode->level,
874 znode->child_cnt, znode->flags);
876 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
877 spin_unlock(&dbg_lock);
878 return;
881 printk(KERN_DEBUG "zbranches:\n");
882 for (n = 0; n < znode->child_cnt; n++) {
883 zbr = &znode->zbranch[n];
884 if (znode->level > 0)
885 printk(KERN_DEBUG "\t%d: znode %p LEB %d:%d len %d key "
886 "%s\n", n, zbr->znode, zbr->lnum,
887 zbr->offs, zbr->len,
888 DBGKEY(&zbr->key));
889 else
890 printk(KERN_DEBUG "\t%d: LNC %p LEB %d:%d len %d key "
891 "%s\n", n, zbr->znode, zbr->lnum,
892 zbr->offs, zbr->len,
893 DBGKEY(&zbr->key));
895 spin_unlock(&dbg_lock);
898 void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
900 int i;
902 printk(KERN_DEBUG "(pid %d) start dumping heap cat %d (%d elements)\n",
903 current->pid, cat, heap->cnt);
904 for (i = 0; i < heap->cnt; i++) {
905 struct ubifs_lprops *lprops = heap->arr[i];
907 printk(KERN_DEBUG "\t%d. LEB %d hpos %d free %d dirty %d "
908 "flags %d\n", i, lprops->lnum, lprops->hpos,
909 lprops->free, lprops->dirty, lprops->flags);
911 printk(KERN_DEBUG "(pid %d) finish dumping heap\n", current->pid);
914 void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
915 struct ubifs_nnode *parent, int iip)
917 int i;
919 printk(KERN_DEBUG "(pid %d) dumping pnode:\n", current->pid);
920 printk(KERN_DEBUG "\taddress %zx parent %zx cnext %zx\n",
921 (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
922 printk(KERN_DEBUG "\tflags %lu iip %d level %d num %d\n",
923 pnode->flags, iip, pnode->level, pnode->num);
924 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
925 struct ubifs_lprops *lp = &pnode->lprops[i];
927 printk(KERN_DEBUG "\t%d: free %d dirty %d flags %d lnum %d\n",
928 i, lp->free, lp->dirty, lp->flags, lp->lnum);
932 void dbg_dump_tnc(struct ubifs_info *c)
934 struct ubifs_znode *znode;
935 int level;
937 printk(KERN_DEBUG "\n");
938 printk(KERN_DEBUG "(pid %d) start dumping TNC tree\n", current->pid);
939 znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
940 level = znode->level;
941 printk(KERN_DEBUG "== Level %d ==\n", level);
942 while (znode) {
943 if (level != znode->level) {
944 level = znode->level;
945 printk(KERN_DEBUG "== Level %d ==\n", level);
947 dbg_dump_znode(c, znode);
948 znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
950 printk(KERN_DEBUG "(pid %d) finish dumping TNC tree\n", current->pid);
953 static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
954 void *priv)
956 dbg_dump_znode(c, znode);
957 return 0;
961 * dbg_dump_index - dump the on-flash index.
962 * @c: UBIFS file-system description object
964 * This function dumps whole UBIFS indexing B-tree, unlike 'dbg_dump_tnc()'
965 * which dumps only in-memory znodes and does not read znodes which from flash.
967 void dbg_dump_index(struct ubifs_info *c)
969 dbg_walk_index(c, NULL, dump_znode, NULL);
973 * dbg_save_space_info - save information about flash space.
974 * @c: UBIFS file-system description object
976 * This function saves information about UBIFS free space, dirty space, etc, in
977 * order to check it later.
979 void dbg_save_space_info(struct ubifs_info *c)
981 struct ubifs_debug_info *d = c->dbg;
982 int freeable_cnt;
984 spin_lock(&c->space_lock);
985 memcpy(&d->saved_lst, &c->lst, sizeof(struct ubifs_lp_stats));
988 * We use a dirty hack here and zero out @c->freeable_cnt, because it
989 * affects the free space calculations, and UBIFS might not know about
990 * all freeable eraseblocks. Indeed, we know about freeable eraseblocks
991 * only when we read their lprops, and we do this only lazily, upon the
992 * need. So at any given point of time @c->freeable_cnt might be not
993 * exactly accurate.
995 * Just one example about the issue we hit when we did not zero
996 * @c->freeable_cnt.
997 * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the
998 * amount of free space in @d->saved_free
999 * 2. We re-mount R/W, which makes UBIFS to read the "lsave"
1000 * information from flash, where we cache LEBs from various
1001 * categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()'
1002 * -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()'
1003 * -> 'ubifs_get_pnode()' -> 'update_cats()'
1004 * -> 'ubifs_add_to_cat()').
1005 * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt
1006 * becomes %1.
1007 * 4. We calculate the amount of free space when the re-mount is
1008 * finished in 'dbg_check_space_info()' and it does not match
1009 * @d->saved_free.
1011 freeable_cnt = c->freeable_cnt;
1012 c->freeable_cnt = 0;
1013 d->saved_free = ubifs_get_free_space_nolock(c);
1014 c->freeable_cnt = freeable_cnt;
1015 spin_unlock(&c->space_lock);
1019 * dbg_check_space_info - check flash space information.
1020 * @c: UBIFS file-system description object
1022 * This function compares current flash space information with the information
1023 * which was saved when the 'dbg_save_space_info()' function was called.
1024 * Returns zero if the information has not changed, and %-EINVAL it it has
1025 * changed.
1027 int dbg_check_space_info(struct ubifs_info *c)
1029 struct ubifs_debug_info *d = c->dbg;
1030 struct ubifs_lp_stats lst;
1031 long long free;
1032 int freeable_cnt;
1034 spin_lock(&c->space_lock);
1035 freeable_cnt = c->freeable_cnt;
1036 c->freeable_cnt = 0;
1037 free = ubifs_get_free_space_nolock(c);
1038 c->freeable_cnt = freeable_cnt;
1039 spin_unlock(&c->space_lock);
1041 if (free != d->saved_free) {
1042 ubifs_err("free space changed from %lld to %lld",
1043 d->saved_free, free);
1044 goto out;
1047 return 0;
1049 out:
1050 ubifs_msg("saved lprops statistics dump");
1051 dbg_dump_lstats(&d->saved_lst);
1052 ubifs_get_lp_stats(c, &lst);
1054 ubifs_msg("current lprops statistics dump");
1055 dbg_dump_lstats(&lst);
1056 dbg_dump_budg(c);
1057 dump_stack();
1058 return -EINVAL;
1062 * dbg_check_synced_i_size - check synchronized inode size.
1063 * @inode: inode to check
1065 * If inode is clean, synchronized inode size has to be equivalent to current
1066 * inode size. This function has to be called only for locked inodes (@i_mutex
1067 * has to be locked). Returns %0 if synchronized inode size if correct, and
1068 * %-EINVAL if not.
1070 int dbg_check_synced_i_size(struct inode *inode)
1072 int err = 0;
1073 struct ubifs_inode *ui = ubifs_inode(inode);
1075 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
1076 return 0;
1077 if (!S_ISREG(inode->i_mode))
1078 return 0;
1080 mutex_lock(&ui->ui_mutex);
1081 spin_lock(&ui->ui_lock);
1082 if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
1083 ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode "
1084 "is clean", ui->ui_size, ui->synced_i_size);
1085 ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
1086 inode->i_mode, i_size_read(inode));
1087 dbg_dump_stack();
1088 err = -EINVAL;
1090 spin_unlock(&ui->ui_lock);
1091 mutex_unlock(&ui->ui_mutex);
1092 return err;
1096 * dbg_check_dir - check directory inode size and link count.
1097 * @c: UBIFS file-system description object
1098 * @dir: the directory to calculate size for
1099 * @size: the result is returned here
1101 * This function makes sure that directory size and link count are correct.
1102 * Returns zero in case of success and a negative error code in case of
1103 * failure.
1105 * Note, it is good idea to make sure the @dir->i_mutex is locked before
1106 * calling this function.
1108 int dbg_check_dir_size(struct ubifs_info *c, const struct inode *dir)
1110 unsigned int nlink = 2;
1111 union ubifs_key key;
1112 struct ubifs_dent_node *dent, *pdent = NULL;
1113 struct qstr nm = { .name = NULL };
1114 loff_t size = UBIFS_INO_NODE_SZ;
1116 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
1117 return 0;
1119 if (!S_ISDIR(dir->i_mode))
1120 return 0;
1122 lowest_dent_key(c, &key, dir->i_ino);
1123 while (1) {
1124 int err;
1126 dent = ubifs_tnc_next_ent(c, &key, &nm);
1127 if (IS_ERR(dent)) {
1128 err = PTR_ERR(dent);
1129 if (err == -ENOENT)
1130 break;
1131 return err;
1134 nm.name = dent->name;
1135 nm.len = le16_to_cpu(dent->nlen);
1136 size += CALC_DENT_SIZE(nm.len);
1137 if (dent->type == UBIFS_ITYPE_DIR)
1138 nlink += 1;
1139 kfree(pdent);
1140 pdent = dent;
1141 key_read(c, &dent->key, &key);
1143 kfree(pdent);
1145 if (i_size_read(dir) != size) {
1146 ubifs_err("directory inode %lu has size %llu, "
1147 "but calculated size is %llu", dir->i_ino,
1148 (unsigned long long)i_size_read(dir),
1149 (unsigned long long)size);
1150 dump_stack();
1151 return -EINVAL;
1153 if (dir->i_nlink != nlink) {
1154 ubifs_err("directory inode %lu has nlink %u, but calculated "
1155 "nlink is %u", dir->i_ino, dir->i_nlink, nlink);
1156 dump_stack();
1157 return -EINVAL;
1160 return 0;
1164 * dbg_check_key_order - make sure that colliding keys are properly ordered.
1165 * @c: UBIFS file-system description object
1166 * @zbr1: first zbranch
1167 * @zbr2: following zbranch
1169 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
1170 * names of the direntries/xentries which are referred by the keys. This
1171 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
1172 * sure the name of direntry/xentry referred by @zbr1 is less than
1173 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
1174 * and a negative error code in case of failure.
1176 static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
1177 struct ubifs_zbranch *zbr2)
1179 int err, nlen1, nlen2, cmp;
1180 struct ubifs_dent_node *dent1, *dent2;
1181 union ubifs_key key;
1183 ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
1184 dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1185 if (!dent1)
1186 return -ENOMEM;
1187 dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1188 if (!dent2) {
1189 err = -ENOMEM;
1190 goto out_free;
1193 err = ubifs_tnc_read_node(c, zbr1, dent1);
1194 if (err)
1195 goto out_free;
1196 err = ubifs_validate_entry(c, dent1);
1197 if (err)
1198 goto out_free;
1200 err = ubifs_tnc_read_node(c, zbr2, dent2);
1201 if (err)
1202 goto out_free;
1203 err = ubifs_validate_entry(c, dent2);
1204 if (err)
1205 goto out_free;
1207 /* Make sure node keys are the same as in zbranch */
1208 err = 1;
1209 key_read(c, &dent1->key, &key);
1210 if (keys_cmp(c, &zbr1->key, &key)) {
1211 dbg_err("1st entry at %d:%d has key %s", zbr1->lnum,
1212 zbr1->offs, DBGKEY(&key));
1213 dbg_err("but it should have key %s according to tnc",
1214 DBGKEY(&zbr1->key));
1215 dbg_dump_node(c, dent1);
1216 goto out_free;
1219 key_read(c, &dent2->key, &key);
1220 if (keys_cmp(c, &zbr2->key, &key)) {
1221 dbg_err("2nd entry at %d:%d has key %s", zbr1->lnum,
1222 zbr1->offs, DBGKEY(&key));
1223 dbg_err("but it should have key %s according to tnc",
1224 DBGKEY(&zbr2->key));
1225 dbg_dump_node(c, dent2);
1226 goto out_free;
1229 nlen1 = le16_to_cpu(dent1->nlen);
1230 nlen2 = le16_to_cpu(dent2->nlen);
1232 cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
1233 if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
1234 err = 0;
1235 goto out_free;
1237 if (cmp == 0 && nlen1 == nlen2)
1238 dbg_err("2 xent/dent nodes with the same name");
1239 else
1240 dbg_err("bad order of colliding key %s",
1241 DBGKEY(&key));
1243 ubifs_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs);
1244 dbg_dump_node(c, dent1);
1245 ubifs_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs);
1246 dbg_dump_node(c, dent2);
1248 out_free:
1249 kfree(dent2);
1250 kfree(dent1);
1251 return err;
1255 * dbg_check_znode - check if znode is all right.
1256 * @c: UBIFS file-system description object
1257 * @zbr: zbranch which points to this znode
1259 * This function makes sure that znode referred to by @zbr is all right.
1260 * Returns zero if it is, and %-EINVAL if it is not.
1262 static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
1264 struct ubifs_znode *znode = zbr->znode;
1265 struct ubifs_znode *zp = znode->parent;
1266 int n, err, cmp;
1268 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
1269 err = 1;
1270 goto out;
1272 if (znode->level < 0) {
1273 err = 2;
1274 goto out;
1276 if (znode->iip < 0 || znode->iip >= c->fanout) {
1277 err = 3;
1278 goto out;
1281 if (zbr->len == 0)
1282 /* Only dirty zbranch may have no on-flash nodes */
1283 if (!ubifs_zn_dirty(znode)) {
1284 err = 4;
1285 goto out;
1288 if (ubifs_zn_dirty(znode)) {
1290 * If znode is dirty, its parent has to be dirty as well. The
1291 * order of the operation is important, so we have to have
1292 * memory barriers.
1294 smp_mb();
1295 if (zp && !ubifs_zn_dirty(zp)) {
1297 * The dirty flag is atomic and is cleared outside the
1298 * TNC mutex, so znode's dirty flag may now have
1299 * been cleared. The child is always cleared before the
1300 * parent, so we just need to check again.
1302 smp_mb();
1303 if (ubifs_zn_dirty(znode)) {
1304 err = 5;
1305 goto out;
1310 if (zp) {
1311 const union ubifs_key *min, *max;
1313 if (znode->level != zp->level - 1) {
1314 err = 6;
1315 goto out;
1318 /* Make sure the 'parent' pointer in our znode is correct */
1319 err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
1320 if (!err) {
1321 /* This zbranch does not exist in the parent */
1322 err = 7;
1323 goto out;
1326 if (znode->iip >= zp->child_cnt) {
1327 err = 8;
1328 goto out;
1331 if (znode->iip != n) {
1332 /* This may happen only in case of collisions */
1333 if (keys_cmp(c, &zp->zbranch[n].key,
1334 &zp->zbranch[znode->iip].key)) {
1335 err = 9;
1336 goto out;
1338 n = znode->iip;
1342 * Make sure that the first key in our znode is greater than or
1343 * equal to the key in the pointing zbranch.
1345 min = &zbr->key;
1346 cmp = keys_cmp(c, min, &znode->zbranch[0].key);
1347 if (cmp == 1) {
1348 err = 10;
1349 goto out;
1352 if (n + 1 < zp->child_cnt) {
1353 max = &zp->zbranch[n + 1].key;
1356 * Make sure the last key in our znode is less or
1357 * equivalent than the key in the zbranch which goes
1358 * after our pointing zbranch.
1360 cmp = keys_cmp(c, max,
1361 &znode->zbranch[znode->child_cnt - 1].key);
1362 if (cmp == -1) {
1363 err = 11;
1364 goto out;
1367 } else {
1368 /* This may only be root znode */
1369 if (zbr != &c->zroot) {
1370 err = 12;
1371 goto out;
1376 * Make sure that next key is greater or equivalent then the previous
1377 * one.
1379 for (n = 1; n < znode->child_cnt; n++) {
1380 cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
1381 &znode->zbranch[n].key);
1382 if (cmp > 0) {
1383 err = 13;
1384 goto out;
1386 if (cmp == 0) {
1387 /* This can only be keys with colliding hash */
1388 if (!is_hash_key(c, &znode->zbranch[n].key)) {
1389 err = 14;
1390 goto out;
1393 if (znode->level != 0 || c->replaying)
1394 continue;
1397 * Colliding keys should follow binary order of
1398 * corresponding xentry/dentry names.
1400 err = dbg_check_key_order(c, &znode->zbranch[n - 1],
1401 &znode->zbranch[n]);
1402 if (err < 0)
1403 return err;
1404 if (err) {
1405 err = 15;
1406 goto out;
1411 for (n = 0; n < znode->child_cnt; n++) {
1412 if (!znode->zbranch[n].znode &&
1413 (znode->zbranch[n].lnum == 0 ||
1414 znode->zbranch[n].len == 0)) {
1415 err = 16;
1416 goto out;
1419 if (znode->zbranch[n].lnum != 0 &&
1420 znode->zbranch[n].len == 0) {
1421 err = 17;
1422 goto out;
1425 if (znode->zbranch[n].lnum == 0 &&
1426 znode->zbranch[n].len != 0) {
1427 err = 18;
1428 goto out;
1431 if (znode->zbranch[n].lnum == 0 &&
1432 znode->zbranch[n].offs != 0) {
1433 err = 19;
1434 goto out;
1437 if (znode->level != 0 && znode->zbranch[n].znode)
1438 if (znode->zbranch[n].znode->parent != znode) {
1439 err = 20;
1440 goto out;
1444 return 0;
1446 out:
1447 ubifs_err("failed, error %d", err);
1448 ubifs_msg("dump of the znode");
1449 dbg_dump_znode(c, znode);
1450 if (zp) {
1451 ubifs_msg("dump of the parent znode");
1452 dbg_dump_znode(c, zp);
1454 dump_stack();
1455 return -EINVAL;
1459 * dbg_check_tnc - check TNC tree.
1460 * @c: UBIFS file-system description object
1461 * @extra: do extra checks that are possible at start commit
1463 * This function traverses whole TNC tree and checks every znode. Returns zero
1464 * if everything is all right and %-EINVAL if something is wrong with TNC.
1466 int dbg_check_tnc(struct ubifs_info *c, int extra)
1468 struct ubifs_znode *znode;
1469 long clean_cnt = 0, dirty_cnt = 0;
1470 int err, last;
1472 if (!(ubifs_chk_flags & UBIFS_CHK_TNC))
1473 return 0;
1475 ubifs_assert(mutex_is_locked(&c->tnc_mutex));
1476 if (!c->zroot.znode)
1477 return 0;
1479 znode = ubifs_tnc_postorder_first(c->zroot.znode);
1480 while (1) {
1481 struct ubifs_znode *prev;
1482 struct ubifs_zbranch *zbr;
1484 if (!znode->parent)
1485 zbr = &c->zroot;
1486 else
1487 zbr = &znode->parent->zbranch[znode->iip];
1489 err = dbg_check_znode(c, zbr);
1490 if (err)
1491 return err;
1493 if (extra) {
1494 if (ubifs_zn_dirty(znode))
1495 dirty_cnt += 1;
1496 else
1497 clean_cnt += 1;
1500 prev = znode;
1501 znode = ubifs_tnc_postorder_next(znode);
1502 if (!znode)
1503 break;
1506 * If the last key of this znode is equivalent to the first key
1507 * of the next znode (collision), then check order of the keys.
1509 last = prev->child_cnt - 1;
1510 if (prev->level == 0 && znode->level == 0 && !c->replaying &&
1511 !keys_cmp(c, &prev->zbranch[last].key,
1512 &znode->zbranch[0].key)) {
1513 err = dbg_check_key_order(c, &prev->zbranch[last],
1514 &znode->zbranch[0]);
1515 if (err < 0)
1516 return err;
1517 if (err) {
1518 ubifs_msg("first znode");
1519 dbg_dump_znode(c, prev);
1520 ubifs_msg("second znode");
1521 dbg_dump_znode(c, znode);
1522 return -EINVAL;
1527 if (extra) {
1528 if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
1529 ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld",
1530 atomic_long_read(&c->clean_zn_cnt),
1531 clean_cnt);
1532 return -EINVAL;
1534 if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
1535 ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld",
1536 atomic_long_read(&c->dirty_zn_cnt),
1537 dirty_cnt);
1538 return -EINVAL;
1542 return 0;
1546 * dbg_walk_index - walk the on-flash index.
1547 * @c: UBIFS file-system description object
1548 * @leaf_cb: called for each leaf node
1549 * @znode_cb: called for each indexing node
1550 * @priv: private data which is passed to callbacks
1552 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1553 * node and @znode_cb for each indexing node. Returns zero in case of success
1554 * and a negative error code in case of failure.
1556 * It would be better if this function removed every znode it pulled to into
1557 * the TNC, so that the behavior more closely matched the non-debugging
1558 * behavior.
1560 int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1561 dbg_znode_callback znode_cb, void *priv)
1563 int err;
1564 struct ubifs_zbranch *zbr;
1565 struct ubifs_znode *znode, *child;
1567 mutex_lock(&c->tnc_mutex);
1568 /* If the root indexing node is not in TNC - pull it */
1569 if (!c->zroot.znode) {
1570 c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1571 if (IS_ERR(c->zroot.znode)) {
1572 err = PTR_ERR(c->zroot.znode);
1573 c->zroot.znode = NULL;
1574 goto out_unlock;
1579 * We are going to traverse the indexing tree in the postorder manner.
1580 * Go down and find the leftmost indexing node where we are going to
1581 * start from.
1583 znode = c->zroot.znode;
1584 while (znode->level > 0) {
1585 zbr = &znode->zbranch[0];
1586 child = zbr->znode;
1587 if (!child) {
1588 child = ubifs_load_znode(c, zbr, znode, 0);
1589 if (IS_ERR(child)) {
1590 err = PTR_ERR(child);
1591 goto out_unlock;
1593 zbr->znode = child;
1596 znode = child;
1599 /* Iterate over all indexing nodes */
1600 while (1) {
1601 int idx;
1603 cond_resched();
1605 if (znode_cb) {
1606 err = znode_cb(c, znode, priv);
1607 if (err) {
1608 ubifs_err("znode checking function returned "
1609 "error %d", err);
1610 dbg_dump_znode(c, znode);
1611 goto out_dump;
1614 if (leaf_cb && znode->level == 0) {
1615 for (idx = 0; idx < znode->child_cnt; idx++) {
1616 zbr = &znode->zbranch[idx];
1617 err = leaf_cb(c, zbr, priv);
1618 if (err) {
1619 ubifs_err("leaf checking function "
1620 "returned error %d, for leaf "
1621 "at LEB %d:%d",
1622 err, zbr->lnum, zbr->offs);
1623 goto out_dump;
1628 if (!znode->parent)
1629 break;
1631 idx = znode->iip + 1;
1632 znode = znode->parent;
1633 if (idx < znode->child_cnt) {
1634 /* Switch to the next index in the parent */
1635 zbr = &znode->zbranch[idx];
1636 child = zbr->znode;
1637 if (!child) {
1638 child = ubifs_load_znode(c, zbr, znode, idx);
1639 if (IS_ERR(child)) {
1640 err = PTR_ERR(child);
1641 goto out_unlock;
1643 zbr->znode = child;
1645 znode = child;
1646 } else
1648 * This is the last child, switch to the parent and
1649 * continue.
1651 continue;
1653 /* Go to the lowest leftmost znode in the new sub-tree */
1654 while (znode->level > 0) {
1655 zbr = &znode->zbranch[0];
1656 child = zbr->znode;
1657 if (!child) {
1658 child = ubifs_load_znode(c, zbr, znode, 0);
1659 if (IS_ERR(child)) {
1660 err = PTR_ERR(child);
1661 goto out_unlock;
1663 zbr->znode = child;
1665 znode = child;
1669 mutex_unlock(&c->tnc_mutex);
1670 return 0;
1672 out_dump:
1673 if (znode->parent)
1674 zbr = &znode->parent->zbranch[znode->iip];
1675 else
1676 zbr = &c->zroot;
1677 ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1678 dbg_dump_znode(c, znode);
1679 out_unlock:
1680 mutex_unlock(&c->tnc_mutex);
1681 return err;
1685 * add_size - add znode size to partially calculated index size.
1686 * @c: UBIFS file-system description object
1687 * @znode: znode to add size for
1688 * @priv: partially calculated index size
1690 * This is a helper function for 'dbg_check_idx_size()' which is called for
1691 * every indexing node and adds its size to the 'long long' variable pointed to
1692 * by @priv.
1694 static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1696 long long *idx_size = priv;
1697 int add;
1699 add = ubifs_idx_node_sz(c, znode->child_cnt);
1700 add = ALIGN(add, 8);
1701 *idx_size += add;
1702 return 0;
1706 * dbg_check_idx_size - check index size.
1707 * @c: UBIFS file-system description object
1708 * @idx_size: size to check
1710 * This function walks the UBIFS index, calculates its size and checks that the
1711 * size is equivalent to @idx_size. Returns zero in case of success and a
1712 * negative error code in case of failure.
1714 int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
1716 int err;
1717 long long calc = 0;
1719 if (!(ubifs_chk_flags & UBIFS_CHK_IDX_SZ))
1720 return 0;
1722 err = dbg_walk_index(c, NULL, add_size, &calc);
1723 if (err) {
1724 ubifs_err("error %d while walking the index", err);
1725 return err;
1728 if (calc != idx_size) {
1729 ubifs_err("index size check failed: calculated size is %lld, "
1730 "should be %lld", calc, idx_size);
1731 dump_stack();
1732 return -EINVAL;
1735 return 0;
1739 * struct fsck_inode - information about an inode used when checking the file-system.
1740 * @rb: link in the RB-tree of inodes
1741 * @inum: inode number
1742 * @mode: inode type, permissions, etc
1743 * @nlink: inode link count
1744 * @xattr_cnt: count of extended attributes
1745 * @references: how many directory/xattr entries refer this inode (calculated
1746 * while walking the index)
1747 * @calc_cnt: for directory inode count of child directories
1748 * @size: inode size (read from on-flash inode)
1749 * @xattr_sz: summary size of all extended attributes (read from on-flash
1750 * inode)
1751 * @calc_sz: for directories calculated directory size
1752 * @calc_xcnt: count of extended attributes
1753 * @calc_xsz: calculated summary size of all extended attributes
1754 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1755 * inode (read from on-flash inode)
1756 * @calc_xnms: calculated sum of lengths of all extended attribute names
1758 struct fsck_inode {
1759 struct rb_node rb;
1760 ino_t inum;
1761 umode_t mode;
1762 unsigned int nlink;
1763 unsigned int xattr_cnt;
1764 int references;
1765 int calc_cnt;
1766 long long size;
1767 unsigned int xattr_sz;
1768 long long calc_sz;
1769 long long calc_xcnt;
1770 long long calc_xsz;
1771 unsigned int xattr_nms;
1772 long long calc_xnms;
1776 * struct fsck_data - private FS checking information.
1777 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1779 struct fsck_data {
1780 struct rb_root inodes;
1784 * add_inode - add inode information to RB-tree of inodes.
1785 * @c: UBIFS file-system description object
1786 * @fsckd: FS checking information
1787 * @ino: raw UBIFS inode to add
1789 * This is a helper function for 'check_leaf()' which adds information about
1790 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1791 * case of success and a negative error code in case of failure.
1793 static struct fsck_inode *add_inode(struct ubifs_info *c,
1794 struct fsck_data *fsckd,
1795 struct ubifs_ino_node *ino)
1797 struct rb_node **p, *parent = NULL;
1798 struct fsck_inode *fscki;
1799 ino_t inum = key_inum_flash(c, &ino->key);
1801 p = &fsckd->inodes.rb_node;
1802 while (*p) {
1803 parent = *p;
1804 fscki = rb_entry(parent, struct fsck_inode, rb);
1805 if (inum < fscki->inum)
1806 p = &(*p)->rb_left;
1807 else if (inum > fscki->inum)
1808 p = &(*p)->rb_right;
1809 else
1810 return fscki;
1813 if (inum > c->highest_inum) {
1814 ubifs_err("too high inode number, max. is %lu",
1815 (unsigned long)c->highest_inum);
1816 return ERR_PTR(-EINVAL);
1819 fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
1820 if (!fscki)
1821 return ERR_PTR(-ENOMEM);
1823 fscki->inum = inum;
1824 fscki->nlink = le32_to_cpu(ino->nlink);
1825 fscki->size = le64_to_cpu(ino->size);
1826 fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
1827 fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
1828 fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
1829 fscki->mode = le32_to_cpu(ino->mode);
1830 if (S_ISDIR(fscki->mode)) {
1831 fscki->calc_sz = UBIFS_INO_NODE_SZ;
1832 fscki->calc_cnt = 2;
1834 rb_link_node(&fscki->rb, parent, p);
1835 rb_insert_color(&fscki->rb, &fsckd->inodes);
1836 return fscki;
1840 * search_inode - search inode in the RB-tree of inodes.
1841 * @fsckd: FS checking information
1842 * @inum: inode number to search
1844 * This is a helper function for 'check_leaf()' which searches inode @inum in
1845 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1846 * the inode was not found.
1848 static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
1850 struct rb_node *p;
1851 struct fsck_inode *fscki;
1853 p = fsckd->inodes.rb_node;
1854 while (p) {
1855 fscki = rb_entry(p, struct fsck_inode, rb);
1856 if (inum < fscki->inum)
1857 p = p->rb_left;
1858 else if (inum > fscki->inum)
1859 p = p->rb_right;
1860 else
1861 return fscki;
1863 return NULL;
1867 * read_add_inode - read inode node and add it to RB-tree of inodes.
1868 * @c: UBIFS file-system description object
1869 * @fsckd: FS checking information
1870 * @inum: inode number to read
1872 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1873 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1874 * information pointer in case of success and a negative error code in case of
1875 * failure.
1877 static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1878 struct fsck_data *fsckd, ino_t inum)
1880 int n, err;
1881 union ubifs_key key;
1882 struct ubifs_znode *znode;
1883 struct ubifs_zbranch *zbr;
1884 struct ubifs_ino_node *ino;
1885 struct fsck_inode *fscki;
1887 fscki = search_inode(fsckd, inum);
1888 if (fscki)
1889 return fscki;
1891 ino_key_init(c, &key, inum);
1892 err = ubifs_lookup_level0(c, &key, &znode, &n);
1893 if (!err) {
1894 ubifs_err("inode %lu not found in index", (unsigned long)inum);
1895 return ERR_PTR(-ENOENT);
1896 } else if (err < 0) {
1897 ubifs_err("error %d while looking up inode %lu",
1898 err, (unsigned long)inum);
1899 return ERR_PTR(err);
1902 zbr = &znode->zbranch[n];
1903 if (zbr->len < UBIFS_INO_NODE_SZ) {
1904 ubifs_err("bad node %lu node length %d",
1905 (unsigned long)inum, zbr->len);
1906 return ERR_PTR(-EINVAL);
1909 ino = kmalloc(zbr->len, GFP_NOFS);
1910 if (!ino)
1911 return ERR_PTR(-ENOMEM);
1913 err = ubifs_tnc_read_node(c, zbr, ino);
1914 if (err) {
1915 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
1916 zbr->lnum, zbr->offs, err);
1917 kfree(ino);
1918 return ERR_PTR(err);
1921 fscki = add_inode(c, fsckd, ino);
1922 kfree(ino);
1923 if (IS_ERR(fscki)) {
1924 ubifs_err("error %ld while adding inode %lu node",
1925 PTR_ERR(fscki), (unsigned long)inum);
1926 return fscki;
1929 return fscki;
1933 * check_leaf - check leaf node.
1934 * @c: UBIFS file-system description object
1935 * @zbr: zbranch of the leaf node to check
1936 * @priv: FS checking information
1938 * This is a helper function for 'dbg_check_filesystem()' which is called for
1939 * every single leaf node while walking the indexing tree. It checks that the
1940 * leaf node referred from the indexing tree exists, has correct CRC, and does
1941 * some other basic validation. This function is also responsible for building
1942 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
1943 * calculates reference count, size, etc for each inode in order to later
1944 * compare them to the information stored inside the inodes and detect possible
1945 * inconsistencies. Returns zero in case of success and a negative error code
1946 * in case of failure.
1948 static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
1949 void *priv)
1951 ino_t inum;
1952 void *node;
1953 struct ubifs_ch *ch;
1954 int err, type = key_type(c, &zbr->key);
1955 struct fsck_inode *fscki;
1957 if (zbr->len < UBIFS_CH_SZ) {
1958 ubifs_err("bad leaf length %d (LEB %d:%d)",
1959 zbr->len, zbr->lnum, zbr->offs);
1960 return -EINVAL;
1963 node = kmalloc(zbr->len, GFP_NOFS);
1964 if (!node)
1965 return -ENOMEM;
1967 err = ubifs_tnc_read_node(c, zbr, node);
1968 if (err) {
1969 ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
1970 zbr->lnum, zbr->offs, err);
1971 goto out_free;
1974 /* If this is an inode node, add it to RB-tree of inodes */
1975 if (type == UBIFS_INO_KEY) {
1976 fscki = add_inode(c, priv, node);
1977 if (IS_ERR(fscki)) {
1978 err = PTR_ERR(fscki);
1979 ubifs_err("error %d while adding inode node", err);
1980 goto out_dump;
1982 goto out;
1985 if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
1986 type != UBIFS_DATA_KEY) {
1987 ubifs_err("unexpected node type %d at LEB %d:%d",
1988 type, zbr->lnum, zbr->offs);
1989 err = -EINVAL;
1990 goto out_free;
1993 ch = node;
1994 if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
1995 ubifs_err("too high sequence number, max. is %llu",
1996 c->max_sqnum);
1997 err = -EINVAL;
1998 goto out_dump;
2001 if (type == UBIFS_DATA_KEY) {
2002 long long blk_offs;
2003 struct ubifs_data_node *dn = node;
2006 * Search the inode node this data node belongs to and insert
2007 * it to the RB-tree of inodes.
2009 inum = key_inum_flash(c, &dn->key);
2010 fscki = read_add_inode(c, priv, inum);
2011 if (IS_ERR(fscki)) {
2012 err = PTR_ERR(fscki);
2013 ubifs_err("error %d while processing data node and "
2014 "trying to find inode node %lu",
2015 err, (unsigned long)inum);
2016 goto out_dump;
2019 /* Make sure the data node is within inode size */
2020 blk_offs = key_block_flash(c, &dn->key);
2021 blk_offs <<= UBIFS_BLOCK_SHIFT;
2022 blk_offs += le32_to_cpu(dn->size);
2023 if (blk_offs > fscki->size) {
2024 ubifs_err("data node at LEB %d:%d is not within inode "
2025 "size %lld", zbr->lnum, zbr->offs,
2026 fscki->size);
2027 err = -EINVAL;
2028 goto out_dump;
2030 } else {
2031 int nlen;
2032 struct ubifs_dent_node *dent = node;
2033 struct fsck_inode *fscki1;
2035 err = ubifs_validate_entry(c, dent);
2036 if (err)
2037 goto out_dump;
2040 * Search the inode node this entry refers to and the parent
2041 * inode node and insert them to the RB-tree of inodes.
2043 inum = le64_to_cpu(dent->inum);
2044 fscki = read_add_inode(c, priv, inum);
2045 if (IS_ERR(fscki)) {
2046 err = PTR_ERR(fscki);
2047 ubifs_err("error %d while processing entry node and "
2048 "trying to find inode node %lu",
2049 err, (unsigned long)inum);
2050 goto out_dump;
2053 /* Count how many direntries or xentries refers this inode */
2054 fscki->references += 1;
2056 inum = key_inum_flash(c, &dent->key);
2057 fscki1 = read_add_inode(c, priv, inum);
2058 if (IS_ERR(fscki1)) {
2059 err = PTR_ERR(fscki1);
2060 ubifs_err("error %d while processing entry node and "
2061 "trying to find parent inode node %lu",
2062 err, (unsigned long)inum);
2063 goto out_dump;
2066 nlen = le16_to_cpu(dent->nlen);
2067 if (type == UBIFS_XENT_KEY) {
2068 fscki1->calc_xcnt += 1;
2069 fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
2070 fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
2071 fscki1->calc_xnms += nlen;
2072 } else {
2073 fscki1->calc_sz += CALC_DENT_SIZE(nlen);
2074 if (dent->type == UBIFS_ITYPE_DIR)
2075 fscki1->calc_cnt += 1;
2079 out:
2080 kfree(node);
2081 return 0;
2083 out_dump:
2084 ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
2085 dbg_dump_node(c, node);
2086 out_free:
2087 kfree(node);
2088 return err;
2092 * free_inodes - free RB-tree of inodes.
2093 * @fsckd: FS checking information
2095 static void free_inodes(struct fsck_data *fsckd)
2097 struct rb_node *this = fsckd->inodes.rb_node;
2098 struct fsck_inode *fscki;
2100 while (this) {
2101 if (this->rb_left)
2102 this = this->rb_left;
2103 else if (this->rb_right)
2104 this = this->rb_right;
2105 else {
2106 fscki = rb_entry(this, struct fsck_inode, rb);
2107 this = rb_parent(this);
2108 if (this) {
2109 if (this->rb_left == &fscki->rb)
2110 this->rb_left = NULL;
2111 else
2112 this->rb_right = NULL;
2114 kfree(fscki);
2120 * check_inodes - checks all inodes.
2121 * @c: UBIFS file-system description object
2122 * @fsckd: FS checking information
2124 * This is a helper function for 'dbg_check_filesystem()' which walks the
2125 * RB-tree of inodes after the index scan has been finished, and checks that
2126 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
2127 * %-EINVAL if not, and a negative error code in case of failure.
2129 static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
2131 int n, err;
2132 union ubifs_key key;
2133 struct ubifs_znode *znode;
2134 struct ubifs_zbranch *zbr;
2135 struct ubifs_ino_node *ino;
2136 struct fsck_inode *fscki;
2137 struct rb_node *this = rb_first(&fsckd->inodes);
2139 while (this) {
2140 fscki = rb_entry(this, struct fsck_inode, rb);
2141 this = rb_next(this);
2143 if (S_ISDIR(fscki->mode)) {
2145 * Directories have to have exactly one reference (they
2146 * cannot have hardlinks), although root inode is an
2147 * exception.
2149 if (fscki->inum != UBIFS_ROOT_INO &&
2150 fscki->references != 1) {
2151 ubifs_err("directory inode %lu has %d "
2152 "direntries which refer it, but "
2153 "should be 1",
2154 (unsigned long)fscki->inum,
2155 fscki->references);
2156 goto out_dump;
2158 if (fscki->inum == UBIFS_ROOT_INO &&
2159 fscki->references != 0) {
2160 ubifs_err("root inode %lu has non-zero (%d) "
2161 "direntries which refer it",
2162 (unsigned long)fscki->inum,
2163 fscki->references);
2164 goto out_dump;
2166 if (fscki->calc_sz != fscki->size) {
2167 ubifs_err("directory inode %lu size is %lld, "
2168 "but calculated size is %lld",
2169 (unsigned long)fscki->inum,
2170 fscki->size, fscki->calc_sz);
2171 goto out_dump;
2173 if (fscki->calc_cnt != fscki->nlink) {
2174 ubifs_err("directory inode %lu nlink is %d, "
2175 "but calculated nlink is %d",
2176 (unsigned long)fscki->inum,
2177 fscki->nlink, fscki->calc_cnt);
2178 goto out_dump;
2180 } else {
2181 if (fscki->references != fscki->nlink) {
2182 ubifs_err("inode %lu nlink is %d, but "
2183 "calculated nlink is %d",
2184 (unsigned long)fscki->inum,
2185 fscki->nlink, fscki->references);
2186 goto out_dump;
2189 if (fscki->xattr_sz != fscki->calc_xsz) {
2190 ubifs_err("inode %lu has xattr size %u, but "
2191 "calculated size is %lld",
2192 (unsigned long)fscki->inum, fscki->xattr_sz,
2193 fscki->calc_xsz);
2194 goto out_dump;
2196 if (fscki->xattr_cnt != fscki->calc_xcnt) {
2197 ubifs_err("inode %lu has %u xattrs, but "
2198 "calculated count is %lld",
2199 (unsigned long)fscki->inum,
2200 fscki->xattr_cnt, fscki->calc_xcnt);
2201 goto out_dump;
2203 if (fscki->xattr_nms != fscki->calc_xnms) {
2204 ubifs_err("inode %lu has xattr names' size %u, but "
2205 "calculated names' size is %lld",
2206 (unsigned long)fscki->inum, fscki->xattr_nms,
2207 fscki->calc_xnms);
2208 goto out_dump;
2212 return 0;
2214 out_dump:
2215 /* Read the bad inode and dump it */
2216 ino_key_init(c, &key, fscki->inum);
2217 err = ubifs_lookup_level0(c, &key, &znode, &n);
2218 if (!err) {
2219 ubifs_err("inode %lu not found in index",
2220 (unsigned long)fscki->inum);
2221 return -ENOENT;
2222 } else if (err < 0) {
2223 ubifs_err("error %d while looking up inode %lu",
2224 err, (unsigned long)fscki->inum);
2225 return err;
2228 zbr = &znode->zbranch[n];
2229 ino = kmalloc(zbr->len, GFP_NOFS);
2230 if (!ino)
2231 return -ENOMEM;
2233 err = ubifs_tnc_read_node(c, zbr, ino);
2234 if (err) {
2235 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2236 zbr->lnum, zbr->offs, err);
2237 kfree(ino);
2238 return err;
2241 ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",
2242 (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
2243 dbg_dump_node(c, ino);
2244 kfree(ino);
2245 return -EINVAL;
2249 * dbg_check_filesystem - check the file-system.
2250 * @c: UBIFS file-system description object
2252 * This function checks the file system, namely:
2253 * o makes sure that all leaf nodes exist and their CRCs are correct;
2254 * o makes sure inode nlink, size, xattr size/count are correct (for all
2255 * inodes).
2257 * The function reads whole indexing tree and all nodes, so it is pretty
2258 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2259 * not, and a negative error code in case of failure.
2261 int dbg_check_filesystem(struct ubifs_info *c)
2263 int err;
2264 struct fsck_data fsckd;
2266 if (!(ubifs_chk_flags & UBIFS_CHK_FS))
2267 return 0;
2269 fsckd.inodes = RB_ROOT;
2270 err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
2271 if (err)
2272 goto out_free;
2274 err = check_inodes(c, &fsckd);
2275 if (err)
2276 goto out_free;
2278 free_inodes(&fsckd);
2279 return 0;
2281 out_free:
2282 ubifs_err("file-system check failed with error %d", err);
2283 dump_stack();
2284 free_inodes(&fsckd);
2285 return err;
2289 * dbg_check_data_nodes_order - check that list of data nodes is sorted.
2290 * @c: UBIFS file-system description object
2291 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2293 * This function returns zero if the list of data nodes is sorted correctly,
2294 * and %-EINVAL if not.
2296 int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head)
2298 struct list_head *cur;
2299 struct ubifs_scan_node *sa, *sb;
2301 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
2302 return 0;
2304 for (cur = head->next; cur->next != head; cur = cur->next) {
2305 ino_t inuma, inumb;
2306 uint32_t blka, blkb;
2308 cond_resched();
2309 sa = container_of(cur, struct ubifs_scan_node, list);
2310 sb = container_of(cur->next, struct ubifs_scan_node, list);
2312 if (sa->type != UBIFS_DATA_NODE) {
2313 ubifs_err("bad node type %d", sa->type);
2314 dbg_dump_node(c, sa->node);
2315 return -EINVAL;
2317 if (sb->type != UBIFS_DATA_NODE) {
2318 ubifs_err("bad node type %d", sb->type);
2319 dbg_dump_node(c, sb->node);
2320 return -EINVAL;
2323 inuma = key_inum(c, &sa->key);
2324 inumb = key_inum(c, &sb->key);
2326 if (inuma < inumb)
2327 continue;
2328 if (inuma > inumb) {
2329 ubifs_err("larger inum %lu goes before inum %lu",
2330 (unsigned long)inuma, (unsigned long)inumb);
2331 goto error_dump;
2334 blka = key_block(c, &sa->key);
2335 blkb = key_block(c, &sb->key);
2337 if (blka > blkb) {
2338 ubifs_err("larger block %u goes before %u", blka, blkb);
2339 goto error_dump;
2341 if (blka == blkb) {
2342 ubifs_err("two data nodes for the same block");
2343 goto error_dump;
2347 return 0;
2349 error_dump:
2350 dbg_dump_node(c, sa->node);
2351 dbg_dump_node(c, sb->node);
2352 return -EINVAL;
2356 * dbg_check_nondata_nodes_order - check that list of data nodes is sorted.
2357 * @c: UBIFS file-system description object
2358 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2360 * This function returns zero if the list of non-data nodes is sorted correctly,
2361 * and %-EINVAL if not.
2363 int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head)
2365 struct list_head *cur;
2366 struct ubifs_scan_node *sa, *sb;
2368 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
2369 return 0;
2371 for (cur = head->next; cur->next != head; cur = cur->next) {
2372 ino_t inuma, inumb;
2373 uint32_t hasha, hashb;
2375 cond_resched();
2376 sa = container_of(cur, struct ubifs_scan_node, list);
2377 sb = container_of(cur->next, struct ubifs_scan_node, list);
2379 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2380 sa->type != UBIFS_XENT_NODE) {
2381 ubifs_err("bad node type %d", sa->type);
2382 dbg_dump_node(c, sa->node);
2383 return -EINVAL;
2385 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2386 sa->type != UBIFS_XENT_NODE) {
2387 ubifs_err("bad node type %d", sb->type);
2388 dbg_dump_node(c, sb->node);
2389 return -EINVAL;
2392 if (sa->type != UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2393 ubifs_err("non-inode node goes before inode node");
2394 goto error_dump;
2397 if (sa->type == UBIFS_INO_NODE && sb->type != UBIFS_INO_NODE)
2398 continue;
2400 if (sa->type == UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2401 /* Inode nodes are sorted in descending size order */
2402 if (sa->len < sb->len) {
2403 ubifs_err("smaller inode node goes first");
2404 goto error_dump;
2406 continue;
2410 * This is either a dentry or xentry, which should be sorted in
2411 * ascending (parent ino, hash) order.
2413 inuma = key_inum(c, &sa->key);
2414 inumb = key_inum(c, &sb->key);
2416 if (inuma < inumb)
2417 continue;
2418 if (inuma > inumb) {
2419 ubifs_err("larger inum %lu goes before inum %lu",
2420 (unsigned long)inuma, (unsigned long)inumb);
2421 goto error_dump;
2424 hasha = key_block(c, &sa->key);
2425 hashb = key_block(c, &sb->key);
2427 if (hasha > hashb) {
2428 ubifs_err("larger hash %u goes before %u",
2429 hasha, hashb);
2430 goto error_dump;
2434 return 0;
2436 error_dump:
2437 ubifs_msg("dumping first node");
2438 dbg_dump_node(c, sa->node);
2439 ubifs_msg("dumping second node");
2440 dbg_dump_node(c, sb->node);
2441 return -EINVAL;
2442 return 0;
2445 static int invocation_cnt;
2447 int dbg_force_in_the_gaps(void)
2449 if (!dbg_force_in_the_gaps_enabled)
2450 return 0;
2451 /* Force in-the-gaps every 8th commit */
2452 return !((invocation_cnt++) & 0x7);
2455 /* Failure mode for recovery testing */
2457 #define chance(n, d) (simple_rand() <= (n) * 32768LL / (d))
2459 struct failure_mode_info {
2460 struct list_head list;
2461 struct ubifs_info *c;
2464 static LIST_HEAD(fmi_list);
2465 static DEFINE_SPINLOCK(fmi_lock);
2467 static unsigned int next;
2469 static int simple_rand(void)
2471 if (next == 0)
2472 next = current->pid;
2473 next = next * 1103515245 + 12345;
2474 return (next >> 16) & 32767;
2477 static void failure_mode_init(struct ubifs_info *c)
2479 struct failure_mode_info *fmi;
2481 fmi = kmalloc(sizeof(struct failure_mode_info), GFP_NOFS);
2482 if (!fmi) {
2483 ubifs_err("Failed to register failure mode - no memory");
2484 return;
2486 fmi->c = c;
2487 spin_lock(&fmi_lock);
2488 list_add_tail(&fmi->list, &fmi_list);
2489 spin_unlock(&fmi_lock);
2492 static void failure_mode_exit(struct ubifs_info *c)
2494 struct failure_mode_info *fmi, *tmp;
2496 spin_lock(&fmi_lock);
2497 list_for_each_entry_safe(fmi, tmp, &fmi_list, list)
2498 if (fmi->c == c) {
2499 list_del(&fmi->list);
2500 kfree(fmi);
2502 spin_unlock(&fmi_lock);
2505 static struct ubifs_info *dbg_find_info(struct ubi_volume_desc *desc)
2507 struct failure_mode_info *fmi;
2509 spin_lock(&fmi_lock);
2510 list_for_each_entry(fmi, &fmi_list, list)
2511 if (fmi->c->ubi == desc) {
2512 struct ubifs_info *c = fmi->c;
2514 spin_unlock(&fmi_lock);
2515 return c;
2517 spin_unlock(&fmi_lock);
2518 return NULL;
2521 static int in_failure_mode(struct ubi_volume_desc *desc)
2523 struct ubifs_info *c = dbg_find_info(desc);
2525 if (c && dbg_failure_mode)
2526 return c->dbg->failure_mode;
2527 return 0;
2530 static int do_fail(struct ubi_volume_desc *desc, int lnum, int write)
2532 struct ubifs_info *c = dbg_find_info(desc);
2533 struct ubifs_debug_info *d;
2535 if (!c || !dbg_failure_mode)
2536 return 0;
2537 d = c->dbg;
2538 if (d->failure_mode)
2539 return 1;
2540 if (!d->fail_cnt) {
2541 /* First call - decide delay to failure */
2542 if (chance(1, 2)) {
2543 unsigned int delay = 1 << (simple_rand() >> 11);
2545 if (chance(1, 2)) {
2546 d->fail_delay = 1;
2547 d->fail_timeout = jiffies +
2548 msecs_to_jiffies(delay);
2549 dbg_rcvry("failing after %ums", delay);
2550 } else {
2551 d->fail_delay = 2;
2552 d->fail_cnt_max = delay;
2553 dbg_rcvry("failing after %u calls", delay);
2556 d->fail_cnt += 1;
2558 /* Determine if failure delay has expired */
2559 if (d->fail_delay == 1) {
2560 if (time_before(jiffies, d->fail_timeout))
2561 return 0;
2562 } else if (d->fail_delay == 2)
2563 if (d->fail_cnt++ < d->fail_cnt_max)
2564 return 0;
2565 if (lnum == UBIFS_SB_LNUM) {
2566 if (write) {
2567 if (chance(1, 2))
2568 return 0;
2569 } else if (chance(19, 20))
2570 return 0;
2571 dbg_rcvry("failing in super block LEB %d", lnum);
2572 } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2573 if (chance(19, 20))
2574 return 0;
2575 dbg_rcvry("failing in master LEB %d", lnum);
2576 } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2577 if (write) {
2578 if (chance(99, 100))
2579 return 0;
2580 } else if (chance(399, 400))
2581 return 0;
2582 dbg_rcvry("failing in log LEB %d", lnum);
2583 } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2584 if (write) {
2585 if (chance(7, 8))
2586 return 0;
2587 } else if (chance(19, 20))
2588 return 0;
2589 dbg_rcvry("failing in LPT LEB %d", lnum);
2590 } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2591 if (write) {
2592 if (chance(1, 2))
2593 return 0;
2594 } else if (chance(9, 10))
2595 return 0;
2596 dbg_rcvry("failing in orphan LEB %d", lnum);
2597 } else if (lnum == c->ihead_lnum) {
2598 if (chance(99, 100))
2599 return 0;
2600 dbg_rcvry("failing in index head LEB %d", lnum);
2601 } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2602 if (chance(9, 10))
2603 return 0;
2604 dbg_rcvry("failing in GC head LEB %d", lnum);
2605 } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
2606 !ubifs_search_bud(c, lnum)) {
2607 if (chance(19, 20))
2608 return 0;
2609 dbg_rcvry("failing in non-bud LEB %d", lnum);
2610 } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
2611 c->cmt_state == COMMIT_RUNNING_REQUIRED) {
2612 if (chance(999, 1000))
2613 return 0;
2614 dbg_rcvry("failing in bud LEB %d commit running", lnum);
2615 } else {
2616 if (chance(9999, 10000))
2617 return 0;
2618 dbg_rcvry("failing in bud LEB %d commit not running", lnum);
2620 ubifs_err("*** SETTING FAILURE MODE ON (LEB %d) ***", lnum);
2621 d->failure_mode = 1;
2622 dump_stack();
2623 return 1;
2626 static void cut_data(const void *buf, int len)
2628 int flen, i;
2629 unsigned char *p = (void *)buf;
2631 flen = (len * (long long)simple_rand()) >> 15;
2632 for (i = flen; i < len; i++)
2633 p[i] = 0xff;
2636 int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
2637 int len, int check)
2639 if (in_failure_mode(desc))
2640 return -EIO;
2641 return ubi_leb_read(desc, lnum, buf, offset, len, check);
2644 int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
2645 int offset, int len, int dtype)
2647 int err, failing;
2649 if (in_failure_mode(desc))
2650 return -EIO;
2651 failing = do_fail(desc, lnum, 1);
2652 if (failing)
2653 cut_data(buf, len);
2654 err = ubi_leb_write(desc, lnum, buf, offset, len, dtype);
2655 if (err)
2656 return err;
2657 if (failing)
2658 return -EIO;
2659 return 0;
2662 int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
2663 int len, int dtype)
2665 int err;
2667 if (do_fail(desc, lnum, 1))
2668 return -EIO;
2669 err = ubi_leb_change(desc, lnum, buf, len, dtype);
2670 if (err)
2671 return err;
2672 if (do_fail(desc, lnum, 1))
2673 return -EIO;
2674 return 0;
2677 int dbg_leb_erase(struct ubi_volume_desc *desc, int lnum)
2679 int err;
2681 if (do_fail(desc, lnum, 0))
2682 return -EIO;
2683 err = ubi_leb_erase(desc, lnum);
2684 if (err)
2685 return err;
2686 if (do_fail(desc, lnum, 0))
2687 return -EIO;
2688 return 0;
2691 int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum)
2693 int err;
2695 if (do_fail(desc, lnum, 0))
2696 return -EIO;
2697 err = ubi_leb_unmap(desc, lnum);
2698 if (err)
2699 return err;
2700 if (do_fail(desc, lnum, 0))
2701 return -EIO;
2702 return 0;
2705 int dbg_is_mapped(struct ubi_volume_desc *desc, int lnum)
2707 if (in_failure_mode(desc))
2708 return -EIO;
2709 return ubi_is_mapped(desc, lnum);
2712 int dbg_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
2714 int err;
2716 if (do_fail(desc, lnum, 0))
2717 return -EIO;
2718 err = ubi_leb_map(desc, lnum, dtype);
2719 if (err)
2720 return err;
2721 if (do_fail(desc, lnum, 0))
2722 return -EIO;
2723 return 0;
2727 * ubifs_debugging_init - initialize UBIFS debugging.
2728 * @c: UBIFS file-system description object
2730 * This function initializes debugging-related data for the file system.
2731 * Returns zero in case of success and a negative error code in case of
2732 * failure.
2734 int ubifs_debugging_init(struct ubifs_info *c)
2736 c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
2737 if (!c->dbg)
2738 return -ENOMEM;
2740 failure_mode_init(c);
2741 return 0;
2745 * ubifs_debugging_exit - free debugging data.
2746 * @c: UBIFS file-system description object
2748 void ubifs_debugging_exit(struct ubifs_info *c)
2750 failure_mode_exit(c);
2751 kfree(c->dbg);
2755 * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
2756 * contain the stuff specific to particular file-system mounts.
2758 static struct dentry *dfs_rootdir;
2761 * dbg_debugfs_init - initialize debugfs file-system.
2763 * UBIFS uses debugfs file-system to expose various debugging knobs to
2764 * user-space. This function creates "ubifs" directory in the debugfs
2765 * file-system. Returns zero in case of success and a negative error code in
2766 * case of failure.
2768 int dbg_debugfs_init(void)
2770 dfs_rootdir = debugfs_create_dir("ubifs", NULL);
2771 if (IS_ERR(dfs_rootdir)) {
2772 int err = PTR_ERR(dfs_rootdir);
2773 ubifs_err("cannot create \"ubifs\" debugfs directory, "
2774 "error %d\n", err);
2775 return err;
2778 return 0;
2782 * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
2784 void dbg_debugfs_exit(void)
2786 debugfs_remove(dfs_rootdir);
2789 static int open_debugfs_file(struct inode *inode, struct file *file)
2791 file->private_data = inode->i_private;
2792 return nonseekable_open(inode, file);
2795 static ssize_t write_debugfs_file(struct file *file, const char __user *buf,
2796 size_t count, loff_t *ppos)
2798 struct ubifs_info *c = file->private_data;
2799 struct ubifs_debug_info *d = c->dbg;
2801 if (file->f_path.dentry == d->dfs_dump_lprops)
2802 dbg_dump_lprops(c);
2803 else if (file->f_path.dentry == d->dfs_dump_budg)
2804 dbg_dump_budg(c);
2805 else if (file->f_path.dentry == d->dfs_dump_tnc) {
2806 mutex_lock(&c->tnc_mutex);
2807 dbg_dump_tnc(c);
2808 mutex_unlock(&c->tnc_mutex);
2809 } else
2810 return -EINVAL;
2812 *ppos += count;
2813 return count;
2816 static const struct file_operations dfs_fops = {
2817 .open = open_debugfs_file,
2818 .write = write_debugfs_file,
2819 .owner = THIS_MODULE,
2820 .llseek = no_llseek,
2824 * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
2825 * @c: UBIFS file-system description object
2827 * This function creates all debugfs files for this instance of UBIFS. Returns
2828 * zero in case of success and a negative error code in case of failure.
2830 * Note, the only reason we have not merged this function with the
2831 * 'ubifs_debugging_init()' function is because it is better to initialize
2832 * debugfs interfaces at the very end of the mount process, and remove them at
2833 * the very beginning of the mount process.
2835 int dbg_debugfs_init_fs(struct ubifs_info *c)
2837 int err;
2838 const char *fname;
2839 struct dentry *dent;
2840 struct ubifs_debug_info *d = c->dbg;
2842 sprintf(d->dfs_dir_name, "ubi%d_%d", c->vi.ubi_num, c->vi.vol_id);
2843 fname = d->dfs_dir_name;
2844 dent = debugfs_create_dir(fname, dfs_rootdir);
2845 if (IS_ERR_OR_NULL(dent))
2846 goto out;
2847 d->dfs_dir = dent;
2849 fname = "dump_lprops";
2850 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2851 if (IS_ERR_OR_NULL(dent))
2852 goto out_remove;
2853 d->dfs_dump_lprops = dent;
2855 fname = "dump_budg";
2856 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2857 if (IS_ERR_OR_NULL(dent))
2858 goto out_remove;
2859 d->dfs_dump_budg = dent;
2861 fname = "dump_tnc";
2862 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2863 if (IS_ERR_OR_NULL(dent))
2864 goto out_remove;
2865 d->dfs_dump_tnc = dent;
2867 return 0;
2869 out_remove:
2870 debugfs_remove_recursive(d->dfs_dir);
2871 out:
2872 err = dent ? PTR_ERR(dent) : -ENODEV;
2873 ubifs_err("cannot create \"%s\" debugfs directory, error %d\n",
2874 fname, err);
2875 return err;
2879 * dbg_debugfs_exit_fs - remove all debugfs files.
2880 * @c: UBIFS file-system description object
2882 void dbg_debugfs_exit_fs(struct ubifs_info *c)
2884 debugfs_remove_recursive(c->dbg->dfs_dir);
2887 #endif /* CONFIG_UBIFS_FS_DEBUG */