blob: fa65b29310345986b4e7ed0c67137698c3ce6600 [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
605void dbg_dump_budg(struct ubifs_info *c)
606{
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 Bityutskiy21a60252008-12-12 11:13:17 -0500613 ubifs_assert(spin_is_locked(&c->space_lock));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300614 spin_lock(&dbg_lock);
Artem Bityutskiy1de94152008-07-25 12:58:38 +0300615 printk(KERN_DEBUG "(pid %d) Budgeting info: budg_data_growth %lld, "
616 "budg_dd_growth %lld, budg_idx_growth %lld\n", current->pid,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300617 c->budg_data_growth, c->budg_dd_growth, c->budg_idx_growth);
618 printk(KERN_DEBUG "\tdata budget sum %lld, total budget sum %lld, "
619 "freeable_cnt %d\n", c->budg_data_growth + c->budg_dd_growth,
620 c->budg_data_growth + c->budg_dd_growth + c->budg_idx_growth,
621 c->freeable_cnt);
622 printk(KERN_DEBUG "\tmin_idx_lebs %d, old_idx_sz %lld, "
623 "calc_idx_sz %lld, idx_gc_cnt %d\n", c->min_idx_lebs,
624 c->old_idx_sz, c->calc_idx_sz, c->idx_gc_cnt);
625 printk(KERN_DEBUG "\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, "
626 "clean_zn_cnt %ld\n", atomic_long_read(&c->dirty_pg_cnt),
627 atomic_long_read(&c->dirty_zn_cnt),
628 atomic_long_read(&c->clean_zn_cnt));
629 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);
631 printk(KERN_DEBUG "\tgc_lnum %d, ihead_lnum %d\n",
632 c->gc_lnum, c->ihead_lnum);
Artem Bityutskiy84abf972009-01-23 14:54:59 +0200633 /* If we are in R/O mode, journal heads do not exist */
634 if (c->jheads)
635 for (i = 0; i < c->jhead_cnt; i++)
Artem Bityutskiy77a7ae52009-09-15 15:03:51 +0300636 printk(KERN_DEBUG "\tjhead %s\t LEB %d\n",
637 dbg_jhead(c->jheads[i].wbuf.jhead),
638 c->jheads[i].wbuf.lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300639 for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
640 bud = rb_entry(rb, struct ubifs_bud, rb);
641 printk(KERN_DEBUG "\tbud LEB %d\n", bud->lnum);
642 }
643 list_for_each_entry(bud, &c->old_buds, list)
644 printk(KERN_DEBUG "\told bud LEB %d\n", bud->lnum);
645 list_for_each_entry(idx_gc, &c->idx_gc, list)
646 printk(KERN_DEBUG "\tGC'ed idx LEB %d unmap %d\n",
647 idx_gc->lnum, idx_gc->unmap);
648 printk(KERN_DEBUG "\tcommit state %d\n", c->cmt_state);
Artem Bityutskiy21a60252008-12-12 11:13:17 -0500649
650 /* Print budgeting predictions */
651 available = ubifs_calc_available(c, c->min_idx_lebs);
652 outstanding = c->budg_data_growth + c->budg_dd_growth;
Artem Bityutskiy84abf972009-01-23 14:54:59 +0200653 free = ubifs_get_free_space_nolock(c);
Artem Bityutskiy21a60252008-12-12 11:13:17 -0500654 printk(KERN_DEBUG "Budgeting predictions:\n");
655 printk(KERN_DEBUG "\tavailable: %lld, outstanding %lld, free %lld\n",
656 available, outstanding, free);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300657 spin_unlock(&dbg_lock);
658}
659
660void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
661{
Artem Bityutskiybe9e62a2008-12-28 10:17:23 +0200662 int i, spc, dark = 0, dead = 0;
663 struct rb_node *rb;
664 struct ubifs_bud *bud;
665
666 spc = lp->free + lp->dirty;
667 if (spc < c->dead_wm)
668 dead = spc;
669 else
670 dark = ubifs_calc_dark(c, spc);
671
672 if (lp->flags & LPROPS_INDEX)
673 printk(KERN_DEBUG "LEB %-7d free %-8d dirty %-8d used %-8d "
674 "free + dirty %-8d flags %#x (", lp->lnum, lp->free,
675 lp->dirty, c->leb_size - spc, spc, lp->flags);
676 else
677 printk(KERN_DEBUG "LEB %-7d free %-8d dirty %-8d used %-8d "
678 "free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d "
679 "flags %#-4x (", lp->lnum, lp->free, lp->dirty,
680 c->leb_size - spc, spc, dark, dead,
681 (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags);
682
683 if (lp->flags & LPROPS_TAKEN) {
684 if (lp->flags & LPROPS_INDEX)
685 printk(KERN_CONT "index, taken");
686 else
687 printk(KERN_CONT "taken");
688 } else {
689 const char *s;
690
691 if (lp->flags & LPROPS_INDEX) {
692 switch (lp->flags & LPROPS_CAT_MASK) {
693 case LPROPS_DIRTY_IDX:
694 s = "dirty index";
695 break;
696 case LPROPS_FRDI_IDX:
697 s = "freeable index";
698 break;
699 default:
700 s = "index";
701 }
702 } else {
703 switch (lp->flags & LPROPS_CAT_MASK) {
704 case LPROPS_UNCAT:
705 s = "not categorized";
706 break;
707 case LPROPS_DIRTY:
708 s = "dirty";
709 break;
710 case LPROPS_FREE:
711 s = "free";
712 break;
713 case LPROPS_EMPTY:
714 s = "empty";
715 break;
716 case LPROPS_FREEABLE:
717 s = "freeable";
718 break;
719 default:
720 s = NULL;
721 break;
722 }
723 }
724 printk(KERN_CONT "%s", s);
725 }
726
727 for (rb = rb_first((struct rb_root *)&c->buds); rb; rb = rb_next(rb)) {
728 bud = rb_entry(rb, struct ubifs_bud, rb);
729 if (bud->lnum == lp->lnum) {
730 int head = 0;
731 for (i = 0; i < c->jhead_cnt; i++) {
732 if (lp->lnum == c->jheads[i].wbuf.lnum) {
733 printk(KERN_CONT ", jhead %s",
734 dbg_jhead(i));
735 head = 1;
736 }
737 }
738 if (!head)
739 printk(KERN_CONT ", bud of jhead %s",
740 dbg_jhead(bud->jhead));
741 }
742 }
743 if (lp->lnum == c->gc_lnum)
744 printk(KERN_CONT ", GC LEB");
745 printk(KERN_CONT ")\n");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300746}
747
748void dbg_dump_lprops(struct ubifs_info *c)
749{
750 int lnum, err;
751 struct ubifs_lprops lp;
752 struct ubifs_lp_stats lst;
753
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200754 printk(KERN_DEBUG "(pid %d) start dumping LEB properties\n",
755 current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300756 ubifs_get_lp_stats(c, &lst);
757 dbg_dump_lstats(&lst);
758
759 for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
760 err = ubifs_read_one_lp(c, lnum, &lp);
761 if (err)
762 ubifs_err("cannot read lprops for LEB %d", lnum);
763
764 dbg_dump_lprop(c, &lp);
765 }
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200766 printk(KERN_DEBUG "(pid %d) finish dumping LEB properties\n",
767 current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300768}
769
Adrian Hunter73944a62008-09-12 18:13:31 +0300770void dbg_dump_lpt_info(struct ubifs_info *c)
771{
772 int i;
773
774 spin_lock(&dbg_lock);
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200775 printk(KERN_DEBUG "(pid %d) dumping LPT information\n", current->pid);
Adrian Hunter73944a62008-09-12 18:13:31 +0300776 printk(KERN_DEBUG "\tlpt_sz: %lld\n", c->lpt_sz);
777 printk(KERN_DEBUG "\tpnode_sz: %d\n", c->pnode_sz);
778 printk(KERN_DEBUG "\tnnode_sz: %d\n", c->nnode_sz);
779 printk(KERN_DEBUG "\tltab_sz: %d\n", c->ltab_sz);
780 printk(KERN_DEBUG "\tlsave_sz: %d\n", c->lsave_sz);
781 printk(KERN_DEBUG "\tbig_lpt: %d\n", c->big_lpt);
782 printk(KERN_DEBUG "\tlpt_hght: %d\n", c->lpt_hght);
783 printk(KERN_DEBUG "\tpnode_cnt: %d\n", c->pnode_cnt);
784 printk(KERN_DEBUG "\tnnode_cnt: %d\n", c->nnode_cnt);
785 printk(KERN_DEBUG "\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt);
786 printk(KERN_DEBUG "\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt);
787 printk(KERN_DEBUG "\tlsave_cnt: %d\n", c->lsave_cnt);
788 printk(KERN_DEBUG "\tspace_bits: %d\n", c->space_bits);
789 printk(KERN_DEBUG "\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
790 printk(KERN_DEBUG "\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
791 printk(KERN_DEBUG "\tlpt_spc_bits: %d\n", c->lpt_spc_bits);
792 printk(KERN_DEBUG "\tpcnt_bits: %d\n", c->pcnt_bits);
793 printk(KERN_DEBUG "\tlnum_bits: %d\n", c->lnum_bits);
794 printk(KERN_DEBUG "\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
795 printk(KERN_DEBUG "\tLPT head is at %d:%d\n",
796 c->nhead_lnum, c->nhead_offs);
Artem Bityutskiyf92b9822008-12-28 11:34:26 +0200797 printk(KERN_DEBUG "\tLPT ltab is at %d:%d\n",
798 c->ltab_lnum, c->ltab_offs);
Adrian Hunter73944a62008-09-12 18:13:31 +0300799 if (c->big_lpt)
800 printk(KERN_DEBUG "\tLPT lsave is at %d:%d\n",
801 c->lsave_lnum, c->lsave_offs);
802 for (i = 0; i < c->lpt_lebs; i++)
803 printk(KERN_DEBUG "\tLPT LEB %d free %d dirty %d tgc %d "
804 "cmt %d\n", i + c->lpt_first, c->ltab[i].free,
805 c->ltab[i].dirty, c->ltab[i].tgc, c->ltab[i].cmt);
806 spin_unlock(&dbg_lock);
807}
808
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300809void dbg_dump_leb(const struct ubifs_info *c, int lnum)
810{
811 struct ubifs_scan_leb *sleb;
812 struct ubifs_scan_node *snod;
Artem Bityutskiy73d9aec2011-03-11 15:39:09 +0200813 void *buf;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300814
815 if (dbg_failure_mode)
816 return;
817
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200818 printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n",
819 current->pid, lnum);
Artem Bityutskiy73d9aec2011-03-11 15:39:09 +0200820
Artem Bityutskiyfc5e58c2011-03-24 16:14:26 +0200821 buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
Artem Bityutskiy73d9aec2011-03-11 15:39:09 +0200822 if (!buf) {
823 ubifs_err("cannot allocate memory for dumping LEB %d", lnum);
824 return;
825 }
826
827 sleb = ubifs_scan(c, lnum, 0, buf, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300828 if (IS_ERR(sleb)) {
829 ubifs_err("scan error %d", (int)PTR_ERR(sleb));
Artem Bityutskiy73d9aec2011-03-11 15:39:09 +0200830 goto out;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300831 }
832
833 printk(KERN_DEBUG "LEB %d has %d nodes ending at %d\n", lnum,
834 sleb->nodes_cnt, sleb->endpt);
835
836 list_for_each_entry(snod, &sleb->nodes, list) {
837 cond_resched();
838 printk(KERN_DEBUG "Dumping node at LEB %d:%d len %d\n", lnum,
839 snod->offs, snod->len);
840 dbg_dump_node(c, snod->node);
841 }
842
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200843 printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n",
844 current->pid, lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300845 ubifs_scan_destroy(sleb);
Artem Bityutskiy73d9aec2011-03-11 15:39:09 +0200846
847out:
848 vfree(buf);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300849 return;
850}
851
852void dbg_dump_znode(const struct ubifs_info *c,
853 const struct ubifs_znode *znode)
854{
855 int n;
856 const struct ubifs_zbranch *zbr;
857
858 spin_lock(&dbg_lock);
859 if (znode->parent)
860 zbr = &znode->parent->zbranch[znode->iip];
861 else
862 zbr = &c->zroot;
863
864 printk(KERN_DEBUG "znode %p, LEB %d:%d len %d parent %p iip %d level %d"
865 " child_cnt %d flags %lx\n", znode, zbr->lnum, zbr->offs,
866 zbr->len, znode->parent, znode->iip, znode->level,
867 znode->child_cnt, znode->flags);
868
869 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
870 spin_unlock(&dbg_lock);
871 return;
872 }
873
874 printk(KERN_DEBUG "zbranches:\n");
875 for (n = 0; n < znode->child_cnt; n++) {
876 zbr = &znode->zbranch[n];
877 if (znode->level > 0)
878 printk(KERN_DEBUG "\t%d: znode %p LEB %d:%d len %d key "
879 "%s\n", n, zbr->znode, zbr->lnum,
880 zbr->offs, zbr->len,
881 DBGKEY(&zbr->key));
882 else
883 printk(KERN_DEBUG "\t%d: LNC %p LEB %d:%d len %d key "
884 "%s\n", n, zbr->znode, zbr->lnum,
885 zbr->offs, zbr->len,
886 DBGKEY(&zbr->key));
887 }
888 spin_unlock(&dbg_lock);
889}
890
891void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
892{
893 int i;
894
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200895 printk(KERN_DEBUG "(pid %d) start dumping heap cat %d (%d elements)\n",
Artem Bityutskiy1de94152008-07-25 12:58:38 +0300896 current->pid, cat, heap->cnt);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300897 for (i = 0; i < heap->cnt; i++) {
898 struct ubifs_lprops *lprops = heap->arr[i];
899
900 printk(KERN_DEBUG "\t%d. LEB %d hpos %d free %d dirty %d "
901 "flags %d\n", i, lprops->lnum, lprops->hpos,
902 lprops->free, lprops->dirty, lprops->flags);
903 }
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200904 printk(KERN_DEBUG "(pid %d) finish dumping heap\n", current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300905}
906
907void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
908 struct ubifs_nnode *parent, int iip)
909{
910 int i;
911
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200912 printk(KERN_DEBUG "(pid %d) dumping pnode:\n", current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300913 printk(KERN_DEBUG "\taddress %zx parent %zx cnext %zx\n",
914 (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
915 printk(KERN_DEBUG "\tflags %lu iip %d level %d num %d\n",
916 pnode->flags, iip, pnode->level, pnode->num);
917 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
918 struct ubifs_lprops *lp = &pnode->lprops[i];
919
920 printk(KERN_DEBUG "\t%d: free %d dirty %d flags %d lnum %d\n",
921 i, lp->free, lp->dirty, lp->flags, lp->lnum);
922 }
923}
924
925void dbg_dump_tnc(struct ubifs_info *c)
926{
927 struct ubifs_znode *znode;
928 int level;
929
930 printk(KERN_DEBUG "\n");
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200931 printk(KERN_DEBUG "(pid %d) start dumping TNC tree\n", current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300932 znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
933 level = znode->level;
934 printk(KERN_DEBUG "== Level %d ==\n", level);
935 while (znode) {
936 if (level != znode->level) {
937 level = znode->level;
938 printk(KERN_DEBUG "== Level %d ==\n", level);
939 }
940 dbg_dump_znode(c, znode);
941 znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
942 }
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200943 printk(KERN_DEBUG "(pid %d) finish dumping TNC tree\n", current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300944}
945
946static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
947 void *priv)
948{
949 dbg_dump_znode(c, znode);
950 return 0;
951}
952
953/**
954 * dbg_dump_index - dump the on-flash index.
955 * @c: UBIFS file-system description object
956 *
957 * This function dumps whole UBIFS indexing B-tree, unlike 'dbg_dump_tnc()'
958 * which dumps only in-memory znodes and does not read znodes which from flash.
959 */
960void dbg_dump_index(struct ubifs_info *c)
961{
962 dbg_walk_index(c, NULL, dump_znode, NULL);
963}
964
965/**
Artem Bityutskiy84abf972009-01-23 14:54:59 +0200966 * dbg_save_space_info - save information about flash space.
967 * @c: UBIFS file-system description object
968 *
969 * This function saves information about UBIFS free space, dirty space, etc, in
970 * order to check it later.
971 */
972void dbg_save_space_info(struct ubifs_info *c)
973{
974 struct ubifs_debug_info *d = c->dbg;
Artem Bityutskiy7da64432011-04-04 17:16:39 +0300975 int freeable_cnt;
Artem Bityutskiy84abf972009-01-23 14:54:59 +0200976
977 spin_lock(&c->space_lock);
Artem Bityutskiy7da64432011-04-04 17:16:39 +0300978 memcpy(&d->saved_lst, &c->lst, sizeof(struct ubifs_lp_stats));
979
980 /*
981 * We use a dirty hack here and zero out @c->freeable_cnt, because it
982 * affects the free space calculations, and UBIFS might not know about
983 * all freeable eraseblocks. Indeed, we know about freeable eraseblocks
984 * only when we read their lprops, and we do this only lazily, upon the
985 * need. So at any given point of time @c->freeable_cnt might be not
986 * exactly accurate.
987 *
988 * Just one example about the issue we hit when we did not zero
989 * @c->freeable_cnt.
990 * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the
991 * amount of free space in @d->saved_free
992 * 2. We re-mount R/W, which makes UBIFS to read the "lsave"
993 * information from flash, where we cache LEBs from various
994 * categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()'
995 * -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()'
996 * -> 'ubifs_get_pnode()' -> 'update_cats()'
997 * -> 'ubifs_add_to_cat()').
998 * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt
999 * becomes %1.
1000 * 4. We calculate the amount of free space when the re-mount is
1001 * finished in 'dbg_check_space_info()' and it does not match
1002 * @d->saved_free.
1003 */
1004 freeable_cnt = c->freeable_cnt;
1005 c->freeable_cnt = 0;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001006 d->saved_free = ubifs_get_free_space_nolock(c);
Artem Bityutskiy7da64432011-04-04 17:16:39 +03001007 c->freeable_cnt = freeable_cnt;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001008 spin_unlock(&c->space_lock);
1009}
1010
1011/**
1012 * dbg_check_space_info - check flash space information.
1013 * @c: UBIFS file-system description object
1014 *
1015 * This function compares current flash space information with the information
1016 * which was saved when the 'dbg_save_space_info()' function was called.
1017 * Returns zero if the information has not changed, and %-EINVAL it it has
1018 * changed.
1019 */
1020int dbg_check_space_info(struct ubifs_info *c)
1021{
1022 struct ubifs_debug_info *d = c->dbg;
1023 struct ubifs_lp_stats lst;
Artem Bityutskiy7da64432011-04-04 17:16:39 +03001024 long long free;
1025 int freeable_cnt;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001026
1027 spin_lock(&c->space_lock);
Artem Bityutskiy7da64432011-04-04 17:16:39 +03001028 freeable_cnt = c->freeable_cnt;
1029 c->freeable_cnt = 0;
1030 free = ubifs_get_free_space_nolock(c);
1031 c->freeable_cnt = freeable_cnt;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001032 spin_unlock(&c->space_lock);
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001033
1034 if (free != d->saved_free) {
1035 ubifs_err("free space changed from %lld to %lld",
1036 d->saved_free, free);
1037 goto out;
1038 }
1039
1040 return 0;
1041
1042out:
1043 ubifs_msg("saved lprops statistics dump");
1044 dbg_dump_lstats(&d->saved_lst);
1045 ubifs_get_lp_stats(c, &lst);
Artem Bityutskiye055f7e2009-09-17 15:08:31 +03001046
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001047 ubifs_msg("current lprops statistics dump");
Artem Bityutskiye055f7e2009-09-17 15:08:31 +03001048 dbg_dump_lstats(&lst);
1049
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001050 spin_lock(&c->space_lock);
1051 dbg_dump_budg(c);
1052 spin_unlock(&c->space_lock);
1053 dump_stack();
1054 return -EINVAL;
1055}
1056
1057/**
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001058 * dbg_check_synced_i_size - check synchronized inode size.
1059 * @inode: inode to check
1060 *
1061 * If inode is clean, synchronized inode size has to be equivalent to current
1062 * inode size. This function has to be called only for locked inodes (@i_mutex
1063 * has to be locked). Returns %0 if synchronized inode size if correct, and
1064 * %-EINVAL if not.
1065 */
1066int dbg_check_synced_i_size(struct inode *inode)
1067{
1068 int err = 0;
1069 struct ubifs_inode *ui = ubifs_inode(inode);
1070
1071 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
1072 return 0;
1073 if (!S_ISREG(inode->i_mode))
1074 return 0;
1075
1076 mutex_lock(&ui->ui_mutex);
1077 spin_lock(&ui->ui_lock);
1078 if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
1079 ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode "
1080 "is clean", ui->ui_size, ui->synced_i_size);
1081 ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
1082 inode->i_mode, i_size_read(inode));
1083 dbg_dump_stack();
1084 err = -EINVAL;
1085 }
1086 spin_unlock(&ui->ui_lock);
1087 mutex_unlock(&ui->ui_mutex);
1088 return err;
1089}
1090
1091/*
1092 * dbg_check_dir - check directory inode size and link count.
1093 * @c: UBIFS file-system description object
1094 * @dir: the directory to calculate size for
1095 * @size: the result is returned here
1096 *
1097 * This function makes sure that directory size and link count are correct.
1098 * Returns zero in case of success and a negative error code in case of
1099 * failure.
1100 *
1101 * Note, it is good idea to make sure the @dir->i_mutex is locked before
1102 * calling this function.
1103 */
1104int dbg_check_dir_size(struct ubifs_info *c, const struct inode *dir)
1105{
1106 unsigned int nlink = 2;
1107 union ubifs_key key;
1108 struct ubifs_dent_node *dent, *pdent = NULL;
1109 struct qstr nm = { .name = NULL };
1110 loff_t size = UBIFS_INO_NODE_SZ;
1111
1112 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
1113 return 0;
1114
1115 if (!S_ISDIR(dir->i_mode))
1116 return 0;
1117
1118 lowest_dent_key(c, &key, dir->i_ino);
1119 while (1) {
1120 int err;
1121
1122 dent = ubifs_tnc_next_ent(c, &key, &nm);
1123 if (IS_ERR(dent)) {
1124 err = PTR_ERR(dent);
1125 if (err == -ENOENT)
1126 break;
1127 return err;
1128 }
1129
1130 nm.name = dent->name;
1131 nm.len = le16_to_cpu(dent->nlen);
1132 size += CALC_DENT_SIZE(nm.len);
1133 if (dent->type == UBIFS_ITYPE_DIR)
1134 nlink += 1;
1135 kfree(pdent);
1136 pdent = dent;
1137 key_read(c, &dent->key, &key);
1138 }
1139 kfree(pdent);
1140
1141 if (i_size_read(dir) != size) {
1142 ubifs_err("directory inode %lu has size %llu, "
1143 "but calculated size is %llu", dir->i_ino,
1144 (unsigned long long)i_size_read(dir),
1145 (unsigned long long)size);
1146 dump_stack();
1147 return -EINVAL;
1148 }
1149 if (dir->i_nlink != nlink) {
1150 ubifs_err("directory inode %lu has nlink %u, but calculated "
1151 "nlink is %u", dir->i_ino, dir->i_nlink, nlink);
1152 dump_stack();
1153 return -EINVAL;
1154 }
1155
1156 return 0;
1157}
1158
1159/**
1160 * dbg_check_key_order - make sure that colliding keys are properly ordered.
1161 * @c: UBIFS file-system description object
1162 * @zbr1: first zbranch
1163 * @zbr2: following zbranch
1164 *
1165 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
1166 * names of the direntries/xentries which are referred by the keys. This
1167 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
1168 * sure the name of direntry/xentry referred by @zbr1 is less than
1169 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
1170 * and a negative error code in case of failure.
1171 */
1172static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
1173 struct ubifs_zbranch *zbr2)
1174{
1175 int err, nlen1, nlen2, cmp;
1176 struct ubifs_dent_node *dent1, *dent2;
1177 union ubifs_key key;
1178
1179 ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
1180 dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1181 if (!dent1)
1182 return -ENOMEM;
1183 dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1184 if (!dent2) {
1185 err = -ENOMEM;
1186 goto out_free;
1187 }
1188
1189 err = ubifs_tnc_read_node(c, zbr1, dent1);
1190 if (err)
1191 goto out_free;
1192 err = ubifs_validate_entry(c, dent1);
1193 if (err)
1194 goto out_free;
1195
1196 err = ubifs_tnc_read_node(c, zbr2, dent2);
1197 if (err)
1198 goto out_free;
1199 err = ubifs_validate_entry(c, dent2);
1200 if (err)
1201 goto out_free;
1202
1203 /* Make sure node keys are the same as in zbranch */
1204 err = 1;
1205 key_read(c, &dent1->key, &key);
1206 if (keys_cmp(c, &zbr1->key, &key)) {
Artem Bityutskiy5d38b3a2008-12-30 17:58:42 +02001207 dbg_err("1st entry at %d:%d has key %s", zbr1->lnum,
1208 zbr1->offs, DBGKEY(&key));
1209 dbg_err("but it should have key %s according to tnc",
1210 DBGKEY(&zbr1->key));
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +02001211 dbg_dump_node(c, dent1);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03001212 goto out_free;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001213 }
1214
1215 key_read(c, &dent2->key, &key);
1216 if (keys_cmp(c, &zbr2->key, &key)) {
Artem Bityutskiy5d38b3a2008-12-30 17:58:42 +02001217 dbg_err("2nd entry at %d:%d has key %s", zbr1->lnum,
1218 zbr1->offs, DBGKEY(&key));
1219 dbg_err("but it should have key %s according to tnc",
1220 DBGKEY(&zbr2->key));
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +02001221 dbg_dump_node(c, dent2);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03001222 goto out_free;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001223 }
1224
1225 nlen1 = le16_to_cpu(dent1->nlen);
1226 nlen2 = le16_to_cpu(dent2->nlen);
1227
1228 cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
1229 if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
1230 err = 0;
1231 goto out_free;
1232 }
1233 if (cmp == 0 && nlen1 == nlen2)
Artem Bityutskiy5d38b3a2008-12-30 17:58:42 +02001234 dbg_err("2 xent/dent nodes with the same name");
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001235 else
Artem Bityutskiy5d38b3a2008-12-30 17:58:42 +02001236 dbg_err("bad order of colliding key %s",
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001237 DBGKEY(&key));
1238
Artem Bityutskiy552ff312008-10-23 11:49:28 +03001239 ubifs_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001240 dbg_dump_node(c, dent1);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03001241 ubifs_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001242 dbg_dump_node(c, dent2);
1243
1244out_free:
1245 kfree(dent2);
1246 kfree(dent1);
1247 return err;
1248}
1249
1250/**
1251 * dbg_check_znode - check if znode is all right.
1252 * @c: UBIFS file-system description object
1253 * @zbr: zbranch which points to this znode
1254 *
1255 * This function makes sure that znode referred to by @zbr is all right.
1256 * Returns zero if it is, and %-EINVAL if it is not.
1257 */
1258static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
1259{
1260 struct ubifs_znode *znode = zbr->znode;
1261 struct ubifs_znode *zp = znode->parent;
1262 int n, err, cmp;
1263
1264 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
1265 err = 1;
1266 goto out;
1267 }
1268 if (znode->level < 0) {
1269 err = 2;
1270 goto out;
1271 }
1272 if (znode->iip < 0 || znode->iip >= c->fanout) {
1273 err = 3;
1274 goto out;
1275 }
1276
1277 if (zbr->len == 0)
1278 /* Only dirty zbranch may have no on-flash nodes */
1279 if (!ubifs_zn_dirty(znode)) {
1280 err = 4;
1281 goto out;
1282 }
1283
1284 if (ubifs_zn_dirty(znode)) {
1285 /*
1286 * If znode is dirty, its parent has to be dirty as well. The
1287 * order of the operation is important, so we have to have
1288 * memory barriers.
1289 */
1290 smp_mb();
1291 if (zp && !ubifs_zn_dirty(zp)) {
1292 /*
1293 * The dirty flag is atomic and is cleared outside the
1294 * TNC mutex, so znode's dirty flag may now have
1295 * been cleared. The child is always cleared before the
1296 * parent, so we just need to check again.
1297 */
1298 smp_mb();
1299 if (ubifs_zn_dirty(znode)) {
1300 err = 5;
1301 goto out;
1302 }
1303 }
1304 }
1305
1306 if (zp) {
1307 const union ubifs_key *min, *max;
1308
1309 if (znode->level != zp->level - 1) {
1310 err = 6;
1311 goto out;
1312 }
1313
1314 /* Make sure the 'parent' pointer in our znode is correct */
1315 err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
1316 if (!err) {
1317 /* This zbranch does not exist in the parent */
1318 err = 7;
1319 goto out;
1320 }
1321
1322 if (znode->iip >= zp->child_cnt) {
1323 err = 8;
1324 goto out;
1325 }
1326
1327 if (znode->iip != n) {
1328 /* This may happen only in case of collisions */
1329 if (keys_cmp(c, &zp->zbranch[n].key,
1330 &zp->zbranch[znode->iip].key)) {
1331 err = 9;
1332 goto out;
1333 }
1334 n = znode->iip;
1335 }
1336
1337 /*
1338 * Make sure that the first key in our znode is greater than or
1339 * equal to the key in the pointing zbranch.
1340 */
1341 min = &zbr->key;
1342 cmp = keys_cmp(c, min, &znode->zbranch[0].key);
1343 if (cmp == 1) {
1344 err = 10;
1345 goto out;
1346 }
1347
1348 if (n + 1 < zp->child_cnt) {
1349 max = &zp->zbranch[n + 1].key;
1350
1351 /*
1352 * Make sure the last key in our znode is less or
Artem Bityutskiy7d4e9cc2009-03-20 19:11:12 +02001353 * equivalent than the key in the zbranch which goes
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001354 * after our pointing zbranch.
1355 */
1356 cmp = keys_cmp(c, max,
1357 &znode->zbranch[znode->child_cnt - 1].key);
1358 if (cmp == -1) {
1359 err = 11;
1360 goto out;
1361 }
1362 }
1363 } else {
1364 /* This may only be root znode */
1365 if (zbr != &c->zroot) {
1366 err = 12;
1367 goto out;
1368 }
1369 }
1370
1371 /*
1372 * Make sure that next key is greater or equivalent then the previous
1373 * one.
1374 */
1375 for (n = 1; n < znode->child_cnt; n++) {
1376 cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
1377 &znode->zbranch[n].key);
1378 if (cmp > 0) {
1379 err = 13;
1380 goto out;
1381 }
1382 if (cmp == 0) {
1383 /* This can only be keys with colliding hash */
1384 if (!is_hash_key(c, &znode->zbranch[n].key)) {
1385 err = 14;
1386 goto out;
1387 }
1388
1389 if (znode->level != 0 || c->replaying)
1390 continue;
1391
1392 /*
1393 * Colliding keys should follow binary order of
1394 * corresponding xentry/dentry names.
1395 */
1396 err = dbg_check_key_order(c, &znode->zbranch[n - 1],
1397 &znode->zbranch[n]);
1398 if (err < 0)
1399 return err;
1400 if (err) {
1401 err = 15;
1402 goto out;
1403 }
1404 }
1405 }
1406
1407 for (n = 0; n < znode->child_cnt; n++) {
1408 if (!znode->zbranch[n].znode &&
1409 (znode->zbranch[n].lnum == 0 ||
1410 znode->zbranch[n].len == 0)) {
1411 err = 16;
1412 goto out;
1413 }
1414
1415 if (znode->zbranch[n].lnum != 0 &&
1416 znode->zbranch[n].len == 0) {
1417 err = 17;
1418 goto out;
1419 }
1420
1421 if (znode->zbranch[n].lnum == 0 &&
1422 znode->zbranch[n].len != 0) {
1423 err = 18;
1424 goto out;
1425 }
1426
1427 if (znode->zbranch[n].lnum == 0 &&
1428 znode->zbranch[n].offs != 0) {
1429 err = 19;
1430 goto out;
1431 }
1432
1433 if (znode->level != 0 && znode->zbranch[n].znode)
1434 if (znode->zbranch[n].znode->parent != znode) {
1435 err = 20;
1436 goto out;
1437 }
1438 }
1439
1440 return 0;
1441
1442out:
1443 ubifs_err("failed, error %d", err);
1444 ubifs_msg("dump of the znode");
1445 dbg_dump_znode(c, znode);
1446 if (zp) {
1447 ubifs_msg("dump of the parent znode");
1448 dbg_dump_znode(c, zp);
1449 }
1450 dump_stack();
1451 return -EINVAL;
1452}
1453
1454/**
1455 * dbg_check_tnc - check TNC tree.
1456 * @c: UBIFS file-system description object
1457 * @extra: do extra checks that are possible at start commit
1458 *
1459 * This function traverses whole TNC tree and checks every znode. Returns zero
1460 * if everything is all right and %-EINVAL if something is wrong with TNC.
1461 */
1462int dbg_check_tnc(struct ubifs_info *c, int extra)
1463{
1464 struct ubifs_znode *znode;
1465 long clean_cnt = 0, dirty_cnt = 0;
1466 int err, last;
1467
1468 if (!(ubifs_chk_flags & UBIFS_CHK_TNC))
1469 return 0;
1470
1471 ubifs_assert(mutex_is_locked(&c->tnc_mutex));
1472 if (!c->zroot.znode)
1473 return 0;
1474
1475 znode = ubifs_tnc_postorder_first(c->zroot.znode);
1476 while (1) {
1477 struct ubifs_znode *prev;
1478 struct ubifs_zbranch *zbr;
1479
1480 if (!znode->parent)
1481 zbr = &c->zroot;
1482 else
1483 zbr = &znode->parent->zbranch[znode->iip];
1484
1485 err = dbg_check_znode(c, zbr);
1486 if (err)
1487 return err;
1488
1489 if (extra) {
1490 if (ubifs_zn_dirty(znode))
1491 dirty_cnt += 1;
1492 else
1493 clean_cnt += 1;
1494 }
1495
1496 prev = znode;
1497 znode = ubifs_tnc_postorder_next(znode);
1498 if (!znode)
1499 break;
1500
1501 /*
1502 * If the last key of this znode is equivalent to the first key
1503 * of the next znode (collision), then check order of the keys.
1504 */
1505 last = prev->child_cnt - 1;
1506 if (prev->level == 0 && znode->level == 0 && !c->replaying &&
1507 !keys_cmp(c, &prev->zbranch[last].key,
1508 &znode->zbranch[0].key)) {
1509 err = dbg_check_key_order(c, &prev->zbranch[last],
1510 &znode->zbranch[0]);
1511 if (err < 0)
1512 return err;
1513 if (err) {
1514 ubifs_msg("first znode");
1515 dbg_dump_znode(c, prev);
1516 ubifs_msg("second znode");
1517 dbg_dump_znode(c, znode);
1518 return -EINVAL;
1519 }
1520 }
1521 }
1522
1523 if (extra) {
1524 if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
1525 ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld",
1526 atomic_long_read(&c->clean_zn_cnt),
1527 clean_cnt);
1528 return -EINVAL;
1529 }
1530 if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
1531 ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld",
1532 atomic_long_read(&c->dirty_zn_cnt),
1533 dirty_cnt);
1534 return -EINVAL;
1535 }
1536 }
1537
1538 return 0;
1539}
1540
1541/**
1542 * dbg_walk_index - walk the on-flash index.
1543 * @c: UBIFS file-system description object
1544 * @leaf_cb: called for each leaf node
1545 * @znode_cb: called for each indexing node
Adrian Hunter227c75c2009-01-29 11:53:51 +02001546 * @priv: private data which is passed to callbacks
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001547 *
1548 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1549 * node and @znode_cb for each indexing node. Returns zero in case of success
1550 * and a negative error code in case of failure.
1551 *
1552 * It would be better if this function removed every znode it pulled to into
1553 * the TNC, so that the behavior more closely matched the non-debugging
1554 * behavior.
1555 */
1556int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1557 dbg_znode_callback znode_cb, void *priv)
1558{
1559 int err;
1560 struct ubifs_zbranch *zbr;
1561 struct ubifs_znode *znode, *child;
1562
1563 mutex_lock(&c->tnc_mutex);
1564 /* If the root indexing node is not in TNC - pull it */
1565 if (!c->zroot.znode) {
1566 c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1567 if (IS_ERR(c->zroot.znode)) {
1568 err = PTR_ERR(c->zroot.znode);
1569 c->zroot.znode = NULL;
1570 goto out_unlock;
1571 }
1572 }
1573
1574 /*
1575 * We are going to traverse the indexing tree in the postorder manner.
1576 * Go down and find the leftmost indexing node where we are going to
1577 * start from.
1578 */
1579 znode = c->zroot.znode;
1580 while (znode->level > 0) {
1581 zbr = &znode->zbranch[0];
1582 child = zbr->znode;
1583 if (!child) {
1584 child = ubifs_load_znode(c, zbr, znode, 0);
1585 if (IS_ERR(child)) {
1586 err = PTR_ERR(child);
1587 goto out_unlock;
1588 }
1589 zbr->znode = child;
1590 }
1591
1592 znode = child;
1593 }
1594
1595 /* Iterate over all indexing nodes */
1596 while (1) {
1597 int idx;
1598
1599 cond_resched();
1600
1601 if (znode_cb) {
1602 err = znode_cb(c, znode, priv);
1603 if (err) {
1604 ubifs_err("znode checking function returned "
1605 "error %d", err);
1606 dbg_dump_znode(c, znode);
1607 goto out_dump;
1608 }
1609 }
1610 if (leaf_cb && znode->level == 0) {
1611 for (idx = 0; idx < znode->child_cnt; idx++) {
1612 zbr = &znode->zbranch[idx];
1613 err = leaf_cb(c, zbr, priv);
1614 if (err) {
1615 ubifs_err("leaf checking function "
1616 "returned error %d, for leaf "
1617 "at LEB %d:%d",
1618 err, zbr->lnum, zbr->offs);
1619 goto out_dump;
1620 }
1621 }
1622 }
1623
1624 if (!znode->parent)
1625 break;
1626
1627 idx = znode->iip + 1;
1628 znode = znode->parent;
1629 if (idx < znode->child_cnt) {
1630 /* Switch to the next index in the parent */
1631 zbr = &znode->zbranch[idx];
1632 child = zbr->znode;
1633 if (!child) {
1634 child = ubifs_load_znode(c, zbr, znode, idx);
1635 if (IS_ERR(child)) {
1636 err = PTR_ERR(child);
1637 goto out_unlock;
1638 }
1639 zbr->znode = child;
1640 }
1641 znode = child;
1642 } else
1643 /*
1644 * This is the last child, switch to the parent and
1645 * continue.
1646 */
1647 continue;
1648
1649 /* Go to the lowest leftmost znode in the new sub-tree */
1650 while (znode->level > 0) {
1651 zbr = &znode->zbranch[0];
1652 child = zbr->znode;
1653 if (!child) {
1654 child = ubifs_load_znode(c, zbr, znode, 0);
1655 if (IS_ERR(child)) {
1656 err = PTR_ERR(child);
1657 goto out_unlock;
1658 }
1659 zbr->znode = child;
1660 }
1661 znode = child;
1662 }
1663 }
1664
1665 mutex_unlock(&c->tnc_mutex);
1666 return 0;
1667
1668out_dump:
1669 if (znode->parent)
1670 zbr = &znode->parent->zbranch[znode->iip];
1671 else
1672 zbr = &c->zroot;
1673 ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1674 dbg_dump_znode(c, znode);
1675out_unlock:
1676 mutex_unlock(&c->tnc_mutex);
1677 return err;
1678}
1679
1680/**
1681 * add_size - add znode size to partially calculated index size.
1682 * @c: UBIFS file-system description object
1683 * @znode: znode to add size for
1684 * @priv: partially calculated index size
1685 *
1686 * This is a helper function for 'dbg_check_idx_size()' which is called for
1687 * every indexing node and adds its size to the 'long long' variable pointed to
1688 * by @priv.
1689 */
1690static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1691{
1692 long long *idx_size = priv;
1693 int add;
1694
1695 add = ubifs_idx_node_sz(c, znode->child_cnt);
1696 add = ALIGN(add, 8);
1697 *idx_size += add;
1698 return 0;
1699}
1700
1701/**
1702 * dbg_check_idx_size - check index size.
1703 * @c: UBIFS file-system description object
1704 * @idx_size: size to check
1705 *
1706 * This function walks the UBIFS index, calculates its size and checks that the
1707 * size is equivalent to @idx_size. Returns zero in case of success and a
1708 * negative error code in case of failure.
1709 */
1710int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
1711{
1712 int err;
1713 long long calc = 0;
1714
1715 if (!(ubifs_chk_flags & UBIFS_CHK_IDX_SZ))
1716 return 0;
1717
1718 err = dbg_walk_index(c, NULL, add_size, &calc);
1719 if (err) {
1720 ubifs_err("error %d while walking the index", err);
1721 return err;
1722 }
1723
1724 if (calc != idx_size) {
1725 ubifs_err("index size check failed: calculated size is %lld, "
1726 "should be %lld", calc, idx_size);
1727 dump_stack();
1728 return -EINVAL;
1729 }
1730
1731 return 0;
1732}
1733
1734/**
1735 * struct fsck_inode - information about an inode used when checking the file-system.
1736 * @rb: link in the RB-tree of inodes
1737 * @inum: inode number
1738 * @mode: inode type, permissions, etc
1739 * @nlink: inode link count
1740 * @xattr_cnt: count of extended attributes
1741 * @references: how many directory/xattr entries refer this inode (calculated
1742 * while walking the index)
1743 * @calc_cnt: for directory inode count of child directories
1744 * @size: inode size (read from on-flash inode)
1745 * @xattr_sz: summary size of all extended attributes (read from on-flash
1746 * inode)
1747 * @calc_sz: for directories calculated directory size
1748 * @calc_xcnt: count of extended attributes
1749 * @calc_xsz: calculated summary size of all extended attributes
1750 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1751 * inode (read from on-flash inode)
1752 * @calc_xnms: calculated sum of lengths of all extended attribute names
1753 */
1754struct fsck_inode {
1755 struct rb_node rb;
1756 ino_t inum;
1757 umode_t mode;
1758 unsigned int nlink;
1759 unsigned int xattr_cnt;
1760 int references;
1761 int calc_cnt;
1762 long long size;
1763 unsigned int xattr_sz;
1764 long long calc_sz;
1765 long long calc_xcnt;
1766 long long calc_xsz;
1767 unsigned int xattr_nms;
1768 long long calc_xnms;
1769};
1770
1771/**
1772 * struct fsck_data - private FS checking information.
1773 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1774 */
1775struct fsck_data {
1776 struct rb_root inodes;
1777};
1778
1779/**
1780 * add_inode - add inode information to RB-tree of inodes.
1781 * @c: UBIFS file-system description object
1782 * @fsckd: FS checking information
1783 * @ino: raw UBIFS inode to add
1784 *
1785 * This is a helper function for 'check_leaf()' which adds information about
1786 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1787 * case of success and a negative error code in case of failure.
1788 */
1789static struct fsck_inode *add_inode(struct ubifs_info *c,
1790 struct fsck_data *fsckd,
1791 struct ubifs_ino_node *ino)
1792{
1793 struct rb_node **p, *parent = NULL;
1794 struct fsck_inode *fscki;
1795 ino_t inum = key_inum_flash(c, &ino->key);
1796
1797 p = &fsckd->inodes.rb_node;
1798 while (*p) {
1799 parent = *p;
1800 fscki = rb_entry(parent, struct fsck_inode, rb);
1801 if (inum < fscki->inum)
1802 p = &(*p)->rb_left;
1803 else if (inum > fscki->inum)
1804 p = &(*p)->rb_right;
1805 else
1806 return fscki;
1807 }
1808
1809 if (inum > c->highest_inum) {
1810 ubifs_err("too high inode number, max. is %lu",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001811 (unsigned long)c->highest_inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001812 return ERR_PTR(-EINVAL);
1813 }
1814
1815 fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
1816 if (!fscki)
1817 return ERR_PTR(-ENOMEM);
1818
1819 fscki->inum = inum;
1820 fscki->nlink = le32_to_cpu(ino->nlink);
1821 fscki->size = le64_to_cpu(ino->size);
1822 fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
1823 fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
1824 fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
1825 fscki->mode = le32_to_cpu(ino->mode);
1826 if (S_ISDIR(fscki->mode)) {
1827 fscki->calc_sz = UBIFS_INO_NODE_SZ;
1828 fscki->calc_cnt = 2;
1829 }
1830 rb_link_node(&fscki->rb, parent, p);
1831 rb_insert_color(&fscki->rb, &fsckd->inodes);
1832 return fscki;
1833}
1834
1835/**
1836 * search_inode - search inode in the RB-tree of inodes.
1837 * @fsckd: FS checking information
1838 * @inum: inode number to search
1839 *
1840 * This is a helper function for 'check_leaf()' which searches inode @inum in
1841 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1842 * the inode was not found.
1843 */
1844static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
1845{
1846 struct rb_node *p;
1847 struct fsck_inode *fscki;
1848
1849 p = fsckd->inodes.rb_node;
1850 while (p) {
1851 fscki = rb_entry(p, struct fsck_inode, rb);
1852 if (inum < fscki->inum)
1853 p = p->rb_left;
1854 else if (inum > fscki->inum)
1855 p = p->rb_right;
1856 else
1857 return fscki;
1858 }
1859 return NULL;
1860}
1861
1862/**
1863 * read_add_inode - read inode node and add it to RB-tree of inodes.
1864 * @c: UBIFS file-system description object
1865 * @fsckd: FS checking information
1866 * @inum: inode number to read
1867 *
1868 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1869 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1870 * information pointer in case of success and a negative error code in case of
1871 * failure.
1872 */
1873static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1874 struct fsck_data *fsckd, ino_t inum)
1875{
1876 int n, err;
1877 union ubifs_key key;
1878 struct ubifs_znode *znode;
1879 struct ubifs_zbranch *zbr;
1880 struct ubifs_ino_node *ino;
1881 struct fsck_inode *fscki;
1882
1883 fscki = search_inode(fsckd, inum);
1884 if (fscki)
1885 return fscki;
1886
1887 ino_key_init(c, &key, inum);
1888 err = ubifs_lookup_level0(c, &key, &znode, &n);
1889 if (!err) {
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001890 ubifs_err("inode %lu not found in index", (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001891 return ERR_PTR(-ENOENT);
1892 } else if (err < 0) {
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001893 ubifs_err("error %d while looking up inode %lu",
1894 err, (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001895 return ERR_PTR(err);
1896 }
1897
1898 zbr = &znode->zbranch[n];
1899 if (zbr->len < UBIFS_INO_NODE_SZ) {
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001900 ubifs_err("bad node %lu node length %d",
1901 (unsigned long)inum, zbr->len);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001902 return ERR_PTR(-EINVAL);
1903 }
1904
1905 ino = kmalloc(zbr->len, GFP_NOFS);
1906 if (!ino)
1907 return ERR_PTR(-ENOMEM);
1908
1909 err = ubifs_tnc_read_node(c, zbr, ino);
1910 if (err) {
1911 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
1912 zbr->lnum, zbr->offs, err);
1913 kfree(ino);
1914 return ERR_PTR(err);
1915 }
1916
1917 fscki = add_inode(c, fsckd, ino);
1918 kfree(ino);
1919 if (IS_ERR(fscki)) {
1920 ubifs_err("error %ld while adding inode %lu node",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001921 PTR_ERR(fscki), (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001922 return fscki;
1923 }
1924
1925 return fscki;
1926}
1927
1928/**
1929 * check_leaf - check leaf node.
1930 * @c: UBIFS file-system description object
1931 * @zbr: zbranch of the leaf node to check
1932 * @priv: FS checking information
1933 *
1934 * This is a helper function for 'dbg_check_filesystem()' which is called for
1935 * every single leaf node while walking the indexing tree. It checks that the
1936 * leaf node referred from the indexing tree exists, has correct CRC, and does
1937 * some other basic validation. This function is also responsible for building
1938 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
1939 * calculates reference count, size, etc for each inode in order to later
1940 * compare them to the information stored inside the inodes and detect possible
1941 * inconsistencies. Returns zero in case of success and a negative error code
1942 * in case of failure.
1943 */
1944static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
1945 void *priv)
1946{
1947 ino_t inum;
1948 void *node;
1949 struct ubifs_ch *ch;
1950 int err, type = key_type(c, &zbr->key);
1951 struct fsck_inode *fscki;
1952
1953 if (zbr->len < UBIFS_CH_SZ) {
1954 ubifs_err("bad leaf length %d (LEB %d:%d)",
1955 zbr->len, zbr->lnum, zbr->offs);
1956 return -EINVAL;
1957 }
1958
1959 node = kmalloc(zbr->len, GFP_NOFS);
1960 if (!node)
1961 return -ENOMEM;
1962
1963 err = ubifs_tnc_read_node(c, zbr, node);
1964 if (err) {
1965 ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
1966 zbr->lnum, zbr->offs, err);
1967 goto out_free;
1968 }
1969
1970 /* If this is an inode node, add it to RB-tree of inodes */
1971 if (type == UBIFS_INO_KEY) {
1972 fscki = add_inode(c, priv, node);
1973 if (IS_ERR(fscki)) {
1974 err = PTR_ERR(fscki);
1975 ubifs_err("error %d while adding inode node", err);
1976 goto out_dump;
1977 }
1978 goto out;
1979 }
1980
1981 if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
1982 type != UBIFS_DATA_KEY) {
1983 ubifs_err("unexpected node type %d at LEB %d:%d",
1984 type, zbr->lnum, zbr->offs);
1985 err = -EINVAL;
1986 goto out_free;
1987 }
1988
1989 ch = node;
1990 if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
1991 ubifs_err("too high sequence number, max. is %llu",
1992 c->max_sqnum);
1993 err = -EINVAL;
1994 goto out_dump;
1995 }
1996
1997 if (type == UBIFS_DATA_KEY) {
1998 long long blk_offs;
1999 struct ubifs_data_node *dn = node;
2000
2001 /*
2002 * Search the inode node this data node belongs to and insert
2003 * it to the RB-tree of inodes.
2004 */
2005 inum = key_inum_flash(c, &dn->key);
2006 fscki = read_add_inode(c, priv, inum);
2007 if (IS_ERR(fscki)) {
2008 err = PTR_ERR(fscki);
2009 ubifs_err("error %d while processing data node and "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002010 "trying to find inode node %lu",
2011 err, (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002012 goto out_dump;
2013 }
2014
2015 /* Make sure the data node is within inode size */
2016 blk_offs = key_block_flash(c, &dn->key);
2017 blk_offs <<= UBIFS_BLOCK_SHIFT;
2018 blk_offs += le32_to_cpu(dn->size);
2019 if (blk_offs > fscki->size) {
2020 ubifs_err("data node at LEB %d:%d is not within inode "
2021 "size %lld", zbr->lnum, zbr->offs,
2022 fscki->size);
2023 err = -EINVAL;
2024 goto out_dump;
2025 }
2026 } else {
2027 int nlen;
2028 struct ubifs_dent_node *dent = node;
2029 struct fsck_inode *fscki1;
2030
2031 err = ubifs_validate_entry(c, dent);
2032 if (err)
2033 goto out_dump;
2034
2035 /*
2036 * Search the inode node this entry refers to and the parent
2037 * inode node and insert them to the RB-tree of inodes.
2038 */
2039 inum = le64_to_cpu(dent->inum);
2040 fscki = read_add_inode(c, priv, inum);
2041 if (IS_ERR(fscki)) {
2042 err = PTR_ERR(fscki);
2043 ubifs_err("error %d while processing entry node and "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002044 "trying to find inode node %lu",
2045 err, (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002046 goto out_dump;
2047 }
2048
2049 /* Count how many direntries or xentries refers this inode */
2050 fscki->references += 1;
2051
2052 inum = key_inum_flash(c, &dent->key);
2053 fscki1 = read_add_inode(c, priv, inum);
2054 if (IS_ERR(fscki1)) {
Roel Kluinb38882f2009-12-07 14:21:45 +01002055 err = PTR_ERR(fscki1);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002056 ubifs_err("error %d while processing entry node and "
2057 "trying to find parent inode node %lu",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002058 err, (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002059 goto out_dump;
2060 }
2061
2062 nlen = le16_to_cpu(dent->nlen);
2063 if (type == UBIFS_XENT_KEY) {
2064 fscki1->calc_xcnt += 1;
2065 fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
2066 fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
2067 fscki1->calc_xnms += nlen;
2068 } else {
2069 fscki1->calc_sz += CALC_DENT_SIZE(nlen);
2070 if (dent->type == UBIFS_ITYPE_DIR)
2071 fscki1->calc_cnt += 1;
2072 }
2073 }
2074
2075out:
2076 kfree(node);
2077 return 0;
2078
2079out_dump:
2080 ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
2081 dbg_dump_node(c, node);
2082out_free:
2083 kfree(node);
2084 return err;
2085}
2086
2087/**
2088 * free_inodes - free RB-tree of inodes.
2089 * @fsckd: FS checking information
2090 */
2091static void free_inodes(struct fsck_data *fsckd)
2092{
2093 struct rb_node *this = fsckd->inodes.rb_node;
2094 struct fsck_inode *fscki;
2095
2096 while (this) {
2097 if (this->rb_left)
2098 this = this->rb_left;
2099 else if (this->rb_right)
2100 this = this->rb_right;
2101 else {
2102 fscki = rb_entry(this, struct fsck_inode, rb);
2103 this = rb_parent(this);
2104 if (this) {
2105 if (this->rb_left == &fscki->rb)
2106 this->rb_left = NULL;
2107 else
2108 this->rb_right = NULL;
2109 }
2110 kfree(fscki);
2111 }
2112 }
2113}
2114
2115/**
2116 * check_inodes - checks all inodes.
2117 * @c: UBIFS file-system description object
2118 * @fsckd: FS checking information
2119 *
2120 * This is a helper function for 'dbg_check_filesystem()' which walks the
2121 * RB-tree of inodes after the index scan has been finished, and checks that
2122 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
2123 * %-EINVAL if not, and a negative error code in case of failure.
2124 */
2125static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
2126{
2127 int n, err;
2128 union ubifs_key key;
2129 struct ubifs_znode *znode;
2130 struct ubifs_zbranch *zbr;
2131 struct ubifs_ino_node *ino;
2132 struct fsck_inode *fscki;
2133 struct rb_node *this = rb_first(&fsckd->inodes);
2134
2135 while (this) {
2136 fscki = rb_entry(this, struct fsck_inode, rb);
2137 this = rb_next(this);
2138
2139 if (S_ISDIR(fscki->mode)) {
2140 /*
2141 * Directories have to have exactly one reference (they
2142 * cannot have hardlinks), although root inode is an
2143 * exception.
2144 */
2145 if (fscki->inum != UBIFS_ROOT_INO &&
2146 fscki->references != 1) {
2147 ubifs_err("directory inode %lu has %d "
2148 "direntries which refer it, but "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002149 "should be 1",
2150 (unsigned long)fscki->inum,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002151 fscki->references);
2152 goto out_dump;
2153 }
2154 if (fscki->inum == UBIFS_ROOT_INO &&
2155 fscki->references != 0) {
2156 ubifs_err("root inode %lu has non-zero (%d) "
2157 "direntries which refer it",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002158 (unsigned long)fscki->inum,
2159 fscki->references);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002160 goto out_dump;
2161 }
2162 if (fscki->calc_sz != fscki->size) {
2163 ubifs_err("directory inode %lu size is %lld, "
2164 "but calculated size is %lld",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002165 (unsigned long)fscki->inum,
2166 fscki->size, fscki->calc_sz);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002167 goto out_dump;
2168 }
2169 if (fscki->calc_cnt != fscki->nlink) {
2170 ubifs_err("directory inode %lu nlink is %d, "
2171 "but calculated nlink is %d",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002172 (unsigned long)fscki->inum,
2173 fscki->nlink, fscki->calc_cnt);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002174 goto out_dump;
2175 }
2176 } else {
2177 if (fscki->references != fscki->nlink) {
2178 ubifs_err("inode %lu nlink is %d, but "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002179 "calculated nlink is %d",
2180 (unsigned long)fscki->inum,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002181 fscki->nlink, fscki->references);
2182 goto out_dump;
2183 }
2184 }
2185 if (fscki->xattr_sz != fscki->calc_xsz) {
2186 ubifs_err("inode %lu has xattr size %u, but "
2187 "calculated size is %lld",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002188 (unsigned long)fscki->inum, fscki->xattr_sz,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002189 fscki->calc_xsz);
2190 goto out_dump;
2191 }
2192 if (fscki->xattr_cnt != fscki->calc_xcnt) {
2193 ubifs_err("inode %lu has %u xattrs, but "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002194 "calculated count is %lld",
2195 (unsigned long)fscki->inum,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002196 fscki->xattr_cnt, fscki->calc_xcnt);
2197 goto out_dump;
2198 }
2199 if (fscki->xattr_nms != fscki->calc_xnms) {
2200 ubifs_err("inode %lu has xattr names' size %u, but "
2201 "calculated names' size is %lld",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002202 (unsigned long)fscki->inum, fscki->xattr_nms,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002203 fscki->calc_xnms);
2204 goto out_dump;
2205 }
2206 }
2207
2208 return 0;
2209
2210out_dump:
2211 /* Read the bad inode and dump it */
2212 ino_key_init(c, &key, fscki->inum);
2213 err = ubifs_lookup_level0(c, &key, &znode, &n);
2214 if (!err) {
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002215 ubifs_err("inode %lu not found in index",
2216 (unsigned long)fscki->inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002217 return -ENOENT;
2218 } else if (err < 0) {
2219 ubifs_err("error %d while looking up inode %lu",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002220 err, (unsigned long)fscki->inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002221 return err;
2222 }
2223
2224 zbr = &znode->zbranch[n];
2225 ino = kmalloc(zbr->len, GFP_NOFS);
2226 if (!ino)
2227 return -ENOMEM;
2228
2229 err = ubifs_tnc_read_node(c, zbr, ino);
2230 if (err) {
2231 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2232 zbr->lnum, zbr->offs, err);
2233 kfree(ino);
2234 return err;
2235 }
2236
2237 ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002238 (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002239 dbg_dump_node(c, ino);
2240 kfree(ino);
2241 return -EINVAL;
2242}
2243
2244/**
2245 * dbg_check_filesystem - check the file-system.
2246 * @c: UBIFS file-system description object
2247 *
2248 * This function checks the file system, namely:
2249 * o makes sure that all leaf nodes exist and their CRCs are correct;
2250 * o makes sure inode nlink, size, xattr size/count are correct (for all
2251 * inodes).
2252 *
2253 * The function reads whole indexing tree and all nodes, so it is pretty
2254 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2255 * not, and a negative error code in case of failure.
2256 */
2257int dbg_check_filesystem(struct ubifs_info *c)
2258{
2259 int err;
2260 struct fsck_data fsckd;
2261
2262 if (!(ubifs_chk_flags & UBIFS_CHK_FS))
2263 return 0;
2264
2265 fsckd.inodes = RB_ROOT;
2266 err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
2267 if (err)
2268 goto out_free;
2269
2270 err = check_inodes(c, &fsckd);
2271 if (err)
2272 goto out_free;
2273
2274 free_inodes(&fsckd);
2275 return 0;
2276
2277out_free:
2278 ubifs_err("file-system check failed with error %d", err);
2279 dump_stack();
2280 free_inodes(&fsckd);
2281 return err;
2282}
2283
Artem Bityutskiy3bb66b42010-08-07 10:06:11 +03002284/**
2285 * dbg_check_data_nodes_order - check that list of data nodes is sorted.
2286 * @c: UBIFS file-system description object
2287 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2288 *
2289 * This function returns zero if the list of data nodes is sorted correctly,
2290 * and %-EINVAL if not.
2291 */
2292int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head)
2293{
2294 struct list_head *cur;
2295 struct ubifs_scan_node *sa, *sb;
2296
2297 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
2298 return 0;
2299
2300 for (cur = head->next; cur->next != head; cur = cur->next) {
2301 ino_t inuma, inumb;
2302 uint32_t blka, blkb;
2303
2304 cond_resched();
2305 sa = container_of(cur, struct ubifs_scan_node, list);
2306 sb = container_of(cur->next, struct ubifs_scan_node, list);
2307
2308 if (sa->type != UBIFS_DATA_NODE) {
2309 ubifs_err("bad node type %d", sa->type);
2310 dbg_dump_node(c, sa->node);
2311 return -EINVAL;
2312 }
2313 if (sb->type != UBIFS_DATA_NODE) {
2314 ubifs_err("bad node type %d", sb->type);
2315 dbg_dump_node(c, sb->node);
2316 return -EINVAL;
2317 }
2318
2319 inuma = key_inum(c, &sa->key);
2320 inumb = key_inum(c, &sb->key);
2321
2322 if (inuma < inumb)
2323 continue;
2324 if (inuma > inumb) {
2325 ubifs_err("larger inum %lu goes before inum %lu",
2326 (unsigned long)inuma, (unsigned long)inumb);
2327 goto error_dump;
2328 }
2329
2330 blka = key_block(c, &sa->key);
2331 blkb = key_block(c, &sb->key);
2332
2333 if (blka > blkb) {
2334 ubifs_err("larger block %u goes before %u", blka, blkb);
2335 goto error_dump;
2336 }
2337 if (blka == blkb) {
2338 ubifs_err("two data nodes for the same block");
2339 goto error_dump;
2340 }
2341 }
2342
2343 return 0;
2344
2345error_dump:
2346 dbg_dump_node(c, sa->node);
2347 dbg_dump_node(c, sb->node);
2348 return -EINVAL;
2349}
2350
2351/**
2352 * dbg_check_nondata_nodes_order - check that list of data nodes is sorted.
2353 * @c: UBIFS file-system description object
2354 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2355 *
2356 * This function returns zero if the list of non-data nodes is sorted correctly,
2357 * and %-EINVAL if not.
2358 */
2359int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head)
2360{
2361 struct list_head *cur;
2362 struct ubifs_scan_node *sa, *sb;
2363
2364 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
2365 return 0;
2366
2367 for (cur = head->next; cur->next != head; cur = cur->next) {
2368 ino_t inuma, inumb;
2369 uint32_t hasha, hashb;
2370
2371 cond_resched();
2372 sa = container_of(cur, struct ubifs_scan_node, list);
2373 sb = container_of(cur->next, struct ubifs_scan_node, list);
2374
2375 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2376 sa->type != UBIFS_XENT_NODE) {
2377 ubifs_err("bad node type %d", sa->type);
2378 dbg_dump_node(c, sa->node);
2379 return -EINVAL;
2380 }
2381 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2382 sa->type != UBIFS_XENT_NODE) {
2383 ubifs_err("bad node type %d", sb->type);
2384 dbg_dump_node(c, sb->node);
2385 return -EINVAL;
2386 }
2387
2388 if (sa->type != UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2389 ubifs_err("non-inode node goes before inode node");
2390 goto error_dump;
2391 }
2392
2393 if (sa->type == UBIFS_INO_NODE && sb->type != UBIFS_INO_NODE)
2394 continue;
2395
2396 if (sa->type == UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2397 /* Inode nodes are sorted in descending size order */
2398 if (sa->len < sb->len) {
2399 ubifs_err("smaller inode node goes first");
2400 goto error_dump;
2401 }
2402 continue;
2403 }
2404
2405 /*
2406 * This is either a dentry or xentry, which should be sorted in
2407 * ascending (parent ino, hash) order.
2408 */
2409 inuma = key_inum(c, &sa->key);
2410 inumb = key_inum(c, &sb->key);
2411
2412 if (inuma < inumb)
2413 continue;
2414 if (inuma > inumb) {
2415 ubifs_err("larger inum %lu goes before inum %lu",
2416 (unsigned long)inuma, (unsigned long)inumb);
2417 goto error_dump;
2418 }
2419
2420 hasha = key_block(c, &sa->key);
2421 hashb = key_block(c, &sb->key);
2422
2423 if (hasha > hashb) {
Artem Bityutskiyc4361572011-03-25 15:27:40 +02002424 ubifs_err("larger hash %u goes before %u",
2425 hasha, hashb);
Artem Bityutskiy3bb66b42010-08-07 10:06:11 +03002426 goto error_dump;
2427 }
2428 }
2429
2430 return 0;
2431
2432error_dump:
2433 ubifs_msg("dumping first node");
2434 dbg_dump_node(c, sa->node);
2435 ubifs_msg("dumping second node");
2436 dbg_dump_node(c, sb->node);
2437 return -EINVAL;
2438 return 0;
2439}
2440
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002441static int invocation_cnt;
2442
2443int dbg_force_in_the_gaps(void)
2444{
2445 if (!dbg_force_in_the_gaps_enabled)
2446 return 0;
2447 /* Force in-the-gaps every 8th commit */
2448 return !((invocation_cnt++) & 0x7);
2449}
2450
2451/* Failure mode for recovery testing */
2452
2453#define chance(n, d) (simple_rand() <= (n) * 32768LL / (d))
2454
2455struct failure_mode_info {
2456 struct list_head list;
2457 struct ubifs_info *c;
2458};
2459
2460static LIST_HEAD(fmi_list);
2461static DEFINE_SPINLOCK(fmi_lock);
2462
2463static unsigned int next;
2464
2465static int simple_rand(void)
2466{
2467 if (next == 0)
2468 next = current->pid;
2469 next = next * 1103515245 + 12345;
2470 return (next >> 16) & 32767;
2471}
2472
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002473static void failure_mode_init(struct ubifs_info *c)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002474{
2475 struct failure_mode_info *fmi;
2476
2477 fmi = kmalloc(sizeof(struct failure_mode_info), GFP_NOFS);
2478 if (!fmi) {
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002479 ubifs_err("Failed to register failure mode - no memory");
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002480 return;
2481 }
2482 fmi->c = c;
2483 spin_lock(&fmi_lock);
2484 list_add_tail(&fmi->list, &fmi_list);
2485 spin_unlock(&fmi_lock);
2486}
2487
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002488static void failure_mode_exit(struct ubifs_info *c)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002489{
2490 struct failure_mode_info *fmi, *tmp;
2491
2492 spin_lock(&fmi_lock);
2493 list_for_each_entry_safe(fmi, tmp, &fmi_list, list)
2494 if (fmi->c == c) {
2495 list_del(&fmi->list);
2496 kfree(fmi);
2497 }
2498 spin_unlock(&fmi_lock);
2499}
2500
2501static struct ubifs_info *dbg_find_info(struct ubi_volume_desc *desc)
2502{
2503 struct failure_mode_info *fmi;
2504
2505 spin_lock(&fmi_lock);
2506 list_for_each_entry(fmi, &fmi_list, list)
2507 if (fmi->c->ubi == desc) {
2508 struct ubifs_info *c = fmi->c;
2509
2510 spin_unlock(&fmi_lock);
2511 return c;
2512 }
2513 spin_unlock(&fmi_lock);
2514 return NULL;
2515}
2516
2517static int in_failure_mode(struct ubi_volume_desc *desc)
2518{
2519 struct ubifs_info *c = dbg_find_info(desc);
2520
2521 if (c && dbg_failure_mode)
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002522 return c->dbg->failure_mode;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002523 return 0;
2524}
2525
2526static int do_fail(struct ubi_volume_desc *desc, int lnum, int write)
2527{
2528 struct ubifs_info *c = dbg_find_info(desc);
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002529 struct ubifs_debug_info *d;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002530
2531 if (!c || !dbg_failure_mode)
2532 return 0;
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002533 d = c->dbg;
2534 if (d->failure_mode)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002535 return 1;
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002536 if (!d->fail_cnt) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002537 /* First call - decide delay to failure */
2538 if (chance(1, 2)) {
2539 unsigned int delay = 1 << (simple_rand() >> 11);
2540
2541 if (chance(1, 2)) {
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002542 d->fail_delay = 1;
2543 d->fail_timeout = jiffies +
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002544 msecs_to_jiffies(delay);
2545 dbg_rcvry("failing after %ums", delay);
2546 } else {
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002547 d->fail_delay = 2;
2548 d->fail_cnt_max = delay;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002549 dbg_rcvry("failing after %u calls", delay);
2550 }
2551 }
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002552 d->fail_cnt += 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002553 }
2554 /* Determine if failure delay has expired */
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002555 if (d->fail_delay == 1) {
2556 if (time_before(jiffies, d->fail_timeout))
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002557 return 0;
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002558 } else if (d->fail_delay == 2)
2559 if (d->fail_cnt++ < d->fail_cnt_max)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002560 return 0;
2561 if (lnum == UBIFS_SB_LNUM) {
2562 if (write) {
2563 if (chance(1, 2))
2564 return 0;
2565 } else if (chance(19, 20))
2566 return 0;
2567 dbg_rcvry("failing in super block LEB %d", lnum);
2568 } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2569 if (chance(19, 20))
2570 return 0;
2571 dbg_rcvry("failing in master LEB %d", lnum);
2572 } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2573 if (write) {
2574 if (chance(99, 100))
2575 return 0;
2576 } else if (chance(399, 400))
2577 return 0;
2578 dbg_rcvry("failing in log LEB %d", lnum);
2579 } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2580 if (write) {
2581 if (chance(7, 8))
2582 return 0;
2583 } else if (chance(19, 20))
2584 return 0;
2585 dbg_rcvry("failing in LPT LEB %d", lnum);
2586 } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2587 if (write) {
2588 if (chance(1, 2))
2589 return 0;
2590 } else if (chance(9, 10))
2591 return 0;
2592 dbg_rcvry("failing in orphan LEB %d", lnum);
2593 } else if (lnum == c->ihead_lnum) {
2594 if (chance(99, 100))
2595 return 0;
2596 dbg_rcvry("failing in index head LEB %d", lnum);
2597 } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2598 if (chance(9, 10))
2599 return 0;
2600 dbg_rcvry("failing in GC head LEB %d", lnum);
2601 } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
2602 !ubifs_search_bud(c, lnum)) {
2603 if (chance(19, 20))
2604 return 0;
2605 dbg_rcvry("failing in non-bud LEB %d", lnum);
2606 } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
2607 c->cmt_state == COMMIT_RUNNING_REQUIRED) {
2608 if (chance(999, 1000))
2609 return 0;
2610 dbg_rcvry("failing in bud LEB %d commit running", lnum);
2611 } else {
2612 if (chance(9999, 10000))
2613 return 0;
2614 dbg_rcvry("failing in bud LEB %d commit not running", lnum);
2615 }
2616 ubifs_err("*** SETTING FAILURE MODE ON (LEB %d) ***", lnum);
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002617 d->failure_mode = 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002618 dump_stack();
2619 return 1;
2620}
2621
2622static void cut_data(const void *buf, int len)
2623{
2624 int flen, i;
2625 unsigned char *p = (void *)buf;
2626
2627 flen = (len * (long long)simple_rand()) >> 15;
2628 for (i = flen; i < len; i++)
2629 p[i] = 0xff;
2630}
2631
2632int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
2633 int len, int check)
2634{
2635 if (in_failure_mode(desc))
2636 return -EIO;
2637 return ubi_leb_read(desc, lnum, buf, offset, len, check);
2638}
2639
2640int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
2641 int offset, int len, int dtype)
2642{
Adrian Hunter16dfd802008-07-18 16:47:41 +03002643 int err, failing;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002644
2645 if (in_failure_mode(desc))
2646 return -EIO;
Adrian Hunter16dfd802008-07-18 16:47:41 +03002647 failing = do_fail(desc, lnum, 1);
2648 if (failing)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002649 cut_data(buf, len);
2650 err = ubi_leb_write(desc, lnum, buf, offset, len, dtype);
2651 if (err)
2652 return err;
Adrian Hunter16dfd802008-07-18 16:47:41 +03002653 if (failing)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002654 return -EIO;
2655 return 0;
2656}
2657
2658int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
2659 int len, int dtype)
2660{
2661 int err;
2662
2663 if (do_fail(desc, lnum, 1))
2664 return -EIO;
2665 err = ubi_leb_change(desc, lnum, buf, len, dtype);
2666 if (err)
2667 return err;
2668 if (do_fail(desc, lnum, 1))
2669 return -EIO;
2670 return 0;
2671}
2672
2673int dbg_leb_erase(struct ubi_volume_desc *desc, int lnum)
2674{
2675 int err;
2676
2677 if (do_fail(desc, lnum, 0))
2678 return -EIO;
2679 err = ubi_leb_erase(desc, lnum);
2680 if (err)
2681 return err;
2682 if (do_fail(desc, lnum, 0))
2683 return -EIO;
2684 return 0;
2685}
2686
2687int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum)
2688{
2689 int err;
2690
2691 if (do_fail(desc, lnum, 0))
2692 return -EIO;
2693 err = ubi_leb_unmap(desc, lnum);
2694 if (err)
2695 return err;
2696 if (do_fail(desc, lnum, 0))
2697 return -EIO;
2698 return 0;
2699}
2700
2701int dbg_is_mapped(struct ubi_volume_desc *desc, int lnum)
2702{
2703 if (in_failure_mode(desc))
2704 return -EIO;
2705 return ubi_is_mapped(desc, lnum);
2706}
2707
2708int dbg_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
2709{
2710 int err;
2711
2712 if (do_fail(desc, lnum, 0))
2713 return -EIO;
2714 err = ubi_leb_map(desc, lnum, dtype);
2715 if (err)
2716 return err;
2717 if (do_fail(desc, lnum, 0))
2718 return -EIO;
2719 return 0;
2720}
2721
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002722/**
2723 * ubifs_debugging_init - initialize UBIFS debugging.
2724 * @c: UBIFS file-system description object
2725 *
2726 * This function initializes debugging-related data for the file system.
2727 * Returns zero in case of success and a negative error code in case of
2728 * failure.
2729 */
2730int ubifs_debugging_init(struct ubifs_info *c)
2731{
2732 c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
2733 if (!c->dbg)
2734 return -ENOMEM;
2735
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002736 failure_mode_init(c);
2737 return 0;
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002738}
2739
2740/**
2741 * ubifs_debugging_exit - free debugging data.
2742 * @c: UBIFS file-system description object
2743 */
2744void ubifs_debugging_exit(struct ubifs_info *c)
2745{
2746 failure_mode_exit(c);
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002747 kfree(c->dbg);
2748}
2749
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002750/*
2751 * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
2752 * contain the stuff specific to particular file-system mounts.
2753 */
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002754static struct dentry *dfs_rootdir;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002755
2756/**
2757 * dbg_debugfs_init - initialize debugfs file-system.
2758 *
2759 * UBIFS uses debugfs file-system to expose various debugging knobs to
2760 * user-space. This function creates "ubifs" directory in the debugfs
2761 * file-system. Returns zero in case of success and a negative error code in
2762 * case of failure.
2763 */
2764int dbg_debugfs_init(void)
2765{
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002766 dfs_rootdir = debugfs_create_dir("ubifs", NULL);
2767 if (IS_ERR(dfs_rootdir)) {
2768 int err = PTR_ERR(dfs_rootdir);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002769 ubifs_err("cannot create \"ubifs\" debugfs directory, "
2770 "error %d\n", err);
2771 return err;
2772 }
2773
2774 return 0;
2775}
2776
2777/**
2778 * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
2779 */
2780void dbg_debugfs_exit(void)
2781{
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002782 debugfs_remove(dfs_rootdir);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002783}
2784
2785static int open_debugfs_file(struct inode *inode, struct file *file)
2786{
2787 file->private_data = inode->i_private;
Artem Bityutskiy1bbfc842011-03-21 16:26:42 +02002788 return nonseekable_open(inode, file);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002789}
2790
2791static ssize_t write_debugfs_file(struct file *file, const char __user *buf,
2792 size_t count, loff_t *ppos)
2793{
2794 struct ubifs_info *c = file->private_data;
2795 struct ubifs_debug_info *d = c->dbg;
2796
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002797 if (file->f_path.dentry == d->dfs_dump_lprops)
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002798 dbg_dump_lprops(c);
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002799 else if (file->f_path.dentry == d->dfs_dump_budg) {
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002800 spin_lock(&c->space_lock);
2801 dbg_dump_budg(c);
2802 spin_unlock(&c->space_lock);
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002803 } else if (file->f_path.dentry == d->dfs_dump_tnc) {
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002804 mutex_lock(&c->tnc_mutex);
2805 dbg_dump_tnc(c);
2806 mutex_unlock(&c->tnc_mutex);
2807 } else
2808 return -EINVAL;
2809
2810 *ppos += count;
2811 return count;
2812}
2813
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002814static const struct file_operations dfs_fops = {
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002815 .open = open_debugfs_file,
2816 .write = write_debugfs_file,
2817 .owner = THIS_MODULE,
Artem Bityutskiy1bbfc842011-03-21 16:26:42 +02002818 .llseek = no_llseek,
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002819};
2820
2821/**
2822 * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
2823 * @c: UBIFS file-system description object
2824 *
2825 * This function creates all debugfs files for this instance of UBIFS. Returns
2826 * zero in case of success and a negative error code in case of failure.
2827 *
2828 * Note, the only reason we have not merged this function with the
2829 * 'ubifs_debugging_init()' function is because it is better to initialize
2830 * debugfs interfaces at the very end of the mount process, and remove them at
2831 * the very beginning of the mount process.
2832 */
2833int dbg_debugfs_init_fs(struct ubifs_info *c)
2834{
2835 int err;
2836 const char *fname;
2837 struct dentry *dent;
2838 struct ubifs_debug_info *d = c->dbg;
2839
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002840 sprintf(d->dfs_dir_name, "ubi%d_%d", c->vi.ubi_num, c->vi.vol_id);
Artem Bityutskiycc6a86b2011-04-01 10:10:52 +03002841 fname = d->dfs_dir_name;
2842 dent = debugfs_create_dir(fname, dfs_rootdir);
Artem Bityutskiy95169532011-04-01 10:16:17 +03002843 if (IS_ERR_OR_NULL(dent))
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002844 goto out;
Artem Bityutskiycc6a86b2011-04-01 10:10:52 +03002845 d->dfs_dir = dent;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002846
2847 fname = "dump_lprops";
Vasiliy Kulikov8c559d32011-02-04 15:24:19 +03002848 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
Artem Bityutskiy95169532011-04-01 10:16:17 +03002849 if (IS_ERR_OR_NULL(dent))
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002850 goto out_remove;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002851 d->dfs_dump_lprops = dent;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002852
2853 fname = "dump_budg";
Vasiliy Kulikov8c559d32011-02-04 15:24:19 +03002854 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
Artem Bityutskiy95169532011-04-01 10:16:17 +03002855 if (IS_ERR_OR_NULL(dent))
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002856 goto out_remove;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002857 d->dfs_dump_budg = dent;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002858
2859 fname = "dump_tnc";
Vasiliy Kulikov8c559d32011-02-04 15:24:19 +03002860 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
Artem Bityutskiy95169532011-04-01 10:16:17 +03002861 if (IS_ERR_OR_NULL(dent))
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002862 goto out_remove;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002863 d->dfs_dump_tnc = dent;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002864
2865 return 0;
2866
2867out_remove:
Artem Bityutskiycc6a86b2011-04-01 10:10:52 +03002868 debugfs_remove_recursive(d->dfs_dir);
2869out:
Artem Bityutskiy95169532011-04-01 10:16:17 +03002870 err = dent ? PTR_ERR(dent) : -ENODEV;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002871 ubifs_err("cannot create \"%s\" debugfs directory, error %d\n",
2872 fname, err);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002873 return err;
2874}
2875
2876/**
2877 * dbg_debugfs_exit_fs - remove all debugfs files.
2878 * @c: UBIFS file-system description object
2879 */
2880void dbg_debugfs_exit_fs(struct ubifs_info *c)
2881{
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002882 debugfs_remove_recursive(c->dbg->dfs_dir);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002883}
2884
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002885#endif /* CONFIG_UBIFS_FS_DEBUG */