18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Squashfs - a compressed read only filesystem for Linux 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2016-present, Facebook, Inc. 68c2ecf20Sopenharmony_ci * All rights reserved. 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * zstd_wrapper.c 98c2ecf20Sopenharmony_ci */ 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include <linux/mutex.h> 128c2ecf20Sopenharmony_ci#include <linux/bio.h> 138c2ecf20Sopenharmony_ci#include <linux/slab.h> 148c2ecf20Sopenharmony_ci#include <linux/zstd.h> 158c2ecf20Sopenharmony_ci#include <linux/vmalloc.h> 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#include "squashfs_fs.h" 188c2ecf20Sopenharmony_ci#include "squashfs_fs_sb.h" 198c2ecf20Sopenharmony_ci#include "squashfs.h" 208c2ecf20Sopenharmony_ci#include "decompressor.h" 218c2ecf20Sopenharmony_ci#include "page_actor.h" 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_cistruct workspace { 248c2ecf20Sopenharmony_ci void *mem; 258c2ecf20Sopenharmony_ci size_t mem_size; 268c2ecf20Sopenharmony_ci size_t window_size; 278c2ecf20Sopenharmony_ci}; 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_cistatic void *zstd_init(struct squashfs_sb_info *msblk, void *buff) 308c2ecf20Sopenharmony_ci{ 318c2ecf20Sopenharmony_ci struct workspace *wksp = kmalloc(sizeof(*wksp), GFP_KERNEL); 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci if (wksp == NULL) 348c2ecf20Sopenharmony_ci goto failed; 358c2ecf20Sopenharmony_ci wksp->window_size = max_t(size_t, 368c2ecf20Sopenharmony_ci msblk->block_size, SQUASHFS_METADATA_SIZE); 378c2ecf20Sopenharmony_ci wksp->mem_size = ZSTD_DStreamWorkspaceBound(wksp->window_size); 388c2ecf20Sopenharmony_ci wksp->mem = vmalloc(wksp->mem_size); 398c2ecf20Sopenharmony_ci if (wksp->mem == NULL) 408c2ecf20Sopenharmony_ci goto failed; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci return wksp; 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_cifailed: 458c2ecf20Sopenharmony_ci ERROR("Failed to allocate zstd workspace\n"); 468c2ecf20Sopenharmony_ci kfree(wksp); 478c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 488c2ecf20Sopenharmony_ci} 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_cistatic void zstd_free(void *strm) 528c2ecf20Sopenharmony_ci{ 538c2ecf20Sopenharmony_ci struct workspace *wksp = strm; 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci if (wksp) 568c2ecf20Sopenharmony_ci vfree(wksp->mem); 578c2ecf20Sopenharmony_ci kfree(wksp); 588c2ecf20Sopenharmony_ci} 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_cistatic int zstd_uncompress(struct squashfs_sb_info *msblk, void *strm, 628c2ecf20Sopenharmony_ci struct bio *bio, int offset, int length, 638c2ecf20Sopenharmony_ci struct squashfs_page_actor *output) 648c2ecf20Sopenharmony_ci{ 658c2ecf20Sopenharmony_ci struct workspace *wksp = strm; 668c2ecf20Sopenharmony_ci ZSTD_DStream *stream; 678c2ecf20Sopenharmony_ci size_t total_out = 0; 688c2ecf20Sopenharmony_ci int error = 0; 698c2ecf20Sopenharmony_ci ZSTD_inBuffer in_buf = { NULL, 0, 0 }; 708c2ecf20Sopenharmony_ci ZSTD_outBuffer out_buf = { NULL, 0, 0 }; 718c2ecf20Sopenharmony_ci struct bvec_iter_all iter_all = {}; 728c2ecf20Sopenharmony_ci struct bio_vec *bvec = bvec_init_iter_all(&iter_all); 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci stream = ZSTD_initDStream(wksp->window_size, wksp->mem, wksp->mem_size); 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci if (!stream) { 778c2ecf20Sopenharmony_ci ERROR("Failed to initialize zstd decompressor\n"); 788c2ecf20Sopenharmony_ci return -EIO; 798c2ecf20Sopenharmony_ci } 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci out_buf.size = PAGE_SIZE; 828c2ecf20Sopenharmony_ci out_buf.dst = squashfs_first_page(output); 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci for (;;) { 858c2ecf20Sopenharmony_ci size_t zstd_err; 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci if (in_buf.pos == in_buf.size) { 888c2ecf20Sopenharmony_ci const void *data; 898c2ecf20Sopenharmony_ci int avail; 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci if (!bio_next_segment(bio, &iter_all)) { 928c2ecf20Sopenharmony_ci error = -EIO; 938c2ecf20Sopenharmony_ci break; 948c2ecf20Sopenharmony_ci } 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci avail = min(length, ((int)bvec->bv_len) - offset); 978c2ecf20Sopenharmony_ci data = page_address(bvec->bv_page) + bvec->bv_offset; 988c2ecf20Sopenharmony_ci length -= avail; 998c2ecf20Sopenharmony_ci in_buf.src = data + offset; 1008c2ecf20Sopenharmony_ci in_buf.size = avail; 1018c2ecf20Sopenharmony_ci in_buf.pos = 0; 1028c2ecf20Sopenharmony_ci offset = 0; 1038c2ecf20Sopenharmony_ci } 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci if (out_buf.pos == out_buf.size) { 1068c2ecf20Sopenharmony_ci out_buf.dst = squashfs_next_page(output); 1078c2ecf20Sopenharmony_ci if (out_buf.dst == NULL) { 1088c2ecf20Sopenharmony_ci /* Shouldn't run out of pages 1098c2ecf20Sopenharmony_ci * before stream is done. 1108c2ecf20Sopenharmony_ci */ 1118c2ecf20Sopenharmony_ci error = -EIO; 1128c2ecf20Sopenharmony_ci break; 1138c2ecf20Sopenharmony_ci } 1148c2ecf20Sopenharmony_ci out_buf.pos = 0; 1158c2ecf20Sopenharmony_ci out_buf.size = PAGE_SIZE; 1168c2ecf20Sopenharmony_ci } 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci total_out -= out_buf.pos; 1198c2ecf20Sopenharmony_ci zstd_err = ZSTD_decompressStream(stream, &out_buf, &in_buf); 1208c2ecf20Sopenharmony_ci total_out += out_buf.pos; /* add the additional data produced */ 1218c2ecf20Sopenharmony_ci if (zstd_err == 0) 1228c2ecf20Sopenharmony_ci break; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci if (ZSTD_isError(zstd_err)) { 1258c2ecf20Sopenharmony_ci ERROR("zstd decompression error: %d\n", 1268c2ecf20Sopenharmony_ci (int)ZSTD_getErrorCode(zstd_err)); 1278c2ecf20Sopenharmony_ci error = -EIO; 1288c2ecf20Sopenharmony_ci break; 1298c2ecf20Sopenharmony_ci } 1308c2ecf20Sopenharmony_ci } 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci squashfs_finish_page(output); 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci return error ? error : total_out; 1358c2ecf20Sopenharmony_ci} 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ciconst struct squashfs_decompressor squashfs_zstd_comp_ops = { 1388c2ecf20Sopenharmony_ci .init = zstd_init, 1398c2ecf20Sopenharmony_ci .free = zstd_free, 1408c2ecf20Sopenharmony_ci .decompress = zstd_uncompress, 1418c2ecf20Sopenharmony_ci .id = ZSTD_COMPRESSION, 1428c2ecf20Sopenharmony_ci .name = "zstd", 1438c2ecf20Sopenharmony_ci .supported = 1 1448c2ecf20Sopenharmony_ci}; 145