2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2018 Aleph One Ltd.
6 * Created by Charles Manning <charles@aleph1.co.uk>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include "yaffs_verify.h"
14 #include "yaffs_trace.h"
15 #include "yaffs_bitmap.h"
16 #include "yaffs_getblockinfo.h"
17 #include "yaffs_nand.h"
19 int yaffs_skip_verification(struct yaffs_dev *dev)
22 return !(yaffs_trace_mask &
23 (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL));
26 static int yaffs_skip_full_verification(struct yaffs_dev *dev)
29 return !(yaffs_trace_mask & (YAFFS_TRACE_VERIFY_FULL));
32 static int yaffs_skip_nand_verification(struct yaffs_dev *dev)
35 return !(yaffs_trace_mask & (YAFFS_TRACE_VERIFY_NAND));
38 static const char * const block_state_name[] = {
51 void yaffs_verify_blk(struct yaffs_dev *dev, struct yaffs_block_info *bi, int n)
56 if (yaffs_skip_verification(dev))
59 /* Report illegal runtime states */
60 if (bi->block_state >= YAFFS_NUMBER_OF_BLOCK_STATES)
61 yaffs_trace(YAFFS_TRACE_VERIFY,
62 "Block %d has undefined state %d",
65 switch (bi->block_state) {
66 case YAFFS_BLOCK_STATE_UNKNOWN:
67 case YAFFS_BLOCK_STATE_SCANNING:
68 case YAFFS_BLOCK_STATE_NEEDS_SCAN:
69 yaffs_trace(YAFFS_TRACE_VERIFY,
70 "Block %d has bad run-state %s",
71 n, block_state_name[bi->block_state]);
74 /* Check pages in use and soft deletions are legal */
76 actually_used = bi->pages_in_use - bi->soft_del_pages;
78 if (bi->pages_in_use < 0 ||
79 bi->pages_in_use > (int)dev->param.chunks_per_block ||
80 bi->soft_del_pages < 0 ||
81 bi->soft_del_pages > (int)dev->param.chunks_per_block ||
82 actually_used < 0 || actually_used > (int)dev->param.chunks_per_block)
83 yaffs_trace(YAFFS_TRACE_VERIFY,
84 "Block %d has illegal values pages_in_used %d soft_del_pages %d",
85 n, bi->pages_in_use, bi->soft_del_pages);
87 /* Check chunk bitmap legal */
88 in_use = yaffs_count_chunk_bits(dev, n);
89 if (in_use != bi->pages_in_use)
90 yaffs_trace(YAFFS_TRACE_VERIFY,
91 "Block %d has inconsistent values pages_in_use %d counted chunk bits %d",
92 n, bi->pages_in_use, in_use);
95 void yaffs_verify_collected_blk(struct yaffs_dev *dev,
96 struct yaffs_block_info *bi, int n)
98 yaffs_verify_blk(dev, bi, n);
100 /* After collection the block should be in the erased state */
102 if (bi->block_state != YAFFS_BLOCK_STATE_COLLECTING &&
103 bi->block_state != YAFFS_BLOCK_STATE_EMPTY) {
104 yaffs_trace(YAFFS_TRACE_ERROR,
105 "Block %d is in state %d after gc, should be erased",
110 void yaffs_verify_blocks(struct yaffs_dev *dev)
113 u32 state_count[YAFFS_NUMBER_OF_BLOCK_STATES];
114 int illegal_states = 0;
116 if (yaffs_skip_verification(dev))
119 memset(state_count, 0, sizeof(state_count));
121 for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
122 struct yaffs_block_info *bi = yaffs_get_block_info(dev, i);
123 yaffs_verify_blk(dev, bi, i);
125 if (bi->block_state < YAFFS_NUMBER_OF_BLOCK_STATES)
126 state_count[bi->block_state]++;
131 yaffs_trace(YAFFS_TRACE_VERIFY, "Block summary");
133 yaffs_trace(YAFFS_TRACE_VERIFY,
134 "%d blocks have illegal states",
136 if (state_count[YAFFS_BLOCK_STATE_ALLOCATING] > 1)
137 yaffs_trace(YAFFS_TRACE_VERIFY,
138 "Too many allocating blocks");
140 for (i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++)
141 yaffs_trace(YAFFS_TRACE_VERIFY,
143 block_state_name[i], state_count[i]);
145 if (dev->blocks_in_checkpt != state_count[YAFFS_BLOCK_STATE_CHECKPOINT])
146 yaffs_trace(YAFFS_TRACE_VERIFY,
147 "Checkpoint block count wrong dev %d count %d",
148 dev->blocks_in_checkpt,
149 state_count[YAFFS_BLOCK_STATE_CHECKPOINT]);
151 if (dev->n_erased_blocks != (int)state_count[YAFFS_BLOCK_STATE_EMPTY])
152 yaffs_trace(YAFFS_TRACE_VERIFY,
153 "Erased block count wrong dev %d count %d",
154 dev->n_erased_blocks,
155 state_count[YAFFS_BLOCK_STATE_EMPTY]);
157 if (state_count[YAFFS_BLOCK_STATE_COLLECTING] > 1)
158 yaffs_trace(YAFFS_TRACE_VERIFY,
159 "Too many collecting blocks %d (max is 1)",
160 state_count[YAFFS_BLOCK_STATE_COLLECTING]);
164 * Verify the object header. oh must be valid, but obj and tags may be NULL in
165 * which case those tests will not be performed.
167 void yaffs_verify_oh(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh,
168 struct yaffs_ext_tags *tags, int parent_check)
170 if (obj && yaffs_skip_verification(obj->my_dev))
173 if (!(tags && obj && oh)) {
174 yaffs_trace(YAFFS_TRACE_VERIFY,
175 "Verifying object header tags %p obj %p oh %p",
180 if (oh->type <= YAFFS_OBJECT_TYPE_UNKNOWN ||
181 oh->type > YAFFS_OBJECT_TYPE_MAX)
182 yaffs_trace(YAFFS_TRACE_VERIFY,
183 "Obj %d header type is illegal value 0x%x",
184 tags->obj_id, oh->type);
186 if (tags->obj_id != obj->obj_id)
187 yaffs_trace(YAFFS_TRACE_VERIFY,
188 "Obj %d header mismatch obj_id %d",
189 tags->obj_id, obj->obj_id);
192 * Check that the object's parent ids match if parent_check requested.
194 * Tests do not apply to the root object.
197 if (parent_check && tags->obj_id > 1 && !obj->parent)
198 yaffs_trace(YAFFS_TRACE_VERIFY,
199 "Obj %d header mismatch parent_id %d obj->parent is NULL",
200 tags->obj_id, oh->parent_obj_id);
202 if (parent_check && obj->parent &&
203 oh->parent_obj_id != obj->parent->obj_id &&
204 (oh->parent_obj_id != YAFFS_OBJECTID_UNLINKED ||
205 obj->parent->obj_id != YAFFS_OBJECTID_DELETED))
206 yaffs_trace(YAFFS_TRACE_VERIFY,
207 "Obj %d header mismatch parent_id %d parent_obj_id %d",
208 tags->obj_id, oh->parent_obj_id,
209 obj->parent->obj_id);
211 if (tags->obj_id > 1 && oh->name[0] == 0) /* Null name */
212 yaffs_trace(YAFFS_TRACE_VERIFY,
213 "Obj %d header name is NULL",
216 if (tags->obj_id > 1 && ((u8) (oh->name[0])) == 0xff) /* Junk name */
217 yaffs_trace(YAFFS_TRACE_VERIFY,
218 "Obj %d header name is 0xff",
222 void yaffs_verify_file(struct yaffs_obj *obj)
231 struct yaffs_dev *dev;
232 struct yaffs_ext_tags tags;
233 struct yaffs_tnode *tn;
239 if (yaffs_skip_verification(obj->my_dev))
243 obj_id = obj->obj_id;
246 /* Check file size is consistent with tnode depth */
247 yaffs_addr_to_chunk(dev, obj->variant.file_variant.file_size,
248 &last_chunk, &offset_in_chunk);
250 x = last_chunk >> YAFFS_TNODES_LEVEL0_BITS;
253 x >>= YAFFS_TNODES_INTERNAL_BITS;
257 /* Check that the chunks in the tnode tree are all correct.
258 * We do this by scanning through the tnode tree and
259 * checking the tags for every chunk match.
262 if (yaffs_skip_nand_verification(dev))
265 for (i = 1; i <= last_chunk; i++) {
266 tn = yaffs_find_tnode_0(dev, &obj->variant.file_variant, i);
271 the_chunk = yaffs_get_group_base(dev, tn, i);
273 yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
275 if (tags.obj_id != obj_id || tags.chunk_id != (u32)i)
276 yaffs_trace(YAFFS_TRACE_VERIFY,
277 "Object %d chunk_id %d NAND mismatch chunk %d tags (%d:%d)",
278 obj_id, i, the_chunk,
279 tags.obj_id, tags.chunk_id);
284 void yaffs_verify_link(struct yaffs_obj *obj)
286 if (obj && yaffs_skip_verification(obj->my_dev))
289 /* Verify sane equivalent object */
292 void yaffs_verify_symlink(struct yaffs_obj *obj)
294 if (obj && yaffs_skip_verification(obj->my_dev))
297 /* Verify symlink string */
300 void yaffs_verify_special(struct yaffs_obj *obj)
302 if (obj && yaffs_skip_verification(obj->my_dev))
306 void yaffs_verify_obj(struct yaffs_obj *obj)
308 struct yaffs_dev *dev;
313 u32 chunk_wrongly_deleted;
319 if (obj->being_created)
324 if (yaffs_skip_verification(dev))
327 /* Check sane object header chunk */
329 chunk_min = dev->internal_start_block * dev->param.chunks_per_block;
331 (dev->internal_end_block + 1) * dev->param.chunks_per_block - 1;
333 chunk_in_range = (((unsigned)(obj->hdr_chunk)) >= chunk_min &&
334 ((unsigned)(obj->hdr_chunk)) <= chunk_max);
335 chunk_id_ok = chunk_in_range || (obj->hdr_chunk == 0);
336 chunk_valid = chunk_in_range &&
337 yaffs_check_chunk_bit(dev,
338 obj->hdr_chunk / dev->param.chunks_per_block,
339 obj->hdr_chunk % dev->param.chunks_per_block);
340 chunk_wrongly_deleted = chunk_in_range && !chunk_valid;
342 if (!obj->fake && (!chunk_id_ok || chunk_wrongly_deleted))
343 yaffs_trace(YAFFS_TRACE_VERIFY,
344 "Obj %d has chunk_id %d %s %s",
345 obj->obj_id, obj->hdr_chunk,
346 chunk_id_ok ? "" : ",out of range",
347 chunk_wrongly_deleted ? ",marked as deleted" : "");
349 if (chunk_valid && !yaffs_skip_nand_verification(dev)) {
350 struct yaffs_ext_tags tags;
351 struct yaffs_obj_hdr *oh;
352 u8 *buffer = yaffs_get_temp_buffer(dev);
354 oh = (struct yaffs_obj_hdr *)buffer;
356 yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, buffer, &tags);
358 yaffs_verify_oh(obj, oh, &tags, 1);
360 yaffs_release_temp_buffer(dev, buffer);
363 /* Verify it has a parent */
364 if (obj && !obj->fake && (!obj->parent || obj->parent->my_dev != dev)) {
365 yaffs_trace(YAFFS_TRACE_VERIFY,
366 "Obj %d has parent pointer %p which does not look like an object",
367 obj->obj_id, obj->parent);
370 /* Verify parent is a directory */
372 obj->parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
373 yaffs_trace(YAFFS_TRACE_VERIFY,
374 "Obj %d's parent is not a directory (type %d)",
375 obj->obj_id, obj->parent->variant_type);
378 switch (obj->variant_type) {
379 case YAFFS_OBJECT_TYPE_FILE:
380 yaffs_verify_file(obj);
382 case YAFFS_OBJECT_TYPE_SYMLINK:
383 yaffs_verify_symlink(obj);
385 case YAFFS_OBJECT_TYPE_DIRECTORY:
386 yaffs_verify_dir(obj);
388 case YAFFS_OBJECT_TYPE_HARDLINK:
389 yaffs_verify_link(obj);
391 case YAFFS_OBJECT_TYPE_SPECIAL:
392 yaffs_verify_special(obj);
394 case YAFFS_OBJECT_TYPE_UNKNOWN:
396 yaffs_trace(YAFFS_TRACE_VERIFY,
397 "Obj %d has illegaltype %d",
398 obj->obj_id, obj->variant_type);
403 void yaffs_verify_objects(struct yaffs_dev *dev)
405 struct yaffs_obj *obj;
407 struct list_head *lh;
409 if (yaffs_skip_verification(dev))
412 /* Iterate through the objects in each hash entry */
414 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
415 list_for_each(lh, &dev->obj_bucket[i].list) {
416 obj = list_entry(lh, struct yaffs_obj, hash_link);
417 yaffs_verify_obj(obj);
422 void yaffs_verify_obj_in_dir(struct yaffs_obj *obj)
424 struct list_head *lh;
425 struct yaffs_obj *list_obj;
429 yaffs_trace(YAFFS_TRACE_ALWAYS, "No object to verify");
434 if (yaffs_skip_verification(obj->my_dev))
438 yaffs_trace(YAFFS_TRACE_ALWAYS, "Object does not have parent");
443 if (obj->parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
444 yaffs_trace(YAFFS_TRACE_ALWAYS, "Parent is not directory");
448 /* Iterate through the objects in each hash entry */
450 list_for_each(lh, &obj->parent->variant.dir_variant.children) {
451 list_obj = list_entry(lh, struct yaffs_obj, siblings);
452 yaffs_verify_obj(list_obj);
458 yaffs_trace(YAFFS_TRACE_ALWAYS,
459 "Object in directory %d times",
465 void yaffs_verify_dir(struct yaffs_obj *directory)
467 struct list_head *lh;
468 struct yaffs_obj *list_obj;
469 struct yaffs_dev *dev;
476 dev = directory->my_dev;
483 if (directory == dev->root_dir ||
484 directory == dev->lost_n_found ||
485 directory == dev->unlinked_dir ||
486 directory == dev->del_dir)
489 if (yaffs_skip_full_verification(directory->my_dev))
492 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
493 yaffs_trace(YAFFS_TRACE_ALWAYS,
494 "Directory has wrong type: %d",
495 directory->variant_type);
499 /* Iterate through the objects in each hash entry */
501 list_for_each(lh, &directory->variant.dir_variant.children) {
502 list_obj = list_entry(lh, struct yaffs_obj, siblings);
503 if (list_obj->parent != directory) {
504 yaffs_trace(YAFFS_TRACE_ALWAYS,
505 "Object in directory list has wrong parent %p",
509 yaffs_verify_obj_in_dir(list_obj);
513 static int yaffs_free_verification_failures;
515 void yaffs_verify_free_chunks(struct yaffs_dev *dev)
520 if (yaffs_skip_verification(dev))
523 counted = yaffs_count_free_chunks(dev);
525 difference = dev->n_free_chunks - counted;
528 yaffs_trace(YAFFS_TRACE_ALWAYS,
529 "Freechunks verification failure %d %d %d",
530 dev->n_free_chunks, counted, difference);
531 yaffs_free_verification_failures++;
535 int yaffs_verify_file_sane(struct yaffs_obj *in)