compression.h 6.0 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. struct btrfs_inode;
  9. /*
  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. /* Maximum length of compressed data stored on disk */
  19. #define BTRFS_MAX_COMPRESSED (SZ_128K)
  20. static_assert((BTRFS_MAX_COMPRESSED % PAGE_SIZE) == 0);
  21. /* Maximum size of data before compression */
  22. #define BTRFS_MAX_UNCOMPRESSED (SZ_128K)
  23. #define BTRFS_ZLIB_DEFAULT_LEVEL 3
  24. struct compressed_bio {
  25. /* Number of outstanding bios */
  26. refcount_t pending_ios;
  27. /* Number of compressed pages in the array */
  28. unsigned int nr_pages;
  29. /* the pages with the compressed data on them */
  30. struct page **compressed_pages;
  31. /* inode that owns this data */
  32. struct inode *inode;
  33. /* starting offset in the inode for our pages */
  34. u64 start;
  35. /* Number of bytes in the inode we're working on */
  36. unsigned int len;
  37. /* Number of bytes on disk */
  38. unsigned int compressed_len;
  39. /* The compression algorithm for this bio */
  40. u8 compress_type;
  41. /* Whether this is a write for writeback. */
  42. bool writeback;
  43. /* IO errors */
  44. blk_status_t status;
  45. union {
  46. /* For reads, this is the bio we are copying the data into */
  47. struct bio *orig_bio;
  48. struct work_struct write_end_work;
  49. };
  50. };
  51. static inline unsigned int btrfs_compress_type(unsigned int type_level)
  52. {
  53. return (type_level & 0xF);
  54. }
  55. static inline unsigned int btrfs_compress_level(unsigned int type_level)
  56. {
  57. return ((type_level & 0xF0) >> 4);
  58. }
  59. void __init btrfs_init_compress(void);
  60. void __cold btrfs_exit_compress(void);
  61. int btrfs_compress_pages(unsigned int type_level, struct address_space *mapping,
  62. u64 start, struct page **pages,
  63. unsigned long *out_pages,
  64. unsigned long *total_in,
  65. unsigned long *total_out);
  66. int btrfs_decompress(int type, unsigned char *data_in, struct page *dest_page,
  67. unsigned long start_byte, size_t srclen, size_t destlen);
  68. int btrfs_decompress_buf2page(const char *buf, u32 buf_len,
  69. struct compressed_bio *cb, u32 decompressed);
  70. blk_status_t btrfs_submit_compressed_write(struct btrfs_inode *inode, u64 start,
  71. unsigned int len, u64 disk_start,
  72. unsigned int compressed_len,
  73. struct page **compressed_pages,
  74. unsigned int nr_pages,
  75. blk_opf_t write_flags,
  76. struct cgroup_subsys_state *blkcg_css,
  77. bool writeback);
  78. void btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
  79. int mirror_num);
  80. unsigned int btrfs_compress_str2level(unsigned int type, const char *str);
  81. enum btrfs_compression_type {
  82. BTRFS_COMPRESS_NONE = 0,
  83. BTRFS_COMPRESS_ZLIB = 1,
  84. BTRFS_COMPRESS_LZO = 2,
  85. BTRFS_COMPRESS_ZSTD = 3,
  86. BTRFS_NR_COMPRESS_TYPES = 4,
  87. };
  88. struct workspace_manager {
  89. struct list_head idle_ws;
  90. spinlock_t ws_lock;
  91. /* Number of free workspaces */
  92. int free_ws;
  93. /* Total number of allocated workspaces */
  94. atomic_t total_ws;
  95. /* Waiters for a free workspace */
  96. wait_queue_head_t ws_wait;
  97. };
  98. struct list_head *btrfs_get_workspace(int type, unsigned int level);
  99. void btrfs_put_workspace(int type, struct list_head *ws);
  100. struct btrfs_compress_op {
  101. struct workspace_manager *workspace_manager;
  102. /* Maximum level supported by the compression algorithm */
  103. unsigned int max_level;
  104. unsigned int default_level;
  105. };
  106. /* The heuristic workspaces are managed via the 0th workspace manager */
  107. #define BTRFS_NR_WORKSPACE_MANAGERS BTRFS_NR_COMPRESS_TYPES
  108. extern const struct btrfs_compress_op btrfs_heuristic_compress;
  109. extern const struct btrfs_compress_op btrfs_zlib_compress;
  110. extern const struct btrfs_compress_op btrfs_lzo_compress;
  111. extern const struct btrfs_compress_op btrfs_zstd_compress;
  112. const char* btrfs_compress_type2str(enum btrfs_compression_type type);
  113. bool btrfs_compress_is_valid_type(const char *str, size_t len);
  114. int btrfs_compress_heuristic(struct inode *inode, u64 start, u64 end);
  115. int zlib_compress_pages(struct list_head *ws, struct address_space *mapping,
  116. u64 start, struct page **pages, unsigned long *out_pages,
  117. unsigned long *total_in, unsigned long *total_out);
  118. int zlib_decompress_bio(struct list_head *ws, struct compressed_bio *cb);
  119. int zlib_decompress(struct list_head *ws, unsigned char *data_in,
  120. struct page *dest_page, unsigned long start_byte, size_t srclen,
  121. size_t destlen);
  122. struct list_head *zlib_alloc_workspace(unsigned int level);
  123. void zlib_free_workspace(struct list_head *ws);
  124. struct list_head *zlib_get_workspace(unsigned int level);
  125. int lzo_compress_pages(struct list_head *ws, struct address_space *mapping,
  126. u64 start, struct page **pages, unsigned long *out_pages,
  127. unsigned long *total_in, unsigned long *total_out);
  128. int lzo_decompress_bio(struct list_head *ws, struct compressed_bio *cb);
  129. int lzo_decompress(struct list_head *ws, unsigned char *data_in,
  130. struct page *dest_page, unsigned long start_byte, size_t srclen,
  131. size_t destlen);
  132. struct list_head *lzo_alloc_workspace(unsigned int level);
  133. void lzo_free_workspace(struct list_head *ws);
  134. int zstd_compress_pages(struct list_head *ws, struct address_space *mapping,
  135. u64 start, struct page **pages, unsigned long *out_pages,
  136. unsigned long *total_in, unsigned long *total_out);
  137. int zstd_decompress_bio(struct list_head *ws, struct compressed_bio *cb);
  138. int zstd_decompress(struct list_head *ws, unsigned char *data_in,
  139. struct page *dest_page, unsigned long start_byte, size_t srclen,
  140. size_t destlen);
  141. void zstd_init_workspace_manager(void);
  142. void zstd_cleanup_workspace_manager(void);
  143. struct list_head *zstd_alloc_workspace(unsigned int level);
  144. void zstd_free_workspace(struct list_head *ws);
  145. struct list_head *zstd_get_workspace(unsigned int level);
  146. void zstd_put_workspace(struct list_head *ws);
  147. #endif