49eaef1afbca461e7cacd884c2a8fe385fd41e40
[yaffs2.git] / yaffs_summary.c
1 /*
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"
31
32 #define YAFFS_SUMMARY_VERSION 1
33
34 /*
35  * The summary is built up in an array of summary tags.
36  * This gets written to the last one or two (maybe more) chunks in a block.
37  * A summary header is written as the first part of each chunk of summary data.
38  * The summary header must match or the summary is rejected.
39  */
40  
41 /* Summary tags don't need the sequence number because that is redundant. */
42 struct yaffs_summary_tags {
43         unsigned obj_id;
44         unsigned chunk_id;
45         unsigned n_bytes;
46 };
47
48 /* Summary header */
49 struct yaffs_summary_header {
50         unsigned version;       /* Must match current version */
51         unsigned block;         /* Must be this block */
52         unsigned seq;           /* Must be this sequence number */
53         unsigned sum;           /* Just add up all the bytes in the tags */
54 };
55
56
57 static void yaffs_summary_clear(struct yaffs_dev *dev)
58 {
59         if(!dev->sum_tags)
60                 return;
61         memset(dev->sum_tags, 0, dev->chunks_per_summary *
62                 sizeof(struct yaffs_summary_tags));
63 }
64
65
66 void yaffs_summary_deinit(struct yaffs_dev *dev)
67 {
68         kfree(dev->sum_tags);
69         dev->sum_tags = NULL;
70         kfree(dev->gc_sum_tags);
71         dev->gc_sum_tags = NULL;
72         dev->chunks_per_summary = 0;
73 }
74
75 int yaffs_summary_init(struct yaffs_dev *dev)
76 {
77         int sum_bytes;
78         int chunks_used; /* Number of chunks used by summary */
79         int sum_tags_bytes;
80
81         sum_bytes = dev->param.chunks_per_block *
82                         sizeof(struct yaffs_summary_tags);
83
84         chunks_used = (sum_bytes + dev->data_bytes_per_chunk - 1)/
85                         (dev->data_bytes_per_chunk - 
86                                 sizeof(struct yaffs_summary_header));
87
88         dev->chunks_per_summary = dev->param.chunks_per_block - chunks_used;
89         sum_tags_bytes = sizeof(struct yaffs_summary_tags) *
90                                 dev->chunks_per_summary;
91         dev->sum_tags = kmalloc(sum_tags_bytes, GFP_NOFS);
92         dev->gc_sum_tags = kmalloc(sum_tags_bytes, GFP_NOFS);
93         if(!dev->sum_tags || !dev->gc_sum_tags) {
94                 yaffs_summary_deinit(dev);
95                 return YAFFS_FAIL;
96         }
97
98         yaffs_summary_clear(dev);
99
100         return YAFFS_OK;
101 }
102
103 static unsigned yaffs_summary_sum(struct yaffs_dev *dev)
104 {
105         u8 *sum_buffer = (u8 *)dev->sum_tags;
106         int i;
107         unsigned sum = 0;
108
109         i = sizeof(struct yaffs_summary_tags) *
110                                 dev->chunks_per_summary;;
111         while(i > 0){
112                 sum += *sum_buffer;
113                 sum_buffer++;
114                 i--;
115         }
116
117         return sum;     
118 }
119
120 static int yaffs_summary_write(struct yaffs_dev *dev, int blk)
121 {
122         struct yaffs_ext_tags tags;
123         u8 *buffer;
124         u8 *sum_buffer = (u8 *)dev->sum_tags;
125         int n_bytes;
126         int chunk_in_nand;
127         int chunk_in_block;
128         int result;
129         int this_tx;
130         struct yaffs_summary_header hdr;
131         int sum_bytes_per_chunk = dev->data_bytes_per_chunk - sizeof(hdr);
132         struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
133
134         buffer = yaffs_get_temp_buffer(dev);
135         n_bytes = sizeof(struct yaffs_summary_tags) *
136                                 dev->chunks_per_summary;
137         memset(&tags, 0, sizeof(struct yaffs_ext_tags));
138         tags.obj_id = YAFFS_OBJECTID_SUMMARY;
139         tags.chunk_id = 1;
140         chunk_in_block = dev->chunks_per_summary;
141         chunk_in_nand = dev->alloc_block * dev->param.chunks_per_block +
142                                                 dev-> chunks_per_summary;
143         hdr.version = YAFFS_SUMMARY_VERSION;
144         hdr.block = blk;
145         hdr.seq = bi->seq_number;
146         hdr.sum = yaffs_summary_sum(dev);
147         
148         do {
149                 this_tx = n_bytes;
150                 if (this_tx > sum_bytes_per_chunk)
151                         this_tx = sum_bytes_per_chunk;
152                 memcpy(buffer, &hdr, sizeof(hdr));
153                 memcpy(buffer + sizeof(hdr), sum_buffer, this_tx);
154                 tags.n_bytes = this_tx + sizeof(hdr);
155                 result = yaffs_wr_chunk_tags_nand(dev, chunk_in_nand,
156                                                 buffer, &tags);
157
158                 if (result != YAFFS_OK)
159                         break;
160                 yaffs_set_chunk_bit(dev, blk, chunk_in_block);
161                 bi->pages_in_use++;
162                 dev->n_free_chunks--;
163
164                 n_bytes -= this_tx;
165                 sum_buffer += this_tx;
166                 chunk_in_nand++;
167                 chunk_in_block++;
168                 tags.chunk_id++;
169         } while (result == YAFFS_OK && n_bytes > 0);
170         yaffs_release_temp_buffer(dev, buffer);
171
172
173         if (result == YAFFS_OK)
174                 bi->has_summary = 1;
175
176
177         return result;
178 }
179
180 int yaffs_summary_read(struct yaffs_dev *dev,
181                         struct yaffs_summary_tags *st,
182                         int blk)
183 {
184         struct yaffs_ext_tags tags;
185         u8 *buffer;
186         u8 *sum_buffer = (u8 *)st;
187         int n_bytes;
188         int chunk_id;
189         int chunk_in_nand;
190         int chunk_in_block;
191         int result;
192         int this_tx;
193         struct yaffs_summary_header hdr;
194         struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
195         int sum_bytes_per_chunk = dev->data_bytes_per_chunk - sizeof(hdr);
196         int sum_tags_bytes;
197
198         sum_tags_bytes = sizeof(struct yaffs_summary_tags) *
199                                 dev->chunks_per_summary;
200         buffer = yaffs_get_temp_buffer(dev);
201         n_bytes = sizeof(struct yaffs_summary_tags) * dev->chunks_per_summary;
202         chunk_in_block = dev->chunks_per_summary;
203         chunk_in_nand = blk * dev->param.chunks_per_block +
204                                                         dev->chunks_per_summary;
205         chunk_id = 1;
206         do {
207                 this_tx = n_bytes;
208                 if(this_tx > sum_bytes_per_chunk)
209                         this_tx = sum_bytes_per_chunk;
210                 result = yaffs_rd_chunk_tags_nand(dev, chunk_in_nand,
211                                                 buffer, &tags);
212
213                 if (tags.chunk_id != chunk_id ||
214                         tags.obj_id != YAFFS_OBJECTID_SUMMARY ||
215                         tags.chunk_used == 0 ||
216                         tags.ecc_result > YAFFS_ECC_RESULT_FIXED ||
217                         this_tx != tags.n_bytes)
218                                 result = YAFFS_FAIL;
219                 if (result != YAFFS_OK)
220                         break;
221
222                 if (st == dev->sum_tags) {
223                         /* If we're scanning then update the block info */
224                         yaffs_set_chunk_bit(dev, blk, chunk_in_block);
225                         bi->pages_in_use++;
226                 }
227                 memcpy(&hdr, buffer, sizeof(hdr));
228                 memcpy(sum_buffer, buffer + sizeof(hdr), this_tx);
229                 n_bytes -= this_tx;
230                 sum_buffer += this_tx;
231                 chunk_in_nand++;
232                 chunk_in_block++;
233                 chunk_id++;
234         } while (result == YAFFS_OK && n_bytes > 0);
235         yaffs_release_temp_buffer(dev, buffer);
236
237         if(result == YAFFS_OK) {
238                 /* Verify header */             
239                 if (hdr.version != YAFFS_SUMMARY_VERSION ||
240                     hdr.block != blk ||
241                     hdr.seq != bi->seq_number ||
242                     hdr.sum != yaffs_summary_sum(dev))
243                     result = YAFFS_FAIL;
244         }
245         
246         if (st == dev->sum_tags && result == YAFFS_OK)
247                 bi->has_summary = 1;
248
249         return result;
250 }
251
252 int yaffs_summary_add(struct yaffs_dev *dev,
253                         struct yaffs_ext_tags *tags,
254                         int chunk_in_nand)
255 {
256         struct yaffs_packed_tags2_tags_only tags_only;
257         struct yaffs_summary_tags *sum_tags;
258         int block_in_nand = chunk_in_nand / dev->param.chunks_per_block;
259         int chunk_in_block = chunk_in_nand % dev->param.chunks_per_block;
260
261         if(!dev->sum_tags)
262                 return YAFFS_OK;
263
264         if(chunk_in_block >= 0 && chunk_in_block < dev->chunks_per_summary) {
265                 yaffs_pack_tags2_tags_only(&tags_only, tags);
266                 sum_tags = &dev->sum_tags[chunk_in_block];
267                 sum_tags->chunk_id = tags_only.chunk_id;
268                 sum_tags->n_bytes = tags_only.n_bytes;
269                 sum_tags->obj_id = tags_only.obj_id;
270
271                 if(chunk_in_block == dev->chunks_per_summary - 1) {
272                         /* Time to write out the summary */
273                         yaffs_summary_write(dev, block_in_nand);
274                         yaffs_summary_clear(dev);
275                         yaffs_skip_rest_of_block(dev);
276                 }
277         }
278         return YAFFS_OK;
279 }
280
281 int yaffs_summary_fetch(struct yaffs_dev *dev,
282                         struct yaffs_ext_tags *tags,
283                         int chunk_in_block)
284 {
285         struct yaffs_packed_tags2_tags_only tags_only;
286         struct yaffs_summary_tags *sum_tags;
287         if(chunk_in_block >= 0 && chunk_in_block < dev->chunks_per_summary) {
288                 sum_tags = &dev->sum_tags[chunk_in_block];
289                 tags_only.chunk_id = sum_tags->chunk_id;
290                 tags_only.n_bytes = sum_tags->n_bytes;
291                 tags_only.obj_id = sum_tags->obj_id;
292                 yaffs_unpack_tags2_tags_only(tags, &tags_only);
293                 return YAFFS_OK;
294         }
295         return YAFFS_FAIL;
296 }
297
298 void yaffs_summary_gc(struct yaffs_dev *dev, int blk)
299 {
300         struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
301         int i;
302
303         if (!bi->has_summary)
304                 return;
305
306         for (i = dev->chunks_per_summary; i < dev->param.chunks_per_block; i++) {
307                 if( yaffs_check_chunk_bit(dev, blk, i)) {
308                         yaffs_clear_chunk_bit(dev, blk, i);
309                         bi->pages_in_use--;
310                         dev->n_free_chunks++;
311                 }
312         }
313
314 }