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