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