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[] = {
39 [YAFFS_BLOCK_STATE_UNKNOWN] = "Unknown",
40 [YAFFS_BLOCK_STATE_NEEDS_SCAN] = "Needs scan",
41 [YAFFS_BLOCK_STATE_SCANNING] = "Scanning",
42 [YAFFS_BLOCK_STATE_EMPTY] = "Empty",
43 [YAFFS_BLOCK_STATE_ALLOCATING] = "Allocating",
44 [YAFFS_BLOCK_STATE_FULL] = "Full",
45 [YAFFS_BLOCK_STATE_DIRTY] = "Dirty",
46 [YAFFS_BLOCK_STATE_CHECKPOINT] = "Checkpoint",
47 [YAFFS_BLOCK_STATE_COLLECTING] = "Collecting",
48 [YAFFS_BLOCK_STATE_DEAD] = "Dead"
51 void yaffs_verify_blk(struct yaffs_dev *dev, struct yaffs_block_info *bi, int n)
56 (void) block_state_name;
58 if (yaffs_skip_verification(dev))
61 /* Report illegal runtime states */
62 if (bi->block_state >= YAFFS_NUMBER_OF_BLOCK_STATES)
63 yaffs_trace(YAFFS_TRACE_VERIFY,
64 "Block %d has undefined state %d",
67 switch (bi->block_state) {
68 case YAFFS_BLOCK_STATE_UNKNOWN:
69 case YAFFS_BLOCK_STATE_SCANNING:
70 case YAFFS_BLOCK_STATE_NEEDS_SCAN:
71 yaffs_trace(YAFFS_TRACE_VERIFY,
72 "Block %d has bad run-state %s",
73 n, block_state_name[bi->block_state]);
76 /* Check pages in use and soft deletions are legal */
78 actually_used = bi->pages_in_use - bi->soft_del_pages;
80 if (bi->pages_in_use < 0 ||
81 bi->pages_in_use > (int)dev->param.chunks_per_block ||
82 bi->soft_del_pages < 0 ||
83 bi->soft_del_pages > (int)dev->param.chunks_per_block ||
84 actually_used < 0 || actually_used > (int)dev->param.chunks_per_block)
85 yaffs_trace(YAFFS_TRACE_VERIFY,
86 "Block %d has illegal values pages_in_used %d soft_del_pages %d",
87 n, bi->pages_in_use, bi->soft_del_pages);
89 /* Check chunk bitmap legal */
90 in_use = yaffs_count_chunk_bits(dev, n);
91 if (in_use != bi->pages_in_use)
92 yaffs_trace(YAFFS_TRACE_VERIFY,
93 "Block %d has inconsistent values pages_in_use %d counted chunk bits %d",
94 n, bi->pages_in_use, in_use);
97 void yaffs_verify_collected_blk(struct yaffs_dev *dev,
98 struct yaffs_block_info *bi, int n)
100 yaffs_verify_blk(dev, bi, n);
102 /* After collection the block should be in the erased state */
104 if (bi->block_state != YAFFS_BLOCK_STATE_COLLECTING &&
105 bi->block_state != YAFFS_BLOCK_STATE_EMPTY) {
106 yaffs_trace(YAFFS_TRACE_ERROR,
107 "Block %d is in state %d after gc, should be erased",
112 void yaffs_verify_blocks(struct yaffs_dev *dev)
115 u32 state_count[YAFFS_NUMBER_OF_BLOCK_STATES];
116 int illegal_states = 0;
118 if (yaffs_skip_verification(dev))
121 memset(state_count, 0, sizeof(state_count));
123 for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
124 struct yaffs_block_info *bi = yaffs_get_block_info(dev, i);
125 yaffs_verify_blk(dev, bi, i);
127 if (bi->block_state < YAFFS_NUMBER_OF_BLOCK_STATES)
128 state_count[bi->block_state]++;
133 yaffs_trace(YAFFS_TRACE_VERIFY, "Block summary");
135 yaffs_trace(YAFFS_TRACE_VERIFY,
136 "%d blocks have illegal states",
138 if (state_count[YAFFS_BLOCK_STATE_ALLOCATING] > 1)
139 yaffs_trace(YAFFS_TRACE_VERIFY,
140 "Too many allocating blocks");
142 for (i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++)
143 yaffs_trace(YAFFS_TRACE_VERIFY,
145 block_state_name[i], state_count[i]);
147 if (dev->blocks_in_checkpt != state_count[YAFFS_BLOCK_STATE_CHECKPOINT])
148 yaffs_trace(YAFFS_TRACE_VERIFY,
149 "Checkpoint block count wrong dev %d count %d",
150 dev->blocks_in_checkpt,
151 state_count[YAFFS_BLOCK_STATE_CHECKPOINT]);
153 if (dev->n_erased_blocks != (int)state_count[YAFFS_BLOCK_STATE_EMPTY])
154 yaffs_trace(YAFFS_TRACE_VERIFY,
155 "Erased block count wrong dev %d count %d",
156 dev->n_erased_blocks,
157 state_count[YAFFS_BLOCK_STATE_EMPTY]);
159 if (state_count[YAFFS_BLOCK_STATE_COLLECTING] > 1)
160 yaffs_trace(YAFFS_TRACE_VERIFY,
161 "Too many collecting blocks %d (max is 1)",
162 state_count[YAFFS_BLOCK_STATE_COLLECTING]);
166 * Verify the object header. oh must be valid, but obj and tags may be NULL in
167 * which case those tests will not be performed.
169 void yaffs_verify_oh(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh,
170 struct yaffs_ext_tags *tags, int parent_check)
172 if (obj && yaffs_skip_verification(obj->my_dev))
175 if (!(tags && obj && oh)) {
176 yaffs_trace(YAFFS_TRACE_VERIFY,
177 "Verifying object header tags %p obj %p oh %p",
182 if (oh->type <= YAFFS_OBJECT_TYPE_UNKNOWN ||
183 oh->type > YAFFS_OBJECT_TYPE_MAX)
184 yaffs_trace(YAFFS_TRACE_VERIFY,
185 "Obj %d header type is illegal value 0x%x",
186 tags->obj_id, oh->type);
188 if (tags->obj_id != obj->obj_id)
189 yaffs_trace(YAFFS_TRACE_VERIFY,
190 "Obj %d header mismatch obj_id %d",
191 tags->obj_id, obj->obj_id);
194 * Check that the object's parent ids match if parent_check requested.
196 * Tests do not apply to the root object.
199 if (parent_check && tags->obj_id > 1 && !obj->parent)
200 yaffs_trace(YAFFS_TRACE_VERIFY,
201 "Obj %d header mismatch parent_id %d obj->parent is NULL",
202 tags->obj_id, oh->parent_obj_id);
204 if (parent_check && obj->parent &&
205 oh->parent_obj_id != obj->parent->obj_id &&
206 (oh->parent_obj_id != YAFFS_OBJECTID_UNLINKED ||
207 obj->parent->obj_id != YAFFS_OBJECTID_DELETED))
208 yaffs_trace(YAFFS_TRACE_VERIFY,
209 "Obj %d header mismatch parent_id %d parent_obj_id %d",
210 tags->obj_id, oh->parent_obj_id,
211 obj->parent->obj_id);
213 if (tags->obj_id > 1 && oh->name[0] == 0) /* Null name */
214 yaffs_trace(YAFFS_TRACE_VERIFY,
215 "Obj %d header name is NULL",
218 if (tags->obj_id > 1 && ((u8) (oh->name[0])) == 0xff) /* Junk name */
219 yaffs_trace(YAFFS_TRACE_VERIFY,
220 "Obj %d header name is 0xff",
224 void yaffs_verify_file(struct yaffs_obj *obj)
233 struct yaffs_dev *dev;
234 struct yaffs_ext_tags tags;
235 struct yaffs_tnode *tn;
241 if (yaffs_skip_verification(obj->my_dev))
245 obj_id = obj->obj_id;
248 /* Check file size is consistent with tnode depth */
249 yaffs_addr_to_chunk(dev, obj->variant.file_variant.file_size,
250 &last_chunk, &offset_in_chunk);
252 x = last_chunk >> YAFFS_TNODES_LEVEL0_BITS;
255 x >>= YAFFS_TNODES_INTERNAL_BITS;
259 /* Check that the chunks in the tnode tree are all correct.
260 * We do this by scanning through the tnode tree and
261 * checking the tags for every chunk match.
264 if (yaffs_skip_nand_verification(dev))
267 for (i = 1; i <= last_chunk; i++) {
268 tn = yaffs_find_tnode_0(dev, &obj->variant.file_variant, i);
273 the_chunk = yaffs_get_group_base(dev, tn, i);
275 yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
277 if (tags.obj_id != obj_id || tags.chunk_id != (u32)i)
278 yaffs_trace(YAFFS_TRACE_VERIFY,
279 "Object %d chunk_id %d NAND mismatch chunk %d tags (%d:%d)",
280 obj_id, i, the_chunk,
281 tags.obj_id, tags.chunk_id);
286 void yaffs_verify_link(struct yaffs_obj *obj)
288 if (obj && yaffs_skip_verification(obj->my_dev))
291 /* Verify sane equivalent object */
294 void yaffs_verify_symlink(struct yaffs_obj *obj)
296 if (obj && yaffs_skip_verification(obj->my_dev))
299 /* Verify symlink string */
302 void yaffs_verify_special(struct yaffs_obj *obj)
304 if (obj && yaffs_skip_verification(obj->my_dev))
308 void yaffs_verify_obj(struct yaffs_obj *obj)
310 struct yaffs_dev *dev;
315 u32 chunk_wrongly_deleted;
321 if (obj->being_created)
326 if (yaffs_skip_verification(dev))
329 /* Check sane object header chunk */
331 chunk_min = dev->internal_start_block * dev->param.chunks_per_block;
333 (dev->internal_end_block + 1) * dev->param.chunks_per_block - 1;
335 chunk_in_range = (((unsigned)(obj->hdr_chunk)) >= chunk_min &&
336 ((unsigned)(obj->hdr_chunk)) <= chunk_max);
337 chunk_id_ok = chunk_in_range || (obj->hdr_chunk == 0);
338 chunk_valid = chunk_in_range &&
339 yaffs_check_chunk_bit(dev,
340 obj->hdr_chunk / dev->param.chunks_per_block,
341 obj->hdr_chunk % dev->param.chunks_per_block);
342 chunk_wrongly_deleted = chunk_in_range && !chunk_valid;
344 if (!obj->fake && (!chunk_id_ok || chunk_wrongly_deleted))
345 yaffs_trace(YAFFS_TRACE_VERIFY,
346 "Obj %d has chunk_id %d %s %s",
347 obj->obj_id, obj->hdr_chunk,
348 chunk_id_ok ? "" : ",out of range",
349 chunk_wrongly_deleted ? ",marked as deleted" : "");
351 if (chunk_valid && !yaffs_skip_nand_verification(dev)) {
352 struct yaffs_ext_tags tags;
353 struct yaffs_obj_hdr *oh;
354 u8 *buffer = yaffs_get_temp_buffer(dev);
356 oh = (struct yaffs_obj_hdr *)buffer;
358 yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, buffer, &tags);
360 yaffs_verify_oh(obj, oh, &tags, 1);
362 yaffs_release_temp_buffer(dev, buffer);
365 /* Verify it has a parent */
366 if (obj && !obj->fake && (!obj->parent || obj->parent->my_dev != dev)) {
367 yaffs_trace(YAFFS_TRACE_VERIFY,
368 "Obj %d has parent pointer %p which does not look like an object",
369 obj->obj_id, obj->parent);
372 /* Verify parent is a directory */
374 obj->parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
375 yaffs_trace(YAFFS_TRACE_VERIFY,
376 "Obj %d's parent is not a directory (type %d)",
377 obj->obj_id, obj->parent->variant_type);
380 switch (obj->variant_type) {
381 case YAFFS_OBJECT_TYPE_FILE:
382 yaffs_verify_file(obj);
384 case YAFFS_OBJECT_TYPE_SYMLINK:
385 yaffs_verify_symlink(obj);
387 case YAFFS_OBJECT_TYPE_DIRECTORY:
388 yaffs_verify_dir(obj);
390 case YAFFS_OBJECT_TYPE_HARDLINK:
391 yaffs_verify_link(obj);
393 case YAFFS_OBJECT_TYPE_SPECIAL:
394 yaffs_verify_special(obj);
396 case YAFFS_OBJECT_TYPE_UNKNOWN:
398 yaffs_trace(YAFFS_TRACE_VERIFY,
399 "Obj %d has illegaltype %d",
400 obj->obj_id, obj->variant_type);
405 void yaffs_verify_objects(struct yaffs_dev *dev)
407 struct yaffs_obj *obj;
409 struct list_head *lh;
411 if (yaffs_skip_verification(dev))
414 /* Iterate through the objects in each hash entry */
416 for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
417 list_for_each(lh, &dev->obj_bucket[i].list) {
418 obj = list_entry(lh, struct yaffs_obj, hash_link);
419 yaffs_verify_obj(obj);
424 void yaffs_verify_obj_in_dir(struct yaffs_obj *obj)
426 struct list_head *lh;
427 struct yaffs_obj *list_obj;
431 yaffs_trace(YAFFS_TRACE_ALWAYS, "No object to verify");
436 if (yaffs_skip_verification(obj->my_dev))
440 yaffs_trace(YAFFS_TRACE_ALWAYS, "Object does not have parent");
445 if (obj->parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
446 yaffs_trace(YAFFS_TRACE_ALWAYS, "Parent is not directory");
450 /* Iterate through the objects in each hash entry */
452 list_for_each(lh, &obj->parent->variant.dir_variant.children) {
453 list_obj = list_entry(lh, struct yaffs_obj, siblings);
454 yaffs_verify_obj(list_obj);
460 yaffs_trace(YAFFS_TRACE_ALWAYS,
461 "Object in directory %d times",
467 void yaffs_verify_dir(struct yaffs_obj *directory)
469 struct list_head *lh;
470 struct yaffs_obj *list_obj;
471 struct yaffs_dev *dev;
478 dev = directory->my_dev;
485 if (directory == dev->root_dir ||
486 directory == dev->lost_n_found ||
487 directory == dev->unlinked_dir ||
488 directory == dev->del_dir)
491 if (yaffs_skip_full_verification(directory->my_dev))
494 if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
495 yaffs_trace(YAFFS_TRACE_ALWAYS,
496 "Directory has wrong type: %d",
497 directory->variant_type);
501 /* Iterate through the objects in each hash entry */
503 list_for_each(lh, &directory->variant.dir_variant.children) {
504 list_obj = list_entry(lh, struct yaffs_obj, siblings);
505 if (list_obj->parent != directory) {
506 yaffs_trace(YAFFS_TRACE_ALWAYS,
507 "Object in directory list has wrong parent %p",
511 yaffs_verify_obj_in_dir(list_obj);
515 static int yaffs_free_verification_failures;
517 void yaffs_verify_free_chunks(struct yaffs_dev *dev)
522 if (yaffs_skip_verification(dev))
525 counted = yaffs_count_free_chunks(dev);
527 difference = dev->n_free_chunks - counted;
530 yaffs_trace(YAFFS_TRACE_ALWAYS,
531 "Freechunks verification failure %d %d %d",
532 dev->n_free_chunks, counted, difference);
533 yaffs_free_verification_failures++;
537 int yaffs_verify_file_sane(struct yaffs_obj *in)