~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/ubifs/debug.c

Version: ~ [ linux-5.2-rc5 ] ~ [ linux-5.1.11 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.52 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.127 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.182 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.182 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.68 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp