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