BACKPORT: f2fs: invalidate META_MAPPING before IPU/DIO write

Encrypted pages during GC are read and cached in META_MAPPING.
However, due to cached pages in META_MAPPING, there is an issue where
newly written pages are lost by IPU or DIO writes.

Thread A - f2fs_gc()            Thread B
/* phase 3 */
down_write(i_gc_rwsem)
ra_data_block()       ---- (a)
up_write(i_gc_rwsem)
                                f2fs_direct_IO() :
                                 - down_read(i_gc_rwsem)
                                 - __blockdev_direct_io()
                                 - get_data_block_dio_write()
                                 - f2fs_dio_submit_bio()  ---- (b)
                                 - up_read(i_gc_rwsem)
/* phase 4 */
down_write(i_gc_rwsem)
move_data_block()     ---- (c)
up_write(i_gc_rwsem)

(a) In phase 3 of f2fs_gc(), up-to-date page is read from storage and
    cached in META_MAPPING.
(b) In thread B, writing new data by IPU or DIO write on same blkaddr as
    read in (a). cached page in META_MAPPING become out-dated.
(c) In phase 4 of f2fs_gc(), out-dated page in META_MAPPING is copied to
    new blkaddr. In conclusion, the newly written data in (b) is lost.

To address this issue, invalidating pages in META_MAPPING before IPU or
DIO write.

Fixes: 6aa58d8ad20a ("f2fs: readahead encrypted block during GC")
Signed-off-by: Hyeong-Jun Kim <hj514....@samsung.com>
Reviewed-by: Chao Yu <c...@kernel.org>
Signed-off-by: Jaegeuk Kim <jaeg...@kernel.org>
Signed-off-by: Chao Yu <c...@kernel.org>
Signed-off-by: lvgaofei <lvgao...@oppo.com>
(cherry picked from commit e3b49ea36802053f312013fd4ccb6e59920a9f76)
---
 fs/f2fs/data.c    | 5 ++++-
 fs/f2fs/segment.c | 3 +++
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index b2016fd..994a09e 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -1713,9 +1713,12 @@ int f2fs_map_blocks(struct inode *inode, struct 
f2fs_map_blocks *map,
 sync_out:

        /* for hardware encryption, but to avoid potential issue in future */
-       if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED)
+       if (flag == F2FS_GET_BLOCK_DIO && map->m_flags & F2FS_MAP_MAPPED) {
                f2fs_wait_on_block_writeback_range(inode,
                                                map->m_pblk, map->m_len);
+               invalidate_mapping_pages(META_MAPPING(sbi),
+                                               map->m_pblk, map->m_pblk);
+       }

        if (flag == F2FS_GET_BLOCK_PRECACHE) {
                if (map->m_flags & F2FS_MAP_MAPPED) {
diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c
index 19224e7..8549332 100644
--- a/fs/f2fs/segment.c
+++ b/fs/f2fs/segment.c
@@ -3547,6 +3547,9 @@ int f2fs_inplace_write_data(struct f2fs_io_info *fio)
                return -EFSCORRUPTED;
        }

+       invalidate_mapping_pages(META_MAPPING(sbi),
+                               fio->new_blkaddr, fio->new_blkaddr);
+
        stat_inc_inplace_blocks(fio->sbi);

        if (fio->bio && !(SM_I(sbi)->ipu_policy & (1 << F2FS_IPU_NOCACHE)))
--
2.7.4

________________________________
OPPO

本电子邮件及其附件含有OPPO公司的保密信息,仅限于邮件指明的收件人使用(包含个人及群组)。禁止任何人在未经授权的情况下以任何形式使用。如果您错收了本邮件,请立即以电子邮件通知发件人并删除本邮件及其附件。

This e-mail and its attachments contain confidential information from OPPO, 
which is intended only for the person or entity whose address is listed above. 
Any use of the information contained herein in any way (including, but not 
limited to, total or partial disclosure, reproduction, or dissemination) by 
persons other than the intended recipient(s) is prohibited. If you receive this 
e-mail in error, please notify the sender by phone or email immediately and 
delete it!


_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

Reply via email to