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 * Written by Ryusuke Konishi.
19 #include <linux/buffer_head.h>
20 #include <linux/blkdev.h>
21 #include <linux/swap.h>
22 #include <linux/slab.h>
23 #include <linux/crc32.h>
31 * Segment check result
35 NILFS_SEG_NO_SUPER_ROOT
,
39 NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT
,
40 NILFS_SEG_FAIL_CHECKSUM_FULL
,
41 NILFS_SEG_FAIL_CONSISTENCY
,
44 /* work structure for recovery */
45 struct nilfs_recovery_block
{
46 ino_t ino
; /* Inode number of the file that this block
48 sector_t blocknr
; /* block number */
49 __u64 vblocknr
; /* virtual block number */
50 unsigned long blkoff
; /* File offset of the data block (per block) */
51 struct list_head list
;
55 static int nilfs_warn_segment_error(int err
)
58 case NILFS_SEG_FAIL_IO
:
60 "NILFS warning: I/O error on loading last segment\n");
62 case NILFS_SEG_FAIL_MAGIC
:
64 "NILFS warning: Segment magic number invalid\n");
66 case NILFS_SEG_FAIL_SEQ
:
68 "NILFS warning: Sequence number mismatch\n");
70 case NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT
:
72 "NILFS warning: Checksum error in super root\n");
74 case NILFS_SEG_FAIL_CHECKSUM_FULL
:
76 "NILFS warning: Checksum error in segment payload\n");
78 case NILFS_SEG_FAIL_CONSISTENCY
:
80 "NILFS warning: Inconsistent segment\n");
82 case NILFS_SEG_NO_SUPER_ROOT
:
84 "NILFS warning: No super root in the last segment\n");
91 * nilfs_compute_checksum - compute checksum of blocks continuously
92 * @nilfs: nilfs object
93 * @bhs: buffer head of start block
94 * @sum: place to store result
95 * @offset: offset bytes in the first block
96 * @check_bytes: number of bytes to be checked
97 * @start: DBN of start block
98 * @nblock: number of blocks to be checked
100 static int nilfs_compute_checksum(struct the_nilfs
*nilfs
,
101 struct buffer_head
*bhs
, u32
*sum
,
102 unsigned long offset
, u64 check_bytes
,
103 sector_t start
, unsigned long nblock
)
105 unsigned int blocksize
= nilfs
->ns_blocksize
;
109 BUG_ON(offset
>= blocksize
);
110 check_bytes
-= offset
;
111 size
= min_t(u64
, check_bytes
, blocksize
- offset
);
112 crc
= crc32_le(nilfs
->ns_crc_seed
,
113 (unsigned char *)bhs
->b_data
+ offset
, size
);
116 struct buffer_head
*bh
;
118 bh
= __bread(nilfs
->ns_bdev
, ++start
, blocksize
);
122 size
= min_t(u64
, check_bytes
, blocksize
);
123 crc
= crc32_le(crc
, bh
->b_data
, size
);
125 } while (--nblock
> 0);
132 * nilfs_read_super_root_block - read super root block
133 * @nilfs: nilfs object
134 * @sr_block: disk block number of the super root block
135 * @pbh: address of a buffer_head pointer to return super root buffer
136 * @check: CRC check flag
138 int nilfs_read_super_root_block(struct the_nilfs
*nilfs
, sector_t sr_block
,
139 struct buffer_head
**pbh
, int check
)
141 struct buffer_head
*bh_sr
;
142 struct nilfs_super_root
*sr
;
147 bh_sr
= __bread(nilfs
->ns_bdev
, sr_block
, nilfs
->ns_blocksize
);
148 if (unlikely(!bh_sr
)) {
149 ret
= NILFS_SEG_FAIL_IO
;
153 sr
= (struct nilfs_super_root
*)bh_sr
->b_data
;
155 unsigned int bytes
= le16_to_cpu(sr
->sr_bytes
);
157 if (bytes
== 0 || bytes
> nilfs
->ns_blocksize
) {
158 ret
= NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT
;
161 if (nilfs_compute_checksum(
162 nilfs
, bh_sr
, &crc
, sizeof(sr
->sr_sum
), bytes
,
164 ret
= NILFS_SEG_FAIL_IO
;
167 if (crc
!= le32_to_cpu(sr
->sr_sum
)) {
168 ret
= NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT
;
179 return nilfs_warn_segment_error(ret
);
183 * nilfs_read_log_header - read summary header of the specified log
184 * @nilfs: nilfs object
185 * @start_blocknr: start block number of the log
186 * @sum: pointer to return segment summary structure
188 static struct buffer_head
*
189 nilfs_read_log_header(struct the_nilfs
*nilfs
, sector_t start_blocknr
,
190 struct nilfs_segment_summary
**sum
)
192 struct buffer_head
*bh_sum
;
194 bh_sum
= __bread(nilfs
->ns_bdev
, start_blocknr
, nilfs
->ns_blocksize
);
196 *sum
= (struct nilfs_segment_summary
*)bh_sum
->b_data
;
201 * nilfs_validate_log - verify consistency of log
202 * @nilfs: nilfs object
203 * @seg_seq: sequence number of segment
204 * @bh_sum: buffer head of summary block
205 * @sum: segment summary struct
207 static int nilfs_validate_log(struct the_nilfs
*nilfs
, u64 seg_seq
,
208 struct buffer_head
*bh_sum
,
209 struct nilfs_segment_summary
*sum
)
211 unsigned long nblock
;
215 ret
= NILFS_SEG_FAIL_MAGIC
;
216 if (le32_to_cpu(sum
->ss_magic
) != NILFS_SEGSUM_MAGIC
)
219 ret
= NILFS_SEG_FAIL_SEQ
;
220 if (le64_to_cpu(sum
->ss_seq
) != seg_seq
)
223 nblock
= le32_to_cpu(sum
->ss_nblocks
);
224 ret
= NILFS_SEG_FAIL_CONSISTENCY
;
225 if (unlikely(nblock
== 0 || nblock
> nilfs
->ns_blocks_per_segment
))
226 /* This limits the number of blocks read in the CRC check */
229 ret
= NILFS_SEG_FAIL_IO
;
230 if (nilfs_compute_checksum(nilfs
, bh_sum
, &crc
, sizeof(sum
->ss_datasum
),
231 ((u64
)nblock
<< nilfs
->ns_blocksize_bits
),
232 bh_sum
->b_blocknr
, nblock
))
235 ret
= NILFS_SEG_FAIL_CHECKSUM_FULL
;
236 if (crc
!= le32_to_cpu(sum
->ss_datasum
))
244 * nilfs_read_summary_info - read an item on summary blocks of a log
245 * @nilfs: nilfs object
246 * @pbh: the current buffer head on summary blocks [in, out]
247 * @offset: the current byte offset on summary blocks [in, out]
248 * @bytes: byte size of the item to be read
250 static void *nilfs_read_summary_info(struct the_nilfs
*nilfs
,
251 struct buffer_head
**pbh
,
252 unsigned int *offset
, unsigned int bytes
)
257 BUG_ON((*pbh
)->b_size
< *offset
);
258 if (bytes
> (*pbh
)->b_size
- *offset
) {
259 blocknr
= (*pbh
)->b_blocknr
;
261 *pbh
= __bread(nilfs
->ns_bdev
, blocknr
+ 1,
262 nilfs
->ns_blocksize
);
267 ptr
= (*pbh
)->b_data
+ *offset
;
273 * nilfs_skip_summary_info - skip items on summary blocks of a log
274 * @nilfs: nilfs object
275 * @pbh: the current buffer head on summary blocks [in, out]
276 * @offset: the current byte offset on summary blocks [in, out]
277 * @bytes: byte size of the item to be skipped
278 * @count: number of items to be skipped
280 static void nilfs_skip_summary_info(struct the_nilfs
*nilfs
,
281 struct buffer_head
**pbh
,
282 unsigned int *offset
, unsigned int bytes
,
285 unsigned int rest_item_in_current_block
286 = ((*pbh
)->b_size
- *offset
) / bytes
;
288 if (count
<= rest_item_in_current_block
) {
289 *offset
+= bytes
* count
;
291 sector_t blocknr
= (*pbh
)->b_blocknr
;
292 unsigned int nitem_per_block
= (*pbh
)->b_size
/ bytes
;
295 count
-= rest_item_in_current_block
;
296 bcnt
= DIV_ROUND_UP(count
, nitem_per_block
);
297 *offset
= bytes
* (count
- (bcnt
- 1) * nitem_per_block
);
300 *pbh
= __bread(nilfs
->ns_bdev
, blocknr
+ bcnt
,
301 nilfs
->ns_blocksize
);
306 * nilfs_scan_dsync_log - get block information of a log written for data sync
307 * @nilfs: nilfs object
308 * @start_blocknr: start block number of the log
309 * @sum: log summary information
310 * @head: list head to add nilfs_recovery_block struct
312 static int nilfs_scan_dsync_log(struct the_nilfs
*nilfs
, sector_t start_blocknr
,
313 struct nilfs_segment_summary
*sum
,
314 struct list_head
*head
)
316 struct buffer_head
*bh
;
318 u32 nfinfo
, sumbytes
;
323 nfinfo
= le32_to_cpu(sum
->ss_nfinfo
);
327 sumbytes
= le32_to_cpu(sum
->ss_sumbytes
);
328 blocknr
= start_blocknr
+ DIV_ROUND_UP(sumbytes
, nilfs
->ns_blocksize
);
329 bh
= __bread(nilfs
->ns_bdev
, start_blocknr
, nilfs
->ns_blocksize
);
333 offset
= le16_to_cpu(sum
->ss_bytes
);
335 unsigned long nblocks
, ndatablk
, nnodeblk
;
336 struct nilfs_finfo
*finfo
;
338 finfo
= nilfs_read_summary_info(nilfs
, &bh
, &offset
,
340 if (unlikely(!finfo
))
343 ino
= le64_to_cpu(finfo
->fi_ino
);
344 nblocks
= le32_to_cpu(finfo
->fi_nblocks
);
345 ndatablk
= le32_to_cpu(finfo
->fi_ndatablk
);
346 nnodeblk
= nblocks
- ndatablk
;
348 while (ndatablk
-- > 0) {
349 struct nilfs_recovery_block
*rb
;
350 struct nilfs_binfo_v
*binfo
;
352 binfo
= nilfs_read_summary_info(nilfs
, &bh
, &offset
,
354 if (unlikely(!binfo
))
357 rb
= kmalloc(sizeof(*rb
), GFP_NOFS
);
363 rb
->blocknr
= blocknr
++;
364 rb
->vblocknr
= le64_to_cpu(binfo
->bi_vblocknr
);
365 rb
->blkoff
= le64_to_cpu(binfo
->bi_blkoff
);
366 /* INIT_LIST_HEAD(&rb->list); */
367 list_add_tail(&rb
->list
, head
);
371 blocknr
+= nnodeblk
; /* always 0 for data sync logs */
372 nilfs_skip_summary_info(nilfs
, &bh
, &offset
, sizeof(__le64
),
379 brelse(bh
); /* brelse(NULL) is just ignored */
383 static void dispose_recovery_list(struct list_head
*head
)
385 while (!list_empty(head
)) {
386 struct nilfs_recovery_block
*rb
;
388 rb
= list_first_entry(head
, struct nilfs_recovery_block
, list
);
394 struct nilfs_segment_entry
{
395 struct list_head list
;
399 static int nilfs_segment_list_add(struct list_head
*head
, __u64 segnum
)
401 struct nilfs_segment_entry
*ent
= kmalloc(sizeof(*ent
), GFP_NOFS
);
406 ent
->segnum
= segnum
;
407 INIT_LIST_HEAD(&ent
->list
);
408 list_add_tail(&ent
->list
, head
);
412 void nilfs_dispose_segment_list(struct list_head
*head
)
414 while (!list_empty(head
)) {
415 struct nilfs_segment_entry
*ent
;
417 ent
= list_first_entry(head
, struct nilfs_segment_entry
, list
);
418 list_del(&ent
->list
);
423 static int nilfs_prepare_segment_for_recovery(struct the_nilfs
*nilfs
,
424 struct super_block
*sb
,
425 struct nilfs_recovery_info
*ri
)
427 struct list_head
*head
= &ri
->ri_used_segments
;
428 struct nilfs_segment_entry
*ent
, *n
;
429 struct inode
*sufile
= nilfs
->ns_sufile
;
434 segnum
[0] = nilfs
->ns_segnum
;
435 segnum
[1] = nilfs
->ns_nextnum
;
436 segnum
[2] = ri
->ri_segnum
;
437 segnum
[3] = ri
->ri_nextnum
;
440 * Releasing the next segment of the latest super root.
441 * The next segment is invalidated by this recovery.
443 err
= nilfs_sufile_free(sufile
, segnum
[1]);
447 for (i
= 1; i
< 4; i
++) {
448 err
= nilfs_segment_list_add(head
, segnum
[i
]);
454 * Collecting segments written after the latest super root.
455 * These are marked dirty to avoid being reallocated in the next write.
457 list_for_each_entry_safe(ent
, n
, head
, list
) {
458 if (ent
->segnum
!= segnum
[0]) {
459 err
= nilfs_sufile_scrap(sufile
, ent
->segnum
);
463 list_del(&ent
->list
);
467 /* Allocate new segments for recovery */
468 err
= nilfs_sufile_alloc(sufile
, &segnum
[0]);
472 nilfs
->ns_pseg_offset
= 0;
473 nilfs
->ns_seg_seq
= ri
->ri_seq
+ 2;
474 nilfs
->ns_nextnum
= nilfs
->ns_segnum
= segnum
[0];
477 /* No need to recover sufile because it will be destroyed on error */
481 static int nilfs_recovery_copy_block(struct the_nilfs
*nilfs
,
482 struct nilfs_recovery_block
*rb
,
485 struct buffer_head
*bh_org
;
488 bh_org
= __bread(nilfs
->ns_bdev
, rb
->blocknr
, nilfs
->ns_blocksize
);
489 if (unlikely(!bh_org
))
492 kaddr
= kmap_atomic(page
);
493 memcpy(kaddr
+ bh_offset(bh_org
), bh_org
->b_data
, bh_org
->b_size
);
494 kunmap_atomic(kaddr
);
499 static int nilfs_recover_dsync_blocks(struct the_nilfs
*nilfs
,
500 struct super_block
*sb
,
501 struct nilfs_root
*root
,
502 struct list_head
*head
,
503 unsigned long *nr_salvaged_blocks
)
506 struct nilfs_recovery_block
*rb
, *n
;
507 unsigned int blocksize
= nilfs
->ns_blocksize
;
510 int err
= 0, err2
= 0;
512 list_for_each_entry_safe(rb
, n
, head
, list
) {
513 inode
= nilfs_iget(sb
, root
, rb
->ino
);
515 err
= PTR_ERR(inode
);
520 pos
= rb
->blkoff
<< inode
->i_blkbits
;
521 err
= block_write_begin(inode
->i_mapping
, pos
, blocksize
,
522 0, &page
, nilfs_get_block
);
524 loff_t isize
= inode
->i_size
;
526 if (pos
+ blocksize
> isize
)
527 nilfs_write_failed(inode
->i_mapping
,
532 err
= nilfs_recovery_copy_block(nilfs
, rb
, page
);
536 err
= nilfs_set_file_dirty(inode
, 1);
540 block_write_end(NULL
, inode
->i_mapping
, pos
, blocksize
,
541 blocksize
, page
, NULL
);
546 (*nr_salvaged_blocks
)++;
555 "NILFS warning: error recovering data block "
556 "(err=%d, ino=%lu, block-offset=%llu)\n",
557 err
, (unsigned long)rb
->ino
,
558 (unsigned long long)rb
->blkoff
);
562 iput(inode
); /* iput(NULL) is just ignored */
563 list_del_init(&rb
->list
);
570 * nilfs_do_roll_forward - salvage logical segments newer than the latest
572 * @nilfs: nilfs object
573 * @sb: super block instance
574 * @ri: pointer to a nilfs_recovery_info
576 static int nilfs_do_roll_forward(struct the_nilfs
*nilfs
,
577 struct super_block
*sb
,
578 struct nilfs_root
*root
,
579 struct nilfs_recovery_info
*ri
)
581 struct buffer_head
*bh_sum
= NULL
;
582 struct nilfs_segment_summary
*sum
= NULL
;
584 sector_t seg_start
, seg_end
; /* Starting/ending DBN of full segment */
585 unsigned long nsalvaged_blocks
= 0;
588 __u64 segnum
, nextnum
= 0;
591 LIST_HEAD(dsync_blocks
); /* list of data blocks to be recovered */
594 RF_DSYNC_ST
, /* scanning data-sync segments */
596 int state
= RF_INIT_ST
;
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
);
603 while (segnum
!= ri
->ri_segnum
|| pseg_start
<= ri
->ri_pseg_start
) {
605 bh_sum
= nilfs_read_log_header(nilfs
, pseg_start
, &sum
);
611 ret
= nilfs_validate_log(nilfs
, seg_seq
, bh_sum
, sum
);
613 if (ret
== NILFS_SEG_FAIL_IO
) {
620 flags
= le16_to_cpu(sum
->ss_flags
);
621 if (flags
& NILFS_SS_SR
)
624 /* Found a valid partial segment; do recovery actions */
625 nextnum
= nilfs_get_segnum_of_block(nilfs
,
626 le64_to_cpu(sum
->ss_next
));
628 nilfs
->ns_ctime
= le64_to_cpu(sum
->ss_create
);
629 if (!(flags
& NILFS_SS_GC
))
630 nilfs
->ns_nongc_ctime
= nilfs
->ns_ctime
;
634 if (!(flags
& NILFS_SS_LOGBGN
) ||
635 !(flags
& NILFS_SS_SYNDT
))
640 if (!(flags
& NILFS_SS_SYNDT
))
643 err
= nilfs_scan_dsync_log(nilfs
, pseg_start
, sum
,
647 if (flags
& NILFS_SS_LOGEND
) {
648 err
= nilfs_recover_dsync_blocks(
649 nilfs
, sb
, root
, &dsync_blocks
,
655 break; /* Fall through to try_next_pseg */
659 if (pseg_start
== ri
->ri_lsegs_end
)
661 pseg_start
+= le32_to_cpu(sum
->ss_nblocks
);
662 if (pseg_start
< seg_end
)
667 if (pseg_start
== ri
->ri_lsegs_end
)
671 /* Looking to the next full segment */
676 nilfs_get_segment_range(nilfs
, segnum
, &seg_start
, &seg_end
);
677 pseg_start
= seg_start
;
680 if (nsalvaged_blocks
) {
681 printk(KERN_INFO
"NILFS (device %s): salvaged %lu blocks\n",
682 sb
->s_id
, nsalvaged_blocks
);
683 ri
->ri_need_recovery
= NILFS_RECOVERY_ROLLFORWARD_DONE
;
687 dispose_recovery_list(&dsync_blocks
);
694 "NILFS (device %s): Error roll-forwarding "
695 "(err=%d, pseg block=%llu). ",
696 sb
->s_id
, err
, (unsigned long long)pseg_start
);
700 static void nilfs_finish_roll_forward(struct the_nilfs
*nilfs
,
701 struct nilfs_recovery_info
*ri
)
703 struct buffer_head
*bh
;
706 if (nilfs_get_segnum_of_block(nilfs
, ri
->ri_lsegs_start
) !=
707 nilfs_get_segnum_of_block(nilfs
, ri
->ri_super_root
))
710 bh
= __getblk(nilfs
->ns_bdev
, ri
->ri_lsegs_start
, nilfs
->ns_blocksize
);
712 memset(bh
->b_data
, 0, bh
->b_size
);
713 set_buffer_dirty(bh
);
714 err
= sync_dirty_buffer(bh
);
717 "NILFS warning: buffer sync write failed during "
718 "post-cleaning of recovery.\n");
723 * nilfs_salvage_orphan_logs - salvage logs written after the latest checkpoint
724 * @nilfs: nilfs object
725 * @sb: super block instance
726 * @ri: pointer to a nilfs_recovery_info struct to store search results.
728 * Return Value: On success, 0 is returned. On error, one of the following
729 * negative error code is returned.
731 * %-EINVAL - Inconsistent filesystem state.
735 * %-ENOSPC - No space left on device (only in a panic state).
737 * %-ERESTARTSYS - Interrupted.
739 * %-ENOMEM - Insufficient memory available.
741 int nilfs_salvage_orphan_logs(struct the_nilfs
*nilfs
,
742 struct super_block
*sb
,
743 struct nilfs_recovery_info
*ri
)
745 struct nilfs_root
*root
;
748 if (ri
->ri_lsegs_start
== 0 || ri
->ri_lsegs_end
== 0)
751 err
= nilfs_attach_checkpoint(sb
, ri
->ri_cno
, true, &root
);
754 "NILFS: error loading the latest checkpoint.\n");
758 err
= nilfs_do_roll_forward(nilfs
, sb
, root
, ri
);
762 if (ri
->ri_need_recovery
== NILFS_RECOVERY_ROLLFORWARD_DONE
) {
763 err
= nilfs_prepare_segment_for_recovery(nilfs
, sb
, ri
);
765 printk(KERN_ERR
"NILFS: Error preparing segments for "
770 err
= nilfs_attach_log_writer(sb
, root
);
774 set_nilfs_discontinued(nilfs
);
775 err
= nilfs_construct_segment(sb
);
776 nilfs_detach_log_writer(sb
);
779 printk(KERN_ERR
"NILFS: Oops! recovery failed. "
784 nilfs_finish_roll_forward(nilfs
, ri
);
788 nilfs_put_root(root
);
793 * nilfs_search_super_root - search the latest valid super root
795 * @ri: pointer to a nilfs_recovery_info struct to store search results.
797 * nilfs_search_super_root() looks for the latest super-root from a partial
798 * segment pointed by the superblock. It sets up struct the_nilfs through
799 * this search. It fills nilfs_recovery_info (ri) required for recovery.
801 * Return Value: On success, 0 is returned. On error, one of the following
802 * negative error code is returned.
804 * %-EINVAL - No valid segment found
808 * %-ENOMEM - Insufficient memory available.
810 int nilfs_search_super_root(struct the_nilfs
*nilfs
,
811 struct nilfs_recovery_info
*ri
)
813 struct buffer_head
*bh_sum
= NULL
;
814 struct nilfs_segment_summary
*sum
= NULL
;
815 sector_t pseg_start
, pseg_end
, sr_pseg_start
= 0;
816 sector_t seg_start
, seg_end
; /* range of full segment (block number) */
818 unsigned long nblocks
;
821 __u64 segnum
, nextnum
= 0;
824 int empty_seg
= 0, scan_newer
= 0;
827 pseg_start
= nilfs
->ns_last_pseg
;
828 seg_seq
= nilfs
->ns_last_seq
;
829 cno
= nilfs
->ns_last_cno
;
830 segnum
= nilfs_get_segnum_of_block(nilfs
, pseg_start
);
832 /* Calculate range of segment */
833 nilfs_get_segment_range(nilfs
, segnum
, &seg_start
, &seg_end
);
835 /* Read ahead segment */
838 __breadahead(nilfs
->ns_bdev
, b
++, nilfs
->ns_blocksize
);
842 ret
= NILFS_SEG_FAIL_IO
;
843 bh_sum
= nilfs_read_log_header(nilfs
, pseg_start
, &sum
);
847 ret
= nilfs_validate_log(nilfs
, seg_seq
, bh_sum
, sum
);
849 if (ret
== NILFS_SEG_FAIL_IO
)
854 nblocks
= le32_to_cpu(sum
->ss_nblocks
);
855 pseg_end
= pseg_start
+ nblocks
- 1;
856 if (unlikely(pseg_end
> seg_end
)) {
857 ret
= NILFS_SEG_FAIL_CONSISTENCY
;
861 /* A valid partial segment */
862 ri
->ri_pseg_start
= pseg_start
;
863 ri
->ri_seq
= seg_seq
;
864 ri
->ri_segnum
= segnum
;
865 nextnum
= nilfs_get_segnum_of_block(nilfs
,
866 le64_to_cpu(sum
->ss_next
));
867 ri
->ri_nextnum
= nextnum
;
870 flags
= le16_to_cpu(sum
->ss_flags
);
871 if (!(flags
& NILFS_SS_SR
) && !scan_newer
) {
872 /* This will never happen because a superblock
873 (last_segment) always points to a pseg
874 having a super root. */
875 ret
= NILFS_SEG_FAIL_CONSISTENCY
;
879 if (pseg_start
== seg_start
) {
880 nilfs_get_segment_range(nilfs
, nextnum
, &b
, &end
);
882 __breadahead(nilfs
->ns_bdev
, b
++,
883 nilfs
->ns_blocksize
);
885 if (!(flags
& NILFS_SS_SR
)) {
886 if (!ri
->ri_lsegs_start
&& (flags
& NILFS_SS_LOGBGN
)) {
887 ri
->ri_lsegs_start
= pseg_start
;
888 ri
->ri_lsegs_start_seq
= seg_seq
;
890 if (flags
& NILFS_SS_LOGEND
)
891 ri
->ri_lsegs_end
= pseg_start
;
895 /* A valid super root was found. */
897 ri
->ri_super_root
= pseg_end
;
898 ri
->ri_lsegs_start
= ri
->ri_lsegs_end
= 0;
900 nilfs_dispose_segment_list(&segments
);
901 sr_pseg_start
= pseg_start
;
902 nilfs
->ns_pseg_offset
= pseg_start
+ nblocks
- seg_start
;
903 nilfs
->ns_seg_seq
= seg_seq
;
904 nilfs
->ns_segnum
= segnum
;
905 nilfs
->ns_cno
= cno
; /* nilfs->ns_cno = ri->ri_cno + 1 */
906 nilfs
->ns_ctime
= le64_to_cpu(sum
->ss_create
);
907 nilfs
->ns_nextnum
= nextnum
;
910 ri
->ri_need_recovery
= NILFS_RECOVERY_SR_UPDATED
;
912 if (nilfs
->ns_mount_state
& NILFS_VALID_FS
)
913 goto super_root_found
;
918 /* Standing on a course, or met an inconsistent state */
919 pseg_start
+= nblocks
;
920 if (pseg_start
< seg_end
)
928 * This can happen if a checkpoint was written without
929 * barriers, or as a result of an I/O failure.
934 /* Looking to the next full segment */
936 goto super_root_found
; /* found a valid super root */
938 ret
= nilfs_segment_list_add(&segments
, segnum
);
944 nilfs_get_segment_range(nilfs
, segnum
, &seg_start
, &seg_end
);
945 pseg_start
= seg_start
;
949 /* Updating pointers relating to the latest checkpoint */
951 list_splice_tail(&segments
, &ri
->ri_used_segments
);
952 nilfs
->ns_last_pseg
= sr_pseg_start
;
953 nilfs
->ns_last_seq
= nilfs
->ns_seg_seq
;
954 nilfs
->ns_last_cno
= ri
->ri_cno
;
959 nilfs_dispose_segment_list(&segments
);
960 return (ret
< 0) ? ret
: nilfs_warn_segment_error(ret
);