162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (C) 2019 HUAWEI, Inc. 462306a36Sopenharmony_ci * https://www.huawei.com/ 562306a36Sopenharmony_ci */ 662306a36Sopenharmony_ci#ifndef __EROFS_FS_COMPRESS_H 762306a36Sopenharmony_ci#define __EROFS_FS_COMPRESS_H 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci#include "internal.h" 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_cistruct z_erofs_decompress_req { 1262306a36Sopenharmony_ci struct super_block *sb; 1362306a36Sopenharmony_ci struct page **in, **out; 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci unsigned short pageofs_in, pageofs_out; 1662306a36Sopenharmony_ci unsigned int inputsize, outputsize; 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_ci /* indicate the algorithm will be used for decompression */ 1962306a36Sopenharmony_ci unsigned int alg; 2062306a36Sopenharmony_ci bool inplace_io, partial_decoding, fillgaps; 2162306a36Sopenharmony_ci}; 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_cistruct z_erofs_decompressor { 2462306a36Sopenharmony_ci int (*config)(struct super_block *sb, struct erofs_super_block *dsb, 2562306a36Sopenharmony_ci void *data, int size); 2662306a36Sopenharmony_ci int (*decompress)(struct z_erofs_decompress_req *rq, 2762306a36Sopenharmony_ci struct page **pagepool); 2862306a36Sopenharmony_ci char *name; 2962306a36Sopenharmony_ci}; 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ci/* some special page->private (unsigned long, see below) */ 3262306a36Sopenharmony_ci#define Z_EROFS_SHORTLIVED_PAGE (-1UL << 2) 3362306a36Sopenharmony_ci#define Z_EROFS_PREALLOCATED_PAGE (-2UL << 2) 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ci/* 3662306a36Sopenharmony_ci * For all pages in a pcluster, page->private should be one of 3762306a36Sopenharmony_ci * Type Last 2bits page->private 3862306a36Sopenharmony_ci * short-lived page 00 Z_EROFS_SHORTLIVED_PAGE 3962306a36Sopenharmony_ci * preallocated page (tryalloc) 00 Z_EROFS_PREALLOCATED_PAGE 4062306a36Sopenharmony_ci * cached/managed page 00 pointer to z_erofs_pcluster 4162306a36Sopenharmony_ci * online page (file-backed, 01/10/11 sub-index << 2 | count 4262306a36Sopenharmony_ci * some pages can be used for inplace I/O) 4362306a36Sopenharmony_ci * 4462306a36Sopenharmony_ci * page->mapping should be one of 4562306a36Sopenharmony_ci * Type page->mapping 4662306a36Sopenharmony_ci * short-lived page NULL 4762306a36Sopenharmony_ci * preallocated page NULL 4862306a36Sopenharmony_ci * cached/managed page non-NULL or NULL (invalidated/truncated page) 4962306a36Sopenharmony_ci * online page non-NULL 5062306a36Sopenharmony_ci * 5162306a36Sopenharmony_ci * For all managed pages, PG_private should be set with 1 extra refcount, 5262306a36Sopenharmony_ci * which is used for page reclaim / migration. 5362306a36Sopenharmony_ci */ 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci/* 5662306a36Sopenharmony_ci * short-lived pages are pages directly from buddy system with specific 5762306a36Sopenharmony_ci * page->private (no need to set PagePrivate since these are non-LRU / 5862306a36Sopenharmony_ci * non-movable pages and bypass reclaim / migration code). 5962306a36Sopenharmony_ci */ 6062306a36Sopenharmony_cistatic inline bool z_erofs_is_shortlived_page(struct page *page) 6162306a36Sopenharmony_ci{ 6262306a36Sopenharmony_ci if (page->private != Z_EROFS_SHORTLIVED_PAGE) 6362306a36Sopenharmony_ci return false; 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ci DBG_BUGON(page->mapping); 6662306a36Sopenharmony_ci return true; 6762306a36Sopenharmony_ci} 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_cistatic inline bool z_erofs_put_shortlivedpage(struct page **pagepool, 7062306a36Sopenharmony_ci struct page *page) 7162306a36Sopenharmony_ci{ 7262306a36Sopenharmony_ci if (!z_erofs_is_shortlived_page(page)) 7362306a36Sopenharmony_ci return false; 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_ci /* short-lived pages should not be used by others at the same time */ 7662306a36Sopenharmony_ci if (page_ref_count(page) > 1) { 7762306a36Sopenharmony_ci put_page(page); 7862306a36Sopenharmony_ci } else { 7962306a36Sopenharmony_ci /* follow the pcluster rule above. */ 8062306a36Sopenharmony_ci erofs_pagepool_add(pagepool, page); 8162306a36Sopenharmony_ci } 8262306a36Sopenharmony_ci return true; 8362306a36Sopenharmony_ci} 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_ci#define MNGD_MAPPING(sbi) ((sbi)->managed_cache->i_mapping) 8662306a36Sopenharmony_cistatic inline bool erofs_page_is_managed(const struct erofs_sb_info *sbi, 8762306a36Sopenharmony_ci struct page *page) 8862306a36Sopenharmony_ci{ 8962306a36Sopenharmony_ci return page->mapping == MNGD_MAPPING(sbi); 9062306a36Sopenharmony_ci} 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_ciint z_erofs_fixup_insize(struct z_erofs_decompress_req *rq, const char *padbuf, 9362306a36Sopenharmony_ci unsigned int padbufsize); 9462306a36Sopenharmony_ciextern const struct z_erofs_decompressor erofs_decompressors[]; 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci/* prototypes for specific algorithms */ 9762306a36Sopenharmony_ciint z_erofs_load_lzma_config(struct super_block *sb, 9862306a36Sopenharmony_ci struct erofs_super_block *dsb, void *data, int size); 9962306a36Sopenharmony_ciint z_erofs_load_deflate_config(struct super_block *sb, 10062306a36Sopenharmony_ci struct erofs_super_block *dsb, void *data, int size); 10162306a36Sopenharmony_ciint z_erofs_lzma_decompress(struct z_erofs_decompress_req *rq, 10262306a36Sopenharmony_ci struct page **pagepool); 10362306a36Sopenharmony_ciint z_erofs_deflate_decompress(struct z_erofs_decompress_req *rq, 10462306a36Sopenharmony_ci struct page **pagepool); 10562306a36Sopenharmony_ci#endif 106