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_guts.h"
14 #include "yaffs_trace.h"
15 #include "yaffs_yaffs2.h"
16 #include "yaffs_checkptrw.h"
17 #include "yaffs_bitmap.h"
18 #include "yaffs_nand.h"
19 #include "yaffs_getblockinfo.h"
20 #include "yaffs_verify.h"
21 #include "yaffs_attribs.h"
22 #include "yaffs_summary.h"
23 #include "yaffs_endian.h"
26 * Checkpoints are really no benefit on very small partitions.
28 * To save space on small partitions don't bother with checkpoints unless
29 * the partition is at least this big.
31 #define YAFFS_CHECKPOINT_MIN_BLOCKS 60
32 #define YAFFS_SMALL_HOLE_THRESHOLD 4
35 * Oldest Dirty Sequence Number handling.
38 /* yaffs_calc_oldest_dirty_seq()
39 * yaffs2_find_oldest_dirty_seq()
40 * Calculate the oldest dirty sequence number if we don't know it.
42 void yaffs_calc_oldest_dirty_seq(struct yaffs_dev *dev)
46 unsigned block_no = 0;
47 struct yaffs_block_info *b;
49 if (!dev->param.is_yaffs2)
52 /* Find the oldest dirty sequence number. */
53 seq = dev->seq_number + 1;
55 for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
56 if (b->block_state == YAFFS_BLOCK_STATE_FULL &&
57 (u32)(b->pages_in_use - b->soft_del_pages) <
58 dev->param.chunks_per_block &&
59 b->seq_number < seq) {
67 dev->oldest_dirty_seq = seq;
68 dev->oldest_dirty_block = block_no;
72 void yaffs2_find_oldest_dirty_seq(struct yaffs_dev *dev)
74 if (!dev->param.is_yaffs2)
77 if (!dev->oldest_dirty_seq)
78 yaffs_calc_oldest_dirty_seq(dev);
82 * yaffs_clear_oldest_dirty_seq()
83 * Called when a block is erased or marked bad. (ie. when its seq_number
84 * becomes invalid). If the value matches the oldest then we clear
85 * dev->oldest_dirty_seq to force its recomputation.
87 void yaffs2_clear_oldest_dirty_seq(struct yaffs_dev *dev,
88 struct yaffs_block_info *bi)
91 if (!dev->param.is_yaffs2)
94 if (!bi || bi->seq_number == dev->oldest_dirty_seq) {
95 dev->oldest_dirty_seq = 0;
96 dev->oldest_dirty_block = 0;
101 * yaffs2_update_oldest_dirty_seq()
102 * Update the oldest dirty sequence number whenever we dirty a block.
103 * Only do this if the oldest_dirty_seq is actually being tracked.
105 void yaffs2_update_oldest_dirty_seq(struct yaffs_dev *dev, unsigned block_no,
106 struct yaffs_block_info *bi)
108 if (!dev->param.is_yaffs2)
111 if (dev->oldest_dirty_seq) {
112 if (dev->oldest_dirty_seq > bi->seq_number) {
113 dev->oldest_dirty_seq = bi->seq_number;
114 dev->oldest_dirty_block = block_no;
119 int yaffs_block_ok_for_gc(struct yaffs_dev *dev, struct yaffs_block_info *bi)
122 if (!dev->param.is_yaffs2)
123 return 1; /* disqualification only applies to yaffs2. */
125 if (!bi->has_shrink_hdr)
126 return 1; /* can gc */
128 yaffs2_find_oldest_dirty_seq(dev);
130 /* Can't do gc of this block if there are any blocks older than this
131 * one that have discarded pages.
133 return (bi->seq_number <= dev->oldest_dirty_seq);
137 * yaffs2_find_refresh_block()
138 * periodically finds the oldest full block by sequence number for refreshing.
141 u32 yaffs2_find_refresh_block(struct yaffs_dev *dev)
146 struct yaffs_block_info *bi;
148 if (!dev->param.is_yaffs2)
152 * If refresh period < 10 then refreshing is disabled.
154 if (dev->param.refresh_period < 10)
160 if (dev->refresh_skip > dev->param.refresh_period)
161 dev->refresh_skip = dev->param.refresh_period;
163 if (dev->refresh_skip > 0)
167 * Refresh skip is now zero.
168 * We'll do a refresh this time around....
169 * Update the refresh skip and find the oldest block.
171 dev->refresh_skip = dev->param.refresh_period;
172 dev->refresh_count++;
173 bi = dev->block_info;
174 for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
176 if (bi->block_state == YAFFS_BLOCK_STATE_FULL) {
178 if (oldest < 1 || bi->seq_number < oldest_seq) {
180 oldest_seq = bi->seq_number;
187 yaffs_trace(YAFFS_TRACE_GC,
188 "GC refresh count %d selected block %d with seq_number %d",
189 dev->refresh_count, oldest, oldest_seq);
195 int yaffs2_checkpt_required(struct yaffs_dev *dev)
199 if (!dev->param.is_yaffs2)
202 nblocks = dev->internal_end_block - dev->internal_start_block + 1;
204 return !dev->param.skip_checkpt_wr &&
205 !dev->read_only && (nblocks >= YAFFS_CHECKPOINT_MIN_BLOCKS);
208 int yaffs_calc_checkpt_blocks_required(struct yaffs_dev *dev)
215 if (!dev->param.is_yaffs2)
218 if (!dev->checkpoint_blocks_required && yaffs2_checkpt_required(dev)) {
219 /* Not a valid value so recalculate */
220 dev_blocks = dev->param.end_block - dev->param.start_block + 1;
221 n_bytes += sizeof(struct yaffs_checkpt_validity);
222 n_bytes += sizeof(struct yaffs_checkpt_dev);
223 n_bytes += dev_blocks * sizeof(struct yaffs_block_info);
224 n_bytes += dev_blocks * dev->chunk_bit_stride;
226 (sizeof(struct yaffs_checkpt_obj) + sizeof(u32)) *
228 n_bytes += (dev->tnode_size + sizeof(u32)) * dev->n_tnodes;
229 n_bytes += sizeof(struct yaffs_checkpt_validity);
230 n_bytes += sizeof(u32); /* checksum */
232 /* Round up and add 2 blocks to allow for some bad blocks,
237 (dev->data_bytes_per_chunk *
238 dev->param.chunks_per_block)) + 3;
240 dev->checkpoint_blocks_required = n_blocks;
243 retval = dev->checkpoint_blocks_required - dev->blocks_in_checkpt;
249 /*--------------------- Checkpointing --------------------*/
251 static void yaffs2_do_endian_validity_marker(struct yaffs_dev *dev,
252 struct yaffs_checkpt_validity *v)
255 if (!dev->swap_endian)
257 v->struct_type = swap_s32(v->struct_type);
258 v->magic = swap_u32(v->magic);
259 v->version = swap_u32(v->version);
260 v->head = swap_u32(v->head);
263 static int yaffs2_wr_checkpt_validity_marker(struct yaffs_dev *dev, int head)
265 struct yaffs_checkpt_validity cp;
267 memset(&cp, 0, sizeof(cp));
269 cp.struct_type = sizeof(cp);
270 cp.magic = YAFFS_MAGIC;
271 cp.version = YAFFS_CHECKPOINT_VERSION;
272 cp.head = (head) ? 1 : 0;
274 yaffs2_do_endian_validity_marker(dev, &cp);
276 return (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp)) ? 1 : 0;
279 static int yaffs2_rd_checkpt_validity_marker(struct yaffs_dev *dev, int head)
281 struct yaffs_checkpt_validity cp;
284 ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
285 yaffs2_do_endian_validity_marker(dev, &cp);
288 ok = (cp.struct_type == sizeof(cp)) &&
289 (cp.magic == YAFFS_MAGIC) &&
290 (cp.version == YAFFS_CHECKPOINT_VERSION) &&
291 (cp.head == ((head) ? 1 : 0));
295 static void yaffs2_dev_to_checkpt_dev(struct yaffs_checkpt_dev *cp,
296 struct yaffs_dev *dev)
298 cp->struct_type = sizeof(*cp);
300 cp->n_erased_blocks = dev->n_erased_blocks;
301 cp->alloc_block = dev->alloc_block;
302 cp->alloc_page = dev->alloc_page;
303 cp->n_free_chunks = dev->n_free_chunks;
305 cp->n_deleted_files = dev->n_deleted_files;
306 cp->n_unlinked_files = dev->n_unlinked_files;
307 cp->n_bg_deletions = dev->n_bg_deletions;
308 cp->seq_number = dev->seq_number;
312 static void yaffs_checkpt_dev_to_dev(struct yaffs_dev *dev,
313 struct yaffs_checkpt_dev *cp)
315 dev->n_erased_blocks = cp->n_erased_blocks;
316 dev->alloc_block = cp->alloc_block;
317 dev->alloc_page = cp->alloc_page;
318 dev->n_free_chunks = cp->n_free_chunks;
320 dev->n_deleted_files = cp->n_deleted_files;
321 dev->n_unlinked_files = cp->n_unlinked_files;
322 dev->n_bg_deletions = cp->n_bg_deletions;
323 dev->seq_number = cp->seq_number;
326 static void yaffs2_do_endian_checkpt_dev(struct yaffs_dev *dev,
327 struct yaffs_checkpt_dev *cp)
329 if (!dev->swap_endian)
331 cp->struct_type = swap_s32(cp->struct_type);
332 cp->n_erased_blocks = swap_s32(cp->n_erased_blocks);
333 cp->alloc_block = swap_s32(cp->alloc_block);
334 cp->alloc_page = swap_u32(cp->alloc_page);
335 cp->n_free_chunks = swap_s32(cp->n_free_chunks);
336 cp->n_deleted_files = swap_s32(cp->n_deleted_files);
337 cp->n_unlinked_files = swap_s32(cp->n_unlinked_files);
338 cp->n_bg_deletions = swap_s32(cp->n_bg_deletions);
341 static int yaffs2_wr_checkpt_dev(struct yaffs_dev *dev)
343 struct yaffs_checkpt_dev cp;
345 u32 n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
348 union yaffs_block_info_union bu;
350 /* Write device runtime values */
351 yaffs2_dev_to_checkpt_dev(&cp, dev);
352 yaffs2_do_endian_checkpt_dev(dev, &cp);
354 ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
358 /* Write block info. */
359 if (!dev->swap_endian) {
360 n_bytes = n_blocks * sizeof(struct yaffs_block_info);
361 ok = (yaffs2_checkpt_wr(dev, dev->block_info, n_bytes) ==
365 * Need to swap the endianisms. We can't do this in place
366 * since that would damage live data,
367 * so write one block info at a time using a copy.
369 for (i = 0; i < n_blocks && ok; i++) {
370 bu.bi = dev->block_info[i];
371 bu.as_u32[0] = swap_u32(bu.as_u32[0]);
372 bu.as_u32[1] = swap_u32(bu.as_u32[1]);
373 ok = (yaffs2_checkpt_wr(dev, &bu, sizeof(bu)) == sizeof(bu));
381 * Write chunk bits. Chunk bits are in bytes so
382 * no endian conversion is needed.
384 n_bytes = n_blocks * dev->chunk_bit_stride;
385 ok = (yaffs2_checkpt_wr(dev, dev->chunk_bits, n_bytes) ==
391 static int yaffs2_rd_checkpt_dev(struct yaffs_dev *dev)
393 struct yaffs_checkpt_dev cp;
396 (dev->internal_end_block - dev->internal_start_block + 1);
399 ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
402 yaffs2_do_endian_checkpt_dev(dev, &cp);
404 if (cp.struct_type != sizeof(cp))
407 yaffs_checkpt_dev_to_dev(dev, &cp);
409 n_bytes = n_blocks * sizeof(struct yaffs_block_info);
411 ok = (yaffs2_checkpt_rd(dev, dev->block_info, n_bytes) ==
417 if (dev->swap_endian) {
418 /* The block info can just be handled as a list of u32s. */
419 u32 *as_u32 = (u32 *) dev->block_info;
420 u32 n_u32s = n_bytes/sizeof(u32);
423 for (i=0; i < n_u32s; i++)
424 as_u32[i] = swap_u32(as_u32[i]);
427 n_bytes = n_blocks * dev->chunk_bit_stride;
429 ok = (yaffs2_checkpt_rd(dev, dev->chunk_bits, n_bytes) ==
437 static void yaffs2_checkpt_obj_bit_assign(struct yaffs_checkpt_obj *cp,
444 and_mask = ((1<<bit_width)-1) << bit_offset;
446 cp->bit_field &= ~and_mask;
447 cp->bit_field |= ((value << bit_offset) & and_mask);
450 static u32 yaffs2_checkpt_obj_bit_get(struct yaffs_checkpt_obj *cp,
456 and_mask = ((1<<bit_width)-1);
458 return (cp->bit_field >> bit_offset) & and_mask;
461 static void yaffs2_obj_checkpt_obj(struct yaffs_checkpt_obj *cp,
462 struct yaffs_obj *obj)
464 cp->obj_id = obj->obj_id;
465 cp->parent_id = (obj->parent) ? obj->parent->obj_id : 0;
466 cp->hdr_chunk = obj->hdr_chunk;
468 yaffs2_checkpt_obj_bit_assign(cp, CHECKPOINT_VARIANT_BITS, obj->variant_type);
469 yaffs2_checkpt_obj_bit_assign(cp, CHECKPOINT_DELETED_BITS, obj->deleted);
470 yaffs2_checkpt_obj_bit_assign(cp, CHECKPOINT_SOFT_DEL_BITS, obj->soft_del);
471 yaffs2_checkpt_obj_bit_assign(cp, CHECKPOINT_UNLINKED_BITS, obj->unlinked);
472 yaffs2_checkpt_obj_bit_assign(cp, CHECKPOINT_FAKE_BITS, obj->fake);
473 yaffs2_checkpt_obj_bit_assign(cp, CHECKPOINT_RENAME_ALLOWED_BITS, obj->rename_allowed);
474 yaffs2_checkpt_obj_bit_assign(cp, CHECKPOINT_UNLINK_ALLOWED_BITS, obj->unlink_allowed);
475 yaffs2_checkpt_obj_bit_assign(cp, CHECKPOINT_SERIAL_BITS, obj->serial);
477 cp->n_data_chunks = obj->n_data_chunks;
479 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
480 cp->size_or_equiv_obj = obj->variant.file_variant.file_size;
481 else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK)
482 cp->size_or_equiv_obj = obj->variant.hardlink_variant.equiv_id;
485 static int yaffs2_checkpt_obj_to_obj(struct yaffs_obj *obj,
486 struct yaffs_checkpt_obj *cp)
488 struct yaffs_obj *parent;
489 u32 cp_variant_type = yaffs2_checkpt_obj_bit_get(cp, CHECKPOINT_VARIANT_BITS);
491 if (obj->variant_type != cp_variant_type) {
492 yaffs_trace(YAFFS_TRACE_ERROR,
493 "Checkpoint read object %d type %d chunk %d does not match existing object type %d",
494 cp->obj_id, cp_variant_type, cp->hdr_chunk,
499 obj->obj_id = cp->obj_id;
502 parent = yaffs_find_or_create_by_number(obj->my_dev,
504 YAFFS_OBJECT_TYPE_DIRECTORY);
509 if (parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
510 yaffs_trace(YAFFS_TRACE_ALWAYS,
511 "Checkpoint read object %d parent %d type %d chunk %d Parent type, %d, not directory",
512 cp->obj_id, cp->parent_id,
513 cp_variant_type, cp->hdr_chunk,
514 parent->variant_type);
517 yaffs_add_obj_to_dir(parent, obj);
520 obj->hdr_chunk = cp->hdr_chunk;
522 obj->variant_type = yaffs2_checkpt_obj_bit_get(cp, CHECKPOINT_VARIANT_BITS);
523 obj->deleted = yaffs2_checkpt_obj_bit_get(cp, CHECKPOINT_DELETED_BITS);
524 obj->soft_del = yaffs2_checkpt_obj_bit_get(cp, CHECKPOINT_SOFT_DEL_BITS);
525 obj->unlinked = yaffs2_checkpt_obj_bit_get(cp, CHECKPOINT_UNLINKED_BITS);
526 obj->fake = yaffs2_checkpt_obj_bit_get(cp, CHECKPOINT_FAKE_BITS);
527 obj->rename_allowed = yaffs2_checkpt_obj_bit_get(cp, CHECKPOINT_RENAME_ALLOWED_BITS);
528 obj->unlink_allowed = yaffs2_checkpt_obj_bit_get(cp, CHECKPOINT_UNLINK_ALLOWED_BITS);
529 obj->serial = yaffs2_checkpt_obj_bit_get(cp, CHECKPOINT_SERIAL_BITS);
531 obj->n_data_chunks = cp->n_data_chunks;
533 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE) {
534 obj->variant.file_variant.file_size = cp->size_or_equiv_obj;
535 obj->variant.file_variant.stored_size = cp->size_or_equiv_obj;
536 } else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
537 obj->variant.hardlink_variant.equiv_id = cp->size_or_equiv_obj;
539 if (obj->hdr_chunk > 0)
540 obj->lazy_loaded = 1;
544 static void yaffs2_do_endian_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
547 u32 *as_u32 = (u32 *)tn;
548 int tnode_size_u32 = dev->tnode_size / sizeof(u32);
550 if (!dev->swap_endian)
552 /* Swap all the tnode data as u32s to fix endianisms. */
553 for (i = 0; i<tnode_size_u32; i++)
554 as_u32[i] = swap_u32(as_u32[i]);
557 struct yaffs_tnode *yaffs2_do_endian_tnode_copy(struct yaffs_dev *dev,
558 struct yaffs_tnode *tn)
560 if (!dev->swap_endian)
563 memcpy(dev->tn_swap_buffer, tn, dev->tnode_size);
564 tn = dev->tn_swap_buffer;
566 yaffs2_do_endian_tnode(dev, tn);
571 static int yaffs2_checkpt_tnode_worker(struct yaffs_obj *in,
572 struct yaffs_tnode *tn, u32 level,
576 struct yaffs_dev *dev = in->my_dev;
584 for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
585 if (!tn->internal[i])
587 ok = yaffs2_checkpt_tnode_worker(in,
591 YAFFS_TNODES_INTERNAL_BITS) + i);
597 base_offset = chunk_offset << YAFFS_TNODES_LEVEL0_BITS;
598 yaffs_do_endian_u32(dev, &base_offset);
600 ok = (yaffs2_checkpt_wr(dev, &base_offset, sizeof(base_offset)) ==
601 sizeof(base_offset));
604 * NB Can't do an in-place endian swizzle since that would
605 * damage current tnode data.
606 * If a tnode endian conversion is required we do a copy.
608 tn = yaffs2_do_endian_tnode_copy(dev, tn);
609 ok = (yaffs2_checkpt_wr(dev, tn, dev->tnode_size) ==
610 (int)dev->tnode_size);
615 static int yaffs2_wr_checkpt_tnodes(struct yaffs_obj *obj)
620 if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
623 ok = yaffs2_checkpt_tnode_worker(obj,
624 obj->variant.file_variant.top,
625 obj->variant.file_variant.
628 ok = (yaffs2_checkpt_wr(obj->my_dev, &end_marker,
629 sizeof(end_marker)) == sizeof(end_marker));
634 static int yaffs2_rd_checkpt_tnodes(struct yaffs_obj *obj)
638 struct yaffs_dev *dev = obj->my_dev;
639 struct yaffs_file_var *file_stuct_ptr = &obj->variant.file_variant;
640 struct yaffs_tnode *tn;
643 ok = (yaffs2_checkpt_rd(dev, &base_chunk, sizeof(base_chunk)) ==
646 yaffs_do_endian_u32(dev, &base_chunk);
648 while (ok && (~base_chunk)) {
650 /* Read level 0 tnode */
652 tn = yaffs_get_tnode(dev);
654 ok = (yaffs2_checkpt_rd(dev, tn, dev->tnode_size) ==
655 (int)dev->tnode_size);
656 yaffs2_do_endian_tnode(dev, tn);
662 ok = yaffs_add_find_tnode_0(dev,
664 base_chunk, tn) ? 1 : 0;
667 ok = (yaffs2_checkpt_rd
669 sizeof(base_chunk)) == sizeof(base_chunk));
670 yaffs_do_endian_u32(dev, &base_chunk);
675 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
676 "Checkpoint read tnodes %d records, last %d. ok %d",
677 nread, base_chunk, ok);
683 static void yaffs2_do_endian_checkpt_obj(struct yaffs_dev *dev,
684 struct yaffs_checkpt_obj *cp)
686 if (!dev->swap_endian)
688 cp->struct_type = swap_s32(cp->struct_type);
689 cp->obj_id = swap_u32(cp->obj_id);
690 cp->parent_id = swap_u32(cp->parent_id);
691 cp->hdr_chunk = swap_s32(cp->hdr_chunk);
692 cp->bit_field = swap_u32(cp->bit_field);
693 cp->n_data_chunks = swap_s32(cp->n_data_chunks);
694 cp->size_or_equiv_obj = swap_loff_t(cp->size_or_equiv_obj);
697 static int yaffs2_wr_checkpt_objs(struct yaffs_dev *dev)
699 struct yaffs_obj *obj;
700 struct yaffs_checkpt_obj cp;
703 struct list_head *lh;
706 /* Iterate through the objects in each hash entry,
707 * dumping them to the checkpointing stream.
710 (void) cp_variant_type;
712 for (i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++) {
713 list_for_each(lh, &dev->obj_bucket[i].list) {
714 obj = list_entry(lh, struct yaffs_obj, hash_link);
715 if (!obj->defered_free) {
716 yaffs2_obj_checkpt_obj(&cp, obj);
717 cp.struct_type = sizeof(cp);
718 cp_variant_type = yaffs2_checkpt_obj_bit_get(
719 &cp, CHECKPOINT_VARIANT_BITS);
720 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
721 "Checkpoint write object %d parent %d type %d chunk %d obj addr %p",
722 cp.obj_id, cp.parent_id,
723 cp_variant_type, cp.hdr_chunk, obj);
725 yaffs2_do_endian_checkpt_obj (dev, &cp);
726 ok = (yaffs2_checkpt_wr(dev, &cp,
727 sizeof(cp)) == sizeof(cp));
731 YAFFS_OBJECT_TYPE_FILE)
732 ok = yaffs2_wr_checkpt_tnodes(obj);
737 /* Dump end of list */
738 memset(&cp, 0xff, sizeof(struct yaffs_checkpt_obj));
739 cp.struct_type = sizeof(cp);
740 yaffs2_do_endian_checkpt_obj (dev, &cp);
743 ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
748 static int yaffs2_rd_checkpt_objs(struct yaffs_dev *dev)
750 struct yaffs_obj *obj;
751 struct yaffs_checkpt_obj cp;
755 LIST_HEAD(hard_list);
758 while (ok && !done) {
759 ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
760 yaffs2_do_endian_checkpt_obj (dev, &cp);
762 if (cp.struct_type != sizeof(cp)) {
763 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
764 "struct size %d instead of %d ok %d",
765 cp.struct_type, (int)sizeof(cp), ok);
769 cp_variant_type = yaffs2_checkpt_obj_bit_get(
770 &cp, CHECKPOINT_VARIANT_BITS);
771 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
772 "Checkpoint read object %d parent %d type %d chunk %d ",
773 cp.obj_id, cp.parent_id, cp_variant_type,
776 if (ok && cp.obj_id == (u32)(~0)) {
780 yaffs_find_or_create_by_number(dev, cp.obj_id,
783 ok = yaffs2_checkpt_obj_to_obj(obj, &cp);
786 if (obj->variant_type ==
787 YAFFS_OBJECT_TYPE_FILE) {
788 ok = yaffs2_rd_checkpt_tnodes(obj);
789 } else if (obj->variant_type ==
790 YAFFS_OBJECT_TYPE_HARDLINK) {
791 list_add(&obj->hard_links, &hard_list);
800 yaffs_link_fixup(dev, &hard_list);
805 static int yaffs2_wr_checkpt_sum(struct yaffs_dev *dev)
810 yaffs2_get_checkpt_sum(dev, &checkpt_sum);
812 yaffs_do_endian_u32(dev, &checkpt_sum);
814 ok = (yaffs2_checkpt_wr(dev, &checkpt_sum, sizeof(checkpt_sum)) ==
815 sizeof(checkpt_sum));
823 static int yaffs2_rd_checkpt_sum(struct yaffs_dev *dev)
829 yaffs2_get_checkpt_sum(dev, &checkpt_sum0);
831 ok = (yaffs2_checkpt_rd(dev, &checkpt_sum1, sizeof(checkpt_sum1)) ==
832 sizeof(checkpt_sum1));
836 yaffs_do_endian_u32(dev, &checkpt_sum1);
838 if (checkpt_sum0 != checkpt_sum1)
844 static int yaffs2_wr_checkpt_data(struct yaffs_dev *dev)
848 if (!yaffs2_checkpt_required(dev)) {
849 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
850 "skipping checkpoint write");
855 ok = yaffs2_checkpt_open(dev, 1);
858 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
859 "write checkpoint validity");
860 ok = yaffs2_wr_checkpt_validity_marker(dev, 1);
863 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
864 "write checkpoint device");
865 ok = yaffs2_wr_checkpt_dev(dev);
868 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
869 "write checkpoint objects");
870 ok = yaffs2_wr_checkpt_objs(dev);
873 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
874 "write checkpoint validity");
875 ok = yaffs2_wr_checkpt_validity_marker(dev, 0);
879 ok = yaffs2_wr_checkpt_sum(dev);
881 if (!yaffs_checkpt_close(dev))
885 dev->is_checkpointed = 1;
887 dev->is_checkpointed = 0;
889 return dev->is_checkpointed;
892 static int yaffs2_rd_checkpt_data(struct yaffs_dev *dev)
896 if (!dev->param.is_yaffs2)
899 if (ok && dev->param.skip_checkpt_rd) {
900 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
901 "skipping checkpoint read");
906 ok = yaffs2_checkpt_open(dev, 0); /* open for read */
909 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
910 "read checkpoint validity");
911 ok = yaffs2_rd_checkpt_validity_marker(dev, 1);
914 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
915 "read checkpoint device");
916 ok = yaffs2_rd_checkpt_dev(dev);
919 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
920 "read checkpoint objects");
921 ok = yaffs2_rd_checkpt_objs(dev);
924 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
925 "read checkpoint validity");
926 ok = yaffs2_rd_checkpt_validity_marker(dev, 0);
930 ok = yaffs2_rd_checkpt_sum(dev);
931 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
932 "read checkpoint checksum %d", ok);
935 if (!yaffs_checkpt_close(dev))
939 dev->is_checkpointed = 1;
941 dev->is_checkpointed = 0;
946 void yaffs2_checkpt_invalidate(struct yaffs_dev *dev)
948 if (dev->is_checkpointed || dev->blocks_in_checkpt > 0) {
949 dev->is_checkpointed = 0;
950 yaffs2_checkpt_invalidate_stream(dev);
952 if (dev->param.sb_dirty_fn)
953 dev->param.sb_dirty_fn(dev);
956 int yaffs_checkpoint_save(struct yaffs_dev *dev)
958 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
959 "save entry: is_checkpointed %d",
960 dev->is_checkpointed);
962 yaffs_verify_objects(dev);
963 yaffs_verify_blocks(dev);
964 yaffs_verify_free_chunks(dev);
966 if (!dev->is_checkpointed) {
967 yaffs2_checkpt_invalidate(dev);
968 yaffs2_wr_checkpt_data(dev);
971 yaffs_trace(YAFFS_TRACE_CHECKPOINT | YAFFS_TRACE_MOUNT,
972 "save exit: is_checkpointed %d",
973 dev->is_checkpointed);
975 return dev->is_checkpointed;
978 int yaffs2_checkpt_restore(struct yaffs_dev *dev)
982 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
983 "restore entry: is_checkpointed %d",
984 dev->is_checkpointed);
986 retval = yaffs2_rd_checkpt_data(dev);
988 if (dev->is_checkpointed) {
989 yaffs_verify_objects(dev);
990 yaffs_verify_blocks(dev);
991 yaffs_verify_free_chunks(dev);
994 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
995 "restore exit: is_checkpointed %d",
996 dev->is_checkpointed);
1001 /* End of checkpointing */
1003 /* Hole handling logic for truncate past end of file */
1005 int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size)
1007 /* if new_size > old_file_size.
1008 * We're going to be writing a hole.
1009 * If the hole is small then write zeros otherwise write a start
1012 loff_t old_file_size;
1015 int result = YAFFS_OK;
1016 struct yaffs_dev *dev = NULL;
1017 u8 *local_buffer = NULL;
1018 int small_increase_ok = 0;
1023 if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1028 /* Bail out if not yaffs2 mode */
1029 if (!dev->param.is_yaffs2)
1032 old_file_size = obj->variant.file_variant.file_size;
1034 if (new_size <= old_file_size)
1037 increase = new_size - old_file_size;
1039 if (increase < YAFFS_SMALL_HOLE_THRESHOLD * dev->data_bytes_per_chunk &&
1040 yaffs_check_alloc_available(dev, YAFFS_SMALL_HOLE_THRESHOLD + 1))
1046 local_buffer = yaffs_get_temp_buffer(dev);
1049 /* fill hole with zero bytes */
1050 loff_t pos = old_file_size;
1053 memset(local_buffer, 0, dev->data_bytes_per_chunk);
1054 small_increase_ok = 1;
1056 while (increase > 0 && small_increase_ok) {
1057 this_write = increase;
1058 if (this_write > (int)dev->data_bytes_per_chunk)
1059 this_write = dev->data_bytes_per_chunk;
1061 yaffs_do_file_wr(obj, local_buffer, pos, this_write,
1063 if (written == this_write) {
1065 increase -= this_write;
1067 small_increase_ok = 0;
1071 yaffs_release_temp_buffer(dev, local_buffer);
1073 /* If out of space then reverse any chunks we've added */
1074 if (!small_increase_ok)
1075 yaffs_resize_file_down(obj, old_file_size);
1078 if (!small_increase_ok &&
1080 obj->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
1081 obj->parent->obj_id != YAFFS_OBJECTID_DELETED) {
1082 /* Write a hole start header with the old file size */
1083 yaffs_update_oh(obj, NULL, 0, 1, 0, NULL);
1089 /* Yaffs2 scanning */
1091 struct yaffs_block_index {
1096 static int yaffs2_ybicmp(const void *a, const void *b)
1098 int aseq = ((struct yaffs_block_index *)a)->seq;
1099 int bseq = ((struct yaffs_block_index *)b)->seq;
1100 int ablock = ((struct yaffs_block_index *)a)->block;
1101 int bblock = ((struct yaffs_block_index *)b)->block;
1104 return ablock - bblock;
1109 static inline int yaffs2_scan_chunk(struct yaffs_dev *dev,
1110 struct yaffs_block_info *bi,
1111 int blk, int chunk_in_block,
1114 struct list_head *hard_list,
1115 int summary_available)
1117 struct yaffs_obj_hdr *oh;
1118 struct yaffs_obj *in;
1119 struct yaffs_obj *parent;
1124 struct yaffs_ext_tags tags;
1126 int alloc_failed = 0;
1127 int chunk = blk * dev->param.chunks_per_block + chunk_in_block;
1128 struct yaffs_file_var *file_var;
1129 struct yaffs_hardlink_var *hl_var;
1130 struct yaffs_symlink_var *sl_var;
1132 if (summary_available) {
1133 result = yaffs_summary_fetch(dev, &tags, chunk_in_block);
1134 tags.seq_number = bi->seq_number;
1137 if (!summary_available || tags.obj_id == 0) {
1138 result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL, &tags);
1141 dev->summary_used++;
1144 if (result == YAFFS_FAIL)
1145 yaffs_trace(YAFFS_TRACE_SCAN,
1146 "Could not get tags for chunk %d\n", chunk);
1147 /* Let's have a good look at this chunk... */
1149 if (!tags.chunk_used) {
1150 /* An unassigned chunk in the block.
1151 * If there are used chunks after this one, then
1152 * it is a chunk that was skipped due to failing
1153 * the erased check. Just skip it so that it can
1155 * But, more typically, We get here when this is
1156 * an unallocated chunk and his means that
1157 * either the block is empty or this is the one
1158 * being allocated from
1161 if (*found_chunks) {
1162 /* This is a chunk that was skipped due
1163 * to failing the erased check */
1164 } else if (chunk_in_block == 0) {
1165 /* We're looking at the first chunk in
1166 * the block so the block is unused */
1167 bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
1168 dev->n_erased_blocks++;
1170 if (bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN ||
1171 bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) {
1172 if (dev->seq_number == bi->seq_number) {
1173 /* Allocating from this block*/
1174 yaffs_trace(YAFFS_TRACE_SCAN,
1175 " Allocating from %d %d",
1176 blk, chunk_in_block);
1179 YAFFS_BLOCK_STATE_ALLOCATING;
1180 dev->alloc_block = blk;
1181 dev->alloc_page = chunk_in_block;
1182 dev->alloc_block_finder = blk;
1184 /* This is a partially written block
1185 * that is not the current
1188 yaffs_trace(YAFFS_TRACE_SCAN,
1189 "Partially written block %d detected. gc will fix this.",
1195 dev->n_free_chunks++;
1197 } else if (tags.ecc_result ==
1198 YAFFS_ECC_RESULT_UNFIXED) {
1199 yaffs_trace(YAFFS_TRACE_SCAN,
1200 " Unfixed ECC in chunk(%d:%d), chunk ignored",
1201 blk, chunk_in_block);
1202 dev->n_free_chunks++;
1203 } else if (tags.obj_id > YAFFS_MAX_OBJECT_ID ||
1204 tags.chunk_id > YAFFS_MAX_CHUNK_ID ||
1205 tags.obj_id == YAFFS_OBJECTID_SUMMARY ||
1206 (tags.chunk_id > 0 &&
1207 tags.n_bytes > dev->data_bytes_per_chunk) ||
1208 tags.seq_number != bi->seq_number) {
1209 yaffs_trace(YAFFS_TRACE_SCAN,
1210 "Chunk (%d:%d) with bad tags:obj = %d, chunk_id = %d, n_bytes = %d, ignored",
1211 blk, chunk_in_block, tags.obj_id,
1212 tags.chunk_id, tags.n_bytes);
1213 dev->n_free_chunks++;
1214 } else if (tags.chunk_id > 0) {
1215 /* chunk_id > 0 so it is a data chunk... */
1217 loff_t chunk_base = (tags.chunk_id - 1) *
1218 dev->data_bytes_per_chunk;
1222 yaffs_set_chunk_bit(dev, blk, chunk_in_block);
1225 in = yaffs_find_or_create_by_number(dev,
1227 YAFFS_OBJECT_TYPE_FILE);
1233 in->variant_type == YAFFS_OBJECT_TYPE_FILE &&
1234 chunk_base < in->variant.file_variant.shrink_size) {
1235 /* This has not been invalidated by
1237 if (!yaffs_put_chunk_in_file(in, tags.chunk_id,
1241 /* File size is calculated by looking at
1242 * the data chunks if we have not
1243 * seen an object header yet.
1244 * Stop this practice once we find an
1247 endpos = chunk_base + tags.n_bytes;
1250 in->variant.file_variant.stored_size < endpos) {
1251 in->variant.file_variant.
1252 stored_size = endpos;
1253 in->variant.file_variant.
1257 /* This chunk has been invalidated by a
1258 * resize, or a past file deletion
1259 * so delete the chunk*/
1260 yaffs_chunk_del(dev, chunk, 1, __LINE__);
1263 /* chunk_id == 0, so it is an ObjectHeader.
1264 * Thus, we read in the object header and make
1269 yaffs_set_chunk_bit(dev, blk, chunk_in_block);
1275 if (tags.extra_available) {
1276 in = yaffs_find_or_create_by_number(dev,
1278 tags.extra_obj_type);
1284 (!in->valid && dev->param.disable_lazy_load) ||
1285 tags.extra_shadows ||
1286 (!in->valid && (tags.obj_id == YAFFS_OBJECTID_ROOT ||
1287 tags.obj_id == YAFFS_OBJECTID_LOSTNFOUND))) {
1289 /* If we don't have valid info then we
1290 * need to read the chunk
1291 * TODO In future we can probably defer
1292 * reading the chunk and living with
1293 * invalid data until needed.
1296 result = yaffs_rd_chunk_tags_nand(dev,
1301 oh = (struct yaffs_obj_hdr *)chunk_data;
1303 yaffs_do_endian_oh(dev, oh);
1305 if (dev->param.inband_tags) {
1306 /* Fix up the header if they got
1307 * corrupted by inband tags */
1309 oh->inband_shadowed_obj_id;
1311 oh->inband_is_shrink;
1315 in = yaffs_find_or_create_by_number(dev,
1316 tags.obj_id, oh->type);
1323 /* TODO Hoosterman we have a problem! */
1324 yaffs_trace(YAFFS_TRACE_ERROR,
1325 "yaffs tragedy: Could not make object for object %d at chunk %d during scan",
1326 tags.obj_id, chunk);
1331 /* We have already filled this one.
1332 * We have a duplicate that will be
1333 * discarded, but we first have to suck
1334 * out resize info if it is a file.
1336 if ((in->variant_type == YAFFS_OBJECT_TYPE_FILE) &&
1337 ((oh && oh->type == YAFFS_OBJECT_TYPE_FILE) ||
1338 (tags.extra_available &&
1339 tags.extra_obj_type == YAFFS_OBJECT_TYPE_FILE)
1341 loff_t this_size = (oh) ?
1342 yaffs_oh_to_size(dev, oh, 0) :
1343 tags.extra_file_size;
1344 u32 parent_obj_id = (oh) ?
1345 (u32)oh->parent_obj_id :
1346 tags.extra_parent_id;
1350 tags.extra_is_shrink;
1352 /* If it is deleted (unlinked
1353 * at start also means deleted)
1354 * we treat the file size as
1355 * being zeroed at this point.
1357 if (parent_obj_id == YAFFS_OBJECTID_DELETED ||
1358 parent_obj_id == YAFFS_OBJECTID_UNLINKED) {
1364 in->variant.file_variant.shrink_size >
1366 in->variant.file_variant.shrink_size =
1370 bi->has_shrink_hdr = 1;
1372 /* Use existing - destroy this one. */
1373 yaffs_chunk_del(dev, chunk, 1, __LINE__);
1376 if (!in->valid && in->variant_type !=
1377 (oh ? oh->type : tags.extra_obj_type)) {
1378 yaffs_trace(YAFFS_TRACE_ERROR,
1379 "yaffs tragedy: Bad type, %d != %d, for object %d at chunk %d during scan",
1380 oh ? oh->type : tags.extra_obj_type,
1381 in->variant_type, tags.obj_id,
1383 in = yaffs_retype_obj(in, oh ? oh->type : tags.extra_obj_type);
1387 (tags.obj_id == YAFFS_OBJECTID_ROOT ||
1388 tags.obj_id == YAFFS_OBJECTID_LOSTNFOUND)) {
1389 /* We only load some info, don't fiddle
1390 * with directory structure */
1394 in->yst_mode = oh->yst_mode;
1395 yaffs_load_attribs(in, oh);
1396 in->lazy_loaded = 0;
1398 in->lazy_loaded = 1;
1400 in->hdr_chunk = chunk;
1402 } else if (!in->valid) {
1403 /* we need to load this info */
1405 in->hdr_chunk = chunk;
1407 in->variant_type = oh->type;
1408 in->yst_mode = oh->yst_mode;
1409 yaffs_load_attribs(in, oh);
1411 if (oh->shadows_obj > 0)
1412 yaffs_handle_shadowed_obj(dev,
1413 oh->shadows_obj, 1);
1415 yaffs_set_obj_name_from_oh(in, oh);
1416 parent = yaffs_find_or_create_by_number(dev,
1418 YAFFS_OBJECT_TYPE_DIRECTORY);
1419 file_size = yaffs_oh_to_size(dev, oh, 0);
1420 is_shrink = oh->is_shrink;
1421 equiv_id = oh->equiv_id;
1423 in->variant_type = tags.extra_obj_type;
1424 parent = yaffs_find_or_create_by_number(dev,
1425 tags.extra_parent_id,
1426 YAFFS_OBJECT_TYPE_DIRECTORY);
1427 file_size = tags.extra_file_size;
1428 is_shrink = tags.extra_is_shrink;
1429 equiv_id = tags.extra_equiv_id;
1430 in->lazy_loaded = 1;
1437 /* directory stuff...
1442 parent->variant_type == YAFFS_OBJECT_TYPE_UNKNOWN) {
1443 /* Set up as a directory */
1444 parent->variant_type =
1445 YAFFS_OBJECT_TYPE_DIRECTORY;
1446 INIT_LIST_HEAD(&parent->
1447 variant.dir_variant.children);
1448 } else if (!parent ||
1449 parent->variant_type !=
1450 YAFFS_OBJECT_TYPE_DIRECTORY) {
1451 /* Hoosterman, another problem....
1452 * Trying to use a non-directory as a directory
1455 yaffs_trace(YAFFS_TRACE_ERROR,
1456 "yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
1458 parent = dev->lost_n_found;
1460 yaffs_add_obj_to_dir(parent, in);
1462 is_unlinked = (parent == dev->del_dir) ||
1463 (parent == dev->unlinked_dir);
1466 /* Mark the block */
1467 bi->has_shrink_hdr = 1;
1469 /* Note re hardlinks.
1470 * Since we might scan a hardlink before its equivalent
1471 * object is scanned we put them all in a list.
1472 * After scanning is complete, we should have all the
1473 * objects, so we run through this list and fix up all
1477 switch (in->variant_type) {
1478 case YAFFS_OBJECT_TYPE_UNKNOWN:
1479 /* Todo got a problem */
1481 case YAFFS_OBJECT_TYPE_FILE:
1482 file_var = &in->variant.file_variant;
1483 if (file_var->stored_size < file_size) {
1484 /* This covers the case where the file
1485 * size is greater than the data held.
1486 * This will happen if the file is
1487 * resized to be larger than its
1488 * current data extents.
1490 file_var->file_size = file_size;
1491 file_var->stored_size = file_size;
1494 if (file_var->shrink_size > file_size)
1495 file_var->shrink_size = file_size;
1498 case YAFFS_OBJECT_TYPE_HARDLINK:
1499 hl_var = &in->variant.hardlink_variant;
1501 hl_var->equiv_id = equiv_id;
1502 list_add(&in->hard_links, hard_list);
1505 case YAFFS_OBJECT_TYPE_DIRECTORY:
1508 case YAFFS_OBJECT_TYPE_SPECIAL:
1511 case YAFFS_OBJECT_TYPE_SYMLINK:
1512 sl_var = &in->variant.symlink_variant;
1515 yaffs_clone_str(oh->alias);
1523 return alloc_failed ? YAFFS_FAIL : YAFFS_OK;
1526 int yaffs2_scan_backwards(struct yaffs_dev *dev)
1533 enum yaffs_block_state state;
1535 LIST_HEAD(hard_list);
1536 struct yaffs_block_info *bi;
1538 int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
1541 int alloc_failed = 0;
1542 struct yaffs_block_index *block_index = NULL;
1543 int alt_block_index = 0;
1544 int summary_available;
1546 yaffs_trace(YAFFS_TRACE_SCAN,
1547 "yaffs2_scan_backwards starts intstartblk %d intendblk %d...",
1548 dev->internal_start_block, dev->internal_end_block);
1550 dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
1553 kmalloc(n_blocks * sizeof(struct yaffs_block_index), GFP_NOFS);
1557 vmalloc(n_blocks * sizeof(struct yaffs_block_index));
1558 alt_block_index = 1;
1562 yaffs_trace(YAFFS_TRACE_SCAN,
1563 "yaffs2_scan_backwards() could not allocate block index!"
1568 dev->blocks_in_checkpt = 0;
1570 chunk_data = yaffs_get_temp_buffer(dev);
1572 /* Scan all the blocks to determine their state */
1573 bi = dev->block_info;
1574 for (blk = dev->internal_start_block; blk <= dev->internal_end_block;
1576 yaffs_clear_chunk_bits(dev, blk);
1577 bi->pages_in_use = 0;
1578 bi->soft_del_pages = 0;
1580 yaffs_query_init_block_state(dev, blk, &state, &seq_number);
1582 bi->block_state = state;
1583 bi->seq_number = seq_number;
1585 if (bi->seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA)
1586 bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
1587 if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
1588 bi->block_state = YAFFS_BLOCK_STATE_DEAD;
1590 yaffs_trace(YAFFS_TRACE_SCAN_DEBUG,
1591 "Block scanning block %d state %d seq %d",
1592 blk, bi->block_state, seq_number);
1594 if (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT) {
1595 dev->blocks_in_checkpt++;
1597 } else if (bi->block_state == YAFFS_BLOCK_STATE_DEAD) {
1598 yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
1599 "block %d is bad", blk);
1600 } else if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
1601 yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "Block empty ");
1602 dev->n_erased_blocks++;
1603 dev->n_free_chunks += dev->param.chunks_per_block;
1604 } else if (bi->block_state ==
1605 YAFFS_BLOCK_STATE_NEEDS_SCAN) {
1606 /* Determine the highest sequence number */
1607 if (seq_number >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
1608 seq_number < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
1609 block_index[n_to_scan].seq = seq_number;
1610 block_index[n_to_scan].block = blk;
1612 if (seq_number >= dev->seq_number)
1613 dev->seq_number = seq_number;
1615 /* TODO: Nasty sequence number! */
1616 yaffs_trace(YAFFS_TRACE_SCAN,
1617 "Block scanning block %d has bad sequence number %d",
1624 yaffs_trace(YAFFS_TRACE_ALWAYS, "%d blocks to be sorted...", n_to_scan);
1628 /* Sort the blocks by sequence number */
1629 sort(block_index, n_to_scan, sizeof(struct yaffs_block_index),
1630 yaffs2_ybicmp, NULL);
1634 yaffs_trace(YAFFS_TRACE_SCAN, "...done");
1636 /* Now scan the blocks looking at the data. */
1638 end_iter = n_to_scan - 1;
1639 yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "%d blocks to scan", n_to_scan);
1641 /* For each block.... backwards */
1642 for (block_iter = end_iter;
1643 !alloc_failed && block_iter >= start_iter;
1645 /* Cooperative multitasking! This loop can run for so
1646 long that watchdog timers expire. */
1649 /* get the block to scan in the correct order */
1650 blk = block_index[block_iter].block;
1651 bi = yaffs_get_block_info(dev, blk);
1653 summary_available = yaffs_summary_read(dev, dev->sum_tags, blk);
1655 /* For each chunk in each block that needs scanning.... */
1657 if (summary_available)
1658 c = dev->chunks_per_summary - 1;
1660 c = dev->param.chunks_per_block - 1;
1662 for (/* c is already initialised */;
1663 !alloc_failed && c >= 0 &&
1664 (bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN ||
1665 bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING);
1667 /* Scan backwards...
1668 * Read the tags and decide what to do
1670 if (yaffs2_scan_chunk(dev, bi, blk, c,
1671 &found_chunks, chunk_data,
1672 &hard_list, summary_available) ==
1677 if (bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN) {
1678 /* If we got this far while scanning, then the block
1679 * is fully allocated. */
1680 bi->block_state = YAFFS_BLOCK_STATE_FULL;
1683 /* Now let's see if it was dirty */
1684 if (bi->pages_in_use == 0 &&
1685 !bi->has_shrink_hdr &&
1686 bi->block_state == YAFFS_BLOCK_STATE_FULL) {
1687 yaffs_block_became_dirty(dev, blk);
1691 yaffs_skip_rest_of_block(dev);
1693 if (alt_block_index)
1698 /* Ok, we've done all the scanning.
1699 * Fix up the hard link chains.
1700 * We have scanned all the objects, now it's time to add these
1703 yaffs_link_fixup(dev, &hard_list);
1705 yaffs_release_temp_buffer(dev, chunk_data);
1710 yaffs_trace(YAFFS_TRACE_SCAN, "yaffs2_scan_backwards ends");