William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 1 | /* |
| 2 | * YAFFS: Yet Another Flash File System. A NAND-flash specific file system. |
| 3 | * |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 4 | * Copyright (C) 2002-2011 Aleph One Ltd. |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 5 | * for Toby Churchill Ltd and Brightstar Engineering |
| 6 | * |
| 7 | * Created by Charles Manning <charles@aleph1.co.uk> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 14 | #include "yaffs_checkptrw.h" |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 15 | #include "yaffs_getblockinfo.h" |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 16 | #include <dm/devres.h> |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 17 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 18 | static int yaffs2_checkpt_space_ok(struct yaffs_dev *dev) |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 19 | { |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 20 | int blocks_avail = dev->n_erased_blocks - dev->param.n_reserved_blocks; |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 21 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 22 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, |
| 23 | "checkpt blocks_avail = %d", blocks_avail); |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 24 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 25 | return (blocks_avail <= 0) ? 0 : 1; |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 26 | } |
| 27 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 28 | static int yaffs_checkpt_erase(struct yaffs_dev *dev) |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 29 | { |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 30 | int i; |
| 31 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 32 | if (!dev->param.erase_fn) |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 33 | return 0; |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 34 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, |
| 35 | "checking blocks %d to %d", |
| 36 | dev->internal_start_block, dev->internal_end_block); |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 37 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 38 | for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) { |
| 39 | struct yaffs_block_info *bi = yaffs_get_block_info(dev, i); |
| 40 | if (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT) { |
| 41 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, |
| 42 | "erasing checkpt block %d", i); |
| 43 | |
| 44 | dev->n_erasures++; |
| 45 | |
| 46 | if (dev->param. |
| 47 | erase_fn(dev, |
| 48 | i - dev->block_offset /* realign */)) { |
| 49 | bi->block_state = YAFFS_BLOCK_STATE_EMPTY; |
| 50 | dev->n_erased_blocks++; |
| 51 | dev->n_free_chunks += |
| 52 | dev->param.chunks_per_block; |
| 53 | } else { |
| 54 | dev->param.bad_block_fn(dev, i); |
| 55 | bi->block_state = YAFFS_BLOCK_STATE_DEAD; |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | } |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 59 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 60 | dev->blocks_in_checkpt = 0; |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 61 | |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 62 | return 1; |
| 63 | } |
| 64 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 65 | static void yaffs2_checkpt_find_erased_block(struct yaffs_dev *dev) |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 66 | { |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 67 | int i; |
| 68 | int blocks_avail = dev->n_erased_blocks - dev->param.n_reserved_blocks; |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 69 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 70 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, |
| 71 | "allocating checkpt block: erased %d reserved %d avail %d next %d ", |
| 72 | dev->n_erased_blocks, dev->param.n_reserved_blocks, |
| 73 | blocks_avail, dev->checkpt_next_block); |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 74 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 75 | if (dev->checkpt_next_block >= 0 && |
| 76 | dev->checkpt_next_block <= dev->internal_end_block && |
| 77 | blocks_avail > 0) { |
| 78 | |
| 79 | for (i = dev->checkpt_next_block; i <= dev->internal_end_block; |
| 80 | i++) { |
| 81 | struct yaffs_block_info *bi = |
| 82 | yaffs_get_block_info(dev, i); |
| 83 | if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) { |
| 84 | dev->checkpt_next_block = i + 1; |
| 85 | dev->checkpt_cur_block = i; |
| 86 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, |
| 87 | "allocating checkpt block %d", i); |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 88 | return; |
| 89 | } |
| 90 | } |
| 91 | } |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 92 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, "out of checkpt blocks"); |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 93 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 94 | dev->checkpt_next_block = -1; |
| 95 | dev->checkpt_cur_block = -1; |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 98 | static void yaffs2_checkpt_find_block(struct yaffs_dev *dev) |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 99 | { |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 100 | int i; |
| 101 | struct yaffs_ext_tags tags; |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 102 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 103 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, |
| 104 | "find next checkpt block: start: blocks %d next %d", |
| 105 | dev->blocks_in_checkpt, dev->checkpt_next_block); |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 106 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 107 | if (dev->blocks_in_checkpt < dev->checkpt_max_blocks) |
| 108 | for (i = dev->checkpt_next_block; i <= dev->internal_end_block; |
| 109 | i++) { |
| 110 | int chunk = i * dev->param.chunks_per_block; |
| 111 | int realigned_chunk = chunk - dev->chunk_offset; |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 112 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 113 | dev->param.read_chunk_tags_fn(dev, realigned_chunk, |
| 114 | NULL, &tags); |
| 115 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, |
| 116 | "find next checkpt block: search: block %d oid %d seq %d eccr %d", |
| 117 | i, tags.obj_id, tags.seq_number, |
| 118 | tags.ecc_result); |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 119 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 120 | if (tags.seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA) { |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 121 | /* Right kind of block */ |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 122 | dev->checkpt_next_block = tags.obj_id; |
| 123 | dev->checkpt_cur_block = i; |
| 124 | dev->checkpt_block_list[dev-> |
| 125 | blocks_in_checkpt] = i; |
| 126 | dev->blocks_in_checkpt++; |
| 127 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, |
| 128 | "found checkpt block %d", i); |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 129 | return; |
| 130 | } |
| 131 | } |
| 132 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 133 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, "found no more checkpt blocks"); |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 134 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 135 | dev->checkpt_next_block = -1; |
| 136 | dev->checkpt_cur_block = -1; |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 137 | } |
| 138 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 139 | int yaffs2_checkpt_open(struct yaffs_dev *dev, int writing) |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 140 | { |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 141 | int i; |
| 142 | |
| 143 | dev->checkpt_open_write = writing; |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 144 | |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 145 | /* Got the functions we need? */ |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 146 | if (!dev->param.write_chunk_tags_fn || |
| 147 | !dev->param.read_chunk_tags_fn || |
| 148 | !dev->param.erase_fn || !dev->param.bad_block_fn) |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 149 | return 0; |
| 150 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 151 | if (writing && !yaffs2_checkpt_space_ok(dev)) |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 152 | return 0; |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 153 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 154 | if (!dev->checkpt_buffer) |
| 155 | dev->checkpt_buffer = |
| 156 | kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS); |
| 157 | if (!dev->checkpt_buffer) |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 158 | return 0; |
| 159 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 160 | dev->checkpt_page_seq = 0; |
| 161 | dev->checkpt_byte_count = 0; |
| 162 | dev->checkpt_sum = 0; |
| 163 | dev->checkpt_xor = 0; |
| 164 | dev->checkpt_cur_block = -1; |
| 165 | dev->checkpt_cur_chunk = -1; |
| 166 | dev->checkpt_next_block = dev->internal_start_block; |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 167 | |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 168 | /* Erase all the blocks in the checkpoint area */ |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 169 | if (writing) { |
| 170 | memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk); |
| 171 | dev->checkpt_byte_offs = 0; |
| 172 | return yaffs_checkpt_erase(dev); |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 173 | } |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 174 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 175 | /* Set to a value that will kick off a read */ |
| 176 | dev->checkpt_byte_offs = dev->data_bytes_per_chunk; |
| 177 | /* A checkpoint block list of 1 checkpoint block per 16 block is |
| 178 | * (hopefully) going to be way more than we need */ |
| 179 | dev->blocks_in_checkpt = 0; |
| 180 | dev->checkpt_max_blocks = |
| 181 | (dev->internal_end_block - dev->internal_start_block) / 16 + 2; |
| 182 | dev->checkpt_block_list = |
| 183 | kmalloc(sizeof(int) * dev->checkpt_max_blocks, GFP_NOFS); |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 184 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 185 | if (!dev->checkpt_block_list) |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 186 | return 0; |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 187 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 188 | for (i = 0; i < dev->checkpt_max_blocks; i++) |
| 189 | dev->checkpt_block_list[i] = -1; |
| 190 | |
| 191 | return 1; |
| 192 | } |
| 193 | |
| 194 | int yaffs2_get_checkpt_sum(struct yaffs_dev *dev, u32 * sum) |
| 195 | { |
| 196 | u32 composite_sum; |
| 197 | |
| 198 | composite_sum = (dev->checkpt_sum << 8) | (dev->checkpt_xor & 0xff); |
| 199 | *sum = composite_sum; |
| 200 | return 1; |
| 201 | } |
| 202 | |
| 203 | static int yaffs2_checkpt_flush_buffer(struct yaffs_dev *dev) |
| 204 | { |
| 205 | int chunk; |
| 206 | int realigned_chunk; |
| 207 | struct yaffs_ext_tags tags; |
| 208 | |
| 209 | if (dev->checkpt_cur_block < 0) { |
| 210 | yaffs2_checkpt_find_erased_block(dev); |
| 211 | dev->checkpt_cur_chunk = 0; |
| 212 | } |
| 213 | |
| 214 | if (dev->checkpt_cur_block < 0) |
| 215 | return 0; |
| 216 | |
| 217 | tags.is_deleted = 0; |
| 218 | tags.obj_id = dev->checkpt_next_block; /* Hint to next place to look */ |
| 219 | tags.chunk_id = dev->checkpt_page_seq + 1; |
| 220 | tags.seq_number = YAFFS_SEQUENCE_CHECKPOINT_DATA; |
| 221 | tags.n_bytes = dev->data_bytes_per_chunk; |
| 222 | if (dev->checkpt_cur_chunk == 0) { |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 223 | /* First chunk we write for the block? Set block state to |
| 224 | checkpoint */ |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 225 | struct yaffs_block_info *bi = |
| 226 | yaffs_get_block_info(dev, dev->checkpt_cur_block); |
| 227 | bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT; |
| 228 | dev->blocks_in_checkpt++; |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 229 | } |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 230 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 231 | chunk = |
| 232 | dev->checkpt_cur_block * dev->param.chunks_per_block + |
| 233 | dev->checkpt_cur_chunk; |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 234 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 235 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, |
| 236 | "checkpoint wite buffer nand %d(%d:%d) objid %d chId %d", |
| 237 | chunk, dev->checkpt_cur_block, dev->checkpt_cur_chunk, |
| 238 | tags.obj_id, tags.chunk_id); |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 239 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 240 | realigned_chunk = chunk - dev->chunk_offset; |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 241 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 242 | dev->n_page_writes++; |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 243 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 244 | dev->param.write_chunk_tags_fn(dev, realigned_chunk, |
| 245 | dev->checkpt_buffer, &tags); |
| 246 | dev->checkpt_byte_offs = 0; |
| 247 | dev->checkpt_page_seq++; |
| 248 | dev->checkpt_cur_chunk++; |
| 249 | if (dev->checkpt_cur_chunk >= dev->param.chunks_per_block) { |
| 250 | dev->checkpt_cur_chunk = 0; |
| 251 | dev->checkpt_cur_block = -1; |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 252 | } |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 253 | memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk); |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 254 | |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 255 | return 1; |
| 256 | } |
| 257 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 258 | int yaffs2_checkpt_wr(struct yaffs_dev *dev, const void *data, int n_bytes) |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 259 | { |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 260 | int i = 0; |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 261 | int ok = 1; |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 262 | u8 *data_bytes = (u8 *) data; |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 263 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 264 | if (!dev->checkpt_buffer) |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 265 | return 0; |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 266 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 267 | if (!dev->checkpt_open_write) |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 268 | return -1; |
| 269 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 270 | while (i < n_bytes && ok) { |
| 271 | dev->checkpt_buffer[dev->checkpt_byte_offs] = *data_bytes; |
| 272 | dev->checkpt_sum += *data_bytes; |
| 273 | dev->checkpt_xor ^= *data_bytes; |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 274 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 275 | dev->checkpt_byte_offs++; |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 276 | i++; |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 277 | data_bytes++; |
| 278 | dev->checkpt_byte_count++; |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 279 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 280 | if (dev->checkpt_byte_offs < 0 || |
| 281 | dev->checkpt_byte_offs >= dev->data_bytes_per_chunk) |
| 282 | ok = yaffs2_checkpt_flush_buffer(dev); |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 283 | } |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 284 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 285 | return i; |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 286 | } |
| 287 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 288 | int yaffs2_checkpt_rd(struct yaffs_dev *dev, void *data, int n_bytes) |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 289 | { |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 290 | int i = 0; |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 291 | int ok = 1; |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 292 | struct yaffs_ext_tags tags; |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 293 | int chunk; |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 294 | int realigned_chunk; |
| 295 | u8 *data_bytes = (u8 *) data; |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 296 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 297 | if (!dev->checkpt_buffer) |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 298 | return 0; |
| 299 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 300 | if (dev->checkpt_open_write) |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 301 | return -1; |
| 302 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 303 | while (i < n_bytes && ok) { |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 304 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 305 | if (dev->checkpt_byte_offs < 0 || |
| 306 | dev->checkpt_byte_offs >= dev->data_bytes_per_chunk) { |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 307 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 308 | if (dev->checkpt_cur_block < 0) { |
| 309 | yaffs2_checkpt_find_block(dev); |
| 310 | dev->checkpt_cur_chunk = 0; |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 311 | } |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 312 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 313 | if (dev->checkpt_cur_block < 0) { |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 314 | ok = 0; |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 315 | break; |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 316 | } |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 317 | |
| 318 | chunk = dev->checkpt_cur_block * |
| 319 | dev->param.chunks_per_block + |
| 320 | dev->checkpt_cur_chunk; |
| 321 | |
| 322 | realigned_chunk = chunk - dev->chunk_offset; |
| 323 | dev->n_page_reads++; |
| 324 | |
| 325 | /* read in the next chunk */ |
| 326 | dev->param.read_chunk_tags_fn(dev, |
| 327 | realigned_chunk, |
| 328 | dev->checkpt_buffer, |
| 329 | &tags); |
| 330 | |
| 331 | if (tags.chunk_id != (dev->checkpt_page_seq + 1) || |
| 332 | tags.ecc_result > YAFFS_ECC_RESULT_FIXED || |
| 333 | tags.seq_number != YAFFS_SEQUENCE_CHECKPOINT_DATA) { |
| 334 | ok = 0; |
| 335 | break; |
| 336 | } |
| 337 | |
| 338 | dev->checkpt_byte_offs = 0; |
| 339 | dev->checkpt_page_seq++; |
| 340 | dev->checkpt_cur_chunk++; |
| 341 | |
| 342 | if (dev->checkpt_cur_chunk >= |
| 343 | dev->param.chunks_per_block) |
| 344 | dev->checkpt_cur_block = -1; |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 345 | } |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 346 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 347 | *data_bytes = dev->checkpt_buffer[dev->checkpt_byte_offs]; |
| 348 | dev->checkpt_sum += *data_bytes; |
| 349 | dev->checkpt_xor ^= *data_bytes; |
| 350 | dev->checkpt_byte_offs++; |
| 351 | i++; |
| 352 | data_bytes++; |
| 353 | dev->checkpt_byte_count++; |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 354 | } |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 355 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 356 | return i; |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 357 | } |
| 358 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 359 | int yaffs_checkpt_close(struct yaffs_dev *dev) |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 360 | { |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 361 | int i; |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 362 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 363 | if (dev->checkpt_open_write) { |
| 364 | if (dev->checkpt_byte_offs != 0) |
| 365 | yaffs2_checkpt_flush_buffer(dev); |
| 366 | } else if (dev->checkpt_block_list) { |
| 367 | for (i = 0; |
| 368 | i < dev->blocks_in_checkpt && |
| 369 | dev->checkpt_block_list[i] >= 0; i++) { |
| 370 | int blk = dev->checkpt_block_list[i]; |
| 371 | struct yaffs_block_info *bi = NULL; |
| 372 | |
| 373 | if (dev->internal_start_block <= blk && |
| 374 | blk <= dev->internal_end_block) |
| 375 | bi = yaffs_get_block_info(dev, blk); |
| 376 | if (bi && bi->block_state == YAFFS_BLOCK_STATE_EMPTY) |
| 377 | bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT; |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 378 | } |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 379 | kfree(dev->checkpt_block_list); |
| 380 | dev->checkpt_block_list = NULL; |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 381 | } |
| 382 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 383 | dev->n_free_chunks -= |
| 384 | dev->blocks_in_checkpt * dev->param.chunks_per_block; |
| 385 | dev->n_erased_blocks -= dev->blocks_in_checkpt; |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 386 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 387 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, "checkpoint byte count %d", |
| 388 | dev->checkpt_byte_count); |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 389 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 390 | if (dev->checkpt_buffer) { |
Wolfgang Denk | 4b07080 | 2008-08-14 14:41:06 +0200 | [diff] [blame] | 391 | /* free the buffer */ |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 392 | kfree(dev->checkpt_buffer); |
| 393 | dev->checkpt_buffer = NULL; |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 394 | return 1; |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 395 | } else { |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 396 | return 0; |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 397 | } |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 398 | } |
| 399 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 400 | int yaffs2_checkpt_invalidate_stream(struct yaffs_dev *dev) |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 401 | { |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 402 | /* Erase the checkpoint data */ |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 403 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 404 | yaffs_trace(YAFFS_TRACE_CHECKPOINT, |
| 405 | "checkpoint invalidate of %d blocks", |
| 406 | dev->blocks_in_checkpt); |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 407 | |
Charles Manning | 753ac61 | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 408 | return yaffs_checkpt_erase(dev); |
William Juul | 0e8cc8b | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 409 | } |