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.
13 #include "yaffs_yaffs1.h"
15 #include "yaffs_trace.h"
16 #include "yaffs_bitmap.h"
17 #include "yaffs_getblockinfo.h"
18 #include "yaffs_nand.h"
21 int yaffs1_scan(yaffs_Device *dev)
23 yaffs_ExtendedTags tags;
33 yaffs_BlockState state;
34 yaffs_Object *hardList = NULL;
37 yaffs_ObjectHeader *oh;
43 struct yaffs_ShadowFixerStruct *shadowFixerList = NULL;
51 (TSTR("yaffs1_scan starts intstartblk %d intendblk %d..." TENDSTR),
52 dev->internalStartBlock, dev->internalEndBlock));
54 chunkData = yaffs_get_temp_buffer(dev, __LINE__);
56 dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
58 /* Scan all the blocks to determine their state */
60 for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
61 yaffs_clear_chunk_bits(dev, blk);
63 bi->softDeletions = 0;
65 yaffs_query_init_block_state(dev, blk, &state, &sequenceNumber);
67 bi->blockState = state;
68 bi->sequenceNumber = sequenceNumber;
70 if (bi->sequenceNumber == YAFFS_SEQUENCE_BAD_BLOCK)
71 bi->blockState = state = YAFFS_BLOCK_STATE_DEAD;
73 T(YAFFS_TRACE_SCAN_DEBUG,
74 (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
75 state, sequenceNumber));
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)));
84 dev->nFreeChunks += dev->param.nChunksPerBlock;
89 startIterator = dev->internalStartBlock;
90 endIterator = dev->internalEndBlock;
92 /* For each block.... */
93 for (blockIterator = startIterator; !alloc_failed && blockIterator <= endIterator;
102 bi = yaffs_get_block_info(dev, blk);
103 state = bi->blockState;
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;
113 result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL,
116 /* Let's have a good look at this chunk... */
118 if (tags.eccResult == YAFFS_ECC_RESULT_UNFIXED || tags.chunkDeleted) {
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
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++;
136 /* this is the block being allocated from */
139 (" Allocating from %d %d" TENDSTR),
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. */
149 dev->nFreeChunks += (dev->param.nChunksPerBlock - c);
150 } else if (tags.chunkId > 0) {
151 /* chunkId > 0 so it is a data chunk... */
154 yaffs_set_chunk_bit(dev, blk, c);
157 in = yaffs_find_or_create_by_number(dev,
160 YAFFS_OBJECT_TYPE_FILE);
161 /* PutChunkIntoFile checks for a clash (two data chunks with
169 if (!yaffs_put_chunk_in_file(in, tags.chunkId, chunk, 1))
174 (tags.chunkId - 1) * dev->nDataBytesPerChunk +
177 in->variantType == YAFFS_OBJECT_TYPE_FILE
178 && in->variant.fileVariant.scannedFileSize <
180 in->variant.fileVariant.
181 scannedFileSize = endpos;
182 if (!dev->param.useHeaderFileSize) {
183 in->variant.fileVariant.
185 in->variant.fileVariant.
190 /* T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId)); */
192 /* chunkId == 0, so it is an ObjectHeader.
193 * Thus, we read in the object header and make the object
195 yaffs_set_chunk_bit(dev, blk, c);
198 result = yaffs_rd_chunk_tags_nand(dev, chunk,
202 oh = (yaffs_ObjectHeader *) chunkData;
204 in = yaffs_find_by_number(dev,
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.
217 in = yaffs_find_or_create_by_number(dev,
225 if (in && oh->shadowsObject > 0) {
227 struct yaffs_ShadowFixerStruct *fixer;
228 fixer = YMALLOC(sizeof(struct yaffs_ShadowFixerStruct));
230 fixer->next = shadowFixerList;
231 shadowFixerList = fixer;
232 fixer->objectId = tags.objectId;
233 fixer->shadowedId = oh->shadowsObject;
236 (" Shadow fixer: %d shadows %d" TENDSTR),
237 fixer->objectId, fixer->shadowedId));
243 if (in && in->valid) {
244 /* We have already filled this one. We have a duplicate and need to resolve it. */
246 unsigned existingSerial = in->serial;
247 unsigned newSerial = tags.serialNumber;
249 if (((existingSerial + 1) & 3) == newSerial) {
250 /* Use new one - destroy the exisiting one */
256 /* Use existing - destroy this one. */
257 yaffs_chunk_del(dev, chunk, 1,
262 if (in && !in->valid &&
263 (tags.objectId == YAFFS_OBJECTID_ROOT ||
264 tags.objectId == YAFFS_OBJECTID_LOSTNFOUND)) {
265 /* We only load some info, don't fiddle with directory structure */
267 in->variantType = oh->type;
269 in->yst_mode = oh->yst_mode;
270 #ifdef CONFIG_YAFFS_WINCE
271 in->win_atime[0] = oh->win_atime[0];
272 in->win_ctime[0] = oh->win_ctime[0];
273 in->win_mtime[0] = oh->win_mtime[0];
274 in->win_atime[1] = oh->win_atime[1];
275 in->win_ctime[1] = oh->win_ctime[1];
276 in->win_mtime[1] = oh->win_mtime[1];
278 in->yst_uid = oh->yst_uid;
279 in->yst_gid = oh->yst_gid;
280 in->yst_atime = oh->yst_atime;
281 in->yst_mtime = oh->yst_mtime;
282 in->yst_ctime = oh->yst_ctime;
283 in->yst_rdev = oh->yst_rdev;
285 in->hdrChunk = chunk;
286 in->serial = tags.serialNumber;
288 } else if (in && !in->valid) {
289 /* we need to load this info */
292 in->variantType = oh->type;
294 in->yst_mode = oh->yst_mode;
295 #ifdef CONFIG_YAFFS_WINCE
296 in->win_atime[0] = oh->win_atime[0];
297 in->win_ctime[0] = oh->win_ctime[0];
298 in->win_mtime[0] = oh->win_mtime[0];
299 in->win_atime[1] = oh->win_atime[1];
300 in->win_ctime[1] = oh->win_ctime[1];
301 in->win_mtime[1] = oh->win_mtime[1];
303 in->yst_uid = oh->yst_uid;
304 in->yst_gid = oh->yst_gid;
305 in->yst_atime = oh->yst_atime;
306 in->yst_mtime = oh->yst_mtime;
307 in->yst_ctime = oh->yst_ctime;
308 in->yst_rdev = oh->yst_rdev;
310 in->hdrChunk = chunk;
311 in->serial = tags.serialNumber;
313 yaffs_set_obj_name_from_oh(in, oh);
316 /* directory stuff...
321 yaffs_find_or_create_by_number
322 (dev, oh->parentObjectId,
323 YAFFS_OBJECT_TYPE_DIRECTORY);
326 if (parent && parent->variantType ==
327 YAFFS_OBJECT_TYPE_UNKNOWN) {
328 /* Set up as a directory */
329 parent->variantType =
330 YAFFS_OBJECT_TYPE_DIRECTORY;
331 YINIT_LIST_HEAD(&parent->variant.
334 } else if (!parent || parent->variantType !=
335 YAFFS_OBJECT_TYPE_DIRECTORY) {
336 /* Hoosterman, another problem....
337 * We're trying to use a non-directory as a directory
342 ("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
344 parent = dev->lostNFoundDir;
347 yaffs_add_obj_to_dir(parent, in);
349 if (0 && (parent == dev->deletedDir ||
350 parent == dev->unlinkedDir)) {
351 in->deleted = 1; /* If it is unlinked at start up then it wants deleting */
352 dev->nDeletedFiles++;
354 /* Note re hardlinks.
355 * Since we might scan a hardlink before its equivalent object is scanned
356 * we put them all in a list.
357 * After scanning is complete, we should have all the objects, so we run through this
358 * list and fix up all the chains.
361 switch (in->variantType) {
362 case YAFFS_OBJECT_TYPE_UNKNOWN:
363 /* Todo got a problem */
365 case YAFFS_OBJECT_TYPE_FILE:
366 if (dev->param.useHeaderFileSize)
368 in->variant.fileVariant.
373 case YAFFS_OBJECT_TYPE_HARDLINK:
374 in->variant.hardLinkVariant.
376 oh->equivalentObjectId;
378 (struct ylist_head *)
382 case YAFFS_OBJECT_TYPE_DIRECTORY:
385 case YAFFS_OBJECT_TYPE_SPECIAL:
388 case YAFFS_OBJECT_TYPE_SYMLINK:
389 in->variant.symLinkVariant.alias =
390 yaffs_clone_str(oh->alias);
391 if (!in->variant.symLinkVariant.alias)
400 if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
401 /* If we got this far while scanning, then the block is fully allocated.*/
402 state = YAFFS_BLOCK_STATE_FULL;
405 if (state == YAFFS_BLOCK_STATE_ALLOCATING) {
406 /* If the block was partially allocated then treat it as fully allocated.*/
407 state = YAFFS_BLOCK_STATE_FULL;
408 dev->allocationBlock = -1;
411 bi->blockState = state;
413 /* Now let's see if it was dirty */
414 if (bi->pagesInUse == 0 &&
415 !bi->hasShrinkHeader &&
416 bi->blockState == YAFFS_BLOCK_STATE_FULL) {
417 yaffs_block_became_dirty(dev, blk);
423 /* Ok, we've done all the scanning.
424 * Fix up the hard link chains.
425 * We should now have scanned all the objects, now it's time to add these
429 yaffs_link_fixup(dev, hardList);
431 /* Fix up any shadowed objects */
433 struct yaffs_ShadowFixerStruct *fixer;
436 while (shadowFixerList) {
437 fixer = shadowFixerList;
438 shadowFixerList = fixer->next;
439 /* Complete the rename transaction by deleting the shadowed object
440 * then setting the object header to unshadowed.
442 obj = yaffs_find_by_number(dev, fixer->shadowedId);
446 obj = yaffs_find_by_number(dev, fixer->objectId);
449 yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
455 yaffs_release_temp_buffer(dev, chunkData, __LINE__);
460 T(YAFFS_TRACE_SCAN, (TSTR("yaffs1_scan ends" TENDSTR)));