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