2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2011 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"
16 #include <dm/devres.h>
18 static int yaffs2_checkpt_space_ok(struct yaffs_dev *dev)
20 int blocks_avail = dev->n_erased_blocks - dev->param.n_reserved_blocks;
22 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
23 "checkpt blocks_avail = %d", blocks_avail);
25 return (blocks_avail <= 0) ? 0 : 1;
28 static int yaffs_checkpt_erase(struct yaffs_dev *dev)
32 if (!dev->param.erase_fn)
34 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
35 "checking blocks %d to %d",
36 dev->internal_start_block, dev->internal_end_block);
38 for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
39 struct yaffs_block_info *bi = yaffs_get_block_info(dev, i);
40 if (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT) {
41 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
42 "erasing checkpt block %d", i);
48 i - dev->block_offset /* realign */)) {
49 bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
50 dev->n_erased_blocks++;
52 dev->param.chunks_per_block;
54 dev->param.bad_block_fn(dev, i);
55 bi->block_state = YAFFS_BLOCK_STATE_DEAD;
60 dev->blocks_in_checkpt = 0;
65 static void yaffs2_checkpt_find_erased_block(struct yaffs_dev *dev)
68 int blocks_avail = dev->n_erased_blocks - dev->param.n_reserved_blocks;
70 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
71 "allocating checkpt block: erased %d reserved %d avail %d next %d ",
72 dev->n_erased_blocks, dev->param.n_reserved_blocks,
73 blocks_avail, dev->checkpt_next_block);
75 if (dev->checkpt_next_block >= 0 &&
76 dev->checkpt_next_block <= dev->internal_end_block &&
79 for (i = dev->checkpt_next_block; i <= dev->internal_end_block;
81 struct yaffs_block_info *bi =
82 yaffs_get_block_info(dev, i);
83 if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
84 dev->checkpt_next_block = i + 1;
85 dev->checkpt_cur_block = i;
86 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
87 "allocating checkpt block %d", i);
92 yaffs_trace(YAFFS_TRACE_CHECKPOINT, "out of checkpt blocks");
94 dev->checkpt_next_block = -1;
95 dev->checkpt_cur_block = -1;
98 static void yaffs2_checkpt_find_block(struct yaffs_dev *dev)
101 struct yaffs_ext_tags tags;
103 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
104 "find next checkpt block: start: blocks %d next %d",
105 dev->blocks_in_checkpt, dev->checkpt_next_block);
107 if (dev->blocks_in_checkpt < dev->checkpt_max_blocks)
108 for (i = dev->checkpt_next_block; i <= dev->internal_end_block;
110 int chunk = i * dev->param.chunks_per_block;
111 int realigned_chunk = chunk - dev->chunk_offset;
113 dev->param.read_chunk_tags_fn(dev, realigned_chunk,
115 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
116 "find next checkpt block: search: block %d oid %d seq %d eccr %d",
117 i, tags.obj_id, tags.seq_number,
120 if (tags.seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA) {
121 /* Right kind of block */
122 dev->checkpt_next_block = tags.obj_id;
123 dev->checkpt_cur_block = i;
124 dev->checkpt_block_list[dev->
125 blocks_in_checkpt] = i;
126 dev->blocks_in_checkpt++;
127 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
128 "found checkpt block %d", i);
133 yaffs_trace(YAFFS_TRACE_CHECKPOINT, "found no more checkpt blocks");
135 dev->checkpt_next_block = -1;
136 dev->checkpt_cur_block = -1;
139 int yaffs2_checkpt_open(struct yaffs_dev *dev, int writing)
143 dev->checkpt_open_write = writing;
145 /* Got the functions we need? */
146 if (!dev->param.write_chunk_tags_fn ||
147 !dev->param.read_chunk_tags_fn ||
148 !dev->param.erase_fn || !dev->param.bad_block_fn)
151 if (writing && !yaffs2_checkpt_space_ok(dev))
154 if (!dev->checkpt_buffer)
155 dev->checkpt_buffer =
156 kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
157 if (!dev->checkpt_buffer)
160 dev->checkpt_page_seq = 0;
161 dev->checkpt_byte_count = 0;
162 dev->checkpt_sum = 0;
163 dev->checkpt_xor = 0;
164 dev->checkpt_cur_block = -1;
165 dev->checkpt_cur_chunk = -1;
166 dev->checkpt_next_block = dev->internal_start_block;
168 /* Erase all the blocks in the checkpoint area */
170 memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
171 dev->checkpt_byte_offs = 0;
172 return yaffs_checkpt_erase(dev);
175 /* Set to a value that will kick off a read */
176 dev->checkpt_byte_offs = dev->data_bytes_per_chunk;
177 /* A checkpoint block list of 1 checkpoint block per 16 block is
178 * (hopefully) going to be way more than we need */
179 dev->blocks_in_checkpt = 0;
180 dev->checkpt_max_blocks =
181 (dev->internal_end_block - dev->internal_start_block) / 16 + 2;
182 dev->checkpt_block_list =
183 kmalloc(sizeof(int) * dev->checkpt_max_blocks, GFP_NOFS);
185 if (!dev->checkpt_block_list)
188 for (i = 0; i < dev->checkpt_max_blocks; i++)
189 dev->checkpt_block_list[i] = -1;
194 int yaffs2_get_checkpt_sum(struct yaffs_dev *dev, u32 * sum)
198 composite_sum = (dev->checkpt_sum << 8) | (dev->checkpt_xor & 0xff);
199 *sum = composite_sum;
203 static int yaffs2_checkpt_flush_buffer(struct yaffs_dev *dev)
207 struct yaffs_ext_tags tags;
209 if (dev->checkpt_cur_block < 0) {
210 yaffs2_checkpt_find_erased_block(dev);
211 dev->checkpt_cur_chunk = 0;
214 if (dev->checkpt_cur_block < 0)
218 tags.obj_id = dev->checkpt_next_block; /* Hint to next place to look */
219 tags.chunk_id = dev->checkpt_page_seq + 1;
220 tags.seq_number = YAFFS_SEQUENCE_CHECKPOINT_DATA;
221 tags.n_bytes = dev->data_bytes_per_chunk;
222 if (dev->checkpt_cur_chunk == 0) {
223 /* First chunk we write for the block? Set block state to
225 struct yaffs_block_info *bi =
226 yaffs_get_block_info(dev, dev->checkpt_cur_block);
227 bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
228 dev->blocks_in_checkpt++;
232 dev->checkpt_cur_block * dev->param.chunks_per_block +
233 dev->checkpt_cur_chunk;
235 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
236 "checkpoint wite buffer nand %d(%d:%d) objid %d chId %d",
237 chunk, dev->checkpt_cur_block, dev->checkpt_cur_chunk,
238 tags.obj_id, tags.chunk_id);
240 realigned_chunk = chunk - dev->chunk_offset;
242 dev->n_page_writes++;
244 dev->param.write_chunk_tags_fn(dev, realigned_chunk,
245 dev->checkpt_buffer, &tags);
246 dev->checkpt_byte_offs = 0;
247 dev->checkpt_page_seq++;
248 dev->checkpt_cur_chunk++;
249 if (dev->checkpt_cur_chunk >= dev->param.chunks_per_block) {
250 dev->checkpt_cur_chunk = 0;
251 dev->checkpt_cur_block = -1;
253 memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
258 int yaffs2_checkpt_wr(struct yaffs_dev *dev, const void *data, int n_bytes)
262 u8 *data_bytes = (u8 *) data;
264 if (!dev->checkpt_buffer)
267 if (!dev->checkpt_open_write)
270 while (i < n_bytes && ok) {
271 dev->checkpt_buffer[dev->checkpt_byte_offs] = *data_bytes;
272 dev->checkpt_sum += *data_bytes;
273 dev->checkpt_xor ^= *data_bytes;
275 dev->checkpt_byte_offs++;
278 dev->checkpt_byte_count++;
280 if (dev->checkpt_byte_offs < 0 ||
281 dev->checkpt_byte_offs >= dev->data_bytes_per_chunk)
282 ok = yaffs2_checkpt_flush_buffer(dev);
288 int yaffs2_checkpt_rd(struct yaffs_dev *dev, void *data, int n_bytes)
292 struct yaffs_ext_tags tags;
295 u8 *data_bytes = (u8 *) data;
297 if (!dev->checkpt_buffer)
300 if (dev->checkpt_open_write)
303 while (i < n_bytes && ok) {
305 if (dev->checkpt_byte_offs < 0 ||
306 dev->checkpt_byte_offs >= dev->data_bytes_per_chunk) {
308 if (dev->checkpt_cur_block < 0) {
309 yaffs2_checkpt_find_block(dev);
310 dev->checkpt_cur_chunk = 0;
313 if (dev->checkpt_cur_block < 0) {
318 chunk = dev->checkpt_cur_block *
319 dev->param.chunks_per_block +
320 dev->checkpt_cur_chunk;
322 realigned_chunk = chunk - dev->chunk_offset;
325 /* read in the next chunk */
326 dev->param.read_chunk_tags_fn(dev,
331 if (tags.chunk_id != (dev->checkpt_page_seq + 1) ||
332 tags.ecc_result > YAFFS_ECC_RESULT_FIXED ||
333 tags.seq_number != YAFFS_SEQUENCE_CHECKPOINT_DATA) {
338 dev->checkpt_byte_offs = 0;
339 dev->checkpt_page_seq++;
340 dev->checkpt_cur_chunk++;
342 if (dev->checkpt_cur_chunk >=
343 dev->param.chunks_per_block)
344 dev->checkpt_cur_block = -1;
347 *data_bytes = dev->checkpt_buffer[dev->checkpt_byte_offs];
348 dev->checkpt_sum += *data_bytes;
349 dev->checkpt_xor ^= *data_bytes;
350 dev->checkpt_byte_offs++;
353 dev->checkpt_byte_count++;
359 int yaffs_checkpt_close(struct yaffs_dev *dev)
363 if (dev->checkpt_open_write) {
364 if (dev->checkpt_byte_offs != 0)
365 yaffs2_checkpt_flush_buffer(dev);
366 } else if (dev->checkpt_block_list) {
368 i < dev->blocks_in_checkpt &&
369 dev->checkpt_block_list[i] >= 0; i++) {
370 int blk = dev->checkpt_block_list[i];
371 struct yaffs_block_info *bi = NULL;
373 if (dev->internal_start_block <= blk &&
374 blk <= dev->internal_end_block)
375 bi = yaffs_get_block_info(dev, blk);
376 if (bi && bi->block_state == YAFFS_BLOCK_STATE_EMPTY)
377 bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
379 kfree(dev->checkpt_block_list);
380 dev->checkpt_block_list = NULL;
383 dev->n_free_chunks -=
384 dev->blocks_in_checkpt * dev->param.chunks_per_block;
385 dev->n_erased_blocks -= dev->blocks_in_checkpt;
387 yaffs_trace(YAFFS_TRACE_CHECKPOINT, "checkpoint byte count %d",
388 dev->checkpt_byte_count);
390 if (dev->checkpt_buffer) {
391 /* free the buffer */
392 kfree(dev->checkpt_buffer);
393 dev->checkpt_buffer = NULL;
400 int yaffs2_checkpt_invalidate_stream(struct yaffs_dev *dev)
402 /* Erase the checkpoint data */
404 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
405 "checkpoint invalidate of %d blocks",
406 dev->blocks_in_checkpt);
408 return yaffs_checkpt_erase(dev);