xref: /openbmc/linux/fs/erofs/compress.h (revision e481ff3f)
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 enum {
12 	Z_EROFS_COMPRESSION_SHIFTED = Z_EROFS_COMPRESSION_MAX,
13 	Z_EROFS_COMPRESSION_RUNTIME_MAX
14 };
15 
16 struct z_erofs_decompress_req {
17 	struct super_block *sb;
18 	struct page **in, **out;
19 
20 	unsigned short pageofs_out;
21 	unsigned int inputsize, outputsize;
22 
23 	/* indicate the algorithm will be used for decompression */
24 	unsigned int alg;
25 	bool inplace_io, partial_decoding;
26 };
27 
28 /* some special page->private (unsigned long, see below) */
29 #define Z_EROFS_SHORTLIVED_PAGE		(-1UL << 2)
30 #define Z_EROFS_PREALLOCATED_PAGE	(-2UL << 2)
31 
32 /*
33  * For all pages in a pcluster, page->private should be one of
34  * Type                         Last 2bits      page->private
35  * short-lived page             00              Z_EROFS_SHORTLIVED_PAGE
36  * preallocated page (tryalloc) 00              Z_EROFS_PREALLOCATED_PAGE
37  * cached/managed page          00              pointer to z_erofs_pcluster
38  * online page (file-backed,    01/10/11        sub-index << 2 | count
39  *              some pages can be used for inplace I/O)
40  *
41  * page->mapping should be one of
42  * Type                 page->mapping
43  * short-lived page     NULL
44  * preallocated page    NULL
45  * cached/managed page  non-NULL or NULL (invalidated/truncated page)
46  * online page          non-NULL
47  *
48  * For all managed pages, PG_private should be set with 1 extra refcount,
49  * which is used for page reclaim / migration.
50  */
51 
52 /*
53  * short-lived pages are pages directly from buddy system with specific
54  * page->private (no need to set PagePrivate since these are non-LRU /
55  * non-movable pages and bypass reclaim / migration code).
56  */
57 static inline bool z_erofs_is_shortlived_page(struct page *page)
58 {
59 	if (page->private != Z_EROFS_SHORTLIVED_PAGE)
60 		return false;
61 
62 	DBG_BUGON(page->mapping);
63 	return true;
64 }
65 
66 static inline bool z_erofs_put_shortlivedpage(struct list_head *pagepool,
67 					      struct page *page)
68 {
69 	if (!z_erofs_is_shortlived_page(page))
70 		return false;
71 
72 	/* short-lived pages should not be used by others at the same time */
73 	if (page_ref_count(page) > 1) {
74 		put_page(page);
75 	} else {
76 		/* follow the pcluster rule above. */
77 		set_page_private(page, 0);
78 		list_add(&page->lru, pagepool);
79 	}
80 	return true;
81 }
82 
83 int z_erofs_decompress(struct z_erofs_decompress_req *rq,
84 		       struct list_head *pagepool);
85 
86 #endif
87