Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Utility functions for file contents encryption/decryption on
0004  * block device-based filesystems.
0005  *
0006  * Copyright (C) 2015, Google, Inc.
0007  * Copyright (C) 2015, Motorola Mobility
0008  */
0009 
0010 #include <linux/pagemap.h>
0011 #include <linux/module.h>
0012 #include <linux/bio.h>
0013 #include <linux/namei.h>
0014 #include "fscrypt_private.h"
0015 
0016 /**
0017  * fscrypt_decrypt_bio() - decrypt the contents of a bio
0018  * @bio: the bio to decrypt
0019  *
0020  * Decrypt the contents of a "read" bio following successful completion of the
0021  * underlying disk read.  The bio must be reading a whole number of blocks of an
0022  * encrypted file directly into the page cache.  If the bio is reading the
0023  * ciphertext into bounce pages instead of the page cache (for example, because
0024  * the file is also compressed, so decompression is required after decryption),
0025  * then this function isn't applicable.  This function may sleep, so it must be
0026  * called from a workqueue rather than from the bio's bi_end_io callback.
0027  *
0028  * This function sets PG_error on any pages that contain any blocks that failed
0029  * to be decrypted.  The filesystem must not mark such pages uptodate.
0030  */
0031 void fscrypt_decrypt_bio(struct bio *bio)
0032 {
0033     struct bio_vec *bv;
0034     struct bvec_iter_all iter_all;
0035 
0036     bio_for_each_segment_all(bv, bio, iter_all) {
0037         struct page *page = bv->bv_page;
0038         int ret = fscrypt_decrypt_pagecache_blocks(page, bv->bv_len,
0039                                bv->bv_offset);
0040         if (ret)
0041             SetPageError(page);
0042     }
0043 }
0044 EXPORT_SYMBOL(fscrypt_decrypt_bio);
0045 
0046 static int fscrypt_zeroout_range_inline_crypt(const struct inode *inode,
0047                           pgoff_t lblk, sector_t pblk,
0048                           unsigned int len)
0049 {
0050     const unsigned int blockbits = inode->i_blkbits;
0051     const unsigned int blocks_per_page = 1 << (PAGE_SHIFT - blockbits);
0052     struct bio *bio;
0053     int ret, err = 0;
0054     int num_pages = 0;
0055 
0056     /* This always succeeds since __GFP_DIRECT_RECLAIM is set. */
0057     bio = bio_alloc(inode->i_sb->s_bdev, BIO_MAX_VECS, REQ_OP_WRITE,
0058             GFP_NOFS);
0059 
0060     while (len) {
0061         unsigned int blocks_this_page = min(len, blocks_per_page);
0062         unsigned int bytes_this_page = blocks_this_page << blockbits;
0063 
0064         if (num_pages == 0) {
0065             fscrypt_set_bio_crypt_ctx(bio, inode, lblk, GFP_NOFS);
0066             bio->bi_iter.bi_sector =
0067                     pblk << (blockbits - SECTOR_SHIFT);
0068         }
0069         ret = bio_add_page(bio, ZERO_PAGE(0), bytes_this_page, 0);
0070         if (WARN_ON(ret != bytes_this_page)) {
0071             err = -EIO;
0072             goto out;
0073         }
0074         num_pages++;
0075         len -= blocks_this_page;
0076         lblk += blocks_this_page;
0077         pblk += blocks_this_page;
0078         if (num_pages == BIO_MAX_VECS || !len ||
0079             !fscrypt_mergeable_bio(bio, inode, lblk)) {
0080             err = submit_bio_wait(bio);
0081             if (err)
0082                 goto out;
0083             bio_reset(bio, inode->i_sb->s_bdev, REQ_OP_WRITE);
0084             num_pages = 0;
0085         }
0086     }
0087 out:
0088     bio_put(bio);
0089     return err;
0090 }
0091 
0092 /**
0093  * fscrypt_zeroout_range() - zero out a range of blocks in an encrypted file
0094  * @inode: the file's inode
0095  * @lblk: the first file logical block to zero out
0096  * @pblk: the first filesystem physical block to zero out
0097  * @len: number of blocks to zero out
0098  *
0099  * Zero out filesystem blocks in an encrypted regular file on-disk, i.e. write
0100  * ciphertext blocks which decrypt to the all-zeroes block.  The blocks must be
0101  * both logically and physically contiguous.  It's also assumed that the
0102  * filesystem only uses a single block device, ->s_bdev.
0103  *
0104  * Note that since each block uses a different IV, this involves writing a
0105  * different ciphertext to each block; we can't simply reuse the same one.
0106  *
0107  * Return: 0 on success; -errno on failure.
0108  */
0109 int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
0110               sector_t pblk, unsigned int len)
0111 {
0112     const unsigned int blockbits = inode->i_blkbits;
0113     const unsigned int blocksize = 1 << blockbits;
0114     const unsigned int blocks_per_page_bits = PAGE_SHIFT - blockbits;
0115     const unsigned int blocks_per_page = 1 << blocks_per_page_bits;
0116     struct page *pages[16]; /* write up to 16 pages at a time */
0117     unsigned int nr_pages;
0118     unsigned int i;
0119     unsigned int offset;
0120     struct bio *bio;
0121     int ret, err;
0122 
0123     if (len == 0)
0124         return 0;
0125 
0126     if (fscrypt_inode_uses_inline_crypto(inode))
0127         return fscrypt_zeroout_range_inline_crypt(inode, lblk, pblk,
0128                               len);
0129 
0130     BUILD_BUG_ON(ARRAY_SIZE(pages) > BIO_MAX_VECS);
0131     nr_pages = min_t(unsigned int, ARRAY_SIZE(pages),
0132              (len + blocks_per_page - 1) >> blocks_per_page_bits);
0133 
0134     /*
0135      * We need at least one page for ciphertext.  Allocate the first one
0136      * from a mempool, with __GFP_DIRECT_RECLAIM set so that it can't fail.
0137      *
0138      * Any additional page allocations are allowed to fail, as they only
0139      * help performance, and waiting on the mempool for them could deadlock.
0140      */
0141     for (i = 0; i < nr_pages; i++) {
0142         pages[i] = fscrypt_alloc_bounce_page(i == 0 ? GFP_NOFS :
0143                              GFP_NOWAIT | __GFP_NOWARN);
0144         if (!pages[i])
0145             break;
0146     }
0147     nr_pages = i;
0148     if (WARN_ON(nr_pages <= 0))
0149         return -EINVAL;
0150 
0151     /* This always succeeds since __GFP_DIRECT_RECLAIM is set. */
0152     bio = bio_alloc(inode->i_sb->s_bdev, nr_pages, REQ_OP_WRITE, GFP_NOFS);
0153 
0154     do {
0155         bio->bi_iter.bi_sector = pblk << (blockbits - 9);
0156 
0157         i = 0;
0158         offset = 0;
0159         do {
0160             err = fscrypt_crypt_block(inode, FS_ENCRYPT, lblk,
0161                           ZERO_PAGE(0), pages[i],
0162                           blocksize, offset, GFP_NOFS);
0163             if (err)
0164                 goto out;
0165             lblk++;
0166             pblk++;
0167             len--;
0168             offset += blocksize;
0169             if (offset == PAGE_SIZE || len == 0) {
0170                 ret = bio_add_page(bio, pages[i++], offset, 0);
0171                 if (WARN_ON(ret != offset)) {
0172                     err = -EIO;
0173                     goto out;
0174                 }
0175                 offset = 0;
0176             }
0177         } while (i != nr_pages && len != 0);
0178 
0179         err = submit_bio_wait(bio);
0180         if (err)
0181             goto out;
0182         bio_reset(bio, inode->i_sb->s_bdev, REQ_OP_WRITE);
0183     } while (len != 0);
0184     err = 0;
0185 out:
0186     bio_put(bio);
0187     for (i = 0; i < nr_pages; i++)
0188         fscrypt_free_bounce_page(pages[i]);
0189     return err;
0190 }
0191 EXPORT_SYMBOL(fscrypt_zeroout_range);