2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
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.
14 #include "yaffs_checkptrw.h"
15 #include "yaffs_getblockinfo.h"
17 static int yaffs2_checkpt_space_ok(struct yaffs_dev *dev)
19 int blocks_avail = dev->n_erased_blocks - dev->param.n_reserved_blocks;
21 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
22 "checkpt blocks_avail = %d", blocks_avail);
24 return (blocks_avail <= 0) ? 0 : 1;
27 static int yaffs_checkpt_erase(struct yaffs_dev *dev)
31 if (!dev->param.erase_fn)
33 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
34 "checking blocks %d to %d",
35 dev->internal_start_block, dev->internal_end_block);
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);
47 i - dev->block_offset /* realign */)) {
48 bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
49 dev->n_erased_blocks++;
51 dev->param.chunks_per_block;
53 dev->param.bad_block_fn(dev, i);
54 bi->block_state = YAFFS_BLOCK_STATE_DEAD;
59 dev->blocks_in_checkpt = 0;
64 static void yaffs2_checkpt_find_erased_block(struct yaffs_dev *dev)
67 int blocks_avail = dev->n_erased_blocks - dev->param.n_reserved_blocks;
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);
74 if (dev->checkpt_next_block >= 0 &&
75 dev->checkpt_next_block <= dev->internal_end_block &&
78 for (i = dev->checkpt_next_block; i <= dev->internal_end_block;
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);
91 yaffs_trace(YAFFS_TRACE_CHECKPOINT, "out of checkpt blocks");
93 dev->checkpt_next_block = -1;
94 dev->checkpt_cur_block = -1;
97 static void yaffs2_checkpt_find_block(struct yaffs_dev *dev)
100 struct yaffs_ext_tags tags;
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);
106 if (dev->blocks_in_checkpt < dev->checkpt_max_blocks)
107 for (i = dev->checkpt_next_block; i <= dev->internal_end_block;
109 int chunk = i * dev->param.chunks_per_block;
110 int realigned_chunk = chunk - dev->chunk_offset;
112 dev->param.read_chunk_tags_fn(dev, realigned_chunk,
114 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
115 "find next checkpt block: search: block %d oid %d seq %d eccr %d",
116 i, tags.obj_id, tags.seq_number,
119 if (tags.seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA) {
120 /* Right kind of block */
121 dev->checkpt_next_block = tags.obj_id;
122 dev->checkpt_cur_block = i;
123 dev->checkpt_block_list[dev->
124 blocks_in_checkpt] = i;
125 dev->blocks_in_checkpt++;
126 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
127 "found checkpt block %d", i);
132 yaffs_trace(YAFFS_TRACE_CHECKPOINT, "found no more checkpt blocks");
134 dev->checkpt_next_block = -1;
135 dev->checkpt_cur_block = -1;
138 int yaffs2_checkpt_open(struct yaffs_dev *dev, int writing)
140 dev->checkpt_open_write = writing;
142 /* Got the functions we need? */
143 if (!dev->param.write_chunk_tags_fn ||
144 !dev->param.read_chunk_tags_fn ||
145 !dev->param.erase_fn || !dev->param.bad_block_fn)
148 if (writing && !yaffs2_checkpt_space_ok(dev))
151 if (!dev->checkpt_buffer)
152 dev->checkpt_buffer =
153 kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
154 if (!dev->checkpt_buffer)
157 dev->checkpt_page_seq = 0;
158 dev->checkpt_byte_count = 0;
159 dev->checkpt_sum = 0;
160 dev->checkpt_xor = 0;
161 dev->checkpt_cur_block = -1;
162 dev->checkpt_cur_chunk = -1;
163 dev->checkpt_next_block = dev->internal_start_block;
165 /* Erase all the blocks in the checkpoint area */
167 memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
168 dev->checkpt_byte_offs = 0;
169 return yaffs_checkpt_erase(dev);
172 /* Set to a value that will kick off a read */
173 dev->checkpt_byte_offs = dev->data_bytes_per_chunk;
174 /* A checkpoint block list of 1 checkpoint block per 16 block is
175 * (hopefully) going to be way more than we need */
176 dev->blocks_in_checkpt = 0;
177 dev->checkpt_max_blocks =
178 (dev->internal_end_block - dev->internal_start_block) / 16 +
180 dev->checkpt_block_list =
181 kmalloc(sizeof(int) * dev->checkpt_max_blocks, GFP_NOFS);
182 if (!dev->checkpt_block_list)
185 for (i = 0; i < dev->checkpt_max_blocks; i++)
186 dev->checkpt_block_list[i] = -1;
192 int yaffs2_get_checkpt_sum(struct yaffs_dev *dev, u32 * sum)
196 composite_sum = (dev->checkpt_sum << 8) | (dev->checkpt_xor & 0xFF);
197 *sum = composite_sum;
201 static int yaffs2_checkpt_flush_buffer(struct yaffs_dev *dev)
205 struct yaffs_ext_tags tags;
207 if (dev->checkpt_cur_block < 0) {
208 yaffs2_checkpt_find_erased_block(dev);
209 dev->checkpt_cur_chunk = 0;
212 if (dev->checkpt_cur_block < 0)
216 tags.obj_id = dev->checkpt_next_block; /* Hint to next place to look */
217 tags.chunk_id = dev->checkpt_page_seq + 1;
218 tags.seq_number = YAFFS_SEQUENCE_CHECKPOINT_DATA;
219 tags.n_bytes = dev->data_bytes_per_chunk;
220 if (dev->checkpt_cur_chunk == 0) {
221 /* First chunk we write for the block? Set block state to
223 struct yaffs_block_info *bi =
224 yaffs_get_block_info(dev, dev->checkpt_cur_block);
225 bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
226 dev->blocks_in_checkpt++;
230 dev->checkpt_cur_block * dev->param.chunks_per_block +
231 dev->checkpt_cur_chunk;
233 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
234 "checkpoint wite buffer nand %d(%d:%d) objid %d chId %d",
235 chunk, dev->checkpt_cur_block, dev->checkpt_cur_chunk,
236 tags.obj_id, tags.chunk_id);
238 realigned_chunk = chunk - dev->chunk_offset;
240 dev->n_page_writes++;
242 dev->param.write_chunk_tags_fn(dev, realigned_chunk,
243 dev->checkpt_buffer, &tags);
244 dev->checkpt_byte_offs = 0;
245 dev->checkpt_page_seq++;
246 dev->checkpt_cur_chunk++;
247 if (dev->checkpt_cur_chunk >= dev->param.chunks_per_block) {
248 dev->checkpt_cur_chunk = 0;
249 dev->checkpt_cur_block = -1;
251 memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
256 int yaffs2_checkpt_wr(struct yaffs_dev *dev, const void *data, int n_bytes)
260 u8 *data_bytes = (u8 *) data;
262 if (!dev->checkpt_buffer)
265 if (!dev->checkpt_open_write)
268 while (i < n_bytes && ok) {
269 dev->checkpt_buffer[dev->checkpt_byte_offs] = *data_bytes;
270 dev->checkpt_sum += *data_bytes;
271 dev->checkpt_xor ^= *data_bytes;
273 dev->checkpt_byte_offs++;
276 dev->checkpt_byte_count++;
278 if (dev->checkpt_byte_offs < 0 ||
279 dev->checkpt_byte_offs >= dev->data_bytes_per_chunk)
280 ok = yaffs2_checkpt_flush_buffer(dev);
286 int yaffs2_checkpt_rd(struct yaffs_dev *dev, void *data, int n_bytes)
290 struct yaffs_ext_tags tags;
293 u8 *data_bytes = (u8 *) data;
295 if (!dev->checkpt_buffer)
298 if (dev->checkpt_open_write)
301 while (i < n_bytes && ok) {
303 if (dev->checkpt_byte_offs < 0 ||
304 dev->checkpt_byte_offs >= dev->data_bytes_per_chunk) {
306 if (dev->checkpt_cur_block < 0) {
307 yaffs2_checkpt_find_block(dev);
308 dev->checkpt_cur_chunk = 0;
311 if (dev->checkpt_cur_block < 0)
314 chunk = dev->checkpt_cur_block *
315 dev->param.chunks_per_block +
316 dev->checkpt_cur_chunk;
318 realigned_chunk = chunk - dev->chunk_offset;
322 /* read in the next chunk */
323 dev->param.read_chunk_tags_fn(dev,
328 if (tags.chunk_id != (dev->checkpt_page_seq + 1)
329 || tags.ecc_result > YAFFS_ECC_RESULT_FIXED
330 || tags.seq_number !=
331 YAFFS_SEQUENCE_CHECKPOINT_DATA)
334 dev->checkpt_byte_offs = 0;
335 dev->checkpt_page_seq++;
336 dev->checkpt_cur_chunk++;
338 if (dev->checkpt_cur_chunk >=
339 dev->param.chunks_per_block)
340 dev->checkpt_cur_block = -1;
346 dev->checkpt_buffer[dev->checkpt_byte_offs];
347 dev->checkpt_sum += *data_bytes;
348 dev->checkpt_xor ^= *data_bytes;
349 dev->checkpt_byte_offs++;
352 dev->checkpt_byte_count++;
359 int yaffs_checkpt_close(struct yaffs_dev *dev)
361 if (dev->checkpt_open_write) {
362 if (dev->checkpt_byte_offs != 0)
363 yaffs2_checkpt_flush_buffer(dev);
364 } else if (dev->checkpt_block_list) {
367 i < dev->blocks_in_checkpt
368 && dev->checkpt_block_list[i] >= 0; i++) {
369 int blk = dev->checkpt_block_list[i];
370 struct yaffs_block_info *bi = NULL;
371 if (dev->internal_start_block <= blk
372 && blk <= dev->internal_end_block)
373 bi = yaffs_get_block_info(dev, blk);
374 if (bi && bi->block_state == YAFFS_BLOCK_STATE_EMPTY)
375 bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
377 kfree(dev->checkpt_block_list);
378 dev->checkpt_block_list = NULL;
381 dev->n_free_chunks -=
382 dev->blocks_in_checkpt * dev->param.chunks_per_block;
383 dev->n_erased_blocks -= dev->blocks_in_checkpt;
385 yaffs_trace(YAFFS_TRACE_CHECKPOINT, "checkpoint byte count %d",
386 dev->checkpt_byte_count);
388 if (dev->checkpt_buffer) {
389 /* free the buffer */
390 kfree(dev->checkpt_buffer);
391 dev->checkpt_buffer = NULL;
398 int yaffs2_checkpt_invalidate_stream(struct yaffs_dev *dev)
400 /* Erase the checkpoint data */
402 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
403 "checkpoint invalidate of %d blocks",
404 dev->blocks_in_checkpt);
406 return yaffs_checkpt_erase(dev);