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