blob: 628f02bb48d25a15cc4dc64b7a1f993f42ea5599 [file] [log] [blame]
William Juulc051bbe2007-11-15 11:13:05 +01001/*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
Charles Manning3796e1f2012-05-09 16:55:17 +00004 * Copyright (C) 2002-2011 Aleph One Ltd.
William Juulc051bbe2007-11-15 11:13:05 +01005 * 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 Juulc051bbe2007-11-15 11:13:05 +010014#include "yaffs_checkptrw.h"
Charles Manning3796e1f2012-05-09 16:55:17 +000015#include "yaffs_getblockinfo.h"
Simon Glassd66c5f72020-02-03 07:36:15 -070016#include <dm/devres.h>
William Juulc051bbe2007-11-15 11:13:05 +010017
Charles Manning3796e1f2012-05-09 16:55:17 +000018static int yaffs2_checkpt_space_ok(struct yaffs_dev *dev)
William Juulc051bbe2007-11-15 11:13:05 +010019{
Charles Manning3796e1f2012-05-09 16:55:17 +000020 int blocks_avail = dev->n_erased_blocks - dev->param.n_reserved_blocks;
William Juulc051bbe2007-11-15 11:13:05 +010021
Charles Manning3796e1f2012-05-09 16:55:17 +000022 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
23 "checkpt blocks_avail = %d", blocks_avail);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +020024
Charles Manning3796e1f2012-05-09 16:55:17 +000025 return (blocks_avail <= 0) ? 0 : 1;
William Juulc051bbe2007-11-15 11:13:05 +010026}
27
Charles Manning3796e1f2012-05-09 16:55:17 +000028static int yaffs_checkpt_erase(struct yaffs_dev *dev)
William Juulc051bbe2007-11-15 11:13:05 +010029{
William Juulc051bbe2007-11-15 11:13:05 +010030 int i;
William Juulc051bbe2007-11-15 11:13:05 +010031
Charles Manning3796e1f2012-05-09 16:55:17 +000032 if (!dev->param.erase_fn)
William Juulc051bbe2007-11-15 11:13:05 +010033 return 0;
Charles Manning3796e1f2012-05-09 16:55:17 +000034 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
35 "checking blocks %d to %d",
36 dev->internal_start_block, dev->internal_end_block);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +020037
Charles Manning3796e1f2012-05-09 16:55:17 +000038 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 Juulc051bbe2007-11-15 11:13:05 +010056 }
57 }
58 }
Wolfgang Denk74e0dde2008-08-14 14:41:06 +020059
Charles Manning3796e1f2012-05-09 16:55:17 +000060 dev->blocks_in_checkpt = 0;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +020061
William Juulc051bbe2007-11-15 11:13:05 +010062 return 1;
63}
64
Charles Manning3796e1f2012-05-09 16:55:17 +000065static void yaffs2_checkpt_find_erased_block(struct yaffs_dev *dev)
William Juulc051bbe2007-11-15 11:13:05 +010066{
Charles Manning3796e1f2012-05-09 16:55:17 +000067 int i;
68 int blocks_avail = dev->n_erased_blocks - dev->param.n_reserved_blocks;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +020069
Charles Manning3796e1f2012-05-09 16:55:17 +000070 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 Denk74e0dde2008-08-14 14:41:06 +020074
Charles Manning3796e1f2012-05-09 16:55:17 +000075 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 Juulc051bbe2007-11-15 11:13:05 +010088 return;
89 }
90 }
91 }
Charles Manning3796e1f2012-05-09 16:55:17 +000092 yaffs_trace(YAFFS_TRACE_CHECKPOINT, "out of checkpt blocks");
Wolfgang Denk74e0dde2008-08-14 14:41:06 +020093
Charles Manning3796e1f2012-05-09 16:55:17 +000094 dev->checkpt_next_block = -1;
95 dev->checkpt_cur_block = -1;
William Juulc051bbe2007-11-15 11:13:05 +010096}
97
Charles Manning3796e1f2012-05-09 16:55:17 +000098static void yaffs2_checkpt_find_block(struct yaffs_dev *dev)
William Juulc051bbe2007-11-15 11:13:05 +010099{
Charles Manning3796e1f2012-05-09 16:55:17 +0000100 int i;
101 struct yaffs_ext_tags tags;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200102
Charles Manning3796e1f2012-05-09 16:55:17 +0000103 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 Denk74e0dde2008-08-14 14:41:06 +0200106
Charles Manning3796e1f2012-05-09 16:55:17 +0000107 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 Juulc051bbe2007-11-15 11:13:05 +0100112
Charles Manning3796e1f2012-05-09 16:55:17 +0000113 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 Denk74e0dde2008-08-14 14:41:06 +0200119
Charles Manning3796e1f2012-05-09 16:55:17 +0000120 if (tags.seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA) {
William Juulc051bbe2007-11-15 11:13:05 +0100121 /* Right kind of block */
Charles Manning3796e1f2012-05-09 16:55:17 +0000122 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 Juulc051bbe2007-11-15 11:13:05 +0100129 return;
130 }
131 }
132
Charles Manning3796e1f2012-05-09 16:55:17 +0000133 yaffs_trace(YAFFS_TRACE_CHECKPOINT, "found no more checkpt blocks");
William Juulc051bbe2007-11-15 11:13:05 +0100134
Charles Manning3796e1f2012-05-09 16:55:17 +0000135 dev->checkpt_next_block = -1;
136 dev->checkpt_cur_block = -1;
William Juulc051bbe2007-11-15 11:13:05 +0100137}
138
Charles Manning3796e1f2012-05-09 16:55:17 +0000139int yaffs2_checkpt_open(struct yaffs_dev *dev, int writing)
William Juulc051bbe2007-11-15 11:13:05 +0100140{
Charles Manning3796e1f2012-05-09 16:55:17 +0000141 int i;
142
143 dev->checkpt_open_write = writing;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200144
William Juulc051bbe2007-11-15 11:13:05 +0100145 /* Got the functions we need? */
Charles Manning3796e1f2012-05-09 16:55:17 +0000146 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 Juulc051bbe2007-11-15 11:13:05 +0100149 return 0;
150
Charles Manning3796e1f2012-05-09 16:55:17 +0000151 if (writing && !yaffs2_checkpt_space_ok(dev))
William Juulc051bbe2007-11-15 11:13:05 +0100152 return 0;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200153
Charles Manning3796e1f2012-05-09 16:55:17 +0000154 if (!dev->checkpt_buffer)
155 dev->checkpt_buffer =
156 kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
157 if (!dev->checkpt_buffer)
William Juulc051bbe2007-11-15 11:13:05 +0100158 return 0;
159
Charles Manning3796e1f2012-05-09 16:55:17 +0000160 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 Denk74e0dde2008-08-14 14:41:06 +0200167
William Juulc051bbe2007-11-15 11:13:05 +0100168 /* Erase all the blocks in the checkpoint area */
Charles Manning3796e1f2012-05-09 16:55:17 +0000169 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);
173 }
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200174
Charles Manning3796e1f2012-05-09 16:55:17 +0000175 /* 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);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200184
Charles Manning3796e1f2012-05-09 16:55:17 +0000185 if (!dev->checkpt_block_list)
186 return 0;
187
188 for (i = 0; i < dev->checkpt_max_blocks; i++)
189 dev->checkpt_block_list[i] = -1;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200190
William Juulc051bbe2007-11-15 11:13:05 +0100191 return 1;
192}
193
Charles Manning3796e1f2012-05-09 16:55:17 +0000194int yaffs2_get_checkpt_sum(struct yaffs_dev *dev, u32 * sum)
William Juulc051bbe2007-11-15 11:13:05 +0100195{
Charles Manning3796e1f2012-05-09 16:55:17 +0000196 u32 composite_sum;
197
198 composite_sum = (dev->checkpt_sum << 8) | (dev->checkpt_xor & 0xff);
199 *sum = composite_sum;
William Juulc051bbe2007-11-15 11:13:05 +0100200 return 1;
201}
202
Charles Manning3796e1f2012-05-09 16:55:17 +0000203static int yaffs2_checkpt_flush_buffer(struct yaffs_dev *dev)
William Juulc051bbe2007-11-15 11:13:05 +0100204{
William Juulc051bbe2007-11-15 11:13:05 +0100205 int chunk;
Charles Manning3796e1f2012-05-09 16:55:17 +0000206 int realigned_chunk;
207 struct yaffs_ext_tags tags;
William Juulc051bbe2007-11-15 11:13:05 +0100208
Charles Manning3796e1f2012-05-09 16:55:17 +0000209 if (dev->checkpt_cur_block < 0) {
210 yaffs2_checkpt_find_erased_block(dev);
211 dev->checkpt_cur_chunk = 0;
William Juulc051bbe2007-11-15 11:13:05 +0100212 }
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200213
Charles Manning3796e1f2012-05-09 16:55:17 +0000214 if (dev->checkpt_cur_block < 0)
William Juulc051bbe2007-11-15 11:13:05 +0100215 return 0;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200216
Charles Manning3796e1f2012-05-09 16:55:17 +0000217 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 Juulc051bbe2007-11-15 11:13:05 +0100223 /* First chunk we write for the block? Set block state to
224 checkpoint */
Charles Manning3796e1f2012-05-09 16:55:17 +0000225 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 Juulc051bbe2007-11-15 11:13:05 +0100229 }
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200230
Charles Manning3796e1f2012-05-09 16:55:17 +0000231 chunk =
232 dev->checkpt_cur_block * dev->param.chunks_per_block +
233 dev->checkpt_cur_chunk;
William Juulc051bbe2007-11-15 11:13:05 +0100234
Charles Manning3796e1f2012-05-09 16:55:17 +0000235 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 Denk74e0dde2008-08-14 14:41:06 +0200239
Charles Manning3796e1f2012-05-09 16:55:17 +0000240 realigned_chunk = chunk - dev->chunk_offset;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200241
Charles Manning3796e1f2012-05-09 16:55:17 +0000242 dev->n_page_writes++;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200243
Charles Manning3796e1f2012-05-09 16:55:17 +0000244 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 Juulc051bbe2007-11-15 11:13:05 +0100252 }
Charles Manning3796e1f2012-05-09 16:55:17 +0000253 memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200254
William Juulc051bbe2007-11-15 11:13:05 +0100255 return 1;
256}
257
Charles Manning3796e1f2012-05-09 16:55:17 +0000258int yaffs2_checkpt_wr(struct yaffs_dev *dev, const void *data, int n_bytes)
William Juulc051bbe2007-11-15 11:13:05 +0100259{
Charles Manning3796e1f2012-05-09 16:55:17 +0000260 int i = 0;
William Juulc051bbe2007-11-15 11:13:05 +0100261 int ok = 1;
Charles Manning3796e1f2012-05-09 16:55:17 +0000262 u8 *data_bytes = (u8 *) data;
William Juulc051bbe2007-11-15 11:13:05 +0100263
Charles Manning3796e1f2012-05-09 16:55:17 +0000264 if (!dev->checkpt_buffer)
William Juulc051bbe2007-11-15 11:13:05 +0100265 return 0;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200266
Charles Manning3796e1f2012-05-09 16:55:17 +0000267 if (!dev->checkpt_open_write)
William Juulc051bbe2007-11-15 11:13:05 +0100268 return -1;
269
Charles Manning3796e1f2012-05-09 16:55:17 +0000270 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 Juulc051bbe2007-11-15 11:13:05 +0100274
Charles Manning3796e1f2012-05-09 16:55:17 +0000275 dev->checkpt_byte_offs++;
William Juulc051bbe2007-11-15 11:13:05 +0100276 i++;
Charles Manning3796e1f2012-05-09 16:55:17 +0000277 data_bytes++;
278 dev->checkpt_byte_count++;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200279
Charles Manning3796e1f2012-05-09 16:55:17 +0000280 if (dev->checkpt_byte_offs < 0 ||
281 dev->checkpt_byte_offs >= dev->data_bytes_per_chunk)
282 ok = yaffs2_checkpt_flush_buffer(dev);
William Juulc051bbe2007-11-15 11:13:05 +0100283 }
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200284
Charles Manning3796e1f2012-05-09 16:55:17 +0000285 return i;
William Juulc051bbe2007-11-15 11:13:05 +0100286}
287
Charles Manning3796e1f2012-05-09 16:55:17 +0000288int yaffs2_checkpt_rd(struct yaffs_dev *dev, void *data, int n_bytes)
William Juulc051bbe2007-11-15 11:13:05 +0100289{
Charles Manning3796e1f2012-05-09 16:55:17 +0000290 int i = 0;
William Juulc051bbe2007-11-15 11:13:05 +0100291 int ok = 1;
Charles Manning3796e1f2012-05-09 16:55:17 +0000292 struct yaffs_ext_tags tags;
William Juulc051bbe2007-11-15 11:13:05 +0100293 int chunk;
Charles Manning3796e1f2012-05-09 16:55:17 +0000294 int realigned_chunk;
295 u8 *data_bytes = (u8 *) data;
William Juulc051bbe2007-11-15 11:13:05 +0100296
Charles Manning3796e1f2012-05-09 16:55:17 +0000297 if (!dev->checkpt_buffer)
William Juulc051bbe2007-11-15 11:13:05 +0100298 return 0;
299
Charles Manning3796e1f2012-05-09 16:55:17 +0000300 if (dev->checkpt_open_write)
William Juulc051bbe2007-11-15 11:13:05 +0100301 return -1;
302
Charles Manning3796e1f2012-05-09 16:55:17 +0000303 while (i < n_bytes && ok) {
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200304
Charles Manning3796e1f2012-05-09 16:55:17 +0000305 if (dev->checkpt_byte_offs < 0 ||
306 dev->checkpt_byte_offs >= dev->data_bytes_per_chunk) {
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200307
Charles Manning3796e1f2012-05-09 16:55:17 +0000308 if (dev->checkpt_cur_block < 0) {
309 yaffs2_checkpt_find_block(dev);
310 dev->checkpt_cur_chunk = 0;
William Juulc051bbe2007-11-15 11:13:05 +0100311 }
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200312
Charles Manning3796e1f2012-05-09 16:55:17 +0000313 if (dev->checkpt_cur_block < 0) {
William Juulc051bbe2007-11-15 11:13:05 +0100314 ok = 0;
Charles Manning3796e1f2012-05-09 16:55:17 +0000315 break;
316 }
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200317
Charles Manning3796e1f2012-05-09 16:55:17 +0000318 chunk = dev->checkpt_cur_block *
319 dev->param.chunks_per_block +
320 dev->checkpt_cur_chunk;
William Juulc051bbe2007-11-15 11:13:05 +0100321
Charles Manning3796e1f2012-05-09 16:55:17 +0000322 realigned_chunk = chunk - dev->chunk_offset;
323 dev->n_page_reads++;
William Juulc051bbe2007-11-15 11:13:05 +0100324
Charles Manning3796e1f2012-05-09 16:55:17 +0000325 /* read in the next chunk */
326 dev->param.read_chunk_tags_fn(dev,
327 realigned_chunk,
328 dev->checkpt_buffer,
329 &tags);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200330
Charles Manning3796e1f2012-05-09 16:55:17 +0000331 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 }
William Juulc051bbe2007-11-15 11:13:05 +0100337
Charles Manning3796e1f2012-05-09 16:55:17 +0000338 dev->checkpt_byte_offs = 0;
339 dev->checkpt_page_seq++;
340 dev->checkpt_cur_chunk++;
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200341
Charles Manning3796e1f2012-05-09 16:55:17 +0000342 if (dev->checkpt_cur_chunk >=
343 dev->param.chunks_per_block)
344 dev->checkpt_cur_block = -1;
William Juulc051bbe2007-11-15 11:13:05 +0100345 }
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200346
Charles Manning3796e1f2012-05-09 16:55:17 +0000347 *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 Juulc051bbe2007-11-15 11:13:05 +0100354 }
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200355
Charles Manning3796e1f2012-05-09 16:55:17 +0000356 return i;
William Juulc051bbe2007-11-15 11:13:05 +0100357}
358
Charles Manning3796e1f2012-05-09 16:55:17 +0000359int yaffs_checkpt_close(struct yaffs_dev *dev)
William Juulc051bbe2007-11-15 11:13:05 +0100360{
Charles Manning3796e1f2012-05-09 16:55:17 +0000361 int i;
William Juulc051bbe2007-11-15 11:13:05 +0100362
Charles Manning3796e1f2012-05-09 16:55:17 +0000363 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 Juulc051bbe2007-11-15 11:13:05 +0100378 }
Charles Manning3796e1f2012-05-09 16:55:17 +0000379 kfree(dev->checkpt_block_list);
380 dev->checkpt_block_list = NULL;
William Juulc051bbe2007-11-15 11:13:05 +0100381 }
382
Charles Manning3796e1f2012-05-09 16:55:17 +0000383 dev->n_free_chunks -=
384 dev->blocks_in_checkpt * dev->param.chunks_per_block;
385 dev->n_erased_blocks -= dev->blocks_in_checkpt;
William Juulc051bbe2007-11-15 11:13:05 +0100386
Charles Manning3796e1f2012-05-09 16:55:17 +0000387 yaffs_trace(YAFFS_TRACE_CHECKPOINT, "checkpoint byte count %d",
388 dev->checkpt_byte_count);
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200389
Charles Manning3796e1f2012-05-09 16:55:17 +0000390 if (dev->checkpt_buffer) {
Wolfgang Denk74e0dde2008-08-14 14:41:06 +0200391 /* free the buffer */
Charles Manning3796e1f2012-05-09 16:55:17 +0000392 kfree(dev->checkpt_buffer);
393 dev->checkpt_buffer = NULL;
William Juulc051bbe2007-11-15 11:13:05 +0100394 return 1;
Charles Manning3796e1f2012-05-09 16:55:17 +0000395 } else {
William Juulc051bbe2007-11-15 11:13:05 +0100396 return 0;
Charles Manning3796e1f2012-05-09 16:55:17 +0000397 }
William Juulc051bbe2007-11-15 11:13:05 +0100398}
399
Charles Manning3796e1f2012-05-09 16:55:17 +0000400int yaffs2_checkpt_invalidate_stream(struct yaffs_dev *dev)
William Juulc051bbe2007-11-15 11:13:05 +0100401{
Charles Manning3796e1f2012-05-09 16:55:17 +0000402 /* Erase the checkpoint data */
William Juulc051bbe2007-11-15 11:13:05 +0100403
Charles Manning3796e1f2012-05-09 16:55:17 +0000404 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
405 "checkpoint invalidate of %d blocks",
406 dev->blocks_in_checkpt);
William Juulc051bbe2007-11-15 11:13:05 +0100407
Charles Manning3796e1f2012-05-09 16:55:17 +0000408 return yaffs_checkpt_erase(dev);
William Juulc051bbe2007-11-15 11:13:05 +0100409}