Commit 23ad83c3 authored by Jaegeuk Kim's avatar Jaegeuk Kim Committed by Greg Kroah-Hartman

f2fs: avoid wrong decrypted data from disk

[ Upstream commit 0ded69f6 ]

1. Create a file in an encrypted directory
2. Do GC & drop caches
3. Read stale data before its bio for metapage was not issued yet
Reviewed-by: default avatarChao Yu <yuchao0@huawei.com>
Signed-off-by: default avatarJaegeuk Kim <jaegeuk@kernel.org>
Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
parent b6410b95
...@@ -575,9 +575,6 @@ static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr, ...@@ -575,9 +575,6 @@ static struct bio *f2fs_grab_read_bio(struct inode *inode, block_t blkaddr,
ctx->bio = bio; ctx->bio = bio;
ctx->enabled_steps = post_read_steps; ctx->enabled_steps = post_read_steps;
bio->bi_private = ctx; bio->bi_private = ctx;
/* wait the page to be moved by cleaning */
f2fs_wait_on_block_writeback(sbi, blkaddr);
} }
return bio; return bio;
...@@ -592,6 +589,9 @@ static int f2fs_submit_page_read(struct inode *inode, struct page *page, ...@@ -592,6 +589,9 @@ static int f2fs_submit_page_read(struct inode *inode, struct page *page,
if (IS_ERR(bio)) if (IS_ERR(bio))
return PTR_ERR(bio); return PTR_ERR(bio);
/* wait for GCed page writeback via META_MAPPING */
f2fs_wait_on_block_writeback(inode, blkaddr);
if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) { if (bio_add_page(bio, page, PAGE_SIZE, 0) < PAGE_SIZE) {
bio_put(bio); bio_put(bio);
return -EFAULT; return -EFAULT;
...@@ -1569,6 +1569,12 @@ static int f2fs_mpage_readpages(struct address_space *mapping, ...@@ -1569,6 +1569,12 @@ static int f2fs_mpage_readpages(struct address_space *mapping,
} }
} }
/*
* If the page is under writeback, we need to wait for
* its completion to see the correct decrypted data.
*/
f2fs_wait_on_block_writeback(inode, block_nr);
if (bio_add_page(bio, page, blocksize, 0) < blocksize) if (bio_add_page(bio, page, blocksize, 0) < blocksize)
goto submit_and_realloc; goto submit_and_realloc;
...@@ -1637,7 +1643,7 @@ static int encrypt_one_page(struct f2fs_io_info *fio) ...@@ -1637,7 +1643,7 @@ static int encrypt_one_page(struct f2fs_io_info *fio)
return 0; return 0;
/* wait for GCed page writeback via META_MAPPING */ /* wait for GCed page writeback via META_MAPPING */
f2fs_wait_on_block_writeback(fio->sbi, fio->old_blkaddr); f2fs_wait_on_block_writeback(inode, fio->old_blkaddr);
retry_encrypt: retry_encrypt:
fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page, fio->encrypted_page = fscrypt_encrypt_page(inode, fio->page,
...@@ -2402,10 +2408,6 @@ static int f2fs_write_begin(struct file *file, struct address_space *mapping, ...@@ -2402,10 +2408,6 @@ static int f2fs_write_begin(struct file *file, struct address_space *mapping,
f2fs_wait_on_page_writeback(page, DATA, false); f2fs_wait_on_page_writeback(page, DATA, false);
/* wait for GCed page writeback via META_MAPPING */
if (f2fs_post_read_required(inode))
f2fs_wait_on_block_writeback(sbi, blkaddr);
if (len == PAGE_SIZE || PageUptodate(page)) if (len == PAGE_SIZE || PageUptodate(page))
return 0; return 0;
......
...@@ -2973,7 +2973,7 @@ void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page, ...@@ -2973,7 +2973,7 @@ void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
struct f2fs_io_info *fio, bool add_list); struct f2fs_io_info *fio, bool add_list);
void f2fs_wait_on_page_writeback(struct page *page, void f2fs_wait_on_page_writeback(struct page *page,
enum page_type type, bool ordered); enum page_type type, bool ordered);
void f2fs_wait_on_block_writeback(struct f2fs_sb_info *sbi, block_t blkaddr); void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr);
void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk); void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk); void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type, int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
......
...@@ -112,8 +112,7 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf) ...@@ -112,8 +112,7 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
f2fs_wait_on_page_writeback(page, DATA, false); f2fs_wait_on_page_writeback(page, DATA, false);
/* wait for GCed page writeback via META_MAPPING */ /* wait for GCed page writeback via META_MAPPING */
if (f2fs_post_read_required(inode)) f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
f2fs_wait_on_block_writeback(sbi, dn.data_blkaddr);
out_sem: out_sem:
up_read(&F2FS_I(inode)->i_mmap_sem); up_read(&F2FS_I(inode)->i_mmap_sem);
......
...@@ -3214,10 +3214,14 @@ void f2fs_wait_on_page_writeback(struct page *page, ...@@ -3214,10 +3214,14 @@ void f2fs_wait_on_page_writeback(struct page *page,
} }
} }
void f2fs_wait_on_block_writeback(struct f2fs_sb_info *sbi, block_t blkaddr) void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr)
{ {
struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
struct page *cpage; struct page *cpage;
if (!f2fs_post_read_required(inode))
return;
if (!is_valid_data_blkaddr(sbi, blkaddr)) if (!is_valid_data_blkaddr(sbi, blkaddr))
return; return;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment