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 } else if (!tags.chunk_used) {
114 /* An unassigned chunk in the block
115 * This means that either the block is empty or
116 * this is the one being allocated from
120 /* We're looking at the first chunk in the block so the block is unused */
121 state = YAFFS_BLOCK_STATE_EMPTY;
122 dev->n_erased_blocks++;
124 /* this is the block being allocated from */
125 yaffs_trace(YAFFS_TRACE_SCAN,
126 " Allocating from %d %d",
128 state = YAFFS_BLOCK_STATE_ALLOCATING;
129 dev->alloc_block = blk;
131 dev->alloc_block_finder = blk;
132 /* Set block finder here to encourage the allocator to go forth from here. */
136 dev->n_free_chunks +=
137 (dev->param.chunks_per_block - c);
138 } else if (tags.chunk_id > 0) {
139 /* chunk_id > 0 so it is a data chunk... */
142 yaffs_set_chunk_bit(dev, blk, c);
145 in = yaffs_find_or_create_by_number(dev,
147 YAFFS_OBJECT_TYPE_FILE);
148 /* PutChunkIntoFile checks for a clash (two data chunks with
149 * the same chunk_id).
156 if (!yaffs_put_chunk_in_file
157 (in, tags.chunk_id, chunk, 1))
163 1) * dev->data_bytes_per_chunk +
166 && in->variant_type ==
167 YAFFS_OBJECT_TYPE_FILE
168 && in->variant.file_variant.scanned_size <
170 in->variant.file_variant.scanned_size =
172 if (!dev->param.use_header_file_size) {
174 file_variant.file_size =
176 file_variant.scanned_size;
181 /* chunk_id == 0, so it is an ObjectHeader.
182 * Thus, we read in the object header and make the object
184 yaffs_set_chunk_bit(dev, blk, c);
187 result = yaffs_rd_chunk_tags_nand(dev, chunk,
191 oh = (struct yaffs_obj_hdr *)chunk_data;
193 in = yaffs_find_by_number(dev, tags.obj_id);
194 if (in && in->variant_type != oh->type) {
195 /* This should not happen, but somehow
196 * Wev'e ended up with an obj_id that has been reused but not yet
197 * deleted, and worse still it has changed type. Delete the old object.
205 in = yaffs_find_or_create_by_number(dev,
212 if (in && oh->shadows_obj > 0) {
214 struct yaffs_shadow_fixer *fixer;
217 (struct yaffs_shadow_fixer),
220 fixer->next = shadow_fixers;
221 shadow_fixers = fixer;
222 fixer->obj_id = tags.obj_id;
225 yaffs_trace(YAFFS_TRACE_SCAN,
226 " Shadow fixer: %d shadows %d",
234 if (in && in->valid) {
235 /* We have already filled this one. We have a duplicate and need to resolve it. */
237 unsigned existing_serial = in->serial;
238 unsigned new_serial =
241 if (((existing_serial + 1) & 3) ==
243 /* Use new one - destroy the exisiting one */
249 /* Use existing - destroy this one. */
250 yaffs_chunk_del(dev, chunk, 1,
255 if (in && !in->valid &&
256 (tags.obj_id == YAFFS_OBJECTID_ROOT ||
258 YAFFS_OBJECTID_LOSTNFOUND)) {
259 /* We only load some info, don't fiddle with directory structure */
261 in->variant_type = oh->type;
263 in->yst_mode = oh->yst_mode;
264 yaffs_load_attribs(in, oh);
265 in->hdr_chunk = chunk;
266 in->serial = tags.serial_number;
268 } else if (in && !in->valid) {
269 /* we need to load this info */
272 in->variant_type = oh->type;
274 in->yst_mode = oh->yst_mode;
275 yaffs_load_attribs(in, oh);
276 in->hdr_chunk = chunk;
277 in->serial = tags.serial_number;
279 yaffs_set_obj_name_from_oh(in, oh);
282 /* directory stuff...
287 yaffs_find_or_create_by_number
288 (dev, oh->parent_obj_id,
289 YAFFS_OBJECT_TYPE_DIRECTORY);
292 if (parent && parent->variant_type ==
293 YAFFS_OBJECT_TYPE_UNKNOWN) {
294 /* Set up as a directory */
295 parent->variant_type =
296 YAFFS_OBJECT_TYPE_DIRECTORY;
297 INIT_LIST_HEAD(&parent->
298 variant.dir_variant.children);
300 || parent->variant_type !=
301 YAFFS_OBJECT_TYPE_DIRECTORY) {
302 /* Hoosterman, another problem....
303 * We're trying to use a non-directory as a directory
306 yaffs_trace(YAFFS_TRACE_ERROR,
307 "yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
309 parent = dev->lost_n_found;
312 yaffs_add_obj_to_dir(parent, in);
314 if (0 && (parent == dev->del_dir ||
316 dev->unlinked_dir)) {
317 in->deleted = 1; /* If it is unlinked at start up then it wants deleting */
318 dev->n_deleted_files++;
320 /* Note re hardlinks.
321 * Since we might scan a hardlink before its equivalent object is scanned
322 * we put them all in a list.
323 * After scanning is complete, we should have all the objects, so we run through this
324 * list and fix up all the chains.
327 switch (in->variant_type) {
328 case YAFFS_OBJECT_TYPE_UNKNOWN:
329 /* Todo got a problem */
331 case YAFFS_OBJECT_TYPE_FILE:
333 use_header_file_size)
336 file_variant.file_size
340 case YAFFS_OBJECT_TYPE_HARDLINK:
342 hardlink_variant.equiv_id =
344 in->hard_links.next =
349 case YAFFS_OBJECT_TYPE_DIRECTORY:
352 case YAFFS_OBJECT_TYPE_SPECIAL:
355 case YAFFS_OBJECT_TYPE_SYMLINK:
356 in->variant.symlink_variant.
358 yaffs_clone_str(oh->alias);
360 symlink_variant.alias)
369 if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
370 /* If we got this far while scanning, then the block is fully allocated. */
371 state = YAFFS_BLOCK_STATE_FULL;
374 if (state == YAFFS_BLOCK_STATE_ALLOCATING) {
375 /* If the block was partially allocated then treat it as fully allocated. */
376 state = YAFFS_BLOCK_STATE_FULL;
377 dev->alloc_block = -1;
380 bi->block_state = state;
382 /* Now let's see if it was dirty */
383 if (bi->pages_in_use == 0 &&
384 !bi->has_shrink_hdr &&
385 bi->block_state == YAFFS_BLOCK_STATE_FULL) {
386 yaffs_block_became_dirty(dev, blk);
391 /* Ok, we've done all the scanning.
392 * Fix up the hard link chains.
393 * We should now have scanned all the objects, now it's time to add these
397 yaffs_link_fixup(dev, hard_list);
399 /* Fix up any shadowed objects */
401 struct yaffs_shadow_fixer *fixer;
402 struct yaffs_obj *obj;
404 while (shadow_fixers) {
405 fixer = shadow_fixers;
406 shadow_fixers = fixer->next;
407 /* Complete the rename transaction by deleting the shadowed object
408 * then setting the object header to unshadowed.
410 obj = yaffs_find_by_number(dev, fixer->shadowed_id);
414 obj = yaffs_find_by_number(dev, fixer->obj_id);
417 yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
423 yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
428 yaffs_trace(YAFFS_TRACE_SCAN, "yaffs1_scan ends");