Ryusuke Konishi | ae98043 | 2018-09-04 15:46:30 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 2 | /* |
Ryusuke Konishi | 94ee1d9 | 2021-11-08 18:35:01 -0800 | [diff] [blame] | 3 | * NILFS recovery logic |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation. |
| 6 | * |
Ryusuke Konishi | 4b420ab | 2016-05-23 16:23:09 -0700 | [diff] [blame] | 7 | * Written by Ryusuke Konishi. |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/buffer_head.h> |
| 11 | #include <linux/blkdev.h> |
| 12 | #include <linux/swap.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 14 | #include <linux/crc32.h> |
| 15 | #include "nilfs.h" |
| 16 | #include "segment.h" |
| 17 | #include "sufile.h" |
| 18 | #include "page.h" |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 19 | #include "segbuf.h" |
| 20 | |
| 21 | /* |
| 22 | * Segment check result |
| 23 | */ |
| 24 | enum { |
| 25 | NILFS_SEG_VALID, |
| 26 | NILFS_SEG_NO_SUPER_ROOT, |
| 27 | NILFS_SEG_FAIL_IO, |
| 28 | NILFS_SEG_FAIL_MAGIC, |
| 29 | NILFS_SEG_FAIL_SEQ, |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 30 | NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT, |
| 31 | NILFS_SEG_FAIL_CHECKSUM_FULL, |
| 32 | NILFS_SEG_FAIL_CONSISTENCY, |
| 33 | }; |
| 34 | |
| 35 | /* work structure for recovery */ |
| 36 | struct nilfs_recovery_block { |
Ryusuke Konishi | 076a378 | 2016-05-23 16:23:48 -0700 | [diff] [blame] | 37 | ino_t ino; /* |
| 38 | * Inode number of the file that this block |
| 39 | * belongs to |
| 40 | */ |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 41 | sector_t blocknr; /* block number */ |
| 42 | __u64 vblocknr; /* virtual block number */ |
| 43 | unsigned long blkoff; /* File offset of the data block (per block) */ |
| 44 | struct list_head list; |
| 45 | }; |
| 46 | |
| 47 | |
Ryusuke Konishi | feee880 | 2016-08-02 14:05:10 -0700 | [diff] [blame] | 48 | static int nilfs_warn_segment_error(struct super_block *sb, int err) |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 49 | { |
Ryusuke Konishi | feee880 | 2016-08-02 14:05:10 -0700 | [diff] [blame] | 50 | const char *msg = NULL; |
| 51 | |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 52 | switch (err) { |
| 53 | case NILFS_SEG_FAIL_IO: |
Joe Perches | a1d0747 | 2020-08-11 18:35:49 -0700 | [diff] [blame] | 54 | nilfs_err(sb, "I/O error reading segment"); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 55 | return -EIO; |
| 56 | case NILFS_SEG_FAIL_MAGIC: |
Ryusuke Konishi | feee880 | 2016-08-02 14:05:10 -0700 | [diff] [blame] | 57 | msg = "Magic number mismatch"; |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 58 | break; |
| 59 | case NILFS_SEG_FAIL_SEQ: |
Ryusuke Konishi | feee880 | 2016-08-02 14:05:10 -0700 | [diff] [blame] | 60 | msg = "Sequence number mismatch"; |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 61 | break; |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 62 | case NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT: |
Ryusuke Konishi | feee880 | 2016-08-02 14:05:10 -0700 | [diff] [blame] | 63 | msg = "Checksum error in super root"; |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 64 | break; |
| 65 | case NILFS_SEG_FAIL_CHECKSUM_FULL: |
Ryusuke Konishi | feee880 | 2016-08-02 14:05:10 -0700 | [diff] [blame] | 66 | msg = "Checksum error in segment payload"; |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 67 | break; |
| 68 | case NILFS_SEG_FAIL_CONSISTENCY: |
Ryusuke Konishi | feee880 | 2016-08-02 14:05:10 -0700 | [diff] [blame] | 69 | msg = "Inconsistency found"; |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 70 | break; |
| 71 | case NILFS_SEG_NO_SUPER_ROOT: |
Ryusuke Konishi | feee880 | 2016-08-02 14:05:10 -0700 | [diff] [blame] | 72 | msg = "No super root in the last segment"; |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 73 | break; |
Ryusuke Konishi | feee880 | 2016-08-02 14:05:10 -0700 | [diff] [blame] | 74 | default: |
Joe Perches | a1d0747 | 2020-08-11 18:35:49 -0700 | [diff] [blame] | 75 | nilfs_err(sb, "unrecognized segment error %d", err); |
Ryusuke Konishi | feee880 | 2016-08-02 14:05:10 -0700 | [diff] [blame] | 76 | return -EINVAL; |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 77 | } |
Joe Perches | a1d0747 | 2020-08-11 18:35:49 -0700 | [diff] [blame] | 78 | nilfs_warn(sb, "invalid segment: %s", msg); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 79 | return -EINVAL; |
| 80 | } |
| 81 | |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 82 | /** |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 83 | * nilfs_compute_checksum - compute checksum of blocks continuously |
| 84 | * @nilfs: nilfs object |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 85 | * @bhs: buffer head of start block |
| 86 | * @sum: place to store result |
| 87 | * @offset: offset bytes in the first block |
| 88 | * @check_bytes: number of bytes to be checked |
| 89 | * @start: DBN of start block |
| 90 | * @nblock: number of blocks to be checked |
| 91 | */ |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 92 | static int nilfs_compute_checksum(struct the_nilfs *nilfs, |
| 93 | struct buffer_head *bhs, u32 *sum, |
| 94 | unsigned long offset, u64 check_bytes, |
| 95 | sector_t start, unsigned long nblock) |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 96 | { |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 97 | unsigned int blocksize = nilfs->ns_blocksize; |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 98 | unsigned long size; |
| 99 | u32 crc; |
| 100 | |
| 101 | BUG_ON(offset >= blocksize); |
| 102 | check_bytes -= offset; |
| 103 | size = min_t(u64, check_bytes, blocksize - offset); |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 104 | crc = crc32_le(nilfs->ns_crc_seed, |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 105 | (unsigned char *)bhs->b_data + offset, size); |
| 106 | if (--nblock > 0) { |
| 107 | do { |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 108 | struct buffer_head *bh; |
| 109 | |
| 110 | bh = __bread(nilfs->ns_bdev, ++start, blocksize); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 111 | if (!bh) |
| 112 | return -EIO; |
| 113 | check_bytes -= size; |
| 114 | size = min_t(u64, check_bytes, blocksize); |
| 115 | crc = crc32_le(crc, bh->b_data, size); |
| 116 | brelse(bh); |
| 117 | } while (--nblock > 0); |
| 118 | } |
| 119 | *sum = crc; |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * nilfs_read_super_root_block - read super root block |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 125 | * @nilfs: nilfs object |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 126 | * @sr_block: disk block number of the super root block |
| 127 | * @pbh: address of a buffer_head pointer to return super root buffer |
| 128 | * @check: CRC check flag |
| 129 | */ |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 130 | int nilfs_read_super_root_block(struct the_nilfs *nilfs, sector_t sr_block, |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 131 | struct buffer_head **pbh, int check) |
| 132 | { |
| 133 | struct buffer_head *bh_sr; |
| 134 | struct nilfs_super_root *sr; |
| 135 | u32 crc; |
| 136 | int ret; |
| 137 | |
| 138 | *pbh = NULL; |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 139 | bh_sr = __bread(nilfs->ns_bdev, sr_block, nilfs->ns_blocksize); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 140 | if (unlikely(!bh_sr)) { |
| 141 | ret = NILFS_SEG_FAIL_IO; |
| 142 | goto failed; |
| 143 | } |
| 144 | |
| 145 | sr = (struct nilfs_super_root *)bh_sr->b_data; |
| 146 | if (check) { |
Ryusuke Konishi | 0c6c44c | 2016-05-23 16:23:39 -0700 | [diff] [blame] | 147 | unsigned int bytes = le16_to_cpu(sr->sr_bytes); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 148 | |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 149 | if (bytes == 0 || bytes > nilfs->ns_blocksize) { |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 150 | ret = NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT; |
| 151 | goto failed_bh; |
| 152 | } |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 153 | if (nilfs_compute_checksum( |
| 154 | nilfs, bh_sr, &crc, sizeof(sr->sr_sum), bytes, |
| 155 | sr_block, 1)) { |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 156 | ret = NILFS_SEG_FAIL_IO; |
| 157 | goto failed_bh; |
| 158 | } |
| 159 | if (crc != le32_to_cpu(sr->sr_sum)) { |
| 160 | ret = NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT; |
| 161 | goto failed_bh; |
| 162 | } |
| 163 | } |
| 164 | *pbh = bh_sr; |
| 165 | return 0; |
| 166 | |
| 167 | failed_bh: |
| 168 | brelse(bh_sr); |
| 169 | |
| 170 | failed: |
Ryusuke Konishi | feee880 | 2016-08-02 14:05:10 -0700 | [diff] [blame] | 171 | return nilfs_warn_segment_error(nilfs->ns_sb, ret); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
Ryusuke Konishi | 354fa8b | 2010-05-23 19:21:49 +0900 | [diff] [blame] | 175 | * nilfs_read_log_header - read summary header of the specified log |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 176 | * @nilfs: nilfs object |
Ryusuke Konishi | 354fa8b | 2010-05-23 19:21:49 +0900 | [diff] [blame] | 177 | * @start_blocknr: start block number of the log |
| 178 | * @sum: pointer to return segment summary structure |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 179 | */ |
Ryusuke Konishi | 354fa8b | 2010-05-23 19:21:49 +0900 | [diff] [blame] | 180 | static struct buffer_head * |
| 181 | nilfs_read_log_header(struct the_nilfs *nilfs, sector_t start_blocknr, |
| 182 | struct nilfs_segment_summary **sum) |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 183 | { |
| 184 | struct buffer_head *bh_sum; |
Ryusuke Konishi | 354fa8b | 2010-05-23 19:21:49 +0900 | [diff] [blame] | 185 | |
| 186 | bh_sum = __bread(nilfs->ns_bdev, start_blocknr, nilfs->ns_blocksize); |
| 187 | if (bh_sum) |
| 188 | *sum = (struct nilfs_segment_summary *)bh_sum->b_data; |
| 189 | return bh_sum; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * nilfs_validate_log - verify consistency of log |
| 194 | * @nilfs: nilfs object |
| 195 | * @seg_seq: sequence number of segment |
| 196 | * @bh_sum: buffer head of summary block |
| 197 | * @sum: segment summary struct |
| 198 | */ |
| 199 | static int nilfs_validate_log(struct the_nilfs *nilfs, u64 seg_seq, |
| 200 | struct buffer_head *bh_sum, |
| 201 | struct nilfs_segment_summary *sum) |
| 202 | { |
Jiro SEKIBA | 03f2936 | 2010-02-18 19:11:35 +0900 | [diff] [blame] | 203 | unsigned long nblock; |
| 204 | u32 crc; |
Ryusuke Konishi | 354fa8b | 2010-05-23 19:21:49 +0900 | [diff] [blame] | 205 | int ret; |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 206 | |
Ryusuke Konishi | 354fa8b | 2010-05-23 19:21:49 +0900 | [diff] [blame] | 207 | ret = NILFS_SEG_FAIL_MAGIC; |
| 208 | if (le32_to_cpu(sum->ss_magic) != NILFS_SEGSUM_MAGIC) |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 209 | goto out; |
| 210 | |
Ryusuke Konishi | 354fa8b | 2010-05-23 19:21:49 +0900 | [diff] [blame] | 211 | ret = NILFS_SEG_FAIL_SEQ; |
| 212 | if (le64_to_cpu(sum->ss_seq) != seg_seq) |
| 213 | goto out; |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 214 | |
Ryusuke Konishi | 354fa8b | 2010-05-23 19:21:49 +0900 | [diff] [blame] | 215 | nblock = le32_to_cpu(sum->ss_nblocks); |
| 216 | ret = NILFS_SEG_FAIL_CONSISTENCY; |
| 217 | if (unlikely(nblock == 0 || nblock > nilfs->ns_blocks_per_segment)) |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 218 | /* This limits the number of blocks read in the CRC check */ |
Ryusuke Konishi | 354fa8b | 2010-05-23 19:21:49 +0900 | [diff] [blame] | 219 | goto out; |
| 220 | |
| 221 | ret = NILFS_SEG_FAIL_IO; |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 222 | if (nilfs_compute_checksum(nilfs, bh_sum, &crc, sizeof(sum->ss_datasum), |
| 223 | ((u64)nblock << nilfs->ns_blocksize_bits), |
Ryusuke Konishi | 354fa8b | 2010-05-23 19:21:49 +0900 | [diff] [blame] | 224 | bh_sum->b_blocknr, nblock)) |
| 225 | goto out; |
| 226 | |
| 227 | ret = NILFS_SEG_FAIL_CHECKSUM_FULL; |
| 228 | if (crc != le32_to_cpu(sum->ss_datasum)) |
| 229 | goto out; |
| 230 | ret = 0; |
| 231 | out: |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 232 | return ret; |
| 233 | } |
| 234 | |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 235 | /** |
| 236 | * nilfs_read_summary_info - read an item on summary blocks of a log |
| 237 | * @nilfs: nilfs object |
| 238 | * @pbh: the current buffer head on summary blocks [in, out] |
| 239 | * @offset: the current byte offset on summary blocks [in, out] |
| 240 | * @bytes: byte size of the item to be read |
| 241 | */ |
| 242 | static void *nilfs_read_summary_info(struct the_nilfs *nilfs, |
| 243 | struct buffer_head **pbh, |
| 244 | unsigned int *offset, unsigned int bytes) |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 245 | { |
| 246 | void *ptr; |
| 247 | sector_t blocknr; |
| 248 | |
| 249 | BUG_ON((*pbh)->b_size < *offset); |
| 250 | if (bytes > (*pbh)->b_size - *offset) { |
| 251 | blocknr = (*pbh)->b_blocknr; |
| 252 | brelse(*pbh); |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 253 | *pbh = __bread(nilfs->ns_bdev, blocknr + 1, |
| 254 | nilfs->ns_blocksize); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 255 | if (unlikely(!*pbh)) |
| 256 | return NULL; |
| 257 | *offset = 0; |
| 258 | } |
| 259 | ptr = (*pbh)->b_data + *offset; |
| 260 | *offset += bytes; |
| 261 | return ptr; |
| 262 | } |
| 263 | |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 264 | /** |
| 265 | * nilfs_skip_summary_info - skip items on summary blocks of a log |
| 266 | * @nilfs: nilfs object |
| 267 | * @pbh: the current buffer head on summary blocks [in, out] |
| 268 | * @offset: the current byte offset on summary blocks [in, out] |
| 269 | * @bytes: byte size of the item to be skipped |
| 270 | * @count: number of items to be skipped |
| 271 | */ |
| 272 | static void nilfs_skip_summary_info(struct the_nilfs *nilfs, |
| 273 | struct buffer_head **pbh, |
| 274 | unsigned int *offset, unsigned int bytes, |
| 275 | unsigned long count) |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 276 | { |
| 277 | unsigned int rest_item_in_current_block |
| 278 | = ((*pbh)->b_size - *offset) / bytes; |
| 279 | |
| 280 | if (count <= rest_item_in_current_block) { |
| 281 | *offset += bytes * count; |
| 282 | } else { |
| 283 | sector_t blocknr = (*pbh)->b_blocknr; |
| 284 | unsigned int nitem_per_block = (*pbh)->b_size / bytes; |
| 285 | unsigned int bcnt; |
| 286 | |
| 287 | count -= rest_item_in_current_block; |
| 288 | bcnt = DIV_ROUND_UP(count, nitem_per_block); |
| 289 | *offset = bytes * (count - (bcnt - 1) * nitem_per_block); |
| 290 | |
| 291 | brelse(*pbh); |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 292 | *pbh = __bread(nilfs->ns_bdev, blocknr + bcnt, |
| 293 | nilfs->ns_blocksize); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 297 | /** |
| 298 | * nilfs_scan_dsync_log - get block information of a log written for data sync |
| 299 | * @nilfs: nilfs object |
| 300 | * @start_blocknr: start block number of the log |
Ryusuke Konishi | 8565548 | 2010-05-23 19:46:44 +0900 | [diff] [blame] | 301 | * @sum: log summary information |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 302 | * @head: list head to add nilfs_recovery_block struct |
| 303 | */ |
| 304 | static int nilfs_scan_dsync_log(struct the_nilfs *nilfs, sector_t start_blocknr, |
Ryusuke Konishi | 8565548 | 2010-05-23 19:46:44 +0900 | [diff] [blame] | 305 | struct nilfs_segment_summary *sum, |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 306 | struct list_head *head) |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 307 | { |
| 308 | struct buffer_head *bh; |
| 309 | unsigned int offset; |
Ryusuke Konishi | 8565548 | 2010-05-23 19:46:44 +0900 | [diff] [blame] | 310 | u32 nfinfo, sumbytes; |
| 311 | sector_t blocknr; |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 312 | ino_t ino; |
| 313 | int err = -EIO; |
| 314 | |
Ryusuke Konishi | 8565548 | 2010-05-23 19:46:44 +0900 | [diff] [blame] | 315 | nfinfo = le32_to_cpu(sum->ss_nfinfo); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 316 | if (!nfinfo) |
| 317 | return 0; |
| 318 | |
Ryusuke Konishi | 8565548 | 2010-05-23 19:46:44 +0900 | [diff] [blame] | 319 | sumbytes = le32_to_cpu(sum->ss_sumbytes); |
| 320 | blocknr = start_blocknr + DIV_ROUND_UP(sumbytes, nilfs->ns_blocksize); |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 321 | bh = __bread(nilfs->ns_bdev, start_blocknr, nilfs->ns_blocksize); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 322 | if (unlikely(!bh)) |
| 323 | goto out; |
| 324 | |
Ryusuke Konishi | 8565548 | 2010-05-23 19:46:44 +0900 | [diff] [blame] | 325 | offset = le16_to_cpu(sum->ss_bytes); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 326 | for (;;) { |
| 327 | unsigned long nblocks, ndatablk, nnodeblk; |
| 328 | struct nilfs_finfo *finfo; |
| 329 | |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 330 | finfo = nilfs_read_summary_info(nilfs, &bh, &offset, |
| 331 | sizeof(*finfo)); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 332 | if (unlikely(!finfo)) |
| 333 | goto out; |
| 334 | |
| 335 | ino = le64_to_cpu(finfo->fi_ino); |
| 336 | nblocks = le32_to_cpu(finfo->fi_nblocks); |
| 337 | ndatablk = le32_to_cpu(finfo->fi_ndatablk); |
| 338 | nnodeblk = nblocks - ndatablk; |
| 339 | |
| 340 | while (ndatablk-- > 0) { |
| 341 | struct nilfs_recovery_block *rb; |
| 342 | struct nilfs_binfo_v *binfo; |
| 343 | |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 344 | binfo = nilfs_read_summary_info(nilfs, &bh, &offset, |
| 345 | sizeof(*binfo)); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 346 | if (unlikely(!binfo)) |
| 347 | goto out; |
| 348 | |
| 349 | rb = kmalloc(sizeof(*rb), GFP_NOFS); |
| 350 | if (unlikely(!rb)) { |
| 351 | err = -ENOMEM; |
| 352 | goto out; |
| 353 | } |
| 354 | rb->ino = ino; |
| 355 | rb->blocknr = blocknr++; |
| 356 | rb->vblocknr = le64_to_cpu(binfo->bi_vblocknr); |
| 357 | rb->blkoff = le64_to_cpu(binfo->bi_blkoff); |
| 358 | /* INIT_LIST_HEAD(&rb->list); */ |
| 359 | list_add_tail(&rb->list, head); |
| 360 | } |
| 361 | if (--nfinfo == 0) |
| 362 | break; |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 363 | blocknr += nnodeblk; /* always 0 for data sync logs */ |
| 364 | nilfs_skip_summary_info(nilfs, &bh, &offset, sizeof(__le64), |
| 365 | nnodeblk); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 366 | if (unlikely(!bh)) |
| 367 | goto out; |
| 368 | } |
| 369 | err = 0; |
| 370 | out: |
| 371 | brelse(bh); /* brelse(NULL) is just ignored */ |
| 372 | return err; |
| 373 | } |
| 374 | |
| 375 | static void dispose_recovery_list(struct list_head *head) |
| 376 | { |
| 377 | while (!list_empty(head)) { |
Ryusuke Konishi | 0cc128388 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 378 | struct nilfs_recovery_block *rb; |
| 379 | |
| 380 | rb = list_first_entry(head, struct nilfs_recovery_block, list); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 381 | list_del(&rb->list); |
| 382 | kfree(rb); |
| 383 | } |
| 384 | } |
| 385 | |
Ryusuke Konishi | 654137d | 2009-05-17 19:07:21 +0900 | [diff] [blame] | 386 | struct nilfs_segment_entry { |
| 387 | struct list_head list; |
| 388 | __u64 segnum; |
| 389 | }; |
| 390 | |
| 391 | static int nilfs_segment_list_add(struct list_head *head, __u64 segnum) |
| 392 | { |
| 393 | struct nilfs_segment_entry *ent = kmalloc(sizeof(*ent), GFP_NOFS); |
| 394 | |
| 395 | if (unlikely(!ent)) |
| 396 | return -ENOMEM; |
| 397 | |
| 398 | ent->segnum = segnum; |
| 399 | INIT_LIST_HEAD(&ent->list); |
| 400 | list_add_tail(&ent->list, head); |
| 401 | return 0; |
| 402 | } |
| 403 | |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 404 | void nilfs_dispose_segment_list(struct list_head *head) |
| 405 | { |
| 406 | while (!list_empty(head)) { |
Ryusuke Konishi | 0cc128388 | 2011-05-05 12:56:51 +0900 | [diff] [blame] | 407 | struct nilfs_segment_entry *ent; |
| 408 | |
| 409 | ent = list_first_entry(head, struct nilfs_segment_entry, list); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 410 | list_del(&ent->list); |
Ryusuke Konishi | 654137d | 2009-05-17 19:07:21 +0900 | [diff] [blame] | 411 | kfree(ent); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | |
| 415 | static int nilfs_prepare_segment_for_recovery(struct the_nilfs *nilfs, |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 416 | struct super_block *sb, |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 417 | struct nilfs_recovery_info *ri) |
| 418 | { |
| 419 | struct list_head *head = &ri->ri_used_segments; |
| 420 | struct nilfs_segment_entry *ent, *n; |
| 421 | struct inode *sufile = nilfs->ns_sufile; |
| 422 | __u64 segnum[4]; |
| 423 | int err; |
| 424 | int i; |
| 425 | |
| 426 | segnum[0] = nilfs->ns_segnum; |
| 427 | segnum[1] = nilfs->ns_nextnum; |
| 428 | segnum[2] = ri->ri_segnum; |
| 429 | segnum[3] = ri->ri_nextnum; |
| 430 | |
| 431 | /* |
| 432 | * Releasing the next segment of the latest super root. |
| 433 | * The next segment is invalidated by this recovery. |
| 434 | */ |
| 435 | err = nilfs_sufile_free(sufile, segnum[1]); |
| 436 | if (unlikely(err)) |
| 437 | goto failed; |
| 438 | |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 439 | for (i = 1; i < 4; i++) { |
Ryusuke Konishi | 654137d | 2009-05-17 19:07:21 +0900 | [diff] [blame] | 440 | err = nilfs_segment_list_add(head, segnum[i]); |
| 441 | if (unlikely(err)) |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 442 | goto failed; |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /* |
| 446 | * Collecting segments written after the latest super root. |
Ryusuke Konishi | 2c2e52f | 2009-04-06 19:01:54 -0700 | [diff] [blame] | 447 | * These are marked dirty to avoid being reallocated in the next write. |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 448 | */ |
| 449 | list_for_each_entry_safe(ent, n, head, list) { |
Ryusuke Konishi | c85399c | 2009-04-05 18:30:58 +0900 | [diff] [blame] | 450 | if (ent->segnum != segnum[0]) { |
| 451 | err = nilfs_sufile_scrap(sufile, ent->segnum); |
| 452 | if (unlikely(err)) |
| 453 | goto failed; |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 454 | } |
Ryusuke Konishi | 2c2e52f | 2009-04-06 19:01:54 -0700 | [diff] [blame] | 455 | list_del(&ent->list); |
Ryusuke Konishi | 654137d | 2009-05-17 19:07:21 +0900 | [diff] [blame] | 456 | kfree(ent); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 457 | } |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 458 | |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 459 | /* Allocate new segments for recovery */ |
| 460 | err = nilfs_sufile_alloc(sufile, &segnum[0]); |
| 461 | if (unlikely(err)) |
| 462 | goto failed; |
| 463 | |
| 464 | nilfs->ns_pseg_offset = 0; |
| 465 | nilfs->ns_seg_seq = ri->ri_seq + 2; |
| 466 | nilfs->ns_nextnum = nilfs->ns_segnum = segnum[0]; |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 467 | |
| 468 | failed: |
| 469 | /* No need to recover sufile because it will be destroyed on error */ |
| 470 | return err; |
| 471 | } |
| 472 | |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 473 | static int nilfs_recovery_copy_block(struct the_nilfs *nilfs, |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 474 | struct nilfs_recovery_block *rb, |
| 475 | struct page *page) |
| 476 | { |
| 477 | struct buffer_head *bh_org; |
| 478 | void *kaddr; |
| 479 | |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 480 | bh_org = __bread(nilfs->ns_bdev, rb->blocknr, nilfs->ns_blocksize); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 481 | if (unlikely(!bh_org)) |
| 482 | return -EIO; |
| 483 | |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 484 | kaddr = kmap_atomic(page); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 485 | memcpy(kaddr + bh_offset(bh_org), bh_org->b_data, bh_org->b_size); |
Cong Wang | 7b9c097 | 2011-11-25 23:14:33 +0800 | [diff] [blame] | 486 | kunmap_atomic(kaddr); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 487 | brelse(bh_org); |
| 488 | return 0; |
| 489 | } |
| 490 | |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 491 | static int nilfs_recover_dsync_blocks(struct the_nilfs *nilfs, |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 492 | struct super_block *sb, |
Ryusuke Konishi | 4d8d929 | 2010-08-25 17:45:44 +0900 | [diff] [blame] | 493 | struct nilfs_root *root, |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 494 | struct list_head *head, |
| 495 | unsigned long *nr_salvaged_blocks) |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 496 | { |
| 497 | struct inode *inode; |
| 498 | struct nilfs_recovery_block *rb, *n; |
Ryusuke Konishi | 0c6c44c | 2016-05-23 16:23:39 -0700 | [diff] [blame] | 499 | unsigned int blocksize = nilfs->ns_blocksize; |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 500 | struct page *page; |
| 501 | loff_t pos; |
| 502 | int err = 0, err2 = 0; |
| 503 | |
| 504 | list_for_each_entry_safe(rb, n, head, list) { |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 505 | inode = nilfs_iget(sb, root, rb->ino); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 506 | if (IS_ERR(inode)) { |
| 507 | err = PTR_ERR(inode); |
| 508 | inode = NULL; |
| 509 | goto failed_inode; |
| 510 | } |
| 511 | |
| 512 | pos = rb->blkoff << inode->i_blkbits; |
Christoph Hellwig | 155130a | 2010-06-04 11:29:58 +0200 | [diff] [blame] | 513 | err = block_write_begin(inode->i_mapping, pos, blocksize, |
| 514 | 0, &page, nilfs_get_block); |
| 515 | if (unlikely(err)) { |
| 516 | loff_t isize = inode->i_size; |
Ryusuke Konishi | 4ad364c | 2016-05-23 16:23:25 -0700 | [diff] [blame] | 517 | |
Christoph Hellwig | 155130a | 2010-06-04 11:29:58 +0200 | [diff] [blame] | 518 | if (pos + blocksize > isize) |
Marco Stornelli | 2d1b399 | 2012-12-15 11:57:37 +0100 | [diff] [blame] | 519 | nilfs_write_failed(inode->i_mapping, |
| 520 | pos + blocksize); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 521 | goto failed_inode; |
Christoph Hellwig | 155130a | 2010-06-04 11:29:58 +0200 | [diff] [blame] | 522 | } |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 523 | |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 524 | err = nilfs_recovery_copy_block(nilfs, rb, page); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 525 | if (unlikely(err)) |
| 526 | goto failed_page; |
| 527 | |
Ryusuke Konishi | bcbc8c6 | 2010-12-27 00:05:49 +0900 | [diff] [blame] | 528 | err = nilfs_set_file_dirty(inode, 1); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 529 | if (unlikely(err)) |
| 530 | goto failed_page; |
| 531 | |
| 532 | block_write_end(NULL, inode->i_mapping, pos, blocksize, |
| 533 | blocksize, page, NULL); |
| 534 | |
| 535 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 536 | put_page(page); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 537 | |
| 538 | (*nr_salvaged_blocks)++; |
| 539 | goto next; |
| 540 | |
| 541 | failed_page: |
| 542 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 543 | put_page(page); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 544 | |
| 545 | failed_inode: |
Joe Perches | a1d0747 | 2020-08-11 18:35:49 -0700 | [diff] [blame] | 546 | nilfs_warn(sb, |
| 547 | "error %d recovering data block (ino=%lu, block-offset=%llu)", |
| 548 | err, (unsigned long)rb->ino, |
| 549 | (unsigned long long)rb->blkoff); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 550 | if (!err2) |
| 551 | err2 = err; |
| 552 | next: |
| 553 | iput(inode); /* iput(NULL) is just ignored */ |
| 554 | list_del_init(&rb->list); |
| 555 | kfree(rb); |
| 556 | } |
| 557 | return err2; |
| 558 | } |
| 559 | |
| 560 | /** |
| 561 | * nilfs_do_roll_forward - salvage logical segments newer than the latest |
| 562 | * checkpoint |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 563 | * @nilfs: nilfs object |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 564 | * @sb: super block instance |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 565 | * @ri: pointer to a nilfs_recovery_info |
| 566 | */ |
| 567 | static int nilfs_do_roll_forward(struct the_nilfs *nilfs, |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 568 | struct super_block *sb, |
Ryusuke Konishi | 4d8d929 | 2010-08-25 17:45:44 +0900 | [diff] [blame] | 569 | struct nilfs_root *root, |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 570 | struct nilfs_recovery_info *ri) |
| 571 | { |
Ryusuke Konishi | 354fa8b | 2010-05-23 19:21:49 +0900 | [diff] [blame] | 572 | struct buffer_head *bh_sum = NULL; |
Ryusuke Konishi | 4f05028 | 2015-11-06 16:32:16 -0800 | [diff] [blame] | 573 | struct nilfs_segment_summary *sum = NULL; |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 574 | sector_t pseg_start; |
| 575 | sector_t seg_start, seg_end; /* Starting/ending DBN of full segment */ |
| 576 | unsigned long nsalvaged_blocks = 0; |
Ryusuke Konishi | 8565548 | 2010-05-23 19:46:44 +0900 | [diff] [blame] | 577 | unsigned int flags; |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 578 | u64 seg_seq; |
| 579 | __u64 segnum, nextnum = 0; |
| 580 | int empty_seg = 0; |
| 581 | int err = 0, ret; |
| 582 | LIST_HEAD(dsync_blocks); /* list of data blocks to be recovered */ |
| 583 | enum { |
| 584 | RF_INIT_ST, |
| 585 | RF_DSYNC_ST, /* scanning data-sync segments */ |
| 586 | }; |
| 587 | int state = RF_INIT_ST; |
| 588 | |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 589 | pseg_start = ri->ri_lsegs_start; |
| 590 | seg_seq = ri->ri_lsegs_start_seq; |
| 591 | segnum = nilfs_get_segnum_of_block(nilfs, pseg_start); |
| 592 | nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end); |
| 593 | |
| 594 | while (segnum != ri->ri_segnum || pseg_start <= ri->ri_pseg_start) { |
Ryusuke Konishi | 354fa8b | 2010-05-23 19:21:49 +0900 | [diff] [blame] | 595 | brelse(bh_sum); |
| 596 | bh_sum = nilfs_read_log_header(nilfs, pseg_start, &sum); |
| 597 | if (!bh_sum) { |
| 598 | err = -EIO; |
| 599 | goto failed; |
| 600 | } |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 601 | |
Ryusuke Konishi | 354fa8b | 2010-05-23 19:21:49 +0900 | [diff] [blame] | 602 | ret = nilfs_validate_log(nilfs, seg_seq, bh_sum, sum); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 603 | if (ret) { |
| 604 | if (ret == NILFS_SEG_FAIL_IO) { |
| 605 | err = -EIO; |
| 606 | goto failed; |
| 607 | } |
| 608 | goto strayed; |
| 609 | } |
Ryusuke Konishi | 354fa8b | 2010-05-23 19:21:49 +0900 | [diff] [blame] | 610 | |
Ryusuke Konishi | 8565548 | 2010-05-23 19:46:44 +0900 | [diff] [blame] | 611 | flags = le16_to_cpu(sum->ss_flags); |
| 612 | if (flags & NILFS_SS_SR) |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 613 | goto confused; |
| 614 | |
| 615 | /* Found a valid partial segment; do recovery actions */ |
Ryusuke Konishi | 8565548 | 2010-05-23 19:46:44 +0900 | [diff] [blame] | 616 | nextnum = nilfs_get_segnum_of_block(nilfs, |
| 617 | le64_to_cpu(sum->ss_next)); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 618 | empty_seg = 0; |
Ryusuke Konishi | 8565548 | 2010-05-23 19:46:44 +0900 | [diff] [blame] | 619 | nilfs->ns_ctime = le64_to_cpu(sum->ss_create); |
| 620 | if (!(flags & NILFS_SS_GC)) |
| 621 | nilfs->ns_nongc_ctime = nilfs->ns_ctime; |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 622 | |
| 623 | switch (state) { |
| 624 | case RF_INIT_ST: |
Ryusuke Konishi | 8565548 | 2010-05-23 19:46:44 +0900 | [diff] [blame] | 625 | if (!(flags & NILFS_SS_LOGBGN) || |
| 626 | !(flags & NILFS_SS_SYNDT)) |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 627 | goto try_next_pseg; |
| 628 | state = RF_DSYNC_ST; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 629 | fallthrough; |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 630 | case RF_DSYNC_ST: |
Ryusuke Konishi | 8565548 | 2010-05-23 19:46:44 +0900 | [diff] [blame] | 631 | if (!(flags & NILFS_SS_SYNDT)) |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 632 | goto confused; |
| 633 | |
Ryusuke Konishi | 8565548 | 2010-05-23 19:46:44 +0900 | [diff] [blame] | 634 | err = nilfs_scan_dsync_log(nilfs, pseg_start, sum, |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 635 | &dsync_blocks); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 636 | if (unlikely(err)) |
| 637 | goto failed; |
Ryusuke Konishi | 8565548 | 2010-05-23 19:46:44 +0900 | [diff] [blame] | 638 | if (flags & NILFS_SS_LOGEND) { |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 639 | err = nilfs_recover_dsync_blocks( |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 640 | nilfs, sb, root, &dsync_blocks, |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 641 | &nsalvaged_blocks); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 642 | if (unlikely(err)) |
| 643 | goto failed; |
| 644 | state = RF_INIT_ST; |
| 645 | } |
| 646 | break; /* Fall through to try_next_pseg */ |
| 647 | } |
| 648 | |
| 649 | try_next_pseg: |
| 650 | if (pseg_start == ri->ri_lsegs_end) |
| 651 | break; |
Ryusuke Konishi | 8565548 | 2010-05-23 19:46:44 +0900 | [diff] [blame] | 652 | pseg_start += le32_to_cpu(sum->ss_nblocks); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 653 | if (pseg_start < seg_end) |
| 654 | continue; |
| 655 | goto feed_segment; |
| 656 | |
| 657 | strayed: |
| 658 | if (pseg_start == ri->ri_lsegs_end) |
| 659 | break; |
| 660 | |
| 661 | feed_segment: |
| 662 | /* Looking to the next full segment */ |
| 663 | if (empty_seg++) |
| 664 | break; |
| 665 | seg_seq++; |
| 666 | segnum = nextnum; |
| 667 | nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end); |
| 668 | pseg_start = seg_start; |
| 669 | } |
| 670 | |
| 671 | if (nsalvaged_blocks) { |
Joe Perches | a1d0747 | 2020-08-11 18:35:49 -0700 | [diff] [blame] | 672 | nilfs_info(sb, "salvaged %lu blocks", nsalvaged_blocks); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 673 | ri->ri_need_recovery = NILFS_RECOVERY_ROLLFORWARD_DONE; |
| 674 | } |
| 675 | out: |
Ryusuke Konishi | 354fa8b | 2010-05-23 19:21:49 +0900 | [diff] [blame] | 676 | brelse(bh_sum); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 677 | dispose_recovery_list(&dsync_blocks); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 678 | return err; |
| 679 | |
| 680 | confused: |
| 681 | err = -EINVAL; |
| 682 | failed: |
Joe Perches | a1d0747 | 2020-08-11 18:35:49 -0700 | [diff] [blame] | 683 | nilfs_err(sb, |
Ryusuke Konishi | feee880 | 2016-08-02 14:05:10 -0700 | [diff] [blame] | 684 | "error %d roll-forwarding partial segment at blocknr = %llu", |
| 685 | err, (unsigned long long)pseg_start); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 686 | goto out; |
| 687 | } |
| 688 | |
| 689 | static void nilfs_finish_roll_forward(struct the_nilfs *nilfs, |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 690 | struct nilfs_recovery_info *ri) |
| 691 | { |
| 692 | struct buffer_head *bh; |
| 693 | int err; |
| 694 | |
| 695 | if (nilfs_get_segnum_of_block(nilfs, ri->ri_lsegs_start) != |
| 696 | nilfs_get_segnum_of_block(nilfs, ri->ri_super_root)) |
| 697 | return; |
| 698 | |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 699 | bh = __getblk(nilfs->ns_bdev, ri->ri_lsegs_start, nilfs->ns_blocksize); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 700 | BUG_ON(!bh); |
| 701 | memset(bh->b_data, 0, bh->b_size); |
| 702 | set_buffer_dirty(bh); |
| 703 | err = sync_dirty_buffer(bh); |
| 704 | if (unlikely(err)) |
Joe Perches | a1d0747 | 2020-08-11 18:35:49 -0700 | [diff] [blame] | 705 | nilfs_warn(nilfs->ns_sb, |
| 706 | "buffer sync write failed during post-cleaning of recovery."); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 707 | brelse(bh); |
| 708 | } |
| 709 | |
| 710 | /** |
Ryusuke Konishi | aee5ce2 | 2010-05-23 12:21:57 +0900 | [diff] [blame] | 711 | * nilfs_salvage_orphan_logs - salvage logs written after the latest checkpoint |
| 712 | * @nilfs: nilfs object |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 713 | * @sb: super block instance |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 714 | * @ri: pointer to a nilfs_recovery_info struct to store search results. |
| 715 | * |
| 716 | * Return Value: On success, 0 is returned. On error, one of the following |
| 717 | * negative error code is returned. |
| 718 | * |
| 719 | * %-EINVAL - Inconsistent filesystem state. |
| 720 | * |
| 721 | * %-EIO - I/O error |
| 722 | * |
| 723 | * %-ENOSPC - No space left on device (only in a panic state). |
| 724 | * |
| 725 | * %-ERESTARTSYS - Interrupted. |
| 726 | * |
| 727 | * %-ENOMEM - Insufficient memory available. |
| 728 | */ |
Ryusuke Konishi | aee5ce2 | 2010-05-23 12:21:57 +0900 | [diff] [blame] | 729 | int nilfs_salvage_orphan_logs(struct the_nilfs *nilfs, |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 730 | struct super_block *sb, |
Ryusuke Konishi | aee5ce2 | 2010-05-23 12:21:57 +0900 | [diff] [blame] | 731 | struct nilfs_recovery_info *ri) |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 732 | { |
Ryusuke Konishi | 4d8d929 | 2010-08-25 17:45:44 +0900 | [diff] [blame] | 733 | struct nilfs_root *root; |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 734 | int err; |
| 735 | |
| 736 | if (ri->ri_lsegs_start == 0 || ri->ri_lsegs_end == 0) |
| 737 | return 0; |
| 738 | |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 739 | err = nilfs_attach_checkpoint(sb, ri->ri_cno, true, &root); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 740 | if (unlikely(err)) { |
Joe Perches | a1d0747 | 2020-08-11 18:35:49 -0700 | [diff] [blame] | 741 | nilfs_err(sb, "error %d loading the latest checkpoint", err); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 742 | return err; |
| 743 | } |
| 744 | |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 745 | err = nilfs_do_roll_forward(nilfs, sb, root, ri); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 746 | if (unlikely(err)) |
| 747 | goto failed; |
| 748 | |
| 749 | if (ri->ri_need_recovery == NILFS_RECOVERY_ROLLFORWARD_DONE) { |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 750 | err = nilfs_prepare_segment_for_recovery(nilfs, sb, ri); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 751 | if (unlikely(err)) { |
Joe Perches | a1d0747 | 2020-08-11 18:35:49 -0700 | [diff] [blame] | 752 | nilfs_err(sb, "error %d preparing segment for recovery", |
Ryusuke Konishi | feee880 | 2016-08-02 14:05:10 -0700 | [diff] [blame] | 753 | err); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 754 | goto failed; |
| 755 | } |
| 756 | |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 757 | err = nilfs_attach_log_writer(sb, root); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 758 | if (unlikely(err)) |
| 759 | goto failed; |
| 760 | |
| 761 | set_nilfs_discontinued(nilfs); |
Ryusuke Konishi | f754514 | 2011-03-09 11:05:08 +0900 | [diff] [blame] | 762 | err = nilfs_construct_segment(sb); |
| 763 | nilfs_detach_log_writer(sb); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 764 | |
| 765 | if (unlikely(err)) { |
Joe Perches | a1d0747 | 2020-08-11 18:35:49 -0700 | [diff] [blame] | 766 | nilfs_err(sb, "error %d writing segment for recovery", |
Ryusuke Konishi | feee880 | 2016-08-02 14:05:10 -0700 | [diff] [blame] | 767 | err); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 768 | goto failed; |
| 769 | } |
| 770 | |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 771 | nilfs_finish_roll_forward(nilfs, ri); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 772 | } |
| 773 | |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 774 | failed: |
Ryusuke Konishi | 4d8d929 | 2010-08-25 17:45:44 +0900 | [diff] [blame] | 775 | nilfs_put_root(root); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 776 | return err; |
| 777 | } |
| 778 | |
| 779 | /** |
| 780 | * nilfs_search_super_root - search the latest valid super root |
| 781 | * @nilfs: the_nilfs |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 782 | * @ri: pointer to a nilfs_recovery_info struct to store search results. |
| 783 | * |
| 784 | * nilfs_search_super_root() looks for the latest super-root from a partial |
| 785 | * segment pointed by the superblock. It sets up struct the_nilfs through |
| 786 | * this search. It fills nilfs_recovery_info (ri) required for recovery. |
| 787 | * |
| 788 | * Return Value: On success, 0 is returned. On error, one of the following |
| 789 | * negative error code is returned. |
| 790 | * |
| 791 | * %-EINVAL - No valid segment found |
| 792 | * |
| 793 | * %-EIO - I/O error |
Ryusuke Konishi | 2d72b99 | 2010-06-28 19:15:25 +0900 | [diff] [blame] | 794 | * |
| 795 | * %-ENOMEM - Insufficient memory available. |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 796 | */ |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 797 | int nilfs_search_super_root(struct the_nilfs *nilfs, |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 798 | struct nilfs_recovery_info *ri) |
| 799 | { |
Ryusuke Konishi | 354fa8b | 2010-05-23 19:21:49 +0900 | [diff] [blame] | 800 | struct buffer_head *bh_sum = NULL; |
Ryusuke Konishi | 4f05028 | 2015-11-06 16:32:16 -0800 | [diff] [blame] | 801 | struct nilfs_segment_summary *sum = NULL; |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 802 | sector_t pseg_start, pseg_end, sr_pseg_start = 0; |
| 803 | sector_t seg_start, seg_end; /* range of full segment (block number) */ |
Ryusuke Konishi | 050b414 | 2009-11-19 22:24:48 +0900 | [diff] [blame] | 804 | sector_t b, end; |
Ryusuke Konishi | 8565548 | 2010-05-23 19:46:44 +0900 | [diff] [blame] | 805 | unsigned long nblocks; |
| 806 | unsigned int flags; |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 807 | u64 seg_seq; |
| 808 | __u64 segnum, nextnum = 0; |
| 809 | __u64 cno; |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 810 | LIST_HEAD(segments); |
| 811 | int empty_seg = 0, scan_newer = 0; |
| 812 | int ret; |
| 813 | |
| 814 | pseg_start = nilfs->ns_last_pseg; |
| 815 | seg_seq = nilfs->ns_last_seq; |
| 816 | cno = nilfs->ns_last_cno; |
| 817 | segnum = nilfs_get_segnum_of_block(nilfs, pseg_start); |
| 818 | |
| 819 | /* Calculate range of segment */ |
| 820 | nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end); |
| 821 | |
Ryusuke Konishi | 050b414 | 2009-11-19 22:24:48 +0900 | [diff] [blame] | 822 | /* Read ahead segment */ |
| 823 | b = seg_start; |
| 824 | while (b <= seg_end) |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 825 | __breadahead(nilfs->ns_bdev, b++, nilfs->ns_blocksize); |
Ryusuke Konishi | 050b414 | 2009-11-19 22:24:48 +0900 | [diff] [blame] | 826 | |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 827 | for (;;) { |
Ryusuke Konishi | 354fa8b | 2010-05-23 19:21:49 +0900 | [diff] [blame] | 828 | brelse(bh_sum); |
| 829 | ret = NILFS_SEG_FAIL_IO; |
| 830 | bh_sum = nilfs_read_log_header(nilfs, pseg_start, &sum); |
| 831 | if (!bh_sum) |
| 832 | goto failed; |
| 833 | |
| 834 | ret = nilfs_validate_log(nilfs, seg_seq, bh_sum, sum); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 835 | if (ret) { |
| 836 | if (ret == NILFS_SEG_FAIL_IO) |
| 837 | goto failed; |
| 838 | goto strayed; |
| 839 | } |
Ryusuke Konishi | 354fa8b | 2010-05-23 19:21:49 +0900 | [diff] [blame] | 840 | |
Ryusuke Konishi | 8565548 | 2010-05-23 19:46:44 +0900 | [diff] [blame] | 841 | nblocks = le32_to_cpu(sum->ss_nblocks); |
| 842 | pseg_end = pseg_start + nblocks - 1; |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 843 | if (unlikely(pseg_end > seg_end)) { |
| 844 | ret = NILFS_SEG_FAIL_CONSISTENCY; |
| 845 | goto strayed; |
| 846 | } |
| 847 | |
| 848 | /* A valid partial segment */ |
| 849 | ri->ri_pseg_start = pseg_start; |
| 850 | ri->ri_seq = seg_seq; |
| 851 | ri->ri_segnum = segnum; |
Ryusuke Konishi | 8565548 | 2010-05-23 19:46:44 +0900 | [diff] [blame] | 852 | nextnum = nilfs_get_segnum_of_block(nilfs, |
| 853 | le64_to_cpu(sum->ss_next)); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 854 | ri->ri_nextnum = nextnum; |
| 855 | empty_seg = 0; |
| 856 | |
Ryusuke Konishi | 8565548 | 2010-05-23 19:46:44 +0900 | [diff] [blame] | 857 | flags = le16_to_cpu(sum->ss_flags); |
| 858 | if (!(flags & NILFS_SS_SR) && !scan_newer) { |
Ryusuke Konishi | 076a378 | 2016-05-23 16:23:48 -0700 | [diff] [blame] | 859 | /* |
| 860 | * This will never happen because a superblock |
| 861 | * (last_segment) always points to a pseg with |
| 862 | * a super root. |
| 863 | */ |
Ryusuke Konishi | 050b414 | 2009-11-19 22:24:48 +0900 | [diff] [blame] | 864 | ret = NILFS_SEG_FAIL_CONSISTENCY; |
| 865 | goto failed; |
| 866 | } |
| 867 | |
| 868 | if (pseg_start == seg_start) { |
| 869 | nilfs_get_segment_range(nilfs, nextnum, &b, &end); |
| 870 | while (b <= end) |
Ryusuke Konishi | 8b94025 | 2010-05-23 01:39:02 +0900 | [diff] [blame] | 871 | __breadahead(nilfs->ns_bdev, b++, |
| 872 | nilfs->ns_blocksize); |
Ryusuke Konishi | 050b414 | 2009-11-19 22:24:48 +0900 | [diff] [blame] | 873 | } |
Ryusuke Konishi | 8565548 | 2010-05-23 19:46:44 +0900 | [diff] [blame] | 874 | if (!(flags & NILFS_SS_SR)) { |
| 875 | if (!ri->ri_lsegs_start && (flags & NILFS_SS_LOGBGN)) { |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 876 | ri->ri_lsegs_start = pseg_start; |
| 877 | ri->ri_lsegs_start_seq = seg_seq; |
| 878 | } |
Ryusuke Konishi | 8565548 | 2010-05-23 19:46:44 +0900 | [diff] [blame] | 879 | if (flags & NILFS_SS_LOGEND) |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 880 | ri->ri_lsegs_end = pseg_start; |
| 881 | goto try_next_pseg; |
| 882 | } |
| 883 | |
| 884 | /* A valid super root was found. */ |
| 885 | ri->ri_cno = cno++; |
| 886 | ri->ri_super_root = pseg_end; |
| 887 | ri->ri_lsegs_start = ri->ri_lsegs_end = 0; |
| 888 | |
| 889 | nilfs_dispose_segment_list(&segments); |
Ryusuke Konishi | 8565548 | 2010-05-23 19:46:44 +0900 | [diff] [blame] | 890 | sr_pseg_start = pseg_start; |
| 891 | nilfs->ns_pseg_offset = pseg_start + nblocks - seg_start; |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 892 | nilfs->ns_seg_seq = seg_seq; |
| 893 | nilfs->ns_segnum = segnum; |
| 894 | nilfs->ns_cno = cno; /* nilfs->ns_cno = ri->ri_cno + 1 */ |
Ryusuke Konishi | 8565548 | 2010-05-23 19:46:44 +0900 | [diff] [blame] | 895 | nilfs->ns_ctime = le64_to_cpu(sum->ss_create); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 896 | nilfs->ns_nextnum = nextnum; |
| 897 | |
| 898 | if (scan_newer) |
| 899 | ri->ri_need_recovery = NILFS_RECOVERY_SR_UPDATED; |
Ryusuke Konishi | 2c2e52f | 2009-04-06 19:01:54 -0700 | [diff] [blame] | 900 | else { |
Ryusuke Konishi | 2c2e52f | 2009-04-06 19:01:54 -0700 | [diff] [blame] | 901 | if (nilfs->ns_mount_state & NILFS_VALID_FS) |
| 902 | goto super_root_found; |
| 903 | scan_newer = 1; |
| 904 | } |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 905 | |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 906 | try_next_pseg: |
| 907 | /* Standing on a course, or met an inconsistent state */ |
Ryusuke Konishi | 8565548 | 2010-05-23 19:46:44 +0900 | [diff] [blame] | 908 | pseg_start += nblocks; |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 909 | if (pseg_start < seg_end) |
| 910 | continue; |
| 911 | goto feed_segment; |
| 912 | |
| 913 | strayed: |
| 914 | /* Off the trail */ |
| 915 | if (!scan_newer) |
| 916 | /* |
| 917 | * This can happen if a checkpoint was written without |
| 918 | * barriers, or as a result of an I/O failure. |
| 919 | */ |
| 920 | goto failed; |
| 921 | |
| 922 | feed_segment: |
| 923 | /* Looking to the next full segment */ |
| 924 | if (empty_seg++) |
| 925 | goto super_root_found; /* found a valid super root */ |
| 926 | |
Ryusuke Konishi | 654137d | 2009-05-17 19:07:21 +0900 | [diff] [blame] | 927 | ret = nilfs_segment_list_add(&segments, segnum); |
| 928 | if (unlikely(ret)) |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 929 | goto failed; |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 930 | |
| 931 | seg_seq++; |
| 932 | segnum = nextnum; |
| 933 | nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end); |
| 934 | pseg_start = seg_start; |
| 935 | } |
| 936 | |
| 937 | super_root_found: |
| 938 | /* Updating pointers relating to the latest checkpoint */ |
Ryusuke Konishi | 354fa8b | 2010-05-23 19:21:49 +0900 | [diff] [blame] | 939 | brelse(bh_sum); |
Ryusuke Konishi | 0935db7 | 2009-11-29 02:39:11 +0900 | [diff] [blame] | 940 | list_splice_tail(&segments, &ri->ri_used_segments); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 941 | nilfs->ns_last_pseg = sr_pseg_start; |
| 942 | nilfs->ns_last_seq = nilfs->ns_seg_seq; |
| 943 | nilfs->ns_last_cno = ri->ri_cno; |
| 944 | return 0; |
| 945 | |
| 946 | failed: |
Ryusuke Konishi | 354fa8b | 2010-05-23 19:21:49 +0900 | [diff] [blame] | 947 | brelse(bh_sum); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 948 | nilfs_dispose_segment_list(&segments); |
Ryusuke Konishi | feee880 | 2016-08-02 14:05:10 -0700 | [diff] [blame] | 949 | return ret < 0 ? ret : nilfs_warn_segment_error(nilfs->ns_sb, ret); |
Ryusuke Konishi | 0f3e1c7 | 2009-04-06 19:01:38 -0700 | [diff] [blame] | 950 | } |