Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Copyright (C) 2008 Oracle.  All rights reserved.
0004  */
0005 
0006 #ifndef BTRFS_COMPRESSION_H
0007 #define BTRFS_COMPRESSION_H
0008 
0009 #include <linux/sizes.h>
0010 
0011 struct btrfs_inode;
0012 
0013 /*
0014  * We want to make sure that amount of RAM required to uncompress an extent is
0015  * reasonable, so we limit the total size in ram of a compressed extent to
0016  * 128k.  This is a crucial number because it also controls how easily we can
0017  * spread reads across cpus for decompression.
0018  *
0019  * We also want to make sure the amount of IO required to do a random read is
0020  * reasonably small, so we limit the size of a compressed extent to 128k.
0021  */
0022 
0023 /* Maximum length of compressed data stored on disk */
0024 #define BTRFS_MAX_COMPRESSED        (SZ_128K)
0025 static_assert((BTRFS_MAX_COMPRESSED % PAGE_SIZE) == 0);
0026 
0027 /* Maximum size of data before compression */
0028 #define BTRFS_MAX_UNCOMPRESSED      (SZ_128K)
0029 
0030 #define BTRFS_ZLIB_DEFAULT_LEVEL        3
0031 
0032 struct compressed_bio {
0033     /* Number of outstanding bios */
0034     refcount_t pending_ios;
0035 
0036     /* Number of compressed pages in the array */
0037     unsigned int nr_pages;
0038 
0039     /* the pages with the compressed data on them */
0040     struct page **compressed_pages;
0041 
0042     /* inode that owns this data */
0043     struct inode *inode;
0044 
0045     /* starting offset in the inode for our pages */
0046     u64 start;
0047 
0048     /* Number of bytes in the inode we're working on */
0049     unsigned int len;
0050 
0051     /* Number of bytes on disk */
0052     unsigned int compressed_len;
0053 
0054     /* The compression algorithm for this bio */
0055     u8 compress_type;
0056 
0057     /* Whether this is a write for writeback. */
0058     bool writeback;
0059 
0060     /* IO errors */
0061     blk_status_t status;
0062 
0063     union {
0064         /* For reads, this is the bio we are copying the data into */
0065         struct bio *orig_bio;
0066         struct work_struct write_end_work;
0067     };
0068 };
0069 
0070 static inline unsigned int btrfs_compress_type(unsigned int type_level)
0071 {
0072     return (type_level & 0xF);
0073 }
0074 
0075 static inline unsigned int btrfs_compress_level(unsigned int type_level)
0076 {
0077     return ((type_level & 0xF0) >> 4);
0078 }
0079 
0080 void __init btrfs_init_compress(void);
0081 void __cold btrfs_exit_compress(void);
0082 
0083 int btrfs_compress_pages(unsigned int type_level, struct address_space *mapping,
0084              u64 start, struct page **pages,
0085              unsigned long *out_pages,
0086              unsigned long *total_in,
0087              unsigned long *total_out);
0088 int btrfs_decompress(int type, unsigned char *data_in, struct page *dest_page,
0089              unsigned long start_byte, size_t srclen, size_t destlen);
0090 int btrfs_decompress_buf2page(const char *buf, u32 buf_len,
0091                   struct compressed_bio *cb, u32 decompressed);
0092 
0093 blk_status_t btrfs_submit_compressed_write(struct btrfs_inode *inode, u64 start,
0094                   unsigned int len, u64 disk_start,
0095                   unsigned int compressed_len,
0096                   struct page **compressed_pages,
0097                   unsigned int nr_pages,
0098                   blk_opf_t write_flags,
0099                   struct cgroup_subsys_state *blkcg_css,
0100                   bool writeback);
0101 void btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
0102                   int mirror_num);
0103 
0104 unsigned int btrfs_compress_str2level(unsigned int type, const char *str);
0105 
0106 enum btrfs_compression_type {
0107     BTRFS_COMPRESS_NONE  = 0,
0108     BTRFS_COMPRESS_ZLIB  = 1,
0109     BTRFS_COMPRESS_LZO   = 2,
0110     BTRFS_COMPRESS_ZSTD  = 3,
0111     BTRFS_NR_COMPRESS_TYPES = 4,
0112 };
0113 
0114 struct workspace_manager {
0115     struct list_head idle_ws;
0116     spinlock_t ws_lock;
0117     /* Number of free workspaces */
0118     int free_ws;
0119     /* Total number of allocated workspaces */
0120     atomic_t total_ws;
0121     /* Waiters for a free workspace */
0122     wait_queue_head_t ws_wait;
0123 };
0124 
0125 struct list_head *btrfs_get_workspace(int type, unsigned int level);
0126 void btrfs_put_workspace(int type, struct list_head *ws);
0127 
0128 struct btrfs_compress_op {
0129     struct workspace_manager *workspace_manager;
0130     /* Maximum level supported by the compression algorithm */
0131     unsigned int max_level;
0132     unsigned int default_level;
0133 };
0134 
0135 /* The heuristic workspaces are managed via the 0th workspace manager */
0136 #define BTRFS_NR_WORKSPACE_MANAGERS BTRFS_NR_COMPRESS_TYPES
0137 
0138 extern const struct btrfs_compress_op btrfs_heuristic_compress;
0139 extern const struct btrfs_compress_op btrfs_zlib_compress;
0140 extern const struct btrfs_compress_op btrfs_lzo_compress;
0141 extern const struct btrfs_compress_op btrfs_zstd_compress;
0142 
0143 const char* btrfs_compress_type2str(enum btrfs_compression_type type);
0144 bool btrfs_compress_is_valid_type(const char *str, size_t len);
0145 
0146 int btrfs_compress_heuristic(struct inode *inode, u64 start, u64 end);
0147 
0148 int zlib_compress_pages(struct list_head *ws, struct address_space *mapping,
0149         u64 start, struct page **pages, unsigned long *out_pages,
0150         unsigned long *total_in, unsigned long *total_out);
0151 int zlib_decompress_bio(struct list_head *ws, struct compressed_bio *cb);
0152 int zlib_decompress(struct list_head *ws, unsigned char *data_in,
0153         struct page *dest_page, unsigned long start_byte, size_t srclen,
0154         size_t destlen);
0155 struct list_head *zlib_alloc_workspace(unsigned int level);
0156 void zlib_free_workspace(struct list_head *ws);
0157 struct list_head *zlib_get_workspace(unsigned int level);
0158 
0159 int lzo_compress_pages(struct list_head *ws, struct address_space *mapping,
0160         u64 start, struct page **pages, unsigned long *out_pages,
0161         unsigned long *total_in, unsigned long *total_out);
0162 int lzo_decompress_bio(struct list_head *ws, struct compressed_bio *cb);
0163 int lzo_decompress(struct list_head *ws, unsigned char *data_in,
0164         struct page *dest_page, unsigned long start_byte, size_t srclen,
0165         size_t destlen);
0166 struct list_head *lzo_alloc_workspace(unsigned int level);
0167 void lzo_free_workspace(struct list_head *ws);
0168 
0169 int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
0170         u64 start, struct page **pages, unsigned long *out_pages,
0171         unsigned long *total_in, unsigned long *total_out);
0172 int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb);
0173 int zstd_decompress(struct list_head *ws, unsigned char *data_in,
0174         struct page *dest_page, unsigned long start_byte, size_t srclen,
0175         size_t destlen);
0176 void zstd_init_workspace_manager(void);
0177 void zstd_cleanup_workspace_manager(void);
0178 struct list_head *zstd_alloc_workspace(unsigned int level);
0179 void zstd_free_workspace(struct list_head *ws);
0180 struct list_head *zstd_get_workspace(unsigned int level);
0181 void zstd_put_workspace(struct list_head *ws);
0182 
0183 #endif