yaffs_verify.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  3. *
  4. * Copyright (C) 2002-2011 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. #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. int yaffs_skip_verification(struct yaffs_dev *dev)
  19. {
  20. (void) dev;
  21. return !(yaffs_trace_mask &
  22. (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL));
  23. }
  24. static int yaffs_skip_full_verification(struct yaffs_dev *dev)
  25. {
  26. (void) dev;
  27. return !(yaffs_trace_mask & (YAFFS_TRACE_VERIFY_FULL));
  28. }
  29. static int yaffs_skip_nand_verification(struct yaffs_dev *dev)
  30. {
  31. (void) dev;
  32. return !(yaffs_trace_mask & (YAFFS_TRACE_VERIFY_NAND));
  33. }
  34. static const char * const block_state_name[] = {
  35. "Unknown",
  36. "Needs scan",
  37. "Scanning",
  38. "Empty",
  39. "Allocating",
  40. "Full",
  41. "Dirty",
  42. "Checkpoint",
  43. "Collecting",
  44. "Dead"
  45. };
  46. void yaffs_verify_blk(struct yaffs_dev *dev, struct yaffs_block_info *bi, int n)
  47. {
  48. int actually_used;
  49. int in_use;
  50. if (yaffs_skip_verification(dev))
  51. return;
  52. /* Report illegal runtime states */
  53. if (bi->block_state >= YAFFS_NUMBER_OF_BLOCK_STATES)
  54. yaffs_trace(YAFFS_TRACE_VERIFY,
  55. "Block %d has undefined state %d",
  56. n, bi->block_state);
  57. switch (bi->block_state) {
  58. case YAFFS_BLOCK_STATE_UNKNOWN:
  59. case YAFFS_BLOCK_STATE_SCANNING:
  60. case YAFFS_BLOCK_STATE_NEEDS_SCAN:
  61. yaffs_trace(YAFFS_TRACE_VERIFY,
  62. "Block %d has bad run-state %s",
  63. n, block_state_name[bi->block_state]);
  64. }
  65. /* Check pages in use and soft deletions are legal */
  66. actually_used = bi->pages_in_use - bi->soft_del_pages;
  67. if (bi->pages_in_use < 0 ||
  68. bi->pages_in_use > dev->param.chunks_per_block ||
  69. bi->soft_del_pages < 0 ||
  70. bi->soft_del_pages > dev->param.chunks_per_block ||
  71. actually_used < 0 || actually_used > dev->param.chunks_per_block)
  72. yaffs_trace(YAFFS_TRACE_VERIFY,
  73. "Block %d has illegal values pages_in_used %d soft_del_pages %d",
  74. n, bi->pages_in_use, bi->soft_del_pages);
  75. /* Check chunk bitmap legal */
  76. in_use = yaffs_count_chunk_bits(dev, n);
  77. if (in_use != bi->pages_in_use)
  78. yaffs_trace(YAFFS_TRACE_VERIFY,
  79. "Block %d has inconsistent values pages_in_use %d counted chunk bits %d",
  80. n, bi->pages_in_use, in_use);
  81. }
  82. void yaffs_verify_collected_blk(struct yaffs_dev *dev,
  83. struct yaffs_block_info *bi, int n)
  84. {
  85. yaffs_verify_blk(dev, bi, n);
  86. /* After collection the block should be in the erased state */
  87. if (bi->block_state != YAFFS_BLOCK_STATE_COLLECTING &&
  88. bi->block_state != YAFFS_BLOCK_STATE_EMPTY) {
  89. yaffs_trace(YAFFS_TRACE_ERROR,
  90. "Block %d is in state %d after gc, should be erased",
  91. n, bi->block_state);
  92. }
  93. }
  94. void yaffs_verify_blocks(struct yaffs_dev *dev)
  95. {
  96. int i;
  97. int state_count[YAFFS_NUMBER_OF_BLOCK_STATES];
  98. int illegal_states = 0;
  99. if (yaffs_skip_verification(dev))
  100. return;
  101. memset(state_count, 0, sizeof(state_count));
  102. for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
  103. struct yaffs_block_info *bi = yaffs_get_block_info(dev, i);
  104. yaffs_verify_blk(dev, bi, i);
  105. if (bi->block_state < YAFFS_NUMBER_OF_BLOCK_STATES)
  106. state_count[bi->block_state]++;
  107. else
  108. illegal_states++;
  109. }
  110. yaffs_trace(YAFFS_TRACE_VERIFY, "Block summary");
  111. yaffs_trace(YAFFS_TRACE_VERIFY,
  112. "%d blocks have illegal states",
  113. illegal_states);
  114. if (state_count[YAFFS_BLOCK_STATE_ALLOCATING] > 1)
  115. yaffs_trace(YAFFS_TRACE_VERIFY,
  116. "Too many allocating blocks");
  117. for (i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++)
  118. yaffs_trace(YAFFS_TRACE_VERIFY,
  119. "%s %d blocks",
  120. block_state_name[i], state_count[i]);
  121. if (dev->blocks_in_checkpt != state_count[YAFFS_BLOCK_STATE_CHECKPOINT])
  122. yaffs_trace(YAFFS_TRACE_VERIFY,
  123. "Checkpoint block count wrong dev %d count %d",
  124. dev->blocks_in_checkpt,
  125. state_count[YAFFS_BLOCK_STATE_CHECKPOINT]);
  126. if (dev->n_erased_blocks != state_count[YAFFS_BLOCK_STATE_EMPTY])
  127. yaffs_trace(YAFFS_TRACE_VERIFY,
  128. "Erased block count wrong dev %d count %d",
  129. dev->n_erased_blocks,
  130. state_count[YAFFS_BLOCK_STATE_EMPTY]);
  131. if (state_count[YAFFS_BLOCK_STATE_COLLECTING] > 1)
  132. yaffs_trace(YAFFS_TRACE_VERIFY,
  133. "Too many collecting blocks %d (max is 1)",
  134. state_count[YAFFS_BLOCK_STATE_COLLECTING]);
  135. }
  136. /*
  137. * Verify the object header. oh must be valid, but obj and tags may be NULL in
  138. * which case those tests will not be performed.
  139. */
  140. void yaffs_verify_oh(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh,
  141. struct yaffs_ext_tags *tags, int parent_check)
  142. {
  143. if (obj && yaffs_skip_verification(obj->my_dev))
  144. return;
  145. if (!(tags && obj && oh)) {
  146. yaffs_trace(YAFFS_TRACE_VERIFY,
  147. "Verifying object header tags %p obj %p oh %p",
  148. tags, obj, oh);
  149. return;
  150. }
  151. if (oh->type <= YAFFS_OBJECT_TYPE_UNKNOWN ||
  152. oh->type > YAFFS_OBJECT_TYPE_MAX)
  153. yaffs_trace(YAFFS_TRACE_VERIFY,
  154. "Obj %d header type is illegal value 0x%x",
  155. tags->obj_id, oh->type);
  156. if (tags->obj_id != obj->obj_id)
  157. yaffs_trace(YAFFS_TRACE_VERIFY,
  158. "Obj %d header mismatch obj_id %d",
  159. tags->obj_id, obj->obj_id);
  160. /*
  161. * Check that the object's parent ids match if parent_check requested.
  162. *
  163. * Tests do not apply to the root object.
  164. */
  165. if (parent_check && tags->obj_id > 1 && !obj->parent)
  166. yaffs_trace(YAFFS_TRACE_VERIFY,
  167. "Obj %d header mismatch parent_id %d obj->parent is NULL",
  168. tags->obj_id, oh->parent_obj_id);
  169. if (parent_check && obj->parent &&
  170. oh->parent_obj_id != obj->parent->obj_id &&
  171. (oh->parent_obj_id != YAFFS_OBJECTID_UNLINKED ||
  172. obj->parent->obj_id != YAFFS_OBJECTID_DELETED))
  173. yaffs_trace(YAFFS_TRACE_VERIFY,
  174. "Obj %d header mismatch parent_id %d parent_obj_id %d",
  175. tags->obj_id, oh->parent_obj_id,
  176. obj->parent->obj_id);
  177. if (tags->obj_id > 1 && oh->name[0] == 0) /* Null name */
  178. yaffs_trace(YAFFS_TRACE_VERIFY,
  179. "Obj %d header name is NULL",
  180. obj->obj_id);
  181. if (tags->obj_id > 1 && ((u8) (oh->name[0])) == 0xff) /* Junk name */
  182. yaffs_trace(YAFFS_TRACE_VERIFY,
  183. "Obj %d header name is 0xff",
  184. obj->obj_id);
  185. }
  186. void yaffs_verify_file(struct yaffs_obj *obj)
  187. {
  188. u32 x;
  189. int required_depth;
  190. int actual_depth;
  191. int last_chunk;
  192. u32 offset_in_chunk;
  193. u32 the_chunk;
  194. u32 i;
  195. struct yaffs_dev *dev;
  196. struct yaffs_ext_tags tags;
  197. struct yaffs_tnode *tn;
  198. u32 obj_id;
  199. if (!obj)
  200. return;
  201. if (yaffs_skip_verification(obj->my_dev))
  202. return;
  203. dev = obj->my_dev;
  204. obj_id = obj->obj_id;
  205. /* Check file size is consistent with tnode depth */
  206. yaffs_addr_to_chunk(dev, obj->variant.file_variant.file_size,
  207. &last_chunk, &offset_in_chunk);
  208. last_chunk++;
  209. x = last_chunk >> YAFFS_TNODES_LEVEL0_BITS;
  210. required_depth = 0;
  211. while (x > 0) {
  212. x >>= YAFFS_TNODES_INTERNAL_BITS;
  213. required_depth++;
  214. }
  215. actual_depth = obj->variant.file_variant.top_level;
  216. /* Check that the chunks in the tnode tree are all correct.
  217. * We do this by scanning through the tnode tree and
  218. * checking the tags for every chunk match.
  219. */
  220. if (yaffs_skip_nand_verification(dev))
  221. return;
  222. for (i = 1; i <= last_chunk; i++) {
  223. tn = yaffs_find_tnode_0(dev, &obj->variant.file_variant, i);
  224. if (!tn)
  225. continue;
  226. the_chunk = yaffs_get_group_base(dev, tn, i);
  227. if (the_chunk > 0) {
  228. yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
  229. &tags);
  230. if (tags.obj_id != obj_id || tags.chunk_id != i)
  231. yaffs_trace(YAFFS_TRACE_VERIFY,
  232. "Object %d chunk_id %d NAND mismatch chunk %d tags (%d:%d)",
  233. obj_id, i, the_chunk,
  234. tags.obj_id, tags.chunk_id);
  235. }
  236. }
  237. }
  238. void yaffs_verify_link(struct yaffs_obj *obj)
  239. {
  240. if (obj && yaffs_skip_verification(obj->my_dev))
  241. return;
  242. /* Verify sane equivalent object */
  243. }
  244. void yaffs_verify_symlink(struct yaffs_obj *obj)
  245. {
  246. if (obj && yaffs_skip_verification(obj->my_dev))
  247. return;
  248. /* Verify symlink string */
  249. }
  250. void yaffs_verify_special(struct yaffs_obj *obj)
  251. {
  252. if (obj && yaffs_skip_verification(obj->my_dev))
  253. return;
  254. }
  255. void yaffs_verify_obj(struct yaffs_obj *obj)
  256. {
  257. struct yaffs_dev *dev;
  258. u32 chunk_min;
  259. u32 chunk_max;
  260. u32 chunk_id_ok;
  261. u32 chunk_in_range;
  262. u32 chunk_wrongly_deleted;
  263. u32 chunk_valid;
  264. if (!obj)
  265. return;
  266. if (obj->being_created)
  267. return;
  268. dev = obj->my_dev;
  269. if (yaffs_skip_verification(dev))
  270. return;
  271. /* Check sane object header chunk */
  272. chunk_min = dev->internal_start_block * dev->param.chunks_per_block;
  273. chunk_max =
  274. (dev->internal_end_block + 1) * dev->param.chunks_per_block - 1;
  275. chunk_in_range = (((unsigned)(obj->hdr_chunk)) >= chunk_min &&
  276. ((unsigned)(obj->hdr_chunk)) <= chunk_max);
  277. chunk_id_ok = chunk_in_range || (obj->hdr_chunk == 0);
  278. chunk_valid = chunk_in_range &&
  279. yaffs_check_chunk_bit(dev,
  280. obj->hdr_chunk / dev->param.chunks_per_block,
  281. obj->hdr_chunk % dev->param.chunks_per_block);
  282. chunk_wrongly_deleted = chunk_in_range && !chunk_valid;
  283. if (!obj->fake && (!chunk_id_ok || chunk_wrongly_deleted))
  284. yaffs_trace(YAFFS_TRACE_VERIFY,
  285. "Obj %d has chunk_id %d %s %s",
  286. obj->obj_id, obj->hdr_chunk,
  287. chunk_id_ok ? "" : ",out of range",
  288. chunk_wrongly_deleted ? ",marked as deleted" : "");
  289. if (chunk_valid && !yaffs_skip_nand_verification(dev)) {
  290. struct yaffs_ext_tags tags;
  291. struct yaffs_obj_hdr *oh;
  292. u8 *buffer = yaffs_get_temp_buffer(dev);
  293. oh = (struct yaffs_obj_hdr *)buffer;
  294. yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, buffer, &tags);
  295. yaffs_verify_oh(obj, oh, &tags, 1);
  296. yaffs_release_temp_buffer(dev, buffer);
  297. }
  298. /* Verify it has a parent */
  299. if (obj && !obj->fake && (!obj->parent || obj->parent->my_dev != dev)) {
  300. yaffs_trace(YAFFS_TRACE_VERIFY,
  301. "Obj %d has parent pointer %p which does not look like an object",
  302. obj->obj_id, obj->parent);
  303. }
  304. /* Verify parent is a directory */
  305. if (obj->parent &&
  306. obj->parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  307. yaffs_trace(YAFFS_TRACE_VERIFY,
  308. "Obj %d's parent is not a directory (type %d)",
  309. obj->obj_id, obj->parent->variant_type);
  310. }
  311. switch (obj->variant_type) {
  312. case YAFFS_OBJECT_TYPE_FILE:
  313. yaffs_verify_file(obj);
  314. break;
  315. case YAFFS_OBJECT_TYPE_SYMLINK:
  316. yaffs_verify_symlink(obj);
  317. break;
  318. case YAFFS_OBJECT_TYPE_DIRECTORY:
  319. yaffs_verify_dir(obj);
  320. break;
  321. case YAFFS_OBJECT_TYPE_HARDLINK:
  322. yaffs_verify_link(obj);
  323. break;
  324. case YAFFS_OBJECT_TYPE_SPECIAL:
  325. yaffs_verify_special(obj);
  326. break;
  327. case YAFFS_OBJECT_TYPE_UNKNOWN:
  328. default:
  329. yaffs_trace(YAFFS_TRACE_VERIFY,
  330. "Obj %d has illegaltype %d",
  331. obj->obj_id, obj->variant_type);
  332. break;
  333. }
  334. }
  335. void yaffs_verify_objects(struct yaffs_dev *dev)
  336. {
  337. struct yaffs_obj *obj;
  338. int i;
  339. struct list_head *lh;
  340. if (yaffs_skip_verification(dev))
  341. return;
  342. /* Iterate through the objects in each hash entry */
  343. for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
  344. list_for_each(lh, &dev->obj_bucket[i].list) {
  345. obj = list_entry(lh, struct yaffs_obj, hash_link);
  346. yaffs_verify_obj(obj);
  347. }
  348. }
  349. }
  350. void yaffs_verify_obj_in_dir(struct yaffs_obj *obj)
  351. {
  352. struct list_head *lh;
  353. struct yaffs_obj *list_obj;
  354. int count = 0;
  355. if (!obj) {
  356. yaffs_trace(YAFFS_TRACE_ALWAYS, "No object to verify");
  357. BUG();
  358. return;
  359. }
  360. if (yaffs_skip_verification(obj->my_dev))
  361. return;
  362. if (!obj->parent) {
  363. yaffs_trace(YAFFS_TRACE_ALWAYS, "Object does not have parent");
  364. BUG();
  365. return;
  366. }
  367. if (obj->parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  368. yaffs_trace(YAFFS_TRACE_ALWAYS, "Parent is not directory");
  369. BUG();
  370. }
  371. /* Iterate through the objects in each hash entry */
  372. list_for_each(lh, &obj->parent->variant.dir_variant.children) {
  373. list_obj = list_entry(lh, struct yaffs_obj, siblings);
  374. yaffs_verify_obj(list_obj);
  375. if (obj == list_obj)
  376. count++;
  377. }
  378. if (count != 1) {
  379. yaffs_trace(YAFFS_TRACE_ALWAYS,
  380. "Object in directory %d times",
  381. count);
  382. BUG();
  383. }
  384. }
  385. void yaffs_verify_dir(struct yaffs_obj *directory)
  386. {
  387. struct list_head *lh;
  388. struct yaffs_obj *list_obj;
  389. if (!directory) {
  390. BUG();
  391. return;
  392. }
  393. if (yaffs_skip_full_verification(directory->my_dev))
  394. return;
  395. if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  396. yaffs_trace(YAFFS_TRACE_ALWAYS,
  397. "Directory has wrong type: %d",
  398. directory->variant_type);
  399. BUG();
  400. }
  401. /* Iterate through the objects in each hash entry */
  402. list_for_each(lh, &directory->variant.dir_variant.children) {
  403. list_obj = list_entry(lh, struct yaffs_obj, siblings);
  404. if (list_obj->parent != directory) {
  405. yaffs_trace(YAFFS_TRACE_ALWAYS,
  406. "Object in directory list has wrong parent %p",
  407. list_obj->parent);
  408. BUG();
  409. }
  410. yaffs_verify_obj_in_dir(list_obj);
  411. }
  412. }
  413. static int yaffs_free_verification_failures;
  414. void yaffs_verify_free_chunks(struct yaffs_dev *dev)
  415. {
  416. int counted;
  417. int difference;
  418. if (yaffs_skip_verification(dev))
  419. return;
  420. counted = yaffs_count_free_chunks(dev);
  421. difference = dev->n_free_chunks - counted;
  422. if (difference) {
  423. yaffs_trace(YAFFS_TRACE_ALWAYS,
  424. "Freechunks verification failure %d %d %d",
  425. dev->n_free_chunks, counted, difference);
  426. yaffs_free_verification_failures++;
  427. }
  428. }
  429. int yaffs_verify_file_sane(struct yaffs_obj *in)
  430. {
  431. (void) in;
  432. return YAFFS_OK;
  433. }