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