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