UBIFS: improve inode dumping function
[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
333 if (dbg_failure_mode)
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
AB
888
889 if (dbg_failure_mode)
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.
1135 * @inode: inode to check
1136 *
1137 * If inode is clean, synchronized inode size has to be equivalent to current
1138 * inode size. This function has to be called only for locked inodes (@i_mutex
1139 * has to be locked). Returns %0 if synchronized inode size if correct, and
1140 * %-EINVAL if not.
1141 */
1142int dbg_check_synced_i_size(struct inode *inode)
1143{
1144 int err = 0;
1145 struct ubifs_inode *ui = ubifs_inode(inode);
1146
1147 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
1148 return 0;
1149 if (!S_ISREG(inode->i_mode))
1150 return 0;
1151
1152 mutex_lock(&ui->ui_mutex);
1153 spin_lock(&ui->ui_lock);
1154 if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
1155 ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode "
1156 "is clean", ui->ui_size, ui->synced_i_size);
1157 ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
1158 inode->i_mode, i_size_read(inode));
1159 dbg_dump_stack();
1160 err = -EINVAL;
1161 }
1162 spin_unlock(&ui->ui_lock);
1163 mutex_unlock(&ui->ui_mutex);
1164 return err;
1165}
1166
1167/*
1168 * dbg_check_dir - check directory inode size and link count.
1169 * @c: UBIFS file-system description object
1170 * @dir: the directory to calculate size for
1171 * @size: the result is returned here
1172 *
1173 * This function makes sure that directory size and link count are correct.
1174 * Returns zero in case of success and a negative error code in case of
1175 * failure.
1176 *
1177 * Note, it is good idea to make sure the @dir->i_mutex is locked before
1178 * calling this function.
1179 */
1180int dbg_check_dir_size(struct ubifs_info *c, const struct inode *dir)
1181{
1182 unsigned int nlink = 2;
1183 union ubifs_key key;
1184 struct ubifs_dent_node *dent, *pdent = NULL;
1185 struct qstr nm = { .name = NULL };
1186 loff_t size = UBIFS_INO_NODE_SZ;
1187
1188 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
1189 return 0;
1190
1191 if (!S_ISDIR(dir->i_mode))
1192 return 0;
1193
1194 lowest_dent_key(c, &key, dir->i_ino);
1195 while (1) {
1196 int err;
1197
1198 dent = ubifs_tnc_next_ent(c, &key, &nm);
1199 if (IS_ERR(dent)) {
1200 err = PTR_ERR(dent);
1201 if (err == -ENOENT)
1202 break;
1203 return err;
1204 }
1205
1206 nm.name = dent->name;
1207 nm.len = le16_to_cpu(dent->nlen);
1208 size += CALC_DENT_SIZE(nm.len);
1209 if (dent->type == UBIFS_ITYPE_DIR)
1210 nlink += 1;
1211 kfree(pdent);
1212 pdent = dent;
1213 key_read(c, &dent->key, &key);
1214 }
1215 kfree(pdent);
1216
1217 if (i_size_read(dir) != size) {
1218 ubifs_err("directory inode %lu has size %llu, "
1219 "but calculated size is %llu", dir->i_ino,
1220 (unsigned long long)i_size_read(dir),
1221 (unsigned long long)size);
4315fb40 1222 dbg_dump_inode(c, dir);
1e51764a
AB
1223 dump_stack();
1224 return -EINVAL;
1225 }
1226 if (dir->i_nlink != nlink) {
1227 ubifs_err("directory inode %lu has nlink %u, but calculated "
1228 "nlink is %u", dir->i_ino, dir->i_nlink, nlink);
4315fb40 1229 dbg_dump_inode(c, dir);
1e51764a
AB
1230 dump_stack();
1231 return -EINVAL;
1232 }
1233
1234 return 0;
1235}
1236
1237/**
1238 * dbg_check_key_order - make sure that colliding keys are properly ordered.
1239 * @c: UBIFS file-system description object
1240 * @zbr1: first zbranch
1241 * @zbr2: following zbranch
1242 *
1243 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
1244 * names of the direntries/xentries which are referred by the keys. This
1245 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
1246 * sure the name of direntry/xentry referred by @zbr1 is less than
1247 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
1248 * and a negative error code in case of failure.
1249 */
1250static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
1251 struct ubifs_zbranch *zbr2)
1252{
1253 int err, nlen1, nlen2, cmp;
1254 struct ubifs_dent_node *dent1, *dent2;
1255 union ubifs_key key;
1256
1257 ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
1258 dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1259 if (!dent1)
1260 return -ENOMEM;
1261 dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1262 if (!dent2) {
1263 err = -ENOMEM;
1264 goto out_free;
1265 }
1266
1267 err = ubifs_tnc_read_node(c, zbr1, dent1);
1268 if (err)
1269 goto out_free;
1270 err = ubifs_validate_entry(c, dent1);
1271 if (err)
1272 goto out_free;
1273
1274 err = ubifs_tnc_read_node(c, zbr2, dent2);
1275 if (err)
1276 goto out_free;
1277 err = ubifs_validate_entry(c, dent2);
1278 if (err)
1279 goto out_free;
1280
1281 /* Make sure node keys are the same as in zbranch */
1282 err = 1;
1283 key_read(c, &dent1->key, &key);
1284 if (keys_cmp(c, &zbr1->key, &key)) {
5d38b3ac
AB
1285 dbg_err("1st entry at %d:%d has key %s", zbr1->lnum,
1286 zbr1->offs, DBGKEY(&key));
1287 dbg_err("but it should have key %s according to tnc",
1288 DBGKEY(&zbr1->key));
2ba5f7ae 1289 dbg_dump_node(c, dent1);
552ff317 1290 goto out_free;
1e51764a
AB
1291 }
1292
1293 key_read(c, &dent2->key, &key);
1294 if (keys_cmp(c, &zbr2->key, &key)) {
5d38b3ac
AB
1295 dbg_err("2nd entry at %d:%d has key %s", zbr1->lnum,
1296 zbr1->offs, DBGKEY(&key));
1297 dbg_err("but it should have key %s according to tnc",
1298 DBGKEY(&zbr2->key));
2ba5f7ae 1299 dbg_dump_node(c, dent2);
552ff317 1300 goto out_free;
1e51764a
AB
1301 }
1302
1303 nlen1 = le16_to_cpu(dent1->nlen);
1304 nlen2 = le16_to_cpu(dent2->nlen);
1305
1306 cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
1307 if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
1308 err = 0;
1309 goto out_free;
1310 }
1311 if (cmp == 0 && nlen1 == nlen2)
5d38b3ac 1312 dbg_err("2 xent/dent nodes with the same name");
1e51764a 1313 else
5d38b3ac 1314 dbg_err("bad order of colliding key %s",
1e51764a
AB
1315 DBGKEY(&key));
1316
552ff317 1317 ubifs_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs);
1e51764a 1318 dbg_dump_node(c, dent1);
552ff317 1319 ubifs_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs);
1e51764a
AB
1320 dbg_dump_node(c, dent2);
1321
1322out_free:
1323 kfree(dent2);
1324 kfree(dent1);
1325 return err;
1326}
1327
1328/**
1329 * dbg_check_znode - check if znode is all right.
1330 * @c: UBIFS file-system description object
1331 * @zbr: zbranch which points to this znode
1332 *
1333 * This function makes sure that znode referred to by @zbr is all right.
1334 * Returns zero if it is, and %-EINVAL if it is not.
1335 */
1336static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
1337{
1338 struct ubifs_znode *znode = zbr->znode;
1339 struct ubifs_znode *zp = znode->parent;
1340 int n, err, cmp;
1341
1342 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
1343 err = 1;
1344 goto out;
1345 }
1346 if (znode->level < 0) {
1347 err = 2;
1348 goto out;
1349 }
1350 if (znode->iip < 0 || znode->iip >= c->fanout) {
1351 err = 3;
1352 goto out;
1353 }
1354
1355 if (zbr->len == 0)
1356 /* Only dirty zbranch may have no on-flash nodes */
1357 if (!ubifs_zn_dirty(znode)) {
1358 err = 4;
1359 goto out;
1360 }
1361
1362 if (ubifs_zn_dirty(znode)) {
1363 /*
1364 * If znode is dirty, its parent has to be dirty as well. The
1365 * order of the operation is important, so we have to have
1366 * memory barriers.
1367 */
1368 smp_mb();
1369 if (zp && !ubifs_zn_dirty(zp)) {
1370 /*
1371 * The dirty flag is atomic and is cleared outside the
1372 * TNC mutex, so znode's dirty flag may now have
1373 * been cleared. The child is always cleared before the
1374 * parent, so we just need to check again.
1375 */
1376 smp_mb();
1377 if (ubifs_zn_dirty(znode)) {
1378 err = 5;
1379 goto out;
1380 }
1381 }
1382 }
1383
1384 if (zp) {
1385 const union ubifs_key *min, *max;
1386
1387 if (znode->level != zp->level - 1) {
1388 err = 6;
1389 goto out;
1390 }
1391
1392 /* Make sure the 'parent' pointer in our znode is correct */
1393 err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
1394 if (!err) {
1395 /* This zbranch does not exist in the parent */
1396 err = 7;
1397 goto out;
1398 }
1399
1400 if (znode->iip >= zp->child_cnt) {
1401 err = 8;
1402 goto out;
1403 }
1404
1405 if (znode->iip != n) {
1406 /* This may happen only in case of collisions */
1407 if (keys_cmp(c, &zp->zbranch[n].key,
1408 &zp->zbranch[znode->iip].key)) {
1409 err = 9;
1410 goto out;
1411 }
1412 n = znode->iip;
1413 }
1414
1415 /*
1416 * Make sure that the first key in our znode is greater than or
1417 * equal to the key in the pointing zbranch.
1418 */
1419 min = &zbr->key;
1420 cmp = keys_cmp(c, min, &znode->zbranch[0].key);
1421 if (cmp == 1) {
1422 err = 10;
1423 goto out;
1424 }
1425
1426 if (n + 1 < zp->child_cnt) {
1427 max = &zp->zbranch[n + 1].key;
1428
1429 /*
1430 * Make sure the last key in our znode is less or
7d4e9ccb 1431 * equivalent than the key in the zbranch which goes
1e51764a
AB
1432 * after our pointing zbranch.
1433 */
1434 cmp = keys_cmp(c, max,
1435 &znode->zbranch[znode->child_cnt - 1].key);
1436 if (cmp == -1) {
1437 err = 11;
1438 goto out;
1439 }
1440 }
1441 } else {
1442 /* This may only be root znode */
1443 if (zbr != &c->zroot) {
1444 err = 12;
1445 goto out;
1446 }
1447 }
1448
1449 /*
1450 * Make sure that next key is greater or equivalent then the previous
1451 * one.
1452 */
1453 for (n = 1; n < znode->child_cnt; n++) {
1454 cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
1455 &znode->zbranch[n].key);
1456 if (cmp > 0) {
1457 err = 13;
1458 goto out;
1459 }
1460 if (cmp == 0) {
1461 /* This can only be keys with colliding hash */
1462 if (!is_hash_key(c, &znode->zbranch[n].key)) {
1463 err = 14;
1464 goto out;
1465 }
1466
1467 if (znode->level != 0 || c->replaying)
1468 continue;
1469
1470 /*
1471 * Colliding keys should follow binary order of
1472 * corresponding xentry/dentry names.
1473 */
1474 err = dbg_check_key_order(c, &znode->zbranch[n - 1],
1475 &znode->zbranch[n]);
1476 if (err < 0)
1477 return err;
1478 if (err) {
1479 err = 15;
1480 goto out;
1481 }
1482 }
1483 }
1484
1485 for (n = 0; n < znode->child_cnt; n++) {
1486 if (!znode->zbranch[n].znode &&
1487 (znode->zbranch[n].lnum == 0 ||
1488 znode->zbranch[n].len == 0)) {
1489 err = 16;
1490 goto out;
1491 }
1492
1493 if (znode->zbranch[n].lnum != 0 &&
1494 znode->zbranch[n].len == 0) {
1495 err = 17;
1496 goto out;
1497 }
1498
1499 if (znode->zbranch[n].lnum == 0 &&
1500 znode->zbranch[n].len != 0) {
1501 err = 18;
1502 goto out;
1503 }
1504
1505 if (znode->zbranch[n].lnum == 0 &&
1506 znode->zbranch[n].offs != 0) {
1507 err = 19;
1508 goto out;
1509 }
1510
1511 if (znode->level != 0 && znode->zbranch[n].znode)
1512 if (znode->zbranch[n].znode->parent != znode) {
1513 err = 20;
1514 goto out;
1515 }
1516 }
1517
1518 return 0;
1519
1520out:
1521 ubifs_err("failed, error %d", err);
1522 ubifs_msg("dump of the znode");
1523 dbg_dump_znode(c, znode);
1524 if (zp) {
1525 ubifs_msg("dump of the parent znode");
1526 dbg_dump_znode(c, zp);
1527 }
1528 dump_stack();
1529 return -EINVAL;
1530}
1531
1532/**
1533 * dbg_check_tnc - check TNC tree.
1534 * @c: UBIFS file-system description object
1535 * @extra: do extra checks that are possible at start commit
1536 *
1537 * This function traverses whole TNC tree and checks every znode. Returns zero
1538 * if everything is all right and %-EINVAL if something is wrong with TNC.
1539 */
1540int dbg_check_tnc(struct ubifs_info *c, int extra)
1541{
1542 struct ubifs_znode *znode;
1543 long clean_cnt = 0, dirty_cnt = 0;
1544 int err, last;
1545
1546 if (!(ubifs_chk_flags & UBIFS_CHK_TNC))
1547 return 0;
1548
1549 ubifs_assert(mutex_is_locked(&c->tnc_mutex));
1550 if (!c->zroot.znode)
1551 return 0;
1552
1553 znode = ubifs_tnc_postorder_first(c->zroot.znode);
1554 while (1) {
1555 struct ubifs_znode *prev;
1556 struct ubifs_zbranch *zbr;
1557
1558 if (!znode->parent)
1559 zbr = &c->zroot;
1560 else
1561 zbr = &znode->parent->zbranch[znode->iip];
1562
1563 err = dbg_check_znode(c, zbr);
1564 if (err)
1565 return err;
1566
1567 if (extra) {
1568 if (ubifs_zn_dirty(znode))
1569 dirty_cnt += 1;
1570 else
1571 clean_cnt += 1;
1572 }
1573
1574 prev = znode;
1575 znode = ubifs_tnc_postorder_next(znode);
1576 if (!znode)
1577 break;
1578
1579 /*
1580 * If the last key of this znode is equivalent to the first key
1581 * of the next znode (collision), then check order of the keys.
1582 */
1583 last = prev->child_cnt - 1;
1584 if (prev->level == 0 && znode->level == 0 && !c->replaying &&
1585 !keys_cmp(c, &prev->zbranch[last].key,
1586 &znode->zbranch[0].key)) {
1587 err = dbg_check_key_order(c, &prev->zbranch[last],
1588 &znode->zbranch[0]);
1589 if (err < 0)
1590 return err;
1591 if (err) {
1592 ubifs_msg("first znode");
1593 dbg_dump_znode(c, prev);
1594 ubifs_msg("second znode");
1595 dbg_dump_znode(c, znode);
1596 return -EINVAL;
1597 }
1598 }
1599 }
1600
1601 if (extra) {
1602 if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
1603 ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld",
1604 atomic_long_read(&c->clean_zn_cnt),
1605 clean_cnt);
1606 return -EINVAL;
1607 }
1608 if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
1609 ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld",
1610 atomic_long_read(&c->dirty_zn_cnt),
1611 dirty_cnt);
1612 return -EINVAL;
1613 }
1614 }
1615
1616 return 0;
1617}
1618
1619/**
1620 * dbg_walk_index - walk the on-flash index.
1621 * @c: UBIFS file-system description object
1622 * @leaf_cb: called for each leaf node
1623 * @znode_cb: called for each indexing node
227c75c9 1624 * @priv: private data which is passed to callbacks
1e51764a
AB
1625 *
1626 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1627 * node and @znode_cb for each indexing node. Returns zero in case of success
1628 * and a negative error code in case of failure.
1629 *
1630 * It would be better if this function removed every znode it pulled to into
1631 * the TNC, so that the behavior more closely matched the non-debugging
1632 * behavior.
1633 */
1634int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1635 dbg_znode_callback znode_cb, void *priv)
1636{
1637 int err;
1638 struct ubifs_zbranch *zbr;
1639 struct ubifs_znode *znode, *child;
1640
1641 mutex_lock(&c->tnc_mutex);
1642 /* If the root indexing node is not in TNC - pull it */
1643 if (!c->zroot.znode) {
1644 c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1645 if (IS_ERR(c->zroot.znode)) {
1646 err = PTR_ERR(c->zroot.znode);
1647 c->zroot.znode = NULL;
1648 goto out_unlock;
1649 }
1650 }
1651
1652 /*
1653 * We are going to traverse the indexing tree in the postorder manner.
1654 * Go down and find the leftmost indexing node where we are going to
1655 * start from.
1656 */
1657 znode = c->zroot.znode;
1658 while (znode->level > 0) {
1659 zbr = &znode->zbranch[0];
1660 child = zbr->znode;
1661 if (!child) {
1662 child = ubifs_load_znode(c, zbr, znode, 0);
1663 if (IS_ERR(child)) {
1664 err = PTR_ERR(child);
1665 goto out_unlock;
1666 }
1667 zbr->znode = child;
1668 }
1669
1670 znode = child;
1671 }
1672
1673 /* Iterate over all indexing nodes */
1674 while (1) {
1675 int idx;
1676
1677 cond_resched();
1678
1679 if (znode_cb) {
1680 err = znode_cb(c, znode, priv);
1681 if (err) {
1682 ubifs_err("znode checking function returned "
1683 "error %d", err);
1684 dbg_dump_znode(c, znode);
1685 goto out_dump;
1686 }
1687 }
1688 if (leaf_cb && znode->level == 0) {
1689 for (idx = 0; idx < znode->child_cnt; idx++) {
1690 zbr = &znode->zbranch[idx];
1691 err = leaf_cb(c, zbr, priv);
1692 if (err) {
1693 ubifs_err("leaf checking function "
1694 "returned error %d, for leaf "
1695 "at LEB %d:%d",
1696 err, zbr->lnum, zbr->offs);
1697 goto out_dump;
1698 }
1699 }
1700 }
1701
1702 if (!znode->parent)
1703 break;
1704
1705 idx = znode->iip + 1;
1706 znode = znode->parent;
1707 if (idx < znode->child_cnt) {
1708 /* Switch to the next index in the parent */
1709 zbr = &znode->zbranch[idx];
1710 child = zbr->znode;
1711 if (!child) {
1712 child = ubifs_load_znode(c, zbr, znode, idx);
1713 if (IS_ERR(child)) {
1714 err = PTR_ERR(child);
1715 goto out_unlock;
1716 }
1717 zbr->znode = child;
1718 }
1719 znode = child;
1720 } else
1721 /*
1722 * This is the last child, switch to the parent and
1723 * continue.
1724 */
1725 continue;
1726
1727 /* Go to the lowest leftmost znode in the new sub-tree */
1728 while (znode->level > 0) {
1729 zbr = &znode->zbranch[0];
1730 child = zbr->znode;
1731 if (!child) {
1732 child = ubifs_load_znode(c, zbr, znode, 0);
1733 if (IS_ERR(child)) {
1734 err = PTR_ERR(child);
1735 goto out_unlock;
1736 }
1737 zbr->znode = child;
1738 }
1739 znode = child;
1740 }
1741 }
1742
1743 mutex_unlock(&c->tnc_mutex);
1744 return 0;
1745
1746out_dump:
1747 if (znode->parent)
1748 zbr = &znode->parent->zbranch[znode->iip];
1749 else
1750 zbr = &c->zroot;
1751 ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1752 dbg_dump_znode(c, znode);
1753out_unlock:
1754 mutex_unlock(&c->tnc_mutex);
1755 return err;
1756}
1757
1758/**
1759 * add_size - add znode size to partially calculated index size.
1760 * @c: UBIFS file-system description object
1761 * @znode: znode to add size for
1762 * @priv: partially calculated index size
1763 *
1764 * This is a helper function for 'dbg_check_idx_size()' which is called for
1765 * every indexing node and adds its size to the 'long long' variable pointed to
1766 * by @priv.
1767 */
1768static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1769{
1770 long long *idx_size = priv;
1771 int add;
1772
1773 add = ubifs_idx_node_sz(c, znode->child_cnt);
1774 add = ALIGN(add, 8);
1775 *idx_size += add;
1776 return 0;
1777}
1778
1779/**
1780 * dbg_check_idx_size - check index size.
1781 * @c: UBIFS file-system description object
1782 * @idx_size: size to check
1783 *
1784 * This function walks the UBIFS index, calculates its size and checks that the
1785 * size is equivalent to @idx_size. Returns zero in case of success and a
1786 * negative error code in case of failure.
1787 */
1788int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
1789{
1790 int err;
1791 long long calc = 0;
1792
1793 if (!(ubifs_chk_flags & UBIFS_CHK_IDX_SZ))
1794 return 0;
1795
1796 err = dbg_walk_index(c, NULL, add_size, &calc);
1797 if (err) {
1798 ubifs_err("error %d while walking the index", err);
1799 return err;
1800 }
1801
1802 if (calc != idx_size) {
1803 ubifs_err("index size check failed: calculated size is %lld, "
1804 "should be %lld", calc, idx_size);
1805 dump_stack();
1806 return -EINVAL;
1807 }
1808
1809 return 0;
1810}
1811
1812/**
1813 * struct fsck_inode - information about an inode used when checking the file-system.
1814 * @rb: link in the RB-tree of inodes
1815 * @inum: inode number
1816 * @mode: inode type, permissions, etc
1817 * @nlink: inode link count
1818 * @xattr_cnt: count of extended attributes
1819 * @references: how many directory/xattr entries refer this inode (calculated
1820 * while walking the index)
1821 * @calc_cnt: for directory inode count of child directories
1822 * @size: inode size (read from on-flash inode)
1823 * @xattr_sz: summary size of all extended attributes (read from on-flash
1824 * inode)
1825 * @calc_sz: for directories calculated directory size
1826 * @calc_xcnt: count of extended attributes
1827 * @calc_xsz: calculated summary size of all extended attributes
1828 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1829 * inode (read from on-flash inode)
1830 * @calc_xnms: calculated sum of lengths of all extended attribute names
1831 */
1832struct fsck_inode {
1833 struct rb_node rb;
1834 ino_t inum;
1835 umode_t mode;
1836 unsigned int nlink;
1837 unsigned int xattr_cnt;
1838 int references;
1839 int calc_cnt;
1840 long long size;
1841 unsigned int xattr_sz;
1842 long long calc_sz;
1843 long long calc_xcnt;
1844 long long calc_xsz;
1845 unsigned int xattr_nms;
1846 long long calc_xnms;
1847};
1848
1849/**
1850 * struct fsck_data - private FS checking information.
1851 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1852 */
1853struct fsck_data {
1854 struct rb_root inodes;
1855};
1856
1857/**
1858 * add_inode - add inode information to RB-tree of inodes.
1859 * @c: UBIFS file-system description object
1860 * @fsckd: FS checking information
1861 * @ino: raw UBIFS inode to add
1862 *
1863 * This is a helper function for 'check_leaf()' which adds information about
1864 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1865 * case of success and a negative error code in case of failure.
1866 */
1867static struct fsck_inode *add_inode(struct ubifs_info *c,
1868 struct fsck_data *fsckd,
1869 struct ubifs_ino_node *ino)
1870{
1871 struct rb_node **p, *parent = NULL;
1872 struct fsck_inode *fscki;
1873 ino_t inum = key_inum_flash(c, &ino->key);
45cd5cdd
AB
1874 struct inode *inode;
1875 struct ubifs_inode *ui;
1e51764a
AB
1876
1877 p = &fsckd->inodes.rb_node;
1878 while (*p) {
1879 parent = *p;
1880 fscki = rb_entry(parent, struct fsck_inode, rb);
1881 if (inum < fscki->inum)
1882 p = &(*p)->rb_left;
1883 else if (inum > fscki->inum)
1884 p = &(*p)->rb_right;
1885 else
1886 return fscki;
1887 }
1888
1889 if (inum > c->highest_inum) {
1890 ubifs_err("too high inode number, max. is %lu",
e84461ad 1891 (unsigned long)c->highest_inum);
1e51764a
AB
1892 return ERR_PTR(-EINVAL);
1893 }
1894
1895 fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
1896 if (!fscki)
1897 return ERR_PTR(-ENOMEM);
1898
45cd5cdd
AB
1899 inode = ilookup(c->vfs_sb, inum);
1900
1e51764a 1901 fscki->inum = inum;
45cd5cdd
AB
1902 /*
1903 * If the inode is present in the VFS inode cache, use it instead of
1904 * the on-flash inode which might be out-of-date. E.g., the size might
1905 * be out-of-date. If we do not do this, the following may happen, for
1906 * example:
1907 * 1. A power cut happens
1908 * 2. We mount the file-system R/O, the replay process fixes up the
1909 * inode size in the VFS cache, but on on-flash.
1910 * 3. 'check_leaf()' fails because it hits a data node beyond inode
1911 * size.
1912 */
1913 if (!inode) {
1914 fscki->nlink = le32_to_cpu(ino->nlink);
1915 fscki->size = le64_to_cpu(ino->size);
1916 fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
1917 fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
1918 fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
1919 fscki->mode = le32_to_cpu(ino->mode);
1920 } else {
1921 ui = ubifs_inode(inode);
1922 fscki->nlink = inode->i_nlink;
1923 fscki->size = inode->i_size;
1924 fscki->xattr_cnt = ui->xattr_cnt;
1925 fscki->xattr_sz = ui->xattr_size;
1926 fscki->xattr_nms = ui->xattr_names;
1927 fscki->mode = inode->i_mode;
1928 iput(inode);
1929 }
1930
1e51764a
AB
1931 if (S_ISDIR(fscki->mode)) {
1932 fscki->calc_sz = UBIFS_INO_NODE_SZ;
1933 fscki->calc_cnt = 2;
1934 }
45cd5cdd 1935
1e51764a
AB
1936 rb_link_node(&fscki->rb, parent, p);
1937 rb_insert_color(&fscki->rb, &fsckd->inodes);
45cd5cdd 1938
1e51764a
AB
1939 return fscki;
1940}
1941
1942/**
1943 * search_inode - search inode in the RB-tree of inodes.
1944 * @fsckd: FS checking information
1945 * @inum: inode number to search
1946 *
1947 * This is a helper function for 'check_leaf()' which searches inode @inum in
1948 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1949 * the inode was not found.
1950 */
1951static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
1952{
1953 struct rb_node *p;
1954 struct fsck_inode *fscki;
1955
1956 p = fsckd->inodes.rb_node;
1957 while (p) {
1958 fscki = rb_entry(p, struct fsck_inode, rb);
1959 if (inum < fscki->inum)
1960 p = p->rb_left;
1961 else if (inum > fscki->inum)
1962 p = p->rb_right;
1963 else
1964 return fscki;
1965 }
1966 return NULL;
1967}
1968
1969/**
1970 * read_add_inode - read inode node and add it to RB-tree of inodes.
1971 * @c: UBIFS file-system description object
1972 * @fsckd: FS checking information
1973 * @inum: inode number to read
1974 *
1975 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1976 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1977 * information pointer in case of success and a negative error code in case of
1978 * failure.
1979 */
1980static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1981 struct fsck_data *fsckd, ino_t inum)
1982{
1983 int n, err;
1984 union ubifs_key key;
1985 struct ubifs_znode *znode;
1986 struct ubifs_zbranch *zbr;
1987 struct ubifs_ino_node *ino;
1988 struct fsck_inode *fscki;
1989
1990 fscki = search_inode(fsckd, inum);
1991 if (fscki)
1992 return fscki;
1993
1994 ino_key_init(c, &key, inum);
1995 err = ubifs_lookup_level0(c, &key, &znode, &n);
1996 if (!err) {
e84461ad 1997 ubifs_err("inode %lu not found in index", (unsigned long)inum);
1e51764a
AB
1998 return ERR_PTR(-ENOENT);
1999 } else if (err < 0) {
e84461ad
AB
2000 ubifs_err("error %d while looking up inode %lu",
2001 err, (unsigned long)inum);
1e51764a
AB
2002 return ERR_PTR(err);
2003 }
2004
2005 zbr = &znode->zbranch[n];
2006 if (zbr->len < UBIFS_INO_NODE_SZ) {
e84461ad
AB
2007 ubifs_err("bad node %lu node length %d",
2008 (unsigned long)inum, zbr->len);
1e51764a
AB
2009 return ERR_PTR(-EINVAL);
2010 }
2011
2012 ino = kmalloc(zbr->len, GFP_NOFS);
2013 if (!ino)
2014 return ERR_PTR(-ENOMEM);
2015
2016 err = ubifs_tnc_read_node(c, zbr, ino);
2017 if (err) {
2018 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2019 zbr->lnum, zbr->offs, err);
2020 kfree(ino);
2021 return ERR_PTR(err);
2022 }
2023
2024 fscki = add_inode(c, fsckd, ino);
2025 kfree(ino);
2026 if (IS_ERR(fscki)) {
2027 ubifs_err("error %ld while adding inode %lu node",
e84461ad 2028 PTR_ERR(fscki), (unsigned long)inum);
1e51764a
AB
2029 return fscki;
2030 }
2031
2032 return fscki;
2033}
2034
2035/**
2036 * check_leaf - check leaf node.
2037 * @c: UBIFS file-system description object
2038 * @zbr: zbranch of the leaf node to check
2039 * @priv: FS checking information
2040 *
2041 * This is a helper function for 'dbg_check_filesystem()' which is called for
2042 * every single leaf node while walking the indexing tree. It checks that the
2043 * leaf node referred from the indexing tree exists, has correct CRC, and does
2044 * some other basic validation. This function is also responsible for building
2045 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
2046 * calculates reference count, size, etc for each inode in order to later
2047 * compare them to the information stored inside the inodes and detect possible
2048 * inconsistencies. Returns zero in case of success and a negative error code
2049 * in case of failure.
2050 */
2051static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
2052 void *priv)
2053{
2054 ino_t inum;
2055 void *node;
2056 struct ubifs_ch *ch;
2057 int err, type = key_type(c, &zbr->key);
2058 struct fsck_inode *fscki;
2059
2060 if (zbr->len < UBIFS_CH_SZ) {
2061 ubifs_err("bad leaf length %d (LEB %d:%d)",
2062 zbr->len, zbr->lnum, zbr->offs);
2063 return -EINVAL;
2064 }
2065
2066 node = kmalloc(zbr->len, GFP_NOFS);
2067 if (!node)
2068 return -ENOMEM;
2069
2070 err = ubifs_tnc_read_node(c, zbr, node);
2071 if (err) {
2072 ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
2073 zbr->lnum, zbr->offs, err);
2074 goto out_free;
2075 }
2076
2077 /* If this is an inode node, add it to RB-tree of inodes */
2078 if (type == UBIFS_INO_KEY) {
2079 fscki = add_inode(c, priv, node);
2080 if (IS_ERR(fscki)) {
2081 err = PTR_ERR(fscki);
2082 ubifs_err("error %d while adding inode node", err);
2083 goto out_dump;
2084 }
2085 goto out;
2086 }
2087
2088 if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
2089 type != UBIFS_DATA_KEY) {
2090 ubifs_err("unexpected node type %d at LEB %d:%d",
2091 type, zbr->lnum, zbr->offs);
2092 err = -EINVAL;
2093 goto out_free;
2094 }
2095
2096 ch = node;
2097 if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
2098 ubifs_err("too high sequence number, max. is %llu",
2099 c->max_sqnum);
2100 err = -EINVAL;
2101 goto out_dump;
2102 }
2103
2104 if (type == UBIFS_DATA_KEY) {
2105 long long blk_offs;
2106 struct ubifs_data_node *dn = node;
2107
2108 /*
2109 * Search the inode node this data node belongs to and insert
2110 * it to the RB-tree of inodes.
2111 */
2112 inum = key_inum_flash(c, &dn->key);
2113 fscki = read_add_inode(c, priv, inum);
2114 if (IS_ERR(fscki)) {
2115 err = PTR_ERR(fscki);
2116 ubifs_err("error %d while processing data node and "
e84461ad
AB
2117 "trying to find inode node %lu",
2118 err, (unsigned long)inum);
1e51764a
AB
2119 goto out_dump;
2120 }
2121
2122 /* Make sure the data node is within inode size */
2123 blk_offs = key_block_flash(c, &dn->key);
2124 blk_offs <<= UBIFS_BLOCK_SHIFT;
2125 blk_offs += le32_to_cpu(dn->size);
2126 if (blk_offs > fscki->size) {
2127 ubifs_err("data node at LEB %d:%d is not within inode "
2128 "size %lld", zbr->lnum, zbr->offs,
2129 fscki->size);
2130 err = -EINVAL;
2131 goto out_dump;
2132 }
2133 } else {
2134 int nlen;
2135 struct ubifs_dent_node *dent = node;
2136 struct fsck_inode *fscki1;
2137
2138 err = ubifs_validate_entry(c, dent);
2139 if (err)
2140 goto out_dump;
2141
2142 /*
2143 * Search the inode node this entry refers to and the parent
2144 * inode node and insert them to the RB-tree of inodes.
2145 */
2146 inum = le64_to_cpu(dent->inum);
2147 fscki = read_add_inode(c, priv, inum);
2148 if (IS_ERR(fscki)) {
2149 err = PTR_ERR(fscki);
2150 ubifs_err("error %d while processing entry node and "
e84461ad
AB
2151 "trying to find inode node %lu",
2152 err, (unsigned long)inum);
1e51764a
AB
2153 goto out_dump;
2154 }
2155
2156 /* Count how many direntries or xentries refers this inode */
2157 fscki->references += 1;
2158
2159 inum = key_inum_flash(c, &dent->key);
2160 fscki1 = read_add_inode(c, priv, inum);
2161 if (IS_ERR(fscki1)) {
b38882f5 2162 err = PTR_ERR(fscki1);
1e51764a
AB
2163 ubifs_err("error %d while processing entry node and "
2164 "trying to find parent inode node %lu",
e84461ad 2165 err, (unsigned long)inum);
1e51764a
AB
2166 goto out_dump;
2167 }
2168
2169 nlen = le16_to_cpu(dent->nlen);
2170 if (type == UBIFS_XENT_KEY) {
2171 fscki1->calc_xcnt += 1;
2172 fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
2173 fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
2174 fscki1->calc_xnms += nlen;
2175 } else {
2176 fscki1->calc_sz += CALC_DENT_SIZE(nlen);
2177 if (dent->type == UBIFS_ITYPE_DIR)
2178 fscki1->calc_cnt += 1;
2179 }
2180 }
2181
2182out:
2183 kfree(node);
2184 return 0;
2185
2186out_dump:
2187 ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
2188 dbg_dump_node(c, node);
2189out_free:
2190 kfree(node);
2191 return err;
2192}
2193
2194/**
2195 * free_inodes - free RB-tree of inodes.
2196 * @fsckd: FS checking information
2197 */
2198static void free_inodes(struct fsck_data *fsckd)
2199{
2200 struct rb_node *this = fsckd->inodes.rb_node;
2201 struct fsck_inode *fscki;
2202
2203 while (this) {
2204 if (this->rb_left)
2205 this = this->rb_left;
2206 else if (this->rb_right)
2207 this = this->rb_right;
2208 else {
2209 fscki = rb_entry(this, struct fsck_inode, rb);
2210 this = rb_parent(this);
2211 if (this) {
2212 if (this->rb_left == &fscki->rb)
2213 this->rb_left = NULL;
2214 else
2215 this->rb_right = NULL;
2216 }
2217 kfree(fscki);
2218 }
2219 }
2220}
2221
2222/**
2223 * check_inodes - checks all inodes.
2224 * @c: UBIFS file-system description object
2225 * @fsckd: FS checking information
2226 *
2227 * This is a helper function for 'dbg_check_filesystem()' which walks the
2228 * RB-tree of inodes after the index scan has been finished, and checks that
2229 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
2230 * %-EINVAL if not, and a negative error code in case of failure.
2231 */
2232static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
2233{
2234 int n, err;
2235 union ubifs_key key;
2236 struct ubifs_znode *znode;
2237 struct ubifs_zbranch *zbr;
2238 struct ubifs_ino_node *ino;
2239 struct fsck_inode *fscki;
2240 struct rb_node *this = rb_first(&fsckd->inodes);
2241
2242 while (this) {
2243 fscki = rb_entry(this, struct fsck_inode, rb);
2244 this = rb_next(this);
2245
2246 if (S_ISDIR(fscki->mode)) {
2247 /*
2248 * Directories have to have exactly one reference (they
2249 * cannot have hardlinks), although root inode is an
2250 * exception.
2251 */
2252 if (fscki->inum != UBIFS_ROOT_INO &&
2253 fscki->references != 1) {
2254 ubifs_err("directory inode %lu has %d "
2255 "direntries which refer it, but "
e84461ad
AB
2256 "should be 1",
2257 (unsigned long)fscki->inum,
1e51764a
AB
2258 fscki->references);
2259 goto out_dump;
2260 }
2261 if (fscki->inum == UBIFS_ROOT_INO &&
2262 fscki->references != 0) {
2263 ubifs_err("root inode %lu has non-zero (%d) "
2264 "direntries which refer it",
e84461ad
AB
2265 (unsigned long)fscki->inum,
2266 fscki->references);
1e51764a
AB
2267 goto out_dump;
2268 }
2269 if (fscki->calc_sz != fscki->size) {
2270 ubifs_err("directory inode %lu size is %lld, "
2271 "but calculated size is %lld",
e84461ad
AB
2272 (unsigned long)fscki->inum,
2273 fscki->size, fscki->calc_sz);
1e51764a
AB
2274 goto out_dump;
2275 }
2276 if (fscki->calc_cnt != fscki->nlink) {
2277 ubifs_err("directory inode %lu nlink is %d, "
2278 "but calculated nlink is %d",
e84461ad
AB
2279 (unsigned long)fscki->inum,
2280 fscki->nlink, fscki->calc_cnt);
1e51764a
AB
2281 goto out_dump;
2282 }
2283 } else {
2284 if (fscki->references != fscki->nlink) {
2285 ubifs_err("inode %lu nlink is %d, but "
e84461ad
AB
2286 "calculated nlink is %d",
2287 (unsigned long)fscki->inum,
1e51764a
AB
2288 fscki->nlink, fscki->references);
2289 goto out_dump;
2290 }
2291 }
2292 if (fscki->xattr_sz != fscki->calc_xsz) {
2293 ubifs_err("inode %lu has xattr size %u, but "
2294 "calculated size is %lld",
e84461ad 2295 (unsigned long)fscki->inum, fscki->xattr_sz,
1e51764a
AB
2296 fscki->calc_xsz);
2297 goto out_dump;
2298 }
2299 if (fscki->xattr_cnt != fscki->calc_xcnt) {
2300 ubifs_err("inode %lu has %u xattrs, but "
e84461ad
AB
2301 "calculated count is %lld",
2302 (unsigned long)fscki->inum,
1e51764a
AB
2303 fscki->xattr_cnt, fscki->calc_xcnt);
2304 goto out_dump;
2305 }
2306 if (fscki->xattr_nms != fscki->calc_xnms) {
2307 ubifs_err("inode %lu has xattr names' size %u, but "
2308 "calculated names' size is %lld",
e84461ad 2309 (unsigned long)fscki->inum, fscki->xattr_nms,
1e51764a
AB
2310 fscki->calc_xnms);
2311 goto out_dump;
2312 }
2313 }
2314
2315 return 0;
2316
2317out_dump:
2318 /* Read the bad inode and dump it */
2319 ino_key_init(c, &key, fscki->inum);
2320 err = ubifs_lookup_level0(c, &key, &znode, &n);
2321 if (!err) {
e84461ad
AB
2322 ubifs_err("inode %lu not found in index",
2323 (unsigned long)fscki->inum);
1e51764a
AB
2324 return -ENOENT;
2325 } else if (err < 0) {
2326 ubifs_err("error %d while looking up inode %lu",
e84461ad 2327 err, (unsigned long)fscki->inum);
1e51764a
AB
2328 return err;
2329 }
2330
2331 zbr = &znode->zbranch[n];
2332 ino = kmalloc(zbr->len, GFP_NOFS);
2333 if (!ino)
2334 return -ENOMEM;
2335
2336 err = ubifs_tnc_read_node(c, zbr, ino);
2337 if (err) {
2338 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2339 zbr->lnum, zbr->offs, err);
2340 kfree(ino);
2341 return err;
2342 }
2343
2344 ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",
e84461ad 2345 (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
1e51764a
AB
2346 dbg_dump_node(c, ino);
2347 kfree(ino);
2348 return -EINVAL;
2349}
2350
2351/**
2352 * dbg_check_filesystem - check the file-system.
2353 * @c: UBIFS file-system description object
2354 *
2355 * This function checks the file system, namely:
2356 * o makes sure that all leaf nodes exist and their CRCs are correct;
2357 * o makes sure inode nlink, size, xattr size/count are correct (for all
2358 * inodes).
2359 *
2360 * The function reads whole indexing tree and all nodes, so it is pretty
2361 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2362 * not, and a negative error code in case of failure.
2363 */
2364int dbg_check_filesystem(struct ubifs_info *c)
2365{
2366 int err;
2367 struct fsck_data fsckd;
2368
2369 if (!(ubifs_chk_flags & UBIFS_CHK_FS))
2370 return 0;
2371
2372 fsckd.inodes = RB_ROOT;
2373 err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
2374 if (err)
2375 goto out_free;
2376
2377 err = check_inodes(c, &fsckd);
2378 if (err)
2379 goto out_free;
2380
2381 free_inodes(&fsckd);
2382 return 0;
2383
2384out_free:
2385 ubifs_err("file-system check failed with error %d", err);
2386 dump_stack();
2387 free_inodes(&fsckd);
2388 return err;
2389}
2390
3bb66b47
AB
2391/**
2392 * dbg_check_data_nodes_order - check that list of data nodes is sorted.
2393 * @c: UBIFS file-system description object
2394 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2395 *
2396 * This function returns zero if the list of data nodes is sorted correctly,
2397 * and %-EINVAL if not.
2398 */
2399int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head)
2400{
2401 struct list_head *cur;
2402 struct ubifs_scan_node *sa, *sb;
2403
2404 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
2405 return 0;
2406
2407 for (cur = head->next; cur->next != head; cur = cur->next) {
2408 ino_t inuma, inumb;
2409 uint32_t blka, blkb;
2410
2411 cond_resched();
2412 sa = container_of(cur, struct ubifs_scan_node, list);
2413 sb = container_of(cur->next, struct ubifs_scan_node, list);
2414
2415 if (sa->type != UBIFS_DATA_NODE) {
2416 ubifs_err("bad node type %d", sa->type);
2417 dbg_dump_node(c, sa->node);
2418 return -EINVAL;
2419 }
2420 if (sb->type != UBIFS_DATA_NODE) {
2421 ubifs_err("bad node type %d", sb->type);
2422 dbg_dump_node(c, sb->node);
2423 return -EINVAL;
2424 }
2425
2426 inuma = key_inum(c, &sa->key);
2427 inumb = key_inum(c, &sb->key);
2428
2429 if (inuma < inumb)
2430 continue;
2431 if (inuma > inumb) {
2432 ubifs_err("larger inum %lu goes before inum %lu",
2433 (unsigned long)inuma, (unsigned long)inumb);
2434 goto error_dump;
2435 }
2436
2437 blka = key_block(c, &sa->key);
2438 blkb = key_block(c, &sb->key);
2439
2440 if (blka > blkb) {
2441 ubifs_err("larger block %u goes before %u", blka, blkb);
2442 goto error_dump;
2443 }
2444 if (blka == blkb) {
2445 ubifs_err("two data nodes for the same block");
2446 goto error_dump;
2447 }
2448 }
2449
2450 return 0;
2451
2452error_dump:
2453 dbg_dump_node(c, sa->node);
2454 dbg_dump_node(c, sb->node);
2455 return -EINVAL;
2456}
2457
2458/**
2459 * dbg_check_nondata_nodes_order - check that list of data nodes is sorted.
2460 * @c: UBIFS file-system description object
2461 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2462 *
2463 * This function returns zero if the list of non-data nodes is sorted correctly,
2464 * and %-EINVAL if not.
2465 */
2466int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head)
2467{
2468 struct list_head *cur;
2469 struct ubifs_scan_node *sa, *sb;
2470
2471 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
2472 return 0;
2473
2474 for (cur = head->next; cur->next != head; cur = cur->next) {
2475 ino_t inuma, inumb;
2476 uint32_t hasha, hashb;
2477
2478 cond_resched();
2479 sa = container_of(cur, struct ubifs_scan_node, list);
2480 sb = container_of(cur->next, struct ubifs_scan_node, list);
2481
2482 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2483 sa->type != UBIFS_XENT_NODE) {
2484 ubifs_err("bad node type %d", sa->type);
2485 dbg_dump_node(c, sa->node);
2486 return -EINVAL;
2487 }
2488 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2489 sa->type != UBIFS_XENT_NODE) {
2490 ubifs_err("bad node type %d", sb->type);
2491 dbg_dump_node(c, sb->node);
2492 return -EINVAL;
2493 }
2494
2495 if (sa->type != UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2496 ubifs_err("non-inode node goes before inode node");
2497 goto error_dump;
2498 }
2499
2500 if (sa->type == UBIFS_INO_NODE && sb->type != UBIFS_INO_NODE)
2501 continue;
2502
2503 if (sa->type == UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2504 /* Inode nodes are sorted in descending size order */
2505 if (sa->len < sb->len) {
2506 ubifs_err("smaller inode node goes first");
2507 goto error_dump;
2508 }
2509 continue;
2510 }
2511
2512 /*
2513 * This is either a dentry or xentry, which should be sorted in
2514 * ascending (parent ino, hash) order.
2515 */
2516 inuma = key_inum(c, &sa->key);
2517 inumb = key_inum(c, &sb->key);
2518
2519 if (inuma < inumb)
2520 continue;
2521 if (inuma > inumb) {
2522 ubifs_err("larger inum %lu goes before inum %lu",
2523 (unsigned long)inuma, (unsigned long)inumb);
2524 goto error_dump;
2525 }
2526
2527 hasha = key_block(c, &sa->key);
2528 hashb = key_block(c, &sb->key);
2529
2530 if (hasha > hashb) {
c4361570
AB
2531 ubifs_err("larger hash %u goes before %u",
2532 hasha, hashb);
3bb66b47
AB
2533 goto error_dump;
2534 }
2535 }
2536
2537 return 0;
2538
2539error_dump:
2540 ubifs_msg("dumping first node");
2541 dbg_dump_node(c, sa->node);
2542 ubifs_msg("dumping second node");
2543 dbg_dump_node(c, sb->node);
2544 return -EINVAL;
2545 return 0;
2546}
2547
1e51764a
AB
2548int dbg_force_in_the_gaps(void)
2549{
bc3f07f0 2550 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
1e51764a 2551 return 0;
bc3f07f0
AB
2552
2553 return !(random32() & 7);
1e51764a
AB
2554}
2555
2556/* Failure mode for recovery testing */
2557
2558#define chance(n, d) (simple_rand() <= (n) * 32768LL / (d))
2559
2560struct failure_mode_info {
2561 struct list_head list;
2562 struct ubifs_info *c;
2563};
2564
2565static LIST_HEAD(fmi_list);
2566static DEFINE_SPINLOCK(fmi_lock);
2567
2568static unsigned int next;
2569
2570static int simple_rand(void)
2571{
2572 if (next == 0)
2573 next = current->pid;
2574 next = next * 1103515245 + 12345;
2575 return (next >> 16) & 32767;
2576}
2577
17c2f9f8 2578static void failure_mode_init(struct ubifs_info *c)
1e51764a
AB
2579{
2580 struct failure_mode_info *fmi;
2581
2582 fmi = kmalloc(sizeof(struct failure_mode_info), GFP_NOFS);
2583 if (!fmi) {
552ff317 2584 ubifs_err("Failed to register failure mode - no memory");
1e51764a
AB
2585 return;
2586 }
2587 fmi->c = c;
2588 spin_lock(&fmi_lock);
2589 list_add_tail(&fmi->list, &fmi_list);
2590 spin_unlock(&fmi_lock);
2591}
2592
17c2f9f8 2593static void failure_mode_exit(struct ubifs_info *c)
1e51764a
AB
2594{
2595 struct failure_mode_info *fmi, *tmp;
2596
2597 spin_lock(&fmi_lock);
2598 list_for_each_entry_safe(fmi, tmp, &fmi_list, list)
2599 if (fmi->c == c) {
2600 list_del(&fmi->list);
2601 kfree(fmi);
2602 }
2603 spin_unlock(&fmi_lock);
2604}
2605
2606static struct ubifs_info *dbg_find_info(struct ubi_volume_desc *desc)
2607{
2608 struct failure_mode_info *fmi;
2609
2610 spin_lock(&fmi_lock);
2611 list_for_each_entry(fmi, &fmi_list, list)
2612 if (fmi->c->ubi == desc) {
2613 struct ubifs_info *c = fmi->c;
2614
2615 spin_unlock(&fmi_lock);
2616 return c;
2617 }
2618 spin_unlock(&fmi_lock);
2619 return NULL;
2620}
2621
2622static int in_failure_mode(struct ubi_volume_desc *desc)
2623{
2624 struct ubifs_info *c = dbg_find_info(desc);
2625
2626 if (c && dbg_failure_mode)
17c2f9f8 2627 return c->dbg->failure_mode;
1e51764a
AB
2628 return 0;
2629}
2630
2631static int do_fail(struct ubi_volume_desc *desc, int lnum, int write)
2632{
2633 struct ubifs_info *c = dbg_find_info(desc);
17c2f9f8 2634 struct ubifs_debug_info *d;
1e51764a
AB
2635
2636 if (!c || !dbg_failure_mode)
2637 return 0;
17c2f9f8
AB
2638 d = c->dbg;
2639 if (d->failure_mode)
1e51764a 2640 return 1;
17c2f9f8 2641 if (!d->fail_cnt) {
1e51764a
AB
2642 /* First call - decide delay to failure */
2643 if (chance(1, 2)) {
2644 unsigned int delay = 1 << (simple_rand() >> 11);
2645
2646 if (chance(1, 2)) {
17c2f9f8
AB
2647 d->fail_delay = 1;
2648 d->fail_timeout = jiffies +
1e51764a
AB
2649 msecs_to_jiffies(delay);
2650 dbg_rcvry("failing after %ums", delay);
2651 } else {
17c2f9f8
AB
2652 d->fail_delay = 2;
2653 d->fail_cnt_max = delay;
1e51764a
AB
2654 dbg_rcvry("failing after %u calls", delay);
2655 }
2656 }
17c2f9f8 2657 d->fail_cnt += 1;
1e51764a
AB
2658 }
2659 /* Determine if failure delay has expired */
17c2f9f8
AB
2660 if (d->fail_delay == 1) {
2661 if (time_before(jiffies, d->fail_timeout))
1e51764a 2662 return 0;
17c2f9f8
AB
2663 } else if (d->fail_delay == 2)
2664 if (d->fail_cnt++ < d->fail_cnt_max)
1e51764a
AB
2665 return 0;
2666 if (lnum == UBIFS_SB_LNUM) {
2667 if (write) {
2668 if (chance(1, 2))
2669 return 0;
2670 } else if (chance(19, 20))
2671 return 0;
2672 dbg_rcvry("failing in super block LEB %d", lnum);
2673 } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2674 if (chance(19, 20))
2675 return 0;
2676 dbg_rcvry("failing in master LEB %d", lnum);
2677 } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2678 if (write) {
2679 if (chance(99, 100))
2680 return 0;
2681 } else if (chance(399, 400))
2682 return 0;
2683 dbg_rcvry("failing in log LEB %d", lnum);
2684 } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2685 if (write) {
2686 if (chance(7, 8))
2687 return 0;
2688 } else if (chance(19, 20))
2689 return 0;
2690 dbg_rcvry("failing in LPT LEB %d", lnum);
2691 } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2692 if (write) {
2693 if (chance(1, 2))
2694 return 0;
2695 } else if (chance(9, 10))
2696 return 0;
2697 dbg_rcvry("failing in orphan LEB %d", lnum);
2698 } else if (lnum == c->ihead_lnum) {
2699 if (chance(99, 100))
2700 return 0;
2701 dbg_rcvry("failing in index head LEB %d", lnum);
2702 } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2703 if (chance(9, 10))
2704 return 0;
2705 dbg_rcvry("failing in GC head LEB %d", lnum);
2706 } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
2707 !ubifs_search_bud(c, lnum)) {
2708 if (chance(19, 20))
2709 return 0;
2710 dbg_rcvry("failing in non-bud LEB %d", lnum);
2711 } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
2712 c->cmt_state == COMMIT_RUNNING_REQUIRED) {
2713 if (chance(999, 1000))
2714 return 0;
2715 dbg_rcvry("failing in bud LEB %d commit running", lnum);
2716 } else {
2717 if (chance(9999, 10000))
2718 return 0;
2719 dbg_rcvry("failing in bud LEB %d commit not running", lnum);
2720 }
2721 ubifs_err("*** SETTING FAILURE MODE ON (LEB %d) ***", lnum);
17c2f9f8 2722 d->failure_mode = 1;
1e51764a
AB
2723 dump_stack();
2724 return 1;
2725}
2726
2727static void cut_data(const void *buf, int len)
2728{
2729 int flen, i;
2730 unsigned char *p = (void *)buf;
2731
2732 flen = (len * (long long)simple_rand()) >> 15;
2733 for (i = flen; i < len; i++)
2734 p[i] = 0xff;
2735}
2736
2737int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
2738 int len, int check)
2739{
2740 if (in_failure_mode(desc))
1a29af8b 2741 return -EROFS;
1e51764a
AB
2742 return ubi_leb_read(desc, lnum, buf, offset, len, check);
2743}
2744
2745int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
2746 int offset, int len, int dtype)
2747{
16dfd804 2748 int err, failing;
1e51764a
AB
2749
2750 if (in_failure_mode(desc))
1a29af8b 2751 return -EROFS;
16dfd804
AH
2752 failing = do_fail(desc, lnum, 1);
2753 if (failing)
1e51764a
AB
2754 cut_data(buf, len);
2755 err = ubi_leb_write(desc, lnum, buf, offset, len, dtype);
2756 if (err)
2757 return err;
16dfd804 2758 if (failing)
1a29af8b 2759 return -EROFS;
1e51764a
AB
2760 return 0;
2761}
2762
2763int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
2764 int len, int dtype)
2765{
2766 int err;
2767
2768 if (do_fail(desc, lnum, 1))
1a29af8b 2769 return -EROFS;
1e51764a
AB
2770 err = ubi_leb_change(desc, lnum, buf, len, dtype);
2771 if (err)
2772 return err;
2773 if (do_fail(desc, lnum, 1))
1a29af8b 2774 return -EROFS;
1e51764a
AB
2775 return 0;
2776}
2777
2778int dbg_leb_erase(struct ubi_volume_desc *desc, int lnum)
2779{
2780 int err;
2781
2782 if (do_fail(desc, lnum, 0))
1a29af8b 2783 return -EROFS;
1e51764a
AB
2784 err = ubi_leb_erase(desc, lnum);
2785 if (err)
2786 return err;
2787 if (do_fail(desc, lnum, 0))
1a29af8b 2788 return -EROFS;
1e51764a
AB
2789 return 0;
2790}
2791
2792int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum)
2793{
2794 int err;
2795
2796 if (do_fail(desc, lnum, 0))
1a29af8b 2797 return -EROFS;
1e51764a
AB
2798 err = ubi_leb_unmap(desc, lnum);
2799 if (err)
2800 return err;
2801 if (do_fail(desc, lnum, 0))
1a29af8b 2802 return -EROFS;
1e51764a
AB
2803 return 0;
2804}
2805
2806int dbg_is_mapped(struct ubi_volume_desc *desc, int lnum)
2807{
2808 if (in_failure_mode(desc))
1a29af8b 2809 return -EROFS;
1e51764a
AB
2810 return ubi_is_mapped(desc, lnum);
2811}
2812
2813int dbg_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
2814{
2815 int err;
2816
2817 if (do_fail(desc, lnum, 0))
1a29af8b 2818 return -EROFS;
1e51764a
AB
2819 err = ubi_leb_map(desc, lnum, dtype);
2820 if (err)
2821 return err;
2822 if (do_fail(desc, lnum, 0))
1a29af8b 2823 return -EROFS;
1e51764a
AB
2824 return 0;
2825}
2826
17c2f9f8
AB
2827/**
2828 * ubifs_debugging_init - initialize UBIFS debugging.
2829 * @c: UBIFS file-system description object
2830 *
2831 * This function initializes debugging-related data for the file system.
2832 * Returns zero in case of success and a negative error code in case of
2833 * failure.
2834 */
2835int ubifs_debugging_init(struct ubifs_info *c)
2836{
2837 c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
2838 if (!c->dbg)
2839 return -ENOMEM;
2840
17c2f9f8
AB
2841 failure_mode_init(c);
2842 return 0;
17c2f9f8
AB
2843}
2844
2845/**
2846 * ubifs_debugging_exit - free debugging data.
2847 * @c: UBIFS file-system description object
2848 */
2849void ubifs_debugging_exit(struct ubifs_info *c)
2850{
2851 failure_mode_exit(c);
17c2f9f8
AB
2852 kfree(c->dbg);
2853}
2854
552ff317
AB
2855/*
2856 * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
2857 * contain the stuff specific to particular file-system mounts.
2858 */
84abf972 2859static struct dentry *dfs_rootdir;
552ff317
AB
2860
2861/**
2862 * dbg_debugfs_init - initialize debugfs file-system.
2863 *
2864 * UBIFS uses debugfs file-system to expose various debugging knobs to
2865 * user-space. This function creates "ubifs" directory in the debugfs
2866 * file-system. Returns zero in case of success and a negative error code in
2867 * case of failure.
2868 */
2869int dbg_debugfs_init(void)
2870{
84abf972 2871 dfs_rootdir = debugfs_create_dir("ubifs", NULL);
ae380ce0
AB
2872 if (IS_ERR_OR_NULL(dfs_rootdir)) {
2873 int err = dfs_rootdir ? PTR_ERR(dfs_rootdir) : -ENODEV;
552ff317
AB
2874 ubifs_err("cannot create \"ubifs\" debugfs directory, "
2875 "error %d\n", err);
2876 return err;
2877 }
2878
2879 return 0;
2880}
2881
2882/**
2883 * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
2884 */
2885void dbg_debugfs_exit(void)
2886{
84abf972 2887 debugfs_remove(dfs_rootdir);
552ff317
AB
2888}
2889
2890static int open_debugfs_file(struct inode *inode, struct file *file)
2891{
2892 file->private_data = inode->i_private;
1bbfc848 2893 return nonseekable_open(inode, file);
552ff317
AB
2894}
2895
2896static ssize_t write_debugfs_file(struct file *file, const char __user *buf,
2897 size_t count, loff_t *ppos)
2898{
2899 struct ubifs_info *c = file->private_data;
2900 struct ubifs_debug_info *d = c->dbg;
2901
84abf972 2902 if (file->f_path.dentry == d->dfs_dump_lprops)
552ff317 2903 dbg_dump_lprops(c);
8ff83089 2904 else if (file->f_path.dentry == d->dfs_dump_budg)
f1bd66af 2905 dbg_dump_budg(c, &c->bi);
8ff83089 2906 else if (file->f_path.dentry == d->dfs_dump_tnc) {
552ff317
AB
2907 mutex_lock(&c->tnc_mutex);
2908 dbg_dump_tnc(c);
2909 mutex_unlock(&c->tnc_mutex);
2910 } else
2911 return -EINVAL;
2912
552ff317
AB
2913 return count;
2914}
2915
84abf972 2916static const struct file_operations dfs_fops = {
552ff317
AB
2917 .open = open_debugfs_file,
2918 .write = write_debugfs_file,
2919 .owner = THIS_MODULE,
1bbfc848 2920 .llseek = no_llseek,
552ff317
AB
2921};
2922
2923/**
2924 * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
2925 * @c: UBIFS file-system description object
2926 *
2927 * This function creates all debugfs files for this instance of UBIFS. Returns
2928 * zero in case of success and a negative error code in case of failure.
2929 *
2930 * Note, the only reason we have not merged this function with the
2931 * 'ubifs_debugging_init()' function is because it is better to initialize
2932 * debugfs interfaces at the very end of the mount process, and remove them at
2933 * the very beginning of the mount process.
2934 */
2935int dbg_debugfs_init_fs(struct ubifs_info *c)
2936{
ae380ce0 2937 int err, n;
552ff317
AB
2938 const char *fname;
2939 struct dentry *dent;
2940 struct ubifs_debug_info *d = c->dbg;
2941
ae380ce0
AB
2942 n = snprintf(d->dfs_dir_name, UBIFS_DFS_DIR_LEN + 1, UBIFS_DFS_DIR_NAME,
2943 c->vi.ubi_num, c->vi.vol_id);
2944 if (n == UBIFS_DFS_DIR_LEN) {
2945 /* The array size is too small */
2946 fname = UBIFS_DFS_DIR_NAME;
2947 dent = ERR_PTR(-EINVAL);
2948 goto out;
2949 }
2950
cc6a86b9
AB
2951 fname = d->dfs_dir_name;
2952 dent = debugfs_create_dir(fname, dfs_rootdir);
95169535 2953 if (IS_ERR_OR_NULL(dent))
552ff317 2954 goto out;
cc6a86b9 2955 d->dfs_dir = dent;
552ff317
AB
2956
2957 fname = "dump_lprops";
8c559d30 2958 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
95169535 2959 if (IS_ERR_OR_NULL(dent))
552ff317 2960 goto out_remove;
84abf972 2961 d->dfs_dump_lprops = dent;
552ff317
AB
2962
2963 fname = "dump_budg";
8c559d30 2964 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
95169535 2965 if (IS_ERR_OR_NULL(dent))
552ff317 2966 goto out_remove;
84abf972 2967 d->dfs_dump_budg = dent;
552ff317
AB
2968
2969 fname = "dump_tnc";
8c559d30 2970 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
95169535 2971 if (IS_ERR_OR_NULL(dent))
552ff317 2972 goto out_remove;
84abf972 2973 d->dfs_dump_tnc = dent;
552ff317
AB
2974
2975 return 0;
2976
2977out_remove:
cc6a86b9
AB
2978 debugfs_remove_recursive(d->dfs_dir);
2979out:
95169535 2980 err = dent ? PTR_ERR(dent) : -ENODEV;
ae380ce0 2981 ubifs_err("cannot create \"%s\" debugfs filr or directory, error %d\n",
552ff317 2982 fname, err);
552ff317
AB
2983 return err;
2984}
2985
2986/**
2987 * dbg_debugfs_exit_fs - remove all debugfs files.
2988 * @c: UBIFS file-system description object
2989 */
2990void dbg_debugfs_exit_fs(struct ubifs_info *c)
2991{
84abf972 2992 debugfs_remove_recursive(c->dbg->dfs_dir);
552ff317
AB
2993}
2994
1e51764a 2995#endif /* CONFIG_UBIFS_FS_DEBUG */