UBIFS: fix bulk-read handling uptodate pages
[deliverable/linux.git] / fs / ubifs / debug.c
CommitLineData
1e51764a
AB
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
20 * Adrian Hunter
21 */
22
23/*
24 * This file implements most of the debugging stuff which is compiled in only
25 * when it is enabled. But some debugging check functions are implemented in
26 * corresponding subsystem, just because they are closely related and utilize
27 * various local functions of those subsystems.
28 */
29
30#define UBIFS_DBG_PRESERVE_UBI
31
32#include "ubifs.h"
33#include <linux/module.h>
34#include <linux/moduleparam.h>
35
36#ifdef CONFIG_UBIFS_FS_DEBUG
37
38DEFINE_SPINLOCK(dbg_lock);
39
40static char dbg_key_buf0[128];
41static char dbg_key_buf1[128];
42
43unsigned int ubifs_msg_flags = UBIFS_MSG_FLAGS_DEFAULT;
44unsigned int ubifs_chk_flags = UBIFS_CHK_FLAGS_DEFAULT;
45unsigned int ubifs_tst_flags;
46
47module_param_named(debug_msgs, ubifs_msg_flags, uint, S_IRUGO | S_IWUSR);
48module_param_named(debug_chks, ubifs_chk_flags, uint, S_IRUGO | S_IWUSR);
49module_param_named(debug_tsts, ubifs_tst_flags, uint, S_IRUGO | S_IWUSR);
50
51MODULE_PARM_DESC(debug_msgs, "Debug message type flags");
52MODULE_PARM_DESC(debug_chks, "Debug check flags");
53MODULE_PARM_DESC(debug_tsts, "Debug special test flags");
54
55static const char *get_key_fmt(int fmt)
56{
57 switch (fmt) {
58 case UBIFS_SIMPLE_KEY_FMT:
59 return "simple";
60 default:
61 return "unknown/invalid format";
62 }
63}
64
65static const char *get_key_hash(int hash)
66{
67 switch (hash) {
68 case UBIFS_KEY_HASH_R5:
69 return "R5";
70 case UBIFS_KEY_HASH_TEST:
71 return "test";
72 default:
73 return "unknown/invalid name hash";
74 }
75}
76
77static const char *get_key_type(int type)
78{
79 switch (type) {
80 case UBIFS_INO_KEY:
81 return "inode";
82 case UBIFS_DENT_KEY:
83 return "direntry";
84 case UBIFS_XENT_KEY:
85 return "xentry";
86 case UBIFS_DATA_KEY:
87 return "data";
88 case UBIFS_TRUN_KEY:
89 return "truncate";
90 default:
91 return "unknown/invalid key";
92 }
93}
94
95static void sprintf_key(const struct ubifs_info *c, const union ubifs_key *key,
96 char *buffer)
97{
98 char *p = buffer;
99 int type = key_type(c, key);
100
101 if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
102 switch (type) {
103 case UBIFS_INO_KEY:
104 sprintf(p, "(%lu, %s)", key_inum(c, key),
105 get_key_type(type));
106 break;
107 case UBIFS_DENT_KEY:
108 case UBIFS_XENT_KEY:
109 sprintf(p, "(%lu, %s, %#08x)", key_inum(c, key),
110 get_key_type(type), key_hash(c, key));
111 break;
112 case UBIFS_DATA_KEY:
113 sprintf(p, "(%lu, %s, %u)", key_inum(c, key),
114 get_key_type(type), key_block(c, key));
115 break;
116 case UBIFS_TRUN_KEY:
117 sprintf(p, "(%lu, %s)",
118 key_inum(c, key), get_key_type(type));
119 break;
120 default:
121 sprintf(p, "(bad key type: %#08x, %#08x)",
122 key->u32[0], key->u32[1]);
123 }
124 } else
125 sprintf(p, "bad key format %d", c->key_fmt);
126}
127
128const char *dbg_key_str0(const struct ubifs_info *c, const union ubifs_key *key)
129{
130 /* dbg_lock must be held */
131 sprintf_key(c, key, dbg_key_buf0);
132 return dbg_key_buf0;
133}
134
135const char *dbg_key_str1(const struct ubifs_info *c, const union ubifs_key *key)
136{
137 /* dbg_lock must be held */
138 sprintf_key(c, key, dbg_key_buf1);
139 return dbg_key_buf1;
140}
141
142const char *dbg_ntype(int type)
143{
144 switch (type) {
145 case UBIFS_PAD_NODE:
146 return "padding node";
147 case UBIFS_SB_NODE:
148 return "superblock node";
149 case UBIFS_MST_NODE:
150 return "master node";
151 case UBIFS_REF_NODE:
152 return "reference node";
153 case UBIFS_INO_NODE:
154 return "inode node";
155 case UBIFS_DENT_NODE:
156 return "direntry node";
157 case UBIFS_XENT_NODE:
158 return "xentry node";
159 case UBIFS_DATA_NODE:
160 return "data node";
161 case UBIFS_TRUN_NODE:
162 return "truncate node";
163 case UBIFS_IDX_NODE:
164 return "indexing node";
165 case UBIFS_CS_NODE:
166 return "commit start node";
167 case UBIFS_ORPH_NODE:
168 return "orphan node";
169 default:
170 return "unknown node";
171 }
172}
173
174static const char *dbg_gtype(int type)
175{
176 switch (type) {
177 case UBIFS_NO_NODE_GROUP:
178 return "no node group";
179 case UBIFS_IN_NODE_GROUP:
180 return "in node group";
181 case UBIFS_LAST_OF_NODE_GROUP:
182 return "last of node group";
183 default:
184 return "unknown";
185 }
186}
187
188const char *dbg_cstate(int cmt_state)
189{
190 switch (cmt_state) {
191 case COMMIT_RESTING:
192 return "commit resting";
193 case COMMIT_BACKGROUND:
194 return "background commit requested";
195 case COMMIT_REQUIRED:
196 return "commit required";
197 case COMMIT_RUNNING_BACKGROUND:
198 return "BACKGROUND commit running";
199 case COMMIT_RUNNING_REQUIRED:
200 return "commit running and required";
201 case COMMIT_BROKEN:
202 return "broken commit";
203 default:
204 return "unknown commit state";
205 }
206}
207
208static void dump_ch(const struct ubifs_ch *ch)
209{
210 printk(KERN_DEBUG "\tmagic %#x\n", le32_to_cpu(ch->magic));
211 printk(KERN_DEBUG "\tcrc %#x\n", le32_to_cpu(ch->crc));
212 printk(KERN_DEBUG "\tnode_type %d (%s)\n", ch->node_type,
213 dbg_ntype(ch->node_type));
214 printk(KERN_DEBUG "\tgroup_type %d (%s)\n", ch->group_type,
215 dbg_gtype(ch->group_type));
216 printk(KERN_DEBUG "\tsqnum %llu\n",
217 (unsigned long long)le64_to_cpu(ch->sqnum));
218 printk(KERN_DEBUG "\tlen %u\n", le32_to_cpu(ch->len));
219}
220
221void dbg_dump_inode(const struct ubifs_info *c, const struct inode *inode)
222{
223 const struct ubifs_inode *ui = ubifs_inode(inode);
224
b5e426e9
AB
225 printk(KERN_DEBUG "Dump in-memory inode:");
226 printk(KERN_DEBUG "\tinode %lu\n", inode->i_ino);
227 printk(KERN_DEBUG "\tsize %llu\n",
1e51764a 228 (unsigned long long)i_size_read(inode));
b5e426e9
AB
229 printk(KERN_DEBUG "\tnlink %u\n", inode->i_nlink);
230 printk(KERN_DEBUG "\tuid %u\n", (unsigned int)inode->i_uid);
231 printk(KERN_DEBUG "\tgid %u\n", (unsigned int)inode->i_gid);
232 printk(KERN_DEBUG "\tatime %u.%u\n",
1e51764a
AB
233 (unsigned int)inode->i_atime.tv_sec,
234 (unsigned int)inode->i_atime.tv_nsec);
b5e426e9 235 printk(KERN_DEBUG "\tmtime %u.%u\n",
1e51764a
AB
236 (unsigned int)inode->i_mtime.tv_sec,
237 (unsigned int)inode->i_mtime.tv_nsec);
b5e426e9 238 printk(KERN_DEBUG "\tctime %u.%u\n",
1e51764a
AB
239 (unsigned int)inode->i_ctime.tv_sec,
240 (unsigned int)inode->i_ctime.tv_nsec);
b5e426e9
AB
241 printk(KERN_DEBUG "\tcreat_sqnum %llu\n", ui->creat_sqnum);
242 printk(KERN_DEBUG "\txattr_size %u\n", ui->xattr_size);
243 printk(KERN_DEBUG "\txattr_cnt %u\n", ui->xattr_cnt);
244 printk(KERN_DEBUG "\txattr_names %u\n", ui->xattr_names);
245 printk(KERN_DEBUG "\tdirty %u\n", ui->dirty);
246 printk(KERN_DEBUG "\txattr %u\n", ui->xattr);
247 printk(KERN_DEBUG "\tbulk_read %u\n", ui->xattr);
248 printk(KERN_DEBUG "\tsynced_i_size %llu\n",
249 (unsigned long long)ui->synced_i_size);
250 printk(KERN_DEBUG "\tui_size %llu\n",
251 (unsigned long long)ui->ui_size);
252 printk(KERN_DEBUG "\tflags %d\n", ui->flags);
253 printk(KERN_DEBUG "\tcompr_type %d\n", ui->compr_type);
254 printk(KERN_DEBUG "\tlast_page_read %lu\n", ui->last_page_read);
255 printk(KERN_DEBUG "\tread_in_a_row %lu\n", ui->read_in_a_row);
256 printk(KERN_DEBUG "\tdata_len %d\n", ui->data_len);
1e51764a
AB
257}
258
259void dbg_dump_node(const struct ubifs_info *c, const void *node)
260{
261 int i, n;
262 union ubifs_key key;
263 const struct ubifs_ch *ch = node;
264
265 if (dbg_failure_mode)
266 return;
267
268 /* If the magic is incorrect, just hexdump the first bytes */
269 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) {
270 printk(KERN_DEBUG "Not a node, first %zu bytes:", UBIFS_CH_SZ);
271 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
272 (void *)node, UBIFS_CH_SZ, 1);
273 return;
274 }
275
276 spin_lock(&dbg_lock);
277 dump_ch(node);
278
279 switch (ch->node_type) {
280 case UBIFS_PAD_NODE:
281 {
282 const struct ubifs_pad_node *pad = node;
283
284 printk(KERN_DEBUG "\tpad_len %u\n",
285 le32_to_cpu(pad->pad_len));
286 break;
287 }
288 case UBIFS_SB_NODE:
289 {
290 const struct ubifs_sb_node *sup = node;
291 unsigned int sup_flags = le32_to_cpu(sup->flags);
292
293 printk(KERN_DEBUG "\tkey_hash %d (%s)\n",
294 (int)sup->key_hash, get_key_hash(sup->key_hash));
295 printk(KERN_DEBUG "\tkey_fmt %d (%s)\n",
296 (int)sup->key_fmt, get_key_fmt(sup->key_fmt));
297 printk(KERN_DEBUG "\tflags %#x\n", sup_flags);
298 printk(KERN_DEBUG "\t big_lpt %u\n",
299 !!(sup_flags & UBIFS_FLG_BIGLPT));
300 printk(KERN_DEBUG "\tmin_io_size %u\n",
301 le32_to_cpu(sup->min_io_size));
302 printk(KERN_DEBUG "\tleb_size %u\n",
303 le32_to_cpu(sup->leb_size));
304 printk(KERN_DEBUG "\tleb_cnt %u\n",
305 le32_to_cpu(sup->leb_cnt));
306 printk(KERN_DEBUG "\tmax_leb_cnt %u\n",
307 le32_to_cpu(sup->max_leb_cnt));
308 printk(KERN_DEBUG "\tmax_bud_bytes %llu\n",
309 (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
310 printk(KERN_DEBUG "\tlog_lebs %u\n",
311 le32_to_cpu(sup->log_lebs));
312 printk(KERN_DEBUG "\tlpt_lebs %u\n",
313 le32_to_cpu(sup->lpt_lebs));
314 printk(KERN_DEBUG "\torph_lebs %u\n",
315 le32_to_cpu(sup->orph_lebs));
316 printk(KERN_DEBUG "\tjhead_cnt %u\n",
317 le32_to_cpu(sup->jhead_cnt));
318 printk(KERN_DEBUG "\tfanout %u\n",
319 le32_to_cpu(sup->fanout));
320 printk(KERN_DEBUG "\tlsave_cnt %u\n",
321 le32_to_cpu(sup->lsave_cnt));
322 printk(KERN_DEBUG "\tdefault_compr %u\n",
323 (int)le16_to_cpu(sup->default_compr));
324 printk(KERN_DEBUG "\trp_size %llu\n",
325 (unsigned long long)le64_to_cpu(sup->rp_size));
326 printk(KERN_DEBUG "\trp_uid %u\n",
327 le32_to_cpu(sup->rp_uid));
328 printk(KERN_DEBUG "\trp_gid %u\n",
329 le32_to_cpu(sup->rp_gid));
330 printk(KERN_DEBUG "\tfmt_version %u\n",
331 le32_to_cpu(sup->fmt_version));
332 printk(KERN_DEBUG "\ttime_gran %u\n",
333 le32_to_cpu(sup->time_gran));
334 printk(KERN_DEBUG "\tUUID %02X%02X%02X%02X-%02X%02X"
335 "-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X\n",
336 sup->uuid[0], sup->uuid[1], sup->uuid[2], sup->uuid[3],
337 sup->uuid[4], sup->uuid[5], sup->uuid[6], sup->uuid[7],
338 sup->uuid[8], sup->uuid[9], sup->uuid[10], sup->uuid[11],
339 sup->uuid[12], sup->uuid[13], sup->uuid[14],
340 sup->uuid[15]);
341 break;
342 }
343 case UBIFS_MST_NODE:
344 {
345 const struct ubifs_mst_node *mst = node;
346
347 printk(KERN_DEBUG "\thighest_inum %llu\n",
348 (unsigned long long)le64_to_cpu(mst->highest_inum));
349 printk(KERN_DEBUG "\tcommit number %llu\n",
350 (unsigned long long)le64_to_cpu(mst->cmt_no));
351 printk(KERN_DEBUG "\tflags %#x\n",
352 le32_to_cpu(mst->flags));
353 printk(KERN_DEBUG "\tlog_lnum %u\n",
354 le32_to_cpu(mst->log_lnum));
355 printk(KERN_DEBUG "\troot_lnum %u\n",
356 le32_to_cpu(mst->root_lnum));
357 printk(KERN_DEBUG "\troot_offs %u\n",
358 le32_to_cpu(mst->root_offs));
359 printk(KERN_DEBUG "\troot_len %u\n",
360 le32_to_cpu(mst->root_len));
361 printk(KERN_DEBUG "\tgc_lnum %u\n",
362 le32_to_cpu(mst->gc_lnum));
363 printk(KERN_DEBUG "\tihead_lnum %u\n",
364 le32_to_cpu(mst->ihead_lnum));
365 printk(KERN_DEBUG "\tihead_offs %u\n",
366 le32_to_cpu(mst->ihead_offs));
367 printk(KERN_DEBUG "\tindex_size %u\n",
368 le32_to_cpu(mst->index_size));
369 printk(KERN_DEBUG "\tlpt_lnum %u\n",
370 le32_to_cpu(mst->lpt_lnum));
371 printk(KERN_DEBUG "\tlpt_offs %u\n",
372 le32_to_cpu(mst->lpt_offs));
373 printk(KERN_DEBUG "\tnhead_lnum %u\n",
374 le32_to_cpu(mst->nhead_lnum));
375 printk(KERN_DEBUG "\tnhead_offs %u\n",
376 le32_to_cpu(mst->nhead_offs));
377 printk(KERN_DEBUG "\tltab_lnum %u\n",
378 le32_to_cpu(mst->ltab_lnum));
379 printk(KERN_DEBUG "\tltab_offs %u\n",
380 le32_to_cpu(mst->ltab_offs));
381 printk(KERN_DEBUG "\tlsave_lnum %u\n",
382 le32_to_cpu(mst->lsave_lnum));
383 printk(KERN_DEBUG "\tlsave_offs %u\n",
384 le32_to_cpu(mst->lsave_offs));
385 printk(KERN_DEBUG "\tlscan_lnum %u\n",
386 le32_to_cpu(mst->lscan_lnum));
387 printk(KERN_DEBUG "\tleb_cnt %u\n",
388 le32_to_cpu(mst->leb_cnt));
389 printk(KERN_DEBUG "\tempty_lebs %u\n",
390 le32_to_cpu(mst->empty_lebs));
391 printk(KERN_DEBUG "\tidx_lebs %u\n",
392 le32_to_cpu(mst->idx_lebs));
393 printk(KERN_DEBUG "\ttotal_free %llu\n",
394 (unsigned long long)le64_to_cpu(mst->total_free));
395 printk(KERN_DEBUG "\ttotal_dirty %llu\n",
396 (unsigned long long)le64_to_cpu(mst->total_dirty));
397 printk(KERN_DEBUG "\ttotal_used %llu\n",
398 (unsigned long long)le64_to_cpu(mst->total_used));
399 printk(KERN_DEBUG "\ttotal_dead %llu\n",
400 (unsigned long long)le64_to_cpu(mst->total_dead));
401 printk(KERN_DEBUG "\ttotal_dark %llu\n",
402 (unsigned long long)le64_to_cpu(mst->total_dark));
403 break;
404 }
405 case UBIFS_REF_NODE:
406 {
407 const struct ubifs_ref_node *ref = node;
408
409 printk(KERN_DEBUG "\tlnum %u\n",
410 le32_to_cpu(ref->lnum));
411 printk(KERN_DEBUG "\toffs %u\n",
412 le32_to_cpu(ref->offs));
413 printk(KERN_DEBUG "\tjhead %u\n",
414 le32_to_cpu(ref->jhead));
415 break;
416 }
417 case UBIFS_INO_NODE:
418 {
419 const struct ubifs_ino_node *ino = node;
420
421 key_read(c, &ino->key, &key);
422 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
423 printk(KERN_DEBUG "\tcreat_sqnum %llu\n",
424 (unsigned long long)le64_to_cpu(ino->creat_sqnum));
425 printk(KERN_DEBUG "\tsize %llu\n",
426 (unsigned long long)le64_to_cpu(ino->size));
427 printk(KERN_DEBUG "\tnlink %u\n",
428 le32_to_cpu(ino->nlink));
429 printk(KERN_DEBUG "\tatime %lld.%u\n",
430 (long long)le64_to_cpu(ino->atime_sec),
431 le32_to_cpu(ino->atime_nsec));
432 printk(KERN_DEBUG "\tmtime %lld.%u\n",
433 (long long)le64_to_cpu(ino->mtime_sec),
434 le32_to_cpu(ino->mtime_nsec));
435 printk(KERN_DEBUG "\tctime %lld.%u\n",
436 (long long)le64_to_cpu(ino->ctime_sec),
437 le32_to_cpu(ino->ctime_nsec));
438 printk(KERN_DEBUG "\tuid %u\n",
439 le32_to_cpu(ino->uid));
440 printk(KERN_DEBUG "\tgid %u\n",
441 le32_to_cpu(ino->gid));
442 printk(KERN_DEBUG "\tmode %u\n",
443 le32_to_cpu(ino->mode));
444 printk(KERN_DEBUG "\tflags %#x\n",
445 le32_to_cpu(ino->flags));
446 printk(KERN_DEBUG "\txattr_cnt %u\n",
447 le32_to_cpu(ino->xattr_cnt));
448 printk(KERN_DEBUG "\txattr_size %u\n",
449 le32_to_cpu(ino->xattr_size));
450 printk(KERN_DEBUG "\txattr_names %u\n",
451 le32_to_cpu(ino->xattr_names));
452 printk(KERN_DEBUG "\tcompr_type %#x\n",
453 (int)le16_to_cpu(ino->compr_type));
454 printk(KERN_DEBUG "\tdata len %u\n",
455 le32_to_cpu(ino->data_len));
456 break;
457 }
458 case UBIFS_DENT_NODE:
459 case UBIFS_XENT_NODE:
460 {
461 const struct ubifs_dent_node *dent = node;
462 int nlen = le16_to_cpu(dent->nlen);
463
464 key_read(c, &dent->key, &key);
465 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
466 printk(KERN_DEBUG "\tinum %llu\n",
467 (unsigned long long)le64_to_cpu(dent->inum));
468 printk(KERN_DEBUG "\ttype %d\n", (int)dent->type);
469 printk(KERN_DEBUG "\tnlen %d\n", nlen);
470 printk(KERN_DEBUG "\tname ");
471
472 if (nlen > UBIFS_MAX_NLEN)
473 printk(KERN_DEBUG "(bad name length, not printing, "
474 "bad or corrupted node)");
475 else {
476 for (i = 0; i < nlen && dent->name[i]; i++)
477 printk("%c", dent->name[i]);
478 }
479 printk("\n");
480
481 break;
482 }
483 case UBIFS_DATA_NODE:
484 {
485 const struct ubifs_data_node *dn = node;
486 int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
487
488 key_read(c, &dn->key, &key);
489 printk(KERN_DEBUG "\tkey %s\n", DBGKEY(&key));
490 printk(KERN_DEBUG "\tsize %u\n",
491 le32_to_cpu(dn->size));
492 printk(KERN_DEBUG "\tcompr_typ %d\n",
493 (int)le16_to_cpu(dn->compr_type));
494 printk(KERN_DEBUG "\tdata size %d\n",
495 dlen);
496 printk(KERN_DEBUG "\tdata:\n");
497 print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET, 32, 1,
498 (void *)&dn->data, dlen, 0);
499 break;
500 }
501 case UBIFS_TRUN_NODE:
502 {
503 const struct ubifs_trun_node *trun = node;
504
505 printk(KERN_DEBUG "\tinum %u\n",
506 le32_to_cpu(trun->inum));
507 printk(KERN_DEBUG "\told_size %llu\n",
508 (unsigned long long)le64_to_cpu(trun->old_size));
509 printk(KERN_DEBUG "\tnew_size %llu\n",
510 (unsigned long long)le64_to_cpu(trun->new_size));
511 break;
512 }
513 case UBIFS_IDX_NODE:
514 {
515 const struct ubifs_idx_node *idx = node;
516
517 n = le16_to_cpu(idx->child_cnt);
518 printk(KERN_DEBUG "\tchild_cnt %d\n", n);
519 printk(KERN_DEBUG "\tlevel %d\n",
520 (int)le16_to_cpu(idx->level));
521 printk(KERN_DEBUG "\tBranches:\n");
522
523 for (i = 0; i < n && i < c->fanout - 1; i++) {
524 const struct ubifs_branch *br;
525
526 br = ubifs_idx_branch(c, idx, i);
527 key_read(c, &br->key, &key);
528 printk(KERN_DEBUG "\t%d: LEB %d:%d len %d key %s\n",
529 i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
530 le32_to_cpu(br->len), DBGKEY(&key));
531 }
532 break;
533 }
534 case UBIFS_CS_NODE:
535 break;
536 case UBIFS_ORPH_NODE:
537 {
538 const struct ubifs_orph_node *orph = node;
539
540 printk(KERN_DEBUG "\tcommit number %llu\n",
541 (unsigned long long)
542 le64_to_cpu(orph->cmt_no) & LLONG_MAX);
543 printk(KERN_DEBUG "\tlast node flag %llu\n",
544 (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
545 n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
546 printk(KERN_DEBUG "\t%d orphan inode numbers:\n", n);
547 for (i = 0; i < n; i++)
548 printk(KERN_DEBUG "\t ino %llu\n",
7424bac8 549 (unsigned long long)le64_to_cpu(orph->inos[i]));
1e51764a
AB
550 break;
551 }
552 default:
553 printk(KERN_DEBUG "node type %d was not recognized\n",
554 (int)ch->node_type);
555 }
556 spin_unlock(&dbg_lock);
557}
558
559void dbg_dump_budget_req(const struct ubifs_budget_req *req)
560{
561 spin_lock(&dbg_lock);
562 printk(KERN_DEBUG "Budgeting request: new_ino %d, dirtied_ino %d\n",
563 req->new_ino, req->dirtied_ino);
564 printk(KERN_DEBUG "\tnew_ino_d %d, dirtied_ino_d %d\n",
565 req->new_ino_d, req->dirtied_ino_d);
566 printk(KERN_DEBUG "\tnew_page %d, dirtied_page %d\n",
567 req->new_page, req->dirtied_page);
568 printk(KERN_DEBUG "\tnew_dent %d, mod_dent %d\n",
569 req->new_dent, req->mod_dent);
570 printk(KERN_DEBUG "\tidx_growth %d\n", req->idx_growth);
571 printk(KERN_DEBUG "\tdata_growth %d dd_growth %d\n",
572 req->data_growth, req->dd_growth);
573 spin_unlock(&dbg_lock);
574}
575
576void dbg_dump_lstats(const struct ubifs_lp_stats *lst)
577{
578 spin_lock(&dbg_lock);
1de94159
AB
579 printk(KERN_DEBUG "(pid %d) Lprops statistics: empty_lebs %d, "
580 "idx_lebs %d\n", current->pid, lst->empty_lebs, lst->idx_lebs);
1e51764a
AB
581 printk(KERN_DEBUG "\ttaken_empty_lebs %d, total_free %lld, "
582 "total_dirty %lld\n", lst->taken_empty_lebs, lst->total_free,
583 lst->total_dirty);
584 printk(KERN_DEBUG "\ttotal_used %lld, total_dark %lld, "
585 "total_dead %lld\n", lst->total_used, lst->total_dark,
586 lst->total_dead);
587 spin_unlock(&dbg_lock);
588}
589
590void dbg_dump_budg(struct ubifs_info *c)
591{
592 int i;
593 struct rb_node *rb;
594 struct ubifs_bud *bud;
595 struct ubifs_gced_idx_leb *idx_gc;
596
597 spin_lock(&dbg_lock);
1de94159
AB
598 printk(KERN_DEBUG "(pid %d) Budgeting info: budg_data_growth %lld, "
599 "budg_dd_growth %lld, budg_idx_growth %lld\n", current->pid,
1e51764a
AB
600 c->budg_data_growth, c->budg_dd_growth, c->budg_idx_growth);
601 printk(KERN_DEBUG "\tdata budget sum %lld, total budget sum %lld, "
602 "freeable_cnt %d\n", c->budg_data_growth + c->budg_dd_growth,
603 c->budg_data_growth + c->budg_dd_growth + c->budg_idx_growth,
604 c->freeable_cnt);
605 printk(KERN_DEBUG "\tmin_idx_lebs %d, old_idx_sz %lld, "
606 "calc_idx_sz %lld, idx_gc_cnt %d\n", c->min_idx_lebs,
607 c->old_idx_sz, c->calc_idx_sz, c->idx_gc_cnt);
608 printk(KERN_DEBUG "\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, "
609 "clean_zn_cnt %ld\n", atomic_long_read(&c->dirty_pg_cnt),
610 atomic_long_read(&c->dirty_zn_cnt),
611 atomic_long_read(&c->clean_zn_cnt));
612 printk(KERN_DEBUG "\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n",
613 c->dark_wm, c->dead_wm, c->max_idx_node_sz);
614 printk(KERN_DEBUG "\tgc_lnum %d, ihead_lnum %d\n",
615 c->gc_lnum, c->ihead_lnum);
616 for (i = 0; i < c->jhead_cnt; i++)
617 printk(KERN_DEBUG "\tjhead %d\t LEB %d\n",
618 c->jheads[i].wbuf.jhead, c->jheads[i].wbuf.lnum);
619 for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {
620 bud = rb_entry(rb, struct ubifs_bud, rb);
621 printk(KERN_DEBUG "\tbud LEB %d\n", bud->lnum);
622 }
623 list_for_each_entry(bud, &c->old_buds, list)
624 printk(KERN_DEBUG "\told bud LEB %d\n", bud->lnum);
625 list_for_each_entry(idx_gc, &c->idx_gc, list)
626 printk(KERN_DEBUG "\tGC'ed idx LEB %d unmap %d\n",
627 idx_gc->lnum, idx_gc->unmap);
628 printk(KERN_DEBUG "\tcommit state %d\n", c->cmt_state);
629 spin_unlock(&dbg_lock);
630}
631
632void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
633{
634 printk(KERN_DEBUG "LEB %d lprops: free %d, dirty %d (used %d), "
635 "flags %#x\n", lp->lnum, lp->free, lp->dirty,
636 c->leb_size - lp->free - lp->dirty, lp->flags);
637}
638
639void dbg_dump_lprops(struct ubifs_info *c)
640{
641 int lnum, err;
642 struct ubifs_lprops lp;
643 struct ubifs_lp_stats lst;
644
1de94159 645 printk(KERN_DEBUG "(pid %d) Dumping LEB properties\n", current->pid);
1e51764a
AB
646 ubifs_get_lp_stats(c, &lst);
647 dbg_dump_lstats(&lst);
648
649 for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
650 err = ubifs_read_one_lp(c, lnum, &lp);
651 if (err)
652 ubifs_err("cannot read lprops for LEB %d", lnum);
653
654 dbg_dump_lprop(c, &lp);
655 }
656}
657
658void dbg_dump_leb(const struct ubifs_info *c, int lnum)
659{
660 struct ubifs_scan_leb *sleb;
661 struct ubifs_scan_node *snod;
662
663 if (dbg_failure_mode)
664 return;
665
1de94159 666 printk(KERN_DEBUG "(pid %d) Dumping LEB %d\n", current->pid, lnum);
1e51764a
AB
667
668 sleb = ubifs_scan(c, lnum, 0, c->dbg_buf);
669 if (IS_ERR(sleb)) {
670 ubifs_err("scan error %d", (int)PTR_ERR(sleb));
671 return;
672 }
673
674 printk(KERN_DEBUG "LEB %d has %d nodes ending at %d\n", lnum,
675 sleb->nodes_cnt, sleb->endpt);
676
677 list_for_each_entry(snod, &sleb->nodes, list) {
678 cond_resched();
679 printk(KERN_DEBUG "Dumping node at LEB %d:%d len %d\n", lnum,
680 snod->offs, snod->len);
681 dbg_dump_node(c, snod->node);
682 }
683
684 ubifs_scan_destroy(sleb);
685 return;
686}
687
688void dbg_dump_znode(const struct ubifs_info *c,
689 const struct ubifs_znode *znode)
690{
691 int n;
692 const struct ubifs_zbranch *zbr;
693
694 spin_lock(&dbg_lock);
695 if (znode->parent)
696 zbr = &znode->parent->zbranch[znode->iip];
697 else
698 zbr = &c->zroot;
699
700 printk(KERN_DEBUG "znode %p, LEB %d:%d len %d parent %p iip %d level %d"
701 " child_cnt %d flags %lx\n", znode, zbr->lnum, zbr->offs,
702 zbr->len, znode->parent, znode->iip, znode->level,
703 znode->child_cnt, znode->flags);
704
705 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
706 spin_unlock(&dbg_lock);
707 return;
708 }
709
710 printk(KERN_DEBUG "zbranches:\n");
711 for (n = 0; n < znode->child_cnt; n++) {
712 zbr = &znode->zbranch[n];
713 if (znode->level > 0)
714 printk(KERN_DEBUG "\t%d: znode %p LEB %d:%d len %d key "
715 "%s\n", n, zbr->znode, zbr->lnum,
716 zbr->offs, zbr->len,
717 DBGKEY(&zbr->key));
718 else
719 printk(KERN_DEBUG "\t%d: LNC %p LEB %d:%d len %d key "
720 "%s\n", n, zbr->znode, zbr->lnum,
721 zbr->offs, zbr->len,
722 DBGKEY(&zbr->key));
723 }
724 spin_unlock(&dbg_lock);
725}
726
727void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)
728{
729 int i;
730
1de94159
AB
731 printk(KERN_DEBUG "(pid %d) Dumping heap cat %d (%d elements)\n",
732 current->pid, cat, heap->cnt);
1e51764a
AB
733 for (i = 0; i < heap->cnt; i++) {
734 struct ubifs_lprops *lprops = heap->arr[i];
735
736 printk(KERN_DEBUG "\t%d. LEB %d hpos %d free %d dirty %d "
737 "flags %d\n", i, lprops->lnum, lprops->hpos,
738 lprops->free, lprops->dirty, lprops->flags);
739 }
740}
741
742void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
743 struct ubifs_nnode *parent, int iip)
744{
745 int i;
746
1de94159 747 printk(KERN_DEBUG "(pid %d) Dumping pnode:\n", current->pid);
1e51764a
AB
748 printk(KERN_DEBUG "\taddress %zx parent %zx cnext %zx\n",
749 (size_t)pnode, (size_t)parent, (size_t)pnode->cnext);
750 printk(KERN_DEBUG "\tflags %lu iip %d level %d num %d\n",
751 pnode->flags, iip, pnode->level, pnode->num);
752 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
753 struct ubifs_lprops *lp = &pnode->lprops[i];
754
755 printk(KERN_DEBUG "\t%d: free %d dirty %d flags %d lnum %d\n",
756 i, lp->free, lp->dirty, lp->flags, lp->lnum);
757 }
758}
759
760void dbg_dump_tnc(struct ubifs_info *c)
761{
762 struct ubifs_znode *znode;
763 int level;
764
765 printk(KERN_DEBUG "\n");
1de94159 766 printk(KERN_DEBUG "(pid %d) Dumping the TNC tree\n", current->pid);
1e51764a
AB
767 znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);
768 level = znode->level;
769 printk(KERN_DEBUG "== Level %d ==\n", level);
770 while (znode) {
771 if (level != znode->level) {
772 level = znode->level;
773 printk(KERN_DEBUG "== Level %d ==\n", level);
774 }
775 dbg_dump_znode(c, znode);
776 znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);
777 }
778
779 printk(KERN_DEBUG "\n");
780}
781
782static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,
783 void *priv)
784{
785 dbg_dump_znode(c, znode);
786 return 0;
787}
788
789/**
790 * dbg_dump_index - dump the on-flash index.
791 * @c: UBIFS file-system description object
792 *
793 * This function dumps whole UBIFS indexing B-tree, unlike 'dbg_dump_tnc()'
794 * which dumps only in-memory znodes and does not read znodes which from flash.
795 */
796void dbg_dump_index(struct ubifs_info *c)
797{
798 dbg_walk_index(c, NULL, dump_znode, NULL);
799}
800
801/**
802 * dbg_check_synced_i_size - check synchronized inode size.
803 * @inode: inode to check
804 *
805 * If inode is clean, synchronized inode size has to be equivalent to current
806 * inode size. This function has to be called only for locked inodes (@i_mutex
807 * has to be locked). Returns %0 if synchronized inode size if correct, and
808 * %-EINVAL if not.
809 */
810int dbg_check_synced_i_size(struct inode *inode)
811{
812 int err = 0;
813 struct ubifs_inode *ui = ubifs_inode(inode);
814
815 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
816 return 0;
817 if (!S_ISREG(inode->i_mode))
818 return 0;
819
820 mutex_lock(&ui->ui_mutex);
821 spin_lock(&ui->ui_lock);
822 if (ui->ui_size != ui->synced_i_size && !ui->dirty) {
823 ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode "
824 "is clean", ui->ui_size, ui->synced_i_size);
825 ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,
826 inode->i_mode, i_size_read(inode));
827 dbg_dump_stack();
828 err = -EINVAL;
829 }
830 spin_unlock(&ui->ui_lock);
831 mutex_unlock(&ui->ui_mutex);
832 return err;
833}
834
835/*
836 * dbg_check_dir - check directory inode size and link count.
837 * @c: UBIFS file-system description object
838 * @dir: the directory to calculate size for
839 * @size: the result is returned here
840 *
841 * This function makes sure that directory size and link count are correct.
842 * Returns zero in case of success and a negative error code in case of
843 * failure.
844 *
845 * Note, it is good idea to make sure the @dir->i_mutex is locked before
846 * calling this function.
847 */
848int dbg_check_dir_size(struct ubifs_info *c, const struct inode *dir)
849{
850 unsigned int nlink = 2;
851 union ubifs_key key;
852 struct ubifs_dent_node *dent, *pdent = NULL;
853 struct qstr nm = { .name = NULL };
854 loff_t size = UBIFS_INO_NODE_SZ;
855
856 if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
857 return 0;
858
859 if (!S_ISDIR(dir->i_mode))
860 return 0;
861
862 lowest_dent_key(c, &key, dir->i_ino);
863 while (1) {
864 int err;
865
866 dent = ubifs_tnc_next_ent(c, &key, &nm);
867 if (IS_ERR(dent)) {
868 err = PTR_ERR(dent);
869 if (err == -ENOENT)
870 break;
871 return err;
872 }
873
874 nm.name = dent->name;
875 nm.len = le16_to_cpu(dent->nlen);
876 size += CALC_DENT_SIZE(nm.len);
877 if (dent->type == UBIFS_ITYPE_DIR)
878 nlink += 1;
879 kfree(pdent);
880 pdent = dent;
881 key_read(c, &dent->key, &key);
882 }
883 kfree(pdent);
884
885 if (i_size_read(dir) != size) {
886 ubifs_err("directory inode %lu has size %llu, "
887 "but calculated size is %llu", dir->i_ino,
888 (unsigned long long)i_size_read(dir),
889 (unsigned long long)size);
890 dump_stack();
891 return -EINVAL;
892 }
893 if (dir->i_nlink != nlink) {
894 ubifs_err("directory inode %lu has nlink %u, but calculated "
895 "nlink is %u", dir->i_ino, dir->i_nlink, nlink);
896 dump_stack();
897 return -EINVAL;
898 }
899
900 return 0;
901}
902
903/**
904 * dbg_check_key_order - make sure that colliding keys are properly ordered.
905 * @c: UBIFS file-system description object
906 * @zbr1: first zbranch
907 * @zbr2: following zbranch
908 *
909 * In UBIFS indexing B-tree colliding keys has to be sorted in binary order of
910 * names of the direntries/xentries which are referred by the keys. This
911 * function reads direntries/xentries referred by @zbr1 and @zbr2 and makes
912 * sure the name of direntry/xentry referred by @zbr1 is less than
913 * direntry/xentry referred by @zbr2. Returns zero if this is true, %1 if not,
914 * and a negative error code in case of failure.
915 */
916static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,
917 struct ubifs_zbranch *zbr2)
918{
919 int err, nlen1, nlen2, cmp;
920 struct ubifs_dent_node *dent1, *dent2;
921 union ubifs_key key;
922
923 ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));
924 dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
925 if (!dent1)
926 return -ENOMEM;
927 dent2 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS);
928 if (!dent2) {
929 err = -ENOMEM;
930 goto out_free;
931 }
932
933 err = ubifs_tnc_read_node(c, zbr1, dent1);
934 if (err)
935 goto out_free;
936 err = ubifs_validate_entry(c, dent1);
937 if (err)
938 goto out_free;
939
940 err = ubifs_tnc_read_node(c, zbr2, dent2);
941 if (err)
942 goto out_free;
943 err = ubifs_validate_entry(c, dent2);
944 if (err)
945 goto out_free;
946
947 /* Make sure node keys are the same as in zbranch */
948 err = 1;
949 key_read(c, &dent1->key, &key);
950 if (keys_cmp(c, &zbr1->key, &key)) {
951 dbg_err("1st entry at %d:%d has key %s", zbr1->lnum,
952 zbr1->offs, DBGKEY(&key));
953 dbg_err("but it should have key %s according to tnc",
954 DBGKEY(&zbr1->key));
955 dbg_dump_node(c, dent1);
956 goto out_free;
957 }
958
959 key_read(c, &dent2->key, &key);
960 if (keys_cmp(c, &zbr2->key, &key)) {
961 dbg_err("2nd entry at %d:%d has key %s", zbr1->lnum,
962 zbr1->offs, DBGKEY(&key));
963 dbg_err("but it should have key %s according to tnc",
964 DBGKEY(&zbr2->key));
965 dbg_dump_node(c, dent2);
966 goto out_free;
967 }
968
969 nlen1 = le16_to_cpu(dent1->nlen);
970 nlen2 = le16_to_cpu(dent2->nlen);
971
972 cmp = memcmp(dent1->name, dent2->name, min_t(int, nlen1, nlen2));
973 if (cmp < 0 || (cmp == 0 && nlen1 < nlen2)) {
974 err = 0;
975 goto out_free;
976 }
977 if (cmp == 0 && nlen1 == nlen2)
978 dbg_err("2 xent/dent nodes with the same name");
979 else
980 dbg_err("bad order of colliding key %s",
981 DBGKEY(&key));
982
983 dbg_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs);
984 dbg_dump_node(c, dent1);
985 dbg_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs);
986 dbg_dump_node(c, dent2);
987
988out_free:
989 kfree(dent2);
990 kfree(dent1);
991 return err;
992}
993
994/**
995 * dbg_check_znode - check if znode is all right.
996 * @c: UBIFS file-system description object
997 * @zbr: zbranch which points to this znode
998 *
999 * This function makes sure that znode referred to by @zbr is all right.
1000 * Returns zero if it is, and %-EINVAL if it is not.
1001 */
1002static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)
1003{
1004 struct ubifs_znode *znode = zbr->znode;
1005 struct ubifs_znode *zp = znode->parent;
1006 int n, err, cmp;
1007
1008 if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {
1009 err = 1;
1010 goto out;
1011 }
1012 if (znode->level < 0) {
1013 err = 2;
1014 goto out;
1015 }
1016 if (znode->iip < 0 || znode->iip >= c->fanout) {
1017 err = 3;
1018 goto out;
1019 }
1020
1021 if (zbr->len == 0)
1022 /* Only dirty zbranch may have no on-flash nodes */
1023 if (!ubifs_zn_dirty(znode)) {
1024 err = 4;
1025 goto out;
1026 }
1027
1028 if (ubifs_zn_dirty(znode)) {
1029 /*
1030 * If znode is dirty, its parent has to be dirty as well. The
1031 * order of the operation is important, so we have to have
1032 * memory barriers.
1033 */
1034 smp_mb();
1035 if (zp && !ubifs_zn_dirty(zp)) {
1036 /*
1037 * The dirty flag is atomic and is cleared outside the
1038 * TNC mutex, so znode's dirty flag may now have
1039 * been cleared. The child is always cleared before the
1040 * parent, so we just need to check again.
1041 */
1042 smp_mb();
1043 if (ubifs_zn_dirty(znode)) {
1044 err = 5;
1045 goto out;
1046 }
1047 }
1048 }
1049
1050 if (zp) {
1051 const union ubifs_key *min, *max;
1052
1053 if (znode->level != zp->level - 1) {
1054 err = 6;
1055 goto out;
1056 }
1057
1058 /* Make sure the 'parent' pointer in our znode is correct */
1059 err = ubifs_search_zbranch(c, zp, &zbr->key, &n);
1060 if (!err) {
1061 /* This zbranch does not exist in the parent */
1062 err = 7;
1063 goto out;
1064 }
1065
1066 if (znode->iip >= zp->child_cnt) {
1067 err = 8;
1068 goto out;
1069 }
1070
1071 if (znode->iip != n) {
1072 /* This may happen only in case of collisions */
1073 if (keys_cmp(c, &zp->zbranch[n].key,
1074 &zp->zbranch[znode->iip].key)) {
1075 err = 9;
1076 goto out;
1077 }
1078 n = znode->iip;
1079 }
1080
1081 /*
1082 * Make sure that the first key in our znode is greater than or
1083 * equal to the key in the pointing zbranch.
1084 */
1085 min = &zbr->key;
1086 cmp = keys_cmp(c, min, &znode->zbranch[0].key);
1087 if (cmp == 1) {
1088 err = 10;
1089 goto out;
1090 }
1091
1092 if (n + 1 < zp->child_cnt) {
1093 max = &zp->zbranch[n + 1].key;
1094
1095 /*
1096 * Make sure the last key in our znode is less or
1097 * equivalent than the the key in zbranch which goes
1098 * after our pointing zbranch.
1099 */
1100 cmp = keys_cmp(c, max,
1101 &znode->zbranch[znode->child_cnt - 1].key);
1102 if (cmp == -1) {
1103 err = 11;
1104 goto out;
1105 }
1106 }
1107 } else {
1108 /* This may only be root znode */
1109 if (zbr != &c->zroot) {
1110 err = 12;
1111 goto out;
1112 }
1113 }
1114
1115 /*
1116 * Make sure that next key is greater or equivalent then the previous
1117 * one.
1118 */
1119 for (n = 1; n < znode->child_cnt; n++) {
1120 cmp = keys_cmp(c, &znode->zbranch[n - 1].key,
1121 &znode->zbranch[n].key);
1122 if (cmp > 0) {
1123 err = 13;
1124 goto out;
1125 }
1126 if (cmp == 0) {
1127 /* This can only be keys with colliding hash */
1128 if (!is_hash_key(c, &znode->zbranch[n].key)) {
1129 err = 14;
1130 goto out;
1131 }
1132
1133 if (znode->level != 0 || c->replaying)
1134 continue;
1135
1136 /*
1137 * Colliding keys should follow binary order of
1138 * corresponding xentry/dentry names.
1139 */
1140 err = dbg_check_key_order(c, &znode->zbranch[n - 1],
1141 &znode->zbranch[n]);
1142 if (err < 0)
1143 return err;
1144 if (err) {
1145 err = 15;
1146 goto out;
1147 }
1148 }
1149 }
1150
1151 for (n = 0; n < znode->child_cnt; n++) {
1152 if (!znode->zbranch[n].znode &&
1153 (znode->zbranch[n].lnum == 0 ||
1154 znode->zbranch[n].len == 0)) {
1155 err = 16;
1156 goto out;
1157 }
1158
1159 if (znode->zbranch[n].lnum != 0 &&
1160 znode->zbranch[n].len == 0) {
1161 err = 17;
1162 goto out;
1163 }
1164
1165 if (znode->zbranch[n].lnum == 0 &&
1166 znode->zbranch[n].len != 0) {
1167 err = 18;
1168 goto out;
1169 }
1170
1171 if (znode->zbranch[n].lnum == 0 &&
1172 znode->zbranch[n].offs != 0) {
1173 err = 19;
1174 goto out;
1175 }
1176
1177 if (znode->level != 0 && znode->zbranch[n].znode)
1178 if (znode->zbranch[n].znode->parent != znode) {
1179 err = 20;
1180 goto out;
1181 }
1182 }
1183
1184 return 0;
1185
1186out:
1187 ubifs_err("failed, error %d", err);
1188 ubifs_msg("dump of the znode");
1189 dbg_dump_znode(c, znode);
1190 if (zp) {
1191 ubifs_msg("dump of the parent znode");
1192 dbg_dump_znode(c, zp);
1193 }
1194 dump_stack();
1195 return -EINVAL;
1196}
1197
1198/**
1199 * dbg_check_tnc - check TNC tree.
1200 * @c: UBIFS file-system description object
1201 * @extra: do extra checks that are possible at start commit
1202 *
1203 * This function traverses whole TNC tree and checks every znode. Returns zero
1204 * if everything is all right and %-EINVAL if something is wrong with TNC.
1205 */
1206int dbg_check_tnc(struct ubifs_info *c, int extra)
1207{
1208 struct ubifs_znode *znode;
1209 long clean_cnt = 0, dirty_cnt = 0;
1210 int err, last;
1211
1212 if (!(ubifs_chk_flags & UBIFS_CHK_TNC))
1213 return 0;
1214
1215 ubifs_assert(mutex_is_locked(&c->tnc_mutex));
1216 if (!c->zroot.znode)
1217 return 0;
1218
1219 znode = ubifs_tnc_postorder_first(c->zroot.znode);
1220 while (1) {
1221 struct ubifs_znode *prev;
1222 struct ubifs_zbranch *zbr;
1223
1224 if (!znode->parent)
1225 zbr = &c->zroot;
1226 else
1227 zbr = &znode->parent->zbranch[znode->iip];
1228
1229 err = dbg_check_znode(c, zbr);
1230 if (err)
1231 return err;
1232
1233 if (extra) {
1234 if (ubifs_zn_dirty(znode))
1235 dirty_cnt += 1;
1236 else
1237 clean_cnt += 1;
1238 }
1239
1240 prev = znode;
1241 znode = ubifs_tnc_postorder_next(znode);
1242 if (!znode)
1243 break;
1244
1245 /*
1246 * If the last key of this znode is equivalent to the first key
1247 * of the next znode (collision), then check order of the keys.
1248 */
1249 last = prev->child_cnt - 1;
1250 if (prev->level == 0 && znode->level == 0 && !c->replaying &&
1251 !keys_cmp(c, &prev->zbranch[last].key,
1252 &znode->zbranch[0].key)) {
1253 err = dbg_check_key_order(c, &prev->zbranch[last],
1254 &znode->zbranch[0]);
1255 if (err < 0)
1256 return err;
1257 if (err) {
1258 ubifs_msg("first znode");
1259 dbg_dump_znode(c, prev);
1260 ubifs_msg("second znode");
1261 dbg_dump_znode(c, znode);
1262 return -EINVAL;
1263 }
1264 }
1265 }
1266
1267 if (extra) {
1268 if (clean_cnt != atomic_long_read(&c->clean_zn_cnt)) {
1269 ubifs_err("incorrect clean_zn_cnt %ld, calculated %ld",
1270 atomic_long_read(&c->clean_zn_cnt),
1271 clean_cnt);
1272 return -EINVAL;
1273 }
1274 if (dirty_cnt != atomic_long_read(&c->dirty_zn_cnt)) {
1275 ubifs_err("incorrect dirty_zn_cnt %ld, calculated %ld",
1276 atomic_long_read(&c->dirty_zn_cnt),
1277 dirty_cnt);
1278 return -EINVAL;
1279 }
1280 }
1281
1282 return 0;
1283}
1284
1285/**
1286 * dbg_walk_index - walk the on-flash index.
1287 * @c: UBIFS file-system description object
1288 * @leaf_cb: called for each leaf node
1289 * @znode_cb: called for each indexing node
1290 * @priv: private date which is passed to callbacks
1291 *
1292 * This function walks the UBIFS index and calls the @leaf_cb for each leaf
1293 * node and @znode_cb for each indexing node. Returns zero in case of success
1294 * and a negative error code in case of failure.
1295 *
1296 * It would be better if this function removed every znode it pulled to into
1297 * the TNC, so that the behavior more closely matched the non-debugging
1298 * behavior.
1299 */
1300int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,
1301 dbg_znode_callback znode_cb, void *priv)
1302{
1303 int err;
1304 struct ubifs_zbranch *zbr;
1305 struct ubifs_znode *znode, *child;
1306
1307 mutex_lock(&c->tnc_mutex);
1308 /* If the root indexing node is not in TNC - pull it */
1309 if (!c->zroot.znode) {
1310 c->zroot.znode = ubifs_load_znode(c, &c->zroot, NULL, 0);
1311 if (IS_ERR(c->zroot.znode)) {
1312 err = PTR_ERR(c->zroot.znode);
1313 c->zroot.znode = NULL;
1314 goto out_unlock;
1315 }
1316 }
1317
1318 /*
1319 * We are going to traverse the indexing tree in the postorder manner.
1320 * Go down and find the leftmost indexing node where we are going to
1321 * start from.
1322 */
1323 znode = c->zroot.znode;
1324 while (znode->level > 0) {
1325 zbr = &znode->zbranch[0];
1326 child = zbr->znode;
1327 if (!child) {
1328 child = ubifs_load_znode(c, zbr, znode, 0);
1329 if (IS_ERR(child)) {
1330 err = PTR_ERR(child);
1331 goto out_unlock;
1332 }
1333 zbr->znode = child;
1334 }
1335
1336 znode = child;
1337 }
1338
1339 /* Iterate over all indexing nodes */
1340 while (1) {
1341 int idx;
1342
1343 cond_resched();
1344
1345 if (znode_cb) {
1346 err = znode_cb(c, znode, priv);
1347 if (err) {
1348 ubifs_err("znode checking function returned "
1349 "error %d", err);
1350 dbg_dump_znode(c, znode);
1351 goto out_dump;
1352 }
1353 }
1354 if (leaf_cb && znode->level == 0) {
1355 for (idx = 0; idx < znode->child_cnt; idx++) {
1356 zbr = &znode->zbranch[idx];
1357 err = leaf_cb(c, zbr, priv);
1358 if (err) {
1359 ubifs_err("leaf checking function "
1360 "returned error %d, for leaf "
1361 "at LEB %d:%d",
1362 err, zbr->lnum, zbr->offs);
1363 goto out_dump;
1364 }
1365 }
1366 }
1367
1368 if (!znode->parent)
1369 break;
1370
1371 idx = znode->iip + 1;
1372 znode = znode->parent;
1373 if (idx < znode->child_cnt) {
1374 /* Switch to the next index in the parent */
1375 zbr = &znode->zbranch[idx];
1376 child = zbr->znode;
1377 if (!child) {
1378 child = ubifs_load_znode(c, zbr, znode, idx);
1379 if (IS_ERR(child)) {
1380 err = PTR_ERR(child);
1381 goto out_unlock;
1382 }
1383 zbr->znode = child;
1384 }
1385 znode = child;
1386 } else
1387 /*
1388 * This is the last child, switch to the parent and
1389 * continue.
1390 */
1391 continue;
1392
1393 /* Go to the lowest leftmost znode in the new sub-tree */
1394 while (znode->level > 0) {
1395 zbr = &znode->zbranch[0];
1396 child = zbr->znode;
1397 if (!child) {
1398 child = ubifs_load_znode(c, zbr, znode, 0);
1399 if (IS_ERR(child)) {
1400 err = PTR_ERR(child);
1401 goto out_unlock;
1402 }
1403 zbr->znode = child;
1404 }
1405 znode = child;
1406 }
1407 }
1408
1409 mutex_unlock(&c->tnc_mutex);
1410 return 0;
1411
1412out_dump:
1413 if (znode->parent)
1414 zbr = &znode->parent->zbranch[znode->iip];
1415 else
1416 zbr = &c->zroot;
1417 ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs);
1418 dbg_dump_znode(c, znode);
1419out_unlock:
1420 mutex_unlock(&c->tnc_mutex);
1421 return err;
1422}
1423
1424/**
1425 * add_size - add znode size to partially calculated index size.
1426 * @c: UBIFS file-system description object
1427 * @znode: znode to add size for
1428 * @priv: partially calculated index size
1429 *
1430 * This is a helper function for 'dbg_check_idx_size()' which is called for
1431 * every indexing node and adds its size to the 'long long' variable pointed to
1432 * by @priv.
1433 */
1434static int add_size(struct ubifs_info *c, struct ubifs_znode *znode, void *priv)
1435{
1436 long long *idx_size = priv;
1437 int add;
1438
1439 add = ubifs_idx_node_sz(c, znode->child_cnt);
1440 add = ALIGN(add, 8);
1441 *idx_size += add;
1442 return 0;
1443}
1444
1445/**
1446 * dbg_check_idx_size - check index size.
1447 * @c: UBIFS file-system description object
1448 * @idx_size: size to check
1449 *
1450 * This function walks the UBIFS index, calculates its size and checks that the
1451 * size is equivalent to @idx_size. Returns zero in case of success and a
1452 * negative error code in case of failure.
1453 */
1454int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)
1455{
1456 int err;
1457 long long calc = 0;
1458
1459 if (!(ubifs_chk_flags & UBIFS_CHK_IDX_SZ))
1460 return 0;
1461
1462 err = dbg_walk_index(c, NULL, add_size, &calc);
1463 if (err) {
1464 ubifs_err("error %d while walking the index", err);
1465 return err;
1466 }
1467
1468 if (calc != idx_size) {
1469 ubifs_err("index size check failed: calculated size is %lld, "
1470 "should be %lld", calc, idx_size);
1471 dump_stack();
1472 return -EINVAL;
1473 }
1474
1475 return 0;
1476}
1477
1478/**
1479 * struct fsck_inode - information about an inode used when checking the file-system.
1480 * @rb: link in the RB-tree of inodes
1481 * @inum: inode number
1482 * @mode: inode type, permissions, etc
1483 * @nlink: inode link count
1484 * @xattr_cnt: count of extended attributes
1485 * @references: how many directory/xattr entries refer this inode (calculated
1486 * while walking the index)
1487 * @calc_cnt: for directory inode count of child directories
1488 * @size: inode size (read from on-flash inode)
1489 * @xattr_sz: summary size of all extended attributes (read from on-flash
1490 * inode)
1491 * @calc_sz: for directories calculated directory size
1492 * @calc_xcnt: count of extended attributes
1493 * @calc_xsz: calculated summary size of all extended attributes
1494 * @xattr_nms: sum of lengths of all extended attribute names belonging to this
1495 * inode (read from on-flash inode)
1496 * @calc_xnms: calculated sum of lengths of all extended attribute names
1497 */
1498struct fsck_inode {
1499 struct rb_node rb;
1500 ino_t inum;
1501 umode_t mode;
1502 unsigned int nlink;
1503 unsigned int xattr_cnt;
1504 int references;
1505 int calc_cnt;
1506 long long size;
1507 unsigned int xattr_sz;
1508 long long calc_sz;
1509 long long calc_xcnt;
1510 long long calc_xsz;
1511 unsigned int xattr_nms;
1512 long long calc_xnms;
1513};
1514
1515/**
1516 * struct fsck_data - private FS checking information.
1517 * @inodes: RB-tree of all inodes (contains @struct fsck_inode objects)
1518 */
1519struct fsck_data {
1520 struct rb_root inodes;
1521};
1522
1523/**
1524 * add_inode - add inode information to RB-tree of inodes.
1525 * @c: UBIFS file-system description object
1526 * @fsckd: FS checking information
1527 * @ino: raw UBIFS inode to add
1528 *
1529 * This is a helper function for 'check_leaf()' which adds information about
1530 * inode @ino to the RB-tree of inodes. Returns inode information pointer in
1531 * case of success and a negative error code in case of failure.
1532 */
1533static struct fsck_inode *add_inode(struct ubifs_info *c,
1534 struct fsck_data *fsckd,
1535 struct ubifs_ino_node *ino)
1536{
1537 struct rb_node **p, *parent = NULL;
1538 struct fsck_inode *fscki;
1539 ino_t inum = key_inum_flash(c, &ino->key);
1540
1541 p = &fsckd->inodes.rb_node;
1542 while (*p) {
1543 parent = *p;
1544 fscki = rb_entry(parent, struct fsck_inode, rb);
1545 if (inum < fscki->inum)
1546 p = &(*p)->rb_left;
1547 else if (inum > fscki->inum)
1548 p = &(*p)->rb_right;
1549 else
1550 return fscki;
1551 }
1552
1553 if (inum > c->highest_inum) {
1554 ubifs_err("too high inode number, max. is %lu",
1555 c->highest_inum);
1556 return ERR_PTR(-EINVAL);
1557 }
1558
1559 fscki = kzalloc(sizeof(struct fsck_inode), GFP_NOFS);
1560 if (!fscki)
1561 return ERR_PTR(-ENOMEM);
1562
1563 fscki->inum = inum;
1564 fscki->nlink = le32_to_cpu(ino->nlink);
1565 fscki->size = le64_to_cpu(ino->size);
1566 fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
1567 fscki->xattr_sz = le32_to_cpu(ino->xattr_size);
1568 fscki->xattr_nms = le32_to_cpu(ino->xattr_names);
1569 fscki->mode = le32_to_cpu(ino->mode);
1570 if (S_ISDIR(fscki->mode)) {
1571 fscki->calc_sz = UBIFS_INO_NODE_SZ;
1572 fscki->calc_cnt = 2;
1573 }
1574 rb_link_node(&fscki->rb, parent, p);
1575 rb_insert_color(&fscki->rb, &fsckd->inodes);
1576 return fscki;
1577}
1578
1579/**
1580 * search_inode - search inode in the RB-tree of inodes.
1581 * @fsckd: FS checking information
1582 * @inum: inode number to search
1583 *
1584 * This is a helper function for 'check_leaf()' which searches inode @inum in
1585 * the RB-tree of inodes and returns an inode information pointer or %NULL if
1586 * the inode was not found.
1587 */
1588static struct fsck_inode *search_inode(struct fsck_data *fsckd, ino_t inum)
1589{
1590 struct rb_node *p;
1591 struct fsck_inode *fscki;
1592
1593 p = fsckd->inodes.rb_node;
1594 while (p) {
1595 fscki = rb_entry(p, struct fsck_inode, rb);
1596 if (inum < fscki->inum)
1597 p = p->rb_left;
1598 else if (inum > fscki->inum)
1599 p = p->rb_right;
1600 else
1601 return fscki;
1602 }
1603 return NULL;
1604}
1605
1606/**
1607 * read_add_inode - read inode node and add it to RB-tree of inodes.
1608 * @c: UBIFS file-system description object
1609 * @fsckd: FS checking information
1610 * @inum: inode number to read
1611 *
1612 * This is a helper function for 'check_leaf()' which finds inode node @inum in
1613 * the index, reads it, and adds it to the RB-tree of inodes. Returns inode
1614 * information pointer in case of success and a negative error code in case of
1615 * failure.
1616 */
1617static struct fsck_inode *read_add_inode(struct ubifs_info *c,
1618 struct fsck_data *fsckd, ino_t inum)
1619{
1620 int n, err;
1621 union ubifs_key key;
1622 struct ubifs_znode *znode;
1623 struct ubifs_zbranch *zbr;
1624 struct ubifs_ino_node *ino;
1625 struct fsck_inode *fscki;
1626
1627 fscki = search_inode(fsckd, inum);
1628 if (fscki)
1629 return fscki;
1630
1631 ino_key_init(c, &key, inum);
1632 err = ubifs_lookup_level0(c, &key, &znode, &n);
1633 if (!err) {
1634 ubifs_err("inode %lu not found in index", inum);
1635 return ERR_PTR(-ENOENT);
1636 } else if (err < 0) {
1637 ubifs_err("error %d while looking up inode %lu", err, inum);
1638 return ERR_PTR(err);
1639 }
1640
1641 zbr = &znode->zbranch[n];
1642 if (zbr->len < UBIFS_INO_NODE_SZ) {
1643 ubifs_err("bad node %lu node length %d", inum, zbr->len);
1644 return ERR_PTR(-EINVAL);
1645 }
1646
1647 ino = kmalloc(zbr->len, GFP_NOFS);
1648 if (!ino)
1649 return ERR_PTR(-ENOMEM);
1650
1651 err = ubifs_tnc_read_node(c, zbr, ino);
1652 if (err) {
1653 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
1654 zbr->lnum, zbr->offs, err);
1655 kfree(ino);
1656 return ERR_PTR(err);
1657 }
1658
1659 fscki = add_inode(c, fsckd, ino);
1660 kfree(ino);
1661 if (IS_ERR(fscki)) {
1662 ubifs_err("error %ld while adding inode %lu node",
1663 PTR_ERR(fscki), inum);
1664 return fscki;
1665 }
1666
1667 return fscki;
1668}
1669
1670/**
1671 * check_leaf - check leaf node.
1672 * @c: UBIFS file-system description object
1673 * @zbr: zbranch of the leaf node to check
1674 * @priv: FS checking information
1675 *
1676 * This is a helper function for 'dbg_check_filesystem()' which is called for
1677 * every single leaf node while walking the indexing tree. It checks that the
1678 * leaf node referred from the indexing tree exists, has correct CRC, and does
1679 * some other basic validation. This function is also responsible for building
1680 * an RB-tree of inodes - it adds all inodes into the RB-tree. It also
1681 * calculates reference count, size, etc for each inode in order to later
1682 * compare them to the information stored inside the inodes and detect possible
1683 * inconsistencies. Returns zero in case of success and a negative error code
1684 * in case of failure.
1685 */
1686static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,
1687 void *priv)
1688{
1689 ino_t inum;
1690 void *node;
1691 struct ubifs_ch *ch;
1692 int err, type = key_type(c, &zbr->key);
1693 struct fsck_inode *fscki;
1694
1695 if (zbr->len < UBIFS_CH_SZ) {
1696 ubifs_err("bad leaf length %d (LEB %d:%d)",
1697 zbr->len, zbr->lnum, zbr->offs);
1698 return -EINVAL;
1699 }
1700
1701 node = kmalloc(zbr->len, GFP_NOFS);
1702 if (!node)
1703 return -ENOMEM;
1704
1705 err = ubifs_tnc_read_node(c, zbr, node);
1706 if (err) {
1707 ubifs_err("cannot read leaf node at LEB %d:%d, error %d",
1708 zbr->lnum, zbr->offs, err);
1709 goto out_free;
1710 }
1711
1712 /* If this is an inode node, add it to RB-tree of inodes */
1713 if (type == UBIFS_INO_KEY) {
1714 fscki = add_inode(c, priv, node);
1715 if (IS_ERR(fscki)) {
1716 err = PTR_ERR(fscki);
1717 ubifs_err("error %d while adding inode node", err);
1718 goto out_dump;
1719 }
1720 goto out;
1721 }
1722
1723 if (type != UBIFS_DENT_KEY && type != UBIFS_XENT_KEY &&
1724 type != UBIFS_DATA_KEY) {
1725 ubifs_err("unexpected node type %d at LEB %d:%d",
1726 type, zbr->lnum, zbr->offs);
1727 err = -EINVAL;
1728 goto out_free;
1729 }
1730
1731 ch = node;
1732 if (le64_to_cpu(ch->sqnum) > c->max_sqnum) {
1733 ubifs_err("too high sequence number, max. is %llu",
1734 c->max_sqnum);
1735 err = -EINVAL;
1736 goto out_dump;
1737 }
1738
1739 if (type == UBIFS_DATA_KEY) {
1740 long long blk_offs;
1741 struct ubifs_data_node *dn = node;
1742
1743 /*
1744 * Search the inode node this data node belongs to and insert
1745 * it to the RB-tree of inodes.
1746 */
1747 inum = key_inum_flash(c, &dn->key);
1748 fscki = read_add_inode(c, priv, inum);
1749 if (IS_ERR(fscki)) {
1750 err = PTR_ERR(fscki);
1751 ubifs_err("error %d while processing data node and "
1752 "trying to find inode node %lu", err, inum);
1753 goto out_dump;
1754 }
1755
1756 /* Make sure the data node is within inode size */
1757 blk_offs = key_block_flash(c, &dn->key);
1758 blk_offs <<= UBIFS_BLOCK_SHIFT;
1759 blk_offs += le32_to_cpu(dn->size);
1760 if (blk_offs > fscki->size) {
1761 ubifs_err("data node at LEB %d:%d is not within inode "
1762 "size %lld", zbr->lnum, zbr->offs,
1763 fscki->size);
1764 err = -EINVAL;
1765 goto out_dump;
1766 }
1767 } else {
1768 int nlen;
1769 struct ubifs_dent_node *dent = node;
1770 struct fsck_inode *fscki1;
1771
1772 err = ubifs_validate_entry(c, dent);
1773 if (err)
1774 goto out_dump;
1775
1776 /*
1777 * Search the inode node this entry refers to and the parent
1778 * inode node and insert them to the RB-tree of inodes.
1779 */
1780 inum = le64_to_cpu(dent->inum);
1781 fscki = read_add_inode(c, priv, inum);
1782 if (IS_ERR(fscki)) {
1783 err = PTR_ERR(fscki);
1784 ubifs_err("error %d while processing entry node and "
1785 "trying to find inode node %lu", err, inum);
1786 goto out_dump;
1787 }
1788
1789 /* Count how many direntries or xentries refers this inode */
1790 fscki->references += 1;
1791
1792 inum = key_inum_flash(c, &dent->key);
1793 fscki1 = read_add_inode(c, priv, inum);
1794 if (IS_ERR(fscki1)) {
1795 err = PTR_ERR(fscki);
1796 ubifs_err("error %d while processing entry node and "
1797 "trying to find parent inode node %lu",
1798 err, inum);
1799 goto out_dump;
1800 }
1801
1802 nlen = le16_to_cpu(dent->nlen);
1803 if (type == UBIFS_XENT_KEY) {
1804 fscki1->calc_xcnt += 1;
1805 fscki1->calc_xsz += CALC_DENT_SIZE(nlen);
1806 fscki1->calc_xsz += CALC_XATTR_BYTES(fscki->size);
1807 fscki1->calc_xnms += nlen;
1808 } else {
1809 fscki1->calc_sz += CALC_DENT_SIZE(nlen);
1810 if (dent->type == UBIFS_ITYPE_DIR)
1811 fscki1->calc_cnt += 1;
1812 }
1813 }
1814
1815out:
1816 kfree(node);
1817 return 0;
1818
1819out_dump:
1820 ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs);
1821 dbg_dump_node(c, node);
1822out_free:
1823 kfree(node);
1824 return err;
1825}
1826
1827/**
1828 * free_inodes - free RB-tree of inodes.
1829 * @fsckd: FS checking information
1830 */
1831static void free_inodes(struct fsck_data *fsckd)
1832{
1833 struct rb_node *this = fsckd->inodes.rb_node;
1834 struct fsck_inode *fscki;
1835
1836 while (this) {
1837 if (this->rb_left)
1838 this = this->rb_left;
1839 else if (this->rb_right)
1840 this = this->rb_right;
1841 else {
1842 fscki = rb_entry(this, struct fsck_inode, rb);
1843 this = rb_parent(this);
1844 if (this) {
1845 if (this->rb_left == &fscki->rb)
1846 this->rb_left = NULL;
1847 else
1848 this->rb_right = NULL;
1849 }
1850 kfree(fscki);
1851 }
1852 }
1853}
1854
1855/**
1856 * check_inodes - checks all inodes.
1857 * @c: UBIFS file-system description object
1858 * @fsckd: FS checking information
1859 *
1860 * This is a helper function for 'dbg_check_filesystem()' which walks the
1861 * RB-tree of inodes after the index scan has been finished, and checks that
1862 * inode nlink, size, etc are correct. Returns zero if inodes are fine,
1863 * %-EINVAL if not, and a negative error code in case of failure.
1864 */
1865static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)
1866{
1867 int n, err;
1868 union ubifs_key key;
1869 struct ubifs_znode *znode;
1870 struct ubifs_zbranch *zbr;
1871 struct ubifs_ino_node *ino;
1872 struct fsck_inode *fscki;
1873 struct rb_node *this = rb_first(&fsckd->inodes);
1874
1875 while (this) {
1876 fscki = rb_entry(this, struct fsck_inode, rb);
1877 this = rb_next(this);
1878
1879 if (S_ISDIR(fscki->mode)) {
1880 /*
1881 * Directories have to have exactly one reference (they
1882 * cannot have hardlinks), although root inode is an
1883 * exception.
1884 */
1885 if (fscki->inum != UBIFS_ROOT_INO &&
1886 fscki->references != 1) {
1887 ubifs_err("directory inode %lu has %d "
1888 "direntries which refer it, but "
1889 "should be 1", fscki->inum,
1890 fscki->references);
1891 goto out_dump;
1892 }
1893 if (fscki->inum == UBIFS_ROOT_INO &&
1894 fscki->references != 0) {
1895 ubifs_err("root inode %lu has non-zero (%d) "
1896 "direntries which refer it",
1897 fscki->inum, fscki->references);
1898 goto out_dump;
1899 }
1900 if (fscki->calc_sz != fscki->size) {
1901 ubifs_err("directory inode %lu size is %lld, "
1902 "but calculated size is %lld",
1903 fscki->inum, fscki->size,
1904 fscki->calc_sz);
1905 goto out_dump;
1906 }
1907 if (fscki->calc_cnt != fscki->nlink) {
1908 ubifs_err("directory inode %lu nlink is %d, "
1909 "but calculated nlink is %d",
1910 fscki->inum, fscki->nlink,
1911 fscki->calc_cnt);
1912 goto out_dump;
1913 }
1914 } else {
1915 if (fscki->references != fscki->nlink) {
1916 ubifs_err("inode %lu nlink is %d, but "
1917 "calculated nlink is %d", fscki->inum,
1918 fscki->nlink, fscki->references);
1919 goto out_dump;
1920 }
1921 }
1922 if (fscki->xattr_sz != fscki->calc_xsz) {
1923 ubifs_err("inode %lu has xattr size %u, but "
1924 "calculated size is %lld",
1925 fscki->inum, fscki->xattr_sz,
1926 fscki->calc_xsz);
1927 goto out_dump;
1928 }
1929 if (fscki->xattr_cnt != fscki->calc_xcnt) {
1930 ubifs_err("inode %lu has %u xattrs, but "
1931 "calculated count is %lld", fscki->inum,
1932 fscki->xattr_cnt, fscki->calc_xcnt);
1933 goto out_dump;
1934 }
1935 if (fscki->xattr_nms != fscki->calc_xnms) {
1936 ubifs_err("inode %lu has xattr names' size %u, but "
1937 "calculated names' size is %lld",
1938 fscki->inum, fscki->xattr_nms,
1939 fscki->calc_xnms);
1940 goto out_dump;
1941 }
1942 }
1943
1944 return 0;
1945
1946out_dump:
1947 /* Read the bad inode and dump it */
1948 ino_key_init(c, &key, fscki->inum);
1949 err = ubifs_lookup_level0(c, &key, &znode, &n);
1950 if (!err) {
1951 ubifs_err("inode %lu not found in index", fscki->inum);
1952 return -ENOENT;
1953 } else if (err < 0) {
1954 ubifs_err("error %d while looking up inode %lu",
1955 err, fscki->inum);
1956 return err;
1957 }
1958
1959 zbr = &znode->zbranch[n];
1960 ino = kmalloc(zbr->len, GFP_NOFS);
1961 if (!ino)
1962 return -ENOMEM;
1963
1964 err = ubifs_tnc_read_node(c, zbr, ino);
1965 if (err) {
1966 ubifs_err("cannot read inode node at LEB %d:%d, error %d",
1967 zbr->lnum, zbr->offs, err);
1968 kfree(ino);
1969 return err;
1970 }
1971
1972 ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",
1973 fscki->inum, zbr->lnum, zbr->offs);
1974 dbg_dump_node(c, ino);
1975 kfree(ino);
1976 return -EINVAL;
1977}
1978
1979/**
1980 * dbg_check_filesystem - check the file-system.
1981 * @c: UBIFS file-system description object
1982 *
1983 * This function checks the file system, namely:
1984 * o makes sure that all leaf nodes exist and their CRCs are correct;
1985 * o makes sure inode nlink, size, xattr size/count are correct (for all
1986 * inodes).
1987 *
1988 * The function reads whole indexing tree and all nodes, so it is pretty
1989 * heavy-weight. Returns zero if the file-system is consistent, %-EINVAL if
1990 * not, and a negative error code in case of failure.
1991 */
1992int dbg_check_filesystem(struct ubifs_info *c)
1993{
1994 int err;
1995 struct fsck_data fsckd;
1996
1997 if (!(ubifs_chk_flags & UBIFS_CHK_FS))
1998 return 0;
1999
2000 fsckd.inodes = RB_ROOT;
2001 err = dbg_walk_index(c, check_leaf, NULL, &fsckd);
2002 if (err)
2003 goto out_free;
2004
2005 err = check_inodes(c, &fsckd);
2006 if (err)
2007 goto out_free;
2008
2009 free_inodes(&fsckd);
2010 return 0;
2011
2012out_free:
2013 ubifs_err("file-system check failed with error %d", err);
2014 dump_stack();
2015 free_inodes(&fsckd);
2016 return err;
2017}
2018
2019static int invocation_cnt;
2020
2021int dbg_force_in_the_gaps(void)
2022{
2023 if (!dbg_force_in_the_gaps_enabled)
2024 return 0;
2025 /* Force in-the-gaps every 8th commit */
2026 return !((invocation_cnt++) & 0x7);
2027}
2028
2029/* Failure mode for recovery testing */
2030
2031#define chance(n, d) (simple_rand() <= (n) * 32768LL / (d))
2032
2033struct failure_mode_info {
2034 struct list_head list;
2035 struct ubifs_info *c;
2036};
2037
2038static LIST_HEAD(fmi_list);
2039static DEFINE_SPINLOCK(fmi_lock);
2040
2041static unsigned int next;
2042
2043static int simple_rand(void)
2044{
2045 if (next == 0)
2046 next = current->pid;
2047 next = next * 1103515245 + 12345;
2048 return (next >> 16) & 32767;
2049}
2050
2051void dbg_failure_mode_registration(struct ubifs_info *c)
2052{
2053 struct failure_mode_info *fmi;
2054
2055 fmi = kmalloc(sizeof(struct failure_mode_info), GFP_NOFS);
2056 if (!fmi) {
2057 dbg_err("Failed to register failure mode - no memory");
2058 return;
2059 }
2060 fmi->c = c;
2061 spin_lock(&fmi_lock);
2062 list_add_tail(&fmi->list, &fmi_list);
2063 spin_unlock(&fmi_lock);
2064}
2065
2066void dbg_failure_mode_deregistration(struct ubifs_info *c)
2067{
2068 struct failure_mode_info *fmi, *tmp;
2069
2070 spin_lock(&fmi_lock);
2071 list_for_each_entry_safe(fmi, tmp, &fmi_list, list)
2072 if (fmi->c == c) {
2073 list_del(&fmi->list);
2074 kfree(fmi);
2075 }
2076 spin_unlock(&fmi_lock);
2077}
2078
2079static struct ubifs_info *dbg_find_info(struct ubi_volume_desc *desc)
2080{
2081 struct failure_mode_info *fmi;
2082
2083 spin_lock(&fmi_lock);
2084 list_for_each_entry(fmi, &fmi_list, list)
2085 if (fmi->c->ubi == desc) {
2086 struct ubifs_info *c = fmi->c;
2087
2088 spin_unlock(&fmi_lock);
2089 return c;
2090 }
2091 spin_unlock(&fmi_lock);
2092 return NULL;
2093}
2094
2095static int in_failure_mode(struct ubi_volume_desc *desc)
2096{
2097 struct ubifs_info *c = dbg_find_info(desc);
2098
2099 if (c && dbg_failure_mode)
2100 return c->failure_mode;
2101 return 0;
2102}
2103
2104static int do_fail(struct ubi_volume_desc *desc, int lnum, int write)
2105{
2106 struct ubifs_info *c = dbg_find_info(desc);
2107
2108 if (!c || !dbg_failure_mode)
2109 return 0;
2110 if (c->failure_mode)
2111 return 1;
2112 if (!c->fail_cnt) {
2113 /* First call - decide delay to failure */
2114 if (chance(1, 2)) {
2115 unsigned int delay = 1 << (simple_rand() >> 11);
2116
2117 if (chance(1, 2)) {
2118 c->fail_delay = 1;
2119 c->fail_timeout = jiffies +
2120 msecs_to_jiffies(delay);
2121 dbg_rcvry("failing after %ums", delay);
2122 } else {
2123 c->fail_delay = 2;
2124 c->fail_cnt_max = delay;
2125 dbg_rcvry("failing after %u calls", delay);
2126 }
2127 }
2128 c->fail_cnt += 1;
2129 }
2130 /* Determine if failure delay has expired */
2131 if (c->fail_delay == 1) {
2132 if (time_before(jiffies, c->fail_timeout))
2133 return 0;
2134 } else if (c->fail_delay == 2)
2135 if (c->fail_cnt++ < c->fail_cnt_max)
2136 return 0;
2137 if (lnum == UBIFS_SB_LNUM) {
2138 if (write) {
2139 if (chance(1, 2))
2140 return 0;
2141 } else if (chance(19, 20))
2142 return 0;
2143 dbg_rcvry("failing in super block LEB %d", lnum);
2144 } else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {
2145 if (chance(19, 20))
2146 return 0;
2147 dbg_rcvry("failing in master LEB %d", lnum);
2148 } else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) {
2149 if (write) {
2150 if (chance(99, 100))
2151 return 0;
2152 } else if (chance(399, 400))
2153 return 0;
2154 dbg_rcvry("failing in log LEB %d", lnum);
2155 } else if (lnum >= c->lpt_first && lnum <= c->lpt_last) {
2156 if (write) {
2157 if (chance(7, 8))
2158 return 0;
2159 } else if (chance(19, 20))
2160 return 0;
2161 dbg_rcvry("failing in LPT LEB %d", lnum);
2162 } else if (lnum >= c->orph_first && lnum <= c->orph_last) {
2163 if (write) {
2164 if (chance(1, 2))
2165 return 0;
2166 } else if (chance(9, 10))
2167 return 0;
2168 dbg_rcvry("failing in orphan LEB %d", lnum);
2169 } else if (lnum == c->ihead_lnum) {
2170 if (chance(99, 100))
2171 return 0;
2172 dbg_rcvry("failing in index head LEB %d", lnum);
2173 } else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {
2174 if (chance(9, 10))
2175 return 0;
2176 dbg_rcvry("failing in GC head LEB %d", lnum);
2177 } else if (write && !RB_EMPTY_ROOT(&c->buds) &&
2178 !ubifs_search_bud(c, lnum)) {
2179 if (chance(19, 20))
2180 return 0;
2181 dbg_rcvry("failing in non-bud LEB %d", lnum);
2182 } else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||
2183 c->cmt_state == COMMIT_RUNNING_REQUIRED) {
2184 if (chance(999, 1000))
2185 return 0;
2186 dbg_rcvry("failing in bud LEB %d commit running", lnum);
2187 } else {
2188 if (chance(9999, 10000))
2189 return 0;
2190 dbg_rcvry("failing in bud LEB %d commit not running", lnum);
2191 }
2192 ubifs_err("*** SETTING FAILURE MODE ON (LEB %d) ***", lnum);
2193 c->failure_mode = 1;
2194 dump_stack();
2195 return 1;
2196}
2197
2198static void cut_data(const void *buf, int len)
2199{
2200 int flen, i;
2201 unsigned char *p = (void *)buf;
2202
2203 flen = (len * (long long)simple_rand()) >> 15;
2204 for (i = flen; i < len; i++)
2205 p[i] = 0xff;
2206}
2207
2208int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
2209 int len, int check)
2210{
2211 if (in_failure_mode(desc))
2212 return -EIO;
2213 return ubi_leb_read(desc, lnum, buf, offset, len, check);
2214}
2215
2216int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
2217 int offset, int len, int dtype)
2218{
16dfd804 2219 int err, failing;
1e51764a
AB
2220
2221 if (in_failure_mode(desc))
2222 return -EIO;
16dfd804
AH
2223 failing = do_fail(desc, lnum, 1);
2224 if (failing)
1e51764a
AB
2225 cut_data(buf, len);
2226 err = ubi_leb_write(desc, lnum, buf, offset, len, dtype);
2227 if (err)
2228 return err;
16dfd804 2229 if (failing)
1e51764a
AB
2230 return -EIO;
2231 return 0;
2232}
2233
2234int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
2235 int len, int dtype)
2236{
2237 int err;
2238
2239 if (do_fail(desc, lnum, 1))
2240 return -EIO;
2241 err = ubi_leb_change(desc, lnum, buf, len, dtype);
2242 if (err)
2243 return err;
2244 if (do_fail(desc, lnum, 1))
2245 return -EIO;
2246 return 0;
2247}
2248
2249int dbg_leb_erase(struct ubi_volume_desc *desc, int lnum)
2250{
2251 int err;
2252
2253 if (do_fail(desc, lnum, 0))
2254 return -EIO;
2255 err = ubi_leb_erase(desc, lnum);
2256 if (err)
2257 return err;
2258 if (do_fail(desc, lnum, 0))
2259 return -EIO;
2260 return 0;
2261}
2262
2263int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum)
2264{
2265 int err;
2266
2267 if (do_fail(desc, lnum, 0))
2268 return -EIO;
2269 err = ubi_leb_unmap(desc, lnum);
2270 if (err)
2271 return err;
2272 if (do_fail(desc, lnum, 0))
2273 return -EIO;
2274 return 0;
2275}
2276
2277int dbg_is_mapped(struct ubi_volume_desc *desc, int lnum)
2278{
2279 if (in_failure_mode(desc))
2280 return -EIO;
2281 return ubi_is_mapped(desc, lnum);
2282}
2283
2284int dbg_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype)
2285{
2286 int err;
2287
2288 if (do_fail(desc, lnum, 0))
2289 return -EIO;
2290 err = ubi_leb_map(desc, lnum, dtype);
2291 if (err)
2292 return err;
2293 if (do_fail(desc, lnum, 0))
2294 return -EIO;
2295 return 0;
2296}
2297
2298#endif /* CONFIG_UBIFS_FS_DEBUG */
This page took 0.16007 seconds and 5 git commands to generate.