compress.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2019 HUAWEI, Inc.
  4. * https://www.huawei.com/
  5. */
  6. #ifndef __EROFS_FS_COMPRESS_H
  7. #define __EROFS_FS_COMPRESS_H
  8. #include "internal.h"
  9. struct z_erofs_decompress_req {
  10. struct super_block *sb;
  11. struct page **in, **out;
  12. unsigned short pageofs_in, pageofs_out;
  13. unsigned int inputsize, outputsize;
  14. unsigned int alg; /* the algorithm for decompression */
  15. bool inplace_io, partial_decoding, fillgaps;
  16. gfp_t gfp; /* allocation flags for extra temporary buffers */
  17. };
  18. struct z_erofs_decompressor {
  19. int (*config)(struct super_block *sb, struct erofs_super_block *dsb,
  20. void *data, int size);
  21. int (*decompress)(struct z_erofs_decompress_req *rq,
  22. struct page **pagepool);
  23. int (*init)(void);
  24. void (*exit)(void);
  25. char *name;
  26. };
  27. /* some special page->private (unsigned long, see below) */
  28. #define Z_EROFS_SHORTLIVED_PAGE (-1UL << 2)
  29. #define Z_EROFS_PREALLOCATED_PAGE (-2UL << 2)
  30. /*
  31. * For all pages in a pcluster, page->private should be one of
  32. * Type Last 2bits page->private
  33. * short-lived page 00 Z_EROFS_SHORTLIVED_PAGE
  34. * preallocated page (tryalloc) 00 Z_EROFS_PREALLOCATED_PAGE
  35. * cached/managed page 00 pointer to z_erofs_pcluster
  36. * online page (file-backed, 01/10/11 sub-index << 2 | count
  37. * some pages can be used for inplace I/O)
  38. *
  39. * page->mapping should be one of
  40. * Type page->mapping
  41. * short-lived page NULL
  42. * preallocated page NULL
  43. * cached/managed page non-NULL or NULL (invalidated/truncated page)
  44. * online page non-NULL
  45. *
  46. * For all managed pages, PG_private should be set with 1 extra refcount,
  47. * which is used for page reclaim / migration.
  48. */
  49. /*
  50. * Currently, short-lived pages are pages directly from buddy system
  51. * with specific page->private (Z_EROFS_SHORTLIVED_PAGE).
  52. * In the future world of Memdescs, it should be type 0 (Misc) memory
  53. * which type can be checked with a new helper.
  54. */
  55. static inline bool z_erofs_is_shortlived_page(struct page *page)
  56. {
  57. return page->private == Z_EROFS_SHORTLIVED_PAGE;
  58. }
  59. static inline bool z_erofs_put_shortlivedpage(struct page **pagepool,
  60. struct page *page)
  61. {
  62. if (!z_erofs_is_shortlived_page(page))
  63. return false;
  64. erofs_pagepool_add(pagepool, page);
  65. return true;
  66. }
  67. extern const struct z_erofs_decompressor z_erofs_lzma_decomp;
  68. extern const struct z_erofs_decompressor z_erofs_deflate_decomp;
  69. extern const struct z_erofs_decompressor z_erofs_zstd_decomp;
  70. extern const struct z_erofs_decompressor *z_erofs_decomp[];
  71. struct z_erofs_stream_dctx {
  72. struct z_erofs_decompress_req *rq;
  73. unsigned int inpages, outpages; /* # of {en,de}coded pages */
  74. int no, ni; /* the current {en,de}coded page # */
  75. unsigned int avail_out; /* remaining bytes in the decoded buffer */
  76. unsigned int inbuf_pos, inbuf_sz;
  77. /* current status of the encoded buffer */
  78. u8 *kin, *kout; /* buffer mapped pointers */
  79. void *bounce; /* bounce buffer for inplace I/Os */
  80. bool bounced; /* is the bounce buffer used now? */
  81. };
  82. int z_erofs_stream_switch_bufs(struct z_erofs_stream_dctx *dctx, void **dst,
  83. void **src, struct page **pgpl);
  84. int z_erofs_fixup_insize(struct z_erofs_decompress_req *rq, const char *padbuf,
  85. unsigned int padbufsize);
  86. int __init z_erofs_init_decompressor(void);
  87. void z_erofs_exit_decompressor(void);
  88. #endif