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