compression.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (C) 2008 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #ifndef __BTRFS_COMPRESSION_
  19. #define __BTRFS_COMPRESSION_
  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. /* Maximum size of data before compression */
  32. #define BTRFS_MAX_UNCOMPRESSED (SZ_128K)
  33. #define BTRFS_ZLIB_DEFAULT_LEVEL 3
  34. struct compressed_bio {
  35. /* number of bios pending for this compressed extent */
  36. refcount_t pending_bios;
  37. /* the pages with the compressed data on them */
  38. struct page **compressed_pages;
  39. /* inode that owns this data */
  40. struct inode *inode;
  41. /* starting offset in the inode for our pages */
  42. u64 start;
  43. /* number of bytes in the inode we're working on */
  44. unsigned long len;
  45. /* number of bytes on disk */
  46. unsigned long compressed_len;
  47. /* the compression algorithm for this bio */
  48. int compress_type;
  49. /* number of compressed pages in the array */
  50. unsigned long nr_pages;
  51. /* IO errors */
  52. int errors;
  53. int mirror_num;
  54. /* for reads, this is the bio we are copying the data into */
  55. struct bio *orig_bio;
  56. /*
  57. * the start of a variable length array of checksums only
  58. * used by reads
  59. */
  60. u32 sums;
  61. };
  62. void btrfs_init_compress(void);
  63. void btrfs_exit_compress(void);
  64. int btrfs_compress_pages(unsigned int type_level, struct address_space *mapping,
  65. u64 start, struct page **pages,
  66. unsigned long *out_pages,
  67. unsigned long *total_in,
  68. unsigned long *total_out);
  69. int btrfs_decompress(int type, unsigned char *data_in, struct page *dest_page,
  70. unsigned long start_byte, size_t srclen, size_t destlen);
  71. int btrfs_decompress_buf2page(const char *buf, unsigned long buf_start,
  72. unsigned long total_out, u64 disk_start,
  73. struct bio *bio);
  74. blk_status_t btrfs_submit_compressed_write(struct inode *inode, u64 start,
  75. unsigned long len, u64 disk_start,
  76. unsigned long compressed_len,
  77. struct page **compressed_pages,
  78. unsigned long nr_pages,
  79. unsigned int write_flags);
  80. blk_status_t btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
  81. int mirror_num, unsigned long bio_flags);
  82. unsigned btrfs_compress_str2level(const char *str);
  83. enum btrfs_compression_type {
  84. BTRFS_COMPRESS_NONE = 0,
  85. BTRFS_COMPRESS_ZLIB = 1,
  86. BTRFS_COMPRESS_LZO = 2,
  87. BTRFS_COMPRESS_ZSTD = 3,
  88. BTRFS_COMPRESS_TYPES = 3,
  89. };
  90. struct btrfs_compress_op {
  91. struct list_head *(*alloc_workspace)(void);
  92. void (*free_workspace)(struct list_head *workspace);
  93. int (*compress_pages)(struct list_head *workspace,
  94. struct address_space *mapping,
  95. u64 start,
  96. struct page **pages,
  97. unsigned long *out_pages,
  98. unsigned long *total_in,
  99. unsigned long *total_out);
  100. int (*decompress_bio)(struct list_head *workspace,
  101. struct compressed_bio *cb);
  102. int (*decompress)(struct list_head *workspace,
  103. unsigned char *data_in,
  104. struct page *dest_page,
  105. unsigned long start_byte,
  106. size_t srclen, size_t destlen);
  107. void (*set_level)(struct list_head *ws, unsigned int type);
  108. };
  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. int btrfs_compress_heuristic(struct inode *inode, u64 start, u64 end);
  113. #endif