UBIFS: remove custom list of superblocks
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ubifs / debug.c
blob1e880cedefa41756a62e2ecb6832bc87d39a9ca0
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 #include "ubifs.h"
31 #include <linux/module.h>
32 #include <linux/debugfs.h>
33 #include <linux/math64.h>
34 #include <linux/uaccess.h>
36 #ifdef CONFIG_UBIFS_FS_DEBUG
38 DEFINE_SPINLOCK(dbg_lock);
40 static char dbg_key_buf0[128];
41 static char dbg_key_buf1[128];
43 static const char *get_key_fmt(int fmt)
45 switch (fmt) {
46 case UBIFS_SIMPLE_KEY_FMT:
47 return "simple";
48 default:
49 return "unknown/invalid format";
53 static const char *get_key_hash(int hash)
55 switch (hash) {
56 case UBIFS_KEY_HASH_R5:
57 return "R5";
58 case UBIFS_KEY_HASH_TEST:
59 return "test";
60 default:
61 return "unknown/invalid name hash";
65 static const char *get_key_type(int type)
67 switch (type) {
68 case UBIFS_INO_KEY:
69 return "inode";
70 case UBIFS_DENT_KEY:
71 return "direntry";
72 case UBIFS_XENT_KEY:
73 return "xentry";
74 case UBIFS_DATA_KEY:
75 return "data";
76 case UBIFS_TRUN_KEY:
77 return "truncate";
78 default:
79 return "unknown/invalid key";
83 static const char *get_dent_type(int type)
85 switch (type) {
86 case UBIFS_ITYPE_REG:
87 return "file";
88 case UBIFS_ITYPE_DIR:
89 return "dir";
90 case UBIFS_ITYPE_LNK:
91 return "symlink";
92 case UBIFS_ITYPE_BLK:
93 return "blkdev";
94 case UBIFS_ITYPE_CHR:
95 return "char dev";
96 case UBIFS_ITYPE_FIFO:
97 return "fifo";
98 case UBIFS_ITYPE_SOCK:
99 return "socket";
100 default:
101 return "unknown/invalid type";
105 static void sprintf_key(const struct ubifs_info *c, const union ubifs_key *key,
106 char *buffer)
108 char *p = buffer;
109 int type = key_type(c, key);
111 if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
112 switch (type) {
113 case UBIFS_INO_KEY:
114 sprintf(p, "(%lu, %s)", (unsigned long)key_inum(c, key),
115 get_key_type(type));
116 break;
117 case UBIFS_DENT_KEY:
118 case UBIFS_XENT_KEY:
119 sprintf(p, "(%lu, %s, %#08x)",
120 (unsigned long)key_inum(c, key),
121 get_key_type(type), key_hash(c, key));
122 break;
123 case UBIFS_DATA_KEY:
124 sprintf(p, "(%lu, %s, %u)",
125 (unsigned long)key_inum(c, key),
126 get_key_type(type), key_block(c, key));
127 break;
128 case UBIFS_TRUN_KEY:
129 sprintf(p, "(%lu, %s)",
130 (unsigned long)key_inum(c, key),
131 get_key_type(type));
132 break;
133 default:
134 sprintf(p, "(bad key type: %#08x, %#08x)",
135 key->u32[0], key->u32[1]);
137 } else
138 sprintf(p, "bad key format %d", c->key_fmt);
141 const char *dbg_key_str0(const struct ubifs_info *c, const union ubifs_key *key)
143 /* dbg_lock must be held */
144 sprintf_key(c, key, dbg_key_buf0);
145 return dbg_key_buf0;
148 const char *dbg_key_str1(const struct ubifs_info *c, const union ubifs_key *key)
150 /* dbg_lock must be held */
151 sprintf_key(c, key, dbg_key_buf1);
152 return dbg_key_buf1;
155 const char *dbg_ntype(int type)
157 switch (type) {
158 case UBIFS_PAD_NODE:
159 return "padding node";
160 case UBIFS_SB_NODE:
161 return "superblock node";
162 case UBIFS_MST_NODE:
163 return "master node";
164 case UBIFS_REF_NODE:
165 return "reference node";
166 case UBIFS_INO_NODE:
167 return "inode node";
168 case UBIFS_DENT_NODE:
169 return "direntry node";
170 case UBIFS_XENT_NODE:
171 return "xentry node";
172 case UBIFS_DATA_NODE:
173 return "data node";
174 case UBIFS_TRUN_NODE:
175 return "truncate node";
176 case UBIFS_IDX_NODE:
177 return "indexing node";
178 case UBIFS_CS_NODE:
179 return "commit start node";
180 case UBIFS_ORPH_NODE:
181 return "orphan node";
182 default:
183 return "unknown node";
187 static const char *dbg_gtype(int type)
189 switch (type) {
190 case UBIFS_NO_NODE_GROUP:
191 return "no node group";
192 case UBIFS_IN_NODE_GROUP:
193 return "in node group";
194 case UBIFS_LAST_OF_NODE_GROUP:
195 return "last of node group";
196 default:
197 return "unknown";
201 const char *dbg_cstate(int cmt_state)
203 switch (cmt_state) {
204 case COMMIT_RESTING:
205 return "commit resting";
206 case COMMIT_BACKGROUND:
207 return "background commit requested";
208 case COMMIT_REQUIRED:
209 return "commit required";
210 case COMMIT_RUNNING_BACKGROUND:
211 return "BACKGROUND commit running";
212 case COMMIT_RUNNING_REQUIRED:
213 return "commit running and required";
214 case COMMIT_BROKEN:
215 return "broken commit";
216 default:
217 return "unknown commit state";
221 const char *dbg_jhead(int jhead)
223 switch (jhead) {
224 case GCHD:
225 return "0 (GC)";
226 case BASEHD:
227 return "1 (base)";
228 case DATAHD:
229 return "2 (data)";
230 default:
231 return "unknown journal head";
235 static void dump_ch(const struct ubifs_ch *ch)
237 printk(KERN_DEBUG "\tmagic %#x\n", le32_to_cpu(ch->magic));
238 printk(KERN_DEBUG "\tcrc %#x\n", le32_to_cpu(ch->crc));
239 printk(KERN_DEBUG "\tnode_type %d (%s)\n", ch->node_type,
240 dbg_ntype(ch->node_type));
241 printk(KERN_DEBUG "\tgroup_type %d (%s)\n", ch->group_type,
242 dbg_gtype(ch->group_type));
243 printk(KERN_DEBUG "\tsqnum %llu\n",
244 (unsigned long long)le64_to_cpu(ch->sqnum));
245 printk(KERN_DEBUG "\tlen %u\n", le32_to_cpu(ch->len));
248 void dbg_dump_inode(struct ubifs_info *c, const struct inode *inode)
250 const struct ubifs_inode *ui = ubifs_inode(inode);
251 struct qstr nm = { .name = NULL };
252 union ubifs_key key;
253 struct ubifs_dent_node *dent, *pdent = NULL;
254 int count = 2;
256 printk(KERN_DEBUG "Dump in-memory inode:");
257 printk(KERN_DEBUG "\tinode %lu\n", inode->i_ino);
258 printk(KERN_DEBUG "\tsize %llu\n",
259 (unsigned long long)i_size_read(inode));
260 printk(KERN_DEBUG "\tnlink %u\n", inode->i_nlink);
261 printk(KERN_DEBUG "\tuid %u\n", (unsigned int)inode->i_uid);
262 printk(KERN_DEBUG "\tgid %u\n", (unsigned int)inode->i_gid);
263 printk(KERN_DEBUG "\tatime %u.%u\n",
264 (unsigned int)inode->i_atime.tv_sec,
265 (unsigned int)inode->i_atime.tv_nsec);
266 printk(KERN_DEBUG "\tmtime %u.%u\n",
267 (unsigned int)inode->i_mtime.tv_sec,
268 (unsigned int)inode->i_mtime.tv_nsec);
269 printk(KERN_DEBUG "\tctime %u.%u\n",
270 (unsigned int)inode->i_ctime.tv_sec,
271 (unsigned int)inode->i_ctime.tv_nsec);
272 printk(KERN_DEBUG "\tcreat_sqnum %llu\n", ui->creat_sqnum);
273 printk(KERN_DEBUG "\txattr_size %u\n", ui->xattr_size);
274 printk(KERN_DEBUG "\txattr_cnt %u\n", ui->xattr_cnt);
275 printk(KERN_DEBUG "\txattr_names %u\n", ui->xattr_names);
276 printk(KERN_DEBUG "\tdirty %u\n", ui->dirty);
277 printk(KERN_DEBUG "\txattr %u\n", ui->xattr);
278 printk(KERN_DEBUG "\tbulk_read %u\n", ui->xattr);
279 printk(KERN_DEBUG "\tsynced_i_size %llu\n",
280 (unsigned long long)ui->synced_i_size);
281 printk(KERN_DEBUG "\tui_size %llu\n",
282 (unsigned long long)ui->ui_size);
283 printk(KERN_DEBUG "\tflags %d\n", ui->flags);
284 printk(KERN_DEBUG "\tcompr_type %d\n", ui->compr_type);
285 printk(KERN_DEBUG "\tlast_page_read %lu\n", ui->last_page_read);
286 printk(KERN_DEBUG "\tread_in_a_row %lu\n", ui->read_in_a_row);
287 printk(KERN_DEBUG "\tdata_len %d\n", ui->data_len);
289 if (!S_ISDIR(inode->i_mode))
290 return;
292 printk(KERN_DEBUG "List of directory entries:\n");
293 ubifs_assert(!mutex_is_locked(&c->tnc_mutex));
295 lowest_dent_key(c, &key, inode->i_ino);
296 while (1) {
297 dent = ubifs_tnc_next_ent(c, &key, &nm);
298 if (IS_ERR(dent)) {
299 if (PTR_ERR(dent) != -ENOENT)
300 printk(KERN_DEBUG "error %ld\n", PTR_ERR(dent));
301 break;
304 printk(KERN_DEBUG "\t%d: %s (%s)\n",
305 count++, dent->name, get_dent_type(dent->type));
307 nm.name = dent->name;
308 nm.len = le16_to_cpu(dent->nlen);
309 kfree(pdent);
310 pdent = dent;
311 key_read(c, &dent->key, &key);
313 kfree(pdent);
316 void dbg_dump_node(const struct ubifs_info *c, const void *node)
318 int i, n;
319 union ubifs_key key;
320 const struct ubifs_ch *ch = node;
322 if (dbg_is_tst_rcvry(c))
323 return;
325 /* If the magic is incorrect, just hexdump the first bytes */
326 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
327 printk(KERN_DEBUG "Not a node, first %zu bytes:", UBIFS_CH_SZ);
328 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
329 (void *)node, UBIFS_CH_SZ, 1);
330 return;
333 spin_lock(&dbg_lock);
334 dump_ch(node);
336 switch (ch->node_type) {
337 case UBIFS_PAD_NODE:
339 const struct ubifs_pad_node *pad = node;
341 printk(KERN_DEBUG "\tpad_len %u\n",
342 le32_to_cpu(pad->pad_len));
343 break;
345 case UBIFS_SB_NODE:
347 const struct ubifs_sb_node *sup = node;
348 unsigned int sup_flags = le32_to_cpu(sup->flags);
350 printk(KERN_DEBUG "\tkey_hash %d (%s)\n",
351 (int)sup->key_hash, get_key_hash(sup->key_hash));
352 printk(KERN_DEBUG "\tkey_fmt %d (%s)\n",
353 (int)sup->key_fmt, get_key_fmt(sup->key_fmt));
354 printk(KERN_DEBUG "\tflags %#x\n", sup_flags);
355 printk(KERN_DEBUG "\t big_lpt %u\n",
356 !!(sup_flags & UBIFS_FLG_BIGLPT));
357 printk(KERN_DEBUG "\t space_fixup %u\n",
358 !!(sup_flags & UBIFS_FLG_SPACE_FIXUP));
359 printk(KERN_DEBUG "\tmin_io_size %u\n",
360 le32_to_cpu(sup->min_io_size));
361 printk(KERN_DEBUG "\tleb_size %u\n",
362 le32_to_cpu(sup->leb_size));
363 printk(KERN_DEBUG "\tleb_cnt %u\n",
364 le32_to_cpu(sup->leb_cnt));
365 printk(KERN_DEBUG "\tmax_leb_cnt %u\n",
366 le32_to_cpu(sup->max_leb_cnt));
367 printk(KERN_DEBUG "\tmax_bud_bytes %llu\n",
368 (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
369 printk(KERN_DEBUG "\tlog_lebs %u\n",
370 le32_to_cpu(sup->log_lebs));
371 printk(KERN_DEBUG "\tlpt_lebs %u\n",
372 le32_to_cpu(sup->lpt_lebs));
373 printk(KERN_DEBUG "\torph_lebs %u\n",
374 le32_to_cpu(sup->orph_lebs));
375 printk(KERN_DEBUG "\tjhead_cnt %u\n",
376 le32_to_cpu(sup->jhead_cnt));
377 printk(KERN_DEBUG "\tfanout %u\n",
378 le32_to_cpu(sup->fanout));
379 printk(KERN_DEBUG "\tlsave_cnt %u\n",
380 le32_to_cpu(sup->lsave_cnt));
381 printk(KERN_DEBUG "\tdefault_compr %u\n",
382 (int)le16_to_cpu(sup->default_compr));
383 printk(KERN_DEBUG "\trp_size %llu\n",
384 (unsigned long long)le64_to_cpu(sup->rp_size));
385 printk(KERN_DEBUG "\trp_uid %u\n",
386 le32_to_cpu(sup->rp_uid));
387 printk(KERN_DEBUG "\trp_gid %u\n",
388 le32_to_cpu(sup->rp_gid));
389 printk(KERN_DEBUG "\tfmt_version %u\n",
390 le32_to_cpu(sup->fmt_version));
391 printk(KERN_DEBUG "\ttime_gran %u\n",
392 le32_to_cpu(sup->time_gran));
393 printk(KERN_DEBUG "\tUUID %pUB\n",
394 sup->uuid);
395 break;
397 case UBIFS_MST_NODE:
399 const struct ubifs_mst_node *mst = node;
401 printk(KERN_DEBUG "\thighest_inum %llu\n",
402 (unsigned long long)le64_to_cpu(mst->highest_inum));
403 printk(KERN_DEBUG "\tcommit number %llu\n",
404 (unsigned long long)le64_to_cpu(mst->cmt_no));
405 printk(KERN_DEBUG "\tflags %#x\n",
406 le32_to_cpu(mst->flags));
407 printk(KERN_DEBUG "\tlog_lnum %u\n",
408 le32_to_cpu(mst->log_lnum));
409 printk(KERN_DEBUG "\troot_lnum %u\n",
410 le32_to_cpu(mst->root_lnum));
411 printk(KERN_DEBUG "\troot_offs %u\n",
412 le32_to_cpu(mst->root_offs));
413 printk(KERN_DEBUG "\troot_len %u\n",
414 le32_to_cpu(mst->root_len));
415 printk(KERN_DEBUG "\tgc_lnum %u\n",
416 le32_to_cpu(mst->gc_lnum));
417 printk(KERN_DEBUG "\tihead_lnum %u\n",
418 le32_to_cpu(mst->ihead_lnum));
419 printk(KERN_DEBUG "\tihead_offs %u\n",
420 le32_to_cpu(mst->ihead_offs));
421 printk(KERN_DEBUG "\tindex_size %llu\n",
422 (unsigned long long)le64_to_cpu(mst->index_size));
423 printk(KERN_DEBUG "\tlpt_lnum %u\n",
424 le32_to_cpu(mst->lpt_lnum));
425 printk(KERN_DEBUG "\tlpt_offs %u\n",
426 le32_to_cpu(mst->lpt_offs));
427 printk(KERN_DEBUG "\tnhead_lnum %u\n",
428 le32_to_cpu(mst->nhead_lnum));
429 printk(KERN_DEBUG "\tnhead_offs %u\n",
430 le32_to_cpu(mst->nhead_offs));
431 printk(KERN_DEBUG "\tltab_lnum %u\n",
432 le32_to_cpu(mst->ltab_lnum));
433 printk(KERN_DEBUG "\tltab_offs %u\n",
434 le32_to_cpu(mst->ltab_offs));
435 printk(KERN_DEBUG "\tlsave_lnum %u\n",
436 le32_to_cpu(mst->lsave_lnum));
437 printk(KERN_DEBUG "\tlsave_offs %u\n",
438 le32_to_cpu(mst->lsave_offs));
439 printk(KERN_DEBUG "\tlscan_lnum %u\n",
440 le32_to_cpu(mst->lscan_lnum));
441 printk(KERN_DEBUG "\tleb_cnt %u\n",
442 le32_to_cpu(mst->leb_cnt));
443 printk(KERN_DEBUG "\tempty_lebs %u\n",
444 le32_to_cpu(mst->empty_lebs));
445 printk(KERN_DEBUG "\tidx_lebs %u\n",
446 le32_to_cpu(mst->idx_lebs));
447 printk(KERN_DEBUG "\ttotal_free %llu\n",
448 (unsigned long long)le64_to_cpu(mst->total_free));
449 printk(KERN_DEBUG "\ttotal_dirty %llu\n",
450 (unsigned long long)le64_to_cpu(mst->total_dirty));
451 printk(KERN_DEBUG "\ttotal_used %llu\n",
452 (unsigned long long)le64_to_cpu(mst->total_used));
453 printk(KERN_DEBUG "\ttotal_dead %llu\n",
454 (unsigned long long)le64_to_cpu(mst->total_dead));
455 printk(KERN_DEBUG "\ttotal_dark %llu\n",
456 (unsigned long long)le64_to_cpu(mst->total_dark));
457 break;
459 case UBIFS_REF_NODE:
461 const struct ubifs_ref_node *ref = node;
463 printk(KERN_DEBUG "\tlnum %u\n",
464 le32_to_cpu(ref->lnum));
465 printk(KERN_DEBUG "\toffs %u\n",
466 le32_to_cpu(ref->offs));
467 printk(KERN_DEBUG "\tjhead %u\n",
468 le32_to_cpu(ref->jhead));
469 break;
471 case UBIFS_INO_NODE:
473 const struct ubifs_ino_node *ino = node;
475 key_read(c, &ino->key, &key);
476 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
477 printk(KERN_DEBUG "\tcreat_sqnum %llu\n",
478 (unsigned long long)le64_to_cpu(ino->creat_sqnum));
479 printk(KERN_DEBUG "\tsize %llu\n",
480 (unsigned long long)le64_to_cpu(ino->size));
481 printk(KERN_DEBUG "\tnlink %u\n",
482 le32_to_cpu(ino->nlink));
483 printk(KERN_DEBUG "\tatime %lld.%u\n",
484 (long long)le64_to_cpu(ino->atime_sec),
485 le32_to_cpu(ino->atime_nsec));
486 printk(KERN_DEBUG "\tmtime %lld.%u\n",
487 (long long)le64_to_cpu(ino->mtime_sec),
488 le32_to_cpu(ino->mtime_nsec));
489 printk(KERN_DEBUG "\tctime %lld.%u\n",
490 (long long)le64_to_cpu(ino->ctime_sec),
491 le32_to_cpu(ino->ctime_nsec));
492 printk(KERN_DEBUG "\tuid %u\n",
493 le32_to_cpu(ino->uid));
494 printk(KERN_DEBUG "\tgid %u\n",
495 le32_to_cpu(ino->gid));
496 printk(KERN_DEBUG "\tmode %u\n",
497 le32_to_cpu(ino->mode));
498 printk(KERN_DEBUG "\tflags %#x\n",
499 le32_to_cpu(ino->flags));
500 printk(KERN_DEBUG "\txattr_cnt %u\n",
501 le32_to_cpu(ino->xattr_cnt));
502 printk(KERN_DEBUG "\txattr_size %u\n",
503 le32_to_cpu(ino->xattr_size));
504 printk(KERN_DEBUG "\txattr_names %u\n",
505 le32_to_cpu(ino->xattr_names));
506 printk(KERN_DEBUG "\tcompr_type %#x\n",
507 (int)le16_to_cpu(ino->compr_type));
508 printk(KERN_DEBUG "\tdata len %u\n",
509 le32_to_cpu(ino->data_len));
510 break;
512 case UBIFS_DENT_NODE:
513 case UBIFS_XENT_NODE:
515 const struct ubifs_dent_node *dent = node;
516 int nlen = le16_to_cpu(dent->nlen);
518 key_read(c, &dent->key, &key);
519 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
520 printk(KERN_DEBUG "\tinum %llu\n",
521 (unsigned long long)le64_to_cpu(dent->inum));
522 printk(KERN_DEBUG "\ttype %d\n", (int)dent->type);
523 printk(KERN_DEBUG "\tnlen %d\n", nlen);
524 printk(KERN_DEBUG "\tname ");
526 if (nlen > UBIFS_MAX_NLEN)
527 printk(KERN_DEBUG "(bad name length, not printing, "
528 "bad or corrupted node)");
529 else {
530 for (i = 0; i < nlen && dent->name[i]; i++)
531 printk(KERN_CONT "%c", dent->name[i]);
533 printk(KERN_CONT "\n");
535 break;
537 case UBIFS_DATA_NODE:
539 const struct ubifs_data_node *dn = node;
540 int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
542 key_read(c, &dn->key, &key);
543 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
544 printk(KERN_DEBUG "\tsize %u\n",
545 le32_to_cpu(dn->size));
546 printk(KERN_DEBUG "\tcompr_typ %d\n",
547 (int)le16_to_cpu(dn->compr_type));
548 printk(KERN_DEBUG "\tdata size %d\n",
549 dlen);
550 printk(KERN_DEBUG "\tdata:\n");
551 print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET, 32, 1,
552 (void *)&dn->data, dlen, 0);
553 break;
555 case UBIFS_TRUN_NODE:
557 const struct ubifs_trun_node *trun = node;
559 printk(KERN_DEBUG "\tinum %u\n",
560 le32_to_cpu(trun->inum));
561 printk(KERN_DEBUG "\told_size %llu\n",
562 (unsigned long long)le64_to_cpu(trun->old_size));
563 printk(KERN_DEBUG "\tnew_size %llu\n",
564 (unsigned long long)le64_to_cpu(trun->new_size));
565 break;
567 case UBIFS_IDX_NODE:
569 const struct ubifs_idx_node *idx = node;
571 n = le16_to_cpu(idx->child_cnt);
572 printk(KERN_DEBUG "\tchild_cnt %d\n", n);
573 printk(KERN_DEBUG "\tlevel %d\n",
574 (int)le16_to_cpu(idx->level));
575 printk(KERN_DEBUG "\tBranches:\n");
577 for (i = 0; i < n && i < c->fanout - 1; i++) {
578 const struct ubifs_branch *br;
580 br = ubifs_idx_branch(c, idx, i);
581 key_read(c, &br->key, &key);
582 printk(KERN_DEBUG "\t%d: LEB %d:%d len %d key %s\n",
583 i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
584 le32_to_cpu(br->len), DBGKEY(&key));
586 break;
588 case UBIFS_CS_NODE:
589 break;
590 case UBIFS_ORPH_NODE:
592 const struct ubifs_orph_node *orph = node;
594 printk(KERN_DEBUG "\tcommit number %llu\n",
595 (unsigned long long)
596 le64_to_cpu(orph->cmt_no) & LLONG_MAX);
597 printk(KERN_DEBUG "\tlast node flag %llu\n",
598 (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
599 n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
600 printk(KERN_DEBUG "\t%d orphan inode numbers:\n", n);
601 for (i = 0; i < n; i++)
602 printk(KERN_DEBUG "\t ino %llu\n",
603 (unsigned long long)le64_to_cpu(orph->inos[i]));
604 break;
606 default:
607 printk(KERN_DEBUG "node type %d was not recognized\n",
608 (int)ch->node_type);
610 spin_unlock(&dbg_lock);
613 void dbg_dump_budget_req(const struct ubifs_budget_req *req)
615 spin_lock(&dbg_lock);
616 printk(KERN_DEBUG "Budgeting request: new_ino %d, dirtied_ino %d\n",
617 req->new_ino, req->dirtied_ino);
618 printk(KERN_DEBUG "\tnew_ino_d %d, dirtied_ino_d %d\n",
619 req->new_ino_d, req->dirtied_ino_d);
620 printk(KERN_DEBUG "\tnew_page %d, dirtied_page %d\n",
621 req->new_page, req->dirtied_page);
622 printk(KERN_DEBUG "\tnew_dent %d, mod_dent %d\n",
623 req->new_dent, req->mod_dent);
624 printk(KERN_DEBUG "\tidx_growth %d\n", req->idx_growth);
625 printk(KERN_DEBUG "\tdata_growth %d dd_growth %d\n",
626 req->data_growth, req->dd_growth);
627 spin_unlock(&dbg_lock);
630 void dbg_dump_lstats(const struct ubifs_lp_stats *lst)
632 spin_lock(&dbg_lock);
633 printk(KERN_DEBUG "(pid %d) Lprops statistics: empty_lebs %d, "
634 "idx_lebs %d\n", current->pid, lst->empty_lebs, lst->idx_lebs);
635 printk(KERN_DEBUG "\ttaken_empty_lebs %d, total_free %lld, "
636 "total_dirty %lld\n", lst->taken_empty_lebs, lst->total_free,
637 lst->total_dirty);
638 printk(KERN_DEBUG "\ttotal_used %lld, total_dark %lld, "
639 "total_dead %lld\n", lst->total_used, lst->total_dark,
640 lst->total_dead);
641 spin_unlock(&dbg_lock);
644 void dbg_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)
646 int i;
647 struct rb_node *rb;
648 struct ubifs_bud *bud;
649 struct ubifs_gced_idx_leb *idx_gc;
650 long long available, outstanding, free;
652 spin_lock(&c->space_lock);
653 spin_lock(&dbg_lock);
654 printk(KERN_DEBUG "(pid %d) Budgeting info: data budget sum %lld, "
655 "total budget sum %lld\n", current->pid,
656 bi->data_growth + bi->dd_growth,
657 bi->data_growth + bi->dd_growth + bi->idx_growth);
658 printk(KERN_DEBUG "\tbudg_data_growth %lld, budg_dd_growth %lld, "
659 "budg_idx_growth %lld\n", bi->data_growth, bi->dd_growth,
660 bi->idx_growth);
661 printk(KERN_DEBUG "\tmin_idx_lebs %d, old_idx_sz %llu, "
662 "uncommitted_idx %lld\n", bi->min_idx_lebs, bi->old_idx_sz,
663 bi->uncommitted_idx);
664 printk(KERN_DEBUG "\tpage_budget %d, inode_budget %d, dent_budget %d\n",
665 bi->page_budget, bi->inode_budget, bi->dent_budget);
666 printk(KERN_DEBUG "\tnospace %u, nospace_rp %u\n",
667 bi->nospace, bi->nospace_rp);
668 printk(KERN_DEBUG "\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
669 c->dark_wm, c->dead_wm, c->max_idx_node_sz);
671 if (bi != &c->bi)
673 * If we are dumping saved budgeting data, do not print
674 * additional information which is about the current state, not
675 * the old one which corresponded to the saved budgeting data.
677 goto out_unlock;
679 printk(KERN_DEBUG "\tfreeable_cnt %d, calc_idx_sz %lld, idx_gc_cnt %d\n",
680 c->freeable_cnt, c->calc_idx_sz, c->idx_gc_cnt);
681 printk(KERN_DEBUG "\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, "
682 "clean_zn_cnt %ld\n", atomic_long_read(&c->dirty_pg_cnt),
683 atomic_long_read(&c->dirty_zn_cnt),
684 atomic_long_read(&c->clean_zn_cnt));
685 printk(KERN_DEBUG "\tgc_lnum %d, ihead_lnum %d\n",
686 c->gc_lnum, c->ihead_lnum);
688 /* If we are in R/O mode, journal heads do not exist */
689 if (c->jheads)
690 for (i = 0; i < c->jhead_cnt; i++)
691 printk(KERN_DEBUG "\tjhead %s\t LEB %d\n",
692 dbg_jhead(c->jheads[i].wbuf.jhead),
693 c->jheads[i].wbuf.lnum);
694 for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
695 bud = rb_entry(rb, struct ubifs_bud, rb);
696 printk(KERN_DEBUG "\tbud LEB %d\n", bud->lnum);
698 list_for_each_entry(bud, &c->old_buds, list)
699 printk(KERN_DEBUG "\told bud LEB %d\n", bud->lnum);
700 list_for_each_entry(idx_gc, &c->idx_gc, list)
701 printk(KERN_DEBUG "\tGC'ed idx LEB %d unmap %d\n",
702 idx_gc->lnum, idx_gc->unmap);
703 printk(KERN_DEBUG "\tcommit state %d\n", c->cmt_state);
705 /* Print budgeting predictions */
706 available = ubifs_calc_available(c, c->bi.min_idx_lebs);
707 outstanding = c->bi.data_growth + c->bi.dd_growth;
708 free = ubifs_get_free_space_nolock(c);
709 printk(KERN_DEBUG "Budgeting predictions:\n");
710 printk(KERN_DEBUG "\tavailable: %lld, outstanding %lld, free %lld\n",
711 available, outstanding, free);
712 out_unlock:
713 spin_unlock(&dbg_lock);
714 spin_unlock(&c->space_lock);
717 void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
719 int i, spc, dark = 0, dead = 0;
720 struct rb_node *rb;
721 struct ubifs_bud *bud;
723 spc = lp->free + lp->dirty;
724 if (spc < c->dead_wm)
725 dead = spc;
726 else
727 dark = ubifs_calc_dark(c, spc);
729 if (lp->flags & LPROPS_INDEX)
730 printk(KERN_DEBUG "LEB %-7d free %-8d dirty %-8d used %-8d "
731 "free + dirty %-8d flags %#x (", lp->lnum, lp->free,
732 lp->dirty, c->leb_size - spc, spc, lp->flags);
733 else
734 printk(KERN_DEBUG "LEB %-7d free %-8d dirty %-8d used %-8d "
735 "free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d "
736 "flags %#-4x (", lp->lnum, lp->free, lp->dirty,
737 c->leb_size - spc, spc, dark, dead,
738 (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags);
740 if (lp->flags & LPROPS_TAKEN) {
741 if (lp->flags & LPROPS_INDEX)
742 printk(KERN_CONT "index, taken");
743 else
744 printk(KERN_CONT "taken");
745 } else {
746 const char *s;
748 if (lp->flags & LPROPS_INDEX) {
749 switch (lp->flags & LPROPS_CAT_MASK) {
750 case LPROPS_DIRTY_IDX:
751 s = "dirty index";
752 break;
753 case LPROPS_FRDI_IDX:
754 s = "freeable index";
755 break;
756 default:
757 s = "index";
759 } else {
760 switch (lp->flags & LPROPS_CAT_MASK) {
761 case LPROPS_UNCAT:
762 s = "not categorized";
763 break;
764 case LPROPS_DIRTY:
765 s = "dirty";
766 break;
767 case LPROPS_FREE:
768 s = "free";
769 break;
770 case LPROPS_EMPTY:
771 s = "empty";
772 break;
773 case LPROPS_FREEABLE:
774 s = "freeable";
775 break;
776 default:
777 s = NULL;
778 break;
781 printk(KERN_CONT "%s", s);
784 for (rb = rb_first((struct rb_root *)&c->buds); rb; rb = rb_next(rb)) {
785 bud = rb_entry(rb, struct ubifs_bud, rb);
786 if (bud->lnum == lp->lnum) {
787 int head = 0;
788 for (i = 0; i < c->jhead_cnt; i++) {
790 * Note, if we are in R/O mode or in the middle
791 * of mounting/re-mounting, the write-buffers do
792 * not exist.
794 if (c->jheads &&
795 lp->lnum == c->jheads[i].wbuf.lnum) {
796 printk(KERN_CONT ", jhead %s",
797 dbg_jhead(i));
798 head = 1;
801 if (!head)
802 printk(KERN_CONT ", bud of jhead %s",
803 dbg_jhead(bud->jhead));
806 if (lp->lnum == c->gc_lnum)
807 printk(KERN_CONT ", GC LEB");
808 printk(KERN_CONT ")\n");
811 void dbg_dump_lprops(struct ubifs_info *c)
813 int lnum, err;
814 struct ubifs_lprops lp;
815 struct ubifs_lp_stats lst;
817 printk(KERN_DEBUG "(pid %d) start dumping LEB properties\n",
818 current->pid);
819 ubifs_get_lp_stats(c, &lst);
820 dbg_dump_lstats(&lst);
822 for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
823 err = ubifs_read_one_lp(c, lnum, &lp);
824 if (err)
825 ubifs_err("cannot read lprops for LEB %d", lnum);
827 dbg_dump_lprop(c, &lp);
829 printk(KERN_DEBUG "(pid %d) finish dumping LEB properties\n",
830 current->pid);
833 void dbg_dump_lpt_info(struct ubifs_info *c)
835 int i;
837 spin_lock(&dbg_lock);
838 printk(KERN_DEBUG "(pid %d) dumping LPT information\n", current->pid);
839 printk(KERN_DEBUG "\tlpt_sz: %lld\n", c->lpt_sz);
840 printk(KERN_DEBUG "\tpnode_sz: %d\n", c->pnode_sz);
841 printk(KERN_DEBUG "\tnnode_sz: %d\n", c->nnode_sz);
842 printk(KERN_DEBUG "\tltab_sz: %d\n", c->ltab_sz);
843 printk(KERN_DEBUG "\tlsave_sz: %d\n", c->lsave_sz);
844 printk(KERN_DEBUG "\tbig_lpt: %d\n", c->big_lpt);
845 printk(KERN_DEBUG "\tlpt_hght: %d\n", c->lpt_hght);
846 printk(KERN_DEBUG "\tpnode_cnt: %d\n", c->pnode_cnt);
847 printk(KERN_DEBUG "\tnnode_cnt: %d\n", c->nnode_cnt);
848 printk(KERN_DEBUG "\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt);
849 printk(KERN_DEBUG "\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt);
850 printk(KERN_DEBUG "\tlsave_cnt: %d\n", c->lsave_cnt);
851 printk(KERN_DEBUG "\tspace_bits: %d\n", c->space_bits);
852 printk(KERN_DEBUG "\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
853 printk(KERN_DEBUG "\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
854 printk(KERN_DEBUG "\tlpt_spc_bits: %d\n", c->lpt_spc_bits);
855 printk(KERN_DEBUG "\tpcnt_bits: %d\n", c->pcnt_bits);
856 printk(KERN_DEBUG "\tlnum_bits: %d\n", c->lnum_bits);
857 printk(KERN_DEBUG "\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
858 printk(KERN_DEBUG "\tLPT head is at %d:%d\n",
859 c->nhead_lnum, c->nhead_offs);
860 printk(KERN_DEBUG "\tLPT ltab is at %d:%d\n",
861 c->ltab_lnum, c->ltab_offs);
862 if (c->big_lpt)
863 printk(KERN_DEBUG "\tLPT lsave is at %d:%d\n",
864 c->lsave_lnum, c->lsave_offs);
865 for (i = 0; i < c->lpt_lebs; i++)
866 printk(KERN_DEBUG "\tLPT LEB %d free %d dirty %d tgc %d "
867 "cmt %d\n", i + c->lpt_first, c->ltab[i].free,
868 c->ltab[i].dirty, c->ltab[i].tgc, c->ltab[i].cmt);
869 spin_unlock(&dbg_lock);
872 void dbg_dump_leb(const struct ubifs_info *c, int lnum)
874 struct ubifs_scan_leb *sleb;
875 struct ubifs_scan_node *snod;
876 void *buf;
878 if (dbg_is_tst_rcvry(c))
879 return;
881 printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n",
882 current->pid, lnum);
884 buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
885 if (!buf) {
886 ubifs_err("cannot allocate memory for dumping LEB %d", lnum);
887 return;
890 sleb = ubifs_scan(c, lnum, 0, buf, 0);
891 if (IS_ERR(sleb)) {
892 ubifs_err("scan error %d", (int)PTR_ERR(sleb));
893 goto out;
896 printk(KERN_DEBUG "LEB %d has %d nodes ending at %d\n", lnum,
897 sleb->nodes_cnt, sleb->endpt);
899 list_for_each_entry(snod, &sleb->nodes, list) {
900 cond_resched();
901 printk(KERN_DEBUG "Dumping node at LEB %d:%d len %d\n", lnum,
902 snod->offs, snod->len);
903 dbg_dump_node(c, snod->node);
906 printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n",
907 current->pid, lnum);
908 ubifs_scan_destroy(sleb);
910 out:
911 vfree(buf);
912 return;
915 void dbg_dump_znode(const struct ubifs_info *c,
916 const struct ubifs_znode *znode)
918 int n;
919 const struct ubifs_zbranch *zbr;
921 spin_lock(&dbg_lock);
922 if (znode->parent)
923 zbr = &znode->parent->zbranch[znode->iip];
924 else
925 zbr = &c->zroot;
927 printk(KERN_DEBUG "znode %p, LEB %d:%d len %d parent %p iip %d level %d"
928 " child_cnt %d flags %lx\n", znode, zbr->lnum, zbr->offs,
929 zbr->len, znode->parent, znode->iip, znode->level,
930 znode->child_cnt, znode->flags);
932 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
933 spin_unlock(&dbg_lock);
934 return;
937 printk(KERN_DEBUG "zbranches:\n");
938 for (n = 0; n < znode->child_cnt; n++) {
939 zbr = &znode->zbranch[n];
940 if (znode->level > 0)
941 printk(KERN_DEBUG "\t%d: znode %p LEB %d:%d len %d key "
942 "%s\n", n, zbr->znode, zbr->lnum,
943 zbr->offs, zbr->len,
944 DBGKEY(&zbr->key));
945 else
946 printk(KERN_DEBUG "\t%d: LNC %p LEB %d:%d len %d key "
947 "%s\n", n, zbr->znode, zbr->lnum,
948 zbr->offs, zbr->len,
949 DBGKEY(&zbr->key));
951 spin_unlock(&dbg_lock);
954 void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
956 int i;
958 printk(KERN_DEBUG "(pid %d) start dumping heap cat %d (%d elements)\n",
959 current->pid, cat, heap->cnt);
960 for (i = 0; i < heap->cnt; i++) {
961 struct ubifs_lprops *lprops = heap->arr[i];
963 printk(KERN_DEBUG "\t%d. LEB %d hpos %d free %d dirty %d "
964 "flags %d\n", i, lprops->lnum, lprops->hpos,
965 lprops->free, lprops->dirty, lprops->flags);
967 printk(KERN_DEBUG "(pid %d) finish dumping heap\n", current->pid);
970 void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
971 struct ubifs_nnode *parent, int iip)
973 int i;
975 printk(KERN_DEBUG "(pid %d) dumping pnode:\n", current->pid);
976 printk(KERN_DEBUG "\taddress %zx parent %zx cnext %zx\n",
977 (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
978 printk(KERN_DEBUG "\tflags %lu iip %d level %d num %d\n",
979 pnode->flags, iip, pnode->level, pnode->num);
980 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
981 struct ubifs_lprops *lp = &pnode->lprops[i];
983 printk(KERN_DEBUG "\t%d: free %d dirty %d flags %d lnum %d\n",
984 i, lp->free, lp->dirty, lp->flags, lp->lnum);
988 void dbg_dump_tnc(struct ubifs_info *c)
990 struct ubifs_znode *znode;
991 int level;
993 printk(KERN_DEBUG "\n");
994 printk(KERN_DEBUG "(pid %d) start dumping TNC tree\n", current->pid);
995 znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
996 level = znode->level;
997 printk(KERN_DEBUG "== Level %d ==\n", level);
998 while (znode) {
999 if (level != znode->level) {
1000 level = znode->level;
1001 printk(KERN_DEBUG "== Level %d ==\n", level);
1003 dbg_dump_znode(c, znode);
1004 znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
1006 printk(KERN_DEBUG "(pid %d) finish dumping TNC tree\n", current->pid);
1009 static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
1010 void *priv)
1012 dbg_dump_znode(c, znode);
1013 return 0;
1017 * dbg_dump_index - dump the on-flash index.
1018 * @c: UBIFS file-system description object
1020 * This function dumps whole UBIFS indexing B-tree, unlike 'dbg_dump_tnc()'
1021 * which dumps only in-memory znodes and does not read znodes which from flash.
1023 void dbg_dump_index(struct ubifs_info *c)
1025 dbg_walk_index(c, NULL, dump_znode, NULL);
1029 * dbg_save_space_info - save information about flash space.
1030 * @c: UBIFS file-system description object
1032 * This function saves information about UBIFS free space, dirty space, etc, in
1033 * order to check it later.
1035 void dbg_save_space_info(struct ubifs_info *c)
1037 struct ubifs_debug_info *d = c->dbg;
1038 int freeable_cnt;
1040 spin_lock(&c->space_lock);
1041 memcpy(&d->saved_lst, &c->lst, sizeof(struct ubifs_lp_stats));
1042 memcpy(&d->saved_bi, &c->bi, sizeof(struct ubifs_budg_info));
1043 d->saved_idx_gc_cnt = c->idx_gc_cnt;
1046 * We use a dirty hack here and zero out @c->freeable_cnt, because it
1047 * affects the free space calculations, and UBIFS might not know about
1048 * all freeable eraseblocks. Indeed, we know about freeable eraseblocks
1049 * only when we read their lprops, and we do this only lazily, upon the
1050 * need. So at any given point of time @c->freeable_cnt might be not
1051 * exactly accurate.
1053 * Just one example about the issue we hit when we did not zero
1054 * @c->freeable_cnt.
1055 * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the
1056 * amount of free space in @d->saved_free
1057 * 2. We re-mount R/W, which makes UBIFS to read the "lsave"
1058 * information from flash, where we cache LEBs from various
1059 * categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()'
1060 * -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()'
1061 * -> 'ubifs_get_pnode()' -> 'update_cats()'
1062 * -> 'ubifs_add_to_cat()').
1063 * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt
1064 * becomes %1.
1065 * 4. We calculate the amount of free space when the re-mount is
1066 * finished in 'dbg_check_space_info()' and it does not match
1067 * @d->saved_free.
1069 freeable_cnt = c->freeable_cnt;
1070 c->freeable_cnt = 0;
1071 d->saved_free = ubifs_get_free_space_nolock(c);
1072 c->freeable_cnt = freeable_cnt;
1073 spin_unlock(&c->space_lock);
1077 * dbg_check_space_info - check flash space information.
1078 * @c: UBIFS file-system description object
1080 * This function compares current flash space information with the information
1081 * which was saved when the 'dbg_save_space_info()' function was called.
1082 * Returns zero if the information has not changed, and %-EINVAL it it has
1083 * changed.
1085 int dbg_check_space_info(struct ubifs_info *c)
1087 struct ubifs_debug_info *d = c->dbg;
1088 struct ubifs_lp_stats lst;
1089 long long free;
1090 int freeable_cnt;
1092 spin_lock(&c->space_lock);
1093 freeable_cnt = c->freeable_cnt;
1094 c->freeable_cnt = 0;
1095 free = ubifs_get_free_space_nolock(c);
1096 c->freeable_cnt = freeable_cnt;
1097 spin_unlock(&c->space_lock);
1099 if (free != d->saved_free) {
1100 ubifs_err("free space changed from %lld to %lld",
1101 d->saved_free, free);
1102 goto out;
1105 return 0;
1107 out:
1108 ubifs_msg("saved lprops statistics dump");
1109 dbg_dump_lstats(&d->saved_lst);
1110 ubifs_msg("saved budgeting info dump");
1111 dbg_dump_budg(c, &d->saved_bi);
1112 ubifs_msg("saved idx_gc_cnt %d", d->saved_idx_gc_cnt);
1113 ubifs_msg("current lprops statistics dump");
1114 ubifs_get_lp_stats(c, &lst);
1115 dbg_dump_lstats(&lst);
1116 ubifs_msg("current budgeting info dump");
1117 dbg_dump_budg(c, &c->bi);
1118 dump_stack();
1119 return -EINVAL;
1123 * dbg_check_synced_i_size - check synchronized inode size.
1124 * @c: UBIFS file-system description object
1125 * @inode: inode to check
1127 * If inode is clean, synchronized inode size has to be equivalent to current
1128 * inode size. This function has to be called only for locked inodes (@i_mutex
1129 * has to be locked). Returns %0 if synchronized inode size if correct, and
1130 * %-EINVAL if not.
1132 int dbg_check_synced_i_size(const struct ubifs_info *c, struct inode *inode)
1134 int err = 0;
1135 struct ubifs_inode *ui = ubifs_inode(inode);
1137 if (!dbg_is_chk_gen(c))
1138 return 0;
1139 if (!S_ISREG(inode->i_mode))
1140 return 0;
1142 mutex_lock(&ui->ui_mutex);
1143 spin_lock(&ui->ui_lock);
1144 if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
1145 ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode "
1146 "is clean", ui->ui_size, ui->synced_i_size);
1147 ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
1148 inode->i_mode, i_size_read(inode));
1149 dbg_dump_stack();
1150 err = -EINVAL;
1152 spin_unlock(&ui->ui_lock);
1153 mutex_unlock(&ui->ui_mutex);
1154 return err;
1158 * dbg_check_dir - check directory inode size and link count.
1159 * @c: UBIFS file-system description object
1160 * @dir: the directory to calculate size for
1161 * @size: the result is returned here
1163 * This function makes sure that directory size and link count are correct.
1164 * Returns zero in case of success and a negative error code in case of
1165 * failure.
1167 * Note, it is good idea to make sure the @dir->i_mutex is locked before
1168 * calling this function.
1170 int dbg_check_dir(struct ubifs_info *c, const struct inode *dir)
1172 unsigned int nlink = 2;
1173 union ubifs_key key;
1174 struct ubifs_dent_node *dent, *pdent = NULL;
1175 struct qstr nm = { .name = NULL };
1176 loff_t size = UBIFS_INO_NODE_SZ;
1178 if (!dbg_is_chk_gen(c))
1179 return 0;
1181 if (!S_ISDIR(dir->i_mode))
1182 return 0;
1184 lowest_dent_key(c, &key, dir->i_ino);
1185 while (1) {
1186 int err;
1188 dent = ubifs_tnc_next_ent(c, &key, &nm);
1189 if (IS_ERR(dent)) {
1190 err = PTR_ERR(dent);
1191 if (err == -ENOENT)
1192 break;
1193 return err;
1196 nm.name = dent->name;
1197 nm.len = le16_to_cpu(dent->nlen);
1198 size += CALC_DENT_SIZE(nm.len);
1199 if (dent->type == UBIFS_ITYPE_DIR)
1200 nlink += 1;
1201 kfree(pdent);
1202 pdent = dent;
1203 key_read(c, &dent->key, &key);
1205 kfree(pdent);
1207 if (i_size_read(dir) != size) {
1208 ubifs_err("directory inode %lu has size %llu, "
1209 "but calculated size is %llu", dir->i_ino,
1210 (unsigned long long)i_size_read(dir),
1211 (unsigned long long)size);
1212 dbg_dump_inode(c, dir);
1213 dump_stack();
1214 return -EINVAL;
1216 if (dir->i_nlink != nlink) {
1217 ubifs_err("directory inode %lu has nlink %u, but calculated "
1218 "nlink is %u", dir->i_ino, dir->i_nlink, nlink);
1219 dbg_dump_inode(c, dir);
1220 dump_stack();
1221 return -EINVAL;
1224 return 0;
1228 * dbg_check_key_order - make sure that colliding keys are properly ordered.
1229 * @c: UBIFS file-system description object
1230 * @zbr1: first zbranch
1231 * @zbr2: following zbranch
1233 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
1234 * names of the direntries/xentries which are referred by the keys. This
1235 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
1236 * sure the name of direntry/xentry referred by @zbr1 is less than
1237 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
1238 * and a negative error code in case of failure.
1240 static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
1241 struct ubifs_zbranch *zbr2)
1243 int err, nlen1, nlen2, cmp;
1244 struct ubifs_dent_node *dent1, *dent2;
1245 union ubifs_key key;
1247 ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
1248 dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1249 if (!dent1)
1250 return -ENOMEM;
1251 dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1252 if (!dent2) {
1253 err = -ENOMEM;
1254 goto out_free;
1257 err = ubifs_tnc_read_node(c, zbr1, dent1);
1258 if (err)
1259 goto out_free;
1260 err = ubifs_validate_entry(c, dent1);
1261 if (err)
1262 goto out_free;
1264 err = ubifs_tnc_read_node(c, zbr2, dent2);
1265 if (err)
1266 goto out_free;
1267 err = ubifs_validate_entry(c, dent2);
1268 if (err)
1269 goto out_free;
1271 /* Make sure node keys are the same as in zbranch */
1272 err = 1;
1273 key_read(c, &dent1->key, &key);
1274 if (keys_cmp(c, &zbr1->key, &key)) {
1275 dbg_err("1st entry at %d:%d has key %s", zbr1->lnum,
1276 zbr1->offs, DBGKEY(&key));
1277 dbg_err("but it should have key %s according to tnc",
1278 DBGKEY(&zbr1->key));
1279 dbg_dump_node(c, dent1);
1280 goto out_free;
1283 key_read(c, &dent2->key, &key);
1284 if (keys_cmp(c, &zbr2->key, &key)) {
1285 dbg_err("2nd entry at %d:%d has key %s", zbr1->lnum,
1286 zbr1->offs, DBGKEY(&key));
1287 dbg_err("but it should have key %s according to tnc",
1288 DBGKEY(&zbr2->key));
1289 dbg_dump_node(c, dent2);
1290 goto out_free;
1293 nlen1 = le16_to_cpu(dent1->nlen);
1294 nlen2 = le16_to_cpu(dent2->nlen);
1296 cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
1297 if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
1298 err = 0;
1299 goto out_free;
1301 if (cmp == 0 && nlen1 == nlen2)
1302 dbg_err("2 xent/dent nodes with the same name");
1303 else
1304 dbg_err("bad order of colliding key %s",
1305 DBGKEY(&key));
1307 ubifs_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs);
1308 dbg_dump_node(c, dent1);
1309 ubifs_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs);
1310 dbg_dump_node(c, dent2);
1312 out_free:
1313 kfree(dent2);
1314 kfree(dent1);
1315 return err;
1319 * dbg_check_znode - check if znode is all right.
1320 * @c: UBIFS file-system description object
1321 * @zbr: zbranch which points to this znode
1323 * This function makes sure that znode referred to by @zbr is all right.
1324 * Returns zero if it is, and %-EINVAL if it is not.
1326 static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
1328 struct ubifs_znode *znode = zbr->znode;
1329 struct ubifs_znode *zp = znode->parent;
1330 int n, err, cmp;
1332 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
1333 err = 1;
1334 goto out;
1336 if (znode->level < 0) {
1337 err = 2;
1338 goto out;
1340 if (znode->iip < 0 || znode->iip >= c->fanout) {
1341 err = 3;
1342 goto out;
1345 if (zbr->len == 0)
1346 /* Only dirty zbranch may have no on-flash nodes */
1347 if (!ubifs_zn_dirty(znode)) {
1348 err = 4;
1349 goto out;
1352 if (ubifs_zn_dirty(znode)) {
1354 * If znode is dirty, its parent has to be dirty as well. The
1355 * order of the operation is important, so we have to have
1356 * memory barriers.
1358 smp_mb();
1359 if (zp && !ubifs_zn_dirty(zp)) {
1361 * The dirty flag is atomic and is cleared outside the
1362 * TNC mutex, so znode's dirty flag may now have
1363 * been cleared. The child is always cleared before the
1364 * parent, so we just need to check again.
1366 smp_mb();
1367 if (ubifs_zn_dirty(znode)) {
1368 err = 5;
1369 goto out;
1374 if (zp) {
1375 const union ubifs_key *min, *max;
1377 if (znode->level != zp->level - 1) {
1378 err = 6;
1379 goto out;
1382 /* Make sure the 'parent' pointer in our znode is correct */
1383 err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
1384 if (!err) {
1385 /* This zbranch does not exist in the parent */
1386 err = 7;
1387 goto out;
1390 if (znode->iip >= zp->child_cnt) {
1391 err = 8;
1392 goto out;
1395 if (znode->iip != n) {
1396 /* This may happen only in case of collisions */
1397 if (keys_cmp(c, &zp->zbranch[n].key,
1398 &zp->zbranch[znode->iip].key)) {
1399 err = 9;
1400 goto out;
1402 n = znode->iip;
1406 * Make sure that the first key in our znode is greater than or
1407 * equal to the key in the pointing zbranch.
1409 min = &zbr->key;
1410 cmp = keys_cmp(c, min, &znode->zbranch[0].key);
1411 if (cmp == 1) {
1412 err = 10;
1413 goto out;
1416 if (n + 1 < zp->child_cnt) {
1417 max = &zp->zbranch[n + 1].key;
1420 * Make sure the last key in our znode is less or
1421 * equivalent than the key in the zbranch which goes
1422 * after our pointing zbranch.
1424 cmp = keys_cmp(c, max,
1425 &znode->zbranch[znode->child_cnt - 1].key);
1426 if (cmp == -1) {
1427 err = 11;
1428 goto out;
1431 } else {
1432 /* This may only be root znode */
1433 if (zbr != &c->zroot) {
1434 err = 12;
1435 goto out;
1440 * Make sure that next key is greater or equivalent then the previous
1441 * one.
1443 for (n = 1; n < znode->child_cnt; n++) {
1444 cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
1445 &znode->zbranch[n].key);
1446 if (cmp > 0) {
1447 err = 13;
1448 goto out;
1450 if (cmp == 0) {
1451 /* This can only be keys with colliding hash */
1452 if (!is_hash_key(c, &znode->zbranch[n].key)) {
1453 err = 14;
1454 goto out;
1457 if (znode->level != 0 || c->replaying)
1458 continue;
1461 * Colliding keys should follow binary order of
1462 * corresponding xentry/dentry names.
1464 err = dbg_check_key_order(c, &znode->zbranch[n - 1],
1465 &znode->zbranch[n]);
1466 if (err < 0)
1467 return err;
1468 if (err) {
1469 err = 15;
1470 goto out;
1475 for (n = 0; n < znode->child_cnt; n++) {
1476 if (!znode->zbranch[n].znode &&
1477 (znode->zbranch[n].lnum == 0 ||
1478 znode->zbranch[n].len == 0)) {
1479 err = 16;
1480 goto out;
1483 if (znode->zbranch[n].lnum != 0 &&
1484 znode->zbranch[n].len == 0) {
1485 err = 17;
1486 goto out;
1489 if (znode->zbranch[n].lnum == 0 &&
1490 znode->zbranch[n].len != 0) {
1491 err = 18;
1492 goto out;
1495 if (znode->zbranch[n].lnum == 0 &&
1496 znode->zbranch[n].offs != 0) {
1497 err = 19;
1498 goto out;
1501 if (znode->level != 0 && znode->zbranch[n].znode)
1502 if (znode->zbranch[n].znode->parent != znode) {
1503 err = 20;
1504 goto out;
1508 return 0;
1510 out:
1511 ubifs_err("failed, error %d", err);
1512 ubifs_msg("dump of the znode");
1513 dbg_dump_znode(c, znode);
1514 if (zp) {
1515 ubifs_msg("dump of the parent znode");
1516 dbg_dump_znode(c, zp);
1518 dump_stack();
1519 return -EINVAL;
1523 * dbg_check_tnc - check TNC tree.
1524 * @c: UBIFS file-system description object
1525 * @extra: do extra checks that are possible at start commit
1527 * This function traverses whole TNC tree and checks every znode. Returns zero
1528 * if everything is all right and %-EINVAL if something is wrong with TNC.
1530 int dbg_check_tnc(struct ubifs_info *c, int extra)
1532 struct ubifs_znode *znode;
1533 long clean_cnt = 0, dirty_cnt = 0;
1534 int err, last;
1536 if (!dbg_is_chk_index(c))
1537 return 0;
1539 ubifs_assert(mutex_is_locked(&c->tnc_mutex));
1540 if (!c->zroot.znode)
1541 return 0;
1543 znode = ubifs_tnc_postorder_first(c->zroot.znode);
1544 while (1) {
1545 struct ubifs_znode *prev;
1546 struct ubifs_zbranch *zbr;
1548 if (!znode->parent)
1549 zbr = &c->zroot;
1550 else
1551 zbr = &znode->parent->zbranch[znode->iip];
1553 err = dbg_check_znode(c, zbr);
1554 if (err)
1555 return err;
1557 if (extra) {
1558 if (ubifs_zn_dirty(znode))
1559 dirty_cnt += 1;
1560 else
1561 clean_cnt += 1;
1564 prev = znode;
1565 znode = ubifs_tnc_postorder_next(znode);
1566 if (!znode)
1567 break;
1570 * If the last key of this znode is equivalent to the first key
1571 * of the next znode (collision), then check order of the keys.
1573 last = prev->child_cnt - 1;
1574 if (prev->level == 0 && znode->level == 0 && !c->replaying &&
1575 !keys_cmp(c, &prev->zbranch[last].key,
1576 &znode->zbranch[0].key)) {
1577 err = dbg_check_key_order(c, &prev->zbranch[last],
1578 &znode->zbranch[0]);
1579 if (err < 0)
1580 return err;
1581 if (err) {
1582 ubifs_msg("first znode");
1583 dbg_dump_znode(c, prev);
1584 ubifs_msg("second znode");
1585 dbg_dump_znode(c, znode);
1586 return -EINVAL;
1591 if (extra) {
1592 if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
1593 ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld",
1594 atomic_long_read(&c->clean_zn_cnt),
1595 clean_cnt);
1596 return -EINVAL;
1598 if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
1599 ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld",
1600 atomic_long_read(&c->dirty_zn_cnt),
1601 dirty_cnt);
1602 return -EINVAL;
1606 return 0;
1610 * dbg_walk_index - walk the on-flash index.
1611 * @c: UBIFS file-system description object
1612 * @leaf_cb: called for each leaf node
1613 * @znode_cb: called for each indexing node
1614 * @priv: private data which is passed to callbacks
1616 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1617 * node and @znode_cb for each indexing node. Returns zero in case of success
1618 * and a negative error code in case of failure.
1620 * It would be better if this function removed every znode it pulled to into
1621 * the TNC, so that the behavior more closely matched the non-debugging
1622 * behavior.
1624 int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1625 dbg_znode_callback znode_cb, void *priv)
1627 int err;
1628 struct ubifs_zbranch *zbr;
1629 struct ubifs_znode *znode, *child;
1631 mutex_lock(&c->tnc_mutex);
1632 /* If the root indexing node is not in TNC - pull it */
1633 if (!c->zroot.znode) {
1634 c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1635 if (IS_ERR(c->zroot.znode)) {
1636 err = PTR_ERR(c->zroot.znode);
1637 c->zroot.znode = NULL;
1638 goto out_unlock;
1643 * We are going to traverse the indexing tree in the postorder manner.
1644 * Go down and find the leftmost indexing node where we are going to
1645 * start from.
1647 znode = c->zroot.znode;
1648 while (znode->level > 0) {
1649 zbr = &znode->zbranch[0];
1650 child = zbr->znode;
1651 if (!child) {
1652 child = ubifs_load_znode(c, zbr, znode, 0);
1653 if (IS_ERR(child)) {
1654 err = PTR_ERR(child);
1655 goto out_unlock;
1657 zbr->znode = child;
1660 znode = child;
1663 /* Iterate over all indexing nodes */
1664 while (1) {
1665 int idx;
1667 cond_resched();
1669 if (znode_cb) {
1670 err = znode_cb(c, znode, priv);
1671 if (err) {
1672 ubifs_err("znode checking function returned "
1673 "error %d", err);
1674 dbg_dump_znode(c, znode);
1675 goto out_dump;
1678 if (leaf_cb && znode->level == 0) {
1679 for (idx = 0; idx < znode->child_cnt; idx++) {
1680 zbr = &znode->zbranch[idx];
1681 err = leaf_cb(c, zbr, priv);
1682 if (err) {
1683 ubifs_err("leaf checking function "
1684 "returned error %d, for leaf "
1685 "at LEB %d:%d",
1686 err, zbr->lnum, zbr->offs);
1687 goto out_dump;
1692 if (!znode->parent)
1693 break;
1695 idx = znode->iip + 1;
1696 znode = znode->parent;
1697 if (idx < znode->child_cnt) {
1698 /* Switch to the next index in the parent */
1699 zbr = &znode->zbranch[idx];
1700 child = zbr->znode;
1701 if (!child) {
1702 child = ubifs_load_znode(c, zbr, znode, idx);
1703 if (IS_ERR(child)) {
1704 err = PTR_ERR(child);
1705 goto out_unlock;
1707 zbr->znode = child;
1709 znode = child;
1710 } else
1712 * This is the last child, switch to the parent and
1713 * continue.
1715 continue;
1717 /* Go to the lowest leftmost znode in the new sub-tree */
1718 while (znode->level > 0) {
1719 zbr = &znode->zbranch[0];
1720 child = zbr->znode;
1721 if (!child) {
1722 child = ubifs_load_znode(c, zbr, znode, 0);
1723 if (IS_ERR(child)) {
1724 err = PTR_ERR(child);
1725 goto out_unlock;
1727 zbr->znode = child;
1729 znode = child;
1733 mutex_unlock(&c->tnc_mutex);
1734 return 0;
1736 out_dump:
1737 if (znode->parent)
1738 zbr = &znode->parent->zbranch[znode->iip];
1739 else
1740 zbr = &c->zroot;
1741 ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1742 dbg_dump_znode(c, znode);
1743 out_unlock:
1744 mutex_unlock(&c->tnc_mutex);
1745 return err;
1749 * add_size - add znode size to partially calculated index size.
1750 * @c: UBIFS file-system description object
1751 * @znode: znode to add size for
1752 * @priv: partially calculated index size
1754 * This is a helper function for 'dbg_check_idx_size()' which is called for
1755 * every indexing node and adds its size to the 'long long' variable pointed to
1756 * by @priv.
1758 static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1760 long long *idx_size = priv;
1761 int add;
1763 add = ubifs_idx_node_sz(c, znode->child_cnt);
1764 add = ALIGN(add, 8);
1765 *idx_size += add;
1766 return 0;
1770 * dbg_check_idx_size - check index size.
1771 * @c: UBIFS file-system description object
1772 * @idx_size: size to check
1774 * This function walks the UBIFS index, calculates its size and checks that the
1775 * size is equivalent to @idx_size. Returns zero in case of success and a
1776 * negative error code in case of failure.
1778 int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
1780 int err;
1781 long long calc = 0;
1783 if (!dbg_is_chk_index(c))
1784 return 0;
1786 err = dbg_walk_index(c, NULL, add_size, &calc);
1787 if (err) {
1788 ubifs_err("error %d while walking the index", err);
1789 return err;
1792 if (calc != idx_size) {
1793 ubifs_err("index size check failed: calculated size is %lld, "
1794 "should be %lld", calc, idx_size);
1795 dump_stack();
1796 return -EINVAL;
1799 return 0;
1803 * struct fsck_inode - information about an inode used when checking the file-system.
1804 * @rb: link in the RB-tree of inodes
1805 * @inum: inode number
1806 * @mode: inode type, permissions, etc
1807 * @nlink: inode link count
1808 * @xattr_cnt: count of extended attributes
1809 * @references: how many directory/xattr entries refer this inode (calculated
1810 * while walking the index)
1811 * @calc_cnt: for directory inode count of child directories
1812 * @size: inode size (read from on-flash inode)
1813 * @xattr_sz: summary size of all extended attributes (read from on-flash
1814 * inode)
1815 * @calc_sz: for directories calculated directory size
1816 * @calc_xcnt: count of extended attributes
1817 * @calc_xsz: calculated summary size of all extended attributes
1818 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1819 * inode (read from on-flash inode)
1820 * @calc_xnms: calculated sum of lengths of all extended attribute names
1822 struct fsck_inode {
1823 struct rb_node rb;
1824 ino_t inum;
1825 umode_t mode;
1826 unsigned int nlink;
1827 unsigned int xattr_cnt;
1828 int references;
1829 int calc_cnt;
1830 long long size;
1831 unsigned int xattr_sz;
1832 long long calc_sz;
1833 long long calc_xcnt;
1834 long long calc_xsz;
1835 unsigned int xattr_nms;
1836 long long calc_xnms;
1840 * struct fsck_data - private FS checking information.
1841 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1843 struct fsck_data {
1844 struct rb_root inodes;
1848 * add_inode - add inode information to RB-tree of inodes.
1849 * @c: UBIFS file-system description object
1850 * @fsckd: FS checking information
1851 * @ino: raw UBIFS inode to add
1853 * This is a helper function for 'check_leaf()' which adds information about
1854 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1855 * case of success and a negative error code in case of failure.
1857 static struct fsck_inode *add_inode(struct ubifs_info *c,
1858 struct fsck_data *fsckd,
1859 struct ubifs_ino_node *ino)
1861 struct rb_node **p, *parent = NULL;
1862 struct fsck_inode *fscki;
1863 ino_t inum = key_inum_flash(c, &ino->key);
1864 struct inode *inode;
1865 struct ubifs_inode *ui;
1867 p = &fsckd->inodes.rb_node;
1868 while (*p) {
1869 parent = *p;
1870 fscki = rb_entry(parent, struct fsck_inode, rb);
1871 if (inum < fscki->inum)
1872 p = &(*p)->rb_left;
1873 else if (inum > fscki->inum)
1874 p = &(*p)->rb_right;
1875 else
1876 return fscki;
1879 if (inum > c->highest_inum) {
1880 ubifs_err("too high inode number, max. is %lu",
1881 (unsigned long)c->highest_inum);
1882 return ERR_PTR(-EINVAL);
1885 fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
1886 if (!fscki)
1887 return ERR_PTR(-ENOMEM);
1889 inode = ilookup(c->vfs_sb, inum);
1891 fscki->inum = inum;
1893 * If the inode is present in the VFS inode cache, use it instead of
1894 * the on-flash inode which might be out-of-date. E.g., the size might
1895 * be out-of-date. If we do not do this, the following may happen, for
1896 * example:
1897 * 1. A power cut happens
1898 * 2. We mount the file-system R/O, the replay process fixes up the
1899 * inode size in the VFS cache, but on on-flash.
1900 * 3. 'check_leaf()' fails because it hits a data node beyond inode
1901 * size.
1903 if (!inode) {
1904 fscki->nlink = le32_to_cpu(ino->nlink);
1905 fscki->size = le64_to_cpu(ino->size);
1906 fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
1907 fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
1908 fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
1909 fscki->mode = le32_to_cpu(ino->mode);
1910 } else {
1911 ui = ubifs_inode(inode);
1912 fscki->nlink = inode->i_nlink;
1913 fscki->size = inode->i_size;
1914 fscki->xattr_cnt = ui->xattr_cnt;
1915 fscki->xattr_sz = ui->xattr_size;
1916 fscki->xattr_nms = ui->xattr_names;
1917 fscki->mode = inode->i_mode;
1918 iput(inode);
1921 if (S_ISDIR(fscki->mode)) {
1922 fscki->calc_sz = UBIFS_INO_NODE_SZ;
1923 fscki->calc_cnt = 2;
1926 rb_link_node(&fscki->rb, parent, p);
1927 rb_insert_color(&fscki->rb, &fsckd->inodes);
1929 return fscki;
1933 * search_inode - search inode in the RB-tree of inodes.
1934 * @fsckd: FS checking information
1935 * @inum: inode number to search
1937 * This is a helper function for 'check_leaf()' which searches inode @inum in
1938 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1939 * the inode was not found.
1941 static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
1943 struct rb_node *p;
1944 struct fsck_inode *fscki;
1946 p = fsckd->inodes.rb_node;
1947 while (p) {
1948 fscki = rb_entry(p, struct fsck_inode, rb);
1949 if (inum < fscki->inum)
1950 p = p->rb_left;
1951 else if (inum > fscki->inum)
1952 p = p->rb_right;
1953 else
1954 return fscki;
1956 return NULL;
1960 * read_add_inode - read inode node and add it to RB-tree of inodes.
1961 * @c: UBIFS file-system description object
1962 * @fsckd: FS checking information
1963 * @inum: inode number to read
1965 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1966 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1967 * information pointer in case of success and a negative error code in case of
1968 * failure.
1970 static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1971 struct fsck_data *fsckd, ino_t inum)
1973 int n, err;
1974 union ubifs_key key;
1975 struct ubifs_znode *znode;
1976 struct ubifs_zbranch *zbr;
1977 struct ubifs_ino_node *ino;
1978 struct fsck_inode *fscki;
1980 fscki = search_inode(fsckd, inum);
1981 if (fscki)
1982 return fscki;
1984 ino_key_init(c, &key, inum);
1985 err = ubifs_lookup_level0(c, &key, &znode, &n);
1986 if (!err) {
1987 ubifs_err("inode %lu not found in index", (unsigned long)inum);
1988 return ERR_PTR(-ENOENT);
1989 } else if (err < 0) {
1990 ubifs_err("error %d while looking up inode %lu",
1991 err, (unsigned long)inum);
1992 return ERR_PTR(err);
1995 zbr = &znode->zbranch[n];
1996 if (zbr->len < UBIFS_INO_NODE_SZ) {
1997 ubifs_err("bad node %lu node length %d",
1998 (unsigned long)inum, zbr->len);
1999 return ERR_PTR(-EINVAL);
2002 ino = kmalloc(zbr->len, GFP_NOFS);
2003 if (!ino)
2004 return ERR_PTR(-ENOMEM);
2006 err = ubifs_tnc_read_node(c, zbr, ino);
2007 if (err) {
2008 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2009 zbr->lnum, zbr->offs, err);
2010 kfree(ino);
2011 return ERR_PTR(err);
2014 fscki = add_inode(c, fsckd, ino);
2015 kfree(ino);
2016 if (IS_ERR(fscki)) {
2017 ubifs_err("error %ld while adding inode %lu node",
2018 PTR_ERR(fscki), (unsigned long)inum);
2019 return fscki;
2022 return fscki;
2026 * check_leaf - check leaf node.
2027 * @c: UBIFS file-system description object
2028 * @zbr: zbranch of the leaf node to check
2029 * @priv: FS checking information
2031 * This is a helper function for 'dbg_check_filesystem()' which is called for
2032 * every single leaf node while walking the indexing tree. It checks that the
2033 * leaf node referred from the indexing tree exists, has correct CRC, and does
2034 * some other basic validation. This function is also responsible for building
2035 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
2036 * calculates reference count, size, etc for each inode in order to later
2037 * compare them to the information stored inside the inodes and detect possible
2038 * inconsistencies. Returns zero in case of success and a negative error code
2039 * in case of failure.
2041 static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
2042 void *priv)
2044 ino_t inum;
2045 void *node;
2046 struct ubifs_ch *ch;
2047 int err, type = key_type(c, &zbr->key);
2048 struct fsck_inode *fscki;
2050 if (zbr->len < UBIFS_CH_SZ) {
2051 ubifs_err("bad leaf length %d (LEB %d:%d)",
2052 zbr->len, zbr->lnum, zbr->offs);
2053 return -EINVAL;
2056 node = kmalloc(zbr->len, GFP_NOFS);
2057 if (!node)
2058 return -ENOMEM;
2060 err = ubifs_tnc_read_node(c, zbr, node);
2061 if (err) {
2062 ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
2063 zbr->lnum, zbr->offs, err);
2064 goto out_free;
2067 /* If this is an inode node, add it to RB-tree of inodes */
2068 if (type == UBIFS_INO_KEY) {
2069 fscki = add_inode(c, priv, node);
2070 if (IS_ERR(fscki)) {
2071 err = PTR_ERR(fscki);
2072 ubifs_err("error %d while adding inode node", err);
2073 goto out_dump;
2075 goto out;
2078 if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
2079 type != UBIFS_DATA_KEY) {
2080 ubifs_err("unexpected node type %d at LEB %d:%d",
2081 type, zbr->lnum, zbr->offs);
2082 err = -EINVAL;
2083 goto out_free;
2086 ch = node;
2087 if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
2088 ubifs_err("too high sequence number, max. is %llu",
2089 c->max_sqnum);
2090 err = -EINVAL;
2091 goto out_dump;
2094 if (type == UBIFS_DATA_KEY) {
2095 long long blk_offs;
2096 struct ubifs_data_node *dn = node;
2099 * Search the inode node this data node belongs to and insert
2100 * it to the RB-tree of inodes.
2102 inum = key_inum_flash(c, &dn->key);
2103 fscki = read_add_inode(c, priv, inum);
2104 if (IS_ERR(fscki)) {
2105 err = PTR_ERR(fscki);
2106 ubifs_err("error %d while processing data node and "
2107 "trying to find inode node %lu",
2108 err, (unsigned long)inum);
2109 goto out_dump;
2112 /* Make sure the data node is within inode size */
2113 blk_offs = key_block_flash(c, &dn->key);
2114 blk_offs <<= UBIFS_BLOCK_SHIFT;
2115 blk_offs += le32_to_cpu(dn->size);
2116 if (blk_offs > fscki->size) {
2117 ubifs_err("data node at LEB %d:%d is not within inode "
2118 "size %lld", zbr->lnum, zbr->offs,
2119 fscki->size);
2120 err = -EINVAL;
2121 goto out_dump;
2123 } else {
2124 int nlen;
2125 struct ubifs_dent_node *dent = node;
2126 struct fsck_inode *fscki1;
2128 err = ubifs_validate_entry(c, dent);
2129 if (err)
2130 goto out_dump;
2133 * Search the inode node this entry refers to and the parent
2134 * inode node and insert them to the RB-tree of inodes.
2136 inum = le64_to_cpu(dent->inum);
2137 fscki = read_add_inode(c, priv, inum);
2138 if (IS_ERR(fscki)) {
2139 err = PTR_ERR(fscki);
2140 ubifs_err("error %d while processing entry node and "
2141 "trying to find inode node %lu",
2142 err, (unsigned long)inum);
2143 goto out_dump;
2146 /* Count how many direntries or xentries refers this inode */
2147 fscki->references += 1;
2149 inum = key_inum_flash(c, &dent->key);
2150 fscki1 = read_add_inode(c, priv, inum);
2151 if (IS_ERR(fscki1)) {
2152 err = PTR_ERR(fscki1);
2153 ubifs_err("error %d while processing entry node and "
2154 "trying to find parent inode node %lu",
2155 err, (unsigned long)inum);
2156 goto out_dump;
2159 nlen = le16_to_cpu(dent->nlen);
2160 if (type == UBIFS_XENT_KEY) {
2161 fscki1->calc_xcnt += 1;
2162 fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
2163 fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
2164 fscki1->calc_xnms += nlen;
2165 } else {
2166 fscki1->calc_sz += CALC_DENT_SIZE(nlen);
2167 if (dent->type == UBIFS_ITYPE_DIR)
2168 fscki1->calc_cnt += 1;
2172 out:
2173 kfree(node);
2174 return 0;
2176 out_dump:
2177 ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
2178 dbg_dump_node(c, node);
2179 out_free:
2180 kfree(node);
2181 return err;
2185 * free_inodes - free RB-tree of inodes.
2186 * @fsckd: FS checking information
2188 static void free_inodes(struct fsck_data *fsckd)
2190 struct rb_node *this = fsckd->inodes.rb_node;
2191 struct fsck_inode *fscki;
2193 while (this) {
2194 if (this->rb_left)
2195 this = this->rb_left;
2196 else if (this->rb_right)
2197 this = this->rb_right;
2198 else {
2199 fscki = rb_entry(this, struct fsck_inode, rb);
2200 this = rb_parent(this);
2201 if (this) {
2202 if (this->rb_left == &fscki->rb)
2203 this->rb_left = NULL;
2204 else
2205 this->rb_right = NULL;
2207 kfree(fscki);
2213 * check_inodes - checks all inodes.
2214 * @c: UBIFS file-system description object
2215 * @fsckd: FS checking information
2217 * This is a helper function for 'dbg_check_filesystem()' which walks the
2218 * RB-tree of inodes after the index scan has been finished, and checks that
2219 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
2220 * %-EINVAL if not, and a negative error code in case of failure.
2222 static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
2224 int n, err;
2225 union ubifs_key key;
2226 struct ubifs_znode *znode;
2227 struct ubifs_zbranch *zbr;
2228 struct ubifs_ino_node *ino;
2229 struct fsck_inode *fscki;
2230 struct rb_node *this = rb_first(&fsckd->inodes);
2232 while (this) {
2233 fscki = rb_entry(this, struct fsck_inode, rb);
2234 this = rb_next(this);
2236 if (S_ISDIR(fscki->mode)) {
2238 * Directories have to have exactly one reference (they
2239 * cannot have hardlinks), although root inode is an
2240 * exception.
2242 if (fscki->inum != UBIFS_ROOT_INO &&
2243 fscki->references != 1) {
2244 ubifs_err("directory inode %lu has %d "
2245 "direntries which refer it, but "
2246 "should be 1",
2247 (unsigned long)fscki->inum,
2248 fscki->references);
2249 goto out_dump;
2251 if (fscki->inum == UBIFS_ROOT_INO &&
2252 fscki->references != 0) {
2253 ubifs_err("root inode %lu has non-zero (%d) "
2254 "direntries which refer it",
2255 (unsigned long)fscki->inum,
2256 fscki->references);
2257 goto out_dump;
2259 if (fscki->calc_sz != fscki->size) {
2260 ubifs_err("directory inode %lu size is %lld, "
2261 "but calculated size is %lld",
2262 (unsigned long)fscki->inum,
2263 fscki->size, fscki->calc_sz);
2264 goto out_dump;
2266 if (fscki->calc_cnt != fscki->nlink) {
2267 ubifs_err("directory inode %lu nlink is %d, "
2268 "but calculated nlink is %d",
2269 (unsigned long)fscki->inum,
2270 fscki->nlink, fscki->calc_cnt);
2271 goto out_dump;
2273 } else {
2274 if (fscki->references != fscki->nlink) {
2275 ubifs_err("inode %lu nlink is %d, but "
2276 "calculated nlink is %d",
2277 (unsigned long)fscki->inum,
2278 fscki->nlink, fscki->references);
2279 goto out_dump;
2282 if (fscki->xattr_sz != fscki->calc_xsz) {
2283 ubifs_err("inode %lu has xattr size %u, but "
2284 "calculated size is %lld",
2285 (unsigned long)fscki->inum, fscki->xattr_sz,
2286 fscki->calc_xsz);
2287 goto out_dump;
2289 if (fscki->xattr_cnt != fscki->calc_xcnt) {
2290 ubifs_err("inode %lu has %u xattrs, but "
2291 "calculated count is %lld",
2292 (unsigned long)fscki->inum,
2293 fscki->xattr_cnt, fscki->calc_xcnt);
2294 goto out_dump;
2296 if (fscki->xattr_nms != fscki->calc_xnms) {
2297 ubifs_err("inode %lu has xattr names' size %u, but "
2298 "calculated names' size is %lld",
2299 (unsigned long)fscki->inum, fscki->xattr_nms,
2300 fscki->calc_xnms);
2301 goto out_dump;
2305 return 0;
2307 out_dump:
2308 /* Read the bad inode and dump it */
2309 ino_key_init(c, &key, fscki->inum);
2310 err = ubifs_lookup_level0(c, &key, &znode, &n);
2311 if (!err) {
2312 ubifs_err("inode %lu not found in index",
2313 (unsigned long)fscki->inum);
2314 return -ENOENT;
2315 } else if (err < 0) {
2316 ubifs_err("error %d while looking up inode %lu",
2317 err, (unsigned long)fscki->inum);
2318 return err;
2321 zbr = &znode->zbranch[n];
2322 ino = kmalloc(zbr->len, GFP_NOFS);
2323 if (!ino)
2324 return -ENOMEM;
2326 err = ubifs_tnc_read_node(c, zbr, ino);
2327 if (err) {
2328 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2329 zbr->lnum, zbr->offs, err);
2330 kfree(ino);
2331 return err;
2334 ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",
2335 (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
2336 dbg_dump_node(c, ino);
2337 kfree(ino);
2338 return -EINVAL;
2342 * dbg_check_filesystem - check the file-system.
2343 * @c: UBIFS file-system description object
2345 * This function checks the file system, namely:
2346 * o makes sure that all leaf nodes exist and their CRCs are correct;
2347 * o makes sure inode nlink, size, xattr size/count are correct (for all
2348 * inodes).
2350 * The function reads whole indexing tree and all nodes, so it is pretty
2351 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2352 * not, and a negative error code in case of failure.
2354 int dbg_check_filesystem(struct ubifs_info *c)
2356 int err;
2357 struct fsck_data fsckd;
2359 if (!dbg_is_chk_fs(c))
2360 return 0;
2362 fsckd.inodes = RB_ROOT;
2363 err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
2364 if (err)
2365 goto out_free;
2367 err = check_inodes(c, &fsckd);
2368 if (err)
2369 goto out_free;
2371 free_inodes(&fsckd);
2372 return 0;
2374 out_free:
2375 ubifs_err("file-system check failed with error %d", err);
2376 dump_stack();
2377 free_inodes(&fsckd);
2378 return err;
2382 * dbg_check_data_nodes_order - check that list of data nodes is sorted.
2383 * @c: UBIFS file-system description object
2384 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2386 * This function returns zero if the list of data nodes is sorted correctly,
2387 * and %-EINVAL if not.
2389 int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head)
2391 struct list_head *cur;
2392 struct ubifs_scan_node *sa, *sb;
2394 if (!dbg_is_chk_gen(c))
2395 return 0;
2397 for (cur = head->next; cur->next != head; cur = cur->next) {
2398 ino_t inuma, inumb;
2399 uint32_t blka, blkb;
2401 cond_resched();
2402 sa = container_of(cur, struct ubifs_scan_node, list);
2403 sb = container_of(cur->next, struct ubifs_scan_node, list);
2405 if (sa->type != UBIFS_DATA_NODE) {
2406 ubifs_err("bad node type %d", sa->type);
2407 dbg_dump_node(c, sa->node);
2408 return -EINVAL;
2410 if (sb->type != UBIFS_DATA_NODE) {
2411 ubifs_err("bad node type %d", sb->type);
2412 dbg_dump_node(c, sb->node);
2413 return -EINVAL;
2416 inuma = key_inum(c, &sa->key);
2417 inumb = key_inum(c, &sb->key);
2419 if (inuma < inumb)
2420 continue;
2421 if (inuma > inumb) {
2422 ubifs_err("larger inum %lu goes before inum %lu",
2423 (unsigned long)inuma, (unsigned long)inumb);
2424 goto error_dump;
2427 blka = key_block(c, &sa->key);
2428 blkb = key_block(c, &sb->key);
2430 if (blka > blkb) {
2431 ubifs_err("larger block %u goes before %u", blka, blkb);
2432 goto error_dump;
2434 if (blka == blkb) {
2435 ubifs_err("two data nodes for the same block");
2436 goto error_dump;
2440 return 0;
2442 error_dump:
2443 dbg_dump_node(c, sa->node);
2444 dbg_dump_node(c, sb->node);
2445 return -EINVAL;
2449 * dbg_check_nondata_nodes_order - check that list of data nodes is sorted.
2450 * @c: UBIFS file-system description object
2451 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2453 * This function returns zero if the list of non-data nodes is sorted correctly,
2454 * and %-EINVAL if not.
2456 int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head)
2458 struct list_head *cur;
2459 struct ubifs_scan_node *sa, *sb;
2461 if (!dbg_is_chk_gen(c))
2462 return 0;
2464 for (cur = head->next; cur->next != head; cur = cur->next) {
2465 ino_t inuma, inumb;
2466 uint32_t hasha, hashb;
2468 cond_resched();
2469 sa = container_of(cur, struct ubifs_scan_node, list);
2470 sb = container_of(cur->next, struct ubifs_scan_node, list);
2472 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2473 sa->type != UBIFS_XENT_NODE) {
2474 ubifs_err("bad node type %d", sa->type);
2475 dbg_dump_node(c, sa->node);
2476 return -EINVAL;
2478 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2479 sa->type != UBIFS_XENT_NODE) {
2480 ubifs_err("bad node type %d", sb->type);
2481 dbg_dump_node(c, sb->node);
2482 return -EINVAL;
2485 if (sa->type != UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2486 ubifs_err("non-inode node goes before inode node");
2487 goto error_dump;
2490 if (sa->type == UBIFS_INO_NODE && sb->type != UBIFS_INO_NODE)
2491 continue;
2493 if (sa->type == UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2494 /* Inode nodes are sorted in descending size order */
2495 if (sa->len < sb->len) {
2496 ubifs_err("smaller inode node goes first");
2497 goto error_dump;
2499 continue;
2503 * This is either a dentry or xentry, which should be sorted in
2504 * ascending (parent ino, hash) order.
2506 inuma = key_inum(c, &sa->key);
2507 inumb = key_inum(c, &sb->key);
2509 if (inuma < inumb)
2510 continue;
2511 if (inuma > inumb) {
2512 ubifs_err("larger inum %lu goes before inum %lu",
2513 (unsigned long)inuma, (unsigned long)inumb);
2514 goto error_dump;
2517 hasha = key_block(c, &sa->key);
2518 hashb = key_block(c, &sb->key);
2520 if (hasha > hashb) {
2521 ubifs_err("larger hash %u goes before %u",
2522 hasha, hashb);
2523 goto error_dump;
2527 return 0;
2529 error_dump:
2530 ubifs_msg("dumping first node");
2531 dbg_dump_node(c, sa->node);
2532 ubifs_msg("dumping second node");
2533 dbg_dump_node(c, sb->node);
2534 return -EINVAL;
2535 return 0;
2538 /* Failure mode for recovery testing */
2540 #define chance(n, d) (simple_rand() <= (n) * 32768LL / (d))
2542 static unsigned int next;
2543 static int simple_rand(void)
2545 if (next == 0)
2546 next = current->pid;
2547 next = next * 1103515245 + 12345;
2548 return (next >> 16) & 32767;
2551 static int do_fail(struct ubifs_info *c, int lnum, int write)
2553 struct ubifs_debug_info *d = c->dbg;
2555 ubifs_assert(dbg_is_tst_rcvry(c));
2557 if (d->failure_mode)
2558 return 1;
2560 if (!d->fail_cnt) {
2561 /* First call - decide delay to failure */
2562 if (chance(1, 2)) {
2563 unsigned int delay = 1 << (simple_rand() >> 11);
2565 if (chance(1, 2)) {
2566 d->fail_delay = 1;
2567 d->fail_timeout = jiffies +
2568 msecs_to_jiffies(delay);
2569 ubifs_warn("failing after %ums", delay);
2570 } else {
2571 d->fail_delay = 2;
2572 d->fail_cnt_max = delay;
2573 ubifs_warn("failing after %u calls", delay);
2576 d->fail_cnt += 1;
2578 /* Determine if failure delay has expired */
2579 if (d->fail_delay == 1) {
2580 if (time_before(jiffies, d->fail_timeout))
2581 return 0;
2582 } else if (d->fail_delay == 2)
2583 if (d->fail_cnt++ < d->fail_cnt_max)
2584 return 0;
2585 if (lnum == UBIFS_SB_LNUM) {
2586 if (write) {
2587 if (chance(1, 2))
2588 return 0;
2589 } else if (chance(19, 20))
2590 return 0;
2591 ubifs_warn("failing in super block LEB %d", lnum);
2592 } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2593 if (chance(19, 20))
2594 return 0;
2595 ubifs_warn("failing in master LEB %d", lnum);
2596 } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2597 if (write) {
2598 if (chance(99, 100))
2599 return 0;
2600 } else if (chance(399, 400))
2601 return 0;
2602 ubifs_warn("failing in log LEB %d", lnum);
2603 } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2604 if (write) {
2605 if (chance(7, 8))
2606 return 0;
2607 } else if (chance(19, 20))
2608 return 0;
2609 ubifs_warn("failing in LPT LEB %d", lnum);
2610 } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2611 if (write) {
2612 if (chance(1, 2))
2613 return 0;
2614 } else if (chance(9, 10))
2615 return 0;
2616 ubifs_warn("failing in orphan LEB %d", lnum);
2617 } else if (lnum == c->ihead_lnum) {
2618 if (chance(99, 100))
2619 return 0;
2620 ubifs_warn("failing in index head LEB %d", lnum);
2621 } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2622 if (chance(9, 10))
2623 return 0;
2624 ubifs_warn("failing in GC head LEB %d", lnum);
2625 } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
2626 !ubifs_search_bud(c, lnum)) {
2627 if (chance(19, 20))
2628 return 0;
2629 ubifs_warn("failing in non-bud LEB %d", lnum);
2630 } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
2631 c->cmt_state == COMMIT_RUNNING_REQUIRED) {
2632 if (chance(999, 1000))
2633 return 0;
2634 ubifs_warn("failing in bud LEB %d commit running", lnum);
2635 } else {
2636 if (chance(9999, 10000))
2637 return 0;
2638 ubifs_warn("failing in bud LEB %d commit not running", lnum);
2641 d->failure_mode = 1;
2642 dump_stack();
2643 return 1;
2646 static void cut_data(const void *buf, int len)
2648 int flen, i;
2649 unsigned char *p = (void *)buf;
2651 flen = (len * (long long)simple_rand()) >> 15;
2652 for (i = flen; i < len; i++)
2653 p[i] = 0xff;
2656 int dbg_leb_write(struct ubifs_info *c, int lnum, const void *buf,
2657 int offs, int len, int dtype)
2659 int err, failing;
2661 if (c->dbg->failure_mode)
2662 return -EROFS;
2663 failing = do_fail(c, lnum, 1);
2664 if (failing)
2665 cut_data(buf, len);
2666 err = ubi_leb_write(c->ubi, lnum, buf, offs, len, dtype);
2667 if (err)
2668 return err;
2669 if (failing)
2670 return -EROFS;
2671 return 0;
2674 int dbg_leb_change(struct ubifs_info *c, int lnum, const void *buf,
2675 int len, int dtype)
2677 int err;
2679 if (do_fail(c, lnum, 1))
2680 return -EROFS;
2681 err = ubi_leb_change(c->ubi, lnum, buf, len, dtype);
2682 if (err)
2683 return err;
2684 if (do_fail(c, lnum, 1))
2685 return -EROFS;
2686 return 0;
2689 int dbg_leb_unmap(struct ubifs_info *c, int lnum)
2691 int err;
2693 if (do_fail(c, lnum, 0))
2694 return -EROFS;
2695 err = ubi_leb_unmap(c->ubi, lnum);
2696 if (err)
2697 return err;
2698 if (do_fail(c, lnum, 0))
2699 return -EROFS;
2700 return 0;
2703 int dbg_leb_map(struct ubifs_info *c, int lnum, int dtype)
2705 int err;
2707 if (do_fail(c, lnum, 0))
2708 return -EROFS;
2709 err = ubi_leb_map(c->ubi, lnum, dtype);
2710 if (err)
2711 return err;
2712 if (do_fail(c, lnum, 0))
2713 return -EROFS;
2714 return 0;
2718 * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
2719 * contain the stuff specific to particular file-system mounts.
2721 static struct dentry *dfs_rootdir;
2723 static int dfs_file_open(struct inode *inode, struct file *file)
2725 file->private_data = inode->i_private;
2726 return nonseekable_open(inode, file);
2730 * provide_user_output - provide output to the user reading a debugfs file.
2731 * @val: boolean value for the answer
2732 * @u: the buffer to store the answer at
2733 * @count: size of the buffer
2734 * @ppos: position in the @u output buffer
2736 * This is a simple helper function which stores @val boolean value in the user
2737 * buffer when the user reads one of UBIFS debugfs files. Returns amount of
2738 * bytes written to @u in case of success and a negative error code in case of
2739 * failure.
2741 static int provide_user_output(int val, char __user *u, size_t count,
2742 loff_t *ppos)
2744 char buf[3];
2746 if (val)
2747 buf[0] = '1';
2748 else
2749 buf[0] = '0';
2750 buf[1] = '\n';
2751 buf[2] = 0x00;
2753 return simple_read_from_buffer(u, count, ppos, buf, 2);
2756 static ssize_t dfs_file_read(struct file *file, char __user *u, size_t count,
2757 loff_t *ppos)
2759 struct dentry *dent = file->f_path.dentry;
2760 struct ubifs_info *c = file->private_data;
2761 struct ubifs_debug_info *d = c->dbg;
2762 int val;
2764 if (dent == d->dfs_chk_gen)
2765 val = d->chk_gen;
2766 else if (dent == d->dfs_chk_index)
2767 val = d->chk_index;
2768 else if (dent == d->dfs_chk_orph)
2769 val = d->chk_orph;
2770 else if (dent == d->dfs_chk_lprops)
2771 val = d->chk_lprops;
2772 else if (dent == d->dfs_chk_fs)
2773 val = d->chk_fs;
2774 else if (dent == d->dfs_tst_rcvry)
2775 val = d->tst_rcvry;
2776 else
2777 return -EINVAL;
2779 return provide_user_output(val, u, count, ppos);
2783 * interpret_user_input - interpret user debugfs file input.
2784 * @u: user-provided buffer with the input
2785 * @count: buffer size
2787 * This is a helper function which interpret user input to a boolean UBIFS
2788 * debugfs file. Returns %0 or %1 in case of success and a negative error code
2789 * in case of failure.
2791 static int interpret_user_input(const char __user *u, size_t count)
2793 size_t buf_size;
2794 char buf[8];
2796 buf_size = min_t(size_t, count, (sizeof(buf) - 1));
2797 if (copy_from_user(buf, u, buf_size))
2798 return -EFAULT;
2800 if (buf[0] == '1')
2801 return 1;
2802 else if (buf[0] == '0')
2803 return 0;
2805 return -EINVAL;
2808 static ssize_t dfs_file_write(struct file *file, const char __user *u,
2809 size_t count, loff_t *ppos)
2811 struct ubifs_info *c = file->private_data;
2812 struct ubifs_debug_info *d = c->dbg;
2813 struct dentry *dent = file->f_path.dentry;
2814 int val;
2817 * TODO: this is racy - the file-system might have already been
2818 * unmounted and we'd oops in this case. The plan is to fix it with
2819 * help of 'iterate_supers_type()' which we should have in v3.0: when
2820 * a debugfs opened, we rember FS's UUID in file->private_data. Then
2821 * whenever we access the FS via a debugfs file, we iterate all UBIFS
2822 * superblocks and fine the one with the same UUID, and take the
2823 * locking right.
2825 * The other way to go suggested by Al Viro is to create a separate
2826 * 'ubifs-debug' file-system instead.
2828 if (file->f_path.dentry == d->dfs_dump_lprops) {
2829 dbg_dump_lprops(c);
2830 return count;
2832 if (file->f_path.dentry == d->dfs_dump_budg) {
2833 dbg_dump_budg(c, &c->bi);
2834 return count;
2836 if (file->f_path.dentry == d->dfs_dump_tnc) {
2837 mutex_lock(&c->tnc_mutex);
2838 dbg_dump_tnc(c);
2839 mutex_unlock(&c->tnc_mutex);
2840 return count;
2843 val = interpret_user_input(u, count);
2844 if (val < 0)
2845 return val;
2847 if (dent == d->dfs_chk_gen)
2848 d->chk_gen = val;
2849 else if (dent == d->dfs_chk_index)
2850 d->chk_index = val;
2851 else if (dent == d->dfs_chk_orph)
2852 d->chk_orph = val;
2853 else if (dent == d->dfs_chk_lprops)
2854 d->chk_lprops = val;
2855 else if (dent == d->dfs_chk_fs)
2856 d->chk_fs = val;
2857 else if (dent == d->dfs_tst_rcvry)
2858 d->tst_rcvry = val;
2859 else
2860 return -EINVAL;
2862 return count;
2865 static const struct file_operations dfs_fops = {
2866 .open = dfs_file_open,
2867 .read = dfs_file_read,
2868 .write = dfs_file_write,
2869 .owner = THIS_MODULE,
2870 .llseek = no_llseek,
2874 * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
2875 * @c: UBIFS file-system description object
2877 * This function creates all debugfs files for this instance of UBIFS. Returns
2878 * zero in case of success and a negative error code in case of failure.
2880 * Note, the only reason we have not merged this function with the
2881 * 'ubifs_debugging_init()' function is because it is better to initialize
2882 * debugfs interfaces at the very end of the mount process, and remove them at
2883 * the very beginning of the mount process.
2885 int dbg_debugfs_init_fs(struct ubifs_info *c)
2887 int err, n;
2888 const char *fname;
2889 struct dentry *dent;
2890 struct ubifs_debug_info *d = c->dbg;
2892 n = snprintf(d->dfs_dir_name, UBIFS_DFS_DIR_LEN + 1, UBIFS_DFS_DIR_NAME,
2893 c->vi.ubi_num, c->vi.vol_id);
2894 if (n == UBIFS_DFS_DIR_LEN) {
2895 /* The array size is too small */
2896 fname = UBIFS_DFS_DIR_NAME;
2897 dent = ERR_PTR(-EINVAL);
2898 goto out;
2901 fname = d->dfs_dir_name;
2902 dent = debugfs_create_dir(fname, dfs_rootdir);
2903 if (IS_ERR_OR_NULL(dent))
2904 goto out;
2905 d->dfs_dir = dent;
2907 fname = "dump_lprops";
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_lprops = dent;
2913 fname = "dump_budg";
2914 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2915 if (IS_ERR_OR_NULL(dent))
2916 goto out_remove;
2917 d->dfs_dump_budg = dent;
2919 fname = "dump_tnc";
2920 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2921 if (IS_ERR_OR_NULL(dent))
2922 goto out_remove;
2923 d->dfs_dump_tnc = dent;
2925 fname = "chk_general";
2926 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2927 &dfs_fops);
2928 if (IS_ERR_OR_NULL(dent))
2929 goto out_remove;
2930 d->dfs_chk_gen = dent;
2932 fname = "chk_index";
2933 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2934 &dfs_fops);
2935 if (IS_ERR_OR_NULL(dent))
2936 goto out_remove;
2937 d->dfs_chk_index = dent;
2939 fname = "chk_orphans";
2940 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2941 &dfs_fops);
2942 if (IS_ERR_OR_NULL(dent))
2943 goto out_remove;
2944 d->dfs_chk_orph = dent;
2946 fname = "chk_lprops";
2947 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2948 &dfs_fops);
2949 if (IS_ERR_OR_NULL(dent))
2950 goto out_remove;
2951 d->dfs_chk_lprops = dent;
2953 fname = "chk_fs";
2954 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2955 &dfs_fops);
2956 if (IS_ERR_OR_NULL(dent))
2957 goto out_remove;
2958 d->dfs_chk_fs = dent;
2960 fname = "tst_recovery";
2961 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2962 &dfs_fops);
2963 if (IS_ERR_OR_NULL(dent))
2964 goto out_remove;
2965 d->dfs_tst_rcvry = dent;
2967 return 0;
2969 out_remove:
2970 debugfs_remove_recursive(d->dfs_dir);
2971 out:
2972 err = dent ? PTR_ERR(dent) : -ENODEV;
2973 ubifs_err("cannot create \"%s\" debugfs file or directory, error %d\n",
2974 fname, err);
2975 return err;
2979 * dbg_debugfs_exit_fs - remove all debugfs files.
2980 * @c: UBIFS file-system description object
2982 void dbg_debugfs_exit_fs(struct ubifs_info *c)
2984 debugfs_remove_recursive(c->dbg->dfs_dir);
2987 struct ubifs_global_debug_info ubifs_dbg;
2989 static struct dentry *dfs_chk_gen;
2990 static struct dentry *dfs_chk_index;
2991 static struct dentry *dfs_chk_orph;
2992 static struct dentry *dfs_chk_lprops;
2993 static struct dentry *dfs_chk_fs;
2994 static struct dentry *dfs_tst_rcvry;
2996 static ssize_t dfs_global_file_read(struct file *file, char __user *u,
2997 size_t count, loff_t *ppos)
2999 struct dentry *dent = file->f_path.dentry;
3000 int val;
3002 if (dent == dfs_chk_gen)
3003 val = ubifs_dbg.chk_gen;
3004 else if (dent == dfs_chk_index)
3005 val = ubifs_dbg.chk_index;
3006 else if (dent == dfs_chk_orph)
3007 val = ubifs_dbg.chk_orph;
3008 else if (dent == dfs_chk_lprops)
3009 val = ubifs_dbg.chk_lprops;
3010 else if (dent == dfs_chk_fs)
3011 val = ubifs_dbg.chk_fs;
3012 else if (dent == dfs_tst_rcvry)
3013 val = ubifs_dbg.tst_rcvry;
3014 else
3015 return -EINVAL;
3017 return provide_user_output(val, u, count, ppos);
3020 static ssize_t dfs_global_file_write(struct file *file, const char __user *u,
3021 size_t count, loff_t *ppos)
3023 struct dentry *dent = file->f_path.dentry;
3024 int val;
3026 val = interpret_user_input(u, count);
3027 if (val < 0)
3028 return val;
3030 if (dent == dfs_chk_gen)
3031 ubifs_dbg.chk_gen = val;
3032 else if (dent == dfs_chk_index)
3033 ubifs_dbg.chk_index = val;
3034 else if (dent == dfs_chk_orph)
3035 ubifs_dbg.chk_orph = val;
3036 else if (dent == dfs_chk_lprops)
3037 ubifs_dbg.chk_lprops = val;
3038 else if (dent == dfs_chk_fs)
3039 ubifs_dbg.chk_fs = val;
3040 else if (dent == dfs_tst_rcvry)
3041 ubifs_dbg.tst_rcvry = val;
3042 else
3043 return -EINVAL;
3045 return count;
3048 static const struct file_operations dfs_global_fops = {
3049 .read = dfs_global_file_read,
3050 .write = dfs_global_file_write,
3051 .owner = THIS_MODULE,
3052 .llseek = no_llseek,
3056 * dbg_debugfs_init - initialize debugfs file-system.
3058 * UBIFS uses debugfs file-system to expose various debugging knobs to
3059 * user-space. This function creates "ubifs" directory in the debugfs
3060 * file-system. Returns zero in case of success and a negative error code in
3061 * case of failure.
3063 int dbg_debugfs_init(void)
3065 int err;
3066 const char *fname;
3067 struct dentry *dent;
3069 fname = "ubifs";
3070 dent = debugfs_create_dir(fname, NULL);
3071 if (IS_ERR_OR_NULL(dent))
3072 goto out;
3073 dfs_rootdir = dent;
3075 fname = "chk_general";
3076 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3077 &dfs_global_fops);
3078 if (IS_ERR_OR_NULL(dent))
3079 goto out_remove;
3080 dfs_chk_gen = dent;
3082 fname = "chk_index";
3083 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3084 &dfs_global_fops);
3085 if (IS_ERR_OR_NULL(dent))
3086 goto out_remove;
3087 dfs_chk_index = dent;
3089 fname = "chk_orphans";
3090 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3091 &dfs_global_fops);
3092 if (IS_ERR_OR_NULL(dent))
3093 goto out_remove;
3094 dfs_chk_orph = dent;
3096 fname = "chk_lprops";
3097 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3098 &dfs_global_fops);
3099 if (IS_ERR_OR_NULL(dent))
3100 goto out_remove;
3101 dfs_chk_lprops = dent;
3103 fname = "chk_fs";
3104 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3105 &dfs_global_fops);
3106 if (IS_ERR_OR_NULL(dent))
3107 goto out_remove;
3108 dfs_chk_fs = dent;
3110 fname = "tst_recovery";
3111 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3112 &dfs_global_fops);
3113 if (IS_ERR_OR_NULL(dent))
3114 goto out_remove;
3115 dfs_tst_rcvry = dent;
3117 return 0;
3119 out_remove:
3120 debugfs_remove_recursive(dfs_rootdir);
3121 out:
3122 err = dent ? PTR_ERR(dent) : -ENODEV;
3123 ubifs_err("cannot create \"%s\" debugfs file or directory, error %d\n",
3124 fname, err);
3125 return err;
3129 * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
3131 void dbg_debugfs_exit(void)
3133 debugfs_remove_recursive(dfs_rootdir);
3137 * ubifs_debugging_init - initialize UBIFS debugging.
3138 * @c: UBIFS file-system description object
3140 * This function initializes debugging-related data for the file system.
3141 * Returns zero in case of success and a negative error code in case of
3142 * failure.
3144 int ubifs_debugging_init(struct ubifs_info *c)
3146 c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
3147 if (!c->dbg)
3148 return -ENOMEM;
3150 return 0;
3154 * ubifs_debugging_exit - free debugging data.
3155 * @c: UBIFS file-system description object
3157 void ubifs_debugging_exit(struct ubifs_info *c)
3159 kfree(c->dbg);
3162 #endif /* CONFIG_UBIFS_FS_DEBUG */