blob: 33397af7003d1fd6249cf7769a5e7683c594e431 [file] [log] [blame]
Arve Hjønnevåg3de69a72010-05-18 20:35:30 -07001/*
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 */
42void 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
72void 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 */
87void 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 */
105void 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
119int 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 */
141u32 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
197int 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
210int 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
253static 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
267static 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
282static 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
297static 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
311static 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
343static 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
374static 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
397static 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
453static 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
495static 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
514static 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
557static 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 if (lh) {
572 obj =
573 list_entry(lh, struct yaffs_obj, hash_link);
574 if (!obj->defered_free) {
575 yaffs2_obj_checkpt_obj(&cp, obj);
576 cp.struct_type = sizeof(cp);
577
578 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
579 "Checkpoint write object %d parent %d type %d chunk %d obj addr %p",
580 cp.obj_id, cp.parent_id,
581 cp.variant_type, cp.hdr_chunk, obj);
582
583 ok = (yaffs2_checkpt_wr
584 (dev, &cp,
585 sizeof(cp)) == sizeof(cp));
586
587 if (ok
588 && obj->variant_type ==
589 YAFFS_OBJECT_TYPE_FILE)
590 ok = yaffs2_wr_checkpt_tnodes
591 (obj);
592 }
593 }
594 }
595 }
596
597 /* Dump end of list */
598 memset(&cp, 0xFF, sizeof(struct yaffs_checkpt_obj));
599 cp.struct_type = sizeof(cp);
600
601 if (ok)
602 ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
603
604 return ok ? 1 : 0;
605}
606
607static int yaffs2_rd_checkpt_objs(struct yaffs_dev *dev)
608{
609 struct yaffs_obj *obj;
610 struct yaffs_checkpt_obj cp;
611 int ok = 1;
612 int done = 0;
613 struct yaffs_obj *hard_list = NULL;
614
615 while (ok && !done) {
616 ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
617 if (cp.struct_type != sizeof(cp)) {
618 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
619 "struct size %d instead of %d ok %d",
620 cp.struct_type, (int)sizeof(cp), ok);
621 ok = 0;
622 }
623
624 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
625 "Checkpoint read object %d parent %d type %d chunk %d ",
626 cp.obj_id, cp.parent_id, cp.variant_type,
627 cp.hdr_chunk);
628
629 if (ok && cp.obj_id == ~0) {
630 done = 1;
631 } else if (ok) {
632 obj =
633 yaffs_find_or_create_by_number(dev, cp.obj_id,
634 cp.variant_type);
635 if (obj) {
636 ok = taffs2_checkpt_obj_to_obj(obj, &cp);
637 if (!ok)
638 break;
639 if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE) {
640 ok = yaffs2_rd_checkpt_tnodes(obj);
641 } else if (obj->variant_type ==
642 YAFFS_OBJECT_TYPE_HARDLINK) {
643 obj->hard_links.next =
644 (struct list_head *)hard_list;
645 hard_list = obj;
646 }
647 } else {
648 ok = 0;
649 }
650 }
651 }
652
653 if (ok)
654 yaffs_link_fixup(dev, hard_list);
655
656 return ok ? 1 : 0;
657}
658
659static int yaffs2_wr_checkpt_sum(struct yaffs_dev *dev)
660{
661 u32 checkpt_sum;
662 int ok;
663
664 yaffs2_get_checkpt_sum(dev, &checkpt_sum);
665
666 ok = (yaffs2_checkpt_wr(dev, &checkpt_sum, sizeof(checkpt_sum)) ==
667 sizeof(checkpt_sum));
668
669 if (!ok)
670 return 0;
671
672 return 1;
673}
674
675static int yaffs2_rd_checkpt_sum(struct yaffs_dev *dev)
676{
677 u32 checkpt_sum0;
678 u32 checkpt_sum1;
679 int ok;
680
681 yaffs2_get_checkpt_sum(dev, &checkpt_sum0);
682
683 ok = (yaffs2_checkpt_rd(dev, &checkpt_sum1, sizeof(checkpt_sum1)) ==
684 sizeof(checkpt_sum1));
685
686 if (!ok)
687 return 0;
688
689 if (checkpt_sum0 != checkpt_sum1)
690 return 0;
691
692 return 1;
693}
694
695static int yaffs2_wr_checkpt_data(struct yaffs_dev *dev)
696{
697 int ok = 1;
698
699 if (!yaffs2_checkpt_required(dev)) {
700 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
701 "skipping checkpoint write");
702 ok = 0;
703 }
704
705 if (ok)
706 ok = yaffs2_checkpt_open(dev, 1);
707
708 if (ok) {
709 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
710 "write checkpoint validity");
711 ok = yaffs2_wr_checkpt_validity_marker(dev, 1);
712 }
713 if (ok) {
714 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
715 "write checkpoint device");
716 ok = yaffs2_wr_checkpt_dev(dev);
717 }
718 if (ok) {
719 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
720 "write checkpoint objects");
721 ok = yaffs2_wr_checkpt_objs(dev);
722 }
723 if (ok) {
724 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
725 "write checkpoint validity");
726 ok = yaffs2_wr_checkpt_validity_marker(dev, 0);
727 }
728
729 if (ok)
730 ok = yaffs2_wr_checkpt_sum(dev);
731
732 if (!yaffs_checkpt_close(dev))
733 ok = 0;
734
735 if (ok)
736 dev->is_checkpointed = 1;
737 else
738 dev->is_checkpointed = 0;
739
740 return dev->is_checkpointed;
741}
742
743static int yaffs2_rd_checkpt_data(struct yaffs_dev *dev)
744{
745 int ok = 1;
746
747 if (!dev->param.is_yaffs2)
748 ok = 0;
749
750 if (ok && dev->param.skip_checkpt_rd) {
751 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
752 "skipping checkpoint read");
753 ok = 0;
754 }
755
756 if (ok)
757 ok = yaffs2_checkpt_open(dev, 0); /* open for read */
758
759 if (ok) {
760 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
761 "read checkpoint validity");
762 ok = yaffs2_rd_checkpt_validity_marker(dev, 1);
763 }
764 if (ok) {
765 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
766 "read checkpoint device");
767 ok = yaffs2_rd_checkpt_dev(dev);
768 }
769 if (ok) {
770 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
771 "read checkpoint objects");
772 ok = yaffs2_rd_checkpt_objs(dev);
773 }
774 if (ok) {
775 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
776 "read checkpoint validity");
777 ok = yaffs2_rd_checkpt_validity_marker(dev, 0);
778 }
779
780 if (ok) {
781 ok = yaffs2_rd_checkpt_sum(dev);
782 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
783 "read checkpoint checksum %d", ok);
784 }
785
786 if (!yaffs_checkpt_close(dev))
787 ok = 0;
788
789 if (ok)
790 dev->is_checkpointed = 1;
791 else
792 dev->is_checkpointed = 0;
793
794 return ok ? 1 : 0;
795
796}
797
798void yaffs2_checkpt_invalidate(struct yaffs_dev *dev)
799{
800 if (dev->is_checkpointed || dev->blocks_in_checkpt > 0) {
801 dev->is_checkpointed = 0;
802 yaffs2_checkpt_invalidate_stream(dev);
803 }
804 if (dev->param.sb_dirty_fn)
805 dev->param.sb_dirty_fn(dev);
806}
807
808int yaffs_checkpoint_save(struct yaffs_dev *dev)
809{
810
811 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
812 "save entry: is_checkpointed %d",
813 dev->is_checkpointed);
814
815 yaffs_verify_objects(dev);
816 yaffs_verify_blocks(dev);
817 yaffs_verify_free_chunks(dev);
818
819 if (!dev->is_checkpointed) {
820 yaffs2_checkpt_invalidate(dev);
821 yaffs2_wr_checkpt_data(dev);
822 }
823
824 yaffs_trace(YAFFS_TRACE_CHECKPOINT | YAFFS_TRACE_MOUNT,
825 "save exit: is_checkpointed %d",
826 dev->is_checkpointed);
827
828 return dev->is_checkpointed;
829}
830
831int yaffs2_checkpt_restore(struct yaffs_dev *dev)
832{
833 int retval;
834 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
835 "restore entry: is_checkpointed %d",
836 dev->is_checkpointed);
837
838 retval = yaffs2_rd_checkpt_data(dev);
839
840 if (dev->is_checkpointed) {
841 yaffs_verify_objects(dev);
842 yaffs_verify_blocks(dev);
843 yaffs_verify_free_chunks(dev);
844 }
845
846 yaffs_trace(YAFFS_TRACE_CHECKPOINT,
847 "restore exit: is_checkpointed %d",
848 dev->is_checkpointed);
849
850 return retval;
851}
852
853int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size)
854{
855 /* if new_size > old_file_size.
856 * We're going to be writing a hole.
857 * If the hole is small then write zeros otherwise write a start of hole marker.
858 */
859
860 loff_t old_file_size;
861 int increase;
862 int small_hole;
863 int result = YAFFS_OK;
864 struct yaffs_dev *dev = NULL;
865
866 u8 *local_buffer = NULL;
867
868 int small_increase_ok = 0;
869
870 if (!obj)
871 return YAFFS_FAIL;
872
873 if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
874 return YAFFS_FAIL;
875
876 dev = obj->my_dev;
877
878 /* Bail out if not yaffs2 mode */
879 if (!dev->param.is_yaffs2)
880 return YAFFS_OK;
881
882 old_file_size = obj->variant.file_variant.file_size;
883
884 if (new_size <= old_file_size)
885 return YAFFS_OK;
886
887 increase = new_size - old_file_size;
888
889 if (increase < YAFFS_SMALL_HOLE_THRESHOLD * dev->data_bytes_per_chunk &&
890 yaffs_check_alloc_available(dev, YAFFS_SMALL_HOLE_THRESHOLD + 1))
891 small_hole = 1;
892 else
893 small_hole = 0;
894
895 if (small_hole)
896 local_buffer = yaffs_get_temp_buffer(dev, __LINE__);
897
898 if (local_buffer) {
899 /* fill hole with zero bytes */
900 int pos = old_file_size;
901 int this_write;
902 int written;
903 memset(local_buffer, 0, dev->data_bytes_per_chunk);
904 small_increase_ok = 1;
905
906 while (increase > 0 && small_increase_ok) {
907 this_write = increase;
908 if (this_write > dev->data_bytes_per_chunk)
909 this_write = dev->data_bytes_per_chunk;
910 written =
911 yaffs_do_file_wr(obj, local_buffer, pos, this_write,
912 0);
913 if (written == this_write) {
914 pos += this_write;
915 increase -= this_write;
916 } else {
917 small_increase_ok = 0;
918 }
919 }
920
921 yaffs_release_temp_buffer(dev, local_buffer, __LINE__);
922
923 /* If we were out of space then reverse any chunks we've added */
924 if (!small_increase_ok)
925 yaffs_resize_file_down(obj, old_file_size);
926 }
927
928 if (!small_increase_ok &&
929 obj->parent &&
930 obj->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
931 obj->parent->obj_id != YAFFS_OBJECTID_DELETED) {
932 /* Write a hole start header with the old file size */
933 yaffs_update_oh(obj, NULL, 0, 1, 0, NULL);
934 }
935
936 return result;
937
938}
939
940struct yaffs_block_index {
941 int seq;
942 int block;
943};
944
945static int yaffs2_ybicmp(const void *a, const void *b)
946{
947 int aseq = ((struct yaffs_block_index *)a)->seq;
948 int bseq = ((struct yaffs_block_index *)b)->seq;
949 int ablock = ((struct yaffs_block_index *)a)->block;
950 int bblock = ((struct yaffs_block_index *)b)->block;
951 if (aseq == bseq)
952 return ablock - bblock;
953 else
954 return aseq - bseq;
955}
956
957int yaffs2_scan_backwards(struct yaffs_dev *dev)
958{
959 struct yaffs_ext_tags tags;
960 int blk;
961 int block_iter;
962 int start_iter;
963 int end_iter;
964 int n_to_scan = 0;
965
966 int chunk;
967 int result;
968 int c;
969 int deleted;
970 enum yaffs_block_state state;
971 struct yaffs_obj *hard_list = NULL;
972 struct yaffs_block_info *bi;
973 u32 seq_number;
974 struct yaffs_obj_hdr *oh;
975 struct yaffs_obj *in;
976 struct yaffs_obj *parent;
977 int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
978 int is_unlinked;
979 u8 *chunk_data;
980
981 int file_size;
982 int is_shrink;
983 int found_chunks;
984 int equiv_id;
985 int alloc_failed = 0;
986
987 struct yaffs_block_index *block_index = NULL;
988 int alt_block_index = 0;
989
990 yaffs_trace(YAFFS_TRACE_SCAN,
991 "yaffs2_scan_backwards starts intstartblk %d intendblk %d...",
992 dev->internal_start_block, dev->internal_end_block);
993
994 dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
995
996 block_index = kmalloc(n_blocks * sizeof(struct yaffs_block_index),
997 GFP_NOFS);
998
999 if (!block_index) {
1000 block_index =
1001 vmalloc(n_blocks * sizeof(struct yaffs_block_index));
1002 alt_block_index = 1;
1003 }
1004
1005 if (!block_index) {
1006 yaffs_trace(YAFFS_TRACE_SCAN,
1007 "yaffs2_scan_backwards() could not allocate block index!"
1008 );
1009 return YAFFS_FAIL;
1010 }
1011
1012 dev->blocks_in_checkpt = 0;
1013
1014 chunk_data = yaffs_get_temp_buffer(dev, __LINE__);
1015
1016 /* Scan all the blocks to determine their state */
1017 bi = dev->block_info;
1018 for (blk = dev->internal_start_block; blk <= dev->internal_end_block;
1019 blk++) {
1020 yaffs_clear_chunk_bits(dev, blk);
1021 bi->pages_in_use = 0;
1022 bi->soft_del_pages = 0;
1023
1024 yaffs_query_init_block_state(dev, blk, &state, &seq_number);
1025
1026 bi->block_state = state;
1027 bi->seq_number = seq_number;
1028
1029 if (bi->seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA)
1030 bi->block_state = state = YAFFS_BLOCK_STATE_CHECKPOINT;
1031 if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
1032 bi->block_state = state = YAFFS_BLOCK_STATE_DEAD;
1033
1034 yaffs_trace(YAFFS_TRACE_SCAN_DEBUG,
1035 "Block scanning block %d state %d seq %d",
1036 blk, state, seq_number);
1037
1038 if (state == YAFFS_BLOCK_STATE_CHECKPOINT) {
1039 dev->blocks_in_checkpt++;
1040
1041 } else if (state == YAFFS_BLOCK_STATE_DEAD) {
1042 yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
1043 "block %d is bad", blk);
1044 } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
1045 yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "Block empty ");
1046 dev->n_erased_blocks++;
1047 dev->n_free_chunks += dev->param.chunks_per_block;
1048 } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
1049
1050 /* Determine the highest sequence number */
1051 if (seq_number >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
1052 seq_number < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
1053
1054 block_index[n_to_scan].seq = seq_number;
1055 block_index[n_to_scan].block = blk;
1056
1057 n_to_scan++;
1058
1059 if (seq_number >= dev->seq_number)
1060 dev->seq_number = seq_number;
1061 } else {
1062 /* TODO: Nasty sequence number! */
1063 yaffs_trace(YAFFS_TRACE_SCAN,
1064 "Block scanning block %d has bad sequence number %d",
1065 blk, seq_number);
1066
1067 }
1068 }
1069 bi++;
1070 }
1071
1072 yaffs_trace(YAFFS_TRACE_SCAN, "%d blocks to be sorted...", n_to_scan);
1073
1074 cond_resched();
1075
1076 /* Sort the blocks by sequence number */
1077 sort(block_index, n_to_scan, sizeof(struct yaffs_block_index),
1078 yaffs2_ybicmp, NULL);
1079
1080 cond_resched();
1081
1082 yaffs_trace(YAFFS_TRACE_SCAN, "...done");
1083
1084 /* Now scan the blocks looking at the data. */
1085 start_iter = 0;
1086 end_iter = n_to_scan - 1;
1087 yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "%d blocks to scan", n_to_scan);
1088
1089 /* For each block.... backwards */
1090 for (block_iter = end_iter; !alloc_failed && block_iter >= start_iter;
1091 block_iter--) {
1092 /* Cooperative multitasking! This loop can run for so
1093 long that watchdog timers expire. */
1094 cond_resched();
1095
1096 /* get the block to scan in the correct order */
1097 blk = block_index[block_iter].block;
1098
1099 bi = yaffs_get_block_info(dev, blk);
1100
1101 state = bi->block_state;
1102
1103 deleted = 0;
1104
1105 /* For each chunk in each block that needs scanning.... */
1106 found_chunks = 0;
1107 for (c = dev->param.chunks_per_block - 1;
1108 !alloc_failed && c >= 0 &&
1109 (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
1110 state == YAFFS_BLOCK_STATE_ALLOCATING); c--) {
1111 /* Scan backwards...
1112 * Read the tags and decide what to do
1113 */
1114
1115 chunk = blk * dev->param.chunks_per_block + c;
1116
1117 result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL,
1118 &tags);
1119
1120 /* Let's have a good look at this chunk... */
1121
1122 if (!tags.chunk_used) {
1123 /* An unassigned chunk in the block.
1124 * If there are used chunks after this one, then
1125 * it is a chunk that was skipped due to failing the erased
1126 * check. Just skip it so that it can be deleted.
1127 * But, more typically, We get here when this is an unallocated
1128 * chunk and his means that either the block is empty or
1129 * this is the one being allocated from
1130 */
1131
1132 if (found_chunks) {
1133 /* This is a chunk that was skipped due to failing the erased check */
1134 } else if (c == 0) {
1135 /* We're looking at the first chunk in the block so the block is unused */
1136 state = YAFFS_BLOCK_STATE_EMPTY;
1137 dev->n_erased_blocks++;
1138 } else {
1139 if (state ==
1140 YAFFS_BLOCK_STATE_NEEDS_SCANNING
1141 || state ==
1142 YAFFS_BLOCK_STATE_ALLOCATING) {
1143 if (dev->seq_number ==
1144 bi->seq_number) {
1145 /* this is the block being allocated from */
1146
1147 yaffs_trace(YAFFS_TRACE_SCAN,
1148 " Allocating from %d %d",
1149 blk, c);
1150
1151 state =
1152 YAFFS_BLOCK_STATE_ALLOCATING;
1153 dev->alloc_block = blk;
1154 dev->alloc_page = c;
1155 dev->
1156 alloc_block_finder =
1157 blk;
1158 } else {
1159 /* This is a partially written block that is not
1160 * the current allocation block.
1161 */
1162
1163 yaffs_trace(YAFFS_TRACE_SCAN,
1164 "Partially written block %d detected",
1165 blk);
1166 }
1167 }
1168 }
1169
1170 dev->n_free_chunks++;
1171
1172 } else if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED) {
1173 yaffs_trace(YAFFS_TRACE_SCAN,
1174 " Unfixed ECC in chunk(%d:%d), chunk ignored",
1175 blk, c);
1176
1177 dev->n_free_chunks++;
1178
1179 } else if (tags.obj_id > YAFFS_MAX_OBJECT_ID ||
1180 tags.chunk_id > YAFFS_MAX_CHUNK_ID ||
1181 (tags.chunk_id > 0
1182 && tags.n_bytes > dev->data_bytes_per_chunk)
1183 || tags.seq_number != bi->seq_number) {
1184 yaffs_trace(YAFFS_TRACE_SCAN,
1185 "Chunk (%d:%d) with bad tags:obj = %d, chunk_id = %d, n_bytes = %d, ignored",
1186 blk, c, tags.obj_id,
1187 tags.chunk_id, tags.n_bytes);
1188
1189 dev->n_free_chunks++;
1190
1191 } else if (tags.chunk_id > 0) {
1192 /* chunk_id > 0 so it is a data chunk... */
1193 unsigned int endpos;
1194 u32 chunk_base =
1195 (tags.chunk_id -
1196 1) * dev->data_bytes_per_chunk;
1197
1198 found_chunks = 1;
1199
1200 yaffs_set_chunk_bit(dev, blk, c);
1201 bi->pages_in_use++;
1202
1203 in = yaffs_find_or_create_by_number(dev,
1204 tags.obj_id,
1205 YAFFS_OBJECT_TYPE_FILE);
1206 if (!in) {
1207 /* Out of memory */
1208 alloc_failed = 1;
1209 }
1210
1211 if (in &&
1212 in->variant_type == YAFFS_OBJECT_TYPE_FILE
1213 && chunk_base <
1214 in->variant.file_variant.shrink_size) {
1215 /* This has not been invalidated by a resize */
1216 if (!yaffs_put_chunk_in_file
1217 (in, tags.chunk_id, chunk, -1)) {
1218 alloc_failed = 1;
1219 }
1220
1221 /* File size is calculated by looking at the data chunks if we have not
1222 * seen an object header yet. Stop this practice once we find an object header.
1223 */
1224 endpos = chunk_base + tags.n_bytes;
1225
1226 if (!in->valid && /* have not got an object header yet */
1227 in->variant.file_variant.
1228 scanned_size < endpos) {
1229 in->variant.file_variant.
1230 scanned_size = endpos;
1231 in->variant.file_variant.
1232 file_size = endpos;
1233 }
1234
1235 } else if (in) {
1236 /* This chunk has been invalidated by a resize, or a past file deletion
1237 * so delete the chunk*/
1238 yaffs_chunk_del(dev, chunk, 1,
1239 __LINE__);
1240
1241 }
1242 } else {
1243 /* chunk_id == 0, so it is an ObjectHeader.
1244 * Thus, we read in the object header and make the object
1245 */
1246 found_chunks = 1;
1247
1248 yaffs_set_chunk_bit(dev, blk, c);
1249 bi->pages_in_use++;
1250
1251 oh = NULL;
1252 in = NULL;
1253
1254 if (tags.extra_available) {
1255 in = yaffs_find_or_create_by_number(dev,
1256 tags.
1257 obj_id,
1258 tags.
1259 extra_obj_type);
1260 if (!in)
1261 alloc_failed = 1;
1262 }
1263
1264 if (!in ||
1265 (!in->valid && dev->param.disable_lazy_load)
1266 || tags.extra_shadows || (!in->valid
1267 && (tags.obj_id ==
1268 YAFFS_OBJECTID_ROOT
1269 || tags.
1270 obj_id ==
1271 YAFFS_OBJECTID_LOSTNFOUND)))
1272 {
1273
1274 /* If we don't have valid info then we need to read the chunk
1275 * TODO In future we can probably defer reading the chunk and
1276 * living with invalid data until needed.
1277 */
1278
1279 result = yaffs_rd_chunk_tags_nand(dev,
1280 chunk,
1281 chunk_data,
1282 NULL);
1283
1284 oh = (struct yaffs_obj_hdr *)chunk_data;
1285
1286 if (dev->param.inband_tags) {
1287 /* Fix up the header if they got corrupted by inband tags */
1288 oh->shadows_obj =
1289 oh->inband_shadowed_obj_id;
1290 oh->is_shrink =
1291 oh->inband_is_shrink;
1292 }
1293
1294 if (!in) {
1295 in = yaffs_find_or_create_by_number(dev, tags.obj_id, oh->type);
1296 if (!in)
1297 alloc_failed = 1;
1298 }
1299
1300 }
1301
1302 if (!in) {
1303 /* TODO Hoosterman we have a problem! */
1304 yaffs_trace(YAFFS_TRACE_ERROR,
1305 "yaffs tragedy: Could not make object for object %d at chunk %d during scan",
1306 tags.obj_id, chunk);
1307 continue;
1308 }
1309
1310 if (in->valid) {
1311 /* We have already filled this one.
1312 * We have a duplicate that will be discarded, but
1313 * we first have to suck out resize info if it is a file.
1314 */
1315
1316 if ((in->variant_type ==
1317 YAFFS_OBJECT_TYPE_FILE) && ((oh
1318 &&
1319 oh->
1320 type
1321 ==
1322 YAFFS_OBJECT_TYPE_FILE)
1323 ||
1324 (tags.
1325 extra_available
1326 &&
1327 tags.
1328 extra_obj_type
1329 ==
1330 YAFFS_OBJECT_TYPE_FILE)))
1331 {
1332 u32 this_size =
1333 (oh) ? oh->
1334 file_size :
1335 tags.extra_length;
1336 u32 parent_obj_id =
1337 (oh) ? oh->parent_obj_id :
1338 tags.extra_parent_id;
1339
1340 is_shrink =
1341 (oh) ? oh->
1342 is_shrink :
1343 tags.extra_is_shrink;
1344
1345 /* If it is deleted (unlinked at start also means deleted)
1346 * we treat the file size as being zeroed at this point.
1347 */
1348 if (parent_obj_id ==
1349 YAFFS_OBJECTID_DELETED
1350 || parent_obj_id ==
1351 YAFFS_OBJECTID_UNLINKED) {
1352 this_size = 0;
1353 is_shrink = 1;
1354 }
1355
1356 if (is_shrink
1357 && in->variant.file_variant.
1358 shrink_size > this_size)
1359 in->variant.
1360 file_variant.
1361 shrink_size =
1362 this_size;
1363
1364 if (is_shrink)
1365 bi->has_shrink_hdr = 1;
1366
1367 }
1368 /* Use existing - destroy this one. */
1369 yaffs_chunk_del(dev, chunk, 1,
1370 __LINE__);
1371
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 object type, %d != %d, for object %d at chunk %d during scan",
1378 oh ?
1379 oh->type : tags.extra_obj_type,
1380 in->variant_type, tags.obj_id,
1381 chunk);
1382
1383 if (!in->valid &&
1384 (tags.obj_id == YAFFS_OBJECTID_ROOT ||
1385 tags.obj_id ==
1386 YAFFS_OBJECTID_LOSTNFOUND)) {
1387 /* We only load some info, don't fiddle with directory structure */
1388 in->valid = 1;
1389
1390 if (oh) {
1391
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
1403 in->valid = 1;
1404 in->hdr_chunk = chunk;
1405
1406 if (oh) {
1407 in->variant_type = oh->type;
1408
1409 in->yst_mode = oh->yst_mode;
1410 yaffs_load_attribs(in, oh);
1411
1412 if (oh->shadows_obj > 0)
1413 yaffs_handle_shadowed_obj
1414 (dev,
1415 oh->shadows_obj,
1416 1);
1417
1418 yaffs_set_obj_name_from_oh(in,
1419 oh);
1420 parent =
1421 yaffs_find_or_create_by_number
1422 (dev, oh->parent_obj_id,
1423 YAFFS_OBJECT_TYPE_DIRECTORY);
1424
1425 file_size = oh->file_size;
1426 is_shrink = oh->is_shrink;
1427 equiv_id = oh->equiv_id;
1428
1429 } else {
1430 in->variant_type =
1431 tags.extra_obj_type;
1432 parent =
1433 yaffs_find_or_create_by_number
1434 (dev, tags.extra_parent_id,
1435 YAFFS_OBJECT_TYPE_DIRECTORY);
1436 file_size = tags.extra_length;
1437 is_shrink =
1438 tags.extra_is_shrink;
1439 equiv_id = tags.extra_equiv_id;
1440 in->lazy_loaded = 1;
1441
1442 }
1443 in->dirty = 0;
1444
1445 if (!parent)
1446 alloc_failed = 1;
1447
1448 /* directory stuff...
1449 * hook up to parent
1450 */
1451
1452 if (parent && parent->variant_type ==
1453 YAFFS_OBJECT_TYPE_UNKNOWN) {
1454 /* Set up as a directory */
1455 parent->variant_type =
1456 YAFFS_OBJECT_TYPE_DIRECTORY;
1457 INIT_LIST_HEAD(&parent->
1458 variant.dir_variant.children);
1459 } else if (!parent
1460 || parent->variant_type !=
1461 YAFFS_OBJECT_TYPE_DIRECTORY) {
1462 /* Hoosterman, another problem....
1463 * We're trying to use a non-directory as a directory
1464 */
1465
1466 yaffs_trace(YAFFS_TRACE_ERROR,
1467 "yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
1468 );
1469 parent = dev->lost_n_found;
1470 }
1471
1472 yaffs_add_obj_to_dir(parent, in);
1473
1474 is_unlinked = (parent == dev->del_dir)
1475 || (parent == dev->unlinked_dir);
1476
1477 if (is_shrink) {
1478 /* Mark the block as having a shrink header */
1479 bi->has_shrink_hdr = 1;
1480 }
1481
1482 /* Note re hardlinks.
1483 * Since we might scan a hardlink before its equivalent object is scanned
1484 * we put them all in a list.
1485 * After scanning is complete, we should have all the objects, so we run
1486 * through this list and fix up all the chains.
1487 */
1488
1489 switch (in->variant_type) {
1490 case YAFFS_OBJECT_TYPE_UNKNOWN:
1491 /* Todo got a problem */
1492 break;
1493 case YAFFS_OBJECT_TYPE_FILE:
1494
1495 if (in->variant.
1496 file_variant.scanned_size <
1497 file_size) {
1498 /* This covers the case where the file size is greater
1499 * than where the data is
1500 * This will happen if the file is resized to be larger
1501 * than its current data extents.
1502 */
1503 in->variant.
1504 file_variant.
1505 file_size =
1506 file_size;
1507 in->variant.
1508 file_variant.
1509 scanned_size =
1510 file_size;
1511 }
1512
1513 if (in->variant.file_variant.
1514 shrink_size > file_size)
1515 in->variant.
1516 file_variant.
1517 shrink_size =
1518 file_size;
1519
1520 break;
1521 case YAFFS_OBJECT_TYPE_HARDLINK:
1522 if (!is_unlinked) {
1523 in->variant.
1524 hardlink_variant.
1525 equiv_id = equiv_id;
1526 in->hard_links.next =
1527 (struct list_head *)
1528 hard_list;
1529 hard_list = in;
1530 }
1531 break;
1532 case YAFFS_OBJECT_TYPE_DIRECTORY:
1533 /* Do nothing */
1534 break;
1535 case YAFFS_OBJECT_TYPE_SPECIAL:
1536 /* Do nothing */
1537 break;
1538 case YAFFS_OBJECT_TYPE_SYMLINK:
1539 if (oh) {
1540 in->variant.
1541 symlink_variant.
1542 alias =
1543 yaffs_clone_str(oh->
1544 alias);
1545 if (!in->variant.
1546 symlink_variant.
1547 alias)
1548 alloc_failed =
1549 1;
1550 }
1551 break;
1552 }
1553
1554 }
1555
1556 }
1557
1558 } /* End of scanning for each chunk */
1559
1560 if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
1561 /* If we got this far while scanning, then the block is fully allocated. */
1562 state = YAFFS_BLOCK_STATE_FULL;
1563 }
1564
1565 bi->block_state = state;
1566
1567 /* Now let's see if it was dirty */
1568 if (bi->pages_in_use == 0 &&
1569 !bi->has_shrink_hdr &&
1570 bi->block_state == YAFFS_BLOCK_STATE_FULL) {
1571 yaffs_block_became_dirty(dev, blk);
1572 }
1573
1574 }
1575
1576 yaffs_skip_rest_of_block(dev);
1577
1578 if (alt_block_index)
1579 vfree(block_index);
1580 else
1581 kfree(block_index);
1582
1583 /* Ok, we've done all the scanning.
1584 * Fix up the hard link chains.
1585 * We should now have scanned all the objects, now it's time to add these
1586 * hardlinks.
1587 */
1588 yaffs_link_fixup(dev, hard_list);
1589
1590 yaffs_release_temp_buffer(dev, chunk_data, __LINE__);
1591
1592 if (alloc_failed)
1593 return YAFFS_FAIL;
1594
1595 yaffs_trace(YAFFS_TRACE_SCAN, "yaffs2_scan_backwards ends");
1596
1597 return YAFFS_OK;
1598}