blob: 50a6919aa97a6116dfcbdb34c0cbec6822f9affe [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2018 Aleph One Ltd.
5 *
6 * Created by Charles Manning <charles@aleph1.co.uk>
7 *
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.
11 */
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
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"
30
31/*
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.
36 */
37
38/* Summary tags don't need the sequence number because that is redundant. */
39struct yaffs_summary_tags {
40 unsigned obj_id;
41 unsigned chunk_id;
42 unsigned n_bytes;
43};
44
45/* Summary header */
46struct 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 */
51};
52
53
54static void yaffs_summary_clear(struct yaffs_dev *dev)
55{
56 if (!dev->sum_tags)
57 return;
58 memset(dev->sum_tags, 0, dev->chunks_per_summary *
59 sizeof(struct yaffs_summary_tags));
60}
61
62
63void yaffs_summary_deinit(struct yaffs_dev *dev)
64{
65 kfree(dev->sum_tags);
66 dev->sum_tags = NULL;
67 kfree(dev->gc_sum_tags);
68 dev->gc_sum_tags = NULL;
69 dev->chunks_per_summary = 0;
70}
71
72int yaffs_summary_init(struct yaffs_dev *dev)
73{
74 int sum_bytes;
75 int chunks_used; /* Number of chunks used by summary */
76 int sum_tags_bytes;
77
78 sum_bytes = dev->param.chunks_per_block *
79 sizeof(struct yaffs_summary_tags);
80
81 chunks_used = (sum_bytes + dev->data_bytes_per_chunk - 1)/
82 (dev->data_bytes_per_chunk -
83 sizeof(struct yaffs_summary_header));
84
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);
92 return YAFFS_FAIL;
93 }
94
95 yaffs_summary_clear(dev);
96
97 return YAFFS_OK;
98}
99
100static unsigned yaffs_summary_sum(struct yaffs_dev *dev)
101{
102 u8 *sum_buffer = (u8 *)dev->sum_tags;
103 int i;
104 unsigned sum = 0;
105
106 i = sizeof(struct yaffs_summary_tags) *
107 dev->chunks_per_summary;
108 while (i > 0) {
109 sum += *sum_buffer;
110 sum_buffer++;
111 i--;
112 }
113
114 return sum;
115}
116
117static int yaffs_summary_write(struct yaffs_dev *dev, int blk)
118{
119 struct yaffs_ext_tags tags;
120 u8 *buffer;
121 u8 *sum_buffer = (u8 *)dev->sum_tags;
122 int n_bytes;
123 int chunk_in_nand;
124 int chunk_in_block;
125 int result;
126 int this_tx;
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);
130
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;
136 tags.chunk_id = 1;
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;
141 hdr.block = blk;
142 hdr.seq = bi->seq_number;
143 hdr.sum = yaffs_summary_sum(dev);
144
145 do {
146 this_tx = n_bytes;
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,
153 buffer, &tags);
154
155 if (result != YAFFS_OK)
156 break;
157 yaffs_set_chunk_bit(dev, blk, chunk_in_block);
158 bi->pages_in_use++;
159 dev->n_free_chunks--;
160
161 n_bytes -= this_tx;
162 sum_buffer += this_tx;
163 chunk_in_nand++;
164 chunk_in_block++;
165 tags.chunk_id++;
166 } while (result == YAFFS_OK && n_bytes > 0);
167 yaffs_release_temp_buffer(dev, buffer);
168
169
170 if (result == YAFFS_OK)
171 bi->has_summary = 1;
172
173
174 return result;
175}
176
177int yaffs_summary_read(struct yaffs_dev *dev,
178 struct yaffs_summary_tags *st,
179 int blk)
180{
181 struct yaffs_ext_tags tags;
182 u8 *buffer;
183 u8 *sum_buffer = (u8 *)st;
184 int n_bytes;
185 u32 chunk_id;
186 int chunk_in_nand;
187 int chunk_in_block;
188 int result;
189 int this_tx;
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);
193
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;
199 chunk_id = 1;
200 do {
201 this_tx = n_bytes;
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,
205 buffer, &tags);
206
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)))
212 result = YAFFS_FAIL;
213 if (result != YAFFS_OK)
214 break;
215
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);
219 bi->pages_in_use++;
220 }
221 memcpy(&hdr, buffer, sizeof(hdr));
222 memcpy(sum_buffer, buffer + sizeof(hdr), this_tx);
223 n_bytes -= this_tx;
224 sum_buffer += this_tx;
225 chunk_in_nand++;
226 chunk_in_block++;
227 chunk_id++;
228 } while (result == YAFFS_OK && n_bytes > 0);
229 yaffs_release_temp_buffer(dev, buffer);
230
231 if (result == YAFFS_OK) {
232 /* Verify header */
233 if (hdr.version != YAFFS_SUMMARY_VERSION ||
234 hdr.seq != bi->seq_number ||
235 hdr.sum != yaffs_summary_sum(dev))
236 result = YAFFS_FAIL;
237 }
238
239 if (st == dev->sum_tags && result == YAFFS_OK)
240 bi->has_summary = 1;
241
242 return result;
243}
244
245int yaffs_summary_add(struct yaffs_dev *dev,
246 struct yaffs_ext_tags *tags,
247 int chunk_in_nand)
248{
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;
253
254 if (!dev->sum_tags)
255 return YAFFS_OK;
256
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];
260
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;
264
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);
270 }
271 }
272 return YAFFS_OK;
273}
274
275int yaffs_summary_fetch(struct yaffs_dev *dev,
276 struct yaffs_ext_tags *tags,
277 int chunk_in_block)
278{
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);
287 return YAFFS_OK;
288 }
289 return YAFFS_FAIL;
290}
291
292void yaffs_summary_gc(struct yaffs_dev *dev, int blk)
293{
294 struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
295 u32 i;
296
297 if (!bi->has_summary)
298 return;
299
300 for (i = dev->chunks_per_summary;
301 i < dev->param.chunks_per_block;
302 i++) {
303 if (yaffs_check_chunk_bit(dev, blk, i)) {
304 yaffs_clear_chunk_bit(dev, blk, i);
305 bi->pages_in_use--;
306 dev->n_free_chunks++;
307 }
308 }
309}