Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/fs/btrfs/compression.h
29267 views
1
/* SPDX-License-Identifier: GPL-2.0 */
2
/*
3
* Copyright (C) 2008 Oracle. All rights reserved.
4
*/
5
6
#ifndef BTRFS_COMPRESSION_H
7
#define BTRFS_COMPRESSION_H
8
9
#include <linux/sizes.h>
10
#include <linux/mm.h>
11
#include <linux/list.h>
12
#include <linux/workqueue.h>
13
#include <linux/wait.h>
14
#include <linux/pagemap.h>
15
#include "bio.h"
16
#include "fs.h"
17
#include "messages.h"
18
19
struct address_space;
20
struct page;
21
struct inode;
22
struct btrfs_inode;
23
struct btrfs_ordered_extent;
24
struct btrfs_bio;
25
26
/*
27
* We want to make sure that amount of RAM required to uncompress an extent is
28
* reasonable, so we limit the total size in ram of a compressed extent to
29
* 128k. This is a crucial number because it also controls how easily we can
30
* spread reads across cpus for decompression.
31
*
32
* We also want to make sure the amount of IO required to do a random read is
33
* reasonably small, so we limit the size of a compressed extent to 128k.
34
*/
35
36
/* Maximum length of compressed data stored on disk */
37
#define BTRFS_MAX_COMPRESSED (SZ_128K)
38
#define BTRFS_MAX_COMPRESSED_PAGES (BTRFS_MAX_COMPRESSED / PAGE_SIZE)
39
static_assert((BTRFS_MAX_COMPRESSED % PAGE_SIZE) == 0);
40
41
/* Maximum size of data before compression */
42
#define BTRFS_MAX_UNCOMPRESSED (SZ_128K)
43
44
#define BTRFS_ZLIB_DEFAULT_LEVEL 3
45
46
struct compressed_bio {
47
/* Number of compressed folios in the array. */
48
unsigned int nr_folios;
49
50
/* The folios with the compressed data on them. */
51
struct folio **compressed_folios;
52
53
/* starting offset in the inode for our pages */
54
u64 start;
55
56
/* Number of bytes in the inode we're working on */
57
unsigned int len;
58
59
/* Number of bytes on disk */
60
unsigned int compressed_len;
61
62
/* The compression algorithm for this bio */
63
u8 compress_type;
64
65
/* Whether this is a write for writeback. */
66
bool writeback;
67
68
union {
69
/* For reads, this is the bio we are copying the data into */
70
struct btrfs_bio *orig_bbio;
71
struct work_struct write_end_work;
72
};
73
74
/* Must be last. */
75
struct btrfs_bio bbio;
76
};
77
78
static inline struct btrfs_fs_info *cb_to_fs_info(const struct compressed_bio *cb)
79
{
80
return cb->bbio.fs_info;
81
}
82
83
/* @range_end must be exclusive. */
84
static inline u32 btrfs_calc_input_length(struct folio *folio, u64 range_end, u64 cur)
85
{
86
/* @cur must be inside the folio. */
87
ASSERT(folio_pos(folio) <= cur);
88
ASSERT(cur < folio_end(folio));
89
return min(range_end, folio_end(folio)) - cur;
90
}
91
92
int btrfs_alloc_compress_wsm(struct btrfs_fs_info *fs_info);
93
void btrfs_free_compress_wsm(struct btrfs_fs_info *fs_info);
94
95
int __init btrfs_init_compress(void);
96
void __cold btrfs_exit_compress(void);
97
98
bool btrfs_compress_level_valid(unsigned int type, int level);
99
int btrfs_compress_folios(unsigned int type, int level, struct btrfs_inode *inode,
100
u64 start, struct folio **folios, unsigned long *out_folios,
101
unsigned long *total_in, unsigned long *total_out);
102
int btrfs_decompress(int type, const u8 *data_in, struct folio *dest_folio,
103
unsigned long start_byte, size_t srclen, size_t destlen);
104
int btrfs_decompress_buf2page(const char *buf, u32 buf_len,
105
struct compressed_bio *cb, u32 decompressed);
106
107
void btrfs_submit_compressed_write(struct btrfs_ordered_extent *ordered,
108
struct folio **compressed_folios,
109
unsigned int nr_folios, blk_opf_t write_flags,
110
bool writeback);
111
void btrfs_submit_compressed_read(struct btrfs_bio *bbio);
112
113
int btrfs_compress_str2level(unsigned int type, const char *str, int *level_ret);
114
115
struct folio *btrfs_alloc_compr_folio(struct btrfs_fs_info *fs_info);
116
void btrfs_free_compr_folio(struct folio *folio);
117
118
struct workspace_manager {
119
struct list_head idle_ws;
120
spinlock_t ws_lock;
121
/* Number of free workspaces */
122
int free_ws;
123
/* Total number of allocated workspaces */
124
atomic_t total_ws;
125
/* Waiters for a free workspace */
126
wait_queue_head_t ws_wait;
127
};
128
129
struct list_head *btrfs_get_workspace(struct btrfs_fs_info *fs_info, int type, int level);
130
void btrfs_put_workspace(struct btrfs_fs_info *fs_info, int type, struct list_head *ws);
131
132
struct btrfs_compress_levels {
133
/* Maximum level supported by the compression algorithm */
134
int min_level;
135
int max_level;
136
int default_level;
137
};
138
139
/* The heuristic workspaces are managed via the 0th workspace manager */
140
#define BTRFS_NR_WORKSPACE_MANAGERS BTRFS_NR_COMPRESS_TYPES
141
142
extern const struct btrfs_compress_levels btrfs_heuristic_compress;
143
extern const struct btrfs_compress_levels btrfs_zlib_compress;
144
extern const struct btrfs_compress_levels btrfs_lzo_compress;
145
extern const struct btrfs_compress_levels btrfs_zstd_compress;
146
147
const char* btrfs_compress_type2str(enum btrfs_compression_type type);
148
bool btrfs_compress_is_valid_type(const char *str, size_t len);
149
150
int btrfs_compress_heuristic(struct btrfs_inode *inode, u64 start, u64 end);
151
152
int btrfs_compress_filemap_get_folio(struct address_space *mapping, u64 start,
153
struct folio **in_folio_ret);
154
155
int zlib_compress_folios(struct list_head *ws, struct btrfs_inode *inode,
156
u64 start, struct folio **folios, unsigned long *out_folios,
157
unsigned long *total_in, unsigned long *total_out);
158
int zlib_decompress_bio(struct list_head *ws, struct compressed_bio *cb);
159
int zlib_decompress(struct list_head *ws, const u8 *data_in,
160
struct folio *dest_folio, unsigned long dest_pgoff, size_t srclen,
161
size_t destlen);
162
struct list_head *zlib_alloc_workspace(struct btrfs_fs_info *fs_info, unsigned int level);
163
void zlib_free_workspace(struct list_head *ws);
164
struct list_head *zlib_get_workspace(struct btrfs_fs_info *fs_info, unsigned int level);
165
166
int lzo_compress_folios(struct list_head *ws, struct btrfs_inode *inode,
167
u64 start, struct folio **folios, unsigned long *out_folios,
168
unsigned long *total_in, unsigned long *total_out);
169
int lzo_decompress_bio(struct list_head *ws, struct compressed_bio *cb);
170
int lzo_decompress(struct list_head *ws, const u8 *data_in,
171
struct folio *dest_folio, unsigned long dest_pgoff, size_t srclen,
172
size_t destlen);
173
struct list_head *lzo_alloc_workspace(struct btrfs_fs_info *fs_info);
174
void lzo_free_workspace(struct list_head *ws);
175
176
int zstd_compress_folios(struct list_head *ws, struct btrfs_inode *inode,
177
u64 start, struct folio **folios, unsigned long *out_folios,
178
unsigned long *total_in, unsigned long *total_out);
179
int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb);
180
int zstd_decompress(struct list_head *ws, const u8 *data_in,
181
struct folio *dest_folio, unsigned long dest_pgoff, size_t srclen,
182
size_t destlen);
183
int zstd_alloc_workspace_manager(struct btrfs_fs_info *fs_info);
184
void zstd_free_workspace_manager(struct btrfs_fs_info *fs_info);
185
struct list_head *zstd_alloc_workspace(struct btrfs_fs_info *fs_info, int level);
186
void zstd_free_workspace(struct list_head *ws);
187
struct list_head *zstd_get_workspace(struct btrfs_fs_info *fs_info, int level);
188
void zstd_put_workspace(struct btrfs_fs_info *fs_info, struct list_head *ws);
189
190
#endif
191
192