f857cf6f22d4c4cb24fef3787e7358293c605a0b
[firefly-linux-kernel-4.4.55.git] / fs / squashfs / decompressor_single.c
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,
65         void **buffer, struct buffer_head **bh, int b, int offset, int length,
66         int srclength, int pages)
67 {
68         int res;
69         struct squashfs_stream *stream = msblk->stream;
70
71         mutex_lock(&stream->mutex);
72         res = msblk->decompressor->decompress(msblk, stream->stream, buffer,
73                 bh, b, offset, length, srclength, pages);
74         mutex_unlock(&stream->mutex);
75
76         if (res < 0)
77                 ERROR("%s decompression failed, data probably corrupt\n",
78                         msblk->decompressor->name);
79
80         return res;
81 }
82
83 int squashfs_max_decompressors(void)
84 {
85         return 1;
86 }