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 yaffs_CheckpointSpaceOk(yaffs_Device *dev)
19 int blocksAvailable = dev->nErasedBlocks - dev->param.nReservedBlocks;
21 T(YAFFS_TRACE_CHECKPOINT,
22 (TSTR("checkpt blocks available = %d" TENDSTR),
25 return (blocksAvailable <= 0) ? 0 : 1;
29 static int yaffs_CheckpointErase(yaffs_Device *dev)
33 if (!dev->param.eraseBlockInNAND)
35 T(YAFFS_TRACE_CHECKPOINT, (TSTR("checking blocks %d to %d"TENDSTR),
36 dev->internalStartBlock, dev->internalEndBlock));
38 for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
39 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, i);
40 if (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT) {
41 T(YAFFS_TRACE_CHECKPOINT, (TSTR("erasing checkpt block %d"TENDSTR), i));
43 dev->nBlockErasures++;
45 if (dev->param.eraseBlockInNAND(dev, i - dev->blockOffset /* realign */)) {
46 bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
48 dev->nFreeChunks += dev->param.nChunksPerBlock;
50 dev->param.markNANDBlockBad(dev, i);
51 bi->blockState = YAFFS_BLOCK_STATE_DEAD;
56 dev->blocksInCheckpoint = 0;
62 static void yaffs_CheckpointFindNextErasedBlock(yaffs_Device *dev)
65 int blocksAvailable = dev->nErasedBlocks - dev->param.nReservedBlocks;
66 T(YAFFS_TRACE_CHECKPOINT,
67 (TSTR("allocating checkpt block: erased %d reserved %d avail %d next %d "TENDSTR),
68 dev->nErasedBlocks, dev->param.nReservedBlocks, blocksAvailable, dev->checkpointNextBlock));
70 if (dev->checkpointNextBlock >= 0 &&
71 dev->checkpointNextBlock <= dev->internalEndBlock &&
72 blocksAvailable > 0) {
74 for (i = dev->checkpointNextBlock; i <= dev->internalEndBlock; i++) {
75 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, i);
76 if (bi->blockState == YAFFS_BLOCK_STATE_EMPTY) {
77 dev->checkpointNextBlock = i + 1;
78 dev->checkpointCurrentBlock = i;
79 T(YAFFS_TRACE_CHECKPOINT, (TSTR("allocating checkpt block %d"TENDSTR), i));
84 T(YAFFS_TRACE_CHECKPOINT, (TSTR("out of checkpt blocks"TENDSTR)));
86 dev->checkpointNextBlock = -1;
87 dev->checkpointCurrentBlock = -1;
90 static void yaffs_CheckpointFindNextCheckpointBlock(yaffs_Device *dev)
93 yaffs_ExtendedTags tags;
95 T(YAFFS_TRACE_CHECKPOINT, (TSTR("find next checkpt block: start: blocks %d next %d" TENDSTR),
96 dev->blocksInCheckpoint, dev->checkpointNextBlock));
98 if (dev->blocksInCheckpoint < dev->checkpointMaxBlocks)
99 for (i = dev->checkpointNextBlock; i <= dev->internalEndBlock; i++) {
100 int chunk = i * dev->param.nChunksPerBlock;
101 int realignedChunk = chunk - dev->chunkOffset;
103 dev->param.readChunkWithTagsFromNAND(dev, realignedChunk,
105 T(YAFFS_TRACE_CHECKPOINT, (TSTR("find next checkpt block: search: block %d oid %d seq %d eccr %d" TENDSTR),
106 i, tags.objectId, tags.sequenceNumber, tags.eccResult));
108 if (tags.sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA) {
109 /* Right kind of block */
110 dev->checkpointNextBlock = tags.objectId;
111 dev->checkpointCurrentBlock = i;
112 dev->checkpointBlockList[dev->blocksInCheckpoint] = i;
113 dev->blocksInCheckpoint++;
114 T(YAFFS_TRACE_CHECKPOINT, (TSTR("found checkpt block %d"TENDSTR), i));
119 T(YAFFS_TRACE_CHECKPOINT, (TSTR("found no more checkpt blocks"TENDSTR)));
121 dev->checkpointNextBlock = -1;
122 dev->checkpointCurrentBlock = -1;
126 int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting)
130 dev->checkpointOpenForWrite = forWriting;
132 /* Got the functions we need? */
133 if (!dev->param.writeChunkWithTagsToNAND ||
134 !dev->param.readChunkWithTagsFromNAND ||
135 !dev->param.eraseBlockInNAND ||
136 !dev->param.markNANDBlockBad)
139 if (forWriting && !yaffs_CheckpointSpaceOk(dev))
142 if (!dev->checkpointBuffer)
143 dev->checkpointBuffer = YMALLOC_DMA(dev->param.totalBytesPerChunk);
144 if (!dev->checkpointBuffer)
148 dev->checkpointPageSequence = 0;
149 dev->checkpointByteCount = 0;
150 dev->checkpointSum = 0;
151 dev->checkpointXor = 0;
152 dev->checkpointCurrentBlock = -1;
153 dev->checkpointCurrentChunk = -1;
154 dev->checkpointNextBlock = dev->internalStartBlock;
156 /* Erase all the blocks in the checkpoint area */
158 memset(dev->checkpointBuffer, 0, dev->nDataBytesPerChunk);
159 dev->checkpointByteOffset = 0;
160 return yaffs_CheckpointErase(dev);
163 /* Set to a value that will kick off a read */
164 dev->checkpointByteOffset = dev->nDataBytesPerChunk;
165 /* A checkpoint block list of 1 checkpoint block per 16 block is (hopefully)
166 * going to be way more than we need */
167 dev->blocksInCheckpoint = 0;
168 dev->checkpointMaxBlocks = (dev->internalEndBlock - dev->internalStartBlock)/16 + 2;
169 dev->checkpointBlockList = YMALLOC(sizeof(int) * dev->checkpointMaxBlocks);
170 if(!dev->checkpointBlockList)
173 for (i = 0; i < dev->checkpointMaxBlocks; i++)
174 dev->checkpointBlockList[i] = -1;
180 int yaffs_GetCheckpointSum(yaffs_Device *dev, __u32 *sum)
183 compositeSum = (dev->checkpointSum << 8) | (dev->checkpointXor & 0xFF);
188 static int yaffs_CheckpointFlushBuffer(yaffs_Device *dev)
193 yaffs_ExtendedTags tags;
195 if (dev->checkpointCurrentBlock < 0) {
196 yaffs_CheckpointFindNextErasedBlock(dev);
197 dev->checkpointCurrentChunk = 0;
200 if (dev->checkpointCurrentBlock < 0)
203 tags.chunkDeleted = 0;
204 tags.objectId = dev->checkpointNextBlock; /* Hint to next place to look */
205 tags.chunkId = dev->checkpointPageSequence + 1;
206 tags.sequenceNumber = YAFFS_SEQUENCE_CHECKPOINT_DATA;
207 tags.byteCount = dev->nDataBytesPerChunk;
208 if (dev->checkpointCurrentChunk == 0) {
209 /* First chunk we write for the block? Set block state to
211 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, dev->checkpointCurrentBlock);
212 bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
213 dev->blocksInCheckpoint++;
216 chunk = dev->checkpointCurrentBlock * dev->param.nChunksPerBlock + dev->checkpointCurrentChunk;
219 T(YAFFS_TRACE_CHECKPOINT, (TSTR("checkpoint wite buffer nand %d(%d:%d) objid %d chId %d" TENDSTR),
220 chunk, dev->checkpointCurrentBlock, dev->checkpointCurrentChunk, tags.objectId, tags.chunkId));
222 realignedChunk = chunk - dev->chunkOffset;
226 dev->param.writeChunkWithTagsToNAND(dev, realignedChunk,
227 dev->checkpointBuffer, &tags);
228 dev->checkpointByteOffset = 0;
229 dev->checkpointPageSequence++;
230 dev->checkpointCurrentChunk++;
231 if (dev->checkpointCurrentChunk >= dev->param.nChunksPerBlock) {
232 dev->checkpointCurrentChunk = 0;
233 dev->checkpointCurrentBlock = -1;
235 memset(dev->checkpointBuffer, 0, dev->nDataBytesPerChunk);
241 int yaffs_CheckpointWrite(yaffs_Device *dev, const void *data, int nBytes)
247 __u8 * dataBytes = (__u8 *)data;
251 if (!dev->checkpointBuffer)
254 if (!dev->checkpointOpenForWrite)
257 while (i < nBytes && ok) {
258 dev->checkpointBuffer[dev->checkpointByteOffset] = *dataBytes;
259 dev->checkpointSum += *dataBytes;
260 dev->checkpointXor ^= *dataBytes;
262 dev->checkpointByteOffset++;
265 dev->checkpointByteCount++;
268 if (dev->checkpointByteOffset < 0 ||
269 dev->checkpointByteOffset >= dev->nDataBytesPerChunk)
270 ok = yaffs_CheckpointFlushBuffer(dev);
276 int yaffs_CheckpointRead(yaffs_Device *dev, void *data, int nBytes)
280 yaffs_ExtendedTags tags;
286 __u8 *dataBytes = (__u8 *)data;
288 if (!dev->checkpointBuffer)
291 if (dev->checkpointOpenForWrite)
294 while (i < nBytes && ok) {
297 if (dev->checkpointByteOffset < 0 ||
298 dev->checkpointByteOffset >= dev->nDataBytesPerChunk) {
300 if (dev->checkpointCurrentBlock < 0) {
301 yaffs_CheckpointFindNextCheckpointBlock(dev);
302 dev->checkpointCurrentChunk = 0;
305 if (dev->checkpointCurrentBlock < 0)
308 chunk = dev->checkpointCurrentBlock *
309 dev->param.nChunksPerBlock +
310 dev->checkpointCurrentChunk;
312 realignedChunk = chunk - dev->chunkOffset;
316 /* read in the next chunk */
317 /* printf("read checkpoint page %d\n",dev->checkpointPage); */
318 dev->param.readChunkWithTagsFromNAND(dev,
320 dev->checkpointBuffer,
323 if (tags.chunkId != (dev->checkpointPageSequence + 1) ||
324 tags.eccResult > YAFFS_ECC_RESULT_FIXED ||
325 tags.sequenceNumber != YAFFS_SEQUENCE_CHECKPOINT_DATA)
328 dev->checkpointByteOffset = 0;
329 dev->checkpointPageSequence++;
330 dev->checkpointCurrentChunk++;
332 if (dev->checkpointCurrentChunk >= dev->param.nChunksPerBlock)
333 dev->checkpointCurrentBlock = -1;
338 *dataBytes = dev->checkpointBuffer[dev->checkpointByteOffset];
339 dev->checkpointSum += *dataBytes;
340 dev->checkpointXor ^= *dataBytes;
341 dev->checkpointByteOffset++;
344 dev->checkpointByteCount++;
351 int yaffs_CheckpointClose(yaffs_Device *dev)
354 if (dev->checkpointOpenForWrite) {
355 if (dev->checkpointByteOffset != 0)
356 yaffs_CheckpointFlushBuffer(dev);
357 } else if(dev->checkpointBlockList){
359 for (i = 0; i < dev->blocksInCheckpoint && dev->checkpointBlockList[i] >= 0; i++) {
360 int blk = dev->checkpointBlockList[i];
361 yaffs_BlockInfo *bi = NULL;
362 if( dev->internalStartBlock <= blk && blk <= dev->internalEndBlock)
363 bi = yaffs_GetBlockInfo(dev, blk);
364 if (bi && bi->blockState == YAFFS_BLOCK_STATE_EMPTY)
365 bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
367 /* Todo this looks odd... */
370 YFREE(dev->checkpointBlockList);
371 dev->checkpointBlockList = NULL;
374 dev->nFreeChunks -= dev->blocksInCheckpoint * dev->param.nChunksPerBlock;
375 dev->nErasedBlocks -= dev->blocksInCheckpoint;
378 T(YAFFS_TRACE_CHECKPOINT, (TSTR("checkpoint byte count %d" TENDSTR),
379 dev->checkpointByteCount));
381 if (dev->checkpointBuffer) {
382 /* free the buffer */
383 YFREE(dev->checkpointBuffer);
384 dev->checkpointBuffer = NULL;
390 int yaffs_CheckpointInvalidateStream(yaffs_Device *dev)
392 /* Erase the checkpoint data */
394 T(YAFFS_TRACE_CHECKPOINT, (TSTR("checkpoint invalidate of %d blocks"TENDSTR),
395 dev->blocksInCheckpoint));
397 return yaffs_CheckpointErase(dev);