Commit 0b578f35 authored by Chandan Rajendra's avatar Chandan Rajendra Committed by Eric Biggers

ext4: decrypt only the needed blocks in ext4_block_write_begin()

In ext4_block_write_begin(), only decrypt the blocks that actually need
to be decrypted (up to two blocks which intersect the boundaries of the
region that will be written to), rather than assuming blocksize ==
PAGE_SIZE and decrypting the whole page.

This is in preparation for allowing encryption on ext4 filesystems with
blocksize != PAGE_SIZE.
Signed-off-by: default avatarChandan Rajendra <chandan@linux.ibm.com>
(EB: rebase onto previous changes, improve the commit message,
 and move the check for encrypted inode)
Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
parent 7e0785fc
...@@ -1164,8 +1164,9 @@ static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len, ...@@ -1164,8 +1164,9 @@ static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len,
int err = 0; int err = 0;
unsigned blocksize = inode->i_sb->s_blocksize; unsigned blocksize = inode->i_sb->s_blocksize;
unsigned bbits; unsigned bbits;
struct buffer_head *bh, *head, *wait[2], **wait_bh = wait; struct buffer_head *bh, *head, *wait[2];
bool decrypt = false; int nr_wait = 0;
int i;
BUG_ON(!PageLocked(page)); BUG_ON(!PageLocked(page));
BUG_ON(from > PAGE_SIZE); BUG_ON(from > PAGE_SIZE);
...@@ -1217,24 +1218,30 @@ static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len, ...@@ -1217,24 +1218,30 @@ static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len,
!buffer_unwritten(bh) && !buffer_unwritten(bh) &&
(block_start < from || block_end > to)) { (block_start < from || block_end > to)) {
ll_rw_block(REQ_OP_READ, 0, 1, &bh); ll_rw_block(REQ_OP_READ, 0, 1, &bh);
*wait_bh++ = bh; wait[nr_wait++] = bh;
decrypt = IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
} }
} }
/* /*
* If we issued read requests, let them complete. * If we issued read requests, let them complete.
*/ */
while (wait_bh > wait) { for (i = 0; i < nr_wait; i++) {
wait_on_buffer(*--wait_bh); wait_on_buffer(wait[i]);
if (!buffer_uptodate(*wait_bh)) if (!buffer_uptodate(wait[i]))
err = -EIO; err = -EIO;
} }
if (unlikely(err)) { if (unlikely(err)) {
page_zero_new_buffers(page, from, to); page_zero_new_buffers(page, from, to);
} else if (decrypt) { } else if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) {
err = fscrypt_decrypt_pagecache_blocks(page, PAGE_SIZE, 0); for (i = 0; i < nr_wait; i++) {
if (err) int err2;
clear_buffer_uptodate(*wait_bh);
err2 = fscrypt_decrypt_pagecache_blocks(page, blocksize,
bh_offset(wait[i]));
if (err2) {
clear_buffer_uptodate(wait[i]);
err = err2;
}
}
} }
return err; return err;
......
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