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