blob: c9609a63512ed504597b9e7dde5b39318d2ada31 [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_chk_flags;
Artem Bityutskiy1e517642008-07-14 19:08:37 +030046unsigned int ubifs_tst_flags;
47
Artem Bityutskiy1e517642008-07-14 19:08:37 +030048module_param_named(debug_chks, ubifs_chk_flags, uint, S_IRUGO | S_IWUSR);
49module_param_named(debug_tsts, ubifs_tst_flags, uint, S_IRUGO | S_IWUSR);
50
Artem Bityutskiy1e517642008-07-14 19:08:37 +030051MODULE_PARM_DESC(debug_chks, "Debug check flags");
52MODULE_PARM_DESC(debug_tsts, "Debug special test flags");
53
54static const char *get_key_fmt(int fmt)
55{
56 switch (fmt) {
57 case UBIFS_SIMPLE_KEY_FMT:
58 return "simple";
59 default:
60 return "unknown/invalid format";
61 }
62}
63
64static const char *get_key_hash(int hash)
65{
66 switch (hash) {
67 case UBIFS_KEY_HASH_R5:
68 return "R5";
69 case UBIFS_KEY_HASH_TEST:
70 return "test";
71 default:
72 return "unknown/invalid name hash";
73 }
74}
75
76static const char *get_key_type(int type)
77{
78 switch (type) {
79 case UBIFS_INO_KEY:
80 return "inode";
81 case UBIFS_DENT_KEY:
82 return "direntry";
83 case UBIFS_XENT_KEY:
84 return "xentry";
85 case UBIFS_DATA_KEY:
86 return "data";
87 case UBIFS_TRUN_KEY:
88 return "truncate";
89 default:
90 return "unknown/invalid key";
91 }
92}
93
94static void sprintf_key(const struct ubifs_info *c, const union ubifs_key *key,
95 char *buffer)
96{
97 char *p = buffer;
98 int type = key_type(c, key);
99
100 if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
101 switch (type) {
102 case UBIFS_INO_KEY:
Artem Bityutskiye84461a2008-10-29 12:08:43 +0200103 sprintf(p, "(%lu, %s)", (unsigned long)key_inum(c, key),
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300104 get_key_type(type));
105 break;
106 case UBIFS_DENT_KEY:
107 case UBIFS_XENT_KEY:
Artem Bityutskiye84461a2008-10-29 12:08:43 +0200108 sprintf(p, "(%lu, %s, %#08x)",
109 (unsigned long)key_inum(c, key),
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300110 get_key_type(type), key_hash(c, key));
111 break;
112 case UBIFS_DATA_KEY:
Artem Bityutskiye84461a2008-10-29 12:08:43 +0200113 sprintf(p, "(%lu, %s, %u)",
114 (unsigned long)key_inum(c, key),
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300115 get_key_type(type), key_block(c, key));
116 break;
117 case UBIFS_TRUN_KEY:
118 sprintf(p, "(%lu, %s)",
Artem Bityutskiye84461a2008-10-29 12:08:43 +0200119 (unsigned long)key_inum(c, key),
120 get_key_type(type));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300121 break;
122 default:
123 sprintf(p, "(bad key type: %#08x, %#08x)",
124 key->u32[0], key->u32[1]);
125 }
126 } else
127 sprintf(p, "bad key format %d", c->key_fmt);
128}
129
130const char *dbg_key_str0(const struct ubifs_info *c, const union ubifs_key *key)
131{
132 /* dbg_lock must be held */
133 sprintf_key(c, key, dbg_key_buf0);
134 return dbg_key_buf0;
135}
136
137const char *dbg_key_str1(const struct ubifs_info *c, const union ubifs_key *key)
138{
139 /* dbg_lock must be held */
140 sprintf_key(c, key, dbg_key_buf1);
141 return dbg_key_buf1;
142}
143
144const char *dbg_ntype(int type)
145{
146 switch (type) {
147 case UBIFS_PAD_NODE:
148 return "padding node";
149 case UBIFS_SB_NODE:
150 return "superblock node";
151 case UBIFS_MST_NODE:
152 return "master node";
153 case UBIFS_REF_NODE:
154 return "reference node";
155 case UBIFS_INO_NODE:
156 return "inode node";
157 case UBIFS_DENT_NODE:
158 return "direntry node";
159 case UBIFS_XENT_NODE:
160 return "xentry node";
161 case UBIFS_DATA_NODE:
162 return "data node";
163 case UBIFS_TRUN_NODE:
164 return "truncate node";
165 case UBIFS_IDX_NODE:
166 return "indexing node";
167 case UBIFS_CS_NODE:
168 return "commit start node";
169 case UBIFS_ORPH_NODE:
170 return "orphan node";
171 default:
172 return "unknown node";
173 }
174}
175
176static const char *dbg_gtype(int type)
177{
178 switch (type) {
179 case UBIFS_NO_NODE_GROUP:
180 return "no node group";
181 case UBIFS_IN_NODE_GROUP:
182 return "in node group";
183 case UBIFS_LAST_OF_NODE_GROUP:
184 return "last of node group";
185 default:
186 return "unknown";
187 }
188}
189
190const char *dbg_cstate(int cmt_state)
191{
192 switch (cmt_state) {
193 case COMMIT_RESTING:
194 return "commit resting";
195 case COMMIT_BACKGROUND:
196 return "background commit requested";
197 case COMMIT_REQUIRED:
198 return "commit required";
199 case COMMIT_RUNNING_BACKGROUND:
200 return "BACKGROUND commit running";
201 case COMMIT_RUNNING_REQUIRED:
202 return "commit running and required";
203 case COMMIT_BROKEN:
204 return "broken commit";
205 default:
206 return "unknown commit state";
207 }
208}
209
Artem Bityutskiy77a7ae52009-09-15 15:03:51 +0300210const char *dbg_jhead(int jhead)
211{
212 switch (jhead) {
213 case GCHD:
214 return "0 (GC)";
215 case BASEHD:
216 return "1 (base)";
217 case DATAHD:
218 return "2 (data)";
219 default:
220 return "unknown journal head";
221 }
222}
223
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300224static void dump_ch(const struct ubifs_ch *ch)
225{
226 printk(KERN_DEBUG "\tmagic %#x\n", le32_to_cpu(ch->magic));
227 printk(KERN_DEBUG "\tcrc %#x\n", le32_to_cpu(ch->crc));
228 printk(KERN_DEBUG "\tnode_type %d (%s)\n", ch->node_type,
229 dbg_ntype(ch->node_type));
230 printk(KERN_DEBUG "\tgroup_type %d (%s)\n", ch->group_type,
231 dbg_gtype(ch->group_type));
232 printk(KERN_DEBUG "\tsqnum %llu\n",
233 (unsigned long long)le64_to_cpu(ch->sqnum));
234 printk(KERN_DEBUG "\tlen %u\n", le32_to_cpu(ch->len));
235}
236
237void dbg_dump_inode(const struct ubifs_info *c, const struct inode *inode)
238{
239 const struct ubifs_inode *ui = ubifs_inode(inode);
240
Artem Bityutskiyb5e426e2008-09-09 11:20:35 +0300241 printk(KERN_DEBUG "Dump in-memory inode:");
242 printk(KERN_DEBUG "\tinode %lu\n", inode->i_ino);
243 printk(KERN_DEBUG "\tsize %llu\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300244 (unsigned long long)i_size_read(inode));
Artem Bityutskiyb5e426e2008-09-09 11:20:35 +0300245 printk(KERN_DEBUG "\tnlink %u\n", inode->i_nlink);
246 printk(KERN_DEBUG "\tuid %u\n", (unsigned int)inode->i_uid);
247 printk(KERN_DEBUG "\tgid %u\n", (unsigned int)inode->i_gid);
248 printk(KERN_DEBUG "\tatime %u.%u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300249 (unsigned int)inode->i_atime.tv_sec,
250 (unsigned int)inode->i_atime.tv_nsec);
Artem Bityutskiyb5e426e2008-09-09 11:20:35 +0300251 printk(KERN_DEBUG "\tmtime %u.%u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300252 (unsigned int)inode->i_mtime.tv_sec,
253 (unsigned int)inode->i_mtime.tv_nsec);
Artem Bityutskiyb5e426e2008-09-09 11:20:35 +0300254 printk(KERN_DEBUG "\tctime %u.%u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300255 (unsigned int)inode->i_ctime.tv_sec,
256 (unsigned int)inode->i_ctime.tv_nsec);
Artem Bityutskiyb5e426e2008-09-09 11:20:35 +0300257 printk(KERN_DEBUG "\tcreat_sqnum %llu\n", ui->creat_sqnum);
258 printk(KERN_DEBUG "\txattr_size %u\n", ui->xattr_size);
259 printk(KERN_DEBUG "\txattr_cnt %u\n", ui->xattr_cnt);
260 printk(KERN_DEBUG "\txattr_names %u\n", ui->xattr_names);
261 printk(KERN_DEBUG "\tdirty %u\n", ui->dirty);
262 printk(KERN_DEBUG "\txattr %u\n", ui->xattr);
263 printk(KERN_DEBUG "\tbulk_read %u\n", ui->xattr);
264 printk(KERN_DEBUG "\tsynced_i_size %llu\n",
265 (unsigned long long)ui->synced_i_size);
266 printk(KERN_DEBUG "\tui_size %llu\n",
267 (unsigned long long)ui->ui_size);
268 printk(KERN_DEBUG "\tflags %d\n", ui->flags);
269 printk(KERN_DEBUG "\tcompr_type %d\n", ui->compr_type);
270 printk(KERN_DEBUG "\tlast_page_read %lu\n", ui->last_page_read);
271 printk(KERN_DEBUG "\tread_in_a_row %lu\n", ui->read_in_a_row);
272 printk(KERN_DEBUG "\tdata_len %d\n", ui->data_len);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300273}
274
275void dbg_dump_node(const struct ubifs_info *c, const void *node)
276{
277 int i, n;
278 union ubifs_key key;
279 const struct ubifs_ch *ch = node;
280
281 if (dbg_failure_mode)
282 return;
283
284 /* If the magic is incorrect, just hexdump the first bytes */
285 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
286 printk(KERN_DEBUG "Not a node, first %zu bytes:", UBIFS_CH_SZ);
287 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
288 (void *)node, UBIFS_CH_SZ, 1);
289 return;
290 }
291
292 spin_lock(&dbg_lock);
293 dump_ch(node);
294
295 switch (ch->node_type) {
296 case UBIFS_PAD_NODE:
297 {
298 const struct ubifs_pad_node *pad = node;
299
300 printk(KERN_DEBUG "\tpad_len %u\n",
301 le32_to_cpu(pad->pad_len));
302 break;
303 }
304 case UBIFS_SB_NODE:
305 {
306 const struct ubifs_sb_node *sup = node;
307 unsigned int sup_flags = le32_to_cpu(sup->flags);
308
309 printk(KERN_DEBUG "\tkey_hash %d (%s)\n",
310 (int)sup->key_hash, get_key_hash(sup->key_hash));
311 printk(KERN_DEBUG "\tkey_fmt %d (%s)\n",
312 (int)sup->key_fmt, get_key_fmt(sup->key_fmt));
313 printk(KERN_DEBUG "\tflags %#x\n", sup_flags);
314 printk(KERN_DEBUG "\t big_lpt %u\n",
315 !!(sup_flags & UBIFS_FLG_BIGLPT));
Matthew L. Creech9f58d352011-05-05 16:33:20 -0400316 printk(KERN_DEBUG "\t space_fixup %u\n",
317 !!(sup_flags & UBIFS_FLG_SPACE_FIXUP));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300318 printk(KERN_DEBUG "\tmin_io_size %u\n",
319 le32_to_cpu(sup->min_io_size));
320 printk(KERN_DEBUG "\tleb_size %u\n",
321 le32_to_cpu(sup->leb_size));
322 printk(KERN_DEBUG "\tleb_cnt %u\n",
323 le32_to_cpu(sup->leb_cnt));
324 printk(KERN_DEBUG "\tmax_leb_cnt %u\n",
325 le32_to_cpu(sup->max_leb_cnt));
326 printk(KERN_DEBUG "\tmax_bud_bytes %llu\n",
327 (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
328 printk(KERN_DEBUG "\tlog_lebs %u\n",
329 le32_to_cpu(sup->log_lebs));
330 printk(KERN_DEBUG "\tlpt_lebs %u\n",
331 le32_to_cpu(sup->lpt_lebs));
332 printk(KERN_DEBUG "\torph_lebs %u\n",
333 le32_to_cpu(sup->orph_lebs));
334 printk(KERN_DEBUG "\tjhead_cnt %u\n",
335 le32_to_cpu(sup->jhead_cnt));
336 printk(KERN_DEBUG "\tfanout %u\n",
337 le32_to_cpu(sup->fanout));
338 printk(KERN_DEBUG "\tlsave_cnt %u\n",
339 le32_to_cpu(sup->lsave_cnt));
340 printk(KERN_DEBUG "\tdefault_compr %u\n",
341 (int)le16_to_cpu(sup->default_compr));
342 printk(KERN_DEBUG "\trp_size %llu\n",
343 (unsigned long long)le64_to_cpu(sup->rp_size));
344 printk(KERN_DEBUG "\trp_uid %u\n",
345 le32_to_cpu(sup->rp_uid));
346 printk(KERN_DEBUG "\trp_gid %u\n",
347 le32_to_cpu(sup->rp_gid));
348 printk(KERN_DEBUG "\tfmt_version %u\n",
349 le32_to_cpu(sup->fmt_version));
350 printk(KERN_DEBUG "\ttime_gran %u\n",
351 le32_to_cpu(sup->time_gran));
Joe Perches7f2f4e72009-12-14 18:01:13 -0800352 printk(KERN_DEBUG "\tUUID %pUB\n",
353 sup->uuid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300354 break;
355 }
356 case UBIFS_MST_NODE:
357 {
358 const struct ubifs_mst_node *mst = node;
359
360 printk(KERN_DEBUG "\thighest_inum %llu\n",
361 (unsigned long long)le64_to_cpu(mst->highest_inum));
362 printk(KERN_DEBUG "\tcommit number %llu\n",
363 (unsigned long long)le64_to_cpu(mst->cmt_no));
364 printk(KERN_DEBUG "\tflags %#x\n",
365 le32_to_cpu(mst->flags));
366 printk(KERN_DEBUG "\tlog_lnum %u\n",
367 le32_to_cpu(mst->log_lnum));
368 printk(KERN_DEBUG "\troot_lnum %u\n",
369 le32_to_cpu(mst->root_lnum));
370 printk(KERN_DEBUG "\troot_offs %u\n",
371 le32_to_cpu(mst->root_offs));
372 printk(KERN_DEBUG "\troot_len %u\n",
373 le32_to_cpu(mst->root_len));
374 printk(KERN_DEBUG "\tgc_lnum %u\n",
375 le32_to_cpu(mst->gc_lnum));
376 printk(KERN_DEBUG "\tihead_lnum %u\n",
377 le32_to_cpu(mst->ihead_lnum));
378 printk(KERN_DEBUG "\tihead_offs %u\n",
379 le32_to_cpu(mst->ihead_offs));
Harvey Harrison0ecb9522008-10-24 10:52:57 -0700380 printk(KERN_DEBUG "\tindex_size %llu\n",
381 (unsigned long long)le64_to_cpu(mst->index_size));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300382 printk(KERN_DEBUG "\tlpt_lnum %u\n",
383 le32_to_cpu(mst->lpt_lnum));
384 printk(KERN_DEBUG "\tlpt_offs %u\n",
385 le32_to_cpu(mst->lpt_offs));
386 printk(KERN_DEBUG "\tnhead_lnum %u\n",
387 le32_to_cpu(mst->nhead_lnum));
388 printk(KERN_DEBUG "\tnhead_offs %u\n",
389 le32_to_cpu(mst->nhead_offs));
390 printk(KERN_DEBUG "\tltab_lnum %u\n",
391 le32_to_cpu(mst->ltab_lnum));
392 printk(KERN_DEBUG "\tltab_offs %u\n",
393 le32_to_cpu(mst->ltab_offs));
394 printk(KERN_DEBUG "\tlsave_lnum %u\n",
395 le32_to_cpu(mst->lsave_lnum));
396 printk(KERN_DEBUG "\tlsave_offs %u\n",
397 le32_to_cpu(mst->lsave_offs));
398 printk(KERN_DEBUG "\tlscan_lnum %u\n",
399 le32_to_cpu(mst->lscan_lnum));
400 printk(KERN_DEBUG "\tleb_cnt %u\n",
401 le32_to_cpu(mst->leb_cnt));
402 printk(KERN_DEBUG "\tempty_lebs %u\n",
403 le32_to_cpu(mst->empty_lebs));
404 printk(KERN_DEBUG "\tidx_lebs %u\n",
405 le32_to_cpu(mst->idx_lebs));
406 printk(KERN_DEBUG "\ttotal_free %llu\n",
407 (unsigned long long)le64_to_cpu(mst->total_free));
408 printk(KERN_DEBUG "\ttotal_dirty %llu\n",
409 (unsigned long long)le64_to_cpu(mst->total_dirty));
410 printk(KERN_DEBUG "\ttotal_used %llu\n",
411 (unsigned long long)le64_to_cpu(mst->total_used));
412 printk(KERN_DEBUG "\ttotal_dead %llu\n",
413 (unsigned long long)le64_to_cpu(mst->total_dead));
414 printk(KERN_DEBUG "\ttotal_dark %llu\n",
415 (unsigned long long)le64_to_cpu(mst->total_dark));
416 break;
417 }
418 case UBIFS_REF_NODE:
419 {
420 const struct ubifs_ref_node *ref = node;
421
422 printk(KERN_DEBUG "\tlnum %u\n",
423 le32_to_cpu(ref->lnum));
424 printk(KERN_DEBUG "\toffs %u\n",
425 le32_to_cpu(ref->offs));
426 printk(KERN_DEBUG "\tjhead %u\n",
427 le32_to_cpu(ref->jhead));
428 break;
429 }
430 case UBIFS_INO_NODE:
431 {
432 const struct ubifs_ino_node *ino = node;
433
434 key_read(c, &ino->key, &key);
435 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
436 printk(KERN_DEBUG "\tcreat_sqnum %llu\n",
437 (unsigned long long)le64_to_cpu(ino->creat_sqnum));
438 printk(KERN_DEBUG "\tsize %llu\n",
439 (unsigned long long)le64_to_cpu(ino->size));
440 printk(KERN_DEBUG "\tnlink %u\n",
441 le32_to_cpu(ino->nlink));
442 printk(KERN_DEBUG "\tatime %lld.%u\n",
443 (long long)le64_to_cpu(ino->atime_sec),
444 le32_to_cpu(ino->atime_nsec));
445 printk(KERN_DEBUG "\tmtime %lld.%u\n",
446 (long long)le64_to_cpu(ino->mtime_sec),
447 le32_to_cpu(ino->mtime_nsec));
448 printk(KERN_DEBUG "\tctime %lld.%u\n",
449 (long long)le64_to_cpu(ino->ctime_sec),
450 le32_to_cpu(ino->ctime_nsec));
451 printk(KERN_DEBUG "\tuid %u\n",
452 le32_to_cpu(ino->uid));
453 printk(KERN_DEBUG "\tgid %u\n",
454 le32_to_cpu(ino->gid));
455 printk(KERN_DEBUG "\tmode %u\n",
456 le32_to_cpu(ino->mode));
457 printk(KERN_DEBUG "\tflags %#x\n",
458 le32_to_cpu(ino->flags));
459 printk(KERN_DEBUG "\txattr_cnt %u\n",
460 le32_to_cpu(ino->xattr_cnt));
461 printk(KERN_DEBUG "\txattr_size %u\n",
462 le32_to_cpu(ino->xattr_size));
463 printk(KERN_DEBUG "\txattr_names %u\n",
464 le32_to_cpu(ino->xattr_names));
465 printk(KERN_DEBUG "\tcompr_type %#x\n",
466 (int)le16_to_cpu(ino->compr_type));
467 printk(KERN_DEBUG "\tdata len %u\n",
468 le32_to_cpu(ino->data_len));
469 break;
470 }
471 case UBIFS_DENT_NODE:
472 case UBIFS_XENT_NODE:
473 {
474 const struct ubifs_dent_node *dent = node;
475 int nlen = le16_to_cpu(dent->nlen);
476
477 key_read(c, &dent->key, &key);
478 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
479 printk(KERN_DEBUG "\tinum %llu\n",
480 (unsigned long long)le64_to_cpu(dent->inum));
481 printk(KERN_DEBUG "\ttype %d\n", (int)dent->type);
482 printk(KERN_DEBUG "\tnlen %d\n", nlen);
483 printk(KERN_DEBUG "\tname ");
484
485 if (nlen > UBIFS_MAX_NLEN)
486 printk(KERN_DEBUG "(bad name length, not printing, "
487 "bad or corrupted node)");
488 else {
489 for (i = 0; i < nlen && dent->name[i]; i++)
Artem Bityutskiyc9927c32009-03-16 09:42:03 +0200490 printk(KERN_CONT "%c", dent->name[i]);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300491 }
Artem Bityutskiyc9927c32009-03-16 09:42:03 +0200492 printk(KERN_CONT "\n");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300493
494 break;
495 }
496 case UBIFS_DATA_NODE:
497 {
498 const struct ubifs_data_node *dn = node;
499 int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
500
501 key_read(c, &dn->key, &key);
502 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
503 printk(KERN_DEBUG "\tsize %u\n",
504 le32_to_cpu(dn->size));
505 printk(KERN_DEBUG "\tcompr_typ %d\n",
506 (int)le16_to_cpu(dn->compr_type));
507 printk(KERN_DEBUG "\tdata size %d\n",
508 dlen);
509 printk(KERN_DEBUG "\tdata:\n");
510 print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET, 32, 1,
511 (void *)&dn->data, dlen, 0);
512 break;
513 }
514 case UBIFS_TRUN_NODE:
515 {
516 const struct ubifs_trun_node *trun = node;
517
518 printk(KERN_DEBUG "\tinum %u\n",
519 le32_to_cpu(trun->inum));
520 printk(KERN_DEBUG "\told_size %llu\n",
521 (unsigned long long)le64_to_cpu(trun->old_size));
522 printk(KERN_DEBUG "\tnew_size %llu\n",
523 (unsigned long long)le64_to_cpu(trun->new_size));
524 break;
525 }
526 case UBIFS_IDX_NODE:
527 {
528 const struct ubifs_idx_node *idx = node;
529
530 n = le16_to_cpu(idx->child_cnt);
531 printk(KERN_DEBUG "\tchild_cnt %d\n", n);
532 printk(KERN_DEBUG "\tlevel %d\n",
533 (int)le16_to_cpu(idx->level));
534 printk(KERN_DEBUG "\tBranches:\n");
535
536 for (i = 0; i < n && i < c->fanout - 1; i++) {
537 const struct ubifs_branch *br;
538
539 br = ubifs_idx_branch(c, idx, i);
540 key_read(c, &br->key, &key);
541 printk(KERN_DEBUG "\t%d: LEB %d:%d len %d key %s\n",
542 i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
543 le32_to_cpu(br->len), DBGKEY(&key));
544 }
545 break;
546 }
547 case UBIFS_CS_NODE:
548 break;
549 case UBIFS_ORPH_NODE:
550 {
551 const struct ubifs_orph_node *orph = node;
552
553 printk(KERN_DEBUG "\tcommit number %llu\n",
554 (unsigned long long)
555 le64_to_cpu(orph->cmt_no) & LLONG_MAX);
556 printk(KERN_DEBUG "\tlast node flag %llu\n",
557 (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
558 n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
559 printk(KERN_DEBUG "\t%d orphan inode numbers:\n", n);
560 for (i = 0; i < n; i++)
561 printk(KERN_DEBUG "\t ino %llu\n",
Alexander Beregalov7424bac2008-09-17 22:09:41 +0400562 (unsigned long long)le64_to_cpu(orph->inos[i]));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300563 break;
564 }
565 default:
566 printk(KERN_DEBUG "node type %d was not recognized\n",
567 (int)ch->node_type);
568 }
569 spin_unlock(&dbg_lock);
570}
571
572void dbg_dump_budget_req(const struct ubifs_budget_req *req)
573{
574 spin_lock(&dbg_lock);
575 printk(KERN_DEBUG "Budgeting request: new_ino %d, dirtied_ino %d\n",
576 req->new_ino, req->dirtied_ino);
577 printk(KERN_DEBUG "\tnew_ino_d %d, dirtied_ino_d %d\n",
578 req->new_ino_d, req->dirtied_ino_d);
579 printk(KERN_DEBUG "\tnew_page %d, dirtied_page %d\n",
580 req->new_page, req->dirtied_page);
581 printk(KERN_DEBUG "\tnew_dent %d, mod_dent %d\n",
582 req->new_dent, req->mod_dent);
583 printk(KERN_DEBUG "\tidx_growth %d\n", req->idx_growth);
584 printk(KERN_DEBUG "\tdata_growth %d dd_growth %d\n",
585 req->data_growth, req->dd_growth);
586 spin_unlock(&dbg_lock);
587}
588
589void dbg_dump_lstats(const struct ubifs_lp_stats *lst)
590{
591 spin_lock(&dbg_lock);
Artem Bityutskiy1de94152008-07-25 12:58:38 +0300592 printk(KERN_DEBUG "(pid %d) Lprops statistics: empty_lebs %d, "
593 "idx_lebs %d\n", current->pid, lst->empty_lebs, lst->idx_lebs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300594 printk(KERN_DEBUG "\ttaken_empty_lebs %d, total_free %lld, "
595 "total_dirty %lld\n", lst->taken_empty_lebs, lst->total_free,
596 lst->total_dirty);
597 printk(KERN_DEBUG "\ttotal_used %lld, total_dark %lld, "
598 "total_dead %lld\n", lst->total_used, lst->total_dark,
599 lst->total_dead);
600 spin_unlock(&dbg_lock);
601}
602
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300603void dbg_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300604{
605 int i;
606 struct rb_node *rb;
607 struct ubifs_bud *bud;
608 struct ubifs_gced_idx_leb *idx_gc;
Artem Bityutskiy21a60252008-12-12 11:13:17 -0500609 long long available, outstanding, free;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300610
Artem Bityutskiy8ff83082011-03-29 18:19:50 +0300611 spin_lock(&c->space_lock);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300612 spin_lock(&dbg_lock);
Artem Bityutskiy8c3067e2011-03-30 13:18:58 +0300613 printk(KERN_DEBUG "(pid %d) Budgeting info: data budget sum %lld, "
614 "total budget sum %lld\n", current->pid,
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300615 bi->data_growth + bi->dd_growth,
616 bi->data_growth + bi->dd_growth + bi->idx_growth);
Artem Bityutskiy8c3067e2011-03-30 13:18:58 +0300617 printk(KERN_DEBUG "\tbudg_data_growth %lld, budg_dd_growth %lld, "
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300618 "budg_idx_growth %lld\n", bi->data_growth, bi->dd_growth,
619 bi->idx_growth);
Artem Bityutskiy8c3067e2011-03-30 13:18:58 +0300620 printk(KERN_DEBUG "\tmin_idx_lebs %d, old_idx_sz %llu, "
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300621 "uncommitted_idx %lld\n", bi->min_idx_lebs, bi->old_idx_sz,
622 bi->uncommitted_idx);
Artem Bityutskiy8c3067e2011-03-30 13:18:58 +0300623 printk(KERN_DEBUG "\tpage_budget %d, inode_budget %d, dent_budget %d\n",
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300624 bi->page_budget, bi->inode_budget, bi->dent_budget);
Artem Bityutskiy8c3067e2011-03-30 13:18:58 +0300625 printk(KERN_DEBUG "\tnospace %u, nospace_rp %u\n",
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300626 bi->nospace, bi->nospace_rp);
Artem Bityutskiy8c3067e2011-03-30 13:18:58 +0300627 printk(KERN_DEBUG "\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
628 c->dark_wm, c->dead_wm, c->max_idx_node_sz);
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300629
630 if (bi != &c->bi)
631 /*
632 * If we are dumping saved budgeting data, do not print
633 * additional information which is about the current state, not
634 * the old one which corresponded to the saved budgeting data.
635 */
636 goto out_unlock;
637
Artem Bityutskiy8c3067e2011-03-30 13:18:58 +0300638 printk(KERN_DEBUG "\tfreeable_cnt %d, calc_idx_sz %lld, idx_gc_cnt %d\n",
639 c->freeable_cnt, c->calc_idx_sz, c->idx_gc_cnt);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300640 printk(KERN_DEBUG "\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, "
641 "clean_zn_cnt %ld\n", atomic_long_read(&c->dirty_pg_cnt),
642 atomic_long_read(&c->dirty_zn_cnt),
643 atomic_long_read(&c->clean_zn_cnt));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300644 printk(KERN_DEBUG "\tgc_lnum %d, ihead_lnum %d\n",
645 c->gc_lnum, c->ihead_lnum);
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300646
Artem Bityutskiy84abf972009-01-23 14:54:59 +0200647 /* If we are in R/O mode, journal heads do not exist */
648 if (c->jheads)
649 for (i = 0; i < c->jhead_cnt; i++)
Artem Bityutskiy77a7ae52009-09-15 15:03:51 +0300650 printk(KERN_DEBUG "\tjhead %s\t LEB %d\n",
651 dbg_jhead(c->jheads[i].wbuf.jhead),
652 c->jheads[i].wbuf.lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300653 for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
654 bud = rb_entry(rb, struct ubifs_bud, rb);
655 printk(KERN_DEBUG "\tbud LEB %d\n", bud->lnum);
656 }
657 list_for_each_entry(bud, &c->old_buds, list)
658 printk(KERN_DEBUG "\told bud LEB %d\n", bud->lnum);
659 list_for_each_entry(idx_gc, &c->idx_gc, list)
660 printk(KERN_DEBUG "\tGC'ed idx LEB %d unmap %d\n",
661 idx_gc->lnum, idx_gc->unmap);
662 printk(KERN_DEBUG "\tcommit state %d\n", c->cmt_state);
Artem Bityutskiy21a60252008-12-12 11:13:17 -0500663
664 /* Print budgeting predictions */
Artem Bityutskiyb1375452011-03-29 18:04:05 +0300665 available = ubifs_calc_available(c, c->bi.min_idx_lebs);
666 outstanding = c->bi.data_growth + c->bi.dd_growth;
Artem Bityutskiy84abf972009-01-23 14:54:59 +0200667 free = ubifs_get_free_space_nolock(c);
Artem Bityutskiy21a60252008-12-12 11:13:17 -0500668 printk(KERN_DEBUG "Budgeting predictions:\n");
669 printk(KERN_DEBUG "\tavailable: %lld, outstanding %lld, free %lld\n",
670 available, outstanding, free);
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300671out_unlock:
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300672 spin_unlock(&dbg_lock);
Artem Bityutskiy8ff83082011-03-29 18:19:50 +0300673 spin_unlock(&c->space_lock);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300674}
675
676void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
677{
Artem Bityutskiybe9e62a2008-12-28 10:17:23 +0200678 int i, spc, dark = 0, dead = 0;
679 struct rb_node *rb;
680 struct ubifs_bud *bud;
681
682 spc = lp->free + lp->dirty;
683 if (spc < c->dead_wm)
684 dead = spc;
685 else
686 dark = ubifs_calc_dark(c, spc);
687
688 if (lp->flags & LPROPS_INDEX)
689 printk(KERN_DEBUG "LEB %-7d free %-8d dirty %-8d used %-8d "
690 "free + dirty %-8d flags %#x (", lp->lnum, lp->free,
691 lp->dirty, c->leb_size - spc, spc, lp->flags);
692 else
693 printk(KERN_DEBUG "LEB %-7d free %-8d dirty %-8d used %-8d "
694 "free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d "
695 "flags %#-4x (", lp->lnum, lp->free, lp->dirty,
696 c->leb_size - spc, spc, dark, dead,
697 (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags);
698
699 if (lp->flags & LPROPS_TAKEN) {
700 if (lp->flags & LPROPS_INDEX)
701 printk(KERN_CONT "index, taken");
702 else
703 printk(KERN_CONT "taken");
704 } else {
705 const char *s;
706
707 if (lp->flags & LPROPS_INDEX) {
708 switch (lp->flags & LPROPS_CAT_MASK) {
709 case LPROPS_DIRTY_IDX:
710 s = "dirty index";
711 break;
712 case LPROPS_FRDI_IDX:
713 s = "freeable index";
714 break;
715 default:
716 s = "index";
717 }
718 } else {
719 switch (lp->flags & LPROPS_CAT_MASK) {
720 case LPROPS_UNCAT:
721 s = "not categorized";
722 break;
723 case LPROPS_DIRTY:
724 s = "dirty";
725 break;
726 case LPROPS_FREE:
727 s = "free";
728 break;
729 case LPROPS_EMPTY:
730 s = "empty";
731 break;
732 case LPROPS_FREEABLE:
733 s = "freeable";
734 break;
735 default:
736 s = NULL;
737 break;
738 }
739 }
740 printk(KERN_CONT "%s", s);
741 }
742
743 for (rb = rb_first((struct rb_root *)&c->buds); rb; rb = rb_next(rb)) {
744 bud = rb_entry(rb, struct ubifs_bud, rb);
745 if (bud->lnum == lp->lnum) {
746 int head = 0;
747 for (i = 0; i < c->jhead_cnt; i++) {
Artem Bityutskiy13216572011-04-24 10:53:17 +0300748 /*
749 * Note, if we are in R/O mode or in the middle
750 * of mounting/re-mounting, the write-buffers do
751 * not exist.
752 */
753 if (c->jheads &&
754 lp->lnum == c->jheads[i].wbuf.lnum) {
Artem Bityutskiybe9e62a2008-12-28 10:17:23 +0200755 printk(KERN_CONT ", jhead %s",
756 dbg_jhead(i));
757 head = 1;
758 }
759 }
760 if (!head)
761 printk(KERN_CONT ", bud of jhead %s",
762 dbg_jhead(bud->jhead));
763 }
764 }
765 if (lp->lnum == c->gc_lnum)
766 printk(KERN_CONT ", GC LEB");
767 printk(KERN_CONT ")\n");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300768}
769
770void dbg_dump_lprops(struct ubifs_info *c)
771{
772 int lnum, err;
773 struct ubifs_lprops lp;
774 struct ubifs_lp_stats lst;
775
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200776 printk(KERN_DEBUG "(pid %d) start dumping LEB properties\n",
777 current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300778 ubifs_get_lp_stats(c, &lst);
779 dbg_dump_lstats(&lst);
780
781 for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
782 err = ubifs_read_one_lp(c, lnum, &lp);
783 if (err)
784 ubifs_err("cannot read lprops for LEB %d", lnum);
785
786 dbg_dump_lprop(c, &lp);
787 }
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200788 printk(KERN_DEBUG "(pid %d) finish dumping LEB properties\n",
789 current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300790}
791
Adrian Hunter73944a62008-09-12 18:13:31 +0300792void dbg_dump_lpt_info(struct ubifs_info *c)
793{
794 int i;
795
796 spin_lock(&dbg_lock);
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200797 printk(KERN_DEBUG "(pid %d) dumping LPT information\n", current->pid);
Adrian Hunter73944a62008-09-12 18:13:31 +0300798 printk(KERN_DEBUG "\tlpt_sz: %lld\n", c->lpt_sz);
799 printk(KERN_DEBUG "\tpnode_sz: %d\n", c->pnode_sz);
800 printk(KERN_DEBUG "\tnnode_sz: %d\n", c->nnode_sz);
801 printk(KERN_DEBUG "\tltab_sz: %d\n", c->ltab_sz);
802 printk(KERN_DEBUG "\tlsave_sz: %d\n", c->lsave_sz);
803 printk(KERN_DEBUG "\tbig_lpt: %d\n", c->big_lpt);
804 printk(KERN_DEBUG "\tlpt_hght: %d\n", c->lpt_hght);
805 printk(KERN_DEBUG "\tpnode_cnt: %d\n", c->pnode_cnt);
806 printk(KERN_DEBUG "\tnnode_cnt: %d\n", c->nnode_cnt);
807 printk(KERN_DEBUG "\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt);
808 printk(KERN_DEBUG "\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt);
809 printk(KERN_DEBUG "\tlsave_cnt: %d\n", c->lsave_cnt);
810 printk(KERN_DEBUG "\tspace_bits: %d\n", c->space_bits);
811 printk(KERN_DEBUG "\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
812 printk(KERN_DEBUG "\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
813 printk(KERN_DEBUG "\tlpt_spc_bits: %d\n", c->lpt_spc_bits);
814 printk(KERN_DEBUG "\tpcnt_bits: %d\n", c->pcnt_bits);
815 printk(KERN_DEBUG "\tlnum_bits: %d\n", c->lnum_bits);
816 printk(KERN_DEBUG "\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
817 printk(KERN_DEBUG "\tLPT head is at %d:%d\n",
818 c->nhead_lnum, c->nhead_offs);
Artem Bityutskiyf92b9822008-12-28 11:34:26 +0200819 printk(KERN_DEBUG "\tLPT ltab is at %d:%d\n",
820 c->ltab_lnum, c->ltab_offs);
Adrian Hunter73944a62008-09-12 18:13:31 +0300821 if (c->big_lpt)
822 printk(KERN_DEBUG "\tLPT lsave is at %d:%d\n",
823 c->lsave_lnum, c->lsave_offs);
824 for (i = 0; i < c->lpt_lebs; i++)
825 printk(KERN_DEBUG "\tLPT LEB %d free %d dirty %d tgc %d "
826 "cmt %d\n", i + c->lpt_first, c->ltab[i].free,
827 c->ltab[i].dirty, c->ltab[i].tgc, c->ltab[i].cmt);
828 spin_unlock(&dbg_lock);
829}
830
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300831void dbg_dump_leb(const struct ubifs_info *c, int lnum)
832{
833 struct ubifs_scan_leb *sleb;
834 struct ubifs_scan_node *snod;
Artem Bityutskiy73d9aec2011-03-11 15:39:09 +0200835 void *buf;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300836
837 if (dbg_failure_mode)
838 return;
839
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200840 printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n",
841 current->pid, lnum);
Artem Bityutskiy73d9aec2011-03-11 15:39:09 +0200842
Artem Bityutskiyfc5e58c2011-03-24 16:14:26 +0200843 buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
Artem Bityutskiy73d9aec2011-03-11 15:39:09 +0200844 if (!buf) {
845 ubifs_err("cannot allocate memory for dumping LEB %d", lnum);
846 return;
847 }
848
849 sleb = ubifs_scan(c, lnum, 0, buf, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300850 if (IS_ERR(sleb)) {
851 ubifs_err("scan error %d", (int)PTR_ERR(sleb));
Artem Bityutskiy73d9aec2011-03-11 15:39:09 +0200852 goto out;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300853 }
854
855 printk(KERN_DEBUG "LEB %d has %d nodes ending at %d\n", lnum,
856 sleb->nodes_cnt, sleb->endpt);
857
858 list_for_each_entry(snod, &sleb->nodes, list) {
859 cond_resched();
860 printk(KERN_DEBUG "Dumping node at LEB %d:%d len %d\n", lnum,
861 snod->offs, snod->len);
862 dbg_dump_node(c, snod->node);
863 }
864
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200865 printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n",
866 current->pid, lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300867 ubifs_scan_destroy(sleb);
Artem Bityutskiy73d9aec2011-03-11 15:39:09 +0200868
869out:
870 vfree(buf);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300871 return;
872}
873
874void dbg_dump_znode(const struct ubifs_info *c,
875 const struct ubifs_znode *znode)
876{
877 int n;
878 const struct ubifs_zbranch *zbr;
879
880 spin_lock(&dbg_lock);
881 if (znode->parent)
882 zbr = &znode->parent->zbranch[znode->iip];
883 else
884 zbr = &c->zroot;
885
886 printk(KERN_DEBUG "znode %p, LEB %d:%d len %d parent %p iip %d level %d"
887 " child_cnt %d flags %lx\n", znode, zbr->lnum, zbr->offs,
888 zbr->len, znode->parent, znode->iip, znode->level,
889 znode->child_cnt, znode->flags);
890
891 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
892 spin_unlock(&dbg_lock);
893 return;
894 }
895
896 printk(KERN_DEBUG "zbranches:\n");
897 for (n = 0; n < znode->child_cnt; n++) {
898 zbr = &znode->zbranch[n];
899 if (znode->level > 0)
900 printk(KERN_DEBUG "\t%d: znode %p LEB %d:%d len %d key "
901 "%s\n", n, zbr->znode, zbr->lnum,
902 zbr->offs, zbr->len,
903 DBGKEY(&zbr->key));
904 else
905 printk(KERN_DEBUG "\t%d: LNC %p LEB %d:%d len %d key "
906 "%s\n", n, zbr->znode, zbr->lnum,
907 zbr->offs, zbr->len,
908 DBGKEY(&zbr->key));
909 }
910 spin_unlock(&dbg_lock);
911}
912
913void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
914{
915 int i;
916
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200917 printk(KERN_DEBUG "(pid %d) start dumping heap cat %d (%d elements)\n",
Artem Bityutskiy1de94152008-07-25 12:58:38 +0300918 current->pid, cat, heap->cnt);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300919 for (i = 0; i < heap->cnt; i++) {
920 struct ubifs_lprops *lprops = heap->arr[i];
921
922 printk(KERN_DEBUG "\t%d. LEB %d hpos %d free %d dirty %d "
923 "flags %d\n", i, lprops->lnum, lprops->hpos,
924 lprops->free, lprops->dirty, lprops->flags);
925 }
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200926 printk(KERN_DEBUG "(pid %d) finish dumping heap\n", current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300927}
928
929void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
930 struct ubifs_nnode *parent, int iip)
931{
932 int i;
933
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200934 printk(KERN_DEBUG "(pid %d) dumping pnode:\n", current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300935 printk(KERN_DEBUG "\taddress %zx parent %zx cnext %zx\n",
936 (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
937 printk(KERN_DEBUG "\tflags %lu iip %d level %d num %d\n",
938 pnode->flags, iip, pnode->level, pnode->num);
939 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
940 struct ubifs_lprops *lp = &pnode->lprops[i];
941
942 printk(KERN_DEBUG "\t%d: free %d dirty %d flags %d lnum %d\n",
943 i, lp->free, lp->dirty, lp->flags, lp->lnum);
944 }
945}
946
947void dbg_dump_tnc(struct ubifs_info *c)
948{
949 struct ubifs_znode *znode;
950 int level;
951
952 printk(KERN_DEBUG "\n");
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200953 printk(KERN_DEBUG "(pid %d) start dumping TNC tree\n", current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300954 znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
955 level = znode->level;
956 printk(KERN_DEBUG "== Level %d ==\n", level);
957 while (znode) {
958 if (level != znode->level) {
959 level = znode->level;
960 printk(KERN_DEBUG "== Level %d ==\n", level);
961 }
962 dbg_dump_znode(c, znode);
963 znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
964 }
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200965 printk(KERN_DEBUG "(pid %d) finish dumping TNC tree\n", current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300966}
967
968static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
969 void *priv)
970{
971 dbg_dump_znode(c, znode);
972 return 0;
973}
974
975/**
976 * dbg_dump_index - dump the on-flash index.
977 * @c: UBIFS file-system description object
978 *
979 * This function dumps whole UBIFS indexing B-tree, unlike 'dbg_dump_tnc()'
980 * which dumps only in-memory znodes and does not read znodes which from flash.
981 */
982void dbg_dump_index(struct ubifs_info *c)
983{
984 dbg_walk_index(c, NULL, dump_znode, NULL);
985}
986
987/**
Artem Bityutskiy84abf972009-01-23 14:54:59 +0200988 * dbg_save_space_info - save information about flash space.
989 * @c: UBIFS file-system description object
990 *
991 * This function saves information about UBIFS free space, dirty space, etc, in
992 * order to check it later.
993 */
994void dbg_save_space_info(struct ubifs_info *c)
995{
996 struct ubifs_debug_info *d = c->dbg;
Artem Bityutskiy7da64432011-04-04 17:16:39 +0300997 int freeable_cnt;
Artem Bityutskiy84abf972009-01-23 14:54:59 +0200998
999 spin_lock(&c->space_lock);
Artem Bityutskiy7da64432011-04-04 17:16:39 +03001000 memcpy(&d->saved_lst, &c->lst, sizeof(struct ubifs_lp_stats));
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +03001001 memcpy(&d->saved_bi, &c->bi, sizeof(struct ubifs_budg_info));
1002 d->saved_idx_gc_cnt = c->idx_gc_cnt;
Artem Bityutskiy7da64432011-04-04 17:16:39 +03001003
1004 /*
1005 * We use a dirty hack here and zero out @c->freeable_cnt, because it
1006 * affects the free space calculations, and UBIFS might not know about
1007 * all freeable eraseblocks. Indeed, we know about freeable eraseblocks
1008 * only when we read their lprops, and we do this only lazily, upon the
1009 * need. So at any given point of time @c->freeable_cnt might be not
1010 * exactly accurate.
1011 *
1012 * Just one example about the issue we hit when we did not zero
1013 * @c->freeable_cnt.
1014 * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the
1015 * amount of free space in @d->saved_free
1016 * 2. We re-mount R/W, which makes UBIFS to read the "lsave"
1017 * information from flash, where we cache LEBs from various
1018 * categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()'
1019 * -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()'
1020 * -> 'ubifs_get_pnode()' -> 'update_cats()'
1021 * -> 'ubifs_add_to_cat()').
1022 * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt
1023 * becomes %1.
1024 * 4. We calculate the amount of free space when the re-mount is
1025 * finished in 'dbg_check_space_info()' and it does not match
1026 * @d->saved_free.
1027 */
1028 freeable_cnt = c->freeable_cnt;
1029 c->freeable_cnt = 0;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001030 d->saved_free = ubifs_get_free_space_nolock(c);
Artem Bityutskiy7da64432011-04-04 17:16:39 +03001031 c->freeable_cnt = freeable_cnt;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001032 spin_unlock(&c->space_lock);
1033}
1034
1035/**
1036 * dbg_check_space_info - check flash space information.
1037 * @c: UBIFS file-system description object
1038 *
1039 * This function compares current flash space information with the information
1040 * which was saved when the 'dbg_save_space_info()' function was called.
1041 * Returns zero if the information has not changed, and %-EINVAL it it has
1042 * changed.
1043 */
1044int dbg_check_space_info(struct ubifs_info *c)
1045{
1046 struct ubifs_debug_info *d = c->dbg;
1047 struct ubifs_lp_stats lst;
Artem Bityutskiy7da64432011-04-04 17:16:39 +03001048 long long free;
1049 int freeable_cnt;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001050
1051 spin_lock(&c->space_lock);
Artem Bityutskiy7da64432011-04-04 17:16:39 +03001052 freeable_cnt = c->freeable_cnt;
1053 c->freeable_cnt = 0;
1054 free = ubifs_get_free_space_nolock(c);
1055 c->freeable_cnt = freeable_cnt;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001056 spin_unlock(&c->space_lock);
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001057
1058 if (free != d->saved_free) {
1059 ubifs_err("free space changed from %lld to %lld",
1060 d->saved_free, free);
1061 goto out;
1062 }
1063
1064 return 0;
1065
1066out:
1067 ubifs_msg("saved lprops statistics dump");
1068 dbg_dump_lstats(&d->saved_lst);
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +03001069 ubifs_msg("saved budgeting info dump");
1070 dbg_dump_budg(c, &d->saved_bi);
1071 ubifs_msg("saved idx_gc_cnt %d", d->saved_idx_gc_cnt);
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001072 ubifs_msg("current lprops statistics dump");
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +03001073 ubifs_get_lp_stats(c, &lst);
Artem Bityutskiye055f7e2009-09-17 15:08:31 +03001074 dbg_dump_lstats(&lst);
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +03001075 ubifs_msg("current budgeting info dump");
1076 dbg_dump_budg(c, &c->bi);
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001077 dump_stack();
1078 return -EINVAL;
1079}
1080
1081/**
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001082 * dbg_check_synced_i_size - check synchronized inode size.
1083 * @inode: inode to check
1084 *
1085 * If inode is clean, synchronized inode size has to be equivalent to current
1086 * inode size. This function has to be called only for locked inodes (@i_mutex
1087 * has to be locked). Returns %0 if synchronized inode size if correct, and
1088 * %-EINVAL if not.
1089 */
1090int dbg_check_synced_i_size(struct inode *inode)
1091{
1092 int err = 0;
1093 struct ubifs_inode *ui = ubifs_inode(inode);
1094
1095 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
1096 return 0;
1097 if (!S_ISREG(inode->i_mode))
1098 return 0;
1099
1100 mutex_lock(&ui->ui_mutex);
1101 spin_lock(&ui->ui_lock);
1102 if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
1103 ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode "
1104 "is clean", ui->ui_size, ui->synced_i_size);
1105 ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
1106 inode->i_mode, i_size_read(inode));
1107 dbg_dump_stack();
1108 err = -EINVAL;
1109 }
1110 spin_unlock(&ui->ui_lock);
1111 mutex_unlock(&ui->ui_mutex);
1112 return err;
1113}
1114
1115/*
1116 * dbg_check_dir - check directory inode size and link count.
1117 * @c: UBIFS file-system description object
1118 * @dir: the directory to calculate size for
1119 * @size: the result is returned here
1120 *
1121 * This function makes sure that directory size and link count are correct.
1122 * Returns zero in case of success and a negative error code in case of
1123 * failure.
1124 *
1125 * Note, it is good idea to make sure the @dir->i_mutex is locked before
1126 * calling this function.
1127 */
1128int dbg_check_dir_size(struct ubifs_info *c, const struct inode *dir)
1129{
1130 unsigned int nlink = 2;
1131 union ubifs_key key;
1132 struct ubifs_dent_node *dent, *pdent = NULL;
1133 struct qstr nm = { .name = NULL };
1134 loff_t size = UBIFS_INO_NODE_SZ;
1135
1136 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
1137 return 0;
1138
1139 if (!S_ISDIR(dir->i_mode))
1140 return 0;
1141
1142 lowest_dent_key(c, &key, dir->i_ino);
1143 while (1) {
1144 int err;
1145
1146 dent = ubifs_tnc_next_ent(c, &key, &nm);
1147 if (IS_ERR(dent)) {
1148 err = PTR_ERR(dent);
1149 if (err == -ENOENT)
1150 break;
1151 return err;
1152 }
1153
1154 nm.name = dent->name;
1155 nm.len = le16_to_cpu(dent->nlen);
1156 size += CALC_DENT_SIZE(nm.len);
1157 if (dent->type == UBIFS_ITYPE_DIR)
1158 nlink += 1;
1159 kfree(pdent);
1160 pdent = dent;
1161 key_read(c, &dent->key, &key);
1162 }
1163 kfree(pdent);
1164
1165 if (i_size_read(dir) != size) {
1166 ubifs_err("directory inode %lu has size %llu, "
1167 "but calculated size is %llu", dir->i_ino,
1168 (unsigned long long)i_size_read(dir),
1169 (unsigned long long)size);
1170 dump_stack();
1171 return -EINVAL;
1172 }
1173 if (dir->i_nlink != nlink) {
1174 ubifs_err("directory inode %lu has nlink %u, but calculated "
1175 "nlink is %u", dir->i_ino, dir->i_nlink, nlink);
1176 dump_stack();
1177 return -EINVAL;
1178 }
1179
1180 return 0;
1181}
1182
1183/**
1184 * dbg_check_key_order - make sure that colliding keys are properly ordered.
1185 * @c: UBIFS file-system description object
1186 * @zbr1: first zbranch
1187 * @zbr2: following zbranch
1188 *
1189 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
1190 * names of the direntries/xentries which are referred by the keys. This
1191 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
1192 * sure the name of direntry/xentry referred by @zbr1 is less than
1193 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
1194 * and a negative error code in case of failure.
1195 */
1196static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
1197 struct ubifs_zbranch *zbr2)
1198{
1199 int err, nlen1, nlen2, cmp;
1200 struct ubifs_dent_node *dent1, *dent2;
1201 union ubifs_key key;
1202
1203 ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
1204 dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1205 if (!dent1)
1206 return -ENOMEM;
1207 dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1208 if (!dent2) {
1209 err = -ENOMEM;
1210 goto out_free;
1211 }
1212
1213 err = ubifs_tnc_read_node(c, zbr1, dent1);
1214 if (err)
1215 goto out_free;
1216 err = ubifs_validate_entry(c, dent1);
1217 if (err)
1218 goto out_free;
1219
1220 err = ubifs_tnc_read_node(c, zbr2, dent2);
1221 if (err)
1222 goto out_free;
1223 err = ubifs_validate_entry(c, dent2);
1224 if (err)
1225 goto out_free;
1226
1227 /* Make sure node keys are the same as in zbranch */
1228 err = 1;
1229 key_read(c, &dent1->key, &key);
1230 if (keys_cmp(c, &zbr1->key, &key)) {
Artem Bityutskiy5d38b3a2008-12-30 17:58:42 +02001231 dbg_err("1st entry at %d:%d has key %s", zbr1->lnum,
1232 zbr1->offs, DBGKEY(&key));
1233 dbg_err("but it should have key %s according to tnc",
1234 DBGKEY(&zbr1->key));
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +02001235 dbg_dump_node(c, dent1);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03001236 goto out_free;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001237 }
1238
1239 key_read(c, &dent2->key, &key);
1240 if (keys_cmp(c, &zbr2->key, &key)) {
Artem Bityutskiy5d38b3a2008-12-30 17:58:42 +02001241 dbg_err("2nd entry at %d:%d has key %s", zbr1->lnum,
1242 zbr1->offs, DBGKEY(&key));
1243 dbg_err("but it should have key %s according to tnc",
1244 DBGKEY(&zbr2->key));
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +02001245 dbg_dump_node(c, dent2);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03001246 goto out_free;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001247 }
1248
1249 nlen1 = le16_to_cpu(dent1->nlen);
1250 nlen2 = le16_to_cpu(dent2->nlen);
1251
1252 cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
1253 if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
1254 err = 0;
1255 goto out_free;
1256 }
1257 if (cmp == 0 && nlen1 == nlen2)
Artem Bityutskiy5d38b3a2008-12-30 17:58:42 +02001258 dbg_err("2 xent/dent nodes with the same name");
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001259 else
Artem Bityutskiy5d38b3a2008-12-30 17:58:42 +02001260 dbg_err("bad order of colliding key %s",
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001261 DBGKEY(&key));
1262
Artem Bityutskiy552ff312008-10-23 11:49:28 +03001263 ubifs_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001264 dbg_dump_node(c, dent1);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03001265 ubifs_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001266 dbg_dump_node(c, dent2);
1267
1268out_free:
1269 kfree(dent2);
1270 kfree(dent1);
1271 return err;
1272}
1273
1274/**
1275 * dbg_check_znode - check if znode is all right.
1276 * @c: UBIFS file-system description object
1277 * @zbr: zbranch which points to this znode
1278 *
1279 * This function makes sure that znode referred to by @zbr is all right.
1280 * Returns zero if it is, and %-EINVAL if it is not.
1281 */
1282static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
1283{
1284 struct ubifs_znode *znode = zbr->znode;
1285 struct ubifs_znode *zp = znode->parent;
1286 int n, err, cmp;
1287
1288 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
1289 err = 1;
1290 goto out;
1291 }
1292 if (znode->level < 0) {
1293 err = 2;
1294 goto out;
1295 }
1296 if (znode->iip < 0 || znode->iip >= c->fanout) {
1297 err = 3;
1298 goto out;
1299 }
1300
1301 if (zbr->len == 0)
1302 /* Only dirty zbranch may have no on-flash nodes */
1303 if (!ubifs_zn_dirty(znode)) {
1304 err = 4;
1305 goto out;
1306 }
1307
1308 if (ubifs_zn_dirty(znode)) {
1309 /*
1310 * If znode is dirty, its parent has to be dirty as well. The
1311 * order of the operation is important, so we have to have
1312 * memory barriers.
1313 */
1314 smp_mb();
1315 if (zp && !ubifs_zn_dirty(zp)) {
1316 /*
1317 * The dirty flag is atomic and is cleared outside the
1318 * TNC mutex, so znode's dirty flag may now have
1319 * been cleared. The child is always cleared before the
1320 * parent, so we just need to check again.
1321 */
1322 smp_mb();
1323 if (ubifs_zn_dirty(znode)) {
1324 err = 5;
1325 goto out;
1326 }
1327 }
1328 }
1329
1330 if (zp) {
1331 const union ubifs_key *min, *max;
1332
1333 if (znode->level != zp->level - 1) {
1334 err = 6;
1335 goto out;
1336 }
1337
1338 /* Make sure the 'parent' pointer in our znode is correct */
1339 err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
1340 if (!err) {
1341 /* This zbranch does not exist in the parent */
1342 err = 7;
1343 goto out;
1344 }
1345
1346 if (znode->iip >= zp->child_cnt) {
1347 err = 8;
1348 goto out;
1349 }
1350
1351 if (znode->iip != n) {
1352 /* This may happen only in case of collisions */
1353 if (keys_cmp(c, &zp->zbranch[n].key,
1354 &zp->zbranch[znode->iip].key)) {
1355 err = 9;
1356 goto out;
1357 }
1358 n = znode->iip;
1359 }
1360
1361 /*
1362 * Make sure that the first key in our znode is greater than or
1363 * equal to the key in the pointing zbranch.
1364 */
1365 min = &zbr->key;
1366 cmp = keys_cmp(c, min, &znode->zbranch[0].key);
1367 if (cmp == 1) {
1368 err = 10;
1369 goto out;
1370 }
1371
1372 if (n + 1 < zp->child_cnt) {
1373 max = &zp->zbranch[n + 1].key;
1374
1375 /*
1376 * Make sure the last key in our znode is less or
Artem Bityutskiy7d4e9cc2009-03-20 19:11:12 +02001377 * equivalent than the key in the zbranch which goes
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001378 * after our pointing zbranch.
1379 */
1380 cmp = keys_cmp(c, max,
1381 &znode->zbranch[znode->child_cnt - 1].key);
1382 if (cmp == -1) {
1383 err = 11;
1384 goto out;
1385 }
1386 }
1387 } else {
1388 /* This may only be root znode */
1389 if (zbr != &c->zroot) {
1390 err = 12;
1391 goto out;
1392 }
1393 }
1394
1395 /*
1396 * Make sure that next key is greater or equivalent then the previous
1397 * one.
1398 */
1399 for (n = 1; n < znode->child_cnt; n++) {
1400 cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
1401 &znode->zbranch[n].key);
1402 if (cmp > 0) {
1403 err = 13;
1404 goto out;
1405 }
1406 if (cmp == 0) {
1407 /* This can only be keys with colliding hash */
1408 if (!is_hash_key(c, &znode->zbranch[n].key)) {
1409 err = 14;
1410 goto out;
1411 }
1412
1413 if (znode->level != 0 || c->replaying)
1414 continue;
1415
1416 /*
1417 * Colliding keys should follow binary order of
1418 * corresponding xentry/dentry names.
1419 */
1420 err = dbg_check_key_order(c, &znode->zbranch[n - 1],
1421 &znode->zbranch[n]);
1422 if (err < 0)
1423 return err;
1424 if (err) {
1425 err = 15;
1426 goto out;
1427 }
1428 }
1429 }
1430
1431 for (n = 0; n < znode->child_cnt; n++) {
1432 if (!znode->zbranch[n].znode &&
1433 (znode->zbranch[n].lnum == 0 ||
1434 znode->zbranch[n].len == 0)) {
1435 err = 16;
1436 goto out;
1437 }
1438
1439 if (znode->zbranch[n].lnum != 0 &&
1440 znode->zbranch[n].len == 0) {
1441 err = 17;
1442 goto out;
1443 }
1444
1445 if (znode->zbranch[n].lnum == 0 &&
1446 znode->zbranch[n].len != 0) {
1447 err = 18;
1448 goto out;
1449 }
1450
1451 if (znode->zbranch[n].lnum == 0 &&
1452 znode->zbranch[n].offs != 0) {
1453 err = 19;
1454 goto out;
1455 }
1456
1457 if (znode->level != 0 && znode->zbranch[n].znode)
1458 if (znode->zbranch[n].znode->parent != znode) {
1459 err = 20;
1460 goto out;
1461 }
1462 }
1463
1464 return 0;
1465
1466out:
1467 ubifs_err("failed, error %d", err);
1468 ubifs_msg("dump of the znode");
1469 dbg_dump_znode(c, znode);
1470 if (zp) {
1471 ubifs_msg("dump of the parent znode");
1472 dbg_dump_znode(c, zp);
1473 }
1474 dump_stack();
1475 return -EINVAL;
1476}
1477
1478/**
1479 * dbg_check_tnc - check TNC tree.
1480 * @c: UBIFS file-system description object
1481 * @extra: do extra checks that are possible at start commit
1482 *
1483 * This function traverses whole TNC tree and checks every znode. Returns zero
1484 * if everything is all right and %-EINVAL if something is wrong with TNC.
1485 */
1486int dbg_check_tnc(struct ubifs_info *c, int extra)
1487{
1488 struct ubifs_znode *znode;
1489 long clean_cnt = 0, dirty_cnt = 0;
1490 int err, last;
1491
1492 if (!(ubifs_chk_flags & UBIFS_CHK_TNC))
1493 return 0;
1494
1495 ubifs_assert(mutex_is_locked(&c->tnc_mutex));
1496 if (!c->zroot.znode)
1497 return 0;
1498
1499 znode = ubifs_tnc_postorder_first(c->zroot.znode);
1500 while (1) {
1501 struct ubifs_znode *prev;
1502 struct ubifs_zbranch *zbr;
1503
1504 if (!znode->parent)
1505 zbr = &c->zroot;
1506 else
1507 zbr = &znode->parent->zbranch[znode->iip];
1508
1509 err = dbg_check_znode(c, zbr);
1510 if (err)
1511 return err;
1512
1513 if (extra) {
1514 if (ubifs_zn_dirty(znode))
1515 dirty_cnt += 1;
1516 else
1517 clean_cnt += 1;
1518 }
1519
1520 prev = znode;
1521 znode = ubifs_tnc_postorder_next(znode);
1522 if (!znode)
1523 break;
1524
1525 /*
1526 * If the last key of this znode is equivalent to the first key
1527 * of the next znode (collision), then check order of the keys.
1528 */
1529 last = prev->child_cnt - 1;
1530 if (prev->level == 0 && znode->level == 0 && !c->replaying &&
1531 !keys_cmp(c, &prev->zbranch[last].key,
1532 &znode->zbranch[0].key)) {
1533 err = dbg_check_key_order(c, &prev->zbranch[last],
1534 &znode->zbranch[0]);
1535 if (err < 0)
1536 return err;
1537 if (err) {
1538 ubifs_msg("first znode");
1539 dbg_dump_znode(c, prev);
1540 ubifs_msg("second znode");
1541 dbg_dump_znode(c, znode);
1542 return -EINVAL;
1543 }
1544 }
1545 }
1546
1547 if (extra) {
1548 if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
1549 ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld",
1550 atomic_long_read(&c->clean_zn_cnt),
1551 clean_cnt);
1552 return -EINVAL;
1553 }
1554 if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
1555 ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld",
1556 atomic_long_read(&c->dirty_zn_cnt),
1557 dirty_cnt);
1558 return -EINVAL;
1559 }
1560 }
1561
1562 return 0;
1563}
1564
1565/**
1566 * dbg_walk_index - walk the on-flash index.
1567 * @c: UBIFS file-system description object
1568 * @leaf_cb: called for each leaf node
1569 * @znode_cb: called for each indexing node
Adrian Hunter227c75c2009-01-29 11:53:51 +02001570 * @priv: private data which is passed to callbacks
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001571 *
1572 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1573 * node and @znode_cb for each indexing node. Returns zero in case of success
1574 * and a negative error code in case of failure.
1575 *
1576 * It would be better if this function removed every znode it pulled to into
1577 * the TNC, so that the behavior more closely matched the non-debugging
1578 * behavior.
1579 */
1580int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1581 dbg_znode_callback znode_cb, void *priv)
1582{
1583 int err;
1584 struct ubifs_zbranch *zbr;
1585 struct ubifs_znode *znode, *child;
1586
1587 mutex_lock(&c->tnc_mutex);
1588 /* If the root indexing node is not in TNC - pull it */
1589 if (!c->zroot.znode) {
1590 c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1591 if (IS_ERR(c->zroot.znode)) {
1592 err = PTR_ERR(c->zroot.znode);
1593 c->zroot.znode = NULL;
1594 goto out_unlock;
1595 }
1596 }
1597
1598 /*
1599 * We are going to traverse the indexing tree in the postorder manner.
1600 * Go down and find the leftmost indexing node where we are going to
1601 * start from.
1602 */
1603 znode = c->zroot.znode;
1604 while (znode->level > 0) {
1605 zbr = &znode->zbranch[0];
1606 child = zbr->znode;
1607 if (!child) {
1608 child = ubifs_load_znode(c, zbr, znode, 0);
1609 if (IS_ERR(child)) {
1610 err = PTR_ERR(child);
1611 goto out_unlock;
1612 }
1613 zbr->znode = child;
1614 }
1615
1616 znode = child;
1617 }
1618
1619 /* Iterate over all indexing nodes */
1620 while (1) {
1621 int idx;
1622
1623 cond_resched();
1624
1625 if (znode_cb) {
1626 err = znode_cb(c, znode, priv);
1627 if (err) {
1628 ubifs_err("znode checking function returned "
1629 "error %d", err);
1630 dbg_dump_znode(c, znode);
1631 goto out_dump;
1632 }
1633 }
1634 if (leaf_cb && znode->level == 0) {
1635 for (idx = 0; idx < znode->child_cnt; idx++) {
1636 zbr = &znode->zbranch[idx];
1637 err = leaf_cb(c, zbr, priv);
1638 if (err) {
1639 ubifs_err("leaf checking function "
1640 "returned error %d, for leaf "
1641 "at LEB %d:%d",
1642 err, zbr->lnum, zbr->offs);
1643 goto out_dump;
1644 }
1645 }
1646 }
1647
1648 if (!znode->parent)
1649 break;
1650
1651 idx = znode->iip + 1;
1652 znode = znode->parent;
1653 if (idx < znode->child_cnt) {
1654 /* Switch to the next index in the parent */
1655 zbr = &znode->zbranch[idx];
1656 child = zbr->znode;
1657 if (!child) {
1658 child = ubifs_load_znode(c, zbr, znode, idx);
1659 if (IS_ERR(child)) {
1660 err = PTR_ERR(child);
1661 goto out_unlock;
1662 }
1663 zbr->znode = child;
1664 }
1665 znode = child;
1666 } else
1667 /*
1668 * This is the last child, switch to the parent and
1669 * continue.
1670 */
1671 continue;
1672
1673 /* Go to the lowest leftmost znode in the new sub-tree */
1674 while (znode->level > 0) {
1675 zbr = &znode->zbranch[0];
1676 child = zbr->znode;
1677 if (!child) {
1678 child = ubifs_load_znode(c, zbr, znode, 0);
1679 if (IS_ERR(child)) {
1680 err = PTR_ERR(child);
1681 goto out_unlock;
1682 }
1683 zbr->znode = child;
1684 }
1685 znode = child;
1686 }
1687 }
1688
1689 mutex_unlock(&c->tnc_mutex);
1690 return 0;
1691
1692out_dump:
1693 if (znode->parent)
1694 zbr = &znode->parent->zbranch[znode->iip];
1695 else
1696 zbr = &c->zroot;
1697 ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1698 dbg_dump_znode(c, znode);
1699out_unlock:
1700 mutex_unlock(&c->tnc_mutex);
1701 return err;
1702}
1703
1704/**
1705 * add_size - add znode size to partially calculated index size.
1706 * @c: UBIFS file-system description object
1707 * @znode: znode to add size for
1708 * @priv: partially calculated index size
1709 *
1710 * This is a helper function for 'dbg_check_idx_size()' which is called for
1711 * every indexing node and adds its size to the 'long long' variable pointed to
1712 * by @priv.
1713 */
1714static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1715{
1716 long long *idx_size = priv;
1717 int add;
1718
1719 add = ubifs_idx_node_sz(c, znode->child_cnt);
1720 add = ALIGN(add, 8);
1721 *idx_size += add;
1722 return 0;
1723}
1724
1725/**
1726 * dbg_check_idx_size - check index size.
1727 * @c: UBIFS file-system description object
1728 * @idx_size: size to check
1729 *
1730 * This function walks the UBIFS index, calculates its size and checks that the
1731 * size is equivalent to @idx_size. Returns zero in case of success and a
1732 * negative error code in case of failure.
1733 */
1734int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
1735{
1736 int err;
1737 long long calc = 0;
1738
1739 if (!(ubifs_chk_flags & UBIFS_CHK_IDX_SZ))
1740 return 0;
1741
1742 err = dbg_walk_index(c, NULL, add_size, &calc);
1743 if (err) {
1744 ubifs_err("error %d while walking the index", err);
1745 return err;
1746 }
1747
1748 if (calc != idx_size) {
1749 ubifs_err("index size check failed: calculated size is %lld, "
1750 "should be %lld", calc, idx_size);
1751 dump_stack();
1752 return -EINVAL;
1753 }
1754
1755 return 0;
1756}
1757
1758/**
1759 * struct fsck_inode - information about an inode used when checking the file-system.
1760 * @rb: link in the RB-tree of inodes
1761 * @inum: inode number
1762 * @mode: inode type, permissions, etc
1763 * @nlink: inode link count
1764 * @xattr_cnt: count of extended attributes
1765 * @references: how many directory/xattr entries refer this inode (calculated
1766 * while walking the index)
1767 * @calc_cnt: for directory inode count of child directories
1768 * @size: inode size (read from on-flash inode)
1769 * @xattr_sz: summary size of all extended attributes (read from on-flash
1770 * inode)
1771 * @calc_sz: for directories calculated directory size
1772 * @calc_xcnt: count of extended attributes
1773 * @calc_xsz: calculated summary size of all extended attributes
1774 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1775 * inode (read from on-flash inode)
1776 * @calc_xnms: calculated sum of lengths of all extended attribute names
1777 */
1778struct fsck_inode {
1779 struct rb_node rb;
1780 ino_t inum;
1781 umode_t mode;
1782 unsigned int nlink;
1783 unsigned int xattr_cnt;
1784 int references;
1785 int calc_cnt;
1786 long long size;
1787 unsigned int xattr_sz;
1788 long long calc_sz;
1789 long long calc_xcnt;
1790 long long calc_xsz;
1791 unsigned int xattr_nms;
1792 long long calc_xnms;
1793};
1794
1795/**
1796 * struct fsck_data - private FS checking information.
1797 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1798 */
1799struct fsck_data {
1800 struct rb_root inodes;
1801};
1802
1803/**
1804 * add_inode - add inode information to RB-tree of inodes.
1805 * @c: UBIFS file-system description object
1806 * @fsckd: FS checking information
1807 * @ino: raw UBIFS inode to add
1808 *
1809 * This is a helper function for 'check_leaf()' which adds information about
1810 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1811 * case of success and a negative error code in case of failure.
1812 */
1813static struct fsck_inode *add_inode(struct ubifs_info *c,
1814 struct fsck_data *fsckd,
1815 struct ubifs_ino_node *ino)
1816{
1817 struct rb_node **p, *parent = NULL;
1818 struct fsck_inode *fscki;
1819 ino_t inum = key_inum_flash(c, &ino->key);
Artem Bityutskiy45cd5cd2011-05-02 22:34:39 +03001820 struct inode *inode;
1821 struct ubifs_inode *ui;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001822
1823 p = &fsckd->inodes.rb_node;
1824 while (*p) {
1825 parent = *p;
1826 fscki = rb_entry(parent, struct fsck_inode, rb);
1827 if (inum < fscki->inum)
1828 p = &(*p)->rb_left;
1829 else if (inum > fscki->inum)
1830 p = &(*p)->rb_right;
1831 else
1832 return fscki;
1833 }
1834
1835 if (inum > c->highest_inum) {
1836 ubifs_err("too high inode number, max. is %lu",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001837 (unsigned long)c->highest_inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001838 return ERR_PTR(-EINVAL);
1839 }
1840
1841 fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
1842 if (!fscki)
1843 return ERR_PTR(-ENOMEM);
1844
Artem Bityutskiy45cd5cd2011-05-02 22:34:39 +03001845 inode = ilookup(c->vfs_sb, inum);
1846
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001847 fscki->inum = inum;
Artem Bityutskiy45cd5cd2011-05-02 22:34:39 +03001848 /*
1849 * If the inode is present in the VFS inode cache, use it instead of
1850 * the on-flash inode which might be out-of-date. E.g., the size might
1851 * be out-of-date. If we do not do this, the following may happen, for
1852 * example:
1853 * 1. A power cut happens
1854 * 2. We mount the file-system R/O, the replay process fixes up the
1855 * inode size in the VFS cache, but on on-flash.
1856 * 3. 'check_leaf()' fails because it hits a data node beyond inode
1857 * size.
1858 */
1859 if (!inode) {
1860 fscki->nlink = le32_to_cpu(ino->nlink);
1861 fscki->size = le64_to_cpu(ino->size);
1862 fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
1863 fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
1864 fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
1865 fscki->mode = le32_to_cpu(ino->mode);
1866 } else {
1867 ui = ubifs_inode(inode);
1868 fscki->nlink = inode->i_nlink;
1869 fscki->size = inode->i_size;
1870 fscki->xattr_cnt = ui->xattr_cnt;
1871 fscki->xattr_sz = ui->xattr_size;
1872 fscki->xattr_nms = ui->xattr_names;
1873 fscki->mode = inode->i_mode;
1874 iput(inode);
1875 }
1876
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001877 if (S_ISDIR(fscki->mode)) {
1878 fscki->calc_sz = UBIFS_INO_NODE_SZ;
1879 fscki->calc_cnt = 2;
1880 }
Artem Bityutskiy45cd5cd2011-05-02 22:34:39 +03001881
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001882 rb_link_node(&fscki->rb, parent, p);
1883 rb_insert_color(&fscki->rb, &fsckd->inodes);
Artem Bityutskiy45cd5cd2011-05-02 22:34:39 +03001884
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001885 return fscki;
1886}
1887
1888/**
1889 * search_inode - search inode in the RB-tree of inodes.
1890 * @fsckd: FS checking information
1891 * @inum: inode number to search
1892 *
1893 * This is a helper function for 'check_leaf()' which searches inode @inum in
1894 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1895 * the inode was not found.
1896 */
1897static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
1898{
1899 struct rb_node *p;
1900 struct fsck_inode *fscki;
1901
1902 p = fsckd->inodes.rb_node;
1903 while (p) {
1904 fscki = rb_entry(p, struct fsck_inode, rb);
1905 if (inum < fscki->inum)
1906 p = p->rb_left;
1907 else if (inum > fscki->inum)
1908 p = p->rb_right;
1909 else
1910 return fscki;
1911 }
1912 return NULL;
1913}
1914
1915/**
1916 * read_add_inode - read inode node and add it to RB-tree of inodes.
1917 * @c: UBIFS file-system description object
1918 * @fsckd: FS checking information
1919 * @inum: inode number to read
1920 *
1921 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1922 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1923 * information pointer in case of success and a negative error code in case of
1924 * failure.
1925 */
1926static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1927 struct fsck_data *fsckd, ino_t inum)
1928{
1929 int n, err;
1930 union ubifs_key key;
1931 struct ubifs_znode *znode;
1932 struct ubifs_zbranch *zbr;
1933 struct ubifs_ino_node *ino;
1934 struct fsck_inode *fscki;
1935
1936 fscki = search_inode(fsckd, inum);
1937 if (fscki)
1938 return fscki;
1939
1940 ino_key_init(c, &key, inum);
1941 err = ubifs_lookup_level0(c, &key, &znode, &n);
1942 if (!err) {
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001943 ubifs_err("inode %lu not found in index", (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001944 return ERR_PTR(-ENOENT);
1945 } else if (err < 0) {
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001946 ubifs_err("error %d while looking up inode %lu",
1947 err, (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001948 return ERR_PTR(err);
1949 }
1950
1951 zbr = &znode->zbranch[n];
1952 if (zbr->len < UBIFS_INO_NODE_SZ) {
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001953 ubifs_err("bad node %lu node length %d",
1954 (unsigned long)inum, zbr->len);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001955 return ERR_PTR(-EINVAL);
1956 }
1957
1958 ino = kmalloc(zbr->len, GFP_NOFS);
1959 if (!ino)
1960 return ERR_PTR(-ENOMEM);
1961
1962 err = ubifs_tnc_read_node(c, zbr, ino);
1963 if (err) {
1964 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
1965 zbr->lnum, zbr->offs, err);
1966 kfree(ino);
1967 return ERR_PTR(err);
1968 }
1969
1970 fscki = add_inode(c, fsckd, ino);
1971 kfree(ino);
1972 if (IS_ERR(fscki)) {
1973 ubifs_err("error %ld while adding inode %lu node",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001974 PTR_ERR(fscki), (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001975 return fscki;
1976 }
1977
1978 return fscki;
1979}
1980
1981/**
1982 * check_leaf - check leaf node.
1983 * @c: UBIFS file-system description object
1984 * @zbr: zbranch of the leaf node to check
1985 * @priv: FS checking information
1986 *
1987 * This is a helper function for 'dbg_check_filesystem()' which is called for
1988 * every single leaf node while walking the indexing tree. It checks that the
1989 * leaf node referred from the indexing tree exists, has correct CRC, and does
1990 * some other basic validation. This function is also responsible for building
1991 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
1992 * calculates reference count, size, etc for each inode in order to later
1993 * compare them to the information stored inside the inodes and detect possible
1994 * inconsistencies. Returns zero in case of success and a negative error code
1995 * in case of failure.
1996 */
1997static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
1998 void *priv)
1999{
2000 ino_t inum;
2001 void *node;
2002 struct ubifs_ch *ch;
2003 int err, type = key_type(c, &zbr->key);
2004 struct fsck_inode *fscki;
2005
2006 if (zbr->len < UBIFS_CH_SZ) {
2007 ubifs_err("bad leaf length %d (LEB %d:%d)",
2008 zbr->len, zbr->lnum, zbr->offs);
2009 return -EINVAL;
2010 }
2011
2012 node = kmalloc(zbr->len, GFP_NOFS);
2013 if (!node)
2014 return -ENOMEM;
2015
2016 err = ubifs_tnc_read_node(c, zbr, node);
2017 if (err) {
2018 ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
2019 zbr->lnum, zbr->offs, err);
2020 goto out_free;
2021 }
2022
2023 /* If this is an inode node, add it to RB-tree of inodes */
2024 if (type == UBIFS_INO_KEY) {
2025 fscki = add_inode(c, priv, node);
2026 if (IS_ERR(fscki)) {
2027 err = PTR_ERR(fscki);
2028 ubifs_err("error %d while adding inode node", err);
2029 goto out_dump;
2030 }
2031 goto out;
2032 }
2033
2034 if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
2035 type != UBIFS_DATA_KEY) {
2036 ubifs_err("unexpected node type %d at LEB %d:%d",
2037 type, zbr->lnum, zbr->offs);
2038 err = -EINVAL;
2039 goto out_free;
2040 }
2041
2042 ch = node;
2043 if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
2044 ubifs_err("too high sequence number, max. is %llu",
2045 c->max_sqnum);
2046 err = -EINVAL;
2047 goto out_dump;
2048 }
2049
2050 if (type == UBIFS_DATA_KEY) {
2051 long long blk_offs;
2052 struct ubifs_data_node *dn = node;
2053
2054 /*
2055 * Search the inode node this data node belongs to and insert
2056 * it to the RB-tree of inodes.
2057 */
2058 inum = key_inum_flash(c, &dn->key);
2059 fscki = read_add_inode(c, priv, inum);
2060 if (IS_ERR(fscki)) {
2061 err = PTR_ERR(fscki);
2062 ubifs_err("error %d while processing data node and "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002063 "trying to find inode node %lu",
2064 err, (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002065 goto out_dump;
2066 }
2067
2068 /* Make sure the data node is within inode size */
2069 blk_offs = key_block_flash(c, &dn->key);
2070 blk_offs <<= UBIFS_BLOCK_SHIFT;
2071 blk_offs += le32_to_cpu(dn->size);
2072 if (blk_offs > fscki->size) {
2073 ubifs_err("data node at LEB %d:%d is not within inode "
2074 "size %lld", zbr->lnum, zbr->offs,
2075 fscki->size);
2076 err = -EINVAL;
2077 goto out_dump;
2078 }
2079 } else {
2080 int nlen;
2081 struct ubifs_dent_node *dent = node;
2082 struct fsck_inode *fscki1;
2083
2084 err = ubifs_validate_entry(c, dent);
2085 if (err)
2086 goto out_dump;
2087
2088 /*
2089 * Search the inode node this entry refers to and the parent
2090 * inode node and insert them to the RB-tree of inodes.
2091 */
2092 inum = le64_to_cpu(dent->inum);
2093 fscki = read_add_inode(c, priv, inum);
2094 if (IS_ERR(fscki)) {
2095 err = PTR_ERR(fscki);
2096 ubifs_err("error %d while processing entry node and "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002097 "trying to find inode node %lu",
2098 err, (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002099 goto out_dump;
2100 }
2101
2102 /* Count how many direntries or xentries refers this inode */
2103 fscki->references += 1;
2104
2105 inum = key_inum_flash(c, &dent->key);
2106 fscki1 = read_add_inode(c, priv, inum);
2107 if (IS_ERR(fscki1)) {
Roel Kluinb38882f2009-12-07 14:21:45 +01002108 err = PTR_ERR(fscki1);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002109 ubifs_err("error %d while processing entry node and "
2110 "trying to find parent inode node %lu",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002111 err, (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002112 goto out_dump;
2113 }
2114
2115 nlen = le16_to_cpu(dent->nlen);
2116 if (type == UBIFS_XENT_KEY) {
2117 fscki1->calc_xcnt += 1;
2118 fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
2119 fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
2120 fscki1->calc_xnms += nlen;
2121 } else {
2122 fscki1->calc_sz += CALC_DENT_SIZE(nlen);
2123 if (dent->type == UBIFS_ITYPE_DIR)
2124 fscki1->calc_cnt += 1;
2125 }
2126 }
2127
2128out:
2129 kfree(node);
2130 return 0;
2131
2132out_dump:
2133 ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
2134 dbg_dump_node(c, node);
2135out_free:
2136 kfree(node);
2137 return err;
2138}
2139
2140/**
2141 * free_inodes - free RB-tree of inodes.
2142 * @fsckd: FS checking information
2143 */
2144static void free_inodes(struct fsck_data *fsckd)
2145{
2146 struct rb_node *this = fsckd->inodes.rb_node;
2147 struct fsck_inode *fscki;
2148
2149 while (this) {
2150 if (this->rb_left)
2151 this = this->rb_left;
2152 else if (this->rb_right)
2153 this = this->rb_right;
2154 else {
2155 fscki = rb_entry(this, struct fsck_inode, rb);
2156 this = rb_parent(this);
2157 if (this) {
2158 if (this->rb_left == &fscki->rb)
2159 this->rb_left = NULL;
2160 else
2161 this->rb_right = NULL;
2162 }
2163 kfree(fscki);
2164 }
2165 }
2166}
2167
2168/**
2169 * check_inodes - checks all inodes.
2170 * @c: UBIFS file-system description object
2171 * @fsckd: FS checking information
2172 *
2173 * This is a helper function for 'dbg_check_filesystem()' which walks the
2174 * RB-tree of inodes after the index scan has been finished, and checks that
2175 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
2176 * %-EINVAL if not, and a negative error code in case of failure.
2177 */
2178static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
2179{
2180 int n, err;
2181 union ubifs_key key;
2182 struct ubifs_znode *znode;
2183 struct ubifs_zbranch *zbr;
2184 struct ubifs_ino_node *ino;
2185 struct fsck_inode *fscki;
2186 struct rb_node *this = rb_first(&fsckd->inodes);
2187
2188 while (this) {
2189 fscki = rb_entry(this, struct fsck_inode, rb);
2190 this = rb_next(this);
2191
2192 if (S_ISDIR(fscki->mode)) {
2193 /*
2194 * Directories have to have exactly one reference (they
2195 * cannot have hardlinks), although root inode is an
2196 * exception.
2197 */
2198 if (fscki->inum != UBIFS_ROOT_INO &&
2199 fscki->references != 1) {
2200 ubifs_err("directory inode %lu has %d "
2201 "direntries which refer it, but "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002202 "should be 1",
2203 (unsigned long)fscki->inum,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002204 fscki->references);
2205 goto out_dump;
2206 }
2207 if (fscki->inum == UBIFS_ROOT_INO &&
2208 fscki->references != 0) {
2209 ubifs_err("root inode %lu has non-zero (%d) "
2210 "direntries which refer it",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002211 (unsigned long)fscki->inum,
2212 fscki->references);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002213 goto out_dump;
2214 }
2215 if (fscki->calc_sz != fscki->size) {
2216 ubifs_err("directory inode %lu size is %lld, "
2217 "but calculated size is %lld",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002218 (unsigned long)fscki->inum,
2219 fscki->size, fscki->calc_sz);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002220 goto out_dump;
2221 }
2222 if (fscki->calc_cnt != fscki->nlink) {
2223 ubifs_err("directory inode %lu nlink is %d, "
2224 "but calculated nlink is %d",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002225 (unsigned long)fscki->inum,
2226 fscki->nlink, fscki->calc_cnt);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002227 goto out_dump;
2228 }
2229 } else {
2230 if (fscki->references != fscki->nlink) {
2231 ubifs_err("inode %lu nlink is %d, but "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002232 "calculated nlink is %d",
2233 (unsigned long)fscki->inum,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002234 fscki->nlink, fscki->references);
2235 goto out_dump;
2236 }
2237 }
2238 if (fscki->xattr_sz != fscki->calc_xsz) {
2239 ubifs_err("inode %lu has xattr size %u, but "
2240 "calculated size is %lld",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002241 (unsigned long)fscki->inum, fscki->xattr_sz,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002242 fscki->calc_xsz);
2243 goto out_dump;
2244 }
2245 if (fscki->xattr_cnt != fscki->calc_xcnt) {
2246 ubifs_err("inode %lu has %u xattrs, but "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002247 "calculated count is %lld",
2248 (unsigned long)fscki->inum,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002249 fscki->xattr_cnt, fscki->calc_xcnt);
2250 goto out_dump;
2251 }
2252 if (fscki->xattr_nms != fscki->calc_xnms) {
2253 ubifs_err("inode %lu has xattr names' size %u, but "
2254 "calculated names' size is %lld",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002255 (unsigned long)fscki->inum, fscki->xattr_nms,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002256 fscki->calc_xnms);
2257 goto out_dump;
2258 }
2259 }
2260
2261 return 0;
2262
2263out_dump:
2264 /* Read the bad inode and dump it */
2265 ino_key_init(c, &key, fscki->inum);
2266 err = ubifs_lookup_level0(c, &key, &znode, &n);
2267 if (!err) {
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002268 ubifs_err("inode %lu not found in index",
2269 (unsigned long)fscki->inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002270 return -ENOENT;
2271 } else if (err < 0) {
2272 ubifs_err("error %d while looking up inode %lu",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002273 err, (unsigned long)fscki->inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002274 return err;
2275 }
2276
2277 zbr = &znode->zbranch[n];
2278 ino = kmalloc(zbr->len, GFP_NOFS);
2279 if (!ino)
2280 return -ENOMEM;
2281
2282 err = ubifs_tnc_read_node(c, zbr, ino);
2283 if (err) {
2284 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2285 zbr->lnum, zbr->offs, err);
2286 kfree(ino);
2287 return err;
2288 }
2289
2290 ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002291 (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002292 dbg_dump_node(c, ino);
2293 kfree(ino);
2294 return -EINVAL;
2295}
2296
2297/**
2298 * dbg_check_filesystem - check the file-system.
2299 * @c: UBIFS file-system description object
2300 *
2301 * This function checks the file system, namely:
2302 * o makes sure that all leaf nodes exist and their CRCs are correct;
2303 * o makes sure inode nlink, size, xattr size/count are correct (for all
2304 * inodes).
2305 *
2306 * The function reads whole indexing tree and all nodes, so it is pretty
2307 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2308 * not, and a negative error code in case of failure.
2309 */
2310int dbg_check_filesystem(struct ubifs_info *c)
2311{
2312 int err;
2313 struct fsck_data fsckd;
2314
2315 if (!(ubifs_chk_flags & UBIFS_CHK_FS))
2316 return 0;
2317
2318 fsckd.inodes = RB_ROOT;
2319 err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
2320 if (err)
2321 goto out_free;
2322
2323 err = check_inodes(c, &fsckd);
2324 if (err)
2325 goto out_free;
2326
2327 free_inodes(&fsckd);
2328 return 0;
2329
2330out_free:
2331 ubifs_err("file-system check failed with error %d", err);
2332 dump_stack();
2333 free_inodes(&fsckd);
2334 return err;
2335}
2336
Artem Bityutskiy3bb66b42010-08-07 10:06:11 +03002337/**
2338 * dbg_check_data_nodes_order - check that list of data nodes is sorted.
2339 * @c: UBIFS file-system description object
2340 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2341 *
2342 * This function returns zero if the list of data nodes is sorted correctly,
2343 * and %-EINVAL if not.
2344 */
2345int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head)
2346{
2347 struct list_head *cur;
2348 struct ubifs_scan_node *sa, *sb;
2349
2350 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
2351 return 0;
2352
2353 for (cur = head->next; cur->next != head; cur = cur->next) {
2354 ino_t inuma, inumb;
2355 uint32_t blka, blkb;
2356
2357 cond_resched();
2358 sa = container_of(cur, struct ubifs_scan_node, list);
2359 sb = container_of(cur->next, struct ubifs_scan_node, list);
2360
2361 if (sa->type != UBIFS_DATA_NODE) {
2362 ubifs_err("bad node type %d", sa->type);
2363 dbg_dump_node(c, sa->node);
2364 return -EINVAL;
2365 }
2366 if (sb->type != UBIFS_DATA_NODE) {
2367 ubifs_err("bad node type %d", sb->type);
2368 dbg_dump_node(c, sb->node);
2369 return -EINVAL;
2370 }
2371
2372 inuma = key_inum(c, &sa->key);
2373 inumb = key_inum(c, &sb->key);
2374
2375 if (inuma < inumb)
2376 continue;
2377 if (inuma > inumb) {
2378 ubifs_err("larger inum %lu goes before inum %lu",
2379 (unsigned long)inuma, (unsigned long)inumb);
2380 goto error_dump;
2381 }
2382
2383 blka = key_block(c, &sa->key);
2384 blkb = key_block(c, &sb->key);
2385
2386 if (blka > blkb) {
2387 ubifs_err("larger block %u goes before %u", blka, blkb);
2388 goto error_dump;
2389 }
2390 if (blka == blkb) {
2391 ubifs_err("two data nodes for the same block");
2392 goto error_dump;
2393 }
2394 }
2395
2396 return 0;
2397
2398error_dump:
2399 dbg_dump_node(c, sa->node);
2400 dbg_dump_node(c, sb->node);
2401 return -EINVAL;
2402}
2403
2404/**
2405 * dbg_check_nondata_nodes_order - check that list of data nodes is sorted.
2406 * @c: UBIFS file-system description object
2407 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2408 *
2409 * This function returns zero if the list of non-data nodes is sorted correctly,
2410 * and %-EINVAL if not.
2411 */
2412int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head)
2413{
2414 struct list_head *cur;
2415 struct ubifs_scan_node *sa, *sb;
2416
2417 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
2418 return 0;
2419
2420 for (cur = head->next; cur->next != head; cur = cur->next) {
2421 ino_t inuma, inumb;
2422 uint32_t hasha, hashb;
2423
2424 cond_resched();
2425 sa = container_of(cur, struct ubifs_scan_node, list);
2426 sb = container_of(cur->next, struct ubifs_scan_node, list);
2427
2428 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2429 sa->type != UBIFS_XENT_NODE) {
2430 ubifs_err("bad node type %d", sa->type);
2431 dbg_dump_node(c, sa->node);
2432 return -EINVAL;
2433 }
2434 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2435 sa->type != UBIFS_XENT_NODE) {
2436 ubifs_err("bad node type %d", sb->type);
2437 dbg_dump_node(c, sb->node);
2438 return -EINVAL;
2439 }
2440
2441 if (sa->type != UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2442 ubifs_err("non-inode node goes before inode node");
2443 goto error_dump;
2444 }
2445
2446 if (sa->type == UBIFS_INO_NODE && sb->type != UBIFS_INO_NODE)
2447 continue;
2448
2449 if (sa->type == UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2450 /* Inode nodes are sorted in descending size order */
2451 if (sa->len < sb->len) {
2452 ubifs_err("smaller inode node goes first");
2453 goto error_dump;
2454 }
2455 continue;
2456 }
2457
2458 /*
2459 * This is either a dentry or xentry, which should be sorted in
2460 * ascending (parent ino, hash) order.
2461 */
2462 inuma = key_inum(c, &sa->key);
2463 inumb = key_inum(c, &sb->key);
2464
2465 if (inuma < inumb)
2466 continue;
2467 if (inuma > inumb) {
2468 ubifs_err("larger inum %lu goes before inum %lu",
2469 (unsigned long)inuma, (unsigned long)inumb);
2470 goto error_dump;
2471 }
2472
2473 hasha = key_block(c, &sa->key);
2474 hashb = key_block(c, &sb->key);
2475
2476 if (hasha > hashb) {
Artem Bityutskiyc4361572011-03-25 15:27:40 +02002477 ubifs_err("larger hash %u goes before %u",
2478 hasha, hashb);
Artem Bityutskiy3bb66b42010-08-07 10:06:11 +03002479 goto error_dump;
2480 }
2481 }
2482
2483 return 0;
2484
2485error_dump:
2486 ubifs_msg("dumping first node");
2487 dbg_dump_node(c, sa->node);
2488 ubifs_msg("dumping second node");
2489 dbg_dump_node(c, sb->node);
2490 return -EINVAL;
2491 return 0;
2492}
2493
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002494int dbg_force_in_the_gaps(void)
2495{
Artem Bityutskiybc3f07f2011-04-05 13:52:20 +03002496 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002497 return 0;
Artem Bityutskiybc3f07f2011-04-05 13:52:20 +03002498
2499 return !(random32() & 7);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002500}
2501
2502/* Failure mode for recovery testing */
2503
2504#define chance(n, d) (simple_rand() <= (n) * 32768LL / (d))
2505
2506struct failure_mode_info {
2507 struct list_head list;
2508 struct ubifs_info *c;
2509};
2510
2511static LIST_HEAD(fmi_list);
2512static DEFINE_SPINLOCK(fmi_lock);
2513
2514static unsigned int next;
2515
2516static int simple_rand(void)
2517{
2518 if (next == 0)
2519 next = current->pid;
2520 next = next * 1103515245 + 12345;
2521 return (next >> 16) & 32767;
2522}
2523
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002524static void failure_mode_init(struct ubifs_info *c)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002525{
2526 struct failure_mode_info *fmi;
2527
2528 fmi = kmalloc(sizeof(struct failure_mode_info), GFP_NOFS);
2529 if (!fmi) {
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002530 ubifs_err("Failed to register failure mode - no memory");
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002531 return;
2532 }
2533 fmi->c = c;
2534 spin_lock(&fmi_lock);
2535 list_add_tail(&fmi->list, &fmi_list);
2536 spin_unlock(&fmi_lock);
2537}
2538
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002539static void failure_mode_exit(struct ubifs_info *c)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002540{
2541 struct failure_mode_info *fmi, *tmp;
2542
2543 spin_lock(&fmi_lock);
2544 list_for_each_entry_safe(fmi, tmp, &fmi_list, list)
2545 if (fmi->c == c) {
2546 list_del(&fmi->list);
2547 kfree(fmi);
2548 }
2549 spin_unlock(&fmi_lock);
2550}
2551
2552static struct ubifs_info *dbg_find_info(struct ubi_volume_desc *desc)
2553{
2554 struct failure_mode_info *fmi;
2555
2556 spin_lock(&fmi_lock);
2557 list_for_each_entry(fmi, &fmi_list, list)
2558 if (fmi->c->ubi == desc) {
2559 struct ubifs_info *c = fmi->c;
2560
2561 spin_unlock(&fmi_lock);
2562 return c;
2563 }
2564 spin_unlock(&fmi_lock);
2565 return NULL;
2566}
2567
2568static int in_failure_mode(struct ubi_volume_desc *desc)
2569{
2570 struct ubifs_info *c = dbg_find_info(desc);
2571
2572 if (c && dbg_failure_mode)
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002573 return c->dbg->failure_mode;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002574 return 0;
2575}
2576
2577static int do_fail(struct ubi_volume_desc *desc, int lnum, int write)
2578{
2579 struct ubifs_info *c = dbg_find_info(desc);
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002580 struct ubifs_debug_info *d;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002581
2582 if (!c || !dbg_failure_mode)
2583 return 0;
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002584 d = c->dbg;
2585 if (d->failure_mode)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002586 return 1;
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002587 if (!d->fail_cnt) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002588 /* First call - decide delay to failure */
2589 if (chance(1, 2)) {
2590 unsigned int delay = 1 << (simple_rand() >> 11);
2591
2592 if (chance(1, 2)) {
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002593 d->fail_delay = 1;
2594 d->fail_timeout = jiffies +
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002595 msecs_to_jiffies(delay);
2596 dbg_rcvry("failing after %ums", delay);
2597 } else {
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002598 d->fail_delay = 2;
2599 d->fail_cnt_max = delay;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002600 dbg_rcvry("failing after %u calls", delay);
2601 }
2602 }
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002603 d->fail_cnt += 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002604 }
2605 /* Determine if failure delay has expired */
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002606 if (d->fail_delay == 1) {
2607 if (time_before(jiffies, d->fail_timeout))
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002608 return 0;
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002609 } else if (d->fail_delay == 2)
2610 if (d->fail_cnt++ < d->fail_cnt_max)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002611 return 0;
2612 if (lnum == UBIFS_SB_LNUM) {
2613 if (write) {
2614 if (chance(1, 2))
2615 return 0;
2616 } else if (chance(19, 20))
2617 return 0;
2618 dbg_rcvry("failing in super block LEB %d", lnum);
2619 } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2620 if (chance(19, 20))
2621 return 0;
2622 dbg_rcvry("failing in master LEB %d", lnum);
2623 } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2624 if (write) {
2625 if (chance(99, 100))
2626 return 0;
2627 } else if (chance(399, 400))
2628 return 0;
2629 dbg_rcvry("failing in log LEB %d", lnum);
2630 } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2631 if (write) {
2632 if (chance(7, 8))
2633 return 0;
2634 } else if (chance(19, 20))
2635 return 0;
2636 dbg_rcvry("failing in LPT LEB %d", lnum);
2637 } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2638 if (write) {
2639 if (chance(1, 2))
2640 return 0;
2641 } else if (chance(9, 10))
2642 return 0;
2643 dbg_rcvry("failing in orphan LEB %d", lnum);
2644 } else if (lnum == c->ihead_lnum) {
2645 if (chance(99, 100))
2646 return 0;
2647 dbg_rcvry("failing in index head LEB %d", lnum);
2648 } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2649 if (chance(9, 10))
2650 return 0;
2651 dbg_rcvry("failing in GC head LEB %d", lnum);
2652 } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
2653 !ubifs_search_bud(c, lnum)) {
2654 if (chance(19, 20))
2655 return 0;
2656 dbg_rcvry("failing in non-bud LEB %d", lnum);
2657 } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
2658 c->cmt_state == COMMIT_RUNNING_REQUIRED) {
2659 if (chance(999, 1000))
2660 return 0;
2661 dbg_rcvry("failing in bud LEB %d commit running", lnum);
2662 } else {
2663 if (chance(9999, 10000))
2664 return 0;
2665 dbg_rcvry("failing in bud LEB %d commit not running", lnum);
2666 }
2667 ubifs_err("*** SETTING FAILURE MODE ON (LEB %d) ***", lnum);
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002668 d->failure_mode = 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002669 dump_stack();
2670 return 1;
2671}
2672
2673static void cut_data(const void *buf, int len)
2674{
2675 int flen, i;
2676 unsigned char *p = (void *)buf;
2677
2678 flen = (len * (long long)simple_rand()) >> 15;
2679 for (i = flen; i < len; i++)
2680 p[i] = 0xff;
2681}
2682
2683int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
2684 int len, int check)
2685{
2686 if (in_failure_mode(desc))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002687 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002688 return ubi_leb_read(desc, lnum, buf, offset, len, check);
2689}
2690
2691int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
2692 int offset, int len, int dtype)
2693{
Adrian Hunter16dfd802008-07-18 16:47:41 +03002694 int err, failing;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002695
2696 if (in_failure_mode(desc))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002697 return -EROFS;
Adrian Hunter16dfd802008-07-18 16:47:41 +03002698 failing = do_fail(desc, lnum, 1);
2699 if (failing)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002700 cut_data(buf, len);
2701 err = ubi_leb_write(desc, lnum, buf, offset, len, dtype);
2702 if (err)
2703 return err;
Adrian Hunter16dfd802008-07-18 16:47:41 +03002704 if (failing)
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002705 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002706 return 0;
2707}
2708
2709int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
2710 int len, int dtype)
2711{
2712 int err;
2713
2714 if (do_fail(desc, lnum, 1))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002715 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002716 err = ubi_leb_change(desc, lnum, buf, len, dtype);
2717 if (err)
2718 return err;
2719 if (do_fail(desc, lnum, 1))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002720 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002721 return 0;
2722}
2723
2724int dbg_leb_erase(struct ubi_volume_desc *desc, int lnum)
2725{
2726 int err;
2727
2728 if (do_fail(desc, lnum, 0))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002729 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002730 err = ubi_leb_erase(desc, lnum);
2731 if (err)
2732 return err;
2733 if (do_fail(desc, lnum, 0))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002734 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002735 return 0;
2736}
2737
2738int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum)
2739{
2740 int err;
2741
2742 if (do_fail(desc, lnum, 0))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002743 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002744 err = ubi_leb_unmap(desc, lnum);
2745 if (err)
2746 return err;
2747 if (do_fail(desc, lnum, 0))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002748 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002749 return 0;
2750}
2751
2752int dbg_is_mapped(struct ubi_volume_desc *desc, int lnum)
2753{
2754 if (in_failure_mode(desc))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002755 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002756 return ubi_is_mapped(desc, lnum);
2757}
2758
2759int dbg_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
2760{
2761 int err;
2762
2763 if (do_fail(desc, lnum, 0))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002764 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002765 err = ubi_leb_map(desc, lnum, dtype);
2766 if (err)
2767 return err;
2768 if (do_fail(desc, lnum, 0))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002769 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002770 return 0;
2771}
2772
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002773/**
2774 * ubifs_debugging_init - initialize UBIFS debugging.
2775 * @c: UBIFS file-system description object
2776 *
2777 * This function initializes debugging-related data for the file system.
2778 * Returns zero in case of success and a negative error code in case of
2779 * failure.
2780 */
2781int ubifs_debugging_init(struct ubifs_info *c)
2782{
2783 c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
2784 if (!c->dbg)
2785 return -ENOMEM;
2786
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002787 failure_mode_init(c);
2788 return 0;
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002789}
2790
2791/**
2792 * ubifs_debugging_exit - free debugging data.
2793 * @c: UBIFS file-system description object
2794 */
2795void ubifs_debugging_exit(struct ubifs_info *c)
2796{
2797 failure_mode_exit(c);
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002798 kfree(c->dbg);
2799}
2800
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002801/*
2802 * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
2803 * contain the stuff specific to particular file-system mounts.
2804 */
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002805static struct dentry *dfs_rootdir;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002806
2807/**
2808 * dbg_debugfs_init - initialize debugfs file-system.
2809 *
2810 * UBIFS uses debugfs file-system to expose various debugging knobs to
2811 * user-space. This function creates "ubifs" directory in the debugfs
2812 * file-system. Returns zero in case of success and a negative error code in
2813 * case of failure.
2814 */
2815int dbg_debugfs_init(void)
2816{
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002817 dfs_rootdir = debugfs_create_dir("ubifs", NULL);
Artem Bityutskiyae380ce2011-05-19 14:13:16 +03002818 if (IS_ERR_OR_NULL(dfs_rootdir)) {
2819 int err = dfs_rootdir ? PTR_ERR(dfs_rootdir) : -ENODEV;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002820 ubifs_err("cannot create \"ubifs\" debugfs directory, "
2821 "error %d\n", err);
2822 return err;
2823 }
2824
2825 return 0;
2826}
2827
2828/**
2829 * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
2830 */
2831void dbg_debugfs_exit(void)
2832{
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002833 debugfs_remove(dfs_rootdir);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002834}
2835
2836static int open_debugfs_file(struct inode *inode, struct file *file)
2837{
2838 file->private_data = inode->i_private;
Artem Bityutskiy1bbfc842011-03-21 16:26:42 +02002839 return nonseekable_open(inode, file);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002840}
2841
2842static ssize_t write_debugfs_file(struct file *file, const char __user *buf,
2843 size_t count, loff_t *ppos)
2844{
2845 struct ubifs_info *c = file->private_data;
2846 struct ubifs_debug_info *d = c->dbg;
2847
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002848 if (file->f_path.dentry == d->dfs_dump_lprops)
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002849 dbg_dump_lprops(c);
Artem Bityutskiy8ff83082011-03-29 18:19:50 +03002850 else if (file->f_path.dentry == d->dfs_dump_budg)
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +03002851 dbg_dump_budg(c, &c->bi);
Artem Bityutskiy8ff83082011-03-29 18:19:50 +03002852 else if (file->f_path.dentry == d->dfs_dump_tnc) {
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002853 mutex_lock(&c->tnc_mutex);
2854 dbg_dump_tnc(c);
2855 mutex_unlock(&c->tnc_mutex);
2856 } else
2857 return -EINVAL;
2858
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002859 return count;
2860}
2861
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002862static const struct file_operations dfs_fops = {
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002863 .open = open_debugfs_file,
2864 .write = write_debugfs_file,
2865 .owner = THIS_MODULE,
Artem Bityutskiy1bbfc842011-03-21 16:26:42 +02002866 .llseek = no_llseek,
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002867};
2868
2869/**
2870 * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
2871 * @c: UBIFS file-system description object
2872 *
2873 * This function creates all debugfs files for this instance of UBIFS. Returns
2874 * zero in case of success and a negative error code in case of failure.
2875 *
2876 * Note, the only reason we have not merged this function with the
2877 * 'ubifs_debugging_init()' function is because it is better to initialize
2878 * debugfs interfaces at the very end of the mount process, and remove them at
2879 * the very beginning of the mount process.
2880 */
2881int dbg_debugfs_init_fs(struct ubifs_info *c)
2882{
Artem Bityutskiyae380ce2011-05-19 14:13:16 +03002883 int err, n;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002884 const char *fname;
2885 struct dentry *dent;
2886 struct ubifs_debug_info *d = c->dbg;
2887
Artem Bityutskiyae380ce2011-05-19 14:13:16 +03002888 n = snprintf(d->dfs_dir_name, UBIFS_DFS_DIR_LEN + 1, UBIFS_DFS_DIR_NAME,
2889 c->vi.ubi_num, c->vi.vol_id);
2890 if (n == UBIFS_DFS_DIR_LEN) {
2891 /* The array size is too small */
2892 fname = UBIFS_DFS_DIR_NAME;
2893 dent = ERR_PTR(-EINVAL);
2894 goto out;
2895 }
2896
Artem Bityutskiycc6a86b2011-04-01 10:10:52 +03002897 fname = d->dfs_dir_name;
2898 dent = debugfs_create_dir(fname, dfs_rootdir);
Artem Bityutskiy95169532011-04-01 10:16:17 +03002899 if (IS_ERR_OR_NULL(dent))
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002900 goto out;
Artem Bityutskiycc6a86b2011-04-01 10:10:52 +03002901 d->dfs_dir = dent;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002902
2903 fname = "dump_lprops";
Vasiliy Kulikov8c559d32011-02-04 15:24:19 +03002904 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
Artem Bityutskiy95169532011-04-01 10:16:17 +03002905 if (IS_ERR_OR_NULL(dent))
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002906 goto out_remove;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002907 d->dfs_dump_lprops = dent;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002908
2909 fname = "dump_budg";
Vasiliy Kulikov8c559d32011-02-04 15:24:19 +03002910 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
Artem Bityutskiy95169532011-04-01 10:16:17 +03002911 if (IS_ERR_OR_NULL(dent))
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002912 goto out_remove;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002913 d->dfs_dump_budg = dent;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002914
2915 fname = "dump_tnc";
Vasiliy Kulikov8c559d32011-02-04 15:24:19 +03002916 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
Artem Bityutskiy95169532011-04-01 10:16:17 +03002917 if (IS_ERR_OR_NULL(dent))
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002918 goto out_remove;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002919 d->dfs_dump_tnc = dent;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002920
2921 return 0;
2922
2923out_remove:
Artem Bityutskiycc6a86b2011-04-01 10:10:52 +03002924 debugfs_remove_recursive(d->dfs_dir);
2925out:
Artem Bityutskiy95169532011-04-01 10:16:17 +03002926 err = dent ? PTR_ERR(dent) : -ENODEV;
Artem Bityutskiyae380ce2011-05-19 14:13:16 +03002927 ubifs_err("cannot create \"%s\" debugfs filr or directory, error %d\n",
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002928 fname, err);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002929 return err;
2930}
2931
2932/**
2933 * dbg_debugfs_exit_fs - remove all debugfs files.
2934 * @c: UBIFS file-system description object
2935 */
2936void dbg_debugfs_exit_fs(struct ubifs_info *c)
2937{
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002938 debugfs_remove_recursive(c->dbg->dfs_dir);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002939}
2940
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002941#endif /* CONFIG_UBIFS_FS_DEBUG */