2 * Copyright (C) 2014 Sergey Senozhatsky.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
10 #include <linux/kernel.h>
11 #include <linux/string.h>
12 #include <linux/slab.h>
13 #include <linux/wait.h>
14 #include <linux/sched.h>
17 #include "zcomp_lzo.h"
20 * single zcomp_strm backend
22 struct zcomp_strm_single {
23 struct mutex strm_lock;
24 struct zcomp_strm *zstrm;
28 * multi zcomp_strm backend
30 struct zcomp_strm_multi {
31 /* protect strm list */
33 /* max possible number of zstrm streams */
35 /* number of available zstrm streams */
37 /* list of available strms */
38 struct list_head idle_strm;
39 wait_queue_head_t strm_wait;
42 static struct zcomp_backend *backends[] = {
47 static struct zcomp_backend *find_backend(const char *compress)
51 if (sysfs_streq(compress, backends[i]->name))
58 static void zcomp_strm_free(struct zcomp *comp, struct zcomp_strm *zstrm)
61 comp->backend->destroy(zstrm->private);
62 free_pages((unsigned long)zstrm->buffer, 1);
67 * allocate new zcomp_strm structure with ->private initialized by
68 * backend, return NULL on error
70 static struct zcomp_strm *zcomp_strm_alloc(struct zcomp *comp)
72 struct zcomp_strm *zstrm = kmalloc(sizeof(*zstrm), GFP_KERNEL);
76 zstrm->private = comp->backend->create();
78 * allocate 2 pages. 1 for compressed data, plus 1 extra for the
79 * case when compressed size is larger than the original one
81 zstrm->buffer = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
82 if (!zstrm->private || !zstrm->buffer) {
83 zcomp_strm_free(comp, zstrm);
90 * get idle zcomp_strm or wait until other process release
91 * (zcomp_strm_release()) one for us
93 static struct zcomp_strm *zcomp_strm_multi_find(struct zcomp *comp)
95 struct zcomp_strm_multi *zs = comp->stream;
96 struct zcomp_strm *zstrm;
99 spin_lock(&zs->strm_lock);
100 if (!list_empty(&zs->idle_strm)) {
101 zstrm = list_entry(zs->idle_strm.next,
102 struct zcomp_strm, list);
103 list_del(&zstrm->list);
104 spin_unlock(&zs->strm_lock);
107 /* zstrm streams limit reached, wait for idle stream */
108 if (zs->avail_strm >= zs->max_strm) {
109 spin_unlock(&zs->strm_lock);
110 wait_event(zs->strm_wait, !list_empty(&zs->idle_strm));
113 /* allocate new zstrm stream */
115 spin_unlock(&zs->strm_lock);
117 zstrm = zcomp_strm_alloc(comp);
119 spin_lock(&zs->strm_lock);
121 spin_unlock(&zs->strm_lock);
122 wait_event(zs->strm_wait, !list_empty(&zs->idle_strm));
130 /* add stream back to idle list and wake up waiter or free the stream */
131 static void zcomp_strm_multi_release(struct zcomp *comp, struct zcomp_strm *zstrm)
133 struct zcomp_strm_multi *zs = comp->stream;
135 spin_lock(&zs->strm_lock);
136 if (zs->avail_strm <= zs->max_strm) {
137 list_add(&zstrm->list, &zs->idle_strm);
138 spin_unlock(&zs->strm_lock);
139 wake_up(&zs->strm_wait);
144 spin_unlock(&zs->strm_lock);
145 zcomp_strm_free(comp, zstrm);
148 /* change max_strm limit */
149 static int zcomp_strm_multi_set_max_streams(struct zcomp *comp, int num_strm)
151 struct zcomp_strm_multi *zs = comp->stream;
152 struct zcomp_strm *zstrm;
154 spin_lock(&zs->strm_lock);
155 zs->max_strm = num_strm;
157 * if user has lowered the limit and there are idle streams,
158 * immediately free as much streams (and memory) as we can.
160 while (zs->avail_strm > num_strm && !list_empty(&zs->idle_strm)) {
161 zstrm = list_entry(zs->idle_strm.next,
162 struct zcomp_strm, list);
163 list_del(&zstrm->list);
164 zcomp_strm_free(comp, zstrm);
167 spin_unlock(&zs->strm_lock);
171 static void zcomp_strm_multi_destroy(struct zcomp *comp)
173 struct zcomp_strm_multi *zs = comp->stream;
174 struct zcomp_strm *zstrm;
176 while (!list_empty(&zs->idle_strm)) {
177 zstrm = list_entry(zs->idle_strm.next,
178 struct zcomp_strm, list);
179 list_del(&zstrm->list);
180 zcomp_strm_free(comp, zstrm);
185 static int zcomp_strm_multi_create(struct zcomp *comp, int max_strm)
187 struct zcomp_strm *zstrm;
188 struct zcomp_strm_multi *zs;
190 comp->destroy = zcomp_strm_multi_destroy;
191 comp->strm_find = zcomp_strm_multi_find;
192 comp->strm_release = zcomp_strm_multi_release;
193 comp->set_max_streams = zcomp_strm_multi_set_max_streams;
194 zs = kmalloc(sizeof(struct zcomp_strm_multi), GFP_KERNEL);
199 spin_lock_init(&zs->strm_lock);
200 INIT_LIST_HEAD(&zs->idle_strm);
201 init_waitqueue_head(&zs->strm_wait);
202 zs->max_strm = max_strm;
205 zstrm = zcomp_strm_alloc(comp);
210 list_add(&zstrm->list, &zs->idle_strm);
214 static struct zcomp_strm *zcomp_strm_single_find(struct zcomp *comp)
216 struct zcomp_strm_single *zs = comp->stream;
217 mutex_lock(&zs->strm_lock);
221 static void zcomp_strm_single_release(struct zcomp *comp,
222 struct zcomp_strm *zstrm)
224 struct zcomp_strm_single *zs = comp->stream;
225 mutex_unlock(&zs->strm_lock);
228 static int zcomp_strm_single_set_max_streams(struct zcomp *comp, int num_strm)
230 /* zcomp_strm_single support only max_comp_streams == 1 */
234 static void zcomp_strm_single_destroy(struct zcomp *comp)
236 struct zcomp_strm_single *zs = comp->stream;
237 zcomp_strm_free(comp, zs->zstrm);
241 static int zcomp_strm_single_create(struct zcomp *comp)
243 struct zcomp_strm_single *zs;
245 comp->destroy = zcomp_strm_single_destroy;
246 comp->strm_find = zcomp_strm_single_find;
247 comp->strm_release = zcomp_strm_single_release;
248 comp->set_max_streams = zcomp_strm_single_set_max_streams;
249 zs = kmalloc(sizeof(struct zcomp_strm_single), GFP_KERNEL);
254 mutex_init(&zs->strm_lock);
255 zs->zstrm = zcomp_strm_alloc(comp);
263 /* show available compressors */
264 ssize_t zcomp_available_show(const char *comp, char *buf)
269 while (backends[i]) {
270 if (sysfs_streq(comp, backends[i]->name))
271 sz += sprintf(buf + sz, "[%s] ", backends[i]->name);
273 sz += sprintf(buf + sz, "%s ", backends[i]->name);
276 sz += sprintf(buf + sz, "\n");
280 int zcomp_set_max_streams(struct zcomp *comp, int num_strm)
282 return comp->set_max_streams(comp, num_strm);
285 struct zcomp_strm *zcomp_strm_find(struct zcomp *comp)
287 return comp->strm_find(comp);
290 void zcomp_strm_release(struct zcomp *comp, struct zcomp_strm *zstrm)
292 comp->strm_release(comp, zstrm);
295 int zcomp_compress(struct zcomp *comp, struct zcomp_strm *zstrm,
296 const unsigned char *src, size_t *dst_len)
298 return comp->backend->compress(src, zstrm->buffer, dst_len,
302 int zcomp_decompress(struct zcomp *comp, const unsigned char *src,
303 size_t src_len, unsigned char *dst)
305 return comp->backend->decompress(src, src_len, dst);
308 void zcomp_destroy(struct zcomp *comp)
315 * search available compressors for requested algorithm.
316 * allocate new zcomp and initialize it. return NULL
317 * if requested algorithm is not supported or in case
320 struct zcomp *zcomp_create(const char *compress, int max_strm)
323 struct zcomp_backend *backend;
325 backend = find_backend(compress);
329 comp = kzalloc(sizeof(struct zcomp), GFP_KERNEL);
333 comp->backend = backend;
335 zcomp_strm_multi_create(comp, max_strm);
337 zcomp_strm_single_create(comp);