1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
7 * Written by Ryusuke Konishi.
10 #include <linux/buffer_head.h>
11 #include <linux/blkdev.h>
12 #include <linux/swap.h>
13 #include <linux/slab.h>
14 #include <linux/crc32.h>
22 * Segment check result
26 NILFS_SEG_NO_SUPER_ROOT
,
30 NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT
,
31 NILFS_SEG_FAIL_CHECKSUM_FULL
,
32 NILFS_SEG_FAIL_CONSISTENCY
,
35 /* work structure for recovery */
36 struct nilfs_recovery_block
{
38 * Inode number of the file that this block
41 sector_t blocknr
; /* block number */
42 __u64 vblocknr
; /* virtual block number */
43 unsigned long blkoff
; /* File offset of the data block (per block) */
44 struct list_head list
;
48 static int nilfs_warn_segment_error(struct super_block
*sb
, int err
)
50 const char *msg
= NULL
;
53 case NILFS_SEG_FAIL_IO
:
54 nilfs_err(sb
, "I/O error reading segment");
56 case NILFS_SEG_FAIL_MAGIC
:
57 msg
= "Magic number mismatch";
59 case NILFS_SEG_FAIL_SEQ
:
60 msg
= "Sequence number mismatch";
62 case NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT
:
63 msg
= "Checksum error in super root";
65 case NILFS_SEG_FAIL_CHECKSUM_FULL
:
66 msg
= "Checksum error in segment payload";
68 case NILFS_SEG_FAIL_CONSISTENCY
:
69 msg
= "Inconsistency found";
71 case NILFS_SEG_NO_SUPER_ROOT
:
72 msg
= "No super root in the last segment";
75 nilfs_err(sb
, "unrecognized segment error %d", err
);
78 nilfs_warn(sb
, "invalid segment: %s", msg
);
83 * nilfs_compute_checksum - compute checksum of blocks continuously
84 * @nilfs: nilfs object
85 * @bhs: buffer head of start block
86 * @sum: place to store result
87 * @offset: offset bytes in the first block
88 * @check_bytes: number of bytes to be checked
89 * @start: DBN of start block
90 * @nblock: number of blocks to be checked
92 static int nilfs_compute_checksum(struct the_nilfs
*nilfs
,
93 struct buffer_head
*bhs
, u32
*sum
,
94 unsigned long offset
, u64 check_bytes
,
95 sector_t start
, unsigned long nblock
)
97 unsigned int blocksize
= nilfs
->ns_blocksize
;
101 BUG_ON(offset
>= blocksize
);
102 check_bytes
-= offset
;
103 size
= min_t(u64
, check_bytes
, blocksize
- offset
);
104 crc
= crc32_le(nilfs
->ns_crc_seed
,
105 (unsigned char *)bhs
->b_data
+ offset
, size
);
108 struct buffer_head
*bh
;
110 bh
= __bread(nilfs
->ns_bdev
, ++start
, blocksize
);
114 size
= min_t(u64
, check_bytes
, blocksize
);
115 crc
= crc32_le(crc
, bh
->b_data
, size
);
117 } while (--nblock
> 0);
124 * nilfs_read_super_root_block - read super root block
125 * @nilfs: nilfs object
126 * @sr_block: disk block number of the super root block
127 * @pbh: address of a buffer_head pointer to return super root buffer
128 * @check: CRC check flag
130 int nilfs_read_super_root_block(struct the_nilfs
*nilfs
, sector_t sr_block
,
131 struct buffer_head
**pbh
, int check
)
133 struct buffer_head
*bh_sr
;
134 struct nilfs_super_root
*sr
;
139 bh_sr
= __bread(nilfs
->ns_bdev
, sr_block
, nilfs
->ns_blocksize
);
140 if (unlikely(!bh_sr
)) {
141 ret
= NILFS_SEG_FAIL_IO
;
145 sr
= (struct nilfs_super_root
*)bh_sr
->b_data
;
147 unsigned int bytes
= le16_to_cpu(sr
->sr_bytes
);
149 if (bytes
== 0 || bytes
> nilfs
->ns_blocksize
) {
150 ret
= NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT
;
153 if (nilfs_compute_checksum(
154 nilfs
, bh_sr
, &crc
, sizeof(sr
->sr_sum
), bytes
,
156 ret
= NILFS_SEG_FAIL_IO
;
159 if (crc
!= le32_to_cpu(sr
->sr_sum
)) {
160 ret
= NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT
;
171 return nilfs_warn_segment_error(nilfs
->ns_sb
, ret
);
175 * nilfs_read_log_header - read summary header of the specified log
176 * @nilfs: nilfs object
177 * @start_blocknr: start block number of the log
178 * @sum: pointer to return segment summary structure
180 static struct buffer_head
*
181 nilfs_read_log_header(struct the_nilfs
*nilfs
, sector_t start_blocknr
,
182 struct nilfs_segment_summary
**sum
)
184 struct buffer_head
*bh_sum
;
186 bh_sum
= __bread(nilfs
->ns_bdev
, start_blocknr
, nilfs
->ns_blocksize
);
188 *sum
= (struct nilfs_segment_summary
*)bh_sum
->b_data
;
193 * nilfs_validate_log - verify consistency of log
194 * @nilfs: nilfs object
195 * @seg_seq: sequence number of segment
196 * @bh_sum: buffer head of summary block
197 * @sum: segment summary struct
199 static int nilfs_validate_log(struct the_nilfs
*nilfs
, u64 seg_seq
,
200 struct buffer_head
*bh_sum
,
201 struct nilfs_segment_summary
*sum
)
203 unsigned long nblock
;
207 ret
= NILFS_SEG_FAIL_MAGIC
;
208 if (le32_to_cpu(sum
->ss_magic
) != NILFS_SEGSUM_MAGIC
)
211 ret
= NILFS_SEG_FAIL_SEQ
;
212 if (le64_to_cpu(sum
->ss_seq
) != seg_seq
)
215 nblock
= le32_to_cpu(sum
->ss_nblocks
);
216 ret
= NILFS_SEG_FAIL_CONSISTENCY
;
217 if (unlikely(nblock
== 0 || nblock
> nilfs
->ns_blocks_per_segment
))
218 /* This limits the number of blocks read in the CRC check */
221 ret
= NILFS_SEG_FAIL_IO
;
222 if (nilfs_compute_checksum(nilfs
, bh_sum
, &crc
, sizeof(sum
->ss_datasum
),
223 ((u64
)nblock
<< nilfs
->ns_blocksize_bits
),
224 bh_sum
->b_blocknr
, nblock
))
227 ret
= NILFS_SEG_FAIL_CHECKSUM_FULL
;
228 if (crc
!= le32_to_cpu(sum
->ss_datasum
))
236 * nilfs_read_summary_info - read an item on summary blocks of a log
237 * @nilfs: nilfs object
238 * @pbh: the current buffer head on summary blocks [in, out]
239 * @offset: the current byte offset on summary blocks [in, out]
240 * @bytes: byte size of the item to be read
242 static void *nilfs_read_summary_info(struct the_nilfs
*nilfs
,
243 struct buffer_head
**pbh
,
244 unsigned int *offset
, unsigned int bytes
)
249 BUG_ON((*pbh
)->b_size
< *offset
);
250 if (bytes
> (*pbh
)->b_size
- *offset
) {
251 blocknr
= (*pbh
)->b_blocknr
;
253 *pbh
= __bread(nilfs
->ns_bdev
, blocknr
+ 1,
254 nilfs
->ns_blocksize
);
259 ptr
= (*pbh
)->b_data
+ *offset
;
265 * nilfs_skip_summary_info - skip items on summary blocks of a log
266 * @nilfs: nilfs object
267 * @pbh: the current buffer head on summary blocks [in, out]
268 * @offset: the current byte offset on summary blocks [in, out]
269 * @bytes: byte size of the item to be skipped
270 * @count: number of items to be skipped
272 static void nilfs_skip_summary_info(struct the_nilfs
*nilfs
,
273 struct buffer_head
**pbh
,
274 unsigned int *offset
, unsigned int bytes
,
277 unsigned int rest_item_in_current_block
278 = ((*pbh
)->b_size
- *offset
) / bytes
;
280 if (count
<= rest_item_in_current_block
) {
281 *offset
+= bytes
* count
;
283 sector_t blocknr
= (*pbh
)->b_blocknr
;
284 unsigned int nitem_per_block
= (*pbh
)->b_size
/ bytes
;
287 count
-= rest_item_in_current_block
;
288 bcnt
= DIV_ROUND_UP(count
, nitem_per_block
);
289 *offset
= bytes
* (count
- (bcnt
- 1) * nitem_per_block
);
292 *pbh
= __bread(nilfs
->ns_bdev
, blocknr
+ bcnt
,
293 nilfs
->ns_blocksize
);
298 * nilfs_scan_dsync_log - get block information of a log written for data sync
299 * @nilfs: nilfs object
300 * @start_blocknr: start block number of the log
301 * @sum: log summary information
302 * @head: list head to add nilfs_recovery_block struct
304 static int nilfs_scan_dsync_log(struct the_nilfs
*nilfs
, sector_t start_blocknr
,
305 struct nilfs_segment_summary
*sum
,
306 struct list_head
*head
)
308 struct buffer_head
*bh
;
310 u32 nfinfo
, sumbytes
;
315 nfinfo
= le32_to_cpu(sum
->ss_nfinfo
);
319 sumbytes
= le32_to_cpu(sum
->ss_sumbytes
);
320 blocknr
= start_blocknr
+ DIV_ROUND_UP(sumbytes
, nilfs
->ns_blocksize
);
321 bh
= __bread(nilfs
->ns_bdev
, start_blocknr
, nilfs
->ns_blocksize
);
325 offset
= le16_to_cpu(sum
->ss_bytes
);
327 unsigned long nblocks
, ndatablk
, nnodeblk
;
328 struct nilfs_finfo
*finfo
;
330 finfo
= nilfs_read_summary_info(nilfs
, &bh
, &offset
,
332 if (unlikely(!finfo
))
335 ino
= le64_to_cpu(finfo
->fi_ino
);
336 nblocks
= le32_to_cpu(finfo
->fi_nblocks
);
337 ndatablk
= le32_to_cpu(finfo
->fi_ndatablk
);
338 nnodeblk
= nblocks
- ndatablk
;
340 while (ndatablk
-- > 0) {
341 struct nilfs_recovery_block
*rb
;
342 struct nilfs_binfo_v
*binfo
;
344 binfo
= nilfs_read_summary_info(nilfs
, &bh
, &offset
,
346 if (unlikely(!binfo
))
349 rb
= kmalloc(sizeof(*rb
), GFP_NOFS
);
355 rb
->blocknr
= blocknr
++;
356 rb
->vblocknr
= le64_to_cpu(binfo
->bi_vblocknr
);
357 rb
->blkoff
= le64_to_cpu(binfo
->bi_blkoff
);
358 /* INIT_LIST_HEAD(&rb->list); */
359 list_add_tail(&rb
->list
, head
);
363 blocknr
+= nnodeblk
; /* always 0 for data sync logs */
364 nilfs_skip_summary_info(nilfs
, &bh
, &offset
, sizeof(__le64
),
371 brelse(bh
); /* brelse(NULL) is just ignored */
375 static void dispose_recovery_list(struct list_head
*head
)
377 while (!list_empty(head
)) {
378 struct nilfs_recovery_block
*rb
;
380 rb
= list_first_entry(head
, struct nilfs_recovery_block
, list
);
386 struct nilfs_segment_entry
{
387 struct list_head list
;
391 static int nilfs_segment_list_add(struct list_head
*head
, __u64 segnum
)
393 struct nilfs_segment_entry
*ent
= kmalloc(sizeof(*ent
), GFP_NOFS
);
398 ent
->segnum
= segnum
;
399 INIT_LIST_HEAD(&ent
->list
);
400 list_add_tail(&ent
->list
, head
);
404 void nilfs_dispose_segment_list(struct list_head
*head
)
406 while (!list_empty(head
)) {
407 struct nilfs_segment_entry
*ent
;
409 ent
= list_first_entry(head
, struct nilfs_segment_entry
, list
);
410 list_del(&ent
->list
);
415 static int nilfs_prepare_segment_for_recovery(struct the_nilfs
*nilfs
,
416 struct super_block
*sb
,
417 struct nilfs_recovery_info
*ri
)
419 struct list_head
*head
= &ri
->ri_used_segments
;
420 struct nilfs_segment_entry
*ent
, *n
;
421 struct inode
*sufile
= nilfs
->ns_sufile
;
426 segnum
[0] = nilfs
->ns_segnum
;
427 segnum
[1] = nilfs
->ns_nextnum
;
428 segnum
[2] = ri
->ri_segnum
;
429 segnum
[3] = ri
->ri_nextnum
;
432 * Releasing the next segment of the latest super root.
433 * The next segment is invalidated by this recovery.
435 err
= nilfs_sufile_free(sufile
, segnum
[1]);
437 if (err
== -ENOENT
) {
439 "checkpoint log inconsistency at block %llu (segment %llu): next segment %llu is unallocated",
440 (unsigned long long)nilfs
->ns_last_pseg
,
441 (unsigned long long)nilfs
->ns_segnum
,
442 (unsigned long long)segnum
[1]);
448 for (i
= 1; i
< 4; i
++) {
449 err
= nilfs_segment_list_add(head
, segnum
[i
]);
455 * Collecting segments written after the latest super root.
456 * These are marked dirty to avoid being reallocated in the next write.
458 list_for_each_entry_safe(ent
, n
, head
, list
) {
459 if (ent
->segnum
!= segnum
[0]) {
460 err
= nilfs_sufile_scrap(sufile
, ent
->segnum
);
464 list_del(&ent
->list
);
468 /* Allocate new segments for recovery */
469 err
= nilfs_sufile_alloc(sufile
, &segnum
[0]);
473 nilfs
->ns_pseg_offset
= 0;
474 nilfs
->ns_seg_seq
= ri
->ri_seq
+ 2;
475 nilfs
->ns_nextnum
= nilfs
->ns_segnum
= segnum
[0];
478 /* No need to recover sufile because it will be destroyed on error */
482 static int nilfs_recovery_copy_block(struct the_nilfs
*nilfs
,
483 struct nilfs_recovery_block
*rb
,
484 loff_t pos
, struct folio
*folio
)
486 struct buffer_head
*bh_org
;
487 size_t from
= offset_in_folio(folio
, pos
);
489 bh_org
= __bread(nilfs
->ns_bdev
, rb
->blocknr
, nilfs
->ns_blocksize
);
490 if (unlikely(!bh_org
))
493 memcpy_to_folio(folio
, from
, bh_org
->b_data
, bh_org
->b_size
);
498 static int nilfs_recover_dsync_blocks(struct the_nilfs
*nilfs
,
499 struct super_block
*sb
,
500 struct nilfs_root
*root
,
501 struct list_head
*head
,
502 unsigned long *nr_salvaged_blocks
)
505 struct nilfs_recovery_block
*rb
, *n
;
506 unsigned int blocksize
= nilfs
->ns_blocksize
;
509 int err
= 0, err2
= 0;
511 list_for_each_entry_safe(rb
, n
, head
, list
) {
512 inode
= nilfs_iget(sb
, root
, rb
->ino
);
514 err
= PTR_ERR(inode
);
519 pos
= rb
->blkoff
<< inode
->i_blkbits
;
520 err
= block_write_begin(inode
->i_mapping
, pos
, blocksize
,
521 &folio
, nilfs_get_block
);
523 loff_t isize
= inode
->i_size
;
525 if (pos
+ blocksize
> isize
)
526 nilfs_write_failed(inode
->i_mapping
,
531 err
= nilfs_recovery_copy_block(nilfs
, rb
, pos
, folio
);
535 err
= nilfs_set_file_dirty(inode
, 1);
539 block_write_end(NULL
, inode
->i_mapping
, pos
, blocksize
,
540 blocksize
, folio
, NULL
);
545 (*nr_salvaged_blocks
)++;
554 "error %d recovering data block (ino=%lu, block-offset=%llu)",
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 * @nilfs: nilfs object
571 * @sb: super block instance
572 * @root: NILFS root instance
573 * @ri: pointer to a nilfs_recovery_info
575 static int nilfs_do_roll_forward(struct the_nilfs
*nilfs
,
576 struct super_block
*sb
,
577 struct nilfs_root
*root
,
578 struct nilfs_recovery_info
*ri
)
580 struct buffer_head
*bh_sum
= NULL
;
581 struct nilfs_segment_summary
*sum
= NULL
;
583 sector_t seg_start
, seg_end
; /* Starting/ending DBN of full segment */
584 unsigned long nsalvaged_blocks
= 0;
587 __u64 segnum
, nextnum
= 0;
590 LIST_HEAD(dsync_blocks
); /* list of data blocks to be recovered */
593 RF_DSYNC_ST
, /* scanning data-sync segments */
595 int state
= RF_INIT_ST
;
597 pseg_start
= ri
->ri_lsegs_start
;
598 seg_seq
= ri
->ri_lsegs_start_seq
;
599 segnum
= nilfs_get_segnum_of_block(nilfs
, pseg_start
);
600 nilfs_get_segment_range(nilfs
, segnum
, &seg_start
, &seg_end
);
602 while (segnum
!= ri
->ri_segnum
|| pseg_start
<= ri
->ri_pseg_start
) {
604 bh_sum
= nilfs_read_log_header(nilfs
, pseg_start
, &sum
);
610 ret
= nilfs_validate_log(nilfs
, seg_seq
, bh_sum
, sum
);
612 if (ret
== NILFS_SEG_FAIL_IO
) {
619 flags
= le16_to_cpu(sum
->ss_flags
);
620 if (flags
& NILFS_SS_SR
)
623 /* Found a valid partial segment; do recovery actions */
624 nextnum
= nilfs_get_segnum_of_block(nilfs
,
625 le64_to_cpu(sum
->ss_next
));
627 nilfs
->ns_ctime
= le64_to_cpu(sum
->ss_create
);
628 if (!(flags
& NILFS_SS_GC
))
629 nilfs
->ns_nongc_ctime
= nilfs
->ns_ctime
;
633 if (!(flags
& NILFS_SS_LOGBGN
) ||
634 !(flags
& NILFS_SS_SYNDT
))
639 if (!(flags
& NILFS_SS_SYNDT
))
642 err
= nilfs_scan_dsync_log(nilfs
, pseg_start
, sum
,
646 if (flags
& NILFS_SS_LOGEND
) {
647 err
= nilfs_recover_dsync_blocks(
648 nilfs
, sb
, root
, &dsync_blocks
,
654 break; /* Fall through to try_next_pseg */
658 if (pseg_start
== ri
->ri_lsegs_end
)
660 pseg_start
+= le32_to_cpu(sum
->ss_nblocks
);
661 if (pseg_start
< seg_end
)
666 if (pseg_start
== ri
->ri_lsegs_end
)
670 /* Looking to the next full segment */
675 nilfs_get_segment_range(nilfs
, segnum
, &seg_start
, &seg_end
);
676 pseg_start
= seg_start
;
679 if (nsalvaged_blocks
) {
680 nilfs_info(sb
, "salvaged %lu blocks", nsalvaged_blocks
);
681 ri
->ri_need_recovery
= NILFS_RECOVERY_ROLLFORWARD_DONE
;
685 dispose_recovery_list(&dsync_blocks
);
692 "error %d roll-forwarding partial segment at blocknr = %llu",
693 err
, (unsigned long long)pseg_start
);
697 static void nilfs_finish_roll_forward(struct the_nilfs
*nilfs
,
698 struct nilfs_recovery_info
*ri
)
700 struct buffer_head
*bh
;
703 if (nilfs_get_segnum_of_block(nilfs
, ri
->ri_lsegs_start
) !=
704 nilfs_get_segnum_of_block(nilfs
, ri
->ri_super_root
))
707 bh
= __getblk(nilfs
->ns_bdev
, ri
->ri_lsegs_start
, nilfs
->ns_blocksize
);
709 return; /* should never happen */
712 memset(bh
->b_data
, 0, bh
->b_size
);
713 set_buffer_uptodate(bh
);
714 set_buffer_dirty(bh
);
717 err
= sync_dirty_buffer(bh
);
719 nilfs_warn(nilfs
->ns_sb
,
720 "buffer sync write failed during post-cleaning of recovery.");
725 * nilfs_abort_roll_forward - cleaning up after a failed rollforward recovery
726 * @nilfs: nilfs object
728 static void nilfs_abort_roll_forward(struct the_nilfs
*nilfs
)
730 struct nilfs_inode_info
*ii
, *n
;
733 /* Abandon inodes that have read recovery data */
734 spin_lock(&nilfs
->ns_inode_lock
);
735 list_splice_init(&nilfs
->ns_dirty_files
, &head
);
736 spin_unlock(&nilfs
->ns_inode_lock
);
737 if (list_empty(&head
))
740 set_nilfs_purging(nilfs
);
741 list_for_each_entry_safe(ii
, n
, &head
, i_dirty
) {
742 spin_lock(&nilfs
->ns_inode_lock
);
743 list_del_init(&ii
->i_dirty
);
744 spin_unlock(&nilfs
->ns_inode_lock
);
746 iput(&ii
->vfs_inode
);
748 clear_nilfs_purging(nilfs
);
752 * nilfs_salvage_orphan_logs - salvage logs written after the latest checkpoint
753 * @nilfs: nilfs object
754 * @sb: super block instance
755 * @ri: pointer to a nilfs_recovery_info struct to store search results.
757 * Return Value: On success, 0 is returned. On error, one of the following
758 * negative error code is returned.
760 * %-EINVAL - Inconsistent filesystem state.
764 * %-ENOSPC - No space left on device (only in a panic state).
766 * %-ERESTARTSYS - Interrupted.
768 * %-ENOMEM - Insufficient memory available.
770 int nilfs_salvage_orphan_logs(struct the_nilfs
*nilfs
,
771 struct super_block
*sb
,
772 struct nilfs_recovery_info
*ri
)
774 struct nilfs_root
*root
;
777 if (ri
->ri_lsegs_start
== 0 || ri
->ri_lsegs_end
== 0)
780 err
= nilfs_attach_checkpoint(sb
, ri
->ri_cno
, true, &root
);
782 nilfs_err(sb
, "error %d loading the latest checkpoint", err
);
786 err
= nilfs_do_roll_forward(nilfs
, sb
, root
, ri
);
790 if (ri
->ri_need_recovery
== NILFS_RECOVERY_ROLLFORWARD_DONE
) {
791 err
= nilfs_prepare_segment_for_recovery(nilfs
, sb
, ri
);
793 nilfs_err(sb
, "error %d preparing segment for recovery",
798 err
= nilfs_attach_log_writer(sb
, root
);
802 set_nilfs_discontinued(nilfs
);
803 err
= nilfs_construct_segment(sb
);
804 nilfs_detach_log_writer(sb
);
807 nilfs_err(sb
, "error %d writing segment for recovery",
812 nilfs_finish_roll_forward(nilfs
, ri
);
816 nilfs_put_root(root
);
820 nilfs_abort_roll_forward(nilfs
);
825 * nilfs_search_super_root - search the latest valid super root
827 * @ri: pointer to a nilfs_recovery_info struct to store search results.
829 * nilfs_search_super_root() looks for the latest super-root from a partial
830 * segment pointed by the superblock. It sets up struct the_nilfs through
831 * this search. It fills nilfs_recovery_info (ri) required for recovery.
833 * Return Value: On success, 0 is returned. On error, one of the following
834 * negative error code is returned.
836 * %-EINVAL - No valid segment found
840 * %-ENOMEM - Insufficient memory available.
842 int nilfs_search_super_root(struct the_nilfs
*nilfs
,
843 struct nilfs_recovery_info
*ri
)
845 struct buffer_head
*bh_sum
= NULL
;
846 struct nilfs_segment_summary
*sum
= NULL
;
847 sector_t pseg_start
, pseg_end
, sr_pseg_start
= 0;
848 sector_t seg_start
, seg_end
; /* range of full segment (block number) */
850 unsigned long nblocks
;
853 __u64 segnum
, nextnum
= 0;
856 int empty_seg
= 0, scan_newer
= 0;
859 pseg_start
= nilfs
->ns_last_pseg
;
860 seg_seq
= nilfs
->ns_last_seq
;
861 cno
= nilfs
->ns_last_cno
;
862 segnum
= nilfs_get_segnum_of_block(nilfs
, pseg_start
);
864 /* Calculate range of segment */
865 nilfs_get_segment_range(nilfs
, segnum
, &seg_start
, &seg_end
);
867 /* Read ahead segment */
870 __breadahead(nilfs
->ns_bdev
, b
++, nilfs
->ns_blocksize
);
874 ret
= NILFS_SEG_FAIL_IO
;
875 bh_sum
= nilfs_read_log_header(nilfs
, pseg_start
, &sum
);
879 ret
= nilfs_validate_log(nilfs
, seg_seq
, bh_sum
, sum
);
881 if (ret
== NILFS_SEG_FAIL_IO
)
886 nblocks
= le32_to_cpu(sum
->ss_nblocks
);
887 pseg_end
= pseg_start
+ nblocks
- 1;
888 if (unlikely(pseg_end
> seg_end
)) {
889 ret
= NILFS_SEG_FAIL_CONSISTENCY
;
893 /* A valid partial segment */
894 ri
->ri_pseg_start
= pseg_start
;
895 ri
->ri_seq
= seg_seq
;
896 ri
->ri_segnum
= segnum
;
897 nextnum
= nilfs_get_segnum_of_block(nilfs
,
898 le64_to_cpu(sum
->ss_next
));
899 ri
->ri_nextnum
= nextnum
;
902 flags
= le16_to_cpu(sum
->ss_flags
);
903 if (!(flags
& NILFS_SS_SR
) && !scan_newer
) {
905 * This will never happen because a superblock
906 * (last_segment) always points to a pseg with
909 ret
= NILFS_SEG_FAIL_CONSISTENCY
;
913 if (pseg_start
== seg_start
) {
914 nilfs_get_segment_range(nilfs
, nextnum
, &b
, &end
);
916 __breadahead(nilfs
->ns_bdev
, b
++,
917 nilfs
->ns_blocksize
);
919 if (!(flags
& NILFS_SS_SR
)) {
920 if (!ri
->ri_lsegs_start
&& (flags
& NILFS_SS_LOGBGN
)) {
921 ri
->ri_lsegs_start
= pseg_start
;
922 ri
->ri_lsegs_start_seq
= seg_seq
;
924 if (flags
& NILFS_SS_LOGEND
)
925 ri
->ri_lsegs_end
= pseg_start
;
929 /* A valid super root was found. */
931 ri
->ri_super_root
= pseg_end
;
932 ri
->ri_lsegs_start
= ri
->ri_lsegs_end
= 0;
934 nilfs_dispose_segment_list(&segments
);
935 sr_pseg_start
= pseg_start
;
936 nilfs
->ns_pseg_offset
= pseg_start
+ nblocks
- seg_start
;
937 nilfs
->ns_seg_seq
= seg_seq
;
938 nilfs
->ns_segnum
= segnum
;
939 nilfs
->ns_cno
= cno
; /* nilfs->ns_cno = ri->ri_cno + 1 */
940 nilfs
->ns_ctime
= le64_to_cpu(sum
->ss_create
);
941 nilfs
->ns_nextnum
= nextnum
;
944 ri
->ri_need_recovery
= NILFS_RECOVERY_SR_UPDATED
;
946 if (nilfs
->ns_mount_state
& NILFS_VALID_FS
)
947 goto super_root_found
;
952 /* Standing on a course, or met an inconsistent state */
953 pseg_start
+= nblocks
;
954 if (pseg_start
< seg_end
)
962 * This can happen if a checkpoint was written without
963 * barriers, or as a result of an I/O failure.
968 /* Looking to the next full segment */
970 goto super_root_found
; /* found a valid super root */
972 ret
= nilfs_segment_list_add(&segments
, segnum
);
978 nilfs_get_segment_range(nilfs
, segnum
, &seg_start
, &seg_end
);
979 pseg_start
= seg_start
;
983 /* Updating pointers relating to the latest checkpoint */
985 list_splice_tail(&segments
, &ri
->ri_used_segments
);
986 nilfs
->ns_last_pseg
= sr_pseg_start
;
987 nilfs
->ns_last_seq
= nilfs
->ns_seg_seq
;
988 nilfs
->ns_last_cno
= ri
->ri_cno
;
993 nilfs_dispose_segment_list(&segments
);
994 return ret
< 0 ? ret
: nilfs_warn_segment_error(nilfs
->ns_sb
, ret
);