From: Eric Biggers <ebiggers3@gmail.com>
To: Chandan Rajendra <chandan@linux.vnet.ibm.com>
Cc: linux-ext4@vger.kernel.org, linux-fsdevel@vger.kernel.org,
linux-fscrypt@vger.kernel.org, tytso@mit.edu
Subject: Re: [RFC PATCH V2 10/11] Enable writing encrypted files in blocksize less than pagesize setup
Date: Wed, 21 Feb 2018 10:53:31 -0800 [thread overview]
Message-ID: <20180221185331.GA114620@gmail.com> (raw)
In-Reply-To: <2490066.ZFX8CK6sZb@localhost.localdomain>
On Wed, Feb 21, 2018 at 03:27:29PM +0530, Chandan Rajendra wrote:
> On Wednesday, February 21, 2018 6:24:54 AM IST Eric Biggers wrote:
> > On Mon, Feb 12, 2018 at 03:13:46PM +0530, Chandan Rajendra wrote:
> > > This commit splits the functionality of fscrypt_encrypt_block(). The
> > > allocation of fscrypt context and cipher text page is moved to a new
> > > function fscrypt_prep_ciphertext_page().
> > >
> > > ext4_bio_write_page() is modified to appropriately make use of the above
> > > two functions.
> > >
> > > Signed-off-by: Chandan Rajendra <chandan@linux.vnet.ibm.com>
> >
> > Well, this patch also modifies ext4_bio_write_page() to support the blocksize <
> > pagesize case. The commit message makes it sound like it's just refactoring.
> >
> > > diff --git a/fs/ext4/page-io.c b/fs/ext4/page-io.c
> > > index 0a4a1e7..1e869d5 100644
> > > --- a/fs/ext4/page-io.c
> > > +++ b/fs/ext4/page-io.c
> > > @@ -419,9 +419,12 @@ int ext4_bio_write_page(struct ext4_io_submit *io,
> > > struct inode *inode = page->mapping->host;
> > > unsigned block_start;
> > > struct buffer_head *bh, *head;
> > > + u64 blk_nr;
> > > + gfp_t gfp_flags = GFP_NOFS;
> > > int ret = 0;
> > > int nr_submitted = 0;
> > > int nr_to_submit = 0;
> > > + int blocksize = (1 << inode->i_blkbits);
> > >
> > > BUG_ON(!PageLocked(page));
> > > BUG_ON(PageWriteback(page));
> > > @@ -475,15 +478,11 @@ int ext4_bio_write_page(struct ext4_io_submit *io,
> > > nr_to_submit++;
> > > } while ((bh = bh->b_this_page) != head);
> > >
> > > - bh = head = page_buffers(page);
> > > -
> > > - if (ext4_encrypted_inode(inode) && S_ISREG(inode->i_mode) &&
> > > - nr_to_submit) {
> > > - gfp_t gfp_flags = GFP_NOFS;
> > > -
> > > - retry_encrypt:
> > > - data_page = fscrypt_encrypt_block(inode, page, PAGE_SIZE, 0,
> > > - page->index, gfp_flags);
> > > + if (ext4_encrypted_inode(inode) && S_ISREG(inode->i_mode)
> > > + && nr_to_submit) {
> > > + retry_prep_ciphertext_page:
> > > + data_page = fscrypt_prep_ciphertext_page(inode, page,
> > > + gfp_flags);
> > > if (IS_ERR(data_page)) {
> > > ret = PTR_ERR(data_page);
> > > if (ret == -ENOMEM && wbc->sync_mode == WB_SYNC_ALL) {
> > > @@ -492,17 +491,28 @@ int ext4_bio_write_page(struct ext4_io_submit *io,
> > > congestion_wait(BLK_RW_ASYNC, HZ/50);
> > > }
> > > gfp_flags |= __GFP_NOFAIL;
> > > - goto retry_encrypt;
> > > + goto retry_prep_ciphertext_page;
> > > }
> > > data_page = NULL;
> > > goto out;
> > > }
> > > }
> > >
> > > + blk_nr = page->index << (PAGE_SHIFT - inode->i_blkbits);
> > > +
> > > /* Now submit buffers to write */
> > > + bh = head = page_buffers(page);
> > > do {
> > > if (!buffer_async_write(bh))
> > > continue;
> > > +
> > > + if (ext4_encrypted_inode(inode) && S_ISREG(inode->i_mode)) {
> > > + ret = fscrypt_encrypt_block(inode, page, data_page, blocksize,
> > > + bh_offset(bh), blk_nr, gfp_flags);
> > > + if (ret)
> > > + break;
> > > + }
> > > +
> > > ret = io_submit_add_bh(io, inode,
> > > data_page ? data_page : page, bh);
> > > if (ret) {
> > > @@ -515,12 +525,12 @@ int ext4_bio_write_page(struct ext4_io_submit *io,
> > > }
> > > nr_submitted++;
> > > clear_buffer_dirty(bh);
> > > - } while ((bh = bh->b_this_page) != head);
> > > + } while (++blk_nr, (bh = bh->b_this_page) != head);
> > >
> > > /* Error stopped previous loop? Clean up buffers... */
> > > if (ret) {
> > > out:
> > > - if (data_page)
> > > + if (data_page && bh == head)
> > > fscrypt_restore_control_page(data_page);
> > > printk_ratelimited(KERN_ERR "%s: ret = %d\n", __func__, ret);
> > > redirty_page_for_writepage(wbc, page);
> >
> > I'm wondering why you didn't move the crypto stuff in ext4_bio_write_page() into
> > a separate function like I had suggested? It's true we don't have to encrypt
> > all the blocks in the page at once, but it would make the crypto stuff more
> > self-contained.
>
> Eric, Are you suggesting that the entire block of code that has invocations to
> fscrypt_prep_ciphertext_page() and fscrypt_encrypt_block() be moved to a
> separate function that gets defined in fscrypt module?
I just had in mind that it would be a separate function in ext4.
>
> If yes, In Ext4, We have the invocation of io_submit_add_bh() being
> interleaved with calls to fscrypt_encrypt_block().
>
Well yes that's what your patch does. But we could instead just encrypt all the
blocks at once, right? It would be a bit less efficient since we'd have to
iterate through the buffer_head list twice, but the advantage is that we end up
with ~105 lines ext4_bio_write_page() instead of 130, since the crypto stuff
would be more self-contained. Here's an example, given as a diff from master
(beware, it's compile-tested only):
diff --git a/fs/ext4/page-io.c b/fs/ext4/page-io.c
index db7590178dfc..e0153c8c4bc4 100644
--- a/fs/ext4/page-io.c
+++ b/fs/ext4/page-io.c
@@ -409,6 +409,47 @@ static int io_submit_add_bh(struct ext4_io_submit *io,
return 0;
}
+static struct page *
+encrypt_page(struct inode *inode, struct page *page,
+ struct writeback_control *wbc, struct ext4_io_submit *io)
+{
+ struct page *data_page;
+ struct buffer_head *bh, *head;
+ gfp_t gfp_flags = GFP_NOFS;
+ u64 blk_nr;
+ int err;
+
+retry:
+ data_page = fscrypt_prep_ciphertext_page(inode, page, gfp_flags);
+ if (IS_ERR(data_page))
+ goto out;
+
+ bh = head = page_buffers(page);
+ blk_nr = (u64)page->index << (PAGE_SHIFT - inode->i_blkbits);
+ do {
+ if (!buffer_async_write(bh))
+ continue;
+ err = fscrypt_encrypt_block(inode, page, data_page, bh->b_size,
+ bh_offset(bh), blk_nr, gfp_flags);
+ if (err) {
+ fscrypt_restore_control_page(data_page);
+ data_page = ERR_PTR(err);
+ break;
+ }
+ } while (blk_nr++, (bh = bh->b_this_page) != head);
+
+out:
+ if (data_page == ERR_PTR(-ENOMEM) && wbc->sync_mode == WB_SYNC_ALL) {
+ if (io->io_bio) {
+ ext4_io_submit(io);
+ congestion_wait(BLK_RW_ASYNC, HZ/50);
+ }
+ gfp_flags |= __GFP_NOFAIL;
+ goto retry;
+ }
+ return data_page;
+}
+
int ext4_bio_write_page(struct ext4_io_submit *io,
struct page *page,
int len,
@@ -477,23 +518,10 @@ int ext4_bio_write_page(struct ext4_io_submit *io,
bh = head = page_buffers(page);
- if (ext4_encrypted_inode(inode) && S_ISREG(inode->i_mode) &&
- nr_to_submit) {
- gfp_t gfp_flags = GFP_NOFS;
-
- retry_encrypt:
- data_page = fscrypt_encrypt_page(inode, page, PAGE_SIZE, 0,
- page->index, gfp_flags);
+ if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode) && nr_to_submit) {
+ data_page = encrypt_page(inode, page, wbc, io);
if (IS_ERR(data_page)) {
ret = PTR_ERR(data_page);
- if (ret == -ENOMEM && wbc->sync_mode == WB_SYNC_ALL) {
- if (io->io_bio) {
- ext4_io_submit(io);
- congestion_wait(BLK_RW_ASYNC, HZ/50);
- }
- gfp_flags |= __GFP_NOFAIL;
- goto retry_encrypt;
- }
data_page = NULL;
goto out;
}
next prev parent reply other threads:[~2018-02-21 18:53 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-02-12 9:43 [RFC PATCH V2 00/11] Ext4 encryption support for blocksize < pagesize Chandan Rajendra
2018-02-12 9:43 ` [RFC PATCH V2 01/11] ext4: Clear BH_Uptodate flag on decryption error Chandan Rajendra
2018-02-12 9:43 ` [RFC PATCH V2 02/11] fs/buffer.c: Export end_buffer_async_read and create_page_buffers Chandan Rajendra
2018-02-12 9:43 ` [RFC PATCH V2 03/11] fs/crypto/: Rename functions to indicate that they operate on FS blocks Chandan Rajendra
2018-02-12 9:43 ` [RFC PATCH V2 04/11] completion_pages: Decrypt all contiguous blocks in a page Chandan Rajendra
2018-02-12 9:43 ` [RFC PATCH V2 05/11] ext4: Decrypt all boundary blocks when doing buffered write Chandan Rajendra
2018-02-21 1:01 ` Eric Biggers
2018-02-21 9:57 ` Chandan Rajendra
2018-02-12 9:43 ` [RFC PATCH V2 06/11] ext4: Decrypt the block that needs to be partially zeroed Chandan Rajendra
2018-02-12 9:43 ` [RFC PATCH V2 07/11] fscrypt_zeroout_range: Encrypt all zeroed out blocks of a page Chandan Rajendra
2018-02-21 1:16 ` Eric Biggers
2018-02-21 9:57 ` Chandan Rajendra
2018-03-26 6:05 ` Theodore Y. Ts'o
2018-03-26 8:22 ` Chandan Rajendra
2018-03-27 19:40 ` Theodore Y. Ts'o
2018-03-28 13:36 ` Chandan Rajendra
2018-04-05 7:03 ` Chandan Rajendra
2018-04-05 12:47 ` Theodore Y. Ts'o
2018-04-05 13:07 ` Chandan Rajendra
2018-04-05 20:50 ` Theodore Y. Ts'o
2018-02-12 9:43 ` [RFC PATCH V2 08/11] Enable reading encrypted files in blocksize less than pagesize setup Chandan Rajendra
2018-02-12 9:43 ` [RFC PATCH V2 09/11] fscrypt: Move completion_pages to crypto/readpage.c Chandan Rajendra
2018-02-12 9:43 ` [RFC PATCH V2 10/11] Enable writing encrypted files in blocksize less than pagesize setup Chandan Rajendra
2018-02-21 0:54 ` Eric Biggers
2018-02-21 9:57 ` Chandan Rajendra
2018-02-21 18:53 ` Eric Biggers [this message]
2018-02-12 9:43 ` [RFC PATCH V2 11/11] ext4: Enable encryption for blocksize less than page size Chandan Rajendra
2018-02-12 9:43 ` Chandan Rajendra
2018-02-21 0:48 ` [RFC PATCH V2 00/11] Ext4 encryption support for blocksize < pagesize Eric Biggers
2018-02-21 9:57 ` Chandan Rajendra
2018-02-21 19:06 ` Eric Biggers
2018-02-22 8:50 ` Chandan Rajendra
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180221185331.GA114620@gmail.com \
--to=ebiggers3@gmail.com \
--cc=chandan@linux.vnet.ibm.com \
--cc=linux-ext4@vger.kernel.org \
--cc=linux-fscrypt@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=tytso@mit.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.