mfd: Copy the device pointer to the twl4030-madc structure
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ubifs / debug.c
blob0bb2bcef0de9a8ab8815dfd62103b0bbe601202f
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_chk_flags;
46 unsigned int ubifs_tst_flags;
48 module_param_named(debug_chks, ubifs_chk_flags, uint, S_IRUGO | S_IWUSR);
49 module_param_named(debug_tsts, ubifs_tst_flags, uint, S_IRUGO | S_IWUSR);
51 MODULE_PARM_DESC(debug_chks, "Debug check flags");
52 MODULE_PARM_DESC(debug_tsts, "Debug special test flags");
54 static const char *get_key_fmt(int fmt)
56 switch (fmt) {
57 case UBIFS_SIMPLE_KEY_FMT:
58 return "simple";
59 default:
60 return "unknown/invalid format";
64 static const char *get_key_hash(int hash)
66 switch (hash) {
67 case UBIFS_KEY_HASH_R5:
68 return "R5";
69 case UBIFS_KEY_HASH_TEST:
70 return "test";
71 default:
72 return "unknown/invalid name hash";
76 static const char *get_key_type(int type)
78 switch (type) {
79 case UBIFS_INO_KEY:
80 return "inode";
81 case UBIFS_DENT_KEY:
82 return "direntry";
83 case UBIFS_XENT_KEY:
84 return "xentry";
85 case UBIFS_DATA_KEY:
86 return "data";
87 case UBIFS_TRUN_KEY:
88 return "truncate";
89 default:
90 return "unknown/invalid key";
94 static void sprintf_key(const struct ubifs_info *c, const union ubifs_key *key,
95 char *buffer)
97 char *p = buffer;
98 int type = key_type(c, key);
100 if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
101 switch (type) {
102 case UBIFS_INO_KEY:
103 sprintf(p, "(%lu, %s)", (unsigned long)key_inum(c, key),
104 get_key_type(type));
105 break;
106 case UBIFS_DENT_KEY:
107 case UBIFS_XENT_KEY:
108 sprintf(p, "(%lu, %s, %#08x)",
109 (unsigned long)key_inum(c, key),
110 get_key_type(type), key_hash(c, key));
111 break;
112 case UBIFS_DATA_KEY:
113 sprintf(p, "(%lu, %s, %u)",
114 (unsigned long)key_inum(c, key),
115 get_key_type(type), key_block(c, key));
116 break;
117 case UBIFS_TRUN_KEY:
118 sprintf(p, "(%lu, %s)",
119 (unsigned long)key_inum(c, key),
120 get_key_type(type));
121 break;
122 default:
123 sprintf(p, "(bad key type: %#08x, %#08x)",
124 key->u32[0], key->u32[1]);
126 } else
127 sprintf(p, "bad key format %d", c->key_fmt);
130 const char *dbg_key_str0(const struct ubifs_info *c, const union ubifs_key *key)
132 /* dbg_lock must be held */
133 sprintf_key(c, key, dbg_key_buf0);
134 return dbg_key_buf0;
137 const char *dbg_key_str1(const struct ubifs_info *c, const union ubifs_key *key)
139 /* dbg_lock must be held */
140 sprintf_key(c, key, dbg_key_buf1);
141 return dbg_key_buf1;
144 const char *dbg_ntype(int type)
146 switch (type) {
147 case UBIFS_PAD_NODE:
148 return "padding node";
149 case UBIFS_SB_NODE:
150 return "superblock node";
151 case UBIFS_MST_NODE:
152 return "master node";
153 case UBIFS_REF_NODE:
154 return "reference node";
155 case UBIFS_INO_NODE:
156 return "inode node";
157 case UBIFS_DENT_NODE:
158 return "direntry node";
159 case UBIFS_XENT_NODE:
160 return "xentry node";
161 case UBIFS_DATA_NODE:
162 return "data node";
163 case UBIFS_TRUN_NODE:
164 return "truncate node";
165 case UBIFS_IDX_NODE:
166 return "indexing node";
167 case UBIFS_CS_NODE:
168 return "commit start node";
169 case UBIFS_ORPH_NODE:
170 return "orphan node";
171 default:
172 return "unknown node";
176 static const char *dbg_gtype(int type)
178 switch (type) {
179 case UBIFS_NO_NODE_GROUP:
180 return "no node group";
181 case UBIFS_IN_NODE_GROUP:
182 return "in node group";
183 case UBIFS_LAST_OF_NODE_GROUP:
184 return "last of node group";
185 default:
186 return "unknown";
190 const char *dbg_cstate(int cmt_state)
192 switch (cmt_state) {
193 case COMMIT_RESTING:
194 return "commit resting";
195 case COMMIT_BACKGROUND:
196 return "background commit requested";
197 case COMMIT_REQUIRED:
198 return "commit required";
199 case COMMIT_RUNNING_BACKGROUND:
200 return "BACKGROUND commit running";
201 case COMMIT_RUNNING_REQUIRED:
202 return "commit running and required";
203 case COMMIT_BROKEN:
204 return "broken commit";
205 default:
206 return "unknown commit state";
210 const char *dbg_jhead(int jhead)
212 switch (jhead) {
213 case GCHD:
214 return "0 (GC)";
215 case BASEHD:
216 return "1 (base)";
217 case DATAHD:
218 return "2 (data)";
219 default:
220 return "unknown journal head";
224 static void dump_ch(const struct ubifs_ch *ch)
226 printk(KERN_DEBUG "\tmagic %#x\n", le32_to_cpu(ch->magic));
227 printk(KERN_DEBUG "\tcrc %#x\n", le32_to_cpu(ch->crc));
228 printk(KERN_DEBUG "\tnode_type %d (%s)\n", ch->node_type,
229 dbg_ntype(ch->node_type));
230 printk(KERN_DEBUG "\tgroup_type %d (%s)\n", ch->group_type,
231 dbg_gtype(ch->group_type));
232 printk(KERN_DEBUG "\tsqnum %llu\n",
233 (unsigned long long)le64_to_cpu(ch->sqnum));
234 printk(KERN_DEBUG "\tlen %u\n", le32_to_cpu(ch->len));
237 void dbg_dump_inode(const struct ubifs_info *c, const struct inode *inode)
239 const struct ubifs_inode *ui = ubifs_inode(inode);
241 printk(KERN_DEBUG "Dump in-memory inode:");
242 printk(KERN_DEBUG "\tinode %lu\n", inode->i_ino);
243 printk(KERN_DEBUG "\tsize %llu\n",
244 (unsigned long long)i_size_read(inode));
245 printk(KERN_DEBUG "\tnlink %u\n", inode->i_nlink);
246 printk(KERN_DEBUG "\tuid %u\n", (unsigned int)inode->i_uid);
247 printk(KERN_DEBUG "\tgid %u\n", (unsigned int)inode->i_gid);
248 printk(KERN_DEBUG "\tatime %u.%u\n",
249 (unsigned int)inode->i_atime.tv_sec,
250 (unsigned int)inode->i_atime.tv_nsec);
251 printk(KERN_DEBUG "\tmtime %u.%u\n",
252 (unsigned int)inode->i_mtime.tv_sec,
253 (unsigned int)inode->i_mtime.tv_nsec);
254 printk(KERN_DEBUG "\tctime %u.%u\n",
255 (unsigned int)inode->i_ctime.tv_sec,
256 (unsigned int)inode->i_ctime.tv_nsec);
257 printk(KERN_DEBUG "\tcreat_sqnum %llu\n", ui->creat_sqnum);
258 printk(KERN_DEBUG "\txattr_size %u\n", ui->xattr_size);
259 printk(KERN_DEBUG "\txattr_cnt %u\n", ui->xattr_cnt);
260 printk(KERN_DEBUG "\txattr_names %u\n", ui->xattr_names);
261 printk(KERN_DEBUG "\tdirty %u\n", ui->dirty);
262 printk(KERN_DEBUG "\txattr %u\n", ui->xattr);
263 printk(KERN_DEBUG "\tbulk_read %u\n", ui->xattr);
264 printk(KERN_DEBUG "\tsynced_i_size %llu\n",
265 (unsigned long long)ui->synced_i_size);
266 printk(KERN_DEBUG "\tui_size %llu\n",
267 (unsigned long long)ui->ui_size);
268 printk(KERN_DEBUG "\tflags %d\n", ui->flags);
269 printk(KERN_DEBUG "\tcompr_type %d\n", ui->compr_type);
270 printk(KERN_DEBUG "\tlast_page_read %lu\n", ui->last_page_read);
271 printk(KERN_DEBUG "\tread_in_a_row %lu\n", ui->read_in_a_row);
272 printk(KERN_DEBUG "\tdata_len %d\n", ui->data_len);
275 void dbg_dump_node(const struct ubifs_info *c, const void *node)
277 int i, n;
278 union ubifs_key key;
279 const struct ubifs_ch *ch = node;
281 if (dbg_failure_mode)
282 return;
284 /* If the magic is incorrect, just hexdump the first bytes */
285 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
286 printk(KERN_DEBUG "Not a node, first %zu bytes:", UBIFS_CH_SZ);
287 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
288 (void *)node, UBIFS_CH_SZ, 1);
289 return;
292 spin_lock(&dbg_lock);
293 dump_ch(node);
295 switch (ch->node_type) {
296 case UBIFS_PAD_NODE:
298 const struct ubifs_pad_node *pad = node;
300 printk(KERN_DEBUG "\tpad_len %u\n",
301 le32_to_cpu(pad->pad_len));
302 break;
304 case UBIFS_SB_NODE:
306 const struct ubifs_sb_node *sup = node;
307 unsigned int sup_flags = le32_to_cpu(sup->flags);
309 printk(KERN_DEBUG "\tkey_hash %d (%s)\n",
310 (int)sup->key_hash, get_key_hash(sup->key_hash));
311 printk(KERN_DEBUG "\tkey_fmt %d (%s)\n",
312 (int)sup->key_fmt, get_key_fmt(sup->key_fmt));
313 printk(KERN_DEBUG "\tflags %#x\n", sup_flags);
314 printk(KERN_DEBUG "\t big_lpt %u\n",
315 !!(sup_flags & UBIFS_FLG_BIGLPT));
316 printk(KERN_DEBUG "\t space_fixup %u\n",
317 !!(sup_flags & UBIFS_FLG_SPACE_FIXUP));
318 printk(KERN_DEBUG "\tmin_io_size %u\n",
319 le32_to_cpu(sup->min_io_size));
320 printk(KERN_DEBUG "\tleb_size %u\n",
321 le32_to_cpu(sup->leb_size));
322 printk(KERN_DEBUG "\tleb_cnt %u\n",
323 le32_to_cpu(sup->leb_cnt));
324 printk(KERN_DEBUG "\tmax_leb_cnt %u\n",
325 le32_to_cpu(sup->max_leb_cnt));
326 printk(KERN_DEBUG "\tmax_bud_bytes %llu\n",
327 (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
328 printk(KERN_DEBUG "\tlog_lebs %u\n",
329 le32_to_cpu(sup->log_lebs));
330 printk(KERN_DEBUG "\tlpt_lebs %u\n",
331 le32_to_cpu(sup->lpt_lebs));
332 printk(KERN_DEBUG "\torph_lebs %u\n",
333 le32_to_cpu(sup->orph_lebs));
334 printk(KERN_DEBUG "\tjhead_cnt %u\n",
335 le32_to_cpu(sup->jhead_cnt));
336 printk(KERN_DEBUG "\tfanout %u\n",
337 le32_to_cpu(sup->fanout));
338 printk(KERN_DEBUG "\tlsave_cnt %u\n",
339 le32_to_cpu(sup->lsave_cnt));
340 printk(KERN_DEBUG "\tdefault_compr %u\n",
341 (int)le16_to_cpu(sup->default_compr));
342 printk(KERN_DEBUG "\trp_size %llu\n",
343 (unsigned long long)le64_to_cpu(sup->rp_size));
344 printk(KERN_DEBUG "\trp_uid %u\n",
345 le32_to_cpu(sup->rp_uid));
346 printk(KERN_DEBUG "\trp_gid %u\n",
347 le32_to_cpu(sup->rp_gid));
348 printk(KERN_DEBUG "\tfmt_version %u\n",
349 le32_to_cpu(sup->fmt_version));
350 printk(KERN_DEBUG "\ttime_gran %u\n",
351 le32_to_cpu(sup->time_gran));
352 printk(KERN_DEBUG "\tUUID %pUB\n",
353 sup->uuid);
354 break;
356 case UBIFS_MST_NODE:
358 const struct ubifs_mst_node *mst = node;
360 printk(KERN_DEBUG "\thighest_inum %llu\n",
361 (unsigned long long)le64_to_cpu(mst->highest_inum));
362 printk(KERN_DEBUG "\tcommit number %llu\n",
363 (unsigned long long)le64_to_cpu(mst->cmt_no));
364 printk(KERN_DEBUG "\tflags %#x\n",
365 le32_to_cpu(mst->flags));
366 printk(KERN_DEBUG "\tlog_lnum %u\n",
367 le32_to_cpu(mst->log_lnum));
368 printk(KERN_DEBUG "\troot_lnum %u\n",
369 le32_to_cpu(mst->root_lnum));
370 printk(KERN_DEBUG "\troot_offs %u\n",
371 le32_to_cpu(mst->root_offs));
372 printk(KERN_DEBUG "\troot_len %u\n",
373 le32_to_cpu(mst->root_len));
374 printk(KERN_DEBUG "\tgc_lnum %u\n",
375 le32_to_cpu(mst->gc_lnum));
376 printk(KERN_DEBUG "\tihead_lnum %u\n",
377 le32_to_cpu(mst->ihead_lnum));
378 printk(KERN_DEBUG "\tihead_offs %u\n",
379 le32_to_cpu(mst->ihead_offs));
380 printk(KERN_DEBUG "\tindex_size %llu\n",
381 (unsigned long long)le64_to_cpu(mst->index_size));
382 printk(KERN_DEBUG "\tlpt_lnum %u\n",
383 le32_to_cpu(mst->lpt_lnum));
384 printk(KERN_DEBUG "\tlpt_offs %u\n",
385 le32_to_cpu(mst->lpt_offs));
386 printk(KERN_DEBUG "\tnhead_lnum %u\n",
387 le32_to_cpu(mst->nhead_lnum));
388 printk(KERN_DEBUG "\tnhead_offs %u\n",
389 le32_to_cpu(mst->nhead_offs));
390 printk(KERN_DEBUG "\tltab_lnum %u\n",
391 le32_to_cpu(mst->ltab_lnum));
392 printk(KERN_DEBUG "\tltab_offs %u\n",
393 le32_to_cpu(mst->ltab_offs));
394 printk(KERN_DEBUG "\tlsave_lnum %u\n",
395 le32_to_cpu(mst->lsave_lnum));
396 printk(KERN_DEBUG "\tlsave_offs %u\n",
397 le32_to_cpu(mst->lsave_offs));
398 printk(KERN_DEBUG "\tlscan_lnum %u\n",
399 le32_to_cpu(mst->lscan_lnum));
400 printk(KERN_DEBUG "\tleb_cnt %u\n",
401 le32_to_cpu(mst->leb_cnt));
402 printk(KERN_DEBUG "\tempty_lebs %u\n",
403 le32_to_cpu(mst->empty_lebs));
404 printk(KERN_DEBUG "\tidx_lebs %u\n",
405 le32_to_cpu(mst->idx_lebs));
406 printk(KERN_DEBUG "\ttotal_free %llu\n",
407 (unsigned long long)le64_to_cpu(mst->total_free));
408 printk(KERN_DEBUG "\ttotal_dirty %llu\n",
409 (unsigned long long)le64_to_cpu(mst->total_dirty));
410 printk(KERN_DEBUG "\ttotal_used %llu\n",
411 (unsigned long long)le64_to_cpu(mst->total_used));
412 printk(KERN_DEBUG "\ttotal_dead %llu\n",
413 (unsigned long long)le64_to_cpu(mst->total_dead));
414 printk(KERN_DEBUG "\ttotal_dark %llu\n",
415 (unsigned long long)le64_to_cpu(mst->total_dark));
416 break;
418 case UBIFS_REF_NODE:
420 const struct ubifs_ref_node *ref = node;
422 printk(KERN_DEBUG "\tlnum %u\n",
423 le32_to_cpu(ref->lnum));
424 printk(KERN_DEBUG "\toffs %u\n",
425 le32_to_cpu(ref->offs));
426 printk(KERN_DEBUG "\tjhead %u\n",
427 le32_to_cpu(ref->jhead));
428 break;
430 case UBIFS_INO_NODE:
432 const struct ubifs_ino_node *ino = node;
434 key_read(c, &ino->key, &key);
435 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
436 printk(KERN_DEBUG "\tcreat_sqnum %llu\n",
437 (unsigned long long)le64_to_cpu(ino->creat_sqnum));
438 printk(KERN_DEBUG "\tsize %llu\n",
439 (unsigned long long)le64_to_cpu(ino->size));
440 printk(KERN_DEBUG "\tnlink %u\n",
441 le32_to_cpu(ino->nlink));
442 printk(KERN_DEBUG "\tatime %lld.%u\n",
443 (long long)le64_to_cpu(ino->atime_sec),
444 le32_to_cpu(ino->atime_nsec));
445 printk(KERN_DEBUG "\tmtime %lld.%u\n",
446 (long long)le64_to_cpu(ino->mtime_sec),
447 le32_to_cpu(ino->mtime_nsec));
448 printk(KERN_DEBUG "\tctime %lld.%u\n",
449 (long long)le64_to_cpu(ino->ctime_sec),
450 le32_to_cpu(ino->ctime_nsec));
451 printk(KERN_DEBUG "\tuid %u\n",
452 le32_to_cpu(ino->uid));
453 printk(KERN_DEBUG "\tgid %u\n",
454 le32_to_cpu(ino->gid));
455 printk(KERN_DEBUG "\tmode %u\n",
456 le32_to_cpu(ino->mode));
457 printk(KERN_DEBUG "\tflags %#x\n",
458 le32_to_cpu(ino->flags));
459 printk(KERN_DEBUG "\txattr_cnt %u\n",
460 le32_to_cpu(ino->xattr_cnt));
461 printk(KERN_DEBUG "\txattr_size %u\n",
462 le32_to_cpu(ino->xattr_size));
463 printk(KERN_DEBUG "\txattr_names %u\n",
464 le32_to_cpu(ino->xattr_names));
465 printk(KERN_DEBUG "\tcompr_type %#x\n",
466 (int)le16_to_cpu(ino->compr_type));
467 printk(KERN_DEBUG "\tdata len %u\n",
468 le32_to_cpu(ino->data_len));
469 break;
471 case UBIFS_DENT_NODE:
472 case UBIFS_XENT_NODE:
474 const struct ubifs_dent_node *dent = node;
475 int nlen = le16_to_cpu(dent->nlen);
477 key_read(c, &dent->key, &key);
478 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
479 printk(KERN_DEBUG "\tinum %llu\n",
480 (unsigned long long)le64_to_cpu(dent->inum));
481 printk(KERN_DEBUG "\ttype %d\n", (int)dent->type);
482 printk(KERN_DEBUG "\tnlen %d\n", nlen);
483 printk(KERN_DEBUG "\tname ");
485 if (nlen > UBIFS_MAX_NLEN)
486 printk(KERN_DEBUG "(bad name length, not printing, "
487 "bad or corrupted node)");
488 else {
489 for (i = 0; i < nlen && dent->name[i]; i++)
490 printk(KERN_CONT "%c", dent->name[i]);
492 printk(KERN_CONT "\n");
494 break;
496 case UBIFS_DATA_NODE:
498 const struct ubifs_data_node *dn = node;
499 int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
501 key_read(c, &dn->key, &key);
502 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
503 printk(KERN_DEBUG "\tsize %u\n",
504 le32_to_cpu(dn->size));
505 printk(KERN_DEBUG "\tcompr_typ %d\n",
506 (int)le16_to_cpu(dn->compr_type));
507 printk(KERN_DEBUG "\tdata size %d\n",
508 dlen);
509 printk(KERN_DEBUG "\tdata:\n");
510 print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET, 32, 1,
511 (void *)&dn->data, dlen, 0);
512 break;
514 case UBIFS_TRUN_NODE:
516 const struct ubifs_trun_node *trun = node;
518 printk(KERN_DEBUG "\tinum %u\n",
519 le32_to_cpu(trun->inum));
520 printk(KERN_DEBUG "\told_size %llu\n",
521 (unsigned long long)le64_to_cpu(trun->old_size));
522 printk(KERN_DEBUG "\tnew_size %llu\n",
523 (unsigned long long)le64_to_cpu(trun->new_size));
524 break;
526 case UBIFS_IDX_NODE:
528 const struct ubifs_idx_node *idx = node;
530 n = le16_to_cpu(idx->child_cnt);
531 printk(KERN_DEBUG "\tchild_cnt %d\n", n);
532 printk(KERN_DEBUG "\tlevel %d\n",
533 (int)le16_to_cpu(idx->level));
534 printk(KERN_DEBUG "\tBranches:\n");
536 for (i = 0; i < n && i < c->fanout - 1; i++) {
537 const struct ubifs_branch *br;
539 br = ubifs_idx_branch(c, idx, i);
540 key_read(c, &br->key, &key);
541 printk(KERN_DEBUG "\t%d: LEB %d:%d len %d key %s\n",
542 i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
543 le32_to_cpu(br->len), DBGKEY(&key));
545 break;
547 case UBIFS_CS_NODE:
548 break;
549 case UBIFS_ORPH_NODE:
551 const struct ubifs_orph_node *orph = node;
553 printk(KERN_DEBUG "\tcommit number %llu\n",
554 (unsigned long long)
555 le64_to_cpu(orph->cmt_no) & LLONG_MAX);
556 printk(KERN_DEBUG "\tlast node flag %llu\n",
557 (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
558 n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
559 printk(KERN_DEBUG "\t%d orphan inode numbers:\n", n);
560 for (i = 0; i < n; i++)
561 printk(KERN_DEBUG "\t ino %llu\n",
562 (unsigned long long)le64_to_cpu(orph->inos[i]));
563 break;
565 default:
566 printk(KERN_DEBUG "node type %d was not recognized\n",
567 (int)ch->node_type);
569 spin_unlock(&dbg_lock);
572 void dbg_dump_budget_req(const struct ubifs_budget_req *req)
574 spin_lock(&dbg_lock);
575 printk(KERN_DEBUG "Budgeting request: new_ino %d, dirtied_ino %d\n",
576 req->new_ino, req->dirtied_ino);
577 printk(KERN_DEBUG "\tnew_ino_d %d, dirtied_ino_d %d\n",
578 req->new_ino_d, req->dirtied_ino_d);
579 printk(KERN_DEBUG "\tnew_page %d, dirtied_page %d\n",
580 req->new_page, req->dirtied_page);
581 printk(KERN_DEBUG "\tnew_dent %d, mod_dent %d\n",
582 req->new_dent, req->mod_dent);
583 printk(KERN_DEBUG "\tidx_growth %d\n", req->idx_growth);
584 printk(KERN_DEBUG "\tdata_growth %d dd_growth %d\n",
585 req->data_growth, req->dd_growth);
586 spin_unlock(&dbg_lock);
589 void dbg_dump_lstats(const struct ubifs_lp_stats *lst)
591 spin_lock(&dbg_lock);
592 printk(KERN_DEBUG "(pid %d) Lprops statistics: empty_lebs %d, "
593 "idx_lebs %d\n", current->pid, lst->empty_lebs, lst->idx_lebs);
594 printk(KERN_DEBUG "\ttaken_empty_lebs %d, total_free %lld, "
595 "total_dirty %lld\n", lst->taken_empty_lebs, lst->total_free,
596 lst->total_dirty);
597 printk(KERN_DEBUG "\ttotal_used %lld, total_dark %lld, "
598 "total_dead %lld\n", lst->total_used, lst->total_dark,
599 lst->total_dead);
600 spin_unlock(&dbg_lock);
603 void dbg_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)
605 int i;
606 struct rb_node *rb;
607 struct ubifs_bud *bud;
608 struct ubifs_gced_idx_leb *idx_gc;
609 long long available, outstanding, free;
611 spin_lock(&c->space_lock);
612 spin_lock(&dbg_lock);
613 printk(KERN_DEBUG "(pid %d) Budgeting info: data budget sum %lld, "
614 "total budget sum %lld\n", current->pid,
615 bi->data_growth + bi->dd_growth,
616 bi->data_growth + bi->dd_growth + bi->idx_growth);
617 printk(KERN_DEBUG "\tbudg_data_growth %lld, budg_dd_growth %lld, "
618 "budg_idx_growth %lld\n", bi->data_growth, bi->dd_growth,
619 bi->idx_growth);
620 printk(KERN_DEBUG "\tmin_idx_lebs %d, old_idx_sz %llu, "
621 "uncommitted_idx %lld\n", bi->min_idx_lebs, bi->old_idx_sz,
622 bi->uncommitted_idx);
623 printk(KERN_DEBUG "\tpage_budget %d, inode_budget %d, dent_budget %d\n",
624 bi->page_budget, bi->inode_budget, bi->dent_budget);
625 printk(KERN_DEBUG "\tnospace %u, nospace_rp %u\n",
626 bi->nospace, bi->nospace_rp);
627 printk(KERN_DEBUG "\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
628 c->dark_wm, c->dead_wm, c->max_idx_node_sz);
630 if (bi != &c->bi)
632 * If we are dumping saved budgeting data, do not print
633 * additional information which is about the current state, not
634 * the old one which corresponded to the saved budgeting data.
636 goto out_unlock;
638 printk(KERN_DEBUG "\tfreeable_cnt %d, calc_idx_sz %lld, idx_gc_cnt %d\n",
639 c->freeable_cnt, c->calc_idx_sz, c->idx_gc_cnt);
640 printk(KERN_DEBUG "\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, "
641 "clean_zn_cnt %ld\n", atomic_long_read(&c->dirty_pg_cnt),
642 atomic_long_read(&c->dirty_zn_cnt),
643 atomic_long_read(&c->clean_zn_cnt));
644 printk(KERN_DEBUG "\tgc_lnum %d, ihead_lnum %d\n",
645 c->gc_lnum, c->ihead_lnum);
647 /* If we are in R/O mode, journal heads do not exist */
648 if (c->jheads)
649 for (i = 0; i < c->jhead_cnt; i++)
650 printk(KERN_DEBUG "\tjhead %s\t LEB %d\n",
651 dbg_jhead(c->jheads[i].wbuf.jhead),
652 c->jheads[i].wbuf.lnum);
653 for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
654 bud = rb_entry(rb, struct ubifs_bud, rb);
655 printk(KERN_DEBUG "\tbud LEB %d\n", bud->lnum);
657 list_for_each_entry(bud, &c->old_buds, list)
658 printk(KERN_DEBUG "\told bud LEB %d\n", bud->lnum);
659 list_for_each_entry(idx_gc, &c->idx_gc, list)
660 printk(KERN_DEBUG "\tGC'ed idx LEB %d unmap %d\n",
661 idx_gc->lnum, idx_gc->unmap);
662 printk(KERN_DEBUG "\tcommit state %d\n", c->cmt_state);
664 /* Print budgeting predictions */
665 available = ubifs_calc_available(c, c->bi.min_idx_lebs);
666 outstanding = c->bi.data_growth + c->bi.dd_growth;
667 free = ubifs_get_free_space_nolock(c);
668 printk(KERN_DEBUG "Budgeting predictions:\n");
669 printk(KERN_DEBUG "\tavailable: %lld, outstanding %lld, free %lld\n",
670 available, outstanding, free);
671 out_unlock:
672 spin_unlock(&dbg_lock);
673 spin_unlock(&c->space_lock);
676 void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
678 int i, spc, dark = 0, dead = 0;
679 struct rb_node *rb;
680 struct ubifs_bud *bud;
682 spc = lp->free + lp->dirty;
683 if (spc < c->dead_wm)
684 dead = spc;
685 else
686 dark = ubifs_calc_dark(c, spc);
688 if (lp->flags & LPROPS_INDEX)
689 printk(KERN_DEBUG "LEB %-7d free %-8d dirty %-8d used %-8d "
690 "free + dirty %-8d flags %#x (", lp->lnum, lp->free,
691 lp->dirty, c->leb_size - spc, spc, lp->flags);
692 else
693 printk(KERN_DEBUG "LEB %-7d free %-8d dirty %-8d used %-8d "
694 "free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d "
695 "flags %#-4x (", lp->lnum, lp->free, lp->dirty,
696 c->leb_size - spc, spc, dark, dead,
697 (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags);
699 if (lp->flags & LPROPS_TAKEN) {
700 if (lp->flags & LPROPS_INDEX)
701 printk(KERN_CONT "index, taken");
702 else
703 printk(KERN_CONT "taken");
704 } else {
705 const char *s;
707 if (lp->flags & LPROPS_INDEX) {
708 switch (lp->flags & LPROPS_CAT_MASK) {
709 case LPROPS_DIRTY_IDX:
710 s = "dirty index";
711 break;
712 case LPROPS_FRDI_IDX:
713 s = "freeable index";
714 break;
715 default:
716 s = "index";
718 } else {
719 switch (lp->flags & LPROPS_CAT_MASK) {
720 case LPROPS_UNCAT:
721 s = "not categorized";
722 break;
723 case LPROPS_DIRTY:
724 s = "dirty";
725 break;
726 case LPROPS_FREE:
727 s = "free";
728 break;
729 case LPROPS_EMPTY:
730 s = "empty";
731 break;
732 case LPROPS_FREEABLE:
733 s = "freeable";
734 break;
735 default:
736 s = NULL;
737 break;
740 printk(KERN_CONT "%s", s);
743 for (rb = rb_first((struct rb_root *)&c->buds); rb; rb = rb_next(rb)) {
744 bud = rb_entry(rb, struct ubifs_bud, rb);
745 if (bud->lnum == lp->lnum) {
746 int head = 0;
747 for (i = 0; i < c->jhead_cnt; i++) {
749 * Note, if we are in R/O mode or in the middle
750 * of mounting/re-mounting, the write-buffers do
751 * not exist.
753 if (c->jheads &&
754 lp->lnum == c->jheads[i].wbuf.lnum) {
755 printk(KERN_CONT ", jhead %s",
756 dbg_jhead(i));
757 head = 1;
760 if (!head)
761 printk(KERN_CONT ", bud of jhead %s",
762 dbg_jhead(bud->jhead));
765 if (lp->lnum == c->gc_lnum)
766 printk(KERN_CONT ", GC LEB");
767 printk(KERN_CONT ")\n");
770 void dbg_dump_lprops(struct ubifs_info *c)
772 int lnum, err;
773 struct ubifs_lprops lp;
774 struct ubifs_lp_stats lst;
776 printk(KERN_DEBUG "(pid %d) start dumping LEB properties\n",
777 current->pid);
778 ubifs_get_lp_stats(c, &lst);
779 dbg_dump_lstats(&lst);
781 for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
782 err = ubifs_read_one_lp(c, lnum, &lp);
783 if (err)
784 ubifs_err("cannot read lprops for LEB %d", lnum);
786 dbg_dump_lprop(c, &lp);
788 printk(KERN_DEBUG "(pid %d) finish dumping LEB properties\n",
789 current->pid);
792 void dbg_dump_lpt_info(struct ubifs_info *c)
794 int i;
796 spin_lock(&dbg_lock);
797 printk(KERN_DEBUG "(pid %d) dumping LPT information\n", current->pid);
798 printk(KERN_DEBUG "\tlpt_sz: %lld\n", c->lpt_sz);
799 printk(KERN_DEBUG "\tpnode_sz: %d\n", c->pnode_sz);
800 printk(KERN_DEBUG "\tnnode_sz: %d\n", c->nnode_sz);
801 printk(KERN_DEBUG "\tltab_sz: %d\n", c->ltab_sz);
802 printk(KERN_DEBUG "\tlsave_sz: %d\n", c->lsave_sz);
803 printk(KERN_DEBUG "\tbig_lpt: %d\n", c->big_lpt);
804 printk(KERN_DEBUG "\tlpt_hght: %d\n", c->lpt_hght);
805 printk(KERN_DEBUG "\tpnode_cnt: %d\n", c->pnode_cnt);
806 printk(KERN_DEBUG "\tnnode_cnt: %d\n", c->nnode_cnt);
807 printk(KERN_DEBUG "\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt);
808 printk(KERN_DEBUG "\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt);
809 printk(KERN_DEBUG "\tlsave_cnt: %d\n", c->lsave_cnt);
810 printk(KERN_DEBUG "\tspace_bits: %d\n", c->space_bits);
811 printk(KERN_DEBUG "\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
812 printk(KERN_DEBUG "\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
813 printk(KERN_DEBUG "\tlpt_spc_bits: %d\n", c->lpt_spc_bits);
814 printk(KERN_DEBUG "\tpcnt_bits: %d\n", c->pcnt_bits);
815 printk(KERN_DEBUG "\tlnum_bits: %d\n", c->lnum_bits);
816 printk(KERN_DEBUG "\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
817 printk(KERN_DEBUG "\tLPT head is at %d:%d\n",
818 c->nhead_lnum, c->nhead_offs);
819 printk(KERN_DEBUG "\tLPT ltab is at %d:%d\n",
820 c->ltab_lnum, c->ltab_offs);
821 if (c->big_lpt)
822 printk(KERN_DEBUG "\tLPT lsave is at %d:%d\n",
823 c->lsave_lnum, c->lsave_offs);
824 for (i = 0; i < c->lpt_lebs; i++)
825 printk(KERN_DEBUG "\tLPT LEB %d free %d dirty %d tgc %d "
826 "cmt %d\n", i + c->lpt_first, c->ltab[i].free,
827 c->ltab[i].dirty, c->ltab[i].tgc, c->ltab[i].cmt);
828 spin_unlock(&dbg_lock);
831 void dbg_dump_leb(const struct ubifs_info *c, int lnum)
833 struct ubifs_scan_leb *sleb;
834 struct ubifs_scan_node *snod;
835 void *buf;
837 if (dbg_failure_mode)
838 return;
840 printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n",
841 current->pid, lnum);
843 buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
844 if (!buf) {
845 ubifs_err("cannot allocate memory for dumping LEB %d", lnum);
846 return;
849 sleb = ubifs_scan(c, lnum, 0, buf, 0);
850 if (IS_ERR(sleb)) {
851 ubifs_err("scan error %d", (int)PTR_ERR(sleb));
852 goto out;
855 printk(KERN_DEBUG "LEB %d has %d nodes ending at %d\n", lnum,
856 sleb->nodes_cnt, sleb->endpt);
858 list_for_each_entry(snod, &sleb->nodes, list) {
859 cond_resched();
860 printk(KERN_DEBUG "Dumping node at LEB %d:%d len %d\n", lnum,
861 snod->offs, snod->len);
862 dbg_dump_node(c, snod->node);
865 printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n",
866 current->pid, lnum);
867 ubifs_scan_destroy(sleb);
869 out:
870 vfree(buf);
871 return;
874 void dbg_dump_znode(const struct ubifs_info *c,
875 const struct ubifs_znode *znode)
877 int n;
878 const struct ubifs_zbranch *zbr;
880 spin_lock(&dbg_lock);
881 if (znode->parent)
882 zbr = &znode->parent->zbranch[znode->iip];
883 else
884 zbr = &c->zroot;
886 printk(KERN_DEBUG "znode %p, LEB %d:%d len %d parent %p iip %d level %d"
887 " child_cnt %d flags %lx\n", znode, zbr->lnum, zbr->offs,
888 zbr->len, znode->parent, znode->iip, znode->level,
889 znode->child_cnt, znode->flags);
891 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
892 spin_unlock(&dbg_lock);
893 return;
896 printk(KERN_DEBUG "zbranches:\n");
897 for (n = 0; n < znode->child_cnt; n++) {
898 zbr = &znode->zbranch[n];
899 if (znode->level > 0)
900 printk(KERN_DEBUG "\t%d: znode %p LEB %d:%d len %d key "
901 "%s\n", n, zbr->znode, zbr->lnum,
902 zbr->offs, zbr->len,
903 DBGKEY(&zbr->key));
904 else
905 printk(KERN_DEBUG "\t%d: LNC %p LEB %d:%d len %d key "
906 "%s\n", n, zbr->znode, zbr->lnum,
907 zbr->offs, zbr->len,
908 DBGKEY(&zbr->key));
910 spin_unlock(&dbg_lock);
913 void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
915 int i;
917 printk(KERN_DEBUG "(pid %d) start dumping heap cat %d (%d elements)\n",
918 current->pid, cat, heap->cnt);
919 for (i = 0; i < heap->cnt; i++) {
920 struct ubifs_lprops *lprops = heap->arr[i];
922 printk(KERN_DEBUG "\t%d. LEB %d hpos %d free %d dirty %d "
923 "flags %d\n", i, lprops->lnum, lprops->hpos,
924 lprops->free, lprops->dirty, lprops->flags);
926 printk(KERN_DEBUG "(pid %d) finish dumping heap\n", current->pid);
929 void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
930 struct ubifs_nnode *parent, int iip)
932 int i;
934 printk(KERN_DEBUG "(pid %d) dumping pnode:\n", current->pid);
935 printk(KERN_DEBUG "\taddress %zx parent %zx cnext %zx\n",
936 (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
937 printk(KERN_DEBUG "\tflags %lu iip %d level %d num %d\n",
938 pnode->flags, iip, pnode->level, pnode->num);
939 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
940 struct ubifs_lprops *lp = &pnode->lprops[i];
942 printk(KERN_DEBUG "\t%d: free %d dirty %d flags %d lnum %d\n",
943 i, lp->free, lp->dirty, lp->flags, lp->lnum);
947 void dbg_dump_tnc(struct ubifs_info *c)
949 struct ubifs_znode *znode;
950 int level;
952 printk(KERN_DEBUG "\n");
953 printk(KERN_DEBUG "(pid %d) start dumping TNC tree\n", current->pid);
954 znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
955 level = znode->level;
956 printk(KERN_DEBUG "== Level %d ==\n", level);
957 while (znode) {
958 if (level != znode->level) {
959 level = znode->level;
960 printk(KERN_DEBUG "== Level %d ==\n", level);
962 dbg_dump_znode(c, znode);
963 znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
965 printk(KERN_DEBUG "(pid %d) finish dumping TNC tree\n", current->pid);
968 static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
969 void *priv)
971 dbg_dump_znode(c, znode);
972 return 0;
976 * dbg_dump_index - dump the on-flash index.
977 * @c: UBIFS file-system description object
979 * This function dumps whole UBIFS indexing B-tree, unlike 'dbg_dump_tnc()'
980 * which dumps only in-memory znodes and does not read znodes which from flash.
982 void dbg_dump_index(struct ubifs_info *c)
984 dbg_walk_index(c, NULL, dump_znode, NULL);
988 * dbg_save_space_info - save information about flash space.
989 * @c: UBIFS file-system description object
991 * This function saves information about UBIFS free space, dirty space, etc, in
992 * order to check it later.
994 void dbg_save_space_info(struct ubifs_info *c)
996 struct ubifs_debug_info *d = c->dbg;
997 int freeable_cnt;
999 spin_lock(&c->space_lock);
1000 memcpy(&d->saved_lst, &c->lst, sizeof(struct ubifs_lp_stats));
1001 memcpy(&d->saved_bi, &c->bi, sizeof(struct ubifs_budg_info));
1002 d->saved_idx_gc_cnt = c->idx_gc_cnt;
1005 * We use a dirty hack here and zero out @c->freeable_cnt, because it
1006 * affects the free space calculations, and UBIFS might not know about
1007 * all freeable eraseblocks. Indeed, we know about freeable eraseblocks
1008 * only when we read their lprops, and we do this only lazily, upon the
1009 * need. So at any given point of time @c->freeable_cnt might be not
1010 * exactly accurate.
1012 * Just one example about the issue we hit when we did not zero
1013 * @c->freeable_cnt.
1014 * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the
1015 * amount of free space in @d->saved_free
1016 * 2. We re-mount R/W, which makes UBIFS to read the "lsave"
1017 * information from flash, where we cache LEBs from various
1018 * categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()'
1019 * -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()'
1020 * -> 'ubifs_get_pnode()' -> 'update_cats()'
1021 * -> 'ubifs_add_to_cat()').
1022 * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt
1023 * becomes %1.
1024 * 4. We calculate the amount of free space when the re-mount is
1025 * finished in 'dbg_check_space_info()' and it does not match
1026 * @d->saved_free.
1028 freeable_cnt = c->freeable_cnt;
1029 c->freeable_cnt = 0;
1030 d->saved_free = ubifs_get_free_space_nolock(c);
1031 c->freeable_cnt = freeable_cnt;
1032 spin_unlock(&c->space_lock);
1036 * dbg_check_space_info - check flash space information.
1037 * @c: UBIFS file-system description object
1039 * This function compares current flash space information with the information
1040 * which was saved when the 'dbg_save_space_info()' function was called.
1041 * Returns zero if the information has not changed, and %-EINVAL it it has
1042 * changed.
1044 int dbg_check_space_info(struct ubifs_info *c)
1046 struct ubifs_debug_info *d = c->dbg;
1047 struct ubifs_lp_stats lst;
1048 long long free;
1049 int freeable_cnt;
1051 spin_lock(&c->space_lock);
1052 freeable_cnt = c->freeable_cnt;
1053 c->freeable_cnt = 0;
1054 free = ubifs_get_free_space_nolock(c);
1055 c->freeable_cnt = freeable_cnt;
1056 spin_unlock(&c->space_lock);
1058 if (free != d->saved_free) {
1059 ubifs_err("free space changed from %lld to %lld",
1060 d->saved_free, free);
1061 goto out;
1064 return 0;
1066 out:
1067 ubifs_msg("saved lprops statistics dump");
1068 dbg_dump_lstats(&d->saved_lst);
1069 ubifs_msg("saved budgeting info dump");
1070 dbg_dump_budg(c, &d->saved_bi);
1071 ubifs_msg("saved idx_gc_cnt %d", d->saved_idx_gc_cnt);
1072 ubifs_msg("current lprops statistics dump");
1073 ubifs_get_lp_stats(c, &lst);
1074 dbg_dump_lstats(&lst);
1075 ubifs_msg("current budgeting info dump");
1076 dbg_dump_budg(c, &c->bi);
1077 dump_stack();
1078 return -EINVAL;
1082 * dbg_check_synced_i_size - check synchronized inode size.
1083 * @inode: inode to check
1085 * If inode is clean, synchronized inode size has to be equivalent to current
1086 * inode size. This function has to be called only for locked inodes (@i_mutex
1087 * has to be locked). Returns %0 if synchronized inode size if correct, and
1088 * %-EINVAL if not.
1090 int dbg_check_synced_i_size(struct inode *inode)
1092 int err = 0;
1093 struct ubifs_inode *ui = ubifs_inode(inode);
1095 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
1096 return 0;
1097 if (!S_ISREG(inode->i_mode))
1098 return 0;
1100 mutex_lock(&ui->ui_mutex);
1101 spin_lock(&ui->ui_lock);
1102 if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
1103 ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode "
1104 "is clean", ui->ui_size, ui->synced_i_size);
1105 ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
1106 inode->i_mode, i_size_read(inode));
1107 dbg_dump_stack();
1108 err = -EINVAL;
1110 spin_unlock(&ui->ui_lock);
1111 mutex_unlock(&ui->ui_mutex);
1112 return err;
1116 * dbg_check_dir - check directory inode size and link count.
1117 * @c: UBIFS file-system description object
1118 * @dir: the directory to calculate size for
1119 * @size: the result is returned here
1121 * This function makes sure that directory size and link count are correct.
1122 * Returns zero in case of success and a negative error code in case of
1123 * failure.
1125 * Note, it is good idea to make sure the @dir->i_mutex is locked before
1126 * calling this function.
1128 int dbg_check_dir_size(struct ubifs_info *c, const struct inode *dir)
1130 unsigned int nlink = 2;
1131 union ubifs_key key;
1132 struct ubifs_dent_node *dent, *pdent = NULL;
1133 struct qstr nm = { .name = NULL };
1134 loff_t size = UBIFS_INO_NODE_SZ;
1136 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
1137 return 0;
1139 if (!S_ISDIR(dir->i_mode))
1140 return 0;
1142 lowest_dent_key(c, &key, dir->i_ino);
1143 while (1) {
1144 int err;
1146 dent = ubifs_tnc_next_ent(c, &key, &nm);
1147 if (IS_ERR(dent)) {
1148 err = PTR_ERR(dent);
1149 if (err == -ENOENT)
1150 break;
1151 return err;
1154 nm.name = dent->name;
1155 nm.len = le16_to_cpu(dent->nlen);
1156 size += CALC_DENT_SIZE(nm.len);
1157 if (dent->type == UBIFS_ITYPE_DIR)
1158 nlink += 1;
1159 kfree(pdent);
1160 pdent = dent;
1161 key_read(c, &dent->key, &key);
1163 kfree(pdent);
1165 if (i_size_read(dir) != size) {
1166 ubifs_err("directory inode %lu has size %llu, "
1167 "but calculated size is %llu", dir->i_ino,
1168 (unsigned long long)i_size_read(dir),
1169 (unsigned long long)size);
1170 dump_stack();
1171 return -EINVAL;
1173 if (dir->i_nlink != nlink) {
1174 ubifs_err("directory inode %lu has nlink %u, but calculated "
1175 "nlink is %u", dir->i_ino, dir->i_nlink, nlink);
1176 dump_stack();
1177 return -EINVAL;
1180 return 0;
1184 * dbg_check_key_order - make sure that colliding keys are properly ordered.
1185 * @c: UBIFS file-system description object
1186 * @zbr1: first zbranch
1187 * @zbr2: following zbranch
1189 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
1190 * names of the direntries/xentries which are referred by the keys. This
1191 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
1192 * sure the name of direntry/xentry referred by @zbr1 is less than
1193 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
1194 * and a negative error code in case of failure.
1196 static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
1197 struct ubifs_zbranch *zbr2)
1199 int err, nlen1, nlen2, cmp;
1200 struct ubifs_dent_node *dent1, *dent2;
1201 union ubifs_key key;
1203 ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
1204 dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1205 if (!dent1)
1206 return -ENOMEM;
1207 dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1208 if (!dent2) {
1209 err = -ENOMEM;
1210 goto out_free;
1213 err = ubifs_tnc_read_node(c, zbr1, dent1);
1214 if (err)
1215 goto out_free;
1216 err = ubifs_validate_entry(c, dent1);
1217 if (err)
1218 goto out_free;
1220 err = ubifs_tnc_read_node(c, zbr2, dent2);
1221 if (err)
1222 goto out_free;
1223 err = ubifs_validate_entry(c, dent2);
1224 if (err)
1225 goto out_free;
1227 /* Make sure node keys are the same as in zbranch */
1228 err = 1;
1229 key_read(c, &dent1->key, &key);
1230 if (keys_cmp(c, &zbr1->key, &key)) {
1231 dbg_err("1st entry at %d:%d has key %s", zbr1->lnum,
1232 zbr1->offs, DBGKEY(&key));
1233 dbg_err("but it should have key %s according to tnc",
1234 DBGKEY(&zbr1->key));
1235 dbg_dump_node(c, dent1);
1236 goto out_free;
1239 key_read(c, &dent2->key, &key);
1240 if (keys_cmp(c, &zbr2->key, &key)) {
1241 dbg_err("2nd entry at %d:%d has key %s", zbr1->lnum,
1242 zbr1->offs, DBGKEY(&key));
1243 dbg_err("but it should have key %s according to tnc",
1244 DBGKEY(&zbr2->key));
1245 dbg_dump_node(c, dent2);
1246 goto out_free;
1249 nlen1 = le16_to_cpu(dent1->nlen);
1250 nlen2 = le16_to_cpu(dent2->nlen);
1252 cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
1253 if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
1254 err = 0;
1255 goto out_free;
1257 if (cmp == 0 && nlen1 == nlen2)
1258 dbg_err("2 xent/dent nodes with the same name");
1259 else
1260 dbg_err("bad order of colliding key %s",
1261 DBGKEY(&key));
1263 ubifs_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs);
1264 dbg_dump_node(c, dent1);
1265 ubifs_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs);
1266 dbg_dump_node(c, dent2);
1268 out_free:
1269 kfree(dent2);
1270 kfree(dent1);
1271 return err;
1275 * dbg_check_znode - check if znode is all right.
1276 * @c: UBIFS file-system description object
1277 * @zbr: zbranch which points to this znode
1279 * This function makes sure that znode referred to by @zbr is all right.
1280 * Returns zero if it is, and %-EINVAL if it is not.
1282 static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
1284 struct ubifs_znode *znode = zbr->znode;
1285 struct ubifs_znode *zp = znode->parent;
1286 int n, err, cmp;
1288 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
1289 err = 1;
1290 goto out;
1292 if (znode->level < 0) {
1293 err = 2;
1294 goto out;
1296 if (znode->iip < 0 || znode->iip >= c->fanout) {
1297 err = 3;
1298 goto out;
1301 if (zbr->len == 0)
1302 /* Only dirty zbranch may have no on-flash nodes */
1303 if (!ubifs_zn_dirty(znode)) {
1304 err = 4;
1305 goto out;
1308 if (ubifs_zn_dirty(znode)) {
1310 * If znode is dirty, its parent has to be dirty as well. The
1311 * order of the operation is important, so we have to have
1312 * memory barriers.
1314 smp_mb();
1315 if (zp && !ubifs_zn_dirty(zp)) {
1317 * The dirty flag is atomic and is cleared outside the
1318 * TNC mutex, so znode's dirty flag may now have
1319 * been cleared. The child is always cleared before the
1320 * parent, so we just need to check again.
1322 smp_mb();
1323 if (ubifs_zn_dirty(znode)) {
1324 err = 5;
1325 goto out;
1330 if (zp) {
1331 const union ubifs_key *min, *max;
1333 if (znode->level != zp->level - 1) {
1334 err = 6;
1335 goto out;
1338 /* Make sure the 'parent' pointer in our znode is correct */
1339 err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
1340 if (!err) {
1341 /* This zbranch does not exist in the parent */
1342 err = 7;
1343 goto out;
1346 if (znode->iip >= zp->child_cnt) {
1347 err = 8;
1348 goto out;
1351 if (znode->iip != n) {
1352 /* This may happen only in case of collisions */
1353 if (keys_cmp(c, &zp->zbranch[n].key,
1354 &zp->zbranch[znode->iip].key)) {
1355 err = 9;
1356 goto out;
1358 n = znode->iip;
1362 * Make sure that the first key in our znode is greater than or
1363 * equal to the key in the pointing zbranch.
1365 min = &zbr->key;
1366 cmp = keys_cmp(c, min, &znode->zbranch[0].key);
1367 if (cmp == 1) {
1368 err = 10;
1369 goto out;
1372 if (n + 1 < zp->child_cnt) {
1373 max = &zp->zbranch[n + 1].key;
1376 * Make sure the last key in our znode is less or
1377 * equivalent than the key in the zbranch which goes
1378 * after our pointing zbranch.
1380 cmp = keys_cmp(c, max,
1381 &znode->zbranch[znode->child_cnt - 1].key);
1382 if (cmp == -1) {
1383 err = 11;
1384 goto out;
1387 } else {
1388 /* This may only be root znode */
1389 if (zbr != &c->zroot) {
1390 err = 12;
1391 goto out;
1396 * Make sure that next key is greater or equivalent then the previous
1397 * one.
1399 for (n = 1; n < znode->child_cnt; n++) {
1400 cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
1401 &znode->zbranch[n].key);
1402 if (cmp > 0) {
1403 err = 13;
1404 goto out;
1406 if (cmp == 0) {
1407 /* This can only be keys with colliding hash */
1408 if (!is_hash_key(c, &znode->zbranch[n].key)) {
1409 err = 14;
1410 goto out;
1413 if (znode->level != 0 || c->replaying)
1414 continue;
1417 * Colliding keys should follow binary order of
1418 * corresponding xentry/dentry names.
1420 err = dbg_check_key_order(c, &znode->zbranch[n - 1],
1421 &znode->zbranch[n]);
1422 if (err < 0)
1423 return err;
1424 if (err) {
1425 err = 15;
1426 goto out;
1431 for (n = 0; n < znode->child_cnt; n++) {
1432 if (!znode->zbranch[n].znode &&
1433 (znode->zbranch[n].lnum == 0 ||
1434 znode->zbranch[n].len == 0)) {
1435 err = 16;
1436 goto out;
1439 if (znode->zbranch[n].lnum != 0 &&
1440 znode->zbranch[n].len == 0) {
1441 err = 17;
1442 goto out;
1445 if (znode->zbranch[n].lnum == 0 &&
1446 znode->zbranch[n].len != 0) {
1447 err = 18;
1448 goto out;
1451 if (znode->zbranch[n].lnum == 0 &&
1452 znode->zbranch[n].offs != 0) {
1453 err = 19;
1454 goto out;
1457 if (znode->level != 0 && znode->zbranch[n].znode)
1458 if (znode->zbranch[n].znode->parent != znode) {
1459 err = 20;
1460 goto out;
1464 return 0;
1466 out:
1467 ubifs_err("failed, error %d", err);
1468 ubifs_msg("dump of the znode");
1469 dbg_dump_znode(c, znode);
1470 if (zp) {
1471 ubifs_msg("dump of the parent znode");
1472 dbg_dump_znode(c, zp);
1474 dump_stack();
1475 return -EINVAL;
1479 * dbg_check_tnc - check TNC tree.
1480 * @c: UBIFS file-system description object
1481 * @extra: do extra checks that are possible at start commit
1483 * This function traverses whole TNC tree and checks every znode. Returns zero
1484 * if everything is all right and %-EINVAL if something is wrong with TNC.
1486 int dbg_check_tnc(struct ubifs_info *c, int extra)
1488 struct ubifs_znode *znode;
1489 long clean_cnt = 0, dirty_cnt = 0;
1490 int err, last;
1492 if (!(ubifs_chk_flags & UBIFS_CHK_TNC))
1493 return 0;
1495 ubifs_assert(mutex_is_locked(&c->tnc_mutex));
1496 if (!c->zroot.znode)
1497 return 0;
1499 znode = ubifs_tnc_postorder_first(c->zroot.znode);
1500 while (1) {
1501 struct ubifs_znode *prev;
1502 struct ubifs_zbranch *zbr;
1504 if (!znode->parent)
1505 zbr = &c->zroot;
1506 else
1507 zbr = &znode->parent->zbranch[znode->iip];
1509 err = dbg_check_znode(c, zbr);
1510 if (err)
1511 return err;
1513 if (extra) {
1514 if (ubifs_zn_dirty(znode))
1515 dirty_cnt += 1;
1516 else
1517 clean_cnt += 1;
1520 prev = znode;
1521 znode = ubifs_tnc_postorder_next(znode);
1522 if (!znode)
1523 break;
1526 * If the last key of this znode is equivalent to the first key
1527 * of the next znode (collision), then check order of the keys.
1529 last = prev->child_cnt - 1;
1530 if (prev->level == 0 && znode->level == 0 && !c->replaying &&
1531 !keys_cmp(c, &prev->zbranch[last].key,
1532 &znode->zbranch[0].key)) {
1533 err = dbg_check_key_order(c, &prev->zbranch[last],
1534 &znode->zbranch[0]);
1535 if (err < 0)
1536 return err;
1537 if (err) {
1538 ubifs_msg("first znode");
1539 dbg_dump_znode(c, prev);
1540 ubifs_msg("second znode");
1541 dbg_dump_znode(c, znode);
1542 return -EINVAL;
1547 if (extra) {
1548 if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
1549 ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld",
1550 atomic_long_read(&c->clean_zn_cnt),
1551 clean_cnt);
1552 return -EINVAL;
1554 if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
1555 ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld",
1556 atomic_long_read(&c->dirty_zn_cnt),
1557 dirty_cnt);
1558 return -EINVAL;
1562 return 0;
1566 * dbg_walk_index - walk the on-flash index.
1567 * @c: UBIFS file-system description object
1568 * @leaf_cb: called for each leaf node
1569 * @znode_cb: called for each indexing node
1570 * @priv: private data which is passed to callbacks
1572 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1573 * node and @znode_cb for each indexing node. Returns zero in case of success
1574 * and a negative error code in case of failure.
1576 * It would be better if this function removed every znode it pulled to into
1577 * the TNC, so that the behavior more closely matched the non-debugging
1578 * behavior.
1580 int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1581 dbg_znode_callback znode_cb, void *priv)
1583 int err;
1584 struct ubifs_zbranch *zbr;
1585 struct ubifs_znode *znode, *child;
1587 mutex_lock(&c->tnc_mutex);
1588 /* If the root indexing node is not in TNC - pull it */
1589 if (!c->zroot.znode) {
1590 c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1591 if (IS_ERR(c->zroot.znode)) {
1592 err = PTR_ERR(c->zroot.znode);
1593 c->zroot.znode = NULL;
1594 goto out_unlock;
1599 * We are going to traverse the indexing tree in the postorder manner.
1600 * Go down and find the leftmost indexing node where we are going to
1601 * start from.
1603 znode = c->zroot.znode;
1604 while (znode->level > 0) {
1605 zbr = &znode->zbranch[0];
1606 child = zbr->znode;
1607 if (!child) {
1608 child = ubifs_load_znode(c, zbr, znode, 0);
1609 if (IS_ERR(child)) {
1610 err = PTR_ERR(child);
1611 goto out_unlock;
1613 zbr->znode = child;
1616 znode = child;
1619 /* Iterate over all indexing nodes */
1620 while (1) {
1621 int idx;
1623 cond_resched();
1625 if (znode_cb) {
1626 err = znode_cb(c, znode, priv);
1627 if (err) {
1628 ubifs_err("znode checking function returned "
1629 "error %d", err);
1630 dbg_dump_znode(c, znode);
1631 goto out_dump;
1634 if (leaf_cb && znode->level == 0) {
1635 for (idx = 0; idx < znode->child_cnt; idx++) {
1636 zbr = &znode->zbranch[idx];
1637 err = leaf_cb(c, zbr, priv);
1638 if (err) {
1639 ubifs_err("leaf checking function "
1640 "returned error %d, for leaf "
1641 "at LEB %d:%d",
1642 err, zbr->lnum, zbr->offs);
1643 goto out_dump;
1648 if (!znode->parent)
1649 break;
1651 idx = znode->iip + 1;
1652 znode = znode->parent;
1653 if (idx < znode->child_cnt) {
1654 /* Switch to the next index in the parent */
1655 zbr = &znode->zbranch[idx];
1656 child = zbr->znode;
1657 if (!child) {
1658 child = ubifs_load_znode(c, zbr, znode, idx);
1659 if (IS_ERR(child)) {
1660 err = PTR_ERR(child);
1661 goto out_unlock;
1663 zbr->znode = child;
1665 znode = child;
1666 } else
1668 * This is the last child, switch to the parent and
1669 * continue.
1671 continue;
1673 /* Go to the lowest leftmost znode in the new sub-tree */
1674 while (znode->level > 0) {
1675 zbr = &znode->zbranch[0];
1676 child = zbr->znode;
1677 if (!child) {
1678 child = ubifs_load_znode(c, zbr, znode, 0);
1679 if (IS_ERR(child)) {
1680 err = PTR_ERR(child);
1681 goto out_unlock;
1683 zbr->znode = child;
1685 znode = child;
1689 mutex_unlock(&c->tnc_mutex);
1690 return 0;
1692 out_dump:
1693 if (znode->parent)
1694 zbr = &znode->parent->zbranch[znode->iip];
1695 else
1696 zbr = &c->zroot;
1697 ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1698 dbg_dump_znode(c, znode);
1699 out_unlock:
1700 mutex_unlock(&c->tnc_mutex);
1701 return err;
1705 * add_size - add znode size to partially calculated index size.
1706 * @c: UBIFS file-system description object
1707 * @znode: znode to add size for
1708 * @priv: partially calculated index size
1710 * This is a helper function for 'dbg_check_idx_size()' which is called for
1711 * every indexing node and adds its size to the 'long long' variable pointed to
1712 * by @priv.
1714 static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1716 long long *idx_size = priv;
1717 int add;
1719 add = ubifs_idx_node_sz(c, znode->child_cnt);
1720 add = ALIGN(add, 8);
1721 *idx_size += add;
1722 return 0;
1726 * dbg_check_idx_size - check index size.
1727 * @c: UBIFS file-system description object
1728 * @idx_size: size to check
1730 * This function walks the UBIFS index, calculates its size and checks that the
1731 * size is equivalent to @idx_size. Returns zero in case of success and a
1732 * negative error code in case of failure.
1734 int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
1736 int err;
1737 long long calc = 0;
1739 if (!(ubifs_chk_flags & UBIFS_CHK_IDX_SZ))
1740 return 0;
1742 err = dbg_walk_index(c, NULL, add_size, &calc);
1743 if (err) {
1744 ubifs_err("error %d while walking the index", err);
1745 return err;
1748 if (calc != idx_size) {
1749 ubifs_err("index size check failed: calculated size is %lld, "
1750 "should be %lld", calc, idx_size);
1751 dump_stack();
1752 return -EINVAL;
1755 return 0;
1759 * struct fsck_inode - information about an inode used when checking the file-system.
1760 * @rb: link in the RB-tree of inodes
1761 * @inum: inode number
1762 * @mode: inode type, permissions, etc
1763 * @nlink: inode link count
1764 * @xattr_cnt: count of extended attributes
1765 * @references: how many directory/xattr entries refer this inode (calculated
1766 * while walking the index)
1767 * @calc_cnt: for directory inode count of child directories
1768 * @size: inode size (read from on-flash inode)
1769 * @xattr_sz: summary size of all extended attributes (read from on-flash
1770 * inode)
1771 * @calc_sz: for directories calculated directory size
1772 * @calc_xcnt: count of extended attributes
1773 * @calc_xsz: calculated summary size of all extended attributes
1774 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1775 * inode (read from on-flash inode)
1776 * @calc_xnms: calculated sum of lengths of all extended attribute names
1778 struct fsck_inode {
1779 struct rb_node rb;
1780 ino_t inum;
1781 umode_t mode;
1782 unsigned int nlink;
1783 unsigned int xattr_cnt;
1784 int references;
1785 int calc_cnt;
1786 long long size;
1787 unsigned int xattr_sz;
1788 long long calc_sz;
1789 long long calc_xcnt;
1790 long long calc_xsz;
1791 unsigned int xattr_nms;
1792 long long calc_xnms;
1796 * struct fsck_data - private FS checking information.
1797 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1799 struct fsck_data {
1800 struct rb_root inodes;
1804 * add_inode - add inode information to RB-tree of inodes.
1805 * @c: UBIFS file-system description object
1806 * @fsckd: FS checking information
1807 * @ino: raw UBIFS inode to add
1809 * This is a helper function for 'check_leaf()' which adds information about
1810 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1811 * case of success and a negative error code in case of failure.
1813 static struct fsck_inode *add_inode(struct ubifs_info *c,
1814 struct fsck_data *fsckd,
1815 struct ubifs_ino_node *ino)
1817 struct rb_node **p, *parent = NULL;
1818 struct fsck_inode *fscki;
1819 ino_t inum = key_inum_flash(c, &ino->key);
1820 struct inode *inode;
1821 struct ubifs_inode *ui;
1823 p = &fsckd->inodes.rb_node;
1824 while (*p) {
1825 parent = *p;
1826 fscki = rb_entry(parent, struct fsck_inode, rb);
1827 if (inum < fscki->inum)
1828 p = &(*p)->rb_left;
1829 else if (inum > fscki->inum)
1830 p = &(*p)->rb_right;
1831 else
1832 return fscki;
1835 if (inum > c->highest_inum) {
1836 ubifs_err("too high inode number, max. is %lu",
1837 (unsigned long)c->highest_inum);
1838 return ERR_PTR(-EINVAL);
1841 fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
1842 if (!fscki)
1843 return ERR_PTR(-ENOMEM);
1845 inode = ilookup(c->vfs_sb, inum);
1847 fscki->inum = inum;
1849 * If the inode is present in the VFS inode cache, use it instead of
1850 * the on-flash inode which might be out-of-date. E.g., the size might
1851 * be out-of-date. If we do not do this, the following may happen, for
1852 * example:
1853 * 1. A power cut happens
1854 * 2. We mount the file-system R/O, the replay process fixes up the
1855 * inode size in the VFS cache, but on on-flash.
1856 * 3. 'check_leaf()' fails because it hits a data node beyond inode
1857 * size.
1859 if (!inode) {
1860 fscki->nlink = le32_to_cpu(ino->nlink);
1861 fscki->size = le64_to_cpu(ino->size);
1862 fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
1863 fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
1864 fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
1865 fscki->mode = le32_to_cpu(ino->mode);
1866 } else {
1867 ui = ubifs_inode(inode);
1868 fscki->nlink = inode->i_nlink;
1869 fscki->size = inode->i_size;
1870 fscki->xattr_cnt = ui->xattr_cnt;
1871 fscki->xattr_sz = ui->xattr_size;
1872 fscki->xattr_nms = ui->xattr_names;
1873 fscki->mode = inode->i_mode;
1874 iput(inode);
1877 if (S_ISDIR(fscki->mode)) {
1878 fscki->calc_sz = UBIFS_INO_NODE_SZ;
1879 fscki->calc_cnt = 2;
1882 rb_link_node(&fscki->rb, parent, p);
1883 rb_insert_color(&fscki->rb, &fsckd->inodes);
1885 return fscki;
1889 * search_inode - search inode in the RB-tree of inodes.
1890 * @fsckd: FS checking information
1891 * @inum: inode number to search
1893 * This is a helper function for 'check_leaf()' which searches inode @inum in
1894 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1895 * the inode was not found.
1897 static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
1899 struct rb_node *p;
1900 struct fsck_inode *fscki;
1902 p = fsckd->inodes.rb_node;
1903 while (p) {
1904 fscki = rb_entry(p, struct fsck_inode, rb);
1905 if (inum < fscki->inum)
1906 p = p->rb_left;
1907 else if (inum > fscki->inum)
1908 p = p->rb_right;
1909 else
1910 return fscki;
1912 return NULL;
1916 * read_add_inode - read inode node and add it to RB-tree of inodes.
1917 * @c: UBIFS file-system description object
1918 * @fsckd: FS checking information
1919 * @inum: inode number to read
1921 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1922 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1923 * information pointer in case of success and a negative error code in case of
1924 * failure.
1926 static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1927 struct fsck_data *fsckd, ino_t inum)
1929 int n, err;
1930 union ubifs_key key;
1931 struct ubifs_znode *znode;
1932 struct ubifs_zbranch *zbr;
1933 struct ubifs_ino_node *ino;
1934 struct fsck_inode *fscki;
1936 fscki = search_inode(fsckd, inum);
1937 if (fscki)
1938 return fscki;
1940 ino_key_init(c, &key, inum);
1941 err = ubifs_lookup_level0(c, &key, &znode, &n);
1942 if (!err) {
1943 ubifs_err("inode %lu not found in index", (unsigned long)inum);
1944 return ERR_PTR(-ENOENT);
1945 } else if (err < 0) {
1946 ubifs_err("error %d while looking up inode %lu",
1947 err, (unsigned long)inum);
1948 return ERR_PTR(err);
1951 zbr = &znode->zbranch[n];
1952 if (zbr->len < UBIFS_INO_NODE_SZ) {
1953 ubifs_err("bad node %lu node length %d",
1954 (unsigned long)inum, zbr->len);
1955 return ERR_PTR(-EINVAL);
1958 ino = kmalloc(zbr->len, GFP_NOFS);
1959 if (!ino)
1960 return ERR_PTR(-ENOMEM);
1962 err = ubifs_tnc_read_node(c, zbr, ino);
1963 if (err) {
1964 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
1965 zbr->lnum, zbr->offs, err);
1966 kfree(ino);
1967 return ERR_PTR(err);
1970 fscki = add_inode(c, fsckd, ino);
1971 kfree(ino);
1972 if (IS_ERR(fscki)) {
1973 ubifs_err("error %ld while adding inode %lu node",
1974 PTR_ERR(fscki), (unsigned long)inum);
1975 return fscki;
1978 return fscki;
1982 * check_leaf - check leaf node.
1983 * @c: UBIFS file-system description object
1984 * @zbr: zbranch of the leaf node to check
1985 * @priv: FS checking information
1987 * This is a helper function for 'dbg_check_filesystem()' which is called for
1988 * every single leaf node while walking the indexing tree. It checks that the
1989 * leaf node referred from the indexing tree exists, has correct CRC, and does
1990 * some other basic validation. This function is also responsible for building
1991 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
1992 * calculates reference count, size, etc for each inode in order to later
1993 * compare them to the information stored inside the inodes and detect possible
1994 * inconsistencies. Returns zero in case of success and a negative error code
1995 * in case of failure.
1997 static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
1998 void *priv)
2000 ino_t inum;
2001 void *node;
2002 struct ubifs_ch *ch;
2003 int err, type = key_type(c, &zbr->key);
2004 struct fsck_inode *fscki;
2006 if (zbr->len < UBIFS_CH_SZ) {
2007 ubifs_err("bad leaf length %d (LEB %d:%d)",
2008 zbr->len, zbr->lnum, zbr->offs);
2009 return -EINVAL;
2012 node = kmalloc(zbr->len, GFP_NOFS);
2013 if (!node)
2014 return -ENOMEM;
2016 err = ubifs_tnc_read_node(c, zbr, node);
2017 if (err) {
2018 ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
2019 zbr->lnum, zbr->offs, err);
2020 goto out_free;
2023 /* If this is an inode node, add it to RB-tree of inodes */
2024 if (type == UBIFS_INO_KEY) {
2025 fscki = add_inode(c, priv, node);
2026 if (IS_ERR(fscki)) {
2027 err = PTR_ERR(fscki);
2028 ubifs_err("error %d while adding inode node", err);
2029 goto out_dump;
2031 goto out;
2034 if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
2035 type != UBIFS_DATA_KEY) {
2036 ubifs_err("unexpected node type %d at LEB %d:%d",
2037 type, zbr->lnum, zbr->offs);
2038 err = -EINVAL;
2039 goto out_free;
2042 ch = node;
2043 if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
2044 ubifs_err("too high sequence number, max. is %llu",
2045 c->max_sqnum);
2046 err = -EINVAL;
2047 goto out_dump;
2050 if (type == UBIFS_DATA_KEY) {
2051 long long blk_offs;
2052 struct ubifs_data_node *dn = node;
2055 * Search the inode node this data node belongs to and insert
2056 * it to the RB-tree of inodes.
2058 inum = key_inum_flash(c, &dn->key);
2059 fscki = read_add_inode(c, priv, inum);
2060 if (IS_ERR(fscki)) {
2061 err = PTR_ERR(fscki);
2062 ubifs_err("error %d while processing data node and "
2063 "trying to find inode node %lu",
2064 err, (unsigned long)inum);
2065 goto out_dump;
2068 /* Make sure the data node is within inode size */
2069 blk_offs = key_block_flash(c, &dn->key);
2070 blk_offs <<= UBIFS_BLOCK_SHIFT;
2071 blk_offs += le32_to_cpu(dn->size);
2072 if (blk_offs > fscki->size) {
2073 ubifs_err("data node at LEB %d:%d is not within inode "
2074 "size %lld", zbr->lnum, zbr->offs,
2075 fscki->size);
2076 err = -EINVAL;
2077 goto out_dump;
2079 } else {
2080 int nlen;
2081 struct ubifs_dent_node *dent = node;
2082 struct fsck_inode *fscki1;
2084 err = ubifs_validate_entry(c, dent);
2085 if (err)
2086 goto out_dump;
2089 * Search the inode node this entry refers to and the parent
2090 * inode node and insert them to the RB-tree of inodes.
2092 inum = le64_to_cpu(dent->inum);
2093 fscki = read_add_inode(c, priv, inum);
2094 if (IS_ERR(fscki)) {
2095 err = PTR_ERR(fscki);
2096 ubifs_err("error %d while processing entry node and "
2097 "trying to find inode node %lu",
2098 err, (unsigned long)inum);
2099 goto out_dump;
2102 /* Count how many direntries or xentries refers this inode */
2103 fscki->references += 1;
2105 inum = key_inum_flash(c, &dent->key);
2106 fscki1 = read_add_inode(c, priv, inum);
2107 if (IS_ERR(fscki1)) {
2108 err = PTR_ERR(fscki1);
2109 ubifs_err("error %d while processing entry node and "
2110 "trying to find parent inode node %lu",
2111 err, (unsigned long)inum);
2112 goto out_dump;
2115 nlen = le16_to_cpu(dent->nlen);
2116 if (type == UBIFS_XENT_KEY) {
2117 fscki1->calc_xcnt += 1;
2118 fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
2119 fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
2120 fscki1->calc_xnms += nlen;
2121 } else {
2122 fscki1->calc_sz += CALC_DENT_SIZE(nlen);
2123 if (dent->type == UBIFS_ITYPE_DIR)
2124 fscki1->calc_cnt += 1;
2128 out:
2129 kfree(node);
2130 return 0;
2132 out_dump:
2133 ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
2134 dbg_dump_node(c, node);
2135 out_free:
2136 kfree(node);
2137 return err;
2141 * free_inodes - free RB-tree of inodes.
2142 * @fsckd: FS checking information
2144 static void free_inodes(struct fsck_data *fsckd)
2146 struct rb_node *this = fsckd->inodes.rb_node;
2147 struct fsck_inode *fscki;
2149 while (this) {
2150 if (this->rb_left)
2151 this = this->rb_left;
2152 else if (this->rb_right)
2153 this = this->rb_right;
2154 else {
2155 fscki = rb_entry(this, struct fsck_inode, rb);
2156 this = rb_parent(this);
2157 if (this) {
2158 if (this->rb_left == &fscki->rb)
2159 this->rb_left = NULL;
2160 else
2161 this->rb_right = NULL;
2163 kfree(fscki);
2169 * check_inodes - checks all inodes.
2170 * @c: UBIFS file-system description object
2171 * @fsckd: FS checking information
2173 * This is a helper function for 'dbg_check_filesystem()' which walks the
2174 * RB-tree of inodes after the index scan has been finished, and checks that
2175 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
2176 * %-EINVAL if not, and a negative error code in case of failure.
2178 static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
2180 int n, err;
2181 union ubifs_key key;
2182 struct ubifs_znode *znode;
2183 struct ubifs_zbranch *zbr;
2184 struct ubifs_ino_node *ino;
2185 struct fsck_inode *fscki;
2186 struct rb_node *this = rb_first(&fsckd->inodes);
2188 while (this) {
2189 fscki = rb_entry(this, struct fsck_inode, rb);
2190 this = rb_next(this);
2192 if (S_ISDIR(fscki->mode)) {
2194 * Directories have to have exactly one reference (they
2195 * cannot have hardlinks), although root inode is an
2196 * exception.
2198 if (fscki->inum != UBIFS_ROOT_INO &&
2199 fscki->references != 1) {
2200 ubifs_err("directory inode %lu has %d "
2201 "direntries which refer it, but "
2202 "should be 1",
2203 (unsigned long)fscki->inum,
2204 fscki->references);
2205 goto out_dump;
2207 if (fscki->inum == UBIFS_ROOT_INO &&
2208 fscki->references != 0) {
2209 ubifs_err("root inode %lu has non-zero (%d) "
2210 "direntries which refer it",
2211 (unsigned long)fscki->inum,
2212 fscki->references);
2213 goto out_dump;
2215 if (fscki->calc_sz != fscki->size) {
2216 ubifs_err("directory inode %lu size is %lld, "
2217 "but calculated size is %lld",
2218 (unsigned long)fscki->inum,
2219 fscki->size, fscki->calc_sz);
2220 goto out_dump;
2222 if (fscki->calc_cnt != fscki->nlink) {
2223 ubifs_err("directory inode %lu nlink is %d, "
2224 "but calculated nlink is %d",
2225 (unsigned long)fscki->inum,
2226 fscki->nlink, fscki->calc_cnt);
2227 goto out_dump;
2229 } else {
2230 if (fscki->references != fscki->nlink) {
2231 ubifs_err("inode %lu nlink is %d, but "
2232 "calculated nlink is %d",
2233 (unsigned long)fscki->inum,
2234 fscki->nlink, fscki->references);
2235 goto out_dump;
2238 if (fscki->xattr_sz != fscki->calc_xsz) {
2239 ubifs_err("inode %lu has xattr size %u, but "
2240 "calculated size is %lld",
2241 (unsigned long)fscki->inum, fscki->xattr_sz,
2242 fscki->calc_xsz);
2243 goto out_dump;
2245 if (fscki->xattr_cnt != fscki->calc_xcnt) {
2246 ubifs_err("inode %lu has %u xattrs, but "
2247 "calculated count is %lld",
2248 (unsigned long)fscki->inum,
2249 fscki->xattr_cnt, fscki->calc_xcnt);
2250 goto out_dump;
2252 if (fscki->xattr_nms != fscki->calc_xnms) {
2253 ubifs_err("inode %lu has xattr names' size %u, but "
2254 "calculated names' size is %lld",
2255 (unsigned long)fscki->inum, fscki->xattr_nms,
2256 fscki->calc_xnms);
2257 goto out_dump;
2261 return 0;
2263 out_dump:
2264 /* Read the bad inode and dump it */
2265 ino_key_init(c, &key, fscki->inum);
2266 err = ubifs_lookup_level0(c, &key, &znode, &n);
2267 if (!err) {
2268 ubifs_err("inode %lu not found in index",
2269 (unsigned long)fscki->inum);
2270 return -ENOENT;
2271 } else if (err < 0) {
2272 ubifs_err("error %d while looking up inode %lu",
2273 err, (unsigned long)fscki->inum);
2274 return err;
2277 zbr = &znode->zbranch[n];
2278 ino = kmalloc(zbr->len, GFP_NOFS);
2279 if (!ino)
2280 return -ENOMEM;
2282 err = ubifs_tnc_read_node(c, zbr, ino);
2283 if (err) {
2284 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2285 zbr->lnum, zbr->offs, err);
2286 kfree(ino);
2287 return err;
2290 ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",
2291 (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
2292 dbg_dump_node(c, ino);
2293 kfree(ino);
2294 return -EINVAL;
2298 * dbg_check_filesystem - check the file-system.
2299 * @c: UBIFS file-system description object
2301 * This function checks the file system, namely:
2302 * o makes sure that all leaf nodes exist and their CRCs are correct;
2303 * o makes sure inode nlink, size, xattr size/count are correct (for all
2304 * inodes).
2306 * The function reads whole indexing tree and all nodes, so it is pretty
2307 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2308 * not, and a negative error code in case of failure.
2310 int dbg_check_filesystem(struct ubifs_info *c)
2312 int err;
2313 struct fsck_data fsckd;
2315 if (!(ubifs_chk_flags & UBIFS_CHK_FS))
2316 return 0;
2318 fsckd.inodes = RB_ROOT;
2319 err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
2320 if (err)
2321 goto out_free;
2323 err = check_inodes(c, &fsckd);
2324 if (err)
2325 goto out_free;
2327 free_inodes(&fsckd);
2328 return 0;
2330 out_free:
2331 ubifs_err("file-system check failed with error %d", err);
2332 dump_stack();
2333 free_inodes(&fsckd);
2334 return err;
2338 * dbg_check_data_nodes_order - check that list of data nodes is sorted.
2339 * @c: UBIFS file-system description object
2340 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2342 * This function returns zero if the list of data nodes is sorted correctly,
2343 * and %-EINVAL if not.
2345 int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head)
2347 struct list_head *cur;
2348 struct ubifs_scan_node *sa, *sb;
2350 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
2351 return 0;
2353 for (cur = head->next; cur->next != head; cur = cur->next) {
2354 ino_t inuma, inumb;
2355 uint32_t blka, blkb;
2357 cond_resched();
2358 sa = container_of(cur, struct ubifs_scan_node, list);
2359 sb = container_of(cur->next, struct ubifs_scan_node, list);
2361 if (sa->type != UBIFS_DATA_NODE) {
2362 ubifs_err("bad node type %d", sa->type);
2363 dbg_dump_node(c, sa->node);
2364 return -EINVAL;
2366 if (sb->type != UBIFS_DATA_NODE) {
2367 ubifs_err("bad node type %d", sb->type);
2368 dbg_dump_node(c, sb->node);
2369 return -EINVAL;
2372 inuma = key_inum(c, &sa->key);
2373 inumb = key_inum(c, &sb->key);
2375 if (inuma < inumb)
2376 continue;
2377 if (inuma > inumb) {
2378 ubifs_err("larger inum %lu goes before inum %lu",
2379 (unsigned long)inuma, (unsigned long)inumb);
2380 goto error_dump;
2383 blka = key_block(c, &sa->key);
2384 blkb = key_block(c, &sb->key);
2386 if (blka > blkb) {
2387 ubifs_err("larger block %u goes before %u", blka, blkb);
2388 goto error_dump;
2390 if (blka == blkb) {
2391 ubifs_err("two data nodes for the same block");
2392 goto error_dump;
2396 return 0;
2398 error_dump:
2399 dbg_dump_node(c, sa->node);
2400 dbg_dump_node(c, sb->node);
2401 return -EINVAL;
2405 * dbg_check_nondata_nodes_order - check that list of data nodes is sorted.
2406 * @c: UBIFS file-system description object
2407 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2409 * This function returns zero if the list of non-data nodes is sorted correctly,
2410 * and %-EINVAL if not.
2412 int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head)
2414 struct list_head *cur;
2415 struct ubifs_scan_node *sa, *sb;
2417 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
2418 return 0;
2420 for (cur = head->next; cur->next != head; cur = cur->next) {
2421 ino_t inuma, inumb;
2422 uint32_t hasha, hashb;
2424 cond_resched();
2425 sa = container_of(cur, struct ubifs_scan_node, list);
2426 sb = container_of(cur->next, struct ubifs_scan_node, list);
2428 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2429 sa->type != UBIFS_XENT_NODE) {
2430 ubifs_err("bad node type %d", sa->type);
2431 dbg_dump_node(c, sa->node);
2432 return -EINVAL;
2434 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2435 sa->type != UBIFS_XENT_NODE) {
2436 ubifs_err("bad node type %d", sb->type);
2437 dbg_dump_node(c, sb->node);
2438 return -EINVAL;
2441 if (sa->type != UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2442 ubifs_err("non-inode node goes before inode node");
2443 goto error_dump;
2446 if (sa->type == UBIFS_INO_NODE && sb->type != UBIFS_INO_NODE)
2447 continue;
2449 if (sa->type == UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2450 /* Inode nodes are sorted in descending size order */
2451 if (sa->len < sb->len) {
2452 ubifs_err("smaller inode node goes first");
2453 goto error_dump;
2455 continue;
2459 * This is either a dentry or xentry, which should be sorted in
2460 * ascending (parent ino, hash) order.
2462 inuma = key_inum(c, &sa->key);
2463 inumb = key_inum(c, &sb->key);
2465 if (inuma < inumb)
2466 continue;
2467 if (inuma > inumb) {
2468 ubifs_err("larger inum %lu goes before inum %lu",
2469 (unsigned long)inuma, (unsigned long)inumb);
2470 goto error_dump;
2473 hasha = key_block(c, &sa->key);
2474 hashb = key_block(c, &sb->key);
2476 if (hasha > hashb) {
2477 ubifs_err("larger hash %u goes before %u",
2478 hasha, hashb);
2479 goto error_dump;
2483 return 0;
2485 error_dump:
2486 ubifs_msg("dumping first node");
2487 dbg_dump_node(c, sa->node);
2488 ubifs_msg("dumping second node");
2489 dbg_dump_node(c, sb->node);
2490 return -EINVAL;
2491 return 0;
2494 int dbg_force_in_the_gaps(void)
2496 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
2497 return 0;
2499 return !(random32() & 7);
2502 /* Failure mode for recovery testing */
2504 #define chance(n, d) (simple_rand() <= (n) * 32768LL / (d))
2506 struct failure_mode_info {
2507 struct list_head list;
2508 struct ubifs_info *c;
2511 static LIST_HEAD(fmi_list);
2512 static DEFINE_SPINLOCK(fmi_lock);
2514 static unsigned int next;
2516 static int simple_rand(void)
2518 if (next == 0)
2519 next = current->pid;
2520 next = next * 1103515245 + 12345;
2521 return (next >> 16) & 32767;
2524 static void failure_mode_init(struct ubifs_info *c)
2526 struct failure_mode_info *fmi;
2528 fmi = kmalloc(sizeof(struct failure_mode_info), GFP_NOFS);
2529 if (!fmi) {
2530 ubifs_err("Failed to register failure mode - no memory");
2531 return;
2533 fmi->c = c;
2534 spin_lock(&fmi_lock);
2535 list_add_tail(&fmi->list, &fmi_list);
2536 spin_unlock(&fmi_lock);
2539 static void failure_mode_exit(struct ubifs_info *c)
2541 struct failure_mode_info *fmi, *tmp;
2543 spin_lock(&fmi_lock);
2544 list_for_each_entry_safe(fmi, tmp, &fmi_list, list)
2545 if (fmi->c == c) {
2546 list_del(&fmi->list);
2547 kfree(fmi);
2549 spin_unlock(&fmi_lock);
2552 static struct ubifs_info *dbg_find_info(struct ubi_volume_desc *desc)
2554 struct failure_mode_info *fmi;
2556 spin_lock(&fmi_lock);
2557 list_for_each_entry(fmi, &fmi_list, list)
2558 if (fmi->c->ubi == desc) {
2559 struct ubifs_info *c = fmi->c;
2561 spin_unlock(&fmi_lock);
2562 return c;
2564 spin_unlock(&fmi_lock);
2565 return NULL;
2568 static int in_failure_mode(struct ubi_volume_desc *desc)
2570 struct ubifs_info *c = dbg_find_info(desc);
2572 if (c && dbg_failure_mode)
2573 return c->dbg->failure_mode;
2574 return 0;
2577 static int do_fail(struct ubi_volume_desc *desc, int lnum, int write)
2579 struct ubifs_info *c = dbg_find_info(desc);
2580 struct ubifs_debug_info *d;
2582 if (!c || !dbg_failure_mode)
2583 return 0;
2584 d = c->dbg;
2585 if (d->failure_mode)
2586 return 1;
2587 if (!d->fail_cnt) {
2588 /* First call - decide delay to failure */
2589 if (chance(1, 2)) {
2590 unsigned int delay = 1 << (simple_rand() >> 11);
2592 if (chance(1, 2)) {
2593 d->fail_delay = 1;
2594 d->fail_timeout = jiffies +
2595 msecs_to_jiffies(delay);
2596 dbg_rcvry("failing after %ums", delay);
2597 } else {
2598 d->fail_delay = 2;
2599 d->fail_cnt_max = delay;
2600 dbg_rcvry("failing after %u calls", delay);
2603 d->fail_cnt += 1;
2605 /* Determine if failure delay has expired */
2606 if (d->fail_delay == 1) {
2607 if (time_before(jiffies, d->fail_timeout))
2608 return 0;
2609 } else if (d->fail_delay == 2)
2610 if (d->fail_cnt++ < d->fail_cnt_max)
2611 return 0;
2612 if (lnum == UBIFS_SB_LNUM) {
2613 if (write) {
2614 if (chance(1, 2))
2615 return 0;
2616 } else if (chance(19, 20))
2617 return 0;
2618 dbg_rcvry("failing in super block LEB %d", lnum);
2619 } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2620 if (chance(19, 20))
2621 return 0;
2622 dbg_rcvry("failing in master LEB %d", lnum);
2623 } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2624 if (write) {
2625 if (chance(99, 100))
2626 return 0;
2627 } else if (chance(399, 400))
2628 return 0;
2629 dbg_rcvry("failing in log LEB %d", lnum);
2630 } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2631 if (write) {
2632 if (chance(7, 8))
2633 return 0;
2634 } else if (chance(19, 20))
2635 return 0;
2636 dbg_rcvry("failing in LPT LEB %d", lnum);
2637 } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2638 if (write) {
2639 if (chance(1, 2))
2640 return 0;
2641 } else if (chance(9, 10))
2642 return 0;
2643 dbg_rcvry("failing in orphan LEB %d", lnum);
2644 } else if (lnum == c->ihead_lnum) {
2645 if (chance(99, 100))
2646 return 0;
2647 dbg_rcvry("failing in index head LEB %d", lnum);
2648 } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2649 if (chance(9, 10))
2650 return 0;
2651 dbg_rcvry("failing in GC head LEB %d", lnum);
2652 } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
2653 !ubifs_search_bud(c, lnum)) {
2654 if (chance(19, 20))
2655 return 0;
2656 dbg_rcvry("failing in non-bud LEB %d", lnum);
2657 } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
2658 c->cmt_state == COMMIT_RUNNING_REQUIRED) {
2659 if (chance(999, 1000))
2660 return 0;
2661 dbg_rcvry("failing in bud LEB %d commit running", lnum);
2662 } else {
2663 if (chance(9999, 10000))
2664 return 0;
2665 dbg_rcvry("failing in bud LEB %d commit not running", lnum);
2667 ubifs_err("*** SETTING FAILURE MODE ON (LEB %d) ***", lnum);
2668 d->failure_mode = 1;
2669 dump_stack();
2670 return 1;
2673 static void cut_data(const void *buf, int len)
2675 int flen, i;
2676 unsigned char *p = (void *)buf;
2678 flen = (len * (long long)simple_rand()) >> 15;
2679 for (i = flen; i < len; i++)
2680 p[i] = 0xff;
2683 int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
2684 int len, int check)
2686 if (in_failure_mode(desc))
2687 return -EROFS;
2688 return ubi_leb_read(desc, lnum, buf, offset, len, check);
2691 int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
2692 int offset, int len, int dtype)
2694 int err, failing;
2696 if (in_failure_mode(desc))
2697 return -EROFS;
2698 failing = do_fail(desc, lnum, 1);
2699 if (failing)
2700 cut_data(buf, len);
2701 err = ubi_leb_write(desc, lnum, buf, offset, len, dtype);
2702 if (err)
2703 return err;
2704 if (failing)
2705 return -EROFS;
2706 return 0;
2709 int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
2710 int len, int dtype)
2712 int err;
2714 if (do_fail(desc, lnum, 1))
2715 return -EROFS;
2716 err = ubi_leb_change(desc, lnum, buf, len, dtype);
2717 if (err)
2718 return err;
2719 if (do_fail(desc, lnum, 1))
2720 return -EROFS;
2721 return 0;
2724 int dbg_leb_erase(struct ubi_volume_desc *desc, int lnum)
2726 int err;
2728 if (do_fail(desc, lnum, 0))
2729 return -EROFS;
2730 err = ubi_leb_erase(desc, lnum);
2731 if (err)
2732 return err;
2733 if (do_fail(desc, lnum, 0))
2734 return -EROFS;
2735 return 0;
2738 int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum)
2740 int err;
2742 if (do_fail(desc, lnum, 0))
2743 return -EROFS;
2744 err = ubi_leb_unmap(desc, lnum);
2745 if (err)
2746 return err;
2747 if (do_fail(desc, lnum, 0))
2748 return -EROFS;
2749 return 0;
2752 int dbg_is_mapped(struct ubi_volume_desc *desc, int lnum)
2754 if (in_failure_mode(desc))
2755 return -EROFS;
2756 return ubi_is_mapped(desc, lnum);
2759 int dbg_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
2761 int err;
2763 if (do_fail(desc, lnum, 0))
2764 return -EROFS;
2765 err = ubi_leb_map(desc, lnum, dtype);
2766 if (err)
2767 return err;
2768 if (do_fail(desc, lnum, 0))
2769 return -EROFS;
2770 return 0;
2774 * ubifs_debugging_init - initialize UBIFS debugging.
2775 * @c: UBIFS file-system description object
2777 * This function initializes debugging-related data for the file system.
2778 * Returns zero in case of success and a negative error code in case of
2779 * failure.
2781 int ubifs_debugging_init(struct ubifs_info *c)
2783 c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
2784 if (!c->dbg)
2785 return -ENOMEM;
2787 failure_mode_init(c);
2788 return 0;
2792 * ubifs_debugging_exit - free debugging data.
2793 * @c: UBIFS file-system description object
2795 void ubifs_debugging_exit(struct ubifs_info *c)
2797 failure_mode_exit(c);
2798 kfree(c->dbg);
2802 * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
2803 * contain the stuff specific to particular file-system mounts.
2805 static struct dentry *dfs_rootdir;
2808 * dbg_debugfs_init - initialize debugfs file-system.
2810 * UBIFS uses debugfs file-system to expose various debugging knobs to
2811 * user-space. This function creates "ubifs" directory in the debugfs
2812 * file-system. Returns zero in case of success and a negative error code in
2813 * case of failure.
2815 int dbg_debugfs_init(void)
2817 dfs_rootdir = debugfs_create_dir("ubifs", NULL);
2818 if (IS_ERR(dfs_rootdir)) {
2819 int err = PTR_ERR(dfs_rootdir);
2820 ubifs_err("cannot create \"ubifs\" debugfs directory, "
2821 "error %d\n", err);
2822 return err;
2825 return 0;
2829 * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
2831 void dbg_debugfs_exit(void)
2833 debugfs_remove(dfs_rootdir);
2836 static int open_debugfs_file(struct inode *inode, struct file *file)
2838 file->private_data = inode->i_private;
2839 return nonseekable_open(inode, file);
2842 static ssize_t write_debugfs_file(struct file *file, const char __user *buf,
2843 size_t count, loff_t *ppos)
2845 struct ubifs_info *c = file->private_data;
2846 struct ubifs_debug_info *d = c->dbg;
2848 if (file->f_path.dentry == d->dfs_dump_lprops)
2849 dbg_dump_lprops(c);
2850 else if (file->f_path.dentry == d->dfs_dump_budg)
2851 dbg_dump_budg(c, &c->bi);
2852 else if (file->f_path.dentry == d->dfs_dump_tnc) {
2853 mutex_lock(&c->tnc_mutex);
2854 dbg_dump_tnc(c);
2855 mutex_unlock(&c->tnc_mutex);
2856 } else
2857 return -EINVAL;
2859 return count;
2862 static const struct file_operations dfs_fops = {
2863 .open = open_debugfs_file,
2864 .write = write_debugfs_file,
2865 .owner = THIS_MODULE,
2866 .llseek = no_llseek,
2870 * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
2871 * @c: UBIFS file-system description object
2873 * This function creates all debugfs files for this instance of UBIFS. Returns
2874 * zero in case of success and a negative error code in case of failure.
2876 * Note, the only reason we have not merged this function with the
2877 * 'ubifs_debugging_init()' function is because it is better to initialize
2878 * debugfs interfaces at the very end of the mount process, and remove them at
2879 * the very beginning of the mount process.
2881 int dbg_debugfs_init_fs(struct ubifs_info *c)
2883 int err;
2884 const char *fname;
2885 struct dentry *dent;
2886 struct ubifs_debug_info *d = c->dbg;
2888 sprintf(d->dfs_dir_name, "ubi%d_%d", c->vi.ubi_num, c->vi.vol_id);
2889 fname = d->dfs_dir_name;
2890 dent = debugfs_create_dir(fname, dfs_rootdir);
2891 if (IS_ERR_OR_NULL(dent))
2892 goto out;
2893 d->dfs_dir = dent;
2895 fname = "dump_lprops";
2896 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2897 if (IS_ERR_OR_NULL(dent))
2898 goto out_remove;
2899 d->dfs_dump_lprops = dent;
2901 fname = "dump_budg";
2902 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2903 if (IS_ERR_OR_NULL(dent))
2904 goto out_remove;
2905 d->dfs_dump_budg = dent;
2907 fname = "dump_tnc";
2908 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2909 if (IS_ERR_OR_NULL(dent))
2910 goto out_remove;
2911 d->dfs_dump_tnc = dent;
2913 return 0;
2915 out_remove:
2916 debugfs_remove_recursive(d->dfs_dir);
2917 out:
2918 err = dent ? PTR_ERR(dent) : -ENODEV;
2919 ubifs_err("cannot create \"%s\" debugfs directory, error %d\n",
2920 fname, err);
2921 return err;
2925 * dbg_debugfs_exit_fs - remove all debugfs files.
2926 * @c: UBIFS file-system description object
2928 void dbg_debugfs_exit_fs(struct ubifs_info *c)
2930 debugfs_remove_recursive(c->dbg->dfs_dir);
2933 #endif /* CONFIG_UBIFS_FS_DEBUG */