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_guts.h"
15 #include "yaffs_trace.h"
16 #include "yaffs_yaffs2.h"
17 #include "yaffs_checkptrw.h"
18 #include "yaffs_bitmap.h"
19 #include "yaffs_nand.h"
20 #include "yaffs_getblockinfo.h"
21 #include "yaffs_verify.h"
22 #include "yaffs_attribs.h"
25 * Checkpoints are really no benefit on very small partitions.
27 * To save space on small partitions don't bother with checkpoints unless
28 * the partition is at least this big.
30 #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 (b->pages_in_use - b->soft_del_pages) <
58 dev->param.chunks_per_block && b->seq_number < seq) {
66 dev->oldest_dirty_seq = seq;
67 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 one that have
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)
148 struct yaffs_block_info *bi;
150 if (!dev->param.is_yaffs2)
154 * If refresh period < 10 then refreshing is disabled.
156 if (dev->param.refresh_period < 10)
162 if (dev->refresh_skip > dev->param.refresh_period)
163 dev->refresh_skip = dev->param.refresh_period;
165 if (dev->refresh_skip > 0)
169 * Refresh skip is now zero.
170 * We'll do a refresh this time around....
171 * Update the refresh skip and find the oldest block.
173 dev->refresh_skip = dev->param.refresh_period;
174 dev->refresh_count++;
175 bi = dev->block_info;
176 for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
178 if (bi->block_state == YAFFS_BLOCK_STATE_FULL) {
180 if (oldest < 1 || bi->seq_number < oldest_seq) {
182 oldest_seq = bi->seq_number;
191 ("GC refresh count %d selected block %d with seq_number %d"
192 TENDSTR), dev->refresh_count, oldest, oldest_seq));
198 int yaffs2_checkpt_required(struct yaffs_dev *dev)
202 if (!dev->param.is_yaffs2)
205 nblocks = dev->internal_end_block - dev->internal_start_block + 1;
207 return !dev->param.skip_checkpt_wr &&
208 !dev->read_only && (nblocks >= YAFFS_CHECKPOINT_MIN_BLOCKS);
211 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 */
223 (dev->param.end_block - dev->param.start_block + 1);
225 n_bytes += sizeof(struct yaffs_checkpt_validity);
226 n_bytes += sizeof(struct yaffs_checkpt_dev);
227 n_bytes += dev_blocks * sizeof(struct yaffs_block_info);
228 n_bytes += dev_blocks * dev->chunk_bit_stride;
230 (sizeof(struct yaffs_checkpt_obj) +
231 sizeof(u32)) * (dev->n_obj);
232 n_bytes += (dev->tnode_size + sizeof(u32)) * (dev->n_tnodes);
233 n_bytes += sizeof(struct yaffs_checkpt_validity);
234 n_bytes += sizeof(u32); /* checksum */
236 /* Round up and add 2 blocks to allow for some bad blocks, so add 3 */
240 (dev->data_bytes_per_chunk *
241 dev->param.chunks_per_block)) + 3;
243 dev->checkpoint_blocks_required = n_blocks;
246 retval = dev->checkpoint_blocks_required - dev->blocks_in_checkpt;
252 /*--------------------- Checkpointing --------------------*/
254 static int yaffs2_wr_checkpt_validity_marker(struct yaffs_dev *dev, int head)
256 struct yaffs_checkpt_validity cp;
258 memset(&cp, 0, sizeof(cp));
260 cp.struct_type = sizeof(cp);
261 cp.magic = YAFFS_MAGIC;
262 cp.version = YAFFS_CHECKPOINT_VERSION;
263 cp.head = (head) ? 1 : 0;
265 return (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp)) ? 1 : 0;
268 static int yaffs2_rd_checkpt_validity_marker(struct yaffs_dev *dev, int head)
270 struct yaffs_checkpt_validity cp;
273 ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
276 ok = (cp.struct_type == sizeof(cp)) &&
277 (cp.magic == YAFFS_MAGIC) &&
278 (cp.version == YAFFS_CHECKPOINT_VERSION) &&
279 (cp.head == ((head) ? 1 : 0));
283 static void yaffs2_dev_to_checkpt_dev(struct yaffs_checkpt_dev *cp,
284 struct yaffs_dev *dev)
286 cp->n_erased_blocks = dev->n_erased_blocks;
287 cp->alloc_block = dev->alloc_block;
288 cp->alloc_page = dev->alloc_page;
289 cp->n_free_chunks = dev->n_free_chunks;
291 cp->n_deleted_files = dev->n_deleted_files;
292 cp->n_unlinked_files = dev->n_unlinked_files;
293 cp->n_bg_deletions = dev->n_bg_deletions;
294 cp->seq_number = dev->seq_number;
298 static void yaffs_checkpt_dev_to_dev(struct yaffs_dev *dev,
299 struct yaffs_checkpt_dev *cp)
301 dev->n_erased_blocks = cp->n_erased_blocks;
302 dev->alloc_block = cp->alloc_block;
303 dev->alloc_page = cp->alloc_page;
304 dev->n_free_chunks = cp->n_free_chunks;
306 dev->n_deleted_files = cp->n_deleted_files;
307 dev->n_unlinked_files = cp->n_unlinked_files;
308 dev->n_bg_deletions = cp->n_bg_deletions;
309 dev->seq_number = cp->seq_number;
312 static int yaffs2_wr_checkpt_dev(struct yaffs_dev *dev)
314 struct yaffs_checkpt_dev cp;
317 (dev->internal_end_block - dev->internal_start_block + 1);
321 /* Write device runtime values */
322 yaffs2_dev_to_checkpt_dev(&cp, dev);
323 cp.struct_type = sizeof(cp);
325 ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
327 /* Write block info */
329 n_bytes = n_blocks * sizeof(struct yaffs_block_info);
330 ok = (yaffs2_checkpt_wr(dev, dev->block_info, n_bytes) ==
334 /* Write chunk bits */
336 n_bytes = n_blocks * dev->chunk_bit_stride;
337 ok = (yaffs2_checkpt_wr(dev, dev->chunk_bits, n_bytes) ==
344 static int yaffs2_rd_checkpt_dev(struct yaffs_dev *dev)
346 struct yaffs_checkpt_dev cp;
349 (dev->internal_end_block - dev->internal_start_block + 1);
353 ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
357 if (cp.struct_type != sizeof(cp))
360 yaffs_checkpt_dev_to_dev(dev, &cp);
362 n_bytes = n_blocks * sizeof(struct yaffs_block_info);
364 ok = (yaffs2_checkpt_rd(dev, dev->block_info, n_bytes) == n_bytes);
368 n_bytes = n_blocks * dev->chunk_bit_stride;
370 ok = (yaffs2_checkpt_rd(dev, dev->chunk_bits, n_bytes) == n_bytes);
375 static void yaffs2_obj_checkpt_obj(struct yaffs_checkpt_obj *cp,
376 struct yaffs_obj *obj)
379 cp->obj_id = obj->obj_id;
380 cp->parent_id = (obj->parent) ? obj->parent->obj_id : 0;
381 cp->hdr_chunk = obj->hdr_chunk;
382 cp->variant_type = obj->variant_type;
383 cp->deleted = obj->deleted;
384 cp->soft_del = obj->soft_del;
385 cp->unlinked = obj->unlinked;
386 cp->fake = obj->fake;
387 cp->rename_allowed = obj->rename_allowed;
388 cp->unlink_allowed = obj->unlink_allowed;
389 cp->serial = obj->serial;
390 cp->n_data_chunks = obj->n_data_chunks;
392 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
393 cp->size_or_equiv_obj = obj->variant.file_variant.file_size;
394 else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK)
395 cp->size_or_equiv_obj = obj->variant.hardlink_variant.equiv_id;
398 static int taffs2_checkpt_obj_to_obj(struct yaffs_obj *obj,
399 struct yaffs_checkpt_obj *cp)
402 struct yaffs_obj *parent;
404 if (obj->variant_type != cp->variant_type) {
405 T(YAFFS_TRACE_ERROR, (TSTR("Checkpoint read object %d type %d "
407 ("chunk %d does not match existing object type %d")
408 TENDSTR), cp->obj_id,
409 cp->variant_type, cp->hdr_chunk,
414 obj->obj_id = cp->obj_id;
417 parent = yaffs_find_or_create_by_number(obj->my_dev,
419 YAFFS_OBJECT_TYPE_DIRECTORY);
424 if (parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
425 T(YAFFS_TRACE_ALWAYS,
427 ("Checkpoint read object %d parent %d type %d"
428 TCONT(" chunk %d Parent type, %d, not directory")
429 TENDSTR), cp->obj_id, cp->parent_id,
430 cp->variant_type, cp->hdr_chunk,
431 parent->variant_type));
434 yaffs_add_obj_to_dir(parent, obj);
437 obj->hdr_chunk = cp->hdr_chunk;
438 obj->variant_type = cp->variant_type;
439 obj->deleted = cp->deleted;
440 obj->soft_del = cp->soft_del;
441 obj->unlinked = cp->unlinked;
442 obj->fake = cp->fake;
443 obj->rename_allowed = cp->rename_allowed;
444 obj->unlink_allowed = cp->unlink_allowed;
445 obj->serial = cp->serial;
446 obj->n_data_chunks = cp->n_data_chunks;
448 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
449 obj->variant.file_variant.file_size = cp->size_or_equiv_obj;
450 else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK)
451 obj->variant.hardlink_variant.equiv_id = cp->size_or_equiv_obj;
453 if (obj->hdr_chunk > 0)
454 obj->lazy_loaded = 1;
458 static int yaffs2_checkpt_tnode_worker(struct yaffs_obj *in,
459 struct yaffs_tnode *tn, u32 level,
463 struct yaffs_dev *dev = in->my_dev;
469 for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
470 if (tn->internal[i]) {
471 ok = yaffs2_checkpt_tnode_worker(in,
479 YAFFS_TNODES_INTERNAL_BITS)
483 } else if (level == 0) {
485 chunk_offset << YAFFS_TNODES_LEVEL0_BITS;
486 ok = (yaffs2_checkpt_wr
488 sizeof(base_offset)) == sizeof(base_offset));
490 ok = (yaffs2_checkpt_wr
492 dev->tnode_size) == dev->tnode_size);
500 static int yaffs2_wr_checkpt_tnodes(struct yaffs_obj *obj)
505 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE) {
506 ok = yaffs2_checkpt_tnode_worker(obj,
507 obj->variant.file_variant.top,
508 obj->variant.file_variant.
511 ok = (yaffs2_checkpt_wr
512 (obj->my_dev, &end_marker,
513 sizeof(end_marker)) == sizeof(end_marker));
519 static int yaffs2_rd_checkpt_tnodes(struct yaffs_obj *obj)
523 struct yaffs_dev *dev = obj->my_dev;
524 struct yaffs_file_var *file_stuct_ptr = &obj->variant.file_variant;
525 struct yaffs_tnode *tn;
528 ok = (yaffs2_checkpt_rd(dev, &base_chunk, sizeof(base_chunk)) ==
531 while (ok && (~base_chunk)) {
533 /* Read level 0 tnode */
535 tn = yaffs_get_tnode(dev);
537 ok = (yaffs2_checkpt_rd(dev, tn, dev->tnode_size) ==
544 ok = yaffs_add_find_tnode_0(dev,
546 base_chunk, tn) ? 1 : 0;
549 ok = (yaffs2_checkpt_rd
551 sizeof(base_chunk)) == sizeof(base_chunk));
555 T(YAFFS_TRACE_CHECKPOINT,
556 (TSTR("Checkpoint read tnodes %d records, last %d. ok %d" TENDSTR),
557 nread, base_chunk, ok));
562 static int yaffs2_wr_checkpt_objs(struct yaffs_dev *dev)
564 struct yaffs_obj *obj;
565 struct yaffs_checkpt_obj cp;
568 struct list_head *lh;
570 /* Iterate through the objects in each hash entry,
571 * dumping them to the checkpointing stream.
574 for (i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++) {
575 list_for_each(lh, &dev->obj_bucket[i].list) {
578 list_entry(lh, struct yaffs_obj, hash_link);
579 if (!obj->defered_free) {
580 yaffs2_obj_checkpt_obj(&cp, obj);
581 cp.struct_type = sizeof(cp);
583 T(YAFFS_TRACE_CHECKPOINT,
585 ("Checkpoint write object %d parent %d type %d chunk %d obj addr %p"
586 TENDSTR), cp.obj_id, cp.parent_id,
587 cp.variant_type, cp.hdr_chunk, obj));
589 ok = (yaffs2_checkpt_wr
591 sizeof(cp)) == sizeof(cp));
594 && obj->variant_type ==
595 YAFFS_OBJECT_TYPE_FILE)
596 ok = yaffs2_wr_checkpt_tnodes
603 /* Dump end of list */
604 memset(&cp, 0xFF, sizeof(struct yaffs_checkpt_obj));
605 cp.struct_type = sizeof(cp);
608 ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
613 static int yaffs2_rd_checkpt_objs(struct yaffs_dev *dev)
615 struct yaffs_obj *obj;
616 struct yaffs_checkpt_obj cp;
619 struct yaffs_obj *hard_list = NULL;
621 while (ok && !done) {
622 ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
623 if (cp.struct_type != sizeof(cp)) {
624 T(YAFFS_TRACE_CHECKPOINT,
625 (TSTR("struct size %d instead of %d ok %d" TENDSTR),
626 cp.struct_type, (int)sizeof(cp), ok));
630 T(YAFFS_TRACE_CHECKPOINT,
632 ("Checkpoint read object %d parent %d type %d chunk %d "
633 TENDSTR), cp.obj_id, cp.parent_id, cp.variant_type,
636 if (ok && cp.obj_id == ~0) {
640 yaffs_find_or_create_by_number(dev, cp.obj_id,
643 ok = taffs2_checkpt_obj_to_obj(obj, &cp);
646 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE) {
647 ok = yaffs2_rd_checkpt_tnodes(obj);
648 } else if (obj->variant_type ==
649 YAFFS_OBJECT_TYPE_HARDLINK) {
650 obj->hard_links.next =
651 (struct list_head *)hard_list;
661 yaffs_link_fixup(dev, hard_list);
666 static int yaffs2_wr_checkpt_sum(struct yaffs_dev *dev)
671 yaffs2_get_checkpt_sum(dev, &checkpt_sum);
673 ok = (yaffs2_checkpt_wr(dev, &checkpt_sum, sizeof(checkpt_sum)) ==
674 sizeof(checkpt_sum));
682 static int yaffs2_rd_checkpt_sum(struct yaffs_dev *dev)
688 yaffs2_get_checkpt_sum(dev, &checkpt_sum0);
690 ok = (yaffs2_checkpt_rd(dev, &checkpt_sum1, sizeof(checkpt_sum1)) ==
691 sizeof(checkpt_sum1));
696 if (checkpt_sum0 != checkpt_sum1)
702 static int yaffs2_wr_checkpt_data(struct yaffs_dev *dev)
706 if (!yaffs2_checkpt_required(dev)) {
707 T(YAFFS_TRACE_CHECKPOINT,
708 (TSTR("skipping checkpoint write" TENDSTR)));
713 ok = yaffs2_checkpt_open(dev, 1);
716 T(YAFFS_TRACE_CHECKPOINT,
717 (TSTR("write checkpoint validity" TENDSTR)));
718 ok = yaffs2_wr_checkpt_validity_marker(dev, 1);
721 T(YAFFS_TRACE_CHECKPOINT,
722 (TSTR("write checkpoint device" TENDSTR)));
723 ok = yaffs2_wr_checkpt_dev(dev);
726 T(YAFFS_TRACE_CHECKPOINT,
727 (TSTR("write checkpoint objects" TENDSTR)));
728 ok = yaffs2_wr_checkpt_objs(dev);
731 T(YAFFS_TRACE_CHECKPOINT,
732 (TSTR("write checkpoint validity" TENDSTR)));
733 ok = yaffs2_wr_checkpt_validity_marker(dev, 0);
737 ok = yaffs2_wr_checkpt_sum(dev);
739 if (!yaffs_checkpt_close(dev))
743 dev->is_checkpointed = 1;
745 dev->is_checkpointed = 0;
747 return dev->is_checkpointed;
750 static int yaffs2_rd_checkpt_data(struct yaffs_dev *dev)
754 if (!dev->param.is_yaffs2)
757 if (ok && dev->param.skip_checkpt_rd) {
758 T(YAFFS_TRACE_CHECKPOINT,
759 (TSTR("skipping checkpoint read" TENDSTR)));
764 ok = yaffs2_checkpt_open(dev, 0); /* open for read */
767 T(YAFFS_TRACE_CHECKPOINT,
768 (TSTR("read checkpoint validity" TENDSTR)));
769 ok = yaffs2_rd_checkpt_validity_marker(dev, 1);
772 T(YAFFS_TRACE_CHECKPOINT,
773 (TSTR("read checkpoint device" TENDSTR)));
774 ok = yaffs2_rd_checkpt_dev(dev);
777 T(YAFFS_TRACE_CHECKPOINT,
778 (TSTR("read checkpoint objects" TENDSTR)));
779 ok = yaffs2_rd_checkpt_objs(dev);
782 T(YAFFS_TRACE_CHECKPOINT,
783 (TSTR("read checkpoint validity" TENDSTR)));
784 ok = yaffs2_rd_checkpt_validity_marker(dev, 0);
788 ok = yaffs2_rd_checkpt_sum(dev);
789 T(YAFFS_TRACE_CHECKPOINT,
790 (TSTR("read checkpoint checksum %d" TENDSTR), ok));
793 if (!yaffs_checkpt_close(dev))
797 dev->is_checkpointed = 1;
799 dev->is_checkpointed = 0;
805 void yaffs2_checkpt_invalidate(struct yaffs_dev *dev)
807 if (dev->is_checkpointed || dev->blocks_in_checkpt > 0) {
808 dev->is_checkpointed = 0;
809 yaffs2_checkpt_invalidate_stream(dev);
811 if (dev->param.sb_dirty_fn)
812 dev->param.sb_dirty_fn(dev);
815 int yaffs_checkpoint_save(struct yaffs_dev *dev)
818 T(YAFFS_TRACE_CHECKPOINT,
819 (TSTR("save entry: is_checkpointed %d" TENDSTR),
820 dev->is_checkpointed));
822 yaffs_verify_objects(dev);
823 yaffs_verify_blocks(dev);
824 yaffs_verify_free_chunks(dev);
826 if (!dev->is_checkpointed) {
827 yaffs2_checkpt_invalidate(dev);
828 yaffs2_wr_checkpt_data(dev);
831 T(YAFFS_TRACE_ALWAYS,
832 (TSTR("save exit: is_checkpointed %d" TENDSTR),
833 dev->is_checkpointed));
835 return dev->is_checkpointed;
838 int yaffs2_checkpt_restore(struct yaffs_dev *dev)
841 T(YAFFS_TRACE_CHECKPOINT,
842 (TSTR("restore entry: is_checkpointed %d" TENDSTR),
843 dev->is_checkpointed));
845 retval = yaffs2_rd_checkpt_data(dev);
847 if (dev->is_checkpointed) {
848 yaffs_verify_objects(dev);
849 yaffs_verify_blocks(dev);
850 yaffs_verify_free_chunks(dev);
853 T(YAFFS_TRACE_CHECKPOINT,
854 (TSTR("restore exit: is_checkpointed %d" TENDSTR),
855 dev->is_checkpointed));
860 int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size)
862 /* if new_size > old_file_size.
863 * We're going to be writing a hole.
864 * If the hole is small then write zeros otherwise write a start of hole marker.
867 loff_t old_file_size;
870 int result = YAFFS_OK;
871 struct yaffs_dev *dev = NULL;
873 u8 *local_buffer = NULL;
875 int small_increase_ok = 0;
880 if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
885 /* Bail out if not yaffs2 mode */
886 if (!dev->param.is_yaffs2)
889 old_file_size = obj->variant.file_variant.file_size;
891 if (new_size <= old_file_size)
894 increase = new_size - old_file_size;
896 if (increase < YAFFS_SMALL_HOLE_THRESHOLD * dev->data_bytes_per_chunk &&
897 yaffs_check_alloc_available(dev, YAFFS_SMALL_HOLE_THRESHOLD + 1))
903 local_buffer = yaffs_get_temp_buffer(dev, __LINE__);
906 /* fill hole with zero bytes */
907 int pos = old_file_size;
910 memset(local_buffer, 0, dev->data_bytes_per_chunk);
911 small_increase_ok = 1;
913 while (increase > 0 && small_increase_ok) {
914 this_write = increase;
915 if (this_write > dev->data_bytes_per_chunk)
916 this_write = dev->data_bytes_per_chunk;
918 yaffs_do_file_wr(obj, local_buffer, pos, this_write,
920 if (written == this_write) {
922 increase -= this_write;
924 small_increase_ok = 0;
928 yaffs_release_temp_buffer(dev, local_buffer, __LINE__);
930 /* If we were out of space then reverse any chunks we've added */
931 if (!small_increase_ok)
932 yaffs_resize_file_down(obj, old_file_size);
935 if (!small_increase_ok &&
937 obj->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
938 obj->parent->obj_id != YAFFS_OBJECTID_DELETED) {
939 /* Write a hole start header with the old file size */
940 yaffs_update_oh(obj, NULL, 0, 1, 0, NULL);
947 struct yaffs_block_index {
952 static int yaffs2_ybicmp(const void *a, const void *b)
954 int aseq = ((struct yaffs_block_index *)a)->seq;
955 int bseq = ((struct yaffs_block_index *)b)->seq;
956 int ablock = ((struct yaffs_block_index *)a)->block;
957 int bblock = ((struct yaffs_block_index *)b)->block;
959 return ablock - bblock;
964 int yaffs2_scan_backwards(struct yaffs_dev *dev)
966 struct yaffs_ext_tags tags;
977 enum yaffs_block_state state;
978 struct yaffs_obj *hard_list = NULL;
979 struct yaffs_block_info *bi;
981 struct yaffs_obj_hdr *oh;
982 struct yaffs_obj *in;
983 struct yaffs_obj *parent;
984 int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
992 int alloc_failed = 0;
994 struct yaffs_block_index *block_index = NULL;
995 int alt_block_index = 0;
999 ("yaffs2_scan_backwards starts intstartblk %d intendblk %d..."
1000 TENDSTR), dev->internal_start_block, dev->internal_end_block));
1002 dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
1004 block_index = YMALLOC(n_blocks * sizeof(struct yaffs_block_index));
1008 YMALLOC_ALT(n_blocks * sizeof(struct yaffs_block_index));
1009 alt_block_index = 1;
1015 ("yaffs2_scan_backwards() could not allocate block index!"
1020 dev->blocks_in_checkpt = 0;
1022 chunk_data = yaffs_get_temp_buffer(dev, __LINE__);
1024 /* Scan all the blocks to determine their state */
1025 bi = dev->block_info;
1026 for (blk = dev->internal_start_block; blk <= dev->internal_end_block;
1028 yaffs_clear_chunk_bits(dev, blk);
1029 bi->pages_in_use = 0;
1030 bi->soft_del_pages = 0;
1032 yaffs_query_init_block_state(dev, blk, &state, &seq_number);
1034 bi->block_state = state;
1035 bi->seq_number = seq_number;
1037 if (bi->seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA)
1038 bi->block_state = state = YAFFS_BLOCK_STATE_CHECKPOINT;
1039 if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
1040 bi->block_state = state = YAFFS_BLOCK_STATE_DEAD;
1042 T(YAFFS_TRACE_SCAN_DEBUG,
1043 (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
1044 state, seq_number));
1046 if (state == YAFFS_BLOCK_STATE_CHECKPOINT) {
1047 dev->blocks_in_checkpt++;
1049 } else if (state == YAFFS_BLOCK_STATE_DEAD) {
1050 T(YAFFS_TRACE_BAD_BLOCKS,
1051 (TSTR("block %d is bad" TENDSTR), blk));
1052 } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
1053 T(YAFFS_TRACE_SCAN_DEBUG,
1054 (TSTR("Block empty " TENDSTR)));
1055 dev->n_erased_blocks++;
1056 dev->n_free_chunks += dev->param.chunks_per_block;
1057 } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
1059 /* Determine the highest sequence number */
1060 if (seq_number >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
1061 seq_number < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
1063 block_index[n_to_scan].seq = seq_number;
1064 block_index[n_to_scan].block = blk;
1068 if (seq_number >= dev->seq_number)
1069 dev->seq_number = seq_number;
1071 /* TODO: Nasty sequence number! */
1074 ("Block scanning block %d has bad sequence number %d"
1075 TENDSTR), blk, seq_number));
1083 (TSTR("%d blocks to be sorted..." TENDSTR), n_to_scan));
1087 /* Sort the blocks by sequence number */
1088 yaffs_sort(block_index, n_to_scan, sizeof(struct yaffs_block_index),
1093 T(YAFFS_TRACE_SCAN, (TSTR("...done" TENDSTR)));
1095 /* Now scan the blocks looking at the data. */
1097 end_iter = n_to_scan - 1;
1098 T(YAFFS_TRACE_SCAN_DEBUG,
1099 (TSTR("%d blocks to be scanned" TENDSTR), n_to_scan));
1101 /* For each block.... backwards */
1102 for (block_iter = end_iter; !alloc_failed && block_iter >= start_iter;
1104 /* Cooperative multitasking! This loop can run for so
1105 long that watchdog timers expire. */
1108 /* get the block to scan in the correct order */
1109 blk = block_index[block_iter].block;
1111 bi = yaffs_get_block_info(dev, blk);
1113 state = bi->block_state;
1117 /* For each chunk in each block that needs scanning.... */
1119 for (c = dev->param.chunks_per_block - 1;
1120 !alloc_failed && c >= 0 &&
1121 (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
1122 state == YAFFS_BLOCK_STATE_ALLOCATING); c--) {
1123 /* Scan backwards...
1124 * Read the tags and decide what to do
1127 chunk = blk * dev->param.chunks_per_block + c;
1129 result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL,
1132 /* Let's have a good look at this chunk... */
1134 if (!tags.chunk_used) {
1135 /* An unassigned chunk in the block.
1136 * If there are used chunks after this one, then
1137 * it is a chunk that was skipped due to failing the erased
1138 * check. Just skip it so that it can be deleted.
1139 * But, more typically, We get here when this is an unallocated
1140 * chunk and his means that either the block is empty or
1141 * this is the one being allocated from
1145 /* This is a chunk that was skipped due to failing the erased check */
1146 } else if (c == 0) {
1147 /* We're looking at the first chunk in the block so the block is unused */
1148 state = YAFFS_BLOCK_STATE_EMPTY;
1149 dev->n_erased_blocks++;
1152 YAFFS_BLOCK_STATE_NEEDS_SCANNING
1154 YAFFS_BLOCK_STATE_ALLOCATING) {
1155 if (dev->seq_number ==
1157 /* this is the block being allocated from */
1161 (" Allocating from %d %d"
1165 YAFFS_BLOCK_STATE_ALLOCATING;
1166 dev->alloc_block = blk;
1167 dev->alloc_page = c;
1169 alloc_block_finder =
1172 /* This is a partially written block that is not
1173 * the current allocation block.
1178 ("Partially written block %d detected"
1184 dev->n_free_chunks++;
1186 } else if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED) {
1189 (" Unfixed ECC in chunk(%d:%d), chunk ignored"
1192 dev->n_free_chunks++;
1194 } else if (tags.obj_id > YAFFS_MAX_OBJECT_ID ||
1195 tags.chunk_id > YAFFS_MAX_CHUNK_ID ||
1197 && tags.n_bytes > dev->data_bytes_per_chunk)
1198 || tags.seq_number != bi->seq_number) {
1201 ("Chunk (%d:%d) with bad tags:obj = %d, chunk_id = %d, n_bytes = %d, ignored"
1202 TENDSTR), blk, c, tags.obj_id,
1203 tags.chunk_id, tags.n_bytes));
1205 dev->n_free_chunks++;
1207 } else if (tags.chunk_id > 0) {
1208 /* chunk_id > 0 so it is a data chunk... */
1209 unsigned int endpos;
1212 1) * dev->data_bytes_per_chunk;
1216 yaffs_set_chunk_bit(dev, blk, c);
1219 in = yaffs_find_or_create_by_number(dev,
1221 YAFFS_OBJECT_TYPE_FILE);
1228 in->variant_type == YAFFS_OBJECT_TYPE_FILE
1230 in->variant.file_variant.shrink_size) {
1231 /* This has not been invalidated by a resize */
1232 if (!yaffs_put_chunk_in_file
1233 (in, tags.chunk_id, chunk, -1)) {
1237 /* File size is calculated by looking at the data chunks if we have not
1238 * seen an object header yet. Stop this practice once we find an object header.
1240 endpos = chunk_base + tags.n_bytes;
1242 if (!in->valid && /* have not got an object header yet */
1243 in->variant.file_variant.
1244 scanned_size < endpos) {
1245 in->variant.file_variant.
1246 scanned_size = endpos;
1247 in->variant.file_variant.
1252 /* This chunk has been invalidated by a resize, or a past file deletion
1253 * so delete the chunk*/
1254 yaffs_chunk_del(dev, chunk, 1,
1259 /* chunk_id == 0, so it is an ObjectHeader.
1260 * Thus, we read in the object header and make the object
1264 yaffs_set_chunk_bit(dev, blk, c);
1270 if (tags.extra_available) {
1271 in = yaffs_find_or_create_by_number(dev,
1281 (!in->valid && dev->param.disable_lazy_load)
1282 || tags.extra_shadows || (!in->valid
1287 YAFFS_OBJECTID_LOSTNFOUND)))
1290 /* If we don't have valid info then we need to read the chunk
1291 * TODO In future we can probably defer reading the chunk and
1292 * living with invalid data until needed.
1295 result = yaffs_rd_chunk_tags_nand(dev,
1300 oh = (struct yaffs_obj_hdr *)chunk_data;
1302 if (dev->param.inband_tags) {
1303 /* Fix up the header if they got corrupted by inband tags */
1305 oh->inband_shadowed_obj_id;
1307 oh->inband_is_shrink;
1311 in = yaffs_find_or_create_by_number(dev, tags.obj_id, oh->type);
1319 /* TODO Hoosterman we have a problem! */
1320 T(YAFFS_TRACE_ERROR,
1322 ("yaffs tragedy: Could not make object for object %d at chunk %d during scan"
1323 TENDSTR), tags.obj_id, chunk));
1328 /* We have already filled this one.
1329 * We have a duplicate that will be discarded, but
1330 * we first have to suck out resize info if it is a file.
1333 if ((in->variant_type ==
1334 YAFFS_OBJECT_TYPE_FILE) && ((oh
1339 YAFFS_OBJECT_TYPE_FILE)
1347 YAFFS_OBJECT_TYPE_FILE)))
1354 (oh) ? oh->parent_obj_id :
1355 tags.extra_parent_id;
1360 tags.extra_is_shrink;
1362 /* If it is deleted (unlinked at start also means deleted)
1363 * we treat the file size as being zeroed at this point.
1365 if (parent_obj_id ==
1366 YAFFS_OBJECTID_DELETED
1368 YAFFS_OBJECTID_UNLINKED) {
1374 && in->variant.file_variant.
1375 shrink_size > this_size)
1382 bi->has_shrink_hdr = 1;
1385 /* Use existing - destroy this one. */
1386 yaffs_chunk_del(dev, chunk, 1,
1391 if (!in->valid && in->variant_type !=
1392 (oh ? oh->type : tags.extra_obj_type))
1393 T(YAFFS_TRACE_ERROR,
1395 ("yaffs tragedy: Bad object type, "
1397 ("%d != %d, for object %d at chunk ")
1398 TCONT("%d during scan")
1400 oh->type : tags.extra_obj_type,
1401 in->variant_type, tags.obj_id,
1405 (tags.obj_id == YAFFS_OBJECTID_ROOT ||
1407 YAFFS_OBJECTID_LOSTNFOUND)) {
1408 /* We only load some info, don't fiddle with directory structure */
1413 in->yst_mode = oh->yst_mode;
1414 yaffs_load_attribs(in, oh);
1415 in->lazy_loaded = 0;
1417 in->lazy_loaded = 1;
1419 in->hdr_chunk = chunk;
1421 } else if (!in->valid) {
1422 /* we need to load this info */
1425 in->hdr_chunk = chunk;
1428 in->variant_type = oh->type;
1430 in->yst_mode = oh->yst_mode;
1431 yaffs_load_attribs(in, oh);
1433 if (oh->shadows_obj > 0)
1434 yaffs_handle_shadowed_obj
1439 yaffs_set_obj_name_from_oh(in,
1442 yaffs_find_or_create_by_number
1443 (dev, oh->parent_obj_id,
1444 YAFFS_OBJECT_TYPE_DIRECTORY);
1446 file_size = oh->file_size;
1447 is_shrink = oh->is_shrink;
1448 equiv_id = oh->equiv_id;
1452 tags.extra_obj_type;
1454 yaffs_find_or_create_by_number
1455 (dev, tags.extra_parent_id,
1456 YAFFS_OBJECT_TYPE_DIRECTORY);
1457 file_size = tags.extra_length;
1459 tags.extra_is_shrink;
1460 equiv_id = tags.extra_equiv_id;
1461 in->lazy_loaded = 1;
1469 /* directory stuff...
1473 if (parent && parent->variant_type ==
1474 YAFFS_OBJECT_TYPE_UNKNOWN) {
1475 /* Set up as a directory */
1476 parent->variant_type =
1477 YAFFS_OBJECT_TYPE_DIRECTORY;
1478 INIT_LIST_HEAD(&parent->
1479 variant.dir_variant.children);
1481 || parent->variant_type !=
1482 YAFFS_OBJECT_TYPE_DIRECTORY) {
1483 /* Hoosterman, another problem....
1484 * We're trying to use a non-directory as a directory
1487 T(YAFFS_TRACE_ERROR,
1489 ("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
1491 parent = dev->lost_n_found;
1494 yaffs_add_obj_to_dir(parent, in);
1496 is_unlinked = (parent == dev->del_dir)
1497 || (parent == dev->unlinked_dir);
1500 /* Mark the block as having a shrink header */
1501 bi->has_shrink_hdr = 1;
1504 /* Note re hardlinks.
1505 * Since we might scan a hardlink before its equivalent object is scanned
1506 * we put them all in a list.
1507 * After scanning is complete, we should have all the objects, so we run
1508 * through this list and fix up all the chains.
1511 switch (in->variant_type) {
1512 case YAFFS_OBJECT_TYPE_UNKNOWN:
1513 /* Todo got a problem */
1515 case YAFFS_OBJECT_TYPE_FILE:
1518 file_variant.scanned_size <
1520 /* This covers the case where the file size is greater
1521 * than where the data is
1522 * This will happen if the file is resized to be larger
1523 * than its current data extents.
1535 if (in->variant.file_variant.
1536 shrink_size > file_size)
1543 case YAFFS_OBJECT_TYPE_HARDLINK:
1547 equiv_id = equiv_id;
1548 in->hard_links.next =
1549 (struct list_head *)
1554 case YAFFS_OBJECT_TYPE_DIRECTORY:
1557 case YAFFS_OBJECT_TYPE_SPECIAL:
1560 case YAFFS_OBJECT_TYPE_SYMLINK:
1565 yaffs_clone_str(oh->
1580 } /* End of scanning for each chunk */
1582 if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
1583 /* If we got this far while scanning, then the block is fully allocated. */
1584 state = YAFFS_BLOCK_STATE_FULL;
1587 bi->block_state = state;
1589 /* Now let's see if it was dirty */
1590 if (bi->pages_in_use == 0 &&
1591 !bi->has_shrink_hdr &&
1592 bi->block_state == YAFFS_BLOCK_STATE_FULL) {
1593 yaffs_block_became_dirty(dev, blk);
1598 yaffs_skip_rest_of_block(dev);
1600 if (alt_block_index)
1601 YFREE_ALT(block_index);
1605 /* Ok, we've done all the scanning.
1606 * Fix up the hard link chains.
1607 * We should now have scanned all the objects, now it's time to add these
1610 yaffs_link_fixup(dev, hard_list);
1612 yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
1617 T(YAFFS_TRACE_SCAN, (TSTR("yaffs2_scan_backwards ends" TENDSTR)));