147e4937aSGao Xiang /* SPDX-License-Identifier: GPL-2.0-only */
247e4937aSGao Xiang /*
347e4937aSGao Xiang * Copyright (C) 2019 HUAWEI, Inc.
4592e7cd0SAlexander A. Klimov * https://www.huawei.com/
547e4937aSGao Xiang */
647e4937aSGao Xiang #ifndef __EROFS_FS_COMPRESS_H
747e4937aSGao Xiang #define __EROFS_FS_COMPRESS_H
847e4937aSGao Xiang
947e4937aSGao Xiang #include "internal.h"
1047e4937aSGao Xiang
1147e4937aSGao Xiang struct z_erofs_decompress_req {
1247e4937aSGao Xiang struct super_block *sb;
1347e4937aSGao Xiang struct page **in, **out;
1447e4937aSGao Xiang
1510e5f6e4SGao Xiang unsigned short pageofs_in, pageofs_out;
1647e4937aSGao Xiang unsigned int inputsize, outputsize;
1747e4937aSGao Xiang
1847e4937aSGao Xiang /* indicate the algorithm will be used for decompression */
1947e4937aSGao Xiang unsigned int alg;
20267f2492SGao Xiang bool inplace_io, partial_decoding, fillgaps;
2147e4937aSGao Xiang };
2247e4937aSGao Xiang
23622ceaddSGao Xiang struct z_erofs_decompressor {
24*586814edSGao Xiang int (*config)(struct super_block *sb, struct erofs_super_block *dsb,
25*586814edSGao Xiang void *data, int size);
26622ceaddSGao Xiang int (*decompress)(struct z_erofs_decompress_req *rq,
27eaa9172aSGao Xiang struct page **pagepool);
28622ceaddSGao Xiang char *name;
29622ceaddSGao Xiang };
30622ceaddSGao Xiang
316aaa7b06SGao Xiang /* some special page->private (unsigned long, see below) */
326aaa7b06SGao Xiang #define Z_EROFS_SHORTLIVED_PAGE (-1UL << 2)
331825c8d7SGao Xiang #define Z_EROFS_PREALLOCATED_PAGE (-2UL << 2)
346aaa7b06SGao Xiang
3547e4937aSGao Xiang /*
366aaa7b06SGao Xiang * For all pages in a pcluster, page->private should be one of
376aaa7b06SGao Xiang * Type Last 2bits page->private
386aaa7b06SGao Xiang * short-lived page 00 Z_EROFS_SHORTLIVED_PAGE
391825c8d7SGao Xiang * preallocated page (tryalloc) 00 Z_EROFS_PREALLOCATED_PAGE
406aaa7b06SGao Xiang * cached/managed page 00 pointer to z_erofs_pcluster
416aaa7b06SGao Xiang * online page (file-backed, 01/10/11 sub-index << 2 | count
426aaa7b06SGao Xiang * some pages can be used for inplace I/O)
436aaa7b06SGao Xiang *
446aaa7b06SGao Xiang * page->mapping should be one of
456aaa7b06SGao Xiang * Type page->mapping
466aaa7b06SGao Xiang * short-lived page NULL
471825c8d7SGao Xiang * preallocated page NULL
486aaa7b06SGao Xiang * cached/managed page non-NULL or NULL (invalidated/truncated page)
496aaa7b06SGao Xiang * online page non-NULL
506aaa7b06SGao Xiang *
516aaa7b06SGao Xiang * For all managed pages, PG_private should be set with 1 extra refcount,
526aaa7b06SGao Xiang * which is used for page reclaim / migration.
5347e4937aSGao Xiang */
5447e4937aSGao Xiang
556aaa7b06SGao Xiang /*
566aaa7b06SGao Xiang * short-lived pages are pages directly from buddy system with specific
576aaa7b06SGao Xiang * page->private (no need to set PagePrivate since these are non-LRU /
586aaa7b06SGao Xiang * non-movable pages and bypass reclaim / migration code).
596aaa7b06SGao Xiang */
z_erofs_is_shortlived_page(struct page * page)606aaa7b06SGao Xiang static inline bool z_erofs_is_shortlived_page(struct page *page)
6147e4937aSGao Xiang {
626aaa7b06SGao Xiang if (page->private != Z_EROFS_SHORTLIVED_PAGE)
6347e4937aSGao Xiang return false;
6447e4937aSGao Xiang
656aaa7b06SGao Xiang DBG_BUGON(page->mapping);
666aaa7b06SGao Xiang return true;
676aaa7b06SGao Xiang }
686aaa7b06SGao Xiang
z_erofs_put_shortlivedpage(struct page ** pagepool,struct page * page)69eaa9172aSGao Xiang static inline bool z_erofs_put_shortlivedpage(struct page **pagepool,
706aaa7b06SGao Xiang struct page *page)
716aaa7b06SGao Xiang {
726aaa7b06SGao Xiang if (!z_erofs_is_shortlived_page(page))
736aaa7b06SGao Xiang return false;
746aaa7b06SGao Xiang
756aaa7b06SGao Xiang /* short-lived pages should not be used by others at the same time */
766aaa7b06SGao Xiang if (page_ref_count(page) > 1) {
7747e4937aSGao Xiang put_page(page);
786aaa7b06SGao Xiang } else {
796aaa7b06SGao Xiang /* follow the pcluster rule above. */
80eaa9172aSGao Xiang erofs_pagepool_add(pagepool, page);
816aaa7b06SGao Xiang }
8247e4937aSGao Xiang return true;
8347e4937aSGao Xiang }
8447e4937aSGao Xiang
85622ceaddSGao Xiang #define MNGD_MAPPING(sbi) ((sbi)->managed_cache->i_mapping)
erofs_page_is_managed(const struct erofs_sb_info * sbi,struct page * page)86622ceaddSGao Xiang static inline bool erofs_page_is_managed(const struct erofs_sb_info *sbi,
87622ceaddSGao Xiang struct page *page)
88622ceaddSGao Xiang {
89622ceaddSGao Xiang return page->mapping == MNGD_MAPPING(sbi);
90622ceaddSGao Xiang }
91622ceaddSGao Xiang
9210e5f6e4SGao Xiang int z_erofs_fixup_insize(struct z_erofs_decompress_req *rq, const char *padbuf,
9310e5f6e4SGao Xiang unsigned int padbufsize);
94597e2953SYue Hu extern const struct z_erofs_decompressor erofs_decompressors[];
9547e4937aSGao Xiang
96622ceaddSGao Xiang /* prototypes for specific algorithms */
97*586814edSGao Xiang int z_erofs_load_lzma_config(struct super_block *sb,
98*586814edSGao Xiang struct erofs_super_block *dsb, void *data, int size);
99*586814edSGao Xiang int z_erofs_load_deflate_config(struct super_block *sb,
100*586814edSGao Xiang struct erofs_super_block *dsb, void *data, int size);
101622ceaddSGao Xiang int z_erofs_lzma_decompress(struct z_erofs_decompress_req *rq,
102eaa9172aSGao Xiang struct page **pagepool);
103ffa09b3bSGao Xiang int z_erofs_deflate_decompress(struct z_erofs_decompress_req *rq,
104ffa09b3bSGao Xiang struct page **pagepool);
10547e4937aSGao Xiang #endif
106