blob: cc605f7b23fbf3bf0f0ed56ac625daa9982b149a [file] [log] [blame]
David Sterba9888c342018-04-03 19:16:55 +02001/* SPDX-License-Identifier: GPL-2.0 */
Chris Masonc8b97812008-10-29 14:49:59 -04002/*
3 * Copyright (C) 2008 Oracle. All rights reserved.
Chris Masonc8b97812008-10-29 14:49:59 -04004 */
5
David Sterba9888c342018-04-03 19:16:55 +02006#ifndef BTRFS_COMPRESSION_H
7#define BTRFS_COMPRESSION_H
Chris Masonc8b97812008-10-29 14:49:59 -04008
David Sterbaff763862017-02-14 19:30:39 +01009/*
10 * We want to make sure that amount of RAM required to uncompress an extent is
11 * reasonable, so we limit the total size in ram of a compressed extent to
12 * 128k. This is a crucial number because it also controls how easily we can
13 * spread reads across cpus for decompression.
14 *
15 * We also want to make sure the amount of IO required to do a random read is
16 * reasonably small, so we limit the size of a compressed extent to 128k.
17 */
18
19/* Maximum length of compressed data stored on disk */
20#define BTRFS_MAX_COMPRESSED (SZ_128K)
21/* Maximum size of data before compression */
22#define BTRFS_MAX_UNCOMPRESSED (SZ_128K)
23
Qu Wenruoeae8d822017-11-06 10:43:18 +080024#define BTRFS_ZLIB_DEFAULT_LEVEL 3
25
Anand Jaine1ddce72017-05-26 15:44:59 +080026struct compressed_bio {
27 /* number of bios pending for this compressed extent */
28 refcount_t pending_bios;
29
30 /* the pages with the compressed data on them */
31 struct page **compressed_pages;
32
33 /* inode that owns this data */
34 struct inode *inode;
35
36 /* starting offset in the inode for our pages */
37 u64 start;
38
39 /* number of bytes in the inode we're working on */
40 unsigned long len;
41
42 /* number of bytes on disk */
43 unsigned long compressed_len;
44
45 /* the compression algorithm for this bio */
46 int compress_type;
47
48 /* number of compressed pages in the array */
49 unsigned long nr_pages;
50
51 /* IO errors */
52 int errors;
53 int mirror_num;
54
55 /* for reads, this is the bio we are copying the data into */
56 struct bio *orig_bio;
57
58 /*
59 * the start of a variable length array of checksums only
60 * used by reads
61 */
62 u32 sums;
63};
64
Liu Bof5c29bd2017-11-02 17:21:50 -060065void __init btrfs_init_compress(void);
David Sterbae67c7182018-02-19 17:24:18 +010066void __cold btrfs_exit_compress(void);
Li Zefan261507a02010-12-17 14:21:50 +080067
David Sterbaf51d2b52017-09-15 17:36:57 +020068int btrfs_compress_pages(unsigned int type_level, struct address_space *mapping,
David Sterba38c31462017-02-14 19:04:07 +010069 u64 start, struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +080070 unsigned long *out_pages,
71 unsigned long *total_in,
David Sterbae5d74902017-02-14 19:45:05 +010072 unsigned long *total_out);
Li Zefan261507a02010-12-17 14:21:50 +080073int btrfs_decompress(int type, unsigned char *data_in, struct page *dest_page,
74 unsigned long start_byte, size_t srclen, size_t destlen);
David Sterba14a33572017-02-14 17:58:04 +010075int btrfs_decompress_buf2page(const char *buf, unsigned long buf_start,
Li Zefan3a39c182010-11-08 15:22:19 +080076 unsigned long total_out, u64 disk_start,
Christoph Hellwig974b1ad2016-11-25 09:07:46 +010077 struct bio *bio);
Li Zefan261507a02010-12-17 14:21:50 +080078
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020079blk_status_t btrfs_submit_compressed_write(struct inode *inode, u64 start,
Chris Masonc8b97812008-10-29 14:49:59 -040080 unsigned long len, u64 disk_start,
81 unsigned long compressed_len,
82 struct page **compressed_pages,
Liu Bof82b7352017-10-23 23:18:16 -060083 unsigned long nr_pages,
84 unsigned int write_flags);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020085blk_status_t btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
Chris Masonc8b97812008-10-29 14:49:59 -040086 int mirror_num, unsigned long bio_flags);
Anand Jainebb87652016-03-10 17:26:59 +080087
David Sterbaf51d2b52017-09-15 17:36:57 +020088unsigned btrfs_compress_str2level(const char *str);
89
Anand Jainebb87652016-03-10 17:26:59 +080090enum btrfs_compression_type {
91 BTRFS_COMPRESS_NONE = 0,
92 BTRFS_COMPRESS_ZLIB = 1,
93 BTRFS_COMPRESS_LZO = 2,
Nick Terrell5c1aab12017-08-09 19:39:02 -070094 BTRFS_COMPRESS_ZSTD = 3,
95 BTRFS_COMPRESS_TYPES = 3,
Anand Jainebb87652016-03-10 17:26:59 +080096};
97
Li Zefan261507a02010-12-17 14:21:50 +080098struct btrfs_compress_op {
99 struct list_head *(*alloc_workspace)(void);
100
101 void (*free_workspace)(struct list_head *workspace);
102
103 int (*compress_pages)(struct list_head *workspace,
104 struct address_space *mapping,
David Sterba38c31462017-02-14 19:04:07 +0100105 u64 start,
Li Zefan261507a02010-12-17 14:21:50 +0800106 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800107 unsigned long *out_pages,
108 unsigned long *total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100109 unsigned long *total_out);
Li Zefan261507a02010-12-17 14:21:50 +0800110
Christoph Hellwig974b1ad2016-11-25 09:07:46 +0100111 int (*decompress_bio)(struct list_head *workspace,
Anand Jaine1ddce72017-05-26 15:44:59 +0800112 struct compressed_bio *cb);
Li Zefan261507a02010-12-17 14:21:50 +0800113
114 int (*decompress)(struct list_head *workspace,
115 unsigned char *data_in,
116 struct page *dest_page,
117 unsigned long start_byte,
118 size_t srclen, size_t destlen);
David Sterbaf51d2b52017-09-15 17:36:57 +0200119
120 void (*set_level)(struct list_head *ws, unsigned int type);
Li Zefan261507a02010-12-17 14:21:50 +0800121};
122
David Sterbae8c9f182015-01-02 18:23:10 +0100123extern const struct btrfs_compress_op btrfs_zlib_compress;
124extern const struct btrfs_compress_op btrfs_lzo_compress;
Nick Terrell5c1aab12017-08-09 19:39:02 -0700125extern const struct btrfs_compress_op btrfs_zstd_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800126
David Sterbae128f9c2017-10-31 17:24:26 +0100127const char* btrfs_compress_type2str(enum btrfs_compression_type type);
128
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300129int btrfs_compress_heuristic(struct inode *inode, u64 start, u64 end);
130
Chris Masonc8b97812008-10-29 14:49:59 -0400131#endif