@@ -3839,25 +3839,34 @@ static int f2fs_migrate_blocks(struct inode *inode, block_t start_blk,
38393839 unsigned int blkofs ;
38403840 unsigned int blk_per_sec = BLKS_PER_SEC (sbi );
38413841 unsigned int secidx = start_blk / blk_per_sec ;
3842- unsigned int end_sec = secidx + blkcnt / blk_per_sec ;
3842+ unsigned int end_sec ;
38433843 int ret = 0 ;
38443844
3845+ if (!blkcnt )
3846+ return 0 ;
3847+ end_sec = secidx + (blkcnt - 1 ) / blk_per_sec ;
3848+
38453849 f2fs_down_write (& F2FS_I (inode )-> i_gc_rwsem [WRITE ]);
38463850 filemap_invalidate_lock (inode -> i_mapping );
38473851
38483852 set_inode_flag (inode , FI_ALIGNED_WRITE );
38493853 set_inode_flag (inode , FI_OPU_WRITE );
38503854
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+
38523859 f2fs_down_write (& sbi -> pin_sem );
38533860
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+ }
38573866
38583867 set_inode_flag (inode , FI_SKIP_WRITES );
38593868
3860- for (blkofs = 0 ; blkofs < blk_per_sec ; blkofs ++ ) {
3869+ for (blkofs = 0 ; blkofs <= blkofs_end ; blkofs ++ ) {
38613870 struct page * page ;
38623871 unsigned int blkidx = secidx * blk_per_sec + blkofs ;
38633872
@@ -3946,27 +3955,34 @@ static int check_swap_activate(struct swap_info_struct *sis,
39463955 nr_pblocks = map .m_len ;
39473956
39483957 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+
39503962 not_aligned ++ ;
39513963
39523964 nr_pblocks = roundup (nr_pblocks , blks_per_sec );
39533965 if (cur_lblock + nr_pblocks > sis -> max )
39543966 nr_pblocks -= blks_per_sec ;
39553967
3968+ /* this extent is last one */
39563969 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;
39613972 }
39623973
39633974 ret = f2fs_migrate_blocks (inode , cur_lblock ,
39643975 nr_pblocks );
3965- if (ret )
3976+ if (ret ) {
3977+ if (ret == - ENOENT )
3978+ ret = - EINVAL ;
39663979 goto out ;
3967- goto retry ;
3980+ }
3981+
3982+ if (!last_extent )
3983+ goto retry ;
39683984 }
3969- next :
3985+
39703986 if (cur_lblock + nr_pblocks >= sis -> max )
39713987 nr_pblocks = sis -> max - cur_lblock ;
39723988
@@ -4004,17 +4020,17 @@ static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
40044020 sector_t * span )
40054021{
40064022 struct inode * inode = file_inode (file );
4023+ struct f2fs_sb_info * sbi = F2FS_I_SB (inode );
40074024 int ret ;
40084025
40094026 if (!S_ISREG (inode -> i_mode ))
40104027 return - EINVAL ;
40114028
4012- if (f2fs_readonly (F2FS_I_SB ( inode ) -> sb ))
4029+ if (f2fs_readonly (sbi -> sb ))
40134030 return - EROFS ;
40144031
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" );
40184034 return - EINVAL ;
40194035 }
40204036
@@ -4027,13 +4043,17 @@ static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file,
40274043
40284044 f2fs_precache_extents (inode );
40294045
4046+ ret = filemap_fdatawrite (inode -> i_mapping );
4047+ if (ret < 0 )
4048+ return ret ;
4049+
40304050 ret = check_swap_activate (sis , file , span );
40314051 if (ret < 0 )
40324052 return ret ;
40334053
40344054 stat_inc_swapfile_inode (inode );
40354055 set_inode_flag (inode , FI_PIN_FILE );
4036- f2fs_update_time (F2FS_I_SB ( inode ) , REQ_TIME );
4056+ f2fs_update_time (sbi , REQ_TIME );
40374057 return ret ;
40384058}
40394059
0 commit comments