yaffs_checkptrw.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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_checkptrw.h"
  14. #include "yaffs_getblockinfo.h"
  15. struct yaffs_checkpt_chunk_hdr {
  16. int version;
  17. int seq;
  18. u32 sum;
  19. u32 xor;
  20. } ;
  21. static int apply_chunk_offset(struct yaffs_dev *dev, int chunk)
  22. {
  23. return chunk - dev->chunk_offset;
  24. }
  25. static int apply_block_offset(struct yaffs_dev *dev, int block)
  26. {
  27. return block - dev->block_offset;
  28. }
  29. static void yaffs2_checkpt_init_chunk_hdr(struct yaffs_dev *dev)
  30. {
  31. struct yaffs_checkpt_chunk_hdr hdr;
  32. hdr.version = YAFFS_CHECKPOINT_VERSION;
  33. hdr.seq = dev->checkpt_page_seq;
  34. hdr.sum = dev->checkpt_sum;
  35. hdr.xor = dev->checkpt_xor;
  36. dev->checkpt_byte_offs = sizeof(hdr);
  37. memcpy(dev->checkpt_buffer, &hdr, sizeof(hdr));
  38. }
  39. static int yaffs2_checkpt_check_chunk_hdr(struct yaffs_dev *dev)
  40. {
  41. struct yaffs_checkpt_chunk_hdr hdr;
  42. memcpy(&hdr, dev->checkpt_buffer, sizeof(hdr));
  43. dev->checkpt_byte_offs = sizeof(hdr);
  44. return hdr.version == YAFFS_CHECKPOINT_VERSION &&
  45. hdr.seq == dev->checkpt_page_seq &&
  46. hdr.sum == dev->checkpt_sum &&
  47. hdr.xor == dev->checkpt_xor;
  48. }
  49. static int yaffs2_checkpt_space_ok(struct yaffs_dev *dev)
  50. {
  51. int blocks_avail = dev->n_erased_blocks - dev->param.n_reserved_blocks;
  52. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  53. "checkpt blocks_avail = %d", blocks_avail);
  54. return (blocks_avail <= 0) ? 0 : 1;
  55. }
  56. static int yaffs_checkpt_erase(struct yaffs_dev *dev)
  57. {
  58. int i;
  59. if (!dev->drv.drv_erase_fn)
  60. return 0;
  61. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  62. "checking blocks %d to %d",
  63. dev->internal_start_block, dev->internal_end_block);
  64. for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
  65. struct yaffs_block_info *bi = yaffs_get_block_info(dev, i);
  66. int offset_i = apply_block_offset(dev, i);
  67. int result;
  68. if (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT) {
  69. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  70. "erasing checkpt block %d", i);
  71. dev->n_erasures++;
  72. result = dev->drv.drv_erase_fn(dev, offset_i);
  73. if(result) {
  74. bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
  75. dev->n_erased_blocks++;
  76. dev->n_free_chunks +=
  77. dev->param.chunks_per_block;
  78. } else {
  79. dev->drv.drv_mark_bad_fn(dev, offset_i);
  80. bi->block_state = YAFFS_BLOCK_STATE_DEAD;
  81. }
  82. }
  83. }
  84. dev->blocks_in_checkpt = 0;
  85. return 1;
  86. }
  87. static void yaffs2_checkpt_find_erased_block(struct yaffs_dev *dev)
  88. {
  89. int i;
  90. int blocks_avail = dev->n_erased_blocks - dev->param.n_reserved_blocks;
  91. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  92. "allocating checkpt block: erased %d reserved %d avail %d next %d ",
  93. dev->n_erased_blocks, dev->param.n_reserved_blocks,
  94. blocks_avail, dev->checkpt_next_block);
  95. if (dev->checkpt_next_block >= 0 &&
  96. dev->checkpt_next_block <= dev->internal_end_block &&
  97. blocks_avail > 0) {
  98. for (i = dev->checkpt_next_block; i <= dev->internal_end_block;
  99. i++) {
  100. struct yaffs_block_info *bi;
  101. bi = yaffs_get_block_info(dev, i);
  102. if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
  103. dev->checkpt_next_block = i + 1;
  104. dev->checkpt_cur_block = i;
  105. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  106. "allocating checkpt block %d", i);
  107. return;
  108. }
  109. }
  110. }
  111. yaffs_trace(YAFFS_TRACE_CHECKPOINT, "out of checkpt blocks");
  112. dev->checkpt_next_block = -1;
  113. dev->checkpt_cur_block = -1;
  114. }
  115. static void yaffs2_checkpt_find_block(struct yaffs_dev *dev)
  116. {
  117. int i;
  118. struct yaffs_ext_tags tags;
  119. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  120. "find next checkpt block: start: blocks %d next %d",
  121. dev->blocks_in_checkpt, dev->checkpt_next_block);
  122. if (dev->blocks_in_checkpt < dev->checkpt_max_blocks)
  123. for (i = dev->checkpt_next_block; i <= dev->internal_end_block;
  124. i++) {
  125. int chunk = i * dev->param.chunks_per_block;
  126. enum yaffs_block_state state;
  127. u32 seq;
  128. dev->tagger.read_chunk_tags_fn(dev,
  129. apply_chunk_offset(dev, chunk),
  130. NULL, &tags);
  131. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  132. "find next checkpt block: search: block %d state %d oid %d seq %d eccr %d",
  133. i, (int) state,
  134. tags.obj_id, tags.seq_number,
  135. tags.ecc_result);
  136. if (tags.seq_number != YAFFS_SEQUENCE_CHECKPOINT_DATA)
  137. continue;
  138. dev->tagger.query_block_fn(dev,
  139. apply_block_offset(dev, i),
  140. &state, &seq);
  141. if (state == YAFFS_BLOCK_STATE_DEAD)
  142. continue;
  143. /* Right kind of block */
  144. dev->checkpt_next_block = tags.obj_id;
  145. dev->checkpt_cur_block = i;
  146. dev->checkpt_block_list[dev->blocks_in_checkpt] = i;
  147. dev->blocks_in_checkpt++;
  148. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  149. "found checkpt block %d", i);
  150. return;
  151. }
  152. yaffs_trace(YAFFS_TRACE_CHECKPOINT, "found no more checkpt blocks");
  153. dev->checkpt_next_block = -1;
  154. dev->checkpt_cur_block = -1;
  155. }
  156. int yaffs2_checkpt_open(struct yaffs_dev *dev, int writing)
  157. {
  158. int i;
  159. dev->checkpt_open_write = writing;
  160. /* Got the functions we need? */
  161. if (!dev->tagger.write_chunk_tags_fn ||
  162. !dev->tagger.read_chunk_tags_fn ||
  163. !dev->drv.drv_erase_fn ||
  164. !dev->drv.drv_mark_bad_fn)
  165. return 0;
  166. if (writing && !yaffs2_checkpt_space_ok(dev))
  167. return 0;
  168. if (!dev->checkpt_buffer)
  169. dev->checkpt_buffer =
  170. kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
  171. if (!dev->checkpt_buffer)
  172. return 0;
  173. dev->checkpt_page_seq = 0;
  174. dev->checkpt_byte_count = 0;
  175. dev->checkpt_sum = 0;
  176. dev->checkpt_xor = 0;
  177. dev->checkpt_cur_block = -1;
  178. dev->checkpt_cur_chunk = -1;
  179. dev->checkpt_next_block = dev->internal_start_block;
  180. if (writing) {
  181. memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
  182. yaffs2_checkpt_init_chunk_hdr(dev);
  183. return yaffs_checkpt_erase(dev);
  184. }
  185. /* Opening for a read */
  186. /* Set to a value that will kick off a read */
  187. dev->checkpt_byte_offs = dev->data_bytes_per_chunk;
  188. /* A checkpoint block list of 1 checkpoint block per 16 block is
  189. * (hopefully) going to be way more than we need */
  190. dev->blocks_in_checkpt = 0;
  191. dev->checkpt_max_blocks =
  192. (dev->internal_end_block - dev->internal_start_block) / 16 + 2;
  193. if (!dev->checkpt_block_list)
  194. dev->checkpt_block_list =
  195. kmalloc(sizeof(int) * dev->checkpt_max_blocks, GFP_NOFS);
  196. if (!dev->checkpt_block_list)
  197. return 0;
  198. for (i = 0; i < dev->checkpt_max_blocks; i++)
  199. dev->checkpt_block_list[i] = -1;
  200. return 1;
  201. }
  202. int yaffs2_get_checkpt_sum(struct yaffs_dev *dev, u32 * sum)
  203. {
  204. u32 composite_sum;
  205. composite_sum = (dev->checkpt_sum << 8) | (dev->checkpt_xor & 0xff);
  206. *sum = composite_sum;
  207. return 1;
  208. }
  209. static int yaffs2_checkpt_flush_buffer(struct yaffs_dev *dev)
  210. {
  211. int chunk;
  212. int offset_chunk;
  213. struct yaffs_ext_tags tags;
  214. if (dev->checkpt_cur_block < 0) {
  215. yaffs2_checkpt_find_erased_block(dev);
  216. dev->checkpt_cur_chunk = 0;
  217. }
  218. if (dev->checkpt_cur_block < 0)
  219. return 0;
  220. tags.is_deleted = 0;
  221. tags.obj_id = dev->checkpt_next_block; /* Hint to next place to look */
  222. tags.chunk_id = dev->checkpt_page_seq + 1;
  223. tags.seq_number = YAFFS_SEQUENCE_CHECKPOINT_DATA;
  224. tags.n_bytes = dev->data_bytes_per_chunk;
  225. if (dev->checkpt_cur_chunk == 0) {
  226. /* First chunk we write for the block? Set block state to
  227. checkpoint */
  228. struct yaffs_block_info *bi =
  229. yaffs_get_block_info(dev, dev->checkpt_cur_block);
  230. bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
  231. dev->blocks_in_checkpt++;
  232. }
  233. chunk =
  234. dev->checkpt_cur_block * dev->param.chunks_per_block +
  235. dev->checkpt_cur_chunk;
  236. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  237. "checkpoint wite buffer nand %d(%d:%d) objid %d chId %d",
  238. chunk, dev->checkpt_cur_block, dev->checkpt_cur_chunk,
  239. tags.obj_id, tags.chunk_id);
  240. offset_chunk = apply_chunk_offset(dev, chunk);
  241. dev->n_page_writes++;
  242. dev->tagger.write_chunk_tags_fn(dev, offset_chunk,
  243. dev->checkpt_buffer, &tags);
  244. dev->checkpt_page_seq++;
  245. dev->checkpt_cur_chunk++;
  246. if (dev->checkpt_cur_chunk >= dev->param.chunks_per_block) {
  247. dev->checkpt_cur_chunk = 0;
  248. dev->checkpt_cur_block = -1;
  249. }
  250. memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
  251. yaffs2_checkpt_init_chunk_hdr(dev);
  252. return 1;
  253. }
  254. int yaffs2_checkpt_wr(struct yaffs_dev *dev, const void *data, int n_bytes)
  255. {
  256. int i = 0;
  257. int ok = 1;
  258. u8 *data_bytes = (u8 *) data;
  259. if (!dev->checkpt_buffer)
  260. return 0;
  261. if (!dev->checkpt_open_write)
  262. return -1;
  263. while (i < n_bytes && ok) {
  264. dev->checkpt_buffer[dev->checkpt_byte_offs] = *data_bytes;
  265. dev->checkpt_sum += *data_bytes;
  266. dev->checkpt_xor ^= *data_bytes;
  267. dev->checkpt_byte_offs++;
  268. i++;
  269. data_bytes++;
  270. dev->checkpt_byte_count++;
  271. if (dev->checkpt_byte_offs < 0 ||
  272. dev->checkpt_byte_offs >= dev->data_bytes_per_chunk)
  273. ok = yaffs2_checkpt_flush_buffer(dev);
  274. }
  275. return i;
  276. }
  277. int yaffs2_checkpt_rd(struct yaffs_dev *dev, void *data, int n_bytes)
  278. {
  279. int i = 0;
  280. struct yaffs_ext_tags tags;
  281. int chunk;
  282. int offset_chunk;
  283. u8 *data_bytes = (u8 *) data;
  284. if (!dev->checkpt_buffer)
  285. return 0;
  286. if (dev->checkpt_open_write)
  287. return -1;
  288. while (i < n_bytes) {
  289. if (dev->checkpt_byte_offs < 0 ||
  290. dev->checkpt_byte_offs >= dev->data_bytes_per_chunk) {
  291. if (dev->checkpt_cur_block < 0) {
  292. yaffs2_checkpt_find_block(dev);
  293. dev->checkpt_cur_chunk = 0;
  294. }
  295. /* Bail out if we can't find a checpoint block */
  296. if (dev->checkpt_cur_block < 0)
  297. break;
  298. chunk = dev->checkpt_cur_block *
  299. dev->param.chunks_per_block +
  300. dev->checkpt_cur_chunk;
  301. offset_chunk = apply_chunk_offset(dev, chunk);
  302. dev->n_page_reads++;
  303. /* Read in the next chunk */
  304. dev->tagger.read_chunk_tags_fn(dev,
  305. offset_chunk,
  306. dev->checkpt_buffer,
  307. &tags);
  308. /* Bail out if the chunk is corrupted. */
  309. if (tags.chunk_id != (dev->checkpt_page_seq + 1) ||
  310. tags.ecc_result > YAFFS_ECC_RESULT_FIXED ||
  311. tags.seq_number != YAFFS_SEQUENCE_CHECKPOINT_DATA)
  312. break;
  313. /* Bail out if it is not a checkpoint chunk. */
  314. if(!yaffs2_checkpt_check_chunk_hdr(dev))
  315. break;
  316. dev->checkpt_page_seq++;
  317. dev->checkpt_cur_chunk++;
  318. if (dev->checkpt_cur_chunk >=
  319. dev->param.chunks_per_block)
  320. dev->checkpt_cur_block = -1;
  321. }
  322. *data_bytes = dev->checkpt_buffer[dev->checkpt_byte_offs];
  323. dev->checkpt_sum += *data_bytes;
  324. dev->checkpt_xor ^= *data_bytes;
  325. dev->checkpt_byte_offs++;
  326. i++;
  327. data_bytes++;
  328. dev->checkpt_byte_count++;
  329. }
  330. return i; /* Number of bytes read */
  331. }
  332. int yaffs_checkpt_close(struct yaffs_dev *dev)
  333. {
  334. int i;
  335. if (dev->checkpt_open_write) {
  336. if (dev->checkpt_byte_offs !=
  337. sizeof(sizeof(struct yaffs_checkpt_chunk_hdr)))
  338. yaffs2_checkpt_flush_buffer(dev);
  339. } else if (dev->checkpt_block_list) {
  340. for (i = 0;
  341. i < dev->blocks_in_checkpt &&
  342. dev->checkpt_block_list[i] >= 0; i++) {
  343. int blk = dev->checkpt_block_list[i];
  344. struct yaffs_block_info *bi = NULL;
  345. if (dev->internal_start_block <= blk &&
  346. blk <= dev->internal_end_block)
  347. bi = yaffs_get_block_info(dev, blk);
  348. if (bi && bi->block_state == YAFFS_BLOCK_STATE_EMPTY)
  349. bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
  350. }
  351. }
  352. dev->n_free_chunks -=
  353. dev->blocks_in_checkpt * dev->param.chunks_per_block;
  354. dev->n_erased_blocks -= dev->blocks_in_checkpt;
  355. yaffs_trace(YAFFS_TRACE_CHECKPOINT, "checkpoint byte count %d",
  356. dev->checkpt_byte_count);
  357. if (dev->checkpt_buffer)
  358. return 1;
  359. else
  360. return 0;
  361. }
  362. int yaffs2_checkpt_invalidate_stream(struct yaffs_dev *dev)
  363. {
  364. /* Erase the checkpoint data */
  365. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  366. "checkpoint invalidate of %d blocks",
  367. dev->blocks_in_checkpt);
  368. return yaffs_checkpt_erase(dev);
  369. }