@@ -3839,25 +3839,34 @@ static int f2fs_migrate_blocks(struct inode *inode, block_t start_blk,
3839
3839
unsigned int blkofs ;
3840
3840
unsigned int blk_per_sec = BLKS_PER_SEC (sbi );
3841
3841
unsigned int secidx = start_blk / blk_per_sec ;
3842
- unsigned int end_sec = secidx + blkcnt / blk_per_sec ;
3842
+ unsigned int end_sec ;
3843
3843
int ret = 0 ;
3844
3844
3845
+ if (!blkcnt )
3846
+ return 0 ;
3847
+ end_sec = secidx + (blkcnt - 1 ) / blk_per_sec ;
3848
+
3845
3849
f2fs_down_write (& F2FS_I (inode )-> i_gc_rwsem [WRITE ]);
3846
3850
filemap_invalidate_lock (inode -> i_mapping );
3847
3851
3848
3852
set_inode_flag (inode , FI_ALIGNED_WRITE );
3849
3853
set_inode_flag (inode , FI_OPU_WRITE );
3850
3854
3851
- for (; secidx < end_sec ; secidx ++ ) {
3855
+ for (; secidx <= end_sec ; secidx ++ ) {
3856
+ unsigned int blkofs_end = secidx == end_sec ?
3857
+ (blkcnt - 1 ) % blk_per_sec : blk_per_sec - 1 ;
3858
+
3852
3859
f2fs_down_write (& sbi -> pin_sem );
3853
3860
3854
- f2fs_lock_op (sbi );
3855
- f2fs_allocate_new_section (sbi , CURSEG_COLD_DATA_PINNED , false);
3856
- f2fs_unlock_op (sbi );
3861
+ ret = f2fs_allocate_pinning_section (sbi );
3862
+ if (ret ) {
3863
+ f2fs_up_write (& sbi -> pin_sem );
3864
+ break ;
3865
+ }
3857
3866
3858
3867
set_inode_flag (inode , FI_SKIP_WRITES );
3859
3868
3860
- for (blkofs = 0 ; blkofs < blk_per_sec ; blkofs ++ ) {
3869
+ for (blkofs = 0 ; blkofs <= blkofs_end ; blkofs ++ ) {
3861
3870
struct page * page ;
3862
3871
unsigned int blkidx = secidx * blk_per_sec + blkofs ;
3863
3872
@@ -3946,27 +3955,34 @@ static int check_swap_activate(struct swap_info_struct *sis,
3946
3955
nr_pblocks = map .m_len ;
3947
3956
3948
3957
if ((pblock - SM_I (sbi )-> main_blkaddr ) & sec_blks_mask ||
3949
- nr_pblocks & sec_blks_mask ) {
3958
+ nr_pblocks & sec_blks_mask ||
3959
+ !f2fs_valid_pinned_area (sbi , pblock )) {
3960
+ bool last_extent = false;
3961
+
3950
3962
not_aligned ++ ;
3951
3963
3952
3964
nr_pblocks = roundup (nr_pblocks , blks_per_sec );
3953
3965
if (cur_lblock + nr_pblocks > sis -> max )
3954
3966
nr_pblocks -= blks_per_sec ;
3955
3967
3968
+ /* this extent is last one */
3956
3969
if (!nr_pblocks ) {
3957
- /* this extent is last one */
3958
- nr_pblocks = map .m_len ;
3959
- f2fs_warn (sbi , "Swapfile: last extent is not aligned to section" );
3960
- goto next ;
3970
+ nr_pblocks = last_lblock - cur_lblock ;
3971
+ last_extent = true;
3961
3972
}
3962
3973
3963
3974
ret = f2fs_migrate_blocks (inode , cur_lblock ,
3964
3975
nr_pblocks );
3965
- if (ret )
3976
+ if (ret ) {
3977
+ if (ret == - ENOENT )
3978
+ ret = - EINVAL ;
3966
3979
goto out ;
3967
- goto retry ;
3980
+ }
3981
+
3982
+ if (!last_extent )
3983
+ goto retry ;
3968
3984
}
3969
- next :
3985
+
3970
3986
if (cur_lblock + nr_pblocks >= sis -> max )
3971
3987
nr_pblocks = sis -> max - cur_lblock ;
3972
3988
@@ -4004,17 +4020,17 @@ static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
4004
4020
sector_t * span )
4005
4021
{
4006
4022
struct inode * inode = file_inode (file );
4023
+ struct f2fs_sb_info * sbi = F2FS_I_SB (inode );
4007
4024
int ret ;
4008
4025
4009
4026
if (!S_ISREG (inode -> i_mode ))
4010
4027
return - EINVAL ;
4011
4028
4012
- if (f2fs_readonly (F2FS_I_SB ( inode ) -> sb ))
4029
+ if (f2fs_readonly (sbi -> sb ))
4013
4030
return - EROFS ;
4014
4031
4015
- if (f2fs_lfs_mode (F2FS_I_SB (inode ))) {
4016
- f2fs_err (F2FS_I_SB (inode ),
4017
- "Swapfile not supported in LFS mode" );
4032
+ if (f2fs_lfs_mode (sbi ) && !f2fs_sb_has_blkzoned (sbi )) {
4033
+ f2fs_err (sbi , "Swapfile not supported in LFS mode" );
4018
4034
return - EINVAL ;
4019
4035
}
4020
4036
@@ -4027,13 +4043,17 @@ static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
4027
4043
4028
4044
f2fs_precache_extents (inode );
4029
4045
4046
+ ret = filemap_fdatawrite (inode -> i_mapping );
4047
+ if (ret < 0 )
4048
+ return ret ;
4049
+
4030
4050
ret = check_swap_activate (sis , file , span );
4031
4051
if (ret < 0 )
4032
4052
return ret ;
4033
4053
4034
4054
stat_inc_swapfile_inode (inode );
4035
4055
set_inode_flag (inode , FI_PIN_FILE );
4036
- f2fs_update_time (F2FS_I_SB ( inode ) , REQ_TIME );
4056
+ f2fs_update_time (sbi , REQ_TIME );
4037
4057
return ret ;
4038
4058
}
4039
4059
0 commit comments