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;
46 (TSTR("yaffs1_scan starts intstartblk %d intendblk %d..." TENDSTR),
47 dev->internal_start_block, dev->internal_end_block));
49 chunk_data = yaffs_get_temp_buffer(dev, __LINE__);
51 dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
53 /* Scan all the blocks to determine their state */
55 for (blk = dev->internal_start_block; blk <= dev->internal_end_block;
57 yaffs_clear_chunk_bits(dev, blk);
59 bi->soft_del_pages = 0;
61 yaffs_query_init_block_state(dev, blk, &state, &seq_number);
63 bi->block_state = state;
64 bi->seq_number = seq_number;
66 if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
67 bi->block_state = state = YAFFS_BLOCK_STATE_DEAD;
69 T(YAFFS_TRACE_SCAN_DEBUG,
70 (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
73 if (state == YAFFS_BLOCK_STATE_DEAD) {
74 T(YAFFS_TRACE_BAD_BLOCKS,
75 (TSTR("block %d is bad" TENDSTR), blk));
76 } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
77 T(YAFFS_TRACE_SCAN_DEBUG,
78 (TSTR("Block empty " TENDSTR)));
79 dev->n_erased_blocks++;
80 dev->n_free_chunks += dev->param.chunks_per_block;
85 /* For each block.... */
86 for (blk = dev->internal_start_block;
87 !alloc_failed && blk <= dev->internal_end_block; blk++) {
91 bi = yaffs_get_block_info(dev, blk);
92 state = bi->block_state;
96 /* For each chunk in each block that needs scanning.... */
97 for (c = 0; !alloc_failed && c < dev->param.chunks_per_block &&
98 state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) {
99 /* Read the tags and decide what to do */
100 chunk = blk * dev->param.chunks_per_block + c;
102 result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL,
105 /* Let's have a good look at this chunk... */
107 if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED
108 || tags.is_deleted) {
113 dev->n_free_chunks++;
114 /*T((" %d %d deleted\n",blk,c)); */
115 } else if (!tags.chunk_used) {
116 /* An unassigned chunk in the block
117 * This means that either the block is empty or
118 * this is the one being allocated from
122 /* We're looking at the first chunk in the block so the block is unused */
123 state = YAFFS_BLOCK_STATE_EMPTY;
124 dev->n_erased_blocks++;
126 /* this is the block being allocated from */
129 (" Allocating from %d %d" TENDSTR),
131 state = YAFFS_BLOCK_STATE_ALLOCATING;
132 dev->alloc_block = blk;
134 dev->alloc_block_finder = blk;
135 /* Set block finder here to encourage the allocator to go forth from here. */
139 dev->n_free_chunks +=
140 (dev->param.chunks_per_block - c);
141 } else if (tags.chunk_id > 0) {
142 /* chunk_id > 0 so it is a data chunk... */
145 yaffs_set_chunk_bit(dev, blk, c);
148 in = yaffs_find_or_create_by_number(dev,
150 YAFFS_OBJECT_TYPE_FILE);
151 /* PutChunkIntoFile checks for a clash (two data chunks with
152 * the same chunk_id).
159 if (!yaffs_put_chunk_in_file
160 (in, tags.chunk_id, chunk, 1))
166 1) * dev->data_bytes_per_chunk +
169 && in->variant_type ==
170 YAFFS_OBJECT_TYPE_FILE
171 && in->variant.file_variant.scanned_size <
173 in->variant.file_variant.scanned_size =
175 if (!dev->param.use_header_file_size) {
177 file_variant.file_size =
179 file_variant.scanned_size;
183 /* T((" %d %d data %d %d\n",blk,c,tags.obj_id,tags.chunk_id)); */
185 /* chunk_id == 0, so it is an ObjectHeader.
186 * Thus, we read in the object header and make the object
188 yaffs_set_chunk_bit(dev, blk, c);
191 result = yaffs_rd_chunk_tags_nand(dev, chunk,
195 oh = (struct yaffs_obj_hdr *)chunk_data;
197 in = yaffs_find_by_number(dev, tags.obj_id);
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,
216 if (in && oh->shadows_obj > 0) {
218 struct yaffs_shadow_fixer *fixer;
222 yaffs_shadow_fixer));
224 fixer->next = shadow_fixers;
225 shadow_fixers = fixer;
226 fixer->obj_id = tags.obj_id;
231 (" Shadow fixer: %d shadows %d"
232 TENDSTR), fixer->obj_id,
233 fixer->shadowed_id));
239 if (in && in->valid) {
240 /* We have already filled this one. We have a duplicate and need to resolve it. */
242 unsigned existing_serial = in->serial;
243 unsigned new_serial =
246 if (((existing_serial + 1) & 3) ==
248 /* Use new one - destroy the exisiting one */
254 /* Use existing - destroy this one. */
255 yaffs_chunk_del(dev, chunk, 1,
260 if (in && !in->valid &&
261 (tags.obj_id == YAFFS_OBJECTID_ROOT ||
263 YAFFS_OBJECTID_LOSTNFOUND)) {
264 /* We only load some info, don't fiddle with directory structure */
266 in->variant_type = oh->type;
268 in->yst_mode = oh->yst_mode;
269 yaffs_load_attribs(in, oh);
270 in->hdr_chunk = chunk;
271 in->serial = tags.serial_number;
273 } else if (in && !in->valid) {
274 /* we need to load this info */
277 in->variant_type = oh->type;
279 in->yst_mode = oh->yst_mode;
280 yaffs_load_attribs(in, oh);
281 in->hdr_chunk = chunk;
282 in->serial = tags.serial_number;
284 yaffs_set_obj_name_from_oh(in, oh);
287 /* directory stuff...
292 yaffs_find_or_create_by_number
293 (dev, oh->parent_obj_id,
294 YAFFS_OBJECT_TYPE_DIRECTORY);
297 if (parent && parent->variant_type ==
298 YAFFS_OBJECT_TYPE_UNKNOWN) {
299 /* Set up as a directory */
300 parent->variant_type =
301 YAFFS_OBJECT_TYPE_DIRECTORY;
302 INIT_LIST_HEAD(&parent->
303 variant.dir_variant.children);
305 || parent->variant_type !=
306 YAFFS_OBJECT_TYPE_DIRECTORY) {
307 /* Hoosterman, another problem....
308 * We're trying to use a non-directory as a directory
313 ("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
315 parent = dev->lost_n_found;
318 yaffs_add_obj_to_dir(parent, in);
320 if (0 && (parent == dev->del_dir ||
322 dev->unlinked_dir)) {
323 in->deleted = 1; /* If it is unlinked at start up then it wants deleting */
324 dev->n_deleted_files++;
326 /* Note re hardlinks.
327 * Since we might scan a hardlink before its equivalent object is scanned
328 * we put them all in a list.
329 * After scanning is complete, we should have all the objects, so we run through this
330 * list and fix up all the chains.
333 switch (in->variant_type) {
334 case YAFFS_OBJECT_TYPE_UNKNOWN:
335 /* Todo got a problem */
337 case YAFFS_OBJECT_TYPE_FILE:
339 use_header_file_size)
342 file_variant.file_size
346 case YAFFS_OBJECT_TYPE_HARDLINK:
348 hardlink_variant.equiv_id =
350 in->hard_links.next =
355 case YAFFS_OBJECT_TYPE_DIRECTORY:
358 case YAFFS_OBJECT_TYPE_SPECIAL:
361 case YAFFS_OBJECT_TYPE_SYMLINK:
362 in->variant.symlink_variant.
364 yaffs_clone_str(oh->alias);
366 symlink_variant.alias)
375 if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
376 /* If we got this far while scanning, then the block is fully allocated. */
377 state = YAFFS_BLOCK_STATE_FULL;
380 if (state == YAFFS_BLOCK_STATE_ALLOCATING) {
381 /* If the block was partially allocated then treat it as fully allocated. */
382 state = YAFFS_BLOCK_STATE_FULL;
383 dev->alloc_block = -1;
386 bi->block_state = state;
388 /* Now let's see if it was dirty */
389 if (bi->pages_in_use == 0 &&
390 !bi->has_shrink_hdr &&
391 bi->block_state == YAFFS_BLOCK_STATE_FULL) {
392 yaffs_block_became_dirty(dev, blk);
397 /* Ok, we've done all the scanning.
398 * Fix up the hard link chains.
399 * We should now have scanned all the objects, now it's time to add these
403 yaffs_link_fixup(dev, hard_list);
405 /* Fix up any shadowed objects */
407 struct yaffs_shadow_fixer *fixer;
408 struct yaffs_obj *obj;
410 while (shadow_fixers) {
411 fixer = shadow_fixers;
412 shadow_fixers = fixer->next;
413 /* Complete the rename transaction by deleting the shadowed object
414 * then setting the object header to unshadowed.
416 obj = yaffs_find_by_number(dev, fixer->shadowed_id);
420 obj = yaffs_find_by_number(dev, fixer->obj_id);
423 yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
429 yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
434 T(YAFFS_TRACE_SCAN, (TSTR("yaffs1_scan ends" TENDSTR)));