xref: /linux/fs/erofs/compress.h (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
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 	int (*init)(void);
28 	void (*exit)(void);
29 	char *name;
30 };
31 
32 /* some special page->private (unsigned long, see below) */
33 #define Z_EROFS_SHORTLIVED_PAGE		(-1UL << 2)
34 #define Z_EROFS_PREALLOCATED_PAGE	(-2UL << 2)
35 
36 /*
37  * For all pages in a pcluster, page->private should be one of
38  * Type                         Last 2bits      page->private
39  * short-lived page             00              Z_EROFS_SHORTLIVED_PAGE
40  * preallocated page (tryalloc) 00              Z_EROFS_PREALLOCATED_PAGE
41  * cached/managed page          00              pointer to z_erofs_pcluster
42  * online page (file-backed,    01/10/11        sub-index << 2 | count
43  *              some pages can be used for inplace I/O)
44  *
45  * page->mapping should be one of
46  * Type                 page->mapping
47  * short-lived page     NULL
48  * preallocated page    NULL
49  * cached/managed page  non-NULL or NULL (invalidated/truncated page)
50  * online page          non-NULL
51  *
52  * For all managed pages, PG_private should be set with 1 extra refcount,
53  * which is used for page reclaim / migration.
54  */
55 
56 /*
57  * Currently, short-lived pages are pages directly from buddy system
58  * with specific page->private (Z_EROFS_SHORTLIVED_PAGE).
59  * In the future world of Memdescs, it should be type 0 (Misc) memory
60  * which type can be checked with a new helper.
61  */
z_erofs_is_shortlived_page(struct page * page)62 static inline bool z_erofs_is_shortlived_page(struct page *page)
63 {
64 	return page->private == Z_EROFS_SHORTLIVED_PAGE;
65 }
66 
z_erofs_put_shortlivedpage(struct page ** pagepool,struct page * page)67 static inline bool z_erofs_put_shortlivedpage(struct page **pagepool,
68 					      struct page *page)
69 {
70 	if (!z_erofs_is_shortlived_page(page))
71 		return false;
72 	erofs_pagepool_add(pagepool, page);
73 	return true;
74 }
75 
76 extern const struct z_erofs_decompressor z_erofs_lzma_decomp;
77 extern const struct z_erofs_decompressor z_erofs_deflate_decomp;
78 extern const struct z_erofs_decompressor z_erofs_zstd_decomp;
79 extern const struct z_erofs_decompressor *z_erofs_decomp[];
80 
81 struct z_erofs_stream_dctx {
82 	struct z_erofs_decompress_req *rq;
83 	unsigned int inpages, outpages;	/* # of {en,de}coded pages */
84 	int no, ni;			/* the current {en,de}coded page # */
85 
86 	unsigned int avail_out;		/* remaining bytes in the decoded buffer */
87 	unsigned int inbuf_pos, inbuf_sz;
88 					/* current status of the encoded buffer */
89 	u8 *kin, *kout;			/* buffer mapped pointers */
90 	void *bounce;			/* bounce buffer for inplace I/Os */
91 	bool bounced;			/* is the bounce buffer used now? */
92 };
93 
94 int z_erofs_stream_switch_bufs(struct z_erofs_stream_dctx *dctx, void **dst,
95 			       void **src, struct page **pgpl);
96 int z_erofs_fixup_insize(struct z_erofs_decompress_req *rq, const char *padbuf,
97 			 unsigned int padbufsize);
98 int __init z_erofs_init_decompressor(void);
99 void z_erofs_exit_decompressor(void);
100 #endif
101