yaffs_verify.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. dev = 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. dev = dev;
  27. return !(yaffs_trace_mask & (YAFFS_TRACE_VERIFY_FULL));
  28. }
  29. static int yaffs_skip_nand_verification(struct yaffs_dev *dev)
  30. {
  31. dev = 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 last_chunk;
  191. u32 offset_in_chunk;
  192. u32 the_chunk;
  193. u32 i;
  194. struct yaffs_dev *dev;
  195. struct yaffs_ext_tags tags;
  196. struct yaffs_tnode *tn;
  197. u32 obj_id;
  198. if (!obj)
  199. return;
  200. if (yaffs_skip_verification(obj->my_dev))
  201. return;
  202. dev = obj->my_dev;
  203. obj_id = obj->obj_id;
  204. /* Check file size is consistent with tnode depth */
  205. yaffs_addr_to_chunk(dev, obj->variant.file_variant.file_size,
  206. &last_chunk, &offset_in_chunk);
  207. last_chunk++;
  208. x = last_chunk >> YAFFS_TNODES_LEVEL0_BITS;
  209. required_depth = 0;
  210. while (x > 0) {
  211. x >>= YAFFS_TNODES_INTERNAL_BITS;
  212. required_depth++;
  213. }
  214. /* Check that the chunks in the tnode tree are all correct.
  215. * We do this by scanning through the tnode tree and
  216. * checking the tags for every chunk match.
  217. */
  218. if (yaffs_skip_nand_verification(dev))
  219. return;
  220. for (i = 1; i <= last_chunk; i++) {
  221. tn = yaffs_find_tnode_0(dev, &obj->variant.file_variant, i);
  222. if (!tn)
  223. continue;
  224. the_chunk = yaffs_get_group_base(dev, tn, i);
  225. if (the_chunk > 0) {
  226. yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
  227. &tags);
  228. if (tags.obj_id != obj_id || tags.chunk_id != i)
  229. yaffs_trace(YAFFS_TRACE_VERIFY,
  230. "Object %d chunk_id %d NAND mismatch chunk %d tags (%d:%d)",
  231. obj_id, i, the_chunk,
  232. tags.obj_id, tags.chunk_id);
  233. }
  234. }
  235. }
  236. void yaffs_verify_link(struct yaffs_obj *obj)
  237. {
  238. if (obj && yaffs_skip_verification(obj->my_dev))
  239. return;
  240. /* Verify sane equivalent object */
  241. }
  242. void yaffs_verify_symlink(struct yaffs_obj *obj)
  243. {
  244. if (obj && yaffs_skip_verification(obj->my_dev))
  245. return;
  246. /* Verify symlink string */
  247. }
  248. void yaffs_verify_special(struct yaffs_obj *obj)
  249. {
  250. if (obj && yaffs_skip_verification(obj->my_dev))
  251. return;
  252. }
  253. void yaffs_verify_obj(struct yaffs_obj *obj)
  254. {
  255. struct yaffs_dev *dev;
  256. u32 chunk_min;
  257. u32 chunk_max;
  258. u32 chunk_id_ok;
  259. u32 chunk_in_range;
  260. u32 chunk_wrongly_deleted;
  261. u32 chunk_valid;
  262. if (!obj)
  263. return;
  264. if (obj->being_created)
  265. return;
  266. dev = obj->my_dev;
  267. if (yaffs_skip_verification(dev))
  268. return;
  269. /* Check sane object header chunk */
  270. chunk_min = dev->internal_start_block * dev->param.chunks_per_block;
  271. chunk_max =
  272. (dev->internal_end_block + 1) * dev->param.chunks_per_block - 1;
  273. chunk_in_range = (((unsigned)(obj->hdr_chunk)) >= chunk_min &&
  274. ((unsigned)(obj->hdr_chunk)) <= chunk_max);
  275. chunk_id_ok = chunk_in_range || (obj->hdr_chunk == 0);
  276. chunk_valid = chunk_in_range &&
  277. yaffs_check_chunk_bit(dev,
  278. obj->hdr_chunk / dev->param.chunks_per_block,
  279. obj->hdr_chunk % dev->param.chunks_per_block);
  280. chunk_wrongly_deleted = chunk_in_range && !chunk_valid;
  281. if (!obj->fake && (!chunk_id_ok || chunk_wrongly_deleted))
  282. yaffs_trace(YAFFS_TRACE_VERIFY,
  283. "Obj %d has chunk_id %d %s %s",
  284. obj->obj_id, obj->hdr_chunk,
  285. chunk_id_ok ? "" : ",out of range",
  286. chunk_wrongly_deleted ? ",marked as deleted" : "");
  287. if (chunk_valid && !yaffs_skip_nand_verification(dev)) {
  288. struct yaffs_ext_tags tags;
  289. struct yaffs_obj_hdr *oh;
  290. u8 *buffer = yaffs_get_temp_buffer(dev);
  291. oh = (struct yaffs_obj_hdr *)buffer;
  292. yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, buffer, &tags);
  293. yaffs_verify_oh(obj, oh, &tags, 1);
  294. yaffs_release_temp_buffer(dev, buffer);
  295. }
  296. /* Verify it has a parent */
  297. if (obj && !obj->fake && (!obj->parent || obj->parent->my_dev != dev)) {
  298. yaffs_trace(YAFFS_TRACE_VERIFY,
  299. "Obj %d has parent pointer %p which does not look like an object",
  300. obj->obj_id, obj->parent);
  301. }
  302. /* Verify parent is a directory */
  303. if (obj->parent &&
  304. obj->parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  305. yaffs_trace(YAFFS_TRACE_VERIFY,
  306. "Obj %d's parent is not a directory (type %d)",
  307. obj->obj_id, obj->parent->variant_type);
  308. }
  309. switch (obj->variant_type) {
  310. case YAFFS_OBJECT_TYPE_FILE:
  311. yaffs_verify_file(obj);
  312. break;
  313. case YAFFS_OBJECT_TYPE_SYMLINK:
  314. yaffs_verify_symlink(obj);
  315. break;
  316. case YAFFS_OBJECT_TYPE_DIRECTORY:
  317. yaffs_verify_dir(obj);
  318. break;
  319. case YAFFS_OBJECT_TYPE_HARDLINK:
  320. yaffs_verify_link(obj);
  321. break;
  322. case YAFFS_OBJECT_TYPE_SPECIAL:
  323. yaffs_verify_special(obj);
  324. break;
  325. case YAFFS_OBJECT_TYPE_UNKNOWN:
  326. default:
  327. yaffs_trace(YAFFS_TRACE_VERIFY,
  328. "Obj %d has illegaltype %d",
  329. obj->obj_id, obj->variant_type);
  330. break;
  331. }
  332. }
  333. void yaffs_verify_objects(struct yaffs_dev *dev)
  334. {
  335. struct yaffs_obj *obj;
  336. int i;
  337. struct list_head *lh;
  338. if (yaffs_skip_verification(dev))
  339. return;
  340. /* Iterate through the objects in each hash entry */
  341. for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
  342. list_for_each(lh, &dev->obj_bucket[i].list) {
  343. obj = list_entry(lh, struct yaffs_obj, hash_link);
  344. yaffs_verify_obj(obj);
  345. }
  346. }
  347. }
  348. void yaffs_verify_obj_in_dir(struct yaffs_obj *obj)
  349. {
  350. struct list_head *lh;
  351. struct yaffs_obj *list_obj;
  352. int count = 0;
  353. if (!obj) {
  354. yaffs_trace(YAFFS_TRACE_ALWAYS, "No object to verify");
  355. BUG();
  356. return;
  357. }
  358. if (yaffs_skip_verification(obj->my_dev))
  359. return;
  360. if (!obj->parent) {
  361. yaffs_trace(YAFFS_TRACE_ALWAYS, "Object does not have parent");
  362. BUG();
  363. return;
  364. }
  365. if (obj->parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  366. yaffs_trace(YAFFS_TRACE_ALWAYS, "Parent is not directory");
  367. BUG();
  368. }
  369. /* Iterate through the objects in each hash entry */
  370. list_for_each(lh, &obj->parent->variant.dir_variant.children) {
  371. list_obj = list_entry(lh, struct yaffs_obj, siblings);
  372. yaffs_verify_obj(list_obj);
  373. if (obj == list_obj)
  374. count++;
  375. }
  376. if (count != 1) {
  377. yaffs_trace(YAFFS_TRACE_ALWAYS,
  378. "Object in directory %d times",
  379. count);
  380. BUG();
  381. }
  382. }
  383. void yaffs_verify_dir(struct yaffs_obj *directory)
  384. {
  385. struct list_head *lh;
  386. struct yaffs_obj *list_obj;
  387. if (!directory) {
  388. BUG();
  389. return;
  390. }
  391. if (yaffs_skip_full_verification(directory->my_dev))
  392. return;
  393. if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  394. yaffs_trace(YAFFS_TRACE_ALWAYS,
  395. "Directory has wrong type: %d",
  396. directory->variant_type);
  397. BUG();
  398. }
  399. /* Iterate through the objects in each hash entry */
  400. list_for_each(lh, &directory->variant.dir_variant.children) {
  401. list_obj = list_entry(lh, struct yaffs_obj, siblings);
  402. if (list_obj->parent != directory) {
  403. yaffs_trace(YAFFS_TRACE_ALWAYS,
  404. "Object in directory list has wrong parent %p",
  405. list_obj->parent);
  406. BUG();
  407. }
  408. yaffs_verify_obj_in_dir(list_obj);
  409. }
  410. }
  411. static int yaffs_free_verification_failures;
  412. void yaffs_verify_free_chunks(struct yaffs_dev *dev)
  413. {
  414. int counted;
  415. int difference;
  416. if (yaffs_skip_verification(dev))
  417. return;
  418. counted = yaffs_count_free_chunks(dev);
  419. difference = dev->n_free_chunks - counted;
  420. if (difference) {
  421. yaffs_trace(YAFFS_TRACE_ALWAYS,
  422. "Freechunks verification failure %d %d %d",
  423. dev->n_free_chunks, counted, difference);
  424. yaffs_free_verification_failures++;
  425. }
  426. }
  427. int yaffs_verify_file_sane(struct yaffs_obj *in)
  428. {
  429. in = in;
  430. return YAFFS_OK;
  431. }