blob: ef387b19682ca7d7bf044a5514f6db34f9b9da84 [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>
26#include <linux/crc32.h>
27#include "nilfs.h"
28#include "segment.h"
29#include "sufile.h"
30#include "page.h"
31#include "seglist.h"
32#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,
43 NILFS_SEG_FAIL_CHECKSUM_SEGSUM,
44 NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT,
45 NILFS_SEG_FAIL_CHECKSUM_FULL,
46 NILFS_SEG_FAIL_CONSISTENCY,
47};
48
49/* work structure for recovery */
50struct nilfs_recovery_block {
51 ino_t ino; /* Inode number of the file that this block
52 belongs to */
53 sector_t blocknr; /* block number */
54 __u64 vblocknr; /* virtual block number */
55 unsigned long blkoff; /* File offset of the data block (per block) */
56 struct list_head list;
57};
58
59
60static int nilfs_warn_segment_error(int err)
61{
62 switch (err) {
63 case NILFS_SEG_FAIL_IO:
64 printk(KERN_WARNING
65 "NILFS warning: I/O error on loading last segment\n");
66 return -EIO;
67 case NILFS_SEG_FAIL_MAGIC:
68 printk(KERN_WARNING
69 "NILFS warning: Segment magic number invalid\n");
70 break;
71 case NILFS_SEG_FAIL_SEQ:
72 printk(KERN_WARNING
73 "NILFS warning: Sequence number mismatch\n");
74 break;
75 case NILFS_SEG_FAIL_CHECKSUM_SEGSUM:
76 printk(KERN_WARNING
77 "NILFS warning: Checksum error in segment summary\n");
78 break;
79 case NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT:
80 printk(KERN_WARNING
81 "NILFS warning: Checksum error in super root\n");
82 break;
83 case NILFS_SEG_FAIL_CHECKSUM_FULL:
84 printk(KERN_WARNING
85 "NILFS warning: Checksum error in segment payload\n");
86 break;
87 case NILFS_SEG_FAIL_CONSISTENCY:
88 printk(KERN_WARNING
89 "NILFS warning: Inconsistent segment\n");
90 break;
91 case NILFS_SEG_NO_SUPER_ROOT:
92 printk(KERN_WARNING
93 "NILFS warning: No super root in the last segment\n");
94 break;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -070095 }
96 return -EINVAL;
97}
98
99static void store_segsum_info(struct nilfs_segsum_info *ssi,
100 struct nilfs_segment_summary *sum,
101 unsigned int blocksize)
102{
103 ssi->flags = le16_to_cpu(sum->ss_flags);
104 ssi->seg_seq = le64_to_cpu(sum->ss_seq);
105 ssi->ctime = le64_to_cpu(sum->ss_create);
106 ssi->next = le64_to_cpu(sum->ss_next);
107 ssi->nblocks = le32_to_cpu(sum->ss_nblocks);
108 ssi->nfinfo = le32_to_cpu(sum->ss_nfinfo);
109 ssi->sumbytes = le32_to_cpu(sum->ss_sumbytes);
110
111 ssi->nsumblk = DIV_ROUND_UP(ssi->sumbytes, blocksize);
112 ssi->nfileblk = ssi->nblocks - ssi->nsumblk - !!NILFS_SEG_HAS_SR(ssi);
113}
114
115/**
116 * calc_crc_cont - check CRC of blocks continuously
117 * @sbi: nilfs_sb_info
118 * @bhs: buffer head of start block
119 * @sum: place to store result
120 * @offset: offset bytes in the first block
121 * @check_bytes: number of bytes to be checked
122 * @start: DBN of start block
123 * @nblock: number of blocks to be checked
124 */
125static int calc_crc_cont(struct nilfs_sb_info *sbi, struct buffer_head *bhs,
126 u32 *sum, unsigned long offset, u64 check_bytes,
127 sector_t start, unsigned long nblock)
128{
129 unsigned long blocksize = sbi->s_super->s_blocksize;
130 unsigned long size;
131 u32 crc;
132
133 BUG_ON(offset >= blocksize);
134 check_bytes -= offset;
135 size = min_t(u64, check_bytes, blocksize - offset);
136 crc = crc32_le(sbi->s_nilfs->ns_crc_seed,
137 (unsigned char *)bhs->b_data + offset, size);
138 if (--nblock > 0) {
139 do {
140 struct buffer_head *bh
141 = sb_bread(sbi->s_super, ++start);
142 if (!bh)
143 return -EIO;
144 check_bytes -= size;
145 size = min_t(u64, check_bytes, blocksize);
146 crc = crc32_le(crc, bh->b_data, size);
147 brelse(bh);
148 } while (--nblock > 0);
149 }
150 *sum = crc;
151 return 0;
152}
153
154/**
155 * nilfs_read_super_root_block - read super root block
156 * @sb: super_block
157 * @sr_block: disk block number of the super root block
158 * @pbh: address of a buffer_head pointer to return super root buffer
159 * @check: CRC check flag
160 */
161int nilfs_read_super_root_block(struct super_block *sb, sector_t sr_block,
162 struct buffer_head **pbh, int check)
163{
164 struct buffer_head *bh_sr;
165 struct nilfs_super_root *sr;
166 u32 crc;
167 int ret;
168
169 *pbh = NULL;
170 bh_sr = sb_bread(sb, sr_block);
171 if (unlikely(!bh_sr)) {
172 ret = NILFS_SEG_FAIL_IO;
173 goto failed;
174 }
175
176 sr = (struct nilfs_super_root *)bh_sr->b_data;
177 if (check) {
178 unsigned bytes = le16_to_cpu(sr->sr_bytes);
179
180 if (bytes == 0 || bytes > sb->s_blocksize) {
181 ret = NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT;
182 goto failed_bh;
183 }
184 if (calc_crc_cont(NILFS_SB(sb), bh_sr, &crc,
185 sizeof(sr->sr_sum), bytes, sr_block, 1)) {
186 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/**
205 * load_segment_summary - read segment summary of the specified partial segment
206 * @sbi: nilfs_sb_info
207 * @pseg_start: start disk block number of partial segment
208 * @seg_seq: sequence number requested
209 * @ssi: pointer to nilfs_segsum_info struct to store information
210 * @full_check: full check flag
211 * (0: only checks segment summary CRC, 1: data CRC)
212 */
213static int
214load_segment_summary(struct nilfs_sb_info *sbi, sector_t pseg_start,
215 u64 seg_seq, struct nilfs_segsum_info *ssi,
216 int full_check)
217{
218 struct buffer_head *bh_sum;
219 struct nilfs_segment_summary *sum;
220 unsigned long offset, nblock;
221 u64 check_bytes;
222 u32 crc, crc_sum;
223 int ret = NILFS_SEG_FAIL_IO;
224
225 bh_sum = sb_bread(sbi->s_super, pseg_start);
226 if (!bh_sum)
227 goto out;
228
229 sum = (struct nilfs_segment_summary *)bh_sum->b_data;
230
231 /* Check consistency of segment summary */
232 if (le32_to_cpu(sum->ss_magic) != NILFS_SEGSUM_MAGIC) {
233 ret = NILFS_SEG_FAIL_MAGIC;
234 goto failed;
235 }
236 store_segsum_info(ssi, sum, sbi->s_super->s_blocksize);
237 if (seg_seq != ssi->seg_seq) {
238 ret = NILFS_SEG_FAIL_SEQ;
239 goto failed;
240 }
241 if (full_check) {
242 offset = sizeof(sum->ss_datasum);
243 check_bytes =
244 ((u64)ssi->nblocks << sbi->s_super->s_blocksize_bits);
245 nblock = ssi->nblocks;
246 crc_sum = le32_to_cpu(sum->ss_datasum);
247 ret = NILFS_SEG_FAIL_CHECKSUM_FULL;
248 } else { /* only checks segment summary */
249 offset = sizeof(sum->ss_datasum) + sizeof(sum->ss_sumsum);
250 check_bytes = ssi->sumbytes;
251 nblock = ssi->nsumblk;
252 crc_sum = le32_to_cpu(sum->ss_sumsum);
253 ret = NILFS_SEG_FAIL_CHECKSUM_SEGSUM;
254 }
255
256 if (unlikely(nblock == 0 ||
257 nblock > sbi->s_nilfs->ns_blocks_per_segment)) {
258 /* This limits the number of blocks read in the CRC check */
259 ret = NILFS_SEG_FAIL_CONSISTENCY;
260 goto failed;
261 }
262 if (calc_crc_cont(sbi, bh_sum, &crc, offset, check_bytes,
263 pseg_start, nblock)) {
264 ret = NILFS_SEG_FAIL_IO;
265 goto failed;
266 }
267 if (crc == crc_sum)
268 ret = 0;
269 failed:
270 brelse(bh_sum);
271 out:
272 return ret;
273}
274
275static void *segsum_get(struct super_block *sb, struct buffer_head **pbh,
276 unsigned int *offset, unsigned int bytes)
277{
278 void *ptr;
279 sector_t blocknr;
280
281 BUG_ON((*pbh)->b_size < *offset);
282 if (bytes > (*pbh)->b_size - *offset) {
283 blocknr = (*pbh)->b_blocknr;
284 brelse(*pbh);
285 *pbh = sb_bread(sb, blocknr + 1);
286 if (unlikely(!*pbh))
287 return NULL;
288 *offset = 0;
289 }
290 ptr = (*pbh)->b_data + *offset;
291 *offset += bytes;
292 return ptr;
293}
294
295static void segsum_skip(struct super_block *sb, struct buffer_head **pbh,
296 unsigned int *offset, unsigned int bytes,
297 unsigned long count)
298{
299 unsigned int rest_item_in_current_block
300 = ((*pbh)->b_size - *offset) / bytes;
301
302 if (count <= rest_item_in_current_block) {
303 *offset += bytes * count;
304 } else {
305 sector_t blocknr = (*pbh)->b_blocknr;
306 unsigned int nitem_per_block = (*pbh)->b_size / bytes;
307 unsigned int bcnt;
308
309 count -= rest_item_in_current_block;
310 bcnt = DIV_ROUND_UP(count, nitem_per_block);
311 *offset = bytes * (count - (bcnt - 1) * nitem_per_block);
312
313 brelse(*pbh);
314 *pbh = sb_bread(sb, blocknr + bcnt);
315 }
316}
317
318static int
319collect_blocks_from_segsum(struct nilfs_sb_info *sbi, sector_t sum_blocknr,
320 struct nilfs_segsum_info *ssi,
321 struct list_head *head)
322{
323 struct buffer_head *bh;
324 unsigned int offset;
325 unsigned long nfinfo = ssi->nfinfo;
326 sector_t blocknr = sum_blocknr + ssi->nsumblk;
327 ino_t ino;
328 int err = -EIO;
329
330 if (!nfinfo)
331 return 0;
332
333 bh = sb_bread(sbi->s_super, sum_blocknr);
334 if (unlikely(!bh))
335 goto out;
336
337 offset = le16_to_cpu(
338 ((struct nilfs_segment_summary *)bh->b_data)->ss_bytes);
339 for (;;) {
340 unsigned long nblocks, ndatablk, nnodeblk;
341 struct nilfs_finfo *finfo;
342
343 finfo = segsum_get(sbi->s_super, &bh, &offset, sizeof(*finfo));
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
356 binfo = segsum_get(sbi->s_super, &bh, &offset,
357 sizeof(*binfo));
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;
375 blocknr += nnodeblk; /* always 0 for the data sync segments */
376 segsum_skip(sbi->s_super, &bh, &offset, sizeof(__le64),
377 nnodeblk);
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
387static 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
398void nilfs_dispose_segment_list(struct list_head *head)
399{
400 while (!list_empty(head)) {
401 struct nilfs_segment_entry *ent
402 = list_entry(head->next,
403 struct nilfs_segment_entry, list);
404 list_del(&ent->list);
405 nilfs_free_segment_entry(ent);
406 }
407}
408
409static int nilfs_prepare_segment_for_recovery(struct the_nilfs *nilfs,
410 struct nilfs_recovery_info *ri)
411{
412 struct list_head *head = &ri->ri_used_segments;
413 struct nilfs_segment_entry *ent, *n;
414 struct inode *sufile = nilfs->ns_sufile;
415 __u64 segnum[4];
Ryusuke Konishi2c2e52f2009-04-06 19:01:54 -0700416 time_t mtime;
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700417 int err;
418 int i;
419
420 segnum[0] = nilfs->ns_segnum;
421 segnum[1] = nilfs->ns_nextnum;
422 segnum[2] = ri->ri_segnum;
423 segnum[3] = ri->ri_nextnum;
424
425 /*
426 * Releasing the next segment of the latest super root.
427 * The next segment is invalidated by this recovery.
428 */
429 err = nilfs_sufile_free(sufile, segnum[1]);
430 if (unlikely(err))
431 goto failed;
432
433 err = -ENOMEM;
434 for (i = 1; i < 4; i++) {
435 ent = nilfs_alloc_segment_entry(segnum[i]);
436 if (unlikely(!ent))
437 goto failed;
438 list_add_tail(&ent->list, head);
439 }
440
441 /*
442 * Collecting segments written after the latest super root.
Ryusuke Konishi2c2e52f2009-04-06 19:01:54 -0700443 * These are marked dirty to avoid being reallocated in the next write.
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700444 */
Ryusuke Konishi2c2e52f2009-04-06 19:01:54 -0700445 mtime = get_seconds();
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700446 list_for_each_entry_safe(ent, n, head, list) {
447 if (ent->segnum == segnum[0]) {
448 list_del(&ent->list);
449 nilfs_free_segment_entry(ent);
450 continue;
451 }
452 err = nilfs_open_segment_entry(ent, sufile);
453 if (unlikely(err))
454 goto failed;
Ryusuke Konishi2c2e52f2009-04-06 19:01:54 -0700455 if (!nilfs_segment_usage_dirty(ent->raw_su)) {
456 /* make the segment garbage */
457 ent->raw_su->su_nblocks = cpu_to_le32(0);
458 ent->raw_su->su_lastmod = cpu_to_le32(mtime);
459 nilfs_segment_usage_set_dirty(ent->raw_su);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700460 }
Ryusuke Konishi2c2e52f2009-04-06 19:01:54 -0700461 list_del(&ent->list);
462 nilfs_close_segment_entry(ent, sufile);
463 nilfs_free_segment_entry(ent);
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700464 }
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700465
466 /*
467 * The segment having the latest super root is active, and
468 * should be deactivated on the next construction for recovery.
469 */
470 err = -ENOMEM;
471 ent = nilfs_alloc_segment_entry(segnum[0]);
472 if (unlikely(!ent))
473 goto failed;
474 list_add_tail(&ent->list, &ri->ri_used_segments);
475
476 /* Allocate new segments for recovery */
477 err = nilfs_sufile_alloc(sufile, &segnum[0]);
478 if (unlikely(err))
479 goto failed;
480
481 nilfs->ns_pseg_offset = 0;
482 nilfs->ns_seg_seq = ri->ri_seq + 2;
483 nilfs->ns_nextnum = nilfs->ns_segnum = segnum[0];
484 return 0;
485
486 failed:
487 /* No need to recover sufile because it will be destroyed on error */
488 return err;
489}
490
491static int nilfs_recovery_copy_block(struct nilfs_sb_info *sbi,
492 struct nilfs_recovery_block *rb,
493 struct page *page)
494{
495 struct buffer_head *bh_org;
496 void *kaddr;
497
498 bh_org = sb_bread(sbi->s_super, rb->blocknr);
499 if (unlikely(!bh_org))
500 return -EIO;
501
502 kaddr = kmap_atomic(page, KM_USER0);
503 memcpy(kaddr + bh_offset(bh_org), bh_org->b_data, bh_org->b_size);
504 kunmap_atomic(kaddr, KM_USER0);
505 brelse(bh_org);
506 return 0;
507}
508
509static int recover_dsync_blocks(struct nilfs_sb_info *sbi,
510 struct list_head *head,
511 unsigned long *nr_salvaged_blocks)
512{
513 struct inode *inode;
514 struct nilfs_recovery_block *rb, *n;
515 unsigned blocksize = sbi->s_super->s_blocksize;
516 struct page *page;
517 loff_t pos;
518 int err = 0, err2 = 0;
519
520 list_for_each_entry_safe(rb, n, head, list) {
521 inode = nilfs_iget(sbi->s_super, rb->ino);
522 if (IS_ERR(inode)) {
523 err = PTR_ERR(inode);
524 inode = NULL;
525 goto failed_inode;
526 }
527
528 pos = rb->blkoff << inode->i_blkbits;
529 page = NULL;
530 err = block_write_begin(NULL, inode->i_mapping, pos, blocksize,
531 0, &page, NULL, nilfs_get_block);
532 if (unlikely(err))
533 goto failed_inode;
534
535 err = nilfs_recovery_copy_block(sbi, rb, page);
536 if (unlikely(err))
537 goto failed_page;
538
539 err = nilfs_set_file_dirty(sbi, inode, 1);
540 if (unlikely(err))
541 goto failed_page;
542
543 block_write_end(NULL, inode->i_mapping, pos, blocksize,
544 blocksize, page, NULL);
545
546 unlock_page(page);
547 page_cache_release(page);
548
549 (*nr_salvaged_blocks)++;
550 goto next;
551
552 failed_page:
553 unlock_page(page);
554 page_cache_release(page);
555
556 failed_inode:
557 printk(KERN_WARNING
558 "NILFS warning: error recovering data block "
559 "(err=%d, ino=%lu, block-offset=%llu)\n",
560 err, rb->ino, (unsigned long long)rb->blkoff);
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
574 * @sbi: nilfs_sb_info
575 * @nilfs: the_nilfs
576 * @ri: pointer to a nilfs_recovery_info
577 */
578static int nilfs_do_roll_forward(struct the_nilfs *nilfs,
579 struct nilfs_sb_info *sbi,
580 struct nilfs_recovery_info *ri)
581{
582 struct nilfs_segsum_info ssi;
583 sector_t pseg_start;
584 sector_t seg_start, seg_end; /* Starting/ending DBN of full segment */
585 unsigned long nsalvaged_blocks = 0;
586 u64 seg_seq;
587 __u64 segnum, nextnum = 0;
588 int empty_seg = 0;
589 int err = 0, ret;
590 LIST_HEAD(dsync_blocks); /* list of data blocks to be recovered */
591 enum {
592 RF_INIT_ST,
593 RF_DSYNC_ST, /* scanning data-sync segments */
594 };
595 int state = RF_INIT_ST;
596
597 nilfs_attach_writer(nilfs, sbi);
598 pseg_start = ri->ri_lsegs_start;
599 seg_seq = ri->ri_lsegs_start_seq;
600 segnum = nilfs_get_segnum_of_block(nilfs, pseg_start);
601 nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
602
603 while (segnum != ri->ri_segnum || pseg_start <= ri->ri_pseg_start) {
604
605 ret = load_segment_summary(sbi, pseg_start, seg_seq, &ssi, 1);
606 if (ret) {
607 if (ret == NILFS_SEG_FAIL_IO) {
608 err = -EIO;
609 goto failed;
610 }
611 goto strayed;
612 }
613 if (unlikely(NILFS_SEG_HAS_SR(&ssi)))
614 goto confused;
615
616 /* Found a valid partial segment; do recovery actions */
617 nextnum = nilfs_get_segnum_of_block(nilfs, ssi.next);
618 empty_seg = 0;
619 nilfs->ns_ctime = ssi.ctime;
620 if (!(ssi.flags & NILFS_SS_GC))
621 nilfs->ns_nongc_ctime = ssi.ctime;
622
623 switch (state) {
624 case RF_INIT_ST:
625 if (!NILFS_SEG_LOGBGN(&ssi) || !NILFS_SEG_DSYNC(&ssi))
626 goto try_next_pseg;
627 state = RF_DSYNC_ST;
628 /* Fall through */
629 case RF_DSYNC_ST:
630 if (!NILFS_SEG_DSYNC(&ssi))
631 goto confused;
632
633 err = collect_blocks_from_segsum(
634 sbi, pseg_start, &ssi, &dsync_blocks);
635 if (unlikely(err))
636 goto failed;
637 if (NILFS_SEG_LOGEND(&ssi)) {
638 err = recover_dsync_blocks(
639 sbi, &dsync_blocks, &nsalvaged_blocks);
640 if (unlikely(err))
641 goto failed;
642 state = RF_INIT_ST;
643 }
644 break; /* Fall through to try_next_pseg */
645 }
646
647 try_next_pseg:
648 if (pseg_start == ri->ri_lsegs_end)
649 break;
650 pseg_start += ssi.nblocks;
651 if (pseg_start < seg_end)
652 continue;
653 goto feed_segment;
654
655 strayed:
656 if (pseg_start == ri->ri_lsegs_end)
657 break;
658
659 feed_segment:
660 /* Looking to the next full segment */
661 if (empty_seg++)
662 break;
663 seg_seq++;
664 segnum = nextnum;
665 nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
666 pseg_start = seg_start;
667 }
668
669 if (nsalvaged_blocks) {
670 printk(KERN_INFO "NILFS (device %s): salvaged %lu blocks\n",
671 sbi->s_super->s_id, nsalvaged_blocks);
672 ri->ri_need_recovery = NILFS_RECOVERY_ROLLFORWARD_DONE;
673 }
674 out:
675 dispose_recovery_list(&dsync_blocks);
676 nilfs_detach_writer(sbi->s_nilfs, sbi);
677 return err;
678
679 confused:
680 err = -EINVAL;
681 failed:
682 printk(KERN_ERR
683 "NILFS (device %s): Error roll-forwarding "
684 "(err=%d, pseg block=%llu). ",
685 sbi->s_super->s_id, err, (unsigned long long)pseg_start);
686 goto out;
687}
688
689static void nilfs_finish_roll_forward(struct the_nilfs *nilfs,
690 struct nilfs_sb_info *sbi,
691 struct nilfs_recovery_info *ri)
692{
693 struct buffer_head *bh;
694 int err;
695
696 if (nilfs_get_segnum_of_block(nilfs, ri->ri_lsegs_start) !=
697 nilfs_get_segnum_of_block(nilfs, ri->ri_super_root))
698 return;
699
700 bh = sb_getblk(sbi->s_super, ri->ri_lsegs_start);
701 BUG_ON(!bh);
702 memset(bh->b_data, 0, bh->b_size);
703 set_buffer_dirty(bh);
704 err = sync_dirty_buffer(bh);
705 if (unlikely(err))
706 printk(KERN_WARNING
707 "NILFS warning: buffer sync write failed during "
708 "post-cleaning of recovery.\n");
709 brelse(bh);
710}
711
712/**
713 * nilfs_recover_logical_segments - salvage logical segments written after
714 * the latest super root
715 * @nilfs: the_nilfs
716 * @sbi: nilfs_sb_info
717 * @ri: pointer to a nilfs_recovery_info struct to store search results.
718 *
719 * Return Value: On success, 0 is returned. On error, one of the following
720 * negative error code is returned.
721 *
722 * %-EINVAL - Inconsistent filesystem state.
723 *
724 * %-EIO - I/O error
725 *
726 * %-ENOSPC - No space left on device (only in a panic state).
727 *
728 * %-ERESTARTSYS - Interrupted.
729 *
730 * %-ENOMEM - Insufficient memory available.
731 */
732int nilfs_recover_logical_segments(struct the_nilfs *nilfs,
733 struct nilfs_sb_info *sbi,
734 struct nilfs_recovery_info *ri)
735{
736 int err;
737
738 if (ri->ri_lsegs_start == 0 || ri->ri_lsegs_end == 0)
739 return 0;
740
741 err = nilfs_attach_checkpoint(sbi, ri->ri_cno);
742 if (unlikely(err)) {
743 printk(KERN_ERR
744 "NILFS: error loading the latest checkpoint.\n");
745 return err;
746 }
747
748 err = nilfs_do_roll_forward(nilfs, sbi, ri);
749 if (unlikely(err))
750 goto failed;
751
752 if (ri->ri_need_recovery == NILFS_RECOVERY_ROLLFORWARD_DONE) {
753 err = nilfs_prepare_segment_for_recovery(nilfs, ri);
754 if (unlikely(err)) {
755 printk(KERN_ERR "NILFS: Error preparing segments for "
756 "recovery.\n");
757 goto failed;
758 }
759
760 err = nilfs_attach_segment_constructor(sbi, ri);
761 if (unlikely(err))
762 goto failed;
763
764 set_nilfs_discontinued(nilfs);
765 err = nilfs_construct_segment(sbi->s_super);
766 nilfs_detach_segment_constructor(sbi);
767
768 if (unlikely(err)) {
769 printk(KERN_ERR "NILFS: Oops! recovery failed. "
770 "(err=%d)\n", err);
771 goto failed;
772 }
773
774 nilfs_finish_roll_forward(nilfs, sbi, ri);
775 }
776
777 nilfs_detach_checkpoint(sbi);
778 return 0;
779
780 failed:
781 nilfs_detach_checkpoint(sbi);
782 nilfs_mdt_clear(nilfs->ns_cpfile);
783 nilfs_mdt_clear(nilfs->ns_sufile);
784 nilfs_mdt_clear(nilfs->ns_dat);
785 return err;
786}
787
788/**
789 * nilfs_search_super_root - search the latest valid super root
790 * @nilfs: the_nilfs
791 * @sbi: nilfs_sb_info
792 * @ri: pointer to a nilfs_recovery_info struct to store search results.
793 *
794 * nilfs_search_super_root() looks for the latest super-root from a partial
795 * segment pointed by the superblock. It sets up struct the_nilfs through
796 * this search. It fills nilfs_recovery_info (ri) required for recovery.
797 *
798 * Return Value: On success, 0 is returned. On error, one of the following
799 * negative error code is returned.
800 *
801 * %-EINVAL - No valid segment found
802 *
803 * %-EIO - I/O error
804 */
805int nilfs_search_super_root(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi,
806 struct nilfs_recovery_info *ri)
807{
808 struct nilfs_segsum_info ssi;
809 sector_t pseg_start, pseg_end, sr_pseg_start = 0;
810 sector_t seg_start, seg_end; /* range of full segment (block number) */
811 u64 seg_seq;
812 __u64 segnum, nextnum = 0;
813 __u64 cno;
814 struct nilfs_segment_entry *ent;
815 LIST_HEAD(segments);
816 int empty_seg = 0, scan_newer = 0;
817 int ret;
818
819 pseg_start = nilfs->ns_last_pseg;
820 seg_seq = nilfs->ns_last_seq;
821 cno = nilfs->ns_last_cno;
822 segnum = nilfs_get_segnum_of_block(nilfs, pseg_start);
823
824 /* Calculate range of segment */
825 nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
826
827 for (;;) {
828 /* Load segment summary */
829 ret = load_segment_summary(sbi, pseg_start, seg_seq, &ssi, 1);
830 if (ret) {
831 if (ret == NILFS_SEG_FAIL_IO)
832 goto failed;
833 goto strayed;
834 }
835 pseg_end = pseg_start + ssi.nblocks - 1;
836 if (unlikely(pseg_end > seg_end)) {
837 ret = NILFS_SEG_FAIL_CONSISTENCY;
838 goto strayed;
839 }
840
841 /* A valid partial segment */
842 ri->ri_pseg_start = pseg_start;
843 ri->ri_seq = seg_seq;
844 ri->ri_segnum = segnum;
845 nextnum = nilfs_get_segnum_of_block(nilfs, ssi.next);
846 ri->ri_nextnum = nextnum;
847 empty_seg = 0;
848
849 if (!NILFS_SEG_HAS_SR(&ssi)) {
850 if (!scan_newer) {
851 /* This will never happen because a superblock
852 (last_segment) always points to a pseg
853 having a super root. */
854 ret = NILFS_SEG_FAIL_CONSISTENCY;
855 goto failed;
856 }
857 if (!ri->ri_lsegs_start && NILFS_SEG_LOGBGN(&ssi)) {
858 ri->ri_lsegs_start = pseg_start;
859 ri->ri_lsegs_start_seq = seg_seq;
860 }
861 if (NILFS_SEG_LOGEND(&ssi))
862 ri->ri_lsegs_end = pseg_start;
863 goto try_next_pseg;
864 }
865
866 /* A valid super root was found. */
867 ri->ri_cno = cno++;
868 ri->ri_super_root = pseg_end;
869 ri->ri_lsegs_start = ri->ri_lsegs_end = 0;
870
871 nilfs_dispose_segment_list(&segments);
872 nilfs->ns_pseg_offset = (sr_pseg_start = pseg_start)
873 + ssi.nblocks - seg_start;
874 nilfs->ns_seg_seq = seg_seq;
875 nilfs->ns_segnum = segnum;
876 nilfs->ns_cno = cno; /* nilfs->ns_cno = ri->ri_cno + 1 */
877 nilfs->ns_ctime = ssi.ctime;
878 nilfs->ns_nextnum = nextnum;
879
880 if (scan_newer)
881 ri->ri_need_recovery = NILFS_RECOVERY_SR_UPDATED;
Ryusuke Konishi2c2e52f2009-04-06 19:01:54 -0700882 else {
883 nilfs->ns_prot_seq = ssi.seg_seq;
884 if (nilfs->ns_mount_state & NILFS_VALID_FS)
885 goto super_root_found;
886 scan_newer = 1;
887 }
Ryusuke Konishi0f3e1c72009-04-06 19:01:38 -0700888
889 /* reset region for roll-forward */
890 pseg_start += ssi.nblocks;
891 if (pseg_start < seg_end)
892 continue;
893 goto feed_segment;
894
895 try_next_pseg:
896 /* Standing on a course, or met an inconsistent state */
897 pseg_start += ssi.nblocks;
898 if (pseg_start < seg_end)
899 continue;
900 goto feed_segment;
901
902 strayed:
903 /* Off the trail */
904 if (!scan_newer)
905 /*
906 * This can happen if a checkpoint was written without
907 * barriers, or as a result of an I/O failure.
908 */
909 goto failed;
910
911 feed_segment:
912 /* Looking to the next full segment */
913 if (empty_seg++)
914 goto super_root_found; /* found a valid super root */
915
916 ent = nilfs_alloc_segment_entry(segnum);
917 if (unlikely(!ent)) {
918 ret = -ENOMEM;
919 goto failed;
920 }
921 list_add_tail(&ent->list, &segments);
922
923 seg_seq++;
924 segnum = nextnum;
925 nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
926 pseg_start = seg_start;
927 }
928
929 super_root_found:
930 /* Updating pointers relating to the latest checkpoint */
931 list_splice(&segments, ri->ri_used_segments.prev);
932 nilfs->ns_last_pseg = sr_pseg_start;
933 nilfs->ns_last_seq = nilfs->ns_seg_seq;
934 nilfs->ns_last_cno = ri->ri_cno;
935 return 0;
936
937 failed:
938 nilfs_dispose_segment_list(&segments);
939 return (ret < 0) ? ret : nilfs_warn_segment_error(ret);
940}