1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50#include <linux/crc32.h>
51#include <linux/slab.h>
52#include "ubifs.h"
53
54
55
56
57
58
59
60
61
62static int is_empty(void *buf, int len)
63{
64 uint8_t *p = buf;
65 int i;
66
67 for (i = 0; i < len; i++)
68 if (*p++ != 0xff)
69 return 0;
70 return 1;
71}
72
73
74
75
76
77
78
79
80
81static int first_non_ff(void *buf, int len)
82{
83 uint8_t *p = buf;
84 int i;
85
86 for (i = 0; i < len; i++)
87 if (*p++ != 0xff)
88 return i;
89 return -1;
90}
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109static int get_master_node(const struct ubifs_info *c, int lnum, void **pbuf,
110 struct ubifs_mst_node **mst, void **cor)
111{
112 const int sz = c->mst_node_alsz;
113 int err, offs, len;
114 void *sbuf, *buf;
115
116 sbuf = vmalloc(c->leb_size);
117 if (!sbuf)
118 return -ENOMEM;
119
120 err = ubifs_leb_read(c, lnum, sbuf, 0, c->leb_size, 0);
121 if (err && err != -EBADMSG)
122 goto out_free;
123
124
125 offs = 0;
126 buf = sbuf;
127 len = c->leb_size;
128 while (offs + UBIFS_MST_NODE_SZ <= c->leb_size) {
129 struct ubifs_ch *ch = buf;
130
131 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC)
132 break;
133 offs += sz;
134 buf += sz;
135 len -= sz;
136 }
137
138 if (offs) {
139 int ret;
140
141 offs -= sz;
142 buf -= sz;
143 len += sz;
144 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
145 if (ret != SCANNED_A_NODE && offs) {
146
147 offs -= sz;
148 buf -= sz;
149 len += sz;
150 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
151 if (ret != SCANNED_A_NODE)
152
153
154
155
156
157 goto out_err;
158 }
159 if (ret == SCANNED_A_NODE) {
160 struct ubifs_ch *ch = buf;
161
162 if (ch->node_type != UBIFS_MST_NODE)
163 goto out_err;
164 dbg_rcvry("found a master node at %d:%d", lnum, offs);
165 *mst = buf;
166 offs += sz;
167 buf += sz;
168 len -= sz;
169 }
170 }
171
172 if (offs < c->leb_size) {
173 if (!is_empty(buf, min_t(int, len, sz))) {
174 *cor = buf;
175 dbg_rcvry("found corruption at %d:%d", lnum, offs);
176 }
177 offs += sz;
178 buf += sz;
179 len -= sz;
180 }
181
182 if (offs < c->leb_size)
183 if (!is_empty(buf, len))
184 goto out_err;
185 *pbuf = sbuf;
186 return 0;
187
188out_err:
189 err = -EINVAL;
190out_free:
191 vfree(sbuf);
192 *mst = NULL;
193 *cor = NULL;
194 return err;
195}
196
197
198
199
200
201
202
203
204static int write_rcvrd_mst_node(struct ubifs_info *c,
205 struct ubifs_mst_node *mst)
206{
207 int err = 0, lnum = UBIFS_MST_LNUM, sz = c->mst_node_alsz;
208 __le32 save_flags;
209
210 dbg_rcvry("recovery");
211
212 save_flags = mst->flags;
213 mst->flags |= cpu_to_le32(UBIFS_MST_RCVRY);
214
215 ubifs_prepare_node(c, mst, UBIFS_MST_NODE_SZ, 1);
216 err = ubifs_leb_change(c, lnum, mst, sz);
217 if (err)
218 goto out;
219 err = ubifs_leb_change(c, lnum + 1, mst, sz);
220 if (err)
221 goto out;
222out:
223 mst->flags = save_flags;
224 return err;
225}
226
227
228
229
230
231
232
233
234
235
236int ubifs_recover_master_node(struct ubifs_info *c)
237{
238 void *buf1 = NULL, *buf2 = NULL, *cor1 = NULL, *cor2 = NULL;
239 struct ubifs_mst_node *mst1 = NULL, *mst2 = NULL, *mst;
240 const int sz = c->mst_node_alsz;
241 int err, offs1, offs2;
242
243 dbg_rcvry("recovery");
244
245 err = get_master_node(c, UBIFS_MST_LNUM, &buf1, &mst1, &cor1);
246 if (err)
247 goto out_free;
248
249 err = get_master_node(c, UBIFS_MST_LNUM + 1, &buf2, &mst2, &cor2);
250 if (err)
251 goto out_free;
252
253 if (mst1) {
254 offs1 = (void *)mst1 - buf1;
255 if ((le32_to_cpu(mst1->flags) & UBIFS_MST_RCVRY) &&
256 (offs1 == 0 && !cor1)) {
257
258
259
260
261 dbg_rcvry("recovery recovery");
262 mst = mst1;
263 } else if (mst2) {
264 offs2 = (void *)mst2 - buf2;
265 if (offs1 == offs2) {
266
267 if (memcmp((void *)mst1 + UBIFS_CH_SZ,
268 (void *)mst2 + UBIFS_CH_SZ,
269 UBIFS_MST_NODE_SZ - UBIFS_CH_SZ))
270 goto out_err;
271 mst = mst1;
272 } else if (offs2 + sz == offs1) {
273
274 if (cor1)
275 goto out_err;
276 mst = mst1;
277 } else if (offs1 == 0 &&
278 c->leb_size - offs2 - sz < sz) {
279
280 if (cor1)
281 goto out_err;
282 mst = mst1;
283 } else
284 goto out_err;
285 } else {
286
287
288
289
290
291 if (offs1 != 0 || cor1)
292 goto out_err;
293 mst = mst1;
294 }
295 } else {
296 if (!mst2)
297 goto out_err;
298
299
300
301
302 offs2 = (void *)mst2 - buf2;
303 if (offs2 + sz + sz <= c->leb_size)
304 goto out_err;
305 mst = mst2;
306 }
307
308 ubifs_msg("recovered master node from LEB %d",
309 (mst == mst1 ? UBIFS_MST_LNUM : UBIFS_MST_LNUM + 1));
310
311 memcpy(c->mst_node, mst, UBIFS_MST_NODE_SZ);
312
313 if (c->ro_mount) {
314
315 c->rcvrd_mst_node = kmalloc(sz, GFP_KERNEL);
316 if (!c->rcvrd_mst_node) {
317 err = -ENOMEM;
318 goto out_free;
319 }
320 memcpy(c->rcvrd_mst_node, c->mst_node, UBIFS_MST_NODE_SZ);
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
347 } else {
348
349 c->max_sqnum = le64_to_cpu(mst->ch.sqnum) - 1;
350 err = write_rcvrd_mst_node(c, c->mst_node);
351 if (err)
352 goto out_free;
353 }
354
355 vfree(buf2);
356 vfree(buf1);
357
358 return 0;
359
360out_err:
361 err = -EINVAL;
362out_free:
363 ubifs_err("failed to recover master node");
364 if (mst1) {
365 ubifs_err("dumping first master node");
366 ubifs_dump_node(c, mst1);
367 }
368 if (mst2) {
369 ubifs_err("dumping second master node");
370 ubifs_dump_node(c, mst2);
371 }
372 vfree(buf2);
373 vfree(buf1);
374 return err;
375}
376
377
378
379
380
381
382
383
384
385
386int ubifs_write_rcvrd_mst_node(struct ubifs_info *c)
387{
388 int err;
389
390 if (!c->rcvrd_mst_node)
391 return 0;
392 c->rcvrd_mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
393 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
394 err = write_rcvrd_mst_node(c, c->rcvrd_mst_node);
395 if (err)
396 return err;
397 kfree(c->rcvrd_mst_node);
398 c->rcvrd_mst_node = NULL;
399 return 0;
400}
401
402
403
404
405
406
407
408
409
410
411
412
413static int is_last_write(const struct ubifs_info *c, void *buf, int offs)
414{
415 int empty_offs, check_len;
416 uint8_t *p;
417
418
419
420
421
422 empty_offs = ALIGN(offs + 1, c->max_write_size);
423 check_len = c->leb_size - empty_offs;
424 p = buf + empty_offs - offs;
425 return is_empty(p, check_len);
426}
427
428
429
430
431
432
433
434
435
436
437
438
439
440static void clean_buf(const struct ubifs_info *c, void **buf, int lnum,
441 int *offs, int *len)
442{
443 int empty_offs, pad_len;
444
445 lnum = lnum;
446 dbg_rcvry("cleaning corruption at %d:%d", lnum, *offs);
447
448 ubifs_assert(!(*offs & 7));
449 empty_offs = ALIGN(*offs, c->min_io_size);
450 pad_len = empty_offs - *offs;
451 ubifs_pad(c, *buf, pad_len);
452 *offs += pad_len;
453 *buf += pad_len;
454 *len -= pad_len;
455 memset(*buf, 0xff, c->leb_size - empty_offs);
456}
457
458
459
460
461
462
463
464
465
466
467
468
469
470static int no_more_nodes(const struct ubifs_info *c, void *buf, int len,
471 int lnum, int offs)
472{
473 struct ubifs_ch *ch = buf;
474 int skip, dlen = le32_to_cpu(ch->len);
475
476
477 skip = ALIGN(offs + UBIFS_CH_SZ, c->max_write_size) - offs;
478 if (is_empty(buf + skip, len - skip))
479 return 1;
480
481
482
483
484 if (ubifs_check_node(c, buf, lnum, offs, 1, 0) != -EUCLEAN) {
485 dbg_rcvry("unexpected bad common header at %d:%d", lnum, offs);
486 return 0;
487 }
488
489 skip = ALIGN(offs + dlen, c->max_write_size) - offs;
490
491 if (is_empty(buf + skip, len - skip))
492 return 1;
493 dbg_rcvry("unexpected data at %d:%d", lnum, offs + skip);
494 return 0;
495}
496
497
498
499
500
501
502
503static int fix_unclean_leb(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
504 int start)
505{
506 int lnum = sleb->lnum, endpt = start;
507
508
509 if (!list_empty(&sleb->nodes)) {
510 struct ubifs_scan_node *snod;
511
512 snod = list_entry(sleb->nodes.prev,
513 struct ubifs_scan_node, list);
514 endpt = snod->offs + snod->len;
515 }
516
517 if (c->ro_mount && !c->remounting_rw) {
518
519 struct ubifs_unclean_leb *ucleb;
520
521 dbg_rcvry("need to fix LEB %d start %d endpt %d",
522 lnum, start, sleb->endpt);
523 ucleb = kzalloc(sizeof(struct ubifs_unclean_leb), GFP_NOFS);
524 if (!ucleb)
525 return -ENOMEM;
526 ucleb->lnum = lnum;
527 ucleb->endpt = endpt;
528 list_add_tail(&ucleb->list, &c->unclean_leb_list);
529 } else {
530
531 int err;
532
533 dbg_rcvry("fixing LEB %d start %d endpt %d",
534 lnum, start, sleb->endpt);
535 if (endpt == 0) {
536 err = ubifs_leb_unmap(c, lnum);
537 if (err)
538 return err;
539 } else {
540 int len = ALIGN(endpt, c->min_io_size);
541
542 if (start) {
543 err = ubifs_leb_read(c, lnum, sleb->buf, 0,
544 start, 1);
545 if (err)
546 return err;
547 }
548
549 if (len > endpt) {
550 int pad_len = len - ALIGN(endpt, 8);
551
552 if (pad_len > 0) {
553 void *buf = sleb->buf + len - pad_len;
554
555 ubifs_pad(c, buf, pad_len);
556 }
557 }
558 err = ubifs_leb_change(c, lnum, sleb->buf, len);
559 if (err)
560 return err;
561 }
562 }
563 return 0;
564}
565
566
567
568
569
570
571
572
573
574static void drop_last_group(struct ubifs_scan_leb *sleb, int *offs)
575{
576 while (!list_empty(&sleb->nodes)) {
577 struct ubifs_scan_node *snod;
578 struct ubifs_ch *ch;
579
580 snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
581 list);
582 ch = snod->node;
583 if (ch->group_type != UBIFS_IN_NODE_GROUP)
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
597
598
599
600
601
602
603
604static 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",
613 sleb->lnum, snod->offs);
614 *offs = snod->offs;
615 list_del(&snod->list);
616 kfree(snod);
617 sleb->nodes_cnt -= 1;
618 }
619}
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635struct ubifs_scan_leb *ubifs_recover_leb(struct ubifs_info *c, int lnum,
636 int offs, void *sbuf, int jhead)
637{
638 int ret = 0, err, len = c->leb_size - offs, start = offs, min_io_unit;
639 int grouped = jhead == -1 ? 0 : c->jheads[jhead].grouped;
640 struct ubifs_scan_leb *sleb;
641 void *buf = sbuf + offs;
642
643 dbg_rcvry("%d:%d, jhead %d, grouped %d", lnum, offs, jhead, grouped);
644
645 sleb = ubifs_start_scan(c, lnum, offs, sbuf);
646 if (IS_ERR(sleb))
647 return sleb;
648
649 ubifs_assert(len >= 8);
650 while (len >= 8) {
651 dbg_scan("look at LEB %d:%d (%d bytes left)",
652 lnum, offs, len);
653
654 cond_resched();
655
656
657
658
659
660 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
661 if (ret == SCANNED_A_NODE) {
662
663 struct ubifs_ch *ch = buf;
664 int node_len;
665
666 err = ubifs_add_snod(c, sleb, buf, offs);
667 if (err)
668 goto error;
669 node_len = ALIGN(le32_to_cpu(ch->len), 8);
670 offs += node_len;
671 buf += node_len;
672 len -= node_len;
673 } else if (ret > 0) {
674
675 offs += ret;
676 buf += ret;
677 len -= ret;
678 } else if (ret == SCANNED_EMPTY_SPACE ||
679 ret == SCANNED_GARBAGE ||
680 ret == SCANNED_A_BAD_PAD_NODE ||
681 ret == SCANNED_A_CORRUPT_NODE) {
682 dbg_rcvry("found corruption (%d) at %d:%d",
683 ret, lnum, offs);
684 break;
685 } else {
686 ubifs_err("unexpected return value %d", ret);
687 err = -EINVAL;
688 goto error;
689 }
690 }
691
692 if (ret == SCANNED_GARBAGE || ret == SCANNED_A_BAD_PAD_NODE) {
693 if (!is_last_write(c, buf, offs))
694 goto corrupted_rescan;
695 } else if (ret == SCANNED_A_CORRUPT_NODE) {
696 if (!no_more_nodes(c, buf, len, lnum, offs))
697 goto corrupted_rescan;
698 } else if (!is_empty(buf, len)) {
699 if (!is_last_write(c, buf, offs)) {
700 int corruption = first_non_ff(buf, len);
701
702
703
704
705
706 ubifs_err("corrupt empty space LEB %d:%d, corruption starts at %d",
707 lnum, offs, corruption);
708
709 offs += corruption;
710 buf += corruption;
711 goto corrupted;
712 }
713 }
714
715 min_io_unit = round_down(offs, c->min_io_size);
716 if (grouped)
717
718
719
720
721 drop_last_group(sleb, &offs);
722
723 if (jhead == GCHD) {
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774 while (offs > min_io_unit)
775 drop_last_node(sleb, &offs);
776 }
777
778 buf = sbuf + offs;
779 len = c->leb_size - offs;
780
781 clean_buf(c, &buf, lnum, &offs, &len);
782 ubifs_end_scan(c, sleb, lnum, offs);
783
784 err = fix_unclean_leb(c, sleb, start);
785 if (err)
786 goto error;
787
788 return sleb;
789
790corrupted_rescan:
791
792 ubifs_err("corruption %d", ret);
793 ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
794corrupted:
795 ubifs_scanned_corruption(c, lnum, offs, buf);
796 err = -EUCLEAN;
797error:
798 ubifs_err("LEB %d scanning failed", lnum);
799 ubifs_scan_destroy(sleb);
800 return ERR_PTR(err);
801}
802
803
804
805
806
807
808
809
810
811
812static int get_cs_sqnum(struct ubifs_info *c, int lnum, int offs,
813 unsigned long long *cs_sqnum)
814{
815 struct ubifs_cs_node *cs_node = NULL;
816 int err, ret;
817
818 dbg_rcvry("at %d:%d", lnum, offs);
819 cs_node = kmalloc(UBIFS_CS_NODE_SZ, GFP_KERNEL);
820 if (!cs_node)
821 return -ENOMEM;
822 if (c->leb_size - offs < UBIFS_CS_NODE_SZ)
823 goto out_err;
824 err = ubifs_leb_read(c, lnum, (void *)cs_node, offs,
825 UBIFS_CS_NODE_SZ, 0);
826 if (err && err != -EBADMSG)
827 goto out_free;
828 ret = ubifs_scan_a_node(c, cs_node, UBIFS_CS_NODE_SZ, lnum, offs, 0);
829 if (ret != SCANNED_A_NODE) {
830 ubifs_err("Not a valid node");
831 goto out_err;
832 }
833 if (cs_node->ch.node_type != UBIFS_CS_NODE) {
834 ubifs_err("Node a CS node, type is %d", cs_node->ch.node_type);
835 goto out_err;
836 }
837 if (le64_to_cpu(cs_node->cmt_no) != c->cmt_no) {
838 ubifs_err("CS node cmt_no %llu != current cmt_no %llu",
839 (unsigned long long)le64_to_cpu(cs_node->cmt_no),
840 c->cmt_no);
841 goto out_err;
842 }
843 *cs_sqnum = le64_to_cpu(cs_node->ch.sqnum);
844 dbg_rcvry("commit start sqnum %llu", *cs_sqnum);
845 kfree(cs_node);
846 return 0;
847
848out_err:
849 err = -EINVAL;
850out_free:
851 ubifs_err("failed to get CS sqnum");
852 kfree(cs_node);
853 return err;
854}
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869struct ubifs_scan_leb *ubifs_recover_log_leb(struct ubifs_info *c, int lnum,
870 int offs, void *sbuf)
871{
872 struct ubifs_scan_leb *sleb;
873 int next_lnum;
874
875 dbg_rcvry("LEB %d", lnum);
876 next_lnum = lnum + 1;
877 if (next_lnum >= UBIFS_LOG_LNUM + c->log_lebs)
878 next_lnum = UBIFS_LOG_LNUM;
879 if (next_lnum != c->ltail_lnum) {
880
881
882
883
884 sleb = ubifs_scan(c, next_lnum, 0, sbuf, 0);
885 if (IS_ERR(sleb))
886 return sleb;
887 if (sleb->nodes_cnt) {
888 struct ubifs_scan_node *snod;
889 unsigned long long cs_sqnum = c->cs_sqnum;
890
891 snod = list_entry(sleb->nodes.next,
892 struct ubifs_scan_node, list);
893 if (cs_sqnum == 0) {
894 int err;
895
896 err = get_cs_sqnum(c, lnum, offs, &cs_sqnum);
897 if (err) {
898 ubifs_scan_destroy(sleb);
899 return ERR_PTR(err);
900 }
901 }
902 if (snod->sqnum > cs_sqnum) {
903 ubifs_err("unrecoverable log corruption in LEB %d",
904 lnum);
905 ubifs_scan_destroy(sleb);
906 return ERR_PTR(-EUCLEAN);
907 }
908 }
909 ubifs_scan_destroy(sleb);
910 }
911 return ubifs_recover_leb(c, lnum, offs, sbuf, -1);
912}
913
914
915
916
917
918
919
920
921
922
923
924
925static int recover_head(struct ubifs_info *c, int lnum, int offs, void *sbuf)
926{
927 int len = c->max_write_size, err;
928
929 if (offs + len > c->leb_size)
930 len = c->leb_size - offs;
931
932 if (!len)
933 return 0;
934
935
936 err = ubifs_leb_read(c, lnum, sbuf, offs, len, 1);
937 if (err || !is_empty(sbuf, len)) {
938 dbg_rcvry("cleaning head at %d:%d", lnum, offs);
939 if (offs == 0)
940 return ubifs_leb_unmap(c, lnum);
941 err = ubifs_leb_read(c, lnum, sbuf, 0, offs, 1);
942 if (err)
943 return err;
944 return ubifs_leb_change(c, lnum, sbuf, offs);
945 }
946
947 return 0;
948}
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967int ubifs_recover_inl_heads(struct ubifs_info *c, void *sbuf)
968{
969 int err;
970
971 ubifs_assert(!c->ro_mount || c->remounting_rw);
972
973 dbg_rcvry("checking index head at %d:%d", c->ihead_lnum, c->ihead_offs);
974 err = recover_head(c, c->ihead_lnum, c->ihead_offs, sbuf);
975 if (err)
976 return err;
977
978 dbg_rcvry("checking LPT head at %d:%d", c->nhead_lnum, c->nhead_offs);
979 err = recover_head(c, c->nhead_lnum, c->nhead_offs, sbuf);
980 if (err)
981 return err;
982
983 return 0;
984}
985
986
987
988
989
990
991
992
993
994
995
996
997
998static int clean_an_unclean_leb(struct ubifs_info *c,
999 struct ubifs_unclean_leb *ucleb, void *sbuf)
1000{
1001 int err, lnum = ucleb->lnum, offs = 0, len = ucleb->endpt, quiet = 1;
1002 void *buf = sbuf;
1003
1004 dbg_rcvry("LEB %d len %d", lnum, len);
1005
1006 if (len == 0) {
1007
1008 err = ubifs_leb_unmap(c, lnum);
1009 if (err)
1010 return err;
1011 return 0;
1012 }
1013
1014 err = ubifs_leb_read(c, lnum, buf, offs, len, 0);
1015 if (err && err != -EBADMSG)
1016 return err;
1017
1018 while (len >= 8) {
1019 int ret;
1020
1021 cond_resched();
1022
1023
1024 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
1025
1026 if (ret == SCANNED_A_NODE) {
1027
1028 struct ubifs_ch *ch = buf;
1029 int node_len;
1030
1031 node_len = ALIGN(le32_to_cpu(ch->len), 8);
1032 offs += node_len;
1033 buf += node_len;
1034 len -= node_len;
1035 continue;
1036 }
1037
1038 if (ret > 0) {
1039
1040 offs += ret;
1041 buf += ret;
1042 len -= ret;
1043 continue;
1044 }
1045
1046 if (ret == SCANNED_EMPTY_SPACE) {
1047 ubifs_err("unexpected empty space at %d:%d",
1048 lnum, offs);
1049 return -EUCLEAN;
1050 }
1051
1052 if (quiet) {
1053
1054 quiet = 0;
1055 continue;
1056 }
1057
1058 ubifs_scanned_corruption(c, lnum, offs, buf);
1059 return -EUCLEAN;
1060 }
1061
1062
1063 len = ALIGN(ucleb->endpt, c->min_io_size);
1064 if (len > ucleb->endpt) {
1065 int pad_len = len - ALIGN(ucleb->endpt, 8);
1066
1067 if (pad_len > 0) {
1068 buf = c->sbuf + len - pad_len;
1069 ubifs_pad(c, buf, pad_len);
1070 }
1071 }
1072
1073
1074 err = ubifs_leb_change(c, lnum, sbuf, len);
1075 if (err)
1076 return err;
1077
1078 dbg_rcvry("cleaned LEB %d", lnum);
1079
1080 return 0;
1081}
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094int ubifs_clean_lebs(struct ubifs_info *c, void *sbuf)
1095{
1096 dbg_rcvry("recovery");
1097 while (!list_empty(&c->unclean_leb_list)) {
1098 struct ubifs_unclean_leb *ucleb;
1099 int err;
1100
1101 ucleb = list_entry(c->unclean_leb_list.next,
1102 struct ubifs_unclean_leb, list);
1103 err = clean_an_unclean_leb(c, ucleb, sbuf);
1104 if (err)
1105 return err;
1106 list_del(&ucleb->list);
1107 kfree(ucleb);
1108 }
1109 return 0;
1110}
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120static int grab_empty_leb(struct ubifs_info *c)
1121{
1122 int lnum, err;
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139 lnum = ubifs_find_free_leb_for_idx(c);
1140 if (lnum < 0) {
1141 ubifs_err("could not find an empty LEB");
1142 ubifs_dump_lprops(c);
1143 ubifs_dump_budg(c, &c->bi);
1144 return lnum;
1145 }
1146
1147
1148 err = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
1149 LPROPS_INDEX, 0);
1150 if (err)
1151 return err;
1152
1153 c->gc_lnum = lnum;
1154 dbg_rcvry("found empty LEB %d, run commit", lnum);
1155
1156 return ubifs_run_commit(c);
1157}
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177int ubifs_rcvry_gc_commit(struct ubifs_info *c)
1178{
1179 struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
1180 struct ubifs_lprops lp;
1181 int err;
1182
1183 dbg_rcvry("GC head LEB %d, offs %d", wbuf->lnum, wbuf->offs);
1184
1185 c->gc_lnum = -1;
1186 if (wbuf->lnum == -1 || wbuf->offs == c->leb_size)
1187 return grab_empty_leb(c);
1188
1189 err = ubifs_find_dirty_leb(c, &lp, wbuf->offs, 2);
1190 if (err) {
1191 if (err != -ENOSPC)
1192 return err;
1193
1194 dbg_rcvry("could not find a dirty LEB");
1195 return grab_empty_leb(c);
1196 }
1197
1198 ubifs_assert(!(lp.flags & LPROPS_INDEX));
1199 ubifs_assert(lp.free + lp.dirty >= wbuf->offs);
1200
1201
1202
1203
1204
1205 dbg_rcvry("committing");
1206 err = ubifs_run_commit(c);
1207 if (err)
1208 return err;
1209
1210 dbg_rcvry("GC'ing LEB %d", lp.lnum);
1211 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
1212 err = ubifs_garbage_collect_leb(c, &lp);
1213 if (err >= 0) {
1214 int err2 = ubifs_wbuf_sync_nolock(wbuf);
1215
1216 if (err2)
1217 err = err2;
1218 }
1219 mutex_unlock(&wbuf->io_mutex);
1220 if (err < 0) {
1221 ubifs_err("GC failed, error %d", err);
1222 if (err == -EAGAIN)
1223 err = -EINVAL;
1224 return err;
1225 }
1226
1227 ubifs_assert(err == LEB_RETAINED);
1228 if (err != LEB_RETAINED)
1229 return -EINVAL;
1230
1231 err = ubifs_leb_unmap(c, c->gc_lnum);
1232 if (err)
1233 return err;
1234
1235 dbg_rcvry("allocated LEB %d for GC", lp.lnum);
1236 return 0;
1237}
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248struct size_entry {
1249 struct rb_node rb;
1250 ino_t inum;
1251 loff_t i_size;
1252 loff_t d_size;
1253 int exists;
1254 struct inode *inode;
1255};
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265static int add_ino(struct ubifs_info *c, ino_t inum, loff_t i_size,
1266 loff_t d_size, int exists)
1267{
1268 struct rb_node **p = &c->size_tree.rb_node, *parent = NULL;
1269 struct size_entry *e;
1270
1271 while (*p) {
1272 parent = *p;
1273 e = rb_entry(parent, struct size_entry, rb);
1274 if (inum < e->inum)
1275 p = &(*p)->rb_left;
1276 else
1277 p = &(*p)->rb_right;
1278 }
1279
1280 e = kzalloc(sizeof(struct size_entry), GFP_KERNEL);
1281 if (!e)
1282 return -ENOMEM;
1283
1284 e->inum = inum;
1285 e->i_size = i_size;
1286 e->d_size = d_size;
1287 e->exists = exists;
1288
1289 rb_link_node(&e->rb, parent, p);
1290 rb_insert_color(&e->rb, &c->size_tree);
1291
1292 return 0;
1293}
1294
1295
1296
1297
1298
1299
1300static struct size_entry *find_ino(struct ubifs_info *c, ino_t inum)
1301{
1302 struct rb_node *p = c->size_tree.rb_node;
1303 struct size_entry *e;
1304
1305 while (p) {
1306 e = rb_entry(p, struct size_entry, rb);
1307 if (inum < e->inum)
1308 p = p->rb_left;
1309 else if (inum > e->inum)
1310 p = p->rb_right;
1311 else
1312 return e;
1313 }
1314 return NULL;
1315}
1316
1317
1318
1319
1320
1321
1322static void remove_ino(struct ubifs_info *c, ino_t inum)
1323{
1324 struct size_entry *e = find_ino(c, inum);
1325
1326 if (!e)
1327 return;
1328 rb_erase(&e->rb, &c->size_tree);
1329 kfree(e);
1330}
1331
1332
1333
1334
1335
1336void ubifs_destroy_size_tree(struct ubifs_info *c)
1337{
1338 struct size_entry *e, *n;
1339
1340 rbtree_postorder_for_each_entry_safe(e, n, &c->size_tree, rb) {
1341 if (e->inode)
1342 iput(e->inode);
1343 kfree(e);
1344 }
1345
1346 c->size_tree = RB_ROOT;
1347}
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374int ubifs_recover_size_accum(struct ubifs_info *c, union ubifs_key *key,
1375 int deletion, loff_t new_size)
1376{
1377 ino_t inum = key_inum(c, key);
1378 struct size_entry *e;
1379 int err;
1380
1381 switch (key_type(c, key)) {
1382 case UBIFS_INO_KEY:
1383 if (deletion)
1384 remove_ino(c, inum);
1385 else {
1386 e = find_ino(c, inum);
1387 if (e) {
1388 e->i_size = new_size;
1389 e->exists = 1;
1390 } else {
1391 err = add_ino(c, inum, new_size, 0, 1);
1392 if (err)
1393 return err;
1394 }
1395 }
1396 break;
1397 case UBIFS_DATA_KEY:
1398 e = find_ino(c, inum);
1399 if (e) {
1400 if (new_size > e->d_size)
1401 e->d_size = new_size;
1402 } else {
1403 err = add_ino(c, inum, 0, new_size, 0);
1404 if (err)
1405 return err;
1406 }
1407 break;
1408 case UBIFS_TRUN_KEY:
1409 e = find_ino(c, inum);
1410 if (e)
1411 e->d_size = new_size;
1412 break;
1413 }
1414 return 0;
1415}
1416
1417
1418
1419
1420
1421
1422static int fix_size_in_place(struct ubifs_info *c, struct size_entry *e)
1423{
1424 struct ubifs_ino_node *ino = c->sbuf;
1425 unsigned char *p;
1426 union ubifs_key key;
1427 int err, lnum, offs, len;
1428 loff_t i_size;
1429 uint32_t crc;
1430
1431
1432 ino_key_init(c, &key, e->inum);
1433 err = ubifs_tnc_locate(c, &key, ino, &lnum, &offs);
1434 if (err)
1435 goto out;
1436
1437
1438
1439
1440 i_size = le64_to_cpu(ino->size);
1441 if (i_size >= e->d_size)
1442 return 0;
1443
1444 err = ubifs_leb_read(c, lnum, c->sbuf, 0, c->leb_size, 1);
1445 if (err)
1446 goto out;
1447
1448 ino = c->sbuf + offs;
1449 ino->size = cpu_to_le64(e->d_size);
1450 len = le32_to_cpu(ino->ch.len);
1451 crc = crc32(UBIFS_CRC32_INIT, (void *)ino + 8, len - 8);
1452 ino->ch.crc = cpu_to_le32(crc);
1453
1454 p = c->sbuf;
1455 len = c->leb_size - 1;
1456 while (p[len] == 0xff)
1457 len -= 1;
1458 len = ALIGN(len + 1, c->min_io_size);
1459
1460 err = ubifs_leb_change(c, lnum, c->sbuf, len);
1461 if (err)
1462 goto out;
1463 dbg_rcvry("inode %lu at %d:%d size %lld -> %lld",
1464 (unsigned long)e->inum, lnum, offs, i_size, e->d_size);
1465 return 0;
1466
1467out:
1468 ubifs_warn("inode %lu failed to fix size %lld -> %lld error %d",
1469 (unsigned long)e->inum, e->i_size, e->d_size, err);
1470 return err;
1471}
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482int ubifs_recover_size(struct ubifs_info *c)
1483{
1484 struct rb_node *this = rb_first(&c->size_tree);
1485
1486 while (this) {
1487 struct size_entry *e;
1488 int err;
1489
1490 e = rb_entry(this, struct size_entry, rb);
1491 if (!e->exists) {
1492 union ubifs_key key;
1493
1494 ino_key_init(c, &key, e->inum);
1495 err = ubifs_tnc_lookup(c, &key, c->sbuf);
1496 if (err && err != -ENOENT)
1497 return err;
1498 if (err == -ENOENT) {
1499
1500 dbg_rcvry("removing ino %lu",
1501 (unsigned long)e->inum);
1502 err = ubifs_tnc_remove_ino(c, e->inum);
1503 if (err)
1504 return err;
1505 } else {
1506 struct ubifs_ino_node *ino = c->sbuf;
1507
1508 e->exists = 1;
1509 e->i_size = le64_to_cpu(ino->size);
1510 }
1511 }
1512
1513 if (e->exists && e->i_size < e->d_size) {
1514 if (c->ro_mount) {
1515
1516 struct inode *inode;
1517 struct ubifs_inode *ui;
1518
1519 ubifs_assert(!e->inode);
1520
1521 inode = ubifs_iget(c->vfs_sb, e->inum);
1522 if (IS_ERR(inode))
1523 return PTR_ERR(inode);
1524
1525 ui = ubifs_inode(inode);
1526 if (inode->i_size < e->d_size) {
1527 dbg_rcvry("ino %lu size %lld -> %lld",
1528 (unsigned long)e->inum,
1529 inode->i_size, e->d_size);
1530 inode->i_size = e->d_size;
1531 ui->ui_size = e->d_size;
1532 ui->synced_i_size = e->d_size;
1533 e->inode = inode;
1534 this = rb_next(this);
1535 continue;
1536 }
1537 iput(inode);
1538 } else {
1539
1540 err = fix_size_in_place(c, e);
1541 if (err)
1542 return err;
1543 if (e->inode)
1544 iput(e->inode);
1545 }
1546 }
1547
1548 this = rb_next(this);
1549 rb_erase(&e->rb, &c->size_tree);
1550 kfree(e);
1551 }
1552
1553 return 0;
1554}
1555