Skip to content

Commit 9932f00

Browse files
committed
Merge tag 'fscrypt-for-linus' of git://git.kernel.org/pub/scm/fs/fscrypt/linux
Pull fscrypt updates from Eric Biggers: - Various cleanups for the interface between fs/crypto/ and filesystems, from Christoph Hellwig - Simplify and optimize the implementation of v1 key derivation by using the AES library instead of the crypto_skcipher API * tag 'fscrypt-for-linus' of git://git.kernel.org/pub/scm/fs/fscrypt/linux: fscrypt: use AES library for v1 key derivation ext4: use a byte granularity cursor in ext4_mpage_readpages fscrypt: pass a real sector_t to fscrypt_zeroout_range fscrypt: pass a byte length to fscrypt_zeroout_range fscrypt: pass a byte offset to fscrypt_zeroout_range fscrypt: pass a byte length to fscrypt_zeroout_range_inline_crypt fscrypt: pass a byte offset to fscrypt_zeroout_range_inline_crypt fscrypt: pass a byte offset to fscrypt_set_bio_crypt_ctx fscrypt: pass a byte offset to fscrypt_mergeable_bio fscrypt: pass a byte offset to fscrypt_generate_dun fscrypt: move fscrypt_set_bio_crypt_ctx_bh to buffer.c ext4, fscrypt: merge fscrypt_mergeable_bio_bh into io_submit_need_new_bio ext4: factor out a io_submit_need_new_bio helper ext4: open code fscrypt_set_bio_crypt_ctx_bh ext4: initialize the write hint in io_submit_init_bio
2 parents 81dc1e4 + 1546d3f commit 9932f00

14 files changed

Lines changed: 120 additions & 213 deletions

File tree

fs/buffer.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2667,6 +2667,21 @@ static void end_bio_bh_io_sync(struct bio *bio)
26672667
bio_put(bio);
26682668
}
26692669

2670+
static void buffer_set_crypto_ctx(struct bio *bio, const struct buffer_head *bh,
2671+
gfp_t gfp_mask)
2672+
{
2673+
const struct address_space *mapping = folio_mapping(bh->b_folio);
2674+
2675+
/*
2676+
* The ext4 journal (jbd2) can submit a buffer_head it directly created
2677+
* for a non-pagecache page. fscrypt doesn't care about these.
2678+
*/
2679+
if (!mapping)
2680+
return;
2681+
fscrypt_set_bio_crypt_ctx(bio, mapping->host,
2682+
folio_pos(bh->b_folio) + bh_offset(bh), gfp_mask);
2683+
}
2684+
26702685
static void submit_bh_wbc(blk_opf_t opf, struct buffer_head *bh,
26712686
enum rw_hint write_hint,
26722687
struct writeback_control *wbc)
@@ -2693,7 +2708,8 @@ static void submit_bh_wbc(blk_opf_t opf, struct buffer_head *bh,
26932708

26942709
bio = bio_alloc(bh->b_bdev, 1, opf, GFP_NOIO);
26952710

2696-
fscrypt_set_bio_crypt_ctx_bh(bio, bh, GFP_NOIO);
2711+
if (IS_ENABLED(CONFIG_FS_ENCRYPTION))
2712+
buffer_set_crypto_ctx(bio, bh, GFP_NOIO);
26972713

26982714
bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
26992715
bio->bi_write_hint = write_hint;

fs/crypto/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ config FS_ENCRYPTION
33
bool "FS Encryption (Per-file encryption)"
44
select CRYPTO
55
select CRYPTO_SKCIPHER
6+
select CRYPTO_LIB_AES
67
select CRYPTO_LIB_SHA256
78
select CRYPTO_LIB_SHA512
89
select KEYS
@@ -30,7 +31,6 @@ config FS_ENCRYPTION_ALGS
3031
select CRYPTO_AES
3132
select CRYPTO_CBC
3233
select CRYPTO_CTS
33-
select CRYPTO_ECB
3434
select CRYPTO_XTS
3535

3636
config FS_ENCRYPTION_INLINE_CRYPT

fs/crypto/bio.c

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,9 @@ static void fscrypt_zeroout_range_end_io(struct bio *bio)
7070
}
7171

7272
static int fscrypt_zeroout_range_inline_crypt(const struct inode *inode,
73-
pgoff_t lblk, sector_t sector,
74-
unsigned int len)
73+
loff_t pos, sector_t sector,
74+
u64 len)
7575
{
76-
const unsigned int blockbits = inode->i_blkbits;
77-
const unsigned int blocks_per_page = 1 << (PAGE_SHIFT - blockbits);
7876
struct fscrypt_zero_done done = {
7977
.pending = ATOMIC_INIT(1),
8078
.done = COMPLETION_INITIALIZER_ONSTACK(done.done),
@@ -89,18 +87,16 @@ static int fscrypt_zeroout_range_inline_crypt(const struct inode *inode,
8987
bio->bi_iter.bi_sector = sector;
9088
bio->bi_private = &done;
9189
bio->bi_end_io = fscrypt_zeroout_range_end_io;
92-
fscrypt_set_bio_crypt_ctx(bio, inode, lblk, GFP_NOFS);
90+
fscrypt_set_bio_crypt_ctx(bio, inode, pos, GFP_NOFS);
9391

9492
for (n = 0; n < BIO_MAX_VECS; n++) {
95-
unsigned int blocks_this_page =
96-
min(len, blocks_per_page);
97-
unsigned int bytes_this_page = blocks_this_page << blockbits;
93+
unsigned int bytes_this_page = min(len, PAGE_SIZE);
9894

9995
__bio_add_page(bio, ZERO_PAGE(0), bytes_this_page, 0);
100-
len -= blocks_this_page;
101-
lblk += blocks_this_page;
96+
len -= bytes_this_page;
97+
pos += bytes_this_page;
10298
sector += (bytes_this_page >> SECTOR_SHIFT);
103-
if (!len || !fscrypt_mergeable_bio(bio, inode, lblk))
99+
if (!len || !fscrypt_mergeable_bio(bio, inode, pos))
104100
break;
105101
}
106102

@@ -117,31 +113,31 @@ static int fscrypt_zeroout_range_inline_crypt(const struct inode *inode,
117113
/**
118114
* fscrypt_zeroout_range() - zero out a range of blocks in an encrypted file
119115
* @inode: the file's inode
120-
* @lblk: the first file logical block to zero out
121-
* @pblk: the first filesystem physical block to zero out
122-
* @len: number of blocks to zero out
116+
* @pos: the first file position (in bytes) to zero out
117+
* @sector: the first sector to zero out
118+
* @len: bytes to zero out
123119
*
124120
* Zero out filesystem blocks in an encrypted regular file on-disk, i.e. write
125121
* ciphertext blocks which decrypt to the all-zeroes block. The blocks must be
126122
* both logically and physically contiguous. It's also assumed that the
127-
* filesystem only uses a single block device, ->s_bdev.
123+
* filesystem only uses a single block device, ->s_bdev. @len must be a
124+
* multiple of the file system logical block size.
128125
*
129126
* Note that since each block uses a different IV, this involves writing a
130127
* different ciphertext to each block; we can't simply reuse the same one.
131128
*
132129
* Return: 0 on success; -errno on failure.
133130
*/
134-
int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
135-
sector_t pblk, unsigned int len)
131+
int fscrypt_zeroout_range(const struct inode *inode, loff_t pos,
132+
sector_t sector, u64 len)
136133
{
137134
const struct fscrypt_inode_info *ci = fscrypt_get_inode_info_raw(inode);
138135
const unsigned int du_bits = ci->ci_data_unit_bits;
139136
const unsigned int du_size = 1U << du_bits;
140137
const unsigned int du_per_page_bits = PAGE_SHIFT - du_bits;
141138
const unsigned int du_per_page = 1U << du_per_page_bits;
142-
u64 du_index = (u64)lblk << (inode->i_blkbits - du_bits);
143-
u64 du_remaining = (u64)len << (inode->i_blkbits - du_bits);
144-
sector_t sector = pblk << (inode->i_blkbits - SECTOR_SHIFT);
139+
u64 du_index = pos >> du_bits;
140+
u64 du_remaining = len >> du_bits;
145141
struct page *pages[16]; /* write up to 16 pages at a time */
146142
unsigned int nr_pages;
147143
unsigned int i;
@@ -153,7 +149,7 @@ int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
153149
return 0;
154150

155151
if (fscrypt_inode_uses_inline_crypto(inode))
156-
return fscrypt_zeroout_range_inline_crypt(inode, lblk, sector,
152+
return fscrypt_zeroout_range_inline_crypt(inode, pos, sector,
157153
len);
158154

159155
BUILD_BUG_ON(ARRAY_SIZE(pages) > BIO_MAX_VECS);

fs/crypto/fscrypt_private.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,6 @@ struct fscrypt_inode_info {
278278
*/
279279
u8 ci_data_unit_bits;
280280

281-
/* Cached value: log2 of number of data units per FS block */
282-
u8 ci_data_units_per_block_bits;
283-
284281
/* Hashed inode number. Only set for IV_INO_LBLK_32 */
285282
u32 ci_hashed_ino;
286283

fs/crypto/inline_crypt.c

Lines changed: 8 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,12 @@ bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode)
268268
EXPORT_SYMBOL_GPL(__fscrypt_inode_uses_inline_crypto);
269269

270270
static void fscrypt_generate_dun(const struct fscrypt_inode_info *ci,
271-
u64 lblk_num,
272-
u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE])
271+
loff_t pos, u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE])
273272
{
274-
u64 index = lblk_num << ci->ci_data_units_per_block_bits;
275273
union fscrypt_iv iv;
276274
int i;
277275

278-
fscrypt_generate_iv(&iv, index, ci);
276+
fscrypt_generate_iv(&iv, pos >> ci->ci_data_unit_bits, ci);
279277

280278
BUILD_BUG_ON(FSCRYPT_MAX_IV_SIZE > BLK_CRYPTO_MAX_IV_SIZE);
281279
memset(dun, 0, BLK_CRYPTO_MAX_IV_SIZE);
@@ -287,7 +285,7 @@ static void fscrypt_generate_dun(const struct fscrypt_inode_info *ci,
287285
* fscrypt_set_bio_crypt_ctx() - prepare a file contents bio for inline crypto
288286
* @bio: a bio which will eventually be submitted to the file
289287
* @inode: the file's inode
290-
* @first_lblk: the first file logical block number in the I/O
288+
* @pos: the first file position (in bytes) in the I/O
291289
* @gfp_mask: memory allocation flags - these must be a waiting mask so that
292290
* bio_crypt_set_ctx can't fail.
293291
*
@@ -300,7 +298,7 @@ static void fscrypt_generate_dun(const struct fscrypt_inode_info *ci,
300298
* The encryption context will be freed automatically when the bio is freed.
301299
*/
302300
void fscrypt_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
303-
u64 first_lblk, gfp_t gfp_mask)
301+
loff_t pos, gfp_t gfp_mask)
304302
{
305303
const struct fscrypt_inode_info *ci;
306304
u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE];
@@ -309,61 +307,16 @@ void fscrypt_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode,
309307
return;
310308
ci = fscrypt_get_inode_info_raw(inode);
311309

312-
fscrypt_generate_dun(ci, first_lblk, dun);
310+
fscrypt_generate_dun(ci, pos, dun);
313311
bio_crypt_set_ctx(bio, ci->ci_enc_key.blk_key, dun, gfp_mask);
314312
}
315313
EXPORT_SYMBOL_GPL(fscrypt_set_bio_crypt_ctx);
316314

317-
/* Extract the inode and logical block number from a buffer_head. */
318-
static bool bh_get_inode_and_lblk_num(const struct buffer_head *bh,
319-
const struct inode **inode_ret,
320-
u64 *lblk_num_ret)
321-
{
322-
struct folio *folio = bh->b_folio;
323-
const struct address_space *mapping;
324-
const struct inode *inode;
325-
326-
/*
327-
* The ext4 journal (jbd2) can submit a buffer_head it directly created
328-
* for a non-pagecache page. fscrypt doesn't care about these.
329-
*/
330-
mapping = folio_mapping(folio);
331-
if (!mapping)
332-
return false;
333-
inode = mapping->host;
334-
335-
*inode_ret = inode;
336-
*lblk_num_ret = (folio_pos(folio) + bh_offset(bh)) >> inode->i_blkbits;
337-
return true;
338-
}
339-
340-
/**
341-
* fscrypt_set_bio_crypt_ctx_bh() - prepare a file contents bio for inline
342-
* crypto
343-
* @bio: a bio which will eventually be submitted to the file
344-
* @first_bh: the first buffer_head for which I/O will be submitted
345-
* @gfp_mask: memory allocation flags
346-
*
347-
* Same as fscrypt_set_bio_crypt_ctx(), except this takes a buffer_head instead
348-
* of an inode and block number directly.
349-
*/
350-
void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio,
351-
const struct buffer_head *first_bh,
352-
gfp_t gfp_mask)
353-
{
354-
const struct inode *inode;
355-
u64 first_lblk;
356-
357-
if (bh_get_inode_and_lblk_num(first_bh, &inode, &first_lblk))
358-
fscrypt_set_bio_crypt_ctx(bio, inode, first_lblk, gfp_mask);
359-
}
360-
EXPORT_SYMBOL_GPL(fscrypt_set_bio_crypt_ctx_bh);
361-
362315
/**
363316
* fscrypt_mergeable_bio() - test whether data can be added to a bio
364317
* @bio: the bio being built up
365318
* @inode: the inode for the next part of the I/O
366-
* @next_lblk: the next file logical block number in the I/O
319+
* @pos: the next file position (in bytes) in the I/O
367320
*
368321
* When building a bio which may contain data which should undergo inline
369322
* encryption (or decryption) via fscrypt, filesystems should call this function
@@ -381,7 +334,7 @@ EXPORT_SYMBOL_GPL(fscrypt_set_bio_crypt_ctx_bh);
381334
* Return: true iff the I/O is mergeable
382335
*/
383336
bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
384-
u64 next_lblk)
337+
loff_t pos)
385338
{
386339
const struct bio_crypt_ctx *bc = bio->bi_crypt_context;
387340
const struct fscrypt_inode_info *ci;
@@ -401,34 +354,11 @@ bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
401354
if (bc->bc_key != ci->ci_enc_key.blk_key)
402355
return false;
403356

404-
fscrypt_generate_dun(ci, next_lblk, next_dun);
357+
fscrypt_generate_dun(ci, pos, next_dun);
405358
return bio_crypt_dun_is_contiguous(bc, bio->bi_iter.bi_size, next_dun);
406359
}
407360
EXPORT_SYMBOL_GPL(fscrypt_mergeable_bio);
408361

409-
/**
410-
* fscrypt_mergeable_bio_bh() - test whether data can be added to a bio
411-
* @bio: the bio being built up
412-
* @next_bh: the next buffer_head for which I/O will be submitted
413-
*
414-
* Same as fscrypt_mergeable_bio(), except this takes a buffer_head instead of
415-
* an inode and block number directly.
416-
*
417-
* Return: true iff the I/O is mergeable
418-
*/
419-
bool fscrypt_mergeable_bio_bh(struct bio *bio,
420-
const struct buffer_head *next_bh)
421-
{
422-
const struct inode *inode;
423-
u64 next_lblk;
424-
425-
if (!bh_get_inode_and_lblk_num(next_bh, &inode, &next_lblk))
426-
return !bio->bi_crypt_context;
427-
428-
return fscrypt_mergeable_bio(bio, inode, next_lblk);
429-
}
430-
EXPORT_SYMBOL_GPL(fscrypt_mergeable_bio_bh);
431-
432362
/**
433363
* fscrypt_dio_supported() - check whether DIO (direct I/O) is supported on an
434364
* inode, as far as encryption is concerned

fs/crypto/keysetup.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,8 +609,6 @@ fscrypt_setup_encryption_info(struct inode *inode,
609609

610610
crypt_info->ci_data_unit_bits =
611611
fscrypt_policy_du_bits(&crypt_info->ci_policy, inode);
612-
crypt_info->ci_data_units_per_block_bits =
613-
inode->i_blkbits - crypt_info->ci_data_unit_bits;
614612

615613
res = setup_file_encryption_key(crypt_info, need_dirhash_key, &mk);
616614
if (res)

0 commit comments

Comments
 (0)