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