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