compression.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. /*
  9. * We want to make sure that amount of RAM required to uncompress an extent is
  10. * reasonable, so we limit the total size in ram of a compressed extent to
  11. * 128k. This is a crucial number because it also controls how easily we can
  12. * spread reads across cpus for decompression.
  13. *
  14. * We also want to make sure the amount of IO required to do a random read is
  15. * reasonably small, so we limit the size of a compressed extent to 128k.
  16. */
  17. /* Maximum length of compressed data stored on disk */
  18. #define BTRFS_MAX_COMPRESSED (SZ_128K)
  19. /* Maximum size of data before compression */
  20. #define BTRFS_MAX_UNCOMPRESSED (SZ_128K)
  21. #define BTRFS_ZLIB_DEFAULT_LEVEL 3
  22. struct compressed_bio {
  23. /* number of bios pending for this compressed extent */
  24. refcount_t pending_bios;
  25. /* the pages with the compressed data on them */
  26. struct page **compressed_pages;
  27. /* inode that owns this data */
  28. struct inode *inode;
  29. /* starting offset in the inode for our pages */
  30. u64 start;
  31. /* number of bytes in the inode we're working on */
  32. unsigned long len;
  33. /* number of bytes on disk */
  34. unsigned long compressed_len;
  35. /* the compression algorithm for this bio */
  36. int compress_type;
  37. /* number of compressed pages in the array */
  38. unsigned long nr_pages;
  39. /* IO errors */
  40. int errors;
  41. int mirror_num;
  42. /* for reads, this is the bio we are copying the data into */
  43. struct bio *orig_bio;
  44. /*
  45. * the start of a variable length array of checksums only
  46. * used by reads
  47. */
  48. u32 sums;
  49. };
  50. void __init btrfs_init_compress(void);
  51. void __cold btrfs_exit_compress(void);
  52. int btrfs_compress_pages(unsigned int type_level, struct address_space *mapping,
  53. u64 start, struct page **pages,
  54. unsigned long *out_pages,
  55. unsigned long *total_in,
  56. unsigned long *total_out);
  57. int btrfs_decompress(int type, unsigned char *data_in, struct page *dest_page,
  58. unsigned long start_byte, size_t srclen, size_t destlen);
  59. int btrfs_decompress_buf2page(const char *buf, unsigned long buf_start,
  60. unsigned long total_out, u64 disk_start,
  61. struct bio *bio);
  62. blk_status_t btrfs_submit_compressed_write(struct inode *inode, u64 start,
  63. unsigned long len, u64 disk_start,
  64. unsigned long compressed_len,
  65. struct page **compressed_pages,
  66. unsigned long nr_pages,
  67. unsigned int write_flags);
  68. blk_status_t btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
  69. int mirror_num, unsigned long bio_flags);
  70. unsigned btrfs_compress_str2level(const char *str);
  71. enum btrfs_compression_type {
  72. BTRFS_COMPRESS_NONE = 0,
  73. BTRFS_COMPRESS_ZLIB = 1,
  74. BTRFS_COMPRESS_LZO = 2,
  75. BTRFS_COMPRESS_ZSTD = 3,
  76. BTRFS_COMPRESS_TYPES = 3,
  77. };
  78. struct btrfs_compress_op {
  79. struct list_head *(*alloc_workspace)(void);
  80. void (*free_workspace)(struct list_head *workspace);
  81. int (*compress_pages)(struct list_head *workspace,
  82. struct address_space *mapping,
  83. u64 start,
  84. struct page **pages,
  85. unsigned long *out_pages,
  86. unsigned long *total_in,
  87. unsigned long *total_out);
  88. int (*decompress_bio)(struct list_head *workspace,
  89. struct compressed_bio *cb);
  90. int (*decompress)(struct list_head *workspace,
  91. unsigned char *data_in,
  92. struct page *dest_page,
  93. unsigned long start_byte,
  94. size_t srclen, size_t destlen);
  95. void (*set_level)(struct list_head *ws, unsigned int type);
  96. };
  97. extern const struct btrfs_compress_op btrfs_zlib_compress;
  98. extern const struct btrfs_compress_op btrfs_lzo_compress;
  99. extern const struct btrfs_compress_op btrfs_zstd_compress;
  100. const char* btrfs_compress_type2str(enum btrfs_compression_type type);
  101. bool btrfs_compress_is_valid_type(const char *str, size_t len);
  102. int btrfs_compress_heuristic(struct inode *inode, u64 start, u64 end);
  103. #endif