yaffs Change tracing to yaffs_trace
[yaffs2.git] / yaffs_verify.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2010 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
20 int yaffs_skip_verification(struct yaffs_dev *dev)
21 {
22         dev = dev;
23         return !(yaffs_trace_mask &
24                  (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL));
25 }
26
27 static int yaffs_skip_full_verification(struct yaffs_dev *dev)
28 {
29         dev = dev;
30         return !(yaffs_trace_mask & (YAFFS_TRACE_VERIFY_FULL));
31 }
32
33 static int yaffs_skip_nand_verification(struct yaffs_dev *dev)
34 {
35         dev = dev;
36         return !(yaffs_trace_mask & (YAFFS_TRACE_VERIFY_NAND));
37 }
38
39 static const char *block_state_name[] = {
40         "Unknown",
41         "Needs scanning",
42         "Scanning",
43         "Empty",
44         "Allocating",
45         "Full",
46         "Dirty",
47         "Checkpoint",
48         "Collecting",
49         "Dead"
50 };
51
52 void yaffs_verify_blk(struct yaffs_dev *dev, struct yaffs_block_info *bi, int n)
53 {
54         int actually_used;
55         int in_use;
56
57         if (yaffs_skip_verification(dev))
58                 return;
59
60         /* Report illegal runtime states */
61         if (bi->block_state >= YAFFS_NUMBER_OF_BLOCK_STATES)
62                 yaffs_trace(YAFFS_TRACE_VERIFY,
63                         "Block %d has undefined state %d",
64                         n, bi->block_state);
65
66         switch (bi->block_state) {
67         case YAFFS_BLOCK_STATE_UNKNOWN:
68         case YAFFS_BLOCK_STATE_SCANNING:
69         case YAFFS_BLOCK_STATE_NEEDS_SCANNING:
70                 yaffs_trace(YAFFS_TRACE_VERIFY,
71                         "Block %d has bad run-state %s",
72                         n, block_state_name[bi->block_state]);
73         }
74
75         /* Check pages in use and soft deletions are legal */
76
77         actually_used = bi->pages_in_use - bi->soft_del_pages;
78
79         if (bi->pages_in_use < 0
80             || bi->pages_in_use > dev->param.chunks_per_block
81             || bi->soft_del_pages < 0
82             || bi->soft_del_pages > dev->param.chunks_per_block
83             || actually_used < 0 || actually_used > dev->param.chunks_per_block)
84                 yaffs_trace(YAFFS_TRACE_VERIFY,
85                         "Block %d has illegal values pages_in_used %d soft_del_pages %d",
86                         n, bi->pages_in_use, bi->soft_del_pages);
87
88         /* Check chunk bitmap legal */
89         in_use = yaffs_count_chunk_bits(dev, n);
90         if (in_use != bi->pages_in_use)
91                 yaffs_trace(YAFFS_TRACE_VERIFY,
92                         "Block %d has inconsistent values pages_in_use %d counted chunk bits %d",
93                         n, bi->pages_in_use, in_use);
94
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         int i;
115         int 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 != 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 which
167  * 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)   /* Trashed 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         int required_depth;
227         int actual_depth;
228         u32 last_chunk;
229         u32 x;
230         u32 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         /* Check file size is consistent with tnode depth */
246         last_chunk =
247             obj->variant.file_variant.file_size / dev->data_bytes_per_chunk + 1;
248         x = last_chunk >> YAFFS_TNODES_LEVEL0_BITS;
249         required_depth = 0;
250         while (x > 0) {
251                 x >>= YAFFS_TNODES_INTERNAL_BITS;
252                 required_depth++;
253         }
254
255         actual_depth = obj->variant.file_variant.top_level;
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                         u32 the_chunk = yaffs_get_group_base(dev, tn, i);
270                         if (the_chunk > 0) {
271                                 /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),obj_id,i,the_chunk)); */
272                                 yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
273                                                          &tags);
274                                 if (tags.obj_id != obj_id || tags.chunk_id != i)
275                                 yaffs_trace(YAFFS_TRACE_VERIFY,
276                                         "Object %d chunk_id %d NAND mismatch chunk %d tags (%d:%d)",
277                                          obj_id, i, the_chunk,
278                                          tags.obj_id, tags.chunk_id);
279                         }
280                 }
281         }
282 }
283
284 void 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
292 void 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
300 void yaffs_verify_special(struct yaffs_obj *obj)
301 {
302         if (obj && yaffs_skip_verification(obj->my_dev))
303                 return;
304 }
305
306 void yaffs_verify_obj(struct yaffs_obj *obj)
307 {
308         struct yaffs_dev *dev;
309
310         u32 chunk_min;
311         u32 chunk_max;
312
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, __LINE__);
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, __LINE__);
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                         if (lh) {
419                                 obj =
420                                     list_entry(lh, struct yaffs_obj, hash_link);
421                                 yaffs_verify_obj(obj);
422                         }
423                 }
424         }
425 }
426
427 void yaffs_verify_obj_in_dir(struct yaffs_obj *obj)
428 {
429         struct list_head *lh;
430         struct yaffs_obj *list_obj;
431
432         int count = 0;
433
434         if (!obj) {
435                 yaffs_trace(YAFFS_TRACE_ALWAYS, "No object to verify");
436                 YBUG();
437                 return;
438         }
439
440         if (yaffs_skip_verification(obj->my_dev))
441                 return;
442
443         if (!obj->parent) {
444                 yaffs_trace(YAFFS_TRACE_ALWAYS, "Object does not have parent" );
445                 YBUG();
446                 return;
447         }
448
449         if (obj->parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
450                 yaffs_trace(YAFFS_TRACE_ALWAYS, "Parent is not directory");
451                 YBUG();
452         }
453
454         /* Iterate through the objects in each hash entry */
455
456         list_for_each(lh, &obj->parent->variant.dir_variant.children) {
457                 if (lh) {
458                         list_obj = list_entry(lh, struct yaffs_obj, siblings);
459                         yaffs_verify_obj(list_obj);
460                         if (obj == list_obj)
461                                 count++;
462                 }
463         }
464
465         if (count != 1) {
466                 yaffs_trace(YAFFS_TRACE_ALWAYS,
467                         "Object in directory %d times",
468                         count);
469                 YBUG();
470         }
471 }
472
473 void yaffs_verify_dir(struct yaffs_obj *directory)
474 {
475         struct list_head *lh;
476         struct yaffs_obj *list_obj;
477
478         if (!directory) {
479                 YBUG();
480                 return;
481         }
482
483         if (yaffs_skip_full_verification(directory->my_dev))
484                 return;
485
486         if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
487                 yaffs_trace(YAFFS_TRACE_ALWAYS,
488                         "Directory has wrong type: %d",
489                         directory->variant_type);
490                 YBUG();
491         }
492
493         /* Iterate through the objects in each hash entry */
494
495         list_for_each(lh, &directory->variant.dir_variant.children) {
496                 if (lh) {
497                         list_obj = list_entry(lh, struct yaffs_obj, siblings);
498                         if (list_obj->parent != directory) {
499                                 yaffs_trace(YAFFS_TRACE_ALWAYS,
500                                         "Object in directory list has wrong parent %p",
501                                         list_obj->parent);
502                                 YBUG();
503                         }
504                         yaffs_verify_obj_in_dir(list_obj);
505                 }
506         }
507 }
508
509 static int yaffs_free_verification_failures;
510
511 void yaffs_verify_free_chunks(struct yaffs_dev *dev)
512 {
513         int counted;
514         int difference;
515
516         if (yaffs_skip_verification(dev))
517                 return;
518
519         counted = yaffs_count_free_chunks(dev);
520
521         difference = dev->n_free_chunks - counted;
522
523         if (difference) {
524                 yaffs_trace(YAFFS_TRACE_ALWAYS,
525                         "Freechunks verification failure %d %d %d",
526                         dev->n_free_chunks, counted, difference);
527                 yaffs_free_verification_failures++;
528         }
529 }
530
531 int yaffs_verify_file_sane(struct yaffs_obj *in)
532 {
533         in = in;
534         return YAFFS_OK;
535 }
536