yaffs Normalized headers for release branch.
[yaffs2.git] / yaffs_yaffs1.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_yaffs1.h"
15 #include "yportenv.h"
16 #include "yaffs_trace.h"
17 #include "yaffs_bitmap.h"
18 #include "yaffs_getblockinfo.h"
19 #include "yaffs_nand.h"
20
21
22 int yaffs1_Scan(yaffs_Device *dev)
23 {
24         yaffs_ExtendedTags tags;
25         int blk;
26         int blockIterator;
27         int startIterator;
28         int endIterator;
29         int result;
30
31         int chunk;
32         int c;
33         int deleted;
34         yaffs_BlockState state;
35         yaffs_Object *hardList = NULL;
36         yaffs_BlockInfo *bi;
37         __u32 sequenceNumber;
38         yaffs_ObjectHeader *oh;
39         yaffs_Object *in;
40         yaffs_Object *parent;
41
42         int alloc_failed = 0;
43
44         struct yaffs_ShadowFixerStruct *shadowFixerList = NULL;
45
46
47         __u8 *chunkData;
48
49
50
51         T(YAFFS_TRACE_SCAN,
52           (TSTR("yaffs1_Scan starts  intstartblk %d intendblk %d..." TENDSTR),
53            dev->internalStartBlock, dev->internalEndBlock));
54
55         chunkData = yaffs_GetTempBuffer(dev, __LINE__);
56
57         dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
58
59         /* Scan all the blocks to determine their state */
60         bi = dev->blockInfo;
61         for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
62                 yaffs_ClearChunkBits(dev, blk);
63                 bi->pagesInUse = 0;
64                 bi->softDeletions = 0;
65
66                 yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
67
68                 bi->blockState = state;
69                 bi->sequenceNumber = sequenceNumber;
70
71                 if (bi->sequenceNumber == YAFFS_SEQUENCE_BAD_BLOCK)
72                         bi->blockState = state = YAFFS_BLOCK_STATE_DEAD;
73
74                 T(YAFFS_TRACE_SCAN_DEBUG,
75                   (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
76                    state, sequenceNumber));
77
78                 if (state == YAFFS_BLOCK_STATE_DEAD) {
79                         T(YAFFS_TRACE_BAD_BLOCKS,
80                           (TSTR("block %d is bad" TENDSTR), blk));
81                 } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
82                         T(YAFFS_TRACE_SCAN_DEBUG,
83                           (TSTR("Block empty " TENDSTR)));
84                         dev->nErasedBlocks++;
85                         dev->nFreeChunks += dev->param.nChunksPerBlock;
86                 }
87                 bi++;
88         }
89
90         startIterator = dev->internalStartBlock;
91         endIterator = dev->internalEndBlock;
92
93         /* For each block.... */
94         for (blockIterator = startIterator; !alloc_failed && blockIterator <= endIterator;
95              blockIterator++) {
96
97                 YYIELD();
98
99                 YYIELD();
100
101                 blk = blockIterator;
102
103                 bi = yaffs_GetBlockInfo(dev, blk);
104                 state = bi->blockState;
105
106                 deleted = 0;
107
108                 /* For each chunk in each block that needs scanning....*/
109                 for (c = 0; !alloc_failed && c < dev->param.nChunksPerBlock &&
110                      state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) {
111                         /* Read the tags and decide what to do */
112                         chunk = blk * dev->param.nChunksPerBlock + c;
113
114                         result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
115                                                         &tags);
116
117                         /* Let's have a good look at this chunk... */
118
119                         if (tags.eccResult == YAFFS_ECC_RESULT_UNFIXED || tags.chunkDeleted) {
120                                 /* YAFFS1 only...
121                                  * A deleted chunk
122                                  */
123                                 deleted++;
124                                 dev->nFreeChunks++;
125                                 /*T((" %d %d deleted\n",blk,c)); */
126                         } else if (!tags.chunkUsed) {
127                                 /* An unassigned chunk in the block
128                                  * This means that either the block is empty or
129                                  * this is the one being allocated from
130                                  */
131
132                                 if (c == 0) {
133                                         /* We're looking at the first chunk in the block so the block is unused */
134                                         state = YAFFS_BLOCK_STATE_EMPTY;
135                                         dev->nErasedBlocks++;
136                                 } else {
137                                         /* this is the block being allocated from */
138                                         T(YAFFS_TRACE_SCAN,
139                                           (TSTR
140                                            (" Allocating from %d %d" TENDSTR),
141                                            blk, c));
142                                         state = YAFFS_BLOCK_STATE_ALLOCATING;
143                                         dev->allocationBlock = blk;
144                                         dev->allocationPage = c;
145                                         dev->allocationBlockFinder = blk;
146                                         /* Set block finder here to encourage the allocator to go forth from here. */
147
148                                 }
149
150                                 dev->nFreeChunks += (dev->param.nChunksPerBlock - c);
151                         } else if (tags.chunkId > 0) {
152                                 /* chunkId > 0 so it is a data chunk... */
153                                 unsigned int endpos;
154
155                                 yaffs_SetChunkBit(dev, blk, c);
156                                 bi->pagesInUse++;
157
158                                 in = yaffs_FindOrCreateObjectByNumber(dev,
159                                                                       tags.
160                                                                       objectId,
161                                                                       YAFFS_OBJECT_TYPE_FILE);
162                                 /* PutChunkIntoFile checks for a clash (two data chunks with
163                                  * the same chunkId).
164                                  */
165
166                                 if (!in)
167                                         alloc_failed = 1;
168
169                                 if (in) {
170                                         if (!yaffs_PutChunkIntoFile(in, tags.chunkId, chunk, 1))
171                                                 alloc_failed = 1;
172                                 }
173
174                                 endpos =
175                                     (tags.chunkId - 1) * dev->nDataBytesPerChunk +
176                                     tags.byteCount;
177                                 if (in &&
178                                     in->variantType == YAFFS_OBJECT_TYPE_FILE
179                                     && in->variant.fileVariant.scannedFileSize <
180                                     endpos) {
181                                         in->variant.fileVariant.
182                                             scannedFileSize = endpos;
183                                         if (!dev->param.useHeaderFileSize) {
184                                                 in->variant.fileVariant.
185                                                     fileSize =
186                                                     in->variant.fileVariant.
187                                                     scannedFileSize;
188                                         }
189
190                                 }
191                                 /* T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId));   */
192                         } else {
193                                 /* chunkId == 0, so it is an ObjectHeader.
194                                  * Thus, we read in the object header and make the object
195                                  */
196                                 yaffs_SetChunkBit(dev, blk, c);
197                                 bi->pagesInUse++;
198
199                                 result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk,
200                                                                 chunkData,
201                                                                 NULL);
202
203                                 oh = (yaffs_ObjectHeader *) chunkData;
204
205                                 in = yaffs_FindObjectByNumber(dev,
206                                                               tags.objectId);
207                                 if (in && in->variantType != oh->type) {
208                                         /* This should not happen, but somehow
209                                          * Wev'e ended up with an objectId that has been reused but not yet
210                                          * deleted, and worse still it has changed type. Delete the old object.
211                                          */
212
213                                         yaffs_DeleteObject(in);
214
215                                         in = 0;
216                                 }
217
218                                 in = yaffs_FindOrCreateObjectByNumber(dev,
219                                                                       tags.
220                                                                       objectId,
221                                                                       oh->type);
222
223                                 if (!in)
224                                         alloc_failed = 1;
225
226                                 if (in && oh->shadowsObject > 0) {
227
228                                         struct yaffs_ShadowFixerStruct *fixer;
229                                         fixer = YMALLOC(sizeof(struct yaffs_ShadowFixerStruct));
230                                         if (fixer) {
231                                                 fixer->next = shadowFixerList;
232                                                 shadowFixerList = fixer;
233                                                 fixer->objectId = tags.objectId;
234                                                 fixer->shadowedId = oh->shadowsObject;
235                                                 T(YAFFS_TRACE_SCAN,
236                                                   (TSTR
237                                                    (" Shadow fixer: %d shadows %d" TENDSTR),
238                                                    fixer->objectId, fixer->shadowedId));
239
240                                         }
241
242                                 }
243
244                                 if (in && in->valid) {
245                                         /* We have already filled this one. We have a duplicate and need to resolve it. */
246
247                                         unsigned existingSerial = in->serial;
248                                         unsigned newSerial = tags.serialNumber;
249
250                                         if (((existingSerial + 1) & 3) == newSerial) {
251                                                 /* Use new one - destroy the exisiting one */
252                                                 yaffs_DeleteChunk(dev,
253                                                                   in->hdrChunk,
254                                                                   1, __LINE__);
255                                                 in->valid = 0;
256                                         } else {
257                                                 /* Use existing - destroy this one. */
258                                                 yaffs_DeleteChunk(dev, chunk, 1,
259                                                                   __LINE__);
260                                         }
261                                 }
262
263                                 if (in && !in->valid &&
264                                     (tags.objectId == YAFFS_OBJECTID_ROOT ||
265                                      tags.objectId == YAFFS_OBJECTID_LOSTNFOUND)) {
266                                         /* We only load some info, don't fiddle with directory structure */
267                                         in->valid = 1;
268                                         in->variantType = oh->type;
269
270                                         in->yst_mode = oh->yst_mode;
271 #ifdef CONFIG_YAFFS_WINCE
272                                         in->win_atime[0] = oh->win_atime[0];
273                                         in->win_ctime[0] = oh->win_ctime[0];
274                                         in->win_mtime[0] = oh->win_mtime[0];
275                                         in->win_atime[1] = oh->win_atime[1];
276                                         in->win_ctime[1] = oh->win_ctime[1];
277                                         in->win_mtime[1] = oh->win_mtime[1];
278 #else
279                                         in->yst_uid = oh->yst_uid;
280                                         in->yst_gid = oh->yst_gid;
281                                         in->yst_atime = oh->yst_atime;
282                                         in->yst_mtime = oh->yst_mtime;
283                                         in->yst_ctime = oh->yst_ctime;
284                                         in->yst_rdev = oh->yst_rdev;
285 #endif
286                                         in->hdrChunk = chunk;
287                                         in->serial = tags.serialNumber;
288
289                                 } else if (in && !in->valid) {
290                                         /* we need to load this info */
291
292                                         in->valid = 1;
293                                         in->variantType = oh->type;
294
295                                         in->yst_mode = oh->yst_mode;
296 #ifdef CONFIG_YAFFS_WINCE
297                                         in->win_atime[0] = oh->win_atime[0];
298                                         in->win_ctime[0] = oh->win_ctime[0];
299                                         in->win_mtime[0] = oh->win_mtime[0];
300                                         in->win_atime[1] = oh->win_atime[1];
301                                         in->win_ctime[1] = oh->win_ctime[1];
302                                         in->win_mtime[1] = oh->win_mtime[1];
303 #else
304                                         in->yst_uid = oh->yst_uid;
305                                         in->yst_gid = oh->yst_gid;
306                                         in->yst_atime = oh->yst_atime;
307                                         in->yst_mtime = oh->yst_mtime;
308                                         in->yst_ctime = oh->yst_ctime;
309                                         in->yst_rdev = oh->yst_rdev;
310 #endif
311                                         in->hdrChunk = chunk;
312                                         in->serial = tags.serialNumber;
313
314                                         yaffs_SetObjectNameFromOH(in, oh);
315                                         in->dirty = 0;
316
317                                         /* directory stuff...
318                                          * hook up to parent
319                                          */
320
321                                         parent =
322                                             yaffs_FindOrCreateObjectByNumber
323                                             (dev, oh->parentObjectId,
324                                              YAFFS_OBJECT_TYPE_DIRECTORY);
325                                         if (!parent)
326                                                 alloc_failed = 1;
327                                         if (parent && parent->variantType ==
328                                             YAFFS_OBJECT_TYPE_UNKNOWN) {
329                                                 /* Set up as a directory */
330                                                 parent->variantType =
331                                                         YAFFS_OBJECT_TYPE_DIRECTORY;
332                                                 YINIT_LIST_HEAD(&parent->variant.
333                                                                 directoryVariant.
334                                                                 children);
335                                         } else if (!parent || parent->variantType !=
336                                                    YAFFS_OBJECT_TYPE_DIRECTORY) {
337                                                 /* Hoosterman, another problem....
338                                                  * We're trying to use a non-directory as a directory
339                                                  */
340
341                                                 T(YAFFS_TRACE_ERROR,
342                                                   (TSTR
343                                                    ("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
344                                                     TENDSTR)));
345                                                 parent = dev->lostNFoundDir;
346                                         }
347
348                                         yaffs_AddObjectToDirectory(parent, in);
349
350                                         if (0 && (parent == dev->deletedDir ||
351                                                   parent == dev->unlinkedDir)) {
352                                                 in->deleted = 1;        /* If it is unlinked at start up then it wants deleting */
353                                                 dev->nDeletedFiles++;
354                                         }
355                                         /* Note re hardlinks.
356                                          * Since we might scan a hardlink before its equivalent object is scanned
357                                          * we put them all in a list.
358                                          * After scanning is complete, we should have all the objects, so we run through this
359                                          * list and fix up all the chains.
360                                          */
361
362                                         switch (in->variantType) {
363                                         case YAFFS_OBJECT_TYPE_UNKNOWN:
364                                                 /* Todo got a problem */
365                                                 break;
366                                         case YAFFS_OBJECT_TYPE_FILE:
367                                                 if (dev->param.useHeaderFileSize)
368
369                                                         in->variant.fileVariant.
370                                                             fileSize =
371                                                             oh->fileSize;
372
373                                                 break;
374                                         case YAFFS_OBJECT_TYPE_HARDLINK:
375                                                 in->variant.hardLinkVariant.
376                                                         equivalentObjectId =
377                                                         oh->equivalentObjectId;
378                                                 in->hardLinks.next =
379                                                         (struct ylist_head *)
380                                                         hardList;
381                                                 hardList = in;
382                                                 break;
383                                         case YAFFS_OBJECT_TYPE_DIRECTORY:
384                                                 /* Do nothing */
385                                                 break;
386                                         case YAFFS_OBJECT_TYPE_SPECIAL:
387                                                 /* Do nothing */
388                                                 break;
389                                         case YAFFS_OBJECT_TYPE_SYMLINK:
390                                                 in->variant.symLinkVariant.alias =
391                                                     yaffs_CloneString(oh->alias);
392                                                 if (!in->variant.symLinkVariant.alias)
393                                                         alloc_failed = 1;
394                                                 break;
395                                         }
396
397                                 }
398                         }
399                 }
400
401                 if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
402                         /* If we got this far while scanning, then the block is fully allocated.*/
403                         state = YAFFS_BLOCK_STATE_FULL;
404                 }
405
406                 if (state == YAFFS_BLOCK_STATE_ALLOCATING) {
407                         /* If the block was partially allocated then treat it as fully allocated.*/
408                         state = YAFFS_BLOCK_STATE_FULL;
409                         dev->allocationBlock = -1;
410                 }
411
412                 bi->blockState = state;
413
414                 /* Now let's see if it was dirty */
415                 if (bi->pagesInUse == 0 &&
416                     !bi->hasShrinkHeader &&
417                     bi->blockState == YAFFS_BLOCK_STATE_FULL) {
418                         yaffs_BlockBecameDirty(dev, blk);
419                 }
420
421         }
422
423
424         /* Ok, we've done all the scanning.
425          * Fix up the hard link chains.
426          * We should now have scanned all the objects, now it's time to add these
427          * hardlinks.
428          */
429
430         yaffs_HardlinkFixup(dev, hardList);
431
432         /* Fix up any shadowed objects */
433         {
434                 struct yaffs_ShadowFixerStruct *fixer;
435                 yaffs_Object *obj;
436
437                 while (shadowFixerList) {
438                         fixer = shadowFixerList;
439                         shadowFixerList = fixer->next;
440                         /* Complete the rename transaction by deleting the shadowed object
441                          * then setting the object header to unshadowed.
442                          */
443                         obj = yaffs_FindObjectByNumber(dev, fixer->shadowedId);
444                         if (obj)
445                                 yaffs_DeleteObject(obj);
446
447                         obj = yaffs_FindObjectByNumber(dev, fixer->objectId);
448
449                         if (obj)
450                                 yaffs_UpdateObjectHeader(obj, NULL, 1, 0, 0, NULL);
451
452                         YFREE(fixer);
453                 }
454         }
455
456         yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
457
458         if (alloc_failed)
459                 return YAFFS_FAIL;
460
461         T(YAFFS_TRACE_SCAN, (TSTR("yaffs1_Scan ends" TENDSTR)));
462
463
464         return YAFFS_OK;
465 }
466