0001
0002
0003
0004
0005
0006 #include <linux/types.h>
0007 #include <linux/mutex.h>
0008 #include <linux/slab.h>
0009 #include <linux/bio.h>
0010 #include <linux/sched.h>
0011 #include <linux/wait.h>
0012 #include <linux/cpumask.h>
0013
0014 #include "squashfs_fs.h"
0015 #include "squashfs_fs_sb.h"
0016 #include "decompressor.h"
0017 #include "squashfs.h"
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 #define MAX_DECOMPRESSOR (num_online_cpus() * 2)
0030
0031
0032 int squashfs_max_decompressors(void)
0033 {
0034 return MAX_DECOMPRESSOR;
0035 }
0036
0037
0038 struct squashfs_stream {
0039 void *comp_opts;
0040 struct list_head strm_list;
0041 struct mutex mutex;
0042 int avail_decomp;
0043 wait_queue_head_t wait;
0044 };
0045
0046
0047 struct decomp_stream {
0048 void *stream;
0049 struct list_head list;
0050 };
0051
0052
0053 static void put_decomp_stream(struct decomp_stream *decomp_strm,
0054 struct squashfs_stream *stream)
0055 {
0056 mutex_lock(&stream->mutex);
0057 list_add(&decomp_strm->list, &stream->strm_list);
0058 mutex_unlock(&stream->mutex);
0059 wake_up(&stream->wait);
0060 }
0061
0062 void *squashfs_decompressor_create(struct squashfs_sb_info *msblk,
0063 void *comp_opts)
0064 {
0065 struct squashfs_stream *stream;
0066 struct decomp_stream *decomp_strm = NULL;
0067 int err = -ENOMEM;
0068
0069 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
0070 if (!stream)
0071 goto out;
0072
0073 stream->comp_opts = comp_opts;
0074 mutex_init(&stream->mutex);
0075 INIT_LIST_HEAD(&stream->strm_list);
0076 init_waitqueue_head(&stream->wait);
0077
0078
0079
0080
0081
0082
0083
0084 decomp_strm = kmalloc(sizeof(*decomp_strm), GFP_KERNEL);
0085 if (!decomp_strm)
0086 goto out;
0087
0088 decomp_strm->stream = msblk->decompressor->init(msblk,
0089 stream->comp_opts);
0090 if (IS_ERR(decomp_strm->stream)) {
0091 err = PTR_ERR(decomp_strm->stream);
0092 goto out;
0093 }
0094
0095 list_add(&decomp_strm->list, &stream->strm_list);
0096 stream->avail_decomp = 1;
0097 return stream;
0098
0099 out:
0100 kfree(decomp_strm);
0101 kfree(stream);
0102 return ERR_PTR(err);
0103 }
0104
0105
0106 void squashfs_decompressor_destroy(struct squashfs_sb_info *msblk)
0107 {
0108 struct squashfs_stream *stream = msblk->stream;
0109 if (stream) {
0110 struct decomp_stream *decomp_strm;
0111
0112 while (!list_empty(&stream->strm_list)) {
0113 decomp_strm = list_entry(stream->strm_list.prev,
0114 struct decomp_stream, list);
0115 list_del(&decomp_strm->list);
0116 msblk->decompressor->free(decomp_strm->stream);
0117 kfree(decomp_strm);
0118 stream->avail_decomp--;
0119 }
0120 WARN_ON(stream->avail_decomp);
0121 kfree(stream->comp_opts);
0122 kfree(stream);
0123 }
0124 }
0125
0126
0127 static struct decomp_stream *get_decomp_stream(struct squashfs_sb_info *msblk,
0128 struct squashfs_stream *stream)
0129 {
0130 struct decomp_stream *decomp_strm;
0131
0132 while (1) {
0133 mutex_lock(&stream->mutex);
0134
0135
0136 if (!list_empty(&stream->strm_list)) {
0137 decomp_strm = list_entry(stream->strm_list.prev,
0138 struct decomp_stream, list);
0139 list_del(&decomp_strm->list);
0140 mutex_unlock(&stream->mutex);
0141 break;
0142 }
0143
0144
0145
0146
0147
0148 if (stream->avail_decomp >= MAX_DECOMPRESSOR)
0149 goto wait;
0150
0151
0152 decomp_strm = kmalloc(sizeof(*decomp_strm), GFP_KERNEL);
0153 if (!decomp_strm)
0154 goto wait;
0155
0156 decomp_strm->stream = msblk->decompressor->init(msblk,
0157 stream->comp_opts);
0158 if (IS_ERR(decomp_strm->stream)) {
0159 kfree(decomp_strm);
0160 goto wait;
0161 }
0162
0163 stream->avail_decomp++;
0164 WARN_ON(stream->avail_decomp > MAX_DECOMPRESSOR);
0165
0166 mutex_unlock(&stream->mutex);
0167 break;
0168 wait:
0169
0170
0171
0172
0173
0174 mutex_unlock(&stream->mutex);
0175 wait_event(stream->wait,
0176 !list_empty(&stream->strm_list));
0177 }
0178
0179 return decomp_strm;
0180 }
0181
0182
0183 int squashfs_decompress(struct squashfs_sb_info *msblk, struct bio *bio,
0184 int offset, int length,
0185 struct squashfs_page_actor *output)
0186 {
0187 int res;
0188 struct squashfs_stream *stream = msblk->stream;
0189 struct decomp_stream *decomp_stream = get_decomp_stream(msblk, stream);
0190 res = msblk->decompressor->decompress(msblk, decomp_stream->stream,
0191 bio, offset, length, output);
0192 put_decomp_stream(decomp_stream, stream);
0193 if (res < 0)
0194 ERROR("%s decompression failed, data probably corrupt\n",
0195 msblk->decompressor->name);
0196 return res;
0197 }