xref: /openbmc/linux/fs/erofs/compress.h (revision 6aaa7b06)
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  * Created by Gao Xiang <gaoxiang25@huawei.com>
647e4937aSGao Xiang  */
747e4937aSGao Xiang #ifndef __EROFS_FS_COMPRESS_H
847e4937aSGao Xiang #define __EROFS_FS_COMPRESS_H
947e4937aSGao Xiang 
1047e4937aSGao Xiang #include "internal.h"
1147e4937aSGao Xiang 
1247e4937aSGao Xiang enum {
1347e4937aSGao Xiang 	Z_EROFS_COMPRESSION_SHIFTED = Z_EROFS_COMPRESSION_MAX,
1447e4937aSGao Xiang 	Z_EROFS_COMPRESSION_RUNTIME_MAX
1547e4937aSGao Xiang };
1647e4937aSGao Xiang 
1747e4937aSGao Xiang struct z_erofs_decompress_req {
1847e4937aSGao Xiang 	struct super_block *sb;
1947e4937aSGao Xiang 	struct page **in, **out;
2047e4937aSGao Xiang 
2147e4937aSGao Xiang 	unsigned short pageofs_out;
2247e4937aSGao Xiang 	unsigned int inputsize, outputsize;
2347e4937aSGao Xiang 
2447e4937aSGao Xiang 	/* indicate the algorithm will be used for decompression */
2547e4937aSGao Xiang 	unsigned int alg;
2647e4937aSGao Xiang 	bool inplace_io, partial_decoding;
2747e4937aSGao Xiang };
2847e4937aSGao Xiang 
29*6aaa7b06SGao Xiang /* some special page->private (unsigned long, see below) */
30*6aaa7b06SGao Xiang #define Z_EROFS_SHORTLIVED_PAGE		(-1UL << 2)
31*6aaa7b06SGao Xiang 
3247e4937aSGao Xiang /*
33*6aaa7b06SGao Xiang  * For all pages in a pcluster, page->private should be one of
34*6aaa7b06SGao Xiang  * Type                         Last 2bits      page->private
35*6aaa7b06SGao Xiang  * short-lived page             00              Z_EROFS_SHORTLIVED_PAGE
36*6aaa7b06SGao Xiang  * cached/managed page          00              pointer to z_erofs_pcluster
37*6aaa7b06SGao Xiang  * online page (file-backed,    01/10/11        sub-index << 2 | count
38*6aaa7b06SGao Xiang  *              some pages can be used for inplace I/O)
39*6aaa7b06SGao Xiang  *
40*6aaa7b06SGao Xiang  * page->mapping should be one of
41*6aaa7b06SGao Xiang  * Type                 page->mapping
42*6aaa7b06SGao Xiang  * short-lived page     NULL
43*6aaa7b06SGao Xiang  * cached/managed page  non-NULL or NULL (invalidated/truncated page)
44*6aaa7b06SGao Xiang  * online page          non-NULL
45*6aaa7b06SGao Xiang  *
46*6aaa7b06SGao Xiang  * For all managed pages, PG_private should be set with 1 extra refcount,
47*6aaa7b06SGao Xiang  * which is used for page reclaim / migration.
4847e4937aSGao Xiang  */
4947e4937aSGao Xiang 
50*6aaa7b06SGao Xiang /*
51*6aaa7b06SGao Xiang  * short-lived pages are pages directly from buddy system with specific
52*6aaa7b06SGao Xiang  * page->private (no need to set PagePrivate since these are non-LRU /
53*6aaa7b06SGao Xiang  * non-movable pages and bypass reclaim / migration code).
54*6aaa7b06SGao Xiang  */
55*6aaa7b06SGao Xiang static inline bool z_erofs_is_shortlived_page(struct page *page)
5647e4937aSGao Xiang {
57*6aaa7b06SGao Xiang 	if (page->private != Z_EROFS_SHORTLIVED_PAGE)
5847e4937aSGao Xiang 		return false;
5947e4937aSGao Xiang 
60*6aaa7b06SGao Xiang 	DBG_BUGON(page->mapping);
61*6aaa7b06SGao Xiang 	return true;
62*6aaa7b06SGao Xiang }
63*6aaa7b06SGao Xiang 
64*6aaa7b06SGao Xiang static inline bool z_erofs_put_shortlivedpage(struct list_head *pagepool,
65*6aaa7b06SGao Xiang 					      struct page *page)
66*6aaa7b06SGao Xiang {
67*6aaa7b06SGao Xiang 	if (!z_erofs_is_shortlived_page(page))
68*6aaa7b06SGao Xiang 		return false;
69*6aaa7b06SGao Xiang 
70*6aaa7b06SGao Xiang 	/* short-lived pages should not be used by others at the same time */
71*6aaa7b06SGao Xiang 	if (page_ref_count(page) > 1) {
7247e4937aSGao Xiang 		put_page(page);
73*6aaa7b06SGao Xiang 	} else {
74*6aaa7b06SGao Xiang 		/* follow the pcluster rule above. */
75*6aaa7b06SGao Xiang 		set_page_private(page, 0);
7647e4937aSGao Xiang 		list_add(&page->lru, pagepool);
77*6aaa7b06SGao Xiang 	}
7847e4937aSGao Xiang 	return true;
7947e4937aSGao Xiang }
8047e4937aSGao Xiang 
8147e4937aSGao Xiang int z_erofs_decompress(struct z_erofs_decompress_req *rq,
8247e4937aSGao Xiang 		       struct list_head *pagepool);
8347e4937aSGao Xiang 
8447e4937aSGao Xiang #endif
8547e4937aSGao Xiang 
86