xref: /linux/fs/erofs/compress.h (revision 7255fcc80d4b525cc10cfaaf7f485830d4ed2000)
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 
9 #include "internal.h"
10 
11 struct z_erofs_decompress_req {
12 	struct super_block *sb;
13 	struct page **in, **out;
14 	unsigned short pageofs_in, pageofs_out;
15 	unsigned int inputsize, outputsize;
16 
17 	unsigned int alg;       /* the algorithm for decompression */
18 	bool inplace_io, partial_decoding, fillgaps;
19 	gfp_t gfp;      /* allocation flags for extra temporary buffers */
20 };
21 
22 struct z_erofs_decompressor {
23 	int (*config)(struct super_block *sb, struct erofs_super_block *dsb,
24 		      void *data, int size);
25 	int (*decompress)(struct z_erofs_decompress_req *rq,
26 			  struct page **pagepool);
27 	char *name;
28 };
29 
30 /* some special page->private (unsigned long, see below) */
31 #define Z_EROFS_SHORTLIVED_PAGE		(-1UL << 2)
32 #define Z_EROFS_PREALLOCATED_PAGE	(-2UL << 2)
33 
34 /*
35  * For all pages in a pcluster, page->private should be one of
36  * Type                         Last 2bits      page->private
37  * short-lived page             00              Z_EROFS_SHORTLIVED_PAGE
38  * preallocated page (tryalloc) 00              Z_EROFS_PREALLOCATED_PAGE
39  * cached/managed page          00              pointer to z_erofs_pcluster
40  * online page (file-backed,    01/10/11        sub-index << 2 | count
41  *              some pages can be used for inplace I/O)
42  *
43  * page->mapping should be one of
44  * Type                 page->mapping
45  * short-lived page     NULL
46  * preallocated page    NULL
47  * cached/managed page  non-NULL or NULL (invalidated/truncated page)
48  * online page          non-NULL
49  *
50  * For all managed pages, PG_private should be set with 1 extra refcount,
51  * which is used for page reclaim / migration.
52  */
53 
54 /*
55  * short-lived pages are pages directly from buddy system with specific
56  * page->private (no need to set PagePrivate since these are non-LRU /
57  * non-movable pages and bypass reclaim / migration code).
58  */
59 static inline bool z_erofs_is_shortlived_page(struct page *page)
60 {
61 	if (page->private != Z_EROFS_SHORTLIVED_PAGE)
62 		return false;
63 
64 	DBG_BUGON(page->mapping);
65 	return true;
66 }
67 
68 static inline bool z_erofs_put_shortlivedpage(struct page **pagepool,
69 					      struct page *page)
70 {
71 	if (!z_erofs_is_shortlived_page(page))
72 		return false;
73 
74 	/* short-lived pages should not be used by others at the same time */
75 	if (page_ref_count(page) > 1) {
76 		put_page(page);
77 	} else {
78 		/* follow the pcluster rule above. */
79 		erofs_pagepool_add(pagepool, page);
80 	}
81 	return true;
82 }
83 
84 int z_erofs_fixup_insize(struct z_erofs_decompress_req *rq, const char *padbuf,
85 			 unsigned int padbufsize);
86 extern const struct z_erofs_decompressor erofs_decompressors[];
87 
88 /* prototypes for specific algorithms */
89 int z_erofs_load_lzma_config(struct super_block *sb,
90 			struct erofs_super_block *dsb, void *data, int size);
91 int z_erofs_load_deflate_config(struct super_block *sb,
92 			struct erofs_super_block *dsb, void *data, int size);
93 int z_erofs_lzma_decompress(struct z_erofs_decompress_req *rq,
94 			    struct page **pagepool);
95 int z_erofs_deflate_decompress(struct z_erofs_decompress_req *rq,
96 			       struct page **pagepool);
97 #endif
98