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