2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2011 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
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.
14 /* Summaries write the useful part of the tags for the chunks in a block into an
15 * an array which is written to the last n chunks of the block.
16 * Reading the summaries gives all the tags for the block in one read. Much
19 * Chunks holding summaries are marked with tags making it look like
20 * they are part of a fake file.
22 * The summary could also be used during gc.
26 #include "yaffs_summary.h"
27 #include "yaffs_packedtags2.h"
28 #include "yaffs_nand.h"
29 #include "yaffs_getblockinfo.h"
30 #include "yaffs_bitmap.h"
32 /* Summary tags don't need the sequence number because that is redundant. */
33 struct yaffs_summary_tags {
39 static void yaffs_summary_clear(struct yaffs_dev *dev)
43 memset(dev->sum_tags, 0, dev->chunks_per_summary *
44 sizeof(struct yaffs_summary_tags));
47 int yaffs_summary_init(struct yaffs_dev *dev)
50 int chunks_used; /* Number of chunks used by summary */
52 sum_bytes = dev->param.chunks_per_block *
53 sizeof(struct yaffs_summary_tags);
55 chunks_used = (sum_bytes + dev->data_bytes_per_chunk - 1)/
56 dev->data_bytes_per_chunk;
57 dev->chunks_per_summary = dev->param.chunks_per_block - chunks_used;
58 dev->sum_tags = kmalloc(sizeof(struct yaffs_summary_tags) *
59 dev->chunks_per_summary, GFP_NOFS);
60 dev->gc_sum_tags = kmalloc(sizeof(struct yaffs_summary_tags) *
61 dev->chunks_per_summary, GFP_NOFS);
62 if(!dev->sum_tags || !dev->gc_sum_tags) {
64 kfree(dev->gc_sum_tags);
68 yaffs_summary_clear(dev);
73 void yaffs_summary_deinit(struct yaffs_dev *dev)
77 kfree(dev->gc_sum_tags);
78 dev->gc_sum_tags = NULL;
79 dev->chunks_per_summary = 0;
82 static int yaffs_summary_write(struct yaffs_dev *dev, int blk)
84 struct yaffs_ext_tags tags;
86 u8 *sum_buffer = (u8 *)dev->sum_tags;
92 struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
94 buffer = yaffs_get_temp_buffer(dev);
95 n_bytes = sizeof(struct yaffs_summary_tags) * dev->chunks_per_summary;
96 memset(&tags, 0, sizeof(struct yaffs_ext_tags));
97 tags.obj_id = YAFFS_OBJECTID_SUMMARY;
99 chunk_in_block = dev->chunks_per_summary;
100 chunk_in_nand = dev->alloc_block * dev->param.chunks_per_block +
101 dev-> chunks_per_summary;
104 if (this_tx > dev->data_bytes_per_chunk)
105 this_tx = dev->data_bytes_per_chunk;
106 memcpy(buffer, sum_buffer, this_tx);
107 tags.n_bytes = this_tx;
108 result = yaffs_wr_chunk_tags_nand(dev, chunk_in_nand,
111 if (result != YAFFS_OK)
113 yaffs_set_chunk_bit(dev, blk, chunk_in_block);
115 dev->n_free_chunks--;
118 sum_buffer += this_tx;
122 } while (result == YAFFS_OK && n_bytes > 0);
123 yaffs_release_temp_buffer(dev, buffer);
126 if (result == YAFFS_OK)
133 int yaffs_summary_read(struct yaffs_dev *dev,
134 struct yaffs_summary_tags *st,
137 struct yaffs_ext_tags tags;
139 u8 *sum_buffer = (u8 *)st;
146 struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
148 buffer = yaffs_get_temp_buffer(dev);
149 n_bytes = sizeof(struct yaffs_summary_tags) * dev->chunks_per_summary;
150 chunk_in_block = dev->chunks_per_summary;
151 chunk_in_nand = blk * dev->param.chunks_per_block +
152 dev->chunks_per_summary;
156 if(this_tx > dev->data_bytes_per_chunk)
157 this_tx = dev->data_bytes_per_chunk;
158 result = yaffs_rd_chunk_tags_nand(dev, chunk_in_nand,
161 if (tags.chunk_id != chunk_id ||
162 tags.obj_id != YAFFS_OBJECTID_SUMMARY ||
163 tags.chunk_used == 0 ||
164 tags.ecc_result > YAFFS_ECC_RESULT_FIXED ||
165 this_tx != tags.n_bytes)
167 if (result != YAFFS_OK)
170 if (st == dev->sum_tags) {
171 /* If we're scanning then update the block info */
172 yaffs_set_chunk_bit(dev, blk, chunk_in_block);
176 memcpy(sum_buffer, buffer, this_tx);
178 sum_buffer += this_tx;
182 } while (result == YAFFS_OK && n_bytes > 0);
183 yaffs_release_temp_buffer(dev, buffer);
185 if (st == dev->sum_tags && result == YAFFS_OK)
190 int yaffs_summary_add(struct yaffs_dev *dev,
191 struct yaffs_ext_tags *tags,
194 struct yaffs_packed_tags2_tags_only tags_only;
195 struct yaffs_summary_tags *sum_tags;
196 int block_in_nand = chunk_in_nand / dev->param.chunks_per_block;
197 int chunk_in_block = chunk_in_nand % dev->param.chunks_per_block;
202 if(chunk_in_block >= 0 && chunk_in_block < dev->chunks_per_summary) {
203 yaffs_pack_tags2_tags_only(&tags_only, tags);
204 sum_tags = &dev->sum_tags[chunk_in_block];
205 sum_tags->chunk_id = tags_only.chunk_id;
206 sum_tags->n_bytes = tags_only.n_bytes;
207 sum_tags->obj_id = tags_only.obj_id;
209 if(chunk_in_block == dev->chunks_per_summary - 1) {
210 /* Time to write out the summary */
211 yaffs_summary_write(dev, block_in_nand);
212 yaffs_summary_clear(dev);
213 yaffs_skip_rest_of_block(dev);
219 int yaffs_summary_fetch(struct yaffs_dev *dev,
220 struct yaffs_ext_tags *tags,
223 struct yaffs_packed_tags2_tags_only tags_only;
224 struct yaffs_summary_tags *sum_tags;
225 if(chunk_in_block >= 0 && chunk_in_block < dev->chunks_per_summary) {
226 sum_tags = &dev->sum_tags[chunk_in_block];
227 tags_only.chunk_id = sum_tags->chunk_id;
228 tags_only.n_bytes = sum_tags->n_bytes;
229 tags_only.obj_id = sum_tags->obj_id;
230 yaffs_unpack_tags2_tags_only(tags, &tags_only);
236 void yaffs_summary_gc(struct yaffs_dev *dev, int blk)
238 struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
241 if (!bi->has_summary)
244 for (i = dev->chunks_per_summary; i < dev->param.chunks_per_block; i++) {
245 if( yaffs_check_chunk_bit(dev, blk, i)) {
246 yaffs_clear_chunk_bit(dev, blk, i);
248 dev->n_free_chunks++;