- 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
- * 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)));
- YBUG();
- }
-
- yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
- return YAFFS_OK;
- }
-
- tn = yaffs_add_find_tnode_0(dev,
- &in->variant.file_variant,
- inode_chunk, NULL);
- if (!tn)
- return YAFFS_FAIL;
-
- if (!nand_chunk)
- /* Dummy insert, bail now */
- return YAFFS_OK;
-
- existing_cunk = yaffs_get_group_base(dev, tn, 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
- * 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.
- */
-
- 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.
- *
- * 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.
- */
-
- if (in_scan > 0) {
- /* Only do this for forward scanning */
- yaffs_rd_chunk_tags_nand(dev,
- nand_chunk,
- NULL, &new_tags);
-
- /* Do a proper find */
- existing_cunk =
- yaffs_find_chunk_in_file(in, inode_chunk,
- &existing_tags);
- }
-
- if (existing_cunk <= 0) {
- /*Hoosterman - how did this happen? */
-
- T(YAFFS_TRACE_ERROR,
- (TSTR
- ("yaffs tragedy: existing chunk < 0 in scan"
- TENDSTR)));
-
- }
-
- /* NB The deleted flags should be false, otherwise the chunks will
- * not be loaded during a scan
- */
-
- if (in_scan > 0) {
- new_serial = new_tags.serial_number;
- existing_serial = existing_tags.serial_number;
- }
-
- if ((in_scan > 0) &&
- (existing_cunk <= 0 ||
- ((existing_serial + 1) & 3) == new_serial)) {
- /* Forward scanning.
- * Use new
- * 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
- */
- yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
- return YAFFS_OK;
- }
- }
-
- }
-
- if (existing_cunk == 0)
- in->n_data_chunks++;
-
- yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk);
-
- return YAFFS_OK;
-}
-
-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 {
- 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;
- }
-
-}
-
-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) {
- 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);
- }
-
- }
-
-}
-
-static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
- const u8 * buffer, int n_bytes, int use_reserve)
-{
- /* 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;
-