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