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 int inpages, outpages;
15 	unsigned short pageofs_in, pageofs_out;
16 	unsigned int inputsize, outputsize;
17 
18 	unsigned int alg;       /* the algorithm for decompression */
19 	bool inplace_io, partial_decoding, fillgaps;
20 	gfp_t gfp;      /* allocation flags for extra temporary buffers */
21 };
22 
23 struct z_erofs_decompressor {
24 	int (*config)(struct super_block *sb, struct erofs_super_block *dsb,
25 		      void *data, int size);
26 	int (*decompress)(struct z_erofs_decompress_req *rq,
27 			  struct page **pagepool);
28 	int (*init)(void);
29 	void (*exit)(void);
30 	char *name;
31 };
32 
33 #define Z_EROFS_SHORTLIVED_PAGE		(-1UL << 2)
34 #define Z_EROFS_PREALLOCATED_FOLIO	((void *)(-2UL << 2))
35 
36 /*
37  * Currently, short-lived pages are pages directly from buddy system
38  * with specific page->private (Z_EROFS_SHORTLIVED_PAGE).
39  * In the future world of Memdescs, it should be type 0 (Misc) memory
40  * which type can be checked with a new helper.
41  */
42 static inline bool z_erofs_is_shortlived_page(struct page *page)
43 {
44 	return page->private == Z_EROFS_SHORTLIVED_PAGE;
45 }
46 
47 static inline bool z_erofs_put_shortlivedpage(struct page **pagepool,
48 					      struct page *page)
49 {
50 	if (!z_erofs_is_shortlived_page(page))
51 		return false;
52 	erofs_pagepool_add(pagepool, page);
53 	return true;
54 }
55 
56 extern const struct z_erofs_decompressor z_erofs_lzma_decomp;
57 extern const struct z_erofs_decompressor z_erofs_deflate_decomp;
58 extern const struct z_erofs_decompressor z_erofs_zstd_decomp;
59 extern const struct z_erofs_decompressor *z_erofs_decomp[];
60 
61 struct z_erofs_stream_dctx {
62 	struct z_erofs_decompress_req *rq;
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 int z_erofs_crypto_decompress(struct z_erofs_decompress_req *rq,
80 			      struct page **pgpl);
81 int z_erofs_crypto_enable_engine(const char *name, int len);
82 #ifdef CONFIG_EROFS_FS_ZIP_ACCEL
83 void z_erofs_crypto_disable_all_engines(void);
84 int z_erofs_crypto_show_engines(char *buf, int size, char sep);
85 #else
86 static inline void z_erofs_crypto_disable_all_engines(void) {}
87 static inline int z_erofs_crypto_show_engines(char *buf, int size, char sep) { return 0; }
88 #endif
89 #endif
90