yaffs_summary.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. /* Summaries write the useful part of the tags for the chunks in a block into an
  14. * an array which is written to the last n chunks of the block.
  15. * Reading the summaries gives all the tags for the block in one read. Much
  16. * faster.
  17. *
  18. * Chunks holding summaries are marked with tags making it look like
  19. * they are part of a fake file.
  20. *
  21. * The summary could also be used during gc.
  22. *
  23. */
  24. #include "yaffs_summary.h"
  25. #include "yaffs_packedtags2.h"
  26. #include "yaffs_nand.h"
  27. #include "yaffs_getblockinfo.h"
  28. #include "yaffs_bitmap.h"
  29. /*
  30. * The summary is built up in an array of summary tags.
  31. * This gets written to the last one or two (maybe more) chunks in a block.
  32. * A summary header is written as the first part of each chunk of summary data.
  33. * The summary header must match or the summary is rejected.
  34. */
  35. /* Summary tags don't need the sequence number because that is redundant. */
  36. struct yaffs_summary_tags {
  37. unsigned obj_id;
  38. unsigned chunk_id;
  39. unsigned n_bytes;
  40. };
  41. /* Summary header */
  42. struct yaffs_summary_header {
  43. unsigned version; /* Must match current version */
  44. unsigned block; /* Must be this block */
  45. unsigned seq; /* Must be this sequence number */
  46. unsigned sum; /* Just add up all the bytes in the tags */
  47. };
  48. static void yaffs_summary_clear(struct yaffs_dev *dev)
  49. {
  50. if (!dev->sum_tags)
  51. return;
  52. memset(dev->sum_tags, 0, dev->chunks_per_summary *
  53. sizeof(struct yaffs_summary_tags));
  54. }
  55. void yaffs_summary_deinit(struct yaffs_dev *dev)
  56. {
  57. kfree(dev->sum_tags);
  58. dev->sum_tags = NULL;
  59. kfree(dev->gc_sum_tags);
  60. dev->gc_sum_tags = NULL;
  61. dev->chunks_per_summary = 0;
  62. }
  63. int yaffs_summary_init(struct yaffs_dev *dev)
  64. {
  65. int sum_bytes;
  66. int chunks_used; /* Number of chunks used by summary */
  67. int sum_tags_bytes;
  68. sum_bytes = dev->param.chunks_per_block *
  69. sizeof(struct yaffs_summary_tags);
  70. chunks_used = (sum_bytes + dev->data_bytes_per_chunk - 1)/
  71. (dev->data_bytes_per_chunk -
  72. sizeof(struct yaffs_summary_header));
  73. dev->chunks_per_summary = dev->param.chunks_per_block - chunks_used;
  74. sum_tags_bytes = sizeof(struct yaffs_summary_tags) *
  75. dev->chunks_per_summary;
  76. dev->sum_tags = kmalloc(sum_tags_bytes, GFP_NOFS);
  77. dev->gc_sum_tags = kmalloc(sum_tags_bytes, GFP_NOFS);
  78. if (!dev->sum_tags || !dev->gc_sum_tags) {
  79. yaffs_summary_deinit(dev);
  80. return YAFFS_FAIL;
  81. }
  82. yaffs_summary_clear(dev);
  83. return YAFFS_OK;
  84. }
  85. static unsigned yaffs_summary_sum(struct yaffs_dev *dev)
  86. {
  87. u8 *sum_buffer = (u8 *)dev->sum_tags;
  88. int i;
  89. unsigned sum = 0;
  90. i = sizeof(struct yaffs_summary_tags) *
  91. dev->chunks_per_summary;
  92. while (i > 0) {
  93. sum += *sum_buffer;
  94. sum_buffer++;
  95. i--;
  96. }
  97. return sum;
  98. }
  99. static int yaffs_summary_write(struct yaffs_dev *dev, int blk)
  100. {
  101. struct yaffs_ext_tags tags;
  102. u8 *buffer;
  103. u8 *sum_buffer = (u8 *)dev->sum_tags;
  104. int n_bytes;
  105. int chunk_in_nand;
  106. int chunk_in_block;
  107. int result;
  108. int this_tx;
  109. struct yaffs_summary_header hdr;
  110. int sum_bytes_per_chunk = dev->data_bytes_per_chunk - sizeof(hdr);
  111. struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
  112. buffer = yaffs_get_temp_buffer(dev);
  113. n_bytes = sizeof(struct yaffs_summary_tags) *
  114. dev->chunks_per_summary;
  115. memset(&tags, 0, sizeof(struct yaffs_ext_tags));
  116. tags.obj_id = YAFFS_OBJECTID_SUMMARY;
  117. tags.chunk_id = 1;
  118. chunk_in_block = dev->chunks_per_summary;
  119. chunk_in_nand = dev->alloc_block * dev->param.chunks_per_block +
  120. dev->chunks_per_summary;
  121. hdr.version = YAFFS_SUMMARY_VERSION;
  122. hdr.block = blk;
  123. hdr.seq = bi->seq_number;
  124. hdr.sum = yaffs_summary_sum(dev);
  125. do {
  126. this_tx = n_bytes;
  127. if (this_tx > sum_bytes_per_chunk)
  128. this_tx = sum_bytes_per_chunk;
  129. memcpy(buffer, &hdr, sizeof(hdr));
  130. memcpy(buffer + sizeof(hdr), sum_buffer, this_tx);
  131. tags.n_bytes = this_tx + sizeof(hdr);
  132. result = yaffs_wr_chunk_tags_nand(dev, chunk_in_nand,
  133. buffer, &tags);
  134. if (result != YAFFS_OK)
  135. break;
  136. yaffs_set_chunk_bit(dev, blk, chunk_in_block);
  137. bi->pages_in_use++;
  138. dev->n_free_chunks--;
  139. n_bytes -= this_tx;
  140. sum_buffer += this_tx;
  141. chunk_in_nand++;
  142. chunk_in_block++;
  143. tags.chunk_id++;
  144. } while (result == YAFFS_OK && n_bytes > 0);
  145. yaffs_release_temp_buffer(dev, buffer);
  146. if (result == YAFFS_OK)
  147. bi->has_summary = 1;
  148. return result;
  149. }
  150. int yaffs_summary_read(struct yaffs_dev *dev,
  151. struct yaffs_summary_tags *st,
  152. int blk)
  153. {
  154. struct yaffs_ext_tags tags;
  155. u8 *buffer;
  156. u8 *sum_buffer = (u8 *)st;
  157. int n_bytes;
  158. int chunk_id;
  159. int chunk_in_nand;
  160. int chunk_in_block;
  161. int result;
  162. int this_tx;
  163. struct yaffs_summary_header hdr;
  164. struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
  165. int sum_bytes_per_chunk = dev->data_bytes_per_chunk - sizeof(hdr);
  166. int sum_tags_bytes;
  167. sum_tags_bytes = sizeof(struct yaffs_summary_tags) *
  168. dev->chunks_per_summary;
  169. buffer = yaffs_get_temp_buffer(dev);
  170. n_bytes = sizeof(struct yaffs_summary_tags) * dev->chunks_per_summary;
  171. chunk_in_block = dev->chunks_per_summary;
  172. chunk_in_nand = blk * dev->param.chunks_per_block +
  173. dev->chunks_per_summary;
  174. chunk_id = 1;
  175. do {
  176. this_tx = n_bytes;
  177. if (this_tx > sum_bytes_per_chunk)
  178. this_tx = sum_bytes_per_chunk;
  179. result = yaffs_rd_chunk_tags_nand(dev, chunk_in_nand,
  180. buffer, &tags);
  181. if (tags.chunk_id != chunk_id ||
  182. tags.obj_id != YAFFS_OBJECTID_SUMMARY ||
  183. tags.chunk_used == 0 ||
  184. tags.ecc_result > YAFFS_ECC_RESULT_FIXED ||
  185. tags.n_bytes != (this_tx + sizeof(hdr)))
  186. result = YAFFS_FAIL;
  187. if (result != YAFFS_OK)
  188. break;
  189. if (st == dev->sum_tags) {
  190. /* If we're scanning then update the block info */
  191. yaffs_set_chunk_bit(dev, blk, chunk_in_block);
  192. bi->pages_in_use++;
  193. }
  194. memcpy(&hdr, buffer, sizeof(hdr));
  195. memcpy(sum_buffer, buffer + sizeof(hdr), this_tx);
  196. n_bytes -= this_tx;
  197. sum_buffer += this_tx;
  198. chunk_in_nand++;
  199. chunk_in_block++;
  200. chunk_id++;
  201. } while (result == YAFFS_OK && n_bytes > 0);
  202. yaffs_release_temp_buffer(dev, buffer);
  203. if (result == YAFFS_OK) {
  204. /* Verify header */
  205. if (hdr.version != YAFFS_SUMMARY_VERSION ||
  206. hdr.block != blk ||
  207. hdr.seq != bi->seq_number ||
  208. hdr.sum != yaffs_summary_sum(dev))
  209. result = YAFFS_FAIL;
  210. }
  211. if (st == dev->sum_tags && result == YAFFS_OK)
  212. bi->has_summary = 1;
  213. return result;
  214. }
  215. int yaffs_summary_add(struct yaffs_dev *dev,
  216. struct yaffs_ext_tags *tags,
  217. int chunk_in_nand)
  218. {
  219. struct yaffs_packed_tags2_tags_only tags_only;
  220. struct yaffs_summary_tags *sum_tags;
  221. int block_in_nand = chunk_in_nand / dev->param.chunks_per_block;
  222. int chunk_in_block = chunk_in_nand % dev->param.chunks_per_block;
  223. if (!dev->sum_tags)
  224. return YAFFS_OK;
  225. if (chunk_in_block >= 0 && chunk_in_block < dev->chunks_per_summary) {
  226. yaffs_pack_tags2_tags_only(&tags_only, tags);
  227. sum_tags = &dev->sum_tags[chunk_in_block];
  228. sum_tags->chunk_id = tags_only.chunk_id;
  229. sum_tags->n_bytes = tags_only.n_bytes;
  230. sum_tags->obj_id = tags_only.obj_id;
  231. if (chunk_in_block == dev->chunks_per_summary - 1) {
  232. /* Time to write out the summary */
  233. yaffs_summary_write(dev, block_in_nand);
  234. yaffs_summary_clear(dev);
  235. yaffs_skip_rest_of_block(dev);
  236. }
  237. }
  238. return YAFFS_OK;
  239. }
  240. int yaffs_summary_fetch(struct yaffs_dev *dev,
  241. struct yaffs_ext_tags *tags,
  242. int chunk_in_block)
  243. {
  244. struct yaffs_packed_tags2_tags_only tags_only;
  245. struct yaffs_summary_tags *sum_tags;
  246. if (chunk_in_block >= 0 && chunk_in_block < dev->chunks_per_summary) {
  247. sum_tags = &dev->sum_tags[chunk_in_block];
  248. tags_only.chunk_id = sum_tags->chunk_id;
  249. tags_only.n_bytes = sum_tags->n_bytes;
  250. tags_only.obj_id = sum_tags->obj_id;
  251. yaffs_unpack_tags2_tags_only(tags, &tags_only);
  252. return YAFFS_OK;
  253. }
  254. return YAFFS_FAIL;
  255. }
  256. void yaffs_summary_gc(struct yaffs_dev *dev, int blk)
  257. {
  258. struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
  259. int i;
  260. if (!bi->has_summary)
  261. return;
  262. for (i = dev->chunks_per_summary;
  263. i < dev->param.chunks_per_block;
  264. i++) {
  265. if (yaffs_check_chunk_bit(dev, blk, i)) {
  266. yaffs_clear_chunk_bit(dev, blk, i);
  267. bi->pages_in_use--;
  268. dev->n_free_chunks++;
  269. }
  270. }
  271. }