blob: 9141eb5dbd2dbf4f0afa54db3e910cb8e8055640 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2018 Aleph One Ltd.
5 *
6 * Created by Charles Manning <charles@aleph1.co.uk>
7 *
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.
11 */
12
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"
18
19int yaffs_skip_verification(struct yaffs_dev *dev)
20{
21 (void) dev;
22 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{
28 (void) dev;
29 return !(yaffs_trace_mask & (YAFFS_TRACE_VERIFY_FULL));
30}
31
32static int yaffs_skip_nand_verification(struct yaffs_dev *dev)
33{
34 (void) dev;
35 return !(yaffs_trace_mask & (YAFFS_TRACE_VERIFY_NAND));
36}
37
38static const char * const block_state_name[] = {
39 "Unknown",
40 "Needs scan",
41 "Scanning",
42 "Empty",
43 "Allocating",
44 "Full",
45 "Dirty",
46 "Checkpoint",
47 "Collecting",
48 "Dead"
49};
50
51void yaffs_verify_blk(struct yaffs_dev *dev, struct yaffs_block_info *bi, int n)
52{
53 int actually_used;
54 int in_use;
55
56 if (yaffs_skip_verification(dev))
57 return;
58
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",
63 n, bi->block_state);
64
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]);
72 }
73
74 /* Check pages in use and soft deletions are legal */
75
76 actually_used = bi->pages_in_use - bi->soft_del_pages;
77
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);
86
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);
93}
94
95void yaffs_verify_collected_blk(struct yaffs_dev *dev,
96 struct yaffs_block_info *bi, int n)
97{
98 yaffs_verify_blk(dev, bi, n);
99
100 /* After collection the block should be in the erased state */
101
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",
106 n, bi->block_state);
107 }
108}
109
110void yaffs_verify_blocks(struct yaffs_dev *dev)
111{
112 u32 i;
113 u32 state_count[YAFFS_NUMBER_OF_BLOCK_STATES];
114 int illegal_states = 0;
115
116 if (yaffs_skip_verification(dev))
117 return;
118
119 memset(state_count, 0, sizeof(state_count));
120
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);
124
125 if (bi->block_state < YAFFS_NUMBER_OF_BLOCK_STATES)
126 state_count[bi->block_state]++;
127 else
128 illegal_states++;
129 }
130
131 yaffs_trace(YAFFS_TRACE_VERIFY, "Block summary");
132
133 yaffs_trace(YAFFS_TRACE_VERIFY,
134 "%d blocks have illegal states",
135 illegal_states);
136 if (state_count[YAFFS_BLOCK_STATE_ALLOCATING] > 1)
137 yaffs_trace(YAFFS_TRACE_VERIFY,
138 "Too many allocating blocks");
139
140 for (i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++)
141 yaffs_trace(YAFFS_TRACE_VERIFY,
142 "%s %d blocks",
143 block_state_name[i], state_count[i]);
144
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]);
150
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]);
156
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]);
161}
162
163/*
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.
166 */
167void yaffs_verify_oh(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh,
168 struct yaffs_ext_tags *tags, int parent_check)
169{
170 if (obj && yaffs_skip_verification(obj->my_dev))
171 return;
172
173 if (!(tags && obj && oh)) {
174 yaffs_trace(YAFFS_TRACE_VERIFY,
175 "Verifying object header tags %p obj %p oh %p",
176 tags, obj, oh);
177 return;
178 }
179
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);
185
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);
190
191 /*
192 * Check that the object's parent ids match if parent_check requested.
193 *
194 * Tests do not apply to the root object.
195 */
196
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);
201
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);
210
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",
214 obj->obj_id);
215
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",
219 obj->obj_id);
220}
221
222void yaffs_verify_file(struct yaffs_obj *obj)
223{
224 u32 x;
225 int required_depth;
226 int last_chunk;
227 u32 offset_in_chunk;
228 u32 the_chunk;
229
230 int i;
231 struct yaffs_dev *dev;
232 struct yaffs_ext_tags tags;
233 struct yaffs_tnode *tn;
234 u32 obj_id;
235
236 if (!obj)
237 return;
238
239 if (yaffs_skip_verification(obj->my_dev))
240 return;
241
242 dev = obj->my_dev;
243 obj_id = obj->obj_id;
244
245
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);
249 last_chunk++;
250 x = last_chunk >> YAFFS_TNODES_LEVEL0_BITS;
251 required_depth = 0;
252 while (x > 0) {
253 x >>= YAFFS_TNODES_INTERNAL_BITS;
254 required_depth++;
255 }
256
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.
260 */
261
262 if (yaffs_skip_nand_verification(dev))
263 return;
264
265 for (i = 1; i <= last_chunk; i++) {
266 tn = yaffs_find_tnode_0(dev, &obj->variant.file_variant, i);
267
268 if (!tn)
269 continue;
270
271 the_chunk = yaffs_get_group_base(dev, tn, i);
272 if (the_chunk > 0) {
273 yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
274 &tags);
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);
280 }
281 }
282}
283
284void yaffs_verify_link(struct yaffs_obj *obj)
285{
286 if (obj && yaffs_skip_verification(obj->my_dev))
287 return;
288
289 /* Verify sane equivalent object */
290}
291
292void yaffs_verify_symlink(struct yaffs_obj *obj)
293{
294 if (obj && yaffs_skip_verification(obj->my_dev))
295 return;
296
297 /* Verify symlink string */
298}
299
300void yaffs_verify_special(struct yaffs_obj *obj)
301{
302 if (obj && yaffs_skip_verification(obj->my_dev))
303 return;
304}
305
306void yaffs_verify_obj(struct yaffs_obj *obj)
307{
308 struct yaffs_dev *dev;
309 u32 chunk_min;
310 u32 chunk_max;
311 u32 chunk_id_ok;
312 u32 chunk_in_range;
313 u32 chunk_wrongly_deleted;
314 u32 chunk_valid;
315
316 if (!obj)
317 return;
318
319 if (obj->being_created)
320 return;
321
322 dev = obj->my_dev;
323
324 if (yaffs_skip_verification(dev))
325 return;
326
327 /* Check sane object header chunk */
328
329 chunk_min = dev->internal_start_block * dev->param.chunks_per_block;
330 chunk_max =
331 (dev->internal_end_block + 1) * dev->param.chunks_per_block - 1;
332
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;
341
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" : "");
348
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);
353
354 oh = (struct yaffs_obj_hdr *)buffer;
355
356 yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, buffer, &tags);
357
358 yaffs_verify_oh(obj, oh, &tags, 1);
359
360 yaffs_release_temp_buffer(dev, buffer);
361 }
362
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);
368 }
369
370 /* Verify parent is a directory */
371 if (obj->parent &&
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);
376 }
377
378 switch (obj->variant_type) {
379 case YAFFS_OBJECT_TYPE_FILE:
380 yaffs_verify_file(obj);
381 break;
382 case YAFFS_OBJECT_TYPE_SYMLINK:
383 yaffs_verify_symlink(obj);
384 break;
385 case YAFFS_OBJECT_TYPE_DIRECTORY:
386 yaffs_verify_dir(obj);
387 break;
388 case YAFFS_OBJECT_TYPE_HARDLINK:
389 yaffs_verify_link(obj);
390 break;
391 case YAFFS_OBJECT_TYPE_SPECIAL:
392 yaffs_verify_special(obj);
393 break;
394 case YAFFS_OBJECT_TYPE_UNKNOWN:
395 default:
396 yaffs_trace(YAFFS_TRACE_VERIFY,
397 "Obj %d has illegaltype %d",
398 obj->obj_id, obj->variant_type);
399 break;
400 }
401}
402
403void yaffs_verify_objects(struct yaffs_dev *dev)
404{
405 struct yaffs_obj *obj;
406 int i;
407 struct list_head *lh;
408
409 if (yaffs_skip_verification(dev))
410 return;
411
412 /* Iterate through the objects in each hash entry */
413
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);
418 }
419 }
420}
421
422void yaffs_verify_obj_in_dir(struct yaffs_obj *obj)
423{
424 struct list_head *lh;
425 struct yaffs_obj *list_obj;
426 int count = 0;
427
428 if (!obj) {
429 yaffs_trace(YAFFS_TRACE_ALWAYS, "No object to verify");
430 BUG();
431 return;
432 }
433
434 if (yaffs_skip_verification(obj->my_dev))
435 return;
436
437 if (!obj->parent) {
438 yaffs_trace(YAFFS_TRACE_ALWAYS, "Object does not have parent");
439 BUG();
440 return;
441 }
442
443 if (obj->parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
444 yaffs_trace(YAFFS_TRACE_ALWAYS, "Parent is not directory");
445 BUG();
446 }
447
448 /* Iterate through the objects in each hash entry */
449
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);
453 if (obj == list_obj)
454 count++;
455 }
456
457 if (count != 1) {
458 yaffs_trace(YAFFS_TRACE_ALWAYS,
459 "Object in directory %d times",
460 count);
461 BUG();
462 }
463}
464
465void yaffs_verify_dir(struct yaffs_obj *directory)
466{
467 struct list_head *lh;
468 struct yaffs_obj *list_obj;
469 struct yaffs_dev *dev;
470
471 if (!directory) {
472 BUG();
473 return;
474 }
475
476 dev = directory->my_dev;
477
478 if (!dev) {
479 BUG();
480 return;
481 }
482
483 if (directory == dev->root_dir ||
484 directory == dev->lost_n_found ||
485 directory == dev->unlinked_dir ||
486 directory == dev->del_dir)
487 return;
488
489 if (yaffs_skip_full_verification(directory->my_dev))
490 return;
491
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);
496 BUG();
497 }
498
499 /* Iterate through the objects in each hash entry */
500
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",
506 list_obj->parent);
507 BUG();
508 }
509 yaffs_verify_obj_in_dir(list_obj);
510 }
511}
512
513static int yaffs_free_verification_failures;
514
515void yaffs_verify_free_chunks(struct yaffs_dev *dev)
516{
517 int counted;
518 int difference;
519
520 if (yaffs_skip_verification(dev))
521 return;
522
523 counted = yaffs_count_free_chunks(dev);
524
525 difference = dev->n_free_chunks - counted;
526
527 if (difference) {
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++;
532 }
533}
534
535int yaffs_verify_file_sane(struct yaffs_obj *in)
536{
537 (void) in;
538 return YAFFS_OK;
539}