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 all the tags for the chunks in a block into packed tags
15 * (just the tags part - no ECC) in 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 chunks that hold the summary are removed from free space and are marked
25 * THa above might need to be revisited.
28 #include "yaffs_summary.h"
29 #include "yaffs_packedtags2.h"
30 #include "yaffs_nand.h"
31 #include "yaffs_getblockinfo.h"
32 #include "yaffs_bitmap.h"
34 /* Summary tags don't need the sequence number becase that is redundant. */
35 struct yaffs_summary_tags {
41 static void yaffs_summary_clear(struct yaffs_dev *dev)
45 memset(dev->sum_tags, 0, dev->chunks_per_summary *
46 sizeof(struct yaffs_summary_tags));
49 int yaffs_summary_init(struct yaffs_dev *dev)
52 int chunks_used; /* Number of chunks used by summary */
54 sum_bytes = dev->param.chunks_per_block *
55 sizeof(struct yaffs_summary_tags);
57 chunks_used = (sum_bytes + dev->data_bytes_per_chunk - 1)/
58 dev->data_bytes_per_chunk;
59 dev->chunks_per_summary = dev->param.chunks_per_block - chunks_used;
60 dev->sum_tags = kmalloc(sizeof(struct yaffs_summary_tags) *
61 dev->chunks_per_summary, GFP_NOFS);
62 dev->gc_sum_tags = kmalloc(sizeof(struct yaffs_summary_tags) *
63 dev->chunks_per_summary, GFP_NOFS);
64 if(!dev->sum_tags || !dev->gc_sum_tags) {
66 kfree(dev->gc_sum_tags);
70 yaffs_summary_clear(dev);
75 void yaffs_summary_deinit(struct yaffs_dev *dev)
81 dev->chunks_per_summary = 0;
84 static int yaffs_summary_write(struct yaffs_dev *dev, int blk)
86 struct yaffs_ext_tags tags;
88 u8 *sum_buffer = (u8 *)dev->sum_tags;
94 struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
96 buffer = yaffs_get_temp_buffer(dev);
97 n_bytes = sizeof(struct yaffs_summary_tags) * dev->chunks_per_summary;
98 memset(&tags, 0, sizeof(struct yaffs_ext_tags));
99 tags.obj_id = YAFFS_OBJECTID_SUMMARY;
101 chunk_in_block = dev-> chunks_per_summary;
102 chunk_in_nand = dev->alloc_block * dev->param.chunks_per_block +
103 dev-> chunks_per_summary;
106 if (this_tx > dev->data_bytes_per_chunk)
107 this_tx = dev->data_bytes_per_chunk;
108 memcpy(buffer, sum_buffer, this_tx);
109 tags.n_bytes = this_tx;
110 result = yaffs_wr_chunk_tags_nand(dev, chunk_in_nand,
113 if (result != YAFFS_OK)
115 yaffs_set_chunk_bit(dev, blk, chunk_in_block);
117 dev->n_free_chunks--;
120 sum_buffer += this_tx;
126 } while (result == YAFFS_OK && n_bytes > 0);
127 yaffs_release_temp_buffer(dev, buffer);
130 if (result == YAFFS_OK)
137 int yaffs_summary_read(struct yaffs_dev *dev,
138 struct yaffs_summary_tags *st,
141 struct yaffs_ext_tags tags;
143 u8 *sum_buffer = (u8 *)st;
150 struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
152 buffer = yaffs_get_temp_buffer(dev);
153 n_bytes = sizeof(struct yaffs_summary_tags) * dev->chunks_per_summary;
154 chunk_in_block = dev->chunks_per_summary;
155 chunk_in_nand = blk * dev->param.chunks_per_block +
156 dev->chunks_per_summary;
160 if(this_tx > dev->data_bytes_per_chunk)
161 this_tx = dev->data_bytes_per_chunk;
162 result = yaffs_rd_chunk_tags_nand(dev, chunk_in_nand,
165 if (tags.chunk_id != chunk_id ||
166 tags.obj_id != YAFFS_OBJECTID_SUMMARY ||
167 tags.chunk_used == 0 ||
168 tags.ecc_result > YAFFS_ECC_RESULT_FIXED ||
169 this_tx != tags.n_bytes)
171 if (result != YAFFS_OK)
174 if (st == dev->sum_tags) {
175 /* If we're scanning then update the block info */
176 yaffs_set_chunk_bit(dev, blk, chunk_in_block);
180 memcpy(sum_buffer, buffer, this_tx);
182 sum_buffer += this_tx;
186 } while (result == YAFFS_OK && n_bytes > 0);
187 yaffs_release_temp_buffer(dev, buffer);
189 if (st == dev->sum_tags && result == YAFFS_OK)
194 int yaffs_summary_add(struct yaffs_dev *dev,
195 struct yaffs_ext_tags *tags,
198 struct yaffs_packed_tags2_tags_only tags_only;
199 struct yaffs_summary_tags *sum_tags;
200 int block_in_nand = chunk_in_nand / dev->param.chunks_per_block;
201 int chunk_in_block = chunk_in_nand % dev->param.chunks_per_block;
206 if(chunk_in_block >= 0 && chunk_in_block < dev->chunks_per_summary) {
207 yaffs_pack_tags2_tags_only(&tags_only, tags);
208 sum_tags = &dev->sum_tags[chunk_in_block];
209 sum_tags->chunk_id = tags_only.chunk_id;
210 sum_tags->n_bytes = tags_only.n_bytes;
211 sum_tags->obj_id = tags_only.obj_id;
213 if(chunk_in_block == dev->chunks_per_summary - 1) {
214 /* Time to write out the summary */
215 yaffs_summary_write(dev, block_in_nand);
216 yaffs_summary_clear(dev);
217 yaffs_skip_rest_of_block(dev);
223 int yaffs_summary_fetch(struct yaffs_dev *dev,
224 struct yaffs_ext_tags *tags,
227 struct yaffs_packed_tags2_tags_only tags_only;
228 struct yaffs_summary_tags *sum_tags;
229 if(chunk_in_block >= 0 && chunk_in_block < dev->chunks_per_summary) {
230 sum_tags = &dev->sum_tags[chunk_in_block];
231 tags_only.chunk_id = sum_tags->chunk_id;
232 tags_only.n_bytes = sum_tags->n_bytes;
233 tags_only.obj_id = sum_tags->obj_id;
234 yaffs_unpack_tags2_tags_only(tags, &tags_only);
240 void yaffs_summary_gc(struct yaffs_dev *dev, int blk)
242 struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
245 if (!bi->has_summary)
248 for (i = dev->chunks_per_summary; i < dev->param.chunks_per_block; i++)
249 yaffs_clear_chunk_bit(dev, blk, i);