Skip to content

Commit 0cb8dae

Browse files
ebiggersJaegeuk Kim
authored andcommitted
fscrypt: allow synchronous bio decryption
Currently, fscrypt provides fscrypt_decrypt_bio_pages() which decrypts a bio's pages asynchronously, then unlocks them afterwards. But, this assumes that decryption is the last "postprocessing step" for the bio, so it's incompatible with additional postprocessing steps such as authenticity verification after decryption. Therefore, rename the existing fscrypt_decrypt_bio_pages() to fscrypt_enqueue_decrypt_bio(). Then, add fscrypt_decrypt_bio() which decrypts the pages in the bio synchronously without unlocking the pages, nor setting them Uptodate; and add fscrypt_enqueue_decrypt_work(), which enqueues work on the fscrypt_read_workqueue. The new functions will be used by filesystems that support both fscrypt and fs-verity. Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
1 parent 2d618bd commit 0cb8dae

7 files changed

Lines changed: 45 additions & 21 deletions

File tree

‎fs/crypto/bio.c‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,8 @@
2626
#include <linux/namei.h>
2727
#include "fscrypt_private.h"
2828

29-
/*
30-
* Call fscrypt_decrypt_page on every single page, reusing the encryption
31-
* context.
32-
*/
33-
static void completion_pages(struct work_struct *work)
29+
static void __fscrypt_decrypt_bio(struct bio *bio, bool done)
3430
{
35-
struct fscrypt_ctx *ctx =
36-
container_of(work, struct fscrypt_ctx, r.work);
37-
struct bio *bio = ctx->r.bio;
3831
struct bio_vec *bv;
3932
int i;
4033

@@ -46,22 +39,38 @@ static void completion_pages(struct work_struct *work)
4639
if (ret) {
4740
WARN_ON_ONCE(1);
4841
SetPageError(page);
49-
} else {
42+
} else if (done) {
5043
SetPageUptodate(page);
5144
}
52-
unlock_page(page);
45+
if (done)
46+
unlock_page(page);
5347
}
48+
}
49+
50+
void fscrypt_decrypt_bio(struct bio *bio)
51+
{
52+
__fscrypt_decrypt_bio(bio, false);
53+
}
54+
EXPORT_SYMBOL(fscrypt_decrypt_bio);
55+
56+
static void completion_pages(struct work_struct *work)
57+
{
58+
struct fscrypt_ctx *ctx =
59+
container_of(work, struct fscrypt_ctx, r.work);
60+
struct bio *bio = ctx->r.bio;
61+
62+
__fscrypt_decrypt_bio(bio, true);
5463
fscrypt_release_ctx(ctx);
5564
bio_put(bio);
5665
}
5766

58-
void fscrypt_decrypt_bio_pages(struct fscrypt_ctx *ctx, struct bio *bio)
67+
void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx, struct bio *bio)
5968
{
6069
INIT_WORK(&ctx->r.work, completion_pages);
6170
ctx->r.bio = bio;
62-
queue_work(fscrypt_read_workqueue, &ctx->r.work);
71+
fscrypt_enqueue_decrypt_work(&ctx->r.work);
6372
}
64-
EXPORT_SYMBOL(fscrypt_decrypt_bio_pages);
73+
EXPORT_SYMBOL(fscrypt_enqueue_decrypt_bio);
6574

6675
void fscrypt_pullback_bio_page(struct page **page, bool restore)
6776
{

‎fs/crypto/crypto.c‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,18 @@ static mempool_t *fscrypt_bounce_page_pool = NULL;
4545
static LIST_HEAD(fscrypt_free_ctxs);
4646
static DEFINE_SPINLOCK(fscrypt_ctx_lock);
4747

48-
struct workqueue_struct *fscrypt_read_workqueue;
48+
static struct workqueue_struct *fscrypt_read_workqueue;
4949
static DEFINE_MUTEX(fscrypt_init_mutex);
5050

5151
static struct kmem_cache *fscrypt_ctx_cachep;
5252
struct kmem_cache *fscrypt_info_cachep;
5353

54+
void fscrypt_enqueue_decrypt_work(struct work_struct *work)
55+
{
56+
queue_work(fscrypt_read_workqueue, work);
57+
}
58+
EXPORT_SYMBOL(fscrypt_enqueue_decrypt_work);
59+
5460
/**
5561
* fscrypt_release_ctx() - Releases an encryption context
5662
* @ctx: The encryption context to release.

‎fs/crypto/fscrypt_private.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ static inline bool fscrypt_valid_enc_modes(u32 contents_mode,
9797
/* crypto.c */
9898
extern struct kmem_cache *fscrypt_info_cachep;
9999
extern int fscrypt_initialize(unsigned int cop_flags);
100-
extern struct workqueue_struct *fscrypt_read_workqueue;
101100
extern int fscrypt_do_page_crypto(const struct inode *inode,
102101
fscrypt_direction_t rw, u64 lblk_num,
103102
struct page *src_page,

‎fs/ext4/readpage.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static void mpage_end_io(struct bio *bio)
7777
if (bio->bi_status) {
7878
fscrypt_release_ctx(bio->bi_private);
7979
} else {
80-
fscrypt_decrypt_bio_pages(bio->bi_private, bio);
80+
fscrypt_enqueue_decrypt_bio(bio->bi_private, bio);
8181
return;
8282
}
8383
}

‎fs/f2fs/data.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static void f2fs_read_end_io(struct bio *bio)
6666
if (bio->bi_status) {
6767
fscrypt_release_ctx(bio->bi_private);
6868
} else {
69-
fscrypt_decrypt_bio_pages(bio->bi_private, bio);
69+
fscrypt_enqueue_decrypt_bio(bio->bi_private, bio);
7070
return;
7171
}
7272
}

‎include/linux/fscrypt_notsupp.h‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
2525
}
2626

2727
/* crypto.c */
28+
static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
29+
{
30+
}
31+
2832
static inline struct fscrypt_ctx *fscrypt_get_ctx(const struct inode *inode,
2933
gfp_t gfp_flags)
3034
{
@@ -160,10 +164,13 @@ static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
160164
}
161165

162166
/* bio.c */
163-
static inline void fscrypt_decrypt_bio_pages(struct fscrypt_ctx *ctx,
164-
struct bio *bio)
167+
static inline void fscrypt_decrypt_bio(struct bio *bio)
168+
{
169+
}
170+
171+
static inline void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
172+
struct bio *bio)
165173
{
166-
return;
167174
}
168175

169176
static inline void fscrypt_pullback_bio_page(struct page **page, bool restore)

‎include/linux/fscrypt_supp.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
5959
}
6060

6161
/* crypto.c */
62+
extern void fscrypt_enqueue_decrypt_work(struct work_struct *);
6263
extern struct fscrypt_ctx *fscrypt_get_ctx(const struct inode *, gfp_t);
6364
extern void fscrypt_release_ctx(struct fscrypt_ctx *);
6465
extern struct page *fscrypt_encrypt_page(const struct inode *, struct page *,
@@ -188,7 +189,9 @@ static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
188189
}
189190

190191
/* bio.c */
191-
extern void fscrypt_decrypt_bio_pages(struct fscrypt_ctx *, struct bio *);
192+
extern void fscrypt_decrypt_bio(struct bio *);
193+
extern void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
194+
struct bio *bio);
192195
extern void fscrypt_pullback_bio_page(struct page **, bool);
193196
extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
194197
unsigned int);

0 commit comments

Comments
 (0)