yaffs_checkptrw.c 10 KB

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