Skip to content

Commit aa8bc1a

Browse files
committed
fscrypt: support decrypting multiple filesystem blocks per page
Rename fscrypt_decrypt_page() to fscrypt_decrypt_pagecache_blocks() and redefine its behavior to decrypt all filesystem blocks in the given region of the given page, rather than assuming that the region consists of just one filesystem block. Also remove the 'inode' and 'lblk_num' parameters, since they can be retrieved from the page as it's already assumed to be a pagecache page. This is in preparation for allowing encryption on ext4 filesystems with blocksize != PAGE_SIZE. This is based on work by Chandan Rajendra. Reviewed-by: Chandan Rajendra <chandan@linux.ibm.com> Signed-off-by: Eric Biggers <ebiggers@google.com>
1 parent 41adbcb commit aa8bc1a

4 files changed

Lines changed: 42 additions & 26 deletions

File tree

‎fs/crypto/bio.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ static void __fscrypt_decrypt_bio(struct bio *bio, bool done)
3333

3434
bio_for_each_segment_all(bv, bio, iter_all) {
3535
struct page *page = bv->bv_page;
36-
int ret = fscrypt_decrypt_page(page->mapping->host, page,
37-
PAGE_SIZE, 0, page->index);
36+
int ret = fscrypt_decrypt_pagecache_blocks(page, PAGE_SIZE, 0);
3837

3938
if (ret)
4039
SetPageError(page);

‎fs/crypto/crypto.c‎

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -283,29 +283,47 @@ int fscrypt_encrypt_block_inplace(const struct inode *inode, struct page *page,
283283
EXPORT_SYMBOL(fscrypt_encrypt_block_inplace);
284284

285285
/**
286-
* fscrypt_decrypt_page() - Decrypts a page in-place
287-
* @inode: The corresponding inode for the page to decrypt.
288-
* @page: The page to decrypt. Must be locked.
289-
* @len: Number of bytes in @page to be decrypted.
290-
* @offs: Start of data in @page.
291-
* @lblk_num: Logical block number.
286+
* fscrypt_decrypt_pagecache_blocks() - Decrypt filesystem blocks in a pagecache page
287+
* @page: The locked pagecache page containing the block(s) to decrypt
288+
* @len: Total size of the block(s) to decrypt. Must be a nonzero
289+
* multiple of the filesystem's block size.
290+
* @offs: Byte offset within @page of the first block to decrypt. Must be
291+
* a multiple of the filesystem's block size.
292292
*
293-
* Decrypts page in-place using the ctx encryption context.
293+
* The specified block(s) are decrypted in-place within the pagecache page,
294+
* which must still be locked and not uptodate. Normally, blocksize ==
295+
* PAGE_SIZE and the whole page is decrypted at once.
294296
*
295-
* Called from the read completion callback.
297+
* This is for use by the filesystem's ->readpages() method.
296298
*
297-
* Return: Zero on success, non-zero otherwise.
299+
* Return: 0 on success; -errno on failure
298300
*/
299-
int fscrypt_decrypt_page(const struct inode *inode, struct page *page,
300-
unsigned int len, unsigned int offs, u64 lblk_num)
301+
int fscrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
302+
unsigned int offs)
301303
{
304+
const struct inode *inode = page->mapping->host;
305+
const unsigned int blockbits = inode->i_blkbits;
306+
const unsigned int blocksize = 1 << blockbits;
307+
u64 lblk_num = ((u64)page->index << (PAGE_SHIFT - blockbits)) +
308+
(offs >> blockbits);
309+
unsigned int i;
310+
int err;
311+
302312
if (WARN_ON_ONCE(!PageLocked(page)))
303313
return -EINVAL;
304314

305-
return fscrypt_crypt_block(inode, FS_DECRYPT, lblk_num, page, page,
306-
len, offs, GFP_NOFS);
315+
if (WARN_ON_ONCE(len <= 0 || !IS_ALIGNED(len | offs, blocksize)))
316+
return -EINVAL;
317+
318+
for (i = offs; i < offs + len; i += blocksize, lblk_num++) {
319+
err = fscrypt_crypt_block(inode, FS_DECRYPT, lblk_num, page,
320+
page, blocksize, i, GFP_NOFS);
321+
if (err)
322+
return err;
323+
}
324+
return 0;
307325
}
308-
EXPORT_SYMBOL(fscrypt_decrypt_page);
326+
EXPORT_SYMBOL(fscrypt_decrypt_pagecache_blocks);
309327

310328
/**
311329
* fscrypt_decrypt_block_inplace() - Decrypt a filesystem block in-place

‎fs/ext4/inode.c‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,8 +1232,7 @@ static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len,
12321232
if (unlikely(err))
12331233
page_zero_new_buffers(page, from, to);
12341234
else if (decrypt)
1235-
err = fscrypt_decrypt_page(page->mapping->host, page,
1236-
PAGE_SIZE, 0, page->index);
1235+
err = fscrypt_decrypt_pagecache_blocks(page, PAGE_SIZE, 0);
12371236
return err;
12381237
}
12391238
#endif
@@ -4066,8 +4065,8 @@ static int __ext4_block_zero_page_range(handle_t *handle,
40664065
/* We expect the key to be set. */
40674066
BUG_ON(!fscrypt_has_encryption_key(inode));
40684067
BUG_ON(blocksize != PAGE_SIZE);
4069-
WARN_ON_ONCE(fscrypt_decrypt_page(page->mapping->host,
4070-
page, PAGE_SIZE, 0, page->index));
4068+
WARN_ON_ONCE(fscrypt_decrypt_pagecache_blocks(
4069+
page, PAGE_SIZE, 0));
40714070
}
40724071
}
40734072
if (ext4_should_journal_data(inode)) {

‎include/linux/fscrypt.h‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ extern int fscrypt_encrypt_block_inplace(const struct inode *inode,
112112
struct page *page, unsigned int len,
113113
unsigned int offs, u64 lblk_num,
114114
gfp_t gfp_flags);
115-
extern int fscrypt_decrypt_page(const struct inode *, struct page *, unsigned int,
116-
unsigned int, u64);
115+
116+
extern int fscrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
117+
unsigned int offs);
117118
extern int fscrypt_decrypt_block_inplace(const struct inode *inode,
118119
struct page *page, unsigned int len,
119120
unsigned int offs, u64 lblk_num);
@@ -310,10 +311,9 @@ static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
310311
return -EOPNOTSUPP;
311312
}
312313

313-
static inline int fscrypt_decrypt_page(const struct inode *inode,
314-
struct page *page,
315-
unsigned int len, unsigned int offs,
316-
u64 lblk_num)
314+
static inline int fscrypt_decrypt_pagecache_blocks(struct page *page,
315+
unsigned int len,
316+
unsigned int offs)
317317
{
318318
return -EOPNOTSUPP;
319319
}

0 commit comments

Comments
 (0)