Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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

+120 -213
+17 -1
fs/buffer.c
··· 2667 2667 bio_put(bio); 2668 2668 } 2669 2669 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 + 2670 2685 static void submit_bh_wbc(blk_opf_t opf, struct buffer_head *bh, 2671 2686 enum rw_hint write_hint, 2672 2687 struct writeback_control *wbc) ··· 2708 2693 2709 2694 bio = bio_alloc(bh->b_bdev, 1, opf, GFP_NOIO); 2710 2695 2711 - fscrypt_set_bio_crypt_ctx_bh(bio, bh, GFP_NOIO); 2696 + if (IS_ENABLED(CONFIG_FS_ENCRYPTION)) 2697 + buffer_set_crypto_ctx(bio, bh, GFP_NOIO); 2712 2698 2713 2699 bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9); 2714 2700 bio->bi_write_hint = write_hint;
+1 -1
fs/crypto/Kconfig
··· 3 3 bool "FS Encryption (Per-file encryption)" 4 4 select CRYPTO 5 5 select CRYPTO_SKCIPHER 6 + select CRYPTO_LIB_AES 6 7 select CRYPTO_LIB_SHA256 7 8 select CRYPTO_LIB_SHA512 8 9 select KEYS ··· 31 30 select CRYPTO_AES 32 31 select CRYPTO_CBC 33 32 select CRYPTO_CTS 34 - select CRYPTO_ECB 35 33 select CRYPTO_XTS 36 34 37 35 config FS_ENCRYPTION_INLINE_CRYPT
+17 -21
fs/crypto/bio.c
··· 70 70 } 71 71 72 72 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) 75 75 { 76 - const unsigned int blockbits = inode->i_blkbits; 77 - const unsigned int blocks_per_page = 1 << (PAGE_SHIFT - blockbits); 78 76 struct fscrypt_zero_done done = { 79 77 .pending = ATOMIC_INIT(1), 80 78 .done = COMPLETION_INITIALIZER_ONSTACK(done.done), ··· 87 89 bio->bi_iter.bi_sector = sector; 88 90 bio->bi_private = &done; 89 91 bio->bi_end_io = fscrypt_zeroout_range_end_io; 90 - fscrypt_set_bio_crypt_ctx(bio, inode, lblk, GFP_NOFS); 92 + fscrypt_set_bio_crypt_ctx(bio, inode, pos, GFP_NOFS); 91 93 92 94 for (n = 0; n < BIO_MAX_VECS; n++) { 93 - unsigned int blocks_this_page = 94 - min(len, blocks_per_page); 95 - unsigned int bytes_this_page = blocks_this_page << blockbits; 95 + unsigned int bytes_this_page = min(len, PAGE_SIZE); 96 96 97 97 __bio_add_page(bio, ZERO_PAGE(0), bytes_this_page, 0); 98 - len -= blocks_this_page; 99 - lblk += blocks_this_page; 98 + len -= bytes_this_page; 99 + pos += bytes_this_page; 100 100 sector += (bytes_this_page >> SECTOR_SHIFT); 101 - if (!len || !fscrypt_mergeable_bio(bio, inode, lblk)) 101 + if (!len || !fscrypt_mergeable_bio(bio, inode, pos)) 102 102 break; 103 103 } 104 104 ··· 113 117 /** 114 118 * fscrypt_zeroout_range() - zero out a range of blocks in an encrypted file 115 119 * @inode: the file's inode 116 - * @lblk: the first file logical block to zero out 117 - * @pblk: the first filesystem physical block to zero out 118 - * @len: number of blocks to zero out 120 + * @pos: the first file position (in bytes) to zero out 121 + * @sector: the first sector to zero out 122 + * @len: bytes to zero out 119 123 * 120 124 * Zero out filesystem blocks in an encrypted regular file on-disk, i.e. write 121 125 * ciphertext blocks which decrypt to the all-zeroes block. The blocks must be 122 126 * both logically and physically contiguous. It's also assumed that the 123 - * filesystem only uses a single block device, ->s_bdev. 127 + * filesystem only uses a single block device, ->s_bdev. @len must be a 128 + * multiple of the file system logical block size. 124 129 * 125 130 * Note that since each block uses a different IV, this involves writing a 126 131 * different ciphertext to each block; we can't simply reuse the same one. 127 132 * 128 133 * Return: 0 on success; -errno on failure. 129 134 */ 130 - int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk, 131 - sector_t pblk, unsigned int len) 135 + int fscrypt_zeroout_range(const struct inode *inode, loff_t pos, 136 + sector_t sector, u64 len) 132 137 { 133 138 const struct fscrypt_inode_info *ci = fscrypt_get_inode_info_raw(inode); 134 139 const unsigned int du_bits = ci->ci_data_unit_bits; 135 140 const unsigned int du_size = 1U << du_bits; 136 141 const unsigned int du_per_page_bits = PAGE_SHIFT - du_bits; 137 142 const unsigned int du_per_page = 1U << du_per_page_bits; 138 - u64 du_index = (u64)lblk << (inode->i_blkbits - du_bits); 139 - u64 du_remaining = (u64)len << (inode->i_blkbits - du_bits); 140 - sector_t sector = pblk << (inode->i_blkbits - SECTOR_SHIFT); 143 + u64 du_index = pos >> du_bits; 144 + u64 du_remaining = len >> du_bits; 141 145 struct page *pages[16]; /* write up to 16 pages at a time */ 142 146 unsigned int nr_pages; 143 147 unsigned int i; ··· 149 153 return 0; 150 154 151 155 if (fscrypt_inode_uses_inline_crypto(inode)) 152 - return fscrypt_zeroout_range_inline_crypt(inode, lblk, sector, 156 + return fscrypt_zeroout_range_inline_crypt(inode, pos, sector, 153 157 len); 154 158 155 159 BUILD_BUG_ON(ARRAY_SIZE(pages) > BIO_MAX_VECS);
-3
fs/crypto/fscrypt_private.h
··· 278 278 */ 279 279 u8 ci_data_unit_bits; 280 280 281 - /* Cached value: log2 of number of data units per FS block */ 282 - u8 ci_data_units_per_block_bits; 283 - 284 281 /* Hashed inode number. Only set for IV_INO_LBLK_32 */ 285 282 u32 ci_hashed_ino; 286 283
+8 -78
fs/crypto/inline_crypt.c
··· 268 268 EXPORT_SYMBOL_GPL(__fscrypt_inode_uses_inline_crypto); 269 269 270 270 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]) 273 272 { 274 - u64 index = lblk_num << ci->ci_data_units_per_block_bits; 275 273 union fscrypt_iv iv; 276 274 int i; 277 275 278 - fscrypt_generate_iv(&iv, index, ci); 276 + fscrypt_generate_iv(&iv, pos >> ci->ci_data_unit_bits, ci); 279 277 280 278 BUILD_BUG_ON(FSCRYPT_MAX_IV_SIZE > BLK_CRYPTO_MAX_IV_SIZE); 281 279 memset(dun, 0, BLK_CRYPTO_MAX_IV_SIZE); ··· 285 287 * fscrypt_set_bio_crypt_ctx() - prepare a file contents bio for inline crypto 286 288 * @bio: a bio which will eventually be submitted to the file 287 289 * @inode: the file's inode 288 - * @first_lblk: the first file logical block number in the I/O 290 + * @pos: the first file position (in bytes) in the I/O 289 291 * @gfp_mask: memory allocation flags - these must be a waiting mask so that 290 292 * bio_crypt_set_ctx can't fail. 291 293 * ··· 298 300 * The encryption context will be freed automatically when the bio is freed. 299 301 */ 300 302 void fscrypt_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode, 301 - u64 first_lblk, gfp_t gfp_mask) 303 + loff_t pos, gfp_t gfp_mask) 302 304 { 303 305 const struct fscrypt_inode_info *ci; 304 306 u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE]; ··· 307 309 return; 308 310 ci = fscrypt_get_inode_info_raw(inode); 309 311 310 - fscrypt_generate_dun(ci, first_lblk, dun); 312 + fscrypt_generate_dun(ci, pos, dun); 311 313 bio_crypt_set_ctx(bio, ci->ci_enc_key.blk_key, dun, gfp_mask); 312 314 } 313 315 EXPORT_SYMBOL_GPL(fscrypt_set_bio_crypt_ctx); 314 - 315 - /* Extract the inode and logical block number from a buffer_head. */ 316 - static bool bh_get_inode_and_lblk_num(const struct buffer_head *bh, 317 - const struct inode **inode_ret, 318 - u64 *lblk_num_ret) 319 - { 320 - struct folio *folio = bh->b_folio; 321 - const struct address_space *mapping; 322 - const struct inode *inode; 323 - 324 - /* 325 - * The ext4 journal (jbd2) can submit a buffer_head it directly created 326 - * for a non-pagecache page. fscrypt doesn't care about these. 327 - */ 328 - mapping = folio_mapping(folio); 329 - if (!mapping) 330 - return false; 331 - inode = mapping->host; 332 - 333 - *inode_ret = inode; 334 - *lblk_num_ret = (folio_pos(folio) + bh_offset(bh)) >> inode->i_blkbits; 335 - return true; 336 - } 337 - 338 - /** 339 - * fscrypt_set_bio_crypt_ctx_bh() - prepare a file contents bio for inline 340 - * crypto 341 - * @bio: a bio which will eventually be submitted to the file 342 - * @first_bh: the first buffer_head for which I/O will be submitted 343 - * @gfp_mask: memory allocation flags 344 - * 345 - * Same as fscrypt_set_bio_crypt_ctx(), except this takes a buffer_head instead 346 - * of an inode and block number directly. 347 - */ 348 - void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio, 349 - const struct buffer_head *first_bh, 350 - gfp_t gfp_mask) 351 - { 352 - const struct inode *inode; 353 - u64 first_lblk; 354 - 355 - if (bh_get_inode_and_lblk_num(first_bh, &inode, &first_lblk)) 356 - fscrypt_set_bio_crypt_ctx(bio, inode, first_lblk, gfp_mask); 357 - } 358 - EXPORT_SYMBOL_GPL(fscrypt_set_bio_crypt_ctx_bh); 359 316 360 317 /** 361 318 * fscrypt_mergeable_bio() - test whether data can be added to a bio 362 319 * @bio: the bio being built up 363 320 * @inode: the inode for the next part of the I/O 364 - * @next_lblk: the next file logical block number in the I/O 321 + * @pos: the next file position (in bytes) in the I/O 365 322 * 366 323 * When building a bio which may contain data which should undergo inline 367 324 * encryption (or decryption) via fscrypt, filesystems should call this function ··· 334 381 * Return: true iff the I/O is mergeable 335 382 */ 336 383 bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode, 337 - u64 next_lblk) 384 + loff_t pos) 338 385 { 339 386 const struct bio_crypt_ctx *bc = bio->bi_crypt_context; 340 387 const struct fscrypt_inode_info *ci; ··· 354 401 if (bc->bc_key != ci->ci_enc_key.blk_key) 355 402 return false; 356 403 357 - fscrypt_generate_dun(ci, next_lblk, next_dun); 404 + fscrypt_generate_dun(ci, pos, next_dun); 358 405 return bio_crypt_dun_is_contiguous(bc, bio->bi_iter.bi_size, next_dun); 359 406 } 360 407 EXPORT_SYMBOL_GPL(fscrypt_mergeable_bio); 361 - 362 - /** 363 - * fscrypt_mergeable_bio_bh() - test whether data can be added to a bio 364 - * @bio: the bio being built up 365 - * @next_bh: the next buffer_head for which I/O will be submitted 366 - * 367 - * Same as fscrypt_mergeable_bio(), except this takes a buffer_head instead of 368 - * an inode and block number directly. 369 - * 370 - * Return: true iff the I/O is mergeable 371 - */ 372 - bool fscrypt_mergeable_bio_bh(struct bio *bio, 373 - const struct buffer_head *next_bh) 374 - { 375 - const struct inode *inode; 376 - u64 next_lblk; 377 - 378 - if (!bh_get_inode_and_lblk_num(next_bh, &inode, &next_lblk)) 379 - return !bio->bi_crypt_context; 380 - 381 - return fscrypt_mergeable_bio(bio, inode, next_lblk); 382 - } 383 - EXPORT_SYMBOL_GPL(fscrypt_mergeable_bio_bh); 384 408 385 409 /** 386 410 * fscrypt_dio_supported() - check whether DIO (direct I/O) is supported on an
-2
fs/crypto/keysetup.c
··· 609 609 610 610 crypt_info->ci_data_unit_bits = 611 611 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; 614 612 615 613 res = setup_file_encryption_key(crypt_info, need_dirhash_key, &mk); 616 614 if (res)
+28 -59
fs/crypto/keysetup_v1.c
··· 20 20 * managed alongside the master keys in the filesystem-level keyring) 21 21 */ 22 22 23 - #include <crypto/skcipher.h> 23 + #include <crypto/aes.h> 24 24 #include <crypto/utils.h> 25 25 #include <keys/user-type.h> 26 26 #include <linux/hashtable.h> 27 - #include <linux/scatterlist.h> 28 27 29 28 #include "fscrypt_private.h" 30 29 31 30 /* Table of keys referenced by DIRECT_KEY policies */ 32 31 static DEFINE_HASHTABLE(fscrypt_direct_keys, 6); /* 6 bits = 64 buckets */ 33 32 static DEFINE_SPINLOCK(fscrypt_direct_keys_lock); 34 - 35 - /* 36 - * v1 key derivation function. This generates the derived key by encrypting the 37 - * master key with AES-128-ECB using the nonce as the AES key. This provides a 38 - * unique derived key with sufficient entropy for each inode. However, it's 39 - * nonstandard, non-extensible, doesn't evenly distribute the entropy from the 40 - * master key, and is trivially reversible: an attacker who compromises a 41 - * derived key can "decrypt" it to get back to the master key, then derive any 42 - * other key. For all new code, use HKDF instead. 43 - * 44 - * The master key must be at least as long as the derived key. If the master 45 - * key is longer, then only the first 'derived_keysize' bytes are used. 46 - */ 47 - static int derive_key_aes(const u8 *master_key, 48 - const u8 nonce[FSCRYPT_FILE_NONCE_SIZE], 49 - u8 *derived_key, unsigned int derived_keysize) 50 - { 51 - struct crypto_sync_skcipher *tfm; 52 - int err; 53 - 54 - tfm = crypto_alloc_sync_skcipher("ecb(aes)", 0, FSCRYPT_CRYPTOAPI_MASK); 55 - if (IS_ERR(tfm)) 56 - return PTR_ERR(tfm); 57 - 58 - err = crypto_sync_skcipher_setkey(tfm, nonce, FSCRYPT_FILE_NONCE_SIZE); 59 - if (err == 0) { 60 - SYNC_SKCIPHER_REQUEST_ON_STACK(req, tfm); 61 - struct scatterlist src_sg, dst_sg; 62 - 63 - skcipher_request_set_callback(req, 64 - CRYPTO_TFM_REQ_MAY_BACKLOG | 65 - CRYPTO_TFM_REQ_MAY_SLEEP, 66 - NULL, NULL); 67 - sg_init_one(&src_sg, master_key, derived_keysize); 68 - sg_init_one(&dst_sg, derived_key, derived_keysize); 69 - skcipher_request_set_crypt(req, &src_sg, &dst_sg, 70 - derived_keysize, NULL); 71 - err = crypto_skcipher_encrypt(req); 72 - } 73 - crypto_free_sync_skcipher(tfm); 74 - return err; 75 - } 76 33 77 34 /* 78 35 * Search the current task's subscribed keyrings for a "logon" key with ··· 212 255 return 0; 213 256 } 214 257 215 - /* v1 policy, !DIRECT_KEY: derive the file's encryption key */ 258 + /* 259 + * v1 policy, !DIRECT_KEY: derive the file's encryption key. 260 + * 261 + * The v1 key derivation function generates the derived key by encrypting the 262 + * master key with AES-128-ECB using the file's nonce as the AES key. This 263 + * provides a unique derived key with sufficient entropy for each inode. 264 + * However, it's nonstandard, non-extensible, doesn't evenly distribute the 265 + * entropy from the master key, and is trivially reversible: an attacker who 266 + * compromises a derived key can "decrypt" it to get back to the master key, 267 + * then derive any other key. For all new code, use HKDF instead. 268 + * 269 + * The master key must be at least as long as the derived key. If the master 270 + * key is longer, then only the first ci->ci_mode->keysize bytes are used. 271 + */ 216 272 static int setup_v1_file_key_derived(struct fscrypt_inode_info *ci, 217 273 const u8 *raw_master_key) 218 274 { 219 - u8 *derived_key; 275 + const unsigned int derived_keysize = ci->ci_mode->keysize; 276 + u8 derived_key[FSCRYPT_MAX_RAW_KEY_SIZE]; 277 + struct aes_enckey aes; 220 278 int err; 221 279 222 - /* 223 - * This cannot be a stack buffer because it will be passed to the 224 - * scatterlist crypto API during derive_key_aes(). 225 - */ 226 - derived_key = kmalloc(ci->ci_mode->keysize, GFP_KERNEL); 227 - if (!derived_key) 228 - return -ENOMEM; 280 + if (WARN_ON_ONCE(derived_keysize > FSCRYPT_MAX_RAW_KEY_SIZE || 281 + derived_keysize % AES_BLOCK_SIZE != 0)) 282 + return -EINVAL; 229 283 230 - err = derive_key_aes(raw_master_key, ci->ci_nonce, 231 - derived_key, ci->ci_mode->keysize); 232 - if (err) 233 - goto out; 284 + static_assert(FSCRYPT_FILE_NONCE_SIZE == AES_KEYSIZE_128); 285 + aes_prepareenckey(&aes, ci->ci_nonce, FSCRYPT_FILE_NONCE_SIZE); 286 + for (unsigned int i = 0; i < derived_keysize; i += AES_BLOCK_SIZE) 287 + aes_encrypt(&aes, &derived_key[i], &raw_master_key[i]); 234 288 235 289 err = fscrypt_set_per_file_enc_key(ci, derived_key); 236 - out: 237 - kfree_sensitive(derived_key); 290 + 291 + memzero_explicit(derived_key, derived_keysize); 292 + /* No need to zeroize 'aes', as its key is not secret. */ 238 293 return err; 239 294 } 240 295
+4 -1
fs/ext4/inode.c
··· 417 417 KUNIT_STATIC_STUB_REDIRECT(ext4_issue_zeroout, inode, lblk, pblk, len); 418 418 419 419 if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) 420 - return fscrypt_zeroout_range(inode, lblk, pblk, len); 420 + return fscrypt_zeroout_range(inode, 421 + (loff_t)lblk << inode->i_blkbits, 422 + pblk << (inode->i_blkbits - SECTOR_SHIFT), 423 + (u64)len << inode->i_blkbits); 421 424 422 425 ret = sb_issue_zeroout(inode->i_sb, pblk, len, GFP_NOFS); 423 426 if (ret > 0)
+21 -7
fs/ext4/page-io.c
··· 416 416 } 417 417 418 418 static void io_submit_init_bio(struct ext4_io_submit *io, 419 + struct inode *inode, 420 + struct folio *folio, 419 421 struct buffer_head *bh) 420 422 { 421 423 struct bio *bio; ··· 427 425 * __GFP_DIRECT_RECLAIM is set, see comments for bio_alloc_bioset(). 428 426 */ 429 427 bio = bio_alloc(bh->b_bdev, BIO_MAX_VECS, REQ_OP_WRITE, GFP_NOIO); 430 - fscrypt_set_bio_crypt_ctx_bh(bio, bh, GFP_NOIO); 428 + fscrypt_set_bio_crypt_ctx(bio, inode, folio_pos(folio) + bh_offset(bh), 429 + GFP_NOIO); 431 430 bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9); 432 431 bio->bi_end_io = ext4_end_bio; 433 432 bio->bi_private = ext4_get_io_end(io->io_end); 433 + bio->bi_write_hint = inode->i_write_hint; 434 434 io->io_bio = bio; 435 435 io->io_next_block = bh->b_blocknr; 436 436 wbc_init_bio(io->io_wbc, bio); 437 + } 438 + 439 + static bool io_submit_need_new_bio(struct ext4_io_submit *io, 440 + struct inode *inode, 441 + struct folio *folio, 442 + struct buffer_head *bh) 443 + { 444 + if (bh->b_blocknr != io->io_next_block) 445 + return true; 446 + if (!fscrypt_mergeable_bio(io->io_bio, inode, 447 + folio_pos(folio) + bh_offset(bh))) 448 + return true; 449 + return false; 437 450 } 438 451 439 452 static void io_submit_add_bh(struct ext4_io_submit *io, ··· 457 440 struct folio *io_folio, 458 441 struct buffer_head *bh) 459 442 { 460 - if (io->io_bio && (bh->b_blocknr != io->io_next_block || 461 - !fscrypt_mergeable_bio_bh(io->io_bio, bh))) { 443 + if (io->io_bio && io_submit_need_new_bio(io, inode, folio, bh)) { 462 444 submit_and_retry: 463 445 ext4_io_submit(io); 464 446 } 465 - if (io->io_bio == NULL) { 466 - io_submit_init_bio(io, bh); 467 - io->io_bio->bi_write_hint = inode->i_write_hint; 468 - } 447 + if (io->io_bio == NULL) 448 + io_submit_init_bio(io, inode, folio, bh); 469 449 if (!bio_add_folio(io->io_bio, io_folio, bh->b_size, bh_offset(bh))) 470 450 goto submit_and_retry; 471 451 wbc_account_cgroup_owner(io->io_wbc, folio, bh->b_size);
+5 -5
fs/ext4/readpage.c
··· 215 215 sector_t last_block_in_bio = 0; 216 216 const unsigned blkbits = inode->i_blkbits; 217 217 const unsigned blocksize = 1 << blkbits; 218 - sector_t next_block; 219 218 sector_t block_in_file; 220 219 sector_t last_block; 221 220 sector_t last_block_in_file; 222 221 sector_t first_block; 222 + loff_t pos; 223 223 unsigned page_block; 224 224 struct block_device *bdev = inode->i_sb->s_bdev; 225 225 int length; ··· 249 249 250 250 blocks_per_folio = folio_size(folio) >> blkbits; 251 251 first_hole = blocks_per_folio; 252 - block_in_file = next_block = EXT4_PG_TO_LBLK(inode, folio->index); 252 + pos = folio_pos(folio); 253 + block_in_file = pos >> blkbits; 253 254 last_block = EXT4_PG_TO_LBLK(inode, folio->index + nr_pages); 254 255 last_block_in_file = (ext4_readpage_limit(inode) + 255 256 blocksize - 1) >> blkbits; ··· 343 342 * BIO off first? 344 343 */ 345 344 if (bio && (last_block_in_bio != first_block - 1 || 346 - !fscrypt_mergeable_bio(bio, inode, next_block))) { 345 + !fscrypt_mergeable_bio(bio, inode, pos))) { 347 346 submit_and_realloc: 348 347 blk_crypto_submit_bio(bio); 349 348 bio = NULL; ··· 355 354 */ 356 355 bio = bio_alloc(bdev, bio_max_segs(nr_pages), 357 356 REQ_OP_READ, GFP_KERNEL); 358 - fscrypt_set_bio_crypt_ctx(bio, inode, next_block, 359 - GFP_KERNEL); 357 + fscrypt_set_bio_crypt_ctx(bio, inode, pos, GFP_KERNEL); 360 358 ext4_set_bio_post_read_ctx(bio, inode, vi); 361 359 bio->bi_iter.bi_sector = first_block << (blkbits - 9); 362 360 bio->bi_end_io = mpage_end_io;
+5 -2
fs/f2fs/data.c
··· 527 527 * read/write raw data without encryption. 528 528 */ 529 529 if (!fio || !fio->encrypted_page) 530 - fscrypt_set_bio_crypt_ctx(bio, inode, first_idx, gfp_mask); 530 + fscrypt_set_bio_crypt_ctx(bio, inode, 531 + (loff_t)first_idx << inode->i_blkbits, 532 + gfp_mask); 531 533 } 532 534 533 535 static bool f2fs_crypt_mergeable_bio(struct bio *bio, const struct inode *inode, ··· 543 541 if (fio && fio->encrypted_page) 544 542 return !bio_has_crypt_ctx(bio); 545 543 546 - return fscrypt_mergeable_bio(bio, inode, next_idx); 544 + return fscrypt_mergeable_bio(bio, inode, 545 + (loff_t)next_idx << inode->i_blkbits); 547 546 } 548 547 549 548 void f2fs_submit_read_bio(struct f2fs_sb_info *sbi, struct bio *bio,
+3 -1
fs/f2fs/file.c
··· 4162 4162 4163 4163 if (!ret && (flags & F2FS_TRIM_FILE_ZEROOUT)) { 4164 4164 if (IS_ENCRYPTED(inode)) 4165 - ret = fscrypt_zeroout_range(inode, off, block, len); 4165 + ret = fscrypt_zeroout_range(inode, 4166 + (loff_t)off << inode->i_blkbits, sector, 4167 + (u64)len << inode->i_blkbits); 4166 4168 else 4167 4169 ret = blkdev_issue_zeroout(bdev, sector, nr_sects, 4168 4170 GFP_NOFS, 0);
+2 -4
fs/iomap/direct-io.c
··· 315 315 316 316 bio = iomap_dio_alloc_bio(iter, dio, nr_vecs, 317 317 REQ_OP_WRITE | REQ_SYNC | REQ_IDLE); 318 - fscrypt_set_bio_crypt_ctx(bio, inode, pos >> inode->i_blkbits, 319 - GFP_KERNEL); 318 + fscrypt_set_bio_crypt_ctx(bio, inode, pos, GFP_KERNEL); 320 319 bio->bi_iter.bi_sector = iomap_sector(&iter->iomap, pos); 321 320 bio->bi_private = dio; 322 321 bio->bi_end_io = iomap_dio_bio_end_io; ··· 345 346 nr_vecs = bio_iov_vecs_to_alloc(dio->submit.iter, BIO_MAX_VECS); 346 347 347 348 bio = iomap_dio_alloc_bio(iter, dio, nr_vecs, op); 348 - fscrypt_set_bio_crypt_ctx(bio, iter->inode, 349 - pos >> iter->inode->i_blkbits, GFP_KERNEL); 349 + fscrypt_set_bio_crypt_ctx(bio, iter->inode, pos, GFP_KERNEL); 350 350 bio->bi_iter.bi_sector = iomap_sector(&iter->iomap, pos); 351 351 bio->bi_write_hint = iter->inode->i_write_hint; 352 352 bio->bi_ioprio = dio->iocb->ki_ioprio;
+9 -28
include/linux/fscrypt.h
··· 450 450 451 451 /* bio.c */ 452 452 bool fscrypt_decrypt_bio(struct bio *bio); 453 - int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk, 454 - sector_t pblk, unsigned int len); 453 + int fscrypt_zeroout_range(const struct inode *inode, loff_t pos, 454 + sector_t sector, u64 len); 455 455 456 456 /* hooks.c */ 457 457 int fscrypt_file_open(struct inode *inode, struct file *filp); ··· 755 755 return true; 756 756 } 757 757 758 - static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk, 759 - sector_t pblk, unsigned int len) 758 + static inline int fscrypt_zeroout_range(const struct inode *inode, loff_t pos, 759 + sector_t sector, u64 len) 760 760 { 761 761 return -EOPNOTSUPP; 762 762 } ··· 865 865 866 866 bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode); 867 867 868 - void fscrypt_set_bio_crypt_ctx(struct bio *bio, 869 - const struct inode *inode, u64 first_lblk, 870 - gfp_t gfp_mask); 871 - 872 - void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio, 873 - const struct buffer_head *first_bh, 874 - gfp_t gfp_mask); 868 + void fscrypt_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode, 869 + loff_t pos, gfp_t gfp_mask); 875 870 876 871 bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode, 877 - u64 next_lblk); 878 - 879 - bool fscrypt_mergeable_bio_bh(struct bio *bio, 880 - const struct buffer_head *next_bh); 872 + loff_t pos); 881 873 882 874 bool fscrypt_dio_supported(struct inode *inode); 883 875 ··· 884 892 885 893 static inline void fscrypt_set_bio_crypt_ctx(struct bio *bio, 886 894 const struct inode *inode, 887 - u64 first_lblk, gfp_t gfp_mask) { } 888 - 889 - static inline void fscrypt_set_bio_crypt_ctx_bh( 890 - struct bio *bio, 891 - const struct buffer_head *first_bh, 892 - gfp_t gfp_mask) { } 895 + loff_t pos, gfp_t gfp_mask) { } 893 896 894 897 static inline bool fscrypt_mergeable_bio(struct bio *bio, 895 898 const struct inode *inode, 896 - u64 next_lblk) 897 - { 898 - return true; 899 - } 900 - 901 - static inline bool fscrypt_mergeable_bio_bh(struct bio *bio, 902 - const struct buffer_head *next_bh) 899 + loff_t pos) 903 900 { 904 901 return true; 905 902 }