Revert "blk-cgroup: Flush stats at blkgs destruction path"
[linux-2.6-block.git] / block / blk-crypto-internal.h
CommitLineData
a892c8d5
ST
1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Copyright 2019 Google LLC
4 */
5
6#ifndef __LINUX_BLK_CRYPTO_INTERNAL_H
7#define __LINUX_BLK_CRYPTO_INTERNAL_H
8
9#include <linux/bio.h>
24b83deb 10#include <linux/blk-mq.h>
a892c8d5
ST
11
12/* Represents a crypto mode supported by blk-crypto */
13struct blk_crypto_mode {
20f01f16 14 const char *name; /* name of this mode, shown in sysfs */
488f6682 15 const char *cipher_str; /* crypto API name (for fallback case) */
a892c8d5
ST
16 unsigned int keysize; /* key size in bytes */
17 unsigned int ivsize; /* iv size in bytes */
18};
19
488f6682
ST
20extern const struct blk_crypto_mode blk_crypto_modes[];
21
a892c8d5
ST
22#ifdef CONFIG_BLK_INLINE_ENCRYPTION
23
20f01f16
EB
24int blk_crypto_sysfs_register(struct request_queue *q);
25
26void blk_crypto_sysfs_unregister(struct request_queue *q);
27
a892c8d5
ST
28void bio_crypt_dun_increment(u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE],
29 unsigned int inc);
30
31bool bio_crypt_rq_ctx_compatible(struct request *rq, struct bio *bio);
32
33bool bio_crypt_ctx_mergeable(struct bio_crypt_ctx *bc1, unsigned int bc1_bytes,
34 struct bio_crypt_ctx *bc2);
35
36static inline bool bio_crypt_ctx_back_mergeable(struct request *req,
37 struct bio *bio)
38{
39 return bio_crypt_ctx_mergeable(req->crypt_ctx, blk_rq_bytes(req),
40 bio->bi_crypt_context);
41}
42
43static inline bool bio_crypt_ctx_front_mergeable(struct request *req,
44 struct bio *bio)
45{
46 return bio_crypt_ctx_mergeable(bio->bi_crypt_context,
47 bio->bi_iter.bi_size, req->crypt_ctx);
48}
49
50static inline bool bio_crypt_ctx_merge_rq(struct request *req,
51 struct request *next)
52{
53 return bio_crypt_ctx_mergeable(req->crypt_ctx, blk_rq_bytes(req),
54 next->crypt_ctx);
55}
56
57static inline void blk_crypto_rq_set_defaults(struct request *rq)
58{
59 rq->crypt_ctx = NULL;
60 rq->crypt_keyslot = NULL;
61}
62
63static inline bool blk_crypto_rq_is_encrypted(struct request *rq)
64{
65 return rq->crypt_ctx;
66}
67
3569788c
CH
68blk_status_t blk_crypto_get_keyslot(struct blk_crypto_profile *profile,
69 const struct blk_crypto_key *key,
70 struct blk_crypto_keyslot **slot_ptr);
71
72void blk_crypto_put_keyslot(struct blk_crypto_keyslot *slot);
73
74int __blk_crypto_evict_key(struct blk_crypto_profile *profile,
75 const struct blk_crypto_key *key);
76
77bool __blk_crypto_cfg_supported(struct blk_crypto_profile *profile,
78 const struct blk_crypto_config *cfg);
79
a892c8d5
ST
80#else /* CONFIG_BLK_INLINE_ENCRYPTION */
81
20f01f16
EB
82static inline int blk_crypto_sysfs_register(struct request_queue *q)
83{
84 return 0;
85}
86
87static inline void blk_crypto_sysfs_unregister(struct request_queue *q) { }
88
a892c8d5
ST
89static inline bool bio_crypt_rq_ctx_compatible(struct request *rq,
90 struct bio *bio)
91{
92 return true;
93}
94
95static inline bool bio_crypt_ctx_front_mergeable(struct request *req,
96 struct bio *bio)
97{
98 return true;
99}
100
101static inline bool bio_crypt_ctx_back_mergeable(struct request *req,
102 struct bio *bio)
103{
104 return true;
105}
106
107static inline bool bio_crypt_ctx_merge_rq(struct request *req,
108 struct request *next)
109{
110 return true;
111}
112
113static inline void blk_crypto_rq_set_defaults(struct request *rq) { }
114
115static inline bool blk_crypto_rq_is_encrypted(struct request *rq)
116{
117 return false;
118}
119
120#endif /* CONFIG_BLK_INLINE_ENCRYPTION */
121
122void __bio_crypt_advance(struct bio *bio, unsigned int bytes);
123static inline void bio_crypt_advance(struct bio *bio, unsigned int bytes)
124{
125 if (bio_has_crypt_ctx(bio))
126 __bio_crypt_advance(bio, bytes);
127}
128
129void __bio_crypt_free_ctx(struct bio *bio);
130static inline void bio_crypt_free_ctx(struct bio *bio)
131{
132 if (bio_has_crypt_ctx(bio))
133 __bio_crypt_free_ctx(bio);
134}
135
136static inline void bio_crypt_do_front_merge(struct request *rq,
137 struct bio *bio)
138{
139#ifdef CONFIG_BLK_INLINE_ENCRYPTION
140 if (bio_has_crypt_ctx(bio))
141 memcpy(rq->crypt_ctx->bc_dun, bio->bi_crypt_context->bc_dun,
142 sizeof(rq->crypt_ctx->bc_dun));
143#endif
144}
145
146bool __blk_crypto_bio_prep(struct bio **bio_ptr);
147static inline bool blk_crypto_bio_prep(struct bio **bio_ptr)
148{
149 if (bio_has_crypt_ctx(*bio_ptr))
150 return __blk_crypto_bio_prep(bio_ptr);
151 return true;
152}
153
154blk_status_t __blk_crypto_init_request(struct request *rq);
155static inline blk_status_t blk_crypto_init_request(struct request *rq)
156{
157 if (blk_crypto_rq_is_encrypted(rq))
158 return __blk_crypto_init_request(rq);
159 return BLK_STS_OK;
160}
161
162void __blk_crypto_free_request(struct request *rq);
163static inline void blk_crypto_free_request(struct request *rq)
164{
165 if (blk_crypto_rq_is_encrypted(rq))
166 __blk_crypto_free_request(rq);
167}
168
93f221ae
EB
169int __blk_crypto_rq_bio_prep(struct request *rq, struct bio *bio,
170 gfp_t gfp_mask);
171/**
172 * blk_crypto_rq_bio_prep - Prepare a request's crypt_ctx when its first bio
173 * is inserted
174 * @rq: The request to prepare
175 * @bio: The first bio being inserted into the request
176 * @gfp_mask: Memory allocation flags
177 *
178 * Return: 0 on success, -ENOMEM if out of memory. -ENOMEM is only possible if
179 * @gfp_mask doesn't include %__GFP_DIRECT_RECLAIM.
180 */
181static inline int blk_crypto_rq_bio_prep(struct request *rq, struct bio *bio,
182 gfp_t gfp_mask)
a892c8d5
ST
183{
184 if (bio_has_crypt_ctx(bio))
93f221ae
EB
185 return __blk_crypto_rq_bio_prep(rq, bio, gfp_mask);
186 return 0;
a892c8d5
ST
187}
188
189/**
190 * blk_crypto_insert_cloned_request - Prepare a cloned request to be inserted
191 * into a request queue.
192 * @rq: the request being queued
193 *
194 * Return: BLK_STS_OK on success, nonzero on error.
195 */
196static inline blk_status_t blk_crypto_insert_cloned_request(struct request *rq)
197{
198
199 if (blk_crypto_rq_is_encrypted(rq))
200 return blk_crypto_init_request(rq);
201 return BLK_STS_OK;
202}
203
488f6682
ST
204#ifdef CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK
205
206int blk_crypto_fallback_start_using_mode(enum blk_crypto_mode_num mode_num);
207
208bool blk_crypto_fallback_bio_prep(struct bio **bio_ptr);
209
210int blk_crypto_fallback_evict_key(const struct blk_crypto_key *key);
211
212#else /* CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK */
213
214static inline int
215blk_crypto_fallback_start_using_mode(enum blk_crypto_mode_num mode_num)
216{
217 pr_warn_once("crypto API fallback is disabled\n");
218 return -ENOPKG;
219}
220
221static inline bool blk_crypto_fallback_bio_prep(struct bio **bio_ptr)
222{
223 pr_warn_once("crypto API fallback disabled; failing request.\n");
224 (*bio_ptr)->bi_status = BLK_STS_NOTSUPP;
225 return false;
226}
227
228static inline int
229blk_crypto_fallback_evict_key(const struct blk_crypto_key *key)
230{
231 return 0;
232}
233
234#endif /* CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK */
235
a892c8d5 236#endif /* __LINUX_BLK_CRYPTO_INTERNAL_H */