--- /dev/null
+From 7cb9994754f8a36ae9e5ec4597c5c4c2d6c03832 Mon Sep 17 00:00:00 2001
+From: Hu Weiwen <sehuww@mail.scut.edu.cn>
+Date: Fri, 1 Jul 2022 10:52:27 +0800
+Subject: ceph: don't truncate file in atomic_open
+
+From: Hu Weiwen <sehuww@mail.scut.edu.cn>
+
+commit 7cb9994754f8a36ae9e5ec4597c5c4c2d6c03832 upstream.
+
+Clear O_TRUNC from the flags sent in the MDS create request.
+
+`atomic_open' is called before permission check. We should not do any
+modification to the file here. The caller will do the truncation
+afterward.
+
+Fixes: 124e68e74099 ("ceph: file operations")
+Signed-off-by: Hu Weiwen <sehuww@mail.scut.edu.cn>
+Reviewed-by: Xiubo Li <xiubli@redhat.com>
+Signed-off-by: Ilya Dryomov <idryomov@gmail.com>
+[Xiubo: fixed a trivial conflict for 5.10 backport]
+Signed-off-by: Xiubo Li <xiubli@redhat.com>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+---
+ fs/ceph/file.c | 10 +++++++---
+ 1 file changed, 7 insertions(+), 3 deletions(-)
+
+--- a/fs/ceph/file.c
++++ b/fs/ceph/file.c
+@@ -703,6 +703,12 @@ int ceph_atomic_open(struct inode *dir,
+ if (dentry->d_name.len > NAME_MAX)
+ return -ENAMETOOLONG;
+
++ /*
++ * Do not truncate the file, since atomic_open is called before the
++ * permission check. The caller will do the truncation afterward.
++ */
++ flags &= ~O_TRUNC;
++
+ if (flags & O_CREAT) {
+ if (ceph_quota_is_max_files_exceeded(dir))
+ return -EDQUOT;
+@@ -769,9 +775,7 @@ retry:
+ }
+
+ set_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
+- err = ceph_mdsc_do_request(mdsc,
+- (flags & (O_CREAT|O_TRUNC)) ? dir : NULL,
+- req);
++ err = ceph_mdsc_do_request(mdsc, (flags & O_CREAT) ? dir : NULL, req);
+ err = ceph_handle_snapdir(req, dentry, err);
+ if (err)
+ goto out_req;
--- /dev/null
+From e3b49ea36802053f312013fd4ccb6e59920a9f76 Mon Sep 17 00:00:00 2001
+From: Hyeong-Jun Kim <hj514.kim@samsung.com>
+Date: Tue, 2 Nov 2021 16:10:02 +0900
+Subject: f2fs: invalidate META_MAPPING before IPU/DIO write
+
+From: Hyeong-Jun Kim <hj514.kim@samsung.com>
+
+commit e3b49ea36802053f312013fd4ccb6e59920a9f76 upstream.
+
+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.kim@samsung.com>
+Reviewed-by: Chao Yu <chao@kernel.org>
+Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>a
+Signed-off-by: Chao Yu <chao@kernel.org>
+Signed-off-by: lvgaofei <lvgaofei@oppo.com>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+---
+ fs/f2fs/data.c | 2 ++
+ fs/f2fs/segment.c | 3 +++
+ 2 files changed, 5 insertions(+)
+
+--- a/fs/f2fs/data.c
++++ b/fs/f2fs/data.c
+@@ -1716,6 +1716,8 @@ sync_out:
+ 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) {
+--- a/fs/f2fs/segment.c
++++ b/fs/f2fs/segment.c
+@@ -3547,6 +3547,9 @@ int f2fs_inplace_write_data(struct f2fs_
+ 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)))