1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2011 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 #include <dm/devres.h>
17 
yaffs2_checkpt_space_ok(struct yaffs_dev * dev)18 static int yaffs2_checkpt_space_ok(struct yaffs_dev *dev)
19 {
20 	int blocks_avail = dev->n_erased_blocks - dev->param.n_reserved_blocks;
21 
22 	yaffs_trace(YAFFS_TRACE_CHECKPOINT,
23 		"checkpt blocks_avail = %d", blocks_avail);
24 
25 	return (blocks_avail <= 0) ? 0 : 1;
26 }
27 
yaffs_checkpt_erase(struct yaffs_dev * dev)28 static int yaffs_checkpt_erase(struct yaffs_dev *dev)
29 {
30 	int i;
31 
32 	if (!dev->param.erase_fn)
33 		return 0;
34 	yaffs_trace(YAFFS_TRACE_CHECKPOINT,
35 		"checking blocks %d to %d",
36 		dev->internal_start_block, dev->internal_end_block);
37 
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;
56 			}
57 		}
58 	}
59 
60 	dev->blocks_in_checkpt = 0;
61 
62 	return 1;
63 }
64 
yaffs2_checkpt_find_erased_block(struct yaffs_dev * dev)65 static void yaffs2_checkpt_find_erased_block(struct yaffs_dev *dev)
66 {
67 	int i;
68 	int blocks_avail = dev->n_erased_blocks - dev->param.n_reserved_blocks;
69 
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);
74 
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);
88 				return;
89 			}
90 		}
91 	}
92 	yaffs_trace(YAFFS_TRACE_CHECKPOINT, "out of checkpt blocks");
93 
94 	dev->checkpt_next_block = -1;
95 	dev->checkpt_cur_block = -1;
96 }
97 
yaffs2_checkpt_find_block(struct yaffs_dev * dev)98 static void yaffs2_checkpt_find_block(struct yaffs_dev *dev)
99 {
100 	int i;
101 	struct yaffs_ext_tags tags;
102 
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);
106 
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;
112 
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);
119 
120 			if (tags.seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA) {
121 				/* Right kind of block */
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);
129 				return;
130 			}
131 		}
132 
133 	yaffs_trace(YAFFS_TRACE_CHECKPOINT, "found no more checkpt blocks");
134 
135 	dev->checkpt_next_block = -1;
136 	dev->checkpt_cur_block = -1;
137 }
138 
yaffs2_checkpt_open(struct yaffs_dev * dev,int writing)139 int yaffs2_checkpt_open(struct yaffs_dev *dev, int writing)
140 {
141 	int i;
142 
143 	dev->checkpt_open_write = writing;
144 
145 	/* Got the functions we need? */
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)
149 		return 0;
150 
151 	if (writing && !yaffs2_checkpt_space_ok(dev))
152 		return 0;
153 
154 	if (!dev->checkpt_buffer)
155 		dev->checkpt_buffer =
156 		    kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
157 	if (!dev->checkpt_buffer)
158 		return 0;
159 
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;
167 
168 	/* Erase all the blocks in the checkpoint area */
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);
173 	}
174 
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);
184 
185 	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;
190 
191 	return 1;
192 }
193 
yaffs2_get_checkpt_sum(struct yaffs_dev * dev,u32 * sum)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 
yaffs2_checkpt_flush_buffer(struct yaffs_dev * dev)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) {
223 		/* First chunk we write for the block? Set block state to
224 		   checkpoint */
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++;
229 	}
230 
231 	chunk =
232 	    dev->checkpt_cur_block * dev->param.chunks_per_block +
233 	    dev->checkpt_cur_chunk;
234 
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);
239 
240 	realigned_chunk = chunk - dev->chunk_offset;
241 
242 	dev->n_page_writes++;
243 
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;
252 	}
253 	memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
254 
255 	return 1;
256 }
257 
yaffs2_checkpt_wr(struct yaffs_dev * dev,const void * data,int n_bytes)258 int yaffs2_checkpt_wr(struct yaffs_dev *dev, const void *data, int n_bytes)
259 {
260 	int i = 0;
261 	int ok = 1;
262 	u8 *data_bytes = (u8 *) data;
263 
264 	if (!dev->checkpt_buffer)
265 		return 0;
266 
267 	if (!dev->checkpt_open_write)
268 		return -1;
269 
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;
274 
275 		dev->checkpt_byte_offs++;
276 		i++;
277 		data_bytes++;
278 		dev->checkpt_byte_count++;
279 
280 		if (dev->checkpt_byte_offs < 0 ||
281 		    dev->checkpt_byte_offs >= dev->data_bytes_per_chunk)
282 			ok = yaffs2_checkpt_flush_buffer(dev);
283 	}
284 
285 	return i;
286 }
287 
yaffs2_checkpt_rd(struct yaffs_dev * dev,void * data,int n_bytes)288 int yaffs2_checkpt_rd(struct yaffs_dev *dev, void *data, int n_bytes)
289 {
290 	int i = 0;
291 	int ok = 1;
292 	struct yaffs_ext_tags tags;
293 	int chunk;
294 	int realigned_chunk;
295 	u8 *data_bytes = (u8 *) data;
296 
297 	if (!dev->checkpt_buffer)
298 		return 0;
299 
300 	if (dev->checkpt_open_write)
301 		return -1;
302 
303 	while (i < n_bytes && ok) {
304 
305 		if (dev->checkpt_byte_offs < 0 ||
306 		    dev->checkpt_byte_offs >= dev->data_bytes_per_chunk) {
307 
308 			if (dev->checkpt_cur_block < 0) {
309 				yaffs2_checkpt_find_block(dev);
310 				dev->checkpt_cur_chunk = 0;
311 			}
312 
313 			if (dev->checkpt_cur_block < 0) {
314 				ok = 0;
315 				break;
316 			}
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;
345 		}
346 
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++;
354 	}
355 
356 	return i;
357 }
358 
yaffs_checkpt_close(struct yaffs_dev * dev)359 int yaffs_checkpt_close(struct yaffs_dev *dev)
360 {
361 	int i;
362 
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;
378 		}
379 		kfree(dev->checkpt_block_list);
380 		dev->checkpt_block_list = NULL;
381 	}
382 
383 	dev->n_free_chunks -=
384 		dev->blocks_in_checkpt * dev->param.chunks_per_block;
385 	dev->n_erased_blocks -= dev->blocks_in_checkpt;
386 
387 	yaffs_trace(YAFFS_TRACE_CHECKPOINT, "checkpoint byte count %d",
388 		dev->checkpt_byte_count);
389 
390 	if (dev->checkpt_buffer) {
391 		/* free the buffer */
392 		kfree(dev->checkpt_buffer);
393 		dev->checkpt_buffer = NULL;
394 		return 1;
395 	} else {
396 		return 0;
397 	}
398 }
399 
yaffs2_checkpt_invalidate_stream(struct yaffs_dev * dev)400 int yaffs2_checkpt_invalidate_stream(struct yaffs_dev *dev)
401 {
402 	/* Erase the checkpoint data */
403 
404 	yaffs_trace(YAFFS_TRACE_CHECKPOINT,
405 		"checkpoint invalidate of %d blocks",
406 		dev->blocks_in_checkpt);
407 
408 	return yaffs_checkpt_erase(dev);
409 }
410