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