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