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