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
, (unsigned long)rb
->ino
,
556 (unsigned long long)rb
->blkoff
);
560 iput(inode
); /* iput(NULL) is just ignored */
561 list_del_init(&rb
->list
);
568 * nilfs_do_roll_forward - salvage logical segments newer than the latest
570 * @sbi: nilfs_sb_info
572 * @ri: pointer to a nilfs_recovery_info
574 static int nilfs_do_roll_forward(struct the_nilfs
*nilfs
,
575 struct nilfs_sb_info
*sbi
,
576 struct nilfs_recovery_info
*ri
)
578 struct nilfs_segsum_info ssi
;
580 sector_t seg_start
, seg_end
; /* Starting/ending DBN of full segment */
581 unsigned long nsalvaged_blocks
= 0;
583 __u64 segnum
, nextnum
= 0;
586 LIST_HEAD(dsync_blocks
); /* list of data blocks to be recovered */
589 RF_DSYNC_ST
, /* scanning data-sync segments */
591 int state
= RF_INIT_ST
;
593 nilfs_attach_writer(nilfs
, sbi
);
594 pseg_start
= ri
->ri_lsegs_start
;
595 seg_seq
= ri
->ri_lsegs_start_seq
;
596 segnum
= nilfs_get_segnum_of_block(nilfs
, pseg_start
);
597 nilfs_get_segment_range(nilfs
, segnum
, &seg_start
, &seg_end
);
599 while (segnum
!= ri
->ri_segnum
|| pseg_start
<= ri
->ri_pseg_start
) {
601 ret
= load_segment_summary(sbi
, pseg_start
, seg_seq
, &ssi
, 1);
603 if (ret
== NILFS_SEG_FAIL_IO
) {
609 if (unlikely(NILFS_SEG_HAS_SR(&ssi
)))
612 /* Found a valid partial segment; do recovery actions */
613 nextnum
= nilfs_get_segnum_of_block(nilfs
, ssi
.next
);
615 nilfs
->ns_ctime
= ssi
.ctime
;
616 if (!(ssi
.flags
& NILFS_SS_GC
))
617 nilfs
->ns_nongc_ctime
= ssi
.ctime
;
621 if (!NILFS_SEG_LOGBGN(&ssi
) || !NILFS_SEG_DSYNC(&ssi
))
626 if (!NILFS_SEG_DSYNC(&ssi
))
629 err
= collect_blocks_from_segsum(
630 sbi
, pseg_start
, &ssi
, &dsync_blocks
);
633 if (NILFS_SEG_LOGEND(&ssi
)) {
634 err
= recover_dsync_blocks(
635 sbi
, &dsync_blocks
, &nsalvaged_blocks
);
640 break; /* Fall through to try_next_pseg */
644 if (pseg_start
== ri
->ri_lsegs_end
)
646 pseg_start
+= ssi
.nblocks
;
647 if (pseg_start
< seg_end
)
652 if (pseg_start
== ri
->ri_lsegs_end
)
656 /* Looking to the next full segment */
661 nilfs_get_segment_range(nilfs
, segnum
, &seg_start
, &seg_end
);
662 pseg_start
= seg_start
;
665 if (nsalvaged_blocks
) {
666 printk(KERN_INFO
"NILFS (device %s): salvaged %lu blocks\n",
667 sbi
->s_super
->s_id
, nsalvaged_blocks
);
668 ri
->ri_need_recovery
= NILFS_RECOVERY_ROLLFORWARD_DONE
;
671 dispose_recovery_list(&dsync_blocks
);
672 nilfs_detach_writer(sbi
->s_nilfs
, sbi
);
679 "NILFS (device %s): Error roll-forwarding "
680 "(err=%d, pseg block=%llu). ",
681 sbi
->s_super
->s_id
, err
, (unsigned long long)pseg_start
);
685 static void nilfs_finish_roll_forward(struct the_nilfs
*nilfs
,
686 struct nilfs_sb_info
*sbi
,
687 struct nilfs_recovery_info
*ri
)
689 struct buffer_head
*bh
;
692 if (nilfs_get_segnum_of_block(nilfs
, ri
->ri_lsegs_start
) !=
693 nilfs_get_segnum_of_block(nilfs
, ri
->ri_super_root
))
696 bh
= sb_getblk(sbi
->s_super
, ri
->ri_lsegs_start
);
698 memset(bh
->b_data
, 0, bh
->b_size
);
699 set_buffer_dirty(bh
);
700 err
= sync_dirty_buffer(bh
);
703 "NILFS warning: buffer sync write failed during "
704 "post-cleaning of recovery.\n");
709 * nilfs_recover_logical_segments - salvage logical segments written after
710 * the latest super root
712 * @sbi: nilfs_sb_info
713 * @ri: pointer to a nilfs_recovery_info struct to store search results.
715 * Return Value: On success, 0 is returned. On error, one of the following
716 * negative error code is returned.
718 * %-EINVAL - Inconsistent filesystem state.
722 * %-ENOSPC - No space left on device (only in a panic state).
724 * %-ERESTARTSYS - Interrupted.
726 * %-ENOMEM - Insufficient memory available.
728 int nilfs_recover_logical_segments(struct the_nilfs
*nilfs
,
729 struct nilfs_sb_info
*sbi
,
730 struct nilfs_recovery_info
*ri
)
734 if (ri
->ri_lsegs_start
== 0 || ri
->ri_lsegs_end
== 0)
737 err
= nilfs_attach_checkpoint(sbi
, ri
->ri_cno
);
740 "NILFS: error loading the latest checkpoint.\n");
744 err
= nilfs_do_roll_forward(nilfs
, sbi
, ri
);
748 if (ri
->ri_need_recovery
== NILFS_RECOVERY_ROLLFORWARD_DONE
) {
749 err
= nilfs_prepare_segment_for_recovery(nilfs
, sbi
, ri
);
751 printk(KERN_ERR
"NILFS: Error preparing segments for "
756 err
= nilfs_attach_segment_constructor(sbi
);
760 set_nilfs_discontinued(nilfs
);
761 err
= nilfs_construct_segment(sbi
->s_super
);
762 nilfs_detach_segment_constructor(sbi
);
765 printk(KERN_ERR
"NILFS: Oops! recovery failed. "
770 nilfs_finish_roll_forward(nilfs
, sbi
, ri
);
773 nilfs_detach_checkpoint(sbi
);
777 nilfs_detach_checkpoint(sbi
);
778 nilfs_mdt_clear(nilfs
->ns_cpfile
);
779 nilfs_mdt_clear(nilfs
->ns_sufile
);
780 nilfs_mdt_clear(nilfs
->ns_dat
);
785 * nilfs_search_super_root - search the latest valid super root
787 * @sbi: nilfs_sb_info
788 * @ri: pointer to a nilfs_recovery_info struct to store search results.
790 * nilfs_search_super_root() looks for the latest super-root from a partial
791 * segment pointed by the superblock. It sets up struct the_nilfs through
792 * this search. It fills nilfs_recovery_info (ri) required for recovery.
794 * Return Value: On success, 0 is returned. On error, one of the following
795 * negative error code is returned.
797 * %-EINVAL - No valid segment found
801 int nilfs_search_super_root(struct the_nilfs
*nilfs
, struct nilfs_sb_info
*sbi
,
802 struct nilfs_recovery_info
*ri
)
804 struct nilfs_segsum_info ssi
;
805 sector_t pseg_start
, pseg_end
, sr_pseg_start
= 0;
806 sector_t seg_start
, seg_end
; /* range of full segment (block number) */
808 __u64 segnum
, nextnum
= 0;
811 int empty_seg
= 0, scan_newer
= 0;
814 pseg_start
= nilfs
->ns_last_pseg
;
815 seg_seq
= nilfs
->ns_last_seq
;
816 cno
= nilfs
->ns_last_cno
;
817 segnum
= nilfs_get_segnum_of_block(nilfs
, pseg_start
);
819 /* Calculate range of segment */
820 nilfs_get_segment_range(nilfs
, segnum
, &seg_start
, &seg_end
);
823 /* Load segment summary */
824 ret
= load_segment_summary(sbi
, pseg_start
, seg_seq
, &ssi
, 1);
826 if (ret
== NILFS_SEG_FAIL_IO
)
830 pseg_end
= pseg_start
+ ssi
.nblocks
- 1;
831 if (unlikely(pseg_end
> seg_end
)) {
832 ret
= NILFS_SEG_FAIL_CONSISTENCY
;
836 /* A valid partial segment */
837 ri
->ri_pseg_start
= pseg_start
;
838 ri
->ri_seq
= seg_seq
;
839 ri
->ri_segnum
= segnum
;
840 nextnum
= nilfs_get_segnum_of_block(nilfs
, ssi
.next
);
841 ri
->ri_nextnum
= nextnum
;
844 if (!NILFS_SEG_HAS_SR(&ssi
)) {
846 /* This will never happen because a superblock
847 (last_segment) always points to a pseg
848 having a super root. */
849 ret
= NILFS_SEG_FAIL_CONSISTENCY
;
852 if (!ri
->ri_lsegs_start
&& NILFS_SEG_LOGBGN(&ssi
)) {
853 ri
->ri_lsegs_start
= pseg_start
;
854 ri
->ri_lsegs_start_seq
= seg_seq
;
856 if (NILFS_SEG_LOGEND(&ssi
))
857 ri
->ri_lsegs_end
= pseg_start
;
861 /* A valid super root was found. */
863 ri
->ri_super_root
= pseg_end
;
864 ri
->ri_lsegs_start
= ri
->ri_lsegs_end
= 0;
866 nilfs_dispose_segment_list(&segments
);
867 nilfs
->ns_pseg_offset
= (sr_pseg_start
= pseg_start
)
868 + ssi
.nblocks
- seg_start
;
869 nilfs
->ns_seg_seq
= seg_seq
;
870 nilfs
->ns_segnum
= segnum
;
871 nilfs
->ns_cno
= cno
; /* nilfs->ns_cno = ri->ri_cno + 1 */
872 nilfs
->ns_ctime
= ssi
.ctime
;
873 nilfs
->ns_nextnum
= nextnum
;
876 ri
->ri_need_recovery
= NILFS_RECOVERY_SR_UPDATED
;
878 if (nilfs
->ns_mount_state
& NILFS_VALID_FS
)
879 goto super_root_found
;
883 /* reset region for roll-forward */
884 pseg_start
+= ssi
.nblocks
;
885 if (pseg_start
< seg_end
)
890 /* Standing on a course, or met an inconsistent state */
891 pseg_start
+= ssi
.nblocks
;
892 if (pseg_start
< seg_end
)
900 * This can happen if a checkpoint was written without
901 * barriers, or as a result of an I/O failure.
906 /* Looking to the next full segment */
908 goto super_root_found
; /* found a valid super root */
910 ret
= nilfs_segment_list_add(&segments
, segnum
);
916 nilfs_get_segment_range(nilfs
, segnum
, &seg_start
, &seg_end
);
917 pseg_start
= seg_start
;
921 /* Updating pointers relating to the latest checkpoint */
922 list_splice(&segments
, ri
->ri_used_segments
.prev
);
923 nilfs
->ns_last_pseg
= sr_pseg_start
;
924 nilfs
->ns_last_seq
= nilfs
->ns_seg_seq
;
925 nilfs
->ns_last_cno
= ri
->ri_cno
;
929 nilfs_dispose_segment_list(&segments
);
930 return (ret
< 0) ? ret
: nilfs_warn_segment_error(ret
);