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_yaffs1.h"
15#include "yportenv.h"
16#include "yaffs_trace.h"
17#include "yaffs_bitmap.h"
18#include "yaffs_getblockinfo.h"
19#include "yaffs_nand.h"
20#include "yaffs_attribs.h"
21
22int yaffs1_scan(struct yaffs_dev *dev)
23{
24	struct yaffs_ext_tags tags;
25	int blk;
26	int result;
27	int chunk;
28	int c;
29	int deleted;
30	enum yaffs_block_state state;
31	LIST_HEAD(hard_list);
32	struct yaffs_block_info *bi;
33	u32 seq_number;
34	struct yaffs_obj_hdr *oh;
35	struct yaffs_obj *in;
36	struct yaffs_obj *parent;
37	int alloc_failed = 0;
38	struct yaffs_shadow_fixer *shadow_fixers = NULL;
39	u8 *chunk_data;
40
41	yaffs_trace(YAFFS_TRACE_SCAN,
42		"yaffs1_scan starts  intstartblk %d intendblk %d...",
43		dev->internal_start_block, dev->internal_end_block);
44
45	chunk_data = yaffs_get_temp_buffer(dev);
46
47	dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
48
49	/* Scan all the blocks to determine their state */
50	bi = dev->block_info;
51	for (blk = dev->internal_start_block; blk <= dev->internal_end_block;
52	     blk++) {
53		yaffs_clear_chunk_bits(dev, blk);
54		bi->pages_in_use = 0;
55		bi->soft_del_pages = 0;
56
57		yaffs_query_init_block_state(dev, blk, &state, &seq_number);
58
59		bi->block_state = state;
60		bi->seq_number = seq_number;
61
62		if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
63			bi->block_state = state = YAFFS_BLOCK_STATE_DEAD;
64
65		yaffs_trace(YAFFS_TRACE_SCAN_DEBUG,
66			"Block scanning block %d state %d seq %d",
67			blk, state, seq_number);
68
69		if (state == YAFFS_BLOCK_STATE_DEAD) {
70			yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
71				"block %d is bad", blk);
72		} else if (state == YAFFS_BLOCK_STATE_EMPTY) {
73			yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "Block empty ");
74			dev->n_erased_blocks++;
75			dev->n_free_chunks += dev->param.chunks_per_block;
76		}
77		bi++;
78	}
79
80	/* For each block.... */
81	for (blk = dev->internal_start_block;
82	     !alloc_failed && blk <= dev->internal_end_block; blk++) {
83
84		cond_resched();
85
86		bi = yaffs_get_block_info(dev, blk);
87		state = bi->block_state;
88
89		deleted = 0;
90
91		/* For each chunk in each block that needs scanning.... */
92		for (c = 0;
93			!alloc_failed && c < dev->param.chunks_per_block &&
94			state == YAFFS_BLOCK_STATE_NEEDS_SCAN; c++) {
95			/* Read the tags and decide what to do */
96			chunk = blk * dev->param.chunks_per_block + c;
97
98			result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL,
99							  &tags);
100
101			/* Let's have a good look at this chunk... */
102
103			if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED ||
104			    tags.is_deleted) {
105				/* YAFFS1 only...
106				 * A deleted chunk
107				 */
108				deleted++;
109				dev->n_free_chunks++;
110			} else if (!tags.chunk_used) {
111				/* An unassigned chunk in the block
112				 * This means that either the block is empty or
113				 * this is the one being allocated from
114				 */
115
116				if (c == 0) {
117					/* We're looking at the first chunk in
118					 *the block so the block is unused */
119					state = YAFFS_BLOCK_STATE_EMPTY;
120					dev->n_erased_blocks++;
121				} else {
122					/* this is the block being allocated */
123					yaffs_trace(YAFFS_TRACE_SCAN,
124						" Allocating from %d %d",
125						blk, c);
126					state = YAFFS_BLOCK_STATE_ALLOCATING;
127					dev->alloc_block = blk;
128					dev->alloc_page = c;
129					dev->alloc_block_finder = blk;
130
131				}
132
133				dev->n_free_chunks +=
134				    (dev->param.chunks_per_block - c);
135			} else if (tags.chunk_id > 0) {
136				/* chunk_id > 0 so it is a data chunk... */
137				unsigned int endpos;
138
139				yaffs_set_chunk_bit(dev, blk, c);
140				bi->pages_in_use++;
141
142				in = yaffs_find_or_create_by_number(dev,
143							tags.obj_id,
144							YAFFS_OBJECT_TYPE_FILE);
145				/* PutChunkIntoFile checks for a clash
146				 * (two data chunks with the same chunk_id).
147				 */
148
149				if (!in)
150					alloc_failed = 1;
151
152				if (in) {
153					if (!yaffs_put_chunk_in_file
154					    (in, tags.chunk_id, chunk, 1))
155						alloc_failed = 1;
156				}
157
158				endpos =
159				    (tags.chunk_id - 1) *
160				    dev->data_bytes_per_chunk +
161				    tags.n_bytes;
162				if (in &&
163				    in->variant_type ==
164				     YAFFS_OBJECT_TYPE_FILE &&
165				    in->variant.file_variant.scanned_size <
166				      endpos) {
167					in->variant.file_variant.scanned_size =
168					    endpos;
169					if (!dev->param.use_header_file_size) {
170						in->variant.
171						    file_variant.file_size =
172						    in->variant.
173						    file_variant.scanned_size;
174					}
175
176				}
177			} else {
178				/* chunk_id == 0, so it is an ObjectHeader.
179				 * Make the object
180				 */
181				yaffs_set_chunk_bit(dev, blk, c);
182				bi->pages_in_use++;
183
184				result = yaffs_rd_chunk_tags_nand(dev, chunk,
185								  chunk_data,
186								  NULL);
187
188				oh = (struct yaffs_obj_hdr *)chunk_data;
189
190				in = yaffs_find_by_number(dev, tags.obj_id);
191				if (in && in->variant_type != oh->type) {
192					/* This should not happen, but somehow
193					 * Wev'e ended up with an obj_id that
194					 * has been reused but not yet deleted,
195					 * and worse still it has changed type.
196					 * Delete the old object.
197					 */
198
199					yaffs_del_obj(in);
200					in = NULL;
201				}
202
203				in = yaffs_find_or_create_by_number(dev,
204								tags.obj_id,
205								oh->type);
206
207				if (!in)
208					alloc_failed = 1;
209
210				if (in && oh->shadows_obj > 0) {
211
212					struct yaffs_shadow_fixer *fixer;
213					fixer =
214						kmalloc(sizeof
215						(struct yaffs_shadow_fixer),
216						GFP_NOFS);
217					if (fixer) {
218						fixer->next = shadow_fixers;
219						shadow_fixers = fixer;
220						fixer->obj_id = tags.obj_id;
221						fixer->shadowed_id =
222						    oh->shadows_obj;
223						yaffs_trace(YAFFS_TRACE_SCAN,
224							" Shadow fixer: %d shadows %d",
225							fixer->obj_id,
226							fixer->shadowed_id);
227
228					}
229
230				}
231
232				if (in && in->valid) {
233					/* We have already filled this one.
234					 * We have a duplicate and need to
235					 * resolve it. */
236
237					unsigned existing_serial = in->serial;
238					unsigned new_serial =
239					    tags.serial_number;
240
241					if (((existing_serial + 1) & 3) ==
242					    new_serial) {
243						/* Use new one - destroy the
244						 * exisiting one */
245						yaffs_chunk_del(dev,
246								in->hdr_chunk,
247								1, __LINE__);
248						in->valid = 0;
249					} else {
250						/* Use existing - destroy
251						 * this one. */
252						yaffs_chunk_del(dev, chunk, 1,
253								__LINE__);
254					}
255				}
256
257				if (in && !in->valid &&
258				    (tags.obj_id == YAFFS_OBJECTID_ROOT ||
259				     tags.obj_id ==
260				     YAFFS_OBJECTID_LOSTNFOUND)) {
261					/* We only load some info, don't fiddle
262					 * with directory structure */
263					in->valid = 1;
264					in->variant_type = oh->type;
265
266					in->yst_mode = oh->yst_mode;
267					yaffs_load_attribs(in, oh);
268					in->hdr_chunk = chunk;
269					in->serial = tags.serial_number;
270
271				} else if (in && !in->valid) {
272					/* we need to load this info */
273
274					in->valid = 1;
275					in->variant_type = oh->type;
276
277					in->yst_mode = oh->yst_mode;
278					yaffs_load_attribs(in, oh);
279					in->hdr_chunk = chunk;
280					in->serial = tags.serial_number;
281
282					yaffs_set_obj_name_from_oh(in, oh);
283					in->dirty = 0;
284
285					/* directory stuff...
286					 * hook up to parent
287					 */
288
289					parent =
290					    yaffs_find_or_create_by_number
291					    (dev, oh->parent_obj_id,
292					     YAFFS_OBJECT_TYPE_DIRECTORY);
293					if (!parent)
294						alloc_failed = 1;
295					if (parent && parent->variant_type ==
296					    YAFFS_OBJECT_TYPE_UNKNOWN) {
297						/* Set up as a directory */
298						parent->variant_type =
299						    YAFFS_OBJECT_TYPE_DIRECTORY;
300						INIT_LIST_HEAD(&parent->
301							variant.dir_variant.
302							children);
303					} else if (!parent ||
304						parent->variant_type !=
305						YAFFS_OBJECT_TYPE_DIRECTORY) {
306						/* Hoosterman, a problem....
307						 * We're trying to use a
308						 * non-directory as a directory
309						 */
310
311						yaffs_trace(YAFFS_TRACE_ERROR,
312							"yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
313							);
314						parent = dev->lost_n_found;
315					}
316
317					yaffs_add_obj_to_dir(parent, in);
318
319					switch (in->variant_type) {
320					case YAFFS_OBJECT_TYPE_UNKNOWN:
321						/* Todo got a problem */
322						break;
323					case YAFFS_OBJECT_TYPE_FILE:
324						if (dev->param.
325						    use_header_file_size)
326							in->variant.
327							file_variant.file_size
328							= yaffs_oh_to_size(oh);
329						break;
330					case YAFFS_OBJECT_TYPE_HARDLINK:
331						in->variant.
332						    hardlink_variant.equiv_id =
333						    oh->equiv_id;
334						list_add(&in->hard_links,
335								&hard_list);
336						break;
337					case YAFFS_OBJECT_TYPE_DIRECTORY:
338						/* Do nothing */
339						break;
340					case YAFFS_OBJECT_TYPE_SPECIAL:
341						/* Do nothing */
342						break;
343					case YAFFS_OBJECT_TYPE_SYMLINK:
344						in->variant.symlink_variant.
345						    alias =
346						    yaffs_clone_str(oh->alias);
347						if (!in->variant.
348						    symlink_variant.alias)
349							alloc_failed = 1;
350						break;
351					}
352				}
353			}
354		}
355
356		if (state == YAFFS_BLOCK_STATE_NEEDS_SCAN) {
357			/* If we got this far while scanning,
358			 * then the block is fully allocated. */
359			state = YAFFS_BLOCK_STATE_FULL;
360		}
361
362		if (state == YAFFS_BLOCK_STATE_ALLOCATING) {
363			/* If the block was partially allocated then
364			 * treat it as fully allocated. */
365			state = YAFFS_BLOCK_STATE_FULL;
366			dev->alloc_block = -1;
367		}
368
369		bi->block_state = state;
370
371		/* Now let's see if it was dirty */
372		if (bi->pages_in_use == 0 &&
373		    !bi->has_shrink_hdr &&
374		    bi->block_state == YAFFS_BLOCK_STATE_FULL)
375			yaffs_block_became_dirty(dev, blk);
376	}
377
378	/* Ok, we've done all the scanning.
379	 * Fix up the hard link chains.
380	 * We should now have scanned all the objects, now it's time to add
381	 * these hardlinks.
382	 */
383
384	yaffs_link_fixup(dev, &hard_list);
385
386	/*
387	 * Fix up any shadowed objects.
388	 * There should not be more than one of these.
389	 */
390	{
391		struct yaffs_shadow_fixer *fixer;
392		struct yaffs_obj *obj;
393
394		while (shadow_fixers) {
395			fixer = shadow_fixers;
396			shadow_fixers = fixer->next;
397			/* Complete the rename transaction by deleting the
398			 * shadowed object then setting the object header
399			 to unshadowed.
400			 */
401			obj = yaffs_find_by_number(dev, fixer->shadowed_id);
402			if (obj)
403				yaffs_del_obj(obj);
404
405			obj = yaffs_find_by_number(dev, fixer->obj_id);
406
407			if (obj)
408				yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
409
410			kfree(fixer);
411		}
412	}
413
414	yaffs_release_temp_buffer(dev, chunk_data);
415
416	if (alloc_failed)
417		return YAFFS_FAIL;
418
419	yaffs_trace(YAFFS_TRACE_SCAN, "yaffs1_scan ends");
420
421	return YAFFS_OK;
422}
423