blob: e975bd82f38b8d31d6a4ae4b25a587b6c5494318 [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
213static void dump_ch(const struct ubifs_ch *ch)
214{
215 printk(KERN_DEBUG "\tmagic %#x\n", le32_to_cpu(ch->magic));
216 printk(KERN_DEBUG "\tcrc %#x\n", le32_to_cpu(ch->crc));
217 printk(KERN_DEBUG "\tnode_type %d (%s)\n", ch->node_type,
218 dbg_ntype(ch->node_type));
219 printk(KERN_DEBUG "\tgroup_type %d (%s)\n", ch->group_type,
220 dbg_gtype(ch->group_type));
221 printk(KERN_DEBUG "\tsqnum %llu\n",
222 (unsigned long long)le64_to_cpu(ch->sqnum));
223 printk(KERN_DEBUG "\tlen %u\n", le32_to_cpu(ch->len));
224}
225
226void dbg_dump_inode(const struct ubifs_info *c, const struct inode *inode)
227{
228 const struct ubifs_inode *ui = ubifs_inode(inode);
229
Artem Bityutskiyb5e426e2008-09-09 11:20:35 +0300230 printk(KERN_DEBUG "Dump in-memory inode:");
231 printk(KERN_DEBUG "\tinode %lu\n", inode->i_ino);
232 printk(KERN_DEBUG "\tsize %llu\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300233 (unsigned long long)i_size_read(inode));
Artem Bityutskiyb5e426e2008-09-09 11:20:35 +0300234 printk(KERN_DEBUG "\tnlink %u\n", inode->i_nlink);
235 printk(KERN_DEBUG "\tuid %u\n", (unsigned int)inode->i_uid);
236 printk(KERN_DEBUG "\tgid %u\n", (unsigned int)inode->i_gid);
237 printk(KERN_DEBUG "\tatime %u.%u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300238 (unsigned int)inode->i_atime.tv_sec,
239 (unsigned int)inode->i_atime.tv_nsec);
Artem Bityutskiyb5e426e2008-09-09 11:20:35 +0300240 printk(KERN_DEBUG "\tmtime %u.%u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300241 (unsigned int)inode->i_mtime.tv_sec,
242 (unsigned int)inode->i_mtime.tv_nsec);
Artem Bityutskiyb5e426e2008-09-09 11:20:35 +0300243 printk(KERN_DEBUG "\tctime %u.%u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300244 (unsigned int)inode->i_ctime.tv_sec,
245 (unsigned int)inode->i_ctime.tv_nsec);
Artem Bityutskiyb5e426e2008-09-09 11:20:35 +0300246 printk(KERN_DEBUG "\tcreat_sqnum %llu\n", ui->creat_sqnum);
247 printk(KERN_DEBUG "\txattr_size %u\n", ui->xattr_size);
248 printk(KERN_DEBUG "\txattr_cnt %u\n", ui->xattr_cnt);
249 printk(KERN_DEBUG "\txattr_names %u\n", ui->xattr_names);
250 printk(KERN_DEBUG "\tdirty %u\n", ui->dirty);
251 printk(KERN_DEBUG "\txattr %u\n", ui->xattr);
252 printk(KERN_DEBUG "\tbulk_read %u\n", ui->xattr);
253 printk(KERN_DEBUG "\tsynced_i_size %llu\n",
254 (unsigned long long)ui->synced_i_size);
255 printk(KERN_DEBUG "\tui_size %llu\n",
256 (unsigned long long)ui->ui_size);
257 printk(KERN_DEBUG "\tflags %d\n", ui->flags);
258 printk(KERN_DEBUG "\tcompr_type %d\n", ui->compr_type);
259 printk(KERN_DEBUG "\tlast_page_read %lu\n", ui->last_page_read);
260 printk(KERN_DEBUG "\tread_in_a_row %lu\n", ui->read_in_a_row);
261 printk(KERN_DEBUG "\tdata_len %d\n", ui->data_len);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300262}
263
264void dbg_dump_node(const struct ubifs_info *c, const void *node)
265{
266 int i, n;
267 union ubifs_key key;
268 const struct ubifs_ch *ch = node;
269
270 if (dbg_failure_mode)
271 return;
272
273 /* If the magic is incorrect, just hexdump the first bytes */
274 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
275 printk(KERN_DEBUG "Not a node, first %zu bytes:", UBIFS_CH_SZ);
276 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
277 (void *)node, UBIFS_CH_SZ, 1);
278 return;
279 }
280
281 spin_lock(&dbg_lock);
282 dump_ch(node);
283
284 switch (ch->node_type) {
285 case UBIFS_PAD_NODE:
286 {
287 const struct ubifs_pad_node *pad = node;
288
289 printk(KERN_DEBUG "\tpad_len %u\n",
290 le32_to_cpu(pad->pad_len));
291 break;
292 }
293 case UBIFS_SB_NODE:
294 {
295 const struct ubifs_sb_node *sup = node;
296 unsigned int sup_flags = le32_to_cpu(sup->flags);
297
298 printk(KERN_DEBUG "\tkey_hash %d (%s)\n",
299 (int)sup->key_hash, get_key_hash(sup->key_hash));
300 printk(KERN_DEBUG "\tkey_fmt %d (%s)\n",
301 (int)sup->key_fmt, get_key_fmt(sup->key_fmt));
302 printk(KERN_DEBUG "\tflags %#x\n", sup_flags);
303 printk(KERN_DEBUG "\t big_lpt %u\n",
304 !!(sup_flags & UBIFS_FLG_BIGLPT));
305 printk(KERN_DEBUG "\tmin_io_size %u\n",
306 le32_to_cpu(sup->min_io_size));
307 printk(KERN_DEBUG "\tleb_size %u\n",
308 le32_to_cpu(sup->leb_size));
309 printk(KERN_DEBUG "\tleb_cnt %u\n",
310 le32_to_cpu(sup->leb_cnt));
311 printk(KERN_DEBUG "\tmax_leb_cnt %u\n",
312 le32_to_cpu(sup->max_leb_cnt));
313 printk(KERN_DEBUG "\tmax_bud_bytes %llu\n",
314 (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
315 printk(KERN_DEBUG "\tlog_lebs %u\n",
316 le32_to_cpu(sup->log_lebs));
317 printk(KERN_DEBUG "\tlpt_lebs %u\n",
318 le32_to_cpu(sup->lpt_lebs));
319 printk(KERN_DEBUG "\torph_lebs %u\n",
320 le32_to_cpu(sup->orph_lebs));
321 printk(KERN_DEBUG "\tjhead_cnt %u\n",
322 le32_to_cpu(sup->jhead_cnt));
323 printk(KERN_DEBUG "\tfanout %u\n",
324 le32_to_cpu(sup->fanout));
325 printk(KERN_DEBUG "\tlsave_cnt %u\n",
326 le32_to_cpu(sup->lsave_cnt));
327 printk(KERN_DEBUG "\tdefault_compr %u\n",
328 (int)le16_to_cpu(sup->default_compr));
329 printk(KERN_DEBUG "\trp_size %llu\n",
330 (unsigned long long)le64_to_cpu(sup->rp_size));
331 printk(KERN_DEBUG "\trp_uid %u\n",
332 le32_to_cpu(sup->rp_uid));
333 printk(KERN_DEBUG "\trp_gid %u\n",
334 le32_to_cpu(sup->rp_gid));
335 printk(KERN_DEBUG "\tfmt_version %u\n",
336 le32_to_cpu(sup->fmt_version));
337 printk(KERN_DEBUG "\ttime_gran %u\n",
338 le32_to_cpu(sup->time_gran));
339 printk(KERN_DEBUG "\tUUID %02X%02X%02X%02X-%02X%02X"
340 "-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
341 sup->uuid[0], sup->uuid[1], sup->uuid[2], sup->uuid[3],
342 sup->uuid[4], sup->uuid[5], sup->uuid[6], sup->uuid[7],
343 sup->uuid[8], sup->uuid[9], sup->uuid[10], sup->uuid[11],
344 sup->uuid[12], sup->uuid[13], sup->uuid[14],
345 sup->uuid[15]);
346 break;
347 }
348 case UBIFS_MST_NODE:
349 {
350 const struct ubifs_mst_node *mst = node;
351
352 printk(KERN_DEBUG "\thighest_inum %llu\n",
353 (unsigned long long)le64_to_cpu(mst->highest_inum));
354 printk(KERN_DEBUG "\tcommit number %llu\n",
355 (unsigned long long)le64_to_cpu(mst->cmt_no));
356 printk(KERN_DEBUG "\tflags %#x\n",
357 le32_to_cpu(mst->flags));
358 printk(KERN_DEBUG "\tlog_lnum %u\n",
359 le32_to_cpu(mst->log_lnum));
360 printk(KERN_DEBUG "\troot_lnum %u\n",
361 le32_to_cpu(mst->root_lnum));
362 printk(KERN_DEBUG "\troot_offs %u\n",
363 le32_to_cpu(mst->root_offs));
364 printk(KERN_DEBUG "\troot_len %u\n",
365 le32_to_cpu(mst->root_len));
366 printk(KERN_DEBUG "\tgc_lnum %u\n",
367 le32_to_cpu(mst->gc_lnum));
368 printk(KERN_DEBUG "\tihead_lnum %u\n",
369 le32_to_cpu(mst->ihead_lnum));
370 printk(KERN_DEBUG "\tihead_offs %u\n",
371 le32_to_cpu(mst->ihead_offs));
Harvey Harrison0ecb9522008-10-24 10:52:57 -0700372 printk(KERN_DEBUG "\tindex_size %llu\n",
373 (unsigned long long)le64_to_cpu(mst->index_size));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300374 printk(KERN_DEBUG "\tlpt_lnum %u\n",
375 le32_to_cpu(mst->lpt_lnum));
376 printk(KERN_DEBUG "\tlpt_offs %u\n",
377 le32_to_cpu(mst->lpt_offs));
378 printk(KERN_DEBUG "\tnhead_lnum %u\n",
379 le32_to_cpu(mst->nhead_lnum));
380 printk(KERN_DEBUG "\tnhead_offs %u\n",
381 le32_to_cpu(mst->nhead_offs));
382 printk(KERN_DEBUG "\tltab_lnum %u\n",
383 le32_to_cpu(mst->ltab_lnum));
384 printk(KERN_DEBUG "\tltab_offs %u\n",
385 le32_to_cpu(mst->ltab_offs));
386 printk(KERN_DEBUG "\tlsave_lnum %u\n",
387 le32_to_cpu(mst->lsave_lnum));
388 printk(KERN_DEBUG "\tlsave_offs %u\n",
389 le32_to_cpu(mst->lsave_offs));
390 printk(KERN_DEBUG "\tlscan_lnum %u\n",
391 le32_to_cpu(mst->lscan_lnum));
392 printk(KERN_DEBUG "\tleb_cnt %u\n",
393 le32_to_cpu(mst->leb_cnt));
394 printk(KERN_DEBUG "\tempty_lebs %u\n",
395 le32_to_cpu(mst->empty_lebs));
396 printk(KERN_DEBUG "\tidx_lebs %u\n",
397 le32_to_cpu(mst->idx_lebs));
398 printk(KERN_DEBUG "\ttotal_free %llu\n",
399 (unsigned long long)le64_to_cpu(mst->total_free));
400 printk(KERN_DEBUG "\ttotal_dirty %llu\n",
401 (unsigned long long)le64_to_cpu(mst->total_dirty));
402 printk(KERN_DEBUG "\ttotal_used %llu\n",
403 (unsigned long long)le64_to_cpu(mst->total_used));
404 printk(KERN_DEBUG "\ttotal_dead %llu\n",
405 (unsigned long long)le64_to_cpu(mst->total_dead));
406 printk(KERN_DEBUG "\ttotal_dark %llu\n",
407 (unsigned long long)le64_to_cpu(mst->total_dark));
408 break;
409 }
410 case UBIFS_REF_NODE:
411 {
412 const struct ubifs_ref_node *ref = node;
413
414 printk(KERN_DEBUG "\tlnum %u\n",
415 le32_to_cpu(ref->lnum));
416 printk(KERN_DEBUG "\toffs %u\n",
417 le32_to_cpu(ref->offs));
418 printk(KERN_DEBUG "\tjhead %u\n",
419 le32_to_cpu(ref->jhead));
420 break;
421 }
422 case UBIFS_INO_NODE:
423 {
424 const struct ubifs_ino_node *ino = node;
425
426 key_read(c, &ino->key, &key);
427 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
428 printk(KERN_DEBUG "\tcreat_sqnum %llu\n",
429 (unsigned long long)le64_to_cpu(ino->creat_sqnum));
430 printk(KERN_DEBUG "\tsize %llu\n",
431 (unsigned long long)le64_to_cpu(ino->size));
432 printk(KERN_DEBUG "\tnlink %u\n",
433 le32_to_cpu(ino->nlink));
434 printk(KERN_DEBUG "\tatime %lld.%u\n",
435 (long long)le64_to_cpu(ino->atime_sec),
436 le32_to_cpu(ino->atime_nsec));
437 printk(KERN_DEBUG "\tmtime %lld.%u\n",
438 (long long)le64_to_cpu(ino->mtime_sec),
439 le32_to_cpu(ino->mtime_nsec));
440 printk(KERN_DEBUG "\tctime %lld.%u\n",
441 (long long)le64_to_cpu(ino->ctime_sec),
442 le32_to_cpu(ino->ctime_nsec));
443 printk(KERN_DEBUG "\tuid %u\n",
444 le32_to_cpu(ino->uid));
445 printk(KERN_DEBUG "\tgid %u\n",
446 le32_to_cpu(ino->gid));
447 printk(KERN_DEBUG "\tmode %u\n",
448 le32_to_cpu(ino->mode));
449 printk(KERN_DEBUG "\tflags %#x\n",
450 le32_to_cpu(ino->flags));
451 printk(KERN_DEBUG "\txattr_cnt %u\n",
452 le32_to_cpu(ino->xattr_cnt));
453 printk(KERN_DEBUG "\txattr_size %u\n",
454 le32_to_cpu(ino->xattr_size));
455 printk(KERN_DEBUG "\txattr_names %u\n",
456 le32_to_cpu(ino->xattr_names));
457 printk(KERN_DEBUG "\tcompr_type %#x\n",
458 (int)le16_to_cpu(ino->compr_type));
459 printk(KERN_DEBUG "\tdata len %u\n",
460 le32_to_cpu(ino->data_len));
461 break;
462 }
463 case UBIFS_DENT_NODE:
464 case UBIFS_XENT_NODE:
465 {
466 const struct ubifs_dent_node *dent = node;
467 int nlen = le16_to_cpu(dent->nlen);
468
469 key_read(c, &dent->key, &key);
470 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
471 printk(KERN_DEBUG "\tinum %llu\n",
472 (unsigned long long)le64_to_cpu(dent->inum));
473 printk(KERN_DEBUG "\ttype %d\n", (int)dent->type);
474 printk(KERN_DEBUG "\tnlen %d\n", nlen);
475 printk(KERN_DEBUG "\tname ");
476
477 if (nlen > UBIFS_MAX_NLEN)
478 printk(KERN_DEBUG "(bad name length, not printing, "
479 "bad or corrupted node)");
480 else {
481 for (i = 0; i < nlen && dent->name[i]; i++)
482 printk("%c", dent->name[i]);
483 }
484 printk("\n");
485
486 break;
487 }
488 case UBIFS_DATA_NODE:
489 {
490 const struct ubifs_data_node *dn = node;
491 int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
492
493 key_read(c, &dn->key, &key);
494 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
495 printk(KERN_DEBUG "\tsize %u\n",
496 le32_to_cpu(dn->size));
497 printk(KERN_DEBUG "\tcompr_typ %d\n",
498 (int)le16_to_cpu(dn->compr_type));
499 printk(KERN_DEBUG "\tdata size %d\n",
500 dlen);
501 printk(KERN_DEBUG "\tdata:\n");
502 print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET, 32, 1,
503 (void *)&dn->data, dlen, 0);
504 break;
505 }
506 case UBIFS_TRUN_NODE:
507 {
508 const struct ubifs_trun_node *trun = node;
509
510 printk(KERN_DEBUG "\tinum %u\n",
511 le32_to_cpu(trun->inum));
512 printk(KERN_DEBUG "\told_size %llu\n",
513 (unsigned long long)le64_to_cpu(trun->old_size));
514 printk(KERN_DEBUG "\tnew_size %llu\n",
515 (unsigned long long)le64_to_cpu(trun->new_size));
516 break;
517 }
518 case UBIFS_IDX_NODE:
519 {
520 const struct ubifs_idx_node *idx = node;
521
522 n = le16_to_cpu(idx->child_cnt);
523 printk(KERN_DEBUG "\tchild_cnt %d\n", n);
524 printk(KERN_DEBUG "\tlevel %d\n",
525 (int)le16_to_cpu(idx->level));
526 printk(KERN_DEBUG "\tBranches:\n");
527
528 for (i = 0; i < n && i < c->fanout - 1; i++) {
529 const struct ubifs_branch *br;
530
531 br = ubifs_idx_branch(c, idx, i);
532 key_read(c, &br->key, &key);
533 printk(KERN_DEBUG "\t%d: LEB %d:%d len %d key %s\n",
534 i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
535 le32_to_cpu(br->len), DBGKEY(&key));
536 }
537 break;
538 }
539 case UBIFS_CS_NODE:
540 break;
541 case UBIFS_ORPH_NODE:
542 {
543 const struct ubifs_orph_node *orph = node;
544
545 printk(KERN_DEBUG "\tcommit number %llu\n",
546 (unsigned long long)
547 le64_to_cpu(orph->cmt_no) & LLONG_MAX);
548 printk(KERN_DEBUG "\tlast node flag %llu\n",
549 (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
550 n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
551 printk(KERN_DEBUG "\t%d orphan inode numbers:\n", n);
552 for (i = 0; i < n; i++)
553 printk(KERN_DEBUG "\t ino %llu\n",
Alexander Beregalov7424bac2008-09-17 22:09:41 +0400554 (unsigned long long)le64_to_cpu(orph->inos[i]));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300555 break;
556 }
557 default:
558 printk(KERN_DEBUG "node type %d was not recognized\n",
559 (int)ch->node_type);
560 }
561 spin_unlock(&dbg_lock);
562}
563
564void dbg_dump_budget_req(const struct ubifs_budget_req *req)
565{
566 spin_lock(&dbg_lock);
567 printk(KERN_DEBUG "Budgeting request: new_ino %d, dirtied_ino %d\n",
568 req->new_ino, req->dirtied_ino);
569 printk(KERN_DEBUG "\tnew_ino_d %d, dirtied_ino_d %d\n",
570 req->new_ino_d, req->dirtied_ino_d);
571 printk(KERN_DEBUG "\tnew_page %d, dirtied_page %d\n",
572 req->new_page, req->dirtied_page);
573 printk(KERN_DEBUG "\tnew_dent %d, mod_dent %d\n",
574 req->new_dent, req->mod_dent);
575 printk(KERN_DEBUG "\tidx_growth %d\n", req->idx_growth);
576 printk(KERN_DEBUG "\tdata_growth %d dd_growth %d\n",
577 req->data_growth, req->dd_growth);
578 spin_unlock(&dbg_lock);
579}
580
581void dbg_dump_lstats(const struct ubifs_lp_stats *lst)
582{
583 spin_lock(&dbg_lock);
Artem Bityutskiy1de94152008-07-25 12:58:38 +0300584 printk(KERN_DEBUG "(pid %d) Lprops statistics: empty_lebs %d, "
585 "idx_lebs %d\n", current->pid, lst->empty_lebs, lst->idx_lebs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300586 printk(KERN_DEBUG "\ttaken_empty_lebs %d, total_free %lld, "
587 "total_dirty %lld\n", lst->taken_empty_lebs, lst->total_free,
588 lst->total_dirty);
589 printk(KERN_DEBUG "\ttotal_used %lld, total_dark %lld, "
590 "total_dead %lld\n", lst->total_used, lst->total_dark,
591 lst->total_dead);
592 spin_unlock(&dbg_lock);
593}
594
595void dbg_dump_budg(struct ubifs_info *c)
596{
597 int i;
598 struct rb_node *rb;
599 struct ubifs_bud *bud;
600 struct ubifs_gced_idx_leb *idx_gc;
Artem Bityutskiy21a60252008-12-12 11:13:17 -0500601 long long available, outstanding, free;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300602
Artem Bityutskiy21a60252008-12-12 11:13:17 -0500603 ubifs_assert(spin_is_locked(&c->space_lock));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300604 spin_lock(&dbg_lock);
Artem Bityutskiy1de94152008-07-25 12:58:38 +0300605 printk(KERN_DEBUG "(pid %d) Budgeting info: budg_data_growth %lld, "
606 "budg_dd_growth %lld, budg_idx_growth %lld\n", current->pid,
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300607 c->budg_data_growth, c->budg_dd_growth, c->budg_idx_growth);
608 printk(KERN_DEBUG "\tdata budget sum %lld, total budget sum %lld, "
609 "freeable_cnt %d\n", c->budg_data_growth + c->budg_dd_growth,
610 c->budg_data_growth + c->budg_dd_growth + c->budg_idx_growth,
611 c->freeable_cnt);
612 printk(KERN_DEBUG "\tmin_idx_lebs %d, old_idx_sz %lld, "
613 "calc_idx_sz %lld, idx_gc_cnt %d\n", c->min_idx_lebs,
614 c->old_idx_sz, c->calc_idx_sz, c->idx_gc_cnt);
615 printk(KERN_DEBUG "\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, "
616 "clean_zn_cnt %ld\n", atomic_long_read(&c->dirty_pg_cnt),
617 atomic_long_read(&c->dirty_zn_cnt),
618 atomic_long_read(&c->clean_zn_cnt));
619 printk(KERN_DEBUG "\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
620 c->dark_wm, c->dead_wm, c->max_idx_node_sz);
621 printk(KERN_DEBUG "\tgc_lnum %d, ihead_lnum %d\n",
622 c->gc_lnum, c->ihead_lnum);
Artem Bityutskiy84abf972009-01-23 14:54:59 +0200623 /* If we are in R/O mode, journal heads do not exist */
624 if (c->jheads)
625 for (i = 0; i < c->jhead_cnt; i++)
626 printk(KERN_DEBUG "\tjhead %d\t LEB %d\n",
627 c->jheads[i].wbuf.jhead, c->jheads[i].wbuf.lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300628 for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
629 bud = rb_entry(rb, struct ubifs_bud, rb);
630 printk(KERN_DEBUG "\tbud LEB %d\n", bud->lnum);
631 }
632 list_for_each_entry(bud, &c->old_buds, list)
633 printk(KERN_DEBUG "\told bud LEB %d\n", bud->lnum);
634 list_for_each_entry(idx_gc, &c->idx_gc, list)
635 printk(KERN_DEBUG "\tGC'ed idx LEB %d unmap %d\n",
636 idx_gc->lnum, idx_gc->unmap);
637 printk(KERN_DEBUG "\tcommit state %d\n", c->cmt_state);
Artem Bityutskiy21a60252008-12-12 11:13:17 -0500638
639 /* Print budgeting predictions */
640 available = ubifs_calc_available(c, c->min_idx_lebs);
641 outstanding = c->budg_data_growth + c->budg_dd_growth;
Artem Bityutskiy84abf972009-01-23 14:54:59 +0200642 free = ubifs_get_free_space_nolock(c);
Artem Bityutskiy21a60252008-12-12 11:13:17 -0500643 printk(KERN_DEBUG "Budgeting predictions:\n");
644 printk(KERN_DEBUG "\tavailable: %lld, outstanding %lld, free %lld\n",
645 available, outstanding, free);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300646 spin_unlock(&dbg_lock);
647}
648
649void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
650{
651 printk(KERN_DEBUG "LEB %d lprops: free %d, dirty %d (used %d), "
652 "flags %#x\n", lp->lnum, lp->free, lp->dirty,
653 c->leb_size - lp->free - lp->dirty, lp->flags);
654}
655
656void dbg_dump_lprops(struct ubifs_info *c)
657{
658 int lnum, err;
659 struct ubifs_lprops lp;
660 struct ubifs_lp_stats lst;
661
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200662 printk(KERN_DEBUG "(pid %d) start dumping LEB properties\n",
663 current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300664 ubifs_get_lp_stats(c, &lst);
665 dbg_dump_lstats(&lst);
666
667 for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
668 err = ubifs_read_one_lp(c, lnum, &lp);
669 if (err)
670 ubifs_err("cannot read lprops for LEB %d", lnum);
671
672 dbg_dump_lprop(c, &lp);
673 }
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200674 printk(KERN_DEBUG "(pid %d) finish dumping LEB properties\n",
675 current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300676}
677
Adrian Hunter73944a62008-09-12 18:13:31 +0300678void dbg_dump_lpt_info(struct ubifs_info *c)
679{
680 int i;
681
682 spin_lock(&dbg_lock);
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200683 printk(KERN_DEBUG "(pid %d) dumping LPT information\n", current->pid);
Adrian Hunter73944a62008-09-12 18:13:31 +0300684 printk(KERN_DEBUG "\tlpt_sz: %lld\n", c->lpt_sz);
685 printk(KERN_DEBUG "\tpnode_sz: %d\n", c->pnode_sz);
686 printk(KERN_DEBUG "\tnnode_sz: %d\n", c->nnode_sz);
687 printk(KERN_DEBUG "\tltab_sz: %d\n", c->ltab_sz);
688 printk(KERN_DEBUG "\tlsave_sz: %d\n", c->lsave_sz);
689 printk(KERN_DEBUG "\tbig_lpt: %d\n", c->big_lpt);
690 printk(KERN_DEBUG "\tlpt_hght: %d\n", c->lpt_hght);
691 printk(KERN_DEBUG "\tpnode_cnt: %d\n", c->pnode_cnt);
692 printk(KERN_DEBUG "\tnnode_cnt: %d\n", c->nnode_cnt);
693 printk(KERN_DEBUG "\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt);
694 printk(KERN_DEBUG "\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt);
695 printk(KERN_DEBUG "\tlsave_cnt: %d\n", c->lsave_cnt);
696 printk(KERN_DEBUG "\tspace_bits: %d\n", c->space_bits);
697 printk(KERN_DEBUG "\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
698 printk(KERN_DEBUG "\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
699 printk(KERN_DEBUG "\tlpt_spc_bits: %d\n", c->lpt_spc_bits);
700 printk(KERN_DEBUG "\tpcnt_bits: %d\n", c->pcnt_bits);
701 printk(KERN_DEBUG "\tlnum_bits: %d\n", c->lnum_bits);
702 printk(KERN_DEBUG "\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
703 printk(KERN_DEBUG "\tLPT head is at %d:%d\n",
704 c->nhead_lnum, c->nhead_offs);
Artem Bityutskiyf92b9822008-12-28 11:34:26 +0200705 printk(KERN_DEBUG "\tLPT ltab is at %d:%d\n",
706 c->ltab_lnum, c->ltab_offs);
Adrian Hunter73944a62008-09-12 18:13:31 +0300707 if (c->big_lpt)
708 printk(KERN_DEBUG "\tLPT lsave is at %d:%d\n",
709 c->lsave_lnum, c->lsave_offs);
710 for (i = 0; i < c->lpt_lebs; i++)
711 printk(KERN_DEBUG "\tLPT LEB %d free %d dirty %d tgc %d "
712 "cmt %d\n", i + c->lpt_first, c->ltab[i].free,
713 c->ltab[i].dirty, c->ltab[i].tgc, c->ltab[i].cmt);
714 spin_unlock(&dbg_lock);
715}
716
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300717void dbg_dump_leb(const struct ubifs_info *c, int lnum)
718{
719 struct ubifs_scan_leb *sleb;
720 struct ubifs_scan_node *snod;
721
722 if (dbg_failure_mode)
723 return;
724
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200725 printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n",
726 current->pid, lnum);
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +0300727 sleb = ubifs_scan(c, lnum, 0, c->dbg->buf);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300728 if (IS_ERR(sleb)) {
729 ubifs_err("scan error %d", (int)PTR_ERR(sleb));
730 return;
731 }
732
733 printk(KERN_DEBUG "LEB %d has %d nodes ending at %d\n", lnum,
734 sleb->nodes_cnt, sleb->endpt);
735
736 list_for_each_entry(snod, &sleb->nodes, list) {
737 cond_resched();
738 printk(KERN_DEBUG "Dumping node at LEB %d:%d len %d\n", lnum,
739 snod->offs, snod->len);
740 dbg_dump_node(c, snod->node);
741 }
742
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200743 printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n",
744 current->pid, lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300745 ubifs_scan_destroy(sleb);
746 return;
747}
748
749void dbg_dump_znode(const struct ubifs_info *c,
750 const struct ubifs_znode *znode)
751{
752 int n;
753 const struct ubifs_zbranch *zbr;
754
755 spin_lock(&dbg_lock);
756 if (znode->parent)
757 zbr = &znode->parent->zbranch[znode->iip];
758 else
759 zbr = &c->zroot;
760
761 printk(KERN_DEBUG "znode %p, LEB %d:%d len %d parent %p iip %d level %d"
762 " child_cnt %d flags %lx\n", znode, zbr->lnum, zbr->offs,
763 zbr->len, znode->parent, znode->iip, znode->level,
764 znode->child_cnt, znode->flags);
765
766 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
767 spin_unlock(&dbg_lock);
768 return;
769 }
770
771 printk(KERN_DEBUG "zbranches:\n");
772 for (n = 0; n < znode->child_cnt; n++) {
773 zbr = &znode->zbranch[n];
774 if (znode->level > 0)
775 printk(KERN_DEBUG "\t%d: znode %p LEB %d:%d len %d key "
776 "%s\n", n, zbr->znode, zbr->lnum,
777 zbr->offs, zbr->len,
778 DBGKEY(&zbr->key));
779 else
780 printk(KERN_DEBUG "\t%d: LNC %p LEB %d:%d len %d key "
781 "%s\n", n, zbr->znode, zbr->lnum,
782 zbr->offs, zbr->len,
783 DBGKEY(&zbr->key));
784 }
785 spin_unlock(&dbg_lock);
786}
787
788void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
789{
790 int i;
791
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200792 printk(KERN_DEBUG "(pid %d) start dumping heap cat %d (%d elements)\n",
Artem Bityutskiy1de94152008-07-25 12:58:38 +0300793 current->pid, cat, heap->cnt);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300794 for (i = 0; i < heap->cnt; i++) {
795 struct ubifs_lprops *lprops = heap->arr[i];
796
797 printk(KERN_DEBUG "\t%d. LEB %d hpos %d free %d dirty %d "
798 "flags %d\n", i, lprops->lnum, lprops->hpos,
799 lprops->free, lprops->dirty, lprops->flags);
800 }
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200801 printk(KERN_DEBUG "(pid %d) finish dumping heap\n", current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300802}
803
804void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
805 struct ubifs_nnode *parent, int iip)
806{
807 int i;
808
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200809 printk(KERN_DEBUG "(pid %d) dumping pnode:\n", current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300810 printk(KERN_DEBUG "\taddress %zx parent %zx cnext %zx\n",
811 (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
812 printk(KERN_DEBUG "\tflags %lu iip %d level %d num %d\n",
813 pnode->flags, iip, pnode->level, pnode->num);
814 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
815 struct ubifs_lprops *lp = &pnode->lprops[i];
816
817 printk(KERN_DEBUG "\t%d: free %d dirty %d flags %d lnum %d\n",
818 i, lp->free, lp->dirty, lp->flags, lp->lnum);
819 }
820}
821
822void dbg_dump_tnc(struct ubifs_info *c)
823{
824 struct ubifs_znode *znode;
825 int level;
826
827 printk(KERN_DEBUG "\n");
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200828 printk(KERN_DEBUG "(pid %d) start dumping TNC tree\n", current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300829 znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
830 level = znode->level;
831 printk(KERN_DEBUG "== Level %d ==\n", level);
832 while (znode) {
833 if (level != znode->level) {
834 level = znode->level;
835 printk(KERN_DEBUG "== Level %d ==\n", level);
836 }
837 dbg_dump_znode(c, znode);
838 znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
839 }
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200840 printk(KERN_DEBUG "(pid %d) finish dumping TNC tree\n", current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300841}
842
843static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
844 void *priv)
845{
846 dbg_dump_znode(c, znode);
847 return 0;
848}
849
850/**
851 * dbg_dump_index - dump the on-flash index.
852 * @c: UBIFS file-system description object
853 *
854 * This function dumps whole UBIFS indexing B-tree, unlike 'dbg_dump_tnc()'
855 * which dumps only in-memory znodes and does not read znodes which from flash.
856 */
857void dbg_dump_index(struct ubifs_info *c)
858{
859 dbg_walk_index(c, NULL, dump_znode, NULL);
860}
861
862/**
Artem Bityutskiy84abf972009-01-23 14:54:59 +0200863 * dbg_save_space_info - save information about flash space.
864 * @c: UBIFS file-system description object
865 *
866 * This function saves information about UBIFS free space, dirty space, etc, in
867 * order to check it later.
868 */
869void dbg_save_space_info(struct ubifs_info *c)
870{
871 struct ubifs_debug_info *d = c->dbg;
872
873 ubifs_get_lp_stats(c, &d->saved_lst);
874
875 spin_lock(&c->space_lock);
876 d->saved_free = ubifs_get_free_space_nolock(c);
877 spin_unlock(&c->space_lock);
878}
879
880/**
881 * dbg_check_space_info - check flash space information.
882 * @c: UBIFS file-system description object
883 *
884 * This function compares current flash space information with the information
885 * which was saved when the 'dbg_save_space_info()' function was called.
886 * Returns zero if the information has not changed, and %-EINVAL it it has
887 * changed.
888 */
889int dbg_check_space_info(struct ubifs_info *c)
890{
891 struct ubifs_debug_info *d = c->dbg;
892 struct ubifs_lp_stats lst;
893 long long avail, free;
894
895 spin_lock(&c->space_lock);
896 avail = ubifs_calc_available(c, c->min_idx_lebs);
897 spin_unlock(&c->space_lock);
898 free = ubifs_get_free_space(c);
899
900 if (free != d->saved_free) {
901 ubifs_err("free space changed from %lld to %lld",
902 d->saved_free, free);
903 goto out;
904 }
905
906 return 0;
907
908out:
909 ubifs_msg("saved lprops statistics dump");
910 dbg_dump_lstats(&d->saved_lst);
911 ubifs_get_lp_stats(c, &lst);
912 ubifs_msg("current lprops statistics dump");
913 dbg_dump_lstats(&d->saved_lst);
914 spin_lock(&c->space_lock);
915 dbg_dump_budg(c);
916 spin_unlock(&c->space_lock);
917 dump_stack();
918 return -EINVAL;
919}
920
921/**
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300922 * dbg_check_synced_i_size - check synchronized inode size.
923 * @inode: inode to check
924 *
925 * If inode is clean, synchronized inode size has to be equivalent to current
926 * inode size. This function has to be called only for locked inodes (@i_mutex
927 * has to be locked). Returns %0 if synchronized inode size if correct, and
928 * %-EINVAL if not.
929 */
930int dbg_check_synced_i_size(struct inode *inode)
931{
932 int err = 0;
933 struct ubifs_inode *ui = ubifs_inode(inode);
934
935 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
936 return 0;
937 if (!S_ISREG(inode->i_mode))
938 return 0;
939
940 mutex_lock(&ui->ui_mutex);
941 spin_lock(&ui->ui_lock);
942 if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
943 ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode "
944 "is clean", ui->ui_size, ui->synced_i_size);
945 ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
946 inode->i_mode, i_size_read(inode));
947 dbg_dump_stack();
948 err = -EINVAL;
949 }
950 spin_unlock(&ui->ui_lock);
951 mutex_unlock(&ui->ui_mutex);
952 return err;
953}
954
955/*
956 * dbg_check_dir - check directory inode size and link count.
957 * @c: UBIFS file-system description object
958 * @dir: the directory to calculate size for
959 * @size: the result is returned here
960 *
961 * This function makes sure that directory size and link count are correct.
962 * Returns zero in case of success and a negative error code in case of
963 * failure.
964 *
965 * Note, it is good idea to make sure the @dir->i_mutex is locked before
966 * calling this function.
967 */
968int dbg_check_dir_size(struct ubifs_info *c, const struct inode *dir)
969{
970 unsigned int nlink = 2;
971 union ubifs_key key;
972 struct ubifs_dent_node *dent, *pdent = NULL;
973 struct qstr nm = { .name = NULL };
974 loff_t size = UBIFS_INO_NODE_SZ;
975
976 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
977 return 0;
978
979 if (!S_ISDIR(dir->i_mode))
980 return 0;
981
982 lowest_dent_key(c, &key, dir->i_ino);
983 while (1) {
984 int err;
985
986 dent = ubifs_tnc_next_ent(c, &key, &nm);
987 if (IS_ERR(dent)) {
988 err = PTR_ERR(dent);
989 if (err == -ENOENT)
990 break;
991 return err;
992 }
993
994 nm.name = dent->name;
995 nm.len = le16_to_cpu(dent->nlen);
996 size += CALC_DENT_SIZE(nm.len);
997 if (dent->type == UBIFS_ITYPE_DIR)
998 nlink += 1;
999 kfree(pdent);
1000 pdent = dent;
1001 key_read(c, &dent->key, &key);
1002 }
1003 kfree(pdent);
1004
1005 if (i_size_read(dir) != size) {
1006 ubifs_err("directory inode %lu has size %llu, "
1007 "but calculated size is %llu", dir->i_ino,
1008 (unsigned long long)i_size_read(dir),
1009 (unsigned long long)size);
1010 dump_stack();
1011 return -EINVAL;
1012 }
1013 if (dir->i_nlink != nlink) {
1014 ubifs_err("directory inode %lu has nlink %u, but calculated "
1015 "nlink is %u", dir->i_ino, dir->i_nlink, nlink);
1016 dump_stack();
1017 return -EINVAL;
1018 }
1019
1020 return 0;
1021}
1022
1023/**
1024 * dbg_check_key_order - make sure that colliding keys are properly ordered.
1025 * @c: UBIFS file-system description object
1026 * @zbr1: first zbranch
1027 * @zbr2: following zbranch
1028 *
1029 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
1030 * names of the direntries/xentries which are referred by the keys. This
1031 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
1032 * sure the name of direntry/xentry referred by @zbr1 is less than
1033 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
1034 * and a negative error code in case of failure.
1035 */
1036static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
1037 struct ubifs_zbranch *zbr2)
1038{
1039 int err, nlen1, nlen2, cmp;
1040 struct ubifs_dent_node *dent1, *dent2;
1041 union ubifs_key key;
1042
1043 ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
1044 dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1045 if (!dent1)
1046 return -ENOMEM;
1047 dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1048 if (!dent2) {
1049 err = -ENOMEM;
1050 goto out_free;
1051 }
1052
1053 err = ubifs_tnc_read_node(c, zbr1, dent1);
1054 if (err)
1055 goto out_free;
1056 err = ubifs_validate_entry(c, dent1);
1057 if (err)
1058 goto out_free;
1059
1060 err = ubifs_tnc_read_node(c, zbr2, dent2);
1061 if (err)
1062 goto out_free;
1063 err = ubifs_validate_entry(c, dent2);
1064 if (err)
1065 goto out_free;
1066
1067 /* Make sure node keys are the same as in zbranch */
1068 err = 1;
1069 key_read(c, &dent1->key, &key);
1070 if (keys_cmp(c, &zbr1->key, &key)) {
Artem Bityutskiy5d38b3a2008-12-30 17:58:42 +02001071 dbg_err("1st entry at %d:%d has key %s", zbr1->lnum,
1072 zbr1->offs, DBGKEY(&key));
1073 dbg_err("but it should have key %s according to tnc",
1074 DBGKEY(&zbr1->key));
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +02001075 dbg_dump_node(c, dent1);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03001076 goto out_free;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001077 }
1078
1079 key_read(c, &dent2->key, &key);
1080 if (keys_cmp(c, &zbr2->key, &key)) {
Artem Bityutskiy5d38b3a2008-12-30 17:58:42 +02001081 dbg_err("2nd entry at %d:%d has key %s", zbr1->lnum,
1082 zbr1->offs, DBGKEY(&key));
1083 dbg_err("but it should have key %s according to tnc",
1084 DBGKEY(&zbr2->key));
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +02001085 dbg_dump_node(c, dent2);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03001086 goto out_free;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001087 }
1088
1089 nlen1 = le16_to_cpu(dent1->nlen);
1090 nlen2 = le16_to_cpu(dent2->nlen);
1091
1092 cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
1093 if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
1094 err = 0;
1095 goto out_free;
1096 }
1097 if (cmp == 0 && nlen1 == nlen2)
Artem Bityutskiy5d38b3a2008-12-30 17:58:42 +02001098 dbg_err("2 xent/dent nodes with the same name");
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001099 else
Artem Bityutskiy5d38b3a2008-12-30 17:58:42 +02001100 dbg_err("bad order of colliding key %s",
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001101 DBGKEY(&key));
1102
Artem Bityutskiy552ff312008-10-23 11:49:28 +03001103 ubifs_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001104 dbg_dump_node(c, dent1);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03001105 ubifs_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001106 dbg_dump_node(c, dent2);
1107
1108out_free:
1109 kfree(dent2);
1110 kfree(dent1);
1111 return err;
1112}
1113
1114/**
1115 * dbg_check_znode - check if znode is all right.
1116 * @c: UBIFS file-system description object
1117 * @zbr: zbranch which points to this znode
1118 *
1119 * This function makes sure that znode referred to by @zbr is all right.
1120 * Returns zero if it is, and %-EINVAL if it is not.
1121 */
1122static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
1123{
1124 struct ubifs_znode *znode = zbr->znode;
1125 struct ubifs_znode *zp = znode->parent;
1126 int n, err, cmp;
1127
1128 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
1129 err = 1;
1130 goto out;
1131 }
1132 if (znode->level < 0) {
1133 err = 2;
1134 goto out;
1135 }
1136 if (znode->iip < 0 || znode->iip >= c->fanout) {
1137 err = 3;
1138 goto out;
1139 }
1140
1141 if (zbr->len == 0)
1142 /* Only dirty zbranch may have no on-flash nodes */
1143 if (!ubifs_zn_dirty(znode)) {
1144 err = 4;
1145 goto out;
1146 }
1147
1148 if (ubifs_zn_dirty(znode)) {
1149 /*
1150 * If znode is dirty, its parent has to be dirty as well. The
1151 * order of the operation is important, so we have to have
1152 * memory barriers.
1153 */
1154 smp_mb();
1155 if (zp && !ubifs_zn_dirty(zp)) {
1156 /*
1157 * The dirty flag is atomic and is cleared outside the
1158 * TNC mutex, so znode's dirty flag may now have
1159 * been cleared. The child is always cleared before the
1160 * parent, so we just need to check again.
1161 */
1162 smp_mb();
1163 if (ubifs_zn_dirty(znode)) {
1164 err = 5;
1165 goto out;
1166 }
1167 }
1168 }
1169
1170 if (zp) {
1171 const union ubifs_key *min, *max;
1172
1173 if (znode->level != zp->level - 1) {
1174 err = 6;
1175 goto out;
1176 }
1177
1178 /* Make sure the 'parent' pointer in our znode is correct */
1179 err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
1180 if (!err) {
1181 /* This zbranch does not exist in the parent */
1182 err = 7;
1183 goto out;
1184 }
1185
1186 if (znode->iip >= zp->child_cnt) {
1187 err = 8;
1188 goto out;
1189 }
1190
1191 if (znode->iip != n) {
1192 /* This may happen only in case of collisions */
1193 if (keys_cmp(c, &zp->zbranch[n].key,
1194 &zp->zbranch[znode->iip].key)) {
1195 err = 9;
1196 goto out;
1197 }
1198 n = znode->iip;
1199 }
1200
1201 /*
1202 * Make sure that the first key in our znode is greater than or
1203 * equal to the key in the pointing zbranch.
1204 */
1205 min = &zbr->key;
1206 cmp = keys_cmp(c, min, &znode->zbranch[0].key);
1207 if (cmp == 1) {
1208 err = 10;
1209 goto out;
1210 }
1211
1212 if (n + 1 < zp->child_cnt) {
1213 max = &zp->zbranch[n + 1].key;
1214
1215 /*
1216 * Make sure the last key in our znode is less or
1217 * equivalent than the the key in zbranch which goes
1218 * after our pointing zbranch.
1219 */
1220 cmp = keys_cmp(c, max,
1221 &znode->zbranch[znode->child_cnt - 1].key);
1222 if (cmp == -1) {
1223 err = 11;
1224 goto out;
1225 }
1226 }
1227 } else {
1228 /* This may only be root znode */
1229 if (zbr != &c->zroot) {
1230 err = 12;
1231 goto out;
1232 }
1233 }
1234
1235 /*
1236 * Make sure that next key is greater or equivalent then the previous
1237 * one.
1238 */
1239 for (n = 1; n < znode->child_cnt; n++) {
1240 cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
1241 &znode->zbranch[n].key);
1242 if (cmp > 0) {
1243 err = 13;
1244 goto out;
1245 }
1246 if (cmp == 0) {
1247 /* This can only be keys with colliding hash */
1248 if (!is_hash_key(c, &znode->zbranch[n].key)) {
1249 err = 14;
1250 goto out;
1251 }
1252
1253 if (znode->level != 0 || c->replaying)
1254 continue;
1255
1256 /*
1257 * Colliding keys should follow binary order of
1258 * corresponding xentry/dentry names.
1259 */
1260 err = dbg_check_key_order(c, &znode->zbranch[n - 1],
1261 &znode->zbranch[n]);
1262 if (err < 0)
1263 return err;
1264 if (err) {
1265 err = 15;
1266 goto out;
1267 }
1268 }
1269 }
1270
1271 for (n = 0; n < znode->child_cnt; n++) {
1272 if (!znode->zbranch[n].znode &&
1273 (znode->zbranch[n].lnum == 0 ||
1274 znode->zbranch[n].len == 0)) {
1275 err = 16;
1276 goto out;
1277 }
1278
1279 if (znode->zbranch[n].lnum != 0 &&
1280 znode->zbranch[n].len == 0) {
1281 err = 17;
1282 goto out;
1283 }
1284
1285 if (znode->zbranch[n].lnum == 0 &&
1286 znode->zbranch[n].len != 0) {
1287 err = 18;
1288 goto out;
1289 }
1290
1291 if (znode->zbranch[n].lnum == 0 &&
1292 znode->zbranch[n].offs != 0) {
1293 err = 19;
1294 goto out;
1295 }
1296
1297 if (znode->level != 0 && znode->zbranch[n].znode)
1298 if (znode->zbranch[n].znode->parent != znode) {
1299 err = 20;
1300 goto out;
1301 }
1302 }
1303
1304 return 0;
1305
1306out:
1307 ubifs_err("failed, error %d", err);
1308 ubifs_msg("dump of the znode");
1309 dbg_dump_znode(c, znode);
1310 if (zp) {
1311 ubifs_msg("dump of the parent znode");
1312 dbg_dump_znode(c, zp);
1313 }
1314 dump_stack();
1315 return -EINVAL;
1316}
1317
1318/**
1319 * dbg_check_tnc - check TNC tree.
1320 * @c: UBIFS file-system description object
1321 * @extra: do extra checks that are possible at start commit
1322 *
1323 * This function traverses whole TNC tree and checks every znode. Returns zero
1324 * if everything is all right and %-EINVAL if something is wrong with TNC.
1325 */
1326int dbg_check_tnc(struct ubifs_info *c, int extra)
1327{
1328 struct ubifs_znode *znode;
1329 long clean_cnt = 0, dirty_cnt = 0;
1330 int err, last;
1331
1332 if (!(ubifs_chk_flags & UBIFS_CHK_TNC))
1333 return 0;
1334
1335 ubifs_assert(mutex_is_locked(&c->tnc_mutex));
1336 if (!c->zroot.znode)
1337 return 0;
1338
1339 znode = ubifs_tnc_postorder_first(c->zroot.znode);
1340 while (1) {
1341 struct ubifs_znode *prev;
1342 struct ubifs_zbranch *zbr;
1343
1344 if (!znode->parent)
1345 zbr = &c->zroot;
1346 else
1347 zbr = &znode->parent->zbranch[znode->iip];
1348
1349 err = dbg_check_znode(c, zbr);
1350 if (err)
1351 return err;
1352
1353 if (extra) {
1354 if (ubifs_zn_dirty(znode))
1355 dirty_cnt += 1;
1356 else
1357 clean_cnt += 1;
1358 }
1359
1360 prev = znode;
1361 znode = ubifs_tnc_postorder_next(znode);
1362 if (!znode)
1363 break;
1364
1365 /*
1366 * If the last key of this znode is equivalent to the first key
1367 * of the next znode (collision), then check order of the keys.
1368 */
1369 last = prev->child_cnt - 1;
1370 if (prev->level == 0 && znode->level == 0 && !c->replaying &&
1371 !keys_cmp(c, &prev->zbranch[last].key,
1372 &znode->zbranch[0].key)) {
1373 err = dbg_check_key_order(c, &prev->zbranch[last],
1374 &znode->zbranch[0]);
1375 if (err < 0)
1376 return err;
1377 if (err) {
1378 ubifs_msg("first znode");
1379 dbg_dump_znode(c, prev);
1380 ubifs_msg("second znode");
1381 dbg_dump_znode(c, znode);
1382 return -EINVAL;
1383 }
1384 }
1385 }
1386
1387 if (extra) {
1388 if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
1389 ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld",
1390 atomic_long_read(&c->clean_zn_cnt),
1391 clean_cnt);
1392 return -EINVAL;
1393 }
1394 if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
1395 ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld",
1396 atomic_long_read(&c->dirty_zn_cnt),
1397 dirty_cnt);
1398 return -EINVAL;
1399 }
1400 }
1401
1402 return 0;
1403}
1404
1405/**
1406 * dbg_walk_index - walk the on-flash index.
1407 * @c: UBIFS file-system description object
1408 * @leaf_cb: called for each leaf node
1409 * @znode_cb: called for each indexing node
Adrian Hunter227c75c2009-01-29 11:53:51 +02001410 * @priv: private data which is passed to callbacks
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001411 *
1412 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1413 * node and @znode_cb for each indexing node. Returns zero in case of success
1414 * and a negative error code in case of failure.
1415 *
1416 * It would be better if this function removed every znode it pulled to into
1417 * the TNC, so that the behavior more closely matched the non-debugging
1418 * behavior.
1419 */
1420int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1421 dbg_znode_callback znode_cb, void *priv)
1422{
1423 int err;
1424 struct ubifs_zbranch *zbr;
1425 struct ubifs_znode *znode, *child;
1426
1427 mutex_lock(&c->tnc_mutex);
1428 /* If the root indexing node is not in TNC - pull it */
1429 if (!c->zroot.znode) {
1430 c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1431 if (IS_ERR(c->zroot.znode)) {
1432 err = PTR_ERR(c->zroot.znode);
1433 c->zroot.znode = NULL;
1434 goto out_unlock;
1435 }
1436 }
1437
1438 /*
1439 * We are going to traverse the indexing tree in the postorder manner.
1440 * Go down and find the leftmost indexing node where we are going to
1441 * start from.
1442 */
1443 znode = c->zroot.znode;
1444 while (znode->level > 0) {
1445 zbr = &znode->zbranch[0];
1446 child = zbr->znode;
1447 if (!child) {
1448 child = ubifs_load_znode(c, zbr, znode, 0);
1449 if (IS_ERR(child)) {
1450 err = PTR_ERR(child);
1451 goto out_unlock;
1452 }
1453 zbr->znode = child;
1454 }
1455
1456 znode = child;
1457 }
1458
1459 /* Iterate over all indexing nodes */
1460 while (1) {
1461 int idx;
1462
1463 cond_resched();
1464
1465 if (znode_cb) {
1466 err = znode_cb(c, znode, priv);
1467 if (err) {
1468 ubifs_err("znode checking function returned "
1469 "error %d", err);
1470 dbg_dump_znode(c, znode);
1471 goto out_dump;
1472 }
1473 }
1474 if (leaf_cb && znode->level == 0) {
1475 for (idx = 0; idx < znode->child_cnt; idx++) {
1476 zbr = &znode->zbranch[idx];
1477 err = leaf_cb(c, zbr, priv);
1478 if (err) {
1479 ubifs_err("leaf checking function "
1480 "returned error %d, for leaf "
1481 "at LEB %d:%d",
1482 err, zbr->lnum, zbr->offs);
1483 goto out_dump;
1484 }
1485 }
1486 }
1487
1488 if (!znode->parent)
1489 break;
1490
1491 idx = znode->iip + 1;
1492 znode = znode->parent;
1493 if (idx < znode->child_cnt) {
1494 /* Switch to the next index in the parent */
1495 zbr = &znode->zbranch[idx];
1496 child = zbr->znode;
1497 if (!child) {
1498 child = ubifs_load_znode(c, zbr, znode, idx);
1499 if (IS_ERR(child)) {
1500 err = PTR_ERR(child);
1501 goto out_unlock;
1502 }
1503 zbr->znode = child;
1504 }
1505 znode = child;
1506 } else
1507 /*
1508 * This is the last child, switch to the parent and
1509 * continue.
1510 */
1511 continue;
1512
1513 /* Go to the lowest leftmost znode in the new sub-tree */
1514 while (znode->level > 0) {
1515 zbr = &znode->zbranch[0];
1516 child = zbr->znode;
1517 if (!child) {
1518 child = ubifs_load_znode(c, zbr, znode, 0);
1519 if (IS_ERR(child)) {
1520 err = PTR_ERR(child);
1521 goto out_unlock;
1522 }
1523 zbr->znode = child;
1524 }
1525 znode = child;
1526 }
1527 }
1528
1529 mutex_unlock(&c->tnc_mutex);
1530 return 0;
1531
1532out_dump:
1533 if (znode->parent)
1534 zbr = &znode->parent->zbranch[znode->iip];
1535 else
1536 zbr = &c->zroot;
1537 ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1538 dbg_dump_znode(c, znode);
1539out_unlock:
1540 mutex_unlock(&c->tnc_mutex);
1541 return err;
1542}
1543
1544/**
1545 * add_size - add znode size to partially calculated index size.
1546 * @c: UBIFS file-system description object
1547 * @znode: znode to add size for
1548 * @priv: partially calculated index size
1549 *
1550 * This is a helper function for 'dbg_check_idx_size()' which is called for
1551 * every indexing node and adds its size to the 'long long' variable pointed to
1552 * by @priv.
1553 */
1554static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1555{
1556 long long *idx_size = priv;
1557 int add;
1558
1559 add = ubifs_idx_node_sz(c, znode->child_cnt);
1560 add = ALIGN(add, 8);
1561 *idx_size += add;
1562 return 0;
1563}
1564
1565/**
1566 * dbg_check_idx_size - check index size.
1567 * @c: UBIFS file-system description object
1568 * @idx_size: size to check
1569 *
1570 * This function walks the UBIFS index, calculates its size and checks that the
1571 * size is equivalent to @idx_size. Returns zero in case of success and a
1572 * negative error code in case of failure.
1573 */
1574int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
1575{
1576 int err;
1577 long long calc = 0;
1578
1579 if (!(ubifs_chk_flags & UBIFS_CHK_IDX_SZ))
1580 return 0;
1581
1582 err = dbg_walk_index(c, NULL, add_size, &calc);
1583 if (err) {
1584 ubifs_err("error %d while walking the index", err);
1585 return err;
1586 }
1587
1588 if (calc != idx_size) {
1589 ubifs_err("index size check failed: calculated size is %lld, "
1590 "should be %lld", calc, idx_size);
1591 dump_stack();
1592 return -EINVAL;
1593 }
1594
1595 return 0;
1596}
1597
1598/**
1599 * struct fsck_inode - information about an inode used when checking the file-system.
1600 * @rb: link in the RB-tree of inodes
1601 * @inum: inode number
1602 * @mode: inode type, permissions, etc
1603 * @nlink: inode link count
1604 * @xattr_cnt: count of extended attributes
1605 * @references: how many directory/xattr entries refer this inode (calculated
1606 * while walking the index)
1607 * @calc_cnt: for directory inode count of child directories
1608 * @size: inode size (read from on-flash inode)
1609 * @xattr_sz: summary size of all extended attributes (read from on-flash
1610 * inode)
1611 * @calc_sz: for directories calculated directory size
1612 * @calc_xcnt: count of extended attributes
1613 * @calc_xsz: calculated summary size of all extended attributes
1614 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1615 * inode (read from on-flash inode)
1616 * @calc_xnms: calculated sum of lengths of all extended attribute names
1617 */
1618struct fsck_inode {
1619 struct rb_node rb;
1620 ino_t inum;
1621 umode_t mode;
1622 unsigned int nlink;
1623 unsigned int xattr_cnt;
1624 int references;
1625 int calc_cnt;
1626 long long size;
1627 unsigned int xattr_sz;
1628 long long calc_sz;
1629 long long calc_xcnt;
1630 long long calc_xsz;
1631 unsigned int xattr_nms;
1632 long long calc_xnms;
1633};
1634
1635/**
1636 * struct fsck_data - private FS checking information.
1637 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1638 */
1639struct fsck_data {
1640 struct rb_root inodes;
1641};
1642
1643/**
1644 * add_inode - add inode information to RB-tree of inodes.
1645 * @c: UBIFS file-system description object
1646 * @fsckd: FS checking information
1647 * @ino: raw UBIFS inode to add
1648 *
1649 * This is a helper function for 'check_leaf()' which adds information about
1650 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1651 * case of success and a negative error code in case of failure.
1652 */
1653static struct fsck_inode *add_inode(struct ubifs_info *c,
1654 struct fsck_data *fsckd,
1655 struct ubifs_ino_node *ino)
1656{
1657 struct rb_node **p, *parent = NULL;
1658 struct fsck_inode *fscki;
1659 ino_t inum = key_inum_flash(c, &ino->key);
1660
1661 p = &fsckd->inodes.rb_node;
1662 while (*p) {
1663 parent = *p;
1664 fscki = rb_entry(parent, struct fsck_inode, rb);
1665 if (inum < fscki->inum)
1666 p = &(*p)->rb_left;
1667 else if (inum > fscki->inum)
1668 p = &(*p)->rb_right;
1669 else
1670 return fscki;
1671 }
1672
1673 if (inum > c->highest_inum) {
1674 ubifs_err("too high inode number, max. is %lu",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001675 (unsigned long)c->highest_inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001676 return ERR_PTR(-EINVAL);
1677 }
1678
1679 fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
1680 if (!fscki)
1681 return ERR_PTR(-ENOMEM);
1682
1683 fscki->inum = inum;
1684 fscki->nlink = le32_to_cpu(ino->nlink);
1685 fscki->size = le64_to_cpu(ino->size);
1686 fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
1687 fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
1688 fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
1689 fscki->mode = le32_to_cpu(ino->mode);
1690 if (S_ISDIR(fscki->mode)) {
1691 fscki->calc_sz = UBIFS_INO_NODE_SZ;
1692 fscki->calc_cnt = 2;
1693 }
1694 rb_link_node(&fscki->rb, parent, p);
1695 rb_insert_color(&fscki->rb, &fsckd->inodes);
1696 return fscki;
1697}
1698
1699/**
1700 * search_inode - search inode in the RB-tree of inodes.
1701 * @fsckd: FS checking information
1702 * @inum: inode number to search
1703 *
1704 * This is a helper function for 'check_leaf()' which searches inode @inum in
1705 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1706 * the inode was not found.
1707 */
1708static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
1709{
1710 struct rb_node *p;
1711 struct fsck_inode *fscki;
1712
1713 p = fsckd->inodes.rb_node;
1714 while (p) {
1715 fscki = rb_entry(p, struct fsck_inode, rb);
1716 if (inum < fscki->inum)
1717 p = p->rb_left;
1718 else if (inum > fscki->inum)
1719 p = p->rb_right;
1720 else
1721 return fscki;
1722 }
1723 return NULL;
1724}
1725
1726/**
1727 * read_add_inode - read inode node and add it to RB-tree of inodes.
1728 * @c: UBIFS file-system description object
1729 * @fsckd: FS checking information
1730 * @inum: inode number to read
1731 *
1732 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1733 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1734 * information pointer in case of success and a negative error code in case of
1735 * failure.
1736 */
1737static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1738 struct fsck_data *fsckd, ino_t inum)
1739{
1740 int n, err;
1741 union ubifs_key key;
1742 struct ubifs_znode *znode;
1743 struct ubifs_zbranch *zbr;
1744 struct ubifs_ino_node *ino;
1745 struct fsck_inode *fscki;
1746
1747 fscki = search_inode(fsckd, inum);
1748 if (fscki)
1749 return fscki;
1750
1751 ino_key_init(c, &key, inum);
1752 err = ubifs_lookup_level0(c, &key, &znode, &n);
1753 if (!err) {
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001754 ubifs_err("inode %lu not found in index", (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001755 return ERR_PTR(-ENOENT);
1756 } else if (err < 0) {
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001757 ubifs_err("error %d while looking up inode %lu",
1758 err, (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001759 return ERR_PTR(err);
1760 }
1761
1762 zbr = &znode->zbranch[n];
1763 if (zbr->len < UBIFS_INO_NODE_SZ) {
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001764 ubifs_err("bad node %lu node length %d",
1765 (unsigned long)inum, zbr->len);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001766 return ERR_PTR(-EINVAL);
1767 }
1768
1769 ino = kmalloc(zbr->len, GFP_NOFS);
1770 if (!ino)
1771 return ERR_PTR(-ENOMEM);
1772
1773 err = ubifs_tnc_read_node(c, zbr, ino);
1774 if (err) {
1775 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
1776 zbr->lnum, zbr->offs, err);
1777 kfree(ino);
1778 return ERR_PTR(err);
1779 }
1780
1781 fscki = add_inode(c, fsckd, ino);
1782 kfree(ino);
1783 if (IS_ERR(fscki)) {
1784 ubifs_err("error %ld while adding inode %lu node",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001785 PTR_ERR(fscki), (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001786 return fscki;
1787 }
1788
1789 return fscki;
1790}
1791
1792/**
1793 * check_leaf - check leaf node.
1794 * @c: UBIFS file-system description object
1795 * @zbr: zbranch of the leaf node to check
1796 * @priv: FS checking information
1797 *
1798 * This is a helper function for 'dbg_check_filesystem()' which is called for
1799 * every single leaf node while walking the indexing tree. It checks that the
1800 * leaf node referred from the indexing tree exists, has correct CRC, and does
1801 * some other basic validation. This function is also responsible for building
1802 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
1803 * calculates reference count, size, etc for each inode in order to later
1804 * compare them to the information stored inside the inodes and detect possible
1805 * inconsistencies. Returns zero in case of success and a negative error code
1806 * in case of failure.
1807 */
1808static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
1809 void *priv)
1810{
1811 ino_t inum;
1812 void *node;
1813 struct ubifs_ch *ch;
1814 int err, type = key_type(c, &zbr->key);
1815 struct fsck_inode *fscki;
1816
1817 if (zbr->len < UBIFS_CH_SZ) {
1818 ubifs_err("bad leaf length %d (LEB %d:%d)",
1819 zbr->len, zbr->lnum, zbr->offs);
1820 return -EINVAL;
1821 }
1822
1823 node = kmalloc(zbr->len, GFP_NOFS);
1824 if (!node)
1825 return -ENOMEM;
1826
1827 err = ubifs_tnc_read_node(c, zbr, node);
1828 if (err) {
1829 ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
1830 zbr->lnum, zbr->offs, err);
1831 goto out_free;
1832 }
1833
1834 /* If this is an inode node, add it to RB-tree of inodes */
1835 if (type == UBIFS_INO_KEY) {
1836 fscki = add_inode(c, priv, node);
1837 if (IS_ERR(fscki)) {
1838 err = PTR_ERR(fscki);
1839 ubifs_err("error %d while adding inode node", err);
1840 goto out_dump;
1841 }
1842 goto out;
1843 }
1844
1845 if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
1846 type != UBIFS_DATA_KEY) {
1847 ubifs_err("unexpected node type %d at LEB %d:%d",
1848 type, zbr->lnum, zbr->offs);
1849 err = -EINVAL;
1850 goto out_free;
1851 }
1852
1853 ch = node;
1854 if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
1855 ubifs_err("too high sequence number, max. is %llu",
1856 c->max_sqnum);
1857 err = -EINVAL;
1858 goto out_dump;
1859 }
1860
1861 if (type == UBIFS_DATA_KEY) {
1862 long long blk_offs;
1863 struct ubifs_data_node *dn = node;
1864
1865 /*
1866 * Search the inode node this data node belongs to and insert
1867 * it to the RB-tree of inodes.
1868 */
1869 inum = key_inum_flash(c, &dn->key);
1870 fscki = read_add_inode(c, priv, inum);
1871 if (IS_ERR(fscki)) {
1872 err = PTR_ERR(fscki);
1873 ubifs_err("error %d while processing data node and "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001874 "trying to find inode node %lu",
1875 err, (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001876 goto out_dump;
1877 }
1878
1879 /* Make sure the data node is within inode size */
1880 blk_offs = key_block_flash(c, &dn->key);
1881 blk_offs <<= UBIFS_BLOCK_SHIFT;
1882 blk_offs += le32_to_cpu(dn->size);
1883 if (blk_offs > fscki->size) {
1884 ubifs_err("data node at LEB %d:%d is not within inode "
1885 "size %lld", zbr->lnum, zbr->offs,
1886 fscki->size);
1887 err = -EINVAL;
1888 goto out_dump;
1889 }
1890 } else {
1891 int nlen;
1892 struct ubifs_dent_node *dent = node;
1893 struct fsck_inode *fscki1;
1894
1895 err = ubifs_validate_entry(c, dent);
1896 if (err)
1897 goto out_dump;
1898
1899 /*
1900 * Search the inode node this entry refers to and the parent
1901 * inode node and insert them to the RB-tree of inodes.
1902 */
1903 inum = le64_to_cpu(dent->inum);
1904 fscki = read_add_inode(c, priv, inum);
1905 if (IS_ERR(fscki)) {
1906 err = PTR_ERR(fscki);
1907 ubifs_err("error %d while processing entry node and "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001908 "trying to find inode node %lu",
1909 err, (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001910 goto out_dump;
1911 }
1912
1913 /* Count how many direntries or xentries refers this inode */
1914 fscki->references += 1;
1915
1916 inum = key_inum_flash(c, &dent->key);
1917 fscki1 = read_add_inode(c, priv, inum);
1918 if (IS_ERR(fscki1)) {
1919 err = PTR_ERR(fscki);
1920 ubifs_err("error %d while processing entry node and "
1921 "trying to find parent inode node %lu",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001922 err, (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001923 goto out_dump;
1924 }
1925
1926 nlen = le16_to_cpu(dent->nlen);
1927 if (type == UBIFS_XENT_KEY) {
1928 fscki1->calc_xcnt += 1;
1929 fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
1930 fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
1931 fscki1->calc_xnms += nlen;
1932 } else {
1933 fscki1->calc_sz += CALC_DENT_SIZE(nlen);
1934 if (dent->type == UBIFS_ITYPE_DIR)
1935 fscki1->calc_cnt += 1;
1936 }
1937 }
1938
1939out:
1940 kfree(node);
1941 return 0;
1942
1943out_dump:
1944 ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
1945 dbg_dump_node(c, node);
1946out_free:
1947 kfree(node);
1948 return err;
1949}
1950
1951/**
1952 * free_inodes - free RB-tree of inodes.
1953 * @fsckd: FS checking information
1954 */
1955static void free_inodes(struct fsck_data *fsckd)
1956{
1957 struct rb_node *this = fsckd->inodes.rb_node;
1958 struct fsck_inode *fscki;
1959
1960 while (this) {
1961 if (this->rb_left)
1962 this = this->rb_left;
1963 else if (this->rb_right)
1964 this = this->rb_right;
1965 else {
1966 fscki = rb_entry(this, struct fsck_inode, rb);
1967 this = rb_parent(this);
1968 if (this) {
1969 if (this->rb_left == &fscki->rb)
1970 this->rb_left = NULL;
1971 else
1972 this->rb_right = NULL;
1973 }
1974 kfree(fscki);
1975 }
1976 }
1977}
1978
1979/**
1980 * check_inodes - checks all inodes.
1981 * @c: UBIFS file-system description object
1982 * @fsckd: FS checking information
1983 *
1984 * This is a helper function for 'dbg_check_filesystem()' which walks the
1985 * RB-tree of inodes after the index scan has been finished, and checks that
1986 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
1987 * %-EINVAL if not, and a negative error code in case of failure.
1988 */
1989static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
1990{
1991 int n, err;
1992 union ubifs_key key;
1993 struct ubifs_znode *znode;
1994 struct ubifs_zbranch *zbr;
1995 struct ubifs_ino_node *ino;
1996 struct fsck_inode *fscki;
1997 struct rb_node *this = rb_first(&fsckd->inodes);
1998
1999 while (this) {
2000 fscki = rb_entry(this, struct fsck_inode, rb);
2001 this = rb_next(this);
2002
2003 if (S_ISDIR(fscki->mode)) {
2004 /*
2005 * Directories have to have exactly one reference (they
2006 * cannot have hardlinks), although root inode is an
2007 * exception.
2008 */
2009 if (fscki->inum != UBIFS_ROOT_INO &&
2010 fscki->references != 1) {
2011 ubifs_err("directory inode %lu has %d "
2012 "direntries which refer it, but "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002013 "should be 1",
2014 (unsigned long)fscki->inum,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002015 fscki->references);
2016 goto out_dump;
2017 }
2018 if (fscki->inum == UBIFS_ROOT_INO &&
2019 fscki->references != 0) {
2020 ubifs_err("root inode %lu has non-zero (%d) "
2021 "direntries which refer it",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002022 (unsigned long)fscki->inum,
2023 fscki->references);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002024 goto out_dump;
2025 }
2026 if (fscki->calc_sz != fscki->size) {
2027 ubifs_err("directory inode %lu size is %lld, "
2028 "but calculated size is %lld",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002029 (unsigned long)fscki->inum,
2030 fscki->size, fscki->calc_sz);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002031 goto out_dump;
2032 }
2033 if (fscki->calc_cnt != fscki->nlink) {
2034 ubifs_err("directory inode %lu nlink is %d, "
2035 "but calculated nlink is %d",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002036 (unsigned long)fscki->inum,
2037 fscki->nlink, fscki->calc_cnt);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002038 goto out_dump;
2039 }
2040 } else {
2041 if (fscki->references != fscki->nlink) {
2042 ubifs_err("inode %lu nlink is %d, but "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002043 "calculated nlink is %d",
2044 (unsigned long)fscki->inum,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002045 fscki->nlink, fscki->references);
2046 goto out_dump;
2047 }
2048 }
2049 if (fscki->xattr_sz != fscki->calc_xsz) {
2050 ubifs_err("inode %lu has xattr size %u, but "
2051 "calculated size is %lld",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002052 (unsigned long)fscki->inum, fscki->xattr_sz,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002053 fscki->calc_xsz);
2054 goto out_dump;
2055 }
2056 if (fscki->xattr_cnt != fscki->calc_xcnt) {
2057 ubifs_err("inode %lu has %u xattrs, but "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002058 "calculated count is %lld",
2059 (unsigned long)fscki->inum,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002060 fscki->xattr_cnt, fscki->calc_xcnt);
2061 goto out_dump;
2062 }
2063 if (fscki->xattr_nms != fscki->calc_xnms) {
2064 ubifs_err("inode %lu has xattr names' size %u, but "
2065 "calculated names' size is %lld",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002066 (unsigned long)fscki->inum, fscki->xattr_nms,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002067 fscki->calc_xnms);
2068 goto out_dump;
2069 }
2070 }
2071
2072 return 0;
2073
2074out_dump:
2075 /* Read the bad inode and dump it */
2076 ino_key_init(c, &key, fscki->inum);
2077 err = ubifs_lookup_level0(c, &key, &znode, &n);
2078 if (!err) {
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002079 ubifs_err("inode %lu not found in index",
2080 (unsigned long)fscki->inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002081 return -ENOENT;
2082 } else if (err < 0) {
2083 ubifs_err("error %d while looking up inode %lu",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002084 err, (unsigned long)fscki->inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002085 return err;
2086 }
2087
2088 zbr = &znode->zbranch[n];
2089 ino = kmalloc(zbr->len, GFP_NOFS);
2090 if (!ino)
2091 return -ENOMEM;
2092
2093 err = ubifs_tnc_read_node(c, zbr, ino);
2094 if (err) {
2095 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2096 zbr->lnum, zbr->offs, err);
2097 kfree(ino);
2098 return err;
2099 }
2100
2101 ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002102 (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002103 dbg_dump_node(c, ino);
2104 kfree(ino);
2105 return -EINVAL;
2106}
2107
2108/**
2109 * dbg_check_filesystem - check the file-system.
2110 * @c: UBIFS file-system description object
2111 *
2112 * This function checks the file system, namely:
2113 * o makes sure that all leaf nodes exist and their CRCs are correct;
2114 * o makes sure inode nlink, size, xattr size/count are correct (for all
2115 * inodes).
2116 *
2117 * The function reads whole indexing tree and all nodes, so it is pretty
2118 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2119 * not, and a negative error code in case of failure.
2120 */
2121int dbg_check_filesystem(struct ubifs_info *c)
2122{
2123 int err;
2124 struct fsck_data fsckd;
2125
2126 if (!(ubifs_chk_flags & UBIFS_CHK_FS))
2127 return 0;
2128
2129 fsckd.inodes = RB_ROOT;
2130 err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
2131 if (err)
2132 goto out_free;
2133
2134 err = check_inodes(c, &fsckd);
2135 if (err)
2136 goto out_free;
2137
2138 free_inodes(&fsckd);
2139 return 0;
2140
2141out_free:
2142 ubifs_err("file-system check failed with error %d", err);
2143 dump_stack();
2144 free_inodes(&fsckd);
2145 return err;
2146}
2147
2148static int invocation_cnt;
2149
2150int dbg_force_in_the_gaps(void)
2151{
2152 if (!dbg_force_in_the_gaps_enabled)
2153 return 0;
2154 /* Force in-the-gaps every 8th commit */
2155 return !((invocation_cnt++) & 0x7);
2156}
2157
2158/* Failure mode for recovery testing */
2159
2160#define chance(n, d) (simple_rand() <= (n) * 32768LL / (d))
2161
2162struct failure_mode_info {
2163 struct list_head list;
2164 struct ubifs_info *c;
2165};
2166
2167static LIST_HEAD(fmi_list);
2168static DEFINE_SPINLOCK(fmi_lock);
2169
2170static unsigned int next;
2171
2172static int simple_rand(void)
2173{
2174 if (next == 0)
2175 next = current->pid;
2176 next = next * 1103515245 + 12345;
2177 return (next >> 16) & 32767;
2178}
2179
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002180static void failure_mode_init(struct ubifs_info *c)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002181{
2182 struct failure_mode_info *fmi;
2183
2184 fmi = kmalloc(sizeof(struct failure_mode_info), GFP_NOFS);
2185 if (!fmi) {
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002186 ubifs_err("Failed to register failure mode - no memory");
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002187 return;
2188 }
2189 fmi->c = c;
2190 spin_lock(&fmi_lock);
2191 list_add_tail(&fmi->list, &fmi_list);
2192 spin_unlock(&fmi_lock);
2193}
2194
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002195static void failure_mode_exit(struct ubifs_info *c)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002196{
2197 struct failure_mode_info *fmi, *tmp;
2198
2199 spin_lock(&fmi_lock);
2200 list_for_each_entry_safe(fmi, tmp, &fmi_list, list)
2201 if (fmi->c == c) {
2202 list_del(&fmi->list);
2203 kfree(fmi);
2204 }
2205 spin_unlock(&fmi_lock);
2206}
2207
2208static struct ubifs_info *dbg_find_info(struct ubi_volume_desc *desc)
2209{
2210 struct failure_mode_info *fmi;
2211
2212 spin_lock(&fmi_lock);
2213 list_for_each_entry(fmi, &fmi_list, list)
2214 if (fmi->c->ubi == desc) {
2215 struct ubifs_info *c = fmi->c;
2216
2217 spin_unlock(&fmi_lock);
2218 return c;
2219 }
2220 spin_unlock(&fmi_lock);
2221 return NULL;
2222}
2223
2224static int in_failure_mode(struct ubi_volume_desc *desc)
2225{
2226 struct ubifs_info *c = dbg_find_info(desc);
2227
2228 if (c && dbg_failure_mode)
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002229 return c->dbg->failure_mode;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002230 return 0;
2231}
2232
2233static int do_fail(struct ubi_volume_desc *desc, int lnum, int write)
2234{
2235 struct ubifs_info *c = dbg_find_info(desc);
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002236 struct ubifs_debug_info *d;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002237
2238 if (!c || !dbg_failure_mode)
2239 return 0;
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002240 d = c->dbg;
2241 if (d->failure_mode)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002242 return 1;
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002243 if (!d->fail_cnt) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002244 /* First call - decide delay to failure */
2245 if (chance(1, 2)) {
2246 unsigned int delay = 1 << (simple_rand() >> 11);
2247
2248 if (chance(1, 2)) {
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002249 d->fail_delay = 1;
2250 d->fail_timeout = jiffies +
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002251 msecs_to_jiffies(delay);
2252 dbg_rcvry("failing after %ums", delay);
2253 } else {
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002254 d->fail_delay = 2;
2255 d->fail_cnt_max = delay;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002256 dbg_rcvry("failing after %u calls", delay);
2257 }
2258 }
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002259 d->fail_cnt += 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002260 }
2261 /* Determine if failure delay has expired */
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002262 if (d->fail_delay == 1) {
2263 if (time_before(jiffies, d->fail_timeout))
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002264 return 0;
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002265 } else if (d->fail_delay == 2)
2266 if (d->fail_cnt++ < d->fail_cnt_max)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002267 return 0;
2268 if (lnum == UBIFS_SB_LNUM) {
2269 if (write) {
2270 if (chance(1, 2))
2271 return 0;
2272 } else if (chance(19, 20))
2273 return 0;
2274 dbg_rcvry("failing in super block LEB %d", lnum);
2275 } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2276 if (chance(19, 20))
2277 return 0;
2278 dbg_rcvry("failing in master LEB %d", lnum);
2279 } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2280 if (write) {
2281 if (chance(99, 100))
2282 return 0;
2283 } else if (chance(399, 400))
2284 return 0;
2285 dbg_rcvry("failing in log LEB %d", lnum);
2286 } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2287 if (write) {
2288 if (chance(7, 8))
2289 return 0;
2290 } else if (chance(19, 20))
2291 return 0;
2292 dbg_rcvry("failing in LPT LEB %d", lnum);
2293 } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2294 if (write) {
2295 if (chance(1, 2))
2296 return 0;
2297 } else if (chance(9, 10))
2298 return 0;
2299 dbg_rcvry("failing in orphan LEB %d", lnum);
2300 } else if (lnum == c->ihead_lnum) {
2301 if (chance(99, 100))
2302 return 0;
2303 dbg_rcvry("failing in index head LEB %d", lnum);
2304 } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2305 if (chance(9, 10))
2306 return 0;
2307 dbg_rcvry("failing in GC head LEB %d", lnum);
2308 } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
2309 !ubifs_search_bud(c, lnum)) {
2310 if (chance(19, 20))
2311 return 0;
2312 dbg_rcvry("failing in non-bud LEB %d", lnum);
2313 } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
2314 c->cmt_state == COMMIT_RUNNING_REQUIRED) {
2315 if (chance(999, 1000))
2316 return 0;
2317 dbg_rcvry("failing in bud LEB %d commit running", lnum);
2318 } else {
2319 if (chance(9999, 10000))
2320 return 0;
2321 dbg_rcvry("failing in bud LEB %d commit not running", lnum);
2322 }
2323 ubifs_err("*** SETTING FAILURE MODE ON (LEB %d) ***", lnum);
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002324 d->failure_mode = 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002325 dump_stack();
2326 return 1;
2327}
2328
2329static void cut_data(const void *buf, int len)
2330{
2331 int flen, i;
2332 unsigned char *p = (void *)buf;
2333
2334 flen = (len * (long long)simple_rand()) >> 15;
2335 for (i = flen; i < len; i++)
2336 p[i] = 0xff;
2337}
2338
2339int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
2340 int len, int check)
2341{
2342 if (in_failure_mode(desc))
2343 return -EIO;
2344 return ubi_leb_read(desc, lnum, buf, offset, len, check);
2345}
2346
2347int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
2348 int offset, int len, int dtype)
2349{
Adrian Hunter16dfd802008-07-18 16:47:41 +03002350 int err, failing;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002351
2352 if (in_failure_mode(desc))
2353 return -EIO;
Adrian Hunter16dfd802008-07-18 16:47:41 +03002354 failing = do_fail(desc, lnum, 1);
2355 if (failing)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002356 cut_data(buf, len);
2357 err = ubi_leb_write(desc, lnum, buf, offset, len, dtype);
2358 if (err)
2359 return err;
Adrian Hunter16dfd802008-07-18 16:47:41 +03002360 if (failing)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002361 return -EIO;
2362 return 0;
2363}
2364
2365int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
2366 int len, int dtype)
2367{
2368 int err;
2369
2370 if (do_fail(desc, lnum, 1))
2371 return -EIO;
2372 err = ubi_leb_change(desc, lnum, buf, len, dtype);
2373 if (err)
2374 return err;
2375 if (do_fail(desc, lnum, 1))
2376 return -EIO;
2377 return 0;
2378}
2379
2380int dbg_leb_erase(struct ubi_volume_desc *desc, int lnum)
2381{
2382 int err;
2383
2384 if (do_fail(desc, lnum, 0))
2385 return -EIO;
2386 err = ubi_leb_erase(desc, lnum);
2387 if (err)
2388 return err;
2389 if (do_fail(desc, lnum, 0))
2390 return -EIO;
2391 return 0;
2392}
2393
2394int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum)
2395{
2396 int err;
2397
2398 if (do_fail(desc, lnum, 0))
2399 return -EIO;
2400 err = ubi_leb_unmap(desc, lnum);
2401 if (err)
2402 return err;
2403 if (do_fail(desc, lnum, 0))
2404 return -EIO;
2405 return 0;
2406}
2407
2408int dbg_is_mapped(struct ubi_volume_desc *desc, int lnum)
2409{
2410 if (in_failure_mode(desc))
2411 return -EIO;
2412 return ubi_is_mapped(desc, lnum);
2413}
2414
2415int dbg_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
2416{
2417 int err;
2418
2419 if (do_fail(desc, lnum, 0))
2420 return -EIO;
2421 err = ubi_leb_map(desc, lnum, dtype);
2422 if (err)
2423 return err;
2424 if (do_fail(desc, lnum, 0))
2425 return -EIO;
2426 return 0;
2427}
2428
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002429/**
2430 * ubifs_debugging_init - initialize UBIFS debugging.
2431 * @c: UBIFS file-system description object
2432 *
2433 * This function initializes debugging-related data for the file system.
2434 * Returns zero in case of success and a negative error code in case of
2435 * failure.
2436 */
2437int ubifs_debugging_init(struct ubifs_info *c)
2438{
2439 c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
2440 if (!c->dbg)
2441 return -ENOMEM;
2442
2443 c->dbg->buf = vmalloc(c->leb_size);
2444 if (!c->dbg->buf)
2445 goto out;
2446
2447 failure_mode_init(c);
2448 return 0;
2449
2450out:
2451 kfree(c->dbg);
2452 return -ENOMEM;
2453}
2454
2455/**
2456 * ubifs_debugging_exit - free debugging data.
2457 * @c: UBIFS file-system description object
2458 */
2459void ubifs_debugging_exit(struct ubifs_info *c)
2460{
2461 failure_mode_exit(c);
2462 vfree(c->dbg->buf);
2463 kfree(c->dbg);
2464}
2465
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002466/*
2467 * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
2468 * contain the stuff specific to particular file-system mounts.
2469 */
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002470static struct dentry *dfs_rootdir;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002471
2472/**
2473 * dbg_debugfs_init - initialize debugfs file-system.
2474 *
2475 * UBIFS uses debugfs file-system to expose various debugging knobs to
2476 * user-space. This function creates "ubifs" directory in the debugfs
2477 * file-system. Returns zero in case of success and a negative error code in
2478 * case of failure.
2479 */
2480int dbg_debugfs_init(void)
2481{
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002482 dfs_rootdir = debugfs_create_dir("ubifs", NULL);
2483 if (IS_ERR(dfs_rootdir)) {
2484 int err = PTR_ERR(dfs_rootdir);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002485 ubifs_err("cannot create \"ubifs\" debugfs directory, "
2486 "error %d\n", err);
2487 return err;
2488 }
2489
2490 return 0;
2491}
2492
2493/**
2494 * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
2495 */
2496void dbg_debugfs_exit(void)
2497{
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002498 debugfs_remove(dfs_rootdir);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002499}
2500
2501static int open_debugfs_file(struct inode *inode, struct file *file)
2502{
2503 file->private_data = inode->i_private;
2504 return 0;
2505}
2506
2507static ssize_t write_debugfs_file(struct file *file, const char __user *buf,
2508 size_t count, loff_t *ppos)
2509{
2510 struct ubifs_info *c = file->private_data;
2511 struct ubifs_debug_info *d = c->dbg;
2512
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002513 if (file->f_path.dentry == d->dfs_dump_lprops)
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002514 dbg_dump_lprops(c);
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002515 else if (file->f_path.dentry == d->dfs_dump_budg) {
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002516 spin_lock(&c->space_lock);
2517 dbg_dump_budg(c);
2518 spin_unlock(&c->space_lock);
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002519 } else if (file->f_path.dentry == d->dfs_dump_tnc) {
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002520 mutex_lock(&c->tnc_mutex);
2521 dbg_dump_tnc(c);
2522 mutex_unlock(&c->tnc_mutex);
2523 } else
2524 return -EINVAL;
2525
2526 *ppos += count;
2527 return count;
2528}
2529
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002530static const struct file_operations dfs_fops = {
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002531 .open = open_debugfs_file,
2532 .write = write_debugfs_file,
2533 .owner = THIS_MODULE,
2534};
2535
2536/**
2537 * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
2538 * @c: UBIFS file-system description object
2539 *
2540 * This function creates all debugfs files for this instance of UBIFS. Returns
2541 * zero in case of success and a negative error code in case of failure.
2542 *
2543 * Note, the only reason we have not merged this function with the
2544 * 'ubifs_debugging_init()' function is because it is better to initialize
2545 * debugfs interfaces at the very end of the mount process, and remove them at
2546 * the very beginning of the mount process.
2547 */
2548int dbg_debugfs_init_fs(struct ubifs_info *c)
2549{
2550 int err;
2551 const char *fname;
2552 struct dentry *dent;
2553 struct ubifs_debug_info *d = c->dbg;
2554
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002555 sprintf(d->dfs_dir_name, "ubi%d_%d", c->vi.ubi_num, c->vi.vol_id);
2556 d->dfs_dir = debugfs_create_dir(d->dfs_dir_name, dfs_rootdir);
2557 if (IS_ERR(d->dfs_dir)) {
2558 err = PTR_ERR(d->dfs_dir);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002559 ubifs_err("cannot create \"%s\" debugfs directory, error %d\n",
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002560 d->dfs_dir_name, err);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002561 goto out;
2562 }
2563
2564 fname = "dump_lprops";
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002565 dent = debugfs_create_file(fname, S_IWUGO, d->dfs_dir, c, &dfs_fops);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002566 if (IS_ERR(dent))
2567 goto out_remove;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002568 d->dfs_dump_lprops = dent;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002569
2570 fname = "dump_budg";
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002571 dent = debugfs_create_file(fname, S_IWUGO, d->dfs_dir, c, &dfs_fops);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002572 if (IS_ERR(dent))
2573 goto out_remove;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002574 d->dfs_dump_budg = dent;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002575
2576 fname = "dump_tnc";
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002577 dent = debugfs_create_file(fname, S_IWUGO, d->dfs_dir, c, &dfs_fops);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002578 if (IS_ERR(dent))
2579 goto out_remove;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002580 d->dfs_dump_tnc = dent;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002581
2582 return 0;
2583
2584out_remove:
2585 err = PTR_ERR(dent);
2586 ubifs_err("cannot create \"%s\" debugfs directory, error %d\n",
2587 fname, err);
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002588 debugfs_remove_recursive(d->dfs_dir);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002589out:
2590 return err;
2591}
2592
2593/**
2594 * dbg_debugfs_exit_fs - remove all debugfs files.
2595 * @c: UBIFS file-system description object
2596 */
2597void dbg_debugfs_exit_fs(struct ubifs_info *c)
2598{
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002599 debugfs_remove_recursive(c->dbg->dfs_dir);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002600}
2601
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002602#endif /* CONFIG_UBIFS_FS_DEBUG */