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