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