compression.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2008 Oracle. All rights reserved.
  4. */
  5. #ifndef BTRFS_COMPRESSION_H
  6. #define BTRFS_COMPRESSION_H
  7. #include <linux/sizes.h>
  8. #include <linux/mm.h>
  9. #include <linux/list.h>
  10. #include <linux/workqueue.h>
  11. #include <linux/wait.h>
  12. #include <linux/pagemap.h>
  13. #include "bio.h"
  14. #include "fs.h"
  15. #include "btrfs_inode.h"
  16. struct address_space;
  17. struct inode;
  18. struct btrfs_inode;
  19. struct btrfs_ordered_extent;
  20. /*
  21. * We want to make sure that amount of RAM required to uncompress an extent is
  22. * reasonable, so we limit the total size in ram of a compressed extent to
  23. * 128k. This is a crucial number because it also controls how easily we can
  24. * spread reads across cpus for decompression.
  25. *
  26. * We also want to make sure the amount of IO required to do a random read is
  27. * reasonably small, so we limit the size of a compressed extent to 128k.
  28. */
  29. /* Maximum length of compressed data stored on disk */
  30. #define BTRFS_MAX_COMPRESSED (SZ_128K)
  31. #define BTRFS_MAX_COMPRESSED_PAGES (BTRFS_MAX_COMPRESSED / PAGE_SIZE)
  32. static_assert((BTRFS_MAX_COMPRESSED % PAGE_SIZE) == 0);
  33. /* Maximum size of data before compression */
  34. #define BTRFS_MAX_UNCOMPRESSED (SZ_128K)
  35. #define BTRFS_ZLIB_DEFAULT_LEVEL 3
  36. struct compressed_bio {
  37. /* starting offset in the inode for our pages */
  38. u64 start;
  39. /* Number of bytes in the inode we're working on */
  40. unsigned int len;
  41. /* Number of bytes on disk */
  42. unsigned int compressed_len;
  43. /* The compression algorithm for this bio */
  44. u8 compress_type;
  45. /* Whether this is a write for writeback. */
  46. bool writeback;
  47. /* For reads, this is the bio we are copying the data into. */
  48. struct btrfs_bio *orig_bbio;
  49. /* Must be last. */
  50. struct btrfs_bio bbio;
  51. };
  52. static inline struct btrfs_fs_info *cb_to_fs_info(const struct compressed_bio *cb)
  53. {
  54. return cb->bbio.inode->root->fs_info;
  55. }
  56. /* @range_end must be exclusive. */
  57. static inline u32 btrfs_calc_input_length(struct folio *folio, u64 range_end, u64 cur)
  58. {
  59. /* @cur must be inside the folio. */
  60. ASSERT(folio_pos(folio) <= cur);
  61. ASSERT(cur < folio_next_pos(folio));
  62. return umin(range_end, folio_next_pos(folio)) - cur;
  63. }
  64. int btrfs_alloc_compress_wsm(struct btrfs_fs_info *fs_info);
  65. void btrfs_free_compress_wsm(struct btrfs_fs_info *fs_info);
  66. int __init btrfs_init_compress(void);
  67. void __cold btrfs_exit_compress(void);
  68. bool btrfs_compress_level_valid(unsigned int type, int level);
  69. int btrfs_decompress(int type, const u8 *data_in, struct folio *dest_folio,
  70. unsigned long dest_pgoff, size_t srclen, size_t destlen);
  71. int btrfs_decompress_buf2page(const char *buf, u32 buf_len,
  72. struct compressed_bio *cb, u32 decompressed);
  73. struct compressed_bio *btrfs_alloc_compressed_write(struct btrfs_inode *inode,
  74. u64 start, u64 len);
  75. void btrfs_submit_compressed_write(struct btrfs_ordered_extent *ordered,
  76. struct compressed_bio *cb);
  77. void btrfs_submit_compressed_read(struct btrfs_bio *bbio);
  78. int btrfs_compress_str2level(unsigned int type, const char *str, int *level_ret);
  79. struct folio *btrfs_alloc_compr_folio(struct btrfs_fs_info *fs_info);
  80. void btrfs_free_compr_folio(struct folio *folio);
  81. struct workspace_manager {
  82. struct list_head idle_ws;
  83. spinlock_t ws_lock;
  84. /* Number of free workspaces */
  85. int free_ws;
  86. /* Total number of allocated workspaces */
  87. atomic_t total_ws;
  88. /* Waiters for a free workspace */
  89. wait_queue_head_t ws_wait;
  90. };
  91. struct list_head *btrfs_get_workspace(struct btrfs_fs_info *fs_info, int type, int level);
  92. void btrfs_put_workspace(struct btrfs_fs_info *fs_info, int type, struct list_head *ws);
  93. struct btrfs_compress_levels {
  94. /* Maximum level supported by the compression algorithm */
  95. int min_level;
  96. int max_level;
  97. int default_level;
  98. };
  99. /* The heuristic workspaces are managed via the 0th workspace manager */
  100. #define BTRFS_NR_WORKSPACE_MANAGERS BTRFS_NR_COMPRESS_TYPES
  101. extern const struct btrfs_compress_levels btrfs_heuristic_compress;
  102. extern const struct btrfs_compress_levels btrfs_zlib_compress;
  103. extern const struct btrfs_compress_levels btrfs_lzo_compress;
  104. extern const struct btrfs_compress_levels btrfs_zstd_compress;
  105. const char* btrfs_compress_type2str(enum btrfs_compression_type type);
  106. bool btrfs_compress_is_valid_type(const char *str, size_t len);
  107. int btrfs_compress_heuristic(struct btrfs_inode *inode, u64 start, u64 end);
  108. int btrfs_compress_filemap_get_folio(struct address_space *mapping, u64 start,
  109. struct folio **in_folio_ret);
  110. struct compressed_bio *btrfs_compress_bio(struct btrfs_inode *inode,
  111. u64 start, u32 len, unsigned int type,
  112. int level, blk_opf_t write_flags);
  113. static inline void cleanup_compressed_bio(struct compressed_bio *cb)
  114. {
  115. struct bio *bio = &cb->bbio.bio;
  116. struct folio_iter fi;
  117. bio_for_each_folio_all(fi, bio)
  118. btrfs_free_compr_folio(fi.folio);
  119. bio_put(bio);
  120. }
  121. int zlib_compress_bio(struct list_head *ws, struct compressed_bio *cb);
  122. int zlib_decompress_bio(struct list_head *ws, struct compressed_bio *cb);
  123. int zlib_decompress(struct list_head *ws, const u8 *data_in,
  124. struct folio *dest_folio, unsigned long dest_pgoff, size_t srclen,
  125. size_t destlen);
  126. struct list_head *zlib_alloc_workspace(struct btrfs_fs_info *fs_info, unsigned int level);
  127. void zlib_free_workspace(struct list_head *ws);
  128. struct list_head *zlib_get_workspace(struct btrfs_fs_info *fs_info, unsigned int level);
  129. int lzo_compress_bio(struct list_head *ws, struct compressed_bio *cb);
  130. int lzo_decompress_bio(struct list_head *ws, struct compressed_bio *cb);
  131. int lzo_decompress(struct list_head *ws, const u8 *data_in,
  132. struct folio *dest_folio, unsigned long dest_pgoff, size_t srclen,
  133. size_t destlen);
  134. struct list_head *lzo_alloc_workspace(struct btrfs_fs_info *fs_info);
  135. void lzo_free_workspace(struct list_head *ws);
  136. int zstd_compress_bio(struct list_head *ws, struct compressed_bio *cb);
  137. int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb);
  138. int zstd_decompress(struct list_head *ws, const u8 *data_in,
  139. struct folio *dest_folio, unsigned long dest_pgoff, size_t srclen,
  140. size_t destlen);
  141. int zstd_alloc_workspace_manager(struct btrfs_fs_info *fs_info);
  142. void zstd_free_workspace_manager(struct btrfs_fs_info *fs_info);
  143. struct list_head *zstd_alloc_workspace(struct btrfs_fs_info *fs_info, int level);
  144. void zstd_free_workspace(struct list_head *ws);
  145. struct list_head *zstd_get_workspace(struct btrfs_fs_info *fs_info, int level);
  146. void zstd_put_workspace(struct btrfs_fs_info *fs_info, struct list_head *ws);
  147. #endif