yaffs_summary.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. buffer = yaffs_get_temp_buffer(dev);
  167. n_bytes = sizeof(struct yaffs_summary_tags) * dev->chunks_per_summary;
  168. chunk_in_block = dev->chunks_per_summary;
  169. chunk_in_nand = blk * dev->param.chunks_per_block +
  170. dev->chunks_per_summary;
  171. chunk_id = 1;
  172. do {
  173. this_tx = n_bytes;
  174. if (this_tx > sum_bytes_per_chunk)
  175. this_tx = sum_bytes_per_chunk;
  176. result = yaffs_rd_chunk_tags_nand(dev, chunk_in_nand,
  177. buffer, &tags);
  178. if (tags.chunk_id != chunk_id ||
  179. tags.obj_id != YAFFS_OBJECTID_SUMMARY ||
  180. tags.chunk_used == 0 ||
  181. tags.ecc_result > YAFFS_ECC_RESULT_FIXED ||
  182. tags.n_bytes != (this_tx + sizeof(hdr)))
  183. result = YAFFS_FAIL;
  184. if (result != YAFFS_OK)
  185. break;
  186. if (st == dev->sum_tags) {
  187. /* If we're scanning then update the block info */
  188. yaffs_set_chunk_bit(dev, blk, chunk_in_block);
  189. bi->pages_in_use++;
  190. }
  191. memcpy(&hdr, buffer, sizeof(hdr));
  192. memcpy(sum_buffer, buffer + sizeof(hdr), this_tx);
  193. n_bytes -= this_tx;
  194. sum_buffer += this_tx;
  195. chunk_in_nand++;
  196. chunk_in_block++;
  197. chunk_id++;
  198. } while (result == YAFFS_OK && n_bytes > 0);
  199. yaffs_release_temp_buffer(dev, buffer);
  200. if (result == YAFFS_OK) {
  201. /* Verify header */
  202. if (hdr.version != YAFFS_SUMMARY_VERSION ||
  203. hdr.seq != bi->seq_number ||
  204. hdr.sum != yaffs_summary_sum(dev))
  205. result = YAFFS_FAIL;
  206. }
  207. if (st == dev->sum_tags && result == YAFFS_OK)
  208. bi->has_summary = 1;
  209. return result;
  210. }
  211. int yaffs_summary_add(struct yaffs_dev *dev,
  212. struct yaffs_ext_tags *tags,
  213. int chunk_in_nand)
  214. {
  215. struct yaffs_packed_tags2_tags_only tags_only;
  216. struct yaffs_summary_tags *sum_tags;
  217. int block_in_nand = chunk_in_nand / dev->param.chunks_per_block;
  218. int chunk_in_block = chunk_in_nand % dev->param.chunks_per_block;
  219. if (!dev->sum_tags)
  220. return YAFFS_OK;
  221. if (chunk_in_block >= 0 && chunk_in_block < dev->chunks_per_summary) {
  222. yaffs_pack_tags2_tags_only(&tags_only, tags);
  223. sum_tags = &dev->sum_tags[chunk_in_block];
  224. sum_tags->chunk_id = tags_only.chunk_id;
  225. sum_tags->n_bytes = tags_only.n_bytes;
  226. sum_tags->obj_id = tags_only.obj_id;
  227. if (chunk_in_block == dev->chunks_per_summary - 1) {
  228. /* Time to write out the summary */
  229. yaffs_summary_write(dev, block_in_nand);
  230. yaffs_summary_clear(dev);
  231. yaffs_skip_rest_of_block(dev);
  232. }
  233. }
  234. return YAFFS_OK;
  235. }
  236. int yaffs_summary_fetch(struct yaffs_dev *dev,
  237. struct yaffs_ext_tags *tags,
  238. int chunk_in_block)
  239. {
  240. struct yaffs_packed_tags2_tags_only tags_only;
  241. struct yaffs_summary_tags *sum_tags;
  242. if (chunk_in_block >= 0 && chunk_in_block < dev->chunks_per_summary) {
  243. sum_tags = &dev->sum_tags[chunk_in_block];
  244. tags_only.chunk_id = sum_tags->chunk_id;
  245. tags_only.n_bytes = sum_tags->n_bytes;
  246. tags_only.obj_id = sum_tags->obj_id;
  247. yaffs_unpack_tags2_tags_only(tags, &tags_only);
  248. return YAFFS_OK;
  249. }
  250. return YAFFS_FAIL;
  251. }
  252. void yaffs_summary_gc(struct yaffs_dev *dev, int blk)
  253. {
  254. struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
  255. int i;
  256. if (!bi->has_summary)
  257. return;
  258. for (i = dev->chunks_per_summary;
  259. i < dev->param.chunks_per_block;
  260. i++) {
  261. if (yaffs_check_chunk_bit(dev, blk, i)) {
  262. yaffs_clear_chunk_bit(dev, blk, i);
  263. bi->pages_in_use--;
  264. dev->n_free_chunks++;
  265. }
  266. }
  267. }