1*1944540cSEric Biggers // SPDX-License-Identifier: GPL-2.0 2*1944540cSEric Biggers /* 3*1944540cSEric Biggers * File contents en/decryption on block-based filesystems 4*1944540cSEric Biggers * 5*1944540cSEric Biggers * Copyright 2019 Google LLC 6*1944540cSEric Biggers */ 7*1944540cSEric Biggers 8*1944540cSEric Biggers /* 9*1944540cSEric Biggers * This file implements fscrypt's file contents en/decryption using blk-crypto 10*1944540cSEric Biggers * (Documentation/block/inline-encryption.rst). fscrypt assigns a bio_crypt_ctx 11*1944540cSEric Biggers * with a key and IV to each bio, and the block layer does the en/decryption. 12*1944540cSEric Biggers * 13*1944540cSEric Biggers * This file's exported functions are called only by block-based filesystems. 14*1944540cSEric Biggers */ 15*1944540cSEric Biggers 16*1944540cSEric Biggers #include <linux/blk-crypto.h> 17*1944540cSEric Biggers #include <linux/blkdev.h> 18*1944540cSEric Biggers #include <linux/export.h> 19*1944540cSEric Biggers #include <linux/sched/mm.h> 20*1944540cSEric Biggers #include <linux/slab.h> 21*1944540cSEric Biggers #include <linux/uio.h> 22*1944540cSEric Biggers 23*1944540cSEric Biggers #include "fscrypt_private.h" 24*1944540cSEric Biggers 25*1944540cSEric Biggers static unsigned int 26*1944540cSEric Biggers fscrypt_get_devices(struct super_block *sb, 27*1944540cSEric Biggers struct block_device *devs[FSCRYPT_MAX_DEVICES]) 28*1944540cSEric Biggers { 29*1944540cSEric Biggers if (sb->s_cop->get_devices) 30*1944540cSEric Biggers return sb->s_cop->get_devices(sb, devs); 31*1944540cSEric Biggers devs[0] = sb->s_bdev; 32*1944540cSEric Biggers return 1; 33*1944540cSEric Biggers } 34*1944540cSEric Biggers 35*1944540cSEric Biggers static unsigned int fscrypt_get_dun_bytes(const struct fscrypt_inode_info *ci) 36*1944540cSEric Biggers { 37*1944540cSEric Biggers const struct super_block *sb = ci->ci_inode->i_sb; 38*1944540cSEric Biggers unsigned int flags = fscrypt_policy_flags(&ci->ci_policy); 39*1944540cSEric Biggers int dun_bits; 40*1944540cSEric Biggers 41*1944540cSEric Biggers if (flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) 42*1944540cSEric Biggers return offsetofend(union fscrypt_iv, nonce); 43*1944540cSEric Biggers 44*1944540cSEric Biggers if (flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) 45*1944540cSEric Biggers return sizeof(__le64); 46*1944540cSEric Biggers 47*1944540cSEric Biggers if (flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) 48*1944540cSEric Biggers return sizeof(__le32); 49*1944540cSEric Biggers 50*1944540cSEric Biggers /* Default case: IVs are just the file data unit index */ 51*1944540cSEric Biggers dun_bits = fscrypt_max_file_dun_bits(sb, ci->ci_data_unit_bits); 52*1944540cSEric Biggers return DIV_ROUND_UP(dun_bits, 8); 53*1944540cSEric Biggers } 54*1944540cSEric Biggers 55*1944540cSEric Biggers /* 56*1944540cSEric Biggers * Log a message when starting to use blk-crypto (native) or blk-crypto-fallback 57*1944540cSEric Biggers * for an encryption mode for the first time. This is the blk-crypto 58*1944540cSEric Biggers * counterpart to the message logged when starting to use the crypto API for the 59*1944540cSEric Biggers * first time. A limitation is that these messages don't convey which specific 60*1944540cSEric Biggers * filesystems or files are using each implementation. However, *usually* 61*1944540cSEric Biggers * systems use just one implementation per mode, which makes these messages 62*1944540cSEric Biggers * helpful for debugging problems where the "wrong" implementation is used. 63*1944540cSEric Biggers */ 64*1944540cSEric Biggers static void fscrypt_log_blk_crypto_impl(struct fscrypt_mode *mode, 65*1944540cSEric Biggers struct block_device *dev, 66*1944540cSEric Biggers const struct blk_crypto_key *blk_key) 67*1944540cSEric Biggers { 68*1944540cSEric Biggers if (blk_crypto_config_supported_natively(dev, &blk_key->crypto_cfg)) { 69*1944540cSEric Biggers if (!xchg(&mode->logged_blk_crypto_native, 1)) 70*1944540cSEric Biggers pr_info("fscrypt: %s using blk-crypto (native)\n", 71*1944540cSEric Biggers mode->friendly_name); 72*1944540cSEric Biggers } else if (!xchg(&mode->logged_blk_crypto_fallback, 1)) { 73*1944540cSEric Biggers pr_info("fscrypt: %s using blk-crypto-fallback\n", 74*1944540cSEric Biggers mode->friendly_name); 75*1944540cSEric Biggers } 76*1944540cSEric Biggers } 77*1944540cSEric Biggers 78*1944540cSEric Biggers int fscrypt_prepare_inline_crypt_key(struct fscrypt_prepared_key *prep_key, 79*1944540cSEric Biggers const u8 *key_bytes, size_t key_size, 80*1944540cSEric Biggers bool is_hw_wrapped, 81*1944540cSEric Biggers const struct fscrypt_inode_info *ci) 82*1944540cSEric Biggers { 83*1944540cSEric Biggers const struct inode *inode = ci->ci_inode; 84*1944540cSEric Biggers struct super_block *sb = inode->i_sb; 85*1944540cSEric Biggers bool inlinecrypt = sb->s_flags & SB_INLINECRYPT; 86*1944540cSEric Biggers struct fscrypt_mode *mode = ci->ci_mode; 87*1944540cSEric Biggers enum blk_crypto_key_type key_type = is_hw_wrapped ? 88*1944540cSEric Biggers BLK_CRYPTO_KEY_TYPE_HW_WRAPPED : BLK_CRYPTO_KEY_TYPE_RAW; 89*1944540cSEric Biggers struct blk_crypto_key *blk_key; 90*1944540cSEric Biggers struct block_device *devs[FSCRYPT_MAX_DEVICES]; 91*1944540cSEric Biggers unsigned int num_devs; 92*1944540cSEric Biggers unsigned int i; 93*1944540cSEric Biggers int err; 94*1944540cSEric Biggers 95*1944540cSEric Biggers if (is_hw_wrapped && !inlinecrypt) { 96*1944540cSEric Biggers /* 97*1944540cSEric Biggers * blk_crypto_init_key() would catch this anyway, but this 98*1944540cSEric Biggers * provides a clearer error message. 99*1944540cSEric Biggers */ 100*1944540cSEric Biggers fscrypt_err( 101*1944540cSEric Biggers inode, 102*1944540cSEric Biggers "Hardware-wrapped keys require inline encryption (-o inlinecrypt)"); 103*1944540cSEric Biggers return -EINVAL; 104*1944540cSEric Biggers } 105*1944540cSEric Biggers 106*1944540cSEric Biggers blk_key = kmalloc_obj(*blk_key); 107*1944540cSEric Biggers if (!blk_key) 108*1944540cSEric Biggers return -ENOMEM; 109*1944540cSEric Biggers 110*1944540cSEric Biggers err = blk_crypto_init_key(blk_key, key_bytes, key_size, key_type, 111*1944540cSEric Biggers mode->blk_crypto_mode, 112*1944540cSEric Biggers fscrypt_get_dun_bytes(ci), 113*1944540cSEric Biggers 1U << ci->ci_data_unit_bits, 114*1944540cSEric Biggers inlinecrypt ? BLK_CRYPTO_CFG_ALLOW_HW : 0); 115*1944540cSEric Biggers if (err) { 116*1944540cSEric Biggers fscrypt_err(inode, "Error %d initializing blk-crypto key", err); 117*1944540cSEric Biggers goto fail; 118*1944540cSEric Biggers } 119*1944540cSEric Biggers 120*1944540cSEric Biggers /* Start using blk-crypto on all the filesystem's block devices. */ 121*1944540cSEric Biggers num_devs = fscrypt_get_devices(sb, devs); 122*1944540cSEric Biggers for (i = 0; i < num_devs; i++) { 123*1944540cSEric Biggers err = blk_crypto_start_using_key(devs[i], blk_key); 124*1944540cSEric Biggers if (err) 125*1944540cSEric Biggers break; 126*1944540cSEric Biggers fscrypt_log_blk_crypto_impl(mode, devs[i], blk_key); 127*1944540cSEric Biggers } 128*1944540cSEric Biggers if (err) { 129*1944540cSEric Biggers if (err == -EOPNOTSUPP && is_hw_wrapped) 130*1944540cSEric Biggers fscrypt_err( 131*1944540cSEric Biggers inode, 132*1944540cSEric Biggers "Hardware-wrapped key required, but no suitable inline encryption capabilities are available"); 133*1944540cSEric Biggers else 134*1944540cSEric Biggers fscrypt_err(inode, 135*1944540cSEric Biggers "Error %d starting to use blk-crypto", err); 136*1944540cSEric Biggers goto fail; 137*1944540cSEric Biggers } 138*1944540cSEric Biggers 139*1944540cSEric Biggers prep_key->blk_key = blk_key; 140*1944540cSEric Biggers return 0; 141*1944540cSEric Biggers 142*1944540cSEric Biggers fail: 143*1944540cSEric Biggers kfree_sensitive(blk_key); 144*1944540cSEric Biggers return err; 145*1944540cSEric Biggers } 146*1944540cSEric Biggers 147*1944540cSEric Biggers void fscrypt_destroy_inline_crypt_key(struct super_block *sb, 148*1944540cSEric Biggers struct fscrypt_prepared_key *prep_key) 149*1944540cSEric Biggers { 150*1944540cSEric Biggers struct blk_crypto_key *blk_key = prep_key->blk_key; 151*1944540cSEric Biggers struct block_device *devs[FSCRYPT_MAX_DEVICES]; 152*1944540cSEric Biggers unsigned int num_devs; 153*1944540cSEric Biggers unsigned int i; 154*1944540cSEric Biggers 155*1944540cSEric Biggers if (!blk_key) 156*1944540cSEric Biggers return; 157*1944540cSEric Biggers 158*1944540cSEric Biggers /* 159*1944540cSEric Biggers * Evict the key from all the filesystem's block devices. 160*1944540cSEric Biggers * This *must* be done before the key is freed. 161*1944540cSEric Biggers */ 162*1944540cSEric Biggers num_devs = fscrypt_get_devices(sb, devs); 163*1944540cSEric Biggers for (i = 0; i < num_devs; i++) 164*1944540cSEric Biggers blk_crypto_evict_key(devs[i], blk_key); 165*1944540cSEric Biggers 166*1944540cSEric Biggers kfree_sensitive(blk_key); 167*1944540cSEric Biggers } 168*1944540cSEric Biggers 169*1944540cSEric Biggers /* 170*1944540cSEric Biggers * Ask the inline encryption hardware to derive the software secret from a 171*1944540cSEric Biggers * hardware-wrapped key. Returns -EOPNOTSUPP if hardware-wrapped keys aren't 172*1944540cSEric Biggers * supported on this filesystem or hardware. 173*1944540cSEric Biggers */ 174*1944540cSEric Biggers int fscrypt_derive_sw_secret(struct super_block *sb, 175*1944540cSEric Biggers const u8 *wrapped_key, size_t wrapped_key_size, 176*1944540cSEric Biggers u8 sw_secret[BLK_CRYPTO_SW_SECRET_SIZE]) 177*1944540cSEric Biggers { 178*1944540cSEric Biggers int err; 179*1944540cSEric Biggers 180*1944540cSEric Biggers /* The filesystem must be mounted with -o inlinecrypt. */ 181*1944540cSEric Biggers if (!(sb->s_flags & SB_INLINECRYPT)) { 182*1944540cSEric Biggers fscrypt_warn(NULL, 183*1944540cSEric Biggers "%s: filesystem not mounted with inlinecrypt\n", 184*1944540cSEric Biggers sb->s_id); 185*1944540cSEric Biggers return -EOPNOTSUPP; 186*1944540cSEric Biggers } 187*1944540cSEric Biggers 188*1944540cSEric Biggers err = blk_crypto_derive_sw_secret(sb->s_bdev, wrapped_key, 189*1944540cSEric Biggers wrapped_key_size, sw_secret); 190*1944540cSEric Biggers if (err == -EOPNOTSUPP) 191*1944540cSEric Biggers fscrypt_warn(NULL, 192*1944540cSEric Biggers "%s: block device doesn't support hardware-wrapped keys\n", 193*1944540cSEric Biggers sb->s_id); 194*1944540cSEric Biggers return err; 195*1944540cSEric Biggers } 196*1944540cSEric Biggers 197*1944540cSEric Biggers static void fscrypt_generate_dun(const struct fscrypt_inode_info *ci, 198*1944540cSEric Biggers loff_t pos, u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE]) 199*1944540cSEric Biggers { 200*1944540cSEric Biggers union fscrypt_iv iv; 201*1944540cSEric Biggers int i; 202*1944540cSEric Biggers 203*1944540cSEric Biggers fscrypt_generate_iv(&iv, pos >> ci->ci_data_unit_bits, ci); 204*1944540cSEric Biggers 205*1944540cSEric Biggers BUILD_BUG_ON(FSCRYPT_MAX_IV_SIZE > BLK_CRYPTO_MAX_IV_SIZE); 206*1944540cSEric Biggers memset(dun, 0, BLK_CRYPTO_MAX_IV_SIZE); 207*1944540cSEric Biggers for (i = 0; i < ci->ci_mode->ivsize/sizeof(dun[0]); i++) 208*1944540cSEric Biggers dun[i] = le64_to_cpu(iv.dun[i]); 209*1944540cSEric Biggers } 210*1944540cSEric Biggers 211*1944540cSEric Biggers /** 212*1944540cSEric Biggers * fscrypt_set_bio_crypt_ctx() - prepare a file contents bio for inline crypto 213*1944540cSEric Biggers * @bio: a bio which will eventually be submitted to the file 214*1944540cSEric Biggers * @inode: the file's inode 215*1944540cSEric Biggers * @pos: the first file position (in bytes) in the I/O 216*1944540cSEric Biggers * @gfp_mask: memory allocation flags - these must be a waiting mask so that 217*1944540cSEric Biggers * bio_crypt_set_ctx can't fail. 218*1944540cSEric Biggers * 219*1944540cSEric Biggers * If the contents of the file should be encrypted (or decrypted), then assign 220*1944540cSEric Biggers * the appropriate encryption context to the bio. 221*1944540cSEric Biggers * 222*1944540cSEric Biggers * Normally the bio should be newly allocated (i.e. no pages added yet), as 223*1944540cSEric Biggers * otherwise fscrypt_mergeable_bio() won't work as intended. 224*1944540cSEric Biggers * 225*1944540cSEric Biggers * The encryption context will be freed automatically when the bio is freed. 226*1944540cSEric Biggers */ 227*1944540cSEric Biggers void fscrypt_set_bio_crypt_ctx(struct bio *bio, const struct inode *inode, 228*1944540cSEric Biggers loff_t pos, gfp_t gfp_mask) 229*1944540cSEric Biggers { 230*1944540cSEric Biggers const struct fscrypt_inode_info *ci; 231*1944540cSEric Biggers u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE]; 232*1944540cSEric Biggers 233*1944540cSEric Biggers if (!fscrypt_needs_contents_encryption(inode)) 234*1944540cSEric Biggers return; 235*1944540cSEric Biggers ci = fscrypt_get_inode_info_raw(inode); 236*1944540cSEric Biggers 237*1944540cSEric Biggers fscrypt_generate_dun(ci, pos, dun); 238*1944540cSEric Biggers bio_crypt_set_ctx(bio, ci->ci_enc_key.blk_key, dun, gfp_mask); 239*1944540cSEric Biggers } 240*1944540cSEric Biggers EXPORT_SYMBOL_GPL(fscrypt_set_bio_crypt_ctx); 241*1944540cSEric Biggers 242*1944540cSEric Biggers /** 243*1944540cSEric Biggers * fscrypt_mergeable_bio() - test whether data can be added to a bio 244*1944540cSEric Biggers * @bio: the bio being built up 245*1944540cSEric Biggers * @inode: the inode for the next part of the I/O 246*1944540cSEric Biggers * @pos: the next file position (in bytes) in the I/O 247*1944540cSEric Biggers * 248*1944540cSEric Biggers * When building a bio which may contain data which should undergo encryption 249*1944540cSEric Biggers * (or decryption) via fscrypt, filesystems should call this function to ensure 250*1944540cSEric Biggers * that the resulting bio contains only contiguous data unit numbers. This will 251*1944540cSEric Biggers * return false if the next part of the I/O cannot be merged with the bio 252*1944540cSEric Biggers * because either the encryption key would be different or the encryption data 253*1944540cSEric Biggers * unit numbers would be discontiguous. 254*1944540cSEric Biggers * 255*1944540cSEric Biggers * fscrypt_set_bio_crypt_ctx() must have already been called on the bio. 256*1944540cSEric Biggers * 257*1944540cSEric Biggers * This function isn't required in cases where crypto-mergeability is ensured in 258*1944540cSEric Biggers * another way, such as I/O targeting only a single file (and thus a single key) 259*1944540cSEric Biggers * combined with fscrypt_limit_io_blocks() to ensure DUN contiguity. 260*1944540cSEric Biggers * 261*1944540cSEric Biggers * Return: true iff the I/O is mergeable 262*1944540cSEric Biggers */ 263*1944540cSEric Biggers bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode, 264*1944540cSEric Biggers loff_t pos) 265*1944540cSEric Biggers { 266*1944540cSEric Biggers const struct bio_crypt_ctx *bc = bio->bi_crypt_context; 267*1944540cSEric Biggers const struct fscrypt_inode_info *ci; 268*1944540cSEric Biggers u64 next_dun[BLK_CRYPTO_DUN_ARRAY_SIZE]; 269*1944540cSEric Biggers 270*1944540cSEric Biggers if (!!bc != fscrypt_needs_contents_encryption(inode)) 271*1944540cSEric Biggers return false; 272*1944540cSEric Biggers if (!bc) 273*1944540cSEric Biggers return true; 274*1944540cSEric Biggers ci = fscrypt_get_inode_info_raw(inode); 275*1944540cSEric Biggers 276*1944540cSEric Biggers /* 277*1944540cSEric Biggers * Comparing the key pointers is good enough, as all I/O for each key 278*1944540cSEric Biggers * uses the same pointer. I.e., there's currently no need to support 279*1944540cSEric Biggers * merging requests where the keys are the same but the pointers differ. 280*1944540cSEric Biggers */ 281*1944540cSEric Biggers if (bc->bc_key != ci->ci_enc_key.blk_key) 282*1944540cSEric Biggers return false; 283*1944540cSEric Biggers 284*1944540cSEric Biggers fscrypt_generate_dun(ci, pos, next_dun); 285*1944540cSEric Biggers return bio_crypt_dun_is_contiguous(bc, bio->bi_iter.bi_size, next_dun); 286*1944540cSEric Biggers } 287*1944540cSEric Biggers EXPORT_SYMBOL_GPL(fscrypt_mergeable_bio); 288*1944540cSEric Biggers 289*1944540cSEric Biggers /** 290*1944540cSEric Biggers * fscrypt_limit_io_blocks() - limit I/O blocks to avoid discontiguous DUNs 291*1944540cSEric Biggers * @inode: the file on which I/O is being done 292*1944540cSEric Biggers * @lblk: the block at which the I/O is being started from 293*1944540cSEric Biggers * @nr_blocks: the number of blocks we want to submit starting at @lblk 294*1944540cSEric Biggers * 295*1944540cSEric Biggers * Determine the limit to the number of blocks that can be submitted in a bio 296*1944540cSEric Biggers * targeting @lblk without causing a data unit number (DUN) discontiguity. 297*1944540cSEric Biggers * 298*1944540cSEric Biggers * This is normally just @nr_blocks, as normally the DUNs just increment along 299*1944540cSEric Biggers * with the logical blocks. (Or the file is not encrypted.) 300*1944540cSEric Biggers * 301*1944540cSEric Biggers * In rare cases, fscrypt can be using an IV generation method that allows the 302*1944540cSEric Biggers * DUN to wrap around within logically contiguous blocks, and that wraparound 303*1944540cSEric Biggers * will occur. If this happens, a value less than @nr_blocks will be returned 304*1944540cSEric Biggers * so that the wraparound doesn't occur in the middle of a bio, which would 305*1944540cSEric Biggers * cause encryption/decryption to produce wrong results. 306*1944540cSEric Biggers * 307*1944540cSEric Biggers * Return: the actual number of blocks that can be submitted 308*1944540cSEric Biggers */ 309*1944540cSEric Biggers u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk, u64 nr_blocks) 310*1944540cSEric Biggers { 311*1944540cSEric Biggers const struct fscrypt_inode_info *ci; 312*1944540cSEric Biggers u32 dun; 313*1944540cSEric Biggers 314*1944540cSEric Biggers if (!fscrypt_needs_contents_encryption(inode)) 315*1944540cSEric Biggers return nr_blocks; 316*1944540cSEric Biggers 317*1944540cSEric Biggers if (nr_blocks <= 1) 318*1944540cSEric Biggers return nr_blocks; 319*1944540cSEric Biggers 320*1944540cSEric Biggers ci = fscrypt_get_inode_info_raw(inode); 321*1944540cSEric Biggers if (!(fscrypt_policy_flags(&ci->ci_policy) & 322*1944540cSEric Biggers FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) 323*1944540cSEric Biggers return nr_blocks; 324*1944540cSEric Biggers 325*1944540cSEric Biggers /* With IV_INO_LBLK_32, the DUN can wrap around from U32_MAX to 0. */ 326*1944540cSEric Biggers 327*1944540cSEric Biggers dun = ci->ci_hashed_ino + lblk; 328*1944540cSEric Biggers 329*1944540cSEric Biggers return min_t(u64, nr_blocks, (u64)U32_MAX + 1 - dun); 330*1944540cSEric Biggers } 331*1944540cSEric Biggers EXPORT_SYMBOL_GPL(fscrypt_limit_io_blocks); 332*1944540cSEric Biggers 333*1944540cSEric Biggers struct fscrypt_zero_done { 334*1944540cSEric Biggers atomic_t pending; 335*1944540cSEric Biggers blk_status_t status; 336*1944540cSEric Biggers struct completion done; 337*1944540cSEric Biggers }; 338*1944540cSEric Biggers 339*1944540cSEric Biggers static void fscrypt_zeroout_range_done(struct fscrypt_zero_done *done) 340*1944540cSEric Biggers { 341*1944540cSEric Biggers if (atomic_dec_and_test(&done->pending)) 342*1944540cSEric Biggers complete(&done->done); 343*1944540cSEric Biggers } 344*1944540cSEric Biggers 345*1944540cSEric Biggers static void fscrypt_zeroout_range_end_io(struct bio *bio) 346*1944540cSEric Biggers { 347*1944540cSEric Biggers struct fscrypt_zero_done *done = bio->bi_private; 348*1944540cSEric Biggers 349*1944540cSEric Biggers if (bio->bi_status) 350*1944540cSEric Biggers cmpxchg(&done->status, 0, bio->bi_status); 351*1944540cSEric Biggers fscrypt_zeroout_range_done(done); 352*1944540cSEric Biggers bio_put(bio); 353*1944540cSEric Biggers } 354*1944540cSEric Biggers 355*1944540cSEric Biggers /** 356*1944540cSEric Biggers * fscrypt_zeroout_range() - zero out a range of blocks in an encrypted file 357*1944540cSEric Biggers * @inode: the file's inode 358*1944540cSEric Biggers * @pos: the first file position (in bytes) to zero out 359*1944540cSEric Biggers * @sector: the first sector to zero out 360*1944540cSEric Biggers * @len: bytes to zero out 361*1944540cSEric Biggers * 362*1944540cSEric Biggers * Zero out filesystem blocks in an encrypted regular file on-disk, i.e. write 363*1944540cSEric Biggers * ciphertext blocks which decrypt to the all-zeroes block. The blocks must be 364*1944540cSEric Biggers * both logically and physically contiguous. It's also assumed that the 365*1944540cSEric Biggers * filesystem only uses a single block device, ->s_bdev. @len must be a 366*1944540cSEric Biggers * multiple of the file system logical block size. 367*1944540cSEric Biggers * 368*1944540cSEric Biggers * Note that since each block uses a different IV, this involves writing a 369*1944540cSEric Biggers * different ciphertext to each block; we can't simply reuse the same one. 370*1944540cSEric Biggers * 371*1944540cSEric Biggers * Return: 0 on success; -errno on failure. 372*1944540cSEric Biggers */ 373*1944540cSEric Biggers int fscrypt_zeroout_range(const struct inode *inode, loff_t pos, 374*1944540cSEric Biggers sector_t sector, u64 len) 375*1944540cSEric Biggers { 376*1944540cSEric Biggers struct fscrypt_zero_done done = { 377*1944540cSEric Biggers .pending = ATOMIC_INIT(1), 378*1944540cSEric Biggers .done = COMPLETION_INITIALIZER_ONSTACK(done.done), 379*1944540cSEric Biggers }; 380*1944540cSEric Biggers 381*1944540cSEric Biggers if (len == 0) 382*1944540cSEric Biggers return 0; 383*1944540cSEric Biggers 384*1944540cSEric Biggers do { 385*1944540cSEric Biggers struct bio *bio; 386*1944540cSEric Biggers unsigned int n; 387*1944540cSEric Biggers 388*1944540cSEric Biggers bio = bio_alloc(inode->i_sb->s_bdev, BIO_MAX_VECS, REQ_OP_WRITE, 389*1944540cSEric Biggers GFP_NOFS); 390*1944540cSEric Biggers bio->bi_iter.bi_sector = sector; 391*1944540cSEric Biggers bio->bi_private = &done; 392*1944540cSEric Biggers bio->bi_end_io = fscrypt_zeroout_range_end_io; 393*1944540cSEric Biggers fscrypt_set_bio_crypt_ctx(bio, inode, pos, GFP_NOFS); 394*1944540cSEric Biggers 395*1944540cSEric Biggers for (n = 0; n < BIO_MAX_VECS; n++) { 396*1944540cSEric Biggers unsigned int bytes_this_page = min(len, PAGE_SIZE); 397*1944540cSEric Biggers 398*1944540cSEric Biggers __bio_add_page(bio, ZERO_PAGE(0), bytes_this_page, 0); 399*1944540cSEric Biggers len -= bytes_this_page; 400*1944540cSEric Biggers pos += bytes_this_page; 401*1944540cSEric Biggers sector += (bytes_this_page >> SECTOR_SHIFT); 402*1944540cSEric Biggers if (!len || !fscrypt_mergeable_bio(bio, inode, pos)) 403*1944540cSEric Biggers break; 404*1944540cSEric Biggers } 405*1944540cSEric Biggers 406*1944540cSEric Biggers atomic_inc(&done.pending); 407*1944540cSEric Biggers blk_crypto_submit_bio(bio); 408*1944540cSEric Biggers } while (len); 409*1944540cSEric Biggers 410*1944540cSEric Biggers fscrypt_zeroout_range_done(&done); 411*1944540cSEric Biggers 412*1944540cSEric Biggers wait_for_completion(&done.done); 413*1944540cSEric Biggers return blk_status_to_errno(done.status); 414*1944540cSEric Biggers } 415*1944540cSEric Biggers EXPORT_SYMBOL(fscrypt_zeroout_range); 416