yaffsfs.c: Fix NULL dereference in yaffs_unmount2_reldev()
[yaffs2.git] / yaffs_yaffs2.c
index 32840e76566803c2b50cd2d955252597d8624edd..6be3d6a0c6208f082e0fd1a4a22845c97ba118c2 100644 (file)
@@ -1,8 +1,7 @@
 /*
  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  *
- * Copyright (C) 2002-2010 Aleph One Ltd.
- *   for Toby Churchill Ltd and Brightstar Engineering
+ * Copyright (C) 2002-2018 Aleph One Ltd.
  *
  * Created by Charles Manning <charles@aleph1.co.uk>
  *
@@ -20,6 +19,8 @@
 #include "yaffs_getblockinfo.h"
 #include "yaffs_verify.h"
 #include "yaffs_attribs.h"
+#include "yaffs_summary.h"
+#include "yaffs_endian.h"
 
 /*
  * Checkpoints are really no benefit on very small partitions.
@@ -40,7 +41,7 @@
  */
 void yaffs_calc_oldest_dirty_seq(struct yaffs_dev *dev)
 {
-       int i;
+       u32 i;
        unsigned seq;
        unsigned block_no = 0;
        struct yaffs_block_info *b;
@@ -53,7 +54,7 @@ void yaffs_calc_oldest_dirty_seq(struct yaffs_dev *dev)
        b = dev->block_info;
        for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
                if (b->block_state == YAFFS_BLOCK_STATE_FULL &&
-                   (b->pages_in_use - b->soft_del_pages) <
+                   (u32)(b->pages_in_use - b->soft_del_pages) <
                    dev->param.chunks_per_block &&
                    b->seq_number < seq) {
                        seq = b->seq_number;
@@ -247,6 +248,18 @@ int yaffs_calc_checkpt_blocks_required(struct yaffs_dev *dev)
 
 /*--------------------- Checkpointing --------------------*/
 
+static void yaffs2_do_endian_validity_marker(struct yaffs_dev *dev,
+                                            struct yaffs_checkpt_validity *v)
+{
+
+       if (!dev->swap_endian)
+               return;
+       v->struct_type = swap_s32(v->struct_type);
+       v->magic = swap_u32(v->magic);
+       v->version = swap_u32(v->version);
+       v->head = swap_u32(v->head);
+}
+
 static int yaffs2_wr_checkpt_validity_marker(struct yaffs_dev *dev, int head)
 {
        struct yaffs_checkpt_validity cp;
@@ -258,6 +271,8 @@ static int yaffs2_wr_checkpt_validity_marker(struct yaffs_dev *dev, int head)
        cp.version = YAFFS_CHECKPOINT_VERSION;
        cp.head = (head) ? 1 : 0;
 
+       yaffs2_do_endian_validity_marker(dev, &cp);
+
        return (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp)) ? 1 : 0;
 }
 
@@ -267,6 +282,7 @@ static int yaffs2_rd_checkpt_validity_marker(struct yaffs_dev *dev, int head)
        int ok;
 
        ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
+       yaffs2_do_endian_validity_marker(dev, &cp);
 
        if (ok)
                ok = (cp.struct_type == sizeof(cp)) &&
@@ -279,6 +295,8 @@ static int yaffs2_rd_checkpt_validity_marker(struct yaffs_dev *dev, int head)
 static void yaffs2_dev_to_checkpt_dev(struct yaffs_checkpt_dev *cp,
                                      struct yaffs_dev *dev)
 {
+       cp->struct_type = sizeof(*cp);
+
        cp->n_erased_blocks = dev->n_erased_blocks;
        cp->alloc_block = dev->alloc_block;
        cp->alloc_page = dev->alloc_page;
@@ -305,30 +323,67 @@ static void yaffs_checkpt_dev_to_dev(struct yaffs_dev *dev,
        dev->seq_number = cp->seq_number;
 }
 
+static void yaffs2_do_endian_checkpt_dev(struct yaffs_dev *dev,
+                                    struct yaffs_checkpt_dev *cp)
+{
+       if (!dev->swap_endian)
+               return;
+       cp->struct_type = swap_s32(cp->struct_type);
+       cp->n_erased_blocks = swap_s32(cp->n_erased_blocks);
+       cp->alloc_block = swap_s32(cp->alloc_block);
+       cp->alloc_page = swap_u32(cp->alloc_page);
+       cp->n_free_chunks = swap_s32(cp->n_free_chunks);
+       cp->n_deleted_files = swap_s32(cp->n_deleted_files);
+       cp->n_unlinked_files = swap_s32(cp->n_unlinked_files);
+       cp->n_bg_deletions = swap_s32(cp->n_bg_deletions);
+}
+
 static int yaffs2_wr_checkpt_dev(struct yaffs_dev *dev)
 {
        struct yaffs_checkpt_dev cp;
        u32 n_bytes;
        u32 n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
        int ok;
+       u32 i;
+       union yaffs_block_info_union bu;
 
        /* Write device runtime values */
        yaffs2_dev_to_checkpt_dev(&cp, dev);
-       cp.struct_type = sizeof(cp);
+       yaffs2_do_endian_checkpt_dev(dev, &cp);
 
        ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
        if (!ok)
                return 0;
 
-       /* Write block info */
-       n_bytes = n_blocks * sizeof(struct yaffs_block_info);
-       ok = (yaffs2_checkpt_wr(dev, dev->block_info, n_bytes) == n_bytes);
+       /* Write block info. */
+       if (!dev->swap_endian) {
+               n_bytes = n_blocks * sizeof(struct yaffs_block_info);
+               ok = (yaffs2_checkpt_wr(dev, dev->block_info, n_bytes) ==
+                       (int)n_bytes);
+       } else {
+               /*
+                * Need to swap the endianisms. We can't do this in place
+                * since that would damage live data,
+                * so write one block info at a time using a copy.
+                */
+               for (i = 0; i < n_blocks && ok; i++) {
+                       bu.bi = dev->block_info[i];
+                       bu.as_u32[0] = swap_u32(bu.as_u32[0]);
+                       bu.as_u32[1] = swap_u32(bu.as_u32[1]);
+                       ok = (yaffs2_checkpt_wr(dev, &bu, sizeof(bu)) == sizeof(bu));
+               }
+       }
+
        if (!ok)
                return 0;
 
-       /* Write chunk bits */
+       /*
+        * Write chunk bits. Chunk bits are in bytes so
+        * no endian conversion is needed.
+        */
        n_bytes = n_blocks * dev->chunk_bit_stride;
-       ok = (yaffs2_checkpt_wr(dev, dev->chunk_bits, n_bytes) == n_bytes);
+       ok = (yaffs2_checkpt_wr(dev, dev->chunk_bits, n_bytes) ==
+               (int)n_bytes);
 
        return ok ? 1 : 0;
 }
@@ -344,6 +399,7 @@ static int yaffs2_rd_checkpt_dev(struct yaffs_dev *dev)
        ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
        if (!ok)
                return 0;
+       yaffs2_do_endian_checkpt_dev(dev, &cp);
 
        if (cp.struct_type != sizeof(cp))
                return 0;
@@ -352,32 +408,72 @@ static int yaffs2_rd_checkpt_dev(struct yaffs_dev *dev)
 
        n_bytes = n_blocks * sizeof(struct yaffs_block_info);
 
-       ok = (yaffs2_checkpt_rd(dev, dev->block_info, n_bytes) == n_bytes);
+       ok = (yaffs2_checkpt_rd(dev, dev->block_info, n_bytes) ==
+               (int)n_bytes);
 
        if (!ok)
                return 0;
 
+       if (dev->swap_endian) {
+               /* The block info can just be handled as a list of u32s. */
+               u32 *as_u32 = (u32 *) dev->block_info;
+               u32 n_u32s = n_bytes/sizeof(u32);
+               u32 i;
+
+               for (i=0; i < n_u32s; i++)
+                       as_u32[i] = swap_u32(as_u32[i]);
+       }
+
        n_bytes = n_blocks * dev->chunk_bit_stride;
 
-       ok = (yaffs2_checkpt_rd(dev, dev->chunk_bits, n_bytes) == n_bytes);
+       ok = (yaffs2_checkpt_rd(dev, dev->chunk_bits, n_bytes) ==
+               (int)n_bytes);
+
 
        return ok ? 1 : 0;
 }
 
+
+static void yaffs2_checkpt_obj_bit_assign(struct yaffs_checkpt_obj *cp,
+                                         int bit_offset,
+                                         int bit_width,
+                                         u32 value)
+{
+       u32 and_mask;
+
+       and_mask = ((1<<bit_width)-1) << bit_offset;
+
+       cp->bit_field &= ~and_mask;
+       cp->bit_field |= ((value << bit_offset) & and_mask);
+}
+
+static u32 yaffs2_checkpt_obj_bit_get(struct yaffs_checkpt_obj *cp,
+                                     int bit_offset,
+                                     int bit_width)
+{
+       u32 and_mask;
+
+       and_mask = ((1<<bit_width)-1);
+
+       return (cp->bit_field >> bit_offset) & and_mask;
+}
+
 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;
-       cp->variant_type = obj->variant_type;
-       cp->deleted = obj->deleted;
-       cp->soft_del = obj->soft_del;
-       cp->unlinked = obj->unlinked;
-       cp->fake = obj->fake;
-       cp->rename_allowed = obj->rename_allowed;
-       cp->unlink_allowed = obj->unlink_allowed;
-       cp->serial = obj->serial;
+
+       yaffs2_checkpt_obj_bit_assign(cp, CHECKPOINT_VARIANT_BITS, obj->variant_type);
+       yaffs2_checkpt_obj_bit_assign(cp, CHECKPOINT_DELETED_BITS, obj->deleted);
+       yaffs2_checkpt_obj_bit_assign(cp, CHECKPOINT_SOFT_DEL_BITS, obj->soft_del);
+       yaffs2_checkpt_obj_bit_assign(cp, CHECKPOINT_UNLINKED_BITS, obj->unlinked);
+       yaffs2_checkpt_obj_bit_assign(cp, CHECKPOINT_FAKE_BITS, obj->fake);
+       yaffs2_checkpt_obj_bit_assign(cp, CHECKPOINT_RENAME_ALLOWED_BITS, obj->rename_allowed);
+       yaffs2_checkpt_obj_bit_assign(cp, CHECKPOINT_UNLINK_ALLOWED_BITS, obj->unlink_allowed);
+       yaffs2_checkpt_obj_bit_assign(cp, CHECKPOINT_SERIAL_BITS, obj->serial);
+
        cp->n_data_chunks = obj->n_data_chunks;
 
        if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
@@ -390,11 +486,12 @@ static int yaffs2_checkpt_obj_to_obj(struct yaffs_obj *obj,
                                     struct yaffs_checkpt_obj *cp)
 {
        struct yaffs_obj *parent;
+       u32 cp_variant_type = yaffs2_checkpt_obj_bit_get(cp, CHECKPOINT_VARIANT_BITS);
 
-       if (obj->variant_type != cp->variant_type) {
+       if (obj->variant_type != cp_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,
+                       cp->obj_id, cp_variant_type, cp->hdr_chunk,
                        obj->variant_type);
                return 0;
        }
@@ -413,7 +510,7 @@ static int yaffs2_checkpt_obj_to_obj(struct yaffs_obj *obj,
                        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,
+                               cp_variant_type, cp->hdr_chunk,
                                parent->variant_type);
                        return 0;
                }
@@ -421,26 +518,56 @@ static int yaffs2_checkpt_obj_to_obj(struct yaffs_obj *obj,
        }
 
        obj->hdr_chunk = cp->hdr_chunk;
-       obj->variant_type = cp->variant_type;
-       obj->deleted = cp->deleted;
-       obj->soft_del = cp->soft_del;
-       obj->unlinked = cp->unlinked;
-       obj->fake = cp->fake;
-       obj->rename_allowed = cp->rename_allowed;
-       obj->unlink_allowed = cp->unlink_allowed;
-       obj->serial = cp->serial;
+
+       obj->variant_type = yaffs2_checkpt_obj_bit_get(cp, CHECKPOINT_VARIANT_BITS);
+       obj->deleted = yaffs2_checkpt_obj_bit_get(cp, CHECKPOINT_DELETED_BITS);
+       obj->soft_del = yaffs2_checkpt_obj_bit_get(cp, CHECKPOINT_SOFT_DEL_BITS);
+       obj->unlinked = yaffs2_checkpt_obj_bit_get(cp, CHECKPOINT_UNLINKED_BITS);
+       obj->fake = yaffs2_checkpt_obj_bit_get(cp, CHECKPOINT_FAKE_BITS);
+       obj->rename_allowed = yaffs2_checkpt_obj_bit_get(cp, CHECKPOINT_RENAME_ALLOWED_BITS);
+       obj->unlink_allowed = yaffs2_checkpt_obj_bit_get(cp, CHECKPOINT_UNLINK_ALLOWED_BITS);
+       obj->serial = yaffs2_checkpt_obj_bit_get(cp, CHECKPOINT_SERIAL_BITS);
+
        obj->n_data_chunks = cp->n_data_chunks;
 
-       if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
+       if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE) {
                obj->variant.file_variant.file_size = cp->size_or_equiv_obj;
-       else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK)
+               obj->variant.file_variant.stored_size = cp->size_or_equiv_obj;
+       } else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
                obj->variant.hardlink_variant.equiv_id = cp->size_or_equiv_obj;
-
+       }
        if (obj->hdr_chunk > 0)
                obj->lazy_loaded = 1;
        return 1;
 }
 
+static void yaffs2_do_endian_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
+{
+       int i;
+       u32 *as_u32 = (u32 *)tn;
+       int tnode_size_u32 = dev->tnode_size / sizeof(u32);
+
+       if (!dev->swap_endian)
+               return;
+       /* Swap all the tnode data as u32s to fix endianisms. */
+       for (i = 0; i<tnode_size_u32; i++)
+               as_u32[i] = swap_u32(as_u32[i]);
+}
+
+struct yaffs_tnode *yaffs2_do_endian_tnode_copy(struct yaffs_dev *dev,
+                                              struct yaffs_tnode *tn)
+{
+       if (!dev->swap_endian)
+               return tn;
+
+       memcpy(dev->tn_swap_buffer, tn, dev->tnode_size);
+       tn = dev->tn_swap_buffer;
+
+       yaffs2_do_endian_tnode(dev, tn);
+
+       return tn;
+}
+
 static int yaffs2_checkpt_tnode_worker(struct yaffs_obj *in,
                                       struct yaffs_tnode *tn, u32 level,
                                       int chunk_offset)
@@ -468,12 +595,20 @@ static int yaffs2_checkpt_tnode_worker(struct yaffs_obj *in,
 
        /* Level 0 tnode */
        base_offset = chunk_offset << YAFFS_TNODES_LEVEL0_BITS;
+       yaffs_do_endian_u32(dev, &base_offset);
+
        ok = (yaffs2_checkpt_wr(dev, &base_offset, sizeof(base_offset)) ==
                        sizeof(base_offset));
-       if (ok)
+       if (ok) {
+               /*
+                * NB Can't do an in-place endian swizzle since that would
+                * damage current tnode data.
+                * If a tnode endian conversion is required we do a copy.
+                */
+               tn = yaffs2_do_endian_tnode_copy(dev, tn);
                ok = (yaffs2_checkpt_wr(dev, tn, dev->tnode_size) ==
-                       dev->tnode_size);
-
+                       (int)dev->tnode_size);
+       }
        return ok;
 }
 
@@ -508,14 +643,18 @@ static int yaffs2_rd_checkpt_tnodes(struct yaffs_obj *obj)
        ok = (yaffs2_checkpt_rd(dev, &base_chunk, sizeof(base_chunk)) ==
              sizeof(base_chunk));
 
+       yaffs_do_endian_u32(dev, &base_chunk);
+
        while (ok && (~base_chunk)) {
                nread++;
                /* 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);
+                               (int)dev->tnode_size);
+                       yaffs2_do_endian_tnode(dev, tn);
+               }
                else
                        ok = 0;
 
@@ -524,10 +663,13 @@ static int yaffs2_rd_checkpt_tnodes(struct yaffs_obj *obj)
                                                    file_stuct_ptr,
                                                    base_chunk, tn) ? 1 : 0;
 
-               if (ok)
+               if (ok) {
                        ok = (yaffs2_checkpt_rd
                              (dev, &base_chunk,
                               sizeof(base_chunk)) == sizeof(base_chunk));
+                       yaffs_do_endian_u32(dev, &base_chunk);
+               }
+
        }
 
        yaffs_trace(YAFFS_TRACE_CHECKPOINT,
@@ -537,6 +679,21 @@ static int yaffs2_rd_checkpt_tnodes(struct yaffs_obj *obj)
        return ok ? 1 : 0;
 }
 
+
+static void yaffs2_do_endian_checkpt_obj(struct yaffs_dev *dev,
+                                        struct yaffs_checkpt_obj *cp)
+{
+       if (!dev->swap_endian)
+               return;
+       cp->struct_type = swap_s32(cp->struct_type);
+       cp->obj_id = swap_u32(cp->obj_id);
+       cp->parent_id = swap_u32(cp->parent_id);
+       cp->hdr_chunk = swap_s32(cp->hdr_chunk);
+       cp->bit_field = swap_u32(cp->bit_field);
+       cp->n_data_chunks = swap_s32(cp->n_data_chunks);
+       cp->size_or_equiv_obj = swap_loff_t(cp->size_or_equiv_obj);
+}
+
 static int yaffs2_wr_checkpt_objs(struct yaffs_dev *dev)
 {
        struct yaffs_obj *obj;
@@ -544,23 +701,28 @@ static int yaffs2_wr_checkpt_objs(struct yaffs_dev *dev)
        int i;
        int ok = 1;
        struct list_head *lh;
+       u32 cp_variant_type;
 
        /* Iterate through the objects in each hash entry,
         * dumping them to the checkpointing stream.
         */
 
+       (void) cp_variant_type;
+
        for (i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++) {
                list_for_each(lh, &dev->obj_bucket[i].list) {
                        obj = list_entry(lh, struct yaffs_obj, hash_link);
                        if (!obj->defered_free) {
                                yaffs2_obj_checkpt_obj(&cp, obj);
                                cp.struct_type = sizeof(cp);
-
+                               cp_variant_type = yaffs2_checkpt_obj_bit_get(
+                                               &cp, CHECKPOINT_VARIANT_BITS);
                                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);
+                                       cp_variant_type, cp.hdr_chunk, obj);
 
+                               yaffs2_do_endian_checkpt_obj (dev, &cp);
                                ok = (yaffs2_checkpt_wr(dev, &cp,
                                                sizeof(cp)) == sizeof(cp));
 
@@ -573,8 +735,9 @@ static int yaffs2_wr_checkpt_objs(struct yaffs_dev *dev)
        }
 
        /* Dump end of list */
-       memset(&cp, 0xFF, sizeof(struct yaffs_checkpt_obj));
+       memset(&cp, 0xff, sizeof(struct yaffs_checkpt_obj));
        cp.struct_type = sizeof(cp);
+       yaffs2_do_endian_checkpt_obj (dev, &cp);
 
        if (ok)
                ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
@@ -588,11 +751,14 @@ static int yaffs2_rd_checkpt_objs(struct yaffs_dev *dev)
        struct yaffs_checkpt_obj cp;
        int ok = 1;
        int done = 0;
+       u32 cp_variant_type;
        LIST_HEAD(hard_list);
 
 
        while (ok && !done) {
                ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
+               yaffs2_do_endian_checkpt_obj (dev, &cp);
+
                if (cp.struct_type != sizeof(cp)) {
                        yaffs_trace(YAFFS_TRACE_CHECKPOINT,
                                "struct size %d instead of %d ok %d",
@@ -600,17 +766,19 @@ static int yaffs2_rd_checkpt_objs(struct yaffs_dev *dev)
                        ok = 0;
                }
 
+               cp_variant_type = yaffs2_checkpt_obj_bit_get(
+                                               &cp, CHECKPOINT_VARIANT_BITS);
                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.obj_id, cp.parent_id, cp_variant_type,
                        cp.hdr_chunk);
 
-               if (ok && cp.obj_id == ~0) {
+               if (ok && cp.obj_id == (u32)(~0)) {
                        done = 1;
                } else if (ok) {
                        obj =
                            yaffs_find_or_create_by_number(dev, cp.obj_id,
-                                                          cp.variant_type);
+                                                          cp_variant_type);
                        if (obj) {
                                ok = yaffs2_checkpt_obj_to_obj(obj, &cp);
                                if (!ok)
@@ -641,6 +809,8 @@ static int yaffs2_wr_checkpt_sum(struct yaffs_dev *dev)
 
        yaffs2_get_checkpt_sum(dev, &checkpt_sum);
 
+       yaffs_do_endian_u32(dev, &checkpt_sum);
+
        ok = (yaffs2_checkpt_wr(dev, &checkpt_sum, sizeof(checkpt_sum)) ==
                sizeof(checkpt_sum));
 
@@ -663,6 +833,7 @@ static int yaffs2_rd_checkpt_sum(struct yaffs_dev *dev)
 
        if (!ok)
                return 0;
+       yaffs_do_endian_u32(dev, &checkpt_sum1);
 
        if (checkpt_sum0 != checkpt_sum1)
                return 0;
@@ -827,6 +998,10 @@ int yaffs2_checkpt_restore(struct yaffs_dev *dev)
        return retval;
 }
 
+/* End of checkpointing */
+
+/* Hole handling logic for truncate past end of file */
+
 int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size)
 {
        /* if new_size > old_file_size.
@@ -835,7 +1010,7 @@ int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size)
         * of hole marker.
         */
        loff_t old_file_size;
-       int increase;
+       loff_t increase;
        int small_hole;
        int result = YAFFS_OK;
        struct yaffs_dev *dev = NULL;
@@ -868,11 +1043,11 @@ int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size)
                small_hole = 0;
 
        if (small_hole)
-               local_buffer = yaffs_get_temp_buffer(dev, __LINE__);
+               local_buffer = yaffs_get_temp_buffer(dev);
 
        if (local_buffer) {
                /* fill hole with zero bytes */
-               int pos = old_file_size;
+               loff_t pos = old_file_size;
                int this_write;
                int written;
                memset(local_buffer, 0, dev->data_bytes_per_chunk);
@@ -880,7 +1055,7 @@ int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size)
 
                while (increase > 0 && small_increase_ok) {
                        this_write = increase;
-                       if (this_write > dev->data_bytes_per_chunk)
+                       if (this_write > (int)dev->data_bytes_per_chunk)
                                this_write = dev->data_bytes_per_chunk;
                        written =
                            yaffs_do_file_wr(obj, local_buffer, pos, this_write,
@@ -893,7 +1068,7 @@ int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size)
                        }
                }
 
-               yaffs_release_temp_buffer(dev, local_buffer, __LINE__);
+               yaffs_release_temp_buffer(dev, local_buffer);
 
                /* If out of space then reverse any chunks we've added */
                if (!small_increase_ok)
@@ -911,6 +1086,8 @@ int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size)
        return result;
 }
 
+/* Yaffs2 scanning */
+
 struct yaffs_block_index {
        int seq;
        int block;
@@ -934,13 +1111,14 @@ static inline int yaffs2_scan_chunk(struct yaffs_dev *dev,
                int blk, int chunk_in_block,
                int *found_chunks,
                u8 *chunk_data,
-               struct list_head *hard_list)
+               struct list_head *hard_list,
+               int summary_available)
 {
        struct yaffs_obj_hdr *oh;
        struct yaffs_obj *in;
        struct yaffs_obj *parent;
        int equiv_id;
-       int file_size;
+       loff_t file_size;
        int is_shrink;
        int is_unlinked;
        struct yaffs_ext_tags tags;
@@ -951,8 +1129,21 @@ static inline int yaffs2_scan_chunk(struct yaffs_dev *dev,
        struct yaffs_hardlink_var *hl_var;
        struct yaffs_symlink_var *sl_var;
 
-       result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL, &tags);
+       if (summary_available) {
+               result = yaffs_summary_fetch(dev, &tags, chunk_in_block);
+               tags.seq_number = bi->seq_number;
+       }
+
+       if (!summary_available || tags.obj_id == 0) {
+               result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL, &tags);
+               dev->tags_used++;
+       } else {
+               dev->summary_used++;
+       }
 
+       if (result == YAFFS_FAIL)
+               yaffs_trace(YAFFS_TRACE_SCAN,
+                               "Could not get tags for chunk %d\n", chunk);
        /* Let's have a good look at this chunk... */
 
        if (!tags.chunk_used) {
@@ -1011,6 +1202,7 @@ static inline int yaffs2_scan_chunk(struct yaffs_dev *dev,
                        dev->n_free_chunks++;
        } else if (tags.obj_id > YAFFS_MAX_OBJECT_ID ||
                   tags.chunk_id > YAFFS_MAX_CHUNK_ID ||
+                  tags.obj_id == YAFFS_OBJECTID_SUMMARY ||
                   (tags.chunk_id > 0 &&
                     tags.n_bytes > dev->data_bytes_per_chunk) ||
                   tags.seq_number != bi->seq_number) {
@@ -1021,8 +1213,8 @@ static inline int yaffs2_scan_chunk(struct yaffs_dev *dev,
                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) *
+               loff_t endpos;
+               loff_t chunk_base = (tags.chunk_id - 1) *
                                        dev->data_bytes_per_chunk;
 
                *found_chunks = 1;
@@ -1055,9 +1247,9 @@ static inline int yaffs2_scan_chunk(struct yaffs_dev *dev,
                        endpos = chunk_base + tags.n_bytes;
 
                        if (!in->valid &&
-                           in->variant.file_variant.scanned_size < endpos) {
+                           in->variant.file_variant.stored_size < endpos) {
                                in->variant.file_variant.
-                                   scanned_size = endpos;
+                                   stored_size = endpos;
                                in->variant.file_variant.
                                    file_size = endpos;
                        }
@@ -1108,6 +1300,8 @@ static inline int yaffs2_scan_chunk(struct yaffs_dev *dev,
 
                        oh = (struct yaffs_obj_hdr *)chunk_data;
 
+                       yaffs_do_endian_oh(dev, oh);
+
                        if (dev->param.inband_tags) {
                                /* Fix up the header if they got
                                 * corrupted by inband tags */
@@ -1144,11 +1338,11 @@ static inline int yaffs2_scan_chunk(struct yaffs_dev *dev,
                                 (tags.extra_available &&
                                  tags.extra_obj_type == YAFFS_OBJECT_TYPE_FILE)
                                )) {
-                               u32 this_size = (oh) ?
-                                       oh->file_size :
-                                       tags.extra_length;
+                               loff_t this_size = (oh) ?
+                                       yaffs_oh_to_size(dev, oh, 0) :
+                                       tags.extra_file_size;
                                u32 parent_obj_id = (oh) ?
-                                       oh->parent_obj_id :
+                                       (u32)oh->parent_obj_id :
                                        tags.extra_parent_id;
 
                                is_shrink = (oh) ?
@@ -1180,12 +1374,14 @@ static inline int yaffs2_scan_chunk(struct yaffs_dev *dev,
                }
 
                if (!in->valid && in->variant_type !=
-                   (oh ? oh->type : tags.extra_obj_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",
+                               "yaffs tragedy: Bad 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);
+                       in = yaffs_retype_obj(in, oh ? oh->type : tags.extra_obj_type);
+               }
 
                if (!in->valid &&
                    (tags.obj_id == YAFFS_OBJECTID_ROOT ||
@@ -1220,7 +1416,7 @@ static inline int yaffs2_scan_chunk(struct yaffs_dev *dev,
                                parent = yaffs_find_or_create_by_number(dev,
                                                oh->parent_obj_id,
                                                YAFFS_OBJECT_TYPE_DIRECTORY);
-                               file_size = oh->file_size;
+                               file_size = yaffs_oh_to_size(dev, oh, 0);
                                is_shrink = oh->is_shrink;
                                equiv_id = oh->equiv_id;
                        } else {
@@ -1228,7 +1424,7 @@ static inline int yaffs2_scan_chunk(struct yaffs_dev *dev,
                                parent = yaffs_find_or_create_by_number(dev,
                                                tags.extra_parent_id,
                                                YAFFS_OBJECT_TYPE_DIRECTORY);
-                               file_size = tags.extra_length;
+                               file_size = tags.extra_file_size;
                                is_shrink = tags.extra_is_shrink;
                                equiv_id = tags.extra_equiv_id;
                                in->lazy_loaded = 1;
@@ -1284,7 +1480,7 @@ static inline int yaffs2_scan_chunk(struct yaffs_dev *dev,
                                break;
                        case YAFFS_OBJECT_TYPE_FILE:
                                file_var = &in->variant.file_variant;
-                               if (file_var->scanned_size < file_size) {
+                               if (file_var->stored_size < file_size) {
                                        /* This covers the case where the file
                                         * size is greater than the data held.
                                         * This will happen if the file is
@@ -1292,7 +1488,7 @@ static inline int yaffs2_scan_chunk(struct yaffs_dev *dev,
                                         * current data extents.
                                         */
                                        file_var->file_size = file_size;
-                                       file_var->scanned_size = file_size;
+                                       file_var->stored_size = file_size;
                                }
 
                                if (file_var->shrink_size > file_size)
@@ -1329,14 +1525,13 @@ static inline int yaffs2_scan_chunk(struct yaffs_dev *dev,
 
 int yaffs2_scan_backwards(struct yaffs_dev *dev)
 {
-       int blk;
+       u32 blk;
        int block_iter;
        int start_iter;
        int end_iter;
        int n_to_scan = 0;
        enum yaffs_block_state state;
        int c;
-       int deleted;
        LIST_HEAD(hard_list);
        struct yaffs_block_info *bi;
        u32 seq_number;
@@ -1346,6 +1541,7 @@ int yaffs2_scan_backwards(struct yaffs_dev *dev)
        int alloc_failed = 0;
        struct yaffs_block_index *block_index = NULL;
        int alt_block_index = 0;
+       int summary_available;
 
        yaffs_trace(YAFFS_TRACE_SCAN,
                "yaffs2_scan_backwards starts  intstartblk %d intendblk %d...",
@@ -1371,7 +1567,7 @@ int yaffs2_scan_backwards(struct yaffs_dev *dev)
 
        dev->blocks_in_checkpt = 0;
 
-       chunk_data = yaffs_get_temp_buffer(dev, __LINE__);
+       chunk_data = yaffs_get_temp_buffer(dev);
 
        /* Scan all the blocks to determine their state */
        bi = dev->block_info;
@@ -1425,7 +1621,7 @@ int yaffs2_scan_backwards(struct yaffs_dev *dev)
                bi++;
        }
 
-       yaffs_trace(YAFFS_TRACE_SCAN, "%d blocks to be sorted...", n_to_scan);
+       yaffs_trace(YAFFS_TRACE_ALWAYS, "%d blocks to be sorted...", n_to_scan);
 
        cond_resched();
 
@@ -1453,20 +1649,28 @@ int yaffs2_scan_backwards(struct yaffs_dev *dev)
                /* get the block to scan in the correct order */
                blk = block_index[block_iter].block;
                bi = yaffs_get_block_info(dev, blk);
-               deleted = 0;
+
+               summary_available = yaffs_summary_read(dev, dev->sum_tags, blk);
 
                /* For each chunk in each block that needs scanning.... */
                found_chunks = 0;
-               for (c = dev->param.chunks_per_block - 1;
+               if (summary_available)
+                       c = dev->chunks_per_summary - 1;
+               else
+                       c = dev->param.chunks_per_block - 1;
+
+               for (/* c is already initialised */;
                     !alloc_failed && c >= 0 &&
                     (bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN ||
-                     bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING); c--) {
+                     bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING);
+                     c--) {
                        /* Scan backwards...
                         * Read the tags and decide what to do
                         */
                        if (yaffs2_scan_chunk(dev, bi, blk, c,
                                        &found_chunks, chunk_data,
-                                       &hard_list) == YAFFS_FAIL)
+                                       &hard_list, summary_available) ==
+                                       YAFFS_FAIL)
                                alloc_failed = 1;
                }
 
@@ -1498,7 +1702,7 @@ int yaffs2_scan_backwards(struct yaffs_dev *dev)
         */
        yaffs_link_fixup(dev, &hard_list);
 
-       yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
+       yaffs_release_temp_buffer(dev, chunk_data);
 
        if (alloc_failed)
                return YAFFS_FAIL;