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