yaffs_checkptrw.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. 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. int i;
  117. dev->checkpt_open_write = writing;
  118. /* Got the functions we need? */
  119. if (!dev->param.write_chunk_tags_fn ||
  120. !dev->param.read_chunk_tags_fn ||
  121. !dev->param.erase_fn || !dev->param.bad_block_fn)
  122. return 0;
  123. if (writing && !yaffs2_checkpt_space_ok(dev))
  124. return 0;
  125. if (!dev->checkpt_buffer)
  126. dev->checkpt_buffer =
  127. kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
  128. if (!dev->checkpt_buffer)
  129. return 0;
  130. dev->checkpt_page_seq = 0;
  131. dev->checkpt_byte_count = 0;
  132. dev->checkpt_sum = 0;
  133. dev->checkpt_xor = 0;
  134. dev->checkpt_cur_block = -1;
  135. dev->checkpt_cur_chunk = -1;
  136. dev->checkpt_next_block = dev->internal_start_block;
  137. /* Erase all the blocks in the checkpoint area */
  138. if (writing) {
  139. memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
  140. dev->checkpt_byte_offs = 0;
  141. return yaffs_checkpt_erase(dev);
  142. }
  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
  146. * (hopefully) 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 + 2;
  150. dev->checkpt_block_list =
  151. kmalloc(sizeof(int) * dev->checkpt_max_blocks, GFP_NOFS);
  152. if (!dev->checkpt_block_list)
  153. return 0;
  154. for (i = 0; i < dev->checkpt_max_blocks; i++)
  155. dev->checkpt_block_list[i] = -1;
  156. return 1;
  157. }
  158. int yaffs2_get_checkpt_sum(struct yaffs_dev *dev, u32 * sum)
  159. {
  160. u32 composite_sum;
  161. composite_sum = (dev->checkpt_sum << 8) | (dev->checkpt_xor & 0xff);
  162. *sum = composite_sum;
  163. return 1;
  164. }
  165. static int yaffs2_checkpt_flush_buffer(struct yaffs_dev *dev)
  166. {
  167. int chunk;
  168. int realigned_chunk;
  169. struct yaffs_ext_tags tags;
  170. if (dev->checkpt_cur_block < 0) {
  171. yaffs2_checkpt_find_erased_block(dev);
  172. dev->checkpt_cur_chunk = 0;
  173. }
  174. if (dev->checkpt_cur_block < 0)
  175. return 0;
  176. tags.is_deleted = 0;
  177. tags.obj_id = dev->checkpt_next_block; /* Hint to next place to look */
  178. tags.chunk_id = dev->checkpt_page_seq + 1;
  179. tags.seq_number = YAFFS_SEQUENCE_CHECKPOINT_DATA;
  180. tags.n_bytes = dev->data_bytes_per_chunk;
  181. if (dev->checkpt_cur_chunk == 0) {
  182. /* First chunk we write for the block? Set block state to
  183. checkpoint */
  184. struct yaffs_block_info *bi =
  185. yaffs_get_block_info(dev, dev->checkpt_cur_block);
  186. bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
  187. dev->blocks_in_checkpt++;
  188. }
  189. chunk =
  190. dev->checkpt_cur_block * dev->param.chunks_per_block +
  191. dev->checkpt_cur_chunk;
  192. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  193. "checkpoint wite buffer nand %d(%d:%d) objid %d chId %d",
  194. chunk, dev->checkpt_cur_block, dev->checkpt_cur_chunk,
  195. tags.obj_id, tags.chunk_id);
  196. realigned_chunk = chunk - dev->chunk_offset;
  197. dev->n_page_writes++;
  198. dev->param.write_chunk_tags_fn(dev, realigned_chunk,
  199. dev->checkpt_buffer, &tags);
  200. dev->checkpt_byte_offs = 0;
  201. dev->checkpt_page_seq++;
  202. dev->checkpt_cur_chunk++;
  203. if (dev->checkpt_cur_chunk >= dev->param.chunks_per_block) {
  204. dev->checkpt_cur_chunk = 0;
  205. dev->checkpt_cur_block = -1;
  206. }
  207. memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
  208. return 1;
  209. }
  210. int yaffs2_checkpt_wr(struct yaffs_dev *dev, const void *data, int n_bytes)
  211. {
  212. int i = 0;
  213. int ok = 1;
  214. u8 *data_bytes = (u8 *) data;
  215. if (!dev->checkpt_buffer)
  216. return 0;
  217. if (!dev->checkpt_open_write)
  218. return -1;
  219. while (i < n_bytes && ok) {
  220. dev->checkpt_buffer[dev->checkpt_byte_offs] = *data_bytes;
  221. dev->checkpt_sum += *data_bytes;
  222. dev->checkpt_xor ^= *data_bytes;
  223. dev->checkpt_byte_offs++;
  224. i++;
  225. data_bytes++;
  226. dev->checkpt_byte_count++;
  227. if (dev->checkpt_byte_offs < 0 ||
  228. dev->checkpt_byte_offs >= dev->data_bytes_per_chunk)
  229. ok = yaffs2_checkpt_flush_buffer(dev);
  230. }
  231. return i;
  232. }
  233. int yaffs2_checkpt_rd(struct yaffs_dev *dev, void *data, int n_bytes)
  234. {
  235. int i = 0;
  236. int ok = 1;
  237. struct yaffs_ext_tags tags;
  238. int chunk;
  239. int realigned_chunk;
  240. u8 *data_bytes = (u8 *) data;
  241. if (!dev->checkpt_buffer)
  242. return 0;
  243. if (dev->checkpt_open_write)
  244. return -1;
  245. while (i < n_bytes && ok) {
  246. if (dev->checkpt_byte_offs < 0 ||
  247. dev->checkpt_byte_offs >= dev->data_bytes_per_chunk) {
  248. if (dev->checkpt_cur_block < 0) {
  249. yaffs2_checkpt_find_block(dev);
  250. dev->checkpt_cur_chunk = 0;
  251. }
  252. if (dev->checkpt_cur_block < 0) {
  253. ok = 0;
  254. break;
  255. }
  256. chunk = dev->checkpt_cur_block *
  257. dev->param.chunks_per_block +
  258. dev->checkpt_cur_chunk;
  259. realigned_chunk = chunk - dev->chunk_offset;
  260. dev->n_page_reads++;
  261. /* read in the next chunk */
  262. dev->param.read_chunk_tags_fn(dev,
  263. realigned_chunk,
  264. dev->checkpt_buffer,
  265. &tags);
  266. if (tags.chunk_id != (dev->checkpt_page_seq + 1) ||
  267. tags.ecc_result > YAFFS_ECC_RESULT_FIXED ||
  268. tags.seq_number != YAFFS_SEQUENCE_CHECKPOINT_DATA) {
  269. ok = 0;
  270. break;
  271. }
  272. dev->checkpt_byte_offs = 0;
  273. dev->checkpt_page_seq++;
  274. dev->checkpt_cur_chunk++;
  275. if (dev->checkpt_cur_chunk >=
  276. dev->param.chunks_per_block)
  277. dev->checkpt_cur_block = -1;
  278. }
  279. *data_bytes = dev->checkpt_buffer[dev->checkpt_byte_offs];
  280. dev->checkpt_sum += *data_bytes;
  281. dev->checkpt_xor ^= *data_bytes;
  282. dev->checkpt_byte_offs++;
  283. i++;
  284. data_bytes++;
  285. dev->checkpt_byte_count++;
  286. }
  287. return i;
  288. }
  289. int yaffs_checkpt_close(struct yaffs_dev *dev)
  290. {
  291. int i;
  292. if (dev->checkpt_open_write) {
  293. if (dev->checkpt_byte_offs != 0)
  294. yaffs2_checkpt_flush_buffer(dev);
  295. } else if (dev->checkpt_block_list) {
  296. for (i = 0;
  297. i < dev->blocks_in_checkpt &&
  298. dev->checkpt_block_list[i] >= 0; i++) {
  299. int blk = dev->checkpt_block_list[i];
  300. struct yaffs_block_info *bi = NULL;
  301. if (dev->internal_start_block <= blk &&
  302. blk <= dev->internal_end_block)
  303. bi = yaffs_get_block_info(dev, blk);
  304. if (bi && bi->block_state == YAFFS_BLOCK_STATE_EMPTY)
  305. bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
  306. }
  307. kfree(dev->checkpt_block_list);
  308. dev->checkpt_block_list = NULL;
  309. }
  310. dev->n_free_chunks -=
  311. dev->blocks_in_checkpt * dev->param.chunks_per_block;
  312. dev->n_erased_blocks -= dev->blocks_in_checkpt;
  313. yaffs_trace(YAFFS_TRACE_CHECKPOINT, "checkpoint byte count %d",
  314. dev->checkpt_byte_count);
  315. if (dev->checkpt_buffer) {
  316. /* free the buffer */
  317. kfree(dev->checkpt_buffer);
  318. dev->checkpt_buffer = NULL;
  319. return 1;
  320. } else {
  321. return 0;
  322. }
  323. }
  324. int yaffs2_checkpt_invalidate_stream(struct yaffs_dev *dev)
  325. {
  326. /* Erase the checkpoint data */
  327. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  328. "checkpoint invalidate of %d blocks",
  329. dev->blocks_in_checkpt);
  330. return yaffs_checkpt_erase(dev);
  331. }