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;
27 static struct zcomp_backend *find_backend(const char *compress)
29 if (strncmp(compress, "lzo", 3) == 0)
34 static void zcomp_strm_free(struct zcomp *comp, struct zcomp_strm *zstrm)
37 comp->backend->destroy(zstrm->private);
38 free_pages((unsigned long)zstrm->buffer, 1);
43 * allocate new zcomp_strm structure with ->private initialized by
44 * backend, return NULL on error
46 static struct zcomp_strm *zcomp_strm_alloc(struct zcomp *comp)
48 struct zcomp_strm *zstrm = kmalloc(sizeof(*zstrm), GFP_KERNEL);
52 zstrm->private = comp->backend->create();
54 * allocate 2 pages. 1 for compressed data, plus 1 extra for the
55 * case when compressed size is larger than the original one
57 zstrm->buffer = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
58 if (!zstrm->private || !zstrm->buffer) {
59 zcomp_strm_free(comp, zstrm);
65 static struct zcomp_strm *zcomp_strm_single_find(struct zcomp *comp)
67 struct zcomp_strm_single *zs = comp->stream;
68 mutex_lock(&zs->strm_lock);
72 static void zcomp_strm_single_release(struct zcomp *comp,
73 struct zcomp_strm *zstrm)
75 struct zcomp_strm_single *zs = comp->stream;
76 mutex_unlock(&zs->strm_lock);
79 static void zcomp_strm_single_destroy(struct zcomp *comp)
81 struct zcomp_strm_single *zs = comp->stream;
82 zcomp_strm_free(comp, zs->zstrm);
86 static int zcomp_strm_single_create(struct zcomp *comp)
88 struct zcomp_strm_single *zs;
90 comp->destroy = zcomp_strm_single_destroy;
91 comp->strm_find = zcomp_strm_single_find;
92 comp->strm_release = zcomp_strm_single_release;
93 zs = kmalloc(sizeof(struct zcomp_strm_single), GFP_KERNEL);
98 mutex_init(&zs->strm_lock);
99 zs->zstrm = zcomp_strm_alloc(comp);
107 struct zcomp_strm *zcomp_strm_find(struct zcomp *comp)
109 return comp->strm_find(comp);
112 void zcomp_strm_release(struct zcomp *comp, struct zcomp_strm *zstrm)
114 comp->strm_release(comp, zstrm);
117 int zcomp_compress(struct zcomp *comp, struct zcomp_strm *zstrm,
118 const unsigned char *src, size_t *dst_len)
120 return comp->backend->compress(src, zstrm->buffer, dst_len,
124 int zcomp_decompress(struct zcomp *comp, const unsigned char *src,
125 size_t src_len, unsigned char *dst)
127 return comp->backend->decompress(src, src_len, dst);
130 void zcomp_destroy(struct zcomp *comp)
137 * search available compressors for requested algorithm.
138 * allocate new zcomp and initialize it. return NULL
139 * if requested algorithm is not supported or in case
142 struct zcomp *zcomp_create(const char *compress)
145 struct zcomp_backend *backend;
147 backend = find_backend(compress);
151 comp = kzalloc(sizeof(struct zcomp), GFP_KERNEL);
155 comp->backend = backend;
156 if (zcomp_strm_single_create(comp) != 0) {