blob: b5ba2ea5b6269b8cb7ae0394a51144a2e55e0b76 [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
Artem Bityutskiycce3f612011-03-09 13:36:23 +020045unsigned int ubifs_msg_flags;
46unsigned int ubifs_chk_flags;
Artem Bityutskiy1e517642008-07-14 19:08:37 +030047unsigned 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));
Joe Perches7f2f4e72009-12-14 18:01:13 -0800353 printk(KERN_DEBUG "\tUUID %pUB\n",
354 sup->uuid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300355 break;
356 }
357 case UBIFS_MST_NODE:
358 {
359 const struct ubifs_mst_node *mst = node;
360
361 printk(KERN_DEBUG "\thighest_inum %llu\n",
362 (unsigned long long)le64_to_cpu(mst->highest_inum));
363 printk(KERN_DEBUG "\tcommit number %llu\n",
364 (unsigned long long)le64_to_cpu(mst->cmt_no));
365 printk(KERN_DEBUG "\tflags %#x\n",
366 le32_to_cpu(mst->flags));
367 printk(KERN_DEBUG "\tlog_lnum %u\n",
368 le32_to_cpu(mst->log_lnum));
369 printk(KERN_DEBUG "\troot_lnum %u\n",
370 le32_to_cpu(mst->root_lnum));
371 printk(KERN_DEBUG "\troot_offs %u\n",
372 le32_to_cpu(mst->root_offs));
373 printk(KERN_DEBUG "\troot_len %u\n",
374 le32_to_cpu(mst->root_len));
375 printk(KERN_DEBUG "\tgc_lnum %u\n",
376 le32_to_cpu(mst->gc_lnum));
377 printk(KERN_DEBUG "\tihead_lnum %u\n",
378 le32_to_cpu(mst->ihead_lnum));
379 printk(KERN_DEBUG "\tihead_offs %u\n",
380 le32_to_cpu(mst->ihead_offs));
Harvey Harrison0ecb9522008-10-24 10:52:57 -0700381 printk(KERN_DEBUG "\tindex_size %llu\n",
382 (unsigned long long)le64_to_cpu(mst->index_size));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300383 printk(KERN_DEBUG "\tlpt_lnum %u\n",
384 le32_to_cpu(mst->lpt_lnum));
385 printk(KERN_DEBUG "\tlpt_offs %u\n",
386 le32_to_cpu(mst->lpt_offs));
387 printk(KERN_DEBUG "\tnhead_lnum %u\n",
388 le32_to_cpu(mst->nhead_lnum));
389 printk(KERN_DEBUG "\tnhead_offs %u\n",
390 le32_to_cpu(mst->nhead_offs));
391 printk(KERN_DEBUG "\tltab_lnum %u\n",
392 le32_to_cpu(mst->ltab_lnum));
393 printk(KERN_DEBUG "\tltab_offs %u\n",
394 le32_to_cpu(mst->ltab_offs));
395 printk(KERN_DEBUG "\tlsave_lnum %u\n",
396 le32_to_cpu(mst->lsave_lnum));
397 printk(KERN_DEBUG "\tlsave_offs %u\n",
398 le32_to_cpu(mst->lsave_offs));
399 printk(KERN_DEBUG "\tlscan_lnum %u\n",
400 le32_to_cpu(mst->lscan_lnum));
401 printk(KERN_DEBUG "\tleb_cnt %u\n",
402 le32_to_cpu(mst->leb_cnt));
403 printk(KERN_DEBUG "\tempty_lebs %u\n",
404 le32_to_cpu(mst->empty_lebs));
405 printk(KERN_DEBUG "\tidx_lebs %u\n",
406 le32_to_cpu(mst->idx_lebs));
407 printk(KERN_DEBUG "\ttotal_free %llu\n",
408 (unsigned long long)le64_to_cpu(mst->total_free));
409 printk(KERN_DEBUG "\ttotal_dirty %llu\n",
410 (unsigned long long)le64_to_cpu(mst->total_dirty));
411 printk(KERN_DEBUG "\ttotal_used %llu\n",
412 (unsigned long long)le64_to_cpu(mst->total_used));
413 printk(KERN_DEBUG "\ttotal_dead %llu\n",
414 (unsigned long long)le64_to_cpu(mst->total_dead));
415 printk(KERN_DEBUG "\ttotal_dark %llu\n",
416 (unsigned long long)le64_to_cpu(mst->total_dark));
417 break;
418 }
419 case UBIFS_REF_NODE:
420 {
421 const struct ubifs_ref_node *ref = node;
422
423 printk(KERN_DEBUG "\tlnum %u\n",
424 le32_to_cpu(ref->lnum));
425 printk(KERN_DEBUG "\toffs %u\n",
426 le32_to_cpu(ref->offs));
427 printk(KERN_DEBUG "\tjhead %u\n",
428 le32_to_cpu(ref->jhead));
429 break;
430 }
431 case UBIFS_INO_NODE:
432 {
433 const struct ubifs_ino_node *ino = node;
434
435 key_read(c, &ino->key, &key);
436 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
437 printk(KERN_DEBUG "\tcreat_sqnum %llu\n",
438 (unsigned long long)le64_to_cpu(ino->creat_sqnum));
439 printk(KERN_DEBUG "\tsize %llu\n",
440 (unsigned long long)le64_to_cpu(ino->size));
441 printk(KERN_DEBUG "\tnlink %u\n",
442 le32_to_cpu(ino->nlink));
443 printk(KERN_DEBUG "\tatime %lld.%u\n",
444 (long long)le64_to_cpu(ino->atime_sec),
445 le32_to_cpu(ino->atime_nsec));
446 printk(KERN_DEBUG "\tmtime %lld.%u\n",
447 (long long)le64_to_cpu(ino->mtime_sec),
448 le32_to_cpu(ino->mtime_nsec));
449 printk(KERN_DEBUG "\tctime %lld.%u\n",
450 (long long)le64_to_cpu(ino->ctime_sec),
451 le32_to_cpu(ino->ctime_nsec));
452 printk(KERN_DEBUG "\tuid %u\n",
453 le32_to_cpu(ino->uid));
454 printk(KERN_DEBUG "\tgid %u\n",
455 le32_to_cpu(ino->gid));
456 printk(KERN_DEBUG "\tmode %u\n",
457 le32_to_cpu(ino->mode));
458 printk(KERN_DEBUG "\tflags %#x\n",
459 le32_to_cpu(ino->flags));
460 printk(KERN_DEBUG "\txattr_cnt %u\n",
461 le32_to_cpu(ino->xattr_cnt));
462 printk(KERN_DEBUG "\txattr_size %u\n",
463 le32_to_cpu(ino->xattr_size));
464 printk(KERN_DEBUG "\txattr_names %u\n",
465 le32_to_cpu(ino->xattr_names));
466 printk(KERN_DEBUG "\tcompr_type %#x\n",
467 (int)le16_to_cpu(ino->compr_type));
468 printk(KERN_DEBUG "\tdata len %u\n",
469 le32_to_cpu(ino->data_len));
470 break;
471 }
472 case UBIFS_DENT_NODE:
473 case UBIFS_XENT_NODE:
474 {
475 const struct ubifs_dent_node *dent = node;
476 int nlen = le16_to_cpu(dent->nlen);
477
478 key_read(c, &dent->key, &key);
479 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
480 printk(KERN_DEBUG "\tinum %llu\n",
481 (unsigned long long)le64_to_cpu(dent->inum));
482 printk(KERN_DEBUG "\ttype %d\n", (int)dent->type);
483 printk(KERN_DEBUG "\tnlen %d\n", nlen);
484 printk(KERN_DEBUG "\tname ");
485
486 if (nlen > UBIFS_MAX_NLEN)
487 printk(KERN_DEBUG "(bad name length, not printing, "
488 "bad or corrupted node)");
489 else {
490 for (i = 0; i < nlen && dent->name[i]; i++)
Artem Bityutskiyc9927c32009-03-16 09:42:03 +0200491 printk(KERN_CONT "%c", dent->name[i]);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300492 }
Artem Bityutskiyc9927c32009-03-16 09:42:03 +0200493 printk(KERN_CONT "\n");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300494
495 break;
496 }
497 case UBIFS_DATA_NODE:
498 {
499 const struct ubifs_data_node *dn = node;
500 int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
501
502 key_read(c, &dn->key, &key);
503 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
504 printk(KERN_DEBUG "\tsize %u\n",
505 le32_to_cpu(dn->size));
506 printk(KERN_DEBUG "\tcompr_typ %d\n",
507 (int)le16_to_cpu(dn->compr_type));
508 printk(KERN_DEBUG "\tdata size %d\n",
509 dlen);
510 printk(KERN_DEBUG "\tdata:\n");
511 print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET, 32, 1,
512 (void *)&dn->data, dlen, 0);
513 break;
514 }
515 case UBIFS_TRUN_NODE:
516 {
517 const struct ubifs_trun_node *trun = node;
518
519 printk(KERN_DEBUG "\tinum %u\n",
520 le32_to_cpu(trun->inum));
521 printk(KERN_DEBUG "\told_size %llu\n",
522 (unsigned long long)le64_to_cpu(trun->old_size));
523 printk(KERN_DEBUG "\tnew_size %llu\n",
524 (unsigned long long)le64_to_cpu(trun->new_size));
525 break;
526 }
527 case UBIFS_IDX_NODE:
528 {
529 const struct ubifs_idx_node *idx = node;
530
531 n = le16_to_cpu(idx->child_cnt);
532 printk(KERN_DEBUG "\tchild_cnt %d\n", n);
533 printk(KERN_DEBUG "\tlevel %d\n",
534 (int)le16_to_cpu(idx->level));
535 printk(KERN_DEBUG "\tBranches:\n");
536
537 for (i = 0; i < n && i < c->fanout - 1; i++) {
538 const struct ubifs_branch *br;
539
540 br = ubifs_idx_branch(c, idx, i);
541 key_read(c, &br->key, &key);
542 printk(KERN_DEBUG "\t%d: LEB %d:%d len %d key %s\n",
543 i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
544 le32_to_cpu(br->len), DBGKEY(&key));
545 }
546 break;
547 }
548 case UBIFS_CS_NODE:
549 break;
550 case UBIFS_ORPH_NODE:
551 {
552 const struct ubifs_orph_node *orph = node;
553
554 printk(KERN_DEBUG "\tcommit number %llu\n",
555 (unsigned long long)
556 le64_to_cpu(orph->cmt_no) & LLONG_MAX);
557 printk(KERN_DEBUG "\tlast node flag %llu\n",
558 (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
559 n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
560 printk(KERN_DEBUG "\t%d orphan inode numbers:\n", n);
561 for (i = 0; i < n; i++)
562 printk(KERN_DEBUG "\t ino %llu\n",
Alexander Beregalov7424bac2008-09-17 22:09:41 +0400563 (unsigned long long)le64_to_cpu(orph->inos[i]));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300564 break;
565 }
566 default:
567 printk(KERN_DEBUG "node type %d was not recognized\n",
568 (int)ch->node_type);
569 }
570 spin_unlock(&dbg_lock);
571}
572
573void dbg_dump_budget_req(const struct ubifs_budget_req *req)
574{
575 spin_lock(&dbg_lock);
576 printk(KERN_DEBUG "Budgeting request: new_ino %d, dirtied_ino %d\n",
577 req->new_ino, req->dirtied_ino);
578 printk(KERN_DEBUG "\tnew_ino_d %d, dirtied_ino_d %d\n",
579 req->new_ino_d, req->dirtied_ino_d);
580 printk(KERN_DEBUG "\tnew_page %d, dirtied_page %d\n",
581 req->new_page, req->dirtied_page);
582 printk(KERN_DEBUG "\tnew_dent %d, mod_dent %d\n",
583 req->new_dent, req->mod_dent);
584 printk(KERN_DEBUG "\tidx_growth %d\n", req->idx_growth);
585 printk(KERN_DEBUG "\tdata_growth %d dd_growth %d\n",
586 req->data_growth, req->dd_growth);
587 spin_unlock(&dbg_lock);
588}
589
590void dbg_dump_lstats(const struct ubifs_lp_stats *lst)
591{
592 spin_lock(&dbg_lock);
Artem Bityutskiy1de94152008-07-25 12:58:38 +0300593 printk(KERN_DEBUG "(pid %d) Lprops statistics: empty_lebs %d, "
594 "idx_lebs %d\n", current->pid, lst->empty_lebs, lst->idx_lebs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300595 printk(KERN_DEBUG "\ttaken_empty_lebs %d, total_free %lld, "
596 "total_dirty %lld\n", lst->taken_empty_lebs, lst->total_free,
597 lst->total_dirty);
598 printk(KERN_DEBUG "\ttotal_used %lld, total_dark %lld, "
599 "total_dead %lld\n", lst->total_used, lst->total_dark,
600 lst->total_dead);
601 spin_unlock(&dbg_lock);
602}
603
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300604void dbg_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300605{
606 int i;
607 struct rb_node *rb;
608 struct ubifs_bud *bud;
609 struct ubifs_gced_idx_leb *idx_gc;
Artem Bityutskiy21a60252008-12-12 11:13:17 -0500610 long long available, outstanding, free;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300611
Artem Bityutskiy8ff83082011-03-29 18:19:50 +0300612 spin_lock(&c->space_lock);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300613 spin_lock(&dbg_lock);
Artem Bityutskiy8c3067e2011-03-30 13:18:58 +0300614 printk(KERN_DEBUG "(pid %d) Budgeting info: data budget sum %lld, "
615 "total budget sum %lld\n", current->pid,
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300616 bi->data_growth + bi->dd_growth,
617 bi->data_growth + bi->dd_growth + bi->idx_growth);
Artem Bityutskiy8c3067e2011-03-30 13:18:58 +0300618 printk(KERN_DEBUG "\tbudg_data_growth %lld, budg_dd_growth %lld, "
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300619 "budg_idx_growth %lld\n", bi->data_growth, bi->dd_growth,
620 bi->idx_growth);
Artem Bityutskiy8c3067e2011-03-30 13:18:58 +0300621 printk(KERN_DEBUG "\tmin_idx_lebs %d, old_idx_sz %llu, "
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300622 "uncommitted_idx %lld\n", bi->min_idx_lebs, bi->old_idx_sz,
623 bi->uncommitted_idx);
Artem Bityutskiy8c3067e2011-03-30 13:18:58 +0300624 printk(KERN_DEBUG "\tpage_budget %d, inode_budget %d, dent_budget %d\n",
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300625 bi->page_budget, bi->inode_budget, bi->dent_budget);
Artem Bityutskiy8c3067e2011-03-30 13:18:58 +0300626 printk(KERN_DEBUG "\tnospace %u, nospace_rp %u\n",
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300627 bi->nospace, bi->nospace_rp);
Artem Bityutskiy8c3067e2011-03-30 13:18:58 +0300628 printk(KERN_DEBUG "\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
629 c->dark_wm, c->dead_wm, c->max_idx_node_sz);
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300630
631 if (bi != &c->bi)
632 /*
633 * If we are dumping saved budgeting data, do not print
634 * additional information which is about the current state, not
635 * the old one which corresponded to the saved budgeting data.
636 */
637 goto out_unlock;
638
Artem Bityutskiy8c3067e2011-03-30 13:18:58 +0300639 printk(KERN_DEBUG "\tfreeable_cnt %d, calc_idx_sz %lld, idx_gc_cnt %d\n",
640 c->freeable_cnt, c->calc_idx_sz, c->idx_gc_cnt);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300641 printk(KERN_DEBUG "\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, "
642 "clean_zn_cnt %ld\n", atomic_long_read(&c->dirty_pg_cnt),
643 atomic_long_read(&c->dirty_zn_cnt),
644 atomic_long_read(&c->clean_zn_cnt));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300645 printk(KERN_DEBUG "\tgc_lnum %d, ihead_lnum %d\n",
646 c->gc_lnum, c->ihead_lnum);
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300647
Artem Bityutskiy84abf972009-01-23 14:54:59 +0200648 /* If we are in R/O mode, journal heads do not exist */
649 if (c->jheads)
650 for (i = 0; i < c->jhead_cnt; i++)
Artem Bityutskiy77a7ae52009-09-15 15:03:51 +0300651 printk(KERN_DEBUG "\tjhead %s\t LEB %d\n",
652 dbg_jhead(c->jheads[i].wbuf.jhead),
653 c->jheads[i].wbuf.lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300654 for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
655 bud = rb_entry(rb, struct ubifs_bud, rb);
656 printk(KERN_DEBUG "\tbud LEB %d\n", bud->lnum);
657 }
658 list_for_each_entry(bud, &c->old_buds, list)
659 printk(KERN_DEBUG "\told bud LEB %d\n", bud->lnum);
660 list_for_each_entry(idx_gc, &c->idx_gc, list)
661 printk(KERN_DEBUG "\tGC'ed idx LEB %d unmap %d\n",
662 idx_gc->lnum, idx_gc->unmap);
663 printk(KERN_DEBUG "\tcommit state %d\n", c->cmt_state);
Artem Bityutskiy21a60252008-12-12 11:13:17 -0500664
665 /* Print budgeting predictions */
Artem Bityutskiyb1375452011-03-29 18:04:05 +0300666 available = ubifs_calc_available(c, c->bi.min_idx_lebs);
667 outstanding = c->bi.data_growth + c->bi.dd_growth;
Artem Bityutskiy84abf972009-01-23 14:54:59 +0200668 free = ubifs_get_free_space_nolock(c);
Artem Bityutskiy21a60252008-12-12 11:13:17 -0500669 printk(KERN_DEBUG "Budgeting predictions:\n");
670 printk(KERN_DEBUG "\tavailable: %lld, outstanding %lld, free %lld\n",
671 available, outstanding, free);
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300672out_unlock:
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300673 spin_unlock(&dbg_lock);
Artem Bityutskiy8ff83082011-03-29 18:19:50 +0300674 spin_unlock(&c->space_lock);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300675}
676
677void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
678{
Artem Bityutskiybe9e62a2008-12-28 10:17:23 +0200679 int i, spc, dark = 0, dead = 0;
680 struct rb_node *rb;
681 struct ubifs_bud *bud;
682
683 spc = lp->free + lp->dirty;
684 if (spc < c->dead_wm)
685 dead = spc;
686 else
687 dark = ubifs_calc_dark(c, spc);
688
689 if (lp->flags & LPROPS_INDEX)
690 printk(KERN_DEBUG "LEB %-7d free %-8d dirty %-8d used %-8d "
691 "free + dirty %-8d flags %#x (", lp->lnum, lp->free,
692 lp->dirty, c->leb_size - spc, spc, lp->flags);
693 else
694 printk(KERN_DEBUG "LEB %-7d free %-8d dirty %-8d used %-8d "
695 "free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d "
696 "flags %#-4x (", lp->lnum, lp->free, lp->dirty,
697 c->leb_size - spc, spc, dark, dead,
698 (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags);
699
700 if (lp->flags & LPROPS_TAKEN) {
701 if (lp->flags & LPROPS_INDEX)
702 printk(KERN_CONT "index, taken");
703 else
704 printk(KERN_CONT "taken");
705 } else {
706 const char *s;
707
708 if (lp->flags & LPROPS_INDEX) {
709 switch (lp->flags & LPROPS_CAT_MASK) {
710 case LPROPS_DIRTY_IDX:
711 s = "dirty index";
712 break;
713 case LPROPS_FRDI_IDX:
714 s = "freeable index";
715 break;
716 default:
717 s = "index";
718 }
719 } else {
720 switch (lp->flags & LPROPS_CAT_MASK) {
721 case LPROPS_UNCAT:
722 s = "not categorized";
723 break;
724 case LPROPS_DIRTY:
725 s = "dirty";
726 break;
727 case LPROPS_FREE:
728 s = "free";
729 break;
730 case LPROPS_EMPTY:
731 s = "empty";
732 break;
733 case LPROPS_FREEABLE:
734 s = "freeable";
735 break;
736 default:
737 s = NULL;
738 break;
739 }
740 }
741 printk(KERN_CONT "%s", s);
742 }
743
744 for (rb = rb_first((struct rb_root *)&c->buds); rb; rb = rb_next(rb)) {
745 bud = rb_entry(rb, struct ubifs_bud, rb);
746 if (bud->lnum == lp->lnum) {
747 int head = 0;
748 for (i = 0; i < c->jhead_cnt; i++) {
Artem Bityutskiy13216572011-04-24 10:53:17 +0300749 /*
750 * Note, if we are in R/O mode or in the middle
751 * of mounting/re-mounting, the write-buffers do
752 * not exist.
753 */
754 if (c->jheads &&
755 lp->lnum == c->jheads[i].wbuf.lnum) {
Artem Bityutskiybe9e62a2008-12-28 10:17:23 +0200756 printk(KERN_CONT ", jhead %s",
757 dbg_jhead(i));
758 head = 1;
759 }
760 }
761 if (!head)
762 printk(KERN_CONT ", bud of jhead %s",
763 dbg_jhead(bud->jhead));
764 }
765 }
766 if (lp->lnum == c->gc_lnum)
767 printk(KERN_CONT ", GC LEB");
768 printk(KERN_CONT ")\n");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300769}
770
771void dbg_dump_lprops(struct ubifs_info *c)
772{
773 int lnum, err;
774 struct ubifs_lprops lp;
775 struct ubifs_lp_stats lst;
776
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200777 printk(KERN_DEBUG "(pid %d) start dumping LEB properties\n",
778 current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300779 ubifs_get_lp_stats(c, &lst);
780 dbg_dump_lstats(&lst);
781
782 for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
783 err = ubifs_read_one_lp(c, lnum, &lp);
784 if (err)
785 ubifs_err("cannot read lprops for LEB %d", lnum);
786
787 dbg_dump_lprop(c, &lp);
788 }
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200789 printk(KERN_DEBUG "(pid %d) finish dumping LEB properties\n",
790 current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300791}
792
Adrian Hunter73944a62008-09-12 18:13:31 +0300793void dbg_dump_lpt_info(struct ubifs_info *c)
794{
795 int i;
796
797 spin_lock(&dbg_lock);
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200798 printk(KERN_DEBUG "(pid %d) dumping LPT information\n", current->pid);
Adrian Hunter73944a62008-09-12 18:13:31 +0300799 printk(KERN_DEBUG "\tlpt_sz: %lld\n", c->lpt_sz);
800 printk(KERN_DEBUG "\tpnode_sz: %d\n", c->pnode_sz);
801 printk(KERN_DEBUG "\tnnode_sz: %d\n", c->nnode_sz);
802 printk(KERN_DEBUG "\tltab_sz: %d\n", c->ltab_sz);
803 printk(KERN_DEBUG "\tlsave_sz: %d\n", c->lsave_sz);
804 printk(KERN_DEBUG "\tbig_lpt: %d\n", c->big_lpt);
805 printk(KERN_DEBUG "\tlpt_hght: %d\n", c->lpt_hght);
806 printk(KERN_DEBUG "\tpnode_cnt: %d\n", c->pnode_cnt);
807 printk(KERN_DEBUG "\tnnode_cnt: %d\n", c->nnode_cnt);
808 printk(KERN_DEBUG "\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt);
809 printk(KERN_DEBUG "\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt);
810 printk(KERN_DEBUG "\tlsave_cnt: %d\n", c->lsave_cnt);
811 printk(KERN_DEBUG "\tspace_bits: %d\n", c->space_bits);
812 printk(KERN_DEBUG "\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
813 printk(KERN_DEBUG "\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
814 printk(KERN_DEBUG "\tlpt_spc_bits: %d\n", c->lpt_spc_bits);
815 printk(KERN_DEBUG "\tpcnt_bits: %d\n", c->pcnt_bits);
816 printk(KERN_DEBUG "\tlnum_bits: %d\n", c->lnum_bits);
817 printk(KERN_DEBUG "\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
818 printk(KERN_DEBUG "\tLPT head is at %d:%d\n",
819 c->nhead_lnum, c->nhead_offs);
Artem Bityutskiyf92b9822008-12-28 11:34:26 +0200820 printk(KERN_DEBUG "\tLPT ltab is at %d:%d\n",
821 c->ltab_lnum, c->ltab_offs);
Adrian Hunter73944a62008-09-12 18:13:31 +0300822 if (c->big_lpt)
823 printk(KERN_DEBUG "\tLPT lsave is at %d:%d\n",
824 c->lsave_lnum, c->lsave_offs);
825 for (i = 0; i < c->lpt_lebs; i++)
826 printk(KERN_DEBUG "\tLPT LEB %d free %d dirty %d tgc %d "
827 "cmt %d\n", i + c->lpt_first, c->ltab[i].free,
828 c->ltab[i].dirty, c->ltab[i].tgc, c->ltab[i].cmt);
829 spin_unlock(&dbg_lock);
830}
831
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300832void dbg_dump_leb(const struct ubifs_info *c, int lnum)
833{
834 struct ubifs_scan_leb *sleb;
835 struct ubifs_scan_node *snod;
Artem Bityutskiy73d9aec2011-03-11 15:39:09 +0200836 void *buf;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300837
838 if (dbg_failure_mode)
839 return;
840
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200841 printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n",
842 current->pid, lnum);
Artem Bityutskiy73d9aec2011-03-11 15:39:09 +0200843
Artem Bityutskiyfc5e58c2011-03-24 16:14:26 +0200844 buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
Artem Bityutskiy73d9aec2011-03-11 15:39:09 +0200845 if (!buf) {
846 ubifs_err("cannot allocate memory for dumping LEB %d", lnum);
847 return;
848 }
849
850 sleb = ubifs_scan(c, lnum, 0, buf, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300851 if (IS_ERR(sleb)) {
852 ubifs_err("scan error %d", (int)PTR_ERR(sleb));
Artem Bityutskiy73d9aec2011-03-11 15:39:09 +0200853 goto out;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300854 }
855
856 printk(KERN_DEBUG "LEB %d has %d nodes ending at %d\n", lnum,
857 sleb->nodes_cnt, sleb->endpt);
858
859 list_for_each_entry(snod, &sleb->nodes, list) {
860 cond_resched();
861 printk(KERN_DEBUG "Dumping node at LEB %d:%d len %d\n", lnum,
862 snod->offs, snod->len);
863 dbg_dump_node(c, snod->node);
864 }
865
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200866 printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n",
867 current->pid, lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300868 ubifs_scan_destroy(sleb);
Artem Bityutskiy73d9aec2011-03-11 15:39:09 +0200869
870out:
871 vfree(buf);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300872 return;
873}
874
875void dbg_dump_znode(const struct ubifs_info *c,
876 const struct ubifs_znode *znode)
877{
878 int n;
879 const struct ubifs_zbranch *zbr;
880
881 spin_lock(&dbg_lock);
882 if (znode->parent)
883 zbr = &znode->parent->zbranch[znode->iip];
884 else
885 zbr = &c->zroot;
886
887 printk(KERN_DEBUG "znode %p, LEB %d:%d len %d parent %p iip %d level %d"
888 " child_cnt %d flags %lx\n", znode, zbr->lnum, zbr->offs,
889 zbr->len, znode->parent, znode->iip, znode->level,
890 znode->child_cnt, znode->flags);
891
892 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
893 spin_unlock(&dbg_lock);
894 return;
895 }
896
897 printk(KERN_DEBUG "zbranches:\n");
898 for (n = 0; n < znode->child_cnt; n++) {
899 zbr = &znode->zbranch[n];
900 if (znode->level > 0)
901 printk(KERN_DEBUG "\t%d: znode %p LEB %d:%d len %d key "
902 "%s\n", n, zbr->znode, zbr->lnum,
903 zbr->offs, zbr->len,
904 DBGKEY(&zbr->key));
905 else
906 printk(KERN_DEBUG "\t%d: LNC %p LEB %d:%d len %d key "
907 "%s\n", n, zbr->znode, zbr->lnum,
908 zbr->offs, zbr->len,
909 DBGKEY(&zbr->key));
910 }
911 spin_unlock(&dbg_lock);
912}
913
914void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
915{
916 int i;
917
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200918 printk(KERN_DEBUG "(pid %d) start dumping heap cat %d (%d elements)\n",
Artem Bityutskiy1de94152008-07-25 12:58:38 +0300919 current->pid, cat, heap->cnt);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300920 for (i = 0; i < heap->cnt; i++) {
921 struct ubifs_lprops *lprops = heap->arr[i];
922
923 printk(KERN_DEBUG "\t%d. LEB %d hpos %d free %d dirty %d "
924 "flags %d\n", i, lprops->lnum, lprops->hpos,
925 lprops->free, lprops->dirty, lprops->flags);
926 }
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200927 printk(KERN_DEBUG "(pid %d) finish dumping heap\n", current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300928}
929
930void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
931 struct ubifs_nnode *parent, int iip)
932{
933 int i;
934
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200935 printk(KERN_DEBUG "(pid %d) dumping pnode:\n", current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300936 printk(KERN_DEBUG "\taddress %zx parent %zx cnext %zx\n",
937 (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
938 printk(KERN_DEBUG "\tflags %lu iip %d level %d num %d\n",
939 pnode->flags, iip, pnode->level, pnode->num);
940 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
941 struct ubifs_lprops *lp = &pnode->lprops[i];
942
943 printk(KERN_DEBUG "\t%d: free %d dirty %d flags %d lnum %d\n",
944 i, lp->free, lp->dirty, lp->flags, lp->lnum);
945 }
946}
947
948void dbg_dump_tnc(struct ubifs_info *c)
949{
950 struct ubifs_znode *znode;
951 int level;
952
953 printk(KERN_DEBUG "\n");
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200954 printk(KERN_DEBUG "(pid %d) start dumping TNC tree\n", current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300955 znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
956 level = znode->level;
957 printk(KERN_DEBUG "== Level %d ==\n", level);
958 while (znode) {
959 if (level != znode->level) {
960 level = znode->level;
961 printk(KERN_DEBUG "== Level %d ==\n", level);
962 }
963 dbg_dump_znode(c, znode);
964 znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
965 }
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200966 printk(KERN_DEBUG "(pid %d) finish dumping TNC tree\n", current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300967}
968
969static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
970 void *priv)
971{
972 dbg_dump_znode(c, znode);
973 return 0;
974}
975
976/**
977 * dbg_dump_index - dump the on-flash index.
978 * @c: UBIFS file-system description object
979 *
980 * This function dumps whole UBIFS indexing B-tree, unlike 'dbg_dump_tnc()'
981 * which dumps only in-memory znodes and does not read znodes which from flash.
982 */
983void dbg_dump_index(struct ubifs_info *c)
984{
985 dbg_walk_index(c, NULL, dump_znode, NULL);
986}
987
988/**
Artem Bityutskiy84abf972009-01-23 14:54:59 +0200989 * dbg_save_space_info - save information about flash space.
990 * @c: UBIFS file-system description object
991 *
992 * This function saves information about UBIFS free space, dirty space, etc, in
993 * order to check it later.
994 */
995void dbg_save_space_info(struct ubifs_info *c)
996{
997 struct ubifs_debug_info *d = c->dbg;
Artem Bityutskiy7da64432011-04-04 17:16:39 +0300998 int freeable_cnt;
Artem Bityutskiy84abf972009-01-23 14:54:59 +0200999
1000 spin_lock(&c->space_lock);
Artem Bityutskiy7da64432011-04-04 17:16:39 +03001001 memcpy(&d->saved_lst, &c->lst, sizeof(struct ubifs_lp_stats));
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +03001002 memcpy(&d->saved_bi, &c->bi, sizeof(struct ubifs_budg_info));
1003 d->saved_idx_gc_cnt = c->idx_gc_cnt;
Artem Bityutskiy7da64432011-04-04 17:16:39 +03001004
1005 /*
1006 * We use a dirty hack here and zero out @c->freeable_cnt, because it
1007 * affects the free space calculations, and UBIFS might not know about
1008 * all freeable eraseblocks. Indeed, we know about freeable eraseblocks
1009 * only when we read their lprops, and we do this only lazily, upon the
1010 * need. So at any given point of time @c->freeable_cnt might be not
1011 * exactly accurate.
1012 *
1013 * Just one example about the issue we hit when we did not zero
1014 * @c->freeable_cnt.
1015 * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the
1016 * amount of free space in @d->saved_free
1017 * 2. We re-mount R/W, which makes UBIFS to read the "lsave"
1018 * information from flash, where we cache LEBs from various
1019 * categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()'
1020 * -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()'
1021 * -> 'ubifs_get_pnode()' -> 'update_cats()'
1022 * -> 'ubifs_add_to_cat()').
1023 * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt
1024 * becomes %1.
1025 * 4. We calculate the amount of free space when the re-mount is
1026 * finished in 'dbg_check_space_info()' and it does not match
1027 * @d->saved_free.
1028 */
1029 freeable_cnt = c->freeable_cnt;
1030 c->freeable_cnt = 0;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001031 d->saved_free = ubifs_get_free_space_nolock(c);
Artem Bityutskiy7da64432011-04-04 17:16:39 +03001032 c->freeable_cnt = freeable_cnt;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001033 spin_unlock(&c->space_lock);
1034}
1035
1036/**
1037 * dbg_check_space_info - check flash space information.
1038 * @c: UBIFS file-system description object
1039 *
1040 * This function compares current flash space information with the information
1041 * which was saved when the 'dbg_save_space_info()' function was called.
1042 * Returns zero if the information has not changed, and %-EINVAL it it has
1043 * changed.
1044 */
1045int dbg_check_space_info(struct ubifs_info *c)
1046{
1047 struct ubifs_debug_info *d = c->dbg;
1048 struct ubifs_lp_stats lst;
Artem Bityutskiy7da64432011-04-04 17:16:39 +03001049 long long free;
1050 int freeable_cnt;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001051
1052 spin_lock(&c->space_lock);
Artem Bityutskiy7da64432011-04-04 17:16:39 +03001053 freeable_cnt = c->freeable_cnt;
1054 c->freeable_cnt = 0;
1055 free = ubifs_get_free_space_nolock(c);
1056 c->freeable_cnt = freeable_cnt;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001057 spin_unlock(&c->space_lock);
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001058
1059 if (free != d->saved_free) {
1060 ubifs_err("free space changed from %lld to %lld",
1061 d->saved_free, free);
1062 goto out;
1063 }
1064
1065 return 0;
1066
1067out:
1068 ubifs_msg("saved lprops statistics dump");
1069 dbg_dump_lstats(&d->saved_lst);
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +03001070 ubifs_msg("saved budgeting info dump");
1071 dbg_dump_budg(c, &d->saved_bi);
1072 ubifs_msg("saved idx_gc_cnt %d", d->saved_idx_gc_cnt);
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001073 ubifs_msg("current lprops statistics dump");
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +03001074 ubifs_get_lp_stats(c, &lst);
Artem Bityutskiye055f7e2009-09-17 15:08:31 +03001075 dbg_dump_lstats(&lst);
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +03001076 ubifs_msg("current budgeting info dump");
1077 dbg_dump_budg(c, &c->bi);
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001078 dump_stack();
1079 return -EINVAL;
1080}
1081
1082/**
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001083 * dbg_check_synced_i_size - check synchronized inode size.
1084 * @inode: inode to check
1085 *
1086 * If inode is clean, synchronized inode size has to be equivalent to current
1087 * inode size. This function has to be called only for locked inodes (@i_mutex
1088 * has to be locked). Returns %0 if synchronized inode size if correct, and
1089 * %-EINVAL if not.
1090 */
1091int dbg_check_synced_i_size(struct inode *inode)
1092{
1093 int err = 0;
1094 struct ubifs_inode *ui = ubifs_inode(inode);
1095
1096 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
1097 return 0;
1098 if (!S_ISREG(inode->i_mode))
1099 return 0;
1100
1101 mutex_lock(&ui->ui_mutex);
1102 spin_lock(&ui->ui_lock);
1103 if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
1104 ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode "
1105 "is clean", ui->ui_size, ui->synced_i_size);
1106 ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
1107 inode->i_mode, i_size_read(inode));
1108 dbg_dump_stack();
1109 err = -EINVAL;
1110 }
1111 spin_unlock(&ui->ui_lock);
1112 mutex_unlock(&ui->ui_mutex);
1113 return err;
1114}
1115
1116/*
1117 * dbg_check_dir - check directory inode size and link count.
1118 * @c: UBIFS file-system description object
1119 * @dir: the directory to calculate size for
1120 * @size: the result is returned here
1121 *
1122 * This function makes sure that directory size and link count are correct.
1123 * Returns zero in case of success and a negative error code in case of
1124 * failure.
1125 *
1126 * Note, it is good idea to make sure the @dir->i_mutex is locked before
1127 * calling this function.
1128 */
1129int dbg_check_dir_size(struct ubifs_info *c, const struct inode *dir)
1130{
1131 unsigned int nlink = 2;
1132 union ubifs_key key;
1133 struct ubifs_dent_node *dent, *pdent = NULL;
1134 struct qstr nm = { .name = NULL };
1135 loff_t size = UBIFS_INO_NODE_SZ;
1136
1137 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
1138 return 0;
1139
1140 if (!S_ISDIR(dir->i_mode))
1141 return 0;
1142
1143 lowest_dent_key(c, &key, dir->i_ino);
1144 while (1) {
1145 int err;
1146
1147 dent = ubifs_tnc_next_ent(c, &key, &nm);
1148 if (IS_ERR(dent)) {
1149 err = PTR_ERR(dent);
1150 if (err == -ENOENT)
1151 break;
1152 return err;
1153 }
1154
1155 nm.name = dent->name;
1156 nm.len = le16_to_cpu(dent->nlen);
1157 size += CALC_DENT_SIZE(nm.len);
1158 if (dent->type == UBIFS_ITYPE_DIR)
1159 nlink += 1;
1160 kfree(pdent);
1161 pdent = dent;
1162 key_read(c, &dent->key, &key);
1163 }
1164 kfree(pdent);
1165
1166 if (i_size_read(dir) != size) {
1167 ubifs_err("directory inode %lu has size %llu, "
1168 "but calculated size is %llu", dir->i_ino,
1169 (unsigned long long)i_size_read(dir),
1170 (unsigned long long)size);
1171 dump_stack();
1172 return -EINVAL;
1173 }
1174 if (dir->i_nlink != nlink) {
1175 ubifs_err("directory inode %lu has nlink %u, but calculated "
1176 "nlink is %u", dir->i_ino, dir->i_nlink, nlink);
1177 dump_stack();
1178 return -EINVAL;
1179 }
1180
1181 return 0;
1182}
1183
1184/**
1185 * dbg_check_key_order - make sure that colliding keys are properly ordered.
1186 * @c: UBIFS file-system description object
1187 * @zbr1: first zbranch
1188 * @zbr2: following zbranch
1189 *
1190 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
1191 * names of the direntries/xentries which are referred by the keys. This
1192 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
1193 * sure the name of direntry/xentry referred by @zbr1 is less than
1194 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
1195 * and a negative error code in case of failure.
1196 */
1197static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
1198 struct ubifs_zbranch *zbr2)
1199{
1200 int err, nlen1, nlen2, cmp;
1201 struct ubifs_dent_node *dent1, *dent2;
1202 union ubifs_key key;
1203
1204 ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
1205 dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1206 if (!dent1)
1207 return -ENOMEM;
1208 dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1209 if (!dent2) {
1210 err = -ENOMEM;
1211 goto out_free;
1212 }
1213
1214 err = ubifs_tnc_read_node(c, zbr1, dent1);
1215 if (err)
1216 goto out_free;
1217 err = ubifs_validate_entry(c, dent1);
1218 if (err)
1219 goto out_free;
1220
1221 err = ubifs_tnc_read_node(c, zbr2, dent2);
1222 if (err)
1223 goto out_free;
1224 err = ubifs_validate_entry(c, dent2);
1225 if (err)
1226 goto out_free;
1227
1228 /* Make sure node keys are the same as in zbranch */
1229 err = 1;
1230 key_read(c, &dent1->key, &key);
1231 if (keys_cmp(c, &zbr1->key, &key)) {
Artem Bityutskiy5d38b3a2008-12-30 17:58:42 +02001232 dbg_err("1st entry at %d:%d has key %s", zbr1->lnum,
1233 zbr1->offs, DBGKEY(&key));
1234 dbg_err("but it should have key %s according to tnc",
1235 DBGKEY(&zbr1->key));
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +02001236 dbg_dump_node(c, dent1);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03001237 goto out_free;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001238 }
1239
1240 key_read(c, &dent2->key, &key);
1241 if (keys_cmp(c, &zbr2->key, &key)) {
Artem Bityutskiy5d38b3a2008-12-30 17:58:42 +02001242 dbg_err("2nd entry at %d:%d has key %s", zbr1->lnum,
1243 zbr1->offs, DBGKEY(&key));
1244 dbg_err("but it should have key %s according to tnc",
1245 DBGKEY(&zbr2->key));
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +02001246 dbg_dump_node(c, dent2);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03001247 goto out_free;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001248 }
1249
1250 nlen1 = le16_to_cpu(dent1->nlen);
1251 nlen2 = le16_to_cpu(dent2->nlen);
1252
1253 cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
1254 if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
1255 err = 0;
1256 goto out_free;
1257 }
1258 if (cmp == 0 && nlen1 == nlen2)
Artem Bityutskiy5d38b3a2008-12-30 17:58:42 +02001259 dbg_err("2 xent/dent nodes with the same name");
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001260 else
Artem Bityutskiy5d38b3a2008-12-30 17:58:42 +02001261 dbg_err("bad order of colliding key %s",
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001262 DBGKEY(&key));
1263
Artem Bityutskiy552ff312008-10-23 11:49:28 +03001264 ubifs_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001265 dbg_dump_node(c, dent1);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03001266 ubifs_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001267 dbg_dump_node(c, dent2);
1268
1269out_free:
1270 kfree(dent2);
1271 kfree(dent1);
1272 return err;
1273}
1274
1275/**
1276 * dbg_check_znode - check if znode is all right.
1277 * @c: UBIFS file-system description object
1278 * @zbr: zbranch which points to this znode
1279 *
1280 * This function makes sure that znode referred to by @zbr is all right.
1281 * Returns zero if it is, and %-EINVAL if it is not.
1282 */
1283static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
1284{
1285 struct ubifs_znode *znode = zbr->znode;
1286 struct ubifs_znode *zp = znode->parent;
1287 int n, err, cmp;
1288
1289 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
1290 err = 1;
1291 goto out;
1292 }
1293 if (znode->level < 0) {
1294 err = 2;
1295 goto out;
1296 }
1297 if (znode->iip < 0 || znode->iip >= c->fanout) {
1298 err = 3;
1299 goto out;
1300 }
1301
1302 if (zbr->len == 0)
1303 /* Only dirty zbranch may have no on-flash nodes */
1304 if (!ubifs_zn_dirty(znode)) {
1305 err = 4;
1306 goto out;
1307 }
1308
1309 if (ubifs_zn_dirty(znode)) {
1310 /*
1311 * If znode is dirty, its parent has to be dirty as well. The
1312 * order of the operation is important, so we have to have
1313 * memory barriers.
1314 */
1315 smp_mb();
1316 if (zp && !ubifs_zn_dirty(zp)) {
1317 /*
1318 * The dirty flag is atomic and is cleared outside the
1319 * TNC mutex, so znode's dirty flag may now have
1320 * been cleared. The child is always cleared before the
1321 * parent, so we just need to check again.
1322 */
1323 smp_mb();
1324 if (ubifs_zn_dirty(znode)) {
1325 err = 5;
1326 goto out;
1327 }
1328 }
1329 }
1330
1331 if (zp) {
1332 const union ubifs_key *min, *max;
1333
1334 if (znode->level != zp->level - 1) {
1335 err = 6;
1336 goto out;
1337 }
1338
1339 /* Make sure the 'parent' pointer in our znode is correct */
1340 err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
1341 if (!err) {
1342 /* This zbranch does not exist in the parent */
1343 err = 7;
1344 goto out;
1345 }
1346
1347 if (znode->iip >= zp->child_cnt) {
1348 err = 8;
1349 goto out;
1350 }
1351
1352 if (znode->iip != n) {
1353 /* This may happen only in case of collisions */
1354 if (keys_cmp(c, &zp->zbranch[n].key,
1355 &zp->zbranch[znode->iip].key)) {
1356 err = 9;
1357 goto out;
1358 }
1359 n = znode->iip;
1360 }
1361
1362 /*
1363 * Make sure that the first key in our znode is greater than or
1364 * equal to the key in the pointing zbranch.
1365 */
1366 min = &zbr->key;
1367 cmp = keys_cmp(c, min, &znode->zbranch[0].key);
1368 if (cmp == 1) {
1369 err = 10;
1370 goto out;
1371 }
1372
1373 if (n + 1 < zp->child_cnt) {
1374 max = &zp->zbranch[n + 1].key;
1375
1376 /*
1377 * Make sure the last key in our znode is less or
Artem Bityutskiy7d4e9cc2009-03-20 19:11:12 +02001378 * equivalent than the key in the zbranch which goes
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001379 * after our pointing zbranch.
1380 */
1381 cmp = keys_cmp(c, max,
1382 &znode->zbranch[znode->child_cnt - 1].key);
1383 if (cmp == -1) {
1384 err = 11;
1385 goto out;
1386 }
1387 }
1388 } else {
1389 /* This may only be root znode */
1390 if (zbr != &c->zroot) {
1391 err = 12;
1392 goto out;
1393 }
1394 }
1395
1396 /*
1397 * Make sure that next key is greater or equivalent then the previous
1398 * one.
1399 */
1400 for (n = 1; n < znode->child_cnt; n++) {
1401 cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
1402 &znode->zbranch[n].key);
1403 if (cmp > 0) {
1404 err = 13;
1405 goto out;
1406 }
1407 if (cmp == 0) {
1408 /* This can only be keys with colliding hash */
1409 if (!is_hash_key(c, &znode->zbranch[n].key)) {
1410 err = 14;
1411 goto out;
1412 }
1413
1414 if (znode->level != 0 || c->replaying)
1415 continue;
1416
1417 /*
1418 * Colliding keys should follow binary order of
1419 * corresponding xentry/dentry names.
1420 */
1421 err = dbg_check_key_order(c, &znode->zbranch[n - 1],
1422 &znode->zbranch[n]);
1423 if (err < 0)
1424 return err;
1425 if (err) {
1426 err = 15;
1427 goto out;
1428 }
1429 }
1430 }
1431
1432 for (n = 0; n < znode->child_cnt; n++) {
1433 if (!znode->zbranch[n].znode &&
1434 (znode->zbranch[n].lnum == 0 ||
1435 znode->zbranch[n].len == 0)) {
1436 err = 16;
1437 goto out;
1438 }
1439
1440 if (znode->zbranch[n].lnum != 0 &&
1441 znode->zbranch[n].len == 0) {
1442 err = 17;
1443 goto out;
1444 }
1445
1446 if (znode->zbranch[n].lnum == 0 &&
1447 znode->zbranch[n].len != 0) {
1448 err = 18;
1449 goto out;
1450 }
1451
1452 if (znode->zbranch[n].lnum == 0 &&
1453 znode->zbranch[n].offs != 0) {
1454 err = 19;
1455 goto out;
1456 }
1457
1458 if (znode->level != 0 && znode->zbranch[n].znode)
1459 if (znode->zbranch[n].znode->parent != znode) {
1460 err = 20;
1461 goto out;
1462 }
1463 }
1464
1465 return 0;
1466
1467out:
1468 ubifs_err("failed, error %d", err);
1469 ubifs_msg("dump of the znode");
1470 dbg_dump_znode(c, znode);
1471 if (zp) {
1472 ubifs_msg("dump of the parent znode");
1473 dbg_dump_znode(c, zp);
1474 }
1475 dump_stack();
1476 return -EINVAL;
1477}
1478
1479/**
1480 * dbg_check_tnc - check TNC tree.
1481 * @c: UBIFS file-system description object
1482 * @extra: do extra checks that are possible at start commit
1483 *
1484 * This function traverses whole TNC tree and checks every znode. Returns zero
1485 * if everything is all right and %-EINVAL if something is wrong with TNC.
1486 */
1487int dbg_check_tnc(struct ubifs_info *c, int extra)
1488{
1489 struct ubifs_znode *znode;
1490 long clean_cnt = 0, dirty_cnt = 0;
1491 int err, last;
1492
1493 if (!(ubifs_chk_flags & UBIFS_CHK_TNC))
1494 return 0;
1495
1496 ubifs_assert(mutex_is_locked(&c->tnc_mutex));
1497 if (!c->zroot.znode)
1498 return 0;
1499
1500 znode = ubifs_tnc_postorder_first(c->zroot.znode);
1501 while (1) {
1502 struct ubifs_znode *prev;
1503 struct ubifs_zbranch *zbr;
1504
1505 if (!znode->parent)
1506 zbr = &c->zroot;
1507 else
1508 zbr = &znode->parent->zbranch[znode->iip];
1509
1510 err = dbg_check_znode(c, zbr);
1511 if (err)
1512 return err;
1513
1514 if (extra) {
1515 if (ubifs_zn_dirty(znode))
1516 dirty_cnt += 1;
1517 else
1518 clean_cnt += 1;
1519 }
1520
1521 prev = znode;
1522 znode = ubifs_tnc_postorder_next(znode);
1523 if (!znode)
1524 break;
1525
1526 /*
1527 * If the last key of this znode is equivalent to the first key
1528 * of the next znode (collision), then check order of the keys.
1529 */
1530 last = prev->child_cnt - 1;
1531 if (prev->level == 0 && znode->level == 0 && !c->replaying &&
1532 !keys_cmp(c, &prev->zbranch[last].key,
1533 &znode->zbranch[0].key)) {
1534 err = dbg_check_key_order(c, &prev->zbranch[last],
1535 &znode->zbranch[0]);
1536 if (err < 0)
1537 return err;
1538 if (err) {
1539 ubifs_msg("first znode");
1540 dbg_dump_znode(c, prev);
1541 ubifs_msg("second znode");
1542 dbg_dump_znode(c, znode);
1543 return -EINVAL;
1544 }
1545 }
1546 }
1547
1548 if (extra) {
1549 if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
1550 ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld",
1551 atomic_long_read(&c->clean_zn_cnt),
1552 clean_cnt);
1553 return -EINVAL;
1554 }
1555 if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
1556 ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld",
1557 atomic_long_read(&c->dirty_zn_cnt),
1558 dirty_cnt);
1559 return -EINVAL;
1560 }
1561 }
1562
1563 return 0;
1564}
1565
1566/**
1567 * dbg_walk_index - walk the on-flash index.
1568 * @c: UBIFS file-system description object
1569 * @leaf_cb: called for each leaf node
1570 * @znode_cb: called for each indexing node
Adrian Hunter227c75c2009-01-29 11:53:51 +02001571 * @priv: private data which is passed to callbacks
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001572 *
1573 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1574 * node and @znode_cb for each indexing node. Returns zero in case of success
1575 * and a negative error code in case of failure.
1576 *
1577 * It would be better if this function removed every znode it pulled to into
1578 * the TNC, so that the behavior more closely matched the non-debugging
1579 * behavior.
1580 */
1581int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1582 dbg_znode_callback znode_cb, void *priv)
1583{
1584 int err;
1585 struct ubifs_zbranch *zbr;
1586 struct ubifs_znode *znode, *child;
1587
1588 mutex_lock(&c->tnc_mutex);
1589 /* If the root indexing node is not in TNC - pull it */
1590 if (!c->zroot.znode) {
1591 c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1592 if (IS_ERR(c->zroot.znode)) {
1593 err = PTR_ERR(c->zroot.znode);
1594 c->zroot.znode = NULL;
1595 goto out_unlock;
1596 }
1597 }
1598
1599 /*
1600 * We are going to traverse the indexing tree in the postorder manner.
1601 * Go down and find the leftmost indexing node where we are going to
1602 * start from.
1603 */
1604 znode = c->zroot.znode;
1605 while (znode->level > 0) {
1606 zbr = &znode->zbranch[0];
1607 child = zbr->znode;
1608 if (!child) {
1609 child = ubifs_load_znode(c, zbr, znode, 0);
1610 if (IS_ERR(child)) {
1611 err = PTR_ERR(child);
1612 goto out_unlock;
1613 }
1614 zbr->znode = child;
1615 }
1616
1617 znode = child;
1618 }
1619
1620 /* Iterate over all indexing nodes */
1621 while (1) {
1622 int idx;
1623
1624 cond_resched();
1625
1626 if (znode_cb) {
1627 err = znode_cb(c, znode, priv);
1628 if (err) {
1629 ubifs_err("znode checking function returned "
1630 "error %d", err);
1631 dbg_dump_znode(c, znode);
1632 goto out_dump;
1633 }
1634 }
1635 if (leaf_cb && znode->level == 0) {
1636 for (idx = 0; idx < znode->child_cnt; idx++) {
1637 zbr = &znode->zbranch[idx];
1638 err = leaf_cb(c, zbr, priv);
1639 if (err) {
1640 ubifs_err("leaf checking function "
1641 "returned error %d, for leaf "
1642 "at LEB %d:%d",
1643 err, zbr->lnum, zbr->offs);
1644 goto out_dump;
1645 }
1646 }
1647 }
1648
1649 if (!znode->parent)
1650 break;
1651
1652 idx = znode->iip + 1;
1653 znode = znode->parent;
1654 if (idx < znode->child_cnt) {
1655 /* Switch to the next index in the parent */
1656 zbr = &znode->zbranch[idx];
1657 child = zbr->znode;
1658 if (!child) {
1659 child = ubifs_load_znode(c, zbr, znode, idx);
1660 if (IS_ERR(child)) {
1661 err = PTR_ERR(child);
1662 goto out_unlock;
1663 }
1664 zbr->znode = child;
1665 }
1666 znode = child;
1667 } else
1668 /*
1669 * This is the last child, switch to the parent and
1670 * continue.
1671 */
1672 continue;
1673
1674 /* Go to the lowest leftmost znode in the new sub-tree */
1675 while (znode->level > 0) {
1676 zbr = &znode->zbranch[0];
1677 child = zbr->znode;
1678 if (!child) {
1679 child = ubifs_load_znode(c, zbr, znode, 0);
1680 if (IS_ERR(child)) {
1681 err = PTR_ERR(child);
1682 goto out_unlock;
1683 }
1684 zbr->znode = child;
1685 }
1686 znode = child;
1687 }
1688 }
1689
1690 mutex_unlock(&c->tnc_mutex);
1691 return 0;
1692
1693out_dump:
1694 if (znode->parent)
1695 zbr = &znode->parent->zbranch[znode->iip];
1696 else
1697 zbr = &c->zroot;
1698 ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1699 dbg_dump_znode(c, znode);
1700out_unlock:
1701 mutex_unlock(&c->tnc_mutex);
1702 return err;
1703}
1704
1705/**
1706 * add_size - add znode size to partially calculated index size.
1707 * @c: UBIFS file-system description object
1708 * @znode: znode to add size for
1709 * @priv: partially calculated index size
1710 *
1711 * This is a helper function for 'dbg_check_idx_size()' which is called for
1712 * every indexing node and adds its size to the 'long long' variable pointed to
1713 * by @priv.
1714 */
1715static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1716{
1717 long long *idx_size = priv;
1718 int add;
1719
1720 add = ubifs_idx_node_sz(c, znode->child_cnt);
1721 add = ALIGN(add, 8);
1722 *idx_size += add;
1723 return 0;
1724}
1725
1726/**
1727 * dbg_check_idx_size - check index size.
1728 * @c: UBIFS file-system description object
1729 * @idx_size: size to check
1730 *
1731 * This function walks the UBIFS index, calculates its size and checks that the
1732 * size is equivalent to @idx_size. Returns zero in case of success and a
1733 * negative error code in case of failure.
1734 */
1735int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
1736{
1737 int err;
1738 long long calc = 0;
1739
1740 if (!(ubifs_chk_flags & UBIFS_CHK_IDX_SZ))
1741 return 0;
1742
1743 err = dbg_walk_index(c, NULL, add_size, &calc);
1744 if (err) {
1745 ubifs_err("error %d while walking the index", err);
1746 return err;
1747 }
1748
1749 if (calc != idx_size) {
1750 ubifs_err("index size check failed: calculated size is %lld, "
1751 "should be %lld", calc, idx_size);
1752 dump_stack();
1753 return -EINVAL;
1754 }
1755
1756 return 0;
1757}
1758
1759/**
1760 * struct fsck_inode - information about an inode used when checking the file-system.
1761 * @rb: link in the RB-tree of inodes
1762 * @inum: inode number
1763 * @mode: inode type, permissions, etc
1764 * @nlink: inode link count
1765 * @xattr_cnt: count of extended attributes
1766 * @references: how many directory/xattr entries refer this inode (calculated
1767 * while walking the index)
1768 * @calc_cnt: for directory inode count of child directories
1769 * @size: inode size (read from on-flash inode)
1770 * @xattr_sz: summary size of all extended attributes (read from on-flash
1771 * inode)
1772 * @calc_sz: for directories calculated directory size
1773 * @calc_xcnt: count of extended attributes
1774 * @calc_xsz: calculated summary size of all extended attributes
1775 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1776 * inode (read from on-flash inode)
1777 * @calc_xnms: calculated sum of lengths of all extended attribute names
1778 */
1779struct fsck_inode {
1780 struct rb_node rb;
1781 ino_t inum;
1782 umode_t mode;
1783 unsigned int nlink;
1784 unsigned int xattr_cnt;
1785 int references;
1786 int calc_cnt;
1787 long long size;
1788 unsigned int xattr_sz;
1789 long long calc_sz;
1790 long long calc_xcnt;
1791 long long calc_xsz;
1792 unsigned int xattr_nms;
1793 long long calc_xnms;
1794};
1795
1796/**
1797 * struct fsck_data - private FS checking information.
1798 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1799 */
1800struct fsck_data {
1801 struct rb_root inodes;
1802};
1803
1804/**
1805 * add_inode - add inode information to RB-tree of inodes.
1806 * @c: UBIFS file-system description object
1807 * @fsckd: FS checking information
1808 * @ino: raw UBIFS inode to add
1809 *
1810 * This is a helper function for 'check_leaf()' which adds information about
1811 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1812 * case of success and a negative error code in case of failure.
1813 */
1814static struct fsck_inode *add_inode(struct ubifs_info *c,
1815 struct fsck_data *fsckd,
1816 struct ubifs_ino_node *ino)
1817{
1818 struct rb_node **p, *parent = NULL;
1819 struct fsck_inode *fscki;
1820 ino_t inum = key_inum_flash(c, &ino->key);
Artem Bityutskiy45cd5cd2011-05-02 22:34:39 +03001821 struct inode *inode;
1822 struct ubifs_inode *ui;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001823
1824 p = &fsckd->inodes.rb_node;
1825 while (*p) {
1826 parent = *p;
1827 fscki = rb_entry(parent, struct fsck_inode, rb);
1828 if (inum < fscki->inum)
1829 p = &(*p)->rb_left;
1830 else if (inum > fscki->inum)
1831 p = &(*p)->rb_right;
1832 else
1833 return fscki;
1834 }
1835
1836 if (inum > c->highest_inum) {
1837 ubifs_err("too high inode number, max. is %lu",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001838 (unsigned long)c->highest_inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001839 return ERR_PTR(-EINVAL);
1840 }
1841
1842 fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
1843 if (!fscki)
1844 return ERR_PTR(-ENOMEM);
1845
Artem Bityutskiy45cd5cd2011-05-02 22:34:39 +03001846 inode = ilookup(c->vfs_sb, inum);
1847
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001848 fscki->inum = inum;
Artem Bityutskiy45cd5cd2011-05-02 22:34:39 +03001849 /*
1850 * If the inode is present in the VFS inode cache, use it instead of
1851 * the on-flash inode which might be out-of-date. E.g., the size might
1852 * be out-of-date. If we do not do this, the following may happen, for
1853 * example:
1854 * 1. A power cut happens
1855 * 2. We mount the file-system R/O, the replay process fixes up the
1856 * inode size in the VFS cache, but on on-flash.
1857 * 3. 'check_leaf()' fails because it hits a data node beyond inode
1858 * size.
1859 */
1860 if (!inode) {
1861 fscki->nlink = le32_to_cpu(ino->nlink);
1862 fscki->size = le64_to_cpu(ino->size);
1863 fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
1864 fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
1865 fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
1866 fscki->mode = le32_to_cpu(ino->mode);
1867 } else {
1868 ui = ubifs_inode(inode);
1869 fscki->nlink = inode->i_nlink;
1870 fscki->size = inode->i_size;
1871 fscki->xattr_cnt = ui->xattr_cnt;
1872 fscki->xattr_sz = ui->xattr_size;
1873 fscki->xattr_nms = ui->xattr_names;
1874 fscki->mode = inode->i_mode;
1875 iput(inode);
1876 }
1877
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001878 if (S_ISDIR(fscki->mode)) {
1879 fscki->calc_sz = UBIFS_INO_NODE_SZ;
1880 fscki->calc_cnt = 2;
1881 }
Artem Bityutskiy45cd5cd2011-05-02 22:34:39 +03001882
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001883 rb_link_node(&fscki->rb, parent, p);
1884 rb_insert_color(&fscki->rb, &fsckd->inodes);
Artem Bityutskiy45cd5cd2011-05-02 22:34:39 +03001885
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001886 return fscki;
1887}
1888
1889/**
1890 * search_inode - search inode in the RB-tree of inodes.
1891 * @fsckd: FS checking information
1892 * @inum: inode number to search
1893 *
1894 * This is a helper function for 'check_leaf()' which searches inode @inum in
1895 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1896 * the inode was not found.
1897 */
1898static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
1899{
1900 struct rb_node *p;
1901 struct fsck_inode *fscki;
1902
1903 p = fsckd->inodes.rb_node;
1904 while (p) {
1905 fscki = rb_entry(p, struct fsck_inode, rb);
1906 if (inum < fscki->inum)
1907 p = p->rb_left;
1908 else if (inum > fscki->inum)
1909 p = p->rb_right;
1910 else
1911 return fscki;
1912 }
1913 return NULL;
1914}
1915
1916/**
1917 * read_add_inode - read inode node and add it to RB-tree of inodes.
1918 * @c: UBIFS file-system description object
1919 * @fsckd: FS checking information
1920 * @inum: inode number to read
1921 *
1922 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1923 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1924 * information pointer in case of success and a negative error code in case of
1925 * failure.
1926 */
1927static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1928 struct fsck_data *fsckd, ino_t inum)
1929{
1930 int n, err;
1931 union ubifs_key key;
1932 struct ubifs_znode *znode;
1933 struct ubifs_zbranch *zbr;
1934 struct ubifs_ino_node *ino;
1935 struct fsck_inode *fscki;
1936
1937 fscki = search_inode(fsckd, inum);
1938 if (fscki)
1939 return fscki;
1940
1941 ino_key_init(c, &key, inum);
1942 err = ubifs_lookup_level0(c, &key, &znode, &n);
1943 if (!err) {
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001944 ubifs_err("inode %lu not found in index", (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001945 return ERR_PTR(-ENOENT);
1946 } else if (err < 0) {
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001947 ubifs_err("error %d while looking up inode %lu",
1948 err, (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001949 return ERR_PTR(err);
1950 }
1951
1952 zbr = &znode->zbranch[n];
1953 if (zbr->len < UBIFS_INO_NODE_SZ) {
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001954 ubifs_err("bad node %lu node length %d",
1955 (unsigned long)inum, zbr->len);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001956 return ERR_PTR(-EINVAL);
1957 }
1958
1959 ino = kmalloc(zbr->len, GFP_NOFS);
1960 if (!ino)
1961 return ERR_PTR(-ENOMEM);
1962
1963 err = ubifs_tnc_read_node(c, zbr, ino);
1964 if (err) {
1965 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
1966 zbr->lnum, zbr->offs, err);
1967 kfree(ino);
1968 return ERR_PTR(err);
1969 }
1970
1971 fscki = add_inode(c, fsckd, ino);
1972 kfree(ino);
1973 if (IS_ERR(fscki)) {
1974 ubifs_err("error %ld while adding inode %lu node",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001975 PTR_ERR(fscki), (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001976 return fscki;
1977 }
1978
1979 return fscki;
1980}
1981
1982/**
1983 * check_leaf - check leaf node.
1984 * @c: UBIFS file-system description object
1985 * @zbr: zbranch of the leaf node to check
1986 * @priv: FS checking information
1987 *
1988 * This is a helper function for 'dbg_check_filesystem()' which is called for
1989 * every single leaf node while walking the indexing tree. It checks that the
1990 * leaf node referred from the indexing tree exists, has correct CRC, and does
1991 * some other basic validation. This function is also responsible for building
1992 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
1993 * calculates reference count, size, etc for each inode in order to later
1994 * compare them to the information stored inside the inodes and detect possible
1995 * inconsistencies. Returns zero in case of success and a negative error code
1996 * in case of failure.
1997 */
1998static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
1999 void *priv)
2000{
2001 ino_t inum;
2002 void *node;
2003 struct ubifs_ch *ch;
2004 int err, type = key_type(c, &zbr->key);
2005 struct fsck_inode *fscki;
2006
2007 if (zbr->len < UBIFS_CH_SZ) {
2008 ubifs_err("bad leaf length %d (LEB %d:%d)",
2009 zbr->len, zbr->lnum, zbr->offs);
2010 return -EINVAL;
2011 }
2012
2013 node = kmalloc(zbr->len, GFP_NOFS);
2014 if (!node)
2015 return -ENOMEM;
2016
2017 err = ubifs_tnc_read_node(c, zbr, node);
2018 if (err) {
2019 ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
2020 zbr->lnum, zbr->offs, err);
2021 goto out_free;
2022 }
2023
2024 /* If this is an inode node, add it to RB-tree of inodes */
2025 if (type == UBIFS_INO_KEY) {
2026 fscki = add_inode(c, priv, node);
2027 if (IS_ERR(fscki)) {
2028 err = PTR_ERR(fscki);
2029 ubifs_err("error %d while adding inode node", err);
2030 goto out_dump;
2031 }
2032 goto out;
2033 }
2034
2035 if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
2036 type != UBIFS_DATA_KEY) {
2037 ubifs_err("unexpected node type %d at LEB %d:%d",
2038 type, zbr->lnum, zbr->offs);
2039 err = -EINVAL;
2040 goto out_free;
2041 }
2042
2043 ch = node;
2044 if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
2045 ubifs_err("too high sequence number, max. is %llu",
2046 c->max_sqnum);
2047 err = -EINVAL;
2048 goto out_dump;
2049 }
2050
2051 if (type == UBIFS_DATA_KEY) {
2052 long long blk_offs;
2053 struct ubifs_data_node *dn = node;
2054
2055 /*
2056 * Search the inode node this data node belongs to and insert
2057 * it to the RB-tree of inodes.
2058 */
2059 inum = key_inum_flash(c, &dn->key);
2060 fscki = read_add_inode(c, priv, inum);
2061 if (IS_ERR(fscki)) {
2062 err = PTR_ERR(fscki);
2063 ubifs_err("error %d while processing data node and "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002064 "trying to find inode node %lu",
2065 err, (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002066 goto out_dump;
2067 }
2068
2069 /* Make sure the data node is within inode size */
2070 blk_offs = key_block_flash(c, &dn->key);
2071 blk_offs <<= UBIFS_BLOCK_SHIFT;
2072 blk_offs += le32_to_cpu(dn->size);
2073 if (blk_offs > fscki->size) {
2074 ubifs_err("data node at LEB %d:%d is not within inode "
2075 "size %lld", zbr->lnum, zbr->offs,
2076 fscki->size);
2077 err = -EINVAL;
2078 goto out_dump;
2079 }
2080 } else {
2081 int nlen;
2082 struct ubifs_dent_node *dent = node;
2083 struct fsck_inode *fscki1;
2084
2085 err = ubifs_validate_entry(c, dent);
2086 if (err)
2087 goto out_dump;
2088
2089 /*
2090 * Search the inode node this entry refers to and the parent
2091 * inode node and insert them to the RB-tree of inodes.
2092 */
2093 inum = le64_to_cpu(dent->inum);
2094 fscki = read_add_inode(c, priv, inum);
2095 if (IS_ERR(fscki)) {
2096 err = PTR_ERR(fscki);
2097 ubifs_err("error %d while processing entry node and "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002098 "trying to find inode node %lu",
2099 err, (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002100 goto out_dump;
2101 }
2102
2103 /* Count how many direntries or xentries refers this inode */
2104 fscki->references += 1;
2105
2106 inum = key_inum_flash(c, &dent->key);
2107 fscki1 = read_add_inode(c, priv, inum);
2108 if (IS_ERR(fscki1)) {
Roel Kluinb38882f2009-12-07 14:21:45 +01002109 err = PTR_ERR(fscki1);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002110 ubifs_err("error %d while processing entry node and "
2111 "trying to find parent inode node %lu",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002112 err, (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002113 goto out_dump;
2114 }
2115
2116 nlen = le16_to_cpu(dent->nlen);
2117 if (type == UBIFS_XENT_KEY) {
2118 fscki1->calc_xcnt += 1;
2119 fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
2120 fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
2121 fscki1->calc_xnms += nlen;
2122 } else {
2123 fscki1->calc_sz += CALC_DENT_SIZE(nlen);
2124 if (dent->type == UBIFS_ITYPE_DIR)
2125 fscki1->calc_cnt += 1;
2126 }
2127 }
2128
2129out:
2130 kfree(node);
2131 return 0;
2132
2133out_dump:
2134 ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
2135 dbg_dump_node(c, node);
2136out_free:
2137 kfree(node);
2138 return err;
2139}
2140
2141/**
2142 * free_inodes - free RB-tree of inodes.
2143 * @fsckd: FS checking information
2144 */
2145static void free_inodes(struct fsck_data *fsckd)
2146{
2147 struct rb_node *this = fsckd->inodes.rb_node;
2148 struct fsck_inode *fscki;
2149
2150 while (this) {
2151 if (this->rb_left)
2152 this = this->rb_left;
2153 else if (this->rb_right)
2154 this = this->rb_right;
2155 else {
2156 fscki = rb_entry(this, struct fsck_inode, rb);
2157 this = rb_parent(this);
2158 if (this) {
2159 if (this->rb_left == &fscki->rb)
2160 this->rb_left = NULL;
2161 else
2162 this->rb_right = NULL;
2163 }
2164 kfree(fscki);
2165 }
2166 }
2167}
2168
2169/**
2170 * check_inodes - checks all inodes.
2171 * @c: UBIFS file-system description object
2172 * @fsckd: FS checking information
2173 *
2174 * This is a helper function for 'dbg_check_filesystem()' which walks the
2175 * RB-tree of inodes after the index scan has been finished, and checks that
2176 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
2177 * %-EINVAL if not, and a negative error code in case of failure.
2178 */
2179static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
2180{
2181 int n, err;
2182 union ubifs_key key;
2183 struct ubifs_znode *znode;
2184 struct ubifs_zbranch *zbr;
2185 struct ubifs_ino_node *ino;
2186 struct fsck_inode *fscki;
2187 struct rb_node *this = rb_first(&fsckd->inodes);
2188
2189 while (this) {
2190 fscki = rb_entry(this, struct fsck_inode, rb);
2191 this = rb_next(this);
2192
2193 if (S_ISDIR(fscki->mode)) {
2194 /*
2195 * Directories have to have exactly one reference (they
2196 * cannot have hardlinks), although root inode is an
2197 * exception.
2198 */
2199 if (fscki->inum != UBIFS_ROOT_INO &&
2200 fscki->references != 1) {
2201 ubifs_err("directory inode %lu has %d "
2202 "direntries which refer it, but "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002203 "should be 1",
2204 (unsigned long)fscki->inum,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002205 fscki->references);
2206 goto out_dump;
2207 }
2208 if (fscki->inum == UBIFS_ROOT_INO &&
2209 fscki->references != 0) {
2210 ubifs_err("root inode %lu has non-zero (%d) "
2211 "direntries which refer it",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002212 (unsigned long)fscki->inum,
2213 fscki->references);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002214 goto out_dump;
2215 }
2216 if (fscki->calc_sz != fscki->size) {
2217 ubifs_err("directory inode %lu size is %lld, "
2218 "but calculated size is %lld",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002219 (unsigned long)fscki->inum,
2220 fscki->size, fscki->calc_sz);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002221 goto out_dump;
2222 }
2223 if (fscki->calc_cnt != fscki->nlink) {
2224 ubifs_err("directory inode %lu nlink is %d, "
2225 "but calculated nlink is %d",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002226 (unsigned long)fscki->inum,
2227 fscki->nlink, fscki->calc_cnt);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002228 goto out_dump;
2229 }
2230 } else {
2231 if (fscki->references != fscki->nlink) {
2232 ubifs_err("inode %lu nlink is %d, but "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002233 "calculated nlink is %d",
2234 (unsigned long)fscki->inum,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002235 fscki->nlink, fscki->references);
2236 goto out_dump;
2237 }
2238 }
2239 if (fscki->xattr_sz != fscki->calc_xsz) {
2240 ubifs_err("inode %lu has xattr size %u, but "
2241 "calculated size is %lld",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002242 (unsigned long)fscki->inum, fscki->xattr_sz,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002243 fscki->calc_xsz);
2244 goto out_dump;
2245 }
2246 if (fscki->xattr_cnt != fscki->calc_xcnt) {
2247 ubifs_err("inode %lu has %u xattrs, but "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002248 "calculated count is %lld",
2249 (unsigned long)fscki->inum,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002250 fscki->xattr_cnt, fscki->calc_xcnt);
2251 goto out_dump;
2252 }
2253 if (fscki->xattr_nms != fscki->calc_xnms) {
2254 ubifs_err("inode %lu has xattr names' size %u, but "
2255 "calculated names' size is %lld",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002256 (unsigned long)fscki->inum, fscki->xattr_nms,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002257 fscki->calc_xnms);
2258 goto out_dump;
2259 }
2260 }
2261
2262 return 0;
2263
2264out_dump:
2265 /* Read the bad inode and dump it */
2266 ino_key_init(c, &key, fscki->inum);
2267 err = ubifs_lookup_level0(c, &key, &znode, &n);
2268 if (!err) {
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002269 ubifs_err("inode %lu not found in index",
2270 (unsigned long)fscki->inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002271 return -ENOENT;
2272 } else if (err < 0) {
2273 ubifs_err("error %d while looking up inode %lu",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002274 err, (unsigned long)fscki->inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002275 return err;
2276 }
2277
2278 zbr = &znode->zbranch[n];
2279 ino = kmalloc(zbr->len, GFP_NOFS);
2280 if (!ino)
2281 return -ENOMEM;
2282
2283 err = ubifs_tnc_read_node(c, zbr, ino);
2284 if (err) {
2285 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2286 zbr->lnum, zbr->offs, err);
2287 kfree(ino);
2288 return err;
2289 }
2290
2291 ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002292 (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002293 dbg_dump_node(c, ino);
2294 kfree(ino);
2295 return -EINVAL;
2296}
2297
2298/**
2299 * dbg_check_filesystem - check the file-system.
2300 * @c: UBIFS file-system description object
2301 *
2302 * This function checks the file system, namely:
2303 * o makes sure that all leaf nodes exist and their CRCs are correct;
2304 * o makes sure inode nlink, size, xattr size/count are correct (for all
2305 * inodes).
2306 *
2307 * The function reads whole indexing tree and all nodes, so it is pretty
2308 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2309 * not, and a negative error code in case of failure.
2310 */
2311int dbg_check_filesystem(struct ubifs_info *c)
2312{
2313 int err;
2314 struct fsck_data fsckd;
2315
2316 if (!(ubifs_chk_flags & UBIFS_CHK_FS))
2317 return 0;
2318
2319 fsckd.inodes = RB_ROOT;
2320 err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
2321 if (err)
2322 goto out_free;
2323
2324 err = check_inodes(c, &fsckd);
2325 if (err)
2326 goto out_free;
2327
2328 free_inodes(&fsckd);
2329 return 0;
2330
2331out_free:
2332 ubifs_err("file-system check failed with error %d", err);
2333 dump_stack();
2334 free_inodes(&fsckd);
2335 return err;
2336}
2337
Artem Bityutskiy3bb66b42010-08-07 10:06:11 +03002338/**
2339 * dbg_check_data_nodes_order - check that list of data nodes is sorted.
2340 * @c: UBIFS file-system description object
2341 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2342 *
2343 * This function returns zero if the list of data nodes is sorted correctly,
2344 * and %-EINVAL if not.
2345 */
2346int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head)
2347{
2348 struct list_head *cur;
2349 struct ubifs_scan_node *sa, *sb;
2350
2351 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
2352 return 0;
2353
2354 for (cur = head->next; cur->next != head; cur = cur->next) {
2355 ino_t inuma, inumb;
2356 uint32_t blka, blkb;
2357
2358 cond_resched();
2359 sa = container_of(cur, struct ubifs_scan_node, list);
2360 sb = container_of(cur->next, struct ubifs_scan_node, list);
2361
2362 if (sa->type != UBIFS_DATA_NODE) {
2363 ubifs_err("bad node type %d", sa->type);
2364 dbg_dump_node(c, sa->node);
2365 return -EINVAL;
2366 }
2367 if (sb->type != UBIFS_DATA_NODE) {
2368 ubifs_err("bad node type %d", sb->type);
2369 dbg_dump_node(c, sb->node);
2370 return -EINVAL;
2371 }
2372
2373 inuma = key_inum(c, &sa->key);
2374 inumb = key_inum(c, &sb->key);
2375
2376 if (inuma < inumb)
2377 continue;
2378 if (inuma > inumb) {
2379 ubifs_err("larger inum %lu goes before inum %lu",
2380 (unsigned long)inuma, (unsigned long)inumb);
2381 goto error_dump;
2382 }
2383
2384 blka = key_block(c, &sa->key);
2385 blkb = key_block(c, &sb->key);
2386
2387 if (blka > blkb) {
2388 ubifs_err("larger block %u goes before %u", blka, blkb);
2389 goto error_dump;
2390 }
2391 if (blka == blkb) {
2392 ubifs_err("two data nodes for the same block");
2393 goto error_dump;
2394 }
2395 }
2396
2397 return 0;
2398
2399error_dump:
2400 dbg_dump_node(c, sa->node);
2401 dbg_dump_node(c, sb->node);
2402 return -EINVAL;
2403}
2404
2405/**
2406 * dbg_check_nondata_nodes_order - check that list of data nodes is sorted.
2407 * @c: UBIFS file-system description object
2408 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2409 *
2410 * This function returns zero if the list of non-data nodes is sorted correctly,
2411 * and %-EINVAL if not.
2412 */
2413int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head)
2414{
2415 struct list_head *cur;
2416 struct ubifs_scan_node *sa, *sb;
2417
2418 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
2419 return 0;
2420
2421 for (cur = head->next; cur->next != head; cur = cur->next) {
2422 ino_t inuma, inumb;
2423 uint32_t hasha, hashb;
2424
2425 cond_resched();
2426 sa = container_of(cur, struct ubifs_scan_node, list);
2427 sb = container_of(cur->next, struct ubifs_scan_node, list);
2428
2429 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2430 sa->type != UBIFS_XENT_NODE) {
2431 ubifs_err("bad node type %d", sa->type);
2432 dbg_dump_node(c, sa->node);
2433 return -EINVAL;
2434 }
2435 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2436 sa->type != UBIFS_XENT_NODE) {
2437 ubifs_err("bad node type %d", sb->type);
2438 dbg_dump_node(c, sb->node);
2439 return -EINVAL;
2440 }
2441
2442 if (sa->type != UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2443 ubifs_err("non-inode node goes before inode node");
2444 goto error_dump;
2445 }
2446
2447 if (sa->type == UBIFS_INO_NODE && sb->type != UBIFS_INO_NODE)
2448 continue;
2449
2450 if (sa->type == UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2451 /* Inode nodes are sorted in descending size order */
2452 if (sa->len < sb->len) {
2453 ubifs_err("smaller inode node goes first");
2454 goto error_dump;
2455 }
2456 continue;
2457 }
2458
2459 /*
2460 * This is either a dentry or xentry, which should be sorted in
2461 * ascending (parent ino, hash) order.
2462 */
2463 inuma = key_inum(c, &sa->key);
2464 inumb = key_inum(c, &sb->key);
2465
2466 if (inuma < inumb)
2467 continue;
2468 if (inuma > inumb) {
2469 ubifs_err("larger inum %lu goes before inum %lu",
2470 (unsigned long)inuma, (unsigned long)inumb);
2471 goto error_dump;
2472 }
2473
2474 hasha = key_block(c, &sa->key);
2475 hashb = key_block(c, &sb->key);
2476
2477 if (hasha > hashb) {
Artem Bityutskiyc4361572011-03-25 15:27:40 +02002478 ubifs_err("larger hash %u goes before %u",
2479 hasha, hashb);
Artem Bityutskiy3bb66b42010-08-07 10:06:11 +03002480 goto error_dump;
2481 }
2482 }
2483
2484 return 0;
2485
2486error_dump:
2487 ubifs_msg("dumping first node");
2488 dbg_dump_node(c, sa->node);
2489 ubifs_msg("dumping second node");
2490 dbg_dump_node(c, sb->node);
2491 return -EINVAL;
2492 return 0;
2493}
2494
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002495int dbg_force_in_the_gaps(void)
2496{
Artem Bityutskiybc3f07f2011-04-05 13:52:20 +03002497 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002498 return 0;
Artem Bityutskiybc3f07f2011-04-05 13:52:20 +03002499
2500 return !(random32() & 7);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002501}
2502
2503/* Failure mode for recovery testing */
2504
2505#define chance(n, d) (simple_rand() <= (n) * 32768LL / (d))
2506
2507struct failure_mode_info {
2508 struct list_head list;
2509 struct ubifs_info *c;
2510};
2511
2512static LIST_HEAD(fmi_list);
2513static DEFINE_SPINLOCK(fmi_lock);
2514
2515static unsigned int next;
2516
2517static int simple_rand(void)
2518{
2519 if (next == 0)
2520 next = current->pid;
2521 next = next * 1103515245 + 12345;
2522 return (next >> 16) & 32767;
2523}
2524
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002525static void failure_mode_init(struct ubifs_info *c)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002526{
2527 struct failure_mode_info *fmi;
2528
2529 fmi = kmalloc(sizeof(struct failure_mode_info), GFP_NOFS);
2530 if (!fmi) {
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002531 ubifs_err("Failed to register failure mode - no memory");
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002532 return;
2533 }
2534 fmi->c = c;
2535 spin_lock(&fmi_lock);
2536 list_add_tail(&fmi->list, &fmi_list);
2537 spin_unlock(&fmi_lock);
2538}
2539
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002540static void failure_mode_exit(struct ubifs_info *c)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002541{
2542 struct failure_mode_info *fmi, *tmp;
2543
2544 spin_lock(&fmi_lock);
2545 list_for_each_entry_safe(fmi, tmp, &fmi_list, list)
2546 if (fmi->c == c) {
2547 list_del(&fmi->list);
2548 kfree(fmi);
2549 }
2550 spin_unlock(&fmi_lock);
2551}
2552
2553static struct ubifs_info *dbg_find_info(struct ubi_volume_desc *desc)
2554{
2555 struct failure_mode_info *fmi;
2556
2557 spin_lock(&fmi_lock);
2558 list_for_each_entry(fmi, &fmi_list, list)
2559 if (fmi->c->ubi == desc) {
2560 struct ubifs_info *c = fmi->c;
2561
2562 spin_unlock(&fmi_lock);
2563 return c;
2564 }
2565 spin_unlock(&fmi_lock);
2566 return NULL;
2567}
2568
2569static int in_failure_mode(struct ubi_volume_desc *desc)
2570{
2571 struct ubifs_info *c = dbg_find_info(desc);
2572
2573 if (c && dbg_failure_mode)
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002574 return c->dbg->failure_mode;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002575 return 0;
2576}
2577
2578static int do_fail(struct ubi_volume_desc *desc, int lnum, int write)
2579{
2580 struct ubifs_info *c = dbg_find_info(desc);
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002581 struct ubifs_debug_info *d;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002582
2583 if (!c || !dbg_failure_mode)
2584 return 0;
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002585 d = c->dbg;
2586 if (d->failure_mode)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002587 return 1;
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002588 if (!d->fail_cnt) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002589 /* First call - decide delay to failure */
2590 if (chance(1, 2)) {
2591 unsigned int delay = 1 << (simple_rand() >> 11);
2592
2593 if (chance(1, 2)) {
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002594 d->fail_delay = 1;
2595 d->fail_timeout = jiffies +
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002596 msecs_to_jiffies(delay);
2597 dbg_rcvry("failing after %ums", delay);
2598 } else {
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002599 d->fail_delay = 2;
2600 d->fail_cnt_max = delay;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002601 dbg_rcvry("failing after %u calls", delay);
2602 }
2603 }
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002604 d->fail_cnt += 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002605 }
2606 /* Determine if failure delay has expired */
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002607 if (d->fail_delay == 1) {
2608 if (time_before(jiffies, d->fail_timeout))
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002609 return 0;
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002610 } else if (d->fail_delay == 2)
2611 if (d->fail_cnt++ < d->fail_cnt_max)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002612 return 0;
2613 if (lnum == UBIFS_SB_LNUM) {
2614 if (write) {
2615 if (chance(1, 2))
2616 return 0;
2617 } else if (chance(19, 20))
2618 return 0;
2619 dbg_rcvry("failing in super block LEB %d", lnum);
2620 } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2621 if (chance(19, 20))
2622 return 0;
2623 dbg_rcvry("failing in master LEB %d", lnum);
2624 } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2625 if (write) {
2626 if (chance(99, 100))
2627 return 0;
2628 } else if (chance(399, 400))
2629 return 0;
2630 dbg_rcvry("failing in log LEB %d", lnum);
2631 } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2632 if (write) {
2633 if (chance(7, 8))
2634 return 0;
2635 } else if (chance(19, 20))
2636 return 0;
2637 dbg_rcvry("failing in LPT LEB %d", lnum);
2638 } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2639 if (write) {
2640 if (chance(1, 2))
2641 return 0;
2642 } else if (chance(9, 10))
2643 return 0;
2644 dbg_rcvry("failing in orphan LEB %d", lnum);
2645 } else if (lnum == c->ihead_lnum) {
2646 if (chance(99, 100))
2647 return 0;
2648 dbg_rcvry("failing in index head LEB %d", lnum);
2649 } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2650 if (chance(9, 10))
2651 return 0;
2652 dbg_rcvry("failing in GC head LEB %d", lnum);
2653 } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
2654 !ubifs_search_bud(c, lnum)) {
2655 if (chance(19, 20))
2656 return 0;
2657 dbg_rcvry("failing in non-bud LEB %d", lnum);
2658 } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
2659 c->cmt_state == COMMIT_RUNNING_REQUIRED) {
2660 if (chance(999, 1000))
2661 return 0;
2662 dbg_rcvry("failing in bud LEB %d commit running", lnum);
2663 } else {
2664 if (chance(9999, 10000))
2665 return 0;
2666 dbg_rcvry("failing in bud LEB %d commit not running", lnum);
2667 }
2668 ubifs_err("*** SETTING FAILURE MODE ON (LEB %d) ***", lnum);
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002669 d->failure_mode = 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002670 dump_stack();
2671 return 1;
2672}
2673
2674static void cut_data(const void *buf, int len)
2675{
2676 int flen, i;
2677 unsigned char *p = (void *)buf;
2678
2679 flen = (len * (long long)simple_rand()) >> 15;
2680 for (i = flen; i < len; i++)
2681 p[i] = 0xff;
2682}
2683
2684int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
2685 int len, int check)
2686{
2687 if (in_failure_mode(desc))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002688 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002689 return ubi_leb_read(desc, lnum, buf, offset, len, check);
2690}
2691
2692int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
2693 int offset, int len, int dtype)
2694{
Adrian Hunter16dfd802008-07-18 16:47:41 +03002695 int err, failing;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002696
2697 if (in_failure_mode(desc))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002698 return -EROFS;
Adrian Hunter16dfd802008-07-18 16:47:41 +03002699 failing = do_fail(desc, lnum, 1);
2700 if (failing)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002701 cut_data(buf, len);
2702 err = ubi_leb_write(desc, lnum, buf, offset, len, dtype);
2703 if (err)
2704 return err;
Adrian Hunter16dfd802008-07-18 16:47:41 +03002705 if (failing)
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002706 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002707 return 0;
2708}
2709
2710int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
2711 int len, int dtype)
2712{
2713 int err;
2714
2715 if (do_fail(desc, lnum, 1))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002716 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002717 err = ubi_leb_change(desc, lnum, buf, len, dtype);
2718 if (err)
2719 return err;
2720 if (do_fail(desc, lnum, 1))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002721 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002722 return 0;
2723}
2724
2725int dbg_leb_erase(struct ubi_volume_desc *desc, int lnum)
2726{
2727 int err;
2728
2729 if (do_fail(desc, lnum, 0))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002730 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002731 err = ubi_leb_erase(desc, lnum);
2732 if (err)
2733 return err;
2734 if (do_fail(desc, lnum, 0))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002735 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002736 return 0;
2737}
2738
2739int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum)
2740{
2741 int err;
2742
2743 if (do_fail(desc, lnum, 0))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002744 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002745 err = ubi_leb_unmap(desc, lnum);
2746 if (err)
2747 return err;
2748 if (do_fail(desc, lnum, 0))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002749 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002750 return 0;
2751}
2752
2753int dbg_is_mapped(struct ubi_volume_desc *desc, int lnum)
2754{
2755 if (in_failure_mode(desc))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002756 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002757 return ubi_is_mapped(desc, lnum);
2758}
2759
2760int dbg_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
2761{
2762 int err;
2763
2764 if (do_fail(desc, lnum, 0))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002765 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002766 err = ubi_leb_map(desc, lnum, dtype);
2767 if (err)
2768 return err;
2769 if (do_fail(desc, lnum, 0))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002770 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002771 return 0;
2772}
2773
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002774/**
2775 * ubifs_debugging_init - initialize UBIFS debugging.
2776 * @c: UBIFS file-system description object
2777 *
2778 * This function initializes debugging-related data for the file system.
2779 * Returns zero in case of success and a negative error code in case of
2780 * failure.
2781 */
2782int ubifs_debugging_init(struct ubifs_info *c)
2783{
2784 c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
2785 if (!c->dbg)
2786 return -ENOMEM;
2787
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002788 failure_mode_init(c);
2789 return 0;
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002790}
2791
2792/**
2793 * ubifs_debugging_exit - free debugging data.
2794 * @c: UBIFS file-system description object
2795 */
2796void ubifs_debugging_exit(struct ubifs_info *c)
2797{
2798 failure_mode_exit(c);
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002799 kfree(c->dbg);
2800}
2801
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002802/*
2803 * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
2804 * contain the stuff specific to particular file-system mounts.
2805 */
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002806static struct dentry *dfs_rootdir;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002807
2808/**
2809 * dbg_debugfs_init - initialize debugfs file-system.
2810 *
2811 * UBIFS uses debugfs file-system to expose various debugging knobs to
2812 * user-space. This function creates "ubifs" directory in the debugfs
2813 * file-system. Returns zero in case of success and a negative error code in
2814 * case of failure.
2815 */
2816int dbg_debugfs_init(void)
2817{
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002818 dfs_rootdir = debugfs_create_dir("ubifs", NULL);
2819 if (IS_ERR(dfs_rootdir)) {
2820 int err = PTR_ERR(dfs_rootdir);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002821 ubifs_err("cannot create \"ubifs\" debugfs directory, "
2822 "error %d\n", err);
2823 return err;
2824 }
2825
2826 return 0;
2827}
2828
2829/**
2830 * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
2831 */
2832void dbg_debugfs_exit(void)
2833{
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002834 debugfs_remove(dfs_rootdir);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002835}
2836
2837static int open_debugfs_file(struct inode *inode, struct file *file)
2838{
2839 file->private_data = inode->i_private;
Artem Bityutskiy1bbfc842011-03-21 16:26:42 +02002840 return nonseekable_open(inode, file);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002841}
2842
2843static ssize_t write_debugfs_file(struct file *file, const char __user *buf,
2844 size_t count, loff_t *ppos)
2845{
2846 struct ubifs_info *c = file->private_data;
2847 struct ubifs_debug_info *d = c->dbg;
2848
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002849 if (file->f_path.dentry == d->dfs_dump_lprops)
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002850 dbg_dump_lprops(c);
Artem Bityutskiy8ff83082011-03-29 18:19:50 +03002851 else if (file->f_path.dentry == d->dfs_dump_budg)
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +03002852 dbg_dump_budg(c, &c->bi);
Artem Bityutskiy8ff83082011-03-29 18:19:50 +03002853 else if (file->f_path.dentry == d->dfs_dump_tnc) {
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002854 mutex_lock(&c->tnc_mutex);
2855 dbg_dump_tnc(c);
2856 mutex_unlock(&c->tnc_mutex);
2857 } else
2858 return -EINVAL;
2859
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002860 return count;
2861}
2862
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002863static const struct file_operations dfs_fops = {
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002864 .open = open_debugfs_file,
2865 .write = write_debugfs_file,
2866 .owner = THIS_MODULE,
Artem Bityutskiy1bbfc842011-03-21 16:26:42 +02002867 .llseek = no_llseek,
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002868};
2869
2870/**
2871 * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
2872 * @c: UBIFS file-system description object
2873 *
2874 * This function creates all debugfs files for this instance of UBIFS. Returns
2875 * zero in case of success and a negative error code in case of failure.
2876 *
2877 * Note, the only reason we have not merged this function with the
2878 * 'ubifs_debugging_init()' function is because it is better to initialize
2879 * debugfs interfaces at the very end of the mount process, and remove them at
2880 * the very beginning of the mount process.
2881 */
2882int dbg_debugfs_init_fs(struct ubifs_info *c)
2883{
2884 int err;
2885 const char *fname;
2886 struct dentry *dent;
2887 struct ubifs_debug_info *d = c->dbg;
2888
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002889 sprintf(d->dfs_dir_name, "ubi%d_%d", c->vi.ubi_num, c->vi.vol_id);
Artem Bityutskiycc6a86b2011-04-01 10:10:52 +03002890 fname = d->dfs_dir_name;
2891 dent = debugfs_create_dir(fname, dfs_rootdir);
Artem Bityutskiy95169532011-04-01 10:16:17 +03002892 if (IS_ERR_OR_NULL(dent))
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002893 goto out;
Artem Bityutskiycc6a86b2011-04-01 10:10:52 +03002894 d->dfs_dir = dent;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002895
2896 fname = "dump_lprops";
Vasiliy Kulikov8c559d32011-02-04 15:24:19 +03002897 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
Artem Bityutskiy95169532011-04-01 10:16:17 +03002898 if (IS_ERR_OR_NULL(dent))
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002899 goto out_remove;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002900 d->dfs_dump_lprops = dent;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002901
2902 fname = "dump_budg";
Vasiliy Kulikov8c559d32011-02-04 15:24:19 +03002903 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
Artem Bityutskiy95169532011-04-01 10:16:17 +03002904 if (IS_ERR_OR_NULL(dent))
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002905 goto out_remove;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002906 d->dfs_dump_budg = dent;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002907
2908 fname = "dump_tnc";
Vasiliy Kulikov8c559d32011-02-04 15:24:19 +03002909 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
Artem Bityutskiy95169532011-04-01 10:16:17 +03002910 if (IS_ERR_OR_NULL(dent))
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002911 goto out_remove;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002912 d->dfs_dump_tnc = dent;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002913
2914 return 0;
2915
2916out_remove:
Artem Bityutskiycc6a86b2011-04-01 10:10:52 +03002917 debugfs_remove_recursive(d->dfs_dir);
2918out:
Artem Bityutskiy95169532011-04-01 10:16:17 +03002919 err = dent ? PTR_ERR(dent) : -ENODEV;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002920 ubifs_err("cannot create \"%s\" debugfs directory, error %d\n",
2921 fname, err);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002922 return err;
2923}
2924
2925/**
2926 * dbg_debugfs_exit_fs - remove all debugfs files.
2927 * @c: UBIFS file-system description object
2928 */
2929void dbg_debugfs_exit_fs(struct ubifs_info *c)
2930{
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002931 debugfs_remove_recursive(c->dbg->dfs_dir);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002932}
2933
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002934#endif /* CONFIG_UBIFS_FS_DEBUG */