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