2 * recovery.c - NILFS recovery logic
4 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
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.
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.
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
20 * Written by Ryusuke Konishi <ryusuke@osrg.net>
23 #include <linux/buffer_head.h>
24 #include <linux/blkdev.h>
25 #include <linux/swap.h>
26 #include <linux/crc32.h>
34 * Segment check result
38 NILFS_SEG_NO_SUPER_ROOT
,
42 NILFS_SEG_FAIL_CHECKSUM_SEGSUM
,
43 NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT
,
44 NILFS_SEG_FAIL_CHECKSUM_FULL
,
45 NILFS_SEG_FAIL_CONSISTENCY
,
48 /* work structure for recovery */
49 struct nilfs_recovery_block
{
50 ino_t ino
; /* Inode number of the file that this block
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
;
59 static int nilfs_warn_segment_error(int err
)
62 case NILFS_SEG_FAIL_IO
:
64 "NILFS warning: I/O error on loading last segment\n");
66 case NILFS_SEG_FAIL_MAGIC
:
68 "NILFS warning: Segment magic number invalid\n");
70 case NILFS_SEG_FAIL_SEQ
:
72 "NILFS warning: Sequence number mismatch\n");
74 case NILFS_SEG_FAIL_CHECKSUM_SEGSUM
:
76 "NILFS warning: Checksum error in segment summary\n");
78 case NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT
:
80 "NILFS warning: Checksum error in super root\n");
82 case NILFS_SEG_FAIL_CHECKSUM_FULL
:
84 "NILFS warning: Checksum error in segment payload\n");
86 case NILFS_SEG_FAIL_CONSISTENCY
:
88 "NILFS warning: Inconsistent segment\n");
90 case NILFS_SEG_NO_SUPER_ROOT
:
92 "NILFS warning: No super root in the last segment\n");
98 static void store_segsum_info(struct nilfs_segsum_info
*ssi
,
99 struct nilfs_segment_summary
*sum
,
100 unsigned int blocksize
)
102 ssi
->flags
= le16_to_cpu(sum
->ss_flags
);
103 ssi
->seg_seq
= le64_to_cpu(sum
->ss_seq
);
104 ssi
->ctime
= le64_to_cpu(sum
->ss_create
);
105 ssi
->next
= le64_to_cpu(sum
->ss_next
);
106 ssi
->nblocks
= le32_to_cpu(sum
->ss_nblocks
);
107 ssi
->nfinfo
= le32_to_cpu(sum
->ss_nfinfo
);
108 ssi
->sumbytes
= le32_to_cpu(sum
->ss_sumbytes
);
110 ssi
->nsumblk
= DIV_ROUND_UP(ssi
->sumbytes
, blocksize
);
111 ssi
->nfileblk
= ssi
->nblocks
- ssi
->nsumblk
- !!NILFS_SEG_HAS_SR(ssi
);
115 * calc_crc_cont - check CRC of blocks continuously
116 * @sbi: nilfs_sb_info
117 * @bhs: buffer head of start block
118 * @sum: place to store result
119 * @offset: offset bytes in the first block
120 * @check_bytes: number of bytes to be checked
121 * @start: DBN of start block
122 * @nblock: number of blocks to be checked
124 static int calc_crc_cont(struct nilfs_sb_info
*sbi
, struct buffer_head
*bhs
,
125 u32
*sum
, unsigned long offset
, u64 check_bytes
,
126 sector_t start
, unsigned long nblock
)
128 unsigned long blocksize
= sbi
->s_super
->s_blocksize
;
132 BUG_ON(offset
>= blocksize
);
133 check_bytes
-= offset
;
134 size
= min_t(u64
, check_bytes
, blocksize
- offset
);
135 crc
= crc32_le(sbi
->s_nilfs
->ns_crc_seed
,
136 (unsigned char *)bhs
->b_data
+ offset
, size
);
139 struct buffer_head
*bh
140 = sb_bread(sbi
->s_super
, ++start
);
144 size
= min_t(u64
, check_bytes
, blocksize
);
145 crc
= crc32_le(crc
, bh
->b_data
, size
);
147 } while (--nblock
> 0);
154 * nilfs_read_super_root_block - read super root block
156 * @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
160 int nilfs_read_super_root_block(struct super_block
*sb
, sector_t sr_block
,
161 struct buffer_head
**pbh
, int check
)
163 struct buffer_head
*bh_sr
;
164 struct nilfs_super_root
*sr
;
169 bh_sr
= sb_bread(sb
, sr_block
);
170 if (unlikely(!bh_sr
)) {
171 ret
= NILFS_SEG_FAIL_IO
;
175 sr
= (struct nilfs_super_root
*)bh_sr
->b_data
;
177 unsigned bytes
= le16_to_cpu(sr
->sr_bytes
);
179 if (bytes
== 0 || bytes
> sb
->s_blocksize
) {
180 ret
= NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT
;
183 if (calc_crc_cont(NILFS_SB(sb
), bh_sr
, &crc
,
184 sizeof(sr
->sr_sum
), bytes
, sr_block
, 1)) {
185 ret
= NILFS_SEG_FAIL_IO
;
188 if (crc
!= le32_to_cpu(sr
->sr_sum
)) {
189 ret
= NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT
;
200 return nilfs_warn_segment_error(ret
);
204 * load_segment_summary - read segment summary of the specified partial segment
205 * @sbi: nilfs_sb_info
206 * @pseg_start: start disk block number of partial segment
207 * @seg_seq: sequence number requested
208 * @ssi: pointer to nilfs_segsum_info struct to store information
209 * @full_check: full check flag
210 * (0: only checks segment summary CRC, 1: data CRC)
213 load_segment_summary(struct nilfs_sb_info
*sbi
, sector_t pseg_start
,
214 u64 seg_seq
, struct nilfs_segsum_info
*ssi
,
217 struct buffer_head
*bh_sum
;
218 struct nilfs_segment_summary
*sum
;
219 unsigned long offset
, nblock
;
222 int ret
= NILFS_SEG_FAIL_IO
;
224 bh_sum
= sb_bread(sbi
->s_super
, pseg_start
);
228 sum
= (struct nilfs_segment_summary
*)bh_sum
->b_data
;
230 /* Check consistency of segment summary */
231 if (le32_to_cpu(sum
->ss_magic
) != NILFS_SEGSUM_MAGIC
) {
232 ret
= NILFS_SEG_FAIL_MAGIC
;
235 store_segsum_info(ssi
, sum
, sbi
->s_super
->s_blocksize
);
236 if (seg_seq
!= ssi
->seg_seq
) {
237 ret
= NILFS_SEG_FAIL_SEQ
;
241 offset
= sizeof(sum
->ss_datasum
);
243 ((u64
)ssi
->nblocks
<< sbi
->s_super
->s_blocksize_bits
);
244 nblock
= ssi
->nblocks
;
245 crc_sum
= le32_to_cpu(sum
->ss_datasum
);
246 ret
= NILFS_SEG_FAIL_CHECKSUM_FULL
;
247 } else { /* only checks segment summary */
248 offset
= sizeof(sum
->ss_datasum
) + sizeof(sum
->ss_sumsum
);
249 check_bytes
= ssi
->sumbytes
;
250 nblock
= ssi
->nsumblk
;
251 crc_sum
= le32_to_cpu(sum
->ss_sumsum
);
252 ret
= NILFS_SEG_FAIL_CHECKSUM_SEGSUM
;
255 if (unlikely(nblock
== 0 ||
256 nblock
> sbi
->s_nilfs
->ns_blocks_per_segment
)) {
257 /* This limits the number of blocks read in the CRC check */
258 ret
= NILFS_SEG_FAIL_CONSISTENCY
;
261 if (calc_crc_cont(sbi
, bh_sum
, &crc
, offset
, check_bytes
,
262 pseg_start
, nblock
)) {
263 ret
= NILFS_SEG_FAIL_IO
;
274 static void *segsum_get(struct super_block
*sb
, struct buffer_head
**pbh
,
275 unsigned int *offset
, unsigned int bytes
)
280 BUG_ON((*pbh
)->b_size
< *offset
);
281 if (bytes
> (*pbh
)->b_size
- *offset
) {
282 blocknr
= (*pbh
)->b_blocknr
;
284 *pbh
= sb_bread(sb
, blocknr
+ 1);
289 ptr
= (*pbh
)->b_data
+ *offset
;
294 static void segsum_skip(struct super_block
*sb
, struct buffer_head
**pbh
,
295 unsigned int *offset
, unsigned int bytes
,
298 unsigned int rest_item_in_current_block
299 = ((*pbh
)->b_size
- *offset
) / bytes
;
301 if (count
<= rest_item_in_current_block
) {
302 *offset
+= bytes
* count
;
304 sector_t blocknr
= (*pbh
)->b_blocknr
;
305 unsigned int nitem_per_block
= (*pbh
)->b_size
/ bytes
;
308 count
-= rest_item_in_current_block
;
309 bcnt
= DIV_ROUND_UP(count
, nitem_per_block
);
310 *offset
= bytes
* (count
- (bcnt
- 1) * nitem_per_block
);
313 *pbh
= sb_bread(sb
, blocknr
+ bcnt
);
318 collect_blocks_from_segsum(struct nilfs_sb_info
*sbi
, sector_t sum_blocknr
,
319 struct nilfs_segsum_info
*ssi
,
320 struct list_head
*head
)
322 struct buffer_head
*bh
;
324 unsigned long nfinfo
= ssi
->nfinfo
;
325 sector_t blocknr
= sum_blocknr
+ ssi
->nsumblk
;
332 bh
= sb_bread(sbi
->s_super
, sum_blocknr
);
336 offset
= le16_to_cpu(
337 ((struct nilfs_segment_summary
*)bh
->b_data
)->ss_bytes
);
339 unsigned long nblocks
, ndatablk
, nnodeblk
;
340 struct nilfs_finfo
*finfo
;
342 finfo
= segsum_get(sbi
->s_super
, &bh
, &offset
, sizeof(*finfo
));
343 if (unlikely(!finfo
))
346 ino
= le64_to_cpu(finfo
->fi_ino
);
347 nblocks
= le32_to_cpu(finfo
->fi_nblocks
);
348 ndatablk
= le32_to_cpu(finfo
->fi_ndatablk
);
349 nnodeblk
= nblocks
- ndatablk
;
351 while (ndatablk
-- > 0) {
352 struct nilfs_recovery_block
*rb
;
353 struct nilfs_binfo_v
*binfo
;
355 binfo
= segsum_get(sbi
->s_super
, &bh
, &offset
,
357 if (unlikely(!binfo
))
360 rb
= kmalloc(sizeof(*rb
), GFP_NOFS
);
366 rb
->blocknr
= blocknr
++;
367 rb
->vblocknr
= le64_to_cpu(binfo
->bi_vblocknr
);
368 rb
->blkoff
= le64_to_cpu(binfo
->bi_blkoff
);
369 /* INIT_LIST_HEAD(&rb->list); */
370 list_add_tail(&rb
->list
, head
);
374 blocknr
+= nnodeblk
; /* always 0 for the data sync segments */
375 segsum_skip(sbi
->s_super
, &bh
, &offset
, sizeof(__le64
),
382 brelse(bh
); /* brelse(NULL) is just ignored */
386 static void dispose_recovery_list(struct list_head
*head
)
388 while (!list_empty(head
)) {
389 struct nilfs_recovery_block
*rb
390 = list_entry(head
->next
,
391 struct nilfs_recovery_block
, list
);
397 struct nilfs_segment_entry
{
398 struct list_head list
;
402 static int nilfs_segment_list_add(struct list_head
*head
, __u64 segnum
)
404 struct nilfs_segment_entry
*ent
= kmalloc(sizeof(*ent
), GFP_NOFS
);
409 ent
->segnum
= segnum
;
410 INIT_LIST_HEAD(&ent
->list
);
411 list_add_tail(&ent
->list
, head
);
415 void nilfs_dispose_segment_list(struct list_head
*head
)
417 while (!list_empty(head
)) {
418 struct nilfs_segment_entry
*ent
419 = list_entry(head
->next
,
420 struct nilfs_segment_entry
, list
);
421 list_del(&ent
->list
);
426 static int nilfs_prepare_segment_for_recovery(struct the_nilfs
*nilfs
,
427 struct nilfs_sb_info
*sbi
,
428 struct nilfs_recovery_info
*ri
)
430 struct list_head
*head
= &ri
->ri_used_segments
;
431 struct nilfs_segment_entry
*ent
, *n
;
432 struct inode
*sufile
= nilfs
->ns_sufile
;
437 segnum
[0] = nilfs
->ns_segnum
;
438 segnum
[1] = nilfs
->ns_nextnum
;
439 segnum
[2] = ri
->ri_segnum
;
440 segnum
[3] = ri
->ri_nextnum
;
442 nilfs_attach_writer(nilfs
, sbi
);
444 * Releasing the next segment of the latest super root.
445 * The next segment is invalidated by this recovery.
447 err
= nilfs_sufile_free(sufile
, segnum
[1]);
451 for (i
= 1; i
< 4; i
++) {
452 err
= nilfs_segment_list_add(head
, segnum
[i
]);
458 * Collecting segments written after the latest super root.
459 * These are marked dirty to avoid being reallocated in the next write.
461 list_for_each_entry_safe(ent
, n
, head
, list
) {
462 if (ent
->segnum
!= segnum
[0]) {
463 err
= nilfs_sufile_scrap(sufile
, ent
->segnum
);
467 list_del(&ent
->list
);
471 /* Allocate new segments for recovery */
472 err
= nilfs_sufile_alloc(sufile
, &segnum
[0]);
476 nilfs
->ns_pseg_offset
= 0;
477 nilfs
->ns_seg_seq
= ri
->ri_seq
+ 2;
478 nilfs
->ns_nextnum
= nilfs
->ns_segnum
= segnum
[0];
481 /* No need to recover sufile because it will be destroyed on error */
482 nilfs_detach_writer(nilfs
, sbi
);
486 static int nilfs_recovery_copy_block(struct nilfs_sb_info
*sbi
,
487 struct nilfs_recovery_block
*rb
,
490 struct buffer_head
*bh_org
;
493 bh_org
= sb_bread(sbi
->s_super
, rb
->blocknr
);
494 if (unlikely(!bh_org
))
497 kaddr
= kmap_atomic(page
, KM_USER0
);
498 memcpy(kaddr
+ bh_offset(bh_org
), bh_org
->b_data
, bh_org
->b_size
);
499 kunmap_atomic(kaddr
, KM_USER0
);
504 static int recover_dsync_blocks(struct nilfs_sb_info
*sbi
,
505 struct list_head
*head
,
506 unsigned long *nr_salvaged_blocks
)
509 struct nilfs_recovery_block
*rb
, *n
;
510 unsigned blocksize
= sbi
->s_super
->s_blocksize
;
513 int err
= 0, err2
= 0;
515 list_for_each_entry_safe(rb
, n
, head
, list
) {
516 inode
= nilfs_iget(sbi
->s_super
, rb
->ino
);
518 err
= PTR_ERR(inode
);
523 pos
= rb
->blkoff
<< inode
->i_blkbits
;
525 err
= block_write_begin(NULL
, inode
->i_mapping
, pos
, blocksize
,
526 0, &page
, NULL
, nilfs_get_block
);
530 err
= nilfs_recovery_copy_block(sbi
, rb
, page
);
534 err
= nilfs_set_file_dirty(sbi
, inode
, 1);
538 block_write_end(NULL
, inode
->i_mapping
, pos
, blocksize
,
539 blocksize
, page
, NULL
);
542 page_cache_release(page
);
544 (*nr_salvaged_blocks
)++;
549 page_cache_release(page
);
553 "NILFS warning: error recovering data block "
554 "(err=%d, ino=%lu, block-offset=%llu)\n",
555 err
, rb
->ino
, (unsigned long long)rb
->blkoff
);
559 iput(inode
); /* iput(NULL) is just ignored */
560 list_del_init(&rb
->list
);
567 * nilfs_do_roll_forward - salvage logical segments newer than the latest
569 * @sbi: nilfs_sb_info
571 * @ri: pointer to a nilfs_recovery_info
573 static int nilfs_do_roll_forward(struct the_nilfs
*nilfs
,
574 struct nilfs_sb_info
*sbi
,
575 struct nilfs_recovery_info
*ri
)
577 struct nilfs_segsum_info ssi
;
579 sector_t seg_start
, seg_end
; /* Starting/ending DBN of full segment */
580 unsigned long nsalvaged_blocks
= 0;
582 __u64 segnum
, nextnum
= 0;
585 LIST_HEAD(dsync_blocks
); /* list of data blocks to be recovered */
588 RF_DSYNC_ST
, /* scanning data-sync segments */
590 int state
= RF_INIT_ST
;
592 nilfs_attach_writer(nilfs
, sbi
);
593 pseg_start
= ri
->ri_lsegs_start
;
594 seg_seq
= ri
->ri_lsegs_start_seq
;
595 segnum
= nilfs_get_segnum_of_block(nilfs
, pseg_start
);
596 nilfs_get_segment_range(nilfs
, segnum
, &seg_start
, &seg_end
);
598 while (segnum
!= ri
->ri_segnum
|| pseg_start
<= ri
->ri_pseg_start
) {
600 ret
= load_segment_summary(sbi
, pseg_start
, seg_seq
, &ssi
, 1);
602 if (ret
== NILFS_SEG_FAIL_IO
) {
608 if (unlikely(NILFS_SEG_HAS_SR(&ssi
)))
611 /* Found a valid partial segment; do recovery actions */
612 nextnum
= nilfs_get_segnum_of_block(nilfs
, ssi
.next
);
614 nilfs
->ns_ctime
= ssi
.ctime
;
615 if (!(ssi
.flags
& NILFS_SS_GC
))
616 nilfs
->ns_nongc_ctime
= ssi
.ctime
;
620 if (!NILFS_SEG_LOGBGN(&ssi
) || !NILFS_SEG_DSYNC(&ssi
))
625 if (!NILFS_SEG_DSYNC(&ssi
))
628 err
= collect_blocks_from_segsum(
629 sbi
, pseg_start
, &ssi
, &dsync_blocks
);
632 if (NILFS_SEG_LOGEND(&ssi
)) {
633 err
= recover_dsync_blocks(
634 sbi
, &dsync_blocks
, &nsalvaged_blocks
);
639 break; /* Fall through to try_next_pseg */
643 if (pseg_start
== ri
->ri_lsegs_end
)
645 pseg_start
+= ssi
.nblocks
;
646 if (pseg_start
< seg_end
)
651 if (pseg_start
== ri
->ri_lsegs_end
)
655 /* Looking to the next full segment */
660 nilfs_get_segment_range(nilfs
, segnum
, &seg_start
, &seg_end
);
661 pseg_start
= seg_start
;
664 if (nsalvaged_blocks
) {
665 printk(KERN_INFO
"NILFS (device %s): salvaged %lu blocks\n",
666 sbi
->s_super
->s_id
, nsalvaged_blocks
);
667 ri
->ri_need_recovery
= NILFS_RECOVERY_ROLLFORWARD_DONE
;
670 dispose_recovery_list(&dsync_blocks
);
671 nilfs_detach_writer(sbi
->s_nilfs
, sbi
);
678 "NILFS (device %s): Error roll-forwarding "
679 "(err=%d, pseg block=%llu). ",
680 sbi
->s_super
->s_id
, err
, (unsigned long long)pseg_start
);
684 static void nilfs_finish_roll_forward(struct the_nilfs
*nilfs
,
685 struct nilfs_sb_info
*sbi
,
686 struct nilfs_recovery_info
*ri
)
688 struct buffer_head
*bh
;
691 if (nilfs_get_segnum_of_block(nilfs
, ri
->ri_lsegs_start
) !=
692 nilfs_get_segnum_of_block(nilfs
, ri
->ri_super_root
))
695 bh
= sb_getblk(sbi
->s_super
, ri
->ri_lsegs_start
);
697 memset(bh
->b_data
, 0, bh
->b_size
);
698 set_buffer_dirty(bh
);
699 err
= sync_dirty_buffer(bh
);
702 "NILFS warning: buffer sync write failed during "
703 "post-cleaning of recovery.\n");
708 * nilfs_recover_logical_segments - salvage logical segments written after
709 * the latest super root
711 * @sbi: nilfs_sb_info
712 * @ri: pointer to a nilfs_recovery_info struct to store search results.
714 * Return Value: On success, 0 is returned. On error, one of the following
715 * negative error code is returned.
717 * %-EINVAL - Inconsistent filesystem state.
721 * %-ENOSPC - No space left on device (only in a panic state).
723 * %-ERESTARTSYS - Interrupted.
725 * %-ENOMEM - Insufficient memory available.
727 int nilfs_recover_logical_segments(struct the_nilfs
*nilfs
,
728 struct nilfs_sb_info
*sbi
,
729 struct nilfs_recovery_info
*ri
)
733 if (ri
->ri_lsegs_start
== 0 || ri
->ri_lsegs_end
== 0)
736 err
= nilfs_attach_checkpoint(sbi
, ri
->ri_cno
);
739 "NILFS: error loading the latest checkpoint.\n");
743 err
= nilfs_do_roll_forward(nilfs
, sbi
, ri
);
747 if (ri
->ri_need_recovery
== NILFS_RECOVERY_ROLLFORWARD_DONE
) {
748 err
= nilfs_prepare_segment_for_recovery(nilfs
, sbi
, ri
);
750 printk(KERN_ERR
"NILFS: Error preparing segments for "
755 err
= nilfs_attach_segment_constructor(sbi
);
759 set_nilfs_discontinued(nilfs
);
760 err
= nilfs_construct_segment(sbi
->s_super
);
761 nilfs_detach_segment_constructor(sbi
);
764 printk(KERN_ERR
"NILFS: Oops! recovery failed. "
769 nilfs_finish_roll_forward(nilfs
, sbi
, ri
);
772 nilfs_detach_checkpoint(sbi
);
776 nilfs_detach_checkpoint(sbi
);
777 nilfs_mdt_clear(nilfs
->ns_cpfile
);
778 nilfs_mdt_clear(nilfs
->ns_sufile
);
779 nilfs_mdt_clear(nilfs
->ns_dat
);
784 * nilfs_search_super_root - search the latest valid super root
786 * @sbi: nilfs_sb_info
787 * @ri: pointer to a nilfs_recovery_info struct to store search results.
789 * nilfs_search_super_root() looks for the latest super-root from a partial
790 * segment pointed by the superblock. It sets up struct the_nilfs through
791 * this search. It fills nilfs_recovery_info (ri) required for recovery.
793 * Return Value: On success, 0 is returned. On error, one of the following
794 * negative error code is returned.
796 * %-EINVAL - No valid segment found
800 int nilfs_search_super_root(struct the_nilfs
*nilfs
, struct nilfs_sb_info
*sbi
,
801 struct nilfs_recovery_info
*ri
)
803 struct nilfs_segsum_info ssi
;
804 sector_t pseg_start
, pseg_end
, sr_pseg_start
= 0;
805 sector_t seg_start
, seg_end
; /* range of full segment (block number) */
807 __u64 segnum
, nextnum
= 0;
810 int empty_seg
= 0, scan_newer
= 0;
813 pseg_start
= nilfs
->ns_last_pseg
;
814 seg_seq
= nilfs
->ns_last_seq
;
815 cno
= nilfs
->ns_last_cno
;
816 segnum
= nilfs_get_segnum_of_block(nilfs
, pseg_start
);
818 /* Calculate range of segment */
819 nilfs_get_segment_range(nilfs
, segnum
, &seg_start
, &seg_end
);
822 /* Load segment summary */
823 ret
= load_segment_summary(sbi
, pseg_start
, seg_seq
, &ssi
, 1);
825 if (ret
== NILFS_SEG_FAIL_IO
)
829 pseg_end
= pseg_start
+ ssi
.nblocks
- 1;
830 if (unlikely(pseg_end
> seg_end
)) {
831 ret
= NILFS_SEG_FAIL_CONSISTENCY
;
835 /* A valid partial segment */
836 ri
->ri_pseg_start
= pseg_start
;
837 ri
->ri_seq
= seg_seq
;
838 ri
->ri_segnum
= segnum
;
839 nextnum
= nilfs_get_segnum_of_block(nilfs
, ssi
.next
);
840 ri
->ri_nextnum
= nextnum
;
843 if (!NILFS_SEG_HAS_SR(&ssi
)) {
845 /* This will never happen because a superblock
846 (last_segment) always points to a pseg
847 having a super root. */
848 ret
= NILFS_SEG_FAIL_CONSISTENCY
;
851 if (!ri
->ri_lsegs_start
&& NILFS_SEG_LOGBGN(&ssi
)) {
852 ri
->ri_lsegs_start
= pseg_start
;
853 ri
->ri_lsegs_start_seq
= seg_seq
;
855 if (NILFS_SEG_LOGEND(&ssi
))
856 ri
->ri_lsegs_end
= pseg_start
;
860 /* A valid super root was found. */
862 ri
->ri_super_root
= pseg_end
;
863 ri
->ri_lsegs_start
= ri
->ri_lsegs_end
= 0;
865 nilfs_dispose_segment_list(&segments
);
866 nilfs
->ns_pseg_offset
= (sr_pseg_start
= pseg_start
)
867 + ssi
.nblocks
- seg_start
;
868 nilfs
->ns_seg_seq
= seg_seq
;
869 nilfs
->ns_segnum
= segnum
;
870 nilfs
->ns_cno
= cno
; /* nilfs->ns_cno = ri->ri_cno + 1 */
871 nilfs
->ns_ctime
= ssi
.ctime
;
872 nilfs
->ns_nextnum
= nextnum
;
875 ri
->ri_need_recovery
= NILFS_RECOVERY_SR_UPDATED
;
877 if (nilfs
->ns_mount_state
& NILFS_VALID_FS
)
878 goto super_root_found
;
882 /* reset region for roll-forward */
883 pseg_start
+= ssi
.nblocks
;
884 if (pseg_start
< seg_end
)
889 /* Standing on a course, or met an inconsistent state */
890 pseg_start
+= ssi
.nblocks
;
891 if (pseg_start
< seg_end
)
899 * This can happen if a checkpoint was written without
900 * barriers, or as a result of an I/O failure.
905 /* Looking to the next full segment */
907 goto super_root_found
; /* found a valid super root */
909 ret
= nilfs_segment_list_add(&segments
, segnum
);
915 nilfs_get_segment_range(nilfs
, segnum
, &seg_start
, &seg_end
);
916 pseg_start
= seg_start
;
920 /* Updating pointers relating to the latest checkpoint */
921 list_splice(&segments
, ri
->ri_used_segments
.prev
);
922 nilfs
->ns_last_pseg
= sr_pseg_start
;
923 nilfs
->ns_last_seq
= nilfs
->ns_seg_seq
;
924 nilfs
->ns_last_cno
= ri
->ri_cno
;
928 nilfs_dispose_segment_list(&segments
);
929 return (ret
< 0) ? ret
: nilfs_warn_segment_error(ret
);