Loading...
1/*
2 * Copyright (c) 2013
3 * Phillip Lougher <phillip@squashfs.org.uk>
4 *
5 * This work is licensed under the terms of the GNU GPL, version 2. See
6 * the COPYING file in the top-level directory.
7 */
8
9#include <linux/types.h>
10#include <linux/mutex.h>
11#include <linux/slab.h>
12#include <linux/buffer_head.h>
13
14#include "squashfs_fs.h"
15#include "squashfs_fs_sb.h"
16#include "decompressor.h"
17#include "squashfs.h"
18
19/*
20 * This file implements single-threaded decompression in the
21 * decompressor framework
22 */
23
24struct squashfs_stream {
25 void *stream;
26 struct mutex mutex;
27};
28
29void *squashfs_decompressor_create(struct squashfs_sb_info *msblk,
30 void *comp_opts)
31{
32 struct squashfs_stream *stream;
33 int err = -ENOMEM;
34
35 stream = kmalloc(sizeof(*stream), GFP_KERNEL);
36 if (stream == NULL)
37 goto out;
38
39 stream->stream = msblk->decompressor->init(msblk, comp_opts);
40 if (IS_ERR(stream->stream)) {
41 err = PTR_ERR(stream->stream);
42 goto out;
43 }
44
45 kfree(comp_opts);
46 mutex_init(&stream->mutex);
47 return stream;
48
49out:
50 kfree(stream);
51 return ERR_PTR(err);
52}
53
54void squashfs_decompressor_destroy(struct squashfs_sb_info *msblk)
55{
56 struct squashfs_stream *stream = msblk->stream;
57
58 if (stream) {
59 msblk->decompressor->free(stream->stream);
60 kfree(stream);
61 }
62}
63
64int squashfs_decompress(struct squashfs_sb_info *msblk, struct buffer_head **bh,
65 int b, int offset, int length, struct squashfs_page_actor *output)
66{
67 int res;
68 struct squashfs_stream *stream = msblk->stream;
69
70 mutex_lock(&stream->mutex);
71 res = msblk->decompressor->decompress(msblk, stream->stream, bh, b,
72 offset, length, output);
73 mutex_unlock(&stream->mutex);
74
75 if (res < 0)
76 ERROR("%s decompression failed, data probably corrupt\n",
77 msblk->decompressor->name);
78
79 return res;
80}
81
82int squashfs_max_decompressors(void)
83{
84 return 1;
85}
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (c) 2013
4 * Phillip Lougher <phillip@squashfs.org.uk>
5 */
6
7#include <linux/types.h>
8#include <linux/mutex.h>
9#include <linux/slab.h>
10#include <linux/bio.h>
11
12#include "squashfs_fs.h"
13#include "squashfs_fs_sb.h"
14#include "decompressor.h"
15#include "squashfs.h"
16
17/*
18 * This file implements single-threaded decompression in the
19 * decompressor framework
20 */
21
22struct squashfs_stream {
23 void *stream;
24 struct mutex mutex;
25};
26
27static void *squashfs_decompressor_create(struct squashfs_sb_info *msblk,
28 void *comp_opts)
29{
30 struct squashfs_stream *stream;
31 int err = -ENOMEM;
32
33 stream = kmalloc(sizeof(*stream), GFP_KERNEL);
34 if (stream == NULL)
35 goto out;
36
37 stream->stream = msblk->decompressor->init(msblk, comp_opts);
38 if (IS_ERR(stream->stream)) {
39 err = PTR_ERR(stream->stream);
40 goto out;
41 }
42
43 kfree(comp_opts);
44 mutex_init(&stream->mutex);
45 return stream;
46
47out:
48 kfree(stream);
49 return ERR_PTR(err);
50}
51
52static void squashfs_decompressor_destroy(struct squashfs_sb_info *msblk)
53{
54 struct squashfs_stream *stream = msblk->stream;
55
56 if (stream) {
57 msblk->decompressor->free(stream->stream);
58 kfree(stream);
59 }
60}
61
62static int squashfs_decompress(struct squashfs_sb_info *msblk, struct bio *bio,
63 int offset, int length,
64 struct squashfs_page_actor *output)
65{
66 int res;
67 struct squashfs_stream *stream = msblk->stream;
68
69 mutex_lock(&stream->mutex);
70 res = msblk->decompressor->decompress(msblk, stream->stream, bio,
71 offset, length, output);
72 mutex_unlock(&stream->mutex);
73
74 if (res < 0)
75 ERROR("%s decompression failed, data probably corrupt\n",
76 msblk->decompressor->name);
77
78 return res;
79}
80
81static int squashfs_max_decompressors(void)
82{
83 return 1;
84}
85
86const struct squashfs_decompressor_thread_ops squashfs_decompressor_single = {
87 .create = squashfs_decompressor_create,
88 .destroy = squashfs_decompressor_destroy,
89 .decompress = squashfs_decompress,
90 .max_decompressors = squashfs_max_decompressors,
91};