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/slab.h>
27 #include <linux/crc32.h>
35 * Segment check result
39 NILFS_SEG_NO_SUPER_ROOT
,
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_SUPER_ROOT
:
76 "NILFS warning: Checksum error in super root\n");
78 case NILFS_SEG_FAIL_CHECKSUM_FULL
:
80 "NILFS warning: Checksum error in segment payload\n");
82 case NILFS_SEG_FAIL_CONSISTENCY
:
84 "NILFS warning: Inconsistent segment\n");
86 case NILFS_SEG_NO_SUPER_ROOT
:
88 "NILFS warning: No super root in the last segment\n");
95 * nilfs_compute_checksum - compute checksum of blocks continuously
96 * @nilfs: nilfs object
97 * @bhs: buffer head of start block
98 * @sum: place to store result
99 * @offset: offset bytes in the first block
100 * @check_bytes: number of bytes to be checked
101 * @start: DBN of start block
102 * @nblock: number of blocks to be checked
104 static int nilfs_compute_checksum(struct the_nilfs
*nilfs
,
105 struct buffer_head
*bhs
, u32
*sum
,
106 unsigned long offset
, u64 check_bytes
,
107 sector_t start
, unsigned long nblock
)
109 unsigned int blocksize
= nilfs
->ns_blocksize
;
113 BUG_ON(offset
>= blocksize
);
114 check_bytes
-= offset
;
115 size
= min_t(u64
, check_bytes
, blocksize
- offset
);
116 crc
= crc32_le(nilfs
->ns_crc_seed
,
117 (unsigned char *)bhs
->b_data
+ offset
, size
);
120 struct buffer_head
*bh
;
122 bh
= __bread(nilfs
->ns_bdev
, ++start
, blocksize
);
126 size
= min_t(u64
, check_bytes
, blocksize
);
127 crc
= crc32_le(crc
, bh
->b_data
, size
);
129 } while (--nblock
> 0);
136 * nilfs_read_super_root_block - read super root block
137 * @nilfs: nilfs object
138 * @sr_block: disk block number of the super root block
139 * @pbh: address of a buffer_head pointer to return super root buffer
140 * @check: CRC check flag
142 int nilfs_read_super_root_block(struct the_nilfs
*nilfs
, sector_t sr_block
,
143 struct buffer_head
**pbh
, int check
)
145 struct buffer_head
*bh_sr
;
146 struct nilfs_super_root
*sr
;
151 bh_sr
= __bread(nilfs
->ns_bdev
, sr_block
, nilfs
->ns_blocksize
);
152 if (unlikely(!bh_sr
)) {
153 ret
= NILFS_SEG_FAIL_IO
;
157 sr
= (struct nilfs_super_root
*)bh_sr
->b_data
;
159 unsigned bytes
= le16_to_cpu(sr
->sr_bytes
);
161 if (bytes
== 0 || bytes
> nilfs
->ns_blocksize
) {
162 ret
= NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT
;
165 if (nilfs_compute_checksum(
166 nilfs
, bh_sr
, &crc
, sizeof(sr
->sr_sum
), bytes
,
168 ret
= NILFS_SEG_FAIL_IO
;
171 if (crc
!= le32_to_cpu(sr
->sr_sum
)) {
172 ret
= NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT
;
183 return nilfs_warn_segment_error(ret
);
187 * nilfs_read_log_header - read summary header of the specified log
188 * @nilfs: nilfs object
189 * @start_blocknr: start block number of the log
190 * @sum: pointer to return segment summary structure
192 static struct buffer_head
*
193 nilfs_read_log_header(struct the_nilfs
*nilfs
, sector_t start_blocknr
,
194 struct nilfs_segment_summary
**sum
)
196 struct buffer_head
*bh_sum
;
198 bh_sum
= __bread(nilfs
->ns_bdev
, start_blocknr
, nilfs
->ns_blocksize
);
200 *sum
= (struct nilfs_segment_summary
*)bh_sum
->b_data
;
205 * nilfs_validate_log - verify consistency of log
206 * @nilfs: nilfs object
207 * @seg_seq: sequence number of segment
208 * @bh_sum: buffer head of summary block
209 * @sum: segment summary struct
211 static int nilfs_validate_log(struct the_nilfs
*nilfs
, u64 seg_seq
,
212 struct buffer_head
*bh_sum
,
213 struct nilfs_segment_summary
*sum
)
215 unsigned long nblock
;
219 ret
= NILFS_SEG_FAIL_MAGIC
;
220 if (le32_to_cpu(sum
->ss_magic
) != NILFS_SEGSUM_MAGIC
)
223 ret
= NILFS_SEG_FAIL_SEQ
;
224 if (le64_to_cpu(sum
->ss_seq
) != seg_seq
)
227 nblock
= le32_to_cpu(sum
->ss_nblocks
);
228 ret
= NILFS_SEG_FAIL_CONSISTENCY
;
229 if (unlikely(nblock
== 0 || nblock
> nilfs
->ns_blocks_per_segment
))
230 /* This limits the number of blocks read in the CRC check */
233 ret
= NILFS_SEG_FAIL_IO
;
234 if (nilfs_compute_checksum(nilfs
, bh_sum
, &crc
, sizeof(sum
->ss_datasum
),
235 ((u64
)nblock
<< nilfs
->ns_blocksize_bits
),
236 bh_sum
->b_blocknr
, nblock
))
239 ret
= NILFS_SEG_FAIL_CHECKSUM_FULL
;
240 if (crc
!= le32_to_cpu(sum
->ss_datasum
))
248 * nilfs_read_summary_info - read an item on summary blocks of a log
249 * @nilfs: nilfs object
250 * @pbh: the current buffer head on summary blocks [in, out]
251 * @offset: the current byte offset on summary blocks [in, out]
252 * @bytes: byte size of the item to be read
254 static void *nilfs_read_summary_info(struct the_nilfs
*nilfs
,
255 struct buffer_head
**pbh
,
256 unsigned int *offset
, unsigned int bytes
)
261 BUG_ON((*pbh
)->b_size
< *offset
);
262 if (bytes
> (*pbh
)->b_size
- *offset
) {
263 blocknr
= (*pbh
)->b_blocknr
;
265 *pbh
= __bread(nilfs
->ns_bdev
, blocknr
+ 1,
266 nilfs
->ns_blocksize
);
271 ptr
= (*pbh
)->b_data
+ *offset
;
277 * nilfs_skip_summary_info - skip items on summary blocks of a log
278 * @nilfs: nilfs object
279 * @pbh: the current buffer head on summary blocks [in, out]
280 * @offset: the current byte offset on summary blocks [in, out]
281 * @bytes: byte size of the item to be skipped
282 * @count: number of items to be skipped
284 static void nilfs_skip_summary_info(struct the_nilfs
*nilfs
,
285 struct buffer_head
**pbh
,
286 unsigned int *offset
, unsigned int bytes
,
289 unsigned int rest_item_in_current_block
290 = ((*pbh
)->b_size
- *offset
) / bytes
;
292 if (count
<= rest_item_in_current_block
) {
293 *offset
+= bytes
* count
;
295 sector_t blocknr
= (*pbh
)->b_blocknr
;
296 unsigned int nitem_per_block
= (*pbh
)->b_size
/ bytes
;
299 count
-= rest_item_in_current_block
;
300 bcnt
= DIV_ROUND_UP(count
, nitem_per_block
);
301 *offset
= bytes
* (count
- (bcnt
- 1) * nitem_per_block
);
304 *pbh
= __bread(nilfs
->ns_bdev
, blocknr
+ bcnt
,
305 nilfs
->ns_blocksize
);
310 * nilfs_scan_dsync_log - get block information of a log written for data sync
311 * @nilfs: nilfs object
312 * @start_blocknr: start block number of the log
313 * @sum: log summary information
314 * @head: list head to add nilfs_recovery_block struct
316 static int nilfs_scan_dsync_log(struct the_nilfs
*nilfs
, sector_t start_blocknr
,
317 struct nilfs_segment_summary
*sum
,
318 struct list_head
*head
)
320 struct buffer_head
*bh
;
322 u32 nfinfo
, sumbytes
;
327 nfinfo
= le32_to_cpu(sum
->ss_nfinfo
);
331 sumbytes
= le32_to_cpu(sum
->ss_sumbytes
);
332 blocknr
= start_blocknr
+ DIV_ROUND_UP(sumbytes
, nilfs
->ns_blocksize
);
333 bh
= __bread(nilfs
->ns_bdev
, start_blocknr
, nilfs
->ns_blocksize
);
337 offset
= le16_to_cpu(sum
->ss_bytes
);
339 unsigned long nblocks
, ndatablk
, nnodeblk
;
340 struct nilfs_finfo
*finfo
;
342 finfo
= nilfs_read_summary_info(nilfs
, &bh
, &offset
,
344 if (unlikely(!finfo
))
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
;
352 while (ndatablk
-- > 0) {
353 struct nilfs_recovery_block
*rb
;
354 struct nilfs_binfo_v
*binfo
;
356 binfo
= nilfs_read_summary_info(nilfs
, &bh
, &offset
,
358 if (unlikely(!binfo
))
361 rb
= kmalloc(sizeof(*rb
), GFP_NOFS
);
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
);
375 blocknr
+= nnodeblk
; /* always 0 for data sync logs */
376 nilfs_skip_summary_info(nilfs
, &bh
, &offset
, sizeof(__le64
),
383 brelse(bh
); /* brelse(NULL) is just ignored */
387 static void dispose_recovery_list(struct list_head
*head
)
389 while (!list_empty(head
)) {
390 struct nilfs_recovery_block
*rb
;
392 rb
= list_first_entry(head
, struct nilfs_recovery_block
, list
);
398 struct nilfs_segment_entry
{
399 struct list_head list
;
403 static int nilfs_segment_list_add(struct list_head
*head
, __u64 segnum
)
405 struct nilfs_segment_entry
*ent
= kmalloc(sizeof(*ent
), GFP_NOFS
);
410 ent
->segnum
= segnum
;
411 INIT_LIST_HEAD(&ent
->list
);
412 list_add_tail(&ent
->list
, head
);
416 void nilfs_dispose_segment_list(struct list_head
*head
)
418 while (!list_empty(head
)) {
419 struct nilfs_segment_entry
*ent
;
421 ent
= list_first_entry(head
, struct nilfs_segment_entry
, list
);
422 list_del(&ent
->list
);
427 static int nilfs_prepare_segment_for_recovery(struct the_nilfs
*nilfs
,
428 struct super_block
*sb
,
429 struct nilfs_recovery_info
*ri
)
431 struct list_head
*head
= &ri
->ri_used_segments
;
432 struct nilfs_segment_entry
*ent
, *n
;
433 struct inode
*sufile
= nilfs
->ns_sufile
;
438 segnum
[0] = nilfs
->ns_segnum
;
439 segnum
[1] = nilfs
->ns_nextnum
;
440 segnum
[2] = ri
->ri_segnum
;
441 segnum
[3] = ri
->ri_nextnum
;
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 */
485 static int nilfs_recovery_copy_block(struct the_nilfs
*nilfs
,
486 struct nilfs_recovery_block
*rb
,
489 struct buffer_head
*bh_org
;
492 bh_org
= __bread(nilfs
->ns_bdev
, rb
->blocknr
, nilfs
->ns_blocksize
);
493 if (unlikely(!bh_org
))
496 kaddr
= kmap_atomic(page
);
497 memcpy(kaddr
+ bh_offset(bh_org
), bh_org
->b_data
, bh_org
->b_size
);
498 kunmap_atomic(kaddr
);
503 static int nilfs_recover_dsync_blocks(struct the_nilfs
*nilfs
,
504 struct super_block
*sb
,
505 struct nilfs_root
*root
,
506 struct list_head
*head
,
507 unsigned long *nr_salvaged_blocks
)
510 struct nilfs_recovery_block
*rb
, *n
;
511 unsigned blocksize
= nilfs
->ns_blocksize
;
514 int err
= 0, err2
= 0;
516 list_for_each_entry_safe(rb
, n
, head
, list
) {
517 inode
= nilfs_iget(sb
, root
, rb
->ino
);
519 err
= PTR_ERR(inode
);
524 pos
= rb
->blkoff
<< inode
->i_blkbits
;
525 err
= block_write_begin(inode
->i_mapping
, pos
, blocksize
,
526 0, &page
, nilfs_get_block
);
528 loff_t isize
= inode
->i_size
;
529 if (pos
+ blocksize
> isize
)
530 nilfs_write_failed(inode
->i_mapping
,
535 err
= nilfs_recovery_copy_block(nilfs
, rb
, page
);
539 err
= nilfs_set_file_dirty(inode
, 1);
543 block_write_end(NULL
, inode
->i_mapping
, pos
, blocksize
,
544 blocksize
, page
, NULL
);
547 page_cache_release(page
);
549 (*nr_salvaged_blocks
)++;
554 page_cache_release(page
);
558 "NILFS warning: error recovering data block "
559 "(err=%d, ino=%lu, block-offset=%llu)\n",
560 err
, (unsigned long)rb
->ino
,
561 (unsigned long long)rb
->blkoff
);
565 iput(inode
); /* iput(NULL) is just ignored */
566 list_del_init(&rb
->list
);
573 * nilfs_do_roll_forward - salvage logical segments newer than the latest
575 * @nilfs: nilfs object
576 * @sb: super block instance
577 * @ri: pointer to a nilfs_recovery_info
579 static int nilfs_do_roll_forward(struct the_nilfs
*nilfs
,
580 struct super_block
*sb
,
581 struct nilfs_root
*root
,
582 struct nilfs_recovery_info
*ri
)
584 struct buffer_head
*bh_sum
= NULL
;
585 struct nilfs_segment_summary
*sum
;
587 sector_t seg_start
, seg_end
; /* Starting/ending DBN of full segment */
588 unsigned long nsalvaged_blocks
= 0;
591 __u64 segnum
, nextnum
= 0;
594 LIST_HEAD(dsync_blocks
); /* list of data blocks to be recovered */
597 RF_DSYNC_ST
, /* scanning data-sync segments */
599 int state
= RF_INIT_ST
;
601 pseg_start
= ri
->ri_lsegs_start
;
602 seg_seq
= ri
->ri_lsegs_start_seq
;
603 segnum
= nilfs_get_segnum_of_block(nilfs
, pseg_start
);
604 nilfs_get_segment_range(nilfs
, segnum
, &seg_start
, &seg_end
);
606 while (segnum
!= ri
->ri_segnum
|| pseg_start
<= ri
->ri_pseg_start
) {
608 bh_sum
= nilfs_read_log_header(nilfs
, pseg_start
, &sum
);
614 ret
= nilfs_validate_log(nilfs
, seg_seq
, bh_sum
, sum
);
616 if (ret
== NILFS_SEG_FAIL_IO
) {
623 flags
= le16_to_cpu(sum
->ss_flags
);
624 if (flags
& NILFS_SS_SR
)
627 /* Found a valid partial segment; do recovery actions */
628 nextnum
= nilfs_get_segnum_of_block(nilfs
,
629 le64_to_cpu(sum
->ss_next
));
631 nilfs
->ns_ctime
= le64_to_cpu(sum
->ss_create
);
632 if (!(flags
& NILFS_SS_GC
))
633 nilfs
->ns_nongc_ctime
= nilfs
->ns_ctime
;
637 if (!(flags
& NILFS_SS_LOGBGN
) ||
638 !(flags
& NILFS_SS_SYNDT
))
643 if (!(flags
& NILFS_SS_SYNDT
))
646 err
= nilfs_scan_dsync_log(nilfs
, pseg_start
, sum
,
650 if (flags
& NILFS_SS_LOGEND
) {
651 err
= nilfs_recover_dsync_blocks(
652 nilfs
, sb
, root
, &dsync_blocks
,
658 break; /* Fall through to try_next_pseg */
662 if (pseg_start
== ri
->ri_lsegs_end
)
664 pseg_start
+= le32_to_cpu(sum
->ss_nblocks
);
665 if (pseg_start
< seg_end
)
670 if (pseg_start
== ri
->ri_lsegs_end
)
674 /* Looking to the next full segment */
679 nilfs_get_segment_range(nilfs
, segnum
, &seg_start
, &seg_end
);
680 pseg_start
= seg_start
;
683 if (nsalvaged_blocks
) {
684 printk(KERN_INFO
"NILFS (device %s): salvaged %lu blocks\n",
685 sb
->s_id
, nsalvaged_blocks
);
686 ri
->ri_need_recovery
= NILFS_RECOVERY_ROLLFORWARD_DONE
;
690 dispose_recovery_list(&dsync_blocks
);
697 "NILFS (device %s): Error roll-forwarding "
698 "(err=%d, pseg block=%llu). ",
699 sb
->s_id
, err
, (unsigned long long)pseg_start
);
703 static void nilfs_finish_roll_forward(struct the_nilfs
*nilfs
,
704 struct nilfs_recovery_info
*ri
)
706 struct buffer_head
*bh
;
709 if (nilfs_get_segnum_of_block(nilfs
, ri
->ri_lsegs_start
) !=
710 nilfs_get_segnum_of_block(nilfs
, ri
->ri_super_root
))
713 bh
= __getblk(nilfs
->ns_bdev
, ri
->ri_lsegs_start
, nilfs
->ns_blocksize
);
715 memset(bh
->b_data
, 0, bh
->b_size
);
716 set_buffer_dirty(bh
);
717 err
= sync_dirty_buffer(bh
);
720 "NILFS warning: buffer sync write failed during "
721 "post-cleaning of recovery.\n");
726 * nilfs_salvage_orphan_logs - salvage logs written after the latest checkpoint
727 * @nilfs: nilfs object
728 * @sb: super block instance
729 * @ri: pointer to a nilfs_recovery_info struct to store search results.
731 * Return Value: On success, 0 is returned. On error, one of the following
732 * negative error code is returned.
734 * %-EINVAL - Inconsistent filesystem state.
738 * %-ENOSPC - No space left on device (only in a panic state).
740 * %-ERESTARTSYS - Interrupted.
742 * %-ENOMEM - Insufficient memory available.
744 int nilfs_salvage_orphan_logs(struct the_nilfs
*nilfs
,
745 struct super_block
*sb
,
746 struct nilfs_recovery_info
*ri
)
748 struct nilfs_root
*root
;
751 if (ri
->ri_lsegs_start
== 0 || ri
->ri_lsegs_end
== 0)
754 err
= nilfs_attach_checkpoint(sb
, ri
->ri_cno
, true, &root
);
757 "NILFS: error loading the latest checkpoint.\n");
761 err
= nilfs_do_roll_forward(nilfs
, sb
, root
, ri
);
765 if (ri
->ri_need_recovery
== NILFS_RECOVERY_ROLLFORWARD_DONE
) {
766 err
= nilfs_prepare_segment_for_recovery(nilfs
, sb
, ri
);
768 printk(KERN_ERR
"NILFS: Error preparing segments for "
773 err
= nilfs_attach_log_writer(sb
, root
);
777 set_nilfs_discontinued(nilfs
);
778 err
= nilfs_construct_segment(sb
);
779 nilfs_detach_log_writer(sb
);
782 printk(KERN_ERR
"NILFS: Oops! recovery failed. "
787 nilfs_finish_roll_forward(nilfs
, ri
);
791 nilfs_put_root(root
);
796 * nilfs_search_super_root - search the latest valid super root
798 * @ri: pointer to a nilfs_recovery_info struct to store search results.
800 * nilfs_search_super_root() looks for the latest super-root from a partial
801 * segment pointed by the superblock. It sets up struct the_nilfs through
802 * this search. It fills nilfs_recovery_info (ri) required for recovery.
804 * Return Value: On success, 0 is returned. On error, one of the following
805 * negative error code is returned.
807 * %-EINVAL - No valid segment found
811 * %-ENOMEM - Insufficient memory available.
813 int nilfs_search_super_root(struct the_nilfs
*nilfs
,
814 struct nilfs_recovery_info
*ri
)
816 struct buffer_head
*bh_sum
= NULL
;
817 struct nilfs_segment_summary
*sum
;
818 sector_t pseg_start
, pseg_end
, sr_pseg_start
= 0;
819 sector_t seg_start
, seg_end
; /* range of full segment (block number) */
821 unsigned long nblocks
;
824 __u64 segnum
, nextnum
= 0;
827 int empty_seg
= 0, scan_newer
= 0;
830 pseg_start
= nilfs
->ns_last_pseg
;
831 seg_seq
= nilfs
->ns_last_seq
;
832 cno
= nilfs
->ns_last_cno
;
833 segnum
= nilfs_get_segnum_of_block(nilfs
, pseg_start
);
835 /* Calculate range of segment */
836 nilfs_get_segment_range(nilfs
, segnum
, &seg_start
, &seg_end
);
838 /* Read ahead segment */
841 __breadahead(nilfs
->ns_bdev
, b
++, nilfs
->ns_blocksize
);
845 ret
= NILFS_SEG_FAIL_IO
;
846 bh_sum
= nilfs_read_log_header(nilfs
, pseg_start
, &sum
);
850 ret
= nilfs_validate_log(nilfs
, seg_seq
, bh_sum
, sum
);
852 if (ret
== NILFS_SEG_FAIL_IO
)
857 nblocks
= le32_to_cpu(sum
->ss_nblocks
);
858 pseg_end
= pseg_start
+ nblocks
- 1;
859 if (unlikely(pseg_end
> seg_end
)) {
860 ret
= NILFS_SEG_FAIL_CONSISTENCY
;
864 /* A valid partial segment */
865 ri
->ri_pseg_start
= pseg_start
;
866 ri
->ri_seq
= seg_seq
;
867 ri
->ri_segnum
= segnum
;
868 nextnum
= nilfs_get_segnum_of_block(nilfs
,
869 le64_to_cpu(sum
->ss_next
));
870 ri
->ri_nextnum
= nextnum
;
873 flags
= le16_to_cpu(sum
->ss_flags
);
874 if (!(flags
& NILFS_SS_SR
) && !scan_newer
) {
875 /* This will never happen because a superblock
876 (last_segment) always points to a pseg
877 having a super root. */
878 ret
= NILFS_SEG_FAIL_CONSISTENCY
;
882 if (pseg_start
== seg_start
) {
883 nilfs_get_segment_range(nilfs
, nextnum
, &b
, &end
);
885 __breadahead(nilfs
->ns_bdev
, b
++,
886 nilfs
->ns_blocksize
);
888 if (!(flags
& NILFS_SS_SR
)) {
889 if (!ri
->ri_lsegs_start
&& (flags
& NILFS_SS_LOGBGN
)) {
890 ri
->ri_lsegs_start
= pseg_start
;
891 ri
->ri_lsegs_start_seq
= seg_seq
;
893 if (flags
& NILFS_SS_LOGEND
)
894 ri
->ri_lsegs_end
= pseg_start
;
898 /* A valid super root was found. */
900 ri
->ri_super_root
= pseg_end
;
901 ri
->ri_lsegs_start
= ri
->ri_lsegs_end
= 0;
903 nilfs_dispose_segment_list(&segments
);
904 sr_pseg_start
= pseg_start
;
905 nilfs
->ns_pseg_offset
= pseg_start
+ nblocks
- seg_start
;
906 nilfs
->ns_seg_seq
= seg_seq
;
907 nilfs
->ns_segnum
= segnum
;
908 nilfs
->ns_cno
= cno
; /* nilfs->ns_cno = ri->ri_cno + 1 */
909 nilfs
->ns_ctime
= le64_to_cpu(sum
->ss_create
);
910 nilfs
->ns_nextnum
= nextnum
;
913 ri
->ri_need_recovery
= NILFS_RECOVERY_SR_UPDATED
;
915 if (nilfs
->ns_mount_state
& NILFS_VALID_FS
)
916 goto super_root_found
;
921 /* Standing on a course, or met an inconsistent state */
922 pseg_start
+= nblocks
;
923 if (pseg_start
< seg_end
)
931 * This can happen if a checkpoint was written without
932 * barriers, or as a result of an I/O failure.
937 /* Looking to the next full segment */
939 goto super_root_found
; /* found a valid super root */
941 ret
= nilfs_segment_list_add(&segments
, segnum
);
947 nilfs_get_segment_range(nilfs
, segnum
, &seg_start
, &seg_end
);
948 pseg_start
= seg_start
;
952 /* Updating pointers relating to the latest checkpoint */
954 list_splice_tail(&segments
, &ri
->ri_used_segments
);
955 nilfs
->ns_last_pseg
= sr_pseg_start
;
956 nilfs
->ns_last_seq
= nilfs
->ns_seg_seq
;
957 nilfs
->ns_last_cno
= ri
->ri_cno
;
962 nilfs_dispose_segment_list(&segments
);
963 return (ret
< 0) ? ret
: nilfs_warn_segment_error(ret
);