blob: f10fe03bc429cc8e789fac0c558f963368421f2e [file] [log] [blame]
Charles Manning3796e1f2012-05-09 16:55:17 +00001/*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2011 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include "yaffs_verify.h"
15#include "yaffs_trace.h"
16#include "yaffs_bitmap.h"
17#include "yaffs_getblockinfo.h"
18#include "yaffs_nand.h"
19
20int yaffs_skip_verification(struct yaffs_dev *dev)
21{
Charles Manning3796e1f2012-05-09 16:55:17 +000022 return !(yaffs_trace_mask &
23 (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL));
24}
25
26static int yaffs_skip_full_verification(struct yaffs_dev *dev)
27{
Charles Manning3796e1f2012-05-09 16:55:17 +000028 return !(yaffs_trace_mask & (YAFFS_TRACE_VERIFY_FULL));
29}
30
31static int yaffs_skip_nand_verification(struct yaffs_dev *dev)
32{
Charles Manning3796e1f2012-05-09 16:55:17 +000033 return !(yaffs_trace_mask & (YAFFS_TRACE_VERIFY_NAND));
34}
35
36static const char * const block_state_name[] = {
37 "Unknown",
38 "Needs scan",
39 "Scanning",
40 "Empty",
41 "Allocating",
42 "Full",
43 "Dirty",
44 "Checkpoint",
45 "Collecting",
46 "Dead"
47};
48
49void yaffs_verify_blk(struct yaffs_dev *dev, struct yaffs_block_info *bi, int n)
50{
51 int actually_used;
52 int in_use;
53
54 if (yaffs_skip_verification(dev))
55 return;
56
57 /* Report illegal runtime states */
58 if (bi->block_state >= YAFFS_NUMBER_OF_BLOCK_STATES)
59 yaffs_trace(YAFFS_TRACE_VERIFY,
60 "Block %d has undefined state %d",
61 n, bi->block_state);
62
63 switch (bi->block_state) {
64 case YAFFS_BLOCK_STATE_UNKNOWN:
65 case YAFFS_BLOCK_STATE_SCANNING:
66 case YAFFS_BLOCK_STATE_NEEDS_SCAN:
67 yaffs_trace(YAFFS_TRACE_VERIFY,
68 "Block %d has bad run-state %s",
69 n, block_state_name[bi->block_state]);
70 }
71
72 /* Check pages in use and soft deletions are legal */
73
74 actually_used = bi->pages_in_use - bi->soft_del_pages;
75
76 if (bi->pages_in_use < 0 ||
77 bi->pages_in_use > dev->param.chunks_per_block ||
78 bi->soft_del_pages < 0 ||
79 bi->soft_del_pages > dev->param.chunks_per_block ||
80 actually_used < 0 || actually_used > dev->param.chunks_per_block)
81 yaffs_trace(YAFFS_TRACE_VERIFY,
82 "Block %d has illegal values pages_in_used %d soft_del_pages %d",
83 n, bi->pages_in_use, bi->soft_del_pages);
84
85 /* Check chunk bitmap legal */
86 in_use = yaffs_count_chunk_bits(dev, n);
87 if (in_use != bi->pages_in_use)
88 yaffs_trace(YAFFS_TRACE_VERIFY,
89 "Block %d has inconsistent values pages_in_use %d counted chunk bits %d",
90 n, bi->pages_in_use, in_use);
91}
92
93void yaffs_verify_collected_blk(struct yaffs_dev *dev,
94 struct yaffs_block_info *bi, int n)
95{
96 yaffs_verify_blk(dev, bi, n);
97
98 /* After collection the block should be in the erased state */
99
100 if (bi->block_state != YAFFS_BLOCK_STATE_COLLECTING &&
101 bi->block_state != YAFFS_BLOCK_STATE_EMPTY) {
102 yaffs_trace(YAFFS_TRACE_ERROR,
103 "Block %d is in state %d after gc, should be erased",
104 n, bi->block_state);
105 }
106}
107
108void yaffs_verify_blocks(struct yaffs_dev *dev)
109{
110 int i;
111 int state_count[YAFFS_NUMBER_OF_BLOCK_STATES];
112 int illegal_states = 0;
113
114 if (yaffs_skip_verification(dev))
115 return;
116
117 memset(state_count, 0, sizeof(state_count));
118
119 for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
120 struct yaffs_block_info *bi = yaffs_get_block_info(dev, i);
121 yaffs_verify_blk(dev, bi, i);
122
123 if (bi->block_state < YAFFS_NUMBER_OF_BLOCK_STATES)
124 state_count[bi->block_state]++;
125 else
126 illegal_states++;
127 }
128
129 yaffs_trace(YAFFS_TRACE_VERIFY, "Block summary");
130
131 yaffs_trace(YAFFS_TRACE_VERIFY,
132 "%d blocks have illegal states",
133 illegal_states);
134 if (state_count[YAFFS_BLOCK_STATE_ALLOCATING] > 1)
135 yaffs_trace(YAFFS_TRACE_VERIFY,
136 "Too many allocating blocks");
137
138 for (i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++)
139 yaffs_trace(YAFFS_TRACE_VERIFY,
140 "%s %d blocks",
141 block_state_name[i], state_count[i]);
142
143 if (dev->blocks_in_checkpt != state_count[YAFFS_BLOCK_STATE_CHECKPOINT])
144 yaffs_trace(YAFFS_TRACE_VERIFY,
145 "Checkpoint block count wrong dev %d count %d",
146 dev->blocks_in_checkpt,
147 state_count[YAFFS_BLOCK_STATE_CHECKPOINT]);
148
149 if (dev->n_erased_blocks != state_count[YAFFS_BLOCK_STATE_EMPTY])
150 yaffs_trace(YAFFS_TRACE_VERIFY,
151 "Erased block count wrong dev %d count %d",
152 dev->n_erased_blocks,
153 state_count[YAFFS_BLOCK_STATE_EMPTY]);
154
155 if (state_count[YAFFS_BLOCK_STATE_COLLECTING] > 1)
156 yaffs_trace(YAFFS_TRACE_VERIFY,
157 "Too many collecting blocks %d (max is 1)",
158 state_count[YAFFS_BLOCK_STATE_COLLECTING]);
159}
160
161/*
162 * Verify the object header. oh must be valid, but obj and tags may be NULL in
163 * which case those tests will not be performed.
164 */
165void yaffs_verify_oh(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh,
166 struct yaffs_ext_tags *tags, int parent_check)
167{
168 if (obj && yaffs_skip_verification(obj->my_dev))
169 return;
170
171 if (!(tags && obj && oh)) {
172 yaffs_trace(YAFFS_TRACE_VERIFY,
173 "Verifying object header tags %p obj %p oh %p",
174 tags, obj, oh);
175 return;
176 }
177
178 if (oh->type <= YAFFS_OBJECT_TYPE_UNKNOWN ||
179 oh->type > YAFFS_OBJECT_TYPE_MAX)
180 yaffs_trace(YAFFS_TRACE_VERIFY,
181 "Obj %d header type is illegal value 0x%x",
182 tags->obj_id, oh->type);
183
184 if (tags->obj_id != obj->obj_id)
185 yaffs_trace(YAFFS_TRACE_VERIFY,
186 "Obj %d header mismatch obj_id %d",
187 tags->obj_id, obj->obj_id);
188
189 /*
190 * Check that the object's parent ids match if parent_check requested.
191 *
192 * Tests do not apply to the root object.
193 */
194
195 if (parent_check && tags->obj_id > 1 && !obj->parent)
196 yaffs_trace(YAFFS_TRACE_VERIFY,
197 "Obj %d header mismatch parent_id %d obj->parent is NULL",
198 tags->obj_id, oh->parent_obj_id);
199
200 if (parent_check && obj->parent &&
201 oh->parent_obj_id != obj->parent->obj_id &&
202 (oh->parent_obj_id != YAFFS_OBJECTID_UNLINKED ||
203 obj->parent->obj_id != YAFFS_OBJECTID_DELETED))
204 yaffs_trace(YAFFS_TRACE_VERIFY,
205 "Obj %d header mismatch parent_id %d parent_obj_id %d",
206 tags->obj_id, oh->parent_obj_id,
207 obj->parent->obj_id);
208
209 if (tags->obj_id > 1 && oh->name[0] == 0) /* Null name */
210 yaffs_trace(YAFFS_TRACE_VERIFY,
211 "Obj %d header name is NULL",
212 obj->obj_id);
213
214 if (tags->obj_id > 1 && ((u8) (oh->name[0])) == 0xff) /* Junk name */
215 yaffs_trace(YAFFS_TRACE_VERIFY,
216 "Obj %d header name is 0xff",
217 obj->obj_id);
218}
219
220void yaffs_verify_file(struct yaffs_obj *obj)
221{
222 u32 x;
223 int required_depth;
Charles Manning3796e1f2012-05-09 16:55:17 +0000224 int last_chunk;
225 u32 offset_in_chunk;
226 u32 the_chunk;
227
228 u32 i;
229 struct yaffs_dev *dev;
230 struct yaffs_ext_tags tags;
231 struct yaffs_tnode *tn;
232 u32 obj_id;
233
234 if (!obj)
235 return;
236
237 if (yaffs_skip_verification(obj->my_dev))
238 return;
239
240 dev = obj->my_dev;
241 obj_id = obj->obj_id;
242
Charles Manning3796e1f2012-05-09 16:55:17 +0000243 /* Check file size is consistent with tnode depth */
244 yaffs_addr_to_chunk(dev, obj->variant.file_variant.file_size,
245 &last_chunk, &offset_in_chunk);
246 last_chunk++;
247 x = last_chunk >> YAFFS_TNODES_LEVEL0_BITS;
248 required_depth = 0;
249 while (x > 0) {
250 x >>= YAFFS_TNODES_INTERNAL_BITS;
251 required_depth++;
252 }
253
Charles Manning3796e1f2012-05-09 16:55:17 +0000254 /* Check that the chunks in the tnode tree are all correct.
255 * We do this by scanning through the tnode tree and
256 * checking the tags for every chunk match.
257 */
258
259 if (yaffs_skip_nand_verification(dev))
260 return;
261
262 for (i = 1; i <= last_chunk; i++) {
263 tn = yaffs_find_tnode_0(dev, &obj->variant.file_variant, i);
264
265 if (!tn)
266 continue;
267
268 the_chunk = yaffs_get_group_base(dev, tn, i);
269 if (the_chunk > 0) {
270 yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
271 &tags);
272 if (tags.obj_id != obj_id || tags.chunk_id != i)
273 yaffs_trace(YAFFS_TRACE_VERIFY,
274 "Object %d chunk_id %d NAND mismatch chunk %d tags (%d:%d)",
275 obj_id, i, the_chunk,
276 tags.obj_id, tags.chunk_id);
277 }
278 }
279}
280
281void yaffs_verify_link(struct yaffs_obj *obj)
282{
283 if (obj && yaffs_skip_verification(obj->my_dev))
284 return;
285
286 /* Verify sane equivalent object */
287}
288
289void yaffs_verify_symlink(struct yaffs_obj *obj)
290{
291 if (obj && yaffs_skip_verification(obj->my_dev))
292 return;
293
294 /* Verify symlink string */
295}
296
297void yaffs_verify_special(struct yaffs_obj *obj)
298{
299 if (obj && yaffs_skip_verification(obj->my_dev))
300 return;
301}
302
303void yaffs_verify_obj(struct yaffs_obj *obj)
304{
305 struct yaffs_dev *dev;
306 u32 chunk_min;
307 u32 chunk_max;
308 u32 chunk_id_ok;
309 u32 chunk_in_range;
310 u32 chunk_wrongly_deleted;
311 u32 chunk_valid;
312
313 if (!obj)
314 return;
315
316 if (obj->being_created)
317 return;
318
319 dev = obj->my_dev;
320
321 if (yaffs_skip_verification(dev))
322 return;
323
324 /* Check sane object header chunk */
325
326 chunk_min = dev->internal_start_block * dev->param.chunks_per_block;
327 chunk_max =
328 (dev->internal_end_block + 1) * dev->param.chunks_per_block - 1;
329
330 chunk_in_range = (((unsigned)(obj->hdr_chunk)) >= chunk_min &&
331 ((unsigned)(obj->hdr_chunk)) <= chunk_max);
332 chunk_id_ok = chunk_in_range || (obj->hdr_chunk == 0);
333 chunk_valid = chunk_in_range &&
334 yaffs_check_chunk_bit(dev,
335 obj->hdr_chunk / dev->param.chunks_per_block,
336 obj->hdr_chunk % dev->param.chunks_per_block);
337 chunk_wrongly_deleted = chunk_in_range && !chunk_valid;
338
339 if (!obj->fake && (!chunk_id_ok || chunk_wrongly_deleted))
340 yaffs_trace(YAFFS_TRACE_VERIFY,
341 "Obj %d has chunk_id %d %s %s",
342 obj->obj_id, obj->hdr_chunk,
343 chunk_id_ok ? "" : ",out of range",
344 chunk_wrongly_deleted ? ",marked as deleted" : "");
345
346 if (chunk_valid && !yaffs_skip_nand_verification(dev)) {
347 struct yaffs_ext_tags tags;
348 struct yaffs_obj_hdr *oh;
349 u8 *buffer = yaffs_get_temp_buffer(dev);
350
351 oh = (struct yaffs_obj_hdr *)buffer;
352
353 yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, buffer, &tags);
354
355 yaffs_verify_oh(obj, oh, &tags, 1);
356
357 yaffs_release_temp_buffer(dev, buffer);
358 }
359
360 /* Verify it has a parent */
361 if (obj && !obj->fake && (!obj->parent || obj->parent->my_dev != dev)) {
362 yaffs_trace(YAFFS_TRACE_VERIFY,
363 "Obj %d has parent pointer %p which does not look like an object",
364 obj->obj_id, obj->parent);
365 }
366
367 /* Verify parent is a directory */
368 if (obj->parent &&
369 obj->parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
370 yaffs_trace(YAFFS_TRACE_VERIFY,
371 "Obj %d's parent is not a directory (type %d)",
372 obj->obj_id, obj->parent->variant_type);
373 }
374
375 switch (obj->variant_type) {
376 case YAFFS_OBJECT_TYPE_FILE:
377 yaffs_verify_file(obj);
378 break;
379 case YAFFS_OBJECT_TYPE_SYMLINK:
380 yaffs_verify_symlink(obj);
381 break;
382 case YAFFS_OBJECT_TYPE_DIRECTORY:
383 yaffs_verify_dir(obj);
384 break;
385 case YAFFS_OBJECT_TYPE_HARDLINK:
386 yaffs_verify_link(obj);
387 break;
388 case YAFFS_OBJECT_TYPE_SPECIAL:
389 yaffs_verify_special(obj);
390 break;
391 case YAFFS_OBJECT_TYPE_UNKNOWN:
392 default:
393 yaffs_trace(YAFFS_TRACE_VERIFY,
394 "Obj %d has illegaltype %d",
395 obj->obj_id, obj->variant_type);
396 break;
397 }
398}
399
400void yaffs_verify_objects(struct yaffs_dev *dev)
401{
402 struct yaffs_obj *obj;
403 int i;
404 struct list_head *lh;
405
406 if (yaffs_skip_verification(dev))
407 return;
408
409 /* Iterate through the objects in each hash entry */
410
411 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
412 list_for_each(lh, &dev->obj_bucket[i].list) {
413 obj = list_entry(lh, struct yaffs_obj, hash_link);
414 yaffs_verify_obj(obj);
415 }
416 }
417}
418
419void yaffs_verify_obj_in_dir(struct yaffs_obj *obj)
420{
421 struct list_head *lh;
422 struct yaffs_obj *list_obj;
423 int count = 0;
424
425 if (!obj) {
426 yaffs_trace(YAFFS_TRACE_ALWAYS, "No object to verify");
427 BUG();
428 return;
429 }
430
431 if (yaffs_skip_verification(obj->my_dev))
432 return;
433
434 if (!obj->parent) {
435 yaffs_trace(YAFFS_TRACE_ALWAYS, "Object does not have parent");
436 BUG();
437 return;
438 }
439
440 if (obj->parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
441 yaffs_trace(YAFFS_TRACE_ALWAYS, "Parent is not directory");
442 BUG();
443 }
444
445 /* Iterate through the objects in each hash entry */
446
447 list_for_each(lh, &obj->parent->variant.dir_variant.children) {
448 list_obj = list_entry(lh, struct yaffs_obj, siblings);
449 yaffs_verify_obj(list_obj);
450 if (obj == list_obj)
451 count++;
452 }
453
454 if (count != 1) {
455 yaffs_trace(YAFFS_TRACE_ALWAYS,
456 "Object in directory %d times",
457 count);
458 BUG();
459 }
460}
461
462void yaffs_verify_dir(struct yaffs_obj *directory)
463{
464 struct list_head *lh;
465 struct yaffs_obj *list_obj;
466
467 if (!directory) {
468 BUG();
469 return;
470 }
471
472 if (yaffs_skip_full_verification(directory->my_dev))
473 return;
474
475 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
476 yaffs_trace(YAFFS_TRACE_ALWAYS,
477 "Directory has wrong type: %d",
478 directory->variant_type);
479 BUG();
480 }
481
482 /* Iterate through the objects in each hash entry */
483
484 list_for_each(lh, &directory->variant.dir_variant.children) {
485 list_obj = list_entry(lh, struct yaffs_obj, siblings);
486 if (list_obj->parent != directory) {
487 yaffs_trace(YAFFS_TRACE_ALWAYS,
488 "Object in directory list has wrong parent %p",
489 list_obj->parent);
490 BUG();
491 }
492 yaffs_verify_obj_in_dir(list_obj);
493 }
494}
495
496static int yaffs_free_verification_failures;
497
498void yaffs_verify_free_chunks(struct yaffs_dev *dev)
499{
500 int counted;
501 int difference;
502
503 if (yaffs_skip_verification(dev))
504 return;
505
506 counted = yaffs_count_free_chunks(dev);
507
508 difference = dev->n_free_chunks - counted;
509
510 if (difference) {
511 yaffs_trace(YAFFS_TRACE_ALWAYS,
512 "Freechunks verification failure %d %d %d",
513 dev->n_free_chunks, counted, difference);
514 yaffs_free_verification_failures++;
515 }
516}
517
518int yaffs_verify_file_sane(struct yaffs_obj *in)
519{
Charles Manning3796e1f2012-05-09 16:55:17 +0000520 return YAFFS_OK;
521}