deal with races in /proc/*/{syscall,stack,personality}
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ubifs / debug.c
blobceaa1d3e0336e4fa20c8fdcd50cc4ca1dcb31007
1 /*
2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
20 * Adrian Hunter
24 * This file implements most of the debugging stuff which is compiled in only
25 * when it is enabled. But some debugging check functions are implemented in
26 * corresponding subsystem, just because they are closely related and utilize
27 * various local functions of those subsystems.
30 #define UBIFS_DBG_PRESERVE_UBI
32 #include "ubifs.h"
33 #include <linux/module.h>
34 #include <linux/moduleparam.h>
35 #include <linux/debugfs.h>
36 #include <linux/math64.h>
38 #ifdef CONFIG_UBIFS_FS_DEBUG
40 DEFINE_SPINLOCK(dbg_lock);
42 static char dbg_key_buf0[128];
43 static char dbg_key_buf1[128];
45 unsigned int ubifs_msg_flags = UBIFS_MSG_FLAGS_DEFAULT;
46 unsigned int ubifs_chk_flags = UBIFS_CHK_FLAGS_DEFAULT;
47 unsigned int ubifs_tst_flags;
49 module_param_named(debug_msgs, ubifs_msg_flags, uint, S_IRUGO | S_IWUSR);
50 module_param_named(debug_chks, ubifs_chk_flags, uint, S_IRUGO | S_IWUSR);
51 module_param_named(debug_tsts, ubifs_tst_flags, uint, S_IRUGO | S_IWUSR);
53 MODULE_PARM_DESC(debug_msgs, "Debug message type flags");
54 MODULE_PARM_DESC(debug_chks, "Debug check flags");
55 MODULE_PARM_DESC(debug_tsts, "Debug special test flags");
57 static const char *get_key_fmt(int fmt)
59 switch (fmt) {
60 case UBIFS_SIMPLE_KEY_FMT:
61 return "simple";
62 default:
63 return "unknown/invalid format";
67 static const char *get_key_hash(int hash)
69 switch (hash) {
70 case UBIFS_KEY_HASH_R5:
71 return "R5";
72 case UBIFS_KEY_HASH_TEST:
73 return "test";
74 default:
75 return "unknown/invalid name hash";
79 static const char *get_key_type(int type)
81 switch (type) {
82 case UBIFS_INO_KEY:
83 return "inode";
84 case UBIFS_DENT_KEY:
85 return "direntry";
86 case UBIFS_XENT_KEY:
87 return "xentry";
88 case UBIFS_DATA_KEY:
89 return "data";
90 case UBIFS_TRUN_KEY:
91 return "truncate";
92 default:
93 return "unknown/invalid key";
97 static void sprintf_key(const struct ubifs_info *c, const union ubifs_key *key,
98 char *buffer)
100 char *p = buffer;
101 int type = key_type(c, key);
103 if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
104 switch (type) {
105 case UBIFS_INO_KEY:
106 sprintf(p, "(%lu, %s)", (unsigned long)key_inum(c, key),
107 get_key_type(type));
108 break;
109 case UBIFS_DENT_KEY:
110 case UBIFS_XENT_KEY:
111 sprintf(p, "(%lu, %s, %#08x)",
112 (unsigned long)key_inum(c, key),
113 get_key_type(type), key_hash(c, key));
114 break;
115 case UBIFS_DATA_KEY:
116 sprintf(p, "(%lu, %s, %u)",
117 (unsigned long)key_inum(c, key),
118 get_key_type(type), key_block(c, key));
119 break;
120 case UBIFS_TRUN_KEY:
121 sprintf(p, "(%lu, %s)",
122 (unsigned long)key_inum(c, key),
123 get_key_type(type));
124 break;
125 default:
126 sprintf(p, "(bad key type: %#08x, %#08x)",
127 key->u32[0], key->u32[1]);
129 } else
130 sprintf(p, "bad key format %d", c->key_fmt);
133 const char *dbg_key_str0(const struct ubifs_info *c, const union ubifs_key *key)
135 /* dbg_lock must be held */
136 sprintf_key(c, key, dbg_key_buf0);
137 return dbg_key_buf0;
140 const char *dbg_key_str1(const struct ubifs_info *c, const union ubifs_key *key)
142 /* dbg_lock must be held */
143 sprintf_key(c, key, dbg_key_buf1);
144 return dbg_key_buf1;
147 const char *dbg_ntype(int type)
149 switch (type) {
150 case UBIFS_PAD_NODE:
151 return "padding node";
152 case UBIFS_SB_NODE:
153 return "superblock node";
154 case UBIFS_MST_NODE:
155 return "master node";
156 case UBIFS_REF_NODE:
157 return "reference node";
158 case UBIFS_INO_NODE:
159 return "inode node";
160 case UBIFS_DENT_NODE:
161 return "direntry node";
162 case UBIFS_XENT_NODE:
163 return "xentry node";
164 case UBIFS_DATA_NODE:
165 return "data node";
166 case UBIFS_TRUN_NODE:
167 return "truncate node";
168 case UBIFS_IDX_NODE:
169 return "indexing node";
170 case UBIFS_CS_NODE:
171 return "commit start node";
172 case UBIFS_ORPH_NODE:
173 return "orphan node";
174 default:
175 return "unknown node";
179 static const char *dbg_gtype(int type)
181 switch (type) {
182 case UBIFS_NO_NODE_GROUP:
183 return "no node group";
184 case UBIFS_IN_NODE_GROUP:
185 return "in node group";
186 case UBIFS_LAST_OF_NODE_GROUP:
187 return "last of node group";
188 default:
189 return "unknown";
193 const char *dbg_cstate(int cmt_state)
195 switch (cmt_state) {
196 case COMMIT_RESTING:
197 return "commit resting";
198 case COMMIT_BACKGROUND:
199 return "background commit requested";
200 case COMMIT_REQUIRED:
201 return "commit required";
202 case COMMIT_RUNNING_BACKGROUND:
203 return "BACKGROUND commit running";
204 case COMMIT_RUNNING_REQUIRED:
205 return "commit running and required";
206 case COMMIT_BROKEN:
207 return "broken commit";
208 default:
209 return "unknown commit state";
213 const char *dbg_jhead(int jhead)
215 switch (jhead) {
216 case GCHD:
217 return "0 (GC)";
218 case BASEHD:
219 return "1 (base)";
220 case DATAHD:
221 return "2 (data)";
222 default:
223 return "unknown journal head";
227 static void dump_ch(const struct ubifs_ch *ch)
229 printk(KERN_DEBUG "\tmagic %#x\n", le32_to_cpu(ch->magic));
230 printk(KERN_DEBUG "\tcrc %#x\n", le32_to_cpu(ch->crc));
231 printk(KERN_DEBUG "\tnode_type %d (%s)\n", ch->node_type,
232 dbg_ntype(ch->node_type));
233 printk(KERN_DEBUG "\tgroup_type %d (%s)\n", ch->group_type,
234 dbg_gtype(ch->group_type));
235 printk(KERN_DEBUG "\tsqnum %llu\n",
236 (unsigned long long)le64_to_cpu(ch->sqnum));
237 printk(KERN_DEBUG "\tlen %u\n", le32_to_cpu(ch->len));
240 void dbg_dump_inode(const struct ubifs_info *c, const struct inode *inode)
242 const struct ubifs_inode *ui = ubifs_inode(inode);
244 printk(KERN_DEBUG "Dump in-memory inode:");
245 printk(KERN_DEBUG "\tinode %lu\n", inode->i_ino);
246 printk(KERN_DEBUG "\tsize %llu\n",
247 (unsigned long long)i_size_read(inode));
248 printk(KERN_DEBUG "\tnlink %u\n", inode->i_nlink);
249 printk(KERN_DEBUG "\tuid %u\n", (unsigned int)inode->i_uid);
250 printk(KERN_DEBUG "\tgid %u\n", (unsigned int)inode->i_gid);
251 printk(KERN_DEBUG "\tatime %u.%u\n",
252 (unsigned int)inode->i_atime.tv_sec,
253 (unsigned int)inode->i_atime.tv_nsec);
254 printk(KERN_DEBUG "\tmtime %u.%u\n",
255 (unsigned int)inode->i_mtime.tv_sec,
256 (unsigned int)inode->i_mtime.tv_nsec);
257 printk(KERN_DEBUG "\tctime %u.%u\n",
258 (unsigned int)inode->i_ctime.tv_sec,
259 (unsigned int)inode->i_ctime.tv_nsec);
260 printk(KERN_DEBUG "\tcreat_sqnum %llu\n", ui->creat_sqnum);
261 printk(KERN_DEBUG "\txattr_size %u\n", ui->xattr_size);
262 printk(KERN_DEBUG "\txattr_cnt %u\n", ui->xattr_cnt);
263 printk(KERN_DEBUG "\txattr_names %u\n", ui->xattr_names);
264 printk(KERN_DEBUG "\tdirty %u\n", ui->dirty);
265 printk(KERN_DEBUG "\txattr %u\n", ui->xattr);
266 printk(KERN_DEBUG "\tbulk_read %u\n", ui->xattr);
267 printk(KERN_DEBUG "\tsynced_i_size %llu\n",
268 (unsigned long long)ui->synced_i_size);
269 printk(KERN_DEBUG "\tui_size %llu\n",
270 (unsigned long long)ui->ui_size);
271 printk(KERN_DEBUG "\tflags %d\n", ui->flags);
272 printk(KERN_DEBUG "\tcompr_type %d\n", ui->compr_type);
273 printk(KERN_DEBUG "\tlast_page_read %lu\n", ui->last_page_read);
274 printk(KERN_DEBUG "\tread_in_a_row %lu\n", ui->read_in_a_row);
275 printk(KERN_DEBUG "\tdata_len %d\n", ui->data_len);
278 void dbg_dump_node(const struct ubifs_info *c, const void *node)
280 int i, n;
281 union ubifs_key key;
282 const struct ubifs_ch *ch = node;
284 if (dbg_failure_mode)
285 return;
287 /* If the magic is incorrect, just hexdump the first bytes */
288 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
289 printk(KERN_DEBUG "Not a node, first %zu bytes:", UBIFS_CH_SZ);
290 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
291 (void *)node, UBIFS_CH_SZ, 1);
292 return;
295 spin_lock(&dbg_lock);
296 dump_ch(node);
298 switch (ch->node_type) {
299 case UBIFS_PAD_NODE:
301 const struct ubifs_pad_node *pad = node;
303 printk(KERN_DEBUG "\tpad_len %u\n",
304 le32_to_cpu(pad->pad_len));
305 break;
307 case UBIFS_SB_NODE:
309 const struct ubifs_sb_node *sup = node;
310 unsigned int sup_flags = le32_to_cpu(sup->flags);
312 printk(KERN_DEBUG "\tkey_hash %d (%s)\n",
313 (int)sup->key_hash, get_key_hash(sup->key_hash));
314 printk(KERN_DEBUG "\tkey_fmt %d (%s)\n",
315 (int)sup->key_fmt, get_key_fmt(sup->key_fmt));
316 printk(KERN_DEBUG "\tflags %#x\n", sup_flags);
317 printk(KERN_DEBUG "\t big_lpt %u\n",
318 !!(sup_flags & UBIFS_FLG_BIGLPT));
319 printk(KERN_DEBUG "\tmin_io_size %u\n",
320 le32_to_cpu(sup->min_io_size));
321 printk(KERN_DEBUG "\tleb_size %u\n",
322 le32_to_cpu(sup->leb_size));
323 printk(KERN_DEBUG "\tleb_cnt %u\n",
324 le32_to_cpu(sup->leb_cnt));
325 printk(KERN_DEBUG "\tmax_leb_cnt %u\n",
326 le32_to_cpu(sup->max_leb_cnt));
327 printk(KERN_DEBUG "\tmax_bud_bytes %llu\n",
328 (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
329 printk(KERN_DEBUG "\tlog_lebs %u\n",
330 le32_to_cpu(sup->log_lebs));
331 printk(KERN_DEBUG "\tlpt_lebs %u\n",
332 le32_to_cpu(sup->lpt_lebs));
333 printk(KERN_DEBUG "\torph_lebs %u\n",
334 le32_to_cpu(sup->orph_lebs));
335 printk(KERN_DEBUG "\tjhead_cnt %u\n",
336 le32_to_cpu(sup->jhead_cnt));
337 printk(KERN_DEBUG "\tfanout %u\n",
338 le32_to_cpu(sup->fanout));
339 printk(KERN_DEBUG "\tlsave_cnt %u\n",
340 le32_to_cpu(sup->lsave_cnt));
341 printk(KERN_DEBUG "\tdefault_compr %u\n",
342 (int)le16_to_cpu(sup->default_compr));
343 printk(KERN_DEBUG "\trp_size %llu\n",
344 (unsigned long long)le64_to_cpu(sup->rp_size));
345 printk(KERN_DEBUG "\trp_uid %u\n",
346 le32_to_cpu(sup->rp_uid));
347 printk(KERN_DEBUG "\trp_gid %u\n",
348 le32_to_cpu(sup->rp_gid));
349 printk(KERN_DEBUG "\tfmt_version %u\n",
350 le32_to_cpu(sup->fmt_version));
351 printk(KERN_DEBUG "\ttime_gran %u\n",
352 le32_to_cpu(sup->time_gran));
353 printk(KERN_DEBUG "\tUUID %02X%02X%02X%02X-%02X%02X"
354 "-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
355 sup->uuid[0], sup->uuid[1], sup->uuid[2], sup->uuid[3],
356 sup->uuid[4], sup->uuid[5], sup->uuid[6], sup->uuid[7],
357 sup->uuid[8], sup->uuid[9], sup->uuid[10], sup->uuid[11],
358 sup->uuid[12], sup->uuid[13], sup->uuid[14],
359 sup->uuid[15]);
360 break;
362 case UBIFS_MST_NODE:
364 const struct ubifs_mst_node *mst = node;
366 printk(KERN_DEBUG "\thighest_inum %llu\n",
367 (unsigned long long)le64_to_cpu(mst->highest_inum));
368 printk(KERN_DEBUG "\tcommit number %llu\n",
369 (unsigned long long)le64_to_cpu(mst->cmt_no));
370 printk(KERN_DEBUG "\tflags %#x\n",
371 le32_to_cpu(mst->flags));
372 printk(KERN_DEBUG "\tlog_lnum %u\n",
373 le32_to_cpu(mst->log_lnum));
374 printk(KERN_DEBUG "\troot_lnum %u\n",
375 le32_to_cpu(mst->root_lnum));
376 printk(KERN_DEBUG "\troot_offs %u\n",
377 le32_to_cpu(mst->root_offs));
378 printk(KERN_DEBUG "\troot_len %u\n",
379 le32_to_cpu(mst->root_len));
380 printk(KERN_DEBUG "\tgc_lnum %u\n",
381 le32_to_cpu(mst->gc_lnum));
382 printk(KERN_DEBUG "\tihead_lnum %u\n",
383 le32_to_cpu(mst->ihead_lnum));
384 printk(KERN_DEBUG "\tihead_offs %u\n",
385 le32_to_cpu(mst->ihead_offs));
386 printk(KERN_DEBUG "\tindex_size %llu\n",
387 (unsigned long long)le64_to_cpu(mst->index_size));
388 printk(KERN_DEBUG "\tlpt_lnum %u\n",
389 le32_to_cpu(mst->lpt_lnum));
390 printk(KERN_DEBUG "\tlpt_offs %u\n",
391 le32_to_cpu(mst->lpt_offs));
392 printk(KERN_DEBUG "\tnhead_lnum %u\n",
393 le32_to_cpu(mst->nhead_lnum));
394 printk(KERN_DEBUG "\tnhead_offs %u\n",
395 le32_to_cpu(mst->nhead_offs));
396 printk(KERN_DEBUG "\tltab_lnum %u\n",
397 le32_to_cpu(mst->ltab_lnum));
398 printk(KERN_DEBUG "\tltab_offs %u\n",
399 le32_to_cpu(mst->ltab_offs));
400 printk(KERN_DEBUG "\tlsave_lnum %u\n",
401 le32_to_cpu(mst->lsave_lnum));
402 printk(KERN_DEBUG "\tlsave_offs %u\n",
403 le32_to_cpu(mst->lsave_offs));
404 printk(KERN_DEBUG "\tlscan_lnum %u\n",
405 le32_to_cpu(mst->lscan_lnum));
406 printk(KERN_DEBUG "\tleb_cnt %u\n",
407 le32_to_cpu(mst->leb_cnt));
408 printk(KERN_DEBUG "\tempty_lebs %u\n",
409 le32_to_cpu(mst->empty_lebs));
410 printk(KERN_DEBUG "\tidx_lebs %u\n",
411 le32_to_cpu(mst->idx_lebs));
412 printk(KERN_DEBUG "\ttotal_free %llu\n",
413 (unsigned long long)le64_to_cpu(mst->total_free));
414 printk(KERN_DEBUG "\ttotal_dirty %llu\n",
415 (unsigned long long)le64_to_cpu(mst->total_dirty));
416 printk(KERN_DEBUG "\ttotal_used %llu\n",
417 (unsigned long long)le64_to_cpu(mst->total_used));
418 printk(KERN_DEBUG "\ttotal_dead %llu\n",
419 (unsigned long long)le64_to_cpu(mst->total_dead));
420 printk(KERN_DEBUG "\ttotal_dark %llu\n",
421 (unsigned long long)le64_to_cpu(mst->total_dark));
422 break;
424 case UBIFS_REF_NODE:
426 const struct ubifs_ref_node *ref = node;
428 printk(KERN_DEBUG "\tlnum %u\n",
429 le32_to_cpu(ref->lnum));
430 printk(KERN_DEBUG "\toffs %u\n",
431 le32_to_cpu(ref->offs));
432 printk(KERN_DEBUG "\tjhead %u\n",
433 le32_to_cpu(ref->jhead));
434 break;
436 case UBIFS_INO_NODE:
438 const struct ubifs_ino_node *ino = node;
440 key_read(c, &ino->key, &key);
441 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
442 printk(KERN_DEBUG "\tcreat_sqnum %llu\n",
443 (unsigned long long)le64_to_cpu(ino->creat_sqnum));
444 printk(KERN_DEBUG "\tsize %llu\n",
445 (unsigned long long)le64_to_cpu(ino->size));
446 printk(KERN_DEBUG "\tnlink %u\n",
447 le32_to_cpu(ino->nlink));
448 printk(KERN_DEBUG "\tatime %lld.%u\n",
449 (long long)le64_to_cpu(ino->atime_sec),
450 le32_to_cpu(ino->atime_nsec));
451 printk(KERN_DEBUG "\tmtime %lld.%u\n",
452 (long long)le64_to_cpu(ino->mtime_sec),
453 le32_to_cpu(ino->mtime_nsec));
454 printk(KERN_DEBUG "\tctime %lld.%u\n",
455 (long long)le64_to_cpu(ino->ctime_sec),
456 le32_to_cpu(ino->ctime_nsec));
457 printk(KERN_DEBUG "\tuid %u\n",
458 le32_to_cpu(ino->uid));
459 printk(KERN_DEBUG "\tgid %u\n",
460 le32_to_cpu(ino->gid));
461 printk(KERN_DEBUG "\tmode %u\n",
462 le32_to_cpu(ino->mode));
463 printk(KERN_DEBUG "\tflags %#x\n",
464 le32_to_cpu(ino->flags));
465 printk(KERN_DEBUG "\txattr_cnt %u\n",
466 le32_to_cpu(ino->xattr_cnt));
467 printk(KERN_DEBUG "\txattr_size %u\n",
468 le32_to_cpu(ino->xattr_size));
469 printk(KERN_DEBUG "\txattr_names %u\n",
470 le32_to_cpu(ino->xattr_names));
471 printk(KERN_DEBUG "\tcompr_type %#x\n",
472 (int)le16_to_cpu(ino->compr_type));
473 printk(KERN_DEBUG "\tdata len %u\n",
474 le32_to_cpu(ino->data_len));
475 break;
477 case UBIFS_DENT_NODE:
478 case UBIFS_XENT_NODE:
480 const struct ubifs_dent_node *dent = node;
481 int nlen = le16_to_cpu(dent->nlen);
483 key_read(c, &dent->key, &key);
484 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
485 printk(KERN_DEBUG "\tinum %llu\n",
486 (unsigned long long)le64_to_cpu(dent->inum));
487 printk(KERN_DEBUG "\ttype %d\n", (int)dent->type);
488 printk(KERN_DEBUG "\tnlen %d\n", nlen);
489 printk(KERN_DEBUG "\tname ");
491 if (nlen > UBIFS_MAX_NLEN)
492 printk(KERN_DEBUG "(bad name length, not printing, "
493 "bad or corrupted node)");
494 else {
495 for (i = 0; i < nlen && dent->name[i]; i++)
496 printk(KERN_CONT "%c", dent->name[i]);
498 printk(KERN_CONT "\n");
500 break;
502 case UBIFS_DATA_NODE:
504 const struct ubifs_data_node *dn = node;
505 int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
507 key_read(c, &dn->key, &key);
508 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
509 printk(KERN_DEBUG "\tsize %u\n",
510 le32_to_cpu(dn->size));
511 printk(KERN_DEBUG "\tcompr_typ %d\n",
512 (int)le16_to_cpu(dn->compr_type));
513 printk(KERN_DEBUG "\tdata size %d\n",
514 dlen);
515 printk(KERN_DEBUG "\tdata:\n");
516 print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET, 32, 1,
517 (void *)&dn->data, dlen, 0);
518 break;
520 case UBIFS_TRUN_NODE:
522 const struct ubifs_trun_node *trun = node;
524 printk(KERN_DEBUG "\tinum %u\n",
525 le32_to_cpu(trun->inum));
526 printk(KERN_DEBUG "\told_size %llu\n",
527 (unsigned long long)le64_to_cpu(trun->old_size));
528 printk(KERN_DEBUG "\tnew_size %llu\n",
529 (unsigned long long)le64_to_cpu(trun->new_size));
530 break;
532 case UBIFS_IDX_NODE:
534 const struct ubifs_idx_node *idx = node;
536 n = le16_to_cpu(idx->child_cnt);
537 printk(KERN_DEBUG "\tchild_cnt %d\n", n);
538 printk(KERN_DEBUG "\tlevel %d\n",
539 (int)le16_to_cpu(idx->level));
540 printk(KERN_DEBUG "\tBranches:\n");
542 for (i = 0; i < n && i < c->fanout - 1; i++) {
543 const struct ubifs_branch *br;
545 br = ubifs_idx_branch(c, idx, i);
546 key_read(c, &br->key, &key);
547 printk(KERN_DEBUG "\t%d: LEB %d:%d len %d key %s\n",
548 i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
549 le32_to_cpu(br->len), DBGKEY(&key));
551 break;
553 case UBIFS_CS_NODE:
554 break;
555 case UBIFS_ORPH_NODE:
557 const struct ubifs_orph_node *orph = node;
559 printk(KERN_DEBUG "\tcommit number %llu\n",
560 (unsigned long long)
561 le64_to_cpu(orph->cmt_no) & LLONG_MAX);
562 printk(KERN_DEBUG "\tlast node flag %llu\n",
563 (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
564 n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
565 printk(KERN_DEBUG "\t%d orphan inode numbers:\n", n);
566 for (i = 0; i < n; i++)
567 printk(KERN_DEBUG "\t ino %llu\n",
568 (unsigned long long)le64_to_cpu(orph->inos[i]));
569 break;
571 default:
572 printk(KERN_DEBUG "node type %d was not recognized\n",
573 (int)ch->node_type);
575 spin_unlock(&dbg_lock);
578 void dbg_dump_budget_req(const struct ubifs_budget_req *req)
580 spin_lock(&dbg_lock);
581 printk(KERN_DEBUG "Budgeting request: new_ino %d, dirtied_ino %d\n",
582 req->new_ino, req->dirtied_ino);
583 printk(KERN_DEBUG "\tnew_ino_d %d, dirtied_ino_d %d\n",
584 req->new_ino_d, req->dirtied_ino_d);
585 printk(KERN_DEBUG "\tnew_page %d, dirtied_page %d\n",
586 req->new_page, req->dirtied_page);
587 printk(KERN_DEBUG "\tnew_dent %d, mod_dent %d\n",
588 req->new_dent, req->mod_dent);
589 printk(KERN_DEBUG "\tidx_growth %d\n", req->idx_growth);
590 printk(KERN_DEBUG "\tdata_growth %d dd_growth %d\n",
591 req->data_growth, req->dd_growth);
592 spin_unlock(&dbg_lock);
595 void dbg_dump_lstats(const struct ubifs_lp_stats *lst)
597 spin_lock(&dbg_lock);
598 printk(KERN_DEBUG "(pid %d) Lprops statistics: empty_lebs %d, "
599 "idx_lebs %d\n", current->pid, lst->empty_lebs, lst->idx_lebs);
600 printk(KERN_DEBUG "\ttaken_empty_lebs %d, total_free %lld, "
601 "total_dirty %lld\n", lst->taken_empty_lebs, lst->total_free,
602 lst->total_dirty);
603 printk(KERN_DEBUG "\ttotal_used %lld, total_dark %lld, "
604 "total_dead %lld\n", lst->total_used, lst->total_dark,
605 lst->total_dead);
606 spin_unlock(&dbg_lock);
609 void dbg_dump_budg(struct ubifs_info *c)
611 int i;
612 struct rb_node *rb;
613 struct ubifs_bud *bud;
614 struct ubifs_gced_idx_leb *idx_gc;
615 long long available, outstanding, free;
617 ubifs_assert(spin_is_locked(&c->space_lock));
618 spin_lock(&dbg_lock);
619 printk(KERN_DEBUG "(pid %d) Budgeting info: budg_data_growth %lld, "
620 "budg_dd_growth %lld, budg_idx_growth %lld\n", current->pid,
621 c->budg_data_growth, c->budg_dd_growth, c->budg_idx_growth);
622 printk(KERN_DEBUG "\tdata budget sum %lld, total budget sum %lld, "
623 "freeable_cnt %d\n", c->budg_data_growth + c->budg_dd_growth,
624 c->budg_data_growth + c->budg_dd_growth + c->budg_idx_growth,
625 c->freeable_cnt);
626 printk(KERN_DEBUG "\tmin_idx_lebs %d, old_idx_sz %lld, "
627 "calc_idx_sz %lld, idx_gc_cnt %d\n", c->min_idx_lebs,
628 c->old_idx_sz, c->calc_idx_sz, c->idx_gc_cnt);
629 printk(KERN_DEBUG "\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, "
630 "clean_zn_cnt %ld\n", atomic_long_read(&c->dirty_pg_cnt),
631 atomic_long_read(&c->dirty_zn_cnt),
632 atomic_long_read(&c->clean_zn_cnt));
633 printk(KERN_DEBUG "\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
634 c->dark_wm, c->dead_wm, c->max_idx_node_sz);
635 printk(KERN_DEBUG "\tgc_lnum %d, ihead_lnum %d\n",
636 c->gc_lnum, c->ihead_lnum);
637 /* If we are in R/O mode, journal heads do not exist */
638 if (c->jheads)
639 for (i = 0; i < c->jhead_cnt; i++)
640 printk(KERN_DEBUG "\tjhead %s\t LEB %d\n",
641 dbg_jhead(c->jheads[i].wbuf.jhead),
642 c->jheads[i].wbuf.lnum);
643 for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
644 bud = rb_entry(rb, struct ubifs_bud, rb);
645 printk(KERN_DEBUG "\tbud LEB %d\n", bud->lnum);
647 list_for_each_entry(bud, &c->old_buds, list)
648 printk(KERN_DEBUG "\told bud LEB %d\n", bud->lnum);
649 list_for_each_entry(idx_gc, &c->idx_gc, list)
650 printk(KERN_DEBUG "\tGC'ed idx LEB %d unmap %d\n",
651 idx_gc->lnum, idx_gc->unmap);
652 printk(KERN_DEBUG "\tcommit state %d\n", c->cmt_state);
654 /* Print budgeting predictions */
655 available = ubifs_calc_available(c, c->min_idx_lebs);
656 outstanding = c->budg_data_growth + c->budg_dd_growth;
657 free = ubifs_get_free_space_nolock(c);
658 printk(KERN_DEBUG "Budgeting predictions:\n");
659 printk(KERN_DEBUG "\tavailable: %lld, outstanding %lld, free %lld\n",
660 available, outstanding, free);
661 spin_unlock(&dbg_lock);
664 void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
666 int i, spc, dark = 0, dead = 0;
667 struct rb_node *rb;
668 struct ubifs_bud *bud;
670 spc = lp->free + lp->dirty;
671 if (spc < c->dead_wm)
672 dead = spc;
673 else
674 dark = ubifs_calc_dark(c, spc);
676 if (lp->flags & LPROPS_INDEX)
677 printk(KERN_DEBUG "LEB %-7d free %-8d dirty %-8d used %-8d "
678 "free + dirty %-8d flags %#x (", lp->lnum, lp->free,
679 lp->dirty, c->leb_size - spc, spc, lp->flags);
680 else
681 printk(KERN_DEBUG "LEB %-7d free %-8d dirty %-8d used %-8d "
682 "free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d "
683 "flags %#-4x (", lp->lnum, lp->free, lp->dirty,
684 c->leb_size - spc, spc, dark, dead,
685 (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags);
687 if (lp->flags & LPROPS_TAKEN) {
688 if (lp->flags & LPROPS_INDEX)
689 printk(KERN_CONT "index, taken");
690 else
691 printk(KERN_CONT "taken");
692 } else {
693 const char *s;
695 if (lp->flags & LPROPS_INDEX) {
696 switch (lp->flags & LPROPS_CAT_MASK) {
697 case LPROPS_DIRTY_IDX:
698 s = "dirty index";
699 break;
700 case LPROPS_FRDI_IDX:
701 s = "freeable index";
702 break;
703 default:
704 s = "index";
706 } else {
707 switch (lp->flags & LPROPS_CAT_MASK) {
708 case LPROPS_UNCAT:
709 s = "not categorized";
710 break;
711 case LPROPS_DIRTY:
712 s = "dirty";
713 break;
714 case LPROPS_FREE:
715 s = "free";
716 break;
717 case LPROPS_EMPTY:
718 s = "empty";
719 break;
720 case LPROPS_FREEABLE:
721 s = "freeable";
722 break;
723 default:
724 s = NULL;
725 break;
728 printk(KERN_CONT "%s", s);
731 for (rb = rb_first((struct rb_root *)&c->buds); rb; rb = rb_next(rb)) {
732 bud = rb_entry(rb, struct ubifs_bud, rb);
733 if (bud->lnum == lp->lnum) {
734 int head = 0;
735 for (i = 0; i < c->jhead_cnt; i++) {
736 if (lp->lnum == c->jheads[i].wbuf.lnum) {
737 printk(KERN_CONT ", jhead %s",
738 dbg_jhead(i));
739 head = 1;
742 if (!head)
743 printk(KERN_CONT ", bud of jhead %s",
744 dbg_jhead(bud->jhead));
747 if (lp->lnum == c->gc_lnum)
748 printk(KERN_CONT ", GC LEB");
749 printk(KERN_CONT ")\n");
752 void dbg_dump_lprops(struct ubifs_info *c)
754 int lnum, err;
755 struct ubifs_lprops lp;
756 struct ubifs_lp_stats lst;
758 printk(KERN_DEBUG "(pid %d) start dumping LEB properties\n",
759 current->pid);
760 ubifs_get_lp_stats(c, &lst);
761 dbg_dump_lstats(&lst);
763 for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
764 err = ubifs_read_one_lp(c, lnum, &lp);
765 if (err)
766 ubifs_err("cannot read lprops for LEB %d", lnum);
768 dbg_dump_lprop(c, &lp);
770 printk(KERN_DEBUG "(pid %d) finish dumping LEB properties\n",
771 current->pid);
774 void dbg_dump_lpt_info(struct ubifs_info *c)
776 int i;
778 spin_lock(&dbg_lock);
779 printk(KERN_DEBUG "(pid %d) dumping LPT information\n", current->pid);
780 printk(KERN_DEBUG "\tlpt_sz: %lld\n", c->lpt_sz);
781 printk(KERN_DEBUG "\tpnode_sz: %d\n", c->pnode_sz);
782 printk(KERN_DEBUG "\tnnode_sz: %d\n", c->nnode_sz);
783 printk(KERN_DEBUG "\tltab_sz: %d\n", c->ltab_sz);
784 printk(KERN_DEBUG "\tlsave_sz: %d\n", c->lsave_sz);
785 printk(KERN_DEBUG "\tbig_lpt: %d\n", c->big_lpt);
786 printk(KERN_DEBUG "\tlpt_hght: %d\n", c->lpt_hght);
787 printk(KERN_DEBUG "\tpnode_cnt: %d\n", c->pnode_cnt);
788 printk(KERN_DEBUG "\tnnode_cnt: %d\n", c->nnode_cnt);
789 printk(KERN_DEBUG "\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt);
790 printk(KERN_DEBUG "\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt);
791 printk(KERN_DEBUG "\tlsave_cnt: %d\n", c->lsave_cnt);
792 printk(KERN_DEBUG "\tspace_bits: %d\n", c->space_bits);
793 printk(KERN_DEBUG "\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
794 printk(KERN_DEBUG "\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
795 printk(KERN_DEBUG "\tlpt_spc_bits: %d\n", c->lpt_spc_bits);
796 printk(KERN_DEBUG "\tpcnt_bits: %d\n", c->pcnt_bits);
797 printk(KERN_DEBUG "\tlnum_bits: %d\n", c->lnum_bits);
798 printk(KERN_DEBUG "\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
799 printk(KERN_DEBUG "\tLPT head is at %d:%d\n",
800 c->nhead_lnum, c->nhead_offs);
801 printk(KERN_DEBUG "\tLPT ltab is at %d:%d\n",
802 c->ltab_lnum, c->ltab_offs);
803 if (c->big_lpt)
804 printk(KERN_DEBUG "\tLPT lsave is at %d:%d\n",
805 c->lsave_lnum, c->lsave_offs);
806 for (i = 0; i < c->lpt_lebs; i++)
807 printk(KERN_DEBUG "\tLPT LEB %d free %d dirty %d tgc %d "
808 "cmt %d\n", i + c->lpt_first, c->ltab[i].free,
809 c->ltab[i].dirty, c->ltab[i].tgc, c->ltab[i].cmt);
810 spin_unlock(&dbg_lock);
813 void dbg_dump_leb(const struct ubifs_info *c, int lnum)
815 struct ubifs_scan_leb *sleb;
816 struct ubifs_scan_node *snod;
818 if (dbg_failure_mode)
819 return;
821 printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n",
822 current->pid, lnum);
823 sleb = ubifs_scan(c, lnum, 0, c->dbg->buf, 0);
824 if (IS_ERR(sleb)) {
825 ubifs_err("scan error %d", (int)PTR_ERR(sleb));
826 return;
829 printk(KERN_DEBUG "LEB %d has %d nodes ending at %d\n", lnum,
830 sleb->nodes_cnt, sleb->endpt);
832 list_for_each_entry(snod, &sleb->nodes, list) {
833 cond_resched();
834 printk(KERN_DEBUG "Dumping node at LEB %d:%d len %d\n", lnum,
835 snod->offs, snod->len);
836 dbg_dump_node(c, snod->node);
839 printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n",
840 current->pid, lnum);
841 ubifs_scan_destroy(sleb);
842 return;
845 void dbg_dump_znode(const struct ubifs_info *c,
846 const struct ubifs_znode *znode)
848 int n;
849 const struct ubifs_zbranch *zbr;
851 spin_lock(&dbg_lock);
852 if (znode->parent)
853 zbr = &znode->parent->zbranch[znode->iip];
854 else
855 zbr = &c->zroot;
857 printk(KERN_DEBUG "znode %p, LEB %d:%d len %d parent %p iip %d level %d"
858 " child_cnt %d flags %lx\n", znode, zbr->lnum, zbr->offs,
859 zbr->len, znode->parent, znode->iip, znode->level,
860 znode->child_cnt, znode->flags);
862 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
863 spin_unlock(&dbg_lock);
864 return;
867 printk(KERN_DEBUG "zbranches:\n");
868 for (n = 0; n < znode->child_cnt; n++) {
869 zbr = &znode->zbranch[n];
870 if (znode->level > 0)
871 printk(KERN_DEBUG "\t%d: znode %p LEB %d:%d len %d key "
872 "%s\n", n, zbr->znode, zbr->lnum,
873 zbr->offs, zbr->len,
874 DBGKEY(&zbr->key));
875 else
876 printk(KERN_DEBUG "\t%d: LNC %p LEB %d:%d len %d key "
877 "%s\n", n, zbr->znode, zbr->lnum,
878 zbr->offs, zbr->len,
879 DBGKEY(&zbr->key));
881 spin_unlock(&dbg_lock);
884 void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
886 int i;
888 printk(KERN_DEBUG "(pid %d) start dumping heap cat %d (%d elements)\n",
889 current->pid, cat, heap->cnt);
890 for (i = 0; i < heap->cnt; i++) {
891 struct ubifs_lprops *lprops = heap->arr[i];
893 printk(KERN_DEBUG "\t%d. LEB %d hpos %d free %d dirty %d "
894 "flags %d\n", i, lprops->lnum, lprops->hpos,
895 lprops->free, lprops->dirty, lprops->flags);
897 printk(KERN_DEBUG "(pid %d) finish dumping heap\n", current->pid);
900 void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
901 struct ubifs_nnode *parent, int iip)
903 int i;
905 printk(KERN_DEBUG "(pid %d) dumping pnode:\n", current->pid);
906 printk(KERN_DEBUG "\taddress %zx parent %zx cnext %zx\n",
907 (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
908 printk(KERN_DEBUG "\tflags %lu iip %d level %d num %d\n",
909 pnode->flags, iip, pnode->level, pnode->num);
910 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
911 struct ubifs_lprops *lp = &pnode->lprops[i];
913 printk(KERN_DEBUG "\t%d: free %d dirty %d flags %d lnum %d\n",
914 i, lp->free, lp->dirty, lp->flags, lp->lnum);
918 void dbg_dump_tnc(struct ubifs_info *c)
920 struct ubifs_znode *znode;
921 int level;
923 printk(KERN_DEBUG "\n");
924 printk(KERN_DEBUG "(pid %d) start dumping TNC tree\n", current->pid);
925 znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
926 level = znode->level;
927 printk(KERN_DEBUG "== Level %d ==\n", level);
928 while (znode) {
929 if (level != znode->level) {
930 level = znode->level;
931 printk(KERN_DEBUG "== Level %d ==\n", level);
933 dbg_dump_znode(c, znode);
934 znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
936 printk(KERN_DEBUG "(pid %d) finish dumping TNC tree\n", current->pid);
939 static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
940 void *priv)
942 dbg_dump_znode(c, znode);
943 return 0;
947 * dbg_dump_index - dump the on-flash index.
948 * @c: UBIFS file-system description object
950 * This function dumps whole UBIFS indexing B-tree, unlike 'dbg_dump_tnc()'
951 * which dumps only in-memory znodes and does not read znodes which from flash.
953 void dbg_dump_index(struct ubifs_info *c)
955 dbg_walk_index(c, NULL, dump_znode, NULL);
959 * dbg_save_space_info - save information about flash space.
960 * @c: UBIFS file-system description object
962 * This function saves information about UBIFS free space, dirty space, etc, in
963 * order to check it later.
965 void dbg_save_space_info(struct ubifs_info *c)
967 struct ubifs_debug_info *d = c->dbg;
968 int freeable_cnt;
970 spin_lock(&c->space_lock);
971 memcpy(&d->saved_lst, &c->lst, sizeof(struct ubifs_lp_stats));
974 * We use a dirty hack here and zero out @c->freeable_cnt, because it
975 * affects the free space calculations, and UBIFS might not know about
976 * all freeable eraseblocks. Indeed, we know about freeable eraseblocks
977 * only when we read their lprops, and we do this only lazily, upon the
978 * need. So at any given point of time @c->freeable_cnt might be not
979 * exactly accurate.
981 * Just one example about the issue we hit when we did not zero
982 * @c->freeable_cnt.
983 * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the
984 * amount of free space in @d->saved_free
985 * 2. We re-mount R/W, which makes UBIFS to read the "lsave"
986 * information from flash, where we cache LEBs from various
987 * categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()'
988 * -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()'
989 * -> 'ubifs_get_pnode()' -> 'update_cats()'
990 * -> 'ubifs_add_to_cat()').
991 * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt
992 * becomes %1.
993 * 4. We calculate the amount of free space when the re-mount is
994 * finished in 'dbg_check_space_info()' and it does not match
995 * @d->saved_free.
997 freeable_cnt = c->freeable_cnt;
998 c->freeable_cnt = 0;
999 d->saved_free = ubifs_get_free_space_nolock(c);
1000 c->freeable_cnt = freeable_cnt;
1001 spin_unlock(&c->space_lock);
1005 * dbg_check_space_info - check flash space information.
1006 * @c: UBIFS file-system description object
1008 * This function compares current flash space information with the information
1009 * which was saved when the 'dbg_save_space_info()' function was called.
1010 * Returns zero if the information has not changed, and %-EINVAL it it has
1011 * changed.
1013 int dbg_check_space_info(struct ubifs_info *c)
1015 struct ubifs_debug_info *d = c->dbg;
1016 struct ubifs_lp_stats lst;
1017 long long free;
1018 int freeable_cnt;
1020 spin_lock(&c->space_lock);
1021 freeable_cnt = c->freeable_cnt;
1022 c->freeable_cnt = 0;
1023 free = ubifs_get_free_space_nolock(c);
1024 c->freeable_cnt = freeable_cnt;
1025 spin_unlock(&c->space_lock);
1027 if (free != d->saved_free) {
1028 ubifs_err("free space changed from %lld to %lld",
1029 d->saved_free, free);
1030 goto out;
1033 return 0;
1035 out:
1036 ubifs_msg("saved lprops statistics dump");
1037 dbg_dump_lstats(&d->saved_lst);
1038 ubifs_get_lp_stats(c, &lst);
1040 ubifs_msg("current lprops statistics dump");
1041 dbg_dump_lstats(&lst);
1043 spin_lock(&c->space_lock);
1044 dbg_dump_budg(c);
1045 spin_unlock(&c->space_lock);
1046 dump_stack();
1047 return -EINVAL;
1051 * dbg_check_synced_i_size - check synchronized inode size.
1052 * @inode: inode to check
1054 * If inode is clean, synchronized inode size has to be equivalent to current
1055 * inode size. This function has to be called only for locked inodes (@i_mutex
1056 * has to be locked). Returns %0 if synchronized inode size if correct, and
1057 * %-EINVAL if not.
1059 int dbg_check_synced_i_size(struct inode *inode)
1061 int err = 0;
1062 struct ubifs_inode *ui = ubifs_inode(inode);
1064 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
1065 return 0;
1066 if (!S_ISREG(inode->i_mode))
1067 return 0;
1069 mutex_lock(&ui->ui_mutex);
1070 spin_lock(&ui->ui_lock);
1071 if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
1072 ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode "
1073 "is clean", ui->ui_size, ui->synced_i_size);
1074 ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
1075 inode->i_mode, i_size_read(inode));
1076 dbg_dump_stack();
1077 err = -EINVAL;
1079 spin_unlock(&ui->ui_lock);
1080 mutex_unlock(&ui->ui_mutex);
1081 return err;
1085 * dbg_check_dir - check directory inode size and link count.
1086 * @c: UBIFS file-system description object
1087 * @dir: the directory to calculate size for
1088 * @size: the result is returned here
1090 * This function makes sure that directory size and link count are correct.
1091 * Returns zero in case of success and a negative error code in case of
1092 * failure.
1094 * Note, it is good idea to make sure the @dir->i_mutex is locked before
1095 * calling this function.
1097 int dbg_check_dir_size(struct ubifs_info *c, const struct inode *dir)
1099 unsigned int nlink = 2;
1100 union ubifs_key key;
1101 struct ubifs_dent_node *dent, *pdent = NULL;
1102 struct qstr nm = { .name = NULL };
1103 loff_t size = UBIFS_INO_NODE_SZ;
1105 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
1106 return 0;
1108 if (!S_ISDIR(dir->i_mode))
1109 return 0;
1111 lowest_dent_key(c, &key, dir->i_ino);
1112 while (1) {
1113 int err;
1115 dent = ubifs_tnc_next_ent(c, &key, &nm);
1116 if (IS_ERR(dent)) {
1117 err = PTR_ERR(dent);
1118 if (err == -ENOENT)
1119 break;
1120 return err;
1123 nm.name = dent->name;
1124 nm.len = le16_to_cpu(dent->nlen);
1125 size += CALC_DENT_SIZE(nm.len);
1126 if (dent->type == UBIFS_ITYPE_DIR)
1127 nlink += 1;
1128 kfree(pdent);
1129 pdent = dent;
1130 key_read(c, &dent->key, &key);
1132 kfree(pdent);
1134 if (i_size_read(dir) != size) {
1135 ubifs_err("directory inode %lu has size %llu, "
1136 "but calculated size is %llu", dir->i_ino,
1137 (unsigned long long)i_size_read(dir),
1138 (unsigned long long)size);
1139 dump_stack();
1140 return -EINVAL;
1142 if (dir->i_nlink != nlink) {
1143 ubifs_err("directory inode %lu has nlink %u, but calculated "
1144 "nlink is %u", dir->i_ino, dir->i_nlink, nlink);
1145 dump_stack();
1146 return -EINVAL;
1149 return 0;
1153 * dbg_check_key_order - make sure that colliding keys are properly ordered.
1154 * @c: UBIFS file-system description object
1155 * @zbr1: first zbranch
1156 * @zbr2: following zbranch
1158 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
1159 * names of the direntries/xentries which are referred by the keys. This
1160 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
1161 * sure the name of direntry/xentry referred by @zbr1 is less than
1162 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
1163 * and a negative error code in case of failure.
1165 static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
1166 struct ubifs_zbranch *zbr2)
1168 int err, nlen1, nlen2, cmp;
1169 struct ubifs_dent_node *dent1, *dent2;
1170 union ubifs_key key;
1172 ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
1173 dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1174 if (!dent1)
1175 return -ENOMEM;
1176 dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1177 if (!dent2) {
1178 err = -ENOMEM;
1179 goto out_free;
1182 err = ubifs_tnc_read_node(c, zbr1, dent1);
1183 if (err)
1184 goto out_free;
1185 err = ubifs_validate_entry(c, dent1);
1186 if (err)
1187 goto out_free;
1189 err = ubifs_tnc_read_node(c, zbr2, dent2);
1190 if (err)
1191 goto out_free;
1192 err = ubifs_validate_entry(c, dent2);
1193 if (err)
1194 goto out_free;
1196 /* Make sure node keys are the same as in zbranch */
1197 err = 1;
1198 key_read(c, &dent1->key, &key);
1199 if (keys_cmp(c, &zbr1->key, &key)) {
1200 dbg_err("1st entry at %d:%d has key %s", zbr1->lnum,
1201 zbr1->offs, DBGKEY(&key));
1202 dbg_err("but it should have key %s according to tnc",
1203 DBGKEY(&zbr1->key));
1204 dbg_dump_node(c, dent1);
1205 goto out_free;
1208 key_read(c, &dent2->key, &key);
1209 if (keys_cmp(c, &zbr2->key, &key)) {
1210 dbg_err("2nd entry at %d:%d has key %s", zbr1->lnum,
1211 zbr1->offs, DBGKEY(&key));
1212 dbg_err("but it should have key %s according to tnc",
1213 DBGKEY(&zbr2->key));
1214 dbg_dump_node(c, dent2);
1215 goto out_free;
1218 nlen1 = le16_to_cpu(dent1->nlen);
1219 nlen2 = le16_to_cpu(dent2->nlen);
1221 cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
1222 if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
1223 err = 0;
1224 goto out_free;
1226 if (cmp == 0 && nlen1 == nlen2)
1227 dbg_err("2 xent/dent nodes with the same name");
1228 else
1229 dbg_err("bad order of colliding key %s",
1230 DBGKEY(&key));
1232 ubifs_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs);
1233 dbg_dump_node(c, dent1);
1234 ubifs_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs);
1235 dbg_dump_node(c, dent2);
1237 out_free:
1238 kfree(dent2);
1239 kfree(dent1);
1240 return err;
1244 * dbg_check_znode - check if znode is all right.
1245 * @c: UBIFS file-system description object
1246 * @zbr: zbranch which points to this znode
1248 * This function makes sure that znode referred to by @zbr is all right.
1249 * Returns zero if it is, and %-EINVAL if it is not.
1251 static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
1253 struct ubifs_znode *znode = zbr->znode;
1254 struct ubifs_znode *zp = znode->parent;
1255 int n, err, cmp;
1257 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
1258 err = 1;
1259 goto out;
1261 if (znode->level < 0) {
1262 err = 2;
1263 goto out;
1265 if (znode->iip < 0 || znode->iip >= c->fanout) {
1266 err = 3;
1267 goto out;
1270 if (zbr->len == 0)
1271 /* Only dirty zbranch may have no on-flash nodes */
1272 if (!ubifs_zn_dirty(znode)) {
1273 err = 4;
1274 goto out;
1277 if (ubifs_zn_dirty(znode)) {
1279 * If znode is dirty, its parent has to be dirty as well. The
1280 * order of the operation is important, so we have to have
1281 * memory barriers.
1283 smp_mb();
1284 if (zp && !ubifs_zn_dirty(zp)) {
1286 * The dirty flag is atomic and is cleared outside the
1287 * TNC mutex, so znode's dirty flag may now have
1288 * been cleared. The child is always cleared before the
1289 * parent, so we just need to check again.
1291 smp_mb();
1292 if (ubifs_zn_dirty(znode)) {
1293 err = 5;
1294 goto out;
1299 if (zp) {
1300 const union ubifs_key *min, *max;
1302 if (znode->level != zp->level - 1) {
1303 err = 6;
1304 goto out;
1307 /* Make sure the 'parent' pointer in our znode is correct */
1308 err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
1309 if (!err) {
1310 /* This zbranch does not exist in the parent */
1311 err = 7;
1312 goto out;
1315 if (znode->iip >= zp->child_cnt) {
1316 err = 8;
1317 goto out;
1320 if (znode->iip != n) {
1321 /* This may happen only in case of collisions */
1322 if (keys_cmp(c, &zp->zbranch[n].key,
1323 &zp->zbranch[znode->iip].key)) {
1324 err = 9;
1325 goto out;
1327 n = znode->iip;
1331 * Make sure that the first key in our znode is greater than or
1332 * equal to the key in the pointing zbranch.
1334 min = &zbr->key;
1335 cmp = keys_cmp(c, min, &znode->zbranch[0].key);
1336 if (cmp == 1) {
1337 err = 10;
1338 goto out;
1341 if (n + 1 < zp->child_cnt) {
1342 max = &zp->zbranch[n + 1].key;
1345 * Make sure the last key in our znode is less or
1346 * equivalent than the key in the zbranch which goes
1347 * after our pointing zbranch.
1349 cmp = keys_cmp(c, max,
1350 &znode->zbranch[znode->child_cnt - 1].key);
1351 if (cmp == -1) {
1352 err = 11;
1353 goto out;
1356 } else {
1357 /* This may only be root znode */
1358 if (zbr != &c->zroot) {
1359 err = 12;
1360 goto out;
1365 * Make sure that next key is greater or equivalent then the previous
1366 * one.
1368 for (n = 1; n < znode->child_cnt; n++) {
1369 cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
1370 &znode->zbranch[n].key);
1371 if (cmp > 0) {
1372 err = 13;
1373 goto out;
1375 if (cmp == 0) {
1376 /* This can only be keys with colliding hash */
1377 if (!is_hash_key(c, &znode->zbranch[n].key)) {
1378 err = 14;
1379 goto out;
1382 if (znode->level != 0 || c->replaying)
1383 continue;
1386 * Colliding keys should follow binary order of
1387 * corresponding xentry/dentry names.
1389 err = dbg_check_key_order(c, &znode->zbranch[n - 1],
1390 &znode->zbranch[n]);
1391 if (err < 0)
1392 return err;
1393 if (err) {
1394 err = 15;
1395 goto out;
1400 for (n = 0; n < znode->child_cnt; n++) {
1401 if (!znode->zbranch[n].znode &&
1402 (znode->zbranch[n].lnum == 0 ||
1403 znode->zbranch[n].len == 0)) {
1404 err = 16;
1405 goto out;
1408 if (znode->zbranch[n].lnum != 0 &&
1409 znode->zbranch[n].len == 0) {
1410 err = 17;
1411 goto out;
1414 if (znode->zbranch[n].lnum == 0 &&
1415 znode->zbranch[n].len != 0) {
1416 err = 18;
1417 goto out;
1420 if (znode->zbranch[n].lnum == 0 &&
1421 znode->zbranch[n].offs != 0) {
1422 err = 19;
1423 goto out;
1426 if (znode->level != 0 && znode->zbranch[n].znode)
1427 if (znode->zbranch[n].znode->parent != znode) {
1428 err = 20;
1429 goto out;
1433 return 0;
1435 out:
1436 ubifs_err("failed, error %d", err);
1437 ubifs_msg("dump of the znode");
1438 dbg_dump_znode(c, znode);
1439 if (zp) {
1440 ubifs_msg("dump of the parent znode");
1441 dbg_dump_znode(c, zp);
1443 dump_stack();
1444 return -EINVAL;
1448 * dbg_check_tnc - check TNC tree.
1449 * @c: UBIFS file-system description object
1450 * @extra: do extra checks that are possible at start commit
1452 * This function traverses whole TNC tree and checks every znode. Returns zero
1453 * if everything is all right and %-EINVAL if something is wrong with TNC.
1455 int dbg_check_tnc(struct ubifs_info *c, int extra)
1457 struct ubifs_znode *znode;
1458 long clean_cnt = 0, dirty_cnt = 0;
1459 int err, last;
1461 if (!(ubifs_chk_flags & UBIFS_CHK_TNC))
1462 return 0;
1464 ubifs_assert(mutex_is_locked(&c->tnc_mutex));
1465 if (!c->zroot.znode)
1466 return 0;
1468 znode = ubifs_tnc_postorder_first(c->zroot.znode);
1469 while (1) {
1470 struct ubifs_znode *prev;
1471 struct ubifs_zbranch *zbr;
1473 if (!znode->parent)
1474 zbr = &c->zroot;
1475 else
1476 zbr = &znode->parent->zbranch[znode->iip];
1478 err = dbg_check_znode(c, zbr);
1479 if (err)
1480 return err;
1482 if (extra) {
1483 if (ubifs_zn_dirty(znode))
1484 dirty_cnt += 1;
1485 else
1486 clean_cnt += 1;
1489 prev = znode;
1490 znode = ubifs_tnc_postorder_next(znode);
1491 if (!znode)
1492 break;
1495 * If the last key of this znode is equivalent to the first key
1496 * of the next znode (collision), then check order of the keys.
1498 last = prev->child_cnt - 1;
1499 if (prev->level == 0 && znode->level == 0 && !c->replaying &&
1500 !keys_cmp(c, &prev->zbranch[last].key,
1501 &znode->zbranch[0].key)) {
1502 err = dbg_check_key_order(c, &prev->zbranch[last],
1503 &znode->zbranch[0]);
1504 if (err < 0)
1505 return err;
1506 if (err) {
1507 ubifs_msg("first znode");
1508 dbg_dump_znode(c, prev);
1509 ubifs_msg("second znode");
1510 dbg_dump_znode(c, znode);
1511 return -EINVAL;
1516 if (extra) {
1517 if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
1518 ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld",
1519 atomic_long_read(&c->clean_zn_cnt),
1520 clean_cnt);
1521 return -EINVAL;
1523 if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
1524 ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld",
1525 atomic_long_read(&c->dirty_zn_cnt),
1526 dirty_cnt);
1527 return -EINVAL;
1531 return 0;
1535 * dbg_walk_index - walk the on-flash index.
1536 * @c: UBIFS file-system description object
1537 * @leaf_cb: called for each leaf node
1538 * @znode_cb: called for each indexing node
1539 * @priv: private data which is passed to callbacks
1541 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1542 * node and @znode_cb for each indexing node. Returns zero in case of success
1543 * and a negative error code in case of failure.
1545 * It would be better if this function removed every znode it pulled to into
1546 * the TNC, so that the behavior more closely matched the non-debugging
1547 * behavior.
1549 int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1550 dbg_znode_callback znode_cb, void *priv)
1552 int err;
1553 struct ubifs_zbranch *zbr;
1554 struct ubifs_znode *znode, *child;
1556 mutex_lock(&c->tnc_mutex);
1557 /* If the root indexing node is not in TNC - pull it */
1558 if (!c->zroot.znode) {
1559 c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1560 if (IS_ERR(c->zroot.znode)) {
1561 err = PTR_ERR(c->zroot.znode);
1562 c->zroot.znode = NULL;
1563 goto out_unlock;
1568 * We are going to traverse the indexing tree in the postorder manner.
1569 * Go down and find the leftmost indexing node where we are going to
1570 * start from.
1572 znode = c->zroot.znode;
1573 while (znode->level > 0) {
1574 zbr = &znode->zbranch[0];
1575 child = zbr->znode;
1576 if (!child) {
1577 child = ubifs_load_znode(c, zbr, znode, 0);
1578 if (IS_ERR(child)) {
1579 err = PTR_ERR(child);
1580 goto out_unlock;
1582 zbr->znode = child;
1585 znode = child;
1588 /* Iterate over all indexing nodes */
1589 while (1) {
1590 int idx;
1592 cond_resched();
1594 if (znode_cb) {
1595 err = znode_cb(c, znode, priv);
1596 if (err) {
1597 ubifs_err("znode checking function returned "
1598 "error %d", err);
1599 dbg_dump_znode(c, znode);
1600 goto out_dump;
1603 if (leaf_cb && znode->level == 0) {
1604 for (idx = 0; idx < znode->child_cnt; idx++) {
1605 zbr = &znode->zbranch[idx];
1606 err = leaf_cb(c, zbr, priv);
1607 if (err) {
1608 ubifs_err("leaf checking function "
1609 "returned error %d, for leaf "
1610 "at LEB %d:%d",
1611 err, zbr->lnum, zbr->offs);
1612 goto out_dump;
1617 if (!znode->parent)
1618 break;
1620 idx = znode->iip + 1;
1621 znode = znode->parent;
1622 if (idx < znode->child_cnt) {
1623 /* Switch to the next index in the parent */
1624 zbr = &znode->zbranch[idx];
1625 child = zbr->znode;
1626 if (!child) {
1627 child = ubifs_load_znode(c, zbr, znode, idx);
1628 if (IS_ERR(child)) {
1629 err = PTR_ERR(child);
1630 goto out_unlock;
1632 zbr->znode = child;
1634 znode = child;
1635 } else
1637 * This is the last child, switch to the parent and
1638 * continue.
1640 continue;
1642 /* Go to the lowest leftmost znode in the new sub-tree */
1643 while (znode->level > 0) {
1644 zbr = &znode->zbranch[0];
1645 child = zbr->znode;
1646 if (!child) {
1647 child = ubifs_load_znode(c, zbr, znode, 0);
1648 if (IS_ERR(child)) {
1649 err = PTR_ERR(child);
1650 goto out_unlock;
1652 zbr->znode = child;
1654 znode = child;
1658 mutex_unlock(&c->tnc_mutex);
1659 return 0;
1661 out_dump:
1662 if (znode->parent)
1663 zbr = &znode->parent->zbranch[znode->iip];
1664 else
1665 zbr = &c->zroot;
1666 ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1667 dbg_dump_znode(c, znode);
1668 out_unlock:
1669 mutex_unlock(&c->tnc_mutex);
1670 return err;
1674 * add_size - add znode size to partially calculated index size.
1675 * @c: UBIFS file-system description object
1676 * @znode: znode to add size for
1677 * @priv: partially calculated index size
1679 * This is a helper function for 'dbg_check_idx_size()' which is called for
1680 * every indexing node and adds its size to the 'long long' variable pointed to
1681 * by @priv.
1683 static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1685 long long *idx_size = priv;
1686 int add;
1688 add = ubifs_idx_node_sz(c, znode->child_cnt);
1689 add = ALIGN(add, 8);
1690 *idx_size += add;
1691 return 0;
1695 * dbg_check_idx_size - check index size.
1696 * @c: UBIFS file-system description object
1697 * @idx_size: size to check
1699 * This function walks the UBIFS index, calculates its size and checks that the
1700 * size is equivalent to @idx_size. Returns zero in case of success and a
1701 * negative error code in case of failure.
1703 int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
1705 int err;
1706 long long calc = 0;
1708 if (!(ubifs_chk_flags & UBIFS_CHK_IDX_SZ))
1709 return 0;
1711 err = dbg_walk_index(c, NULL, add_size, &calc);
1712 if (err) {
1713 ubifs_err("error %d while walking the index", err);
1714 return err;
1717 if (calc != idx_size) {
1718 ubifs_err("index size check failed: calculated size is %lld, "
1719 "should be %lld", calc, idx_size);
1720 dump_stack();
1721 return -EINVAL;
1724 return 0;
1728 * struct fsck_inode - information about an inode used when checking the file-system.
1729 * @rb: link in the RB-tree of inodes
1730 * @inum: inode number
1731 * @mode: inode type, permissions, etc
1732 * @nlink: inode link count
1733 * @xattr_cnt: count of extended attributes
1734 * @references: how many directory/xattr entries refer this inode (calculated
1735 * while walking the index)
1736 * @calc_cnt: for directory inode count of child directories
1737 * @size: inode size (read from on-flash inode)
1738 * @xattr_sz: summary size of all extended attributes (read from on-flash
1739 * inode)
1740 * @calc_sz: for directories calculated directory size
1741 * @calc_xcnt: count of extended attributes
1742 * @calc_xsz: calculated summary size of all extended attributes
1743 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1744 * inode (read from on-flash inode)
1745 * @calc_xnms: calculated sum of lengths of all extended attribute names
1747 struct fsck_inode {
1748 struct rb_node rb;
1749 ino_t inum;
1750 umode_t mode;
1751 unsigned int nlink;
1752 unsigned int xattr_cnt;
1753 int references;
1754 int calc_cnt;
1755 long long size;
1756 unsigned int xattr_sz;
1757 long long calc_sz;
1758 long long calc_xcnt;
1759 long long calc_xsz;
1760 unsigned int xattr_nms;
1761 long long calc_xnms;
1765 * struct fsck_data - private FS checking information.
1766 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1768 struct fsck_data {
1769 struct rb_root inodes;
1773 * add_inode - add inode information to RB-tree of inodes.
1774 * @c: UBIFS file-system description object
1775 * @fsckd: FS checking information
1776 * @ino: raw UBIFS inode to add
1778 * This is a helper function for 'check_leaf()' which adds information about
1779 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1780 * case of success and a negative error code in case of failure.
1782 static struct fsck_inode *add_inode(struct ubifs_info *c,
1783 struct fsck_data *fsckd,
1784 struct ubifs_ino_node *ino)
1786 struct rb_node **p, *parent = NULL;
1787 struct fsck_inode *fscki;
1788 ino_t inum = key_inum_flash(c, &ino->key);
1790 p = &fsckd->inodes.rb_node;
1791 while (*p) {
1792 parent = *p;
1793 fscki = rb_entry(parent, struct fsck_inode, rb);
1794 if (inum < fscki->inum)
1795 p = &(*p)->rb_left;
1796 else if (inum > fscki->inum)
1797 p = &(*p)->rb_right;
1798 else
1799 return fscki;
1802 if (inum > c->highest_inum) {
1803 ubifs_err("too high inode number, max. is %lu",
1804 (unsigned long)c->highest_inum);
1805 return ERR_PTR(-EINVAL);
1808 fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
1809 if (!fscki)
1810 return ERR_PTR(-ENOMEM);
1812 fscki->inum = inum;
1813 fscki->nlink = le32_to_cpu(ino->nlink);
1814 fscki->size = le64_to_cpu(ino->size);
1815 fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
1816 fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
1817 fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
1818 fscki->mode = le32_to_cpu(ino->mode);
1819 if (S_ISDIR(fscki->mode)) {
1820 fscki->calc_sz = UBIFS_INO_NODE_SZ;
1821 fscki->calc_cnt = 2;
1823 rb_link_node(&fscki->rb, parent, p);
1824 rb_insert_color(&fscki->rb, &fsckd->inodes);
1825 return fscki;
1829 * search_inode - search inode in the RB-tree of inodes.
1830 * @fsckd: FS checking information
1831 * @inum: inode number to search
1833 * This is a helper function for 'check_leaf()' which searches inode @inum in
1834 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1835 * the inode was not found.
1837 static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
1839 struct rb_node *p;
1840 struct fsck_inode *fscki;
1842 p = fsckd->inodes.rb_node;
1843 while (p) {
1844 fscki = rb_entry(p, struct fsck_inode, rb);
1845 if (inum < fscki->inum)
1846 p = p->rb_left;
1847 else if (inum > fscki->inum)
1848 p = p->rb_right;
1849 else
1850 return fscki;
1852 return NULL;
1856 * read_add_inode - read inode node and add it to RB-tree of inodes.
1857 * @c: UBIFS file-system description object
1858 * @fsckd: FS checking information
1859 * @inum: inode number to read
1861 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1862 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1863 * information pointer in case of success and a negative error code in case of
1864 * failure.
1866 static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1867 struct fsck_data *fsckd, ino_t inum)
1869 int n, err;
1870 union ubifs_key key;
1871 struct ubifs_znode *znode;
1872 struct ubifs_zbranch *zbr;
1873 struct ubifs_ino_node *ino;
1874 struct fsck_inode *fscki;
1876 fscki = search_inode(fsckd, inum);
1877 if (fscki)
1878 return fscki;
1880 ino_key_init(c, &key, inum);
1881 err = ubifs_lookup_level0(c, &key, &znode, &n);
1882 if (!err) {
1883 ubifs_err("inode %lu not found in index", (unsigned long)inum);
1884 return ERR_PTR(-ENOENT);
1885 } else if (err < 0) {
1886 ubifs_err("error %d while looking up inode %lu",
1887 err, (unsigned long)inum);
1888 return ERR_PTR(err);
1891 zbr = &znode->zbranch[n];
1892 if (zbr->len < UBIFS_INO_NODE_SZ) {
1893 ubifs_err("bad node %lu node length %d",
1894 (unsigned long)inum, zbr->len);
1895 return ERR_PTR(-EINVAL);
1898 ino = kmalloc(zbr->len, GFP_NOFS);
1899 if (!ino)
1900 return ERR_PTR(-ENOMEM);
1902 err = ubifs_tnc_read_node(c, zbr, ino);
1903 if (err) {
1904 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
1905 zbr->lnum, zbr->offs, err);
1906 kfree(ino);
1907 return ERR_PTR(err);
1910 fscki = add_inode(c, fsckd, ino);
1911 kfree(ino);
1912 if (IS_ERR(fscki)) {
1913 ubifs_err("error %ld while adding inode %lu node",
1914 PTR_ERR(fscki), (unsigned long)inum);
1915 return fscki;
1918 return fscki;
1922 * check_leaf - check leaf node.
1923 * @c: UBIFS file-system description object
1924 * @zbr: zbranch of the leaf node to check
1925 * @priv: FS checking information
1927 * This is a helper function for 'dbg_check_filesystem()' which is called for
1928 * every single leaf node while walking the indexing tree. It checks that the
1929 * leaf node referred from the indexing tree exists, has correct CRC, and does
1930 * some other basic validation. This function is also responsible for building
1931 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
1932 * calculates reference count, size, etc for each inode in order to later
1933 * compare them to the information stored inside the inodes and detect possible
1934 * inconsistencies. Returns zero in case of success and a negative error code
1935 * in case of failure.
1937 static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
1938 void *priv)
1940 ino_t inum;
1941 void *node;
1942 struct ubifs_ch *ch;
1943 int err, type = key_type(c, &zbr->key);
1944 struct fsck_inode *fscki;
1946 if (zbr->len < UBIFS_CH_SZ) {
1947 ubifs_err("bad leaf length %d (LEB %d:%d)",
1948 zbr->len, zbr->lnum, zbr->offs);
1949 return -EINVAL;
1952 node = kmalloc(zbr->len, GFP_NOFS);
1953 if (!node)
1954 return -ENOMEM;
1956 err = ubifs_tnc_read_node(c, zbr, node);
1957 if (err) {
1958 ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
1959 zbr->lnum, zbr->offs, err);
1960 goto out_free;
1963 /* If this is an inode node, add it to RB-tree of inodes */
1964 if (type == UBIFS_INO_KEY) {
1965 fscki = add_inode(c, priv, node);
1966 if (IS_ERR(fscki)) {
1967 err = PTR_ERR(fscki);
1968 ubifs_err("error %d while adding inode node", err);
1969 goto out_dump;
1971 goto out;
1974 if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
1975 type != UBIFS_DATA_KEY) {
1976 ubifs_err("unexpected node type %d at LEB %d:%d",
1977 type, zbr->lnum, zbr->offs);
1978 err = -EINVAL;
1979 goto out_free;
1982 ch = node;
1983 if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
1984 ubifs_err("too high sequence number, max. is %llu",
1985 c->max_sqnum);
1986 err = -EINVAL;
1987 goto out_dump;
1990 if (type == UBIFS_DATA_KEY) {
1991 long long blk_offs;
1992 struct ubifs_data_node *dn = node;
1995 * Search the inode node this data node belongs to and insert
1996 * it to the RB-tree of inodes.
1998 inum = key_inum_flash(c, &dn->key);
1999 fscki = read_add_inode(c, priv, inum);
2000 if (IS_ERR(fscki)) {
2001 err = PTR_ERR(fscki);
2002 ubifs_err("error %d while processing data node and "
2003 "trying to find inode node %lu",
2004 err, (unsigned long)inum);
2005 goto out_dump;
2008 /* Make sure the data node is within inode size */
2009 blk_offs = key_block_flash(c, &dn->key);
2010 blk_offs <<= UBIFS_BLOCK_SHIFT;
2011 blk_offs += le32_to_cpu(dn->size);
2012 if (blk_offs > fscki->size) {
2013 ubifs_err("data node at LEB %d:%d is not within inode "
2014 "size %lld", zbr->lnum, zbr->offs,
2015 fscki->size);
2016 err = -EINVAL;
2017 goto out_dump;
2019 } else {
2020 int nlen;
2021 struct ubifs_dent_node *dent = node;
2022 struct fsck_inode *fscki1;
2024 err = ubifs_validate_entry(c, dent);
2025 if (err)
2026 goto out_dump;
2029 * Search the inode node this entry refers to and the parent
2030 * inode node and insert them to the RB-tree of inodes.
2032 inum = le64_to_cpu(dent->inum);
2033 fscki = read_add_inode(c, priv, inum);
2034 if (IS_ERR(fscki)) {
2035 err = PTR_ERR(fscki);
2036 ubifs_err("error %d while processing entry node and "
2037 "trying to find inode node %lu",
2038 err, (unsigned long)inum);
2039 goto out_dump;
2042 /* Count how many direntries or xentries refers this inode */
2043 fscki->references += 1;
2045 inum = key_inum_flash(c, &dent->key);
2046 fscki1 = read_add_inode(c, priv, inum);
2047 if (IS_ERR(fscki1)) {
2048 err = PTR_ERR(fscki);
2049 ubifs_err("error %d while processing entry node and "
2050 "trying to find parent inode node %lu",
2051 err, (unsigned long)inum);
2052 goto out_dump;
2055 nlen = le16_to_cpu(dent->nlen);
2056 if (type == UBIFS_XENT_KEY) {
2057 fscki1->calc_xcnt += 1;
2058 fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
2059 fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
2060 fscki1->calc_xnms += nlen;
2061 } else {
2062 fscki1->calc_sz += CALC_DENT_SIZE(nlen);
2063 if (dent->type == UBIFS_ITYPE_DIR)
2064 fscki1->calc_cnt += 1;
2068 out:
2069 kfree(node);
2070 return 0;
2072 out_dump:
2073 ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
2074 dbg_dump_node(c, node);
2075 out_free:
2076 kfree(node);
2077 return err;
2081 * free_inodes - free RB-tree of inodes.
2082 * @fsckd: FS checking information
2084 static void free_inodes(struct fsck_data *fsckd)
2086 struct rb_node *this = fsckd->inodes.rb_node;
2087 struct fsck_inode *fscki;
2089 while (this) {
2090 if (this->rb_left)
2091 this = this->rb_left;
2092 else if (this->rb_right)
2093 this = this->rb_right;
2094 else {
2095 fscki = rb_entry(this, struct fsck_inode, rb);
2096 this = rb_parent(this);
2097 if (this) {
2098 if (this->rb_left == &fscki->rb)
2099 this->rb_left = NULL;
2100 else
2101 this->rb_right = NULL;
2103 kfree(fscki);
2109 * check_inodes - checks all inodes.
2110 * @c: UBIFS file-system description object
2111 * @fsckd: FS checking information
2113 * This is a helper function for 'dbg_check_filesystem()' which walks the
2114 * RB-tree of inodes after the index scan has been finished, and checks that
2115 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
2116 * %-EINVAL if not, and a negative error code in case of failure.
2118 static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
2120 int n, err;
2121 union ubifs_key key;
2122 struct ubifs_znode *znode;
2123 struct ubifs_zbranch *zbr;
2124 struct ubifs_ino_node *ino;
2125 struct fsck_inode *fscki;
2126 struct rb_node *this = rb_first(&fsckd->inodes);
2128 while (this) {
2129 fscki = rb_entry(this, struct fsck_inode, rb);
2130 this = rb_next(this);
2132 if (S_ISDIR(fscki->mode)) {
2134 * Directories have to have exactly one reference (they
2135 * cannot have hardlinks), although root inode is an
2136 * exception.
2138 if (fscki->inum != UBIFS_ROOT_INO &&
2139 fscki->references != 1) {
2140 ubifs_err("directory inode %lu has %d "
2141 "direntries which refer it, but "
2142 "should be 1",
2143 (unsigned long)fscki->inum,
2144 fscki->references);
2145 goto out_dump;
2147 if (fscki->inum == UBIFS_ROOT_INO &&
2148 fscki->references != 0) {
2149 ubifs_err("root inode %lu has non-zero (%d) "
2150 "direntries which refer it",
2151 (unsigned long)fscki->inum,
2152 fscki->references);
2153 goto out_dump;
2155 if (fscki->calc_sz != fscki->size) {
2156 ubifs_err("directory inode %lu size is %lld, "
2157 "but calculated size is %lld",
2158 (unsigned long)fscki->inum,
2159 fscki->size, fscki->calc_sz);
2160 goto out_dump;
2162 if (fscki->calc_cnt != fscki->nlink) {
2163 ubifs_err("directory inode %lu nlink is %d, "
2164 "but calculated nlink is %d",
2165 (unsigned long)fscki->inum,
2166 fscki->nlink, fscki->calc_cnt);
2167 goto out_dump;
2169 } else {
2170 if (fscki->references != fscki->nlink) {
2171 ubifs_err("inode %lu nlink is %d, but "
2172 "calculated nlink is %d",
2173 (unsigned long)fscki->inum,
2174 fscki->nlink, fscki->references);
2175 goto out_dump;
2178 if (fscki->xattr_sz != fscki->calc_xsz) {
2179 ubifs_err("inode %lu has xattr size %u, but "
2180 "calculated size is %lld",
2181 (unsigned long)fscki->inum, fscki->xattr_sz,
2182 fscki->calc_xsz);
2183 goto out_dump;
2185 if (fscki->xattr_cnt != fscki->calc_xcnt) {
2186 ubifs_err("inode %lu has %u xattrs, but "
2187 "calculated count is %lld",
2188 (unsigned long)fscki->inum,
2189 fscki->xattr_cnt, fscki->calc_xcnt);
2190 goto out_dump;
2192 if (fscki->xattr_nms != fscki->calc_xnms) {
2193 ubifs_err("inode %lu has xattr names' size %u, but "
2194 "calculated names' size is %lld",
2195 (unsigned long)fscki->inum, fscki->xattr_nms,
2196 fscki->calc_xnms);
2197 goto out_dump;
2201 return 0;
2203 out_dump:
2204 /* Read the bad inode and dump it */
2205 ino_key_init(c, &key, fscki->inum);
2206 err = ubifs_lookup_level0(c, &key, &znode, &n);
2207 if (!err) {
2208 ubifs_err("inode %lu not found in index",
2209 (unsigned long)fscki->inum);
2210 return -ENOENT;
2211 } else if (err < 0) {
2212 ubifs_err("error %d while looking up inode %lu",
2213 err, (unsigned long)fscki->inum);
2214 return err;
2217 zbr = &znode->zbranch[n];
2218 ino = kmalloc(zbr->len, GFP_NOFS);
2219 if (!ino)
2220 return -ENOMEM;
2222 err = ubifs_tnc_read_node(c, zbr, ino);
2223 if (err) {
2224 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2225 zbr->lnum, zbr->offs, err);
2226 kfree(ino);
2227 return err;
2230 ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",
2231 (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
2232 dbg_dump_node(c, ino);
2233 kfree(ino);
2234 return -EINVAL;
2238 * dbg_check_filesystem - check the file-system.
2239 * @c: UBIFS file-system description object
2241 * This function checks the file system, namely:
2242 * o makes sure that all leaf nodes exist and their CRCs are correct;
2243 * o makes sure inode nlink, size, xattr size/count are correct (for all
2244 * inodes).
2246 * The function reads whole indexing tree and all nodes, so it is pretty
2247 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2248 * not, and a negative error code in case of failure.
2250 int dbg_check_filesystem(struct ubifs_info *c)
2252 int err;
2253 struct fsck_data fsckd;
2255 if (!(ubifs_chk_flags & UBIFS_CHK_FS))
2256 return 0;
2258 fsckd.inodes = RB_ROOT;
2259 err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
2260 if (err)
2261 goto out_free;
2263 err = check_inodes(c, &fsckd);
2264 if (err)
2265 goto out_free;
2267 free_inodes(&fsckd);
2268 return 0;
2270 out_free:
2271 ubifs_err("file-system check failed with error %d", err);
2272 dump_stack();
2273 free_inodes(&fsckd);
2274 return err;
2277 static int invocation_cnt;
2279 int dbg_force_in_the_gaps(void)
2281 if (!dbg_force_in_the_gaps_enabled)
2282 return 0;
2283 /* Force in-the-gaps every 8th commit */
2284 return !((invocation_cnt++) & 0x7);
2287 /* Failure mode for recovery testing */
2289 #define chance(n, d) (simple_rand() <= (n) * 32768LL / (d))
2291 struct failure_mode_info {
2292 struct list_head list;
2293 struct ubifs_info *c;
2296 static LIST_HEAD(fmi_list);
2297 static DEFINE_SPINLOCK(fmi_lock);
2299 static unsigned int next;
2301 static int simple_rand(void)
2303 if (next == 0)
2304 next = current->pid;
2305 next = next * 1103515245 + 12345;
2306 return (next >> 16) & 32767;
2309 static void failure_mode_init(struct ubifs_info *c)
2311 struct failure_mode_info *fmi;
2313 fmi = kmalloc(sizeof(struct failure_mode_info), GFP_NOFS);
2314 if (!fmi) {
2315 ubifs_err("Failed to register failure mode - no memory");
2316 return;
2318 fmi->c = c;
2319 spin_lock(&fmi_lock);
2320 list_add_tail(&fmi->list, &fmi_list);
2321 spin_unlock(&fmi_lock);
2324 static void failure_mode_exit(struct ubifs_info *c)
2326 struct failure_mode_info *fmi, *tmp;
2328 spin_lock(&fmi_lock);
2329 list_for_each_entry_safe(fmi, tmp, &fmi_list, list)
2330 if (fmi->c == c) {
2331 list_del(&fmi->list);
2332 kfree(fmi);
2334 spin_unlock(&fmi_lock);
2337 static struct ubifs_info *dbg_find_info(struct ubi_volume_desc *desc)
2339 struct failure_mode_info *fmi;
2341 spin_lock(&fmi_lock);
2342 list_for_each_entry(fmi, &fmi_list, list)
2343 if (fmi->c->ubi == desc) {
2344 struct ubifs_info *c = fmi->c;
2346 spin_unlock(&fmi_lock);
2347 return c;
2349 spin_unlock(&fmi_lock);
2350 return NULL;
2353 static int in_failure_mode(struct ubi_volume_desc *desc)
2355 struct ubifs_info *c = dbg_find_info(desc);
2357 if (c && dbg_failure_mode)
2358 return c->dbg->failure_mode;
2359 return 0;
2362 static int do_fail(struct ubi_volume_desc *desc, int lnum, int write)
2364 struct ubifs_info *c = dbg_find_info(desc);
2365 struct ubifs_debug_info *d;
2367 if (!c || !dbg_failure_mode)
2368 return 0;
2369 d = c->dbg;
2370 if (d->failure_mode)
2371 return 1;
2372 if (!d->fail_cnt) {
2373 /* First call - decide delay to failure */
2374 if (chance(1, 2)) {
2375 unsigned int delay = 1 << (simple_rand() >> 11);
2377 if (chance(1, 2)) {
2378 d->fail_delay = 1;
2379 d->fail_timeout = jiffies +
2380 msecs_to_jiffies(delay);
2381 dbg_rcvry("failing after %ums", delay);
2382 } else {
2383 d->fail_delay = 2;
2384 d->fail_cnt_max = delay;
2385 dbg_rcvry("failing after %u calls", delay);
2388 d->fail_cnt += 1;
2390 /* Determine if failure delay has expired */
2391 if (d->fail_delay == 1) {
2392 if (time_before(jiffies, d->fail_timeout))
2393 return 0;
2394 } else if (d->fail_delay == 2)
2395 if (d->fail_cnt++ < d->fail_cnt_max)
2396 return 0;
2397 if (lnum == UBIFS_SB_LNUM) {
2398 if (write) {
2399 if (chance(1, 2))
2400 return 0;
2401 } else if (chance(19, 20))
2402 return 0;
2403 dbg_rcvry("failing in super block LEB %d", lnum);
2404 } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2405 if (chance(19, 20))
2406 return 0;
2407 dbg_rcvry("failing in master LEB %d", lnum);
2408 } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2409 if (write) {
2410 if (chance(99, 100))
2411 return 0;
2412 } else if (chance(399, 400))
2413 return 0;
2414 dbg_rcvry("failing in log LEB %d", lnum);
2415 } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2416 if (write) {
2417 if (chance(7, 8))
2418 return 0;
2419 } else if (chance(19, 20))
2420 return 0;
2421 dbg_rcvry("failing in LPT LEB %d", lnum);
2422 } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2423 if (write) {
2424 if (chance(1, 2))
2425 return 0;
2426 } else if (chance(9, 10))
2427 return 0;
2428 dbg_rcvry("failing in orphan LEB %d", lnum);
2429 } else if (lnum == c->ihead_lnum) {
2430 if (chance(99, 100))
2431 return 0;
2432 dbg_rcvry("failing in index head LEB %d", lnum);
2433 } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2434 if (chance(9, 10))
2435 return 0;
2436 dbg_rcvry("failing in GC head LEB %d", lnum);
2437 } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
2438 !ubifs_search_bud(c, lnum)) {
2439 if (chance(19, 20))
2440 return 0;
2441 dbg_rcvry("failing in non-bud LEB %d", lnum);
2442 } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
2443 c->cmt_state == COMMIT_RUNNING_REQUIRED) {
2444 if (chance(999, 1000))
2445 return 0;
2446 dbg_rcvry("failing in bud LEB %d commit running", lnum);
2447 } else {
2448 if (chance(9999, 10000))
2449 return 0;
2450 dbg_rcvry("failing in bud LEB %d commit not running", lnum);
2452 ubifs_err("*** SETTING FAILURE MODE ON (LEB %d) ***", lnum);
2453 d->failure_mode = 1;
2454 dump_stack();
2455 return 1;
2458 static void cut_data(const void *buf, int len)
2460 int flen, i;
2461 unsigned char *p = (void *)buf;
2463 flen = (len * (long long)simple_rand()) >> 15;
2464 for (i = flen; i < len; i++)
2465 p[i] = 0xff;
2468 int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
2469 int len, int check)
2471 if (in_failure_mode(desc))
2472 return -EIO;
2473 return ubi_leb_read(desc, lnum, buf, offset, len, check);
2476 int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
2477 int offset, int len, int dtype)
2479 int err, failing;
2481 if (in_failure_mode(desc))
2482 return -EIO;
2483 failing = do_fail(desc, lnum, 1);
2484 if (failing)
2485 cut_data(buf, len);
2486 err = ubi_leb_write(desc, lnum, buf, offset, len, dtype);
2487 if (err)
2488 return err;
2489 if (failing)
2490 return -EIO;
2491 return 0;
2494 int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
2495 int len, int dtype)
2497 int err;
2499 if (do_fail(desc, lnum, 1))
2500 return -EIO;
2501 err = ubi_leb_change(desc, lnum, buf, len, dtype);
2502 if (err)
2503 return err;
2504 if (do_fail(desc, lnum, 1))
2505 return -EIO;
2506 return 0;
2509 int dbg_leb_erase(struct ubi_volume_desc *desc, int lnum)
2511 int err;
2513 if (do_fail(desc, lnum, 0))
2514 return -EIO;
2515 err = ubi_leb_erase(desc, lnum);
2516 if (err)
2517 return err;
2518 if (do_fail(desc, lnum, 0))
2519 return -EIO;
2520 return 0;
2523 int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum)
2525 int err;
2527 if (do_fail(desc, lnum, 0))
2528 return -EIO;
2529 err = ubi_leb_unmap(desc, lnum);
2530 if (err)
2531 return err;
2532 if (do_fail(desc, lnum, 0))
2533 return -EIO;
2534 return 0;
2537 int dbg_is_mapped(struct ubi_volume_desc *desc, int lnum)
2539 if (in_failure_mode(desc))
2540 return -EIO;
2541 return ubi_is_mapped(desc, lnum);
2544 int dbg_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
2546 int err;
2548 if (do_fail(desc, lnum, 0))
2549 return -EIO;
2550 err = ubi_leb_map(desc, lnum, dtype);
2551 if (err)
2552 return err;
2553 if (do_fail(desc, lnum, 0))
2554 return -EIO;
2555 return 0;
2559 * ubifs_debugging_init - initialize UBIFS debugging.
2560 * @c: UBIFS file-system description object
2562 * This function initializes debugging-related data for the file system.
2563 * Returns zero in case of success and a negative error code in case of
2564 * failure.
2566 int ubifs_debugging_init(struct ubifs_info *c)
2568 c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
2569 if (!c->dbg)
2570 return -ENOMEM;
2572 c->dbg->buf = vmalloc(c->leb_size);
2573 if (!c->dbg->buf)
2574 goto out;
2576 failure_mode_init(c);
2577 return 0;
2579 out:
2580 kfree(c->dbg);
2581 return -ENOMEM;
2585 * ubifs_debugging_exit - free debugging data.
2586 * @c: UBIFS file-system description object
2588 void ubifs_debugging_exit(struct ubifs_info *c)
2590 failure_mode_exit(c);
2591 vfree(c->dbg->buf);
2592 kfree(c->dbg);
2596 * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
2597 * contain the stuff specific to particular file-system mounts.
2599 static struct dentry *dfs_rootdir;
2602 * dbg_debugfs_init - initialize debugfs file-system.
2604 * UBIFS uses debugfs file-system to expose various debugging knobs to
2605 * user-space. This function creates "ubifs" directory in the debugfs
2606 * file-system. Returns zero in case of success and a negative error code in
2607 * case of failure.
2609 int dbg_debugfs_init(void)
2611 dfs_rootdir = debugfs_create_dir("ubifs", NULL);
2612 if (IS_ERR(dfs_rootdir)) {
2613 int err = PTR_ERR(dfs_rootdir);
2614 ubifs_err("cannot create \"ubifs\" debugfs directory, "
2615 "error %d\n", err);
2616 return err;
2619 return 0;
2623 * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
2625 void dbg_debugfs_exit(void)
2627 debugfs_remove(dfs_rootdir);
2630 static int open_debugfs_file(struct inode *inode, struct file *file)
2632 file->private_data = inode->i_private;
2633 return 0;
2636 static ssize_t write_debugfs_file(struct file *file, const char __user *buf,
2637 size_t count, loff_t *ppos)
2639 struct ubifs_info *c = file->private_data;
2640 struct ubifs_debug_info *d = c->dbg;
2642 if (file->f_path.dentry == d->dfs_dump_lprops)
2643 dbg_dump_lprops(c);
2644 else if (file->f_path.dentry == d->dfs_dump_budg) {
2645 spin_lock(&c->space_lock);
2646 dbg_dump_budg(c);
2647 spin_unlock(&c->space_lock);
2648 } else if (file->f_path.dentry == d->dfs_dump_tnc) {
2649 mutex_lock(&c->tnc_mutex);
2650 dbg_dump_tnc(c);
2651 mutex_unlock(&c->tnc_mutex);
2652 } else
2653 return -EINVAL;
2655 *ppos += count;
2656 return count;
2659 static const struct file_operations dfs_fops = {
2660 .open = open_debugfs_file,
2661 .write = write_debugfs_file,
2662 .owner = THIS_MODULE,
2666 * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
2667 * @c: UBIFS file-system description object
2669 * This function creates all debugfs files for this instance of UBIFS. Returns
2670 * zero in case of success and a negative error code in case of failure.
2672 * Note, the only reason we have not merged this function with the
2673 * 'ubifs_debugging_init()' function is because it is better to initialize
2674 * debugfs interfaces at the very end of the mount process, and remove them at
2675 * the very beginning of the mount process.
2677 int dbg_debugfs_init_fs(struct ubifs_info *c)
2679 int err;
2680 const char *fname;
2681 struct dentry *dent;
2682 struct ubifs_debug_info *d = c->dbg;
2684 sprintf(d->dfs_dir_name, "ubi%d_%d", c->vi.ubi_num, c->vi.vol_id);
2685 d->dfs_dir = debugfs_create_dir(d->dfs_dir_name, dfs_rootdir);
2686 if (IS_ERR(d->dfs_dir)) {
2687 err = PTR_ERR(d->dfs_dir);
2688 ubifs_err("cannot create \"%s\" debugfs directory, error %d\n",
2689 d->dfs_dir_name, err);
2690 goto out;
2693 fname = "dump_lprops";
2694 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2695 if (IS_ERR(dent))
2696 goto out_remove;
2697 d->dfs_dump_lprops = dent;
2699 fname = "dump_budg";
2700 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2701 if (IS_ERR(dent))
2702 goto out_remove;
2703 d->dfs_dump_budg = dent;
2705 fname = "dump_tnc";
2706 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2707 if (IS_ERR(dent))
2708 goto out_remove;
2709 d->dfs_dump_tnc = dent;
2711 return 0;
2713 out_remove:
2714 err = PTR_ERR(dent);
2715 ubifs_err("cannot create \"%s\" debugfs directory, error %d\n",
2716 fname, err);
2717 debugfs_remove_recursive(d->dfs_dir);
2718 out:
2719 return err;
2723 * dbg_debugfs_exit_fs - remove all debugfs files.
2724 * @c: UBIFS file-system description object
2726 void dbg_debugfs_exit_fs(struct ubifs_info *c)
2728 debugfs_remove_recursive(c->dbg->dfs_dir);
2731 #endif /* CONFIG_UBIFS_FS_DEBUG */