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#include "ubifs.h"
34
35
36
37
38
39
40
41
42
43static int is_empty(void *buf, int len)
44{
45 uint8_t *p = buf;
46 int i;
47
48 for (i = 0; i < len; i++)
49 if (*p++ != 0xff)
50 return 0;
51 return 1;
52}
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71static int get_master_node(const struct ubifs_info *c, int lnum, void **pbuf,
72 struct ubifs_mst_node **mst, void **cor)
73{
74 const int sz = c->mst_node_alsz;
75 int err, offs, len;
76 void *sbuf, *buf;
77
78 sbuf = vmalloc(c->leb_size);
79 if (!sbuf)
80 return -ENOMEM;
81
82 err = ubi_read(c->ubi, lnum, sbuf, 0, c->leb_size);
83 if (err && err != -EBADMSG)
84 goto out_free;
85
86
87 offs = 0;
88 buf = sbuf;
89 len = c->leb_size;
90 while (offs + UBIFS_MST_NODE_SZ <= c->leb_size) {
91 struct ubifs_ch *ch = buf;
92
93 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC)
94 break;
95 offs += sz;
96 buf += sz;
97 len -= sz;
98 }
99
100 if (offs) {
101 int ret;
102
103 offs -= sz;
104 buf -= sz;
105 len += sz;
106 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
107 if (ret != SCANNED_A_NODE && offs) {
108
109 offs -= sz;
110 buf -= sz;
111 len += sz;
112 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
113 if (ret != SCANNED_A_NODE)
114
115
116
117
118
119 goto out_err;
120 }
121 if (ret == SCANNED_A_NODE) {
122 struct ubifs_ch *ch = buf;
123
124 if (ch->node_type != UBIFS_MST_NODE)
125 goto out_err;
126 dbg_rcvry("found a master node at %d:%d", lnum, offs);
127 *mst = buf;
128 offs += sz;
129 buf += sz;
130 len -= sz;
131 }
132 }
133
134 if (offs < c->leb_size) {
135 if (!is_empty(buf, min_t(int, len, sz))) {
136 *cor = buf;
137 dbg_rcvry("found corruption at %d:%d", lnum, offs);
138 }
139 offs += sz;
140 buf += sz;
141 len -= sz;
142 }
143
144 if (offs < c->leb_size)
145 if (!is_empty(buf, len))
146 goto out_err;
147 *pbuf = sbuf;
148 return 0;
149
150out_err:
151 err = -EINVAL;
152out_free:
153 vfree(sbuf);
154 *mst = NULL;
155 *cor = NULL;
156 return err;
157}
158
159
160
161
162
163
164
165
166static int write_rcvrd_mst_node(struct ubifs_info *c,
167 struct ubifs_mst_node *mst)
168{
169 int err = 0, lnum = UBIFS_MST_LNUM, sz = c->mst_node_alsz;
170 __le32 save_flags;
171
172 dbg_rcvry("recovery");
173
174 save_flags = mst->flags;
175 mst->flags |= cpu_to_le32(UBIFS_MST_RCVRY);
176
177 ubifs_prepare_node(c, mst, UBIFS_MST_NODE_SZ, 1);
178 err = ubi_leb_change(c->ubi, lnum, mst, sz, UBI_SHORTTERM);
179 if (err)
180 goto out;
181 err = ubi_leb_change(c->ubi, lnum + 1, mst, sz, UBI_SHORTTERM);
182 if (err)
183 goto out;
184out:
185 mst->flags = save_flags;
186 return err;
187}
188
189
190
191
192
193
194
195
196
197
198int ubifs_recover_master_node(struct ubifs_info *c)
199{
200 void *buf1 = NULL, *buf2 = NULL, *cor1 = NULL, *cor2 = NULL;
201 struct ubifs_mst_node *mst1 = NULL, *mst2 = NULL, *mst;
202 const int sz = c->mst_node_alsz;
203 int err, offs1, offs2;
204
205 dbg_rcvry("recovery");
206
207 err = get_master_node(c, UBIFS_MST_LNUM, &buf1, &mst1, &cor1);
208 if (err)
209 goto out_free;
210
211 err = get_master_node(c, UBIFS_MST_LNUM + 1, &buf2, &mst2, &cor2);
212 if (err)
213 goto out_free;
214
215 if (mst1) {
216 offs1 = (void *)mst1 - buf1;
217 if ((le32_to_cpu(mst1->flags) & UBIFS_MST_RCVRY) &&
218 (offs1 == 0 && !cor1)) {
219
220
221
222
223 dbg_rcvry("recovery recovery");
224 mst = mst1;
225 } else if (mst2) {
226 offs2 = (void *)mst2 - buf2;
227 if (offs1 == offs2) {
228
229 if (memcmp((void *)mst1 + UBIFS_CH_SZ,
230 (void *)mst2 + UBIFS_CH_SZ,
231 UBIFS_MST_NODE_SZ - UBIFS_CH_SZ))
232 goto out_err;
233 mst = mst1;
234 } else if (offs2 + sz == offs1) {
235
236 if (cor1)
237 goto out_err;
238 mst = mst1;
239 } else if (offs1 == 0 && offs2 + sz >= c->leb_size) {
240
241 if (cor1)
242 goto out_err;
243 mst = mst1;
244 } else
245 goto out_err;
246 } else {
247
248
249
250
251
252 if (offs1 != 0 || cor1)
253 goto out_err;
254 mst = mst1;
255 }
256 } else {
257 if (!mst2)
258 goto out_err;
259
260
261
262
263 offs2 = (void *)mst2 - buf2;
264 if (offs2 + sz + sz <= c->leb_size)
265 goto out_err;
266 mst = mst2;
267 }
268
269 dbg_rcvry("recovered master node from LEB %d",
270 (mst == mst1 ? UBIFS_MST_LNUM : UBIFS_MST_LNUM + 1));
271
272 memcpy(c->mst_node, mst, UBIFS_MST_NODE_SZ);
273
274 if ((c->vfs_sb->s_flags & MS_RDONLY)) {
275
276 c->rcvrd_mst_node = kmalloc(sz, GFP_KERNEL);
277 if (!c->rcvrd_mst_node) {
278 err = -ENOMEM;
279 goto out_free;
280 }
281 memcpy(c->rcvrd_mst_node, c->mst_node, UBIFS_MST_NODE_SZ);
282 }
283
284 vfree(buf2);
285 vfree(buf1);
286
287 return 0;
288
289out_err:
290 err = -EINVAL;
291out_free:
292 ubifs_err("failed to recover master node");
293 if (mst1) {
294 dbg_err("dumping first master node");
295 dbg_dump_node(c, mst1);
296 }
297 if (mst2) {
298 dbg_err("dumping second master node");
299 dbg_dump_node(c, mst2);
300 }
301 vfree(buf2);
302 vfree(buf1);
303 return err;
304}
305
306
307
308
309
310
311
312
313
314
315int ubifs_write_rcvrd_mst_node(struct ubifs_info *c)
316{
317 int err;
318
319 if (!c->rcvrd_mst_node)
320 return 0;
321 c->rcvrd_mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
322 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
323 err = write_rcvrd_mst_node(c, c->rcvrd_mst_node);
324 if (err)
325 return err;
326 kfree(c->rcvrd_mst_node);
327 c->rcvrd_mst_node = NULL;
328 return 0;
329}
330
331
332
333
334
335
336
337
338
339
340
341
342static int is_last_write(const struct ubifs_info *c, void *buf, int offs)
343{
344 int empty_offs;
345 int check_len;
346 uint8_t *p;
347
348 if (c->min_io_size == 1) {
349 check_len = c->leb_size - offs;
350 p = buf + check_len;
351 for (; check_len > 0; check_len--)
352 if (*--p != 0xff)
353 break;
354
355
356
357
358
359 if (check_len > UBIFS_MAX_NODE_SZ)
360 return 0;
361 return 1;
362 }
363
364
365
366
367
368 empty_offs = ALIGN(offs + 1, c->min_io_size);
369 check_len = c->leb_size - empty_offs;
370 p = buf + empty_offs - offs;
371
372 for (; check_len > 0; check_len--)
373 if (*p++ != 0xff)
374 return 0;
375 return 1;
376}
377
378
379
380
381
382
383
384
385
386
387
388
389
390static void clean_buf(const struct ubifs_info *c, void **buf, int lnum,
391 int *offs, int *len)
392{
393 int empty_offs, pad_len;
394
395 lnum = lnum;
396 dbg_rcvry("cleaning corruption at %d:%d", lnum, *offs);
397
398 if (c->min_io_size == 1) {
399 memset(*buf, 0xff, c->leb_size - *offs);
400 return;
401 }
402
403 ubifs_assert(!(*offs & 7));
404 empty_offs = ALIGN(*offs, c->min_io_size);
405 pad_len = empty_offs - *offs;
406 ubifs_pad(c, *buf, pad_len);
407 *offs += pad_len;
408 *buf += pad_len;
409 *len -= pad_len;
410 memset(*buf, 0xff, c->leb_size - empty_offs);
411}
412
413
414
415
416
417
418
419
420
421
422
423
424
425static int no_more_nodes(const struct ubifs_info *c, void *buf, int len,
426 int lnum, int offs)
427{
428 struct ubifs_ch *ch = buf;
429 int skip, dlen = le32_to_cpu(ch->len);
430
431
432 skip = ALIGN(offs + UBIFS_CH_SZ, c->min_io_size) - offs;
433 if (is_empty(buf + skip, len - skip))
434 return 1;
435
436
437
438
439 if (ubifs_check_node(c, buf, lnum, offs, 1, 0) != -EUCLEAN) {
440 dbg_rcvry("unexpected bad common header at %d:%d", lnum, offs);
441 return 0;
442 }
443
444 skip = ALIGN(offs + dlen, c->min_io_size) - offs;
445
446 if (is_empty(buf + skip, len - skip))
447 return 1;
448 dbg_rcvry("unexpected data at %d:%d", lnum, offs + skip);
449 return 0;
450}
451
452
453
454
455
456
457
458static int fix_unclean_leb(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
459 int start)
460{
461 int lnum = sleb->lnum, endpt = start;
462
463
464 if (!list_empty(&sleb->nodes)) {
465 struct ubifs_scan_node *snod;
466
467 snod = list_entry(sleb->nodes.prev,
468 struct ubifs_scan_node, list);
469 endpt = snod->offs + snod->len;
470 }
471
472 if ((c->vfs_sb->s_flags & MS_RDONLY) && !c->remounting_rw) {
473
474 struct ubifs_unclean_leb *ucleb;
475
476 dbg_rcvry("need to fix LEB %d start %d endpt %d",
477 lnum, start, sleb->endpt);
478 ucleb = kzalloc(sizeof(struct ubifs_unclean_leb), GFP_NOFS);
479 if (!ucleb)
480 return -ENOMEM;
481 ucleb->lnum = lnum;
482 ucleb->endpt = endpt;
483 list_add_tail(&ucleb->list, &c->unclean_leb_list);
484 }
485 return 0;
486}
487
488
489
490
491
492
493
494
495static int drop_incomplete_group(struct ubifs_scan_leb *sleb, int *offs)
496{
497 int dropped = 0;
498
499 while (!list_empty(&sleb->nodes)) {
500 struct ubifs_scan_node *snod;
501 struct ubifs_ch *ch;
502
503 snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
504 list);
505 ch = snod->node;
506 if (ch->group_type != UBIFS_IN_NODE_GROUP)
507 return dropped;
508 dbg_rcvry("dropping node at %d:%d", sleb->lnum, snod->offs);
509 *offs = snod->offs;
510 list_del(&snod->list);
511 kfree(snod);
512 sleb->nodes_cnt -= 1;
513 dropped = 1;
514 }
515 return dropped;
516}
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531struct ubifs_scan_leb *ubifs_recover_leb(struct ubifs_info *c, int lnum,
532 int offs, void *sbuf, int grouped)
533{
534 int err, len = c->leb_size - offs, need_clean = 0, quiet = 1;
535 int empty_chkd = 0, start = offs;
536 struct ubifs_scan_leb *sleb;
537 void *buf = sbuf + offs;
538
539 dbg_rcvry("%d:%d", lnum, offs);
540
541 sleb = ubifs_start_scan(c, lnum, offs, sbuf);
542 if (IS_ERR(sleb))
543 return sleb;
544
545 if (sleb->ecc)
546 need_clean = 1;
547
548 while (len >= 8) {
549 int ret;
550
551 dbg_scan("look at LEB %d:%d (%d bytes left)",
552 lnum, offs, len);
553
554 cond_resched();
555
556
557
558
559
560 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
561
562 if (ret == SCANNED_A_NODE) {
563
564 struct ubifs_ch *ch = buf;
565 int node_len;
566
567 err = ubifs_add_snod(c, sleb, buf, offs);
568 if (err)
569 goto error;
570 node_len = ALIGN(le32_to_cpu(ch->len), 8);
571 offs += node_len;
572 buf += node_len;
573 len -= node_len;
574 continue;
575 }
576
577 if (ret > 0) {
578
579 offs += ret;
580 buf += ret;
581 len -= ret;
582 continue;
583 }
584
585 if (ret == SCANNED_EMPTY_SPACE) {
586 if (!is_empty(buf, len)) {
587 if (!is_last_write(c, buf, offs))
588 break;
589 clean_buf(c, &buf, lnum, &offs, &len);
590 need_clean = 1;
591 }
592 empty_chkd = 1;
593 break;
594 }
595
596 if (ret == SCANNED_GARBAGE || ret == SCANNED_A_BAD_PAD_NODE)
597 if (is_last_write(c, buf, offs)) {
598 clean_buf(c, &buf, lnum, &offs, &len);
599 need_clean = 1;
600 empty_chkd = 1;
601 break;
602 }
603
604 if (ret == SCANNED_A_CORRUPT_NODE)
605 if (no_more_nodes(c, buf, len, lnum, offs)) {
606 clean_buf(c, &buf, lnum, &offs, &len);
607 need_clean = 1;
608 empty_chkd = 1;
609 break;
610 }
611
612 if (quiet) {
613
614 quiet = 0;
615 continue;
616 }
617
618 switch (ret) {
619 case SCANNED_GARBAGE:
620 dbg_err("garbage");
621 goto corrupted;
622 case SCANNED_A_CORRUPT_NODE:
623 case SCANNED_A_BAD_PAD_NODE:
624 dbg_err("bad node");
625 goto corrupted;
626 default:
627 dbg_err("unknown");
628 goto corrupted;
629 }
630 }
631
632 if (!empty_chkd && !is_empty(buf, len)) {
633 if (is_last_write(c, buf, offs)) {
634 clean_buf(c, &buf, lnum, &offs, &len);
635 need_clean = 1;
636 } else {
637 ubifs_err("corrupt empty space at LEB %d:%d",
638 lnum, offs);
639 goto corrupted;
640 }
641 }
642
643
644 if (grouped && drop_incomplete_group(sleb, &offs)) {
645 buf = sbuf + offs;
646 len = c->leb_size - offs;
647 clean_buf(c, &buf, lnum, &offs, &len);
648 need_clean = 1;
649 }
650
651 if (offs % c->min_io_size) {
652 clean_buf(c, &buf, lnum, &offs, &len);
653 need_clean = 1;
654 }
655
656 ubifs_end_scan(c, sleb, lnum, offs);
657
658 if (need_clean) {
659 err = fix_unclean_leb(c, sleb, start);
660 if (err)
661 goto error;
662 }
663
664 return sleb;
665
666corrupted:
667 ubifs_scanned_corruption(c, lnum, offs, buf);
668 err = -EUCLEAN;
669error:
670 ubifs_err("LEB %d scanning failed", lnum);
671 ubifs_scan_destroy(sleb);
672 return ERR_PTR(err);
673}
674
675
676
677
678
679
680
681
682
683
684static int get_cs_sqnum(struct ubifs_info *c, int lnum, int offs,
685 unsigned long long *cs_sqnum)
686{
687 struct ubifs_cs_node *cs_node = NULL;
688 int err, ret;
689
690 dbg_rcvry("at %d:%d", lnum, offs);
691 cs_node = kmalloc(UBIFS_CS_NODE_SZ, GFP_KERNEL);
692 if (!cs_node)
693 return -ENOMEM;
694 if (c->leb_size - offs < UBIFS_CS_NODE_SZ)
695 goto out_err;
696 err = ubi_read(c->ubi, lnum, (void *)cs_node, offs, UBIFS_CS_NODE_SZ);
697 if (err && err != -EBADMSG)
698 goto out_free;
699 ret = ubifs_scan_a_node(c, cs_node, UBIFS_CS_NODE_SZ, lnum, offs, 0);
700 if (ret != SCANNED_A_NODE) {
701 dbg_err("Not a valid node");
702 goto out_err;
703 }
704 if (cs_node->ch.node_type != UBIFS_CS_NODE) {
705 dbg_err("Node a CS node, type is %d", cs_node->ch.node_type);
706 goto out_err;
707 }
708 if (le64_to_cpu(cs_node->cmt_no) != c->cmt_no) {
709 dbg_err("CS node cmt_no %llu != current cmt_no %llu",
710 (unsigned long long)le64_to_cpu(cs_node->cmt_no),
711 c->cmt_no);
712 goto out_err;
713 }
714 *cs_sqnum = le64_to_cpu(cs_node->ch.sqnum);
715 dbg_rcvry("commit start sqnum %llu", *cs_sqnum);
716 kfree(cs_node);
717 return 0;
718
719out_err:
720 err = -EINVAL;
721out_free:
722 ubifs_err("failed to get CS sqnum");
723 kfree(cs_node);
724 return err;
725}
726
727
728
729
730
731
732
733
734
735
736
737
738
739struct ubifs_scan_leb *ubifs_recover_log_leb(struct ubifs_info *c, int lnum,
740 int offs, void *sbuf)
741{
742 struct ubifs_scan_leb *sleb;
743 int next_lnum;
744
745 dbg_rcvry("LEB %d", lnum);
746 next_lnum = lnum + 1;
747 if (next_lnum >= UBIFS_LOG_LNUM + c->log_lebs)
748 next_lnum = UBIFS_LOG_LNUM;
749 if (next_lnum != c->ltail_lnum) {
750
751
752
753
754 sleb = ubifs_scan(c, next_lnum, 0, sbuf);
755 if (IS_ERR(sleb))
756 return sleb;
757 if (sleb->nodes_cnt) {
758 struct ubifs_scan_node *snod;
759 unsigned long long cs_sqnum = c->cs_sqnum;
760
761 snod = list_entry(sleb->nodes.next,
762 struct ubifs_scan_node, list);
763 if (cs_sqnum == 0) {
764 int err;
765
766 err = get_cs_sqnum(c, lnum, offs, &cs_sqnum);
767 if (err) {
768 ubifs_scan_destroy(sleb);
769 return ERR_PTR(err);
770 }
771 }
772 if (snod->sqnum > cs_sqnum) {
773 ubifs_err("unrecoverable log corruption "
774 "in LEB %d", lnum);
775 ubifs_scan_destroy(sleb);
776 return ERR_PTR(-EUCLEAN);
777 }
778 }
779 ubifs_scan_destroy(sleb);
780 }
781 return ubifs_recover_leb(c, lnum, offs, sbuf, 0);
782}
783
784
785
786
787
788
789
790
791
792
793
794
795static int recover_head(const struct ubifs_info *c, int lnum, int offs,
796 void *sbuf)
797{
798 int len, err, need_clean = 0;
799
800 if (c->min_io_size > 1)
801 len = c->min_io_size;
802 else
803 len = 512;
804 if (offs + len > c->leb_size)
805 len = c->leb_size - offs;
806
807 if (!len)
808 return 0;
809
810
811 err = ubi_read(c->ubi, lnum, sbuf, offs, len);
812 if (err)
813 need_clean = 1;
814 else {
815 uint8_t *p = sbuf;
816
817 while (len--)
818 if (*p++ != 0xff) {
819 need_clean = 1;
820 break;
821 }
822 }
823
824 if (need_clean) {
825 dbg_rcvry("cleaning head at %d:%d", lnum, offs);
826 if (offs == 0)
827 return ubifs_leb_unmap(c, lnum);
828 err = ubi_read(c->ubi, lnum, sbuf, 0, offs);
829 if (err)
830 return err;
831 return ubi_leb_change(c->ubi, lnum, sbuf, offs, UBI_UNKNOWN);
832 }
833
834 return 0;
835}
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854int ubifs_recover_inl_heads(const struct ubifs_info *c, void *sbuf)
855{
856 int err;
857
858 ubifs_assert(!(c->vfs_sb->s_flags & MS_RDONLY) || c->remounting_rw);
859
860 dbg_rcvry("checking index head at %d:%d", c->ihead_lnum, c->ihead_offs);
861 err = recover_head(c, c->ihead_lnum, c->ihead_offs, sbuf);
862 if (err)
863 return err;
864
865 dbg_rcvry("checking LPT head at %d:%d", c->nhead_lnum, c->nhead_offs);
866 err = recover_head(c, c->nhead_lnum, c->nhead_offs, sbuf);
867 if (err)
868 return err;
869
870 return 0;
871}
872
873
874
875
876
877
878
879
880
881
882
883
884
885static int clean_an_unclean_leb(const struct ubifs_info *c,
886 struct ubifs_unclean_leb *ucleb, void *sbuf)
887{
888 int err, lnum = ucleb->lnum, offs = 0, len = ucleb->endpt, quiet = 1;
889 void *buf = sbuf;
890
891 dbg_rcvry("LEB %d len %d", lnum, len);
892
893 if (len == 0) {
894
895 err = ubifs_leb_unmap(c, lnum);
896 if (err)
897 return err;
898 return 0;
899 }
900
901 err = ubi_read(c->ubi, lnum, buf, offs, len);
902 if (err && err != -EBADMSG)
903 return err;
904
905 while (len >= 8) {
906 int ret;
907
908 cond_resched();
909
910
911 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
912
913 if (ret == SCANNED_A_NODE) {
914
915 struct ubifs_ch *ch = buf;
916 int node_len;
917
918 node_len = ALIGN(le32_to_cpu(ch->len), 8);
919 offs += node_len;
920 buf += node_len;
921 len -= node_len;
922 continue;
923 }
924
925 if (ret > 0) {
926
927 offs += ret;
928 buf += ret;
929 len -= ret;
930 continue;
931 }
932
933 if (ret == SCANNED_EMPTY_SPACE) {
934 ubifs_err("unexpected empty space at %d:%d",
935 lnum, offs);
936 return -EUCLEAN;
937 }
938
939 if (quiet) {
940
941 quiet = 0;
942 continue;
943 }
944
945 ubifs_scanned_corruption(c, lnum, offs, buf);
946 return -EUCLEAN;
947 }
948
949
950 len = ALIGN(ucleb->endpt, c->min_io_size);
951 if (len > ucleb->endpt) {
952 int pad_len = len - ALIGN(ucleb->endpt, 8);
953
954 if (pad_len > 0) {
955 buf = c->sbuf + len - pad_len;
956 ubifs_pad(c, buf, pad_len);
957 }
958 }
959
960
961 err = ubi_leb_change(c->ubi, lnum, sbuf, len, UBI_UNKNOWN);
962 if (err)
963 return err;
964
965 dbg_rcvry("cleaned LEB %d", lnum);
966
967 return 0;
968}
969
970
971
972
973
974
975
976
977
978
979
980
981int ubifs_clean_lebs(const struct ubifs_info *c, void *sbuf)
982{
983 dbg_rcvry("recovery");
984 while (!list_empty(&c->unclean_leb_list)) {
985 struct ubifs_unclean_leb *ucleb;
986 int err;
987
988 ucleb = list_entry(c->unclean_leb_list.next,
989 struct ubifs_unclean_leb, list);
990 err = clean_an_unclean_leb(c, ucleb, sbuf);
991 if (err)
992 return err;
993 list_del(&ucleb->list);
994 kfree(ucleb);
995 }
996 return 0;
997}
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008struct size_entry {
1009 struct rb_node rb;
1010 ino_t inum;
1011 loff_t i_size;
1012 loff_t d_size;
1013 int exists;
1014 struct inode *inode;
1015};
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025static int add_ino(struct ubifs_info *c, ino_t inum, loff_t i_size,
1026 loff_t d_size, int exists)
1027{
1028 struct rb_node **p = &c->size_tree.rb_node, *parent = NULL;
1029 struct size_entry *e;
1030
1031 while (*p) {
1032 parent = *p;
1033 e = rb_entry(parent, struct size_entry, rb);
1034 if (inum < e->inum)
1035 p = &(*p)->rb_left;
1036 else
1037 p = &(*p)->rb_right;
1038 }
1039
1040 e = kzalloc(sizeof(struct size_entry), GFP_KERNEL);
1041 if (!e)
1042 return -ENOMEM;
1043
1044 e->inum = inum;
1045 e->i_size = i_size;
1046 e->d_size = d_size;
1047 e->exists = exists;
1048
1049 rb_link_node(&e->rb, parent, p);
1050 rb_insert_color(&e->rb, &c->size_tree);
1051
1052 return 0;
1053}
1054
1055
1056
1057
1058
1059
1060static struct size_entry *find_ino(struct ubifs_info *c, ino_t inum)
1061{
1062 struct rb_node *p = c->size_tree.rb_node;
1063 struct size_entry *e;
1064
1065 while (p) {
1066 e = rb_entry(p, struct size_entry, rb);
1067 if (inum < e->inum)
1068 p = p->rb_left;
1069 else if (inum > e->inum)
1070 p = p->rb_right;
1071 else
1072 return e;
1073 }
1074 return NULL;
1075}
1076
1077
1078
1079
1080
1081
1082static void remove_ino(struct ubifs_info *c, ino_t inum)
1083{
1084 struct size_entry *e = find_ino(c, inum);
1085
1086 if (!e)
1087 return;
1088 rb_erase(&e->rb, &c->size_tree);
1089 kfree(e);
1090}
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117int ubifs_recover_size_accum(struct ubifs_info *c, union ubifs_key *key,
1118 int deletion, loff_t new_size)
1119{
1120 ino_t inum = key_inum(c, key);
1121 struct size_entry *e;
1122 int err;
1123
1124 switch (key_type(c, key)) {
1125 case UBIFS_INO_KEY:
1126 if (deletion)
1127 remove_ino(c, inum);
1128 else {
1129 e = find_ino(c, inum);
1130 if (e) {
1131 e->i_size = new_size;
1132 e->exists = 1;
1133 } else {
1134 err = add_ino(c, inum, new_size, 0, 1);
1135 if (err)
1136 return err;
1137 }
1138 }
1139 break;
1140 case UBIFS_DATA_KEY:
1141 e = find_ino(c, inum);
1142 if (e) {
1143 if (new_size > e->d_size)
1144 e->d_size = new_size;
1145 } else {
1146 err = add_ino(c, inum, 0, new_size, 0);
1147 if (err)
1148 return err;
1149 }
1150 break;
1151 case UBIFS_TRUN_KEY:
1152 e = find_ino(c, inum);
1153 if (e)
1154 e->d_size = new_size;
1155 break;
1156 }
1157 return 0;
1158}
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169int ubifs_recover_size(struct ubifs_info *c)
1170{
1171 struct rb_node *this = rb_first(&c->size_tree);
1172
1173 while (this) {
1174 struct size_entry *e;
1175 int err;
1176
1177 e = rb_entry(this, struct size_entry, rb);
1178 if (!e->exists) {
1179 union ubifs_key key;
1180
1181 ino_key_init(c, &key, e->inum);
1182 err = ubifs_tnc_lookup(c, &key, c->sbuf);
1183 if (err && err != -ENOENT)
1184 return err;
1185 if (err == -ENOENT) {
1186
1187 dbg_rcvry("removing ino %lu",
1188 (unsigned long)e->inum);
1189 err = ubifs_tnc_remove_ino(c, e->inum);
1190 if (err)
1191 return err;
1192 } else {
1193 struct ubifs_ino_node *ino = c->sbuf;
1194
1195 e->exists = 1;
1196 e->i_size = le64_to_cpu(ino->size);
1197 }
1198 }
1199 if (e->exists && e->i_size < e->d_size) {
1200 if (!e->inode && (c->vfs_sb->s_flags & MS_RDONLY)) {
1201
1202 struct inode *inode;
1203
1204 inode = ubifs_iget(c->vfs_sb, e->inum);
1205 if (IS_ERR(inode))
1206 return PTR_ERR(inode);
1207 if (inode->i_size < e->d_size) {
1208 dbg_rcvry("ino %lu size %lld -> %lld",
1209 (unsigned long)e->inum,
1210 e->d_size, inode->i_size);
1211 inode->i_size = e->d_size;
1212 ubifs_inode(inode)->ui_size = e->d_size;
1213 e->inode = inode;
1214 this = rb_next(this);
1215 continue;
1216 }
1217 iput(inode);
1218 }
1219 }
1220 this = rb_next(this);
1221 rb_erase(&e->rb, &c->size_tree);
1222 kfree(e);
1223 }
1224 return 0;
1225}
1226