yaffs2: Yaffs endian support
[yaffs2.git] / yaffs_yaffs1.c
index b6c43e5..e98d04d 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  *
- * Copyright (C) 2002-2010 Aleph One Ltd.
+ * Copyright (C) 2002-2011 Aleph One Ltd.
  *   for Toby Churchill Ltd and Brightstar Engineering
  *
  * Created by Charles Manning <charles@aleph1.co.uk>
@@ -24,29 +24,25 @@ int yaffs1_scan(struct yaffs_dev *dev)
        struct yaffs_ext_tags tags;
        int blk;
        int result;
-
        int chunk;
        int c;
        int deleted;
        enum yaffs_block_state state;
-       struct yaffs_obj *hard_list = NULL;
+       LIST_HEAD(hard_list);
        struct yaffs_block_info *bi;
        u32 seq_number;
        struct yaffs_obj_hdr *oh;
        struct yaffs_obj *in;
        struct yaffs_obj *parent;
-
        int alloc_failed = 0;
-
        struct yaffs_shadow_fixer *shadow_fixers = NULL;
-
        u8 *chunk_data;
 
-       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__);
+       chunk_data = yaffs_get_temp_buffer(dev);
 
        dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
 
@@ -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;
@@ -94,8 +89,9 @@ int yaffs1_scan(struct yaffs_dev *dev)
                deleted = 0;
 
                /* For each chunk in each block that needs scanning.... */
-               for (c = 0; !alloc_failed && c < dev->param.chunks_per_block &&
-                    state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) {
+               for (c = 0;
+                       !alloc_failed && c < dev->param.chunks_per_block &&
+                       state == YAFFS_BLOCK_STATE_NEEDS_SCAN; c++) {
                        /* Read the tags and decide what to do */
                        chunk = blk * dev->param.chunks_per_block + c;
 
@@ -104,14 +100,13 @@ int yaffs1_scan(struct yaffs_dev *dev)
 
                        /* Let's have a good look at this chunk... */
 
-                       if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED
-                           || tags.is_deleted) {
+                       if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED ||
+                           tags.is_deleted) {
                                /* YAFFS1 only...
                                 * A deleted chunk
                                 */
                                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 +114,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 +140,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,28 +156,27 @@ 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 <
-                                   endpos) {
-                                       in->variant.file_variant.scanned_size =
+                               if (in &&
+                                   in->variant_type ==
+                                    YAFFS_OBJECT_TYPE_FILE &&
+                                   in->variant.file_variant.stored_size <
+                                     endpos) {
+                                       in->variant.file_variant.stored_size =
                                            endpos;
                                        if (!dev->param.use_header_file_size) {
                                                in->variant.
                                                    file_variant.file_size =
                                                    in->variant.
-                                                   file_variant.scanned_size;
+                                                   file_variant.stored_size;
                                        }
 
                                }
-                               /* 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 +190,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 +211,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 +240,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 +258,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,36 +298,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 */
@@ -337,20 +323,16 @@ int yaffs1_scan(struct yaffs_dev *dev)
                                        case YAFFS_OBJECT_TYPE_FILE:
                                                if (dev->param.
                                                    use_header_file_size)
-
                                                        in->variant.
-                                                           file_variant.file_size
-                                                           = oh->file_size;
-
+                                                       file_variant.file_size
+                                                       = yaffs_oh_to_size(dev, oh, 0);
                                                break;
                                        case YAFFS_OBJECT_TYPE_HARDLINK:
                                                in->variant.
                                                    hardlink_variant.equiv_id =
                                                    oh->equiv_id;
-                                               in->hard_links.next =
-                                                   (struct list_head *)
-                                                   hard_list;
-                                               hard_list = in;
+                                               list_add(&in->hard_links,
+                                                               &hard_list);
                                                break;
                                        case YAFFS_OBJECT_TYPE_DIRECTORY:
                                                /* Do nothing */
@@ -367,18 +349,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 (state == YAFFS_BLOCK_STATE_NEEDS_SCAN) {
+                       /* If we got this far while scanning,
+                        * then the block is fully allocated. */
                        state = YAFFS_BLOCK_STATE_FULL;
                }
 
                if (state == YAFFS_BLOCK_STATE_ALLOCATING) {
-                       /* If the block was partially allocated then treat it as fully allocated. */
+                       /* If the block was partially allocated then
+                        * treat it as fully allocated. */
                        state = YAFFS_BLOCK_STATE_FULL;
                        dev->alloc_block = -1;
                }
@@ -388,21 +371,22 @@ int yaffs1_scan(struct yaffs_dev *dev)
                /* Now let's see if it was dirty */
                if (bi->pages_in_use == 0 &&
                    !bi->has_shrink_hdr &&
-                   bi->block_state == YAFFS_BLOCK_STATE_FULL) {
+                   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);
+       yaffs_link_fixup(dev, &hard_list);
 
-       /* Fix up any shadowed objects */
+       /*
+        * Fix up any shadowed objects.
+        * There should not be more than one of these.
+        */
        {
                struct yaffs_shadow_fixer *fixer;
                struct yaffs_obj *obj;
@@ -410,8 +394,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)
@@ -422,16 +407,16 @@ int yaffs1_scan(struct yaffs_dev *dev)
                        if (obj)
                                yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
 
-                       YFREE(fixer);
+                       kfree(fixer);
                }
        }
 
-       yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
+       yaffs_release_temp_buffer(dev, chunk_data);
 
        if (alloc_failed)
                return YAFFS_FAIL;
 
-       T(YAFFS_TRACE_SCAN, (TSTR("yaffs1_scan ends" TENDSTR)));
+       yaffs_trace(YAFFS_TRACE_SCAN, "yaffs1_scan ends");
 
        return YAFFS_OK;
 }