blob: 72e8071f9d73e1d74940bf2f06e89bfb1018fb3a [file] [log] [blame]
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -07001/*
2 * Copyright (C) 2014 Sergey Senozhatsky.
3 *
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.
8 */
9
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>
15
16#include "zcomp.h"
17#include "zcomp_lzo.h"
18
Sergey Senozhatsky9cc97522014-04-07 15:38:13 -070019/*
20 * single zcomp_strm backend
21 */
22struct zcomp_strm_single {
23 struct mutex strm_lock;
24 struct zcomp_strm *zstrm;
25};
26
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -070027static struct zcomp_backend *find_backend(const char *compress)
28{
29 if (strncmp(compress, "lzo", 3) == 0)
30 return &zcomp_lzo;
31 return NULL;
32}
33
34static void zcomp_strm_free(struct zcomp *comp, struct zcomp_strm *zstrm)
35{
36 if (zstrm->private)
37 comp->backend->destroy(zstrm->private);
38 free_pages((unsigned long)zstrm->buffer, 1);
39 kfree(zstrm);
40}
41
42/*
43 * allocate new zcomp_strm structure with ->private initialized by
44 * backend, return NULL on error
45 */
46static struct zcomp_strm *zcomp_strm_alloc(struct zcomp *comp)
47{
48 struct zcomp_strm *zstrm = kmalloc(sizeof(*zstrm), GFP_KERNEL);
49 if (!zstrm)
50 return NULL;
51
52 zstrm->private = comp->backend->create();
53 /*
54 * allocate 2 pages. 1 for compressed data, plus 1 extra for the
55 * case when compressed size is larger than the original one
56 */
57 zstrm->buffer = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
58 if (!zstrm->private || !zstrm->buffer) {
59 zcomp_strm_free(comp, zstrm);
60 zstrm = NULL;
61 }
62 return zstrm;
63}
64
Sergey Senozhatsky9cc97522014-04-07 15:38:13 -070065static struct zcomp_strm *zcomp_strm_single_find(struct zcomp *comp)
66{
67 struct zcomp_strm_single *zs = comp->stream;
68 mutex_lock(&zs->strm_lock);
69 return zs->zstrm;
70}
71
72static void zcomp_strm_single_release(struct zcomp *comp,
73 struct zcomp_strm *zstrm)
74{
75 struct zcomp_strm_single *zs = comp->stream;
76 mutex_unlock(&zs->strm_lock);
77}
78
79static void zcomp_strm_single_destroy(struct zcomp *comp)
80{
81 struct zcomp_strm_single *zs = comp->stream;
82 zcomp_strm_free(comp, zs->zstrm);
83 kfree(zs);
84}
85
86static int zcomp_strm_single_create(struct zcomp *comp)
87{
88 struct zcomp_strm_single *zs;
89
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);
94 if (!zs)
95 return -ENOMEM;
96
97 comp->stream = zs;
98 mutex_init(&zs->strm_lock);
99 zs->zstrm = zcomp_strm_alloc(comp);
100 if (!zs->zstrm) {
101 kfree(zs);
102 return -ENOMEM;
103 }
104 return 0;
105}
106
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -0700107struct zcomp_strm *zcomp_strm_find(struct zcomp *comp)
108{
Sergey Senozhatsky9cc97522014-04-07 15:38:13 -0700109 return comp->strm_find(comp);
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -0700110}
111
112void zcomp_strm_release(struct zcomp *comp, struct zcomp_strm *zstrm)
113{
Sergey Senozhatsky9cc97522014-04-07 15:38:13 -0700114 comp->strm_release(comp, zstrm);
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -0700115}
116
117int zcomp_compress(struct zcomp *comp, struct zcomp_strm *zstrm,
118 const unsigned char *src, size_t *dst_len)
119{
120 return comp->backend->compress(src, zstrm->buffer, dst_len,
121 zstrm->private);
122}
123
124int zcomp_decompress(struct zcomp *comp, const unsigned char *src,
125 size_t src_len, unsigned char *dst)
126{
127 return comp->backend->decompress(src, src_len, dst);
128}
129
130void zcomp_destroy(struct zcomp *comp)
131{
Sergey Senozhatsky9cc97522014-04-07 15:38:13 -0700132 comp->destroy(comp);
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -0700133 kfree(comp);
134}
135
136/*
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
140 * of init error
141 */
142struct zcomp *zcomp_create(const char *compress)
143{
144 struct zcomp *comp;
145 struct zcomp_backend *backend;
146
147 backend = find_backend(compress);
148 if (!backend)
149 return NULL;
150
151 comp = kzalloc(sizeof(struct zcomp), GFP_KERNEL);
152 if (!comp)
153 return NULL;
154
155 comp->backend = backend;
Sergey Senozhatsky9cc97522014-04-07 15:38:13 -0700156 if (zcomp_strm_single_create(comp) != 0) {
Sergey Senozhatskye7e1ef42014-04-07 15:38:11 -0700157 kfree(comp);
158 return NULL;
159 }
160 return comp;
161}