Merge branch 'time_upgrade'
[yaffs2.git] / yaffs_verify.c
1 /*
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
19 int 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
26 static int yaffs_skip_full_verification(struct yaffs_dev *dev)
27 {
28         (void) dev;
29         return !(yaffs_trace_mask & (YAFFS_TRACE_VERIFY_FULL));
30 }
31
32 static int yaffs_skip_nand_verification(struct yaffs_dev *dev)
33 {
34         (void) dev;
35         return !(yaffs_trace_mask & (YAFFS_TRACE_VERIFY_NAND));
36 }
37
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"
49 };
50
51 void 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         (void) block_state_name;
57
58         if (yaffs_skip_verification(dev))
59                 return;
60
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",
65                         n, bi->block_state);
66
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]);
74         }
75
76         /* Check pages in use and soft deletions are legal */
77
78         actually_used = bi->pages_in_use - bi->soft_del_pages;
79
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);
88
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);
95 }
96
97 void yaffs_verify_collected_blk(struct yaffs_dev *dev,
98                                 struct yaffs_block_info *bi, int n)
99 {
100         yaffs_verify_blk(dev, bi, n);
101
102         /* After collection the block should be in the erased state */
103
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",
108                         n, bi->block_state);
109         }
110 }
111
112 void yaffs_verify_blocks(struct yaffs_dev *dev)
113 {
114         u32 i;
115         u32 state_count[YAFFS_NUMBER_OF_BLOCK_STATES];
116         int illegal_states = 0;
117
118         if (yaffs_skip_verification(dev))
119                 return;
120
121         memset(state_count, 0, sizeof(state_count));
122
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);
126
127                 if (bi->block_state < YAFFS_NUMBER_OF_BLOCK_STATES)
128                         state_count[bi->block_state]++;
129                 else
130                         illegal_states++;
131         }
132
133         yaffs_trace(YAFFS_TRACE_VERIFY, "Block summary");
134
135         yaffs_trace(YAFFS_TRACE_VERIFY,
136                 "%d blocks have illegal states",
137                 illegal_states);
138         if (state_count[YAFFS_BLOCK_STATE_ALLOCATING] > 1)
139                 yaffs_trace(YAFFS_TRACE_VERIFY,
140                         "Too many allocating blocks");
141
142         for (i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++)
143                 yaffs_trace(YAFFS_TRACE_VERIFY,
144                         "%s %d blocks",
145                         block_state_name[i], state_count[i]);
146
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]);
152
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]);
158
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]);
163 }
164
165 /*
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.
168  */
169 void yaffs_verify_oh(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh,
170                      struct yaffs_ext_tags *tags, int parent_check)
171 {
172         if (obj && yaffs_skip_verification(obj->my_dev))
173                 return;
174
175         if (!(tags && obj && oh)) {
176                 yaffs_trace(YAFFS_TRACE_VERIFY,
177                         "Verifying object header tags %p obj %p oh %p",
178                         tags, obj, oh);
179                 return;
180         }
181
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);
187
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);
192
193         /*
194          * Check that the object's parent ids match if parent_check requested.
195          *
196          * Tests do not apply to the root object.
197          */
198
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);
203
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);
212
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",
216                         obj->obj_id);
217
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",
221                         obj->obj_id);
222 }
223
224 void yaffs_verify_file(struct yaffs_obj *obj)
225 {
226         u32 x;
227         int required_depth;
228         int last_chunk;
229         u32 offset_in_chunk;
230         u32 the_chunk;
231
232         int i;
233         struct yaffs_dev *dev;
234         struct yaffs_ext_tags tags;
235         struct yaffs_tnode *tn;
236         u32 obj_id;
237
238         if (!obj)
239                 return;
240
241         if (yaffs_skip_verification(obj->my_dev))
242                 return;
243
244         dev = obj->my_dev;
245         obj_id = obj->obj_id;
246
247
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);
251         last_chunk++;
252         x = last_chunk >> YAFFS_TNODES_LEVEL0_BITS;
253         required_depth = 0;
254         while (x > 0) {
255                 x >>= YAFFS_TNODES_INTERNAL_BITS;
256                 required_depth++;
257         }
258
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.
262          */
263
264         if (yaffs_skip_nand_verification(dev))
265                 return;
266
267         for (i = 1; i <= last_chunk; i++) {
268                 tn = yaffs_find_tnode_0(dev, &obj->variant.file_variant, i);
269
270                 if (!tn)
271                         continue;
272
273                 the_chunk = yaffs_get_group_base(dev, tn, i);
274                 if (the_chunk > 0) {
275                         yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
276                                                  &tags);
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);
282                 }
283         }
284 }
285
286 void yaffs_verify_link(struct yaffs_obj *obj)
287 {
288         if (obj && yaffs_skip_verification(obj->my_dev))
289                 return;
290
291         /* Verify sane equivalent object */
292 }
293
294 void yaffs_verify_symlink(struct yaffs_obj *obj)
295 {
296         if (obj && yaffs_skip_verification(obj->my_dev))
297                 return;
298
299         /* Verify symlink string */
300 }
301
302 void yaffs_verify_special(struct yaffs_obj *obj)
303 {
304         if (obj && yaffs_skip_verification(obj->my_dev))
305                 return;
306 }
307
308 void yaffs_verify_obj(struct yaffs_obj *obj)
309 {
310         struct yaffs_dev *dev;
311         u32 chunk_min;
312         u32 chunk_max;
313         u32 chunk_id_ok;
314         u32 chunk_in_range;
315         u32 chunk_wrongly_deleted;
316         u32 chunk_valid;
317
318         if (!obj)
319                 return;
320
321         if (obj->being_created)
322                 return;
323
324         dev = obj->my_dev;
325
326         if (yaffs_skip_verification(dev))
327                 return;
328
329         /* Check sane object header chunk */
330
331         chunk_min = dev->internal_start_block * dev->param.chunks_per_block;
332         chunk_max =
333             (dev->internal_end_block + 1) * dev->param.chunks_per_block - 1;
334
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;
343
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" : "");
350
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);
355
356                 oh = (struct yaffs_obj_hdr *)buffer;
357
358                 yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, buffer, &tags);
359
360                 yaffs_verify_oh(obj, oh, &tags, 1);
361
362                 yaffs_release_temp_buffer(dev, buffer);
363         }
364
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);
370         }
371
372         /* Verify parent is a directory */
373         if (obj->parent &&
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);
378         }
379
380         switch (obj->variant_type) {
381         case YAFFS_OBJECT_TYPE_FILE:
382                 yaffs_verify_file(obj);
383                 break;
384         case YAFFS_OBJECT_TYPE_SYMLINK:
385                 yaffs_verify_symlink(obj);
386                 break;
387         case YAFFS_OBJECT_TYPE_DIRECTORY:
388                 yaffs_verify_dir(obj);
389                 break;
390         case YAFFS_OBJECT_TYPE_HARDLINK:
391                 yaffs_verify_link(obj);
392                 break;
393         case YAFFS_OBJECT_TYPE_SPECIAL:
394                 yaffs_verify_special(obj);
395                 break;
396         case YAFFS_OBJECT_TYPE_UNKNOWN:
397         default:
398                 yaffs_trace(YAFFS_TRACE_VERIFY,
399                         "Obj %d has illegaltype %d",
400                    obj->obj_id, obj->variant_type);
401                 break;
402         }
403 }
404
405 void yaffs_verify_objects(struct yaffs_dev *dev)
406 {
407         struct yaffs_obj *obj;
408         int i;
409         struct list_head *lh;
410
411         if (yaffs_skip_verification(dev))
412                 return;
413
414         /* Iterate through the objects in each hash entry */
415
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);
420                 }
421         }
422 }
423
424 void yaffs_verify_obj_in_dir(struct yaffs_obj *obj)
425 {
426         struct list_head *lh;
427         struct yaffs_obj *list_obj;
428         int count = 0;
429
430         if (!obj) {
431                 yaffs_trace(YAFFS_TRACE_ALWAYS, "No object to verify");
432                 BUG();
433                 return;
434         }
435
436         if (yaffs_skip_verification(obj->my_dev))
437                 return;
438
439         if (!obj->parent) {
440                 yaffs_trace(YAFFS_TRACE_ALWAYS, "Object does not have parent");
441                 BUG();
442                 return;
443         }
444
445         if (obj->parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
446                 yaffs_trace(YAFFS_TRACE_ALWAYS, "Parent is not directory");
447                 BUG();
448         }
449
450         /* Iterate through the objects in each hash entry */
451
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);
455                 if (obj == list_obj)
456                         count++;
457         }
458
459         if (count != 1) {
460                 yaffs_trace(YAFFS_TRACE_ALWAYS,
461                         "Object in directory %d times",
462                         count);
463                 BUG();
464         }
465 }
466
467 void yaffs_verify_dir(struct yaffs_obj *directory)
468 {
469         struct list_head *lh;
470         struct yaffs_obj *list_obj;
471         struct yaffs_dev *dev;
472
473         if (!directory) {
474                 BUG();
475                 return;
476         }
477
478         dev = directory->my_dev;
479
480         if (!dev) {
481                 BUG();
482                 return;
483         }
484
485         if (directory == dev->root_dir ||
486             directory == dev->lost_n_found ||
487             directory == dev->unlinked_dir ||
488             directory == dev->del_dir)
489                 return;
490
491         if (yaffs_skip_full_verification(directory->my_dev))
492                 return;
493
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);
498                 BUG();
499         }
500
501         /* Iterate through the objects in each hash entry */
502
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",
508                                 list_obj->parent);
509                         BUG();
510                 }
511                 yaffs_verify_obj_in_dir(list_obj);
512         }
513 }
514
515 static int yaffs_free_verification_failures;
516
517 void yaffs_verify_free_chunks(struct yaffs_dev *dev)
518 {
519         int counted;
520         int difference;
521
522         if (yaffs_skip_verification(dev))
523                 return;
524
525         counted = yaffs_count_free_chunks(dev);
526
527         difference = dev->n_free_chunks - counted;
528
529         if (difference) {
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++;
534         }
535 }
536
537 int yaffs_verify_file_sane(struct yaffs_obj *in)
538 {
539         (void) in;
540         return YAFFS_OK;
541 }