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