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