xref: /linux/fs/bcachefs/compress.h (revision 1f20a5769446a1acae67ac9e63d07a594829a789)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_COMPRESS_H
3 #define _BCACHEFS_COMPRESS_H
4 
5 #include "extents_types.h"
6 
7 static const unsigned __bch2_compression_opt_to_type[] = {
8 #define x(t, n) [BCH_COMPRESSION_OPT_##t] = BCH_COMPRESSION_TYPE_##t,
9 	BCH_COMPRESSION_OPTS()
10 #undef x
11 };
12 
13 struct bch_compression_opt {
14 	u8		type:4,
15 			level:4;
16 };
17 
18 static inline struct bch_compression_opt __bch2_compression_decode(unsigned v)
19 {
20 	return (struct bch_compression_opt) {
21 		.type	= v & 15,
22 		.level	= v >> 4,
23 	};
24 }
25 
26 static inline bool bch2_compression_opt_valid(unsigned v)
27 {
28 	struct bch_compression_opt opt = __bch2_compression_decode(v);
29 
30 	return opt.type < ARRAY_SIZE(__bch2_compression_opt_to_type) && !(!opt.type && opt.level);
31 }
32 
33 static inline struct bch_compression_opt bch2_compression_decode(unsigned v)
34 {
35 	return bch2_compression_opt_valid(v)
36 		? __bch2_compression_decode(v)
37 		: (struct bch_compression_opt) { 0 };
38 }
39 
40 static inline unsigned bch2_compression_encode(struct bch_compression_opt opt)
41 {
42 	return opt.type|(opt.level << 4);
43 }
44 
45 static inline enum bch_compression_type bch2_compression_opt_to_type(unsigned v)
46 {
47 	return __bch2_compression_opt_to_type[bch2_compression_decode(v).type];
48 }
49 
50 int bch2_bio_uncompress_inplace(struct bch_fs *, struct bio *,
51 				struct bch_extent_crc_unpacked *);
52 int bch2_bio_uncompress(struct bch_fs *, struct bio *, struct bio *,
53 		       struct bvec_iter, struct bch_extent_crc_unpacked);
54 unsigned bch2_bio_compress(struct bch_fs *, struct bio *, size_t *,
55 			   struct bio *, size_t *, unsigned);
56 
57 int bch2_check_set_has_compressed_data(struct bch_fs *, unsigned);
58 void bch2_fs_compress_exit(struct bch_fs *);
59 int bch2_fs_compress_init(struct bch_fs *);
60 
61 void bch2_compression_opt_to_text(struct printbuf *, u64);
62 
63 int bch2_opt_compression_parse(struct bch_fs *, const char *, u64 *, struct printbuf *);
64 void bch2_opt_compression_to_text(struct printbuf *, struct bch_fs *, struct bch_sb *, u64);
65 int bch2_opt_compression_validate(u64, struct printbuf *);
66 
67 #define bch2_opt_compression (struct bch_opt_fn) {		\
68 	.parse		= bch2_opt_compression_parse,		\
69 	.to_text	= bch2_opt_compression_to_text,		\
70 	.validate	= bch2_opt_compression_validate,	\
71 }
72 
73 #endif /* _BCACHEFS_COMPRESS_H */
74