xref: /openbmc/u-boot/fs/yaffs2/yaffs_yaffs2.c (revision 8cc64bafc00415877ed51769ca90cb562ded5c01)
1753ac610SCharles Manning /*
2753ac610SCharles Manning  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3753ac610SCharles Manning  *
4753ac610SCharles Manning  * Copyright (C) 2002-2011 Aleph One Ltd.
5753ac610SCharles Manning  *   for Toby Churchill Ltd and Brightstar Engineering
6753ac610SCharles Manning  *
7753ac610SCharles Manning  * Created by Charles Manning <charles@aleph1.co.uk>
8753ac610SCharles Manning  *
9753ac610SCharles Manning  * This program is free software; you can redistribute it and/or modify
10753ac610SCharles Manning  * it under the terms of the GNU General Public License version 2 as
11753ac610SCharles Manning  * published by the Free Software Foundation.
12753ac610SCharles Manning  */
13753ac610SCharles Manning 
14753ac610SCharles Manning #include "yaffs_guts.h"
15753ac610SCharles Manning #include "yaffs_trace.h"
16753ac610SCharles Manning #include "yaffs_yaffs2.h"
17753ac610SCharles Manning #include "yaffs_checkptrw.h"
18753ac610SCharles Manning #include "yaffs_bitmap.h"
19753ac610SCharles Manning #include "yaffs_nand.h"
20753ac610SCharles Manning #include "yaffs_getblockinfo.h"
21753ac610SCharles Manning #include "yaffs_verify.h"
22753ac610SCharles Manning #include "yaffs_attribs.h"
23753ac610SCharles Manning #include "yaffs_summary.h"
24753ac610SCharles Manning 
25753ac610SCharles Manning /*
26753ac610SCharles Manning  * Checkpoints are really no benefit on very small partitions.
27753ac610SCharles Manning  *
28753ac610SCharles Manning  * To save space on small partitions don't bother with checkpoints unless
29753ac610SCharles Manning  * the partition is at least this big.
30753ac610SCharles Manning  */
31753ac610SCharles Manning #define YAFFS_CHECKPOINT_MIN_BLOCKS 60
32753ac610SCharles Manning #define YAFFS_SMALL_HOLE_THRESHOLD 4
33753ac610SCharles Manning 
34753ac610SCharles Manning /*
35753ac610SCharles Manning  * Oldest Dirty Sequence Number handling.
36753ac610SCharles Manning  */
37753ac610SCharles Manning 
38753ac610SCharles Manning /* yaffs_calc_oldest_dirty_seq()
39753ac610SCharles Manning  * yaffs2_find_oldest_dirty_seq()
40753ac610SCharles Manning  * Calculate the oldest dirty sequence number if we don't know it.
41753ac610SCharles Manning  */
yaffs_calc_oldest_dirty_seq(struct yaffs_dev * dev)42753ac610SCharles Manning void yaffs_calc_oldest_dirty_seq(struct yaffs_dev *dev)
43753ac610SCharles Manning {
44753ac610SCharles Manning 	int i;
45753ac610SCharles Manning 	unsigned seq;
46753ac610SCharles Manning 	unsigned block_no = 0;
47753ac610SCharles Manning 	struct yaffs_block_info *b;
48753ac610SCharles Manning 
49753ac610SCharles Manning 	if (!dev->param.is_yaffs2)
50753ac610SCharles Manning 		return;
51753ac610SCharles Manning 
52753ac610SCharles Manning 	/* Find the oldest dirty sequence number. */
53753ac610SCharles Manning 	seq = dev->seq_number + 1;
54753ac610SCharles Manning 	b = dev->block_info;
55753ac610SCharles Manning 	for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
56753ac610SCharles Manning 		if (b->block_state == YAFFS_BLOCK_STATE_FULL &&
57753ac610SCharles Manning 		    (b->pages_in_use - b->soft_del_pages) <
58753ac610SCharles Manning 		    dev->param.chunks_per_block &&
59753ac610SCharles Manning 		    b->seq_number < seq) {
60753ac610SCharles Manning 			seq = b->seq_number;
61753ac610SCharles Manning 			block_no = i;
62753ac610SCharles Manning 		}
63753ac610SCharles Manning 		b++;
64753ac610SCharles Manning 	}
65753ac610SCharles Manning 
66753ac610SCharles Manning 	if (block_no) {
67753ac610SCharles Manning 		dev->oldest_dirty_seq = seq;
68753ac610SCharles Manning 		dev->oldest_dirty_block = block_no;
69753ac610SCharles Manning 	}
70753ac610SCharles Manning }
71753ac610SCharles Manning 
yaffs2_find_oldest_dirty_seq(struct yaffs_dev * dev)72753ac610SCharles Manning void yaffs2_find_oldest_dirty_seq(struct yaffs_dev *dev)
73753ac610SCharles Manning {
74753ac610SCharles Manning 	if (!dev->param.is_yaffs2)
75753ac610SCharles Manning 		return;
76753ac610SCharles Manning 
77753ac610SCharles Manning 	if (!dev->oldest_dirty_seq)
78753ac610SCharles Manning 		yaffs_calc_oldest_dirty_seq(dev);
79753ac610SCharles Manning }
80753ac610SCharles Manning 
81753ac610SCharles Manning /*
82753ac610SCharles Manning  * yaffs_clear_oldest_dirty_seq()
83753ac610SCharles Manning  * Called when a block is erased or marked bad. (ie. when its seq_number
84753ac610SCharles Manning  * becomes invalid). If the value matches the oldest then we clear
85753ac610SCharles Manning  * dev->oldest_dirty_seq to force its recomputation.
86753ac610SCharles Manning  */
yaffs2_clear_oldest_dirty_seq(struct yaffs_dev * dev,struct yaffs_block_info * bi)87753ac610SCharles Manning void yaffs2_clear_oldest_dirty_seq(struct yaffs_dev *dev,
88753ac610SCharles Manning 				   struct yaffs_block_info *bi)
89753ac610SCharles Manning {
90753ac610SCharles Manning 
91753ac610SCharles Manning 	if (!dev->param.is_yaffs2)
92753ac610SCharles Manning 		return;
93753ac610SCharles Manning 
94753ac610SCharles Manning 	if (!bi || bi->seq_number == dev->oldest_dirty_seq) {
95753ac610SCharles Manning 		dev->oldest_dirty_seq = 0;
96753ac610SCharles Manning 		dev->oldest_dirty_block = 0;
97753ac610SCharles Manning 	}
98753ac610SCharles Manning }
99753ac610SCharles Manning 
100753ac610SCharles Manning /*
101753ac610SCharles Manning  * yaffs2_update_oldest_dirty_seq()
102753ac610SCharles Manning  * Update the oldest dirty sequence number whenever we dirty a block.
103753ac610SCharles Manning  * Only do this if the oldest_dirty_seq is actually being tracked.
104753ac610SCharles Manning  */
yaffs2_update_oldest_dirty_seq(struct yaffs_dev * dev,unsigned block_no,struct yaffs_block_info * bi)105753ac610SCharles Manning void yaffs2_update_oldest_dirty_seq(struct yaffs_dev *dev, unsigned block_no,
106753ac610SCharles Manning 				    struct yaffs_block_info *bi)
107753ac610SCharles Manning {
108753ac610SCharles Manning 	if (!dev->param.is_yaffs2)
109753ac610SCharles Manning 		return;
110753ac610SCharles Manning 
111753ac610SCharles Manning 	if (dev->oldest_dirty_seq) {
112753ac610SCharles Manning 		if (dev->oldest_dirty_seq > bi->seq_number) {
113753ac610SCharles Manning 			dev->oldest_dirty_seq = bi->seq_number;
114753ac610SCharles Manning 			dev->oldest_dirty_block = block_no;
115753ac610SCharles Manning 		}
116753ac610SCharles Manning 	}
117753ac610SCharles Manning }
118753ac610SCharles Manning 
yaffs_block_ok_for_gc(struct yaffs_dev * dev,struct yaffs_block_info * bi)119753ac610SCharles Manning int yaffs_block_ok_for_gc(struct yaffs_dev *dev, struct yaffs_block_info *bi)
120753ac610SCharles Manning {
121753ac610SCharles Manning 
122753ac610SCharles Manning 	if (!dev->param.is_yaffs2)
123753ac610SCharles Manning 		return 1;	/* disqualification only applies to yaffs2. */
124753ac610SCharles Manning 
125753ac610SCharles Manning 	if (!bi->has_shrink_hdr)
126753ac610SCharles Manning 		return 1;	/* can gc */
127753ac610SCharles Manning 
128753ac610SCharles Manning 	yaffs2_find_oldest_dirty_seq(dev);
129753ac610SCharles Manning 
130753ac610SCharles Manning 	/* Can't do gc of this block if there are any blocks older than this
131753ac610SCharles Manning 	 * one that have discarded pages.
132753ac610SCharles Manning 	 */
133753ac610SCharles Manning 	return (bi->seq_number <= dev->oldest_dirty_seq);
134753ac610SCharles Manning }
135753ac610SCharles Manning 
136753ac610SCharles Manning /*
137753ac610SCharles Manning  * yaffs2_find_refresh_block()
138753ac610SCharles Manning  * periodically finds the oldest full block by sequence number for refreshing.
139753ac610SCharles Manning  * Only for yaffs2.
140753ac610SCharles Manning  */
yaffs2_find_refresh_block(struct yaffs_dev * dev)141753ac610SCharles Manning u32 yaffs2_find_refresh_block(struct yaffs_dev *dev)
142753ac610SCharles Manning {
143753ac610SCharles Manning 	u32 b;
144753ac610SCharles Manning 	u32 oldest = 0;
145753ac610SCharles Manning 	u32 oldest_seq = 0;
146753ac610SCharles Manning 	struct yaffs_block_info *bi;
147753ac610SCharles Manning 
148753ac610SCharles Manning 	if (!dev->param.is_yaffs2)
149753ac610SCharles Manning 		return oldest;
150753ac610SCharles Manning 
151753ac610SCharles Manning 	/*
152753ac610SCharles Manning 	 * If refresh period < 10 then refreshing is disabled.
153753ac610SCharles Manning 	 */
154753ac610SCharles Manning 	if (dev->param.refresh_period < 10)
155753ac610SCharles Manning 		return oldest;
156753ac610SCharles Manning 
157753ac610SCharles Manning 	/*
158753ac610SCharles Manning 	 * Fix broken values.
159753ac610SCharles Manning 	 */
160753ac610SCharles Manning 	if (dev->refresh_skip > dev->param.refresh_period)
161753ac610SCharles Manning 		dev->refresh_skip = dev->param.refresh_period;
162753ac610SCharles Manning 
163753ac610SCharles Manning 	if (dev->refresh_skip > 0)
164753ac610SCharles Manning 		return oldest;
165753ac610SCharles Manning 
166753ac610SCharles Manning 	/*
167753ac610SCharles Manning 	 * Refresh skip is now zero.
168753ac610SCharles Manning 	 * We'll do a refresh this time around....
169753ac610SCharles Manning 	 * Update the refresh skip and find the oldest block.
170753ac610SCharles Manning 	 */
171753ac610SCharles Manning 	dev->refresh_skip = dev->param.refresh_period;
172753ac610SCharles Manning 	dev->refresh_count++;
173753ac610SCharles Manning 	bi = dev->block_info;
174753ac610SCharles Manning 	for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
175753ac610SCharles Manning 
176753ac610SCharles Manning 		if (bi->block_state == YAFFS_BLOCK_STATE_FULL) {
177753ac610SCharles Manning 
178753ac610SCharles Manning 			if (oldest < 1 || bi->seq_number < oldest_seq) {
179753ac610SCharles Manning 				oldest = b;
180753ac610SCharles Manning 				oldest_seq = bi->seq_number;
181753ac610SCharles Manning 			}
182753ac610SCharles Manning 		}
183753ac610SCharles Manning 		bi++;
184753ac610SCharles Manning 	}
185753ac610SCharles Manning 
186753ac610SCharles Manning 	if (oldest > 0) {
187753ac610SCharles Manning 		yaffs_trace(YAFFS_TRACE_GC,
188753ac610SCharles Manning 			"GC refresh count %d selected block %d with seq_number %d",
189753ac610SCharles Manning 			dev->refresh_count, oldest, oldest_seq);
190753ac610SCharles Manning 	}
191753ac610SCharles Manning 
192753ac610SCharles Manning 	return oldest;
193753ac610SCharles Manning }
194753ac610SCharles Manning 
yaffs2_checkpt_required(struct yaffs_dev * dev)195753ac610SCharles Manning int yaffs2_checkpt_required(struct yaffs_dev *dev)
196753ac610SCharles Manning {
197753ac610SCharles Manning 	int nblocks;
198753ac610SCharles Manning 
199753ac610SCharles Manning 	if (!dev->param.is_yaffs2)
200753ac610SCharles Manning 		return 0;
201753ac610SCharles Manning 
202753ac610SCharles Manning 	nblocks = dev->internal_end_block - dev->internal_start_block + 1;
203753ac610SCharles Manning 
204753ac610SCharles Manning 	return !dev->param.skip_checkpt_wr &&
205753ac610SCharles Manning 	    !dev->read_only && (nblocks >= YAFFS_CHECKPOINT_MIN_BLOCKS);
206753ac610SCharles Manning }
207753ac610SCharles Manning 
yaffs_calc_checkpt_blocks_required(struct yaffs_dev * dev)208753ac610SCharles Manning int yaffs_calc_checkpt_blocks_required(struct yaffs_dev *dev)
209753ac610SCharles Manning {
210753ac610SCharles Manning 	int retval;
211753ac610SCharles Manning 	int n_bytes = 0;
212753ac610SCharles Manning 	int n_blocks;
213753ac610SCharles Manning 	int dev_blocks;
214753ac610SCharles Manning 
215753ac610SCharles Manning 	if (!dev->param.is_yaffs2)
216753ac610SCharles Manning 		return 0;
217753ac610SCharles Manning 
218753ac610SCharles Manning 	if (!dev->checkpoint_blocks_required && yaffs2_checkpt_required(dev)) {
219753ac610SCharles Manning 		/* Not a valid value so recalculate */
220753ac610SCharles Manning 		dev_blocks = dev->param.end_block - dev->param.start_block + 1;
221753ac610SCharles Manning 		n_bytes += sizeof(struct yaffs_checkpt_validity);
222753ac610SCharles Manning 		n_bytes += sizeof(struct yaffs_checkpt_dev);
223753ac610SCharles Manning 		n_bytes += dev_blocks * sizeof(struct yaffs_block_info);
224753ac610SCharles Manning 		n_bytes += dev_blocks * dev->chunk_bit_stride;
225753ac610SCharles Manning 		n_bytes +=
226753ac610SCharles Manning 		    (sizeof(struct yaffs_checkpt_obj) + sizeof(u32)) *
227753ac610SCharles Manning 		    dev->n_obj;
228753ac610SCharles Manning 		n_bytes += (dev->tnode_size + sizeof(u32)) * dev->n_tnodes;
229753ac610SCharles Manning 		n_bytes += sizeof(struct yaffs_checkpt_validity);
230753ac610SCharles Manning 		n_bytes += sizeof(u32);	/* checksum */
231753ac610SCharles Manning 
232753ac610SCharles Manning 		/* Round up and add 2 blocks to allow for some bad blocks,
233753ac610SCharles Manning 		 * so add 3 */
234753ac610SCharles Manning 
235753ac610SCharles Manning 		n_blocks =
236753ac610SCharles Manning 		    (n_bytes /
237753ac610SCharles Manning 		     (dev->data_bytes_per_chunk *
238753ac610SCharles Manning 		      dev->param.chunks_per_block)) + 3;
239753ac610SCharles Manning 
240753ac610SCharles Manning 		dev->checkpoint_blocks_required = n_blocks;
241753ac610SCharles Manning 	}
242753ac610SCharles Manning 
243753ac610SCharles Manning 	retval = dev->checkpoint_blocks_required - dev->blocks_in_checkpt;
244753ac610SCharles Manning 	if (retval < 0)
245753ac610SCharles Manning 		retval = 0;
246753ac610SCharles Manning 	return retval;
247753ac610SCharles Manning }
248753ac610SCharles Manning 
249753ac610SCharles Manning /*--------------------- Checkpointing --------------------*/
250753ac610SCharles Manning 
yaffs2_wr_checkpt_validity_marker(struct yaffs_dev * dev,int head)251753ac610SCharles Manning static int yaffs2_wr_checkpt_validity_marker(struct yaffs_dev *dev, int head)
252753ac610SCharles Manning {
253753ac610SCharles Manning 	struct yaffs_checkpt_validity cp;
254753ac610SCharles Manning 
255753ac610SCharles Manning 	memset(&cp, 0, sizeof(cp));
256753ac610SCharles Manning 
257753ac610SCharles Manning 	cp.struct_type = sizeof(cp);
258753ac610SCharles Manning 	cp.magic = YAFFS_MAGIC;
259753ac610SCharles Manning 	cp.version = YAFFS_CHECKPOINT_VERSION;
260753ac610SCharles Manning 	cp.head = (head) ? 1 : 0;
261753ac610SCharles Manning 
262753ac610SCharles Manning 	return (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp)) ? 1 : 0;
263753ac610SCharles Manning }
264753ac610SCharles Manning 
yaffs2_rd_checkpt_validity_marker(struct yaffs_dev * dev,int head)265753ac610SCharles Manning static int yaffs2_rd_checkpt_validity_marker(struct yaffs_dev *dev, int head)
266753ac610SCharles Manning {
267753ac610SCharles Manning 	struct yaffs_checkpt_validity cp;
268753ac610SCharles Manning 	int ok;
269753ac610SCharles Manning 
270753ac610SCharles Manning 	ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
271753ac610SCharles Manning 
272753ac610SCharles Manning 	if (ok)
273753ac610SCharles Manning 		ok = (cp.struct_type == sizeof(cp)) &&
274753ac610SCharles Manning 		    (cp.magic == YAFFS_MAGIC) &&
275753ac610SCharles Manning 		    (cp.version == YAFFS_CHECKPOINT_VERSION) &&
276753ac610SCharles Manning 		    (cp.head == ((head) ? 1 : 0));
277753ac610SCharles Manning 	return ok ? 1 : 0;
278753ac610SCharles Manning }
279753ac610SCharles Manning 
yaffs2_dev_to_checkpt_dev(struct yaffs_checkpt_dev * cp,struct yaffs_dev * dev)280753ac610SCharles Manning static void yaffs2_dev_to_checkpt_dev(struct yaffs_checkpt_dev *cp,
281753ac610SCharles Manning 				      struct yaffs_dev *dev)
282753ac610SCharles Manning {
283753ac610SCharles Manning 	cp->n_erased_blocks = dev->n_erased_blocks;
284753ac610SCharles Manning 	cp->alloc_block = dev->alloc_block;
285753ac610SCharles Manning 	cp->alloc_page = dev->alloc_page;
286753ac610SCharles Manning 	cp->n_free_chunks = dev->n_free_chunks;
287753ac610SCharles Manning 
288753ac610SCharles Manning 	cp->n_deleted_files = dev->n_deleted_files;
289753ac610SCharles Manning 	cp->n_unlinked_files = dev->n_unlinked_files;
290753ac610SCharles Manning 	cp->n_bg_deletions = dev->n_bg_deletions;
291753ac610SCharles Manning 	cp->seq_number = dev->seq_number;
292753ac610SCharles Manning 
293753ac610SCharles Manning }
294753ac610SCharles Manning 
yaffs_checkpt_dev_to_dev(struct yaffs_dev * dev,struct yaffs_checkpt_dev * cp)295753ac610SCharles Manning static void yaffs_checkpt_dev_to_dev(struct yaffs_dev *dev,
296753ac610SCharles Manning 				     struct yaffs_checkpt_dev *cp)
297753ac610SCharles Manning {
298753ac610SCharles Manning 	dev->n_erased_blocks = cp->n_erased_blocks;
299753ac610SCharles Manning 	dev->alloc_block = cp->alloc_block;
300753ac610SCharles Manning 	dev->alloc_page = cp->alloc_page;
301753ac610SCharles Manning 	dev->n_free_chunks = cp->n_free_chunks;
302753ac610SCharles Manning 
303753ac610SCharles Manning 	dev->n_deleted_files = cp->n_deleted_files;
304753ac610SCharles Manning 	dev->n_unlinked_files = cp->n_unlinked_files;
305753ac610SCharles Manning 	dev->n_bg_deletions = cp->n_bg_deletions;
306753ac610SCharles Manning 	dev->seq_number = cp->seq_number;
307753ac610SCharles Manning }
308753ac610SCharles Manning 
yaffs2_wr_checkpt_dev(struct yaffs_dev * dev)309753ac610SCharles Manning static int yaffs2_wr_checkpt_dev(struct yaffs_dev *dev)
310753ac610SCharles Manning {
311753ac610SCharles Manning 	struct yaffs_checkpt_dev cp;
312753ac610SCharles Manning 	u32 n_bytes;
313753ac610SCharles Manning 	u32 n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
314753ac610SCharles Manning 	int ok;
315753ac610SCharles Manning 
316753ac610SCharles Manning 	/* Write device runtime values */
317753ac610SCharles Manning 	yaffs2_dev_to_checkpt_dev(&cp, dev);
318753ac610SCharles Manning 	cp.struct_type = sizeof(cp);
319753ac610SCharles Manning 
320753ac610SCharles Manning 	ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
321753ac610SCharles Manning 	if (!ok)
322753ac610SCharles Manning 		return 0;
323753ac610SCharles Manning 
324753ac610SCharles Manning 	/* Write block info */
325753ac610SCharles Manning 	n_bytes = n_blocks * sizeof(struct yaffs_block_info);
326753ac610SCharles Manning 	ok = (yaffs2_checkpt_wr(dev, dev->block_info, n_bytes) == n_bytes);
327753ac610SCharles Manning 	if (!ok)
328753ac610SCharles Manning 		return 0;
329753ac610SCharles Manning 
330753ac610SCharles Manning 	/* Write chunk bits */
331753ac610SCharles Manning 	n_bytes = n_blocks * dev->chunk_bit_stride;
332753ac610SCharles Manning 	ok = (yaffs2_checkpt_wr(dev, dev->chunk_bits, n_bytes) == n_bytes);
333753ac610SCharles Manning 
334753ac610SCharles Manning 	return ok ? 1 : 0;
335753ac610SCharles Manning }
336753ac610SCharles Manning 
yaffs2_rd_checkpt_dev(struct yaffs_dev * dev)337753ac610SCharles Manning static int yaffs2_rd_checkpt_dev(struct yaffs_dev *dev)
338753ac610SCharles Manning {
339753ac610SCharles Manning 	struct yaffs_checkpt_dev cp;
340753ac610SCharles Manning 	u32 n_bytes;
341753ac610SCharles Manning 	u32 n_blocks =
342753ac610SCharles Manning 	    (dev->internal_end_block - dev->internal_start_block + 1);
343753ac610SCharles Manning 	int ok;
344753ac610SCharles Manning 
345753ac610SCharles Manning 	ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
346753ac610SCharles Manning 	if (!ok)
347753ac610SCharles Manning 		return 0;
348753ac610SCharles Manning 
349753ac610SCharles Manning 	if (cp.struct_type != sizeof(cp))
350753ac610SCharles Manning 		return 0;
351753ac610SCharles Manning 
352753ac610SCharles Manning 	yaffs_checkpt_dev_to_dev(dev, &cp);
353753ac610SCharles Manning 
354753ac610SCharles Manning 	n_bytes = n_blocks * sizeof(struct yaffs_block_info);
355753ac610SCharles Manning 
356753ac610SCharles Manning 	ok = (yaffs2_checkpt_rd(dev, dev->block_info, n_bytes) == n_bytes);
357753ac610SCharles Manning 
358753ac610SCharles Manning 	if (!ok)
359753ac610SCharles Manning 		return 0;
360753ac610SCharles Manning 
361753ac610SCharles Manning 	n_bytes = n_blocks * dev->chunk_bit_stride;
362753ac610SCharles Manning 
363753ac610SCharles Manning 	ok = (yaffs2_checkpt_rd(dev, dev->chunk_bits, n_bytes) == n_bytes);
364753ac610SCharles Manning 
365753ac610SCharles Manning 	return ok ? 1 : 0;
366753ac610SCharles Manning }
367753ac610SCharles Manning 
yaffs2_obj_checkpt_obj(struct yaffs_checkpt_obj * cp,struct yaffs_obj * obj)368753ac610SCharles Manning static void yaffs2_obj_checkpt_obj(struct yaffs_checkpt_obj *cp,
369753ac610SCharles Manning 				   struct yaffs_obj *obj)
370753ac610SCharles Manning {
371753ac610SCharles Manning 	cp->obj_id = obj->obj_id;
372753ac610SCharles Manning 	cp->parent_id = (obj->parent) ? obj->parent->obj_id : 0;
373753ac610SCharles Manning 	cp->hdr_chunk = obj->hdr_chunk;
374753ac610SCharles Manning 	cp->variant_type = obj->variant_type;
375753ac610SCharles Manning 	cp->deleted = obj->deleted;
376753ac610SCharles Manning 	cp->soft_del = obj->soft_del;
377753ac610SCharles Manning 	cp->unlinked = obj->unlinked;
378753ac610SCharles Manning 	cp->fake = obj->fake;
379753ac610SCharles Manning 	cp->rename_allowed = obj->rename_allowed;
380753ac610SCharles Manning 	cp->unlink_allowed = obj->unlink_allowed;
381753ac610SCharles Manning 	cp->serial = obj->serial;
382753ac610SCharles Manning 	cp->n_data_chunks = obj->n_data_chunks;
383753ac610SCharles Manning 
384753ac610SCharles Manning 	if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
385753ac610SCharles Manning 		cp->size_or_equiv_obj = obj->variant.file_variant.file_size;
386753ac610SCharles Manning 	else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK)
387753ac610SCharles Manning 		cp->size_or_equiv_obj = obj->variant.hardlink_variant.equiv_id;
388753ac610SCharles Manning }
389753ac610SCharles Manning 
yaffs2_checkpt_obj_to_obj(struct yaffs_obj * obj,struct yaffs_checkpt_obj * cp)390753ac610SCharles Manning static int yaffs2_checkpt_obj_to_obj(struct yaffs_obj *obj,
391753ac610SCharles Manning 				     struct yaffs_checkpt_obj *cp)
392753ac610SCharles Manning {
393753ac610SCharles Manning 	struct yaffs_obj *parent;
394753ac610SCharles Manning 
395753ac610SCharles Manning 	if (obj->variant_type != cp->variant_type) {
396753ac610SCharles Manning 		yaffs_trace(YAFFS_TRACE_ERROR,
397753ac610SCharles Manning 			"Checkpoint read object %d type %d chunk %d does not match existing object type %d",
398753ac610SCharles Manning 			cp->obj_id, cp->variant_type, cp->hdr_chunk,
399753ac610SCharles Manning 			obj->variant_type);
400753ac610SCharles Manning 		return 0;
401753ac610SCharles Manning 	}
402753ac610SCharles Manning 
403753ac610SCharles Manning 	obj->obj_id = cp->obj_id;
404753ac610SCharles Manning 
405753ac610SCharles Manning 	if (cp->parent_id)
406753ac610SCharles Manning 		parent = yaffs_find_or_create_by_number(obj->my_dev,
407753ac610SCharles Manning 						cp->parent_id,
408753ac610SCharles Manning 						YAFFS_OBJECT_TYPE_DIRECTORY);
409753ac610SCharles Manning 	else
410753ac610SCharles Manning 		parent = NULL;
411753ac610SCharles Manning 
412753ac610SCharles Manning 	if (parent) {
413753ac610SCharles Manning 		if (parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
414753ac610SCharles Manning 			yaffs_trace(YAFFS_TRACE_ALWAYS,
415753ac610SCharles Manning 				"Checkpoint read object %d parent %d type %d chunk %d Parent type, %d, not directory",
416753ac610SCharles Manning 				cp->obj_id, cp->parent_id,
417753ac610SCharles Manning 				cp->variant_type, cp->hdr_chunk,
418753ac610SCharles Manning 				parent->variant_type);
419753ac610SCharles Manning 			return 0;
420753ac610SCharles Manning 		}
421753ac610SCharles Manning 		yaffs_add_obj_to_dir(parent, obj);
422753ac610SCharles Manning 	}
423753ac610SCharles Manning 
424753ac610SCharles Manning 	obj->hdr_chunk = cp->hdr_chunk;
425753ac610SCharles Manning 	obj->variant_type = cp->variant_type;
426753ac610SCharles Manning 	obj->deleted = cp->deleted;
427753ac610SCharles Manning 	obj->soft_del = cp->soft_del;
428753ac610SCharles Manning 	obj->unlinked = cp->unlinked;
429753ac610SCharles Manning 	obj->fake = cp->fake;
430753ac610SCharles Manning 	obj->rename_allowed = cp->rename_allowed;
431753ac610SCharles Manning 	obj->unlink_allowed = cp->unlink_allowed;
432753ac610SCharles Manning 	obj->serial = cp->serial;
433753ac610SCharles Manning 	obj->n_data_chunks = cp->n_data_chunks;
434753ac610SCharles Manning 
435753ac610SCharles Manning 	if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
436753ac610SCharles Manning 		obj->variant.file_variant.file_size = cp->size_or_equiv_obj;
437753ac610SCharles Manning 	else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK)
438753ac610SCharles Manning 		obj->variant.hardlink_variant.equiv_id = cp->size_or_equiv_obj;
439753ac610SCharles Manning 
440753ac610SCharles Manning 	if (obj->hdr_chunk > 0)
441753ac610SCharles Manning 		obj->lazy_loaded = 1;
442753ac610SCharles Manning 	return 1;
443753ac610SCharles Manning }
444753ac610SCharles Manning 
yaffs2_checkpt_tnode_worker(struct yaffs_obj * in,struct yaffs_tnode * tn,u32 level,int chunk_offset)445753ac610SCharles Manning static int yaffs2_checkpt_tnode_worker(struct yaffs_obj *in,
446753ac610SCharles Manning 				       struct yaffs_tnode *tn, u32 level,
447753ac610SCharles Manning 				       int chunk_offset)
448753ac610SCharles Manning {
449753ac610SCharles Manning 	int i;
450753ac610SCharles Manning 	struct yaffs_dev *dev = in->my_dev;
451753ac610SCharles Manning 	int ok = 1;
452753ac610SCharles Manning 	u32 base_offset;
453753ac610SCharles Manning 
454753ac610SCharles Manning 	if (!tn)
455753ac610SCharles Manning 		return 1;
456753ac610SCharles Manning 
457753ac610SCharles Manning 	if (level > 0) {
458753ac610SCharles Manning 		for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
459753ac610SCharles Manning 			if (!tn->internal[i])
460753ac610SCharles Manning 				continue;
461753ac610SCharles Manning 			ok = yaffs2_checkpt_tnode_worker(in,
462753ac610SCharles Manning 				 tn->internal[i],
463753ac610SCharles Manning 				 level - 1,
464753ac610SCharles Manning 				 (chunk_offset <<
465753ac610SCharles Manning 				  YAFFS_TNODES_INTERNAL_BITS) + i);
466753ac610SCharles Manning 		}
467753ac610SCharles Manning 		return ok;
468753ac610SCharles Manning 	}
469753ac610SCharles Manning 
470753ac610SCharles Manning 	/* Level 0 tnode */
471753ac610SCharles Manning 	base_offset = chunk_offset << YAFFS_TNODES_LEVEL0_BITS;
472753ac610SCharles Manning 	ok = (yaffs2_checkpt_wr(dev, &base_offset, sizeof(base_offset)) ==
473753ac610SCharles Manning 			sizeof(base_offset));
474753ac610SCharles Manning 	if (ok)
475753ac610SCharles Manning 		ok = (yaffs2_checkpt_wr(dev, tn, dev->tnode_size) ==
476753ac610SCharles Manning 			dev->tnode_size);
477753ac610SCharles Manning 
478753ac610SCharles Manning 	return ok;
479753ac610SCharles Manning }
480753ac610SCharles Manning 
yaffs2_wr_checkpt_tnodes(struct yaffs_obj * obj)481753ac610SCharles Manning static int yaffs2_wr_checkpt_tnodes(struct yaffs_obj *obj)
482753ac610SCharles Manning {
483753ac610SCharles Manning 	u32 end_marker = ~0;
484753ac610SCharles Manning 	int ok = 1;
485753ac610SCharles Manning 
486753ac610SCharles Manning 	if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
487753ac610SCharles Manning 		return ok;
488753ac610SCharles Manning 
489753ac610SCharles Manning 	ok = yaffs2_checkpt_tnode_worker(obj,
490753ac610SCharles Manning 					 obj->variant.file_variant.top,
491753ac610SCharles Manning 					 obj->variant.file_variant.
492753ac610SCharles Manning 					 top_level, 0);
493753ac610SCharles Manning 	if (ok)
494753ac610SCharles Manning 		ok = (yaffs2_checkpt_wr(obj->my_dev, &end_marker,
495753ac610SCharles Manning 				sizeof(end_marker)) == sizeof(end_marker));
496753ac610SCharles Manning 
497753ac610SCharles Manning 	return ok ? 1 : 0;
498753ac610SCharles Manning }
499753ac610SCharles Manning 
yaffs2_rd_checkpt_tnodes(struct yaffs_obj * obj)500753ac610SCharles Manning static int yaffs2_rd_checkpt_tnodes(struct yaffs_obj *obj)
501753ac610SCharles Manning {
502753ac610SCharles Manning 	u32 base_chunk;
503753ac610SCharles Manning 	int ok = 1;
504753ac610SCharles Manning 	struct yaffs_dev *dev = obj->my_dev;
505753ac610SCharles Manning 	struct yaffs_file_var *file_stuct_ptr = &obj->variant.file_variant;
506753ac610SCharles Manning 	struct yaffs_tnode *tn;
507753ac610SCharles Manning 	int nread = 0;
508753ac610SCharles Manning 
509753ac610SCharles Manning 	ok = (yaffs2_checkpt_rd(dev, &base_chunk, sizeof(base_chunk)) ==
510753ac610SCharles Manning 	      sizeof(base_chunk));
511753ac610SCharles Manning 
512753ac610SCharles Manning 	while (ok && (~base_chunk)) {
513753ac610SCharles Manning 		nread++;
514753ac610SCharles Manning 		/* Read level 0 tnode */
515753ac610SCharles Manning 
516753ac610SCharles Manning 		tn = yaffs_get_tnode(dev);
517753ac610SCharles Manning 		if (tn)
518753ac610SCharles Manning 			ok = (yaffs2_checkpt_rd(dev, tn, dev->tnode_size) ==
519753ac610SCharles Manning 				dev->tnode_size);
520753ac610SCharles Manning 		else
521753ac610SCharles Manning 			ok = 0;
522753ac610SCharles Manning 
523753ac610SCharles Manning 		if (tn && ok)
524753ac610SCharles Manning 			ok = yaffs_add_find_tnode_0(dev,
525753ac610SCharles Manning 						    file_stuct_ptr,
526753ac610SCharles Manning 						    base_chunk, tn) ? 1 : 0;
527753ac610SCharles Manning 
528753ac610SCharles Manning 		if (ok)
529753ac610SCharles Manning 			ok = (yaffs2_checkpt_rd
530753ac610SCharles Manning 			      (dev, &base_chunk,
531753ac610SCharles Manning 			       sizeof(base_chunk)) == sizeof(base_chunk));
532753ac610SCharles Manning 	}
533753ac610SCharles Manning 
534753ac610SCharles Manning 	yaffs_trace(YAFFS_TRACE_CHECKPOINT,
535753ac610SCharles Manning 		"Checkpoint read tnodes %d records, last %d. ok %d",
536753ac610SCharles Manning 		nread, base_chunk, ok);
537753ac610SCharles Manning 
538753ac610SCharles Manning 	return ok ? 1 : 0;
539753ac610SCharles Manning }
540753ac610SCharles Manning 
yaffs2_wr_checkpt_objs(struct yaffs_dev * dev)541753ac610SCharles Manning static int yaffs2_wr_checkpt_objs(struct yaffs_dev *dev)
542753ac610SCharles Manning {
543753ac610SCharles Manning 	struct yaffs_obj *obj;
544753ac610SCharles Manning 	struct yaffs_checkpt_obj cp;
545753ac610SCharles Manning 	int i;
546753ac610SCharles Manning 	int ok = 1;
547753ac610SCharles Manning 	struct list_head *lh;
548753ac610SCharles Manning 
549753ac610SCharles Manning 	/* Iterate through the objects in each hash entry,
550753ac610SCharles Manning 	 * dumping them to the checkpointing stream.
551753ac610SCharles Manning 	 */
552753ac610SCharles Manning 
553753ac610SCharles Manning 	for (i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++) {
554753ac610SCharles Manning 		list_for_each(lh, &dev->obj_bucket[i].list) {
555753ac610SCharles Manning 			obj = list_entry(lh, struct yaffs_obj, hash_link);
556753ac610SCharles Manning 			if (!obj->defered_free) {
557753ac610SCharles Manning 				yaffs2_obj_checkpt_obj(&cp, obj);
558753ac610SCharles Manning 				cp.struct_type = sizeof(cp);
559753ac610SCharles Manning 
560753ac610SCharles Manning 				yaffs_trace(YAFFS_TRACE_CHECKPOINT,
561753ac610SCharles Manning 					"Checkpoint write object %d parent %d type %d chunk %d obj addr %p",
562753ac610SCharles Manning 					cp.obj_id, cp.parent_id,
563753ac610SCharles Manning 					cp.variant_type, cp.hdr_chunk, obj);
564753ac610SCharles Manning 
565753ac610SCharles Manning 				ok = (yaffs2_checkpt_wr(dev, &cp,
566753ac610SCharles Manning 						sizeof(cp)) == sizeof(cp));
567753ac610SCharles Manning 
568753ac610SCharles Manning 				if (ok &&
569753ac610SCharles Manning 					obj->variant_type ==
570753ac610SCharles Manning 					YAFFS_OBJECT_TYPE_FILE)
571753ac610SCharles Manning 					ok = yaffs2_wr_checkpt_tnodes(obj);
572753ac610SCharles Manning 			}
573753ac610SCharles Manning 		}
574753ac610SCharles Manning 	}
575753ac610SCharles Manning 
576753ac610SCharles Manning 	/* Dump end of list */
577753ac610SCharles Manning 	memset(&cp, 0xff, sizeof(struct yaffs_checkpt_obj));
578753ac610SCharles Manning 	cp.struct_type = sizeof(cp);
579753ac610SCharles Manning 
580753ac610SCharles Manning 	if (ok)
581753ac610SCharles Manning 		ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
582753ac610SCharles Manning 
583753ac610SCharles Manning 	return ok ? 1 : 0;
584753ac610SCharles Manning }
585753ac610SCharles Manning 
yaffs2_rd_checkpt_objs(struct yaffs_dev * dev)586753ac610SCharles Manning static int yaffs2_rd_checkpt_objs(struct yaffs_dev *dev)
587753ac610SCharles Manning {
588753ac610SCharles Manning 	struct yaffs_obj *obj;
589753ac610SCharles Manning 	struct yaffs_checkpt_obj cp;
590753ac610SCharles Manning 	int ok = 1;
591753ac610SCharles Manning 	int done = 0;
592753ac610SCharles Manning 	LIST_HEAD(hard_list);
593753ac610SCharles Manning 
594753ac610SCharles Manning 
595753ac610SCharles Manning 	while (ok && !done) {
596753ac610SCharles Manning 		ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
597753ac610SCharles Manning 		if (cp.struct_type != sizeof(cp)) {
598753ac610SCharles Manning 			yaffs_trace(YAFFS_TRACE_CHECKPOINT,
599753ac610SCharles Manning 				"struct size %d instead of %d ok %d",
600753ac610SCharles Manning 				cp.struct_type, (int)sizeof(cp), ok);
601753ac610SCharles Manning 			ok = 0;
602753ac610SCharles Manning 		}
603753ac610SCharles Manning 
604753ac610SCharles Manning 		yaffs_trace(YAFFS_TRACE_CHECKPOINT,
605753ac610SCharles Manning 			"Checkpoint read object %d parent %d type %d chunk %d ",
606753ac610SCharles Manning 			cp.obj_id, cp.parent_id, cp.variant_type,
607753ac610SCharles Manning 			cp.hdr_chunk);
608753ac610SCharles Manning 
609753ac610SCharles Manning 		if (ok && cp.obj_id == ~0) {
610753ac610SCharles Manning 			done = 1;
611753ac610SCharles Manning 		} else if (ok) {
612753ac610SCharles Manning 			obj =
613753ac610SCharles Manning 			    yaffs_find_or_create_by_number(dev, cp.obj_id,
614753ac610SCharles Manning 							   cp.variant_type);
615753ac610SCharles Manning 			if (obj) {
616753ac610SCharles Manning 				ok = yaffs2_checkpt_obj_to_obj(obj, &cp);
617753ac610SCharles Manning 				if (!ok)
618753ac610SCharles Manning 					break;
619753ac610SCharles Manning 				if (obj->variant_type ==
620753ac610SCharles Manning 					YAFFS_OBJECT_TYPE_FILE) {
621753ac610SCharles Manning 					ok = yaffs2_rd_checkpt_tnodes(obj);
622753ac610SCharles Manning 				} else if (obj->variant_type ==
623753ac610SCharles Manning 					YAFFS_OBJECT_TYPE_HARDLINK) {
624753ac610SCharles Manning 					list_add(&obj->hard_links, &hard_list);
625753ac610SCharles Manning 				}
626753ac610SCharles Manning 			} else {
627753ac610SCharles Manning 				ok = 0;
628753ac610SCharles Manning 			}
629753ac610SCharles Manning 		}
630753ac610SCharles Manning 	}
631753ac610SCharles Manning 
632753ac610SCharles Manning 	if (ok)
633753ac610SCharles Manning 		yaffs_link_fixup(dev, &hard_list);
634753ac610SCharles Manning 
635753ac610SCharles Manning 	return ok ? 1 : 0;
636753ac610SCharles Manning }
637753ac610SCharles Manning 
yaffs2_wr_checkpt_sum(struct yaffs_dev * dev)638753ac610SCharles Manning static int yaffs2_wr_checkpt_sum(struct yaffs_dev *dev)
639753ac610SCharles Manning {
640753ac610SCharles Manning 	u32 checkpt_sum;
641753ac610SCharles Manning 	int ok;
642753ac610SCharles Manning 
643753ac610SCharles Manning 	yaffs2_get_checkpt_sum(dev, &checkpt_sum);
644753ac610SCharles Manning 
645753ac610SCharles Manning 	ok = (yaffs2_checkpt_wr(dev, &checkpt_sum, sizeof(checkpt_sum)) ==
646753ac610SCharles Manning 		sizeof(checkpt_sum));
647753ac610SCharles Manning 
648753ac610SCharles Manning 	if (!ok)
649753ac610SCharles Manning 		return 0;
650753ac610SCharles Manning 
651753ac610SCharles Manning 	return 1;
652753ac610SCharles Manning }
653753ac610SCharles Manning 
yaffs2_rd_checkpt_sum(struct yaffs_dev * dev)654753ac610SCharles Manning static int yaffs2_rd_checkpt_sum(struct yaffs_dev *dev)
655753ac610SCharles Manning {
656753ac610SCharles Manning 	u32 checkpt_sum0;
657753ac610SCharles Manning 	u32 checkpt_sum1;
658753ac610SCharles Manning 	int ok;
659753ac610SCharles Manning 
660753ac610SCharles Manning 	yaffs2_get_checkpt_sum(dev, &checkpt_sum0);
661753ac610SCharles Manning 
662753ac610SCharles Manning 	ok = (yaffs2_checkpt_rd(dev, &checkpt_sum1, sizeof(checkpt_sum1)) ==
663753ac610SCharles Manning 		sizeof(checkpt_sum1));
664753ac610SCharles Manning 
665753ac610SCharles Manning 	if (!ok)
666753ac610SCharles Manning 		return 0;
667753ac610SCharles Manning 
668753ac610SCharles Manning 	if (checkpt_sum0 != checkpt_sum1)
669753ac610SCharles Manning 		return 0;
670753ac610SCharles Manning 
671753ac610SCharles Manning 	return 1;
672753ac610SCharles Manning }
673753ac610SCharles Manning 
yaffs2_wr_checkpt_data(struct yaffs_dev * dev)674753ac610SCharles Manning static int yaffs2_wr_checkpt_data(struct yaffs_dev *dev)
675753ac610SCharles Manning {
676753ac610SCharles Manning 	int ok = 1;
677753ac610SCharles Manning 
678753ac610SCharles Manning 	if (!yaffs2_checkpt_required(dev)) {
679753ac610SCharles Manning 		yaffs_trace(YAFFS_TRACE_CHECKPOINT,
680753ac610SCharles Manning 			"skipping checkpoint write");
681753ac610SCharles Manning 		ok = 0;
682753ac610SCharles Manning 	}
683753ac610SCharles Manning 
684753ac610SCharles Manning 	if (ok)
685753ac610SCharles Manning 		ok = yaffs2_checkpt_open(dev, 1);
686753ac610SCharles Manning 
687753ac610SCharles Manning 	if (ok) {
688753ac610SCharles Manning 		yaffs_trace(YAFFS_TRACE_CHECKPOINT,
689753ac610SCharles Manning 			"write checkpoint validity");
690753ac610SCharles Manning 		ok = yaffs2_wr_checkpt_validity_marker(dev, 1);
691753ac610SCharles Manning 	}
692753ac610SCharles Manning 	if (ok) {
693753ac610SCharles Manning 		yaffs_trace(YAFFS_TRACE_CHECKPOINT,
694753ac610SCharles Manning 			"write checkpoint device");
695753ac610SCharles Manning 		ok = yaffs2_wr_checkpt_dev(dev);
696753ac610SCharles Manning 	}
697753ac610SCharles Manning 	if (ok) {
698753ac610SCharles Manning 		yaffs_trace(YAFFS_TRACE_CHECKPOINT,
699753ac610SCharles Manning 			"write checkpoint objects");
700753ac610SCharles Manning 		ok = yaffs2_wr_checkpt_objs(dev);
701753ac610SCharles Manning 	}
702753ac610SCharles Manning 	if (ok) {
703753ac610SCharles Manning 		yaffs_trace(YAFFS_TRACE_CHECKPOINT,
704753ac610SCharles Manning 			"write checkpoint validity");
705753ac610SCharles Manning 		ok = yaffs2_wr_checkpt_validity_marker(dev, 0);
706753ac610SCharles Manning 	}
707753ac610SCharles Manning 
708753ac610SCharles Manning 	if (ok)
709753ac610SCharles Manning 		ok = yaffs2_wr_checkpt_sum(dev);
710753ac610SCharles Manning 
711753ac610SCharles Manning 	if (!yaffs_checkpt_close(dev))
712753ac610SCharles Manning 		ok = 0;
713753ac610SCharles Manning 
714753ac610SCharles Manning 	if (ok)
715753ac610SCharles Manning 		dev->is_checkpointed = 1;
716753ac610SCharles Manning 	else
717753ac610SCharles Manning 		dev->is_checkpointed = 0;
718753ac610SCharles Manning 
719753ac610SCharles Manning 	return dev->is_checkpointed;
720753ac610SCharles Manning }
721753ac610SCharles Manning 
yaffs2_rd_checkpt_data(struct yaffs_dev * dev)722753ac610SCharles Manning static int yaffs2_rd_checkpt_data(struct yaffs_dev *dev)
723753ac610SCharles Manning {
724753ac610SCharles Manning 	int ok = 1;
725753ac610SCharles Manning 
726753ac610SCharles Manning 	if (!dev->param.is_yaffs2)
727753ac610SCharles Manning 		ok = 0;
728753ac610SCharles Manning 
729753ac610SCharles Manning 	if (ok && dev->param.skip_checkpt_rd) {
730753ac610SCharles Manning 		yaffs_trace(YAFFS_TRACE_CHECKPOINT,
731753ac610SCharles Manning 			"skipping checkpoint read");
732753ac610SCharles Manning 		ok = 0;
733753ac610SCharles Manning 	}
734753ac610SCharles Manning 
735753ac610SCharles Manning 	if (ok)
736753ac610SCharles Manning 		ok = yaffs2_checkpt_open(dev, 0); /* open for read */
737753ac610SCharles Manning 
738753ac610SCharles Manning 	if (ok) {
739753ac610SCharles Manning 		yaffs_trace(YAFFS_TRACE_CHECKPOINT,
740753ac610SCharles Manning 			"read checkpoint validity");
741753ac610SCharles Manning 		ok = yaffs2_rd_checkpt_validity_marker(dev, 1);
742753ac610SCharles Manning 	}
743753ac610SCharles Manning 	if (ok) {
744753ac610SCharles Manning 		yaffs_trace(YAFFS_TRACE_CHECKPOINT,
745753ac610SCharles Manning 			"read checkpoint device");
746753ac610SCharles Manning 		ok = yaffs2_rd_checkpt_dev(dev);
747753ac610SCharles Manning 	}
748753ac610SCharles Manning 	if (ok) {
749753ac610SCharles Manning 		yaffs_trace(YAFFS_TRACE_CHECKPOINT,
750753ac610SCharles Manning 			"read checkpoint objects");
751753ac610SCharles Manning 		ok = yaffs2_rd_checkpt_objs(dev);
752753ac610SCharles Manning 	}
753753ac610SCharles Manning 	if (ok) {
754753ac610SCharles Manning 		yaffs_trace(YAFFS_TRACE_CHECKPOINT,
755753ac610SCharles Manning 			"read checkpoint validity");
756753ac610SCharles Manning 		ok = yaffs2_rd_checkpt_validity_marker(dev, 0);
757753ac610SCharles Manning 	}
758753ac610SCharles Manning 
759753ac610SCharles Manning 	if (ok) {
760753ac610SCharles Manning 		ok = yaffs2_rd_checkpt_sum(dev);
761753ac610SCharles Manning 		yaffs_trace(YAFFS_TRACE_CHECKPOINT,
762753ac610SCharles Manning 			"read checkpoint checksum %d", ok);
763753ac610SCharles Manning 	}
764753ac610SCharles Manning 
765753ac610SCharles Manning 	if (!yaffs_checkpt_close(dev))
766753ac610SCharles Manning 		ok = 0;
767753ac610SCharles Manning 
768753ac610SCharles Manning 	if (ok)
769753ac610SCharles Manning 		dev->is_checkpointed = 1;
770753ac610SCharles Manning 	else
771753ac610SCharles Manning 		dev->is_checkpointed = 0;
772753ac610SCharles Manning 
773753ac610SCharles Manning 	return ok ? 1 : 0;
774753ac610SCharles Manning }
775753ac610SCharles Manning 
yaffs2_checkpt_invalidate(struct yaffs_dev * dev)776753ac610SCharles Manning void yaffs2_checkpt_invalidate(struct yaffs_dev *dev)
777753ac610SCharles Manning {
778753ac610SCharles Manning 	if (dev->is_checkpointed || dev->blocks_in_checkpt > 0) {
779753ac610SCharles Manning 		dev->is_checkpointed = 0;
780753ac610SCharles Manning 		yaffs2_checkpt_invalidate_stream(dev);
781753ac610SCharles Manning 	}
782753ac610SCharles Manning 	if (dev->param.sb_dirty_fn)
783753ac610SCharles Manning 		dev->param.sb_dirty_fn(dev);
784753ac610SCharles Manning }
785753ac610SCharles Manning 
yaffs_checkpoint_save(struct yaffs_dev * dev)786753ac610SCharles Manning int yaffs_checkpoint_save(struct yaffs_dev *dev)
787753ac610SCharles Manning {
788753ac610SCharles Manning 	yaffs_trace(YAFFS_TRACE_CHECKPOINT,
789753ac610SCharles Manning 		"save entry: is_checkpointed %d",
790753ac610SCharles Manning 		dev->is_checkpointed);
791753ac610SCharles Manning 
792753ac610SCharles Manning 	yaffs_verify_objects(dev);
793753ac610SCharles Manning 	yaffs_verify_blocks(dev);
794753ac610SCharles Manning 	yaffs_verify_free_chunks(dev);
795753ac610SCharles Manning 
796753ac610SCharles Manning 	if (!dev->is_checkpointed) {
797753ac610SCharles Manning 		yaffs2_checkpt_invalidate(dev);
798753ac610SCharles Manning 		yaffs2_wr_checkpt_data(dev);
799753ac610SCharles Manning 	}
800753ac610SCharles Manning 
801753ac610SCharles Manning 	yaffs_trace(YAFFS_TRACE_CHECKPOINT | YAFFS_TRACE_MOUNT,
802753ac610SCharles Manning 		"save exit: is_checkpointed %d",
803753ac610SCharles Manning 		dev->is_checkpointed);
804753ac610SCharles Manning 
805753ac610SCharles Manning 	return dev->is_checkpointed;
806753ac610SCharles Manning }
807753ac610SCharles Manning 
yaffs2_checkpt_restore(struct yaffs_dev * dev)808753ac610SCharles Manning int yaffs2_checkpt_restore(struct yaffs_dev *dev)
809753ac610SCharles Manning {
810753ac610SCharles Manning 	int retval;
811753ac610SCharles Manning 
812753ac610SCharles Manning 	yaffs_trace(YAFFS_TRACE_CHECKPOINT,
813753ac610SCharles Manning 		"restore entry: is_checkpointed %d",
814753ac610SCharles Manning 		dev->is_checkpointed);
815753ac610SCharles Manning 
816753ac610SCharles Manning 	retval = yaffs2_rd_checkpt_data(dev);
817753ac610SCharles Manning 
818753ac610SCharles Manning 	if (dev->is_checkpointed) {
819753ac610SCharles Manning 		yaffs_verify_objects(dev);
820753ac610SCharles Manning 		yaffs_verify_blocks(dev);
821753ac610SCharles Manning 		yaffs_verify_free_chunks(dev);
822753ac610SCharles Manning 	}
823753ac610SCharles Manning 
824753ac610SCharles Manning 	yaffs_trace(YAFFS_TRACE_CHECKPOINT,
825753ac610SCharles Manning 		"restore exit: is_checkpointed %d",
826753ac610SCharles Manning 		dev->is_checkpointed);
827753ac610SCharles Manning 
828753ac610SCharles Manning 	return retval;
829753ac610SCharles Manning }
830753ac610SCharles Manning 
yaffs2_handle_hole(struct yaffs_obj * obj,loff_t new_size)831753ac610SCharles Manning int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size)
832753ac610SCharles Manning {
833753ac610SCharles Manning 	/* if new_size > old_file_size.
834753ac610SCharles Manning 	 * We're going to be writing a hole.
835753ac610SCharles Manning 	 * If the hole is small then write zeros otherwise write a start
836753ac610SCharles Manning 	 * of hole marker.
837753ac610SCharles Manning 	 */
838753ac610SCharles Manning 	loff_t old_file_size;
839753ac610SCharles Manning 	loff_t increase;
840753ac610SCharles Manning 	int small_hole;
841753ac610SCharles Manning 	int result = YAFFS_OK;
842753ac610SCharles Manning 	struct yaffs_dev *dev = NULL;
843753ac610SCharles Manning 	u8 *local_buffer = NULL;
844753ac610SCharles Manning 	int small_increase_ok = 0;
845753ac610SCharles Manning 
846753ac610SCharles Manning 	if (!obj)
847753ac610SCharles Manning 		return YAFFS_FAIL;
848753ac610SCharles Manning 
849753ac610SCharles Manning 	if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
850753ac610SCharles Manning 		return YAFFS_FAIL;
851753ac610SCharles Manning 
852753ac610SCharles Manning 	dev = obj->my_dev;
853753ac610SCharles Manning 
854753ac610SCharles Manning 	/* Bail out if not yaffs2 mode */
855753ac610SCharles Manning 	if (!dev->param.is_yaffs2)
856753ac610SCharles Manning 		return YAFFS_OK;
857753ac610SCharles Manning 
858753ac610SCharles Manning 	old_file_size = obj->variant.file_variant.file_size;
859753ac610SCharles Manning 
860753ac610SCharles Manning 	if (new_size <= old_file_size)
861753ac610SCharles Manning 		return YAFFS_OK;
862753ac610SCharles Manning 
863753ac610SCharles Manning 	increase = new_size - old_file_size;
864753ac610SCharles Manning 
865753ac610SCharles Manning 	if (increase < YAFFS_SMALL_HOLE_THRESHOLD * dev->data_bytes_per_chunk &&
866753ac610SCharles Manning 	    yaffs_check_alloc_available(dev, YAFFS_SMALL_HOLE_THRESHOLD + 1))
867753ac610SCharles Manning 		small_hole = 1;
868753ac610SCharles Manning 	else
869753ac610SCharles Manning 		small_hole = 0;
870753ac610SCharles Manning 
871753ac610SCharles Manning 	if (small_hole)
872753ac610SCharles Manning 		local_buffer = yaffs_get_temp_buffer(dev);
873753ac610SCharles Manning 
874753ac610SCharles Manning 	if (local_buffer) {
875753ac610SCharles Manning 		/* fill hole with zero bytes */
876753ac610SCharles Manning 		loff_t pos = old_file_size;
877753ac610SCharles Manning 		int this_write;
878753ac610SCharles Manning 		int written;
879753ac610SCharles Manning 		memset(local_buffer, 0, dev->data_bytes_per_chunk);
880753ac610SCharles Manning 		small_increase_ok = 1;
881753ac610SCharles Manning 
882753ac610SCharles Manning 		while (increase > 0 && small_increase_ok) {
883753ac610SCharles Manning 			this_write = increase;
884753ac610SCharles Manning 			if (this_write > dev->data_bytes_per_chunk)
885753ac610SCharles Manning 				this_write = dev->data_bytes_per_chunk;
886753ac610SCharles Manning 			written =
887753ac610SCharles Manning 			    yaffs_do_file_wr(obj, local_buffer, pos, this_write,
888753ac610SCharles Manning 					     0);
889753ac610SCharles Manning 			if (written == this_write) {
890753ac610SCharles Manning 				pos += this_write;
891753ac610SCharles Manning 				increase -= this_write;
892753ac610SCharles Manning 			} else {
893753ac610SCharles Manning 				small_increase_ok = 0;
894753ac610SCharles Manning 			}
895753ac610SCharles Manning 		}
896753ac610SCharles Manning 
897753ac610SCharles Manning 		yaffs_release_temp_buffer(dev, local_buffer);
898753ac610SCharles Manning 
899753ac610SCharles Manning 		/* If out of space then reverse any chunks we've added */
900753ac610SCharles Manning 		if (!small_increase_ok)
901753ac610SCharles Manning 			yaffs_resize_file_down(obj, old_file_size);
902753ac610SCharles Manning 	}
903753ac610SCharles Manning 
904753ac610SCharles Manning 	if (!small_increase_ok &&
905753ac610SCharles Manning 	    obj->parent &&
906753ac610SCharles Manning 	    obj->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
907753ac610SCharles Manning 	    obj->parent->obj_id != YAFFS_OBJECTID_DELETED) {
908753ac610SCharles Manning 		/* Write a hole start header with the old file size */
909753ac610SCharles Manning 		yaffs_update_oh(obj, NULL, 0, 1, 0, NULL);
910753ac610SCharles Manning 	}
911753ac610SCharles Manning 
912753ac610SCharles Manning 	return result;
913753ac610SCharles Manning }
914753ac610SCharles Manning 
915753ac610SCharles Manning struct yaffs_block_index {
916753ac610SCharles Manning 	int seq;
917753ac610SCharles Manning 	int block;
918753ac610SCharles Manning };
919753ac610SCharles Manning 
yaffs2_ybicmp(const void * a,const void * b)920753ac610SCharles Manning static int yaffs2_ybicmp(const void *a, const void *b)
921753ac610SCharles Manning {
922753ac610SCharles Manning 	int aseq = ((struct yaffs_block_index *)a)->seq;
923753ac610SCharles Manning 	int bseq = ((struct yaffs_block_index *)b)->seq;
924753ac610SCharles Manning 	int ablock = ((struct yaffs_block_index *)a)->block;
925753ac610SCharles Manning 	int bblock = ((struct yaffs_block_index *)b)->block;
926753ac610SCharles Manning 
927753ac610SCharles Manning 	if (aseq == bseq)
928753ac610SCharles Manning 		return ablock - bblock;
929753ac610SCharles Manning 
930753ac610SCharles Manning 	return aseq - bseq;
931753ac610SCharles Manning }
932753ac610SCharles Manning 
yaffs2_scan_chunk(struct yaffs_dev * dev,struct yaffs_block_info * bi,int blk,int chunk_in_block,int * found_chunks,u8 * chunk_data,struct list_head * hard_list,int summary_available)933753ac610SCharles Manning static inline int yaffs2_scan_chunk(struct yaffs_dev *dev,
934753ac610SCharles Manning 		struct yaffs_block_info *bi,
935753ac610SCharles Manning 		int blk, int chunk_in_block,
936753ac610SCharles Manning 		int *found_chunks,
937753ac610SCharles Manning 		u8 *chunk_data,
938753ac610SCharles Manning 		struct list_head *hard_list,
939753ac610SCharles Manning 		int summary_available)
940753ac610SCharles Manning {
941753ac610SCharles Manning 	struct yaffs_obj_hdr *oh;
942753ac610SCharles Manning 	struct yaffs_obj *in;
943753ac610SCharles Manning 	struct yaffs_obj *parent;
944753ac610SCharles Manning 	int equiv_id;
945753ac610SCharles Manning 	loff_t file_size;
946753ac610SCharles Manning 	int is_shrink;
947753ac610SCharles Manning 	int is_unlinked;
948753ac610SCharles Manning 	struct yaffs_ext_tags tags;
949753ac610SCharles Manning 	int alloc_failed = 0;
950753ac610SCharles Manning 	int chunk = blk * dev->param.chunks_per_block + chunk_in_block;
951753ac610SCharles Manning 	struct yaffs_file_var *file_var;
952753ac610SCharles Manning 	struct yaffs_hardlink_var *hl_var;
953753ac610SCharles Manning 	struct yaffs_symlink_var *sl_var;
954753ac610SCharles Manning 
955753ac610SCharles Manning 	if (summary_available) {
956*8cc64bafSAnatolij Gustschin 		yaffs_summary_fetch(dev, &tags, chunk_in_block);
957753ac610SCharles Manning 		tags.seq_number = bi->seq_number;
958753ac610SCharles Manning 	}
959753ac610SCharles Manning 
960753ac610SCharles Manning 	if (!summary_available || tags.obj_id == 0) {
961*8cc64bafSAnatolij Gustschin 		yaffs_rd_chunk_tags_nand(dev, chunk, NULL, &tags);
962753ac610SCharles Manning 		dev->tags_used++;
963753ac610SCharles Manning 	} else {
964753ac610SCharles Manning 		dev->summary_used++;
965753ac610SCharles Manning 	}
966753ac610SCharles Manning 
967753ac610SCharles Manning 	/* Let's have a good look at this chunk... */
968753ac610SCharles Manning 
969753ac610SCharles Manning 	if (!tags.chunk_used) {
970753ac610SCharles Manning 		/* An unassigned chunk in the block.
971753ac610SCharles Manning 		 * If there are used chunks after this one, then
972753ac610SCharles Manning 		 * it is a chunk that was skipped due to failing
973753ac610SCharles Manning 		 * the erased check. Just skip it so that it can
974753ac610SCharles Manning 		 * be deleted.
975753ac610SCharles Manning 		 * But, more typically, We get here when this is
976753ac610SCharles Manning 		 * an unallocated chunk and his means that
977753ac610SCharles Manning 		 * either the block is empty or this is the one
978753ac610SCharles Manning 		 * being allocated from
979753ac610SCharles Manning 		 */
980753ac610SCharles Manning 
981753ac610SCharles Manning 		if (*found_chunks) {
982753ac610SCharles Manning 			/* This is a chunk that was skipped due
983753ac610SCharles Manning 			 * to failing the erased check */
984753ac610SCharles Manning 		} else if (chunk_in_block == 0) {
985753ac610SCharles Manning 			/* We're looking at the first chunk in
986753ac610SCharles Manning 			 * the block so the block is unused */
987753ac610SCharles Manning 			bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
988753ac610SCharles Manning 			dev->n_erased_blocks++;
989753ac610SCharles Manning 		} else {
990753ac610SCharles Manning 			if (bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN ||
991753ac610SCharles Manning 			    bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) {
992753ac610SCharles Manning 				if (dev->seq_number == bi->seq_number) {
993753ac610SCharles Manning 					/* Allocating from this block*/
994753ac610SCharles Manning 					yaffs_trace(YAFFS_TRACE_SCAN,
995753ac610SCharles Manning 					    " Allocating from %d %d",
996753ac610SCharles Manning 					    blk, chunk_in_block);
997753ac610SCharles Manning 
998753ac610SCharles Manning 					bi->block_state =
999753ac610SCharles Manning 						YAFFS_BLOCK_STATE_ALLOCATING;
1000753ac610SCharles Manning 					dev->alloc_block = blk;
1001753ac610SCharles Manning 					dev->alloc_page = chunk_in_block;
1002753ac610SCharles Manning 					dev->alloc_block_finder = blk;
1003753ac610SCharles Manning 				} else {
1004753ac610SCharles Manning 					/* This is a partially written block
1005753ac610SCharles Manning 					 * that is not the current
1006753ac610SCharles Manning 					 * allocation block.
1007753ac610SCharles Manning 					 */
1008753ac610SCharles Manning 					yaffs_trace(YAFFS_TRACE_SCAN,
1009753ac610SCharles Manning 						"Partially written block %d detected. gc will fix this.",
1010753ac610SCharles Manning 						blk);
1011753ac610SCharles Manning 				}
1012753ac610SCharles Manning 			}
1013753ac610SCharles Manning 		}
1014753ac610SCharles Manning 
1015753ac610SCharles Manning 		dev->n_free_chunks++;
1016753ac610SCharles Manning 
1017753ac610SCharles Manning 	} else if (tags.ecc_result ==
1018753ac610SCharles Manning 		YAFFS_ECC_RESULT_UNFIXED) {
1019753ac610SCharles Manning 		yaffs_trace(YAFFS_TRACE_SCAN,
1020753ac610SCharles Manning 			" Unfixed ECC in chunk(%d:%d), chunk ignored",
1021753ac610SCharles Manning 			blk, chunk_in_block);
1022753ac610SCharles Manning 			dev->n_free_chunks++;
1023753ac610SCharles Manning 	} else if (tags.obj_id > YAFFS_MAX_OBJECT_ID ||
1024753ac610SCharles Manning 		   tags.chunk_id > YAFFS_MAX_CHUNK_ID ||
1025753ac610SCharles Manning 		   tags.obj_id == YAFFS_OBJECTID_SUMMARY ||
1026753ac610SCharles Manning 		   (tags.chunk_id > 0 &&
1027753ac610SCharles Manning 		     tags.n_bytes > dev->data_bytes_per_chunk) ||
1028753ac610SCharles Manning 		   tags.seq_number != bi->seq_number) {
1029753ac610SCharles Manning 		yaffs_trace(YAFFS_TRACE_SCAN,
1030753ac610SCharles Manning 			"Chunk (%d:%d) with bad tags:obj = %d, chunk_id = %d, n_bytes = %d, ignored",
1031753ac610SCharles Manning 			blk, chunk_in_block, tags.obj_id,
1032753ac610SCharles Manning 			tags.chunk_id, tags.n_bytes);
1033753ac610SCharles Manning 		dev->n_free_chunks++;
1034753ac610SCharles Manning 	} else if (tags.chunk_id > 0) {
1035753ac610SCharles Manning 		/* chunk_id > 0 so it is a data chunk... */
1036753ac610SCharles Manning 		loff_t endpos;
1037753ac610SCharles Manning 		loff_t chunk_base = (tags.chunk_id - 1) *
1038753ac610SCharles Manning 					dev->data_bytes_per_chunk;
1039753ac610SCharles Manning 
1040753ac610SCharles Manning 		*found_chunks = 1;
1041753ac610SCharles Manning 
1042753ac610SCharles Manning 		yaffs_set_chunk_bit(dev, blk, chunk_in_block);
1043753ac610SCharles Manning 		bi->pages_in_use++;
1044753ac610SCharles Manning 
1045753ac610SCharles Manning 		in = yaffs_find_or_create_by_number(dev,
1046753ac610SCharles Manning 					tags.obj_id,
1047753ac610SCharles Manning 					YAFFS_OBJECT_TYPE_FILE);
1048753ac610SCharles Manning 		if (!in)
1049753ac610SCharles Manning 			/* Out of memory */
1050753ac610SCharles Manning 			alloc_failed = 1;
1051753ac610SCharles Manning 
1052753ac610SCharles Manning 		if (in &&
1053753ac610SCharles Manning 		    in->variant_type == YAFFS_OBJECT_TYPE_FILE &&
1054753ac610SCharles Manning 		    chunk_base < in->variant.file_variant.shrink_size) {
1055753ac610SCharles Manning 			/* This has not been invalidated by
1056753ac610SCharles Manning 			 * a resize */
1057753ac610SCharles Manning 			if (!yaffs_put_chunk_in_file(in, tags.chunk_id,
1058753ac610SCharles Manning 								chunk, -1))
1059753ac610SCharles Manning 				alloc_failed = 1;
1060753ac610SCharles Manning 
1061753ac610SCharles Manning 			/* File size is calculated by looking at
1062753ac610SCharles Manning 			 * the data chunks if we have not
1063753ac610SCharles Manning 			 * seen an object header yet.
1064753ac610SCharles Manning 			 * Stop this practice once we find an
1065753ac610SCharles Manning 			 * object header.
1066753ac610SCharles Manning 			 */
1067753ac610SCharles Manning 			endpos = chunk_base + tags.n_bytes;
1068753ac610SCharles Manning 
1069753ac610SCharles Manning 			if (!in->valid &&
1070753ac610SCharles Manning 			    in->variant.file_variant.scanned_size < endpos) {
1071753ac610SCharles Manning 				in->variant.file_variant.
1072753ac610SCharles Manning 				    scanned_size = endpos;
1073753ac610SCharles Manning 				in->variant.file_variant.
1074753ac610SCharles Manning 				    file_size = endpos;
1075753ac610SCharles Manning 			}
1076753ac610SCharles Manning 		} else if (in) {
1077753ac610SCharles Manning 			/* This chunk has been invalidated by a
1078753ac610SCharles Manning 			 * resize, or a past file deletion
1079753ac610SCharles Manning 			 * so delete the chunk*/
1080753ac610SCharles Manning 			yaffs_chunk_del(dev, chunk, 1, __LINE__);
1081753ac610SCharles Manning 		}
1082753ac610SCharles Manning 	} else {
1083753ac610SCharles Manning 		/* chunk_id == 0, so it is an ObjectHeader.
1084753ac610SCharles Manning 		 * Thus, we read in the object header and make
1085753ac610SCharles Manning 		 * the object
1086753ac610SCharles Manning 		 */
1087753ac610SCharles Manning 		*found_chunks = 1;
1088753ac610SCharles Manning 
1089753ac610SCharles Manning 		yaffs_set_chunk_bit(dev, blk, chunk_in_block);
1090753ac610SCharles Manning 		bi->pages_in_use++;
1091753ac610SCharles Manning 
1092753ac610SCharles Manning 		oh = NULL;
1093753ac610SCharles Manning 		in = NULL;
1094753ac610SCharles Manning 
1095753ac610SCharles Manning 		if (tags.extra_available) {
1096753ac610SCharles Manning 			in = yaffs_find_or_create_by_number(dev,
1097753ac610SCharles Manning 					tags.obj_id,
1098753ac610SCharles Manning 					tags.extra_obj_type);
1099753ac610SCharles Manning 			if (!in)
1100753ac610SCharles Manning 				alloc_failed = 1;
1101753ac610SCharles Manning 		}
1102753ac610SCharles Manning 
1103753ac610SCharles Manning 		if (!in ||
1104753ac610SCharles Manning 		    (!in->valid && dev->param.disable_lazy_load) ||
1105753ac610SCharles Manning 		    tags.extra_shadows ||
1106753ac610SCharles Manning 		    (!in->valid && (tags.obj_id == YAFFS_OBJECTID_ROOT ||
1107753ac610SCharles Manning 				 tags.obj_id == YAFFS_OBJECTID_LOSTNFOUND))) {
1108753ac610SCharles Manning 
1109753ac610SCharles Manning 			/* If we don't have  valid info then we
1110753ac610SCharles Manning 			 * need to read the chunk
1111753ac610SCharles Manning 			 * TODO In future we can probably defer
1112753ac610SCharles Manning 			 * reading the chunk and living with
1113753ac610SCharles Manning 			 * invalid data until needed.
1114753ac610SCharles Manning 			 */
1115753ac610SCharles Manning 
1116*8cc64bafSAnatolij Gustschin 			yaffs_rd_chunk_tags_nand(dev, chunk, chunk_data, NULL);
1117753ac610SCharles Manning 
1118753ac610SCharles Manning 			oh = (struct yaffs_obj_hdr *)chunk_data;
1119753ac610SCharles Manning 
1120753ac610SCharles Manning 			if (dev->param.inband_tags) {
1121753ac610SCharles Manning 				/* Fix up the header if they got
1122753ac610SCharles Manning 				 * corrupted by inband tags */
1123753ac610SCharles Manning 				oh->shadows_obj =
1124753ac610SCharles Manning 				    oh->inband_shadowed_obj_id;
1125753ac610SCharles Manning 				oh->is_shrink =
1126753ac610SCharles Manning 				    oh->inband_is_shrink;
1127753ac610SCharles Manning 			}
1128753ac610SCharles Manning 
1129753ac610SCharles Manning 			if (!in) {
1130753ac610SCharles Manning 				in = yaffs_find_or_create_by_number(dev,
1131753ac610SCharles Manning 							tags.obj_id, oh->type);
1132753ac610SCharles Manning 				if (!in)
1133753ac610SCharles Manning 					alloc_failed = 1;
1134753ac610SCharles Manning 			}
1135753ac610SCharles Manning 		}
1136753ac610SCharles Manning 
1137753ac610SCharles Manning 		if (!in) {
1138753ac610SCharles Manning 			/* TODO Hoosterman we have a problem! */
1139753ac610SCharles Manning 			yaffs_trace(YAFFS_TRACE_ERROR,
1140753ac610SCharles Manning 				"yaffs tragedy: Could not make object for object  %d at chunk %d during scan",
1141753ac610SCharles Manning 				tags.obj_id, chunk);
1142753ac610SCharles Manning 			return YAFFS_FAIL;
1143753ac610SCharles Manning 		}
1144753ac610SCharles Manning 
1145753ac610SCharles Manning 		if (in->valid) {
1146753ac610SCharles Manning 			/* We have already filled this one.
1147753ac610SCharles Manning 			 * We have a duplicate that will be
1148753ac610SCharles Manning 			 * discarded, but we first have to suck
1149753ac610SCharles Manning 			 * out resize info if it is a file.
1150753ac610SCharles Manning 			 */
1151753ac610SCharles Manning 			if ((in->variant_type == YAFFS_OBJECT_TYPE_FILE) &&
1152753ac610SCharles Manning 				((oh && oh->type == YAFFS_OBJECT_TYPE_FILE) ||
1153753ac610SCharles Manning 				 (tags.extra_available &&
1154753ac610SCharles Manning 				  tags.extra_obj_type == YAFFS_OBJECT_TYPE_FILE)
1155753ac610SCharles Manning 				)) {
1156753ac610SCharles Manning 				loff_t this_size = (oh) ?
1157753ac610SCharles Manning 					yaffs_oh_to_size(oh) :
1158753ac610SCharles Manning 					tags.extra_file_size;
1159753ac610SCharles Manning 				u32 parent_obj_id = (oh) ?
1160753ac610SCharles Manning 					oh->parent_obj_id :
1161753ac610SCharles Manning 					tags.extra_parent_id;
1162753ac610SCharles Manning 
1163753ac610SCharles Manning 				is_shrink = (oh) ?
1164753ac610SCharles Manning 					oh->is_shrink :
1165753ac610SCharles Manning 					tags.extra_is_shrink;
1166753ac610SCharles Manning 
1167753ac610SCharles Manning 				/* If it is deleted (unlinked
1168753ac610SCharles Manning 				 * at start also means deleted)
1169753ac610SCharles Manning 				 * we treat the file size as
1170753ac610SCharles Manning 				 * being zeroed at this point.
1171753ac610SCharles Manning 				 */
1172753ac610SCharles Manning 				if (parent_obj_id == YAFFS_OBJECTID_DELETED ||
1173753ac610SCharles Manning 				    parent_obj_id == YAFFS_OBJECTID_UNLINKED) {
1174753ac610SCharles Manning 					this_size = 0;
1175753ac610SCharles Manning 					is_shrink = 1;
1176753ac610SCharles Manning 				}
1177753ac610SCharles Manning 
1178753ac610SCharles Manning 				if (is_shrink &&
1179753ac610SCharles Manning 				    in->variant.file_variant.shrink_size >
1180753ac610SCharles Manning 				    this_size)
1181753ac610SCharles Manning 					in->variant.file_variant.shrink_size =
1182753ac610SCharles Manning 					this_size;
1183753ac610SCharles Manning 
1184753ac610SCharles Manning 				if (is_shrink)
1185753ac610SCharles Manning 					bi->has_shrink_hdr = 1;
1186753ac610SCharles Manning 			}
1187753ac610SCharles Manning 			/* Use existing - destroy this one. */
1188753ac610SCharles Manning 			yaffs_chunk_del(dev, chunk, 1, __LINE__);
1189753ac610SCharles Manning 		}
1190753ac610SCharles Manning 
1191753ac610SCharles Manning 		if (!in->valid && in->variant_type !=
1192753ac610SCharles Manning 		    (oh ? oh->type : tags.extra_obj_type))
1193753ac610SCharles Manning 			yaffs_trace(YAFFS_TRACE_ERROR,
1194753ac610SCharles Manning 				"yaffs tragedy: Bad object type, %d != %d, for object %d at chunk %d during scan",
1195753ac610SCharles Manning 				oh ? oh->type : tags.extra_obj_type,
1196753ac610SCharles Manning 				in->variant_type, tags.obj_id,
1197753ac610SCharles Manning 				chunk);
1198753ac610SCharles Manning 
1199753ac610SCharles Manning 		if (!in->valid &&
1200753ac610SCharles Manning 		    (tags.obj_id == YAFFS_OBJECTID_ROOT ||
1201753ac610SCharles Manning 		     tags.obj_id == YAFFS_OBJECTID_LOSTNFOUND)) {
1202753ac610SCharles Manning 			/* We only load some info, don't fiddle
1203753ac610SCharles Manning 			 * with directory structure */
1204753ac610SCharles Manning 			in->valid = 1;
1205753ac610SCharles Manning 
1206753ac610SCharles Manning 			if (oh) {
1207753ac610SCharles Manning 				in->yst_mode = oh->yst_mode;
1208753ac610SCharles Manning 				yaffs_load_attribs(in, oh);
1209753ac610SCharles Manning 				in->lazy_loaded = 0;
1210753ac610SCharles Manning 			} else {
1211753ac610SCharles Manning 				in->lazy_loaded = 1;
1212753ac610SCharles Manning 			}
1213753ac610SCharles Manning 			in->hdr_chunk = chunk;
1214753ac610SCharles Manning 
1215753ac610SCharles Manning 		} else if (!in->valid) {
1216753ac610SCharles Manning 			/* we need to load this info */
1217753ac610SCharles Manning 			in->valid = 1;
1218753ac610SCharles Manning 			in->hdr_chunk = chunk;
1219753ac610SCharles Manning 			if (oh) {
1220753ac610SCharles Manning 				in->variant_type = oh->type;
1221753ac610SCharles Manning 				in->yst_mode = oh->yst_mode;
1222753ac610SCharles Manning 				yaffs_load_attribs(in, oh);
1223753ac610SCharles Manning 
1224753ac610SCharles Manning 				if (oh->shadows_obj > 0)
1225753ac610SCharles Manning 					yaffs_handle_shadowed_obj(dev,
1226753ac610SCharles Manning 					     oh->shadows_obj, 1);
1227753ac610SCharles Manning 
1228753ac610SCharles Manning 				yaffs_set_obj_name_from_oh(in, oh);
1229753ac610SCharles Manning 				parent = yaffs_find_or_create_by_number(dev,
1230753ac610SCharles Manning 						oh->parent_obj_id,
1231753ac610SCharles Manning 						YAFFS_OBJECT_TYPE_DIRECTORY);
1232753ac610SCharles Manning 				file_size = yaffs_oh_to_size(oh);
1233753ac610SCharles Manning 				is_shrink = oh->is_shrink;
1234753ac610SCharles Manning 				equiv_id = oh->equiv_id;
1235753ac610SCharles Manning 			} else {
1236753ac610SCharles Manning 				in->variant_type = tags.extra_obj_type;
1237753ac610SCharles Manning 				parent = yaffs_find_or_create_by_number(dev,
1238753ac610SCharles Manning 						tags.extra_parent_id,
1239753ac610SCharles Manning 						YAFFS_OBJECT_TYPE_DIRECTORY);
1240753ac610SCharles Manning 				file_size = tags.extra_file_size;
1241753ac610SCharles Manning 				is_shrink = tags.extra_is_shrink;
1242753ac610SCharles Manning 				equiv_id = tags.extra_equiv_id;
1243753ac610SCharles Manning 				in->lazy_loaded = 1;
1244753ac610SCharles Manning 			}
1245753ac610SCharles Manning 			in->dirty = 0;
1246753ac610SCharles Manning 
1247753ac610SCharles Manning 			if (!parent)
1248753ac610SCharles Manning 				alloc_failed = 1;
1249753ac610SCharles Manning 
1250753ac610SCharles Manning 			/* directory stuff...
1251753ac610SCharles Manning 			 * hook up to parent
1252753ac610SCharles Manning 			 */
1253753ac610SCharles Manning 
1254753ac610SCharles Manning 			if (parent &&
1255753ac610SCharles Manning 			    parent->variant_type == YAFFS_OBJECT_TYPE_UNKNOWN) {
1256753ac610SCharles Manning 				/* Set up as a directory */
1257753ac610SCharles Manning 				parent->variant_type =
1258753ac610SCharles Manning 					YAFFS_OBJECT_TYPE_DIRECTORY;
1259753ac610SCharles Manning 				INIT_LIST_HEAD(&parent->
1260753ac610SCharles Manning 						variant.dir_variant.children);
1261753ac610SCharles Manning 			} else if (!parent ||
1262753ac610SCharles Manning 				   parent->variant_type !=
1263753ac610SCharles Manning 					YAFFS_OBJECT_TYPE_DIRECTORY) {
1264753ac610SCharles Manning 				/* Hoosterman, another problem....
1265753ac610SCharles Manning 				 * Trying to use a non-directory as a directory
1266753ac610SCharles Manning 				 */
1267753ac610SCharles Manning 
1268753ac610SCharles Manning 				yaffs_trace(YAFFS_TRACE_ERROR,
1269753ac610SCharles Manning 					"yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
1270753ac610SCharles Manning 					);
1271753ac610SCharles Manning 				parent = dev->lost_n_found;
1272753ac610SCharles Manning 			}
1273753ac610SCharles Manning 			yaffs_add_obj_to_dir(parent, in);
1274753ac610SCharles Manning 
1275753ac610SCharles Manning 			is_unlinked = (parent == dev->del_dir) ||
1276753ac610SCharles Manning 					(parent == dev->unlinked_dir);
1277753ac610SCharles Manning 
1278753ac610SCharles Manning 			if (is_shrink)
1279753ac610SCharles Manning 				/* Mark the block */
1280753ac610SCharles Manning 				bi->has_shrink_hdr = 1;
1281753ac610SCharles Manning 
1282753ac610SCharles Manning 			/* Note re hardlinks.
1283753ac610SCharles Manning 			 * Since we might scan a hardlink before its equivalent
1284753ac610SCharles Manning 			 * object is scanned we put them all in a list.
1285753ac610SCharles Manning 			 * After scanning is complete, we should have all the
1286753ac610SCharles Manning 			 * objects, so we run through this list and fix up all
1287753ac610SCharles Manning 			 * the chains.
1288753ac610SCharles Manning 			 */
1289753ac610SCharles Manning 
1290753ac610SCharles Manning 			switch (in->variant_type) {
1291753ac610SCharles Manning 			case YAFFS_OBJECT_TYPE_UNKNOWN:
1292753ac610SCharles Manning 				/* Todo got a problem */
1293753ac610SCharles Manning 				break;
1294753ac610SCharles Manning 			case YAFFS_OBJECT_TYPE_FILE:
1295753ac610SCharles Manning 				file_var = &in->variant.file_variant;
1296753ac610SCharles Manning 				if (file_var->scanned_size < file_size) {
1297753ac610SCharles Manning 					/* This covers the case where the file
1298753ac610SCharles Manning 					 * size is greater than the data held.
1299753ac610SCharles Manning 					 * This will happen if the file is
1300753ac610SCharles Manning 					 * resized to be larger than its
1301753ac610SCharles Manning 					 * current data extents.
1302753ac610SCharles Manning 					 */
1303753ac610SCharles Manning 					file_var->file_size = file_size;
1304753ac610SCharles Manning 					file_var->scanned_size = file_size;
1305753ac610SCharles Manning 				}
1306753ac610SCharles Manning 
1307753ac610SCharles Manning 				if (file_var->shrink_size > file_size)
1308753ac610SCharles Manning 					file_var->shrink_size = file_size;
1309753ac610SCharles Manning 
1310753ac610SCharles Manning 				break;
1311753ac610SCharles Manning 			case YAFFS_OBJECT_TYPE_HARDLINK:
1312753ac610SCharles Manning 				hl_var = &in->variant.hardlink_variant;
1313753ac610SCharles Manning 				if (!is_unlinked) {
1314753ac610SCharles Manning 					hl_var->equiv_id = equiv_id;
1315753ac610SCharles Manning 					list_add(&in->hard_links, hard_list);
1316753ac610SCharles Manning 				}
1317753ac610SCharles Manning 				break;
1318753ac610SCharles Manning 			case YAFFS_OBJECT_TYPE_DIRECTORY:
1319753ac610SCharles Manning 				/* Do nothing */
1320753ac610SCharles Manning 				break;
1321753ac610SCharles Manning 			case YAFFS_OBJECT_TYPE_SPECIAL:
1322753ac610SCharles Manning 				/* Do nothing */
1323753ac610SCharles Manning 				break;
1324753ac610SCharles Manning 			case YAFFS_OBJECT_TYPE_SYMLINK:
1325753ac610SCharles Manning 				sl_var = &in->variant.symlink_variant;
1326753ac610SCharles Manning 				if (oh) {
1327753ac610SCharles Manning 					sl_var->alias =
1328753ac610SCharles Manning 					    yaffs_clone_str(oh->alias);
1329753ac610SCharles Manning 					if (!sl_var->alias)
1330753ac610SCharles Manning 						alloc_failed = 1;
1331753ac610SCharles Manning 				}
1332753ac610SCharles Manning 				break;
1333753ac610SCharles Manning 			}
1334753ac610SCharles Manning 		}
1335753ac610SCharles Manning 	}
1336753ac610SCharles Manning 	return alloc_failed ? YAFFS_FAIL : YAFFS_OK;
1337753ac610SCharles Manning }
1338753ac610SCharles Manning 
yaffs2_scan_backwards(struct yaffs_dev * dev)1339753ac610SCharles Manning int yaffs2_scan_backwards(struct yaffs_dev *dev)
1340753ac610SCharles Manning {
1341753ac610SCharles Manning 	int blk;
1342753ac610SCharles Manning 	int block_iter;
1343753ac610SCharles Manning 	int start_iter;
1344753ac610SCharles Manning 	int end_iter;
1345753ac610SCharles Manning 	int n_to_scan = 0;
1346753ac610SCharles Manning 	enum yaffs_block_state state;
1347753ac610SCharles Manning 	int c;
1348753ac610SCharles Manning 	LIST_HEAD(hard_list);
1349753ac610SCharles Manning 	struct yaffs_block_info *bi;
1350753ac610SCharles Manning 	u32 seq_number;
1351753ac610SCharles Manning 	int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
1352753ac610SCharles Manning 	u8 *chunk_data;
1353753ac610SCharles Manning 	int found_chunks;
1354753ac610SCharles Manning 	int alloc_failed = 0;
1355753ac610SCharles Manning 	struct yaffs_block_index *block_index = NULL;
1356753ac610SCharles Manning 	int alt_block_index = 0;
1357753ac610SCharles Manning 	int summary_available;
1358753ac610SCharles Manning 
1359753ac610SCharles Manning 	yaffs_trace(YAFFS_TRACE_SCAN,
1360753ac610SCharles Manning 		"yaffs2_scan_backwards starts  intstartblk %d intendblk %d...",
1361753ac610SCharles Manning 		dev->internal_start_block, dev->internal_end_block);
1362753ac610SCharles Manning 
1363753ac610SCharles Manning 	dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
1364753ac610SCharles Manning 
1365753ac610SCharles Manning 	block_index =
1366753ac610SCharles Manning 		kmalloc(n_blocks * sizeof(struct yaffs_block_index), GFP_NOFS);
1367753ac610SCharles Manning 
1368753ac610SCharles Manning 	if (!block_index) {
1369753ac610SCharles Manning 		block_index =
1370753ac610SCharles Manning 		    vmalloc(n_blocks * sizeof(struct yaffs_block_index));
1371753ac610SCharles Manning 		alt_block_index = 1;
1372753ac610SCharles Manning 	}
1373753ac610SCharles Manning 
1374753ac610SCharles Manning 	if (!block_index) {
1375753ac610SCharles Manning 		yaffs_trace(YAFFS_TRACE_SCAN,
1376753ac610SCharles Manning 			"yaffs2_scan_backwards() could not allocate block index!"
1377753ac610SCharles Manning 			);
1378753ac610SCharles Manning 		return YAFFS_FAIL;
1379753ac610SCharles Manning 	}
1380753ac610SCharles Manning 
1381753ac610SCharles Manning 	dev->blocks_in_checkpt = 0;
1382753ac610SCharles Manning 
1383753ac610SCharles Manning 	chunk_data = yaffs_get_temp_buffer(dev);
1384753ac610SCharles Manning 
1385753ac610SCharles Manning 	/* Scan all the blocks to determine their state */
1386753ac610SCharles Manning 	bi = dev->block_info;
1387753ac610SCharles Manning 	for (blk = dev->internal_start_block; blk <= dev->internal_end_block;
1388753ac610SCharles Manning 	     blk++) {
1389753ac610SCharles Manning 		yaffs_clear_chunk_bits(dev, blk);
1390753ac610SCharles Manning 		bi->pages_in_use = 0;
1391753ac610SCharles Manning 		bi->soft_del_pages = 0;
1392753ac610SCharles Manning 
1393753ac610SCharles Manning 		yaffs_query_init_block_state(dev, blk, &state, &seq_number);
1394753ac610SCharles Manning 
1395753ac610SCharles Manning 		bi->block_state = state;
1396753ac610SCharles Manning 		bi->seq_number = seq_number;
1397753ac610SCharles Manning 
1398753ac610SCharles Manning 		if (bi->seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA)
1399753ac610SCharles Manning 			bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
1400753ac610SCharles Manning 		if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
1401753ac610SCharles Manning 			bi->block_state = YAFFS_BLOCK_STATE_DEAD;
1402753ac610SCharles Manning 
1403753ac610SCharles Manning 		yaffs_trace(YAFFS_TRACE_SCAN_DEBUG,
1404753ac610SCharles Manning 			"Block scanning block %d state %d seq %d",
1405753ac610SCharles Manning 			blk, bi->block_state, seq_number);
1406753ac610SCharles Manning 
1407753ac610SCharles Manning 		if (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT) {
1408753ac610SCharles Manning 			dev->blocks_in_checkpt++;
1409753ac610SCharles Manning 
1410753ac610SCharles Manning 		} else if (bi->block_state == YAFFS_BLOCK_STATE_DEAD) {
1411753ac610SCharles Manning 			yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
1412753ac610SCharles Manning 				"block %d is bad", blk);
1413753ac610SCharles Manning 		} else if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
1414753ac610SCharles Manning 			yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "Block empty ");
1415753ac610SCharles Manning 			dev->n_erased_blocks++;
1416753ac610SCharles Manning 			dev->n_free_chunks += dev->param.chunks_per_block;
1417753ac610SCharles Manning 		} else if (bi->block_state ==
1418753ac610SCharles Manning 				YAFFS_BLOCK_STATE_NEEDS_SCAN) {
1419753ac610SCharles Manning 			/* Determine the highest sequence number */
1420753ac610SCharles Manning 			if (seq_number >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
1421753ac610SCharles Manning 			    seq_number < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
1422753ac610SCharles Manning 				block_index[n_to_scan].seq = seq_number;
1423753ac610SCharles Manning 				block_index[n_to_scan].block = blk;
1424753ac610SCharles Manning 				n_to_scan++;
1425753ac610SCharles Manning 				if (seq_number >= dev->seq_number)
1426753ac610SCharles Manning 					dev->seq_number = seq_number;
1427753ac610SCharles Manning 			} else {
1428753ac610SCharles Manning 				/* TODO: Nasty sequence number! */
1429753ac610SCharles Manning 				yaffs_trace(YAFFS_TRACE_SCAN,
1430753ac610SCharles Manning 					"Block scanning block %d has bad sequence number %d",
1431753ac610SCharles Manning 					blk, seq_number);
1432753ac610SCharles Manning 			}
1433753ac610SCharles Manning 		}
1434753ac610SCharles Manning 		bi++;
1435753ac610SCharles Manning 	}
1436753ac610SCharles Manning 
1437753ac610SCharles Manning 	yaffs_trace(YAFFS_TRACE_SCAN, "%d blocks to be sorted...", n_to_scan);
1438753ac610SCharles Manning 
1439753ac610SCharles Manning 	cond_resched();
1440753ac610SCharles Manning 
1441753ac610SCharles Manning 	/* Sort the blocks by sequence number */
1442753ac610SCharles Manning 	sort(block_index, n_to_scan, sizeof(struct yaffs_block_index),
1443753ac610SCharles Manning 		   yaffs2_ybicmp, NULL);
1444753ac610SCharles Manning 
1445753ac610SCharles Manning 	cond_resched();
1446753ac610SCharles Manning 
1447753ac610SCharles Manning 	yaffs_trace(YAFFS_TRACE_SCAN, "...done");
1448753ac610SCharles Manning 
1449753ac610SCharles Manning 	/* Now scan the blocks looking at the data. */
1450753ac610SCharles Manning 	start_iter = 0;
1451753ac610SCharles Manning 	end_iter = n_to_scan - 1;
1452753ac610SCharles Manning 	yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "%d blocks to scan", n_to_scan);
1453753ac610SCharles Manning 
1454753ac610SCharles Manning 	/* For each block.... backwards */
1455753ac610SCharles Manning 	for (block_iter = end_iter;
1456753ac610SCharles Manning 	     !alloc_failed && block_iter >= start_iter;
1457753ac610SCharles Manning 	     block_iter--) {
1458753ac610SCharles Manning 		/* Cooperative multitasking! This loop can run for so
1459753ac610SCharles Manning 		   long that watchdog timers expire. */
1460753ac610SCharles Manning 		cond_resched();
1461753ac610SCharles Manning 
1462753ac610SCharles Manning 		/* get the block to scan in the correct order */
1463753ac610SCharles Manning 		blk = block_index[block_iter].block;
1464753ac610SCharles Manning 		bi = yaffs_get_block_info(dev, blk);
1465753ac610SCharles Manning 
1466753ac610SCharles Manning 		summary_available = yaffs_summary_read(dev, dev->sum_tags, blk);
1467753ac610SCharles Manning 
1468753ac610SCharles Manning 		/* For each chunk in each block that needs scanning.... */
1469753ac610SCharles Manning 		found_chunks = 0;
1470753ac610SCharles Manning 		if (summary_available)
1471753ac610SCharles Manning 			c = dev->chunks_per_summary - 1;
1472753ac610SCharles Manning 		else
1473753ac610SCharles Manning 			c = dev->param.chunks_per_block - 1;
1474753ac610SCharles Manning 
1475753ac610SCharles Manning 		for (/* c is already initialised */;
1476753ac610SCharles Manning 		     !alloc_failed && c >= 0 &&
1477753ac610SCharles Manning 		     (bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN ||
1478753ac610SCharles Manning 		      bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING);
1479753ac610SCharles Manning 		      c--) {
1480753ac610SCharles Manning 			/* Scan backwards...
1481753ac610SCharles Manning 			 * Read the tags and decide what to do
1482753ac610SCharles Manning 			 */
1483753ac610SCharles Manning 			if (yaffs2_scan_chunk(dev, bi, blk, c,
1484753ac610SCharles Manning 					&found_chunks, chunk_data,
1485753ac610SCharles Manning 					&hard_list, summary_available) ==
1486753ac610SCharles Manning 					YAFFS_FAIL)
1487753ac610SCharles Manning 				alloc_failed = 1;
1488753ac610SCharles Manning 		}
1489753ac610SCharles Manning 
1490753ac610SCharles Manning 		if (bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN) {
1491753ac610SCharles Manning 			/* If we got this far while scanning, then the block
1492753ac610SCharles Manning 			 * is fully allocated. */
1493753ac610SCharles Manning 			bi->block_state = YAFFS_BLOCK_STATE_FULL;
1494753ac610SCharles Manning 		}
1495753ac610SCharles Manning 
1496753ac610SCharles Manning 		/* Now let's see if it was dirty */
1497753ac610SCharles Manning 		if (bi->pages_in_use == 0 &&
1498753ac610SCharles Manning 		    !bi->has_shrink_hdr &&
1499753ac610SCharles Manning 		    bi->block_state == YAFFS_BLOCK_STATE_FULL) {
1500753ac610SCharles Manning 			yaffs_block_became_dirty(dev, blk);
1501753ac610SCharles Manning 		}
1502753ac610SCharles Manning 	}
1503753ac610SCharles Manning 
1504753ac610SCharles Manning 	yaffs_skip_rest_of_block(dev);
1505753ac610SCharles Manning 
1506753ac610SCharles Manning 	if (alt_block_index)
1507753ac610SCharles Manning 		vfree(block_index);
1508753ac610SCharles Manning 	else
1509753ac610SCharles Manning 		kfree(block_index);
1510753ac610SCharles Manning 
1511753ac610SCharles Manning 	/* Ok, we've done all the scanning.
1512753ac610SCharles Manning 	 * Fix up the hard link chains.
1513753ac610SCharles Manning 	 * We have scanned all the objects, now it's time to add these
1514753ac610SCharles Manning 	 * hardlinks.
1515753ac610SCharles Manning 	 */
1516753ac610SCharles Manning 	yaffs_link_fixup(dev, &hard_list);
1517753ac610SCharles Manning 
1518753ac610SCharles Manning 	yaffs_release_temp_buffer(dev, chunk_data);
1519753ac610SCharles Manning 
1520753ac610SCharles Manning 	if (alloc_failed)
1521753ac610SCharles Manning 		return YAFFS_FAIL;
1522753ac610SCharles Manning 
1523753ac610SCharles Manning 	yaffs_trace(YAFFS_TRACE_SCAN, "yaffs2_scan_backwards ends");
1524753ac610SCharles Manning 
1525753ac610SCharles Manning 	return YAFFS_OK;
1526753ac610SCharles Manning }
1527