-
- chunk = blk * dev->param.chunks_per_block + c;
-
- result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL,
- &tags);
-
- /* Let's have a good look at this chunk... */
-
- if (!tags.chunk_used) {
- /* An unassigned chunk in the block.
- * If there are used chunks after this one, then
- * it is a chunk that was skipped due to failing the erased
- * check. Just skip it so that it can be deleted.
- * But, more typically, We get here when this is an unallocated
- * chunk and his means that either the block is empty or
- * this is the one being allocated from
- */
-
- if (foundChunksInBlock) {
- /* This is a chunk that was skipped due to failing the erased check */
- } else if (c == 0) {
- /* We're looking at the first chunk in the block so the block is unused */
- state = YAFFS_BLOCK_STATE_EMPTY;
- dev->n_erased_blocks++;
- } else {
- if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
- state == YAFFS_BLOCK_STATE_ALLOCATING) {
- if (dev->seq_number == bi->seq_number) {
- /* this is the block being allocated from */
-
- T(YAFFS_TRACE_SCAN,
- (TSTR
- (" Allocating from %d %d"
- TENDSTR), blk, c));
-
- state = YAFFS_BLOCK_STATE_ALLOCATING;
- dev->alloc_block = blk;
- dev->alloc_page = c;
- dev->alloc_block_finder = blk;
- } else {
- /* This is a partially written block that is not
- * the current allocation block.
- */
-
- T(YAFFS_TRACE_SCAN,
- (TSTR("Partially written block %d detected" TENDSTR),
- blk));
- }
- }
- }
-
- dev->n_free_chunks++;
-
- } else if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED) {
- T(YAFFS_TRACE_SCAN,
- (TSTR(" Unfixed ECC in chunk(%d:%d), chunk ignored"TENDSTR),
- blk, c));
-
- dev->n_free_chunks++;
-
- } else if (tags.obj_id > YAFFS_MAX_OBJECT_ID ||
- tags.chunk_id > YAFFS_MAX_CHUNK_ID ||
- (tags.chunk_id > 0 && tags.n_bytes > dev->data_bytes_per_chunk) ||
- tags.seq_number != bi->seq_number ) {
- T(YAFFS_TRACE_SCAN,
- (TSTR("Chunk (%d:%d) with bad tags:obj = %d, chunk_id = %d, n_bytes = %d, ignored"TENDSTR),
- blk, c,tags.obj_id, tags.chunk_id, tags.n_bytes));
-
- dev->n_free_chunks++;
-
- } else if (tags.chunk_id > 0) {
- /* chunk_id > 0 so it is a data chunk... */
- unsigned int endpos;
- __u32 chunkBase =
- (tags.chunk_id - 1) * dev->data_bytes_per_chunk;
-
- foundChunksInBlock = 1;
-
-
- yaffs_set_chunk_bit(dev, blk, c);
- bi->pages_in_use++;
-
- in = yaffs_find_or_create_by_number(dev,
- tags.
- obj_id,
- YAFFS_OBJECT_TYPE_FILE);
- if (!in) {
- /* Out of memory */
- alloc_failed = 1;
- }
-
- if (in &&
- in->variant_type == YAFFS_OBJECT_TYPE_FILE
- && chunkBase < in->variant.file_variant.shrink_size) {
- /* This has not been invalidated by a resize */
- if (!yaffs_put_chunk_in_file(in, tags.chunk_id, chunk, -1)) {
- alloc_failed = 1;
- }
-
- /* File size is calculated by looking at the data chunks if we have not
- * seen an object header yet. Stop this practice once we find an object header.
- */
- endpos = chunkBase + tags.n_bytes;
-
- if (!in->valid && /* have not got an object header yet */
- in->variant.file_variant.scanned_size < endpos) {
- in->variant.file_variant.scanned_size = endpos;
- in->variant.file_variant.file_size = endpos;
- }
-
- } else if (in) {
- /* This chunk has been invalidated by a resize, or a past file deletion
- * so delete the chunk*/
- yaffs_chunk_del(dev, chunk, 1, __LINE__);
-
- }
- } else {
- /* chunk_id == 0, so it is an ObjectHeader.
- * Thus, we read in the object header and make the object
- */
- foundChunksInBlock = 1;
-
- yaffs_set_chunk_bit(dev, blk, c);
- bi->pages_in_use++;
-
- oh = NULL;
- in = NULL;
-
- if (tags.extra_available) {
- in = yaffs_find_or_create_by_number(dev,
- tags.obj_id,
- tags.extra_obj_type);
- if (!in)
- alloc_failed = 1;
- }
-
- if (!in ||
- (!in->valid && dev->param.disable_lazy_load) ||
- tags.extra_shadows ||
- (!in->valid &&
- (tags.obj_id == YAFFS_OBJECTID_ROOT ||
- tags.obj_id == YAFFS_OBJECTID_LOSTNFOUND))) {
-
- /* If we don't have valid info then we need to read the chunk
- * TODO In future we can probably defer reading the chunk and
- * living with invalid data until needed.
- */
-
- result = yaffs_rd_chunk_tags_nand(dev,
- chunk,
- chunkData,
- NULL);
-
- oh = (yaffs_obj_header *) chunkData;
-
- if (dev->param.inband_tags) {
- /* Fix up the header if they got corrupted by inband tags */
- oh->shadows_obj = oh->inband_shadowed_obj_id;
- oh->is_shrink = oh->inband_is_shrink;
- }
-
- if (!in) {
- in = yaffs_find_or_create_by_number(dev, tags.obj_id, oh->type);
- if (!in)
- alloc_failed = 1;
- }
-
- }
-
- if (!in) {
- /* TODO Hoosterman we have a problem! */
- T(YAFFS_TRACE_ERROR,
- (TSTR
- ("yaffs tragedy: Could not make object for object %d at chunk %d during scan"
- TENDSTR), tags.obj_id, chunk));
- continue;
- }
-
- if (in->valid) {
- /* We have already filled this one.
- * We have a duplicate that will be discarded, but
- * we first have to suck out resize info if it is a file.
- */
-
- if ((in->variant_type == YAFFS_OBJECT_TYPE_FILE) &&
- ((oh &&
- oh->type == YAFFS_OBJECT_TYPE_FILE) ||
- (tags.extra_available &&
- tags.extra_obj_type == YAFFS_OBJECT_TYPE_FILE))) {
- __u32 thisSize =
- (oh) ? oh->file_size : tags.
- extra_length;
- __u32 parent_obj_id =
- (oh) ? oh->
- parent_obj_id : tags.
- extra_parent_id;
-
-
- is_shrink =
- (oh) ? oh->is_shrink : tags.
- extra_is_shrink;
-
- /* If it is deleted (unlinked at start also means deleted)
- * we treat the file size as being zeroed at this point.
- */
- if (parent_obj_id ==
- YAFFS_OBJECTID_DELETED
- || parent_obj_id ==
- YAFFS_OBJECTID_UNLINKED) {
- thisSize = 0;
- is_shrink = 1;
- }
-
- if (is_shrink && in->variant.file_variant.shrink_size > thisSize)
- in->variant.file_variant.shrink_size = thisSize;
-
- if (is_shrink)
- bi->has_shrink_hdr = 1;
-
- }
- /* Use existing - destroy this one. */
- yaffs_chunk_del(dev, chunk, 1, __LINE__);
-
- }
-
- if (!in->valid && in->variant_type !=
- (oh ? oh->type : tags.extra_obj_type))
- T(YAFFS_TRACE_ERROR, (
- TSTR("yaffs tragedy: Bad object type, "
- TCONT("%d != %d, for object %d at chunk ")
- TCONT("%d during scan")
- TENDSTR), oh ?
- oh->type : tags.extra_obj_type,
- in->variant_type, tags.obj_id,
- chunk));
-
- if (!in->valid &&
- (tags.obj_id == YAFFS_OBJECTID_ROOT ||
- tags.obj_id ==
- YAFFS_OBJECTID_LOSTNFOUND)) {
- /* We only load some info, don't fiddle with directory structure */
- in->valid = 1;
-
- if (oh) {
-
- in->yst_mode = oh->yst_mode;
-#ifdef CONFIG_YAFFS_WINCE
- in->win_atime[0] = oh->win_atime[0];
- in->win_ctime[0] = oh->win_ctime[0];
- in->win_mtime[0] = oh->win_mtime[0];
- in->win_atime[1] = oh->win_atime[1];
- in->win_ctime[1] = oh->win_ctime[1];
- in->win_mtime[1] = oh->win_mtime[1];
-#else
- in->yst_uid = oh->yst_uid;
- in->yst_gid = oh->yst_gid;
- in->yst_atime = oh->yst_atime;
- in->yst_mtime = oh->yst_mtime;
- in->yst_ctime = oh->yst_ctime;
- in->yst_rdev = oh->yst_rdev;
-
- in->lazy_loaded = 0;
-
-#endif
- } else
- in->lazy_loaded = 1;
-
- in->hdr_chunk = chunk;
-
- } else if (!in->valid) {
- /* we need to load this info */
-
- in->valid = 1;
- in->hdr_chunk = chunk;
-
- if (oh) {
- in->variant_type = oh->type;
-
- in->yst_mode = oh->yst_mode;
-#ifdef CONFIG_YAFFS_WINCE
- in->win_atime[0] = oh->win_atime[0];
- in->win_ctime[0] = oh->win_ctime[0];
- in->win_mtime[0] = oh->win_mtime[0];
- in->win_atime[1] = oh->win_atime[1];
- in->win_ctime[1] = oh->win_ctime[1];
- in->win_mtime[1] = oh->win_mtime[1];
-#else
- in->yst_uid = oh->yst_uid;
- in->yst_gid = oh->yst_gid;
- in->yst_atime = oh->yst_atime;
- in->yst_mtime = oh->yst_mtime;
- in->yst_ctime = oh->yst_ctime;
- in->yst_rdev = oh->yst_rdev;
-#endif
-
- if (oh->shadows_obj > 0)
- yaffs_handle_shadowed_obj(dev,
- oh->
- shadows_obj,
- 1);
-
-
-
- yaffs_set_obj_name_from_oh(in, oh);
- parent =
- yaffs_find_or_create_by_number
- (dev, oh->parent_obj_id,
- YAFFS_OBJECT_TYPE_DIRECTORY);
-
- file_size = oh->file_size;
- is_shrink = oh->is_shrink;
- equiv_id = oh->equiv_id;
-
- } else {
- in->variant_type = tags.extra_obj_type;
- parent =
- yaffs_find_or_create_by_number
- (dev, tags.extra_parent_id,
- YAFFS_OBJECT_TYPE_DIRECTORY);
- file_size = tags.extra_length;
- is_shrink = tags.extra_is_shrink;
- equiv_id = tags.extra_equiv_id;
- in->lazy_loaded = 1;
-
- }
- in->dirty = 0;
-
- if (!parent)
- alloc_failed = 1;
-
- /* directory stuff...
- * hook up to parent
- */
-
- if (parent && parent->variant_type ==
- YAFFS_OBJECT_TYPE_UNKNOWN) {
- /* Set up as a directory */
- parent->variant_type =
- YAFFS_OBJECT_TYPE_DIRECTORY;
- YINIT_LIST_HEAD(&parent->variant.
- dir_variant.
- children);
- } else if (!parent || parent->variant_type !=
- YAFFS_OBJECT_TYPE_DIRECTORY) {
- /* Hoosterman, another problem....
- * We're trying to use a non-directory as a directory
- */
-
- T(YAFFS_TRACE_ERROR,
- (TSTR
- ("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
- TENDSTR)));
- parent = dev->lost_n_found;
- }
-
- yaffs_add_obj_to_dir(parent, in);
-
- itsUnlinked = (parent == dev->del_dir) ||
- (parent == dev->unlinked_dir);
-
- if (is_shrink) {
- /* Mark the block as having a shrinkHeader */
- bi->has_shrink_hdr = 1;
- }
-
- /* Note re hardlinks.
- * Since we might scan a hardlink before its equivalent object is scanned
- * we put them all in a list.
- * After scanning is complete, we should have all the objects, so we run
- * through this list and fix up all the chains.
- */
-
- switch (in->variant_type) {
- case YAFFS_OBJECT_TYPE_UNKNOWN:
- /* Todo got a problem */
- break;
- case YAFFS_OBJECT_TYPE_FILE:
-
- if (in->variant.file_variant.
- scanned_size < file_size) {
- /* This covers the case where the file size is greater
- * than where the data is
- * This will happen if the file is resized to be larger
- * than its current data extents.
- */
- in->variant.file_variant.file_size = file_size;
- in->variant.file_variant.scanned_size = file_size;
- }
-
- if (in->variant.file_variant.shrink_size > file_size)
- in->variant.file_variant.shrink_size = file_size;
-
-
- break;
- case YAFFS_OBJECT_TYPE_HARDLINK:
- if (!itsUnlinked) {
- in->variant.hardlink_variant.equiv_id =
- equiv_id;
- in->hard_links.next =
- (struct ylist_head *) hard_list;
- hard_list = in;
- }
- break;
- case YAFFS_OBJECT_TYPE_DIRECTORY:
- /* Do nothing */
- break;
- case YAFFS_OBJECT_TYPE_SPECIAL:
- /* Do nothing */
- break;
- case YAFFS_OBJECT_TYPE_SYMLINK:
- if (oh) {
- in->variant.symlink_variant.alias =
- yaffs_clone_str(oh->alias);
- if (!in->variant.symlink_variant.alias)
- alloc_failed = 1;
- }
- break;
- }
-
- }
-
- }
-
- } /* End of scanning for each chunk */
-
- if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
- /* If we got this far while scanning, then the block is fully allocated. */
- state = YAFFS_BLOCK_STATE_FULL;