fs/btrfs/zstd.c
Source file repositories/reference/linux-study-clean/fs/btrfs/zstd.c
File Facts
- System
- Linux kernel
- Corpus path
fs/btrfs/zstd.c- Extension
.c- Size
- 21221 bytes
- Lines
- 737
- 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/bio.hlinux/bitmap.hlinux/err.hlinux/init.hlinux/kernel.hlinux/mm.hlinux/sched/mm.hlinux/pagemap.hlinux/refcount.hlinux/sched.hlinux/slab.hlinux/zstd.hmisc.hfs.hbtrfs_inode.hcompression.hsuper.h
Detected Declarations
struct workspacestruct zstd_workspace_managerfunction Copyrightfunction clip_levelfunction zstd_reclaim_timer_fnfunction list_for_each_prev_safefunction zstd_calc_ws_mem_sizesfunction zstd_alloc_workspace_managerfunction zstd_free_workspace_managerfunction zstd_put_workspacefunction zstd_free_workspacefunction zstd_compress_biofunction zstd_decompress_biofunction zstd_decompress
Annotated Snippet
struct workspace {
void *mem;
size_t size;
char *buf;
int level;
int req_level;
unsigned long last_used; /* jiffies */
struct list_head list;
struct list_head lru_list;
zstd_in_buffer in_buf;
zstd_out_buffer out_buf;
zstd_parameters params;
};
/*
* Zstd Workspace Management
*
* Zstd workspaces have different memory requirements depending on the level.
* The zstd workspaces are managed by having individual lists for each level
* and a global lru. Forward progress is maintained by protecting a max level
* workspace.
*
* Getting a workspace is done by using the bitmap to identify the levels that
* have available workspaces and scans up. This lets us recycle higher level
* workspaces because of the monotonic memory guarantee. A workspace's
* last_used is only updated if it is being used by the corresponding memory
* level. Putting a workspace involves adding it back to the appropriate places
* and adding it back to the lru if necessary.
*
* A timer is used to reclaim workspaces if they have not been used for
* ZSTD_BTRFS_RECLAIM_JIFFIES. This helps keep only active workspaces around.
* The upper bound is provided by the workqueue limit which is 2 (percpu limit).
*/
struct zstd_workspace_manager {
spinlock_t lock;
struct list_head lru_list;
struct list_head idle_ws[ZSTD_BTRFS_MAX_LEVEL];
unsigned long active_map;
wait_queue_head_t wait;
struct timer_list timer;
};
static size_t zstd_ws_mem_sizes[ZSTD_BTRFS_MAX_LEVEL];
static inline struct workspace *list_to_workspace(struct list_head *list)
{
return container_of(list, struct workspace, list);
}
static inline int clip_level(int level)
{
return max(0, level - 1);
}
/*
* Timer callback to free unused workspaces.
*
* @t: timer
*
* This scans the lru_list and attempts to reclaim any workspace that hasn't
* been used for ZSTD_BTRFS_RECLAIM_JIFFIES.
*
* The context is softirq and does not need the _bh locking primitives.
*/
static void zstd_reclaim_timer_fn(struct timer_list *timer)
{
struct zstd_workspace_manager *zwsm =
container_of(timer, struct zstd_workspace_manager, timer);
unsigned long reclaim_threshold = jiffies - ZSTD_BTRFS_RECLAIM_JIFFIES;
struct list_head *pos, *next;
spin_lock(&zwsm->lock);
if (list_empty(&zwsm->lru_list)) {
spin_unlock(&zwsm->lock);
return;
}
list_for_each_prev_safe(pos, next, &zwsm->lru_list) {
struct workspace *victim = container_of(pos, struct workspace,
lru_list);
int level;
if (time_after(victim->last_used, reclaim_threshold))
break;
/* workspace is in use */
if (victim->req_level)
continue;
Annotation
- Immediate include surface: `linux/bio.h`, `linux/bitmap.h`, `linux/err.h`, `linux/init.h`, `linux/kernel.h`, `linux/mm.h`, `linux/sched/mm.h`, `linux/pagemap.h`.
- Detected declarations: `struct workspace`, `struct zstd_workspace_manager`, `function Copyright`, `function clip_level`, `function zstd_reclaim_timer_fn`, `function list_for_each_prev_safe`, `function zstd_calc_ws_mem_sizes`, `function zstd_alloc_workspace_manager`, `function zstd_free_workspace_manager`, `function zstd_put_workspace`.
- 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.