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 for (i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++) {
711 list_for_each(lh, &dev->obj_bucket[i].list) {
712 obj = list_entry(lh, struct yaffs_obj, hash_link);
713 if (!obj->defered_free) {
714 yaffs2_obj_checkpt_obj(&cp, obj);
715 cp.struct_type = sizeof(cp);
716 cp_variant_type = yaffs2_checkpt_obj_bit_get(
717 &cp, CHECKPOINT_VARIANT_BITS);
718 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
719 "Checkpoint write object %d parent %d type %d chunk %d obj addr %p",
720 cp.obj_id, cp.parent_id,
721 cp_variant_type, cp.hdr_chunk, obj);
723 yaffs2_do_endian_checkpt_obj (dev, &cp);
724 ok = (yaffs2_checkpt_wr(dev, &cp,
725 sizeof(cp)) == sizeof(cp));
729 YAFFS_OBJECT_TYPE_FILE)
730 ok = yaffs2_wr_checkpt_tnodes(obj);
735 /* Dump end of list */
736 memset(&cp, 0xff, sizeof(struct yaffs_checkpt_obj));
737 cp.struct_type = sizeof(cp);
738 yaffs2_do_endian_checkpt_obj (dev, &cp);
741 ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
746 static int yaffs2_rd_checkpt_objs(struct yaffs_dev *dev)
748 struct yaffs_obj *obj;
749 struct yaffs_checkpt_obj cp;
753 LIST_HEAD(hard_list);
756 while (ok && !done) {
757 ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
758 yaffs2_do_endian_checkpt_obj (dev, &cp);
760 if (cp.struct_type != sizeof(cp)) {
761 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
762 "struct size %d instead of %d ok %d",
763 cp.struct_type, (int)sizeof(cp), ok);
767 cp_variant_type = yaffs2_checkpt_obj_bit_get(
768 &cp, CHECKPOINT_VARIANT_BITS);
769 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
770 "Checkpoint read object %d parent %d type %d chunk %d ",
771 cp.obj_id, cp.parent_id, cp_variant_type,
774 if (ok && cp.obj_id == (u32)(~0)) {
778 yaffs_find_or_create_by_number(dev, cp.obj_id,
781 ok = yaffs2_checkpt_obj_to_obj(obj, &cp);
784 if (obj->variant_type ==
785 YAFFS_OBJECT_TYPE_FILE) {
786 ok = yaffs2_rd_checkpt_tnodes(obj);
787 } else if (obj->variant_type ==
788 YAFFS_OBJECT_TYPE_HARDLINK) {
789 list_add(&obj->hard_links, &hard_list);
798 yaffs_link_fixup(dev, &hard_list);
803 static int yaffs2_wr_checkpt_sum(struct yaffs_dev *dev)
808 yaffs2_get_checkpt_sum(dev, &checkpt_sum);
810 yaffs_do_endian_u32(dev, &checkpt_sum);
812 ok = (yaffs2_checkpt_wr(dev, &checkpt_sum, sizeof(checkpt_sum)) ==
813 sizeof(checkpt_sum));
821 static int yaffs2_rd_checkpt_sum(struct yaffs_dev *dev)
827 yaffs2_get_checkpt_sum(dev, &checkpt_sum0);
829 ok = (yaffs2_checkpt_rd(dev, &checkpt_sum1, sizeof(checkpt_sum1)) ==
830 sizeof(checkpt_sum1));
834 yaffs_do_endian_u32(dev, &checkpt_sum1);
836 if (checkpt_sum0 != checkpt_sum1)
842 static int yaffs2_wr_checkpt_data(struct yaffs_dev *dev)
846 if (!yaffs2_checkpt_required(dev)) {
847 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
848 "skipping checkpoint write");
853 ok = yaffs2_checkpt_open(dev, 1);
856 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
857 "write checkpoint validity");
858 ok = yaffs2_wr_checkpt_validity_marker(dev, 1);
861 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
862 "write checkpoint device");
863 ok = yaffs2_wr_checkpt_dev(dev);
866 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
867 "write checkpoint objects");
868 ok = yaffs2_wr_checkpt_objs(dev);
871 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
872 "write checkpoint validity");
873 ok = yaffs2_wr_checkpt_validity_marker(dev, 0);
877 ok = yaffs2_wr_checkpt_sum(dev);
879 if (!yaffs_checkpt_close(dev))
883 dev->is_checkpointed = 1;
885 dev->is_checkpointed = 0;
887 return dev->is_checkpointed;
890 static int yaffs2_rd_checkpt_data(struct yaffs_dev *dev)
894 if (!dev->param.is_yaffs2)
897 if (ok && dev->param.skip_checkpt_rd) {
898 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
899 "skipping checkpoint read");
904 ok = yaffs2_checkpt_open(dev, 0); /* open for read */
907 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
908 "read checkpoint validity");
909 ok = yaffs2_rd_checkpt_validity_marker(dev, 1);
912 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
913 "read checkpoint device");
914 ok = yaffs2_rd_checkpt_dev(dev);
917 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
918 "read checkpoint objects");
919 ok = yaffs2_rd_checkpt_objs(dev);
922 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
923 "read checkpoint validity");
924 ok = yaffs2_rd_checkpt_validity_marker(dev, 0);
928 ok = yaffs2_rd_checkpt_sum(dev);
929 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
930 "read checkpoint checksum %d", ok);
933 if (!yaffs_checkpt_close(dev))
937 dev->is_checkpointed = 1;
939 dev->is_checkpointed = 0;
944 void yaffs2_checkpt_invalidate(struct yaffs_dev *dev)
946 if (dev->is_checkpointed || dev->blocks_in_checkpt > 0) {
947 dev->is_checkpointed = 0;
948 yaffs2_checkpt_invalidate_stream(dev);
950 if (dev->param.sb_dirty_fn)
951 dev->param.sb_dirty_fn(dev);
954 int yaffs_checkpoint_save(struct yaffs_dev *dev)
956 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
957 "save entry: is_checkpointed %d",
958 dev->is_checkpointed);
960 yaffs_verify_objects(dev);
961 yaffs_verify_blocks(dev);
962 yaffs_verify_free_chunks(dev);
964 if (!dev->is_checkpointed) {
965 yaffs2_checkpt_invalidate(dev);
966 yaffs2_wr_checkpt_data(dev);
969 yaffs_trace(YAFFS_TRACE_CHECKPOINT | YAFFS_TRACE_MOUNT,
970 "save exit: is_checkpointed %d",
971 dev->is_checkpointed);
973 return dev->is_checkpointed;
976 int yaffs2_checkpt_restore(struct yaffs_dev *dev)
980 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
981 "restore entry: is_checkpointed %d",
982 dev->is_checkpointed);
984 retval = yaffs2_rd_checkpt_data(dev);
986 if (dev->is_checkpointed) {
987 yaffs_verify_objects(dev);
988 yaffs_verify_blocks(dev);
989 yaffs_verify_free_chunks(dev);
992 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
993 "restore exit: is_checkpointed %d",
994 dev->is_checkpointed);
999 /* End of checkpointing */
1001 /* Hole handling logic for truncate past end of file */
1003 int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size)
1005 /* if new_size > old_file_size.
1006 * We're going to be writing a hole.
1007 * If the hole is small then write zeros otherwise write a start
1010 loff_t old_file_size;
1013 int result = YAFFS_OK;
1014 struct yaffs_dev *dev = NULL;
1015 u8 *local_buffer = NULL;
1016 int small_increase_ok = 0;
1021 if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1026 /* Bail out if not yaffs2 mode */
1027 if (!dev->param.is_yaffs2)
1030 old_file_size = obj->variant.file_variant.file_size;
1032 if (new_size <= old_file_size)
1035 increase = new_size - old_file_size;
1037 if (increase < YAFFS_SMALL_HOLE_THRESHOLD * dev->data_bytes_per_chunk &&
1038 yaffs_check_alloc_available(dev, YAFFS_SMALL_HOLE_THRESHOLD + 1))
1044 local_buffer = yaffs_get_temp_buffer(dev);
1047 /* fill hole with zero bytes */
1048 loff_t pos = old_file_size;
1051 memset(local_buffer, 0, dev->data_bytes_per_chunk);
1052 small_increase_ok = 1;
1054 while (increase > 0 && small_increase_ok) {
1055 this_write = increase;
1056 if (this_write > (int)dev->data_bytes_per_chunk)
1057 this_write = dev->data_bytes_per_chunk;
1059 yaffs_do_file_wr(obj, local_buffer, pos, this_write,
1061 if (written == this_write) {
1063 increase -= this_write;
1065 small_increase_ok = 0;
1069 yaffs_release_temp_buffer(dev, local_buffer);
1071 /* If out of space then reverse any chunks we've added */
1072 if (!small_increase_ok)
1073 yaffs_resize_file_down(obj, old_file_size);
1076 if (!small_increase_ok &&
1078 obj->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
1079 obj->parent->obj_id != YAFFS_OBJECTID_DELETED) {
1080 /* Write a hole start header with the old file size */
1081 yaffs_update_oh(obj, NULL, 0, 1, 0, NULL);
1087 /* Yaffs2 scanning */
1089 struct yaffs_block_index {
1094 static int yaffs2_ybicmp(const void *a, const void *b)
1096 int aseq = ((struct yaffs_block_index *)a)->seq;
1097 int bseq = ((struct yaffs_block_index *)b)->seq;
1098 int ablock = ((struct yaffs_block_index *)a)->block;
1099 int bblock = ((struct yaffs_block_index *)b)->block;
1102 return ablock - bblock;
1107 static inline int yaffs2_scan_chunk(struct yaffs_dev *dev,
1108 struct yaffs_block_info *bi,
1109 int blk, int chunk_in_block,
1112 struct list_head *hard_list,
1113 int summary_available)
1115 struct yaffs_obj_hdr *oh;
1116 struct yaffs_obj *in;
1117 struct yaffs_obj *parent;
1122 struct yaffs_ext_tags tags;
1124 int alloc_failed = 0;
1125 int chunk = blk * dev->param.chunks_per_block + chunk_in_block;
1126 struct yaffs_file_var *file_var;
1127 struct yaffs_hardlink_var *hl_var;
1128 struct yaffs_symlink_var *sl_var;
1130 if (summary_available) {
1131 result = yaffs_summary_fetch(dev, &tags, chunk_in_block);
1132 tags.seq_number = bi->seq_number;
1135 if (!summary_available || tags.obj_id == 0) {
1136 result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL, &tags);
1139 dev->summary_used++;
1142 if (result == YAFFS_FAIL)
1143 yaffs_trace(YAFFS_TRACE_SCAN,
1144 "Could not get tags for chunk %d\n", chunk);
1145 /* Let's have a good look at this chunk... */
1147 if (!tags.chunk_used) {
1148 /* An unassigned chunk in the block.
1149 * If there are used chunks after this one, then
1150 * it is a chunk that was skipped due to failing
1151 * the erased check. Just skip it so that it can
1153 * But, more typically, We get here when this is
1154 * an unallocated chunk and his means that
1155 * either the block is empty or this is the one
1156 * being allocated from
1159 if (*found_chunks) {
1160 /* This is a chunk that was skipped due
1161 * to failing the erased check */
1162 } else if (chunk_in_block == 0) {
1163 /* We're looking at the first chunk in
1164 * the block so the block is unused */
1165 bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
1166 dev->n_erased_blocks++;
1168 if (bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN ||
1169 bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) {
1170 if (dev->seq_number == bi->seq_number) {
1171 /* Allocating from this block*/
1172 yaffs_trace(YAFFS_TRACE_SCAN,
1173 " Allocating from %d %d",
1174 blk, chunk_in_block);
1177 YAFFS_BLOCK_STATE_ALLOCATING;
1178 dev->alloc_block = blk;
1179 dev->alloc_page = chunk_in_block;
1180 dev->alloc_block_finder = blk;
1182 /* This is a partially written block
1183 * that is not the current
1186 yaffs_trace(YAFFS_TRACE_SCAN,
1187 "Partially written block %d detected. gc will fix this.",
1193 dev->n_free_chunks++;
1195 } else if (tags.ecc_result ==
1196 YAFFS_ECC_RESULT_UNFIXED) {
1197 yaffs_trace(YAFFS_TRACE_SCAN,
1198 " Unfixed ECC in chunk(%d:%d), chunk ignored",
1199 blk, chunk_in_block);
1200 dev->n_free_chunks++;
1201 } else if (tags.obj_id > YAFFS_MAX_OBJECT_ID ||
1202 tags.chunk_id > YAFFS_MAX_CHUNK_ID ||
1203 tags.obj_id == YAFFS_OBJECTID_SUMMARY ||
1204 (tags.chunk_id > 0 &&
1205 tags.n_bytes > dev->data_bytes_per_chunk) ||
1206 tags.seq_number != bi->seq_number) {
1207 yaffs_trace(YAFFS_TRACE_SCAN,
1208 "Chunk (%d:%d) with bad tags:obj = %d, chunk_id = %d, n_bytes = %d, ignored",
1209 blk, chunk_in_block, tags.obj_id,
1210 tags.chunk_id, tags.n_bytes);
1211 dev->n_free_chunks++;
1212 } else if (tags.chunk_id > 0) {
1213 /* chunk_id > 0 so it is a data chunk... */
1215 loff_t chunk_base = (tags.chunk_id - 1) *
1216 dev->data_bytes_per_chunk;
1220 yaffs_set_chunk_bit(dev, blk, chunk_in_block);
1223 in = yaffs_find_or_create_by_number(dev,
1225 YAFFS_OBJECT_TYPE_FILE);
1231 in->variant_type == YAFFS_OBJECT_TYPE_FILE &&
1232 chunk_base < in->variant.file_variant.shrink_size) {
1233 /* This has not been invalidated by
1235 if (!yaffs_put_chunk_in_file(in, tags.chunk_id,
1239 /* File size is calculated by looking at
1240 * the data chunks if we have not
1241 * seen an object header yet.
1242 * Stop this practice once we find an
1245 endpos = chunk_base + tags.n_bytes;
1248 in->variant.file_variant.stored_size < endpos) {
1249 in->variant.file_variant.
1250 stored_size = endpos;
1251 in->variant.file_variant.
1255 /* This chunk has been invalidated by a
1256 * resize, or a past file deletion
1257 * so delete the chunk*/
1258 yaffs_chunk_del(dev, chunk, 1, __LINE__);
1261 /* chunk_id == 0, so it is an ObjectHeader.
1262 * Thus, we read in the object header and make
1267 yaffs_set_chunk_bit(dev, blk, chunk_in_block);
1273 if (tags.extra_available) {
1274 in = yaffs_find_or_create_by_number(dev,
1276 tags.extra_obj_type);
1282 (!in->valid && dev->param.disable_lazy_load) ||
1283 tags.extra_shadows ||
1284 (!in->valid && (tags.obj_id == YAFFS_OBJECTID_ROOT ||
1285 tags.obj_id == YAFFS_OBJECTID_LOSTNFOUND))) {
1287 /* If we don't have valid info then we
1288 * need to read the chunk
1289 * TODO In future we can probably defer
1290 * reading the chunk and living with
1291 * invalid data until needed.
1294 result = yaffs_rd_chunk_tags_nand(dev,
1299 oh = (struct yaffs_obj_hdr *)chunk_data;
1301 yaffs_do_endian_oh(dev, oh);
1303 if (dev->param.inband_tags) {
1304 /* Fix up the header if they got
1305 * corrupted by inband tags */
1307 oh->inband_shadowed_obj_id;
1309 oh->inband_is_shrink;
1313 in = yaffs_find_or_create_by_number(dev,
1314 tags.obj_id, oh->type);
1321 /* TODO Hoosterman we have a problem! */
1322 yaffs_trace(YAFFS_TRACE_ERROR,
1323 "yaffs tragedy: Could not make object for object %d at chunk %d during scan",
1324 tags.obj_id, chunk);
1329 /* We have already filled this one.
1330 * We have a duplicate that will be
1331 * discarded, but we first have to suck
1332 * out resize info if it is a file.
1334 if ((in->variant_type == YAFFS_OBJECT_TYPE_FILE) &&
1335 ((oh && oh->type == YAFFS_OBJECT_TYPE_FILE) ||
1336 (tags.extra_available &&
1337 tags.extra_obj_type == YAFFS_OBJECT_TYPE_FILE)
1339 loff_t this_size = (oh) ?
1340 yaffs_oh_to_size(dev, oh, 0) :
1341 tags.extra_file_size;
1342 u32 parent_obj_id = (oh) ?
1343 (u32)oh->parent_obj_id :
1344 tags.extra_parent_id;
1348 tags.extra_is_shrink;
1350 /* If it is deleted (unlinked
1351 * at start also means deleted)
1352 * we treat the file size as
1353 * being zeroed at this point.
1355 if (parent_obj_id == YAFFS_OBJECTID_DELETED ||
1356 parent_obj_id == YAFFS_OBJECTID_UNLINKED) {
1362 in->variant.file_variant.shrink_size >
1364 in->variant.file_variant.shrink_size =
1368 bi->has_shrink_hdr = 1;
1370 /* Use existing - destroy this one. */
1371 yaffs_chunk_del(dev, chunk, 1, __LINE__);
1374 if (!in->valid && in->variant_type !=
1375 (oh ? oh->type : tags.extra_obj_type)) {
1376 yaffs_trace(YAFFS_TRACE_ERROR,
1377 "yaffs tragedy: Bad type, %d != %d, for object %d at chunk %d during scan",
1378 oh ? oh->type : tags.extra_obj_type,
1379 in->variant_type, tags.obj_id,
1381 in = yaffs_retype_obj(in, oh ? oh->type : tags.extra_obj_type);
1385 (tags.obj_id == YAFFS_OBJECTID_ROOT ||
1386 tags.obj_id == YAFFS_OBJECTID_LOSTNFOUND)) {
1387 /* We only load some info, don't fiddle
1388 * with directory structure */
1392 in->yst_mode = oh->yst_mode;
1393 yaffs_load_attribs(in, oh);
1394 in->lazy_loaded = 0;
1396 in->lazy_loaded = 1;
1398 in->hdr_chunk = chunk;
1400 } else if (!in->valid) {
1401 /* we need to load this info */
1403 in->hdr_chunk = chunk;
1405 in->variant_type = oh->type;
1406 in->yst_mode = oh->yst_mode;
1407 yaffs_load_attribs(in, oh);
1409 if (oh->shadows_obj > 0)
1410 yaffs_handle_shadowed_obj(dev,
1411 oh->shadows_obj, 1);
1413 yaffs_set_obj_name_from_oh(in, oh);
1414 parent = yaffs_find_or_create_by_number(dev,
1416 YAFFS_OBJECT_TYPE_DIRECTORY);
1417 file_size = yaffs_oh_to_size(dev, oh, 0);
1418 is_shrink = oh->is_shrink;
1419 equiv_id = oh->equiv_id;
1421 in->variant_type = tags.extra_obj_type;
1422 parent = yaffs_find_or_create_by_number(dev,
1423 tags.extra_parent_id,
1424 YAFFS_OBJECT_TYPE_DIRECTORY);
1425 file_size = tags.extra_file_size;
1426 is_shrink = tags.extra_is_shrink;
1427 equiv_id = tags.extra_equiv_id;
1428 in->lazy_loaded = 1;
1435 /* directory stuff...
1440 parent->variant_type == YAFFS_OBJECT_TYPE_UNKNOWN) {
1441 /* Set up as a directory */
1442 parent->variant_type =
1443 YAFFS_OBJECT_TYPE_DIRECTORY;
1444 INIT_LIST_HEAD(&parent->
1445 variant.dir_variant.children);
1446 } else if (!parent ||
1447 parent->variant_type !=
1448 YAFFS_OBJECT_TYPE_DIRECTORY) {
1449 /* Hoosterman, another problem....
1450 * Trying to use a non-directory as a directory
1453 yaffs_trace(YAFFS_TRACE_ERROR,
1454 "yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
1456 parent = dev->lost_n_found;
1458 yaffs_add_obj_to_dir(parent, in);
1460 is_unlinked = (parent == dev->del_dir) ||
1461 (parent == dev->unlinked_dir);
1464 /* Mark the block */
1465 bi->has_shrink_hdr = 1;
1467 /* Note re hardlinks.
1468 * Since we might scan a hardlink before its equivalent
1469 * object is scanned we put them all in a list.
1470 * After scanning is complete, we should have all the
1471 * objects, so we run through this list and fix up all
1475 switch (in->variant_type) {
1476 case YAFFS_OBJECT_TYPE_UNKNOWN:
1477 /* Todo got a problem */
1479 case YAFFS_OBJECT_TYPE_FILE:
1480 file_var = &in->variant.file_variant;
1481 if (file_var->stored_size < file_size) {
1482 /* This covers the case where the file
1483 * size is greater than the data held.
1484 * This will happen if the file is
1485 * resized to be larger than its
1486 * current data extents.
1488 file_var->file_size = file_size;
1489 file_var->stored_size = file_size;
1492 if (file_var->shrink_size > file_size)
1493 file_var->shrink_size = file_size;
1496 case YAFFS_OBJECT_TYPE_HARDLINK:
1497 hl_var = &in->variant.hardlink_variant;
1499 hl_var->equiv_id = equiv_id;
1500 list_add(&in->hard_links, hard_list);
1503 case YAFFS_OBJECT_TYPE_DIRECTORY:
1506 case YAFFS_OBJECT_TYPE_SPECIAL:
1509 case YAFFS_OBJECT_TYPE_SYMLINK:
1510 sl_var = &in->variant.symlink_variant;
1513 yaffs_clone_str(oh->alias);
1521 return alloc_failed ? YAFFS_FAIL : YAFFS_OK;
1524 int yaffs2_scan_backwards(struct yaffs_dev *dev)
1531 enum yaffs_block_state state;
1533 LIST_HEAD(hard_list);
1534 struct yaffs_block_info *bi;
1536 int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
1539 int alloc_failed = 0;
1540 struct yaffs_block_index *block_index = NULL;
1541 int alt_block_index = 0;
1542 int summary_available;
1544 yaffs_trace(YAFFS_TRACE_SCAN,
1545 "yaffs2_scan_backwards starts intstartblk %d intendblk %d...",
1546 dev->internal_start_block, dev->internal_end_block);
1548 dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
1551 kmalloc(n_blocks * sizeof(struct yaffs_block_index), GFP_NOFS);
1555 vmalloc(n_blocks * sizeof(struct yaffs_block_index));
1556 alt_block_index = 1;
1560 yaffs_trace(YAFFS_TRACE_SCAN,
1561 "yaffs2_scan_backwards() could not allocate block index!"
1566 dev->blocks_in_checkpt = 0;
1568 chunk_data = yaffs_get_temp_buffer(dev);
1570 /* Scan all the blocks to determine their state */
1571 bi = dev->block_info;
1572 for (blk = dev->internal_start_block; blk <= dev->internal_end_block;
1574 yaffs_clear_chunk_bits(dev, blk);
1575 bi->pages_in_use = 0;
1576 bi->soft_del_pages = 0;
1578 yaffs_query_init_block_state(dev, blk, &state, &seq_number);
1580 bi->block_state = state;
1581 bi->seq_number = seq_number;
1583 if (bi->seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA)
1584 bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
1585 if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
1586 bi->block_state = YAFFS_BLOCK_STATE_DEAD;
1588 yaffs_trace(YAFFS_TRACE_SCAN_DEBUG,
1589 "Block scanning block %d state %d seq %d",
1590 blk, bi->block_state, seq_number);
1592 if (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT) {
1593 dev->blocks_in_checkpt++;
1595 } else if (bi->block_state == YAFFS_BLOCK_STATE_DEAD) {
1596 yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
1597 "block %d is bad", blk);
1598 } else if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
1599 yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "Block empty ");
1600 dev->n_erased_blocks++;
1601 dev->n_free_chunks += dev->param.chunks_per_block;
1602 } else if (bi->block_state ==
1603 YAFFS_BLOCK_STATE_NEEDS_SCAN) {
1604 /* Determine the highest sequence number */
1605 if (seq_number >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
1606 seq_number < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
1607 block_index[n_to_scan].seq = seq_number;
1608 block_index[n_to_scan].block = blk;
1610 if (seq_number >= dev->seq_number)
1611 dev->seq_number = seq_number;
1613 /* TODO: Nasty sequence number! */
1614 yaffs_trace(YAFFS_TRACE_SCAN,
1615 "Block scanning block %d has bad sequence number %d",
1622 yaffs_trace(YAFFS_TRACE_ALWAYS, "%d blocks to be sorted...", n_to_scan);
1626 /* Sort the blocks by sequence number */
1627 sort(block_index, n_to_scan, sizeof(struct yaffs_block_index),
1628 yaffs2_ybicmp, NULL);
1632 yaffs_trace(YAFFS_TRACE_SCAN, "...done");
1634 /* Now scan the blocks looking at the data. */
1636 end_iter = n_to_scan - 1;
1637 yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "%d blocks to scan", n_to_scan);
1639 /* For each block.... backwards */
1640 for (block_iter = end_iter;
1641 !alloc_failed && block_iter >= start_iter;
1643 /* Cooperative multitasking! This loop can run for so
1644 long that watchdog timers expire. */
1647 /* get the block to scan in the correct order */
1648 blk = block_index[block_iter].block;
1649 bi = yaffs_get_block_info(dev, blk);
1651 summary_available = yaffs_summary_read(dev, dev->sum_tags, blk);
1653 /* For each chunk in each block that needs scanning.... */
1655 if (summary_available)
1656 c = dev->chunks_per_summary - 1;
1658 c = dev->param.chunks_per_block - 1;
1660 for (/* c is already initialised */;
1661 !alloc_failed && c >= 0 &&
1662 (bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN ||
1663 bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING);
1665 /* Scan backwards...
1666 * Read the tags and decide what to do
1668 if (yaffs2_scan_chunk(dev, bi, blk, c,
1669 &found_chunks, chunk_data,
1670 &hard_list, summary_available) ==
1675 if (bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN) {
1676 /* If we got this far while scanning, then the block
1677 * is fully allocated. */
1678 bi->block_state = YAFFS_BLOCK_STATE_FULL;
1681 /* Now let's see if it was dirty */
1682 if (bi->pages_in_use == 0 &&
1683 !bi->has_shrink_hdr &&
1684 bi->block_state == YAFFS_BLOCK_STATE_FULL) {
1685 yaffs_block_became_dirty(dev, blk);
1689 yaffs_skip_rest_of_block(dev);
1691 if (alt_block_index)
1696 /* Ok, we've done all the scanning.
1697 * Fix up the hard link chains.
1698 * We have scanned all the objects, now it's time to add these
1701 yaffs_link_fixup(dev, &hard_list);
1703 yaffs_release_temp_buffer(dev, chunk_data);
1708 yaffs_trace(YAFFS_TRACE_SCAN, "yaffs2_scan_backwards ends");