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;
45 yaffs_trace(YAFFS_TRACE_SCAN,
46 "yaffs1_scan starts intstartblk %d intendblk %d...",
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 yaffs_trace(YAFFS_TRACE_SCAN_DEBUG,
70 "Block scanning block %d state %d seq %d",
71 blk, state, seq_number);
73 if (state == YAFFS_BLOCK_STATE_DEAD) {
74 yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
75 "block %d is bad", blk);
76 } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
77 yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "Block empty ");
78 dev->n_erased_blocks++;
79 dev->n_free_chunks += dev->param.chunks_per_block;
84 /* For each block.... */
85 for (blk = dev->internal_start_block;
86 !alloc_failed && blk <= dev->internal_end_block; blk++) {
90 bi = yaffs_get_block_info(dev, blk);
91 state = bi->block_state;
95 /* For each chunk in each block that needs scanning.... */
96 for (c = 0; !alloc_failed && c < dev->param.chunks_per_block &&
97 state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) {
98 /* Read the tags and decide what to do */
99 chunk = blk * dev->param.chunks_per_block + c;
101 result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL,
104 /* Let's have a good look at this chunk... */
106 if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED
107 || tags.is_deleted) {
112 dev->n_free_chunks++;
113 /*T((" %d %d deleted\n",blk,c)); */
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 the block so the block is unused */
122 state = YAFFS_BLOCK_STATE_EMPTY;
123 dev->n_erased_blocks++;
125 /* this is the block being allocated from */
126 yaffs_trace(YAFFS_TRACE_SCAN,
127 " Allocating from %d %d",
129 state = YAFFS_BLOCK_STATE_ALLOCATING;
130 dev->alloc_block = blk;
132 dev->alloc_block_finder = blk;
133 /* Set block finder here to encourage the allocator to go forth from here. */
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 (two data chunks with
150 * the same chunk_id).
157 if (!yaffs_put_chunk_in_file
158 (in, tags.chunk_id, chunk, 1))
164 1) * dev->data_bytes_per_chunk +
167 && in->variant_type ==
168 YAFFS_OBJECT_TYPE_FILE
169 && in->variant.file_variant.scanned_size <
171 in->variant.file_variant.scanned_size =
173 if (!dev->param.use_header_file_size) {
175 file_variant.file_size =
177 file_variant.scanned_size;
181 /* T((" %d %d data %d %d\n",blk,c,tags.obj_id,tags.chunk_id)); */
183 /* chunk_id == 0, so it is an ObjectHeader.
184 * Thus, we read in the object header and make the object
186 yaffs_set_chunk_bit(dev, blk, c);
189 result = yaffs_rd_chunk_tags_nand(dev, chunk,
193 oh = (struct yaffs_obj_hdr *)chunk_data;
195 in = yaffs_find_by_number(dev, tags.obj_id);
196 if (in && in->variant_type != oh->type) {
197 /* This should not happen, but somehow
198 * Wev'e ended up with an obj_id that has been reused but not yet
199 * deleted, and worse still it has changed type. 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. We have a duplicate and need to resolve it. */
239 unsigned existing_serial = in->serial;
240 unsigned new_serial =
243 if (((existing_serial + 1) & 3) ==
245 /* Use new one - destroy the exisiting one */
251 /* Use existing - destroy this one. */
252 yaffs_chunk_del(dev, chunk, 1,
257 if (in && !in->valid &&
258 (tags.obj_id == YAFFS_OBJECTID_ROOT ||
260 YAFFS_OBJECTID_LOSTNFOUND)) {
261 /* We only load some info, don't fiddle with directory structure */
263 in->variant_type = oh->type;
265 in->yst_mode = oh->yst_mode;
266 yaffs_load_attribs(in, oh);
267 in->hdr_chunk = chunk;
268 in->serial = tags.serial_number;
270 } else if (in && !in->valid) {
271 /* we need to load this info */
274 in->variant_type = oh->type;
276 in->yst_mode = oh->yst_mode;
277 yaffs_load_attribs(in, oh);
278 in->hdr_chunk = chunk;
279 in->serial = tags.serial_number;
281 yaffs_set_obj_name_from_oh(in, oh);
284 /* directory stuff...
289 yaffs_find_or_create_by_number
290 (dev, oh->parent_obj_id,
291 YAFFS_OBJECT_TYPE_DIRECTORY);
294 if (parent && parent->variant_type ==
295 YAFFS_OBJECT_TYPE_UNKNOWN) {
296 /* Set up as a directory */
297 parent->variant_type =
298 YAFFS_OBJECT_TYPE_DIRECTORY;
299 INIT_LIST_HEAD(&parent->
300 variant.dir_variant.children);
302 || parent->variant_type !=
303 YAFFS_OBJECT_TYPE_DIRECTORY) {
304 /* Hoosterman, another problem....
305 * We're trying to use a non-directory as a directory
308 yaffs_trace(YAFFS_TRACE_ERROR,
309 "yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
311 parent = dev->lost_n_found;
314 yaffs_add_obj_to_dir(parent, in);
316 if (0 && (parent == dev->del_dir ||
318 dev->unlinked_dir)) {
319 in->deleted = 1; /* If it is unlinked at start up then it wants deleting */
320 dev->n_deleted_files++;
322 /* Note re hardlinks.
323 * Since we might scan a hardlink before its equivalent object is scanned
324 * we put them all in a list.
325 * After scanning is complete, we should have all the objects, so we run through this
326 * list and fix up all the chains.
329 switch (in->variant_type) {
330 case YAFFS_OBJECT_TYPE_UNKNOWN:
331 /* Todo got a problem */
333 case YAFFS_OBJECT_TYPE_FILE:
335 use_header_file_size)
338 file_variant.file_size
342 case YAFFS_OBJECT_TYPE_HARDLINK:
344 hardlink_variant.equiv_id =
346 in->hard_links.next =
351 case YAFFS_OBJECT_TYPE_DIRECTORY:
354 case YAFFS_OBJECT_TYPE_SPECIAL:
357 case YAFFS_OBJECT_TYPE_SYMLINK:
358 in->variant.symlink_variant.
360 yaffs_clone_str(oh->alias);
362 symlink_variant.alias)
371 if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
372 /* If we got this far while scanning, then the block is fully allocated. */
373 state = YAFFS_BLOCK_STATE_FULL;
376 if (state == YAFFS_BLOCK_STATE_ALLOCATING) {
377 /* If the block was partially allocated then treat it as fully allocated. */
378 state = YAFFS_BLOCK_STATE_FULL;
379 dev->alloc_block = -1;
382 bi->block_state = state;
384 /* Now let's see if it was dirty */
385 if (bi->pages_in_use == 0 &&
386 !bi->has_shrink_hdr &&
387 bi->block_state == YAFFS_BLOCK_STATE_FULL) {
388 yaffs_block_became_dirty(dev, blk);
393 /* Ok, we've done all the scanning.
394 * Fix up the hard link chains.
395 * We should now have scanned all the objects, now it's time to add these
399 yaffs_link_fixup(dev, hard_list);
401 /* Fix up any shadowed objects */
403 struct yaffs_shadow_fixer *fixer;
404 struct yaffs_obj *obj;
406 while (shadow_fixers) {
407 fixer = shadow_fixers;
408 shadow_fixers = fixer->next;
409 /* Complete the rename transaction by deleting the shadowed object
410 * then setting the object header to unshadowed.
412 obj = yaffs_find_by_number(dev, fixer->shadowed_id);
416 obj = yaffs_find_by_number(dev, fixer->obj_id);
419 yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
425 yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
430 yaffs_trace(YAFFS_TRACE_SCAN, "yaffs1_scan ends");