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