PageRenderTime 41ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/yaffs2/yaffs_checkptrw.c

https://gitlab.com/LiquidSmooth-Devices/android_kernel_htc_msm8974
C | 411 lines | 315 code | 84 blank | 12 comment | 63 complexity | 56604aae6ccba44d9bf009f4317f9ae0 MD5 | raw file
Possible License(s): GPL-2.0
  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 )) {
  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. dev->checkpt_next_block = tags.obj_id;
  100. dev->checkpt_cur_block = i;
  101. dev->checkpt_block_list[dev->
  102. blocks_in_checkpt] = i;
  103. dev->blocks_in_checkpt++;
  104. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  105. "found checkpt block %d", i);
  106. return;
  107. }
  108. }
  109. yaffs_trace(YAFFS_TRACE_CHECKPOINT, "found no more checkpt blocks");
  110. dev->checkpt_next_block = -1;
  111. dev->checkpt_cur_block = -1;
  112. }
  113. int yaffs2_checkpt_open(struct yaffs_dev *dev, int writing)
  114. {
  115. dev->checkpt_open_write = writing;
  116. if (!dev->param.write_chunk_tags_fn ||
  117. !dev->param.read_chunk_tags_fn ||
  118. !dev->param.erase_fn || !dev->param.bad_block_fn)
  119. return 0;
  120. if (writing && !yaffs2_checkpt_space_ok(dev))
  121. return 0;
  122. if (!dev->checkpt_buffer)
  123. dev->checkpt_buffer =
  124. kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
  125. if (!dev->checkpt_buffer)
  126. return 0;
  127. dev->checkpt_page_seq = 0;
  128. dev->checkpt_byte_count = 0;
  129. dev->checkpt_sum = 0;
  130. dev->checkpt_xor = 0;
  131. dev->checkpt_cur_block = -1;
  132. dev->checkpt_cur_chunk = -1;
  133. dev->checkpt_next_block = dev->internal_start_block;
  134. if (writing) {
  135. memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
  136. dev->checkpt_byte_offs = 0;
  137. return yaffs_checkpt_erase(dev);
  138. } else {
  139. int i;
  140. dev->checkpt_byte_offs = dev->data_bytes_per_chunk;
  141. dev->blocks_in_checkpt = 0;
  142. dev->checkpt_max_blocks =
  143. (dev->internal_end_block - dev->internal_start_block) / 16 +
  144. 2;
  145. dev->checkpt_block_list =
  146. kmalloc(sizeof(int) * dev->checkpt_max_blocks, GFP_NOFS);
  147. if (!dev->checkpt_block_list)
  148. return 0;
  149. for (i = 0; i < dev->checkpt_max_blocks; i++)
  150. dev->checkpt_block_list[i] = -1;
  151. }
  152. return 1;
  153. }
  154. int yaffs2_get_checkpt_sum(struct yaffs_dev *dev, u32 * sum)
  155. {
  156. u32 composite_sum;
  157. composite_sum = (dev->checkpt_sum << 8) | (dev->checkpt_xor & 0xFF);
  158. *sum = composite_sum;
  159. return 1;
  160. }
  161. static int yaffs2_checkpt_flush_buffer(struct yaffs_dev *dev)
  162. {
  163. int chunk;
  164. int realigned_chunk;
  165. struct yaffs_ext_tags tags;
  166. if (dev->checkpt_cur_block < 0) {
  167. yaffs2_checkpt_find_erased_block(dev);
  168. dev->checkpt_cur_chunk = 0;
  169. }
  170. if (dev->checkpt_cur_block < 0)
  171. return 0;
  172. tags.is_deleted = 0;
  173. tags.obj_id = dev->checkpt_next_block;
  174. tags.chunk_id = dev->checkpt_page_seq + 1;
  175. tags.seq_number = YAFFS_SEQUENCE_CHECKPOINT_DATA;
  176. tags.n_bytes = dev->data_bytes_per_chunk;
  177. if (dev->checkpt_cur_chunk == 0) {
  178. struct yaffs_block_info *bi =
  179. yaffs_get_block_info(dev, dev->checkpt_cur_block);
  180. bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
  181. dev->blocks_in_checkpt++;
  182. }
  183. chunk =
  184. dev->checkpt_cur_block * dev->param.chunks_per_block +
  185. dev->checkpt_cur_chunk;
  186. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  187. "checkpoint wite buffer nand %d(%d:%d) objid %d chId %d",
  188. chunk, dev->checkpt_cur_block, dev->checkpt_cur_chunk,
  189. tags.obj_id, tags.chunk_id);
  190. realigned_chunk = chunk - dev->chunk_offset;
  191. dev->n_page_writes++;
  192. dev->param.write_chunk_tags_fn(dev, realigned_chunk,
  193. dev->checkpt_buffer, &tags);
  194. dev->checkpt_byte_offs = 0;
  195. dev->checkpt_page_seq++;
  196. dev->checkpt_cur_chunk++;
  197. if (dev->checkpt_cur_chunk >= dev->param.chunks_per_block) {
  198. dev->checkpt_cur_chunk = 0;
  199. dev->checkpt_cur_block = -1;
  200. }
  201. memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
  202. return 1;
  203. }
  204. int yaffs2_checkpt_wr(struct yaffs_dev *dev, const void *data, int n_bytes)
  205. {
  206. int i = 0;
  207. int ok = 1;
  208. u8 *data_bytes = (u8 *) data;
  209. if (!dev->checkpt_buffer)
  210. return 0;
  211. if (!dev->checkpt_open_write)
  212. return -1;
  213. while (i < n_bytes && ok) {
  214. dev->checkpt_buffer[dev->checkpt_byte_offs] = *data_bytes;
  215. dev->checkpt_sum += *data_bytes;
  216. dev->checkpt_xor ^= *data_bytes;
  217. dev->checkpt_byte_offs++;
  218. i++;
  219. data_bytes++;
  220. dev->checkpt_byte_count++;
  221. if (dev->checkpt_byte_offs < 0 ||
  222. dev->checkpt_byte_offs >= dev->data_bytes_per_chunk)
  223. ok = yaffs2_checkpt_flush_buffer(dev);
  224. }
  225. return i;
  226. }
  227. int yaffs2_checkpt_rd(struct yaffs_dev *dev, void *data, int n_bytes)
  228. {
  229. int i = 0;
  230. int ok = 1;
  231. struct yaffs_ext_tags tags;
  232. int chunk;
  233. int realigned_chunk;
  234. u8 *data_bytes = (u8 *) data;
  235. if (!dev->checkpt_buffer)
  236. return 0;
  237. if (dev->checkpt_open_write)
  238. return -1;
  239. while (i < n_bytes && ok) {
  240. if (dev->checkpt_byte_offs < 0 ||
  241. dev->checkpt_byte_offs >= dev->data_bytes_per_chunk) {
  242. if (dev->checkpt_cur_block < 0) {
  243. yaffs2_checkpt_find_block(dev);
  244. dev->checkpt_cur_chunk = 0;
  245. }
  246. if (dev->checkpt_cur_block < 0)
  247. ok = 0;
  248. else {
  249. chunk = dev->checkpt_cur_block *
  250. dev->param.chunks_per_block +
  251. dev->checkpt_cur_chunk;
  252. realigned_chunk = chunk - dev->chunk_offset;
  253. dev->n_page_reads++;
  254. dev->param.read_chunk_tags_fn(dev,
  255. realigned_chunk,
  256. dev->
  257. checkpt_buffer,
  258. &tags);
  259. if (tags.chunk_id != (dev->checkpt_page_seq + 1)
  260. || tags.ecc_result > YAFFS_ECC_RESULT_FIXED
  261. || tags.seq_number !=
  262. YAFFS_SEQUENCE_CHECKPOINT_DATA)
  263. ok = 0;
  264. dev->checkpt_byte_offs = 0;
  265. dev->checkpt_page_seq++;
  266. dev->checkpt_cur_chunk++;
  267. if (dev->checkpt_cur_chunk >=
  268. dev->param.chunks_per_block)
  269. dev->checkpt_cur_block = -1;
  270. }
  271. }
  272. if (ok) {
  273. *data_bytes =
  274. dev->checkpt_buffer[dev->checkpt_byte_offs];
  275. dev->checkpt_sum += *data_bytes;
  276. dev->checkpt_xor ^= *data_bytes;
  277. dev->checkpt_byte_offs++;
  278. i++;
  279. data_bytes++;
  280. dev->checkpt_byte_count++;
  281. }
  282. }
  283. return i;
  284. }
  285. int yaffs_checkpt_close(struct yaffs_dev *dev)
  286. {
  287. if (dev->checkpt_open_write) {
  288. if (dev->checkpt_byte_offs != 0)
  289. yaffs2_checkpt_flush_buffer(dev);
  290. } else if (dev->checkpt_block_list) {
  291. int i;
  292. for (i = 0;
  293. i < dev->blocks_in_checkpt
  294. && dev->checkpt_block_list[i] >= 0; i++) {
  295. int blk = dev->checkpt_block_list[i];
  296. struct yaffs_block_info *bi = NULL;
  297. if (dev->internal_start_block <= blk
  298. && blk <= dev->internal_end_block)
  299. bi = yaffs_get_block_info(dev, blk);
  300. if (bi && bi->block_state == YAFFS_BLOCK_STATE_EMPTY)
  301. bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
  302. else {
  303. }
  304. }
  305. kfree(dev->checkpt_block_list);
  306. dev->checkpt_block_list = NULL;
  307. }
  308. dev->n_free_chunks -=
  309. dev->blocks_in_checkpt * dev->param.chunks_per_block;
  310. dev->n_erased_blocks -= dev->blocks_in_checkpt;
  311. yaffs_trace(YAFFS_TRACE_CHECKPOINT,"checkpoint byte count %d",
  312. dev->checkpt_byte_count);
  313. if (dev->checkpt_buffer) {
  314. kfree(dev->checkpt_buffer);
  315. dev->checkpt_buffer = NULL;
  316. return 1;
  317. } else {
  318. return 0;
  319. }
  320. }
  321. int yaffs2_checkpt_invalidate_stream(struct yaffs_dev *dev)
  322. {
  323. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  324. "checkpoint invalidate of %d blocks",
  325. dev->blocks_in_checkpt);
  326. return yaffs_checkpt_erase(dev);
  327. }