compression.h 3.8 KB

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