block/blk-crypto.c
Source file repositories/reference/linux-study-clean/block/blk-crypto.c
File Facts
- System
- Linux kernel
- Corpus path
block/blk-crypto.c- Extension
.c- Size
- 16492 bytes
- Lines
- 585
- Domain
- Representative Device Path
- Bucket
- PCIe NVMe Storage Path
- Inferred role
- Representative Device Path: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Part of the selected hardware vertical slice: PCI discovery, driver binding, NVMe queues, block requests, DMA, interrupts, and completion.
- Part of the selected hardware vertical slice: PCI discovery, driver binding, NVMe queues, block requests, DMA, interrupts, and completion.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/bio.hlinux/blkdev.hlinux/blk-crypto-profile.hlinux/module.hlinux/ratelimit.hlinux/slab.hblk-crypto-internal.h
Detected Declarations
function bio_crypt_ctx_initfunction bio_crypt_set_ctxfunction __bio_crypt_free_ctxfunction __bio_crypt_clonefunction bio_crypt_dun_incrementfunction __bio_crypt_advancefunction bio_crypt_dun_is_contiguousfunction bio_crypt_ctx_compatiblefunction bio_crypt_rq_ctx_compatiblefunction continuousfunction __blk_crypto_rq_get_keyslotfunction __blk_crypto_rq_put_keyslotfunction __blk_crypto_free_requestfunction __blk_crypto_submit_biofunction __blk_crypto_rq_bio_prepfunction blk_crypto_init_keyfunction blk_crypto_config_supported_nativelyfunction blk_crypto_config_supportedfunction blk_crypto_start_using_keyfunction blk_crypto_evict_keyfunction blk_crypto_ioctl_import_keyfunction copy_to_userfunction blk_crypto_ioctl_generate_keyfunction copy_to_userfunction blk_crypto_ioctl_prepare_keyfunction copy_to_userfunction blk_crypto_ioctlmodule init bio_crypt_ctx_initexport bio_crypt_set_ctxexport __blk_crypto_submit_bioexport blk_crypto_init_keyexport blk_crypto_start_using_keyexport blk_crypto_evict_key
Annotated Snippet
subsys_initcall(bio_crypt_ctx_init);
void bio_crypt_set_ctx(struct bio *bio, const struct blk_crypto_key *key,
const u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE], gfp_t gfp_mask)
{
struct bio_crypt_ctx *bc;
/*
* The caller must use a gfp_mask that contains __GFP_DIRECT_RECLAIM so
* that the mempool_alloc() can't fail.
*/
WARN_ON_ONCE(!(gfp_mask & __GFP_DIRECT_RECLAIM));
bc = mempool_alloc(bio_crypt_ctx_pool, gfp_mask);
bc->bc_key = key;
memcpy(bc->bc_dun, dun, sizeof(bc->bc_dun));
bio->bi_crypt_context = bc;
}
EXPORT_SYMBOL_GPL(bio_crypt_set_ctx);
void __bio_crypt_free_ctx(struct bio *bio)
{
mempool_free(bio->bi_crypt_context, bio_crypt_ctx_pool);
bio->bi_crypt_context = NULL;
}
int __bio_crypt_clone(struct bio *dst, struct bio *src, gfp_t gfp_mask)
{
dst->bi_crypt_context = mempool_alloc(bio_crypt_ctx_pool, gfp_mask);
if (!dst->bi_crypt_context)
return -ENOMEM;
*dst->bi_crypt_context = *src->bi_crypt_context;
return 0;
}
/* Increments @dun by @inc, treating @dun as a multi-limb integer. */
void bio_crypt_dun_increment(u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE],
unsigned int inc)
{
int i;
for (i = 0; inc && i < BLK_CRYPTO_DUN_ARRAY_SIZE; i++) {
dun[i] += inc;
/*
* If the addition in this limb overflowed, then we need to
* carry 1 into the next limb. Else the carry is 0.
*/
if (dun[i] < inc)
inc = 1;
else
inc = 0;
}
}
void __bio_crypt_advance(struct bio *bio, unsigned int bytes)
{
struct bio_crypt_ctx *bc = bio->bi_crypt_context;
bio_crypt_dun_increment(bc->bc_dun,
bytes >> bc->bc_key->data_unit_size_bits);
}
/*
* Returns true if @bc->bc_dun plus @bytes converted to data units is equal to
* @next_dun, treating the DUNs as multi-limb integers.
*/
bool bio_crypt_dun_is_contiguous(const struct bio_crypt_ctx *bc,
unsigned int bytes,
const u64 next_dun[BLK_CRYPTO_DUN_ARRAY_SIZE])
{
int i;
unsigned int carry = bytes >> bc->bc_key->data_unit_size_bits;
for (i = 0; i < BLK_CRYPTO_DUN_ARRAY_SIZE; i++) {
if (bc->bc_dun[i] + carry != next_dun[i])
return false;
/*
* If the addition in this limb overflowed, then we need to
* carry 1 into the next limb. Else the carry is 0.
*/
if ((bc->bc_dun[i] + carry) < carry)
carry = 1;
else
carry = 0;
}
/* If the DUN wrapped through 0, don't treat it as contiguous. */
return carry == 0;
Annotation
- Immediate include surface: `linux/bio.h`, `linux/blkdev.h`, `linux/blk-crypto-profile.h`, `linux/module.h`, `linux/ratelimit.h`, `linux/slab.h`, `blk-crypto-internal.h`.
- Detected declarations: `function bio_crypt_ctx_init`, `function bio_crypt_set_ctx`, `function __bio_crypt_free_ctx`, `function __bio_crypt_clone`, `function bio_crypt_dun_increment`, `function __bio_crypt_advance`, `function bio_crypt_dun_is_contiguous`, `function bio_crypt_ctx_compatible`, `function bio_crypt_rq_ctx_compatible`, `function continuous`.
- Atlas domain: Representative Device Path / PCIe NVMe Storage Path.
- Implementation status: integration implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.