918c382a4752c416478a0b9b08ed2617ffa25c05
[yaffs2.git] / yaffs_checkptrw.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 #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         yaffs_trace(YAFFS_TRACE_CHECKPOINT,
22                 "checkpt blocks_avail = %d", 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         yaffs_trace(YAFFS_TRACE_CHECKPOINT,
34                 "checking blocks %d to %d",
35                 dev->internal_start_block, 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                         yaffs_trace(YAFFS_TRACE_CHECKPOINT,
41                         "erasing checkpt block %d", 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
69         yaffs_trace(YAFFS_TRACE_CHECKPOINT,
70                 "allocating checkpt block: erased %d reserved %d avail %d next %d ",
71                 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                                 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
86                                         "allocating checkpt block %d", i);
87                                 return;
88                         }
89                 }
90         }
91         yaffs_trace(YAFFS_TRACE_CHECKPOINT, "out of checkpt blocks");
92
93         dev->checkpt_next_block = -1;
94         dev->checkpt_cur_block = -1;
95 }
96
97 static void yaffs2_checkpt_find_block(struct yaffs_dev *dev)
98 {
99         int i;
100         struct yaffs_ext_tags tags;
101
102         yaffs_trace(YAFFS_TRACE_CHECKPOINT,
103                 "find next checkpt block: start:  blocks %d next %d",
104                 dev->blocks_in_checkpt, dev->checkpt_next_block);
105
106         if (dev->blocks_in_checkpt < dev->checkpt_max_blocks)
107                 for (i = dev->checkpt_next_block; i <= dev->internal_end_block;
108                      i++) {
109                         int chunk = i * dev->param.chunks_per_block;
110                         int realigned_chunk = chunk - dev->chunk_offset;
111                         enum yaffs_block_state state;
112                         u32 seq;
113
114                         dev->param.read_chunk_tags_fn(dev, realigned_chunk,
115                                                       NULL, &tags);
116                         yaffs_trace(YAFFS_TRACE_CHECKPOINT,
117                                 "find next checkpt block: search: block %d state %d oid %d seq %d eccr %d",
118                                 i, (int) state,
119                                 tags.obj_id, tags.seq_number,
120                                 tags.ecc_result);
121
122                         if (tags.seq_number != YAFFS_SEQUENCE_CHECKPOINT_DATA)
123                                 continue;
124
125                         dev->param.query_block_fn(dev, i, &state, &seq);
126                         if (state == YAFFS_BLOCK_STATE_DEAD)
127                                 continue;
128
129                         /* Right kind of block */
130                         dev->checkpt_next_block = tags.obj_id;
131                         dev->checkpt_cur_block = i;
132                         dev->checkpt_block_list[dev->blocks_in_checkpt] = i;
133                         dev->blocks_in_checkpt++;
134                         yaffs_trace(YAFFS_TRACE_CHECKPOINT,
135                                 "found checkpt block %d", i);
136                         return;
137                 }
138
139         yaffs_trace(YAFFS_TRACE_CHECKPOINT, "found no more checkpt blocks");
140
141         dev->checkpt_next_block = -1;
142         dev->checkpt_cur_block = -1;
143 }
144
145 int yaffs2_checkpt_open(struct yaffs_dev *dev, int writing)
146 {
147         int i;
148
149         dev->checkpt_open_write = writing;
150
151         /* Got the functions we need? */
152         if (!dev->param.write_chunk_tags_fn ||
153             !dev->param.read_chunk_tags_fn ||
154             !dev->param.erase_fn || !dev->param.bad_block_fn)
155                 return 0;
156
157         if (writing && !yaffs2_checkpt_space_ok(dev))
158                 return 0;
159
160         if (!dev->checkpt_buffer)
161                 dev->checkpt_buffer =
162                     kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
163         if (!dev->checkpt_buffer)
164                 return 0;
165
166         dev->checkpt_page_seq = 0;
167         dev->checkpt_byte_count = 0;
168         dev->checkpt_sum = 0;
169         dev->checkpt_xor = 0;
170         dev->checkpt_cur_block = -1;
171         dev->checkpt_cur_chunk = -1;
172         dev->checkpt_next_block = dev->internal_start_block;
173
174         /* Erase all the blocks in the checkpoint area */
175         if (writing) {
176                 memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
177                 dev->checkpt_byte_offs = 0;
178                 return yaffs_checkpt_erase(dev);
179         }
180
181         /* Set to a value that will kick off a read */
182         dev->checkpt_byte_offs = dev->data_bytes_per_chunk;
183         /* A checkpoint block list of 1 checkpoint block per 16 block is
184          * (hopefully) going to be way more than we need */
185         dev->blocks_in_checkpt = 0;
186         dev->checkpt_max_blocks =
187             (dev->internal_end_block - dev->internal_start_block) / 16 + 2;
188         dev->checkpt_block_list =
189             kmalloc(sizeof(int) * dev->checkpt_max_blocks, GFP_NOFS);
190
191         if (!dev->checkpt_block_list)
192                 return 0;
193
194         for (i = 0; i < dev->checkpt_max_blocks; i++)
195                 dev->checkpt_block_list[i] = -1;
196
197         return 1;
198 }
199
200 int yaffs2_get_checkpt_sum(struct yaffs_dev *dev, u32 * sum)
201 {
202         u32 composite_sum;
203
204         composite_sum = (dev->checkpt_sum << 8) | (dev->checkpt_xor & 0xff);
205         *sum = composite_sum;
206         return 1;
207 }
208
209 static int yaffs2_checkpt_flush_buffer(struct yaffs_dev *dev)
210 {
211         int chunk;
212         int realigned_chunk;
213         struct yaffs_ext_tags tags;
214
215         if (dev->checkpt_cur_block < 0) {
216                 yaffs2_checkpt_find_erased_block(dev);
217                 dev->checkpt_cur_chunk = 0;
218         }
219
220         if (dev->checkpt_cur_block < 0)
221                 return 0;
222
223         tags.is_deleted = 0;
224         tags.obj_id = dev->checkpt_next_block;  /* Hint to next place to look */
225         tags.chunk_id = dev->checkpt_page_seq + 1;
226         tags.seq_number = YAFFS_SEQUENCE_CHECKPOINT_DATA;
227         tags.n_bytes = dev->data_bytes_per_chunk;
228         if (dev->checkpt_cur_chunk == 0) {
229                 /* First chunk we write for the block? Set block state to
230                    checkpoint */
231                 struct yaffs_block_info *bi =
232                     yaffs_get_block_info(dev, dev->checkpt_cur_block);
233                 bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
234                 dev->blocks_in_checkpt++;
235         }
236
237         chunk =
238             dev->checkpt_cur_block * dev->param.chunks_per_block +
239             dev->checkpt_cur_chunk;
240
241         yaffs_trace(YAFFS_TRACE_CHECKPOINT,
242                 "checkpoint wite buffer nand %d(%d:%d) objid %d chId %d",
243                 chunk, dev->checkpt_cur_block, dev->checkpt_cur_chunk,
244                 tags.obj_id, tags.chunk_id);
245
246         realigned_chunk = chunk - dev->chunk_offset;
247
248         dev->n_page_writes++;
249
250         dev->param.write_chunk_tags_fn(dev, realigned_chunk,
251                                        dev->checkpt_buffer, &tags);
252         dev->checkpt_byte_offs = 0;
253         dev->checkpt_page_seq++;
254         dev->checkpt_cur_chunk++;
255         if (dev->checkpt_cur_chunk >= dev->param.chunks_per_block) {
256                 dev->checkpt_cur_chunk = 0;
257                 dev->checkpt_cur_block = -1;
258         }
259         memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
260
261         return 1;
262 }
263
264 int yaffs2_checkpt_wr(struct yaffs_dev *dev, const void *data, int n_bytes)
265 {
266         int i = 0;
267         int ok = 1;
268         u8 *data_bytes = (u8 *) data;
269
270         if (!dev->checkpt_buffer)
271                 return 0;
272
273         if (!dev->checkpt_open_write)
274                 return -1;
275
276         while (i < n_bytes && ok) {
277                 dev->checkpt_buffer[dev->checkpt_byte_offs] = *data_bytes;
278                 dev->checkpt_sum += *data_bytes;
279                 dev->checkpt_xor ^= *data_bytes;
280
281                 dev->checkpt_byte_offs++;
282                 i++;
283                 data_bytes++;
284                 dev->checkpt_byte_count++;
285
286                 if (dev->checkpt_byte_offs < 0 ||
287                     dev->checkpt_byte_offs >= dev->data_bytes_per_chunk)
288                         ok = yaffs2_checkpt_flush_buffer(dev);
289         }
290
291         return i;
292 }
293
294 int yaffs2_checkpt_rd(struct yaffs_dev *dev, void *data, int n_bytes)
295 {
296         int i = 0;
297         int ok = 1;
298         struct yaffs_ext_tags tags;
299         int chunk;
300         int realigned_chunk;
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                                 break;
322                         }
323
324                         chunk = dev->checkpt_cur_block *
325                             dev->param.chunks_per_block +
326                             dev->checkpt_cur_chunk;
327
328                         realigned_chunk = chunk - dev->chunk_offset;
329                         dev->n_page_reads++;
330
331                         /* read in the next chunk */
332                         dev->param.read_chunk_tags_fn(dev,
333                                                 realigned_chunk,
334                                                 dev->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 != YAFFS_SEQUENCE_CHECKPOINT_DATA) {
340                                 ok = 0;
341                                 break;
342                         }
343
344                         dev->checkpt_byte_offs = 0;
345                         dev->checkpt_page_seq++;
346                         dev->checkpt_cur_chunk++;
347
348                         if (dev->checkpt_cur_chunk >=
349                                         dev->param.chunks_per_block)
350                                 dev->checkpt_cur_block = -1;
351                 }
352
353                 *data_bytes = dev->checkpt_buffer[dev->checkpt_byte_offs];
354                 dev->checkpt_sum += *data_bytes;
355                 dev->checkpt_xor ^= *data_bytes;
356                 dev->checkpt_byte_offs++;
357                 i++;
358                 data_bytes++;
359                 dev->checkpt_byte_count++;
360         }
361
362         return i;
363 }
364
365 int yaffs_checkpt_close(struct yaffs_dev *dev)
366 {
367         int i;
368
369         if (dev->checkpt_open_write) {
370                 if (dev->checkpt_byte_offs != 0)
371                         yaffs2_checkpt_flush_buffer(dev);
372         } else if (dev->checkpt_block_list) {
373                 for (i = 0;
374                      i < dev->blocks_in_checkpt &&
375                      dev->checkpt_block_list[i] >= 0; i++) {
376                         int blk = dev->checkpt_block_list[i];
377                         struct yaffs_block_info *bi = NULL;
378
379                         if (dev->internal_start_block <= blk &&
380                             blk <= dev->internal_end_block)
381                                 bi = yaffs_get_block_info(dev, blk);
382                         if (bi && bi->block_state == YAFFS_BLOCK_STATE_EMPTY)
383                                 bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
384                 }
385                 kfree(dev->checkpt_block_list);
386                 dev->checkpt_block_list = NULL;
387         }
388
389         dev->n_free_chunks -=
390                 dev->blocks_in_checkpt * dev->param.chunks_per_block;
391         dev->n_erased_blocks -= dev->blocks_in_checkpt;
392
393         yaffs_trace(YAFFS_TRACE_CHECKPOINT, "checkpoint byte count %d",
394                 dev->checkpt_byte_count);
395
396         if (dev->checkpt_buffer) {
397                 /* free the buffer */
398                 kfree(dev->checkpt_buffer);
399                 dev->checkpt_buffer = NULL;
400                 return 1;
401         } else {
402                 return 0;
403         }
404 }
405
406 int yaffs2_checkpt_invalidate_stream(struct yaffs_dev *dev)
407 {
408         /* Erase the checkpoint data */
409
410         yaffs_trace(YAFFS_TRACE_CHECKPOINT,
411                 "checkpoint invalidate of %d blocks",
412                 dev->blocks_in_checkpt);
413
414         return yaffs_checkpt_erase(dev);
415 }