fs/btrfs/compression.c
Source file repositories/reference/linux-study-clean/fs/btrfs/compression.c
File Facts
- System
- Linux kernel
- Corpus path
fs/btrfs/compression.c- Extension
.c- Size
- 44971 bytes
- Lines
- 1663
- Domain
- Core OS
- Bucket
- VFS And Filesystem Core
- Inferred role
- Core OS: implementation source
- Status
- source 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.
- 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
linux/kernel.hlinux/bio.hlinux/file.hlinux/fs.hlinux/pagemap.hlinux/folio_batch.hlinux/highmem.hlinux/kthread.hlinux/time.hlinux/init.hlinux/string.hlinux/backing-dev.hlinux/writeback.hlinux/psi.hlinux/slab.hlinux/sched/mm.hlinux/log2.hlinux/shrinker.hmisc.hctree.hfs.hbtrfs_inode.hbio.hordered-data.hcompression.hextent_io.hextent_map.hsubpage.hmessages.hsuper.h
Detected Declarations
struct bucket_itemstruct heuristic_wsfunction btrfs_compress_type2strfunction btrfs_compress_is_valid_typefunction compression_decompress_biofunction compression_decompressfunction btrfs_compr_pool_countfunction btrfs_compr_pool_scanfunction list_for_each_safefunction btrfs_free_compr_foliofunction end_bbio_compressed_readfunction end_compressed_writebackfunction end_bbio_compressed_writefunction btrfs_submit_compressed_writefunction add_ra_bio_foliosfunction btrfs_submit_compressed_readfunction free_heuristic_wsfunction free_workspacefunction alloc_workspace_managerfunction free_workspace_managerfunction btrfs_put_workspacefunction put_workspacefunction btrfs_compress_set_levelfunction btrfs_compress_level_validfunction btrfs_compress_filemap_get_foliofunction btrfs_alloc_compr_foliofunction btrfs_decompress_biofunction btrfs_decompressfunction btrfs_alloc_compress_wsmfunction btrfs_free_compress_wsmfunction btrfs_init_compressfunction btrfs_exit_compressfunction btrfs_decompress_buf2pagefunction ilog2function shannon_entropyfunction get4bitsfunction radix_sortfunction samefunction datafunction sample_repeated_patternsfunction heuristic_collect_samplefunction btrfs_compress_heuristicfunction suffix
Annotated Snippet
struct bucket_item {
u32 count;
};
struct heuristic_ws {
/* Partial copy of input data */
u8 *sample;
u32 sample_size;
/* Buckets store counters for each byte value */
struct bucket_item *bucket;
/* Sorting buffer */
struct bucket_item *bucket_b;
struct list_head list;
};
static void free_heuristic_ws(struct list_head *ws)
{
struct heuristic_ws *workspace;
workspace = list_entry(ws, struct heuristic_ws, list);
kvfree(workspace->sample);
kfree(workspace->bucket);
kfree(workspace->bucket_b);
kfree(workspace);
}
static struct list_head *alloc_heuristic_ws(struct btrfs_fs_info *fs_info)
{
struct heuristic_ws *ws;
ws = kzalloc_obj(*ws);
if (!ws)
return ERR_PTR(-ENOMEM);
ws->sample = kvmalloc(MAX_SAMPLE_SIZE, GFP_KERNEL);
if (!ws->sample)
goto fail;
ws->bucket = kzalloc_objs(*ws->bucket, BUCKET_SIZE);
if (!ws->bucket)
goto fail;
ws->bucket_b = kzalloc_objs(*ws->bucket_b, BUCKET_SIZE);
if (!ws->bucket_b)
goto fail;
INIT_LIST_HEAD(&ws->list);
return &ws->list;
fail:
free_heuristic_ws(&ws->list);
return ERR_PTR(-ENOMEM);
}
const struct btrfs_compress_levels btrfs_heuristic_compress = { 0 };
static const struct btrfs_compress_levels * const btrfs_compress_levels[] = {
/* The heuristic is represented as compression type 0 */
&btrfs_heuristic_compress,
&btrfs_zlib_compress,
&btrfs_lzo_compress,
&btrfs_zstd_compress,
};
static struct list_head *alloc_workspace(struct btrfs_fs_info *fs_info, int type, int level)
{
switch (type) {
case BTRFS_COMPRESS_NONE: return alloc_heuristic_ws(fs_info);
case BTRFS_COMPRESS_ZLIB: return zlib_alloc_workspace(fs_info, level);
case BTRFS_COMPRESS_LZO: return lzo_alloc_workspace(fs_info);
case BTRFS_COMPRESS_ZSTD: return zstd_alloc_workspace(fs_info, level);
default:
/*
* This can't happen, the type is validated several times
* before we get here.
*/
BUG();
}
}
static void free_workspace(int type, struct list_head *ws)
{
switch (type) {
case BTRFS_COMPRESS_NONE: return free_heuristic_ws(ws);
case BTRFS_COMPRESS_ZLIB: return zlib_free_workspace(ws);
case BTRFS_COMPRESS_LZO: return lzo_free_workspace(ws);
case BTRFS_COMPRESS_ZSTD: return zstd_free_workspace(ws);
default:
/*
* This can't happen, the type is validated several times
Annotation
- Immediate include surface: `linux/kernel.h`, `linux/bio.h`, `linux/file.h`, `linux/fs.h`, `linux/pagemap.h`, `linux/folio_batch.h`, `linux/highmem.h`, `linux/kthread.h`.
- Detected declarations: `struct bucket_item`, `struct heuristic_ws`, `function btrfs_compress_type2str`, `function btrfs_compress_is_valid_type`, `function compression_decompress_bio`, `function compression_decompress`, `function btrfs_compr_pool_count`, `function btrfs_compr_pool_scan`, `function list_for_each_safe`, `function btrfs_free_compr_folio`.
- Atlas domain: Core OS / VFS And Filesystem Core.
- Implementation status: source 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.