xref: /openbmc/linux/fs/btrfs/compression.h (revision 26d0dfbb16fcb17d128a79dc70f3020ea6992af0)
19888c340SDavid Sterba /* SPDX-License-Identifier: GPL-2.0 */
2c8b97818SChris Mason /*
3c8b97818SChris Mason  * Copyright (C) 2008 Oracle.  All rights reserved.
4c8b97818SChris Mason  */
5c8b97818SChris Mason 
69888c340SDavid Sterba #ifndef BTRFS_COMPRESSION_H
79888c340SDavid Sterba #define BTRFS_COMPRESSION_H
8c8b97818SChris Mason 
9d5c1d68fSQu Wenruo #include <linux/sizes.h>
10544fe4a9SChristoph Hellwig #include "bio.h"
11d5c1d68fSQu Wenruo 
12c7ee1819SNikolay Borisov struct btrfs_inode;
13d611935bSChristoph Hellwig struct btrfs_ordered_extent;
14c7ee1819SNikolay Borisov 
15ff763866SDavid Sterba /*
16ff763866SDavid Sterba  * We want to make sure that amount of RAM required to uncompress an extent is
17ff763866SDavid Sterba  * reasonable, so we limit the total size in ram of a compressed extent to
18ff763866SDavid Sterba  * 128k.  This is a crucial number because it also controls how easily we can
19ff763866SDavid Sterba  * spread reads across cpus for decompression.
20ff763866SDavid Sterba  *
21ff763866SDavid Sterba  * We also want to make sure the amount of IO required to do a random read is
22ff763866SDavid Sterba  * reasonably small, so we limit the size of a compressed extent to 128k.
23ff763866SDavid Sterba  */
24ff763866SDavid Sterba 
25ff763866SDavid Sterba /* Maximum length of compressed data stored on disk */
26ff763866SDavid Sterba #define BTRFS_MAX_COMPRESSED		(SZ_128K)
27544fe4a9SChristoph Hellwig #define BTRFS_MAX_COMPRESSED_PAGES	(BTRFS_MAX_COMPRESSED / PAGE_SIZE)
28a55e65b8SDavid Sterba static_assert((BTRFS_MAX_COMPRESSED % PAGE_SIZE) == 0);
29a55e65b8SDavid Sterba 
30ff763866SDavid Sterba /* Maximum size of data before compression */
31ff763866SDavid Sterba #define BTRFS_MAX_UNCOMPRESSED		(SZ_128K)
32ff763866SDavid Sterba 
33eae8d825SQu Wenruo #define	BTRFS_ZLIB_DEFAULT_LEVEL		3
34eae8d825SQu Wenruo 
35e1ddce71SAnand Jain struct compressed_bio {
36282ab3ffSDavid Sterba 	/* Number of compressed pages in the array */
37282ab3ffSDavid Sterba 	unsigned int nr_pages;
38282ab3ffSDavid Sterba 
39e1ddce71SAnand Jain 	/* the pages with the compressed data on them */
40e1ddce71SAnand Jain 	struct page **compressed_pages;
41e1ddce71SAnand Jain 
42e1ddce71SAnand Jain 	/* starting offset in the inode for our pages */
43e1ddce71SAnand Jain 	u64 start;
44e1ddce71SAnand Jain 
45282ab3ffSDavid Sterba 	/* Number of bytes in the inode we're working on */
46282ab3ffSDavid Sterba 	unsigned int len;
47e1ddce71SAnand Jain 
48282ab3ffSDavid Sterba 	/* Number of bytes on disk */
49282ab3ffSDavid Sterba 	unsigned int compressed_len;
50e1ddce71SAnand Jain 
51282ab3ffSDavid Sterba 	/* The compression algorithm for this bio */
52282ab3ffSDavid Sterba 	u8 compress_type;
53e1ddce71SAnand Jain 
547c0c7269SOmar Sandoval 	/* Whether this is a write for writeback. */
557c0c7269SOmar Sandoval 	bool writeback;
567c0c7269SOmar Sandoval 
57fed8a72dSChristoph Hellwig 	union {
58fed8a72dSChristoph Hellwig 		/* For reads, this is the bio we are copying the data into */
59b7d463a1SChristoph Hellwig 		struct btrfs_bio *orig_bbio;
60fed8a72dSChristoph Hellwig 		struct work_struct write_end_work;
61fed8a72dSChristoph Hellwig 	};
62544fe4a9SChristoph Hellwig 
63544fe4a9SChristoph Hellwig 	/* Must be last. */
64544fe4a9SChristoph Hellwig 	struct btrfs_bio bbio;
65e1ddce71SAnand Jain };
66e1ddce71SAnand Jain 
btrfs_compress_type(unsigned int type_level)671972708aSDennis Zhou static inline unsigned int btrfs_compress_type(unsigned int type_level)
681972708aSDennis Zhou {
691972708aSDennis Zhou 	return (type_level & 0xF);
701972708aSDennis Zhou }
711972708aSDennis Zhou 
btrfs_compress_level(unsigned int type_level)721972708aSDennis Zhou static inline unsigned int btrfs_compress_level(unsigned int type_level)
731972708aSDennis Zhou {
741972708aSDennis Zhou 	return ((type_level & 0xF0) >> 4);
751972708aSDennis Zhou }
761972708aSDennis Zhou 
775565b8e0SQu Wenruo int __init btrfs_init_compress(void);
78e67c718bSDavid Sterba void __cold btrfs_exit_compress(void);
79261507a0SLi Zefan 
80f51d2b59SDavid Sterba int btrfs_compress_pages(unsigned int type_level, struct address_space *mapping,
8138c31464SDavid Sterba 			 u64 start, struct page **pages,
82c8b97818SChris Mason 			 unsigned long *out_pages,
83c8b97818SChris Mason 			 unsigned long *total_in,
84e5d74902SDavid Sterba 			 unsigned long *total_out);
853e09b5b2SDavid Sterba int btrfs_decompress(int type, const u8 *data_in, struct page *dest_page,
86261507a0SLi Zefan 		     unsigned long start_byte, size_t srclen, size_t destlen);
871c3dc173SQu Wenruo int btrfs_decompress_buf2page(const char *buf, u32 buf_len,
881c3dc173SQu Wenruo 			      struct compressed_bio *cb, u32 decompressed);
89261507a0SLi Zefan 
90d611935bSChristoph Hellwig void btrfs_submit_compressed_write(struct btrfs_ordered_extent *ordered,
91c8b97818SChris Mason 				  struct page **compressed_pages,
9265b5355fSAnand Jain 				  unsigned int nr_pages,
93bf9486d6SBart Van Assche 				  blk_opf_t write_flags,
947c0c7269SOmar Sandoval 				  bool writeback);
95e1949310SChristoph Hellwig void btrfs_submit_compressed_read(struct btrfs_bio *bbio);
96ebb8765bSAnand Jain 
97d0ab62ceSDennis Zhou unsigned int btrfs_compress_str2level(unsigned int type, const char *str);
98f51d2b59SDavid Sterba 
99ebb8765bSAnand Jain enum btrfs_compression_type {
100ebb8765bSAnand Jain 	BTRFS_COMPRESS_NONE  = 0,
101ebb8765bSAnand Jain 	BTRFS_COMPRESS_ZLIB  = 1,
102ebb8765bSAnand Jain 	BTRFS_COMPRESS_LZO   = 2,
1035c1aab1dSNick Terrell 	BTRFS_COMPRESS_ZSTD  = 3,
104ce96b7ffSChengguang Xu 	BTRFS_NR_COMPRESS_TYPES = 4,
105ebb8765bSAnand Jain };
106ebb8765bSAnand Jain 
10792ee5530SDennis Zhou struct workspace_manager {
10892ee5530SDennis Zhou 	struct list_head idle_ws;
10992ee5530SDennis Zhou 	spinlock_t ws_lock;
11092ee5530SDennis Zhou 	/* Number of free workspaces */
11192ee5530SDennis Zhou 	int free_ws;
11292ee5530SDennis Zhou 	/* Total number of allocated workspaces */
11392ee5530SDennis Zhou 	atomic_t total_ws;
11492ee5530SDennis Zhou 	/* Waiters for a free workspace */
11592ee5530SDennis Zhou 	wait_queue_head_t ws_wait;
11692ee5530SDennis Zhou };
11792ee5530SDennis Zhou 
1185907a9bbSDavid Sterba struct list_head *btrfs_get_workspace(int type, unsigned int level);
119a3bbd2a9SDavid Sterba void btrfs_put_workspace(int type, struct list_head *ws);
12092ee5530SDennis Zhou 
121261507a0SLi Zefan struct btrfs_compress_op {
122be951045SDavid Sterba 	struct workspace_manager *workspace_manager;
123e18333a7SDavid Sterba 	/* Maximum level supported by the compression algorithm */
124e18333a7SDavid Sterba 	unsigned int max_level;
125e18333a7SDavid Sterba 	unsigned int default_level;
126261507a0SLi Zefan };
127261507a0SLi Zefan 
128ca4ac360SDennis Zhou /* The heuristic workspaces are managed via the 0th workspace manager */
129ce96b7ffSChengguang Xu #define BTRFS_NR_WORKSPACE_MANAGERS	BTRFS_NR_COMPRESS_TYPES
130ca4ac360SDennis Zhou 
131ca4ac360SDennis Zhou extern const struct btrfs_compress_op btrfs_heuristic_compress;
132e8c9f186SDavid Sterba extern const struct btrfs_compress_op btrfs_zlib_compress;
133e8c9f186SDavid Sterba extern const struct btrfs_compress_op btrfs_lzo_compress;
1345c1aab1dSNick Terrell extern const struct btrfs_compress_op btrfs_zstd_compress;
135261507a0SLi Zefan 
136e128f9c3SDavid Sterba const char* btrfs_compress_type2str(enum btrfs_compression_type type);
137aa53e3bfSJohannes Thumshirn bool btrfs_compress_is_valid_type(const char *str, size_t len);
138e128f9c3SDavid Sterba 
139c2fcdcdfSTimofey Titovets int btrfs_compress_heuristic(struct inode *inode, u64 start, u64 end);
140c2fcdcdfSTimofey Titovets 
141cb4c9198SDavid Sterba int zlib_compress_pages(struct list_head *ws, struct address_space *mapping,
142cb4c9198SDavid Sterba 		u64 start, struct page **pages, unsigned long *out_pages,
143cb4c9198SDavid Sterba 		unsigned long *total_in, unsigned long *total_out);
144cb4c9198SDavid Sterba int zlib_decompress_bio(struct list_head *ws, struct compressed_bio *cb);
1453e09b5b2SDavid Sterba int zlib_decompress(struct list_head *ws, const u8 *data_in,
146*56a1bf2bSQu Wenruo 		struct page *dest_page, unsigned long dest_pgoff, size_t srclen,
147cb4c9198SDavid Sterba 		size_t destlen);
148cb4c9198SDavid Sterba struct list_head *zlib_alloc_workspace(unsigned int level);
149cb4c9198SDavid Sterba void zlib_free_workspace(struct list_head *ws);
150cb4c9198SDavid Sterba struct list_head *zlib_get_workspace(unsigned int level);
151cb4c9198SDavid Sterba 
152cb4c9198SDavid Sterba int lzo_compress_pages(struct list_head *ws, struct address_space *mapping,
153cb4c9198SDavid Sterba 		u64 start, struct page **pages, unsigned long *out_pages,
154cb4c9198SDavid Sterba 		unsigned long *total_in, unsigned long *total_out);
155cb4c9198SDavid Sterba int lzo_decompress_bio(struct list_head *ws, struct compressed_bio *cb);
1563e09b5b2SDavid Sterba int lzo_decompress(struct list_head *ws, const u8 *data_in,
157cb4c9198SDavid Sterba 		struct page *dest_page, unsigned long start_byte, size_t srclen,
158cb4c9198SDavid Sterba 		size_t destlen);
159cb4c9198SDavid Sterba struct list_head *lzo_alloc_workspace(unsigned int level);
160cb4c9198SDavid Sterba void lzo_free_workspace(struct list_head *ws);
161cb4c9198SDavid Sterba 
162cb4c9198SDavid Sterba int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
163cb4c9198SDavid Sterba 		u64 start, struct page **pages, unsigned long *out_pages,
164cb4c9198SDavid Sterba 		unsigned long *total_in, unsigned long *total_out);
165cb4c9198SDavid Sterba int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb);
1663e09b5b2SDavid Sterba int zstd_decompress(struct list_head *ws, const u8 *data_in,
167cb4c9198SDavid Sterba 		struct page *dest_page, unsigned long start_byte, size_t srclen,
168cb4c9198SDavid Sterba 		size_t destlen);
169cb4c9198SDavid Sterba void zstd_init_workspace_manager(void);
170cb4c9198SDavid Sterba void zstd_cleanup_workspace_manager(void);
171cb4c9198SDavid Sterba struct list_head *zstd_alloc_workspace(unsigned int level);
172cb4c9198SDavid Sterba void zstd_free_workspace(struct list_head *ws);
173cb4c9198SDavid Sterba struct list_head *zstd_get_workspace(unsigned int level);
174cb4c9198SDavid Sterba void zstd_put_workspace(struct list_head *ws);
175cb4c9198SDavid Sterba 
176c8b97818SChris Mason #endif
177