xref: /linux/fs/erofs/compress.h (revision c2da8b3f914f83fb9089d26a692eb8f22146ddb9)
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 #define Z_EROFS_SHORTLIVED_PAGE		(-1UL << 2)
33 #define Z_EROFS_PREALLOCATED_FOLIO	((void *)(-2UL << 2))
34 
35 /*
36  * Currently, short-lived pages are pages directly from buddy system
37  * with specific page->private (Z_EROFS_SHORTLIVED_PAGE).
38  * In the future world of Memdescs, it should be type 0 (Misc) memory
39  * which type can be checked with a new helper.
40  */
z_erofs_is_shortlived_page(struct page * page)41 static inline bool z_erofs_is_shortlived_page(struct page *page)
42 {
43 	return page->private == Z_EROFS_SHORTLIVED_PAGE;
44 }
45 
z_erofs_put_shortlivedpage(struct page ** pagepool,struct page * page)46 static inline bool z_erofs_put_shortlivedpage(struct page **pagepool,
47 					      struct page *page)
48 {
49 	if (!z_erofs_is_shortlived_page(page))
50 		return false;
51 	erofs_pagepool_add(pagepool, page);
52 	return true;
53 }
54 
55 extern const struct z_erofs_decompressor z_erofs_lzma_decomp;
56 extern const struct z_erofs_decompressor z_erofs_deflate_decomp;
57 extern const struct z_erofs_decompressor z_erofs_zstd_decomp;
58 extern const struct z_erofs_decompressor *z_erofs_decomp[];
59 
60 struct z_erofs_stream_dctx {
61 	struct z_erofs_decompress_req *rq;
62 	unsigned int inpages, outpages;	/* # of {en,de}coded pages */
63 	int no, ni;			/* the current {en,de}coded page # */
64 
65 	unsigned int avail_out;		/* remaining bytes in the decoded buffer */
66 	unsigned int inbuf_pos, inbuf_sz;
67 					/* current status of the encoded buffer */
68 	u8 *kin, *kout;			/* buffer mapped pointers */
69 	void *bounce;			/* bounce buffer for inplace I/Os */
70 	bool bounced;			/* is the bounce buffer used now? */
71 };
72 
73 int z_erofs_stream_switch_bufs(struct z_erofs_stream_dctx *dctx, void **dst,
74 			       void **src, struct page **pgpl);
75 int z_erofs_fixup_insize(struct z_erofs_decompress_req *rq, const char *padbuf,
76 			 unsigned int padbufsize);
77 int __init z_erofs_init_decompressor(void);
78 void z_erofs_exit_decompressor(void);
79 #endif
80