blob: f82c45ac285a5579c643a4a5f754627bcbf8c17a [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07002/*
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
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070023#include <linux/pagemap.h>
24#include <linux/mempool.h>
25#include <linux/module.h>
26#include <linux/scatterlist.h>
27#include <linux/ratelimit.h>
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070028#include <linux/dcache.h>
Jaegeuk Kim03a8bb02016-04-12 16:05:36 -070029#include <linux/namei.h>
Daniel Walterb7e7cf72017-06-19 09:27:58 +020030#include <crypto/aes.h>
Eric Biggersa5757842018-01-05 10:45:00 -080031#include <crypto/skcipher.h>
Theodore Ts'occ4e0df2016-11-26 22:05:18 -050032#include "fscrypt_private.h"
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070033
34static unsigned int num_prealloc_crypto_pages = 32;
35static unsigned int num_prealloc_crypto_ctxs = 128;
36
37module_param(num_prealloc_crypto_pages, uint, 0444);
38MODULE_PARM_DESC(num_prealloc_crypto_pages,
39 "Number of crypto pages to preallocate");
40module_param(num_prealloc_crypto_ctxs, uint, 0444);
41MODULE_PARM_DESC(num_prealloc_crypto_ctxs,
42 "Number of crypto contexts to preallocate");
43
44static mempool_t *fscrypt_bounce_page_pool = NULL;
45
46static LIST_HEAD(fscrypt_free_ctxs);
47static DEFINE_SPINLOCK(fscrypt_ctx_lock);
48
Eric Biggers0cb8dae2018-04-18 11:09:47 -070049static struct workqueue_struct *fscrypt_read_workqueue;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070050static DEFINE_MUTEX(fscrypt_init_mutex);
51
52static struct kmem_cache *fscrypt_ctx_cachep;
53struct kmem_cache *fscrypt_info_cachep;
54
Eric Biggers0cb8dae2018-04-18 11:09:47 -070055void fscrypt_enqueue_decrypt_work(struct work_struct *work)
56{
57 queue_work(fscrypt_read_workqueue, work);
58}
59EXPORT_SYMBOL(fscrypt_enqueue_decrypt_work);
60
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070061/**
Eric Biggers2a415a02019-05-20 09:29:40 -070062 * fscrypt_release_ctx() - Release a decryption context
63 * @ctx: The decryption context to release.
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070064 *
Eric Biggers2a415a02019-05-20 09:29:40 -070065 * If the decryption context was allocated from the pre-allocated pool, return
66 * it to that pool. Else, free it.
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070067 */
68void fscrypt_release_ctx(struct fscrypt_ctx *ctx)
69{
70 unsigned long flags;
71
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070072 if (ctx->flags & FS_CTX_REQUIRES_FREE_ENCRYPT_FL) {
73 kmem_cache_free(fscrypt_ctx_cachep, ctx);
74 } else {
75 spin_lock_irqsave(&fscrypt_ctx_lock, flags);
76 list_add(&ctx->free_list, &fscrypt_free_ctxs);
77 spin_unlock_irqrestore(&fscrypt_ctx_lock, flags);
78 }
79}
80EXPORT_SYMBOL(fscrypt_release_ctx);
81
82/**
Eric Biggers2a415a02019-05-20 09:29:40 -070083 * fscrypt_get_ctx() - Get a decryption context
Jaegeuk Kimb32e44822016-04-11 15:51:57 -070084 * @gfp_flags: The gfp flag for memory allocation
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070085 *
Eric Biggers2a415a02019-05-20 09:29:40 -070086 * Allocate and initialize a decryption context.
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070087 *
Eric Biggers2a415a02019-05-20 09:29:40 -070088 * Return: A new decryption context on success; an ERR_PTR() otherwise.
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070089 */
Eric Biggerscd0265f2019-03-18 10:23:33 -070090struct fscrypt_ctx *fscrypt_get_ctx(gfp_t gfp_flags)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070091{
Eric Biggerscd0265f2019-03-18 10:23:33 -070092 struct fscrypt_ctx *ctx;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070093 unsigned long flags;
94
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070095 /*
Eric Biggersd2d07272019-05-20 09:29:39 -070096 * First try getting a ctx from the free list so that we don't have to
97 * call into the slab allocator.
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070098 */
99 spin_lock_irqsave(&fscrypt_ctx_lock, flags);
100 ctx = list_first_entry_or_null(&fscrypt_free_ctxs,
101 struct fscrypt_ctx, free_list);
102 if (ctx)
103 list_del(&ctx->free_list);
104 spin_unlock_irqrestore(&fscrypt_ctx_lock, flags);
105 if (!ctx) {
Jaegeuk Kimb32e44822016-04-11 15:51:57 -0700106 ctx = kmem_cache_zalloc(fscrypt_ctx_cachep, gfp_flags);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700107 if (!ctx)
108 return ERR_PTR(-ENOMEM);
109 ctx->flags |= FS_CTX_REQUIRES_FREE_ENCRYPT_FL;
110 } else {
111 ctx->flags &= ~FS_CTX_REQUIRES_FREE_ENCRYPT_FL;
112 }
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700113 return ctx;
114}
115EXPORT_SYMBOL(fscrypt_get_ctx);
116
Eric Biggersd2d07272019-05-20 09:29:39 -0700117struct page *fscrypt_alloc_bounce_page(gfp_t gfp_flags)
118{
119 return mempool_alloc(fscrypt_bounce_page_pool, gfp_flags);
120}
121
122/**
123 * fscrypt_free_bounce_page() - free a ciphertext bounce page
124 *
Eric Biggers53bc1d852019-05-20 09:29:44 -0700125 * Free a bounce page that was allocated by fscrypt_encrypt_pagecache_blocks(),
126 * or by fscrypt_alloc_bounce_page() directly.
Eric Biggersd2d07272019-05-20 09:29:39 -0700127 */
128void fscrypt_free_bounce_page(struct page *bounce_page)
129{
130 if (!bounce_page)
131 return;
132 set_page_private(bounce_page, (unsigned long)NULL);
133 ClearPagePrivate(bounce_page);
134 mempool_free(bounce_page, fscrypt_bounce_page_pool);
135}
136EXPORT_SYMBOL(fscrypt_free_bounce_page);
137
Eric Biggers8094c3c2019-01-06 08:36:21 -0500138void fscrypt_generate_iv(union fscrypt_iv *iv, u64 lblk_num,
139 const struct fscrypt_info *ci)
140{
141 memset(iv, 0, ci->ci_mode->ivsize);
142 iv->lblk_num = cpu_to_le64(lblk_num);
143
144 if (ci->ci_flags & FS_POLICY_FLAG_DIRECT_KEY)
145 memcpy(iv->nonce, ci->ci_nonce, FS_KEY_DERIVATION_NONCE_SIZE);
146
147 if (ci->ci_essiv_tfm != NULL)
148 crypto_cipher_encrypt_one(ci->ci_essiv_tfm, iv->raw, iv->raw);
149}
150
Eric Biggersf47fcbb2019-05-20 09:29:41 -0700151/* Encrypt or decrypt a single filesystem block of file contents */
152int fscrypt_crypt_block(const struct inode *inode, fscrypt_direction_t rw,
153 u64 lblk_num, struct page *src_page,
154 struct page *dest_page, unsigned int len,
155 unsigned int offs, gfp_t gfp_flags)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700156{
Eric Biggers8094c3c2019-01-06 08:36:21 -0500157 union fscrypt_iv iv;
Linus Torvaldsd4075742016-03-21 11:03:02 -0700158 struct skcipher_request *req = NULL;
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +0100159 DECLARE_CRYPTO_WAIT(wait);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700160 struct scatterlist dst, src;
161 struct fscrypt_info *ci = inode->i_crypt_info;
Linus Torvaldsd4075742016-03-21 11:03:02 -0700162 struct crypto_skcipher *tfm = ci->ci_ctfm;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700163 int res = 0;
164
Eric Biggerseeacfdc2019-05-20 09:29:42 -0700165 if (WARN_ON_ONCE(len <= 0))
166 return -EINVAL;
167 if (WARN_ON_ONCE(len % FS_CRYPTO_BLOCK_SIZE != 0))
168 return -EINVAL;
David Gstir14004512016-12-06 23:53:55 +0100169
Eric Biggers8094c3c2019-01-06 08:36:21 -0500170 fscrypt_generate_iv(&iv, lblk_num, ci);
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200171
Jaegeuk Kimb32e44822016-04-11 15:51:57 -0700172 req = skcipher_request_alloc(tfm, gfp_flags);
Eric Biggersc90fd7752018-04-30 15:51:38 -0700173 if (!req)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700174 return -ENOMEM;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700175
Linus Torvaldsd4075742016-03-21 11:03:02 -0700176 skcipher_request_set_callback(
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700177 req, CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +0100178 crypto_req_done, &wait);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700179
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700180 sg_init_table(&dst, 1);
David Gstir14004512016-12-06 23:53:55 +0100181 sg_set_page(&dst, dest_page, len, offs);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700182 sg_init_table(&src, 1);
David Gstir14004512016-12-06 23:53:55 +0100183 sg_set_page(&src, src_page, len, offs);
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200184 skcipher_request_set_crypt(req, &src, &dst, len, &iv);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700185 if (rw == FS_DECRYPT)
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +0100186 res = crypto_wait_req(crypto_skcipher_decrypt(req), &wait);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700187 else
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +0100188 res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
Linus Torvaldsd4075742016-03-21 11:03:02 -0700189 skcipher_request_free(req);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700190 if (res) {
Eric Biggers544d08f2018-04-30 15:51:47 -0700191 fscrypt_err(inode->i_sb,
192 "%scryption failed for inode %lu, block %llu: %d",
193 (rw == FS_DECRYPT ? "de" : "en"),
194 inode->i_ino, lblk_num, res);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700195 return res;
196 }
197 return 0;
198}
199
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700200/**
Eric Biggers53bc1d852019-05-20 09:29:44 -0700201 * fscrypt_encrypt_pagecache_blocks() - Encrypt filesystem blocks from a pagecache page
202 * @page: The locked pagecache page containing the block(s) to encrypt
203 * @len: Total size of the block(s) to encrypt. Must be a nonzero
204 * multiple of the filesystem's block size.
205 * @offs: Byte offset within @page of the first block to encrypt. Must be
206 * a multiple of the filesystem's block size.
207 * @gfp_flags: Memory allocation flags
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700208 *
Eric Biggers53bc1d852019-05-20 09:29:44 -0700209 * A new bounce page is allocated, and the specified block(s) are encrypted into
210 * it. In the bounce page, the ciphertext block(s) will be located at the same
211 * offsets at which the plaintext block(s) were located in the source page; any
212 * other parts of the bounce page will be left uninitialized. However, normally
213 * blocksize == PAGE_SIZE and the whole page is encrypted at once.
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700214 *
Eric Biggers53bc1d852019-05-20 09:29:44 -0700215 * This is for use by the filesystem's ->writepages() method.
216 *
217 * Return: the new encrypted bounce page on success; an ERR_PTR() on failure
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700218 */
Eric Biggers53bc1d852019-05-20 09:29:44 -0700219struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
220 unsigned int len,
221 unsigned int offs,
222 gfp_t gfp_flags)
David Gstir7821d4d2016-11-13 22:20:46 +0100223
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700224{
Eric Biggers53bc1d852019-05-20 09:29:44 -0700225 const struct inode *inode = page->mapping->host;
226 const unsigned int blockbits = inode->i_blkbits;
227 const unsigned int blocksize = 1 << blockbits;
Eric Biggers03569f22019-05-20 09:29:43 -0700228 struct page *ciphertext_page;
Eric Biggers53bc1d852019-05-20 09:29:44 -0700229 u64 lblk_num = ((u64)page->index << (PAGE_SHIFT - blockbits)) +
230 (offs >> blockbits);
231 unsigned int i;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700232 int err;
233
Eric Biggerseeacfdc2019-05-20 09:29:42 -0700234 if (WARN_ON_ONCE(!PageLocked(page)))
235 return ERR_PTR(-EINVAL);
David Gstirbd7b8292016-12-06 23:53:56 +0100236
Eric Biggers53bc1d852019-05-20 09:29:44 -0700237 if (WARN_ON_ONCE(len <= 0 || !IS_ALIGNED(len | offs, blocksize)))
238 return ERR_PTR(-EINVAL);
239
Eric Biggersd2d07272019-05-20 09:29:39 -0700240 ciphertext_page = fscrypt_alloc_bounce_page(gfp_flags);
241 if (!ciphertext_page)
242 return ERR_PTR(-ENOMEM);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700243
Eric Biggers53bc1d852019-05-20 09:29:44 -0700244 for (i = offs; i < offs + len; i += blocksize, lblk_num++) {
245 err = fscrypt_crypt_block(inode, FS_ENCRYPT, lblk_num,
246 page, ciphertext_page,
247 blocksize, i, gfp_flags);
248 if (err) {
249 fscrypt_free_bounce_page(ciphertext_page);
250 return ERR_PTR(err);
251 }
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700252 }
David Gstir9e532772016-12-06 23:53:54 +0100253 SetPagePrivate(ciphertext_page);
Eric Biggersd2d07272019-05-20 09:29:39 -0700254 set_page_private(ciphertext_page, (unsigned long)page);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700255 return ciphertext_page;
256}
Eric Biggers53bc1d852019-05-20 09:29:44 -0700257EXPORT_SYMBOL(fscrypt_encrypt_pagecache_blocks);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700258
259/**
Eric Biggers03569f22019-05-20 09:29:43 -0700260 * fscrypt_encrypt_block_inplace() - Encrypt a filesystem block in-place
261 * @inode: The inode to which this block belongs
262 * @page: The page containing the block to encrypt
263 * @len: Size of block to encrypt. Doesn't need to be a multiple of the
264 * fs block size, but must be a multiple of FS_CRYPTO_BLOCK_SIZE.
265 * @offs: Byte offset within @page at which the block to encrypt begins
266 * @lblk_num: Filesystem logical block number of the block, i.e. the 0-based
267 * number of the block within the file
268 * @gfp_flags: Memory allocation flags
269 *
270 * Encrypt a possibly-compressed filesystem block that is located in an
271 * arbitrary page, not necessarily in the original pagecache page. The @inode
272 * and @lblk_num must be specified, as they can't be determined from @page.
273 *
274 * Return: 0 on success; -errno on failure
275 */
276int fscrypt_encrypt_block_inplace(const struct inode *inode, struct page *page,
277 unsigned int len, unsigned int offs,
278 u64 lblk_num, gfp_t gfp_flags)
279{
280 return fscrypt_crypt_block(inode, FS_ENCRYPT, lblk_num, page, page,
281 len, offs, gfp_flags);
282}
283EXPORT_SYMBOL(fscrypt_encrypt_block_inplace);
284
285/**
David Gstir7821d4d2016-11-13 22:20:46 +0100286 * fscrypt_decrypt_page() - Decrypts a page in-place
David Gstir14004512016-12-06 23:53:55 +0100287 * @inode: The corresponding inode for the page to decrypt.
Eric Biggers41adbcb2019-05-20 09:29:46 -0700288 * @page: The page to decrypt. Must be locked.
David Gstir14004512016-12-06 23:53:55 +0100289 * @len: Number of bytes in @page to be decrypted.
290 * @offs: Start of data in @page.
291 * @lblk_num: Logical block number.
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700292 *
293 * Decrypts page in-place using the ctx encryption context.
294 *
295 * Called from the read completion callback.
296 *
297 * Return: Zero on success, non-zero otherwise.
298 */
David Gstir0b93e1b2016-11-13 22:20:47 +0100299int fscrypt_decrypt_page(const struct inode *inode, struct page *page,
David Gstir14004512016-12-06 23:53:55 +0100300 unsigned int len, unsigned int offs, u64 lblk_num)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700301{
Eric Biggers41adbcb2019-05-20 09:29:46 -0700302 if (WARN_ON_ONCE(!PageLocked(page)))
Eric Biggerseeacfdc2019-05-20 09:29:42 -0700303 return -EINVAL;
David Gstirbd7b8292016-12-06 23:53:56 +0100304
Eric Biggersf47fcbb2019-05-20 09:29:41 -0700305 return fscrypt_crypt_block(inode, FS_DECRYPT, lblk_num, page, page,
306 len, offs, GFP_NOFS);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700307}
308EXPORT_SYMBOL(fscrypt_decrypt_page);
309
Eric Biggers41adbcb2019-05-20 09:29:46 -0700310/**
311 * fscrypt_decrypt_block_inplace() - Decrypt a filesystem block in-place
312 * @inode: The inode to which this block belongs
313 * @page: The page containing the block to decrypt
314 * @len: Size of block to decrypt. Doesn't need to be a multiple of the
315 * fs block size, but must be a multiple of FS_CRYPTO_BLOCK_SIZE.
316 * @offs: Byte offset within @page at which the block to decrypt begins
317 * @lblk_num: Filesystem logical block number of the block, i.e. the 0-based
318 * number of the block within the file
319 *
320 * Decrypt a possibly-compressed filesystem block that is located in an
321 * arbitrary page, not necessarily in the original pagecache page. The @inode
322 * and @lblk_num must be specified, as they can't be determined from @page.
323 *
324 * Return: 0 on success; -errno on failure
325 */
326int fscrypt_decrypt_block_inplace(const struct inode *inode, struct page *page,
327 unsigned int len, unsigned int offs,
328 u64 lblk_num)
329{
330 return fscrypt_crypt_block(inode, FS_DECRYPT, lblk_num, page, page,
331 len, offs, GFP_NOFS);
332}
333EXPORT_SYMBOL(fscrypt_decrypt_block_inplace);
334
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700335/*
Eric Biggers6cc24862019-03-20 11:39:09 -0700336 * Validate dentries in encrypted directories to make sure we aren't potentially
337 * caching stale dentries after a key has been added.
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700338 */
339static int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags)
340{
Jaegeuk Kimd7d75352016-04-11 15:10:11 -0700341 struct dentry *dir;
Eric Biggers6cc24862019-03-20 11:39:09 -0700342 int err;
343 int valid;
344
345 /*
346 * Plaintext names are always valid, since fscrypt doesn't support
347 * reverting to ciphertext names without evicting the directory's inode
348 * -- which implies eviction of the dentries in the directory.
349 */
350 if (!(dentry->d_flags & DCACHE_ENCRYPTED_NAME))
351 return 1;
352
353 /*
354 * Ciphertext name; valid if the directory's key is still unavailable.
355 *
356 * Although fscrypt forbids rename() on ciphertext names, we still must
357 * use dget_parent() here rather than use ->d_parent directly. That's
358 * because a corrupted fs image may contain directory hard links, which
359 * the VFS handles by moving the directory's dentry tree in the dcache
360 * each time ->lookup() finds the directory and it already has a dentry
361 * elsewhere. Thus ->d_parent can be changing, and we must safely grab
362 * a reference to some ->d_parent to prevent it from being freed.
363 */
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700364
Jaegeuk Kim03a8bb02016-04-12 16:05:36 -0700365 if (flags & LOOKUP_RCU)
366 return -ECHILD;
367
Jaegeuk Kimd7d75352016-04-11 15:10:11 -0700368 dir = dget_parent(dentry);
Eric Biggers6cc24862019-03-20 11:39:09 -0700369 err = fscrypt_get_encryption_info(d_inode(dir));
370 valid = !fscrypt_has_encryption_key(d_inode(dir));
Jaegeuk Kimd7d75352016-04-11 15:10:11 -0700371 dput(dir);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700372
Eric Biggers6cc24862019-03-20 11:39:09 -0700373 if (err < 0)
374 return err;
375
376 return valid;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700377}
378
379const struct dentry_operations fscrypt_d_ops = {
380 .d_revalidate = fscrypt_d_revalidate,
381};
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700382
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700383static void fscrypt_destroy(void)
384{
385 struct fscrypt_ctx *pos, *n;
386
387 list_for_each_entry_safe(pos, n, &fscrypt_free_ctxs, free_list)
388 kmem_cache_free(fscrypt_ctx_cachep, pos);
389 INIT_LIST_HEAD(&fscrypt_free_ctxs);
390 mempool_destroy(fscrypt_bounce_page_pool);
391 fscrypt_bounce_page_pool = NULL;
392}
393
394/**
395 * fscrypt_initialize() - allocate major buffers for fs encryption.
David Gstirf32d7ac2016-12-06 23:53:57 +0100396 * @cop_flags: fscrypt operations flags
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700397 *
398 * We only call this when we start accessing encrypted files, since it
399 * results in memory getting allocated that wouldn't otherwise be used.
400 *
401 * Return: Zero on success, non-zero otherwise.
402 */
David Gstirf32d7ac2016-12-06 23:53:57 +0100403int fscrypt_initialize(unsigned int cop_flags)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700404{
405 int i, res = -ENOMEM;
406
Eric Biggersa0b3bc82017-10-29 06:30:19 -0400407 /* No need to allocate a bounce page pool if this FS won't use it. */
408 if (cop_flags & FS_CFLG_OWN_PAGES)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700409 return 0;
410
411 mutex_lock(&fscrypt_init_mutex);
412 if (fscrypt_bounce_page_pool)
413 goto already_initialized;
414
415 for (i = 0; i < num_prealloc_crypto_ctxs; i++) {
416 struct fscrypt_ctx *ctx;
417
418 ctx = kmem_cache_zalloc(fscrypt_ctx_cachep, GFP_NOFS);
419 if (!ctx)
420 goto fail;
421 list_add(&ctx->free_list, &fscrypt_free_ctxs);
422 }
423
424 fscrypt_bounce_page_pool =
425 mempool_create_page_pool(num_prealloc_crypto_pages, 0);
426 if (!fscrypt_bounce_page_pool)
427 goto fail;
428
429already_initialized:
430 mutex_unlock(&fscrypt_init_mutex);
431 return 0;
432fail:
433 fscrypt_destroy();
434 mutex_unlock(&fscrypt_init_mutex);
435 return res;
436}
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700437
Eric Biggers544d08f2018-04-30 15:51:47 -0700438void fscrypt_msg(struct super_block *sb, const char *level,
439 const char *fmt, ...)
440{
441 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
442 DEFAULT_RATELIMIT_BURST);
443 struct va_format vaf;
444 va_list args;
445
446 if (!__ratelimit(&rs))
447 return;
448
449 va_start(args, fmt);
450 vaf.fmt = fmt;
451 vaf.va = &args;
452 if (sb)
453 printk("%sfscrypt (%s): %pV\n", level, sb->s_id, &vaf);
454 else
455 printk("%sfscrypt: %pV\n", level, &vaf);
456 va_end(args);
457}
458
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700459/**
460 * fscrypt_init() - Set up for fs encryption.
461 */
462static int __init fscrypt_init(void)
463{
Eric Biggers36dd26e2018-04-20 16:30:02 -0700464 /*
465 * Use an unbound workqueue to allow bios to be decrypted in parallel
466 * even when they happen to complete on the same CPU. This sacrifices
467 * locality, but it's worthwhile since decryption is CPU-intensive.
468 *
469 * Also use a high-priority workqueue to prioritize decryption work,
470 * which blocks reads from completing, over regular application tasks.
471 */
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700472 fscrypt_read_workqueue = alloc_workqueue("fscrypt_read_queue",
Eric Biggers36dd26e2018-04-20 16:30:02 -0700473 WQ_UNBOUND | WQ_HIGHPRI,
474 num_online_cpus());
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700475 if (!fscrypt_read_workqueue)
476 goto fail;
477
478 fscrypt_ctx_cachep = KMEM_CACHE(fscrypt_ctx, SLAB_RECLAIM_ACCOUNT);
479 if (!fscrypt_ctx_cachep)
480 goto fail_free_queue;
481
482 fscrypt_info_cachep = KMEM_CACHE(fscrypt_info, SLAB_RECLAIM_ACCOUNT);
483 if (!fscrypt_info_cachep)
484 goto fail_free_ctx;
485
486 return 0;
487
488fail_free_ctx:
489 kmem_cache_destroy(fscrypt_ctx_cachep);
490fail_free_queue:
491 destroy_workqueue(fscrypt_read_workqueue);
492fail:
493 return -ENOMEM;
494}
495module_init(fscrypt_init)
496
497/**
498 * fscrypt_exit() - Shutdown the fs encryption system
499 */
500static void __exit fscrypt_exit(void)
501{
502 fscrypt_destroy();
503
504 if (fscrypt_read_workqueue)
505 destroy_workqueue(fscrypt_read_workqueue);
506 kmem_cache_destroy(fscrypt_ctx_cachep);
507 kmem_cache_destroy(fscrypt_info_cachep);
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200508
509 fscrypt_essiv_cleanup();
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700510}
511module_exit(fscrypt_exit);
512
513MODULE_LICENSE("GPL");