~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise

« back to all changes in this revision

Viewing changes to fs/ubifs/recovery.c

  • Committer: Bazaar Package Importer
  • Author(s): Paolo Pisati
  • Date: 2011-06-29 15:23:51 UTC
  • mfrom: (26.1.1 natty-proposed)
  • Revision ID: james.westby@ubuntu.com-20110629152351-xs96tm303d95rpbk
Tags: 3.0.0-1200.2
* Rebased against 3.0.0-6.7
* BSP from TI based on 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
 * UBIFS always cleans away all remnants of an unclean un-mount, so that
29
29
 * errors do not accumulate. However UBIFS defers recovery if it is mounted
30
30
 * read-only, and the flash is not modified in that case.
 
31
 *
 
32
 * The general UBIFS approach to the recovery is that it recovers from
 
33
 * corruptions which could be caused by power cuts, but it refuses to recover
 
34
 * from corruption caused by other reasons. And UBIFS tries to distinguish
 
35
 * between these 2 reasons of corruptions and silently recover in the former
 
36
 * case and loudly complain in the latter case.
 
37
 *
 
38
 * UBIFS writes only to erased LEBs, so it writes only to the flash space
 
39
 * containing only 0xFFs. UBIFS also always writes strictly from the beginning
 
40
 * of the LEB to the end. And UBIFS assumes that the underlying flash media
 
41
 * writes in @c->max_write_size bytes at a time.
 
42
 *
 
43
 * Hence, if UBIFS finds a corrupted node at offset X, it expects only the min.
 
44
 * I/O unit corresponding to offset X to contain corrupted data, all the
 
45
 * following min. I/O units have to contain empty space (all 0xFFs). If this is
 
46
 * not true, the corruption cannot be the result of a power cut, and UBIFS
 
47
 * refuses to mount.
31
48
 */
32
49
 
33
50
#include <linux/crc32.h>
300
317
                        goto out_free;
301
318
                }
302
319
                memcpy(c->rcvrd_mst_node, c->mst_node, UBIFS_MST_NODE_SZ);
 
320
 
 
321
                /*
 
322
                 * We had to recover the master node, which means there was an
 
323
                 * unclean reboot. However, it is possible that the master node
 
324
                 * is clean at this point, i.e., %UBIFS_MST_DIRTY is not set.
 
325
                 * E.g., consider the following chain of events:
 
326
                 *
 
327
                 * 1. UBIFS was cleanly unmounted, so the master node is clean
 
328
                 * 2. UBIFS is being mounted R/W and starts changing the master
 
329
                 *    node in the first (%UBIFS_MST_LNUM). A power cut happens,
 
330
                 *    so this LEB ends up with some amount of garbage at the
 
331
                 *    end.
 
332
                 * 3. UBIFS is being mounted R/O. We reach this place and
 
333
                 *    recover the master node from the second LEB
 
334
                 *    (%UBIFS_MST_LNUM + 1). But we cannot update the media
 
335
                 *    because we are being mounted R/O. We have to defer the
 
336
                 *    operation.
 
337
                 * 4. However, this master node (@c->mst_node) is marked as
 
338
                 *    clean (since the step 1). And if we just return, the
 
339
                 *    mount code will be confused and won't recover the master
 
340
                 *    node when it is re-mounter R/W later.
 
341
                 *
 
342
                 *    Thus, to force the recovery by marking the master node as
 
343
                 *    dirty.
 
344
                 */
 
345
                c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
303
346
        } else {
304
347
                /* Write the recovered master node */
305
348
                c->max_sqnum = le64_to_cpu(mst->ch.sqnum) - 1;
362
405
 * @offs: offset to check
363
406
 *
364
407
 * This function returns %1 if @offs was in the last write to the LEB whose data
365
 
 * is in @buf, otherwise %0 is returned.  The determination is made by checking
366
 
 * for subsequent empty space starting from the next @c->min_io_size boundary.
 
408
 * is in @buf, otherwise %0 is returned. The determination is made by checking
 
409
 * for subsequent empty space starting from the next @c->max_write_size
 
410
 * boundary.
367
411
 */
368
412
static int is_last_write(const struct ubifs_info *c, void *buf, int offs)
369
413
{
371
415
        uint8_t *p;
372
416
 
373
417
        /*
374
 
         * Round up to the next @c->min_io_size boundary i.e. @offs is in the
375
 
         * last wbuf written. After that should be empty space.
 
418
         * Round up to the next @c->max_write_size boundary i.e. @offs is in
 
419
         * the last wbuf written. After that should be empty space.
376
420
         */
377
 
        empty_offs = ALIGN(offs + 1, c->min_io_size);
 
421
        empty_offs = ALIGN(offs + 1, c->max_write_size);
378
422
        check_len = c->leb_size - empty_offs;
379
423
        p = buf + empty_offs - offs;
380
424
        return is_empty(p, check_len);
429
473
        int skip, dlen = le32_to_cpu(ch->len);
430
474
 
431
475
        /* Check for empty space after the corrupt node's common header */
432
 
        skip = ALIGN(offs + UBIFS_CH_SZ, c->min_io_size) - offs;
 
476
        skip = ALIGN(offs + UBIFS_CH_SZ, c->max_write_size) - offs;
433
477
        if (is_empty(buf + skip, len - skip))
434
478
                return 1;
435
479
        /*
441
485
                return 0;
442
486
        }
443
487
        /* Now we know the corrupt node's length we can skip over it */
444
 
        skip = ALIGN(offs + dlen, c->min_io_size) - offs;
 
488
        skip = ALIGN(offs + dlen, c->max_write_size) - offs;
445
489
        /* After which there should be empty space */
446
490
        if (is_empty(buf + skip, len - skip))
447
491
                return 1;
520
564
}
521
565
 
522
566
/**
523
 
 * drop_incomplete_group - drop nodes from an incomplete group.
 
567
 * drop_last_group - drop the last group of nodes.
524
568
 * @sleb: scanned LEB information
525
569
 * @offs: offset of dropped nodes is returned here
526
570
 *
527
 
 * This function returns %1 if nodes are dropped and %0 otherwise.
 
571
 * This is a helper function for 'ubifs_recover_leb()' which drops the last
 
572
 * group of nodes of the scanned LEB.
528
573
 */
529
 
static int drop_incomplete_group(struct ubifs_scan_leb *sleb, int *offs)
 
574
static void drop_last_group(struct ubifs_scan_leb *sleb, int *offs)
530
575
{
531
 
        int dropped = 0;
532
 
 
533
576
        while (!list_empty(&sleb->nodes)) {
534
577
                struct ubifs_scan_node *snod;
535
578
                struct ubifs_ch *ch;
538
581
                                  list);
539
582
                ch = snod->node;
540
583
                if (ch->group_type != UBIFS_IN_NODE_GROUP)
541
 
                        return dropped;
542
 
                dbg_rcvry("dropping node at %d:%d", sleb->lnum, snod->offs);
543
 
                *offs = snod->offs;
544
 
                list_del(&snod->list);
545
 
                kfree(snod);
546
 
                sleb->nodes_cnt -= 1;
547
 
                dropped = 1;
548
 
        }
549
 
        return dropped;
 
584
                        break;
 
585
 
 
586
                dbg_rcvry("dropping grouped node at %d:%d",
 
587
                          sleb->lnum, snod->offs);
 
588
                *offs = snod->offs;
 
589
                list_del(&snod->list);
 
590
                kfree(snod);
 
591
                sleb->nodes_cnt -= 1;
 
592
        }
 
593
}
 
594
 
 
595
/**
 
596
 * drop_last_node - drop the last node.
 
597
 * @sleb: scanned LEB information
 
598
 * @offs: offset of dropped nodes is returned here
 
599
 * @grouped: non-zero if whole group of nodes have to be dropped
 
600
 *
 
601
 * This is a helper function for 'ubifs_recover_leb()' which drops the last
 
602
 * node of the scanned LEB.
 
603
 */
 
604
static void drop_last_node(struct ubifs_scan_leb *sleb, int *offs)
 
605
{
 
606
        struct ubifs_scan_node *snod;
 
607
 
 
608
        if (!list_empty(&sleb->nodes)) {
 
609
                snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
 
610
                                  list);
 
611
 
 
612
                dbg_rcvry("dropping last node at %d:%d", sleb->lnum, snod->offs);
 
613
                *offs = snod->offs;
 
614
                list_del(&snod->list);
 
615
                kfree(snod);
 
616
                sleb->nodes_cnt -= 1;
 
617
        }
550
618
}
551
619
 
552
620
/**
555
623
 * @lnum: LEB number
556
624
 * @offs: offset
557
625
 * @sbuf: LEB-sized buffer to use
558
 
 * @grouped: nodes may be grouped for recovery
 
626
 * @jhead: journal head number this LEB belongs to (%-1 if the LEB does not
 
627
 *         belong to any journal head)
559
628
 *
560
629
 * This function does a scan of a LEB, but caters for errors that might have
561
630
 * been caused by the unclean unmount from which we are attempting to recover.
563
632
 * found, and a negative error code in case of failure.
564
633
 */
565
634
struct ubifs_scan_leb *ubifs_recover_leb(struct ubifs_info *c, int lnum,
566
 
                                         int offs, void *sbuf, int grouped)
 
635
                                         int offs, void *sbuf, int jhead)
567
636
{
568
 
        int err, len = c->leb_size - offs, need_clean = 0, quiet = 1;
569
 
        int empty_chkd = 0, start = offs;
 
637
        int ret = 0, err, len = c->leb_size - offs, start = offs, min_io_unit;
 
638
        int grouped = jhead == -1 ? 0 : c->jheads[jhead].grouped;
570
639
        struct ubifs_scan_leb *sleb;
571
640
        void *buf = sbuf + offs;
572
641
 
573
 
        dbg_rcvry("%d:%d", lnum, offs);
 
642
        dbg_rcvry("%d:%d, jhead %d, grouped %d", lnum, offs, jhead, grouped);
574
643
 
575
644
        sleb = ubifs_start_scan(c, lnum, offs, sbuf);
576
645
        if (IS_ERR(sleb))
577
646
                return sleb;
578
647
 
579
 
        if (sleb->ecc)
580
 
                need_clean = 1;
581
 
 
 
648
        ubifs_assert(len >= 8);
582
649
        while (len >= 8) {
583
 
                int ret;
584
 
 
585
650
                dbg_scan("look at LEB %d:%d (%d bytes left)",
586
651
                         lnum, offs, len);
587
652
 
591
656
                 * Scan quietly until there is an error from which we cannot
592
657
                 * recover
593
658
                 */
594
 
                ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
595
 
 
 
659
                ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
596
660
                if (ret == SCANNED_A_NODE) {
597
661
                        /* A valid node, and not a padding node */
598
662
                        struct ubifs_ch *ch = buf;
605
669
                        offs += node_len;
606
670
                        buf += node_len;
607
671
                        len -= node_len;
608
 
                        continue;
609
 
                }
610
 
 
611
 
                if (ret > 0) {
 
672
                } else if (ret > 0) {
612
673
                        /* Padding bytes or a valid padding node */
613
674
                        offs += ret;
614
675
                        buf += ret;
615
676
                        len -= ret;
616
 
                        continue;
617
 
                }
618
 
 
619
 
                if (ret == SCANNED_EMPTY_SPACE) {
620
 
                        if (!is_empty(buf, len)) {
621
 
                                if (!is_last_write(c, buf, offs))
622
 
                                        break;
623
 
                                clean_buf(c, &buf, lnum, &offs, &len);
624
 
                                need_clean = 1;
625
 
                        }
626
 
                        empty_chkd = 1;
 
677
                } else if (ret == SCANNED_EMPTY_SPACE ||
 
678
                           ret == SCANNED_GARBAGE     ||
 
679
                           ret == SCANNED_A_BAD_PAD_NODE ||
 
680
                           ret == SCANNED_A_CORRUPT_NODE) {
 
681
                        dbg_rcvry("found corruption - %d", ret);
627
682
                        break;
628
 
                }
629
 
 
630
 
                if (ret == SCANNED_GARBAGE || ret == SCANNED_A_BAD_PAD_NODE)
631
 
                        if (is_last_write(c, buf, offs)) {
632
 
                                clean_buf(c, &buf, lnum, &offs, &len);
633
 
                                need_clean = 1;
634
 
                                empty_chkd = 1;
635
 
                                break;
636
 
                        }
637
 
 
638
 
                if (ret == SCANNED_A_CORRUPT_NODE)
639
 
                        if (no_more_nodes(c, buf, len, lnum, offs)) {
640
 
                                clean_buf(c, &buf, lnum, &offs, &len);
641
 
                                need_clean = 1;
642
 
                                empty_chkd = 1;
643
 
                                break;
644
 
                        }
645
 
 
646
 
                if (quiet) {
647
 
                        /* Redo the last scan but noisily */
648
 
                        quiet = 0;
649
 
                        continue;
650
 
                }
651
 
 
652
 
                switch (ret) {
653
 
                case SCANNED_GARBAGE:
654
 
                        dbg_err("garbage");
655
 
                        goto corrupted;
656
 
                case SCANNED_A_CORRUPT_NODE:
657
 
                case SCANNED_A_BAD_PAD_NODE:
658
 
                        dbg_err("bad node");
659
 
                        goto corrupted;
660
 
                default:
661
 
                        dbg_err("unknown");
 
683
                } else {
 
684
                        dbg_err("unexpected return value %d", ret);
662
685
                        err = -EINVAL;
663
686
                        goto error;
664
687
                }
665
688
        }
666
689
 
667
 
        if (!empty_chkd && !is_empty(buf, len)) {
668
 
                if (is_last_write(c, buf, offs)) {
669
 
                        clean_buf(c, &buf, lnum, &offs, &len);
670
 
                        need_clean = 1;
671
 
                } else {
 
690
        if (ret == SCANNED_GARBAGE || ret == SCANNED_A_BAD_PAD_NODE) {
 
691
                if (!is_last_write(c, buf, offs))
 
692
                        goto corrupted_rescan;
 
693
        } else if (ret == SCANNED_A_CORRUPT_NODE) {
 
694
                if (!no_more_nodes(c, buf, len, lnum, offs))
 
695
                        goto corrupted_rescan;
 
696
        } else if (!is_empty(buf, len)) {
 
697
                if (!is_last_write(c, buf, offs)) {
672
698
                        int corruption = first_non_ff(buf, len);
673
699
 
 
700
                        /*
 
701
                         * See header comment for this file for more
 
702
                         * explanations about the reasons we have this check.
 
703
                         */
674
704
                        ubifs_err("corrupt empty space LEB %d:%d, corruption "
675
705
                                  "starts at %d", lnum, offs, corruption);
676
706
                        /* Make sure we dump interesting non-0xFF data */
677
 
                        offs = corruption;
 
707
                        offs += corruption;
678
708
                        buf += corruption;
679
709
                        goto corrupted;
680
710
                }
681
711
        }
682
712
 
683
 
        /* Drop nodes from incomplete group */
684
 
        if (grouped && drop_incomplete_group(sleb, &offs)) {
685
 
                buf = sbuf + offs;
686
 
                len = c->leb_size - offs;
687
 
                clean_buf(c, &buf, lnum, &offs, &len);
688
 
                need_clean = 1;
689
 
        }
690
 
 
691
 
        if (offs % c->min_io_size) {
692
 
                clean_buf(c, &buf, lnum, &offs, &len);
693
 
                need_clean = 1;
694
 
        }
695
 
 
 
713
        min_io_unit = round_down(offs, c->min_io_size);
 
714
        if (grouped)
 
715
                /*
 
716
                 * If nodes are grouped, always drop the incomplete group at
 
717
                 * the end.
 
718
                 */
 
719
                drop_last_group(sleb, &offs);
 
720
 
 
721
        if (jhead == GCHD) {
 
722
                /*
 
723
                 * If this LEB belongs to the GC head then while we are in the
 
724
                 * middle of the same min. I/O unit keep dropping nodes. So
 
725
                 * basically, what we want is to make sure that the last min.
 
726
                 * I/O unit where we saw the corruption is dropped completely
 
727
                 * with all the uncorrupted nodes which may possibly sit there.
 
728
                 *
 
729
                 * In other words, let's name the min. I/O unit where the
 
730
                 * corruption starts B, and the previous min. I/O unit A. The
 
731
                 * below code tries to deal with a situation when half of B
 
732
                 * contains valid nodes or the end of a valid node, and the
 
733
                 * second half of B contains corrupted data or garbage. This
 
734
                 * means that UBIFS had been writing to B just before the power
 
735
                 * cut happened. I do not know how realistic is this scenario
 
736
                 * that half of the min. I/O unit had been written successfully
 
737
                 * and the other half not, but this is possible in our 'failure
 
738
                 * mode emulation' infrastructure at least.
 
739
                 *
 
740
                 * So what is the problem, why we need to drop those nodes? Why
 
741
                 * can't we just clean-up the second half of B by putting a
 
742
                 * padding node there? We can, and this works fine with one
 
743
                 * exception which was reproduced with power cut emulation
 
744
                 * testing and happens extremely rarely.
 
745
                 *
 
746
                 * Imagine the file-system is full, we run GC which starts
 
747
                 * moving valid nodes from LEB X to LEB Y (obviously, LEB Y is
 
748
                 * the current GC head LEB). The @c->gc_lnum is -1, which means
 
749
                 * that GC will retain LEB X and will try to continue. Imagine
 
750
                 * that LEB X is currently the dirtiest LEB, and the amount of
 
751
                 * used space in LEB Y is exactly the same as amount of free
 
752
                 * space in LEB X.
 
753
                 *
 
754
                 * And a power cut happens when nodes are moved from LEB X to
 
755
                 * LEB Y. We are here trying to recover LEB Y which is the GC
 
756
                 * head LEB. We find the min. I/O unit B as described above.
 
757
                 * Then we clean-up LEB Y by padding min. I/O unit. And later
 
758
                 * 'ubifs_rcvry_gc_commit()' function fails, because it cannot
 
759
                 * find a dirty LEB which could be GC'd into LEB Y! Even LEB X
 
760
                 * does not match because the amount of valid nodes there does
 
761
                 * not fit the free space in LEB Y any more! And this is
 
762
                 * because of the padding node which we added to LEB Y. The
 
763
                 * user-visible effect of this which I once observed and
 
764
                 * analysed is that we cannot mount the file-system with
 
765
                 * -ENOSPC error.
 
766
                 *
 
767
                 * So obviously, to make sure that situation does not happen we
 
768
                 * should free min. I/O unit B in LEB Y completely and the last
 
769
                 * used min. I/O unit in LEB Y should be A. This is basically
 
770
                 * what the below code tries to do.
 
771
                 */
 
772
                while (offs > min_io_unit)
 
773
                        drop_last_node(sleb, &offs);
 
774
        }
 
775
 
 
776
        buf = sbuf + offs;
 
777
        len = c->leb_size - offs;
 
778
 
 
779
        clean_buf(c, &buf, lnum, &offs, &len);
696
780
        ubifs_end_scan(c, sleb, lnum, offs);
697
781
 
698
 
        if (need_clean) {
699
 
                err = fix_unclean_leb(c, sleb, start);
700
 
                if (err)
701
 
                        goto error;
702
 
        }
 
782
        err = fix_unclean_leb(c, sleb, start);
 
783
        if (err)
 
784
                goto error;
703
785
 
704
786
        return sleb;
705
787
 
 
788
corrupted_rescan:
 
789
        /* Re-scan the corrupted data with verbose messages */
 
790
        dbg_err("corruptio %d", ret);
 
791
        ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
706
792
corrupted:
707
793
        ubifs_scanned_corruption(c, lnum, offs, buf);
708
794
        err = -EUCLEAN;
819
905
                }
820
906
                ubifs_scan_destroy(sleb);
821
907
        }
822
 
        return ubifs_recover_leb(c, lnum, offs, sbuf, 0);
 
908
        return ubifs_recover_leb(c, lnum, offs, sbuf, -1);
823
909
}
824
910
 
825
911
/**
836
922
static int recover_head(const struct ubifs_info *c, int lnum, int offs,
837
923
                        void *sbuf)
838
924
{
839
 
        int len, err;
 
925
        int len = c->max_write_size, err;
840
926
 
841
 
        if (c->min_io_size > 1)
842
 
                len = c->min_io_size;
843
 
        else
844
 
                len = 512;
845
927
        if (offs + len > c->leb_size)
846
928
                len = c->leb_size - offs;
847
929
 
1026
1108
}
1027
1109
 
1028
1110
/**
 
1111
 * grab_empty_leb - grab an empty LEB to use as GC LEB and run commit.
 
1112
 * @c: UBIFS file-system description object
 
1113
 *
 
1114
 * This is a helper function for 'ubifs_rcvry_gc_commit()' which grabs an empty
 
1115
 * LEB to be used as GC LEB (@c->gc_lnum), and then runs the commit. Returns
 
1116
 * zero in case of success and a negative error code in case of failure.
 
1117
 */
 
1118
static int grab_empty_leb(struct ubifs_info *c)
 
1119
{
 
1120
        int lnum, err;
 
1121
 
 
1122
        /*
 
1123
         * Note, it is very important to first search for an empty LEB and then
 
1124
         * run the commit, not vice-versa. The reason is that there might be
 
1125
         * only one empty LEB at the moment, the one which has been the
 
1126
         * @c->gc_lnum just before the power cut happened. During the regular
 
1127
         * UBIFS operation (not now) @c->gc_lnum is marked as "taken", so no
 
1128
         * one but GC can grab it. But at this moment this single empty LEB is
 
1129
         * not marked as taken, so if we run commit - what happens? Right, the
 
1130
         * commit will grab it and write the index there. Remember that the
 
1131
         * index always expands as long as there is free space, and it only
 
1132
         * starts consolidating when we run out of space.
 
1133
         *
 
1134
         * IOW, if we run commit now, we might not be able to find a free LEB
 
1135
         * after this.
 
1136
         */
 
1137
        lnum = ubifs_find_free_leb_for_idx(c);
 
1138
        if (lnum < 0) {
 
1139
                dbg_err("could not find an empty LEB");
 
1140
                dbg_dump_lprops(c);
 
1141
                dbg_dump_budg(c, &c->bi);
 
1142
                return lnum;
 
1143
        }
 
1144
 
 
1145
        /* Reset the index flag */
 
1146
        err = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
 
1147
                                  LPROPS_INDEX, 0);
 
1148
        if (err)
 
1149
                return err;
 
1150
 
 
1151
        c->gc_lnum = lnum;
 
1152
        dbg_rcvry("found empty LEB %d, run commit", lnum);
 
1153
 
 
1154
        return ubifs_run_commit(c);
 
1155
}
 
1156
 
 
1157
/**
1029
1158
 * ubifs_rcvry_gc_commit - recover the GC LEB number and run the commit.
1030
1159
 * @c: UBIFS file-system description object
1031
1160
 *
1047
1176
{
1048
1177
        struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
1049
1178
        struct ubifs_lprops lp;
1050
 
        int lnum, err;
 
1179
        int err;
 
1180
 
 
1181
        dbg_rcvry("GC head LEB %d, offs %d", wbuf->lnum, wbuf->offs);
1051
1182
 
1052
1183
        c->gc_lnum = -1;
1053
 
        if (wbuf->lnum == -1) {
1054
 
                dbg_rcvry("no GC head LEB");
1055
 
                goto find_free;
1056
 
        }
1057
 
        /*
1058
 
         * See whether the used space in the dirtiest LEB fits in the GC head
1059
 
         * LEB.
1060
 
         */
1061
 
        if (wbuf->offs == c->leb_size) {
1062
 
                dbg_rcvry("no room in GC head LEB");
1063
 
                goto find_free;
1064
 
        }
 
1184
        if (wbuf->lnum == -1 || wbuf->offs == c->leb_size)
 
1185
                return grab_empty_leb(c);
 
1186
 
1065
1187
        err = ubifs_find_dirty_leb(c, &lp, wbuf->offs, 2);
1066
1188
        if (err) {
1067
 
                /*
1068
 
                 * There are no dirty or empty LEBs subject to here being
1069
 
                 * enough for the index. Try to use
1070
 
                 * 'ubifs_find_free_leb_for_idx()', which will return any empty
1071
 
                 * LEBs (ignoring index requirements). If the index then
1072
 
                 * doesn't have enough LEBs the recovery commit will fail -
1073
 
                 * which is the  same result anyway i.e. recovery fails. So
1074
 
                 * there is no problem ignoring index  requirements and just
1075
 
                 * grabbing a free LEB since we have already established there
1076
 
                 * is not a dirty LEB we could have used instead.
1077
 
                 */
1078
 
                if (err == -ENOSPC) {
1079
 
                        dbg_rcvry("could not find a dirty LEB");
1080
 
                        goto find_free;
1081
 
                }
1082
 
                return err;
 
1189
                if (err != -ENOSPC)
 
1190
                        return err;
 
1191
 
 
1192
                dbg_rcvry("could not find a dirty LEB");
 
1193
                return grab_empty_leb(c);
1083
1194
        }
 
1195
 
1084
1196
        ubifs_assert(!(lp.flags & LPROPS_INDEX));
1085
 
        lnum = lp.lnum;
1086
 
        if (lp.free + lp.dirty == c->leb_size) {
1087
 
                /* An empty LEB was returned */
1088
 
                if (lp.free != c->leb_size) {
1089
 
                        err = ubifs_change_one_lp(c, lnum, c->leb_size,
1090
 
                                                  0, 0, 0, 0);
1091
 
                        if (err)
1092
 
                                return err;
1093
 
                }
1094
 
                err = ubifs_leb_unmap(c, lnum);
1095
 
                if (err)
1096
 
                        return err;
1097
 
                c->gc_lnum = lnum;
1098
 
                dbg_rcvry("allocated LEB %d for GC", lnum);
1099
 
                /* Run the commit */
1100
 
                dbg_rcvry("committing");
1101
 
                return ubifs_run_commit(c);
1102
 
        }
1103
 
        /*
1104
 
         * There was no empty LEB so the used space in the dirtiest LEB must fit
1105
 
         * in the GC head LEB.
1106
 
         */
1107
 
        if (lp.free + lp.dirty < wbuf->offs) {
1108
 
                dbg_rcvry("LEB %d doesn't fit in GC head LEB %d:%d",
1109
 
                          lnum, wbuf->lnum, wbuf->offs);
1110
 
                err = ubifs_return_leb(c, lnum);
1111
 
                if (err)
1112
 
                        return err;
1113
 
                goto find_free;
1114
 
        }
 
1197
        ubifs_assert(lp.free + lp.dirty >= wbuf->offs);
 
1198
 
1115
1199
        /*
1116
1200
         * We run the commit before garbage collection otherwise subsequent
1117
1201
         * mounts will see the GC and orphan deletion in a different order.
1120
1204
        err = ubifs_run_commit(c);
1121
1205
        if (err)
1122
1206
                return err;
1123
 
        /*
1124
 
         * The data in the dirtiest LEB fits in the GC head LEB, so do the GC
1125
 
         * - use locking to keep 'ubifs_assert()' happy.
1126
 
         */
1127
 
        dbg_rcvry("GC'ing LEB %d", lnum);
 
1207
 
 
1208
        dbg_rcvry("GC'ing LEB %d", lp.lnum);
1128
1209
        mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
1129
1210
        err = ubifs_garbage_collect_leb(c, &lp);
1130
1211
        if (err >= 0) {
1140
1221
                        err = -EINVAL;
1141
1222
                return err;
1142
1223
        }
1143
 
        if (err != LEB_RETAINED) {
1144
 
                dbg_err("GC returned %d", err);
 
1224
 
 
1225
        ubifs_assert(err == LEB_RETAINED);
 
1226
        if (err != LEB_RETAINED)
1145
1227
                return -EINVAL;
1146
 
        }
 
1228
 
1147
1229
        err = ubifs_leb_unmap(c, c->gc_lnum);
1148
1230
        if (err)
1149
1231
                return err;
1150
 
        dbg_rcvry("allocated LEB %d for GC", lnum);
 
1232
 
 
1233
        dbg_rcvry("allocated LEB %d for GC", lp.lnum);
1151
1234
        return 0;
1152
 
 
1153
 
find_free:
1154
 
        /*
1155
 
         * There is no GC head LEB or the free space in the GC head LEB is too
1156
 
         * small, or there are not dirty LEBs. Allocate gc_lnum by calling
1157
 
         * 'ubifs_find_free_leb_for_idx()' so GC is not run.
1158
 
         */
1159
 
        lnum = ubifs_find_free_leb_for_idx(c);
1160
 
        if (lnum < 0) {
1161
 
                dbg_err("could not find an empty LEB");
1162
 
                return lnum;
1163
 
        }
1164
 
        /* And reset the index flag */
1165
 
        err = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
1166
 
                                  LPROPS_INDEX, 0);
1167
 
        if (err)
1168
 
                return err;
1169
 
        c->gc_lnum = lnum;
1170
 
        dbg_rcvry("allocated LEB %d for GC", lnum);
1171
 
        /* Run the commit */
1172
 
        dbg_rcvry("committing");
1173
 
        return ubifs_run_commit(c);
1174
1235
}
1175
1236
 
1176
1237
/**
1412
1473
        err = ubi_leb_change(c->ubi, lnum, c->sbuf, len, UBI_UNKNOWN);
1413
1474
        if (err)
1414
1475
                goto out;
1415
 
        dbg_rcvry("inode %lu at %d:%d size %lld -> %lld ",
 
1476
        dbg_rcvry("inode %lu at %d:%d size %lld -> %lld",
1416
1477
                  (unsigned long)e->inum, lnum, offs, i_size, e->d_size);
1417
1478
        return 0;
1418
1479
 
1461
1522
                                e->i_size = le64_to_cpu(ino->size);
1462
1523
                        }
1463
1524
                }
 
1525
 
1464
1526
                if (e->exists && e->i_size < e->d_size) {
1465
 
                        if (!e->inode && c->ro_mount) {
 
1527
                        if (c->ro_mount) {
1466
1528
                                /* Fix the inode size and pin it in memory */
1467
1529
                                struct inode *inode;
 
1530
                                struct ubifs_inode *ui;
 
1531
 
 
1532
                                ubifs_assert(!e->inode);
1468
1533
 
1469
1534
                                inode = ubifs_iget(c->vfs_sb, e->inum);
1470
1535
                                if (IS_ERR(inode))
1471
1536
                                        return PTR_ERR(inode);
 
1537
 
 
1538
                                ui = ubifs_inode(inode);
1472
1539
                                if (inode->i_size < e->d_size) {
1473
1540
                                        dbg_rcvry("ino %lu size %lld -> %lld",
1474
1541
                                                  (unsigned long)e->inum,
1475
 
                                                  e->d_size, inode->i_size);
 
1542
                                                  inode->i_size, e->d_size);
1476
1543
                                        inode->i_size = e->d_size;
1477
 
                                        ubifs_inode(inode)->ui_size = e->d_size;
 
1544
                                        ui->ui_size = e->d_size;
 
1545
                                        ui->synced_i_size = e->d_size;
1478
1546
                                        e->inode = inode;
1479
1547
                                        this = rb_next(this);
1480
1548
                                        continue;
1489
1557
                                        iput(e->inode);
1490
1558
                        }
1491
1559
                }
 
1560
 
1492
1561
                this = rb_next(this);
1493
1562
                rb_erase(&e->rb, &c->size_tree);
1494
1563
                kfree(e);
1495
1564
        }
 
1565
 
1496
1566
        return 0;
1497
1567
}