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)
31 yaffs_block_state_t state;
32 yaffs_obj_t *hard_list = NULL;
33 yaffs_block_info_t *bi;
41 struct yaffs_shadow_fixer_s *shadow_fixers = NULL;
49 (TSTR("yaffs1_scan starts intstartblk %d intendblk %d..." TENDSTR),
50 dev->internal_start_block, dev->internal_end_block));
52 chunk_data = yaffs_get_temp_buffer(dev, __LINE__);
54 dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
56 /* Scan all the blocks to determine their state */
58 for (blk = dev->internal_start_block; blk <= dev->internal_end_block; blk++) {
59 yaffs_clear_chunk_bits(dev, blk);
61 bi->soft_del_pages = 0;
63 yaffs_query_init_block_state(dev, blk, &state, &seq_number);
65 bi->block_state = state;
66 bi->seq_number = seq_number;
68 if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
69 bi->block_state = state = YAFFS_BLOCK_STATE_DEAD;
71 T(YAFFS_TRACE_SCAN_DEBUG,
72 (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
75 if (state == YAFFS_BLOCK_STATE_DEAD) {
76 T(YAFFS_TRACE_BAD_BLOCKS,
77 (TSTR("block %d is bad" TENDSTR), blk));
78 } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
79 T(YAFFS_TRACE_SCAN_DEBUG,
80 (TSTR("Block empty " TENDSTR)));
81 dev->n_erased_blocks++;
82 dev->n_free_chunks += dev->param.chunks_per_block;
87 /* For each block.... */
88 for (blk= dev->internal_start_block;
89 !alloc_failed && blk <= dev->internal_end_block;
94 bi = yaffs_get_block_info(dev, blk);
95 state = bi->block_state;
99 /* For each chunk in each block that needs scanning....*/
100 for (c = 0; !alloc_failed && c < dev->param.chunks_per_block &&
101 state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) {
102 /* Read the tags and decide what to do */
103 chunk = blk * dev->param.chunks_per_block + c;
105 result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL,
108 /* Let's have a good look at this chunk... */
110 if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED || tags.is_deleted) {
115 dev->n_free_chunks++;
116 /*T((" %d %d deleted\n",blk,c)); */
117 } else if (!tags.chunk_used) {
118 /* An unassigned chunk in the block
119 * This means that either the block is empty or
120 * this is the one being allocated from
124 /* We're looking at the first chunk in the block so the block is unused */
125 state = YAFFS_BLOCK_STATE_EMPTY;
126 dev->n_erased_blocks++;
128 /* this is the block being allocated from */
131 (" Allocating from %d %d" TENDSTR),
133 state = YAFFS_BLOCK_STATE_ALLOCATING;
134 dev->alloc_block = blk;
136 dev->alloc_block_finder = blk;
137 /* Set block finder here to encourage the allocator to go forth from here. */
141 dev->n_free_chunks += (dev->param.chunks_per_block - c);
142 } else if (tags.chunk_id > 0) {
143 /* chunk_id > 0 so it is a data chunk... */
146 yaffs_set_chunk_bit(dev, blk, c);
149 in = yaffs_find_or_create_by_number(dev,
152 YAFFS_OBJECT_TYPE_FILE);
153 /* PutChunkIntoFile checks for a clash (two data chunks with
154 * the same chunk_id).
161 if (!yaffs_put_chunk_in_file(in, tags.chunk_id, chunk, 1))
166 (tags.chunk_id - 1) * dev->data_bytes_per_chunk +
169 in->variant_type == YAFFS_OBJECT_TYPE_FILE
170 && in->variant.file_variant.scanned_size <
172 in->variant.file_variant.
173 scanned_size = endpos;
174 if (!dev->param.use_header_file_size) {
175 in->variant.file_variant.
177 in->variant.file_variant.
182 /* T((" %d %d data %d %d\n",blk,c,tags.obj_id,tags.chunk_id)); */
184 /* chunk_id == 0, so it is an ObjectHeader.
185 * Thus, we read in the object header and make the object
187 yaffs_set_chunk_bit(dev, blk, c);
190 result = yaffs_rd_chunk_tags_nand(dev, chunk,
194 oh = (yaffs_obj_header *) chunk_data;
196 in = yaffs_find_by_number(dev,
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,
217 if (in && oh->shadows_obj > 0) {
219 struct yaffs_shadow_fixer_s *fixer;
220 fixer = YMALLOC(sizeof(struct yaffs_shadow_fixer_s));
222 fixer->next = shadow_fixers;
223 shadow_fixers = fixer;
224 fixer->obj_id = tags.obj_id;
225 fixer->shadowed_id = oh->shadows_obj;
228 (" Shadow fixer: %d shadows %d" TENDSTR),
229 fixer->obj_id, fixer->shadowed_id));
235 if (in && in->valid) {
236 /* We have already filled this one. We have a duplicate and need to resolve it. */
238 unsigned existing_serial = in->serial;
239 unsigned new_serial = tags.serial_number;
241 if (((existing_serial + 1) & 3) == new_serial) {
242 /* Use new one - destroy the exisiting one */
248 /* Use existing - destroy this one. */
249 yaffs_chunk_del(dev, chunk, 1,
254 if (in && !in->valid &&
255 (tags.obj_id == YAFFS_OBJECTID_ROOT ||
256 tags.obj_id == YAFFS_OBJECTID_LOSTNFOUND)) {
257 /* We only load some info, don't fiddle with directory structure */
259 in->variant_type = oh->type;
261 in->yst_mode = oh->yst_mode;
262 #ifdef CONFIG_YAFFS_WINCE
263 in->win_atime[0] = oh->win_atime[0];
264 in->win_ctime[0] = oh->win_ctime[0];
265 in->win_mtime[0] = oh->win_mtime[0];
266 in->win_atime[1] = oh->win_atime[1];
267 in->win_ctime[1] = oh->win_ctime[1];
268 in->win_mtime[1] = oh->win_mtime[1];
270 in->yst_uid = oh->yst_uid;
271 in->yst_gid = oh->yst_gid;
272 in->yst_atime = oh->yst_atime;
273 in->yst_mtime = oh->yst_mtime;
274 in->yst_ctime = oh->yst_ctime;
275 in->yst_rdev = oh->yst_rdev;
277 in->hdr_chunk = chunk;
278 in->serial = tags.serial_number;
280 } else if (in && !in->valid) {
281 /* we need to load this info */
284 in->variant_type = oh->type;
286 in->yst_mode = oh->yst_mode;
287 #ifdef CONFIG_YAFFS_WINCE
288 in->win_atime[0] = oh->win_atime[0];
289 in->win_ctime[0] = oh->win_ctime[0];
290 in->win_mtime[0] = oh->win_mtime[0];
291 in->win_atime[1] = oh->win_atime[1];
292 in->win_ctime[1] = oh->win_ctime[1];
293 in->win_mtime[1] = oh->win_mtime[1];
295 in->yst_uid = oh->yst_uid;
296 in->yst_gid = oh->yst_gid;
297 in->yst_atime = oh->yst_atime;
298 in->yst_mtime = oh->yst_mtime;
299 in->yst_ctime = oh->yst_ctime;
300 in->yst_rdev = oh->yst_rdev;
302 in->hdr_chunk = chunk;
303 in->serial = tags.serial_number;
305 yaffs_set_obj_name_from_oh(in, oh);
308 /* directory stuff...
313 yaffs_find_or_create_by_number
314 (dev, oh->parent_obj_id,
315 YAFFS_OBJECT_TYPE_DIRECTORY);
318 if (parent && parent->variant_type ==
319 YAFFS_OBJECT_TYPE_UNKNOWN) {
320 /* Set up as a directory */
321 parent->variant_type =
322 YAFFS_OBJECT_TYPE_DIRECTORY;
323 YINIT_LIST_HEAD(&parent->variant.
326 } else if (!parent || parent->variant_type !=
327 YAFFS_OBJECT_TYPE_DIRECTORY) {
328 /* Hoosterman, another problem....
329 * We're trying to use a non-directory as a directory
334 ("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
336 parent = dev->lost_n_found;
339 yaffs_add_obj_to_dir(parent, in);
341 if (0 && (parent == dev->del_dir ||
342 parent == dev->unlinked_dir)) {
343 in->deleted = 1; /* If it is unlinked at start up then it wants deleting */
344 dev->n_deleted_files++;
346 /* Note re hardlinks.
347 * Since we might scan a hardlink before its equivalent object is scanned
348 * we put them all in a list.
349 * After scanning is complete, we should have all the objects, so we run through this
350 * list and fix up all the chains.
353 switch (in->variant_type) {
354 case YAFFS_OBJECT_TYPE_UNKNOWN:
355 /* Todo got a problem */
357 case YAFFS_OBJECT_TYPE_FILE:
358 if (dev->param.use_header_file_size)
360 in->variant.file_variant.
365 case YAFFS_OBJECT_TYPE_HARDLINK:
366 in->variant.hardlink_variant.
369 in->hard_links.next =
370 (struct ylist_head *)
374 case YAFFS_OBJECT_TYPE_DIRECTORY:
377 case YAFFS_OBJECT_TYPE_SPECIAL:
380 case YAFFS_OBJECT_TYPE_SYMLINK:
381 in->variant.symlink_variant.alias =
382 yaffs_clone_str(oh->alias);
383 if (!in->variant.symlink_variant.alias)
392 if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
393 /* If we got this far while scanning, then the block is fully allocated.*/
394 state = YAFFS_BLOCK_STATE_FULL;
397 if (state == YAFFS_BLOCK_STATE_ALLOCATING) {
398 /* If the block was partially allocated then treat it as fully allocated.*/
399 state = YAFFS_BLOCK_STATE_FULL;
400 dev->alloc_block = -1;
403 bi->block_state = state;
405 /* Now let's see if it was dirty */
406 if (bi->pages_in_use == 0 &&
407 !bi->has_shrink_hdr &&
408 bi->block_state == YAFFS_BLOCK_STATE_FULL) {
409 yaffs_block_became_dirty(dev, blk);
415 /* Ok, we've done all the scanning.
416 * Fix up the hard link chains.
417 * We should now have scanned all the objects, now it's time to add these
421 yaffs_link_fixup(dev, hard_list);
423 /* Fix up any shadowed objects */
425 struct yaffs_shadow_fixer_s *fixer;
428 while (shadow_fixers) {
429 fixer = shadow_fixers;
430 shadow_fixers = fixer->next;
431 /* Complete the rename transaction by deleting the shadowed object
432 * then setting the object header to unshadowed.
434 obj = yaffs_find_by_number(dev, fixer->shadowed_id);
438 obj = yaffs_find_by_number(dev, fixer->obj_id);
441 yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
447 yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
452 T(YAFFS_TRACE_SCAN, (TSTR("yaffs1_scan ends" TENDSTR)));