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 
24 struct squashfs_stream {
25 	void		*stream;
26 	struct mutex	mutex;
27 };
28 
29 void *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 
49 out:
50 	kfree(stream);
51 	return ERR_PTR(err);
52 }
53 
54 void 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 
64 int 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 
82 int squashfs_max_decompressors(void)
83 {
84 	return 1;
85 }
86