]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
f2fs: fix to align to section for fallocate() on pinned file
authorChao Yu <yuchao0@huawei.com>
Fri, 5 Mar 2021 09:56:01 +0000 (17:56 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 19 May 2021 08:56:22 +0000 (10:56 +0200)
[ Upstream commit e1175f02291141bbd924fc578299305fcde35855 ]

Now, fallocate() on a pinned file only allocates blocks which aligns
to segment rather than section, so GC may try to migrate pinned file's
block, and after several times of failure, pinned file's block could
be migrated to other place, however user won't be aware of such
condition, and then old obsolete block address may be readed/written
incorrectly.

To avoid such condition, let's try to allocate pinned file's blocks
with section alignment.

Signed-off-by: Chao Yu <yuchao0@huawei.com>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
fs/f2fs/f2fs.h
fs/f2fs/file.c
fs/f2fs/segment.c

index cccdfb1a40ab129015d5b516b00ea60befec25e7..45a83f8c9e87a038ffab0b6e7d325ac7e267d3e9 100644 (file)
@@ -3383,7 +3383,7 @@ void f2fs_get_new_segment(struct f2fs_sb_info *sbi,
                        unsigned int *newseg, bool new_sec, int dir);
 void f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
                                        unsigned int start, unsigned int end);
-void f2fs_allocate_new_segment(struct f2fs_sb_info *sbi, int type);
+void f2fs_allocate_new_section(struct f2fs_sb_info *sbi, int type);
 void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi);
 int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
 bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
index 1863944f4073801bc108d2d3d6f98f8cb4979d93..bd5a77091d2375cefafa7602404092703dca908b 100644 (file)
@@ -1646,27 +1646,26 @@ static int expand_inode_data(struct inode *inode, loff_t offset,
                return 0;
 
        if (f2fs_is_pinned_file(inode)) {
-               block_t len = (map.m_len >> sbi->log_blocks_per_seg) <<
-                                       sbi->log_blocks_per_seg;
+               block_t sec_blks = BLKS_PER_SEC(sbi);
+               block_t sec_len = roundup(map.m_len, sec_blks);
                block_t done = 0;
 
-               if (map.m_len % sbi->blocks_per_seg)
-                       len += sbi->blocks_per_seg;
-
-               map.m_len = sbi->blocks_per_seg;
+               map.m_len = sec_blks;
 next_alloc:
                if (has_not_enough_free_secs(sbi, 0,
                        GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi)))) {
                        down_write(&sbi->gc_lock);
                        err = f2fs_gc(sbi, true, false, false, NULL_SEGNO);
-                       if (err && err != -ENODATA && err != -EAGAIN)
+                       if (err && err != -ENODATA && err != -EAGAIN) {
+                               map.m_len = done;
                                goto out_err;
+                       }
                }
 
                down_write(&sbi->pin_sem);
 
                f2fs_lock_op(sbi);
-               f2fs_allocate_new_segment(sbi, CURSEG_COLD_DATA_PINNED);
+               f2fs_allocate_new_section(sbi, CURSEG_COLD_DATA_PINNED);
                f2fs_unlock_op(sbi);
 
                map.m_seg_type = CURSEG_COLD_DATA_PINNED;
@@ -1675,9 +1674,9 @@ next_alloc:
                up_write(&sbi->pin_sem);
 
                done += map.m_len;
-               len -= map.m_len;
+               sec_len -= map.m_len;
                map.m_lblk += map.m_len;
-               if (!err && len)
+               if (!err && sec_len)
                        goto next_alloc;
 
                map.m_len = done;
index dcba4ac3dd2bc1d4c311174e8a75a70fbb75f8cf..91708460f584a3889e51c79e31a7c3168ba172cd 100644 (file)
@@ -2893,7 +2893,8 @@ unlock:
        up_read(&SM_I(sbi)->curseg_lock);
 }
 
-static void __allocate_new_segment(struct f2fs_sb_info *sbi, int type)
+static void __allocate_new_segment(struct f2fs_sb_info *sbi, int type,
+                                                               bool new_sec)
 {
        struct curseg_info *curseg = CURSEG_I(sbi, type);
        unsigned int old_segno;
@@ -2901,10 +2902,22 @@ static void __allocate_new_segment(struct f2fs_sb_info *sbi, int type)
        if (!curseg->inited)
                goto alloc;
 
-       if (!curseg->next_blkoff &&
-               !get_valid_blocks(sbi, curseg->segno, false) &&
-               !get_ckpt_valid_blocks(sbi, curseg->segno))
-               return;
+       if (curseg->next_blkoff ||
+               get_valid_blocks(sbi, curseg->segno, new_sec))
+               goto alloc;
+
+       if (new_sec) {
+               unsigned int segno = START_SEGNO(curseg->segno);
+               int i;
+
+               for (i = 0; i < sbi->segs_per_sec; i++, segno++) {
+                       if (get_ckpt_valid_blocks(sbi, segno))
+                               goto alloc;
+               }
+       } else {
+               if (!get_ckpt_valid_blocks(sbi, curseg->segno))
+                       return;
+       }
 
 alloc:
        old_segno = curseg->segno;
@@ -2912,10 +2925,15 @@ alloc:
        locate_dirty_segment(sbi, old_segno);
 }
 
-void f2fs_allocate_new_segment(struct f2fs_sb_info *sbi, int type)
+static void __allocate_new_section(struct f2fs_sb_info *sbi, int type)
+{
+       __allocate_new_segment(sbi, type, true);
+}
+
+void f2fs_allocate_new_section(struct f2fs_sb_info *sbi, int type)
 {
        down_write(&SIT_I(sbi)->sentry_lock);
-       __allocate_new_segment(sbi, type);
+       __allocate_new_section(sbi, type);
        up_write(&SIT_I(sbi)->sentry_lock);
 }
 
@@ -2925,7 +2943,7 @@ void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi)
 
        down_write(&SIT_I(sbi)->sentry_lock);
        for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++)
-               __allocate_new_segment(sbi, i);
+               __allocate_new_segment(sbi, i, false);
        up_write(&SIT_I(sbi)->sentry_lock);
 }