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_yaffs1.h"
16 #include "yaffs_trace.h"
17 #include "yaffs_bitmap.h"
18 #include "yaffs_getblockinfo.h"
19 #include "yaffs_nand.h"
22 int yaffs1_Scan(yaffs_Device *dev)
24 yaffs_ExtendedTags tags;
34 yaffs_BlockState state;
35 yaffs_Object *hardList = NULL;
38 yaffs_ObjectHeader *oh;
44 struct yaffs_ShadowFixerStruct *shadowFixerList = NULL;
52 (TSTR("yaffs1_Scan starts intstartblk %d intendblk %d..." TENDSTR),
53 dev->internalStartBlock, dev->internalEndBlock));
55 chunkData = yaffs_GetTempBuffer(dev, __LINE__);
57 dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
59 /* Scan all the blocks to determine their state */
61 for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
62 yaffs_ClearChunkBits(dev, blk);
64 bi->softDeletions = 0;
66 yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
68 bi->blockState = state;
69 bi->sequenceNumber = sequenceNumber;
71 if (bi->sequenceNumber == YAFFS_SEQUENCE_BAD_BLOCK)
72 bi->blockState = state = YAFFS_BLOCK_STATE_DEAD;
74 T(YAFFS_TRACE_SCAN_DEBUG,
75 (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
76 state, sequenceNumber));
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)));
85 dev->nFreeChunks += dev->param.nChunksPerBlock;
90 startIterator = dev->internalStartBlock;
91 endIterator = dev->internalEndBlock;
93 /* For each block.... */
94 for (blockIterator = startIterator; !alloc_failed && blockIterator <= endIterator;
103 bi = yaffs_GetBlockInfo(dev, blk);
104 state = bi->blockState;
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;
114 result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
117 /* Let's have a good look at this chunk... */
119 if (tags.eccResult == YAFFS_ECC_RESULT_UNFIXED || tags.chunkDeleted) {
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
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++;
137 /* this is the block being allocated from */
140 (" Allocating from %d %d" TENDSTR),
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. */
150 dev->nFreeChunks += (dev->param.nChunksPerBlock - c);
151 } else if (tags.chunkId > 0) {
152 /* chunkId > 0 so it is a data chunk... */
155 yaffs_SetChunkBit(dev, blk, c);
158 in = yaffs_FindOrCreateObjectByNumber(dev,
161 YAFFS_OBJECT_TYPE_FILE);
162 /* PutChunkIntoFile checks for a clash (two data chunks with
170 if (!yaffs_PutChunkIntoFile(in, tags.chunkId, chunk, 1))
175 (tags.chunkId - 1) * dev->nDataBytesPerChunk +
178 in->variantType == YAFFS_OBJECT_TYPE_FILE
179 && in->variant.fileVariant.scannedFileSize <
181 in->variant.fileVariant.
182 scannedFileSize = endpos;
183 if (!dev->param.useHeaderFileSize) {
184 in->variant.fileVariant.
186 in->variant.fileVariant.
191 /* T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId)); */
193 /* chunkId == 0, so it is an ObjectHeader.
194 * Thus, we read in the object header and make the object
196 yaffs_SetChunkBit(dev, blk, c);
199 result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk,
203 oh = (yaffs_ObjectHeader *) chunkData;
205 in = yaffs_FindObjectByNumber(dev,
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.
213 yaffs_DeleteObject(in);
218 in = yaffs_FindOrCreateObjectByNumber(dev,
226 if (in && oh->shadowsObject > 0) {
228 struct yaffs_ShadowFixerStruct *fixer;
229 fixer = YMALLOC(sizeof(struct yaffs_ShadowFixerStruct));
231 fixer->next = shadowFixerList;
232 shadowFixerList = fixer;
233 fixer->objectId = tags.objectId;
234 fixer->shadowedId = oh->shadowsObject;
237 (" Shadow fixer: %d shadows %d" TENDSTR),
238 fixer->objectId, fixer->shadowedId));
244 if (in && in->valid) {
245 /* We have already filled this one. We have a duplicate and need to resolve it. */
247 unsigned existingSerial = in->serial;
248 unsigned newSerial = tags.serialNumber;
250 if (((existingSerial + 1) & 3) == newSerial) {
251 /* Use new one - destroy the exisiting one */
252 yaffs_DeleteChunk(dev,
257 /* Use existing - destroy this one. */
258 yaffs_DeleteChunk(dev, chunk, 1,
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 */
268 in->variantType = oh->type;
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];
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;
286 in->hdrChunk = chunk;
287 in->serial = tags.serialNumber;
289 } else if (in && !in->valid) {
290 /* we need to load this info */
293 in->variantType = oh->type;
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];
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;
311 in->hdrChunk = chunk;
312 in->serial = tags.serialNumber;
314 yaffs_SetObjectNameFromOH(in, oh);
317 /* directory stuff...
322 yaffs_FindOrCreateObjectByNumber
323 (dev, oh->parentObjectId,
324 YAFFS_OBJECT_TYPE_DIRECTORY);
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.
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
343 ("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
345 parent = dev->lostNFoundDir;
348 yaffs_AddObjectToDirectory(parent, in);
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++;
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.
362 switch (in->variantType) {
363 case YAFFS_OBJECT_TYPE_UNKNOWN:
364 /* Todo got a problem */
366 case YAFFS_OBJECT_TYPE_FILE:
367 if (dev->param.useHeaderFileSize)
369 in->variant.fileVariant.
374 case YAFFS_OBJECT_TYPE_HARDLINK:
375 in->variant.hardLinkVariant.
377 oh->equivalentObjectId;
379 (struct ylist_head *)
383 case YAFFS_OBJECT_TYPE_DIRECTORY:
386 case YAFFS_OBJECT_TYPE_SPECIAL:
389 case YAFFS_OBJECT_TYPE_SYMLINK:
390 in->variant.symLinkVariant.alias =
391 yaffs_CloneString(oh->alias);
392 if (!in->variant.symLinkVariant.alias)
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;
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;
412 bi->blockState = state;
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);
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
430 yaffs_HardlinkFixup(dev, hardList);
432 /* Fix up any shadowed objects */
434 struct yaffs_ShadowFixerStruct *fixer;
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.
443 obj = yaffs_FindObjectByNumber(dev, fixer->shadowedId);
445 yaffs_DeleteObject(obj);
447 obj = yaffs_FindObjectByNumber(dev, fixer->objectId);
450 yaffs_UpdateObjectHeader(obj, NULL, 1, 0, 0, NULL);
456 yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
461 T(YAFFS_TRACE_SCAN, (TSTR("yaffs1_Scan ends" TENDSTR)));