blob: 19e6c56a9f47184e0995baebbe4004a077fb10f8 [file] [log] [blame]
Gao Xiang29b24f62019-07-31 23:57:31 +08001/* SPDX-License-Identifier: GPL-2.0-only */
Gao Xiang27481232019-06-24 15:22:54 +08002/*
Gao Xiang27481232019-06-24 15:22:54 +08003 * Copyright (C) 2019 HUAWEI, Inc.
Alexander A. Klimov592e7cd2020-07-13 15:09:44 +02004 * https://www.huawei.com/
Gao Xiang27481232019-06-24 15:22:54 +08005 */
6#ifndef __EROFS_FS_COMPRESS_H
7#define __EROFS_FS_COMPRESS_H
8
Gao Xiang7fc45db2019-06-24 15:22:55 +08009#include "internal.h"
10
Gao Xiang7fc45db2019-06-24 15:22:55 +080011struct z_erofs_decompress_req {
Gao Xiang0ffd71b2019-06-24 15:22:56 +080012 struct super_block *sb;
Gao Xiang7fc45db2019-06-24 15:22:55 +080013 struct page **in, **out;
14
Gao Xiang10e5f6e2021-12-28 13:46:01 +080015 unsigned short pageofs_in, pageofs_out;
Gao Xiang7fc45db2019-06-24 15:22:55 +080016 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
Gao Xiang622cead2021-10-11 05:31:45 +080023struct z_erofs_decompressor {
24 int (*decompress)(struct z_erofs_decompress_req *rq,
Gao Xiangeaa91722021-10-22 17:01:20 +080025 struct page **pagepool);
Gao Xiang622cead2021-10-11 05:31:45 +080026 char *name;
27};
28
Gao Xiang6aaa7b02020-12-08 17:58:32 +080029/* some special page->private (unsigned long, see below) */
30#define Z_EROFS_SHORTLIVED_PAGE (-1UL << 2)
Gao Xiang1825c8d2020-12-09 20:37:17 +080031#define Z_EROFS_PREALLOCATED_PAGE (-2UL << 2)
Gao Xiang6aaa7b02020-12-08 17:58:32 +080032
Gao Xiang27481232019-06-24 15:22:54 +080033/*
Gao Xiang6aaa7b02020-12-08 17:58:32 +080034 * For all pages in a pcluster, page->private should be one of
35 * Type Last 2bits page->private
36 * short-lived page 00 Z_EROFS_SHORTLIVED_PAGE
Gao Xiang1825c8d2020-12-09 20:37:17 +080037 * preallocated page (tryalloc) 00 Z_EROFS_PREALLOCATED_PAGE
Gao Xiang6aaa7b02020-12-08 17:58:32 +080038 * cached/managed page 00 pointer to z_erofs_pcluster
39 * online page (file-backed, 01/10/11 sub-index << 2 | count
40 * some pages can be used for inplace I/O)
41 *
42 * page->mapping should be one of
43 * Type page->mapping
44 * short-lived page NULL
Gao Xiang1825c8d2020-12-09 20:37:17 +080045 * preallocated page NULL
Gao Xiang6aaa7b02020-12-08 17:58:32 +080046 * cached/managed page non-NULL or NULL (invalidated/truncated page)
47 * online page non-NULL
48 *
49 * For all managed pages, PG_private should be set with 1 extra refcount,
50 * which is used for page reclaim / migration.
Gao Xiang27481232019-06-24 15:22:54 +080051 */
Gao Xiang27481232019-06-24 15:22:54 +080052
Gao Xiang6aaa7b02020-12-08 17:58:32 +080053/*
54 * short-lived pages are pages directly from buddy system with specific
55 * page->private (no need to set PagePrivate since these are non-LRU /
56 * non-movable pages and bypass reclaim / migration code).
57 */
58static inline bool z_erofs_is_shortlived_page(struct page *page)
Gao Xiang27481232019-06-24 15:22:54 +080059{
Gao Xiang6aaa7b02020-12-08 17:58:32 +080060 if (page->private != Z_EROFS_SHORTLIVED_PAGE)
Gao Xiang27481232019-06-24 15:22:54 +080061 return false;
62
Gao Xiang6aaa7b02020-12-08 17:58:32 +080063 DBG_BUGON(page->mapping);
64 return true;
65}
66
Gao Xiangeaa91722021-10-22 17:01:20 +080067static inline bool z_erofs_put_shortlivedpage(struct page **pagepool,
Gao Xiang6aaa7b02020-12-08 17:58:32 +080068 struct page *page)
69{
70 if (!z_erofs_is_shortlived_page(page))
71 return false;
72
73 /* short-lived pages should not be used by others at the same time */
74 if (page_ref_count(page) > 1) {
Gao Xiang27481232019-06-24 15:22:54 +080075 put_page(page);
Gao Xiang6aaa7b02020-12-08 17:58:32 +080076 } else {
77 /* follow the pcluster rule above. */
Gao Xiangeaa91722021-10-22 17:01:20 +080078 erofs_pagepool_add(pagepool, page);
Gao Xiang6aaa7b02020-12-08 17:58:32 +080079 }
Gao Xiang27481232019-06-24 15:22:54 +080080 return true;
81}
82
Gao Xiang622cead2021-10-11 05:31:45 +080083#define MNGD_MAPPING(sbi) ((sbi)->managed_cache->i_mapping)
84static inline bool erofs_page_is_managed(const struct erofs_sb_info *sbi,
85 struct page *page)
86{
87 return page->mapping == MNGD_MAPPING(sbi);
88}
89
Gao Xiang10e5f6e2021-12-28 13:46:01 +080090int z_erofs_fixup_insize(struct z_erofs_decompress_req *rq, const char *padbuf,
91 unsigned int padbufsize);
Gao Xiang7fc45db2019-06-24 15:22:55 +080092int z_erofs_decompress(struct z_erofs_decompress_req *rq,
Gao Xiangeaa91722021-10-22 17:01:20 +080093 struct page **pagepool);
Gao Xiang7fc45db2019-06-24 15:22:55 +080094
Gao Xiang622cead2021-10-11 05:31:45 +080095/* prototypes for specific algorithms */
96int z_erofs_lzma_decompress(struct z_erofs_decompress_req *rq,
Gao Xiangeaa91722021-10-22 17:01:20 +080097 struct page **pagepool);
Gao Xiang27481232019-06-24 15:22:54 +080098#endif