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"
22 int yaffs1_scan(yaffs_dev_t *dev)
34 yaffs_block_state_t state;
35 yaffs_obj_t *hard_list = NULL;
36 yaffs_block_info_t *bi;
44 struct yaffs_shadow_fixer_s *shadowFixerList = NULL;
52 (TSTR("yaffs1_scan starts intstartblk %d intendblk %d..." TENDSTR),
53 dev->internal_start_block, dev->internal_end_block));
55 chunkData = yaffs_get_temp_buffer(dev, __LINE__);
57 dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
59 /* Scan all the blocks to determine their state */
61 for (blk = dev->internal_start_block; blk <= dev->internal_end_block; blk++) {
62 yaffs_clear_chunk_bits(dev, blk);
64 bi->soft_del_pages = 0;
66 yaffs_query_init_block_state(dev, blk, &state, &seq_number);
68 bi->block_state = state;
69 bi->seq_number = seq_number;
71 if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
72 bi->block_state = state = YAFFS_BLOCK_STATE_DEAD;
74 T(YAFFS_TRACE_SCAN_DEBUG,
75 (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
78 if (state == YAFFS_BLOCK_STATE_DEAD) {
79 T(YAFFS_TRACE_BAD_BLOCKS,
80 (TSTR("block %d is bad" TENDSTR), blk));
81 } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
82 T(YAFFS_TRACE_SCAN_DEBUG,
83 (TSTR("Block empty " TENDSTR)));
84 dev->n_erased_blocks++;
85 dev->n_free_chunks += dev->param.chunks_per_block;
90 startIterator = dev->internal_start_block;
91 endIterator = dev->internal_end_block;
93 /* For each block.... */
94 for (blockIterator = startIterator; !alloc_failed && blockIterator <= endIterator;
103 bi = yaffs_get_block_info(dev, blk);
104 state = bi->block_state;
108 /* For each chunk in each block that needs scanning....*/
109 for (c = 0; !alloc_failed && c < dev->param.chunks_per_block &&
110 state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) {
111 /* Read the tags and decide what to do */
112 chunk = blk * dev->param.chunks_per_block + c;
114 result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL,
117 /* Let's have a good look at this chunk... */
119 if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED || tags.is_deleted) {
124 dev->n_free_chunks++;
125 /*T((" %d %d deleted\n",blk,c)); */
126 } else if (!tags.chunk_used) {
127 /* An unassigned chunk in the block
128 * This means that either the block is empty or
129 * this is the one being allocated from
133 /* We're looking at the first chunk in the block so the block is unused */
134 state = YAFFS_BLOCK_STATE_EMPTY;
135 dev->n_erased_blocks++;
137 /* this is the block being allocated from */
140 (" Allocating from %d %d" TENDSTR),
142 state = YAFFS_BLOCK_STATE_ALLOCATING;
143 dev->alloc_block = blk;
145 dev->alloc_block_finder = blk;
146 /* Set block finder here to encourage the allocator to go forth from here. */
150 dev->n_free_chunks += (dev->param.chunks_per_block - c);
151 } else if (tags.chunk_id > 0) {
152 /* chunk_id > 0 so it is a data chunk... */
155 yaffs_set_chunk_bit(dev, blk, c);
158 in = yaffs_find_or_create_by_number(dev,
161 YAFFS_OBJECT_TYPE_FILE);
162 /* PutChunkIntoFile checks for a clash (two data chunks with
163 * the same chunk_id).
170 if (!yaffs_put_chunk_in_file(in, tags.chunk_id, chunk, 1))
175 (tags.chunk_id - 1) * dev->data_bytes_per_chunk +
178 in->variant_type == YAFFS_OBJECT_TYPE_FILE
179 && in->variant.file_variant.scanned_size <
181 in->variant.file_variant.
182 scanned_size = endpos;
183 if (!dev->param.use_header_file_size) {
184 in->variant.file_variant.
186 in->variant.file_variant.
191 /* T((" %d %d data %d %d\n",blk,c,tags.obj_id,tags.chunk_id)); */
193 /* chunk_id == 0, so it is an ObjectHeader.
194 * Thus, we read in the object header and make the object
196 yaffs_set_chunk_bit(dev, blk, c);
199 result = yaffs_rd_chunk_tags_nand(dev, chunk,
203 oh = (yaffs_obj_header *) chunkData;
205 in = yaffs_find_by_number(dev,
207 if (in && in->variant_type != oh->type) {
208 /* This should not happen, but somehow
209 * Wev'e ended up with an obj_id that has been reused but not yet
210 * deleted, and worse still it has changed type. Delete the old object.
218 in = yaffs_find_or_create_by_number(dev,
226 if (in && oh->shadows_obj > 0) {
228 struct yaffs_shadow_fixer_s *fixer;
229 fixer = YMALLOC(sizeof(struct yaffs_shadow_fixer_s));
231 fixer->next = shadowFixerList;
232 shadowFixerList = fixer;
233 fixer->obj_id = tags.obj_id;
234 fixer->shadowed_id = oh->shadows_obj;
237 (" Shadow fixer: %d shadows %d" TENDSTR),
238 fixer->obj_id, fixer->shadowed_id));
244 if (in && in->valid) {
245 /* We have already filled this one. We have a duplicate and need to resolve it. */
247 unsigned existingSerial = in->serial;
248 unsigned newSerial = tags.serial_number;
250 if (((existingSerial + 1) & 3) == newSerial) {
251 /* Use new one - destroy the exisiting one */
257 /* Use existing - destroy this one. */
258 yaffs_chunk_del(dev, chunk, 1,
263 if (in && !in->valid &&
264 (tags.obj_id == YAFFS_OBJECTID_ROOT ||
265 tags.obj_id == YAFFS_OBJECTID_LOSTNFOUND)) {
266 /* We only load some info, don't fiddle with directory structure */
268 in->variant_type = oh->type;
270 in->yst_mode = oh->yst_mode;
271 #ifdef CONFIG_YAFFS_WINCE
272 in->win_atime[0] = oh->win_atime[0];
273 in->win_ctime[0] = oh->win_ctime[0];
274 in->win_mtime[0] = oh->win_mtime[0];
275 in->win_atime[1] = oh->win_atime[1];
276 in->win_ctime[1] = oh->win_ctime[1];
277 in->win_mtime[1] = oh->win_mtime[1];
279 in->yst_uid = oh->yst_uid;
280 in->yst_gid = oh->yst_gid;
281 in->yst_atime = oh->yst_atime;
282 in->yst_mtime = oh->yst_mtime;
283 in->yst_ctime = oh->yst_ctime;
284 in->yst_rdev = oh->yst_rdev;
286 in->hdr_chunk = chunk;
287 in->serial = tags.serial_number;
289 } else if (in && !in->valid) {
290 /* we need to load this info */
293 in->variant_type = oh->type;
295 in->yst_mode = oh->yst_mode;
296 #ifdef CONFIG_YAFFS_WINCE
297 in->win_atime[0] = oh->win_atime[0];
298 in->win_ctime[0] = oh->win_ctime[0];
299 in->win_mtime[0] = oh->win_mtime[0];
300 in->win_atime[1] = oh->win_atime[1];
301 in->win_ctime[1] = oh->win_ctime[1];
302 in->win_mtime[1] = oh->win_mtime[1];
304 in->yst_uid = oh->yst_uid;
305 in->yst_gid = oh->yst_gid;
306 in->yst_atime = oh->yst_atime;
307 in->yst_mtime = oh->yst_mtime;
308 in->yst_ctime = oh->yst_ctime;
309 in->yst_rdev = oh->yst_rdev;
311 in->hdr_chunk = chunk;
312 in->serial = tags.serial_number;
314 yaffs_set_obj_name_from_oh(in, oh);
317 /* directory stuff...
322 yaffs_find_or_create_by_number
323 (dev, oh->parent_obj_id,
324 YAFFS_OBJECT_TYPE_DIRECTORY);
327 if (parent && parent->variant_type ==
328 YAFFS_OBJECT_TYPE_UNKNOWN) {
329 /* Set up as a directory */
330 parent->variant_type =
331 YAFFS_OBJECT_TYPE_DIRECTORY;
332 YINIT_LIST_HEAD(&parent->variant.
335 } else if (!parent || parent->variant_type !=
336 YAFFS_OBJECT_TYPE_DIRECTORY) {
337 /* Hoosterman, another problem....
338 * We're trying to use a non-directory as a directory
343 ("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
345 parent = dev->lost_n_found;
348 yaffs_add_obj_to_dir(parent, in);
350 if (0 && (parent == dev->del_dir ||
351 parent == dev->unlinked_dir)) {
352 in->deleted = 1; /* If it is unlinked at start up then it wants deleting */
353 dev->n_deleted_files++;
355 /* Note re hardlinks.
356 * Since we might scan a hardlink before its equivalent object is scanned
357 * we put them all in a list.
358 * After scanning is complete, we should have all the objects, so we run through this
359 * list and fix up all the chains.
362 switch (in->variant_type) {
363 case YAFFS_OBJECT_TYPE_UNKNOWN:
364 /* Todo got a problem */
366 case YAFFS_OBJECT_TYPE_FILE:
367 if (dev->param.use_header_file_size)
369 in->variant.file_variant.
374 case YAFFS_OBJECT_TYPE_HARDLINK:
375 in->variant.hardlink_variant.
378 in->hard_links.next =
379 (struct ylist_head *)
383 case YAFFS_OBJECT_TYPE_DIRECTORY:
386 case YAFFS_OBJECT_TYPE_SPECIAL:
389 case YAFFS_OBJECT_TYPE_SYMLINK:
390 in->variant.symlink_variant.alias =
391 yaffs_clone_str(oh->alias);
392 if (!in->variant.symlink_variant.alias)
401 if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
402 /* If we got this far while scanning, then the block is fully allocated.*/
403 state = YAFFS_BLOCK_STATE_FULL;
406 if (state == YAFFS_BLOCK_STATE_ALLOCATING) {
407 /* If the block was partially allocated then treat it as fully allocated.*/
408 state = YAFFS_BLOCK_STATE_FULL;
409 dev->alloc_block = -1;
412 bi->block_state = state;
414 /* Now let's see if it was dirty */
415 if (bi->pages_in_use == 0 &&
416 !bi->has_shrink_hdr &&
417 bi->block_state == YAFFS_BLOCK_STATE_FULL) {
418 yaffs_block_became_dirty(dev, blk);
424 /* Ok, we've done all the scanning.
425 * Fix up the hard link chains.
426 * We should now have scanned all the objects, now it's time to add these
430 yaffs_link_fixup(dev, hard_list);
432 /* Fix up any shadowed objects */
434 struct yaffs_shadow_fixer_s *fixer;
437 while (shadowFixerList) {
438 fixer = shadowFixerList;
439 shadowFixerList = fixer->next;
440 /* Complete the rename transaction by deleting the shadowed object
441 * then setting the object header to unshadowed.
443 obj = yaffs_find_by_number(dev, fixer->shadowed_id);
447 obj = yaffs_find_by_number(dev, fixer->obj_id);
450 yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
456 yaffs_release_temp_buffer(dev, chunkData, __LINE__);
461 T(YAFFS_TRACE_SCAN, (TSTR("yaffs1_scan ends" TENDSTR)));