2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2018 Aleph One Ltd.
6 * Created by Charles Manning <charles@aleph1.co.uk>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
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
18 * Chunks holding summaries are marked with tags making it look like
19 * they are part of a fake file.
21 * The summary could also be used during gc.
25 #include "yaffs_summary.h"
26 #include "yaffs_packedtags2.h"
27 #include "yaffs_nand.h"
28 #include "yaffs_getblockinfo.h"
29 #include "yaffs_bitmap.h"
32 * The summary is built up in an array of summary tags.
33 * This gets written to the last one or two (maybe more) chunks in a block.
34 * A summary header is written as the first part of each chunk of summary data.
35 * The summary header must match or the summary is rejected.
38 /* Summary tags don't need the sequence number because that is redundant. */
39 struct yaffs_summary_tags {
46 struct yaffs_summary_header {
47 unsigned version; /* Must match current version */
48 unsigned block; /* Must be this block */
49 unsigned seq; /* Must be this sequence number */
50 unsigned sum; /* Just add up all the bytes in the tags */
54 static void yaffs_summary_clear(struct yaffs_dev *dev)
58 memset(dev->sum_tags, 0, dev->chunks_per_summary *
59 sizeof(struct yaffs_summary_tags));
63 void yaffs_summary_deinit(struct yaffs_dev *dev)
67 kfree(dev->gc_sum_tags);
68 dev->gc_sum_tags = NULL;
69 dev->chunks_per_summary = 0;
72 int yaffs_summary_init(struct yaffs_dev *dev)
75 int chunks_used; /* Number of chunks used by summary */
78 sum_bytes = dev->param.chunks_per_block *
79 sizeof(struct yaffs_summary_tags);
81 chunks_used = (sum_bytes + dev->data_bytes_per_chunk - 1)/
82 (dev->data_bytes_per_chunk -
83 sizeof(struct yaffs_summary_header));
85 dev->chunks_per_summary = dev->param.chunks_per_block - chunks_used;
86 sum_tags_bytes = sizeof(struct yaffs_summary_tags) *
87 dev->chunks_per_summary;
88 dev->sum_tags = kmalloc(sum_tags_bytes, GFP_NOFS);
89 dev->gc_sum_tags = kmalloc(sum_tags_bytes, GFP_NOFS);
90 if (!dev->sum_tags || !dev->gc_sum_tags) {
91 yaffs_summary_deinit(dev);
95 yaffs_summary_clear(dev);
100 static unsigned yaffs_summary_sum(struct yaffs_dev *dev)
102 u8 *sum_buffer = (u8 *)dev->sum_tags;
106 i = sizeof(struct yaffs_summary_tags) *
107 dev->chunks_per_summary;
117 static int yaffs_summary_write(struct yaffs_dev *dev, int blk)
119 struct yaffs_ext_tags tags;
121 u8 *sum_buffer = (u8 *)dev->sum_tags;
127 struct yaffs_summary_header hdr;
128 int sum_bytes_per_chunk = dev->data_bytes_per_chunk - sizeof(hdr);
129 struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
131 buffer = yaffs_get_temp_buffer(dev);
132 n_bytes = sizeof(struct yaffs_summary_tags) *
133 dev->chunks_per_summary;
134 memset(&tags, 0, sizeof(struct yaffs_ext_tags));
135 tags.obj_id = YAFFS_OBJECTID_SUMMARY;
137 chunk_in_block = dev->chunks_per_summary;
138 chunk_in_nand = dev->alloc_block * dev->param.chunks_per_block +
139 dev->chunks_per_summary;
140 hdr.version = YAFFS_SUMMARY_VERSION;
142 hdr.seq = bi->seq_number;
143 hdr.sum = yaffs_summary_sum(dev);
147 if (this_tx > sum_bytes_per_chunk)
148 this_tx = sum_bytes_per_chunk;
149 memcpy(buffer, &hdr, sizeof(hdr));
150 memcpy(buffer + sizeof(hdr), sum_buffer, this_tx);
151 tags.n_bytes = this_tx + sizeof(hdr);
152 result = yaffs_wr_chunk_tags_nand(dev, chunk_in_nand,
155 if (result != YAFFS_OK)
157 yaffs_set_chunk_bit(dev, blk, chunk_in_block);
159 dev->n_free_chunks--;
162 sum_buffer += this_tx;
166 } while (result == YAFFS_OK && n_bytes > 0);
167 yaffs_release_temp_buffer(dev, buffer);
170 if (result == YAFFS_OK)
177 int yaffs_summary_read(struct yaffs_dev *dev,
178 struct yaffs_summary_tags *st,
181 struct yaffs_ext_tags tags;
183 u8 *sum_buffer = (u8 *)st;
190 struct yaffs_summary_header hdr;
191 struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
192 int sum_bytes_per_chunk = dev->data_bytes_per_chunk - sizeof(hdr);
194 buffer = yaffs_get_temp_buffer(dev);
195 n_bytes = sizeof(struct yaffs_summary_tags) * dev->chunks_per_summary;
196 chunk_in_block = dev->chunks_per_summary;
197 chunk_in_nand = blk * dev->param.chunks_per_block +
198 dev->chunks_per_summary;
202 if (this_tx > sum_bytes_per_chunk)
203 this_tx = sum_bytes_per_chunk;
204 result = yaffs_rd_chunk_tags_nand(dev, chunk_in_nand,
207 if (tags.chunk_id != chunk_id ||
208 tags.obj_id != YAFFS_OBJECTID_SUMMARY ||
209 tags.chunk_used == 0 ||
210 tags.ecc_result > YAFFS_ECC_RESULT_FIXED ||
211 tags.n_bytes != (this_tx + sizeof(hdr)))
213 if (result != YAFFS_OK)
216 if (st == dev->sum_tags) {
217 /* If we're scanning then update the block info */
218 yaffs_set_chunk_bit(dev, blk, chunk_in_block);
221 memcpy(&hdr, buffer, sizeof(hdr));
222 memcpy(sum_buffer, buffer + sizeof(hdr), this_tx);
224 sum_buffer += this_tx;
228 } while (result == YAFFS_OK && n_bytes > 0);
229 yaffs_release_temp_buffer(dev, buffer);
231 if (result == YAFFS_OK) {
233 if (hdr.version != YAFFS_SUMMARY_VERSION ||
234 hdr.seq != bi->seq_number ||
235 hdr.sum != yaffs_summary_sum(dev))
239 if (st == dev->sum_tags && result == YAFFS_OK)
245 int yaffs_summary_add(struct yaffs_dev *dev,
246 struct yaffs_ext_tags *tags,
249 struct yaffs_packed_tags2_tags_only tags_only;
250 struct yaffs_summary_tags *sum_tags;
251 int block_in_nand = chunk_in_nand / dev->param.chunks_per_block;
252 int chunk_in_block = chunk_in_nand % dev->param.chunks_per_block;
257 if (chunk_in_block >= 0 && chunk_in_block < dev->chunks_per_summary) {
258 yaffs_pack_tags2_tags_only(dev, &tags_only, tags);
259 sum_tags = &dev->sum_tags[chunk_in_block];
261 sum_tags->chunk_id = tags_only.chunk_id;
262 sum_tags->n_bytes = tags_only.n_bytes;
263 sum_tags->obj_id = tags_only.obj_id;
265 if (chunk_in_block == dev->chunks_per_summary - 1) {
266 /* Time to write out the summary */
267 yaffs_summary_write(dev, block_in_nand);
268 yaffs_summary_clear(dev);
269 yaffs_skip_rest_of_block(dev);
275 int yaffs_summary_fetch(struct yaffs_dev *dev,
276 struct yaffs_ext_tags *tags,
279 struct yaffs_packed_tags2_tags_only tags_only;
280 struct yaffs_summary_tags *sum_tags;
281 if (chunk_in_block >= 0 && chunk_in_block < dev->chunks_per_summary) {
282 sum_tags = &dev->sum_tags[chunk_in_block];
283 tags_only.chunk_id = sum_tags->chunk_id;
284 tags_only.n_bytes = sum_tags->n_bytes;
285 tags_only.obj_id = sum_tags->obj_id;
286 yaffs_unpack_tags2_tags_only(dev, tags, &tags_only);
292 void yaffs_summary_gc(struct yaffs_dev *dev, int blk)
294 struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
297 if (!bi->has_summary)
300 for (i = dev->chunks_per_summary;
301 i < dev->param.chunks_per_block;
303 if (yaffs_check_chunk_bit(dev, blk, i)) {
304 yaffs_clear_chunk_bit(dev, blk, i);
306 dev->n_free_chunks++;