zram: factor out single stream compression
[firefly-linux-kernel-4.4.55.git] / drivers / block / zram / zcomp.c
1 /*
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
19 /*
20  * single zcomp_strm backend
21  */
22 struct zcomp_strm_single {
23         struct mutex strm_lock;
24         struct zcomp_strm *zstrm;
25 };
26
27 static 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
34 static 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  */
46 static 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
65 static 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
72 static 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
79 static 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
86 static 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
107 struct zcomp_strm *zcomp_strm_find(struct zcomp *comp)
108 {
109         return comp->strm_find(comp);
110 }
111
112 void zcomp_strm_release(struct zcomp *comp, struct zcomp_strm *zstrm)
113 {
114         comp->strm_release(comp, zstrm);
115 }
116
117 int 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
124 int 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
130 void zcomp_destroy(struct zcomp *comp)
131 {
132         comp->destroy(comp);
133         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  */
142 struct 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;
156         if (zcomp_strm_single_create(comp) != 0) {
157                 kfree(comp);
158                 return NULL;
159         }
160         return comp;
161 }