xref: /openbmc/linux/fs/erofs/compress.h (revision 597e2953)
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 {
24622ceaddSGao Xiang 	int (*decompress)(struct z_erofs_decompress_req *rq,
25eaa9172aSGao Xiang 			  struct page **pagepool);
26622ceaddSGao Xiang 	char *name;
27622ceaddSGao Xiang };
28622ceaddSGao Xiang 
296aaa7b06SGao Xiang /* some special page->private (unsigned long, see below) */
306aaa7b06SGao Xiang #define Z_EROFS_SHORTLIVED_PAGE		(-1UL << 2)
311825c8d7SGao Xiang #define Z_EROFS_PREALLOCATED_PAGE	(-2UL << 2)
326aaa7b06SGao Xiang 
3347e4937aSGao Xiang /*
346aaa7b06SGao Xiang  * For all pages in a pcluster, page->private should be one of
356aaa7b06SGao Xiang  * Type                         Last 2bits      page->private
366aaa7b06SGao Xiang  * short-lived page             00              Z_EROFS_SHORTLIVED_PAGE
371825c8d7SGao Xiang  * preallocated page (tryalloc) 00              Z_EROFS_PREALLOCATED_PAGE
386aaa7b06SGao Xiang  * cached/managed page          00              pointer to z_erofs_pcluster
396aaa7b06SGao Xiang  * online page (file-backed,    01/10/11        sub-index << 2 | count
406aaa7b06SGao Xiang  *              some pages can be used for inplace I/O)
416aaa7b06SGao Xiang  *
426aaa7b06SGao Xiang  * page->mapping should be one of
436aaa7b06SGao Xiang  * Type                 page->mapping
446aaa7b06SGao Xiang  * short-lived page     NULL
451825c8d7SGao Xiang  * preallocated page    NULL
466aaa7b06SGao Xiang  * cached/managed page  non-NULL or NULL (invalidated/truncated page)
476aaa7b06SGao Xiang  * online page          non-NULL
486aaa7b06SGao Xiang  *
496aaa7b06SGao Xiang  * For all managed pages, PG_private should be set with 1 extra refcount,
506aaa7b06SGao Xiang  * which is used for page reclaim / migration.
5147e4937aSGao Xiang  */
5247e4937aSGao Xiang 
536aaa7b06SGao Xiang /*
546aaa7b06SGao Xiang  * short-lived pages are pages directly from buddy system with specific
556aaa7b06SGao Xiang  * page->private (no need to set PagePrivate since these are non-LRU /
566aaa7b06SGao Xiang  * non-movable pages and bypass reclaim / migration code).
576aaa7b06SGao Xiang  */
586aaa7b06SGao Xiang static inline bool z_erofs_is_shortlived_page(struct page *page)
5947e4937aSGao Xiang {
606aaa7b06SGao Xiang 	if (page->private != Z_EROFS_SHORTLIVED_PAGE)
6147e4937aSGao Xiang 		return false;
6247e4937aSGao Xiang 
636aaa7b06SGao Xiang 	DBG_BUGON(page->mapping);
646aaa7b06SGao Xiang 	return true;
656aaa7b06SGao Xiang }
666aaa7b06SGao Xiang 
67eaa9172aSGao Xiang static inline bool z_erofs_put_shortlivedpage(struct page **pagepool,
686aaa7b06SGao Xiang 					      struct page *page)
696aaa7b06SGao Xiang {
706aaa7b06SGao Xiang 	if (!z_erofs_is_shortlived_page(page))
716aaa7b06SGao Xiang 		return false;
726aaa7b06SGao Xiang 
736aaa7b06SGao Xiang 	/* short-lived pages should not be used by others at the same time */
746aaa7b06SGao Xiang 	if (page_ref_count(page) > 1) {
7547e4937aSGao Xiang 		put_page(page);
766aaa7b06SGao Xiang 	} else {
776aaa7b06SGao Xiang 		/* follow the pcluster rule above. */
78eaa9172aSGao Xiang 		erofs_pagepool_add(pagepool, page);
796aaa7b06SGao Xiang 	}
8047e4937aSGao Xiang 	return true;
8147e4937aSGao Xiang }
8247e4937aSGao Xiang 
83622ceaddSGao Xiang #define MNGD_MAPPING(sbi)	((sbi)->managed_cache->i_mapping)
84622ceaddSGao Xiang static inline bool erofs_page_is_managed(const struct erofs_sb_info *sbi,
85622ceaddSGao Xiang 					 struct page *page)
86622ceaddSGao Xiang {
87622ceaddSGao Xiang 	return page->mapping == MNGD_MAPPING(sbi);
88622ceaddSGao Xiang }
89622ceaddSGao Xiang 
9010e5f6e4SGao Xiang int z_erofs_fixup_insize(struct z_erofs_decompress_req *rq, const char *padbuf,
9110e5f6e4SGao Xiang 			 unsigned int padbufsize);
92*597e2953SYue Hu extern const struct z_erofs_decompressor erofs_decompressors[];
9347e4937aSGao Xiang 
94622ceaddSGao Xiang /* prototypes for specific algorithms */
95622ceaddSGao Xiang int z_erofs_lzma_decompress(struct z_erofs_decompress_req *rq,
96eaa9172aSGao Xiang 			    struct page **pagepool);
9747e4937aSGao Xiang #endif
98