yaffs_verify.c 13 KB

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