1 // SPDX-License-Identifier: GPL-2.0
3 * This contains encryption functions for per-file encryption.
5 * Copyright (C) 2015, Google, Inc.
6 * Copyright (C) 2015, Motorola Mobility
8 * Written by Michael Halcrow, 2014.
10 * Filename encryption additions
11 * Uday Savagaonkar, 2014
12 * Encryption policy handling additions
13 * Ildar Muslukhov, 2014
14 * Add fscrypt_pullback_bio_page()
17 * This has not yet undergone a rigorous security audit.
19 * The usage of AES-XTS should conform to recommendations in NIST
20 * Special Publication 800-38E and IEEE P1619/D16.
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"
30 * Call fscrypt_decrypt_page on every single page, reusing the encryption
33 static void completion_pages(struct work_struct
*work
)
35 struct fscrypt_ctx
*ctx
=
36 container_of(work
, struct fscrypt_ctx
, r
.work
);
37 struct bio
*bio
= ctx
->r
.bio
;
41 bio_for_each_segment_all(bv
, bio
, i
) {
42 struct page
*page
= bv
->bv_page
;
43 int ret
= fscrypt_decrypt_page(page
->mapping
->host
, page
,
44 PAGE_SIZE
, 0, page
->index
);
50 SetPageUptodate(page
);
54 fscrypt_release_ctx(ctx
);
58 void fscrypt_decrypt_bio_pages(struct fscrypt_ctx
*ctx
, struct bio
*bio
)
60 INIT_WORK(&ctx
->r
.work
, completion_pages
);
62 queue_work(fscrypt_read_workqueue
, &ctx
->r
.work
);
64 EXPORT_SYMBOL(fscrypt_decrypt_bio_pages
);
66 void fscrypt_pullback_bio_page(struct page
**page
, bool restore
)
68 struct fscrypt_ctx
*ctx
;
69 struct page
*bounce_page
;
71 /* The bounce data pages are unmapped. */
75 /* The bounce data page is unmapped. */
77 ctx
= (struct fscrypt_ctx
*)page_private(bounce_page
);
79 /* restore control page */
80 *page
= ctx
->w
.control_page
;
83 fscrypt_restore_control_page(bounce_page
);
85 EXPORT_SYMBOL(fscrypt_pullback_bio_page
);
87 int fscrypt_zeroout_range(const struct inode
*inode
, pgoff_t lblk
,
88 sector_t pblk
, unsigned int len
)
90 struct fscrypt_ctx
*ctx
;
91 struct page
*ciphertext_page
= NULL
;
95 BUG_ON(inode
->i_sb
->s_blocksize
!= PAGE_SIZE
);
97 ctx
= fscrypt_get_ctx(inode
, GFP_NOFS
);
101 ciphertext_page
= fscrypt_alloc_bounce_page(ctx
, GFP_NOWAIT
);
102 if (IS_ERR(ciphertext_page
)) {
103 err
= PTR_ERR(ciphertext_page
);
108 err
= fscrypt_do_page_crypto(inode
, FS_ENCRYPT
, lblk
,
109 ZERO_PAGE(0), ciphertext_page
,
110 PAGE_SIZE
, 0, GFP_NOFS
);
114 bio
= bio_alloc(GFP_NOWAIT
, 1);
119 bio_set_dev(bio
, inode
->i_sb
->s_bdev
);
120 bio
->bi_iter
.bi_sector
=
121 pblk
<< (inode
->i_sb
->s_blocksize_bits
- 9);
122 bio_set_op_attrs(bio
, REQ_OP_WRITE
, 0);
123 ret
= bio_add_page(bio
, ciphertext_page
,
124 inode
->i_sb
->s_blocksize
, 0);
125 if (ret
!= inode
->i_sb
->s_blocksize
) {
126 /* should never happen! */
132 err
= submit_bio_wait(bio
);
133 if (err
== 0 && bio
->bi_status
)
143 fscrypt_release_ctx(ctx
);
146 EXPORT_SYMBOL(fscrypt_zeroout_range
);