yaffs_summary.c 8.1 KB

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