blob: 35506b1704d1d5340fc19e2d1640fc9adda9ed94 [file] [log] [blame]
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -07001/*
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 Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -070027#include <linux/crc32.h>
28#include "nilfs.h"
29#include "segment.h"
30#include "sufile.h"
31#include "page.h"
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -070032#include "segbuf.h"
33
34/*
35 * Segment check result
36 */
37enum {
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 Konishi0f3e1c72009-04-06 19:01:38 -070043 NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT,
44 NILFS_SEG_FAIL_CHECKSUM_FULL,
45 NILFS_SEG_FAIL_CONSISTENCY,
46};
47
48/* work structure for recovery */
49struct 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
59static 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 Konishi0f3e1c72009-04-06 19:01:38 -070074 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 Konishi0f3e1c72009-04-06 19:01:38 -070090 }
91 return -EINVAL;
92}
93
94static void store_segsum_info(struct nilfs_segsum_info *ssi,
95 struct nilfs_segment_summary *sum,
96 unsigned int blocksize)
97{
98 ssi->flags = le16_to_cpu(sum->ss_flags);
99 ssi->seg_seq = le64_to_cpu(sum->ss_seq);
100 ssi->ctime = le64_to_cpu(sum->ss_create);
101 ssi->next = le64_to_cpu(sum->ss_next);
102 ssi->nblocks = le32_to_cpu(sum->ss_nblocks);
103 ssi->nfinfo = le32_to_cpu(sum->ss_nfinfo);
104 ssi->sumbytes = le32_to_cpu(sum->ss_sumbytes);
105
106 ssi->nsumblk = DIV_ROUND_UP(ssi->sumbytes, blocksize);
107 ssi->nfileblk = ssi->nblocks - ssi->nsumblk - !!NILFS_SEG_HAS_SR(ssi);
Ryusuke Konishi50614bc2010-04-10 17:59:15 +0900108
109 /* need to verify ->ss_bytes field if read ->ss_cno */
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700110}
111
112/**
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900113 * nilfs_compute_checksum - compute checksum of blocks continuously
114 * @nilfs: nilfs object
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700115 * @bhs: buffer head of start block
116 * @sum: place to store result
117 * @offset: offset bytes in the first block
118 * @check_bytes: number of bytes to be checked
119 * @start: DBN of start block
120 * @nblock: number of blocks to be checked
121 */
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900122static int nilfs_compute_checksum(struct the_nilfs *nilfs,
123 struct buffer_head *bhs, u32 *sum,
124 unsigned long offset, u64 check_bytes,
125 sector_t start, unsigned long nblock)
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700126{
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900127 unsigned int blocksize = nilfs->ns_blocksize;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700128 unsigned long size;
129 u32 crc;
130
131 BUG_ON(offset >= blocksize);
132 check_bytes -= offset;
133 size = min_t(u64, check_bytes, blocksize - offset);
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900134 crc = crc32_le(nilfs->ns_crc_seed,
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700135 (unsigned char *)bhs->b_data + offset, size);
136 if (--nblock > 0) {
137 do {
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900138 struct buffer_head *bh;
139
140 bh = __bread(nilfs->ns_bdev, ++start, blocksize);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700141 if (!bh)
142 return -EIO;
143 check_bytes -= size;
144 size = min_t(u64, check_bytes, blocksize);
145 crc = crc32_le(crc, bh->b_data, size);
146 brelse(bh);
147 } while (--nblock > 0);
148 }
149 *sum = crc;
150 return 0;
151}
152
153/**
154 * nilfs_read_super_root_block - read super root block
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900155 * @nilfs: nilfs object
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700156 * @sr_block: disk block number of the super root block
157 * @pbh: address of a buffer_head pointer to return super root buffer
158 * @check: CRC check flag
159 */
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900160int nilfs_read_super_root_block(struct the_nilfs *nilfs, sector_t sr_block,
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700161 struct buffer_head **pbh, int check)
162{
163 struct buffer_head *bh_sr;
164 struct nilfs_super_root *sr;
165 u32 crc;
166 int ret;
167
168 *pbh = NULL;
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900169 bh_sr = __bread(nilfs->ns_bdev, sr_block, nilfs->ns_blocksize);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700170 if (unlikely(!bh_sr)) {
171 ret = NILFS_SEG_FAIL_IO;
172 goto failed;
173 }
174
175 sr = (struct nilfs_super_root *)bh_sr->b_data;
176 if (check) {
177 unsigned bytes = le16_to_cpu(sr->sr_bytes);
178
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900179 if (bytes == 0 || bytes > nilfs->ns_blocksize) {
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700180 ret = NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT;
181 goto failed_bh;
182 }
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900183 if (nilfs_compute_checksum(
184 nilfs, bh_sr, &crc, sizeof(sr->sr_sum), bytes,
185 sr_block, 1)) {
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700186 ret = NILFS_SEG_FAIL_IO;
187 goto failed_bh;
188 }
189 if (crc != le32_to_cpu(sr->sr_sum)) {
190 ret = NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT;
191 goto failed_bh;
192 }
193 }
194 *pbh = bh_sr;
195 return 0;
196
197 failed_bh:
198 brelse(bh_sr);
199
200 failed:
201 return nilfs_warn_segment_error(ret);
202}
203
204/**
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900205 * nilfs_read_log_header - read summary header of the specified log
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900206 * @nilfs: nilfs object
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900207 * @start_blocknr: start block number of the log
208 * @sum: pointer to return segment summary structure
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700209 */
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900210static struct buffer_head *
211nilfs_read_log_header(struct the_nilfs *nilfs, sector_t start_blocknr,
212 struct nilfs_segment_summary **sum)
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700213{
214 struct buffer_head *bh_sum;
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900215
216 bh_sum = __bread(nilfs->ns_bdev, start_blocknr, nilfs->ns_blocksize);
217 if (bh_sum)
218 *sum = (struct nilfs_segment_summary *)bh_sum->b_data;
219 return bh_sum;
220}
221
222/**
223 * nilfs_validate_log - verify consistency of log
224 * @nilfs: nilfs object
225 * @seg_seq: sequence number of segment
226 * @bh_sum: buffer head of summary block
227 * @sum: segment summary struct
228 */
229static int nilfs_validate_log(struct the_nilfs *nilfs, u64 seg_seq,
230 struct buffer_head *bh_sum,
231 struct nilfs_segment_summary *sum)
232{
Jiro SEKIBA03f29362010-02-18 19:11:35 +0900233 unsigned long nblock;
234 u32 crc;
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900235 int ret;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700236
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900237 ret = NILFS_SEG_FAIL_MAGIC;
238 if (le32_to_cpu(sum->ss_magic) != NILFS_SEGSUM_MAGIC)
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700239 goto out;
240
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900241 ret = NILFS_SEG_FAIL_SEQ;
242 if (le64_to_cpu(sum->ss_seq) != seg_seq)
243 goto out;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700244
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900245 nblock = le32_to_cpu(sum->ss_nblocks);
246 ret = NILFS_SEG_FAIL_CONSISTENCY;
247 if (unlikely(nblock == 0 || nblock > nilfs->ns_blocks_per_segment))
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700248 /* This limits the number of blocks read in the CRC check */
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900249 goto out;
250
251 ret = NILFS_SEG_FAIL_IO;
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900252 if (nilfs_compute_checksum(nilfs, bh_sum, &crc, sizeof(sum->ss_datasum),
253 ((u64)nblock << nilfs->ns_blocksize_bits),
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900254 bh_sum->b_blocknr, nblock))
255 goto out;
256
257 ret = NILFS_SEG_FAIL_CHECKSUM_FULL;
258 if (crc != le32_to_cpu(sum->ss_datasum))
259 goto out;
260 ret = 0;
261out:
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700262 return ret;
263}
264
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900265/**
266 * nilfs_read_summary_info - read an item on summary blocks of a log
267 * @nilfs: nilfs object
268 * @pbh: the current buffer head on summary blocks [in, out]
269 * @offset: the current byte offset on summary blocks [in, out]
270 * @bytes: byte size of the item to be read
271 */
272static void *nilfs_read_summary_info(struct the_nilfs *nilfs,
273 struct buffer_head **pbh,
274 unsigned int *offset, unsigned int bytes)
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700275{
276 void *ptr;
277 sector_t blocknr;
278
279 BUG_ON((*pbh)->b_size < *offset);
280 if (bytes > (*pbh)->b_size - *offset) {
281 blocknr = (*pbh)->b_blocknr;
282 brelse(*pbh);
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900283 *pbh = __bread(nilfs->ns_bdev, blocknr + 1,
284 nilfs->ns_blocksize);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700285 if (unlikely(!*pbh))
286 return NULL;
287 *offset = 0;
288 }
289 ptr = (*pbh)->b_data + *offset;
290 *offset += bytes;
291 return ptr;
292}
293
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900294/**
295 * nilfs_skip_summary_info - skip items on summary blocks of a log
296 * @nilfs: nilfs object
297 * @pbh: the current buffer head on summary blocks [in, out]
298 * @offset: the current byte offset on summary blocks [in, out]
299 * @bytes: byte size of the item to be skipped
300 * @count: number of items to be skipped
301 */
302static void nilfs_skip_summary_info(struct the_nilfs *nilfs,
303 struct buffer_head **pbh,
304 unsigned int *offset, unsigned int bytes,
305 unsigned long count)
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700306{
307 unsigned int rest_item_in_current_block
308 = ((*pbh)->b_size - *offset) / bytes;
309
310 if (count <= rest_item_in_current_block) {
311 *offset += bytes * count;
312 } else {
313 sector_t blocknr = (*pbh)->b_blocknr;
314 unsigned int nitem_per_block = (*pbh)->b_size / bytes;
315 unsigned int bcnt;
316
317 count -= rest_item_in_current_block;
318 bcnt = DIV_ROUND_UP(count, nitem_per_block);
319 *offset = bytes * (count - (bcnt - 1) * nitem_per_block);
320
321 brelse(*pbh);
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900322 *pbh = __bread(nilfs->ns_bdev, blocknr + bcnt,
323 nilfs->ns_blocksize);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700324 }
325}
326
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900327/**
328 * nilfs_scan_dsync_log - get block information of a log written for data sync
329 * @nilfs: nilfs object
330 * @start_blocknr: start block number of the log
331 * @ssi: log summary information
332 * @head: list head to add nilfs_recovery_block struct
333 */
334static int nilfs_scan_dsync_log(struct the_nilfs *nilfs, sector_t start_blocknr,
335 struct nilfs_segsum_info *ssi,
336 struct list_head *head)
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700337{
338 struct buffer_head *bh;
339 unsigned int offset;
340 unsigned long nfinfo = ssi->nfinfo;
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900341 sector_t blocknr = start_blocknr + ssi->nsumblk;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700342 ino_t ino;
343 int err = -EIO;
344
345 if (!nfinfo)
346 return 0;
347
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900348 bh = __bread(nilfs->ns_bdev, start_blocknr, nilfs->ns_blocksize);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700349 if (unlikely(!bh))
350 goto out;
351
352 offset = le16_to_cpu(
353 ((struct nilfs_segment_summary *)bh->b_data)->ss_bytes);
354 for (;;) {
355 unsigned long nblocks, ndatablk, nnodeblk;
356 struct nilfs_finfo *finfo;
357
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900358 finfo = nilfs_read_summary_info(nilfs, &bh, &offset,
359 sizeof(*finfo));
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700360 if (unlikely(!finfo))
361 goto out;
362
363 ino = le64_to_cpu(finfo->fi_ino);
364 nblocks = le32_to_cpu(finfo->fi_nblocks);
365 ndatablk = le32_to_cpu(finfo->fi_ndatablk);
366 nnodeblk = nblocks - ndatablk;
367
368 while (ndatablk-- > 0) {
369 struct nilfs_recovery_block *rb;
370 struct nilfs_binfo_v *binfo;
371
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900372 binfo = nilfs_read_summary_info(nilfs, &bh, &offset,
373 sizeof(*binfo));
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700374 if (unlikely(!binfo))
375 goto out;
376
377 rb = kmalloc(sizeof(*rb), GFP_NOFS);
378 if (unlikely(!rb)) {
379 err = -ENOMEM;
380 goto out;
381 }
382 rb->ino = ino;
383 rb->blocknr = blocknr++;
384 rb->vblocknr = le64_to_cpu(binfo->bi_vblocknr);
385 rb->blkoff = le64_to_cpu(binfo->bi_blkoff);
386 /* INIT_LIST_HEAD(&rb->list); */
387 list_add_tail(&rb->list, head);
388 }
389 if (--nfinfo == 0)
390 break;
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900391 blocknr += nnodeblk; /* always 0 for data sync logs */
392 nilfs_skip_summary_info(nilfs, &bh, &offset, sizeof(__le64),
393 nnodeblk);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700394 if (unlikely(!bh))
395 goto out;
396 }
397 err = 0;
398 out:
399 brelse(bh); /* brelse(NULL) is just ignored */
400 return err;
401}
402
403static void dispose_recovery_list(struct list_head *head)
404{
405 while (!list_empty(head)) {
406 struct nilfs_recovery_block *rb
407 = list_entry(head->next,
408 struct nilfs_recovery_block, list);
409 list_del(&rb->list);
410 kfree(rb);
411 }
412}
413
Ryusuke Konishi654137d2009-05-17 19:07:21 +0900414struct nilfs_segment_entry {
415 struct list_head list;
416 __u64 segnum;
417};
418
419static int nilfs_segment_list_add(struct list_head *head, __u64 segnum)
420{
421 struct nilfs_segment_entry *ent = kmalloc(sizeof(*ent), GFP_NOFS);
422
423 if (unlikely(!ent))
424 return -ENOMEM;
425
426 ent->segnum = segnum;
427 INIT_LIST_HEAD(&ent->list);
428 list_add_tail(&ent->list, head);
429 return 0;
430}
431
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700432void nilfs_dispose_segment_list(struct list_head *head)
433{
434 while (!list_empty(head)) {
435 struct nilfs_segment_entry *ent
436 = list_entry(head->next,
437 struct nilfs_segment_entry, list);
438 list_del(&ent->list);
Ryusuke Konishi654137d2009-05-17 19:07:21 +0900439 kfree(ent);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700440 }
441}
442
443static int nilfs_prepare_segment_for_recovery(struct the_nilfs *nilfs,
Ryusuke Konishi85c2a742009-04-28 23:38:46 +0900444 struct nilfs_sb_info *sbi,
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700445 struct nilfs_recovery_info *ri)
446{
447 struct list_head *head = &ri->ri_used_segments;
448 struct nilfs_segment_entry *ent, *n;
449 struct inode *sufile = nilfs->ns_sufile;
450 __u64 segnum[4];
451 int err;
452 int i;
453
454 segnum[0] = nilfs->ns_segnum;
455 segnum[1] = nilfs->ns_nextnum;
456 segnum[2] = ri->ri_segnum;
457 segnum[3] = ri->ri_nextnum;
458
Ryusuke Konishi85c2a742009-04-28 23:38:46 +0900459 nilfs_attach_writer(nilfs, sbi);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700460 /*
461 * Releasing the next segment of the latest super root.
462 * The next segment is invalidated by this recovery.
463 */
464 err = nilfs_sufile_free(sufile, segnum[1]);
465 if (unlikely(err))
466 goto failed;
467
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700468 for (i = 1; i < 4; i++) {
Ryusuke Konishi654137d2009-05-17 19:07:21 +0900469 err = nilfs_segment_list_add(head, segnum[i]);
470 if (unlikely(err))
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700471 goto failed;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700472 }
473
474 /*
475 * Collecting segments written after the latest super root.
Ryusuke Konishi2c2e52f2009-04-06 19:01:54 -0700476 * These are marked dirty to avoid being reallocated in the next write.
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700477 */
478 list_for_each_entry_safe(ent, n, head, list) {
Ryusuke Konishic85399c2009-04-05 18:30:58 +0900479 if (ent->segnum != segnum[0]) {
480 err = nilfs_sufile_scrap(sufile, ent->segnum);
481 if (unlikely(err))
482 goto failed;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700483 }
Ryusuke Konishi2c2e52f2009-04-06 19:01:54 -0700484 list_del(&ent->list);
Ryusuke Konishi654137d2009-05-17 19:07:21 +0900485 kfree(ent);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700486 }
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700487
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700488 /* Allocate new segments for recovery */
489 err = nilfs_sufile_alloc(sufile, &segnum[0]);
490 if (unlikely(err))
491 goto failed;
492
493 nilfs->ns_pseg_offset = 0;
494 nilfs->ns_seg_seq = ri->ri_seq + 2;
495 nilfs->ns_nextnum = nilfs->ns_segnum = segnum[0];
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700496
497 failed:
498 /* No need to recover sufile because it will be destroyed on error */
Ryusuke Konishi85c2a742009-04-28 23:38:46 +0900499 nilfs_detach_writer(nilfs, sbi);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700500 return err;
501}
502
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900503static int nilfs_recovery_copy_block(struct the_nilfs *nilfs,
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700504 struct nilfs_recovery_block *rb,
505 struct page *page)
506{
507 struct buffer_head *bh_org;
508 void *kaddr;
509
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900510 bh_org = __bread(nilfs->ns_bdev, rb->blocknr, nilfs->ns_blocksize);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700511 if (unlikely(!bh_org))
512 return -EIO;
513
514 kaddr = kmap_atomic(page, KM_USER0);
515 memcpy(kaddr + bh_offset(bh_org), bh_org->b_data, bh_org->b_size);
516 kunmap_atomic(kaddr, KM_USER0);
517 brelse(bh_org);
518 return 0;
519}
520
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900521static int nilfs_recover_dsync_blocks(struct the_nilfs *nilfs,
522 struct nilfs_sb_info *sbi,
523 struct list_head *head,
524 unsigned long *nr_salvaged_blocks)
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700525{
526 struct inode *inode;
527 struct nilfs_recovery_block *rb, *n;
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900528 unsigned blocksize = nilfs->ns_blocksize;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700529 struct page *page;
530 loff_t pos;
531 int err = 0, err2 = 0;
532
533 list_for_each_entry_safe(rb, n, head, list) {
534 inode = nilfs_iget(sbi->s_super, rb->ino);
535 if (IS_ERR(inode)) {
536 err = PTR_ERR(inode);
537 inode = NULL;
538 goto failed_inode;
539 }
540
541 pos = rb->blkoff << inode->i_blkbits;
542 page = NULL;
543 err = block_write_begin(NULL, inode->i_mapping, pos, blocksize,
544 0, &page, NULL, nilfs_get_block);
545 if (unlikely(err))
546 goto failed_inode;
547
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900548 err = nilfs_recovery_copy_block(nilfs, rb, page);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700549 if (unlikely(err))
550 goto failed_page;
551
552 err = nilfs_set_file_dirty(sbi, inode, 1);
553 if (unlikely(err))
554 goto failed_page;
555
556 block_write_end(NULL, inode->i_mapping, pos, blocksize,
557 blocksize, page, NULL);
558
559 unlock_page(page);
560 page_cache_release(page);
561
562 (*nr_salvaged_blocks)++;
563 goto next;
564
565 failed_page:
566 unlock_page(page);
567 page_cache_release(page);
568
569 failed_inode:
570 printk(KERN_WARNING
571 "NILFS warning: error recovering data block "
572 "(err=%d, ino=%lu, block-offset=%llu)\n",
Heiko Carstensb5696e52009-09-03 17:42:48 +0200573 err, (unsigned long)rb->ino,
574 (unsigned long long)rb->blkoff);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700575 if (!err2)
576 err2 = err;
577 next:
578 iput(inode); /* iput(NULL) is just ignored */
579 list_del_init(&rb->list);
580 kfree(rb);
581 }
582 return err2;
583}
584
585/**
586 * nilfs_do_roll_forward - salvage logical segments newer than the latest
587 * checkpoint
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900588 * @nilfs: nilfs object
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700589 * @sbi: nilfs_sb_info
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700590 * @ri: pointer to a nilfs_recovery_info
591 */
592static int nilfs_do_roll_forward(struct the_nilfs *nilfs,
593 struct nilfs_sb_info *sbi,
594 struct nilfs_recovery_info *ri)
595{
596 struct nilfs_segsum_info ssi;
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900597 struct buffer_head *bh_sum = NULL;
598 struct nilfs_segment_summary *sum;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700599 sector_t pseg_start;
600 sector_t seg_start, seg_end; /* Starting/ending DBN of full segment */
601 unsigned long nsalvaged_blocks = 0;
602 u64 seg_seq;
603 __u64 segnum, nextnum = 0;
604 int empty_seg = 0;
605 int err = 0, ret;
606 LIST_HEAD(dsync_blocks); /* list of data blocks to be recovered */
607 enum {
608 RF_INIT_ST,
609 RF_DSYNC_ST, /* scanning data-sync segments */
610 };
611 int state = RF_INIT_ST;
612
613 nilfs_attach_writer(nilfs, sbi);
614 pseg_start = ri->ri_lsegs_start;
615 seg_seq = ri->ri_lsegs_start_seq;
616 segnum = nilfs_get_segnum_of_block(nilfs, pseg_start);
617 nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
618
619 while (segnum != ri->ri_segnum || pseg_start <= ri->ri_pseg_start) {
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900620 brelse(bh_sum);
621 bh_sum = nilfs_read_log_header(nilfs, pseg_start, &sum);
622 if (!bh_sum) {
623 err = -EIO;
624 goto failed;
625 }
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700626
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900627 ret = nilfs_validate_log(nilfs, seg_seq, bh_sum, sum);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700628 if (ret) {
629 if (ret == NILFS_SEG_FAIL_IO) {
630 err = -EIO;
631 goto failed;
632 }
633 goto strayed;
634 }
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900635
636 store_segsum_info(&ssi, sum, nilfs->ns_blocksize);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700637 if (unlikely(NILFS_SEG_HAS_SR(&ssi)))
638 goto confused;
639
640 /* Found a valid partial segment; do recovery actions */
641 nextnum = nilfs_get_segnum_of_block(nilfs, ssi.next);
642 empty_seg = 0;
643 nilfs->ns_ctime = ssi.ctime;
644 if (!(ssi.flags & NILFS_SS_GC))
645 nilfs->ns_nongc_ctime = ssi.ctime;
646
647 switch (state) {
648 case RF_INIT_ST:
649 if (!NILFS_SEG_LOGBGN(&ssi) || !NILFS_SEG_DSYNC(&ssi))
650 goto try_next_pseg;
651 state = RF_DSYNC_ST;
652 /* Fall through */
653 case RF_DSYNC_ST:
654 if (!NILFS_SEG_DSYNC(&ssi))
655 goto confused;
656
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900657 err = nilfs_scan_dsync_log(nilfs, pseg_start, &ssi,
658 &dsync_blocks);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700659 if (unlikely(err))
660 goto failed;
661 if (NILFS_SEG_LOGEND(&ssi)) {
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900662 err = nilfs_recover_dsync_blocks(
663 nilfs, sbi, &dsync_blocks,
664 &nsalvaged_blocks);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700665 if (unlikely(err))
666 goto failed;
667 state = RF_INIT_ST;
668 }
669 break; /* Fall through to try_next_pseg */
670 }
671
672 try_next_pseg:
673 if (pseg_start == ri->ri_lsegs_end)
674 break;
675 pseg_start += ssi.nblocks;
676 if (pseg_start < seg_end)
677 continue;
678 goto feed_segment;
679
680 strayed:
681 if (pseg_start == ri->ri_lsegs_end)
682 break;
683
684 feed_segment:
685 /* Looking to the next full segment */
686 if (empty_seg++)
687 break;
688 seg_seq++;
689 segnum = nextnum;
690 nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
691 pseg_start = seg_start;
692 }
693
694 if (nsalvaged_blocks) {
695 printk(KERN_INFO "NILFS (device %s): salvaged %lu blocks\n",
696 sbi->s_super->s_id, nsalvaged_blocks);
697 ri->ri_need_recovery = NILFS_RECOVERY_ROLLFORWARD_DONE;
698 }
699 out:
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900700 brelse(bh_sum);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700701 dispose_recovery_list(&dsync_blocks);
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900702 nilfs_detach_writer(nilfs, sbi);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700703 return err;
704
705 confused:
706 err = -EINVAL;
707 failed:
708 printk(KERN_ERR
709 "NILFS (device %s): Error roll-forwarding "
710 "(err=%d, pseg block=%llu). ",
711 sbi->s_super->s_id, err, (unsigned long long)pseg_start);
712 goto out;
713}
714
715static void nilfs_finish_roll_forward(struct the_nilfs *nilfs,
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700716 struct nilfs_recovery_info *ri)
717{
718 struct buffer_head *bh;
719 int err;
720
721 if (nilfs_get_segnum_of_block(nilfs, ri->ri_lsegs_start) !=
722 nilfs_get_segnum_of_block(nilfs, ri->ri_super_root))
723 return;
724
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900725 bh = __getblk(nilfs->ns_bdev, ri->ri_lsegs_start, nilfs->ns_blocksize);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700726 BUG_ON(!bh);
727 memset(bh->b_data, 0, bh->b_size);
728 set_buffer_dirty(bh);
729 err = sync_dirty_buffer(bh);
730 if (unlikely(err))
731 printk(KERN_WARNING
732 "NILFS warning: buffer sync write failed during "
733 "post-cleaning of recovery.\n");
734 brelse(bh);
735}
736
737/**
Ryusuke Konishiaee5ce22010-05-23 12:21:57 +0900738 * nilfs_salvage_orphan_logs - salvage logs written after the latest checkpoint
739 * @nilfs: nilfs object
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700740 * @sbi: nilfs_sb_info
741 * @ri: pointer to a nilfs_recovery_info struct to store search results.
742 *
743 * Return Value: On success, 0 is returned. On error, one of the following
744 * negative error code is returned.
745 *
746 * %-EINVAL - Inconsistent filesystem state.
747 *
748 * %-EIO - I/O error
749 *
750 * %-ENOSPC - No space left on device (only in a panic state).
751 *
752 * %-ERESTARTSYS - Interrupted.
753 *
754 * %-ENOMEM - Insufficient memory available.
755 */
Ryusuke Konishiaee5ce22010-05-23 12:21:57 +0900756int nilfs_salvage_orphan_logs(struct the_nilfs *nilfs,
757 struct nilfs_sb_info *sbi,
758 struct nilfs_recovery_info *ri)
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700759{
760 int err;
761
762 if (ri->ri_lsegs_start == 0 || ri->ri_lsegs_end == 0)
763 return 0;
764
765 err = nilfs_attach_checkpoint(sbi, ri->ri_cno);
766 if (unlikely(err)) {
767 printk(KERN_ERR
768 "NILFS: error loading the latest checkpoint.\n");
769 return err;
770 }
771
772 err = nilfs_do_roll_forward(nilfs, sbi, ri);
773 if (unlikely(err))
774 goto failed;
775
776 if (ri->ri_need_recovery == NILFS_RECOVERY_ROLLFORWARD_DONE) {
Ryusuke Konishi85c2a742009-04-28 23:38:46 +0900777 err = nilfs_prepare_segment_for_recovery(nilfs, sbi, ri);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700778 if (unlikely(err)) {
779 printk(KERN_ERR "NILFS: Error preparing segments for "
780 "recovery.\n");
781 goto failed;
782 }
783
Ryusuke Konishicece5522009-04-06 19:01:58 -0700784 err = nilfs_attach_segment_constructor(sbi);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700785 if (unlikely(err))
786 goto failed;
787
788 set_nilfs_discontinued(nilfs);
789 err = nilfs_construct_segment(sbi->s_super);
790 nilfs_detach_segment_constructor(sbi);
791
792 if (unlikely(err)) {
793 printk(KERN_ERR "NILFS: Oops! recovery failed. "
794 "(err=%d)\n", err);
795 goto failed;
796 }
797
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900798 nilfs_finish_roll_forward(nilfs, ri);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700799 }
800
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700801 failed:
802 nilfs_detach_checkpoint(sbi);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700803 return err;
804}
805
806/**
807 * nilfs_search_super_root - search the latest valid super root
808 * @nilfs: the_nilfs
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700809 * @ri: pointer to a nilfs_recovery_info struct to store search results.
810 *
811 * nilfs_search_super_root() looks for the latest super-root from a partial
812 * segment pointed by the superblock. It sets up struct the_nilfs through
813 * this search. It fills nilfs_recovery_info (ri) required for recovery.
814 *
815 * Return Value: On success, 0 is returned. On error, one of the following
816 * negative error code is returned.
817 *
818 * %-EINVAL - No valid segment found
819 *
820 * %-EIO - I/O error
821 */
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900822int nilfs_search_super_root(struct the_nilfs *nilfs,
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700823 struct nilfs_recovery_info *ri)
824{
825 struct nilfs_segsum_info ssi;
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900826 struct buffer_head *bh_sum = NULL;
827 struct nilfs_segment_summary *sum;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700828 sector_t pseg_start, pseg_end, sr_pseg_start = 0;
829 sector_t seg_start, seg_end; /* range of full segment (block number) */
Ryusuke Konishi050b4142009-11-19 22:24:48 +0900830 sector_t b, end;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700831 u64 seg_seq;
832 __u64 segnum, nextnum = 0;
833 __u64 cno;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700834 LIST_HEAD(segments);
835 int empty_seg = 0, scan_newer = 0;
836 int ret;
837
838 pseg_start = nilfs->ns_last_pseg;
839 seg_seq = nilfs->ns_last_seq;
840 cno = nilfs->ns_last_cno;
841 segnum = nilfs_get_segnum_of_block(nilfs, pseg_start);
842
843 /* Calculate range of segment */
844 nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
845
Ryusuke Konishi050b4142009-11-19 22:24:48 +0900846 /* Read ahead segment */
847 b = seg_start;
848 while (b <= seg_end)
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900849 __breadahead(nilfs->ns_bdev, b++, nilfs->ns_blocksize);
Ryusuke Konishi050b4142009-11-19 22:24:48 +0900850
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700851 for (;;) {
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900852 brelse(bh_sum);
853 ret = NILFS_SEG_FAIL_IO;
854 bh_sum = nilfs_read_log_header(nilfs, pseg_start, &sum);
855 if (!bh_sum)
856 goto failed;
857
858 ret = nilfs_validate_log(nilfs, seg_seq, bh_sum, sum);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700859 if (ret) {
860 if (ret == NILFS_SEG_FAIL_IO)
861 goto failed;
862 goto strayed;
863 }
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900864
865 store_segsum_info(&ssi, sum, nilfs->ns_blocksize);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700866 pseg_end = pseg_start + ssi.nblocks - 1;
867 if (unlikely(pseg_end > seg_end)) {
868 ret = NILFS_SEG_FAIL_CONSISTENCY;
869 goto strayed;
870 }
871
872 /* A valid partial segment */
873 ri->ri_pseg_start = pseg_start;
874 ri->ri_seq = seg_seq;
875 ri->ri_segnum = segnum;
876 nextnum = nilfs_get_segnum_of_block(nilfs, ssi.next);
877 ri->ri_nextnum = nextnum;
878 empty_seg = 0;
879
Ryusuke Konishi050b4142009-11-19 22:24:48 +0900880 if (!NILFS_SEG_HAS_SR(&ssi) && !scan_newer) {
881 /* This will never happen because a superblock
882 (last_segment) always points to a pseg
883 having a super root. */
884 ret = NILFS_SEG_FAIL_CONSISTENCY;
885 goto failed;
886 }
887
888 if (pseg_start == seg_start) {
889 nilfs_get_segment_range(nilfs, nextnum, &b, &end);
890 while (b <= end)
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900891 __breadahead(nilfs->ns_bdev, b++,
892 nilfs->ns_blocksize);
Ryusuke Konishi050b4142009-11-19 22:24:48 +0900893 }
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700894 if (!NILFS_SEG_HAS_SR(&ssi)) {
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700895 if (!ri->ri_lsegs_start && NILFS_SEG_LOGBGN(&ssi)) {
896 ri->ri_lsegs_start = pseg_start;
897 ri->ri_lsegs_start_seq = seg_seq;
898 }
899 if (NILFS_SEG_LOGEND(&ssi))
900 ri->ri_lsegs_end = pseg_start;
901 goto try_next_pseg;
902 }
903
904 /* A valid super root was found. */
905 ri->ri_cno = cno++;
906 ri->ri_super_root = pseg_end;
907 ri->ri_lsegs_start = ri->ri_lsegs_end = 0;
908
909 nilfs_dispose_segment_list(&segments);
910 nilfs->ns_pseg_offset = (sr_pseg_start = pseg_start)
911 + ssi.nblocks - seg_start;
912 nilfs->ns_seg_seq = seg_seq;
913 nilfs->ns_segnum = segnum;
914 nilfs->ns_cno = cno; /* nilfs->ns_cno = ri->ri_cno + 1 */
915 nilfs->ns_ctime = ssi.ctime;
916 nilfs->ns_nextnum = nextnum;
917
918 if (scan_newer)
919 ri->ri_need_recovery = NILFS_RECOVERY_SR_UPDATED;
Ryusuke Konishi2c2e52f2009-04-06 19:01:54 -0700920 else {
Ryusuke Konishi2c2e52f2009-04-06 19:01:54 -0700921 if (nilfs->ns_mount_state & NILFS_VALID_FS)
922 goto super_root_found;
923 scan_newer = 1;
924 }
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700925
926 /* reset region for roll-forward */
927 pseg_start += ssi.nblocks;
928 if (pseg_start < seg_end)
929 continue;
930 goto feed_segment;
931
932 try_next_pseg:
933 /* Standing on a course, or met an inconsistent state */
934 pseg_start += ssi.nblocks;
935 if (pseg_start < seg_end)
936 continue;
937 goto feed_segment;
938
939 strayed:
940 /* Off the trail */
941 if (!scan_newer)
942 /*
943 * This can happen if a checkpoint was written without
944 * barriers, or as a result of an I/O failure.
945 */
946 goto failed;
947
948 feed_segment:
949 /* Looking to the next full segment */
950 if (empty_seg++)
951 goto super_root_found; /* found a valid super root */
952
Ryusuke Konishi654137d2009-05-17 19:07:21 +0900953 ret = nilfs_segment_list_add(&segments, segnum);
954 if (unlikely(ret))
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700955 goto failed;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700956
957 seg_seq++;
958 segnum = nextnum;
959 nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
960 pseg_start = seg_start;
961 }
962
963 super_root_found:
964 /* Updating pointers relating to the latest checkpoint */
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900965 brelse(bh_sum);
Ryusuke Konishi0935db72009-11-29 02:39:11 +0900966 list_splice_tail(&segments, &ri->ri_used_segments);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700967 nilfs->ns_last_pseg = sr_pseg_start;
968 nilfs->ns_last_seq = nilfs->ns_seg_seq;
969 nilfs->ns_last_cno = ri->ri_cno;
970 return 0;
971
972 failed:
Ryusuke Konishi354fa8b2010-05-23 19:21:49 +0900973 brelse(bh_sum);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700974 nilfs_dispose_segment_list(&segments);
975 return (ret < 0) ? ret : nilfs_warn_segment_error(ret);
976}