yaffs_verify.c 13 KB

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