blob: ff2a5719c3eb755eaa16e7c4adaf96c27517a881 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roese2fc10f62009-03-19 15:35:05 +01002/*
3 * This file is part of UBIFS.
4 *
5 * Copyright (C) 2006-2008 Nokia Corporation
6 *
Stefan Roese2fc10f62009-03-19 15:35:05 +01007 * Authors: Artem Bityutskiy (Битюцкий Артём)
8 * Adrian Hunter
9 */
10
11/*
12 * This file implements most of the debugging stuff which is compiled in only
13 * when it is enabled. But some debugging check functions are implemented in
14 * corresponding subsystem, just because they are closely related and utilize
15 * various local functions of those subsystems.
16 */
17
Alexey Brodkin2d2fa492018-06-05 17:17:57 +030018#include <hexdump.h>
Simon Glass0f2af882020-05-10 11:40:05 -060019#include <log.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070020#include <dm/devres.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060021#include <linux/printk.h>
Alexey Brodkin2d2fa492018-06-05 17:17:57 +030022
Heiko Schocherf5895d12014-06-24 10:10:04 +020023#ifndef __UBOOT__
24#include <linux/module.h>
25#include <linux/debugfs.h>
26#include <linux/math64.h>
27#include <linux/uaccess.h>
28#include <linux/random.h>
29#else
30#include <linux/compat.h>
31#include <linux/err.h>
32#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +010033#include "ubifs.h"
34
Heiko Schocherf5895d12014-06-24 10:10:04 +020035#ifndef __UBOOT__
36static DEFINE_SPINLOCK(dbg_lock);
37#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +010038
Pali Rohár92722062022-08-07 21:27:09 +020039#ifndef CONFIG_UBIFS_SILENCE_DEBUG_DUMP
Heiko Schocherf5895d12014-06-24 10:10:04 +020040static const char *get_key_fmt(int fmt)
41{
42 switch (fmt) {
43 case UBIFS_SIMPLE_KEY_FMT:
44 return "simple";
45 default:
46 return "unknown/invalid format";
47 }
48}
Stefan Roese2fc10f62009-03-19 15:35:05 +010049
Heiko Schocherf5895d12014-06-24 10:10:04 +020050static const char *get_key_hash(int hash)
51{
52 switch (hash) {
53 case UBIFS_KEY_HASH_R5:
54 return "R5";
55 case UBIFS_KEY_HASH_TEST:
56 return "test";
57 default:
58 return "unknown/invalid name hash";
59 }
60}
Stefan Roese2fc10f62009-03-19 15:35:05 +010061
62static const char *get_key_type(int type)
63{
64 switch (type) {
65 case UBIFS_INO_KEY:
66 return "inode";
67 case UBIFS_DENT_KEY:
68 return "direntry";
69 case UBIFS_XENT_KEY:
70 return "xentry";
71 case UBIFS_DATA_KEY:
72 return "data";
73 case UBIFS_TRUN_KEY:
74 return "truncate";
75 default:
76 return "unknown/invalid key";
77 }
78}
Heiko Schocherf5895d12014-06-24 10:10:04 +020079
80#ifndef __UBOOT__
81static const char *get_dent_type(int type)
82{
83 switch (type) {
84 case UBIFS_ITYPE_REG:
85 return "file";
86 case UBIFS_ITYPE_DIR:
87 return "dir";
88 case UBIFS_ITYPE_LNK:
89 return "symlink";
90 case UBIFS_ITYPE_BLK:
91 return "blkdev";
92 case UBIFS_ITYPE_CHR:
93 return "char dev";
94 case UBIFS_ITYPE_FIFO:
95 return "fifo";
96 case UBIFS_ITYPE_SOCK:
97 return "socket";
98 default:
99 return "unknown/invalid type";
100 }
101}
102#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +0100103
Heiko Schocherf5895d12014-06-24 10:10:04 +0200104const char *dbg_snprintf_key(const struct ubifs_info *c,
105 const union ubifs_key *key, char *buffer, int len)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100106{
107 char *p = buffer;
108 int type = key_type(c, key);
109
110 if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
111 switch (type) {
112 case UBIFS_INO_KEY:
Heiko Schocherf5895d12014-06-24 10:10:04 +0200113 len -= snprintf(p, len, "(%lu, %s)",
114 (unsigned long)key_inum(c, key),
115 get_key_type(type));
Stefan Roese2fc10f62009-03-19 15:35:05 +0100116 break;
117 case UBIFS_DENT_KEY:
118 case UBIFS_XENT_KEY:
Heiko Schocherf5895d12014-06-24 10:10:04 +0200119 len -= snprintf(p, len, "(%lu, %s, %#08x)",
120 (unsigned long)key_inum(c, key),
121 get_key_type(type), key_hash(c, key));
Stefan Roese2fc10f62009-03-19 15:35:05 +0100122 break;
123 case UBIFS_DATA_KEY:
Heiko Schocherf5895d12014-06-24 10:10:04 +0200124 len -= snprintf(p, len, "(%lu, %s, %u)",
125 (unsigned long)key_inum(c, key),
126 get_key_type(type), key_block(c, key));
Stefan Roese2fc10f62009-03-19 15:35:05 +0100127 break;
128 case UBIFS_TRUN_KEY:
Heiko Schocherf5895d12014-06-24 10:10:04 +0200129 len -= snprintf(p, len, "(%lu, %s)",
130 (unsigned long)key_inum(c, key),
131 get_key_type(type));
Stefan Roese2fc10f62009-03-19 15:35:05 +0100132 break;
133 default:
Heiko Schocherf5895d12014-06-24 10:10:04 +0200134 len -= snprintf(p, len, "(bad key type: %#08x, %#08x)",
135 key->u32[0], key->u32[1]);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100136 }
137 } else
Heiko Schocherf5895d12014-06-24 10:10:04 +0200138 len -= snprintf(p, len, "bad key format %d", c->key_fmt);
139 ubifs_assert(len > 0);
140 return p;
141}
142
143const char *dbg_ntype(int type)
144{
145 switch (type) {
146 case UBIFS_PAD_NODE:
147 return "padding node";
148 case UBIFS_SB_NODE:
149 return "superblock node";
150 case UBIFS_MST_NODE:
151 return "master node";
152 case UBIFS_REF_NODE:
153 return "reference node";
154 case UBIFS_INO_NODE:
155 return "inode node";
156 case UBIFS_DENT_NODE:
157 return "direntry node";
158 case UBIFS_XENT_NODE:
159 return "xentry node";
160 case UBIFS_DATA_NODE:
161 return "data node";
162 case UBIFS_TRUN_NODE:
163 return "truncate node";
164 case UBIFS_IDX_NODE:
165 return "indexing node";
166 case UBIFS_CS_NODE:
167 return "commit start node";
168 case UBIFS_ORPH_NODE:
169 return "orphan node";
170 default:
171 return "unknown node";
172 }
173}
174
175static const char *dbg_gtype(int type)
176{
177 switch (type) {
178 case UBIFS_NO_NODE_GROUP:
179 return "no node group";
180 case UBIFS_IN_NODE_GROUP:
181 return "in node group";
182 case UBIFS_LAST_OF_NODE_GROUP:
183 return "last of node group";
184 default:
185 return "unknown";
186 }
187}
188
189const char *dbg_cstate(int cmt_state)
190{
191 switch (cmt_state) {
192 case COMMIT_RESTING:
193 return "commit resting";
194 case COMMIT_BACKGROUND:
195 return "background commit requested";
196 case COMMIT_REQUIRED:
197 return "commit required";
198 case COMMIT_RUNNING_BACKGROUND:
199 return "BACKGROUND commit running";
200 case COMMIT_RUNNING_REQUIRED:
201 return "commit running and required";
202 case COMMIT_BROKEN:
203 return "broken commit";
204 default:
205 return "unknown commit state";
206 }
207}
208
209const char *dbg_jhead(int jhead)
210{
211 switch (jhead) {
212 case GCHD:
213 return "0 (GC)";
214 case BASEHD:
215 return "1 (base)";
216 case DATAHD:
217 return "2 (data)";
218 default:
219 return "unknown journal head";
220 }
221}
222
223static void dump_ch(const struct ubifs_ch *ch)
224{
225 pr_err("\tmagic %#x\n", le32_to_cpu(ch->magic));
226 pr_err("\tcrc %#x\n", le32_to_cpu(ch->crc));
227 pr_err("\tnode_type %d (%s)\n", ch->node_type,
228 dbg_ntype(ch->node_type));
229 pr_err("\tgroup_type %d (%s)\n", ch->group_type,
230 dbg_gtype(ch->group_type));
231 pr_err("\tsqnum %llu\n",
232 (unsigned long long)le64_to_cpu(ch->sqnum));
233 pr_err("\tlen %u\n", le32_to_cpu(ch->len));
234}
Pali Rohár92722062022-08-07 21:27:09 +0200235#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +0200236
237void ubifs_dump_inode(struct ubifs_info *c, const struct inode *inode)
238{
239#ifndef __UBOOT__
240 const struct ubifs_inode *ui = ubifs_inode(inode);
241 struct qstr nm = { .name = NULL };
242 union ubifs_key key;
243 struct ubifs_dent_node *dent, *pdent = NULL;
244 int count = 2;
245
246 pr_err("Dump in-memory inode:");
247 pr_err("\tinode %lu\n", inode->i_ino);
248 pr_err("\tsize %llu\n",
249 (unsigned long long)i_size_read(inode));
250 pr_err("\tnlink %u\n", inode->i_nlink);
251 pr_err("\tuid %u\n", (unsigned int)i_uid_read(inode));
252 pr_err("\tgid %u\n", (unsigned int)i_gid_read(inode));
253 pr_err("\tatime %u.%u\n",
254 (unsigned int)inode->i_atime.tv_sec,
255 (unsigned int)inode->i_atime.tv_nsec);
256 pr_err("\tmtime %u.%u\n",
257 (unsigned int)inode->i_mtime.tv_sec,
258 (unsigned int)inode->i_mtime.tv_nsec);
259 pr_err("\tctime %u.%u\n",
260 (unsigned int)inode->i_ctime.tv_sec,
261 (unsigned int)inode->i_ctime.tv_nsec);
262 pr_err("\tcreat_sqnum %llu\n", ui->creat_sqnum);
263 pr_err("\txattr_size %u\n", ui->xattr_size);
264 pr_err("\txattr_cnt %u\n", ui->xattr_cnt);
265 pr_err("\txattr_names %u\n", ui->xattr_names);
266 pr_err("\tdirty %u\n", ui->dirty);
267 pr_err("\txattr %u\n", ui->xattr);
268 pr_err("\tbulk_read %u\n", ui->xattr);
269 pr_err("\tsynced_i_size %llu\n",
270 (unsigned long long)ui->synced_i_size);
271 pr_err("\tui_size %llu\n",
272 (unsigned long long)ui->ui_size);
273 pr_err("\tflags %d\n", ui->flags);
274 pr_err("\tcompr_type %d\n", ui->compr_type);
275 pr_err("\tlast_page_read %lu\n", ui->last_page_read);
276 pr_err("\tread_in_a_row %lu\n", ui->read_in_a_row);
277 pr_err("\tdata_len %d\n", ui->data_len);
278
279 if (!S_ISDIR(inode->i_mode))
280 return;
281
282 pr_err("List of directory entries:\n");
283 ubifs_assert(!mutex_is_locked(&c->tnc_mutex));
284
285 lowest_dent_key(c, &key, inode->i_ino);
286 while (1) {
287 dent = ubifs_tnc_next_ent(c, &key, &nm);
288 if (IS_ERR(dent)) {
289 if (PTR_ERR(dent) != -ENOENT)
290 pr_err("error %ld\n", PTR_ERR(dent));
291 break;
292 }
293
294 pr_err("\t%d: %s (%s)\n",
295 count++, dent->name, get_dent_type(dent->type));
296
297 nm.name = dent->name;
298 nm.len = le16_to_cpu(dent->nlen);
299 kfree(pdent);
300 pdent = dent;
301 key_read(c, &dent->key, &key);
302 }
303 kfree(pdent);
304#endif
305}
306
307void ubifs_dump_node(const struct ubifs_info *c, const void *node)
308{
Pali Rohár92722062022-08-07 21:27:09 +0200309#ifndef CONFIG_UBIFS_SILENCE_DEBUG_DUMP
Heiko Schocherf5895d12014-06-24 10:10:04 +0200310 int i, n;
311 union ubifs_key key;
312 const struct ubifs_ch *ch = node;
313 char key_buf[DBG_KEY_BUF_LEN];
314
315 /* If the magic is incorrect, just hexdump the first bytes */
316 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
317 pr_err("Not a node, first %zu bytes:", UBIFS_CH_SZ);
Alexey Brodkin2d2fa492018-06-05 17:17:57 +0300318 print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 1,
Heiko Schocherf5895d12014-06-24 10:10:04 +0200319 (void *)node, UBIFS_CH_SZ, 1);
320 return;
321 }
322
323 spin_lock(&dbg_lock);
324 dump_ch(node);
325
326 switch (ch->node_type) {
327 case UBIFS_PAD_NODE:
328 {
329 const struct ubifs_pad_node *pad = node;
330
331 pr_err("\tpad_len %u\n", le32_to_cpu(pad->pad_len));
332 break;
333 }
334 case UBIFS_SB_NODE:
335 {
336 const struct ubifs_sb_node *sup = node;
337 unsigned int sup_flags = le32_to_cpu(sup->flags);
338
339 pr_err("\tkey_hash %d (%s)\n",
340 (int)sup->key_hash, get_key_hash(sup->key_hash));
341 pr_err("\tkey_fmt %d (%s)\n",
342 (int)sup->key_fmt, get_key_fmt(sup->key_fmt));
343 pr_err("\tflags %#x\n", sup_flags);
Heiko Schocher94b66de2015-10-22 06:19:21 +0200344 pr_err("\tbig_lpt %u\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200345 !!(sup_flags & UBIFS_FLG_BIGLPT));
Heiko Schocher94b66de2015-10-22 06:19:21 +0200346 pr_err("\tspace_fixup %u\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200347 !!(sup_flags & UBIFS_FLG_SPACE_FIXUP));
348 pr_err("\tmin_io_size %u\n", le32_to_cpu(sup->min_io_size));
349 pr_err("\tleb_size %u\n", le32_to_cpu(sup->leb_size));
350 pr_err("\tleb_cnt %u\n", le32_to_cpu(sup->leb_cnt));
351 pr_err("\tmax_leb_cnt %u\n", le32_to_cpu(sup->max_leb_cnt));
352 pr_err("\tmax_bud_bytes %llu\n",
353 (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
354 pr_err("\tlog_lebs %u\n", le32_to_cpu(sup->log_lebs));
355 pr_err("\tlpt_lebs %u\n", le32_to_cpu(sup->lpt_lebs));
356 pr_err("\torph_lebs %u\n", le32_to_cpu(sup->orph_lebs));
357 pr_err("\tjhead_cnt %u\n", le32_to_cpu(sup->jhead_cnt));
358 pr_err("\tfanout %u\n", le32_to_cpu(sup->fanout));
359 pr_err("\tlsave_cnt %u\n", le32_to_cpu(sup->lsave_cnt));
360 pr_err("\tdefault_compr %u\n",
361 (int)le16_to_cpu(sup->default_compr));
362 pr_err("\trp_size %llu\n",
363 (unsigned long long)le64_to_cpu(sup->rp_size));
364 pr_err("\trp_uid %u\n", le32_to_cpu(sup->rp_uid));
365 pr_err("\trp_gid %u\n", le32_to_cpu(sup->rp_gid));
366 pr_err("\tfmt_version %u\n", le32_to_cpu(sup->fmt_version));
367 pr_err("\ttime_gran %u\n", le32_to_cpu(sup->time_gran));
368 pr_err("\tUUID %pUB\n", sup->uuid);
369 break;
370 }
371 case UBIFS_MST_NODE:
372 {
373 const struct ubifs_mst_node *mst = node;
374
375 pr_err("\thighest_inum %llu\n",
376 (unsigned long long)le64_to_cpu(mst->highest_inum));
377 pr_err("\tcommit number %llu\n",
378 (unsigned long long)le64_to_cpu(mst->cmt_no));
379 pr_err("\tflags %#x\n", le32_to_cpu(mst->flags));
380 pr_err("\tlog_lnum %u\n", le32_to_cpu(mst->log_lnum));
381 pr_err("\troot_lnum %u\n", le32_to_cpu(mst->root_lnum));
382 pr_err("\troot_offs %u\n", le32_to_cpu(mst->root_offs));
383 pr_err("\troot_len %u\n", le32_to_cpu(mst->root_len));
384 pr_err("\tgc_lnum %u\n", le32_to_cpu(mst->gc_lnum));
385 pr_err("\tihead_lnum %u\n", le32_to_cpu(mst->ihead_lnum));
386 pr_err("\tihead_offs %u\n", le32_to_cpu(mst->ihead_offs));
387 pr_err("\tindex_size %llu\n",
388 (unsigned long long)le64_to_cpu(mst->index_size));
389 pr_err("\tlpt_lnum %u\n", le32_to_cpu(mst->lpt_lnum));
390 pr_err("\tlpt_offs %u\n", le32_to_cpu(mst->lpt_offs));
391 pr_err("\tnhead_lnum %u\n", le32_to_cpu(mst->nhead_lnum));
392 pr_err("\tnhead_offs %u\n", le32_to_cpu(mst->nhead_offs));
393 pr_err("\tltab_lnum %u\n", le32_to_cpu(mst->ltab_lnum));
394 pr_err("\tltab_offs %u\n", le32_to_cpu(mst->ltab_offs));
395 pr_err("\tlsave_lnum %u\n", le32_to_cpu(mst->lsave_lnum));
396 pr_err("\tlsave_offs %u\n", le32_to_cpu(mst->lsave_offs));
397 pr_err("\tlscan_lnum %u\n", le32_to_cpu(mst->lscan_lnum));
398 pr_err("\tleb_cnt %u\n", le32_to_cpu(mst->leb_cnt));
399 pr_err("\tempty_lebs %u\n", le32_to_cpu(mst->empty_lebs));
400 pr_err("\tidx_lebs %u\n", le32_to_cpu(mst->idx_lebs));
401 pr_err("\ttotal_free %llu\n",
402 (unsigned long long)le64_to_cpu(mst->total_free));
403 pr_err("\ttotal_dirty %llu\n",
404 (unsigned long long)le64_to_cpu(mst->total_dirty));
405 pr_err("\ttotal_used %llu\n",
406 (unsigned long long)le64_to_cpu(mst->total_used));
407 pr_err("\ttotal_dead %llu\n",
408 (unsigned long long)le64_to_cpu(mst->total_dead));
409 pr_err("\ttotal_dark %llu\n",
410 (unsigned long long)le64_to_cpu(mst->total_dark));
411 break;
412 }
413 case UBIFS_REF_NODE:
414 {
415 const struct ubifs_ref_node *ref = node;
416
417 pr_err("\tlnum %u\n", le32_to_cpu(ref->lnum));
418 pr_err("\toffs %u\n", le32_to_cpu(ref->offs));
419 pr_err("\tjhead %u\n", le32_to_cpu(ref->jhead));
420 break;
421 }
422 case UBIFS_INO_NODE:
423 {
424 const struct ubifs_ino_node *ino = node;
425
426 key_read(c, &ino->key, &key);
427 pr_err("\tkey %s\n",
428 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
429 pr_err("\tcreat_sqnum %llu\n",
430 (unsigned long long)le64_to_cpu(ino->creat_sqnum));
431 pr_err("\tsize %llu\n",
432 (unsigned long long)le64_to_cpu(ino->size));
433 pr_err("\tnlink %u\n", le32_to_cpu(ino->nlink));
434 pr_err("\tatime %lld.%u\n",
435 (long long)le64_to_cpu(ino->atime_sec),
436 le32_to_cpu(ino->atime_nsec));
437 pr_err("\tmtime %lld.%u\n",
438 (long long)le64_to_cpu(ino->mtime_sec),
439 le32_to_cpu(ino->mtime_nsec));
440 pr_err("\tctime %lld.%u\n",
441 (long long)le64_to_cpu(ino->ctime_sec),
442 le32_to_cpu(ino->ctime_nsec));
443 pr_err("\tuid %u\n", le32_to_cpu(ino->uid));
444 pr_err("\tgid %u\n", le32_to_cpu(ino->gid));
445 pr_err("\tmode %u\n", le32_to_cpu(ino->mode));
446 pr_err("\tflags %#x\n", le32_to_cpu(ino->flags));
447 pr_err("\txattr_cnt %u\n", le32_to_cpu(ino->xattr_cnt));
448 pr_err("\txattr_size %u\n", le32_to_cpu(ino->xattr_size));
449 pr_err("\txattr_names %u\n", le32_to_cpu(ino->xattr_names));
450 pr_err("\tcompr_type %#x\n",
451 (int)le16_to_cpu(ino->compr_type));
452 pr_err("\tdata len %u\n", le32_to_cpu(ino->data_len));
453 break;
454 }
455 case UBIFS_DENT_NODE:
456 case UBIFS_XENT_NODE:
457 {
458 const struct ubifs_dent_node *dent = node;
459 int nlen = le16_to_cpu(dent->nlen);
460
461 key_read(c, &dent->key, &key);
462 pr_err("\tkey %s\n",
463 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
464 pr_err("\tinum %llu\n",
465 (unsigned long long)le64_to_cpu(dent->inum));
466 pr_err("\ttype %d\n", (int)dent->type);
467 pr_err("\tnlen %d\n", nlen);
468 pr_err("\tname ");
469
470 if (nlen > UBIFS_MAX_NLEN)
471 pr_err("(bad name length, not printing, bad or corrupted node)");
472 else {
473 for (i = 0; i < nlen && dent->name[i]; i++)
474 pr_cont("%c", dent->name[i]);
475 }
476 pr_cont("\n");
477
478 break;
479 }
480 case UBIFS_DATA_NODE:
481 {
482 const struct ubifs_data_node *dn = node;
483 int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
484
485 key_read(c, &dn->key, &key);
486 pr_err("\tkey %s\n",
487 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
488 pr_err("\tsize %u\n", le32_to_cpu(dn->size));
489 pr_err("\tcompr_typ %d\n",
490 (int)le16_to_cpu(dn->compr_type));
491 pr_err("\tdata size %d\n", dlen);
492 pr_err("\tdata:\n");
Alexey Brodkin2d2fa492018-06-05 17:17:57 +0300493 print_hex_dump("\t", DUMP_PREFIX_OFFSET, 32, 1,
Heiko Schocherf5895d12014-06-24 10:10:04 +0200494 (void *)&dn->data, dlen, 0);
495 break;
496 }
497 case UBIFS_TRUN_NODE:
498 {
499 const struct ubifs_trun_node *trun = node;
500
501 pr_err("\tinum %u\n", le32_to_cpu(trun->inum));
502 pr_err("\told_size %llu\n",
503 (unsigned long long)le64_to_cpu(trun->old_size));
504 pr_err("\tnew_size %llu\n",
505 (unsigned long long)le64_to_cpu(trun->new_size));
506 break;
507 }
508 case UBIFS_IDX_NODE:
509 {
510 const struct ubifs_idx_node *idx = node;
511
512 n = le16_to_cpu(idx->child_cnt);
513 pr_err("\tchild_cnt %d\n", n);
514 pr_err("\tlevel %d\n", (int)le16_to_cpu(idx->level));
515 pr_err("\tBranches:\n");
516
517 for (i = 0; i < n && i < c->fanout - 1; i++) {
518 const struct ubifs_branch *br;
519
520 br = ubifs_idx_branch(c, idx, i);
521 key_read(c, &br->key, &key);
522 pr_err("\t%d: LEB %d:%d len %d key %s\n",
523 i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
524 le32_to_cpu(br->len),
525 dbg_snprintf_key(c, &key, key_buf,
526 DBG_KEY_BUF_LEN));
527 }
528 break;
529 }
530 case UBIFS_CS_NODE:
531 break;
532 case UBIFS_ORPH_NODE:
533 {
534 const struct ubifs_orph_node *orph = node;
535
536 pr_err("\tcommit number %llu\n",
537 (unsigned long long)
538 le64_to_cpu(orph->cmt_no) & LLONG_MAX);
539 pr_err("\tlast node flag %llu\n",
540 (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
541 n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
542 pr_err("\t%d orphan inode numbers:\n", n);
543 for (i = 0; i < n; i++)
544 pr_err("\t ino %llu\n",
545 (unsigned long long)le64_to_cpu(orph->inos[i]));
546 break;
547 }
548 default:
549 pr_err("node type %d was not recognized\n",
550 (int)ch->node_type);
551 }
552 spin_unlock(&dbg_lock);
Pali Rohár92722062022-08-07 21:27:09 +0200553#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +0200554}
555
556void ubifs_dump_budget_req(const struct ubifs_budget_req *req)
557{
Pali Rohár92722062022-08-07 21:27:09 +0200558#ifndef CONFIG_UBIFS_SILENCE_DEBUG_DUMP
Heiko Schocherf5895d12014-06-24 10:10:04 +0200559 spin_lock(&dbg_lock);
560 pr_err("Budgeting request: new_ino %d, dirtied_ino %d\n",
561 req->new_ino, req->dirtied_ino);
562 pr_err("\tnew_ino_d %d, dirtied_ino_d %d\n",
563 req->new_ino_d, req->dirtied_ino_d);
564 pr_err("\tnew_page %d, dirtied_page %d\n",
565 req->new_page, req->dirtied_page);
566 pr_err("\tnew_dent %d, mod_dent %d\n",
567 req->new_dent, req->mod_dent);
568 pr_err("\tidx_growth %d\n", req->idx_growth);
569 pr_err("\tdata_growth %d dd_growth %d\n",
570 req->data_growth, req->dd_growth);
571 spin_unlock(&dbg_lock);
Pali Rohár92722062022-08-07 21:27:09 +0200572#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +0200573}
574
575void ubifs_dump_lstats(const struct ubifs_lp_stats *lst)
576{
Pali Rohár92722062022-08-07 21:27:09 +0200577#ifndef CONFIG_UBIFS_SILENCE_DEBUG_DUMP
Heiko Schocherf5895d12014-06-24 10:10:04 +0200578 spin_lock(&dbg_lock);
579 pr_err("(pid %d) Lprops statistics: empty_lebs %d, idx_lebs %d\n",
580 current->pid, lst->empty_lebs, lst->idx_lebs);
581 pr_err("\ttaken_empty_lebs %d, total_free %lld, total_dirty %lld\n",
582 lst->taken_empty_lebs, lst->total_free, lst->total_dirty);
583 pr_err("\ttotal_used %lld, total_dark %lld, total_dead %lld\n",
584 lst->total_used, lst->total_dark, lst->total_dead);
585 spin_unlock(&dbg_lock);
Pali Rohár92722062022-08-07 21:27:09 +0200586#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +0200587}
588
589#ifndef __UBOOT__
590void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)
591{
592 int i;
593 struct rb_node *rb;
594 struct ubifs_bud *bud;
595 struct ubifs_gced_idx_leb *idx_gc;
596 long long available, outstanding, free;
597
598 spin_lock(&c->space_lock);
599 spin_lock(&dbg_lock);
600 pr_err("(pid %d) Budgeting info: data budget sum %lld, total budget sum %lld\n",
601 current->pid, bi->data_growth + bi->dd_growth,
602 bi->data_growth + bi->dd_growth + bi->idx_growth);
603 pr_err("\tbudg_data_growth %lld, budg_dd_growth %lld, budg_idx_growth %lld\n",
604 bi->data_growth, bi->dd_growth, bi->idx_growth);
605 pr_err("\tmin_idx_lebs %d, old_idx_sz %llu, uncommitted_idx %lld\n",
606 bi->min_idx_lebs, bi->old_idx_sz, bi->uncommitted_idx);
607 pr_err("\tpage_budget %d, inode_budget %d, dent_budget %d\n",
608 bi->page_budget, bi->inode_budget, bi->dent_budget);
609 pr_err("\tnospace %u, nospace_rp %u\n", bi->nospace, bi->nospace_rp);
610 pr_err("\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
611 c->dark_wm, c->dead_wm, c->max_idx_node_sz);
612
613 if (bi != &c->bi)
614 /*
615 * If we are dumping saved budgeting data, do not print
616 * additional information which is about the current state, not
617 * the old one which corresponded to the saved budgeting data.
618 */
619 goto out_unlock;
620
621 pr_err("\tfreeable_cnt %d, calc_idx_sz %lld, idx_gc_cnt %d\n",
622 c->freeable_cnt, c->calc_idx_sz, c->idx_gc_cnt);
623 pr_err("\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, clean_zn_cnt %ld\n",
624 atomic_long_read(&c->dirty_pg_cnt),
625 atomic_long_read(&c->dirty_zn_cnt),
626 atomic_long_read(&c->clean_zn_cnt));
627 pr_err("\tgc_lnum %d, ihead_lnum %d\n", c->gc_lnum, c->ihead_lnum);
628
629 /* If we are in R/O mode, journal heads do not exist */
630 if (c->jheads)
631 for (i = 0; i < c->jhead_cnt; i++)
632 pr_err("\tjhead %s\t LEB %d\n",
633 dbg_jhead(c->jheads[i].wbuf.jhead),
634 c->jheads[i].wbuf.lnum);
635 for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
636 bud = rb_entry(rb, struct ubifs_bud, rb);
637 pr_err("\tbud LEB %d\n", bud->lnum);
638 }
639 list_for_each_entry(bud, &c->old_buds, list)
640 pr_err("\told bud LEB %d\n", bud->lnum);
641 list_for_each_entry(idx_gc, &c->idx_gc, list)
642 pr_err("\tGC'ed idx LEB %d unmap %d\n",
643 idx_gc->lnum, idx_gc->unmap);
644 pr_err("\tcommit state %d\n", c->cmt_state);
645
646 /* Print budgeting predictions */
647 available = ubifs_calc_available(c, c->bi.min_idx_lebs);
648 outstanding = c->bi.data_growth + c->bi.dd_growth;
649 free = ubifs_get_free_space_nolock(c);
650 pr_err("Budgeting predictions:\n");
651 pr_err("\tavailable: %lld, outstanding %lld, free %lld\n",
652 available, outstanding, free);
653out_unlock:
654 spin_unlock(&dbg_lock);
655 spin_unlock(&c->space_lock);
656}
657#else
658void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)
659{
660}
661#endif
662
663void ubifs_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
664{
Pali Rohár92722062022-08-07 21:27:09 +0200665#ifndef CONFIG_UBIFS_SILENCE_DEBUG_DUMP
Heiko Schocherf5895d12014-06-24 10:10:04 +0200666 int i, spc, dark = 0, dead = 0;
667 struct rb_node *rb;
668 struct ubifs_bud *bud;
669
670 spc = lp->free + lp->dirty;
671 if (spc < c->dead_wm)
672 dead = spc;
673 else
674 dark = ubifs_calc_dark(c, spc);
675
676 if (lp->flags & LPROPS_INDEX)
677 pr_err("LEB %-7d free %-8d dirty %-8d used %-8d free + dirty %-8d flags %#x (",
678 lp->lnum, lp->free, lp->dirty, c->leb_size - spc, spc,
679 lp->flags);
680 else
681 pr_err("LEB %-7d free %-8d dirty %-8d used %-8d free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d flags %#-4x (",
682 lp->lnum, lp->free, lp->dirty, c->leb_size - spc, spc,
683 dark, dead, (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags);
684
685 if (lp->flags & LPROPS_TAKEN) {
686 if (lp->flags & LPROPS_INDEX)
687 pr_cont("index, taken");
688 else
689 pr_cont("taken");
690 } else {
691 const char *s;
692
693 if (lp->flags & LPROPS_INDEX) {
694 switch (lp->flags & LPROPS_CAT_MASK) {
695 case LPROPS_DIRTY_IDX:
696 s = "dirty index";
697 break;
698 case LPROPS_FRDI_IDX:
699 s = "freeable index";
700 break;
701 default:
702 s = "index";
703 }
704 } else {
705 switch (lp->flags & LPROPS_CAT_MASK) {
706 case LPROPS_UNCAT:
707 s = "not categorized";
708 break;
709 case LPROPS_DIRTY:
710 s = "dirty";
711 break;
712 case LPROPS_FREE:
713 s = "free";
714 break;
715 case LPROPS_EMPTY:
716 s = "empty";
717 break;
718 case LPROPS_FREEABLE:
719 s = "freeable";
720 break;
721 default:
722 s = NULL;
723 break;
724 }
725 }
726 pr_cont("%s", s);
727 }
728
729 for (rb = rb_first((struct rb_root *)&c->buds); rb; rb = rb_next(rb)) {
730 bud = rb_entry(rb, struct ubifs_bud, rb);
731 if (bud->lnum == lp->lnum) {
732 int head = 0;
733 for (i = 0; i < c->jhead_cnt; i++) {
734 /*
735 * Note, if we are in R/O mode or in the middle
736 * of mounting/re-mounting, the write-buffers do
737 * not exist.
738 */
739 if (c->jheads &&
740 lp->lnum == c->jheads[i].wbuf.lnum) {
741 pr_cont(", jhead %s", dbg_jhead(i));
742 head = 1;
743 }
744 }
745 if (!head)
746 pr_cont(", bud of jhead %s",
747 dbg_jhead(bud->jhead));
748 }
749 }
750 if (lp->lnum == c->gc_lnum)
751 pr_cont(", GC LEB");
752 pr_cont(")\n");
Pali Rohár92722062022-08-07 21:27:09 +0200753#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +0200754}
755
756void ubifs_dump_lprops(struct ubifs_info *c)
757{
758 int lnum, err;
759 struct ubifs_lprops lp;
760 struct ubifs_lp_stats lst;
761
762 pr_err("(pid %d) start dumping LEB properties\n", current->pid);
763 ubifs_get_lp_stats(c, &lst);
764 ubifs_dump_lstats(&lst);
765
766 for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
767 err = ubifs_read_one_lp(c, lnum, &lp);
Heiko Schocher94b66de2015-10-22 06:19:21 +0200768 if (err) {
769 ubifs_err(c, "cannot read lprops for LEB %d", lnum);
770 continue;
771 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200772
773 ubifs_dump_lprop(c, &lp);
774 }
775 pr_err("(pid %d) finish dumping LEB properties\n", current->pid);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100776}
777
Heiko Schocherf5895d12014-06-24 10:10:04 +0200778void ubifs_dump_lpt_info(struct ubifs_info *c)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100779{
Pali Rohár92722062022-08-07 21:27:09 +0200780#ifndef CONFIG_UBIFS_SILENCE_DEBUG_DUMP
Heiko Schocherf5895d12014-06-24 10:10:04 +0200781 int i;
782
783 spin_lock(&dbg_lock);
784 pr_err("(pid %d) dumping LPT information\n", current->pid);
785 pr_err("\tlpt_sz: %lld\n", c->lpt_sz);
786 pr_err("\tpnode_sz: %d\n", c->pnode_sz);
787 pr_err("\tnnode_sz: %d\n", c->nnode_sz);
788 pr_err("\tltab_sz: %d\n", c->ltab_sz);
789 pr_err("\tlsave_sz: %d\n", c->lsave_sz);
790 pr_err("\tbig_lpt: %d\n", c->big_lpt);
791 pr_err("\tlpt_hght: %d\n", c->lpt_hght);
792 pr_err("\tpnode_cnt: %d\n", c->pnode_cnt);
793 pr_err("\tnnode_cnt: %d\n", c->nnode_cnt);
794 pr_err("\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt);
795 pr_err("\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt);
796 pr_err("\tlsave_cnt: %d\n", c->lsave_cnt);
797 pr_err("\tspace_bits: %d\n", c->space_bits);
798 pr_err("\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
799 pr_err("\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
800 pr_err("\tlpt_spc_bits: %d\n", c->lpt_spc_bits);
801 pr_err("\tpcnt_bits: %d\n", c->pcnt_bits);
802 pr_err("\tlnum_bits: %d\n", c->lnum_bits);
803 pr_err("\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
804 pr_err("\tLPT head is at %d:%d\n",
805 c->nhead_lnum, c->nhead_offs);
806 pr_err("\tLPT ltab is at %d:%d\n", c->ltab_lnum, c->ltab_offs);
807 if (c->big_lpt)
808 pr_err("\tLPT lsave is at %d:%d\n",
809 c->lsave_lnum, c->lsave_offs);
810 for (i = 0; i < c->lpt_lebs; i++)
811 pr_err("\tLPT LEB %d free %d dirty %d tgc %d cmt %d\n",
812 i + c->lpt_first, c->ltab[i].free, c->ltab[i].dirty,
813 c->ltab[i].tgc, c->ltab[i].cmt);
814 spin_unlock(&dbg_lock);
Pali Rohár92722062022-08-07 21:27:09 +0200815#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +0200816}
817
818void ubifs_dump_sleb(const struct ubifs_info *c,
819 const struct ubifs_scan_leb *sleb, int offs)
820{
Pali Rohár92722062022-08-07 21:27:09 +0200821#ifndef CONFIG_UBIFS_SILENCE_DEBUG_DUMP
Heiko Schocherf5895d12014-06-24 10:10:04 +0200822 struct ubifs_scan_node *snod;
823
824 pr_err("(pid %d) start dumping scanned data from LEB %d:%d\n",
825 current->pid, sleb->lnum, offs);
826
827 list_for_each_entry(snod, &sleb->nodes, list) {
828 cond_resched();
829 pr_err("Dumping node at LEB %d:%d len %d\n",
830 sleb->lnum, snod->offs, snod->len);
831 ubifs_dump_node(c, snod->node);
832 }
Pali Rohár92722062022-08-07 21:27:09 +0200833#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +0200834}
835
836void ubifs_dump_leb(const struct ubifs_info *c, int lnum)
837{
Pali Rohár92722062022-08-07 21:27:09 +0200838#ifndef CONFIG_UBIFS_SILENCE_DEBUG_DUMP
Heiko Schocherf5895d12014-06-24 10:10:04 +0200839 struct ubifs_scan_leb *sleb;
840 struct ubifs_scan_node *snod;
841 void *buf;
842
843 pr_err("(pid %d) start dumping LEB %d\n", current->pid, lnum);
844
845 buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
846 if (!buf) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200847 ubifs_err(c, "cannot allocate memory for dumping LEB %d", lnum);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200848 return;
849 }
850
851 sleb = ubifs_scan(c, lnum, 0, buf, 0);
852 if (IS_ERR(sleb)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200853 ubifs_err(c, "scan error %d", (int)PTR_ERR(sleb));
Heiko Schocherf5895d12014-06-24 10:10:04 +0200854 goto out;
855 }
856
857 pr_err("LEB %d has %d nodes ending at %d\n", lnum,
858 sleb->nodes_cnt, sleb->endpt);
859
860 list_for_each_entry(snod, &sleb->nodes, list) {
861 cond_resched();
862 pr_err("Dumping node at LEB %d:%d len %d\n", lnum,
863 snod->offs, snod->len);
864 ubifs_dump_node(c, snod->node);
865 }
866
867 pr_err("(pid %d) finish dumping LEB %d\n", current->pid, lnum);
868 ubifs_scan_destroy(sleb);
869
870out:
871 vfree(buf);
872 return;
Pali Rohár92722062022-08-07 21:27:09 +0200873#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +0200874}
875
876void ubifs_dump_znode(const struct ubifs_info *c,
877 const struct ubifs_znode *znode)
878{
Pali Rohár92722062022-08-07 21:27:09 +0200879#ifndef CONFIG_UBIFS_SILENCE_DEBUG_DUMP
Heiko Schocherf5895d12014-06-24 10:10:04 +0200880 int n;
881 const struct ubifs_zbranch *zbr;
882 char key_buf[DBG_KEY_BUF_LEN];
883
884 spin_lock(&dbg_lock);
885 if (znode->parent)
886 zbr = &znode->parent->zbranch[znode->iip];
887 else
888 zbr = &c->zroot;
889
890 pr_err("znode %p, LEB %d:%d len %d parent %p iip %d level %d child_cnt %d flags %lx\n",
891 znode, zbr->lnum, zbr->offs, zbr->len, znode->parent, znode->iip,
892 znode->level, znode->child_cnt, znode->flags);
893
894 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
895 spin_unlock(&dbg_lock);
896 return;
897 }
898
899 pr_err("zbranches:\n");
900 for (n = 0; n < znode->child_cnt; n++) {
901 zbr = &znode->zbranch[n];
902 if (znode->level > 0)
903 pr_err("\t%d: znode %p LEB %d:%d len %d key %s\n",
904 n, zbr->znode, zbr->lnum, zbr->offs, zbr->len,
905 dbg_snprintf_key(c, &zbr->key, key_buf,
906 DBG_KEY_BUF_LEN));
907 else
908 pr_err("\t%d: LNC %p LEB %d:%d len %d key %s\n",
909 n, zbr->znode, zbr->lnum, zbr->offs, zbr->len,
910 dbg_snprintf_key(c, &zbr->key, key_buf,
911 DBG_KEY_BUF_LEN));
912 }
913 spin_unlock(&dbg_lock);
Pali Rohár92722062022-08-07 21:27:09 +0200914#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +0200915}
916
917void ubifs_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
918{
Pali Rohár92722062022-08-07 21:27:09 +0200919#ifndef CONFIG_UBIFS_SILENCE_DEBUG_DUMP
Heiko Schocherf5895d12014-06-24 10:10:04 +0200920 int i;
921
922 pr_err("(pid %d) start dumping heap cat %d (%d elements)\n",
923 current->pid, cat, heap->cnt);
924 for (i = 0; i < heap->cnt; i++) {
925 struct ubifs_lprops *lprops = heap->arr[i];
926
927 pr_err("\t%d. LEB %d hpos %d free %d dirty %d flags %d\n",
928 i, lprops->lnum, lprops->hpos, lprops->free,
929 lprops->dirty, lprops->flags);
930 }
931 pr_err("(pid %d) finish dumping heap\n", current->pid);
Pali Rohár92722062022-08-07 21:27:09 +0200932#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +0200933}
934
935void ubifs_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
936 struct ubifs_nnode *parent, int iip)
937{
Pali Rohár92722062022-08-07 21:27:09 +0200938#ifndef CONFIG_UBIFS_SILENCE_DEBUG_DUMP
Heiko Schocherf5895d12014-06-24 10:10:04 +0200939 int i;
940
941 pr_err("(pid %d) dumping pnode:\n", current->pid);
942 pr_err("\taddress %zx parent %zx cnext %zx\n",
943 (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
944 pr_err("\tflags %lu iip %d level %d num %d\n",
945 pnode->flags, iip, pnode->level, pnode->num);
946 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
947 struct ubifs_lprops *lp = &pnode->lprops[i];
948
949 pr_err("\t%d: free %d dirty %d flags %d lnum %d\n",
950 i, lp->free, lp->dirty, lp->flags, lp->lnum);
951 }
Pali Rohár92722062022-08-07 21:27:09 +0200952#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +0200953}
954
955void ubifs_dump_tnc(struct ubifs_info *c)
956{
Pali Rohár92722062022-08-07 21:27:09 +0200957#ifndef CONFIG_UBIFS_SILENCE_DEBUG_DUMP
Heiko Schocherf5895d12014-06-24 10:10:04 +0200958 struct ubifs_znode *znode;
959 int level;
960
961 pr_err("\n");
962 pr_err("(pid %d) start dumping TNC tree\n", current->pid);
963 znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
964 level = znode->level;
965 pr_err("== Level %d ==\n", level);
966 while (znode) {
967 if (level != znode->level) {
968 level = znode->level;
969 pr_err("== Level %d ==\n", level);
970 }
971 ubifs_dump_znode(c, znode);
972 znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
973 }
974 pr_err("(pid %d) finish dumping TNC tree\n", current->pid);
Pali Rohár92722062022-08-07 21:27:09 +0200975#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +0100976}
977
Pali Rohár92722062022-08-07 21:27:09 +0200978#ifndef CONFIG_UBIFS_SILENCE_DEBUG_DUMP
Heiko Schocherf5895d12014-06-24 10:10:04 +0200979static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
980 void *priv)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100981{
Heiko Schocherf5895d12014-06-24 10:10:04 +0200982 ubifs_dump_znode(c, znode);
983 return 0;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100984}
Pali Rohár92722062022-08-07 21:27:09 +0200985#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +0100986
987/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200988 * ubifs_dump_index - dump the on-flash index.
Stefan Roese2fc10f62009-03-19 15:35:05 +0100989 * @c: UBIFS file-system description object
990 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200991 * This function dumps whole UBIFS indexing B-tree, unlike 'ubifs_dump_tnc()'
992 * which dumps only in-memory znodes and does not read znodes which from flash.
Stefan Roese2fc10f62009-03-19 15:35:05 +0100993 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200994void ubifs_dump_index(struct ubifs_info *c)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100995{
Pali Rohár92722062022-08-07 21:27:09 +0200996#ifndef CONFIG_UBIFS_SILENCE_DEBUG_DUMP
Heiko Schocherf5895d12014-06-24 10:10:04 +0200997 dbg_walk_index(c, NULL, dump_znode, NULL);
Pali Rohár92722062022-08-07 21:27:09 +0200998#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +0200999}
1000
1001#ifndef __UBOOT__
1002/**
1003 * dbg_save_space_info - save information about flash space.
1004 * @c: UBIFS file-system description object
1005 *
1006 * This function saves information about UBIFS free space, dirty space, etc, in
1007 * order to check it later.
1008 */
1009void dbg_save_space_info(struct ubifs_info *c)
1010{
1011 struct ubifs_debug_info *d = c->dbg;
1012 int freeable_cnt;
1013
1014 spin_lock(&c->space_lock);
1015 memcpy(&d->saved_lst, &c->lst, sizeof(struct ubifs_lp_stats));
1016 memcpy(&d->saved_bi, &c->bi, sizeof(struct ubifs_budg_info));
1017 d->saved_idx_gc_cnt = c->idx_gc_cnt;
1018
1019 /*
1020 * We use a dirty hack here and zero out @c->freeable_cnt, because it
1021 * affects the free space calculations, and UBIFS might not know about
1022 * all freeable eraseblocks. Indeed, we know about freeable eraseblocks
1023 * only when we read their lprops, and we do this only lazily, upon the
1024 * need. So at any given point of time @c->freeable_cnt might be not
1025 * exactly accurate.
1026 *
1027 * Just one example about the issue we hit when we did not zero
1028 * @c->freeable_cnt.
1029 * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the
1030 * amount of free space in @d->saved_free
1031 * 2. We re-mount R/W, which makes UBIFS to read the "lsave"
1032 * information from flash, where we cache LEBs from various
1033 * categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()'
1034 * -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()'
1035 * -> 'ubifs_get_pnode()' -> 'update_cats()'
1036 * -> 'ubifs_add_to_cat()').
1037 * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt
1038 * becomes %1.
1039 * 4. We calculate the amount of free space when the re-mount is
1040 * finished in 'dbg_check_space_info()' and it does not match
1041 * @d->saved_free.
1042 */
1043 freeable_cnt = c->freeable_cnt;
1044 c->freeable_cnt = 0;
1045 d->saved_free = ubifs_get_free_space_nolock(c);
1046 c->freeable_cnt = freeable_cnt;
1047 spin_unlock(&c->space_lock);
1048}
1049
1050/**
1051 * dbg_check_space_info - check flash space information.
1052 * @c: UBIFS file-system description object
1053 *
1054 * This function compares current flash space information with the information
1055 * which was saved when the 'dbg_save_space_info()' function was called.
1056 * Returns zero if the information has not changed, and %-EINVAL it it has
1057 * changed.
1058 */
1059int dbg_check_space_info(struct ubifs_info *c)
1060{
1061 struct ubifs_debug_info *d = c->dbg;
1062 struct ubifs_lp_stats lst;
1063 long long free;
1064 int freeable_cnt;
Stefan Roese2fc10f62009-03-19 15:35:05 +01001065
Heiko Schocherf5895d12014-06-24 10:10:04 +02001066 spin_lock(&c->space_lock);
1067 freeable_cnt = c->freeable_cnt;
1068 c->freeable_cnt = 0;
1069 free = ubifs_get_free_space_nolock(c);
1070 c->freeable_cnt = freeable_cnt;
1071 spin_unlock(&c->space_lock);
1072
1073 if (free != d->saved_free) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001074 ubifs_err(c, "free space changed from %lld to %lld",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001075 d->saved_free, free);
Stefan Roese2fc10f62009-03-19 15:35:05 +01001076 goto out;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001077 }
Stefan Roese2fc10f62009-03-19 15:35:05 +01001078
1079 return 0;
1080
1081out:
Heiko Schocher94b66de2015-10-22 06:19:21 +02001082 ubifs_msg(c, "saved lprops statistics dump");
Heiko Schocherf5895d12014-06-24 10:10:04 +02001083 ubifs_dump_lstats(&d->saved_lst);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001084 ubifs_msg(c, "saved budgeting info dump");
Heiko Schocherf5895d12014-06-24 10:10:04 +02001085 ubifs_dump_budg(c, &d->saved_bi);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001086 ubifs_msg(c, "saved idx_gc_cnt %d", d->saved_idx_gc_cnt);
1087 ubifs_msg(c, "current lprops statistics dump");
Heiko Schocherf5895d12014-06-24 10:10:04 +02001088 ubifs_get_lp_stats(c, &lst);
1089 ubifs_dump_lstats(&lst);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001090 ubifs_msg(c, "current budgeting info dump");
Heiko Schocherf5895d12014-06-24 10:10:04 +02001091 ubifs_dump_budg(c, &c->bi);
1092 dump_stack();
1093 return -EINVAL;
Stefan Roese2fc10f62009-03-19 15:35:05 +01001094}
1095
1096/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001097 * dbg_check_synced_i_size - check synchronized inode size.
Stefan Roese2fc10f62009-03-19 15:35:05 +01001098 * @c: UBIFS file-system description object
Heiko Schocherf5895d12014-06-24 10:10:04 +02001099 * @inode: inode to check
1100 *
1101 * If inode is clean, synchronized inode size has to be equivalent to current
1102 * inode size. This function has to be called only for locked inodes (@i_mutex
1103 * has to be locked). Returns %0 if synchronized inode size if correct, and
1104 * %-EINVAL if not.
Stefan Roese2fc10f62009-03-19 15:35:05 +01001105 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02001106int dbg_check_synced_i_size(const struct ubifs_info *c, struct inode *inode)
Stefan Roese2fc10f62009-03-19 15:35:05 +01001107{
Heiko Schocherf5895d12014-06-24 10:10:04 +02001108 int err = 0;
1109 struct ubifs_inode *ui = ubifs_inode(inode);
1110
1111 if (!dbg_is_chk_gen(c))
1112 return 0;
1113 if (!S_ISREG(inode->i_mode))
1114 return 0;
1115
1116 mutex_lock(&ui->ui_mutex);
1117 spin_lock(&ui->ui_lock);
1118 if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001119 ubifs_err(c, "ui_size is %lld, synced_i_size is %lld, but inode is clean",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001120 ui->ui_size, ui->synced_i_size);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001121 ubifs_err(c, "i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
Heiko Schocherf5895d12014-06-24 10:10:04 +02001122 inode->i_mode, i_size_read(inode));
1123 dump_stack();
1124 err = -EINVAL;
1125 }
1126 spin_unlock(&ui->ui_lock);
1127 mutex_unlock(&ui->ui_mutex);
1128 return err;
1129}
1130
1131/*
1132 * dbg_check_dir - check directory inode size and link count.
1133 * @c: UBIFS file-system description object
1134 * @dir: the directory to calculate size for
1135 * @size: the result is returned here
1136 *
1137 * This function makes sure that directory size and link count are correct.
1138 * Returns zero in case of success and a negative error code in case of
1139 * failure.
1140 *
1141 * Note, it is good idea to make sure the @dir->i_mutex is locked before
1142 * calling this function.
1143 */
1144int dbg_check_dir(struct ubifs_info *c, const struct inode *dir)
1145{
1146 unsigned int nlink = 2;
1147 union ubifs_key key;
1148 struct ubifs_dent_node *dent, *pdent = NULL;
1149 struct qstr nm = { .name = NULL };
1150 loff_t size = UBIFS_INO_NODE_SZ;
1151
1152 if (!dbg_is_chk_gen(c))
1153 return 0;
1154
1155 if (!S_ISDIR(dir->i_mode))
1156 return 0;
1157
1158 lowest_dent_key(c, &key, dir->i_ino);
1159 while (1) {
1160 int err;
1161
1162 dent = ubifs_tnc_next_ent(c, &key, &nm);
1163 if (IS_ERR(dent)) {
1164 err = PTR_ERR(dent);
1165 if (err == -ENOENT)
1166 break;
1167 return err;
1168 }
1169
1170 nm.name = dent->name;
1171 nm.len = le16_to_cpu(dent->nlen);
1172 size += CALC_DENT_SIZE(nm.len);
1173 if (dent->type == UBIFS_ITYPE_DIR)
1174 nlink += 1;
1175 kfree(pdent);
1176 pdent = dent;
1177 key_read(c, &dent->key, &key);
1178 }
1179 kfree(pdent);
1180
1181 if (i_size_read(dir) != size) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001182 ubifs_err(c, "directory inode %lu has size %llu, but calculated size is %llu",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001183 dir->i_ino, (unsigned long long)i_size_read(dir),
1184 (unsigned long long)size);
1185 ubifs_dump_inode(c, dir);
1186 dump_stack();
1187 return -EINVAL;
1188 }
1189 if (dir->i_nlink != nlink) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001190 ubifs_err(c, "directory inode %lu has nlink %u, but calculated nlink is %u",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001191 dir->i_ino, dir->i_nlink, nlink);
1192 ubifs_dump_inode(c, dir);
1193 dump_stack();
1194 return -EINVAL;
1195 }
1196
1197 return 0;
1198}
1199
1200/**
1201 * dbg_check_key_order - make sure that colliding keys are properly ordered.
1202 * @c: UBIFS file-system description object
1203 * @zbr1: first zbranch
1204 * @zbr2: following zbranch
1205 *
1206 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
1207 * names of the direntries/xentries which are referred by the keys. This
1208 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
1209 * sure the name of direntry/xentry referred by @zbr1 is less than
1210 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
1211 * and a negative error code in case of failure.
1212 */
1213static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
1214 struct ubifs_zbranch *zbr2)
1215{
1216 int err, nlen1, nlen2, cmp;
1217 struct ubifs_dent_node *dent1, *dent2;
1218 union ubifs_key key;
1219 char key_buf[DBG_KEY_BUF_LEN];
1220
1221 ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
1222 dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1223 if (!dent1)
1224 return -ENOMEM;
1225 dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1226 if (!dent2) {
1227 err = -ENOMEM;
1228 goto out_free;
1229 }
1230
1231 err = ubifs_tnc_read_node(c, zbr1, dent1);
1232 if (err)
1233 goto out_free;
1234 err = ubifs_validate_entry(c, dent1);
1235 if (err)
1236 goto out_free;
1237
1238 err = ubifs_tnc_read_node(c, zbr2, dent2);
1239 if (err)
1240 goto out_free;
1241 err = ubifs_validate_entry(c, dent2);
1242 if (err)
1243 goto out_free;
1244
1245 /* Make sure node keys are the same as in zbranch */
1246 err = 1;
1247 key_read(c, &dent1->key, &key);
1248 if (keys_cmp(c, &zbr1->key, &key)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001249 ubifs_err(c, "1st entry at %d:%d has key %s", zbr1->lnum,
Heiko Schocherf5895d12014-06-24 10:10:04 +02001250 zbr1->offs, dbg_snprintf_key(c, &key, key_buf,
1251 DBG_KEY_BUF_LEN));
Heiko Schocher94b66de2015-10-22 06:19:21 +02001252 ubifs_err(c, "but it should have key %s according to tnc",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001253 dbg_snprintf_key(c, &zbr1->key, key_buf,
1254 DBG_KEY_BUF_LEN));
1255 ubifs_dump_node(c, dent1);
1256 goto out_free;
1257 }
1258
1259 key_read(c, &dent2->key, &key);
1260 if (keys_cmp(c, &zbr2->key, &key)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001261 ubifs_err(c, "2nd entry at %d:%d has key %s", zbr1->lnum,
Heiko Schocherf5895d12014-06-24 10:10:04 +02001262 zbr1->offs, dbg_snprintf_key(c, &key, key_buf,
1263 DBG_KEY_BUF_LEN));
Heiko Schocher94b66de2015-10-22 06:19:21 +02001264 ubifs_err(c, "but it should have key %s according to tnc",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001265 dbg_snprintf_key(c, &zbr2->key, key_buf,
1266 DBG_KEY_BUF_LEN));
1267 ubifs_dump_node(c, dent2);
1268 goto out_free;
1269 }
1270
1271 nlen1 = le16_to_cpu(dent1->nlen);
1272 nlen2 = le16_to_cpu(dent2->nlen);
1273
1274 cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
1275 if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
1276 err = 0;
1277 goto out_free;
1278 }
1279 if (cmp == 0 && nlen1 == nlen2)
Heiko Schocher94b66de2015-10-22 06:19:21 +02001280 ubifs_err(c, "2 xent/dent nodes with the same name");
Heiko Schocherf5895d12014-06-24 10:10:04 +02001281 else
Heiko Schocher94b66de2015-10-22 06:19:21 +02001282 ubifs_err(c, "bad order of colliding key %s",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001283 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
1284
Heiko Schocher94b66de2015-10-22 06:19:21 +02001285 ubifs_msg(c, "first node at %d:%d\n", zbr1->lnum, zbr1->offs);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001286 ubifs_dump_node(c, dent1);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001287 ubifs_msg(c, "second node at %d:%d\n", zbr2->lnum, zbr2->offs);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001288 ubifs_dump_node(c, dent2);
1289
1290out_free:
1291 kfree(dent2);
1292 kfree(dent1);
1293 return err;
Stefan Roese2fc10f62009-03-19 15:35:05 +01001294}
1295
Heiko Schocherf5895d12014-06-24 10:10:04 +02001296/**
1297 * dbg_check_znode - check if znode is all right.
1298 * @c: UBIFS file-system description object
1299 * @zbr: zbranch which points to this znode
1300 *
1301 * This function makes sure that znode referred to by @zbr is all right.
1302 * Returns zero if it is, and %-EINVAL if it is not.
1303 */
1304static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
1305{
1306 struct ubifs_znode *znode = zbr->znode;
1307 struct ubifs_znode *zp = znode->parent;
1308 int n, err, cmp;
1309
1310 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
1311 err = 1;
1312 goto out;
1313 }
1314 if (znode->level < 0) {
1315 err = 2;
1316 goto out;
1317 }
1318 if (znode->iip < 0 || znode->iip >= c->fanout) {
1319 err = 3;
1320 goto out;
1321 }
1322
1323 if (zbr->len == 0)
1324 /* Only dirty zbranch may have no on-flash nodes */
1325 if (!ubifs_zn_dirty(znode)) {
1326 err = 4;
1327 goto out;
1328 }
1329
1330 if (ubifs_zn_dirty(znode)) {
1331 /*
1332 * If znode is dirty, its parent has to be dirty as well. The
1333 * order of the operation is important, so we have to have
1334 * memory barriers.
1335 */
1336 smp_mb();
1337 if (zp && !ubifs_zn_dirty(zp)) {
1338 /*
1339 * The dirty flag is atomic and is cleared outside the
1340 * TNC mutex, so znode's dirty flag may now have
1341 * been cleared. The child is always cleared before the
1342 * parent, so we just need to check again.
1343 */
1344 smp_mb();
1345 if (ubifs_zn_dirty(znode)) {
1346 err = 5;
1347 goto out;
1348 }
1349 }
1350 }
1351
1352 if (zp) {
1353 const union ubifs_key *min, *max;
1354
1355 if (znode->level != zp->level - 1) {
1356 err = 6;
1357 goto out;
1358 }
1359
1360 /* Make sure the 'parent' pointer in our znode is correct */
1361 err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
1362 if (!err) {
1363 /* This zbranch does not exist in the parent */
1364 err = 7;
1365 goto out;
1366 }
1367
1368 if (znode->iip >= zp->child_cnt) {
1369 err = 8;
1370 goto out;
1371 }
1372
1373 if (znode->iip != n) {
1374 /* This may happen only in case of collisions */
1375 if (keys_cmp(c, &zp->zbranch[n].key,
1376 &zp->zbranch[znode->iip].key)) {
1377 err = 9;
1378 goto out;
1379 }
1380 n = znode->iip;
1381 }
1382
1383 /*
1384 * Make sure that the first key in our znode is greater than or
1385 * equal to the key in the pointing zbranch.
1386 */
1387 min = &zbr->key;
1388 cmp = keys_cmp(c, min, &znode->zbranch[0].key);
1389 if (cmp == 1) {
1390 err = 10;
1391 goto out;
1392 }
1393
1394 if (n + 1 < zp->child_cnt) {
1395 max = &zp->zbranch[n + 1].key;
1396
1397 /*
1398 * Make sure the last key in our znode is less or
1399 * equivalent than the key in the zbranch which goes
1400 * after our pointing zbranch.
1401 */
1402 cmp = keys_cmp(c, max,
1403 &znode->zbranch[znode->child_cnt - 1].key);
1404 if (cmp == -1) {
1405 err = 11;
1406 goto out;
1407 }
1408 }
1409 } else {
1410 /* This may only be root znode */
1411 if (zbr != &c->zroot) {
1412 err = 12;
1413 goto out;
1414 }
1415 }
1416
1417 /*
1418 * Make sure that next key is greater or equivalent then the previous
1419 * one.
1420 */
1421 for (n = 1; n < znode->child_cnt; n++) {
1422 cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
1423 &znode->zbranch[n].key);
1424 if (cmp > 0) {
1425 err = 13;
1426 goto out;
1427 }
1428 if (cmp == 0) {
1429 /* This can only be keys with colliding hash */
1430 if (!is_hash_key(c, &znode->zbranch[n].key)) {
1431 err = 14;
1432 goto out;
1433 }
1434
1435 if (znode->level != 0 || c->replaying)
1436 continue;
1437
1438 /*
1439 * Colliding keys should follow binary order of
1440 * corresponding xentry/dentry names.
1441 */
1442 err = dbg_check_key_order(c, &znode->zbranch[n - 1],
1443 &znode->zbranch[n]);
1444 if (err < 0)
1445 return err;
1446 if (err) {
1447 err = 15;
1448 goto out;
1449 }
1450 }
1451 }
1452
1453 for (n = 0; n < znode->child_cnt; n++) {
1454 if (!znode->zbranch[n].znode &&
1455 (znode->zbranch[n].lnum == 0 ||
1456 znode->zbranch[n].len == 0)) {
1457 err = 16;
1458 goto out;
1459 }
1460
1461 if (znode->zbranch[n].lnum != 0 &&
1462 znode->zbranch[n].len == 0) {
1463 err = 17;
1464 goto out;
1465 }
1466
1467 if (znode->zbranch[n].lnum == 0 &&
1468 znode->zbranch[n].len != 0) {
1469 err = 18;
1470 goto out;
1471 }
1472
1473 if (znode->zbranch[n].lnum == 0 &&
1474 znode->zbranch[n].offs != 0) {
1475 err = 19;
1476 goto out;
1477 }
1478
1479 if (znode->level != 0 && znode->zbranch[n].znode)
1480 if (znode->zbranch[n].znode->parent != znode) {
1481 err = 20;
1482 goto out;
1483 }
1484 }
1485
1486 return 0;
1487
1488out:
Heiko Schocher94b66de2015-10-22 06:19:21 +02001489 ubifs_err(c, "failed, error %d", err);
1490 ubifs_msg(c, "dump of the znode");
Heiko Schocherf5895d12014-06-24 10:10:04 +02001491 ubifs_dump_znode(c, znode);
1492 if (zp) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001493 ubifs_msg(c, "dump of the parent znode");
Heiko Schocherf5895d12014-06-24 10:10:04 +02001494 ubifs_dump_znode(c, zp);
1495 }
1496 dump_stack();
1497 return -EINVAL;
1498}
1499#else
1500
1501int dbg_check_dir(struct ubifs_info *c, const struct inode *dir)
1502{
1503 return 0;
1504}
1505
1506void dbg_debugfs_exit_fs(struct ubifs_info *c)
1507{
1508 return;
1509}
1510
1511int ubifs_debugging_init(struct ubifs_info *c)
1512{
1513 return 0;
1514}
1515void ubifs_debugging_exit(struct ubifs_info *c)
1516{
1517}
1518int dbg_check_filesystem(struct ubifs_info *c)
1519{
1520 return 0;
1521}
1522int dbg_debugfs_init_fs(struct ubifs_info *c)
1523{
1524 return 0;
1525}
1526#endif
1527
1528#ifndef __UBOOT__
1529/**
1530 * dbg_check_tnc - check TNC tree.
1531 * @c: UBIFS file-system description object
1532 * @extra: do extra checks that are possible at start commit
1533 *
1534 * This function traverses whole TNC tree and checks every znode. Returns zero
1535 * if everything is all right and %-EINVAL if something is wrong with TNC.
1536 */
1537int dbg_check_tnc(struct ubifs_info *c, int extra)
1538{
1539 struct ubifs_znode *znode;
1540 long clean_cnt = 0, dirty_cnt = 0;
1541 int err, last;
1542
1543 if (!dbg_is_chk_index(c))
1544 return 0;
1545
1546 ubifs_assert(mutex_is_locked(&c->tnc_mutex));
1547 if (!c->zroot.znode)
1548 return 0;
1549
1550 znode = ubifs_tnc_postorder_first(c->zroot.znode);
1551 while (1) {
1552 struct ubifs_znode *prev;
1553 struct ubifs_zbranch *zbr;
1554
1555 if (!znode->parent)
1556 zbr = &c->zroot;
1557 else
1558 zbr = &znode->parent->zbranch[znode->iip];
1559
1560 err = dbg_check_znode(c, zbr);
1561 if (err)
1562 return err;
1563
1564 if (extra) {
1565 if (ubifs_zn_dirty(znode))
1566 dirty_cnt += 1;
1567 else
1568 clean_cnt += 1;
1569 }
1570
1571 prev = znode;
1572 znode = ubifs_tnc_postorder_next(znode);
1573 if (!znode)
1574 break;
1575
1576 /*
1577 * If the last key of this znode is equivalent to the first key
1578 * of the next znode (collision), then check order of the keys.
1579 */
1580 last = prev->child_cnt - 1;
1581 if (prev->level == 0 && znode->level == 0 && !c->replaying &&
1582 !keys_cmp(c, &prev->zbranch[last].key,
1583 &znode->zbranch[0].key)) {
1584 err = dbg_check_key_order(c, &prev->zbranch[last],
1585 &znode->zbranch[0]);
1586 if (err < 0)
1587 return err;
1588 if (err) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001589 ubifs_msg(c, "first znode");
Heiko Schocherf5895d12014-06-24 10:10:04 +02001590 ubifs_dump_znode(c, prev);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001591 ubifs_msg(c, "second znode");
Heiko Schocherf5895d12014-06-24 10:10:04 +02001592 ubifs_dump_znode(c, znode);
1593 return -EINVAL;
1594 }
1595 }
1596 }
1597
1598 if (extra) {
1599 if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001600 ubifs_err(c, "incorrect clean_zn_cnt %ld, calculated %ld",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001601 atomic_long_read(&c->clean_zn_cnt),
1602 clean_cnt);
1603 return -EINVAL;
1604 }
1605 if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001606 ubifs_err(c, "incorrect dirty_zn_cnt %ld, calculated %ld",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001607 atomic_long_read(&c->dirty_zn_cnt),
1608 dirty_cnt);
1609 return -EINVAL;
1610 }
1611 }
1612
1613 return 0;
1614}
1615#else
1616int dbg_check_tnc(struct ubifs_info *c, int extra)
1617{
1618 return 0;
1619}
1620#endif
1621
1622/**
1623 * dbg_walk_index - walk the on-flash index.
1624 * @c: UBIFS file-system description object
1625 * @leaf_cb: called for each leaf node
1626 * @znode_cb: called for each indexing node
1627 * @priv: private data which is passed to callbacks
1628 *
1629 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1630 * node and @znode_cb for each indexing node. Returns zero in case of success
1631 * and a negative error code in case of failure.
1632 *
1633 * It would be better if this function removed every znode it pulled to into
1634 * the TNC, so that the behavior more closely matched the non-debugging
1635 * behavior.
1636 */
1637int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1638 dbg_znode_callback znode_cb, void *priv)
1639{
1640 int err;
1641 struct ubifs_zbranch *zbr;
1642 struct ubifs_znode *znode, *child;
1643
1644 mutex_lock(&c->tnc_mutex);
1645 /* If the root indexing node is not in TNC - pull it */
1646 if (!c->zroot.znode) {
1647 c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1648 if (IS_ERR(c->zroot.znode)) {
1649 err = PTR_ERR(c->zroot.znode);
1650 c->zroot.znode = NULL;
1651 goto out_unlock;
1652 }
1653 }
1654
1655 /*
1656 * We are going to traverse the indexing tree in the postorder manner.
1657 * Go down and find the leftmost indexing node where we are going to
1658 * start from.
1659 */
1660 znode = c->zroot.znode;
1661 while (znode->level > 0) {
1662 zbr = &znode->zbranch[0];
1663 child = zbr->znode;
1664 if (!child) {
1665 child = ubifs_load_znode(c, zbr, znode, 0);
1666 if (IS_ERR(child)) {
1667 err = PTR_ERR(child);
1668 goto out_unlock;
1669 }
1670 zbr->znode = child;
1671 }
1672
1673 znode = child;
1674 }
1675
1676 /* Iterate over all indexing nodes */
1677 while (1) {
1678 int idx;
1679
1680 cond_resched();
1681
1682 if (znode_cb) {
1683 err = znode_cb(c, znode, priv);
1684 if (err) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001685 ubifs_err(c, "znode checking function returned error %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001686 err);
1687 ubifs_dump_znode(c, znode);
1688 goto out_dump;
1689 }
1690 }
1691 if (leaf_cb && znode->level == 0) {
1692 for (idx = 0; idx < znode->child_cnt; idx++) {
1693 zbr = &znode->zbranch[idx];
1694 err = leaf_cb(c, zbr, priv);
1695 if (err) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001696 ubifs_err(c, "leaf checking function returned error %d, for leaf at LEB %d:%d",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001697 err, zbr->lnum, zbr->offs);
1698 goto out_dump;
1699 }
1700 }
1701 }
1702
1703 if (!znode->parent)
1704 break;
1705
1706 idx = znode->iip + 1;
1707 znode = znode->parent;
1708 if (idx < znode->child_cnt) {
1709 /* Switch to the next index in the parent */
1710 zbr = &znode->zbranch[idx];
1711 child = zbr->znode;
1712 if (!child) {
1713 child = ubifs_load_znode(c, zbr, znode, idx);
1714 if (IS_ERR(child)) {
1715 err = PTR_ERR(child);
1716 goto out_unlock;
1717 }
1718 zbr->znode = child;
1719 }
1720 znode = child;
1721 } else
1722 /*
1723 * This is the last child, switch to the parent and
1724 * continue.
1725 */
1726 continue;
1727
1728 /* Go to the lowest leftmost znode in the new sub-tree */
1729 while (znode->level > 0) {
1730 zbr = &znode->zbranch[0];
1731 child = zbr->znode;
1732 if (!child) {
1733 child = ubifs_load_znode(c, zbr, znode, 0);
1734 if (IS_ERR(child)) {
1735 err = PTR_ERR(child);
1736 goto out_unlock;
1737 }
1738 zbr->znode = child;
1739 }
1740 znode = child;
1741 }
1742 }
1743
1744 mutex_unlock(&c->tnc_mutex);
1745 return 0;
1746
1747out_dump:
1748 if (znode->parent)
1749 zbr = &znode->parent->zbranch[znode->iip];
1750 else
1751 zbr = &c->zroot;
Heiko Schocher94b66de2015-10-22 06:19:21 +02001752 ubifs_msg(c, "dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001753 ubifs_dump_znode(c, znode);
1754out_unlock:
1755 mutex_unlock(&c->tnc_mutex);
1756 return err;
1757}
1758
1759/**
1760 * add_size - add znode size to partially calculated index size.
1761 * @c: UBIFS file-system description object
1762 * @znode: znode to add size for
1763 * @priv: partially calculated index size
1764 *
1765 * This is a helper function for 'dbg_check_idx_size()' which is called for
1766 * every indexing node and adds its size to the 'long long' variable pointed to
1767 * by @priv.
1768 */
1769static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1770{
1771 long long *idx_size = priv;
1772 int add;
1773
1774 add = ubifs_idx_node_sz(c, znode->child_cnt);
1775 add = ALIGN(add, 8);
1776 *idx_size += add;
1777 return 0;
1778}
1779
1780/**
1781 * dbg_check_idx_size - check index size.
1782 * @c: UBIFS file-system description object
1783 * @idx_size: size to check
1784 *
1785 * This function walks the UBIFS index, calculates its size and checks that the
1786 * size is equivalent to @idx_size. Returns zero in case of success and a
1787 * negative error code in case of failure.
1788 */
1789int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
1790{
1791 int err;
1792 long long calc = 0;
1793
1794 if (!dbg_is_chk_index(c))
1795 return 0;
1796
1797 err = dbg_walk_index(c, NULL, add_size, &calc);
1798 if (err) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001799 ubifs_err(c, "error %d while walking the index", err);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001800 return err;
1801 }
1802
1803 if (calc != idx_size) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001804 ubifs_err(c, "index size check failed: calculated size is %lld, should be %lld",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001805 calc, idx_size);
1806 dump_stack();
1807 return -EINVAL;
1808 }
1809
1810 return 0;
1811}
1812
1813#ifndef __UBOOT__
1814/**
1815 * struct fsck_inode - information about an inode used when checking the file-system.
1816 * @rb: link in the RB-tree of inodes
1817 * @inum: inode number
1818 * @mode: inode type, permissions, etc
1819 * @nlink: inode link count
1820 * @xattr_cnt: count of extended attributes
1821 * @references: how many directory/xattr entries refer this inode (calculated
1822 * while walking the index)
1823 * @calc_cnt: for directory inode count of child directories
1824 * @size: inode size (read from on-flash inode)
1825 * @xattr_sz: summary size of all extended attributes (read from on-flash
1826 * inode)
1827 * @calc_sz: for directories calculated directory size
1828 * @calc_xcnt: count of extended attributes
1829 * @calc_xsz: calculated summary size of all extended attributes
1830 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1831 * inode (read from on-flash inode)
1832 * @calc_xnms: calculated sum of lengths of all extended attribute names
1833 */
1834struct fsck_inode {
1835 struct rb_node rb;
1836 ino_t inum;
1837 umode_t mode;
1838 unsigned int nlink;
1839 unsigned int xattr_cnt;
1840 int references;
1841 int calc_cnt;
1842 long long size;
1843 unsigned int xattr_sz;
1844 long long calc_sz;
1845 long long calc_xcnt;
1846 long long calc_xsz;
1847 unsigned int xattr_nms;
1848 long long calc_xnms;
1849};
1850
1851/**
1852 * struct fsck_data - private FS checking information.
1853 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1854 */
1855struct fsck_data {
1856 struct rb_root inodes;
1857};
1858
1859/**
1860 * add_inode - add inode information to RB-tree of inodes.
1861 * @c: UBIFS file-system description object
1862 * @fsckd: FS checking information
1863 * @ino: raw UBIFS inode to add
1864 *
1865 * This is a helper function for 'check_leaf()' which adds information about
1866 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1867 * case of success and a negative error code in case of failure.
1868 */
1869static struct fsck_inode *add_inode(struct ubifs_info *c,
1870 struct fsck_data *fsckd,
1871 struct ubifs_ino_node *ino)
1872{
1873 struct rb_node **p, *parent = NULL;
1874 struct fsck_inode *fscki;
1875 ino_t inum = key_inum_flash(c, &ino->key);
1876 struct inode *inode;
1877 struct ubifs_inode *ui;
1878
1879 p = &fsckd->inodes.rb_node;
1880 while (*p) {
1881 parent = *p;
1882 fscki = rb_entry(parent, struct fsck_inode, rb);
1883 if (inum < fscki->inum)
1884 p = &(*p)->rb_left;
1885 else if (inum > fscki->inum)
1886 p = &(*p)->rb_right;
1887 else
1888 return fscki;
1889 }
1890
1891 if (inum > c->highest_inum) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001892 ubifs_err(c, "too high inode number, max. is %lu",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001893 (unsigned long)c->highest_inum);
1894 return ERR_PTR(-EINVAL);
1895 }
1896
1897 fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
1898 if (!fscki)
1899 return ERR_PTR(-ENOMEM);
1900
1901 inode = ilookup(c->vfs_sb, inum);
1902
1903 fscki->inum = inum;
1904 /*
1905 * If the inode is present in the VFS inode cache, use it instead of
1906 * the on-flash inode which might be out-of-date. E.g., the size might
1907 * be out-of-date. If we do not do this, the following may happen, for
1908 * example:
1909 * 1. A power cut happens
1910 * 2. We mount the file-system R/O, the replay process fixes up the
1911 * inode size in the VFS cache, but on on-flash.
1912 * 3. 'check_leaf()' fails because it hits a data node beyond inode
1913 * size.
1914 */
1915 if (!inode) {
1916 fscki->nlink = le32_to_cpu(ino->nlink);
1917 fscki->size = le64_to_cpu(ino->size);
1918 fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
1919 fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
1920 fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
1921 fscki->mode = le32_to_cpu(ino->mode);
1922 } else {
1923 ui = ubifs_inode(inode);
1924 fscki->nlink = inode->i_nlink;
1925 fscki->size = inode->i_size;
1926 fscki->xattr_cnt = ui->xattr_cnt;
1927 fscki->xattr_sz = ui->xattr_size;
1928 fscki->xattr_nms = ui->xattr_names;
1929 fscki->mode = inode->i_mode;
1930 iput(inode);
1931 }
1932
1933 if (S_ISDIR(fscki->mode)) {
1934 fscki->calc_sz = UBIFS_INO_NODE_SZ;
1935 fscki->calc_cnt = 2;
1936 }
1937
1938 rb_link_node(&fscki->rb, parent, p);
1939 rb_insert_color(&fscki->rb, &fsckd->inodes);
1940
1941 return fscki;
1942}
1943
1944/**
1945 * search_inode - search inode in the RB-tree of inodes.
1946 * @fsckd: FS checking information
1947 * @inum: inode number to search
1948 *
1949 * This is a helper function for 'check_leaf()' which searches inode @inum in
1950 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1951 * the inode was not found.
1952 */
1953static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
1954{
1955 struct rb_node *p;
1956 struct fsck_inode *fscki;
1957
1958 p = fsckd->inodes.rb_node;
1959 while (p) {
1960 fscki = rb_entry(p, struct fsck_inode, rb);
1961 if (inum < fscki->inum)
1962 p = p->rb_left;
1963 else if (inum > fscki->inum)
1964 p = p->rb_right;
1965 else
1966 return fscki;
1967 }
1968 return NULL;
1969}
1970
1971/**
1972 * read_add_inode - read inode node and add it to RB-tree of inodes.
1973 * @c: UBIFS file-system description object
1974 * @fsckd: FS checking information
1975 * @inum: inode number to read
1976 *
1977 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1978 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1979 * information pointer in case of success and a negative error code in case of
1980 * failure.
1981 */
1982static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1983 struct fsck_data *fsckd, ino_t inum)
1984{
1985 int n, err;
1986 union ubifs_key key;
1987 struct ubifs_znode *znode;
1988 struct ubifs_zbranch *zbr;
1989 struct ubifs_ino_node *ino;
1990 struct fsck_inode *fscki;
1991
1992 fscki = search_inode(fsckd, inum);
1993 if (fscki)
1994 return fscki;
1995
1996 ino_key_init(c, &key, inum);
1997 err = ubifs_lookup_level0(c, &key, &znode, &n);
1998 if (!err) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001999 ubifs_err(c, "inode %lu not found in index", (unsigned long)inum);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002000 return ERR_PTR(-ENOENT);
2001 } else if (err < 0) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002002 ubifs_err(c, "error %d while looking up inode %lu",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002003 err, (unsigned long)inum);
2004 return ERR_PTR(err);
2005 }
2006
2007 zbr = &znode->zbranch[n];
2008 if (zbr->len < UBIFS_INO_NODE_SZ) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002009 ubifs_err(c, "bad node %lu node length %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002010 (unsigned long)inum, zbr->len);
2011 return ERR_PTR(-EINVAL);
2012 }
2013
2014 ino = kmalloc(zbr->len, GFP_NOFS);
2015 if (!ino)
2016 return ERR_PTR(-ENOMEM);
2017
2018 err = ubifs_tnc_read_node(c, zbr, ino);
2019 if (err) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002020 ubifs_err(c, "cannot read inode node at LEB %d:%d, error %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002021 zbr->lnum, zbr->offs, err);
2022 kfree(ino);
2023 return ERR_PTR(err);
2024 }
2025
2026 fscki = add_inode(c, fsckd, ino);
2027 kfree(ino);
2028 if (IS_ERR(fscki)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002029 ubifs_err(c, "error %ld while adding inode %lu node",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002030 PTR_ERR(fscki), (unsigned long)inum);
2031 return fscki;
2032 }
2033
2034 return fscki;
2035}
2036
2037/**
2038 * check_leaf - check leaf node.
2039 * @c: UBIFS file-system description object
2040 * @zbr: zbranch of the leaf node to check
2041 * @priv: FS checking information
2042 *
2043 * This is a helper function for 'dbg_check_filesystem()' which is called for
2044 * every single leaf node while walking the indexing tree. It checks that the
2045 * leaf node referred from the indexing tree exists, has correct CRC, and does
2046 * some other basic validation. This function is also responsible for building
2047 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
2048 * calculates reference count, size, etc for each inode in order to later
2049 * compare them to the information stored inside the inodes and detect possible
2050 * inconsistencies. Returns zero in case of success and a negative error code
2051 * in case of failure.
2052 */
2053static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
2054 void *priv)
2055{
2056 ino_t inum;
2057 void *node;
2058 struct ubifs_ch *ch;
2059 int err, type = key_type(c, &zbr->key);
2060 struct fsck_inode *fscki;
2061
2062 if (zbr->len < UBIFS_CH_SZ) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002063 ubifs_err(c, "bad leaf length %d (LEB %d:%d)",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002064 zbr->len, zbr->lnum, zbr->offs);
2065 return -EINVAL;
2066 }
2067
2068 node = kmalloc(zbr->len, GFP_NOFS);
2069 if (!node)
2070 return -ENOMEM;
2071
2072 err = ubifs_tnc_read_node(c, zbr, node);
2073 if (err) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002074 ubifs_err(c, "cannot read leaf node at LEB %d:%d, error %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002075 zbr->lnum, zbr->offs, err);
2076 goto out_free;
2077 }
2078
2079 /* If this is an inode node, add it to RB-tree of inodes */
2080 if (type == UBIFS_INO_KEY) {
2081 fscki = add_inode(c, priv, node);
2082 if (IS_ERR(fscki)) {
2083 err = PTR_ERR(fscki);
Heiko Schocher94b66de2015-10-22 06:19:21 +02002084 ubifs_err(c, "error %d while adding inode node", err);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002085 goto out_dump;
2086 }
2087 goto out;
2088 }
2089
2090 if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
2091 type != UBIFS_DATA_KEY) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002092 ubifs_err(c, "unexpected node type %d at LEB %d:%d",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002093 type, zbr->lnum, zbr->offs);
2094 err = -EINVAL;
2095 goto out_free;
2096 }
2097
2098 ch = node;
2099 if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002100 ubifs_err(c, "too high sequence number, max. is %llu",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002101 c->max_sqnum);
2102 err = -EINVAL;
2103 goto out_dump;
2104 }
2105
2106 if (type == UBIFS_DATA_KEY) {
2107 long long blk_offs;
2108 struct ubifs_data_node *dn = node;
2109
Heiko Schocher94b66de2015-10-22 06:19:21 +02002110 ubifs_assert(zbr->len >= UBIFS_DATA_NODE_SZ);
2111
Heiko Schocherf5895d12014-06-24 10:10:04 +02002112 /*
2113 * Search the inode node this data node belongs to and insert
2114 * it to the RB-tree of inodes.
2115 */
2116 inum = key_inum_flash(c, &dn->key);
2117 fscki = read_add_inode(c, priv, inum);
2118 if (IS_ERR(fscki)) {
2119 err = PTR_ERR(fscki);
Heiko Schocher94b66de2015-10-22 06:19:21 +02002120 ubifs_err(c, "error %d while processing data node and trying to find inode node %lu",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002121 err, (unsigned long)inum);
2122 goto out_dump;
2123 }
2124
2125 /* Make sure the data node is within inode size */
2126 blk_offs = key_block_flash(c, &dn->key);
2127 blk_offs <<= UBIFS_BLOCK_SHIFT;
2128 blk_offs += le32_to_cpu(dn->size);
2129 if (blk_offs > fscki->size) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002130 ubifs_err(c, "data node at LEB %d:%d is not within inode size %lld",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002131 zbr->lnum, zbr->offs, fscki->size);
2132 err = -EINVAL;
2133 goto out_dump;
2134 }
2135 } else {
2136 int nlen;
2137 struct ubifs_dent_node *dent = node;
2138 struct fsck_inode *fscki1;
2139
Heiko Schocher94b66de2015-10-22 06:19:21 +02002140 ubifs_assert(zbr->len >= UBIFS_DENT_NODE_SZ);
2141
Heiko Schocherf5895d12014-06-24 10:10:04 +02002142 err = ubifs_validate_entry(c, dent);
2143 if (err)
2144 goto out_dump;
2145
2146 /*
2147 * Search the inode node this entry refers to and the parent
2148 * inode node and insert them to the RB-tree of inodes.
2149 */
2150 inum = le64_to_cpu(dent->inum);
2151 fscki = read_add_inode(c, priv, inum);
2152 if (IS_ERR(fscki)) {
2153 err = PTR_ERR(fscki);
Heiko Schocher94b66de2015-10-22 06:19:21 +02002154 ubifs_err(c, "error %d while processing entry node and trying to find inode node %lu",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002155 err, (unsigned long)inum);
2156 goto out_dump;
2157 }
2158
2159 /* Count how many direntries or xentries refers this inode */
2160 fscki->references += 1;
2161
2162 inum = key_inum_flash(c, &dent->key);
2163 fscki1 = read_add_inode(c, priv, inum);
2164 if (IS_ERR(fscki1)) {
2165 err = PTR_ERR(fscki1);
Heiko Schocher94b66de2015-10-22 06:19:21 +02002166 ubifs_err(c, "error %d while processing entry node and trying to find parent inode node %lu",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002167 err, (unsigned long)inum);
2168 goto out_dump;
2169 }
2170
2171 nlen = le16_to_cpu(dent->nlen);
2172 if (type == UBIFS_XENT_KEY) {
2173 fscki1->calc_xcnt += 1;
2174 fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
2175 fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
2176 fscki1->calc_xnms += nlen;
2177 } else {
2178 fscki1->calc_sz += CALC_DENT_SIZE(nlen);
2179 if (dent->type == UBIFS_ITYPE_DIR)
2180 fscki1->calc_cnt += 1;
2181 }
2182 }
2183
2184out:
2185 kfree(node);
2186 return 0;
2187
2188out_dump:
Heiko Schocher94b66de2015-10-22 06:19:21 +02002189 ubifs_msg(c, "dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002190 ubifs_dump_node(c, node);
2191out_free:
2192 kfree(node);
2193 return err;
2194}
2195
2196/**
2197 * free_inodes - free RB-tree of inodes.
2198 * @fsckd: FS checking information
2199 */
2200static void free_inodes(struct fsck_data *fsckd)
2201{
2202 struct fsck_inode *fscki, *n;
2203
2204 rbtree_postorder_for_each_entry_safe(fscki, n, &fsckd->inodes, rb)
2205 kfree(fscki);
2206}
2207
2208/**
2209 * check_inodes - checks all inodes.
2210 * @c: UBIFS file-system description object
2211 * @fsckd: FS checking information
2212 *
2213 * This is a helper function for 'dbg_check_filesystem()' which walks the
2214 * RB-tree of inodes after the index scan has been finished, and checks that
2215 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
2216 * %-EINVAL if not, and a negative error code in case of failure.
2217 */
2218static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
2219{
2220 int n, err;
2221 union ubifs_key key;
2222 struct ubifs_znode *znode;
2223 struct ubifs_zbranch *zbr;
2224 struct ubifs_ino_node *ino;
2225 struct fsck_inode *fscki;
2226 struct rb_node *this = rb_first(&fsckd->inodes);
2227
2228 while (this) {
2229 fscki = rb_entry(this, struct fsck_inode, rb);
2230 this = rb_next(this);
2231
2232 if (S_ISDIR(fscki->mode)) {
2233 /*
2234 * Directories have to have exactly one reference (they
2235 * cannot have hardlinks), although root inode is an
2236 * exception.
2237 */
2238 if (fscki->inum != UBIFS_ROOT_INO &&
2239 fscki->references != 1) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002240 ubifs_err(c, "directory inode %lu has %d direntries which refer it, but should be 1",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002241 (unsigned long)fscki->inum,
2242 fscki->references);
2243 goto out_dump;
2244 }
2245 if (fscki->inum == UBIFS_ROOT_INO &&
2246 fscki->references != 0) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002247 ubifs_err(c, "root inode %lu has non-zero (%d) direntries which refer it",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002248 (unsigned long)fscki->inum,
2249 fscki->references);
2250 goto out_dump;
2251 }
2252 if (fscki->calc_sz != fscki->size) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002253 ubifs_err(c, "directory inode %lu size is %lld, but calculated size is %lld",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002254 (unsigned long)fscki->inum,
2255 fscki->size, fscki->calc_sz);
2256 goto out_dump;
2257 }
2258 if (fscki->calc_cnt != fscki->nlink) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002259 ubifs_err(c, "directory inode %lu nlink is %d, but calculated nlink is %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002260 (unsigned long)fscki->inum,
2261 fscki->nlink, fscki->calc_cnt);
2262 goto out_dump;
2263 }
2264 } else {
2265 if (fscki->references != fscki->nlink) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002266 ubifs_err(c, "inode %lu nlink is %d, but calculated nlink is %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002267 (unsigned long)fscki->inum,
2268 fscki->nlink, fscki->references);
2269 goto out_dump;
2270 }
2271 }
2272 if (fscki->xattr_sz != fscki->calc_xsz) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002273 ubifs_err(c, "inode %lu has xattr size %u, but calculated size is %lld",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002274 (unsigned long)fscki->inum, fscki->xattr_sz,
2275 fscki->calc_xsz);
2276 goto out_dump;
2277 }
2278 if (fscki->xattr_cnt != fscki->calc_xcnt) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002279 ubifs_err(c, "inode %lu has %u xattrs, but calculated count is %lld",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002280 (unsigned long)fscki->inum,
2281 fscki->xattr_cnt, fscki->calc_xcnt);
2282 goto out_dump;
2283 }
2284 if (fscki->xattr_nms != fscki->calc_xnms) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002285 ubifs_err(c, "inode %lu has xattr names' size %u, but calculated names' size is %lld",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002286 (unsigned long)fscki->inum, fscki->xattr_nms,
2287 fscki->calc_xnms);
2288 goto out_dump;
2289 }
2290 }
2291
2292 return 0;
2293
2294out_dump:
2295 /* Read the bad inode and dump it */
2296 ino_key_init(c, &key, fscki->inum);
2297 err = ubifs_lookup_level0(c, &key, &znode, &n);
2298 if (!err) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002299 ubifs_err(c, "inode %lu not found in index",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002300 (unsigned long)fscki->inum);
2301 return -ENOENT;
2302 } else if (err < 0) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002303 ubifs_err(c, "error %d while looking up inode %lu",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002304 err, (unsigned long)fscki->inum);
2305 return err;
2306 }
2307
2308 zbr = &znode->zbranch[n];
2309 ino = kmalloc(zbr->len, GFP_NOFS);
2310 if (!ino)
2311 return -ENOMEM;
2312
2313 err = ubifs_tnc_read_node(c, zbr, ino);
2314 if (err) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002315 ubifs_err(c, "cannot read inode node at LEB %d:%d, error %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002316 zbr->lnum, zbr->offs, err);
2317 kfree(ino);
2318 return err;
2319 }
2320
Heiko Schocher94b66de2015-10-22 06:19:21 +02002321 ubifs_msg(c, "dump of the inode %lu sitting in LEB %d:%d",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002322 (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
2323 ubifs_dump_node(c, ino);
2324 kfree(ino);
2325 return -EINVAL;
2326}
2327
2328/**
2329 * dbg_check_filesystem - check the file-system.
2330 * @c: UBIFS file-system description object
2331 *
2332 * This function checks the file system, namely:
2333 * o makes sure that all leaf nodes exist and their CRCs are correct;
2334 * o makes sure inode nlink, size, xattr size/count are correct (for all
2335 * inodes).
2336 *
2337 * The function reads whole indexing tree and all nodes, so it is pretty
2338 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2339 * not, and a negative error code in case of failure.
2340 */
2341int dbg_check_filesystem(struct ubifs_info *c)
2342{
2343 int err;
2344 struct fsck_data fsckd;
2345
2346 if (!dbg_is_chk_fs(c))
2347 return 0;
2348
2349 fsckd.inodes = RB_ROOT;
2350 err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
2351 if (err)
2352 goto out_free;
2353
2354 err = check_inodes(c, &fsckd);
2355 if (err)
2356 goto out_free;
2357
2358 free_inodes(&fsckd);
2359 return 0;
2360
2361out_free:
Heiko Schocher94b66de2015-10-22 06:19:21 +02002362 ubifs_err(c, "file-system check failed with error %d", err);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002363 dump_stack();
2364 free_inodes(&fsckd);
2365 return err;
2366}
2367
2368/**
2369 * dbg_check_data_nodes_order - check that list of data nodes is sorted.
2370 * @c: UBIFS file-system description object
2371 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2372 *
2373 * This function returns zero if the list of data nodes is sorted correctly,
2374 * and %-EINVAL if not.
2375 */
2376int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head)
2377{
2378 struct list_head *cur;
2379 struct ubifs_scan_node *sa, *sb;
2380
2381 if (!dbg_is_chk_gen(c))
2382 return 0;
2383
2384 for (cur = head->next; cur->next != head; cur = cur->next) {
2385 ino_t inuma, inumb;
2386 uint32_t blka, blkb;
2387
2388 cond_resched();
2389 sa = container_of(cur, struct ubifs_scan_node, list);
2390 sb = container_of(cur->next, struct ubifs_scan_node, list);
2391
2392 if (sa->type != UBIFS_DATA_NODE) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002393 ubifs_err(c, "bad node type %d", sa->type);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002394 ubifs_dump_node(c, sa->node);
2395 return -EINVAL;
2396 }
2397 if (sb->type != UBIFS_DATA_NODE) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002398 ubifs_err(c, "bad node type %d", sb->type);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002399 ubifs_dump_node(c, sb->node);
2400 return -EINVAL;
2401 }
2402
2403 inuma = key_inum(c, &sa->key);
2404 inumb = key_inum(c, &sb->key);
2405
2406 if (inuma < inumb)
2407 continue;
2408 if (inuma > inumb) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002409 ubifs_err(c, "larger inum %lu goes before inum %lu",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002410 (unsigned long)inuma, (unsigned long)inumb);
2411 goto error_dump;
2412 }
2413
2414 blka = key_block(c, &sa->key);
2415 blkb = key_block(c, &sb->key);
2416
2417 if (blka > blkb) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002418 ubifs_err(c, "larger block %u goes before %u", blka, blkb);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002419 goto error_dump;
2420 }
2421 if (blka == blkb) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002422 ubifs_err(c, "two data nodes for the same block");
Heiko Schocherf5895d12014-06-24 10:10:04 +02002423 goto error_dump;
2424 }
2425 }
2426
2427 return 0;
2428
2429error_dump:
2430 ubifs_dump_node(c, sa->node);
2431 ubifs_dump_node(c, sb->node);
2432 return -EINVAL;
2433}
2434
2435/**
2436 * dbg_check_nondata_nodes_order - check that list of data nodes is sorted.
2437 * @c: UBIFS file-system description object
2438 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2439 *
2440 * This function returns zero if the list of non-data nodes is sorted correctly,
2441 * and %-EINVAL if not.
2442 */
2443int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head)
2444{
2445 struct list_head *cur;
2446 struct ubifs_scan_node *sa, *sb;
2447
2448 if (!dbg_is_chk_gen(c))
2449 return 0;
2450
2451 for (cur = head->next; cur->next != head; cur = cur->next) {
2452 ino_t inuma, inumb;
2453 uint32_t hasha, hashb;
2454
2455 cond_resched();
2456 sa = container_of(cur, struct ubifs_scan_node, list);
2457 sb = container_of(cur->next, struct ubifs_scan_node, list);
2458
2459 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2460 sa->type != UBIFS_XENT_NODE) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002461 ubifs_err(c, "bad node type %d", sa->type);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002462 ubifs_dump_node(c, sa->node);
2463 return -EINVAL;
2464 }
2465 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2466 sa->type != UBIFS_XENT_NODE) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002467 ubifs_err(c, "bad node type %d", sb->type);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002468 ubifs_dump_node(c, sb->node);
2469 return -EINVAL;
2470 }
2471
2472 if (sa->type != UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002473 ubifs_err(c, "non-inode node goes before inode node");
Heiko Schocherf5895d12014-06-24 10:10:04 +02002474 goto error_dump;
2475 }
2476
2477 if (sa->type == UBIFS_INO_NODE && sb->type != UBIFS_INO_NODE)
2478 continue;
2479
2480 if (sa->type == UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2481 /* Inode nodes are sorted in descending size order */
2482 if (sa->len < sb->len) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002483 ubifs_err(c, "smaller inode node goes first");
Heiko Schocherf5895d12014-06-24 10:10:04 +02002484 goto error_dump;
2485 }
2486 continue;
2487 }
2488
2489 /*
2490 * This is either a dentry or xentry, which should be sorted in
2491 * ascending (parent ino, hash) order.
2492 */
2493 inuma = key_inum(c, &sa->key);
2494 inumb = key_inum(c, &sb->key);
2495
2496 if (inuma < inumb)
2497 continue;
2498 if (inuma > inumb) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002499 ubifs_err(c, "larger inum %lu goes before inum %lu",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002500 (unsigned long)inuma, (unsigned long)inumb);
2501 goto error_dump;
2502 }
2503
2504 hasha = key_block(c, &sa->key);
2505 hashb = key_block(c, &sb->key);
2506
2507 if (hasha > hashb) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02002508 ubifs_err(c, "larger hash %u goes before %u",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002509 hasha, hashb);
2510 goto error_dump;
2511 }
2512 }
2513
2514 return 0;
2515
2516error_dump:
Heiko Schocher94b66de2015-10-22 06:19:21 +02002517 ubifs_msg(c, "dumping first node");
Heiko Schocherf5895d12014-06-24 10:10:04 +02002518 ubifs_dump_node(c, sa->node);
Heiko Schocher94b66de2015-10-22 06:19:21 +02002519 ubifs_msg(c, "dumping second node");
Heiko Schocherf5895d12014-06-24 10:10:04 +02002520 ubifs_dump_node(c, sb->node);
2521 return -EINVAL;
2522 return 0;
2523}
2524
2525static inline int chance(unsigned int n, unsigned int out_of)
2526{
2527 return !!((prandom_u32() % out_of) + 1 <= n);
2528
2529}
2530
2531static int power_cut_emulated(struct ubifs_info *c, int lnum, int write)
2532{
2533 struct ubifs_debug_info *d = c->dbg;
2534
2535 ubifs_assert(dbg_is_tst_rcvry(c));
2536
2537 if (!d->pc_cnt) {
2538 /* First call - decide delay to the power cut */
2539 if (chance(1, 2)) {
2540 unsigned long delay;
2541
2542 if (chance(1, 2)) {
2543 d->pc_delay = 1;
Heiko Schocher94b66de2015-10-22 06:19:21 +02002544 /* Fail within 1 minute */
Heiko Schocherf5895d12014-06-24 10:10:04 +02002545 delay = prandom_u32() % 60000;
2546 d->pc_timeout = jiffies;
2547 d->pc_timeout += msecs_to_jiffies(delay);
Heiko Schocher94b66de2015-10-22 06:19:21 +02002548 ubifs_warn(c, "failing after %lums", delay);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002549 } else {
2550 d->pc_delay = 2;
2551 delay = prandom_u32() % 10000;
2552 /* Fail within 10000 operations */
2553 d->pc_cnt_max = delay;
Heiko Schocher94b66de2015-10-22 06:19:21 +02002554 ubifs_warn(c, "failing after %lu calls", delay);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002555 }
2556 }
2557
2558 d->pc_cnt += 1;
2559 }
2560
2561 /* Determine if failure delay has expired */
2562 if (d->pc_delay == 1 && time_before(jiffies, d->pc_timeout))
2563 return 0;
2564 if (d->pc_delay == 2 && d->pc_cnt++ < d->pc_cnt_max)
2565 return 0;
2566
2567 if (lnum == UBIFS_SB_LNUM) {
2568 if (write && chance(1, 2))
2569 return 0;
2570 if (chance(19, 20))
2571 return 0;
Heiko Schocher94b66de2015-10-22 06:19:21 +02002572 ubifs_warn(c, "failing in super block LEB %d", lnum);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002573 } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2574 if (chance(19, 20))
2575 return 0;
Heiko Schocher94b66de2015-10-22 06:19:21 +02002576 ubifs_warn(c, "failing in master LEB %d", lnum);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002577 } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2578 if (write && chance(99, 100))
2579 return 0;
2580 if (chance(399, 400))
2581 return 0;
Heiko Schocher94b66de2015-10-22 06:19:21 +02002582 ubifs_warn(c, "failing in log LEB %d", lnum);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002583 } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2584 if (write && chance(7, 8))
2585 return 0;
2586 if (chance(19, 20))
2587 return 0;
Heiko Schocher94b66de2015-10-22 06:19:21 +02002588 ubifs_warn(c, "failing in LPT LEB %d", lnum);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002589 } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2590 if (write && chance(1, 2))
2591 return 0;
2592 if (chance(9, 10))
2593 return 0;
Heiko Schocher94b66de2015-10-22 06:19:21 +02002594 ubifs_warn(c, "failing in orphan LEB %d", lnum);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002595 } else if (lnum == c->ihead_lnum) {
2596 if (chance(99, 100))
2597 return 0;
Heiko Schocher94b66de2015-10-22 06:19:21 +02002598 ubifs_warn(c, "failing in index head LEB %d", lnum);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002599 } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2600 if (chance(9, 10))
2601 return 0;
Heiko Schocher94b66de2015-10-22 06:19:21 +02002602 ubifs_warn(c, "failing in GC head LEB %d", lnum);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002603 } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
2604 !ubifs_search_bud(c, lnum)) {
2605 if (chance(19, 20))
2606 return 0;
Heiko Schocher94b66de2015-10-22 06:19:21 +02002607 ubifs_warn(c, "failing in non-bud LEB %d", lnum);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002608 } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
2609 c->cmt_state == COMMIT_RUNNING_REQUIRED) {
2610 if (chance(999, 1000))
2611 return 0;
Heiko Schocher94b66de2015-10-22 06:19:21 +02002612 ubifs_warn(c, "failing in bud LEB %d commit running", lnum);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002613 } else {
2614 if (chance(9999, 10000))
2615 return 0;
Heiko Schocher94b66de2015-10-22 06:19:21 +02002616 ubifs_warn(c, "failing in bud LEB %d commit not running", lnum);
Heiko Schocherf5895d12014-06-24 10:10:04 +02002617 }
2618
2619 d->pc_happened = 1;
Heiko Schocher94b66de2015-10-22 06:19:21 +02002620 ubifs_warn(c, "========== Power cut emulated ==========");
Heiko Schocherf5895d12014-06-24 10:10:04 +02002621 dump_stack();
2622 return 1;
2623}
2624
2625static int corrupt_data(const struct ubifs_info *c, const void *buf,
2626 unsigned int len)
2627{
2628 unsigned int from, to, ffs = chance(1, 2);
2629 unsigned char *p = (void *)buf;
2630
2631 from = prandom_u32() % len;
2632 /* Corruption span max to end of write unit */
2633 to = min(len, ALIGN(from + 1, c->max_write_size));
2634
Heiko Schocher94b66de2015-10-22 06:19:21 +02002635 ubifs_warn(c, "filled bytes %u-%u with %s", from, to - 1,
Heiko Schocherf5895d12014-06-24 10:10:04 +02002636 ffs ? "0xFFs" : "random data");
2637
2638 if (ffs)
2639 memset(p + from, 0xFF, to - from);
2640 else
2641 prandom_bytes(p + from, to - from);
2642
2643 return to;
2644}
2645
2646int dbg_leb_write(struct ubifs_info *c, int lnum, const void *buf,
2647 int offs, int len)
2648{
2649 int err, failing;
2650
2651 if (c->dbg->pc_happened)
2652 return -EROFS;
2653
2654 failing = power_cut_emulated(c, lnum, 1);
2655 if (failing) {
2656 len = corrupt_data(c, buf, len);
Heiko Schocher94b66de2015-10-22 06:19:21 +02002657 ubifs_warn(c, "actually write %d bytes to LEB %d:%d (the buffer was corrupted)",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002658 len, lnum, offs);
2659 }
2660 err = ubi_leb_write(c->ubi, lnum, buf, offs, len);
2661 if (err)
2662 return err;
2663 if (failing)
2664 return -EROFS;
2665 return 0;
2666}
2667
2668int dbg_leb_change(struct ubifs_info *c, int lnum, const void *buf,
2669 int len)
2670{
2671 int err;
2672
2673 if (c->dbg->pc_happened)
2674 return -EROFS;
2675 if (power_cut_emulated(c, lnum, 1))
2676 return -EROFS;
2677 err = ubi_leb_change(c->ubi, lnum, buf, len);
2678 if (err)
2679 return err;
2680 if (power_cut_emulated(c, lnum, 1))
2681 return -EROFS;
2682 return 0;
2683}
2684
2685int dbg_leb_unmap(struct ubifs_info *c, int lnum)
2686{
2687 int err;
2688
2689 if (c->dbg->pc_happened)
2690 return -EROFS;
2691 if (power_cut_emulated(c, lnum, 0))
2692 return -EROFS;
2693 err = ubi_leb_unmap(c->ubi, lnum);
2694 if (err)
2695 return err;
2696 if (power_cut_emulated(c, lnum, 0))
2697 return -EROFS;
2698 return 0;
2699}
2700
2701int dbg_leb_map(struct ubifs_info *c, int lnum)
2702{
2703 int err;
2704
2705 if (c->dbg->pc_happened)
2706 return -EROFS;
2707 if (power_cut_emulated(c, lnum, 0))
2708 return -EROFS;
2709 err = ubi_leb_map(c->ubi, lnum);
2710 if (err)
2711 return err;
2712 if (power_cut_emulated(c, lnum, 0))
2713 return -EROFS;
2714 return 0;
2715}
2716
2717/*
2718 * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
2719 * contain the stuff specific to particular file-system mounts.
2720 */
2721static struct dentry *dfs_rootdir;
2722
2723static int dfs_file_open(struct inode *inode, struct file *file)
2724{
2725 file->private_data = inode->i_private;
2726 return nonseekable_open(inode, file);
2727}
2728
2729/**
2730 * provide_user_output - provide output to the user reading a debugfs file.
2731 * @val: boolean value for the answer
2732 * @u: the buffer to store the answer at
2733 * @count: size of the buffer
2734 * @ppos: position in the @u output buffer
2735 *
2736 * This is a simple helper function which stores @val boolean value in the user
2737 * buffer when the user reads one of UBIFS debugfs files. Returns amount of
2738 * bytes written to @u in case of success and a negative error code in case of
2739 * failure.
2740 */
2741static int provide_user_output(int val, char __user *u, size_t count,
2742 loff_t *ppos)
2743{
2744 char buf[3];
2745
2746 if (val)
2747 buf[0] = '1';
2748 else
2749 buf[0] = '0';
2750 buf[1] = '\n';
2751 buf[2] = 0x00;
2752
2753 return simple_read_from_buffer(u, count, ppos, buf, 2);
2754}
2755
2756static ssize_t dfs_file_read(struct file *file, char __user *u, size_t count,
2757 loff_t *ppos)
2758{
2759 struct dentry *dent = file->f_path.dentry;
2760 struct ubifs_info *c = file->private_data;
2761 struct ubifs_debug_info *d = c->dbg;
2762 int val;
2763
2764 if (dent == d->dfs_chk_gen)
2765 val = d->chk_gen;
2766 else if (dent == d->dfs_chk_index)
2767 val = d->chk_index;
2768 else if (dent == d->dfs_chk_orph)
2769 val = d->chk_orph;
2770 else if (dent == d->dfs_chk_lprops)
2771 val = d->chk_lprops;
2772 else if (dent == d->dfs_chk_fs)
2773 val = d->chk_fs;
2774 else if (dent == d->dfs_tst_rcvry)
2775 val = d->tst_rcvry;
2776 else if (dent == d->dfs_ro_error)
2777 val = c->ro_error;
2778 else
2779 return -EINVAL;
2780
2781 return provide_user_output(val, u, count, ppos);
2782}
2783
2784/**
2785 * interpret_user_input - interpret user debugfs file input.
2786 * @u: user-provided buffer with the input
2787 * @count: buffer size
2788 *
2789 * This is a helper function which interpret user input to a boolean UBIFS
2790 * debugfs file. Returns %0 or %1 in case of success and a negative error code
2791 * in case of failure.
2792 */
2793static int interpret_user_input(const char __user *u, size_t count)
2794{
2795 size_t buf_size;
2796 char buf[8];
2797
2798 buf_size = min_t(size_t, count, (sizeof(buf) - 1));
2799 if (copy_from_user(buf, u, buf_size))
2800 return -EFAULT;
2801
2802 if (buf[0] == '1')
2803 return 1;
2804 else if (buf[0] == '0')
2805 return 0;
2806
2807 return -EINVAL;
2808}
2809
2810static ssize_t dfs_file_write(struct file *file, const char __user *u,
2811 size_t count, loff_t *ppos)
2812{
2813 struct ubifs_info *c = file->private_data;
2814 struct ubifs_debug_info *d = c->dbg;
2815 struct dentry *dent = file->f_path.dentry;
2816 int val;
2817
2818 /*
2819 * TODO: this is racy - the file-system might have already been
2820 * unmounted and we'd oops in this case. The plan is to fix it with
2821 * help of 'iterate_supers_type()' which we should have in v3.0: when
2822 * a debugfs opened, we rember FS's UUID in file->private_data. Then
2823 * whenever we access the FS via a debugfs file, we iterate all UBIFS
2824 * superblocks and fine the one with the same UUID, and take the
2825 * locking right.
2826 *
2827 * The other way to go suggested by Al Viro is to create a separate
2828 * 'ubifs-debug' file-system instead.
2829 */
2830 if (file->f_path.dentry == d->dfs_dump_lprops) {
2831 ubifs_dump_lprops(c);
2832 return count;
2833 }
2834 if (file->f_path.dentry == d->dfs_dump_budg) {
2835 ubifs_dump_budg(c, &c->bi);
2836 return count;
2837 }
2838 if (file->f_path.dentry == d->dfs_dump_tnc) {
2839 mutex_lock(&c->tnc_mutex);
2840 ubifs_dump_tnc(c);
2841 mutex_unlock(&c->tnc_mutex);
2842 return count;
2843 }
2844
2845 val = interpret_user_input(u, count);
2846 if (val < 0)
2847 return val;
2848
2849 if (dent == d->dfs_chk_gen)
2850 d->chk_gen = val;
2851 else if (dent == d->dfs_chk_index)
2852 d->chk_index = val;
2853 else if (dent == d->dfs_chk_orph)
2854 d->chk_orph = val;
2855 else if (dent == d->dfs_chk_lprops)
2856 d->chk_lprops = val;
2857 else if (dent == d->dfs_chk_fs)
2858 d->chk_fs = val;
2859 else if (dent == d->dfs_tst_rcvry)
2860 d->tst_rcvry = val;
2861 else if (dent == d->dfs_ro_error)
2862 c->ro_error = !!val;
2863 else
2864 return -EINVAL;
2865
2866 return count;
2867}
2868
2869static const struct file_operations dfs_fops = {
2870 .open = dfs_file_open,
2871 .read = dfs_file_read,
2872 .write = dfs_file_write,
2873 .owner = THIS_MODULE,
2874 .llseek = no_llseek,
2875};
2876
2877/**
2878 * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
2879 * @c: UBIFS file-system description object
2880 *
2881 * This function creates all debugfs files for this instance of UBIFS. Returns
2882 * zero in case of success and a negative error code in case of failure.
2883 *
2884 * Note, the only reason we have not merged this function with the
2885 * 'ubifs_debugging_init()' function is because it is better to initialize
2886 * debugfs interfaces at the very end of the mount process, and remove them at
2887 * the very beginning of the mount process.
2888 */
2889int dbg_debugfs_init_fs(struct ubifs_info *c)
2890{
2891 int err, n;
2892 const char *fname;
2893 struct dentry *dent;
2894 struct ubifs_debug_info *d = c->dbg;
2895
2896 if (!IS_ENABLED(CONFIG_DEBUG_FS))
2897 return 0;
2898
2899 n = snprintf(d->dfs_dir_name, UBIFS_DFS_DIR_LEN + 1, UBIFS_DFS_DIR_NAME,
2900 c->vi.ubi_num, c->vi.vol_id);
2901 if (n == UBIFS_DFS_DIR_LEN) {
2902 /* The array size is too small */
2903 fname = UBIFS_DFS_DIR_NAME;
2904 dent = ERR_PTR(-EINVAL);
2905 goto out;
2906 }
2907
2908 fname = d->dfs_dir_name;
2909 dent = debugfs_create_dir(fname, dfs_rootdir);
2910 if (IS_ERR_OR_NULL(dent))
2911 goto out;
2912 d->dfs_dir = dent;
2913
2914 fname = "dump_lprops";
2915 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2916 if (IS_ERR_OR_NULL(dent))
2917 goto out_remove;
2918 d->dfs_dump_lprops = dent;
2919
2920 fname = "dump_budg";
2921 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2922 if (IS_ERR_OR_NULL(dent))
2923 goto out_remove;
2924 d->dfs_dump_budg = dent;
2925
2926 fname = "dump_tnc";
2927 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
2928 if (IS_ERR_OR_NULL(dent))
2929 goto out_remove;
2930 d->dfs_dump_tnc = dent;
2931
2932 fname = "chk_general";
2933 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2934 &dfs_fops);
2935 if (IS_ERR_OR_NULL(dent))
2936 goto out_remove;
2937 d->dfs_chk_gen = dent;
2938
2939 fname = "chk_index";
2940 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2941 &dfs_fops);
2942 if (IS_ERR_OR_NULL(dent))
2943 goto out_remove;
2944 d->dfs_chk_index = dent;
2945
2946 fname = "chk_orphans";
2947 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2948 &dfs_fops);
2949 if (IS_ERR_OR_NULL(dent))
2950 goto out_remove;
2951 d->dfs_chk_orph = dent;
2952
2953 fname = "chk_lprops";
2954 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2955 &dfs_fops);
2956 if (IS_ERR_OR_NULL(dent))
2957 goto out_remove;
2958 d->dfs_chk_lprops = dent;
2959
2960 fname = "chk_fs";
2961 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2962 &dfs_fops);
2963 if (IS_ERR_OR_NULL(dent))
2964 goto out_remove;
2965 d->dfs_chk_fs = dent;
2966
2967 fname = "tst_recovery";
2968 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2969 &dfs_fops);
2970 if (IS_ERR_OR_NULL(dent))
2971 goto out_remove;
2972 d->dfs_tst_rcvry = dent;
2973
2974 fname = "ro_error";
2975 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2976 &dfs_fops);
2977 if (IS_ERR_OR_NULL(dent))
2978 goto out_remove;
2979 d->dfs_ro_error = dent;
2980
2981 return 0;
2982
2983out_remove:
2984 debugfs_remove_recursive(d->dfs_dir);
2985out:
2986 err = dent ? PTR_ERR(dent) : -ENODEV;
Heiko Schocher94b66de2015-10-22 06:19:21 +02002987 ubifs_err(c, "cannot create \"%s\" debugfs file or directory, error %d\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +02002988 fname, err);
2989 return err;
2990}
2991
2992/**
2993 * dbg_debugfs_exit_fs - remove all debugfs files.
2994 * @c: UBIFS file-system description object
2995 */
2996void dbg_debugfs_exit_fs(struct ubifs_info *c)
2997{
2998 if (IS_ENABLED(CONFIG_DEBUG_FS))
2999 debugfs_remove_recursive(c->dbg->dfs_dir);
3000}
3001
3002struct ubifs_global_debug_info ubifs_dbg;
3003
3004static struct dentry *dfs_chk_gen;
3005static struct dentry *dfs_chk_index;
3006static struct dentry *dfs_chk_orph;
3007static struct dentry *dfs_chk_lprops;
3008static struct dentry *dfs_chk_fs;
3009static struct dentry *dfs_tst_rcvry;
3010
3011static ssize_t dfs_global_file_read(struct file *file, char __user *u,
3012 size_t count, loff_t *ppos)
3013{
3014 struct dentry *dent = file->f_path.dentry;
3015 int val;
3016
3017 if (dent == dfs_chk_gen)
3018 val = ubifs_dbg.chk_gen;
3019 else if (dent == dfs_chk_index)
3020 val = ubifs_dbg.chk_index;
3021 else if (dent == dfs_chk_orph)
3022 val = ubifs_dbg.chk_orph;
3023 else if (dent == dfs_chk_lprops)
3024 val = ubifs_dbg.chk_lprops;
3025 else if (dent == dfs_chk_fs)
3026 val = ubifs_dbg.chk_fs;
3027 else if (dent == dfs_tst_rcvry)
3028 val = ubifs_dbg.tst_rcvry;
3029 else
3030 return -EINVAL;
3031
3032 return provide_user_output(val, u, count, ppos);
3033}
3034
3035static ssize_t dfs_global_file_write(struct file *file, const char __user *u,
3036 size_t count, loff_t *ppos)
3037{
3038 struct dentry *dent = file->f_path.dentry;
3039 int val;
3040
3041 val = interpret_user_input(u, count);
3042 if (val < 0)
3043 return val;
3044
3045 if (dent == dfs_chk_gen)
3046 ubifs_dbg.chk_gen = val;
3047 else if (dent == dfs_chk_index)
3048 ubifs_dbg.chk_index = val;
3049 else if (dent == dfs_chk_orph)
3050 ubifs_dbg.chk_orph = val;
3051 else if (dent == dfs_chk_lprops)
3052 ubifs_dbg.chk_lprops = val;
3053 else if (dent == dfs_chk_fs)
3054 ubifs_dbg.chk_fs = val;
3055 else if (dent == dfs_tst_rcvry)
3056 ubifs_dbg.tst_rcvry = val;
3057 else
3058 return -EINVAL;
3059
3060 return count;
3061}
3062
3063static const struct file_operations dfs_global_fops = {
3064 .read = dfs_global_file_read,
3065 .write = dfs_global_file_write,
3066 .owner = THIS_MODULE,
3067 .llseek = no_llseek,
3068};
3069
3070/**
3071 * dbg_debugfs_init - initialize debugfs file-system.
3072 *
3073 * UBIFS uses debugfs file-system to expose various debugging knobs to
3074 * user-space. This function creates "ubifs" directory in the debugfs
3075 * file-system. Returns zero in case of success and a negative error code in
3076 * case of failure.
3077 */
3078int dbg_debugfs_init(void)
3079{
3080 int err;
3081 const char *fname;
3082 struct dentry *dent;
3083
3084 if (!IS_ENABLED(CONFIG_DEBUG_FS))
3085 return 0;
3086
3087 fname = "ubifs";
3088 dent = debugfs_create_dir(fname, NULL);
3089 if (IS_ERR_OR_NULL(dent))
3090 goto out;
3091 dfs_rootdir = dent;
3092
3093 fname = "chk_general";
3094 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3095 &dfs_global_fops);
3096 if (IS_ERR_OR_NULL(dent))
3097 goto out_remove;
3098 dfs_chk_gen = dent;
3099
3100 fname = "chk_index";
3101 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3102 &dfs_global_fops);
3103 if (IS_ERR_OR_NULL(dent))
3104 goto out_remove;
3105 dfs_chk_index = dent;
3106
3107 fname = "chk_orphans";
3108 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3109 &dfs_global_fops);
3110 if (IS_ERR_OR_NULL(dent))
3111 goto out_remove;
3112 dfs_chk_orph = dent;
3113
3114 fname = "chk_lprops";
3115 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3116 &dfs_global_fops);
3117 if (IS_ERR_OR_NULL(dent))
3118 goto out_remove;
3119 dfs_chk_lprops = dent;
3120
3121 fname = "chk_fs";
3122 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3123 &dfs_global_fops);
3124 if (IS_ERR_OR_NULL(dent))
3125 goto out_remove;
3126 dfs_chk_fs = dent;
3127
3128 fname = "tst_recovery";
3129 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3130 &dfs_global_fops);
3131 if (IS_ERR_OR_NULL(dent))
3132 goto out_remove;
3133 dfs_tst_rcvry = dent;
3134
3135 return 0;
3136
3137out_remove:
3138 debugfs_remove_recursive(dfs_rootdir);
3139out:
3140 err = dent ? PTR_ERR(dent) : -ENODEV;
Heiko Schocher94b66de2015-10-22 06:19:21 +02003141 pr_err("UBIFS error (pid %d): cannot create \"%s\" debugfs file or directory, error %d\n",
3142 current->pid, fname, err);
Heiko Schocherf5895d12014-06-24 10:10:04 +02003143 return err;
3144}
3145
3146/**
3147 * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
3148 */
3149void dbg_debugfs_exit(void)
3150{
3151 if (IS_ENABLED(CONFIG_DEBUG_FS))
3152 debugfs_remove_recursive(dfs_rootdir);
3153}
3154
3155/**
3156 * ubifs_debugging_init - initialize UBIFS debugging.
3157 * @c: UBIFS file-system description object
3158 *
3159 * This function initializes debugging-related data for the file system.
3160 * Returns zero in case of success and a negative error code in case of
3161 * failure.
3162 */
3163int ubifs_debugging_init(struct ubifs_info *c)
3164{
3165 c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
3166 if (!c->dbg)
3167 return -ENOMEM;
3168
3169 return 0;
3170}
3171
3172/**
3173 * ubifs_debugging_exit - free debugging data.
3174 * @c: UBIFS file-system description object
3175 */
3176void ubifs_debugging_exit(struct ubifs_info *c)
3177{
3178 kfree(c->dbg);
3179}
3180#endif