120c8ccb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
29508c6b9SPhillip Lougher /*
39508c6b9SPhillip Lougher * Copyright (c) 2013
49508c6b9SPhillip Lougher * Phillip Lougher <phillip@squashfs.org.uk>
59508c6b9SPhillip Lougher */
69508c6b9SPhillip Lougher
79508c6b9SPhillip Lougher #include <linux/types.h>
89508c6b9SPhillip Lougher #include <linux/mutex.h>
99508c6b9SPhillip Lougher #include <linux/slab.h>
1093e72b3cSPhilippe Liard #include <linux/bio.h>
119508c6b9SPhillip Lougher
129508c6b9SPhillip Lougher #include "squashfs_fs.h"
139508c6b9SPhillip Lougher #include "squashfs_fs_sb.h"
149508c6b9SPhillip Lougher #include "decompressor.h"
159508c6b9SPhillip Lougher #include "squashfs.h"
169508c6b9SPhillip Lougher
179508c6b9SPhillip Lougher /*
189508c6b9SPhillip Lougher * This file implements single-threaded decompression in the
199508c6b9SPhillip Lougher * decompressor framework
209508c6b9SPhillip Lougher */
219508c6b9SPhillip Lougher
229508c6b9SPhillip Lougher struct squashfs_stream {
239508c6b9SPhillip Lougher void *stream;
249508c6b9SPhillip Lougher struct mutex mutex;
259508c6b9SPhillip Lougher };
269508c6b9SPhillip Lougher
squashfs_decompressor_create(struct squashfs_sb_info * msblk,void * comp_opts)27*80f78409SXiaoming Ni static void *squashfs_decompressor_create(struct squashfs_sb_info *msblk,
289508c6b9SPhillip Lougher void *comp_opts)
299508c6b9SPhillip Lougher {
309508c6b9SPhillip Lougher struct squashfs_stream *stream;
319508c6b9SPhillip Lougher int err = -ENOMEM;
329508c6b9SPhillip Lougher
339508c6b9SPhillip Lougher stream = kmalloc(sizeof(*stream), GFP_KERNEL);
349508c6b9SPhillip Lougher if (stream == NULL)
359508c6b9SPhillip Lougher goto out;
369508c6b9SPhillip Lougher
379508c6b9SPhillip Lougher stream->stream = msblk->decompressor->init(msblk, comp_opts);
389508c6b9SPhillip Lougher if (IS_ERR(stream->stream)) {
399508c6b9SPhillip Lougher err = PTR_ERR(stream->stream);
409508c6b9SPhillip Lougher goto out;
419508c6b9SPhillip Lougher }
429508c6b9SPhillip Lougher
439508c6b9SPhillip Lougher kfree(comp_opts);
449508c6b9SPhillip Lougher mutex_init(&stream->mutex);
459508c6b9SPhillip Lougher return stream;
469508c6b9SPhillip Lougher
479508c6b9SPhillip Lougher out:
489508c6b9SPhillip Lougher kfree(stream);
499508c6b9SPhillip Lougher return ERR_PTR(err);
509508c6b9SPhillip Lougher }
519508c6b9SPhillip Lougher
squashfs_decompressor_destroy(struct squashfs_sb_info * msblk)52*80f78409SXiaoming Ni static void squashfs_decompressor_destroy(struct squashfs_sb_info *msblk)
539508c6b9SPhillip Lougher {
549508c6b9SPhillip Lougher struct squashfs_stream *stream = msblk->stream;
559508c6b9SPhillip Lougher
569508c6b9SPhillip Lougher if (stream) {
579508c6b9SPhillip Lougher msblk->decompressor->free(stream->stream);
589508c6b9SPhillip Lougher kfree(stream);
599508c6b9SPhillip Lougher }
609508c6b9SPhillip Lougher }
619508c6b9SPhillip Lougher
squashfs_decompress(struct squashfs_sb_info * msblk,struct bio * bio,int offset,int length,struct squashfs_page_actor * output)62*80f78409SXiaoming Ni static int squashfs_decompress(struct squashfs_sb_info *msblk, struct bio *bio,
6393e72b3cSPhilippe Liard int offset, int length,
6493e72b3cSPhilippe Liard struct squashfs_page_actor *output)
659508c6b9SPhillip Lougher {
669508c6b9SPhillip Lougher int res;
679508c6b9SPhillip Lougher struct squashfs_stream *stream = msblk->stream;
689508c6b9SPhillip Lougher
699508c6b9SPhillip Lougher mutex_lock(&stream->mutex);
7093e72b3cSPhilippe Liard res = msblk->decompressor->decompress(msblk, stream->stream, bio,
71846b730eSPhillip Lougher offset, length, output);
729508c6b9SPhillip Lougher mutex_unlock(&stream->mutex);
739508c6b9SPhillip Lougher
749508c6b9SPhillip Lougher if (res < 0)
759508c6b9SPhillip Lougher ERROR("%s decompression failed, data probably corrupt\n",
769508c6b9SPhillip Lougher msblk->decompressor->name);
779508c6b9SPhillip Lougher
789508c6b9SPhillip Lougher return res;
799508c6b9SPhillip Lougher }
809508c6b9SPhillip Lougher
squashfs_max_decompressors(void)81*80f78409SXiaoming Ni static int squashfs_max_decompressors(void)
829508c6b9SPhillip Lougher {
839508c6b9SPhillip Lougher return 1;
849508c6b9SPhillip Lougher }
85*80f78409SXiaoming Ni
86*80f78409SXiaoming Ni const struct squashfs_decompressor_thread_ops squashfs_decompressor_single = {
87*80f78409SXiaoming Ni .create = squashfs_decompressor_create,
88*80f78409SXiaoming Ni .destroy = squashfs_decompressor_destroy,
89*80f78409SXiaoming Ni .decompress = squashfs_decompress,
90*80f78409SXiaoming Ni .max_decompressors = squashfs_max_decompressors,
91*80f78409SXiaoming Ni };
92