Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Squashfs - a compressed read only filesystem for Linux
0004  *
0005  * Copyright (c) 2010 LG Electronics
0006  * Chan Jeong <chan.jeong@lge.com>
0007  *
0008  * lzo_wrapper.c
0009  */
0010 
0011 #include <linux/mutex.h>
0012 #include <linux/bio.h>
0013 #include <linux/slab.h>
0014 #include <linux/vmalloc.h>
0015 #include <linux/lzo.h>
0016 
0017 #include "squashfs_fs.h"
0018 #include "squashfs_fs_sb.h"
0019 #include "squashfs.h"
0020 #include "decompressor.h"
0021 #include "page_actor.h"
0022 
0023 struct squashfs_lzo {
0024     void    *input;
0025     void    *output;
0026 };
0027 
0028 static void *lzo_init(struct squashfs_sb_info *msblk, void *buff)
0029 {
0030     int block_size = max_t(int, msblk->block_size, SQUASHFS_METADATA_SIZE);
0031 
0032     struct squashfs_lzo *stream = kzalloc(sizeof(*stream), GFP_KERNEL);
0033     if (stream == NULL)
0034         goto failed;
0035     stream->input = vmalloc(block_size);
0036     if (stream->input == NULL)
0037         goto failed;
0038     stream->output = vmalloc(block_size);
0039     if (stream->output == NULL)
0040         goto failed2;
0041 
0042     return stream;
0043 
0044 failed2:
0045     vfree(stream->input);
0046 failed:
0047     ERROR("Failed to allocate lzo workspace\n");
0048     kfree(stream);
0049     return ERR_PTR(-ENOMEM);
0050 }
0051 
0052 
0053 static void lzo_free(void *strm)
0054 {
0055     struct squashfs_lzo *stream = strm;
0056 
0057     if (stream) {
0058         vfree(stream->input);
0059         vfree(stream->output);
0060     }
0061     kfree(stream);
0062 }
0063 
0064 
0065 static int lzo_uncompress(struct squashfs_sb_info *msblk, void *strm,
0066     struct bio *bio, int offset, int length,
0067     struct squashfs_page_actor *output)
0068 {
0069     struct bvec_iter_all iter_all = {};
0070     struct bio_vec *bvec = bvec_init_iter_all(&iter_all);
0071     struct squashfs_lzo *stream = strm;
0072     void *buff = stream->input, *data;
0073     int bytes = length, res;
0074     size_t out_len = output->length;
0075 
0076     while (bio_next_segment(bio, &iter_all)) {
0077         int avail = min(bytes, ((int)bvec->bv_len) - offset);
0078 
0079         data = bvec_virt(bvec);
0080         memcpy(buff, data + offset, avail);
0081         buff += avail;
0082         bytes -= avail;
0083         offset = 0;
0084     }
0085 
0086     res = lzo1x_decompress_safe(stream->input, (size_t)length,
0087                     stream->output, &out_len);
0088     if (res != LZO_E_OK)
0089         goto failed;
0090 
0091     res = bytes = (int)out_len;
0092     data = squashfs_first_page(output);
0093     buff = stream->output;
0094     while (data) {
0095         if (bytes <= PAGE_SIZE) {
0096             if (!IS_ERR(data))
0097                 memcpy(data, buff, bytes);
0098             break;
0099         } else {
0100             if (!IS_ERR(data))
0101                 memcpy(data, buff, PAGE_SIZE);
0102             buff += PAGE_SIZE;
0103             bytes -= PAGE_SIZE;
0104             data = squashfs_next_page(output);
0105         }
0106     }
0107     squashfs_finish_page(output);
0108 
0109     return res;
0110 
0111 failed:
0112     return -EIO;
0113 }
0114 
0115 const struct squashfs_decompressor squashfs_lzo_comp_ops = {
0116     .init = lzo_init,
0117     .free = lzo_free,
0118     .decompress = lzo_uncompress,
0119     .id = LZO_COMPRESSION,
0120     .name = "lzo",
0121     .alloc_buffer = 0,
0122     .supported = 1
0123 };