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