Staging: bcm: Remove typedef stLocalSFAddRequestAlt.
[deliverable/linux.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) */
5fa5a901 43static unsigned int 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{
c2344348 138 unsigned long handle = zram->table[index].handle;
130f315a 139 u16 size = zram->table[index].size;
306b0c95 140
fd1a30de 141 if (unlikely(!handle)) {
2e882281
NG
142 /*
143 * No memory is allocated for zero filled pages.
144 * Simply clear zero page flag.
145 */
f1e3cfff
NG
146 if (zram_test_flag(zram, index, ZRAM_ZERO)) {
147 zram_clear_flag(zram, index, ZRAM_ZERO);
148 zram_stat_dec(&zram->stats.pages_zero);
306b0c95
NG
149 }
150 return;
151 }
152
130f315a
MK
153 if (unlikely(size > max_zpage_size))
154 zram_stat_dec(&zram->stats.bad_compress);
306b0c95 155
fd1a30de 156 zs_free(zram->mem_pool, handle);
306b0c95 157
130f315a 158 if (size <= PAGE_SIZE / 2)
f1e3cfff 159 zram_stat_dec(&zram->stats.good_compress);
306b0c95 160
fd1a30de
NG
161 zram_stat64_sub(zram, &zram->stats.compr_size,
162 zram->table[index].size);
f1e3cfff 163 zram_stat_dec(&zram->stats.pages_stored);
306b0c95 164
c2344348 165 zram->table[index].handle = 0;
fd1a30de 166 zram->table[index].size = 0;
306b0c95
NG
167}
168
924bd88d 169static void handle_zero_page(struct bio_vec *bvec)
306b0c95 170{
924bd88d 171 struct page *page = bvec->bv_page;
306b0c95 172 void *user_mem;
306b0c95 173
ba82fe2e 174 user_mem = kmap_atomic(page);
924bd88d 175 memset(user_mem + bvec->bv_offset, 0, bvec->bv_len);
ba82fe2e 176 kunmap_atomic(user_mem);
306b0c95 177
30fb8a71 178 flush_dcache_page(page);
306b0c95
NG
179}
180
924bd88d
JM
181static inline int is_partial_io(struct bio_vec *bvec)
182{
183 return bvec->bv_len != PAGE_SIZE;
184}
185
8c921b2b 186static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
924bd88d 187 u32 index, int offset, struct bio *bio)
306b0c95 188{
8c921b2b
JM
189 int ret;
190 size_t clen;
191 struct page *page;
924bd88d 192 unsigned char *user_mem, *cmem, *uncmem = NULL;
a1dd52af 193
8c921b2b 194 page = bvec->bv_page;
306b0c95 195
8c921b2b 196 if (zram_test_flag(zram, index, ZRAM_ZERO)) {
924bd88d 197 handle_zero_page(bvec);
8c921b2b
JM
198 return 0;
199 }
306b0c95 200
8c921b2b 201 /* Requested page is not present in compressed area */
fd1a30de 202 if (unlikely(!zram->table[index].handle)) {
8c921b2b
JM
203 pr_debug("Read before write: sector=%lu, size=%u",
204 (ulong)(bio->bi_sector), bio->bi_size);
924bd88d 205 handle_zero_page(bvec);
8c921b2b
JM
206 return 0;
207 }
306b0c95 208
924bd88d
JM
209 if (is_partial_io(bvec)) {
210 /* Use a temporary buffer to decompress the page */
211 uncmem = kmalloc(PAGE_SIZE, GFP_KERNEL);
212 if (!uncmem) {
213 pr_info("Error allocating temp memory!\n");
214 return -ENOMEM;
215 }
216 }
217
ba82fe2e 218 user_mem = kmap_atomic(page);
924bd88d
JM
219 if (!is_partial_io(bvec))
220 uncmem = user_mem;
8c921b2b 221 clen = PAGE_SIZE;
306b0c95 222
b7418510
SJ
223 cmem = zs_map_object(zram->mem_pool, zram->table[index].handle,
224 ZS_MM_RO);
306b0c95 225
c8f2f0db
NG
226 if (zram->table[index].size == PAGE_SIZE) {
227 memcpy(uncmem, cmem, PAGE_SIZE);
228 ret = LZO_E_OK;
229 } else {
230 ret = lzo1x_decompress_safe(cmem, zram->table[index].size,
924bd88d 231 uncmem, &clen);
c8f2f0db 232 }
924bd88d
JM
233
234 if (is_partial_io(bvec)) {
235 memcpy(user_mem + bvec->bv_offset, uncmem + offset,
236 bvec->bv_len);
237 kfree(uncmem);
238 }
306b0c95 239
fd1a30de 240 zs_unmap_object(zram->mem_pool, zram->table[index].handle);
ba82fe2e 241 kunmap_atomic(user_mem);
a1dd52af 242
8c921b2b
JM
243 /* Should NEVER happen. Return bio error if it does. */
244 if (unlikely(ret != LZO_E_OK)) {
245 pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
246 zram_stat64_inc(zram, &zram->stats.failed_reads);
247 return ret;
a1dd52af 248 }
306b0c95 249
8c921b2b 250 flush_dcache_page(page);
306b0c95 251
8c921b2b 252 return 0;
306b0c95
NG
253}
254
924bd88d
JM
255static int zram_read_before_write(struct zram *zram, char *mem, u32 index)
256{
257 int ret;
258 size_t clen = PAGE_SIZE;
924bd88d 259 unsigned char *cmem;
374a6919 260 unsigned long handle = zram->table[index].handle;
924bd88d 261
374a6919 262 if (zram_test_flag(zram, index, ZRAM_ZERO) || !handle) {
924bd88d
JM
263 memset(mem, 0, PAGE_SIZE);
264 return 0;
265 }
266
b7418510 267 cmem = zs_map_object(zram->mem_pool, handle, ZS_MM_RO);
130f315a 268 ret = lzo1x_decompress_safe(cmem, zram->table[index].size,
924bd88d 269 mem, &clen);
374a6919 270 zs_unmap_object(zram->mem_pool, handle);
924bd88d
JM
271
272 /* Should NEVER happen. Return bio error if it does. */
273 if (unlikely(ret != LZO_E_OK)) {
274 pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
275 zram_stat64_inc(zram, &zram->stats.failed_reads);
276 return ret;
277 }
278
279 return 0;
280}
281
282static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
283 int offset)
306b0c95 284{
8c921b2b 285 int ret;
8c921b2b 286 size_t clen;
c2344348 287 unsigned long handle;
130f315a 288 struct page *page;
924bd88d 289 unsigned char *user_mem, *cmem, *src, *uncmem = NULL;
306b0c95 290
8c921b2b
JM
291 page = bvec->bv_page;
292 src = zram->compress_buffer;
306b0c95 293
924bd88d
JM
294 if (is_partial_io(bvec)) {
295 /*
296 * This is a partial IO. We need to read the full page
297 * before to write the changes.
298 */
299 uncmem = kmalloc(PAGE_SIZE, GFP_KERNEL);
300 if (!uncmem) {
301 pr_info("Error allocating temp memory!\n");
302 ret = -ENOMEM;
303 goto out;
304 }
305 ret = zram_read_before_write(zram, uncmem, index);
306 if (ret) {
307 kfree(uncmem);
308 goto out;
309 }
310 }
311
8c921b2b
JM
312 /*
313 * System overwrites unused sectors. Free memory associated
314 * with this sector now.
315 */
fd1a30de 316 if (zram->table[index].handle ||
8c921b2b
JM
317 zram_test_flag(zram, index, ZRAM_ZERO))
318 zram_free_page(zram, index);
306b0c95 319
ba82fe2e 320 user_mem = kmap_atomic(page);
924bd88d
JM
321
322 if (is_partial_io(bvec))
323 memcpy(uncmem + offset, user_mem + bvec->bv_offset,
324 bvec->bv_len);
325 else
326 uncmem = user_mem;
327
328 if (page_zero_filled(uncmem)) {
ba82fe2e 329 kunmap_atomic(user_mem);
924bd88d
JM
330 if (is_partial_io(bvec))
331 kfree(uncmem);
8c921b2b
JM
332 zram_stat_inc(&zram->stats.pages_zero);
333 zram_set_flag(zram, index, ZRAM_ZERO);
924bd88d
JM
334 ret = 0;
335 goto out;
8c921b2b 336 }
306b0c95 337
924bd88d 338 ret = lzo1x_1_compress(uncmem, PAGE_SIZE, src, &clen,
8c921b2b 339 zram->compress_workmem);
306b0c95 340
ba82fe2e 341 kunmap_atomic(user_mem);
924bd88d
JM
342 if (is_partial_io(bvec))
343 kfree(uncmem);
306b0c95 344
8c921b2b 345 if (unlikely(ret != LZO_E_OK)) {
8c921b2b 346 pr_err("Compression failed! err=%d\n", ret);
924bd88d 347 goto out;
8c921b2b 348 }
306b0c95 349
c8f2f0db 350 if (unlikely(clen > max_zpage_size)) {
130f315a 351 zram_stat_inc(&zram->stats.bad_compress);
c8f2f0db
NG
352 src = uncmem;
353 clen = PAGE_SIZE;
354 }
a1dd52af 355
130f315a 356 handle = zs_malloc(zram->mem_pool, clen);
fd1a30de 357 if (!handle) {
8c921b2b
JM
358 pr_info("Error allocating memory for compressed "
359 "page: %u, size=%zu\n", index, clen);
924bd88d
JM
360 ret = -ENOMEM;
361 goto out;
8c921b2b 362 }
b7418510 363 cmem = zs_map_object(zram->mem_pool, handle, ZS_MM_WO);
306b0c95 364
8c921b2b 365 memcpy(cmem, src, clen);
306b0c95 366
130f315a 367 zs_unmap_object(zram->mem_pool, handle);
fd1a30de
NG
368
369 zram->table[index].handle = handle;
370 zram->table[index].size = clen;
306b0c95 371
8c921b2b
JM
372 /* Update stats */
373 zram_stat64_add(zram, &zram->stats.compr_size, clen);
374 zram_stat_inc(&zram->stats.pages_stored);
375 if (clen <= PAGE_SIZE / 2)
376 zram_stat_inc(&zram->stats.good_compress);
306b0c95 377
8c921b2b 378 return 0;
924bd88d
JM
379
380out:
381 if (ret)
382 zram_stat64_inc(zram, &zram->stats.failed_writes);
383 return ret;
8c921b2b
JM
384}
385
386static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
924bd88d 387 int offset, struct bio *bio, int rw)
8c921b2b 388{
c5bde238 389 int ret;
8c921b2b 390
c5bde238
JM
391 if (rw == READ) {
392 down_read(&zram->lock);
393 ret = zram_bvec_read(zram, bvec, index, offset, bio);
394 up_read(&zram->lock);
395 } else {
396 down_write(&zram->lock);
397 ret = zram_bvec_write(zram, bvec, index, offset);
398 up_write(&zram->lock);
399 }
400
401 return ret;
924bd88d
JM
402}
403
404static void update_position(u32 *index, int *offset, struct bio_vec *bvec)
405{
406 if (*offset + bvec->bv_len >= PAGE_SIZE)
407 (*index)++;
408 *offset = (*offset + bvec->bv_len) % PAGE_SIZE;
8c921b2b
JM
409}
410
411static void __zram_make_request(struct zram *zram, struct bio *bio, int rw)
412{
924bd88d 413 int i, offset;
8c921b2b
JM
414 u32 index;
415 struct bio_vec *bvec;
416
417 switch (rw) {
418 case READ:
419 zram_stat64_inc(zram, &zram->stats.num_reads);
420 break;
421 case WRITE:
422 zram_stat64_inc(zram, &zram->stats.num_writes);
423 break;
424 }
425
426 index = bio->bi_sector >> SECTORS_PER_PAGE_SHIFT;
924bd88d 427 offset = (bio->bi_sector & (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT;
8c921b2b
JM
428
429 bio_for_each_segment(bvec, bio, i) {
924bd88d
JM
430 int max_transfer_size = PAGE_SIZE - offset;
431
432 if (bvec->bv_len > max_transfer_size) {
433 /*
434 * zram_bvec_rw() can only make operation on a single
435 * zram page. Split the bio vector.
436 */
437 struct bio_vec bv;
438
439 bv.bv_page = bvec->bv_page;
440 bv.bv_len = max_transfer_size;
441 bv.bv_offset = bvec->bv_offset;
442
443 if (zram_bvec_rw(zram, &bv, index, offset, bio, rw) < 0)
444 goto out;
445
446 bv.bv_len = bvec->bv_len - max_transfer_size;
447 bv.bv_offset += max_transfer_size;
448 if (zram_bvec_rw(zram, &bv, index+1, 0, bio, rw) < 0)
449 goto out;
450 } else
451 if (zram_bvec_rw(zram, bvec, index, offset, bio, rw)
452 < 0)
453 goto out;
454
455 update_position(&index, &offset, bvec);
a1dd52af 456 }
306b0c95
NG
457
458 set_bit(BIO_UPTODATE, &bio->bi_flags);
459 bio_endio(bio, 0);
7d7854b4 460 return;
306b0c95
NG
461
462out:
306b0c95 463 bio_io_error(bio);
306b0c95
NG
464}
465
306b0c95 466/*
924bd88d 467 * Check if request is within bounds and aligned on zram logical blocks.
306b0c95 468 */
f1e3cfff 469static inline int valid_io_request(struct zram *zram, struct bio *bio)
306b0c95
NG
470{
471 if (unlikely(
f1e3cfff 472 (bio->bi_sector >= (zram->disksize >> SECTOR_SHIFT)) ||
924bd88d
JM
473 (bio->bi_sector & (ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1)) ||
474 (bio->bi_size & (ZRAM_LOGICAL_BLOCK_SIZE - 1)))) {
306b0c95
NG
475
476 return 0;
477 }
478
a1dd52af 479 /* I/O request is valid */
306b0c95
NG
480 return 1;
481}
482
483/*
f1e3cfff 484 * Handler function for all zram I/O requests.
306b0c95 485 */
5a7bbad2 486static void zram_make_request(struct request_queue *queue, struct bio *bio)
306b0c95 487{
f1e3cfff 488 struct zram *zram = queue->queuedata;
306b0c95 489
0900beae
JM
490 if (unlikely(!zram->init_done) && zram_init_device(zram))
491 goto error;
492
493 down_read(&zram->init_lock);
494 if (unlikely(!zram->init_done))
495 goto error_unlock;
496
f1e3cfff
NG
497 if (!valid_io_request(zram, bio)) {
498 zram_stat64_inc(zram, &zram->stats.invalid_io);
0900beae 499 goto error_unlock;
6642a67c
JM
500 }
501
8c921b2b 502 __zram_make_request(zram, bio, bio_data_dir(bio));
0900beae 503 up_read(&zram->init_lock);
306b0c95 504
b4fdcb02 505 return;
0900beae
JM
506
507error_unlock:
508 up_read(&zram->init_lock);
509error:
510 bio_io_error(bio);
306b0c95
NG
511}
512
0900beae 513void __zram_reset_device(struct zram *zram)
306b0c95 514{
97a06382 515 size_t index;
306b0c95 516
f1e3cfff 517 zram->init_done = 0;
7eef7533 518
306b0c95 519 /* Free various per-device buffers */
f1e3cfff
NG
520 kfree(zram->compress_workmem);
521 free_pages((unsigned long)zram->compress_buffer, 1);
306b0c95 522
f1e3cfff
NG
523 zram->compress_workmem = NULL;
524 zram->compress_buffer = NULL;
306b0c95 525
f1e3cfff
NG
526 /* Free all pages that are still in this zram device */
527 for (index = 0; index < zram->disksize >> PAGE_SHIFT; index++) {
c2344348 528 unsigned long handle = zram->table[index].handle;
fd1a30de 529 if (!handle)
306b0c95
NG
530 continue;
531
130f315a 532 zs_free(zram->mem_pool, handle);
306b0c95
NG
533 }
534
f1e3cfff
NG
535 vfree(zram->table);
536 zram->table = NULL;
306b0c95 537
fd1a30de 538 zs_destroy_pool(zram->mem_pool);
f1e3cfff 539 zram->mem_pool = NULL;
306b0c95 540
306b0c95 541 /* Reset stats */
f1e3cfff 542 memset(&zram->stats, 0, sizeof(zram->stats));
306b0c95 543
f1e3cfff 544 zram->disksize = 0;
0900beae
JM
545}
546
547void zram_reset_device(struct zram *zram)
548{
549 down_write(&zram->init_lock);
550 __zram_reset_device(zram);
551 up_write(&zram->init_lock);
306b0c95
NG
552}
553
33863c21 554int zram_init_device(struct zram *zram)
306b0c95
NG
555{
556 int ret;
557 size_t num_pages;
306b0c95 558
0900beae 559 down_write(&zram->init_lock);
484875ad 560
f1e3cfff 561 if (zram->init_done) {
0900beae 562 up_write(&zram->init_lock);
484875ad 563 return 0;
306b0c95
NG
564 }
565
f1e3cfff 566 zram_set_disksize(zram, totalram_pages << PAGE_SHIFT);
306b0c95 567
f1e3cfff
NG
568 zram->compress_workmem = kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
569 if (!zram->compress_workmem) {
306b0c95
NG
570 pr_err("Error allocating compressor working memory!\n");
571 ret = -ENOMEM;
5a18c531 572 goto fail_no_table;
306b0c95
NG
573 }
574
fb927284
JM
575 zram->compress_buffer =
576 (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
f1e3cfff 577 if (!zram->compress_buffer) {
306b0c95
NG
578 pr_err("Error allocating compressor buffer space\n");
579 ret = -ENOMEM;
5a18c531 580 goto fail_no_table;
306b0c95
NG
581 }
582
f1e3cfff 583 num_pages = zram->disksize >> PAGE_SHIFT;
5b84cc78 584 zram->table = vzalloc(num_pages * sizeof(*zram->table));
f1e3cfff
NG
585 if (!zram->table) {
586 pr_err("Error allocating zram address table\n");
306b0c95 587 ret = -ENOMEM;
5a18c531 588 goto fail_no_table;
306b0c95 589 }
306b0c95 590
f1e3cfff 591 set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
306b0c95 592
f1e3cfff
NG
593 /* zram devices sort of resembles non-rotational disks */
594 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zram->disk->queue);
306b0c95 595
fd1a30de 596 zram->mem_pool = zs_create_pool("zram", GFP_NOIO | __GFP_HIGHMEM);
f1e3cfff 597 if (!zram->mem_pool) {
306b0c95
NG
598 pr_err("Error creating memory pool\n");
599 ret = -ENOMEM;
600 goto fail;
601 }
602
f1e3cfff 603 zram->init_done = 1;
0900beae 604 up_write(&zram->init_lock);
306b0c95
NG
605
606 pr_debug("Initialization done!\n");
607 return 0;
608
5a18c531
JM
609fail_no_table:
610 /* To prevent accessing table entries during cleanup */
611 zram->disksize = 0;
306b0c95 612fail:
0900beae
JM
613 __zram_reset_device(zram);
614 up_write(&zram->init_lock);
306b0c95
NG
615 pr_err("Initialization failed: err=%d\n", ret);
616 return ret;
617}
618
2ccbec05
NG
619static void zram_slot_free_notify(struct block_device *bdev,
620 unsigned long index)
107c161b 621{
f1e3cfff 622 struct zram *zram;
107c161b 623
f1e3cfff
NG
624 zram = bdev->bd_disk->private_data;
625 zram_free_page(zram, index);
626 zram_stat64_inc(zram, &zram->stats.notify_free);
107c161b
NG
627}
628
f1e3cfff 629static const struct block_device_operations zram_devops = {
f1e3cfff 630 .swap_slot_free_notify = zram_slot_free_notify,
107c161b 631 .owner = THIS_MODULE
306b0c95
NG
632};
633
f1e3cfff 634static int create_device(struct zram *zram, int device_id)
306b0c95 635{
de1a21a0
NG
636 int ret = 0;
637
c5bde238 638 init_rwsem(&zram->lock);
0900beae 639 init_rwsem(&zram->init_lock);
f1e3cfff 640 spin_lock_init(&zram->stat64_lock);
306b0c95 641
f1e3cfff
NG
642 zram->queue = blk_alloc_queue(GFP_KERNEL);
643 if (!zram->queue) {
306b0c95
NG
644 pr_err("Error allocating disk queue for device %d\n",
645 device_id);
de1a21a0
NG
646 ret = -ENOMEM;
647 goto out;
306b0c95
NG
648 }
649
f1e3cfff
NG
650 blk_queue_make_request(zram->queue, zram_make_request);
651 zram->queue->queuedata = zram;
306b0c95
NG
652
653 /* gendisk structure */
f1e3cfff
NG
654 zram->disk = alloc_disk(1);
655 if (!zram->disk) {
656 blk_cleanup_queue(zram->queue);
94b8435f 657 pr_warn("Error allocating disk structure for device %d\n",
306b0c95 658 device_id);
de1a21a0
NG
659 ret = -ENOMEM;
660 goto out;
306b0c95
NG
661 }
662
f1e3cfff
NG
663 zram->disk->major = zram_major;
664 zram->disk->first_minor = device_id;
665 zram->disk->fops = &zram_devops;
666 zram->disk->queue = zram->queue;
667 zram->disk->private_data = zram;
668 snprintf(zram->disk->disk_name, 16, "zram%d", device_id);
306b0c95 669
33863c21 670 /* Actual capacity set using syfs (/sys/block/zram<id>/disksize */
f1e3cfff 671 set_capacity(zram->disk, 0);
5d83d5a0 672
a1dd52af
NG
673 /*
674 * To ensure that we always get PAGE_SIZE aligned
675 * and n*PAGE_SIZED sized I/O requests.
676 */
f1e3cfff 677 blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE);
7b19b8d4
RJ
678 blk_queue_logical_block_size(zram->disk->queue,
679 ZRAM_LOGICAL_BLOCK_SIZE);
f1e3cfff
NG
680 blk_queue_io_min(zram->disk->queue, PAGE_SIZE);
681 blk_queue_io_opt(zram->disk->queue, PAGE_SIZE);
5d83d5a0 682
f1e3cfff 683 add_disk(zram->disk);
306b0c95 684
33863c21
NG
685 ret = sysfs_create_group(&disk_to_dev(zram->disk)->kobj,
686 &zram_disk_attr_group);
687 if (ret < 0) {
94b8435f 688 pr_warn("Error creating sysfs group");
33863c21
NG
689 goto out;
690 }
33863c21 691
f1e3cfff 692 zram->init_done = 0;
de1a21a0
NG
693
694out:
695 return ret;
306b0c95
NG
696}
697
f1e3cfff 698static void destroy_device(struct zram *zram)
306b0c95 699{
33863c21
NG
700 sysfs_remove_group(&disk_to_dev(zram->disk)->kobj,
701 &zram_disk_attr_group);
33863c21 702
f1e3cfff
NG
703 if (zram->disk) {
704 del_gendisk(zram->disk);
705 put_disk(zram->disk);
306b0c95
NG
706 }
707
f1e3cfff
NG
708 if (zram->queue)
709 blk_cleanup_queue(zram->queue);
306b0c95
NG
710}
711
5fa5a901
NG
712unsigned int zram_get_num_devices(void)
713{
714 return num_devices;
715}
716
f1e3cfff 717static int __init zram_init(void)
306b0c95 718{
de1a21a0 719 int ret, dev_id;
306b0c95 720
5fa5a901 721 if (num_devices > max_num_devices) {
94b8435f 722 pr_warn("Invalid value for num_devices: %u\n",
5fa5a901 723 num_devices);
de1a21a0
NG
724 ret = -EINVAL;
725 goto out;
306b0c95
NG
726 }
727
f1e3cfff
NG
728 zram_major = register_blkdev(0, "zram");
729 if (zram_major <= 0) {
94b8435f 730 pr_warn("Unable to get major number\n");
de1a21a0
NG
731 ret = -EBUSY;
732 goto out;
306b0c95
NG
733 }
734
5fa5a901 735 if (!num_devices) {
306b0c95 736 pr_info("num_devices not specified. Using default: 1\n");
5fa5a901 737 num_devices = 1;
306b0c95
NG
738 }
739
740 /* Allocate the device array and initialize each one */
5fa5a901
NG
741 pr_info("Creating %u devices ...\n", num_devices);
742 zram_devices = kzalloc(num_devices * sizeof(struct zram), GFP_KERNEL);
43801f6e 743 if (!zram_devices) {
de1a21a0
NG
744 ret = -ENOMEM;
745 goto unregister;
746 }
306b0c95 747
5fa5a901 748 for (dev_id = 0; dev_id < num_devices; dev_id++) {
43801f6e 749 ret = create_device(&zram_devices[dev_id], dev_id);
de1a21a0 750 if (ret)
3bf040c7 751 goto free_devices;
de1a21a0
NG
752 }
753
306b0c95 754 return 0;
de1a21a0 755
3bf040c7 756free_devices:
de1a21a0 757 while (dev_id)
43801f6e
NW
758 destroy_device(&zram_devices[--dev_id]);
759 kfree(zram_devices);
de1a21a0 760unregister:
f1e3cfff 761 unregister_blkdev(zram_major, "zram");
de1a21a0 762out:
306b0c95
NG
763 return ret;
764}
765
f1e3cfff 766static void __exit zram_exit(void)
306b0c95
NG
767{
768 int i;
f1e3cfff 769 struct zram *zram;
306b0c95 770
5fa5a901 771 for (i = 0; i < num_devices; i++) {
43801f6e 772 zram = &zram_devices[i];
306b0c95 773
f1e3cfff
NG
774 destroy_device(zram);
775 if (zram->init_done)
33863c21 776 zram_reset_device(zram);
306b0c95
NG
777 }
778
f1e3cfff 779 unregister_blkdev(zram_major, "zram");
306b0c95 780
43801f6e 781 kfree(zram_devices);
306b0c95
NG
782 pr_debug("Cleanup done!\n");
783}
784
5fa5a901
NG
785module_param(num_devices, uint, 0);
786MODULE_PARM_DESC(num_devices, "Number of zram devices");
306b0c95 787
f1e3cfff
NG
788module_init(zram_init);
789module_exit(zram_exit);
306b0c95
NG
790
791MODULE_LICENSE("Dual BSD/GPL");
792MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
f1e3cfff 793MODULE_DESCRIPTION("Compressed RAM Block Device");
This page took 0.345621 seconds and 5 git commands to generate.