yaffs_checkptrw.c 10 KB

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