yaffs Added some more tests to direct/timothy_tests/quick_tests
[yaffs2.git] / yaffs_checkptrw.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2010 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 #include "yaffs_checkptrw.h"
15 #include "yaffs_getblockinfo.h"
16
17 static int yaffs2_checkpt_space_ok(struct yaffs_dev *dev)
18 {
19         int blocks_avail = dev->n_erased_blocks - dev->param.n_reserved_blocks;
20
21         T(YAFFS_TRACE_CHECKPOINT,
22           (TSTR("checkpt blocks available = %d" TENDSTR), blocks_avail));
23
24         return (blocks_avail <= 0) ? 0 : 1;
25 }
26
27 static int yaffs_checkpt_erase(struct yaffs_dev *dev)
28 {
29         int i;
30
31         if (!dev->param.erase_fn)
32                 return 0;
33         T(YAFFS_TRACE_CHECKPOINT, (TSTR("checking blocks %d to %d" TENDSTR),
34                                    dev->internal_start_block,
35                                    dev->internal_end_block));
36
37         for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
38                 struct yaffs_block_info *bi = yaffs_get_block_info(dev, i);
39                 if (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT) {
40                         T(YAFFS_TRACE_CHECKPOINT,
41                           (TSTR("erasing checkpt block %d" TENDSTR), i));
42
43                         dev->n_erasures++;
44
45                         if (dev->param.
46                             erase_fn(dev,
47                                      i - dev->block_offset /* realign */ )) {
48                                 bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
49                                 dev->n_erased_blocks++;
50                                 dev->n_free_chunks +=
51                                     dev->param.chunks_per_block;
52                         } else {
53                                 dev->param.bad_block_fn(dev, i);
54                                 bi->block_state = YAFFS_BLOCK_STATE_DEAD;
55                         }
56                 }
57         }
58
59         dev->blocks_in_checkpt = 0;
60
61         return 1;
62 }
63
64 static void yaffs2_checkpt_find_erased_block(struct yaffs_dev *dev)
65 {
66         int i;
67         int blocks_avail = dev->n_erased_blocks - dev->param.n_reserved_blocks;
68         T(YAFFS_TRACE_CHECKPOINT,
69           (TSTR
70            ("allocating checkpt block: erased %d reserved %d avail %d next %d "
71             TENDSTR), dev->n_erased_blocks, dev->param.n_reserved_blocks,
72            blocks_avail, dev->checkpt_next_block));
73
74         if (dev->checkpt_next_block >= 0 &&
75             dev->checkpt_next_block <= dev->internal_end_block &&
76             blocks_avail > 0) {
77
78                 for (i = dev->checkpt_next_block; i <= dev->internal_end_block;
79                      i++) {
80                         struct yaffs_block_info *bi =
81                             yaffs_get_block_info(dev, i);
82                         if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
83                                 dev->checkpt_next_block = i + 1;
84                                 dev->checkpt_cur_block = i;
85                                 T(YAFFS_TRACE_CHECKPOINT,
86                                   (TSTR("allocating checkpt block %d" TENDSTR),
87                                    i));
88                                 return;
89                         }
90                 }
91         }
92         T(YAFFS_TRACE_CHECKPOINT, (TSTR("out of checkpt blocks" TENDSTR)));
93
94         dev->checkpt_next_block = -1;
95         dev->checkpt_cur_block = -1;
96 }
97
98 static void yaffs2_checkpt_find_block(struct yaffs_dev *dev)
99 {
100         int i;
101         struct yaffs_ext_tags tags;
102
103         T(YAFFS_TRACE_CHECKPOINT,
104           (TSTR("find next checkpt block: start:  blocks %d next %d" TENDSTR),
105            dev->blocks_in_checkpt, dev->checkpt_next_block));
106
107         if (dev->blocks_in_checkpt < dev->checkpt_max_blocks)
108                 for (i = dev->checkpt_next_block; i <= dev->internal_end_block;
109                      i++) {
110                         int chunk = i * dev->param.chunks_per_block;
111                         int realigned_chunk = chunk - dev->chunk_offset;
112
113                         dev->param.read_chunk_tags_fn(dev, realigned_chunk,
114                                                       NULL, &tags);
115                         T(YAFFS_TRACE_CHECKPOINT,
116                           (TSTR
117                            ("find next checkpt block: search: block %d oid %d seq %d eccr %d"
118                             TENDSTR), i, tags.obj_id, tags.seq_number,
119                            tags.ecc_result));
120
121                         if (tags.seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA) {
122                                 /* Right kind of block */
123                                 dev->checkpt_next_block = tags.obj_id;
124                                 dev->checkpt_cur_block = i;
125                                 dev->checkpt_block_list[dev->
126                                                         blocks_in_checkpt] = i;
127                                 dev->blocks_in_checkpt++;
128                                 T(YAFFS_TRACE_CHECKPOINT,
129                                   (TSTR("found checkpt block %d" TENDSTR), i));
130                                 return;
131                         }
132                 }
133
134         T(YAFFS_TRACE_CHECKPOINT,
135           (TSTR("found no more checkpt blocks" TENDSTR)));
136
137         dev->checkpt_next_block = -1;
138         dev->checkpt_cur_block = -1;
139 }
140
141 int yaffs2_checkpt_open(struct yaffs_dev *dev, int writing)
142 {
143
144         dev->checkpt_open_write = writing;
145
146         /* Got the functions we need? */
147         if (!dev->param.write_chunk_tags_fn ||
148             !dev->param.read_chunk_tags_fn ||
149             !dev->param.erase_fn || !dev->param.bad_block_fn)
150                 return 0;
151
152         if (writing && !yaffs2_checkpt_space_ok(dev))
153                 return 0;
154
155         if (!dev->checkpt_buffer)
156                 dev->checkpt_buffer =
157                     YMALLOC_DMA(dev->param.total_bytes_per_chunk);
158         if (!dev->checkpt_buffer)
159                 return 0;
160
161         dev->checkpt_page_seq = 0;
162         dev->checkpt_byte_count = 0;
163         dev->checkpt_sum = 0;
164         dev->checkpt_xor = 0;
165         dev->checkpt_cur_block = -1;
166         dev->checkpt_cur_chunk = -1;
167         dev->checkpt_next_block = dev->internal_start_block;
168
169         /* Erase all the blocks in the checkpoint area */
170         if (writing) {
171                 memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
172                 dev->checkpt_byte_offs = 0;
173                 return yaffs_checkpt_erase(dev);
174         } else {
175                 int i;
176                 /* Set to a value that will kick off a read */
177                 dev->checkpt_byte_offs = dev->data_bytes_per_chunk;
178                 /* A checkpoint block list of 1 checkpoint block per 16 block is (hopefully)
179                  * going to be way more than we need */
180                 dev->blocks_in_checkpt = 0;
181                 dev->checkpt_max_blocks =
182                     (dev->internal_end_block - dev->internal_start_block) / 16 +
183                     2;
184                 dev->checkpt_block_list =
185                     YMALLOC(sizeof(int) * dev->checkpt_max_blocks);
186                 if (!dev->checkpt_block_list)
187                         return 0;
188
189                 for (i = 0; i < dev->checkpt_max_blocks; i++)
190                         dev->checkpt_block_list[i] = -1;
191         }
192
193         return 1;
194 }
195
196 int yaffs2_get_checkpt_sum(struct yaffs_dev *dev, u32 * sum)
197 {
198         u32 composite_sum;
199         composite_sum = (dev->checkpt_sum << 8) | (dev->checkpt_xor & 0xFF);
200         *sum = composite_sum;
201         return 1;
202 }
203
204 static int yaffs2_checkpt_flush_buffer(struct yaffs_dev *dev)
205 {
206         int chunk;
207         int realigned_chunk;
208
209         struct yaffs_ext_tags tags;
210
211         if (dev->checkpt_cur_block < 0) {
212                 yaffs2_checkpt_find_erased_block(dev);
213                 dev->checkpt_cur_chunk = 0;
214         }
215
216         if (dev->checkpt_cur_block < 0)
217                 return 0;
218
219         tags.is_deleted = 0;
220         tags.obj_id = dev->checkpt_next_block;  /* Hint to next place to look */
221         tags.chunk_id = dev->checkpt_page_seq + 1;
222         tags.seq_number = YAFFS_SEQUENCE_CHECKPOINT_DATA;
223         tags.n_bytes = dev->data_bytes_per_chunk;
224         if (dev->checkpt_cur_chunk == 0) {
225                 /* First chunk we write for the block? Set block state to
226                    checkpoint */
227                 struct yaffs_block_info *bi =
228                     yaffs_get_block_info(dev, dev->checkpt_cur_block);
229                 bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
230                 dev->blocks_in_checkpt++;
231         }
232
233         chunk =
234             dev->checkpt_cur_block * dev->param.chunks_per_block +
235             dev->checkpt_cur_chunk;
236
237         T(YAFFS_TRACE_CHECKPOINT,
238           (TSTR
239            ("checkpoint wite buffer nand %d(%d:%d) objid %d chId %d" TENDSTR),
240            chunk, dev->checkpt_cur_block, dev->checkpt_cur_chunk, tags.obj_id,
241            tags.chunk_id));
242
243         realigned_chunk = chunk - dev->chunk_offset;
244
245         dev->n_page_writes++;
246
247         dev->param.write_chunk_tags_fn(dev, realigned_chunk,
248                                        dev->checkpt_buffer, &tags);
249         dev->checkpt_byte_offs = 0;
250         dev->checkpt_page_seq++;
251         dev->checkpt_cur_chunk++;
252         if (dev->checkpt_cur_chunk >= dev->param.chunks_per_block) {
253                 dev->checkpt_cur_chunk = 0;
254                 dev->checkpt_cur_block = -1;
255         }
256         memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
257
258         return 1;
259 }
260
261 int yaffs2_checkpt_wr(struct yaffs_dev *dev, const void *data, int n_bytes)
262 {
263         int i = 0;
264         int ok = 1;
265
266         u8 *data_bytes = (u8 *) data;
267
268         if (!dev->checkpt_buffer)
269                 return 0;
270
271         if (!dev->checkpt_open_write)
272                 return -1;
273
274         while (i < n_bytes && ok) {
275                 dev->checkpt_buffer[dev->checkpt_byte_offs] = *data_bytes;
276                 dev->checkpt_sum += *data_bytes;
277                 dev->checkpt_xor ^= *data_bytes;
278
279                 dev->checkpt_byte_offs++;
280                 i++;
281                 data_bytes++;
282                 dev->checkpt_byte_count++;
283
284                 if (dev->checkpt_byte_offs < 0 ||
285                     dev->checkpt_byte_offs >= dev->data_bytes_per_chunk)
286                         ok = yaffs2_checkpt_flush_buffer(dev);
287         }
288
289         return i;
290 }
291
292 int yaffs2_checkpt_rd(struct yaffs_dev *dev, void *data, int n_bytes)
293 {
294         int i = 0;
295         int ok = 1;
296         struct yaffs_ext_tags tags;
297
298         int chunk;
299         int realigned_chunk;
300
301         u8 *data_bytes = (u8 *) data;
302
303         if (!dev->checkpt_buffer)
304                 return 0;
305
306         if (dev->checkpt_open_write)
307                 return -1;
308
309         while (i < n_bytes && ok) {
310
311                 if (dev->checkpt_byte_offs < 0 ||
312                     dev->checkpt_byte_offs >= dev->data_bytes_per_chunk) {
313
314                         if (dev->checkpt_cur_block < 0) {
315                                 yaffs2_checkpt_find_block(dev);
316                                 dev->checkpt_cur_chunk = 0;
317                         }
318
319                         if (dev->checkpt_cur_block < 0)
320                                 ok = 0;
321                         else {
322                                 chunk = dev->checkpt_cur_block *
323                                     dev->param.chunks_per_block +
324                                     dev->checkpt_cur_chunk;
325
326                                 realigned_chunk = chunk - dev->chunk_offset;
327
328                                 dev->n_page_reads++;
329
330                                 /* read in the next chunk */
331                                 dev->param.read_chunk_tags_fn(dev,
332                                                               realigned_chunk,
333                                                               dev->
334                                                               checkpt_buffer,
335                                                               &tags);
336
337                                 if (tags.chunk_id != (dev->checkpt_page_seq + 1)
338                                     || tags.ecc_result > YAFFS_ECC_RESULT_FIXED
339                                     || tags.seq_number !=
340                                     YAFFS_SEQUENCE_CHECKPOINT_DATA)
341                                         ok = 0;
342
343                                 dev->checkpt_byte_offs = 0;
344                                 dev->checkpt_page_seq++;
345                                 dev->checkpt_cur_chunk++;
346
347                                 if (dev->checkpt_cur_chunk >=
348                                     dev->param.chunks_per_block)
349                                         dev->checkpt_cur_block = -1;
350                         }
351                 }
352
353                 if (ok) {
354                         *data_bytes =
355                             dev->checkpt_buffer[dev->checkpt_byte_offs];
356                         dev->checkpt_sum += *data_bytes;
357                         dev->checkpt_xor ^= *data_bytes;
358                         dev->checkpt_byte_offs++;
359                         i++;
360                         data_bytes++;
361                         dev->checkpt_byte_count++;
362                 }
363         }
364
365         return i;
366 }
367
368 int yaffs_checkpt_close(struct yaffs_dev *dev)
369 {
370
371         if (dev->checkpt_open_write) {
372                 if (dev->checkpt_byte_offs != 0)
373                         yaffs2_checkpt_flush_buffer(dev);
374         } else if (dev->checkpt_block_list) {
375                 int i;
376                 for (i = 0;
377                      i < dev->blocks_in_checkpt
378                      && dev->checkpt_block_list[i] >= 0; i++) {
379                         int blk = dev->checkpt_block_list[i];
380                         struct yaffs_block_info *bi = NULL;
381                         if (dev->internal_start_block <= blk
382                             && blk <= dev->internal_end_block)
383                                 bi = yaffs_get_block_info(dev, blk);
384                         if (bi && bi->block_state == YAFFS_BLOCK_STATE_EMPTY)
385                                 bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
386                         else {
387                                 /* Todo this looks odd... */
388                         }
389                 }
390                 YFREE(dev->checkpt_block_list);
391                 dev->checkpt_block_list = NULL;
392         }
393
394         dev->n_free_chunks -=
395             dev->blocks_in_checkpt * dev->param.chunks_per_block;
396         dev->n_erased_blocks -= dev->blocks_in_checkpt;
397
398         T(YAFFS_TRACE_CHECKPOINT, (TSTR("checkpoint byte count %d" TENDSTR),
399                                    dev->checkpt_byte_count));
400
401         if (dev->checkpt_buffer) {
402                 /* free the buffer */
403                 YFREE(dev->checkpt_buffer);
404                 dev->checkpt_buffer = NULL;
405                 return 1;
406         } else {
407                 return 0;
408         }
409 }
410
411 int yaffs2_checkpt_invalidate_stream(struct yaffs_dev *dev)
412 {
413         /* Erase the checkpoint data */
414
415         T(YAFFS_TRACE_CHECKPOINT,
416           (TSTR("checkpoint invalidate of %d blocks" TENDSTR),
417            dev->blocks_in_checkpt));
418
419         return yaffs_checkpt_erase(dev);
420 }