From 1130964c5c69d6a8f635d2a4e9ccd67cc7161b49 Mon Sep 17 00:00:00 2001 From: Markinus Date: Sat, 29 Jan 2011 12:02:03 +0100 Subject: [PATCH] yaffs: moved to newest official xyaffs driver --- fs/yaffs2/Kconfig | 31 +- fs/yaffs2/yaffs_allocator.c | 76 +- fs/yaffs2/yaffs_attribs.c | 6 +- fs/yaffs2/yaffs_bitmap.c | 31 +- fs/yaffs2/yaffs_checkptrw.c | 106 +- fs/yaffs2/yaffs_ecc.c | 42 +- fs/yaffs2/yaffs_ecc.h | 4 +- fs/yaffs2/yaffs_getblockinfo.h | 9 +- fs/yaffs2/yaffs_guts.c | 6531 ++++++++++++++++---------------- fs/yaffs2/yaffs_guts.h | 254 +- fs/yaffs2/yaffs_linux.h | 4 +- fs/yaffs2/yaffs_mtdif.c | 1 - fs/yaffs2/yaffs_mtdif1.c | 48 +- fs/yaffs2/yaffs_mtdif1.h | 6 +- fs/yaffs2/yaffs_mtdif2.c | 50 +- fs/yaffs2/yaffs_mtdif2.h | 6 +- fs/yaffs2/yaffs_nameval.c | 37 +- fs/yaffs2/yaffs_nand.c | 34 +- fs/yaffs2/yaffs_nand.h | 4 +- fs/yaffs2/yaffs_packedtags1.c | 8 +- fs/yaffs2/yaffs_packedtags2.c | 38 +- fs/yaffs2/yaffs_tagscompat.c | 127 +- fs/yaffs2/yaffs_tagscompat.h | 6 +- fs/yaffs2/yaffs_tagsvalidity.c | 1 - fs/yaffs2/yaffs_trace.h | 3 +- fs/yaffs2/yaffs_verify.c | 236 +- fs/yaffs2/yaffs_vfs.c | 835 ++-- fs/yaffs2/yaffs_yaffs1.c | 161 +- fs/yaffs2/yaffs_yaffs2.c | 1218 +++--- fs/yaffs2/yportenv.h | 270 +- 30 files changed, 4705 insertions(+), 5478 deletions(-) diff --git a/fs/yaffs2/Kconfig b/fs/yaffs2/Kconfig index 7b3988c3..63541405 100644 --- a/fs/yaffs2/Kconfig +++ b/fs/yaffs2/Kconfig @@ -102,22 +102,6 @@ config YAFFS_DISABLE_TAGS_ECC If unsure, say N. - -config YAFFS_DISABLE_WIDE_TNODES - bool "Turn off wide tnodes" - depends on YAFFS_FS - default n - help - Wide tnodes are only used for NAND arrays >=32MB for 512-byte - page devices and >=128MB for 2k page devices. They use slightly - more RAM but are faster since they eliminate chunk group - searching. - - Setting this to 'y' will force tnode width to 16 bits and save - memory but make large arrays slower. - - If unsure, say N. - config YAFFS_ALWAYS_CHECK_CHUNK_ERASED bool "Force chunk erase check" depends on YAFFS_FS @@ -136,17 +120,6 @@ config YAFFS_ALWAYS_CHECK_CHUNK_ERASED If unsure, say Y. -config YAFFS_SHORT_NAMES_IN_RAM - bool "Cache short names in RAM" - depends on YAFFS_FS - default y - help - If this config is set, then short names are stored with the - yaffs_Object. This costs an extra 16 bytes of RAM per object, - but makes look-ups faster. - - If unsure, say Y. - config YAFFS_EMPTY_LOST_AND_FOUND bool "Empty lost and found on boot" depends on YAFFS_FS @@ -177,7 +150,7 @@ config YAFFS_DISABLE_BACKGROUND If this is set, then background processing is disabled. Background processing makes many foreground activities faster. - If unsure, say N. + If unsure, say N. config YAFFS_XATTR bool "Enable yaffs2 xattr support" @@ -186,5 +159,3 @@ config YAFFS_XATTR help If this is set then yaffs2 will provide xattr support. If unsure, say Y. - - diff --git a/fs/yaffs2/yaffs_allocator.c b/fs/yaffs2/yaffs_allocator.c index 03ede863..59cce04a 100644 --- a/fs/yaffs2/yaffs_allocator.c +++ b/fs/yaffs2/yaffs_allocator.c @@ -16,7 +16,8 @@ #include "yaffs_trace.h" #include "yportenv.h" -#ifdef CONFIG_YAFFS_YMALLOC_ALLOCATOR +#ifdef CONFIG_YAFFS_KMALLOC_ALLOCATOR +/* This is an alternative debug allocator. Don't use for production code. */ void yaffs_deinit_raw_tnodes_and_objs(struct yaffs_dev *dev) { @@ -30,13 +31,13 @@ void yaffs_init_raw_tnodes_and_objs(struct yaffs_dev *dev) struct yaffs_tnode *yaffs_alloc_raw_tnode(struct yaffs_dev *dev) { - return (struct yaffs_tnode *)YMALLOC(dev->tnode_size); + return kmalloc(dev->tnode_size, GFP_NOFS); } void yaffs_free_raw_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn) { dev = dev; - YFREE(tn); + kfree(tn); } void yaffs_init_raw_objs(struct yaffs_dev *dev) @@ -52,14 +53,14 @@ void yaffs_deinit_raw_objs(struct yaffs_dev *dev) struct yaffs_obj *yaffs_alloc_raw_obj(struct yaffs_dev *dev) { dev = dev; - return (struct yaffs_obj *)YMALLOC(sizeof(struct yaffs_obj)); + return kmalloc(sizeof(struct yaffs_obj)); } void yaffs_free_raw_obj(struct yaffs_dev *dev, struct yaffs_obj *obj) { dev = dev; - YFREE(obj); + kfree(obj); } #else @@ -89,10 +90,8 @@ struct yaffs_allocator { static void yaffs_deinit_raw_tnodes(struct yaffs_dev *dev) { - struct yaffs_allocator *allocator = (struct yaffs_allocator *)dev->allocator; - struct yaffs_tnode_list *tmp; if (!allocator) { @@ -103,10 +102,9 @@ static void yaffs_deinit_raw_tnodes(struct yaffs_dev *dev) while (allocator->alloc_tnode_list) { tmp = allocator->alloc_tnode_list->next; - YFREE(allocator->alloc_tnode_list->tnodes); - YFREE(allocator->alloc_tnode_list); + kfree(allocator->alloc_tnode_list->tnodes); + kfree(allocator->alloc_tnode_list); allocator->alloc_tnode_list = tmp; - } allocator->free_tnodes = NULL; @@ -123,8 +121,9 @@ static void yaffs_init_raw_tnodes(struct yaffs_dev *dev) allocator->free_tnodes = NULL; allocator->n_free_tnodes = 0; allocator->n_tnodes_created = 0; - } else + } else { YBUG(); + } } static int yaffs_create_tnodes(struct yaffs_dev *dev, int n_tnodes) @@ -147,13 +146,12 @@ static int yaffs_create_tnodes(struct yaffs_dev *dev, int n_tnodes) return YAFFS_OK; /* make these things */ - - new_tnodes = YMALLOC(n_tnodes * dev->tnode_size); + new_tnodes = kmalloc(n_tnodes * dev->tnode_size, GFP_NOFS); mem = (u8 *) new_tnodes; if (!new_tnodes) { - T(YAFFS_TRACE_ERROR, - (TSTR("yaffs: Could not allocate Tnodes" TENDSTR))); + yaffs_trace(YAFFS_TRACE_ERROR, + "yaffs: Could not allocate Tnodes"); return YAFFS_FAIL; } @@ -175,12 +173,10 @@ static int yaffs_create_tnodes(struct yaffs_dev *dev, int n_tnodes) * NB If we can't add this to the management list it isn't fatal * but it just means we can't free this bunch of tnodes later. */ - - tnl = YMALLOC(sizeof(struct yaffs_tnode_list)); + tnl = kmalloc(sizeof(struct yaffs_tnode_list), GFP_NOFS); if (!tnl) { - T(YAFFS_TRACE_ERROR, - (TSTR - ("yaffs: Could not add tnodes to management list" TENDSTR))); + yaffs_trace(YAFFS_TRACE_ERROR, + "Could not add tnodes to management list"); return YAFFS_FAIL; } else { tnl->tnodes = new_tnodes; @@ -188,7 +184,7 @@ static int yaffs_create_tnodes(struct yaffs_dev *dev, int n_tnodes) allocator->alloc_tnode_list = tnl; } - T(YAFFS_TRACE_ALLOCATE, (TSTR("yaffs: Tnodes added" TENDSTR))); + yaffs_trace(YAFFS_TRACE_ALLOCATE, "Tnodes added"); return YAFFS_OK; } @@ -243,8 +239,9 @@ static void yaffs_init_raw_objs(struct yaffs_dev *dev) allocator->allocated_obj_list = NULL; allocator->free_objs = NULL; allocator->n_free_objects = 0; - } else + } else { YBUG(); + } } static void yaffs_deinit_raw_objs(struct yaffs_dev *dev) @@ -259,8 +256,8 @@ static void yaffs_deinit_raw_objs(struct yaffs_dev *dev) while (allocator->allocated_obj_list) { tmp = allocator->allocated_obj_list->next; - YFREE(allocator->allocated_obj_list->objects); - YFREE(allocator->allocated_obj_list); + kfree(allocator->allocated_obj_list->objects); + kfree(allocator->allocated_obj_list); allocator->allocated_obj_list = tmp; } @@ -273,7 +270,6 @@ static void yaffs_deinit_raw_objs(struct yaffs_dev *dev) static int yaffs_create_free_objs(struct yaffs_dev *dev, int n_obj) { struct yaffs_allocator *allocator = dev->allocator; - int i; struct yaffs_obj *new_objs; struct yaffs_obj_list *list; @@ -287,20 +283,16 @@ static int yaffs_create_free_objs(struct yaffs_dev *dev, int n_obj) return YAFFS_OK; /* make these things */ - new_objs = YMALLOC(n_obj * sizeof(struct yaffs_obj)); - list = YMALLOC(sizeof(struct yaffs_obj_list)); + new_objs = kmalloc(n_obj * sizeof(struct yaffs_obj), GFP_NOFS); + list = kmalloc(sizeof(struct yaffs_obj_list), GFP_NOFS); if (!new_objs || !list) { - if (new_objs) { - YFREE(new_objs); - new_objs = NULL; - } - if (list) { - YFREE(list); - list = NULL; - } - T(YAFFS_TRACE_ALLOCATE, - (TSTR("yaffs: Could not allocate more objects" TENDSTR))); + kfree(new_objs); + new_objs = NULL; + kfree(list); + list = NULL; + yaffs_trace(YAFFS_TRACE_ALLOCATE, + "Could not allocate more objects"); return YAFFS_FAIL; } @@ -369,10 +361,11 @@ void yaffs_deinit_raw_tnodes_and_objs(struct yaffs_dev *dev) yaffs_deinit_raw_tnodes(dev); yaffs_deinit_raw_objs(dev); - YFREE(dev->allocator); + kfree(dev->allocator); dev->allocator = NULL; - } else + } else { YBUG(); + } } void yaffs_init_raw_tnodes_and_objs(struct yaffs_dev *dev) @@ -380,14 +373,15 @@ void yaffs_init_raw_tnodes_and_objs(struct yaffs_dev *dev) struct yaffs_allocator *allocator; if (!dev->allocator) { - allocator = YMALLOC(sizeof(struct yaffs_allocator)); + allocator = kmalloc(sizeof(struct yaffs_allocator), GFP_NOFS); if (allocator) { dev->allocator = allocator; yaffs_init_raw_tnodes(dev); yaffs_init_raw_objs(dev); } - } else + } else { YBUG(); + } } #endif diff --git a/fs/yaffs2/yaffs_attribs.c b/fs/yaffs2/yaffs_attribs.c index aaf46850..9b47d376 100644 --- a/fs/yaffs2/yaffs_attribs.c +++ b/fs/yaffs2/yaffs_attribs.c @@ -39,9 +39,9 @@ void yaffs_load_current_time(struct yaffs_obj *obj, int do_a, int do_c) { obj->yst_mtime = Y_CURRENT_TIME; if (do_a) - obj->yst_atime = obj->yst_atime; + obj->yst_atime = obj->yst_mtime; if (do_c) - obj->yst_ctime = obj->yst_atime; + obj->yst_ctime = obj->yst_mtime; } void yaffs_attribs_init(struct yaffs_obj *obj, u32 gid, u32 uid, u32 rdev) @@ -64,7 +64,7 @@ loff_t yaffs_get_file_size(struct yaffs_obj *obj) alias = obj->variant.symlink_variant.alias; if (!alias) return 0; - return yaffs_strnlen(alias, YAFFS_MAX_ALIAS_LENGTH); + return strnlen(alias, YAFFS_MAX_ALIAS_LENGTH); default: return 0; } diff --git a/fs/yaffs2/yaffs_bitmap.c b/fs/yaffs2/yaffs_bitmap.c index 5d1cfb29..dc673e84 100644 --- a/fs/yaffs2/yaffs_bitmap.c +++ b/fs/yaffs2/yaffs_bitmap.c @@ -17,12 +17,12 @@ * Chunk bitmap manipulations */ -static Y_INLINE u8 *yaffs_block_bits(struct yaffs_dev *dev, int blk) +static inline u8 *yaffs_block_bits(struct yaffs_dev *dev, int blk) { if (blk < dev->internal_start_block || blk > dev->internal_end_block) { - T(YAFFS_TRACE_ERROR, - (TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR), - blk)); + yaffs_trace(YAFFS_TRACE_ERROR, + "BlockBits block %d is not valid", + blk); YBUG(); } return dev->chunk_bits + @@ -33,9 +33,9 @@ void yaffs_verify_chunk_bit_id(struct yaffs_dev *dev, int blk, int chunk) { if (blk < dev->internal_start_block || blk > dev->internal_end_block || chunk < 0 || chunk >= dev->param.chunks_per_block) { - T(YAFFS_TRACE_ERROR, - (TSTR("**>> yaffs: Chunk Id (%d:%d) invalid" TENDSTR), - blk, chunk)); + yaffs_trace(YAFFS_TRACE_ERROR, + "Chunk Id (%d:%d) invalid", + blk, chunk); YBUG(); } } @@ -52,7 +52,6 @@ void yaffs_clear_chunk_bit(struct yaffs_dev *dev, int blk, int chunk) u8 *blk_bits = yaffs_block_bits(dev, blk); yaffs_verify_chunk_bit_id(dev, blk, chunk); - blk_bits[chunk / 8] &= ~(1 << (chunk & 7)); } @@ -61,15 +60,14 @@ void yaffs_set_chunk_bit(struct yaffs_dev *dev, int blk, int chunk) u8 *blk_bits = yaffs_block_bits(dev, blk); yaffs_verify_chunk_bit_id(dev, blk, chunk); - blk_bits[chunk / 8] |= (1 << (chunk & 7)); } int yaffs_check_chunk_bit(struct yaffs_dev *dev, int blk, int chunk) { u8 *blk_bits = yaffs_block_bits(dev, blk); - yaffs_verify_chunk_bit_id(dev, blk, chunk); + yaffs_verify_chunk_bit_id(dev, blk, chunk); return (blk_bits[chunk / 8] & (1 << (chunk & 7))) ? 1 : 0; } @@ -77,6 +75,7 @@ int yaffs_still_some_chunks(struct yaffs_dev *dev, int blk) { u8 *blk_bits = yaffs_block_bits(dev, blk); int i; + for (i = 0; i < dev->chunk_bit_stride; i++) { if (*blk_bits) return 1; @@ -90,15 +89,9 @@ int yaffs_count_chunk_bits(struct yaffs_dev *dev, int blk) u8 *blk_bits = yaffs_block_bits(dev, blk); int i; int n = 0; - for (i = 0; i < dev->chunk_bit_stride; i++) { - u8 x = *blk_bits; - while (x) { - if (x & 1) - n++; - x >>= 1; - } - blk_bits++; - } + for (i = 0; i < dev->chunk_bit_stride; i++, blk_bits++) + n += hweight8(*blk_bits); + return n; } diff --git a/fs/yaffs2/yaffs_checkptrw.c b/fs/yaffs2/yaffs_checkptrw.c index 2cb144c2..711df13e 100644 --- a/fs/yaffs2/yaffs_checkptrw.c +++ b/fs/yaffs2/yaffs_checkptrw.c @@ -18,8 +18,8 @@ static int yaffs2_checkpt_space_ok(struct yaffs_dev *dev) { int blocks_avail = dev->n_erased_blocks - dev->param.n_reserved_blocks; - T(YAFFS_TRACE_CHECKPOINT, - (TSTR("checkpt blocks available = %d" TENDSTR), blocks_avail)); + yaffs_trace(YAFFS_TRACE_CHECKPOINT, + "checkpt blocks_avail = %d", blocks_avail); return (blocks_avail <= 0) ? 0 : 1; } @@ -30,21 +30,21 @@ static int yaffs_checkpt_erase(struct yaffs_dev *dev) if (!dev->param.erase_fn) return 0; - T(YAFFS_TRACE_CHECKPOINT, (TSTR("checking blocks %d to %d" TENDSTR), - dev->internal_start_block, - dev->internal_end_block)); + yaffs_trace(YAFFS_TRACE_CHECKPOINT, + "checking blocks %d to %d", + dev->internal_start_block, dev->internal_end_block); for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) { struct yaffs_block_info *bi = yaffs_get_block_info(dev, i); if (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT) { - T(YAFFS_TRACE_CHECKPOINT, - (TSTR("erasing checkpt block %d" TENDSTR), i)); + yaffs_trace(YAFFS_TRACE_CHECKPOINT, + "erasing checkpt block %d", i); dev->n_erasures++; if (dev->param. erase_fn(dev, - i - dev->block_offset /* realign */ )) { + i - dev->block_offset /* realign */)) { bi->block_state = YAFFS_BLOCK_STATE_EMPTY; dev->n_erased_blocks++; dev->n_free_chunks += @@ -65,11 +65,11 @@ static void yaffs2_checkpt_find_erased_block(struct yaffs_dev *dev) { int i; int blocks_avail = dev->n_erased_blocks - dev->param.n_reserved_blocks; - T(YAFFS_TRACE_CHECKPOINT, - (TSTR - ("allocating checkpt block: erased %d reserved %d avail %d next %d " - TENDSTR), dev->n_erased_blocks, dev->param.n_reserved_blocks, - blocks_avail, dev->checkpt_next_block)); + + yaffs_trace(YAFFS_TRACE_CHECKPOINT, + "allocating checkpt block: erased %d reserved %d avail %d next %d ", + dev->n_erased_blocks, dev->param.n_reserved_blocks, + blocks_avail, dev->checkpt_next_block); if (dev->checkpt_next_block >= 0 && dev->checkpt_next_block <= dev->internal_end_block && @@ -82,14 +82,13 @@ static void yaffs2_checkpt_find_erased_block(struct yaffs_dev *dev) if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) { dev->checkpt_next_block = i + 1; dev->checkpt_cur_block = i; - T(YAFFS_TRACE_CHECKPOINT, - (TSTR("allocating checkpt block %d" TENDSTR), - i)); + yaffs_trace(YAFFS_TRACE_CHECKPOINT, + "allocating checkpt block %d", i); return; } } } - T(YAFFS_TRACE_CHECKPOINT, (TSTR("out of checkpt blocks" TENDSTR))); + yaffs_trace(YAFFS_TRACE_CHECKPOINT, "out of checkpt blocks"); dev->checkpt_next_block = -1; dev->checkpt_cur_block = -1; @@ -100,9 +99,9 @@ static void yaffs2_checkpt_find_block(struct yaffs_dev *dev) int i; struct yaffs_ext_tags tags; - T(YAFFS_TRACE_CHECKPOINT, - (TSTR("find next checkpt block: start: blocks %d next %d" TENDSTR), - dev->blocks_in_checkpt, dev->checkpt_next_block)); + yaffs_trace(YAFFS_TRACE_CHECKPOINT, + "find next checkpt block: start: blocks %d next %d", + dev->blocks_in_checkpt, dev->checkpt_next_block); if (dev->blocks_in_checkpt < dev->checkpt_max_blocks) for (i = dev->checkpt_next_block; i <= dev->internal_end_block; @@ -112,11 +111,10 @@ static void yaffs2_checkpt_find_block(struct yaffs_dev *dev) dev->param.read_chunk_tags_fn(dev, realigned_chunk, NULL, &tags); - T(YAFFS_TRACE_CHECKPOINT, - (TSTR - ("find next checkpt block: search: block %d oid %d seq %d eccr %d" - TENDSTR), i, tags.obj_id, tags.seq_number, - tags.ecc_result)); + yaffs_trace(YAFFS_TRACE_CHECKPOINT, + "find next checkpt block: search: block %d oid %d seq %d eccr %d", + i, tags.obj_id, tags.seq_number, + tags.ecc_result); if (tags.seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA) { /* Right kind of block */ @@ -125,14 +123,13 @@ static void yaffs2_checkpt_find_block(struct yaffs_dev *dev) dev->checkpt_block_list[dev-> blocks_in_checkpt] = i; dev->blocks_in_checkpt++; - T(YAFFS_TRACE_CHECKPOINT, - (TSTR("found checkpt block %d" TENDSTR), i)); + yaffs_trace(YAFFS_TRACE_CHECKPOINT, + "found checkpt block %d", i); return; } } - T(YAFFS_TRACE_CHECKPOINT, - (TSTR("found no more checkpt blocks" TENDSTR))); + yaffs_trace(YAFFS_TRACE_CHECKPOINT, "found no more checkpt blocks"); dev->checkpt_next_block = -1; dev->checkpt_cur_block = -1; @@ -140,7 +137,6 @@ static void yaffs2_checkpt_find_block(struct yaffs_dev *dev) int yaffs2_checkpt_open(struct yaffs_dev *dev, int writing) { - dev->checkpt_open_write = writing; /* Got the functions we need? */ @@ -154,7 +150,7 @@ int yaffs2_checkpt_open(struct yaffs_dev *dev, int writing) if (!dev->checkpt_buffer) dev->checkpt_buffer = - YMALLOC_DMA(dev->param.total_bytes_per_chunk); + kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS); if (!dev->checkpt_buffer) return 0; @@ -175,14 +171,14 @@ int yaffs2_checkpt_open(struct yaffs_dev *dev, int writing) int i; /* Set to a value that will kick off a read */ dev->checkpt_byte_offs = dev->data_bytes_per_chunk; - /* A checkpoint block list of 1 checkpoint block per 16 block is (hopefully) - * going to be way more than we need */ + /* A checkpoint block list of 1 checkpoint block per 16 block is + * (hopefully) going to be way more than we need */ dev->blocks_in_checkpt = 0; dev->checkpt_max_blocks = (dev->internal_end_block - dev->internal_start_block) / 16 + 2; dev->checkpt_block_list = - YMALLOC(sizeof(int) * dev->checkpt_max_blocks); + kmalloc(sizeof(int) * dev->checkpt_max_blocks, GFP_NOFS); if (!dev->checkpt_block_list) return 0; @@ -196,6 +192,7 @@ int yaffs2_checkpt_open(struct yaffs_dev *dev, int writing) int yaffs2_get_checkpt_sum(struct yaffs_dev *dev, u32 * sum) { u32 composite_sum; + composite_sum = (dev->checkpt_sum << 8) | (dev->checkpt_xor & 0xFF); *sum = composite_sum; return 1; @@ -205,7 +202,6 @@ static int yaffs2_checkpt_flush_buffer(struct yaffs_dev *dev) { int chunk; int realigned_chunk; - struct yaffs_ext_tags tags; if (dev->checkpt_cur_block < 0) { @@ -234,11 +230,10 @@ static int yaffs2_checkpt_flush_buffer(struct yaffs_dev *dev) dev->checkpt_cur_block * dev->param.chunks_per_block + dev->checkpt_cur_chunk; - T(YAFFS_TRACE_CHECKPOINT, - (TSTR - ("checkpoint wite buffer nand %d(%d:%d) objid %d chId %d" TENDSTR), - chunk, dev->checkpt_cur_block, dev->checkpt_cur_chunk, tags.obj_id, - tags.chunk_id)); + yaffs_trace(YAFFS_TRACE_CHECKPOINT, + "checkpoint wite buffer nand %d(%d:%d) objid %d chId %d", + chunk, dev->checkpt_cur_block, dev->checkpt_cur_chunk, + tags.obj_id, tags.chunk_id); realigned_chunk = chunk - dev->chunk_offset; @@ -262,7 +257,6 @@ int yaffs2_checkpt_wr(struct yaffs_dev *dev, const void *data, int n_bytes) { int i = 0; int ok = 1; - u8 *data_bytes = (u8 *) data; if (!dev->checkpt_buffer) @@ -294,10 +288,8 @@ int yaffs2_checkpt_rd(struct yaffs_dev *dev, void *data, int n_bytes) int i = 0; int ok = 1; struct yaffs_ext_tags tags; - int chunk; int realigned_chunk; - u8 *data_bytes = (u8 *) data; if (!dev->checkpt_buffer) @@ -329,10 +321,9 @@ int yaffs2_checkpt_rd(struct yaffs_dev *dev, void *data, int n_bytes) /* read in the next chunk */ dev->param.read_chunk_tags_fn(dev, - realigned_chunk, - dev-> - checkpt_buffer, - &tags); + realigned_chunk, + dev->checkpt_buffer, + &tags); if (tags.chunk_id != (dev->checkpt_page_seq + 1) || tags.ecc_result > YAFFS_ECC_RESULT_FIXED @@ -367,7 +358,6 @@ int yaffs2_checkpt_rd(struct yaffs_dev *dev, void *data, int n_bytes) int yaffs_checkpt_close(struct yaffs_dev *dev) { - if (dev->checkpt_open_write) { if (dev->checkpt_byte_offs != 0) yaffs2_checkpt_flush_buffer(dev); @@ -383,11 +373,8 @@ int yaffs_checkpt_close(struct yaffs_dev *dev) bi = yaffs_get_block_info(dev, blk); if (bi && bi->block_state == YAFFS_BLOCK_STATE_EMPTY) bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT; - else { - /* Todo this looks odd... */ - } } - YFREE(dev->checkpt_block_list); + kfree(dev->checkpt_block_list); dev->checkpt_block_list = NULL; } @@ -395,25 +382,26 @@ int yaffs_checkpt_close(struct yaffs_dev *dev) dev->blocks_in_checkpt * dev->param.chunks_per_block; dev->n_erased_blocks -= dev->blocks_in_checkpt; - T(YAFFS_TRACE_CHECKPOINT, (TSTR("checkpoint byte count %d" TENDSTR), - dev->checkpt_byte_count)); + yaffs_trace(YAFFS_TRACE_CHECKPOINT, "checkpoint byte count %d", + dev->checkpt_byte_count); if (dev->checkpt_buffer) { /* free the buffer */ - YFREE(dev->checkpt_buffer); + kfree(dev->checkpt_buffer); dev->checkpt_buffer = NULL; return 1; - } else + } else { return 0; + } } int yaffs2_checkpt_invalidate_stream(struct yaffs_dev *dev) { /* Erase the checkpoint data */ - T(YAFFS_TRACE_CHECKPOINT, - (TSTR("checkpoint invalidate of %d blocks" TENDSTR), - dev->blocks_in_checkpt)); + yaffs_trace(YAFFS_TRACE_CHECKPOINT, + "checkpoint invalidate of %d blocks", + dev->blocks_in_checkpt); return yaffs_checkpt_erase(dev); } diff --git a/fs/yaffs2/yaffs_ecc.c b/fs/yaffs2/yaffs_ecc.c index 0ea0305e..4f29b80d 100644 --- a/fs/yaffs2/yaffs_ecc.c +++ b/fs/yaffs2/yaffs_ecc.c @@ -16,16 +16,16 @@ * * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes. * The two unused bit are set to 1. - * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC - * blocks are used on a 512-byte NAND page. + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two + * such ECC blocks are used on a 512-byte NAND page. * */ /* Table generated by gen-ecc.c * Using a table means we do not have to calculate p1..p4 and p1'..p4' * for each byte of data. These are instead provided in a table in bits7..2. - * Bit 0 of each entry indicates whether the entry has an odd or even parity, and therefore - * this bytes influence on the line parity. + * Bit 0 of each entry indicates whether the entry has an odd or even parity, + * and therefore this bytes influence on the line parity. */ #include "yportenv.h" @@ -67,35 +67,11 @@ static const unsigned char column_parity_table[] = { 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00, }; -/* Count the bits in an unsigned char or a U32 */ - -static int yaffs_count_bits(unsigned char x) -{ - int r = 0; - while (x) { - if (x & 1) - r++; - x >>= 1; - } - return r; -} - -static int yaffs_count_bits32(unsigned x) -{ - int r = 0; - while (x) { - if (x & 1) - r++; - x >>= 1; - } - return r; -} /* Calculate the ECC for a 256-byte block of data */ void yaffs_ecc_cacl(const unsigned char *data, unsigned char *ecc) { unsigned int i; - unsigned char col_parity = 0; unsigned char line_parity = 0; unsigned char line_parity_prime = 0; @@ -222,8 +198,7 @@ int yaffs_ecc_correct(unsigned char *data, unsigned char *read_ecc, return 1; /* Corrected the error */ } - if ((yaffs_count_bits(d0) + - yaffs_count_bits(d1) + yaffs_count_bits(d2)) == 1) { + if ((hweight8(d0) + hweight8(d1) + hweight8(d2)) == 1) { /* Reccoverable error in ecc */ read_ecc[0] = test_ecc[0]; @@ -246,7 +221,6 @@ void yaffs_ecc_calc_other(const unsigned char *data, unsigned n_bytes, struct yaffs_ecc_other *ecc_other) { unsigned int i; - unsigned char col_parity = 0; unsigned line_parity = 0; unsigned line_parity_prime = 0; @@ -307,9 +281,9 @@ int yaffs_ecc_correct_other(unsigned char *data, unsigned n_bytes, return 1; /* corrected */ } - if ((yaffs_count_bits32(delta_line) + - yaffs_count_bits32(delta_line_prime) + - yaffs_count_bits(delta_col)) == 1) { + if ((hweight32(delta_line) + + hweight32(delta_line_prime) + + hweight8(delta_col)) == 1) { /* Reccoverable error in ecc */ *read_ecc = *test_ecc; diff --git a/fs/yaffs2/yaffs_ecc.h b/fs/yaffs2/yaffs_ecc.h index b0c461d6..a84e8017 100644 --- a/fs/yaffs2/yaffs_ecc.h +++ b/fs/yaffs2/yaffs_ecc.h @@ -18,8 +18,8 @@ * * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes. * The two unused bit are set to 1. - * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC - * blocks are used on a 512-byte NAND page. + * The ECC can correct single bit errors in a 256-byte page of data. + * Thus, two such ECC blocks are used on a 512-byte NAND page. * */ diff --git a/fs/yaffs2/yaffs_getblockinfo.h b/fs/yaffs2/yaffs_getblockinfo.h index 108c3612..d87acbde 100644 --- a/fs/yaffs2/yaffs_getblockinfo.h +++ b/fs/yaffs2/yaffs_getblockinfo.h @@ -20,14 +20,13 @@ #include "yaffs_trace.h" /* Function to manipulate block info */ -static Y_INLINE struct yaffs_block_info *yaffs_get_block_info(struct yaffs_dev +static inline struct yaffs_block_info *yaffs_get_block_info(struct yaffs_dev *dev, int blk) { if (blk < dev->internal_start_block || blk > dev->internal_end_block) { - T(YAFFS_TRACE_ERROR, - (TSTR - ("**>> yaffs: get_block_info block %d is not valid" TENDSTR), - blk)); + yaffs_trace(YAFFS_TRACE_ERROR, + "**>> yaffs: get_block_info block %d is not valid", + blk); YBUG(); } return &dev->block_info[blk - dev->internal_start_block]; diff --git a/fs/yaffs2/yaffs_guts.c b/fs/yaffs2/yaffs_guts.c index a9fc46c0..496cf5fb 100644 --- a/fs/yaffs2/yaffs_guts.c +++ b/fs/yaffs2/yaffs_guts.c @@ -17,22 +17,16 @@ #include "yaffs_guts.h" #include "yaffs_tagsvalidity.h" #include "yaffs_getblockinfo.h" - #include "yaffs_tagscompat.h" - #include "yaffs_nand.h" - #include "yaffs_yaffs1.h" #include "yaffs_yaffs2.h" #include "yaffs_bitmap.h" #include "yaffs_verify.h" - #include "yaffs_nand.h" #include "yaffs_packedtags2.h" - #include "yaffs_nameval.h" #include "yaffs_allocator.h" - #include "yaffs_attribs.h" /* Note YAFFS_GC_GOOD_ENOUGH must be <= YAFFS_GC_PASSIVE_THRESHOLD */ @@ -41,67 +35,17 @@ #include "yaffs_ecc.h" -/* Robustification (if it ever comes about...) */ -static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block); -static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk, - int erased_ok); -static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk, - const u8 * data, - const struct yaffs_ext_tags *tags); -static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk, - const struct yaffs_ext_tags *tags); +/* Forward declarations */ -/* Other local prototypes */ -static void yaffs_update_parent(struct yaffs_obj *obj); -static int yaffs_unlink_obj(struct yaffs_obj *obj); -static int yaffs_obj_cache_dirty(struct yaffs_obj *obj); +static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk, + const u8 *buffer, int n_bytes, int use_reserve); -static int yaffs_write_new_chunk(struct yaffs_dev *dev, - const u8 * buffer, - struct yaffs_ext_tags *tags, int use_reserver); -static struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number, - enum yaffs_obj_type type); - -static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer, - struct yaffs_xattr_mod *xmod); - -static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj); -static int yaffs_generic_obj_del(struct yaffs_obj *in); - -static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk); - -static int yaffs_unlink_worker(struct yaffs_obj *obj); - -static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id, - int chunk_obj); - -static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver, - struct yaffs_block_info **block_ptr); - -static void yaffs_check_obj_details_loaded(struct yaffs_obj *in); - -static void yaffs_invalidate_whole_cache(struct yaffs_obj *in); -static void yaffs_invalidate_chunk_cache(struct yaffs_obj *object, - int chunk_id); - -static int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk, - struct yaffs_ext_tags *tags); - -static int yaffs_verify_chunk_written(struct yaffs_dev *dev, - int nand_chunk, - const u8 * data, - struct yaffs_ext_tags *tags); - -static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR * name, - const YCHAR * oh_name, int buff_size); -static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR * oh_name, - const YCHAR * name); /* Function to calculate chunk and offset */ static void yaffs_addr_to_chunk(struct yaffs_dev *dev, loff_t addr, - int *chunk_out, u32 * offset_out) + int *chunk_out, u32 *offset_out) { int chunk; u32 offset; @@ -186,7 +130,7 @@ static int yaffs_init_tmp_buffers(struct yaffs_dev *dev) for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) { dev->temp_buffer[i].line = 0; /* not in use */ dev->temp_buffer[i].buffer = buf = - YMALLOC_DMA(dev->param.total_bytes_per_chunk); + kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS); } return buf ? YAFFS_OK : YAFFS_FAIL; @@ -194,7 +138,8 @@ static int yaffs_init_tmp_buffers(struct yaffs_dev *dev) u8 *yaffs_get_temp_buffer(struct yaffs_dev * dev, int line_no) { - int i, j; + int i; + int j; dev->temp_in_use++; if (dev->temp_in_use > dev->max_temp) @@ -214,14 +159,12 @@ u8 *yaffs_get_temp_buffer(struct yaffs_dev * dev, int line_no) } } - T(YAFFS_TRACE_BUFFERS, - (TSTR("Out of temp buffers at line %d, other held by lines:"), - line_no)); + yaffs_trace(YAFFS_TRACE_BUFFERS, + "Out of temp buffers at line %d, other held by lines:", + line_no); for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) - T(YAFFS_TRACE_BUFFERS, - (TSTR(" %d "), dev->temp_buffer[i].line)); - - T(YAFFS_TRACE_BUFFERS, (TSTR(" " TENDSTR))); + yaffs_trace(YAFFS_TRACE_BUFFERS, + " %d", dev->temp_buffer[i].line); /* * If we got here then we have to allocate an unmanaged one @@ -229,11 +172,11 @@ u8 *yaffs_get_temp_buffer(struct yaffs_dev * dev, int line_no) */ dev->unmanaged_buffer_allocs++; - return YMALLOC(dev->data_bytes_per_chunk); + return kmalloc(dev->data_bytes_per_chunk, GFP_NOFS); } -void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 * buffer, int line_no) +void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 *buffer, int line_no) { int i; @@ -248,10 +191,10 @@ void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 * buffer, int line_no) if (buffer) { /* assume it is an unmanaged one. */ - T(YAFFS_TRACE_BUFFERS, - (TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR), - line_no)); - YFREE(buffer); + yaffs_trace(YAFFS_TRACE_BUFFERS, + "Releasing unmanaged temp buffer in line %d", + line_no); + kfree(buffer); dev->unmanaged_buffer_deallocs++; } @@ -260,7 +203,7 @@ void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 * buffer, int line_no) /* * Determine if we have a managed buffer. */ -int yaffs_is_managed_tmp_buffer(struct yaffs_dev *dev, const u8 * buffer) +int yaffs_is_managed_tmp_buffer(struct yaffs_dev *dev, const u8 *buffer) { int i; @@ -277,11 +220,72 @@ int yaffs_is_managed_tmp_buffer(struct yaffs_dev *dev, const u8 * buffer) if (buffer == dev->checkpt_buffer) return 1; - T(YAFFS_TRACE_ALWAYS, - (TSTR("yaffs: unmaged buffer detected.\n" TENDSTR))); + yaffs_trace(YAFFS_TRACE_ALWAYS, + "yaffs: unmaged buffer detected."); return 0; } +/* + * Functions for robustisizing TODO + * + */ + +static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk, + const u8 *data, + const struct yaffs_ext_tags *tags) +{ + dev = dev; + nand_chunk = nand_chunk; + data = data; + tags = tags; +} + +static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk, + const struct yaffs_ext_tags *tags) +{ + dev = dev; + nand_chunk = nand_chunk; + tags = tags; +} + +void yaffs_handle_chunk_error(struct yaffs_dev *dev, + struct yaffs_block_info *bi) +{ + if (!bi->gc_prioritise) { + bi->gc_prioritise = 1; + dev->has_pending_prioritised_gc = 1; + bi->chunk_error_strikes++; + + if (bi->chunk_error_strikes > 3) { + bi->needs_retiring = 1; /* Too many stikes, so retire */ + yaffs_trace(YAFFS_TRACE_ALWAYS, + "yaffs: Block struck out"); + + } + } +} + +static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk, + int erased_ok) +{ + int flash_block = nand_chunk / dev->param.chunks_per_block; + struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block); + + yaffs_handle_chunk_error(dev, bi); + + if (erased_ok) { + /* Was an actual write failure, + * so mark the block for retirement.*/ + bi->needs_retiring = 1; + yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS, + "**>> Block %d needs retiring", flash_block); + } + + /* Delete the chunk */ + yaffs_chunk_del(dev, nand_chunk, 1, __LINE__); + yaffs_skip_rest_of_block(dev); +} + /* * Verification code */ @@ -290,7 +294,7 @@ int yaffs_is_managed_tmp_buffer(struct yaffs_dev *dev, const u8 * buffer) * Simple hash function. Needs to have a reasonable spread */ -static Y_INLINE int yaffs_hash_fn(int n) +static inline int yaffs_hash_fn(int n) { n = abs(n); return n % YAFFS_NOBJECT_BUCKETS; @@ -315,7 +319,7 @@ struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev) * Erased NAND checking functions */ -int yaffs_check_ff(u8 * buffer, int n_bytes) +int yaffs_check_ff(u8 *buffer, int n_bytes) { /* Horrible, slow implementation */ while (n_bytes--) { @@ -338,9 +342,10 @@ static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk) if (tags.ecc_result > YAFFS_ECC_RESULT_NO_ERROR) retval = YAFFS_FAIL; - if (!yaffs_check_ff(data, dev->data_bytes_per_chunk) || tags.chunk_used) { - T(YAFFS_TRACE_NANDACCESS, - (TSTR("Chunk %d not erased" TENDSTR), nand_chunk)); + if (!yaffs_check_ff(data, dev->data_bytes_per_chunk) || + tags.chunk_used) { + yaffs_trace(YAFFS_TRACE_NANDACCESS, + "Chunk %d not erased", nand_chunk); retval = YAFFS_FAIL; } @@ -352,7 +357,7 @@ static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk) static int yaffs_verify_chunk_written(struct yaffs_dev *dev, int nand_chunk, - const u8 * data, + const u8 *data, struct yaffs_ext_tags *tags) { int retval = YAFFS_OK; @@ -372,8 +377,150 @@ static int yaffs_verify_chunk_written(struct yaffs_dev *dev, return retval; } + +int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks) +{ + int reserved_chunks; + int reserved_blocks = dev->param.n_reserved_blocks; + int checkpt_blocks; + + checkpt_blocks = yaffs_calc_checkpt_blocks_required(dev); + + reserved_chunks = + ((reserved_blocks + checkpt_blocks) * dev->param.chunks_per_block); + + return (dev->n_free_chunks > (reserved_chunks + n_chunks)); +} + +static int yaffs_find_alloc_block(struct yaffs_dev *dev) +{ + int i; + struct yaffs_block_info *bi; + + if (dev->n_erased_blocks < 1) { + /* Hoosterman we've got a problem. + * Can't get space to gc + */ + yaffs_trace(YAFFS_TRACE_ERROR, + "yaffs tragedy: no more erased blocks"); + + return -1; + } + + /* Find an empty block. */ + + for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) { + dev->alloc_block_finder++; + if (dev->alloc_block_finder < dev->internal_start_block + || dev->alloc_block_finder > dev->internal_end_block) { + dev->alloc_block_finder = dev->internal_start_block; + } + + bi = yaffs_get_block_info(dev, dev->alloc_block_finder); + + if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) { + bi->block_state = YAFFS_BLOCK_STATE_ALLOCATING; + dev->seq_number++; + bi->seq_number = dev->seq_number; + dev->n_erased_blocks--; + yaffs_trace(YAFFS_TRACE_ALLOCATE, + "Allocated block %d, seq %d, %d left" , + dev->alloc_block_finder, dev->seq_number, + dev->n_erased_blocks); + return dev->alloc_block_finder; + } + } + + yaffs_trace(YAFFS_TRACE_ALWAYS, + "yaffs tragedy: no more erased blocks, but there should have been %d", + dev->n_erased_blocks); + + return -1; +} + +static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver, + struct yaffs_block_info **block_ptr) +{ + int ret_val; + struct yaffs_block_info *bi; + + if (dev->alloc_block < 0) { + /* Get next block to allocate off */ + dev->alloc_block = yaffs_find_alloc_block(dev); + dev->alloc_page = 0; + } + + if (!use_reserver && !yaffs_check_alloc_available(dev, 1)) { + /* No space unless we're allowed to use the reserve. */ + return -1; + } + + if (dev->n_erased_blocks < dev->param.n_reserved_blocks + && dev->alloc_page == 0) + yaffs_trace(YAFFS_TRACE_ALLOCATE, "Allocating reserve"); + + /* Next page please.... */ + if (dev->alloc_block >= 0) { + bi = yaffs_get_block_info(dev, dev->alloc_block); + + ret_val = (dev->alloc_block * dev->param.chunks_per_block) + + dev->alloc_page; + bi->pages_in_use++; + yaffs_set_chunk_bit(dev, dev->alloc_block, dev->alloc_page); + + dev->alloc_page++; + + dev->n_free_chunks--; + + /* If the block is full set the state to full */ + if (dev->alloc_page >= dev->param.chunks_per_block) { + bi->block_state = YAFFS_BLOCK_STATE_FULL; + dev->alloc_block = -1; + } + + if (block_ptr) + *block_ptr = bi; + + return ret_val; + } + + yaffs_trace(YAFFS_TRACE_ERROR, + "!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!"); + + return -1; +} + +static int yaffs_get_erased_chunks(struct yaffs_dev *dev) +{ + int n; + + n = dev->n_erased_blocks * dev->param.chunks_per_block; + + if (dev->alloc_block > 0) + n += (dev->param.chunks_per_block - dev->alloc_page); + + return n; + +} + +/* + * yaffs_skip_rest_of_block() skips over the rest of the allocation block + * if we don't want to write to it. + */ +void yaffs_skip_rest_of_block(struct yaffs_dev *dev) +{ + if (dev->alloc_block > 0) { + struct yaffs_block_info *bi = + yaffs_get_block_info(dev, dev->alloc_block); + if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) { + bi->block_state = YAFFS_BLOCK_STATE_FULL; + dev->alloc_block = -1; + } + } +} + static int yaffs_write_new_chunk(struct yaffs_dev *dev, - const u8 * data, + const u8 *data, struct yaffs_ext_tags *tags, int use_reserver) { int attempts = 0; @@ -412,7 +559,7 @@ static int yaffs_write_new_chunk(struct yaffs_dev *dev, * lot of checks that are most likely not needed. * * Mods to the above - * If an erase check fails or the write fails we skip the + * If an erase check fails or the write fails we skip the * rest of the block. */ @@ -425,9 +572,9 @@ static int yaffs_write_new_chunk(struct yaffs_dev *dev, if (!bi->skip_erased_check) { erased_ok = yaffs_check_chunk_erased(dev, chunk); if (erased_ok != YAFFS_OK) { - T(YAFFS_TRACE_ERROR, - (TSTR("**>> yaffs chunk %d was not erased" - TENDSTR), chunk)); + yaffs_trace(YAFFS_TRACE_ERROR, + "**>> yaffs chunk %d was not erased", + chunk); /* If not erased, delete this one, * skip rest of block and @@ -463,10 +610,9 @@ static int yaffs_write_new_chunk(struct yaffs_dev *dev, chunk = -1; if (attempts > 1) { - T(YAFFS_TRACE_ERROR, - (TSTR("**>> yaffs write required %d attempts" TENDSTR), - attempts)); - + yaffs_trace(YAFFS_TRACE_ERROR, + "**>> yaffs write required %d attempts", + attempts); dev->n_retired_writes += (attempts - 1); } @@ -487,10 +633,9 @@ static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block) if (yaffs_mark_bad(dev, flash_block) != YAFFS_OK) { if (yaffs_erase_block(dev, flash_block) != YAFFS_OK) { - T(YAFFS_TRACE_ALWAYS, - (TSTR - ("yaffs: Failed to mark bad and erase block %d" - TENDSTR), flash_block)); + yaffs_trace(YAFFS_TRACE_ALWAYS, + "yaffs: Failed to mark bad and erase block %d", + flash_block); } else { struct yaffs_ext_tags tags; int chunk_id = @@ -505,11 +650,9 @@ static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block) dev->chunk_offset, buffer, &tags) != YAFFS_OK) - T(YAFFS_TRACE_ALWAYS, - (TSTR - ("yaffs: Failed to " - TCONT("write bad block marker to block %d") - TENDSTR), flash_block)); + yaffs_trace(YAFFS_TRACE_ALWAYS, + "yaffs: Failed to write bad block marker to block %d", + flash_block); yaffs_release_temp_buffer(dev, buffer, __LINE__); } @@ -522,74 +665,14 @@ static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block) dev->n_retired_blocks++; } -/* - * Functions for robustisizing TODO - * - */ - -static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk, - const u8 * data, - const struct yaffs_ext_tags *tags) -{ - dev = dev; - nand_chunk = nand_chunk; - data = data; - tags = tags; -} - -static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk, - const struct yaffs_ext_tags *tags) -{ - dev = dev; - nand_chunk = nand_chunk; - tags = tags; -} - -void yaffs_handle_chunk_error(struct yaffs_dev *dev, - struct yaffs_block_info *bi) -{ - if (!bi->gc_prioritise) { - bi->gc_prioritise = 1; - dev->has_pending_prioritised_gc = 1; - bi->chunk_error_strikes++; - - if (bi->chunk_error_strikes > 3) { - bi->needs_retiring = 1; /* Too many stikes, so retire this */ - T(YAFFS_TRACE_ALWAYS, - (TSTR("yaffs: Block struck out" TENDSTR))); - - } - } -} - -static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk, - int erased_ok) -{ - int flash_block = nand_chunk / dev->param.chunks_per_block; - struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block); - - yaffs_handle_chunk_error(dev, bi); - - if (erased_ok) { - /* Was an actual write failure, so mark the block for retirement */ - bi->needs_retiring = 1; - T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS, - (TSTR("**>> Block %d needs retiring" TENDSTR), flash_block)); - } - - /* Delete the chunk */ - yaffs_chunk_del(dev, nand_chunk, 1, __LINE__); - yaffs_skip_rest_of_block(dev); -} - /*---------------- Name handling functions ------------*/ -static u16 yaffs_calc_name_sum(const YCHAR * name) +static u16 yaffs_calc_name_sum(const YCHAR *name) { u16 sum = 0; u16 i = 1; - const YUCHAR *bname = (const YUCHAR *)name; + if (bname) { while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH / 2))) { @@ -604,14 +687,12 @@ static u16 yaffs_calc_name_sum(const YCHAR * name) void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR * name) { -#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM - memset(obj->short_name, 0, - sizeof(YCHAR) * (YAFFS_SHORT_NAME_LENGTH + 1)); - if (name - && yaffs_strnlen(name, - YAFFS_SHORT_NAME_LENGTH + 1) <= - YAFFS_SHORT_NAME_LENGTH) - yaffs_strcpy(obj->short_name, name); +#ifndef CONFIG_YAFFS_NO_SHORT_NAMES + memset(obj->short_name, 0, sizeof(obj->short_name)); + if (name && + strnlen(name, YAFFS_SHORT_NAME_LENGTH + 1) <= + YAFFS_SHORT_NAME_LENGTH) + strcpy(obj->short_name, name); else obj->short_name[0] = _Y('\0'); #endif @@ -642,6 +723,7 @@ void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj, struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev) { struct yaffs_tnode *tn = yaffs_alloc_raw_tnode(dev); + if (tn) { memset(tn, 0, dev->tnode_size); dev->n_tnodes++; @@ -692,7 +774,7 @@ void yaffs_load_tnode_0(struct yaffs_dev *dev, struct yaffs_tnode *tn, bit_in_word = (32 - bit_in_word); word_in_map++;; mask = - dev->tnode_mask >> ( /*dev->tnode_width - */ bit_in_word); + dev->tnode_mask >> bit_in_word; map[word_in_map] &= ~mask; map[word_in_map] |= (mask & (val >> bit_in_word)); } @@ -778,15 +860,16 @@ struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev, return tn; } -/* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree. +/* add_find_tnode_0 finds the level 0 tnode if it exists, + * otherwise first expands the tree. * This happens in two steps: * 1. If the tree isn't tall enough, then make it taller. * 2. Scan down the tree towards the level 0 tnode adding tnodes if required. * * Used when modifying the tree. * - * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will - * be plugged into the ttree. + * If the tn argument is NULL, then a fresh tnode will be added otherwise the + * specified tn will be plugged into the ttree. */ struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev, @@ -798,7 +881,6 @@ struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev, int i; int l; struct yaffs_tnode *tn; - u32 x; /* Check sane level and page Id */ @@ -829,8 +911,8 @@ struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev, file_struct->top = tn; file_struct->top_level++; } else { - T(YAFFS_TRACE_ERROR, - (TSTR("yaffs: no more tnodes" TENDSTR))); + yaffs_trace(YAFFS_TRACE_ERROR, + "yaffs: no more tnodes"); return NULL; } } @@ -856,11 +938,10 @@ struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev, } else if (l == 1) { /* Looking from level 1 at level 0 */ if (passed_tn) { - /* If we already have one, then release it. */ + /* If we already have one, release it */ if (tn->internal[x]) yaffs_free_tnode(dev, - tn-> - internal[x]); + tn->internal[x]); tn->internal[x] = passed_tn; } else if (!tn->internal[x]) { @@ -886,9 +967,17 @@ struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev, return tn; } +static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id, + int chunk_obj) +{ + return (tags->chunk_id == chunk_obj && + tags->obj_id == obj_id && !tags->is_deleted) ? 1 : 0; + +} + static int yaffs_find_chunk_in_group(struct yaffs_dev *dev, int the_chunk, - struct yaffs_ext_tags *tags, int obj_id, - int inode_chunk) + struct yaffs_ext_tags *tags, int obj_id, + int inode_chunk) { int j; @@ -902,7 +991,8 @@ static int yaffs_find_chunk_in_group(struct yaffs_dev *dev, int the_chunk, else { yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL, tags); - if (yaffs_tags_match(tags, obj_id, inode_chunk)) { + if (yaffs_tags_match(tags, + obj_id, inode_chunk)) { /* found it; */ return the_chunk; } @@ -913,1641 +1003,6 @@ static int yaffs_find_chunk_in_group(struct yaffs_dev *dev, int the_chunk, return -1; } -static void yaffs_soft_del_chunk(struct yaffs_dev *dev, int chunk) -{ - struct yaffs_block_info *the_block; - unsigned block_no; - - T(YAFFS_TRACE_DELETION, (TSTR("soft delete chunk %d" TENDSTR), chunk)); - - block_no = chunk / dev->param.chunks_per_block; - the_block = yaffs_get_block_info(dev, block_no); - if (the_block) { - the_block->soft_del_pages++; - dev->n_free_chunks++; - yaffs2_update_oldest_dirty_seq(dev, block_no, the_block); - } -} - -/* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file. - * All soft deleting does is increment the block's softdelete count and pulls the chunk out - * of the tnode. - * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted. - */ - -static int yaffs_soft_del_worker(struct yaffs_obj *in, struct yaffs_tnode *tn, - u32 level, int chunk_offset) -{ - int i; - int the_chunk; - int all_done = 1; - struct yaffs_dev *dev = in->my_dev; - - if (tn) { - if (level > 0) { - - for (i = YAFFS_NTNODES_INTERNAL - 1; all_done && i >= 0; - i--) { - if (tn->internal[i]) { - all_done = - yaffs_soft_del_worker(in, - tn->internal - [i], - level - 1, - (chunk_offset - << - YAFFS_TNODES_INTERNAL_BITS) - + i); - if (all_done) { - yaffs_free_tnode(dev, - tn->internal - [i]); - tn->internal[i] = NULL; - } else { - /* Hoosterman... how could this happen? */ - } - } - } - return (all_done) ? 1 : 0; - } else if (level == 0) { - - for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) { - the_chunk = yaffs_get_group_base(dev, tn, i); - if (the_chunk) { - /* Note this does not find the real chunk, only the chunk group. - * We make an assumption that a chunk group is not larger than - * a block. - */ - yaffs_soft_del_chunk(dev, the_chunk); - yaffs_load_tnode_0(dev, tn, i, 0); - } - - } - return 1; - - } - - } - - return 1; - -} - -static void yaffs_soft_del_file(struct yaffs_obj *obj) -{ - if (obj->deleted && - obj->variant_type == YAFFS_OBJECT_TYPE_FILE && !obj->soft_del) { - if (obj->n_data_chunks <= 0) { - /* Empty file with no duplicate object headers, just delete it immediately */ - yaffs_free_tnode(obj->my_dev, - obj->variant.file_variant.top); - obj->variant.file_variant.top = NULL; - T(YAFFS_TRACE_TRACING, - (TSTR("yaffs: Deleting empty file %d" TENDSTR), - obj->obj_id)); - yaffs_generic_obj_del(obj); - } else { - yaffs_soft_del_worker(obj, - obj->variant.file_variant.top, - obj->variant. - file_variant.top_level, 0); - obj->soft_del = 1; - } - } -} - -/* Pruning removes any part of the file structure tree that is beyond the - * bounds of the file (ie that does not point to chunks). - * - * A file should only get pruned when its size is reduced. - * - * Before pruning, the chunks must be pulled from the tree and the - * level 0 tnode entries must be zeroed out. - * Could also use this for file deletion, but that's probably better handled - * by a special case. - * - * This function is recursive. For levels > 0 the function is called again on - * any sub-tree. For level == 0 we just check if the sub-tree has data. - * If there is no data in a subtree then it is pruned. - */ - -static struct yaffs_tnode *yaffs_prune_worker(struct yaffs_dev *dev, - struct yaffs_tnode *tn, u32 level, - int del0) -{ - int i; - int has_data; - - if (tn) { - has_data = 0; - - if (level > 0) { - for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) { - if (tn->internal[i]) { - tn->internal[i] = - yaffs_prune_worker(dev, - tn->internal[i], - level - 1, - (i == - 0) ? del0 : 1); - } - - if (tn->internal[i]) - has_data++; - } - } else { - int tnode_size_u32 = dev->tnode_size / sizeof(u32); - u32 *map = (u32 *) tn; - - for (i = 0; !has_data && i < tnode_size_u32; i++) { - if (map[i]) - has_data++; - } - } - - if (has_data == 0 && del0) { - /* Free and return NULL */ - - yaffs_free_tnode(dev, tn); - tn = NULL; - } - - } - - return tn; - -} - -static int yaffs_prune_tree(struct yaffs_dev *dev, - struct yaffs_file_var *file_struct) -{ - int i; - int has_data; - int done = 0; - struct yaffs_tnode *tn; - - if (file_struct->top_level > 0) { - file_struct->top = - yaffs_prune_worker(dev, file_struct->top, - file_struct->top_level, 0); - - /* Now we have a tree with all the non-zero branches NULL but the height - * is the same as it was. - * Let's see if we can trim internal tnodes to shorten the tree. - * We can do this if only the 0th element in the tnode is in use - * (ie all the non-zero are NULL) - */ - - while (file_struct->top_level && !done) { - tn = file_struct->top; - - has_data = 0; - for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) { - if (tn->internal[i]) - has_data++; - } - - if (!has_data) { - file_struct->top = tn->internal[0]; - file_struct->top_level--; - yaffs_free_tnode(dev, tn); - } else { - done = 1; - } - } - } - - return YAFFS_OK; -} - -/*-------------------- End of File Structure functions.-------------------*/ - -/* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */ -static struct yaffs_obj *yaffs_alloc_empty_obj(struct yaffs_dev *dev) -{ - struct yaffs_obj *obj = yaffs_alloc_raw_obj(dev); - - if (obj) { - dev->n_obj++; - - /* Now sweeten it up... */ - - memset(obj, 0, sizeof(struct yaffs_obj)); - obj->being_created = 1; - - obj->my_dev = dev; - obj->hdr_chunk = 0; - obj->variant_type = YAFFS_OBJECT_TYPE_UNKNOWN; - INIT_LIST_HEAD(&(obj->hard_links)); - INIT_LIST_HEAD(&(obj->hash_link)); - INIT_LIST_HEAD(&obj->siblings); - - /* Now make the directory sane */ - if (dev->root_dir) { - obj->parent = dev->root_dir; - list_add(&(obj->siblings), - &dev->root_dir->variant.dir_variant.children); - } - - /* Add it to the lost and found directory. - * NB Can't put root or lost-n-found in lost-n-found so - * check if lost-n-found exists first - */ - if (dev->lost_n_found) - yaffs_add_obj_to_dir(dev->lost_n_found, obj); - - obj->being_created = 0; - } - - dev->checkpoint_blocks_required = 0; /* force recalculation */ - - return obj; -} - -static struct yaffs_obj *yaffs_create_fake_dir(struct yaffs_dev *dev, - int number, u32 mode) -{ - - struct yaffs_obj *obj = - yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY); - if (obj) { - obj->fake = 1; /* it is fake so it might have no NAND presence... */ - obj->rename_allowed = 0; /* ... and we're not allowed to rename it... */ - obj->unlink_allowed = 0; /* ... or unlink it */ - obj->deleted = 0; - obj->unlinked = 0; - obj->yst_mode = mode; - obj->my_dev = dev; - obj->hdr_chunk = 0; /* Not a valid chunk. */ - } - - return obj; - -} - -static void yaffs_unhash_obj(struct yaffs_obj *obj) -{ - int bucket; - struct yaffs_dev *dev = obj->my_dev; - - /* If it is still linked into the bucket list, free from the list */ - if (!list_empty(&obj->hash_link)) { - list_del_init(&obj->hash_link); - bucket = yaffs_hash_fn(obj->obj_id); - dev->obj_bucket[bucket].count--; - } -} - -/* FreeObject frees up a Object and puts it back on the free list */ -static void yaffs_free_obj(struct yaffs_obj *obj) -{ - struct yaffs_dev *dev = obj->my_dev; - - T(YAFFS_TRACE_OS, - (TSTR("FreeObject %p inode %p" TENDSTR), obj, obj->my_inode)); - - if (!obj) - YBUG(); - if (obj->parent) - YBUG(); - if (!list_empty(&obj->siblings)) - YBUG(); - - if (obj->my_inode) { - /* We're still hooked up to a cached inode. - * Don't delete now, but mark for later deletion - */ - obj->defered_free = 1; - return; - } - - yaffs_unhash_obj(obj); - - yaffs_free_raw_obj(dev, obj); - dev->n_obj--; - dev->checkpoint_blocks_required = 0; /* force recalculation */ -} - -void yaffs_handle_defered_free(struct yaffs_obj *obj) -{ - if (obj->defered_free) - yaffs_free_obj(obj); -} - -static void yaffs_init_tnodes_and_objs(struct yaffs_dev *dev) -{ - int i; - - dev->n_obj = 0; - dev->n_tnodes = 0; - - yaffs_init_raw_tnodes_and_objs(dev); - - for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) { - INIT_LIST_HEAD(&dev->obj_bucket[i].list); - dev->obj_bucket[i].count = 0; - } -} - -static int yaffs_find_nice_bucket(struct yaffs_dev *dev) -{ - int i; - int l = 999; - int lowest = 999999; - - /* Search for the shortest list or one that - * isn't too long. - */ - - for (i = 0; i < 10 && lowest > 4; i++) { - dev->bucket_finder++; - dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS; - if (dev->obj_bucket[dev->bucket_finder].count < lowest) { - lowest = dev->obj_bucket[dev->bucket_finder].count; - l = dev->bucket_finder; - } - - } - - return l; -} - -static int yaffs_new_obj_id(struct yaffs_dev *dev) -{ - int bucket = yaffs_find_nice_bucket(dev); - - /* Now find an object value that has not already been taken - * by scanning the list. - */ - - int found = 0; - struct list_head *i; - - u32 n = (u32) bucket; - - /* yaffs_check_obj_hash_sane(); */ - - while (!found) { - found = 1; - n += YAFFS_NOBJECT_BUCKETS; - if (1 || dev->obj_bucket[bucket].count > 0) { - list_for_each(i, &dev->obj_bucket[bucket].list) { - /* If there is already one in the list */ - if (i && list_entry(i, struct yaffs_obj, - hash_link)->obj_id == n) { - found = 0; - } - } - } - } - - return n; -} - -static void yaffs_hash_obj(struct yaffs_obj *in) -{ - int bucket = yaffs_hash_fn(in->obj_id); - struct yaffs_dev *dev = in->my_dev; - - list_add(&in->hash_link, &dev->obj_bucket[bucket].list); - dev->obj_bucket[bucket].count++; -} - -struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number) -{ - int bucket = yaffs_hash_fn(number); - struct list_head *i; - struct yaffs_obj *in; - - list_for_each(i, &dev->obj_bucket[bucket].list) { - /* Look if it is in the list */ - if (i) { - in = list_entry(i, struct yaffs_obj, hash_link); - if (in->obj_id == number) { - - /* Don't tell the VFS about this one if it is defered free */ - if (in->defered_free) - return NULL; - - return in; - } - } - } - - return NULL; -} - -struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number, - enum yaffs_obj_type type) -{ - struct yaffs_obj *the_obj = NULL; - struct yaffs_tnode *tn = NULL; - - if (number < 0) - number = yaffs_new_obj_id(dev); - - if (type == YAFFS_OBJECT_TYPE_FILE) { - tn = yaffs_get_tnode(dev); - if (!tn) - return NULL; - } - - the_obj = yaffs_alloc_empty_obj(dev); - if (!the_obj) { - if (tn) - yaffs_free_tnode(dev, tn); - return NULL; - } - - if (the_obj) { - the_obj->fake = 0; - the_obj->rename_allowed = 1; - the_obj->unlink_allowed = 1; - the_obj->obj_id = number; - yaffs_hash_obj(the_obj); - the_obj->variant_type = type; - yaffs_load_current_time(the_obj, 1, 1); - - switch (type) { - case YAFFS_OBJECT_TYPE_FILE: - the_obj->variant.file_variant.file_size = 0; - the_obj->variant.file_variant.scanned_size = 0; - the_obj->variant.file_variant.shrink_size = ~0; /* max */ - the_obj->variant.file_variant.top_level = 0; - the_obj->variant.file_variant.top = tn; - break; - case YAFFS_OBJECT_TYPE_DIRECTORY: - INIT_LIST_HEAD(&the_obj->variant.dir_variant.children); - INIT_LIST_HEAD(&the_obj->variant.dir_variant.dirty); - break; - case YAFFS_OBJECT_TYPE_SYMLINK: - case YAFFS_OBJECT_TYPE_HARDLINK: - case YAFFS_OBJECT_TYPE_SPECIAL: - /* No action required */ - break; - case YAFFS_OBJECT_TYPE_UNKNOWN: - /* todo this should not happen */ - break; - } - } - - return the_obj; -} - -struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev, - int number, - enum yaffs_obj_type type) -{ - struct yaffs_obj *the_obj = NULL; - - if (number > 0) - the_obj = yaffs_find_by_number(dev, number); - - if (!the_obj) - the_obj = yaffs_new_obj(dev, number, type); - - return the_obj; - -} - -YCHAR *yaffs_clone_str(const YCHAR * str) -{ - YCHAR *new_str = NULL; - int len; - - if (!str) - str = _Y(""); - - len = yaffs_strnlen(str, YAFFS_MAX_ALIAS_LENGTH); - new_str = YMALLOC((len + 1) * sizeof(YCHAR)); - if (new_str) { - yaffs_strncpy(new_str, str, len); - new_str[len] = 0; - } - return new_str; - -} - -/* - * Mknod (create) a new object. - * equiv_obj only has meaning for a hard link; - * alias_str only has meaning for a symlink. - * rdev only has meaning for devices (a subset of special objects) - */ - -static struct yaffs_obj *yaffs_create_obj(enum yaffs_obj_type type, - struct yaffs_obj *parent, - const YCHAR * name, - u32 mode, - u32 uid, - u32 gid, - struct yaffs_obj *equiv_obj, - const YCHAR * alias_str, u32 rdev) -{ - struct yaffs_obj *in; - YCHAR *str = NULL; - - struct yaffs_dev *dev = parent->my_dev; - - /* Check if the entry exists. If it does then fail the call since we don't want a dup. */ - if (yaffs_find_by_name(parent, name)) - return NULL; - - if (type == YAFFS_OBJECT_TYPE_SYMLINK) { - str = yaffs_clone_str(alias_str); - if (!str) - return NULL; - } - - in = yaffs_new_obj(dev, -1, type); - - if (!in) { - if (str) - YFREE(str); - return NULL; - } - - if (in) { - in->hdr_chunk = 0; - in->valid = 1; - in->variant_type = type; - - in->yst_mode = mode; - - yaffs_attribs_init(in, gid, uid, rdev); - - in->n_data_chunks = 0; - - yaffs_set_obj_name(in, name); - in->dirty = 1; - - yaffs_add_obj_to_dir(parent, in); - - in->my_dev = parent->my_dev; - - switch (type) { - case YAFFS_OBJECT_TYPE_SYMLINK: - in->variant.symlink_variant.alias = str; - break; - case YAFFS_OBJECT_TYPE_HARDLINK: - in->variant.hardlink_variant.equiv_obj = equiv_obj; - in->variant.hardlink_variant.equiv_id = - equiv_obj->obj_id; - list_add(&in->hard_links, &equiv_obj->hard_links); - break; - case YAFFS_OBJECT_TYPE_FILE: - case YAFFS_OBJECT_TYPE_DIRECTORY: - case YAFFS_OBJECT_TYPE_SPECIAL: - case YAFFS_OBJECT_TYPE_UNKNOWN: - /* do nothing */ - break; - } - - if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) { - /* Could not create the object header, fail the creation */ - yaffs_del_obj(in); - in = NULL; - } - - yaffs_update_parent(parent); - } - - return in; -} - -struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent, - const YCHAR * name, u32 mode, u32 uid, - u32 gid) -{ - return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode, - uid, gid, NULL, NULL, 0); -} - -struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR * name, - u32 mode, u32 uid, u32 gid) -{ - return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name, - mode, uid, gid, NULL, NULL, 0); -} - -struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent, - const YCHAR * name, u32 mode, u32 uid, - u32 gid, u32 rdev) -{ - return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode, - uid, gid, NULL, NULL, rdev); -} - -struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent, - const YCHAR * name, u32 mode, u32 uid, - u32 gid, const YCHAR * alias) -{ - return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode, - uid, gid, NULL, alias, 0); -} - -/* yaffs_link_obj returns the object id of the equivalent object.*/ -struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name, - struct yaffs_obj *equiv_obj) -{ - /* Get the real object in case we were fed a hard link as an equivalent object */ - equiv_obj = yaffs_get_equivalent_obj(equiv_obj); - - if (yaffs_create_obj - (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0, - equiv_obj, NULL, 0)) { - return equiv_obj; - } else { - return NULL; - } - -} - -static int yaffs_change_obj_name(struct yaffs_obj *obj, - struct yaffs_obj *new_dir, - const YCHAR * new_name, int force, int shadows) -{ - int unlink_op; - int del_op; - - struct yaffs_obj *existing_target; - - if (new_dir == NULL) - new_dir = obj->parent; /* use the old directory */ - - if (new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) { - T(YAFFS_TRACE_ALWAYS, - (TSTR - ("tragedy: yaffs_change_obj_name: new_dir is not a directory" - TENDSTR))); - YBUG(); - } - - /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */ - if (obj->my_dev->param.is_yaffs2) - unlink_op = (new_dir == obj->my_dev->unlinked_dir); - else - unlink_op = (new_dir == obj->my_dev->unlinked_dir - && obj->variant_type == YAFFS_OBJECT_TYPE_FILE); - - del_op = (new_dir == obj->my_dev->del_dir); - - existing_target = yaffs_find_by_name(new_dir, new_name); - - /* If the object is a file going into the unlinked directory, - * then it is OK to just stuff it in since duplicate names are allowed. - * else only proceed if the new name does not exist and if we're putting - * it into a directory. - */ - if ((unlink_op || - del_op || - force || - (shadows > 0) || - !existing_target) && - new_dir->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) { - yaffs_set_obj_name(obj, new_name); - obj->dirty = 1; - - yaffs_add_obj_to_dir(new_dir, obj); - - if (unlink_op) - obj->unlinked = 1; - - /* If it is a deletion then we mark it as a shrink for gc purposes. */ - if (yaffs_update_oh(obj, new_name, 0, del_op, shadows, NULL) >= - 0) - return YAFFS_OK; - } - - return YAFFS_FAIL; -} - -int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR * old_name, - struct yaffs_obj *new_dir, const YCHAR * new_name) -{ - struct yaffs_obj *obj = NULL; - struct yaffs_obj *existing_target = NULL; - int force = 0; - int result; - struct yaffs_dev *dev; - - if (!old_dir || old_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) - YBUG(); - if (!new_dir || new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) - YBUG(); - - dev = old_dir->my_dev; - -#ifdef CONFIG_YAFFS_CASE_INSENSITIVE - /* Special case for case insemsitive systems. - * While look-up is case insensitive, the name isn't. - * Therefore we might want to change x.txt to X.txt - */ - if (old_dir == new_dir && yaffs_strcmp(old_name, new_name) == 0) - force = 1; -#endif - - if (yaffs_strnlen(new_name, YAFFS_MAX_NAME_LENGTH + 1) > - YAFFS_MAX_NAME_LENGTH) - /* ENAMETOOLONG */ - return YAFFS_FAIL; - - obj = yaffs_find_by_name(old_dir, old_name); - - if (obj && obj->rename_allowed) { - - /* Now do the handling for an existing target, if there is one */ - - existing_target = yaffs_find_by_name(new_dir, new_name); - if (existing_target && - existing_target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY - && !list_empty(&existing_target->variant.dir_variant. - children)) { - /* There is a target that is a non-empty directory, so we fail */ - return YAFFS_FAIL; /* EEXIST or ENOTEMPTY */ - } else if (existing_target && existing_target != obj) { - /* Nuke the target first, using shadowing, - * but only if it isn't the same object. - * - * Note we must disable gc otherwise it can mess up the shadowing. - * - */ - dev->gc_disable = 1; - yaffs_change_obj_name(obj, new_dir, new_name, force, - existing_target->obj_id); - existing_target->is_shadowed = 1; - yaffs_unlink_obj(existing_target); - dev->gc_disable = 0; - } - - result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0); - - yaffs_update_parent(old_dir); - if (new_dir != old_dir) - yaffs_update_parent(new_dir); - - return result; - } - return YAFFS_FAIL; -} - -/*------------------------- Block Management and Page Allocation ----------------*/ - -static int yaffs_init_blocks(struct yaffs_dev *dev) -{ - int n_blocks = dev->internal_end_block - dev->internal_start_block + 1; - - dev->block_info = NULL; - dev->chunk_bits = NULL; - - dev->alloc_block = -1; /* force it to get a new one */ - - /* If the first allocation strategy fails, thry the alternate one */ - dev->block_info = YMALLOC(n_blocks * sizeof(struct yaffs_block_info)); - if (!dev->block_info) { - dev->block_info = - YMALLOC_ALT(n_blocks * sizeof(struct yaffs_block_info)); - dev->block_info_alt = 1; - } else - dev->block_info_alt = 0; - - if (dev->block_info) { - /* Set up dynamic blockinfo stuff. */ - dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8; /* round up bytes */ - dev->chunk_bits = YMALLOC(dev->chunk_bit_stride * n_blocks); - if (!dev->chunk_bits) { - dev->chunk_bits = - YMALLOC_ALT(dev->chunk_bit_stride * n_blocks); - dev->chunk_bits_alt = 1; - } else - dev->chunk_bits_alt = 0; - } - - if (dev->block_info && dev->chunk_bits) { - memset(dev->block_info, 0, - n_blocks * sizeof(struct yaffs_block_info)); - memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks); - return YAFFS_OK; - } - - return YAFFS_FAIL; -} - -static void yaffs_deinit_blocks(struct yaffs_dev *dev) -{ - if (dev->block_info_alt && dev->block_info) - YFREE_ALT(dev->block_info); - else if (dev->block_info) - YFREE(dev->block_info); - - dev->block_info_alt = 0; - - dev->block_info = NULL; - - if (dev->chunk_bits_alt && dev->chunk_bits) - YFREE_ALT(dev->chunk_bits); - else if (dev->chunk_bits) - YFREE(dev->chunk_bits); - dev->chunk_bits_alt = 0; - dev->chunk_bits = NULL; -} - -void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no) -{ - struct yaffs_block_info *bi = yaffs_get_block_info(dev, block_no); - - int erased_ok = 0; - - /* If the block is still healthy erase it and mark as clean. - * If the block has had a data failure, then retire it. - */ - - T(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE, - (TSTR("yaffs_block_became_dirty block %d state %d %s" TENDSTR), - block_no, bi->block_state, - (bi->needs_retiring) ? "needs retiring" : "")); - - yaffs2_clear_oldest_dirty_seq(dev, bi); - - bi->block_state = YAFFS_BLOCK_STATE_DIRTY; - - /* If this is the block being garbage collected then stop gc'ing this block */ - if (block_no == dev->gc_block) - dev->gc_block = 0; - - /* If this block is currently the best candidate for gc then drop as a candidate */ - if (block_no == dev->gc_dirtiest) { - dev->gc_dirtiest = 0; - dev->gc_pages_in_use = 0; - } - - if (!bi->needs_retiring) { - yaffs2_checkpt_invalidate(dev); - erased_ok = yaffs_erase_block(dev, block_no); - if (!erased_ok) { - dev->n_erase_failures++; - T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS, - (TSTR("**>> Erasure failed %d" TENDSTR), block_no)); - } - } - - if (erased_ok && - ((yaffs_trace_mask & YAFFS_TRACE_ERASE) - || !yaffs_skip_verification(dev))) { - int i; - for (i = 0; i < dev->param.chunks_per_block; i++) { - if (!yaffs_check_chunk_erased - (dev, block_no * dev->param.chunks_per_block + i)) { - T(YAFFS_TRACE_ERROR, - (TSTR - (">>Block %d erasure supposedly OK, but chunk %d not erased" - TENDSTR), block_no, i)); - } - } - } - - if (erased_ok) { - /* Clean it up... */ - bi->block_state = YAFFS_BLOCK_STATE_EMPTY; - bi->seq_number = 0; - dev->n_erased_blocks++; - bi->pages_in_use = 0; - bi->soft_del_pages = 0; - bi->has_shrink_hdr = 0; - bi->skip_erased_check = 1; /* This is clean, so no need to check */ - bi->gc_prioritise = 0; - yaffs_clear_chunk_bits(dev, block_no); - - T(YAFFS_TRACE_ERASE, - (TSTR("Erased block %d" TENDSTR), block_no)); - } else { - dev->n_free_chunks -= dev->param.chunks_per_block; /* We lost a block of free space */ - - yaffs_retire_block(dev, block_no); - T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS, - (TSTR("**>> Block %d retired" TENDSTR), block_no)); - } -} - -static int yaffs_find_alloc_block(struct yaffs_dev *dev) -{ - int i; - - struct yaffs_block_info *bi; - - if (dev->n_erased_blocks < 1) { - /* Hoosterman we've got a problem. - * Can't get space to gc - */ - T(YAFFS_TRACE_ERROR, - (TSTR("yaffs tragedy: no more erased blocks" TENDSTR))); - - return -1; - } - - /* Find an empty block. */ - - for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) { - dev->alloc_block_finder++; - if (dev->alloc_block_finder < dev->internal_start_block - || dev->alloc_block_finder > dev->internal_end_block) { - dev->alloc_block_finder = dev->internal_start_block; - } - - bi = yaffs_get_block_info(dev, dev->alloc_block_finder); - - if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) { - bi->block_state = YAFFS_BLOCK_STATE_ALLOCATING; - dev->seq_number++; - bi->seq_number = dev->seq_number; - dev->n_erased_blocks--; - T(YAFFS_TRACE_ALLOCATE, - (TSTR("Allocated block %d, seq %d, %d left" TENDSTR), - dev->alloc_block_finder, dev->seq_number, - dev->n_erased_blocks)); - return dev->alloc_block_finder; - } - } - - T(YAFFS_TRACE_ALWAYS, - (TSTR - ("yaffs tragedy: no more erased blocks, but there should have been %d" - TENDSTR), dev->n_erased_blocks)); - - return -1; -} - -/* - * Check if there's space to allocate... - * Thinks.... do we need top make this ths same as yaffs_get_free_chunks()? - */ -int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks) -{ - int reserved_chunks; - int reserved_blocks = dev->param.n_reserved_blocks; - int checkpt_blocks; - - checkpt_blocks = yaffs_calc_checkpt_blocks_required(dev); - - reserved_chunks = - ((reserved_blocks + checkpt_blocks) * dev->param.chunks_per_block); - - return (dev->n_free_chunks > (reserved_chunks + n_chunks)); -} - -static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver, - struct yaffs_block_info **block_ptr) -{ - int ret_val; - struct yaffs_block_info *bi; - - if (dev->alloc_block < 0) { - /* Get next block to allocate off */ - dev->alloc_block = yaffs_find_alloc_block(dev); - dev->alloc_page = 0; - } - - if (!use_reserver && !yaffs_check_alloc_available(dev, 1)) { - /* Not enough space to allocate unless we're allowed to use the reserve. */ - return -1; - } - - if (dev->n_erased_blocks < dev->param.n_reserved_blocks - && dev->alloc_page == 0) { - T(YAFFS_TRACE_ALLOCATE, (TSTR("Allocating reserve" TENDSTR))); - } - - /* Next page please.... */ - if (dev->alloc_block >= 0) { - bi = yaffs_get_block_info(dev, dev->alloc_block); - - ret_val = (dev->alloc_block * dev->param.chunks_per_block) + - dev->alloc_page; - bi->pages_in_use++; - yaffs_set_chunk_bit(dev, dev->alloc_block, dev->alloc_page); - - dev->alloc_page++; - - dev->n_free_chunks--; - - /* If the block is full set the state to full */ - if (dev->alloc_page >= dev->param.chunks_per_block) { - bi->block_state = YAFFS_BLOCK_STATE_FULL; - dev->alloc_block = -1; - } - - if (block_ptr) - *block_ptr = bi; - - return ret_val; - } - - T(YAFFS_TRACE_ERROR, - (TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR))); - - return -1; -} - -static int yaffs_get_erased_chunks(struct yaffs_dev *dev) -{ - int n; - - n = dev->n_erased_blocks * dev->param.chunks_per_block; - - if (dev->alloc_block > 0) - n += (dev->param.chunks_per_block - dev->alloc_page); - - return n; - -} - -/* - * yaffs_skip_rest_of_block() skips over the rest of the allocation block - * if we don't want to write to it. - */ -void yaffs_skip_rest_of_block(struct yaffs_dev *dev) -{ - if (dev->alloc_block > 0) { - struct yaffs_block_info *bi = - yaffs_get_block_info(dev, dev->alloc_block); - if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) { - bi->block_state = YAFFS_BLOCK_STATE_FULL; - dev->alloc_block = -1; - } - } -} - -static int yaffs_gc_block(struct yaffs_dev *dev, int block, int whole_block) -{ - int old_chunk; - int new_chunk; - int mark_flash; - int ret_val = YAFFS_OK; - int i; - int is_checkpt_block; - int matching_chunk; - int max_copies; - - int chunks_before = yaffs_get_erased_chunks(dev); - int chunks_after; - - struct yaffs_ext_tags tags; - - struct yaffs_block_info *bi = yaffs_get_block_info(dev, block); - - struct yaffs_obj *object; - - is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT); - - T(YAFFS_TRACE_TRACING, - (TSTR - ("Collecting block %d, in use %d, shrink %d, whole_block %d" - TENDSTR), block, bi->pages_in_use, bi->has_shrink_hdr, - whole_block)); - - /*yaffs_verify_free_chunks(dev); */ - - if (bi->block_state == YAFFS_BLOCK_STATE_FULL) - bi->block_state = YAFFS_BLOCK_STATE_COLLECTING; - - bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */ - - dev->gc_disable = 1; - - if (is_checkpt_block || !yaffs_still_some_chunks(dev, block)) { - T(YAFFS_TRACE_TRACING, - (TSTR - ("Collecting block %d that has no chunks in use" TENDSTR), - block)); - yaffs_block_became_dirty(dev, block); - } else { - - u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__); - - yaffs_verify_blk(dev, bi, block); - - max_copies = (whole_block) ? dev->param.chunks_per_block : 5; - old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk; - - for ( /* init already done */ ; - ret_val == YAFFS_OK && - dev->gc_chunk < dev->param.chunks_per_block && - (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) && - max_copies > 0; dev->gc_chunk++, old_chunk++) { - if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) { - - /* This page is in use and might need to be copied off */ - - max_copies--; - - mark_flash = 1; - - yaffs_init_tags(&tags); - - yaffs_rd_chunk_tags_nand(dev, old_chunk, - buffer, &tags); - - object = yaffs_find_by_number(dev, tags.obj_id); - - T(YAFFS_TRACE_GC_DETAIL, - (TSTR - ("Collecting chunk in block %d, %d %d %d " - TENDSTR), dev->gc_chunk, tags.obj_id, - tags.chunk_id, tags.n_bytes)); - - if (object && !yaffs_skip_verification(dev)) { - if (tags.chunk_id == 0) - matching_chunk = - object->hdr_chunk; - else if (object->soft_del) - matching_chunk = old_chunk; /* Defeat the test */ - else - matching_chunk = - yaffs_find_chunk_in_file - (object, tags.chunk_id, - NULL); - - if (old_chunk != matching_chunk) - T(YAFFS_TRACE_ERROR, - (TSTR - ("gc: page in gc mismatch: %d %d %d %d" - TENDSTR), old_chunk, - matching_chunk, tags.obj_id, - tags.chunk_id)); - - } - - if (!object) { - T(YAFFS_TRACE_ERROR, - (TSTR - ("page %d in gc has no object: %d %d %d " - TENDSTR), old_chunk, - tags.obj_id, tags.chunk_id, - tags.n_bytes)); - } - - if (object && - object->deleted && - object->soft_del && tags.chunk_id != 0) { - /* Data chunk in a soft deleted file, throw it away - * It's a soft deleted data chunk, - * No need to copy this, just forget about it and - * fix up the object. - */ - - /* Free chunks already includes softdeleted chunks. - * How ever this chunk is going to soon be really deleted - * which will increment free chunks. - * We have to decrement free chunks so this works out properly. - */ - dev->n_free_chunks--; - bi->soft_del_pages--; - - object->n_data_chunks--; - - if (object->n_data_chunks <= 0) { - /* remeber to clean up the object */ - dev->gc_cleanup_list[dev-> - n_clean_ups] - = tags.obj_id; - dev->n_clean_ups++; - } - mark_flash = 0; - } else if (0) { - /* Todo object && object->deleted && object->n_data_chunks == 0 */ - /* Deleted object header with no data chunks. - * Can be discarded and the file deleted. - */ - object->hdr_chunk = 0; - yaffs_free_tnode(object->my_dev, - object-> - variant.file_variant. - top); - object->variant.file_variant.top = NULL; - yaffs_generic_obj_del(object); - - } else if (object) { - /* It's either a data chunk in a live file or - * an ObjectHeader, so we're interested in it. - * NB Need to keep the ObjectHeaders of deleted files - * until the whole file has been deleted off - */ - tags.serial_number++; - - dev->n_gc_copies++; - - if (tags.chunk_id == 0) { - /* It is an object Id, - * We need to nuke the shrinkheader flags first - * Also need to clean up shadowing. - * We no longer want the shrink_header flag since its work is done - * and if it is left in place it will mess up scanning. - */ - - struct yaffs_obj_hdr *oh; - oh = (struct yaffs_obj_hdr *) - buffer; - - oh->is_shrink = 0; - tags.extra_is_shrink = 0; - - oh->shadows_obj = 0; - oh->inband_shadowed_obj_id = 0; - tags.extra_shadows = 0; - - /* Update file size */ - if (object->variant_type == - YAFFS_OBJECT_TYPE_FILE) { - oh->file_size = - object->variant. - file_variant. - file_size; - tags.extra_length = - oh->file_size; - } - - yaffs_verify_oh(object, oh, - &tags, 1); - new_chunk = - yaffs_write_new_chunk(dev, - (u8 *) - oh, - &tags, - 1); - } else - new_chunk = - yaffs_write_new_chunk(dev, - buffer, - &tags, - 1); - - if (new_chunk < 0) { - ret_val = YAFFS_FAIL; - } else { - - /* Ok, now fix up the Tnodes etc. */ - - if (tags.chunk_id == 0) { - /* It's a header */ - object->hdr_chunk = - new_chunk; - object->serial = - tags.serial_number; - } else { - /* It's a data chunk */ - int ok; - ok = yaffs_put_chunk_in_file(object, tags.chunk_id, new_chunk, 0); - } - } - } - - if (ret_val == YAFFS_OK) - yaffs_chunk_del(dev, old_chunk, - mark_flash, __LINE__); - - } - } - - yaffs_release_temp_buffer(dev, buffer, __LINE__); - - } - - yaffs_verify_collected_blk(dev, bi, block); - - if (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) { - /* - * The gc did not complete. Set block state back to FULL - * because checkpointing does not restore gc. - */ - bi->block_state = YAFFS_BLOCK_STATE_FULL; - } else { - /* The gc completed. */ - /* Do any required cleanups */ - for (i = 0; i < dev->n_clean_ups; i++) { - /* Time to delete the file too */ - object = - yaffs_find_by_number(dev, dev->gc_cleanup_list[i]); - if (object) { - yaffs_free_tnode(dev, - object->variant. - file_variant.top); - object->variant.file_variant.top = NULL; - T(YAFFS_TRACE_GC, - (TSTR - ("yaffs: About to finally delete object %d" - TENDSTR), object->obj_id)); - yaffs_generic_obj_del(object); - object->my_dev->n_deleted_files--; - } - - } - - chunks_after = yaffs_get_erased_chunks(dev); - if (chunks_before >= chunks_after) { - T(YAFFS_TRACE_GC, - (TSTR - ("gc did not increase free chunks before %d after %d" - TENDSTR), chunks_before, chunks_after)); - } - dev->gc_block = 0; - dev->gc_chunk = 0; - dev->n_clean_ups = 0; - } - - dev->gc_disable = 0; - - return ret_val; -} - -/* - * FindBlockForgarbageCollection is used to select the dirtiest block (or close enough) - * for garbage collection. - */ - -static unsigned yaffs_find_gc_block(struct yaffs_dev *dev, - int aggressive, int background) -{ - int i; - int iterations; - unsigned selected = 0; - int prioritised = 0; - int prioritised_exist = 0; - struct yaffs_block_info *bi; - int threshold; - - /* First let's see if we need to grab a prioritised block */ - if (dev->has_pending_prioritised_gc && !aggressive) { - dev->gc_dirtiest = 0; - bi = dev->block_info; - for (i = dev->internal_start_block; - i <= dev->internal_end_block && !selected; i++) { - - if (bi->gc_prioritise) { - prioritised_exist = 1; - if (bi->block_state == YAFFS_BLOCK_STATE_FULL && - yaffs_block_ok_for_gc(dev, bi)) { - selected = i; - prioritised = 1; - } - } - bi++; - } - - /* - * If there is a prioritised block and none was selected then - * this happened because there is at least one old dirty block gumming - * up the works. Let's gc the oldest dirty block. - */ - - if (prioritised_exist && - !selected && dev->oldest_dirty_block > 0) - selected = dev->oldest_dirty_block; - - if (!prioritised_exist) /* None found, so we can clear this */ - dev->has_pending_prioritised_gc = 0; - } - - /* If we're doing aggressive GC then we are happy to take a less-dirty block, and - * search harder. - * else (we're doing a leasurely gc), then we only bother to do this if the - * block has only a few pages in use. - */ - - if (!selected) { - int pages_used; - int n_blocks = - dev->internal_end_block - dev->internal_start_block + 1; - if (aggressive) { - threshold = dev->param.chunks_per_block; - iterations = n_blocks; - } else { - int max_threshold; - - if (background) - max_threshold = dev->param.chunks_per_block / 2; - else - max_threshold = dev->param.chunks_per_block / 8; - - if (max_threshold < YAFFS_GC_PASSIVE_THRESHOLD) - max_threshold = YAFFS_GC_PASSIVE_THRESHOLD; - - threshold = background ? (dev->gc_not_done + 2) * 2 : 0; - if (threshold < YAFFS_GC_PASSIVE_THRESHOLD) - threshold = YAFFS_GC_PASSIVE_THRESHOLD; - if (threshold > max_threshold) - threshold = max_threshold; - - iterations = n_blocks / 16 + 1; - if (iterations > 100) - iterations = 100; - } - - for (i = 0; - i < iterations && - (dev->gc_dirtiest < 1 || - dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH); i++) { - dev->gc_block_finder++; - if (dev->gc_block_finder < dev->internal_start_block || - dev->gc_block_finder > dev->internal_end_block) - dev->gc_block_finder = - dev->internal_start_block; - - bi = yaffs_get_block_info(dev, dev->gc_block_finder); - - pages_used = bi->pages_in_use - bi->soft_del_pages; - - if (bi->block_state == YAFFS_BLOCK_STATE_FULL && - pages_used < dev->param.chunks_per_block && - (dev->gc_dirtiest < 1 - || pages_used < dev->gc_pages_in_use) - && yaffs_block_ok_for_gc(dev, bi)) { - dev->gc_dirtiest = dev->gc_block_finder; - dev->gc_pages_in_use = pages_used; - } - } - - if (dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold) - selected = dev->gc_dirtiest; - } - - /* - * If nothing has been selected for a while, try selecting the oldest dirty - * because that's gumming up the works. - */ - - if (!selected && dev->param.is_yaffs2 && - dev->gc_not_done >= (background ? 10 : 20)) { - yaffs2_find_oldest_dirty_seq(dev); - if (dev->oldest_dirty_block > 0) { - selected = dev->oldest_dirty_block; - dev->gc_dirtiest = selected; - dev->oldest_dirty_gc_count++; - bi = yaffs_get_block_info(dev, selected); - dev->gc_pages_in_use = - bi->pages_in_use - bi->soft_del_pages; - } else - dev->gc_not_done = 0; - } - - if (selected) { - T(YAFFS_TRACE_GC, - (TSTR - ("GC Selected block %d with %d free, prioritised:%d" - TENDSTR), selected, - dev->param.chunks_per_block - dev->gc_pages_in_use, - prioritised)); - - dev->n_gc_blocks++; - if (background) - dev->bg_gcs++; - - dev->gc_dirtiest = 0; - dev->gc_pages_in_use = 0; - dev->gc_not_done = 0; - if (dev->refresh_skip > 0) - dev->refresh_skip--; - } else { - dev->gc_not_done++; - T(YAFFS_TRACE_GC, - (TSTR - ("GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s" - TENDSTR), dev->gc_block_finder, dev->gc_not_done, threshold, - dev->gc_dirtiest, dev->gc_pages_in_use, - dev->oldest_dirty_block, background ? " bg" : "")); - } - - return selected; -} - -/* New garbage collector - * If we're very low on erased blocks then we do aggressive garbage collection - * otherwise we do "leasurely" garbage collection. - * Aggressive gc looks further (whole array) and will accept less dirty blocks. - * Passive gc only inspects smaller areas and will only accept more dirty blocks. - * - * The idea is to help clear out space in a more spread-out manner. - * Dunno if it really does anything useful. - */ -static int yaffs_check_gc(struct yaffs_dev *dev, int background) -{ - int aggressive = 0; - int gc_ok = YAFFS_OK; - int max_tries = 0; - int min_erased; - int erased_chunks; - int checkpt_block_adjust; - - if (dev->param.gc_control && (dev->param.gc_control(dev) & 1) == 0) - return YAFFS_OK; - - if (dev->gc_disable) { - /* Bail out so we don't get recursive gc */ - return YAFFS_OK; - } - - /* This loop should pass the first time. - * We'll only see looping here if the collection does not increase space. - */ - - do { - max_tries++; - - checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev); - - min_erased = - dev->param.n_reserved_blocks + checkpt_block_adjust + 1; - erased_chunks = - dev->n_erased_blocks * dev->param.chunks_per_block; - - /* If we need a block soon then do aggressive gc. */ - if (dev->n_erased_blocks < min_erased) - aggressive = 1; - else { - if (!background - && erased_chunks > (dev->n_free_chunks / 4)) - break; - - if (dev->gc_skip > 20) - dev->gc_skip = 20; - if (erased_chunks < dev->n_free_chunks / 2 || - dev->gc_skip < 1 || background) - aggressive = 0; - else { - dev->gc_skip--; - break; - } - } - - dev->gc_skip = 5; - - /* If we don't already have a block being gc'd then see if we should start another */ - - if (dev->gc_block < 1 && !aggressive) { - dev->gc_block = yaffs2_find_refresh_block(dev); - dev->gc_chunk = 0; - dev->n_clean_ups = 0; - } - if (dev->gc_block < 1) { - dev->gc_block = - yaffs_find_gc_block(dev, aggressive, background); - dev->gc_chunk = 0; - dev->n_clean_ups = 0; - } - - if (dev->gc_block > 0) { - dev->all_gcs++; - if (!aggressive) - dev->passive_gc_count++; - - T(YAFFS_TRACE_GC, - (TSTR - ("yaffs: GC n_erased_blocks %d aggressive %d" - TENDSTR), dev->n_erased_blocks, aggressive)); - - gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive); - } - - if (dev->n_erased_blocks < (dev->param.n_reserved_blocks) - && dev->gc_block > 0) { - T(YAFFS_TRACE_GC, - (TSTR - ("yaffs: GC !!!no reclaim!!! n_erased_blocks %d after try %d block %d" - TENDSTR), dev->n_erased_blocks, max_tries, - dev->gc_block)); - } - } while ((dev->n_erased_blocks < dev->param.n_reserved_blocks) && - (dev->gc_block > 0) && (max_tries < 2)); - - return aggressive ? gc_ok : YAFFS_OK; -} - -/* - * yaffs_bg_gc() - * Garbage collects. Intended to be called from a background thread. - * Returns non-zero if at least half the free chunks are erased. - */ -int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency) -{ - int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block; - - T(YAFFS_TRACE_BACKGROUND, (TSTR("Background gc %u" TENDSTR), urgency)); - - yaffs_check_gc(dev, 1); - return erased_chunks > dev->n_free_chunks / 2; -} - -/*------------------------- TAGS --------------------------------*/ - -static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id, - int chunk_obj) -{ - return (tags->chunk_id == chunk_obj && - tags->obj_id == obj_id && !tags->is_deleted) ? 1 : 0; - -} - -/*-------------------- Data file manipulation -----------------*/ - static int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk, struct yaffs_ext_tags *tags) { @@ -2556,7 +1011,6 @@ static int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk, int the_chunk = -1; struct yaffs_ext_tags local_tags; int ret_val = -1; - struct yaffs_dev *dev = in->my_dev; if (!tags) { @@ -2583,7 +1037,6 @@ static int yaffs_find_del_file_chunk(struct yaffs_obj *in, int inode_chunk, struct yaffs_tnode *tn; int the_chunk = -1; struct yaffs_ext_tags local_tags; - struct yaffs_dev *dev = in->my_dev; int ret_val = -1; @@ -2628,14 +1081,14 @@ int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk, unsigned existing_serial, new_serial; if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) { - /* Just ignore an attempt at putting a chunk into a non-file during scanning + /* Just ignore an attempt at putting a chunk into a non-file + * during scanning. * If it is not during Scanning then something went wrong! */ if (!in_scan) { - T(YAFFS_TRACE_ERROR, - (TSTR - ("yaffs tragedy:attempt to put data chunk into a non-file" - TENDSTR))); + yaffs_trace(YAFFS_TRACE_ERROR, + "yaffs tragedy:attempt to put data chunk into a non-file" + ); YBUG(); } @@ -2657,23 +1110,29 @@ int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk, if (in_scan != 0) { /* If we're scanning then we need to test for duplicates - * NB This does not need to be efficient since it should only ever + * NB This does not need to be efficient since it should only * happen when the power fails during a write, then only one * chunk should ever be affected. * - * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO - * Update: For backward scanning we don't need to re-read tags so this is quite cheap. + * Correction for YAFFS2: This could happen quite a lot and we + * need to think about efficiency! TODO + * Update: For backward scanning we don't need to re-read tags + * so this is quite cheap. */ if (existing_cunk > 0) { - /* NB Right now existing chunk will not be real chunk_id if the chunk group size > 1 - * thus we have to do a FindChunkInFile to get the real chunk id. + /* NB Right now existing chunk will not be real + * chunk_id if the chunk group size > 1 + * thus we have to do a FindChunkInFile to get the + * real chunk id. * - * We have a duplicate now we need to decide which one to use: + * We have a duplicate now we need to decide which + * one to use: * - * Backwards scanning YAFFS2: The old one is what we use, dump the new one. - * Forward scanning YAFFS2: The new one is what we use, dump the old one. - * YAFFS1: Get both sets of tags and compare serial numbers. + * Backwards scanning YAFFS2: The old one is what + * we use, dump the new one. + * YAFFS1: Get both sets of tags and compare serial + * numbers. */ if (in_scan > 0) { @@ -2691,15 +1150,14 @@ int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk, if (existing_cunk <= 0) { /*Hoosterman - how did this happen? */ - T(YAFFS_TRACE_ERROR, - (TSTR - ("yaffs tragedy: existing chunk < 0 in scan" - TENDSTR))); + yaffs_trace(YAFFS_TRACE_ERROR, + "yaffs tragedy: existing chunk < 0 in scan" + ); } - /* NB The deleted flags should be false, otherwise the chunks will - * not be loaded during a scan + /* NB The deleted flags should be false, otherwise + * the chunks will not be loaded during a scan */ if (in_scan > 0) { @@ -2712,14 +1170,16 @@ int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk, ((existing_serial + 1) & 3) == new_serial)) { /* Forward scanning. * Use new - * Delete the old one and drop through to update the tnode + * Delete the old one and drop through to + * update the tnode */ yaffs_chunk_del(dev, existing_cunk, 1, __LINE__); } else { - /* Backward scanning or we want to use the existing one - * Use existing. - * Delete the new one and return early so that the tnode isn't changed + /* Backward scanning or we want to use the + * existing one + * Delete the new one and return early so that + * the tnode isn't changed */ yaffs_chunk_del(dev, nand_chunk, 1, __LINE__); return YAFFS_OK; @@ -2736,315 +1196,200 @@ int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk, return YAFFS_OK; } -static int yaffs_rd_data_obj(struct yaffs_obj *in, int inode_chunk, u8 * buffer) +static void yaffs_soft_del_chunk(struct yaffs_dev *dev, int chunk) { - int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL); + struct yaffs_block_info *the_block; + unsigned block_no; - if (nand_chunk >= 0) - return yaffs_rd_chunk_tags_nand(in->my_dev, nand_chunk, - buffer, NULL); - else { - T(YAFFS_TRACE_NANDACCESS, - (TSTR("Chunk %d not found zero instead" TENDSTR), - nand_chunk)); - /* get sane (zero) data if you read a hole */ - memset(buffer, 0, in->my_dev->data_bytes_per_chunk); - return 0; - } + yaffs_trace(YAFFS_TRACE_DELETION, "soft delete chunk %d", chunk); -} - -void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash, - int lyn) -{ - int block; - int page; - struct yaffs_ext_tags tags; - struct yaffs_block_info *bi; - - if (chunk_id <= 0) - return; - - dev->n_deletions++; - block = chunk_id / dev->param.chunks_per_block; - page = chunk_id % dev->param.chunks_per_block; - - if (!yaffs_check_chunk_bit(dev, block, page)) - T(YAFFS_TRACE_VERIFY, - (TSTR("Deleting invalid chunk %d" TENDSTR), chunk_id)); - - bi = yaffs_get_block_info(dev, block); - - yaffs2_update_oldest_dirty_seq(dev, block, bi); - - T(YAFFS_TRACE_DELETION, - (TSTR("line %d delete of chunk %d" TENDSTR), lyn, chunk_id)); - - if (!dev->param.is_yaffs2 && mark_flash && - bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) { - - yaffs_init_tags(&tags); - - tags.is_deleted = 1; - - yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags); - yaffs_handle_chunk_update(dev, chunk_id, &tags); - } else { - dev->n_unmarked_deletions++; - } - - /* Pull out of the management area. - * If the whole block became dirty, this will kick off an erasure. - */ - if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING || - bi->block_state == YAFFS_BLOCK_STATE_FULL || - bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCANNING || - bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) { + block_no = chunk / dev->param.chunks_per_block; + the_block = yaffs_get_block_info(dev, block_no); + if (the_block) { + the_block->soft_del_pages++; dev->n_free_chunks++; - - yaffs_clear_chunk_bit(dev, block, page); - - bi->pages_in_use--; - - if (bi->pages_in_use == 0 && - !bi->has_shrink_hdr && - bi->block_state != YAFFS_BLOCK_STATE_ALLOCATING && - bi->block_state != YAFFS_BLOCK_STATE_NEEDS_SCANNING) { - yaffs_block_became_dirty(dev, block); - } - + yaffs2_update_oldest_dirty_seq(dev, block_no, the_block); } - } -static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk, - const u8 * buffer, int n_bytes, int use_reserve) +/* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all + * the chunks in the file. + * All soft deleting does is increment the block's softdelete count and pulls + * the chunk out of the tnode. + * Thus, essentially this is the same as DeleteWorker except that the chunks + * are soft deleted. + */ + +static int yaffs_soft_del_worker(struct yaffs_obj *in, struct yaffs_tnode *tn, + u32 level, int chunk_offset) { - /* Find old chunk Need to do this to get serial number - * Write new one and patch into tree. - * Invalidate old tags. - */ - - int prev_chunk_id; - struct yaffs_ext_tags prev_tags; - - int new_chunk_id; - struct yaffs_ext_tags new_tags; - + int i; + int the_chunk; + int all_done = 1; struct yaffs_dev *dev = in->my_dev; - yaffs_check_gc(dev, 0); + if (tn) { + if (level > 0) { + for (i = YAFFS_NTNODES_INTERNAL - 1; + all_done && i >= 0; + i--) { + if (tn->internal[i]) { + all_done = + yaffs_soft_del_worker(in, + tn->internal[i], + level - 1, + (chunk_offset << + YAFFS_TNODES_INTERNAL_BITS) + + i); + if (all_done) { + yaffs_free_tnode(dev, + tn->internal[i]); + tn->internal[i] = NULL; + } else { + /* Can this happen? */ + } + } + } + return (all_done) ? 1 : 0; + } else if (level == 0) { + for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) { + the_chunk = yaffs_get_group_base(dev, tn, i); + if (the_chunk) { + yaffs_soft_del_chunk(dev, the_chunk); + yaffs_load_tnode_0(dev, tn, i, 0); + } + } + return 1; + } + } + return 1; +} - /* Get the previous chunk at this location in the file if it exists. - * If it does not exist then put a zero into the tree. This creates - * the tnode now, rather than later when it is harder to clean up. - */ - prev_chunk_id = yaffs_find_chunk_in_file(in, inode_chunk, &prev_tags); - if (prev_chunk_id < 1 && - !yaffs_put_chunk_in_file(in, inode_chunk, 0, 0)) - return 0; +static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj) +{ + struct yaffs_dev *dev = obj->my_dev; + struct yaffs_obj *parent; - /* Set up new tags */ - yaffs_init_tags(&new_tags); + yaffs_verify_obj_in_dir(obj); + parent = obj->parent; - new_tags.chunk_id = inode_chunk; - new_tags.obj_id = in->obj_id; - new_tags.serial_number = - (prev_chunk_id > 0) ? prev_tags.serial_number + 1 : 1; - new_tags.n_bytes = n_bytes; + yaffs_verify_dir(parent); - if (n_bytes < 1 || n_bytes > dev->param.total_bytes_per_chunk) { - T(YAFFS_TRACE_ERROR, - (TSTR("Writing %d bytes to chunk!!!!!!!!!" TENDSTR), - n_bytes)); + if (dev && dev->param.remove_obj_fn) + dev->param.remove_obj_fn(obj); + + list_del_init(&obj->siblings); + obj->parent = NULL; + + yaffs_verify_dir(parent); +} + +void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj) +{ + if (!directory) { + yaffs_trace(YAFFS_TRACE_ALWAYS, + "tragedy: Trying to add an object to a null pointer directory" + ); + YBUG(); + return; + } + if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) { + yaffs_trace(YAFFS_TRACE_ALWAYS, + "tragedy: Trying to add an object to a non-directory" + ); YBUG(); } - new_chunk_id = - yaffs_write_new_chunk(dev, buffer, &new_tags, use_reserve); - - if (new_chunk_id > 0) { - yaffs_put_chunk_in_file(in, inode_chunk, new_chunk_id, 0); - - if (prev_chunk_id > 0) - yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__); - - yaffs_verify_file_sane(in); + if (obj->siblings.prev == NULL) { + /* Not initialised */ + YBUG(); } - return new_chunk_id; + yaffs_verify_dir(directory); + + yaffs_remove_obj_from_dir(obj); + + /* Now add it */ + list_add(&obj->siblings, &directory->variant.dir_variant.children); + obj->parent = directory; + + if (directory == obj->my_dev->unlinked_dir + || directory == obj->my_dev->del_dir) { + obj->unlinked = 1; + obj->my_dev->n_unlinked_files++; + obj->rename_allowed = 0; + } + + yaffs_verify_dir(directory); + yaffs_verify_obj_in_dir(obj); } -/* UpdateObjectHeader updates the header on NAND for an object. - * If name is not NULL, then that new name is used. - */ -int yaffs_update_oh(struct yaffs_obj *in, const YCHAR * name, int force, - int is_shrink, int shadows, struct yaffs_xattr_mod *xmod) +static int yaffs_change_obj_name(struct yaffs_obj *obj, + struct yaffs_obj *new_dir, + const YCHAR *new_name, int force, int shadows) { + int unlink_op; + int del_op; + struct yaffs_obj *existing_target; - struct yaffs_block_info *bi; - - struct yaffs_dev *dev = in->my_dev; - - int prev_chunk_id; - int ret_val = 0; - int result = 0; - - int new_chunk_id; - struct yaffs_ext_tags new_tags; - struct yaffs_ext_tags old_tags; - const YCHAR *alias = NULL; - - u8 *buffer = NULL; - YCHAR old_name[YAFFS_MAX_NAME_LENGTH + 1]; - - struct yaffs_obj_hdr *oh = NULL; - - yaffs_strcpy(old_name, _Y("silly old name")); - - if (!in->fake || in == dev->root_dir || /* The root_dir should also be saved */ - force || xmod) { - - yaffs_check_gc(dev, 0); - yaffs_check_obj_details_loaded(in); - - buffer = yaffs_get_temp_buffer(in->my_dev, __LINE__); - oh = (struct yaffs_obj_hdr *)buffer; - - prev_chunk_id = in->hdr_chunk; - - if (prev_chunk_id > 0) { - result = yaffs_rd_chunk_tags_nand(dev, prev_chunk_id, - buffer, &old_tags); - - yaffs_verify_oh(in, oh, &old_tags, 0); - - memcpy(old_name, oh->name, sizeof(oh->name)); - memset(buffer, 0xFF, sizeof(struct yaffs_obj_hdr)); - } else - memset(buffer, 0xFF, dev->data_bytes_per_chunk); - - oh->type = in->variant_type; - oh->yst_mode = in->yst_mode; - oh->shadows_obj = oh->inband_shadowed_obj_id = shadows; - - yaffs_load_attribs_oh(oh, in); - - if (in->parent) - oh->parent_obj_id = in->parent->obj_id; - else - oh->parent_obj_id = 0; - - if (name && *name) { - memset(oh->name, 0, sizeof(oh->name)); - yaffs_load_oh_from_name(dev, oh->name, name); - } else if (prev_chunk_id > 0) - memcpy(oh->name, old_name, sizeof(oh->name)); - else - memset(oh->name, 0, sizeof(oh->name)); - - oh->is_shrink = is_shrink; - - switch (in->variant_type) { - case YAFFS_OBJECT_TYPE_UNKNOWN: - /* Should not happen */ - break; - case YAFFS_OBJECT_TYPE_FILE: - oh->file_size = - (oh->parent_obj_id == YAFFS_OBJECTID_DELETED - || oh->parent_obj_id == - YAFFS_OBJECTID_UNLINKED) ? 0 : in-> - variant.file_variant.file_size; - break; - case YAFFS_OBJECT_TYPE_HARDLINK: - oh->equiv_id = in->variant.hardlink_variant.equiv_id; - break; - case YAFFS_OBJECT_TYPE_SPECIAL: - /* Do nothing */ - break; - case YAFFS_OBJECT_TYPE_DIRECTORY: - /* Do nothing */ - break; - case YAFFS_OBJECT_TYPE_SYMLINK: - alias = in->variant.symlink_variant.alias; - if (!alias) - alias = _Y("no alias"); - yaffs_strncpy(oh->alias, alias, YAFFS_MAX_ALIAS_LENGTH); - oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0; - break; - } - - /* process any xattrib modifications */ - if (xmod) - yaffs_apply_xattrib_mod(in, (char *)buffer, xmod); - - /* Tags */ - yaffs_init_tags(&new_tags); - in->serial++; - new_tags.chunk_id = 0; - new_tags.obj_id = in->obj_id; - new_tags.serial_number = in->serial; - - /* Add extra info for file header */ - - new_tags.extra_available = 1; - new_tags.extra_parent_id = oh->parent_obj_id; - new_tags.extra_length = oh->file_size; - new_tags.extra_is_shrink = oh->is_shrink; - new_tags.extra_equiv_id = oh->equiv_id; - new_tags.extra_shadows = (oh->shadows_obj > 0) ? 1 : 0; - new_tags.extra_obj_type = in->variant_type; - - yaffs_verify_oh(in, oh, &new_tags, 1); - - /* Create new chunk in NAND */ - new_chunk_id = - yaffs_write_new_chunk(dev, buffer, &new_tags, - (prev_chunk_id > 0) ? 1 : 0); - - if (new_chunk_id >= 0) { - - in->hdr_chunk = new_chunk_id; - - if (prev_chunk_id > 0) { - yaffs_chunk_del(dev, prev_chunk_id, 1, - __LINE__); - } - - if (!yaffs_obj_cache_dirty(in)) - in->dirty = 0; - - /* If this was a shrink, then mark the block that the chunk lives on */ - if (is_shrink) { - bi = yaffs_get_block_info(in->my_dev, - new_chunk_id / - in->my_dev->param. - chunks_per_block); - bi->has_shrink_hdr = 1; - } - - } - - ret_val = new_chunk_id; + if (new_dir == NULL) + new_dir = obj->parent; /* use the old directory */ + if (new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) { + yaffs_trace(YAFFS_TRACE_ALWAYS, + "tragedy: yaffs_change_obj_name: new_dir is not a directory" + ); + YBUG(); } - if (buffer) - yaffs_release_temp_buffer(dev, buffer, __LINE__); + /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */ + if (obj->my_dev->param.is_yaffs2) + unlink_op = (new_dir == obj->my_dev->unlinked_dir); + else + unlink_op = (new_dir == obj->my_dev->unlinked_dir + && obj->variant_type == YAFFS_OBJECT_TYPE_FILE); - return ret_val; + del_op = (new_dir == obj->my_dev->del_dir); + + existing_target = yaffs_find_by_name(new_dir, new_name); + + /* If the object is a file going into the unlinked directory, + * then it is OK to just stuff it in since duplicate names are OK. + * else only proceed if the new name does not exist and we're putting + * it into a directory. + */ + if ((unlink_op || + del_op || + force || + (shadows > 0) || + !existing_target) && + new_dir->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) { + yaffs_set_obj_name(obj, new_name); + obj->dirty = 1; + + yaffs_add_obj_to_dir(new_dir, obj); + + if (unlink_op) + obj->unlinked = 1; + + /* If it is a deletion then we mark it as a shrink for gc */ + if (yaffs_update_oh(obj, new_name, 0, del_op, shadows, NULL) >= + 0) + return YAFFS_OK; + } + return YAFFS_FAIL; } -/*------------------------ Short Operations Cache ---------------------------------------- +/*------------------------ Short Operations Cache ------------------------------ * In many situations where there is no high level buffering a lot of * reads might be short sequential reads, and a lot of writes may be short * sequential writes. eg. scanning/writing a jpeg file. * In these cases, a short read/write cache can provide a huge perfomance * benefit with dumb-as-a-rock code. - * In Linux, the page cache provides read buffering and the short op cache + * In Linux, the page cache provides read buffering and the short op cache * provides write buffering. * - * There are a limited number (~10) of cache chunks per device so that we don't + * There are a small number (~10) of cache chunks per device so that we don't * need a very intelligent search. */ @@ -3077,7 +1422,7 @@ static void yaffs_flush_file_cache(struct yaffs_obj *obj) do { cache = NULL; - /* Find the dirty cache for this object with the lowest chunk id. */ + /* Find the lowest dirty chunk for this object */ for (i = 0; i < n_caches; i++) { if (dev->cache[i].object == obj && dev->cache[i].dirty) { @@ -3092,7 +1437,6 @@ static void yaffs_flush_file_cache(struct yaffs_obj *obj) if (cache && !cache->locked) { /* Write it out and free it up */ - chunk_written = yaffs_wr_data_obj(cache->object, cache->chunk_id, @@ -3101,19 +1445,13 @@ static void yaffs_flush_file_cache(struct yaffs_obj *obj) cache->dirty = 0; cache->object = NULL; } - } while (cache && chunk_written > 0); - if (cache) { + if (cache) /* Hoosterman, disk full while writing cache out. */ - T(YAFFS_TRACE_ERROR, - (TSTR - ("yaffs tragedy: no space during cache write" - TENDSTR))); - - } + yaffs_trace(YAFFS_TRACE_ERROR, + "yaffs tragedy: no space during cache write"); } - } /*yaffs_flush_whole_cache(dev) @@ -3135,11 +1473,9 @@ void yaffs_flush_whole_cache(struct yaffs_dev *dev) for (i = 0; i < n_caches && !obj; i++) { if (dev->cache[i].object && dev->cache[i].dirty) obj = dev->cache[i].object; - } if (obj) yaffs_flush_file_cache(obj); - } while (obj); } @@ -3159,7 +1495,6 @@ static struct yaffs_cache *yaffs_grab_chunk_worker(struct yaffs_dev *dev) return &dev->cache[i]; } } - return NULL; } @@ -3177,15 +1512,16 @@ static struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev) cache = yaffs_grab_chunk_worker(dev); if (!cache) { - /* They were all dirty, find the last recently used object and flush + /* They were all dirty, find the LRU object and flush * its cache, then find again. - * NB what's here is not very accurate, we actually flush the object - * the last recently used page. + * NB what's here is not very accurate, + * we actually flush the object with the LRU chunk. */ - /* With locking we can't assume we can use entry zero */ + /* With locking we can't assume we can use entry zero, + * Set the_obj to a valid pointer for Coverity. */ - the_obj = NULL; + the_obj = dev->cache[0].object; usage = -1; cache = NULL; pushout = -1; @@ -3207,12 +1543,11 @@ static struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev) yaffs_flush_file_cache(the_obj); cache = yaffs_grab_chunk_worker(dev); } - } return cache; - } else + } else { return NULL; - + } } /* Find a cached chunk */ @@ -3221,6 +1556,7 @@ static struct yaffs_cache *yaffs_find_chunk_cache(const struct yaffs_obj *obj, { struct yaffs_dev *dev = obj->my_dev; int i; + if (dev->param.n_caches > 0) { for (i = 0; i < dev->param.n_caches; i++) { if (dev->cache[i].object == obj && @@ -3238,9 +1574,9 @@ static struct yaffs_cache *yaffs_find_chunk_cache(const struct yaffs_obj *obj, static void yaffs_use_cache(struct yaffs_dev *dev, struct yaffs_cache *cache, int is_write) { - if (dev->param.n_caches > 0) { - if (dev->cache_last_use < 0 || dev->cache_last_use > 100000000) { + if (dev->cache_last_use < 0 || + dev->cache_last_use > 100000000) { /* Reset the cache usages */ int i; for (i = 1; i < dev->param.n_caches; i++) @@ -3248,9 +1584,7 @@ static void yaffs_use_cache(struct yaffs_dev *dev, struct yaffs_cache *cache, dev->cache_last_use = 0; } - dev->cache_last_use++; - cache->last_use = dev->cache_last_use; if (is_write) @@ -3290,430 +1624,66 @@ static void yaffs_invalidate_whole_cache(struct yaffs_obj *in) } } -/*--------------------- File read/write ------------------------ - * Read and write have very similar structures. - * In general the read/write has three parts to it - * An incomplete chunk to start with (if the read/write is not chunk-aligned) - * Some complete chunks - * An incomplete chunk to end off with - * - * Curve-balls: the first chunk might also be the last chunk. - */ - -int yaffs_file_rd(struct yaffs_obj *in, u8 * buffer, loff_t offset, int n_bytes) +static void yaffs_unhash_obj(struct yaffs_obj *obj) { - - int chunk; - u32 start; - int n_copy; - int n = n_bytes; - int n_done = 0; - struct yaffs_cache *cache; - - struct yaffs_dev *dev; - - dev = in->my_dev; - - while (n > 0) { - /* chunk = offset / dev->data_bytes_per_chunk + 1; */ - /* start = offset % dev->data_bytes_per_chunk; */ - yaffs_addr_to_chunk(dev, offset, &chunk, &start); - chunk++; - - /* OK now check for the curveball where the start and end are in - * the same chunk. - */ - if ((start + n) < dev->data_bytes_per_chunk) - n_copy = n; - else - n_copy = dev->data_bytes_per_chunk - start; - - cache = yaffs_find_chunk_cache(in, chunk); - - /* If the chunk is already in the cache or it is less than a whole chunk - * or we're using inband tags then use the cache (if there is caching) - * else bypass the cache. - */ - if (cache || n_copy != dev->data_bytes_per_chunk - || dev->param.inband_tags) { - if (dev->param.n_caches > 0) { - - /* If we can't find the data in the cache, then load it up. */ - - if (!cache) { - cache = - yaffs_grab_chunk_cache(in->my_dev); - cache->object = in; - cache->chunk_id = chunk; - cache->dirty = 0; - cache->locked = 0; - yaffs_rd_data_obj(in, chunk, - cache->data); - cache->n_bytes = 0; - } - - yaffs_use_cache(dev, cache, 0); - - cache->locked = 1; - - memcpy(buffer, &cache->data[start], n_copy); - - cache->locked = 0; - } else { - /* Read into the local buffer then copy.. */ - - u8 *local_buffer = - yaffs_get_temp_buffer(dev, __LINE__); - yaffs_rd_data_obj(in, chunk, local_buffer); - - memcpy(buffer, &local_buffer[start], n_copy); - - yaffs_release_temp_buffer(dev, local_buffer, - __LINE__); - } - - } else { - - /* A full chunk. Read directly into the supplied buffer. */ - yaffs_rd_data_obj(in, chunk, buffer); - - } - - n -= n_copy; - offset += n_copy; - buffer += n_copy; - n_done += n_copy; - - } - - return n_done; -} - -int yaffs_do_file_wr(struct yaffs_obj *in, const u8 * buffer, loff_t offset, - int n_bytes, int write_trhrough) -{ - - int chunk; - u32 start; - int n_copy; - int n = n_bytes; - int n_done = 0; - int n_writeback; - int start_write = offset; - int chunk_written = 0; - u32 n_bytes_read; - u32 chunk_start; - - struct yaffs_dev *dev; - - dev = in->my_dev; - - while (n > 0 && chunk_written >= 0) { - yaffs_addr_to_chunk(dev, offset, &chunk, &start); - - if (chunk * dev->data_bytes_per_chunk + start != offset || - start >= dev->data_bytes_per_chunk) { - T(YAFFS_TRACE_ERROR, - (TSTR - ("AddrToChunk of offset %d gives chunk %d start %d" - TENDSTR), (int)offset, chunk, start)); - } - chunk++; /* File pos to chunk in file offset */ - - /* OK now check for the curveball where the start and end are in - * the same chunk. - */ - - if ((start + n) < dev->data_bytes_per_chunk) { - n_copy = n; - - /* Now folks, to calculate how many bytes to write back.... - * If we're overwriting and not writing to then end of file then - * we need to write back as much as was there before. - */ - - chunk_start = ((chunk - 1) * dev->data_bytes_per_chunk); - - if (chunk_start > in->variant.file_variant.file_size) - n_bytes_read = 0; /* Past end of file */ - else - n_bytes_read = - in->variant.file_variant.file_size - - chunk_start; - - if (n_bytes_read > dev->data_bytes_per_chunk) - n_bytes_read = dev->data_bytes_per_chunk; - - n_writeback = - (n_bytes_read > - (start + n)) ? n_bytes_read : (start + n); - - if (n_writeback < 0 - || n_writeback > dev->data_bytes_per_chunk) - YBUG(); - - } else { - n_copy = dev->data_bytes_per_chunk - start; - n_writeback = dev->data_bytes_per_chunk; - } - - if (n_copy != dev->data_bytes_per_chunk - || dev->param.inband_tags) { - /* An incomplete start or end chunk (or maybe both start and end chunk), - * or we're using inband tags, so we want to use the cache buffers. - */ - if (dev->param.n_caches > 0) { - struct yaffs_cache *cache; - /* If we can't find the data in the cache, then load the cache */ - cache = yaffs_find_chunk_cache(in, chunk); - - if (!cache - && yaffs_check_alloc_available(dev, 1)) { - cache = yaffs_grab_chunk_cache(dev); - cache->object = in; - cache->chunk_id = chunk; - cache->dirty = 0; - cache->locked = 0; - yaffs_rd_data_obj(in, chunk, - cache->data); - } else if (cache && - !cache->dirty && - !yaffs_check_alloc_available(dev, - 1)) { - /* Drop the cache if it was a read cache item and - * no space check has been made for it. - */ - cache = NULL; - } - - if (cache) { - yaffs_use_cache(dev, cache, 1); - cache->locked = 1; - - memcpy(&cache->data[start], buffer, - n_copy); - - cache->locked = 0; - cache->n_bytes = n_writeback; - - if (write_trhrough) { - chunk_written = - yaffs_wr_data_obj - (cache->object, - cache->chunk_id, - cache->data, - cache->n_bytes, 1); - cache->dirty = 0; - } - - } else { - chunk_written = -1; /* fail the write */ - } - } else { - /* An incomplete start or end chunk (or maybe both start and end chunk) - * Read into the local buffer then copy, then copy over and write back. - */ - - u8 *local_buffer = - yaffs_get_temp_buffer(dev, __LINE__); - - yaffs_rd_data_obj(in, chunk, local_buffer); - - memcpy(&local_buffer[start], buffer, n_copy); - - chunk_written = - yaffs_wr_data_obj(in, chunk, - local_buffer, - n_writeback, 0); - - yaffs_release_temp_buffer(dev, local_buffer, - __LINE__); - - } - - } else { - /* A full chunk. Write directly from the supplied buffer. */ - - chunk_written = - yaffs_wr_data_obj(in, chunk, buffer, - dev->data_bytes_per_chunk, 0); - - /* Since we've overwritten the cached data, we better invalidate it. */ - yaffs_invalidate_chunk_cache(in, chunk); - } - - if (chunk_written >= 0) { - n -= n_copy; - offset += n_copy; - buffer += n_copy; - n_done += n_copy; - } - - } - - /* Update file object */ - - if ((start_write + n_done) > in->variant.file_variant.file_size) - in->variant.file_variant.file_size = (start_write + n_done); - - in->dirty = 1; - - return n_done; -} - -int yaffs_wr_file(struct yaffs_obj *in, const u8 * buffer, loff_t offset, - int n_bytes, int write_trhrough) -{ - yaffs2_handle_hole(in, offset); - return yaffs_do_file_wr(in, buffer, offset, n_bytes, write_trhrough); -} - -/* ---------------------- File resizing stuff ------------------ */ - -static void yaffs_prune_chunks(struct yaffs_obj *in, int new_size) -{ - - struct yaffs_dev *dev = in->my_dev; - int old_size = in->variant.file_variant.file_size; - - int last_del = 1 + (old_size - 1) / dev->data_bytes_per_chunk; - - int start_del = 1 + (new_size + dev->data_bytes_per_chunk - 1) / - dev->data_bytes_per_chunk; - int i; - int chunk_id; - - /* Delete backwards so that we don't end up with holes if - * power is lost part-way through the operation. - */ - for (i = last_del; i >= start_del; i--) { - /* NB this could be optimised somewhat, - * eg. could retrieve the tags and write them without - * using yaffs_chunk_del - */ - - chunk_id = yaffs_find_del_file_chunk(in, i, NULL); - if (chunk_id > 0) { - if (chunk_id < - (dev->internal_start_block * - dev->param.chunks_per_block) - || chunk_id >= - ((dev->internal_end_block + - 1) * dev->param.chunks_per_block)) { - T(YAFFS_TRACE_ALWAYS, - (TSTR - ("Found daft chunk_id %d for %d" TENDSTR), - chunk_id, i)); - } else { - in->n_data_chunks--; - yaffs_chunk_del(dev, chunk_id, 1, __LINE__); - } - } - } - -} - -void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size) -{ - int new_full; - u32 new_partial; + int bucket; struct yaffs_dev *dev = obj->my_dev; - yaffs_addr_to_chunk(dev, new_size, &new_full, &new_partial); - - yaffs_prune_chunks(obj, new_size); - - if (new_partial != 0) { - int last_chunk = 1 + new_full; - u8 *local_buffer = yaffs_get_temp_buffer(dev, __LINE__); - - /* Got to read and rewrite the last chunk with its new size and zero pad */ - yaffs_rd_data_obj(obj, last_chunk, local_buffer); - memset(local_buffer + new_partial, 0, - dev->data_bytes_per_chunk - new_partial); - - yaffs_wr_data_obj(obj, last_chunk, local_buffer, - new_partial, 1); - - yaffs_release_temp_buffer(dev, local_buffer, __LINE__); + /* If it is still linked into the bucket list, free from the list */ + if (!list_empty(&obj->hash_link)) { + list_del_init(&obj->hash_link); + bucket = yaffs_hash_fn(obj->obj_id); + dev->obj_bucket[bucket].count--; } - - obj->variant.file_variant.file_size = new_size; - - yaffs_prune_tree(dev, &obj->variant.file_variant); } -int yaffs_resize_file(struct yaffs_obj *in, loff_t new_size) +/* FreeObject frees up a Object and puts it back on the free list */ +static void yaffs_free_obj(struct yaffs_obj *obj) { - struct yaffs_dev *dev = in->my_dev; - int old_size = in->variant.file_variant.file_size; + struct yaffs_dev *dev; - yaffs_flush_file_cache(in); - yaffs_invalidate_whole_cache(in); + if (!obj) { + YBUG(); + return; + } + dev = obj->my_dev; + yaffs_trace(YAFFS_TRACE_OS, "FreeObject %p inode %p", + obj, obj->my_inode); + if (obj->parent) + YBUG(); + if (!list_empty(&obj->siblings)) + YBUG(); - yaffs_check_gc(dev, 0); - - if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) - return YAFFS_FAIL; - - if (new_size == old_size) - return YAFFS_OK; - - if (new_size > old_size) { - yaffs2_handle_hole(in, new_size); - in->variant.file_variant.file_size = new_size; - } else { - /* new_size < old_size */ - yaffs_resize_file_down(in, new_size); + if (obj->my_inode) { + /* We're still hooked up to a cached inode. + * Don't delete now, but mark for later deletion + */ + obj->defered_free = 1; + return; } - /* Write a new object header to reflect the resize. - * show we've shrunk the file, if need be - * Do this only if the file is not in the deleted directories - * and is not shadowed. - */ - if (in->parent && - !in->is_shadowed && - in->parent->obj_id != YAFFS_OBJECTID_UNLINKED && - in->parent->obj_id != YAFFS_OBJECTID_DELETED) - yaffs_update_oh(in, NULL, 0, 0, 0, NULL); + yaffs_unhash_obj(obj); - return YAFFS_OK; + yaffs_free_raw_obj(dev, obj); + dev->n_obj--; + dev->checkpoint_blocks_required = 0; /* force recalculation */ } -int yaffs_flush_file(struct yaffs_obj *in, int update_time, int data_sync) +void yaffs_handle_defered_free(struct yaffs_obj *obj) { - int ret_val; - if (in->dirty) { - yaffs_flush_file_cache(in); - if (data_sync) /* Only sync data */ - ret_val = YAFFS_OK; - else { - if (update_time) - yaffs_load_current_time(in, 0, 0); - - ret_val = (yaffs_update_oh(in, NULL, 0, 0, 0, NULL) >= - 0) ? YAFFS_OK : YAFFS_FAIL; - } - } else { - ret_val = YAFFS_OK; - } - - return ret_val; - + if (obj->defered_free) + yaffs_free_obj(obj); } static int yaffs_generic_obj_del(struct yaffs_obj *in) { - - /* First off, invalidate the file's data in the cache, without flushing. */ + /* Iinvalidate the file's data in the cache, without flushing. */ yaffs_invalidate_whole_cache(in); - if (in->my_dev->param.is_yaffs2 && (in->parent != in->my_dev->del_dir)) { - /* Move to the unlinked directory so we have a record that it was deleted. */ + if (in->my_dev->param.is_yaffs2 && in->parent != in->my_dev->del_dir) { + /* Move to unlinked directory so we have a deletion record */ yaffs_change_obj_name(in, in->my_dev->del_dir, _Y("deleted"), 0, 0); - } yaffs_remove_obj_from_dir(in); @@ -3725,495 +1695,378 @@ static int yaffs_generic_obj_del(struct yaffs_obj *in) } -/* yaffs_del_file deletes the whole file data - * and the inode associated with the file. - * It does not delete the links associated with the file. - */ -static int yaffs_unlink_file_if_needed(struct yaffs_obj *in) +static void yaffs_soft_del_file(struct yaffs_obj *obj) { - - int ret_val; - int del_now = 0; - struct yaffs_dev *dev = in->my_dev; - - if (!in->my_inode) - del_now = 1; - - if (del_now) { - ret_val = - yaffs_change_obj_name(in, in->my_dev->del_dir, - _Y("deleted"), 0, 0); - T(YAFFS_TRACE_TRACING, - (TSTR("yaffs: immediate deletion of file %d" TENDSTR), - in->obj_id)); - in->deleted = 1; - in->my_dev->n_deleted_files++; - if (dev->param.disable_soft_del || dev->param.is_yaffs2) - yaffs_resize_file(in, 0); - yaffs_soft_del_file(in); - } else { - ret_val = - yaffs_change_obj_name(in, in->my_dev->unlinked_dir, - _Y("unlinked"), 0, 0); - } - - return ret_val; -} - -int yaffs_del_file(struct yaffs_obj *in) -{ - int ret_val = YAFFS_OK; - int deleted; /* Need to cache value on stack if in is freed */ - struct yaffs_dev *dev = in->my_dev; - - if (dev->param.disable_soft_del || dev->param.is_yaffs2) - yaffs_resize_file(in, 0); - - if (in->n_data_chunks > 0) { - /* Use soft deletion if there is data in the file. - * That won't be the case if it has been resized to zero. - */ - if (!in->unlinked) - ret_val = yaffs_unlink_file_if_needed(in); - - deleted = in->deleted; - - if (ret_val == YAFFS_OK && in->unlinked && !in->deleted) { - in->deleted = 1; - deleted = 1; - in->my_dev->n_deleted_files++; - yaffs_soft_del_file(in); - } - return deleted ? YAFFS_OK : YAFFS_FAIL; - } else { - /* The file has no data chunks so we toss it immediately */ - yaffs_free_tnode(in->my_dev, in->variant.file_variant.top); - in->variant.file_variant.top = NULL; - yaffs_generic_obj_del(in); - - return YAFFS_OK; - } -} - -static int yaffs_is_non_empty_dir(struct yaffs_obj *obj) -{ - return (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) && - !(list_empty(&obj->variant.dir_variant.children)); -} - -static int yaffs_del_dir(struct yaffs_obj *obj) -{ - /* First check that the directory is empty. */ - if (yaffs_is_non_empty_dir(obj)) - return YAFFS_FAIL; - - return yaffs_generic_obj_del(obj); -} - -static int yaffs_del_symlink(struct yaffs_obj *in) -{ - if (in->variant.symlink_variant.alias) - YFREE(in->variant.symlink_variant.alias); - in->variant.symlink_variant.alias = NULL; - - return yaffs_generic_obj_del(in); -} - -static int yaffs_del_link(struct yaffs_obj *in) -{ - /* remove this hardlink from the list assocaited with the equivalent - * object - */ - list_del_init(&in->hard_links); - return yaffs_generic_obj_del(in); -} - -int yaffs_del_obj(struct yaffs_obj *obj) -{ - int ret_val = -1; - switch (obj->variant_type) { - case YAFFS_OBJECT_TYPE_FILE: - ret_val = yaffs_del_file(obj); - break; - case YAFFS_OBJECT_TYPE_DIRECTORY: - if (!list_empty(&obj->variant.dir_variant.dirty)) { - T(YAFFS_TRACE_BACKGROUND, - (TSTR - ("Remove object %d from dirty directories" TENDSTR), - obj->obj_id)); - list_del_init(&obj->variant.dir_variant.dirty); - } - return yaffs_del_dir(obj); - break; - case YAFFS_OBJECT_TYPE_SYMLINK: - ret_val = yaffs_del_symlink(obj); - break; - case YAFFS_OBJECT_TYPE_HARDLINK: - ret_val = yaffs_del_link(obj); - break; - case YAFFS_OBJECT_TYPE_SPECIAL: - ret_val = yaffs_generic_obj_del(obj); - break; - case YAFFS_OBJECT_TYPE_UNKNOWN: - ret_val = 0; - break; /* should not happen. */ - } - - return ret_val; -} - -static int yaffs_unlink_worker(struct yaffs_obj *obj) -{ - - int del_now = 0; - - if (!obj->my_inode) - del_now = 1; - - if (obj) - yaffs_update_parent(obj->parent); - - if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) { - return yaffs_del_link(obj); - } else if (!list_empty(&obj->hard_links)) { - /* Curve ball: We're unlinking an object that has a hardlink. - * - * This problem arises because we are not strictly following - * The Linux link/inode model. - * - * We can't really delete the object. - * Instead, we do the following: - * - Select a hardlink. - * - Unhook it from the hard links - * - Move it from its parent directory (so that the rename can work) - * - Rename the object to the hardlink's name. - * - Delete the hardlink - */ - - struct yaffs_obj *hl; - struct yaffs_obj *parent; - int ret_val; - YCHAR name[YAFFS_MAX_NAME_LENGTH + 1]; - - hl = list_entry(obj->hard_links.next, struct yaffs_obj, - hard_links); - - yaffs_get_obj_name(hl, name, YAFFS_MAX_NAME_LENGTH + 1); - parent = hl->parent; - - list_del_init(&hl->hard_links); - - yaffs_add_obj_to_dir(obj->my_dev->unlinked_dir, hl); - - ret_val = yaffs_change_obj_name(obj, parent, name, 0, 0); - - if (ret_val == YAFFS_OK) - ret_val = yaffs_generic_obj_del(hl); - - return ret_val; - - } else if (del_now) { - switch (obj->variant_type) { - case YAFFS_OBJECT_TYPE_FILE: - return yaffs_del_file(obj); - break; - case YAFFS_OBJECT_TYPE_DIRECTORY: - list_del_init(&obj->variant.dir_variant.dirty); - return yaffs_del_dir(obj); - break; - case YAFFS_OBJECT_TYPE_SYMLINK: - return yaffs_del_symlink(obj); - break; - case YAFFS_OBJECT_TYPE_SPECIAL: - return yaffs_generic_obj_del(obj); - break; - case YAFFS_OBJECT_TYPE_HARDLINK: - case YAFFS_OBJECT_TYPE_UNKNOWN: - default: - return YAFFS_FAIL; - } - } else if (yaffs_is_non_empty_dir(obj)) - return YAFFS_FAIL; - else - return yaffs_change_obj_name(obj, obj->my_dev->unlinked_dir, - _Y("unlinked"), 0, 0); -} - -static int yaffs_unlink_obj(struct yaffs_obj *obj) -{ - - if (obj && obj->unlink_allowed) - return yaffs_unlink_worker(obj); - - return YAFFS_FAIL; - -} - -int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR * name) -{ - struct yaffs_obj *obj; - - obj = yaffs_find_by_name(dir, name); - return yaffs_unlink_obj(obj); -} - -/*----------------------- Initialisation Scanning ---------------------- */ - -void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id, - int backward_scanning) -{ - struct yaffs_obj *obj; - - if (!backward_scanning) { - /* Handle YAFFS1 forward scanning case - * For YAFFS1 we always do the deletion - */ - - } else { - /* Handle YAFFS2 case (backward scanning) - * If the shadowed object exists then ignore. - */ - obj = yaffs_find_by_number(dev, obj_id); - if (obj) - return; - } - - /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc. - * We put it in unlinked dir to be cleaned up after the scanning - */ - obj = - yaffs_find_or_create_by_number(dev, obj_id, YAFFS_OBJECT_TYPE_FILE); - if (!obj) - return; - obj->is_shadowed = 1; - yaffs_add_obj_to_dir(dev->unlinked_dir, obj); - obj->variant.file_variant.shrink_size = 0; - obj->valid = 1; /* So that we don't read any other info for this file */ - -} - -void yaffs_link_fixup(struct yaffs_dev *dev, struct yaffs_obj *hard_list) -{ - struct yaffs_obj *hl; - struct yaffs_obj *in; - - while (hard_list) { - hl = hard_list; - hard_list = (struct yaffs_obj *)(hard_list->hard_links.next); - - in = yaffs_find_by_number(dev, - hl->variant. - hardlink_variant.equiv_id); - - if (in) { - /* Add the hardlink pointers */ - hl->variant.hardlink_variant.equiv_obj = in; - list_add(&hl->hard_links, &in->hard_links); + if (obj->deleted && + obj->variant_type == YAFFS_OBJECT_TYPE_FILE && !obj->soft_del) { + if (obj->n_data_chunks <= 0) { + /* Empty file with no duplicate object headers, + * just delete it immediately */ + yaffs_free_tnode(obj->my_dev, + obj->variant.file_variant.top); + obj->variant.file_variant.top = NULL; + yaffs_trace(YAFFS_TRACE_TRACING, + "yaffs: Deleting empty file %d", + obj->obj_id); + yaffs_generic_obj_del(obj); } else { - /* Todo Need to report/handle this better. - * Got a problem... hardlink to a non-existant object - */ - hl->variant.hardlink_variant.equiv_obj = NULL; - INIT_LIST_HEAD(&hl->hard_links); - + yaffs_soft_del_worker(obj, + obj->variant.file_variant.top, + obj->variant. + file_variant.top_level, 0); + obj->soft_del = 1; } } } -static void yaffs_strip_deleted_objs(struct yaffs_dev *dev) -{ - /* - * Sort out state of unlinked and deleted objects after scanning. - */ - struct list_head *i; - struct list_head *n; - struct yaffs_obj *l; - - if (dev->read_only) - return; - - /* Soft delete all the unlinked files */ - list_for_each_safe(i, n, - &dev->unlinked_dir->variant.dir_variant.children) { - if (i) { - l = list_entry(i, struct yaffs_obj, siblings); - yaffs_del_obj(l); - } - } - - list_for_each_safe(i, n, &dev->del_dir->variant.dir_variant.children) { - if (i) { - l = list_entry(i, struct yaffs_obj, siblings); - yaffs_del_obj(l); - } - } - -} - -/* - * This code iterates through all the objects making sure that they are rooted. - * Any unrooted objects are re-rooted in lost+found. - * An object needs to be in one of: - * - Directly under deleted, unlinked - * - Directly or indirectly under root. +/* Pruning removes any part of the file structure tree that is beyond the + * bounds of the file (ie that does not point to chunks). * - * Note: - * This code assumes that we don't ever change the current relationships between - * directories: - * root_dir->parent == unlinked_dir->parent == del_dir->parent == NULL - * lost-n-found->parent == root_dir + * A file should only get pruned when its size is reduced. * - * This fixes the problem where directories might have inadvertently been deleted - * leaving the object "hanging" without being rooted in the directory tree. + * Before pruning, the chunks must be pulled from the tree and the + * level 0 tnode entries must be zeroed out. + * Could also use this for file deletion, but that's probably better handled + * by a special case. + * + * This function is recursive. For levels > 0 the function is called again on + * any sub-tree. For level == 0 we just check if the sub-tree has data. + * If there is no data in a subtree then it is pruned. */ -static int yaffs_has_null_parent(struct yaffs_dev *dev, struct yaffs_obj *obj) +static struct yaffs_tnode *yaffs_prune_worker(struct yaffs_dev *dev, + struct yaffs_tnode *tn, u32 level, + int del0) { - return (obj == dev->del_dir || - obj == dev->unlinked_dir || obj == dev->root_dir); + int i; + int has_data; + + if (tn) { + has_data = 0; + + if (level > 0) { + for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) { + if (tn->internal[i]) { + tn->internal[i] = + yaffs_prune_worker(dev, + tn->internal[i], + level - 1, + (i == 0) ? del0 : 1); + } + + if (tn->internal[i]) + has_data++; + } + } else { + int tnode_size_u32 = dev->tnode_size / sizeof(u32); + u32 *map = (u32 *) tn; + + for (i = 0; !has_data && i < tnode_size_u32; i++) { + if (map[i]) + has_data++; + } + } + + if (has_data == 0 && del0) { + /* Free and return NULL */ + + yaffs_free_tnode(dev, tn); + tn = NULL; + } + } + return tn; } -static void yaffs_fix_hanging_objs(struct yaffs_dev *dev) +static int yaffs_prune_tree(struct yaffs_dev *dev, + struct yaffs_file_var *file_struct) { - struct yaffs_obj *obj; - struct yaffs_obj *parent; int i; - struct list_head *lh; - struct list_head *n; - int depth_limit; - int hanging; + int has_data; + int done = 0; + struct yaffs_tnode *tn; - if (dev->read_only) - return; + if (file_struct->top_level > 0) { + file_struct->top = + yaffs_prune_worker(dev, file_struct->top, + file_struct->top_level, 0); - /* Iterate through the objects in each hash entry, - * looking at each object. - * Make sure it is rooted. + /* Now we have a tree with all the non-zero branches NULL but + * the height is the same as it was. + * Let's see if we can trim internal tnodes to shorten the tree. + * We can do this if only the 0th element in the tnode is in use + * (ie all the non-zero are NULL) + */ + + while (file_struct->top_level && !done) { + tn = file_struct->top; + + has_data = 0; + for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) { + if (tn->internal[i]) + has_data++; + } + + if (!has_data) { + file_struct->top = tn->internal[0]; + file_struct->top_level--; + yaffs_free_tnode(dev, tn); + } else { + done = 1; + } + } + } + return YAFFS_OK; +} + +/*-------------------- End of File Structure functions.-------------------*/ + +/* alloc_empty_obj gets us a clean Object.*/ +static struct yaffs_obj *yaffs_alloc_empty_obj(struct yaffs_dev *dev) +{ + struct yaffs_obj *obj = yaffs_alloc_raw_obj(dev); + + if (obj) { + dev->n_obj++; + + /* Now sweeten it up... */ + + memset(obj, 0, sizeof(struct yaffs_obj)); + obj->being_created = 1; + + obj->my_dev = dev; + obj->hdr_chunk = 0; + obj->variant_type = YAFFS_OBJECT_TYPE_UNKNOWN; + INIT_LIST_HEAD(&(obj->hard_links)); + INIT_LIST_HEAD(&(obj->hash_link)); + INIT_LIST_HEAD(&obj->siblings); + + /* Now make the directory sane */ + if (dev->root_dir) { + obj->parent = dev->root_dir; + list_add(&(obj->siblings), + &dev->root_dir->variant.dir_variant.children); + } + + /* Add it to the lost and found directory. + * NB Can't put root or lost-n-found in lost-n-found so + * check if lost-n-found exists first + */ + if (dev->lost_n_found) + yaffs_add_obj_to_dir(dev->lost_n_found, obj); + + obj->being_created = 0; + } + + dev->checkpoint_blocks_required = 0; /* force recalculation */ + + return obj; +} + +static int yaffs_find_nice_bucket(struct yaffs_dev *dev) +{ + int i; + int l = 999; + int lowest = 999999; + + /* Search for the shortest list or one that + * isn't too long. */ - for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) { - list_for_each_safe(lh, n, &dev->obj_bucket[i].list) { - if (lh) { - obj = - list_entry(lh, struct yaffs_obj, hash_link); - parent = obj->parent; + for (i = 0; i < 10 && lowest > 4; i++) { + dev->bucket_finder++; + dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS; + if (dev->obj_bucket[dev->bucket_finder].count < lowest) { + lowest = dev->obj_bucket[dev->bucket_finder].count; + l = dev->bucket_finder; + } + } - if (yaffs_has_null_parent(dev, obj)) { - /* These directories are not hanging */ - hanging = 0; - } else if (!parent - || parent->variant_type != - YAFFS_OBJECT_TYPE_DIRECTORY) - hanging = 1; - else if (yaffs_has_null_parent(dev, parent)) - hanging = 0; - else { - /* - * Need to follow the parent chain to see if it is hanging. - */ - hanging = 0; - depth_limit = 100; + return l; +} - while (parent != dev->root_dir && - parent->parent && - parent->parent->variant_type == - YAFFS_OBJECT_TYPE_DIRECTORY - && depth_limit > 0) { - parent = parent->parent; - depth_limit--; - } - if (parent != dev->root_dir) - hanging = 1; - } - if (hanging) { - T(YAFFS_TRACE_SCAN, - (TSTR - ("Hanging object %d moved to lost and found" - TENDSTR), obj->obj_id)); - yaffs_add_obj_to_dir(dev->lost_n_found, - obj); +static int yaffs_new_obj_id(struct yaffs_dev *dev) +{ + int bucket = yaffs_find_nice_bucket(dev); + int found = 0; + struct list_head *i; + u32 n = (u32) bucket; + + /* Now find an object value that has not already been taken + * by scanning the list. + */ + + while (!found) { + found = 1; + n += YAFFS_NOBJECT_BUCKETS; + if (1 || dev->obj_bucket[bucket].count > 0) { + list_for_each(i, &dev->obj_bucket[bucket].list) { + /* If there is already one in the list */ + if (i && list_entry(i, struct yaffs_obj, + hash_link)->obj_id == n) { + found = 0; } } } } + return n; } -/* - * Delete directory contents for cleaning up lost and found. - */ -static void yaffs_del_dir_contents(struct yaffs_obj *dir) +static void yaffs_hash_obj(struct yaffs_obj *in) { - struct yaffs_obj *obj; - struct list_head *lh; - struct list_head *n; + int bucket = yaffs_hash_fn(in->obj_id); + struct yaffs_dev *dev = in->my_dev; - if (dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) - YBUG(); + list_add(&in->hash_link, &dev->obj_bucket[bucket].list); + dev->obj_bucket[bucket].count++; +} - list_for_each_safe(lh, n, &dir->variant.dir_variant.children) { - if (lh) { - obj = list_entry(lh, struct yaffs_obj, siblings); - if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) - yaffs_del_dir_contents(obj); +struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number) +{ + int bucket = yaffs_hash_fn(number); + struct list_head *i; + struct yaffs_obj *in; - T(YAFFS_TRACE_SCAN, - (TSTR("Deleting lost_found object %d" TENDSTR), - obj->obj_id)); - - /* Need to use UnlinkObject since Delete would not handle - * hardlinked objects correctly. - */ - yaffs_unlink_obj(obj); + list_for_each(i, &dev->obj_bucket[bucket].list) { + /* Look if it is in the list */ + in = list_entry(i, struct yaffs_obj, hash_link); + if (in->obj_id == number) { + /* Don't show if it is defered free */ + if (in->defered_free) + return NULL; + return in; } } + return NULL; } -static void yaffs_empty_l_n_f(struct yaffs_dev *dev) +struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number, + enum yaffs_obj_type type) { - yaffs_del_dir_contents(dev->lost_n_found); -} + struct yaffs_obj *the_obj = NULL; + struct yaffs_tnode *tn = NULL; -static void yaffs_check_obj_details_loaded(struct yaffs_obj *in) -{ - u8 *chunk_data; - struct yaffs_obj_hdr *oh; - struct yaffs_dev *dev; - struct yaffs_ext_tags tags; - int result; - int alloc_failed = 0; + if (number < 0) + number = yaffs_new_obj_id(dev); - if (!in) - return; + if (type == YAFFS_OBJECT_TYPE_FILE) { + tn = yaffs_get_tnode(dev); + if (!tn) + return NULL; + } - dev = in->my_dev; + the_obj = yaffs_alloc_empty_obj(dev); + if (!the_obj) { + if (tn) + yaffs_free_tnode(dev, tn); + return NULL; + } - if (in->lazy_loaded && in->hdr_chunk > 0) { - in->lazy_loaded = 0; - chunk_data = yaffs_get_temp_buffer(dev, __LINE__); + if (the_obj) { + the_obj->fake = 0; + the_obj->rename_allowed = 1; + the_obj->unlink_allowed = 1; + the_obj->obj_id = number; + yaffs_hash_obj(the_obj); + the_obj->variant_type = type; + yaffs_load_current_time(the_obj, 1, 1); - result = - yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, chunk_data, - &tags); - oh = (struct yaffs_obj_hdr *)chunk_data; - - in->yst_mode = oh->yst_mode; - yaffs_load_attribs(in, oh); - yaffs_set_obj_name_from_oh(in, oh); - - if (in->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) { - in->variant.symlink_variant.alias = - yaffs_clone_str(oh->alias); - if (!in->variant.symlink_variant.alias) - alloc_failed = 1; /* Not returned to caller */ + switch (type) { + case YAFFS_OBJECT_TYPE_FILE: + the_obj->variant.file_variant.file_size = 0; + the_obj->variant.file_variant.scanned_size = 0; + the_obj->variant.file_variant.shrink_size = ~0; + /* max */ + the_obj->variant.file_variant.top_level = 0; + the_obj->variant.file_variant.top = tn; + break; + case YAFFS_OBJECT_TYPE_DIRECTORY: + INIT_LIST_HEAD(&the_obj->variant.dir_variant.children); + INIT_LIST_HEAD(&the_obj->variant.dir_variant.dirty); + break; + case YAFFS_OBJECT_TYPE_SYMLINK: + case YAFFS_OBJECT_TYPE_HARDLINK: + case YAFFS_OBJECT_TYPE_SPECIAL: + /* No action required */ + break; + case YAFFS_OBJECT_TYPE_UNKNOWN: + /* todo this should not happen */ + break; } + } - yaffs_release_temp_buffer(dev, chunk_data, __LINE__); + return the_obj; +} + +static struct yaffs_obj *yaffs_create_fake_dir(struct yaffs_dev *dev, + int number, u32 mode) +{ + + struct yaffs_obj *obj = + yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY); + + if (obj) { + obj->fake = 1; /* it is fake so it might not use NAND */ + obj->rename_allowed = 0; + obj->unlink_allowed = 0; + obj->deleted = 0; + obj->unlinked = 0; + obj->yst_mode = mode; + obj->my_dev = dev; + obj->hdr_chunk = 0; /* Not a valid chunk. */ + } + return obj; + +} + + +static void yaffs_init_tnodes_and_objs(struct yaffs_dev *dev) +{ + int i; + + dev->n_obj = 0; + dev->n_tnodes = 0; + yaffs_init_raw_tnodes_and_objs(dev); + + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) { + INIT_LIST_HEAD(&dev->obj_bucket[i].list); + dev->obj_bucket[i].count = 0; } } -/*------------------------------ Directory Functions ----------------------------- */ +struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev, + int number, + enum yaffs_obj_type type) +{ + struct yaffs_obj *the_obj = NULL; + if (number > 0) + the_obj = yaffs_find_by_number(dev, number); + + if (!the_obj) + the_obj = yaffs_new_obj(dev, number, type); + + return the_obj; + +} + +YCHAR *yaffs_clone_str(const YCHAR *str) +{ + YCHAR *new_str = NULL; + int len; + + if (!str) + str = _Y(""); + + len = strnlen(str, YAFFS_MAX_ALIAS_LENGTH); + new_str = kmalloc((len + 1) * sizeof(YCHAR), GFP_NOFS); + if (new_str) { + strncpy(new_str, str, len); + new_str[len] = 0; + } + return new_str; + +} /* *yaffs_update_parent() handles fixing a directories mtime and ctime when a new * link (ie. name) is created or deleted in the directory. @@ -4233,6 +2086,7 @@ static void yaffs_check_obj_details_loaded(struct yaffs_obj *in) static void yaffs_update_parent(struct yaffs_obj *obj) { struct yaffs_dev *dev; + if (!obj) return; dev = obj->my_dev; @@ -4243,13 +2097,14 @@ static void yaffs_update_parent(struct yaffs_obj *obj) if (list_empty(link)) { list_add(link, &dev->dirty_dirs); - T(YAFFS_TRACE_BACKGROUND, - (TSTR("Added object %d to dirty directories" TENDSTR), - obj->obj_id)); + yaffs_trace(YAFFS_TRACE_BACKGROUND, + "Added object %d to dirty directories", + obj->obj_id); } - } else + } else { yaffs_update_oh(obj, NULL, 0, 0, 0, NULL); + } } void yaffs_update_dirty_dirs(struct yaffs_dev *dev) @@ -4259,7 +2114,7 @@ void yaffs_update_dirty_dirs(struct yaffs_dev *dev) struct yaffs_dir_var *d_s; union yaffs_obj_var *o_v; - T(YAFFS_TRACE_BACKGROUND, (TSTR("Update dirty directories" TENDSTR))); + yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update dirty directories"); while (!list_empty(&dev->dirty_dirs)) { link = dev->dirty_dirs.next; @@ -4269,378 +2124,948 @@ void yaffs_update_dirty_dirs(struct yaffs_dev *dev) o_v = list_entry(d_s, union yaffs_obj_var, dir_variant); obj = list_entry(o_v, struct yaffs_obj, variant); - T(YAFFS_TRACE_BACKGROUND, - (TSTR("Update directory %d" TENDSTR), obj->obj_id)); + yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update directory %d", + obj->obj_id); if (obj->dirty) yaffs_update_oh(obj, NULL, 0, 0, 0, NULL); } } -static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj) +/* + * Mknod (create) a new object. + * equiv_obj only has meaning for a hard link; + * alias_str only has meaning for a symlink. + * rdev only has meaning for devices (a subset of special objects) + */ + +static struct yaffs_obj *yaffs_create_obj(enum yaffs_obj_type type, + struct yaffs_obj *parent, + const YCHAR *name, + u32 mode, + u32 uid, + u32 gid, + struct yaffs_obj *equiv_obj, + const YCHAR *alias_str, u32 rdev) { - struct yaffs_dev *dev = obj->my_dev; - struct yaffs_obj *parent; + struct yaffs_obj *in; + YCHAR *str = NULL; + struct yaffs_dev *dev = parent->my_dev; - yaffs_verify_obj_in_dir(obj); - parent = obj->parent; - - yaffs_verify_dir(parent); - - if (dev && dev->param.remove_obj_fn) - dev->param.remove_obj_fn(obj); - - list_del_init(&obj->siblings); - obj->parent = NULL; - - yaffs_verify_dir(parent); -} - -void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj) -{ - if (!directory) { - T(YAFFS_TRACE_ALWAYS, - (TSTR - ("tragedy: Trying to add an object to a null pointer directory" - TENDSTR))); - YBUG(); - return; - } - if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) { - T(YAFFS_TRACE_ALWAYS, - (TSTR - ("tragedy: Trying to add an object to a non-directory" - TENDSTR))); - YBUG(); - } - - if (obj->siblings.prev == NULL) { - /* Not initialised */ - YBUG(); - } - - yaffs_verify_dir(directory); - - yaffs_remove_obj_from_dir(obj); - - /* Now add it */ - list_add(&obj->siblings, &directory->variant.dir_variant.children); - obj->parent = directory; - - if (directory == obj->my_dev->unlinked_dir - || directory == obj->my_dev->del_dir) { - obj->unlinked = 1; - obj->my_dev->n_unlinked_files++; - obj->rename_allowed = 0; - } - - yaffs_verify_dir(directory); - yaffs_verify_obj_in_dir(obj); -} - -struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *directory, - const YCHAR * name) -{ - int sum; - - struct list_head *i; - YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1]; - - struct yaffs_obj *l; - - if (!name) + /* Check if the entry exists. + * If it does then fail the call since we don't want a dup. */ + if (yaffs_find_by_name(parent, name)) return NULL; - if (!directory) { - T(YAFFS_TRACE_ALWAYS, - (TSTR - ("tragedy: yaffs_find_by_name: null pointer directory" - TENDSTR))); - YBUG(); + if (type == YAFFS_OBJECT_TYPE_SYMLINK) { + str = yaffs_clone_str(alias_str); + if (!str) + return NULL; + } + + in = yaffs_new_obj(dev, -1, type); + + if (!in) { + kfree(str); return NULL; } - if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) { - T(YAFFS_TRACE_ALWAYS, - (TSTR("tragedy: yaffs_find_by_name: non-directory" TENDSTR))); - YBUG(); + + if (in) { + in->hdr_chunk = 0; + in->valid = 1; + in->variant_type = type; + + in->yst_mode = mode; + + yaffs_attribs_init(in, gid, uid, rdev); + + in->n_data_chunks = 0; + + yaffs_set_obj_name(in, name); + in->dirty = 1; + + yaffs_add_obj_to_dir(parent, in); + + in->my_dev = parent->my_dev; + + switch (type) { + case YAFFS_OBJECT_TYPE_SYMLINK: + in->variant.symlink_variant.alias = str; + break; + case YAFFS_OBJECT_TYPE_HARDLINK: + in->variant.hardlink_variant.equiv_obj = equiv_obj; + in->variant.hardlink_variant.equiv_id = + equiv_obj->obj_id; + list_add(&in->hard_links, &equiv_obj->hard_links); + break; + case YAFFS_OBJECT_TYPE_FILE: + case YAFFS_OBJECT_TYPE_DIRECTORY: + case YAFFS_OBJECT_TYPE_SPECIAL: + case YAFFS_OBJECT_TYPE_UNKNOWN: + /* do nothing */ + break; + } + + if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) { + /* Could not create the object header, fail */ + yaffs_del_obj(in); + in = NULL; + } + + yaffs_update_parent(parent); } - sum = yaffs_calc_name_sum(name); + return in; +} - list_for_each(i, &directory->variant.dir_variant.children) { - if (i) { - l = list_entry(i, struct yaffs_obj, siblings); +struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent, + const YCHAR *name, u32 mode, u32 uid, + u32 gid) +{ + return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode, + uid, gid, NULL, NULL, 0); +} - if (l->parent != directory) - YBUG(); +struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR *name, + u32 mode, u32 uid, u32 gid) +{ + return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name, + mode, uid, gid, NULL, NULL, 0); +} - yaffs_check_obj_details_loaded(l); +struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent, + const YCHAR *name, u32 mode, u32 uid, + u32 gid, u32 rdev) +{ + return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode, + uid, gid, NULL, NULL, rdev); +} - /* Special case for lost-n-found */ - if (l->obj_id == YAFFS_OBJECTID_LOSTNFOUND) { - if (yaffs_strcmp(name, YAFFS_LOSTNFOUND_NAME) == - 0) - return l; - } else if (yaffs_sum_cmp(l->sum, sum) - || l->hdr_chunk <= 0) { - /* LostnFound chunk called Objxxx - * Do a real check - */ - yaffs_get_obj_name(l, buffer, - YAFFS_MAX_NAME_LENGTH + 1); - if (yaffs_strncmp - (name, buffer, YAFFS_MAX_NAME_LENGTH) == 0) - return l; +struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent, + const YCHAR *name, u32 mode, u32 uid, + u32 gid, const YCHAR *alias) +{ + return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode, + uid, gid, NULL, alias, 0); +} + +/* yaffs_link_obj returns the object id of the equivalent object.*/ +struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name, + struct yaffs_obj *equiv_obj) +{ + /* Get the real object in case we were fed a hard link obj */ + equiv_obj = yaffs_get_equivalent_obj(equiv_obj); + + if (yaffs_create_obj + (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0, + equiv_obj, NULL, 0)) + return equiv_obj; + else + return NULL; + +} + + + +/*---------------------- Block Management and Page Allocation -------------*/ + +static int yaffs_init_blocks(struct yaffs_dev *dev) +{ + int n_blocks = dev->internal_end_block - dev->internal_start_block + 1; + + dev->block_info = NULL; + dev->chunk_bits = NULL; + dev->alloc_block = -1; /* force it to get a new one */ + + /* If the first allocation strategy fails, thry the alternate one */ + dev->block_info = + kmalloc(n_blocks * sizeof(struct yaffs_block_info), GFP_NOFS); + if (!dev->block_info) { + dev->block_info = + vmalloc(n_blocks * sizeof(struct yaffs_block_info)); + dev->block_info_alt = 1; + } else { + dev->block_info_alt = 0; + } + + if (dev->block_info) { + /* Set up dynamic blockinfo stuff. Round up bytes. */ + dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8; + dev->chunk_bits = + kmalloc(dev->chunk_bit_stride * n_blocks, GFP_NOFS); + if (!dev->chunk_bits) { + dev->chunk_bits = + vmalloc(dev->chunk_bit_stride * n_blocks); + dev->chunk_bits_alt = 1; + } else { + dev->chunk_bits_alt = 0; + } + } + + if (dev->block_info && dev->chunk_bits) { + memset(dev->block_info, 0, + n_blocks * sizeof(struct yaffs_block_info)); + memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks); + return YAFFS_OK; + } + + return YAFFS_FAIL; +} + +static void yaffs_deinit_blocks(struct yaffs_dev *dev) +{ + if (dev->block_info_alt && dev->block_info) + vfree(dev->block_info); + else + kfree(dev->block_info); + + dev->block_info_alt = 0; + + dev->block_info = NULL; + + if (dev->chunk_bits_alt && dev->chunk_bits) + vfree(dev->chunk_bits); + else + kfree(dev->chunk_bits); + dev->chunk_bits_alt = 0; + dev->chunk_bits = NULL; +} + +void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no) +{ + struct yaffs_block_info *bi = yaffs_get_block_info(dev, block_no); + int erased_ok = 0; + + /* If the block is still healthy erase it and mark as clean. + * If the block has had a data failure, then retire it. + */ + + yaffs_trace(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE, + "yaffs_block_became_dirty block %d state %d %s", + block_no, bi->block_state, + (bi->needs_retiring) ? "needs retiring" : ""); + + yaffs2_clear_oldest_dirty_seq(dev, bi); + + bi->block_state = YAFFS_BLOCK_STATE_DIRTY; + + /* If this is the block being garbage collected then stop gc'ing */ + if (block_no == dev->gc_block) + dev->gc_block = 0; + + /* If this block is currently the best candidate for gc + * then drop as a candidate */ + if (block_no == dev->gc_dirtiest) { + dev->gc_dirtiest = 0; + dev->gc_pages_in_use = 0; + } + + if (!bi->needs_retiring) { + yaffs2_checkpt_invalidate(dev); + erased_ok = yaffs_erase_block(dev, block_no); + if (!erased_ok) { + dev->n_erase_failures++; + yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS, + "**>> Erasure failed %d", block_no); + } + } + + if (erased_ok && + ((yaffs_trace_mask & YAFFS_TRACE_ERASE) + || !yaffs_skip_verification(dev))) { + int i; + for (i = 0; i < dev->param.chunks_per_block; i++) { + if (!yaffs_check_chunk_erased + (dev, block_no * dev->param.chunks_per_block + i)) { + yaffs_trace(YAFFS_TRACE_ERROR, + ">>Block %d erasure supposedly OK, but chunk %d not erased", + block_no, i); } } } - return NULL; + if (erased_ok) { + /* Clean it up... */ + bi->block_state = YAFFS_BLOCK_STATE_EMPTY; + bi->seq_number = 0; + dev->n_erased_blocks++; + bi->pages_in_use = 0; + bi->soft_del_pages = 0; + bi->has_shrink_hdr = 0; + bi->skip_erased_check = 1; /* Clean, so no need to check */ + bi->gc_prioritise = 0; + yaffs_clear_chunk_bits(dev, block_no); + + yaffs_trace(YAFFS_TRACE_ERASE, + "Erased block %d", block_no); + } else { + /* We lost a block of free space */ + dev->n_free_chunks -= dev->param.chunks_per_block; + yaffs_retire_block(dev, block_no); + yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS, + "**>> Block %d retired", block_no); + } } -/* GetEquivalentObject dereferences any hard links to get to the - * actual object. - */ - -struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj) +static int yaffs_gc_process_chunk(struct yaffs_dev *dev, + struct yaffs_block_info *bi, + int old_chunk, u8 *buffer) { - if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) { - /* We want the object id of the equivalent object, not this one */ - obj = obj->variant.hardlink_variant.equiv_obj; - yaffs_check_obj_details_loaded(obj); + int new_chunk; + int mark_flash = 1; + struct yaffs_ext_tags tags; + struct yaffs_obj *object; + int matching_chunk; + int ret_val = YAFFS_OK; + + yaffs_init_tags(&tags); + yaffs_rd_chunk_tags_nand(dev, old_chunk, + buffer, &tags); + object = yaffs_find_by_number(dev, tags.obj_id); + + yaffs_trace(YAFFS_TRACE_GC_DETAIL, + "Collecting chunk in block %d, %d %d %d ", + dev->gc_chunk, tags.obj_id, + tags.chunk_id, tags.n_bytes); + + if (object && !yaffs_skip_verification(dev)) { + if (tags.chunk_id == 0) + matching_chunk = + object->hdr_chunk; + else if (object->soft_del) + /* Defeat the test */ + matching_chunk = old_chunk; + else + matching_chunk = + yaffs_find_chunk_in_file + (object, tags.chunk_id, + NULL); + + if (old_chunk != matching_chunk) + yaffs_trace(YAFFS_TRACE_ERROR, + "gc: page in gc mismatch: %d %d %d %d", + old_chunk, + matching_chunk, + tags.obj_id, + tags.chunk_id); } - return obj; + + if (!object) { + yaffs_trace(YAFFS_TRACE_ERROR, + "page %d in gc has no object: %d %d %d ", + old_chunk, + tags.obj_id, tags.chunk_id, + tags.n_bytes); + } + + if (object && + object->deleted && + object->soft_del && tags.chunk_id != 0) { + /* Data chunk in a soft deleted file, + * throw it away. + * It's a soft deleted data chunk, + * No need to copy this, just forget + * about it and fix up the object. + */ + + /* Free chunks already includes + * softdeleted chunks, how ever this + * chunk is going to soon be really + * deleted which will increment free + * chunks. We have to decrement free + * chunks so this works out properly. + */ + dev->n_free_chunks--; + bi->soft_del_pages--; + + object->n_data_chunks--; + if (object->n_data_chunks <= 0) { + /* remeber to clean up obj */ + dev->gc_cleanup_list[dev->n_clean_ups] = tags.obj_id; + dev->n_clean_ups++; + } + mark_flash = 0; + } else if (object) { + /* It's either a data chunk in a live + * file or an ObjectHeader, so we're + * interested in it. + * NB Need to keep the ObjectHeaders of + * deleted files until the whole file + * has been deleted off + */ + tags.serial_number++; + dev->n_gc_copies++; + + if (tags.chunk_id == 0) { + /* It is an object Id, + * We need to nuke the + * shrinkheader flags since its + * work is done. + * Also need to clean up + * shadowing. + */ + struct yaffs_obj_hdr *oh; + oh = (struct yaffs_obj_hdr *) buffer; + + oh->is_shrink = 0; + tags.extra_is_shrink = 0; + oh->shadows_obj = 0; + oh->inband_shadowed_obj_id = 0; + tags.extra_shadows = 0; + + /* Update file size */ + if (object->variant_type == YAFFS_OBJECT_TYPE_FILE) { + oh->file_size = + object->variant.file_variant.file_size; + tags.extra_length = oh->file_size; + } + + yaffs_verify_oh(object, oh, &tags, 1); + new_chunk = + yaffs_write_new_chunk(dev, (u8 *) oh, &tags, 1); + } else { + new_chunk = + yaffs_write_new_chunk(dev, buffer, &tags, 1); + } + + if (new_chunk < 0) { + ret_val = YAFFS_FAIL; + } else { + + /* Now fix up the Tnodes etc. */ + + if (tags.chunk_id == 0) { + /* It's a header */ + object->hdr_chunk = new_chunk; + object->serial = tags.serial_number; + } else { + /* It's a data chunk */ + yaffs_put_chunk_in_file(object, tags.chunk_id, + new_chunk, 0); + } + } + } + if (ret_val == YAFFS_OK) + yaffs_chunk_del(dev, old_chunk, mark_flash, __LINE__); + return ret_val; +} + +static int yaffs_gc_block(struct yaffs_dev *dev, int block, int whole_block) +{ + int old_chunk; + int ret_val = YAFFS_OK; + int i; + int is_checkpt_block; + int max_copies; + int chunks_before = yaffs_get_erased_chunks(dev); + int chunks_after; + struct yaffs_block_info *bi = yaffs_get_block_info(dev, block); + + is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT); + + yaffs_trace(YAFFS_TRACE_TRACING, + "Collecting block %d, in use %d, shrink %d, whole_block %d", + block, bi->pages_in_use, bi->has_shrink_hdr, + whole_block); + + /*yaffs_verify_free_chunks(dev); */ + + if (bi->block_state == YAFFS_BLOCK_STATE_FULL) + bi->block_state = YAFFS_BLOCK_STATE_COLLECTING; + + bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */ + + dev->gc_disable = 1; + + if (is_checkpt_block || !yaffs_still_some_chunks(dev, block)) { + yaffs_trace(YAFFS_TRACE_TRACING, + "Collecting block %d that has no chunks in use", + block); + yaffs_block_became_dirty(dev, block); + } else { + + u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__); + + yaffs_verify_blk(dev, bi, block); + + max_copies = (whole_block) ? dev->param.chunks_per_block : 5; + old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk; + + for (/* init already done */ ; + ret_val == YAFFS_OK && + dev->gc_chunk < dev->param.chunks_per_block && + (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) && + max_copies > 0; + dev->gc_chunk++, old_chunk++) { + if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) { + /* Page is in use and might need to be copied */ + max_copies--; + ret_val = yaffs_gc_process_chunk(dev, bi, + old_chunk, buffer); + } + } + yaffs_release_temp_buffer(dev, buffer, __LINE__); + } + + yaffs_verify_collected_blk(dev, bi, block); + + if (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) { + /* + * The gc did not complete. Set block state back to FULL + * because checkpointing does not restore gc. + */ + bi->block_state = YAFFS_BLOCK_STATE_FULL; + } else { + /* The gc completed. */ + /* Do any required cleanups */ + for (i = 0; i < dev->n_clean_ups; i++) { + /* Time to delete the file too */ + struct yaffs_obj *object = + yaffs_find_by_number(dev, dev->gc_cleanup_list[i]); + if (object) { + yaffs_free_tnode(dev, + object->variant.file_variant.top); + object->variant.file_variant.top = NULL; + yaffs_trace(YAFFS_TRACE_GC, + "yaffs: About to finally delete object %d", + object->obj_id); + yaffs_generic_obj_del(object); + object->my_dev->n_deleted_files--; + } + + } + chunks_after = yaffs_get_erased_chunks(dev); + if (chunks_before >= chunks_after) + yaffs_trace(YAFFS_TRACE_GC, + "gc did not increase free chunks before %d after %d", + chunks_before, chunks_after); + dev->gc_block = 0; + dev->gc_chunk = 0; + dev->n_clean_ups = 0; + } + + dev->gc_disable = 0; + + return ret_val; } /* - * A note or two on object names. - * * If the object name is missing, we then make one up in the form objnnn - * - * * ASCII names are stored in the object header's name field from byte zero - * * Unicode names are historically stored starting from byte zero. - * - * Then there are automatic Unicode names... - * The purpose of these is to save names in a way that can be read as - * ASCII or Unicode names as appropriate, thus allowing a Unicode and ASCII - * system to share files. - * - * These automatic unicode are stored slightly differently... - * - If the name can fit in the ASCII character space then they are saved as - * ascii names as per above. - * - If the name needs Unicode then the name is saved in Unicode - * starting at oh->name[1]. - + * find_gc_block() selects the dirtiest block (or close enough) + * for garbage collection. */ -static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR * name, - int buffer_size) + +static unsigned yaffs_find_gc_block(struct yaffs_dev *dev, + int aggressive, int background) { - /* Create an object name if we could not find one. */ - if (yaffs_strnlen(name, YAFFS_MAX_NAME_LENGTH) == 0) { - YCHAR local_name[20]; - YCHAR num_string[20]; - YCHAR *x = &num_string[19]; - unsigned v = obj->obj_id; - num_string[19] = 0; - while (v > 0) { - x--; - *x = '0' + (v % 10); - v /= 10; + int i; + int iterations; + unsigned selected = 0; + int prioritised = 0; + int prioritised_exist = 0; + struct yaffs_block_info *bi; + int threshold; + + /* First let's see if we need to grab a prioritised block */ + if (dev->has_pending_prioritised_gc && !aggressive) { + dev->gc_dirtiest = 0; + bi = dev->block_info; + for (i = dev->internal_start_block; + i <= dev->internal_end_block && !selected; i++) { + + if (bi->gc_prioritise) { + prioritised_exist = 1; + if (bi->block_state == YAFFS_BLOCK_STATE_FULL && + yaffs_block_ok_for_gc(dev, bi)) { + selected = i; + prioritised = 1; + } + } + bi++; } - /* make up a name */ - yaffs_strcpy(local_name, YAFFS_LOSTNFOUND_PREFIX); - yaffs_strcat(local_name, x); - yaffs_strncpy(name, local_name, buffer_size - 1); + + /* + * If there is a prioritised block and none was selected then + * this happened because there is at least one old dirty block + * gumming up the works. Let's gc the oldest dirty block. + */ + + if (prioritised_exist && + !selected && dev->oldest_dirty_block > 0) + selected = dev->oldest_dirty_block; + + if (!prioritised_exist) /* None found, so we can clear this */ + dev->has_pending_prioritised_gc = 0; } -} -static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR * name, - const YCHAR * oh_name, int buff_size) -{ -#ifdef CONFIG_YAFFS_AUTO_UNICODE - if (dev->param.auto_unicode) { - if (*oh_name) { - /* It is an ASCII name, so do an ASCII to unicode conversion */ - const char *ascii_oh_name = (const char *)oh_name; - int n = buff_size - 1; - while (n > 0 && *ascii_oh_name) { - *name = *ascii_oh_name; - name++; - ascii_oh_name++; - n--; - } - } else - yaffs_strncpy(name, oh_name + 1, buff_size - 1); - } else -#endif - yaffs_strncpy(name, oh_name, buff_size - 1); -} + /* If we're doing aggressive GC then we are happy to take a less-dirty + * block, and search harder. + * else (leasurely gc), then we only bother to do this if the + * block has only a few pages in use. + */ -static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR * oh_name, - const YCHAR * name) -{ -#ifdef CONFIG_YAFFS_AUTO_UNICODE - - int is_ascii; - YCHAR *w; - - if (dev->param.auto_unicode) { - - is_ascii = 1; - w = name; - - /* Figure out if the name will fit in ascii character set */ - while (is_ascii && *w) { - if ((*w) & 0xff00) - is_ascii = 0; - w++; - } - - if (is_ascii) { - /* It is an ASCII name, so do a unicode to ascii conversion */ - char *ascii_oh_name = (char *)oh_name; - int n = YAFFS_MAX_NAME_LENGTH - 1; - while (n > 0 && *name) { - *ascii_oh_name = *name; - name++; - ascii_oh_name++; - n--; - } + if (!selected) { + int pages_used; + int n_blocks = + dev->internal_end_block - dev->internal_start_block + 1; + if (aggressive) { + threshold = dev->param.chunks_per_block; + iterations = n_blocks; } else { - /* It is a unicode name, so save starting at the second YCHAR */ - *oh_name = 0; - yaffs_strncpy(oh_name + 1, name, - YAFFS_MAX_NAME_LENGTH - 2); + int max_threshold; + + if (background) + max_threshold = dev->param.chunks_per_block / 2; + else + max_threshold = dev->param.chunks_per_block / 8; + + if (max_threshold < YAFFS_GC_PASSIVE_THRESHOLD) + max_threshold = YAFFS_GC_PASSIVE_THRESHOLD; + + threshold = background ? (dev->gc_not_done + 2) * 2 : 0; + if (threshold < YAFFS_GC_PASSIVE_THRESHOLD) + threshold = YAFFS_GC_PASSIVE_THRESHOLD; + if (threshold > max_threshold) + threshold = max_threshold; + + iterations = n_blocks / 16 + 1; + if (iterations > 100) + iterations = 100; } - } else -#endif - yaffs_strncpy(oh_name, name, YAFFS_MAX_NAME_LENGTH - 1); -} + for (i = 0; + i < iterations && + (dev->gc_dirtiest < 1 || + dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH); + i++) { + dev->gc_block_finder++; + if (dev->gc_block_finder < dev->internal_start_block || + dev->gc_block_finder > dev->internal_end_block) + dev->gc_block_finder = + dev->internal_start_block; -int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR * name, int buffer_size) -{ - memset(name, 0, buffer_size * sizeof(YCHAR)); + bi = yaffs_get_block_info(dev, dev->gc_block_finder); - yaffs_check_obj_details_loaded(obj); + pages_used = bi->pages_in_use - bi->soft_del_pages; - if (obj->obj_id == YAFFS_OBJECTID_LOSTNFOUND) { - yaffs_strncpy(name, YAFFS_LOSTNFOUND_NAME, buffer_size - 1); - } -#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM - else if (obj->short_name[0]) { - yaffs_strcpy(name, obj->short_name); - } -#endif - else if (obj->hdr_chunk > 0) { - int result; - u8 *buffer = yaffs_get_temp_buffer(obj->my_dev, __LINE__); - - struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)buffer; - - memset(buffer, 0, obj->my_dev->data_bytes_per_chunk); - - if (obj->hdr_chunk > 0) { - result = yaffs_rd_chunk_tags_nand(obj->my_dev, - obj->hdr_chunk, - buffer, NULL); + if (bi->block_state == YAFFS_BLOCK_STATE_FULL && + pages_used < dev->param.chunks_per_block && + (dev->gc_dirtiest < 1 || + pages_used < dev->gc_pages_in_use) && + yaffs_block_ok_for_gc(dev, bi)) { + dev->gc_dirtiest = dev->gc_block_finder; + dev->gc_pages_in_use = pages_used; + } } - yaffs_load_name_from_oh(obj->my_dev, name, oh->name, - buffer_size); - yaffs_release_temp_buffer(obj->my_dev, buffer, __LINE__); + if (dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold) + selected = dev->gc_dirtiest; } - yaffs_fix_null_name(obj, name, buffer_size); + /* + * If nothing has been selected for a while, try the oldest dirty + * because that's gumming up the works. + */ - return yaffs_strnlen(name, YAFFS_MAX_NAME_LENGTH); -} + if (!selected && dev->param.is_yaffs2 && + dev->gc_not_done >= (background ? 10 : 20)) { + yaffs2_find_oldest_dirty_seq(dev); + if (dev->oldest_dirty_block > 0) { + selected = dev->oldest_dirty_block; + dev->gc_dirtiest = selected; + dev->oldest_dirty_gc_count++; + bi = yaffs_get_block_info(dev, selected); + dev->gc_pages_in_use = + bi->pages_in_use - bi->soft_del_pages; + } else { + dev->gc_not_done = 0; + } + } -int yaffs_get_obj_length(struct yaffs_obj *obj) -{ - /* Dereference any hard linking */ - obj = yaffs_get_equivalent_obj(obj); + if (selected) { + yaffs_trace(YAFFS_TRACE_GC, + "GC Selected block %d with %d free, prioritised:%d", + selected, + dev->param.chunks_per_block - dev->gc_pages_in_use, + prioritised); - if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE) - return obj->variant.file_variant.file_size; - if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) { - if (!obj->variant.symlink_variant.alias) - return 0; - return yaffs_strnlen(obj->variant.symlink_variant.alias, - YAFFS_MAX_ALIAS_LENGTH); + dev->n_gc_blocks++; + if (background) + dev->bg_gcs++; + + dev->gc_dirtiest = 0; + dev->gc_pages_in_use = 0; + dev->gc_not_done = 0; + if (dev->refresh_skip > 0) + dev->refresh_skip--; } else { - /* Only a directory should drop through to here */ - return obj->my_dev->data_bytes_per_chunk; + dev->gc_not_done++; + yaffs_trace(YAFFS_TRACE_GC, + "GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s", + dev->gc_block_finder, dev->gc_not_done, threshold, + dev->gc_dirtiest, dev->gc_pages_in_use, + dev->oldest_dirty_block, background ? " bg" : ""); + } + + return selected; +} + +/* New garbage collector + * If we're very low on erased blocks then we do aggressive garbage collection + * otherwise we do "leasurely" garbage collection. + * Aggressive gc looks further (whole array) and will accept less dirty blocks. + * Passive gc only inspects smaller areas and only accepts more dirty blocks. + * + * The idea is to help clear out space in a more spread-out manner. + * Dunno if it really does anything useful. + */ +static int yaffs_check_gc(struct yaffs_dev *dev, int background) +{ + int aggressive = 0; + int gc_ok = YAFFS_OK; + int max_tries = 0; + int min_erased; + int erased_chunks; + int checkpt_block_adjust; + + if (dev->param.gc_control && (dev->param.gc_control(dev) & 1) == 0) + return YAFFS_OK; + + if (dev->gc_disable) { + /* Bail out so we don't get recursive gc */ + return YAFFS_OK; + } + + /* This loop should pass the first time. + * Only loops here if the collection does not increase space. + */ + + do { + max_tries++; + + checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev); + + min_erased = + dev->param.n_reserved_blocks + checkpt_block_adjust + 1; + erased_chunks = + dev->n_erased_blocks * dev->param.chunks_per_block; + + /* If we need a block soon then do aggressive gc. */ + if (dev->n_erased_blocks < min_erased) + aggressive = 1; + else { + if (!background + && erased_chunks > (dev->n_free_chunks / 4)) + break; + + if (dev->gc_skip > 20) + dev->gc_skip = 20; + if (erased_chunks < dev->n_free_chunks / 2 || + dev->gc_skip < 1 || background) + aggressive = 0; + else { + dev->gc_skip--; + break; + } + } + + dev->gc_skip = 5; + + /* If we don't already have a block being gc'd then see if we + * should start another */ + + if (dev->gc_block < 1 && !aggressive) { + dev->gc_block = yaffs2_find_refresh_block(dev); + dev->gc_chunk = 0; + dev->n_clean_ups = 0; + } + if (dev->gc_block < 1) { + dev->gc_block = + yaffs_find_gc_block(dev, aggressive, background); + dev->gc_chunk = 0; + dev->n_clean_ups = 0; + } + + if (dev->gc_block > 0) { + dev->all_gcs++; + if (!aggressive) + dev->passive_gc_count++; + + yaffs_trace(YAFFS_TRACE_GC, + "yaffs: GC n_erased_blocks %d aggressive %d", + dev->n_erased_blocks, aggressive); + + gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive); + } + + if (dev->n_erased_blocks < (dev->param.n_reserved_blocks) + && dev->gc_block > 0) { + yaffs_trace(YAFFS_TRACE_GC, + "yaffs: GC !!!no reclaim!!! n_erased_blocks %d after try %d block %d", + dev->n_erased_blocks, max_tries, + dev->gc_block); + } + } while ((dev->n_erased_blocks < dev->param.n_reserved_blocks) && + (dev->gc_block > 0) && (max_tries < 2)); + + return aggressive ? gc_ok : YAFFS_OK; +} + +/* + * yaffs_bg_gc() + * Garbage collects. Intended to be called from a background thread. + * Returns non-zero if at least half the free chunks are erased. + */ +int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency) +{ + int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block; + + yaffs_trace(YAFFS_TRACE_BACKGROUND, "Background gc %u", urgency); + + yaffs_check_gc(dev, 1); + return erased_chunks > dev->n_free_chunks / 2; +} + +/*-------------------- Data file manipulation -----------------*/ + +static int yaffs_rd_data_obj(struct yaffs_obj *in, int inode_chunk, u8 * buffer) +{ + int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL); + + if (nand_chunk >= 0) + return yaffs_rd_chunk_tags_nand(in->my_dev, nand_chunk, + buffer, NULL); + else { + yaffs_trace(YAFFS_TRACE_NANDACCESS, + "Chunk %d not found zero instead", + nand_chunk); + /* get sane (zero) data if you read a hole */ + memset(buffer, 0, in->my_dev->data_bytes_per_chunk); + return 0; + } + +} + +void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash, + int lyn) +{ + int block; + int page; + struct yaffs_ext_tags tags; + struct yaffs_block_info *bi; + + if (chunk_id <= 0) + return; + + dev->n_deletions++; + block = chunk_id / dev->param.chunks_per_block; + page = chunk_id % dev->param.chunks_per_block; + + if (!yaffs_check_chunk_bit(dev, block, page)) + yaffs_trace(YAFFS_TRACE_VERIFY, + "Deleting invalid chunk %d", chunk_id); + + bi = yaffs_get_block_info(dev, block); + + yaffs2_update_oldest_dirty_seq(dev, block, bi); + + yaffs_trace(YAFFS_TRACE_DELETION, + "line %d delete of chunk %d", + lyn, chunk_id); + + if (!dev->param.is_yaffs2 && mark_flash && + bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) { + + yaffs_init_tags(&tags); + tags.is_deleted = 1; + yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags); + yaffs_handle_chunk_update(dev, chunk_id, &tags); + } else { + dev->n_unmarked_deletions++; + } + + /* Pull out of the management area. + * If the whole block became dirty, this will kick off an erasure. + */ + if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING || + bi->block_state == YAFFS_BLOCK_STATE_FULL || + bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCANNING || + bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) { + dev->n_free_chunks++; + yaffs_clear_chunk_bit(dev, block, page); + bi->pages_in_use--; + + if (bi->pages_in_use == 0 && + !bi->has_shrink_hdr && + bi->block_state != YAFFS_BLOCK_STATE_ALLOCATING && + bi->block_state != YAFFS_BLOCK_STATE_NEEDS_SCANNING) { + yaffs_block_became_dirty(dev, block); + } } } -int yaffs_get_obj_link_count(struct yaffs_obj *obj) +static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk, + const u8 *buffer, int n_bytes, int use_reserve) { - int count = 0; - struct list_head *i; + /* Find old chunk Need to do this to get serial number + * Write new one and patch into tree. + * Invalidate old tags. + */ - if (!obj->unlinked) - count++; /* the object itself */ + int prev_chunk_id; + struct yaffs_ext_tags prev_tags; + int new_chunk_id; + struct yaffs_ext_tags new_tags; + struct yaffs_dev *dev = in->my_dev; - list_for_each(i, &obj->hard_links) - count++; /* add the hard links; */ + yaffs_check_gc(dev, 0); - return count; -} + /* Get the previous chunk at this location in the file if it exists. + * If it does not exist then put a zero into the tree. This creates + * the tnode now, rather than later when it is harder to clean up. + */ + prev_chunk_id = yaffs_find_chunk_in_file(in, inode_chunk, &prev_tags); + if (prev_chunk_id < 1 && + !yaffs_put_chunk_in_file(in, inode_chunk, 0, 0)) + return 0; -int yaffs_get_obj_inode(struct yaffs_obj *obj) -{ - obj = yaffs_get_equivalent_obj(obj); + /* Set up new tags */ + yaffs_init_tags(&new_tags); - return obj->obj_id; -} + new_tags.chunk_id = inode_chunk; + new_tags.obj_id = in->obj_id; + new_tags.serial_number = + (prev_chunk_id > 0) ? prev_tags.serial_number + 1 : 1; + new_tags.n_bytes = n_bytes; -unsigned yaffs_get_obj_type(struct yaffs_obj *obj) -{ - obj = yaffs_get_equivalent_obj(obj); - - switch (obj->variant_type) { - case YAFFS_OBJECT_TYPE_FILE: - return DT_REG; - break; - case YAFFS_OBJECT_TYPE_DIRECTORY: - return DT_DIR; - break; - case YAFFS_OBJECT_TYPE_SYMLINK: - return DT_LNK; - break; - case YAFFS_OBJECT_TYPE_HARDLINK: - return DT_REG; - break; - case YAFFS_OBJECT_TYPE_SPECIAL: - if (S_ISFIFO(obj->yst_mode)) - return DT_FIFO; - if (S_ISCHR(obj->yst_mode)) - return DT_CHR; - if (S_ISBLK(obj->yst_mode)) - return DT_BLK; - if (S_ISSOCK(obj->yst_mode)) - return DT_SOCK; - default: - return DT_REG; - break; + if (n_bytes < 1 || n_bytes > dev->param.total_bytes_per_chunk) { + yaffs_trace(YAFFS_TRACE_ERROR, + "Writing %d bytes to chunk!!!!!!!!!", + n_bytes); + YBUG(); } + + new_chunk_id = + yaffs_write_new_chunk(dev, buffer, &new_tags, use_reserve); + + if (new_chunk_id > 0) { + yaffs_put_chunk_in_file(in, inode_chunk, new_chunk_id, 0); + + if (prev_chunk_id > 0) + yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__); + + yaffs_verify_file_sane(in); + } + return new_chunk_id; + } -YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj) -{ - obj = yaffs_get_equivalent_obj(obj); - if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) - return yaffs_clone_str(obj->variant.symlink_variant.alias); - else - return yaffs_clone_str(_Y("")); -} + static int yaffs_do_xattrib_mod(struct yaffs_obj *obj, int set, - const YCHAR * name, const void *value, int size, + const YCHAR *name, const void *value, int size, int flags) { struct yaffs_xattr_mod xmod; - int result; xmod.set = set; @@ -4665,7 +3090,6 @@ static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer, int x_offs = sizeof(struct yaffs_obj_hdr); struct yaffs_dev *dev = obj->my_dev; int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr); - char *x_buffer = buffer + x_offs; if (xmod->set) @@ -4677,13 +3101,12 @@ static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer, obj->has_xattr = nval_hasvalues(x_buffer, x_size); obj->xattr_known = 1; - xmod->result = retval; return retval; } -static int yaffs_do_xattrib_fetch(struct yaffs_obj *obj, const YCHAR * name, +static int yaffs_do_xattrib_fetch(struct yaffs_obj *obj, const YCHAR *name, void *value, int size) { char *buffer = NULL; @@ -4692,9 +3115,7 @@ static int yaffs_do_xattrib_fetch(struct yaffs_obj *obj, const YCHAR * name, struct yaffs_dev *dev = obj->my_dev; int x_offs = sizeof(struct yaffs_obj_hdr); int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr); - char *x_buffer; - int retval = 0; if (obj->hdr_chunk < 1) @@ -4758,11 +3179,1403 @@ int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size) return yaffs_do_xattrib_fetch(obj, NULL, buffer, size); } -/*---------------------------- Initialisation code -------------------------------------- */ +static void yaffs_check_obj_details_loaded(struct yaffs_obj *in) +{ + u8 *chunk_data; + struct yaffs_obj_hdr *oh; + struct yaffs_dev *dev; + struct yaffs_ext_tags tags; + int result; + int alloc_failed = 0; + + if (!in) + return; + + dev = in->my_dev; + + if (in->lazy_loaded && in->hdr_chunk > 0) { + in->lazy_loaded = 0; + chunk_data = yaffs_get_temp_buffer(dev, __LINE__); + + result = + yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, chunk_data, + &tags); + oh = (struct yaffs_obj_hdr *)chunk_data; + + in->yst_mode = oh->yst_mode; + yaffs_load_attribs(in, oh); + yaffs_set_obj_name_from_oh(in, oh); + + if (in->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) { + in->variant.symlink_variant.alias = + yaffs_clone_str(oh->alias); + if (!in->variant.symlink_variant.alias) + alloc_failed = 1; /* Not returned */ + } + + yaffs_release_temp_buffer(dev, chunk_data, __LINE__); + } +} + +static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR *name, + const YCHAR *oh_name, int buff_size) +{ +#ifdef CONFIG_YAFFS_AUTO_UNICODE + if (dev->param.auto_unicode) { + if (*oh_name) { + /* It is an ASCII name, do an ASCII to + * unicode conversion */ + const char *ascii_oh_name = (const char *)oh_name; + int n = buff_size - 1; + while (n > 0 && *ascii_oh_name) { + *name = *ascii_oh_name; + name++; + ascii_oh_name++; + n--; + } + } else { + strncpy(name, oh_name + 1, buff_size - 1); + } + } else { +#else + { +#endif + strncpy(name, oh_name, buff_size - 1); + } +} + +static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR *oh_name, + const YCHAR *name) +{ +#ifdef CONFIG_YAFFS_AUTO_UNICODE + + int is_ascii; + YCHAR *w; + + if (dev->param.auto_unicode) { + + is_ascii = 1; + w = name; + + /* Figure out if the name will fit in ascii character set */ + while (is_ascii && *w) { + if ((*w) & 0xff00) + is_ascii = 0; + w++; + } + + if (is_ascii) { + /* It is an ASCII name, so convert unicode to ascii */ + char *ascii_oh_name = (char *)oh_name; + int n = YAFFS_MAX_NAME_LENGTH - 1; + while (n > 0 && *name) { + *ascii_oh_name = *name; + name++; + ascii_oh_name++; + n--; + } + } else { + /* Unicode name, so save starting at the second YCHAR */ + *oh_name = 0; + strncpy(oh_name + 1, name, + YAFFS_MAX_NAME_LENGTH - 2); + } + } else { +#else + { +#endif + strncpy(oh_name, name, YAFFS_MAX_NAME_LENGTH - 1); + } +} + +/* UpdateObjectHeader updates the header on NAND for an object. + * If name is not NULL, then that new name is used. + */ +int yaffs_update_oh(struct yaffs_obj *in, const YCHAR *name, int force, + int is_shrink, int shadows, struct yaffs_xattr_mod *xmod) +{ + + struct yaffs_block_info *bi; + struct yaffs_dev *dev = in->my_dev; + int prev_chunk_id; + int ret_val = 0; + int result = 0; + int new_chunk_id; + struct yaffs_ext_tags new_tags; + struct yaffs_ext_tags old_tags; + const YCHAR *alias = NULL; + u8 *buffer = NULL; + YCHAR old_name[YAFFS_MAX_NAME_LENGTH + 1]; + struct yaffs_obj_hdr *oh = NULL; + + strcpy(old_name, _Y("silly old name")); + + if (!in->fake || in == dev->root_dir || + force || xmod) { + + yaffs_check_gc(dev, 0); + yaffs_check_obj_details_loaded(in); + + buffer = yaffs_get_temp_buffer(in->my_dev, __LINE__); + oh = (struct yaffs_obj_hdr *)buffer; + + prev_chunk_id = in->hdr_chunk; + + if (prev_chunk_id > 0) { + result = yaffs_rd_chunk_tags_nand(dev, prev_chunk_id, + buffer, &old_tags); + + yaffs_verify_oh(in, oh, &old_tags, 0); + + memcpy(old_name, oh->name, sizeof(oh->name)); + memset(buffer, 0xFF, sizeof(struct yaffs_obj_hdr)); + } else { + memset(buffer, 0xFF, dev->data_bytes_per_chunk); + } + + oh->type = in->variant_type; + oh->yst_mode = in->yst_mode; + oh->shadows_obj = oh->inband_shadowed_obj_id = shadows; + + yaffs_load_attribs_oh(oh, in); + + if (in->parent) + oh->parent_obj_id = in->parent->obj_id; + else + oh->parent_obj_id = 0; + + if (name && *name) { + memset(oh->name, 0, sizeof(oh->name)); + yaffs_load_oh_from_name(dev, oh->name, name); + } else if (prev_chunk_id > 0) { + memcpy(oh->name, old_name, sizeof(oh->name)); + } else { + memset(oh->name, 0, sizeof(oh->name)); + } + + oh->is_shrink = is_shrink; + + switch (in->variant_type) { + case YAFFS_OBJECT_TYPE_UNKNOWN: + /* Should not happen */ + break; + case YAFFS_OBJECT_TYPE_FILE: + oh->file_size = + (oh->parent_obj_id == YAFFS_OBJECTID_DELETED + || oh->parent_obj_id == + YAFFS_OBJECTID_UNLINKED) ? 0 : in-> + variant.file_variant.file_size; + break; + case YAFFS_OBJECT_TYPE_HARDLINK: + oh->equiv_id = in->variant.hardlink_variant.equiv_id; + break; + case YAFFS_OBJECT_TYPE_SPECIAL: + /* Do nothing */ + break; + case YAFFS_OBJECT_TYPE_DIRECTORY: + /* Do nothing */ + break; + case YAFFS_OBJECT_TYPE_SYMLINK: + alias = in->variant.symlink_variant.alias; + if (!alias) + alias = _Y("no alias"); + strncpy(oh->alias, alias, YAFFS_MAX_ALIAS_LENGTH); + oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0; + break; + } + + /* process any xattrib modifications */ + if (xmod) + yaffs_apply_xattrib_mod(in, (char *)buffer, xmod); + + /* Tags */ + yaffs_init_tags(&new_tags); + in->serial++; + new_tags.chunk_id = 0; + new_tags.obj_id = in->obj_id; + new_tags.serial_number = in->serial; + + /* Add extra info for file header */ + new_tags.extra_available = 1; + new_tags.extra_parent_id = oh->parent_obj_id; + new_tags.extra_length = oh->file_size; + new_tags.extra_is_shrink = oh->is_shrink; + new_tags.extra_equiv_id = oh->equiv_id; + new_tags.extra_shadows = (oh->shadows_obj > 0) ? 1 : 0; + new_tags.extra_obj_type = in->variant_type; + yaffs_verify_oh(in, oh, &new_tags, 1); + + /* Create new chunk in NAND */ + new_chunk_id = + yaffs_write_new_chunk(dev, buffer, &new_tags, + (prev_chunk_id > 0) ? 1 : 0); + + if (new_chunk_id >= 0) { + + in->hdr_chunk = new_chunk_id; + + if (prev_chunk_id > 0) { + yaffs_chunk_del(dev, prev_chunk_id, 1, + __LINE__); + } + + if (!yaffs_obj_cache_dirty(in)) + in->dirty = 0; + + /* If this was a shrink, then mark the block + * that the chunk lives on */ + if (is_shrink) { + bi = yaffs_get_block_info(in->my_dev, + new_chunk_id / + in->my_dev->param. + chunks_per_block); + bi->has_shrink_hdr = 1; + } + } + ret_val = new_chunk_id; + } + + if (buffer) + yaffs_release_temp_buffer(dev, buffer, __LINE__); + + return ret_val; +} + +/*--------------------- File read/write ------------------------ + * Read and write have very similar structures. + * In general the read/write has three parts to it + * An incomplete chunk to start with (if the read/write is not chunk-aligned) + * Some complete chunks + * An incomplete chunk to end off with + * + * Curve-balls: the first chunk might also be the last chunk. + */ + +int yaffs_file_rd(struct yaffs_obj *in, u8 * buffer, loff_t offset, int n_bytes) +{ + int chunk; + u32 start; + int n_copy; + int n = n_bytes; + int n_done = 0; + struct yaffs_cache *cache; + struct yaffs_dev *dev; + + dev = in->my_dev; + + while (n > 0) { + yaffs_addr_to_chunk(dev, offset, &chunk, &start); + chunk++; + + /* OK now check for the curveball where the start and end are in + * the same chunk. + */ + if ((start + n) < dev->data_bytes_per_chunk) + n_copy = n; + else + n_copy = dev->data_bytes_per_chunk - start; + + cache = yaffs_find_chunk_cache(in, chunk); + + /* If the chunk is already in the cache or it is less than + * a whole chunk or we're using inband tags then use the cache + * (if there is caching) else bypass the cache. + */ + if (cache || n_copy != dev->data_bytes_per_chunk + || dev->param.inband_tags) { + if (dev->param.n_caches > 0) { + + /* If we can't find the data in the cache, + * then load it up. */ + + if (!cache) { + cache = + yaffs_grab_chunk_cache(in->my_dev); + cache->object = in; + cache->chunk_id = chunk; + cache->dirty = 0; + cache->locked = 0; + yaffs_rd_data_obj(in, chunk, + cache->data); + cache->n_bytes = 0; + } + + yaffs_use_cache(dev, cache, 0); + + cache->locked = 1; + + memcpy(buffer, &cache->data[start], n_copy); + + cache->locked = 0; + } else { + /* Read into the local buffer then copy.. */ + + u8 *local_buffer = + yaffs_get_temp_buffer(dev, __LINE__); + yaffs_rd_data_obj(in, chunk, local_buffer); + + memcpy(buffer, &local_buffer[start], n_copy); + + yaffs_release_temp_buffer(dev, local_buffer, + __LINE__); + } + } else { + /* A full chunk. Read directly into the buffer. */ + yaffs_rd_data_obj(in, chunk, buffer); + } + n -= n_copy; + offset += n_copy; + buffer += n_copy; + n_done += n_copy; + } + return n_done; +} + +int yaffs_do_file_wr(struct yaffs_obj *in, const u8 *buffer, loff_t offset, + int n_bytes, int write_trhrough) +{ + + int chunk; + u32 start; + int n_copy; + int n = n_bytes; + int n_done = 0; + int n_writeback; + int start_write = offset; + int chunk_written = 0; + u32 n_bytes_read; + u32 chunk_start; + struct yaffs_dev *dev; + + dev = in->my_dev; + + while (n > 0 && chunk_written >= 0) { + yaffs_addr_to_chunk(dev, offset, &chunk, &start); + + if (chunk * dev->data_bytes_per_chunk + start != offset || + start >= dev->data_bytes_per_chunk) { + yaffs_trace(YAFFS_TRACE_ERROR, + "AddrToChunk of offset %d gives chunk %d start %d", + (int)offset, chunk, start); + } + chunk++; /* File pos to chunk in file offset */ + + /* OK now check for the curveball where the start and end are in + * the same chunk. + */ + + if ((start + n) < dev->data_bytes_per_chunk) { + n_copy = n; + + /* Now calculate how many bytes to write back.... + * If we're overwriting and not writing to then end of + * file then we need to write back as much as was there + * before. + */ + + chunk_start = ((chunk - 1) * dev->data_bytes_per_chunk); + + if (chunk_start > in->variant.file_variant.file_size) + n_bytes_read = 0; /* Past end of file */ + else + n_bytes_read = + in->variant.file_variant.file_size - + chunk_start; + + if (n_bytes_read > dev->data_bytes_per_chunk) + n_bytes_read = dev->data_bytes_per_chunk; + + n_writeback = + (n_bytes_read > + (start + n)) ? n_bytes_read : (start + n); + + if (n_writeback < 0 || + n_writeback > dev->data_bytes_per_chunk) + YBUG(); + + } else { + n_copy = dev->data_bytes_per_chunk - start; + n_writeback = dev->data_bytes_per_chunk; + } + + if (n_copy != dev->data_bytes_per_chunk || + dev->param.inband_tags) { + /* An incomplete start or end chunk (or maybe both + * start and end chunk), or we're using inband tags, + * so we want to use the cache buffers. + */ + if (dev->param.n_caches > 0) { + struct yaffs_cache *cache; + + /* If we can't find the data in the cache, then + * load the cache */ + cache = yaffs_find_chunk_cache(in, chunk); + + if (!cache + && yaffs_check_alloc_available(dev, 1)) { + cache = yaffs_grab_chunk_cache(dev); + cache->object = in; + cache->chunk_id = chunk; + cache->dirty = 0; + cache->locked = 0; + yaffs_rd_data_obj(in, chunk, + cache->data); + } else if (cache && + !cache->dirty && + !yaffs_check_alloc_available(dev, + 1)) { + /* Drop the cache if it was a read cache + * item and no space check has been made + * for it. + */ + cache = NULL; + } + + if (cache) { + yaffs_use_cache(dev, cache, 1); + cache->locked = 1; + + memcpy(&cache->data[start], buffer, + n_copy); + + cache->locked = 0; + cache->n_bytes = n_writeback; + + if (write_trhrough) { + chunk_written = + yaffs_wr_data_obj + (cache->object, + cache->chunk_id, + cache->data, + cache->n_bytes, 1); + cache->dirty = 0; + } + } else { + chunk_written = -1; /* fail write */ + } + } else { + /* An incomplete start or end chunk (or maybe + * both start and end chunk). Read into the + * local buffer then copy over and write back. + */ + + u8 *local_buffer = + yaffs_get_temp_buffer(dev, __LINE__); + + yaffs_rd_data_obj(in, chunk, local_buffer); + memcpy(&local_buffer[start], buffer, n_copy); + + chunk_written = + yaffs_wr_data_obj(in, chunk, + local_buffer, + n_writeback, 0); + + yaffs_release_temp_buffer(dev, local_buffer, + __LINE__); + } + } else { + /* A full chunk. Write directly from the buffer. */ + + chunk_written = + yaffs_wr_data_obj(in, chunk, buffer, + dev->data_bytes_per_chunk, 0); + + /* Since we've overwritten the cached data, + * we better invalidate it. */ + yaffs_invalidate_chunk_cache(in, chunk); + } + + if (chunk_written >= 0) { + n -= n_copy; + offset += n_copy; + buffer += n_copy; + n_done += n_copy; + } + } + + /* Update file object */ + + if ((start_write + n_done) > in->variant.file_variant.file_size) + in->variant.file_variant.file_size = (start_write + n_done); + + in->dirty = 1; + return n_done; +} + +int yaffs_wr_file(struct yaffs_obj *in, const u8 *buffer, loff_t offset, + int n_bytes, int write_trhrough) +{ + yaffs2_handle_hole(in, offset); + return yaffs_do_file_wr(in, buffer, offset, n_bytes, write_trhrough); +} + +/* ---------------------- File resizing stuff ------------------ */ + +static void yaffs_prune_chunks(struct yaffs_obj *in, int new_size) +{ + + struct yaffs_dev *dev = in->my_dev; + int old_size = in->variant.file_variant.file_size; + int i; + int chunk_id; + int last_del = 1 + (old_size - 1) / dev->data_bytes_per_chunk; + int start_del = 1 + (new_size + dev->data_bytes_per_chunk - 1) / + dev->data_bytes_per_chunk; + + + /* Delete backwards so that we don't end up with holes if + * power is lost part-way through the operation. + */ + for (i = last_del; i >= start_del; i--) { + /* NB this could be optimised somewhat, + * eg. could retrieve the tags and write them without + * using yaffs_chunk_del + */ + + chunk_id = yaffs_find_del_file_chunk(in, i, NULL); + if (chunk_id > 0) { + if (chunk_id < + (dev->internal_start_block * + dev->param.chunks_per_block) || + chunk_id >= + ((dev->internal_end_block + 1) * + dev->param.chunks_per_block)) { + yaffs_trace(YAFFS_TRACE_ALWAYS, + "Found daft chunk_id %d for %d", + chunk_id, i); + } else { + in->n_data_chunks--; + yaffs_chunk_del(dev, chunk_id, 1, __LINE__); + } + } + } +} + +void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size) +{ + int new_full; + u32 new_partial; + struct yaffs_dev *dev = obj->my_dev; + + yaffs_addr_to_chunk(dev, new_size, &new_full, &new_partial); + + yaffs_prune_chunks(obj, new_size); + + if (new_partial != 0) { + int last_chunk = 1 + new_full; + u8 *local_buffer = yaffs_get_temp_buffer(dev, __LINE__); + + /* Rewrite the last chunk with its new size and zero pad */ + yaffs_rd_data_obj(obj, last_chunk, local_buffer); + memset(local_buffer + new_partial, 0, + dev->data_bytes_per_chunk - new_partial); + + yaffs_wr_data_obj(obj, last_chunk, local_buffer, + new_partial, 1); + + yaffs_release_temp_buffer(dev, local_buffer, __LINE__); + } + + obj->variant.file_variant.file_size = new_size; + + yaffs_prune_tree(dev, &obj->variant.file_variant); +} + +int yaffs_resize_file(struct yaffs_obj *in, loff_t new_size) +{ + struct yaffs_dev *dev = in->my_dev; + int old_size = in->variant.file_variant.file_size; + + yaffs_flush_file_cache(in); + yaffs_invalidate_whole_cache(in); + + yaffs_check_gc(dev, 0); + + if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) + return YAFFS_FAIL; + + if (new_size == old_size) + return YAFFS_OK; + + if (new_size > old_size) { + yaffs2_handle_hole(in, new_size); + in->variant.file_variant.file_size = new_size; + } else { + /* new_size < old_size */ + yaffs_resize_file_down(in, new_size); + } + + /* Write a new object header to reflect the resize. + * show we've shrunk the file, if need be + * Do this only if the file is not in the deleted directories + * and is not shadowed. + */ + if (in->parent && + !in->is_shadowed && + in->parent->obj_id != YAFFS_OBJECTID_UNLINKED && + in->parent->obj_id != YAFFS_OBJECTID_DELETED) + yaffs_update_oh(in, NULL, 0, 0, 0, NULL); + + return YAFFS_OK; +} + +int yaffs_flush_file(struct yaffs_obj *in, int update_time, int data_sync) +{ + int ret_val; + + if (in->dirty) { + yaffs_flush_file_cache(in); + if (data_sync) /* Only sync data */ + ret_val = YAFFS_OK; + else { + if (update_time) + yaffs_load_current_time(in, 0, 0); + + ret_val = (yaffs_update_oh(in, NULL, 0, 0, 0, NULL) >= + 0) ? YAFFS_OK : YAFFS_FAIL; + } + } else { + ret_val = YAFFS_OK; + } + return ret_val; +} + + +/* yaffs_del_file deletes the whole file data + * and the inode associated with the file. + * It does not delete the links associated with the file. + */ +static int yaffs_unlink_file_if_needed(struct yaffs_obj *in) +{ + int ret_val; + int del_now = 0; + struct yaffs_dev *dev = in->my_dev; + + if (!in->my_inode) + del_now = 1; + + if (del_now) { + ret_val = + yaffs_change_obj_name(in, in->my_dev->del_dir, + _Y("deleted"), 0, 0); + yaffs_trace(YAFFS_TRACE_TRACING, + "yaffs: immediate deletion of file %d", + in->obj_id); + in->deleted = 1; + in->my_dev->n_deleted_files++; + if (dev->param.disable_soft_del || dev->param.is_yaffs2) + yaffs_resize_file(in, 0); + yaffs_soft_del_file(in); + } else { + ret_val = + yaffs_change_obj_name(in, in->my_dev->unlinked_dir, + _Y("unlinked"), 0, 0); + } + return ret_val; +} + +int yaffs_del_file(struct yaffs_obj *in) +{ + int ret_val = YAFFS_OK; + int deleted; /* Need to cache value on stack if in is freed */ + struct yaffs_dev *dev = in->my_dev; + + if (dev->param.disable_soft_del || dev->param.is_yaffs2) + yaffs_resize_file(in, 0); + + if (in->n_data_chunks > 0) { + /* Use soft deletion if there is data in the file. + * That won't be the case if it has been resized to zero. + */ + if (!in->unlinked) + ret_val = yaffs_unlink_file_if_needed(in); + + deleted = in->deleted; + + if (ret_val == YAFFS_OK && in->unlinked && !in->deleted) { + in->deleted = 1; + deleted = 1; + in->my_dev->n_deleted_files++; + yaffs_soft_del_file(in); + } + return deleted ? YAFFS_OK : YAFFS_FAIL; + } else { + /* The file has no data chunks so we toss it immediately */ + yaffs_free_tnode(in->my_dev, in->variant.file_variant.top); + in->variant.file_variant.top = NULL; + yaffs_generic_obj_del(in); + + return YAFFS_OK; + } +} + +int yaffs_is_non_empty_dir(struct yaffs_obj *obj) +{ + return (obj && + obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) && + !(list_empty(&obj->variant.dir_variant.children)); +} + +static int yaffs_del_dir(struct yaffs_obj *obj) +{ + /* First check that the directory is empty. */ + if (yaffs_is_non_empty_dir(obj)) + return YAFFS_FAIL; + + return yaffs_generic_obj_del(obj); +} + +static int yaffs_del_symlink(struct yaffs_obj *in) +{ + kfree(in->variant.symlink_variant.alias); + in->variant.symlink_variant.alias = NULL; + + return yaffs_generic_obj_del(in); +} + +static int yaffs_del_link(struct yaffs_obj *in) +{ + /* remove this hardlink from the list associated with the equivalent + * object + */ + list_del_init(&in->hard_links); + return yaffs_generic_obj_del(in); +} + +int yaffs_del_obj(struct yaffs_obj *obj) +{ + int ret_val = -1; + + switch (obj->variant_type) { + case YAFFS_OBJECT_TYPE_FILE: + ret_val = yaffs_del_file(obj); + break; + case YAFFS_OBJECT_TYPE_DIRECTORY: + if (!list_empty(&obj->variant.dir_variant.dirty)) { + yaffs_trace(YAFFS_TRACE_BACKGROUND, + "Remove object %d from dirty directories", + obj->obj_id); + list_del_init(&obj->variant.dir_variant.dirty); + } + return yaffs_del_dir(obj); + break; + case YAFFS_OBJECT_TYPE_SYMLINK: + ret_val = yaffs_del_symlink(obj); + break; + case YAFFS_OBJECT_TYPE_HARDLINK: + ret_val = yaffs_del_link(obj); + break; + case YAFFS_OBJECT_TYPE_SPECIAL: + ret_val = yaffs_generic_obj_del(obj); + break; + case YAFFS_OBJECT_TYPE_UNKNOWN: + ret_val = 0; + break; /* should not happen. */ + } + return ret_val; +} + +static int yaffs_unlink_worker(struct yaffs_obj *obj) +{ + int del_now = 0; + + if (!obj) + return YAFFS_FAIL; + + if (!obj->my_inode) + del_now = 1; + + yaffs_update_parent(obj->parent); + + if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) { + return yaffs_del_link(obj); + } else if (!list_empty(&obj->hard_links)) { + /* Curve ball: We're unlinking an object that has a hardlink. + * + * This problem arises because we are not strictly following + * The Linux link/inode model. + * + * We can't really delete the object. + * Instead, we do the following: + * - Select a hardlink. + * - Unhook it from the hard links + * - Move it from its parent directory so that the rename works. + * - Rename the object to the hardlink's name. + * - Delete the hardlink + */ + + struct yaffs_obj *hl; + struct yaffs_obj *parent; + int ret_val; + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1]; + + hl = list_entry(obj->hard_links.next, struct yaffs_obj, + hard_links); + + yaffs_get_obj_name(hl, name, YAFFS_MAX_NAME_LENGTH + 1); + parent = hl->parent; + + list_del_init(&hl->hard_links); + + yaffs_add_obj_to_dir(obj->my_dev->unlinked_dir, hl); + + ret_val = yaffs_change_obj_name(obj, parent, name, 0, 0); + + if (ret_val == YAFFS_OK) + ret_val = yaffs_generic_obj_del(hl); + + return ret_val; + + } else if (del_now) { + switch (obj->variant_type) { + case YAFFS_OBJECT_TYPE_FILE: + return yaffs_del_file(obj); + break; + case YAFFS_OBJECT_TYPE_DIRECTORY: + list_del_init(&obj->variant.dir_variant.dirty); + return yaffs_del_dir(obj); + break; + case YAFFS_OBJECT_TYPE_SYMLINK: + return yaffs_del_symlink(obj); + break; + case YAFFS_OBJECT_TYPE_SPECIAL: + return yaffs_generic_obj_del(obj); + break; + case YAFFS_OBJECT_TYPE_HARDLINK: + case YAFFS_OBJECT_TYPE_UNKNOWN: + default: + return YAFFS_FAIL; + } + } else if (yaffs_is_non_empty_dir(obj)) { + return YAFFS_FAIL; + } else { + return yaffs_change_obj_name(obj, obj->my_dev->unlinked_dir, + _Y("unlinked"), 0, 0); + } +} + +static int yaffs_unlink_obj(struct yaffs_obj *obj) +{ + if (obj && obj->unlink_allowed) + return yaffs_unlink_worker(obj); + + return YAFFS_FAIL; +} + +int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR *name) +{ + struct yaffs_obj *obj; + + obj = yaffs_find_by_name(dir, name); + return yaffs_unlink_obj(obj); +} + +/* Note: + * If old_name is NULL then we take old_dir as the object to be renamed. + */ +int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR *old_name, + struct yaffs_obj *new_dir, const YCHAR *new_name) +{ + struct yaffs_obj *obj = NULL; + struct yaffs_obj *existing_target = NULL; + int force = 0; + int result; + struct yaffs_dev *dev; + + if (!old_dir || old_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) { + YBUG(); + return YAFFS_FAIL; + } + if (!new_dir || new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) { + YBUG(); + return YAFFS_FAIL; + } + + dev = old_dir->my_dev; + +#ifdef CONFIG_YAFFS_CASE_INSENSITIVE + /* Special case for case insemsitive systems. + * While look-up is case insensitive, the name isn't. + * Therefore we might want to change x.txt to X.txt + */ + if (old_dir == new_dir && + old_name && new_name && + strcmp(old_name, new_name) == 0) + force = 1; +#endif + + if (strnlen(new_name, YAFFS_MAX_NAME_LENGTH + 1) > + YAFFS_MAX_NAME_LENGTH) + /* ENAMETOOLONG */ + return YAFFS_FAIL; + + if (old_name) + obj = yaffs_find_by_name(old_dir, old_name); + else{ + obj = old_dir; + old_dir = obj->parent; + } + + if (obj && obj->rename_allowed) { + /* Now handle an existing target, if there is one */ + existing_target = yaffs_find_by_name(new_dir, new_name); + if (yaffs_is_non_empty_dir(existing_target)) { + return YAFFS_FAIL; /* ENOTEMPTY */ + } else if (existing_target && existing_target != obj) { + /* Nuke the target first, using shadowing, + * but only if it isn't the same object. + * + * Note we must disable gc here otherwise it can mess + * up the shadowing. + * + */ + dev->gc_disable = 1; + yaffs_change_obj_name(obj, new_dir, new_name, force, + existing_target->obj_id); + existing_target->is_shadowed = 1; + yaffs_unlink_obj(existing_target); + dev->gc_disable = 0; + } + + result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0); + + yaffs_update_parent(old_dir); + if (new_dir != old_dir) + yaffs_update_parent(new_dir); + + return result; + } + return YAFFS_FAIL; +} + +/*----------------------- Initialisation Scanning ---------------------- */ + +void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id, + int backward_scanning) +{ + struct yaffs_obj *obj; + + if (!backward_scanning) { + /* Handle YAFFS1 forward scanning case + * For YAFFS1 we always do the deletion + */ + + } else { + /* Handle YAFFS2 case (backward scanning) + * If the shadowed object exists then ignore. + */ + obj = yaffs_find_by_number(dev, obj_id); + if (obj) + return; + } + + /* Let's create it (if it does not exist) assuming it is a file so that + * it can do shrinking etc. + * We put it in unlinked dir to be cleaned up after the scanning + */ + obj = + yaffs_find_or_create_by_number(dev, obj_id, YAFFS_OBJECT_TYPE_FILE); + if (!obj) + return; + obj->is_shadowed = 1; + yaffs_add_obj_to_dir(dev->unlinked_dir, obj); + obj->variant.file_variant.shrink_size = 0; + obj->valid = 1; /* So that we don't read any other info. */ +} + +void yaffs_link_fixup(struct yaffs_dev *dev, struct yaffs_obj *hard_list) +{ + struct yaffs_obj *hl; + struct yaffs_obj *in; + + while (hard_list) { + hl = hard_list; + hard_list = (struct yaffs_obj *)(hard_list->hard_links.next); + + in = yaffs_find_by_number(dev, + hl->variant. + hardlink_variant.equiv_id); + + if (in) { + /* Add the hardlink pointers */ + hl->variant.hardlink_variant.equiv_obj = in; + list_add(&hl->hard_links, &in->hard_links); + } else { + /* Todo Need to report/handle this better. + * Got a problem... hardlink to a non-existant object + */ + hl->variant.hardlink_variant.equiv_obj = NULL; + INIT_LIST_HEAD(&hl->hard_links); + } + } +} + +static void yaffs_strip_deleted_objs(struct yaffs_dev *dev) +{ + /* + * Sort out state of unlinked and deleted objects after scanning. + */ + struct list_head *i; + struct list_head *n; + struct yaffs_obj *l; + + if (dev->read_only) + return; + + /* Soft delete all the unlinked files */ + list_for_each_safe(i, n, + &dev->unlinked_dir->variant.dir_variant.children) { + l = list_entry(i, struct yaffs_obj, siblings); + yaffs_del_obj(l); + } + + list_for_each_safe(i, n, &dev->del_dir->variant.dir_variant.children) { + l = list_entry(i, struct yaffs_obj, siblings); + yaffs_del_obj(l); + } +} + +/* + * This code iterates through all the objects making sure that they are rooted. + * Any unrooted objects are re-rooted in lost+found. + * An object needs to be in one of: + * - Directly under deleted, unlinked + * - Directly or indirectly under root. + * + * Note: + * This code assumes that we don't ever change the current relationships + * between directories: + * root_dir->parent == unlinked_dir->parent == del_dir->parent == NULL + * lost-n-found->parent == root_dir + * + * This fixes the problem where directories might have inadvertently been + * deleted leaving the object "hanging" without being rooted in the + * directory tree. + */ + +static int yaffs_has_null_parent(struct yaffs_dev *dev, struct yaffs_obj *obj) +{ + return (obj == dev->del_dir || + obj == dev->unlinked_dir || obj == dev->root_dir); +} + +static void yaffs_fix_hanging_objs(struct yaffs_dev *dev) +{ + struct yaffs_obj *obj; + struct yaffs_obj *parent; + int i; + struct list_head *lh; + struct list_head *n; + int depth_limit; + int hanging; + + if (dev->read_only) + return; + + /* Iterate through the objects in each hash entry, + * looking at each object. + * Make sure it is rooted. + */ + + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) { + list_for_each_safe(lh, n, &dev->obj_bucket[i].list) { + obj = list_entry(lh, struct yaffs_obj, hash_link); + parent = obj->parent; + + if (yaffs_has_null_parent(dev, obj)) { + /* These directories are not hanging */ + hanging = 0; + } else if (!parent || + parent->variant_type != + YAFFS_OBJECT_TYPE_DIRECTORY) { + hanging = 1; + } else if (yaffs_has_null_parent(dev, parent)) { + hanging = 0; + } else { + /* + * Need to follow the parent chain to + * see if it is hanging. + */ + hanging = 0; + depth_limit = 100; + + while (parent != dev->root_dir && + parent->parent && + parent->parent->variant_type == + YAFFS_OBJECT_TYPE_DIRECTORY && + depth_limit > 0) { + parent = parent->parent; + depth_limit--; + } + if (parent != dev->root_dir) + hanging = 1; + } + if (hanging) { + yaffs_trace(YAFFS_TRACE_SCAN, + "Hanging object %d moved to lost and found", + obj->obj_id); + yaffs_add_obj_to_dir(dev->lost_n_found, obj); + } + } + } +} + +/* + * Delete directory contents for cleaning up lost and found. + */ +static void yaffs_del_dir_contents(struct yaffs_obj *dir) +{ + struct yaffs_obj *obj; + struct list_head *lh; + struct list_head *n; + + if (dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) + YBUG(); + + list_for_each_safe(lh, n, &dir->variant.dir_variant.children) { + obj = list_entry(lh, struct yaffs_obj, siblings); + if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) + yaffs_del_dir_contents(obj); + yaffs_trace(YAFFS_TRACE_SCAN, + "Deleting lost_found object %d", + obj->obj_id); + yaffs_unlink_obj(obj); + } +} + +static void yaffs_empty_l_n_f(struct yaffs_dev *dev) +{ + yaffs_del_dir_contents(dev->lost_n_found); +} + + +struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *directory, + const YCHAR *name) +{ + int sum; + struct list_head *i; + YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1]; + struct yaffs_obj *l; + + if (!name) + return NULL; + + if (!directory) { + yaffs_trace(YAFFS_TRACE_ALWAYS, + "tragedy: yaffs_find_by_name: null pointer directory" + ); + YBUG(); + return NULL; + } + if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) { + yaffs_trace(YAFFS_TRACE_ALWAYS, + "tragedy: yaffs_find_by_name: non-directory" + ); + YBUG(); + } + + sum = yaffs_calc_name_sum(name); + + list_for_each(i, &directory->variant.dir_variant.children) { + l = list_entry(i, struct yaffs_obj, siblings); + + if (l->parent != directory) + YBUG(); + + yaffs_check_obj_details_loaded(l); + + /* Special case for lost-n-found */ + if (l->obj_id == YAFFS_OBJECTID_LOSTNFOUND) { + if (!strcmp(name, YAFFS_LOSTNFOUND_NAME)) + return l; + } else if (l->sum == sum + || l->hdr_chunk <= 0) { + /* LostnFound chunk called Objxxx + * Do a real check + */ + yaffs_get_obj_name(l, buffer, + YAFFS_MAX_NAME_LENGTH + 1); + if (strncmp(name, buffer, YAFFS_MAX_NAME_LENGTH) == 0) + return l; + } + } + return NULL; +} + +/* GetEquivalentObject dereferences any hard links to get to the + * actual object. + */ + +struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj) +{ + if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) { + obj = obj->variant.hardlink_variant.equiv_obj; + yaffs_check_obj_details_loaded(obj); + } + return obj; +} + +/* + * A note or two on object names. + * * If the object name is missing, we then make one up in the form objnnn + * + * * ASCII names are stored in the object header's name field from byte zero + * * Unicode names are historically stored starting from byte zero. + * + * Then there are automatic Unicode names... + * The purpose of these is to save names in a way that can be read as + * ASCII or Unicode names as appropriate, thus allowing a Unicode and ASCII + * system to share files. + * + * These automatic unicode are stored slightly differently... + * - If the name can fit in the ASCII character space then they are saved as + * ascii names as per above. + * - If the name needs Unicode then the name is saved in Unicode + * starting at oh->name[1]. + + */ +static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR *name, + int buffer_size) +{ + /* Create an object name if we could not find one. */ + if (strnlen(name, YAFFS_MAX_NAME_LENGTH) == 0) { + YCHAR local_name[20]; + YCHAR num_string[20]; + YCHAR *x = &num_string[19]; + unsigned v = obj->obj_id; + num_string[19] = 0; + while (v > 0) { + x--; + *x = '0' + (v % 10); + v /= 10; + } + /* make up a name */ + strcpy(local_name, YAFFS_LOSTNFOUND_PREFIX); + strcat(local_name, x); + strncpy(name, local_name, buffer_size - 1); + } +} + +int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR *name, int buffer_size) +{ + memset(name, 0, buffer_size * sizeof(YCHAR)); + yaffs_check_obj_details_loaded(obj); + if (obj->obj_id == YAFFS_OBJECTID_LOSTNFOUND) + strncpy(name, YAFFS_LOSTNFOUND_NAME, buffer_size - 1); +#ifndef CONFIG_YAFFS_NO_SHORT_NAMES + else if (obj->short_name[0]) + strcpy(name, obj->short_name); +#endif + else if (obj->hdr_chunk > 0) { + int result; + u8 *buffer = yaffs_get_temp_buffer(obj->my_dev, __LINE__); + + struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)buffer; + + memset(buffer, 0, obj->my_dev->data_bytes_per_chunk); + + if (obj->hdr_chunk > 0) { + result = yaffs_rd_chunk_tags_nand(obj->my_dev, + obj->hdr_chunk, + buffer, NULL); + } + yaffs_load_name_from_oh(obj->my_dev, name, oh->name, + buffer_size); + + yaffs_release_temp_buffer(obj->my_dev, buffer, __LINE__); + } + + yaffs_fix_null_name(obj, name, buffer_size); + + return strnlen(name, YAFFS_MAX_NAME_LENGTH); +} + +int yaffs_get_obj_length(struct yaffs_obj *obj) +{ + /* Dereference any hard linking */ + obj = yaffs_get_equivalent_obj(obj); + + if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE) + return obj->variant.file_variant.file_size; + if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) { + if (!obj->variant.symlink_variant.alias) + return 0; + return strnlen(obj->variant.symlink_variant.alias, + YAFFS_MAX_ALIAS_LENGTH); + } else { + /* Only a directory should drop through to here */ + return obj->my_dev->data_bytes_per_chunk; + } +} + +int yaffs_get_obj_link_count(struct yaffs_obj *obj) +{ + int count = 0; + struct list_head *i; + + if (!obj->unlinked) + count++; /* the object itself */ + + list_for_each(i, &obj->hard_links) + count++; /* add the hard links; */ + + return count; +} + +int yaffs_get_obj_inode(struct yaffs_obj *obj) +{ + obj = yaffs_get_equivalent_obj(obj); + + return obj->obj_id; +} + +unsigned yaffs_get_obj_type(struct yaffs_obj *obj) +{ + obj = yaffs_get_equivalent_obj(obj); + + switch (obj->variant_type) { + case YAFFS_OBJECT_TYPE_FILE: + return DT_REG; + break; + case YAFFS_OBJECT_TYPE_DIRECTORY: + return DT_DIR; + break; + case YAFFS_OBJECT_TYPE_SYMLINK: + return DT_LNK; + break; + case YAFFS_OBJECT_TYPE_HARDLINK: + return DT_REG; + break; + case YAFFS_OBJECT_TYPE_SPECIAL: + if (S_ISFIFO(obj->yst_mode)) + return DT_FIFO; + if (S_ISCHR(obj->yst_mode)) + return DT_CHR; + if (S_ISBLK(obj->yst_mode)) + return DT_BLK; + if (S_ISSOCK(obj->yst_mode)) + return DT_SOCK; + return DT_REG; + break; + default: + return DT_REG; + break; + } +} + +YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj) +{ + obj = yaffs_get_equivalent_obj(obj); + if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) + return yaffs_clone_str(obj->variant.symlink_variant.alias); + else + return yaffs_clone_str(_Y("")); +} + +/*--------------------------- Initialisation code -------------------------- */ static int yaffs_check_dev_fns(const struct yaffs_dev *dev) { - /* Common functions, gotta have */ if (!dev->param.erase_fn || !dev->param.initialise_flash_fn) return 0; @@ -4792,17 +4605,13 @@ static int yaffs_check_dev_fns(const struct yaffs_dev *dev) static int yaffs_create_initial_dir(struct yaffs_dev *dev) { - /* Initialise the unlinked, deleted, root and lost and found directories */ - + /* Initialise the unlinked, deleted, root and lost+found directories */ dev->lost_n_found = dev->root_dir = NULL; dev->unlinked_dir = dev->del_dir = NULL; - dev->unlinked_dir = yaffs_create_fake_dir(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR); - dev->del_dir = yaffs_create_fake_dir(dev, YAFFS_OBJECTID_DELETED, S_IFDIR); - dev->root_dir = yaffs_create_fake_dir(dev, YAFFS_OBJECTID_ROOT, YAFFS_ROOT_MODE | S_IFDIR); @@ -4815,7 +4624,6 @@ static int yaffs_create_initial_dir(struct yaffs_dev *dev) yaffs_add_obj_to_dir(dev->root_dir, dev->lost_n_found); return YAFFS_OK; } - return YAFFS_FAIL; } @@ -4825,13 +4633,14 @@ int yaffs_guts_initialise(struct yaffs_dev *dev) unsigned x; int bits; - T(YAFFS_TRACE_TRACING, - (TSTR("yaffs: yaffs_guts_initialise()" TENDSTR))); + yaffs_trace(YAFFS_TRACE_TRACING, "yaffs: yaffs_guts_initialise()"); /* Check stuff that must be set */ if (!dev) { - T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Need a device" TENDSTR))); + yaffs_trace(YAFFS_TRACE_ALWAYS, + "yaffs: Need a device" + ); return YAFFS_FAIL; } @@ -4852,18 +4661,29 @@ int yaffs_guts_initialise(struct yaffs_dev *dev) /* Check geometry parameters. */ - if ((!dev->param.inband_tags && dev->param.is_yaffs2 && dev->param.total_bytes_per_chunk < 1024) || (!dev->param.is_yaffs2 && dev->param.total_bytes_per_chunk < 512) || (dev->param.inband_tags && !dev->param.is_yaffs2) || dev->param.chunks_per_block < 2 || dev->param.n_reserved_blocks < 2 || dev->internal_start_block <= 0 || dev->internal_end_block <= 0 || dev->internal_end_block <= (dev->internal_start_block + dev->param.n_reserved_blocks + 2)) { /* otherwise it is too small */ - T(YAFFS_TRACE_ALWAYS, - (TSTR - ("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s, inband_tags %d " - TENDSTR), dev->param.total_bytes_per_chunk, - dev->param.is_yaffs2 ? "2" : "", dev->param.inband_tags)); + if ((!dev->param.inband_tags && dev->param.is_yaffs2 && + dev->param.total_bytes_per_chunk < 1024) || + (!dev->param.is_yaffs2 && + dev->param.total_bytes_per_chunk < 512) || + (dev->param.inband_tags && !dev->param.is_yaffs2) || + dev->param.chunks_per_block < 2 || + dev->param.n_reserved_blocks < 2 || + dev->internal_start_block <= 0 || + dev->internal_end_block <= 0 || + dev->internal_end_block <= + (dev->internal_start_block + dev->param.n_reserved_blocks + 2) + ) { + /* otherwise it is too small */ + yaffs_trace(YAFFS_TRACE_ALWAYS, + "NAND geometry problems: chunk size %d, type is yaffs%s, inband_tags %d ", + dev->param.total_bytes_per_chunk, + dev->param.is_yaffs2 ? "2" : "", + dev->param.inband_tags); return YAFFS_FAIL; } if (yaffs_init_nand(dev) != YAFFS_OK) { - T(YAFFS_TRACE_ALWAYS, - (TSTR("yaffs: InitialiseNAND failed" TENDSTR))); + yaffs_trace(YAFFS_TRACE_ALWAYS, "InitialiseNAND failed"); return YAFFS_FAIL; } @@ -4878,20 +4698,18 @@ int yaffs_guts_initialise(struct yaffs_dev *dev) /* Got the right mix of functions? */ if (!yaffs_check_dev_fns(dev)) { /* Function missing */ - T(YAFFS_TRACE_ALWAYS, - (TSTR - ("yaffs: device function(s) missing or wrong\n" TENDSTR))); + yaffs_trace(YAFFS_TRACE_ALWAYS, + "device function(s) missing or wrong"); return YAFFS_FAIL; } if (dev->is_mounted) { - T(YAFFS_TRACE_ALWAYS, - (TSTR("yaffs: device already mounted\n" TENDSTR))); + yaffs_trace(YAFFS_TRACE_ALWAYS, "device already mounted"); return YAFFS_FAIL; } - /* Finished with most checks. One or two more checks happen later on too. */ + /* Finished with most checks. Further checks happen later on too. */ dev->is_mounted = 1; @@ -4926,8 +4744,9 @@ int yaffs_guts_initialise(struct yaffs_dev *dev) dev->tnode_width = 16; else dev->tnode_width = bits; - } else + } else { dev->tnode_width = 16; + } dev->tnode_mask = (1 << dev->tnode_width) - 1; @@ -4953,13 +4772,12 @@ int yaffs_guts_initialise(struct yaffs_dev *dev) * the chunk group size > chunks per block. * This can be remedied by using larger "virtual blocks". */ - T(YAFFS_TRACE_ALWAYS, - (TSTR("yaffs: chunk group too large\n" TENDSTR))); + yaffs_trace(YAFFS_TRACE_ALWAYS, "chunk group too large"); return YAFFS_FAIL; } - /* OK, we've finished verifying the device, lets continue with initialisation */ + /* Finished verifying the device, continue with initialisation */ /* More device initialisation */ dev->all_gcs = 0; @@ -4979,7 +4797,8 @@ int yaffs_guts_initialise(struct yaffs_dev *dev) dev->n_erase_failures = 0; dev->n_erased_blocks = 0; dev->gc_disable = 0; - dev->has_pending_prioritised_gc = 1; /* Assume the worst for now, will get fixed on first GC */ + dev->has_pending_prioritised_gc = 1; + /* Assume the worst for now, will get fixed on first GC */ INIT_LIST_HEAD(&dev->dirty_dirs); dev->oldest_dirty_seq = 0; dev->oldest_dirty_block = 0; @@ -5000,7 +4819,7 @@ int yaffs_guts_initialise(struct yaffs_dev *dev) if (dev->param.n_caches > YAFFS_MAX_SHORT_OP_CACHES) dev->param.n_caches = YAFFS_MAX_SHORT_OP_CACHES; - dev->cache = YMALLOC(cache_bytes); + dev->cache = kmalloc(cache_bytes, GFP_NOFS); buf = (u8 *) dev->cache; @@ -5012,7 +4831,7 @@ int yaffs_guts_initialise(struct yaffs_dev *dev) dev->cache[i].last_use = 0; dev->cache[i].dirty = 0; dev->cache[i].data = buf = - YMALLOC_DMA(dev->param.total_bytes_per_chunk); + kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS); } if (!buf) init_failed = 1; @@ -5024,7 +4843,8 @@ int yaffs_guts_initialise(struct yaffs_dev *dev) if (!init_failed) { dev->gc_cleanup_list = - YMALLOC(dev->param.chunks_per_block * sizeof(u32)); + kmalloc(dev->param.chunks_per_block * sizeof(u32), + GFP_NOFS); if (!dev->gc_cleanup_list) init_failed = 1; } @@ -5045,14 +4865,14 @@ int yaffs_guts_initialise(struct yaffs_dev *dev) if (dev->param.is_yaffs2) { if (yaffs2_checkpt_restore(dev)) { yaffs_check_obj_details_loaded(dev->root_dir); - T(YAFFS_TRACE_ALWAYS, - (TSTR - ("yaffs: restored from checkpoint" - TENDSTR))); + yaffs_trace(YAFFS_TRACE_CHECKPOINT | + YAFFS_TRACE_MOUNT, + "yaffs: restored from checkpoint" + ); } else { - /* Clean up the mess caused by an aborted checkpoint load - * and scan backwards. + /* Clean up the mess caused by an aborted + * checkpoint load then scan backwards. */ yaffs_deinit_blocks(dev); @@ -5078,8 +4898,9 @@ int yaffs_guts_initialise(struct yaffs_dev *dev) if (!init_failed && !yaffs2_scan_backwards(dev)) init_failed = 1; } - } else if (!yaffs1_scan(dev)) + } else if (!yaffs1_scan(dev)) { init_failed = 1; + } yaffs_strip_deleted_objs(dev); yaffs_fix_hanging_objs(dev); @@ -5089,8 +4910,8 @@ int yaffs_guts_initialise(struct yaffs_dev *dev) if (init_failed) { /* Clean up the mess */ - T(YAFFS_TRACE_TRACING, - (TSTR("yaffs: yaffs_guts_initialise() aborted.\n" TENDSTR))); + yaffs_trace(YAFFS_TRACE_TRACING, + "yaffs: yaffs_guts_initialise() aborted."); yaffs_deinitialise(dev); return YAFFS_FAIL; @@ -5112,10 +4933,9 @@ int yaffs_guts_initialise(struct yaffs_dev *dev) if (!dev->is_checkpointed && dev->blocks_in_checkpt > 0) yaffs2_checkpt_invalidate(dev); - T(YAFFS_TRACE_TRACING, - (TSTR("yaffs: yaffs_guts_initialise() done.\n" TENDSTR))); + yaffs_trace(YAFFS_TRACE_TRACING, + "yaffs: yaffs_guts_initialise() done."); return YAFFS_OK; - } void yaffs_deinitialise(struct yaffs_dev *dev) @@ -5128,19 +4948,18 @@ void yaffs_deinitialise(struct yaffs_dev *dev) if (dev->param.n_caches > 0 && dev->cache) { for (i = 0; i < dev->param.n_caches; i++) { - if (dev->cache[i].data) - YFREE(dev->cache[i].data); + kfree(dev->cache[i].data); dev->cache[i].data = NULL; } - YFREE(dev->cache); + kfree(dev->cache); dev->cache = NULL; } - YFREE(dev->gc_cleanup_list); + kfree(dev->gc_cleanup_list); for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) - YFREE(dev->temp_buffer[i].buffer); + kfree(dev->temp_buffer[i].buffer); dev->is_mounted = 0; @@ -5153,7 +4972,6 @@ int yaffs_count_free_chunks(struct yaffs_dev *dev) { int n_free = 0; int b; - struct yaffs_block_info *blk; blk = dev->block_info; @@ -5172,14 +4990,12 @@ int yaffs_count_free_chunks(struct yaffs_dev *dev) } blk++; } - return n_free; } int yaffs_get_n_free_chunks(struct yaffs_dev *dev) { /* This is what we report to the outside world */ - int n_free; int n_dirty_caches; int blocks_for_checkpt; @@ -5188,7 +5004,7 @@ int yaffs_get_n_free_chunks(struct yaffs_dev *dev) n_free = dev->n_free_chunks; n_free += dev->n_deleted_files; - /* Now count the number of dirty chunks in the cache and subtract those */ + /* Now count and subtract the number of dirty chunks in the cache. */ for (n_dirty_caches = 0, i = 0; i < dev->param.n_caches; i++) { if (dev->cache[i].dirty) @@ -5200,7 +5016,7 @@ int yaffs_get_n_free_chunks(struct yaffs_dev *dev) n_free -= ((dev->param.n_reserved_blocks + 1) * dev->param.chunks_per_block); - /* Now we figure out how much to reserve for the checkpoint and report that... */ + /* Now figure checkpoint space and report that... */ blocks_for_checkpt = yaffs_calc_checkpt_blocks_required(dev); n_free -= (blocks_for_checkpt * dev->param.chunks_per_block); @@ -5209,5 +5025,4 @@ int yaffs_get_n_free_chunks(struct yaffs_dev *dev) n_free = 0; return n_free; - } diff --git a/fs/yaffs2/yaffs_guts.h b/fs/yaffs2/yaffs_guts.h index 60770ca7..e4680e81 100644 --- a/fs/yaffs2/yaffs_guts.h +++ b/fs/yaffs2/yaffs_guts.h @@ -29,12 +29,12 @@ */ #define YAFFS_MAGIC 0x5941FF53 -#define YAFFS_NTNODES_LEVEL0 16 +#define YAFFS_NTNODES_LEVEL0 16 #define YAFFS_TNODES_LEVEL0_BITS 4 #define YAFFS_TNODES_LEVEL0_MASK 0xf -#define YAFFS_NTNODES_INTERNAL (YAFFS_NTNODES_LEVEL0 / 2) -#define YAFFS_TNODES_INTERNAL_BITS (YAFFS_TNODES_LEVEL0_BITS - 1) +#define YAFFS_NTNODES_INTERNAL (YAFFS_NTNODES_LEVEL0 / 2) +#define YAFFS_TNODES_INTERNAL_BITS (YAFFS_TNODES_LEVEL0_BITS - 1) #define YAFFS_TNODES_INTERNAL_MASK 0x7 #define YAFFS_TNODES_MAX_LEVEL 6 @@ -43,10 +43,10 @@ #define YAFFS_BYTES_PER_CHUNK 512 #define YAFFS_CHUNK_SIZE_SHIFT 9 #define YAFFS_CHUNKS_PER_BLOCK 32 -#define YAFFS_BYTES_PER_BLOCK (YAFFS_CHUNKS_PER_BLOCK*YAFFS_BYTES_PER_CHUNK) +#define YAFFS_BYTES_PER_BLOCK (YAFFS_CHUNKS_PER_BLOCK*YAFFS_BYTES_PER_CHUNK) #endif -#define YAFFS_MIN_YAFFS2_CHUNK_SIZE 1024 +#define YAFFS_MIN_YAFFS2_CHUNK_SIZE 1024 #define YAFFS_MIN_YAFFS2_SPARE_SIZE 32 #define YAFFS_MAX_CHUNK_ID 0x000FFFFF @@ -58,9 +58,9 @@ #define YAFFS_NOBJECT_BUCKETS 256 #define YAFFS_OBJECT_SPACE 0x40000 -#define YAFFS_MAX_OBJECT_ID (YAFFS_OBJECT_SPACE -1) +#define YAFFS_MAX_OBJECT_ID (YAFFS_OBJECT_SPACE - 1) -#define YAFFS_CHECKPOINT_VERSION 4 +#define YAFFS_CHECKPOINT_VERSION 4 #ifdef CONFIG_YAFFS_UNICODE #define YAFFS_MAX_NAME_LENGTH 127 @@ -97,7 +97,7 @@ * The range is limited slightly to help distinguish bad numbers from good. * This also allows us to perhaps in the future use special numbers for * special purposes. - * EFFFFF00 allows the allocation of 8 blocks per second (~1Mbytes) for 15 years, + * EFFFFF00 allows the allocation of 8 blocks/second (~1Mbytes) for 15 years, * and is a larger number than the lifetime of a 2GB device. */ #define YAFFS_LOWEST_SEQUENCE_NUMBER 0x00001000 @@ -118,8 +118,8 @@ struct yaffs_cache { }; /* Tags structures in RAM - * NB This uses bitfield. Bitfields should not straddle a u32 boundary otherwise - * the structure size will get blown out. + * NB This uses bitfield. Bitfields should not straddle a u32 boundary + * otherwise the structure size will get blown out. */ #ifndef CONFIG_YAFFS_NO_YAFFS1 @@ -163,8 +163,8 @@ struct yaffs_ext_tags { unsigned validity0; unsigned chunk_used; /* Status of the chunk: used or unused */ - unsigned obj_id; /* If 0 then this is not part of an object (unused) */ - unsigned chunk_id; /* If 0 then this is a header, else a data chunk */ + unsigned obj_id; /* If 0 this is not used */ + unsigned chunk_id; /* If 0 this is a header, else a data chunk */ unsigned n_bytes; /* Only valid for data chunks */ /* The following stuff only has meaning when we read */ @@ -180,7 +180,7 @@ struct yaffs_ext_tags { /* Extra info if this is an object header (YAFFS2 only) */ - unsigned extra_available; /* There is extra info available if this is not zero */ + unsigned extra_available; /* Extra info available if not zero */ unsigned extra_parent_id; /* The parent object */ unsigned extra_is_shrink; /* Is it a shrink header? */ unsigned extra_shadows; /* Does this shadow another object? */ @@ -188,7 +188,7 @@ struct yaffs_ext_tags { enum yaffs_obj_type extra_obj_type; /* What object type? */ unsigned extra_length; /* Length if it is a file */ - unsigned extra_equiv_id; /* Equivalent object Id if it is a hard link */ + unsigned extra_equiv_id; /* Equivalent object for a hard link */ unsigned validity1; @@ -226,11 +226,14 @@ enum yaffs_block_state { /* Being scanned */ YAFFS_BLOCK_STATE_NEEDS_SCANNING, - /* The block might have something on it (ie it is allocating or full, perhaps empty) - * but it needs to be scanned to determine its true state. + /* The block might have something on it (ie it is allocating or full, + * perhaps empty) but it needs to be scanned to determine its true + * state. * This state is only valid during scanning. - * NB We tolerate empty because the pre-scanner might be incapable of deciding - * However, if this state is returned on a YAFFS2 device, then we expect a sequence number + * NB We tolerate empty because the pre-scanner might be incapable of + * deciding + * However, if this state is returned on a YAFFS2 device, + * then we expect a sequence number */ YAFFS_BLOCK_STATE_EMPTY, @@ -241,7 +244,8 @@ enum yaffs_block_state { * At least one page holds valid data. * This is the one currently being used for page * allocation. Should never be more than one of these. - * If a block is only partially allocated at mount it is treated as full. + * If a block is only partially allocated at mount it is treated as + * full. */ YAFFS_BLOCK_STATE_FULL, @@ -271,16 +275,19 @@ struct yaffs_block_info { int soft_del_pages:10; /* number of soft deleted pages */ int pages_in_use:10; /* number of pages in use */ - unsigned block_state:4; /* One of the above block states. NB use unsigned because enum is sometimes an int */ - u32 needs_retiring:1; /* Data has failed on this block, need to get valid data off */ - /* and retire the block. */ - u32 skip_erased_check:1; /* If this is set we can skip the erased check on this block */ - u32 gc_prioritise:1; /* An ECC check or blank check has failed on this block. - It should be prioritised for GC */ - u32 chunk_error_strikes:3; /* How many times we've had ecc etc failures on this block and tried to reuse it */ + unsigned block_state:4; /* One of the above block states. */ + /* NB use unsigned because enum is sometimes + * an int */ + u32 needs_retiring:1; /* Data has failed on this block, */ + /*need to get valid data off and retire*/ + u32 skip_erased_check:1;/* Skip the erased check on this block */ + u32 gc_prioritise:1; /* An ECC check or blank check has failed. + Block should be prioritised for GC */ + u32 chunk_error_strikes:3; /* How many times we've had ecc etc + failures on this block and tried to reuse it */ #ifdef CONFIG_YAFFS_YAFFS2 - u32 has_shrink_hdr:1; /* This block has at least one shrink object header */ + u32 has_shrink_hdr:1; /* This block has at least one shrink header */ u32 seq_number; /* block sequence number for yaffs2 */ #endif @@ -297,7 +304,7 @@ struct yaffs_obj_hdr { u16 sum_no_longer_used; /* checksum of name. No longer used */ YCHAR name[YAFFS_MAX_NAME_LENGTH + 1]; - /* The following apply to directories, files, symlinks - not hard links */ + /* The following apply to all object types except for hard links */ u32 yst_mode; /* protection */ u32 yst_uid; @@ -315,7 +322,7 @@ struct yaffs_obj_hdr { /* Alias is for symlinks only. */ YCHAR alias[YAFFS_MAX_ALIAS_LENGTH + 1]; - u32 yst_rdev; /* device stuff for block and char devices (major/min) */ + u32 yst_rdev; /* stuff for block and char devices (major/min) */ u32 win_ctime[2]; u32 win_atime[2]; @@ -325,9 +332,10 @@ struct yaffs_obj_hdr { u32 inband_is_shrink; u32 reserved[2]; - int shadows_obj; /* This object header shadows the specified object if > 0 */ + int shadows_obj; /* This object header shadows the + specified object if > 0 */ - /* is_shrink applies to object headers written when we shrink the file (ie resize) */ + /* is_shrink applies to object headers written when wemake a hole. */ u32 is_shrink; }; @@ -378,35 +386,43 @@ union yaffs_obj_var { struct yaffs_obj { u8 deleted:1; /* This should only apply to unlinked files. */ u8 soft_del:1; /* it has also been soft deleted */ - u8 unlinked:1; /* An unlinked file. The file should be in the unlinked directory. */ + u8 unlinked:1; /* An unlinked file.*/ u8 fake:1; /* A fake object has no presence on NAND. */ - u8 rename_allowed:1; /* Some objects are not allowed to be renamed. */ + u8 rename_allowed:1; /* Some objects cannot be renamed. */ u8 unlink_allowed:1; u8 dirty:1; /* the object needs to be written to flash */ u8 valid:1; /* When the file system is being loaded up, this * object might be created before the data - * is available (ie. file data records appear before the header). + * is available + * ie. file data chunks encountered before + * the header. */ - u8 lazy_loaded:1; /* This object has been lazy loaded and is missing some detail */ + u8 lazy_loaded:1; /* This object has been lazy loaded and + * is missing some detail */ - u8 defered_free:1; /* For Linux kernel. Object is removed from NAND, but is - * still in the inode cache. Free of object is defered. + u8 defered_free:1; /* Object is removed from NAND, but is + * still in the inode cache. + * Free of object is defered. * until the inode is released. */ - u8 being_created:1; /* This object is still being created so skip some checks. */ - u8 is_shadowed:1; /* This object is shadowed on the way to being renamed. */ + u8 being_created:1; /* This object is still being created + * so skip some verification checks. */ + u8 is_shadowed:1; /* This object is shadowed on the way + * to being renamed. */ - u8 xattr_known:1; /* We know if this has object has xattribs or not. */ - u8 has_xattr:1; /* This object has xattribs. Valid if xattr_known. */ + u8 xattr_known:1; /* We know if this has object has xattribs + * or not. */ + u8 has_xattr:1; /* This object has xattribs. + * Only valid if xattr_known. */ - u8 serial; /* serial number of chunk in NAND. Cached here */ + u8 serial; /* serial number of chunk in NAND.*/ u16 sum; /* sum of the name to speed searching */ struct yaffs_dev *my_dev; /* The device I'm on */ - struct list_head hash_link; /* list of objects in this hash bucket */ + struct list_head hash_link; /* list of objects in hash bucket */ - struct list_head hard_links; /* all the equivalent hard linked objects */ + struct list_head hard_links; /* hard linked object chain*/ /* directory structure stuff */ /* also used for linking up the free list */ @@ -416,13 +432,13 @@ struct yaffs_obj { /* Where's my object header in NAND? */ int hdr_chunk; - int n_data_chunks; /* Number of data chunks attached to the file. */ + int n_data_chunks; /* Number of data chunks for this file. */ u32 obj_id; /* the object id value */ u32 yst_mode; -#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM +#ifndef CONFIG_YAFFS_NO_SHORT_NAMES YCHAR short_name[YAFFS_SHORT_NAME_LENGTH + 1]; #endif @@ -481,7 +497,7 @@ struct yaffs_checkpt_obj { struct yaffs_buffer { u8 *buffer; - int line; /* track from whence this buffer was allocated */ + int line; /* track from whence this buffer was allocated */ int max_line; }; @@ -497,26 +513,28 @@ struct yaffs_param { */ int inband_tags; /* Use unband tags */ - u32 total_bytes_per_chunk; /* Should be >= 512, does not need to be a power of 2 */ + u32 total_bytes_per_chunk; /* Should be >= 512, does not need to + be a power of 2 */ int chunks_per_block; /* does not need to be a power of 2 */ int spare_bytes_per_chunk; /* spare area size */ int start_block; /* Start block we're allowed to use */ int end_block; /* End block we're allowed to use */ - int n_reserved_blocks; /* We want this tuneable so that we can reduce */ - /* reserved blocks on NOR and RAM. */ + int n_reserved_blocks; /* Tuneable so that we can reduce + * reserved blocks on NOR and RAM. */ - int n_caches; /* If <= 0, then short op caching is disabled, else - * the number of short op caches (don't use too many). - * 10 to 20 is a good bet. + int n_caches; /* If <= 0, then short op caching is disabled, + * else the number of short op caches. */ - int use_nand_ecc; /* Flag to decide whether or not to use NANDECC on data (yaffs1) */ - int no_tags_ecc; /* Flag to decide whether or not to do ECC on packed tags (yaffs2) */ + int use_nand_ecc; /* Flag to decide whether or not to use + * NAND driver ECC on data (yaffs1) */ + int no_tags_ecc; /* Flag to decide whether or not to do ECC + * on packed tags (yaffs2) */ int is_yaffs2; /* Use yaffs2 mode on this device */ int empty_lost_n_found; /* Auto-empty lost+found directory on mount */ - int refresh_period; /* How often we should check to do a block refresh */ + int refresh_period; /* How often to check for a block refresh */ /* Checkpoint control. Can be set before or after initialisation */ u8 skip_checkpt_rd; @@ -526,27 +544,27 @@ struct yaffs_param { /* NAND access functions (Must be set before calling YAFFS) */ - int (*write_chunk_fn) (struct yaffs_dev * dev, - int nand_chunk, const u8 * data, - const struct yaffs_spare * spare); - int (*read_chunk_fn) (struct yaffs_dev * dev, - int nand_chunk, u8 * data, - struct yaffs_spare * spare); - int (*erase_fn) (struct yaffs_dev * dev, int flash_block); - int (*initialise_flash_fn) (struct yaffs_dev * dev); - int (*deinitialise_flash_fn) (struct yaffs_dev * dev); + int (*write_chunk_fn) (struct yaffs_dev *dev, + int nand_chunk, const u8 *data, + const struct yaffs_spare *spare); + int (*read_chunk_fn) (struct yaffs_dev *dev, + int nand_chunk, u8 *data, + struct yaffs_spare *spare); + int (*erase_fn) (struct yaffs_dev *dev, int flash_block); + int (*initialise_flash_fn) (struct yaffs_dev *dev); + int (*deinitialise_flash_fn) (struct yaffs_dev *dev); #ifdef CONFIG_YAFFS_YAFFS2 - int (*write_chunk_tags_fn) (struct yaffs_dev * dev, - int nand_chunk, const u8 * data, - const struct yaffs_ext_tags * tags); - int (*read_chunk_tags_fn) (struct yaffs_dev * dev, - int nand_chunk, u8 * data, - struct yaffs_ext_tags * tags); - int (*bad_block_fn) (struct yaffs_dev * dev, int block_no); - int (*query_block_fn) (struct yaffs_dev * dev, int block_no, - enum yaffs_block_state * state, - u32 * seq_number); + int (*write_chunk_tags_fn) (struct yaffs_dev *dev, + int nand_chunk, const u8 *data, + const struct yaffs_ext_tags *tags); + int (*read_chunk_tags_fn) (struct yaffs_dev *dev, + int nand_chunk, u8 *data, + struct yaffs_ext_tags *tags); + int (*bad_block_fn) (struct yaffs_dev *dev, int block_no); + int (*query_block_fn) (struct yaffs_dev *dev, int block_no, + enum yaffs_block_state *state, + u32 *seq_number); #endif /* The remove_obj_fn function must be supplied by OS flavours that @@ -554,19 +572,21 @@ struct yaffs_param { * yaffs direct uses it to implement the faster readdir. * Linux uses it to protect the directory during unlocking. */ - void (*remove_obj_fn) (struct yaffs_obj * obj); + void (*remove_obj_fn) (struct yaffs_obj *obj); /* Callback to mark the superblock dirty */ - void (*sb_dirty_fn) (struct yaffs_dev * dev); + void (*sb_dirty_fn) (struct yaffs_dev *dev); /* Callback to control garbage collection. */ - unsigned (*gc_control) (struct yaffs_dev * dev); + unsigned (*gc_control) (struct yaffs_dev *dev); /* Debug control flags. Don't use unless you know what you're doing */ - int use_header_file_size; /* Flag to determine if we should use file sizes from the header */ + int use_header_file_size; /* Flag to determine if we should use + * file sizes from the header */ int disable_lazy_load; /* Disable lazy loading on this device */ int wide_tnodes_disabled; /* Set to disable wide tnodes */ - int disable_soft_del; /* yaffs 1 only: Set to disable the use of softdeletion. */ + int disable_soft_del; /* yaffs 1 only: Set to disable the use of + * softdeletion. */ int defered_dir_update; /* Set to defer directory updates */ @@ -602,7 +622,7 @@ struct yaffs_dev { /* Stuff for figuring out file offset to chunk conversions */ u32 chunk_shift; /* Shift value */ - u32 chunk_div; /* Divisor after shifting: 1 for power-of-2 sizes */ + u32 chunk_div; /* Divisor after shifting: 1 for 2^n sizes */ u32 chunk_mask; /* Mask to use for power-of-2 case */ int is_mounted; @@ -616,7 +636,7 @@ struct yaffs_dev { int chunk_offset; /* Runtime checkpointing stuff */ - int checkpt_page_seq; /* running sequence number of checkpoint pages */ + int checkpt_page_seq; /* running sequence number of checkpt pages */ int checkpt_byte_count; int checkpt_byte_offs; u8 *checkpt_buffer; @@ -630,13 +650,14 @@ struct yaffs_dev { u32 checkpt_sum; u32 checkpt_xor; - int checkpoint_blocks_required; /* Number of blocks needed to store current checkpoint set */ + int checkpoint_blocks_required; /* Number of blocks needed to store + * current checkpoint set */ /* Block Info */ struct yaffs_block_info *block_info; u8 *chunk_bits; /* bitmap of chunks in use */ - unsigned block_info_alt:1; /* was allocated using alternative strategy */ - unsigned chunk_bits_alt:1; /* was allocated using alternative strategy */ + unsigned block_info_alt:1; /* allocated using alternative alloc */ + unsigned chunk_bits_alt:1; /* allocated using alternative alloc */ int chunk_bit_stride; /* Number of bytes of chunk_bits per block. * Must be consistent with chunks_per_block. */ @@ -662,7 +683,8 @@ struct yaffs_dev { u32 *gc_cleanup_list; /* objects to delete at the end of a GC. */ u32 n_clean_ups; - unsigned has_pending_prioritised_gc; /* We think this device might have pending prioritised gcs */ + unsigned has_pending_prioritised_gc; /* We think this device might + have pending prioritised gcs */ unsigned gc_disable; unsigned gc_block_finder; unsigned gc_dirtiest; @@ -676,11 +698,6 @@ struct yaffs_dev { struct yaffs_obj *root_dir; struct yaffs_obj *lost_n_found; - /* Buffer areas for storing data to recover from write failures TODO - * u8 buffered_data[YAFFS_CHUNKS_PER_BLOCK][YAFFS_BYTES_PER_CHUNK]; - * struct yaffs_spare buffered_spare[YAFFS_CHUNKS_PER_BLOCK]; - */ - int buffered_block; /* Which block is buffered here? */ int doing_buffered_block_rewrite; @@ -688,9 +705,12 @@ struct yaffs_dev { int cache_last_use; /* Stuff for background deletion and unlinked files. */ - struct yaffs_obj *unlinked_dir; /* Directory where unlinked and deleted files live. */ - struct yaffs_obj *del_dir; /* Directory where deleted objects are sent to disappear. */ - struct yaffs_obj *unlinked_deletion; /* Current file being background deleted. */ + struct yaffs_obj *unlinked_dir; /* Directory where unlinked and deleted + files live. */ + struct yaffs_obj *del_dir; /* Directory where deleted objects are + sent to disappear. */ + struct yaffs_obj *unlinked_deletion; /* Current file being + background deleted. */ int n_deleted_files; /* Count of files awaiting deletion; */ int n_unlinked_files; /* Count of unlinked files. */ int n_bg_deletions; /* Count of background deletions. */ @@ -703,12 +723,14 @@ struct yaffs_dev { int unmanaged_buffer_deallocs; /* yaffs2 runtime stuff */ - unsigned seq_number; /* Sequence number of currently allocating block */ + unsigned seq_number; /* Sequence number of currently + allocating block */ unsigned oldest_dirty_seq; unsigned oldest_dirty_block; /* Block refreshing */ - int refresh_skip; /* A skip down counter. Refresh happens when this gets to zero. */ + int refresh_skip; /* A skip down counter. + * Refresh happens when this gets to zero. */ /* Dirty directory handling */ struct list_head dirty_dirs; /* List of dirty directories */ @@ -737,8 +759,8 @@ struct yaffs_dev { }; -/* The CheckpointDevice structure holds the device information that changes at runtime and - * must be preserved over unmount/mount cycles. +/* The CheckpointDevice structure holds the device information that changes + *at runtime and must be preserved over unmount/mount cycles. */ struct yaffs_checkpt_dev { int struct_type; @@ -752,7 +774,8 @@ struct yaffs_checkpt_dev { int n_bg_deletions; /* Count of background deletions. */ /* yaffs2 runtime stuff */ - unsigned seq_number; /* Sequence number of currently allocating block */ + unsigned seq_number; /* Sequence number of currently + * allocating block */ }; @@ -806,7 +829,7 @@ int yaffs_wr_file(struct yaffs_obj *obj, const u8 * buffer, loff_t offset, int yaffs_resize_file(struct yaffs_obj *obj, loff_t new_size); struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent, - const YCHAR * name, u32 mode, u32 uid, + const YCHAR *name, u32 mode, u32 uid, u32 gid); int yaffs_flush_file(struct yaffs_obj *obj, int update_time, int data_sync); @@ -818,35 +841,35 @@ int yaffs_checkpoint_save(struct yaffs_dev *dev); int yaffs_checkpoint_restore(struct yaffs_dev *dev); /* Directory operations */ -struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR * name, +struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR *name, u32 mode, u32 uid, u32 gid); struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *the_dir, - const YCHAR * name); + const YCHAR *name); struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number); /* Link operations */ -struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name, +struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR *name, struct yaffs_obj *equiv_obj); struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj); /* Symlink operations */ struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent, - const YCHAR * name, u32 mode, u32 uid, - u32 gid, const YCHAR * alias); + const YCHAR *name, u32 mode, u32 uid, + u32 gid, const YCHAR *alias); YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj); /* Special inodes (fifos, sockets and devices) */ struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent, - const YCHAR * name, u32 mode, u32 uid, + const YCHAR *name, u32 mode, u32 uid, u32 gid, u32 rdev); -int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR * name, +int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR *name, const void *value, int size, int flags); -int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR * name, void *value, +int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR *name, void *value, int size); int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size); -int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR * name); +int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR *name); /* Special directories */ struct yaffs_obj *yaffs_root(struct yaffs_dev *dev); @@ -866,26 +889,26 @@ void yaffs_guts_test(struct yaffs_dev *dev); /* A few useful functions to be used within the core files*/ void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash, int lyn); -int yaffs_check_ff(u8 * buffer, int n_bytes); +int yaffs_check_ff(u8 *buffer, int n_bytes); void yaffs_handle_chunk_error(struct yaffs_dev *dev, struct yaffs_block_info *bi); u8 *yaffs_get_temp_buffer(struct yaffs_dev *dev, int line_no); -void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 * buffer, int line_no); +void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 *buffer, int line_no); struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev, int number, enum yaffs_obj_type type); int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk, int nand_chunk, int in_scan); -void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR * name); +void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR *name); void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj, const struct yaffs_obj_hdr *oh); void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj); -YCHAR *yaffs_clone_str(const YCHAR * str); +YCHAR *yaffs_clone_str(const YCHAR *str); void yaffs_link_fixup(struct yaffs_dev *dev, struct yaffs_obj *hard_list); void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no); -int yaffs_update_oh(struct yaffs_obj *in, const YCHAR * name, +int yaffs_update_oh(struct yaffs_obj *in, const YCHAR *name, int force, int is_shrink, int shadows, struct yaffs_xattr_mod *xop); void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id, @@ -897,7 +920,7 @@ struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev, u32 chunk_id, struct yaffs_tnode *passed_tn); -int yaffs_do_file_wr(struct yaffs_obj *in, const u8 * buffer, loff_t offset, +int yaffs_do_file_wr(struct yaffs_obj *in, const u8 *buffer, loff_t offset, int n_bytes, int write_trhrough); void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size); void yaffs_skip_rest_of_block(struct yaffs_dev *dev); @@ -911,4 +934,5 @@ struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev, u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn, unsigned pos); +int yaffs_is_non_empty_dir(struct yaffs_obj *obj); #endif diff --git a/fs/yaffs2/yaffs_linux.h b/fs/yaffs2/yaffs_linux.h index 3b508cbc..8c2f3997 100644 --- a/fs/yaffs2/yaffs_linux.h +++ b/fs/yaffs2/yaffs_linux.h @@ -25,11 +25,11 @@ struct yaffs_linux_context { struct task_struct *bg_thread; /* Background thread for this device */ int bg_running; struct mutex gross_lock; /* Gross locking mutex*/ - u8 *spare_buffer; /* For mtdif2 use. Don't know the size of the buffer + u8 *spare_buffer; /* For mtdif2 use. Don't know the buffer size * at compile time so we have to allocate it. */ struct list_head search_contexts; - void (*put_super_fn) (struct super_block * sb); + void (*put_super_fn) (struct super_block *sb); struct task_struct *readdir_process; unsigned mount_id; diff --git a/fs/yaffs2/yaffs_mtdif.c b/fs/yaffs2/yaffs_mtdif.c index 7cf53b3d..b36a8bec 100644 --- a/fs/yaffs2/yaffs_mtdif.c +++ b/fs/yaffs2/yaffs_mtdif.c @@ -29,7 +29,6 @@ int nandmtd_erase_block(struct yaffs_dev *dev, int block_no) ((loff_t) block_no) * dev->param.total_bytes_per_chunk * dev->param.chunks_per_block; struct erase_info ei; - int retval = 0; ei.mtd = mtd; diff --git a/fs/yaffs2/yaffs_mtdif1.c b/fs/yaffs2/yaffs_mtdif1.c index c3ea8c9b..29100071 100644 --- a/fs/yaffs2/yaffs_mtdif1.c +++ b/fs/yaffs2/yaffs_mtdif1.c @@ -64,7 +64,7 @@ static struct nand_ecclayout nand_oob_16 = { .eccbytes = 6, .eccpos = {8, 9, 10, 13, 14, 15}, .oobavail = 9, - .oobfree = {{0, 4}, {6, 2}, {11, 2}, {4, 1}} + .oobfree = {{0, 4}, {6, 2}, {11, 2}, {4, 1} } }; #endif @@ -90,7 +90,7 @@ static struct nand_ecclayout nand_oob_16 = { * Returns YAFFS_OK or YAFFS_FAIL. */ int nandmtd1_write_chunk_tags(struct yaffs_dev *dev, - int nand_chunk, const u8 * data, + int nand_chunk, const u8 *data, const struct yaffs_ext_tags *etags) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); @@ -120,11 +120,11 @@ int nandmtd1_write_chunk_tags(struct yaffs_dev *dev, pt1.deleted = 0; } #else - ((u8 *) & pt1)[8] = 0xff; + ((u8 *) &pt1)[8] = 0xff; if (etags->is_deleted) { memset(&pt1, 0xff, 8); /* zero page_status byte to indicate deleted */ - ((u8 *) & pt1)[8] = 0; + ((u8 *) &pt1)[8] = 0; } #endif @@ -133,13 +133,13 @@ int nandmtd1_write_chunk_tags(struct yaffs_dev *dev, ops.len = (data) ? chunk_bytes : 0; ops.ooblen = YTAG1_SIZE; ops.datbuf = (u8 *) data; - ops.oobbuf = (u8 *) & pt1; + ops.oobbuf = (u8 *) &pt1; retval = mtd->write_oob(mtd, addr, &ops); if (retval) { - T(YAFFS_TRACE_MTD, - (TSTR("write_oob failed, chunk %d, mtd error %d" TENDSTR), - nand_chunk, retval)); + yaffs_trace(YAFFS_TRACE_MTD, + "write_oob failed, chunk %d, mtd error %d", + nand_chunk, retval); } return retval ? YAFFS_FAIL : YAFFS_OK; } @@ -169,7 +169,7 @@ static int rettags(struct yaffs_ext_tags *etags, int ecc_result, int retval) * Returns YAFFS_OK or YAFFS_FAIL. */ int nandmtd1_read_chunk_tags(struct yaffs_dev *dev, - int nand_chunk, u8 * data, + int nand_chunk, u8 *data, struct yaffs_ext_tags *etags) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); @@ -186,7 +186,7 @@ int nandmtd1_read_chunk_tags(struct yaffs_dev *dev, ops.len = (data) ? chunk_bytes : 0; ops.ooblen = YTAG1_SIZE; ops.datbuf = data; - ops.oobbuf = (u8 *) & pt1; + ops.oobbuf = (u8 *) &pt1; #if (MTD_VERSION_CODE < MTD_VERSION(2, 6, 20)) /* In MTD 2.6.18 to 2.6.19 nand_base.c:nand_do_read_oob() has a bug; @@ -198,11 +198,10 @@ int nandmtd1_read_chunk_tags(struct yaffs_dev *dev, * Check status and determine ECC result. */ retval = mtd->read_oob(mtd, addr, &ops); - if (retval) { - T(YAFFS_TRACE_MTD, - (TSTR("read_oob failed, chunk %d, mtd error %d" TENDSTR), - nand_chunk, retval)); - } + if (retval) + yaffs_trace(YAFFS_TRACE_MTD, + "read_oob failed, chunk %d, mtd error %d", + nand_chunk, retval); switch (retval) { case 0: @@ -227,7 +226,7 @@ int nandmtd1_read_chunk_tags(struct yaffs_dev *dev, /* Check for a blank/erased chunk. */ - if (yaffs_check_ff((u8 *) & pt1, 8)) { + if (yaffs_check_ff((u8 *) &pt1, 8)) { /* when blank, upper layers want ecc_result to be <= NO_ERROR */ return rettags(etags, YAFFS_ECC_RESULT_NO_ERROR, YAFFS_OK); } @@ -239,7 +238,7 @@ int nandmtd1_read_chunk_tags(struct yaffs_dev *dev, deleted = !pt1.deleted; pt1.deleted = 1; #else - deleted = (yaffs_count_bits(((u8 *) & pt1)[8]) < 7); + deleted = (hweight8(((u8 *) &pt1)[8]) < 7); #endif /* Check the packed tags mini-ECC and correct if necessary/possible. @@ -286,8 +285,7 @@ int nandmtd1_mark_block_bad(struct yaffs_dev *dev, int block_no) int blocksize = dev->param.chunks_per_block * dev->data_bytes_per_chunk; int retval; - T(YAFFS_TRACE_BAD_BLOCKS, - (TSTR("marking block %d bad" TENDSTR), block_no)); + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, "marking block %d bad", block_no); retval = mtd->block_markbad(mtd, (loff_t) blocksize * block_no); return (retval) ? YAFFS_FAIL : YAFFS_OK; @@ -304,10 +302,9 @@ static int nandmtd1_test_prerequists(struct mtd_info *mtd) int oobavail = mtd->ecclayout->oobavail; if (oobavail < YTAG1_SIZE) { - T(YAFFS_TRACE_ERROR, - (TSTR - ("mtd device has only %d bytes for tags, need %d" TENDSTR), - oobavail, YTAG1_SIZE)); + yaffs_trace(YAFFS_TRACE_ERROR, + "mtd device has only %d bytes for tags, need %d", + oobavail, YTAG1_SIZE); return YAFFS_FAIL; } return YAFFS_OK; @@ -342,8 +339,9 @@ int nandmtd1_query_block(struct yaffs_dev *dev, int block_no, retval = nandmtd1_read_chunk_tags(dev, chunk_num, NULL, &etags); etags.block_bad = (mtd->block_isbad) (mtd, addr); if (etags.block_bad) { - T(YAFFS_TRACE_BAD_BLOCKS, - (TSTR("block %d is marked bad" TENDSTR), block_no)); + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, + "block %d is marked bad", + block_no); state = YAFFS_BLOCK_STATE_DEAD; } else if (etags.ecc_result != YAFFS_ECC_RESULT_NO_ERROR) { /* bad tags, need to look more closely */ diff --git a/fs/yaffs2/yaffs_mtdif1.h b/fs/yaffs2/yaffs_mtdif1.h index 07ce4524..034c6803 100644 --- a/fs/yaffs2/yaffs_mtdif1.h +++ b/fs/yaffs2/yaffs_mtdif1.h @@ -15,15 +15,15 @@ #define __YAFFS_MTDIF1_H__ int nandmtd1_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk, - const u8 * data, + const u8 *data, const struct yaffs_ext_tags *tags); int nandmtd1_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk, - u8 * data, struct yaffs_ext_tags *tags); + u8 *data, struct yaffs_ext_tags *tags); int nandmtd1_mark_block_bad(struct yaffs_dev *dev, int block_no); int nandmtd1_query_block(struct yaffs_dev *dev, int block_no, - enum yaffs_block_state *state, u32 * seq_number); + enum yaffs_block_state *state, u32 *seq_number); #endif diff --git a/fs/yaffs2/yaffs_mtdif2.c b/fs/yaffs2/yaffs_mtdif2.c index 555f937c..6eac0251 100644 --- a/fs/yaffs2/yaffs_mtdif2.c +++ b/fs/yaffs2/yaffs_mtdif2.c @@ -27,11 +27,11 @@ #include "yaffs_linux.h" /* NB For use with inband tags.... - * We assume that the data buffer is of size total_bytes_per_chunk so that we can also - * use it to load the tags. + * We assume that the data buffer is of size total_bytes_per_chunk so + * that we can also use it to load the tags. */ int nandmtd2_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk, - const u8 * data, + const u8 *data, const struct yaffs_ext_tags *tags) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); @@ -51,10 +51,9 @@ int nandmtd2_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk, void *packed_tags_ptr = dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt; - T(YAFFS_TRACE_MTD, - (TSTR - ("nandmtd2_write_chunk_tags chunk %d data %p tags %p" - TENDSTR), nand_chunk, data, tags)); + yaffs_trace(YAFFS_TRACE_MTD, + "nandmtd2_write_chunk_tags chunk %d data %p tags %p", + nand_chunk, data, tags); addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk; @@ -68,11 +67,12 @@ int nandmtd2_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk, struct yaffs_packed_tags2_tags_only *pt2tp; pt2tp = (struct yaffs_packed_tags2_tags_only *)(data + - dev-> - data_bytes_per_chunk); + dev-> + data_bytes_per_chunk); yaffs_pack_tags2_tags_only(pt2tp, tags); - } else + } else { yaffs_pack_tags2(&pt, tags, !dev->param.no_tags_ecc); + } #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17)) ops.mode = MTD_OOB_AUTO; @@ -102,7 +102,7 @@ int nandmtd2_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk, } int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk, - u8 * data, struct yaffs_ext_tags *tags) + u8 *data, struct yaffs_ext_tags *tags) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17)) @@ -121,10 +121,9 @@ int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk, void *packed_tags_ptr = dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt; - T(YAFFS_TRACE_MTD, - (TSTR - ("nandmtd2_read_chunk_tags chunk %d data %p tags %p" - TENDSTR), nand_chunk, data, tags)); + yaffs_trace(YAFFS_TRACE_MTD, + "nandmtd2_read_chunk_tags chunk %d data %p tags %p", + nand_chunk, data, tags); if (dev->param.inband_tags) { @@ -169,8 +168,8 @@ int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk, if (tags) { struct yaffs_packed_tags2_tags_only *pt2tp; pt2tp = - (struct yaffs_packed_tags2_tags_only *)&data[dev-> - data_bytes_per_chunk]; + (struct yaffs_packed_tags2_tags_only *) + &data[dev->data_bytes_per_chunk]; yaffs_unpack_tags2_tags_only(tags, pt2tp); } } else { @@ -205,8 +204,9 @@ int nandmtd2_mark_block_bad(struct yaffs_dev *dev, int block_no) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); int retval; - T(YAFFS_TRACE_MTD, - (TSTR("nandmtd2_mark_block_bad %d" TENDSTR), block_no)); + yaffs_trace(YAFFS_TRACE_MTD, + "nandmtd2_mark_block_bad %d", + block_no); retval = mtd->block_markbad(mtd, @@ -221,19 +221,19 @@ int nandmtd2_mark_block_bad(struct yaffs_dev *dev, int block_no) } int nandmtd2_query_block(struct yaffs_dev *dev, int block_no, - enum yaffs_block_state *state, u32 * seq_number) + enum yaffs_block_state *state, u32 *seq_number) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); int retval; - T(YAFFS_TRACE_MTD, (TSTR("nandmtd2_query_block %d" TENDSTR), block_no)); + yaffs_trace(YAFFS_TRACE_MTD, "nandmtd2_query_block %d", block_no); retval = mtd->block_isbad(mtd, block_no * dev->param.chunks_per_block * dev->param.total_bytes_per_chunk); if (retval) { - T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR))); + yaffs_trace(YAFFS_TRACE_MTD, "block is bad"); *state = YAFFS_BLOCK_STATE_DEAD; *seq_number = 0; @@ -250,11 +250,13 @@ int nandmtd2_query_block(struct yaffs_dev *dev, int block_no, *state = YAFFS_BLOCK_STATE_EMPTY; } } - T(YAFFS_TRACE_MTD, - (TSTR("block is bad seq %d state %d" TENDSTR), *seq_number, *state)); + yaffs_trace(YAFFS_TRACE_MTD, + "block is bad seq %d state %d", + *seq_number, *state); if (retval == 0) return YAFFS_OK; else return YAFFS_FAIL; } + diff --git a/fs/yaffs2/yaffs_mtdif2.h b/fs/yaffs2/yaffs_mtdif2.h index d8211261..78babf5d 100644 --- a/fs/yaffs2/yaffs_mtdif2.h +++ b/fs/yaffs2/yaffs_mtdif2.h @@ -18,12 +18,12 @@ #include "yaffs_guts.h" int nandmtd2_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk, - const u8 * data, + const u8 *data, const struct yaffs_ext_tags *tags); int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk, - u8 * data, struct yaffs_ext_tags *tags); + u8 *data, struct yaffs_ext_tags *tags); int nandmtd2_mark_block_bad(struct yaffs_dev *dev, int block_no); int nandmtd2_query_block(struct yaffs_dev *dev, int block_no, - enum yaffs_block_state *state, u32 * seq_number); + enum yaffs_block_state *state, u32 *seq_number); #endif diff --git a/fs/yaffs2/yaffs_nameval.c b/fs/yaffs2/yaffs_nameval.c index d7cb72aa..18c30b33 100644 --- a/fs/yaffs2/yaffs_nameval.c +++ b/fs/yaffs2/yaffs_nameval.c @@ -12,15 +12,15 @@ */ /* - * This simple implementation of a name-value store assumes a small number of values and fits - * into a small finite buffer. + * This simple implementation of a name-value store assumes a small number of +* values and fits into a small finite buffer. * * Each attribute is stored as a record: * sizeof(int) bytes record size. * strnlen+1 bytes name null terminated. * nbytes value. * ---------- - * total size stored in record size + * total size stored in record size * * This code has not been tested with unicode yet. */ @@ -29,7 +29,7 @@ #include "yportenv.h" -static int nval_find(const char *xb, int xb_size, const YCHAR * name, +static int nval_find(const char *xb, int xb_size, const YCHAR *name, int *exist_size) { int pos = 0; @@ -37,7 +37,7 @@ static int nval_find(const char *xb, int xb_size, const YCHAR * name, memcpy(&size, xb, sizeof(int)); while (size > 0 && (size < xb_size) && (pos + size < xb_size)) { - if (yaffs_strncmp + if (strncmp ((YCHAR *) (xb + pos + sizeof(int)), name, size) == 0) { if (exist_size) *exist_size = size; @@ -70,26 +70,28 @@ static int nval_used(const char *xb, int xb_size) return pos; } -int nval_del(char *xb, int xb_size, const YCHAR * name) +int nval_del(char *xb, int xb_size, const YCHAR *name) { int pos = nval_find(xb, xb_size, name, NULL); int size; if (pos >= 0 && pos < xb_size) { - /* Find size, shift rest over this record, then zero out the rest of buffer */ + /* Find size, shift rest over this record, + * then zero out the rest of buffer */ memcpy(&size, xb + pos, sizeof(int)); memcpy(xb + pos, xb + pos + size, xb_size - (pos + size)); memset(xb + (xb_size - size), 0, size); return 0; - } else + } else { return -ENODATA; + } } -int nval_set(char *xb, int xb_size, const YCHAR * name, const char *buf, - int bsize, int flags) +int nval_set(char *xb, int xb_size, const YCHAR *name, const char *buf, + int bsize, int flags) { int pos; - int namelen = yaffs_strnlen(name, xb_size); + int namelen = strnlen(name, xb_size); int reclen; int size_exist = 0; int space; @@ -119,7 +121,7 @@ int nval_set(char *xb, int xb_size, const YCHAR * name, const char *buf, memcpy(xb + pos, &reclen, sizeof(int)); pos += sizeof(int); - yaffs_strncpy((YCHAR *) (xb + pos), name, reclen); + strncpy((YCHAR *) (xb + pos), name, reclen); pos += (namelen + 1); memcpy(xb + pos, buf, bsize); return 0; @@ -167,11 +169,13 @@ int nval_list(const char *xb, int xb_size, char *buf, int bsize) int filled = 0; memcpy(&size, xb + pos, sizeof(int)); - while (size > sizeof(int) && size <= xb_size && (pos + size) < xb_size - && !filled) { + while (size > sizeof(int) && + size <= xb_size && + (pos + size) < xb_size && + !filled) { pos += sizeof(int); size -= sizeof(int); - name_len = yaffs_strnlen((YCHAR *) (xb + pos), size); + name_len = strnlen((YCHAR *) (xb + pos), size); if (ncopied + name_len + 1 < bsize) { memcpy(buf, xb + pos, name_len * sizeof(YCHAR)); buf += name_len; @@ -182,8 +186,9 @@ int nval_list(const char *xb, int xb_size, char *buf, int bsize) buf++; } ncopied += (name_len + 1); - } else + } else { filled = 1; + } pos += size; if (pos < xb_size - sizeof(int)) memcpy(&size, xb + pos, sizeof(int)); diff --git a/fs/yaffs2/yaffs_nand.c b/fs/yaffs2/yaffs_nand.c index 84ab0f02..ee061a82 100644 --- a/fs/yaffs2/yaffs_nand.c +++ b/fs/yaffs2/yaffs_nand.c @@ -18,16 +18,15 @@ #include "yaffs_getblockinfo.h" int yaffs_rd_chunk_tags_nand(struct yaffs_dev *dev, int nand_chunk, - u8 * buffer, struct yaffs_ext_tags *tags) + u8 *buffer, struct yaffs_ext_tags *tags) { int result; struct yaffs_ext_tags local_tags; - int realigned_chunk = nand_chunk - dev->chunk_offset; dev->n_page_reads++; - /* If there are no tags provided, use local tags to get prioritised gc working */ + /* If there are no tags provided use local tags. */ if (!tags) tags = &local_tags; @@ -46,38 +45,36 @@ int yaffs_rd_chunk_tags_nand(struct yaffs_dev *dev, int nand_chunk, dev->param.chunks_per_block); yaffs_handle_chunk_error(dev, bi); } - return result; } int yaffs_wr_chunk_tags_nand(struct yaffs_dev *dev, - int nand_chunk, - const u8 * buffer, struct yaffs_ext_tags *tags) + int nand_chunk, + const u8 *buffer, struct yaffs_ext_tags *tags) { - dev->n_page_writes++; - nand_chunk -= dev->chunk_offset; if (tags) { tags->seq_number = dev->seq_number; tags->chunk_used = 1; if (!yaffs_validate_tags(tags)) { - T(YAFFS_TRACE_ERROR, - (TSTR("Writing uninitialised tags" TENDSTR))); + yaffs_trace(YAFFS_TRACE_ERROR, + "Writing uninitialised tags"); YBUG(); } - T(YAFFS_TRACE_WRITE, - (TSTR("Writing chunk %d tags %d %d" TENDSTR), nand_chunk, - tags->obj_id, tags->chunk_id)); + yaffs_trace(YAFFS_TRACE_WRITE, + "Writing chunk %d tags %d %d", + nand_chunk, tags->obj_id, tags->chunk_id); } else { - T(YAFFS_TRACE_ERROR, (TSTR("Writing with no tags" TENDSTR))); + yaffs_trace(YAFFS_TRACE_ERROR, "Writing with no tags"); YBUG(); + return YAFFS_FAIL; } if (dev->param.write_chunk_tags_fn) return dev->param.write_chunk_tags_fn(dev, nand_chunk, buffer, - tags); + tags); else return yaffs_tags_compat_wr(dev, nand_chunk, buffer, tags); } @@ -85,7 +82,6 @@ int yaffs_wr_chunk_tags_nand(struct yaffs_dev *dev, int yaffs_mark_bad(struct yaffs_dev *dev, int block_no) { block_no -= dev->block_offset; - if (dev->param.bad_block_fn) return dev->param.bad_block_fn(dev, block_no); else @@ -95,10 +91,9 @@ int yaffs_mark_bad(struct yaffs_dev *dev, int block_no) int yaffs_query_init_block_state(struct yaffs_dev *dev, int block_no, enum yaffs_block_state *state, - u32 * seq_number) + u32 *seq_number) { block_no -= dev->block_offset; - if (dev->param.query_block_fn) return dev->param.query_block_fn(dev, block_no, state, seq_number); @@ -112,11 +107,8 @@ int yaffs_erase_block(struct yaffs_dev *dev, int flash_block) int result; flash_block -= dev->block_offset; - dev->n_erasures++; - result = dev->param.erase_fn(dev, flash_block); - return result; } diff --git a/fs/yaffs2/yaffs_nand.h b/fs/yaffs2/yaffs_nand.h index 543f1987..a36498a9 100644 --- a/fs/yaffs2/yaffs_nand.h +++ b/fs/yaffs2/yaffs_nand.h @@ -18,11 +18,11 @@ #include "yaffs_guts.h" int yaffs_rd_chunk_tags_nand(struct yaffs_dev *dev, int nand_chunk, - u8 * buffer, struct yaffs_ext_tags *tags); + u8 *buffer, struct yaffs_ext_tags *tags); int yaffs_wr_chunk_tags_nand(struct yaffs_dev *dev, int nand_chunk, - const u8 * buffer, struct yaffs_ext_tags *tags); + const u8 *buffer, struct yaffs_ext_tags *tags); int yaffs_mark_bad(struct yaffs_dev *dev, int block_no); diff --git a/fs/yaffs2/yaffs_packedtags1.c b/fs/yaffs2/yaffs_packedtags1.c index a77f0954..873f0652 100644 --- a/fs/yaffs2/yaffs_packedtags1.c +++ b/fs/yaffs2/yaffs_packedtags1.c @@ -25,15 +25,15 @@ void yaffs_pack_tags1(struct yaffs_packed_tags1 *pt, pt->deleted = (t->is_deleted) ? 0 : 1; pt->unused_stuff = 0; pt->should_be_ff = 0xFFFFFFFF; - } void yaffs_unpack_tags1(struct yaffs_ext_tags *t, const struct yaffs_packed_tags1 *pt) { - static const u8 all_ff[] = - { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff + static const u8 all_ff[12] = { + 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff }; if (memcmp(all_ff, pt, sizeof(struct yaffs_packed_tags1))) { diff --git a/fs/yaffs2/yaffs_packedtags2.c b/fs/yaffs2/yaffs_packedtags2.c index 6c12aed2..2bb31b07 100644 --- a/fs/yaffs2/yaffs_packedtags2.c +++ b/fs/yaffs2/yaffs_packedtags2.c @@ -38,12 +38,12 @@ #define EXTRA_OBJECT_TYPE_SHIFT (28) #define EXTRA_OBJECT_TYPE_MASK ((0x0F) << EXTRA_OBJECT_TYPE_SHIFT) -static void yaffs_dump_packed_tags2_tags_only(const struct - yaffs_packed_tags2_tags_only *ptt) +static void yaffs_dump_packed_tags2_tags_only( + const struct yaffs_packed_tags2_tags_only *ptt) { - T(YAFFS_TRACE_MTD, - (TSTR("packed tags obj %d chunk %d byte %d seq %d" TENDSTR), - ptt->obj_id, ptt->chunk_id, ptt->n_bytes, ptt->seq_number)); + yaffs_trace(YAFFS_TRACE_MTD, + "packed tags obj %d chunk %d byte %d seq %d", + ptt->obj_id, ptt->chunk_id, ptt->n_bytes, ptt->seq_number); } static void yaffs_dump_packed_tags2(const struct yaffs_packed_tags2 *pt) @@ -53,12 +53,11 @@ static void yaffs_dump_packed_tags2(const struct yaffs_packed_tags2 *pt) static void yaffs_dump_tags2(const struct yaffs_ext_tags *t) { - T(YAFFS_TRACE_MTD, - (TSTR - ("ext.tags eccres %d blkbad %d chused %d obj %d chunk%d byte %d del %d ser %d seq %d" - TENDSTR), t->ecc_result, t->block_bad, t->chunk_used, t->obj_id, - t->chunk_id, t->n_bytes, t->is_deleted, t->serial_number, - t->seq_number)); + yaffs_trace(YAFFS_TRACE_MTD, + "ext.tags eccres %d blkbad %d chused %d obj %d chunk%d byte %d del %d ser %d seq %d", + t->ecc_result, t->block_bad, t->chunk_used, t->obj_id, + t->chunk_id, t->n_bytes, t->is_deleted, t->serial_number, + t->seq_number); } @@ -109,9 +108,7 @@ void yaffs_pack_tags2(struct yaffs_packed_tags2 *pt, void yaffs_unpack_tags2_tags_only(struct yaffs_ext_tags *t, struct yaffs_packed_tags2_tags_only *ptt) { - memset(t, 0, sizeof(struct yaffs_ext_tags)); - yaffs_init_tags(t); if (ptt->seq_number != 0xFFFFFFFF) { @@ -125,7 +122,6 @@ void yaffs_unpack_tags2_tags_only(struct yaffs_ext_tags *t, t->seq_number = ptt->seq_number; /* Do extra header info stuff */ - if (ptt->chunk_id & EXTRA_HEADER_INFO_FLAG) { t->chunk_id = 0; t->n_bytes = 0; @@ -147,16 +143,13 @@ void yaffs_unpack_tags2_tags_only(struct yaffs_ext_tags *t, t->extra_length = ptt->n_bytes; } } - yaffs_dump_packed_tags2_tags_only(ptt); yaffs_dump_tags2(t); - } void yaffs_unpack_tags2(struct yaffs_ext_tags *t, struct yaffs_packed_tags2 *pt, int tags_ecc) { - enum yaffs_ecc_result ecc_result = YAFFS_ECC_RESULT_NO_ERROR; if (pt->t.seq_number != 0xFFFFFFFF && tags_ecc) { @@ -165,14 +158,12 @@ void yaffs_unpack_tags2(struct yaffs_ext_tags *t, struct yaffs_packed_tags2 *pt, struct yaffs_ecc_other ecc; int result; yaffs_ecc_calc_other((unsigned char *)&pt->t, - sizeof(struct - yaffs_packed_tags2_tags_only), - &ecc); + sizeof(struct yaffs_packed_tags2_tags_only), + &ecc); result = yaffs_ecc_correct_other((unsigned char *)&pt->t, - sizeof(struct - yaffs_packed_tags2_tags_only), - &pt->ecc, &ecc); + sizeof(struct yaffs_packed_tags2_tags_only), + &pt->ecc, &ecc); switch (result) { case 0: ecc_result = YAFFS_ECC_RESULT_NO_ERROR; @@ -187,7 +178,6 @@ void yaffs_unpack_tags2(struct yaffs_ext_tags *t, struct yaffs_packed_tags2 *pt, ecc_result = YAFFS_ECC_RESULT_UNKNOWN; } } - yaffs_unpack_tags2_tags_only(t, &pt->t); t->ecc_result = ecc_result; diff --git a/fs/yaffs2/yaffs_tagscompat.c b/fs/yaffs2/yaffs_tagscompat.c index 3a9a157e..35d33c91 100644 --- a/fs/yaffs2/yaffs_tagscompat.c +++ b/fs/yaffs2/yaffs_tagscompat.c @@ -19,35 +19,10 @@ static void yaffs_handle_rd_data_error(struct yaffs_dev *dev, int nand_chunk); -static const char yaffs_count_bits_table[256] = { - 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, - 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, - 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, - 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, - 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, - 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, - 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, - 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, - 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, - 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, - 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, - 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, - 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, - 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, - 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, - 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 -}; - -int yaffs_count_bits(u8 x) -{ - int ret_val; - ret_val = yaffs_count_bits_table[x]; - return ret_val; -} /********** Tags ECC calculations *********/ -void yaffs_calc_ecc(const u8 * data, struct yaffs_spare *spare) +void yaffs_calc_ecc(const u8 *data, struct yaffs_spare *spare) { yaffs_ecc_cacl(data, spare->ecc1); yaffs_ecc_cacl(&data[256], spare->ecc2); @@ -56,7 +31,6 @@ void yaffs_calc_ecc(const u8 * data, struct yaffs_spare *spare) void yaffs_calc_tags_ecc(struct yaffs_tags *tags) { /* Calculate an ecc */ - unsigned char *b = ((union yaffs_tags_union *)tags)->as_bytes; unsigned i, j; unsigned ecc = 0; @@ -71,9 +45,7 @@ void yaffs_calc_tags_ecc(struct yaffs_tags *tags) ecc ^= bit; } } - tags->ecc = ecc; - } int yaffs_check_tags_ecc(struct yaffs_tags *tags) @@ -101,7 +73,6 @@ int yaffs_check_tags_ecc(struct yaffs_tags *tags) /* TODO Need to do somethiong here */ return -1; /* unrecovered error */ } - return 0; } @@ -153,13 +124,13 @@ static void yaffs_spare_init(struct yaffs_spare *spare) } static int yaffs_wr_nand(struct yaffs_dev *dev, - int nand_chunk, const u8 * data, + int nand_chunk, const u8 *data, struct yaffs_spare *spare) { if (nand_chunk < dev->param.start_block * dev->param.chunks_per_block) { - T(YAFFS_TRACE_ERROR, - (TSTR("**>> yaffs chunk %d is not valid" TENDSTR), - nand_chunk)); + yaffs_trace(YAFFS_TRACE_ERROR, + "**>> yaffs chunk %d is not valid", + nand_chunk); return YAFFS_FAIL; } @@ -168,7 +139,7 @@ static int yaffs_wr_nand(struct yaffs_dev *dev, static int yaffs_rd_chunk_nand(struct yaffs_dev *dev, int nand_chunk, - u8 * data, + u8 *data, struct yaffs_spare *spare, enum yaffs_ecc_result *ecc_result, int correct_errors) @@ -176,7 +147,7 @@ static int yaffs_rd_chunk_nand(struct yaffs_dev *dev, int ret_val; struct yaffs_spare local_spare; - if (!spare && data) { + if (!spare) { /* If we don't have a real spare, then we use a local one. */ /* Need this for the calculation of the ecc */ spare = &local_spare; @@ -200,30 +171,26 @@ static int yaffs_rd_chunk_nand(struct yaffs_dev *dev, calc_ecc); if (ecc_result1 > 0) { - T(YAFFS_TRACE_ERROR, - (TSTR - ("**>>yaffs ecc error fix performed on chunk %d:0" - TENDSTR), nand_chunk)); + yaffs_trace(YAFFS_TRACE_ERROR, + "**>>yaffs ecc error fix performed on chunk %d:0", + nand_chunk); dev->n_ecc_fixed++; } else if (ecc_result1 < 0) { - T(YAFFS_TRACE_ERROR, - (TSTR - ("**>>yaffs ecc error unfixed on chunk %d:0" - TENDSTR), nand_chunk)); + yaffs_trace(YAFFS_TRACE_ERROR, + "**>>yaffs ecc error unfixed on chunk %d:0", + nand_chunk); dev->n_ecc_unfixed++; } if (ecc_result2 > 0) { - T(YAFFS_TRACE_ERROR, - (TSTR - ("**>>yaffs ecc error fix performed on chunk %d:1" - TENDSTR), nand_chunk)); + yaffs_trace(YAFFS_TRACE_ERROR, + "**>>yaffs ecc error fix performed on chunk %d:1", + nand_chunk); dev->n_ecc_fixed++; } else if (ecc_result2 < 0) { - T(YAFFS_TRACE_ERROR, - (TSTR - ("**>>yaffs ecc error unfixed on chunk %d:1" - TENDSTR), nand_chunk)); + yaffs_trace(YAFFS_TRACE_ERROR, + "**>>yaffs ecc error unfixed on chunk %d:1", + nand_chunk); dev->n_ecc_unfixed++; } @@ -252,27 +219,23 @@ static int yaffs_rd_chunk_nand(struct yaffs_dev *dev, memcpy(spare, &nspare, sizeof(struct yaffs_spare)); if (data && correct_errors) { if (nspare.eccres1 > 0) { - T(YAFFS_TRACE_ERROR, - (TSTR - ("**>>mtd ecc error fix performed on chunk %d:0" - TENDSTR), nand_chunk)); + yaffs_trace(YAFFS_TRACE_ERROR, + "**>>mtd ecc error fix performed on chunk %d:0", + nand_chunk); } else if (nspare.eccres1 < 0) { - T(YAFFS_TRACE_ERROR, - (TSTR - ("**>>mtd ecc error unfixed on chunk %d:0" - TENDSTR), nand_chunk)); + yaffs_trace(YAFFS_TRACE_ERROR, + "**>>mtd ecc error unfixed on chunk %d:0", + nand_chunk); } if (nspare.eccres2 > 0) { - T(YAFFS_TRACE_ERROR, - (TSTR - ("**>>mtd ecc error fix performed on chunk %d:1" - TENDSTR), nand_chunk)); + yaffs_trace(YAFFS_TRACE_ERROR, + "**>>mtd ecc error fix performed on chunk %d:1", + nand_chunk); } else if (nspare.eccres2 < 0) { - T(YAFFS_TRACE_ERROR, - (TSTR - ("**>>mtd ecc error unfixed on chunk %d:1" - TENDSTR), nand_chunk)); + yaffs_trace(YAFFS_TRACE_ERROR, + "**>>mtd ecc error unfixed on chunk %d:1", + nand_chunk); } if (nspare.eccres1 || nspare.eccres2) { @@ -301,11 +264,11 @@ static void yaffs_handle_rd_data_error(struct yaffs_dev *dev, int nand_chunk) int flash_block = nand_chunk / dev->param.chunks_per_block; /* Mark the block for retirement */ - yaffs_get_block_info(dev, - flash_block + dev->block_offset)->needs_retiring = - 1; - T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS, - (TSTR("**>>Block %d marked for retirement" TENDSTR), flash_block)); + yaffs_get_block_info(dev, flash_block + dev->block_offset)-> + needs_retiring = 1; + yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS, + "**>>Block %d marked for retirement", + flash_block); /* TODO: * Just do a garbage collection on the affected block @@ -316,7 +279,7 @@ static void yaffs_handle_rd_data_error(struct yaffs_dev *dev, int nand_chunk) int yaffs_tags_compat_wr(struct yaffs_dev *dev, int nand_chunk, - const u8 * data, const struct yaffs_ext_tags *ext_tags) + const u8 *data, const struct yaffs_ext_tags *ext_tags) { struct yaffs_spare spare; struct yaffs_tags tags; @@ -350,13 +313,11 @@ int yaffs_tags_compat_wr(struct yaffs_dev *dev, int yaffs_tags_compat_rd(struct yaffs_dev *dev, int nand_chunk, - u8 * data, struct yaffs_ext_tags *ext_tags) + u8 *data, struct yaffs_ext_tags *ext_tags) { - struct yaffs_spare spare; struct yaffs_tags tags; enum yaffs_ecc_result ecc_result = YAFFS_ECC_RESULT_UNKNOWN; - static struct yaffs_spare spare_ff; static int init; @@ -365,12 +326,13 @@ int yaffs_tags_compat_rd(struct yaffs_dev *dev, init = 1; } - if (yaffs_rd_chunk_nand(dev, nand_chunk, data, &spare, &ecc_result, 1)) { + if (yaffs_rd_chunk_nand(dev, nand_chunk, + data, &spare, &ecc_result, 1)) { /* ext_tags may be NULL */ if (ext_tags) { int deleted = - (yaffs_count_bits(spare.page_status) < 7) ? 1 : 0; + (hweight8(spare.page_status) < 7) ? 1 : 0; ext_tags->is_deleted = deleted; ext_tags->ecc_result = ecc_result; @@ -404,7 +366,6 @@ int yaffs_tags_compat_rd(struct yaffs_dev *dev, int yaffs_tags_compat_mark_bad(struct yaffs_dev *dev, int flash_block) { - struct yaffs_spare spare; memset(&spare, 0xff, sizeof(struct yaffs_spare)); @@ -417,15 +378,13 @@ int yaffs_tags_compat_mark_bad(struct yaffs_dev *dev, int flash_block) NULL, &spare); return YAFFS_OK; - } int yaffs_tags_compat_query_block(struct yaffs_dev *dev, int block_no, enum yaffs_block_state *state, - u32 * seq_number) + u32 *seq_number) { - struct yaffs_spare spare0, spare1; static struct yaffs_spare spare_ff; static int init; @@ -443,7 +402,7 @@ int yaffs_tags_compat_query_block(struct yaffs_dev *dev, yaffs_rd_chunk_nand(dev, block_no * dev->param.chunks_per_block + 1, NULL, &spare1, &dummy, 1); - if (yaffs_count_bits(spare0.block_status & spare1.block_status) < 7) + if (hweight8(spare0.block_status & spare1.block_status) < 7) *state = YAFFS_BLOCK_STATE_DEAD; else if (memcmp(&spare_ff, &spare0, sizeof(spare_ff)) == 0) *state = YAFFS_BLOCK_STATE_EMPTY; diff --git a/fs/yaffs2/yaffs_tagscompat.h b/fs/yaffs2/yaffs_tagscompat.h index 8cd35dcd..ae15e0bc 100644 --- a/fs/yaffs2/yaffs_tagscompat.h +++ b/fs/yaffs2/yaffs_tagscompat.h @@ -19,15 +19,15 @@ #include "yaffs_guts.h" int yaffs_tags_compat_wr(struct yaffs_dev *dev, int nand_chunk, - const u8 * data, const struct yaffs_ext_tags *tags); + const u8 *data, const struct yaffs_ext_tags *tags); int yaffs_tags_compat_rd(struct yaffs_dev *dev, int nand_chunk, - u8 * data, struct yaffs_ext_tags *tags); + u8 *data, struct yaffs_ext_tags *tags); int yaffs_tags_compat_mark_bad(struct yaffs_dev *dev, int block_no); int yaffs_tags_compat_query_block(struct yaffs_dev *dev, int block_no, enum yaffs_block_state *state, - u32 * seq_number); + u32 *seq_number); void yaffs_calc_tags_ecc(struct yaffs_tags *tags); int yaffs_check_tags_ecc(struct yaffs_tags *tags); diff --git a/fs/yaffs2/yaffs_tagsvalidity.c b/fs/yaffs2/yaffs_tagsvalidity.c index 4358d79d..582f36ac 100644 --- a/fs/yaffs2/yaffs_tagsvalidity.c +++ b/fs/yaffs2/yaffs_tagsvalidity.c @@ -23,5 +23,4 @@ void yaffs_init_tags(struct yaffs_ext_tags *tags) int yaffs_validate_tags(struct yaffs_ext_tags *tags) { return (tags->validity0 == 0xAAAAAAAA && tags->validity1 == 0x55555555); - } diff --git a/fs/yaffs2/yaffs_trace.h b/fs/yaffs2/yaffs_trace.h index 90dbefc9..6273dbf9 100644 --- a/fs/yaffs2/yaffs_trace.h +++ b/fs/yaffs2/yaffs_trace.h @@ -48,11 +48,10 @@ extern unsigned int yaffs_wr_attempts; #define YAFFS_TRACE_SYNC 0x00100000 #define YAFFS_TRACE_BACKGROUND 0x00200000 #define YAFFS_TRACE_LOCK 0x00400000 +#define YAFFS_TRACE_MOUNT 0x00800000 #define YAFFS_TRACE_ERROR 0x40000000 #define YAFFS_TRACE_BUG 0x80000000 #define YAFFS_TRACE_ALWAYS 0xF0000000 -#define T(mask, p) do { if ((mask) & (yaffs_trace_mask | YAFFS_TRACE_ALWAYS)) TOUT(p); } while (0) - #endif diff --git a/fs/yaffs2/yaffs_verify.c b/fs/yaffs2/yaffs_verify.c index fee3a1e4..1a5ed9a7 100644 --- a/fs/yaffs2/yaffs_verify.c +++ b/fs/yaffs2/yaffs_verify.c @@ -36,7 +36,7 @@ static int yaffs_skip_nand_verification(struct yaffs_dev *dev) return !(yaffs_trace_mask & (YAFFS_TRACE_VERIFY_NAND)); } -static const char *block_state_name[] = { +static const char * const block_state_name[] = { "Unknown", "Needs scanning", "Scanning", @@ -59,17 +59,17 @@ void yaffs_verify_blk(struct yaffs_dev *dev, struct yaffs_block_info *bi, int n) /* Report illegal runtime states */ if (bi->block_state >= YAFFS_NUMBER_OF_BLOCK_STATES) - T(YAFFS_TRACE_VERIFY, - (TSTR("Block %d has undefined state %d" TENDSTR), n, - bi->block_state)); + yaffs_trace(YAFFS_TRACE_VERIFY, + "Block %d has undefined state %d", + n, bi->block_state); switch (bi->block_state) { case YAFFS_BLOCK_STATE_UNKNOWN: case YAFFS_BLOCK_STATE_SCANNING: case YAFFS_BLOCK_STATE_NEEDS_SCANNING: - T(YAFFS_TRACE_VERIFY, - (TSTR("Block %d has bad run-state %s" TENDSTR), n, - block_state_name[bi->block_state])); + yaffs_trace(YAFFS_TRACE_VERIFY, + "Block %d has bad run-state %s", + n, block_state_name[bi->block_state]); } /* Check pages in use and soft deletions are legal */ @@ -81,19 +81,16 @@ void yaffs_verify_blk(struct yaffs_dev *dev, struct yaffs_block_info *bi, int n) || bi->soft_del_pages < 0 || bi->soft_del_pages > dev->param.chunks_per_block || actually_used < 0 || actually_used > dev->param.chunks_per_block) - T(YAFFS_TRACE_VERIFY, - (TSTR - ("Block %d has illegal values pages_in_used %d soft_del_pages %d" - TENDSTR), n, bi->pages_in_use, bi->soft_del_pages)); + yaffs_trace(YAFFS_TRACE_VERIFY, + "Block %d has illegal values pages_in_used %d soft_del_pages %d", + n, bi->pages_in_use, bi->soft_del_pages); /* Check chunk bitmap legal */ in_use = yaffs_count_chunk_bits(dev, n); if (in_use != bi->pages_in_use) - T(YAFFS_TRACE_VERIFY, - (TSTR - ("Block %d has inconsistent values pages_in_use %d counted chunk bits %d" - TENDSTR), n, bi->pages_in_use, in_use)); - + yaffs_trace(YAFFS_TRACE_VERIFY, + "Block %d has inconsistent values pages_in_use %d counted chunk bits %d", + n, bi->pages_in_use, in_use); } void yaffs_verify_collected_blk(struct yaffs_dev *dev, @@ -105,10 +102,9 @@ void yaffs_verify_collected_blk(struct yaffs_dev *dev, if (bi->block_state != YAFFS_BLOCK_STATE_COLLECTING && bi->block_state != YAFFS_BLOCK_STATE_EMPTY) { - T(YAFFS_TRACE_ERROR, - (TSTR - ("Block %d is in state %d after gc, should be erased" - TENDSTR), n, bi->block_state)); + yaffs_trace(YAFFS_TRACE_ERROR, + "Block %d is in state %d after gc, should be erased", + n, bi->block_state); } } @@ -133,43 +129,41 @@ void yaffs_verify_blocks(struct yaffs_dev *dev) illegal_states++; } - T(YAFFS_TRACE_VERIFY, (TSTR("" TENDSTR))); - T(YAFFS_TRACE_VERIFY, (TSTR("Block summary" TENDSTR))); + yaffs_trace(YAFFS_TRACE_VERIFY, "Block summary"); - T(YAFFS_TRACE_VERIFY, - (TSTR("%d blocks have illegal states" TENDSTR), illegal_states)); + yaffs_trace(YAFFS_TRACE_VERIFY, + "%d blocks have illegal states", + illegal_states); if (state_count[YAFFS_BLOCK_STATE_ALLOCATING] > 1) - T(YAFFS_TRACE_VERIFY, - (TSTR("Too many allocating blocks" TENDSTR))); + yaffs_trace(YAFFS_TRACE_VERIFY, + "Too many allocating blocks"); for (i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++) - T(YAFFS_TRACE_VERIFY, - (TSTR("%s %d blocks" TENDSTR), - block_state_name[i], state_count[i])); + yaffs_trace(YAFFS_TRACE_VERIFY, + "%s %d blocks", + block_state_name[i], state_count[i]); if (dev->blocks_in_checkpt != state_count[YAFFS_BLOCK_STATE_CHECKPOINT]) - T(YAFFS_TRACE_VERIFY, - (TSTR("Checkpoint block count wrong dev %d count %d" TENDSTR), - dev->blocks_in_checkpt, - state_count[YAFFS_BLOCK_STATE_CHECKPOINT])); + yaffs_trace(YAFFS_TRACE_VERIFY, + "Checkpoint block count wrong dev %d count %d", + dev->blocks_in_checkpt, + state_count[YAFFS_BLOCK_STATE_CHECKPOINT]); if (dev->n_erased_blocks != state_count[YAFFS_BLOCK_STATE_EMPTY]) - T(YAFFS_TRACE_VERIFY, - (TSTR("Erased block count wrong dev %d count %d" TENDSTR), - dev->n_erased_blocks, state_count[YAFFS_BLOCK_STATE_EMPTY])); + yaffs_trace(YAFFS_TRACE_VERIFY, + "Erased block count wrong dev %d count %d", + dev->n_erased_blocks, + state_count[YAFFS_BLOCK_STATE_EMPTY]); if (state_count[YAFFS_BLOCK_STATE_COLLECTING] > 1) - T(YAFFS_TRACE_VERIFY, - (TSTR("Too many collecting blocks %d (max is 1)" TENDSTR), - state_count[YAFFS_BLOCK_STATE_COLLECTING])); - - T(YAFFS_TRACE_VERIFY, (TSTR("" TENDSTR))); - + yaffs_trace(YAFFS_TRACE_VERIFY, + "Too many collecting blocks %d (max is 1)", + state_count[YAFFS_BLOCK_STATE_COLLECTING]); } /* - * Verify the object header. oh must be valid, but obj and tags may be NULL in which - * case those tests will not be performed. + * Verify the object header. oh must be valid, but obj and tags may be NULL in + * which case those tests will not be performed. */ void yaffs_verify_oh(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh, struct yaffs_ext_tags *tags, int parent_check) @@ -178,22 +172,22 @@ void yaffs_verify_oh(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh, return; if (!(tags && obj && oh)) { - T(YAFFS_TRACE_VERIFY, - (TSTR("Verifying object header tags %p obj %p oh %p" TENDSTR), - tags, obj, oh)); + yaffs_trace(YAFFS_TRACE_VERIFY, + "Verifying object header tags %p obj %p oh %p", + tags, obj, oh); return; } if (oh->type <= YAFFS_OBJECT_TYPE_UNKNOWN || oh->type > YAFFS_OBJECT_TYPE_MAX) - T(YAFFS_TRACE_VERIFY, - (TSTR("Obj %d header type is illegal value 0x%x" TENDSTR), - tags->obj_id, oh->type)); + yaffs_trace(YAFFS_TRACE_VERIFY, + "Obj %d header type is illegal value 0x%x", + tags->obj_id, oh->type); if (tags->obj_id != obj->obj_id) - T(YAFFS_TRACE_VERIFY, - (TSTR("Obj %d header mismatch obj_id %d" TENDSTR), - tags->obj_id, obj->obj_id)); + yaffs_trace(YAFFS_TRACE_VERIFY, + "Obj %d header mismatch obj_id %d", + tags->obj_id, obj->obj_id); /* * Check that the object's parent ids match if parent_check requested. @@ -202,28 +196,28 @@ void yaffs_verify_oh(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh, */ if (parent_check && tags->obj_id > 1 && !obj->parent) - T(YAFFS_TRACE_VERIFY, - (TSTR - ("Obj %d header mismatch parent_id %d obj->parent is NULL" - TENDSTR), tags->obj_id, oh->parent_obj_id)); + yaffs_trace(YAFFS_TRACE_VERIFY, + "Obj %d header mismatch parent_id %d obj->parent is NULL", + tags->obj_id, oh->parent_obj_id); if (parent_check && obj->parent && oh->parent_obj_id != obj->parent->obj_id && (oh->parent_obj_id != YAFFS_OBJECTID_UNLINKED || obj->parent->obj_id != YAFFS_OBJECTID_DELETED)) - T(YAFFS_TRACE_VERIFY, - (TSTR - ("Obj %d header mismatch parent_id %d parent_obj_id %d" - TENDSTR), tags->obj_id, oh->parent_obj_id, - obj->parent->obj_id)); + yaffs_trace(YAFFS_TRACE_VERIFY, + "Obj %d header mismatch parent_id %d parent_obj_id %d", + tags->obj_id, oh->parent_obj_id, + obj->parent->obj_id); if (tags->obj_id > 1 && oh->name[0] == 0) /* Null name */ - T(YAFFS_TRACE_VERIFY, - (TSTR("Obj %d header name is NULL" TENDSTR), obj->obj_id)); + yaffs_trace(YAFFS_TRACE_VERIFY, + "Obj %d header name is NULL", + obj->obj_id); - if (tags->obj_id > 1 && ((u8) (oh->name[0])) == 0xff) /* Trashed name */ - T(YAFFS_TRACE_VERIFY, - (TSTR("Obj %d header name is 0xFF" TENDSTR), obj->obj_id)); + if (tags->obj_id > 1 && ((u8) (oh->name[0])) == 0xff) /* Junk name */ + yaffs_trace(YAFFS_TRACE_VERIFY, + "Obj %d header name is 0xFF", + obj->obj_id); } void yaffs_verify_file(struct yaffs_obj *obj) @@ -273,16 +267,13 @@ void yaffs_verify_file(struct yaffs_obj *obj) if (tn) { u32 the_chunk = yaffs_get_group_base(dev, tn, i); if (the_chunk > 0) { - /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),obj_id,i,the_chunk)); */ yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL, &tags); - if (tags.obj_id != obj_id || tags.chunk_id != i) { - T(~0, - (TSTR - ("Object %d chunk_id %d NAND mismatch chunk %d tags (%d:%d)" - TENDSTR), obj_id, i, the_chunk, - tags.obj_id, tags.chunk_id)); - } + if (tags.obj_id != obj_id || tags.chunk_id != i) + yaffs_trace(YAFFS_TRACE_VERIFY, + "Object %d chunk_id %d NAND mismatch chunk %d tags (%d:%d)", + obj_id, i, the_chunk, + tags.obj_id, tags.chunk_id); } } } @@ -313,10 +304,8 @@ void yaffs_verify_special(struct yaffs_obj *obj) void yaffs_verify_obj(struct yaffs_obj *obj) { struct yaffs_dev *dev; - u32 chunk_min; u32 chunk_max; - u32 chunk_id_ok; u32 chunk_in_range; u32 chunk_wrongly_deleted; @@ -348,13 +337,12 @@ void yaffs_verify_obj(struct yaffs_obj *obj) obj->hdr_chunk % dev->param.chunks_per_block); chunk_wrongly_deleted = chunk_in_range && !chunk_valid; - if (!obj->fake && (!chunk_id_ok || chunk_wrongly_deleted)) { - T(YAFFS_TRACE_VERIFY, - (TSTR("Obj %d has chunk_id %d %s %s" TENDSTR), - obj->obj_id, obj->hdr_chunk, - chunk_id_ok ? "" : ",out of range", - chunk_wrongly_deleted ? ",marked as deleted" : "")); - } + if (!obj->fake && (!chunk_id_ok || chunk_wrongly_deleted)) + yaffs_trace(YAFFS_TRACE_VERIFY, + "Obj %d has chunk_id %d %s %s", + obj->obj_id, obj->hdr_chunk, + chunk_id_ok ? "" : ",out of range", + chunk_wrongly_deleted ? ",marked as deleted" : ""); if (chunk_valid && !yaffs_skip_nand_verification(dev)) { struct yaffs_ext_tags tags; @@ -372,18 +360,17 @@ void yaffs_verify_obj(struct yaffs_obj *obj) /* Verify it has a parent */ if (obj && !obj->fake && (!obj->parent || obj->parent->my_dev != dev)) { - T(YAFFS_TRACE_VERIFY, - (TSTR - ("Obj %d has parent pointer %p which does not look like an object" - TENDSTR), obj->obj_id, obj->parent)); + yaffs_trace(YAFFS_TRACE_VERIFY, + "Obj %d has parent pointer %p which does not look like an object", + obj->obj_id, obj->parent); } /* Verify parent is a directory */ if (obj->parent && obj->parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) { - T(YAFFS_TRACE_VERIFY, - (TSTR("Obj %d's parent is not a directory (type %d)" TENDSTR), - obj->obj_id, obj->parent->variant_type)); + yaffs_trace(YAFFS_TRACE_VERIFY, + "Obj %d's parent is not a directory (type %d)", + obj->obj_id, obj->parent->variant_type); } switch (obj->variant_type) { @@ -404,9 +391,9 @@ void yaffs_verify_obj(struct yaffs_obj *obj) break; case YAFFS_OBJECT_TYPE_UNKNOWN: default: - T(YAFFS_TRACE_VERIFY, - (TSTR("Obj %d has illegaltype %d" TENDSTR), - obj->obj_id, obj->variant_type)); + yaffs_trace(YAFFS_TRACE_VERIFY, + "Obj %d has illegaltype %d", + obj->obj_id, obj->variant_type); break; } } @@ -424,11 +411,8 @@ void yaffs_verify_objects(struct yaffs_dev *dev) for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) { list_for_each(lh, &dev->obj_bucket[i].list) { - if (lh) { - obj = - list_entry(lh, struct yaffs_obj, hash_link); - yaffs_verify_obj(obj); - } + obj = list_entry(lh, struct yaffs_obj, hash_link); + yaffs_verify_obj(obj); } } } @@ -437,11 +421,10 @@ void yaffs_verify_obj_in_dir(struct yaffs_obj *obj) { struct list_head *lh; struct yaffs_obj *list_obj; - int count = 0; if (!obj) { - T(YAFFS_TRACE_ALWAYS, (TSTR("No object to verify" TENDSTR))); + yaffs_trace(YAFFS_TRACE_ALWAYS, "No object to verify"); YBUG(); return; } @@ -450,32 +433,29 @@ void yaffs_verify_obj_in_dir(struct yaffs_obj *obj) return; if (!obj->parent) { - T(YAFFS_TRACE_ALWAYS, - (TSTR("Object does not have parent" TENDSTR))); + yaffs_trace(YAFFS_TRACE_ALWAYS, "Object does not have parent"); YBUG(); return; } if (obj->parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) { - T(YAFFS_TRACE_ALWAYS, - (TSTR("Parent is not directory" TENDSTR))); + yaffs_trace(YAFFS_TRACE_ALWAYS, "Parent is not directory"); YBUG(); } /* Iterate through the objects in each hash entry */ list_for_each(lh, &obj->parent->variant.dir_variant.children) { - if (lh) { - list_obj = list_entry(lh, struct yaffs_obj, siblings); - yaffs_verify_obj(list_obj); - if (obj == list_obj) - count++; - } + list_obj = list_entry(lh, struct yaffs_obj, siblings); + yaffs_verify_obj(list_obj); + if (obj == list_obj) + count++; } if (count != 1) { - T(YAFFS_TRACE_ALWAYS, - (TSTR("Object in directory %d times" TENDSTR), count)); + yaffs_trace(YAFFS_TRACE_ALWAYS, + "Object in directory %d times", + count); YBUG(); } } @@ -494,26 +474,23 @@ void yaffs_verify_dir(struct yaffs_obj *directory) return; if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) { - T(YAFFS_TRACE_ALWAYS, - (TSTR("Directory has wrong type: %d" TENDSTR), - directory->variant_type)); + yaffs_trace(YAFFS_TRACE_ALWAYS, + "Directory has wrong type: %d", + directory->variant_type); YBUG(); } /* Iterate through the objects in each hash entry */ list_for_each(lh, &directory->variant.dir_variant.children) { - if (lh) { - list_obj = list_entry(lh, struct yaffs_obj, siblings); - if (list_obj->parent != directory) { - T(YAFFS_TRACE_ALWAYS, - (TSTR - ("Object in directory list has wrong parent %p" - TENDSTR), list_obj->parent)); - YBUG(); - } - yaffs_verify_obj_in_dir(list_obj); + list_obj = list_entry(lh, struct yaffs_obj, siblings); + if (list_obj->parent != directory) { + yaffs_trace(YAFFS_TRACE_ALWAYS, + "Object in directory list has wrong parent %p", + list_obj->parent); + YBUG(); } + yaffs_verify_obj_in_dir(list_obj); } } @@ -532,9 +509,9 @@ void yaffs_verify_free_chunks(struct yaffs_dev *dev) difference = dev->n_free_chunks - counted; if (difference) { - T(YAFFS_TRACE_ALWAYS, - (TSTR("Freechunks verification failure %d %d %d" TENDSTR), - dev->n_free_chunks, counted, difference)); + yaffs_trace(YAFFS_TRACE_ALWAYS, + "Freechunks verification failure %d %d %d", + dev->n_free_chunks, counted, difference); yaffs_free_verification_failures++; } } @@ -544,3 +521,4 @@ int yaffs_verify_file_sane(struct yaffs_obj *in) in = in; return YAFFS_OK; } + diff --git a/fs/yaffs2/yaffs_vfs.c b/fs/yaffs2/yaffs_vfs.c index 9e4e31a8..0d8dd51a 100644 --- a/fs/yaffs2/yaffs_vfs.c +++ b/fs/yaffs2/yaffs_vfs.c @@ -39,24 +39,24 @@ #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10)) #define YAFFS_COMPILE_BACKGROUND -#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6, 23)) +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23)) #define YAFFS_COMPILE_FREEZER #endif #endif -#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)) +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)) #define YAFFS_COMPILE_EXPORTFS #endif -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35)) +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 35)) #define YAFFS_USE_SETATTR_COPY #define YAFFS_USE_TRUNCATE_SETSIZE #endif -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35)) +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 35)) #define YAFFS_HAS_EVICT_INODE #endif -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13)) +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13)) #define YAFFS_NEW_FOLLOW_LINK 1 #else #define YAFFS_NEW_FOLLOW_LINK 0 @@ -126,7 +126,7 @@ #define YPROC_ROOT NULL #endif -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)) +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)) #define Y_INIT_TIMER(a) init_timer(a) #else #define Y_INIT_TIMER(a) init_timer_on_stack(a) @@ -210,7 +210,8 @@ static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino); #define yaffs_inode_to_obj_lv(iptr) ((iptr)->u.generic_ip) #endif -#define yaffs_inode_to_obj(iptr) ((struct yaffs_obj *)(yaffs_inode_to_obj_lv(iptr))) +#define yaffs_inode_to_obj(iptr) \ + ((struct yaffs_obj *)(yaffs_inode_to_obj_lv(iptr))) #define yaffs_dentry_to_obj(dptr) yaffs_inode_to_obj((dptr)->d_inode) #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0)) @@ -221,12 +222,12 @@ static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino); #define update_dir_time(dir) do {\ (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \ - } while(0) + } while (0) static void yaffs_put_super(struct super_block *sb); static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, - loff_t * pos); + loff_t *pos); static ssize_t yaffs_hold_space(struct file *f); static void yaffs_release_space(struct file *f); @@ -307,12 +308,12 @@ static int yaffs_writepage(struct page *page); #endif #ifdef CONFIG_YAFFS_XATTR -int yaffs_setxattr(struct dentry *dentry, const char *name, +static int yaffs_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags); -ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, void *buff, - size_t size); -int yaffs_removexattr(struct dentry *dentry, const char *name); -ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size); +static ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, + void *buff, size_t size); +static int yaffs_removexattr(struct dentry *dentry, const char *name); +static ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size); #endif #if (YAFFS_USE_WRITE_BEGIN_END != 0) @@ -341,8 +342,6 @@ static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd); static void yaffs_touch_super(struct yaffs_dev *dev); -static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin); - static int yaffs_vfs_setattr(struct inode *, struct iattr *); static struct address_space_operations yaffs_file_address_operations = { @@ -398,7 +397,7 @@ static const struct file_operations yaffs_file_operations = { }; #endif -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)) +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)) static void zero_user_segment(struct page *page, unsigned start, unsigned end) { void *kaddr = kmap_atomic(page, KM_USER0); @@ -456,7 +455,7 @@ static const struct file_operations yaffs_dir_operations = { .read = generic_read_dir, .readdir = yaffs_readdir, .fsync = yaffs_sync_object, - .llseek = yaffs_dir_llseek, + .llseek = generic_file_llseek, }; static const struct super_operations yaffs_super_ops = { @@ -481,7 +480,7 @@ static const struct super_operations yaffs_super_ops = { static int yaffs_vfs_setattr(struct inode *inode, struct iattr *attr) { -#ifdef YAFFS_USE_SETATTR_COPY +#ifdef YAFFS_USE_SETATTR_COPY setattr_copy(inode, attr); return 0; #else @@ -492,7 +491,7 @@ static int yaffs_vfs_setattr(struct inode *inode, struct iattr *attr) static int yaffs_vfs_setsize(struct inode *inode, loff_t newsize) { -#ifdef YAFFS_USE_TRUNCATE_SETSIZE +#ifdef YAFFS_USE_TRUNCATE_SETSIZE truncate_setsize(inode, newsize); return 0; #else @@ -509,14 +508,14 @@ static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev) static void yaffs_gross_lock(struct yaffs_dev *dev) { - T(YAFFS_TRACE_LOCK, (TSTR("yaffs locking %p\n"), current)); + yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locking %p", current); mutex_lock(&(yaffs_dev_to_lc(dev)->gross_lock)); - T(YAFFS_TRACE_LOCK, (TSTR("yaffs locked %p\n"), current)); + yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locked %p", current); } static void yaffs_gross_unlock(struct yaffs_dev *dev) { - T(YAFFS_TRACE_LOCK, (TSTR("yaffs unlocking %p\n"), current)); + yaffs_trace(YAFFS_TRACE_LOCK, "yaffs unlocking %p", current); mutex_unlock(&(yaffs_dev_to_lc(dev)->gross_lock)); } @@ -625,7 +624,7 @@ static struct yaffs_search_context *yaffs_new_search(struct yaffs_obj *dir) { struct yaffs_dev *dev = dir->my_dev; struct yaffs_search_context *sc = - YMALLOC(sizeof(struct yaffs_search_context)); + kmalloc(sizeof(struct yaffs_search_context), GFP_NOFS); if (sc) { sc->dir_obj = dir; sc->dev = dev; @@ -648,7 +647,7 @@ static void yaffs_search_end(struct yaffs_search_context *sc) { if (sc) { list_del(&sc->others); - YFREE(sc); + kfree(sc); } } @@ -694,11 +693,9 @@ static void yaffs_remove_obj_callback(struct yaffs_obj *obj) * the search context to the next object to prevent a hanging pointer. */ list_for_each(i, search_contexts) { - if (i) { - sc = list_entry(i, struct yaffs_search_context, others); - if (sc->next_return == obj) - yaffs_search_advance(sc); - } + sc = list_entry(i, struct yaffs_search_context, others); + if (sc->next_return == obj) + yaffs_search_advance(sc); } } @@ -795,9 +792,8 @@ static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry) if (current != yaffs_dev_to_lc(dev)->readdir_process) yaffs_gross_lock(dev); - T(YAFFS_TRACE_OS, - (TSTR("yaffs_lookup for %d:%s\n"), - yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name)); + yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup for %d:%s", + yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name); obj = yaffs_find_by_name(yaffs_inode_to_obj(dir), dentry->d_name.name); @@ -808,26 +804,12 @@ static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry) yaffs_gross_unlock(dev); if (obj) { - T(YAFFS_TRACE_OS, - (TSTR("yaffs_lookup found %d\n"), obj->obj_id)); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_lookup found %d", obj->obj_id); inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj); - - if (inode) { - T(YAFFS_TRACE_OS, (TSTR("yaffs_loookup dentry \n"))); -/* #if 0 asserted by NCB for 2.5/6 compatability - falls through to - * d_add even if NULL inode */ -#if 0 - /*dget(dentry); // try to solve directory bug */ - d_add(dentry, inode); - - /* return dentry; */ - return NULL; -#endif - } - } else { - T(YAFFS_TRACE_OS, (TSTR("yaffs_lookup not found\n"))); + yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup not found"); } @@ -845,9 +827,9 @@ static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry) */ static void yaffs_put_inode(struct inode *inode) { - T(YAFFS_TRACE_OS, - (TSTR("yaffs_put_inode: ino %d, count %d\n"), (int)inode->i_ino, - atomic_read(&inode->i_count))); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_put_inode: ino %d, count %d"), + (int)inode->i_ino, atomic_read(&inode->i_count); } #endif @@ -880,10 +862,10 @@ static void yaffs_evict_inode(struct inode *inode) obj = yaffs_inode_to_obj(inode); - T(YAFFS_TRACE_OS, - (TSTR("yaffs_evict_inode: ino %d, count %d %s\n"), (int)inode->i_ino, - atomic_read(&inode->i_count), - obj ? "object exists" : "null object")); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_evict_inode: ino %d, count %d %s", + (int)inode->i_ino, atomic_read(&inode->i_count), + obj ? "object exists" : "null object"); if (!inode->i_nlink && !is_bad_inode(inode)) deleteme = 1; @@ -902,7 +884,6 @@ static void yaffs_evict_inode(struct inode *inode) yaffs_unstitch_obj(inode, obj); yaffs_gross_unlock(dev); } - } #else @@ -920,10 +901,10 @@ static void yaffs_clear_inode(struct inode *inode) obj = yaffs_inode_to_obj(inode); - T(YAFFS_TRACE_OS, - (TSTR("yaffs_clear_inode: ino %d, count %d %s\n"), (int)inode->i_ino, - atomic_read(&inode->i_count), - obj ? "object exists" : "null object")); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_clear_inode: ino %d, count %d %s", + (int)inode->i_ino, atomic_read(&inode->i_count), + obj ? "object exists" : "null object"); if (obj) { dev = obj->my_dev; @@ -944,10 +925,10 @@ static void yaffs_delete_inode(struct inode *inode) struct yaffs_obj *obj = yaffs_inode_to_obj(inode); struct yaffs_dev *dev; - T(YAFFS_TRACE_OS, - (TSTR("yaffs_delete_inode: ino %d, count %d %s\n"), (int)inode->i_ino, - atomic_read(&inode->i_count), - obj ? "object exists" : "null object")); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_delete_inode: ino %d, count %d %s", + (int)inode->i_ino, atomic_read(&inode->i_count), + obj ? "object exists" : "null object"); if (obj) { dev = obj->my_dev; @@ -972,9 +953,10 @@ static int yaffs_file_flush(struct file *file) struct yaffs_dev *dev = obj->my_dev; - T(YAFFS_TRACE_OS, - (TSTR("yaffs_file_flush object %d (%s)\n"), obj->obj_id, - obj->dirty ? "dirty" : "clean")); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_file_flush object %d (%s)", + obj->obj_id, + obj->dirty ? "dirty" : "clean"); yaffs_gross_lock(dev); @@ -995,10 +977,10 @@ static int yaffs_readpage_nolock(struct file *f, struct page *pg) struct yaffs_dev *dev; - T(YAFFS_TRACE_OS, - (TSTR("yaffs_readpage_nolock at %08x, size %08x\n"), - (unsigned)(pg->index << PAGE_CACHE_SHIFT), - (unsigned)PAGE_CACHE_SIZE)); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_readpage_nolock at %08x, size %08x", + (unsigned)(pg->index << PAGE_CACHE_SHIFT), + (unsigned)PAGE_CACHE_SIZE); obj = yaffs_dentry_to_obj(f->f_dentry); @@ -1035,7 +1017,7 @@ static int yaffs_readpage_nolock(struct file *f, struct page *pg) flush_dcache_page(pg); kunmap(pg); - T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage_nolock done\n"))); + yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage_nolock done"); return ret; } @@ -1050,9 +1032,9 @@ static int yaffs_readpage(struct file *f, struct page *pg) { int ret; - T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage\n"))); + yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage"); ret = yaffs_readpage_unlock(f, pg); - T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage done\n"))); + yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage done"); return ret; } @@ -1089,13 +1071,12 @@ static int yaffs_writepage(struct page *page) n_bytes = i_size & (PAGE_CACHE_SIZE - 1); if (page->index > end_index || !n_bytes) { - T(YAFFS_TRACE_OS, - (TSTR - ("yaffs_writepage at %08x, inode size = %08x!!!\n"), - (unsigned)(page->index << PAGE_CACHE_SHIFT), - (unsigned)inode->i_size)); - T(YAFFS_TRACE_OS, - (TSTR(" -> don't care!!\n"))); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_writepage at %08x, inode size = %08x!!", + (unsigned)(page->index << PAGE_CACHE_SHIFT), + (unsigned)inode->i_size); + yaffs_trace(YAFFS_TRACE_OS, + " -> don't care!!"); zero_user_segment(page, 0, PAGE_CACHE_SIZE); set_page_writeback(page); @@ -1116,21 +1097,21 @@ static int yaffs_writepage(struct page *page) dev = obj->my_dev; yaffs_gross_lock(dev); - T(YAFFS_TRACE_OS, - (TSTR("yaffs_writepage at %08x, size %08x\n"), - (unsigned)(page->index << PAGE_CACHE_SHIFT), n_bytes)); - T(YAFFS_TRACE_OS, - (TSTR("writepag0: obj = %05x, ino = %05x\n"), - (int)obj->variant.file_variant.file_size, (int)inode->i_size)); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_writepage at %08x, size %08x", + (unsigned)(page->index << PAGE_CACHE_SHIFT), n_bytes); + yaffs_trace(YAFFS_TRACE_OS, + "writepag0: obj = %05x, ino = %05x", + (int)obj->variant.file_variant.file_size, (int)inode->i_size); n_written = yaffs_wr_file(obj, buffer, page->index << PAGE_CACHE_SHIFT, n_bytes, 0); yaffs_touch_super(dev); - T(YAFFS_TRACE_OS, - (TSTR("writepag1: obj = %05x, ino = %05x\n"), - (int)obj->variant.file_variant.file_size, (int)inode->i_size)); + yaffs_trace(YAFFS_TRACE_OS, + "writepag1: obj = %05x, ino = %05x", + (int)obj->variant.file_variant.file_size, (int)inode->i_size); yaffs_gross_unlock(dev); @@ -1166,9 +1147,9 @@ static int yaffs_write_begin(struct file *filp, struct address_space *mapping, ret = -ENOMEM; goto out; } - T(YAFFS_TRACE_OS, - (TSTR("start yaffs_write_begin index %d(%x) uptodate %d\n"), - (int)index, (int)index, Page_Uptodate(pg) ? 1 : 0)); + yaffs_trace(YAFFS_TRACE_OS, + "start yaffs_write_begin index %d(%x) uptodate %d", + (int)index, (int)index, Page_Uptodate(pg) ? 1 : 0); /* Get fs space */ space_held = yaffs_hold_space(filp); @@ -1187,13 +1168,13 @@ static int yaffs_write_begin(struct file *filp, struct address_space *mapping, goto out; /* Happy path return */ - T(YAFFS_TRACE_OS, (TSTR("end yaffs_write_begin - ok\n"))); + yaffs_trace(YAFFS_TRACE_OS, "end yaffs_write_begin - ok"); return 0; out: - T(YAFFS_TRACE_OS, - (TSTR("end yaffs_write_begin fail returning %d\n"), ret)); + yaffs_trace(YAFFS_TRACE_OS, + "end yaffs_write_begin fail returning %d", ret); if (space_held) yaffs_release_space(filp); if (pg) { @@ -1208,7 +1189,7 @@ out: static int yaffs_prepare_write(struct file *f, struct page *pg, unsigned offset, unsigned to) { - T(YAFFS_TRACE_OS, (TSTR("yaffs_prepair_write\n"))); + yaffs_trace(YAFFS_TRACE_OS, "yaffs_prepair_write"); if (!Page_Uptodate(pg)) return yaffs_readpage_nolock(f, pg); @@ -1228,19 +1209,17 @@ static int yaffs_write_end(struct file *filp, struct address_space *mapping, kva = kmap(pg); addr = kva + offset_into_page; - T(YAFFS_TRACE_OS, - ("yaffs_write_end addr %p pos %x n_bytes %d\n", - addr, (unsigned)pos, copied)); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_write_end addr %p pos %x n_bytes %d", + addr, (unsigned)pos, copied); ret = yaffs_file_write(filp, addr, copied, &pos); if (ret != copied) { - T(YAFFS_TRACE_OS, - (TSTR("yaffs_write_end not same size ret %d copied %d\n"), - ret, copied)); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_write_end not same size ret %d copied %d", + ret, copied); SetPageError(pg); - } else { - /* Nothing */ } kunmap(pg); @@ -1269,27 +1248,23 @@ static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset, saddr = (unsigned)addr; - T(YAFFS_TRACE_OS, - (TSTR("yaffs_commit_write addr %x pos %x n_bytes %d\n"), - saddr, spos, n_bytes)); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_commit_write addr %x pos %x n_bytes %d", + saddr, spos, n_bytes); n_written = yaffs_file_write(f, addr, n_bytes, &pos); if (n_written != n_bytes) { - T(YAFFS_TRACE_OS, - (TSTR - ("yaffs_commit_write not same size n_written %d n_bytes %d\n"), - n_written, n_bytes)); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_commit_write not same size n_written %d n_bytes %d", + n_written, n_bytes); SetPageError(pg); - } else { - /* Nothing */ } - kunmap(pg); - T(YAFFS_TRACE_OS, - (TSTR("yaffs_commit_write returning %d\n"), - n_written == n_bytes ? 0 : n_written)); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_commit_write returning %d", + n_written == n_bytes ? 0 : n_written); return n_written == n_bytes ? 0 : n_written; } @@ -1361,11 +1336,10 @@ static void yaffs_fill_inode_from_obj(struct inode *inode, inode->i_nlink = yaffs_get_obj_link_count(obj); - T(YAFFS_TRACE_OS, - (TSTR - ("yaffs_fill_inode mode %x uid %d gid %d size %d count %d\n"), - inode->i_mode, inode->i_uid, inode->i_gid, - (int)inode->i_size, atomic_read(&inode->i_count))); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_fill_inode mode %x uid %d gid %d size %d count %d", + inode->i_mode, inode->i_uid, inode->i_gid, + (int)inode->i_size, atomic_read(&inode->i_count)); switch (obj->yst_mode & S_IFMT) { default: /* fifo, device or socket */ @@ -1397,8 +1371,8 @@ static void yaffs_fill_inode_from_obj(struct inode *inode, obj->my_inode = inode; } else { - T(YAFFS_TRACE_OS, - (TSTR("yaffs_fill_inode invalid parameters\n"))); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_fill_inode invalid parameters"); } } @@ -1409,21 +1383,21 @@ struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev, struct inode *inode; if (!sb) { - T(YAFFS_TRACE_OS, - (TSTR("yaffs_get_inode for NULL super_block!!\n"))); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_get_inode for NULL super_block!!"); return NULL; } if (!obj) { - T(YAFFS_TRACE_OS, - (TSTR("yaffs_get_inode for NULL object!!\n"))); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_get_inode for NULL object!!"); return NULL; } - T(YAFFS_TRACE_OS, - (TSTR("yaffs_get_inode for object %d\n"), obj->obj_id)); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_get_inode for object %d", obj->obj_id); inode = Y_IGET(sb, obj->obj_id); if (IS_ERR(inode)) @@ -1446,6 +1420,12 @@ static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, obj = yaffs_dentry_to_obj(f->f_dentry); + if (!obj) { + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_file_write: hey obj is null!"); + return -EINVAL; + } + dev = obj->my_dev; yaffs_gross_lock(dev); @@ -1457,22 +1437,17 @@ static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, else ipos = *pos; - if (!obj) - T(YAFFS_TRACE_OS, - (TSTR("yaffs_file_write: hey obj is null!\n"))); - else - T(YAFFS_TRACE_OS, - (TSTR("yaffs_file_write about to write writing %u(%x) bytes" - "to object %d at %d(%x)\n"), - (unsigned)n, (unsigned)n, obj->obj_id, ipos, ipos)); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_file_write about to write writing %u(%x) bytes to object %d at %d(%x)", + (unsigned)n, (unsigned)n, obj->obj_id, ipos, ipos); n_written = yaffs_wr_file(obj, buf, ipos, n, 0); yaffs_touch_super(dev); - T(YAFFS_TRACE_OS, - (TSTR("yaffs_file_write: %d(%x) bytes written\n"), - (unsigned)n, (unsigned)n)); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_file_write: %d(%x) bytes written", + (unsigned)n, (unsigned)n); if (n_written > 0) { ipos += n_written; @@ -1481,9 +1456,9 @@ static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, inode->i_size = ipos; inode->i_blocks = (ipos + 511) >> 9; - T(YAFFS_TRACE_OS, - (TSTR("yaffs_file_write size updated to %d bytes, " - "%d blocks\n"), ipos, (int)(inode->i_blocks))); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_file_write size updated to %d bytes, %d blocks", + ipos, (int)(inode->i_blocks)); } } @@ -1529,31 +1504,6 @@ static void yaffs_release_space(struct file *f) yaffs_gross_unlock(dev); } -static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin) -{ - long long retval; - - lock_kernel(); - - switch (origin) { - case 2: - offset += i_size_read(file->f_path.dentry->d_inode); - break; - case 1: - offset += file->f_pos; - } - retval = -EINVAL; - - if (offset >= 0) { - if (offset != file->f_pos) - file->f_pos = offset; - - retval = offset; - } - unlock_kernel(); - return retval; -} - static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir) { struct yaffs_obj *obj; @@ -1581,13 +1531,13 @@ static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir) goto out; } - T(YAFFS_TRACE_OS, - (TSTR("yaffs_readdir: starting at %d\n"), (int)offset)); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_readdir: starting at %d", (int)offset); if (offset == 0) { - T(YAFFS_TRACE_OS, - (TSTR("yaffs_readdir: entry . ino %d \n"), - (int)inode->i_ino)); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_readdir: entry . ino %d", + (int)inode->i_ino); yaffs_gross_unlock(dev); if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0) { yaffs_gross_lock(dev); @@ -1598,9 +1548,9 @@ static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir) f->f_pos++; } if (offset == 1) { - T(YAFFS_TRACE_OS, - (TSTR("yaffs_readdir: entry .. ino %d \n"), - (int)f->f_dentry->d_parent->d_inode->i_ino)); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_readdir: entry .. ino %d", + (int)f->f_dentry->d_parent->d_inode->i_ino); yaffs_gross_unlock(dev); if (filldir(dirent, "..", 2, offset, f->f_dentry->d_parent->d_inode->i_ino, @@ -1631,9 +1581,9 @@ static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir) int this_type = yaffs_get_obj_type(l); yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1); - T(YAFFS_TRACE_OS, - (TSTR("yaffs_readdir: %s inode %d\n"), - name, yaffs_get_obj_inode(l))); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_readdir: %s inode %d", + name, yaffs_get_obj_inode(l)); yaffs_gross_unlock(dev); @@ -1695,18 +1645,18 @@ static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, mode |= S_ISGID; if (parent) { - T(YAFFS_TRACE_OS, - (TSTR("yaffs_mknod: parent object %d type %d\n"), - parent->obj_id, parent->variant_type)); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_mknod: parent object %d type %d", + parent->obj_id, parent->variant_type); } else { - T(YAFFS_TRACE_OS, - (TSTR("yaffs_mknod: could not get parent object\n"))); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_mknod: could not get parent object"); return -EPERM; } - T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making oject for %s, " - "mode %x dev %x\n"), - dentry->d_name.name, mode, rdev)); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_mknod: making oject for %s, mode %x dev %x", + dentry->d_name.name, mode, rdev); dev = parent->my_dev; @@ -1715,7 +1665,7 @@ static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, switch (mode & S_IFMT) { default: /* Special (socket, fifo, device...) */ - T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making special\n"))); + yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making special"); #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0)) obj = yaffs_create_special(parent, dentry->d_name.name, mode, uid, @@ -1727,17 +1677,17 @@ static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, #endif break; case S_IFREG: /* file */ - T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making file\n"))); + yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making file"); obj = yaffs_create_file(parent, dentry->d_name.name, mode, uid, gid); break; case S_IFDIR: /* directory */ - T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making directory\n"))); + yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making directory"); obj = yaffs_create_dir(parent, dentry->d_name.name, mode, uid, gid); break; case S_IFLNK: /* symlink */ - T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making symlink\n"))); + yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making symlink"); obj = NULL; /* Do we ever get here? */ break; } @@ -1749,13 +1699,13 @@ static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj); d_instantiate(dentry, inode); update_dir_time(dir); - T(YAFFS_TRACE_OS, - (TSTR("yaffs_mknod created object %d count = %d\n"), - obj->obj_id, atomic_read(&inode->i_count))); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_mknod created object %d count = %d", + obj->obj_id, atomic_read(&inode->i_count)); error = 0; yaffs_fill_inode_from_obj(dir, parent); } else { - T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod failed making object\n"))); + yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod failed making object"); error = -ENOMEM; } @@ -1765,7 +1715,7 @@ static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode) { int ret_val; - T(YAFFS_TRACE_OS, (TSTR("yaffs_mkdir\n"))); + yaffs_trace(YAFFS_TRACE_OS, "yaffs_mkdir"); ret_val = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0); return ret_val; } @@ -1777,7 +1727,7 @@ static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode, static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode) #endif { - T(YAFFS_TRACE_OS, (TSTR("yaffs_create\n"))); + yaffs_trace(YAFFS_TRACE_OS, "yaffs_create"); return yaffs_mknod(dir, dentry, mode | S_IFREG, 0); } @@ -1788,9 +1738,8 @@ static int yaffs_unlink(struct inode *dir, struct dentry *dentry) struct yaffs_dev *dev; struct yaffs_obj *obj; - T(YAFFS_TRACE_OS, - (TSTR("yaffs_unlink %d:%s\n"), - (int)(dir->i_ino), dentry->d_name.name)); + yaffs_trace(YAFFS_TRACE_OS, "yaffs_unlink %d:%s", + (int)(dir->i_ino), dentry->d_name.name); obj = yaffs_inode_to_obj(dir); dev = obj->my_dev; @@ -1821,7 +1770,7 @@ static int yaffs_link(struct dentry *old_dentry, struct inode *dir, struct yaffs_obj *link = NULL; struct yaffs_dev *dev; - T(YAFFS_TRACE_OS, (TSTR("yaffs_link\n"))); + yaffs_trace(YAFFS_TRACE_OS, "yaffs_link"); obj = yaffs_inode_to_obj(inode); dev = obj->my_dev; @@ -1837,10 +1786,10 @@ static int yaffs_link(struct dentry *old_dentry, struct inode *dir, old_dentry->d_inode->i_nlink = yaffs_get_obj_link_count(obj); d_instantiate(dentry, old_dentry->d_inode); atomic_inc(&old_dentry->d_inode->i_count); - T(YAFFS_TRACE_OS, - (TSTR("yaffs_link link count %d i_count %d\n"), - old_dentry->d_inode->i_nlink, - atomic_read(&old_dentry->d_inode->i_count))); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_link link count %d i_count %d", + old_dentry->d_inode->i_nlink, + atomic_read(&old_dentry->d_inode->i_count)); } yaffs_gross_unlock(dev); @@ -1862,7 +1811,7 @@ static int yaffs_symlink(struct inode *dir, struct dentry *dentry, gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid; - T(YAFFS_TRACE_OS, (TSTR("yaffs_symlink\n"))); + yaffs_trace(YAFFS_TRACE_OS, "yaffs_symlink"); dev = yaffs_inode_to_obj(dir)->my_dev; yaffs_gross_lock(dev); @@ -1876,10 +1825,10 @@ static int yaffs_symlink(struct inode *dir, struct dentry *dentry, inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj); d_instantiate(dentry, inode); update_dir_time(dir); - T(YAFFS_TRACE_OS, (TSTR("symlink created OK\n"))); + yaffs_trace(YAFFS_TRACE_OS, "symlink created OK"); return 0; } else { - T(YAFFS_TRACE_OS, (TSTR("symlink not created\n"))); + yaffs_trace(YAFFS_TRACE_OS, "symlink not created"); } return -ENOMEM; @@ -1903,7 +1852,8 @@ static int yaffs_sync_object(struct file *file, struct dentry *dentry, dev = obj->my_dev; - T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, (TSTR("yaffs_sync_object\n"))); + yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, + "yaffs_sync_object"); yaffs_gross_lock(dev); yaffs_flush_file(obj, 1, datasync); yaffs_gross_unlock(dev); @@ -1922,7 +1872,7 @@ static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry, int ret_val = YAFFS_FAIL; struct yaffs_obj *target; - T(YAFFS_TRACE_OS, (TSTR("yaffs_rename\n"))); + yaffs_trace(YAFFS_TRACE_OS, "yaffs_rename"); dev = yaffs_inode_to_obj(old_dir)->my_dev; yaffs_gross_lock(dev); @@ -1934,12 +1884,12 @@ static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry, if (target && target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY && !list_empty(&target->variant.dir_variant.children)) { - T(YAFFS_TRACE_OS, (TSTR("target is non-empty dir\n"))); + yaffs_trace(YAFFS_TRACE_OS, "target is non-empty dir"); ret_val = YAFFS_FAIL; } else { /* Now does unlinking internally using shadowing mechanism */ - T(YAFFS_TRACE_OS, (TSTR("calling yaffs_rename_obj\n"))); + yaffs_trace(YAFFS_TRACE_OS, "calling yaffs_rename_obj"); ret_val = yaffs_rename_obj(yaffs_inode_to_obj(old_dir), old_dentry->d_name.name, @@ -1969,9 +1919,9 @@ static int yaffs_setattr(struct dentry *dentry, struct iattr *attr) int error = 0; struct yaffs_dev *dev; - T(YAFFS_TRACE_OS, - (TSTR("yaffs_setattr of object %d\n"), - yaffs_inode_to_obj(inode)->obj_id)); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_setattr of object %d", + yaffs_inode_to_obj(inode)->obj_id); /* Fail if a requested resize >= 2GB */ if (attr->ia_valid & ATTR_SIZE && (attr->ia_size >> 31)) @@ -1983,7 +1933,7 @@ static int yaffs_setattr(struct dentry *dentry, struct iattr *attr) int result; if (!error) { error = yaffs_vfs_setattr(inode, attr); - T(YAFFS_TRACE_OS, (TSTR("inode_setattr called\n"))); + yaffs_trace(YAFFS_TRACE_OS, "inode_setattr called"); if (attr->ia_valid & ATTR_SIZE) { yaffs_vfs_setsize(inode, attr->ia_size); inode->i_blocks = (inode->i_size + 511) >> 9; @@ -1991,9 +1941,10 @@ static int yaffs_setattr(struct dentry *dentry, struct iattr *attr) } dev = yaffs_inode_to_obj(inode)->my_dev; if (attr->ia_valid & ATTR_SIZE) { - T(YAFFS_TRACE_OS, (TSTR("resize to %d(%x)\n"), - (int)(attr->ia_size), - (int)(attr->ia_size))); + yaffs_trace(YAFFS_TRACE_OS, + "resize to %d(%x)", + (int)(attr->ia_size), + (int)(attr->ia_size)); } yaffs_gross_lock(dev); result = yaffs_set_attribs(yaffs_inode_to_obj(inode), attr); @@ -2006,13 +1957,13 @@ static int yaffs_setattr(struct dentry *dentry, struct iattr *attr) } - T(YAFFS_TRACE_OS, (TSTR("yaffs_setattr done returning %d\n"), error)); + yaffs_trace(YAFFS_TRACE_OS, "yaffs_setattr done returning %d", error); return error; } #ifdef CONFIG_YAFFS_XATTR -int yaffs_setxattr(struct dentry *dentry, const char *name, +static int yaffs_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags) { struct inode *inode = dentry->d_inode; @@ -2020,7 +1971,7 @@ int yaffs_setxattr(struct dentry *dentry, const char *name, struct yaffs_dev *dev; struct yaffs_obj *obj = yaffs_inode_to_obj(inode); - T(YAFFS_TRACE_OS, (TSTR("yaffs_setxattr of object %d\n"), obj->obj_id)); + yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr of object %d", obj->obj_id); if (error == 0) { int result; @@ -2034,21 +1985,22 @@ int yaffs_setxattr(struct dentry *dentry, const char *name, yaffs_gross_unlock(dev); } - T(YAFFS_TRACE_OS, (TSTR("yaffs_setxattr done returning %d\n"), error)); + yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr done returning %d", error); return error; } -ssize_t yaffs_getxattr(struct dentry * dentry, const char *name, void *buff, - size_t size) +static ssize_t yaffs_getxattr(struct dentry * dentry, const char *name, + void *buff, size_t size) { struct inode *inode = dentry->d_inode; int error = 0; struct yaffs_dev *dev; struct yaffs_obj *obj = yaffs_inode_to_obj(inode); - T(YAFFS_TRACE_OS, - (TSTR("yaffs_getxattr \"%s\" from object %d\n"), name, obj->obj_id)); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_getxattr \"%s\" from object %d", + name, obj->obj_id); if (error == 0) { dev = obj->my_dev; @@ -2057,20 +2009,20 @@ ssize_t yaffs_getxattr(struct dentry * dentry, const char *name, void *buff, yaffs_gross_unlock(dev); } - T(YAFFS_TRACE_OS, (TSTR("yaffs_getxattr done returning %d\n"), error)); + yaffs_trace(YAFFS_TRACE_OS, "yaffs_getxattr done returning %d", error); return error; } -int yaffs_removexattr(struct dentry *dentry, const char *name) +static int yaffs_removexattr(struct dentry *dentry, const char *name) { struct inode *inode = dentry->d_inode; int error = 0; struct yaffs_dev *dev; struct yaffs_obj *obj = yaffs_inode_to_obj(inode); - T(YAFFS_TRACE_OS, - (TSTR("yaffs_removexattr of object %d\n"), obj->obj_id)); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_removexattr of object %d", obj->obj_id); if (error == 0) { int result; @@ -2084,21 +2036,21 @@ int yaffs_removexattr(struct dentry *dentry, const char *name) yaffs_gross_unlock(dev); } - T(YAFFS_TRACE_OS, - (TSTR("yaffs_removexattr done returning %d\n"), error)); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_removexattr done returning %d", error); return error; } -ssize_t yaffs_listxattr(struct dentry * dentry, char *buff, size_t size) +static ssize_t yaffs_listxattr(struct dentry * dentry, char *buff, size_t size) { struct inode *inode = dentry->d_inode; int error = 0; struct yaffs_dev *dev; struct yaffs_obj *obj = yaffs_inode_to_obj(inode); - T(YAFFS_TRACE_OS, - (TSTR("yaffs_listxattr of object %d\n"), obj->obj_id)); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_listxattr of object %d", obj->obj_id); if (error == 0) { dev = obj->my_dev; @@ -2107,7 +2059,8 @@ ssize_t yaffs_listxattr(struct dentry * dentry, char *buff, size_t size) yaffs_gross_unlock(dev); } - T(YAFFS_TRACE_OS, (TSTR("yaffs_listxattr done returning %d\n"), error)); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_listxattr done returning %d", error); return error; } @@ -2129,7 +2082,7 @@ static int yaffs_statfs(struct super_block *sb, struct statfs *buf) struct yaffs_dev *dev = yaffs_super_to_dev(sb); #endif - T(YAFFS_TRACE_OS, (TSTR("yaffs_statfs\n"))); + yaffs_trace(YAFFS_TRACE_OS, "yaffs_statfs"); yaffs_gross_lock(dev); @@ -2195,8 +2148,9 @@ static void yaffs_flush_inodes(struct super_block *sb) list_for_each_entry(iptr, &sb->s_inodes, i_sb_list) { obj = yaffs_inode_to_obj(iptr); if (obj) { - T(YAFFS_TRACE_OS, (TSTR("flushing obj %d\n"), - obj->obj_id)); + yaffs_trace(YAFFS_TRACE_OS, + "flushing obj %d", + obj->obj_id); yaffs_flush_file(obj, 1, 0); } } @@ -2245,12 +2199,12 @@ static int yaffs_do_sync_fs(struct super_block *sb, int request_checkpoint) unsigned gc_urgent = yaffs_bg_gc_urgency(dev); int do_checkpoint; - T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND, - (TSTR("yaffs_do_sync_fs: gc-urgency %d %s %s%s\n"), - gc_urgent, - sb->s_dirt ? "dirty" : "clean", - request_checkpoint ? "checkpoint requested" : "no checkpoint", - oneshot_checkpoint ? " one-shot" : "")); + yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND, + "yaffs_do_sync_fs: gc-urgency %d %s %s%s", + gc_urgent, + sb->s_dirt ? "dirty" : "clean", + request_checkpoint ? "checkpoint requested" : "no checkpoint", + oneshot_checkpoint ? " one-shot" : ""); yaffs_gross_lock(dev); do_checkpoint = ((request_checkpoint && !gc_urgent) || @@ -2273,7 +2227,7 @@ static int yaffs_do_sync_fs(struct super_block *sb, int request_checkpoint) * yaffs_bg_start() launches the background thread. * yaffs_bg_stop() cleans up the background thread. * - * NB: + * NB: * The thread should only run after the yaffs is initialised * The thread should be stopped before yaffs is unmounted. * The thread should not do any writing while the fs is in read only. @@ -2299,14 +2253,14 @@ static int yaffs_bg_thread_fn(void *data) int gc_result; struct timer_list timer; - T(YAFFS_TRACE_BACKGROUND, - (TSTR("yaffs_background starting for dev %p\n"), (void *)dev)); + yaffs_trace(YAFFS_TRACE_BACKGROUND, + "yaffs_background starting for dev %p", (void *)dev); #ifdef YAFFS_COMPILE_FREEZER set_freezable(); #endif while (context->bg_running) { - T(YAFFS_TRACE_BACKGROUND, (TSTR("yaffs_background\n"))); + yaffs_trace(YAFFS_TRACE_BACKGROUND, "yaffs_background"); if (kthread_should_stop()) break; @@ -2334,11 +2288,13 @@ static int yaffs_bg_thread_fn(void *data) next_gc = now + HZ / 10 + 1; else next_gc = now + HZ * 2; - } else /* + } else { + /* * gc not running so set to next_dir_update * to cut down on wake ups */ next_gc = next_dir_update; + } } yaffs_gross_unlock(dev); #if 1 @@ -2422,9 +2378,9 @@ static int yaffs_write_super(struct super_block *sb) { unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2); - T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND, - (TSTR("yaffs_write_super%s\n"), - request_checkpoint ? " checkpt" : "")); + yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND, + "yaffs_write_super %s", + request_checkpoint ? " checkpt" : ""); yaffs_do_sync_fs(sb, request_checkpoint); @@ -2441,8 +2397,8 @@ static int yaffs_sync_fs(struct super_block *sb) { unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1); - T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, - (TSTR("yaffs_sync_fs%s\n"), request_checkpoint ? " checkpt" : "")); + yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC, + "yaffs_sync_fs%s", request_checkpoint ? " checkpt" : ""); yaffs_do_sync_fs(sb, request_checkpoint); @@ -2457,7 +2413,7 @@ static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino) struct yaffs_obj *obj; struct yaffs_dev *dev = yaffs_super_to_dev(sb); - T(YAFFS_TRACE_OS, (TSTR("yaffs_iget for %lu\n"), ino)); + yaffs_trace(YAFFS_TRACE_OS, "yaffs_iget for %lu", ino); inode = iget_locked(sb, ino); if (!inode) @@ -2494,8 +2450,8 @@ static void yaffs_read_inode(struct inode *inode) struct yaffs_obj *obj; struct yaffs_dev *dev = yaffs_super_to_dev(inode->i_sb); - T(YAFFS_TRACE_OS, - (TSTR("yaffs_read_inode for %d\n"), (int)inode->i_ino)); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_read_inode for %d", (int)inode->i_ino); if (current != yaffs_dev_to_lc(dev)->readdir_process) yaffs_gross_lock(dev); @@ -2517,13 +2473,13 @@ static void yaffs_put_super(struct super_block *sb) { struct yaffs_dev *dev = yaffs_super_to_dev(sb); - T(YAFFS_TRACE_OS, (TSTR("yaffs_put_super\n"))); + yaffs_trace(YAFFS_TRACE_OS, "yaffs_put_super"); - T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND, - (TSTR("Shutting down yaffs background thread\n"))); + yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND, + "Shutting down yaffs background thread"); yaffs_bg_stop(dev); - T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND, - (TSTR("yaffs background thread shut down\n"))); + yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND, + "yaffs background thread shut down"); yaffs_gross_lock(dev); @@ -2541,7 +2497,7 @@ static void yaffs_put_super(struct super_block *sb) mutex_unlock(&yaffs_context_lock); if (yaffs_dev_to_lc(dev)->spare_buffer) { - YFREE(yaffs_dev_to_lc(dev)->spare_buffer); + kfree(yaffs_dev_to_lc(dev)->spare_buffer); yaffs_dev_to_lc(dev)->spare_buffer = NULL; } @@ -2562,7 +2518,7 @@ static void yaffs_touch_super(struct yaffs_dev *dev) { struct super_block *sb = yaffs_dev_to_lc(dev)->super; - T(YAFFS_TRACE_OS, (TSTR("yaffs_touch_super() sb = %p\n"), sb)); + yaffs_trace(YAFFS_TRACE_OS, "yaffs_touch_super() sb = %p", sb); if (sb) sb->s_dirt = 1; } @@ -2605,9 +2561,9 @@ static int yaffs_parse_options(struct yaffs_options *options, options_str++; } - if (!strcmp(cur_opt, "inband-tags")) + if (!strcmp(cur_opt, "inband-tags")) { options->inband_tags = 1; - else if (!strcmp(cur_opt, "tags-ecc-off")) { + } else if (!strcmp(cur_opt, "tags-ecc-off")) { options->tags_ecc_on = 0; options->tags_ecc_overridden = 1; } else if (!strcmp(cur_opt, "tags-ecc-on")) { @@ -2625,13 +2581,13 @@ static int yaffs_parse_options(struct yaffs_options *options, } else if (!strcmp(cur_opt, "empty-lost-and-found-on")) { options->empty_lost_and_found = 1; options->empty_lost_and_found_overridden = 1; - } else if (!strcmp(cur_opt, "no-cache")) + } else if (!strcmp(cur_opt, "no-cache")) { options->no_cache = 1; - else if (!strcmp(cur_opt, "no-checkpoint-read")) + } else if (!strcmp(cur_opt, "no-checkpoint-read")) { options->skip_checkpoint_read = 1; - else if (!strcmp(cur_opt, "no-checkpoint-write")) + } else if (!strcmp(cur_opt, "no-checkpoint-write")) { options->skip_checkpoint_write = 1; - else if (!strcmp(cur_opt, "no-checkpoint")) { + } else if (!strcmp(cur_opt, "no-checkpoint")) { options->skip_checkpoint_read = 1; options->skip_checkpoint_write = 1; } else { @@ -2668,6 +2624,11 @@ static struct super_block *yaffs_internal_read_super(int yaffs_version, struct yaffs_linux_context *context_iterator; struct list_head *l; + if (!sb) { + printk(KERN_INFO "yaffs: sb is NULL\n"); + return NULL; + } + sb->s_magic = YAFFS_MAGIC; sb->s_op = &yaffs_super_ops; sb->s_flags |= MS_NOATIME; @@ -2678,9 +2639,7 @@ static struct super_block *yaffs_internal_read_super(int yaffs_version, sb->s_export_op = &yaffs_export_ops; #endif - if (!sb) - printk(KERN_INFO "yaffs: sb is NULL\n"); - else if (!sb->s_dev) + if (!sb->s_dev) printk(KERN_INFO "yaffs: sb->s_dev is NULL\n"); else if (!yaffs_devname(sb, devname_buf)) printk(KERN_INFO "yaffs: devname is NULL\n"); @@ -2704,14 +2663,15 @@ static struct super_block *yaffs_internal_read_super(int yaffs_version, sb->s_blocksize = PAGE_CACHE_SIZE; sb->s_blocksize_bits = PAGE_CACHE_SHIFT; - T(YAFFS_TRACE_OS, - (TSTR("yaffs_read_super: Using yaffs%d\n"), yaffs_version)); - T(YAFFS_TRACE_OS, - (TSTR("yaffs_read_super: block size %d\n"), (int)(sb->s_blocksize))); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_read_super: Using yaffs%d", yaffs_version); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_read_super: block size %d", (int)(sb->s_blocksize)); - T(YAFFS_TRACE_ALWAYS, - (TSTR("yaffs: Attempting MTD mount of %u.%u,\"%s\"\n"), - MAJOR(sb->s_dev), MINOR(sb->s_dev), yaffs_devname(sb, devname_buf))); + yaffs_trace(YAFFS_TRACE_ALWAYS, + "yaffs: Attempting MTD mount of %u.%u,\"%s\"", + MAJOR(sb->s_dev), MINOR(sb->s_dev), + yaffs_devname(sb, devname_buf)); /* Check it's an mtd device..... */ if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) @@ -2720,46 +2680,46 @@ static struct super_block *yaffs_internal_read_super(int yaffs_version, /* Get the device */ mtd = get_mtd_device(NULL, MINOR(sb->s_dev)); if (!mtd) { - T(YAFFS_TRACE_ALWAYS, - (TSTR("yaffs: MTD device #%u doesn't appear to exist\n"), - MINOR(sb->s_dev))); + yaffs_trace(YAFFS_TRACE_ALWAYS, + "yaffs: MTD device #%u doesn't appear to exist", + MINOR(sb->s_dev)); return NULL; } /* Check it's NAND */ if (mtd->type != MTD_NANDFLASH) { - T(YAFFS_TRACE_ALWAYS, - (TSTR("yaffs: MTD device is not NAND it's type %d\n"), - mtd->type)); + yaffs_trace(YAFFS_TRACE_ALWAYS, + "yaffs: MTD device is not NAND it's type %d", + mtd->type); return NULL; } - T(YAFFS_TRACE_OS, (TSTR(" erase %p\n"), mtd->erase)); - T(YAFFS_TRACE_OS, (TSTR(" read %p\n"), mtd->read)); - T(YAFFS_TRACE_OS, (TSTR(" write %p\n"), mtd->write)); - T(YAFFS_TRACE_OS, (TSTR(" readoob %p\n"), mtd->read_oob)); - T(YAFFS_TRACE_OS, (TSTR(" writeoob %p\n"), mtd->write_oob)); - T(YAFFS_TRACE_OS, (TSTR(" block_isbad %p\n"), mtd->block_isbad)); - T(YAFFS_TRACE_OS, (TSTR(" block_markbad %p\n"), mtd->block_markbad)); - T(YAFFS_TRACE_OS, (TSTR(" %s %d\n"), WRITE_SIZE_STR, WRITE_SIZE(mtd))); - T(YAFFS_TRACE_OS, (TSTR(" oobsize %d\n"), mtd->oobsize)); - T(YAFFS_TRACE_OS, (TSTR(" erasesize %d\n"), mtd->erasesize)); + yaffs_trace(YAFFS_TRACE_OS, " erase %p", mtd->erase); + yaffs_trace(YAFFS_TRACE_OS, " read %p", mtd->read); + yaffs_trace(YAFFS_TRACE_OS, " write %p", mtd->write); + yaffs_trace(YAFFS_TRACE_OS, " readoob %p", mtd->read_oob); + yaffs_trace(YAFFS_TRACE_OS, " writeoob %p", mtd->write_oob); + yaffs_trace(YAFFS_TRACE_OS, " block_isbad %p", mtd->block_isbad); + yaffs_trace(YAFFS_TRACE_OS, " block_markbad %p", mtd->block_markbad); + yaffs_trace(YAFFS_TRACE_OS, " %s %d", WRITE_SIZE_STR, WRITE_SIZE(mtd)); + yaffs_trace(YAFFS_TRACE_OS, " oobsize %d", mtd->oobsize); + yaffs_trace(YAFFS_TRACE_OS, " erasesize %d", mtd->erasesize); #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29) - T(YAFFS_TRACE_OS, (TSTR(" size %u\n"), mtd->size)); + yaffs_trace(YAFFS_TRACE_OS, " size %u", mtd->size); #else - T(YAFFS_TRACE_OS, (TSTR(" size %lld\n"), mtd->size)); + yaffs_trace(YAFFS_TRACE_OS, " size %lld", mtd->size); #endif #ifdef CONFIG_YAFFS_AUTO_YAFFS2 if (yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) { - T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: auto selecting yaffs2\n"))); + yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs2"); yaffs_version = 2; } /* Added NCB 26/5/2006 for completeness */ if (yaffs_version == 2 && !options.inband_tags && WRITE_SIZE(mtd) == 512) { - T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: auto selecting yaffs1\n"))); + yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs1"); yaffs_version = 1; } #endif @@ -2775,18 +2735,18 @@ static struct super_block *yaffs_internal_read_super(int yaffs_version, !mtd->write_ecc || !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) { #endif - T(YAFFS_TRACE_ALWAYS, - (TSTR("yaffs: MTD device does not support required " - "functions\n"))); + yaffs_trace(YAFFS_TRACE_ALWAYS, + "MTD device does not support required functions" + ); return NULL; } if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE || mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) && !options.inband_tags) { - T(YAFFS_TRACE_ALWAYS, - (TSTR("yaffs: MTD device does not have the " - "right page sizes\n"))); + yaffs_trace(YAFFS_TRACE_ALWAYS, + "MTD device does not have the right page sizes" + ); return NULL; } } else { @@ -2798,17 +2758,17 @@ static struct super_block *yaffs_internal_read_super(int yaffs_version, !mtd->write_ecc || !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) { #endif - T(YAFFS_TRACE_ALWAYS, - (TSTR("yaffs: MTD device does not support required " - "functions\n"))); + yaffs_trace(YAFFS_TRACE_ALWAYS, + "MTD device does not support required functions" + ); return NULL; } if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK || mtd->oobsize != YAFFS_BYTES_PER_SPARE) { - T(YAFFS_TRACE_ALWAYS, - (TSTR("yaffs: MTD device does not support have the " - "right page sizes\n"))); + yaffs_trace(YAFFS_TRACE_ALWAYS, + "MTD device does not support have the right page sizes" + ); return NULL; } } @@ -2821,7 +2781,8 @@ static struct super_block *yaffs_internal_read_super(int yaffs_version, if (!read_only && !(mtd->flags & MTD_WRITEABLE)) { read_only = 1; printk(KERN_INFO - "yaffs: mtd is read only, setting superblock read only"); + "yaffs: mtd is read only, setting superblock read only\n" + ); sb->s_flags |= MS_RDONLY; } @@ -2839,9 +2800,9 @@ static struct super_block *yaffs_internal_read_super(int yaffs_version, if (!dev) { /* Deep shit could not allocate device structure */ - T(YAFFS_TRACE_ALWAYS, - (TSTR("yaffs_read_super: Failed trying to allocate " - "struct yaffs_dev. \n"))); + yaffs_trace(YAFFS_TRACE_ALWAYS, + "yaffs_read_super: Failed trying to allocate struct yaffs_dev." + ); return NULL; } memset(dev, 0, sizeof(struct yaffs_dev)); @@ -2922,7 +2883,8 @@ static struct super_block *yaffs_internal_read_super(int yaffs_version, param->read_chunk_tags_fn = nandmtd2_read_chunk_tags; param->bad_block_fn = nandmtd2_mark_block_bad; param->query_block_fn = nandmtd2_query_block; - yaffs_dev_to_lc(dev)->spare_buffer = YMALLOC(mtd->oobsize); + yaffs_dev_to_lc(dev)->spare_buffer = + kmalloc(mtd->oobsize, GFP_NOFS); param->is_yaffs2 = 1; #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17)) param->total_bytes_per_chunk = mtd->writesize; @@ -2999,9 +2961,9 @@ static struct super_block *yaffs_internal_read_super(int yaffs_version, err = yaffs_guts_initialise(dev); - T(YAFFS_TRACE_OS, - (TSTR("yaffs_read_super: guts initialised %s\n"), - (err == YAFFS_OK) ? "OK" : "FAILED")); + yaffs_trace(YAFFS_TRACE_OS, + "yaffs_read_super: guts initialised %s", + (err == YAFFS_OK) ? "OK" : "FAILED"); if (err == YAFFS_OK) yaffs_bg_start(dev); @@ -3022,11 +2984,11 @@ static struct super_block *yaffs_internal_read_super(int yaffs_version, inode->i_op = &yaffs_dir_inode_operations; inode->i_fop = &yaffs_dir_operations; - T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: got root inode\n"))); + yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: got root inode"); root = d_alloc_root(inode); - T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: d_alloc_root done\n"))); + yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: d_alloc_root done"); if (!root) { iput(inode); @@ -3034,11 +2996,11 @@ static struct super_block *yaffs_internal_read_super(int yaffs_version, } sb->s_root = root; sb->s_dirt = !dev->is_checkpointed; - T(YAFFS_TRACE_ALWAYS, - (TSTR("yaffs_read_super: is_checkpointed %d\n"), - dev->is_checkpointed)); + yaffs_trace(YAFFS_TRACE_ALWAYS, + "yaffs_read_super: is_checkpointed %d", + dev->is_checkpointed); - T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: done\n"))); + yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: done"); return sb; } @@ -3136,40 +3098,30 @@ static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super, #endif /* CONFIG_YAFFS_YAFFS2 */ static struct proc_dir_entry *my_proc_entry; -static struct proc_dir_entry *debug_proc_entry; static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev *dev) { - buf += - sprintf(buf, "start_block.......... %d\n", dev->param.start_block); - buf += sprintf(buf, "end_block............ %d\n", dev->param.end_block); - buf += - sprintf(buf, "total_bytes_per_chunk %d\n", - dev->param.total_bytes_per_chunk); - buf += - sprintf(buf, "use_nand_ecc......... %d\n", dev->param.use_nand_ecc); - buf += - sprintf(buf, "no_tags_ecc.......... %d\n", dev->param.no_tags_ecc); - buf += sprintf(buf, "is_yaffs2............ %d\n", dev->param.is_yaffs2); - buf += - sprintf(buf, "inband_tags.......... %d\n", dev->param.inband_tags); - buf += - sprintf(buf, "empty_lost_n_found... %d\n", - dev->param.empty_lost_n_found); - buf += - sprintf(buf, "disable_lazy_load.... %d\n", - dev->param.disable_lazy_load); - buf += - sprintf(buf, "refresh_period....... %d\n", - dev->param.refresh_period); - buf += sprintf(buf, "n_caches............. %d\n", dev->param.n_caches); - buf += - sprintf(buf, "n_reserved_blocks.... %d\n", - dev->param.n_reserved_blocks); - buf += - sprintf(buf, "always_check_erased.. %d\n", - dev->param.always_check_erased); + struct yaffs_param *param = &dev->param; + buf += sprintf(buf, "start_block.......... %d\n", param->start_block); + buf += sprintf(buf, "end_block............ %d\n", param->end_block); + buf += sprintf(buf, "total_bytes_per_chunk %d\n", + param->total_bytes_per_chunk); + buf += sprintf(buf, "use_nand_ecc......... %d\n", param->use_nand_ecc); + buf += sprintf(buf, "no_tags_ecc.......... %d\n", param->no_tags_ecc); + buf += sprintf(buf, "is_yaffs2............ %d\n", param->is_yaffs2); + buf += sprintf(buf, "inband_tags.......... %d\n", param->inband_tags); + buf += sprintf(buf, "empty_lost_n_found... %d\n", + param->empty_lost_n_found); + buf += sprintf(buf, "disable_lazy_load.... %d\n", + param->disable_lazy_load); + buf += sprintf(buf, "refresh_period....... %d\n", + param->refresh_period); + buf += sprintf(buf, "n_caches............. %d\n", param->n_caches); + buf += sprintf(buf, "n_reserved_blocks.... %d\n", + param->n_reserved_blocks); + buf += sprintf(buf, "always_check_erased.. %d\n", + param->always_check_erased); buf += sprintf(buf, "\n"); return buf; @@ -3177,14 +3129,13 @@ static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev *dev) static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev *dev) { - buf += - sprintf(buf, "data_bytes_per_chunk. %d\n", - dev->data_bytes_per_chunk); + buf += sprintf(buf, "data_bytes_per_chunk. %d\n", + dev->data_bytes_per_chunk); buf += sprintf(buf, "chunk_grp_bits....... %d\n", dev->chunk_grp_bits); buf += sprintf(buf, "chunk_grp_size....... %d\n", dev->chunk_grp_size); buf += sprintf(buf, "n_erased_blocks...... %d\n", dev->n_erased_blocks); - buf += - sprintf(buf, "blocks_in_checkpt.... %d\n", dev->blocks_in_checkpt); + buf += sprintf(buf, "blocks_in_checkpt.... %d\n", + dev->blocks_in_checkpt); buf += sprintf(buf, "\n"); buf += sprintf(buf, "n_tnodes............. %d\n", dev->n_tnodes); buf += sprintf(buf, "n_obj................ %d\n", dev->n_obj); @@ -3195,27 +3146,26 @@ static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev *dev) buf += sprintf(buf, "n_erasures........... %u\n", dev->n_erasures); buf += sprintf(buf, "n_gc_copies.......... %u\n", dev->n_gc_copies); buf += sprintf(buf, "all_gcs.............. %u\n", dev->all_gcs); - buf += - sprintf(buf, "passive_gc_count..... %u\n", dev->passive_gc_count); - buf += - sprintf(buf, "oldest_dirty_gc_count %u\n", - dev->oldest_dirty_gc_count); + buf += sprintf(buf, "passive_gc_count..... %u\n", + dev->passive_gc_count); + buf += sprintf(buf, "oldest_dirty_gc_count %u\n", + dev->oldest_dirty_gc_count); buf += sprintf(buf, "n_gc_blocks.......... %u\n", dev->n_gc_blocks); buf += sprintf(buf, "bg_gcs............... %u\n", dev->bg_gcs); - buf += - sprintf(buf, "n_retired_writes..... %u\n", dev->n_retired_writes); - buf += - sprintf(buf, "n_retired_blocks..... %u\n", dev->n_retired_blocks); + buf += sprintf(buf, "n_retired_writes..... %u\n", + dev->n_retired_writes); + buf += sprintf(buf, "n_retired_blocks..... %u\n", + dev->n_retired_blocks); buf += sprintf(buf, "n_ecc_fixed.......... %u\n", dev->n_ecc_fixed); buf += sprintf(buf, "n_ecc_unfixed........ %u\n", dev->n_ecc_unfixed); - buf += - sprintf(buf, "n_tags_ecc_fixed..... %u\n", dev->n_tags_ecc_fixed); - buf += - sprintf(buf, "n_tags_ecc_unfixed... %u\n", dev->n_tags_ecc_unfixed); + buf += sprintf(buf, "n_tags_ecc_fixed..... %u\n", + dev->n_tags_ecc_fixed); + buf += sprintf(buf, "n_tags_ecc_unfixed... %u\n", + dev->n_tags_ecc_unfixed); buf += sprintf(buf, "cache_hits........... %u\n", dev->cache_hits); buf += sprintf(buf, "n_deleted_files...... %u\n", dev->n_deleted_files); - buf += - sprintf(buf, "n_unlinked_files..... %u\n", dev->n_unlinked_files); + buf += sprintf(buf, "n_unlinked_files..... %u\n", + dev->n_unlinked_files); buf += sprintf(buf, "refresh_count........ %u\n", dev->refresh_count); buf += sprintf(buf, "n_bg_deletions....... %u\n", dev->n_bg_deletions); @@ -3268,8 +3218,9 @@ static int yaffs_proc_read(char *page, sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->param.name); buf = yaffs_dump_dev_part0(buf, dev); - } else + } else { buf = yaffs_dump_dev_part1(buf, dev); + } break; } @@ -3279,37 +3230,6 @@ static int yaffs_proc_read(char *page, return buf - page < count ? buf - page : count; } -static int yaffs_stats_proc_read(char *page, - char **start, - off_t offset, int count, int *eof, void *data) -{ - struct list_head *item; - char *buf = page; - int n = 0; - - mutex_lock(&yaffs_context_lock); - - /* Locate and print the Nth entry. Order N-squared but N is small. */ - list_for_each(item, &yaffs_context_list) { - struct yaffs_linux_context *dc = - list_entry(item, struct yaffs_linux_context, context_list); - struct yaffs_dev *dev = dc->dev; - - int erased_chunks; - - erased_chunks = - dev->n_erased_blocks * dev->param.chunks_per_block; - - buf += sprintf(buf, "%d, %d, %d, %u, %u, %u, %u\n", - n, dev->n_free_chunks, erased_chunks, - dev->bg_gcs, dev->oldest_dirty_gc_count, - dev->n_obj, dev->n_tnodes); - } - mutex_unlock(&yaffs_context_lock); - - return buf - page < count ? buf - page : count; -} - /** * Set the verbosity of the warnings and error messages. * @@ -3320,35 +3240,36 @@ static struct { char *mask_name; unsigned mask_bitfield; } mask_flags[] = { - { - "allocate", YAFFS_TRACE_ALLOCATE}, { - "always", YAFFS_TRACE_ALWAYS}, { - "background", YAFFS_TRACE_BACKGROUND}, { - "bad_blocks", YAFFS_TRACE_BAD_BLOCKS}, { - "buffers", YAFFS_TRACE_BUFFERS}, { - "bug", YAFFS_TRACE_BUG}, { - "checkpt", YAFFS_TRACE_CHECKPOINT}, { - "deletion", YAFFS_TRACE_DELETION}, { - "erase", YAFFS_TRACE_ERASE}, { - "error", YAFFS_TRACE_ERROR}, { - "gc_detail", YAFFS_TRACE_GC_DETAIL}, { - "gc", YAFFS_TRACE_GC}, { - "lock", YAFFS_TRACE_LOCK}, { - "mtd", YAFFS_TRACE_MTD}, { - "nandaccess", YAFFS_TRACE_NANDACCESS}, { - "os", YAFFS_TRACE_OS}, { - "scan_debug", YAFFS_TRACE_SCAN_DEBUG}, { - "scan", YAFFS_TRACE_SCAN}, { - "tracing", YAFFS_TRACE_TRACING}, { - "sync", YAFFS_TRACE_SYNC}, { - "write", YAFFS_TRACE_WRITE}, { - "verify", YAFFS_TRACE_VERIFY}, { - "verify_nand", YAFFS_TRACE_VERIFY_NAND}, { - "verify_full", YAFFS_TRACE_VERIFY_FULL}, { - "verify_all", YAFFS_TRACE_VERIFY_ALL}, { - "all", 0xffffffff}, { - "none", 0}, { -NULL, 0},}; + {"allocate", YAFFS_TRACE_ALLOCATE}, + {"always", YAFFS_TRACE_ALWAYS}, + {"background", YAFFS_TRACE_BACKGROUND}, + {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS}, + {"buffers", YAFFS_TRACE_BUFFERS}, + {"bug", YAFFS_TRACE_BUG}, + {"checkpt", YAFFS_TRACE_CHECKPOINT}, + {"deletion", YAFFS_TRACE_DELETION}, + {"erase", YAFFS_TRACE_ERASE}, + {"error", YAFFS_TRACE_ERROR}, + {"gc_detail", YAFFS_TRACE_GC_DETAIL}, + {"gc", YAFFS_TRACE_GC}, + {"lock", YAFFS_TRACE_LOCK}, + {"mtd", YAFFS_TRACE_MTD}, + {"nandaccess", YAFFS_TRACE_NANDACCESS}, + {"os", YAFFS_TRACE_OS}, + {"scan_debug", YAFFS_TRACE_SCAN_DEBUG}, + {"scan", YAFFS_TRACE_SCAN}, + {"mount", YAFFS_TRACE_MOUNT}, + {"tracing", YAFFS_TRACE_TRACING}, + {"sync", YAFFS_TRACE_SYNC}, + {"write", YAFFS_TRACE_WRITE}, + {"verify", YAFFS_TRACE_VERIFY}, + {"verify_nand", YAFFS_TRACE_VERIFY_NAND}, + {"verify_full", YAFFS_TRACE_VERIFY_FULL}, + {"verify_all", YAFFS_TRACE_VERIFY_ALL}, + {"all", 0xffffffff}, + {"none", 0}, + {NULL, 0}, +}; #define MAX_MASK_NAME_LENGTH 40 static int yaffs_proc_write_trace_options(struct file *file, const char *buf, @@ -3470,13 +3391,13 @@ static int __init init_yaffs_fs(void) int error = 0; struct file_system_to_install *fsinst; - T(YAFFS_TRACE_ALWAYS, - (TSTR("yaffs built " __DATE__ " " __TIME__ " Installing. \n"))); + yaffs_trace(YAFFS_TRACE_ALWAYS, + "yaffs built " __DATE__ " " __TIME__ " Installing."); #ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED - T(YAFFS_TRACE_ALWAYS, - (TSTR - (" \n\n\n\nYAFFS-WARNING CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED selected.\n\n\n\n"))); + yaffs_trace(YAFFS_TRACE_ALWAYS, + " \n\n\n\nYAFFS-WARNING CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED selected.\n\n\n\n" + ); #endif mutex_init(&yaffs_context_lock); @@ -3489,18 +3410,9 @@ static int __init init_yaffs_fs(void) my_proc_entry->write_proc = yaffs_proc_write; my_proc_entry->read_proc = yaffs_proc_read; my_proc_entry->data = NULL; - } else - return -ENOMEM; - - debug_proc_entry = create_proc_entry("yaffs_stats", - S_IRUGO | S_IFREG, YPROC_ROOT); - - if (debug_proc_entry) { - debug_proc_entry->write_proc = NULL; - debug_proc_entry->read_proc = yaffs_stats_proc_read; - debug_proc_entry->data = NULL; - } else + } else { return -ENOMEM; + } /* Now add the file system entries */ @@ -3534,11 +3446,10 @@ static void __exit exit_yaffs_fs(void) struct file_system_to_install *fsinst; - T(YAFFS_TRACE_ALWAYS, - (TSTR("yaffs built " __DATE__ " " __TIME__ " removing. \n"))); + yaffs_trace(YAFFS_TRACE_ALWAYS, + "yaffs built " __DATE__ " " __TIME__ " removing."); remove_proc_entry("yaffs", YPROC_ROOT); - remove_proc_entry("yaffs_stats", YPROC_ROOT); fsinst = fs_to_install; diff --git a/fs/yaffs2/yaffs_yaffs1.c b/fs/yaffs2/yaffs_yaffs1.c index 4ab0f22b..6ee91a13 100644 --- a/fs/yaffs2/yaffs_yaffs1.c +++ b/fs/yaffs2/yaffs_yaffs1.c @@ -24,7 +24,6 @@ int yaffs1_scan(struct yaffs_dev *dev) struct yaffs_ext_tags tags; int blk; int result; - int chunk; int c; int deleted; @@ -35,16 +34,13 @@ int yaffs1_scan(struct yaffs_dev *dev) 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; - T(YAFFS_TRACE_SCAN, - (TSTR("yaffs1_scan starts intstartblk %d intendblk %d..." TENDSTR), - dev->internal_start_block, dev->internal_end_block)); + yaffs_trace(YAFFS_TRACE_SCAN, + "yaffs1_scan starts intstartblk %d intendblk %d...", + dev->internal_start_block, dev->internal_end_block); chunk_data = yaffs_get_temp_buffer(dev, __LINE__); @@ -66,16 +62,15 @@ int yaffs1_scan(struct yaffs_dev *dev) 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, seq_number)); + 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))); + yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "Block empty "); dev->n_erased_blocks++; dev->n_free_chunks += dev->param.chunks_per_block; } @@ -86,7 +81,7 @@ int yaffs1_scan(struct yaffs_dev *dev) for (blk = dev->internal_start_block; !alloc_failed && blk <= dev->internal_end_block; blk++) { - YYIELD(); + cond_resched(); bi = yaffs_get_block_info(dev, blk); state = bi->block_state; @@ -111,7 +106,6 @@ int yaffs1_scan(struct yaffs_dev *dev) */ deleted++; dev->n_free_chunks++; - /*T((" %d %d deleted\n",blk,c)); */ } else if (!tags.chunk_used) { /* An unassigned chunk in the block * This means that either the block is empty or @@ -119,20 +113,19 @@ int yaffs1_scan(struct yaffs_dev *dev) */ 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->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->alloc_block = blk; dev->alloc_page = c; dev->alloc_block_finder = blk; - /* Set block finder here to encourage the allocator to go forth from here. */ } @@ -146,10 +139,10 @@ int yaffs1_scan(struct yaffs_dev *dev) bi->pages_in_use++; 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). + tags.obj_id, + YAFFS_OBJECT_TYPE_FILE); + /* PutChunkIntoFile checks for a clash + * (two data chunks with the same chunk_id). */ if (!in) @@ -162,13 +155,13 @@ int yaffs1_scan(struct yaffs_dev *dev) } endpos = - (tags.chunk_id - - 1) * dev->data_bytes_per_chunk + + (tags.chunk_id - 1) * + dev->data_bytes_per_chunk + tags.n_bytes; - if (in - && in->variant_type == - YAFFS_OBJECT_TYPE_FILE - && in->variant.file_variant.scanned_size < + if (in && + in->variant_type == + YAFFS_OBJECT_TYPE_FILE && + in->variant.file_variant.scanned_size < endpos) { in->variant.file_variant.scanned_size = endpos; @@ -180,10 +173,9 @@ int yaffs1_scan(struct yaffs_dev *dev) } } - /* T((" %d %d data %d %d\n",blk,c,tags.obj_id,tags.chunk_id)); */ } else { /* chunk_id == 0, so it is an ObjectHeader. - * Thus, we read in the object header and make the object + * Make the object */ yaffs_set_chunk_bit(dev, blk, c); bi->pages_in_use++; @@ -197,18 +189,19 @@ int yaffs1_scan(struct yaffs_dev *dev) 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 obj_id 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_del_obj(in); - - in = 0; + in = NULL; } in = yaffs_find_or_create_by_number(dev, - tags.obj_id, - oh->type); + tags.obj_id, + oh->type); if (!in) alloc_failed = 1; @@ -217,27 +210,28 @@ int yaffs1_scan(struct yaffs_dev *dev) struct yaffs_shadow_fixer *fixer; fixer = - YMALLOC(sizeof - (struct - yaffs_shadow_fixer)); + kmalloc(sizeof + (struct yaffs_shadow_fixer), + GFP_NOFS); if (fixer) { fixer->next = shadow_fixers; shadow_fixers = fixer; fixer->obj_id = tags.obj_id; fixer->shadowed_id = oh->shadows_obj; - T(YAFFS_TRACE_SCAN, - (TSTR - (" Shadow fixer: %d shadows %d" - TENDSTR), fixer->obj_id, - fixer->shadowed_id)); + 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. */ + /* We have already filled this one. + * We have a duplicate and need to + * resolve it. */ unsigned existing_serial = in->serial; unsigned new_serial = @@ -245,13 +239,15 @@ int yaffs1_scan(struct yaffs_dev *dev) if (((existing_serial + 1) & 3) == new_serial) { - /* Use new one - destroy the exisiting one */ + /* 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. */ + /* Use existing - destroy + * this one. */ yaffs_chunk_del(dev, chunk, 1, __LINE__); } @@ -261,7 +257,8 @@ int yaffs1_scan(struct yaffs_dev *dev) (tags.obj_id == YAFFS_OBJECTID_ROOT || tags.obj_id == YAFFS_OBJECTID_LOSTNFOUND)) { - /* We only load some info, don't fiddle with directory structure */ + /* We only load some info, don't fiddle + * with directory structure */ in->valid = 1; in->variant_type = oh->type; @@ -300,37 +297,24 @@ int yaffs1_scan(struct yaffs_dev *dev) 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, another problem.... - * We're trying to use a non-directory as a directory + 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))); + 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_add_obj_to_dir(parent, in); - if (0 && (parent == dev->del_dir || - parent == - dev->unlinked_dir)) { - in->deleted = 1; /* If it is unlinked at start up then it wants deleting */ - dev->n_deleted_files++; - } - /* 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 */ @@ -340,7 +324,7 @@ int yaffs1_scan(struct yaffs_dev *dev) use_header_file_size) in->variant. - file_variant.file_size + file_variant.file_size = oh->file_size; break; @@ -368,18 +352,19 @@ int yaffs1_scan(struct yaffs_dev *dev) 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 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->alloc_block = -1; } @@ -392,13 +377,12 @@ int yaffs1_scan(struct yaffs_dev *dev) 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_link_fixup(dev, hard_list); @@ -411,8 +395,9 @@ int yaffs1_scan(struct yaffs_dev *dev) 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. + /* Complete the rename transaction by deleting the + * shadowed object then setting the object header + to unshadowed. */ obj = yaffs_find_by_number(dev, fixer->shadowed_id); if (obj) @@ -423,7 +408,7 @@ int yaffs1_scan(struct yaffs_dev *dev) if (obj) yaffs_update_oh(obj, NULL, 1, 0, 0, NULL); - YFREE(fixer); + kfree(fixer); } } @@ -432,7 +417,7 @@ int yaffs1_scan(struct yaffs_dev *dev) 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; } diff --git a/fs/yaffs2/yaffs_yaffs2.c b/fs/yaffs2/yaffs_yaffs2.c index 97dac454..77101e72 100644 --- a/fs/yaffs2/yaffs_yaffs2.c +++ b/fs/yaffs2/yaffs_yaffs2.c @@ -28,7 +28,6 @@ * the partition is at least this big. */ #define YAFFS_CHECKPOINT_MIN_BLOCKS 60 - #define YAFFS_SMALL_HOLE_THRESHOLD 4 /* @@ -66,7 +65,6 @@ void yaffs_calc_oldest_dirty_seq(struct yaffs_dev *dev) dev->oldest_dirty_seq = seq; dev->oldest_dirty_block = block_no; } - } void yaffs2_find_oldest_dirty_seq(struct yaffs_dev *dev) @@ -81,7 +79,7 @@ void yaffs2_find_oldest_dirty_seq(struct yaffs_dev *dev) /* * yaffs_clear_oldest_dirty_seq() * Called when a block is erased or marked bad. (ie. when its seq_number - * becomes invalid). If the value matches the oldest then we clear + * becomes invalid). If the value matches the oldest then we clear * dev->oldest_dirty_seq to force its recomputation. */ void yaffs2_clear_oldest_dirty_seq(struct yaffs_dev *dev, @@ -127,8 +125,8 @@ int yaffs_block_ok_for_gc(struct yaffs_dev *dev, struct yaffs_block_info *bi) yaffs2_find_oldest_dirty_seq(dev); - /* Can't do gc of this block if there are any blocks older than this one that have - * discarded pages. + /* Can't do gc of this block if there are any blocks older than this + * one that have discarded pages. */ return (bi->seq_number <= dev->oldest_dirty_seq); } @@ -138,13 +136,11 @@ int yaffs_block_ok_for_gc(struct yaffs_dev *dev, struct yaffs_block_info *bi) * periodically finds the oldest full block by sequence number for refreshing. * Only for yaffs2. */ -u32 yaffs2_find_refresh_block(struct yaffs_dev * dev) +u32 yaffs2_find_refresh_block(struct yaffs_dev *dev) { u32 b; - u32 oldest = 0; u32 oldest_seq = 0; - struct yaffs_block_info *bi; if (!dev->param.is_yaffs2) @@ -186,10 +182,9 @@ u32 yaffs2_find_refresh_block(struct yaffs_dev * dev) } if (oldest > 0) { - T(YAFFS_TRACE_GC, - (TSTR - ("GC refresh count %d selected block %d with seq_number %d" - TENDSTR), dev->refresh_count, oldest, oldest_seq)); + yaffs_trace(YAFFS_TRACE_GC, + "GC refresh count %d selected block %d with seq_number %d", + dev->refresh_count, oldest, oldest_seq); } return oldest; @@ -227,13 +222,14 @@ int yaffs_calc_checkpt_blocks_required(struct yaffs_dev *dev) n_bytes += dev_blocks * sizeof(struct yaffs_block_info); n_bytes += dev_blocks * dev->chunk_bit_stride; n_bytes += - (sizeof(struct yaffs_checkpt_obj) + - sizeof(u32)) * (dev->n_obj); - n_bytes += (dev->tnode_size + sizeof(u32)) * (dev->n_tnodes); + (sizeof(struct yaffs_checkpt_obj) + sizeof(u32)) * + dev->n_obj; + n_bytes += (dev->tnode_size + sizeof(u32)) * dev->n_tnodes; n_bytes += sizeof(struct yaffs_checkpt_validity); n_bytes += sizeof(u32); /* checksum */ - /* Round up and add 2 blocks to allow for some bad blocks, so add 3 */ + /* Round up and add 2 blocks to allow for some bad blocks, + * so add 3 */ n_blocks = (n_bytes / @@ -315,7 +311,6 @@ static int yaffs2_wr_checkpt_dev(struct yaffs_dev *dev) u32 n_bytes; u32 n_blocks = (dev->internal_end_block - dev->internal_start_block + 1); - int ok; /* Write device runtime values */ @@ -338,7 +333,6 @@ static int yaffs2_wr_checkpt_dev(struct yaffs_dev *dev) n_bytes); } return ok ? 1 : 0; - } static int yaffs2_rd_checkpt_dev(struct yaffs_dev *dev) @@ -347,7 +341,6 @@ static int yaffs2_rd_checkpt_dev(struct yaffs_dev *dev) u32 n_bytes; u32 n_blocks = (dev->internal_end_block - dev->internal_start_block + 1); - int ok; ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp)); @@ -375,7 +368,6 @@ static int yaffs2_rd_checkpt_dev(struct yaffs_dev *dev) static void yaffs2_obj_checkpt_obj(struct yaffs_checkpt_obj *cp, struct yaffs_obj *obj) { - cp->obj_id = obj->obj_id; cp->parent_id = (obj->parent) ? obj->parent->obj_id : 0; cp->hdr_chunk = obj->hdr_chunk; @@ -398,16 +390,13 @@ static void yaffs2_obj_checkpt_obj(struct yaffs_checkpt_obj *cp, static int taffs2_checkpt_obj_to_obj(struct yaffs_obj *obj, struct yaffs_checkpt_obj *cp) { - struct yaffs_obj *parent; if (obj->variant_type != cp->variant_type) { - T(YAFFS_TRACE_ERROR, (TSTR("Checkpoint read object %d type %d " - TCONT - ("chunk %d does not match existing object type %d") - TENDSTR), cp->obj_id, - cp->variant_type, cp->hdr_chunk, - obj->variant_type)); + yaffs_trace(YAFFS_TRACE_ERROR, + "Checkpoint read object %d type %d chunk %d does not match existing object type %d", + cp->obj_id, cp->variant_type, cp->hdr_chunk, + obj->variant_type); return 0; } @@ -415,20 +404,18 @@ static int taffs2_checkpt_obj_to_obj(struct yaffs_obj *obj, if (cp->parent_id) parent = yaffs_find_or_create_by_number(obj->my_dev, - cp->parent_id, - YAFFS_OBJECT_TYPE_DIRECTORY); + cp->parent_id, + YAFFS_OBJECT_TYPE_DIRECTORY); else parent = NULL; if (parent) { if (parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) { - T(YAFFS_TRACE_ALWAYS, - (TSTR - ("Checkpoint read object %d parent %d type %d" - TCONT(" chunk %d Parent type, %d, not directory") - TENDSTR), cp->obj_id, cp->parent_id, - cp->variant_type, cp->hdr_chunk, - parent->variant_type)); + yaffs_trace(YAFFS_TRACE_ALWAYS, + "Checkpoint read object %d parent %d type %d chunk %d Parent type, %d, not directory", + cp->obj_id, cp->parent_id, + cp->variant_type, cp->hdr_chunk, + parent->variant_type); return 0; } yaffs_add_obj_to_dir(parent, obj); @@ -469,15 +456,11 @@ static int yaffs2_checkpt_tnode_worker(struct yaffs_obj *in, for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) { if (tn->internal[i]) { ok = yaffs2_checkpt_tnode_worker(in, - tn-> - internal - [i], - level - - 1, - (chunk_offset - << - YAFFS_TNODES_INTERNAL_BITS) - + i); + tn->internal[i], + level - 1, + (chunk_offset << + YAFFS_TNODES_INTERNAL_BITS) + + i); } } } else if (level == 0) { @@ -494,7 +477,6 @@ static int yaffs2_checkpt_tnode_worker(struct yaffs_obj *in, } return ok; - } static int yaffs2_wr_checkpt_tnodes(struct yaffs_obj *obj) @@ -533,10 +515,10 @@ static int yaffs2_rd_checkpt_tnodes(struct yaffs_obj *obj) /* Read level 0 tnode */ tn = yaffs_get_tnode(dev); - if (tn) { + if (tn) ok = (yaffs2_checkpt_rd(dev, tn, dev->tnode_size) == - dev->tnode_size); - } else + dev->tnode_size); + else ok = 0; if (tn && ok) @@ -548,12 +530,11 @@ static int yaffs2_rd_checkpt_tnodes(struct yaffs_obj *obj) ok = (yaffs2_checkpt_rd (dev, &base_chunk, sizeof(base_chunk)) == sizeof(base_chunk)); - } - T(YAFFS_TRACE_CHECKPOINT, - (TSTR("Checkpoint read tnodes %d records, last %d. ok %d" TENDSTR), - nread, base_chunk, ok)); + yaffs_trace(YAFFS_TRACE_CHECKPOINT, + "Checkpoint read tnodes %d records, last %d. ok %d", + nread, base_chunk, ok); return ok ? 1 : 0; } @@ -572,29 +553,23 @@ static int yaffs2_wr_checkpt_objs(struct yaffs_dev *dev) for (i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++) { list_for_each(lh, &dev->obj_bucket[i].list) { - if (lh) { - obj = - list_entry(lh, struct yaffs_obj, hash_link); - if (!obj->defered_free) { - yaffs2_obj_checkpt_obj(&cp, obj); - cp.struct_type = sizeof(cp); + obj = list_entry(lh, struct yaffs_obj, hash_link); + if (!obj->defered_free) { + yaffs2_obj_checkpt_obj(&cp, obj); + cp.struct_type = sizeof(cp); - T(YAFFS_TRACE_CHECKPOINT, - (TSTR - ("Checkpoint write object %d parent %d type %d chunk %d obj addr %p" - TENDSTR), cp.obj_id, cp.parent_id, - cp.variant_type, cp.hdr_chunk, obj)); + yaffs_trace(YAFFS_TRACE_CHECKPOINT, + "Checkpoint write object %d parent %d type %d chunk %d obj addr %p", + cp.obj_id, cp.parent_id, + cp.variant_type, cp.hdr_chunk, obj); - ok = (yaffs2_checkpt_wr - (dev, &cp, - sizeof(cp)) == sizeof(cp)); + ok = (yaffs2_checkpt_wr(dev, &cp, + sizeof(cp)) == sizeof(cp)); - if (ok - && obj->variant_type == - YAFFS_OBJECT_TYPE_FILE) - ok = yaffs2_wr_checkpt_tnodes - (obj); - } + if (ok && + obj->variant_type == + YAFFS_OBJECT_TYPE_FILE) + ok = yaffs2_wr_checkpt_tnodes(obj); } } } @@ -620,21 +595,20 @@ static int yaffs2_rd_checkpt_objs(struct yaffs_dev *dev) while (ok && !done) { ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp)); if (cp.struct_type != sizeof(cp)) { - T(YAFFS_TRACE_CHECKPOINT, - (TSTR("struct size %d instead of %d ok %d" TENDSTR), - cp.struct_type, (int)sizeof(cp), ok)); + yaffs_trace(YAFFS_TRACE_CHECKPOINT, + "struct size %d instead of %d ok %d", + cp.struct_type, (int)sizeof(cp), ok); ok = 0; } - T(YAFFS_TRACE_CHECKPOINT, - (TSTR - ("Checkpoint read object %d parent %d type %d chunk %d " - TENDSTR), cp.obj_id, cp.parent_id, cp.variant_type, - cp.hdr_chunk)); + yaffs_trace(YAFFS_TRACE_CHECKPOINT, + "Checkpoint read object %d parent %d type %d chunk %d ", + cp.obj_id, cp.parent_id, cp.variant_type, + cp.hdr_chunk); - if (ok && cp.obj_id == ~0) + if (ok && cp.obj_id == ~0) { done = 1; - else if (ok) { + } else if (ok) { obj = yaffs_find_or_create_by_number(dev, cp.obj_id, cp.variant_type); @@ -642,16 +616,18 @@ static int yaffs2_rd_checkpt_objs(struct yaffs_dev *dev) ok = taffs2_checkpt_obj_to_obj(obj, &cp); if (!ok) break; - if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE) { + if (obj->variant_type == + YAFFS_OBJECT_TYPE_FILE) { ok = yaffs2_rd_checkpt_tnodes(obj); } else if (obj->variant_type == - YAFFS_OBJECT_TYPE_HARDLINK) { + YAFFS_OBJECT_TYPE_HARDLINK) { obj->hard_links.next = (struct list_head *)hard_list; hard_list = obj; } - } else + } else { ok = 0; + } } } @@ -669,7 +645,7 @@ static int yaffs2_wr_checkpt_sum(struct yaffs_dev *dev) yaffs2_get_checkpt_sum(dev, &checkpt_sum); ok = (yaffs2_checkpt_wr(dev, &checkpt_sum, sizeof(checkpt_sum)) == - sizeof(checkpt_sum)); + sizeof(checkpt_sum)); if (!ok) return 0; @@ -686,7 +662,7 @@ static int yaffs2_rd_checkpt_sum(struct yaffs_dev *dev) yaffs2_get_checkpt_sum(dev, &checkpt_sum0); ok = (yaffs2_checkpt_rd(dev, &checkpt_sum1, sizeof(checkpt_sum1)) == - sizeof(checkpt_sum1)); + sizeof(checkpt_sum1)); if (!ok) return 0; @@ -702,8 +678,8 @@ static int yaffs2_wr_checkpt_data(struct yaffs_dev *dev) int ok = 1; if (!yaffs2_checkpt_required(dev)) { - T(YAFFS_TRACE_CHECKPOINT, - (TSTR("skipping checkpoint write" TENDSTR))); + yaffs_trace(YAFFS_TRACE_CHECKPOINT, + "skipping checkpoint write"); ok = 0; } @@ -711,23 +687,23 @@ static int yaffs2_wr_checkpt_data(struct yaffs_dev *dev) ok = yaffs2_checkpt_open(dev, 1); if (ok) { - T(YAFFS_TRACE_CHECKPOINT, - (TSTR("write checkpoint validity" TENDSTR))); + yaffs_trace(YAFFS_TRACE_CHECKPOINT, + "write checkpoint validity"); ok = yaffs2_wr_checkpt_validity_marker(dev, 1); } if (ok) { - T(YAFFS_TRACE_CHECKPOINT, - (TSTR("write checkpoint device" TENDSTR))); + yaffs_trace(YAFFS_TRACE_CHECKPOINT, + "write checkpoint device"); ok = yaffs2_wr_checkpt_dev(dev); } if (ok) { - T(YAFFS_TRACE_CHECKPOINT, - (TSTR("write checkpoint objects" TENDSTR))); + yaffs_trace(YAFFS_TRACE_CHECKPOINT, + "write checkpoint objects"); ok = yaffs2_wr_checkpt_objs(dev); } if (ok) { - T(YAFFS_TRACE_CHECKPOINT, - (TSTR("write checkpoint validity" TENDSTR))); + yaffs_trace(YAFFS_TRACE_CHECKPOINT, + "write checkpoint validity"); ok = yaffs2_wr_checkpt_validity_marker(dev, 0); } @@ -753,39 +729,39 @@ static int yaffs2_rd_checkpt_data(struct yaffs_dev *dev) ok = 0; if (ok && dev->param.skip_checkpt_rd) { - T(YAFFS_TRACE_CHECKPOINT, - (TSTR("skipping checkpoint read" TENDSTR))); + yaffs_trace(YAFFS_TRACE_CHECKPOINT, + "skipping checkpoint read"); ok = 0; } if (ok) - ok = yaffs2_checkpt_open(dev, 0); /* open for read */ + ok = yaffs2_checkpt_open(dev, 0); /* open for read */ if (ok) { - T(YAFFS_TRACE_CHECKPOINT, - (TSTR("read checkpoint validity" TENDSTR))); + yaffs_trace(YAFFS_TRACE_CHECKPOINT, + "read checkpoint validity"); ok = yaffs2_rd_checkpt_validity_marker(dev, 1); } if (ok) { - T(YAFFS_TRACE_CHECKPOINT, - (TSTR("read checkpoint device" TENDSTR))); + yaffs_trace(YAFFS_TRACE_CHECKPOINT, + "read checkpoint device"); ok = yaffs2_rd_checkpt_dev(dev); } if (ok) { - T(YAFFS_TRACE_CHECKPOINT, - (TSTR("read checkpoint objects" TENDSTR))); + yaffs_trace(YAFFS_TRACE_CHECKPOINT, + "read checkpoint objects"); ok = yaffs2_rd_checkpt_objs(dev); } if (ok) { - T(YAFFS_TRACE_CHECKPOINT, - (TSTR("read checkpoint validity" TENDSTR))); + yaffs_trace(YAFFS_TRACE_CHECKPOINT, + "read checkpoint validity"); ok = yaffs2_rd_checkpt_validity_marker(dev, 0); } if (ok) { ok = yaffs2_rd_checkpt_sum(dev); - T(YAFFS_TRACE_CHECKPOINT, - (TSTR("read checkpoint checksum %d" TENDSTR), ok)); + yaffs_trace(YAFFS_TRACE_CHECKPOINT, + "read checkpoint checksum %d", ok); } if (!yaffs_checkpt_close(dev)) @@ -797,7 +773,6 @@ static int yaffs2_rd_checkpt_data(struct yaffs_dev *dev) dev->is_checkpointed = 0; return ok ? 1 : 0; - } void yaffs2_checkpt_invalidate(struct yaffs_dev *dev) @@ -812,10 +787,9 @@ void yaffs2_checkpt_invalidate(struct yaffs_dev *dev) int yaffs_checkpoint_save(struct yaffs_dev *dev) { - - T(YAFFS_TRACE_CHECKPOINT, - (TSTR("save entry: is_checkpointed %d" TENDSTR), - dev->is_checkpointed)); + yaffs_trace(YAFFS_TRACE_CHECKPOINT, + "save entry: is_checkpointed %d", + dev->is_checkpointed); yaffs_verify_objects(dev); yaffs_verify_blocks(dev); @@ -826,9 +800,9 @@ int yaffs_checkpoint_save(struct yaffs_dev *dev) yaffs2_wr_checkpt_data(dev); } - T(YAFFS_TRACE_ALWAYS, - (TSTR("save exit: is_checkpointed %d" TENDSTR), - dev->is_checkpointed)); + yaffs_trace(YAFFS_TRACE_CHECKPOINT | YAFFS_TRACE_MOUNT, + "save exit: is_checkpointed %d", + dev->is_checkpointed); return dev->is_checkpointed; } @@ -836,9 +810,10 @@ int yaffs_checkpoint_save(struct yaffs_dev *dev) int yaffs2_checkpt_restore(struct yaffs_dev *dev) { int retval; - T(YAFFS_TRACE_CHECKPOINT, - (TSTR("restore entry: is_checkpointed %d" TENDSTR), - dev->is_checkpointed)); + + yaffs_trace(YAFFS_TRACE_CHECKPOINT, + "restore entry: is_checkpointed %d", + dev->is_checkpointed); retval = yaffs2_rd_checkpt_data(dev); @@ -848,9 +823,9 @@ int yaffs2_checkpt_restore(struct yaffs_dev *dev) yaffs_verify_free_chunks(dev); } - T(YAFFS_TRACE_CHECKPOINT, - (TSTR("restore exit: is_checkpointed %d" TENDSTR), - dev->is_checkpointed)); + yaffs_trace(YAFFS_TRACE_CHECKPOINT, + "restore exit: is_checkpointed %d", + dev->is_checkpointed); return retval; } @@ -859,17 +834,15 @@ int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size) { /* if new_size > old_file_size. * We're going to be writing a hole. - * If the hole is small then write zeros otherwise write a start of hole marker. + * If the hole is small then write zeros otherwise write a start + * of hole marker. */ - loff_t old_file_size; int increase; int small_hole; int result = YAFFS_OK; struct yaffs_dev *dev = NULL; - u8 *local_buffer = NULL; - int small_increase_ok = 0; if (!obj) @@ -918,13 +891,14 @@ int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size) if (written == this_write) { pos += this_write; increase -= this_write; - } else + } else { small_increase_ok = 0; + } } yaffs_release_temp_buffer(dev, local_buffer, __LINE__); - /* If we were out of space then reverse any chunks we've added */ + /* If out of space then reverse any chunks we've added */ if (!small_increase_ok) yaffs_resize_file_down(obj, old_file_size); } @@ -938,7 +912,6 @@ int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size) } return result; - } struct yaffs_block_index { @@ -952,65 +925,454 @@ static int yaffs2_ybicmp(const void *a, const void *b) int bseq = ((struct yaffs_block_index *)b)->seq; int ablock = ((struct yaffs_block_index *)a)->block; int bblock = ((struct yaffs_block_index *)b)->block; + if (aseq == bseq) return ablock - bblock; else return aseq - bseq; } +static inline int yaffs2_scan_chunk(struct yaffs_dev *dev, + struct yaffs_block_info *bi, + int blk, int chunk_in_block, + int *found_chunks, + u8 *chunk_data, + struct yaffs_obj **hard_list) +{ + struct yaffs_obj_hdr *oh; + struct yaffs_obj *in; + struct yaffs_obj *parent; + int equiv_id; + int file_size; + int is_shrink; + int is_unlinked; + struct yaffs_ext_tags tags; + int result; + int alloc_failed = 0; + int chunk = blk * dev->param.chunks_per_block + chunk_in_block; + struct yaffs_file_var *file_var; + struct yaffs_hardlink_var *hl_var; + struct yaffs_symlink_var *sl_var; + + 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 (*found_chunks) { + /* This is a chunk that was skipped due + * to failing the erased check */ + } else if (chunk_in_block == 0) { + /* We're looking at the first chunk in + * the block so the block is unused */ + bi->block_state = YAFFS_BLOCK_STATE_EMPTY; + dev->n_erased_blocks++; + } else { + if (bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCANNING + || bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) { + if (dev->seq_number == bi->seq_number) { + /* Allocating from this block*/ + yaffs_trace(YAFFS_TRACE_SCAN, + " Allocating from %d %d", + blk, chunk_in_block); + + bi->block_state = + YAFFS_BLOCK_STATE_ALLOCATING; + dev->alloc_block = blk; + dev->alloc_page = chunk_in_block; + dev->alloc_block_finder = blk; + } else { + /* This is a partially written block + * that is not the current + * allocation block. + */ + yaffs_trace(YAFFS_TRACE_SCAN, + "Partially written block %d detected. gc will fix this.", + blk); + } + } + } + + dev->n_free_chunks++; + + } else if (tags.ecc_result == + YAFFS_ECC_RESULT_UNFIXED) { + yaffs_trace(YAFFS_TRACE_SCAN, + " Unfixed ECC in chunk(%d:%d), chunk ignored", + blk, chunk_in_block); + 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) { + yaffs_trace(YAFFS_TRACE_SCAN, + "Chunk (%d:%d) with bad tags:obj = %d, chunk_id = %d, n_bytes = %d, ignored", + blk, chunk_in_block, 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 chunk_base = (tags.chunk_id - 1) * + dev->data_bytes_per_chunk; + + *found_chunks = 1; + + yaffs_set_chunk_bit(dev, blk, chunk_in_block); + 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 && + chunk_base < 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 = chunk_base + tags.n_bytes; + + if (!in->valid && + 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 + */ + *found_chunks = 1; + + yaffs_set_chunk_bit(dev, blk, chunk_in_block); + 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, + chunk_data, + NULL); + + oh = (struct yaffs_obj_hdr *)chunk_data; + + 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! */ + yaffs_trace(YAFFS_TRACE_ERROR, + "yaffs tragedy: Could not make object for object %d at chunk %d during scan", + tags.obj_id, chunk); + return YAFFS_FAIL; + } + + 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 this_size = (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) { + this_size = 0; + is_shrink = 1; + } + + if (is_shrink && + in->variant.file_variant.shrink_size > + this_size) + in->variant.file_variant.shrink_size = + this_size; + + 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)) + yaffs_trace(YAFFS_TRACE_ERROR, + "yaffs tragedy: Bad object type, %d != %d, for object %d at chunk %d during scan", + 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; + yaffs_load_attribs(in, oh); + in->lazy_loaded = 0; + } 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; + yaffs_load_attribs(in, oh); + + 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; + INIT_LIST_HEAD(&parent-> + variant.dir_variant.children); + } else if (!parent || + parent->variant_type != + YAFFS_OBJECT_TYPE_DIRECTORY) { + /* Hoosterman, another problem.... + * Trying to use a non-directory as a directory + */ + + 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_add_obj_to_dir(parent, in); + + is_unlinked = (parent == dev->del_dir) || + (parent == dev->unlinked_dir); + + if (is_shrink) + /* Mark the block */ + 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: + file_var = &in->variant.file_variant; + if (file_var->scanned_size < file_size) { + /* This covers the case where the file + * size is greater than the data held. + * This will happen if the file is + * resized to be larger than its + * current data extents. + */ + file_var->file_size = file_size; + file_var->scanned_size = file_size; + } + + if (file_var->shrink_size > file_size) + file_var->shrink_size = file_size; + + break; + case YAFFS_OBJECT_TYPE_HARDLINK: + hl_var = &in->variant.hardlink_variant; + if (!is_unlinked) { + hl_var->equiv_id = equiv_id; + in->hard_links.next = + (struct list_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: + sl_var = &in->variant.symlink_variant; + if (oh) { + sl_var->alias = + yaffs_clone_str(oh->alias); + if (!sl_var->alias) + alloc_failed = 1; + } + break; + } + } + } + return alloc_failed ? YAFFS_FAIL : YAFFS_OK; +} + int yaffs2_scan_backwards(struct yaffs_dev *dev) { - struct yaffs_ext_tags tags; int blk; int block_iter; int start_iter; int end_iter; int n_to_scan = 0; - - int chunk; - int result; + enum yaffs_block_state state; int c; int deleted; - enum yaffs_block_state state; struct yaffs_obj *hard_list = NULL; struct yaffs_block_info *bi; u32 seq_number; - struct yaffs_obj_hdr *oh; - struct yaffs_obj *in; - struct yaffs_obj *parent; int n_blocks = dev->internal_end_block - dev->internal_start_block + 1; - int is_unlinked; u8 *chunk_data; - - int file_size; - int is_shrink; int found_chunks; - int equiv_id; int alloc_failed = 0; - struct yaffs_block_index *block_index = NULL; int alt_block_index = 0; - T(YAFFS_TRACE_SCAN, - (TSTR - ("yaffs2_scan_backwards starts intstartblk %d intendblk %d..." - TENDSTR), dev->internal_start_block, dev->internal_end_block)); + yaffs_trace(YAFFS_TRACE_SCAN, + "yaffs2_scan_backwards starts intstartblk %d intendblk %d...", + dev->internal_start_block, dev->internal_end_block); dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER; - block_index = YMALLOC(n_blocks * sizeof(struct yaffs_block_index)); + block_index = kmalloc(n_blocks * sizeof(struct yaffs_block_index), + GFP_NOFS); if (!block_index) { block_index = - YMALLOC_ALT(n_blocks * sizeof(struct yaffs_block_index)); + vmalloc(n_blocks * sizeof(struct yaffs_block_index)); alt_block_index = 1; } if (!block_index) { - T(YAFFS_TRACE_SCAN, - (TSTR - ("yaffs2_scan_backwards() could not allocate block index!" - TENDSTR))); + yaffs_trace(YAFFS_TRACE_SCAN, + "yaffs2_scan_backwards() could not allocate block index!" + ); return YAFFS_FAIL; } @@ -1032,557 +1394,94 @@ int yaffs2_scan_backwards(struct yaffs_dev *dev) bi->seq_number = seq_number; if (bi->seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA) - bi->block_state = state = YAFFS_BLOCK_STATE_CHECKPOINT; + bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT; if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK) - bi->block_state = state = YAFFS_BLOCK_STATE_DEAD; + bi->block_state = YAFFS_BLOCK_STATE_DEAD; - T(YAFFS_TRACE_SCAN_DEBUG, - (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk, - state, seq_number)); + yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, + "Block scanning block %d state %d seq %d", + blk, bi->block_state, seq_number); - if (state == YAFFS_BLOCK_STATE_CHECKPOINT) { + if (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT) { dev->blocks_in_checkpt++; - } else if (state == YAFFS_BLOCK_STATE_DEAD) { - T(YAFFS_TRACE_BAD_BLOCKS, - (TSTR("block %d is bad" TENDSTR), blk)); - } else if (state == YAFFS_BLOCK_STATE_EMPTY) { - T(YAFFS_TRACE_SCAN_DEBUG, - (TSTR("Block empty " TENDSTR))); + } else if (bi->block_state == YAFFS_BLOCK_STATE_DEAD) { + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, + "block %d is bad", blk); + } else if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) { + yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "Block empty "); dev->n_erased_blocks++; dev->n_free_chunks += dev->param.chunks_per_block; - } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) { - + } else if (bi->block_state == + YAFFS_BLOCK_STATE_NEEDS_SCANNING) { /* Determine the highest sequence number */ if (seq_number >= YAFFS_LOWEST_SEQUENCE_NUMBER && seq_number < YAFFS_HIGHEST_SEQUENCE_NUMBER) { - block_index[n_to_scan].seq = seq_number; block_index[n_to_scan].block = blk; - n_to_scan++; - if (seq_number >= dev->seq_number) dev->seq_number = seq_number; } else { /* TODO: Nasty sequence number! */ - T(YAFFS_TRACE_SCAN, - (TSTR - ("Block scanning block %d has bad sequence number %d" - TENDSTR), blk, seq_number)); - + yaffs_trace(YAFFS_TRACE_SCAN, + "Block scanning block %d has bad sequence number %d", + blk, seq_number); } } bi++; } - T(YAFFS_TRACE_SCAN, - (TSTR("%d blocks to be sorted..." TENDSTR), n_to_scan)); + yaffs_trace(YAFFS_TRACE_SCAN, "%d blocks to be sorted...", n_to_scan); - YYIELD(); + cond_resched(); /* Sort the blocks by sequence number */ - yaffs_sort(block_index, n_to_scan, sizeof(struct yaffs_block_index), - yaffs2_ybicmp); + sort(block_index, n_to_scan, sizeof(struct yaffs_block_index), + yaffs2_ybicmp, NULL); - YYIELD(); + cond_resched(); - T(YAFFS_TRACE_SCAN, (TSTR("...done" TENDSTR))); + yaffs_trace(YAFFS_TRACE_SCAN, "...done"); /* Now scan the blocks looking at the data. */ start_iter = 0; end_iter = n_to_scan - 1; - T(YAFFS_TRACE_SCAN_DEBUG, - (TSTR("%d blocks to be scanned" TENDSTR), n_to_scan)); + yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "%d blocks to scan", n_to_scan); /* For each block.... backwards */ for (block_iter = end_iter; !alloc_failed && block_iter >= start_iter; block_iter--) { /* Cooperative multitasking! This loop can run for so long that watchdog timers expire. */ - YYIELD(); + cond_resched(); /* get the block to scan in the correct order */ blk = block_index[block_iter].block; - bi = yaffs_get_block_info(dev, blk); - - state = bi->block_state; - deleted = 0; /* For each chunk in each block that needs scanning.... */ found_chunks = 0; for (c = dev->param.chunks_per_block - 1; !alloc_failed && c >= 0 && - (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING || - state == YAFFS_BLOCK_STATE_ALLOCATING); c--) { + (bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCANNING || + bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING); c--) { /* Scan backwards... * Read the tags and decide what to do */ - - 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 (found_chunks) { - /* 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 chunk_base = - (tags.chunk_id - - 1) * dev->data_bytes_per_chunk; - - found_chunks = 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 - && chunk_base < - 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 = chunk_base + 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 - */ - found_chunks = 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, - chunk_data, - NULL); - - oh = (struct yaffs_obj_hdr *)chunk_data; - - 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 this_size = - (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) { - this_size = 0; - is_shrink = 1; - } - - if (is_shrink - && in->variant.file_variant. - shrink_size > this_size) - in->variant. - file_variant. - shrink_size = - this_size; - - 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; - yaffs_load_attribs(in, oh); - in->lazy_loaded = 0; - } 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; - yaffs_load_attribs(in, oh); - - 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; - INIT_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); - - is_unlinked = (parent == dev->del_dir) - || (parent == dev->unlinked_dir); - - if (is_shrink) { - /* Mark the block as having a shrink header */ - 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 (!is_unlinked) { - in->variant. - hardlink_variant. - equiv_id = equiv_id; - in->hard_links.next = - (struct list_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; + if (yaffs2_scan_chunk(dev, bi, blk, c, + &found_chunks, chunk_data, + &hard_list) == + YAFFS_FAIL) + alloc_failed = 1; } - bi->block_state = state; + if (bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) { + /* If we got this far while scanning, then the block + * is fully allocated. */ + bi->block_state = YAFFS_BLOCK_STATE_FULL; + } /* Now let's see if it was dirty */ if (bi->pages_in_use == 0 && @@ -1590,19 +1489,18 @@ int yaffs2_scan_backwards(struct yaffs_dev *dev) bi->block_state == YAFFS_BLOCK_STATE_FULL) { yaffs_block_became_dirty(dev, blk); } - } yaffs_skip_rest_of_block(dev); if (alt_block_index) - YFREE_ALT(block_index); + vfree(block_index); else - YFREE(block_index); + kfree(block_index); /* 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 + * We have scanned all the objects, now it's time to add these * hardlinks. */ yaffs_link_fixup(dev, hard_list); @@ -1612,7 +1510,7 @@ int yaffs2_scan_backwards(struct yaffs_dev *dev) if (alloc_failed) return YAFFS_FAIL; - T(YAFFS_TRACE_SCAN, (TSTR("yaffs2_scan_backwards ends" TENDSTR))); + yaffs_trace(YAFFS_TRACE_SCAN, "yaffs2_scan_backwards ends"); return YAFFS_OK; } diff --git a/fs/yaffs2/yportenv.h b/fs/yaffs2/yportenv.h index ec43fd58..309b146c 100644 --- a/fs/yaffs2/yportenv.h +++ b/fs/yaffs2/yportenv.h @@ -24,15 +24,9 @@ #define MTD_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c)) -#if defined CONFIG_YAFFS_WINCE - -#include "ywinceenv.h" - -#elif defined __KERNEL__ - +#ifdef YAFFS_OUT_OF_TREE #include "moduleconfig.h" - -/* Linux kernel */ +#endif #include #define MTD_VERSION_CODE LINUX_VERSION_CODE @@ -53,33 +47,15 @@ #include #include #include +#include #define YCHAR char #define YUCHAR unsigned char #define _Y(x) x -#define yaffs_strcat(a, b) strcat(a, b) -#define yaffs_strcpy(a, b) strcpy(a, b) -#define yaffs_strncpy(a, b, c) strncpy(a, b, c) -#define yaffs_strncmp(a, b, c) strncmp(a, b, c) -#define yaffs_strnlen(s,m) strnlen(s,m) -#define yaffs_sprintf sprintf -#define yaffs_toupper(a) toupper(a) -#define yaffs_sort(base, n, sz, cmp_fn) sort(base, n, sz, cmp_fn, NULL) - -#define Y_INLINE __inline__ #define YAFFS_LOSTNFOUND_NAME "lost+found" #define YAFFS_LOSTNFOUND_PREFIX "obj" -/* #define YPRINTF(x) printk x */ -#define YMALLOC(x) kmalloc(x, GFP_NOFS) -#define YFREE(x) kfree(x) -#define YMALLOC_ALT(x) vmalloc(x) -#define YFREE_ALT(x) vfree(x) -#define YMALLOC_DMA(x) YMALLOC(x) - -#define YYIELD() schedule() -#define Y_DUMP_STACK() dump_stack() #define YAFFS_ROOT_MODE 0755 #define YAFFS_LOSTNFOUND_MODE 0700 @@ -92,246 +68,24 @@ #define Y_TIME_CONVERT(x) (x) #endif -#define yaffs_sum_cmp(x, y) ((x) == (y)) -#define yaffs_strcmp(a, b) strcmp(a, b) - -#define TENDSTR "\n" -#define TSTR(x) KERN_DEBUG x -#define TCONT(x) x -#define TOUT(p) printk p - #define compile_time_assertion(assertion) \ ({ int x = __builtin_choose_expr(assertion, 0, (void)0); (void) x; }) -#elif defined CONFIG_YAFFS_DIRECT - -#define MTD_VERSION_CODE MTD_VERSION(2, 6, 22) - -/* Direct interface */ -#include "ydirectenv.h" - -#elif defined CONFIG_YAFFS_UTIL - -/* Stuff for YAFFS utilities */ - -#include "stdlib.h" -#include "stdio.h" -#include "string.h" - -#define YMALLOC(x) malloc(x) -#define YFREE(x) free(x) -#define YMALLOC_ALT(x) malloc(x) -#define YFREE_ALT(x) free(x) - -#define YCHAR char -#define YUCHAR unsigned char -#define _Y(x) x -#define yaffs_strcat(a, b) strcat(a, b) -#define yaffs_strcpy(a, b) strcpy(a, b) -#define yaffs_strncpy(a, b, c) strncpy(a, b, c) -#define yaffs_strnlen(s,m) strnlen(s,m) -#define yaffs_sprintf sprintf -#define yaffs_toupper(a) toupper(a) - -#define Y_INLINE inline - -/* #define YINFO(s) YPRINTF(( __FILE__ " %d %s\n",__LINE__,s)) */ -/* #define YALERT(s) YINFO(s) */ - -#define TENDSTR "\n" -#define TSTR(x) x -#define TOUT(p) printf p - -#define YAFFS_LOSTNFOUND_NAME "lost+found" -#define YAFFS_LOSTNFOUND_PREFIX "obj" -/* #define YPRINTF(x) printf x */ - -#define YAFFS_ROOT_MODE 0755 -#define YAFFS_LOSTNFOUND_MODE 0700 - -#define yaffs_sum_cmp(x, y) ((x) == (y)) -#define yaffs_strcmp(a, b) strcmp(a, b) - -#else -/* Should have specified a configuration type */ -#error Unknown configuration - -#endif - -#if defined(CONFIG_YAFFS_DIRECT) || defined(CONFIG_YAFFS_WINCE) - -#ifdef CONFIG_YAFFSFS_PROVIDE_VALUES - -#ifndef O_RDONLY -#define O_RDONLY 00 -#endif - -#ifndef O_WRONLY -#define O_WRONLY 01 -#endif - -#ifndef O_RDWR -#define O_RDWR 02 -#endif - -#ifndef O_CREAT -#define O_CREAT 0100 -#endif - -#ifndef O_EXCL -#define O_EXCL 0200 -#endif - -#ifndef O_TRUNC -#define O_TRUNC 01000 -#endif - -#ifndef O_APPEND -#define O_APPEND 02000 -#endif - -#ifndef SEEK_SET -#define SEEK_SET 0 -#endif - -#ifndef SEEK_CUR -#define SEEK_CUR 1 -#endif - -#ifndef SEEK_END -#define SEEK_END 2 -#endif - -#ifndef EBUSY -#define EBUSY 16 -#endif - -#ifndef ENODEV -#define ENODEV 19 -#endif - -#ifndef EINVAL -#define EINVAL 22 -#endif - -#ifndef ENFILE -#define ENFILE 23 -#endif - -#ifndef EBADF -#define EBADF 9 -#endif - -#ifndef EACCES -#define EACCES 13 -#endif - -#ifndef EXDEV -#define EXDEV 18 -#endif - -#ifndef ENOENT -#define ENOENT 2 -#endif - -#ifndef ENOSPC -#define ENOSPC 28 -#endif - -#ifndef ERANGE -#define ERANGE 34 -#endif - -#ifndef ENODATA -#define ENODATA 61 -#endif - -#ifndef ENOTEMPTY -#define ENOTEMPTY 39 -#endif - -#ifndef ENAMETOOLONG -#define ENAMETOOLONG 36 -#endif - -#ifndef ENOMEM -#define ENOMEM 12 -#endif - -#ifndef EEXIST -#define EEXIST 17 -#endif - -#ifndef ENOTDIR -#define ENOTDIR 20 -#endif - -#ifndef EISDIR -#define EISDIR 21 -#endif - -// Mode flags - -#ifndef S_IFMT -#define S_IFMT 0170000 -#endif - -#ifndef S_IFLNK -#define S_IFLNK 0120000 -#endif - -#ifndef S_IFDIR -#define S_IFDIR 0040000 -#endif - -#ifndef S_IFREG -#define S_IFREG 0100000 -#endif - -#ifndef S_IREAD -#define S_IREAD 0000400 -#endif - -#ifndef S_IWRITE -#define S_IWRITE 0000200 -#endif - -#ifndef S_IEXEC -#define S_IEXEC 0000100 -#endif - -#ifndef XATTR_CREATE -#define XATTR_CREATE 1 -#endif - -#ifndef XATTR_REPLACE -#define XATTR_REPLACE 2 -#endif - -#ifndef R_OK -#define R_OK 4 -#define W_OK 2 -#define X_OK 1 -#define F_OK 0 -#endif - -#else -#include -#include -#include -#endif - -#endif #ifndef Y_DUMP_STACK -#define Y_DUMP_STACK() do { } while (0) +#define Y_DUMP_STACK() dump_stack() #endif +#define yaffs_trace(msk, fmt, ...) do { \ + if (yaffs_trace_mask & (msk)) \ + printk(KERN_DEBUG "yaffs: " fmt "\n", ##__VA_ARGS__); \ +} while (0) + #ifndef YBUG #define YBUG() do {\ - T(YAFFS_TRACE_BUG,\ - (TSTR("==>> yaffs bug: " __FILE__ " %d" TENDSTR),\ - __LINE__));\ + yaffs_trace(YAFFS_TRACE_BUG,\ + "bug " __FILE__ " %d",\ + __LINE__);\ Y_DUMP_STACK();\ } while (0) #endif