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(c, "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(c, "failed to recover master node");
364 if (mst1) {
365 ubifs_err(c, "dumping first master node");
366 ubifs_dump_node(c, mst1);
367 }
368 if (mst2) {
369 ubifs_err(c, "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 dbg_rcvry("cleaning corruption at %d:%d", lnum, *offs);
446
447 ubifs_assert(!(*offs & 7));
448 empty_offs = ALIGN(*offs, c->min_io_size);
449 pad_len = empty_offs - *offs;
450 ubifs_pad(c, *buf, pad_len);
451 *offs += pad_len;
452 *buf += pad_len;
453 *len -= pad_len;
454 memset(*buf, 0xff, c->leb_size - empty_offs);
455}
456
457
458
459
460
461
462
463
464
465
466
467
468
469static int no_more_nodes(const struct ubifs_info *c, void *buf, int len,
470 int lnum, int offs)
471{
472 struct ubifs_ch *ch = buf;
473 int skip, dlen = le32_to_cpu(ch->len);
474
475
476 skip = ALIGN(offs + UBIFS_CH_SZ, c->max_write_size) - offs;
477 if (is_empty(buf + skip, len - skip))
478 return 1;
479
480
481
482
483 if (ubifs_check_node(c, buf, lnum, offs, 1, 0) != -EUCLEAN) {
484 dbg_rcvry("unexpected bad common header at %d:%d", lnum, offs);
485 return 0;
486 }
487
488 skip = ALIGN(offs + dlen, c->max_write_size) - offs;
489
490 if (is_empty(buf + skip, len - skip))
491 return 1;
492 dbg_rcvry("unexpected data at %d:%d", lnum, offs + skip);
493 return 0;
494}
495
496
497
498
499
500
501
502static int fix_unclean_leb(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
503 int start)
504{
505 int lnum = sleb->lnum, endpt = start;
506
507
508 if (!list_empty(&sleb->nodes)) {
509 struct ubifs_scan_node *snod;
510
511 snod = list_entry(sleb->nodes.prev,
512 struct ubifs_scan_node, list);
513 endpt = snod->offs + snod->len;
514 }
515
516 if (c->ro_mount && !c->remounting_rw) {
517
518 struct ubifs_unclean_leb *ucleb;
519
520 dbg_rcvry("need to fix LEB %d start %d endpt %d",
521 lnum, start, sleb->endpt);
522 ucleb = kzalloc(sizeof(struct ubifs_unclean_leb), GFP_NOFS);
523 if (!ucleb)
524 return -ENOMEM;
525 ucleb->lnum = lnum;
526 ucleb->endpt = endpt;
527 list_add_tail(&ucleb->list, &c->unclean_leb_list);
528 } else {
529
530 int err;
531
532 dbg_rcvry("fixing LEB %d start %d endpt %d",
533 lnum, start, sleb->endpt);
534 if (endpt == 0) {
535 err = ubifs_leb_unmap(c, lnum);
536 if (err)
537 return err;
538 } else {
539 int len = ALIGN(endpt, c->min_io_size);
540
541 if (start) {
542 err = ubifs_leb_read(c, lnum, sleb->buf, 0,
543 start, 1);
544 if (err)
545 return err;
546 }
547
548 if (len > endpt) {
549 int pad_len = len - ALIGN(endpt, 8);
550
551 if (pad_len > 0) {
552 void *buf = sleb->buf + len - pad_len;
553
554 ubifs_pad(c, buf, pad_len);
555 }
556 }
557 err = ubifs_leb_change(c, lnum, sleb->buf, len);
558 if (err)
559 return err;
560 }
561 }
562 return 0;
563}
564
565
566
567
568
569
570
571
572
573static void drop_last_group(struct ubifs_scan_leb *sleb, int *offs)
574{
575 while (!list_empty(&sleb->nodes)) {
576 struct ubifs_scan_node *snod;
577 struct ubifs_ch *ch;
578
579 snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
580 list);
581 ch = snod->node;
582 if (ch->group_type != UBIFS_IN_NODE_GROUP)
583 break;
584
585 dbg_rcvry("dropping grouped node at %d:%d",
586 sleb->lnum, snod->offs);
587 *offs = snod->offs;
588 list_del(&snod->list);
589 kfree(snod);
590 sleb->nodes_cnt -= 1;
591 }
592}
593
594
595
596
597
598
599
600
601
602static void drop_last_node(struct ubifs_scan_leb *sleb, int *offs)
603{
604 struct ubifs_scan_node *snod;
605
606 if (!list_empty(&sleb->nodes)) {
607 snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
608 list);
609
610 dbg_rcvry("dropping last node at %d:%d",
611 sleb->lnum, snod->offs);
612 *offs = snod->offs;
613 list_del(&snod->list);
614 kfree(snod);
615 sleb->nodes_cnt -= 1;
616 }
617}
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633struct ubifs_scan_leb *ubifs_recover_leb(struct ubifs_info *c, int lnum,
634 int offs, void *sbuf, int jhead)
635{
636 int ret = 0, err, len = c->leb_size - offs, start = offs, min_io_unit;
637 int grouped = jhead == -1 ? 0 : c->jheads[jhead].grouped;
638 struct ubifs_scan_leb *sleb;
639 void *buf = sbuf + offs;
640
641 dbg_rcvry("%d:%d, jhead %d, grouped %d", lnum, offs, jhead, grouped);
642
643 sleb = ubifs_start_scan(c, lnum, offs, sbuf);
644 if (IS_ERR(sleb))
645 return sleb;
646
647 ubifs_assert(len >= 8);
648 while (len >= 8) {
649 dbg_scan("look at LEB %d:%d (%d bytes left)",
650 lnum, offs, len);
651
652 cond_resched();
653
654
655
656
657
658 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
659 if (ret == SCANNED_A_NODE) {
660
661 struct ubifs_ch *ch = buf;
662 int node_len;
663
664 err = ubifs_add_snod(c, sleb, buf, offs);
665 if (err)
666 goto error;
667 node_len = ALIGN(le32_to_cpu(ch->len), 8);
668 offs += node_len;
669 buf += node_len;
670 len -= node_len;
671 } else if (ret > 0) {
672
673 offs += ret;
674 buf += ret;
675 len -= ret;
676 } else if (ret == SCANNED_EMPTY_SPACE ||
677 ret == SCANNED_GARBAGE ||
678 ret == SCANNED_A_BAD_PAD_NODE ||
679 ret == SCANNED_A_CORRUPT_NODE) {
680 dbg_rcvry("found corruption (%d) at %d:%d",
681 ret, lnum, offs);
682 break;
683 } else {
684 ubifs_err(c, "unexpected return value %d", ret);
685 err = -EINVAL;
686 goto error;
687 }
688 }
689
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)) {
698 int corruption = first_non_ff(buf, len);
699
700
701
702
703
704 ubifs_err(c, "corrupt empty space LEB %d:%d, corruption starts at %d",
705 lnum, offs, corruption);
706
707 offs += corruption;
708 buf += corruption;
709 goto corrupted;
710 }
711 }
712
713 min_io_unit = round_down(offs, c->min_io_size);
714 if (grouped)
715
716
717
718
719 drop_last_group(sleb, &offs);
720
721 if (jhead == GCHD) {
722
723
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 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);
780 ubifs_end_scan(c, sleb, lnum, offs);
781
782 err = fix_unclean_leb(c, sleb, start);
783 if (err)
784 goto error;
785
786 return sleb;
787
788corrupted_rescan:
789
790 ubifs_err(c, "corruption %d", ret);
791 ubifs_scan_a_node(c, buf, len, lnum, offs, 0);
792corrupted:
793 ubifs_scanned_corruption(c, lnum, offs, buf);
794 err = -EUCLEAN;
795error:
796 ubifs_err(c, "LEB %d scanning failed", lnum);
797 ubifs_scan_destroy(sleb);
798 return ERR_PTR(err);
799}
800
801
802
803
804
805
806
807
808
809
810static int get_cs_sqnum(struct ubifs_info *c, int lnum, int offs,
811 unsigned long long *cs_sqnum)
812{
813 struct ubifs_cs_node *cs_node = NULL;
814 int err, ret;
815
816 dbg_rcvry("at %d:%d", lnum, offs);
817 cs_node = kmalloc(UBIFS_CS_NODE_SZ, GFP_KERNEL);
818 if (!cs_node)
819 return -ENOMEM;
820 if (c->leb_size - offs < UBIFS_CS_NODE_SZ)
821 goto out_err;
822 err = ubifs_leb_read(c, lnum, (void *)cs_node, offs,
823 UBIFS_CS_NODE_SZ, 0);
824 if (err && err != -EBADMSG)
825 goto out_free;
826 ret = ubifs_scan_a_node(c, cs_node, UBIFS_CS_NODE_SZ, lnum, offs, 0);
827 if (ret != SCANNED_A_NODE) {
828 ubifs_err(c, "Not a valid node");
829 goto out_err;
830 }
831 if (cs_node->ch.node_type != UBIFS_CS_NODE) {
832 ubifs_err(c, "Node a CS node, type is %d", cs_node->ch.node_type);
833 goto out_err;
834 }
835 if (le64_to_cpu(cs_node->cmt_no) != c->cmt_no) {
836 ubifs_err(c, "CS node cmt_no %llu != current cmt_no %llu",
837 (unsigned long long)le64_to_cpu(cs_node->cmt_no),
838 c->cmt_no);
839 goto out_err;
840 }
841 *cs_sqnum = le64_to_cpu(cs_node->ch.sqnum);
842 dbg_rcvry("commit start sqnum %llu", *cs_sqnum);
843 kfree(cs_node);
844 return 0;
845
846out_err:
847 err = -EINVAL;
848out_free:
849 ubifs_err(c, "failed to get CS sqnum");
850 kfree(cs_node);
851 return err;
852}
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867struct ubifs_scan_leb *ubifs_recover_log_leb(struct ubifs_info *c, int lnum,
868 int offs, void *sbuf)
869{
870 struct ubifs_scan_leb *sleb;
871 int next_lnum;
872
873 dbg_rcvry("LEB %d", lnum);
874 next_lnum = lnum + 1;
875 if (next_lnum >= UBIFS_LOG_LNUM + c->log_lebs)
876 next_lnum = UBIFS_LOG_LNUM;
877 if (next_lnum != c->ltail_lnum) {
878
879
880
881
882 sleb = ubifs_scan(c, next_lnum, 0, sbuf, 0);
883 if (IS_ERR(sleb))
884 return sleb;
885 if (sleb->nodes_cnt) {
886 struct ubifs_scan_node *snod;
887 unsigned long long cs_sqnum = c->cs_sqnum;
888
889 snod = list_entry(sleb->nodes.next,
890 struct ubifs_scan_node, list);
891 if (cs_sqnum == 0) {
892 int err;
893
894 err = get_cs_sqnum(c, lnum, offs, &cs_sqnum);
895 if (err) {
896 ubifs_scan_destroy(sleb);
897 return ERR_PTR(err);
898 }
899 }
900 if (snod->sqnum > cs_sqnum) {
901 ubifs_err(c, "unrecoverable log corruption in LEB %d",
902 lnum);
903 ubifs_scan_destroy(sleb);
904 return ERR_PTR(-EUCLEAN);
905 }
906 }
907 ubifs_scan_destroy(sleb);
908 }
909 return ubifs_recover_leb(c, lnum, offs, sbuf, -1);
910}
911
912
913
914
915
916
917
918
919
920
921
922
923static int recover_head(struct ubifs_info *c, int lnum, int offs, void *sbuf)
924{
925 int len = c->max_write_size, err;
926
927 if (offs + len > c->leb_size)
928 len = c->leb_size - offs;
929
930 if (!len)
931 return 0;
932
933
934 err = ubifs_leb_read(c, lnum, sbuf, offs, len, 1);
935 if (err || !is_empty(sbuf, len)) {
936 dbg_rcvry("cleaning head at %d:%d", lnum, offs);
937 if (offs == 0)
938 return ubifs_leb_unmap(c, lnum);
939 err = ubifs_leb_read(c, lnum, sbuf, 0, offs, 1);
940 if (err)
941 return err;
942 return ubifs_leb_change(c, lnum, sbuf, offs);
943 }
944
945 return 0;
946}
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965int ubifs_recover_inl_heads(struct ubifs_info *c, void *sbuf)
966{
967 int err;
968
969 ubifs_assert(!c->ro_mount || c->remounting_rw);
970
971 dbg_rcvry("checking index head at %d:%d", c->ihead_lnum, c->ihead_offs);
972 err = recover_head(c, c->ihead_lnum, c->ihead_offs, sbuf);
973 if (err)
974 return err;
975
976 dbg_rcvry("checking LPT head at %d:%d", c->nhead_lnum, c->nhead_offs);
977
978 return recover_head(c, c->nhead_lnum, c->nhead_offs, sbuf);
979}
980
981
982
983
984
985
986
987
988
989
990
991
992
993static int clean_an_unclean_leb(struct ubifs_info *c,
994 struct ubifs_unclean_leb *ucleb, void *sbuf)
995{
996 int err, lnum = ucleb->lnum, offs = 0, len = ucleb->endpt, quiet = 1;
997 void *buf = sbuf;
998
999 dbg_rcvry("LEB %d len %d", lnum, len);
1000
1001 if (len == 0) {
1002
1003 return ubifs_leb_unmap(c, lnum);
1004 }
1005
1006 err = ubifs_leb_read(c, lnum, buf, offs, len, 0);
1007 if (err && err != -EBADMSG)
1008 return err;
1009
1010 while (len >= 8) {
1011 int ret;
1012
1013 cond_resched();
1014
1015
1016 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
1017
1018 if (ret == SCANNED_A_NODE) {
1019
1020 struct ubifs_ch *ch = buf;
1021 int node_len;
1022
1023 node_len = ALIGN(le32_to_cpu(ch->len), 8);
1024 offs += node_len;
1025 buf += node_len;
1026 len -= node_len;
1027 continue;
1028 }
1029
1030 if (ret > 0) {
1031
1032 offs += ret;
1033 buf += ret;
1034 len -= ret;
1035 continue;
1036 }
1037
1038 if (ret == SCANNED_EMPTY_SPACE) {
1039 ubifs_err(c, "unexpected empty space at %d:%d",
1040 lnum, offs);
1041 return -EUCLEAN;
1042 }
1043
1044 if (quiet) {
1045
1046 quiet = 0;
1047 continue;
1048 }
1049
1050 ubifs_scanned_corruption(c, lnum, offs, buf);
1051 return -EUCLEAN;
1052 }
1053
1054
1055 len = ALIGN(ucleb->endpt, c->min_io_size);
1056 if (len > ucleb->endpt) {
1057 int pad_len = len - ALIGN(ucleb->endpt, 8);
1058
1059 if (pad_len > 0) {
1060 buf = c->sbuf + len - pad_len;
1061 ubifs_pad(c, buf, pad_len);
1062 }
1063 }
1064
1065
1066 err = ubifs_leb_change(c, lnum, sbuf, len);
1067 if (err)
1068 return err;
1069
1070 dbg_rcvry("cleaned LEB %d", lnum);
1071
1072 return 0;
1073}
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086int ubifs_clean_lebs(struct ubifs_info *c, void *sbuf)
1087{
1088 dbg_rcvry("recovery");
1089 while (!list_empty(&c->unclean_leb_list)) {
1090 struct ubifs_unclean_leb *ucleb;
1091 int err;
1092
1093 ucleb = list_entry(c->unclean_leb_list.next,
1094 struct ubifs_unclean_leb, list);
1095 err = clean_an_unclean_leb(c, ucleb, sbuf);
1096 if (err)
1097 return err;
1098 list_del(&ucleb->list);
1099 kfree(ucleb);
1100 }
1101 return 0;
1102}
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112static int grab_empty_leb(struct ubifs_info *c)
1113{
1114 int lnum, err;
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131 lnum = ubifs_find_free_leb_for_idx(c);
1132 if (lnum < 0) {
1133 ubifs_err(c, "could not find an empty LEB");
1134 ubifs_dump_lprops(c);
1135 ubifs_dump_budg(c, &c->bi);
1136 return lnum;
1137 }
1138
1139
1140 err = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
1141 LPROPS_INDEX, 0);
1142 if (err)
1143 return err;
1144
1145 c->gc_lnum = lnum;
1146 dbg_rcvry("found empty LEB %d, run commit", lnum);
1147
1148 return ubifs_run_commit(c);
1149}
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169int ubifs_rcvry_gc_commit(struct ubifs_info *c)
1170{
1171 struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
1172 struct ubifs_lprops lp;
1173 int err;
1174
1175 dbg_rcvry("GC head LEB %d, offs %d", wbuf->lnum, wbuf->offs);
1176
1177 c->gc_lnum = -1;
1178 if (wbuf->lnum == -1 || wbuf->offs == c->leb_size)
1179 return grab_empty_leb(c);
1180
1181 err = ubifs_find_dirty_leb(c, &lp, wbuf->offs, 2);
1182 if (err) {
1183 if (err != -ENOSPC)
1184 return err;
1185
1186 dbg_rcvry("could not find a dirty LEB");
1187 return grab_empty_leb(c);
1188 }
1189
1190 ubifs_assert(!(lp.flags & LPROPS_INDEX));
1191 ubifs_assert(lp.free + lp.dirty >= wbuf->offs);
1192
1193
1194
1195
1196
1197 dbg_rcvry("committing");
1198 err = ubifs_run_commit(c);
1199 if (err)
1200 return err;
1201
1202 dbg_rcvry("GC'ing LEB %d", lp.lnum);
1203 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
1204 err = ubifs_garbage_collect_leb(c, &lp);
1205 if (err >= 0) {
1206 int err2 = ubifs_wbuf_sync_nolock(wbuf);
1207
1208 if (err2)
1209 err = err2;
1210 }
1211 mutex_unlock(&wbuf->io_mutex);
1212 if (err < 0) {
1213 ubifs_err(c, "GC failed, error %d", err);
1214 if (err == -EAGAIN)
1215 err = -EINVAL;
1216 return err;
1217 }
1218
1219 ubifs_assert(err == LEB_RETAINED);
1220 if (err != LEB_RETAINED)
1221 return -EINVAL;
1222
1223 err = ubifs_leb_unmap(c, c->gc_lnum);
1224 if (err)
1225 return err;
1226
1227 dbg_rcvry("allocated LEB %d for GC", lp.lnum);
1228 return 0;
1229}
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240struct size_entry {
1241 struct rb_node rb;
1242 ino_t inum;
1243 loff_t i_size;
1244 loff_t d_size;
1245 int exists;
1246 struct inode *inode;
1247};
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257static int add_ino(struct ubifs_info *c, ino_t inum, loff_t i_size,
1258 loff_t d_size, int exists)
1259{
1260 struct rb_node **p = &c->size_tree.rb_node, *parent = NULL;
1261 struct size_entry *e;
1262
1263 while (*p) {
1264 parent = *p;
1265 e = rb_entry(parent, struct size_entry, rb);
1266 if (inum < e->inum)
1267 p = &(*p)->rb_left;
1268 else
1269 p = &(*p)->rb_right;
1270 }
1271
1272 e = kzalloc(sizeof(struct size_entry), GFP_KERNEL);
1273 if (!e)
1274 return -ENOMEM;
1275
1276 e->inum = inum;
1277 e->i_size = i_size;
1278 e->d_size = d_size;
1279 e->exists = exists;
1280
1281 rb_link_node(&e->rb, parent, p);
1282 rb_insert_color(&e->rb, &c->size_tree);
1283
1284 return 0;
1285}
1286
1287
1288
1289
1290
1291
1292static struct size_entry *find_ino(struct ubifs_info *c, ino_t inum)
1293{
1294 struct rb_node *p = c->size_tree.rb_node;
1295 struct size_entry *e;
1296
1297 while (p) {
1298 e = rb_entry(p, struct size_entry, rb);
1299 if (inum < e->inum)
1300 p = p->rb_left;
1301 else if (inum > e->inum)
1302 p = p->rb_right;
1303 else
1304 return e;
1305 }
1306 return NULL;
1307}
1308
1309
1310
1311
1312
1313
1314static void remove_ino(struct ubifs_info *c, ino_t inum)
1315{
1316 struct size_entry *e = find_ino(c, inum);
1317
1318 if (!e)
1319 return;
1320 rb_erase(&e->rb, &c->size_tree);
1321 kfree(e);
1322}
1323
1324
1325
1326
1327
1328void ubifs_destroy_size_tree(struct ubifs_info *c)
1329{
1330 struct size_entry *e, *n;
1331
1332 rbtree_postorder_for_each_entry_safe(e, n, &c->size_tree, rb) {
1333 iput(e->inode);
1334 kfree(e);
1335 }
1336
1337 c->size_tree = RB_ROOT;
1338}
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365int ubifs_recover_size_accum(struct ubifs_info *c, union ubifs_key *key,
1366 int deletion, loff_t new_size)
1367{
1368 ino_t inum = key_inum(c, key);
1369 struct size_entry *e;
1370 int err;
1371
1372 switch (key_type(c, key)) {
1373 case UBIFS_INO_KEY:
1374 if (deletion)
1375 remove_ino(c, inum);
1376 else {
1377 e = find_ino(c, inum);
1378 if (e) {
1379 e->i_size = new_size;
1380 e->exists = 1;
1381 } else {
1382 err = add_ino(c, inum, new_size, 0, 1);
1383 if (err)
1384 return err;
1385 }
1386 }
1387 break;
1388 case UBIFS_DATA_KEY:
1389 e = find_ino(c, inum);
1390 if (e) {
1391 if (new_size > e->d_size)
1392 e->d_size = new_size;
1393 } else {
1394 err = add_ino(c, inum, 0, new_size, 0);
1395 if (err)
1396 return err;
1397 }
1398 break;
1399 case UBIFS_TRUN_KEY:
1400 e = find_ino(c, inum);
1401 if (e)
1402 e->d_size = new_size;
1403 break;
1404 }
1405 return 0;
1406}
1407
1408
1409
1410
1411
1412
1413static int fix_size_in_place(struct ubifs_info *c, struct size_entry *e)
1414{
1415 struct ubifs_ino_node *ino = c->sbuf;
1416 unsigned char *p;
1417 union ubifs_key key;
1418 int err, lnum, offs, len;
1419 loff_t i_size;
1420 uint32_t crc;
1421
1422
1423 ino_key_init(c, &key, e->inum);
1424 err = ubifs_tnc_locate(c, &key, ino, &lnum, &offs);
1425 if (err)
1426 goto out;
1427
1428
1429
1430
1431 i_size = le64_to_cpu(ino->size);
1432 if (i_size >= e->d_size)
1433 return 0;
1434
1435 err = ubifs_leb_read(c, lnum, c->sbuf, 0, c->leb_size, 1);
1436 if (err)
1437 goto out;
1438
1439 ino = c->sbuf + offs;
1440 ino->size = cpu_to_le64(e->d_size);
1441 len = le32_to_cpu(ino->ch.len);
1442 crc = crc32(UBIFS_CRC32_INIT, (void *)ino + 8, len - 8);
1443 ino->ch.crc = cpu_to_le32(crc);
1444
1445 p = c->sbuf;
1446 len = c->leb_size - 1;
1447 while (p[len] == 0xff)
1448 len -= 1;
1449 len = ALIGN(len + 1, c->min_io_size);
1450
1451 err = ubifs_leb_change(c, lnum, c->sbuf, len);
1452 if (err)
1453 goto out;
1454 dbg_rcvry("inode %lu at %d:%d size %lld -> %lld",
1455 (unsigned long)e->inum, lnum, offs, i_size, e->d_size);
1456 return 0;
1457
1458out:
1459 ubifs_warn(c, "inode %lu failed to fix size %lld -> %lld error %d",
1460 (unsigned long)e->inum, e->i_size, e->d_size, err);
1461 return err;
1462}
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473int ubifs_recover_size(struct ubifs_info *c)
1474{
1475 struct rb_node *this = rb_first(&c->size_tree);
1476
1477 while (this) {
1478 struct size_entry *e;
1479 int err;
1480
1481 e = rb_entry(this, struct size_entry, rb);
1482 if (!e->exists) {
1483 union ubifs_key key;
1484
1485 ino_key_init(c, &key, e->inum);
1486 err = ubifs_tnc_lookup(c, &key, c->sbuf);
1487 if (err && err != -ENOENT)
1488 return err;
1489 if (err == -ENOENT) {
1490
1491 dbg_rcvry("removing ino %lu",
1492 (unsigned long)e->inum);
1493 err = ubifs_tnc_remove_ino(c, e->inum);
1494 if (err)
1495 return err;
1496 } else {
1497 struct ubifs_ino_node *ino = c->sbuf;
1498
1499 e->exists = 1;
1500 e->i_size = le64_to_cpu(ino->size);
1501 }
1502 }
1503
1504 if (e->exists && e->i_size < e->d_size) {
1505 if (c->ro_mount) {
1506
1507 struct inode *inode;
1508 struct ubifs_inode *ui;
1509
1510 ubifs_assert(!e->inode);
1511
1512 inode = ubifs_iget(c->vfs_sb, e->inum);
1513 if (IS_ERR(inode))
1514 return PTR_ERR(inode);
1515
1516 ui = ubifs_inode(inode);
1517 if (inode->i_size < e->d_size) {
1518 dbg_rcvry("ino %lu size %lld -> %lld",
1519 (unsigned long)e->inum,
1520 inode->i_size, e->d_size);
1521 inode->i_size = e->d_size;
1522 ui->ui_size = e->d_size;
1523 ui->synced_i_size = e->d_size;
1524 e->inode = inode;
1525 this = rb_next(this);
1526 continue;
1527 }
1528 iput(inode);
1529 } else {
1530
1531 err = fix_size_in_place(c, e);
1532 if (err)
1533 return err;
1534 iput(e->inode);
1535 }
1536 }
1537
1538 this = rb_next(this);
1539 rb_erase(&e->rb, &c->size_tree);
1540 kfree(e);
1541 }
1542
1543 return 0;
1544}
1545