staging:iio: Add event monitor example application
[linux-block.git] / drivers / staging / zram / zram_drv.c
CommitLineData
306b0c95 1/*
f1e3cfff 2 * Compressed RAM block device
306b0c95 3 *
1130ebba 4 * Copyright (C) 2008, 2009, 2010 Nitin Gupta
306b0c95
NG
5 *
6 * This code is released using a dual license strategy: BSD/GPL
7 * You can choose the licence that better fits your requirements.
8 *
9 * Released under the terms of 3-clause BSD License
10 * Released under the terms of GNU General Public License Version 2.0
11 *
12 * Project home: http://compcache.googlecode.com
13 */
14
f1e3cfff 15#define KMSG_COMPONENT "zram"
306b0c95
NG
16#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
17
b1f5b81e
RJ
18#ifdef CONFIG_ZRAM_DEBUG
19#define DEBUG
20#endif
21
306b0c95
NG
22#include <linux/module.h>
23#include <linux/kernel.h>
8946a086 24#include <linux/bio.h>
306b0c95
NG
25#include <linux/bitops.h>
26#include <linux/blkdev.h>
27#include <linux/buffer_head.h>
28#include <linux/device.h>
29#include <linux/genhd.h>
30#include <linux/highmem.h>
5a0e3ad6 31#include <linux/slab.h>
306b0c95 32#include <linux/lzo.h>
306b0c95 33#include <linux/string.h>
306b0c95 34#include <linux/vmalloc.h>
306b0c95 35
16a4bfb9 36#include "zram_drv.h"
306b0c95
NG
37
38/* Globals */
f1e3cfff 39static int zram_major;
43801f6e 40struct zram *zram_devices;
306b0c95 41
306b0c95 42/* Module params (documentation at end) */
efd54f43 43unsigned int zram_num_devices;
33863c21
NG
44
45static void zram_stat_inc(u32 *v)
46{
47 *v = *v + 1;
48}
49
50static void zram_stat_dec(u32 *v)
51{
52 *v = *v - 1;
53}
54
55static void zram_stat64_add(struct zram *zram, u64 *v, u64 inc)
56{
57 spin_lock(&zram->stat64_lock);
58 *v = *v + inc;
59 spin_unlock(&zram->stat64_lock);
60}
61
62static void zram_stat64_sub(struct zram *zram, u64 *v, u64 dec)
63{
64 spin_lock(&zram->stat64_lock);
65 *v = *v - dec;
66 spin_unlock(&zram->stat64_lock);
67}
68
69static void zram_stat64_inc(struct zram *zram, u64 *v)
70{
71 zram_stat64_add(zram, v, 1);
72}
306b0c95 73
f1e3cfff
NG
74static int zram_test_flag(struct zram *zram, u32 index,
75 enum zram_pageflags flag)
306b0c95 76{
f1e3cfff 77 return zram->table[index].flags & BIT(flag);
306b0c95
NG
78}
79
f1e3cfff
NG
80static void zram_set_flag(struct zram *zram, u32 index,
81 enum zram_pageflags flag)
306b0c95 82{
f1e3cfff 83 zram->table[index].flags |= BIT(flag);
306b0c95
NG
84}
85
f1e3cfff
NG
86static void zram_clear_flag(struct zram *zram, u32 index,
87 enum zram_pageflags flag)
306b0c95 88{
f1e3cfff 89 zram->table[index].flags &= ~BIT(flag);
306b0c95
NG
90}
91
92static int page_zero_filled(void *ptr)
93{
94 unsigned int pos;
95 unsigned long *page;
96
97 page = (unsigned long *)ptr;
98
99 for (pos = 0; pos != PAGE_SIZE / sizeof(*page); pos++) {
100 if (page[pos])
101 return 0;
102 }
103
104 return 1;
105}
106
f1e3cfff 107static void zram_set_disksize(struct zram *zram, size_t totalram_bytes)
306b0c95 108{
f1e3cfff 109 if (!zram->disksize) {
306b0c95
NG
110 pr_info(
111 "disk size not provided. You can use disksize_kb module "
112 "param to specify size.\nUsing default: (%u%% of RAM).\n",
113 default_disksize_perc_ram
114 );
f1e3cfff 115 zram->disksize = default_disksize_perc_ram *
306b0c95
NG
116 (totalram_bytes / 100);
117 }
118
f1e3cfff 119 if (zram->disksize > 2 * (totalram_bytes)) {
306b0c95 120 pr_info(
f1e3cfff 121 "There is little point creating a zram of greater than "
306b0c95 122 "twice the size of memory since we expect a 2:1 compression "
f1e3cfff
NG
123 "ratio. Note that zram uses about 0.1%% of the size of "
124 "the disk when not in use so a huge zram is "
306b0c95
NG
125 "wasteful.\n"
126 "\tMemory Size: %zu kB\n"
33863c21 127 "\tSize you selected: %llu kB\n"
306b0c95 128 "Continuing anyway ...\n",
f1e3cfff 129 totalram_bytes >> 10, zram->disksize
306b0c95
NG
130 );
131 }
132
f1e3cfff 133 zram->disksize &= PAGE_MASK;
306b0c95
NG
134}
135
f1e3cfff 136static void zram_free_page(struct zram *zram, size_t index)
306b0c95 137{
fd1a30de 138 void *handle = zram->table[index].handle;
306b0c95 139
fd1a30de 140 if (unlikely(!handle)) {
2e882281
NG
141 /*
142 * No memory is allocated for zero filled pages.
143 * Simply clear zero page flag.
144 */
f1e3cfff
NG
145 if (zram_test_flag(zram, index, ZRAM_ZERO)) {
146 zram_clear_flag(zram, index, ZRAM_ZERO);
147 zram_stat_dec(&zram->stats.pages_zero);
306b0c95
NG
148 }
149 return;
150 }
151
f1e3cfff 152 if (unlikely(zram_test_flag(zram, index, ZRAM_UNCOMPRESSED))) {
fd1a30de 153 __free_page(handle);
f1e3cfff
NG
154 zram_clear_flag(zram, index, ZRAM_UNCOMPRESSED);
155 zram_stat_dec(&zram->stats.pages_expand);
306b0c95
NG
156 goto out;
157 }
158
fd1a30de 159 zs_free(zram->mem_pool, handle);
306b0c95 160
fd1a30de 161 if (zram->table[index].size <= PAGE_SIZE / 2)
f1e3cfff 162 zram_stat_dec(&zram->stats.good_compress);
306b0c95
NG
163
164out:
fd1a30de
NG
165 zram_stat64_sub(zram, &zram->stats.compr_size,
166 zram->table[index].size);
f1e3cfff 167 zram_stat_dec(&zram->stats.pages_stored);
306b0c95 168
fd1a30de
NG
169 zram->table[index].handle = NULL;
170 zram->table[index].size = 0;
306b0c95
NG
171}
172
924bd88d 173static void handle_zero_page(struct bio_vec *bvec)
306b0c95 174{
924bd88d 175 struct page *page = bvec->bv_page;
306b0c95 176 void *user_mem;
306b0c95
NG
177
178 user_mem = kmap_atomic(page, KM_USER0);
924bd88d 179 memset(user_mem + bvec->bv_offset, 0, bvec->bv_len);
306b0c95
NG
180 kunmap_atomic(user_mem, KM_USER0);
181
30fb8a71 182 flush_dcache_page(page);
306b0c95
NG
183}
184
924bd88d
JM
185static void handle_uncompressed_page(struct zram *zram, struct bio_vec *bvec,
186 u32 index, int offset)
306b0c95 187{
924bd88d 188 struct page *page = bvec->bv_page;
306b0c95
NG
189 unsigned char *user_mem, *cmem;
190
306b0c95 191 user_mem = kmap_atomic(page, KM_USER0);
fd1a30de 192 cmem = kmap_atomic(zram->table[index].handle, KM_USER1);
306b0c95 193
924bd88d 194 memcpy(user_mem + bvec->bv_offset, cmem + offset, bvec->bv_len);
306b0c95 195 kunmap_atomic(cmem, KM_USER1);
dffbb44d 196 kunmap_atomic(user_mem, KM_USER0);
306b0c95 197
30fb8a71 198 flush_dcache_page(page);
306b0c95
NG
199}
200
924bd88d
JM
201static inline int is_partial_io(struct bio_vec *bvec)
202{
203 return bvec->bv_len != PAGE_SIZE;
204}
205
8c921b2b 206static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
924bd88d 207 u32 index, int offset, struct bio *bio)
306b0c95 208{
8c921b2b
JM
209 int ret;
210 size_t clen;
211 struct page *page;
212 struct zobj_header *zheader;
924bd88d 213 unsigned char *user_mem, *cmem, *uncmem = NULL;
a1dd52af 214
8c921b2b 215 page = bvec->bv_page;
306b0c95 216
8c921b2b 217 if (zram_test_flag(zram, index, ZRAM_ZERO)) {
924bd88d 218 handle_zero_page(bvec);
8c921b2b
JM
219 return 0;
220 }
306b0c95 221
8c921b2b 222 /* Requested page is not present in compressed area */
fd1a30de 223 if (unlikely(!zram->table[index].handle)) {
8c921b2b
JM
224 pr_debug("Read before write: sector=%lu, size=%u",
225 (ulong)(bio->bi_sector), bio->bi_size);
924bd88d 226 handle_zero_page(bvec);
8c921b2b
JM
227 return 0;
228 }
306b0c95 229
8c921b2b
JM
230 /* Page is stored uncompressed since it's incompressible */
231 if (unlikely(zram_test_flag(zram, index, ZRAM_UNCOMPRESSED))) {
924bd88d 232 handle_uncompressed_page(zram, bvec, index, offset);
8c921b2b
JM
233 return 0;
234 }
306b0c95 235
924bd88d
JM
236 if (is_partial_io(bvec)) {
237 /* Use a temporary buffer to decompress the page */
238 uncmem = kmalloc(PAGE_SIZE, GFP_KERNEL);
239 if (!uncmem) {
240 pr_info("Error allocating temp memory!\n");
241 return -ENOMEM;
242 }
243 }
244
8c921b2b 245 user_mem = kmap_atomic(page, KM_USER0);
924bd88d
JM
246 if (!is_partial_io(bvec))
247 uncmem = user_mem;
8c921b2b 248 clen = PAGE_SIZE;
306b0c95 249
fd1a30de 250 cmem = zs_map_object(zram->mem_pool, zram->table[index].handle);
306b0c95 251
8c921b2b 252 ret = lzo1x_decompress_safe(cmem + sizeof(*zheader),
fd1a30de 253 zram->table[index].size,
924bd88d
JM
254 uncmem, &clen);
255
256 if (is_partial_io(bvec)) {
257 memcpy(user_mem + bvec->bv_offset, uncmem + offset,
258 bvec->bv_len);
259 kfree(uncmem);
260 }
306b0c95 261
fd1a30de 262 zs_unmap_object(zram->mem_pool, zram->table[index].handle);
dffbb44d 263 kunmap_atomic(user_mem, KM_USER0);
a1dd52af 264
8c921b2b
JM
265 /* Should NEVER happen. Return bio error if it does. */
266 if (unlikely(ret != LZO_E_OK)) {
267 pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
268 zram_stat64_inc(zram, &zram->stats.failed_reads);
269 return ret;
a1dd52af 270 }
306b0c95 271
8c921b2b 272 flush_dcache_page(page);
306b0c95 273
8c921b2b 274 return 0;
306b0c95
NG
275}
276
924bd88d
JM
277static int zram_read_before_write(struct zram *zram, char *mem, u32 index)
278{
279 int ret;
280 size_t clen = PAGE_SIZE;
281 struct zobj_header *zheader;
282 unsigned char *cmem;
283
284 if (zram_test_flag(zram, index, ZRAM_ZERO) ||
fd1a30de 285 !zram->table[index].handle) {
924bd88d
JM
286 memset(mem, 0, PAGE_SIZE);
287 return 0;
288 }
289
fd1a30de 290 cmem = zs_map_object(zram->mem_pool, zram->table[index].handle);
924bd88d
JM
291
292 /* Page is stored uncompressed since it's incompressible */
293 if (unlikely(zram_test_flag(zram, index, ZRAM_UNCOMPRESSED))) {
294 memcpy(mem, cmem, PAGE_SIZE);
295 kunmap_atomic(cmem, KM_USER0);
296 return 0;
297 }
298
299 ret = lzo1x_decompress_safe(cmem + sizeof(*zheader),
fd1a30de 300 zram->table[index].size,
924bd88d 301 mem, &clen);
fd1a30de 302 zs_unmap_object(zram->mem_pool, zram->table[index].handle);
924bd88d
JM
303
304 /* Should NEVER happen. Return bio error if it does. */
305 if (unlikely(ret != LZO_E_OK)) {
306 pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
307 zram_stat64_inc(zram, &zram->stats.failed_reads);
308 return ret;
309 }
310
311 return 0;
312}
313
314static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
315 int offset)
306b0c95 316{
8c921b2b 317 int ret;
924bd88d 318 u32 store_offset;
8c921b2b 319 size_t clen;
fd1a30de 320 void *handle;
8c921b2b
JM
321 struct zobj_header *zheader;
322 struct page *page, *page_store;
924bd88d 323 unsigned char *user_mem, *cmem, *src, *uncmem = NULL;
306b0c95 324
8c921b2b
JM
325 page = bvec->bv_page;
326 src = zram->compress_buffer;
306b0c95 327
924bd88d
JM
328 if (is_partial_io(bvec)) {
329 /*
330 * This is a partial IO. We need to read the full page
331 * before to write the changes.
332 */
333 uncmem = kmalloc(PAGE_SIZE, GFP_KERNEL);
334 if (!uncmem) {
335 pr_info("Error allocating temp memory!\n");
336 ret = -ENOMEM;
337 goto out;
338 }
339 ret = zram_read_before_write(zram, uncmem, index);
340 if (ret) {
341 kfree(uncmem);
342 goto out;
343 }
344 }
345
8c921b2b
JM
346 /*
347 * System overwrites unused sectors. Free memory associated
348 * with this sector now.
349 */
fd1a30de 350 if (zram->table[index].handle ||
8c921b2b
JM
351 zram_test_flag(zram, index, ZRAM_ZERO))
352 zram_free_page(zram, index);
306b0c95 353
8c921b2b 354 user_mem = kmap_atomic(page, KM_USER0);
924bd88d
JM
355
356 if (is_partial_io(bvec))
357 memcpy(uncmem + offset, user_mem + bvec->bv_offset,
358 bvec->bv_len);
359 else
360 uncmem = user_mem;
361
362 if (page_zero_filled(uncmem)) {
8c921b2b 363 kunmap_atomic(user_mem, KM_USER0);
924bd88d
JM
364 if (is_partial_io(bvec))
365 kfree(uncmem);
8c921b2b
JM
366 zram_stat_inc(&zram->stats.pages_zero);
367 zram_set_flag(zram, index, ZRAM_ZERO);
924bd88d
JM
368 ret = 0;
369 goto out;
8c921b2b 370 }
306b0c95 371
924bd88d 372 ret = lzo1x_1_compress(uncmem, PAGE_SIZE, src, &clen,
8c921b2b 373 zram->compress_workmem);
306b0c95 374
8c921b2b 375 kunmap_atomic(user_mem, KM_USER0);
924bd88d
JM
376 if (is_partial_io(bvec))
377 kfree(uncmem);
306b0c95 378
8c921b2b 379 if (unlikely(ret != LZO_E_OK)) {
8c921b2b 380 pr_err("Compression failed! err=%d\n", ret);
924bd88d 381 goto out;
8c921b2b 382 }
306b0c95 383
8c921b2b
JM
384 /*
385 * Page is incompressible. Store it as-is (uncompressed)
386 * since we do not want to return too many disk write
387 * errors which has side effect of hanging the system.
388 */
389 if (unlikely(clen > max_zpage_size)) {
390 clen = PAGE_SIZE;
391 page_store = alloc_page(GFP_NOIO | __GFP_HIGHMEM);
392 if (unlikely(!page_store)) {
8c921b2b
JM
393 pr_info("Error allocating memory for "
394 "incompressible page: %u\n", index);
924bd88d
JM
395 ret = -ENOMEM;
396 goto out;
397 }
a1dd52af 398
924bd88d 399 store_offset = 0;
8c921b2b
JM
400 zram_set_flag(zram, index, ZRAM_UNCOMPRESSED);
401 zram_stat_inc(&zram->stats.pages_expand);
fd1a30de 402 handle = page_store;
8c921b2b 403 src = kmap_atomic(page, KM_USER0);
fd1a30de 404 cmem = kmap_atomic(page_store, KM_USER1);
8c921b2b
JM
405 goto memstore;
406 }
306b0c95 407
fd1a30de
NG
408 handle = zs_malloc(zram->mem_pool, clen + sizeof(*zheader));
409 if (!handle) {
8c921b2b
JM
410 pr_info("Error allocating memory for compressed "
411 "page: %u, size=%zu\n", index, clen);
924bd88d
JM
412 ret = -ENOMEM;
413 goto out;
8c921b2b 414 }
fd1a30de 415 cmem = zs_map_object(zram->mem_pool, handle);
306b0c95
NG
416
417memstore:
306b0c95 418#if 0
8c921b2b
JM
419 /* Back-reference needed for memory defragmentation */
420 if (!zram_test_flag(zram, index, ZRAM_UNCOMPRESSED)) {
421 zheader = (struct zobj_header *)cmem;
422 zheader->table_idx = index;
423 cmem += sizeof(*zheader);
424 }
306b0c95
NG
425#endif
426
8c921b2b 427 memcpy(cmem, src, clen);
306b0c95 428
fd1a30de
NG
429 if (unlikely(zram_test_flag(zram, index, ZRAM_UNCOMPRESSED))) {
430 kunmap_atomic(cmem, KM_USER1);
8c921b2b 431 kunmap_atomic(src, KM_USER0);
fd1a30de
NG
432 } else {
433 zs_unmap_object(zram->mem_pool, handle);
434 }
435
436 zram->table[index].handle = handle;
437 zram->table[index].size = clen;
306b0c95 438
8c921b2b
JM
439 /* Update stats */
440 zram_stat64_add(zram, &zram->stats.compr_size, clen);
441 zram_stat_inc(&zram->stats.pages_stored);
442 if (clen <= PAGE_SIZE / 2)
443 zram_stat_inc(&zram->stats.good_compress);
306b0c95 444
8c921b2b 445 return 0;
924bd88d
JM
446
447out:
448 if (ret)
449 zram_stat64_inc(zram, &zram->stats.failed_writes);
450 return ret;
8c921b2b
JM
451}
452
453static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
924bd88d 454 int offset, struct bio *bio, int rw)
8c921b2b 455{
c5bde238 456 int ret;
8c921b2b 457
c5bde238
JM
458 if (rw == READ) {
459 down_read(&zram->lock);
460 ret = zram_bvec_read(zram, bvec, index, offset, bio);
461 up_read(&zram->lock);
462 } else {
463 down_write(&zram->lock);
464 ret = zram_bvec_write(zram, bvec, index, offset);
465 up_write(&zram->lock);
466 }
467
468 return ret;
924bd88d
JM
469}
470
471static void update_position(u32 *index, int *offset, struct bio_vec *bvec)
472{
473 if (*offset + bvec->bv_len >= PAGE_SIZE)
474 (*index)++;
475 *offset = (*offset + bvec->bv_len) % PAGE_SIZE;
8c921b2b
JM
476}
477
478static void __zram_make_request(struct zram *zram, struct bio *bio, int rw)
479{
924bd88d 480 int i, offset;
8c921b2b
JM
481 u32 index;
482 struct bio_vec *bvec;
483
484 switch (rw) {
485 case READ:
486 zram_stat64_inc(zram, &zram->stats.num_reads);
487 break;
488 case WRITE:
489 zram_stat64_inc(zram, &zram->stats.num_writes);
490 break;
491 }
492
493 index = bio->bi_sector >> SECTORS_PER_PAGE_SHIFT;
924bd88d 494 offset = (bio->bi_sector & (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT;
8c921b2b
JM
495
496 bio_for_each_segment(bvec, bio, i) {
924bd88d
JM
497 int max_transfer_size = PAGE_SIZE - offset;
498
499 if (bvec->bv_len > max_transfer_size) {
500 /*
501 * zram_bvec_rw() can only make operation on a single
502 * zram page. Split the bio vector.
503 */
504 struct bio_vec bv;
505
506 bv.bv_page = bvec->bv_page;
507 bv.bv_len = max_transfer_size;
508 bv.bv_offset = bvec->bv_offset;
509
510 if (zram_bvec_rw(zram, &bv, index, offset, bio, rw) < 0)
511 goto out;
512
513 bv.bv_len = bvec->bv_len - max_transfer_size;
514 bv.bv_offset += max_transfer_size;
515 if (zram_bvec_rw(zram, &bv, index+1, 0, bio, rw) < 0)
516 goto out;
517 } else
518 if (zram_bvec_rw(zram, bvec, index, offset, bio, rw)
519 < 0)
520 goto out;
521
522 update_position(&index, &offset, bvec);
a1dd52af 523 }
306b0c95
NG
524
525 set_bit(BIO_UPTODATE, &bio->bi_flags);
526 bio_endio(bio, 0);
7d7854b4 527 return;
306b0c95
NG
528
529out:
306b0c95 530 bio_io_error(bio);
306b0c95
NG
531}
532
306b0c95 533/*
924bd88d 534 * Check if request is within bounds and aligned on zram logical blocks.
306b0c95 535 */
f1e3cfff 536static inline int valid_io_request(struct zram *zram, struct bio *bio)
306b0c95
NG
537{
538 if (unlikely(
f1e3cfff 539 (bio->bi_sector >= (zram->disksize >> SECTOR_SHIFT)) ||
924bd88d
JM
540 (bio->bi_sector & (ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1)) ||
541 (bio->bi_size & (ZRAM_LOGICAL_BLOCK_SIZE - 1)))) {
306b0c95
NG
542
543 return 0;
544 }
545
a1dd52af 546 /* I/O request is valid */
306b0c95
NG
547 return 1;
548}
549
550/*
f1e3cfff 551 * Handler function for all zram I/O requests.
306b0c95 552 */
5a7bbad2 553static void zram_make_request(struct request_queue *queue, struct bio *bio)
306b0c95 554{
f1e3cfff 555 struct zram *zram = queue->queuedata;
306b0c95 556
0900beae
JM
557 if (unlikely(!zram->init_done) && zram_init_device(zram))
558 goto error;
559
560 down_read(&zram->init_lock);
561 if (unlikely(!zram->init_done))
562 goto error_unlock;
563
f1e3cfff
NG
564 if (!valid_io_request(zram, bio)) {
565 zram_stat64_inc(zram, &zram->stats.invalid_io);
0900beae 566 goto error_unlock;
6642a67c
JM
567 }
568
8c921b2b 569 __zram_make_request(zram, bio, bio_data_dir(bio));
0900beae 570 up_read(&zram->init_lock);
306b0c95 571
b4fdcb02 572 return;
0900beae
JM
573
574error_unlock:
575 up_read(&zram->init_lock);
576error:
577 bio_io_error(bio);
306b0c95
NG
578}
579
0900beae 580void __zram_reset_device(struct zram *zram)
306b0c95 581{
97a06382 582 size_t index;
306b0c95 583
f1e3cfff 584 zram->init_done = 0;
7eef7533 585
306b0c95 586 /* Free various per-device buffers */
f1e3cfff
NG
587 kfree(zram->compress_workmem);
588 free_pages((unsigned long)zram->compress_buffer, 1);
306b0c95 589
f1e3cfff
NG
590 zram->compress_workmem = NULL;
591 zram->compress_buffer = NULL;
306b0c95 592
f1e3cfff
NG
593 /* Free all pages that are still in this zram device */
594 for (index = 0; index < zram->disksize >> PAGE_SHIFT; index++) {
fd1a30de
NG
595 void *handle = zram->table[index].handle;
596 if (!handle)
306b0c95
NG
597 continue;
598
f1e3cfff 599 if (unlikely(zram_test_flag(zram, index, ZRAM_UNCOMPRESSED)))
fd1a30de 600 __free_page(handle);
306b0c95 601 else
fd1a30de 602 zs_free(zram->mem_pool, handle);
306b0c95
NG
603 }
604
f1e3cfff
NG
605 vfree(zram->table);
606 zram->table = NULL;
306b0c95 607
fd1a30de 608 zs_destroy_pool(zram->mem_pool);
f1e3cfff 609 zram->mem_pool = NULL;
306b0c95 610
306b0c95 611 /* Reset stats */
f1e3cfff 612 memset(&zram->stats, 0, sizeof(zram->stats));
306b0c95 613
f1e3cfff 614 zram->disksize = 0;
0900beae
JM
615}
616
617void zram_reset_device(struct zram *zram)
618{
619 down_write(&zram->init_lock);
620 __zram_reset_device(zram);
621 up_write(&zram->init_lock);
306b0c95
NG
622}
623
33863c21 624int zram_init_device(struct zram *zram)
306b0c95
NG
625{
626 int ret;
627 size_t num_pages;
306b0c95 628
0900beae 629 down_write(&zram->init_lock);
484875ad 630
f1e3cfff 631 if (zram->init_done) {
0900beae 632 up_write(&zram->init_lock);
484875ad 633 return 0;
306b0c95
NG
634 }
635
f1e3cfff 636 zram_set_disksize(zram, totalram_pages << PAGE_SHIFT);
306b0c95 637
f1e3cfff
NG
638 zram->compress_workmem = kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
639 if (!zram->compress_workmem) {
306b0c95
NG
640 pr_err("Error allocating compressor working memory!\n");
641 ret = -ENOMEM;
5a18c531 642 goto fail_no_table;
306b0c95
NG
643 }
644
fb927284
JM
645 zram->compress_buffer =
646 (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
f1e3cfff 647 if (!zram->compress_buffer) {
306b0c95
NG
648 pr_err("Error allocating compressor buffer space\n");
649 ret = -ENOMEM;
5a18c531 650 goto fail_no_table;
306b0c95
NG
651 }
652
f1e3cfff 653 num_pages = zram->disksize >> PAGE_SHIFT;
5b84cc78 654 zram->table = vzalloc(num_pages * sizeof(*zram->table));
f1e3cfff
NG
655 if (!zram->table) {
656 pr_err("Error allocating zram address table\n");
306b0c95 657 ret = -ENOMEM;
5a18c531 658 goto fail_no_table;
306b0c95 659 }
306b0c95 660
f1e3cfff 661 set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
306b0c95 662
f1e3cfff
NG
663 /* zram devices sort of resembles non-rotational disks */
664 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zram->disk->queue);
306b0c95 665
fd1a30de 666 zram->mem_pool = zs_create_pool("zram", GFP_NOIO | __GFP_HIGHMEM);
f1e3cfff 667 if (!zram->mem_pool) {
306b0c95
NG
668 pr_err("Error creating memory pool\n");
669 ret = -ENOMEM;
670 goto fail;
671 }
672
f1e3cfff 673 zram->init_done = 1;
0900beae 674 up_write(&zram->init_lock);
306b0c95
NG
675
676 pr_debug("Initialization done!\n");
677 return 0;
678
5a18c531
JM
679fail_no_table:
680 /* To prevent accessing table entries during cleanup */
681 zram->disksize = 0;
306b0c95 682fail:
0900beae
JM
683 __zram_reset_device(zram);
684 up_write(&zram->init_lock);
306b0c95
NG
685 pr_err("Initialization failed: err=%d\n", ret);
686 return ret;
687}
688
2ccbec05
NG
689static void zram_slot_free_notify(struct block_device *bdev,
690 unsigned long index)
107c161b 691{
f1e3cfff 692 struct zram *zram;
107c161b 693
f1e3cfff
NG
694 zram = bdev->bd_disk->private_data;
695 zram_free_page(zram, index);
696 zram_stat64_inc(zram, &zram->stats.notify_free);
107c161b
NG
697}
698
f1e3cfff 699static const struct block_device_operations zram_devops = {
f1e3cfff 700 .swap_slot_free_notify = zram_slot_free_notify,
107c161b 701 .owner = THIS_MODULE
306b0c95
NG
702};
703
f1e3cfff 704static int create_device(struct zram *zram, int device_id)
306b0c95 705{
de1a21a0
NG
706 int ret = 0;
707
c5bde238 708 init_rwsem(&zram->lock);
0900beae 709 init_rwsem(&zram->init_lock);
f1e3cfff 710 spin_lock_init(&zram->stat64_lock);
306b0c95 711
f1e3cfff
NG
712 zram->queue = blk_alloc_queue(GFP_KERNEL);
713 if (!zram->queue) {
306b0c95
NG
714 pr_err("Error allocating disk queue for device %d\n",
715 device_id);
de1a21a0
NG
716 ret = -ENOMEM;
717 goto out;
306b0c95
NG
718 }
719
f1e3cfff
NG
720 blk_queue_make_request(zram->queue, zram_make_request);
721 zram->queue->queuedata = zram;
306b0c95
NG
722
723 /* gendisk structure */
f1e3cfff
NG
724 zram->disk = alloc_disk(1);
725 if (!zram->disk) {
726 blk_cleanup_queue(zram->queue);
306b0c95
NG
727 pr_warning("Error allocating disk structure for device %d\n",
728 device_id);
de1a21a0
NG
729 ret = -ENOMEM;
730 goto out;
306b0c95
NG
731 }
732
f1e3cfff
NG
733 zram->disk->major = zram_major;
734 zram->disk->first_minor = device_id;
735 zram->disk->fops = &zram_devops;
736 zram->disk->queue = zram->queue;
737 zram->disk->private_data = zram;
738 snprintf(zram->disk->disk_name, 16, "zram%d", device_id);
306b0c95 739
33863c21 740 /* Actual capacity set using syfs (/sys/block/zram<id>/disksize */
f1e3cfff 741 set_capacity(zram->disk, 0);
5d83d5a0 742
a1dd52af
NG
743 /*
744 * To ensure that we always get PAGE_SIZE aligned
745 * and n*PAGE_SIZED sized I/O requests.
746 */
f1e3cfff 747 blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE);
7b19b8d4
RJ
748 blk_queue_logical_block_size(zram->disk->queue,
749 ZRAM_LOGICAL_BLOCK_SIZE);
f1e3cfff
NG
750 blk_queue_io_min(zram->disk->queue, PAGE_SIZE);
751 blk_queue_io_opt(zram->disk->queue, PAGE_SIZE);
5d83d5a0 752
f1e3cfff 753 add_disk(zram->disk);
306b0c95 754
33863c21
NG
755 ret = sysfs_create_group(&disk_to_dev(zram->disk)->kobj,
756 &zram_disk_attr_group);
757 if (ret < 0) {
758 pr_warning("Error creating sysfs group");
759 goto out;
760 }
33863c21 761
f1e3cfff 762 zram->init_done = 0;
de1a21a0
NG
763
764out:
765 return ret;
306b0c95
NG
766}
767
f1e3cfff 768static void destroy_device(struct zram *zram)
306b0c95 769{
33863c21
NG
770 sysfs_remove_group(&disk_to_dev(zram->disk)->kobj,
771 &zram_disk_attr_group);
33863c21 772
f1e3cfff
NG
773 if (zram->disk) {
774 del_gendisk(zram->disk);
775 put_disk(zram->disk);
306b0c95
NG
776 }
777
f1e3cfff
NG
778 if (zram->queue)
779 blk_cleanup_queue(zram->queue);
306b0c95
NG
780}
781
f1e3cfff 782static int __init zram_init(void)
306b0c95 783{
de1a21a0 784 int ret, dev_id;
306b0c95 785
efd54f43 786 if (zram_num_devices > max_num_devices) {
306b0c95 787 pr_warning("Invalid value for num_devices: %u\n",
efd54f43 788 zram_num_devices);
de1a21a0
NG
789 ret = -EINVAL;
790 goto out;
306b0c95
NG
791 }
792
f1e3cfff
NG
793 zram_major = register_blkdev(0, "zram");
794 if (zram_major <= 0) {
306b0c95 795 pr_warning("Unable to get major number\n");
de1a21a0
NG
796 ret = -EBUSY;
797 goto out;
306b0c95
NG
798 }
799
efd54f43 800 if (!zram_num_devices) {
306b0c95 801 pr_info("num_devices not specified. Using default: 1\n");
efd54f43 802 zram_num_devices = 1;
306b0c95
NG
803 }
804
805 /* Allocate the device array and initialize each one */
efd54f43
NW
806 pr_info("Creating %u devices ...\n", zram_num_devices);
807 zram_devices = kzalloc(zram_num_devices * sizeof(struct zram), GFP_KERNEL);
43801f6e 808 if (!zram_devices) {
de1a21a0
NG
809 ret = -ENOMEM;
810 goto unregister;
811 }
306b0c95 812
efd54f43 813 for (dev_id = 0; dev_id < zram_num_devices; dev_id++) {
43801f6e 814 ret = create_device(&zram_devices[dev_id], dev_id);
de1a21a0 815 if (ret)
3bf040c7 816 goto free_devices;
de1a21a0
NG
817 }
818
306b0c95 819 return 0;
de1a21a0 820
3bf040c7 821free_devices:
de1a21a0 822 while (dev_id)
43801f6e
NW
823 destroy_device(&zram_devices[--dev_id]);
824 kfree(zram_devices);
de1a21a0 825unregister:
f1e3cfff 826 unregister_blkdev(zram_major, "zram");
de1a21a0 827out:
306b0c95
NG
828 return ret;
829}
830
f1e3cfff 831static void __exit zram_exit(void)
306b0c95
NG
832{
833 int i;
f1e3cfff 834 struct zram *zram;
306b0c95 835
efd54f43 836 for (i = 0; i < zram_num_devices; i++) {
43801f6e 837 zram = &zram_devices[i];
306b0c95 838
f1e3cfff
NG
839 destroy_device(zram);
840 if (zram->init_done)
33863c21 841 zram_reset_device(zram);
306b0c95
NG
842 }
843
f1e3cfff 844 unregister_blkdev(zram_major, "zram");
306b0c95 845
43801f6e 846 kfree(zram_devices);
306b0c95
NG
847 pr_debug("Cleanup done!\n");
848}
849
efd54f43
NW
850module_param(zram_num_devices, uint, 0);
851MODULE_PARM_DESC(zram_num_devices, "Number of zram devices");
306b0c95 852
f1e3cfff
NG
853module_init(zram_init);
854module_exit(zram_exit);
306b0c95
NG
855
856MODULE_LICENSE("Dual BSD/GPL");
857MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
f1e3cfff 858MODULE_DESCRIPTION("Compressed RAM Block Device");