1
2
3
4
5
6
7
8
9
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12#include <linux/list.h>
13#include <linux/types.h>
14#include <linux/mm.h>
15#include <linux/slab.h>
16#include <linux/spinlock.h>
17#include <linux/module.h>
18#include <linux/zpool.h>
19
20struct zpool {
21 char *type;
22
23 struct zpool_driver *driver;
24 void *pool;
25 struct zpool_ops *ops;
26
27 struct list_head list;
28};
29
30static LIST_HEAD(drivers_head);
31static DEFINE_SPINLOCK(drivers_lock);
32
33static LIST_HEAD(pools_head);
34static DEFINE_SPINLOCK(pools_lock);
35
36
37
38
39
40void zpool_register_driver(struct zpool_driver *driver)
41{
42 spin_lock(&drivers_lock);
43 atomic_set(&driver->refcount, 0);
44 list_add(&driver->list, &drivers_head);
45 spin_unlock(&drivers_lock);
46}
47EXPORT_SYMBOL(zpool_register_driver);
48
49
50
51
52
53
54
55
56
57
58
59int zpool_unregister_driver(struct zpool_driver *driver)
60{
61 int ret = 0, refcount;
62
63 spin_lock(&drivers_lock);
64 refcount = atomic_read(&driver->refcount);
65 WARN_ON(refcount < 0);
66 if (refcount > 0)
67 ret = -EBUSY;
68 else
69 list_del(&driver->list);
70 spin_unlock(&drivers_lock);
71
72 return ret;
73}
74EXPORT_SYMBOL(zpool_unregister_driver);
75
76
77
78
79
80
81
82
83
84int zpool_evict(void *pool, unsigned long handle)
85{
86 struct zpool *zpool;
87
88 spin_lock(&pools_lock);
89 list_for_each_entry(zpool, &pools_head, list) {
90 if (zpool->pool == pool) {
91 spin_unlock(&pools_lock);
92 if (!zpool->ops || !zpool->ops->evict)
93 return -EINVAL;
94 return zpool->ops->evict(zpool, handle);
95 }
96 }
97 spin_unlock(&pools_lock);
98
99 return -ENOENT;
100}
101EXPORT_SYMBOL(zpool_evict);
102
103static struct zpool_driver *zpool_get_driver(char *type)
104{
105 struct zpool_driver *driver;
106
107 spin_lock(&drivers_lock);
108 list_for_each_entry(driver, &drivers_head, list) {
109 if (!strcmp(driver->type, type)) {
110 bool got = try_module_get(driver->owner);
111
112 if (got)
113 atomic_inc(&driver->refcount);
114 spin_unlock(&drivers_lock);
115 return got ? driver : NULL;
116 }
117 }
118
119 spin_unlock(&drivers_lock);
120 return NULL;
121}
122
123static void zpool_put_driver(struct zpool_driver *driver)
124{
125 atomic_dec(&driver->refcount);
126 module_put(driver->owner);
127}
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144struct zpool *zpool_create_pool(char *type, char *name, gfp_t gfp,
145 struct zpool_ops *ops)
146{
147 struct zpool_driver *driver;
148 struct zpool *zpool;
149
150 pr_info("creating pool type %s\n", type);
151
152 driver = zpool_get_driver(type);
153
154 if (!driver) {
155 request_module("zpool-%s", type);
156 driver = zpool_get_driver(type);
157 }
158
159 if (!driver) {
160 pr_err("no driver for type %s\n", type);
161 return NULL;
162 }
163
164 zpool = kmalloc(sizeof(*zpool), gfp);
165 if (!zpool) {
166 pr_err("couldn't create zpool - out of memory\n");
167 zpool_put_driver(driver);
168 return NULL;
169 }
170
171 zpool->type = driver->type;
172 zpool->driver = driver;
173 zpool->pool = driver->create(name, gfp, ops);
174 zpool->ops = ops;
175
176 if (!zpool->pool) {
177 pr_err("couldn't create %s pool\n", type);
178 zpool_put_driver(driver);
179 kfree(zpool);
180 return NULL;
181 }
182
183 pr_info("created %s pool\n", type);
184
185 spin_lock(&pools_lock);
186 list_add(&zpool->list, &pools_head);
187 spin_unlock(&pools_lock);
188
189 return zpool;
190}
191
192
193
194
195
196
197
198
199
200
201
202
203void zpool_destroy_pool(struct zpool *zpool)
204{
205 pr_info("destroying pool type %s\n", zpool->type);
206
207 spin_lock(&pools_lock);
208 list_del(&zpool->list);
209 spin_unlock(&pools_lock);
210 zpool->driver->destroy(zpool->pool);
211 zpool_put_driver(zpool->driver);
212 kfree(zpool);
213}
214
215
216
217
218
219
220
221
222
223
224
225char *zpool_get_type(struct zpool *zpool)
226{
227 return zpool->type;
228}
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246int zpool_malloc(struct zpool *zpool, size_t size, gfp_t gfp,
247 unsigned long *handle)
248{
249 return zpool->driver->malloc(zpool->pool, size, gfp, handle);
250}
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266void zpool_free(struct zpool *zpool, unsigned long handle)
267{
268 zpool->driver->free(zpool->pool, handle);
269}
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288int zpool_shrink(struct zpool *zpool, unsigned int pages,
289 unsigned int *reclaimed)
290{
291 return zpool->driver->shrink(zpool->pool, pages, reclaimed);
292}
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316void *zpool_map_handle(struct zpool *zpool, unsigned long handle,
317 enum zpool_mapmode mapmode)
318{
319 return zpool->driver->map(zpool->pool, handle, mapmode);
320}
321
322
323
324
325
326
327
328
329
330
331
332void zpool_unmap_handle(struct zpool *zpool, unsigned long handle)
333{
334 zpool->driver->unmap(zpool->pool, handle);
335}
336
337
338
339
340
341
342
343
344
345u64 zpool_get_total_size(struct zpool *zpool)
346{
347 return zpool->driver->total_size(zpool->pool);
348}
349
350static int __init init_zpool(void)
351{
352 pr_info("loaded\n");
353 return 0;
354}
355
356static void __exit exit_zpool(void)
357{
358 pr_info("unloaded\n");
359}
360
361module_init(init_zpool);
362module_exit(exit_zpool);
363
364MODULE_LICENSE("GPL");
365MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>");
366MODULE_DESCRIPTION("Common API for compressed memory storage");
367