X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs2.git;a=blobdiff_plain;f=yaffs_yaffs1.c;h=d277e20e2a554cee97d28e91430ea24e839d4775;hp=bcfd594b046ccbff3cd225d98d3ded7930a56dc5;hb=cabb7b51f48a784fbd8bd04ea8878abe2196167a;hpb=780469359d1051ab37ab281b0520137d73fee2e9 diff --git a/yaffs_yaffs1.c b/yaffs_yaffs1.c index bcfd594..d277e20 100644 --- a/yaffs_yaffs1.c +++ b/yaffs_yaffs1.c @@ -1,7 +1,7 @@ /* * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. * - * Copyright (C) 2002-2010 Aleph One Ltd. + * Copyright (C) 2002-2011 Aleph One Ltd. * for Toby Churchill Ltd and Brightstar Engineering * * Created by Charles Manning @@ -10,302 +10,276 @@ * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. */ + #include "yaffs_yaffs1.h" #include "yportenv.h" #include "yaffs_trace.h" #include "yaffs_bitmap.h" #include "yaffs_getblockinfo.h" #include "yaffs_nand.h" +#include "yaffs_attribs.h" - -int yaffs1_Scan(yaffs_Device *dev) +int yaffs1_scan(struct yaffs_dev *dev) { - yaffs_ExtendedTags tags; + struct yaffs_ext_tags tags; int blk; - int blockIterator; - int startIterator; - int endIterator; int result; - int chunk; int c; int deleted; - yaffs_BlockState state; - yaffs_Object *hardList = NULL; - yaffs_BlockInfo *bi; - __u32 sequenceNumber; - yaffs_ObjectHeader *oh; - yaffs_Object *in; - yaffs_Object *parent; - + enum yaffs_block_state state; + LIST_HEAD(hard_list); + struct yaffs_block_info *bi; + u32 seq_number; + struct yaffs_obj_hdr *oh; + struct yaffs_obj *in; + struct yaffs_obj *parent; int alloc_failed = 0; + struct yaffs_shadow_fixer *shadow_fixers = NULL; + u8 *chunk_data; - struct yaffs_ShadowFixerStruct *shadowFixerList = NULL; - - - __u8 *chunkData; - - + yaffs_trace(YAFFS_TRACE_SCAN, + "yaffs1_scan starts intstartblk %d intendblk %d...", + dev->internal_start_block, dev->internal_end_block); - T(YAFFS_TRACE_SCAN, - (TSTR("yaffs1_Scan starts intstartblk %d intendblk %d..." TENDSTR), - dev->internalStartBlock, dev->internalEndBlock)); + chunk_data = yaffs_get_temp_buffer(dev); - chunkData = yaffs_GetTempBuffer(dev, __LINE__); - - dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER; + dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER; /* Scan all the blocks to determine their state */ - bi = dev->blockInfo; - for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) { - yaffs_ClearChunkBits(dev, blk); - bi->pagesInUse = 0; - bi->softDeletions = 0; + bi = dev->block_info; + for (blk = dev->internal_start_block; blk <= dev->internal_end_block; + blk++) { + yaffs_clear_chunk_bits(dev, blk); + bi->pages_in_use = 0; + bi->soft_del_pages = 0; - yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber); + yaffs_query_init_block_state(dev, blk, &state, &seq_number); - bi->blockState = state; - bi->sequenceNumber = sequenceNumber; + bi->block_state = state; + bi->seq_number = seq_number; - if (bi->sequenceNumber == YAFFS_SEQUENCE_BAD_BLOCK) - bi->blockState = state = YAFFS_BLOCK_STATE_DEAD; + if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK) + bi->block_state = state = YAFFS_BLOCK_STATE_DEAD; - T(YAFFS_TRACE_SCAN_DEBUG, - (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk, - state, sequenceNumber)); + yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, + "Block scanning block %d state %d seq %d", + blk, state, seq_number); if (state == YAFFS_BLOCK_STATE_DEAD) { - T(YAFFS_TRACE_BAD_BLOCKS, - (TSTR("block %d is bad" TENDSTR), blk)); + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, + "block %d is bad", blk); } else if (state == YAFFS_BLOCK_STATE_EMPTY) { - T(YAFFS_TRACE_SCAN_DEBUG, - (TSTR("Block empty " TENDSTR))); - dev->nErasedBlocks++; - dev->nFreeChunks += dev->param.nChunksPerBlock; + yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "Block empty "); + dev->n_erased_blocks++; + dev->n_free_chunks += dev->param.chunks_per_block; } bi++; } - startIterator = dev->internalStartBlock; - endIterator = dev->internalEndBlock; - /* For each block.... */ - for (blockIterator = startIterator; !alloc_failed && blockIterator <= endIterator; - blockIterator++) { - - YYIELD(); - - YYIELD(); + for (blk = dev->internal_start_block; + !alloc_failed && blk <= dev->internal_end_block; blk++) { - blk = blockIterator; + cond_resched(); - bi = yaffs_GetBlockInfo(dev, blk); - state = bi->blockState; + bi = yaffs_get_block_info(dev, blk); + state = bi->block_state; deleted = 0; - /* For each chunk in each block that needs scanning....*/ - for (c = 0; !alloc_failed && c < dev->param.nChunksPerBlock && - state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) { + /* For each chunk in each block that needs scanning.... */ + for (c = 0; + !alloc_failed && c < dev->param.chunks_per_block && + state == YAFFS_BLOCK_STATE_NEEDS_SCAN; c++) { /* Read the tags and decide what to do */ - chunk = blk * dev->param.nChunksPerBlock + c; + chunk = blk * dev->param.chunks_per_block + c; - result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL, - &tags); + result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL, + &tags); /* Let's have a good look at this chunk... */ - if (tags.eccResult == YAFFS_ECC_RESULT_UNFIXED || tags.chunkDeleted) { + if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED || + tags.is_deleted) { /* YAFFS1 only... * A deleted chunk */ deleted++; - dev->nFreeChunks++; - /*T((" %d %d deleted\n",blk,c)); */ - } else if (!tags.chunkUsed) { + dev->n_free_chunks++; + } else if (!tags.chunk_used) { /* An unassigned chunk in the block * This means that either the block is empty or * this is the one being allocated from */ if (c == 0) { - /* We're looking at the first chunk in the block so the block is unused */ + /* We're looking at the first chunk in + *the block so the block is unused */ state = YAFFS_BLOCK_STATE_EMPTY; - dev->nErasedBlocks++; + dev->n_erased_blocks++; } else { - /* this is the block being allocated from */ - T(YAFFS_TRACE_SCAN, - (TSTR - (" Allocating from %d %d" TENDSTR), - blk, c)); + /* this is the block being allocated */ + yaffs_trace(YAFFS_TRACE_SCAN, + " Allocating from %d %d", + blk, c); state = YAFFS_BLOCK_STATE_ALLOCATING; - dev->allocationBlock = blk; - dev->allocationPage = c; - dev->allocationBlockFinder = blk; - /* Set block finder here to encourage the allocator to go forth from here. */ + dev->alloc_block = blk; + dev->alloc_page = c; + dev->alloc_block_finder = blk; } - dev->nFreeChunks += (dev->param.nChunksPerBlock - c); - } else if (tags.chunkId > 0) { - /* chunkId > 0 so it is a data chunk... */ + dev->n_free_chunks += + (dev->param.chunks_per_block - c); + } else if (tags.chunk_id > 0) { + /* chunk_id > 0 so it is a data chunk... */ unsigned int endpos; - yaffs_SetChunkBit(dev, blk, c); - bi->pagesInUse++; + yaffs_set_chunk_bit(dev, blk, c); + bi->pages_in_use++; - in = yaffs_FindOrCreateObjectByNumber(dev, - tags. - objectId, - YAFFS_OBJECT_TYPE_FILE); - /* PutChunkIntoFile checks for a clash (two data chunks with - * the same chunkId). + in = yaffs_find_or_create_by_number(dev, + tags.obj_id, + YAFFS_OBJECT_TYPE_FILE); + /* PutChunkIntoFile checks for a clash + * (two data chunks with the same chunk_id). */ if (!in) alloc_failed = 1; if (in) { - if (!yaffs_PutChunkIntoFile(in, tags.chunkId, chunk, 1)) + if (!yaffs_put_chunk_in_file + (in, tags.chunk_id, chunk, 1)) alloc_failed = 1; } endpos = - (tags.chunkId - 1) * dev->nDataBytesPerChunk + - tags.byteCount; + (tags.chunk_id - 1) * + dev->data_bytes_per_chunk + + tags.n_bytes; if (in && - in->variantType == YAFFS_OBJECT_TYPE_FILE - && in->variant.fileVariant.scannedFileSize < - endpos) { - in->variant.fileVariant. - scannedFileSize = endpos; - if (!dev->param.useHeaderFileSize) { - in->variant.fileVariant. - fileSize = - in->variant.fileVariant. - scannedFileSize; + in->variant_type == + YAFFS_OBJECT_TYPE_FILE && + in->variant.file_variant.scanned_size < + endpos) { + in->variant.file_variant.scanned_size = + endpos; + if (!dev->param.use_header_file_size) { + in->variant. + file_variant.file_size = + in->variant. + file_variant.scanned_size; } } - /* T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId)); */ } else { - /* chunkId == 0, so it is an ObjectHeader. - * Thus, we read in the object header and make the object + /* chunk_id == 0, so it is an ObjectHeader. + * Make the object */ - yaffs_SetChunkBit(dev, blk, c); - bi->pagesInUse++; + yaffs_set_chunk_bit(dev, blk, c); + bi->pages_in_use++; - result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, - chunkData, - NULL); + result = yaffs_rd_chunk_tags_nand(dev, chunk, + chunk_data, + NULL); - oh = (yaffs_ObjectHeader *) chunkData; + oh = (struct yaffs_obj_hdr *)chunk_data; - in = yaffs_FindObjectByNumber(dev, - tags.objectId); - if (in && in->variantType != oh->type) { + in = yaffs_find_by_number(dev, tags.obj_id); + if (in && in->variant_type != oh->type) { /* This should not happen, but somehow - * Wev'e ended up with an objectId that has been reused but not yet - * deleted, and worse still it has changed type. Delete the old object. + * Wev'e ended up with an obj_id that + * has been reused but not yet deleted, + * and worse still it has changed type. + * Delete the old object. */ - yaffs_DeleteObject(in); - - in = 0; + yaffs_del_obj(in); + in = NULL; } - in = yaffs_FindOrCreateObjectByNumber(dev, - tags. - objectId, - oh->type); + in = yaffs_find_or_create_by_number(dev, + tags.obj_id, + oh->type); if (!in) alloc_failed = 1; - if (in && oh->shadowsObject > 0) { + if (in && oh->shadows_obj > 0) { - struct yaffs_ShadowFixerStruct *fixer; - fixer = YMALLOC(sizeof(struct yaffs_ShadowFixerStruct)); + struct yaffs_shadow_fixer *fixer; + fixer = + kmalloc(sizeof + (struct yaffs_shadow_fixer), + GFP_NOFS); if (fixer) { - fixer->next = shadowFixerList; - shadowFixerList = fixer; - fixer->objectId = tags.objectId; - fixer->shadowedId = oh->shadowsObject; + fixer->next = shadow_fixers; + shadow_fixers = fixer; + fixer->obj_id = tags.obj_id; + fixer->shadowed_id = + oh->shadows_obj; + yaffs_trace(YAFFS_TRACE_SCAN, + " Shadow fixer: %d shadows %d", + fixer->obj_id, + fixer->shadowed_id); + } } if (in && in->valid) { - /* We have already filled this one. We have a duplicate and need to resolve it. */ - - unsigned existingSerial = in->serial; - unsigned newSerial = tags.serialNumber; - - if (((existingSerial + 1) & 3) == newSerial) { - /* Use new one - destroy the exisiting one */ - yaffs_DeleteChunk(dev, - in->hdrChunk, - 1, __LINE__); + /* We have already filled this one. + * We have a duplicate and need to + * resolve it. */ + + unsigned existing_serial = in->serial; + unsigned new_serial = + tags.serial_number; + + if (((existing_serial + 1) & 3) == + new_serial) { + /* Use new one - destroy the + * exisiting one */ + yaffs_chunk_del(dev, + in->hdr_chunk, + 1, __LINE__); in->valid = 0; } else { - /* Use existing - destroy this one. */ - yaffs_DeleteChunk(dev, chunk, 1, - __LINE__); + /* Use existing - destroy + * this one. */ + yaffs_chunk_del(dev, chunk, 1, + __LINE__); } } if (in && !in->valid && - (tags.objectId == YAFFS_OBJECTID_ROOT || - tags.objectId == YAFFS_OBJECTID_LOSTNFOUND)) { - /* We only load some info, don't fiddle with directory structure */ + (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; - in->variantType = oh->type; + 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 - in->hdrChunk = chunk; - in->serial = tags.serialNumber; + yaffs_load_attribs(in, oh); + in->hdr_chunk = chunk; + in->serial = tags.serial_number; } else if (in && !in->valid) { /* we need to load this info */ in->valid = 1; - in->variantType = oh->type; + 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 - in->hdrChunk = chunk; - in->serial = tags.serialNumber; - - yaffs_SetObjectName(in, oh->name); + yaffs_load_attribs(in, oh); + in->hdr_chunk = chunk; + in->serial = tags.serial_number; + + yaffs_set_obj_name_from_oh(in, oh); in->dirty = 0; /* directory stuff... @@ -313,66 +287,52 @@ int yaffs1_Scan(yaffs_Device *dev) */ parent = - yaffs_FindOrCreateObjectByNumber - (dev, oh->parentObjectId, + yaffs_find_or_create_by_number + (dev, oh->parent_obj_id, YAFFS_OBJECT_TYPE_DIRECTORY); if (!parent) alloc_failed = 1; - if (parent && parent->variantType == + if (parent && parent->variant_type == YAFFS_OBJECT_TYPE_UNKNOWN) { /* Set up as a directory */ - parent->variantType = - YAFFS_OBJECT_TYPE_DIRECTORY; - YINIT_LIST_HEAD(&parent->variant. - directoryVariant. - children); - } else if (!parent || parent->variantType != - YAFFS_OBJECT_TYPE_DIRECTORY) { - /* Hoosterman, another problem.... - * We're trying to use a non-directory as a directory + parent->variant_type = + YAFFS_OBJECT_TYPE_DIRECTORY; + INIT_LIST_HEAD(&parent-> + variant.dir_variant. + children); + } else if (!parent || + parent->variant_type != + YAFFS_OBJECT_TYPE_DIRECTORY) { + /* Hoosterman, a 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->lostNFoundDir; + yaffs_trace(YAFFS_TRACE_ERROR, + "yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found." + ); + parent = dev->lost_n_found; } - yaffs_AddObjectToDirectory(parent, in); + yaffs_add_obj_to_dir(parent, in); - if (0 && (parent == dev->deletedDir || - parent == dev->unlinkedDir)) { - in->deleted = 1; /* If it is unlinked at start up then it wants deleting */ - dev->nDeletedFiles++; - } - /* 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->variantType) { + switch (in->variant_type) { case YAFFS_OBJECT_TYPE_UNKNOWN: /* Todo got a problem */ break; case YAFFS_OBJECT_TYPE_FILE: - if (dev->param.useHeaderFileSize) - - in->variant.fileVariant. - fileSize = - oh->fileSize; - + if (dev->param. + use_header_file_size) + in->variant. + file_variant.file_size + = yaffs_oh_to_size(oh); break; case YAFFS_OBJECT_TYPE_HARDLINK: - in->variant.hardLinkVariant. - equivalentObjectId = - oh->equivalentObjectId; - in->hardLinks.next = - (struct ylist_head *) - hardList; - hardList = in; + in->variant. + hardlink_variant.equiv_id = + oh->equiv_id; + list_add(&in->hard_links, + &hard_list); break; case YAFFS_OBJECT_TYPE_DIRECTORY: /* Do nothing */ @@ -381,80 +341,82 @@ int yaffs1_Scan(yaffs_Device *dev) /* Do nothing */ break; case YAFFS_OBJECT_TYPE_SYMLINK: - in->variant.symLinkVariant.alias = - yaffs_CloneString(oh->alias); - if (!in->variant.symLinkVariant.alias) + in->variant.symlink_variant. + alias = + yaffs_clone_str(oh->alias); + if (!in->variant. + symlink_variant.alias) alloc_failed = 1; break; } - } } } - if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) { - /* If we got this far while scanning, then the block is fully allocated.*/ + if (state == YAFFS_BLOCK_STATE_NEEDS_SCAN) { + /* If we got this far while scanning, + * then the block is fully allocated. */ state = YAFFS_BLOCK_STATE_FULL; } if (state == YAFFS_BLOCK_STATE_ALLOCATING) { - /* If the block was partially allocated then treat it as fully allocated.*/ + /* If the block was partially allocated then + * treat it as fully allocated. */ state = YAFFS_BLOCK_STATE_FULL; - dev->allocationBlock = -1; + dev->alloc_block = -1; } - bi->blockState = state; + bi->block_state = state; /* Now let's see if it was dirty */ - if (bi->pagesInUse == 0 && - !bi->hasShrinkHeader && - bi->blockState == YAFFS_BLOCK_STATE_FULL) { - yaffs_BlockBecameDirty(dev, blk); - } - + if (bi->pages_in_use == 0 && + !bi->has_shrink_hdr && + bi->block_state == YAFFS_BLOCK_STATE_FULL) + yaffs_block_became_dirty(dev, blk); } - /* Ok, we've done all the scanning. * Fix up the hard link chains. - * We should now have scanned all the objects, now it's time to add these - * hardlinks. + * We should now have scanned all the objects, now it's time to add + * these hardlinks. */ - yaffs_HardlinkFixup(dev, hardList); + yaffs_link_fixup(dev, &hard_list); - /* Fix up any shadowed objects */ + /* + * Fix up any shadowed objects. + * There should not be more than one of these. + */ { - struct yaffs_ShadowFixerStruct *fixer; - yaffs_Object *obj; - - while (shadowFixerList) { - fixer = shadowFixerList; - shadowFixerList = fixer->next; - /* Complete the rename transaction by deleting the shadowed object - * then setting the object header to unshadowed. + struct yaffs_shadow_fixer *fixer; + struct yaffs_obj *obj; + + while (shadow_fixers) { + fixer = shadow_fixers; + shadow_fixers = fixer->next; + /* Complete the rename transaction by deleting the + * shadowed object then setting the object header + to unshadowed. */ - obj = yaffs_FindObjectByNumber(dev, fixer->shadowedId); + obj = yaffs_find_by_number(dev, fixer->shadowed_id); if (obj) - yaffs_DeleteObject(obj); + yaffs_del_obj(obj); - obj = yaffs_FindObjectByNumber(dev, fixer->objectId); + obj = yaffs_find_by_number(dev, fixer->obj_id); if (obj) - yaffs_UpdateObjectHeader(obj, NULL, 1, 0, 0, NULL); + yaffs_update_oh(obj, NULL, 1, 0, 0, NULL); - YFREE(fixer); + kfree(fixer); } } - yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__); + yaffs_release_temp_buffer(dev, chunk_data); if (alloc_failed) return YAFFS_FAIL; - T(YAFFS_TRACE_SCAN, (TSTR("yaffs1_Scan ends" TENDSTR))); - + yaffs_trace(YAFFS_TRACE_SCAN, "yaffs1_scan ends"); return YAFFS_OK; } -