2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
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.
14 #include "yaffs_yaffs1.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"
22 int yaffs1_scan(struct yaffs_dev *dev)
24 struct yaffs_ext_tags tags;
31 enum yaffs_block_state state;
32 struct yaffs_obj *hard_list = NULL;
33 struct yaffs_block_info *bi;
35 struct yaffs_obj_hdr *oh;
37 struct yaffs_obj *parent;
41 struct yaffs_shadow_fixer *shadow_fixers = NULL;
49 (TSTR("yaffs1_scan starts intstartblk %d intendblk %d..." TENDSTR),
50 dev->internal_start_block, dev->internal_end_block));
52 chunk_data = yaffs_get_temp_buffer(dev, __LINE__);
54 dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
56 /* Scan all the blocks to determine their state */
58 for (blk = dev->internal_start_block; blk <= dev->internal_end_block; blk++) {
59 yaffs_clear_chunk_bits(dev, blk);
61 bi->soft_del_pages = 0;
63 yaffs_query_init_block_state(dev, blk, &state, &seq_number);
65 bi->block_state = state;
66 bi->seq_number = seq_number;
68 if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
69 bi->block_state = state = YAFFS_BLOCK_STATE_DEAD;
71 T(YAFFS_TRACE_SCAN_DEBUG,
72 (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
75 if (state == YAFFS_BLOCK_STATE_DEAD) {
76 T(YAFFS_TRACE_BAD_BLOCKS,
77 (TSTR("block %d is bad" TENDSTR), blk));
78 } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
79 T(YAFFS_TRACE_SCAN_DEBUG,
80 (TSTR("Block empty " TENDSTR)));
81 dev->n_erased_blocks++;
82 dev->n_free_chunks += dev->param.chunks_per_block;
87 /* For each block.... */
88 for (blk= dev->internal_start_block;
89 !alloc_failed && blk <= dev->internal_end_block;
94 bi = yaffs_get_block_info(dev, blk);
95 state = bi->block_state;
99 /* For each chunk in each block that needs scanning....*/
100 for (c = 0; !alloc_failed && c < dev->param.chunks_per_block &&
101 state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) {
102 /* Read the tags and decide what to do */
103 chunk = blk * dev->param.chunks_per_block + c;
105 result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL,
108 /* Let's have a good look at this chunk... */
110 if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED || tags.is_deleted) {
115 dev->n_free_chunks++;
116 /*T((" %d %d deleted\n",blk,c)); */
117 } else if (!tags.chunk_used) {
118 /* An unassigned chunk in the block
119 * This means that either the block is empty or
120 * this is the one being allocated from
124 /* We're looking at the first chunk in the block so the block is unused */
125 state = YAFFS_BLOCK_STATE_EMPTY;
126 dev->n_erased_blocks++;
128 /* this is the block being allocated from */
131 (" Allocating from %d %d" TENDSTR),
133 state = YAFFS_BLOCK_STATE_ALLOCATING;
134 dev->alloc_block = blk;
136 dev->alloc_block_finder = blk;
137 /* Set block finder here to encourage the allocator to go forth from here. */
141 dev->n_free_chunks += (dev->param.chunks_per_block - c);
142 } else if (tags.chunk_id > 0) {
143 /* chunk_id > 0 so it is a data chunk... */
146 yaffs_set_chunk_bit(dev, blk, c);
149 in = yaffs_find_or_create_by_number(dev,
152 YAFFS_OBJECT_TYPE_FILE);
153 /* PutChunkIntoFile checks for a clash (two data chunks with
154 * the same chunk_id).
161 if (!yaffs_put_chunk_in_file(in, tags.chunk_id, chunk, 1))
166 (tags.chunk_id - 1) * dev->data_bytes_per_chunk +
169 in->variant_type == YAFFS_OBJECT_TYPE_FILE
170 && in->variant.file_variant.scanned_size <
172 in->variant.file_variant.
173 scanned_size = endpos;
174 if (!dev->param.use_header_file_size) {
175 in->variant.file_variant.
177 in->variant.file_variant.
182 /* T((" %d %d data %d %d\n",blk,c,tags.obj_id,tags.chunk_id)); */
184 /* chunk_id == 0, so it is an ObjectHeader.
185 * Thus, we read in the object header and make the object
187 yaffs_set_chunk_bit(dev, blk, c);
190 result = yaffs_rd_chunk_tags_nand(dev, chunk,
194 oh = (struct yaffs_obj_hdr *) chunk_data;
196 in = yaffs_find_by_number(dev,
198 if (in && in->variant_type != oh->type) {
199 /* This should not happen, but somehow
200 * Wev'e ended up with an obj_id that has been reused but not yet
201 * deleted, and worse still it has changed type. Delete the old object.
209 in = yaffs_find_or_create_by_number(dev,
217 if (in && oh->shadows_obj > 0) {
219 struct yaffs_shadow_fixer *fixer;
220 fixer = YMALLOC(sizeof(struct yaffs_shadow_fixer));
222 fixer->next = shadow_fixers;
223 shadow_fixers = fixer;
224 fixer->obj_id = tags.obj_id;
225 fixer->shadowed_id = oh->shadows_obj;
228 (" Shadow fixer: %d shadows %d" TENDSTR),
229 fixer->obj_id, fixer->shadowed_id));
235 if (in && in->valid) {
236 /* We have already filled this one. We have a duplicate and need to resolve it. */
238 unsigned existing_serial = in->serial;
239 unsigned new_serial = tags.serial_number;
241 if (((existing_serial + 1) & 3) == new_serial) {
242 /* Use new one - destroy the exisiting one */
248 /* Use existing - destroy this one. */
249 yaffs_chunk_del(dev, chunk, 1,
254 if (in && !in->valid &&
255 (tags.obj_id == YAFFS_OBJECTID_ROOT ||
256 tags.obj_id == YAFFS_OBJECTID_LOSTNFOUND)) {
257 /* We only load some info, don't fiddle with directory structure */
259 in->variant_type = oh->type;
261 in->yst_mode = oh->yst_mode;
262 yaffs_load_attribs(in, oh);
263 in->hdr_chunk = chunk;
264 in->serial = tags.serial_number;
266 } else if (in && !in->valid) {
267 /* we need to load this info */
270 in->variant_type = oh->type;
272 in->yst_mode = oh->yst_mode;
273 yaffs_load_attribs(in, oh);
274 in->hdr_chunk = chunk;
275 in->serial = tags.serial_number;
277 yaffs_set_obj_name_from_oh(in, oh);
280 /* directory stuff...
285 yaffs_find_or_create_by_number
286 (dev, oh->parent_obj_id,
287 YAFFS_OBJECT_TYPE_DIRECTORY);
290 if (parent && parent->variant_type ==
291 YAFFS_OBJECT_TYPE_UNKNOWN) {
292 /* Set up as a directory */
293 parent->variant_type =
294 YAFFS_OBJECT_TYPE_DIRECTORY;
295 INIT_LIST_HEAD(&parent->variant.
298 } else if (!parent || parent->variant_type !=
299 YAFFS_OBJECT_TYPE_DIRECTORY) {
300 /* Hoosterman, another problem....
301 * We're trying to use a non-directory as a directory
306 ("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
308 parent = dev->lost_n_found;
311 yaffs_add_obj_to_dir(parent, in);
313 if (0 && (parent == dev->del_dir ||
314 parent == dev->unlinked_dir)) {
315 in->deleted = 1; /* If it is unlinked at start up then it wants deleting */
316 dev->n_deleted_files++;
318 /* Note re hardlinks.
319 * Since we might scan a hardlink before its equivalent object is scanned
320 * we put them all in a list.
321 * After scanning is complete, we should have all the objects, so we run through this
322 * list and fix up all the chains.
325 switch (in->variant_type) {
326 case YAFFS_OBJECT_TYPE_UNKNOWN:
327 /* Todo got a problem */
329 case YAFFS_OBJECT_TYPE_FILE:
330 if (dev->param.use_header_file_size)
332 in->variant.file_variant.
337 case YAFFS_OBJECT_TYPE_HARDLINK:
338 in->variant.hardlink_variant.
341 in->hard_links.next =
346 case YAFFS_OBJECT_TYPE_DIRECTORY:
349 case YAFFS_OBJECT_TYPE_SPECIAL:
352 case YAFFS_OBJECT_TYPE_SYMLINK:
353 in->variant.symlink_variant.alias =
354 yaffs_clone_str(oh->alias);
355 if (!in->variant.symlink_variant.alias)
364 if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
365 /* If we got this far while scanning, then the block is fully allocated.*/
366 state = YAFFS_BLOCK_STATE_FULL;
369 if (state == YAFFS_BLOCK_STATE_ALLOCATING) {
370 /* If the block was partially allocated then treat it as fully allocated.*/
371 state = YAFFS_BLOCK_STATE_FULL;
372 dev->alloc_block = -1;
375 bi->block_state = state;
377 /* Now let's see if it was dirty */
378 if (bi->pages_in_use == 0 &&
379 !bi->has_shrink_hdr &&
380 bi->block_state == YAFFS_BLOCK_STATE_FULL) {
381 yaffs_block_became_dirty(dev, blk);
387 /* Ok, we've done all the scanning.
388 * Fix up the hard link chains.
389 * We should now have scanned all the objects, now it's time to add these
393 yaffs_link_fixup(dev, hard_list);
395 /* Fix up any shadowed objects */
397 struct yaffs_shadow_fixer *fixer;
398 struct yaffs_obj *obj;
400 while (shadow_fixers) {
401 fixer = shadow_fixers;
402 shadow_fixers = fixer->next;
403 /* Complete the rename transaction by deleting the shadowed object
404 * then setting the object header to unshadowed.
406 obj = yaffs_find_by_number(dev, fixer->shadowed_id);
410 obj = yaffs_find_by_number(dev, fixer->obj_id);
413 yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
419 yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
424 T(YAFFS_TRACE_SCAN, (TSTR("yaffs1_scan ends" TENDSTR)));