]> asedeno.scripts.mit.edu Git - linux.git/blob - fs/crypto/bio.c
Linux 5.6-rc7
[linux.git] / fs / crypto / bio.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * This contains encryption functions for per-file encryption.
4  *
5  * Copyright (C) 2015, Google, Inc.
6  * Copyright (C) 2015, Motorola Mobility
7  *
8  * Written by Michael Halcrow, 2014.
9  *
10  * Filename encryption additions
11  *      Uday Savagaonkar, 2014
12  * Encryption policy handling additions
13  *      Ildar Muslukhov, 2014
14  * Add fscrypt_pullback_bio_page()
15  *      Jaegeuk Kim, 2015.
16  *
17  * This has not yet undergone a rigorous security audit.
18  *
19  * The usage of AES-XTS should conform to recommendations in NIST
20  * Special Publication 800-38E and IEEE P1619/D16.
21  */
22
23 #include <linux/pagemap.h>
24 #include <linux/module.h>
25 #include <linux/bio.h>
26 #include <linux/namei.h>
27 #include "fscrypt_private.h"
28
29 void fscrypt_decrypt_bio(struct bio *bio)
30 {
31         struct bio_vec *bv;
32         struct bvec_iter_all iter_all;
33
34         bio_for_each_segment_all(bv, bio, iter_all) {
35                 struct page *page = bv->bv_page;
36                 int ret = fscrypt_decrypt_pagecache_blocks(page, bv->bv_len,
37                                                            bv->bv_offset);
38                 if (ret)
39                         SetPageError(page);
40         }
41 }
42 EXPORT_SYMBOL(fscrypt_decrypt_bio);
43
44 /**
45  * fscrypt_zeroout_range() - zero out a range of blocks in an encrypted file
46  * @inode: the file's inode
47  * @lblk: the first file logical block to zero out
48  * @pblk: the first filesystem physical block to zero out
49  * @len: number of blocks to zero out
50  *
51  * Zero out filesystem blocks in an encrypted regular file on-disk, i.e. write
52  * ciphertext blocks which decrypt to the all-zeroes block.  The blocks must be
53  * both logically and physically contiguous.  It's also assumed that the
54  * filesystem only uses a single block device, ->s_bdev.
55  *
56  * Note that since each block uses a different IV, this involves writing a
57  * different ciphertext to each block; we can't simply reuse the same one.
58  *
59  * Return: 0 on success; -errno on failure.
60  */
61 int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
62                           sector_t pblk, unsigned int len)
63 {
64         const unsigned int blockbits = inode->i_blkbits;
65         const unsigned int blocksize = 1 << blockbits;
66         const unsigned int blocks_per_page_bits = PAGE_SHIFT - blockbits;
67         const unsigned int blocks_per_page = 1 << blocks_per_page_bits;
68         struct page *pages[16]; /* write up to 16 pages at a time */
69         unsigned int nr_pages;
70         unsigned int i;
71         unsigned int offset;
72         struct bio *bio;
73         int ret, err;
74
75         if (len == 0)
76                 return 0;
77
78         BUILD_BUG_ON(ARRAY_SIZE(pages) > BIO_MAX_PAGES);
79         nr_pages = min_t(unsigned int, ARRAY_SIZE(pages),
80                          (len + blocks_per_page - 1) >> blocks_per_page_bits);
81
82         /*
83          * We need at least one page for ciphertext.  Allocate the first one
84          * from a mempool, with __GFP_DIRECT_RECLAIM set so that it can't fail.
85          *
86          * Any additional page allocations are allowed to fail, as they only
87          * help performance, and waiting on the mempool for them could deadlock.
88          */
89         for (i = 0; i < nr_pages; i++) {
90                 pages[i] = fscrypt_alloc_bounce_page(i == 0 ? GFP_NOFS :
91                                                      GFP_NOWAIT | __GFP_NOWARN);
92                 if (!pages[i])
93                         break;
94         }
95         nr_pages = i;
96         if (WARN_ON(nr_pages <= 0))
97                 return -EINVAL;
98
99         /* This always succeeds since __GFP_DIRECT_RECLAIM is set. */
100         bio = bio_alloc(GFP_NOFS, nr_pages);
101
102         do {
103                 bio_set_dev(bio, inode->i_sb->s_bdev);
104                 bio->bi_iter.bi_sector = pblk << (blockbits - 9);
105                 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
106
107                 i = 0;
108                 offset = 0;
109                 do {
110                         err = fscrypt_crypt_block(inode, FS_ENCRYPT, lblk,
111                                                   ZERO_PAGE(0), pages[i],
112                                                   blocksize, offset, GFP_NOFS);
113                         if (err)
114                                 goto out;
115                         lblk++;
116                         pblk++;
117                         len--;
118                         offset += blocksize;
119                         if (offset == PAGE_SIZE || len == 0) {
120                                 ret = bio_add_page(bio, pages[i++], offset, 0);
121                                 if (WARN_ON(ret != offset)) {
122                                         err = -EIO;
123                                         goto out;
124                                 }
125                                 offset = 0;
126                         }
127                 } while (i != nr_pages && len != 0);
128
129                 err = submit_bio_wait(bio);
130                 if (err)
131                         goto out;
132                 bio_reset(bio);
133         } while (len != 0);
134         err = 0;
135 out:
136         bio_put(bio);
137         for (i = 0; i < nr_pages; i++)
138                 fscrypt_free_bounce_page(pages[i]);
139         return err;
140 }
141 EXPORT_SYMBOL(fscrypt_zeroout_range);