UBIFS: improve budgeting dump
[pandora-kernel.git] / fs / ubifs / debug.c
1 /*
2  * This file is part of UBIFS.
3  *
4  * Copyright (C) 2006-2008 Nokia Corporation
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published by
8  * the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 51
17  * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * Authors: Artem Bityutskiy (Битюцкий Артём)
20  *          Adrian Hunter
21  */
22
23 /*
24  * This file implements most of the debugging stuff which is compiled in only
25  * when it is enabled. But some debugging check functions are implemented in
26  * corresponding subsystem, just because they are closely related and utilize
27  * various local functions of those subsystems.
28  */
29
30 #define UBIFS_DBG_PRESERVE_UBI
31
32 #include "ubifs.h"
33 #include <linux/module.h>
34 #include <linux/moduleparam.h>
35 #include <linux/debugfs.h>
36
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         long long available, outstanding, free;
601
602         ubifs_assert(spin_is_locked(&c->space_lock));
603         spin_lock(&dbg_lock);
604         printk(KERN_DEBUG "(pid %d) Budgeting info: budg_data_growth %lld, "
605                "budg_dd_growth %lld, budg_idx_growth %lld\n", current->pid,
606                c->budg_data_growth, c->budg_dd_growth, c->budg_idx_growth);
607         printk(KERN_DEBUG "\tdata budget sum %lld, total budget sum %lld, "
608                "freeable_cnt %d\n", c->budg_data_growth + c->budg_dd_growth,
609                c->budg_data_growth + c->budg_dd_growth + c->budg_idx_growth,
610                c->freeable_cnt);
611         printk(KERN_DEBUG "\tmin_idx_lebs %d, old_idx_sz %lld, "
612                "calc_idx_sz %lld, idx_gc_cnt %d\n", c->min_idx_lebs,
613                c->old_idx_sz, c->calc_idx_sz, c->idx_gc_cnt);
614         printk(KERN_DEBUG "\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, "
615                "clean_zn_cnt %ld\n", atomic_long_read(&c->dirty_pg_cnt),
616                atomic_long_read(&c->dirty_zn_cnt),
617                atomic_long_read(&c->clean_zn_cnt));
618         printk(KERN_DEBUG "\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
619                c->dark_wm, c->dead_wm, c->max_idx_node_sz);
620         printk(KERN_DEBUG "\tgc_lnum %d, ihead_lnum %d\n",
621                c->gc_lnum, c->ihead_lnum);
622         for (i = 0; i < c->jhead_cnt; i++)
623                 printk(KERN_DEBUG "\tjhead %d\t LEB %d\n",
624                        c->jheads[i].wbuf.jhead, c->jheads[i].wbuf.lnum);
625         for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
626                 bud = rb_entry(rb, struct ubifs_bud, rb);
627                 printk(KERN_DEBUG "\tbud LEB %d\n", bud->lnum);
628         }
629         list_for_each_entry(bud, &c->old_buds, list)
630                 printk(KERN_DEBUG "\told bud LEB %d\n", bud->lnum);
631         list_for_each_entry(idx_gc, &c->idx_gc, list)
632                 printk(KERN_DEBUG "\tGC'ed idx LEB %d unmap %d\n",
633                        idx_gc->lnum, idx_gc->unmap);
634         printk(KERN_DEBUG "\tcommit state %d\n", c->cmt_state);
635
636         /* Print budgeting predictions */
637         available = ubifs_calc_available(c, c->min_idx_lebs);
638         outstanding = c->budg_data_growth + c->budg_dd_growth;
639         if (available > outstanding)
640                 free = ubifs_reported_space(c, available - outstanding);
641         else
642                 free = 0;
643         printk(KERN_DEBUG "Budgeting predictions:\n");
644         printk(KERN_DEBUG "\tavailable: %lld, outstanding %lld, free %lld\n",
645                available, outstanding, free);
646         spin_unlock(&dbg_lock);
647 }
648
649 void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
650 {
651         printk(KERN_DEBUG "LEB %d lprops: free %d, dirty %d (used %d), "
652                "flags %#x\n", lp->lnum, lp->free, lp->dirty,
653                c->leb_size - lp->free - lp->dirty, lp->flags);
654 }
655
656 void dbg_dump_lprops(struct ubifs_info *c)
657 {
658         int lnum, err;
659         struct ubifs_lprops lp;
660         struct ubifs_lp_stats lst;
661
662         printk(KERN_DEBUG "(pid %d) start dumping LEB properties\n",
663                current->pid);
664         ubifs_get_lp_stats(c, &lst);
665         dbg_dump_lstats(&lst);
666
667         for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
668                 err = ubifs_read_one_lp(c, lnum, &lp);
669                 if (err)
670                         ubifs_err("cannot read lprops for LEB %d", lnum);
671
672                 dbg_dump_lprop(c, &lp);
673         }
674         printk(KERN_DEBUG "(pid %d) finish dumping LEB properties\n",
675                current->pid);
676 }
677
678 void dbg_dump_lpt_info(struct ubifs_info *c)
679 {
680         int i;
681
682         spin_lock(&dbg_lock);
683         printk(KERN_DEBUG "(pid %d) dumping LPT information\n", current->pid);
684         printk(KERN_DEBUG "\tlpt_sz:        %lld\n", c->lpt_sz);
685         printk(KERN_DEBUG "\tpnode_sz:      %d\n", c->pnode_sz);
686         printk(KERN_DEBUG "\tnnode_sz:      %d\n", c->nnode_sz);
687         printk(KERN_DEBUG "\tltab_sz:       %d\n", c->ltab_sz);
688         printk(KERN_DEBUG "\tlsave_sz:      %d\n", c->lsave_sz);
689         printk(KERN_DEBUG "\tbig_lpt:       %d\n", c->big_lpt);
690         printk(KERN_DEBUG "\tlpt_hght:      %d\n", c->lpt_hght);
691         printk(KERN_DEBUG "\tpnode_cnt:     %d\n", c->pnode_cnt);
692         printk(KERN_DEBUG "\tnnode_cnt:     %d\n", c->nnode_cnt);
693         printk(KERN_DEBUG "\tdirty_pn_cnt:  %d\n", c->dirty_pn_cnt);
694         printk(KERN_DEBUG "\tdirty_nn_cnt:  %d\n", c->dirty_nn_cnt);
695         printk(KERN_DEBUG "\tlsave_cnt:     %d\n", c->lsave_cnt);
696         printk(KERN_DEBUG "\tspace_bits:    %d\n", c->space_bits);
697         printk(KERN_DEBUG "\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
698         printk(KERN_DEBUG "\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
699         printk(KERN_DEBUG "\tlpt_spc_bits:  %d\n", c->lpt_spc_bits);
700         printk(KERN_DEBUG "\tpcnt_bits:     %d\n", c->pcnt_bits);
701         printk(KERN_DEBUG "\tlnum_bits:     %d\n", c->lnum_bits);
702         printk(KERN_DEBUG "\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
703         printk(KERN_DEBUG "\tLPT head is at %d:%d\n",
704                c->nhead_lnum, c->nhead_offs);
705         printk(KERN_DEBUG "\tLPT ltab is at %d:%d\n", c->ltab_lnum, c->ltab_offs);
706         if (c->big_lpt)
707                 printk(KERN_DEBUG "\tLPT lsave is at %d:%d\n",
708                        c->lsave_lnum, c->lsave_offs);
709         for (i = 0; i < c->lpt_lebs; i++)
710                 printk(KERN_DEBUG "\tLPT LEB %d free %d dirty %d tgc %d "
711                        "cmt %d\n", i + c->lpt_first, c->ltab[i].free,
712                        c->ltab[i].dirty, c->ltab[i].tgc, c->ltab[i].cmt);
713         spin_unlock(&dbg_lock);
714 }
715
716 void dbg_dump_leb(const struct ubifs_info *c, int lnum)
717 {
718         struct ubifs_scan_leb *sleb;
719         struct ubifs_scan_node *snod;
720
721         if (dbg_failure_mode)
722                 return;
723
724         printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n",
725                current->pid, lnum);
726         sleb = ubifs_scan(c, lnum, 0, c->dbg->buf);
727         if (IS_ERR(sleb)) {
728                 ubifs_err("scan error %d", (int)PTR_ERR(sleb));
729                 return;
730         }
731
732         printk(KERN_DEBUG "LEB %d has %d nodes ending at %d\n", lnum,
733                sleb->nodes_cnt, sleb->endpt);
734
735         list_for_each_entry(snod, &sleb->nodes, list) {
736                 cond_resched();
737                 printk(KERN_DEBUG "Dumping node at LEB %d:%d len %d\n", lnum,
738                        snod->offs, snod->len);
739                 dbg_dump_node(c, snod->node);
740         }
741
742         printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n",
743                current->pid, lnum);
744         ubifs_scan_destroy(sleb);
745         return;
746 }
747
748 void dbg_dump_znode(const struct ubifs_info *c,
749                     const struct ubifs_znode *znode)
750 {
751         int n;
752         const struct ubifs_zbranch *zbr;
753
754         spin_lock(&dbg_lock);
755         if (znode->parent)
756                 zbr = &znode->parent->zbranch[znode->iip];
757         else
758                 zbr = &c->zroot;
759
760         printk(KERN_DEBUG "znode %p, LEB %d:%d len %d parent %p iip %d level %d"
761                " child_cnt %d flags %lx\n", znode, zbr->lnum, zbr->offs,
762                zbr->len, znode->parent, znode->iip, znode->level,
763                znode->child_cnt, znode->flags);
764
765         if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
766                 spin_unlock(&dbg_lock);
767                 return;
768         }
769
770         printk(KERN_DEBUG "zbranches:\n");
771         for (n = 0; n < znode->child_cnt; n++) {
772                 zbr = &znode->zbranch[n];
773                 if (znode->level > 0)
774                         printk(KERN_DEBUG "\t%d: znode %p LEB %d:%d len %d key "
775                                           "%s\n", n, zbr->znode, zbr->lnum,
776                                           zbr->offs, zbr->len,
777                                           DBGKEY(&zbr->key));
778                 else
779                         printk(KERN_DEBUG "\t%d: LNC %p LEB %d:%d len %d key "
780                                           "%s\n", n, zbr->znode, zbr->lnum,
781                                           zbr->offs, zbr->len,
782                                           DBGKEY(&zbr->key));
783         }
784         spin_unlock(&dbg_lock);
785 }
786
787 void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
788 {
789         int i;
790
791         printk(KERN_DEBUG "(pid %d) start dumping heap cat %d (%d elements)\n",
792                current->pid, cat, heap->cnt);
793         for (i = 0; i < heap->cnt; i++) {
794                 struct ubifs_lprops *lprops = heap->arr[i];
795
796                 printk(KERN_DEBUG "\t%d. LEB %d hpos %d free %d dirty %d "
797                        "flags %d\n", i, lprops->lnum, lprops->hpos,
798                        lprops->free, lprops->dirty, lprops->flags);
799         }
800         printk(KERN_DEBUG "(pid %d) finish dumping heap\n", current->pid);
801 }
802
803 void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
804                     struct ubifs_nnode *parent, int iip)
805 {
806         int i;
807
808         printk(KERN_DEBUG "(pid %d) dumping pnode:\n", current->pid);
809         printk(KERN_DEBUG "\taddress %zx parent %zx cnext %zx\n",
810                (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
811         printk(KERN_DEBUG "\tflags %lu iip %d level %d num %d\n",
812                pnode->flags, iip, pnode->level, pnode->num);
813         for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
814                 struct ubifs_lprops *lp = &pnode->lprops[i];
815
816                 printk(KERN_DEBUG "\t%d: free %d dirty %d flags %d lnum %d\n",
817                        i, lp->free, lp->dirty, lp->flags, lp->lnum);
818         }
819 }
820
821 void dbg_dump_tnc(struct ubifs_info *c)
822 {
823         struct ubifs_znode *znode;
824         int level;
825
826         printk(KERN_DEBUG "\n");
827         printk(KERN_DEBUG "(pid %d) start dumping TNC tree\n", current->pid);
828         znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
829         level = znode->level;
830         printk(KERN_DEBUG "== Level %d ==\n", level);
831         while (znode) {
832                 if (level != znode->level) {
833                         level = znode->level;
834                         printk(KERN_DEBUG "== Level %d ==\n", level);
835                 }
836                 dbg_dump_znode(c, znode);
837                 znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
838         }
839         printk(KERN_DEBUG "(pid %d) finish dumping TNC tree\n", current->pid);
840 }
841
842 static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
843                       void *priv)
844 {
845         dbg_dump_znode(c, znode);
846         return 0;
847 }
848
849 /**
850  * dbg_dump_index - dump the on-flash index.
851  * @c: UBIFS file-system description object
852  *
853  * This function dumps whole UBIFS indexing B-tree, unlike 'dbg_dump_tnc()'
854  * which dumps only in-memory znodes and does not read znodes which from flash.
855  */
856 void dbg_dump_index(struct ubifs_info *c)
857 {
858         dbg_walk_index(c, NULL, dump_znode, NULL);
859 }
860
861 /**
862  * dbg_check_synced_i_size - check synchronized inode size.
863  * @inode: inode to check
864  *
865  * If inode is clean, synchronized inode size has to be equivalent to current
866  * inode size. This function has to be called only for locked inodes (@i_mutex
867  * has to be locked). Returns %0 if synchronized inode size if correct, and
868  * %-EINVAL if not.
869  */
870 int dbg_check_synced_i_size(struct inode *inode)
871 {
872         int err = 0;
873         struct ubifs_inode *ui = ubifs_inode(inode);
874
875         if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
876                 return 0;
877         if (!S_ISREG(inode->i_mode))
878                 return 0;
879
880         mutex_lock(&ui->ui_mutex);
881         spin_lock(&ui->ui_lock);
882         if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
883                 ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode "
884                           "is clean", ui->ui_size, ui->synced_i_size);
885                 ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
886                           inode->i_mode, i_size_read(inode));
887                 dbg_dump_stack();
888                 err = -EINVAL;
889         }
890         spin_unlock(&ui->ui_lock);
891         mutex_unlock(&ui->ui_mutex);
892         return err;
893 }
894
895 /*
896  * dbg_check_dir - check directory inode size and link count.
897  * @c: UBIFS file-system description object
898  * @dir: the directory to calculate size for
899  * @size: the result is returned here
900  *
901  * This function makes sure that directory size and link count are correct.
902  * Returns zero in case of success and a negative error code in case of
903  * failure.
904  *
905  * Note, it is good idea to make sure the @dir->i_mutex is locked before
906  * calling this function.
907  */
908 int dbg_check_dir_size(struct ubifs_info *c, const struct inode *dir)
909 {
910         unsigned int nlink = 2;
911         union ubifs_key key;
912         struct ubifs_dent_node *dent, *pdent = NULL;
913         struct qstr nm = { .name = NULL };
914         loff_t size = UBIFS_INO_NODE_SZ;
915
916         if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
917                 return 0;
918
919         if (!S_ISDIR(dir->i_mode))
920                 return 0;
921
922         lowest_dent_key(c, &key, dir->i_ino);
923         while (1) {
924                 int err;
925
926                 dent = ubifs_tnc_next_ent(c, &key, &nm);
927                 if (IS_ERR(dent)) {
928                         err = PTR_ERR(dent);
929                         if (err == -ENOENT)
930                                 break;
931                         return err;
932                 }
933
934                 nm.name = dent->name;
935                 nm.len = le16_to_cpu(dent->nlen);
936                 size += CALC_DENT_SIZE(nm.len);
937                 if (dent->type == UBIFS_ITYPE_DIR)
938                         nlink += 1;
939                 kfree(pdent);
940                 pdent = dent;
941                 key_read(c, &dent->key, &key);
942         }
943         kfree(pdent);
944
945         if (i_size_read(dir) != size) {
946                 ubifs_err("directory inode %lu has size %llu, "
947                           "but calculated size is %llu", dir->i_ino,
948                           (unsigned long long)i_size_read(dir),
949                           (unsigned long long)size);
950                 dump_stack();
951                 return -EINVAL;
952         }
953         if (dir->i_nlink != nlink) {
954                 ubifs_err("directory inode %lu has nlink %u, but calculated "
955                           "nlink is %u", dir->i_ino, dir->i_nlink, nlink);
956                 dump_stack();
957                 return -EINVAL;
958         }
959
960         return 0;
961 }
962
963 /**
964  * dbg_check_key_order - make sure that colliding keys are properly ordered.
965  * @c: UBIFS file-system description object
966  * @zbr1: first zbranch
967  * @zbr2: following zbranch
968  *
969  * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
970  * names of the direntries/xentries which are referred by the keys. This
971  * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
972  * sure the name of direntry/xentry referred by @zbr1 is less than
973  * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
974  * and a negative error code in case of failure.
975  */
976 static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
977                                struct ubifs_zbranch *zbr2)
978 {
979         int err, nlen1, nlen2, cmp;
980         struct ubifs_dent_node *dent1, *dent2;
981         union ubifs_key key;
982
983         ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
984         dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
985         if (!dent1)
986                 return -ENOMEM;
987         dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
988         if (!dent2) {
989                 err = -ENOMEM;
990                 goto out_free;
991         }
992
993         err = ubifs_tnc_read_node(c, zbr1, dent1);
994         if (err)
995                 goto out_free;
996         err = ubifs_validate_entry(c, dent1);
997         if (err)
998                 goto out_free;
999
1000         err = ubifs_tnc_read_node(c, zbr2, dent2);
1001         if (err)
1002                 goto out_free;
1003         err = ubifs_validate_entry(c, dent2);
1004         if (err)
1005                 goto out_free;
1006
1007         /* Make sure node keys are the same as in zbranch */
1008         err = 1;
1009         key_read(c, &dent1->key, &key);
1010         if (keys_cmp(c, &zbr1->key, &key)) {
1011                 ubifs_err("1st entry at %d:%d has key %s", zbr1->lnum,
1012                           zbr1->offs, DBGKEY(&key));
1013                 ubifs_err("but it should have key %s according to tnc",
1014                           DBGKEY(&zbr1->key));
1015                 dbg_dump_node(c, dent1);
1016                 goto out_free;
1017         }
1018
1019         key_read(c, &dent2->key, &key);
1020         if (keys_cmp(c, &zbr2->key, &key)) {
1021                 ubifs_err("2nd entry at %d:%d has key %s", zbr1->lnum,
1022                           zbr1->offs, DBGKEY(&key));
1023                 ubifs_err("but it should have key %s according to tnc",
1024                           DBGKEY(&zbr2->key));
1025                 dbg_dump_node(c, dent2);
1026                 goto out_free;
1027         }
1028
1029         nlen1 = le16_to_cpu(dent1->nlen);
1030         nlen2 = le16_to_cpu(dent2->nlen);
1031
1032         cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
1033         if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
1034                 err = 0;
1035                 goto out_free;
1036         }
1037         if (cmp == 0 && nlen1 == nlen2)
1038                 ubifs_err("2 xent/dent nodes with the same name");
1039         else
1040                 ubifs_err("bad order of colliding key %s",
1041                         DBGKEY(&key));
1042
1043         ubifs_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs);
1044         dbg_dump_node(c, dent1);
1045         ubifs_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs);
1046         dbg_dump_node(c, dent2);
1047
1048 out_free:
1049         kfree(dent2);
1050         kfree(dent1);
1051         return err;
1052 }
1053
1054 /**
1055  * dbg_check_znode - check if znode is all right.
1056  * @c: UBIFS file-system description object
1057  * @zbr: zbranch which points to this znode
1058  *
1059  * This function makes sure that znode referred to by @zbr is all right.
1060  * Returns zero if it is, and %-EINVAL if it is not.
1061  */
1062 static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
1063 {
1064         struct ubifs_znode *znode = zbr->znode;
1065         struct ubifs_znode *zp = znode->parent;
1066         int n, err, cmp;
1067
1068         if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
1069                 err = 1;
1070                 goto out;
1071         }
1072         if (znode->level < 0) {
1073                 err = 2;
1074                 goto out;
1075         }
1076         if (znode->iip < 0 || znode->iip >= c->fanout) {
1077                 err = 3;
1078                 goto out;
1079         }
1080
1081         if (zbr->len == 0)
1082                 /* Only dirty zbranch may have no on-flash nodes */
1083                 if (!ubifs_zn_dirty(znode)) {
1084                         err = 4;
1085                         goto out;
1086                 }
1087
1088         if (ubifs_zn_dirty(znode)) {
1089                 /*
1090                  * If znode is dirty, its parent has to be dirty as well. The
1091                  * order of the operation is important, so we have to have
1092                  * memory barriers.
1093                  */
1094                 smp_mb();
1095                 if (zp && !ubifs_zn_dirty(zp)) {
1096                         /*
1097                          * The dirty flag is atomic and is cleared outside the
1098                          * TNC mutex, so znode's dirty flag may now have
1099                          * been cleared. The child is always cleared before the
1100                          * parent, so we just need to check again.
1101                          */
1102                         smp_mb();
1103                         if (ubifs_zn_dirty(znode)) {
1104                                 err = 5;
1105                                 goto out;
1106                         }
1107                 }
1108         }
1109
1110         if (zp) {
1111                 const union ubifs_key *min, *max;
1112
1113                 if (znode->level != zp->level - 1) {
1114                         err = 6;
1115                         goto out;
1116                 }
1117
1118                 /* Make sure the 'parent' pointer in our znode is correct */
1119                 err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
1120                 if (!err) {
1121                         /* This zbranch does not exist in the parent */
1122                         err = 7;
1123                         goto out;
1124                 }
1125
1126                 if (znode->iip >= zp->child_cnt) {
1127                         err = 8;
1128                         goto out;
1129                 }
1130
1131                 if (znode->iip != n) {
1132                         /* This may happen only in case of collisions */
1133                         if (keys_cmp(c, &zp->zbranch[n].key,
1134                                      &zp->zbranch[znode->iip].key)) {
1135                                 err = 9;
1136                                 goto out;
1137                         }
1138                         n = znode->iip;
1139                 }
1140
1141                 /*
1142                  * Make sure that the first key in our znode is greater than or
1143                  * equal to the key in the pointing zbranch.
1144                  */
1145                 min = &zbr->key;
1146                 cmp = keys_cmp(c, min, &znode->zbranch[0].key);
1147                 if (cmp == 1) {
1148                         err = 10;
1149                         goto out;
1150                 }
1151
1152                 if (n + 1 < zp->child_cnt) {
1153                         max = &zp->zbranch[n + 1].key;
1154
1155                         /*
1156                          * Make sure the last key in our znode is less or
1157                          * equivalent than the the key in zbranch which goes
1158                          * after our pointing zbranch.
1159                          */
1160                         cmp = keys_cmp(c, max,
1161                                 &znode->zbranch[znode->child_cnt - 1].key);
1162                         if (cmp == -1) {
1163                                 err = 11;
1164                                 goto out;
1165                         }
1166                 }
1167         } else {
1168                 /* This may only be root znode */
1169                 if (zbr != &c->zroot) {
1170                         err = 12;
1171                         goto out;
1172                 }
1173         }
1174
1175         /*
1176          * Make sure that next key is greater or equivalent then the previous
1177          * one.
1178          */
1179         for (n = 1; n < znode->child_cnt; n++) {
1180                 cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
1181                                &znode->zbranch[n].key);
1182                 if (cmp > 0) {
1183                         err = 13;
1184                         goto out;
1185                 }
1186                 if (cmp == 0) {
1187                         /* This can only be keys with colliding hash */
1188                         if (!is_hash_key(c, &znode->zbranch[n].key)) {
1189                                 err = 14;
1190                                 goto out;
1191                         }
1192
1193                         if (znode->level != 0 || c->replaying)
1194                                 continue;
1195
1196                         /*
1197                          * Colliding keys should follow binary order of
1198                          * corresponding xentry/dentry names.
1199                          */
1200                         err = dbg_check_key_order(c, &znode->zbranch[n - 1],
1201                                                   &znode->zbranch[n]);
1202                         if (err < 0)
1203                                 return err;
1204                         if (err) {
1205                                 err = 15;
1206                                 goto out;
1207                         }
1208                 }
1209         }
1210
1211         for (n = 0; n < znode->child_cnt; n++) {
1212                 if (!znode->zbranch[n].znode &&
1213                     (znode->zbranch[n].lnum == 0 ||
1214                      znode->zbranch[n].len == 0)) {
1215                         err = 16;
1216                         goto out;
1217                 }
1218
1219                 if (znode->zbranch[n].lnum != 0 &&
1220                     znode->zbranch[n].len == 0) {
1221                         err = 17;
1222                         goto out;
1223                 }
1224
1225                 if (znode->zbranch[n].lnum == 0 &&
1226                     znode->zbranch[n].len != 0) {
1227                         err = 18;
1228                         goto out;
1229                 }
1230
1231                 if (znode->zbranch[n].lnum == 0 &&
1232                     znode->zbranch[n].offs != 0) {
1233                         err = 19;
1234                         goto out;
1235                 }
1236
1237                 if (znode->level != 0 && znode->zbranch[n].znode)
1238                         if (znode->zbranch[n].znode->parent != znode) {
1239                                 err = 20;
1240                                 goto out;
1241                         }
1242         }
1243
1244         return 0;
1245
1246 out:
1247         ubifs_err("failed, error %d", err);
1248         ubifs_msg("dump of the znode");
1249         dbg_dump_znode(c, znode);
1250         if (zp) {
1251                 ubifs_msg("dump of the parent znode");
1252                 dbg_dump_znode(c, zp);
1253         }
1254         dump_stack();
1255         return -EINVAL;
1256 }
1257
1258 /**
1259  * dbg_check_tnc - check TNC tree.
1260  * @c: UBIFS file-system description object
1261  * @extra: do extra checks that are possible at start commit
1262  *
1263  * This function traverses whole TNC tree and checks every znode. Returns zero
1264  * if everything is all right and %-EINVAL if something is wrong with TNC.
1265  */
1266 int dbg_check_tnc(struct ubifs_info *c, int extra)
1267 {
1268         struct ubifs_znode *znode;
1269         long clean_cnt = 0, dirty_cnt = 0;
1270         int err, last;
1271
1272         if (!(ubifs_chk_flags & UBIFS_CHK_TNC))
1273                 return 0;
1274
1275         ubifs_assert(mutex_is_locked(&c->tnc_mutex));
1276         if (!c->zroot.znode)
1277                 return 0;
1278
1279         znode = ubifs_tnc_postorder_first(c->zroot.znode);
1280         while (1) {
1281                 struct ubifs_znode *prev;
1282                 struct ubifs_zbranch *zbr;
1283
1284                 if (!znode->parent)
1285                         zbr = &c->zroot;
1286                 else
1287                         zbr = &znode->parent->zbranch[znode->iip];
1288
1289                 err = dbg_check_znode(c, zbr);
1290                 if (err)
1291                         return err;
1292
1293                 if (extra) {
1294                         if (ubifs_zn_dirty(znode))
1295                                 dirty_cnt += 1;
1296                         else
1297                                 clean_cnt += 1;
1298                 }
1299
1300                 prev = znode;
1301                 znode = ubifs_tnc_postorder_next(znode);
1302                 if (!znode)
1303                         break;
1304
1305                 /*
1306                  * If the last key of this znode is equivalent to the first key
1307                  * of the next znode (collision), then check order of the keys.
1308                  */
1309                 last = prev->child_cnt - 1;
1310                 if (prev->level == 0 && znode->level == 0 && !c->replaying &&
1311                     !keys_cmp(c, &prev->zbranch[last].key,
1312                               &znode->zbranch[0].key)) {
1313                         err = dbg_check_key_order(c, &prev->zbranch[last],
1314                                                   &znode->zbranch[0]);
1315                         if (err < 0)
1316                                 return err;
1317                         if (err) {
1318                                 ubifs_msg("first znode");
1319                                 dbg_dump_znode(c, prev);
1320                                 ubifs_msg("second znode");
1321                                 dbg_dump_znode(c, znode);
1322                                 return -EINVAL;
1323                         }
1324                 }
1325         }
1326
1327         if (extra) {
1328                 if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
1329                         ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld",
1330                                   atomic_long_read(&c->clean_zn_cnt),
1331                                   clean_cnt);
1332                         return -EINVAL;
1333                 }
1334                 if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
1335                         ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld",
1336                                   atomic_long_read(&c->dirty_zn_cnt),
1337                                   dirty_cnt);
1338                         return -EINVAL;
1339                 }
1340         }
1341
1342         return 0;
1343 }
1344
1345 /**
1346  * dbg_walk_index - walk the on-flash index.
1347  * @c: UBIFS file-system description object
1348  * @leaf_cb: called for each leaf node
1349  * @znode_cb: called for each indexing node
1350  * @priv: private date which is passed to callbacks
1351  *
1352  * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1353  * node and @znode_cb for each indexing node. Returns zero in case of success
1354  * and a negative error code in case of failure.
1355  *
1356  * It would be better if this function removed every znode it pulled to into
1357  * the TNC, so that the behavior more closely matched the non-debugging
1358  * behavior.
1359  */
1360 int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1361                    dbg_znode_callback znode_cb, void *priv)
1362 {
1363         int err;
1364         struct ubifs_zbranch *zbr;
1365         struct ubifs_znode *znode, *child;
1366
1367         mutex_lock(&c->tnc_mutex);
1368         /* If the root indexing node is not in TNC - pull it */
1369         if (!c->zroot.znode) {
1370                 c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1371                 if (IS_ERR(c->zroot.znode)) {
1372                         err = PTR_ERR(c->zroot.znode);
1373                         c->zroot.znode = NULL;
1374                         goto out_unlock;
1375                 }
1376         }
1377
1378         /*
1379          * We are going to traverse the indexing tree in the postorder manner.
1380          * Go down and find the leftmost indexing node where we are going to
1381          * start from.
1382          */
1383         znode = c->zroot.znode;
1384         while (znode->level > 0) {
1385                 zbr = &znode->zbranch[0];
1386                 child = zbr->znode;
1387                 if (!child) {
1388                         child = ubifs_load_znode(c, zbr, znode, 0);
1389                         if (IS_ERR(child)) {
1390                                 err = PTR_ERR(child);
1391                                 goto out_unlock;
1392                         }
1393                         zbr->znode = child;
1394                 }
1395
1396                 znode = child;
1397         }
1398
1399         /* Iterate over all indexing nodes */
1400         while (1) {
1401                 int idx;
1402
1403                 cond_resched();
1404
1405                 if (znode_cb) {
1406                         err = znode_cb(c, znode, priv);
1407                         if (err) {
1408                                 ubifs_err("znode checking function returned "
1409                                           "error %d", err);
1410                                 dbg_dump_znode(c, znode);
1411                                 goto out_dump;
1412                         }
1413                 }
1414                 if (leaf_cb && znode->level == 0) {
1415                         for (idx = 0; idx < znode->child_cnt; idx++) {
1416                                 zbr = &znode->zbranch[idx];
1417                                 err = leaf_cb(c, zbr, priv);
1418                                 if (err) {
1419                                         ubifs_err("leaf checking function "
1420                                                   "returned error %d, for leaf "
1421                                                   "at LEB %d:%d",
1422                                                   err, zbr->lnum, zbr->offs);
1423                                         goto out_dump;
1424                                 }
1425                         }
1426                 }
1427
1428                 if (!znode->parent)
1429                         break;
1430
1431                 idx = znode->iip + 1;
1432                 znode = znode->parent;
1433                 if (idx < znode->child_cnt) {
1434                         /* Switch to the next index in the parent */
1435                         zbr = &znode->zbranch[idx];
1436                         child = zbr->znode;
1437                         if (!child) {
1438                                 child = ubifs_load_znode(c, zbr, znode, idx);
1439                                 if (IS_ERR(child)) {
1440                                         err = PTR_ERR(child);
1441                                         goto out_unlock;
1442                                 }
1443                                 zbr->znode = child;
1444                         }
1445                         znode = child;
1446                 } else
1447                         /*
1448                          * This is the last child, switch to the parent and
1449                          * continue.
1450                          */
1451                         continue;
1452
1453                 /* Go to the lowest leftmost znode in the new sub-tree */
1454                 while (znode->level > 0) {
1455                         zbr = &znode->zbranch[0];
1456                         child = zbr->znode;
1457                         if (!child) {
1458                                 child = ubifs_load_znode(c, zbr, znode, 0);
1459                                 if (IS_ERR(child)) {
1460                                         err = PTR_ERR(child);
1461                                         goto out_unlock;
1462                                 }
1463                                 zbr->znode = child;
1464                         }
1465                         znode = child;
1466                 }
1467         }
1468
1469         mutex_unlock(&c->tnc_mutex);
1470         return 0;
1471
1472 out_dump:
1473         if (znode->parent)
1474                 zbr = &znode->parent->zbranch[znode->iip];
1475         else
1476                 zbr = &c->zroot;
1477         ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1478         dbg_dump_znode(c, znode);
1479 out_unlock:
1480         mutex_unlock(&c->tnc_mutex);
1481         return err;
1482 }
1483
1484 /**
1485  * add_size - add znode size to partially calculated index size.
1486  * @c: UBIFS file-system description object
1487  * @znode: znode to add size for
1488  * @priv: partially calculated index size
1489  *
1490  * This is a helper function for 'dbg_check_idx_size()' which is called for
1491  * every indexing node and adds its size to the 'long long' variable pointed to
1492  * by @priv.
1493  */
1494 static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1495 {
1496         long long *idx_size = priv;
1497         int add;
1498
1499         add = ubifs_idx_node_sz(c, znode->child_cnt);
1500         add = ALIGN(add, 8);
1501         *idx_size += add;
1502         return 0;
1503 }
1504
1505 /**
1506  * dbg_check_idx_size - check index size.
1507  * @c: UBIFS file-system description object
1508  * @idx_size: size to check
1509  *
1510  * This function walks the UBIFS index, calculates its size and checks that the
1511  * size is equivalent to @idx_size. Returns zero in case of success and a
1512  * negative error code in case of failure.
1513  */
1514 int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
1515 {
1516         int err;
1517         long long calc = 0;
1518
1519         if (!(ubifs_chk_flags & UBIFS_CHK_IDX_SZ))
1520                 return 0;
1521
1522         err = dbg_walk_index(c, NULL, add_size, &calc);
1523         if (err) {
1524                 ubifs_err("error %d while walking the index", err);
1525                 return err;
1526         }
1527
1528         if (calc != idx_size) {
1529                 ubifs_err("index size check failed: calculated size is %lld, "
1530                           "should be %lld", calc, idx_size);
1531                 dump_stack();
1532                 return -EINVAL;
1533         }
1534
1535         return 0;
1536 }
1537
1538 /**
1539  * struct fsck_inode - information about an inode used when checking the file-system.
1540  * @rb: link in the RB-tree of inodes
1541  * @inum: inode number
1542  * @mode: inode type, permissions, etc
1543  * @nlink: inode link count
1544  * @xattr_cnt: count of extended attributes
1545  * @references: how many directory/xattr entries refer this inode (calculated
1546  *              while walking the index)
1547  * @calc_cnt: for directory inode count of child directories
1548  * @size: inode size (read from on-flash inode)
1549  * @xattr_sz: summary size of all extended attributes (read from on-flash
1550  *            inode)
1551  * @calc_sz: for directories calculated directory size
1552  * @calc_xcnt: count of extended attributes
1553  * @calc_xsz: calculated summary size of all extended attributes
1554  * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1555  *             inode (read from on-flash inode)
1556  * @calc_xnms: calculated sum of lengths of all extended attribute names
1557  */
1558 struct fsck_inode {
1559         struct rb_node rb;
1560         ino_t inum;
1561         umode_t mode;
1562         unsigned int nlink;
1563         unsigned int xattr_cnt;
1564         int references;
1565         int calc_cnt;
1566         long long size;
1567         unsigned int xattr_sz;
1568         long long calc_sz;
1569         long long calc_xcnt;
1570         long long calc_xsz;
1571         unsigned int xattr_nms;
1572         long long calc_xnms;
1573 };
1574
1575 /**
1576  * struct fsck_data - private FS checking information.
1577  * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1578  */
1579 struct fsck_data {
1580         struct rb_root inodes;
1581 };
1582
1583 /**
1584  * add_inode - add inode information to RB-tree of inodes.
1585  * @c: UBIFS file-system description object
1586  * @fsckd: FS checking information
1587  * @ino: raw UBIFS inode to add
1588  *
1589  * This is a helper function for 'check_leaf()' which adds information about
1590  * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1591  * case of success and a negative error code in case of failure.
1592  */
1593 static struct fsck_inode *add_inode(struct ubifs_info *c,
1594                                     struct fsck_data *fsckd,
1595                                     struct ubifs_ino_node *ino)
1596 {
1597         struct rb_node **p, *parent = NULL;
1598         struct fsck_inode *fscki;
1599         ino_t inum = key_inum_flash(c, &ino->key);
1600
1601         p = &fsckd->inodes.rb_node;
1602         while (*p) {
1603                 parent = *p;
1604                 fscki = rb_entry(parent, struct fsck_inode, rb);
1605                 if (inum < fscki->inum)
1606                         p = &(*p)->rb_left;
1607                 else if (inum > fscki->inum)
1608                         p = &(*p)->rb_right;
1609                 else
1610                         return fscki;
1611         }
1612
1613         if (inum > c->highest_inum) {
1614                 ubifs_err("too high inode number, max. is %lu",
1615                           (unsigned long)c->highest_inum);
1616                 return ERR_PTR(-EINVAL);
1617         }
1618
1619         fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
1620         if (!fscki)
1621                 return ERR_PTR(-ENOMEM);
1622
1623         fscki->inum = inum;
1624         fscki->nlink = le32_to_cpu(ino->nlink);
1625         fscki->size = le64_to_cpu(ino->size);
1626         fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
1627         fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
1628         fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
1629         fscki->mode = le32_to_cpu(ino->mode);
1630         if (S_ISDIR(fscki->mode)) {
1631                 fscki->calc_sz = UBIFS_INO_NODE_SZ;
1632                 fscki->calc_cnt = 2;
1633         }
1634         rb_link_node(&fscki->rb, parent, p);
1635         rb_insert_color(&fscki->rb, &fsckd->inodes);
1636         return fscki;
1637 }
1638
1639 /**
1640  * search_inode - search inode in the RB-tree of inodes.
1641  * @fsckd: FS checking information
1642  * @inum: inode number to search
1643  *
1644  * This is a helper function for 'check_leaf()' which searches inode @inum in
1645  * the RB-tree of inodes and returns an inode information pointer or %NULL if
1646  * the inode was not found.
1647  */
1648 static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
1649 {
1650         struct rb_node *p;
1651         struct fsck_inode *fscki;
1652
1653         p = fsckd->inodes.rb_node;
1654         while (p) {
1655                 fscki = rb_entry(p, struct fsck_inode, rb);
1656                 if (inum < fscki->inum)
1657                         p = p->rb_left;
1658                 else if (inum > fscki->inum)
1659                         p = p->rb_right;
1660                 else
1661                         return fscki;
1662         }
1663         return NULL;
1664 }
1665
1666 /**
1667  * read_add_inode - read inode node and add it to RB-tree of inodes.
1668  * @c: UBIFS file-system description object
1669  * @fsckd: FS checking information
1670  * @inum: inode number to read
1671  *
1672  * This is a helper function for 'check_leaf()' which finds inode node @inum in
1673  * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1674  * information pointer in case of success and a negative error code in case of
1675  * failure.
1676  */
1677 static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1678                                          struct fsck_data *fsckd, ino_t inum)
1679 {
1680         int n, err;
1681         union ubifs_key key;
1682         struct ubifs_znode *znode;
1683         struct ubifs_zbranch *zbr;
1684         struct ubifs_ino_node *ino;
1685         struct fsck_inode *fscki;
1686
1687         fscki = search_inode(fsckd, inum);
1688         if (fscki)
1689                 return fscki;
1690
1691         ino_key_init(c, &key, inum);
1692         err = ubifs_lookup_level0(c, &key, &znode, &n);
1693         if (!err) {
1694                 ubifs_err("inode %lu not found in index", (unsigned long)inum);
1695                 return ERR_PTR(-ENOENT);
1696         } else if (err < 0) {
1697                 ubifs_err("error %d while looking up inode %lu",
1698                           err, (unsigned long)inum);
1699                 return ERR_PTR(err);
1700         }
1701
1702         zbr = &znode->zbranch[n];
1703         if (zbr->len < UBIFS_INO_NODE_SZ) {
1704                 ubifs_err("bad node %lu node length %d",
1705                           (unsigned long)inum, zbr->len);
1706                 return ERR_PTR(-EINVAL);
1707         }
1708
1709         ino = kmalloc(zbr->len, GFP_NOFS);
1710         if (!ino)
1711                 return ERR_PTR(-ENOMEM);
1712
1713         err = ubifs_tnc_read_node(c, zbr, ino);
1714         if (err) {
1715                 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
1716                           zbr->lnum, zbr->offs, err);
1717                 kfree(ino);
1718                 return ERR_PTR(err);
1719         }
1720
1721         fscki = add_inode(c, fsckd, ino);
1722         kfree(ino);
1723         if (IS_ERR(fscki)) {
1724                 ubifs_err("error %ld while adding inode %lu node",
1725                           PTR_ERR(fscki), (unsigned long)inum);
1726                 return fscki;
1727         }
1728
1729         return fscki;
1730 }
1731
1732 /**
1733  * check_leaf - check leaf node.
1734  * @c: UBIFS file-system description object
1735  * @zbr: zbranch of the leaf node to check
1736  * @priv: FS checking information
1737  *
1738  * This is a helper function for 'dbg_check_filesystem()' which is called for
1739  * every single leaf node while walking the indexing tree. It checks that the
1740  * leaf node referred from the indexing tree exists, has correct CRC, and does
1741  * some other basic validation. This function is also responsible for building
1742  * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
1743  * calculates reference count, size, etc for each inode in order to later
1744  * compare them to the information stored inside the inodes and detect possible
1745  * inconsistencies. Returns zero in case of success and a negative error code
1746  * in case of failure.
1747  */
1748 static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
1749                       void *priv)
1750 {
1751         ino_t inum;
1752         void *node;
1753         struct ubifs_ch *ch;
1754         int err, type = key_type(c, &zbr->key);
1755         struct fsck_inode *fscki;
1756
1757         if (zbr->len < UBIFS_CH_SZ) {
1758                 ubifs_err("bad leaf length %d (LEB %d:%d)",
1759                           zbr->len, zbr->lnum, zbr->offs);
1760                 return -EINVAL;
1761         }
1762
1763         node = kmalloc(zbr->len, GFP_NOFS);
1764         if (!node)
1765                 return -ENOMEM;
1766
1767         err = ubifs_tnc_read_node(c, zbr, node);
1768         if (err) {
1769                 ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
1770                           zbr->lnum, zbr->offs, err);
1771                 goto out_free;
1772         }
1773
1774         /* If this is an inode node, add it to RB-tree of inodes */
1775         if (type == UBIFS_INO_KEY) {
1776                 fscki = add_inode(c, priv, node);
1777                 if (IS_ERR(fscki)) {
1778                         err = PTR_ERR(fscki);
1779                         ubifs_err("error %d while adding inode node", err);
1780                         goto out_dump;
1781                 }
1782                 goto out;
1783         }
1784
1785         if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
1786             type != UBIFS_DATA_KEY) {
1787                 ubifs_err("unexpected node type %d at LEB %d:%d",
1788                           type, zbr->lnum, zbr->offs);
1789                 err = -EINVAL;
1790                 goto out_free;
1791         }
1792
1793         ch = node;
1794         if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
1795                 ubifs_err("too high sequence number, max. is %llu",
1796                           c->max_sqnum);
1797                 err = -EINVAL;
1798                 goto out_dump;
1799         }
1800
1801         if (type == UBIFS_DATA_KEY) {
1802                 long long blk_offs;
1803                 struct ubifs_data_node *dn = node;
1804
1805                 /*
1806                  * Search the inode node this data node belongs to and insert
1807                  * it to the RB-tree of inodes.
1808                  */
1809                 inum = key_inum_flash(c, &dn->key);
1810                 fscki = read_add_inode(c, priv, inum);
1811                 if (IS_ERR(fscki)) {
1812                         err = PTR_ERR(fscki);
1813                         ubifs_err("error %d while processing data node and "
1814                                   "trying to find inode node %lu",
1815                                   err, (unsigned long)inum);
1816                         goto out_dump;
1817                 }
1818
1819                 /* Make sure the data node is within inode size */
1820                 blk_offs = key_block_flash(c, &dn->key);
1821                 blk_offs <<= UBIFS_BLOCK_SHIFT;
1822                 blk_offs += le32_to_cpu(dn->size);
1823                 if (blk_offs > fscki->size) {
1824                         ubifs_err("data node at LEB %d:%d is not within inode "
1825                                   "size %lld", zbr->lnum, zbr->offs,
1826                                   fscki->size);
1827                         err = -EINVAL;
1828                         goto out_dump;
1829                 }
1830         } else {
1831                 int nlen;
1832                 struct ubifs_dent_node *dent = node;
1833                 struct fsck_inode *fscki1;
1834
1835                 err = ubifs_validate_entry(c, dent);
1836                 if (err)
1837                         goto out_dump;
1838
1839                 /*
1840                  * Search the inode node this entry refers to and the parent
1841                  * inode node and insert them to the RB-tree of inodes.
1842                  */
1843                 inum = le64_to_cpu(dent->inum);
1844                 fscki = read_add_inode(c, priv, inum);
1845                 if (IS_ERR(fscki)) {
1846                         err = PTR_ERR(fscki);
1847                         ubifs_err("error %d while processing entry node and "
1848                                   "trying to find inode node %lu",
1849                                   err, (unsigned long)inum);
1850                         goto out_dump;
1851                 }
1852
1853                 /* Count how many direntries or xentries refers this inode */
1854                 fscki->references += 1;
1855
1856                 inum = key_inum_flash(c, &dent->key);
1857                 fscki1 = read_add_inode(c, priv, inum);
1858                 if (IS_ERR(fscki1)) {
1859                         err = PTR_ERR(fscki);
1860                         ubifs_err("error %d while processing entry node and "
1861                                   "trying to find parent inode node %lu",
1862                                   err, (unsigned long)inum);
1863                         goto out_dump;
1864                 }
1865
1866                 nlen = le16_to_cpu(dent->nlen);
1867                 if (type == UBIFS_XENT_KEY) {
1868                         fscki1->calc_xcnt += 1;
1869                         fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
1870                         fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
1871                         fscki1->calc_xnms += nlen;
1872                 } else {
1873                         fscki1->calc_sz += CALC_DENT_SIZE(nlen);
1874                         if (dent->type == UBIFS_ITYPE_DIR)
1875                                 fscki1->calc_cnt += 1;
1876                 }
1877         }
1878
1879 out:
1880         kfree(node);
1881         return 0;
1882
1883 out_dump:
1884         ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
1885         dbg_dump_node(c, node);
1886 out_free:
1887         kfree(node);
1888         return err;
1889 }
1890
1891 /**
1892  * free_inodes - free RB-tree of inodes.
1893  * @fsckd: FS checking information
1894  */
1895 static void free_inodes(struct fsck_data *fsckd)
1896 {
1897         struct rb_node *this = fsckd->inodes.rb_node;
1898         struct fsck_inode *fscki;
1899
1900         while (this) {
1901                 if (this->rb_left)
1902                         this = this->rb_left;
1903                 else if (this->rb_right)
1904                         this = this->rb_right;
1905                 else {
1906                         fscki = rb_entry(this, struct fsck_inode, rb);
1907                         this = rb_parent(this);
1908                         if (this) {
1909                                 if (this->rb_left == &fscki->rb)
1910                                         this->rb_left = NULL;
1911                                 else
1912                                         this->rb_right = NULL;
1913                         }
1914                         kfree(fscki);
1915                 }
1916         }
1917 }
1918
1919 /**
1920  * check_inodes - checks all inodes.
1921  * @c: UBIFS file-system description object
1922  * @fsckd: FS checking information
1923  *
1924  * This is a helper function for 'dbg_check_filesystem()' which walks the
1925  * RB-tree of inodes after the index scan has been finished, and checks that
1926  * inode nlink, size, etc are correct. Returns zero if inodes are fine,
1927  * %-EINVAL if not, and a negative error code in case of failure.
1928  */
1929 static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
1930 {
1931         int n, err;
1932         union ubifs_key key;
1933         struct ubifs_znode *znode;
1934         struct ubifs_zbranch *zbr;
1935         struct ubifs_ino_node *ino;
1936         struct fsck_inode *fscki;
1937         struct rb_node *this = rb_first(&fsckd->inodes);
1938
1939         while (this) {
1940                 fscki = rb_entry(this, struct fsck_inode, rb);
1941                 this = rb_next(this);
1942
1943                 if (S_ISDIR(fscki->mode)) {
1944                         /*
1945                          * Directories have to have exactly one reference (they
1946                          * cannot have hardlinks), although root inode is an
1947                          * exception.
1948                          */
1949                         if (fscki->inum != UBIFS_ROOT_INO &&
1950                             fscki->references != 1) {
1951                                 ubifs_err("directory inode %lu has %d "
1952                                           "direntries which refer it, but "
1953                                           "should be 1",
1954                                           (unsigned long)fscki->inum,
1955                                           fscki->references);
1956                                 goto out_dump;
1957                         }
1958                         if (fscki->inum == UBIFS_ROOT_INO &&
1959                             fscki->references != 0) {
1960                                 ubifs_err("root inode %lu has non-zero (%d) "
1961                                           "direntries which refer it",
1962                                           (unsigned long)fscki->inum,
1963                                           fscki->references);
1964                                 goto out_dump;
1965                         }
1966                         if (fscki->calc_sz != fscki->size) {
1967                                 ubifs_err("directory inode %lu size is %lld, "
1968                                           "but calculated size is %lld",
1969                                           (unsigned long)fscki->inum,
1970                                           fscki->size, fscki->calc_sz);
1971                                 goto out_dump;
1972                         }
1973                         if (fscki->calc_cnt != fscki->nlink) {
1974                                 ubifs_err("directory inode %lu nlink is %d, "
1975                                           "but calculated nlink is %d",
1976                                           (unsigned long)fscki->inum,
1977                                           fscki->nlink, fscki->calc_cnt);
1978                                 goto out_dump;
1979                         }
1980                 } else {
1981                         if (fscki->references != fscki->nlink) {
1982                                 ubifs_err("inode %lu nlink is %d, but "
1983                                           "calculated nlink is %d",
1984                                           (unsigned long)fscki->inum,
1985                                           fscki->nlink, fscki->references);
1986                                 goto out_dump;
1987                         }
1988                 }
1989                 if (fscki->xattr_sz != fscki->calc_xsz) {
1990                         ubifs_err("inode %lu has xattr size %u, but "
1991                                   "calculated size is %lld",
1992                                   (unsigned long)fscki->inum, fscki->xattr_sz,
1993                                   fscki->calc_xsz);
1994                         goto out_dump;
1995                 }
1996                 if (fscki->xattr_cnt != fscki->calc_xcnt) {
1997                         ubifs_err("inode %lu has %u xattrs, but "
1998                                   "calculated count is %lld",
1999                                   (unsigned long)fscki->inum,
2000                                   fscki->xattr_cnt, fscki->calc_xcnt);
2001                         goto out_dump;
2002                 }
2003                 if (fscki->xattr_nms != fscki->calc_xnms) {
2004                         ubifs_err("inode %lu has xattr names' size %u, but "
2005                                   "calculated names' size is %lld",
2006                                   (unsigned long)fscki->inum, fscki->xattr_nms,
2007                                   fscki->calc_xnms);
2008                         goto out_dump;
2009                 }
2010         }
2011
2012         return 0;
2013
2014 out_dump:
2015         /* Read the bad inode and dump it */
2016         ino_key_init(c, &key, fscki->inum);
2017         err = ubifs_lookup_level0(c, &key, &znode, &n);
2018         if (!err) {
2019                 ubifs_err("inode %lu not found in index",
2020                           (unsigned long)fscki->inum);
2021                 return -ENOENT;
2022         } else if (err < 0) {
2023                 ubifs_err("error %d while looking up inode %lu",
2024                           err, (unsigned long)fscki->inum);
2025                 return err;
2026         }
2027
2028         zbr = &znode->zbranch[n];
2029         ino = kmalloc(zbr->len, GFP_NOFS);
2030         if (!ino)
2031                 return -ENOMEM;
2032
2033         err = ubifs_tnc_read_node(c, zbr, ino);
2034         if (err) {
2035                 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2036                           zbr->lnum, zbr->offs, err);
2037                 kfree(ino);
2038                 return err;
2039         }
2040
2041         ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",
2042                   (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
2043         dbg_dump_node(c, ino);
2044         kfree(ino);
2045         return -EINVAL;
2046 }
2047
2048 /**
2049  * dbg_check_filesystem - check the file-system.
2050  * @c: UBIFS file-system description object
2051  *
2052  * This function checks the file system, namely:
2053  * o makes sure that all leaf nodes exist and their CRCs are correct;
2054  * o makes sure inode nlink, size, xattr size/count are correct (for all
2055  *   inodes).
2056  *
2057  * The function reads whole indexing tree and all nodes, so it is pretty
2058  * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2059  * not, and a negative error code in case of failure.
2060  */
2061 int dbg_check_filesystem(struct ubifs_info *c)
2062 {
2063         int err;
2064         struct fsck_data fsckd;
2065
2066         if (!(ubifs_chk_flags & UBIFS_CHK_FS))
2067                 return 0;
2068
2069         fsckd.inodes = RB_ROOT;
2070         err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
2071         if (err)
2072                 goto out_free;
2073
2074         err = check_inodes(c, &fsckd);
2075         if (err)
2076                 goto out_free;
2077
2078         free_inodes(&fsckd);
2079         return 0;
2080
2081 out_free:
2082         ubifs_err("file-system check failed with error %d", err);
2083         dump_stack();
2084         free_inodes(&fsckd);
2085         return err;
2086 }
2087
2088 static int invocation_cnt;
2089
2090 int dbg_force_in_the_gaps(void)
2091 {
2092         if (!dbg_force_in_the_gaps_enabled)
2093                 return 0;
2094         /* Force in-the-gaps every 8th commit */
2095         return !((invocation_cnt++) & 0x7);
2096 }
2097
2098 /* Failure mode for recovery testing */
2099
2100 #define chance(n, d) (simple_rand() <= (n) * 32768LL / (d))
2101
2102 struct failure_mode_info {
2103         struct list_head list;
2104         struct ubifs_info *c;
2105 };
2106
2107 static LIST_HEAD(fmi_list);
2108 static DEFINE_SPINLOCK(fmi_lock);
2109
2110 static unsigned int next;
2111
2112 static int simple_rand(void)
2113 {
2114         if (next == 0)
2115                 next = current->pid;
2116         next = next * 1103515245 + 12345;
2117         return (next >> 16) & 32767;
2118 }
2119
2120 static void failure_mode_init(struct ubifs_info *c)
2121 {
2122         struct failure_mode_info *fmi;
2123
2124         fmi = kmalloc(sizeof(struct failure_mode_info), GFP_NOFS);
2125         if (!fmi) {
2126                 ubifs_err("Failed to register failure mode - no memory");
2127                 return;
2128         }
2129         fmi->c = c;
2130         spin_lock(&fmi_lock);
2131         list_add_tail(&fmi->list, &fmi_list);
2132         spin_unlock(&fmi_lock);
2133 }
2134
2135 static void failure_mode_exit(struct ubifs_info *c)
2136 {
2137         struct failure_mode_info *fmi, *tmp;
2138
2139         spin_lock(&fmi_lock);
2140         list_for_each_entry_safe(fmi, tmp, &fmi_list, list)
2141                 if (fmi->c == c) {
2142                         list_del(&fmi->list);
2143                         kfree(fmi);
2144                 }
2145         spin_unlock(&fmi_lock);
2146 }
2147
2148 static struct ubifs_info *dbg_find_info(struct ubi_volume_desc *desc)
2149 {
2150         struct failure_mode_info *fmi;
2151
2152         spin_lock(&fmi_lock);
2153         list_for_each_entry(fmi, &fmi_list, list)
2154                 if (fmi->c->ubi == desc) {
2155                         struct ubifs_info *c = fmi->c;
2156
2157                         spin_unlock(&fmi_lock);
2158                         return c;
2159                 }
2160         spin_unlock(&fmi_lock);
2161         return NULL;
2162 }
2163
2164 static int in_failure_mode(struct ubi_volume_desc *desc)
2165 {
2166         struct ubifs_info *c = dbg_find_info(desc);
2167
2168         if (c && dbg_failure_mode)
2169                 return c->dbg->failure_mode;
2170         return 0;
2171 }
2172
2173 static int do_fail(struct ubi_volume_desc *desc, int lnum, int write)
2174 {
2175         struct ubifs_info *c = dbg_find_info(desc);
2176         struct ubifs_debug_info *d;
2177
2178         if (!c || !dbg_failure_mode)
2179                 return 0;
2180         d = c->dbg;
2181         if (d->failure_mode)
2182                 return 1;
2183         if (!d->fail_cnt) {
2184                 /* First call - decide delay to failure */
2185                 if (chance(1, 2)) {
2186                         unsigned int delay = 1 << (simple_rand() >> 11);
2187
2188                         if (chance(1, 2)) {
2189                                 d->fail_delay = 1;
2190                                 d->fail_timeout = jiffies +
2191                                                   msecs_to_jiffies(delay);
2192                                 dbg_rcvry("failing after %ums", delay);
2193                         } else {
2194                                 d->fail_delay = 2;
2195                                 d->fail_cnt_max = delay;
2196                                 dbg_rcvry("failing after %u calls", delay);
2197                         }
2198                 }
2199                 d->fail_cnt += 1;
2200         }
2201         /* Determine if failure delay has expired */
2202         if (d->fail_delay == 1) {
2203                 if (time_before(jiffies, d->fail_timeout))
2204                         return 0;
2205         } else if (d->fail_delay == 2)
2206                 if (d->fail_cnt++ < d->fail_cnt_max)
2207                         return 0;
2208         if (lnum == UBIFS_SB_LNUM) {
2209                 if (write) {
2210                         if (chance(1, 2))
2211                                 return 0;
2212                 } else if (chance(19, 20))
2213                         return 0;
2214                 dbg_rcvry("failing in super block LEB %d", lnum);
2215         } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2216                 if (chance(19, 20))
2217                         return 0;
2218                 dbg_rcvry("failing in master LEB %d", lnum);
2219         } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2220                 if (write) {
2221                         if (chance(99, 100))
2222                                 return 0;
2223                 } else if (chance(399, 400))
2224                         return 0;
2225                 dbg_rcvry("failing in log LEB %d", lnum);
2226         } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2227                 if (write) {
2228                         if (chance(7, 8))
2229                                 return 0;
2230                 } else if (chance(19, 20))
2231                         return 0;
2232                 dbg_rcvry("failing in LPT LEB %d", lnum);
2233         } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2234                 if (write) {
2235                         if (chance(1, 2))
2236                                 return 0;
2237                 } else if (chance(9, 10))
2238                         return 0;
2239                 dbg_rcvry("failing in orphan LEB %d", lnum);
2240         } else if (lnum == c->ihead_lnum) {
2241                 if (chance(99, 100))
2242                         return 0;
2243                 dbg_rcvry("failing in index head LEB %d", lnum);
2244         } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2245                 if (chance(9, 10))
2246                         return 0;
2247                 dbg_rcvry("failing in GC head LEB %d", lnum);
2248         } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
2249                    !ubifs_search_bud(c, lnum)) {
2250                 if (chance(19, 20))
2251                         return 0;
2252                 dbg_rcvry("failing in non-bud LEB %d", lnum);
2253         } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
2254                    c->cmt_state == COMMIT_RUNNING_REQUIRED) {
2255                 if (chance(999, 1000))
2256                         return 0;
2257                 dbg_rcvry("failing in bud LEB %d commit running", lnum);
2258         } else {
2259                 if (chance(9999, 10000))
2260                         return 0;
2261                 dbg_rcvry("failing in bud LEB %d commit not running", lnum);
2262         }
2263         ubifs_err("*** SETTING FAILURE MODE ON (LEB %d) ***", lnum);
2264         d->failure_mode = 1;
2265         dump_stack();
2266         return 1;
2267 }
2268
2269 static void cut_data(const void *buf, int len)
2270 {
2271         int flen, i;
2272         unsigned char *p = (void *)buf;
2273
2274         flen = (len * (long long)simple_rand()) >> 15;
2275         for (i = flen; i < len; i++)
2276                 p[i] = 0xff;
2277 }
2278
2279 int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
2280                  int len, int check)
2281 {
2282         if (in_failure_mode(desc))
2283                 return -EIO;
2284         return ubi_leb_read(desc, lnum, buf, offset, len, check);
2285 }
2286
2287 int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
2288                   int offset, int len, int dtype)
2289 {
2290         int err, failing;
2291
2292         if (in_failure_mode(desc))
2293                 return -EIO;
2294         failing = do_fail(desc, lnum, 1);
2295         if (failing)
2296                 cut_data(buf, len);
2297         err = ubi_leb_write(desc, lnum, buf, offset, len, dtype);
2298         if (err)
2299                 return err;
2300         if (failing)
2301                 return -EIO;
2302         return 0;
2303 }
2304
2305 int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
2306                    int len, int dtype)
2307 {
2308         int err;
2309
2310         if (do_fail(desc, lnum, 1))
2311                 return -EIO;
2312         err = ubi_leb_change(desc, lnum, buf, len, dtype);
2313         if (err)
2314                 return err;
2315         if (do_fail(desc, lnum, 1))
2316                 return -EIO;
2317         return 0;
2318 }
2319
2320 int dbg_leb_erase(struct ubi_volume_desc *desc, int lnum)
2321 {
2322         int err;
2323
2324         if (do_fail(desc, lnum, 0))
2325                 return -EIO;
2326         err = ubi_leb_erase(desc, lnum);
2327         if (err)
2328                 return err;
2329         if (do_fail(desc, lnum, 0))
2330                 return -EIO;
2331         return 0;
2332 }
2333
2334 int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum)
2335 {
2336         int err;
2337
2338         if (do_fail(desc, lnum, 0))
2339                 return -EIO;
2340         err = ubi_leb_unmap(desc, lnum);
2341         if (err)
2342                 return err;
2343         if (do_fail(desc, lnum, 0))
2344                 return -EIO;
2345         return 0;
2346 }
2347
2348 int dbg_is_mapped(struct ubi_volume_desc *desc, int lnum)
2349 {
2350         if (in_failure_mode(desc))
2351                 return -EIO;
2352         return ubi_is_mapped(desc, lnum);
2353 }
2354
2355 int dbg_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
2356 {
2357         int err;
2358
2359         if (do_fail(desc, lnum, 0))
2360                 return -EIO;
2361         err = ubi_leb_map(desc, lnum, dtype);
2362         if (err)
2363                 return err;
2364         if (do_fail(desc, lnum, 0))
2365                 return -EIO;
2366         return 0;
2367 }
2368
2369 /**
2370  * ubifs_debugging_init - initialize UBIFS debugging.
2371  * @c: UBIFS file-system description object
2372  *
2373  * This function initializes debugging-related data for the file system.
2374  * Returns zero in case of success and a negative error code in case of
2375  * failure.
2376  */
2377 int ubifs_debugging_init(struct ubifs_info *c)
2378 {
2379         c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
2380         if (!c->dbg)
2381                 return -ENOMEM;
2382
2383         c->dbg->buf = vmalloc(c->leb_size);
2384         if (!c->dbg->buf)
2385                 goto out;
2386
2387         failure_mode_init(c);
2388         return 0;
2389
2390 out:
2391         kfree(c->dbg);
2392         return -ENOMEM;
2393 }
2394
2395 /**
2396  * ubifs_debugging_exit - free debugging data.
2397  * @c: UBIFS file-system description object
2398  */
2399 void ubifs_debugging_exit(struct ubifs_info *c)
2400 {
2401         failure_mode_exit(c);
2402         vfree(c->dbg->buf);
2403         kfree(c->dbg);
2404 }
2405
2406 /*
2407  * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
2408  * contain the stuff specific to particular file-system mounts.
2409  */
2410 static struct dentry *debugfs_rootdir;
2411
2412 /**
2413  * dbg_debugfs_init - initialize debugfs file-system.
2414  *
2415  * UBIFS uses debugfs file-system to expose various debugging knobs to
2416  * user-space. This function creates "ubifs" directory in the debugfs
2417  * file-system. Returns zero in case of success and a negative error code in
2418  * case of failure.
2419  */
2420 int dbg_debugfs_init(void)
2421 {
2422         debugfs_rootdir = debugfs_create_dir("ubifs", NULL);
2423         if (IS_ERR(debugfs_rootdir)) {
2424                 int err = PTR_ERR(debugfs_rootdir);
2425                 ubifs_err("cannot create \"ubifs\" debugfs directory, "
2426                           "error %d\n", err);
2427                 return err;
2428         }
2429
2430         return 0;
2431 }
2432
2433 /**
2434  * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
2435  */
2436 void dbg_debugfs_exit(void)
2437 {
2438         debugfs_remove(debugfs_rootdir);
2439 }
2440
2441 static int open_debugfs_file(struct inode *inode, struct file *file)
2442 {
2443         file->private_data = inode->i_private;
2444         return 0;
2445 }
2446
2447 static ssize_t write_debugfs_file(struct file *file, const char __user *buf,
2448                                   size_t count, loff_t *ppos)
2449 {
2450         struct ubifs_info *c = file->private_data;
2451         struct ubifs_debug_info *d = c->dbg;
2452
2453         if (file->f_path.dentry == d->dump_lprops)
2454                 dbg_dump_lprops(c);
2455         else if (file->f_path.dentry == d->dump_budg) {
2456                 spin_lock(&c->space_lock);
2457                 dbg_dump_budg(c);
2458                 spin_unlock(&c->space_lock);
2459         } else if (file->f_path.dentry == d->dump_tnc) {
2460                 mutex_lock(&c->tnc_mutex);
2461                 dbg_dump_tnc(c);
2462                 mutex_unlock(&c->tnc_mutex);
2463         } else
2464                 return -EINVAL;
2465
2466         *ppos += count;
2467         return count;
2468 }
2469
2470 static const struct file_operations debugfs_fops = {
2471         .open = open_debugfs_file,
2472         .write = write_debugfs_file,
2473         .owner = THIS_MODULE,
2474 };
2475
2476 /**
2477  * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
2478  * @c: UBIFS file-system description object
2479  *
2480  * This function creates all debugfs files for this instance of UBIFS. Returns
2481  * zero in case of success and a negative error code in case of failure.
2482  *
2483  * Note, the only reason we have not merged this function with the
2484  * 'ubifs_debugging_init()' function is because it is better to initialize
2485  * debugfs interfaces at the very end of the mount process, and remove them at
2486  * the very beginning of the mount process.
2487  */
2488 int dbg_debugfs_init_fs(struct ubifs_info *c)
2489 {
2490         int err;
2491         const char *fname;
2492         struct dentry *dent;
2493         struct ubifs_debug_info *d = c->dbg;
2494
2495         sprintf(d->debugfs_dir_name, "ubi%d_%d", c->vi.ubi_num, c->vi.vol_id);
2496         d->debugfs_dir = debugfs_create_dir(d->debugfs_dir_name,
2497                                               debugfs_rootdir);
2498         if (IS_ERR(d->debugfs_dir)) {
2499                 err = PTR_ERR(d->debugfs_dir);
2500                 ubifs_err("cannot create \"%s\" debugfs directory, error %d\n",
2501                           d->debugfs_dir_name, err);
2502                 goto out;
2503         }
2504
2505         fname = "dump_lprops";
2506         dent = debugfs_create_file(fname, S_IWUGO, d->debugfs_dir, c,
2507                                    &debugfs_fops);
2508         if (IS_ERR(dent))
2509                 goto out_remove;
2510         d->dump_lprops = dent;
2511
2512         fname = "dump_budg";
2513         dent = debugfs_create_file(fname, S_IWUGO, d->debugfs_dir, c,
2514                                    &debugfs_fops);
2515         if (IS_ERR(dent))
2516                 goto out_remove;
2517         d->dump_budg = dent;
2518
2519         fname = "dump_tnc";
2520         dent = debugfs_create_file(fname, S_IWUGO, d->debugfs_dir, c,
2521                                    &debugfs_fops);
2522         if (IS_ERR(dent))
2523                 goto out_remove;
2524         d->dump_tnc = dent;
2525
2526         return 0;
2527
2528 out_remove:
2529         err = PTR_ERR(dent);
2530         ubifs_err("cannot create \"%s\" debugfs directory, error %d\n",
2531                   fname, err);
2532         debugfs_remove_recursive(d->debugfs_dir);
2533 out:
2534         return err;
2535 }
2536
2537 /**
2538  * dbg_debugfs_exit_fs - remove all debugfs files.
2539  * @c: UBIFS file-system description object
2540  */
2541 void dbg_debugfs_exit_fs(struct ubifs_info *c)
2542 {
2543         debugfs_remove_recursive(c->dbg->debugfs_dir);
2544 }
2545
2546 #endif /* CONFIG_UBIFS_FS_DEBUG */