Fix some statics and remove unused function
[yaffs2.git] / yaffs_checkptrw.c
index 5f5a4758e5559f259b6d7f7ca5b1d99a265d32d2..d8970449b04812de994f85737f749fe6120c0e7b 100644 (file)
 #include "yaffs_checkptrw.h"
 #include "yaffs_getblockinfo.h"
 
+struct yaffs_checkpt_chunk_hdr {
+       int version;
+       int seq;
+       u32 sum;
+       u32 xor;
+} ;
+
+static void yaffs2_checkpt_init_chunk_hdr(struct yaffs_dev *dev)
+{
+       struct yaffs_checkpt_chunk_hdr hdr;
+
+       hdr.version = YAFFS_CHECKPOINT_VERSION;
+       hdr.seq = dev->checkpt_page_seq;
+       hdr.sum = dev->checkpt_sum;
+       hdr.xor = dev->checkpt_xor;
+
+       dev->checkpt_byte_offs = sizeof(hdr);
+
+       memcpy(dev->checkpt_buffer, &hdr, sizeof(hdr));
+}
+
+static int yaffs2_checkpt_check_chunk_hdr(struct yaffs_dev *dev)
+{
+       struct yaffs_checkpt_chunk_hdr hdr;
+
+       memcpy(&hdr, dev->checkpt_buffer, sizeof(hdr));
+
+       dev->checkpt_byte_offs = sizeof(hdr);
+
+       return hdr.version == YAFFS_CHECKPOINT_VERSION &&
+               hdr.seq == dev->checkpt_page_seq &&
+               hdr.sum == dev->checkpt_sum &&
+               hdr.xor == dev->checkpt_xor;
+}
+
 static int yaffs2_checkpt_space_ok(struct yaffs_dev *dev)
 {
        int blocks_avail = dev->n_erased_blocks - dev->param.n_reserved_blocks;
@@ -108,25 +143,32 @@ static void yaffs2_checkpt_find_block(struct yaffs_dev *dev)
                     i++) {
                        int chunk = i * dev->param.chunks_per_block;
                        int realigned_chunk = chunk - dev->chunk_offset;
+                       enum yaffs_block_state state;
+                       u32 seq;
 
                        dev->param.read_chunk_tags_fn(dev, realigned_chunk,
                                                      NULL, &tags);
                        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,
+                               "find next checkpt block: search: block %d state %d oid %d seq %d eccr %d",
+                               i, (int) state,
+                               tags.obj_id, tags.seq_number,
                                tags.ecc_result);
 
-                       if (tags.seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA) {
-                               /* Right kind of block */
-                               dev->checkpt_next_block = tags.obj_id;
-                               dev->checkpt_cur_block = i;
-                               dev->checkpt_block_list[dev->
-                                                       blocks_in_checkpt] = i;
-                               dev->blocks_in_checkpt++;
-                               yaffs_trace(YAFFS_TRACE_CHECKPOINT,
-                                       "found checkpt block %d", i);
-                               return;
-                       }
+                       if (tags.seq_number != YAFFS_SEQUENCE_CHECKPOINT_DATA)
+                               continue;
+
+                       dev->param.query_block_fn(dev, i, &state, &seq);
+                       if (state == YAFFS_BLOCK_STATE_DEAD)
+                               continue;
+
+                       /* Right kind of block */
+                       dev->checkpt_next_block = tags.obj_id;
+                       dev->checkpt_cur_block = i;
+                       dev->checkpt_block_list[dev->blocks_in_checkpt] = i;
+                       dev->blocks_in_checkpt++;
+                       yaffs_trace(YAFFS_TRACE_CHECKPOINT,
+                               "found checkpt block %d", i);
+                       return;
                }
 
        yaffs_trace(YAFFS_TRACE_CHECKPOINT, "found no more checkpt blocks");
@@ -164,13 +206,13 @@ int yaffs2_checkpt_open(struct yaffs_dev *dev, int writing)
        dev->checkpt_cur_chunk = -1;
        dev->checkpt_next_block = dev->internal_start_block;
 
-       /* Erase all the blocks in the checkpoint area */
        if (writing) {
                memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
-               dev->checkpt_byte_offs = 0;
+               yaffs2_checkpt_init_chunk_hdr(dev);
                return yaffs_checkpt_erase(dev);
        }
 
+       /* Opening for a read */
        /* 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
@@ -194,7 +236,7 @@ int yaffs2_get_checkpt_sum(struct yaffs_dev *dev, u32 * sum)
 {
        u32 composite_sum;
 
-       composite_sum = (dev->checkpt_sum << 8) | (dev->checkpt_xor & 0xFF);
+       composite_sum = (dev->checkpt_sum << 8) | (dev->checkpt_xor & 0xff);
        *sum = composite_sum;
        return 1;
 }
@@ -242,7 +284,6 @@ static int yaffs2_checkpt_flush_buffer(struct yaffs_dev *dev)
 
        dev->param.write_chunk_tags_fn(dev, realigned_chunk,
                                       dev->checkpt_buffer, &tags);
-       dev->checkpt_byte_offs = 0;
        dev->checkpt_page_seq++;
        dev->checkpt_cur_chunk++;
        if (dev->checkpt_cur_chunk >= dev->param.chunks_per_block) {
@@ -251,6 +292,9 @@ static int yaffs2_checkpt_flush_buffer(struct yaffs_dev *dev)
        }
        memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
 
+       yaffs2_checkpt_init_chunk_hdr(dev);
+
+
        return 1;
 }
 
@@ -333,14 +377,18 @@ int yaffs2_checkpt_rd(struct yaffs_dev *dev, void *data, int n_bytes)
                                ok = 0;
                                break;
                        }
+                       if(!yaffs2_checkpt_check_chunk_hdr(dev)) {
+                               ok = 0;
+                               break;
+                       }
 
-                       dev->checkpt_byte_offs = 0;
                        dev->checkpt_page_seq++;
                        dev->checkpt_cur_chunk++;
 
                        if (dev->checkpt_cur_chunk >=
                                        dev->param.chunks_per_block)
                                dev->checkpt_cur_block = -1;
+
                }
 
                *data_bytes = dev->checkpt_buffer[dev->checkpt_byte_offs];
@@ -360,7 +408,8 @@ int yaffs_checkpt_close(struct yaffs_dev *dev)
        int i;
 
        if (dev->checkpt_open_write) {
-               if (dev->checkpt_byte_offs != 0)
+               if (dev->checkpt_byte_offs !=
+                       sizeof(sizeof(struct yaffs_checkpt_chunk_hdr)))
                        yaffs2_checkpt_flush_buffer(dev);
        } else if (dev->checkpt_block_list) {
                for (i = 0;