d5b167ffcee942820405576590319bf35e685975
[yaffs2.git] / yaffs_yaffs2.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2010 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
7  * Created by Charles Manning <charles@aleph1.co.uk>
8  *
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.
12  */
13
14
15 #include "yaffs_guts.h"
16 #include "yaffs_trace.h"
17 #include "yaffs_yaffs2.h"
18 #include "yaffs_checkptrw.h"
19 #include "yaffs_bitmap.h"
20 #include "yaffs_nand.h"
21 #include "yaffs_getblockinfo.h"
22 #include "yaffs_verify.h"
23
24 /*
25  * Checkpoints are really no benefit on very small partitions.
26  *
27  * To save space on small partitions don't bother with checkpoints unless
28  * the partition is at least this big.
29  */
30 #define YAFFS_CHECKPOINT_MIN_BLOCKS 60
31
32 #define YAFFS_SMALL_HOLE_THRESHOLD 4
33
34
35 /*
36  * Oldest Dirty Sequence Number handling.
37  */
38  
39 /* yaffs_calc_oldest_dirty_seq()
40  * yaffs2_find_oldest_dirty_seq()
41  * Calculate the oldest dirty sequence number if we don't know it.
42  */
43 void yaffs_calc_oldest_dirty_seq(struct yaffs_dev *dev)
44 {
45         int i;
46         unsigned seq;
47         unsigned block_no = 0;
48         struct yaffs_block_info *b;
49
50         if(!dev->param.is_yaffs2)
51                 return;
52
53         /* Find the oldest dirty sequence number. */
54         seq = dev->seq_number + 1;
55         b = dev->block_info;
56         for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
57                 if (b->block_state == YAFFS_BLOCK_STATE_FULL &&
58                         (b->pages_in_use - b->soft_del_pages) < dev->param.chunks_per_block &&
59                         b->seq_number < seq) {
60                         seq = b->seq_number;
61                         block_no = i;
62                 }
63                 b++;
64         }
65
66         if(block_no){
67                 dev->oldest_dirty_seq = seq;
68                 dev->oldest_dirty_block = block_no;
69         }
70
71 }
72
73
74 void yaffs2_find_oldest_dirty_seq(struct yaffs_dev *dev)
75 {
76         if(!dev->param.is_yaffs2)
77                 return;
78
79         if(!dev->oldest_dirty_seq)
80                 yaffs_calc_oldest_dirty_seq(dev);
81 }
82
83 /*
84  * yaffs_clear_oldest_dirty_seq()
85  * Called when a block is erased or marked bad. (ie. when its seq_number
86  * becomes invalid). If the value matches the oldest then we clear 
87  * dev->oldest_dirty_seq to force its recomputation.
88  */
89 void yaffs2_clear_oldest_dirty_seq(struct yaffs_dev *dev, struct yaffs_block_info *bi)
90 {
91
92         if(!dev->param.is_yaffs2)
93                 return;
94
95         if(!bi || bi->seq_number == dev->oldest_dirty_seq){
96                 dev->oldest_dirty_seq = 0;
97                 dev->oldest_dirty_block = 0;
98         }
99 }
100
101 /*
102  * yaffs2_update_oldest_dirty_seq()
103  * Update the oldest dirty sequence number whenever we dirty a block.
104  * Only do this if the oldest_dirty_seq is actually being tracked.
105  */
106 void yaffs2_update_oldest_dirty_seq(struct yaffs_dev *dev, unsigned block_no, struct yaffs_block_info *bi)
107 {
108         if(!dev->param.is_yaffs2)
109                 return;
110
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;
115                 }
116         }
117 }
118
119 int yaffs_block_ok_for_gc(struct yaffs_dev *dev,
120                                         struct yaffs_block_info *bi)
121 {
122
123         if (!dev->param.is_yaffs2)
124                 return 1;       /* disqualification only applies to yaffs2. */
125
126         if (!bi->has_shrink_hdr)
127                 return 1;       /* can gc */
128
129         yaffs2_find_oldest_dirty_seq(dev);
130
131         /* Can't do gc of this block if there are any blocks older than this one that have
132          * discarded pages.
133          */
134         return (bi->seq_number <= dev->oldest_dirty_seq);
135 }
136
137 /*
138  * yaffs2_find_refresh_block()
139  * periodically finds the oldest full block by sequence number for refreshing.
140  * Only for yaffs2.
141  */
142 u32 yaffs2_find_refresh_block(struct yaffs_dev *dev)
143 {
144         u32 b ;
145
146         u32 oldest = 0;
147         u32 oldest_seq = 0;
148
149         struct yaffs_block_info *bi;
150
151         if(!dev->param.is_yaffs2)
152                 return oldest;
153
154         /*
155          * If refresh period < 10 then refreshing is disabled.
156          */
157         if(dev->param.refresh_period < 10)
158                 return oldest;
159
160         /*
161          * Fix broken values.
162          */
163         if(dev->refresh_skip > dev->param.refresh_period)
164                 dev->refresh_skip = dev->param.refresh_period;
165
166         if(dev->refresh_skip > 0)
167                 return oldest;
168
169         /*
170          * Refresh skip is now zero.
171          * We'll do a refresh this time around....
172          * Update the refresh skip and find the oldest block.
173          */
174         dev->refresh_skip = dev->param.refresh_period;
175         dev->refresh_count++;
176         bi = dev->block_info;
177         for (b = dev->internal_start_block; b <=dev->internal_end_block; b++){
178
179                 if (bi->block_state == YAFFS_BLOCK_STATE_FULL){
180
181                         if(oldest < 1 ||
182                                 bi->seq_number < oldest_seq){
183                                 oldest = b;
184                                 oldest_seq = bi->seq_number;
185                         }
186                 }
187                 bi++;
188         }
189
190         if (oldest > 0) {
191                 T(YAFFS_TRACE_GC,
192                   (TSTR("GC refresh count %d selected block %d with seq_number %d" TENDSTR),
193                    dev->refresh_count, oldest, oldest_seq));
194         }
195
196         return oldest;
197 }
198
199 int yaffs2_checkpt_required(struct yaffs_dev *dev)
200 {
201         int nblocks;
202         
203         if(!dev->param.is_yaffs2)
204                 return 0;
205         
206         nblocks = dev->internal_end_block - dev->internal_start_block + 1 ;
207
208         return  !dev->param.skip_checkpt_wr &&
209                 !dev->read_only &&
210                 (nblocks >= YAFFS_CHECKPOINT_MIN_BLOCKS);
211 }
212
213 int yaffs_calc_checkpt_blocks_required(struct yaffs_dev *dev)
214 {
215         int retval;
216
217         if(!dev->param.is_yaffs2)
218                 return 0;
219
220         if (!dev->checkpoint_blocks_required &&
221                 yaffs2_checkpt_required(dev)){
222                 /* Not a valid value so recalculate */
223                 int n_bytes = 0;
224                 int n_blocks;
225                 int dev_blocks = (dev->param.end_block - dev->param.start_block + 1);
226
227                 n_bytes += sizeof(struct yaffs_checkpt_validity);
228                 n_bytes += sizeof(struct yaffs_checkpt_dev);
229                 n_bytes += dev_blocks * sizeof(struct yaffs_block_info);
230                 n_bytes += dev_blocks * dev->chunk_bit_stride;
231                 n_bytes += (sizeof(struct yaffs_checkpt_obj) + 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*/
235
236                 /* Round up and add 2 blocks to allow for some bad blocks, so add 3 */
237
238                 n_blocks = (n_bytes/(dev->data_bytes_per_chunk * dev->param.chunks_per_block)) + 3;
239
240                 dev->checkpoint_blocks_required = n_blocks;
241         }
242
243         retval = dev->checkpoint_blocks_required - dev->blocks_in_checkpt;
244         if(retval < 0)
245                 retval = 0;
246         return retval;
247 }
248
249 /*--------------------- Checkpointing --------------------*/
250
251
252 static int yaffs2_wr_checkpt_validity_marker(struct yaffs_dev *dev, int head)
253 {
254         struct yaffs_checkpt_validity cp;
255
256         memset(&cp, 0, sizeof(cp));
257
258         cp.struct_type = sizeof(cp);
259         cp.magic = YAFFS_MAGIC;
260         cp.version = YAFFS_CHECKPOINT_VERSION;
261         cp.head = (head) ? 1 : 0;
262
263         return (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp)) ?
264                 1 : 0;
265 }
266
267 static int yaffs2_rd_checkpt_validity_marker(struct yaffs_dev *dev, int head)
268 {
269         struct yaffs_checkpt_validity cp;
270         int ok;
271
272         ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
273
274         if (ok)
275                 ok = (cp.struct_type == sizeof(cp)) &&
276                      (cp.magic == YAFFS_MAGIC) &&
277                      (cp.version == YAFFS_CHECKPOINT_VERSION) &&
278                      (cp.head == ((head) ? 1 : 0));
279         return ok ? 1 : 0;
280 }
281
282 static void yaffs2_dev_to_checkpt_dev(struct yaffs_checkpt_dev *cp,
283                                            struct yaffs_dev *dev)
284 {
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;
289
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;
294
295 }
296
297 static void yaffs_checkpt_dev_to_dev(struct yaffs_dev *dev,
298                                    struct yaffs_checkpt_dev *cp)
299 {
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;
304
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;
309 }
310
311
312 static int yaffs2_wr_checkpt_dev(struct yaffs_dev *dev)
313 {
314         struct yaffs_checkpt_dev cp;
315         u32 n_bytes;
316         u32 n_blocks = (dev->internal_end_block - dev->internal_start_block + 1);
317
318         int ok;
319
320         /* Write device runtime values*/
321         yaffs2_dev_to_checkpt_dev(&cp, dev);
322         cp.struct_type = sizeof(cp);
323
324         ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
325
326         /* Write block info */
327         if (ok) {
328                 n_bytes = n_blocks * sizeof(struct yaffs_block_info);
329                 ok = (yaffs2_checkpt_wr(dev, dev->block_info, n_bytes) == n_bytes);
330         }
331
332         /* Write chunk bits */
333         if (ok) {
334                 n_bytes = n_blocks * dev->chunk_bit_stride;
335                 ok = (yaffs2_checkpt_wr(dev, dev->chunk_bits, n_bytes) == n_bytes);
336         }
337         return   ok ? 1 : 0;
338
339 }
340
341 static int yaffs2_rd_checkpt_dev(struct yaffs_dev *dev)
342 {
343         struct yaffs_checkpt_dev cp;
344         u32 n_bytes;
345         u32 n_blocks = (dev->internal_end_block - dev->internal_start_block + 1);
346
347         int ok;
348
349         ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
350         if (!ok)
351                 return 0;
352
353         if (cp.struct_type != sizeof(cp))
354                 return 0;
355
356
357         yaffs_checkpt_dev_to_dev(dev, &cp);
358
359         n_bytes = n_blocks * sizeof(struct yaffs_block_info);
360
361         ok = (yaffs2_checkpt_rd(dev, dev->block_info, n_bytes) == n_bytes);
362
363         if (!ok)
364                 return 0;
365         n_bytes = n_blocks * dev->chunk_bit_stride;
366
367         ok = (yaffs2_checkpt_rd(dev, dev->chunk_bits, n_bytes) == n_bytes);
368
369         return ok ? 1 : 0;
370 }
371
372 static void yaffs2_obj_checkpt_obj(struct yaffs_checkpt_obj *cp,
373                                            struct yaffs_obj *obj)
374 {
375
376         cp->obj_id = obj->obj_id;
377         cp->parent_id = (obj->parent) ? obj->parent->obj_id : 0;
378         cp->hdr_chunk = obj->hdr_chunk;
379         cp->variant_type = obj->variant_type;
380         cp->deleted = obj->deleted;
381         cp->soft_del = obj->soft_del;
382         cp->unlinked = obj->unlinked;
383         cp->fake = obj->fake;
384         cp->rename_allowed = obj->rename_allowed;
385         cp->unlink_allowed = obj->unlink_allowed;
386         cp->serial = obj->serial;
387         cp->n_data_chunks = obj->n_data_chunks;
388
389         if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
390                 cp->size_or_equiv_obj = obj->variant.file_variant.file_size;
391         else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK)
392                 cp->size_or_equiv_obj = obj->variant.hardlink_variant.equiv_id;
393 }
394
395 static int taffs2_checkpt_obj_to_obj(struct yaffs_obj *obj, struct yaffs_checkpt_obj *cp)
396 {
397
398         struct yaffs_obj *parent;
399
400         if (obj->variant_type != cp->variant_type) {
401                 T(YAFFS_TRACE_ERROR, (TSTR("Checkpoint read object %d type %d "
402                         TCONT("chunk %d does not match existing object type %d")
403                         TENDSTR), cp->obj_id, cp->variant_type, cp->hdr_chunk,
404                         obj->variant_type));
405                 return 0;
406         }
407
408         obj->obj_id = cp->obj_id;
409
410         if (cp->parent_id)
411                 parent = yaffs_find_or_create_by_number(
412                                         obj->my_dev,
413                                         cp->parent_id,
414                                         YAFFS_OBJECT_TYPE_DIRECTORY);
415         else
416                 parent = NULL;
417
418         if (parent) {
419                 if (parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
420                         T(YAFFS_TRACE_ALWAYS, (TSTR("Checkpoint read object %d parent %d type %d"
421                                 TCONT(" chunk %d Parent type, %d, not directory")
422                                 TENDSTR),
423                                 cp->obj_id, cp->parent_id, cp->variant_type,
424                                 cp->hdr_chunk, parent->variant_type));
425                         return 0;
426                 }
427                 yaffs_add_obj_to_dir(parent, obj);
428         }
429
430         obj->hdr_chunk = cp->hdr_chunk;
431         obj->variant_type = cp->variant_type;
432         obj->deleted = cp->deleted;
433         obj->soft_del = cp->soft_del;
434         obj->unlinked = cp->unlinked;
435         obj->fake = cp->fake;
436         obj->rename_allowed = cp->rename_allowed;
437         obj->unlink_allowed = cp->unlink_allowed;
438         obj->serial = cp->serial;
439         obj->n_data_chunks = cp->n_data_chunks;
440
441         if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
442                 obj->variant.file_variant.file_size = cp->size_or_equiv_obj;
443         else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK)
444                 obj->variant.hardlink_variant.equiv_id = cp->size_or_equiv_obj;
445
446         if (obj->hdr_chunk > 0)
447                 obj->lazy_loaded = 1;
448         return 1;
449 }
450
451
452
453 static int yaffs2_checkpt_tnode_worker(struct yaffs_obj *in, struct yaffs_tnode *tn,
454                                         u32 level, int chunk_offset)
455 {
456         int i;
457         struct yaffs_dev *dev = in->my_dev;
458         int ok = 1;
459
460         if (tn) {
461                 if (level > 0) {
462
463                         for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
464                                 if (tn->internal[i]) {
465                                         ok = yaffs2_checkpt_tnode_worker(in,
466                                                         tn->internal[i],
467                                                         level - 1,
468                                                         (chunk_offset<<YAFFS_TNODES_INTERNAL_BITS) + i);
469                                 }
470                         }
471                 } else if (level == 0) {
472                         u32 base_offset = chunk_offset <<  YAFFS_TNODES_LEVEL0_BITS;
473                         ok = (yaffs2_checkpt_wr(dev, &base_offset, sizeof(base_offset)) == 
474                                 sizeof(base_offset));
475                         if (ok)
476                                 ok = (yaffs2_checkpt_wr(dev, tn, dev->tnode_size) == dev->tnode_size);
477                 }
478         }
479
480         return ok;
481
482 }
483
484 static int yaffs2_wr_checkpt_tnodes(struct yaffs_obj *obj)
485 {
486         u32 end_marker = ~0;
487         int ok = 1;
488
489         if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE) {
490                 ok = yaffs2_checkpt_tnode_worker(obj,
491                                             obj->variant.file_variant.top,
492                                             obj->variant.file_variant.top_level,
493                                             0);
494                 if (ok)
495                         ok = (yaffs2_checkpt_wr(obj->my_dev, &end_marker, sizeof(end_marker)) ==
496                                 sizeof(end_marker));
497         }
498
499         return ok ? 1 : 0;
500 }
501
502 static int yaffs2_rd_checkpt_tnodes(struct yaffs_obj *obj)
503 {
504         u32 base_chunk;
505         int ok = 1;
506         struct yaffs_dev *dev = obj->my_dev;
507         struct yaffs_file_var *file_stuct_ptr = &obj->variant.file_variant;
508         struct yaffs_tnode *tn;
509         int nread = 0;
510
511         ok = (yaffs2_checkpt_rd(dev, &base_chunk, sizeof(base_chunk)) == sizeof(base_chunk));
512
513         while (ok && (~base_chunk)) {
514                 nread++;
515                 /* Read level 0 tnode */
516
517
518                 tn = yaffs_get_tnode(dev);
519                 if (tn){
520                         ok = (yaffs2_checkpt_rd(dev, tn, dev->tnode_size) == dev->tnode_size);
521                 } else
522                         ok = 0;
523
524                 if (tn && ok)
525                         ok = yaffs_add_find_tnode_0(dev,
526                                                         file_stuct_ptr,
527                                                         base_chunk,
528                                                         tn) ? 1 : 0;
529
530                 if (ok)
531                         ok = (yaffs2_checkpt_rd(dev, &base_chunk, sizeof(base_chunk)) == sizeof(base_chunk));
532
533         }
534
535         T(YAFFS_TRACE_CHECKPOINT, (
536                 TSTR("Checkpoint read tnodes %d records, last %d. ok %d" TENDSTR),
537                 nread, base_chunk, ok));
538
539         return ok ? 1 : 0;
540 }
541
542
543 static int yaffs2_wr_checkpt_objs(struct yaffs_dev *dev)
544 {
545         struct yaffs_obj *obj;
546         struct yaffs_checkpt_obj cp;
547         int i;
548         int ok = 1;
549         struct list_head *lh;
550
551
552         /* Iterate through the objects in each hash entry,
553          * dumping them to the checkpointing stream.
554          */
555
556         for (i = 0; ok &&  i <  YAFFS_NOBJECT_BUCKETS; i++) {
557                 list_for_each(lh, &dev->obj_bucket[i].list) {
558                         if (lh) {
559                                 obj = list_entry(lh, struct yaffs_obj, hash_link);
560                                 if (!obj->defered_free) {
561                                         yaffs2_obj_checkpt_obj(&cp, obj);
562                                         cp.struct_type = sizeof(cp);
563
564                                         T(YAFFS_TRACE_CHECKPOINT, (
565                                                 TSTR("Checkpoint write object %d parent %d type %d chunk %d obj addr %p" TENDSTR),
566                                                 cp.obj_id, cp.parent_id, cp.variant_type, cp.hdr_chunk, obj));
567
568                                         ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
569
570                                         if (ok && obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
571                                                 ok = yaffs2_wr_checkpt_tnodes(obj);
572                                 }
573                         }
574                 }
575         }
576
577         /* Dump end of list */
578         memset(&cp, 0xFF, sizeof(struct yaffs_checkpt_obj));
579         cp.struct_type = sizeof(cp);
580
581         if (ok)
582                 ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
583
584         return ok ? 1 : 0;
585 }
586
587 static int yaffs2_rd_checkpt_objs(struct yaffs_dev *dev)
588 {
589         struct yaffs_obj *obj;
590         struct yaffs_checkpt_obj cp;
591         int ok = 1;
592         int done = 0;
593         struct yaffs_obj *hard_list = NULL;
594
595         while (ok && !done) {
596                 ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
597                 if (cp.struct_type != sizeof(cp)) {
598                         T(YAFFS_TRACE_CHECKPOINT, (TSTR("struct size %d instead of %d ok %d"TENDSTR),
599                                 cp.struct_type, (int)sizeof(cp), ok));
600                         ok = 0;
601                 }
602
603                 T(YAFFS_TRACE_CHECKPOINT, (TSTR("Checkpoint read object %d parent %d type %d chunk %d " TENDSTR),
604                         cp.obj_id, cp.parent_id, cp.variant_type, cp.hdr_chunk));
605
606                 if (ok && cp.obj_id == ~0)
607                         done = 1;
608                 else if (ok) {
609                         obj = yaffs_find_or_create_by_number(dev, cp.obj_id, cp.variant_type);
610                         if (obj) {
611                                 ok = taffs2_checkpt_obj_to_obj(obj, &cp);
612                                 if (!ok)
613                                         break;
614                                 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE) {
615                                         ok = yaffs2_rd_checkpt_tnodes(obj);
616                                 } else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
617                                         obj->hard_links.next =
618                                                 (struct list_head *) hard_list;
619                                         hard_list = obj;
620                                 }
621                         } else
622                                 ok = 0;
623                 }
624         }
625
626         if (ok)
627                 yaffs_link_fixup(dev, hard_list);
628
629         return ok ? 1 : 0;
630 }
631
632 static int yaffs2_wr_checkpt_sum(struct yaffs_dev *dev)
633 {
634         u32 checkpt_sum;
635         int ok;
636
637         yaffs2_get_checkpt_sum(dev, &checkpt_sum);
638
639         ok = (yaffs2_checkpt_wr(dev, &checkpt_sum, sizeof(checkpt_sum)) == sizeof(checkpt_sum));
640
641         if (!ok)
642                 return 0;
643
644         return 1;
645 }
646
647 static int yaffs2_rd_checkpt_sum(struct yaffs_dev *dev)
648 {
649         u32 checkpt_sum0;
650         u32 checkpt_sum1;
651         int ok;
652
653         yaffs2_get_checkpt_sum(dev, &checkpt_sum0);
654
655         ok = (yaffs2_checkpt_rd(dev, &checkpt_sum1, sizeof(checkpt_sum1)) == sizeof(checkpt_sum1));
656
657         if (!ok)
658                 return 0;
659
660         if (checkpt_sum0 != checkpt_sum1)
661                 return 0;
662
663         return 1;
664 }
665
666
667 static int yaffs2_wr_checkpt_data(struct yaffs_dev *dev)
668 {
669         int ok = 1;
670
671         if (!yaffs2_checkpt_required(dev)) {
672                 T(YAFFS_TRACE_CHECKPOINT, (TSTR("skipping checkpoint write" TENDSTR)));
673                 ok = 0;
674         }
675
676         if (ok)
677                 ok = yaffs2_checkpt_open(dev, 1);
678
679         if (ok) {
680                 T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint validity" TENDSTR)));
681                 ok = yaffs2_wr_checkpt_validity_marker(dev, 1);
682         }
683         if (ok) {
684                 T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint device" TENDSTR)));
685                 ok = yaffs2_wr_checkpt_dev(dev);
686         }
687         if (ok) {
688                 T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint objects" TENDSTR)));
689                 ok = yaffs2_wr_checkpt_objs(dev);
690         }
691         if (ok) {
692                 T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint validity" TENDSTR)));
693                 ok = yaffs2_wr_checkpt_validity_marker(dev, 0);
694         }
695
696         if (ok)
697                 ok = yaffs2_wr_checkpt_sum(dev);
698
699         if (!yaffs_checkpt_close(dev))
700                 ok = 0;
701
702         if (ok)
703                 dev->is_checkpointed = 1;
704         else
705                 dev->is_checkpointed = 0;
706
707         return dev->is_checkpointed;
708 }
709
710 static int yaffs2_rd_checkpt_data(struct yaffs_dev *dev)
711 {
712         int ok = 1;
713         
714         if(!dev->param.is_yaffs2)
715                 ok = 0;
716
717         if (ok && dev->param.skip_checkpt_rd) {
718                 T(YAFFS_TRACE_CHECKPOINT, (TSTR("skipping checkpoint read" TENDSTR)));
719                 ok = 0;
720         }
721
722         if (ok)
723                 ok = yaffs2_checkpt_open(dev, 0); /* open for read */
724
725         if (ok) {
726                 T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint validity" TENDSTR)));
727                 ok = yaffs2_rd_checkpt_validity_marker(dev, 1);
728         }
729         if (ok) {
730                 T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint device" TENDSTR)));
731                 ok = yaffs2_rd_checkpt_dev(dev);
732         }
733         if (ok) {
734                 T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint objects" TENDSTR)));
735                 ok = yaffs2_rd_checkpt_objs(dev);
736         }
737         if (ok) {
738                 T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint validity" TENDSTR)));
739                 ok = yaffs2_rd_checkpt_validity_marker(dev, 0);
740         }
741
742         if (ok) {
743                 ok = yaffs2_rd_checkpt_sum(dev);
744                 T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint checksum %d" TENDSTR), ok));
745         }
746
747         if (!yaffs_checkpt_close(dev))
748                 ok = 0;
749
750         if (ok)
751                 dev->is_checkpointed = 1;
752         else
753                 dev->is_checkpointed = 0;
754
755         return ok ? 1 : 0;
756
757 }
758
759 void yaffs2_checkpt_invalidate(struct yaffs_dev *dev)
760 {
761         if (dev->is_checkpointed ||
762                         dev->blocks_in_checkpt > 0) {
763                 dev->is_checkpointed = 0;
764                 yaffs2_checkpt_invalidate_stream(dev);
765         }
766         if (dev->param.sb_dirty_fn)
767                 dev->param.sb_dirty_fn(dev);
768 }
769
770
771 int yaffs_checkpoint_save(struct yaffs_dev *dev)
772 {
773
774         T(YAFFS_TRACE_CHECKPOINT, (TSTR("save entry: is_checkpointed %d"TENDSTR), dev->is_checkpointed));
775
776         yaffs_verify_objects(dev);
777         yaffs_verify_blocks(dev);
778         yaffs_verify_free_chunks(dev);
779
780         if (!dev->is_checkpointed) {
781                 yaffs2_checkpt_invalidate(dev);
782                 yaffs2_wr_checkpt_data(dev);
783         }
784
785         T(YAFFS_TRACE_ALWAYS, (TSTR("save exit: is_checkpointed %d"TENDSTR), dev->is_checkpointed));
786
787         return dev->is_checkpointed;
788 }
789
790 int yaffs2_checkpt_restore(struct yaffs_dev *dev)
791 {
792         int retval;
793         T(YAFFS_TRACE_CHECKPOINT, (TSTR("restore entry: is_checkpointed %d"TENDSTR), dev->is_checkpointed));
794
795         retval = yaffs2_rd_checkpt_data(dev);
796
797         if (dev->is_checkpointed) {
798                 yaffs_verify_objects(dev);
799                 yaffs_verify_blocks(dev);
800                 yaffs_verify_free_chunks(dev);
801         }
802
803         T(YAFFS_TRACE_CHECKPOINT, (TSTR("restore exit: is_checkpointed %d"TENDSTR), dev->is_checkpointed));
804
805         return retval;
806 }
807
808 int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size)
809 {
810         /* if new_size > old_file_size.
811          * We're going to be writing a hole.
812          * If the hole is small then write zeros otherwise write a start of hole marker.
813          */
814                 
815
816         loff_t old_file_size;
817         int increase;
818         int small_hole   ;
819         int result = YAFFS_OK;
820         struct yaffs_dev *dev = NULL;
821
822         u8 *local_buffer = NULL;
823         
824         int small_increase_ok = 0;
825         
826         if(!obj)
827                 return YAFFS_FAIL;
828
829         if(obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
830                 return YAFFS_FAIL;
831         
832         dev = obj->my_dev;
833         
834         /* Bail out if not yaffs2 mode */
835         if(!dev->param.is_yaffs2)
836                 return YAFFS_OK;
837
838         old_file_size = obj->variant.file_variant.file_size;
839
840         if (new_size <= old_file_size)
841                 return YAFFS_OK;
842
843         increase = new_size - old_file_size;
844
845         if(increase < YAFFS_SMALL_HOLE_THRESHOLD * dev->data_bytes_per_chunk &&
846                 yaffs_check_alloc_available(dev, YAFFS_SMALL_HOLE_THRESHOLD + 1))
847                 small_hole = 1;
848         else
849                 small_hole = 0;
850
851         if(small_hole)
852                 local_buffer= yaffs_get_temp_buffer(dev, __LINE__);
853         
854         if(local_buffer){
855                 /* fill hole with zero bytes */
856                 int pos = old_file_size;
857                 int this_write;
858                 int written;
859                 memset(local_buffer,0,dev->data_bytes_per_chunk);
860                 small_increase_ok = 1;
861
862                 while(increase > 0 && small_increase_ok){
863                         this_write = increase;
864                         if(this_write > dev->data_bytes_per_chunk)
865                                 this_write = dev->data_bytes_per_chunk;
866                         written = yaffs_do_file_wr(obj,local_buffer,pos,this_write,0);
867                         if(written == this_write){
868                                 pos += this_write;
869                                 increase -= this_write;
870                         } else
871                                 small_increase_ok = 0;
872                 }
873
874                 yaffs_release_temp_buffer(dev,local_buffer,__LINE__);
875
876                 /* If we were out of space then reverse any chunks we've added */               
877                 if(!small_increase_ok)
878                         yaffs_resize_file_down(obj, old_file_size);
879         }
880         
881         if (!small_increase_ok &&
882                 obj->parent &&
883                 obj->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
884                 obj->parent->obj_id != YAFFS_OBJECTID_DELETED){
885                 /* Write a hole start header with the old file size */
886                 yaffs_update_oh(obj, NULL, 0, 1, 0, NULL);
887         }
888
889         return result;
890
891 }
892
893
894 struct yaffs_block_index{
895         int seq;
896         int block;
897 };
898
899
900 static int yaffs2_ybicmp(const void *a, const void *b)
901 {
902         int aseq = ((struct yaffs_block_index *)a)->seq;
903         int bseq = ((struct yaffs_block_index *)b)->seq;
904         int ablock = ((struct yaffs_block_index *)a)->block;
905         int bblock = ((struct yaffs_block_index *)b)->block;
906         if (aseq == bseq)
907                 return ablock - bblock;
908         else
909                 return aseq - bseq;
910 }
911
912 int yaffs2_scan_backwards(struct yaffs_dev *dev)
913 {
914         struct yaffs_ext_tags tags;
915         int blk;
916         int block_iter;
917         int start_iter;
918         int end_iter;
919         int n_to_scan = 0;
920
921         int chunk;
922         int result;
923         int c;
924         int deleted;
925         enum yaffs_block_state state;
926         struct yaffs_obj *hard_list = NULL;
927         struct yaffs_block_info *bi;
928         u32 seq_number;
929         struct yaffs_obj_hdr *oh;
930         struct yaffs_obj *in;
931         struct yaffs_obj *parent;
932         int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
933         int is_unlinked;
934         u8 *chunk_data;
935
936         int file_size;
937         int is_shrink;
938         int found_chunks;
939         int equiv_id;
940         int alloc_failed = 0;
941
942
943         struct yaffs_block_index *block_index = NULL;
944         int alt_block_index = 0;
945
946         T(YAFFS_TRACE_SCAN,
947           (TSTR
948            ("yaffs2_scan_backwards starts  intstartblk %d intendblk %d..."
949             TENDSTR), dev->internal_start_block, dev->internal_end_block));
950
951
952         dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
953
954         block_index = YMALLOC(n_blocks * sizeof(struct yaffs_block_index));
955
956         if (!block_index) {
957                 block_index = YMALLOC_ALT(n_blocks * sizeof(struct yaffs_block_index));
958                 alt_block_index = 1;
959         }
960
961         if (!block_index) {
962                 T(YAFFS_TRACE_SCAN,
963                   (TSTR("yaffs2_scan_backwards() could not allocate block index!" TENDSTR)));
964                 return YAFFS_FAIL;
965         }
966
967         dev->blocks_in_checkpt = 0;
968
969         chunk_data = yaffs_get_temp_buffer(dev, __LINE__);
970
971         /* Scan all the blocks to determine their state */
972         bi = dev->block_info;
973         for (blk = dev->internal_start_block; blk <= dev->internal_end_block; blk++) {
974                 yaffs_clear_chunk_bits(dev, blk);
975                 bi->pages_in_use = 0;
976                 bi->soft_del_pages = 0;
977
978                 yaffs_query_init_block_state(dev, blk, &state, &seq_number);
979
980                 bi->block_state = state;
981                 bi->seq_number = seq_number;
982
983                 if (bi->seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA)
984                         bi->block_state = state = YAFFS_BLOCK_STATE_CHECKPOINT;
985                 if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
986                         bi->block_state = state = YAFFS_BLOCK_STATE_DEAD;
987
988                 T(YAFFS_TRACE_SCAN_DEBUG,
989                   (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
990                    state, seq_number));
991
992
993                 if (state == YAFFS_BLOCK_STATE_CHECKPOINT) {
994                         dev->blocks_in_checkpt++;
995
996                 } else if (state == YAFFS_BLOCK_STATE_DEAD) {
997                         T(YAFFS_TRACE_BAD_BLOCKS,
998                           (TSTR("block %d is bad" TENDSTR), blk));
999                 } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
1000                         T(YAFFS_TRACE_SCAN_DEBUG,
1001                           (TSTR("Block empty " TENDSTR)));
1002                         dev->n_erased_blocks++;
1003                         dev->n_free_chunks += dev->param.chunks_per_block;
1004                 } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
1005
1006                         /* Determine the highest sequence number */
1007                         if (seq_number >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
1008                             seq_number < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
1009
1010                                 block_index[n_to_scan].seq = seq_number;
1011                                 block_index[n_to_scan].block = blk;
1012
1013                                 n_to_scan++;
1014
1015                                 if (seq_number >= dev->seq_number)
1016                                         dev->seq_number = seq_number;
1017                         } else {
1018                                 /* TODO: Nasty sequence number! */
1019                                 T(YAFFS_TRACE_SCAN,
1020                                   (TSTR
1021                                    ("Block scanning block %d has bad sequence number %d"
1022                                     TENDSTR), blk, seq_number));
1023
1024                         }
1025                 }
1026                 bi++;
1027         }
1028
1029         T(YAFFS_TRACE_SCAN,
1030         (TSTR("%d blocks to be sorted..." TENDSTR), n_to_scan));
1031
1032
1033
1034         YYIELD();
1035
1036         /* Sort the blocks by sequence number*/
1037         yaffs_sort(block_index, n_to_scan, sizeof(struct yaffs_block_index), yaffs2_ybicmp);
1038
1039         YYIELD();
1040
1041         T(YAFFS_TRACE_SCAN, (TSTR("...done" TENDSTR)));
1042
1043         /* Now scan the blocks looking at the data. */
1044         start_iter = 0;
1045         end_iter = n_to_scan - 1;
1046         T(YAFFS_TRACE_SCAN_DEBUG,
1047           (TSTR("%d blocks to be scanned" TENDSTR), n_to_scan));
1048
1049         /* For each block.... backwards */
1050         for (block_iter = end_iter; !alloc_failed && block_iter >= start_iter;
1051                         block_iter--) {
1052                 /* Cooperative multitasking! This loop can run for so
1053                    long that watchdog timers expire. */
1054                 YYIELD();
1055
1056                 /* get the block to scan in the correct order */
1057                 blk = block_index[block_iter].block;
1058
1059                 bi = yaffs_get_block_info(dev, blk);
1060
1061
1062                 state = bi->block_state;
1063
1064                 deleted = 0;
1065
1066                 /* For each chunk in each block that needs scanning.... */
1067                 found_chunks = 0;
1068                 for (c = dev->param.chunks_per_block - 1;
1069                      !alloc_failed && c >= 0 &&
1070                      (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
1071                       state == YAFFS_BLOCK_STATE_ALLOCATING); c--) {
1072                         /* Scan backwards...
1073                          * Read the tags and decide what to do
1074                          */
1075
1076                         chunk = blk * dev->param.chunks_per_block + c;
1077
1078                         result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL,
1079                                                         &tags);
1080
1081                         /* Let's have a good look at this chunk... */
1082
1083                         if (!tags.chunk_used) {
1084                                 /* An unassigned chunk in the block.
1085                                  * If there are used chunks after this one, then
1086                                  * it is a chunk that was skipped due to failing the erased
1087                                  * check. Just skip it so that it can be deleted.
1088                                  * But, more typically, We get here when this is an unallocated
1089                                  * chunk and his means that either the block is empty or
1090                                  * this is the one being allocated from
1091                                  */
1092
1093                                 if (found_chunks) {
1094                                         /* This is a chunk that was skipped due to failing the erased check */
1095                                 } else if (c == 0) {
1096                                         /* We're looking at the first chunk in the block so the block is unused */
1097                                         state = YAFFS_BLOCK_STATE_EMPTY;
1098                                         dev->n_erased_blocks++;
1099                                 } else {
1100                                         if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
1101                                             state == YAFFS_BLOCK_STATE_ALLOCATING) {
1102                                                 if (dev->seq_number == bi->seq_number) {
1103                                                         /* this is the block being allocated from */
1104
1105                                                         T(YAFFS_TRACE_SCAN,
1106                                                           (TSTR
1107                                                            (" Allocating from %d %d"
1108                                                             TENDSTR), blk, c));
1109
1110                                                         state = YAFFS_BLOCK_STATE_ALLOCATING;
1111                                                         dev->alloc_block = blk;
1112                                                         dev->alloc_page = c;
1113                                                         dev->alloc_block_finder = blk;
1114                                                 } else {
1115                                                         /* This is a partially written block that is not
1116                                                          * the current allocation block.
1117                                                          */
1118
1119                                                          T(YAFFS_TRACE_SCAN,
1120                                                          (TSTR("Partially written block %d detected" TENDSTR),
1121                                                          blk));
1122                                                 }
1123                                         }
1124                                 }
1125
1126                                 dev->n_free_chunks++;
1127
1128                         } else if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED) {
1129                                 T(YAFFS_TRACE_SCAN,
1130                                   (TSTR(" Unfixed ECC in chunk(%d:%d), chunk ignored"TENDSTR),
1131                                   blk, c));
1132
1133                                   dev->n_free_chunks++;
1134
1135                         } else if (tags.obj_id > YAFFS_MAX_OBJECT_ID ||
1136                                 tags.chunk_id > YAFFS_MAX_CHUNK_ID ||
1137                                 (tags.chunk_id > 0 && tags.n_bytes > dev->data_bytes_per_chunk) ||
1138                                 tags.seq_number != bi->seq_number ) {
1139                                 T(YAFFS_TRACE_SCAN,
1140                                   (TSTR("Chunk (%d:%d) with bad tags:obj = %d, chunk_id = %d, n_bytes = %d, ignored"TENDSTR),
1141                                   blk, c,tags.obj_id, tags.chunk_id, tags.n_bytes));
1142
1143                                   dev->n_free_chunks++;
1144
1145                         } else if (tags.chunk_id > 0) {
1146                                 /* chunk_id > 0 so it is a data chunk... */
1147                                 unsigned int endpos;
1148                                 u32 chunk_base =
1149                                     (tags.chunk_id - 1) * dev->data_bytes_per_chunk;
1150
1151                                 found_chunks = 1;
1152
1153
1154                                 yaffs_set_chunk_bit(dev, blk, c);
1155                                 bi->pages_in_use++;
1156
1157                                 in = yaffs_find_or_create_by_number(dev,
1158                                                                       tags.
1159                                                                       obj_id,
1160                                                                       YAFFS_OBJECT_TYPE_FILE);
1161                                 if (!in) {
1162                                         /* Out of memory */
1163                                         alloc_failed = 1;
1164                                 }
1165
1166                                 if (in &&
1167                                     in->variant_type == YAFFS_OBJECT_TYPE_FILE
1168                                     && chunk_base < in->variant.file_variant.shrink_size) {
1169                                         /* This has not been invalidated by a resize */
1170                                         if (!yaffs_put_chunk_in_file(in, tags.chunk_id, chunk, -1)) {
1171                                                 alloc_failed = 1;
1172                                         }
1173
1174                                         /* File size is calculated by looking at the data chunks if we have not
1175                                          * seen an object header yet. Stop this practice once we find an object header.
1176                                          */
1177                                         endpos = chunk_base + tags.n_bytes;
1178
1179                                         if (!in->valid &&       /* have not got an object header yet */
1180                                             in->variant.file_variant.scanned_size < endpos) {
1181                                                 in->variant.file_variant.scanned_size = endpos;
1182                                                 in->variant.file_variant.file_size = endpos;
1183                                         }
1184
1185                                 } else if (in) {
1186                                         /* This chunk has been invalidated by a resize, or a past file deletion
1187                                          * so delete the chunk*/
1188                                         yaffs_chunk_del(dev, chunk, 1, __LINE__);
1189
1190                                 }
1191                         } else {
1192                                 /* chunk_id == 0, so it is an ObjectHeader.
1193                                  * Thus, we read in the object header and make the object
1194                                  */
1195                                 found_chunks = 1;
1196
1197                                 yaffs_set_chunk_bit(dev, blk, c);
1198                                 bi->pages_in_use++;
1199
1200                                 oh = NULL;
1201                                 in = NULL;
1202
1203                                 if (tags.extra_available) {
1204                                         in = yaffs_find_or_create_by_number(dev,
1205                                                 tags.obj_id,
1206                                                 tags.extra_obj_type);
1207                                         if (!in)
1208                                                 alloc_failed = 1;
1209                                 }
1210
1211                                 if (!in ||
1212                                     (!in->valid && dev->param.disable_lazy_load) ||
1213                                     tags.extra_shadows ||
1214                                     (!in->valid &&
1215                                     (tags.obj_id == YAFFS_OBJECTID_ROOT ||
1216                                      tags.obj_id == YAFFS_OBJECTID_LOSTNFOUND))) {
1217
1218                                         /* If we don't have  valid info then we need to read the chunk
1219                                          * TODO In future we can probably defer reading the chunk and
1220                                          * living with invalid data until needed.
1221                                          */
1222
1223                                         result = yaffs_rd_chunk_tags_nand(dev,
1224                                                                         chunk,
1225                                                                         chunk_data,
1226                                                                         NULL);
1227
1228                                         oh = (struct yaffs_obj_hdr *) chunk_data;
1229
1230                                         if (dev->param.inband_tags) {
1231                                                 /* Fix up the header if they got corrupted by inband tags */
1232                                                 oh->shadows_obj = oh->inband_shadowed_obj_id;
1233                                                 oh->is_shrink = oh->inband_is_shrink;
1234                                         }
1235
1236                                         if (!in) {
1237                                                 in = yaffs_find_or_create_by_number(dev, tags.obj_id, oh->type);
1238                                                 if (!in)
1239                                                         alloc_failed = 1;
1240                                         }
1241
1242                                 }
1243
1244                                 if (!in) {
1245                                         /* TODO Hoosterman we have a problem! */
1246                                         T(YAFFS_TRACE_ERROR,
1247                                           (TSTR
1248                                            ("yaffs tragedy: Could not make object for object  %d at chunk %d during scan"
1249                                             TENDSTR), tags.obj_id, chunk));
1250                                         continue;
1251                                 }
1252
1253                                 if (in->valid) {
1254                                         /* We have already filled this one.
1255                                          * We have a duplicate that will be discarded, but
1256                                          * we first have to suck out resize info if it is a file.
1257                                          */
1258
1259                                         if ((in->variant_type == YAFFS_OBJECT_TYPE_FILE) &&
1260                                              ((oh &&
1261                                                oh->type == YAFFS_OBJECT_TYPE_FILE) ||
1262                                               (tags.extra_available  &&
1263                                                tags.extra_obj_type == YAFFS_OBJECT_TYPE_FILE))) {
1264                                                 u32 this_size =
1265                                                     (oh) ? oh->file_size : tags.
1266                                                     extra_length;
1267                                                 u32 parent_obj_id =
1268                                                     (oh) ? oh->
1269                                                     parent_obj_id : tags.
1270                                                     extra_parent_id;
1271
1272
1273                                                 is_shrink =
1274                                                     (oh) ? oh->is_shrink : tags.
1275                                                     extra_is_shrink;
1276
1277                                                 /* If it is deleted (unlinked at start also means deleted)
1278                                                  * we treat the file size as being zeroed at this point.
1279                                                  */
1280                                                 if (parent_obj_id ==
1281                                                     YAFFS_OBJECTID_DELETED
1282                                                     || parent_obj_id ==
1283                                                     YAFFS_OBJECTID_UNLINKED) {
1284                                                         this_size = 0;
1285                                                         is_shrink = 1;
1286                                                 }
1287
1288                                                 if (is_shrink && in->variant.file_variant.shrink_size > this_size)
1289                                                         in->variant.file_variant.shrink_size = this_size;
1290
1291                                                 if (is_shrink)
1292                                                         bi->has_shrink_hdr = 1;
1293
1294                                         }
1295                                         /* Use existing - destroy this one. */
1296                                         yaffs_chunk_del(dev, chunk, 1, __LINE__);
1297
1298                                 }
1299
1300                                 if (!in->valid && in->variant_type !=
1301                                     (oh ? oh->type : tags.extra_obj_type))
1302                                         T(YAFFS_TRACE_ERROR, (
1303                                                 TSTR("yaffs tragedy: Bad object type, "
1304                                             TCONT("%d != %d, for object %d at chunk ")
1305                                             TCONT("%d during scan")
1306                                                 TENDSTR), oh ?
1307                                             oh->type : tags.extra_obj_type,
1308                                             in->variant_type, tags.obj_id,
1309                                             chunk));
1310
1311                                 if (!in->valid &&
1312                                     (tags.obj_id == YAFFS_OBJECTID_ROOT ||
1313                                      tags.obj_id ==
1314                                      YAFFS_OBJECTID_LOSTNFOUND)) {
1315                                         /* We only load some info, don't fiddle with directory structure */
1316                                         in->valid = 1;
1317
1318                                         if (oh) {
1319
1320                                                 in->yst_mode = oh->yst_mode;
1321 #ifdef CONFIG_YAFFS_WINCE
1322                                                 in->win_atime[0] = oh->win_atime[0];
1323                                                 in->win_ctime[0] = oh->win_ctime[0];
1324                                                 in->win_mtime[0] = oh->win_mtime[0];
1325                                                 in->win_atime[1] = oh->win_atime[1];
1326                                                 in->win_ctime[1] = oh->win_ctime[1];
1327                                                 in->win_mtime[1] = oh->win_mtime[1];
1328 #else
1329                                                 in->yst_uid = oh->yst_uid;
1330                                                 in->yst_gid = oh->yst_gid;
1331                                                 in->yst_atime = oh->yst_atime;
1332                                                 in->yst_mtime = oh->yst_mtime;
1333                                                 in->yst_ctime = oh->yst_ctime;
1334                                                 in->yst_rdev = oh->yst_rdev;
1335
1336                                                 in->lazy_loaded = 0;
1337
1338 #endif
1339                                         } else
1340                                                 in->lazy_loaded = 1;
1341
1342                                         in->hdr_chunk = chunk;
1343
1344                                 } else if (!in->valid) {
1345                                         /* we need to load this info */
1346
1347                                         in->valid = 1;
1348                                         in->hdr_chunk = chunk;
1349
1350                                         if (oh) {
1351                                                 in->variant_type = oh->type;
1352
1353                                                 in->yst_mode = oh->yst_mode;
1354 #ifdef CONFIG_YAFFS_WINCE
1355                                                 in->win_atime[0] = oh->win_atime[0];
1356                                                 in->win_ctime[0] = oh->win_ctime[0];
1357                                                 in->win_mtime[0] = oh->win_mtime[0];
1358                                                 in->win_atime[1] = oh->win_atime[1];
1359                                                 in->win_ctime[1] = oh->win_ctime[1];
1360                                                 in->win_mtime[1] = oh->win_mtime[1];
1361 #else
1362                                                 in->yst_uid = oh->yst_uid;
1363                                                 in->yst_gid = oh->yst_gid;
1364                                                 in->yst_atime = oh->yst_atime;
1365                                                 in->yst_mtime = oh->yst_mtime;
1366                                                 in->yst_ctime = oh->yst_ctime;
1367                                                 in->yst_rdev = oh->yst_rdev;
1368 #endif
1369
1370                                                 if (oh->shadows_obj > 0)
1371                                                         yaffs_handle_shadowed_obj(dev,
1372                                                                            oh->
1373                                                                            shadows_obj,
1374                                                                            1);
1375                                                         
1376
1377
1378                                                 yaffs_set_obj_name_from_oh(in, oh);
1379                                                 parent =
1380                                                     yaffs_find_or_create_by_number
1381                                                         (dev, oh->parent_obj_id,
1382                                                          YAFFS_OBJECT_TYPE_DIRECTORY);
1383
1384                                                  file_size = oh->file_size;
1385                                                  is_shrink = oh->is_shrink;
1386                                                  equiv_id = oh->equiv_id;
1387
1388                                         } else {
1389                                                 in->variant_type = tags.extra_obj_type;
1390                                                 parent =
1391                                                     yaffs_find_or_create_by_number
1392                                                         (dev, tags.extra_parent_id,
1393                                                          YAFFS_OBJECT_TYPE_DIRECTORY);
1394                                                  file_size = tags.extra_length;
1395                                                  is_shrink = tags.extra_is_shrink;
1396                                                  equiv_id = tags.extra_equiv_id;
1397                                                 in->lazy_loaded = 1;
1398
1399                                         }
1400                                         in->dirty = 0;
1401
1402                                         if (!parent)
1403                                                 alloc_failed = 1;
1404
1405                                         /* directory stuff...
1406                                          * hook up to parent
1407                                          */
1408
1409                                         if (parent && parent->variant_type ==
1410                                             YAFFS_OBJECT_TYPE_UNKNOWN) {
1411                                                 /* Set up as a directory */
1412                                                 parent->variant_type =
1413                                                         YAFFS_OBJECT_TYPE_DIRECTORY;
1414                                                 INIT_LIST_HEAD(&parent->variant.
1415                                                         dir_variant.
1416                                                         children);
1417                                         } else if (!parent || parent->variant_type !=
1418                                                    YAFFS_OBJECT_TYPE_DIRECTORY) {
1419                                                 /* Hoosterman, another problem....
1420                                                  * We're trying to use a non-directory as a directory
1421                                                  */
1422
1423                                                 T(YAFFS_TRACE_ERROR,
1424                                                   (TSTR
1425                                                    ("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
1426                                                     TENDSTR)));
1427                                                 parent = dev->lost_n_found;
1428                                         }
1429
1430                                         yaffs_add_obj_to_dir(parent, in);
1431
1432                                         is_unlinked = (parent == dev->del_dir) ||
1433                                                       (parent == dev->unlinked_dir);
1434
1435                                         if (is_shrink) {
1436                                                 /* Mark the block as having a shrink header */
1437                                                 bi->has_shrink_hdr = 1;
1438                                         }
1439
1440                                         /* Note re hardlinks.
1441                                          * Since we might scan a hardlink before its equivalent object is scanned
1442                                          * we put them all in a list.
1443                                          * After scanning is complete, we should have all the objects, so we run
1444                                          * through this list and fix up all the chains.
1445                                          */
1446
1447                                         switch (in->variant_type) {
1448                                         case YAFFS_OBJECT_TYPE_UNKNOWN:
1449                                                 /* Todo got a problem */
1450                                                 break;
1451                                         case YAFFS_OBJECT_TYPE_FILE:
1452
1453                                                 if (in->variant.file_variant.
1454                                                     scanned_size < file_size) {
1455                                                         /* This covers the case where the file size is greater
1456                                                          * than where the data is
1457                                                          * This will happen if the file is resized to be larger
1458                                                          * than its current data extents.
1459                                                          */
1460                                                         in->variant.file_variant.file_size = file_size;
1461                                                         in->variant.file_variant.scanned_size = file_size;
1462                                                 }
1463
1464                                                 if (in->variant.file_variant.shrink_size > file_size)
1465                                                         in->variant.file_variant.shrink_size = file_size;
1466                                 
1467
1468                                                 break;
1469                                         case YAFFS_OBJECT_TYPE_HARDLINK:
1470                                                 if (!is_unlinked) {
1471                                                         in->variant.hardlink_variant.equiv_id =
1472                                                                 equiv_id;
1473                                                         in->hard_links.next =
1474                                                                 (struct list_head *) hard_list;
1475                                                         hard_list = in;
1476                                                 }
1477                                                 break;
1478                                         case YAFFS_OBJECT_TYPE_DIRECTORY:
1479                                                 /* Do nothing */
1480                                                 break;
1481                                         case YAFFS_OBJECT_TYPE_SPECIAL:
1482                                                 /* Do nothing */
1483                                                 break;
1484                                         case YAFFS_OBJECT_TYPE_SYMLINK:
1485                                                 if (oh) {
1486                                                         in->variant.symlink_variant.alias =
1487                                                                 yaffs_clone_str(oh->alias);
1488                                                         if (!in->variant.symlink_variant.alias)
1489                                                                 alloc_failed = 1;
1490                                                 }
1491                                                 break;
1492                                         }
1493
1494                                 }
1495
1496                         }
1497
1498                 } /* End of scanning for each chunk */
1499
1500                 if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
1501                         /* If we got this far while scanning, then the block is fully allocated. */
1502                         state = YAFFS_BLOCK_STATE_FULL;
1503                 }
1504
1505
1506                 bi->block_state = state;
1507
1508                 /* Now let's see if it was dirty */
1509                 if (bi->pages_in_use == 0 &&
1510                     !bi->has_shrink_hdr &&
1511                     bi->block_state == YAFFS_BLOCK_STATE_FULL) {
1512                         yaffs_block_became_dirty(dev, blk);
1513                 }
1514
1515         }
1516         
1517         yaffs_skip_rest_of_block(dev);
1518
1519         if (alt_block_index)
1520                 YFREE_ALT(block_index);
1521         else
1522                 YFREE(block_index);
1523
1524         /* Ok, we've done all the scanning.
1525          * Fix up the hard link chains.
1526          * We should now have scanned all the objects, now it's time to add these
1527          * hardlinks.
1528          */
1529         yaffs_link_fixup(dev, hard_list);
1530
1531
1532         yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
1533
1534         if (alloc_failed)
1535                 return YAFFS_FAIL;
1536
1537         T(YAFFS_TRACE_SCAN, (TSTR("yaffs2_scan_backwards ends" TENDSTR)));
1538
1539         return YAFFS_OK;
1540 }