blob: 059c586b5ef840b6e00f9a6fd22a7f36810a7cb9 [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>
Artem Bityutskiy552ff312008-10-23 11:49:28 +030034#include <linux/debugfs.h>
Artem Bityutskiy4d61db42008-12-18 14:06:51 +020035#include <linux/math64.h>
Artem Bityutskiy81e79d32011-05-31 18:16:34 +030036#include <linux/uaccess.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 Bityutskiy1e517642008-07-14 19:08:37 +030045static const char *get_key_fmt(int fmt)
46{
47 switch (fmt) {
48 case UBIFS_SIMPLE_KEY_FMT:
49 return "simple";
50 default:
51 return "unknown/invalid format";
52 }
53}
54
55static const char *get_key_hash(int hash)
56{
57 switch (hash) {
58 case UBIFS_KEY_HASH_R5:
59 return "R5";
60 case UBIFS_KEY_HASH_TEST:
61 return "test";
62 default:
63 return "unknown/invalid name hash";
64 }
65}
66
67static const char *get_key_type(int type)
68{
69 switch (type) {
70 case UBIFS_INO_KEY:
71 return "inode";
72 case UBIFS_DENT_KEY:
73 return "direntry";
74 case UBIFS_XENT_KEY:
75 return "xentry";
76 case UBIFS_DATA_KEY:
77 return "data";
78 case UBIFS_TRUN_KEY:
79 return "truncate";
80 default:
81 return "unknown/invalid key";
82 }
83}
84
Artem Bityutskiy4315fb42011-05-25 17:32:42 +030085static const char *get_dent_type(int type)
86{
87 switch (type) {
88 case UBIFS_ITYPE_REG:
89 return "file";
90 case UBIFS_ITYPE_DIR:
91 return "dir";
92 case UBIFS_ITYPE_LNK:
93 return "symlink";
94 case UBIFS_ITYPE_BLK:
95 return "blkdev";
96 case UBIFS_ITYPE_CHR:
97 return "char dev";
98 case UBIFS_ITYPE_FIFO:
99 return "fifo";
100 case UBIFS_ITYPE_SOCK:
101 return "socket";
102 default:
103 return "unknown/invalid type";
104 }
105}
106
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300107static void sprintf_key(const struct ubifs_info *c, const union ubifs_key *key,
108 char *buffer)
109{
110 char *p = buffer;
111 int type = key_type(c, key);
112
113 if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
114 switch (type) {
115 case UBIFS_INO_KEY:
Artem Bityutskiye84461a2008-10-29 12:08:43 +0200116 sprintf(p, "(%lu, %s)", (unsigned long)key_inum(c, key),
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300117 get_key_type(type));
118 break;
119 case UBIFS_DENT_KEY:
120 case UBIFS_XENT_KEY:
Artem Bityutskiye84461a2008-10-29 12:08:43 +0200121 sprintf(p, "(%lu, %s, %#08x)",
122 (unsigned long)key_inum(c, key),
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300123 get_key_type(type), key_hash(c, key));
124 break;
125 case UBIFS_DATA_KEY:
Artem Bityutskiye84461a2008-10-29 12:08:43 +0200126 sprintf(p, "(%lu, %s, %u)",
127 (unsigned long)key_inum(c, key),
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300128 get_key_type(type), key_block(c, key));
129 break;
130 case UBIFS_TRUN_KEY:
131 sprintf(p, "(%lu, %s)",
Artem Bityutskiye84461a2008-10-29 12:08:43 +0200132 (unsigned long)key_inum(c, key),
133 get_key_type(type));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300134 break;
135 default:
136 sprintf(p, "(bad key type: %#08x, %#08x)",
137 key->u32[0], key->u32[1]);
138 }
139 } else
140 sprintf(p, "bad key format %d", c->key_fmt);
141}
142
143const char *dbg_key_str0(const struct ubifs_info *c, const union ubifs_key *key)
144{
145 /* dbg_lock must be held */
146 sprintf_key(c, key, dbg_key_buf0);
147 return dbg_key_buf0;
148}
149
150const char *dbg_key_str1(const struct ubifs_info *c, const union ubifs_key *key)
151{
152 /* dbg_lock must be held */
153 sprintf_key(c, key, dbg_key_buf1);
154 return dbg_key_buf1;
155}
156
157const char *dbg_ntype(int type)
158{
159 switch (type) {
160 case UBIFS_PAD_NODE:
161 return "padding node";
162 case UBIFS_SB_NODE:
163 return "superblock node";
164 case UBIFS_MST_NODE:
165 return "master node";
166 case UBIFS_REF_NODE:
167 return "reference node";
168 case UBIFS_INO_NODE:
169 return "inode node";
170 case UBIFS_DENT_NODE:
171 return "direntry node";
172 case UBIFS_XENT_NODE:
173 return "xentry node";
174 case UBIFS_DATA_NODE:
175 return "data node";
176 case UBIFS_TRUN_NODE:
177 return "truncate node";
178 case UBIFS_IDX_NODE:
179 return "indexing node";
180 case UBIFS_CS_NODE:
181 return "commit start node";
182 case UBIFS_ORPH_NODE:
183 return "orphan node";
184 default:
185 return "unknown node";
186 }
187}
188
189static const char *dbg_gtype(int type)
190{
191 switch (type) {
192 case UBIFS_NO_NODE_GROUP:
193 return "no node group";
194 case UBIFS_IN_NODE_GROUP:
195 return "in node group";
196 case UBIFS_LAST_OF_NODE_GROUP:
197 return "last of node group";
198 default:
199 return "unknown";
200 }
201}
202
203const char *dbg_cstate(int cmt_state)
204{
205 switch (cmt_state) {
206 case COMMIT_RESTING:
207 return "commit resting";
208 case COMMIT_BACKGROUND:
209 return "background commit requested";
210 case COMMIT_REQUIRED:
211 return "commit required";
212 case COMMIT_RUNNING_BACKGROUND:
213 return "BACKGROUND commit running";
214 case COMMIT_RUNNING_REQUIRED:
215 return "commit running and required";
216 case COMMIT_BROKEN:
217 return "broken commit";
218 default:
219 return "unknown commit state";
220 }
221}
222
Artem Bityutskiy77a7ae52009-09-15 15:03:51 +0300223const char *dbg_jhead(int jhead)
224{
225 switch (jhead) {
226 case GCHD:
227 return "0 (GC)";
228 case BASEHD:
229 return "1 (base)";
230 case DATAHD:
231 return "2 (data)";
232 default:
233 return "unknown journal head";
234 }
235}
236
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300237static void dump_ch(const struct ubifs_ch *ch)
238{
239 printk(KERN_DEBUG "\tmagic %#x\n", le32_to_cpu(ch->magic));
240 printk(KERN_DEBUG "\tcrc %#x\n", le32_to_cpu(ch->crc));
241 printk(KERN_DEBUG "\tnode_type %d (%s)\n", ch->node_type,
242 dbg_ntype(ch->node_type));
243 printk(KERN_DEBUG "\tgroup_type %d (%s)\n", ch->group_type,
244 dbg_gtype(ch->group_type));
245 printk(KERN_DEBUG "\tsqnum %llu\n",
246 (unsigned long long)le64_to_cpu(ch->sqnum));
247 printk(KERN_DEBUG "\tlen %u\n", le32_to_cpu(ch->len));
248}
249
Artem Bityutskiy4315fb42011-05-25 17:32:42 +0300250void dbg_dump_inode(struct ubifs_info *c, const struct inode *inode)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300251{
252 const struct ubifs_inode *ui = ubifs_inode(inode);
Artem Bityutskiy4315fb42011-05-25 17:32:42 +0300253 struct qstr nm = { .name = NULL };
254 union ubifs_key key;
255 struct ubifs_dent_node *dent, *pdent = NULL;
256 int count = 2;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300257
Artem Bityutskiyb5e426e2008-09-09 11:20:35 +0300258 printk(KERN_DEBUG "Dump in-memory inode:");
259 printk(KERN_DEBUG "\tinode %lu\n", inode->i_ino);
260 printk(KERN_DEBUG "\tsize %llu\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300261 (unsigned long long)i_size_read(inode));
Artem Bityutskiyb5e426e2008-09-09 11:20:35 +0300262 printk(KERN_DEBUG "\tnlink %u\n", inode->i_nlink);
263 printk(KERN_DEBUG "\tuid %u\n", (unsigned int)inode->i_uid);
264 printk(KERN_DEBUG "\tgid %u\n", (unsigned int)inode->i_gid);
265 printk(KERN_DEBUG "\tatime %u.%u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300266 (unsigned int)inode->i_atime.tv_sec,
267 (unsigned int)inode->i_atime.tv_nsec);
Artem Bityutskiyb5e426e2008-09-09 11:20:35 +0300268 printk(KERN_DEBUG "\tmtime %u.%u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300269 (unsigned int)inode->i_mtime.tv_sec,
270 (unsigned int)inode->i_mtime.tv_nsec);
Artem Bityutskiyb5e426e2008-09-09 11:20:35 +0300271 printk(KERN_DEBUG "\tctime %u.%u\n",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300272 (unsigned int)inode->i_ctime.tv_sec,
273 (unsigned int)inode->i_ctime.tv_nsec);
Artem Bityutskiyb5e426e2008-09-09 11:20:35 +0300274 printk(KERN_DEBUG "\tcreat_sqnum %llu\n", ui->creat_sqnum);
275 printk(KERN_DEBUG "\txattr_size %u\n", ui->xattr_size);
276 printk(KERN_DEBUG "\txattr_cnt %u\n", ui->xattr_cnt);
277 printk(KERN_DEBUG "\txattr_names %u\n", ui->xattr_names);
278 printk(KERN_DEBUG "\tdirty %u\n", ui->dirty);
279 printk(KERN_DEBUG "\txattr %u\n", ui->xattr);
280 printk(KERN_DEBUG "\tbulk_read %u\n", ui->xattr);
281 printk(KERN_DEBUG "\tsynced_i_size %llu\n",
282 (unsigned long long)ui->synced_i_size);
283 printk(KERN_DEBUG "\tui_size %llu\n",
284 (unsigned long long)ui->ui_size);
285 printk(KERN_DEBUG "\tflags %d\n", ui->flags);
286 printk(KERN_DEBUG "\tcompr_type %d\n", ui->compr_type);
287 printk(KERN_DEBUG "\tlast_page_read %lu\n", ui->last_page_read);
288 printk(KERN_DEBUG "\tread_in_a_row %lu\n", ui->read_in_a_row);
289 printk(KERN_DEBUG "\tdata_len %d\n", ui->data_len);
Artem Bityutskiy4315fb42011-05-25 17:32:42 +0300290
291 if (!S_ISDIR(inode->i_mode))
292 return;
293
294 printk(KERN_DEBUG "List of directory entries:\n");
295 ubifs_assert(!mutex_is_locked(&c->tnc_mutex));
296
297 lowest_dent_key(c, &key, inode->i_ino);
298 while (1) {
299 dent = ubifs_tnc_next_ent(c, &key, &nm);
300 if (IS_ERR(dent)) {
301 if (PTR_ERR(dent) != -ENOENT)
302 printk(KERN_DEBUG "error %ld\n", PTR_ERR(dent));
303 break;
304 }
305
306 printk(KERN_DEBUG "\t%d: %s (%s)\n",
307 count++, dent->name, get_dent_type(dent->type));
308
309 nm.name = dent->name;
310 nm.len = le16_to_cpu(dent->nlen);
311 kfree(pdent);
312 pdent = dent;
313 key_read(c, &dent->key, &key);
314 }
315 kfree(pdent);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300316}
317
318void dbg_dump_node(const struct ubifs_info *c, const void *node)
319{
320 int i, n;
321 union ubifs_key key;
322 const struct ubifs_ch *ch = node;
323
Artem Bityutskiy2b1844a2011-06-03 08:31:29 +0300324 if (dbg_is_tst_rcvry(c))
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300325 return;
326
327 /* If the magic is incorrect, just hexdump the first bytes */
328 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
329 printk(KERN_DEBUG "Not a node, first %zu bytes:", UBIFS_CH_SZ);
330 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
331 (void *)node, UBIFS_CH_SZ, 1);
332 return;
333 }
334
335 spin_lock(&dbg_lock);
336 dump_ch(node);
337
338 switch (ch->node_type) {
339 case UBIFS_PAD_NODE:
340 {
341 const struct ubifs_pad_node *pad = node;
342
343 printk(KERN_DEBUG "\tpad_len %u\n",
344 le32_to_cpu(pad->pad_len));
345 break;
346 }
347 case UBIFS_SB_NODE:
348 {
349 const struct ubifs_sb_node *sup = node;
350 unsigned int sup_flags = le32_to_cpu(sup->flags);
351
352 printk(KERN_DEBUG "\tkey_hash %d (%s)\n",
353 (int)sup->key_hash, get_key_hash(sup->key_hash));
354 printk(KERN_DEBUG "\tkey_fmt %d (%s)\n",
355 (int)sup->key_fmt, get_key_fmt(sup->key_fmt));
356 printk(KERN_DEBUG "\tflags %#x\n", sup_flags);
357 printk(KERN_DEBUG "\t big_lpt %u\n",
358 !!(sup_flags & UBIFS_FLG_BIGLPT));
Matthew L. Creech9f58d352011-05-05 16:33:20 -0400359 printk(KERN_DEBUG "\t space_fixup %u\n",
360 !!(sup_flags & UBIFS_FLG_SPACE_FIXUP));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300361 printk(KERN_DEBUG "\tmin_io_size %u\n",
362 le32_to_cpu(sup->min_io_size));
363 printk(KERN_DEBUG "\tleb_size %u\n",
364 le32_to_cpu(sup->leb_size));
365 printk(KERN_DEBUG "\tleb_cnt %u\n",
366 le32_to_cpu(sup->leb_cnt));
367 printk(KERN_DEBUG "\tmax_leb_cnt %u\n",
368 le32_to_cpu(sup->max_leb_cnt));
369 printk(KERN_DEBUG "\tmax_bud_bytes %llu\n",
370 (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
371 printk(KERN_DEBUG "\tlog_lebs %u\n",
372 le32_to_cpu(sup->log_lebs));
373 printk(KERN_DEBUG "\tlpt_lebs %u\n",
374 le32_to_cpu(sup->lpt_lebs));
375 printk(KERN_DEBUG "\torph_lebs %u\n",
376 le32_to_cpu(sup->orph_lebs));
377 printk(KERN_DEBUG "\tjhead_cnt %u\n",
378 le32_to_cpu(sup->jhead_cnt));
379 printk(KERN_DEBUG "\tfanout %u\n",
380 le32_to_cpu(sup->fanout));
381 printk(KERN_DEBUG "\tlsave_cnt %u\n",
382 le32_to_cpu(sup->lsave_cnt));
383 printk(KERN_DEBUG "\tdefault_compr %u\n",
384 (int)le16_to_cpu(sup->default_compr));
385 printk(KERN_DEBUG "\trp_size %llu\n",
386 (unsigned long long)le64_to_cpu(sup->rp_size));
387 printk(KERN_DEBUG "\trp_uid %u\n",
388 le32_to_cpu(sup->rp_uid));
389 printk(KERN_DEBUG "\trp_gid %u\n",
390 le32_to_cpu(sup->rp_gid));
391 printk(KERN_DEBUG "\tfmt_version %u\n",
392 le32_to_cpu(sup->fmt_version));
393 printk(KERN_DEBUG "\ttime_gran %u\n",
394 le32_to_cpu(sup->time_gran));
Joe Perches7f2f4e72009-12-14 18:01:13 -0800395 printk(KERN_DEBUG "\tUUID %pUB\n",
396 sup->uuid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300397 break;
398 }
399 case UBIFS_MST_NODE:
400 {
401 const struct ubifs_mst_node *mst = node;
402
403 printk(KERN_DEBUG "\thighest_inum %llu\n",
404 (unsigned long long)le64_to_cpu(mst->highest_inum));
405 printk(KERN_DEBUG "\tcommit number %llu\n",
406 (unsigned long long)le64_to_cpu(mst->cmt_no));
407 printk(KERN_DEBUG "\tflags %#x\n",
408 le32_to_cpu(mst->flags));
409 printk(KERN_DEBUG "\tlog_lnum %u\n",
410 le32_to_cpu(mst->log_lnum));
411 printk(KERN_DEBUG "\troot_lnum %u\n",
412 le32_to_cpu(mst->root_lnum));
413 printk(KERN_DEBUG "\troot_offs %u\n",
414 le32_to_cpu(mst->root_offs));
415 printk(KERN_DEBUG "\troot_len %u\n",
416 le32_to_cpu(mst->root_len));
417 printk(KERN_DEBUG "\tgc_lnum %u\n",
418 le32_to_cpu(mst->gc_lnum));
419 printk(KERN_DEBUG "\tihead_lnum %u\n",
420 le32_to_cpu(mst->ihead_lnum));
421 printk(KERN_DEBUG "\tihead_offs %u\n",
422 le32_to_cpu(mst->ihead_offs));
Harvey Harrison0ecb9522008-10-24 10:52:57 -0700423 printk(KERN_DEBUG "\tindex_size %llu\n",
424 (unsigned long long)le64_to_cpu(mst->index_size));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300425 printk(KERN_DEBUG "\tlpt_lnum %u\n",
426 le32_to_cpu(mst->lpt_lnum));
427 printk(KERN_DEBUG "\tlpt_offs %u\n",
428 le32_to_cpu(mst->lpt_offs));
429 printk(KERN_DEBUG "\tnhead_lnum %u\n",
430 le32_to_cpu(mst->nhead_lnum));
431 printk(KERN_DEBUG "\tnhead_offs %u\n",
432 le32_to_cpu(mst->nhead_offs));
433 printk(KERN_DEBUG "\tltab_lnum %u\n",
434 le32_to_cpu(mst->ltab_lnum));
435 printk(KERN_DEBUG "\tltab_offs %u\n",
436 le32_to_cpu(mst->ltab_offs));
437 printk(KERN_DEBUG "\tlsave_lnum %u\n",
438 le32_to_cpu(mst->lsave_lnum));
439 printk(KERN_DEBUG "\tlsave_offs %u\n",
440 le32_to_cpu(mst->lsave_offs));
441 printk(KERN_DEBUG "\tlscan_lnum %u\n",
442 le32_to_cpu(mst->lscan_lnum));
443 printk(KERN_DEBUG "\tleb_cnt %u\n",
444 le32_to_cpu(mst->leb_cnt));
445 printk(KERN_DEBUG "\tempty_lebs %u\n",
446 le32_to_cpu(mst->empty_lebs));
447 printk(KERN_DEBUG "\tidx_lebs %u\n",
448 le32_to_cpu(mst->idx_lebs));
449 printk(KERN_DEBUG "\ttotal_free %llu\n",
450 (unsigned long long)le64_to_cpu(mst->total_free));
451 printk(KERN_DEBUG "\ttotal_dirty %llu\n",
452 (unsigned long long)le64_to_cpu(mst->total_dirty));
453 printk(KERN_DEBUG "\ttotal_used %llu\n",
454 (unsigned long long)le64_to_cpu(mst->total_used));
455 printk(KERN_DEBUG "\ttotal_dead %llu\n",
456 (unsigned long long)le64_to_cpu(mst->total_dead));
457 printk(KERN_DEBUG "\ttotal_dark %llu\n",
458 (unsigned long long)le64_to_cpu(mst->total_dark));
459 break;
460 }
461 case UBIFS_REF_NODE:
462 {
463 const struct ubifs_ref_node *ref = node;
464
465 printk(KERN_DEBUG "\tlnum %u\n",
466 le32_to_cpu(ref->lnum));
467 printk(KERN_DEBUG "\toffs %u\n",
468 le32_to_cpu(ref->offs));
469 printk(KERN_DEBUG "\tjhead %u\n",
470 le32_to_cpu(ref->jhead));
471 break;
472 }
473 case UBIFS_INO_NODE:
474 {
475 const struct ubifs_ino_node *ino = node;
476
477 key_read(c, &ino->key, &key);
478 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
479 printk(KERN_DEBUG "\tcreat_sqnum %llu\n",
480 (unsigned long long)le64_to_cpu(ino->creat_sqnum));
481 printk(KERN_DEBUG "\tsize %llu\n",
482 (unsigned long long)le64_to_cpu(ino->size));
483 printk(KERN_DEBUG "\tnlink %u\n",
484 le32_to_cpu(ino->nlink));
485 printk(KERN_DEBUG "\tatime %lld.%u\n",
486 (long long)le64_to_cpu(ino->atime_sec),
487 le32_to_cpu(ino->atime_nsec));
488 printk(KERN_DEBUG "\tmtime %lld.%u\n",
489 (long long)le64_to_cpu(ino->mtime_sec),
490 le32_to_cpu(ino->mtime_nsec));
491 printk(KERN_DEBUG "\tctime %lld.%u\n",
492 (long long)le64_to_cpu(ino->ctime_sec),
493 le32_to_cpu(ino->ctime_nsec));
494 printk(KERN_DEBUG "\tuid %u\n",
495 le32_to_cpu(ino->uid));
496 printk(KERN_DEBUG "\tgid %u\n",
497 le32_to_cpu(ino->gid));
498 printk(KERN_DEBUG "\tmode %u\n",
499 le32_to_cpu(ino->mode));
500 printk(KERN_DEBUG "\tflags %#x\n",
501 le32_to_cpu(ino->flags));
502 printk(KERN_DEBUG "\txattr_cnt %u\n",
503 le32_to_cpu(ino->xattr_cnt));
504 printk(KERN_DEBUG "\txattr_size %u\n",
505 le32_to_cpu(ino->xattr_size));
506 printk(KERN_DEBUG "\txattr_names %u\n",
507 le32_to_cpu(ino->xattr_names));
508 printk(KERN_DEBUG "\tcompr_type %#x\n",
509 (int)le16_to_cpu(ino->compr_type));
510 printk(KERN_DEBUG "\tdata len %u\n",
511 le32_to_cpu(ino->data_len));
512 break;
513 }
514 case UBIFS_DENT_NODE:
515 case UBIFS_XENT_NODE:
516 {
517 const struct ubifs_dent_node *dent = node;
518 int nlen = le16_to_cpu(dent->nlen);
519
520 key_read(c, &dent->key, &key);
521 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
522 printk(KERN_DEBUG "\tinum %llu\n",
523 (unsigned long long)le64_to_cpu(dent->inum));
524 printk(KERN_DEBUG "\ttype %d\n", (int)dent->type);
525 printk(KERN_DEBUG "\tnlen %d\n", nlen);
526 printk(KERN_DEBUG "\tname ");
527
528 if (nlen > UBIFS_MAX_NLEN)
529 printk(KERN_DEBUG "(bad name length, not printing, "
530 "bad or corrupted node)");
531 else {
532 for (i = 0; i < nlen && dent->name[i]; i++)
Artem Bityutskiyc9927c32009-03-16 09:42:03 +0200533 printk(KERN_CONT "%c", dent->name[i]);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300534 }
Artem Bityutskiyc9927c32009-03-16 09:42:03 +0200535 printk(KERN_CONT "\n");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300536
537 break;
538 }
539 case UBIFS_DATA_NODE:
540 {
541 const struct ubifs_data_node *dn = node;
542 int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
543
544 key_read(c, &dn->key, &key);
545 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
546 printk(KERN_DEBUG "\tsize %u\n",
547 le32_to_cpu(dn->size));
548 printk(KERN_DEBUG "\tcompr_typ %d\n",
549 (int)le16_to_cpu(dn->compr_type));
550 printk(KERN_DEBUG "\tdata size %d\n",
551 dlen);
552 printk(KERN_DEBUG "\tdata:\n");
553 print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET, 32, 1,
554 (void *)&dn->data, dlen, 0);
555 break;
556 }
557 case UBIFS_TRUN_NODE:
558 {
559 const struct ubifs_trun_node *trun = node;
560
561 printk(KERN_DEBUG "\tinum %u\n",
562 le32_to_cpu(trun->inum));
563 printk(KERN_DEBUG "\told_size %llu\n",
564 (unsigned long long)le64_to_cpu(trun->old_size));
565 printk(KERN_DEBUG "\tnew_size %llu\n",
566 (unsigned long long)le64_to_cpu(trun->new_size));
567 break;
568 }
569 case UBIFS_IDX_NODE:
570 {
571 const struct ubifs_idx_node *idx = node;
572
573 n = le16_to_cpu(idx->child_cnt);
574 printk(KERN_DEBUG "\tchild_cnt %d\n", n);
575 printk(KERN_DEBUG "\tlevel %d\n",
576 (int)le16_to_cpu(idx->level));
577 printk(KERN_DEBUG "\tBranches:\n");
578
579 for (i = 0; i < n && i < c->fanout - 1; i++) {
580 const struct ubifs_branch *br;
581
582 br = ubifs_idx_branch(c, idx, i);
583 key_read(c, &br->key, &key);
584 printk(KERN_DEBUG "\t%d: LEB %d:%d len %d key %s\n",
585 i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
586 le32_to_cpu(br->len), DBGKEY(&key));
587 }
588 break;
589 }
590 case UBIFS_CS_NODE:
591 break;
592 case UBIFS_ORPH_NODE:
593 {
594 const struct ubifs_orph_node *orph = node;
595
596 printk(KERN_DEBUG "\tcommit number %llu\n",
597 (unsigned long long)
598 le64_to_cpu(orph->cmt_no) & LLONG_MAX);
599 printk(KERN_DEBUG "\tlast node flag %llu\n",
600 (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
601 n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
602 printk(KERN_DEBUG "\t%d orphan inode numbers:\n", n);
603 for (i = 0; i < n; i++)
604 printk(KERN_DEBUG "\t ino %llu\n",
Alexander Beregalov7424bac2008-09-17 22:09:41 +0400605 (unsigned long long)le64_to_cpu(orph->inos[i]));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300606 break;
607 }
608 default:
609 printk(KERN_DEBUG "node type %d was not recognized\n",
610 (int)ch->node_type);
611 }
612 spin_unlock(&dbg_lock);
613}
614
615void dbg_dump_budget_req(const struct ubifs_budget_req *req)
616{
617 spin_lock(&dbg_lock);
618 printk(KERN_DEBUG "Budgeting request: new_ino %d, dirtied_ino %d\n",
619 req->new_ino, req->dirtied_ino);
620 printk(KERN_DEBUG "\tnew_ino_d %d, dirtied_ino_d %d\n",
621 req->new_ino_d, req->dirtied_ino_d);
622 printk(KERN_DEBUG "\tnew_page %d, dirtied_page %d\n",
623 req->new_page, req->dirtied_page);
624 printk(KERN_DEBUG "\tnew_dent %d, mod_dent %d\n",
625 req->new_dent, req->mod_dent);
626 printk(KERN_DEBUG "\tidx_growth %d\n", req->idx_growth);
627 printk(KERN_DEBUG "\tdata_growth %d dd_growth %d\n",
628 req->data_growth, req->dd_growth);
629 spin_unlock(&dbg_lock);
630}
631
632void dbg_dump_lstats(const struct ubifs_lp_stats *lst)
633{
634 spin_lock(&dbg_lock);
Artem Bityutskiy1de94152008-07-25 12:58:38 +0300635 printk(KERN_DEBUG "(pid %d) Lprops statistics: empty_lebs %d, "
636 "idx_lebs %d\n", current->pid, lst->empty_lebs, lst->idx_lebs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300637 printk(KERN_DEBUG "\ttaken_empty_lebs %d, total_free %lld, "
638 "total_dirty %lld\n", lst->taken_empty_lebs, lst->total_free,
639 lst->total_dirty);
640 printk(KERN_DEBUG "\ttotal_used %lld, total_dark %lld, "
641 "total_dead %lld\n", lst->total_used, lst->total_dark,
642 lst->total_dead);
643 spin_unlock(&dbg_lock);
644}
645
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300646void dbg_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300647{
648 int i;
649 struct rb_node *rb;
650 struct ubifs_bud *bud;
651 struct ubifs_gced_idx_leb *idx_gc;
Artem Bityutskiy21a60252008-12-12 11:13:17 -0500652 long long available, outstanding, free;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300653
Artem Bityutskiy8ff83082011-03-29 18:19:50 +0300654 spin_lock(&c->space_lock);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300655 spin_lock(&dbg_lock);
Artem Bityutskiy8c3067e2011-03-30 13:18:58 +0300656 printk(KERN_DEBUG "(pid %d) Budgeting info: data budget sum %lld, "
657 "total budget sum %lld\n", current->pid,
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300658 bi->data_growth + bi->dd_growth,
659 bi->data_growth + bi->dd_growth + bi->idx_growth);
Artem Bityutskiy8c3067e2011-03-30 13:18:58 +0300660 printk(KERN_DEBUG "\tbudg_data_growth %lld, budg_dd_growth %lld, "
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300661 "budg_idx_growth %lld\n", bi->data_growth, bi->dd_growth,
662 bi->idx_growth);
Artem Bityutskiy8c3067e2011-03-30 13:18:58 +0300663 printk(KERN_DEBUG "\tmin_idx_lebs %d, old_idx_sz %llu, "
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300664 "uncommitted_idx %lld\n", bi->min_idx_lebs, bi->old_idx_sz,
665 bi->uncommitted_idx);
Artem Bityutskiy8c3067e2011-03-30 13:18:58 +0300666 printk(KERN_DEBUG "\tpage_budget %d, inode_budget %d, dent_budget %d\n",
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300667 bi->page_budget, bi->inode_budget, bi->dent_budget);
Artem Bityutskiy8c3067e2011-03-30 13:18:58 +0300668 printk(KERN_DEBUG "\tnospace %u, nospace_rp %u\n",
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300669 bi->nospace, bi->nospace_rp);
Artem Bityutskiy8c3067e2011-03-30 13:18:58 +0300670 printk(KERN_DEBUG "\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
671 c->dark_wm, c->dead_wm, c->max_idx_node_sz);
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300672
673 if (bi != &c->bi)
674 /*
675 * If we are dumping saved budgeting data, do not print
676 * additional information which is about the current state, not
677 * the old one which corresponded to the saved budgeting data.
678 */
679 goto out_unlock;
680
Artem Bityutskiy8c3067e2011-03-30 13:18:58 +0300681 printk(KERN_DEBUG "\tfreeable_cnt %d, calc_idx_sz %lld, idx_gc_cnt %d\n",
682 c->freeable_cnt, c->calc_idx_sz, c->idx_gc_cnt);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300683 printk(KERN_DEBUG "\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, "
684 "clean_zn_cnt %ld\n", atomic_long_read(&c->dirty_pg_cnt),
685 atomic_long_read(&c->dirty_zn_cnt),
686 atomic_long_read(&c->clean_zn_cnt));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300687 printk(KERN_DEBUG "\tgc_lnum %d, ihead_lnum %d\n",
688 c->gc_lnum, c->ihead_lnum);
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300689
Artem Bityutskiy84abf972009-01-23 14:54:59 +0200690 /* If we are in R/O mode, journal heads do not exist */
691 if (c->jheads)
692 for (i = 0; i < c->jhead_cnt; i++)
Artem Bityutskiy77a7ae52009-09-15 15:03:51 +0300693 printk(KERN_DEBUG "\tjhead %s\t LEB %d\n",
694 dbg_jhead(c->jheads[i].wbuf.jhead),
695 c->jheads[i].wbuf.lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300696 for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
697 bud = rb_entry(rb, struct ubifs_bud, rb);
698 printk(KERN_DEBUG "\tbud LEB %d\n", bud->lnum);
699 }
700 list_for_each_entry(bud, &c->old_buds, list)
701 printk(KERN_DEBUG "\told bud LEB %d\n", bud->lnum);
702 list_for_each_entry(idx_gc, &c->idx_gc, list)
703 printk(KERN_DEBUG "\tGC'ed idx LEB %d unmap %d\n",
704 idx_gc->lnum, idx_gc->unmap);
705 printk(KERN_DEBUG "\tcommit state %d\n", c->cmt_state);
Artem Bityutskiy21a60252008-12-12 11:13:17 -0500706
707 /* Print budgeting predictions */
Artem Bityutskiyb1375452011-03-29 18:04:05 +0300708 available = ubifs_calc_available(c, c->bi.min_idx_lebs);
709 outstanding = c->bi.data_growth + c->bi.dd_growth;
Artem Bityutskiy84abf972009-01-23 14:54:59 +0200710 free = ubifs_get_free_space_nolock(c);
Artem Bityutskiy21a60252008-12-12 11:13:17 -0500711 printk(KERN_DEBUG "Budgeting predictions:\n");
712 printk(KERN_DEBUG "\tavailable: %lld, outstanding %lld, free %lld\n",
713 available, outstanding, free);
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +0300714out_unlock:
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300715 spin_unlock(&dbg_lock);
Artem Bityutskiy8ff83082011-03-29 18:19:50 +0300716 spin_unlock(&c->space_lock);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300717}
718
719void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
720{
Artem Bityutskiybe9e62a2008-12-28 10:17:23 +0200721 int i, spc, dark = 0, dead = 0;
722 struct rb_node *rb;
723 struct ubifs_bud *bud;
724
725 spc = lp->free + lp->dirty;
726 if (spc < c->dead_wm)
727 dead = spc;
728 else
729 dark = ubifs_calc_dark(c, spc);
730
731 if (lp->flags & LPROPS_INDEX)
732 printk(KERN_DEBUG "LEB %-7d free %-8d dirty %-8d used %-8d "
733 "free + dirty %-8d flags %#x (", lp->lnum, lp->free,
734 lp->dirty, c->leb_size - spc, spc, lp->flags);
735 else
736 printk(KERN_DEBUG "LEB %-7d free %-8d dirty %-8d used %-8d "
737 "free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d "
738 "flags %#-4x (", lp->lnum, lp->free, lp->dirty,
739 c->leb_size - spc, spc, dark, dead,
740 (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags);
741
742 if (lp->flags & LPROPS_TAKEN) {
743 if (lp->flags & LPROPS_INDEX)
744 printk(KERN_CONT "index, taken");
745 else
746 printk(KERN_CONT "taken");
747 } else {
748 const char *s;
749
750 if (lp->flags & LPROPS_INDEX) {
751 switch (lp->flags & LPROPS_CAT_MASK) {
752 case LPROPS_DIRTY_IDX:
753 s = "dirty index";
754 break;
755 case LPROPS_FRDI_IDX:
756 s = "freeable index";
757 break;
758 default:
759 s = "index";
760 }
761 } else {
762 switch (lp->flags & LPROPS_CAT_MASK) {
763 case LPROPS_UNCAT:
764 s = "not categorized";
765 break;
766 case LPROPS_DIRTY:
767 s = "dirty";
768 break;
769 case LPROPS_FREE:
770 s = "free";
771 break;
772 case LPROPS_EMPTY:
773 s = "empty";
774 break;
775 case LPROPS_FREEABLE:
776 s = "freeable";
777 break;
778 default:
779 s = NULL;
780 break;
781 }
782 }
783 printk(KERN_CONT "%s", s);
784 }
785
786 for (rb = rb_first((struct rb_root *)&c->buds); rb; rb = rb_next(rb)) {
787 bud = rb_entry(rb, struct ubifs_bud, rb);
788 if (bud->lnum == lp->lnum) {
789 int head = 0;
790 for (i = 0; i < c->jhead_cnt; i++) {
Artem Bityutskiy13216572011-04-24 10:53:17 +0300791 /*
792 * Note, if we are in R/O mode or in the middle
793 * of mounting/re-mounting, the write-buffers do
794 * not exist.
795 */
796 if (c->jheads &&
797 lp->lnum == c->jheads[i].wbuf.lnum) {
Artem Bityutskiybe9e62a2008-12-28 10:17:23 +0200798 printk(KERN_CONT ", jhead %s",
799 dbg_jhead(i));
800 head = 1;
801 }
802 }
803 if (!head)
804 printk(KERN_CONT ", bud of jhead %s",
805 dbg_jhead(bud->jhead));
806 }
807 }
808 if (lp->lnum == c->gc_lnum)
809 printk(KERN_CONT ", GC LEB");
810 printk(KERN_CONT ")\n");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300811}
812
813void dbg_dump_lprops(struct ubifs_info *c)
814{
815 int lnum, err;
816 struct ubifs_lprops lp;
817 struct ubifs_lp_stats lst;
818
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200819 printk(KERN_DEBUG "(pid %d) start dumping LEB properties\n",
820 current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300821 ubifs_get_lp_stats(c, &lst);
822 dbg_dump_lstats(&lst);
823
824 for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
825 err = ubifs_read_one_lp(c, lnum, &lp);
826 if (err)
827 ubifs_err("cannot read lprops for LEB %d", lnum);
828
829 dbg_dump_lprop(c, &lp);
830 }
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200831 printk(KERN_DEBUG "(pid %d) finish dumping LEB properties\n",
832 current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300833}
834
Adrian Hunter73944a62008-09-12 18:13:31 +0300835void dbg_dump_lpt_info(struct ubifs_info *c)
836{
837 int i;
838
839 spin_lock(&dbg_lock);
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200840 printk(KERN_DEBUG "(pid %d) dumping LPT information\n", current->pid);
Adrian Hunter73944a62008-09-12 18:13:31 +0300841 printk(KERN_DEBUG "\tlpt_sz: %lld\n", c->lpt_sz);
842 printk(KERN_DEBUG "\tpnode_sz: %d\n", c->pnode_sz);
843 printk(KERN_DEBUG "\tnnode_sz: %d\n", c->nnode_sz);
844 printk(KERN_DEBUG "\tltab_sz: %d\n", c->ltab_sz);
845 printk(KERN_DEBUG "\tlsave_sz: %d\n", c->lsave_sz);
846 printk(KERN_DEBUG "\tbig_lpt: %d\n", c->big_lpt);
847 printk(KERN_DEBUG "\tlpt_hght: %d\n", c->lpt_hght);
848 printk(KERN_DEBUG "\tpnode_cnt: %d\n", c->pnode_cnt);
849 printk(KERN_DEBUG "\tnnode_cnt: %d\n", c->nnode_cnt);
850 printk(KERN_DEBUG "\tdirty_pn_cnt: %d\n", c->dirty_pn_cnt);
851 printk(KERN_DEBUG "\tdirty_nn_cnt: %d\n", c->dirty_nn_cnt);
852 printk(KERN_DEBUG "\tlsave_cnt: %d\n", c->lsave_cnt);
853 printk(KERN_DEBUG "\tspace_bits: %d\n", c->space_bits);
854 printk(KERN_DEBUG "\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits);
855 printk(KERN_DEBUG "\tlpt_offs_bits: %d\n", c->lpt_offs_bits);
856 printk(KERN_DEBUG "\tlpt_spc_bits: %d\n", c->lpt_spc_bits);
857 printk(KERN_DEBUG "\tpcnt_bits: %d\n", c->pcnt_bits);
858 printk(KERN_DEBUG "\tlnum_bits: %d\n", c->lnum_bits);
859 printk(KERN_DEBUG "\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
860 printk(KERN_DEBUG "\tLPT head is at %d:%d\n",
861 c->nhead_lnum, c->nhead_offs);
Artem Bityutskiyf92b9822008-12-28 11:34:26 +0200862 printk(KERN_DEBUG "\tLPT ltab is at %d:%d\n",
863 c->ltab_lnum, c->ltab_offs);
Adrian Hunter73944a62008-09-12 18:13:31 +0300864 if (c->big_lpt)
865 printk(KERN_DEBUG "\tLPT lsave is at %d:%d\n",
866 c->lsave_lnum, c->lsave_offs);
867 for (i = 0; i < c->lpt_lebs; i++)
868 printk(KERN_DEBUG "\tLPT LEB %d free %d dirty %d tgc %d "
869 "cmt %d\n", i + c->lpt_first, c->ltab[i].free,
870 c->ltab[i].dirty, c->ltab[i].tgc, c->ltab[i].cmt);
871 spin_unlock(&dbg_lock);
872}
873
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300874void dbg_dump_leb(const struct ubifs_info *c, int lnum)
875{
876 struct ubifs_scan_leb *sleb;
877 struct ubifs_scan_node *snod;
Artem Bityutskiy73d9aec2011-03-11 15:39:09 +0200878 void *buf;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300879
Artem Bityutskiy2b1844a2011-06-03 08:31:29 +0300880 if (dbg_is_tst_rcvry(c))
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300881 return;
882
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200883 printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n",
884 current->pid, lnum);
Artem Bityutskiy73d9aec2011-03-11 15:39:09 +0200885
Artem Bityutskiyfc5e58c2011-03-24 16:14:26 +0200886 buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
Artem Bityutskiy73d9aec2011-03-11 15:39:09 +0200887 if (!buf) {
888 ubifs_err("cannot allocate memory for dumping LEB %d", lnum);
889 return;
890 }
891
892 sleb = ubifs_scan(c, lnum, 0, buf, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300893 if (IS_ERR(sleb)) {
894 ubifs_err("scan error %d", (int)PTR_ERR(sleb));
Artem Bityutskiy73d9aec2011-03-11 15:39:09 +0200895 goto out;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300896 }
897
898 printk(KERN_DEBUG "LEB %d has %d nodes ending at %d\n", lnum,
899 sleb->nodes_cnt, sleb->endpt);
900
901 list_for_each_entry(snod, &sleb->nodes, list) {
902 cond_resched();
903 printk(KERN_DEBUG "Dumping node at LEB %d:%d len %d\n", lnum,
904 snod->offs, snod->len);
905 dbg_dump_node(c, snod->node);
906 }
907
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200908 printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n",
909 current->pid, lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300910 ubifs_scan_destroy(sleb);
Artem Bityutskiy73d9aec2011-03-11 15:39:09 +0200911
912out:
913 vfree(buf);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300914 return;
915}
916
917void dbg_dump_znode(const struct ubifs_info *c,
918 const struct ubifs_znode *znode)
919{
920 int n;
921 const struct ubifs_zbranch *zbr;
922
923 spin_lock(&dbg_lock);
924 if (znode->parent)
925 zbr = &znode->parent->zbranch[znode->iip];
926 else
927 zbr = &c->zroot;
928
929 printk(KERN_DEBUG "znode %p, LEB %d:%d len %d parent %p iip %d level %d"
930 " child_cnt %d flags %lx\n", znode, zbr->lnum, zbr->offs,
931 zbr->len, znode->parent, znode->iip, znode->level,
932 znode->child_cnt, znode->flags);
933
934 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
935 spin_unlock(&dbg_lock);
936 return;
937 }
938
939 printk(KERN_DEBUG "zbranches:\n");
940 for (n = 0; n < znode->child_cnt; n++) {
941 zbr = &znode->zbranch[n];
942 if (znode->level > 0)
943 printk(KERN_DEBUG "\t%d: znode %p LEB %d:%d len %d key "
944 "%s\n", n, zbr->znode, zbr->lnum,
945 zbr->offs, zbr->len,
946 DBGKEY(&zbr->key));
947 else
948 printk(KERN_DEBUG "\t%d: LNC %p LEB %d:%d len %d key "
949 "%s\n", n, zbr->znode, zbr->lnum,
950 zbr->offs, zbr->len,
951 DBGKEY(&zbr->key));
952 }
953 spin_unlock(&dbg_lock);
954}
955
956void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
957{
958 int i;
959
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200960 printk(KERN_DEBUG "(pid %d) start dumping heap cat %d (%d elements)\n",
Artem Bityutskiy1de94152008-07-25 12:58:38 +0300961 current->pid, cat, heap->cnt);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300962 for (i = 0; i < heap->cnt; i++) {
963 struct ubifs_lprops *lprops = heap->arr[i];
964
965 printk(KERN_DEBUG "\t%d. LEB %d hpos %d free %d dirty %d "
966 "flags %d\n", i, lprops->lnum, lprops->hpos,
967 lprops->free, lprops->dirty, lprops->flags);
968 }
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200969 printk(KERN_DEBUG "(pid %d) finish dumping heap\n", current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300970}
971
972void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
973 struct ubifs_nnode *parent, int iip)
974{
975 int i;
976
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200977 printk(KERN_DEBUG "(pid %d) dumping pnode:\n", current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300978 printk(KERN_DEBUG "\taddress %zx parent %zx cnext %zx\n",
979 (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
980 printk(KERN_DEBUG "\tflags %lu iip %d level %d num %d\n",
981 pnode->flags, iip, pnode->level, pnode->num);
982 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
983 struct ubifs_lprops *lp = &pnode->lprops[i];
984
985 printk(KERN_DEBUG "\t%d: free %d dirty %d flags %d lnum %d\n",
986 i, lp->free, lp->dirty, lp->flags, lp->lnum);
987 }
988}
989
990void dbg_dump_tnc(struct ubifs_info *c)
991{
992 struct ubifs_znode *znode;
993 int level;
994
995 printk(KERN_DEBUG "\n");
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +0200996 printk(KERN_DEBUG "(pid %d) start dumping TNC tree\n", current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300997 znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
998 level = znode->level;
999 printk(KERN_DEBUG "== Level %d ==\n", level);
1000 while (znode) {
1001 if (level != znode->level) {
1002 level = znode->level;
1003 printk(KERN_DEBUG "== Level %d ==\n", level);
1004 }
1005 dbg_dump_znode(c, znode);
1006 znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
1007 }
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +02001008 printk(KERN_DEBUG "(pid %d) finish dumping TNC tree\n", current->pid);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001009}
1010
1011static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
1012 void *priv)
1013{
1014 dbg_dump_znode(c, znode);
1015 return 0;
1016}
1017
1018/**
1019 * dbg_dump_index - dump the on-flash index.
1020 * @c: UBIFS file-system description object
1021 *
1022 * This function dumps whole UBIFS indexing B-tree, unlike 'dbg_dump_tnc()'
1023 * which dumps only in-memory znodes and does not read znodes which from flash.
1024 */
1025void dbg_dump_index(struct ubifs_info *c)
1026{
1027 dbg_walk_index(c, NULL, dump_znode, NULL);
1028}
1029
1030/**
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001031 * dbg_save_space_info - save information about flash space.
1032 * @c: UBIFS file-system description object
1033 *
1034 * This function saves information about UBIFS free space, dirty space, etc, in
1035 * order to check it later.
1036 */
1037void dbg_save_space_info(struct ubifs_info *c)
1038{
1039 struct ubifs_debug_info *d = c->dbg;
Artem Bityutskiy7da64432011-04-04 17:16:39 +03001040 int freeable_cnt;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001041
1042 spin_lock(&c->space_lock);
Artem Bityutskiy7da64432011-04-04 17:16:39 +03001043 memcpy(&d->saved_lst, &c->lst, sizeof(struct ubifs_lp_stats));
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +03001044 memcpy(&d->saved_bi, &c->bi, sizeof(struct ubifs_budg_info));
1045 d->saved_idx_gc_cnt = c->idx_gc_cnt;
Artem Bityutskiy7da64432011-04-04 17:16:39 +03001046
1047 /*
1048 * We use a dirty hack here and zero out @c->freeable_cnt, because it
1049 * affects the free space calculations, and UBIFS might not know about
1050 * all freeable eraseblocks. Indeed, we know about freeable eraseblocks
1051 * only when we read their lprops, and we do this only lazily, upon the
1052 * need. So at any given point of time @c->freeable_cnt might be not
1053 * exactly accurate.
1054 *
1055 * Just one example about the issue we hit when we did not zero
1056 * @c->freeable_cnt.
1057 * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the
1058 * amount of free space in @d->saved_free
1059 * 2. We re-mount R/W, which makes UBIFS to read the "lsave"
1060 * information from flash, where we cache LEBs from various
1061 * categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()'
1062 * -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()'
1063 * -> 'ubifs_get_pnode()' -> 'update_cats()'
1064 * -> 'ubifs_add_to_cat()').
1065 * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt
1066 * becomes %1.
1067 * 4. We calculate the amount of free space when the re-mount is
1068 * finished in 'dbg_check_space_info()' and it does not match
1069 * @d->saved_free.
1070 */
1071 freeable_cnt = c->freeable_cnt;
1072 c->freeable_cnt = 0;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001073 d->saved_free = ubifs_get_free_space_nolock(c);
Artem Bityutskiy7da64432011-04-04 17:16:39 +03001074 c->freeable_cnt = freeable_cnt;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001075 spin_unlock(&c->space_lock);
1076}
1077
1078/**
1079 * dbg_check_space_info - check flash space information.
1080 * @c: UBIFS file-system description object
1081 *
1082 * This function compares current flash space information with the information
1083 * which was saved when the 'dbg_save_space_info()' function was called.
1084 * Returns zero if the information has not changed, and %-EINVAL it it has
1085 * changed.
1086 */
1087int dbg_check_space_info(struct ubifs_info *c)
1088{
1089 struct ubifs_debug_info *d = c->dbg;
1090 struct ubifs_lp_stats lst;
Artem Bityutskiy7da64432011-04-04 17:16:39 +03001091 long long free;
1092 int freeable_cnt;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001093
1094 spin_lock(&c->space_lock);
Artem Bityutskiy7da64432011-04-04 17:16:39 +03001095 freeable_cnt = c->freeable_cnt;
1096 c->freeable_cnt = 0;
1097 free = ubifs_get_free_space_nolock(c);
1098 c->freeable_cnt = freeable_cnt;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001099 spin_unlock(&c->space_lock);
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001100
1101 if (free != d->saved_free) {
1102 ubifs_err("free space changed from %lld to %lld",
1103 d->saved_free, free);
1104 goto out;
1105 }
1106
1107 return 0;
1108
1109out:
1110 ubifs_msg("saved lprops statistics dump");
1111 dbg_dump_lstats(&d->saved_lst);
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +03001112 ubifs_msg("saved budgeting info dump");
1113 dbg_dump_budg(c, &d->saved_bi);
1114 ubifs_msg("saved idx_gc_cnt %d", d->saved_idx_gc_cnt);
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001115 ubifs_msg("current lprops statistics dump");
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +03001116 ubifs_get_lp_stats(c, &lst);
Artem Bityutskiye055f7e2009-09-17 15:08:31 +03001117 dbg_dump_lstats(&lst);
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +03001118 ubifs_msg("current budgeting info dump");
1119 dbg_dump_budg(c, &c->bi);
Artem Bityutskiy84abf972009-01-23 14:54:59 +02001120 dump_stack();
1121 return -EINVAL;
1122}
1123
1124/**
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001125 * dbg_check_synced_i_size - check synchronized inode size.
Artem Bityutskiyd808efb2011-05-31 18:14:38 +03001126 * @c: UBIFS file-system description object
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001127 * @inode: inode to check
1128 *
1129 * If inode is clean, synchronized inode size has to be equivalent to current
1130 * inode size. This function has to be called only for locked inodes (@i_mutex
1131 * has to be locked). Returns %0 if synchronized inode size if correct, and
1132 * %-EINVAL if not.
1133 */
Artem Bityutskiyd808efb2011-05-31 18:14:38 +03001134int dbg_check_synced_i_size(const struct ubifs_info *c, struct inode *inode)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001135{
1136 int err = 0;
1137 struct ubifs_inode *ui = ubifs_inode(inode);
1138
Artem Bityutskiy2b1844a2011-06-03 08:31:29 +03001139 if (!dbg_is_chk_gen(c))
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001140 return 0;
1141 if (!S_ISREG(inode->i_mode))
1142 return 0;
1143
1144 mutex_lock(&ui->ui_mutex);
1145 spin_lock(&ui->ui_lock);
1146 if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
1147 ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode "
1148 "is clean", ui->ui_size, ui->synced_i_size);
1149 ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
1150 inode->i_mode, i_size_read(inode));
1151 dbg_dump_stack();
1152 err = -EINVAL;
1153 }
1154 spin_unlock(&ui->ui_lock);
1155 mutex_unlock(&ui->ui_mutex);
1156 return err;
1157}
1158
1159/*
1160 * dbg_check_dir - check directory inode size and link count.
1161 * @c: UBIFS file-system description object
1162 * @dir: the directory to calculate size for
1163 * @size: the result is returned here
1164 *
1165 * This function makes sure that directory size and link count are correct.
1166 * Returns zero in case of success and a negative error code in case of
1167 * failure.
1168 *
1169 * Note, it is good idea to make sure the @dir->i_mutex is locked before
1170 * calling this function.
1171 */
Artem Bityutskiy1b51e982011-05-25 17:38:29 +03001172int dbg_check_dir(struct ubifs_info *c, const struct inode *dir)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001173{
1174 unsigned int nlink = 2;
1175 union ubifs_key key;
1176 struct ubifs_dent_node *dent, *pdent = NULL;
1177 struct qstr nm = { .name = NULL };
1178 loff_t size = UBIFS_INO_NODE_SZ;
1179
Artem Bityutskiy2b1844a2011-06-03 08:31:29 +03001180 if (!dbg_is_chk_gen(c))
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001181 return 0;
1182
1183 if (!S_ISDIR(dir->i_mode))
1184 return 0;
1185
1186 lowest_dent_key(c, &key, dir->i_ino);
1187 while (1) {
1188 int err;
1189
1190 dent = ubifs_tnc_next_ent(c, &key, &nm);
1191 if (IS_ERR(dent)) {
1192 err = PTR_ERR(dent);
1193 if (err == -ENOENT)
1194 break;
1195 return err;
1196 }
1197
1198 nm.name = dent->name;
1199 nm.len = le16_to_cpu(dent->nlen);
1200 size += CALC_DENT_SIZE(nm.len);
1201 if (dent->type == UBIFS_ITYPE_DIR)
1202 nlink += 1;
1203 kfree(pdent);
1204 pdent = dent;
1205 key_read(c, &dent->key, &key);
1206 }
1207 kfree(pdent);
1208
1209 if (i_size_read(dir) != size) {
1210 ubifs_err("directory inode %lu has size %llu, "
1211 "but calculated size is %llu", dir->i_ino,
1212 (unsigned long long)i_size_read(dir),
1213 (unsigned long long)size);
Artem Bityutskiy4315fb42011-05-25 17:32:42 +03001214 dbg_dump_inode(c, dir);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001215 dump_stack();
1216 return -EINVAL;
1217 }
1218 if (dir->i_nlink != nlink) {
1219 ubifs_err("directory inode %lu has nlink %u, but calculated "
1220 "nlink is %u", dir->i_ino, dir->i_nlink, nlink);
Artem Bityutskiy4315fb42011-05-25 17:32:42 +03001221 dbg_dump_inode(c, dir);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001222 dump_stack();
1223 return -EINVAL;
1224 }
1225
1226 return 0;
1227}
1228
1229/**
1230 * dbg_check_key_order - make sure that colliding keys are properly ordered.
1231 * @c: UBIFS file-system description object
1232 * @zbr1: first zbranch
1233 * @zbr2: following zbranch
1234 *
1235 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
1236 * names of the direntries/xentries which are referred by the keys. This
1237 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
1238 * sure the name of direntry/xentry referred by @zbr1 is less than
1239 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
1240 * and a negative error code in case of failure.
1241 */
1242static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
1243 struct ubifs_zbranch *zbr2)
1244{
1245 int err, nlen1, nlen2, cmp;
1246 struct ubifs_dent_node *dent1, *dent2;
1247 union ubifs_key key;
1248
1249 ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
1250 dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1251 if (!dent1)
1252 return -ENOMEM;
1253 dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
1254 if (!dent2) {
1255 err = -ENOMEM;
1256 goto out_free;
1257 }
1258
1259 err = ubifs_tnc_read_node(c, zbr1, dent1);
1260 if (err)
1261 goto out_free;
1262 err = ubifs_validate_entry(c, dent1);
1263 if (err)
1264 goto out_free;
1265
1266 err = ubifs_tnc_read_node(c, zbr2, dent2);
1267 if (err)
1268 goto out_free;
1269 err = ubifs_validate_entry(c, dent2);
1270 if (err)
1271 goto out_free;
1272
1273 /* Make sure node keys are the same as in zbranch */
1274 err = 1;
1275 key_read(c, &dent1->key, &key);
1276 if (keys_cmp(c, &zbr1->key, &key)) {
Artem Bityutskiy5d38b3a2008-12-30 17:58:42 +02001277 dbg_err("1st entry at %d:%d has key %s", zbr1->lnum,
1278 zbr1->offs, DBGKEY(&key));
1279 dbg_err("but it should have key %s according to tnc",
1280 DBGKEY(&zbr1->key));
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +02001281 dbg_dump_node(c, dent1);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03001282 goto out_free;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001283 }
1284
1285 key_read(c, &dent2->key, &key);
1286 if (keys_cmp(c, &zbr2->key, &key)) {
Artem Bityutskiy5d38b3a2008-12-30 17:58:42 +02001287 dbg_err("2nd entry at %d:%d has key %s", zbr1->lnum,
1288 zbr1->offs, DBGKEY(&key));
1289 dbg_err("but it should have key %s according to tnc",
1290 DBGKEY(&zbr2->key));
Artem Bityutskiy2ba5f7a2008-10-31 17:32:30 +02001291 dbg_dump_node(c, dent2);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03001292 goto out_free;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001293 }
1294
1295 nlen1 = le16_to_cpu(dent1->nlen);
1296 nlen2 = le16_to_cpu(dent2->nlen);
1297
1298 cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
1299 if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
1300 err = 0;
1301 goto out_free;
1302 }
1303 if (cmp == 0 && nlen1 == nlen2)
Artem Bityutskiy5d38b3a2008-12-30 17:58:42 +02001304 dbg_err("2 xent/dent nodes with the same name");
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001305 else
Artem Bityutskiy5d38b3a2008-12-30 17:58:42 +02001306 dbg_err("bad order of colliding key %s",
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001307 DBGKEY(&key));
1308
Artem Bityutskiy552ff312008-10-23 11:49:28 +03001309 ubifs_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001310 dbg_dump_node(c, dent1);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03001311 ubifs_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001312 dbg_dump_node(c, dent2);
1313
1314out_free:
1315 kfree(dent2);
1316 kfree(dent1);
1317 return err;
1318}
1319
1320/**
1321 * dbg_check_znode - check if znode is all right.
1322 * @c: UBIFS file-system description object
1323 * @zbr: zbranch which points to this znode
1324 *
1325 * This function makes sure that znode referred to by @zbr is all right.
1326 * Returns zero if it is, and %-EINVAL if it is not.
1327 */
1328static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
1329{
1330 struct ubifs_znode *znode = zbr->znode;
1331 struct ubifs_znode *zp = znode->parent;
1332 int n, err, cmp;
1333
1334 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
1335 err = 1;
1336 goto out;
1337 }
1338 if (znode->level < 0) {
1339 err = 2;
1340 goto out;
1341 }
1342 if (znode->iip < 0 || znode->iip >= c->fanout) {
1343 err = 3;
1344 goto out;
1345 }
1346
1347 if (zbr->len == 0)
1348 /* Only dirty zbranch may have no on-flash nodes */
1349 if (!ubifs_zn_dirty(znode)) {
1350 err = 4;
1351 goto out;
1352 }
1353
1354 if (ubifs_zn_dirty(znode)) {
1355 /*
1356 * If znode is dirty, its parent has to be dirty as well. The
1357 * order of the operation is important, so we have to have
1358 * memory barriers.
1359 */
1360 smp_mb();
1361 if (zp && !ubifs_zn_dirty(zp)) {
1362 /*
1363 * The dirty flag is atomic and is cleared outside the
1364 * TNC mutex, so znode's dirty flag may now have
1365 * been cleared. The child is always cleared before the
1366 * parent, so we just need to check again.
1367 */
1368 smp_mb();
1369 if (ubifs_zn_dirty(znode)) {
1370 err = 5;
1371 goto out;
1372 }
1373 }
1374 }
1375
1376 if (zp) {
1377 const union ubifs_key *min, *max;
1378
1379 if (znode->level != zp->level - 1) {
1380 err = 6;
1381 goto out;
1382 }
1383
1384 /* Make sure the 'parent' pointer in our znode is correct */
1385 err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
1386 if (!err) {
1387 /* This zbranch does not exist in the parent */
1388 err = 7;
1389 goto out;
1390 }
1391
1392 if (znode->iip >= zp->child_cnt) {
1393 err = 8;
1394 goto out;
1395 }
1396
1397 if (znode->iip != n) {
1398 /* This may happen only in case of collisions */
1399 if (keys_cmp(c, &zp->zbranch[n].key,
1400 &zp->zbranch[znode->iip].key)) {
1401 err = 9;
1402 goto out;
1403 }
1404 n = znode->iip;
1405 }
1406
1407 /*
1408 * Make sure that the first key in our znode is greater than or
1409 * equal to the key in the pointing zbranch.
1410 */
1411 min = &zbr->key;
1412 cmp = keys_cmp(c, min, &znode->zbranch[0].key);
1413 if (cmp == 1) {
1414 err = 10;
1415 goto out;
1416 }
1417
1418 if (n + 1 < zp->child_cnt) {
1419 max = &zp->zbranch[n + 1].key;
1420
1421 /*
1422 * Make sure the last key in our znode is less or
Artem Bityutskiy7d4e9cc2009-03-20 19:11:12 +02001423 * equivalent than the key in the zbranch which goes
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001424 * after our pointing zbranch.
1425 */
1426 cmp = keys_cmp(c, max,
1427 &znode->zbranch[znode->child_cnt - 1].key);
1428 if (cmp == -1) {
1429 err = 11;
1430 goto out;
1431 }
1432 }
1433 } else {
1434 /* This may only be root znode */
1435 if (zbr != &c->zroot) {
1436 err = 12;
1437 goto out;
1438 }
1439 }
1440
1441 /*
1442 * Make sure that next key is greater or equivalent then the previous
1443 * one.
1444 */
1445 for (n = 1; n < znode->child_cnt; n++) {
1446 cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
1447 &znode->zbranch[n].key);
1448 if (cmp > 0) {
1449 err = 13;
1450 goto out;
1451 }
1452 if (cmp == 0) {
1453 /* This can only be keys with colliding hash */
1454 if (!is_hash_key(c, &znode->zbranch[n].key)) {
1455 err = 14;
1456 goto out;
1457 }
1458
1459 if (znode->level != 0 || c->replaying)
1460 continue;
1461
1462 /*
1463 * Colliding keys should follow binary order of
1464 * corresponding xentry/dentry names.
1465 */
1466 err = dbg_check_key_order(c, &znode->zbranch[n - 1],
1467 &znode->zbranch[n]);
1468 if (err < 0)
1469 return err;
1470 if (err) {
1471 err = 15;
1472 goto out;
1473 }
1474 }
1475 }
1476
1477 for (n = 0; n < znode->child_cnt; n++) {
1478 if (!znode->zbranch[n].znode &&
1479 (znode->zbranch[n].lnum == 0 ||
1480 znode->zbranch[n].len == 0)) {
1481 err = 16;
1482 goto out;
1483 }
1484
1485 if (znode->zbranch[n].lnum != 0 &&
1486 znode->zbranch[n].len == 0) {
1487 err = 17;
1488 goto out;
1489 }
1490
1491 if (znode->zbranch[n].lnum == 0 &&
1492 znode->zbranch[n].len != 0) {
1493 err = 18;
1494 goto out;
1495 }
1496
1497 if (znode->zbranch[n].lnum == 0 &&
1498 znode->zbranch[n].offs != 0) {
1499 err = 19;
1500 goto out;
1501 }
1502
1503 if (znode->level != 0 && znode->zbranch[n].znode)
1504 if (znode->zbranch[n].znode->parent != znode) {
1505 err = 20;
1506 goto out;
1507 }
1508 }
1509
1510 return 0;
1511
1512out:
1513 ubifs_err("failed, error %d", err);
1514 ubifs_msg("dump of the znode");
1515 dbg_dump_znode(c, znode);
1516 if (zp) {
1517 ubifs_msg("dump of the parent znode");
1518 dbg_dump_znode(c, zp);
1519 }
1520 dump_stack();
1521 return -EINVAL;
1522}
1523
1524/**
1525 * dbg_check_tnc - check TNC tree.
1526 * @c: UBIFS file-system description object
1527 * @extra: do extra checks that are possible at start commit
1528 *
1529 * This function traverses whole TNC tree and checks every znode. Returns zero
1530 * if everything is all right and %-EINVAL if something is wrong with TNC.
1531 */
1532int dbg_check_tnc(struct ubifs_info *c, int extra)
1533{
1534 struct ubifs_znode *znode;
1535 long clean_cnt = 0, dirty_cnt = 0;
1536 int err, last;
1537
Artem Bityutskiy8d7819b2011-06-03 08:53:35 +03001538 if (!dbg_is_chk_index(c))
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001539 return 0;
1540
1541 ubifs_assert(mutex_is_locked(&c->tnc_mutex));
1542 if (!c->zroot.znode)
1543 return 0;
1544
1545 znode = ubifs_tnc_postorder_first(c->zroot.znode);
1546 while (1) {
1547 struct ubifs_znode *prev;
1548 struct ubifs_zbranch *zbr;
1549
1550 if (!znode->parent)
1551 zbr = &c->zroot;
1552 else
1553 zbr = &znode->parent->zbranch[znode->iip];
1554
1555 err = dbg_check_znode(c, zbr);
1556 if (err)
1557 return err;
1558
1559 if (extra) {
1560 if (ubifs_zn_dirty(znode))
1561 dirty_cnt += 1;
1562 else
1563 clean_cnt += 1;
1564 }
1565
1566 prev = znode;
1567 znode = ubifs_tnc_postorder_next(znode);
1568 if (!znode)
1569 break;
1570
1571 /*
1572 * If the last key of this znode is equivalent to the first key
1573 * of the next znode (collision), then check order of the keys.
1574 */
1575 last = prev->child_cnt - 1;
1576 if (prev->level == 0 && znode->level == 0 && !c->replaying &&
1577 !keys_cmp(c, &prev->zbranch[last].key,
1578 &znode->zbranch[0].key)) {
1579 err = dbg_check_key_order(c, &prev->zbranch[last],
1580 &znode->zbranch[0]);
1581 if (err < 0)
1582 return err;
1583 if (err) {
1584 ubifs_msg("first znode");
1585 dbg_dump_znode(c, prev);
1586 ubifs_msg("second znode");
1587 dbg_dump_znode(c, znode);
1588 return -EINVAL;
1589 }
1590 }
1591 }
1592
1593 if (extra) {
1594 if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
1595 ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld",
1596 atomic_long_read(&c->clean_zn_cnt),
1597 clean_cnt);
1598 return -EINVAL;
1599 }
1600 if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
1601 ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld",
1602 atomic_long_read(&c->dirty_zn_cnt),
1603 dirty_cnt);
1604 return -EINVAL;
1605 }
1606 }
1607
1608 return 0;
1609}
1610
1611/**
1612 * dbg_walk_index - walk the on-flash index.
1613 * @c: UBIFS file-system description object
1614 * @leaf_cb: called for each leaf node
1615 * @znode_cb: called for each indexing node
Adrian Hunter227c75c2009-01-29 11:53:51 +02001616 * @priv: private data which is passed to callbacks
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001617 *
1618 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1619 * node and @znode_cb for each indexing node. Returns zero in case of success
1620 * and a negative error code in case of failure.
1621 *
1622 * It would be better if this function removed every znode it pulled to into
1623 * the TNC, so that the behavior more closely matched the non-debugging
1624 * behavior.
1625 */
1626int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1627 dbg_znode_callback znode_cb, void *priv)
1628{
1629 int err;
1630 struct ubifs_zbranch *zbr;
1631 struct ubifs_znode *znode, *child;
1632
1633 mutex_lock(&c->tnc_mutex);
1634 /* If the root indexing node is not in TNC - pull it */
1635 if (!c->zroot.znode) {
1636 c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1637 if (IS_ERR(c->zroot.znode)) {
1638 err = PTR_ERR(c->zroot.znode);
1639 c->zroot.znode = NULL;
1640 goto out_unlock;
1641 }
1642 }
1643
1644 /*
1645 * We are going to traverse the indexing tree in the postorder manner.
1646 * Go down and find the leftmost indexing node where we are going to
1647 * start from.
1648 */
1649 znode = c->zroot.znode;
1650 while (znode->level > 0) {
1651 zbr = &znode->zbranch[0];
1652 child = zbr->znode;
1653 if (!child) {
1654 child = ubifs_load_znode(c, zbr, znode, 0);
1655 if (IS_ERR(child)) {
1656 err = PTR_ERR(child);
1657 goto out_unlock;
1658 }
1659 zbr->znode = child;
1660 }
1661
1662 znode = child;
1663 }
1664
1665 /* Iterate over all indexing nodes */
1666 while (1) {
1667 int idx;
1668
1669 cond_resched();
1670
1671 if (znode_cb) {
1672 err = znode_cb(c, znode, priv);
1673 if (err) {
1674 ubifs_err("znode checking function returned "
1675 "error %d", err);
1676 dbg_dump_znode(c, znode);
1677 goto out_dump;
1678 }
1679 }
1680 if (leaf_cb && znode->level == 0) {
1681 for (idx = 0; idx < znode->child_cnt; idx++) {
1682 zbr = &znode->zbranch[idx];
1683 err = leaf_cb(c, zbr, priv);
1684 if (err) {
1685 ubifs_err("leaf checking function "
1686 "returned error %d, for leaf "
1687 "at LEB %d:%d",
1688 err, zbr->lnum, zbr->offs);
1689 goto out_dump;
1690 }
1691 }
1692 }
1693
1694 if (!znode->parent)
1695 break;
1696
1697 idx = znode->iip + 1;
1698 znode = znode->parent;
1699 if (idx < znode->child_cnt) {
1700 /* Switch to the next index in the parent */
1701 zbr = &znode->zbranch[idx];
1702 child = zbr->znode;
1703 if (!child) {
1704 child = ubifs_load_znode(c, zbr, znode, idx);
1705 if (IS_ERR(child)) {
1706 err = PTR_ERR(child);
1707 goto out_unlock;
1708 }
1709 zbr->znode = child;
1710 }
1711 znode = child;
1712 } else
1713 /*
1714 * This is the last child, switch to the parent and
1715 * continue.
1716 */
1717 continue;
1718
1719 /* Go to the lowest leftmost znode in the new sub-tree */
1720 while (znode->level > 0) {
1721 zbr = &znode->zbranch[0];
1722 child = zbr->znode;
1723 if (!child) {
1724 child = ubifs_load_znode(c, zbr, znode, 0);
1725 if (IS_ERR(child)) {
1726 err = PTR_ERR(child);
1727 goto out_unlock;
1728 }
1729 zbr->znode = child;
1730 }
1731 znode = child;
1732 }
1733 }
1734
1735 mutex_unlock(&c->tnc_mutex);
1736 return 0;
1737
1738out_dump:
1739 if (znode->parent)
1740 zbr = &znode->parent->zbranch[znode->iip];
1741 else
1742 zbr = &c->zroot;
1743 ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1744 dbg_dump_znode(c, znode);
1745out_unlock:
1746 mutex_unlock(&c->tnc_mutex);
1747 return err;
1748}
1749
1750/**
1751 * add_size - add znode size to partially calculated index size.
1752 * @c: UBIFS file-system description object
1753 * @znode: znode to add size for
1754 * @priv: partially calculated index size
1755 *
1756 * This is a helper function for 'dbg_check_idx_size()' which is called for
1757 * every indexing node and adds its size to the 'long long' variable pointed to
1758 * by @priv.
1759 */
1760static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1761{
1762 long long *idx_size = priv;
1763 int add;
1764
1765 add = ubifs_idx_node_sz(c, znode->child_cnt);
1766 add = ALIGN(add, 8);
1767 *idx_size += add;
1768 return 0;
1769}
1770
1771/**
1772 * dbg_check_idx_size - check index size.
1773 * @c: UBIFS file-system description object
1774 * @idx_size: size to check
1775 *
1776 * This function walks the UBIFS index, calculates its size and checks that the
1777 * size is equivalent to @idx_size. Returns zero in case of success and a
1778 * negative error code in case of failure.
1779 */
1780int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
1781{
1782 int err;
1783 long long calc = 0;
1784
Artem Bityutskiy8d7819b2011-06-03 08:53:35 +03001785 if (!dbg_is_chk_index(c))
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001786 return 0;
1787
1788 err = dbg_walk_index(c, NULL, add_size, &calc);
1789 if (err) {
1790 ubifs_err("error %d while walking the index", err);
1791 return err;
1792 }
1793
1794 if (calc != idx_size) {
1795 ubifs_err("index size check failed: calculated size is %lld, "
1796 "should be %lld", calc, idx_size);
1797 dump_stack();
1798 return -EINVAL;
1799 }
1800
1801 return 0;
1802}
1803
1804/**
1805 * struct fsck_inode - information about an inode used when checking the file-system.
1806 * @rb: link in the RB-tree of inodes
1807 * @inum: inode number
1808 * @mode: inode type, permissions, etc
1809 * @nlink: inode link count
1810 * @xattr_cnt: count of extended attributes
1811 * @references: how many directory/xattr entries refer this inode (calculated
1812 * while walking the index)
1813 * @calc_cnt: for directory inode count of child directories
1814 * @size: inode size (read from on-flash inode)
1815 * @xattr_sz: summary size of all extended attributes (read from on-flash
1816 * inode)
1817 * @calc_sz: for directories calculated directory size
1818 * @calc_xcnt: count of extended attributes
1819 * @calc_xsz: calculated summary size of all extended attributes
1820 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1821 * inode (read from on-flash inode)
1822 * @calc_xnms: calculated sum of lengths of all extended attribute names
1823 */
1824struct fsck_inode {
1825 struct rb_node rb;
1826 ino_t inum;
1827 umode_t mode;
1828 unsigned int nlink;
1829 unsigned int xattr_cnt;
1830 int references;
1831 int calc_cnt;
1832 long long size;
1833 unsigned int xattr_sz;
1834 long long calc_sz;
1835 long long calc_xcnt;
1836 long long calc_xsz;
1837 unsigned int xattr_nms;
1838 long long calc_xnms;
1839};
1840
1841/**
1842 * struct fsck_data - private FS checking information.
1843 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1844 */
1845struct fsck_data {
1846 struct rb_root inodes;
1847};
1848
1849/**
1850 * add_inode - add inode information to RB-tree of inodes.
1851 * @c: UBIFS file-system description object
1852 * @fsckd: FS checking information
1853 * @ino: raw UBIFS inode to add
1854 *
1855 * This is a helper function for 'check_leaf()' which adds information about
1856 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1857 * case of success and a negative error code in case of failure.
1858 */
1859static struct fsck_inode *add_inode(struct ubifs_info *c,
1860 struct fsck_data *fsckd,
1861 struct ubifs_ino_node *ino)
1862{
1863 struct rb_node **p, *parent = NULL;
1864 struct fsck_inode *fscki;
1865 ino_t inum = key_inum_flash(c, &ino->key);
Artem Bityutskiy45cd5cd2011-05-02 22:34:39 +03001866 struct inode *inode;
1867 struct ubifs_inode *ui;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001868
1869 p = &fsckd->inodes.rb_node;
1870 while (*p) {
1871 parent = *p;
1872 fscki = rb_entry(parent, struct fsck_inode, rb);
1873 if (inum < fscki->inum)
1874 p = &(*p)->rb_left;
1875 else if (inum > fscki->inum)
1876 p = &(*p)->rb_right;
1877 else
1878 return fscki;
1879 }
1880
1881 if (inum > c->highest_inum) {
1882 ubifs_err("too high inode number, max. is %lu",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001883 (unsigned long)c->highest_inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001884 return ERR_PTR(-EINVAL);
1885 }
1886
1887 fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
1888 if (!fscki)
1889 return ERR_PTR(-ENOMEM);
1890
Artem Bityutskiy45cd5cd2011-05-02 22:34:39 +03001891 inode = ilookup(c->vfs_sb, inum);
1892
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001893 fscki->inum = inum;
Artem Bityutskiy45cd5cd2011-05-02 22:34:39 +03001894 /*
1895 * If the inode is present in the VFS inode cache, use it instead of
1896 * the on-flash inode which might be out-of-date. E.g., the size might
1897 * be out-of-date. If we do not do this, the following may happen, for
1898 * example:
1899 * 1. A power cut happens
1900 * 2. We mount the file-system R/O, the replay process fixes up the
1901 * inode size in the VFS cache, but on on-flash.
1902 * 3. 'check_leaf()' fails because it hits a data node beyond inode
1903 * size.
1904 */
1905 if (!inode) {
1906 fscki->nlink = le32_to_cpu(ino->nlink);
1907 fscki->size = le64_to_cpu(ino->size);
1908 fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
1909 fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
1910 fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
1911 fscki->mode = le32_to_cpu(ino->mode);
1912 } else {
1913 ui = ubifs_inode(inode);
1914 fscki->nlink = inode->i_nlink;
1915 fscki->size = inode->i_size;
1916 fscki->xattr_cnt = ui->xattr_cnt;
1917 fscki->xattr_sz = ui->xattr_size;
1918 fscki->xattr_nms = ui->xattr_names;
1919 fscki->mode = inode->i_mode;
1920 iput(inode);
1921 }
1922
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001923 if (S_ISDIR(fscki->mode)) {
1924 fscki->calc_sz = UBIFS_INO_NODE_SZ;
1925 fscki->calc_cnt = 2;
1926 }
Artem Bityutskiy45cd5cd2011-05-02 22:34:39 +03001927
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001928 rb_link_node(&fscki->rb, parent, p);
1929 rb_insert_color(&fscki->rb, &fsckd->inodes);
Artem Bityutskiy45cd5cd2011-05-02 22:34:39 +03001930
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001931 return fscki;
1932}
1933
1934/**
1935 * search_inode - search inode in the RB-tree of inodes.
1936 * @fsckd: FS checking information
1937 * @inum: inode number to search
1938 *
1939 * This is a helper function for 'check_leaf()' which searches inode @inum in
1940 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1941 * the inode was not found.
1942 */
1943static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
1944{
1945 struct rb_node *p;
1946 struct fsck_inode *fscki;
1947
1948 p = fsckd->inodes.rb_node;
1949 while (p) {
1950 fscki = rb_entry(p, struct fsck_inode, rb);
1951 if (inum < fscki->inum)
1952 p = p->rb_left;
1953 else if (inum > fscki->inum)
1954 p = p->rb_right;
1955 else
1956 return fscki;
1957 }
1958 return NULL;
1959}
1960
1961/**
1962 * read_add_inode - read inode node and add it to RB-tree of inodes.
1963 * @c: UBIFS file-system description object
1964 * @fsckd: FS checking information
1965 * @inum: inode number to read
1966 *
1967 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1968 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1969 * information pointer in case of success and a negative error code in case of
1970 * failure.
1971 */
1972static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1973 struct fsck_data *fsckd, ino_t inum)
1974{
1975 int n, err;
1976 union ubifs_key key;
1977 struct ubifs_znode *znode;
1978 struct ubifs_zbranch *zbr;
1979 struct ubifs_ino_node *ino;
1980 struct fsck_inode *fscki;
1981
1982 fscki = search_inode(fsckd, inum);
1983 if (fscki)
1984 return fscki;
1985
1986 ino_key_init(c, &key, inum);
1987 err = ubifs_lookup_level0(c, &key, &znode, &n);
1988 if (!err) {
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001989 ubifs_err("inode %lu not found in index", (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001990 return ERR_PTR(-ENOENT);
1991 } else if (err < 0) {
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001992 ubifs_err("error %d while looking up inode %lu",
1993 err, (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001994 return ERR_PTR(err);
1995 }
1996
1997 zbr = &znode->zbranch[n];
1998 if (zbr->len < UBIFS_INO_NODE_SZ) {
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001999 ubifs_err("bad node %lu node length %d",
2000 (unsigned long)inum, zbr->len);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002001 return ERR_PTR(-EINVAL);
2002 }
2003
2004 ino = kmalloc(zbr->len, GFP_NOFS);
2005 if (!ino)
2006 return ERR_PTR(-ENOMEM);
2007
2008 err = ubifs_tnc_read_node(c, zbr, ino);
2009 if (err) {
2010 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2011 zbr->lnum, zbr->offs, err);
2012 kfree(ino);
2013 return ERR_PTR(err);
2014 }
2015
2016 fscki = add_inode(c, fsckd, ino);
2017 kfree(ino);
2018 if (IS_ERR(fscki)) {
2019 ubifs_err("error %ld while adding inode %lu node",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002020 PTR_ERR(fscki), (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002021 return fscki;
2022 }
2023
2024 return fscki;
2025}
2026
2027/**
2028 * check_leaf - check leaf node.
2029 * @c: UBIFS file-system description object
2030 * @zbr: zbranch of the leaf node to check
2031 * @priv: FS checking information
2032 *
2033 * This is a helper function for 'dbg_check_filesystem()' which is called for
2034 * every single leaf node while walking the indexing tree. It checks that the
2035 * leaf node referred from the indexing tree exists, has correct CRC, and does
2036 * some other basic validation. This function is also responsible for building
2037 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
2038 * calculates reference count, size, etc for each inode in order to later
2039 * compare them to the information stored inside the inodes and detect possible
2040 * inconsistencies. Returns zero in case of success and a negative error code
2041 * in case of failure.
2042 */
2043static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
2044 void *priv)
2045{
2046 ino_t inum;
2047 void *node;
2048 struct ubifs_ch *ch;
2049 int err, type = key_type(c, &zbr->key);
2050 struct fsck_inode *fscki;
2051
2052 if (zbr->len < UBIFS_CH_SZ) {
2053 ubifs_err("bad leaf length %d (LEB %d:%d)",
2054 zbr->len, zbr->lnum, zbr->offs);
2055 return -EINVAL;
2056 }
2057
2058 node = kmalloc(zbr->len, GFP_NOFS);
2059 if (!node)
2060 return -ENOMEM;
2061
2062 err = ubifs_tnc_read_node(c, zbr, node);
2063 if (err) {
2064 ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
2065 zbr->lnum, zbr->offs, err);
2066 goto out_free;
2067 }
2068
2069 /* If this is an inode node, add it to RB-tree of inodes */
2070 if (type == UBIFS_INO_KEY) {
2071 fscki = add_inode(c, priv, node);
2072 if (IS_ERR(fscki)) {
2073 err = PTR_ERR(fscki);
2074 ubifs_err("error %d while adding inode node", err);
2075 goto out_dump;
2076 }
2077 goto out;
2078 }
2079
2080 if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
2081 type != UBIFS_DATA_KEY) {
2082 ubifs_err("unexpected node type %d at LEB %d:%d",
2083 type, zbr->lnum, zbr->offs);
2084 err = -EINVAL;
2085 goto out_free;
2086 }
2087
2088 ch = node;
2089 if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
2090 ubifs_err("too high sequence number, max. is %llu",
2091 c->max_sqnum);
2092 err = -EINVAL;
2093 goto out_dump;
2094 }
2095
2096 if (type == UBIFS_DATA_KEY) {
2097 long long blk_offs;
2098 struct ubifs_data_node *dn = node;
2099
2100 /*
2101 * Search the inode node this data node belongs to and insert
2102 * it to the RB-tree of inodes.
2103 */
2104 inum = key_inum_flash(c, &dn->key);
2105 fscki = read_add_inode(c, priv, inum);
2106 if (IS_ERR(fscki)) {
2107 err = PTR_ERR(fscki);
2108 ubifs_err("error %d while processing data node and "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002109 "trying to find inode node %lu",
2110 err, (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002111 goto out_dump;
2112 }
2113
2114 /* Make sure the data node is within inode size */
2115 blk_offs = key_block_flash(c, &dn->key);
2116 blk_offs <<= UBIFS_BLOCK_SHIFT;
2117 blk_offs += le32_to_cpu(dn->size);
2118 if (blk_offs > fscki->size) {
2119 ubifs_err("data node at LEB %d:%d is not within inode "
2120 "size %lld", zbr->lnum, zbr->offs,
2121 fscki->size);
2122 err = -EINVAL;
2123 goto out_dump;
2124 }
2125 } else {
2126 int nlen;
2127 struct ubifs_dent_node *dent = node;
2128 struct fsck_inode *fscki1;
2129
2130 err = ubifs_validate_entry(c, dent);
2131 if (err)
2132 goto out_dump;
2133
2134 /*
2135 * Search the inode node this entry refers to and the parent
2136 * inode node and insert them to the RB-tree of inodes.
2137 */
2138 inum = le64_to_cpu(dent->inum);
2139 fscki = read_add_inode(c, priv, inum);
2140 if (IS_ERR(fscki)) {
2141 err = PTR_ERR(fscki);
2142 ubifs_err("error %d while processing entry node and "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002143 "trying to find inode node %lu",
2144 err, (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002145 goto out_dump;
2146 }
2147
2148 /* Count how many direntries or xentries refers this inode */
2149 fscki->references += 1;
2150
2151 inum = key_inum_flash(c, &dent->key);
2152 fscki1 = read_add_inode(c, priv, inum);
2153 if (IS_ERR(fscki1)) {
Roel Kluinb38882f2009-12-07 14:21:45 +01002154 err = PTR_ERR(fscki1);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002155 ubifs_err("error %d while processing entry node and "
2156 "trying to find parent inode node %lu",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002157 err, (unsigned long)inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002158 goto out_dump;
2159 }
2160
2161 nlen = le16_to_cpu(dent->nlen);
2162 if (type == UBIFS_XENT_KEY) {
2163 fscki1->calc_xcnt += 1;
2164 fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
2165 fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
2166 fscki1->calc_xnms += nlen;
2167 } else {
2168 fscki1->calc_sz += CALC_DENT_SIZE(nlen);
2169 if (dent->type == UBIFS_ITYPE_DIR)
2170 fscki1->calc_cnt += 1;
2171 }
2172 }
2173
2174out:
2175 kfree(node);
2176 return 0;
2177
2178out_dump:
2179 ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
2180 dbg_dump_node(c, node);
2181out_free:
2182 kfree(node);
2183 return err;
2184}
2185
2186/**
2187 * free_inodes - free RB-tree of inodes.
2188 * @fsckd: FS checking information
2189 */
2190static void free_inodes(struct fsck_data *fsckd)
2191{
2192 struct rb_node *this = fsckd->inodes.rb_node;
2193 struct fsck_inode *fscki;
2194
2195 while (this) {
2196 if (this->rb_left)
2197 this = this->rb_left;
2198 else if (this->rb_right)
2199 this = this->rb_right;
2200 else {
2201 fscki = rb_entry(this, struct fsck_inode, rb);
2202 this = rb_parent(this);
2203 if (this) {
2204 if (this->rb_left == &fscki->rb)
2205 this->rb_left = NULL;
2206 else
2207 this->rb_right = NULL;
2208 }
2209 kfree(fscki);
2210 }
2211 }
2212}
2213
2214/**
2215 * check_inodes - checks all inodes.
2216 * @c: UBIFS file-system description object
2217 * @fsckd: FS checking information
2218 *
2219 * This is a helper function for 'dbg_check_filesystem()' which walks the
2220 * RB-tree of inodes after the index scan has been finished, and checks that
2221 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
2222 * %-EINVAL if not, and a negative error code in case of failure.
2223 */
2224static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
2225{
2226 int n, err;
2227 union ubifs_key key;
2228 struct ubifs_znode *znode;
2229 struct ubifs_zbranch *zbr;
2230 struct ubifs_ino_node *ino;
2231 struct fsck_inode *fscki;
2232 struct rb_node *this = rb_first(&fsckd->inodes);
2233
2234 while (this) {
2235 fscki = rb_entry(this, struct fsck_inode, rb);
2236 this = rb_next(this);
2237
2238 if (S_ISDIR(fscki->mode)) {
2239 /*
2240 * Directories have to have exactly one reference (they
2241 * cannot have hardlinks), although root inode is an
2242 * exception.
2243 */
2244 if (fscki->inum != UBIFS_ROOT_INO &&
2245 fscki->references != 1) {
2246 ubifs_err("directory inode %lu has %d "
2247 "direntries which refer it, but "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002248 "should be 1",
2249 (unsigned long)fscki->inum,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002250 fscki->references);
2251 goto out_dump;
2252 }
2253 if (fscki->inum == UBIFS_ROOT_INO &&
2254 fscki->references != 0) {
2255 ubifs_err("root inode %lu has non-zero (%d) "
2256 "direntries which refer it",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002257 (unsigned long)fscki->inum,
2258 fscki->references);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002259 goto out_dump;
2260 }
2261 if (fscki->calc_sz != fscki->size) {
2262 ubifs_err("directory inode %lu size is %lld, "
2263 "but calculated size is %lld",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002264 (unsigned long)fscki->inum,
2265 fscki->size, fscki->calc_sz);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002266 goto out_dump;
2267 }
2268 if (fscki->calc_cnt != fscki->nlink) {
2269 ubifs_err("directory inode %lu nlink is %d, "
2270 "but calculated nlink is %d",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002271 (unsigned long)fscki->inum,
2272 fscki->nlink, fscki->calc_cnt);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002273 goto out_dump;
2274 }
2275 } else {
2276 if (fscki->references != fscki->nlink) {
2277 ubifs_err("inode %lu nlink is %d, but "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002278 "calculated nlink is %d",
2279 (unsigned long)fscki->inum,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002280 fscki->nlink, fscki->references);
2281 goto out_dump;
2282 }
2283 }
2284 if (fscki->xattr_sz != fscki->calc_xsz) {
2285 ubifs_err("inode %lu has xattr size %u, but "
2286 "calculated size is %lld",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002287 (unsigned long)fscki->inum, fscki->xattr_sz,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002288 fscki->calc_xsz);
2289 goto out_dump;
2290 }
2291 if (fscki->xattr_cnt != fscki->calc_xcnt) {
2292 ubifs_err("inode %lu has %u xattrs, but "
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002293 "calculated count is %lld",
2294 (unsigned long)fscki->inum,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002295 fscki->xattr_cnt, fscki->calc_xcnt);
2296 goto out_dump;
2297 }
2298 if (fscki->xattr_nms != fscki->calc_xnms) {
2299 ubifs_err("inode %lu has xattr names' size %u, but "
2300 "calculated names' size is %lld",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002301 (unsigned long)fscki->inum, fscki->xattr_nms,
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002302 fscki->calc_xnms);
2303 goto out_dump;
2304 }
2305 }
2306
2307 return 0;
2308
2309out_dump:
2310 /* Read the bad inode and dump it */
2311 ino_key_init(c, &key, fscki->inum);
2312 err = ubifs_lookup_level0(c, &key, &znode, &n);
2313 if (!err) {
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002314 ubifs_err("inode %lu not found in index",
2315 (unsigned long)fscki->inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002316 return -ENOENT;
2317 } else if (err < 0) {
2318 ubifs_err("error %d while looking up inode %lu",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002319 err, (unsigned long)fscki->inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002320 return err;
2321 }
2322
2323 zbr = &znode->zbranch[n];
2324 ino = kmalloc(zbr->len, GFP_NOFS);
2325 if (!ino)
2326 return -ENOMEM;
2327
2328 err = ubifs_tnc_read_node(c, zbr, ino);
2329 if (err) {
2330 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
2331 zbr->lnum, zbr->offs, err);
2332 kfree(ino);
2333 return err;
2334 }
2335
2336 ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02002337 (unsigned long)fscki->inum, zbr->lnum, zbr->offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002338 dbg_dump_node(c, ino);
2339 kfree(ino);
2340 return -EINVAL;
2341}
2342
2343/**
2344 * dbg_check_filesystem - check the file-system.
2345 * @c: UBIFS file-system description object
2346 *
2347 * This function checks the file system, namely:
2348 * o makes sure that all leaf nodes exist and their CRCs are correct;
2349 * o makes sure inode nlink, size, xattr size/count are correct (for all
2350 * inodes).
2351 *
2352 * The function reads whole indexing tree and all nodes, so it is pretty
2353 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
2354 * not, and a negative error code in case of failure.
2355 */
2356int dbg_check_filesystem(struct ubifs_info *c)
2357{
2358 int err;
2359 struct fsck_data fsckd;
2360
Artem Bityutskiy2b1844a2011-06-03 08:31:29 +03002361 if (!dbg_is_chk_fs(c))
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002362 return 0;
2363
2364 fsckd.inodes = RB_ROOT;
2365 err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
2366 if (err)
2367 goto out_free;
2368
2369 err = check_inodes(c, &fsckd);
2370 if (err)
2371 goto out_free;
2372
2373 free_inodes(&fsckd);
2374 return 0;
2375
2376out_free:
2377 ubifs_err("file-system check failed with error %d", err);
2378 dump_stack();
2379 free_inodes(&fsckd);
2380 return err;
2381}
2382
Artem Bityutskiy3bb66b42010-08-07 10:06:11 +03002383/**
2384 * dbg_check_data_nodes_order - check that list of data nodes is sorted.
2385 * @c: UBIFS file-system description object
2386 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2387 *
2388 * This function returns zero if the list of data nodes is sorted correctly,
2389 * and %-EINVAL if not.
2390 */
2391int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head)
2392{
2393 struct list_head *cur;
2394 struct ubifs_scan_node *sa, *sb;
2395
Artem Bityutskiy2b1844a2011-06-03 08:31:29 +03002396 if (!dbg_is_chk_gen(c))
Artem Bityutskiy3bb66b42010-08-07 10:06:11 +03002397 return 0;
2398
2399 for (cur = head->next; cur->next != head; cur = cur->next) {
2400 ino_t inuma, inumb;
2401 uint32_t blka, blkb;
2402
2403 cond_resched();
2404 sa = container_of(cur, struct ubifs_scan_node, list);
2405 sb = container_of(cur->next, struct ubifs_scan_node, list);
2406
2407 if (sa->type != UBIFS_DATA_NODE) {
2408 ubifs_err("bad node type %d", sa->type);
2409 dbg_dump_node(c, sa->node);
2410 return -EINVAL;
2411 }
2412 if (sb->type != UBIFS_DATA_NODE) {
2413 ubifs_err("bad node type %d", sb->type);
2414 dbg_dump_node(c, sb->node);
2415 return -EINVAL;
2416 }
2417
2418 inuma = key_inum(c, &sa->key);
2419 inumb = key_inum(c, &sb->key);
2420
2421 if (inuma < inumb)
2422 continue;
2423 if (inuma > inumb) {
2424 ubifs_err("larger inum %lu goes before inum %lu",
2425 (unsigned long)inuma, (unsigned long)inumb);
2426 goto error_dump;
2427 }
2428
2429 blka = key_block(c, &sa->key);
2430 blkb = key_block(c, &sb->key);
2431
2432 if (blka > blkb) {
2433 ubifs_err("larger block %u goes before %u", blka, blkb);
2434 goto error_dump;
2435 }
2436 if (blka == blkb) {
2437 ubifs_err("two data nodes for the same block");
2438 goto error_dump;
2439 }
2440 }
2441
2442 return 0;
2443
2444error_dump:
2445 dbg_dump_node(c, sa->node);
2446 dbg_dump_node(c, sb->node);
2447 return -EINVAL;
2448}
2449
2450/**
2451 * dbg_check_nondata_nodes_order - check that list of data nodes is sorted.
2452 * @c: UBIFS file-system description object
2453 * @head: the list of nodes ('struct ubifs_scan_node' objects)
2454 *
2455 * This function returns zero if the list of non-data nodes is sorted correctly,
2456 * and %-EINVAL if not.
2457 */
2458int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head)
2459{
2460 struct list_head *cur;
2461 struct ubifs_scan_node *sa, *sb;
2462
Artem Bityutskiy2b1844a2011-06-03 08:31:29 +03002463 if (!dbg_is_chk_gen(c))
Artem Bityutskiy3bb66b42010-08-07 10:06:11 +03002464 return 0;
2465
2466 for (cur = head->next; cur->next != head; cur = cur->next) {
2467 ino_t inuma, inumb;
2468 uint32_t hasha, hashb;
2469
2470 cond_resched();
2471 sa = container_of(cur, struct ubifs_scan_node, list);
2472 sb = container_of(cur->next, struct ubifs_scan_node, list);
2473
2474 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2475 sa->type != UBIFS_XENT_NODE) {
2476 ubifs_err("bad node type %d", sa->type);
2477 dbg_dump_node(c, sa->node);
2478 return -EINVAL;
2479 }
2480 if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&
2481 sa->type != UBIFS_XENT_NODE) {
2482 ubifs_err("bad node type %d", sb->type);
2483 dbg_dump_node(c, sb->node);
2484 return -EINVAL;
2485 }
2486
2487 if (sa->type != UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2488 ubifs_err("non-inode node goes before inode node");
2489 goto error_dump;
2490 }
2491
2492 if (sa->type == UBIFS_INO_NODE && sb->type != UBIFS_INO_NODE)
2493 continue;
2494
2495 if (sa->type == UBIFS_INO_NODE && sb->type == UBIFS_INO_NODE) {
2496 /* Inode nodes are sorted in descending size order */
2497 if (sa->len < sb->len) {
2498 ubifs_err("smaller inode node goes first");
2499 goto error_dump;
2500 }
2501 continue;
2502 }
2503
2504 /*
2505 * This is either a dentry or xentry, which should be sorted in
2506 * ascending (parent ino, hash) order.
2507 */
2508 inuma = key_inum(c, &sa->key);
2509 inumb = key_inum(c, &sb->key);
2510
2511 if (inuma < inumb)
2512 continue;
2513 if (inuma > inumb) {
2514 ubifs_err("larger inum %lu goes before inum %lu",
2515 (unsigned long)inuma, (unsigned long)inumb);
2516 goto error_dump;
2517 }
2518
2519 hasha = key_block(c, &sa->key);
2520 hashb = key_block(c, &sb->key);
2521
2522 if (hasha > hashb) {
Artem Bityutskiyc4361572011-03-25 15:27:40 +02002523 ubifs_err("larger hash %u goes before %u",
2524 hasha, hashb);
Artem Bityutskiy3bb66b42010-08-07 10:06:11 +03002525 goto error_dump;
2526 }
2527 }
2528
2529 return 0;
2530
2531error_dump:
2532 ubifs_msg("dumping first node");
2533 dbg_dump_node(c, sa->node);
2534 ubifs_msg("dumping second node");
2535 dbg_dump_node(c, sb->node);
2536 return -EINVAL;
2537 return 0;
2538}
2539
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002540/* Failure mode for recovery testing */
2541
2542#define chance(n, d) (simple_rand() <= (n) * 32768LL / (d))
2543
2544struct failure_mode_info {
2545 struct list_head list;
2546 struct ubifs_info *c;
2547};
2548
2549static LIST_HEAD(fmi_list);
2550static DEFINE_SPINLOCK(fmi_lock);
2551
2552static unsigned int next;
2553
2554static int simple_rand(void)
2555{
2556 if (next == 0)
2557 next = current->pid;
2558 next = next * 1103515245 + 12345;
2559 return (next >> 16) & 32767;
2560}
2561
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002562static void failure_mode_init(struct ubifs_info *c)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002563{
2564 struct failure_mode_info *fmi;
2565
2566 fmi = kmalloc(sizeof(struct failure_mode_info), GFP_NOFS);
2567 if (!fmi) {
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002568 ubifs_err("Failed to register failure mode - no memory");
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002569 return;
2570 }
2571 fmi->c = c;
2572 spin_lock(&fmi_lock);
2573 list_add_tail(&fmi->list, &fmi_list);
2574 spin_unlock(&fmi_lock);
2575}
2576
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002577static void failure_mode_exit(struct ubifs_info *c)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002578{
2579 struct failure_mode_info *fmi, *tmp;
2580
2581 spin_lock(&fmi_lock);
2582 list_for_each_entry_safe(fmi, tmp, &fmi_list, list)
2583 if (fmi->c == c) {
2584 list_del(&fmi->list);
2585 kfree(fmi);
2586 }
2587 spin_unlock(&fmi_lock);
2588}
2589
2590static struct ubifs_info *dbg_find_info(struct ubi_volume_desc *desc)
2591{
2592 struct failure_mode_info *fmi;
2593
2594 spin_lock(&fmi_lock);
2595 list_for_each_entry(fmi, &fmi_list, list)
2596 if (fmi->c->ubi == desc) {
2597 struct ubifs_info *c = fmi->c;
2598
2599 spin_unlock(&fmi_lock);
2600 return c;
2601 }
2602 spin_unlock(&fmi_lock);
2603 return NULL;
2604}
2605
2606static int in_failure_mode(struct ubi_volume_desc *desc)
2607{
2608 struct ubifs_info *c = dbg_find_info(desc);
2609
Artem Bityutskiy2b1844a2011-06-03 08:31:29 +03002610 if (c && dbg_is_tst_rcvry(c))
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002611 return c->dbg->failure_mode;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002612 return 0;
2613}
2614
2615static int do_fail(struct ubi_volume_desc *desc, int lnum, int write)
2616{
2617 struct ubifs_info *c = dbg_find_info(desc);
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002618 struct ubifs_debug_info *d;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002619
Artem Bityutskiy2b1844a2011-06-03 08:31:29 +03002620 if (!c || !dbg_is_tst_rcvry(c))
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002621 return 0;
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002622 d = c->dbg;
2623 if (d->failure_mode)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002624 return 1;
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002625 if (!d->fail_cnt) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002626 /* First call - decide delay to failure */
2627 if (chance(1, 2)) {
2628 unsigned int delay = 1 << (simple_rand() >> 11);
2629
2630 if (chance(1, 2)) {
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002631 d->fail_delay = 1;
2632 d->fail_timeout = jiffies +
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002633 msecs_to_jiffies(delay);
Artem Bityutskiy24a4f802011-06-01 15:23:25 +03002634 ubifs_warn("failing after %ums", delay);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002635 } else {
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002636 d->fail_delay = 2;
2637 d->fail_cnt_max = delay;
Artem Bityutskiy24a4f802011-06-01 15:23:25 +03002638 ubifs_warn("failing after %u calls", delay);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002639 }
2640 }
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002641 d->fail_cnt += 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002642 }
2643 /* Determine if failure delay has expired */
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002644 if (d->fail_delay == 1) {
2645 if (time_before(jiffies, d->fail_timeout))
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002646 return 0;
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002647 } else if (d->fail_delay == 2)
2648 if (d->fail_cnt++ < d->fail_cnt_max)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002649 return 0;
2650 if (lnum == UBIFS_SB_LNUM) {
2651 if (write) {
2652 if (chance(1, 2))
2653 return 0;
2654 } else if (chance(19, 20))
2655 return 0;
Artem Bityutskiy24a4f802011-06-01 15:23:25 +03002656 ubifs_warn("failing in super block LEB %d", lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002657 } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2658 if (chance(19, 20))
2659 return 0;
Artem Bityutskiy24a4f802011-06-01 15:23:25 +03002660 ubifs_warn("failing in master LEB %d", lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002661 } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2662 if (write) {
2663 if (chance(99, 100))
2664 return 0;
2665 } else if (chance(399, 400))
2666 return 0;
Artem Bityutskiy24a4f802011-06-01 15:23:25 +03002667 ubifs_warn("failing in log LEB %d", lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002668 } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2669 if (write) {
2670 if (chance(7, 8))
2671 return 0;
2672 } else if (chance(19, 20))
2673 return 0;
Artem Bityutskiy24a4f802011-06-01 15:23:25 +03002674 ubifs_warn("failing in LPT LEB %d", lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002675 } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2676 if (write) {
2677 if (chance(1, 2))
2678 return 0;
2679 } else if (chance(9, 10))
2680 return 0;
Artem Bityutskiy24a4f802011-06-01 15:23:25 +03002681 ubifs_warn("failing in orphan LEB %d", lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002682 } else if (lnum == c->ihead_lnum) {
2683 if (chance(99, 100))
2684 return 0;
Artem Bityutskiy24a4f802011-06-01 15:23:25 +03002685 ubifs_warn("failing in index head LEB %d", lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002686 } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2687 if (chance(9, 10))
2688 return 0;
Artem Bityutskiy24a4f802011-06-01 15:23:25 +03002689 ubifs_warn("failing in GC head LEB %d", lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002690 } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
2691 !ubifs_search_bud(c, lnum)) {
2692 if (chance(19, 20))
2693 return 0;
Artem Bityutskiy24a4f802011-06-01 15:23:25 +03002694 ubifs_warn("failing in non-bud LEB %d", lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002695 } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
2696 c->cmt_state == COMMIT_RUNNING_REQUIRED) {
2697 if (chance(999, 1000))
2698 return 0;
Artem Bityutskiy24a4f802011-06-01 15:23:25 +03002699 ubifs_warn("failing in bud LEB %d commit running", lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002700 } else {
2701 if (chance(9999, 10000))
2702 return 0;
Artem Bityutskiy24a4f802011-06-01 15:23:25 +03002703 ubifs_warn("failing in bud LEB %d commit not running", lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002704 }
Artem Bityutskiy24a4f802011-06-01 15:23:25 +03002705
Artem Bityutskiy17c2f9f2008-10-17 13:31:39 +03002706 d->failure_mode = 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002707 dump_stack();
2708 return 1;
2709}
2710
2711static void cut_data(const void *buf, int len)
2712{
2713 int flen, i;
2714 unsigned char *p = (void *)buf;
2715
2716 flen = (len * (long long)simple_rand()) >> 15;
2717 for (i = flen; i < len; i++)
2718 p[i] = 0xff;
2719}
2720
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002721int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
Artem Bityutskiy891a54a2011-06-03 11:32:21 +03002722 int offs, int len, int dtype)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002723{
Adrian Hunter16dfd802008-07-18 16:47:41 +03002724 int err, failing;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002725
2726 if (in_failure_mode(desc))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002727 return -EROFS;
Adrian Hunter16dfd802008-07-18 16:47:41 +03002728 failing = do_fail(desc, lnum, 1);
2729 if (failing)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002730 cut_data(buf, len);
Artem Bityutskiy891a54a2011-06-03 11:32:21 +03002731 err = ubi_leb_write(desc, lnum, buf, offs, len, dtype);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002732 if (err)
2733 return err;
Adrian Hunter16dfd802008-07-18 16:47:41 +03002734 if (failing)
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002735 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002736 return 0;
2737}
2738
2739int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
2740 int len, int dtype)
2741{
2742 int err;
2743
2744 if (do_fail(desc, lnum, 1))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002745 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002746 err = ubi_leb_change(desc, lnum, buf, len, dtype);
2747 if (err)
2748 return err;
2749 if (do_fail(desc, lnum, 1))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002750 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002751 return 0;
2752}
2753
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002754int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum)
2755{
2756 int err;
2757
2758 if (do_fail(desc, lnum, 0))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002759 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002760 err = ubi_leb_unmap(desc, lnum);
2761 if (err)
2762 return err;
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 return 0;
2766}
2767
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002768int dbg_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
2769{
2770 int err;
2771
2772 if (do_fail(desc, lnum, 0))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002773 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002774 err = ubi_leb_map(desc, lnum, dtype);
2775 if (err)
2776 return err;
2777 if (do_fail(desc, lnum, 0))
Artem Bityutskiy1a29af82011-04-20 17:06:17 +03002778 return -EROFS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03002779 return 0;
2780}
2781
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002782/*
2783 * Root directory for UBIFS stuff in debugfs. Contains sub-directories which
2784 * contain the stuff specific to particular file-system mounts.
2785 */
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002786static struct dentry *dfs_rootdir;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002787
Artem Bityutskiy7dae9972011-06-01 15:44:14 +03002788static int dfs_file_open(struct inode *inode, struct file *file)
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002789{
2790 file->private_data = inode->i_private;
Artem Bityutskiy1bbfc842011-03-21 16:26:42 +02002791 return nonseekable_open(inode, file);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002792}
2793
Artem Bityutskiy28488fc2011-06-03 09:58:23 +03002794/**
2795 * provide_user_output - provide output to the user reading a debugfs file.
2796 * @val: boolean value for the answer
2797 * @u: the buffer to store the answer at
2798 * @count: size of the buffer
2799 * @ppos: position in the @u output buffer
2800 *
2801 * This is a simple helper function which stores @val boolean value in the user
2802 * buffer when the user reads one of UBIFS debugfs files. Returns amount of
2803 * bytes written to @u in case of success and a negative error code in case of
2804 * failure.
2805 */
2806static int provide_user_output(int val, char __user *u, size_t count,
2807 loff_t *ppos)
2808{
2809 char buf[3];
2810
2811 if (val)
2812 buf[0] = '1';
2813 else
2814 buf[0] = '0';
2815 buf[1] = '\n';
2816 buf[2] = 0x00;
2817
2818 return simple_read_from_buffer(u, count, ppos, buf, 2);
2819}
2820
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002821static ssize_t dfs_file_read(struct file *file, char __user *u, size_t count,
2822 loff_t *ppos)
2823{
2824 struct dentry *dent = file->f_path.dentry;
2825 struct ubifs_info *c = file->private_data;
2826 struct ubifs_debug_info *d = c->dbg;
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002827 int val;
2828
2829 if (dent == d->dfs_chk_gen)
2830 val = d->chk_gen;
2831 else if (dent == d->dfs_chk_index)
2832 val = d->chk_index;
2833 else if (dent == d->dfs_chk_orph)
2834 val = d->chk_orph;
2835 else if (dent == d->dfs_chk_lprops)
2836 val = d->chk_lprops;
2837 else if (dent == d->dfs_chk_fs)
2838 val = d->chk_fs;
2839 else if (dent == d->dfs_tst_rcvry)
2840 val = d->tst_rcvry;
2841 else
2842 return -EINVAL;
2843
Artem Bityutskiy28488fc2011-06-03 09:58:23 +03002844 return provide_user_output(val, u, count, ppos);
2845}
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002846
Artem Bityutskiy28488fc2011-06-03 09:58:23 +03002847/**
2848 * interpret_user_input - interpret user debugfs file input.
2849 * @u: user-provided buffer with the input
2850 * @count: buffer size
2851 *
2852 * This is a helper function which interpret user input to a boolean UBIFS
2853 * debugfs file. Returns %0 or %1 in case of success and a negative error code
2854 * in case of failure.
2855 */
2856static int interpret_user_input(const char __user *u, size_t count)
2857{
2858 size_t buf_size;
2859 char buf[8];
2860
2861 buf_size = min_t(size_t, count, (sizeof(buf) - 1));
2862 if (copy_from_user(buf, u, buf_size))
2863 return -EFAULT;
2864
2865 if (buf[0] == '1')
2866 return 1;
2867 else if (buf[0] == '0')
2868 return 0;
2869
2870 return -EINVAL;
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002871}
2872
2873static ssize_t dfs_file_write(struct file *file, const char __user *u,
2874 size_t count, loff_t *ppos)
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002875{
2876 struct ubifs_info *c = file->private_data;
2877 struct ubifs_debug_info *d = c->dbg;
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002878 struct dentry *dent = file->f_path.dentry;
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002879 int val;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002880
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002881 /*
Artem Bityutskiy24a4f802011-06-01 15:23:25 +03002882 * TODO: this is racy - the file-system might have already been
2883 * unmounted and we'd oops in this case. The plan is to fix it with
2884 * help of 'iterate_supers_type()' which we should have in v3.0: when
2885 * a debugfs opened, we rember FS's UUID in file->private_data. Then
2886 * whenever we access the FS via a debugfs file, we iterate all UBIFS
2887 * superblocks and fine the one with the same UUID, and take the
2888 * locking right.
2889 *
2890 * The other way to go suggested by Al Viro is to create a separate
2891 * 'ubifs-debug' file-system instead.
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002892 */
2893 if (file->f_path.dentry == d->dfs_dump_lprops) {
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002894 dbg_dump_lprops(c);
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002895 return count;
2896 }
2897 if (file->f_path.dentry == d->dfs_dump_budg) {
Artem Bityutskiyf1bd66a2011-03-29 18:36:21 +03002898 dbg_dump_budg(c, &c->bi);
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002899 return count;
2900 }
2901 if (file->f_path.dentry == d->dfs_dump_tnc) {
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002902 mutex_lock(&c->tnc_mutex);
2903 dbg_dump_tnc(c);
2904 mutex_unlock(&c->tnc_mutex);
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002905 return count;
2906 }
2907
Artem Bityutskiy28488fc2011-06-03 09:58:23 +03002908 val = interpret_user_input(u, count);
2909 if (val < 0)
2910 return val;
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002911
2912 if (dent == d->dfs_chk_gen)
2913 d->chk_gen = val;
2914 else if (dent == d->dfs_chk_index)
2915 d->chk_index = val;
2916 else if (dent == d->dfs_chk_orph)
2917 d->chk_orph = val;
2918 else if (dent == d->dfs_chk_lprops)
2919 d->chk_lprops = val;
2920 else if (dent == d->dfs_chk_fs)
2921 d->chk_fs = val;
2922 else if (dent == d->dfs_tst_rcvry)
2923 d->tst_rcvry = val;
2924 else
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002925 return -EINVAL;
2926
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002927 return count;
2928}
2929
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002930static const struct file_operations dfs_fops = {
Artem Bityutskiy7dae9972011-06-01 15:44:14 +03002931 .open = dfs_file_open,
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002932 .read = dfs_file_read,
2933 .write = dfs_file_write,
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002934 .owner = THIS_MODULE,
Artem Bityutskiy1bbfc842011-03-21 16:26:42 +02002935 .llseek = no_llseek,
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002936};
2937
2938/**
2939 * dbg_debugfs_init_fs - initialize debugfs for UBIFS instance.
2940 * @c: UBIFS file-system description object
2941 *
2942 * This function creates all debugfs files for this instance of UBIFS. Returns
2943 * zero in case of success and a negative error code in case of failure.
2944 *
2945 * Note, the only reason we have not merged this function with the
2946 * 'ubifs_debugging_init()' function is because it is better to initialize
2947 * debugfs interfaces at the very end of the mount process, and remove them at
2948 * the very beginning of the mount process.
2949 */
2950int dbg_debugfs_init_fs(struct ubifs_info *c)
2951{
Artem Bityutskiyae380ce2011-05-19 14:13:16 +03002952 int err, n;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002953 const char *fname;
2954 struct dentry *dent;
2955 struct ubifs_debug_info *d = c->dbg;
2956
Artem Bityutskiyae380ce2011-05-19 14:13:16 +03002957 n = snprintf(d->dfs_dir_name, UBIFS_DFS_DIR_LEN + 1, UBIFS_DFS_DIR_NAME,
2958 c->vi.ubi_num, c->vi.vol_id);
2959 if (n == UBIFS_DFS_DIR_LEN) {
2960 /* The array size is too small */
2961 fname = UBIFS_DFS_DIR_NAME;
2962 dent = ERR_PTR(-EINVAL);
2963 goto out;
2964 }
2965
Artem Bityutskiycc6a86b2011-04-01 10:10:52 +03002966 fname = d->dfs_dir_name;
2967 dent = debugfs_create_dir(fname, dfs_rootdir);
Artem Bityutskiy95169532011-04-01 10:16:17 +03002968 if (IS_ERR_OR_NULL(dent))
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002969 goto out;
Artem Bityutskiycc6a86b2011-04-01 10:10:52 +03002970 d->dfs_dir = dent;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002971
2972 fname = "dump_lprops";
Vasiliy Kulikov8c559d32011-02-04 15:24:19 +03002973 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
Artem Bityutskiy95169532011-04-01 10:16:17 +03002974 if (IS_ERR_OR_NULL(dent))
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002975 goto out_remove;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002976 d->dfs_dump_lprops = dent;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002977
2978 fname = "dump_budg";
Vasiliy Kulikov8c559d32011-02-04 15:24:19 +03002979 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
Artem Bityutskiy95169532011-04-01 10:16:17 +03002980 if (IS_ERR_OR_NULL(dent))
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002981 goto out_remove;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002982 d->dfs_dump_budg = dent;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002983
2984 fname = "dump_tnc";
Vasiliy Kulikov8c559d32011-02-04 15:24:19 +03002985 dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops);
Artem Bityutskiy95169532011-04-01 10:16:17 +03002986 if (IS_ERR_OR_NULL(dent))
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002987 goto out_remove;
Artem Bityutskiy84abf972009-01-23 14:54:59 +02002988 d->dfs_dump_tnc = dent;
Artem Bityutskiy552ff312008-10-23 11:49:28 +03002989
Artem Bityutskiy81e79d32011-05-31 18:16:34 +03002990 fname = "chk_general";
2991 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2992 &dfs_fops);
2993 if (IS_ERR_OR_NULL(dent))
2994 goto out_remove;
2995 d->dfs_chk_gen = dent;
2996
2997 fname = "chk_index";
2998 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
2999 &dfs_fops);
3000 if (IS_ERR_OR_NULL(dent))
3001 goto out_remove;
3002 d->dfs_chk_index = dent;
3003
3004 fname = "chk_orphans";
3005 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
3006 &dfs_fops);
3007 if (IS_ERR_OR_NULL(dent))
3008 goto out_remove;
3009 d->dfs_chk_orph = dent;
3010
3011 fname = "chk_lprops";
3012 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
3013 &dfs_fops);
3014 if (IS_ERR_OR_NULL(dent))
3015 goto out_remove;
3016 d->dfs_chk_lprops = dent;
3017
3018 fname = "chk_fs";
3019 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
3020 &dfs_fops);
3021 if (IS_ERR_OR_NULL(dent))
3022 goto out_remove;
3023 d->dfs_chk_fs = dent;
3024
3025 fname = "tst_recovery";
3026 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c,
3027 &dfs_fops);
3028 if (IS_ERR_OR_NULL(dent))
3029 goto out_remove;
3030 d->dfs_tst_rcvry = dent;
3031
Artem Bityutskiy552ff312008-10-23 11:49:28 +03003032 return 0;
3033
3034out_remove:
Artem Bityutskiycc6a86b2011-04-01 10:10:52 +03003035 debugfs_remove_recursive(d->dfs_dir);
3036out:
Artem Bityutskiy95169532011-04-01 10:16:17 +03003037 err = dent ? PTR_ERR(dent) : -ENODEV;
Artem Bityutskiye77170602011-06-01 17:43:43 +03003038 ubifs_err("cannot create \"%s\" debugfs file or directory, error %d\n",
Artem Bityutskiy552ff312008-10-23 11:49:28 +03003039 fname, err);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03003040 return err;
3041}
3042
3043/**
3044 * dbg_debugfs_exit_fs - remove all debugfs files.
3045 * @c: UBIFS file-system description object
3046 */
3047void dbg_debugfs_exit_fs(struct ubifs_info *c)
3048{
Artem Bityutskiy84abf972009-01-23 14:54:59 +02003049 debugfs_remove_recursive(c->dbg->dfs_dir);
Artem Bityutskiy552ff312008-10-23 11:49:28 +03003050}
3051
Artem Bityutskiye77170602011-06-01 17:43:43 +03003052struct ubifs_global_debug_info ubifs_dbg;
3053
3054static struct dentry *dfs_chk_gen;
3055static struct dentry *dfs_chk_index;
3056static struct dentry *dfs_chk_orph;
3057static struct dentry *dfs_chk_lprops;
3058static struct dentry *dfs_chk_fs;
3059static struct dentry *dfs_tst_rcvry;
3060
3061static ssize_t dfs_global_file_read(struct file *file, char __user *u,
3062 size_t count, loff_t *ppos)
3063{
3064 struct dentry *dent = file->f_path.dentry;
3065 int val;
3066
3067 if (dent == dfs_chk_gen)
3068 val = ubifs_dbg.chk_gen;
3069 else if (dent == dfs_chk_index)
3070 val = ubifs_dbg.chk_index;
3071 else if (dent == dfs_chk_orph)
3072 val = ubifs_dbg.chk_orph;
3073 else if (dent == dfs_chk_lprops)
3074 val = ubifs_dbg.chk_lprops;
3075 else if (dent == dfs_chk_fs)
3076 val = ubifs_dbg.chk_fs;
3077 else if (dent == dfs_tst_rcvry)
3078 val = ubifs_dbg.tst_rcvry;
3079 else
3080 return -EINVAL;
3081
3082 return provide_user_output(val, u, count, ppos);
3083}
3084
3085static ssize_t dfs_global_file_write(struct file *file, const char __user *u,
3086 size_t count, loff_t *ppos)
3087{
3088 struct dentry *dent = file->f_path.dentry;
3089 int val;
3090
3091 val = interpret_user_input(u, count);
3092 if (val < 0)
3093 return val;
3094
3095 if (dent == dfs_chk_gen)
3096 ubifs_dbg.chk_gen = val;
3097 else if (dent == dfs_chk_index)
3098 ubifs_dbg.chk_index = val;
3099 else if (dent == dfs_chk_orph)
3100 ubifs_dbg.chk_orph = val;
3101 else if (dent == dfs_chk_lprops)
3102 ubifs_dbg.chk_lprops = val;
3103 else if (dent == dfs_chk_fs)
3104 ubifs_dbg.chk_fs = val;
3105 else if (dent == dfs_tst_rcvry)
3106 ubifs_dbg.tst_rcvry = val;
3107 else
3108 return -EINVAL;
3109
3110 return count;
3111}
3112
3113static const struct file_operations dfs_global_fops = {
3114 .read = dfs_global_file_read,
3115 .write = dfs_global_file_write,
3116 .owner = THIS_MODULE,
3117 .llseek = no_llseek,
3118};
3119
Artem Bityutskiy7dae9972011-06-01 15:44:14 +03003120/**
3121 * dbg_debugfs_init - initialize debugfs file-system.
3122 *
3123 * UBIFS uses debugfs file-system to expose various debugging knobs to
3124 * user-space. This function creates "ubifs" directory in the debugfs
3125 * file-system. Returns zero in case of success and a negative error code in
3126 * case of failure.
3127 */
3128int dbg_debugfs_init(void)
3129{
Artem Bityutskiye77170602011-06-01 17:43:43 +03003130 int err;
3131 const char *fname;
3132 struct dentry *dent;
3133
3134 fname = "ubifs";
3135 dent = debugfs_create_dir(fname, NULL);
3136 if (IS_ERR_OR_NULL(dent))
3137 goto out;
3138 dfs_rootdir = dent;
3139
3140 fname = "chk_general";
3141 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3142 &dfs_global_fops);
3143 if (IS_ERR_OR_NULL(dent))
3144 goto out_remove;
3145 dfs_chk_gen = dent;
3146
3147 fname = "chk_index";
3148 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3149 &dfs_global_fops);
3150 if (IS_ERR_OR_NULL(dent))
3151 goto out_remove;
3152 dfs_chk_index = dent;
3153
3154 fname = "chk_orphans";
3155 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3156 &dfs_global_fops);
3157 if (IS_ERR_OR_NULL(dent))
3158 goto out_remove;
3159 dfs_chk_orph = dent;
3160
3161 fname = "chk_lprops";
3162 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3163 &dfs_global_fops);
3164 if (IS_ERR_OR_NULL(dent))
3165 goto out_remove;
3166 dfs_chk_lprops = dent;
3167
3168 fname = "chk_fs";
3169 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3170 &dfs_global_fops);
3171 if (IS_ERR_OR_NULL(dent))
3172 goto out_remove;
3173 dfs_chk_fs = dent;
3174
3175 fname = "tst_recovery";
3176 dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL,
3177 &dfs_global_fops);
3178 if (IS_ERR_OR_NULL(dent))
3179 goto out_remove;
3180 dfs_tst_rcvry = dent;
Artem Bityutskiy7dae9972011-06-01 15:44:14 +03003181
3182 return 0;
Artem Bityutskiye77170602011-06-01 17:43:43 +03003183
3184out_remove:
3185 debugfs_remove_recursive(dfs_rootdir);
3186out:
3187 err = dent ? PTR_ERR(dent) : -ENODEV;
3188 ubifs_err("cannot create \"%s\" debugfs file or directory, error %d\n",
3189 fname, err);
3190 return err;
Artem Bityutskiy7dae9972011-06-01 15:44:14 +03003191}
3192
3193/**
3194 * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system.
3195 */
3196void dbg_debugfs_exit(void)
3197{
Artem Bityutskiye77170602011-06-01 17:43:43 +03003198 debugfs_remove_recursive(dfs_rootdir);
Artem Bityutskiy7dae9972011-06-01 15:44:14 +03003199}
3200
3201/**
3202 * ubifs_debugging_init - initialize UBIFS debugging.
3203 * @c: UBIFS file-system description object
3204 *
3205 * This function initializes debugging-related data for the file system.
3206 * Returns zero in case of success and a negative error code in case of
3207 * failure.
3208 */
3209int ubifs_debugging_init(struct ubifs_info *c)
3210{
3211 c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
3212 if (!c->dbg)
3213 return -ENOMEM;
3214
3215 failure_mode_init(c);
3216 return 0;
3217}
3218
3219/**
3220 * ubifs_debugging_exit - free debugging data.
3221 * @c: UBIFS file-system description object
3222 */
3223void ubifs_debugging_exit(struct ubifs_info *c)
3224{
3225 failure_mode_exit(c);
3226 kfree(c->dbg);
3227}
3228
Artem Bityutskiy1e517642008-07-14 19:08:37 +03003229#endif /* CONFIG_UBIFS_FS_DEBUG */