Merge branch 'master' of ssh://www.aleph1.co.uk/home/aleph1/git/yaffs2
authorCharles Manning <cdhmanning@gmail.com>
Mon, 26 Mar 2012 02:37:25 +0000 (15:37 +1300)
committerCharles Manning <cdhmanning@gmail.com>
Mon, 26 Mar 2012 02:37:25 +0000 (15:37 +1300)
direct/basic-test/dtest.c
yaffs_guts.c
yaffs_guts.h
yaffs_summary.c

index d3c9ba549d6479948a907b1a299997d6949465d4..3739acac2ae30b145a8a0f08133bd000ff1aeeff 100644 (file)
@@ -2976,8 +2976,11 @@ void verify_big_sparse_file(int h)
 
 void large_file_test(const char *mountpt)
 {
+       char xx_buffer[1000];
+       int i;
        int handle;
        char fullname[100];
+       loff_t file_end;
 
        yaffs_trace_mask = 0;
 
@@ -3021,6 +3024,21 @@ void large_file_test(const char *mountpt)
        verify_big_sparse_file(handle);
        yaffs_unmount(mountpt);
 
+       /* Check resize by adding to the end, resizing back and verifying. */
+       yaffs_mount_common(mountpt, 0, 1);
+       printf("checking resize\n");
+        dumpDir(mountpt);
+       handle = yaffs_open(fullname, O_RDWR, 0);
+
+       file_end = yaffs_lseek(handle, 0, SEEK_END);
+       printf("file_end %lld\n", file_end);
+       for(i = 0; i < 10000; i++)
+               yaffs_write(handle, xx_buffer, sizeof(xx_buffer));
+       yaffs_ftruncate(handle, file_end);
+
+       verify_big_sparse_file(handle);
+       yaffs_unmount(mountpt);
+
 }
 
 
@@ -3092,10 +3110,10 @@ int main(int argc, char *argv[])
         //basic_utime_test("/yaffs2");
 
         //max_files_test("/yaffs2");
-        
-        start_twice("/yaffs2");
 
-        //large_file_test("/yaffs2");
+        //start_twice("/yaffs2");
+
+        large_file_test("/yaffs2");
 
         //basic_utime_test("/yaffs2");
         //case_insensitive_test("/yaffs2");
index ae37c3fe1c140a844360d7bcd10496fab676ed6b..6ad45fce6c1113d0ab8d926b40a3f68aee0f7475 100644 (file)
@@ -3687,17 +3687,26 @@ int yaffs_wr_file(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
 
 /* ---------------------- File resizing stuff ------------------ */
 
-static void yaffs_prune_chunks(struct yaffs_obj *in, int new_size)
+static void yaffs_prune_chunks(struct yaffs_obj *in, loff_t new_size)
 {
 
        struct yaffs_dev *dev = in->my_dev;
-       int old_size = in->variant.file_variant.file_size;
+       loff_t 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;
+       u32 dummy;
+       int last_del;
+       int start_del;
+
+       if(old_size > 0)
+               yaffs_addr_to_chunk(dev, old_size - 1, &last_del, &dummy);
+       else
+               last_del = 0;
 
+       yaffs_addr_to_chunk(dev, new_size + dev->data_bytes_per_chunk - 1,
+                               &start_del, &dummy);
+       last_del++;
+       start_del++;
 
        /* Delete backwards so that we don't end up with holes if
         * power is lost part-way through the operation.
index b3bc574a51b29fe2b99159898e9ea27157a765bd..a9fdde2213c46b315b56ab333754852867b4cab7 100644 (file)
@@ -75,7 +75,9 @@
 #define YAFFS_OBJECT_SPACE             0x40000
 #define YAFFS_MAX_OBJECT_ID            (YAFFS_OBJECT_SPACE - 1)
 
-#define YAFFS_CHECKPOINT_VERSION       5
+/* Binary data version stamps */
+#define YAFFS_SUMMARY_VERSION          1
+#define YAFFS_CHECKPOINT_VERSION       6
 
 #ifdef CONFIG_YAFFS_UNICODE
 #define YAFFS_MAX_NAME_LENGTH          127
index 4cf171117766db42f27330030ae4035b7a19a8c4..def00525a0ed7c2cc1e21e625993860da8d3776e 100644 (file)
 #include "yaffs_getblockinfo.h"
 #include "yaffs_bitmap.h"
 
+/*
+ * The summary is built up in an array of summary tags.
+ * This gets written to the last one or two (maybe more) chunks in a block.
+ * A summary header is written as the first part of each chunk of summary data.
+ * The summary header must match or the summary is rejected.
+ */
 /* Summary tags don't need the sequence number because that is redundant. */
 struct yaffs_summary_tags {
        unsigned obj_id;
@@ -36,6 +43,15 @@ struct yaffs_summary_tags {
        unsigned n_bytes;
 };
 
+/* Summary header */
+struct yaffs_summary_header {
+       unsigned version;       /* Must match current version */
+       unsigned block;         /* Must be this block */
+       unsigned seq;           /* Must be this sequence number */
+       unsigned sum;           /* Just add up all the bytes in the tags */
+};
+
+
 static void yaffs_summary_clear(struct yaffs_dev *dev)
 {
        if(!dev->sum_tags)
@@ -44,24 +60,36 @@ static void yaffs_summary_clear(struct yaffs_dev *dev)
                sizeof(struct yaffs_summary_tags));
 }
 
+
+void yaffs_summary_deinit(struct yaffs_dev *dev)
+{
+       kfree(dev->sum_tags);
+       dev->sum_tags = NULL;
+       kfree(dev->gc_sum_tags);
+       dev->gc_sum_tags = NULL;
+       dev->chunks_per_summary = 0;
+}
+
 int yaffs_summary_init(struct yaffs_dev *dev)
 {
        int sum_bytes;
        int chunks_used; /* Number of chunks used by summary */
+       int sum_tags_bytes;
 
        sum_bytes = dev->param.chunks_per_block *
                        sizeof(struct yaffs_summary_tags);
 
        chunks_used = (sum_bytes + dev->data_bytes_per_chunk - 1)/
-                       dev->data_bytes_per_chunk;
+                       (dev->data_bytes_per_chunk - 
+                               sizeof(struct yaffs_summary_header));
+
        dev->chunks_per_summary = dev->param.chunks_per_block - chunks_used;
-       dev->sum_tags = kmalloc(sizeof(struct yaffs_summary_tags) *
-                               dev->chunks_per_summary, GFP_NOFS);
-       dev->gc_sum_tags = kmalloc(sizeof(struct yaffs_summary_tags) *
-                               dev->chunks_per_summary, GFP_NOFS);
+       sum_tags_bytes = sizeof(struct yaffs_summary_tags) *
+                               dev->chunks_per_summary;
+       dev->sum_tags = kmalloc(sum_tags_bytes, GFP_NOFS);
+       dev->gc_sum_tags = kmalloc(sum_tags_bytes, GFP_NOFS);
        if(!dev->sum_tags || !dev->gc_sum_tags) {
-               kfree(dev->sum_tags);
-               kfree(dev->gc_sum_tags);
+               yaffs_summary_deinit(dev);
                return YAFFS_FAIL;
        }
 
@@ -70,13 +98,21 @@ int yaffs_summary_init(struct yaffs_dev *dev)
        return YAFFS_OK;
 }
 
-void yaffs_summary_deinit(struct yaffs_dev *dev)
+static unsigned yaffs_summary_sum(struct yaffs_dev *dev)
 {
-       kfree(dev->sum_tags);
-       dev->sum_tags = NULL;
-       kfree(dev->gc_sum_tags);
-       dev->gc_sum_tags = NULL;
-       dev->chunks_per_summary = 0;
+       u8 *sum_buffer = (u8 *)dev->sum_tags;
+       int i;
+       unsigned sum = 0;
+
+       i = sizeof(struct yaffs_summary_tags) *
+                               dev->chunks_per_summary;;
+       while(i > 0){
+               sum += *sum_buffer;
+               sum_buffer++;
+               i--;
+       }
+
+       return sum;     
 }
 
 static int yaffs_summary_write(struct yaffs_dev *dev, int blk)
@@ -89,22 +125,31 @@ static int yaffs_summary_write(struct yaffs_dev *dev, int blk)
        int chunk_in_block;
        int result;
        int this_tx;
+       struct yaffs_summary_header hdr;
+       int sum_bytes_per_chunk = dev->data_bytes_per_chunk - sizeof(hdr);
        struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
 
        buffer = yaffs_get_temp_buffer(dev);
-       n_bytes = sizeof(struct yaffs_summary_tags) * dev->chunks_per_summary;
+       n_bytes = sizeof(struct yaffs_summary_tags) *
+                               dev->chunks_per_summary;
        memset(&tags, 0, sizeof(struct yaffs_ext_tags));
        tags.obj_id = YAFFS_OBJECTID_SUMMARY;
        tags.chunk_id = 1;
        chunk_in_block = dev->chunks_per_summary;
        chunk_in_nand = dev->alloc_block * dev->param.chunks_per_block +
                                                dev-> chunks_per_summary;
+       hdr.version = YAFFS_SUMMARY_VERSION;
+       hdr.block = blk;
+       hdr.seq = bi->seq_number;
+       hdr.sum = yaffs_summary_sum(dev);
+       
        do {
                this_tx = n_bytes;
-               if (this_tx > dev->data_bytes_per_chunk)
-                       this_tx = dev->data_bytes_per_chunk;
-               memcpy(buffer, sum_buffer, this_tx);
-               tags.n_bytes = this_tx;
+               if (this_tx > sum_bytes_per_chunk)
+                       this_tx = sum_bytes_per_chunk;
+               memcpy(buffer, &hdr, sizeof(hdr));
+               memcpy(buffer + sizeof(hdr), sum_buffer, this_tx);
+               tags.n_bytes = this_tx + sizeof(hdr);
                result = yaffs_wr_chunk_tags_nand(dev, chunk_in_nand,
                                                buffer, &tags);
 
@@ -143,8 +188,13 @@ int yaffs_summary_read(struct yaffs_dev *dev,
        int chunk_in_block;
        int result;
        int this_tx;
+       struct yaffs_summary_header hdr;
        struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
+       int sum_bytes_per_chunk = dev->data_bytes_per_chunk - sizeof(hdr);
+       int sum_tags_bytes;
 
+       sum_tags_bytes = sizeof(struct yaffs_summary_tags) *
+                               dev->chunks_per_summary;
        buffer = yaffs_get_temp_buffer(dev);
        n_bytes = sizeof(struct yaffs_summary_tags) * dev->chunks_per_summary;
        chunk_in_block = dev->chunks_per_summary;
@@ -153,8 +203,8 @@ int yaffs_summary_read(struct yaffs_dev *dev,
        chunk_id = 1;
        do {
                this_tx = n_bytes;
-               if(this_tx > dev->data_bytes_per_chunk)
-                       this_tx = dev->data_bytes_per_chunk;
+               if(this_tx > sum_bytes_per_chunk)
+                       this_tx = sum_bytes_per_chunk;
                result = yaffs_rd_chunk_tags_nand(dev, chunk_in_nand,
                                                buffer, &tags);
 
@@ -162,7 +212,7 @@ int yaffs_summary_read(struct yaffs_dev *dev,
                        tags.obj_id != YAFFS_OBJECTID_SUMMARY ||
                        tags.chunk_used == 0 ||
                        tags.ecc_result > YAFFS_ECC_RESULT_FIXED ||
-                       this_tx != tags.n_bytes)
+                       tags.n_bytes != (this_tx + sizeof(hdr)))
                                result = YAFFS_FAIL;
                if (result != YAFFS_OK)
                        break;
@@ -172,8 +222,8 @@ int yaffs_summary_read(struct yaffs_dev *dev,
                        yaffs_set_chunk_bit(dev, blk, chunk_in_block);
                        bi->pages_in_use++;
                }
-
-               memcpy(sum_buffer, buffer, this_tx);
+               memcpy(&hdr, buffer, sizeof(hdr));
+               memcpy(sum_buffer, buffer + sizeof(hdr), this_tx);
                n_bytes -= this_tx;
                sum_buffer += this_tx;
                chunk_in_nand++;
@@ -182,11 +232,21 @@ int yaffs_summary_read(struct yaffs_dev *dev,
        } while (result == YAFFS_OK && n_bytes > 0);
        yaffs_release_temp_buffer(dev, buffer);
 
+       if(result == YAFFS_OK) {
+               /* Verify header */             
+               if (hdr.version != YAFFS_SUMMARY_VERSION ||
+                   hdr.block != blk ||
+                   hdr.seq != bi->seq_number ||
+                   hdr.sum != yaffs_summary_sum(dev))
+                   result = YAFFS_FAIL;
+       }
+       
        if (st == dev->sum_tags && result == YAFFS_OK)
                bi->has_summary = 1;
 
        return result;
 }
+
 int yaffs_summary_add(struct yaffs_dev *dev,
                        struct yaffs_ext_tags *tags,
                        int chunk_in_nand)