0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038 #include <linux/crc32.h>
0039 #include <linux/slab.h>
0040 #include "ubifs.h"
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050 static int is_empty(void *buf, int len)
0051 {
0052 uint8_t *p = buf;
0053 int i;
0054
0055 for (i = 0; i < len; i++)
0056 if (*p++ != 0xff)
0057 return 0;
0058 return 1;
0059 }
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069 static int first_non_ff(void *buf, int len)
0070 {
0071 uint8_t *p = buf;
0072 int i;
0073
0074 for (i = 0; i < len; i++)
0075 if (*p++ != 0xff)
0076 return i;
0077 return -1;
0078 }
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097 static int get_master_node(const struct ubifs_info *c, int lnum, void **pbuf,
0098 struct ubifs_mst_node **mst, void **cor)
0099 {
0100 const int sz = c->mst_node_alsz;
0101 int err, offs, len;
0102 void *sbuf, *buf;
0103
0104 sbuf = vmalloc(c->leb_size);
0105 if (!sbuf)
0106 return -ENOMEM;
0107
0108 err = ubifs_leb_read(c, lnum, sbuf, 0, c->leb_size, 0);
0109 if (err && err != -EBADMSG)
0110 goto out_free;
0111
0112
0113 offs = 0;
0114 buf = sbuf;
0115 len = c->leb_size;
0116 while (offs + UBIFS_MST_NODE_SZ <= c->leb_size) {
0117 struct ubifs_ch *ch = buf;
0118
0119 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC)
0120 break;
0121 offs += sz;
0122 buf += sz;
0123 len -= sz;
0124 }
0125
0126 if (offs) {
0127 int ret;
0128
0129 offs -= sz;
0130 buf -= sz;
0131 len += sz;
0132 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
0133 if (ret != SCANNED_A_NODE && offs) {
0134
0135 offs -= sz;
0136 buf -= sz;
0137 len += sz;
0138 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
0139 if (ret != SCANNED_A_NODE)
0140
0141
0142
0143
0144
0145 goto out_err;
0146 }
0147 if (ret == SCANNED_A_NODE) {
0148 struct ubifs_ch *ch = buf;
0149
0150 if (ch->node_type != UBIFS_MST_NODE)
0151 goto out_err;
0152 dbg_rcvry("found a master node at %d:%d", lnum, offs);
0153 *mst = buf;
0154 offs += sz;
0155 buf += sz;
0156 len -= sz;
0157 }
0158 }
0159
0160 if (offs < c->leb_size) {
0161 if (!is_empty(buf, min_t(int, len, sz))) {
0162 *cor = buf;
0163 dbg_rcvry("found corruption at %d:%d", lnum, offs);
0164 }
0165 offs += sz;
0166 buf += sz;
0167 len -= sz;
0168 }
0169
0170 if (offs < c->leb_size)
0171 if (!is_empty(buf, len))
0172 goto out_err;
0173 *pbuf = sbuf;
0174 return 0;
0175
0176 out_err:
0177 err = -EINVAL;
0178 out_free:
0179 vfree(sbuf);
0180 *mst = NULL;
0181 *cor = NULL;
0182 return err;
0183 }
0184
0185
0186
0187
0188
0189
0190
0191
0192 static int write_rcvrd_mst_node(struct ubifs_info *c,
0193 struct ubifs_mst_node *mst)
0194 {
0195 int err = 0, lnum = UBIFS_MST_LNUM, sz = c->mst_node_alsz;
0196 __le32 save_flags;
0197
0198 dbg_rcvry("recovery");
0199
0200 save_flags = mst->flags;
0201 mst->flags |= cpu_to_le32(UBIFS_MST_RCVRY);
0202
0203 err = ubifs_prepare_node_hmac(c, mst, UBIFS_MST_NODE_SZ,
0204 offsetof(struct ubifs_mst_node, hmac), 1);
0205 if (err)
0206 goto out;
0207 err = ubifs_leb_change(c, lnum, mst, sz);
0208 if (err)
0209 goto out;
0210 err = ubifs_leb_change(c, lnum + 1, mst, sz);
0211 if (err)
0212 goto out;
0213 out:
0214 mst->flags = save_flags;
0215 return err;
0216 }
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227 int ubifs_recover_master_node(struct ubifs_info *c)
0228 {
0229 void *buf1 = NULL, *buf2 = NULL, *cor1 = NULL, *cor2 = NULL;
0230 struct ubifs_mst_node *mst1 = NULL, *mst2 = NULL, *mst;
0231 const int sz = c->mst_node_alsz;
0232 int err, offs1, offs2;
0233
0234 dbg_rcvry("recovery");
0235
0236 err = get_master_node(c, UBIFS_MST_LNUM, &buf1, &mst1, &cor1);
0237 if (err)
0238 goto out_free;
0239
0240 err = get_master_node(c, UBIFS_MST_LNUM + 1, &buf2, &mst2, &cor2);
0241 if (err)
0242 goto out_free;
0243
0244 if (mst1) {
0245 offs1 = (void *)mst1 - buf1;
0246 if ((le32_to_cpu(mst1->flags) & UBIFS_MST_RCVRY) &&
0247 (offs1 == 0 && !cor1)) {
0248
0249
0250
0251
0252 dbg_rcvry("recovery recovery");
0253 mst = mst1;
0254 } else if (mst2) {
0255 offs2 = (void *)mst2 - buf2;
0256 if (offs1 == offs2) {
0257
0258 if (ubifs_compare_master_node(c, mst1, mst2))
0259 goto out_err;
0260 mst = mst1;
0261 } else if (offs2 + sz == offs1) {
0262
0263 if (cor1)
0264 goto out_err;
0265 mst = mst1;
0266 } else if (offs1 == 0 &&
0267 c->leb_size - offs2 - sz < sz) {
0268
0269 if (cor1)
0270 goto out_err;
0271 mst = mst1;
0272 } else
0273 goto out_err;
0274 } else {
0275
0276
0277
0278
0279
0280 if (offs1 != 0 || cor1)
0281 goto out_err;
0282 mst = mst1;
0283 }
0284 } else {
0285 if (!mst2)
0286 goto out_err;
0287
0288
0289
0290
0291 offs2 = (void *)mst2 - buf2;
0292 if (offs2 + sz + sz <= c->leb_size)
0293 goto out_err;
0294 mst = mst2;
0295 }
0296
0297 ubifs_msg(c, "recovered master node from LEB %d",
0298 (mst == mst1 ? UBIFS_MST_LNUM : UBIFS_MST_LNUM + 1));
0299
0300 memcpy(c->mst_node, mst, UBIFS_MST_NODE_SZ);
0301
0302 if (c->ro_mount) {
0303
0304 c->rcvrd_mst_node = kmalloc(sz, GFP_KERNEL);
0305 if (!c->rcvrd_mst_node) {
0306 err = -ENOMEM;
0307 goto out_free;
0308 }
0309 memcpy(c->rcvrd_mst_node, c->mst_node, UBIFS_MST_NODE_SZ);
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
0336 } else {
0337
0338 c->max_sqnum = le64_to_cpu(mst->ch.sqnum) - 1;
0339 err = write_rcvrd_mst_node(c, c->mst_node);
0340 if (err)
0341 goto out_free;
0342 }
0343
0344 vfree(buf2);
0345 vfree(buf1);
0346
0347 return 0;
0348
0349 out_err:
0350 err = -EINVAL;
0351 out_free:
0352 ubifs_err(c, "failed to recover master node");
0353 if (mst1) {
0354 ubifs_err(c, "dumping first master node");
0355 ubifs_dump_node(c, mst1, c->leb_size - ((void *)mst1 - buf1));
0356 }
0357 if (mst2) {
0358 ubifs_err(c, "dumping second master node");
0359 ubifs_dump_node(c, mst2, c->leb_size - ((void *)mst2 - buf2));
0360 }
0361 vfree(buf2);
0362 vfree(buf1);
0363 return err;
0364 }
0365
0366
0367
0368
0369
0370
0371
0372
0373
0374
0375 int ubifs_write_rcvrd_mst_node(struct ubifs_info *c)
0376 {
0377 int err;
0378
0379 if (!c->rcvrd_mst_node)
0380 return 0;
0381 c->rcvrd_mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
0382 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
0383 err = write_rcvrd_mst_node(c, c->rcvrd_mst_node);
0384 if (err)
0385 return err;
0386 kfree(c->rcvrd_mst_node);
0387 c->rcvrd_mst_node = NULL;
0388 return 0;
0389 }
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402 static int is_last_write(const struct ubifs_info *c, void *buf, int offs)
0403 {
0404 int empty_offs, check_len;
0405 uint8_t *p;
0406
0407
0408
0409
0410
0411 empty_offs = ALIGN(offs + 1, c->max_write_size);
0412 check_len = c->leb_size - empty_offs;
0413 p = buf + empty_offs - offs;
0414 return is_empty(p, check_len);
0415 }
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429 static void clean_buf(const struct ubifs_info *c, void **buf, int lnum,
0430 int *offs, int *len)
0431 {
0432 int empty_offs, pad_len;
0433
0434 dbg_rcvry("cleaning corruption at %d:%d", lnum, *offs);
0435
0436 ubifs_assert(c, !(*offs & 7));
0437 empty_offs = ALIGN(*offs, c->min_io_size);
0438 pad_len = empty_offs - *offs;
0439 ubifs_pad(c, *buf, pad_len);
0440 *offs += pad_len;
0441 *buf += pad_len;
0442 *len -= pad_len;
0443 memset(*buf, 0xff, c->leb_size - empty_offs);
0444 }
0445
0446
0447
0448
0449
0450
0451
0452
0453
0454
0455
0456
0457
0458 static int no_more_nodes(const struct ubifs_info *c, void *buf, int len,
0459 int lnum, int offs)
0460 {
0461 struct ubifs_ch *ch = buf;
0462 int skip, dlen = le32_to_cpu(ch->len);
0463
0464
0465 skip = ALIGN(offs + UBIFS_CH_SZ, c->max_write_size) - offs;
0466 if (is_empty(buf + skip, len - skip))
0467 return 1;
0468
0469
0470
0471
0472 if (ubifs_check_node(c, buf, len, lnum, offs, 1, 0) != -EUCLEAN) {
0473 dbg_rcvry("unexpected bad common header at %d:%d", lnum, offs);
0474 return 0;
0475 }
0476
0477 skip = ALIGN(offs + dlen, c->max_write_size) - offs;
0478
0479 if (is_empty(buf + skip, len - skip))
0480 return 1;
0481 dbg_rcvry("unexpected data at %d:%d", lnum, offs + skip);
0482 return 0;
0483 }
0484
0485
0486
0487
0488
0489
0490
0491 static int fix_unclean_leb(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
0492 int start)
0493 {
0494 int lnum = sleb->lnum, endpt = start;
0495
0496
0497 if (!list_empty(&sleb->nodes)) {
0498 struct ubifs_scan_node *snod;
0499
0500 snod = list_entry(sleb->nodes.prev,
0501 struct ubifs_scan_node, list);
0502 endpt = snod->offs + snod->len;
0503 }
0504
0505 if (c->ro_mount && !c->remounting_rw) {
0506
0507 struct ubifs_unclean_leb *ucleb;
0508
0509 dbg_rcvry("need to fix LEB %d start %d endpt %d",
0510 lnum, start, sleb->endpt);
0511 ucleb = kzalloc(sizeof(struct ubifs_unclean_leb), GFP_NOFS);
0512 if (!ucleb)
0513 return -ENOMEM;
0514 ucleb->lnum = lnum;
0515 ucleb->endpt = endpt;
0516 list_add_tail(&ucleb->list, &c->unclean_leb_list);
0517 } else {
0518
0519 int err;
0520
0521 dbg_rcvry("fixing LEB %d start %d endpt %d",
0522 lnum, start, sleb->endpt);
0523 if (endpt == 0) {
0524 err = ubifs_leb_unmap(c, lnum);
0525 if (err)
0526 return err;
0527 } else {
0528 int len = ALIGN(endpt, c->min_io_size);
0529
0530 if (start) {
0531 err = ubifs_leb_read(c, lnum, sleb->buf, 0,
0532 start, 1);
0533 if (err)
0534 return err;
0535 }
0536
0537 if (len > endpt) {
0538 int pad_len = len - ALIGN(endpt, 8);
0539
0540 if (pad_len > 0) {
0541 void *buf = sleb->buf + len - pad_len;
0542
0543 ubifs_pad(c, buf, pad_len);
0544 }
0545 }
0546 err = ubifs_leb_change(c, lnum, sleb->buf, len);
0547 if (err)
0548 return err;
0549 }
0550 }
0551 return 0;
0552 }
0553
0554
0555
0556
0557
0558
0559
0560
0561
0562 static void drop_last_group(struct ubifs_scan_leb *sleb, int *offs)
0563 {
0564 while (!list_empty(&sleb->nodes)) {
0565 struct ubifs_scan_node *snod;
0566 struct ubifs_ch *ch;
0567
0568 snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
0569 list);
0570 ch = snod->node;
0571 if (ch->group_type != UBIFS_IN_NODE_GROUP)
0572 break;
0573
0574 dbg_rcvry("dropping grouped node at %d:%d",
0575 sleb->lnum, snod->offs);
0576 *offs = snod->offs;
0577 list_del(&snod->list);
0578 kfree(snod);
0579 sleb->nodes_cnt -= 1;
0580 }
0581 }
0582
0583
0584
0585
0586
0587
0588
0589
0590
0591 static void drop_last_node(struct ubifs_scan_leb *sleb, int *offs)
0592 {
0593 struct ubifs_scan_node *snod;
0594
0595 if (!list_empty(&sleb->nodes)) {
0596 snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
0597 list);
0598
0599 dbg_rcvry("dropping last node at %d:%d",
0600 sleb->lnum, snod->offs);
0601 *offs = snod->offs;
0602 list_del(&snod->list);
0603 kfree(snod);
0604 sleb->nodes_cnt -= 1;
0605 }
0606 }
0607
0608
0609
0610
0611
0612
0613
0614
0615
0616
0617
0618
0619
0620
0621
0622 struct ubifs_scan_leb *ubifs_recover_leb(struct ubifs_info *c, int lnum,
0623 int offs, void *sbuf, int jhead)
0624 {
0625 int ret = 0, err, len = c->leb_size - offs, start = offs, min_io_unit;
0626 int grouped = jhead == -1 ? 0 : c->jheads[jhead].grouped;
0627 struct ubifs_scan_leb *sleb;
0628 void *buf = sbuf + offs;
0629
0630 dbg_rcvry("%d:%d, jhead %d, grouped %d", lnum, offs, jhead, grouped);
0631
0632 sleb = ubifs_start_scan(c, lnum, offs, sbuf);
0633 if (IS_ERR(sleb))
0634 return sleb;
0635
0636 ubifs_assert(c, len >= 8);
0637 while (len >= 8) {
0638 dbg_scan("look at LEB %d:%d (%d bytes left)",
0639 lnum, offs, len);
0640
0641 cond_resched();
0642
0643
0644
0645
0646
0647 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
0648 if (ret == SCANNED_A_NODE) {
0649
0650 struct ubifs_ch *ch = buf;
0651 int node_len;
0652
0653 err = ubifs_add_snod(c, sleb, buf, offs);
0654 if (err)
0655 goto error;
0656 node_len = ALIGN(le32_to_cpu(ch->len), 8);
0657 offs += node_len;
0658 buf += node_len;
0659 len -= node_len;
0660 } else if (ret > 0) {
0661
0662 offs += ret;
0663 buf += ret;
0664 len -= ret;
0665 } else if (ret == SCANNED_EMPTY_SPACE ||
0666 ret == SCANNED_GARBAGE ||
0667 ret == SCANNED_A_BAD_PAD_NODE ||
0668 ret == SCANNED_A_CORRUPT_NODE) {
0669 dbg_rcvry("found corruption (%d) at %d:%d",
0670 ret, lnum, offs);
0671 break;
0672 } else {
0673 ubifs_err(c, "unexpected return value %d", ret);
0674 err = -EINVAL;
0675 goto error;
0676 }
0677 }
0678
0679 if (ret == SCANNED_GARBAGE || ret == SCANNED_A_BAD_PAD_NODE) {
0680 if (!is_last_write(c, buf, offs))
0681 goto corrupted_rescan;
0682 } else if (ret == SCANNED_A_CORRUPT_NODE) {
0683 if (!no_more_nodes(c, buf, len, lnum, offs))
0684 goto corrupted_rescan;
0685 } else if (!is_empty(buf, len)) {
0686 if (!is_last_write(c, buf, offs)) {
0687 int corruption = first_non_ff(buf, len);
0688
0689
0690
0691
0692
0693 ubifs_err(c, "corrupt empty space LEB %d:%d, corruption starts at %d",
0694 lnum, offs, corruption);
0695
0696 offs += corruption;
0697 buf += corruption;
0698 goto corrupted;
0699 }
0700 }
0701
0702 min_io_unit = round_down(offs, c->min_io_size);
0703 if (grouped)
0704
0705
0706
0707
0708 drop_last_group(sleb, &offs);
0709
0710 if (jhead == GCHD) {
0711
0712
0713
0714
0715
0716
0717
0718
0719
0720
0721
0722
0723
0724
0725
0726
0727
0728
0729
0730
0731
0732
0733
0734
0735
0736
0737
0738
0739
0740
0741
0742
0743
0744
0745
0746
0747
0748
0749
0750
0751
0752
0753
0754
0755
0756
0757
0758
0759
0760
0761 while (offs > min_io_unit)
0762 drop_last_node(sleb, &offs);
0763 }
0764
0765 buf = sbuf + offs;
0766 len = c->leb_size - offs;
0767
0768 clean_buf(c, &buf, lnum, &offs, &len);
0769 ubifs_end_scan(c, sleb, lnum, offs);
0770
0771 err = fix_unclean_leb(c, sleb, start);
0772 if (err)
0773 goto error;
0774
0775 return sleb;
0776
0777 corrupted_rescan:
0778
0779 ubifs_err(c, "corruption %d", ret);
0780 ubifs_scan_a_node(c, buf, len, lnum, offs, 0);
0781 corrupted:
0782 ubifs_scanned_corruption(c, lnum, offs, buf);
0783 err = -EUCLEAN;
0784 error:
0785 ubifs_err(c, "LEB %d scanning failed", lnum);
0786 ubifs_scan_destroy(sleb);
0787 return ERR_PTR(err);
0788 }
0789
0790
0791
0792
0793
0794
0795
0796
0797
0798
0799 static int get_cs_sqnum(struct ubifs_info *c, int lnum, int offs,
0800 unsigned long long *cs_sqnum)
0801 {
0802 struct ubifs_cs_node *cs_node = NULL;
0803 int err, ret;
0804
0805 dbg_rcvry("at %d:%d", lnum, offs);
0806 cs_node = kmalloc(UBIFS_CS_NODE_SZ, GFP_KERNEL);
0807 if (!cs_node)
0808 return -ENOMEM;
0809 if (c->leb_size - offs < UBIFS_CS_NODE_SZ)
0810 goto out_err;
0811 err = ubifs_leb_read(c, lnum, (void *)cs_node, offs,
0812 UBIFS_CS_NODE_SZ, 0);
0813 if (err && err != -EBADMSG)
0814 goto out_free;
0815 ret = ubifs_scan_a_node(c, cs_node, UBIFS_CS_NODE_SZ, lnum, offs, 0);
0816 if (ret != SCANNED_A_NODE) {
0817 ubifs_err(c, "Not a valid node");
0818 goto out_err;
0819 }
0820 if (cs_node->ch.node_type != UBIFS_CS_NODE) {
0821 ubifs_err(c, "Not a CS node, type is %d", cs_node->ch.node_type);
0822 goto out_err;
0823 }
0824 if (le64_to_cpu(cs_node->cmt_no) != c->cmt_no) {
0825 ubifs_err(c, "CS node cmt_no %llu != current cmt_no %llu",
0826 (unsigned long long)le64_to_cpu(cs_node->cmt_no),
0827 c->cmt_no);
0828 goto out_err;
0829 }
0830 *cs_sqnum = le64_to_cpu(cs_node->ch.sqnum);
0831 dbg_rcvry("commit start sqnum %llu", *cs_sqnum);
0832 kfree(cs_node);
0833 return 0;
0834
0835 out_err:
0836 err = -EINVAL;
0837 out_free:
0838 ubifs_err(c, "failed to get CS sqnum");
0839 kfree(cs_node);
0840 return err;
0841 }
0842
0843
0844
0845
0846
0847
0848
0849
0850
0851
0852
0853
0854
0855
0856 struct ubifs_scan_leb *ubifs_recover_log_leb(struct ubifs_info *c, int lnum,
0857 int offs, void *sbuf)
0858 {
0859 struct ubifs_scan_leb *sleb;
0860 int next_lnum;
0861
0862 dbg_rcvry("LEB %d", lnum);
0863 next_lnum = lnum + 1;
0864 if (next_lnum >= UBIFS_LOG_LNUM + c->log_lebs)
0865 next_lnum = UBIFS_LOG_LNUM;
0866 if (next_lnum != c->ltail_lnum) {
0867
0868
0869
0870
0871 sleb = ubifs_scan(c, next_lnum, 0, sbuf, 0);
0872 if (IS_ERR(sleb))
0873 return sleb;
0874 if (sleb->nodes_cnt) {
0875 struct ubifs_scan_node *snod;
0876 unsigned long long cs_sqnum = c->cs_sqnum;
0877
0878 snod = list_entry(sleb->nodes.next,
0879 struct ubifs_scan_node, list);
0880 if (cs_sqnum == 0) {
0881 int err;
0882
0883 err = get_cs_sqnum(c, lnum, offs, &cs_sqnum);
0884 if (err) {
0885 ubifs_scan_destroy(sleb);
0886 return ERR_PTR(err);
0887 }
0888 }
0889 if (snod->sqnum > cs_sqnum) {
0890 ubifs_err(c, "unrecoverable log corruption in LEB %d",
0891 lnum);
0892 ubifs_scan_destroy(sleb);
0893 return ERR_PTR(-EUCLEAN);
0894 }
0895 }
0896 ubifs_scan_destroy(sleb);
0897 }
0898 return ubifs_recover_leb(c, lnum, offs, sbuf, -1);
0899 }
0900
0901
0902
0903
0904
0905
0906
0907
0908
0909
0910
0911
0912 static int recover_head(struct ubifs_info *c, int lnum, int offs, void *sbuf)
0913 {
0914 int len = c->max_write_size, err;
0915
0916 if (offs + len > c->leb_size)
0917 len = c->leb_size - offs;
0918
0919 if (!len)
0920 return 0;
0921
0922
0923 err = ubifs_leb_read(c, lnum, sbuf, offs, len, 1);
0924 if (err || !is_empty(sbuf, len)) {
0925 dbg_rcvry("cleaning head at %d:%d", lnum, offs);
0926 if (offs == 0)
0927 return ubifs_leb_unmap(c, lnum);
0928 err = ubifs_leb_read(c, lnum, sbuf, 0, offs, 1);
0929 if (err)
0930 return err;
0931 return ubifs_leb_change(c, lnum, sbuf, offs);
0932 }
0933
0934 return 0;
0935 }
0936
0937
0938
0939
0940
0941
0942
0943
0944
0945
0946
0947
0948
0949
0950
0951
0952
0953
0954 int ubifs_recover_inl_heads(struct ubifs_info *c, void *sbuf)
0955 {
0956 int err;
0957
0958 ubifs_assert(c, !c->ro_mount || c->remounting_rw);
0959
0960 dbg_rcvry("checking index head at %d:%d", c->ihead_lnum, c->ihead_offs);
0961 err = recover_head(c, c->ihead_lnum, c->ihead_offs, sbuf);
0962 if (err)
0963 return err;
0964
0965 dbg_rcvry("checking LPT head at %d:%d", c->nhead_lnum, c->nhead_offs);
0966
0967 return recover_head(c, c->nhead_lnum, c->nhead_offs, sbuf);
0968 }
0969
0970
0971
0972
0973
0974
0975
0976
0977
0978
0979
0980
0981
0982 static int clean_an_unclean_leb(struct ubifs_info *c,
0983 struct ubifs_unclean_leb *ucleb, void *sbuf)
0984 {
0985 int err, lnum = ucleb->lnum, offs = 0, len = ucleb->endpt, quiet = 1;
0986 void *buf = sbuf;
0987
0988 dbg_rcvry("LEB %d len %d", lnum, len);
0989
0990 if (len == 0) {
0991
0992 return ubifs_leb_unmap(c, lnum);
0993 }
0994
0995 err = ubifs_leb_read(c, lnum, buf, offs, len, 0);
0996 if (err && err != -EBADMSG)
0997 return err;
0998
0999 while (len >= 8) {
1000 int ret;
1001
1002 cond_resched();
1003
1004
1005 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
1006
1007 if (ret == SCANNED_A_NODE) {
1008
1009 struct ubifs_ch *ch = buf;
1010 int node_len;
1011
1012 node_len = ALIGN(le32_to_cpu(ch->len), 8);
1013 offs += node_len;
1014 buf += node_len;
1015 len -= node_len;
1016 continue;
1017 }
1018
1019 if (ret > 0) {
1020
1021 offs += ret;
1022 buf += ret;
1023 len -= ret;
1024 continue;
1025 }
1026
1027 if (ret == SCANNED_EMPTY_SPACE) {
1028 ubifs_err(c, "unexpected empty space at %d:%d",
1029 lnum, offs);
1030 return -EUCLEAN;
1031 }
1032
1033 if (quiet) {
1034
1035 quiet = 0;
1036 continue;
1037 }
1038
1039 ubifs_scanned_corruption(c, lnum, offs, buf);
1040 return -EUCLEAN;
1041 }
1042
1043
1044 len = ALIGN(ucleb->endpt, c->min_io_size);
1045 if (len > ucleb->endpt) {
1046 int pad_len = len - ALIGN(ucleb->endpt, 8);
1047
1048 if (pad_len > 0) {
1049 buf = c->sbuf + len - pad_len;
1050 ubifs_pad(c, buf, pad_len);
1051 }
1052 }
1053
1054
1055 err = ubifs_leb_change(c, lnum, sbuf, len);
1056 if (err)
1057 return err;
1058
1059 dbg_rcvry("cleaned LEB %d", lnum);
1060
1061 return 0;
1062 }
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075 int ubifs_clean_lebs(struct ubifs_info *c, void *sbuf)
1076 {
1077 dbg_rcvry("recovery");
1078 while (!list_empty(&c->unclean_leb_list)) {
1079 struct ubifs_unclean_leb *ucleb;
1080 int err;
1081
1082 ucleb = list_entry(c->unclean_leb_list.next,
1083 struct ubifs_unclean_leb, list);
1084 err = clean_an_unclean_leb(c, ucleb, sbuf);
1085 if (err)
1086 return err;
1087 list_del(&ucleb->list);
1088 kfree(ucleb);
1089 }
1090 return 0;
1091 }
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101 static int grab_empty_leb(struct ubifs_info *c)
1102 {
1103 int lnum, err;
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120 lnum = ubifs_find_free_leb_for_idx(c);
1121 if (lnum < 0) {
1122 ubifs_err(c, "could not find an empty LEB");
1123 ubifs_dump_lprops(c);
1124 ubifs_dump_budg(c, &c->bi);
1125 return lnum;
1126 }
1127
1128
1129 err = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
1130 LPROPS_INDEX, 0);
1131 if (err)
1132 return err;
1133
1134 c->gc_lnum = lnum;
1135 dbg_rcvry("found empty LEB %d, run commit", lnum);
1136
1137 return ubifs_run_commit(c);
1138 }
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158 int ubifs_rcvry_gc_commit(struct ubifs_info *c)
1159 {
1160 struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
1161 struct ubifs_lprops lp;
1162 int err;
1163
1164 dbg_rcvry("GC head LEB %d, offs %d", wbuf->lnum, wbuf->offs);
1165
1166 c->gc_lnum = -1;
1167 if (wbuf->lnum == -1 || wbuf->offs == c->leb_size)
1168 return grab_empty_leb(c);
1169
1170 err = ubifs_find_dirty_leb(c, &lp, wbuf->offs, 2);
1171 if (err) {
1172 if (err != -ENOSPC)
1173 return err;
1174
1175 dbg_rcvry("could not find a dirty LEB");
1176 return grab_empty_leb(c);
1177 }
1178
1179 ubifs_assert(c, !(lp.flags & LPROPS_INDEX));
1180 ubifs_assert(c, lp.free + lp.dirty >= wbuf->offs);
1181
1182
1183
1184
1185
1186 dbg_rcvry("committing");
1187 err = ubifs_run_commit(c);
1188 if (err)
1189 return err;
1190
1191 dbg_rcvry("GC'ing LEB %d", lp.lnum);
1192 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
1193 err = ubifs_garbage_collect_leb(c, &lp);
1194 if (err >= 0) {
1195 int err2 = ubifs_wbuf_sync_nolock(wbuf);
1196
1197 if (err2)
1198 err = err2;
1199 }
1200 mutex_unlock(&wbuf->io_mutex);
1201 if (err < 0) {
1202 ubifs_err(c, "GC failed, error %d", err);
1203 if (err == -EAGAIN)
1204 err = -EINVAL;
1205 return err;
1206 }
1207
1208 ubifs_assert(c, err == LEB_RETAINED);
1209 if (err != LEB_RETAINED)
1210 return -EINVAL;
1211
1212 err = ubifs_leb_unmap(c, c->gc_lnum);
1213 if (err)
1214 return err;
1215
1216 dbg_rcvry("allocated LEB %d for GC", lp.lnum);
1217 return 0;
1218 }
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229 struct size_entry {
1230 struct rb_node rb;
1231 ino_t inum;
1232 loff_t i_size;
1233 loff_t d_size;
1234 int exists;
1235 struct inode *inode;
1236 };
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246 static int add_ino(struct ubifs_info *c, ino_t inum, loff_t i_size,
1247 loff_t d_size, int exists)
1248 {
1249 struct rb_node **p = &c->size_tree.rb_node, *parent = NULL;
1250 struct size_entry *e;
1251
1252 while (*p) {
1253 parent = *p;
1254 e = rb_entry(parent, struct size_entry, rb);
1255 if (inum < e->inum)
1256 p = &(*p)->rb_left;
1257 else
1258 p = &(*p)->rb_right;
1259 }
1260
1261 e = kzalloc(sizeof(struct size_entry), GFP_KERNEL);
1262 if (!e)
1263 return -ENOMEM;
1264
1265 e->inum = inum;
1266 e->i_size = i_size;
1267 e->d_size = d_size;
1268 e->exists = exists;
1269
1270 rb_link_node(&e->rb, parent, p);
1271 rb_insert_color(&e->rb, &c->size_tree);
1272
1273 return 0;
1274 }
1275
1276
1277
1278
1279
1280
1281 static struct size_entry *find_ino(struct ubifs_info *c, ino_t inum)
1282 {
1283 struct rb_node *p = c->size_tree.rb_node;
1284 struct size_entry *e;
1285
1286 while (p) {
1287 e = rb_entry(p, struct size_entry, rb);
1288 if (inum < e->inum)
1289 p = p->rb_left;
1290 else if (inum > e->inum)
1291 p = p->rb_right;
1292 else
1293 return e;
1294 }
1295 return NULL;
1296 }
1297
1298
1299
1300
1301
1302
1303 static void remove_ino(struct ubifs_info *c, ino_t inum)
1304 {
1305 struct size_entry *e = find_ino(c, inum);
1306
1307 if (!e)
1308 return;
1309 rb_erase(&e->rb, &c->size_tree);
1310 kfree(e);
1311 }
1312
1313
1314
1315
1316
1317 void ubifs_destroy_size_tree(struct ubifs_info *c)
1318 {
1319 struct size_entry *e, *n;
1320
1321 rbtree_postorder_for_each_entry_safe(e, n, &c->size_tree, rb) {
1322 iput(e->inode);
1323 kfree(e);
1324 }
1325
1326 c->size_tree = RB_ROOT;
1327 }
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354 int ubifs_recover_size_accum(struct ubifs_info *c, union ubifs_key *key,
1355 int deletion, loff_t new_size)
1356 {
1357 ino_t inum = key_inum(c, key);
1358 struct size_entry *e;
1359 int err;
1360
1361 switch (key_type(c, key)) {
1362 case UBIFS_INO_KEY:
1363 if (deletion)
1364 remove_ino(c, inum);
1365 else {
1366 e = find_ino(c, inum);
1367 if (e) {
1368 e->i_size = new_size;
1369 e->exists = 1;
1370 } else {
1371 err = add_ino(c, inum, new_size, 0, 1);
1372 if (err)
1373 return err;
1374 }
1375 }
1376 break;
1377 case UBIFS_DATA_KEY:
1378 e = find_ino(c, inum);
1379 if (e) {
1380 if (new_size > e->d_size)
1381 e->d_size = new_size;
1382 } else {
1383 err = add_ino(c, inum, 0, new_size, 0);
1384 if (err)
1385 return err;
1386 }
1387 break;
1388 case UBIFS_TRUN_KEY:
1389 e = find_ino(c, inum);
1390 if (e)
1391 e->d_size = new_size;
1392 break;
1393 }
1394 return 0;
1395 }
1396
1397
1398
1399
1400
1401
1402 static int fix_size_in_place(struct ubifs_info *c, struct size_entry *e)
1403 {
1404 struct ubifs_ino_node *ino = c->sbuf;
1405 unsigned char *p;
1406 union ubifs_key key;
1407 int err, lnum, offs, len;
1408 loff_t i_size;
1409 uint32_t crc;
1410
1411
1412 ino_key_init(c, &key, e->inum);
1413 err = ubifs_tnc_locate(c, &key, ino, &lnum, &offs);
1414 if (err)
1415 goto out;
1416
1417
1418
1419
1420 i_size = le64_to_cpu(ino->size);
1421 if (i_size >= e->d_size)
1422 return 0;
1423
1424 err = ubifs_leb_read(c, lnum, c->sbuf, 0, c->leb_size, 1);
1425 if (err)
1426 goto out;
1427
1428 ino = c->sbuf + offs;
1429 ino->size = cpu_to_le64(e->d_size);
1430 len = le32_to_cpu(ino->ch.len);
1431 crc = crc32(UBIFS_CRC32_INIT, (void *)ino + 8, len - 8);
1432 ino->ch.crc = cpu_to_le32(crc);
1433
1434 p = c->sbuf;
1435 len = c->leb_size - 1;
1436 while (p[len] == 0xff)
1437 len -= 1;
1438 len = ALIGN(len + 1, c->min_io_size);
1439
1440 err = ubifs_leb_change(c, lnum, c->sbuf, len);
1441 if (err)
1442 goto out;
1443 dbg_rcvry("inode %lu at %d:%d size %lld -> %lld",
1444 (unsigned long)e->inum, lnum, offs, i_size, e->d_size);
1445 return 0;
1446
1447 out:
1448 ubifs_warn(c, "inode %lu failed to fix size %lld -> %lld error %d",
1449 (unsigned long)e->inum, e->i_size, e->d_size, err);
1450 return err;
1451 }
1452
1453
1454
1455
1456
1457
1458 static int inode_fix_size(struct ubifs_info *c, struct size_entry *e)
1459 {
1460 struct inode *inode;
1461 struct ubifs_inode *ui;
1462 int err;
1463
1464 if (c->ro_mount)
1465 ubifs_assert(c, !e->inode);
1466
1467 if (e->inode) {
1468
1469 inode = e->inode;
1470 } else {
1471 inode = ubifs_iget(c->vfs_sb, e->inum);
1472 if (IS_ERR(inode))
1473 return PTR_ERR(inode);
1474
1475 if (inode->i_size >= e->d_size) {
1476
1477
1478
1479
1480 iput(inode);
1481 return 0;
1482 }
1483
1484 dbg_rcvry("ino %lu size %lld -> %lld",
1485 (unsigned long)e->inum,
1486 inode->i_size, e->d_size);
1487
1488 ui = ubifs_inode(inode);
1489
1490 inode->i_size = e->d_size;
1491 ui->ui_size = e->d_size;
1492 ui->synced_i_size = e->d_size;
1493
1494 e->inode = inode;
1495 }
1496
1497
1498
1499
1500
1501
1502 if (c->ro_mount)
1503 return 0;
1504
1505 err = ubifs_jnl_write_inode(c, inode);
1506
1507 iput(inode);
1508
1509 if (err)
1510 return err;
1511
1512 rb_erase(&e->rb, &c->size_tree);
1513 kfree(e);
1514
1515 return 0;
1516 }
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528 int ubifs_recover_size(struct ubifs_info *c, bool in_place)
1529 {
1530 struct rb_node *this = rb_first(&c->size_tree);
1531
1532 while (this) {
1533 struct size_entry *e;
1534 int err;
1535
1536 e = rb_entry(this, struct size_entry, rb);
1537
1538 this = rb_next(this);
1539
1540 if (!e->exists) {
1541 union ubifs_key key;
1542
1543 ino_key_init(c, &key, e->inum);
1544 err = ubifs_tnc_lookup(c, &key, c->sbuf);
1545 if (err && err != -ENOENT)
1546 return err;
1547 if (err == -ENOENT) {
1548
1549 dbg_rcvry("removing ino %lu",
1550 (unsigned long)e->inum);
1551 err = ubifs_tnc_remove_ino(c, e->inum);
1552 if (err)
1553 return err;
1554 } else {
1555 struct ubifs_ino_node *ino = c->sbuf;
1556
1557 e->exists = 1;
1558 e->i_size = le64_to_cpu(ino->size);
1559 }
1560 }
1561
1562 if (e->exists && e->i_size < e->d_size) {
1563 ubifs_assert(c, !(c->ro_mount && in_place));
1564
1565
1566
1567
1568
1569
1570 if (in_place) {
1571 err = fix_size_in_place(c, e);
1572 if (err)
1573 return err;
1574 iput(e->inode);
1575 } else {
1576 err = inode_fix_size(c, e);
1577 if (err)
1578 return err;
1579 continue;
1580 }
1581 }
1582
1583 rb_erase(&e->rb, &c->size_tree);
1584 kfree(e);
1585 }
1586
1587 return 0;
1588 }