blob: 2efd73a0223a7917ddb8c70ec2a5a8083d6f1b9f [file] [log] [blame]
Charles Manning3796e1f2012-05-09 16:55:17 +00001/*
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
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
17 * faster.
18 *
19 * Chunks holding summaries are marked with tags making it look like
20 * they are part of a fake file.
21 *
22 * The summary could also be used during gc.
23 *
24 */
25
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"
Simon Glassd66c5f72020-02-03 07:36:15 -070031#include <dm/devres.h>
Charles Manning3796e1f2012-05-09 16:55:17 +000032
33/*
34 * The summary is built up in an array of summary tags.
35 * This gets written to the last one or two (maybe more) chunks in a block.
36 * A summary header is written as the first part of each chunk of summary data.
37 * The summary header must match or the summary is rejected.
38 */
39
40/* Summary tags don't need the sequence number because that is redundant. */
41struct yaffs_summary_tags {
42 unsigned obj_id;
43 unsigned chunk_id;
44 unsigned n_bytes;
45};
46
47/* Summary header */
48struct yaffs_summary_header {
49 unsigned version; /* Must match current version */
50 unsigned block; /* Must be this block */
51 unsigned seq; /* Must be this sequence number */
52 unsigned sum; /* Just add up all the bytes in the tags */
53};
54
Charles Manning3796e1f2012-05-09 16:55:17 +000055static void yaffs_summary_clear(struct yaffs_dev *dev)
56{
57 if (!dev->sum_tags)
58 return;
59 memset(dev->sum_tags, 0, dev->chunks_per_summary *
60 sizeof(struct yaffs_summary_tags));
61}
62
Charles Manning3796e1f2012-05-09 16:55:17 +000063void 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
Charles Manning3796e1f2012-05-09 16:55:17 +0000169 if (result == YAFFS_OK)
170 bi->has_summary = 1;
171
Charles Manning3796e1f2012-05-09 16:55:17 +0000172 return result;
173}
174
175int yaffs_summary_read(struct yaffs_dev *dev,
176 struct yaffs_summary_tags *st,
177 int blk)
178{
179 struct yaffs_ext_tags tags;
180 u8 *buffer;
181 u8 *sum_buffer = (u8 *)st;
182 int n_bytes;
183 int chunk_id;
184 int chunk_in_nand;
185 int chunk_in_block;
186 int result;
187 int this_tx;
188 struct yaffs_summary_header hdr;
189 struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
190 int sum_bytes_per_chunk = dev->data_bytes_per_chunk - sizeof(hdr);
Charles Manning3796e1f2012-05-09 16:55:17 +0000191
Charles Manning3796e1f2012-05-09 16:55:17 +0000192 buffer = yaffs_get_temp_buffer(dev);
193 n_bytes = sizeof(struct yaffs_summary_tags) * dev->chunks_per_summary;
194 chunk_in_block = dev->chunks_per_summary;
195 chunk_in_nand = blk * dev->param.chunks_per_block +
196 dev->chunks_per_summary;
197 chunk_id = 1;
198 do {
199 this_tx = n_bytes;
200 if (this_tx > sum_bytes_per_chunk)
201 this_tx = sum_bytes_per_chunk;
202 result = yaffs_rd_chunk_tags_nand(dev, chunk_in_nand,
203 buffer, &tags);
204
205 if (tags.chunk_id != chunk_id ||
206 tags.obj_id != YAFFS_OBJECTID_SUMMARY ||
207 tags.chunk_used == 0 ||
208 tags.ecc_result > YAFFS_ECC_RESULT_FIXED ||
209 tags.n_bytes != (this_tx + sizeof(hdr)))
210 result = YAFFS_FAIL;
211 if (result != YAFFS_OK)
212 break;
213
214 if (st == dev->sum_tags) {
215 /* If we're scanning then update the block info */
216 yaffs_set_chunk_bit(dev, blk, chunk_in_block);
217 bi->pages_in_use++;
218 }
219 memcpy(&hdr, buffer, sizeof(hdr));
220 memcpy(sum_buffer, buffer + sizeof(hdr), this_tx);
221 n_bytes -= this_tx;
222 sum_buffer += this_tx;
223 chunk_in_nand++;
224 chunk_in_block++;
225 chunk_id++;
226 } while (result == YAFFS_OK && n_bytes > 0);
227 yaffs_release_temp_buffer(dev, buffer);
228
229 if (result == YAFFS_OK) {
230 /* Verify header */
231 if (hdr.version != YAFFS_SUMMARY_VERSION ||
Charles Manning3796e1f2012-05-09 16:55:17 +0000232 hdr.seq != bi->seq_number ||
233 hdr.sum != yaffs_summary_sum(dev))
234 result = YAFFS_FAIL;
235 }
236
237 if (st == dev->sum_tags && result == YAFFS_OK)
238 bi->has_summary = 1;
239
240 return result;
241}
242
243int yaffs_summary_add(struct yaffs_dev *dev,
244 struct yaffs_ext_tags *tags,
245 int chunk_in_nand)
246{
247 struct yaffs_packed_tags2_tags_only tags_only;
248 struct yaffs_summary_tags *sum_tags;
249 int block_in_nand = chunk_in_nand / dev->param.chunks_per_block;
250 int chunk_in_block = chunk_in_nand % dev->param.chunks_per_block;
251
252 if (!dev->sum_tags)
253 return YAFFS_OK;
254
255 if (chunk_in_block >= 0 && chunk_in_block < dev->chunks_per_summary) {
256 yaffs_pack_tags2_tags_only(&tags_only, tags);
257 sum_tags = &dev->sum_tags[chunk_in_block];
258 sum_tags->chunk_id = tags_only.chunk_id;
259 sum_tags->n_bytes = tags_only.n_bytes;
260 sum_tags->obj_id = tags_only.obj_id;
261
262 if (chunk_in_block == dev->chunks_per_summary - 1) {
263 /* Time to write out the summary */
264 yaffs_summary_write(dev, block_in_nand);
265 yaffs_summary_clear(dev);
266 yaffs_skip_rest_of_block(dev);
267 }
268 }
269 return YAFFS_OK;
270}
271
272int yaffs_summary_fetch(struct yaffs_dev *dev,
273 struct yaffs_ext_tags *tags,
274 int chunk_in_block)
275{
276 struct yaffs_packed_tags2_tags_only tags_only;
277 struct yaffs_summary_tags *sum_tags;
278 if (chunk_in_block >= 0 && chunk_in_block < dev->chunks_per_summary) {
279 sum_tags = &dev->sum_tags[chunk_in_block];
280 tags_only.chunk_id = sum_tags->chunk_id;
281 tags_only.n_bytes = sum_tags->n_bytes;
282 tags_only.obj_id = sum_tags->obj_id;
283 yaffs_unpack_tags2_tags_only(tags, &tags_only);
284 return YAFFS_OK;
285 }
286 return YAFFS_FAIL;
287}
288
289void yaffs_summary_gc(struct yaffs_dev *dev, int blk)
290{
291 struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
292 int i;
293
294 if (!bi->has_summary)
295 return;
296
297 for (i = dev->chunks_per_summary;
298 i < dev->param.chunks_per_block;
299 i++) {
300 if (yaffs_check_chunk_bit(dev, blk, i)) {
301 yaffs_clear_chunk_bit(dev, blk, i);
302 bi->pages_in_use--;
303 dev->n_free_chunks++;
304 }
305 }
306}