yaffs: Add xattrib file to in-kernel Makefile
[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 yaffs_CheckpointSpaceOk(yaffs_Device *dev)
18 {
19         int blocksAvailable = dev->nErasedBlocks - dev->param.nReservedBlocks;
20
21         T(YAFFS_TRACE_CHECKPOINT,
22                 (TSTR("checkpt blocks available = %d" TENDSTR),
23                 blocksAvailable));
24
25         return (blocksAvailable <= 0) ? 0 : 1;
26 }
27
28
29 static int yaffs_CheckpointErase(yaffs_Device *dev)
30 {
31         int i;
32
33         if (!dev->param.eraseBlockInNAND)
34                 return 0;
35         T(YAFFS_TRACE_CHECKPOINT, (TSTR("checking blocks %d to %d"TENDSTR),
36                 dev->internalStartBlock, dev->internalEndBlock));
37
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));
42
43                         dev->nBlockErasures++;
44
45                         if (dev->param.eraseBlockInNAND(dev, i - dev->blockOffset /* realign */)) {
46                                 bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
47                                 dev->nErasedBlocks++;
48                                 dev->nFreeChunks += dev->param.nChunksPerBlock;
49                         } else {
50                                 dev->param.markNANDBlockBad(dev, i);
51                                 bi->blockState = YAFFS_BLOCK_STATE_DEAD;
52                         }
53                 }
54         }
55
56         dev->blocksInCheckpoint = 0;
57
58         return 1;
59 }
60
61
62 static void yaffs_CheckpointFindNextErasedBlock(yaffs_Device *dev)
63 {
64         int  i;
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));
69
70         if (dev->checkpointNextBlock >= 0 &&
71                         dev->checkpointNextBlock <= dev->internalEndBlock &&
72                         blocksAvailable > 0) {
73
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));
80                                 return;
81                         }
82                 }
83         }
84         T(YAFFS_TRACE_CHECKPOINT, (TSTR("out of checkpt blocks"TENDSTR)));
85
86         dev->checkpointNextBlock = -1;
87         dev->checkpointCurrentBlock = -1;
88 }
89
90 static void yaffs_CheckpointFindNextCheckpointBlock(yaffs_Device *dev)
91 {
92         int  i;
93         yaffs_ExtendedTags tags;
94
95         T(YAFFS_TRACE_CHECKPOINT, (TSTR("find next checkpt block: start:  blocks %d next %d" TENDSTR),
96                 dev->blocksInCheckpoint, dev->checkpointNextBlock));
97
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;
102
103                         dev->param.readChunkWithTagsFromNAND(dev, realignedChunk,
104                                         NULL, &tags);
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));
107
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));
115                                 return;
116                         }
117                 }
118
119         T(YAFFS_TRACE_CHECKPOINT, (TSTR("found no more checkpt blocks"TENDSTR)));
120
121         dev->checkpointNextBlock = -1;
122         dev->checkpointCurrentBlock = -1;
123 }
124
125
126 int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting)
127 {
128
129
130         dev->checkpointOpenForWrite = forWriting;
131
132         /* Got the functions we need? */
133         if (!dev->param.writeChunkWithTagsToNAND ||
134                 !dev->param.readChunkWithTagsFromNAND ||
135                 !dev->param.eraseBlockInNAND ||
136                 !dev->param.markNANDBlockBad)
137                 return 0;
138
139         if (forWriting && !yaffs_CheckpointSpaceOk(dev))
140                 return 0;
141
142         if (!dev->checkpointBuffer)
143                 dev->checkpointBuffer = YMALLOC_DMA(dev->param.totalBytesPerChunk);
144         if (!dev->checkpointBuffer)
145                 return 0;
146
147
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;
155
156         /* Erase all the blocks in the checkpoint area */
157         if (forWriting) {
158                 memset(dev->checkpointBuffer, 0, dev->nDataBytesPerChunk);
159                 dev->checkpointByteOffset = 0;
160                 return yaffs_CheckpointErase(dev);
161         } else {
162                 int i;
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)
171                         return 0;
172
173                 for (i = 0; i < dev->checkpointMaxBlocks; i++)
174                         dev->checkpointBlockList[i] = -1;
175         }
176
177         return 1;
178 }
179
180 int yaffs_GetCheckpointSum(yaffs_Device *dev, __u32 *sum)
181 {
182         __u32 compositeSum;
183         compositeSum =  (dev->checkpointSum << 8) | (dev->checkpointXor & 0xFF);
184         *sum = compositeSum;
185         return 1;
186 }
187
188 static int yaffs_CheckpointFlushBuffer(yaffs_Device *dev)
189 {
190         int chunk;
191         int realignedChunk;
192
193         yaffs_ExtendedTags tags;
194
195         if (dev->checkpointCurrentBlock < 0) {
196                 yaffs_CheckpointFindNextErasedBlock(dev);
197                 dev->checkpointCurrentChunk = 0;
198         }
199
200         if (dev->checkpointCurrentBlock < 0)
201                 return 0;
202
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
210                    checkpoint */
211                 yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, dev->checkpointCurrentBlock);
212                 bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
213                 dev->blocksInCheckpoint++;
214         }
215
216         chunk = dev->checkpointCurrentBlock * dev->param.nChunksPerBlock + dev->checkpointCurrentChunk;
217
218
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));
221
222         realignedChunk = chunk - dev->chunkOffset;
223
224         dev->nPageWrites++;
225
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;
234         }
235         memset(dev->checkpointBuffer, 0, dev->nDataBytesPerChunk);
236
237         return 1;
238 }
239
240
241 int yaffs_CheckpointWrite(yaffs_Device *dev, const void *data, int nBytes)
242 {
243         int i = 0;
244         int ok = 1;
245
246
247         __u8 * dataBytes = (__u8 *)data;
248
249
250
251         if (!dev->checkpointBuffer)
252                 return 0;
253
254         if (!dev->checkpointOpenForWrite)
255                 return -1;
256
257         while (i < nBytes && ok) {
258                 dev->checkpointBuffer[dev->checkpointByteOffset] = *dataBytes;
259                 dev->checkpointSum += *dataBytes;
260                 dev->checkpointXor ^= *dataBytes;
261
262                 dev->checkpointByteOffset++;
263                 i++;
264                 dataBytes++;
265                 dev->checkpointByteCount++;
266
267
268                 if (dev->checkpointByteOffset < 0 ||
269                    dev->checkpointByteOffset >= dev->nDataBytesPerChunk)
270                         ok = yaffs_CheckpointFlushBuffer(dev);
271         }
272
273         return i;
274 }
275
276 int yaffs_CheckpointRead(yaffs_Device *dev, void *data, int nBytes)
277 {
278         int i = 0;
279         int ok = 1;
280         yaffs_ExtendedTags tags;
281
282
283         int chunk;
284         int realignedChunk;
285
286         __u8 *dataBytes = (__u8 *)data;
287
288         if (!dev->checkpointBuffer)
289                 return 0;
290
291         if (dev->checkpointOpenForWrite)
292                 return -1;
293
294         while (i < nBytes && ok) {
295
296
297                 if (dev->checkpointByteOffset < 0 ||
298                         dev->checkpointByteOffset >= dev->nDataBytesPerChunk) {
299
300                         if (dev->checkpointCurrentBlock < 0) {
301                                 yaffs_CheckpointFindNextCheckpointBlock(dev);
302                                 dev->checkpointCurrentChunk = 0;
303                         }
304
305                         if (dev->checkpointCurrentBlock < 0)
306                                 ok = 0;
307                         else {
308                                 chunk = dev->checkpointCurrentBlock *
309                                         dev->param.nChunksPerBlock +
310                                         dev->checkpointCurrentChunk;
311
312                                 realignedChunk = chunk - dev->chunkOffset;
313                                 
314                                 dev->nPageReads++;
315
316                                 /* read in the next chunk */
317                                 /* printf("read checkpoint page %d\n",dev->checkpointPage); */
318                                 dev->param.readChunkWithTagsFromNAND(dev,
319                                                 realignedChunk,
320                                                 dev->checkpointBuffer,
321                                                 &tags);
322
323                                 if (tags.chunkId != (dev->checkpointPageSequence + 1) ||
324                                         tags.eccResult > YAFFS_ECC_RESULT_FIXED ||
325                                         tags.sequenceNumber != YAFFS_SEQUENCE_CHECKPOINT_DATA)
326                                         ok = 0;
327
328                                 dev->checkpointByteOffset = 0;
329                                 dev->checkpointPageSequence++;
330                                 dev->checkpointCurrentChunk++;
331
332                                 if (dev->checkpointCurrentChunk >= dev->param.nChunksPerBlock)
333                                         dev->checkpointCurrentBlock = -1;
334                         }
335                 }
336
337                 if (ok) {
338                         *dataBytes = dev->checkpointBuffer[dev->checkpointByteOffset];
339                         dev->checkpointSum += *dataBytes;
340                         dev->checkpointXor ^= *dataBytes;
341                         dev->checkpointByteOffset++;
342                         i++;
343                         dataBytes++;
344                         dev->checkpointByteCount++;
345                 }
346         }
347
348         return  i;
349 }
350
351 int yaffs_CheckpointClose(yaffs_Device *dev)
352 {
353
354         if (dev->checkpointOpenForWrite) {
355                 if (dev->checkpointByteOffset != 0)
356                         yaffs_CheckpointFlushBuffer(dev);
357         } else if(dev->checkpointBlockList){
358                 int i;
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;
366                         else {
367                                 /* Todo this looks odd... */
368                         }
369                 }
370                 YFREE(dev->checkpointBlockList);
371                 dev->checkpointBlockList = NULL;
372         }
373
374         dev->nFreeChunks -= dev->blocksInCheckpoint * dev->param.nChunksPerBlock;
375         dev->nErasedBlocks -= dev->blocksInCheckpoint;
376
377
378         T(YAFFS_TRACE_CHECKPOINT, (TSTR("checkpoint byte count %d" TENDSTR),
379                         dev->checkpointByteCount));
380
381         if (dev->checkpointBuffer) {
382                 /* free the buffer */
383                 YFREE(dev->checkpointBuffer);
384                 dev->checkpointBuffer = NULL;
385                 return 1;
386         } else
387                 return 0;
388 }
389
390 int yaffs_CheckpointInvalidateStream(yaffs_Device *dev)
391 {
392         /* Erase the checkpoint data */
393
394         T(YAFFS_TRACE_CHECKPOINT, (TSTR("checkpoint invalidate of %d blocks"TENDSTR),
395                 dev->blocksInCheckpoint));
396
397         return yaffs_CheckpointErase(dev);
398 }
399
400
401