Merge branch 'block-summary'
[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
123                 chunk_in_block++;
124         } while (result == YAFFS_OK && n_bytes > 0);
125         yaffs_release_temp_buffer(dev, buffer);
126
127
128         if (result == YAFFS_OK)
129                 bi->has_summary = 1;
130
131
132         return result;
133 }
134
135 int yaffs_summary_read(struct yaffs_dev *dev,
136                         struct yaffs_summary_tags *st,
137                         int blk)
138 {
139         struct yaffs_ext_tags tags;
140         u8 *buffer;
141         u8 *sum_buffer = (u8 *)st;
142         int n_bytes;
143         int chunk_id;
144         int chunk_in_nand;
145         int chunk_in_block;
146         int result;
147         int this_tx;
148         struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
149
150         buffer = yaffs_get_temp_buffer(dev);
151         n_bytes = sizeof(struct yaffs_summary_tags) * dev->chunks_per_summary;
152         chunk_in_block = dev->chunks_per_summary;
153         chunk_in_nand = blk * dev->param.chunks_per_block +
154                                                         dev->chunks_per_summary;
155         chunk_id = 1;
156         do {
157                 this_tx = n_bytes;
158                 if(this_tx > dev->data_bytes_per_chunk)
159                         this_tx = dev->data_bytes_per_chunk;
160                 result = yaffs_rd_chunk_tags_nand(dev, chunk_in_nand,
161                                                 buffer, &tags);
162
163                 if (tags.chunk_id != chunk_id ||
164                         tags.obj_id != YAFFS_OBJECTID_SUMMARY ||
165                         tags.chunk_used == 0 ||
166                         tags.ecc_result > YAFFS_ECC_RESULT_FIXED ||
167                         this_tx != tags.n_bytes)
168                                 result = YAFFS_FAIL;
169                 if (result != YAFFS_OK)
170                         break;
171
172                 if (st == dev->sum_tags) {
173                         /* If we're scanning then update the block info */
174                         yaffs_set_chunk_bit(dev, blk, chunk_in_block);
175                         bi->pages_in_use++;
176                 }
177
178                 memcpy(sum_buffer, buffer, this_tx);
179                 n_bytes -= this_tx;
180                 sum_buffer += this_tx;
181                 chunk_in_nand++;
182                 chunk_in_block++;
183                 chunk_id++;
184         } while (result == YAFFS_OK && n_bytes > 0);
185         yaffs_release_temp_buffer(dev, buffer);
186
187         if (st == dev->sum_tags && result == YAFFS_OK)
188                 bi->has_summary = 1;
189
190         return result;
191 }
192 int yaffs_summary_add(struct yaffs_dev *dev,
193                         struct yaffs_ext_tags *tags,
194                         int chunk_in_nand)
195 {
196         struct yaffs_packed_tags2_tags_only tags_only;
197         struct yaffs_summary_tags *sum_tags;
198         int block_in_nand = chunk_in_nand / dev->param.chunks_per_block;
199         int chunk_in_block = chunk_in_nand % dev->param.chunks_per_block;
200
201         if(!dev->sum_tags)
202                 return YAFFS_OK;
203
204         if(chunk_in_block >= 0 && chunk_in_block < dev->chunks_per_summary) {
205                 yaffs_pack_tags2_tags_only(&tags_only, tags);
206                 sum_tags = &dev->sum_tags[chunk_in_block];
207                 sum_tags->chunk_id = tags_only.chunk_id;
208                 sum_tags->n_bytes = tags_only.n_bytes;
209                 sum_tags->obj_id = tags_only.obj_id;
210
211                 if(chunk_in_block == dev->chunks_per_summary - 1) {
212                         /* Time to write out the summary */
213                         yaffs_summary_write(dev, block_in_nand);
214                         yaffs_summary_clear(dev);
215                         yaffs_skip_rest_of_block(dev);
216                 }
217         }
218         return YAFFS_OK;
219 }
220
221 int yaffs_summary_fetch(struct yaffs_dev *dev,
222                         struct yaffs_ext_tags *tags,
223                         int chunk_in_block)
224 {
225         struct yaffs_packed_tags2_tags_only tags_only;
226         struct yaffs_summary_tags *sum_tags;
227         if(chunk_in_block >= 0 && chunk_in_block < dev->chunks_per_summary) {
228                 sum_tags = &dev->sum_tags[chunk_in_block];
229                 tags_only.chunk_id = sum_tags->chunk_id;
230                 tags_only.n_bytes = sum_tags->n_bytes;
231                 tags_only.obj_id = sum_tags->obj_id;
232                 yaffs_unpack_tags2_tags_only(tags, &tags_only);
233                 return YAFFS_OK;
234         }
235         return YAFFS_FAIL;
236 }
237
238 void yaffs_summary_gc(struct yaffs_dev *dev, int blk)
239 {
240         struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
241         int i;
242
243         if (!bi->has_summary)
244                 return;
245
246         for (i = dev->chunks_per_summary; i < dev->param.chunks_per_block; i++) {
247                 if( yaffs_check_chunk_bit(dev, blk, i)) {
248                         yaffs_clear_chunk_bit(dev, blk, i);
249                         bi->pages_in_use--;
250                         dev->n_free_chunks++;
251                 }
252         }
253
254 }