]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/block/zram/zram_drv.c
zram: drop `init_done' struct zram member
[mirror_ubuntu-artful-kernel.git] / drivers / block / zram / zram_drv.c
CommitLineData
306b0c95 1/*
f1e3cfff 2 * Compressed RAM block device
306b0c95 3 *
1130ebba 4 * Copyright (C) 2008, 2009, 2010 Nitin Gupta
7bfb3de8 5 * 2012, 2013 Minchan Kim
306b0c95
NG
6 *
7 * This code is released using a dual license strategy: BSD/GPL
8 * You can choose the licence that better fits your requirements.
9 *
10 * Released under the terms of 3-clause BSD License
11 * Released under the terms of GNU General Public License Version 2.0
12 *
306b0c95
NG
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;
0f0e3ba3 40static struct zram *zram_devices;
306b0c95 41
306b0c95 42/* Module params (documentation at end) */
ca3d70bd 43static unsigned int num_devices = 1;
33863c21 44
be2d1d56
SS
45static inline int init_done(struct zram *zram)
46{
47 return zram->meta != NULL;
48}
49
9b3bb7ab
SS
50static inline struct zram *dev_to_zram(struct device *dev)
51{
52 return (struct zram *)dev_to_disk(dev)->private_data;
53}
54
55static ssize_t disksize_show(struct device *dev,
56 struct device_attribute *attr, char *buf)
57{
58 struct zram *zram = dev_to_zram(dev);
59
60 return sprintf(buf, "%llu\n", zram->disksize);
61}
62
63static ssize_t initstate_show(struct device *dev,
64 struct device_attribute *attr, char *buf)
65{
66 struct zram *zram = dev_to_zram(dev);
67
be2d1d56 68 return sprintf(buf, "%u\n", init_done(zram));
9b3bb7ab
SS
69}
70
71static ssize_t num_reads_show(struct device *dev,
72 struct device_attribute *attr, char *buf)
73{
74 struct zram *zram = dev_to_zram(dev);
75
76 return sprintf(buf, "%llu\n",
77 (u64)atomic64_read(&zram->stats.num_reads));
78}
79
80static ssize_t num_writes_show(struct device *dev,
81 struct device_attribute *attr, char *buf)
82{
83 struct zram *zram = dev_to_zram(dev);
84
85 return sprintf(buf, "%llu\n",
86 (u64)atomic64_read(&zram->stats.num_writes));
87}
88
89static ssize_t invalid_io_show(struct device *dev,
90 struct device_attribute *attr, char *buf)
91{
92 struct zram *zram = dev_to_zram(dev);
93
94 return sprintf(buf, "%llu\n",
95 (u64)atomic64_read(&zram->stats.invalid_io));
96}
97
98static ssize_t notify_free_show(struct device *dev,
99 struct device_attribute *attr, char *buf)
100{
101 struct zram *zram = dev_to_zram(dev);
102
103 return sprintf(buf, "%llu\n",
104 (u64)atomic64_read(&zram->stats.notify_free));
105}
106
107static ssize_t zero_pages_show(struct device *dev,
108 struct device_attribute *attr, char *buf)
109{
110 struct zram *zram = dev_to_zram(dev);
111
deb0bdeb 112 return sprintf(buf, "%u\n", atomic_read(&zram->stats.pages_zero));
9b3bb7ab
SS
113}
114
115static ssize_t orig_data_size_show(struct device *dev,
116 struct device_attribute *attr, char *buf)
117{
118 struct zram *zram = dev_to_zram(dev);
119
120 return sprintf(buf, "%llu\n",
deb0bdeb 121 (u64)(atomic_read(&zram->stats.pages_stored)) << PAGE_SHIFT);
9b3bb7ab
SS
122}
123
124static ssize_t compr_data_size_show(struct device *dev,
125 struct device_attribute *attr, char *buf)
126{
127 struct zram *zram = dev_to_zram(dev);
128
129 return sprintf(buf, "%llu\n",
130 (u64)atomic64_read(&zram->stats.compr_size));
131}
132
133static ssize_t mem_used_total_show(struct device *dev,
134 struct device_attribute *attr, char *buf)
135{
136 u64 val = 0;
137 struct zram *zram = dev_to_zram(dev);
138 struct zram_meta *meta = zram->meta;
139
140 down_read(&zram->init_lock);
be2d1d56 141 if (init_done(zram))
9b3bb7ab
SS
142 val = zs_get_total_size_bytes(meta->mem_pool);
143 up_read(&zram->init_lock);
144
145 return sprintf(buf, "%llu\n", val);
146}
147
92967471 148/* flag operations needs meta->tb_lock */
8b3cc3ed 149static int zram_test_flag(struct zram_meta *meta, u32 index,
f1e3cfff 150 enum zram_pageflags flag)
306b0c95 151{
8b3cc3ed 152 return meta->table[index].flags & BIT(flag);
306b0c95
NG
153}
154
8b3cc3ed 155static void zram_set_flag(struct zram_meta *meta, u32 index,
f1e3cfff 156 enum zram_pageflags flag)
306b0c95 157{
8b3cc3ed 158 meta->table[index].flags |= BIT(flag);
306b0c95
NG
159}
160
8b3cc3ed 161static void zram_clear_flag(struct zram_meta *meta, u32 index,
f1e3cfff 162 enum zram_pageflags flag)
306b0c95 163{
8b3cc3ed 164 meta->table[index].flags &= ~BIT(flag);
306b0c95
NG
165}
166
9b3bb7ab
SS
167static inline int is_partial_io(struct bio_vec *bvec)
168{
169 return bvec->bv_len != PAGE_SIZE;
170}
171
172/*
173 * Check if request is within bounds and aligned on zram logical blocks.
174 */
175static inline int valid_io_request(struct zram *zram, struct bio *bio)
176{
177 u64 start, end, bound;
a539c72a 178
9b3bb7ab 179 /* unaligned request */
4f024f37
KO
180 if (unlikely(bio->bi_iter.bi_sector &
181 (ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1)))
9b3bb7ab 182 return 0;
4f024f37 183 if (unlikely(bio->bi_iter.bi_size & (ZRAM_LOGICAL_BLOCK_SIZE - 1)))
9b3bb7ab
SS
184 return 0;
185
4f024f37
KO
186 start = bio->bi_iter.bi_sector;
187 end = start + (bio->bi_iter.bi_size >> SECTOR_SHIFT);
9b3bb7ab
SS
188 bound = zram->disksize >> SECTOR_SHIFT;
189 /* out of range range */
75c7caf5 190 if (unlikely(start >= bound || end > bound || start > end))
9b3bb7ab
SS
191 return 0;
192
193 /* I/O request is valid */
194 return 1;
195}
196
197static void zram_meta_free(struct zram_meta *meta)
198{
199 zs_destroy_pool(meta->mem_pool);
200 kfree(meta->compress_workmem);
201 free_pages((unsigned long)meta->compress_buffer, 1);
202 vfree(meta->table);
203 kfree(meta);
204}
205
206static struct zram_meta *zram_meta_alloc(u64 disksize)
207{
208 size_t num_pages;
209 struct zram_meta *meta = kmalloc(sizeof(*meta), GFP_KERNEL);
210 if (!meta)
211 goto out;
212
213 meta->compress_workmem = kzalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
214 if (!meta->compress_workmem)
215 goto free_meta;
216
217 meta->compress_buffer =
218 (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
219 if (!meta->compress_buffer) {
220 pr_err("Error allocating compressor buffer space\n");
221 goto free_workmem;
222 }
223
224 num_pages = disksize >> PAGE_SHIFT;
225 meta->table = vzalloc(num_pages * sizeof(*meta->table));
226 if (!meta->table) {
227 pr_err("Error allocating zram address table\n");
228 goto free_buffer;
229 }
230
231 meta->mem_pool = zs_create_pool(GFP_NOIO | __GFP_HIGHMEM);
232 if (!meta->mem_pool) {
233 pr_err("Error creating memory pool\n");
234 goto free_table;
235 }
236
92967471 237 rwlock_init(&meta->tb_lock);
e46e3315 238 mutex_init(&meta->buffer_lock);
9b3bb7ab
SS
239 return meta;
240
241free_table:
242 vfree(meta->table);
243free_buffer:
244 free_pages((unsigned long)meta->compress_buffer, 1);
245free_workmem:
246 kfree(meta->compress_workmem);
247free_meta:
248 kfree(meta);
249 meta = NULL;
250out:
251 return meta;
252}
253
254static void update_position(u32 *index, int *offset, struct bio_vec *bvec)
255{
256 if (*offset + bvec->bv_len >= PAGE_SIZE)
257 (*index)++;
258 *offset = (*offset + bvec->bv_len) % PAGE_SIZE;
259}
260
306b0c95
NG
261static int page_zero_filled(void *ptr)
262{
263 unsigned int pos;
264 unsigned long *page;
265
266 page = (unsigned long *)ptr;
267
268 for (pos = 0; pos != PAGE_SIZE / sizeof(*page); pos++) {
269 if (page[pos])
270 return 0;
271 }
272
273 return 1;
274}
275
9b3bb7ab
SS
276static void handle_zero_page(struct bio_vec *bvec)
277{
278 struct page *page = bvec->bv_page;
279 void *user_mem;
280
281 user_mem = kmap_atomic(page);
282 if (is_partial_io(bvec))
283 memset(user_mem + bvec->bv_offset, 0, bvec->bv_len);
284 else
285 clear_page(user_mem);
286 kunmap_atomic(user_mem);
287
288 flush_dcache_page(page);
289}
290
92967471 291/* NOTE: caller should hold meta->tb_lock with write-side */
f1e3cfff 292static void zram_free_page(struct zram *zram, size_t index)
306b0c95 293{
8b3cc3ed
MK
294 struct zram_meta *meta = zram->meta;
295 unsigned long handle = meta->table[index].handle;
296 u16 size = meta->table[index].size;
306b0c95 297
fd1a30de 298 if (unlikely(!handle)) {
2e882281
NG
299 /*
300 * No memory is allocated for zero filled pages.
301 * Simply clear zero page flag.
302 */
8b3cc3ed
MK
303 if (zram_test_flag(meta, index, ZRAM_ZERO)) {
304 zram_clear_flag(meta, index, ZRAM_ZERO);
deb0bdeb 305 atomic_dec(&zram->stats.pages_zero);
306b0c95
NG
306 }
307 return;
308 }
309
130f315a 310 if (unlikely(size > max_zpage_size))
deb0bdeb 311 atomic_dec(&zram->stats.bad_compress);
306b0c95 312
8b3cc3ed 313 zs_free(meta->mem_pool, handle);
306b0c95 314
130f315a 315 if (size <= PAGE_SIZE / 2)
deb0bdeb 316 atomic_dec(&zram->stats.good_compress);
306b0c95 317
da5cc7d3 318 atomic64_sub(meta->table[index].size, &zram->stats.compr_size);
deb0bdeb 319 atomic_dec(&zram->stats.pages_stored);
306b0c95 320
8b3cc3ed
MK
321 meta->table[index].handle = 0;
322 meta->table[index].size = 0;
306b0c95
NG
323}
324
37b51fdd 325static int zram_decompress_page(struct zram *zram, char *mem, u32 index)
306b0c95 326{
37b51fdd
SS
327 int ret = LZO_E_OK;
328 size_t clen = PAGE_SIZE;
329 unsigned char *cmem;
8b3cc3ed 330 struct zram_meta *meta = zram->meta;
92967471
MK
331 unsigned long handle;
332 u16 size;
333
334 read_lock(&meta->tb_lock);
335 handle = meta->table[index].handle;
336 size = meta->table[index].size;
306b0c95 337
8b3cc3ed 338 if (!handle || zram_test_flag(meta, index, ZRAM_ZERO)) {
92967471 339 read_unlock(&meta->tb_lock);
42e99bd9 340 clear_page(mem);
8c921b2b
JM
341 return 0;
342 }
306b0c95 343
8b3cc3ed 344 cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_RO);
92967471 345 if (size == PAGE_SIZE)
42e99bd9 346 copy_page(mem, cmem);
37b51fdd 347 else
92967471 348 ret = lzo1x_decompress_safe(cmem, size, mem, &clen);
8b3cc3ed 349 zs_unmap_object(meta->mem_pool, handle);
92967471 350 read_unlock(&meta->tb_lock);
a1dd52af 351
8c921b2b
JM
352 /* Should NEVER happen. Return bio error if it does. */
353 if (unlikely(ret != LZO_E_OK)) {
354 pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
da5cc7d3 355 atomic64_inc(&zram->stats.failed_reads);
8c921b2b 356 return ret;
a1dd52af 357 }
306b0c95 358
8c921b2b 359 return 0;
306b0c95
NG
360}
361
37b51fdd
SS
362static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
363 u32 index, int offset, struct bio *bio)
924bd88d
JM
364{
365 int ret;
37b51fdd
SS
366 struct page *page;
367 unsigned char *user_mem, *uncmem = NULL;
8b3cc3ed 368 struct zram_meta *meta = zram->meta;
37b51fdd
SS
369 page = bvec->bv_page;
370
92967471 371 read_lock(&meta->tb_lock);
8b3cc3ed
MK
372 if (unlikely(!meta->table[index].handle) ||
373 zram_test_flag(meta, index, ZRAM_ZERO)) {
92967471 374 read_unlock(&meta->tb_lock);
37b51fdd 375 handle_zero_page(bvec);
924bd88d
JM
376 return 0;
377 }
92967471 378 read_unlock(&meta->tb_lock);
924bd88d 379
37b51fdd
SS
380 if (is_partial_io(bvec))
381 /* Use a temporary buffer to decompress the page */
7e5a5104
MK
382 uncmem = kmalloc(PAGE_SIZE, GFP_NOIO);
383
384 user_mem = kmap_atomic(page);
385 if (!is_partial_io(bvec))
37b51fdd
SS
386 uncmem = user_mem;
387
388 if (!uncmem) {
389 pr_info("Unable to allocate temp memory\n");
390 ret = -ENOMEM;
391 goto out_cleanup;
392 }
924bd88d 393
37b51fdd 394 ret = zram_decompress_page(zram, uncmem, index);
924bd88d 395 /* Should NEVER happen. Return bio error if it does. */
25eeb667 396 if (unlikely(ret != LZO_E_OK))
37b51fdd 397 goto out_cleanup;
924bd88d 398
37b51fdd
SS
399 if (is_partial_io(bvec))
400 memcpy(user_mem + bvec->bv_offset, uncmem + offset,
401 bvec->bv_len);
402
403 flush_dcache_page(page);
404 ret = 0;
405out_cleanup:
406 kunmap_atomic(user_mem);
407 if (is_partial_io(bvec))
408 kfree(uncmem);
409 return ret;
924bd88d
JM
410}
411
412static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
413 int offset)
306b0c95 414{
397c6066 415 int ret = 0;
8c921b2b 416 size_t clen;
c2344348 417 unsigned long handle;
130f315a 418 struct page *page;
924bd88d 419 unsigned char *user_mem, *cmem, *src, *uncmem = NULL;
8b3cc3ed 420 struct zram_meta *meta = zram->meta;
e46e3315 421 bool locked = false;
306b0c95 422
8c921b2b 423 page = bvec->bv_page;
8b3cc3ed 424 src = meta->compress_buffer;
306b0c95 425
924bd88d
JM
426 if (is_partial_io(bvec)) {
427 /*
428 * This is a partial IO. We need to read the full page
429 * before to write the changes.
430 */
7e5a5104 431 uncmem = kmalloc(PAGE_SIZE, GFP_NOIO);
924bd88d 432 if (!uncmem) {
924bd88d
JM
433 ret = -ENOMEM;
434 goto out;
435 }
37b51fdd 436 ret = zram_decompress_page(zram, uncmem, index);
397c6066 437 if (ret)
924bd88d 438 goto out;
924bd88d
JM
439 }
440
e46e3315
MK
441 mutex_lock(&meta->buffer_lock);
442 locked = true;
ba82fe2e 443 user_mem = kmap_atomic(page);
924bd88d 444
397c6066 445 if (is_partial_io(bvec)) {
924bd88d
JM
446 memcpy(uncmem + offset, user_mem + bvec->bv_offset,
447 bvec->bv_len);
397c6066
NG
448 kunmap_atomic(user_mem);
449 user_mem = NULL;
450 } else {
924bd88d 451 uncmem = user_mem;
397c6066 452 }
924bd88d
JM
453
454 if (page_zero_filled(uncmem)) {
ba82fe2e 455 kunmap_atomic(user_mem);
f40ac2ae 456 /* Free memory associated with this sector now. */
92967471 457 write_lock(&zram->meta->tb_lock);
f40ac2ae 458 zram_free_page(zram, index);
92967471
MK
459 zram_set_flag(meta, index, ZRAM_ZERO);
460 write_unlock(&zram->meta->tb_lock);
f40ac2ae 461
deb0bdeb 462 atomic_inc(&zram->stats.pages_zero);
924bd88d
JM
463 ret = 0;
464 goto out;
8c921b2b 465 }
306b0c95 466
924bd88d 467 ret = lzo1x_1_compress(uncmem, PAGE_SIZE, src, &clen,
8b3cc3ed 468 meta->compress_workmem);
397c6066
NG
469 if (!is_partial_io(bvec)) {
470 kunmap_atomic(user_mem);
471 user_mem = NULL;
472 uncmem = NULL;
473 }
306b0c95 474
8c921b2b 475 if (unlikely(ret != LZO_E_OK)) {
8c921b2b 476 pr_err("Compression failed! err=%d\n", ret);
924bd88d 477 goto out;
8c921b2b 478 }
306b0c95 479
c8f2f0db 480 if (unlikely(clen > max_zpage_size)) {
deb0bdeb 481 atomic_inc(&zram->stats.bad_compress);
c8f2f0db 482 clen = PAGE_SIZE;
397c6066
NG
483 src = NULL;
484 if (is_partial_io(bvec))
485 src = uncmem;
c8f2f0db 486 }
a1dd52af 487
8b3cc3ed 488 handle = zs_malloc(meta->mem_pool, clen);
fd1a30de 489 if (!handle) {
596b3dd4
MR
490 pr_info("Error allocating memory for compressed page: %u, size=%zu\n",
491 index, clen);
924bd88d
JM
492 ret = -ENOMEM;
493 goto out;
8c921b2b 494 }
8b3cc3ed 495 cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_WO);
306b0c95 496
42e99bd9 497 if ((clen == PAGE_SIZE) && !is_partial_io(bvec)) {
397c6066 498 src = kmap_atomic(page);
42e99bd9 499 copy_page(cmem, src);
397c6066 500 kunmap_atomic(src);
42e99bd9
JL
501 } else {
502 memcpy(cmem, src, clen);
503 }
306b0c95 504
8b3cc3ed 505 zs_unmap_object(meta->mem_pool, handle);
fd1a30de 506
f40ac2ae
SS
507 /*
508 * Free memory associated with this sector
509 * before overwriting unused sectors.
510 */
92967471 511 write_lock(&zram->meta->tb_lock);
f40ac2ae
SS
512 zram_free_page(zram, index);
513
8b3cc3ed
MK
514 meta->table[index].handle = handle;
515 meta->table[index].size = clen;
92967471 516 write_unlock(&zram->meta->tb_lock);
306b0c95 517
8c921b2b 518 /* Update stats */
da5cc7d3 519 atomic64_add(clen, &zram->stats.compr_size);
deb0bdeb 520 atomic_inc(&zram->stats.pages_stored);
8c921b2b 521 if (clen <= PAGE_SIZE / 2)
deb0bdeb 522 atomic_inc(&zram->stats.good_compress);
306b0c95 523
924bd88d 524out:
e46e3315
MK
525 if (locked)
526 mutex_unlock(&meta->buffer_lock);
397c6066
NG
527 if (is_partial_io(bvec))
528 kfree(uncmem);
529
924bd88d 530 if (ret)
da5cc7d3 531 atomic64_inc(&zram->stats.failed_writes);
924bd88d 532 return ret;
8c921b2b
JM
533}
534
535static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
924bd88d 536 int offset, struct bio *bio, int rw)
8c921b2b 537{
c5bde238 538 int ret;
8c921b2b 539
e46e3315 540 if (rw == READ)
c5bde238 541 ret = zram_bvec_read(zram, bvec, index, offset, bio);
e46e3315 542 else
c5bde238 543 ret = zram_bvec_write(zram, bvec, index, offset);
c5bde238
JM
544
545 return ret;
924bd88d
JM
546}
547
2b86ab9c 548static void zram_reset_device(struct zram *zram, bool reset_capacity)
924bd88d 549{
9b3bb7ab
SS
550 size_t index;
551 struct zram_meta *meta;
552
644d4787 553 down_write(&zram->init_lock);
be2d1d56 554 if (!init_done(zram)) {
644d4787 555 up_write(&zram->init_lock);
9b3bb7ab 556 return;
644d4787 557 }
9b3bb7ab
SS
558
559 meta = zram->meta;
9b3bb7ab
SS
560 /* Free all pages that are still in this zram device */
561 for (index = 0; index < zram->disksize >> PAGE_SHIFT; index++) {
562 unsigned long handle = meta->table[index].handle;
563 if (!handle)
564 continue;
565
566 zs_free(meta->mem_pool, handle);
567 }
568
569 zram_meta_free(zram->meta);
570 zram->meta = NULL;
571 /* Reset stats */
572 memset(&zram->stats, 0, sizeof(zram->stats));
573
574 zram->disksize = 0;
2b86ab9c
MK
575 if (reset_capacity)
576 set_capacity(zram->disk, 0);
644d4787 577 up_write(&zram->init_lock);
9b3bb7ab
SS
578}
579
580static void zram_init_device(struct zram *zram, struct zram_meta *meta)
581{
582 if (zram->disksize > 2 * (totalram_pages << PAGE_SHIFT)) {
583 pr_info(
584 "There is little point creating a zram of greater than "
585 "twice the size of memory since we expect a 2:1 compression "
586 "ratio. Note that zram uses about 0.1%% of the size of "
587 "the disk when not in use so a huge zram is "
588 "wasteful.\n"
589 "\tMemory Size: %lu kB\n"
590 "\tSize you selected: %llu kB\n"
591 "Continuing anyway ...\n",
592 (totalram_pages << PAGE_SHIFT) >> 10, zram->disksize >> 10
593 );
594 }
595
596 /* zram devices sort of resembles non-rotational disks */
597 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zram->disk->queue);
598
599 zram->meta = meta;
9b3bb7ab
SS
600 pr_debug("Initialization done!\n");
601}
602
603static ssize_t disksize_store(struct device *dev,
604 struct device_attribute *attr, const char *buf, size_t len)
605{
606 u64 disksize;
607 struct zram_meta *meta;
608 struct zram *zram = dev_to_zram(dev);
609
610 disksize = memparse(buf, NULL);
611 if (!disksize)
612 return -EINVAL;
613
614 disksize = PAGE_ALIGN(disksize);
615 meta = zram_meta_alloc(disksize);
db5d711e
MK
616 if (!meta)
617 return -ENOMEM;
9b3bb7ab 618 down_write(&zram->init_lock);
be2d1d56 619 if (init_done(zram)) {
9b3bb7ab
SS
620 up_write(&zram->init_lock);
621 zram_meta_free(meta);
622 pr_info("Cannot change disksize for initialized device\n");
623 return -EBUSY;
624 }
625
626 zram->disksize = disksize;
627 set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
628 zram_init_device(zram, meta);
629 up_write(&zram->init_lock);
630
631 return len;
632}
633
634static ssize_t reset_store(struct device *dev,
635 struct device_attribute *attr, const char *buf, size_t len)
636{
637 int ret;
638 unsigned short do_reset;
639 struct zram *zram;
640 struct block_device *bdev;
641
642 zram = dev_to_zram(dev);
643 bdev = bdget_disk(zram->disk, 0);
644
46a51c80
RK
645 if (!bdev)
646 return -ENOMEM;
647
9b3bb7ab 648 /* Do not reset an active device! */
1b672224
RK
649 if (bdev->bd_holders) {
650 ret = -EBUSY;
651 goto out;
652 }
9b3bb7ab
SS
653
654 ret = kstrtou16(buf, 10, &do_reset);
655 if (ret)
1b672224 656 goto out;
9b3bb7ab 657
1b672224
RK
658 if (!do_reset) {
659 ret = -EINVAL;
660 goto out;
661 }
9b3bb7ab
SS
662
663 /* Make sure all pending I/O is finished */
46a51c80 664 fsync_bdev(bdev);
1b672224 665 bdput(bdev);
9b3bb7ab 666
2b86ab9c 667 zram_reset_device(zram, true);
9b3bb7ab 668 return len;
1b672224
RK
669
670out:
671 bdput(bdev);
672 return ret;
8c921b2b
JM
673}
674
675static void __zram_make_request(struct zram *zram, struct bio *bio, int rw)
676{
7988613b 677 int offset;
8c921b2b 678 u32 index;
7988613b
KO
679 struct bio_vec bvec;
680 struct bvec_iter iter;
8c921b2b
JM
681
682 switch (rw) {
683 case READ:
da5cc7d3 684 atomic64_inc(&zram->stats.num_reads);
8c921b2b
JM
685 break;
686 case WRITE:
da5cc7d3 687 atomic64_inc(&zram->stats.num_writes);
8c921b2b
JM
688 break;
689 }
690
4f024f37
KO
691 index = bio->bi_iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
692 offset = (bio->bi_iter.bi_sector &
693 (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT;
8c921b2b 694
7988613b 695 bio_for_each_segment(bvec, bio, iter) {
924bd88d
JM
696 int max_transfer_size = PAGE_SIZE - offset;
697
7988613b 698 if (bvec.bv_len > max_transfer_size) {
924bd88d
JM
699 /*
700 * zram_bvec_rw() can only make operation on a single
701 * zram page. Split the bio vector.
702 */
703 struct bio_vec bv;
704
7988613b 705 bv.bv_page = bvec.bv_page;
924bd88d 706 bv.bv_len = max_transfer_size;
7988613b 707 bv.bv_offset = bvec.bv_offset;
924bd88d
JM
708
709 if (zram_bvec_rw(zram, &bv, index, offset, bio, rw) < 0)
710 goto out;
711
7988613b 712 bv.bv_len = bvec.bv_len - max_transfer_size;
924bd88d
JM
713 bv.bv_offset += max_transfer_size;
714 if (zram_bvec_rw(zram, &bv, index+1, 0, bio, rw) < 0)
715 goto out;
716 } else
7988613b 717 if (zram_bvec_rw(zram, &bvec, index, offset, bio, rw)
924bd88d
JM
718 < 0)
719 goto out;
720
7988613b 721 update_position(&index, &offset, &bvec);
a1dd52af 722 }
306b0c95
NG
723
724 set_bit(BIO_UPTODATE, &bio->bi_flags);
725 bio_endio(bio, 0);
7d7854b4 726 return;
306b0c95
NG
727
728out:
306b0c95 729 bio_io_error(bio);
306b0c95
NG
730}
731
306b0c95 732/*
f1e3cfff 733 * Handler function for all zram I/O requests.
306b0c95 734 */
5a7bbad2 735static void zram_make_request(struct request_queue *queue, struct bio *bio)
306b0c95 736{
f1e3cfff 737 struct zram *zram = queue->queuedata;
306b0c95 738
0900beae 739 down_read(&zram->init_lock);
be2d1d56 740 if (unlikely(!init_done(zram)))
3de738cd 741 goto error;
0900beae 742
f1e3cfff 743 if (!valid_io_request(zram, bio)) {
da5cc7d3 744 atomic64_inc(&zram->stats.invalid_io);
3de738cd 745 goto error;
6642a67c
JM
746 }
747
8c921b2b 748 __zram_make_request(zram, bio, bio_data_dir(bio));
0900beae 749 up_read(&zram->init_lock);
306b0c95 750
b4fdcb02 751 return;
0900beae 752
0900beae 753error:
3de738cd 754 up_read(&zram->init_lock);
0900beae 755 bio_io_error(bio);
306b0c95
NG
756}
757
2ccbec05
NG
758static void zram_slot_free_notify(struct block_device *bdev,
759 unsigned long index)
107c161b 760{
f1e3cfff 761 struct zram *zram;
f614a9f4 762 struct zram_meta *meta;
107c161b 763
f1e3cfff 764 zram = bdev->bd_disk->private_data;
f614a9f4 765 meta = zram->meta;
a0c516cb 766
f614a9f4
MK
767 write_lock(&meta->tb_lock);
768 zram_free_page(zram, index);
769 write_unlock(&meta->tb_lock);
770 atomic64_inc(&zram->stats.notify_free);
107c161b
NG
771}
772
f1e3cfff 773static const struct block_device_operations zram_devops = {
f1e3cfff 774 .swap_slot_free_notify = zram_slot_free_notify,
107c161b 775 .owner = THIS_MODULE
306b0c95
NG
776};
777
9b3bb7ab
SS
778static DEVICE_ATTR(disksize, S_IRUGO | S_IWUSR,
779 disksize_show, disksize_store);
780static DEVICE_ATTR(initstate, S_IRUGO, initstate_show, NULL);
781static DEVICE_ATTR(reset, S_IWUSR, NULL, reset_store);
782static DEVICE_ATTR(num_reads, S_IRUGO, num_reads_show, NULL);
783static DEVICE_ATTR(num_writes, S_IRUGO, num_writes_show, NULL);
784static DEVICE_ATTR(invalid_io, S_IRUGO, invalid_io_show, NULL);
785static DEVICE_ATTR(notify_free, S_IRUGO, notify_free_show, NULL);
786static DEVICE_ATTR(zero_pages, S_IRUGO, zero_pages_show, NULL);
787static DEVICE_ATTR(orig_data_size, S_IRUGO, orig_data_size_show, NULL);
788static DEVICE_ATTR(compr_data_size, S_IRUGO, compr_data_size_show, NULL);
789static DEVICE_ATTR(mem_used_total, S_IRUGO, mem_used_total_show, NULL);
790
791static struct attribute *zram_disk_attrs[] = {
792 &dev_attr_disksize.attr,
793 &dev_attr_initstate.attr,
794 &dev_attr_reset.attr,
795 &dev_attr_num_reads.attr,
796 &dev_attr_num_writes.attr,
797 &dev_attr_invalid_io.attr,
798 &dev_attr_notify_free.attr,
799 &dev_attr_zero_pages.attr,
800 &dev_attr_orig_data_size.attr,
801 &dev_attr_compr_data_size.attr,
802 &dev_attr_mem_used_total.attr,
803 NULL,
804};
805
806static struct attribute_group zram_disk_attr_group = {
807 .attrs = zram_disk_attrs,
808};
809
f1e3cfff 810static int create_device(struct zram *zram, int device_id)
306b0c95 811{
39a9b8ac 812 int ret = -ENOMEM;
de1a21a0 813
0900beae 814 init_rwsem(&zram->init_lock);
306b0c95 815
f1e3cfff
NG
816 zram->queue = blk_alloc_queue(GFP_KERNEL);
817 if (!zram->queue) {
306b0c95
NG
818 pr_err("Error allocating disk queue for device %d\n",
819 device_id);
de1a21a0 820 goto out;
306b0c95
NG
821 }
822
f1e3cfff
NG
823 blk_queue_make_request(zram->queue, zram_make_request);
824 zram->queue->queuedata = zram;
306b0c95
NG
825
826 /* gendisk structure */
f1e3cfff
NG
827 zram->disk = alloc_disk(1);
828 if (!zram->disk) {
94b8435f 829 pr_warn("Error allocating disk structure for device %d\n",
306b0c95 830 device_id);
39a9b8ac 831 goto out_free_queue;
306b0c95
NG
832 }
833
f1e3cfff
NG
834 zram->disk->major = zram_major;
835 zram->disk->first_minor = device_id;
836 zram->disk->fops = &zram_devops;
837 zram->disk->queue = zram->queue;
838 zram->disk->private_data = zram;
839 snprintf(zram->disk->disk_name, 16, "zram%d", device_id);
306b0c95 840
33863c21 841 /* Actual capacity set using syfs (/sys/block/zram<id>/disksize */
f1e3cfff 842 set_capacity(zram->disk, 0);
5d83d5a0 843
a1dd52af
NG
844 /*
845 * To ensure that we always get PAGE_SIZE aligned
846 * and n*PAGE_SIZED sized I/O requests.
847 */
f1e3cfff 848 blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE);
7b19b8d4
RJ
849 blk_queue_logical_block_size(zram->disk->queue,
850 ZRAM_LOGICAL_BLOCK_SIZE);
f1e3cfff
NG
851 blk_queue_io_min(zram->disk->queue, PAGE_SIZE);
852 blk_queue_io_opt(zram->disk->queue, PAGE_SIZE);
5d83d5a0 853
f1e3cfff 854 add_disk(zram->disk);
306b0c95 855
33863c21
NG
856 ret = sysfs_create_group(&disk_to_dev(zram->disk)->kobj,
857 &zram_disk_attr_group);
858 if (ret < 0) {
94b8435f 859 pr_warn("Error creating sysfs group");
39a9b8ac 860 goto out_free_disk;
33863c21 861 }
33863c21 862
be2d1d56 863 zram->meta = NULL;
39a9b8ac 864 return 0;
de1a21a0 865
39a9b8ac
JL
866out_free_disk:
867 del_gendisk(zram->disk);
868 put_disk(zram->disk);
869out_free_queue:
870 blk_cleanup_queue(zram->queue);
de1a21a0
NG
871out:
872 return ret;
306b0c95
NG
873}
874
f1e3cfff 875static void destroy_device(struct zram *zram)
306b0c95 876{
33863c21
NG
877 sysfs_remove_group(&disk_to_dev(zram->disk)->kobj,
878 &zram_disk_attr_group);
33863c21 879
59d3fe54
RK
880 del_gendisk(zram->disk);
881 put_disk(zram->disk);
306b0c95 882
59d3fe54 883 blk_cleanup_queue(zram->queue);
306b0c95
NG
884}
885
f1e3cfff 886static int __init zram_init(void)
306b0c95 887{
de1a21a0 888 int ret, dev_id;
306b0c95 889
5fa5a901 890 if (num_devices > max_num_devices) {
94b8435f 891 pr_warn("Invalid value for num_devices: %u\n",
5fa5a901 892 num_devices);
de1a21a0
NG
893 ret = -EINVAL;
894 goto out;
306b0c95
NG
895 }
896
f1e3cfff
NG
897 zram_major = register_blkdev(0, "zram");
898 if (zram_major <= 0) {
94b8435f 899 pr_warn("Unable to get major number\n");
de1a21a0
NG
900 ret = -EBUSY;
901 goto out;
306b0c95
NG
902 }
903
306b0c95 904 /* Allocate the device array and initialize each one */
5fa5a901 905 zram_devices = kzalloc(num_devices * sizeof(struct zram), GFP_KERNEL);
43801f6e 906 if (!zram_devices) {
de1a21a0
NG
907 ret = -ENOMEM;
908 goto unregister;
909 }
306b0c95 910
5fa5a901 911 for (dev_id = 0; dev_id < num_devices; dev_id++) {
43801f6e 912 ret = create_device(&zram_devices[dev_id], dev_id);
de1a21a0 913 if (ret)
3bf040c7 914 goto free_devices;
de1a21a0
NG
915 }
916
ca3d70bd
DB
917 pr_info("Created %u device(s) ...\n", num_devices);
918
306b0c95 919 return 0;
de1a21a0 920
3bf040c7 921free_devices:
de1a21a0 922 while (dev_id)
43801f6e
NW
923 destroy_device(&zram_devices[--dev_id]);
924 kfree(zram_devices);
de1a21a0 925unregister:
f1e3cfff 926 unregister_blkdev(zram_major, "zram");
de1a21a0 927out:
306b0c95
NG
928 return ret;
929}
930
f1e3cfff 931static void __exit zram_exit(void)
306b0c95
NG
932{
933 int i;
f1e3cfff 934 struct zram *zram;
306b0c95 935
5fa5a901 936 for (i = 0; i < num_devices; i++) {
43801f6e 937 zram = &zram_devices[i];
306b0c95 938
f1e3cfff 939 destroy_device(zram);
2b86ab9c
MK
940 /*
941 * Shouldn't access zram->disk after destroy_device
942 * because destroy_device already released zram->disk.
943 */
944 zram_reset_device(zram, false);
306b0c95
NG
945 }
946
f1e3cfff 947 unregister_blkdev(zram_major, "zram");
306b0c95 948
43801f6e 949 kfree(zram_devices);
306b0c95
NG
950 pr_debug("Cleanup done!\n");
951}
952
f1e3cfff
NG
953module_init(zram_init);
954module_exit(zram_exit);
306b0c95 955
9b3bb7ab
SS
956module_param(num_devices, uint, 0);
957MODULE_PARM_DESC(num_devices, "Number of zram devices");
958
306b0c95
NG
959MODULE_LICENSE("Dual BSD/GPL");
960MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
f1e3cfff 961MODULE_DESCRIPTION("Compressed RAM Block Device");