yaffs: Fix incorrect incrementing in summary writing.
[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 /* Summary tags don't need the sequence number because that is redundant. */
33 struct yaffs_summary_tags {
34         unsigned obj_id;
35         unsigned chunk_id;
36         unsigned n_bytes;
37 };
38
39 static void yaffs_summary_clear(struct yaffs_dev *dev)
40 {
41         if(!dev->sum_tags)
42                 return;
43         memset(dev->sum_tags, 0, dev->chunks_per_summary *
44                 sizeof(struct yaffs_summary_tags));
45 }
46
47 int yaffs_summary_init(struct yaffs_dev *dev)
48 {
49         int sum_bytes;
50         int chunks_used; /* Number of chunks used by summary */
51
52         sum_bytes = dev->param.chunks_per_block *
53                         sizeof(struct yaffs_summary_tags);
54
55         chunks_used = (sum_bytes + dev->data_bytes_per_chunk - 1)/
56                         dev->data_bytes_per_chunk;
57         dev->chunks_per_summary = dev->param.chunks_per_block - chunks_used;
58         dev->sum_tags = kmalloc(sizeof(struct yaffs_summary_tags) *
59                                 dev->chunks_per_summary, GFP_NOFS);
60         dev->gc_sum_tags = kmalloc(sizeof(struct yaffs_summary_tags) *
61                                 dev->chunks_per_summary, GFP_NOFS);
62         if(!dev->sum_tags || !dev->gc_sum_tags) {
63                 kfree(dev->sum_tags);
64                 kfree(dev->gc_sum_tags);
65                 return YAFFS_FAIL;
66         }
67
68         yaffs_summary_clear(dev);
69
70         return YAFFS_OK;
71 }
72
73 void yaffs_summary_deinit(struct yaffs_dev *dev)
74 {
75         kfree(dev->sum_tags);
76         dev->sum_tags = NULL;
77         kfree(dev->gc_sum_tags);
78         dev->gc_sum_tags = NULL;
79         dev->chunks_per_summary = 0;
80 }
81
82 static int yaffs_summary_write(struct yaffs_dev *dev, int blk)
83 {
84         struct yaffs_ext_tags tags;
85         u8 *buffer;
86         u8 *sum_buffer = (u8 *)dev->sum_tags;
87         int n_bytes;
88         int chunk_in_nand;
89         int chunk_in_block;
90         int result;
91         int this_tx;
92         struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
93
94         buffer = yaffs_get_temp_buffer(dev);
95         n_bytes = sizeof(struct yaffs_summary_tags) * dev->chunks_per_summary;
96         memset(&tags, 0, sizeof(struct yaffs_ext_tags));
97         tags.obj_id = YAFFS_OBJECTID_SUMMARY;
98         tags.chunk_id = 1;
99         chunk_in_block = dev->chunks_per_summary;
100         chunk_in_nand = dev->alloc_block * dev->param.chunks_per_block +
101                                                 dev-> chunks_per_summary;
102         do {
103                 this_tx = n_bytes;
104                 if (this_tx > dev->data_bytes_per_chunk)
105                         this_tx = dev->data_bytes_per_chunk;
106                 memcpy(buffer, sum_buffer, this_tx);
107                 tags.n_bytes = this_tx;
108                 result = yaffs_wr_chunk_tags_nand(dev, chunk_in_nand,
109                                                 buffer, &tags);
110
111                 if (result != YAFFS_OK)
112                         break;
113                 yaffs_set_chunk_bit(dev, blk, chunk_in_block);
114                 bi->pages_in_use++;
115                 dev->n_free_chunks--;
116
117                 n_bytes -= this_tx;
118                 sum_buffer += this_tx;
119                 chunk_in_nand++;
120                 chunk_in_block++;
121                 tags.chunk_id++;
122         } while (result == YAFFS_OK && n_bytes > 0);
123         yaffs_release_temp_buffer(dev, buffer);
124
125
126         if (result == YAFFS_OK)
127                 bi->has_summary = 1;
128
129
130         return result;
131 }
132
133 int yaffs_summary_read(struct yaffs_dev *dev,
134                         struct yaffs_summary_tags *st,
135                         int blk)
136 {
137         struct yaffs_ext_tags tags;
138         u8 *buffer;
139         u8 *sum_buffer = (u8 *)st;
140         int n_bytes;
141         int chunk_id;
142         int chunk_in_nand;
143         int chunk_in_block;
144         int result;
145         int this_tx;
146         struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
147
148         buffer = yaffs_get_temp_buffer(dev);
149         n_bytes = sizeof(struct yaffs_summary_tags) * dev->chunks_per_summary;
150         chunk_in_block = dev->chunks_per_summary;
151         chunk_in_nand = blk * dev->param.chunks_per_block +
152                                                         dev->chunks_per_summary;
153         chunk_id = 1;
154         do {
155                 this_tx = n_bytes;
156                 if(this_tx > dev->data_bytes_per_chunk)
157                         this_tx = dev->data_bytes_per_chunk;
158                 result = yaffs_rd_chunk_tags_nand(dev, chunk_in_nand,
159                                                 buffer, &tags);
160
161                 if (tags.chunk_id != chunk_id ||
162                         tags.obj_id != YAFFS_OBJECTID_SUMMARY ||
163                         tags.chunk_used == 0 ||
164                         tags.ecc_result > YAFFS_ECC_RESULT_FIXED ||
165                         this_tx != tags.n_bytes)
166                                 result = YAFFS_FAIL;
167                 if (result != YAFFS_OK)
168                         break;
169
170                 if (st == dev->sum_tags) {
171                         /* If we're scanning then update the block info */
172                         yaffs_set_chunk_bit(dev, blk, chunk_in_block);
173                         bi->pages_in_use++;
174                 }
175
176                 memcpy(sum_buffer, buffer, this_tx);
177                 n_bytes -= this_tx;
178                 sum_buffer += this_tx;
179                 chunk_in_nand++;
180                 chunk_in_block++;
181                 chunk_id++;
182         } while (result == YAFFS_OK && n_bytes > 0);
183         yaffs_release_temp_buffer(dev, buffer);
184
185         if (st == dev->sum_tags && result == YAFFS_OK)
186                 bi->has_summary = 1;
187
188         return result;
189 }
190 int yaffs_summary_add(struct yaffs_dev *dev,
191                         struct yaffs_ext_tags *tags,
192                         int chunk_in_nand)
193 {
194         struct yaffs_packed_tags2_tags_only tags_only;
195         struct yaffs_summary_tags *sum_tags;
196         int block_in_nand = chunk_in_nand / dev->param.chunks_per_block;
197         int chunk_in_block = chunk_in_nand % dev->param.chunks_per_block;
198
199         if(!dev->sum_tags)
200                 return YAFFS_OK;
201
202         if(chunk_in_block >= 0 && chunk_in_block < dev->chunks_per_summary) {
203                 yaffs_pack_tags2_tags_only(&tags_only, tags);
204                 sum_tags = &dev->sum_tags[chunk_in_block];
205                 sum_tags->chunk_id = tags_only.chunk_id;
206                 sum_tags->n_bytes = tags_only.n_bytes;
207                 sum_tags->obj_id = tags_only.obj_id;
208
209                 if(chunk_in_block == dev->chunks_per_summary - 1) {
210                         /* Time to write out the summary */
211                         yaffs_summary_write(dev, block_in_nand);
212                         yaffs_summary_clear(dev);
213                         yaffs_skip_rest_of_block(dev);
214                 }
215         }
216         return YAFFS_OK;
217 }
218
219 int yaffs_summary_fetch(struct yaffs_dev *dev,
220                         struct yaffs_ext_tags *tags,
221                         int chunk_in_block)
222 {
223         struct yaffs_packed_tags2_tags_only tags_only;
224         struct yaffs_summary_tags *sum_tags;
225         if(chunk_in_block >= 0 && chunk_in_block < dev->chunks_per_summary) {
226                 sum_tags = &dev->sum_tags[chunk_in_block];
227                 tags_only.chunk_id = sum_tags->chunk_id;
228                 tags_only.n_bytes = sum_tags->n_bytes;
229                 tags_only.obj_id = sum_tags->obj_id;
230                 yaffs_unpack_tags2_tags_only(tags, &tags_only);
231                 return YAFFS_OK;
232         }
233         return YAFFS_FAIL;
234 }
235
236 void yaffs_summary_gc(struct yaffs_dev *dev, int blk)
237 {
238         struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
239         int i;
240
241         if (!bi->has_summary)
242                 return;
243
244         for (i = dev->chunks_per_summary; i < dev->param.chunks_per_block; i++) {
245                 if( yaffs_check_chunk_bit(dev, blk, i)) {
246                         yaffs_clear_chunk_bit(dev, blk, i);
247                         bi->pages_in_use--;
248                         dev->n_free_chunks++;
249                 }
250         }
251
252 }