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