0001
0002
0003
0004
0005
0006 #include "xfs.h"
0007 #include "xfs_fs.h"
0008 #include "xfs_shared.h"
0009 #include "xfs_format.h"
0010 #include "xfs_log_format.h"
0011 #include "xfs_trans_resv.h"
0012 #include "xfs_bit.h"
0013 #include "xfs_sb.h"
0014 #include "xfs_mount.h"
0015 #include "xfs_defer.h"
0016 #include "xfs_inode.h"
0017 #include "xfs_trans.h"
0018 #include "xfs_log.h"
0019 #include "xfs_log_priv.h"
0020 #include "xfs_log_recover.h"
0021 #include "xfs_trans_priv.h"
0022 #include "xfs_alloc.h"
0023 #include "xfs_ialloc.h"
0024 #include "xfs_trace.h"
0025 #include "xfs_icache.h"
0026 #include "xfs_error.h"
0027 #include "xfs_buf_item.h"
0028 #include "xfs_ag.h"
0029 #include "xfs_quota.h"
0030 #include "xfs_reflink.h"
0031
0032 #define BLK_AVG(blk1, blk2) ((blk1+blk2) >> 1)
0033
0034 STATIC int
0035 xlog_find_zeroed(
0036 struct xlog *,
0037 xfs_daddr_t *);
0038 STATIC int
0039 xlog_clear_stale_blocks(
0040 struct xlog *,
0041 xfs_lsn_t);
0042 STATIC int
0043 xlog_do_recovery_pass(
0044 struct xlog *, xfs_daddr_t, xfs_daddr_t, int, xfs_daddr_t *);
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055 static inline bool
0056 xlog_verify_bno(
0057 struct xlog *log,
0058 xfs_daddr_t blk_no,
0059 int bbcount)
0060 {
0061 if (blk_no < 0 || blk_no >= log->l_logBBsize)
0062 return false;
0063 if (bbcount <= 0 || (blk_no + bbcount) > log->l_logBBsize)
0064 return false;
0065 return true;
0066 }
0067
0068
0069
0070
0071
0072 static char *
0073 xlog_alloc_buffer(
0074 struct xlog *log,
0075 int nbblks)
0076 {
0077
0078
0079
0080
0081 if (XFS_IS_CORRUPT(log->l_mp, !xlog_verify_bno(log, 0, nbblks))) {
0082 xfs_warn(log->l_mp, "Invalid block length (0x%x) for buffer",
0083 nbblks);
0084 return NULL;
0085 }
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101 if (nbblks > 1 && log->l_sectBBsize > 1)
0102 nbblks += log->l_sectBBsize;
0103 nbblks = round_up(nbblks, log->l_sectBBsize);
0104 return kvzalloc(BBTOB(nbblks), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
0105 }
0106
0107
0108
0109
0110
0111 static inline unsigned int
0112 xlog_align(
0113 struct xlog *log,
0114 xfs_daddr_t blk_no)
0115 {
0116 return BBTOB(blk_no & ((xfs_daddr_t)log->l_sectBBsize - 1));
0117 }
0118
0119 static int
0120 xlog_do_io(
0121 struct xlog *log,
0122 xfs_daddr_t blk_no,
0123 unsigned int nbblks,
0124 char *data,
0125 enum req_op op)
0126 {
0127 int error;
0128
0129 if (XFS_IS_CORRUPT(log->l_mp, !xlog_verify_bno(log, blk_no, nbblks))) {
0130 xfs_warn(log->l_mp,
0131 "Invalid log block/length (0x%llx, 0x%x) for buffer",
0132 blk_no, nbblks);
0133 return -EFSCORRUPTED;
0134 }
0135
0136 blk_no = round_down(blk_no, log->l_sectBBsize);
0137 nbblks = round_up(nbblks, log->l_sectBBsize);
0138 ASSERT(nbblks > 0);
0139
0140 error = xfs_rw_bdev(log->l_targ->bt_bdev, log->l_logBBstart + blk_no,
0141 BBTOB(nbblks), data, op);
0142 if (error && !xlog_is_shutdown(log)) {
0143 xfs_alert(log->l_mp,
0144 "log recovery %s I/O error at daddr 0x%llx len %d error %d",
0145 op == REQ_OP_WRITE ? "write" : "read",
0146 blk_no, nbblks, error);
0147 }
0148 return error;
0149 }
0150
0151 STATIC int
0152 xlog_bread_noalign(
0153 struct xlog *log,
0154 xfs_daddr_t blk_no,
0155 int nbblks,
0156 char *data)
0157 {
0158 return xlog_do_io(log, blk_no, nbblks, data, REQ_OP_READ);
0159 }
0160
0161 STATIC int
0162 xlog_bread(
0163 struct xlog *log,
0164 xfs_daddr_t blk_no,
0165 int nbblks,
0166 char *data,
0167 char **offset)
0168 {
0169 int error;
0170
0171 error = xlog_do_io(log, blk_no, nbblks, data, REQ_OP_READ);
0172 if (!error)
0173 *offset = data + xlog_align(log, blk_no);
0174 return error;
0175 }
0176
0177 STATIC int
0178 xlog_bwrite(
0179 struct xlog *log,
0180 xfs_daddr_t blk_no,
0181 int nbblks,
0182 char *data)
0183 {
0184 return xlog_do_io(log, blk_no, nbblks, data, REQ_OP_WRITE);
0185 }
0186
0187 #ifdef DEBUG
0188
0189
0190
0191 STATIC void
0192 xlog_header_check_dump(
0193 xfs_mount_t *mp,
0194 xlog_rec_header_t *head)
0195 {
0196 xfs_debug(mp, "%s: SB : uuid = %pU, fmt = %d",
0197 __func__, &mp->m_sb.sb_uuid, XLOG_FMT);
0198 xfs_debug(mp, " log : uuid = %pU, fmt = %d",
0199 &head->h_fs_uuid, be32_to_cpu(head->h_fmt));
0200 }
0201 #else
0202 #define xlog_header_check_dump(mp, head)
0203 #endif
0204
0205
0206
0207
0208 STATIC int
0209 xlog_header_check_recover(
0210 xfs_mount_t *mp,
0211 xlog_rec_header_t *head)
0212 {
0213 ASSERT(head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM));
0214
0215
0216
0217
0218
0219
0220 if (XFS_IS_CORRUPT(mp, head->h_fmt != cpu_to_be32(XLOG_FMT))) {
0221 xfs_warn(mp,
0222 "dirty log written in incompatible format - can't recover");
0223 xlog_header_check_dump(mp, head);
0224 return -EFSCORRUPTED;
0225 }
0226 if (XFS_IS_CORRUPT(mp, !uuid_equal(&mp->m_sb.sb_uuid,
0227 &head->h_fs_uuid))) {
0228 xfs_warn(mp,
0229 "dirty log entry has mismatched uuid - can't recover");
0230 xlog_header_check_dump(mp, head);
0231 return -EFSCORRUPTED;
0232 }
0233 return 0;
0234 }
0235
0236
0237
0238
0239 STATIC int
0240 xlog_header_check_mount(
0241 xfs_mount_t *mp,
0242 xlog_rec_header_t *head)
0243 {
0244 ASSERT(head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM));
0245
0246 if (uuid_is_null(&head->h_fs_uuid)) {
0247
0248
0249
0250
0251
0252 xfs_warn(mp, "null uuid in log - IRIX style log");
0253 } else if (XFS_IS_CORRUPT(mp, !uuid_equal(&mp->m_sb.sb_uuid,
0254 &head->h_fs_uuid))) {
0255 xfs_warn(mp, "log has mismatched uuid - can't recover");
0256 xlog_header_check_dump(mp, head);
0257 return -EFSCORRUPTED;
0258 }
0259 return 0;
0260 }
0261
0262
0263
0264
0265
0266
0267
0268 STATIC int
0269 xlog_find_cycle_start(
0270 struct xlog *log,
0271 char *buffer,
0272 xfs_daddr_t first_blk,
0273 xfs_daddr_t *last_blk,
0274 uint cycle)
0275 {
0276 char *offset;
0277 xfs_daddr_t mid_blk;
0278 xfs_daddr_t end_blk;
0279 uint mid_cycle;
0280 int error;
0281
0282 end_blk = *last_blk;
0283 mid_blk = BLK_AVG(first_blk, end_blk);
0284 while (mid_blk != first_blk && mid_blk != end_blk) {
0285 error = xlog_bread(log, mid_blk, 1, buffer, &offset);
0286 if (error)
0287 return error;
0288 mid_cycle = xlog_get_cycle(offset);
0289 if (mid_cycle == cycle)
0290 end_blk = mid_blk;
0291 else
0292 first_blk = mid_blk;
0293 mid_blk = BLK_AVG(first_blk, end_blk);
0294 }
0295 ASSERT((mid_blk == first_blk && mid_blk+1 == end_blk) ||
0296 (mid_blk == end_blk && mid_blk-1 == first_blk));
0297
0298 *last_blk = end_blk;
0299
0300 return 0;
0301 }
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311 STATIC int
0312 xlog_find_verify_cycle(
0313 struct xlog *log,
0314 xfs_daddr_t start_blk,
0315 int nbblks,
0316 uint stop_on_cycle_no,
0317 xfs_daddr_t *new_blk)
0318 {
0319 xfs_daddr_t i, j;
0320 uint cycle;
0321 char *buffer;
0322 xfs_daddr_t bufblks;
0323 char *buf = NULL;
0324 int error = 0;
0325
0326
0327
0328
0329
0330
0331
0332 bufblks = 1 << ffs(nbblks);
0333 while (bufblks > log->l_logBBsize)
0334 bufblks >>= 1;
0335 while (!(buffer = xlog_alloc_buffer(log, bufblks))) {
0336 bufblks >>= 1;
0337 if (bufblks < log->l_sectBBsize)
0338 return -ENOMEM;
0339 }
0340
0341 for (i = start_blk; i < start_blk + nbblks; i += bufblks) {
0342 int bcount;
0343
0344 bcount = min(bufblks, (start_blk + nbblks - i));
0345
0346 error = xlog_bread(log, i, bcount, buffer, &buf);
0347 if (error)
0348 goto out;
0349
0350 for (j = 0; j < bcount; j++) {
0351 cycle = xlog_get_cycle(buf);
0352 if (cycle == stop_on_cycle_no) {
0353 *new_blk = i+j;
0354 goto out;
0355 }
0356
0357 buf += BBSIZE;
0358 }
0359 }
0360
0361 *new_blk = -1;
0362
0363 out:
0364 kmem_free(buffer);
0365 return error;
0366 }
0367
0368 static inline int
0369 xlog_logrec_hblks(struct xlog *log, struct xlog_rec_header *rh)
0370 {
0371 if (xfs_has_logv2(log->l_mp)) {
0372 int h_size = be32_to_cpu(rh->h_size);
0373
0374 if ((be32_to_cpu(rh->h_version) & XLOG_VERSION_2) &&
0375 h_size > XLOG_HEADER_CYCLE_SIZE)
0376 return DIV_ROUND_UP(h_size, XLOG_HEADER_CYCLE_SIZE);
0377 }
0378 return 1;
0379 }
0380
0381
0382
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393 STATIC int
0394 xlog_find_verify_log_record(
0395 struct xlog *log,
0396 xfs_daddr_t start_blk,
0397 xfs_daddr_t *last_blk,
0398 int extra_bblks)
0399 {
0400 xfs_daddr_t i;
0401 char *buffer;
0402 char *offset = NULL;
0403 xlog_rec_header_t *head = NULL;
0404 int error = 0;
0405 int smallmem = 0;
0406 int num_blks = *last_blk - start_blk;
0407 int xhdrs;
0408
0409 ASSERT(start_blk != 0 || *last_blk != start_blk);
0410
0411 buffer = xlog_alloc_buffer(log, num_blks);
0412 if (!buffer) {
0413 buffer = xlog_alloc_buffer(log, 1);
0414 if (!buffer)
0415 return -ENOMEM;
0416 smallmem = 1;
0417 } else {
0418 error = xlog_bread(log, start_blk, num_blks, buffer, &offset);
0419 if (error)
0420 goto out;
0421 offset += ((num_blks - 1) << BBSHIFT);
0422 }
0423
0424 for (i = (*last_blk) - 1; i >= 0; i--) {
0425 if (i < start_blk) {
0426
0427 xfs_warn(log->l_mp,
0428 "Log inconsistent (didn't find previous header)");
0429 ASSERT(0);
0430 error = -EFSCORRUPTED;
0431 goto out;
0432 }
0433
0434 if (smallmem) {
0435 error = xlog_bread(log, i, 1, buffer, &offset);
0436 if (error)
0437 goto out;
0438 }
0439
0440 head = (xlog_rec_header_t *)offset;
0441
0442 if (head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
0443 break;
0444
0445 if (!smallmem)
0446 offset -= BBSIZE;
0447 }
0448
0449
0450
0451
0452
0453
0454 if (i == -1) {
0455 error = 1;
0456 goto out;
0457 }
0458
0459
0460
0461
0462
0463 if ((error = xlog_header_check_mount(log->l_mp, head)))
0464 goto out;
0465
0466
0467
0468
0469
0470
0471
0472
0473 xhdrs = xlog_logrec_hblks(log, head);
0474
0475 if (*last_blk - i + extra_bblks !=
0476 BTOBB(be32_to_cpu(head->h_len)) + xhdrs)
0477 *last_blk = i;
0478
0479 out:
0480 kmem_free(buffer);
0481 return error;
0482 }
0483
0484
0485
0486
0487
0488
0489
0490
0491
0492
0493
0494
0495
0496
0497 STATIC int
0498 xlog_find_head(
0499 struct xlog *log,
0500 xfs_daddr_t *return_head_blk)
0501 {
0502 char *buffer;
0503 char *offset;
0504 xfs_daddr_t new_blk, first_blk, start_blk, last_blk, head_blk;
0505 int num_scan_bblks;
0506 uint first_half_cycle, last_half_cycle;
0507 uint stop_on_cycle;
0508 int error, log_bbnum = log->l_logBBsize;
0509
0510
0511 error = xlog_find_zeroed(log, &first_blk);
0512 if (error < 0) {
0513 xfs_warn(log->l_mp, "empty log check failed");
0514 return error;
0515 }
0516 if (error == 1) {
0517 *return_head_blk = first_blk;
0518
0519
0520 if (!first_blk) {
0521
0522
0523
0524
0525 xfs_warn(log->l_mp, "totally zeroed log");
0526 }
0527
0528 return 0;
0529 }
0530
0531 first_blk = 0;
0532 buffer = xlog_alloc_buffer(log, 1);
0533 if (!buffer)
0534 return -ENOMEM;
0535
0536 error = xlog_bread(log, 0, 1, buffer, &offset);
0537 if (error)
0538 goto out_free_buffer;
0539
0540 first_half_cycle = xlog_get_cycle(offset);
0541
0542 last_blk = head_blk = log_bbnum - 1;
0543 error = xlog_bread(log, last_blk, 1, buffer, &offset);
0544 if (error)
0545 goto out_free_buffer;
0546
0547 last_half_cycle = xlog_get_cycle(offset);
0548 ASSERT(last_half_cycle != 0);
0549
0550
0551
0552
0553
0554
0555
0556
0557
0558
0559
0560
0561 if (first_half_cycle == last_half_cycle) {
0562
0563
0564
0565
0566
0567
0568
0569
0570
0571
0572
0573
0574
0575
0576
0577
0578
0579
0580
0581
0582
0583
0584
0585
0586
0587 head_blk = log_bbnum;
0588 stop_on_cycle = last_half_cycle - 1;
0589 } else {
0590
0591
0592
0593
0594
0595
0596
0597
0598
0599
0600
0601
0602
0603
0604
0605
0606
0607
0608
0609
0610
0611
0612 stop_on_cycle = last_half_cycle;
0613 error = xlog_find_cycle_start(log, buffer, first_blk, &head_blk,
0614 last_half_cycle);
0615 if (error)
0616 goto out_free_buffer;
0617 }
0618
0619
0620
0621
0622
0623
0624
0625
0626 num_scan_bblks = min_t(int, log_bbnum, XLOG_TOTAL_REC_SHIFT(log));
0627 if (head_blk >= num_scan_bblks) {
0628
0629
0630
0631
0632 start_blk = head_blk - num_scan_bblks;
0633 if ((error = xlog_find_verify_cycle(log,
0634 start_blk, num_scan_bblks,
0635 stop_on_cycle, &new_blk)))
0636 goto out_free_buffer;
0637 if (new_blk != -1)
0638 head_blk = new_blk;
0639 } else {
0640
0641
0642
0643
0644
0645
0646
0647
0648
0649
0650
0651
0652
0653
0654
0655
0656
0657
0658
0659
0660
0661
0662
0663
0664
0665
0666
0667 ASSERT(head_blk <= INT_MAX &&
0668 (xfs_daddr_t) num_scan_bblks >= head_blk);
0669 start_blk = log_bbnum - (num_scan_bblks - head_blk);
0670 if ((error = xlog_find_verify_cycle(log, start_blk,
0671 num_scan_bblks - (int)head_blk,
0672 (stop_on_cycle - 1), &new_blk)))
0673 goto out_free_buffer;
0674 if (new_blk != -1) {
0675 head_blk = new_blk;
0676 goto validate_head;
0677 }
0678
0679
0680
0681
0682
0683
0684 start_blk = 0;
0685 ASSERT(head_blk <= INT_MAX);
0686 if ((error = xlog_find_verify_cycle(log,
0687 start_blk, (int)head_blk,
0688 stop_on_cycle, &new_blk)))
0689 goto out_free_buffer;
0690 if (new_blk != -1)
0691 head_blk = new_blk;
0692 }
0693
0694 validate_head:
0695
0696
0697
0698
0699 num_scan_bblks = XLOG_REC_SHIFT(log);
0700 if (head_blk >= num_scan_bblks) {
0701 start_blk = head_blk - num_scan_bblks;
0702
0703
0704 error = xlog_find_verify_log_record(log, start_blk, &head_blk, 0);
0705 if (error == 1)
0706 error = -EIO;
0707 if (error)
0708 goto out_free_buffer;
0709 } else {
0710 start_blk = 0;
0711 ASSERT(head_blk <= INT_MAX);
0712 error = xlog_find_verify_log_record(log, start_blk, &head_blk, 0);
0713 if (error < 0)
0714 goto out_free_buffer;
0715 if (error == 1) {
0716
0717 start_blk = log_bbnum - (num_scan_bblks - head_blk);
0718 new_blk = log_bbnum;
0719 ASSERT(start_blk <= INT_MAX &&
0720 (xfs_daddr_t) log_bbnum-start_blk >= 0);
0721 ASSERT(head_blk <= INT_MAX);
0722 error = xlog_find_verify_log_record(log, start_blk,
0723 &new_blk, (int)head_blk);
0724 if (error == 1)
0725 error = -EIO;
0726 if (error)
0727 goto out_free_buffer;
0728 if (new_blk != log_bbnum)
0729 head_blk = new_blk;
0730 } else if (error)
0731 goto out_free_buffer;
0732 }
0733
0734 kmem_free(buffer);
0735 if (head_blk == log_bbnum)
0736 *return_head_blk = 0;
0737 else
0738 *return_head_blk = head_blk;
0739
0740
0741
0742
0743
0744
0745 return 0;
0746
0747 out_free_buffer:
0748 kmem_free(buffer);
0749 if (error)
0750 xfs_warn(log->l_mp, "failed to find log head");
0751 return error;
0752 }
0753
0754
0755
0756
0757
0758
0759
0760
0761
0762 STATIC int
0763 xlog_rseek_logrec_hdr(
0764 struct xlog *log,
0765 xfs_daddr_t head_blk,
0766 xfs_daddr_t tail_blk,
0767 int count,
0768 char *buffer,
0769 xfs_daddr_t *rblk,
0770 struct xlog_rec_header **rhead,
0771 bool *wrapped)
0772 {
0773 int i;
0774 int error;
0775 int found = 0;
0776 char *offset = NULL;
0777 xfs_daddr_t end_blk;
0778
0779 *wrapped = false;
0780
0781
0782
0783
0784
0785 end_blk = head_blk > tail_blk ? tail_blk : 0;
0786 for (i = (int) head_blk - 1; i >= end_blk; i--) {
0787 error = xlog_bread(log, i, 1, buffer, &offset);
0788 if (error)
0789 goto out_error;
0790
0791 if (*(__be32 *) offset == cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
0792 *rblk = i;
0793 *rhead = (struct xlog_rec_header *) offset;
0794 if (++found == count)
0795 break;
0796 }
0797 }
0798
0799
0800
0801
0802
0803
0804 if (tail_blk >= head_blk && found != count) {
0805 for (i = log->l_logBBsize - 1; i >= (int) tail_blk; i--) {
0806 error = xlog_bread(log, i, 1, buffer, &offset);
0807 if (error)
0808 goto out_error;
0809
0810 if (*(__be32 *)offset ==
0811 cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
0812 *wrapped = true;
0813 *rblk = i;
0814 *rhead = (struct xlog_rec_header *) offset;
0815 if (++found == count)
0816 break;
0817 }
0818 }
0819 }
0820
0821 return found;
0822
0823 out_error:
0824 return error;
0825 }
0826
0827
0828
0829
0830
0831
0832
0833
0834
0835
0836 STATIC int
0837 xlog_seek_logrec_hdr(
0838 struct xlog *log,
0839 xfs_daddr_t head_blk,
0840 xfs_daddr_t tail_blk,
0841 int count,
0842 char *buffer,
0843 xfs_daddr_t *rblk,
0844 struct xlog_rec_header **rhead,
0845 bool *wrapped)
0846 {
0847 int i;
0848 int error;
0849 int found = 0;
0850 char *offset = NULL;
0851 xfs_daddr_t end_blk;
0852
0853 *wrapped = false;
0854
0855
0856
0857
0858
0859 end_blk = head_blk > tail_blk ? head_blk : log->l_logBBsize - 1;
0860 for (i = (int) tail_blk; i <= end_blk; i++) {
0861 error = xlog_bread(log, i, 1, buffer, &offset);
0862 if (error)
0863 goto out_error;
0864
0865 if (*(__be32 *) offset == cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
0866 *rblk = i;
0867 *rhead = (struct xlog_rec_header *) offset;
0868 if (++found == count)
0869 break;
0870 }
0871 }
0872
0873
0874
0875
0876
0877 if (tail_blk > head_blk && found != count) {
0878 for (i = 0; i < (int) head_blk; i++) {
0879 error = xlog_bread(log, i, 1, buffer, &offset);
0880 if (error)
0881 goto out_error;
0882
0883 if (*(__be32 *)offset ==
0884 cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
0885 *wrapped = true;
0886 *rblk = i;
0887 *rhead = (struct xlog_rec_header *) offset;
0888 if (++found == count)
0889 break;
0890 }
0891 }
0892 }
0893
0894 return found;
0895
0896 out_error:
0897 return error;
0898 }
0899
0900
0901
0902
0903 static inline int
0904 xlog_tail_distance(
0905 struct xlog *log,
0906 xfs_daddr_t head_blk,
0907 xfs_daddr_t tail_blk)
0908 {
0909 if (head_blk < tail_blk)
0910 return tail_blk - head_blk;
0911
0912 return tail_blk + (log->l_logBBsize - head_blk);
0913 }
0914
0915
0916
0917
0918
0919
0920
0921
0922
0923
0924
0925
0926
0927
0928
0929
0930
0931
0932
0933
0934 STATIC int
0935 xlog_verify_tail(
0936 struct xlog *log,
0937 xfs_daddr_t head_blk,
0938 xfs_daddr_t *tail_blk,
0939 int hsize)
0940 {
0941 struct xlog_rec_header *thead;
0942 char *buffer;
0943 xfs_daddr_t first_bad;
0944 int error = 0;
0945 bool wrapped;
0946 xfs_daddr_t tmp_tail;
0947 xfs_daddr_t orig_tail = *tail_blk;
0948
0949 buffer = xlog_alloc_buffer(log, 1);
0950 if (!buffer)
0951 return -ENOMEM;
0952
0953
0954
0955
0956
0957 error = xlog_seek_logrec_hdr(log, head_blk, *tail_blk, 1, buffer,
0958 &tmp_tail, &thead, &wrapped);
0959 if (error < 0)
0960 goto out;
0961 if (*tail_blk != tmp_tail)
0962 *tail_blk = tmp_tail;
0963
0964
0965
0966
0967
0968
0969
0970
0971 first_bad = 0;
0972 error = xlog_do_recovery_pass(log, head_blk, *tail_blk,
0973 XLOG_RECOVER_CRCPASS, &first_bad);
0974 while ((error == -EFSBADCRC || error == -EFSCORRUPTED) && first_bad) {
0975 int tail_distance;
0976
0977
0978
0979
0980
0981 tail_distance = xlog_tail_distance(log, head_blk, first_bad);
0982 if (tail_distance > BTOBB(XLOG_MAX_ICLOGS * hsize))
0983 break;
0984
0985
0986 error = xlog_seek_logrec_hdr(log, head_blk, first_bad, 2,
0987 buffer, &tmp_tail, &thead, &wrapped);
0988 if (error < 0)
0989 goto out;
0990
0991 *tail_blk = tmp_tail;
0992 first_bad = 0;
0993 error = xlog_do_recovery_pass(log, head_blk, *tail_blk,
0994 XLOG_RECOVER_CRCPASS, &first_bad);
0995 }
0996
0997 if (!error && *tail_blk != orig_tail)
0998 xfs_warn(log->l_mp,
0999 "Tail block (0x%llx) overwrite detected. Updated to 0x%llx",
1000 orig_tail, *tail_blk);
1001 out:
1002 kmem_free(buffer);
1003 return error;
1004 }
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019 STATIC int
1020 xlog_verify_head(
1021 struct xlog *log,
1022 xfs_daddr_t *head_blk,
1023 xfs_daddr_t *tail_blk,
1024 char *buffer,
1025 xfs_daddr_t *rhead_blk,
1026 struct xlog_rec_header **rhead,
1027 bool *wrapped)
1028 {
1029 struct xlog_rec_header *tmp_rhead;
1030 char *tmp_buffer;
1031 xfs_daddr_t first_bad;
1032 xfs_daddr_t tmp_rhead_blk;
1033 int found;
1034 int error;
1035 bool tmp_wrapped;
1036
1037
1038
1039
1040
1041
1042
1043 tmp_buffer = xlog_alloc_buffer(log, 1);
1044 if (!tmp_buffer)
1045 return -ENOMEM;
1046 error = xlog_rseek_logrec_hdr(log, *head_blk, *tail_blk,
1047 XLOG_MAX_ICLOGS, tmp_buffer,
1048 &tmp_rhead_blk, &tmp_rhead, &tmp_wrapped);
1049 kmem_free(tmp_buffer);
1050 if (error < 0)
1051 return error;
1052
1053
1054
1055
1056
1057
1058 error = xlog_do_recovery_pass(log, *head_blk, tmp_rhead_blk,
1059 XLOG_RECOVER_CRCPASS, &first_bad);
1060 if ((error == -EFSBADCRC || error == -EFSCORRUPTED) && first_bad) {
1061
1062
1063
1064
1065 error = 0;
1066 xfs_warn(log->l_mp,
1067 "Torn write (CRC failure) detected at log block 0x%llx. Truncating head block from 0x%llx.",
1068 first_bad, *head_blk);
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078 found = xlog_rseek_logrec_hdr(log, first_bad, *tail_blk, 1,
1079 buffer, rhead_blk, rhead, wrapped);
1080 if (found < 0)
1081 return found;
1082 if (found == 0)
1083 return -EIO;
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094 *head_blk = first_bad;
1095 *tail_blk = BLOCK_LSN(be64_to_cpu((*rhead)->h_tail_lsn));
1096 if (*head_blk == *tail_blk) {
1097 ASSERT(0);
1098 return 0;
1099 }
1100 }
1101 if (error)
1102 return error;
1103
1104 return xlog_verify_tail(log, *head_blk, tail_blk,
1105 be32_to_cpu((*rhead)->h_size));
1106 }
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116 static inline xfs_daddr_t
1117 xlog_wrap_logbno(
1118 struct xlog *log,
1119 xfs_daddr_t bno)
1120 {
1121 int mod;
1122
1123 div_s64_rem(bno, log->l_logBBsize, &mod);
1124 return mod;
1125 }
1126
1127
1128
1129
1130
1131
1132 static int
1133 xlog_check_unmount_rec(
1134 struct xlog *log,
1135 xfs_daddr_t *head_blk,
1136 xfs_daddr_t *tail_blk,
1137 struct xlog_rec_header *rhead,
1138 xfs_daddr_t rhead_blk,
1139 char *buffer,
1140 bool *clean)
1141 {
1142 struct xlog_op_header *op_head;
1143 xfs_daddr_t umount_data_blk;
1144 xfs_daddr_t after_umount_blk;
1145 int hblks;
1146 int error;
1147 char *offset;
1148
1149 *clean = false;
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160 hblks = xlog_logrec_hblks(log, rhead);
1161 after_umount_blk = xlog_wrap_logbno(log,
1162 rhead_blk + hblks + BTOBB(be32_to_cpu(rhead->h_len)));
1163
1164 if (*head_blk == after_umount_blk &&
1165 be32_to_cpu(rhead->h_num_logops) == 1) {
1166 umount_data_blk = xlog_wrap_logbno(log, rhead_blk + hblks);
1167 error = xlog_bread(log, umount_data_blk, 1, buffer, &offset);
1168 if (error)
1169 return error;
1170
1171 op_head = (struct xlog_op_header *)offset;
1172 if (op_head->oh_flags & XLOG_UNMOUNT_TRANS) {
1173
1174
1175
1176
1177
1178 xlog_assign_atomic_lsn(&log->l_tail_lsn,
1179 log->l_curr_cycle, after_umount_blk);
1180 xlog_assign_atomic_lsn(&log->l_last_sync_lsn,
1181 log->l_curr_cycle, after_umount_blk);
1182 *tail_blk = after_umount_blk;
1183
1184 *clean = true;
1185 }
1186 }
1187
1188 return 0;
1189 }
1190
1191 static void
1192 xlog_set_state(
1193 struct xlog *log,
1194 xfs_daddr_t head_blk,
1195 struct xlog_rec_header *rhead,
1196 xfs_daddr_t rhead_blk,
1197 bool bump_cycle)
1198 {
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209 log->l_prev_block = rhead_blk;
1210 log->l_curr_block = (int)head_blk;
1211 log->l_curr_cycle = be32_to_cpu(rhead->h_cycle);
1212 if (bump_cycle)
1213 log->l_curr_cycle++;
1214 atomic64_set(&log->l_tail_lsn, be64_to_cpu(rhead->h_tail_lsn));
1215 atomic64_set(&log->l_last_sync_lsn, be64_to_cpu(rhead->h_lsn));
1216 xlog_assign_grant_head(&log->l_reserve_head.grant, log->l_curr_cycle,
1217 BBTOB(log->l_curr_block));
1218 xlog_assign_grant_head(&log->l_write_head.grant, log->l_curr_cycle,
1219 BBTOB(log->l_curr_block));
1220 }
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238 STATIC int
1239 xlog_find_tail(
1240 struct xlog *log,
1241 xfs_daddr_t *head_blk,
1242 xfs_daddr_t *tail_blk)
1243 {
1244 xlog_rec_header_t *rhead;
1245 char *offset = NULL;
1246 char *buffer;
1247 int error;
1248 xfs_daddr_t rhead_blk;
1249 xfs_lsn_t tail_lsn;
1250 bool wrapped = false;
1251 bool clean = false;
1252
1253
1254
1255
1256 if ((error = xlog_find_head(log, head_blk)))
1257 return error;
1258 ASSERT(*head_blk < INT_MAX);
1259
1260 buffer = xlog_alloc_buffer(log, 1);
1261 if (!buffer)
1262 return -ENOMEM;
1263 if (*head_blk == 0) {
1264 error = xlog_bread(log, 0, 1, buffer, &offset);
1265 if (error)
1266 goto done;
1267
1268 if (xlog_get_cycle(offset) == 0) {
1269 *tail_blk = 0;
1270
1271 goto done;
1272 }
1273 }
1274
1275
1276
1277
1278
1279
1280 error = xlog_rseek_logrec_hdr(log, *head_blk, *head_blk, 1, buffer,
1281 &rhead_blk, &rhead, &wrapped);
1282 if (error < 0)
1283 goto done;
1284 if (!error) {
1285 xfs_warn(log->l_mp, "%s: couldn't find sync record", __func__);
1286 error = -EFSCORRUPTED;
1287 goto done;
1288 }
1289 *tail_blk = BLOCK_LSN(be64_to_cpu(rhead->h_tail_lsn));
1290
1291
1292
1293
1294 xlog_set_state(log, *head_blk, rhead, rhead_blk, wrapped);
1295 tail_lsn = atomic64_read(&log->l_tail_lsn);
1296
1297
1298
1299
1300
1301 error = xlog_check_unmount_rec(log, head_blk, tail_blk, rhead,
1302 rhead_blk, buffer, &clean);
1303 if (error)
1304 goto done;
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316 if (!clean) {
1317 xfs_daddr_t orig_head = *head_blk;
1318
1319 error = xlog_verify_head(log, head_blk, tail_blk, buffer,
1320 &rhead_blk, &rhead, &wrapped);
1321 if (error)
1322 goto done;
1323
1324
1325 if (*head_blk != orig_head) {
1326 xlog_set_state(log, *head_blk, rhead, rhead_blk,
1327 wrapped);
1328 tail_lsn = atomic64_read(&log->l_tail_lsn);
1329 error = xlog_check_unmount_rec(log, head_blk, tail_blk,
1330 rhead, rhead_blk, buffer,
1331 &clean);
1332 if (error)
1333 goto done;
1334 }
1335 }
1336
1337
1338
1339
1340
1341
1342 if (clean)
1343 set_bit(XFS_OPSTATE_CLEAN, &log->l_mp->m_opstate);
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364 if (!xfs_readonly_buftarg(log->l_targ))
1365 error = xlog_clear_stale_blocks(log, tail_lsn);
1366
1367 done:
1368 kmem_free(buffer);
1369
1370 if (error)
1371 xfs_warn(log->l_mp, "failed to locate log tail");
1372 return error;
1373 }
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391 STATIC int
1392 xlog_find_zeroed(
1393 struct xlog *log,
1394 xfs_daddr_t *blk_no)
1395 {
1396 char *buffer;
1397 char *offset;
1398 uint first_cycle, last_cycle;
1399 xfs_daddr_t new_blk, last_blk, start_blk;
1400 xfs_daddr_t num_scan_bblks;
1401 int error, log_bbnum = log->l_logBBsize;
1402
1403 *blk_no = 0;
1404
1405
1406 buffer = xlog_alloc_buffer(log, 1);
1407 if (!buffer)
1408 return -ENOMEM;
1409 error = xlog_bread(log, 0, 1, buffer, &offset);
1410 if (error)
1411 goto out_free_buffer;
1412
1413 first_cycle = xlog_get_cycle(offset);
1414 if (first_cycle == 0) {
1415 *blk_no = 0;
1416 kmem_free(buffer);
1417 return 1;
1418 }
1419
1420
1421 error = xlog_bread(log, log_bbnum-1, 1, buffer, &offset);
1422 if (error)
1423 goto out_free_buffer;
1424
1425 last_cycle = xlog_get_cycle(offset);
1426 if (last_cycle != 0) {
1427 kmem_free(buffer);
1428 return 0;
1429 }
1430
1431
1432 last_blk = log_bbnum-1;
1433 error = xlog_find_cycle_start(log, buffer, 0, &last_blk, 0);
1434 if (error)
1435 goto out_free_buffer;
1436
1437
1438
1439
1440
1441
1442
1443 num_scan_bblks = XLOG_TOTAL_REC_SHIFT(log);
1444 ASSERT(num_scan_bblks <= INT_MAX);
1445
1446 if (last_blk < num_scan_bblks)
1447 num_scan_bblks = last_blk;
1448 start_blk = last_blk - num_scan_bblks;
1449
1450
1451
1452
1453
1454
1455
1456 if ((error = xlog_find_verify_cycle(log, start_blk,
1457 (int)num_scan_bblks, 0, &new_blk)))
1458 goto out_free_buffer;
1459 if (new_blk != -1)
1460 last_blk = new_blk;
1461
1462
1463
1464
1465
1466 error = xlog_find_verify_log_record(log, start_blk, &last_blk, 0);
1467 if (error == 1)
1468 error = -EIO;
1469 if (error)
1470 goto out_free_buffer;
1471
1472 *blk_no = last_blk;
1473 out_free_buffer:
1474 kmem_free(buffer);
1475 if (error)
1476 return error;
1477 return 1;
1478 }
1479
1480
1481
1482
1483
1484
1485 STATIC void
1486 xlog_add_record(
1487 struct xlog *log,
1488 char *buf,
1489 int cycle,
1490 int block,
1491 int tail_cycle,
1492 int tail_block)
1493 {
1494 xlog_rec_header_t *recp = (xlog_rec_header_t *)buf;
1495
1496 memset(buf, 0, BBSIZE);
1497 recp->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1498 recp->h_cycle = cpu_to_be32(cycle);
1499 recp->h_version = cpu_to_be32(
1500 xfs_has_logv2(log->l_mp) ? 2 : 1);
1501 recp->h_lsn = cpu_to_be64(xlog_assign_lsn(cycle, block));
1502 recp->h_tail_lsn = cpu_to_be64(xlog_assign_lsn(tail_cycle, tail_block));
1503 recp->h_fmt = cpu_to_be32(XLOG_FMT);
1504 memcpy(&recp->h_fs_uuid, &log->l_mp->m_sb.sb_uuid, sizeof(uuid_t));
1505 }
1506
1507 STATIC int
1508 xlog_write_log_records(
1509 struct xlog *log,
1510 int cycle,
1511 int start_block,
1512 int blocks,
1513 int tail_cycle,
1514 int tail_block)
1515 {
1516 char *offset;
1517 char *buffer;
1518 int balign, ealign;
1519 int sectbb = log->l_sectBBsize;
1520 int end_block = start_block + blocks;
1521 int bufblks;
1522 int error = 0;
1523 int i, j = 0;
1524
1525
1526
1527
1528
1529
1530
1531 bufblks = 1 << ffs(blocks);
1532 while (bufblks > log->l_logBBsize)
1533 bufblks >>= 1;
1534 while (!(buffer = xlog_alloc_buffer(log, bufblks))) {
1535 bufblks >>= 1;
1536 if (bufblks < sectbb)
1537 return -ENOMEM;
1538 }
1539
1540
1541
1542
1543
1544 balign = round_down(start_block, sectbb);
1545 if (balign != start_block) {
1546 error = xlog_bread_noalign(log, start_block, 1, buffer);
1547 if (error)
1548 goto out_free_buffer;
1549
1550 j = start_block - balign;
1551 }
1552
1553 for (i = start_block; i < end_block; i += bufblks) {
1554 int bcount, endcount;
1555
1556 bcount = min(bufblks, end_block - start_block);
1557 endcount = bcount - j;
1558
1559
1560
1561
1562
1563 ealign = round_down(end_block, sectbb);
1564 if (j == 0 && (start_block + endcount > ealign)) {
1565 error = xlog_bread_noalign(log, ealign, sectbb,
1566 buffer + BBTOB(ealign - start_block));
1567 if (error)
1568 break;
1569
1570 }
1571
1572 offset = buffer + xlog_align(log, start_block);
1573 for (; j < endcount; j++) {
1574 xlog_add_record(log, offset, cycle, i+j,
1575 tail_cycle, tail_block);
1576 offset += BBSIZE;
1577 }
1578 error = xlog_bwrite(log, start_block, endcount, buffer);
1579 if (error)
1580 break;
1581 start_block += endcount;
1582 j = 0;
1583 }
1584
1585 out_free_buffer:
1586 kmem_free(buffer);
1587 return error;
1588 }
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606 STATIC int
1607 xlog_clear_stale_blocks(
1608 struct xlog *log,
1609 xfs_lsn_t tail_lsn)
1610 {
1611 int tail_cycle, head_cycle;
1612 int tail_block, head_block;
1613 int tail_distance, max_distance;
1614 int distance;
1615 int error;
1616
1617 tail_cycle = CYCLE_LSN(tail_lsn);
1618 tail_block = BLOCK_LSN(tail_lsn);
1619 head_cycle = log->l_curr_cycle;
1620 head_block = log->l_curr_block;
1621
1622
1623
1624
1625
1626
1627
1628 if (head_cycle == tail_cycle) {
1629
1630
1631
1632
1633
1634
1635
1636 if (XFS_IS_CORRUPT(log->l_mp,
1637 head_block < tail_block ||
1638 head_block >= log->l_logBBsize))
1639 return -EFSCORRUPTED;
1640 tail_distance = tail_block + (log->l_logBBsize - head_block);
1641 } else {
1642
1643
1644
1645
1646
1647 if (XFS_IS_CORRUPT(log->l_mp,
1648 head_block >= tail_block ||
1649 head_cycle != tail_cycle + 1))
1650 return -EFSCORRUPTED;
1651 tail_distance = tail_block - head_block;
1652 }
1653
1654
1655
1656
1657
1658 if (tail_distance <= 0) {
1659 ASSERT(tail_distance == 0);
1660 return 0;
1661 }
1662
1663 max_distance = XLOG_TOTAL_REC_SHIFT(log);
1664
1665
1666
1667
1668
1669
1670
1671 max_distance = min(max_distance, tail_distance);
1672
1673 if ((head_block + max_distance) <= log->l_logBBsize) {
1674
1675
1676
1677
1678
1679
1680
1681 error = xlog_write_log_records(log, (head_cycle - 1),
1682 head_block, max_distance, tail_cycle,
1683 tail_block);
1684 if (error)
1685 return error;
1686 } else {
1687
1688
1689
1690
1691
1692
1693
1694 distance = log->l_logBBsize - head_block;
1695 error = xlog_write_log_records(log, (head_cycle - 1),
1696 head_block, distance, tail_cycle,
1697 tail_block);
1698
1699 if (error)
1700 return error;
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710 distance = max_distance - (log->l_logBBsize - head_block);
1711 error = xlog_write_log_records(log, head_cycle, 0, distance,
1712 tail_cycle, tail_block);
1713 if (error)
1714 return error;
1715 }
1716
1717 return 0;
1718 }
1719
1720
1721
1722
1723
1724 void
1725 xlog_recover_release_intent(
1726 struct xlog *log,
1727 unsigned short intent_type,
1728 uint64_t intent_id)
1729 {
1730 struct xfs_ail_cursor cur;
1731 struct xfs_log_item *lip;
1732 struct xfs_ail *ailp = log->l_ailp;
1733
1734 spin_lock(&ailp->ail_lock);
1735 for (lip = xfs_trans_ail_cursor_first(ailp, &cur, 0); lip != NULL;
1736 lip = xfs_trans_ail_cursor_next(ailp, &cur)) {
1737 if (lip->li_type != intent_type)
1738 continue;
1739 if (!lip->li_ops->iop_match(lip, intent_id))
1740 continue;
1741
1742 spin_unlock(&ailp->ail_lock);
1743 lip->li_ops->iop_release(lip);
1744 spin_lock(&ailp->ail_lock);
1745 break;
1746 }
1747
1748 xfs_trans_ail_cursor_done(&cur);
1749 spin_unlock(&ailp->ail_lock);
1750 }
1751
1752 int
1753 xlog_recover_iget(
1754 struct xfs_mount *mp,
1755 xfs_ino_t ino,
1756 struct xfs_inode **ipp)
1757 {
1758 int error;
1759
1760 error = xfs_iget(mp, NULL, ino, 0, 0, ipp);
1761 if (error)
1762 return error;
1763
1764 error = xfs_qm_dqattach(*ipp);
1765 if (error) {
1766 xfs_irele(*ipp);
1767 return error;
1768 }
1769
1770 if (VFS_I(*ipp)->i_nlink == 0)
1771 xfs_iflags_set(*ipp, XFS_IRECOVERY);
1772
1773 return 0;
1774 }
1775
1776
1777
1778
1779
1780
1781
1782 static const struct xlog_recover_item_ops *xlog_recover_item_ops[] = {
1783 &xlog_buf_item_ops,
1784 &xlog_inode_item_ops,
1785 &xlog_dquot_item_ops,
1786 &xlog_quotaoff_item_ops,
1787 &xlog_icreate_item_ops,
1788 &xlog_efi_item_ops,
1789 &xlog_efd_item_ops,
1790 &xlog_rui_item_ops,
1791 &xlog_rud_item_ops,
1792 &xlog_cui_item_ops,
1793 &xlog_cud_item_ops,
1794 &xlog_bui_item_ops,
1795 &xlog_bud_item_ops,
1796 &xlog_attri_item_ops,
1797 &xlog_attrd_item_ops,
1798 };
1799
1800 static const struct xlog_recover_item_ops *
1801 xlog_find_item_ops(
1802 struct xlog_recover_item *item)
1803 {
1804 unsigned int i;
1805
1806 for (i = 0; i < ARRAY_SIZE(xlog_recover_item_ops); i++)
1807 if (ITEM_TYPE(item) == xlog_recover_item_ops[i]->item_type)
1808 return xlog_recover_item_ops[i];
1809
1810 return NULL;
1811 }
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862 STATIC int
1863 xlog_recover_reorder_trans(
1864 struct xlog *log,
1865 struct xlog_recover *trans,
1866 int pass)
1867 {
1868 struct xlog_recover_item *item, *n;
1869 int error = 0;
1870 LIST_HEAD(sort_list);
1871 LIST_HEAD(cancel_list);
1872 LIST_HEAD(buffer_list);
1873 LIST_HEAD(inode_buffer_list);
1874 LIST_HEAD(item_list);
1875
1876 list_splice_init(&trans->r_itemq, &sort_list);
1877 list_for_each_entry_safe(item, n, &sort_list, ri_list) {
1878 enum xlog_recover_reorder fate = XLOG_REORDER_ITEM_LIST;
1879
1880 item->ri_ops = xlog_find_item_ops(item);
1881 if (!item->ri_ops) {
1882 xfs_warn(log->l_mp,
1883 "%s: unrecognized type of log operation (%d)",
1884 __func__, ITEM_TYPE(item));
1885 ASSERT(0);
1886
1887
1888
1889
1890 if (!list_empty(&sort_list))
1891 list_splice_init(&sort_list, &trans->r_itemq);
1892 error = -EFSCORRUPTED;
1893 break;
1894 }
1895
1896 if (item->ri_ops->reorder)
1897 fate = item->ri_ops->reorder(item);
1898
1899 switch (fate) {
1900 case XLOG_REORDER_BUFFER_LIST:
1901 list_move_tail(&item->ri_list, &buffer_list);
1902 break;
1903 case XLOG_REORDER_CANCEL_LIST:
1904 trace_xfs_log_recover_item_reorder_head(log,
1905 trans, item, pass);
1906 list_move(&item->ri_list, &cancel_list);
1907 break;
1908 case XLOG_REORDER_INODE_BUFFER_LIST:
1909 list_move(&item->ri_list, &inode_buffer_list);
1910 break;
1911 case XLOG_REORDER_ITEM_LIST:
1912 trace_xfs_log_recover_item_reorder_tail(log,
1913 trans, item, pass);
1914 list_move_tail(&item->ri_list, &item_list);
1915 break;
1916 }
1917 }
1918
1919 ASSERT(list_empty(&sort_list));
1920 if (!list_empty(&buffer_list))
1921 list_splice(&buffer_list, &trans->r_itemq);
1922 if (!list_empty(&item_list))
1923 list_splice_tail(&item_list, &trans->r_itemq);
1924 if (!list_empty(&inode_buffer_list))
1925 list_splice_tail(&inode_buffer_list, &trans->r_itemq);
1926 if (!list_empty(&cancel_list))
1927 list_splice_tail(&cancel_list, &trans->r_itemq);
1928 return error;
1929 }
1930
1931 void
1932 xlog_buf_readahead(
1933 struct xlog *log,
1934 xfs_daddr_t blkno,
1935 uint len,
1936 const struct xfs_buf_ops *ops)
1937 {
1938 if (!xlog_is_buffer_cancelled(log, blkno, len))
1939 xfs_buf_readahead(log->l_mp->m_ddev_targp, blkno, len, ops);
1940 }
1941
1942 STATIC int
1943 xlog_recover_items_pass2(
1944 struct xlog *log,
1945 struct xlog_recover *trans,
1946 struct list_head *buffer_list,
1947 struct list_head *item_list)
1948 {
1949 struct xlog_recover_item *item;
1950 int error = 0;
1951
1952 list_for_each_entry(item, item_list, ri_list) {
1953 trace_xfs_log_recover_item_recover(log, trans, item,
1954 XLOG_RECOVER_PASS2);
1955
1956 if (item->ri_ops->commit_pass2)
1957 error = item->ri_ops->commit_pass2(log, buffer_list,
1958 item, trans->r_lsn);
1959 if (error)
1960 return error;
1961 }
1962
1963 return error;
1964 }
1965
1966
1967
1968
1969
1970
1971
1972 STATIC int
1973 xlog_recover_commit_trans(
1974 struct xlog *log,
1975 struct xlog_recover *trans,
1976 int pass,
1977 struct list_head *buffer_list)
1978 {
1979 int error = 0;
1980 int items_queued = 0;
1981 struct xlog_recover_item *item;
1982 struct xlog_recover_item *next;
1983 LIST_HEAD (ra_list);
1984 LIST_HEAD (done_list);
1985
1986 #define XLOG_RECOVER_COMMIT_QUEUE_MAX 100
1987
1988 hlist_del_init(&trans->r_list);
1989
1990 error = xlog_recover_reorder_trans(log, trans, pass);
1991 if (error)
1992 return error;
1993
1994 list_for_each_entry_safe(item, next, &trans->r_itemq, ri_list) {
1995 trace_xfs_log_recover_item_recover(log, trans, item, pass);
1996
1997 switch (pass) {
1998 case XLOG_RECOVER_PASS1:
1999 if (item->ri_ops->commit_pass1)
2000 error = item->ri_ops->commit_pass1(log, item);
2001 break;
2002 case XLOG_RECOVER_PASS2:
2003 if (item->ri_ops->ra_pass2)
2004 item->ri_ops->ra_pass2(log, item);
2005 list_move_tail(&item->ri_list, &ra_list);
2006 items_queued++;
2007 if (items_queued >= XLOG_RECOVER_COMMIT_QUEUE_MAX) {
2008 error = xlog_recover_items_pass2(log, trans,
2009 buffer_list, &ra_list);
2010 list_splice_tail_init(&ra_list, &done_list);
2011 items_queued = 0;
2012 }
2013
2014 break;
2015 default:
2016 ASSERT(0);
2017 }
2018
2019 if (error)
2020 goto out;
2021 }
2022
2023 out:
2024 if (!list_empty(&ra_list)) {
2025 if (!error)
2026 error = xlog_recover_items_pass2(log, trans,
2027 buffer_list, &ra_list);
2028 list_splice_tail_init(&ra_list, &done_list);
2029 }
2030
2031 if (!list_empty(&done_list))
2032 list_splice_init(&done_list, &trans->r_itemq);
2033
2034 return error;
2035 }
2036
2037 STATIC void
2038 xlog_recover_add_item(
2039 struct list_head *head)
2040 {
2041 struct xlog_recover_item *item;
2042
2043 item = kmem_zalloc(sizeof(struct xlog_recover_item), 0);
2044 INIT_LIST_HEAD(&item->ri_list);
2045 list_add_tail(&item->ri_list, head);
2046 }
2047
2048 STATIC int
2049 xlog_recover_add_to_cont_trans(
2050 struct xlog *log,
2051 struct xlog_recover *trans,
2052 char *dp,
2053 int len)
2054 {
2055 struct xlog_recover_item *item;
2056 char *ptr, *old_ptr;
2057 int old_len;
2058
2059
2060
2061
2062
2063 if (list_empty(&trans->r_itemq)) {
2064 ASSERT(len <= sizeof(struct xfs_trans_header));
2065 if (len > sizeof(struct xfs_trans_header)) {
2066 xfs_warn(log->l_mp, "%s: bad header length", __func__);
2067 return -EFSCORRUPTED;
2068 }
2069
2070 xlog_recover_add_item(&trans->r_itemq);
2071 ptr = (char *)&trans->r_theader +
2072 sizeof(struct xfs_trans_header) - len;
2073 memcpy(ptr, dp, len);
2074 return 0;
2075 }
2076
2077
2078 item = list_entry(trans->r_itemq.prev, struct xlog_recover_item,
2079 ri_list);
2080
2081 old_ptr = item->ri_buf[item->ri_cnt-1].i_addr;
2082 old_len = item->ri_buf[item->ri_cnt-1].i_len;
2083
2084 ptr = kvrealloc(old_ptr, old_len, len + old_len, GFP_KERNEL);
2085 if (!ptr)
2086 return -ENOMEM;
2087 memcpy(&ptr[old_len], dp, len);
2088 item->ri_buf[item->ri_cnt-1].i_len += len;
2089 item->ri_buf[item->ri_cnt-1].i_addr = ptr;
2090 trace_xfs_log_recover_item_add_cont(log, trans, item, 0);
2091 return 0;
2092 }
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107 STATIC int
2108 xlog_recover_add_to_trans(
2109 struct xlog *log,
2110 struct xlog_recover *trans,
2111 char *dp,
2112 int len)
2113 {
2114 struct xfs_inode_log_format *in_f;
2115 struct xlog_recover_item *item;
2116 char *ptr;
2117
2118 if (!len)
2119 return 0;
2120 if (list_empty(&trans->r_itemq)) {
2121
2122 if (*(uint *)dp != XFS_TRANS_HEADER_MAGIC) {
2123 xfs_warn(log->l_mp, "%s: bad header magic number",
2124 __func__);
2125 ASSERT(0);
2126 return -EFSCORRUPTED;
2127 }
2128
2129 if (len > sizeof(struct xfs_trans_header)) {
2130 xfs_warn(log->l_mp, "%s: bad header length", __func__);
2131 ASSERT(0);
2132 return -EFSCORRUPTED;
2133 }
2134
2135
2136
2137
2138
2139
2140 if (len == sizeof(struct xfs_trans_header))
2141 xlog_recover_add_item(&trans->r_itemq);
2142 memcpy(&trans->r_theader, dp, len);
2143 return 0;
2144 }
2145
2146 ptr = kmem_alloc(len, 0);
2147 memcpy(ptr, dp, len);
2148 in_f = (struct xfs_inode_log_format *)ptr;
2149
2150
2151 item = list_entry(trans->r_itemq.prev, struct xlog_recover_item,
2152 ri_list);
2153 if (item->ri_total != 0 &&
2154 item->ri_total == item->ri_cnt) {
2155
2156 xlog_recover_add_item(&trans->r_itemq);
2157 item = list_entry(trans->r_itemq.prev,
2158 struct xlog_recover_item, ri_list);
2159 }
2160
2161 if (item->ri_total == 0) {
2162 if (in_f->ilf_size == 0 ||
2163 in_f->ilf_size > XLOG_MAX_REGIONS_IN_ITEM) {
2164 xfs_warn(log->l_mp,
2165 "bad number of regions (%d) in inode log format",
2166 in_f->ilf_size);
2167 ASSERT(0);
2168 kmem_free(ptr);
2169 return -EFSCORRUPTED;
2170 }
2171
2172 item->ri_total = in_f->ilf_size;
2173 item->ri_buf =
2174 kmem_zalloc(item->ri_total * sizeof(xfs_log_iovec_t),
2175 0);
2176 }
2177
2178 if (item->ri_total <= item->ri_cnt) {
2179 xfs_warn(log->l_mp,
2180 "log item region count (%d) overflowed size (%d)",
2181 item->ri_cnt, item->ri_total);
2182 ASSERT(0);
2183 kmem_free(ptr);
2184 return -EFSCORRUPTED;
2185 }
2186
2187
2188 item->ri_buf[item->ri_cnt].i_addr = ptr;
2189 item->ri_buf[item->ri_cnt].i_len = len;
2190 item->ri_cnt++;
2191 trace_xfs_log_recover_item_add(log, trans, item, 0);
2192 return 0;
2193 }
2194
2195
2196
2197
2198
2199
2200 STATIC void
2201 xlog_recover_free_trans(
2202 struct xlog_recover *trans)
2203 {
2204 struct xlog_recover_item *item, *n;
2205 int i;
2206
2207 hlist_del_init(&trans->r_list);
2208
2209 list_for_each_entry_safe(item, n, &trans->r_itemq, ri_list) {
2210
2211 list_del(&item->ri_list);
2212 for (i = 0; i < item->ri_cnt; i++)
2213 kmem_free(item->ri_buf[i].i_addr);
2214
2215 kmem_free(item->ri_buf);
2216 kmem_free(item);
2217 }
2218
2219 kmem_free(trans);
2220 }
2221
2222
2223
2224
2225 STATIC int
2226 xlog_recovery_process_trans(
2227 struct xlog *log,
2228 struct xlog_recover *trans,
2229 char *dp,
2230 unsigned int len,
2231 unsigned int flags,
2232 int pass,
2233 struct list_head *buffer_list)
2234 {
2235 int error = 0;
2236 bool freeit = false;
2237
2238
2239 flags &= ~XLOG_END_TRANS;
2240 if (flags & XLOG_WAS_CONT_TRANS)
2241 flags &= ~XLOG_CONTINUE_TRANS;
2242
2243
2244
2245
2246
2247 switch (flags) {
2248
2249 case 0:
2250 case XLOG_CONTINUE_TRANS:
2251 error = xlog_recover_add_to_trans(log, trans, dp, len);
2252 break;
2253 case XLOG_WAS_CONT_TRANS:
2254 error = xlog_recover_add_to_cont_trans(log, trans, dp, len);
2255 break;
2256 case XLOG_COMMIT_TRANS:
2257 error = xlog_recover_commit_trans(log, trans, pass,
2258 buffer_list);
2259
2260 freeit = true;
2261 break;
2262
2263
2264 case XLOG_UNMOUNT_TRANS:
2265
2266 xfs_warn(log->l_mp, "%s: Unmount LR", __func__);
2267 freeit = true;
2268 break;
2269 case XLOG_START_TRANS:
2270 default:
2271 xfs_warn(log->l_mp, "%s: bad flag 0x%x", __func__, flags);
2272 ASSERT(0);
2273 error = -EFSCORRUPTED;
2274 break;
2275 }
2276 if (error || freeit)
2277 xlog_recover_free_trans(trans);
2278 return error;
2279 }
2280
2281
2282
2283
2284
2285
2286
2287
2288 STATIC struct xlog_recover *
2289 xlog_recover_ophdr_to_trans(
2290 struct hlist_head rhash[],
2291 struct xlog_rec_header *rhead,
2292 struct xlog_op_header *ohead)
2293 {
2294 struct xlog_recover *trans;
2295 xlog_tid_t tid;
2296 struct hlist_head *rhp;
2297
2298 tid = be32_to_cpu(ohead->oh_tid);
2299 rhp = &rhash[XLOG_RHASH(tid)];
2300 hlist_for_each_entry(trans, rhp, r_list) {
2301 if (trans->r_log_tid == tid)
2302 return trans;
2303 }
2304
2305
2306
2307
2308
2309 if (!(ohead->oh_flags & XLOG_START_TRANS))
2310 return NULL;
2311
2312 ASSERT(be32_to_cpu(ohead->oh_len) == 0);
2313
2314
2315
2316
2317
2318 trans = kmem_zalloc(sizeof(struct xlog_recover), 0);
2319 trans->r_log_tid = tid;
2320 trans->r_lsn = be64_to_cpu(rhead->h_lsn);
2321 INIT_LIST_HEAD(&trans->r_itemq);
2322 INIT_HLIST_NODE(&trans->r_list);
2323 hlist_add_head(&trans->r_list, rhp);
2324
2325
2326
2327
2328
2329 return NULL;
2330 }
2331
2332 STATIC int
2333 xlog_recover_process_ophdr(
2334 struct xlog *log,
2335 struct hlist_head rhash[],
2336 struct xlog_rec_header *rhead,
2337 struct xlog_op_header *ohead,
2338 char *dp,
2339 char *end,
2340 int pass,
2341 struct list_head *buffer_list)
2342 {
2343 struct xlog_recover *trans;
2344 unsigned int len;
2345 int error;
2346
2347
2348 if (ohead->oh_clientid != XFS_TRANSACTION &&
2349 ohead->oh_clientid != XFS_LOG) {
2350 xfs_warn(log->l_mp, "%s: bad clientid 0x%x",
2351 __func__, ohead->oh_clientid);
2352 ASSERT(0);
2353 return -EFSCORRUPTED;
2354 }
2355
2356
2357
2358
2359 len = be32_to_cpu(ohead->oh_len);
2360 if (dp + len > end) {
2361 xfs_warn(log->l_mp, "%s: bad length 0x%x", __func__, len);
2362 WARN_ON(1);
2363 return -EFSCORRUPTED;
2364 }
2365
2366 trans = xlog_recover_ophdr_to_trans(rhash, rhead, ohead);
2367 if (!trans) {
2368
2369 return 0;
2370 }
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395 if (log->l_recovery_lsn != trans->r_lsn &&
2396 ohead->oh_flags & XLOG_COMMIT_TRANS) {
2397 error = xfs_buf_delwri_submit(buffer_list);
2398 if (error)
2399 return error;
2400 log->l_recovery_lsn = trans->r_lsn;
2401 }
2402
2403 return xlog_recovery_process_trans(log, trans, dp, len,
2404 ohead->oh_flags, pass, buffer_list);
2405 }
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416 STATIC int
2417 xlog_recover_process_data(
2418 struct xlog *log,
2419 struct hlist_head rhash[],
2420 struct xlog_rec_header *rhead,
2421 char *dp,
2422 int pass,
2423 struct list_head *buffer_list)
2424 {
2425 struct xlog_op_header *ohead;
2426 char *end;
2427 int num_logops;
2428 int error;
2429
2430 end = dp + be32_to_cpu(rhead->h_len);
2431 num_logops = be32_to_cpu(rhead->h_num_logops);
2432
2433
2434 if (xlog_header_check_recover(log->l_mp, rhead))
2435 return -EIO;
2436
2437 trace_xfs_log_recover_record(log, rhead, pass);
2438 while ((dp < end) && num_logops) {
2439
2440 ohead = (struct xlog_op_header *)dp;
2441 dp += sizeof(*ohead);
2442 ASSERT(dp <= end);
2443
2444
2445 error = xlog_recover_process_ophdr(log, rhash, rhead, ohead,
2446 dp, end, pass, buffer_list);
2447 if (error)
2448 return error;
2449
2450 dp += be32_to_cpu(ohead->oh_len);
2451 num_logops--;
2452 }
2453 return 0;
2454 }
2455
2456
2457 static int
2458 xlog_finish_defer_ops(
2459 struct xfs_mount *mp,
2460 struct list_head *capture_list)
2461 {
2462 struct xfs_defer_capture *dfc, *next;
2463 struct xfs_trans *tp;
2464 int error = 0;
2465
2466 list_for_each_entry_safe(dfc, next, capture_list, dfc_list) {
2467 struct xfs_trans_res resv;
2468 struct xfs_defer_resources dres;
2469
2470
2471
2472
2473
2474
2475
2476 resv.tr_logres = dfc->dfc_logres;
2477 resv.tr_logcount = 1;
2478 resv.tr_logflags = XFS_TRANS_PERM_LOG_RES;
2479
2480 error = xfs_trans_alloc(mp, &resv, dfc->dfc_blkres,
2481 dfc->dfc_rtxres, XFS_TRANS_RESERVE, &tp);
2482 if (error) {
2483 xlog_force_shutdown(mp->m_log, SHUTDOWN_LOG_IO_ERROR);
2484 return error;
2485 }
2486
2487
2488
2489
2490
2491 list_del_init(&dfc->dfc_list);
2492 xfs_defer_ops_continue(dfc, tp, &dres);
2493 error = xfs_trans_commit(tp);
2494 xfs_defer_resources_rele(&dres);
2495 if (error)
2496 return error;
2497 }
2498
2499 ASSERT(list_empty(capture_list));
2500 return 0;
2501 }
2502
2503
2504 static void
2505 xlog_abort_defer_ops(
2506 struct xfs_mount *mp,
2507 struct list_head *capture_list)
2508 {
2509 struct xfs_defer_capture *dfc;
2510 struct xfs_defer_capture *next;
2511
2512 list_for_each_entry_safe(dfc, next, capture_list, dfc_list) {
2513 list_del_init(&dfc->dfc_list);
2514 xfs_defer_ops_capture_free(mp, dfc);
2515 }
2516 }
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534 STATIC int
2535 xlog_recover_process_intents(
2536 struct xlog *log)
2537 {
2538 LIST_HEAD(capture_list);
2539 struct xfs_ail_cursor cur;
2540 struct xfs_log_item *lip;
2541 struct xfs_ail *ailp;
2542 int error = 0;
2543 #if defined(DEBUG) || defined(XFS_WARN)
2544 xfs_lsn_t last_lsn;
2545 #endif
2546
2547 ailp = log->l_ailp;
2548 spin_lock(&ailp->ail_lock);
2549 #if defined(DEBUG) || defined(XFS_WARN)
2550 last_lsn = xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block);
2551 #endif
2552 for (lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
2553 lip != NULL;
2554 lip = xfs_trans_ail_cursor_next(ailp, &cur)) {
2555 if (!xlog_item_is_intent(lip))
2556 break;
2557
2558
2559
2560
2561
2562
2563 ASSERT(XFS_LSN_CMP(last_lsn, lip->li_lsn) >= 0);
2564
2565
2566
2567
2568
2569
2570
2571 spin_unlock(&ailp->ail_lock);
2572 error = lip->li_ops->iop_recover(lip, &capture_list);
2573 spin_lock(&ailp->ail_lock);
2574 if (error) {
2575 trace_xlog_intent_recovery_failed(log->l_mp, error,
2576 lip->li_ops->iop_recover);
2577 break;
2578 }
2579 }
2580
2581 xfs_trans_ail_cursor_done(&cur);
2582 spin_unlock(&ailp->ail_lock);
2583 if (error)
2584 goto err;
2585
2586 error = xlog_finish_defer_ops(log->l_mp, &capture_list);
2587 if (error)
2588 goto err;
2589
2590 return 0;
2591 err:
2592 xlog_abort_defer_ops(log->l_mp, &capture_list);
2593 return error;
2594 }
2595
2596
2597
2598
2599
2600
2601 STATIC void
2602 xlog_recover_cancel_intents(
2603 struct xlog *log)
2604 {
2605 struct xfs_log_item *lip;
2606 struct xfs_ail_cursor cur;
2607 struct xfs_ail *ailp;
2608
2609 ailp = log->l_ailp;
2610 spin_lock(&ailp->ail_lock);
2611 lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
2612 while (lip != NULL) {
2613 if (!xlog_item_is_intent(lip))
2614 break;
2615
2616 spin_unlock(&ailp->ail_lock);
2617 lip->li_ops->iop_release(lip);
2618 spin_lock(&ailp->ail_lock);
2619 lip = xfs_trans_ail_cursor_next(ailp, &cur);
2620 }
2621
2622 xfs_trans_ail_cursor_done(&cur);
2623 spin_unlock(&ailp->ail_lock);
2624 }
2625
2626
2627
2628
2629
2630 STATIC void
2631 xlog_recover_clear_agi_bucket(
2632 struct xfs_perag *pag,
2633 int bucket)
2634 {
2635 struct xfs_mount *mp = pag->pag_mount;
2636 struct xfs_trans *tp;
2637 struct xfs_agi *agi;
2638 struct xfs_buf *agibp;
2639 int offset;
2640 int error;
2641
2642 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_clearagi, 0, 0, 0, &tp);
2643 if (error)
2644 goto out_error;
2645
2646 error = xfs_read_agi(pag, tp, &agibp);
2647 if (error)
2648 goto out_abort;
2649
2650 agi = agibp->b_addr;
2651 agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
2652 offset = offsetof(xfs_agi_t, agi_unlinked) +
2653 (sizeof(xfs_agino_t) * bucket);
2654 xfs_trans_log_buf(tp, agibp, offset,
2655 (offset + sizeof(xfs_agino_t) - 1));
2656
2657 error = xfs_trans_commit(tp);
2658 if (error)
2659 goto out_error;
2660 return;
2661
2662 out_abort:
2663 xfs_trans_cancel(tp);
2664 out_error:
2665 xfs_warn(mp, "%s: failed to clear agi %d. Continuing.", __func__,
2666 pag->pag_agno);
2667 return;
2668 }
2669
2670 static int
2671 xlog_recover_iunlink_bucket(
2672 struct xfs_perag *pag,
2673 struct xfs_agi *agi,
2674 int bucket)
2675 {
2676 struct xfs_mount *mp = pag->pag_mount;
2677 struct xfs_inode *prev_ip = NULL;
2678 struct xfs_inode *ip;
2679 xfs_agino_t prev_agino, agino;
2680 int error = 0;
2681
2682 agino = be32_to_cpu(agi->agi_unlinked[bucket]);
2683 while (agino != NULLAGINO) {
2684 error = xfs_iget(mp, NULL,
2685 XFS_AGINO_TO_INO(mp, pag->pag_agno, agino),
2686 0, 0, &ip);
2687 if (error)
2688 break;
2689
2690 ASSERT(VFS_I(ip)->i_nlink == 0);
2691 ASSERT(VFS_I(ip)->i_mode != 0);
2692 xfs_iflags_clear(ip, XFS_IRECOVERY);
2693 agino = ip->i_next_unlinked;
2694
2695 if (prev_ip) {
2696 ip->i_prev_unlinked = prev_agino;
2697 xfs_irele(prev_ip);
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708 xfs_inodegc_flush(mp);
2709 }
2710
2711 prev_agino = agino;
2712 prev_ip = ip;
2713 }
2714
2715 if (prev_ip) {
2716 ip->i_prev_unlinked = prev_agino;
2717 xfs_irele(prev_ip);
2718 }
2719 xfs_inodegc_flush(mp);
2720 return error;
2721 }
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746 static void
2747 xlog_recover_iunlink_ag(
2748 struct xfs_perag *pag)
2749 {
2750 struct xfs_agi *agi;
2751 struct xfs_buf *agibp;
2752 int bucket;
2753 int error;
2754
2755 error = xfs_read_agi(pag, NULL, &agibp);
2756 if (error) {
2757
2758
2759
2760
2761
2762
2763 return;
2764 }
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774 agi = agibp->b_addr;
2775 xfs_buf_unlock(agibp);
2776
2777 for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++) {
2778 error = xlog_recover_iunlink_bucket(pag, agi, bucket);
2779 if (error) {
2780
2781
2782
2783
2784
2785
2786 xfs_inodegc_flush(pag->pag_mount);
2787 xlog_recover_clear_agi_bucket(pag, bucket);
2788 }
2789 }
2790
2791 xfs_buf_rele(agibp);
2792 }
2793
2794 static void
2795 xlog_recover_process_iunlinks(
2796 struct xlog *log)
2797 {
2798 struct xfs_perag *pag;
2799 xfs_agnumber_t agno;
2800
2801 for_each_perag(log->l_mp, agno, pag)
2802 xlog_recover_iunlink_ag(pag);
2803
2804
2805
2806
2807
2808
2809 xfs_inodegc_flush(log->l_mp);
2810 }
2811
2812 STATIC void
2813 xlog_unpack_data(
2814 struct xlog_rec_header *rhead,
2815 char *dp,
2816 struct xlog *log)
2817 {
2818 int i, j, k;
2819
2820 for (i = 0; i < BTOBB(be32_to_cpu(rhead->h_len)) &&
2821 i < (XLOG_HEADER_CYCLE_SIZE / BBSIZE); i++) {
2822 *(__be32 *)dp = *(__be32 *)&rhead->h_cycle_data[i];
2823 dp += BBSIZE;
2824 }
2825
2826 if (xfs_has_logv2(log->l_mp)) {
2827 xlog_in_core_2_t *xhdr = (xlog_in_core_2_t *)rhead;
2828 for ( ; i < BTOBB(be32_to_cpu(rhead->h_len)); i++) {
2829 j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
2830 k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
2831 *(__be32 *)dp = xhdr[j].hic_xheader.xh_cycle_data[k];
2832 dp += BBSIZE;
2833 }
2834 }
2835 }
2836
2837
2838
2839
2840 STATIC int
2841 xlog_recover_process(
2842 struct xlog *log,
2843 struct hlist_head rhash[],
2844 struct xlog_rec_header *rhead,
2845 char *dp,
2846 int pass,
2847 struct list_head *buffer_list)
2848 {
2849 __le32 old_crc = rhead->h_crc;
2850 __le32 crc;
2851
2852 crc = xlog_cksum(log, rhead, dp, be32_to_cpu(rhead->h_len));
2853
2854
2855
2856
2857
2858
2859
2860
2861 if (pass == XLOG_RECOVER_CRCPASS) {
2862 if (old_crc && crc != old_crc)
2863 return -EFSBADCRC;
2864 return 0;
2865 }
2866
2867
2868
2869
2870
2871
2872
2873 if (crc != old_crc) {
2874 if (old_crc || xfs_has_crc(log->l_mp)) {
2875 xfs_alert(log->l_mp,
2876 "log record CRC mismatch: found 0x%x, expected 0x%x.",
2877 le32_to_cpu(old_crc),
2878 le32_to_cpu(crc));
2879 xfs_hex_dump(dp, 32);
2880 }
2881
2882
2883
2884
2885
2886 if (xfs_has_crc(log->l_mp)) {
2887 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, log->l_mp);
2888 return -EFSCORRUPTED;
2889 }
2890 }
2891
2892 xlog_unpack_data(rhead, dp, log);
2893
2894 return xlog_recover_process_data(log, rhash, rhead, dp, pass,
2895 buffer_list);
2896 }
2897
2898 STATIC int
2899 xlog_valid_rec_header(
2900 struct xlog *log,
2901 struct xlog_rec_header *rhead,
2902 xfs_daddr_t blkno,
2903 int bufsize)
2904 {
2905 int hlen;
2906
2907 if (XFS_IS_CORRUPT(log->l_mp,
2908 rhead->h_magicno != cpu_to_be32(XLOG_HEADER_MAGIC_NUM)))
2909 return -EFSCORRUPTED;
2910 if (XFS_IS_CORRUPT(log->l_mp,
2911 (!rhead->h_version ||
2912 (be32_to_cpu(rhead->h_version) &
2913 (~XLOG_VERSION_OKBITS))))) {
2914 xfs_warn(log->l_mp, "%s: unrecognised log version (%d).",
2915 __func__, be32_to_cpu(rhead->h_version));
2916 return -EFSCORRUPTED;
2917 }
2918
2919
2920
2921
2922
2923 hlen = be32_to_cpu(rhead->h_len);
2924 if (XFS_IS_CORRUPT(log->l_mp, hlen <= 0 || hlen > bufsize))
2925 return -EFSCORRUPTED;
2926
2927 if (XFS_IS_CORRUPT(log->l_mp,
2928 blkno > log->l_logBBsize || blkno > INT_MAX))
2929 return -EFSCORRUPTED;
2930 return 0;
2931 }
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941 STATIC int
2942 xlog_do_recovery_pass(
2943 struct xlog *log,
2944 xfs_daddr_t head_blk,
2945 xfs_daddr_t tail_blk,
2946 int pass,
2947 xfs_daddr_t *first_bad)
2948 {
2949 xlog_rec_header_t *rhead;
2950 xfs_daddr_t blk_no, rblk_no;
2951 xfs_daddr_t rhead_blk;
2952 char *offset;
2953 char *hbp, *dbp;
2954 int error = 0, h_size, h_len;
2955 int error2 = 0;
2956 int bblks, split_bblks;
2957 int hblks, split_hblks, wrapped_hblks;
2958 int i;
2959 struct hlist_head rhash[XLOG_RHASH_SIZE];
2960 LIST_HEAD (buffer_list);
2961
2962 ASSERT(head_blk != tail_blk);
2963 blk_no = rhead_blk = tail_blk;
2964
2965 for (i = 0; i < XLOG_RHASH_SIZE; i++)
2966 INIT_HLIST_HEAD(&rhash[i]);
2967
2968
2969
2970
2971
2972 if (xfs_has_logv2(log->l_mp)) {
2973
2974
2975
2976
2977
2978 hbp = xlog_alloc_buffer(log, 1);
2979 if (!hbp)
2980 return -ENOMEM;
2981
2982 error = xlog_bread(log, tail_blk, 1, hbp, &offset);
2983 if (error)
2984 goto bread_err1;
2985
2986 rhead = (xlog_rec_header_t *)offset;
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999 h_size = be32_to_cpu(rhead->h_size);
3000 h_len = be32_to_cpu(rhead->h_len);
3001 if (h_len > h_size && h_len <= log->l_mp->m_logbsize &&
3002 rhead->h_num_logops == cpu_to_be32(1)) {
3003 xfs_warn(log->l_mp,
3004 "invalid iclog size (%d bytes), using lsunit (%d bytes)",
3005 h_size, log->l_mp->m_logbsize);
3006 h_size = log->l_mp->m_logbsize;
3007 }
3008
3009 error = xlog_valid_rec_header(log, rhead, tail_blk, h_size);
3010 if (error)
3011 goto bread_err1;
3012
3013 hblks = xlog_logrec_hblks(log, rhead);
3014 if (hblks != 1) {
3015 kmem_free(hbp);
3016 hbp = xlog_alloc_buffer(log, hblks);
3017 }
3018 } else {
3019 ASSERT(log->l_sectBBsize == 1);
3020 hblks = 1;
3021 hbp = xlog_alloc_buffer(log, 1);
3022 h_size = XLOG_BIG_RECORD_BSIZE;
3023 }
3024
3025 if (!hbp)
3026 return -ENOMEM;
3027 dbp = xlog_alloc_buffer(log, BTOBB(h_size));
3028 if (!dbp) {
3029 kmem_free(hbp);
3030 return -ENOMEM;
3031 }
3032
3033 memset(rhash, 0, sizeof(rhash));
3034 if (tail_blk > head_blk) {
3035
3036
3037
3038
3039
3040 while (blk_no < log->l_logBBsize) {
3041
3042
3043
3044 offset = hbp;
3045 split_hblks = 0;
3046 wrapped_hblks = 0;
3047 if (blk_no + hblks <= log->l_logBBsize) {
3048
3049 error = xlog_bread(log, blk_no, hblks, hbp,
3050 &offset);
3051 if (error)
3052 goto bread_err2;
3053 } else {
3054
3055 if (blk_no != log->l_logBBsize) {
3056
3057 ASSERT(blk_no <= INT_MAX);
3058 split_hblks = log->l_logBBsize - (int)blk_no;
3059 ASSERT(split_hblks > 0);
3060 error = xlog_bread(log, blk_no,
3061 split_hblks, hbp,
3062 &offset);
3063 if (error)
3064 goto bread_err2;
3065 }
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079 wrapped_hblks = hblks - split_hblks;
3080 error = xlog_bread_noalign(log, 0,
3081 wrapped_hblks,
3082 offset + BBTOB(split_hblks));
3083 if (error)
3084 goto bread_err2;
3085 }
3086 rhead = (xlog_rec_header_t *)offset;
3087 error = xlog_valid_rec_header(log, rhead,
3088 split_hblks ? blk_no : 0, h_size);
3089 if (error)
3090 goto bread_err2;
3091
3092 bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
3093 blk_no += hblks;
3094
3095
3096
3097
3098
3099
3100
3101
3102 if (blk_no + bblks <= log->l_logBBsize ||
3103 blk_no >= log->l_logBBsize) {
3104 rblk_no = xlog_wrap_logbno(log, blk_no);
3105 error = xlog_bread(log, rblk_no, bblks, dbp,
3106 &offset);
3107 if (error)
3108 goto bread_err2;
3109 } else {
3110
3111
3112 offset = dbp;
3113 split_bblks = 0;
3114 if (blk_no != log->l_logBBsize) {
3115
3116
3117 ASSERT(!wrapped_hblks);
3118 ASSERT(blk_no <= INT_MAX);
3119 split_bblks =
3120 log->l_logBBsize - (int)blk_no;
3121 ASSERT(split_bblks > 0);
3122 error = xlog_bread(log, blk_no,
3123 split_bblks, dbp,
3124 &offset);
3125 if (error)
3126 goto bread_err2;
3127 }
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141 error = xlog_bread_noalign(log, 0,
3142 bblks - split_bblks,
3143 offset + BBTOB(split_bblks));
3144 if (error)
3145 goto bread_err2;
3146 }
3147
3148 error = xlog_recover_process(log, rhash, rhead, offset,
3149 pass, &buffer_list);
3150 if (error)
3151 goto bread_err2;
3152
3153 blk_no += bblks;
3154 rhead_blk = blk_no;
3155 }
3156
3157 ASSERT(blk_no >= log->l_logBBsize);
3158 blk_no -= log->l_logBBsize;
3159 rhead_blk = blk_no;
3160 }
3161
3162
3163 while (blk_no < head_blk) {
3164 error = xlog_bread(log, blk_no, hblks, hbp, &offset);
3165 if (error)
3166 goto bread_err2;
3167
3168 rhead = (xlog_rec_header_t *)offset;
3169 error = xlog_valid_rec_header(log, rhead, blk_no, h_size);
3170 if (error)
3171 goto bread_err2;
3172
3173
3174 bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
3175 error = xlog_bread(log, blk_no+hblks, bblks, dbp,
3176 &offset);
3177 if (error)
3178 goto bread_err2;
3179
3180 error = xlog_recover_process(log, rhash, rhead, offset, pass,
3181 &buffer_list);
3182 if (error)
3183 goto bread_err2;
3184
3185 blk_no += bblks + hblks;
3186 rhead_blk = blk_no;
3187 }
3188
3189 bread_err2:
3190 kmem_free(dbp);
3191 bread_err1:
3192 kmem_free(hbp);
3193
3194
3195
3196
3197
3198 if (!list_empty(&buffer_list))
3199 error2 = xfs_buf_delwri_submit(&buffer_list);
3200
3201 if (error && first_bad)
3202 *first_bad = rhead_blk;
3203
3204
3205
3206
3207
3208
3209 for (i = 0; i < XLOG_RHASH_SIZE; i++) {
3210 struct hlist_node *tmp;
3211 struct xlog_recover *trans;
3212
3213 hlist_for_each_entry_safe(trans, tmp, &rhash[i], r_list)
3214 xlog_recover_free_trans(trans);
3215 }
3216
3217 return error ? error : error2;
3218 }
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233 STATIC int
3234 xlog_do_log_recovery(
3235 struct xlog *log,
3236 xfs_daddr_t head_blk,
3237 xfs_daddr_t tail_blk)
3238 {
3239 int error;
3240
3241 ASSERT(head_blk != tail_blk);
3242
3243
3244
3245
3246
3247 error = xlog_alloc_buf_cancel_table(log);
3248 if (error)
3249 return error;
3250
3251 error = xlog_do_recovery_pass(log, head_blk, tail_blk,
3252 XLOG_RECOVER_PASS1, NULL);
3253 if (error != 0)
3254 goto out_cancel;
3255
3256
3257
3258
3259
3260 error = xlog_do_recovery_pass(log, head_blk, tail_blk,
3261 XLOG_RECOVER_PASS2, NULL);
3262 if (!error)
3263 xlog_check_buf_cancel_table(log);
3264 out_cancel:
3265 xlog_free_buf_cancel_table(log);
3266 return error;
3267 }
3268
3269
3270
3271
3272 STATIC int
3273 xlog_do_recover(
3274 struct xlog *log,
3275 xfs_daddr_t head_blk,
3276 xfs_daddr_t tail_blk)
3277 {
3278 struct xfs_mount *mp = log->l_mp;
3279 struct xfs_buf *bp = mp->m_sb_bp;
3280 struct xfs_sb *sbp = &mp->m_sb;
3281 int error;
3282
3283 trace_xfs_log_recover(log, head_blk, tail_blk);
3284
3285
3286
3287
3288 error = xlog_do_log_recovery(log, head_blk, tail_blk);
3289 if (error)
3290 return error;
3291
3292 if (xlog_is_shutdown(log))
3293 return -EIO;
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304 xlog_assign_tail_lsn(mp);
3305
3306
3307
3308
3309
3310 xfs_buf_lock(bp);
3311 xfs_buf_hold(bp);
3312 error = _xfs_buf_read(bp, XBF_READ);
3313 if (error) {
3314 if (!xlog_is_shutdown(log)) {
3315 xfs_buf_ioerror_alert(bp, __this_address);
3316 ASSERT(0);
3317 }
3318 xfs_buf_relse(bp);
3319 return error;
3320 }
3321
3322
3323 xfs_sb_from_disk(sbp, bp->b_addr);
3324 xfs_buf_relse(bp);
3325
3326
3327 mp->m_features |= xfs_sb_version_to_features(sbp);
3328 xfs_reinit_percpu_counters(mp);
3329 error = xfs_initialize_perag(mp, sbp->sb_agcount, sbp->sb_dblocks,
3330 &mp->m_maxagi);
3331 if (error) {
3332 xfs_warn(mp, "Failed post-recovery per-ag init: %d", error);
3333 return error;
3334 }
3335 mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
3336
3337
3338 clear_bit(XLOG_ACTIVE_RECOVERY, &log->l_opstate);
3339 return 0;
3340 }
3341
3342
3343
3344
3345
3346
3347 int
3348 xlog_recover(
3349 struct xlog *log)
3350 {
3351 xfs_daddr_t head_blk, tail_blk;
3352 int error;
3353
3354
3355 error = xlog_find_tail(log, &head_blk, &tail_blk);
3356 if (error)
3357 return error;
3358
3359
3360
3361
3362
3363
3364 if (xfs_has_crc(log->l_mp) &&
3365 !xfs_log_check_lsn(log->l_mp, log->l_mp->m_sb.sb_lsn))
3366 return -EINVAL;
3367
3368 if (tail_blk != head_blk) {
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380 if ((error = xfs_dev_is_read_only(log->l_mp, "recovery"))) {
3381 return error;
3382 }
3383
3384
3385
3386
3387
3388
3389
3390
3391 if (xfs_sb_is_v5(&log->l_mp->m_sb) &&
3392 xfs_sb_has_incompat_log_feature(&log->l_mp->m_sb,
3393 XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN)) {
3394 xfs_warn(log->l_mp,
3395 "Superblock has unknown incompatible log features (0x%x) enabled.",
3396 (log->l_mp->m_sb.sb_features_log_incompat &
3397 XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN));
3398 xfs_warn(log->l_mp,
3399 "The log can not be fully and/or safely recovered by this kernel.");
3400 xfs_warn(log->l_mp,
3401 "Please recover the log on a kernel that supports the unknown features.");
3402 return -EINVAL;
3403 }
3404
3405
3406
3407
3408
3409
3410 if (xfs_globals.log_recovery_delay) {
3411 xfs_notice(log->l_mp,
3412 "Delaying log recovery for %d seconds.",
3413 xfs_globals.log_recovery_delay);
3414 msleep(xfs_globals.log_recovery_delay * 1000);
3415 }
3416
3417 xfs_notice(log->l_mp, "Starting recovery (logdev: %s)",
3418 log->l_mp->m_logname ? log->l_mp->m_logname
3419 : "internal");
3420
3421 error = xlog_do_recover(log, head_blk, tail_blk);
3422 set_bit(XLOG_RECOVERY_NEEDED, &log->l_opstate);
3423 }
3424 return error;
3425 }
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435 int
3436 xlog_recover_finish(
3437 struct xlog *log)
3438 {
3439 int error;
3440
3441 error = xlog_recover_process_intents(log);
3442 if (error) {
3443
3444
3445
3446
3447
3448
3449
3450 xlog_recover_cancel_intents(log);
3451 xfs_alert(log->l_mp, "Failed to recover intents");
3452 xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR);
3453 return error;
3454 }
3455
3456
3457
3458
3459
3460
3461 xfs_log_force(log->l_mp, XFS_LOG_SYNC);
3462
3463
3464
3465
3466
3467
3468
3469 if (xfs_clear_incompat_log_features(log->l_mp)) {
3470 error = xfs_sync_sb(log->l_mp, false);
3471 if (error < 0) {
3472 xfs_alert(log->l_mp,
3473 "Failed to clear log incompat features on recovery");
3474 return error;
3475 }
3476 }
3477
3478 xlog_recover_process_iunlinks(log);
3479
3480
3481
3482
3483
3484
3485
3486
3487 error = xfs_reflink_recover_cow(log->l_mp);
3488 if (error) {
3489 xfs_alert(log->l_mp,
3490 "Failed to recover leftover CoW staging extents, err %d.",
3491 error);
3492
3493
3494
3495
3496
3497
3498 xlog_force_shutdown(log, SHUTDOWN_LOG_IO_ERROR);
3499 }
3500
3501 return 0;
3502 }
3503
3504 void
3505 xlog_recover_cancel(
3506 struct xlog *log)
3507 {
3508 if (xlog_recovery_needed(log))
3509 xlog_recover_cancel_intents(log);
3510 }
3511