compression.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. struct compressed_bio {
  34. /* number of bios pending for this compressed extent */
  35. refcount_t pending_bios;
  36. /* the pages with the compressed data on them */
  37. struct page **compressed_pages;
  38. /* inode that owns this data */
  39. struct inode *inode;
  40. /* starting offset in the inode for our pages */
  41. u64 start;
  42. /* number of bytes in the inode we're working on */
  43. unsigned long len;
  44. /* number of bytes on disk */
  45. unsigned long compressed_len;
  46. /* the compression algorithm for this bio */
  47. int compress_type;
  48. /* number of compressed pages in the array */
  49. unsigned long nr_pages;
  50. /* IO errors */
  51. int errors;
  52. int mirror_num;
  53. /* for reads, this is the bio we are copying the data into */
  54. struct bio *orig_bio;
  55. /*
  56. * the start of a variable length array of checksums only
  57. * used by reads
  58. */
  59. u32 sums;
  60. };
  61. void btrfs_init_compress(void);
  62. void btrfs_exit_compress(void);
  63. int btrfs_compress_pages(int type, struct address_space *mapping,
  64. u64 start, struct page **pages,
  65. unsigned long *out_pages,
  66. unsigned long *total_in,
  67. unsigned long *total_out);
  68. int btrfs_decompress(int type, unsigned char *data_in, struct page *dest_page,
  69. unsigned long start_byte, size_t srclen, size_t destlen);
  70. int btrfs_decompress_buf2page(const char *buf, unsigned long buf_start,
  71. unsigned long total_out, u64 disk_start,
  72. struct bio *bio);
  73. blk_status_t btrfs_submit_compressed_write(struct inode *inode, u64 start,
  74. unsigned long len, u64 disk_start,
  75. unsigned long compressed_len,
  76. struct page **compressed_pages,
  77. unsigned long nr_pages);
  78. blk_status_t btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
  79. int mirror_num, unsigned long bio_flags);
  80. enum btrfs_compression_type {
  81. BTRFS_COMPRESS_NONE = 0,
  82. BTRFS_COMPRESS_ZLIB = 1,
  83. BTRFS_COMPRESS_LZO = 2,
  84. BTRFS_COMPRESS_TYPES = 2,
  85. BTRFS_COMPRESS_LAST = 3,
  86. };
  87. struct btrfs_compress_op {
  88. struct list_head *(*alloc_workspace)(void);
  89. void (*free_workspace)(struct list_head *workspace);
  90. int (*compress_pages)(struct list_head *workspace,
  91. struct address_space *mapping,
  92. u64 start,
  93. struct page **pages,
  94. unsigned long *out_pages,
  95. unsigned long *total_in,
  96. unsigned long *total_out);
  97. int (*decompress_bio)(struct list_head *workspace,
  98. struct compressed_bio *cb);
  99. int (*decompress)(struct list_head *workspace,
  100. unsigned char *data_in,
  101. struct page *dest_page,
  102. unsigned long start_byte,
  103. size_t srclen, size_t destlen);
  104. };
  105. extern const struct btrfs_compress_op btrfs_zlib_compress;
  106. extern const struct btrfs_compress_op btrfs_lzo_compress;
  107. #endif