UBIFS: introduce helper functions for debugging checks and tests
[linux-block.git] / fs / ubifs / debug.c
CommitLineData
1e51764a
AB
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation
5 *
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.
9 *
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.
14 *
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
18 *
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
20 * Adrian Hunter
21 */
22
23/*
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.
28 */
29
30#define UBIFS_DBG_PRESERVE_UBI
31
32#include "ubifs.h"
33#include <linux/module.h>
34#include <linux/moduleparam.h>
552ff317 35#include <linux/debugfs.h>
4d61db4f 36#include <linux/math64.h>
1e51764a
AB
37
38#ifdef CONFIG_UBIFS_FS_DEBUG
39
40DEFINE_SPINLOCK(dbg_lock);
41
42static char dbg_key_buf0[128];
43static char dbg_key_buf1[128];
44
cce3f612 45unsigned int ubifs_chk_flags;
1e51764a
AB
46unsigned int ubifs_tst_flags;
47
1e51764a
AB
48module_param_named(debug_chks, ubifs_chk_flags, uint, S_IRUGO | S_IWUSR);
49module_param_named(debug_tsts, ubifs_tst_flags, uint, S_IRUGO | S_IWUSR);
50
1e51764a
AB
51MODULE_PARM_DESC(debug_chks, "Debug check flags");
52MODULE_PARM_DESC(debug_tsts, "Debug special test flags");
53
54static const char *get_key_fmt(int fmt)
55{
56 switch (fmt) {
57 case UBIFS_SIMPLE_KEY_FMT:
58 return "simple";
59 default:
60 return "unknown/invalid format";
61 }
62}
63
64static const char *get_key_hash(int hash)
65{
66 switch (hash) {
67 case UBIFS_KEY_HASH_R5:
68 return "R5";
69 case UBIFS_KEY_HASH_TEST:
70 return "test";
71 default:
72 return "unknown/invalid name hash";
73 }
74}
75
76static const char *get_key_type(int type)
77{
78 switch (type) {
79 case UBIFS_INO_KEY:
80 return "inode";
81 case UBIFS_DENT_KEY:
82 return "direntry";
83 case UBIFS_XENT_KEY:
84 return "xentry";
85 case UBIFS_DATA_KEY:
86 return "data";
87 case UBIFS_TRUN_KEY:
88 return "truncate";
89 default:
90 return "unknown/invalid key";
91 }
92}
93
4315fb40
AB
94static const char *get_dent_type(int type)
95{
96 switch (type) {
97 case UBIFS_ITYPE_REG:
98 return "file";
99 case UBIFS_ITYPE_DIR:
100 return "dir";
101 case UBIFS_ITYPE_LNK:
102 return "symlink";
103 case UBIFS_ITYPE_BLK:
104 return "blkdev";
105 case UBIFS_ITYPE_CHR:
106 return "char dev";
107 case UBIFS_ITYPE_FIFO:
108 return "fifo";
109 case UBIFS_ITYPE_SOCK:
110 return "socket";
111 default:
112 return "unknown/invalid type";
113 }
114}
115
1e51764a
AB
116static void sprintf_key(const struct ubifs_info *c, const union ubifs_key *key,
117 char *buffer)
118{
119 char *p = buffer;
120 int type = key_type(c, key);
121
122 if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
123 switch (type) {
124 case UBIFS_INO_KEY:
e84461ad 125 sprintf(p, "(%lu, %s)", (unsigned long)key_inum(c, key),
1e51764a
AB
126 get_key_type(type));
127 break;
128 case UBIFS_DENT_KEY:
129 case UBIFS_XENT_KEY:
e84461ad
AB
130 sprintf(p, "(%lu, %s, %#08x)",
131 (unsigned long)key_inum(c, key),
1e51764a
AB
132 get_key_type(type), key_hash(c, key));
133 break;
134 case UBIFS_DATA_KEY:
e84461ad
AB
135 sprintf(p, "(%lu, %s, %u)",
136 (unsigned long)key_inum(c, key),
1e51764a
AB
137 get_key_type(type), key_block(c, key));
138 break;
139 case UBIFS_TRUN_KEY:
140 sprintf(p, "(%lu, %s)",
e84461ad
AB
141 (unsigned long)key_inum(c, key),
142 get_key_type(type));
1e51764a
AB
143 break;
144 default:
145 sprintf(p, "(bad key type: %#08x, %#08x)",
146 key->u32[0], key->u32[1]);
147 }
148 } else
149 sprintf(p, "bad key format %d", c->key_fmt);
150}
151
152const char *dbg_key_str0(const struct ubifs_info *c, const union ubifs_key *key)
153{
154 /* dbg_lock must be held */
155 sprintf_key(c, key, dbg_key_buf0);
156 return dbg_key_buf0;
157}
158
159const char *dbg_key_str1(const struct ubifs_info *c, const union ubifs_key *key)
160{
161 /* dbg_lock must be held */
162 sprintf_key(c, key, dbg_key_buf1);
163 return dbg_key_buf1;
164}
165
166const char *dbg_ntype(int type)
167{
168 switch (type) {
169 case UBIFS_PAD_NODE:
170 return "padding node";
171 case UBIFS_SB_NODE:
172 return "superblock node";
173 case UBIFS_MST_NODE:
174 return "master node";
175 case UBIFS_REF_NODE:
176 return "reference node";
177 case UBIFS_INO_NODE:
178 return "inode node";
179 case UBIFS_DENT_NODE:
180 return "direntry node";
181 case UBIFS_XENT_NODE:
182 return "xentry node";
183 case UBIFS_DATA_NODE:
184 return "data node";
185 case UBIFS_TRUN_NODE:
186 return "truncate node";
187 case UBIFS_IDX_NODE:
188 return "indexing node";
189 case UBIFS_CS_NODE:
190 return "commit start node";
191 case UBIFS_ORPH_NODE:
192 return "orphan node";
193 default:
194 return "unknown node";
195 }
196}
197
198static const char *dbg_gtype(int type)
199{
200 switch (type) {
201 case UBIFS_NO_NODE_GROUP:
202 return "no node group";
203 case UBIFS_IN_NODE_GROUP:
204 return "in node group";
205 case UBIFS_LAST_OF_NODE_GROUP:
206 return "last of node group";
207 default:
208 return "unknown";
209 }
210}
211
212const char *dbg_cstate(int cmt_state)
213{
214 switch (cmt_state) {
215 case COMMIT_RESTING:
216 return "commit resting";
217 case COMMIT_BACKGROUND:
218 return "background commit requested";
219 case COMMIT_REQUIRED:
220 return "commit required";
221 case COMMIT_RUNNING_BACKGROUND:
222 return "BACKGROUND commit running";
223 case COMMIT_RUNNING_REQUIRED:
224 return "commit running and required";
225 case COMMIT_BROKEN:
226 return "broken commit";
227 default:
228 return "unknown commit state";
229 }
230}
231
77a7ae58
AB
232const char *dbg_jhead(int jhead)
233{
234 switch (jhead) {
235 case GCHD:
236 return "0 (GC)";
237 case BASEHD:
238 return "1 (base)";
239 case DATAHD:
240 return "2 (data)";
241 default:
242 return "unknown journal head";
243 }
244}
245
1e51764a
AB
246static void dump_ch(const struct ubifs_ch *ch)
247{
248 printk(KERN_DEBUG "\tmagic %#x\n", le32_to_cpu(ch->magic));
249 printk(KERN_DEBUG "\tcrc %#x\n", le32_to_cpu(ch->crc));
250 printk(KERN_DEBUG "\tnode_type %d (%s)\n", ch->node_type,
251 dbg_ntype(ch->node_type));
252 printk(KERN_DEBUG "\tgroup_type %d (%s)\n", ch->group_type,
253 dbg_gtype(ch->group_type));
254 printk(KERN_DEBUG "\tsqnum %llu\n",
255 (unsigned long long)le64_to_cpu(ch->sqnum));
256 printk(KERN_DEBUG "\tlen %u\n", le32_to_cpu(ch->len));
257}
258
4315fb40 259void dbg_dump_inode(struct ubifs_info *c, const struct inode *inode)
1e51764a
AB
260{
261 const struct ubifs_inode *ui = ubifs_inode(inode);
4315fb40
AB
262 struct qstr nm = { .name = NULL };
263 union ubifs_key key;
264 struct ubifs_dent_node *dent, *pdent = NULL;
265 int count = 2;
1e51764a 266
b5e426e9
AB
267 printk(KERN_DEBUG "Dump in-memory inode:");
268 printk(KERN_DEBUG "\tinode %lu\n", inode->i_ino);
269 printk(KERN_DEBUG "\tsize %llu\n",
1e51764a 270 (unsigned long long)i_size_read(inode));
b5e426e9
AB
271 printk(KERN_DEBUG "\tnlink %u\n", inode->i_nlink);
272 printk(KERN_DEBUG "\tuid %u\n", (unsigned int)inode->i_uid);
273 printk(KERN_DEBUG "\tgid %u\n", (unsigned int)inode->i_gid);
274 printk(KERN_DEBUG "\tatime %u.%u\n",
1e51764a
AB
275 (unsigned int)inode->i_atime.tv_sec,
276 (unsigned int)inode->i_atime.tv_nsec);
b5e426e9 277 printk(KERN_DEBUG "\tmtime %u.%u\n",
1e51764a
AB
278 (unsigned int)inode->i_mtime.tv_sec,
279 (unsigned int)inode->i_mtime.tv_nsec);
b5e426e9 280 printk(KERN_DEBUG "\tctime %u.%u\n",
1e51764a
AB
281 (unsigned int)inode->i_ctime.tv_sec,
282 (unsigned int)inode->i_ctime.tv_nsec);
b5e426e9
AB
283 printk(KERN_DEBUG "\tcreat_sqnum %llu\n", ui->creat_sqnum);
284 printk(KERN_DEBUG "\txattr_size %u\n", ui->xattr_size);
285 printk(KERN_DEBUG "\txattr_cnt %u\n", ui->xattr_cnt);
286 printk(KERN_DEBUG "\txattr_names %u\n", ui->xattr_names);
287 printk(KERN_DEBUG "\tdirty %u\n", ui->dirty);
288 printk(KERN_DEBUG "\txattr %u\n", ui->xattr);
289 printk(KERN_DEBUG "\tbulk_read %u\n", ui->xattr);
290 printk(KERN_DEBUG "\tsynced_i_size %llu\n",
291 (unsigned long long)ui->synced_i_size);
292 printk(KERN_DEBUG "\tui_size %llu\n",
293 (unsigned long long)ui->ui_size);
294 printk(KERN_DEBUG "\tflags %d\n", ui->flags);
295 printk(KERN_DEBUG "\tcompr_type %d\n", ui->compr_type);
296 printk(KERN_DEBUG "\tlast_page_read %lu\n", ui->last_page_read);
297 printk(KERN_DEBUG "\tread_in_a_row %lu\n", ui->read_in_a_row);
298 printk(KERN_DEBUG "\tdata_len %d\n", ui->data_len);
4315fb40
AB
299
300 if (!S_ISDIR(inode->i_mode))
301 return;
302
303 printk(KERN_DEBUG "List of directory entries:\n");
304 ubifs_assert(!mutex_is_locked(&c->tnc_mutex));
305
306 lowest_dent_key(c, &key, inode->i_ino);
307 while (1) {
308 dent = ubifs_tnc_next_ent(c, &key, &nm);
309 if (IS_ERR(dent)) {
310 if (PTR_ERR(dent) != -ENOENT)
311 printk(KERN_DEBUG "error %ld\n", PTR_ERR(dent));
312 break;
313 }
314
315 printk(KERN_DEBUG "\t%d: %s (%s)\n",
316 count++, dent->name, get_dent_type(dent->type));
317
318 nm.name = dent->name;
319 nm.len = le16_to_cpu(dent->nlen);
320 kfree(pdent);
321 pdent = dent;
322 key_read(c, &dent->key, &key);
323 }
324 kfree(pdent);
1e51764a
AB
325}
326
327void dbg_dump_node(const struct ubifs_info *c, const void *node)
328{
329 int i, n;
330 union ubifs_key key;
331 const struct ubifs_ch *ch = node;
332
2b1844a8 333 if (dbg_is_tst_rcvry(c))
1e51764a
AB
334 return;
335
336 /* If the magic is incorrect, just hexdump the first bytes */
337 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
338 printk(KERN_DEBUG "Not a node, first %zu bytes:", UBIFS_CH_SZ);
339 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
340 (void *)node, UBIFS_CH_SZ, 1);
341 return;
342 }
343
344 spin_lock(&dbg_lock);
345 dump_ch(node);
346
347 switch (ch->node_type) {
348 case UBIFS_PAD_NODE:
349 {
350 const struct ubifs_pad_node *pad = node;
351
352 printk(KERN_DEBUG "\tpad_len %u\n",
353 le32_to_cpu(pad->pad_len));
354 break;
355 }
356 case UBIFS_SB_NODE:
357 {
358 const struct ubifs_sb_node *sup = node;
359 unsigned int sup_flags = le32_to_cpu(sup->flags);
360
361 printk(KERN_DEBUG "\tkey_hash %d (%s)\n",
362 (int)sup->key_hash, get_key_hash(sup->key_hash));
363 printk(KERN_DEBUG "\tkey_fmt %d (%s)\n",
364 (int)sup->key_fmt, get_key_fmt(sup->key_fmt));
365 printk(KERN_DEBUG "\tflags %#x\n", sup_flags);
366 printk(KERN_DEBUG "\t big_lpt %u\n",
367 !!(sup_flags & UBIFS_FLG_BIGLPT));
9f58d350
MC
368 printk(KERN_DEBUG "\t space_fixup %u\n",
369 !!(sup_flags & UBIFS_FLG_SPACE_FIXUP));
1e51764a
AB
370 printk(KERN_DEBUG "\tmin_io_size %u\n",
371 le32_to_cpu(sup->min_io_size));
372 printk(KERN_DEBUG "\tleb_size %u\n",
373 le32_to_cpu(sup->leb_size));
374 printk(KERN_DEBUG "\tleb_cnt %u\n",
375 le32_to_cpu(sup->leb_cnt));
376 printk(KERN_DEBUG "\tmax_leb_cnt %u\n",
377 le32_to_cpu(sup->max_leb_cnt));
378 printk(KERN_DEBUG "\tmax_bud_bytes %llu\n",
379 (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
380 printk(KERN_DEBUG "\tlog_lebs %u\n",
381 le32_to_cpu(sup->log_lebs));
382 printk(KERN_DEBUG "\tlpt_lebs %u\n",
383 le32_to_cpu(sup->lpt_lebs));
384 printk(KERN_DEBUG "\torph_lebs %u\n",
385 le32_to_cpu(sup->orph_lebs));
386 printk(KERN_DEBUG "\tjhead_cnt %u\n",
387 le32_to_cpu(sup->jhead_cnt));
388 printk(KERN_DEBUG "\tfanout %u\n",
389 le32_to_cpu(sup->fanout));
390 printk(KERN_DEBUG "\tlsave_cnt %u\n",
391 le32_to_cpu(sup->lsave_cnt));
392 printk(KERN_DEBUG "\tdefault_compr %u\n",
393 (int)le16_to_cpu(sup->default_compr));
394 printk(KERN_DEBUG "\trp_size %llu\n",
395 (unsigned long long)le64_to_cpu(sup->rp_size));
396 printk(KERN_DEBUG "\trp_uid %u\n",
397 le32_to_cpu(sup->rp_uid));
398 printk(KERN_DEBUG "\trp_gid %u\n",
399 le32_to_cpu(sup->rp_gid));
400 printk(KERN_DEBUG "\tfmt_version %u\n",
401 le32_to_cpu(sup->fmt_version));
402 printk(KERN_DEBUG "\ttime_gran %u\n",
403 le32_to_cpu(sup->time_gran));
7f2f4e72
JP
404 printk(KERN_DEBUG "\tUUID %pUB\n",
405 sup->uuid);
1e51764a
AB
406 break;
407 }
408 case UBIFS_MST_NODE:
409 {
410 const struct ubifs_mst_node *mst = node;
411
412 printk(KERN_DEBUG "\thighest_inum %llu\n",
413 (unsigned long long)le64_to_cpu(mst->highest_inum));
414 printk(KERN_DEBUG "\tcommit number %llu\n",
415 (unsigned long long)le64_to_cpu(mst->cmt_no));
416 printk(KERN_DEBUG "\tflags %#x\n",
417 le32_to_cpu(mst->flags));
418 printk(KERN_DEBUG "\tlog_lnum %u\n",
419 le32_to_cpu(mst->log_lnum));
420 printk(KERN_DEBUG "\troot_lnum %u\n",
421 le32_to_cpu(mst->root_lnum));
422 printk(KERN_DEBUG "\troot_offs %u\n",
423 le32_to_cpu(mst->root_offs));
424 printk(KERN_DEBUG "\troot_len %u\n",
425 le32_to_cpu(mst->root_len));
426 printk(KERN_DEBUG "\tgc_lnum %u\n",
427 le32_to_cpu(mst->gc_lnum));
428 printk(KERN_DEBUG "\tihead_lnum %u\n",
429 le32_to_cpu(mst->ihead_lnum));
430 printk(KERN_DEBUG "\tihead_offs %u\n",
431 le32_to_cpu(mst->ihead_offs));
0ecb9529
HH
432 printk(KERN_DEBUG "\tindex_size %llu\n",
433 (unsigned long long)le64_to_cpu(mst->index_size));
1e51764a
AB
434 printk(KERN_DEBUG "\tlpt_lnum %u\n",
435 le32_to_cpu(mst->lpt_lnum));
436 printk(KERN_DEBUG "\tlpt_offs %u\n",
437 le32_to_cpu(mst->lpt_offs));
438 printk(KERN_DEBUG "\tnhead_lnum %u\n",
439 le32_to_cpu(mst->nhead_lnum));
440 printk(KERN_DEBUG "\tnhead_offs %u\n",
441 le32_to_cpu(mst->nhead_offs));
442 printk(KERN_DEBUG "\tltab_lnum %u\n",
443 le32_to_cpu(mst->ltab_lnum));
444 printk(KERN_DEBUG "\tltab_offs %u\n",
445 le32_to_cpu(mst->ltab_offs));
446 printk(KERN_DEBUG "\tlsave_lnum %u\n",
447 le32_to_cpu(mst->lsave_lnum));
448 printk(KERN_DEBUG "\tlsave_offs %u\n",
449 le32_to_cpu(mst->lsave_offs));
450 printk(KERN_DEBUG "\tlscan_lnum %u\n",
451 le32_to_cpu(mst->lscan_lnum));
452 printk(KERN_DEBUG "\tleb_cnt %u\n",
453 le32_to_cpu(mst->leb_cnt));
454 printk(KERN_DEBUG "\tempty_lebs %u\n",
455 le32_to_cpu(mst->empty_lebs));
456 printk(KERN_DEBUG "\tidx_lebs %u\n",
457 le32_to_cpu(mst->idx_lebs));
458 printk(KERN_DEBUG "\ttotal_free %llu\n",
459 (unsigned long long)le64_to_cpu(mst->total_free));
460 printk(KERN_DEBUG "\ttotal_dirty %llu\n",
461 (unsigned long long)le64_to_cpu(mst->total_dirty));
462 printk(KERN_DEBUG "\ttotal_used %llu\n",
463 (unsigned long long)le64_to_cpu(mst->total_used));
464 printk(KERN_DEBUG "\ttotal_dead %llu\n",
465 (unsigned long long)le64_to_cpu(mst->total_dead));
466 printk(KERN_DEBUG "\ttotal_dark %llu\n",
467 (unsigned long long)le64_to_cpu(mst->total_dark));
468 break;
469 }
470 case UBIFS_REF_NODE:
471 {
472 const struct ubifs_ref_node *ref = node;
473
474 printk(KERN_DEBUG "\tlnum %u\n",
475 le32_to_cpu(ref->lnum));
476 printk(KERN_DEBUG "\toffs %u\n",
477 le32_to_cpu(ref->offs));
478 printk(KERN_DEBUG "\tjhead %u\n",
479 le32_to_cpu(ref->jhead));
480 break;
481 }
482 case UBIFS_INO_NODE:
483 {
484 const struct ubifs_ino_node *ino = node;
485
486 key_read(c, &ino->key, &key);
487 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
488 printk(KERN_DEBUG "\tcreat_sqnum %llu\n",
489 (unsigned long long)le64_to_cpu(ino->creat_sqnum));
490 printk(KERN_DEBUG "\tsize %llu\n",
491 (unsigned long long)le64_to_cpu(ino->size));
492 printk(KERN_DEBUG "\tnlink %u\n",
493 le32_to_cpu(ino->nlink));
494 printk(KERN_DEBUG "\tatime %lld.%u\n",
495 (long long)le64_to_cpu(ino->atime_sec),
496 le32_to_cpu(ino->atime_nsec));
497 printk(KERN_DEBUG "\tmtime %lld.%u\n",
498 (long long)le64_to_cpu(ino->mtime_sec),
499 le32_to_cpu(ino->mtime_nsec));
500 printk(KERN_DEBUG "\tctime %lld.%u\n",
501 (long long)le64_to_cpu(ino->ctime_sec),
502 le32_to_cpu(ino->ctime_nsec));
503 printk(KERN_DEBUG "\tuid %u\n",
504 le32_to_cpu(ino->uid));
505 printk(KERN_DEBUG "\tgid %u\n",
506 le32_to_cpu(ino->gid));
507 printk(KERN_DEBUG "\tmode %u\n",
508 le32_to_cpu(ino->mode));
509 printk(KERN_DEBUG "\tflags %#x\n",
510 le32_to_cpu(ino->flags));
511 printk(KERN_DEBUG "\txattr_cnt %u\n",
512 le32_to_cpu(ino->xattr_cnt));
513 printk(KERN_DEBUG "\txattr_size %u\n",
514 le32_to_cpu(ino->xattr_size));
515 printk(KERN_DEBUG "\txattr_names %u\n",
516 le32_to_cpu(ino->xattr_names));
517 printk(KERN_DEBUG "\tcompr_type %#x\n",
518 (int)le16_to_cpu(ino->compr_type));
519 printk(KERN_DEBUG "\tdata len %u\n",
520 le32_to_cpu(ino->data_len));
521 break;
522 }
523 case UBIFS_DENT_NODE:
524 case UBIFS_XENT_NODE:
525 {
526 const struct ubifs_dent_node *dent = node;
527 int nlen = le16_to_cpu(dent->nlen);
528
529 key_read(c, &dent->key, &key);
530 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
531 printk(KERN_DEBUG "\tinum %llu\n",
532 (unsigned long long)le64_to_cpu(dent->inum));
533 printk(KERN_DEBUG "\ttype %d\n", (int)dent->type);
534 printk(KERN_DEBUG "\tnlen %d\n", nlen);
535 printk(KERN_DEBUG "\tname ");
536
537 if (nlen > UBIFS_MAX_NLEN)
538 printk(KERN_DEBUG "(bad name length, not printing, "
539 "bad or corrupted node)");
540 else {
541 for (i = 0; i < nlen && dent->name[i]; i++)
c9927c3e 542 printk(KERN_CONT "%c", dent->name[i]);
1e51764a 543 }
c9927c3e 544 printk(KERN_CONT "\n");
1e51764a
AB
545
546 break;
547 }
548 case UBIFS_DATA_NODE:
549 {
550 const struct ubifs_data_node *dn = node;
551 int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
552
553 key_read(c, &dn->key, &key);
554 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
555 printk(KERN_DEBUG "\tsize %u\n",
556 le32_to_cpu(dn->size));
557 printk(KERN_DEBUG "\tcompr_typ %d\n",
558 (int)le16_to_cpu(dn->compr_type));
559 printk(KERN_DEBUG "\tdata size %d\n",
560 dlen);
561 printk(KERN_DEBUG "\tdata:\n");
562 print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET, 32, 1,
563 (void *)&dn->data, dlen, 0);
564 break;
565 }
566 case UBIFS_TRUN_NODE:
567 {
568 const struct ubifs_trun_node *trun = node;
569
570 printk(KERN_DEBUG "\tinum %u\n",
571 le32_to_cpu(trun->inum));
572 printk(KERN_DEBUG "\told_size %llu\n",
573 (unsigned long long)le64_to_cpu(trun->old_size));
574 printk(KERN_DEBUG "\tnew_size %llu\n",
575 (unsigned long long)le64_to_cpu(trun->new_size));
576 break;
577 }
578 case UBIFS_IDX_NODE:
579 {
580 const struct ubifs_idx_node *idx = node;
581
582 n = le16_to_cpu(idx->child_cnt);
583 printk(KERN_DEBUG "\tchild_cnt %d\n", n);
584 printk(KERN_DEBUG "\tlevel %d\n",
585 (int)le16_to_cpu(idx->level));
586 printk(KERN_DEBUG "\tBranches:\n");
587
588 for (i = 0; i < n && i < c->fanout - 1; i++) {
589 const struct ubifs_branch *br;
590
591 br = ubifs_idx_branch(c, idx, i);
592 key_read(c, &br->key, &key);
593 printk(KERN_DEBUG "\t%d: LEB %d:%d len %d key %s\n",
594 i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
595 le32_to_cpu(br->len), DBGKEY(&key));
596 }
597 break;
598 }
599 case UBIFS_CS_NODE:
600 break;
601 case UBIFS_ORPH_NODE:
602 {
603 const struct ubifs_orph_node *orph = node;
604
605 printk(KERN_DEBUG "\tcommit number %llu\n",
606 (unsigned long long)
607 le64_to_cpu(orph->cmt_no) & LLONG_MAX);
608 printk(KERN_DEBUG "\tlast node flag %llu\n",
609 (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
610 n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
611 printk(KERN_DEBUG "\t%d orphan inode numbers:\n", n);
612 for (i = 0; i < n; i++)
613 printk(KERN_DEBUG "\t ino %llu\n",
7424bac8 614 (unsigned long long)le64_to_cpu(orph->inos[i]));
1e51764a
AB
615 break;
616 }
617 default:
618 printk(KERN_DEBUG "node type %d was not recognized\n",
619 (int)ch->node_type);
620 }
621 spin_unlock(&dbg_lock);
622}
623
624void dbg_dump_budget_req(const struct ubifs_budget_req *req)
625{
626 spin_lock(&dbg_lock);
627 printk(KERN_DEBUG "Budgeting request: new_ino %d, dirtied_ino %d\n",
628 req->new_ino, req->dirtied_ino);
629 printk(KERN_DEBUG "\tnew_ino_d %d, dirtied_ino_d %d\n",
630 req->new_ino_d, req->dirtied_ino_d);
631 printk(KERN_DEBUG "\tnew_page %d, dirtied_page %d\n",
632 req->new_page, req->dirtied_page);
633 printk(KERN_DEBUG "\tnew_dent %d, mod_dent %d\n",
634 req->new_dent, req->mod_dent);
635 printk(KERN_DEBUG "\tidx_growth %d\n", req->idx_growth);
636 printk(KERN_DEBUG "\tdata_growth %d dd_growth %d\n",
637 req->data_growth, req->dd_growth);
638 spin_unlock(&dbg_lock);
639}
640
641void dbg_dump_lstats(const struct ubifs_lp_stats *lst)
642{
643 spin_lock(&dbg_lock);
1de94159
AB
644 printk(KERN_DEBUG "(pid %d) Lprops statistics: empty_lebs %d, "
645 "idx_lebs %d\n", current->pid, lst->empty_lebs, lst->idx_lebs);
1e51764a
AB
646 printk(KERN_DEBUG "\ttaken_empty_lebs %d, total_free %lld, "
647 "total_dirty %lld\n", lst->taken_empty_lebs, lst->total_free,
648 lst->total_dirty);
649 printk(KERN_DEBUG "\ttotal_used %lld, total_dark %lld, "
650 "total_dead %lld\n", lst->total_used, lst->total_dark,
651 lst->total_dead);
652 spin_unlock(&dbg_lock);
653}
654
f1bd66af 655void dbg_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)
1e51764a
AB
656{
657 int i;
658 struct rb_node *rb;
659 struct ubifs_bud *bud;
660 struct ubifs_gced_idx_leb *idx_gc;
21a60258 661 long long available, outstanding, free;
1e51764a 662
8ff83089 663 spin_lock(&c->space_lock);
1e51764a 664 spin_lock(&dbg_lock);
8c3067e4
AB
665 printk(KERN_DEBUG "(pid %d) Budgeting info: data budget sum %lld, "
666 "total budget sum %lld\n", current->pid,
f1bd66af
AB
667 bi->data_growth + bi->dd_growth,
668 bi->data_growth + bi->dd_growth + bi->idx_growth);
8c3067e4 669 printk(KERN_DEBUG "\tbudg_data_growth %lld, budg_dd_growth %lld, "
f1bd66af
AB
670 "budg_idx_growth %lld\n", bi->data_growth, bi->dd_growth,
671 bi->idx_growth);
8c3067e4 672 printk(KERN_DEBUG "\tmin_idx_lebs %d, old_idx_sz %llu, "
f1bd66af
AB
673 "uncommitted_idx %lld\n", bi->min_idx_lebs, bi->old_idx_sz,
674 bi->uncommitted_idx);
8c3067e4 675 printk(KERN_DEBUG "\tpage_budget %d, inode_budget %d, dent_budget %d\n",
f1bd66af 676 bi->page_budget, bi->inode_budget, bi->dent_budget);
8c3067e4 677 printk(KERN_DEBUG "\tnospace %u, nospace_rp %u\n",
f1bd66af 678 bi->nospace, bi->nospace_rp);
8c3067e4
AB
679 printk(KERN_DEBUG "\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
680 c->dark_wm, c->dead_wm, c->max_idx_node_sz);
f1bd66af
AB
681
682 if (bi != &c->bi)
683 /*
684 * If we are dumping saved budgeting data, do not print
685 * additional information which is about the current state, not
686 * the old one which corresponded to the saved budgeting data.
687 */
688 goto out_unlock;
689
8c3067e4
AB
690 printk(KERN_DEBUG "\tfreeable_cnt %d, calc_idx_sz %lld, idx_gc_cnt %d\n",
691 c->freeable_cnt, c->calc_idx_sz, c->idx_gc_cnt);
1e51764a
AB
692 printk(KERN_DEBUG "\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, "
693 "clean_zn_cnt %ld\n", atomic_long_read(&c->dirty_pg_cnt),
694 atomic_long_read(&c->dirty_zn_cnt),
695 atomic_long_read(&c->clean_zn_cnt));
1e51764a
AB
696 printk(KERN_DEBUG "\tgc_lnum %d, ihead_lnum %d\n",
697 c->gc_lnum, c->ihead_lnum);
f1bd66af 698
84abf972
AB
699 /* If we are in R/O mode, journal heads do not exist */
700 if (c->jheads)
701 for (i = 0; i < c->jhead_cnt; i++)
77a7ae58
AB
702 printk(KERN_DEBUG "\tjhead %s\t LEB %d\n",
703 dbg_jhead(c->jheads[i].wbuf.jhead),
704 c->jheads[i].wbuf.lnum);
1e51764a
AB
705 for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
706 bud = rb_entry(rb, struct ubifs_bud, rb);
707 printk(KERN_DEBUG "\tbud LEB %d\n", bud->lnum);
708 }
709 list_for_each_entry(bud, &c->old_buds, list)
710 printk(KERN_DEBUG "\told bud LEB %d\n", bud->lnum);
711 list_for_each_entry(idx_gc, &c->idx_gc, list)
712 printk(KERN_DEBUG "\tGC'ed idx LEB %d unmap %d\n",
713 idx_gc->lnum, idx_gc->unmap);
714 printk(KERN_DEBUG "\tcommit state %d\n", c->cmt_state);
21a60258
AB
715
716 /* Print budgeting predictions */
b137545c
AB
717 available = ubifs_calc_available(c, c->bi.min_idx_lebs);
718 outstanding = c->bi.data_growth + c->bi.dd_growth;
84abf972 719 free = ubifs_get_free_space_nolock(c);
21a60258
AB
720 printk(KERN_DEBUG "Budgeting predictions:\n");
721 printk(KERN_DEBUG "\tavailable: %lld, outstanding %lld, free %lld\n",
722 available, outstanding, free);
f1bd66af 723out_unlock:
1e51764a 724 spin_unlock(&dbg_lock);
8ff83089 725 spin_unlock(&c->space_lock);
1e51764a
AB
726}
727
728void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
729{
be9e62a7
AB
730 int i, spc, dark = 0, dead = 0;
731 struct rb_node *rb;
732 struct ubifs_bud *bud;
733
734 spc = lp->free + lp->dirty;
735 if (spc < c->dead_wm)
736 dead = spc;
737 else
738 dark = ubifs_calc_dark(c, spc);
739
740 if (lp->flags & LPROPS_INDEX)
741 printk(KERN_DEBUG "LEB %-7d free %-8d dirty %-8d used %-8d "
742 "free + dirty %-8d flags %#x (", lp->lnum, lp->free,
743 lp->dirty, c->leb_size - spc, spc, lp->flags);
744 else
745 printk(KERN_DEBUG "LEB %-7d free %-8d dirty %-8d used %-8d "
746 "free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d "
747 "flags %#-4x (", lp->lnum, lp->free, lp->dirty,
748 c->leb_size - spc, spc, dark, dead,
749 (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags);
750
751 if (lp->flags & LPROPS_TAKEN) {
752 if (lp->flags & LPROPS_INDEX)
753 printk(KERN_CONT "index, taken");
754 else
755 printk(KERN_CONT "taken");
756 } else {
757 const char *s;
758
759 if (lp->flags & LPROPS_INDEX) {
760 switch (lp->flags & LPROPS_CAT_MASK) {
761 case LPROPS_DIRTY_IDX:
762 s = "dirty index";
763 break;
764 case LPROPS_FRDI_IDX:
765 s = "freeable index";
766 break;
767 default:
768 s = "index";
769 }
770 } else {
771 switch (lp->flags & LPROPS_CAT_MASK) {
772 case LPROPS_UNCAT:
773 s = "not categorized";
774 break;
775 case LPROPS_DIRTY:
776 s = "dirty";
777 break;
778 case LPROPS_FREE:
779 s = "free";
780 break;
781 case LPROPS_EMPTY:
782 s = "empty";
783 break;
784 case LPROPS_FREEABLE:
785 s = "freeable";
786 break;
787 default:
788 s = NULL;
789 break;
790 }
791 }
792 printk(KERN_CONT "%s", s);
793 }
794
795 for (rb = rb_first((struct rb_root *)&c->buds); rb; rb = rb_next(rb)) {
796 bud = rb_entry(rb, struct ubifs_bud, rb);
797 if (bud->lnum == lp->lnum) {
798 int head = 0;
799 for (i = 0; i < c->jhead_cnt; i++) {
1321657d
AB
800 /*
801 * Note, if we are in R/O mode or in the middle
802 * of mounting/re-mounting, the write-buffers do
803 * not exist.
804 */
805 if (c->jheads &&
806 lp->lnum == c->jheads[i].wbuf.lnum) {
be9e62a7
AB
807 printk(KERN_CONT ", jhead %s",
808 dbg_jhead(i));
809 head = 1;
810 }
811 }
812 if (!head)
813 printk(KERN_CONT ", bud of jhead %s",
814 dbg_jhead(bud->jhead));
815 }
816 }
817 if (lp->lnum == c->gc_lnum)
818 printk(KERN_CONT ", GC LEB");
819 printk(KERN_CONT ")\n");
1e51764a
AB
820}
821
822void dbg_dump_lprops(struct ubifs_info *c)
823{
824 int lnum, err;
825 struct ubifs_lprops lp;
826 struct ubifs_lp_stats lst;
827
2ba5f7ae
AB
828 printk(KERN_DEBUG "(pid %d) start dumping LEB properties\n",
829 current->pid);
1e51764a
AB
830 ubifs_get_lp_stats(c, &lst);
831 dbg_dump_lstats(&lst);
832
833 for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
834 err = ubifs_read_one_lp(c, lnum, &lp);
835 if (err)
836 ubifs_err("cannot read lprops for LEB %d", lnum);
837
838 dbg_dump_lprop(c, &lp);
839 }
2ba5f7ae
AB
840 printk(KERN_DEBUG "(pid %d) finish dumping LEB properties\n",
841 current->pid);
1e51764a
AB
842}
843
73944a6d
AH
844void dbg_dump_lpt_info(struct ubifs_info *c)
845{
846 int i;
847
848 spin_lock(&dbg_lock);
2ba5f7ae 849 printk(KERN_DEBUG "(pid %d) dumping LPT information\n", current->pid);
73944a6d
AH
850 printk(KERN_DEBUG "\tlpt_sz: %lld\n", c->lpt_sz);
851 printk(KERN_DEBUG "\tpnode_sz: %d\n", c->pnode_sz);
852 printk(KERN_DEBUG "\tnnode_sz: %d\n", c->nnode_sz);
853 printk(KERN_DEBUG "\tltab_sz: %d\n", c->ltab_sz);
854 printk(KERN_DEBUG "\tlsave_sz: %d\n", c->lsave_sz);
855 printk(KERN_DEBUG "\tbig_lpt: %d\n", c->big_lpt);
856 printk(KERN_DEBUG "\tlpt_hght: %d\n", c->lpt_hght);
857 printk(KERN_DEBUG "\tpnode_cnt: %d\n", c->pnode_cnt);
858 printk(KERN_DEBUG "\tnnode_cnt: %d\n", c->nnode_cnt);
859 printk(KERN_DEBUG "\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt);
860 printk(KERN_DEBUG "\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt);
861 printk(KERN_DEBUG "\tlsave_cnt: %d\n", c->lsave_cnt);
862 printk(KERN_DEBUG "\tspace_bits: %d\n", c->space_bits);
863 printk(KERN_DEBUG "\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
864 printk(KERN_DEBUG "\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
865 printk(KERN_DEBUG "\tlpt_spc_bits: %d\n", c->lpt_spc_bits);
866 printk(KERN_DEBUG "\tpcnt_bits: %d\n", c->pcnt_bits);
867 printk(KERN_DEBUG "\tlnum_bits: %d\n", c->lnum_bits);
868 printk(KERN_DEBUG "\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
869 printk(KERN_DEBUG "\tLPT head is at %d:%d\n",
870 c->nhead_lnum, c->nhead_offs);
f92b9826
AB
871 printk(KERN_DEBUG "\tLPT ltab is at %d:%d\n",
872 c->ltab_lnum, c->ltab_offs);
73944a6d
AH
873 if (c->big_lpt)
874 printk(KERN_DEBUG "\tLPT lsave is at %d:%d\n",
875 c->lsave_lnum, c->lsave_offs);
876 for (i = 0; i < c->lpt_lebs; i++)
877 printk(KERN_DEBUG "\tLPT LEB %d free %d dirty %d tgc %d "
878 "cmt %d\n", i + c->lpt_first, c->ltab[i].free,
879 c->ltab[i].dirty, c->ltab[i].tgc, c->ltab[i].cmt);
880 spin_unlock(&dbg_lock);
881}
882
1e51764a
AB
883void dbg_dump_leb(const struct ubifs_info *c, int lnum)
884{
885 struct ubifs_scan_leb *sleb;
886 struct ubifs_scan_node *snod;
73d9aec3 887 void *buf;
1e51764a 888
2b1844a8 889 if (dbg_is_tst_rcvry(c))
1e51764a
AB
890 return;
891
2ba5f7ae
AB
892 printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n",
893 current->pid, lnum);
73d9aec3 894
fc5e58c0 895 buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
73d9aec3
AB
896 if (!buf) {
897 ubifs_err("cannot allocate memory for dumping LEB %d", lnum);
898 return;
899 }
900
901 sleb = ubifs_scan(c, lnum, 0, buf, 0);
1e51764a
AB
902 if (IS_ERR(sleb)) {
903 ubifs_err("scan error %d", (int)PTR_ERR(sleb));
73d9aec3 904 goto out;
1e51764a
AB
905 }
906
907 printk(KERN_DEBUG "LEB %d has %d nodes ending at %d\n", lnum,
908 sleb->nodes_cnt, sleb->endpt);
909
910 list_for_each_entry(snod, &sleb->nodes, list) {
911 cond_resched();
912 printk(KERN_DEBUG "Dumping node at LEB %d:%d len %d\n", lnum,
913 snod->offs, snod->len);
914 dbg_dump_node(c, snod->node);
915 }
916
2ba5f7ae
AB
917 printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n",
918 current->pid, lnum);
1e51764a 919 ubifs_scan_destroy(sleb);
73d9aec3
AB
920
921out:
922 vfree(buf);
1e51764a
AB
923 return;
924}
925
926void dbg_dump_znode(const struct ubifs_info *c,
927 const struct ubifs_znode *znode)
928{
929 int n;
930 const struct ubifs_zbranch *zbr;
931
932 spin_lock(&dbg_lock);
933 if (znode->parent)
934 zbr = &znode->parent->zbranch[znode->iip];
935 else
936 zbr = &c->zroot;
937
938 printk(KERN_DEBUG "znode %p, LEB %d:%d len %d parent %p iip %d level %d"
939 " child_cnt %d flags %lx\n", znode, zbr->lnum, zbr->offs,
940 zbr->len, znode->parent, znode->iip, znode->level,
941 znode->child_cnt, znode->flags);
942
943 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
944 spin_unlock(&dbg_lock);
945 return;
946 }
947
948 printk(KERN_DEBUG "zbranches:\n");
949 for (n = 0; n < znode->child_cnt; n++) {
950 zbr = &znode->zbranch[n];
951 if (znode->level > 0)
952 printk(KERN_DEBUG "\t%d: znode %p LEB %d:%d len %d key "
953 "%s\n", n, zbr->znode, zbr->lnum,
954 zbr->offs, zbr->len,
955 DBGKEY(&zbr->key));
956 else
957 printk(KERN_DEBUG "\t%d: LNC %p LEB %d:%d len %d key "
958 "%s\n", n, zbr->znode, zbr->lnum,
959 zbr->offs, zbr->len,
960 DBGKEY(&zbr->key));
961 }
962 spin_unlock(&dbg_lock);
963}
964
965void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
966{
967 int i;
968
2ba5f7ae 969 printk(KERN_DEBUG "(pid %d) start dumping heap cat %d (%d elements)\n",
1de94159 970 current->pid, cat, heap->cnt);
1e51764a
AB
971 for (i = 0; i < heap->cnt; i++) {
972 struct ubifs_lprops *lprops = heap->arr[i];
973
974 printk(KERN_DEBUG "\t%d. LEB %d hpos %d free %d dirty %d "
975 "flags %d\n", i, lprops->lnum, lprops->hpos,
976 lprops->free, lprops->dirty, lprops->flags);
977 }
2ba5f7ae 978 printk(KERN_DEBUG "(pid %d) finish dumping heap\n", current->pid);
1e51764a
AB
979}
980
981void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
982 struct ubifs_nnode *parent, int iip)
983{
984 int i;
985
2ba5f7ae 986 printk(KERN_DEBUG "(pid %d) dumping pnode:\n", current->pid);
1e51764a
AB
987 printk(KERN_DEBUG "\taddress %zx parent %zx cnext %zx\n",
988 (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
989 printk(KERN_DEBUG "\tflags %lu iip %d level %d num %d\n",
990 pnode->flags, iip, pnode->level, pnode->num);
991 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
992 struct ubifs_lprops *lp = &pnode->lprops[i];
993
994 printk(KERN_DEBUG "\t%d: free %d dirty %d flags %d lnum %d\n",
995 i, lp->free, lp->dirty, lp->flags, lp->lnum);
996 }
997}
998
999void dbg_dump_tnc(struct ubifs_info *c)
1000{
1001 struct ubifs_znode *znode;
1002 int level;
1003
1004 printk(KERN_DEBUG "\n");
2ba5f7ae 1005 printk(KERN_DEBUG "(pid %d) start dumping TNC tree\n", current->pid);
1e51764a
AB
1006 znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
1007 level = znode->level;
1008 printk(KERN_DEBUG "== Level %d ==\n", level);
1009 while (znode) {
1010 if (level != znode->level) {
1011 level = znode->level;
1012 printk(KERN_DEBUG "== Level %d ==\n", level);
1013 }
1014 dbg_dump_znode(c, znode);
1015 znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
1016 }
2ba5f7ae 1017 printk(KERN_DEBUG "(pid %d) finish dumping TNC tree\n", current->pid);
1e51764a
AB
1018}
1019
1020static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
1021 void *priv)
1022{
1023 dbg_dump_znode(c, znode);
1024 return 0;
1025}
1026
1027/**
1028 * dbg_dump_index - dump the on-flash index.
1029 * @c: UBIFS file-system description object
1030 *
1031 * This function dumps whole UBIFS indexing B-tree, unlike 'dbg_dump_tnc()'
1032 * which dumps only in-memory znodes and does not read znodes which from flash.
1033 */
1034void dbg_dump_index(struct ubifs_info *c)
1035{
1036 dbg_walk_index(c, NULL, dump_znode, NULL);
1037}
1038
84abf972
AB
1039/**
1040 * dbg_save_space_info - save information about flash space.
1041 * @c: UBIFS file-system description object
1042 *
1043 * This function saves information about UBIFS free space, dirty space, etc, in
1044 * order to check it later.
1045 */
1046void dbg_save_space_info(struct ubifs_info *c)
1047{
1048 struct ubifs_debug_info *d = c->dbg;
7da6443a 1049 int freeable_cnt;
84abf972
AB
1050
1051 spin_lock(&c->space_lock);
7da6443a 1052 memcpy(&d->saved_lst, &c->lst, sizeof(struct ubifs_lp_stats));
f1bd66af
AB
1053 memcpy(&d->saved_bi, &c->bi, sizeof(struct ubifs_budg_info));
1054 d->saved_idx_gc_cnt = c->idx_gc_cnt;
7da6443a
AB
1055
1056 /*
1057 * We use a dirty hack here and zero out @c->freeable_cnt, because it
1058 * affects the free space calculations, and UBIFS might not know about
1059 * all freeable eraseblocks. Indeed, we know about freeable eraseblocks
1060 * only when we read their lprops, and we do this only lazily, upon the
1061 * need. So at any given point of time @c->freeable_cnt might be not
1062 * exactly accurate.
1063 *
1064 * Just one example about the issue we hit when we did not zero
1065 * @c->freeable_cnt.
1066 * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the
1067 * amount of free space in @d->saved_free
1068 * 2. We re-mount R/W, which makes UBIFS to read the "lsave"
1069 * information from flash, where we cache LEBs from various
1070 * categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()'
1071 * -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()'
1072 * -> 'ubifs_get_pnode()' -> 'update_cats()'
1073 * -> 'ubifs_add_to_cat()').
1074 * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt
1075 * becomes %1.
1076 * 4. We calculate the amount of free space when the re-mount is
1077 * finished in 'dbg_check_space_info()' and it does not match
1078 * @d->saved_free.
1079 */
1080 freeable_cnt = c->freeable_cnt;
1081 c->freeable_cnt = 0;
84abf972 1082 d->saved_free = ubifs_get_free_space_nolock(c);
7da6443a 1083 c->freeable_cnt = freeable_cnt;
84abf972
AB
1084 spin_unlock(&c->space_lock);
1085}
1086
1087/**
1088 * dbg_check_space_info - check flash space information.
1089 * @c: UBIFS file-system description object
1090 *
1091 * This function compares current flash space information with the information
1092 * which was saved when the 'dbg_save_space_info()' function was called.
1093 * Returns zero if the information has not changed, and %-EINVAL it it has
1094 * changed.
1095 */
1096int dbg_check_space_info(struct ubifs_info *c)
1097{
1098 struct ubifs_debug_info *d = c->dbg;
1099 struct ubifs_lp_stats lst;
7da6443a
AB
1100 long long free;
1101 int freeable_cnt;
84abf972
AB
1102
1103 spin_lock(&c->space_lock);
7da6443a
AB
1104 freeable_cnt = c->freeable_cnt;
1105 c->freeable_cnt = 0;
1106 free = ubifs_get_free_space_nolock(c);
1107 c->freeable_cnt = freeable_cnt;
84abf972 1108 spin_unlock(&c->space_lock);
84abf972
AB
1109
1110 if (free != d->saved_free) {
1111 ubifs_err("free space changed from %lld to %lld",
1112 d->saved_free, free);
1113 goto out;
1114 }
1115
1116 return 0;
1117
1118out:
1119 ubifs_msg("saved lprops statistics dump");
1120 dbg_dump_lstats(&d->saved_lst);
f1bd66af
AB
1121 ubifs_msg("saved budgeting info dump");
1122 dbg_dump_budg(c, &d->saved_bi);
1123 ubifs_msg("saved idx_gc_cnt %d", d->saved_idx_gc_cnt);
84abf972 1124 ubifs_msg("current lprops statistics dump");
f1bd66af 1125 ubifs_get_lp_stats(c, &lst);
e055f7e8 1126 dbg_dump_lstats(&lst);
f1bd66af
AB
1127 ubifs_msg("current budgeting info dump");
1128 dbg_dump_budg(c, &c->bi);
84abf972
AB
1129 dump_stack();
1130 return -EINVAL;
1131}
1132
1e51764a
AB
1133/**
1134 * dbg_check_synced_i_size - check synchronized inode size.
d808efb4 1135 * @c: UBIFS file-system description object
1e51764a
AB
1136 * @inode: inode to check
1137 *
1138 * If inode is clean, synchronized inode size has to be equivalent to current
1139 * inode size. This function has to be called only for locked inodes (@i_mutex
1140 * has to be locked). Returns %0 if synchronized inode size if correct, and
1141 * %-EINVAL if not.
1142 */
d808efb4 1143int dbg_check_synced_i_size(const struct ubifs_info *c, struct inode *inode)
1e51764a
AB
1144{
1145 int err = 0;
1146 struct ubifs_inode *ui = ubifs_inode(inode);
1147
2b1844a8 1148 if (!dbg_is_chk_gen(c))
1e51764a
AB
1149 return 0;
1150 if (!S_ISREG(inode->i_mode))
1151 return 0;
1152
1153 mutex_lock(&ui->ui_mutex);
1154 spin_lock(&ui->ui_lock);
1155 if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
1156 ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode "
1157 "is clean", ui->ui_size, ui->synced_i_size);
1158 ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
1159 inode->i_mode, i_size_read(inode));
1160 dbg_dump_stack();
1161 err = -EINVAL;
1162 }
1163 spin_unlock(&ui->ui_lock);
1164 mutex_unlock(&ui->ui_mutex);
1165 return err;
1166}
1167
1168/*
1169 * dbg_check_dir - check directory inode size and link count.
1170 * @c: UBIFS file-system description object
1171 * @dir: the directory to calculate size for
1172 * @size: the result is returned here
1173 *
1174 * This function makes sure that directory size and link count are correct.
1175 * Returns zero in case of success and a negative error code in case of
1176 * failure.
1177 *
1178 * Note, it is good idea to make sure the @dir->i_mutex is locked before
1179 * calling this function.
1180 */
1b51e983 1181int dbg_check_dir(struct ubifs_info *c, const struct inode *dir)
1e51764a
AB
1182{
1183 unsigned int nlink = 2;
1184 union ubifs_key key;
1185 struct ubifs_dent_node *dent, *pdent = NULL;
1186 struct qstr nm = { .name = NULL };
1187 loff_t size = UBIFS_INO_NODE_SZ;
1188
2b1844a8 1189 if (!dbg_is_chk_gen(c))
1e51764a
AB
1190 return 0;
1191
1192 if (!S_ISDIR(dir->i_mode))
1193 return 0;
1194
1195 lowest_dent_key(c, &key, dir->i_ino);
1196 while (1) {
1197 int err;
1198
1199 dent = ubifs_tnc_next_ent(c, &key, &nm);
1200 if (IS_ERR(dent)) {
1201 err = PTR_ERR(dent);
1202 if (err == -ENOENT)
1203 break;
1204 return err;
1205 }
1206
1207 nm.name = dent->name;
1208 nm.len = le16_to_cpu(dent->nlen);
1209 size += CALC_DENT_SIZE(nm.len);
1210 if (dent->type == UBIFS_ITYPE_DIR)
1211 nlink += 1;
1212 kfree(pdent);
1213 pdent = dent;
1214 key_read(c, &dent->key, &key);
1215 }
1216 kfree(pdent);
1217
1218 if (i_size_read(dir) != size) {
1219 ubifs_err("directory inode %lu has size %llu, "
1220 "but calculated size is %llu", dir->i_ino,
1221 (unsigned long long)i_size_read(dir),
1222 (unsigned long long)size);
4315fb40 1223 dbg_dump_inode(c, dir);
1e51764a
AB
1224 dump_stack();
1225 return -EINVAL;
1226 }
1227 if (dir->i_nlink != nlink) {
1228 ubifs_err("directory inode %lu has nlink %u, but calculated "
1229 "nlink is %u", dir->i_ino, dir->i_nlink, nlink);
4315fb40 1230 dbg_dump_inode(c, dir);
1e51764a
AB
1231 dump_stack();
1232 return -EINVAL;
1233 }
1234
1235 return 0;
1236}
1237
1238/**
1239 * dbg_check_key_order - make sure that colliding keys are properly ordered.
1240 * @c: UBIFS file-system description object
1241 * @zbr1: first zbranch
1242 * @zbr2: following zbranch
1243 *
1244 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
1245 * names of the direntries/xentries which are referred by the keys. This
1246 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
1247 * sure the name of direntry/xentry referred by @zbr1 is less than
1248 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
1249 * and a negative error code in case of failure.
1250 */
1251static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
1252 struct ubifs_zbranch *zbr2)
1253{
1254 int err, nlen1, nlen2, cmp;
1255 struct ubifs_dent_node *dent1, *dent2;
1256 union ubifs_key key;
1257
1258 ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
1259 dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1260 if (!dent1)
1261 return -ENOMEM;
1262 dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1263 if (!dent2) {
1264 err = -ENOMEM;
1265 goto out_free;
1266 }
1267
1268 err = ubifs_tnc_read_node(c, zbr1, dent1);
1269 if (err)
1270 goto out_free;
1271 err = ubifs_validate_entry(c, dent1);
1272 if (err)
1273 goto out_free;
1274
1275 err = ubifs_tnc_read_node(c, zbr2, dent2);
1276 if (err)
1277 goto out_free;
1278 err = ubifs_validate_entry(c, dent2);
1279 if (err)
1280 goto out_free;
1281
1282 /* Make sure node keys are the same as in zbranch */
1283 err = 1;
1284 key_read(c, &dent1->key, &key);
1285 if (keys_cmp(c, &zbr1->key, &key)) {
5d38b3ac
AB
1286 dbg_err("1st entry at %d:%d has key %s", zbr1->lnum,
1287 zbr1->offs, DBGKEY(&key));
1288 dbg_err("but it should have key %s according to tnc",
1289 DBGKEY(&zbr1->key));
2ba5f7ae 1290 dbg_dump_node(c, dent1);
552ff317 1291 goto out_free;
1e51764a
AB
1292 }
1293
1294 key_read(c, &dent2->key, &key);
1295 if (keys_cmp(c, &zbr2->key, &key)) {
5d38b3ac
AB
1296 dbg_err("2nd entry at %d:%d has key %s", zbr1->lnum,
1297 zbr1->offs, DBGKEY(&key));
1298 dbg_err("but it should have key %s according to tnc",
1299 DBGKEY(&zbr2->key));
2ba5f7ae 1300 dbg_dump_node(c, dent2);
552ff317 1301 goto out_free;
1e51764a
AB
1302 }
1303
1304 nlen1 = le16_to_cpu(dent1->nlen);
1305 nlen2 = le16_to_cpu(dent2->nlen);
1306
1307 cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
1308 if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
1309 err = 0;
1310 goto out_free;
1311 }
1312 if (cmp == 0 && nlen1 == nlen2)
5d38b3ac 1313 dbg_err("2 xent/dent nodes with the same name");
1e51764a 1314 else
5d38b3ac 1315 dbg_err("bad order of colliding key %s",
1e51764a
AB
1316 DBGKEY(&key));
1317
552ff317 1318 ubifs_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs);
1e51764a 1319 dbg_dump_node(c, dent1);
552ff317 1320 ubifs_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs);
1e51764a
AB
1321 dbg_dump_node(c, dent2);
1322
1323out_free:
1324 kfree(dent2);
1325 kfree(dent1);
1326 return err;
1327}
1328
1329/**
1330 * dbg_check_znode - check if znode is all right.
1331 * @c: UBIFS file-system description object
1332 * @zbr: zbranch which points to this znode
1333 *
1334 * This function makes sure that znode referred to by @zbr is all right.
1335 * Returns zero if it is, and %-EINVAL if it is not.
1336 */
1337static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
1338{
1339 struct ubifs_znode *znode = zbr->znode;
1340 struct ubifs_znode *zp = znode->parent;
1341 int n, err, cmp;
1342
1343 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
1344 err = 1;
1345 goto out;
1346 }
1347 if (znode->level < 0) {
1348 err = 2;
1349 goto out;
1350 }
1351 if (znode->iip < 0 || znode->iip >= c->fanout) {
1352 err = 3;
1353 goto out;
1354 }
1355
1356 if (zbr->len == 0)
1357 /* Only dirty zbranch may have no on-flash nodes */
1358 if (!ubifs_zn_dirty(znode)) {
1359 err = 4;
1360 goto out;
1361 }
1362
1363 if (ubifs_zn_dirty(znode)) {
1364 /*
1365 * If znode is dirty, its parent has to be dirty as well. The
1366 * order of the operation is important, so we have to have
1367 * memory barriers.
1368 */
1369 smp_mb();
1370 if (zp && !ubifs_zn_dirty(zp)) {
1371 /*
1372 * The dirty flag is atomic and is cleared outside the
1373 * TNC mutex, so znode's dirty flag may now have
1374 * been cleared. The child is always cleared before the
1375 * parent, so we just need to check again.
1376 */
1377 smp_mb();
1378 if (ubifs_zn_dirty(znode)) {
1379 err = 5;
1380 goto out;
1381 }
1382 }
1383 }
1384
1385 if (zp) {
1386 const union ubifs_key *min, *max;
1387
1388 if (znode->level != zp->level - 1) {
1389 err = 6;
1390 goto out;
1391 }
1392
1393 /* Make sure the 'parent' pointer in our znode is correct */
1394 err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
1395 if (!err) {
1396 /* This zbranch does not exist in the parent */
1397 err = 7;
1398 goto out;
1399 }
1400
1401 if (znode->iip >= zp->child_cnt) {
1402 err = 8;
1403 goto out;
1404 }
1405
1406 if (znode->iip != n) {
1407 /* This may happen only in case of collisions */
1408 if (keys_cmp(c, &zp->zbranch[n].key,
1409 &zp->zbranch[znode->iip].key)) {
1410 err = 9;
1411 goto out;
1412 }
1413 n = znode->iip;
1414 }
1415
1416 /*
1417 * Make sure that the first key in our znode is greater than or
1418 * equal to the key in the pointing zbranch.
1419 */
1420 min = &zbr->key;
1421 cmp = keys_cmp(c, min, &znode->zbranch[0].key);
1422 if (cmp == 1) {
1423 err = 10;
1424 goto out;
1425 }
1426
1427 if (n + 1 < zp->child_cnt) {
1428 max = &zp->zbranch[n + 1].key;
1429
1430 /*
1431 * Make sure the last key in our znode is less or
7d4e9ccb 1432 * equivalent than the key in the zbranch which goes
1e51764a
AB
1433 * after our pointing zbranch.
1434 */
1435 cmp = keys_cmp(c, max,
1436 &znode->zbranch[znode->child_cnt - 1].key);
1437 if (cmp == -1) {
1438 err = 11;
1439 goto out;
1440 }
1441 }
1442 } else {
1443 /* This may only be root znode */
1444 if (zbr != &c->zroot) {
1445 err = 12;
1446 goto out;
1447 }
1448 }
1449
1450 /*
1451 * Make sure that next key is greater or equivalent then the previous
1452 * one.
1453 */
1454 for (n = 1; n < znode->child_cnt; n++) {
1455 cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
1456 &znode->zbranch[n].key);
1457 if (cmp > 0) {
1458 err = 13;
1459 goto out;
1460 }
1461 if (cmp == 0) {
1462 /* This can only be keys with colliding hash */
1463 if (!is_hash_key(c, &znode->zbranch[n].key)) {
1464 err = 14;
1465 goto out;
1466 }
1467
1468 if (znode->level != 0 || c->replaying)
1469 continue;
1470
1471 /*
1472 * Colliding keys should follow binary order of
1473 * corresponding xentry/dentry names.
1474 */
1475 err = dbg_check_key_order(c, &znode->zbranch[n - 1],
1476 &znode->zbranch[n]);
1477 if (err < 0)
1478 return err;
1479 if (err) {
1480 err = 15;
1481 goto out;
1482 }
1483 }
1484 }
1485
1486 for (n = 0; n < znode->child_cnt; n++) {
1487 if (!znode->zbranch[n].znode &&
1488 (znode->zbranch[n].lnum == 0 ||
1489 znode->zbranch[n].len == 0)) {
1490 err = 16;
1491 goto out;
1492 }
1493
1494 if (znode->zbranch[n].lnum != 0 &&
1495 znode->zbranch[n].len == 0) {
1496 err = 17;
1497 goto out;
1498 }
1499
1500 if (znode->zbranch[n].lnum == 0 &&
1501 znode->zbranch[n].len != 0) {
1502 err = 18;
1503 goto out;
1504 }
1505
1506 if (znode->zbranch[n].lnum == 0 &&
1507 znode->zbranch[n].offs != 0) {
1508 err = 19;
1509 goto out;
1510 }
1511
1512 if (znode->level != 0 && znode->zbranch[n].znode)
1513 if (znode->zbranch[n].znode->parent != znode) {
1514 err = 20;
1515 goto out;
1516 }
1517 }
1518
1519 return 0;
1520
1521out:
1522 ubifs_err("failed, error %d", err);
1523 ubifs_msg("dump of the znode");
1524 dbg_dump_znode(c, znode);
1525 if (zp) {
1526 ubifs_msg("dump of the parent znode");
1527 dbg_dump_znode(c, zp);
1528 }
1529 dump_stack();
1530 return -EINVAL;
1531}
1532
1533/**
1534 * dbg_check_tnc - check TNC tree.
1535 * @c: UBIFS file-system description object
1536 * @extra: do extra checks that are possible at start commit
1537 *
1538 * This function traverses whole TNC tree and checks every znode. Returns zero
1539 * if everything is all right and %-EINVAL if something is wrong with TNC.
1540 */
1541int dbg_check_tnc(struct ubifs_info *c, int extra)
1542{
1543 struct ubifs_znode *znode;
1544 long clean_cnt = 0, dirty_cnt = 0;
1545 int err, last;
1546
2b1844a8 1547 if (!dbg_is_chk_tnc(c))
1e51764a
AB
1548 return 0;
1549
1550 ubifs_assert(mutex_is_locked(&c->tnc_mutex));
1551 if (!c->zroot.znode)
1552 return 0;
1553
1554 znode = ubifs_tnc_postorder_first(c->zroot.znode);
1555 while (1) {
1556 struct ubifs_znode *prev;
1557 struct ubifs_zbranch *zbr;
1558
1559 if (!znode->parent)
1560 zbr = &c->zroot;
1561 else
1562 zbr = &znode->parent->zbranch[znode->iip];
1563
1564 err = dbg_check_znode(c, zbr);
1565 if (err)
1566 return err;
1567
1568 if (extra) {
1569 if (ubifs_zn_dirty(znode))
1570 dirty_cnt += 1;
1571 else
1572 clean_cnt += 1;
1573 }
1574
1575 prev = znode;
1576 znode = ubifs_tnc_postorder_next(znode);
1577 if (!znode)
1578 break;
1579
1580 /*
1581 * If the last key of this znode is equivalent to the first key
1582 * of the next znode (collision), then check order of the keys.
1583 */
1584 last = prev->child_cnt - 1;
1585 if (prev->level == 0 && znode->level == 0 && !c->replaying &&
1586 !keys_cmp(c, &prev->zbranch[last].key,
1587 &znode->zbranch[0].key)) {
1588 err = dbg_check_key_order(c, &prev->zbranch[last],
1589 &znode->zbranch[0]);
1590 if (err < 0)
1591 return err;
1592 if (err) {
1593 ubifs_msg("first znode");
1594 dbg_dump_znode(c, prev);
1595 ubifs_msg("second znode");
1596 dbg_dump_znode(c, znode);
1597 return -EINVAL;
1598 }
1599 }
1600 }
1601
1602 if (extra) {
1603 if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
1604 ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld",
1605 atomic_long_read(&c->clean_zn_cnt),
1606 clean_cnt);
1607 return -EINVAL;
1608 }
1609 if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
1610 ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld",
1611 atomic_long_read(&c->dirty_zn_cnt),
1612 dirty_cnt);
1613 return -EINVAL;
1614 }
1615 }
1616
1617 return 0;
1618}
1619
1620/**
1621 * dbg_walk_index - walk the on-flash index.
1622 * @c: UBIFS file-system description object
1623 * @leaf_cb: called for each leaf node
1624 * @znode_cb: called for each indexing node
227c75c9 1625 * @priv: private data which is passed to callbacks
1e51764a
AB
1626 *
1627 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1628 * node and @znode_cb for each indexing node. Returns zero in case of success
1629 * and a negative error code in case of failure.
1630 *
1631 * It would be better if this function removed every znode it pulled to into
1632 * the TNC, so that the behavior more closely matched the non-debugging
1633 * behavior.
1634 */
1635int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1636 dbg_znode_callback znode_cb, void *priv)
1637{
1638 int err;
1639 struct ubifs_zbranch *zbr;
1640 struct ubifs_znode *znode, *child;
1641
1642 mutex_lock(&c->tnc_mutex);
1643 /* If the root indexing node is not in TNC - pull it */
1644 if (!c->zroot.znode) {
1645 c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1646 if (IS_ERR(c->zroot.znode)) {
1647 err = PTR_ERR(c->zroot.znode);
1648 c->zroot.znode = NULL;
1649 goto out_unlock;
1650 }
1651 }
1652
1653 /*
1654 * We are going to traverse the indexing tree in the postorder manner.
1655 * Go down and find the leftmost indexing node where we are going to
1656 * start from.
1657 */
1658 znode = c->zroot.znode;
1659 while (znode->level > 0) {
1660 zbr = &znode->zbranch[0];
1661 child = zbr->znode;
1662 if (!child) {
1663 child = ubifs_load_znode(c, zbr, znode, 0);
1664 if (IS_ERR(child)) {
1665 err = PTR_ERR(child);
1666 goto out_unlock;
1667 }
1668 zbr->znode = child;
1669 }
1670
1671 znode = child;
1672 }
1673
1674 /* Iterate over all indexing nodes */
1675 while (1) {
1676 int idx;
1677
1678 cond_resched();
1679
1680 if (znode_cb) {
1681 err = znode_cb(c, znode, priv);
1682 if (err) {
1683 ubifs_err("znode checking function returned "
1684 "error %d", err);
1685 dbg_dump_znode(c, znode);
1686 goto out_dump;
1687 }
1688 }
1689 if (leaf_cb && znode->level == 0) {
1690 for (idx = 0; idx < znode->child_cnt; idx++) {
1691 zbr = &znode->zbranch[idx];
1692 err = leaf_cb(c, zbr, priv);
1693 if (err) {
1694 ubifs_err("leaf checking function "
1695 "returned error %d, for leaf "
1696 "at LEB %d:%d",
1697 err, zbr->lnum, zbr->offs);
1698 goto out_dump;
1699 }
1700 }
1701 }
1702
1703 if (!znode->parent)
1704 break;
1705
1706 idx = znode->iip + 1;
1707 znode = znode->parent;
1708 if (idx < znode->child_cnt) {
1709 /* Switch to the next index in the parent */
1710 zbr = &znode->zbranch[idx];
1711 child = zbr->znode;
1712 if (!child) {
1713 child = ubifs_load_znode(c, zbr, znode, idx);
1714 if (IS_ERR(child)) {
1715 err = PTR_ERR(child);
1716 goto out_unlock;
1717 }
1718 zbr->znode = child;
1719 }
1720 znode = child;
1721 } else
1722 /*
1723 * This is the last child, switch to the parent and
1724 * continue.
1725 */
1726 continue;
1727
1728 /* Go to the lowest leftmost znode in the new sub-tree */
1729 while (znode->level > 0) {
1730 zbr = &znode->zbranch[0];
1731 child = zbr->znode;
1732 if (!child) {
1733 child = ubifs_load_znode(c, zbr, znode, 0);
1734 if (IS_ERR(child)) {
1735 err = PTR_ERR(child);
1736 goto out_unlock;
1737 }
1738 zbr->znode = child;
1739 }
1740 znode = child;
1741 }
1742 }
1743
1744 mutex_unlock(&c->tnc_mutex);
1745 return 0;
1746
1747out_dump:
1748 if (znode->parent)
1749 zbr = &znode->parent->zbranch[znode->iip];
1750 else
1751 zbr = &c->zroot;
1752 ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1753 dbg_dump_znode(c, znode);
1754out_unlock:
1755 mutex_unlock(&c->tnc_mutex);
1756 return err;
1757}
1758
1759/**
1760 * add_size - add znode size to partially calculated index size.
1761 * @c: UBIFS file-system description object
1762 * @znode: znode to add size for
1763 * @priv: partially calculated index size
1764 *
1765 * This is a helper function for 'dbg_check_idx_size()' which is called for
1766 * every indexing node and adds its size to the 'long long' variable pointed to
1767 * by @priv.
1768 */
1769static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1770{
1771 long long *idx_size = priv;
1772 int add;
1773
1774 add = ubifs_idx_node_sz(c, znode->child_cnt);
1775 add = ALIGN(add, 8);
1776 *idx_size += add;
1777 return 0;
1778}
1779
1780/**
1781 * dbg_check_idx_size - check index size.
1782 * @c: UBIFS file-system description object
1783 * @idx_size: size to check
1784 *
1785 * This function walks the UBIFS index, calculates its size and checks that the
1786 * size is equivalent to @idx_size. Returns zero in case of success and a
1787 * negative error code in case of failure.
1788 */
1789int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
1790{
1791 int err;
1792 long long calc = 0;
1793
2b1844a8 1794 if (!dbg_is_chk_idx_sz(c))
1e51764a
AB
1795 return 0;
1796
1797 err = dbg_walk_index(c, NULL, add_size, &calc);
1798 if (err) {
1799 ubifs_err("error %d while walking the index", err);
1800 return err;
1801 }
1802
1803 if (calc != idx_size) {
1804 ubifs_err("index size check failed: calculated size is %lld, "
1805 "should be %lld", calc, idx_size);
1806 dump_stack();
1807 return -EINVAL;
1808 }
1809
1810 return 0;
1811}
1812
1813/**
1814 * struct fsck_inode - information about an inode used when checking the file-system.
1815 * @rb: link in the RB-tree of inodes
1816 * @inum: inode number
1817 * @mode: inode type, permissions, etc
1818 * @nlink: inode link count
1819 * @xattr_cnt: count of extended attributes
1820 * @references: how many directory/xattr entries refer this inode (calculated
1821 * while walking the index)
1822 * @calc_cnt: for directory inode count of child directories
1823 * @size: inode size (read from on-flash inode)
1824 * @xattr_sz: summary size of all extended attributes (read from on-flash
1825 * inode)
1826 * @calc_sz: for directories calculated directory size
1827 * @calc_xcnt: count of extended attributes
1828 * @calc_xsz: calculated summary size of all extended attributes
1829 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1830 * inode (read from on-flash inode)
1831 * @calc_xnms: calculated sum of lengths of all extended attribute names
1832 */
1833struct fsck_inode {
1834 struct rb_node rb;
1835 ino_t inum;
1836 umode_t mode;
1837 unsigned int nlink;
1838 unsigned int xattr_cnt;
1839 int references;
1840 int calc_cnt;
1841 long long size;
1842 unsigned int xattr_sz;
1843 long long calc_sz;
1844 long long calc_xcnt;
1845 long long calc_xsz;
1846 unsigned int xattr_nms;
1847 long long calc_xnms;
1848};
1849
1850/**
1851 * struct fsck_data - private FS checking information.
1852 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1853 */
1854struct fsck_data {
1855 struct rb_root inodes;
1856};
1857
1858/**
1859 * add_inode - add inode information to RB-tree of inodes.
1860 * @c: UBIFS file-system description object
1861 * @fsckd: FS checking information
1862 * @ino: raw UBIFS inode to add
1863 *
1864 * This is a helper function for 'check_leaf()' which adds information about
1865 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1866 * case of success and a negative error code in case of failure.
1867 */
1868static struct fsck_inode *add_inode(struct ubifs_info *c,
1869 struct fsck_data *fsckd,
1870 struct ubifs_ino_node *ino)
1871{
1872 struct rb_node **p, *parent = NULL;
1873 struct fsck_inode *fscki;
1874 ino_t inum = key_inum_flash(c, &ino->key);
45cd5cdd
AB
1875 struct inode *inode;
1876 struct ubifs_inode *ui;
1e51764a
AB
1877
1878 p = &fsckd->inodes.rb_node;
1879 while (*p) {
1880 parent = *p;
1881 fscki = rb_entry(parent, struct fsck_inode, rb);
1882 if (inum < fscki->inum)
1883 p = &(*p)->rb_left;
1884 else if (inum > fscki->inum)
1885 p = &(*p)->rb_right;
1886 else
1887 return fscki;
1888 }
1889
1890 if (inum > c->highest_inum) {
1891 ubifs_err("too high inode number, max. is %lu",
e84461ad 1892 (unsigned long)c->highest_inum);
1e51764a
AB
1893 return ERR_PTR(-EINVAL);
1894 }
1895
1896 fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
1897 if (!fscki)
1898 return ERR_PTR(-ENOMEM);
1899
45cd5cdd
AB
1900 inode = ilookup(c->vfs_sb, inum);
1901
1e51764a 1902 fscki->inum = inum;
45cd5cdd
AB
1903 /*
1904 * If the inode is present in the VFS inode cache, use it instead of
1905 * the on-flash inode which might be out-of-date. E.g., the size might
1906 * be out-of-date. If we do not do this, the following may happen, for
1907 * example:
1908 * 1. A power cut happens
1909 * 2. We mount the file-system R/O, the replay process fixes up the
1910 * inode size in the VFS cache, but on on-flash.
1911 * 3. 'check_leaf()' fails because it hits a data node beyond inode
1912 * size.
1913 */
1914 if (!inode) {
1915 fscki->nlink = le32_to_cpu(ino->nlink);
1916 fscki->size = le64_to_cpu(ino->size);
1917 fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
1918 fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
1919 fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
1920 fscki->mode = le32_to_cpu(ino->mode);
1921 } else {
1922 ui = ubifs_inode(inode);
1923 fscki->nlink = inode->i_nlink;
1924 fscki->size = inode->i_size;
1925 fscki->xattr_cnt = ui->xattr_cnt;
1926 fscki->xattr_sz = ui->xattr_size;
1927 fscki->xattr_nms = ui->xattr_names;
1928 fscki->mode = inode->i_mode;
1929 iput(inode);
1930 }
1931
1e51764a
AB
1932 if (S_ISDIR(fscki->mode)) {
1933 fscki->calc_sz = UBIFS_INO_NODE_SZ;
1934 fscki->calc_cnt = 2;
1935 }
45cd5cdd 1936
1e51764a
AB
1937 rb_link_node(&fscki->rb, parent, p);
1938 rb_insert_color(&fscki->rb, &fsckd->inodes);
45cd5cdd 1939
1e51764a
AB
1940 return fscki;
1941}
1942
1943/**
1944 * search_inode - search inode in the RB-tree of inodes.
1945 * @fsckd: FS checking information
1946 * @inum: inode number to search
1947 *
1948 * This is a helper function for 'check_leaf()' which searches inode @inum in
1949 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1950 * the inode was not found.
1951 */
1952static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
1953{
1954 struct rb_node *p;
1955 struct fsck_inode *fscki;
1956
1957 p = fsckd->inodes.rb_node;
1958 while (p) {
1959 fscki = rb_entry(p, struct fsck_inode, rb);
1960 if (inum < fscki->inum)
1961 p = p->rb_left;
1962 else if (inum > fscki->inum)
1963 p = p->rb_right;
1964 else
1965 return fscki;
1966 }
1967 return NULL;
1968}
1969
1970/**
1971 * read_add_inode - read inode node and add it to RB-tree of inodes.
1972 * @c: UBIFS file-system description object
1973 * @fsckd: FS checking information
1974 * @inum: inode number to read
1975 *
1976 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1977 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1978 * information pointer in case of success and a negative error code in case of
1979 * failure.
1980 */
1981static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1982 struct fsck_data *fsckd, ino_t inum)
1983{
1984 int n, err;
1985 union ubifs_key key;
1986 struct ubifs_znode *znode;
1987 struct ubifs_zbranch *zbr;
1988 struct ubifs_ino_node *ino;
1989 struct fsck_inode *fscki;
1990
1991 fscki = search_inode(fsckd, inum);
1992 if (fscki)
1993 return fscki;
1994
1995 ino_key_init(c, &key, inum);
1996 err = ubifs_lookup_level0(c, &key, &znode, &n);
1997 if (!err) {
e84461ad 1998 ubifs_err("inode %lu not found in index", (unsigned long)inum);
1e51764a
AB
1999 return ERR_PTR(-ENOENT);
2000 } else if (err < 0) {
e84461ad
AB
2001 ubifs_err("error %d while looking up inode %lu",
2002 err, (unsigned long)inum);
1e51764a
AB
2003 return ERR_PTR(err);
2004 }
2005
2006 zbr = &znode->zbranch[n];
2007 if (zbr->len < UBIFS_INO_NODE_SZ) {
e84461ad
AB
2008 ubifs_err("bad node %lu node length %d",
2009 (unsigned long)inum, zbr->len);
1e51764a
AB
2010 return ERR_PTR(-EINVAL);
2011 }
2012
2013 ino = kmalloc(zbr->len, GFP_NOFS);
2014 if (!ino)
2015 return ERR_PTR(-ENOMEM);
2016
2017 err = ubifs_tnc_read_node(c, zbr, ino);
2018 if (err) {
2019 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2020 zbr->lnum, zbr->offs, err);
2021 kfree(ino);
2022 return ERR_PTR(err);
2023 }
2024
2025 fscki = add_inode(c, fsckd, ino);
2026 kfree(ino);
2027 if (IS_ERR(fscki)) {
2028 ubifs_err("error %ld while adding inode %lu node",
e84461ad 2029 PTR_ERR(fscki), (unsigned long)inum);
1e51764a
AB
2030 return fscki;
2031 }
2032
2033 return fscki;
2034}
2035
2036/**
2037 * check_leaf - check leaf node.
2038 * @c: UBIFS file-system description object
2039 * @zbr: zbranch of the leaf node to check
2040 * @priv: FS checking information
2041 *
2042 * This is a helper function for 'dbg_check_filesystem()' which is called for
2043 * every single leaf node while walking the indexing tree. It checks that the
2044 * leaf node referred from the indexing tree exists, has correct CRC, and does
2045 * some other basic validation. This function is also responsible for building
2046 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
2047 * calculates reference count, size, etc for each inode in order to later
2048 * compare them to the information stored inside the inodes and detect possible
2049 * inconsistencies. Returns zero in case of success and a negative error code
2050 * in case of failure.
2051 */
2052static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
2053 void *priv)
2054{
2055 ino_t inum;
2056 void *node;
2057 struct ubifs_ch *ch;
2058 int err, type = key_type(c, &zbr->key);
2059 struct fsck_inode *fscki;
2060
2061 if (zbr->len < UBIFS_CH_SZ) {
2062 ubifs_err("bad leaf length %d (LEB %d:%d)",
2063 zbr->len, zbr->lnum, zbr->offs);
2064 return -EINVAL;
2065 }
2066
2067 node = kmalloc(zbr->len, GFP_NOFS);
2068 if (!node)
2069 return -ENOMEM;
2070
2071 err = ubifs_tnc_read_node(c, zbr, node);
2072 if (err) {
2073 ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
2074 zbr->lnum, zbr->offs, err);
2075 goto out_free;
2076 }
2077
2078 /* If this is an inode node, add it to RB-tree of inodes */
2079 if (type == UBIFS_INO_KEY) {
2080 fscki = add_inode(c, priv, node);
2081 if (IS_ERR(fscki)) {
2082 err = PTR_ERR(fscki);
2083 ubifs_err("error %d while adding inode node", err);
2084 goto out_dump;
2085 }
2086 goto out;
2087 }
2088
2089 if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
2090 type != UBIFS_DATA_KEY) {
2091 ubifs_err("unexpected node type %d at LEB %d:%d",
2092 type, zbr->lnum, zbr->offs);
2093 err = -EINVAL;
2094 goto out_free;
2095 }
2096
2097 ch = node;
2098 if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
2099 ubifs_err("too high sequence number, max. is %llu",
2100 c->max_sqnum);
2101 err = -EINVAL;
2102 goto out_dump;
2103 }
2104
2105 if (type == UBIFS_DATA_KEY) {
2106 long long blk_offs;
2107 struct ubifs_data_node *dn = node;
2108
2109 /*
2110 * Search the inode node this data node belongs to and insert
2111 * it to the RB-tree of inodes.
2112 */
2113 inum = key_inum_flash(c, &dn->key);
2114 fscki = read_add_inode(c, priv, inum);
2115 if (IS_ERR(fscki)) {
2116 err = PTR_ERR(fscki);
2117 ubifs_err("error %d while processing data node and "
e84461ad
AB
2118 "trying to find inode node %lu",
2119 err, (unsigned long)inum);
1e51764a
AB
2120 goto out_dump;
2121 }
2122
2123 /* Make sure the data node is within inode size */
2124 blk_offs = key_block_flash(c, &dn->key);
2125 blk_offs <<= UBIFS_BLOCK_SHIFT;
2126 blk_offs += le32_to_cpu(dn->size);
2127 if (blk_offs > fscki->size) {
2128 ubifs_err("data node at LEB %d:%d is not within inode "
2129 "size %lld", zbr->lnum, zbr->offs,
2130 fscki->size);
2131 err = -EINVAL;
2132 goto out_dump;
2133 }
2134 } else {
2135 int nlen;
2136 struct ubifs_dent_node *dent = node;
2137 struct fsck_inode *fscki1;
2138
2139 err = ubifs_validate_entry(c, dent);
2140 if (err)
2141 goto out_dump;
2142
2143 /*
2144 * Search the inode node this entry refers to and the parent
2145 * inode node and insert them to the RB-tree of inodes.
2146 */
2147 inum = le64_to_cpu(dent->inum);
2148 fscki = read_add_inode(c, priv, inum);
2149 if (IS_ERR(fscki)) {
2150 err = PTR_ERR(fscki);
2151 ubifs_err("error %d while processing entry node and "
e84461ad
AB
2152 "trying to find inode node %lu",
2153 err, (unsigned long)inum);
1e51764a
AB
2154 goto out_dump;
2155 }
2156
2157 /* Count how many direntries or xentries refers this inode */
2158 fscki->references += 1;
2159
2160 inum = key_inum_flash(c, &dent->key);
2161 fscki1 = read_add_inode(c, priv, inum);
2162 if (IS_ERR(fscki1)) {
b38882f5 2163 err = PTR_ERR(fscki1);
1e51764a
AB
2164 ubifs_err("error %d while processing entry node and "
2165 "trying to find parent inode node %lu",
e84461ad 2166 err, (unsigned long)inum);
1e51764a
AB
2167 goto out_dump;
2168 }
2169
2170 nlen = le16_to_cpu(dent->nlen);
2171 if (type == UBIFS_XENT_KEY) {
2172 fscki1->calc_xcnt += 1;
2173 fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
2174 fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
2175 fscki1->calc_xnms += nlen;
2176 } else {
2177 fscki1->calc_sz += CALC_DENT_SIZE(nlen);
2178 if (dent->type == UBIFS_ITYPE_DIR)
2179 fscki1->calc_cnt += 1;
2180 }
2181 }
2182
2183out:
2184 kfree(node);
2185 return 0;
2186
2187out_dump:
2188 ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
2189 dbg_dump_node(c, node);
2190out_free:
2191 kfree(node);
2192 return err;
2193}
2194
2195/**
2196 * free_inodes - free RB-tree of inodes.
2197 * @fsckd: FS checking information
2198 */
2199static void free_inodes(struct fsck_data *fsckd)
2200{
2201 struct rb_node *this = fsckd->inodes.rb_node;
2202 struct fsck_inode *fscki;
2203
2204 while (this) {
2205 if (this->rb_left)
2206 this = this->rb_left;
2207 else if (this->rb_right)
2208 this = this->rb_right;
2209 else {
2210 fscki = rb_entry(this, struct fsck_inode, rb);
2211 this = rb_parent(this);
2212 if (this) {
2213 if (this->rb_left == &fscki->rb)
2214 this->rb_left = NULL;
2215 else
2216 this->rb_right = NULL;
2217 }
2218 kfree(fscki);
2219 }
2220 }
2221}
2222
2223/**
2224 * check_inodes - checks all inodes.
2225 * @c: UBIFS file-system description object
2226 * @fsckd: FS checking information
2227 *
2228 * This is a helper function for 'dbg_check_filesystem()' which walks the
2229 * RB-tree of inodes after the index scan has been finished, and checks that
2230 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
2231 * %-EINVAL if not, and a negative error code in case of failure.
2232 */
2233static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
2234{
2235 int n, err;
2236 union ubifs_key key;
2237 struct ubifs_znode *znode;
2238 struct ubifs_zbranch *zbr;
2239 struct ubifs_ino_node *ino;
2240 struct fsck_inode *fscki;
2241 struct rb_node *this = rb_first(&fsckd->inodes);
2242
2243 while (this) {
2244 fscki = rb_entry(this, struct fsck_inode, rb);
2245 this = rb_next(this);
2246
2247 if (S_ISDIR(fscki->mode)) {
2248 /*
2249 * Directories have to have exactly one reference (they
2250 * cannot have hardlinks), although root inode is an
2251 * exception.
2252 */
2253 if (fscki->inum != UBIFS_ROOT_INO &&
2254 fscki->references != 1) {
2255 ubifs_err("directory inode %lu has %d "
2256 "direntries which refer it, but "
e84461ad
AB
2257 "should be 1",
2258 (unsigned long)fscki->inum,
1e51764a
AB
2259 fscki->references);
2260 goto out_dump;
2261 }
2262 if (fscki->inum == UBIFS_ROOT_INO &&
2263 fscki->references != 0) {
2264 ubifs_err("root inode %lu has non-zero (%d) "
2265 "direntries which refer it",
e84461ad
AB
2266 (unsigned long)fscki->inum,
2267 fscki->references);
1e51764a
AB
2268 goto out_dump;
2269 }
2270 if (fscki->calc_sz != fscki->size) {
2271 ubifs_err("directory inode %lu size is %lld, "
2272 "but calculated size is %lld",
e84461ad
AB
2273 (unsigned long)fscki->inum,
2274 fscki->size, fscki->calc_sz);
1e51764a
AB
2275 goto out_dump;
2276 }
2277 if (fscki->calc_cnt != fscki->nlink) {
2278 ubifs_err("directory inode %lu nlink is %d, "
2279 "but calculated nlink is %d",
e84461ad
AB
2280 (unsigned long)fscki->inum,
2281 fscki->nlink, fscki->calc_cnt);
1e51764a
AB
2282 goto out_dump;
2283 }
2284 } else {
2285 if (fscki->references != fscki->nlink) {
2286 ubifs_err("inode %lu nlink is %d, but "
e84461ad
AB
2287 "calculated nlink is %d",
2288 (unsigned long)fscki->inum,
1e51764a
AB
2289 fscki->nlink, fscki->references);
2290 goto out_dump;
2291 }
2292 }
2293 if (fscki->xattr_sz != fscki->calc_xsz) {
2294 ubifs_err("inode %lu has xattr size %u, but "
2295 "calculated size is %lld",
e84461ad 2296 (unsigned long)fscki->inum, fscki->xattr_sz,
1e51764a
AB
2297 fscki->calc_xsz);
2298 goto out_dump;
2299 }
2300 if (fscki->xattr_cnt != fscki->calc_xcnt) {
2301 ubifs_err("inode %lu has %u xattrs, but "
e84461ad
AB
2302 "calculated count is %lld",
2303 (unsigned long)fscki->inum,
1e51764a
AB
2304 fscki->xattr_cnt, fscki->calc_xcnt);
2305 goto out_dump;
2306 }
2307 if (fscki->xattr_nms != fscki->calc_xnms) {
2308 ubifs_err("inode %lu has xattr names' size %u, but "
2309 "calculated names' size is %lld",
e84461ad 2310 (unsigned long)fscki->inum, fscki->xattr_nms,
1e51764a
AB
2311 fscki->calc_xnms);
2312 goto out_dump;
2313 }
2314 }
2315
2316 return 0;
2317
2318out_dump:
2319 /* Read the bad inode and dump it */
2320 ino_key_init(c, &key, fscki->inum);
2321 err = ubifs_lookup_level0(c, &key, &znode, &n);
2322 if (!err) {
e84461ad
AB
2323 ubifs_err("inode %lu not found in index",
2324 (unsigned long)fscki->inum);
1e51764a
AB
2325 return -ENOENT;
2326 } else if (err < 0) {
2327 ubifs_err("error %d while looking up inode %lu",
e84461ad 2328 err, (unsigned long)fscki->inum);
1e51764a
AB
2329 return err;
2330 }
2331
2332 zbr = &znode->zbranch[n];
2333 ino = kmalloc(zbr->len, GFP_NOFS);
2334 if (!ino)
2335 return -ENOMEM;
2336
2337 err = ubifs_tnc_read_node(c, zbr, ino);
2338 if (err) {
2339 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2340 zbr->lnum, zbr->offs, err);
2341 kfree(ino);
2342 return err;
2343 }
2344
2345 ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",
e84461ad 2346 (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
1e51764a
AB
2347 dbg_dump_node(c, ino);
2348 kfree(ino);
2349 return -EINVAL;
2350}
2351
2352/**
2353 * dbg_check_filesystem - check the file-system.
2354 * @c: UBIFS file-system description object
2355 *
2356 * This function checks the file system, namely:
2357 * o makes sure that all leaf nodes exist and their CRCs are correct;
2358 * o makes sure inode nlink, size, xattr size/count are correct (for all
2359 * inodes).
2360 *
2361 * The function reads whole indexing tree and all nodes, so it is pretty
2362 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2363 * not, and a negative error code in case of failure.
2364 */
2365int dbg_check_filesystem(struct ubifs_info *c)
2366{
2367 int err;
2368 struct fsck_data fsckd;
2369
2b1844a8 2370 if (!dbg_is_chk_fs(c))
1e51764a
AB
2371 return 0;
2372
2373 fsckd.inodes = RB_ROOT;
2374 err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
2375 if (err)
2376 goto out_free;
2377
2378 err = check_inodes(c, &fsckd);
2379 if (err)
2380 goto out_free;
2381
2382 free_inodes(&fsckd);
2383 return 0;
2384
2385out_free:
2386 ubifs_err("file-system check failed with error %d", err);
2387 dump_stack();
2388 free_inodes(&fsckd);
2389 return err;
2390}
2391
3bb66b47
AB
2392/**
2393 * dbg_check_data_nodes_order - check that list of data nodes is sorted.
2394 * @c: UBIFS file-system description object
2395 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2396 *
2397 * This function returns zero if the list of data nodes is sorted correctly,
2398 * and %-EINVAL if not.
2399 */
2400int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head)
2401{
2402 struct list_head *cur;
2403 struct ubifs_scan_node *sa, *sb;
2404
2b1844a8 2405 if (!dbg_is_chk_gen(c))
3bb66b47
AB
2406 return 0;
2407
2408 for (cur = head->next; cur->next != head; cur = cur->next) {
2409 ino_t inuma, inumb;
2410 uint32_t blka, blkb;
2411
2412 cond_resched();
2413 sa = container_of(cur, struct ubifs_scan_node, list);
2414 sb = container_of(cur->next, struct ubifs_scan_node, list);
2415
2416 if (sa->type != UBIFS_DATA_NODE) {
2417 ubifs_err("bad node type %d", sa->type);
2418 dbg_dump_node(c, sa->node);
2419 return -EINVAL;
2420 }
2421 if (sb->type != UBIFS_DATA_NODE) {
2422 ubifs_err("bad node type %d", sb->type);
2423 dbg_dump_node(c, sb->node);
2424 return -EINVAL;
2425 }
2426
2427 inuma = key_inum(c, &sa->key);
2428 inumb = key_inum(c, &sb->key);
2429
2430 if (inuma < inumb)
2431 continue;
2432 if (inuma > inumb) {
2433 ubifs_err("larger inum %lu goes before inum %lu",
2434 (unsigned long)inuma, (unsigned long)inumb);
2435 goto error_dump;
2436 }
2437
2438 blka = key_block(c, &sa->key);
2439 blkb = key_block(c, &sb->key);
2440
2441 if (blka > blkb) {
2442 ubifs_err("larger block %u goes before %u", blka, blkb);
2443 goto error_dump;
2444 }
2445 if (blka == blkb) {
2446 ubifs_err("two data nodes for the same block");
2447 goto error_dump;
2448 }
2449 }
2450
2451 return 0;
2452
2453error_dump:
2454 dbg_dump_node(c, sa->node);
2455 dbg_dump_node(c, sb->node);
2456 return -EINVAL;
2457}
2458
2459/**
2460 * dbg_check_nondata_nodes_order - check that list of data nodes is sorted.
2461 * @c: UBIFS file-system description object
2462 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2463 *
2464 * This function returns zero if the list of non-data nodes is sorted correctly,
2465 * and %-EINVAL if not.
2466 */
2467int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head)
2468{
2469 struct list_head *cur;
2470 struct ubifs_scan_node *sa, *sb;
2471
2b1844a8 2472 if (!dbg_is_chk_gen(c))
3bb66b47
AB
2473 return 0;
2474
2475 for (cur = head->next; cur->next != head; cur = cur->next) {
2476 ino_t inuma, inumb;
2477 uint32_t hasha, hashb;
2478
2479 cond_resched();
2480 sa = container_of(cur, struct ubifs_scan_node, list);
2481 sb = container_of(cur->next, struct ubifs_scan_node, list);
2482
2483 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2484 sa->type != UBIFS_XENT_NODE) {
2485 ubifs_err("bad node type %d", sa->type);
2486 dbg_dump_node(c, sa->node);
2487 return -EINVAL;
2488 }
2489 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2490 sa->type != UBIFS_XENT_NODE) {
2491 ubifs_err("bad node type %d", sb->type);
2492 dbg_dump_node(c, sb->node);
2493 return -EINVAL;
2494 }
2495
2496 if (sa->type != UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2497 ubifs_err("non-inode node goes before inode node");
2498 goto error_dump;
2499 }
2500
2501 if (sa->type == UBIFS_INO_NODE && sb->type != UBIFS_INO_NODE)
2502 continue;
2503
2504 if (sa->type == UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2505 /* Inode nodes are sorted in descending size order */
2506 if (sa->len < sb->len) {
2507 ubifs_err("smaller inode node goes first");
2508 goto error_dump;
2509 }
2510 continue;
2511 }
2512
2513 /*
2514 * This is either a dentry or xentry, which should be sorted in
2515 * ascending (parent ino, hash) order.
2516 */
2517 inuma = key_inum(c, &sa->key);
2518 inumb = key_inum(c, &sb->key);
2519
2520 if (inuma < inumb)
2521 continue;
2522 if (inuma > inumb) {
2523 ubifs_err("larger inum %lu goes before inum %lu",
2524 (unsigned long)inuma, (unsigned long)inumb);
2525 goto error_dump;
2526 }
2527
2528 hasha = key_block(c, &sa->key);
2529 hashb = key_block(c, &sb->key);
2530
2531 if (hasha > hashb) {
c4361570
AB
2532 ubifs_err("larger hash %u goes before %u",
2533 hasha, hashb);
3bb66b47
AB
2534 goto error_dump;
2535 }
2536 }
2537
2538 return 0;
2539
2540error_dump:
2541 ubifs_msg("dumping first node");
2542 dbg_dump_node(c, sa->node);
2543 ubifs_msg("dumping second node");
2544 dbg_dump_node(c, sb->node);
2545 return -EINVAL;
2546 return 0;
2547}
2548
1e51764a
AB
2549/* Failure mode for recovery testing */
2550
2551#define chance(n, d) (simple_rand() <= (n) * 32768LL / (d))
2552
2553struct failure_mode_info {
2554 struct list_head list;
2555 struct ubifs_info *c;
2556};
2557
2558static LIST_HEAD(fmi_list);
2559static DEFINE_SPINLOCK(fmi_lock);
2560
2561static unsigned int next;
2562
2563static int simple_rand(void)
2564{
2565 if (next == 0)
2566 next = current->pid;
2567 next = next * 1103515245 + 12345;
2568 return (next >> 16) & 32767;
2569}
2570
17c2f9f8 2571static void failure_mode_init(struct ubifs_info *c)
1e51764a
AB
2572{
2573 struct failure_mode_info *fmi;
2574
2575 fmi = kmalloc(sizeof(struct failure_mode_info), GFP_NOFS);
2576 if (!fmi) {
552ff317 2577 ubifs_err("Failed to register failure mode - no memory");
1e51764a
AB
2578 return;
2579 }
2580 fmi->c = c;
2581 spin_lock(&fmi_lock);
2582 list_add_tail(&fmi->list, &fmi_list);
2583 spin_unlock(&fmi_lock);
2584}
2585
17c2f9f8 2586static void failure_mode_exit(struct ubifs_info *c)
1e51764a
AB
2587{
2588 struct failure_mode_info *fmi, *tmp;
2589
2590 spin_lock(&fmi_lock);
2591 list_for_each_entry_safe(fmi, tmp, &fmi_list, list)
2592 if (fmi->c == c) {
2593 list_del(&fmi->list);
2594 kfree(fmi);
2595 }
2596 spin_unlock(&fmi_lock);
2597}
2598
2599static struct ubifs_info *dbg_find_info(struct ubi_volume_desc *desc)
2600{
2601 struct failure_mode_info *fmi;
2602
2603 spin_lock(&fmi_lock);
2604 list_for_each_entry(fmi, &fmi_list, list)
2605 if (fmi->c->ubi == desc) {
2606 struct ubifs_info *c = fmi->c;
2607
2608 spin_unlock(&fmi_lock);
2609 return c;
2610 }
2611 spin_unlock(&fmi_lock);
2612 return NULL;
2613}
2614
2615static int in_failure_mode(struct ubi_volume_desc *desc)
2616{
2617 struct ubifs_info *c = dbg_find_info(desc);
2618
2b1844a8 2619 if (c && dbg_is_tst_rcvry(c))
17c2f9f8 2620 return c->dbg->failure_mode;
1e51764a
AB
2621 return 0;
2622}
2623
2624static int do_fail(struct ubi_volume_desc *desc, int lnum, int write)
2625{
2626 struct ubifs_info *c = dbg_find_info(desc);
17c2f9f8 2627 struct ubifs_debug_info *d;
1e51764a 2628
2b1844a8 2629 if (!c || !dbg_is_tst_rcvry(c))
1e51764a 2630 return 0;
17c2f9f8
AB
2631 d = c->dbg;
2632 if (d->failure_mode)
1e51764a 2633 return 1;
17c2f9f8 2634 if (!d->fail_cnt) {
1e51764a
AB
2635 /* First call - decide delay to failure */
2636 if (chance(1, 2)) {
2637 unsigned int delay = 1 << (simple_rand() >> 11);
2638
2639 if (chance(1, 2)) {
17c2f9f8
AB
2640 d->fail_delay = 1;
2641 d->fail_timeout = jiffies +
1e51764a
AB
2642 msecs_to_jiffies(delay);
2643 dbg_rcvry("failing after %ums", delay);
2644 } else {
17c2f9f8
AB
2645 d->fail_delay = 2;
2646 d->fail_cnt_max = delay;
1e51764a
AB
2647 dbg_rcvry("failing after %u calls", delay);
2648 }
2649 }
17c2f9f8 2650 d->fail_cnt += 1;
1e51764a
AB
2651 }
2652 /* Determine if failure delay has expired */
17c2f9f8
AB
2653 if (d->fail_delay == 1) {
2654 if (time_before(jiffies, d->fail_timeout))
1e51764a 2655 return 0;
17c2f9f8
AB
2656 } else if (d->fail_delay == 2)
2657 if (d->fail_cnt++ < d->fail_cnt_max)
1e51764a
AB
2658 return 0;
2659 if (lnum == UBIFS_SB_LNUM) {
2660 if (write) {
2661 if (chance(1, 2))
2662 return 0;
2663 } else if (chance(19, 20))
2664 return 0;
2665 dbg_rcvry("failing in super block LEB %d", lnum);
2666 } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2667 if (chance(19, 20))
2668 return 0;
2669 dbg_rcvry("failing in master LEB %d", lnum);
2670 } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2671 if (write) {
2672 if (chance(99, 100))
2673 return 0;
2674 } else if (chance(399, 400))
2675 return 0;
2676 dbg_rcvry("failing in log LEB %d", lnum);
2677 } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2678 if (write) {
2679 if (chance(7, 8))
2680 return 0;
2681 } else if (chance(19, 20))
2682 return 0;
2683 dbg_rcvry("failing in LPT LEB %d", lnum);
2684 } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2685 if (write) {
2686 if (chance(1, 2))
2687 return 0;
2688 } else if (chance(9, 10))
2689 return 0;
2690 dbg_rcvry("failing in orphan LEB %d", lnum);
2691 } else if (lnum == c->ihead_lnum) {
2692 if (chance(99, 100))
2693 return 0;
2694 dbg_rcvry("failing in index head LEB %d", lnum);
2695 } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2696 if (chance(9, 10))
2697 return 0;
2698 dbg_rcvry("failing in GC head LEB %d", lnum);
2699 } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
2700 !ubifs_search_bud(c, lnum)) {
2701 if (chance(19, 20))
2702 return 0;
2703 dbg_rcvry("failing in non-bud LEB %d", lnum);
2704 } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
2705 c->cmt_state == COMMIT_RUNNING_REQUIRED) {
2706 if (chance(999, 1000))
2707 return 0;
2708 dbg_rcvry("failing in bud LEB %d commit running", lnum);
2709 } else {
2710 if (chance(9999, 10000))
2711 return 0;
2712 dbg_rcvry("failing in bud LEB %d commit not running", lnum);
2713 }
2714 ubifs_err("*** SETTING FAILURE MODE ON (LEB %d) ***", lnum);
17c2f9f8 2715 d->failure_mode = 1;
1e51764a
AB
2716 dump_stack();
2717 return 1;
2718}
2719
2720static void cut_data(const void *buf, int len)
2721{
2722 int flen, i;
2723 unsigned char *p = (void *)buf;
2724
2725 flen = (len * (long long)simple_rand()) >> 15;
2726 for (i = flen; i < len; i++)
2727 p[i] = 0xff;
2728}
2729
2730int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
2731 int len, int check)
2732{
2733 if (in_failure_mode(desc))
1a29af8b 2734 return -EROFS;
1e51764a
AB
2735 return ubi_leb_read(desc, lnum, buf, offset, len, check);
2736}
2737
2738int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
2739 int offset, int len, int dtype)
2740{
16dfd804 2741 int err, failing;
1e51764a
AB
2742
2743 if (in_failure_mode(desc))
1a29af8b 2744 return -EROFS;
16dfd804
AH
2745 failing = do_fail(desc, lnum, 1);
2746 if (failing)
1e51764a
AB
2747 cut_data(buf, len);
2748 err = ubi_leb_write(desc, lnum, buf, offset, len, dtype);
2749 if (err)
2750 return err;
16dfd804 2751 if (failing)
1a29af8b 2752 return -EROFS;
1e51764a
AB
2753 return 0;
2754}
2755
2756int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
2757 int len, int dtype)
2758{
2759 int err;
2760
2761 if (do_fail(desc, lnum, 1))
1a29af8b 2762 return -EROFS;
1e51764a
AB
2763 err = ubi_leb_change(desc, lnum, buf, len, dtype);
2764 if (err)
2765 return err;
2766 if (do_fail(desc, lnum, 1))
1a29af8b 2767 return -EROFS;
1e51764a
AB
2768 return 0;
2769}
2770
2771int dbg_leb_erase(struct ubi_volume_desc *desc, int lnum)
2772{
2773 int err;
2774
2775 if (do_fail(desc, lnum, 0))
1a29af8b 2776 return -EROFS;
1e51764a
AB
2777 err = ubi_leb_erase(desc, lnum);
2778 if (err)
2779 return err;
2780 if (do_fail(desc, lnum, 0))
1a29af8b 2781 return -EROFS;
1e51764a
AB
2782 return 0;
2783}
2784
2785int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum)
2786{
2787 int err;
2788
2789 if (do_fail(desc, lnum, 0))
1a29af8b 2790 return -EROFS;
1e51764a
AB
2791 err = ubi_leb_unmap(desc, lnum);
2792 if (err)
2793 return err;
2794 if (do_fail(desc, lnum, 0))
1a29af8b 2795 return -EROFS;
1e51764a
AB
2796 return 0;
2797}
2798
2799int dbg_is_mapped(struct ubi_volume_desc *desc, int lnum)
2800{
2801 if (in_failure_mode(desc))
1a29af8b 2802 return -EROFS;
1e51764a
AB
2803 return ubi_is_mapped(desc, lnum);
2804}
2805
2806int dbg_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
2807{
2808 int err;
2809
2810 if (do_fail(desc, lnum, 0))
1a29af8b 2811 return -EROFS;
1e51764a
AB
2812 err = ubi_leb_map(desc, lnum, dtype);
2813 if (err)
2814 return err;
2815 if (do_fail(desc, lnum, 0))
1a29af8b 2816 return -EROFS;
1e51764a
AB
2817 return 0;
2818}
2819
17c2f9f8
AB
2820/**
2821 * ubifs_debugging_init - initialize UBIFS debugging.
2822 * @c: UBIFS file-system description object
2823 *
2824 * This function initializes debugging-related data for the file system.
2825 * Returns zero in case of success and a negative error code in case of
2826 * failure.
2827 */
2828int ubifs_debugging_init(struct ubifs_info *c)
2829{
2830 c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
2831 if (!c->dbg)
2832 return -ENOMEM;
2833
17c2f9f8
AB
2834 failure_mode_init(c);
2835 return 0;
17c2f9f8
AB
2836}
2837
2838/**
2839 * ubifs_debugging_exit - free debugging data.
2840 * @c: UBIFS file-system description object
2841 */
2842void ubifs_debugging_exit(struct ubifs_info *c)
2843{
2844 failure_mode_exit(c);
17c2f9f8
AB
2845 kfree(c->dbg);
2846}
2847
552ff317
AB
2848/*
2849 * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
2850 * contain the stuff specific to particular file-system mounts.
2851 */
84abf972 2852static struct dentry *dfs_rootdir;
552ff317
AB
2853
2854/**
2855 * dbg_debugfs_init - initialize debugfs file-system.
2856 *
2857 * UBIFS uses debugfs file-system to expose various debugging knobs to
2858 * user-space. This function creates "ubifs" directory in the debugfs
2859 * file-system. Returns zero in case of success and a negative error code in
2860 * case of failure.
2861 */
2862int dbg_debugfs_init(void)
2863{
84abf972 2864 dfs_rootdir = debugfs_create_dir("ubifs", NULL);
ae380ce0
AB
2865 if (IS_ERR_OR_NULL(dfs_rootdir)) {
2866 int err = dfs_rootdir ? PTR_ERR(dfs_rootdir) : -ENODEV;
552ff317
AB
2867 ubifs_err("cannot create \"ubifs\" debugfs directory, "
2868 "error %d\n", err);
2869 return err;
2870 }
2871
2872 return 0;
2873}
2874
2875/**
2876 * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
2877 */
2878void dbg_debugfs_exit(void)
2879{
84abf972 2880 debugfs_remove(dfs_rootdir);
552ff317
AB
2881}
2882
2883static int open_debugfs_file(struct inode *inode, struct file *file)
2884{
2885 file->private_data = inode->i_private;
1bbfc848 2886 return nonseekable_open(inode, file);
552ff317
AB
2887}
2888
2889static ssize_t write_debugfs_file(struct file *file, const char __user *buf,
2890 size_t count, loff_t *ppos)
2891{
2892 struct ubifs_info *c = file->private_data;
2893 struct ubifs_debug_info *d = c->dbg;
2894
84abf972 2895 if (file->f_path.dentry == d->dfs_dump_lprops)
552ff317 2896 dbg_dump_lprops(c);
8ff83089 2897 else if (file->f_path.dentry == d->dfs_dump_budg)
f1bd66af 2898 dbg_dump_budg(c, &c->bi);
8ff83089 2899 else if (file->f_path.dentry == d->dfs_dump_tnc) {
552ff317
AB
2900 mutex_lock(&c->tnc_mutex);
2901 dbg_dump_tnc(c);
2902 mutex_unlock(&c->tnc_mutex);
2903 } else
2904 return -EINVAL;
2905
552ff317
AB
2906 return count;
2907}
2908
84abf972 2909static const struct file_operations dfs_fops = {
552ff317
AB
2910 .open = open_debugfs_file,
2911 .write = write_debugfs_file,
2912 .owner = THIS_MODULE,
1bbfc848 2913 .llseek = no_llseek,
552ff317
AB
2914};
2915
2916/**
2917 * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
2918 * @c: UBIFS file-system description object
2919 *
2920 * This function creates all debugfs files for this instance of UBIFS. Returns
2921 * zero in case of success and a negative error code in case of failure.
2922 *
2923 * Note, the only reason we have not merged this function with the
2924 * 'ubifs_debugging_init()' function is because it is better to initialize
2925 * debugfs interfaces at the very end of the mount process, and remove them at
2926 * the very beginning of the mount process.
2927 */
2928int dbg_debugfs_init_fs(struct ubifs_info *c)
2929{
ae380ce0 2930 int err, n;
552ff317
AB
2931 const char *fname;
2932 struct dentry *dent;
2933 struct ubifs_debug_info *d = c->dbg;
2934
ae380ce0
AB
2935 n = snprintf(d->dfs_dir_name, UBIFS_DFS_DIR_LEN + 1, UBIFS_DFS_DIR_NAME,
2936 c->vi.ubi_num, c->vi.vol_id);
2937 if (n == UBIFS_DFS_DIR_LEN) {
2938 /* The array size is too small */
2939 fname = UBIFS_DFS_DIR_NAME;
2940 dent = ERR_PTR(-EINVAL);
2941 goto out;
2942 }
2943
cc6a86b9
AB
2944 fname = d->dfs_dir_name;
2945 dent = debugfs_create_dir(fname, dfs_rootdir);
95169535 2946 if (IS_ERR_OR_NULL(dent))
552ff317 2947 goto out;
cc6a86b9 2948 d->dfs_dir = dent;
552ff317
AB
2949
2950 fname = "dump_lprops";
8c559d30 2951 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
95169535 2952 if (IS_ERR_OR_NULL(dent))
552ff317 2953 goto out_remove;
84abf972 2954 d->dfs_dump_lprops = dent;
552ff317
AB
2955
2956 fname = "dump_budg";
8c559d30 2957 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
95169535 2958 if (IS_ERR_OR_NULL(dent))
552ff317 2959 goto out_remove;
84abf972 2960 d->dfs_dump_budg = dent;
552ff317
AB
2961
2962 fname = "dump_tnc";
8c559d30 2963 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
95169535 2964 if (IS_ERR_OR_NULL(dent))
552ff317 2965 goto out_remove;
84abf972 2966 d->dfs_dump_tnc = dent;
552ff317
AB
2967
2968 return 0;
2969
2970out_remove:
cc6a86b9
AB
2971 debugfs_remove_recursive(d->dfs_dir);
2972out:
95169535 2973 err = dent ? PTR_ERR(dent) : -ENODEV;
ae380ce0 2974 ubifs_err("cannot create \"%s\" debugfs filr or directory, error %d\n",
552ff317 2975 fname, err);
552ff317
AB
2976 return err;
2977}
2978
2979/**
2980 * dbg_debugfs_exit_fs - remove all debugfs files.
2981 * @c: UBIFS file-system description object
2982 */
2983void dbg_debugfs_exit_fs(struct ubifs_info *c)
2984{
84abf972 2985 debugfs_remove_recursive(c->dbg->dfs_dir);
552ff317
AB
2986}
2987
1e51764a 2988#endif /* CONFIG_UBIFS_FS_DEBUG */