2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2018 Aleph One Ltd.
6 * Created by Charles Manning <charles@aleph1.co.uk>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include "yaffs_yaffs1.h"
15 #include "yaffs_trace.h"
16 #include "yaffs_bitmap.h"
17 #include "yaffs_getblockinfo.h"
18 #include "yaffs_nand.h"
19 #include "yaffs_attribs.h"
21 int yaffs1_scan(struct yaffs_dev *dev)
23 #ifdef CONFIG_YAFFS_NO_YAFFS1
26 struct yaffs_ext_tags tags;
32 enum yaffs_block_state state;
34 struct yaffs_block_info *bi;
36 struct yaffs_obj_hdr *oh;
38 struct yaffs_obj *parent;
40 struct yaffs_shadow_fixer *shadow_fixers = NULL;
43 yaffs_trace(YAFFS_TRACE_SCAN,
44 "yaffs1_scan starts intstartblk %d intendblk %d...",
45 dev->internal_start_block, dev->internal_end_block);
47 chunk_data = yaffs_get_temp_buffer(dev);
49 dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
51 /* Scan all the blocks to determine their state */
53 for (blk = dev->internal_start_block; blk <= dev->internal_end_block;
55 yaffs_clear_chunk_bits(dev, blk);
57 bi->soft_del_pages = 0;
59 yaffs_query_init_block_state(dev, blk, &state, &seq_number);
61 bi->block_state = state;
62 bi->seq_number = seq_number;
64 if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
65 bi->block_state = state = YAFFS_BLOCK_STATE_DEAD;
67 yaffs_trace(YAFFS_TRACE_SCAN_DEBUG,
68 "Block scanning block %d state %d seq %d",
69 blk, state, seq_number);
71 if (state == YAFFS_BLOCK_STATE_DEAD) {
72 yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
73 "block %d is bad", blk);
74 } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
75 yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "Block empty ");
76 dev->n_erased_blocks++;
77 dev->n_free_chunks += dev->param.chunks_per_block;
82 /* For each block.... */
83 for (blk = dev->internal_start_block;
84 !alloc_failed && blk <= dev->internal_end_block; blk++) {
88 bi = yaffs_get_block_info(dev, blk);
89 state = bi->block_state;
93 /* For each chunk in each block that needs scanning.... */
95 !alloc_failed && c < dev->param.chunks_per_block &&
96 state == YAFFS_BLOCK_STATE_NEEDS_SCAN; c++) {
97 /* Read the tags and decide what to do */
98 chunk = blk * dev->param.chunks_per_block + c;
100 result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL,
103 if (result != YAFFS_OK)
105 /* Let's have a good look at this chunk... */
107 if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED ||
113 dev->n_free_chunks++;
114 } else if (!tags.chunk_used) {
115 /* An unassigned chunk in the block
116 * This means that either the block is empty or
117 * this is the one being allocated from
121 /* We're looking at the first chunk in
122 *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 */
127 yaffs_trace(YAFFS_TRACE_SCAN,
128 " Allocating from %d %d",
130 state = YAFFS_BLOCK_STATE_ALLOCATING;
131 dev->alloc_block = blk;
133 dev->alloc_block_finder = blk;
137 dev->n_free_chunks +=
138 (dev->param.chunks_per_block - c);
139 } else if (tags.chunk_id > 0) {
140 /* chunk_id > 0 so it is a data chunk... */
143 yaffs_set_chunk_bit(dev, blk, c);
146 in = yaffs_find_or_create_by_number(dev,
148 YAFFS_OBJECT_TYPE_FILE);
149 /* PutChunkIntoFile checks for a clash
150 * (two data chunks with the same chunk_id).
157 if (!yaffs_put_chunk_in_file
158 (in, tags.chunk_id, chunk, 1))
163 (tags.chunk_id - 1) *
164 dev->data_bytes_per_chunk +
168 YAFFS_OBJECT_TYPE_FILE &&
169 in->variant.file_variant.stored_size <
171 in->variant.file_variant.stored_size =
173 if (!dev->param.use_header_file_size) {
175 file_variant.file_size =
177 file_variant.stored_size;
182 /* chunk_id == 0, so it is an ObjectHeader.
185 yaffs_set_chunk_bit(dev, blk, c);
188 result = yaffs_rd_chunk_tags_nand(dev, chunk,
192 oh = (struct yaffs_obj_hdr *)chunk_data;
194 in = yaffs_find_by_number(dev, tags.obj_id);
195 if (in && in->variant_type != oh->type) {
196 /* This should not happen, but somehow
197 * Wev'e ended up with an obj_id that
198 * has been reused but not yet deleted,
199 * and worse still it has changed type.
200 * Delete the old object.
207 in = yaffs_find_or_create_by_number(dev,
214 if (in && oh->shadows_obj > 0) {
216 struct yaffs_shadow_fixer *fixer;
219 (struct yaffs_shadow_fixer),
222 fixer->next = shadow_fixers;
223 shadow_fixers = fixer;
224 fixer->obj_id = tags.obj_id;
227 yaffs_trace(YAFFS_TRACE_SCAN,
228 " Shadow fixer: %d shadows %d",
236 if (in && in->valid) {
237 /* We have already filled this one.
238 * We have a duplicate and need to
241 unsigned existing_serial = in->serial;
242 unsigned new_serial =
245 if (((existing_serial + 1) & 3) ==
247 /* Use new one - destroy the
254 /* Use existing - destroy
256 yaffs_chunk_del(dev, chunk, 1,
261 if (in && !in->valid &&
262 (tags.obj_id == YAFFS_OBJECTID_ROOT ||
264 YAFFS_OBJECTID_LOSTNFOUND)) {
265 /* We only load some info, don't fiddle
266 * with directory structure */
268 in->variant_type = oh->type;
270 in->yst_mode = oh->yst_mode;
271 yaffs_load_attribs(in, oh);
272 in->hdr_chunk = chunk;
273 in->serial = tags.serial_number;
275 } else if (in && !in->valid) {
276 /* we need to load this info */
279 in->variant_type = oh->type;
281 in->yst_mode = oh->yst_mode;
282 yaffs_load_attribs(in, oh);
283 in->hdr_chunk = chunk;
284 in->serial = tags.serial_number;
286 yaffs_set_obj_name_from_oh(in, oh);
289 /* directory stuff...
294 yaffs_find_or_create_by_number
295 (dev, oh->parent_obj_id,
296 YAFFS_OBJECT_TYPE_DIRECTORY);
299 if (parent && parent->variant_type ==
300 YAFFS_OBJECT_TYPE_UNKNOWN) {
301 /* Set up as a directory */
302 parent->variant_type =
303 YAFFS_OBJECT_TYPE_DIRECTORY;
304 INIT_LIST_HEAD(&parent->
307 } else if (!parent ||
308 parent->variant_type !=
309 YAFFS_OBJECT_TYPE_DIRECTORY) {
310 /* Hoosterman, a problem....
311 * We're trying to use a
312 * non-directory as a directory
315 yaffs_trace(YAFFS_TRACE_ERROR,
316 "yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
318 parent = dev->lost_n_found;
321 yaffs_add_obj_to_dir(parent, in);
323 switch (in->variant_type) {
324 case YAFFS_OBJECT_TYPE_UNKNOWN:
325 /* Todo got a problem */
327 case YAFFS_OBJECT_TYPE_FILE:
329 use_header_file_size)
331 file_variant.file_size
332 = yaffs_oh_to_size(dev, oh, 0);
334 case YAFFS_OBJECT_TYPE_HARDLINK:
336 hardlink_variant.equiv_id =
338 list_add(&in->hard_links,
341 case YAFFS_OBJECT_TYPE_DIRECTORY:
344 case YAFFS_OBJECT_TYPE_SPECIAL:
347 case YAFFS_OBJECT_TYPE_SYMLINK:
348 in->variant.symlink_variant.
350 yaffs_clone_str(oh->alias);
352 symlink_variant.alias)
360 if (state == YAFFS_BLOCK_STATE_NEEDS_SCAN) {
361 /* If we got this far while scanning,
362 * then the block is fully allocated. */
363 state = YAFFS_BLOCK_STATE_FULL;
366 if (state == YAFFS_BLOCK_STATE_ALLOCATING) {
367 /* If the block was partially allocated then
368 * treat it as fully allocated. */
369 state = YAFFS_BLOCK_STATE_FULL;
370 dev->alloc_block = -1;
373 bi->block_state = state;
375 /* Now let's see if it was dirty */
376 if (bi->pages_in_use == 0 &&
377 !bi->has_shrink_hdr &&
378 bi->block_state == YAFFS_BLOCK_STATE_FULL)
379 yaffs_block_became_dirty(dev, blk);
382 /* Ok, we've done all the scanning.
383 * Fix up the hard link chains.
384 * We should now have scanned all the objects, now it's time to add
388 yaffs_link_fixup(dev, &hard_list);
391 * Fix up any shadowed objects.
392 * There should not be more than one of these.
395 struct yaffs_shadow_fixer *fixer;
396 struct yaffs_obj *obj;
398 while (shadow_fixers) {
399 fixer = shadow_fixers;
400 shadow_fixers = fixer->next;
401 /* Complete the rename transaction by deleting the
402 * shadowed object then setting the object header
405 obj = yaffs_find_by_number(dev, fixer->shadowed_id);
409 obj = yaffs_find_by_number(dev, fixer->obj_id);
412 yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
418 yaffs_release_temp_buffer(dev, chunk_data);
423 yaffs_trace(YAFFS_TRACE_SCAN, "yaffs1_scan ends");