fs/crypto/crypto.c
Source file repositories/reference/linux-study-clean/fs/crypto/crypto.c
File Facts
- System
- Linux kernel
- Corpus path
fs/crypto/crypto.c- Extension
.c- Size
- 13717 bytes
- Lines
- 419
- Domain
- Core OS
- Bucket
- VFS And Filesystem Core
- Inferred role
- Core OS: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
crypto/skcipher.hlinux/export.hlinux/mempool.hlinux/module.hlinux/pagemap.hlinux/ratelimit.hlinux/scatterlist.hfscrypt_private.h
Detected Declarations
function fscrypt_enqueue_decrypt_workfunction fscrypt_free_bounce_pagefunction fscrypt_limit_io_blocksfunction fscrypt_crypt_data_unitfunction fscrypt_encrypt_block_inplacefunction fscrypt_decrypt_pagecache_blocksfunction fscrypt_decrypt_block_inplacefunction fscrypt_initializefunction fscrypt_msgfunction fscrypt_initexport fscrypt_enqueue_decrypt_workexport fscrypt_free_bounce_pageexport fscrypt_encrypt_pagecache_blocksexport fscrypt_encrypt_block_inplaceexport fscrypt_decrypt_pagecache_blocksexport fscrypt_decrypt_block_inplace
Annotated Snippet
if (err) {
fscrypt_free_bounce_page(ciphertext_page);
return ERR_PTR(err);
}
}
SetPagePrivate(ciphertext_page);
set_page_private(ciphertext_page, (unsigned long)folio);
return ciphertext_page;
}
EXPORT_SYMBOL(fscrypt_encrypt_pagecache_blocks);
/**
* fscrypt_encrypt_block_inplace() - Encrypt a filesystem block in-place
* @inode: The inode to which this block belongs
* @page: The page containing the block to encrypt
* @len: Size of block to encrypt. This must be a multiple of
* FSCRYPT_CONTENTS_ALIGNMENT.
* @offs: Byte offset within @page at which the block to encrypt begins
* @lblk_num: Filesystem logical block number of the block, i.e. the 0-based
* number of the block within the file
*
* Encrypt a possibly-compressed filesystem block that is located in an
* arbitrary page, not necessarily in the original pagecache page. The @inode
* and @lblk_num must be specified, as they can't be determined from @page.
*
* This is not compatible with fscrypt_operations::supports_subblock_data_units.
*
* Return: 0 on success; -errno on failure
*/
int fscrypt_encrypt_block_inplace(const struct inode *inode, struct page *page,
unsigned int len, unsigned int offs,
u64 lblk_num)
{
if (WARN_ON_ONCE(inode->i_sb->s_cop->supports_subblock_data_units))
return -EOPNOTSUPP;
return fscrypt_crypt_data_unit(fscrypt_get_inode_info_raw(inode),
FS_ENCRYPT, lblk_num, page, page, len,
offs);
}
EXPORT_SYMBOL(fscrypt_encrypt_block_inplace);
/**
* fscrypt_decrypt_pagecache_blocks() - Decrypt data from a pagecache folio
* @folio: the pagecache folio containing the data to decrypt
* @len: size of the data to decrypt, in bytes
* @offs: offset within @folio of the data to decrypt, in bytes
*
* Decrypt data that has just been read from an encrypted file. The data must
* be located in a pagecache folio that is still locked and not yet uptodate.
* The length and offset of the data must be aligned to the file's crypto data
* unit size. Alignment to the filesystem block size fulfills this requirement,
* as the filesystem block size is always a multiple of the data unit size.
*
* Return: 0 on success; -errno on failure
*/
int fscrypt_decrypt_pagecache_blocks(struct folio *folio, size_t len,
size_t offs)
{
const struct inode *inode = folio->mapping->host;
const struct fscrypt_inode_info *ci = fscrypt_get_inode_info_raw(inode);
const unsigned int du_bits = ci->ci_data_unit_bits;
const unsigned int du_size = 1U << du_bits;
u64 index = ((u64)folio->index << (PAGE_SHIFT - du_bits)) +
(offs >> du_bits);
size_t i;
int err;
if (WARN_ON_ONCE(!folio_test_locked(folio)))
return -EINVAL;
if (WARN_ON_ONCE(len <= 0 || !IS_ALIGNED(len | offs, du_size)))
return -EINVAL;
for (i = offs; i < offs + len; i += du_size, index++) {
struct page *page = folio_page(folio, i >> PAGE_SHIFT);
err = fscrypt_crypt_data_unit(ci, FS_DECRYPT, index, page,
page, du_size, i & ~PAGE_MASK);
if (err)
return err;
}
return 0;
}
EXPORT_SYMBOL(fscrypt_decrypt_pagecache_blocks);
/**
* fscrypt_decrypt_block_inplace() - Decrypt a filesystem block in-place
* @inode: The inode to which this block belongs
* @page: The page containing the block to decrypt
* @len: Size of block to decrypt. This must be a multiple of
Annotation
- Immediate include surface: `crypto/skcipher.h`, `linux/export.h`, `linux/mempool.h`, `linux/module.h`, `linux/pagemap.h`, `linux/ratelimit.h`, `linux/scatterlist.h`, `fscrypt_private.h`.
- Detected declarations: `function fscrypt_enqueue_decrypt_work`, `function fscrypt_free_bounce_page`, `function fscrypt_limit_io_blocks`, `function fscrypt_crypt_data_unit`, `function fscrypt_encrypt_block_inplace`, `function fscrypt_decrypt_pagecache_blocks`, `function fscrypt_decrypt_block_inplace`, `function fscrypt_initialize`, `function fscrypt_msg`, `function fscrypt_init`.
- Atlas domain: Core OS / VFS And Filesystem Core.
- Implementation status: integration implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
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.