xref: /openbmc/u-boot/fs/yaffs2/yaffs_summary.c (revision 17998eff9021b7b579c0387e934d8c52603fe247)
1*753ac610SCharles Manning /*
2*753ac610SCharles Manning  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3*753ac610SCharles Manning  *
4*753ac610SCharles Manning  * Copyright (C) 2002-2011 Aleph One Ltd.
5*753ac610SCharles Manning  *   for Toby Churchill Ltd and Brightstar Engineering
6*753ac610SCharles Manning  *
7*753ac610SCharles Manning  * Created by Charles Manning <charles@aleph1.co.uk>
8*753ac610SCharles Manning  *
9*753ac610SCharles Manning  * This program is free software; you can redistribute it and/or modify
10*753ac610SCharles Manning  * it under the terms of the GNU General Public License version 2 as
11*753ac610SCharles Manning  * published by the Free Software Foundation.
12*753ac610SCharles Manning  */
13*753ac610SCharles Manning 
14*753ac610SCharles Manning /* Summaries write the useful part of the tags for the chunks in a block into an
15*753ac610SCharles Manning  * an array which is written to the last n chunks of the block.
16*753ac610SCharles Manning  * Reading the summaries gives all the tags for the block in one read. Much
17*753ac610SCharles Manning  * faster.
18*753ac610SCharles Manning  *
19*753ac610SCharles Manning  * Chunks holding summaries are marked with tags making it look like
20*753ac610SCharles Manning  * they are part of a fake file.
21*753ac610SCharles Manning  *
22*753ac610SCharles Manning  * The summary could also be used during gc.
23*753ac610SCharles Manning  *
24*753ac610SCharles Manning  */
25*753ac610SCharles Manning 
26*753ac610SCharles Manning #include "yaffs_summary.h"
27*753ac610SCharles Manning #include "yaffs_packedtags2.h"
28*753ac610SCharles Manning #include "yaffs_nand.h"
29*753ac610SCharles Manning #include "yaffs_getblockinfo.h"
30*753ac610SCharles Manning #include "yaffs_bitmap.h"
31*753ac610SCharles Manning 
32*753ac610SCharles Manning /*
33*753ac610SCharles Manning  * The summary is built up in an array of summary tags.
34*753ac610SCharles Manning  * This gets written to the last one or two (maybe more) chunks in a block.
35*753ac610SCharles Manning  * A summary header is written as the first part of each chunk of summary data.
36*753ac610SCharles Manning  * The summary header must match or the summary is rejected.
37*753ac610SCharles Manning  */
38*753ac610SCharles Manning 
39*753ac610SCharles Manning /* Summary tags don't need the sequence number because that is redundant. */
40*753ac610SCharles Manning struct yaffs_summary_tags {
41*753ac610SCharles Manning 	unsigned obj_id;
42*753ac610SCharles Manning 	unsigned chunk_id;
43*753ac610SCharles Manning 	unsigned n_bytes;
44*753ac610SCharles Manning };
45*753ac610SCharles Manning 
46*753ac610SCharles Manning /* Summary header */
47*753ac610SCharles Manning struct yaffs_summary_header {
48*753ac610SCharles Manning 	unsigned version;	/* Must match current version */
49*753ac610SCharles Manning 	unsigned block;		/* Must be this block */
50*753ac610SCharles Manning 	unsigned seq;		/* Must be this sequence number */
51*753ac610SCharles Manning 	unsigned sum;		/* Just add up all the bytes in the tags */
52*753ac610SCharles Manning };
53*753ac610SCharles Manning 
54*753ac610SCharles Manning 
yaffs_summary_clear(struct yaffs_dev * dev)55*753ac610SCharles Manning static void yaffs_summary_clear(struct yaffs_dev *dev)
56*753ac610SCharles Manning {
57*753ac610SCharles Manning 	if (!dev->sum_tags)
58*753ac610SCharles Manning 		return;
59*753ac610SCharles Manning 	memset(dev->sum_tags, 0, dev->chunks_per_summary *
60*753ac610SCharles Manning 		sizeof(struct yaffs_summary_tags));
61*753ac610SCharles Manning }
62*753ac610SCharles Manning 
63*753ac610SCharles Manning 
yaffs_summary_deinit(struct yaffs_dev * dev)64*753ac610SCharles Manning void yaffs_summary_deinit(struct yaffs_dev *dev)
65*753ac610SCharles Manning {
66*753ac610SCharles Manning 	kfree(dev->sum_tags);
67*753ac610SCharles Manning 	dev->sum_tags = NULL;
68*753ac610SCharles Manning 	kfree(dev->gc_sum_tags);
69*753ac610SCharles Manning 	dev->gc_sum_tags = NULL;
70*753ac610SCharles Manning 	dev->chunks_per_summary = 0;
71*753ac610SCharles Manning }
72*753ac610SCharles Manning 
yaffs_summary_init(struct yaffs_dev * dev)73*753ac610SCharles Manning int yaffs_summary_init(struct yaffs_dev *dev)
74*753ac610SCharles Manning {
75*753ac610SCharles Manning 	int sum_bytes;
76*753ac610SCharles Manning 	int chunks_used; /* Number of chunks used by summary */
77*753ac610SCharles Manning 	int sum_tags_bytes;
78*753ac610SCharles Manning 
79*753ac610SCharles Manning 	sum_bytes = dev->param.chunks_per_block *
80*753ac610SCharles Manning 			sizeof(struct yaffs_summary_tags);
81*753ac610SCharles Manning 
82*753ac610SCharles Manning 	chunks_used = (sum_bytes + dev->data_bytes_per_chunk - 1)/
83*753ac610SCharles Manning 			(dev->data_bytes_per_chunk -
84*753ac610SCharles Manning 				sizeof(struct yaffs_summary_header));
85*753ac610SCharles Manning 
86*753ac610SCharles Manning 	dev->chunks_per_summary = dev->param.chunks_per_block - chunks_used;
87*753ac610SCharles Manning 	sum_tags_bytes = sizeof(struct yaffs_summary_tags) *
88*753ac610SCharles Manning 				dev->chunks_per_summary;
89*753ac610SCharles Manning 	dev->sum_tags = kmalloc(sum_tags_bytes, GFP_NOFS);
90*753ac610SCharles Manning 	dev->gc_sum_tags = kmalloc(sum_tags_bytes, GFP_NOFS);
91*753ac610SCharles Manning 	if (!dev->sum_tags || !dev->gc_sum_tags) {
92*753ac610SCharles Manning 		yaffs_summary_deinit(dev);
93*753ac610SCharles Manning 		return YAFFS_FAIL;
94*753ac610SCharles Manning 	}
95*753ac610SCharles Manning 
96*753ac610SCharles Manning 	yaffs_summary_clear(dev);
97*753ac610SCharles Manning 
98*753ac610SCharles Manning 	return YAFFS_OK;
99*753ac610SCharles Manning }
100*753ac610SCharles Manning 
yaffs_summary_sum(struct yaffs_dev * dev)101*753ac610SCharles Manning static unsigned yaffs_summary_sum(struct yaffs_dev *dev)
102*753ac610SCharles Manning {
103*753ac610SCharles Manning 	u8 *sum_buffer = (u8 *)dev->sum_tags;
104*753ac610SCharles Manning 	int i;
105*753ac610SCharles Manning 	unsigned sum = 0;
106*753ac610SCharles Manning 
107*753ac610SCharles Manning 	i = sizeof(struct yaffs_summary_tags) *
108*753ac610SCharles Manning 				dev->chunks_per_summary;
109*753ac610SCharles Manning 	while (i > 0) {
110*753ac610SCharles Manning 		sum += *sum_buffer;
111*753ac610SCharles Manning 		sum_buffer++;
112*753ac610SCharles Manning 		i--;
113*753ac610SCharles Manning 	}
114*753ac610SCharles Manning 
115*753ac610SCharles Manning 	return sum;
116*753ac610SCharles Manning }
117*753ac610SCharles Manning 
yaffs_summary_write(struct yaffs_dev * dev,int blk)118*753ac610SCharles Manning static int yaffs_summary_write(struct yaffs_dev *dev, int blk)
119*753ac610SCharles Manning {
120*753ac610SCharles Manning 	struct yaffs_ext_tags tags;
121*753ac610SCharles Manning 	u8 *buffer;
122*753ac610SCharles Manning 	u8 *sum_buffer = (u8 *)dev->sum_tags;
123*753ac610SCharles Manning 	int n_bytes;
124*753ac610SCharles Manning 	int chunk_in_nand;
125*753ac610SCharles Manning 	int chunk_in_block;
126*753ac610SCharles Manning 	int result;
127*753ac610SCharles Manning 	int this_tx;
128*753ac610SCharles Manning 	struct yaffs_summary_header hdr;
129*753ac610SCharles Manning 	int sum_bytes_per_chunk = dev->data_bytes_per_chunk - sizeof(hdr);
130*753ac610SCharles Manning 	struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
131*753ac610SCharles Manning 
132*753ac610SCharles Manning 	buffer = yaffs_get_temp_buffer(dev);
133*753ac610SCharles Manning 	n_bytes = sizeof(struct yaffs_summary_tags) *
134*753ac610SCharles Manning 				dev->chunks_per_summary;
135*753ac610SCharles Manning 	memset(&tags, 0, sizeof(struct yaffs_ext_tags));
136*753ac610SCharles Manning 	tags.obj_id = YAFFS_OBJECTID_SUMMARY;
137*753ac610SCharles Manning 	tags.chunk_id = 1;
138*753ac610SCharles Manning 	chunk_in_block = dev->chunks_per_summary;
139*753ac610SCharles Manning 	chunk_in_nand = dev->alloc_block * dev->param.chunks_per_block +
140*753ac610SCharles Manning 						dev->chunks_per_summary;
141*753ac610SCharles Manning 	hdr.version = YAFFS_SUMMARY_VERSION;
142*753ac610SCharles Manning 	hdr.block = blk;
143*753ac610SCharles Manning 	hdr.seq = bi->seq_number;
144*753ac610SCharles Manning 	hdr.sum = yaffs_summary_sum(dev);
145*753ac610SCharles Manning 
146*753ac610SCharles Manning 	do {
147*753ac610SCharles Manning 		this_tx = n_bytes;
148*753ac610SCharles Manning 		if (this_tx > sum_bytes_per_chunk)
149*753ac610SCharles Manning 			this_tx = sum_bytes_per_chunk;
150*753ac610SCharles Manning 		memcpy(buffer, &hdr, sizeof(hdr));
151*753ac610SCharles Manning 		memcpy(buffer + sizeof(hdr), sum_buffer, this_tx);
152*753ac610SCharles Manning 		tags.n_bytes = this_tx + sizeof(hdr);
153*753ac610SCharles Manning 		result = yaffs_wr_chunk_tags_nand(dev, chunk_in_nand,
154*753ac610SCharles Manning 						buffer, &tags);
155*753ac610SCharles Manning 
156*753ac610SCharles Manning 		if (result != YAFFS_OK)
157*753ac610SCharles Manning 			break;
158*753ac610SCharles Manning 		yaffs_set_chunk_bit(dev, blk, chunk_in_block);
159*753ac610SCharles Manning 		bi->pages_in_use++;
160*753ac610SCharles Manning 		dev->n_free_chunks--;
161*753ac610SCharles Manning 
162*753ac610SCharles Manning 		n_bytes -= this_tx;
163*753ac610SCharles Manning 		sum_buffer += this_tx;
164*753ac610SCharles Manning 		chunk_in_nand++;
165*753ac610SCharles Manning 		chunk_in_block++;
166*753ac610SCharles Manning 		tags.chunk_id++;
167*753ac610SCharles Manning 	} while (result == YAFFS_OK && n_bytes > 0);
168*753ac610SCharles Manning 	yaffs_release_temp_buffer(dev, buffer);
169*753ac610SCharles Manning 
170*753ac610SCharles Manning 
171*753ac610SCharles Manning 	if (result == YAFFS_OK)
172*753ac610SCharles Manning 		bi->has_summary = 1;
173*753ac610SCharles Manning 
174*753ac610SCharles Manning 
175*753ac610SCharles Manning 	return result;
176*753ac610SCharles Manning }
177*753ac610SCharles Manning 
yaffs_summary_read(struct yaffs_dev * dev,struct yaffs_summary_tags * st,int blk)178*753ac610SCharles Manning int yaffs_summary_read(struct yaffs_dev *dev,
179*753ac610SCharles Manning 			struct yaffs_summary_tags *st,
180*753ac610SCharles Manning 			int blk)
181*753ac610SCharles Manning {
182*753ac610SCharles Manning 	struct yaffs_ext_tags tags;
183*753ac610SCharles Manning 	u8 *buffer;
184*753ac610SCharles Manning 	u8 *sum_buffer = (u8 *)st;
185*753ac610SCharles Manning 	int n_bytes;
186*753ac610SCharles Manning 	int chunk_id;
187*753ac610SCharles Manning 	int chunk_in_nand;
188*753ac610SCharles Manning 	int chunk_in_block;
189*753ac610SCharles Manning 	int result;
190*753ac610SCharles Manning 	int this_tx;
191*753ac610SCharles Manning 	struct yaffs_summary_header hdr;
192*753ac610SCharles Manning 	struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
193*753ac610SCharles Manning 	int sum_bytes_per_chunk = dev->data_bytes_per_chunk - sizeof(hdr);
194*753ac610SCharles Manning 
195*753ac610SCharles Manning 	buffer = yaffs_get_temp_buffer(dev);
196*753ac610SCharles Manning 	n_bytes = sizeof(struct yaffs_summary_tags) * dev->chunks_per_summary;
197*753ac610SCharles Manning 	chunk_in_block = dev->chunks_per_summary;
198*753ac610SCharles Manning 	chunk_in_nand = blk * dev->param.chunks_per_block +
199*753ac610SCharles Manning 							dev->chunks_per_summary;
200*753ac610SCharles Manning 	chunk_id = 1;
201*753ac610SCharles Manning 	do {
202*753ac610SCharles Manning 		this_tx = n_bytes;
203*753ac610SCharles Manning 		if (this_tx > sum_bytes_per_chunk)
204*753ac610SCharles Manning 			this_tx = sum_bytes_per_chunk;
205*753ac610SCharles Manning 		result = yaffs_rd_chunk_tags_nand(dev, chunk_in_nand,
206*753ac610SCharles Manning 						buffer, &tags);
207*753ac610SCharles Manning 
208*753ac610SCharles Manning 		if (tags.chunk_id != chunk_id ||
209*753ac610SCharles Manning 			tags.obj_id != YAFFS_OBJECTID_SUMMARY ||
210*753ac610SCharles Manning 			tags.chunk_used == 0 ||
211*753ac610SCharles Manning 			tags.ecc_result > YAFFS_ECC_RESULT_FIXED ||
212*753ac610SCharles Manning 			tags.n_bytes != (this_tx + sizeof(hdr)))
213*753ac610SCharles Manning 				result = YAFFS_FAIL;
214*753ac610SCharles Manning 		if (result != YAFFS_OK)
215*753ac610SCharles Manning 			break;
216*753ac610SCharles Manning 
217*753ac610SCharles Manning 		if (st == dev->sum_tags) {
218*753ac610SCharles Manning 			/* If we're scanning then update the block info */
219*753ac610SCharles Manning 			yaffs_set_chunk_bit(dev, blk, chunk_in_block);
220*753ac610SCharles Manning 			bi->pages_in_use++;
221*753ac610SCharles Manning 		}
222*753ac610SCharles Manning 		memcpy(&hdr, buffer, sizeof(hdr));
223*753ac610SCharles Manning 		memcpy(sum_buffer, buffer + sizeof(hdr), this_tx);
224*753ac610SCharles Manning 		n_bytes -= this_tx;
225*753ac610SCharles Manning 		sum_buffer += this_tx;
226*753ac610SCharles Manning 		chunk_in_nand++;
227*753ac610SCharles Manning 		chunk_in_block++;
228*753ac610SCharles Manning 		chunk_id++;
229*753ac610SCharles Manning 	} while (result == YAFFS_OK && n_bytes > 0);
230*753ac610SCharles Manning 	yaffs_release_temp_buffer(dev, buffer);
231*753ac610SCharles Manning 
232*753ac610SCharles Manning 	if (result == YAFFS_OK) {
233*753ac610SCharles Manning 		/* Verify header */
234*753ac610SCharles Manning 		if (hdr.version != YAFFS_SUMMARY_VERSION ||
235*753ac610SCharles Manning 		    hdr.seq != bi->seq_number ||
236*753ac610SCharles Manning 		    hdr.sum != yaffs_summary_sum(dev))
237*753ac610SCharles Manning 			result = YAFFS_FAIL;
238*753ac610SCharles Manning 	}
239*753ac610SCharles Manning 
240*753ac610SCharles Manning 	if (st == dev->sum_tags && result == YAFFS_OK)
241*753ac610SCharles Manning 		bi->has_summary = 1;
242*753ac610SCharles Manning 
243*753ac610SCharles Manning 	return result;
244*753ac610SCharles Manning }
245*753ac610SCharles Manning 
yaffs_summary_add(struct yaffs_dev * dev,struct yaffs_ext_tags * tags,int chunk_in_nand)246*753ac610SCharles Manning int yaffs_summary_add(struct yaffs_dev *dev,
247*753ac610SCharles Manning 			struct yaffs_ext_tags *tags,
248*753ac610SCharles Manning 			int chunk_in_nand)
249*753ac610SCharles Manning {
250*753ac610SCharles Manning 	struct yaffs_packed_tags2_tags_only tags_only;
251*753ac610SCharles Manning 	struct yaffs_summary_tags *sum_tags;
252*753ac610SCharles Manning 	int block_in_nand = chunk_in_nand / dev->param.chunks_per_block;
253*753ac610SCharles Manning 	int chunk_in_block = chunk_in_nand % dev->param.chunks_per_block;
254*753ac610SCharles Manning 
255*753ac610SCharles Manning 	if (!dev->sum_tags)
256*753ac610SCharles Manning 		return YAFFS_OK;
257*753ac610SCharles Manning 
258*753ac610SCharles Manning 	if (chunk_in_block >= 0 && chunk_in_block < dev->chunks_per_summary) {
259*753ac610SCharles Manning 		yaffs_pack_tags2_tags_only(&tags_only, tags);
260*753ac610SCharles Manning 		sum_tags = &dev->sum_tags[chunk_in_block];
261*753ac610SCharles Manning 		sum_tags->chunk_id = tags_only.chunk_id;
262*753ac610SCharles Manning 		sum_tags->n_bytes = tags_only.n_bytes;
263*753ac610SCharles Manning 		sum_tags->obj_id = tags_only.obj_id;
264*753ac610SCharles Manning 
265*753ac610SCharles Manning 		if (chunk_in_block == dev->chunks_per_summary - 1) {
266*753ac610SCharles Manning 			/* Time to write out the summary */
267*753ac610SCharles Manning 			yaffs_summary_write(dev, block_in_nand);
268*753ac610SCharles Manning 			yaffs_summary_clear(dev);
269*753ac610SCharles Manning 			yaffs_skip_rest_of_block(dev);
270*753ac610SCharles Manning 		}
271*753ac610SCharles Manning 	}
272*753ac610SCharles Manning 	return YAFFS_OK;
273*753ac610SCharles Manning }
274*753ac610SCharles Manning 
yaffs_summary_fetch(struct yaffs_dev * dev,struct yaffs_ext_tags * tags,int chunk_in_block)275*753ac610SCharles Manning int yaffs_summary_fetch(struct yaffs_dev *dev,
276*753ac610SCharles Manning 			struct yaffs_ext_tags *tags,
277*753ac610SCharles Manning 			int chunk_in_block)
278*753ac610SCharles Manning {
279*753ac610SCharles Manning 	struct yaffs_packed_tags2_tags_only tags_only;
280*753ac610SCharles Manning 	struct yaffs_summary_tags *sum_tags;
281*753ac610SCharles Manning 	if (chunk_in_block >= 0 && chunk_in_block < dev->chunks_per_summary) {
282*753ac610SCharles Manning 		sum_tags = &dev->sum_tags[chunk_in_block];
283*753ac610SCharles Manning 		tags_only.chunk_id = sum_tags->chunk_id;
284*753ac610SCharles Manning 		tags_only.n_bytes = sum_tags->n_bytes;
285*753ac610SCharles Manning 		tags_only.obj_id = sum_tags->obj_id;
286*753ac610SCharles Manning 		yaffs_unpack_tags2_tags_only(tags, &tags_only);
287*753ac610SCharles Manning 		return YAFFS_OK;
288*753ac610SCharles Manning 	}
289*753ac610SCharles Manning 	return YAFFS_FAIL;
290*753ac610SCharles Manning }
291*753ac610SCharles Manning 
yaffs_summary_gc(struct yaffs_dev * dev,int blk)292*753ac610SCharles Manning void yaffs_summary_gc(struct yaffs_dev *dev, int blk)
293*753ac610SCharles Manning {
294*753ac610SCharles Manning 	struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
295*753ac610SCharles Manning 	int i;
296*753ac610SCharles Manning 
297*753ac610SCharles Manning 	if (!bi->has_summary)
298*753ac610SCharles Manning 		return;
299*753ac610SCharles Manning 
300*753ac610SCharles Manning 	for (i = dev->chunks_per_summary;
301*753ac610SCharles Manning 	     i < dev->param.chunks_per_block;
302*753ac610SCharles Manning 	     i++) {
303*753ac610SCharles Manning 		if (yaffs_check_chunk_bit(dev, blk, i)) {
304*753ac610SCharles Manning 			yaffs_clear_chunk_bit(dev, blk, i);
305*753ac610SCharles Manning 			bi->pages_in_use--;
306*753ac610SCharles Manning 			dev->n_free_chunks++;
307*753ac610SCharles Manning 		}
308*753ac610SCharles Manning 	}
309*753ac610SCharles Manning }
310