blob: 5dc993fdca8704db7f90b1317a696b6736429c69 [file] [log] [blame]
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001/*
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
Artem Bityutskiy1e517642008-07-14 19:08:37 +030030#include <linux/module.h>
Artem Bityutskiy552ff312008-10-23 11:49:28 +030031#include <linux/debugfs.h>
Artem Bityutskiy4d61db42008-12-18 14:06:51 +020032#include <linux/math64.h>
Artem Bityutskiy81e79d32011-05-31 18:16:34 +030033#include <linux/uaccess.h>
Artem Bityutskiya7fa94a2011-06-03 16:20:03 +030034#include <linux/random.h>
35#include "ubifs.h"
Artem Bityutskiy1e517642008-07-14 19:08:37 +030036
Artem Bityutskiyb06283c2012-01-18 16:06:17 +020037static DEFINE_SPINLOCK(dbg_lock);
Artem Bityutskiy1e517642008-07-14 19:08:37 +030038
Artem Bityutskiy1e517642008-07-14 19:08:37 +030039static const char *get_key_fmt(int fmt)
40{
41 switch (fmt) {
42 case UBIFS_SIMPLE_KEY_FMT:
43 return "simple";
44 default:
45 return "unknown/invalid format";
46 }
47}
48
49static const char *get_key_hash(int hash)
50{
51 switch (hash) {
52 case UBIFS_KEY_HASH_R5:
53 return "R5";
54 case UBIFS_KEY_HASH_TEST:
55 return "test";
56 default:
57 return "unknown/invalid name hash";
58 }
59}
60
61static const char *get_key_type(int type)
62{
63 switch (type) {
64 case UBIFS_INO_KEY:
65 return "inode";
66 case UBIFS_DENT_KEY:
67 return "direntry";
68 case UBIFS_XENT_KEY:
69 return "xentry";
70 case UBIFS_DATA_KEY:
71 return "data";
72 case UBIFS_TRUN_KEY:
73 return "truncate";
74 default:
75 return "unknown/invalid key";
76 }
77}
78
Artem Bityutskiy4315fb42011-05-25 17:32:42 +030079static const char *get_dent_type(int type)
80{
81 switch (type) {
82 case UBIFS_ITYPE_REG:
83 return "file";
84 case UBIFS_ITYPE_DIR:
85 return "dir";
86 case UBIFS_ITYPE_LNK:
87 return "symlink";
88 case UBIFS_ITYPE_BLK:
89 return "blkdev";
90 case UBIFS_ITYPE_CHR:
91 return "char dev";
92 case UBIFS_ITYPE_FIFO:
93 return "fifo";
94 case UBIFS_ITYPE_SOCK:
95 return "socket";
96 default:
97 return "unknown/invalid type";
98 }
99}
100
Artem Bityutskiy515315a2012-01-13 12:33:53 +0200101const char *dbg_snprintf_key(const struct ubifs_info *c,
102 const union ubifs_key *key, char *buffer, int len)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300103{
104 char *p = buffer;
105 int type = key_type(c, key);
106
107 if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
108 switch (type) {
109 case UBIFS_INO_KEY:
Artem Bityutskiybeba0062012-01-11 15:52:09 +0200110 len -= snprintf(p, len, "(%lu, %s)",
111 (unsigned long)key_inum(c, key),
112 get_key_type(type));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300113 break;
114 case UBIFS_DENT_KEY:
115 case UBIFS_XENT_KEY:
Artem Bityutskiybeba0062012-01-11 15:52:09 +0200116 len -= snprintf(p, len, "(%lu, %s, %#08x)",
117 (unsigned long)key_inum(c, key),
118 get_key_type(type), key_hash(c, key));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300119 break;
120 case UBIFS_DATA_KEY:
Artem Bityutskiybeba0062012-01-11 15:52:09 +0200121 len -= snprintf(p, len, "(%lu, %s, %u)",
122 (unsigned long)key_inum(c, key),
123 get_key_type(type), key_block(c, key));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300124 break;
125 case UBIFS_TRUN_KEY:
Artem Bityutskiybeba0062012-01-11 15:52:09 +0200126 len -= snprintf(p, len, "(%lu, %s)",
127 (unsigned long)key_inum(c, key),
128 get_key_type(type));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300129 break;
130 default:
Artem Bityutskiybeba0062012-01-11 15:52:09 +0200131 len -= snprintf(p, len, "(bad key type: %#08x, %#08x)",
132 key->u32[0], key->u32[1]);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300133 }
134 } else
Artem Bityutskiybeba0062012-01-11 15:52:09 +0200135 len -= snprintf(p, len, "bad key format %d", c->key_fmt);
136 ubifs_assert(len > 0);
Artem Bityutskiy515315a2012-01-13 12:33:53 +0200137 return p;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300138}
139
140const char *dbg_ntype(int type)
141{
142 switch (type) {
143 case UBIFS_PAD_NODE:
144 return "padding node";
145 case UBIFS_SB_NODE:
146 return "superblock node";
147 case UBIFS_MST_NODE:
148 return "master node";
149 case UBIFS_REF_NODE:
150 return "reference node";
151 case UBIFS_INO_NODE:
152 return "inode node";
153 case UBIFS_DENT_NODE:
154 return "direntry node";
155 case UBIFS_XENT_NODE:
156 return "xentry node";
157 case UBIFS_DATA_NODE:
158 return "data node";
159 case UBIFS_TRUN_NODE:
160 return "truncate node";
161 case UBIFS_IDX_NODE:
162 return "indexing node";
163 case UBIFS_CS_NODE:
164 return "commit start node";
165 case UBIFS_ORPH_NODE:
166 return "orphan node";
167 default:
168 return "unknown node";
169 }
170}
171
172static const char *dbg_gtype(int type)
173{
174 switch (type) {
175 case UBIFS_NO_NODE_GROUP:
176 return "no node group";
177 case UBIFS_IN_NODE_GROUP:
178 return "in node group";
179 case UBIFS_LAST_OF_NODE_GROUP:
180 return "last of node group";
181 default:
182 return "unknown";
183 }
184}
185
186const char *dbg_cstate(int cmt_state)
187{
188 switch (cmt_state) {
189 case COMMIT_RESTING:
190 return "commit resting";
191 case COMMIT_BACKGROUND:
192 return "background commit requested";
193 case COMMIT_REQUIRED:
194 return "commit required";
195 case COMMIT_RUNNING_BACKGROUND:
196 return "BACKGROUND commit running";
197 case COMMIT_RUNNING_REQUIRED:
198 return "commit running and required";
199 case COMMIT_BROKEN:
200 return "broken commit";
201 default:
202 return "unknown commit state";
203 }
204}
205
Artem Bityutskiy77a7ae52009-09-15 15:03:51 +0300206const char *dbg_jhead(int jhead)
207{
208 switch (jhead) {
209 case GCHD:
210 return "0 (GC)";
211 case BASEHD:
212 return "1 (base)";
213 case DATAHD:
214 return "2 (data)";
215 default:
216 return "unknown journal head";
217 }
218}
219
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300220static void dump_ch(const struct ubifs_ch *ch)
221{
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200222 printk(KERN_ERR "\tmagic %#x\n", le32_to_cpu(ch->magic));
223 printk(KERN_ERR "\tcrc %#x\n", le32_to_cpu(ch->crc));
224 printk(KERN_ERR "\tnode_type %d (%s)\n", ch->node_type,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300225 dbg_ntype(ch->node_type));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200226 printk(KERN_ERR "\tgroup_type %d (%s)\n", ch->group_type,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300227 dbg_gtype(ch->group_type));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200228 printk(KERN_ERR "\tsqnum %llu\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300229 (unsigned long long)le64_to_cpu(ch->sqnum));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200230 printk(KERN_ERR "\tlen %u\n", le32_to_cpu(ch->len));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300231}
232
Artem Bityutskiyedf6be22012-05-16 19:15:56 +0300233void ubifs_dump_inode(struct ubifs_info *c, const struct inode *inode)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300234{
235 const struct ubifs_inode *ui = ubifs_inode(inode);
Artem Bityutskiy4315fb42011-05-25 17:32:42 +0300236 struct qstr nm = { .name = NULL };
237 union ubifs_key key;
238 struct ubifs_dent_node *dent, *pdent = NULL;
239 int count = 2;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300240
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200241 printk(KERN_ERR "Dump in-memory inode:");
242 printk(KERN_ERR "\tinode %lu\n", inode->i_ino);
243 printk(KERN_ERR "\tsize %llu\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300244 (unsigned long long)i_size_read(inode));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200245 printk(KERN_ERR "\tnlink %u\n", inode->i_nlink);
246 printk(KERN_ERR "\tuid %u\n", (unsigned int)inode->i_uid);
247 printk(KERN_ERR "\tgid %u\n", (unsigned int)inode->i_gid);
248 printk(KERN_ERR "\tatime %u.%u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300249 (unsigned int)inode->i_atime.tv_sec,
250 (unsigned int)inode->i_atime.tv_nsec);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200251 printk(KERN_ERR "\tmtime %u.%u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300252 (unsigned int)inode->i_mtime.tv_sec,
253 (unsigned int)inode->i_mtime.tv_nsec);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200254 printk(KERN_ERR "\tctime %u.%u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300255 (unsigned int)inode->i_ctime.tv_sec,
256 (unsigned int)inode->i_ctime.tv_nsec);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200257 printk(KERN_ERR "\tcreat_sqnum %llu\n", ui->creat_sqnum);
258 printk(KERN_ERR "\txattr_size %u\n", ui->xattr_size);
259 printk(KERN_ERR "\txattr_cnt %u\n", ui->xattr_cnt);
260 printk(KERN_ERR "\txattr_names %u\n", ui->xattr_names);
261 printk(KERN_ERR "\tdirty %u\n", ui->dirty);
262 printk(KERN_ERR "\txattr %u\n", ui->xattr);
263 printk(KERN_ERR "\tbulk_read %u\n", ui->xattr);
264 printk(KERN_ERR "\tsynced_i_size %llu\n",
Artem Bityutskiyb5e426e2008-09-09 11:20:35 +0300265 (unsigned long long)ui->synced_i_size);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200266 printk(KERN_ERR "\tui_size %llu\n",
Artem Bityutskiyb5e426e2008-09-09 11:20:35 +0300267 (unsigned long long)ui->ui_size);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200268 printk(KERN_ERR "\tflags %d\n", ui->flags);
269 printk(KERN_ERR "\tcompr_type %d\n", ui->compr_type);
270 printk(KERN_ERR "\tlast_page_read %lu\n", ui->last_page_read);
271 printk(KERN_ERR "\tread_in_a_row %lu\n", ui->read_in_a_row);
272 printk(KERN_ERR "\tdata_len %d\n", ui->data_len);
Artem Bityutskiy4315fb42011-05-25 17:32:42 +0300273
274 if (!S_ISDIR(inode->i_mode))
275 return;
276
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200277 printk(KERN_ERR "List of directory entries:\n");
Artem Bityutskiy4315fb42011-05-25 17:32:42 +0300278 ubifs_assert(!mutex_is_locked(&c->tnc_mutex));
279
280 lowest_dent_key(c, &key, inode->i_ino);
281 while (1) {
282 dent = ubifs_tnc_next_ent(c, &key, &nm);
283 if (IS_ERR(dent)) {
284 if (PTR_ERR(dent) != -ENOENT)
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200285 printk(KERN_ERR "error %ld\n", PTR_ERR(dent));
Artem Bityutskiy4315fb42011-05-25 17:32:42 +0300286 break;
287 }
288
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200289 printk(KERN_ERR "\t%d: %s (%s)\n",
Artem Bityutskiy4315fb42011-05-25 17:32:42 +0300290 count++, dent->name, get_dent_type(dent->type));
291
292 nm.name = dent->name;
293 nm.len = le16_to_cpu(dent->nlen);
294 kfree(pdent);
295 pdent = dent;
296 key_read(c, &dent->key, &key);
297 }
298 kfree(pdent);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300299}
300
Artem Bityutskiyedf6be22012-05-16 19:15:56 +0300301void ubifs_dump_node(const struct ubifs_info *c, const void *node)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300302{
303 int i, n;
304 union ubifs_key key;
305 const struct ubifs_ch *ch = node;
Artem Bityutskiy515315a2012-01-13 12:33:53 +0200306 char key_buf[DBG_KEY_BUF_LEN];
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300307
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300308 /* If the magic is incorrect, just hexdump the first bytes */
309 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200310 printk(KERN_ERR "Not a node, first %zu bytes:", UBIFS_CH_SZ);
311 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 32, 1,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300312 (void *)node, UBIFS_CH_SZ, 1);
313 return;
314 }
315
316 spin_lock(&dbg_lock);
317 dump_ch(node);
318
319 switch (ch->node_type) {
320 case UBIFS_PAD_NODE:
321 {
322 const struct ubifs_pad_node *pad = node;
323
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200324 printk(KERN_ERR "\tpad_len %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300325 le32_to_cpu(pad->pad_len));
326 break;
327 }
328 case UBIFS_SB_NODE:
329 {
330 const struct ubifs_sb_node *sup = node;
331 unsigned int sup_flags = le32_to_cpu(sup->flags);
332
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200333 printk(KERN_ERR "\tkey_hash %d (%s)\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300334 (int)sup->key_hash, get_key_hash(sup->key_hash));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200335 printk(KERN_ERR "\tkey_fmt %d (%s)\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300336 (int)sup->key_fmt, get_key_fmt(sup->key_fmt));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200337 printk(KERN_ERR "\tflags %#x\n", sup_flags);
338 printk(KERN_ERR "\t big_lpt %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300339 !!(sup_flags & UBIFS_FLG_BIGLPT));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200340 printk(KERN_ERR "\t space_fixup %u\n",
Matthew L. Creech9f58d352011-05-05 16:33:20 -0400341 !!(sup_flags & UBIFS_FLG_SPACE_FIXUP));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200342 printk(KERN_ERR "\tmin_io_size %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300343 le32_to_cpu(sup->min_io_size));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200344 printk(KERN_ERR "\tleb_size %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300345 le32_to_cpu(sup->leb_size));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200346 printk(KERN_ERR "\tleb_cnt %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300347 le32_to_cpu(sup->leb_cnt));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200348 printk(KERN_ERR "\tmax_leb_cnt %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300349 le32_to_cpu(sup->max_leb_cnt));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200350 printk(KERN_ERR "\tmax_bud_bytes %llu\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300351 (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200352 printk(KERN_ERR "\tlog_lebs %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300353 le32_to_cpu(sup->log_lebs));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200354 printk(KERN_ERR "\tlpt_lebs %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300355 le32_to_cpu(sup->lpt_lebs));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200356 printk(KERN_ERR "\torph_lebs %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300357 le32_to_cpu(sup->orph_lebs));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200358 printk(KERN_ERR "\tjhead_cnt %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300359 le32_to_cpu(sup->jhead_cnt));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200360 printk(KERN_ERR "\tfanout %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300361 le32_to_cpu(sup->fanout));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200362 printk(KERN_ERR "\tlsave_cnt %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300363 le32_to_cpu(sup->lsave_cnt));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200364 printk(KERN_ERR "\tdefault_compr %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300365 (int)le16_to_cpu(sup->default_compr));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200366 printk(KERN_ERR "\trp_size %llu\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300367 (unsigned long long)le64_to_cpu(sup->rp_size));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200368 printk(KERN_ERR "\trp_uid %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300369 le32_to_cpu(sup->rp_uid));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200370 printk(KERN_ERR "\trp_gid %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300371 le32_to_cpu(sup->rp_gid));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200372 printk(KERN_ERR "\tfmt_version %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300373 le32_to_cpu(sup->fmt_version));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200374 printk(KERN_ERR "\ttime_gran %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300375 le32_to_cpu(sup->time_gran));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200376 printk(KERN_ERR "\tUUID %pUB\n",
Joe Perches7f2f4e72009-12-14 18:01:13 -0800377 sup->uuid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300378 break;
379 }
380 case UBIFS_MST_NODE:
381 {
382 const struct ubifs_mst_node *mst = node;
383
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200384 printk(KERN_ERR "\thighest_inum %llu\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300385 (unsigned long long)le64_to_cpu(mst->highest_inum));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200386 printk(KERN_ERR "\tcommit number %llu\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300387 (unsigned long long)le64_to_cpu(mst->cmt_no));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200388 printk(KERN_ERR "\tflags %#x\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300389 le32_to_cpu(mst->flags));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200390 printk(KERN_ERR "\tlog_lnum %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300391 le32_to_cpu(mst->log_lnum));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200392 printk(KERN_ERR "\troot_lnum %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300393 le32_to_cpu(mst->root_lnum));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200394 printk(KERN_ERR "\troot_offs %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300395 le32_to_cpu(mst->root_offs));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200396 printk(KERN_ERR "\troot_len %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300397 le32_to_cpu(mst->root_len));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200398 printk(KERN_ERR "\tgc_lnum %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300399 le32_to_cpu(mst->gc_lnum));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200400 printk(KERN_ERR "\tihead_lnum %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300401 le32_to_cpu(mst->ihead_lnum));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200402 printk(KERN_ERR "\tihead_offs %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300403 le32_to_cpu(mst->ihead_offs));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200404 printk(KERN_ERR "\tindex_size %llu\n",
Harvey Harrison0ecb9522008-10-24 10:52:57 -0700405 (unsigned long long)le64_to_cpu(mst->index_size));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200406 printk(KERN_ERR "\tlpt_lnum %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300407 le32_to_cpu(mst->lpt_lnum));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200408 printk(KERN_ERR "\tlpt_offs %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300409 le32_to_cpu(mst->lpt_offs));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200410 printk(KERN_ERR "\tnhead_lnum %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300411 le32_to_cpu(mst->nhead_lnum));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200412 printk(KERN_ERR "\tnhead_offs %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300413 le32_to_cpu(mst->nhead_offs));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200414 printk(KERN_ERR "\tltab_lnum %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300415 le32_to_cpu(mst->ltab_lnum));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200416 printk(KERN_ERR "\tltab_offs %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300417 le32_to_cpu(mst->ltab_offs));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200418 printk(KERN_ERR "\tlsave_lnum %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300419 le32_to_cpu(mst->lsave_lnum));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200420 printk(KERN_ERR "\tlsave_offs %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300421 le32_to_cpu(mst->lsave_offs));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200422 printk(KERN_ERR "\tlscan_lnum %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300423 le32_to_cpu(mst->lscan_lnum));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200424 printk(KERN_ERR "\tleb_cnt %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300425 le32_to_cpu(mst->leb_cnt));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200426 printk(KERN_ERR "\tempty_lebs %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300427 le32_to_cpu(mst->empty_lebs));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200428 printk(KERN_ERR "\tidx_lebs %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300429 le32_to_cpu(mst->idx_lebs));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200430 printk(KERN_ERR "\ttotal_free %llu\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300431 (unsigned long long)le64_to_cpu(mst->total_free));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200432 printk(KERN_ERR "\ttotal_dirty %llu\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300433 (unsigned long long)le64_to_cpu(mst->total_dirty));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200434 printk(KERN_ERR "\ttotal_used %llu\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300435 (unsigned long long)le64_to_cpu(mst->total_used));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200436 printk(KERN_ERR "\ttotal_dead %llu\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300437 (unsigned long long)le64_to_cpu(mst->total_dead));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200438 printk(KERN_ERR "\ttotal_dark %llu\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300439 (unsigned long long)le64_to_cpu(mst->total_dark));
440 break;
441 }
442 case UBIFS_REF_NODE:
443 {
444 const struct ubifs_ref_node *ref = node;
445
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200446 printk(KERN_ERR "\tlnum %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300447 le32_to_cpu(ref->lnum));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200448 printk(KERN_ERR "\toffs %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300449 le32_to_cpu(ref->offs));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200450 printk(KERN_ERR "\tjhead %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300451 le32_to_cpu(ref->jhead));
452 break;
453 }
454 case UBIFS_INO_NODE:
455 {
456 const struct ubifs_ino_node *ino = node;
457
458 key_read(c, &ino->key, &key);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200459 printk(KERN_ERR "\tkey %s\n",
Artem Bityutskiy515315a2012-01-13 12:33:53 +0200460 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200461 printk(KERN_ERR "\tcreat_sqnum %llu\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300462 (unsigned long long)le64_to_cpu(ino->creat_sqnum));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200463 printk(KERN_ERR "\tsize %llu\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300464 (unsigned long long)le64_to_cpu(ino->size));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200465 printk(KERN_ERR "\tnlink %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300466 le32_to_cpu(ino->nlink));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200467 printk(KERN_ERR "\tatime %lld.%u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300468 (long long)le64_to_cpu(ino->atime_sec),
469 le32_to_cpu(ino->atime_nsec));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200470 printk(KERN_ERR "\tmtime %lld.%u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300471 (long long)le64_to_cpu(ino->mtime_sec),
472 le32_to_cpu(ino->mtime_nsec));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200473 printk(KERN_ERR "\tctime %lld.%u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300474 (long long)le64_to_cpu(ino->ctime_sec),
475 le32_to_cpu(ino->ctime_nsec));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200476 printk(KERN_ERR "\tuid %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300477 le32_to_cpu(ino->uid));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200478 printk(KERN_ERR "\tgid %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300479 le32_to_cpu(ino->gid));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200480 printk(KERN_ERR "\tmode %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300481 le32_to_cpu(ino->mode));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200482 printk(KERN_ERR "\tflags %#x\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300483 le32_to_cpu(ino->flags));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200484 printk(KERN_ERR "\txattr_cnt %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300485 le32_to_cpu(ino->xattr_cnt));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200486 printk(KERN_ERR "\txattr_size %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300487 le32_to_cpu(ino->xattr_size));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200488 printk(KERN_ERR "\txattr_names %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300489 le32_to_cpu(ino->xattr_names));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200490 printk(KERN_ERR "\tcompr_type %#x\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300491 (int)le16_to_cpu(ino->compr_type));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200492 printk(KERN_ERR "\tdata len %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300493 le32_to_cpu(ino->data_len));
494 break;
495 }
496 case UBIFS_DENT_NODE:
497 case UBIFS_XENT_NODE:
498 {
499 const struct ubifs_dent_node *dent = node;
500 int nlen = le16_to_cpu(dent->nlen);
501
502 key_read(c, &dent->key, &key);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200503 printk(KERN_ERR "\tkey %s\n",
Artem Bityutskiy515315a2012-01-13 12:33:53 +0200504 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200505 printk(KERN_ERR "\tinum %llu\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300506 (unsigned long long)le64_to_cpu(dent->inum));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200507 printk(KERN_ERR "\ttype %d\n", (int)dent->type);
508 printk(KERN_ERR "\tnlen %d\n", nlen);
509 printk(KERN_ERR "\tname ");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300510
511 if (nlen > UBIFS_MAX_NLEN)
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200512 printk(KERN_ERR "(bad name length, not printing, "
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300513 "bad or corrupted node)");
514 else {
515 for (i = 0; i < nlen && dent->name[i]; i++)
Artem Bityutskiyc9927c32009-03-16 09:42:03 +0200516 printk(KERN_CONT "%c", dent->name[i]);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300517 }
Artem Bityutskiyc9927c32009-03-16 09:42:03 +0200518 printk(KERN_CONT "\n");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300519
520 break;
521 }
522 case UBIFS_DATA_NODE:
523 {
524 const struct ubifs_data_node *dn = node;
525 int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
526
527 key_read(c, &dn->key, &key);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200528 printk(KERN_ERR "\tkey %s\n",
Artem Bityutskiy515315a2012-01-13 12:33:53 +0200529 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200530 printk(KERN_ERR "\tsize %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300531 le32_to_cpu(dn->size));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200532 printk(KERN_ERR "\tcompr_typ %d\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300533 (int)le16_to_cpu(dn->compr_type));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200534 printk(KERN_ERR "\tdata size %d\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300535 dlen);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200536 printk(KERN_ERR "\tdata:\n");
537 print_hex_dump(KERN_ERR, "\t", DUMP_PREFIX_OFFSET, 32, 1,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300538 (void *)&dn->data, dlen, 0);
539 break;
540 }
541 case UBIFS_TRUN_NODE:
542 {
543 const struct ubifs_trun_node *trun = node;
544
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200545 printk(KERN_ERR "\tinum %u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300546 le32_to_cpu(trun->inum));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200547 printk(KERN_ERR "\told_size %llu\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300548 (unsigned long long)le64_to_cpu(trun->old_size));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200549 printk(KERN_ERR "\tnew_size %llu\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300550 (unsigned long long)le64_to_cpu(trun->new_size));
551 break;
552 }
553 case UBIFS_IDX_NODE:
554 {
555 const struct ubifs_idx_node *idx = node;
556
557 n = le16_to_cpu(idx->child_cnt);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200558 printk(KERN_ERR "\tchild_cnt %d\n", n);
559 printk(KERN_ERR "\tlevel %d\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300560 (int)le16_to_cpu(idx->level));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200561 printk(KERN_ERR "\tBranches:\n");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300562
563 for (i = 0; i < n && i < c->fanout - 1; i++) {
564 const struct ubifs_branch *br;
565
566 br = ubifs_idx_branch(c, idx, i);
567 key_read(c, &br->key, &key);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200568 printk(KERN_ERR "\t%d: LEB %d:%d len %d key %s\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300569 i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
Artem Bityutskiy515315a2012-01-13 12:33:53 +0200570 le32_to_cpu(br->len),
571 dbg_snprintf_key(c, &key, key_buf,
572 DBG_KEY_BUF_LEN));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300573 }
574 break;
575 }
576 case UBIFS_CS_NODE:
577 break;
578 case UBIFS_ORPH_NODE:
579 {
580 const struct ubifs_orph_node *orph = node;
581
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200582 printk(KERN_ERR "\tcommit number %llu\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300583 (unsigned long long)
584 le64_to_cpu(orph->cmt_no) & LLONG_MAX);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200585 printk(KERN_ERR "\tlast node flag %llu\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300586 (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
587 n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200588 printk(KERN_ERR "\t%d orphan inode numbers:\n", n);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300589 for (i = 0; i < n; i++)
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200590 printk(KERN_ERR "\t ino %llu\n",
Alexander Beregalov7424bac2008-09-17 22:09:41 +0400591 (unsigned long long)le64_to_cpu(orph->inos[i]));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300592 break;
593 }
594 default:
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200595 printk(KERN_ERR "node type %d was not recognized\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300596 (int)ch->node_type);
597 }
598 spin_unlock(&dbg_lock);
599}
600
Artem Bityutskiyedf6be22012-05-16 19:15:56 +0300601void ubifs_dump_budget_req(const struct ubifs_budget_req *req)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300602{
603 spin_lock(&dbg_lock);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200604 printk(KERN_ERR "Budgeting request: new_ino %d, dirtied_ino %d\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300605 req->new_ino, req->dirtied_ino);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200606 printk(KERN_ERR "\tnew_ino_d %d, dirtied_ino_d %d\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300607 req->new_ino_d, req->dirtied_ino_d);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200608 printk(KERN_ERR "\tnew_page %d, dirtied_page %d\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300609 req->new_page, req->dirtied_page);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200610 printk(KERN_ERR "\tnew_dent %d, mod_dent %d\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300611 req->new_dent, req->mod_dent);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200612 printk(KERN_ERR "\tidx_growth %d\n", req->idx_growth);
613 printk(KERN_ERR "\tdata_growth %d dd_growth %d\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300614 req->data_growth, req->dd_growth);
615 spin_unlock(&dbg_lock);
616}
617
Artem Bityutskiyedf6be22012-05-16 19:15:56 +0300618void ubifs_dump_lstats(const struct ubifs_lp_stats *lst)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300619{
620 spin_lock(&dbg_lock);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200621 printk(KERN_ERR "(pid %d) Lprops statistics: empty_lebs %d, "
Artem Bityutskiy1de94152008-07-25 12:58:38 +0300622 "idx_lebs %d\n", current->pid, lst->empty_lebs, lst->idx_lebs);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200623 printk(KERN_ERR "\ttaken_empty_lebs %d, total_free %lld, "
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300624 "total_dirty %lld\n", lst->taken_empty_lebs, lst->total_free,
625 lst->total_dirty);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200626 printk(KERN_ERR "\ttotal_used %lld, total_dark %lld, "
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300627 "total_dead %lld\n", lst->total_used, lst->total_dark,
628 lst->total_dead);
629 spin_unlock(&dbg_lock);
630}
631
Artem Bityutskiyedf6be22012-05-16 19:15:56 +0300632void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300633{
634 int i;
635 struct rb_node *rb;
636 struct ubifs_bud *bud;
637 struct ubifs_gced_idx_leb *idx_gc;
Artem Bityutskiy21a60252008-12-12 11:13:17 -0500638 long long available, outstanding, free;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300639
Artem Bityutskiy8ff83082011-03-29 18:19:50 +0300640 spin_lock(&c->space_lock);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300641 spin_lock(&dbg_lock);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200642 printk(KERN_ERR "(pid %d) Budgeting info: data budget sum %lld, "
Artem Bityutskiy8c3067e2011-03-30 13:18:58 +0300643 "total budget sum %lld\n", current->pid,
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300644 bi->data_growth + bi->dd_growth,
645 bi->data_growth + bi->dd_growth + bi->idx_growth);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200646 printk(KERN_ERR "\tbudg_data_growth %lld, budg_dd_growth %lld, "
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300647 "budg_idx_growth %lld\n", bi->data_growth, bi->dd_growth,
648 bi->idx_growth);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200649 printk(KERN_ERR "\tmin_idx_lebs %d, old_idx_sz %llu, "
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300650 "uncommitted_idx %lld\n", bi->min_idx_lebs, bi->old_idx_sz,
651 bi->uncommitted_idx);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200652 printk(KERN_ERR "\tpage_budget %d, inode_budget %d, dent_budget %d\n",
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300653 bi->page_budget, bi->inode_budget, bi->dent_budget);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200654 printk(KERN_ERR "\tnospace %u, nospace_rp %u\n",
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300655 bi->nospace, bi->nospace_rp);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200656 printk(KERN_ERR "\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
Artem Bityutskiy8c3067e2011-03-30 13:18:58 +0300657 c->dark_wm, c->dead_wm, c->max_idx_node_sz);
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300658
659 if (bi != &c->bi)
660 /*
661 * If we are dumping saved budgeting data, do not print
662 * additional information which is about the current state, not
663 * the old one which corresponded to the saved budgeting data.
664 */
665 goto out_unlock;
666
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200667 printk(KERN_ERR "\tfreeable_cnt %d, calc_idx_sz %lld, idx_gc_cnt %d\n",
Artem Bityutskiy8c3067e2011-03-30 13:18:58 +0300668 c->freeable_cnt, c->calc_idx_sz, c->idx_gc_cnt);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200669 printk(KERN_ERR "\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, "
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300670 "clean_zn_cnt %ld\n", atomic_long_read(&c->dirty_pg_cnt),
671 atomic_long_read(&c->dirty_zn_cnt),
672 atomic_long_read(&c->clean_zn_cnt));
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200673 printk(KERN_ERR "\tgc_lnum %d, ihead_lnum %d\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300674 c->gc_lnum, c->ihead_lnum);
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300675
Artem Bityutskiy84abf972009-01-23 14:54:59 +0200676 /* If we are in R/O mode, journal heads do not exist */
677 if (c->jheads)
678 for (i = 0; i < c->jhead_cnt; i++)
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200679 printk(KERN_ERR "\tjhead %s\t LEB %d\n",
Artem Bityutskiy77a7ae52009-09-15 15:03:51 +0300680 dbg_jhead(c->jheads[i].wbuf.jhead),
681 c->jheads[i].wbuf.lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300682 for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
683 bud = rb_entry(rb, struct ubifs_bud, rb);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200684 printk(KERN_ERR "\tbud LEB %d\n", bud->lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300685 }
686 list_for_each_entry(bud, &c->old_buds, list)
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200687 printk(KERN_ERR "\told bud LEB %d\n", bud->lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300688 list_for_each_entry(idx_gc, &c->idx_gc, list)
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200689 printk(KERN_ERR "\tGC'ed idx LEB %d unmap %d\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300690 idx_gc->lnum, idx_gc->unmap);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200691 printk(KERN_ERR "\tcommit state %d\n", c->cmt_state);
Artem Bityutskiy21a60252008-12-12 11:13:17 -0500692
693 /* Print budgeting predictions */
Artem Bityutskiyb1375452011-03-29 18:04:05 +0300694 available = ubifs_calc_available(c, c->bi.min_idx_lebs);
695 outstanding = c->bi.data_growth + c->bi.dd_growth;
Artem Bityutskiy84abf972009-01-23 14:54:59 +0200696 free = ubifs_get_free_space_nolock(c);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200697 printk(KERN_ERR "Budgeting predictions:\n");
698 printk(KERN_ERR "\tavailable: %lld, outstanding %lld, free %lld\n",
Artem Bityutskiy21a60252008-12-12 11:13:17 -0500699 available, outstanding, free);
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300700out_unlock:
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300701 spin_unlock(&dbg_lock);
Artem Bityutskiy8ff83082011-03-29 18:19:50 +0300702 spin_unlock(&c->space_lock);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300703}
704
Artem Bityutskiyedf6be22012-05-16 19:15:56 +0300705void ubifs_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300706{
Artem Bityutskiybe9e62a2008-12-28 10:17:23 +0200707 int i, spc, dark = 0, dead = 0;
708 struct rb_node *rb;
709 struct ubifs_bud *bud;
710
711 spc = lp->free + lp->dirty;
712 if (spc < c->dead_wm)
713 dead = spc;
714 else
715 dark = ubifs_calc_dark(c, spc);
716
717 if (lp->flags & LPROPS_INDEX)
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200718 printk(KERN_ERR "LEB %-7d free %-8d dirty %-8d used %-8d "
Artem Bityutskiybe9e62a2008-12-28 10:17:23 +0200719 "free + dirty %-8d flags %#x (", lp->lnum, lp->free,
720 lp->dirty, c->leb_size - spc, spc, lp->flags);
721 else
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200722 printk(KERN_ERR "LEB %-7d free %-8d dirty %-8d used %-8d "
Artem Bityutskiybe9e62a2008-12-28 10:17:23 +0200723 "free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d "
724 "flags %#-4x (", lp->lnum, lp->free, lp->dirty,
725 c->leb_size - spc, spc, dark, dead,
726 (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags);
727
728 if (lp->flags & LPROPS_TAKEN) {
729 if (lp->flags & LPROPS_INDEX)
730 printk(KERN_CONT "index, taken");
731 else
732 printk(KERN_CONT "taken");
733 } else {
734 const char *s;
735
736 if (lp->flags & LPROPS_INDEX) {
737 switch (lp->flags & LPROPS_CAT_MASK) {
738 case LPROPS_DIRTY_IDX:
739 s = "dirty index";
740 break;
741 case LPROPS_FRDI_IDX:
742 s = "freeable index";
743 break;
744 default:
745 s = "index";
746 }
747 } else {
748 switch (lp->flags & LPROPS_CAT_MASK) {
749 case LPROPS_UNCAT:
750 s = "not categorized";
751 break;
752 case LPROPS_DIRTY:
753 s = "dirty";
754 break;
755 case LPROPS_FREE:
756 s = "free";
757 break;
758 case LPROPS_EMPTY:
759 s = "empty";
760 break;
761 case LPROPS_FREEABLE:
762 s = "freeable";
763 break;
764 default:
765 s = NULL;
766 break;
767 }
768 }
769 printk(KERN_CONT "%s", s);
770 }
771
772 for (rb = rb_first((struct rb_root *)&c->buds); rb; rb = rb_next(rb)) {
773 bud = rb_entry(rb, struct ubifs_bud, rb);
774 if (bud->lnum == lp->lnum) {
775 int head = 0;
776 for (i = 0; i < c->jhead_cnt; i++) {
Artem Bityutskiy13216572011-04-24 10:53:17 +0300777 /*
778 * Note, if we are in R/O mode or in the middle
779 * of mounting/re-mounting, the write-buffers do
780 * not exist.
781 */
782 if (c->jheads &&
783 lp->lnum == c->jheads[i].wbuf.lnum) {
Artem Bityutskiybe9e62a2008-12-28 10:17:23 +0200784 printk(KERN_CONT ", jhead %s",
785 dbg_jhead(i));
786 head = 1;
787 }
788 }
789 if (!head)
790 printk(KERN_CONT ", bud of jhead %s",
791 dbg_jhead(bud->jhead));
792 }
793 }
794 if (lp->lnum == c->gc_lnum)
795 printk(KERN_CONT ", GC LEB");
796 printk(KERN_CONT ")\n");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300797}
798
Artem Bityutskiyedf6be22012-05-16 19:15:56 +0300799void ubifs_dump_lprops(struct ubifs_info *c)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300800{
801 int lnum, err;
802 struct ubifs_lprops lp;
803 struct ubifs_lp_stats lst;
804
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200805 printk(KERN_ERR "(pid %d) start dumping LEB properties\n",
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200806 current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300807 ubifs_get_lp_stats(c, &lst);
Artem Bityutskiyedf6be22012-05-16 19:15:56 +0300808 ubifs_dump_lstats(&lst);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300809
810 for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
811 err = ubifs_read_one_lp(c, lnum, &lp);
812 if (err)
813 ubifs_err("cannot read lprops for LEB %d", lnum);
814
Artem Bityutskiyedf6be22012-05-16 19:15:56 +0300815 ubifs_dump_lprop(c, &lp);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300816 }
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200817 printk(KERN_ERR "(pid %d) finish dumping LEB properties\n",
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200818 current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300819}
820
Artem Bityutskiyedf6be22012-05-16 19:15:56 +0300821void ubifs_dump_lpt_info(struct ubifs_info *c)
Adrian Hunter73944a62008-09-12 18:13:31 +0300822{
823 int i;
824
825 spin_lock(&dbg_lock);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200826 printk(KERN_ERR "(pid %d) dumping LPT information\n", current->pid);
827 printk(KERN_ERR "\tlpt_sz: %lld\n", c->lpt_sz);
828 printk(KERN_ERR "\tpnode_sz: %d\n", c->pnode_sz);
829 printk(KERN_ERR "\tnnode_sz: %d\n", c->nnode_sz);
830 printk(KERN_ERR "\tltab_sz: %d\n", c->ltab_sz);
831 printk(KERN_ERR "\tlsave_sz: %d\n", c->lsave_sz);
832 printk(KERN_ERR "\tbig_lpt: %d\n", c->big_lpt);
833 printk(KERN_ERR "\tlpt_hght: %d\n", c->lpt_hght);
834 printk(KERN_ERR "\tpnode_cnt: %d\n", c->pnode_cnt);
835 printk(KERN_ERR "\tnnode_cnt: %d\n", c->nnode_cnt);
836 printk(KERN_ERR "\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt);
837 printk(KERN_ERR "\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt);
838 printk(KERN_ERR "\tlsave_cnt: %d\n", c->lsave_cnt);
839 printk(KERN_ERR "\tspace_bits: %d\n", c->space_bits);
840 printk(KERN_ERR "\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
841 printk(KERN_ERR "\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
842 printk(KERN_ERR "\tlpt_spc_bits: %d\n", c->lpt_spc_bits);
843 printk(KERN_ERR "\tpcnt_bits: %d\n", c->pcnt_bits);
844 printk(KERN_ERR "\tlnum_bits: %d\n", c->lnum_bits);
845 printk(KERN_ERR "\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
846 printk(KERN_ERR "\tLPT head is at %d:%d\n",
Adrian Hunter73944a62008-09-12 18:13:31 +0300847 c->nhead_lnum, c->nhead_offs);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200848 printk(KERN_ERR "\tLPT ltab is at %d:%d\n",
Artem Bityutskiyf92b9822008-12-28 11:34:26 +0200849 c->ltab_lnum, c->ltab_offs);
Adrian Hunter73944a62008-09-12 18:13:31 +0300850 if (c->big_lpt)
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200851 printk(KERN_ERR "\tLPT lsave is at %d:%d\n",
Adrian Hunter73944a62008-09-12 18:13:31 +0300852 c->lsave_lnum, c->lsave_offs);
853 for (i = 0; i < c->lpt_lebs; i++)
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200854 printk(KERN_ERR "\tLPT LEB %d free %d dirty %d tgc %d "
Adrian Hunter73944a62008-09-12 18:13:31 +0300855 "cmt %d\n", i + c->lpt_first, c->ltab[i].free,
856 c->ltab[i].dirty, c->ltab[i].tgc, c->ltab[i].cmt);
857 spin_unlock(&dbg_lock);
858}
859
Artem Bityutskiyedf6be22012-05-16 19:15:56 +0300860void ubifs_dump_sleb(const struct ubifs_info *c,
861 const struct ubifs_scan_leb *sleb, int offs)
Artem Bityutskiyd37854c2011-08-22 16:23:56 +0300862{
863 struct ubifs_scan_node *snod;
864
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200865 printk(KERN_ERR "(pid %d) start dumping scanned data from LEB %d:%d\n",
Artem Bityutskiyd37854c2011-08-22 16:23:56 +0300866 current->pid, sleb->lnum, offs);
867
868 list_for_each_entry(snod, &sleb->nodes, list) {
869 cond_resched();
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200870 printk(KERN_ERR "Dumping node at LEB %d:%d len %d\n", sleb->lnum,
Artem Bityutskiyd37854c2011-08-22 16:23:56 +0300871 snod->offs, snod->len);
Artem Bityutskiyedf6be22012-05-16 19:15:56 +0300872 ubifs_dump_node(c, snod->node);
Artem Bityutskiyd37854c2011-08-22 16:23:56 +0300873 }
874}
875
Artem Bityutskiyedf6be22012-05-16 19:15:56 +0300876void ubifs_dump_leb(const struct ubifs_info *c, int lnum)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300877{
878 struct ubifs_scan_leb *sleb;
879 struct ubifs_scan_node *snod;
Artem Bityutskiy73d9aec2011-03-11 15:39:09 +0200880 void *buf;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300881
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200882 printk(KERN_ERR "(pid %d) start dumping LEB %d\n",
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200883 current->pid, lnum);
Artem Bityutskiy73d9aec2011-03-11 15:39:09 +0200884
Artem Bityutskiyfc5e58c2011-03-24 16:14:26 +0200885 buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
Artem Bityutskiy73d9aec2011-03-11 15:39:09 +0200886 if (!buf) {
887 ubifs_err("cannot allocate memory for dumping LEB %d", lnum);
888 return;
889 }
890
891 sleb = ubifs_scan(c, lnum, 0, buf, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300892 if (IS_ERR(sleb)) {
893 ubifs_err("scan error %d", (int)PTR_ERR(sleb));
Artem Bityutskiy73d9aec2011-03-11 15:39:09 +0200894 goto out;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300895 }
896
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200897 printk(KERN_ERR "LEB %d has %d nodes ending at %d\n", lnum,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300898 sleb->nodes_cnt, sleb->endpt);
899
900 list_for_each_entry(snod, &sleb->nodes, list) {
901 cond_resched();
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200902 printk(KERN_ERR "Dumping node at LEB %d:%d len %d\n", lnum,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300903 snod->offs, snod->len);
Artem Bityutskiyedf6be22012-05-16 19:15:56 +0300904 ubifs_dump_node(c, snod->node);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300905 }
906
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200907 printk(KERN_ERR "(pid %d) finish dumping LEB %d\n",
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200908 current->pid, lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300909 ubifs_scan_destroy(sleb);
Artem Bityutskiy73d9aec2011-03-11 15:39:09 +0200910
911out:
912 vfree(buf);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300913 return;
914}
915
Artem Bityutskiyedf6be22012-05-16 19:15:56 +0300916void ubifs_dump_znode(const struct ubifs_info *c,
917 const struct ubifs_znode *znode)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300918{
919 int n;
920 const struct ubifs_zbranch *zbr;
Artem Bityutskiy515315a2012-01-13 12:33:53 +0200921 char key_buf[DBG_KEY_BUF_LEN];
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300922
923 spin_lock(&dbg_lock);
924 if (znode->parent)
925 zbr = &znode->parent->zbranch[znode->iip];
926 else
927 zbr = &c->zroot;
928
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200929 printk(KERN_ERR "znode %p, LEB %d:%d len %d parent %p iip %d level %d"
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300930 " child_cnt %d flags %lx\n", znode, zbr->lnum, zbr->offs,
931 zbr->len, znode->parent, znode->iip, znode->level,
932 znode->child_cnt, znode->flags);
933
934 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
935 spin_unlock(&dbg_lock);
936 return;
937 }
938
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200939 printk(KERN_ERR "zbranches:\n");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300940 for (n = 0; n < znode->child_cnt; n++) {
941 zbr = &znode->zbranch[n];
942 if (znode->level > 0)
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200943 printk(KERN_ERR "\t%d: znode %p LEB %d:%d len %d key "
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300944 "%s\n", n, zbr->znode, zbr->lnum,
945 zbr->offs, zbr->len,
Artem Bityutskiy515315a2012-01-13 12:33:53 +0200946 dbg_snprintf_key(c, &zbr->key,
947 key_buf,
948 DBG_KEY_BUF_LEN));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300949 else
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200950 printk(KERN_ERR "\t%d: LNC %p LEB %d:%d len %d key "
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300951 "%s\n", n, zbr->znode, zbr->lnum,
952 zbr->offs, zbr->len,
Artem Bityutskiy515315a2012-01-13 12:33:53 +0200953 dbg_snprintf_key(c, &zbr->key,
954 key_buf,
955 DBG_KEY_BUF_LEN));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300956 }
957 spin_unlock(&dbg_lock);
958}
959
Artem Bityutskiyedf6be22012-05-16 19:15:56 +0300960void ubifs_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300961{
962 int i;
963
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200964 printk(KERN_ERR "(pid %d) start dumping heap cat %d (%d elements)\n",
Artem Bityutskiy1de94152008-07-25 12:58:38 +0300965 current->pid, cat, heap->cnt);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300966 for (i = 0; i < heap->cnt; i++) {
967 struct ubifs_lprops *lprops = heap->arr[i];
968
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200969 printk(KERN_ERR "\t%d. LEB %d hpos %d free %d dirty %d "
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300970 "flags %d\n", i, lprops->lnum, lprops->hpos,
971 lprops->free, lprops->dirty, lprops->flags);
972 }
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200973 printk(KERN_ERR "(pid %d) finish dumping heap\n", current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300974}
975
Artem Bityutskiyedf6be22012-05-16 19:15:56 +0300976void ubifs_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
977 struct ubifs_nnode *parent, int iip)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300978{
979 int i;
980
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200981 printk(KERN_ERR "(pid %d) dumping pnode:\n", current->pid);
982 printk(KERN_ERR "\taddress %zx parent %zx cnext %zx\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300983 (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200984 printk(KERN_ERR "\tflags %lu iip %d level %d num %d\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300985 pnode->flags, iip, pnode->level, pnode->num);
986 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
987 struct ubifs_lprops *lp = &pnode->lprops[i];
988
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200989 printk(KERN_ERR "\t%d: free %d dirty %d flags %d lnum %d\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300990 i, lp->free, lp->dirty, lp->flags, lp->lnum);
991 }
992}
993
Artem Bityutskiyedf6be22012-05-16 19:15:56 +0300994void ubifs_dump_tnc(struct ubifs_info *c)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300995{
996 struct ubifs_znode *znode;
997 int level;
998
Artem Bityutskiy16c395c2012-01-18 15:46:13 +0200999 printk(KERN_ERR "\n");
1000 printk(KERN_ERR "(pid %d) start dumping TNC tree\n", current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001001 znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
1002 level = znode->level;
Artem Bityutskiy16c395c2012-01-18 15:46:13 +02001003 printk(KERN_ERR "== Level %d ==\n", level);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001004 while (znode) {
1005 if (level != znode->level) {
1006 level = znode->level;
Artem Bityutskiy16c395c2012-01-18 15:46:13 +02001007 printk(KERN_ERR "== Level %d ==\n", level);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001008 }
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03001009 ubifs_dump_znode(c, znode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001010 znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
1011 }
Artem Bityutskiy16c395c2012-01-18 15:46:13 +02001012 printk(KERN_ERR "(pid %d) finish dumping TNC tree\n", current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001013}
1014
1015static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
1016 void *priv)
1017{
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03001018 ubifs_dump_znode(c, znode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001019 return 0;
1020}
1021
1022/**
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03001023 * ubifs_dump_index - dump the on-flash index.
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001024 * @c: UBIFS file-system description object
1025 *
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03001026 * This function dumps whole UBIFS indexing B-tree, unlike 'ubifs_dump_tnc()'
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001027 * which dumps only in-memory znodes and does not read znodes which from flash.
1028 */
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03001029void ubifs_dump_index(struct ubifs_info *c)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001030{
1031 dbg_walk_index(c, NULL, dump_znode, NULL);
1032}
1033
1034/**
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001035 * dbg_save_space_info - save information about flash space.
1036 * @c: UBIFS file-system description object
1037 *
1038 * This function saves information about UBIFS free space, dirty space, etc, in
1039 * order to check it later.
1040 */
1041void dbg_save_space_info(struct ubifs_info *c)
1042{
1043 struct ubifs_debug_info *d = c->dbg;
Artem Bityutskiy7da64432011-04-04 17:16:39 +03001044 int freeable_cnt;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001045
1046 spin_lock(&c->space_lock);
Artem Bityutskiy7da64432011-04-04 17:16:39 +03001047 memcpy(&d->saved_lst, &c->lst, sizeof(struct ubifs_lp_stats));
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +03001048 memcpy(&d->saved_bi, &c->bi, sizeof(struct ubifs_budg_info));
1049 d->saved_idx_gc_cnt = c->idx_gc_cnt;
Artem Bityutskiy7da64432011-04-04 17:16:39 +03001050
1051 /*
1052 * We use a dirty hack here and zero out @c->freeable_cnt, because it
1053 * affects the free space calculations, and UBIFS might not know about
1054 * all freeable eraseblocks. Indeed, we know about freeable eraseblocks
1055 * only when we read their lprops, and we do this only lazily, upon the
1056 * need. So at any given point of time @c->freeable_cnt might be not
1057 * exactly accurate.
1058 *
1059 * Just one example about the issue we hit when we did not zero
1060 * @c->freeable_cnt.
1061 * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the
1062 * amount of free space in @d->saved_free
1063 * 2. We re-mount R/W, which makes UBIFS to read the "lsave"
1064 * information from flash, where we cache LEBs from various
1065 * categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()'
1066 * -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()'
1067 * -> 'ubifs_get_pnode()' -> 'update_cats()'
1068 * -> 'ubifs_add_to_cat()').
1069 * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt
1070 * becomes %1.
1071 * 4. We calculate the amount of free space when the re-mount is
1072 * finished in 'dbg_check_space_info()' and it does not match
1073 * @d->saved_free.
1074 */
1075 freeable_cnt = c->freeable_cnt;
1076 c->freeable_cnt = 0;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001077 d->saved_free = ubifs_get_free_space_nolock(c);
Artem Bityutskiy7da64432011-04-04 17:16:39 +03001078 c->freeable_cnt = freeable_cnt;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001079 spin_unlock(&c->space_lock);
1080}
1081
1082/**
1083 * dbg_check_space_info - check flash space information.
1084 * @c: UBIFS file-system description object
1085 *
1086 * This function compares current flash space information with the information
1087 * which was saved when the 'dbg_save_space_info()' function was called.
1088 * Returns zero if the information has not changed, and %-EINVAL it it has
1089 * changed.
1090 */
1091int dbg_check_space_info(struct ubifs_info *c)
1092{
1093 struct ubifs_debug_info *d = c->dbg;
1094 struct ubifs_lp_stats lst;
Artem Bityutskiy7da64432011-04-04 17:16:39 +03001095 long long free;
1096 int freeable_cnt;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001097
1098 spin_lock(&c->space_lock);
Artem Bityutskiy7da64432011-04-04 17:16:39 +03001099 freeable_cnt = c->freeable_cnt;
1100 c->freeable_cnt = 0;
1101 free = ubifs_get_free_space_nolock(c);
1102 c->freeable_cnt = freeable_cnt;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001103 spin_unlock(&c->space_lock);
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001104
1105 if (free != d->saved_free) {
1106 ubifs_err("free space changed from %lld to %lld",
1107 d->saved_free, free);
1108 goto out;
1109 }
1110
1111 return 0;
1112
1113out:
1114 ubifs_msg("saved lprops statistics dump");
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03001115 ubifs_dump_lstats(&d->saved_lst);
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +03001116 ubifs_msg("saved budgeting info dump");
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03001117 ubifs_dump_budg(c, &d->saved_bi);
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +03001118 ubifs_msg("saved idx_gc_cnt %d", d->saved_idx_gc_cnt);
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001119 ubifs_msg("current lprops statistics dump");
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +03001120 ubifs_get_lp_stats(c, &lst);
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03001121 ubifs_dump_lstats(&lst);
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +03001122 ubifs_msg("current budgeting info dump");
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03001123 ubifs_dump_budg(c, &c->bi);
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001124 dump_stack();
1125 return -EINVAL;
1126}
1127
1128/**
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001129 * dbg_check_synced_i_size - check synchronized inode size.
Artem Bityutskiyd808efb2011-05-31 18:14:38 +03001130 * @c: UBIFS file-system description object
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001131 * @inode: inode to check
1132 *
1133 * If inode is clean, synchronized inode size has to be equivalent to current
1134 * inode size. This function has to be called only for locked inodes (@i_mutex
1135 * has to be locked). Returns %0 if synchronized inode size if correct, and
1136 * %-EINVAL if not.
1137 */
Artem Bityutskiyd808efb2011-05-31 18:14:38 +03001138int dbg_check_synced_i_size(const struct ubifs_info *c, struct inode *inode)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001139{
1140 int err = 0;
1141 struct ubifs_inode *ui = ubifs_inode(inode);
1142
Artem Bityutskiy2b1844a2011-06-03 08:31:29 +03001143 if (!dbg_is_chk_gen(c))
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001144 return 0;
1145 if (!S_ISREG(inode->i_mode))
1146 return 0;
1147
1148 mutex_lock(&ui->ui_mutex);
1149 spin_lock(&ui->ui_lock);
1150 if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
1151 ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode "
1152 "is clean", ui->ui_size, ui->synced_i_size);
1153 ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
1154 inode->i_mode, i_size_read(inode));
Artem Bityutskiy7c46d0a2012-05-16 19:04:54 +03001155 dump_stack();
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001156 err = -EINVAL;
1157 }
1158 spin_unlock(&ui->ui_lock);
1159 mutex_unlock(&ui->ui_mutex);
1160 return err;
1161}
1162
1163/*
1164 * dbg_check_dir - check directory inode size and link count.
1165 * @c: UBIFS file-system description object
1166 * @dir: the directory to calculate size for
1167 * @size: the result is returned here
1168 *
1169 * This function makes sure that directory size and link count are correct.
1170 * Returns zero in case of success and a negative error code in case of
1171 * failure.
1172 *
1173 * Note, it is good idea to make sure the @dir->i_mutex is locked before
1174 * calling this function.
1175 */
Artem Bityutskiy1b51e982011-05-25 17:38:29 +03001176int dbg_check_dir(struct ubifs_info *c, const struct inode *dir)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001177{
1178 unsigned int nlink = 2;
1179 union ubifs_key key;
1180 struct ubifs_dent_node *dent, *pdent = NULL;
1181 struct qstr nm = { .name = NULL };
1182 loff_t size = UBIFS_INO_NODE_SZ;
1183
Artem Bityutskiy2b1844a2011-06-03 08:31:29 +03001184 if (!dbg_is_chk_gen(c))
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001185 return 0;
1186
1187 if (!S_ISDIR(dir->i_mode))
1188 return 0;
1189
1190 lowest_dent_key(c, &key, dir->i_ino);
1191 while (1) {
1192 int err;
1193
1194 dent = ubifs_tnc_next_ent(c, &key, &nm);
1195 if (IS_ERR(dent)) {
1196 err = PTR_ERR(dent);
1197 if (err == -ENOENT)
1198 break;
1199 return err;
1200 }
1201
1202 nm.name = dent->name;
1203 nm.len = le16_to_cpu(dent->nlen);
1204 size += CALC_DENT_SIZE(nm.len);
1205 if (dent->type == UBIFS_ITYPE_DIR)
1206 nlink += 1;
1207 kfree(pdent);
1208 pdent = dent;
1209 key_read(c, &dent->key, &key);
1210 }
1211 kfree(pdent);
1212
1213 if (i_size_read(dir) != size) {
1214 ubifs_err("directory inode %lu has size %llu, "
1215 "but calculated size is %llu", dir->i_ino,
1216 (unsigned long long)i_size_read(dir),
1217 (unsigned long long)size);
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03001218 ubifs_dump_inode(c, dir);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001219 dump_stack();
1220 return -EINVAL;
1221 }
1222 if (dir->i_nlink != nlink) {
1223 ubifs_err("directory inode %lu has nlink %u, but calculated "
1224 "nlink is %u", dir->i_ino, dir->i_nlink, nlink);
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03001225 ubifs_dump_inode(c, dir);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001226 dump_stack();
1227 return -EINVAL;
1228 }
1229
1230 return 0;
1231}
1232
1233/**
1234 * dbg_check_key_order - make sure that colliding keys are properly ordered.
1235 * @c: UBIFS file-system description object
1236 * @zbr1: first zbranch
1237 * @zbr2: following zbranch
1238 *
1239 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
1240 * names of the direntries/xentries which are referred by the keys. This
1241 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
1242 * sure the name of direntry/xentry referred by @zbr1 is less than
1243 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
1244 * and a negative error code in case of failure.
1245 */
1246static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
1247 struct ubifs_zbranch *zbr2)
1248{
1249 int err, nlen1, nlen2, cmp;
1250 struct ubifs_dent_node *dent1, *dent2;
1251 union ubifs_key key;
Artem Bityutskiy515315a2012-01-13 12:33:53 +02001252 char key_buf[DBG_KEY_BUF_LEN];
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001253
1254 ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
1255 dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1256 if (!dent1)
1257 return -ENOMEM;
1258 dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1259 if (!dent2) {
1260 err = -ENOMEM;
1261 goto out_free;
1262 }
1263
1264 err = ubifs_tnc_read_node(c, zbr1, dent1);
1265 if (err)
1266 goto out_free;
1267 err = ubifs_validate_entry(c, dent1);
1268 if (err)
1269 goto out_free;
1270
1271 err = ubifs_tnc_read_node(c, zbr2, dent2);
1272 if (err)
1273 goto out_free;
1274 err = ubifs_validate_entry(c, dent2);
1275 if (err)
1276 goto out_free;
1277
1278 /* Make sure node keys are the same as in zbranch */
1279 err = 1;
1280 key_read(c, &dent1->key, &key);
1281 if (keys_cmp(c, &zbr1->key, &key)) {
Artem Bityutskiya6aae4d2012-05-16 20:11:23 +03001282 ubifs_err("1st entry at %d:%d has key %s", zbr1->lnum,
1283 zbr1->offs, dbg_snprintf_key(c, &key, key_buf,
1284 DBG_KEY_BUF_LEN));
1285 ubifs_err("but it should have key %s according to tnc",
1286 dbg_snprintf_key(c, &zbr1->key, key_buf,
1287 DBG_KEY_BUF_LEN));
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03001288 ubifs_dump_node(c, dent1);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03001289 goto out_free;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001290 }
1291
1292 key_read(c, &dent2->key, &key);
1293 if (keys_cmp(c, &zbr2->key, &key)) {
Artem Bityutskiya6aae4d2012-05-16 20:11:23 +03001294 ubifs_err("2nd entry at %d:%d has key %s", zbr1->lnum,
1295 zbr1->offs, dbg_snprintf_key(c, &key, key_buf,
1296 DBG_KEY_BUF_LEN));
1297 ubifs_err("but it should have key %s according to tnc",
1298 dbg_snprintf_key(c, &zbr2->key, key_buf,
1299 DBG_KEY_BUF_LEN));
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03001300 ubifs_dump_node(c, dent2);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03001301 goto out_free;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001302 }
1303
1304 nlen1 = le16_to_cpu(dent1->nlen);
1305 nlen2 = le16_to_cpu(dent2->nlen);
1306
1307 cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
1308 if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
1309 err = 0;
1310 goto out_free;
1311 }
1312 if (cmp == 0 && nlen1 == nlen2)
Artem Bityutskiya6aae4d2012-05-16 20:11:23 +03001313 ubifs_err("2 xent/dent nodes with the same name");
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001314 else
Artem Bityutskiya6aae4d2012-05-16 20:11:23 +03001315 ubifs_err("bad order of colliding key %s",
1316 dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001317
Artem Bityutskiy552ff312008-10-23 11:49:28 +03001318 ubifs_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs);
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03001319 ubifs_dump_node(c, dent1);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03001320 ubifs_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs);
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03001321 ubifs_dump_node(c, dent2);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001322
1323out_free:
1324 kfree(dent2);
1325 kfree(dent1);
1326 return err;
1327}
1328
1329/**
1330 * dbg_check_znode - check if znode is all right.
1331 * @c: UBIFS file-system description object
1332 * @zbr: zbranch which points to this znode
1333 *
1334 * This function makes sure that znode referred to by @zbr is all right.
1335 * Returns zero if it is, and %-EINVAL if it is not.
1336 */
1337static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
1338{
1339 struct ubifs_znode *znode = zbr->znode;
1340 struct ubifs_znode *zp = znode->parent;
1341 int n, err, cmp;
1342
1343 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
1344 err = 1;
1345 goto out;
1346 }
1347 if (znode->level < 0) {
1348 err = 2;
1349 goto out;
1350 }
1351 if (znode->iip < 0 || znode->iip >= c->fanout) {
1352 err = 3;
1353 goto out;
1354 }
1355
1356 if (zbr->len == 0)
1357 /* Only dirty zbranch may have no on-flash nodes */
1358 if (!ubifs_zn_dirty(znode)) {
1359 err = 4;
1360 goto out;
1361 }
1362
1363 if (ubifs_zn_dirty(znode)) {
1364 /*
1365 * If znode is dirty, its parent has to be dirty as well. The
1366 * order of the operation is important, so we have to have
1367 * memory barriers.
1368 */
1369 smp_mb();
1370 if (zp && !ubifs_zn_dirty(zp)) {
1371 /*
1372 * The dirty flag is atomic and is cleared outside the
1373 * TNC mutex, so znode's dirty flag may now have
1374 * been cleared. The child is always cleared before the
1375 * parent, so we just need to check again.
1376 */
1377 smp_mb();
1378 if (ubifs_zn_dirty(znode)) {
1379 err = 5;
1380 goto out;
1381 }
1382 }
1383 }
1384
1385 if (zp) {
1386 const union ubifs_key *min, *max;
1387
1388 if (znode->level != zp->level - 1) {
1389 err = 6;
1390 goto out;
1391 }
1392
1393 /* Make sure the 'parent' pointer in our znode is correct */
1394 err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
1395 if (!err) {
1396 /* This zbranch does not exist in the parent */
1397 err = 7;
1398 goto out;
1399 }
1400
1401 if (znode->iip >= zp->child_cnt) {
1402 err = 8;
1403 goto out;
1404 }
1405
1406 if (znode->iip != n) {
1407 /* This may happen only in case of collisions */
1408 if (keys_cmp(c, &zp->zbranch[n].key,
1409 &zp->zbranch[znode->iip].key)) {
1410 err = 9;
1411 goto out;
1412 }
1413 n = znode->iip;
1414 }
1415
1416 /*
1417 * Make sure that the first key in our znode is greater than or
1418 * equal to the key in the pointing zbranch.
1419 */
1420 min = &zbr->key;
1421 cmp = keys_cmp(c, min, &znode->zbranch[0].key);
1422 if (cmp == 1) {
1423 err = 10;
1424 goto out;
1425 }
1426
1427 if (n + 1 < zp->child_cnt) {
1428 max = &zp->zbranch[n + 1].key;
1429
1430 /*
1431 * Make sure the last key in our znode is less or
Artem Bityutskiy7d4e9cc2009-03-20 19:11:12 +02001432 * equivalent than the key in the zbranch which goes
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001433 * after our pointing zbranch.
1434 */
1435 cmp = keys_cmp(c, max,
1436 &znode->zbranch[znode->child_cnt - 1].key);
1437 if (cmp == -1) {
1438 err = 11;
1439 goto out;
1440 }
1441 }
1442 } else {
1443 /* This may only be root znode */
1444 if (zbr != &c->zroot) {
1445 err = 12;
1446 goto out;
1447 }
1448 }
1449
1450 /*
1451 * Make sure that next key is greater or equivalent then the previous
1452 * one.
1453 */
1454 for (n = 1; n < znode->child_cnt; n++) {
1455 cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
1456 &znode->zbranch[n].key);
1457 if (cmp > 0) {
1458 err = 13;
1459 goto out;
1460 }
1461 if (cmp == 0) {
1462 /* This can only be keys with colliding hash */
1463 if (!is_hash_key(c, &znode->zbranch[n].key)) {
1464 err = 14;
1465 goto out;
1466 }
1467
1468 if (znode->level != 0 || c->replaying)
1469 continue;
1470
1471 /*
1472 * Colliding keys should follow binary order of
1473 * corresponding xentry/dentry names.
1474 */
1475 err = dbg_check_key_order(c, &znode->zbranch[n - 1],
1476 &znode->zbranch[n]);
1477 if (err < 0)
1478 return err;
1479 if (err) {
1480 err = 15;
1481 goto out;
1482 }
1483 }
1484 }
1485
1486 for (n = 0; n < znode->child_cnt; n++) {
1487 if (!znode->zbranch[n].znode &&
1488 (znode->zbranch[n].lnum == 0 ||
1489 znode->zbranch[n].len == 0)) {
1490 err = 16;
1491 goto out;
1492 }
1493
1494 if (znode->zbranch[n].lnum != 0 &&
1495 znode->zbranch[n].len == 0) {
1496 err = 17;
1497 goto out;
1498 }
1499
1500 if (znode->zbranch[n].lnum == 0 &&
1501 znode->zbranch[n].len != 0) {
1502 err = 18;
1503 goto out;
1504 }
1505
1506 if (znode->zbranch[n].lnum == 0 &&
1507 znode->zbranch[n].offs != 0) {
1508 err = 19;
1509 goto out;
1510 }
1511
1512 if (znode->level != 0 && znode->zbranch[n].znode)
1513 if (znode->zbranch[n].znode->parent != znode) {
1514 err = 20;
1515 goto out;
1516 }
1517 }
1518
1519 return 0;
1520
1521out:
1522 ubifs_err("failed, error %d", err);
1523 ubifs_msg("dump of the znode");
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03001524 ubifs_dump_znode(c, znode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001525 if (zp) {
1526 ubifs_msg("dump of the parent znode");
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03001527 ubifs_dump_znode(c, zp);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001528 }
1529 dump_stack();
1530 return -EINVAL;
1531}
1532
1533/**
1534 * dbg_check_tnc - check TNC tree.
1535 * @c: UBIFS file-system description object
1536 * @extra: do extra checks that are possible at start commit
1537 *
1538 * This function traverses whole TNC tree and checks every znode. Returns zero
1539 * if everything is all right and %-EINVAL if something is wrong with TNC.
1540 */
1541int dbg_check_tnc(struct ubifs_info *c, int extra)
1542{
1543 struct ubifs_znode *znode;
1544 long clean_cnt = 0, dirty_cnt = 0;
1545 int err, last;
1546
Artem Bityutskiy8d7819b2011-06-03 08:53:35 +03001547 if (!dbg_is_chk_index(c))
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001548 return 0;
1549
1550 ubifs_assert(mutex_is_locked(&c->tnc_mutex));
1551 if (!c->zroot.znode)
1552 return 0;
1553
1554 znode = ubifs_tnc_postorder_first(c->zroot.znode);
1555 while (1) {
1556 struct ubifs_znode *prev;
1557 struct ubifs_zbranch *zbr;
1558
1559 if (!znode->parent)
1560 zbr = &c->zroot;
1561 else
1562 zbr = &znode->parent->zbranch[znode->iip];
1563
1564 err = dbg_check_znode(c, zbr);
1565 if (err)
1566 return err;
1567
1568 if (extra) {
1569 if (ubifs_zn_dirty(znode))
1570 dirty_cnt += 1;
1571 else
1572 clean_cnt += 1;
1573 }
1574
1575 prev = znode;
1576 znode = ubifs_tnc_postorder_next(znode);
1577 if (!znode)
1578 break;
1579
1580 /*
1581 * If the last key of this znode is equivalent to the first key
1582 * of the next znode (collision), then check order of the keys.
1583 */
1584 last = prev->child_cnt - 1;
1585 if (prev->level == 0 && znode->level == 0 && !c->replaying &&
1586 !keys_cmp(c, &prev->zbranch[last].key,
1587 &znode->zbranch[0].key)) {
1588 err = dbg_check_key_order(c, &prev->zbranch[last],
1589 &znode->zbranch[0]);
1590 if (err < 0)
1591 return err;
1592 if (err) {
1593 ubifs_msg("first znode");
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03001594 ubifs_dump_znode(c, prev);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001595 ubifs_msg("second znode");
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03001596 ubifs_dump_znode(c, znode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001597 return -EINVAL;
1598 }
1599 }
1600 }
1601
1602 if (extra) {
1603 if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
1604 ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld",
1605 atomic_long_read(&c->clean_zn_cnt),
1606 clean_cnt);
1607 return -EINVAL;
1608 }
1609 if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
1610 ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld",
1611 atomic_long_read(&c->dirty_zn_cnt),
1612 dirty_cnt);
1613 return -EINVAL;
1614 }
1615 }
1616
1617 return 0;
1618}
1619
1620/**
1621 * dbg_walk_index - walk the on-flash index.
1622 * @c: UBIFS file-system description object
1623 * @leaf_cb: called for each leaf node
1624 * @znode_cb: called for each indexing node
Adrian Hunter227c75c2009-01-29 11:53:51 +02001625 * @priv: private data which is passed to callbacks
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001626 *
1627 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1628 * node and @znode_cb for each indexing node. Returns zero in case of success
1629 * and a negative error code in case of failure.
1630 *
1631 * It would be better if this function removed every znode it pulled to into
1632 * the TNC, so that the behavior more closely matched the non-debugging
1633 * behavior.
1634 */
1635int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1636 dbg_znode_callback znode_cb, void *priv)
1637{
1638 int err;
1639 struct ubifs_zbranch *zbr;
1640 struct ubifs_znode *znode, *child;
1641
1642 mutex_lock(&c->tnc_mutex);
1643 /* If the root indexing node is not in TNC - pull it */
1644 if (!c->zroot.znode) {
1645 c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1646 if (IS_ERR(c->zroot.znode)) {
1647 err = PTR_ERR(c->zroot.znode);
1648 c->zroot.znode = NULL;
1649 goto out_unlock;
1650 }
1651 }
1652
1653 /*
1654 * We are going to traverse the indexing tree in the postorder manner.
1655 * Go down and find the leftmost indexing node where we are going to
1656 * start from.
1657 */
1658 znode = c->zroot.znode;
1659 while (znode->level > 0) {
1660 zbr = &znode->zbranch[0];
1661 child = zbr->znode;
1662 if (!child) {
1663 child = ubifs_load_znode(c, zbr, znode, 0);
1664 if (IS_ERR(child)) {
1665 err = PTR_ERR(child);
1666 goto out_unlock;
1667 }
1668 zbr->znode = child;
1669 }
1670
1671 znode = child;
1672 }
1673
1674 /* Iterate over all indexing nodes */
1675 while (1) {
1676 int idx;
1677
1678 cond_resched();
1679
1680 if (znode_cb) {
1681 err = znode_cb(c, znode, priv);
1682 if (err) {
1683 ubifs_err("znode checking function returned "
1684 "error %d", err);
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03001685 ubifs_dump_znode(c, znode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001686 goto out_dump;
1687 }
1688 }
1689 if (leaf_cb && znode->level == 0) {
1690 for (idx = 0; idx < znode->child_cnt; idx++) {
1691 zbr = &znode->zbranch[idx];
1692 err = leaf_cb(c, zbr, priv);
1693 if (err) {
1694 ubifs_err("leaf checking function "
1695 "returned error %d, for leaf "
1696 "at LEB %d:%d",
1697 err, zbr->lnum, zbr->offs);
1698 goto out_dump;
1699 }
1700 }
1701 }
1702
1703 if (!znode->parent)
1704 break;
1705
1706 idx = znode->iip + 1;
1707 znode = znode->parent;
1708 if (idx < znode->child_cnt) {
1709 /* Switch to the next index in the parent */
1710 zbr = &znode->zbranch[idx];
1711 child = zbr->znode;
1712 if (!child) {
1713 child = ubifs_load_znode(c, zbr, znode, idx);
1714 if (IS_ERR(child)) {
1715 err = PTR_ERR(child);
1716 goto out_unlock;
1717 }
1718 zbr->znode = child;
1719 }
1720 znode = child;
1721 } else
1722 /*
1723 * This is the last child, switch to the parent and
1724 * continue.
1725 */
1726 continue;
1727
1728 /* Go to the lowest leftmost znode in the new sub-tree */
1729 while (znode->level > 0) {
1730 zbr = &znode->zbranch[0];
1731 child = zbr->znode;
1732 if (!child) {
1733 child = ubifs_load_znode(c, zbr, znode, 0);
1734 if (IS_ERR(child)) {
1735 err = PTR_ERR(child);
1736 goto out_unlock;
1737 }
1738 zbr->znode = child;
1739 }
1740 znode = child;
1741 }
1742 }
1743
1744 mutex_unlock(&c->tnc_mutex);
1745 return 0;
1746
1747out_dump:
1748 if (znode->parent)
1749 zbr = &znode->parent->zbranch[znode->iip];
1750 else
1751 zbr = &c->zroot;
1752 ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03001753 ubifs_dump_znode(c, znode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001754out_unlock:
1755 mutex_unlock(&c->tnc_mutex);
1756 return err;
1757}
1758
1759/**
1760 * add_size - add znode size to partially calculated index size.
1761 * @c: UBIFS file-system description object
1762 * @znode: znode to add size for
1763 * @priv: partially calculated index size
1764 *
1765 * This is a helper function for 'dbg_check_idx_size()' which is called for
1766 * every indexing node and adds its size to the 'long long' variable pointed to
1767 * by @priv.
1768 */
1769static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1770{
1771 long long *idx_size = priv;
1772 int add;
1773
1774 add = ubifs_idx_node_sz(c, znode->child_cnt);
1775 add = ALIGN(add, 8);
1776 *idx_size += add;
1777 return 0;
1778}
1779
1780/**
1781 * dbg_check_idx_size - check index size.
1782 * @c: UBIFS file-system description object
1783 * @idx_size: size to check
1784 *
1785 * This function walks the UBIFS index, calculates its size and checks that the
1786 * size is equivalent to @idx_size. Returns zero in case of success and a
1787 * negative error code in case of failure.
1788 */
1789int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
1790{
1791 int err;
1792 long long calc = 0;
1793
Artem Bityutskiy8d7819b2011-06-03 08:53:35 +03001794 if (!dbg_is_chk_index(c))
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001795 return 0;
1796
1797 err = dbg_walk_index(c, NULL, add_size, &calc);
1798 if (err) {
1799 ubifs_err("error %d while walking the index", err);
1800 return err;
1801 }
1802
1803 if (calc != idx_size) {
1804 ubifs_err("index size check failed: calculated size is %lld, "
1805 "should be %lld", calc, idx_size);
1806 dump_stack();
1807 return -EINVAL;
1808 }
1809
1810 return 0;
1811}
1812
1813/**
1814 * struct fsck_inode - information about an inode used when checking the file-system.
1815 * @rb: link in the RB-tree of inodes
1816 * @inum: inode number
1817 * @mode: inode type, permissions, etc
1818 * @nlink: inode link count
1819 * @xattr_cnt: count of extended attributes
1820 * @references: how many directory/xattr entries refer this inode (calculated
1821 * while walking the index)
1822 * @calc_cnt: for directory inode count of child directories
1823 * @size: inode size (read from on-flash inode)
1824 * @xattr_sz: summary size of all extended attributes (read from on-flash
1825 * inode)
1826 * @calc_sz: for directories calculated directory size
1827 * @calc_xcnt: count of extended attributes
1828 * @calc_xsz: calculated summary size of all extended attributes
1829 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1830 * inode (read from on-flash inode)
1831 * @calc_xnms: calculated sum of lengths of all extended attribute names
1832 */
1833struct fsck_inode {
1834 struct rb_node rb;
1835 ino_t inum;
1836 umode_t mode;
1837 unsigned int nlink;
1838 unsigned int xattr_cnt;
1839 int references;
1840 int calc_cnt;
1841 long long size;
1842 unsigned int xattr_sz;
1843 long long calc_sz;
1844 long long calc_xcnt;
1845 long long calc_xsz;
1846 unsigned int xattr_nms;
1847 long long calc_xnms;
1848};
1849
1850/**
1851 * struct fsck_data - private FS checking information.
1852 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1853 */
1854struct fsck_data {
1855 struct rb_root inodes;
1856};
1857
1858/**
1859 * add_inode - add inode information to RB-tree of inodes.
1860 * @c: UBIFS file-system description object
1861 * @fsckd: FS checking information
1862 * @ino: raw UBIFS inode to add
1863 *
1864 * This is a helper function for 'check_leaf()' which adds information about
1865 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1866 * case of success and a negative error code in case of failure.
1867 */
1868static struct fsck_inode *add_inode(struct ubifs_info *c,
1869 struct fsck_data *fsckd,
1870 struct ubifs_ino_node *ino)
1871{
1872 struct rb_node **p, *parent = NULL;
1873 struct fsck_inode *fscki;
1874 ino_t inum = key_inum_flash(c, &ino->key);
Artem Bityutskiy45cd5cd2011-05-02 22:34:39 +03001875 struct inode *inode;
1876 struct ubifs_inode *ui;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001877
1878 p = &fsckd->inodes.rb_node;
1879 while (*p) {
1880 parent = *p;
1881 fscki = rb_entry(parent, struct fsck_inode, rb);
1882 if (inum < fscki->inum)
1883 p = &(*p)->rb_left;
1884 else if (inum > fscki->inum)
1885 p = &(*p)->rb_right;
1886 else
1887 return fscki;
1888 }
1889
1890 if (inum > c->highest_inum) {
1891 ubifs_err("too high inode number, max. is %lu",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001892 (unsigned long)c->highest_inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001893 return ERR_PTR(-EINVAL);
1894 }
1895
1896 fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
1897 if (!fscki)
1898 return ERR_PTR(-ENOMEM);
1899
Artem Bityutskiy45cd5cd2011-05-02 22:34:39 +03001900 inode = ilookup(c->vfs_sb, inum);
1901
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001902 fscki->inum = inum;
Artem Bityutskiy45cd5cd2011-05-02 22:34:39 +03001903 /*
1904 * If the inode is present in the VFS inode cache, use it instead of
1905 * the on-flash inode which might be out-of-date. E.g., the size might
1906 * be out-of-date. If we do not do this, the following may happen, for
1907 * example:
1908 * 1. A power cut happens
1909 * 2. We mount the file-system R/O, the replay process fixes up the
1910 * inode size in the VFS cache, but on on-flash.
1911 * 3. 'check_leaf()' fails because it hits a data node beyond inode
1912 * size.
1913 */
1914 if (!inode) {
1915 fscki->nlink = le32_to_cpu(ino->nlink);
1916 fscki->size = le64_to_cpu(ino->size);
1917 fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
1918 fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
1919 fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
1920 fscki->mode = le32_to_cpu(ino->mode);
1921 } else {
1922 ui = ubifs_inode(inode);
1923 fscki->nlink = inode->i_nlink;
1924 fscki->size = inode->i_size;
1925 fscki->xattr_cnt = ui->xattr_cnt;
1926 fscki->xattr_sz = ui->xattr_size;
1927 fscki->xattr_nms = ui->xattr_names;
1928 fscki->mode = inode->i_mode;
1929 iput(inode);
1930 }
1931
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001932 if (S_ISDIR(fscki->mode)) {
1933 fscki->calc_sz = UBIFS_INO_NODE_SZ;
1934 fscki->calc_cnt = 2;
1935 }
Artem Bityutskiy45cd5cd2011-05-02 22:34:39 +03001936
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001937 rb_link_node(&fscki->rb, parent, p);
1938 rb_insert_color(&fscki->rb, &fsckd->inodes);
Artem Bityutskiy45cd5cd2011-05-02 22:34:39 +03001939
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001940 return fscki;
1941}
1942
1943/**
1944 * search_inode - search inode in the RB-tree of inodes.
1945 * @fsckd: FS checking information
1946 * @inum: inode number to search
1947 *
1948 * This is a helper function for 'check_leaf()' which searches inode @inum in
1949 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1950 * the inode was not found.
1951 */
1952static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
1953{
1954 struct rb_node *p;
1955 struct fsck_inode *fscki;
1956
1957 p = fsckd->inodes.rb_node;
1958 while (p) {
1959 fscki = rb_entry(p, struct fsck_inode, rb);
1960 if (inum < fscki->inum)
1961 p = p->rb_left;
1962 else if (inum > fscki->inum)
1963 p = p->rb_right;
1964 else
1965 return fscki;
1966 }
1967 return NULL;
1968}
1969
1970/**
1971 * read_add_inode - read inode node and add it to RB-tree of inodes.
1972 * @c: UBIFS file-system description object
1973 * @fsckd: FS checking information
1974 * @inum: inode number to read
1975 *
1976 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1977 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1978 * information pointer in case of success and a negative error code in case of
1979 * failure.
1980 */
1981static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1982 struct fsck_data *fsckd, ino_t inum)
1983{
1984 int n, err;
1985 union ubifs_key key;
1986 struct ubifs_znode *znode;
1987 struct ubifs_zbranch *zbr;
1988 struct ubifs_ino_node *ino;
1989 struct fsck_inode *fscki;
1990
1991 fscki = search_inode(fsckd, inum);
1992 if (fscki)
1993 return fscki;
1994
1995 ino_key_init(c, &key, inum);
1996 err = ubifs_lookup_level0(c, &key, &znode, &n);
1997 if (!err) {
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001998 ubifs_err("inode %lu not found in index", (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001999 return ERR_PTR(-ENOENT);
2000 } else if (err < 0) {
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002001 ubifs_err("error %d while looking up inode %lu",
2002 err, (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002003 return ERR_PTR(err);
2004 }
2005
2006 zbr = &znode->zbranch[n];
2007 if (zbr->len < UBIFS_INO_NODE_SZ) {
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002008 ubifs_err("bad node %lu node length %d",
2009 (unsigned long)inum, zbr->len);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002010 return ERR_PTR(-EINVAL);
2011 }
2012
2013 ino = kmalloc(zbr->len, GFP_NOFS);
2014 if (!ino)
2015 return ERR_PTR(-ENOMEM);
2016
2017 err = ubifs_tnc_read_node(c, zbr, ino);
2018 if (err) {
2019 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2020 zbr->lnum, zbr->offs, err);
2021 kfree(ino);
2022 return ERR_PTR(err);
2023 }
2024
2025 fscki = add_inode(c, fsckd, ino);
2026 kfree(ino);
2027 if (IS_ERR(fscki)) {
2028 ubifs_err("error %ld while adding inode %lu node",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002029 PTR_ERR(fscki), (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002030 return fscki;
2031 }
2032
2033 return fscki;
2034}
2035
2036/**
2037 * check_leaf - check leaf node.
2038 * @c: UBIFS file-system description object
2039 * @zbr: zbranch of the leaf node to check
2040 * @priv: FS checking information
2041 *
2042 * This is a helper function for 'dbg_check_filesystem()' which is called for
2043 * every single leaf node while walking the indexing tree. It checks that the
2044 * leaf node referred from the indexing tree exists, has correct CRC, and does
2045 * some other basic validation. This function is also responsible for building
2046 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
2047 * calculates reference count, size, etc for each inode in order to later
2048 * compare them to the information stored inside the inodes and detect possible
2049 * inconsistencies. Returns zero in case of success and a negative error code
2050 * in case of failure.
2051 */
2052static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
2053 void *priv)
2054{
2055 ino_t inum;
2056 void *node;
2057 struct ubifs_ch *ch;
2058 int err, type = key_type(c, &zbr->key);
2059 struct fsck_inode *fscki;
2060
2061 if (zbr->len < UBIFS_CH_SZ) {
2062 ubifs_err("bad leaf length %d (LEB %d:%d)",
2063 zbr->len, zbr->lnum, zbr->offs);
2064 return -EINVAL;
2065 }
2066
2067 node = kmalloc(zbr->len, GFP_NOFS);
2068 if (!node)
2069 return -ENOMEM;
2070
2071 err = ubifs_tnc_read_node(c, zbr, node);
2072 if (err) {
2073 ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
2074 zbr->lnum, zbr->offs, err);
2075 goto out_free;
2076 }
2077
2078 /* If this is an inode node, add it to RB-tree of inodes */
2079 if (type == UBIFS_INO_KEY) {
2080 fscki = add_inode(c, priv, node);
2081 if (IS_ERR(fscki)) {
2082 err = PTR_ERR(fscki);
2083 ubifs_err("error %d while adding inode node", err);
2084 goto out_dump;
2085 }
2086 goto out;
2087 }
2088
2089 if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
2090 type != UBIFS_DATA_KEY) {
2091 ubifs_err("unexpected node type %d at LEB %d:%d",
2092 type, zbr->lnum, zbr->offs);
2093 err = -EINVAL;
2094 goto out_free;
2095 }
2096
2097 ch = node;
2098 if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
2099 ubifs_err("too high sequence number, max. is %llu",
2100 c->max_sqnum);
2101 err = -EINVAL;
2102 goto out_dump;
2103 }
2104
2105 if (type == UBIFS_DATA_KEY) {
2106 long long blk_offs;
2107 struct ubifs_data_node *dn = node;
2108
2109 /*
2110 * Search the inode node this data node belongs to and insert
2111 * it to the RB-tree of inodes.
2112 */
2113 inum = key_inum_flash(c, &dn->key);
2114 fscki = read_add_inode(c, priv, inum);
2115 if (IS_ERR(fscki)) {
2116 err = PTR_ERR(fscki);
2117 ubifs_err("error %d while processing data node and "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002118 "trying to find inode node %lu",
2119 err, (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002120 goto out_dump;
2121 }
2122
2123 /* Make sure the data node is within inode size */
2124 blk_offs = key_block_flash(c, &dn->key);
2125 blk_offs <<= UBIFS_BLOCK_SHIFT;
2126 blk_offs += le32_to_cpu(dn->size);
2127 if (blk_offs > fscki->size) {
2128 ubifs_err("data node at LEB %d:%d is not within inode "
2129 "size %lld", zbr->lnum, zbr->offs,
2130 fscki->size);
2131 err = -EINVAL;
2132 goto out_dump;
2133 }
2134 } else {
2135 int nlen;
2136 struct ubifs_dent_node *dent = node;
2137 struct fsck_inode *fscki1;
2138
2139 err = ubifs_validate_entry(c, dent);
2140 if (err)
2141 goto out_dump;
2142
2143 /*
2144 * Search the inode node this entry refers to and the parent
2145 * inode node and insert them to the RB-tree of inodes.
2146 */
2147 inum = le64_to_cpu(dent->inum);
2148 fscki = read_add_inode(c, priv, inum);
2149 if (IS_ERR(fscki)) {
2150 err = PTR_ERR(fscki);
2151 ubifs_err("error %d while processing entry node and "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002152 "trying to find inode node %lu",
2153 err, (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002154 goto out_dump;
2155 }
2156
2157 /* Count how many direntries or xentries refers this inode */
2158 fscki->references += 1;
2159
2160 inum = key_inum_flash(c, &dent->key);
2161 fscki1 = read_add_inode(c, priv, inum);
2162 if (IS_ERR(fscki1)) {
Roel Kluinb38882f2009-12-07 14:21:45 +01002163 err = PTR_ERR(fscki1);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002164 ubifs_err("error %d while processing entry node and "
2165 "trying to find parent inode node %lu",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002166 err, (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002167 goto out_dump;
2168 }
2169
2170 nlen = le16_to_cpu(dent->nlen);
2171 if (type == UBIFS_XENT_KEY) {
2172 fscki1->calc_xcnt += 1;
2173 fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
2174 fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
2175 fscki1->calc_xnms += nlen;
2176 } else {
2177 fscki1->calc_sz += CALC_DENT_SIZE(nlen);
2178 if (dent->type == UBIFS_ITYPE_DIR)
2179 fscki1->calc_cnt += 1;
2180 }
2181 }
2182
2183out:
2184 kfree(node);
2185 return 0;
2186
2187out_dump:
2188 ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03002189 ubifs_dump_node(c, node);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002190out_free:
2191 kfree(node);
2192 return err;
2193}
2194
2195/**
2196 * free_inodes - free RB-tree of inodes.
2197 * @fsckd: FS checking information
2198 */
2199static void free_inodes(struct fsck_data *fsckd)
2200{
2201 struct rb_node *this = fsckd->inodes.rb_node;
2202 struct fsck_inode *fscki;
2203
2204 while (this) {
2205 if (this->rb_left)
2206 this = this->rb_left;
2207 else if (this->rb_right)
2208 this = this->rb_right;
2209 else {
2210 fscki = rb_entry(this, struct fsck_inode, rb);
2211 this = rb_parent(this);
2212 if (this) {
2213 if (this->rb_left == &fscki->rb)
2214 this->rb_left = NULL;
2215 else
2216 this->rb_right = NULL;
2217 }
2218 kfree(fscki);
2219 }
2220 }
2221}
2222
2223/**
2224 * check_inodes - checks all inodes.
2225 * @c: UBIFS file-system description object
2226 * @fsckd: FS checking information
2227 *
2228 * This is a helper function for 'dbg_check_filesystem()' which walks the
2229 * RB-tree of inodes after the index scan has been finished, and checks that
2230 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
2231 * %-EINVAL if not, and a negative error code in case of failure.
2232 */
2233static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
2234{
2235 int n, err;
2236 union ubifs_key key;
2237 struct ubifs_znode *znode;
2238 struct ubifs_zbranch *zbr;
2239 struct ubifs_ino_node *ino;
2240 struct fsck_inode *fscki;
2241 struct rb_node *this = rb_first(&fsckd->inodes);
2242
2243 while (this) {
2244 fscki = rb_entry(this, struct fsck_inode, rb);
2245 this = rb_next(this);
2246
2247 if (S_ISDIR(fscki->mode)) {
2248 /*
2249 * Directories have to have exactly one reference (they
2250 * cannot have hardlinks), although root inode is an
2251 * exception.
2252 */
2253 if (fscki->inum != UBIFS_ROOT_INO &&
2254 fscki->references != 1) {
2255 ubifs_err("directory inode %lu has %d "
2256 "direntries which refer it, but "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002257 "should be 1",
2258 (unsigned long)fscki->inum,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002259 fscki->references);
2260 goto out_dump;
2261 }
2262 if (fscki->inum == UBIFS_ROOT_INO &&
2263 fscki->references != 0) {
2264 ubifs_err("root inode %lu has non-zero (%d) "
2265 "direntries which refer it",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002266 (unsigned long)fscki->inum,
2267 fscki->references);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002268 goto out_dump;
2269 }
2270 if (fscki->calc_sz != fscki->size) {
2271 ubifs_err("directory inode %lu size is %lld, "
2272 "but calculated size is %lld",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002273 (unsigned long)fscki->inum,
2274 fscki->size, fscki->calc_sz);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002275 goto out_dump;
2276 }
2277 if (fscki->calc_cnt != fscki->nlink) {
2278 ubifs_err("directory inode %lu nlink is %d, "
2279 "but calculated nlink is %d",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002280 (unsigned long)fscki->inum,
2281 fscki->nlink, fscki->calc_cnt);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002282 goto out_dump;
2283 }
2284 } else {
2285 if (fscki->references != fscki->nlink) {
2286 ubifs_err("inode %lu nlink is %d, but "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002287 "calculated nlink is %d",
2288 (unsigned long)fscki->inum,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002289 fscki->nlink, fscki->references);
2290 goto out_dump;
2291 }
2292 }
2293 if (fscki->xattr_sz != fscki->calc_xsz) {
2294 ubifs_err("inode %lu has xattr size %u, but "
2295 "calculated size is %lld",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002296 (unsigned long)fscki->inum, fscki->xattr_sz,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002297 fscki->calc_xsz);
2298 goto out_dump;
2299 }
2300 if (fscki->xattr_cnt != fscki->calc_xcnt) {
2301 ubifs_err("inode %lu has %u xattrs, but "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002302 "calculated count is %lld",
2303 (unsigned long)fscki->inum,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002304 fscki->xattr_cnt, fscki->calc_xcnt);
2305 goto out_dump;
2306 }
2307 if (fscki->xattr_nms != fscki->calc_xnms) {
2308 ubifs_err("inode %lu has xattr names' size %u, but "
2309 "calculated names' size is %lld",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002310 (unsigned long)fscki->inum, fscki->xattr_nms,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002311 fscki->calc_xnms);
2312 goto out_dump;
2313 }
2314 }
2315
2316 return 0;
2317
2318out_dump:
2319 /* Read the bad inode and dump it */
2320 ino_key_init(c, &key, fscki->inum);
2321 err = ubifs_lookup_level0(c, &key, &znode, &n);
2322 if (!err) {
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002323 ubifs_err("inode %lu not found in index",
2324 (unsigned long)fscki->inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002325 return -ENOENT;
2326 } else if (err < 0) {
2327 ubifs_err("error %d while looking up inode %lu",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002328 err, (unsigned long)fscki->inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002329 return err;
2330 }
2331
2332 zbr = &znode->zbranch[n];
2333 ino = kmalloc(zbr->len, GFP_NOFS);
2334 if (!ino)
2335 return -ENOMEM;
2336
2337 err = ubifs_tnc_read_node(c, zbr, ino);
2338 if (err) {
2339 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2340 zbr->lnum, zbr->offs, err);
2341 kfree(ino);
2342 return err;
2343 }
2344
2345 ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002346 (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03002347 ubifs_dump_node(c, ino);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002348 kfree(ino);
2349 return -EINVAL;
2350}
2351
2352/**
2353 * dbg_check_filesystem - check the file-system.
2354 * @c: UBIFS file-system description object
2355 *
2356 * This function checks the file system, namely:
2357 * o makes sure that all leaf nodes exist and their CRCs are correct;
2358 * o makes sure inode nlink, size, xattr size/count are correct (for all
2359 * inodes).
2360 *
2361 * The function reads whole indexing tree and all nodes, so it is pretty
2362 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2363 * not, and a negative error code in case of failure.
2364 */
2365int dbg_check_filesystem(struct ubifs_info *c)
2366{
2367 int err;
2368 struct fsck_data fsckd;
2369
Artem Bityutskiy2b1844a2011-06-03 08:31:29 +03002370 if (!dbg_is_chk_fs(c))
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002371 return 0;
2372
2373 fsckd.inodes = RB_ROOT;
2374 err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
2375 if (err)
2376 goto out_free;
2377
2378 err = check_inodes(c, &fsckd);
2379 if (err)
2380 goto out_free;
2381
2382 free_inodes(&fsckd);
2383 return 0;
2384
2385out_free:
2386 ubifs_err("file-system check failed with error %d", err);
2387 dump_stack();
2388 free_inodes(&fsckd);
2389 return err;
2390}
2391
Artem Bityutskiy3bb66b42010-08-07 10:06:11 +03002392/**
2393 * dbg_check_data_nodes_order - check that list of data nodes is sorted.
2394 * @c: UBIFS file-system description object
2395 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2396 *
2397 * This function returns zero if the list of data nodes is sorted correctly,
2398 * and %-EINVAL if not.
2399 */
2400int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head)
2401{
2402 struct list_head *cur;
2403 struct ubifs_scan_node *sa, *sb;
2404
Artem Bityutskiy2b1844a2011-06-03 08:31:29 +03002405 if (!dbg_is_chk_gen(c))
Artem Bityutskiy3bb66b42010-08-07 10:06:11 +03002406 return 0;
2407
2408 for (cur = head->next; cur->next != head; cur = cur->next) {
2409 ino_t inuma, inumb;
2410 uint32_t blka, blkb;
2411
2412 cond_resched();
2413 sa = container_of(cur, struct ubifs_scan_node, list);
2414 sb = container_of(cur->next, struct ubifs_scan_node, list);
2415
2416 if (sa->type != UBIFS_DATA_NODE) {
2417 ubifs_err("bad node type %d", sa->type);
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03002418 ubifs_dump_node(c, sa->node);
Artem Bityutskiy3bb66b42010-08-07 10:06:11 +03002419 return -EINVAL;
2420 }
2421 if (sb->type != UBIFS_DATA_NODE) {
2422 ubifs_err("bad node type %d", sb->type);
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03002423 ubifs_dump_node(c, sb->node);
Artem Bityutskiy3bb66b42010-08-07 10:06:11 +03002424 return -EINVAL;
2425 }
2426
2427 inuma = key_inum(c, &sa->key);
2428 inumb = key_inum(c, &sb->key);
2429
2430 if (inuma < inumb)
2431 continue;
2432 if (inuma > inumb) {
2433 ubifs_err("larger inum %lu goes before inum %lu",
2434 (unsigned long)inuma, (unsigned long)inumb);
2435 goto error_dump;
2436 }
2437
2438 blka = key_block(c, &sa->key);
2439 blkb = key_block(c, &sb->key);
2440
2441 if (blka > blkb) {
2442 ubifs_err("larger block %u goes before %u", blka, blkb);
2443 goto error_dump;
2444 }
2445 if (blka == blkb) {
2446 ubifs_err("two data nodes for the same block");
2447 goto error_dump;
2448 }
2449 }
2450
2451 return 0;
2452
2453error_dump:
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03002454 ubifs_dump_node(c, sa->node);
2455 ubifs_dump_node(c, sb->node);
Artem Bityutskiy3bb66b42010-08-07 10:06:11 +03002456 return -EINVAL;
2457}
2458
2459/**
2460 * dbg_check_nondata_nodes_order - check that list of data nodes is sorted.
2461 * @c: UBIFS file-system description object
2462 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2463 *
2464 * This function returns zero if the list of non-data nodes is sorted correctly,
2465 * and %-EINVAL if not.
2466 */
2467int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head)
2468{
2469 struct list_head *cur;
2470 struct ubifs_scan_node *sa, *sb;
2471
Artem Bityutskiy2b1844a2011-06-03 08:31:29 +03002472 if (!dbg_is_chk_gen(c))
Artem Bityutskiy3bb66b42010-08-07 10:06:11 +03002473 return 0;
2474
2475 for (cur = head->next; cur->next != head; cur = cur->next) {
2476 ino_t inuma, inumb;
2477 uint32_t hasha, hashb;
2478
2479 cond_resched();
2480 sa = container_of(cur, struct ubifs_scan_node, list);
2481 sb = container_of(cur->next, struct ubifs_scan_node, list);
2482
2483 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2484 sa->type != UBIFS_XENT_NODE) {
2485 ubifs_err("bad node type %d", sa->type);
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03002486 ubifs_dump_node(c, sa->node);
Artem Bityutskiy3bb66b42010-08-07 10:06:11 +03002487 return -EINVAL;
2488 }
2489 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2490 sa->type != UBIFS_XENT_NODE) {
2491 ubifs_err("bad node type %d", sb->type);
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03002492 ubifs_dump_node(c, sb->node);
Artem Bityutskiy3bb66b42010-08-07 10:06:11 +03002493 return -EINVAL;
2494 }
2495
2496 if (sa->type != UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2497 ubifs_err("non-inode node goes before inode node");
2498 goto error_dump;
2499 }
2500
2501 if (sa->type == UBIFS_INO_NODE && sb->type != UBIFS_INO_NODE)
2502 continue;
2503
2504 if (sa->type == UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2505 /* Inode nodes are sorted in descending size order */
2506 if (sa->len < sb->len) {
2507 ubifs_err("smaller inode node goes first");
2508 goto error_dump;
2509 }
2510 continue;
2511 }
2512
2513 /*
2514 * This is either a dentry or xentry, which should be sorted in
2515 * ascending (parent ino, hash) order.
2516 */
2517 inuma = key_inum(c, &sa->key);
2518 inumb = key_inum(c, &sb->key);
2519
2520 if (inuma < inumb)
2521 continue;
2522 if (inuma > inumb) {
2523 ubifs_err("larger inum %lu goes before inum %lu",
2524 (unsigned long)inuma, (unsigned long)inumb);
2525 goto error_dump;
2526 }
2527
2528 hasha = key_block(c, &sa->key);
2529 hashb = key_block(c, &sb->key);
2530
2531 if (hasha > hashb) {
Artem Bityutskiyc4361572011-03-25 15:27:40 +02002532 ubifs_err("larger hash %u goes before %u",
2533 hasha, hashb);
Artem Bityutskiy3bb66b42010-08-07 10:06:11 +03002534 goto error_dump;
2535 }
2536 }
2537
2538 return 0;
2539
2540error_dump:
2541 ubifs_msg("dumping first node");
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03002542 ubifs_dump_node(c, sa->node);
Artem Bityutskiy3bb66b42010-08-07 10:06:11 +03002543 ubifs_msg("dumping second node");
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03002544 ubifs_dump_node(c, sb->node);
Artem Bityutskiy3bb66b42010-08-07 10:06:11 +03002545 return -EINVAL;
2546 return 0;
2547}
2548
Artem Bityutskiya7fa94a2011-06-03 16:20:03 +03002549static inline int chance(unsigned int n, unsigned int out_of)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002550{
Artem Bityutskiya7fa94a2011-06-03 16:20:03 +03002551 return !!((random32() % out_of) + 1 <= n);
2552
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002553}
2554
Artem Bityutskiyd27462a2011-06-03 15:10:33 +03002555static int power_cut_emulated(struct ubifs_info *c, int lnum, int write)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002556{
Artem Bityutskiyf57cb182011-06-03 14:51:41 +03002557 struct ubifs_debug_info *d = c->dbg;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002558
Artem Bityutskiyf57cb182011-06-03 14:51:41 +03002559 ubifs_assert(dbg_is_tst_rcvry(c));
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002560
Artem Bityutskiyd27462a2011-06-03 15:10:33 +03002561 if (!d->pc_cnt) {
2562 /* First call - decide delay to the power cut */
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002563 if (chance(1, 2)) {
Artem Bityutskiya7fa94a2011-06-03 16:20:03 +03002564 unsigned long delay;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002565
2566 if (chance(1, 2)) {
Artem Bityutskiyd27462a2011-06-03 15:10:33 +03002567 d->pc_delay = 1;
Artem Bityutskiya7fa94a2011-06-03 16:20:03 +03002568 /* Fail withing 1 minute */
2569 delay = random32() % 60000;
2570 d->pc_timeout = jiffies;
2571 d->pc_timeout += msecs_to_jiffies(delay);
2572 ubifs_warn("failing after %lums", delay);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002573 } else {
Artem Bityutskiyd27462a2011-06-03 15:10:33 +03002574 d->pc_delay = 2;
Artem Bityutskiya7fa94a2011-06-03 16:20:03 +03002575 delay = random32() % 10000;
2576 /* Fail within 10000 operations */
Artem Bityutskiyd27462a2011-06-03 15:10:33 +03002577 d->pc_cnt_max = delay;
Artem Bityutskiya7fa94a2011-06-03 16:20:03 +03002578 ubifs_warn("failing after %lu calls", delay);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002579 }
2580 }
Artem Bityutskiya7fa94a2011-06-03 16:20:03 +03002581
Artem Bityutskiyd27462a2011-06-03 15:10:33 +03002582 d->pc_cnt += 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002583 }
Artem Bityutskiya7fa94a2011-06-03 16:20:03 +03002584
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002585 /* Determine if failure delay has expired */
Artem Bityutskiya7fa94a2011-06-03 16:20:03 +03002586 if (d->pc_delay == 1 && time_before(jiffies, d->pc_timeout))
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002587 return 0;
Artem Bityutskiya7fa94a2011-06-03 16:20:03 +03002588 if (d->pc_delay == 2 && d->pc_cnt++ < d->pc_cnt_max)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002589 return 0;
Artem Bityutskiya7fa94a2011-06-03 16:20:03 +03002590
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002591 if (lnum == UBIFS_SB_LNUM) {
Artem Bityutskiya7fa94a2011-06-03 16:20:03 +03002592 if (write && chance(1, 2))
2593 return 0;
2594 if (chance(19, 20))
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002595 return 0;
Artem Bityutskiy24a4f802011-06-01 15:23:25 +03002596 ubifs_warn("failing in super block LEB %d", lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002597 } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2598 if (chance(19, 20))
2599 return 0;
Artem Bityutskiy24a4f802011-06-01 15:23:25 +03002600 ubifs_warn("failing in master LEB %d", lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002601 } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
Artem Bityutskiya7fa94a2011-06-03 16:20:03 +03002602 if (write && chance(99, 100))
2603 return 0;
2604 if (chance(399, 400))
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002605 return 0;
Artem Bityutskiy24a4f802011-06-01 15:23:25 +03002606 ubifs_warn("failing in log LEB %d", lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002607 } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
Artem Bityutskiya7fa94a2011-06-03 16:20:03 +03002608 if (write && chance(7, 8))
2609 return 0;
2610 if (chance(19, 20))
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002611 return 0;
Artem Bityutskiy24a4f802011-06-01 15:23:25 +03002612 ubifs_warn("failing in LPT LEB %d", lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002613 } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
Artem Bityutskiya7fa94a2011-06-03 16:20:03 +03002614 if (write && chance(1, 2))
2615 return 0;
2616 if (chance(9, 10))
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002617 return 0;
Artem Bityutskiy24a4f802011-06-01 15:23:25 +03002618 ubifs_warn("failing in orphan LEB %d", lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002619 } else if (lnum == c->ihead_lnum) {
2620 if (chance(99, 100))
2621 return 0;
Artem Bityutskiy24a4f802011-06-01 15:23:25 +03002622 ubifs_warn("failing in index head LEB %d", lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002623 } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2624 if (chance(9, 10))
2625 return 0;
Artem Bityutskiy24a4f802011-06-01 15:23:25 +03002626 ubifs_warn("failing in GC head LEB %d", lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002627 } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
2628 !ubifs_search_bud(c, lnum)) {
2629 if (chance(19, 20))
2630 return 0;
Artem Bityutskiy24a4f802011-06-01 15:23:25 +03002631 ubifs_warn("failing in non-bud LEB %d", lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002632 } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
2633 c->cmt_state == COMMIT_RUNNING_REQUIRED) {
2634 if (chance(999, 1000))
2635 return 0;
Artem Bityutskiy24a4f802011-06-01 15:23:25 +03002636 ubifs_warn("failing in bud LEB %d commit running", lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002637 } else {
2638 if (chance(9999, 10000))
2639 return 0;
Artem Bityutskiy24a4f802011-06-01 15:23:25 +03002640 ubifs_warn("failing in bud LEB %d commit not running", lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002641 }
Artem Bityutskiy24a4f802011-06-01 15:23:25 +03002642
Artem Bityutskiyd27462a2011-06-03 15:10:33 +03002643 d->pc_happened = 1;
Artem Bityutskiya7fa94a2011-06-03 16:20:03 +03002644 ubifs_warn("========== Power cut emulated ==========");
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002645 dump_stack();
2646 return 1;
2647}
2648
Artem Bityutskiy8089ed72012-08-22 17:35:11 +03002649static int corrupt_data(const struct ubifs_info *c, const void *buf,
2650 unsigned int len)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002651{
Artem Bityutskiya7fa94a2011-06-03 16:20:03 +03002652 unsigned int from, to, i, ffs = chance(1, 2);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002653 unsigned char *p = (void *)buf;
2654
Artem Bityutskiya7fa94a2011-06-03 16:20:03 +03002655 from = random32() % (len + 1);
Artem Bityutskiy8089ed72012-08-22 17:35:11 +03002656 /* Corruption may only span one max. write unit */
2657 to = min(len, ALIGN(from, c->max_write_size));
Artem Bityutskiya7fa94a2011-06-03 16:20:03 +03002658
Artem Bityutskiy8089ed72012-08-22 17:35:11 +03002659 ubifs_warn("filled bytes %u-%u with %s", from, to - 1,
2660 ffs ? "0xFFs" : "random data");
Artem Bityutskiya7fa94a2011-06-03 16:20:03 +03002661
2662 if (ffs)
2663 for (i = from; i < to; i++)
2664 p[i] = 0xFF;
2665 else
2666 for (i = from; i < to; i++)
2667 p[i] = random32() % 0x100;
Artem Bityutskiy8089ed72012-08-22 17:35:11 +03002668
2669 return to;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002670}
2671
Artem Bityutskiyf57cb182011-06-03 14:51:41 +03002672int dbg_leb_write(struct ubifs_info *c, int lnum, const void *buf,
Richard Weinbergerb36a2612012-05-14 17:55:51 +02002673 int offs, int len)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002674{
Adrian Hunter16dfd802008-07-18 16:47:41 +03002675 int err, failing;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002676
Artem Bityutskiyd27462a2011-06-03 15:10:33 +03002677 if (c->dbg->pc_happened)
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002678 return -EROFS;
Artem Bityutskiyd27462a2011-06-03 15:10:33 +03002679
2680 failing = power_cut_emulated(c, lnum, 1);
Adrian Hunter16dfd802008-07-18 16:47:41 +03002681 if (failing)
Artem Bityutskiy8089ed72012-08-22 17:35:11 +03002682 len = corrupt_data(c, buf, len);
2683 ubifs_warn("actually write %d bytes to LEB %d:%d (the buffer was corrupted)",
2684 len, lnum, offs);
Richard Weinbergerb36a2612012-05-14 17:55:51 +02002685 err = ubi_leb_write(c->ubi, lnum, buf, offs, len);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002686 if (err)
2687 return err;
Adrian Hunter16dfd802008-07-18 16:47:41 +03002688 if (failing)
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002689 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002690 return 0;
2691}
2692
Artem Bityutskiyf57cb182011-06-03 14:51:41 +03002693int dbg_leb_change(struct ubifs_info *c, int lnum, const void *buf,
Richard Weinbergerb36a2612012-05-14 17:55:51 +02002694 int len)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002695{
2696 int err;
2697
Artem Bityutskiyd27462a2011-06-03 15:10:33 +03002698 if (c->dbg->pc_happened)
2699 return -EROFS;
2700 if (power_cut_emulated(c, lnum, 1))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002701 return -EROFS;
Richard Weinbergerb36a2612012-05-14 17:55:51 +02002702 err = ubi_leb_change(c->ubi, lnum, buf, len);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002703 if (err)
2704 return err;
Artem Bityutskiyd27462a2011-06-03 15:10:33 +03002705 if (power_cut_emulated(c, lnum, 1))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002706 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002707 return 0;
2708}
2709
Artem Bityutskiyf57cb182011-06-03 14:51:41 +03002710int dbg_leb_unmap(struct ubifs_info *c, int lnum)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002711{
2712 int err;
2713
Artem Bityutskiyd27462a2011-06-03 15:10:33 +03002714 if (c->dbg->pc_happened)
2715 return -EROFS;
2716 if (power_cut_emulated(c, lnum, 0))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002717 return -EROFS;
Artem Bityutskiyf57cb182011-06-03 14:51:41 +03002718 err = ubi_leb_unmap(c->ubi, lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002719 if (err)
2720 return err;
Artem Bityutskiyd27462a2011-06-03 15:10:33 +03002721 if (power_cut_emulated(c, lnum, 0))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002722 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002723 return 0;
2724}
2725
Richard Weinbergerb36a2612012-05-14 17:55:51 +02002726int dbg_leb_map(struct ubifs_info *c, int lnum)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002727{
2728 int err;
2729
Artem Bityutskiyd27462a2011-06-03 15:10:33 +03002730 if (c->dbg->pc_happened)
2731 return -EROFS;
2732 if (power_cut_emulated(c, lnum, 0))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002733 return -EROFS;
Richard Weinbergerb36a2612012-05-14 17:55:51 +02002734 err = ubi_leb_map(c->ubi, lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002735 if (err)
2736 return err;
Artem Bityutskiyd27462a2011-06-03 15:10:33 +03002737 if (power_cut_emulated(c, lnum, 0))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002738 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002739 return 0;
2740}
2741
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002742/*
2743 * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
2744 * contain the stuff specific to particular file-system mounts.
2745 */
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002746static struct dentry *dfs_rootdir;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002747
Artem Bityutskiy7dae9972011-06-01 15:44:14 +03002748static int dfs_file_open(struct inode *inode, struct file *file)
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002749{
2750 file->private_data = inode->i_private;
Artem Bityutskiy1bbfc842011-03-21 16:26:42 +02002751 return nonseekable_open(inode, file);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002752}
2753
Artem Bityutskiy28488fc2011-06-03 09:58:23 +03002754/**
2755 * provide_user_output - provide output to the user reading a debugfs file.
2756 * @val: boolean value for the answer
2757 * @u: the buffer to store the answer at
2758 * @count: size of the buffer
2759 * @ppos: position in the @u output buffer
2760 *
2761 * This is a simple helper function which stores @val boolean value in the user
2762 * buffer when the user reads one of UBIFS debugfs files. Returns amount of
2763 * bytes written to @u in case of success and a negative error code in case of
2764 * failure.
2765 */
2766static int provide_user_output(int val, char __user *u, size_t count,
2767 loff_t *ppos)
2768{
2769 char buf[3];
2770
2771 if (val)
2772 buf[0] = '1';
2773 else
2774 buf[0] = '0';
2775 buf[1] = '\n';
2776 buf[2] = 0x00;
2777
2778 return simple_read_from_buffer(u, count, ppos, buf, 2);
2779}
2780
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002781static ssize_t dfs_file_read(struct file *file, char __user *u, size_t count,
2782 loff_t *ppos)
2783{
2784 struct dentry *dent = file->f_path.dentry;
2785 struct ubifs_info *c = file->private_data;
2786 struct ubifs_debug_info *d = c->dbg;
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002787 int val;
2788
2789 if (dent == d->dfs_chk_gen)
2790 val = d->chk_gen;
2791 else if (dent == d->dfs_chk_index)
2792 val = d->chk_index;
2793 else if (dent == d->dfs_chk_orph)
2794 val = d->chk_orph;
2795 else if (dent == d->dfs_chk_lprops)
2796 val = d->chk_lprops;
2797 else if (dent == d->dfs_chk_fs)
2798 val = d->chk_fs;
2799 else if (dent == d->dfs_tst_rcvry)
2800 val = d->tst_rcvry;
Artem Bityutskiy06bef942012-07-14 14:19:46 +03002801 else if (dent == d->dfs_ro_error)
2802 val = c->ro_error;
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002803 else
2804 return -EINVAL;
2805
Artem Bityutskiy28488fc2011-06-03 09:58:23 +03002806 return provide_user_output(val, u, count, ppos);
2807}
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002808
Artem Bityutskiy28488fc2011-06-03 09:58:23 +03002809/**
2810 * interpret_user_input - interpret user debugfs file input.
2811 * @u: user-provided buffer with the input
2812 * @count: buffer size
2813 *
2814 * This is a helper function which interpret user input to a boolean UBIFS
2815 * debugfs file. Returns %0 or %1 in case of success and a negative error code
2816 * in case of failure.
2817 */
2818static int interpret_user_input(const char __user *u, size_t count)
2819{
2820 size_t buf_size;
2821 char buf[8];
2822
2823 buf_size = min_t(size_t, count, (sizeof(buf) - 1));
2824 if (copy_from_user(buf, u, buf_size))
2825 return -EFAULT;
2826
2827 if (buf[0] == '1')
2828 return 1;
2829 else if (buf[0] == '0')
2830 return 0;
2831
2832 return -EINVAL;
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002833}
2834
2835static ssize_t dfs_file_write(struct file *file, const char __user *u,
2836 size_t count, loff_t *ppos)
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002837{
2838 struct ubifs_info *c = file->private_data;
2839 struct ubifs_debug_info *d = c->dbg;
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002840 struct dentry *dent = file->f_path.dentry;
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002841 int val;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002842
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002843 /*
Artem Bityutskiy24a4f802011-06-01 15:23:25 +03002844 * TODO: this is racy - the file-system might have already been
2845 * unmounted and we'd oops in this case. The plan is to fix it with
2846 * help of 'iterate_supers_type()' which we should have in v3.0: when
2847 * a debugfs opened, we rember FS's UUID in file->private_data. Then
2848 * whenever we access the FS via a debugfs file, we iterate all UBIFS
2849 * superblocks and fine the one with the same UUID, and take the
2850 * locking right.
2851 *
2852 * The other way to go suggested by Al Viro is to create a separate
2853 * 'ubifs-debug' file-system instead.
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002854 */
2855 if (file->f_path.dentry == d->dfs_dump_lprops) {
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03002856 ubifs_dump_lprops(c);
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002857 return count;
2858 }
2859 if (file->f_path.dentry == d->dfs_dump_budg) {
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03002860 ubifs_dump_budg(c, &c->bi);
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002861 return count;
2862 }
2863 if (file->f_path.dentry == d->dfs_dump_tnc) {
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002864 mutex_lock(&c->tnc_mutex);
Artem Bityutskiyedf6be22012-05-16 19:15:56 +03002865 ubifs_dump_tnc(c);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002866 mutex_unlock(&c->tnc_mutex);
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002867 return count;
2868 }
2869
Artem Bityutskiy28488fc2011-06-03 09:58:23 +03002870 val = interpret_user_input(u, count);
2871 if (val < 0)
2872 return val;
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002873
2874 if (dent == d->dfs_chk_gen)
2875 d->chk_gen = val;
2876 else if (dent == d->dfs_chk_index)
2877 d->chk_index = val;
2878 else if (dent == d->dfs_chk_orph)
2879 d->chk_orph = val;
2880 else if (dent == d->dfs_chk_lprops)
2881 d->chk_lprops = val;
2882 else if (dent == d->dfs_chk_fs)
2883 d->chk_fs = val;
2884 else if (dent == d->dfs_tst_rcvry)
2885 d->tst_rcvry = val;
Artem Bityutskiy06bef942012-07-14 14:19:46 +03002886 else if (dent == d->dfs_ro_error)
2887 c->ro_error = !!val;
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002888 else
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002889 return -EINVAL;
2890
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002891 return count;
2892}
2893
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002894static const struct file_operations dfs_fops = {
Artem Bityutskiy7dae9972011-06-01 15:44:14 +03002895 .open = dfs_file_open,
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002896 .read = dfs_file_read,
2897 .write = dfs_file_write,
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002898 .owner = THIS_MODULE,
Artem Bityutskiy1bbfc842011-03-21 16:26:42 +02002899 .llseek = no_llseek,
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002900};
2901
2902/**
2903 * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
2904 * @c: UBIFS file-system description object
2905 *
2906 * This function creates all debugfs files for this instance of UBIFS. Returns
2907 * zero in case of success and a negative error code in case of failure.
2908 *
2909 * Note, the only reason we have not merged this function with the
2910 * 'ubifs_debugging_init()' function is because it is better to initialize
2911 * debugfs interfaces at the very end of the mount process, and remove them at
2912 * the very beginning of the mount process.
2913 */
2914int dbg_debugfs_init_fs(struct ubifs_info *c)
2915{
Artem Bityutskiyae380ce2011-05-19 14:13:16 +03002916 int err, n;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002917 const char *fname;
2918 struct dentry *dent;
2919 struct ubifs_debug_info *d = c->dbg;
2920
Brian Norris2d4cf5a2012-06-18 16:31:22 -07002921 if (!IS_ENABLED(CONFIG_DEBUG_FS))
Artem Bityutskiy818039c2012-06-06 16:03:10 +03002922 return 0;
2923
Artem Bityutskiyae380ce2011-05-19 14:13:16 +03002924 n = snprintf(d->dfs_dir_name, UBIFS_DFS_DIR_LEN + 1, UBIFS_DFS_DIR_NAME,
2925 c->vi.ubi_num, c->vi.vol_id);
2926 if (n == UBIFS_DFS_DIR_LEN) {
2927 /* The array size is too small */
2928 fname = UBIFS_DFS_DIR_NAME;
2929 dent = ERR_PTR(-EINVAL);
2930 goto out;
2931 }
2932
Artem Bityutskiycc6a86b2011-04-01 10:10:52 +03002933 fname = d->dfs_dir_name;
2934 dent = debugfs_create_dir(fname, dfs_rootdir);
Artem Bityutskiy95169532011-04-01 10:16:17 +03002935 if (IS_ERR_OR_NULL(dent))
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002936 goto out;
Artem Bityutskiycc6a86b2011-04-01 10:10:52 +03002937 d->dfs_dir = dent;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002938
2939 fname = "dump_lprops";
Vasiliy Kulikov8c559d32011-02-04 15:24:19 +03002940 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
Artem Bityutskiy95169532011-04-01 10:16:17 +03002941 if (IS_ERR_OR_NULL(dent))
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002942 goto out_remove;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002943 d->dfs_dump_lprops = dent;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002944
2945 fname = "dump_budg";
Vasiliy Kulikov8c559d32011-02-04 15:24:19 +03002946 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
Artem Bityutskiy95169532011-04-01 10:16:17 +03002947 if (IS_ERR_OR_NULL(dent))
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002948 goto out_remove;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002949 d->dfs_dump_budg = dent;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002950
2951 fname = "dump_tnc";
Vasiliy Kulikov8c559d32011-02-04 15:24:19 +03002952 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
Artem Bityutskiy95169532011-04-01 10:16:17 +03002953 if (IS_ERR_OR_NULL(dent))
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002954 goto out_remove;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002955 d->dfs_dump_tnc = dent;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002956
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002957 fname = "chk_general";
2958 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2959 &dfs_fops);
2960 if (IS_ERR_OR_NULL(dent))
2961 goto out_remove;
2962 d->dfs_chk_gen = dent;
2963
2964 fname = "chk_index";
2965 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2966 &dfs_fops);
2967 if (IS_ERR_OR_NULL(dent))
2968 goto out_remove;
2969 d->dfs_chk_index = dent;
2970
2971 fname = "chk_orphans";
2972 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2973 &dfs_fops);
2974 if (IS_ERR_OR_NULL(dent))
2975 goto out_remove;
2976 d->dfs_chk_orph = dent;
2977
2978 fname = "chk_lprops";
2979 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2980 &dfs_fops);
2981 if (IS_ERR_OR_NULL(dent))
2982 goto out_remove;
2983 d->dfs_chk_lprops = dent;
2984
2985 fname = "chk_fs";
2986 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2987 &dfs_fops);
2988 if (IS_ERR_OR_NULL(dent))
2989 goto out_remove;
2990 d->dfs_chk_fs = dent;
2991
2992 fname = "tst_recovery";
2993 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2994 &dfs_fops);
2995 if (IS_ERR_OR_NULL(dent))
2996 goto out_remove;
2997 d->dfs_tst_rcvry = dent;
2998
Artem Bityutskiy06bef942012-07-14 14:19:46 +03002999 fname = "ro_error";
3000 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
3001 &dfs_fops);
3002 if (IS_ERR_OR_NULL(dent))
3003 goto out_remove;
3004 d->dfs_ro_error = dent;
3005
Artem Bityutskiy552ff312008-10-23 11:49:28 +03003006 return 0;
3007
3008out_remove:
Artem Bityutskiycc6a86b2011-04-01 10:10:52 +03003009 debugfs_remove_recursive(d->dfs_dir);
3010out:
Artem Bityutskiy95169532011-04-01 10:16:17 +03003011 err = dent ? PTR_ERR(dent) : -ENODEV;
Artem Bityutskiye77170602011-06-01 17:43:43 +03003012 ubifs_err("cannot create \"%s\" debugfs file or directory, error %d\n",
Artem Bityutskiy552ff312008-10-23 11:49:28 +03003013 fname, err);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03003014 return err;
3015}
3016
3017/**
3018 * dbg_debugfs_exit_fs - remove all debugfs files.
3019 * @c: UBIFS file-system description object
3020 */
3021void dbg_debugfs_exit_fs(struct ubifs_info *c)
3022{
Brian Norris2d4cf5a2012-06-18 16:31:22 -07003023 if (IS_ENABLED(CONFIG_DEBUG_FS))
Artem Bityutskiy818039c2012-06-06 16:03:10 +03003024 debugfs_remove_recursive(c->dbg->dfs_dir);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03003025}
3026
Artem Bityutskiye77170602011-06-01 17:43:43 +03003027struct ubifs_global_debug_info ubifs_dbg;
3028
3029static struct dentry *dfs_chk_gen;
3030static struct dentry *dfs_chk_index;
3031static struct dentry *dfs_chk_orph;
3032static struct dentry *dfs_chk_lprops;
3033static struct dentry *dfs_chk_fs;
3034static struct dentry *dfs_tst_rcvry;
3035
3036static ssize_t dfs_global_file_read(struct file *file, char __user *u,
3037 size_t count, loff_t *ppos)
3038{
3039 struct dentry *dent = file->f_path.dentry;
3040 int val;
3041
3042 if (dent == dfs_chk_gen)
3043 val = ubifs_dbg.chk_gen;
3044 else if (dent == dfs_chk_index)
3045 val = ubifs_dbg.chk_index;
3046 else if (dent == dfs_chk_orph)
3047 val = ubifs_dbg.chk_orph;
3048 else if (dent == dfs_chk_lprops)
3049 val = ubifs_dbg.chk_lprops;
3050 else if (dent == dfs_chk_fs)
3051 val = ubifs_dbg.chk_fs;
3052 else if (dent == dfs_tst_rcvry)
3053 val = ubifs_dbg.tst_rcvry;
3054 else
3055 return -EINVAL;
3056
3057 return provide_user_output(val, u, count, ppos);
3058}
3059
3060static ssize_t dfs_global_file_write(struct file *file, const char __user *u,
3061 size_t count, loff_t *ppos)
3062{
3063 struct dentry *dent = file->f_path.dentry;
3064 int val;
3065
3066 val = interpret_user_input(u, count);
3067 if (val < 0)
3068 return val;
3069
3070 if (dent == dfs_chk_gen)
3071 ubifs_dbg.chk_gen = val;
3072 else if (dent == dfs_chk_index)
3073 ubifs_dbg.chk_index = val;
3074 else if (dent == dfs_chk_orph)
3075 ubifs_dbg.chk_orph = val;
3076 else if (dent == dfs_chk_lprops)
3077 ubifs_dbg.chk_lprops = val;
3078 else if (dent == dfs_chk_fs)
3079 ubifs_dbg.chk_fs = val;
3080 else if (dent == dfs_tst_rcvry)
3081 ubifs_dbg.tst_rcvry = val;
3082 else
3083 return -EINVAL;
3084
3085 return count;
3086}
3087
3088static const struct file_operations dfs_global_fops = {
3089 .read = dfs_global_file_read,
3090 .write = dfs_global_file_write,
3091 .owner = THIS_MODULE,
3092 .llseek = no_llseek,
3093};
3094
Artem Bityutskiy7dae9972011-06-01 15:44:14 +03003095/**
3096 * dbg_debugfs_init - initialize debugfs file-system.
3097 *
3098 * UBIFS uses debugfs file-system to expose various debugging knobs to
3099 * user-space. This function creates "ubifs" directory in the debugfs
3100 * file-system. Returns zero in case of success and a negative error code in
3101 * case of failure.
3102 */
3103int dbg_debugfs_init(void)
3104{
Artem Bityutskiye77170602011-06-01 17:43:43 +03003105 int err;
3106 const char *fname;
3107 struct dentry *dent;
3108
Brian Norris2d4cf5a2012-06-18 16:31:22 -07003109 if (!IS_ENABLED(CONFIG_DEBUG_FS))
Artem Bityutskiy818039c2012-06-06 16:03:10 +03003110 return 0;
3111
Artem Bityutskiye77170602011-06-01 17:43:43 +03003112 fname = "ubifs";
3113 dent = debugfs_create_dir(fname, NULL);
3114 if (IS_ERR_OR_NULL(dent))
3115 goto out;
3116 dfs_rootdir = dent;
3117
3118 fname = "chk_general";
3119 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3120 &dfs_global_fops);
3121 if (IS_ERR_OR_NULL(dent))
3122 goto out_remove;
3123 dfs_chk_gen = dent;
3124
3125 fname = "chk_index";
3126 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3127 &dfs_global_fops);
3128 if (IS_ERR_OR_NULL(dent))
3129 goto out_remove;
3130 dfs_chk_index = dent;
3131
3132 fname = "chk_orphans";
3133 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3134 &dfs_global_fops);
3135 if (IS_ERR_OR_NULL(dent))
3136 goto out_remove;
3137 dfs_chk_orph = dent;
3138
3139 fname = "chk_lprops";
3140 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3141 &dfs_global_fops);
3142 if (IS_ERR_OR_NULL(dent))
3143 goto out_remove;
3144 dfs_chk_lprops = dent;
3145
3146 fname = "chk_fs";
3147 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3148 &dfs_global_fops);
3149 if (IS_ERR_OR_NULL(dent))
3150 goto out_remove;
3151 dfs_chk_fs = dent;
3152
3153 fname = "tst_recovery";
3154 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3155 &dfs_global_fops);
3156 if (IS_ERR_OR_NULL(dent))
3157 goto out_remove;
3158 dfs_tst_rcvry = dent;
Artem Bityutskiy7dae9972011-06-01 15:44:14 +03003159
3160 return 0;
Artem Bityutskiye77170602011-06-01 17:43:43 +03003161
3162out_remove:
3163 debugfs_remove_recursive(dfs_rootdir);
3164out:
3165 err = dent ? PTR_ERR(dent) : -ENODEV;
3166 ubifs_err("cannot create \"%s\" debugfs file or directory, error %d\n",
3167 fname, err);
3168 return err;
Artem Bityutskiy7dae9972011-06-01 15:44:14 +03003169}
3170
3171/**
3172 * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
3173 */
3174void dbg_debugfs_exit(void)
3175{
Brian Norris2d4cf5a2012-06-18 16:31:22 -07003176 if (IS_ENABLED(CONFIG_DEBUG_FS))
Artem Bityutskiy818039c2012-06-06 16:03:10 +03003177 debugfs_remove_recursive(dfs_rootdir);
Artem Bityutskiy7dae9972011-06-01 15:44:14 +03003178}
3179
3180/**
3181 * ubifs_debugging_init - initialize UBIFS debugging.
3182 * @c: UBIFS file-system description object
3183 *
3184 * This function initializes debugging-related data for the file system.
3185 * Returns zero in case of success and a negative error code in case of
3186 * failure.
3187 */
3188int ubifs_debugging_init(struct ubifs_info *c)
3189{
3190 c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
3191 if (!c->dbg)
3192 return -ENOMEM;
3193
Artem Bityutskiy7dae9972011-06-01 15:44:14 +03003194 return 0;
3195}
3196
3197/**
3198 * ubifs_debugging_exit - free debugging data.
3199 * @c: UBIFS file-system description object
3200 */
3201void ubifs_debugging_exit(struct ubifs_info *c)
3202{
Artem Bityutskiy7dae9972011-06-01 15:44:14 +03003203 kfree(c->dbg);
3204}