yaffs_checkptrw.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. dev->checkpt_block_list =
  194. kmalloc(sizeof(int) * dev->checkpt_max_blocks, GFP_NOFS);
  195. if (!dev->checkpt_block_list)
  196. return 0;
  197. for (i = 0; i < dev->checkpt_max_blocks; i++)
  198. dev->checkpt_block_list[i] = -1;
  199. return 1;
  200. }
  201. int yaffs2_get_checkpt_sum(struct yaffs_dev *dev, u32 * sum)
  202. {
  203. u32 composite_sum;
  204. composite_sum = (dev->checkpt_sum << 8) | (dev->checkpt_xor & 0xff);
  205. *sum = composite_sum;
  206. return 1;
  207. }
  208. static int yaffs2_checkpt_flush_buffer(struct yaffs_dev *dev)
  209. {
  210. int chunk;
  211. int offset_chunk;
  212. struct yaffs_ext_tags tags;
  213. if (dev->checkpt_cur_block < 0) {
  214. yaffs2_checkpt_find_erased_block(dev);
  215. dev->checkpt_cur_chunk = 0;
  216. }
  217. if (dev->checkpt_cur_block < 0)
  218. return 0;
  219. tags.is_deleted = 0;
  220. tags.obj_id = dev->checkpt_next_block; /* Hint to next place to look */
  221. tags.chunk_id = dev->checkpt_page_seq + 1;
  222. tags.seq_number = YAFFS_SEQUENCE_CHECKPOINT_DATA;
  223. tags.n_bytes = dev->data_bytes_per_chunk;
  224. if (dev->checkpt_cur_chunk == 0) {
  225. /* First chunk we write for the block? Set block state to
  226. checkpoint */
  227. struct yaffs_block_info *bi =
  228. yaffs_get_block_info(dev, dev->checkpt_cur_block);
  229. bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
  230. dev->blocks_in_checkpt++;
  231. }
  232. chunk =
  233. dev->checkpt_cur_block * dev->param.chunks_per_block +
  234. dev->checkpt_cur_chunk;
  235. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  236. "checkpoint wite buffer nand %d(%d:%d) objid %d chId %d",
  237. chunk, dev->checkpt_cur_block, dev->checkpt_cur_chunk,
  238. tags.obj_id, tags.chunk_id);
  239. offset_chunk = apply_chunk_offset(dev, chunk);
  240. dev->n_page_writes++;
  241. dev->tagger.write_chunk_tags_fn(dev, offset_chunk,
  242. dev->checkpt_buffer, &tags);
  243. dev->checkpt_page_seq++;
  244. dev->checkpt_cur_chunk++;
  245. if (dev->checkpt_cur_chunk >= dev->param.chunks_per_block) {
  246. dev->checkpt_cur_chunk = 0;
  247. dev->checkpt_cur_block = -1;
  248. }
  249. memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
  250. yaffs2_checkpt_init_chunk_hdr(dev);
  251. return 1;
  252. }
  253. int yaffs2_checkpt_wr(struct yaffs_dev *dev, const void *data, int n_bytes)
  254. {
  255. int i = 0;
  256. int ok = 1;
  257. u8 *data_bytes = (u8 *) data;
  258. if (!dev->checkpt_buffer)
  259. return 0;
  260. if (!dev->checkpt_open_write)
  261. return -1;
  262. while (i < n_bytes && ok) {
  263. dev->checkpt_buffer[dev->checkpt_byte_offs] = *data_bytes;
  264. dev->checkpt_sum += *data_bytes;
  265. dev->checkpt_xor ^= *data_bytes;
  266. dev->checkpt_byte_offs++;
  267. i++;
  268. data_bytes++;
  269. dev->checkpt_byte_count++;
  270. if (dev->checkpt_byte_offs < 0 ||
  271. dev->checkpt_byte_offs >= dev->data_bytes_per_chunk)
  272. ok = yaffs2_checkpt_flush_buffer(dev);
  273. }
  274. return i;
  275. }
  276. int yaffs2_checkpt_rd(struct yaffs_dev *dev, void *data, int n_bytes)
  277. {
  278. int i = 0;
  279. int ok = 1;
  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 && ok) {
  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. if (dev->checkpt_cur_block < 0) {
  296. ok = 0;
  297. break;
  298. }
  299. chunk = dev->checkpt_cur_block *
  300. dev->param.chunks_per_block +
  301. dev->checkpt_cur_chunk;
  302. offset_chunk = apply_chunk_offset(dev, chunk);
  303. dev->n_page_reads++;
  304. /* read in the next chunk */
  305. dev->tagger.read_chunk_tags_fn(dev,
  306. offset_chunk,
  307. dev->checkpt_buffer,
  308. &tags);
  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. ok = 0;
  313. break;
  314. }
  315. if(!yaffs2_checkpt_check_chunk_hdr(dev)) {
  316. ok = 0;
  317. break;
  318. }
  319. dev->checkpt_page_seq++;
  320. dev->checkpt_cur_chunk++;
  321. if (dev->checkpt_cur_chunk >=
  322. dev->param.chunks_per_block)
  323. dev->checkpt_cur_block = -1;
  324. }
  325. *data_bytes = dev->checkpt_buffer[dev->checkpt_byte_offs];
  326. dev->checkpt_sum += *data_bytes;
  327. dev->checkpt_xor ^= *data_bytes;
  328. dev->checkpt_byte_offs++;
  329. i++;
  330. data_bytes++;
  331. dev->checkpt_byte_count++;
  332. }
  333. return i;
  334. }
  335. int yaffs_checkpt_close(struct yaffs_dev *dev)
  336. {
  337. int i;
  338. if (dev->checkpt_open_write) {
  339. if (dev->checkpt_byte_offs !=
  340. sizeof(sizeof(struct yaffs_checkpt_chunk_hdr)))
  341. yaffs2_checkpt_flush_buffer(dev);
  342. } else if (dev->checkpt_block_list) {
  343. for (i = 0;
  344. i < dev->blocks_in_checkpt &&
  345. dev->checkpt_block_list[i] >= 0; i++) {
  346. int blk = dev->checkpt_block_list[i];
  347. struct yaffs_block_info *bi = NULL;
  348. if (dev->internal_start_block <= blk &&
  349. blk <= dev->internal_end_block)
  350. bi = yaffs_get_block_info(dev, blk);
  351. if (bi && bi->block_state == YAFFS_BLOCK_STATE_EMPTY)
  352. bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
  353. }
  354. kfree(dev->checkpt_block_list);
  355. dev->checkpt_block_list = NULL;
  356. }
  357. dev->n_free_chunks -=
  358. dev->blocks_in_checkpt * dev->param.chunks_per_block;
  359. dev->n_erased_blocks -= dev->blocks_in_checkpt;
  360. yaffs_trace(YAFFS_TRACE_CHECKPOINT, "checkpoint byte count %d",
  361. dev->checkpt_byte_count);
  362. if (dev->checkpt_buffer) {
  363. /* free the buffer */
  364. kfree(dev->checkpt_buffer);
  365. dev->checkpt_buffer = NULL;
  366. return 1;
  367. } else {
  368. return 0;
  369. }
  370. }
  371. int yaffs2_checkpt_invalidate_stream(struct yaffs_dev *dev)
  372. {
  373. /* Erase the checkpoint data */
  374. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  375. "checkpoint invalidate of %d blocks",
  376. dev->blocks_in_checkpt);
  377. return yaffs_checkpt_erase(dev);
  378. }