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;
189 yaffs_trace(YAFFS_TRACE_GC,
190 "GC refresh count %d selected block %d with seq_number %d",
191 dev->refresh_count, oldest, oldest_seq);
197 int yaffs2_checkpt_required(struct yaffs_dev *dev)
201 if (!dev->param.is_yaffs2)
204 nblocks = dev->internal_end_block - dev->internal_start_block + 1;
206 return !dev->param.skip_checkpt_wr &&
207 !dev->read_only && (nblocks >= YAFFS_CHECKPOINT_MIN_BLOCKS);
210 int yaffs_calc_checkpt_blocks_required(struct yaffs_dev *dev)
214 if (!dev->param.is_yaffs2)
217 if (!dev->checkpoint_blocks_required && yaffs2_checkpt_required(dev)) {
218 /* Not a valid value so recalculate */
222 (dev->param.end_block - dev->param.start_block + 1);
224 n_bytes += sizeof(struct yaffs_checkpt_validity);
225 n_bytes += sizeof(struct yaffs_checkpt_dev);
226 n_bytes += dev_blocks * sizeof(struct yaffs_block_info);
227 n_bytes += dev_blocks * dev->chunk_bit_stride;
229 (sizeof(struct yaffs_checkpt_obj) +
230 sizeof(u32)) * (dev->n_obj);
231 n_bytes += (dev->tnode_size + sizeof(u32)) * (dev->n_tnodes);
232 n_bytes += sizeof(struct yaffs_checkpt_validity);
233 n_bytes += sizeof(u32); /* checksum */
235 /* Round up and add 2 blocks to allow for some bad blocks, so add 3 */
239 (dev->data_bytes_per_chunk *
240 dev->param.chunks_per_block)) + 3;
242 dev->checkpoint_blocks_required = n_blocks;
245 retval = dev->checkpoint_blocks_required - dev->blocks_in_checkpt;
251 /*--------------------- Checkpointing --------------------*/
253 static int yaffs2_wr_checkpt_validity_marker(struct yaffs_dev *dev, int head)
255 struct yaffs_checkpt_validity cp;
257 memset(&cp, 0, sizeof(cp));
259 cp.struct_type = sizeof(cp);
260 cp.magic = YAFFS_MAGIC;
261 cp.version = YAFFS_CHECKPOINT_VERSION;
262 cp.head = (head) ? 1 : 0;
264 return (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp)) ? 1 : 0;
267 static int yaffs2_rd_checkpt_validity_marker(struct yaffs_dev *dev, int head)
269 struct yaffs_checkpt_validity cp;
272 ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
275 ok = (cp.struct_type == sizeof(cp)) &&
276 (cp.magic == YAFFS_MAGIC) &&
277 (cp.version == YAFFS_CHECKPOINT_VERSION) &&
278 (cp.head == ((head) ? 1 : 0));
282 static void yaffs2_dev_to_checkpt_dev(struct yaffs_checkpt_dev *cp,
283 struct yaffs_dev *dev)
285 cp->n_erased_blocks = dev->n_erased_blocks;
286 cp->alloc_block = dev->alloc_block;
287 cp->alloc_page = dev->alloc_page;
288 cp->n_free_chunks = dev->n_free_chunks;
290 cp->n_deleted_files = dev->n_deleted_files;
291 cp->n_unlinked_files = dev->n_unlinked_files;
292 cp->n_bg_deletions = dev->n_bg_deletions;
293 cp->seq_number = dev->seq_number;
297 static void yaffs_checkpt_dev_to_dev(struct yaffs_dev *dev,
298 struct yaffs_checkpt_dev *cp)
300 dev->n_erased_blocks = cp->n_erased_blocks;
301 dev->alloc_block = cp->alloc_block;
302 dev->alloc_page = cp->alloc_page;
303 dev->n_free_chunks = cp->n_free_chunks;
305 dev->n_deleted_files = cp->n_deleted_files;
306 dev->n_unlinked_files = cp->n_unlinked_files;
307 dev->n_bg_deletions = cp->n_bg_deletions;
308 dev->seq_number = cp->seq_number;
311 static int yaffs2_wr_checkpt_dev(struct yaffs_dev *dev)
313 struct yaffs_checkpt_dev cp;
316 (dev->internal_end_block - dev->internal_start_block + 1);
320 /* Write device runtime values */
321 yaffs2_dev_to_checkpt_dev(&cp, dev);
322 cp.struct_type = sizeof(cp);
324 ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
326 /* Write block info */
328 n_bytes = n_blocks * sizeof(struct yaffs_block_info);
329 ok = (yaffs2_checkpt_wr(dev, dev->block_info, n_bytes) ==
333 /* Write chunk bits */
335 n_bytes = n_blocks * dev->chunk_bit_stride;
336 ok = (yaffs2_checkpt_wr(dev, dev->chunk_bits, n_bytes) ==
343 static int yaffs2_rd_checkpt_dev(struct yaffs_dev *dev)
345 struct yaffs_checkpt_dev cp;
348 (dev->internal_end_block - dev->internal_start_block + 1);
352 ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
356 if (cp.struct_type != sizeof(cp))
359 yaffs_checkpt_dev_to_dev(dev, &cp);
361 n_bytes = n_blocks * sizeof(struct yaffs_block_info);
363 ok = (yaffs2_checkpt_rd(dev, dev->block_info, n_bytes) == n_bytes);
367 n_bytes = n_blocks * dev->chunk_bit_stride;
369 ok = (yaffs2_checkpt_rd(dev, dev->chunk_bits, n_bytes) == n_bytes);
374 static void yaffs2_obj_checkpt_obj(struct yaffs_checkpt_obj *cp,
375 struct yaffs_obj *obj)
378 cp->obj_id = obj->obj_id;
379 cp->parent_id = (obj->parent) ? obj->parent->obj_id : 0;
380 cp->hdr_chunk = obj->hdr_chunk;
381 cp->variant_type = obj->variant_type;
382 cp->deleted = obj->deleted;
383 cp->soft_del = obj->soft_del;
384 cp->unlinked = obj->unlinked;
385 cp->fake = obj->fake;
386 cp->rename_allowed = obj->rename_allowed;
387 cp->unlink_allowed = obj->unlink_allowed;
388 cp->serial = obj->serial;
389 cp->n_data_chunks = obj->n_data_chunks;
391 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
392 cp->size_or_equiv_obj = obj->variant.file_variant.file_size;
393 else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK)
394 cp->size_or_equiv_obj = obj->variant.hardlink_variant.equiv_id;
397 static int taffs2_checkpt_obj_to_obj(struct yaffs_obj *obj,
398 struct yaffs_checkpt_obj *cp)
401 struct yaffs_obj *parent;
403 if (obj->variant_type != cp->variant_type) {
404 yaffs_trace(YAFFS_TRACE_ERROR,
405 "Checkpoint read object %d type %d chunk %d does not match existing object type %d",
406 cp->obj_id, cp->variant_type, cp->hdr_chunk,
411 obj->obj_id = cp->obj_id;
414 parent = yaffs_find_or_create_by_number(obj->my_dev,
416 YAFFS_OBJECT_TYPE_DIRECTORY);
421 if (parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
422 yaffs_trace(YAFFS_TRACE_ALWAYS,
423 "Checkpoint read object %d parent %d type %d chunk %d Parent type, %d, not directory",
424 cp->obj_id, cp->parent_id,
425 cp->variant_type, cp->hdr_chunk,
426 parent->variant_type);
429 yaffs_add_obj_to_dir(parent, obj);
432 obj->hdr_chunk = cp->hdr_chunk;
433 obj->variant_type = cp->variant_type;
434 obj->deleted = cp->deleted;
435 obj->soft_del = cp->soft_del;
436 obj->unlinked = cp->unlinked;
437 obj->fake = cp->fake;
438 obj->rename_allowed = cp->rename_allowed;
439 obj->unlink_allowed = cp->unlink_allowed;
440 obj->serial = cp->serial;
441 obj->n_data_chunks = cp->n_data_chunks;
443 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
444 obj->variant.file_variant.file_size = cp->size_or_equiv_obj;
445 else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK)
446 obj->variant.hardlink_variant.equiv_id = cp->size_or_equiv_obj;
448 if (obj->hdr_chunk > 0)
449 obj->lazy_loaded = 1;
453 static int yaffs2_checkpt_tnode_worker(struct yaffs_obj *in,
454 struct yaffs_tnode *tn, u32 level,
458 struct yaffs_dev *dev = in->my_dev;
464 for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
465 if (tn->internal[i]) {
466 ok = yaffs2_checkpt_tnode_worker(in,
474 YAFFS_TNODES_INTERNAL_BITS)
478 } else if (level == 0) {
480 chunk_offset << YAFFS_TNODES_LEVEL0_BITS;
481 ok = (yaffs2_checkpt_wr
483 sizeof(base_offset)) == sizeof(base_offset));
485 ok = (yaffs2_checkpt_wr
487 dev->tnode_size) == dev->tnode_size);
495 static int yaffs2_wr_checkpt_tnodes(struct yaffs_obj *obj)
500 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE) {
501 ok = yaffs2_checkpt_tnode_worker(obj,
502 obj->variant.file_variant.top,
503 obj->variant.file_variant.
506 ok = (yaffs2_checkpt_wr
507 (obj->my_dev, &end_marker,
508 sizeof(end_marker)) == sizeof(end_marker));
514 static int yaffs2_rd_checkpt_tnodes(struct yaffs_obj *obj)
518 struct yaffs_dev *dev = obj->my_dev;
519 struct yaffs_file_var *file_stuct_ptr = &obj->variant.file_variant;
520 struct yaffs_tnode *tn;
523 ok = (yaffs2_checkpt_rd(dev, &base_chunk, sizeof(base_chunk)) ==
526 while (ok && (~base_chunk)) {
528 /* Read level 0 tnode */
530 tn = yaffs_get_tnode(dev);
532 ok = (yaffs2_checkpt_rd(dev, tn, dev->tnode_size) ==
539 ok = yaffs_add_find_tnode_0(dev,
541 base_chunk, tn) ? 1 : 0;
544 ok = (yaffs2_checkpt_rd
546 sizeof(base_chunk)) == sizeof(base_chunk));
550 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
551 "Checkpoint read tnodes %d records, last %d. ok %d",
552 nread, base_chunk, ok);
557 static int yaffs2_wr_checkpt_objs(struct yaffs_dev *dev)
559 struct yaffs_obj *obj;
560 struct yaffs_checkpt_obj cp;
563 struct list_head *lh;
565 /* Iterate through the objects in each hash entry,
566 * dumping them to the checkpointing stream.
569 for (i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++) {
570 list_for_each(lh, &dev->obj_bucket[i].list) {
571 obj = list_entry(lh, struct yaffs_obj, hash_link);
572 if (!obj->defered_free) {
573 yaffs2_obj_checkpt_obj(&cp, obj);
574 cp.struct_type = sizeof(cp);
576 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
577 "Checkpoint write object %d parent %d type %d chunk %d obj addr %p",
578 cp.obj_id, cp.parent_id,
579 cp.variant_type, cp.hdr_chunk, obj);
581 ok = (yaffs2_checkpt_wr(dev, &cp,
582 sizeof(cp)) == sizeof(cp));
586 YAFFS_OBJECT_TYPE_FILE)
587 ok = yaffs2_wr_checkpt_tnodes(obj);
592 /* Dump end of list */
593 memset(&cp, 0xFF, sizeof(struct yaffs_checkpt_obj));
594 cp.struct_type = sizeof(cp);
597 ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
602 static int yaffs2_rd_checkpt_objs(struct yaffs_dev *dev)
604 struct yaffs_obj *obj;
605 struct yaffs_checkpt_obj cp;
608 struct yaffs_obj *hard_list = NULL;
610 while (ok && !done) {
611 ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
612 if (cp.struct_type != sizeof(cp)) {
613 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
614 "struct size %d instead of %d ok %d",
615 cp.struct_type, (int)sizeof(cp), ok);
619 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
620 "Checkpoint read object %d parent %d type %d chunk %d ",
621 cp.obj_id, cp.parent_id, cp.variant_type,
624 if (ok && cp.obj_id == ~0) {
628 yaffs_find_or_create_by_number(dev, cp.obj_id,
631 ok = taffs2_checkpt_obj_to_obj(obj, &cp);
634 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE) {
635 ok = yaffs2_rd_checkpt_tnodes(obj);
636 } else if (obj->variant_type ==
637 YAFFS_OBJECT_TYPE_HARDLINK) {
638 obj->hard_links.next =
639 (struct list_head *)hard_list;
649 yaffs_link_fixup(dev, hard_list);
654 static int yaffs2_wr_checkpt_sum(struct yaffs_dev *dev)
659 yaffs2_get_checkpt_sum(dev, &checkpt_sum);
661 ok = (yaffs2_checkpt_wr(dev, &checkpt_sum, sizeof(checkpt_sum)) ==
662 sizeof(checkpt_sum));
670 static int yaffs2_rd_checkpt_sum(struct yaffs_dev *dev)
676 yaffs2_get_checkpt_sum(dev, &checkpt_sum0);
678 ok = (yaffs2_checkpt_rd(dev, &checkpt_sum1, sizeof(checkpt_sum1)) ==
679 sizeof(checkpt_sum1));
684 if (checkpt_sum0 != checkpt_sum1)
690 static int yaffs2_wr_checkpt_data(struct yaffs_dev *dev)
694 if (!yaffs2_checkpt_required(dev)) {
695 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
696 "skipping checkpoint write");
701 ok = yaffs2_checkpt_open(dev, 1);
704 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
705 "write checkpoint validity");
706 ok = yaffs2_wr_checkpt_validity_marker(dev, 1);
709 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
710 "write checkpoint device");
711 ok = yaffs2_wr_checkpt_dev(dev);
714 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
715 "write checkpoint objects");
716 ok = yaffs2_wr_checkpt_objs(dev);
719 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
720 "write checkpoint validity");
721 ok = yaffs2_wr_checkpt_validity_marker(dev, 0);
725 ok = yaffs2_wr_checkpt_sum(dev);
727 if (!yaffs_checkpt_close(dev))
731 dev->is_checkpointed = 1;
733 dev->is_checkpointed = 0;
735 return dev->is_checkpointed;
738 static int yaffs2_rd_checkpt_data(struct yaffs_dev *dev)
742 if (!dev->param.is_yaffs2)
745 if (ok && dev->param.skip_checkpt_rd) {
746 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
747 "skipping checkpoint read");
752 ok = yaffs2_checkpt_open(dev, 0); /* open for read */
755 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
756 "read checkpoint validity");
757 ok = yaffs2_rd_checkpt_validity_marker(dev, 1);
760 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
761 "read checkpoint device");
762 ok = yaffs2_rd_checkpt_dev(dev);
765 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
766 "read checkpoint objects");
767 ok = yaffs2_rd_checkpt_objs(dev);
770 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
771 "read checkpoint validity");
772 ok = yaffs2_rd_checkpt_validity_marker(dev, 0);
776 ok = yaffs2_rd_checkpt_sum(dev);
777 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
778 "read checkpoint checksum %d", ok);
781 if (!yaffs_checkpt_close(dev))
785 dev->is_checkpointed = 1;
787 dev->is_checkpointed = 0;
793 void yaffs2_checkpt_invalidate(struct yaffs_dev *dev)
795 if (dev->is_checkpointed || dev->blocks_in_checkpt > 0) {
796 dev->is_checkpointed = 0;
797 yaffs2_checkpt_invalidate_stream(dev);
799 if (dev->param.sb_dirty_fn)
800 dev->param.sb_dirty_fn(dev);
803 int yaffs_checkpoint_save(struct yaffs_dev *dev)
806 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
807 "save entry: is_checkpointed %d",
808 dev->is_checkpointed);
810 yaffs_verify_objects(dev);
811 yaffs_verify_blocks(dev);
812 yaffs_verify_free_chunks(dev);
814 if (!dev->is_checkpointed) {
815 yaffs2_checkpt_invalidate(dev);
816 yaffs2_wr_checkpt_data(dev);
819 yaffs_trace(YAFFS_TRACE_CHECKPOINT | YAFFS_TRACE_MOUNT,
820 "save exit: is_checkpointed %d",
821 dev->is_checkpointed);
823 return dev->is_checkpointed;
826 int yaffs2_checkpt_restore(struct yaffs_dev *dev)
829 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
830 "restore entry: is_checkpointed %d",
831 dev->is_checkpointed);
833 retval = yaffs2_rd_checkpt_data(dev);
835 if (dev->is_checkpointed) {
836 yaffs_verify_objects(dev);
837 yaffs_verify_blocks(dev);
838 yaffs_verify_free_chunks(dev);
841 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
842 "restore exit: is_checkpointed %d",
843 dev->is_checkpointed);
848 int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size)
850 /* if new_size > old_file_size.
851 * We're going to be writing a hole.
852 * If the hole is small then write zeros otherwise write a start of hole marker.
855 loff_t old_file_size;
858 int result = YAFFS_OK;
859 struct yaffs_dev *dev = NULL;
861 u8 *local_buffer = NULL;
863 int small_increase_ok = 0;
868 if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
873 /* Bail out if not yaffs2 mode */
874 if (!dev->param.is_yaffs2)
877 old_file_size = obj->variant.file_variant.file_size;
879 if (new_size <= old_file_size)
882 increase = new_size - old_file_size;
884 if (increase < YAFFS_SMALL_HOLE_THRESHOLD * dev->data_bytes_per_chunk &&
885 yaffs_check_alloc_available(dev, YAFFS_SMALL_HOLE_THRESHOLD + 1))
891 local_buffer = yaffs_get_temp_buffer(dev, __LINE__);
894 /* fill hole with zero bytes */
895 int pos = old_file_size;
898 memset(local_buffer, 0, dev->data_bytes_per_chunk);
899 small_increase_ok = 1;
901 while (increase > 0 && small_increase_ok) {
902 this_write = increase;
903 if (this_write > dev->data_bytes_per_chunk)
904 this_write = dev->data_bytes_per_chunk;
906 yaffs_do_file_wr(obj, local_buffer, pos, this_write,
908 if (written == this_write) {
910 increase -= this_write;
912 small_increase_ok = 0;
916 yaffs_release_temp_buffer(dev, local_buffer, __LINE__);
918 /* If we were out of space then reverse any chunks we've added */
919 if (!small_increase_ok)
920 yaffs_resize_file_down(obj, old_file_size);
923 if (!small_increase_ok &&
925 obj->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
926 obj->parent->obj_id != YAFFS_OBJECTID_DELETED) {
927 /* Write a hole start header with the old file size */
928 yaffs_update_oh(obj, NULL, 0, 1, 0, NULL);
935 struct yaffs_block_index {
940 static int yaffs2_ybicmp(const void *a, const void *b)
942 int aseq = ((struct yaffs_block_index *)a)->seq;
943 int bseq = ((struct yaffs_block_index *)b)->seq;
944 int ablock = ((struct yaffs_block_index *)a)->block;
945 int bblock = ((struct yaffs_block_index *)b)->block;
947 return ablock - bblock;
952 int yaffs2_scan_backwards(struct yaffs_dev *dev)
954 struct yaffs_ext_tags tags;
965 enum yaffs_block_state state;
966 struct yaffs_obj *hard_list = NULL;
967 struct yaffs_block_info *bi;
969 struct yaffs_obj_hdr *oh;
970 struct yaffs_obj *in;
971 struct yaffs_obj *parent;
972 int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
980 int alloc_failed = 0;
982 struct yaffs_block_index *block_index = NULL;
983 int alt_block_index = 0;
985 yaffs_trace(YAFFS_TRACE_SCAN,
986 "yaffs2_scan_backwards starts intstartblk %d intendblk %d...",
987 dev->internal_start_block, dev->internal_end_block);
989 dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
991 block_index = kmalloc(n_blocks * sizeof(struct yaffs_block_index),
996 vmalloc(n_blocks * sizeof(struct yaffs_block_index));
1001 yaffs_trace(YAFFS_TRACE_SCAN,
1002 "yaffs2_scan_backwards() could not allocate block index!"
1007 dev->blocks_in_checkpt = 0;
1009 chunk_data = yaffs_get_temp_buffer(dev, __LINE__);
1011 /* Scan all the blocks to determine their state */
1012 bi = dev->block_info;
1013 for (blk = dev->internal_start_block; blk <= dev->internal_end_block;
1015 yaffs_clear_chunk_bits(dev, blk);
1016 bi->pages_in_use = 0;
1017 bi->soft_del_pages = 0;
1019 yaffs_query_init_block_state(dev, blk, &state, &seq_number);
1021 bi->block_state = state;
1022 bi->seq_number = seq_number;
1024 if (bi->seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA)
1025 bi->block_state = state = YAFFS_BLOCK_STATE_CHECKPOINT;
1026 if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
1027 bi->block_state = state = YAFFS_BLOCK_STATE_DEAD;
1029 yaffs_trace(YAFFS_TRACE_SCAN_DEBUG,
1030 "Block scanning block %d state %d seq %d",
1031 blk, state, seq_number);
1033 if (state == YAFFS_BLOCK_STATE_CHECKPOINT) {
1034 dev->blocks_in_checkpt++;
1036 } else if (state == YAFFS_BLOCK_STATE_DEAD) {
1037 yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
1038 "block %d is bad", blk);
1039 } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
1040 yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "Block empty ");
1041 dev->n_erased_blocks++;
1042 dev->n_free_chunks += dev->param.chunks_per_block;
1043 } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
1045 /* Determine the highest sequence number */
1046 if (seq_number >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
1047 seq_number < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
1049 block_index[n_to_scan].seq = seq_number;
1050 block_index[n_to_scan].block = blk;
1054 if (seq_number >= dev->seq_number)
1055 dev->seq_number = seq_number;
1057 /* TODO: Nasty sequence number! */
1058 yaffs_trace(YAFFS_TRACE_SCAN,
1059 "Block scanning block %d has bad sequence number %d",
1067 yaffs_trace(YAFFS_TRACE_SCAN, "%d blocks to be sorted...", n_to_scan);
1071 /* Sort the blocks by sequence number */
1072 sort(block_index, n_to_scan, sizeof(struct yaffs_block_index),
1073 yaffs2_ybicmp, NULL);
1077 yaffs_trace(YAFFS_TRACE_SCAN, "...done");
1079 /* Now scan the blocks looking at the data. */
1081 end_iter = n_to_scan - 1;
1082 yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "%d blocks to scan", n_to_scan);
1084 /* For each block.... backwards */
1085 for (block_iter = end_iter; !alloc_failed && block_iter >= start_iter;
1087 /* Cooperative multitasking! This loop can run for so
1088 long that watchdog timers expire. */
1091 /* get the block to scan in the correct order */
1092 blk = block_index[block_iter].block;
1094 bi = yaffs_get_block_info(dev, blk);
1096 state = bi->block_state;
1100 /* For each chunk in each block that needs scanning.... */
1102 for (c = dev->param.chunks_per_block - 1;
1103 !alloc_failed && c >= 0 &&
1104 (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
1105 state == YAFFS_BLOCK_STATE_ALLOCATING); c--) {
1106 /* Scan backwards...
1107 * Read the tags and decide what to do
1110 chunk = blk * dev->param.chunks_per_block + c;
1112 result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL,
1115 /* Let's have a good look at this chunk... */
1117 if (!tags.chunk_used) {
1118 /* An unassigned chunk in the block.
1119 * If there are used chunks after this one, then
1120 * it is a chunk that was skipped due to failing the erased
1121 * check. Just skip it so that it can be deleted.
1122 * But, more typically, We get here when this is an unallocated
1123 * chunk and his means that either the block is empty or
1124 * this is the one being allocated from
1128 /* This is a chunk that was skipped due to failing the erased check */
1129 } else if (c == 0) {
1130 /* We're looking at the first chunk in the block so the block is unused */
1131 state = YAFFS_BLOCK_STATE_EMPTY;
1132 dev->n_erased_blocks++;
1135 YAFFS_BLOCK_STATE_NEEDS_SCANNING
1137 YAFFS_BLOCK_STATE_ALLOCATING) {
1138 if (dev->seq_number ==
1140 /* this is the block being allocated from */
1142 yaffs_trace(YAFFS_TRACE_SCAN,
1143 " Allocating from %d %d",
1147 YAFFS_BLOCK_STATE_ALLOCATING;
1148 dev->alloc_block = blk;
1149 dev->alloc_page = c;
1151 alloc_block_finder =
1154 /* This is a partially written block that is not
1155 * the current allocation block.
1158 yaffs_trace(YAFFS_TRACE_SCAN,
1159 "Partially written block %d detected",
1165 dev->n_free_chunks++;
1167 } else if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED) {
1168 yaffs_trace(YAFFS_TRACE_SCAN,
1169 " Unfixed ECC in chunk(%d:%d), chunk ignored",
1172 dev->n_free_chunks++;
1174 } else if (tags.obj_id > YAFFS_MAX_OBJECT_ID ||
1175 tags.chunk_id > YAFFS_MAX_CHUNK_ID ||
1177 && tags.n_bytes > dev->data_bytes_per_chunk)
1178 || tags.seq_number != bi->seq_number) {
1179 yaffs_trace(YAFFS_TRACE_SCAN,
1180 "Chunk (%d:%d) with bad tags:obj = %d, chunk_id = %d, n_bytes = %d, ignored",
1181 blk, c, tags.obj_id,
1182 tags.chunk_id, tags.n_bytes);
1184 dev->n_free_chunks++;
1186 } else if (tags.chunk_id > 0) {
1187 /* chunk_id > 0 so it is a data chunk... */
1188 unsigned int endpos;
1191 1) * dev->data_bytes_per_chunk;
1195 yaffs_set_chunk_bit(dev, blk, c);
1198 in = yaffs_find_or_create_by_number(dev,
1200 YAFFS_OBJECT_TYPE_FILE);
1207 in->variant_type == YAFFS_OBJECT_TYPE_FILE
1209 in->variant.file_variant.shrink_size) {
1210 /* This has not been invalidated by a resize */
1211 if (!yaffs_put_chunk_in_file
1212 (in, tags.chunk_id, chunk, -1)) {
1216 /* File size is calculated by looking at the data chunks if we have not
1217 * seen an object header yet. Stop this practice once we find an object header.
1219 endpos = chunk_base + tags.n_bytes;
1221 if (!in->valid && /* have not got an object header yet */
1222 in->variant.file_variant.
1223 scanned_size < endpos) {
1224 in->variant.file_variant.
1225 scanned_size = endpos;
1226 in->variant.file_variant.
1231 /* This chunk has been invalidated by a resize, or a past file deletion
1232 * so delete the chunk*/
1233 yaffs_chunk_del(dev, chunk, 1,
1238 /* chunk_id == 0, so it is an ObjectHeader.
1239 * Thus, we read in the object header and make the object
1243 yaffs_set_chunk_bit(dev, blk, c);
1249 if (tags.extra_available) {
1250 in = yaffs_find_or_create_by_number(dev,
1260 (!in->valid && dev->param.disable_lazy_load)
1261 || tags.extra_shadows || (!in->valid
1266 YAFFS_OBJECTID_LOSTNFOUND)))
1269 /* If we don't have valid info then we need to read the chunk
1270 * TODO In future we can probably defer reading the chunk and
1271 * living with invalid data until needed.
1274 result = yaffs_rd_chunk_tags_nand(dev,
1279 oh = (struct yaffs_obj_hdr *)chunk_data;
1281 if (dev->param.inband_tags) {
1282 /* Fix up the header if they got corrupted by inband tags */
1284 oh->inband_shadowed_obj_id;
1286 oh->inband_is_shrink;
1290 in = yaffs_find_or_create_by_number(dev, tags.obj_id, oh->type);
1298 /* TODO Hoosterman we have a problem! */
1299 yaffs_trace(YAFFS_TRACE_ERROR,
1300 "yaffs tragedy: Could not make object for object %d at chunk %d during scan",
1301 tags.obj_id, chunk);
1306 /* We have already filled this one.
1307 * We have a duplicate that will be discarded, but
1308 * we first have to suck out resize info if it is a file.
1311 if ((in->variant_type ==
1312 YAFFS_OBJECT_TYPE_FILE) && ((oh
1317 YAFFS_OBJECT_TYPE_FILE)
1325 YAFFS_OBJECT_TYPE_FILE)))
1332 (oh) ? oh->parent_obj_id :
1333 tags.extra_parent_id;
1338 tags.extra_is_shrink;
1340 /* If it is deleted (unlinked at start also means deleted)
1341 * we treat the file size as being zeroed at this point.
1343 if (parent_obj_id ==
1344 YAFFS_OBJECTID_DELETED
1346 YAFFS_OBJECTID_UNLINKED) {
1352 && in->variant.file_variant.
1353 shrink_size > this_size)
1360 bi->has_shrink_hdr = 1;
1363 /* Use existing - destroy this one. */
1364 yaffs_chunk_del(dev, chunk, 1,
1369 if (!in->valid && in->variant_type !=
1370 (oh ? oh->type : tags.extra_obj_type))
1371 yaffs_trace(YAFFS_TRACE_ERROR,
1372 "yaffs tragedy: Bad object type, %d != %d, for object %d at chunk %d during scan",
1374 oh->type : tags.extra_obj_type,
1375 in->variant_type, tags.obj_id,
1379 (tags.obj_id == YAFFS_OBJECTID_ROOT ||
1381 YAFFS_OBJECTID_LOSTNFOUND)) {
1382 /* We only load some info, don't fiddle with directory structure */
1387 in->yst_mode = oh->yst_mode;
1388 yaffs_load_attribs(in, oh);
1389 in->lazy_loaded = 0;
1391 in->lazy_loaded = 1;
1393 in->hdr_chunk = chunk;
1395 } else if (!in->valid) {
1396 /* we need to load this info */
1399 in->hdr_chunk = chunk;
1402 in->variant_type = oh->type;
1404 in->yst_mode = oh->yst_mode;
1405 yaffs_load_attribs(in, oh);
1407 if (oh->shadows_obj > 0)
1408 yaffs_handle_shadowed_obj
1413 yaffs_set_obj_name_from_oh(in,
1416 yaffs_find_or_create_by_number
1417 (dev, oh->parent_obj_id,
1418 YAFFS_OBJECT_TYPE_DIRECTORY);
1420 file_size = oh->file_size;
1421 is_shrink = oh->is_shrink;
1422 equiv_id = oh->equiv_id;
1426 tags.extra_obj_type;
1428 yaffs_find_or_create_by_number
1429 (dev, tags.extra_parent_id,
1430 YAFFS_OBJECT_TYPE_DIRECTORY);
1431 file_size = tags.extra_length;
1433 tags.extra_is_shrink;
1434 equiv_id = tags.extra_equiv_id;
1435 in->lazy_loaded = 1;
1443 /* directory stuff...
1447 if (parent && parent->variant_type ==
1448 YAFFS_OBJECT_TYPE_UNKNOWN) {
1449 /* Set up as a directory */
1450 parent->variant_type =
1451 YAFFS_OBJECT_TYPE_DIRECTORY;
1452 INIT_LIST_HEAD(&parent->
1453 variant.dir_variant.children);
1455 || parent->variant_type !=
1456 YAFFS_OBJECT_TYPE_DIRECTORY) {
1457 /* Hoosterman, another problem....
1458 * We're trying to use a non-directory as a directory
1461 yaffs_trace(YAFFS_TRACE_ERROR,
1462 "yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
1464 parent = dev->lost_n_found;
1467 yaffs_add_obj_to_dir(parent, in);
1469 is_unlinked = (parent == dev->del_dir)
1470 || (parent == dev->unlinked_dir);
1473 /* Mark the block as having a shrink header */
1474 bi->has_shrink_hdr = 1;
1477 /* Note re hardlinks.
1478 * Since we might scan a hardlink before its equivalent object is scanned
1479 * we put them all in a list.
1480 * After scanning is complete, we should have all the objects, so we run
1481 * through this list and fix up all the chains.
1484 switch (in->variant_type) {
1485 case YAFFS_OBJECT_TYPE_UNKNOWN:
1486 /* Todo got a problem */
1488 case YAFFS_OBJECT_TYPE_FILE:
1491 file_variant.scanned_size <
1493 /* This covers the case where the file size is greater
1494 * than where the data is
1495 * This will happen if the file is resized to be larger
1496 * than its current data extents.
1508 if (in->variant.file_variant.
1509 shrink_size > file_size)
1516 case YAFFS_OBJECT_TYPE_HARDLINK:
1520 equiv_id = equiv_id;
1521 in->hard_links.next =
1522 (struct list_head *)
1527 case YAFFS_OBJECT_TYPE_DIRECTORY:
1530 case YAFFS_OBJECT_TYPE_SPECIAL:
1533 case YAFFS_OBJECT_TYPE_SYMLINK:
1538 yaffs_clone_str(oh->
1553 } /* End of scanning for each chunk */
1555 if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
1556 /* If we got this far while scanning, then the block is fully allocated. */
1557 state = YAFFS_BLOCK_STATE_FULL;
1560 bi->block_state = state;
1562 /* Now let's see if it was dirty */
1563 if (bi->pages_in_use == 0 &&
1564 !bi->has_shrink_hdr &&
1565 bi->block_state == YAFFS_BLOCK_STATE_FULL) {
1566 yaffs_block_became_dirty(dev, blk);
1571 yaffs_skip_rest_of_block(dev);
1573 if (alt_block_index)
1578 /* Ok, we've done all the scanning.
1579 * Fix up the hard link chains.
1580 * We should now have scanned all the objects, now it's time to add these
1583 yaffs_link_fixup(dev, hard_list);
1585 yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
1590 yaffs_trace(YAFFS_TRACE_SCAN, "yaffs2_scan_backwards ends");