zram: return zram device_id from zram_add()
[linux-2.6-block.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
18#include <linux/module.h>
19#include <linux/kernel.h>
8946a086 20#include <linux/bio.h>
306b0c95
NG
21#include <linux/bitops.h>
22#include <linux/blkdev.h>
23#include <linux/buffer_head.h>
24#include <linux/device.h>
25#include <linux/genhd.h>
26#include <linux/highmem.h>
5a0e3ad6 27#include <linux/slab.h>
306b0c95 28#include <linux/string.h>
306b0c95 29#include <linux/vmalloc.h>
fcfa8d95 30#include <linux/err.h>
85508ec6 31#include <linux/idr.h>
306b0c95 32
16a4bfb9 33#include "zram_drv.h"
306b0c95 34
85508ec6 35static DEFINE_IDR(zram_index_idr);
f1e3cfff 36static int zram_major;
b7ca232e 37static const char *default_compressor = "lzo";
306b0c95 38
306b0c95 39/* Module params (documentation at end) */
ca3d70bd 40static unsigned int num_devices = 1;
33863c21 41
8f7d282c
SS
42static inline void deprecated_attr_warn(const char *name)
43{
44 pr_warn_once("%d (%s) Attribute %s (and others) will be removed. %s\n",
45 task_pid_nr(current),
46 current->comm,
47 name,
48 "See zram documentation.");
49}
50
a68eb3b6 51#define ZRAM_ATTR_RO(name) \
3bca3ef7 52static ssize_t name##_show(struct device *d, \
a68eb3b6
SS
53 struct device_attribute *attr, char *b) \
54{ \
55 struct zram *zram = dev_to_zram(d); \
8f7d282c
SS
56 \
57 deprecated_attr_warn(__stringify(name)); \
56b4e8cb 58 return scnprintf(b, PAGE_SIZE, "%llu\n", \
a68eb3b6
SS
59 (u64)atomic64_read(&zram->stats.name)); \
60} \
083914ea 61static DEVICE_ATTR_RO(name);
a68eb3b6 62
08eee69f 63static inline bool init_done(struct zram *zram)
be2d1d56 64{
08eee69f 65 return zram->disksize;
be2d1d56
SS
66}
67
9b3bb7ab
SS
68static inline struct zram *dev_to_zram(struct device *dev)
69{
70 return (struct zram *)dev_to_disk(dev)->private_data;
71}
72
b31177f2 73/* flag operations require table entry bit_spin_lock() being held */
522698d7
SS
74static int zram_test_flag(struct zram_meta *meta, u32 index,
75 enum zram_pageflags flag)
99ebbd30 76{
522698d7
SS
77 return meta->table[index].value & BIT(flag);
78}
99ebbd30 79
522698d7
SS
80static void zram_set_flag(struct zram_meta *meta, u32 index,
81 enum zram_pageflags flag)
82{
83 meta->table[index].value |= BIT(flag);
84}
99ebbd30 85
522698d7
SS
86static void zram_clear_flag(struct zram_meta *meta, u32 index,
87 enum zram_pageflags flag)
88{
89 meta->table[index].value &= ~BIT(flag);
90}
99ebbd30 91
522698d7
SS
92static size_t zram_get_obj_size(struct zram_meta *meta, u32 index)
93{
94 return meta->table[index].value & (BIT(ZRAM_FLAG_SHIFT) - 1);
99ebbd30
AM
95}
96
522698d7
SS
97static void zram_set_obj_size(struct zram_meta *meta,
98 u32 index, size_t size)
9b3bb7ab 99{
522698d7 100 unsigned long flags = meta->table[index].value >> ZRAM_FLAG_SHIFT;
9b3bb7ab 101
522698d7
SS
102 meta->table[index].value = (flags << ZRAM_FLAG_SHIFT) | size;
103}
104
105static inline int is_partial_io(struct bio_vec *bvec)
106{
107 return bvec->bv_len != PAGE_SIZE;
108}
109
110/*
111 * Check if request is within bounds and aligned on zram logical blocks.
112 */
113static inline int valid_io_request(struct zram *zram,
114 sector_t start, unsigned int size)
115{
116 u64 end, bound;
117
118 /* unaligned request */
119 if (unlikely(start & (ZRAM_SECTOR_PER_LOGICAL_BLOCK - 1)))
120 return 0;
121 if (unlikely(size & (ZRAM_LOGICAL_BLOCK_SIZE - 1)))
122 return 0;
123
124 end = start + (size >> SECTOR_SHIFT);
125 bound = zram->disksize >> SECTOR_SHIFT;
126 /* out of range range */
127 if (unlikely(start >= bound || end > bound || start > end))
128 return 0;
129
130 /* I/O request is valid */
131 return 1;
132}
133
134static void update_position(u32 *index, int *offset, struct bio_vec *bvec)
135{
136 if (*offset + bvec->bv_len >= PAGE_SIZE)
137 (*index)++;
138 *offset = (*offset + bvec->bv_len) % PAGE_SIZE;
139}
140
141static inline void update_used_max(struct zram *zram,
142 const unsigned long pages)
143{
144 unsigned long old_max, cur_max;
145
146 old_max = atomic_long_read(&zram->stats.max_used_pages);
147
148 do {
149 cur_max = old_max;
150 if (pages > cur_max)
151 old_max = atomic_long_cmpxchg(
152 &zram->stats.max_used_pages, cur_max, pages);
153 } while (old_max != cur_max);
154}
155
156static int page_zero_filled(void *ptr)
157{
158 unsigned int pos;
159 unsigned long *page;
160
161 page = (unsigned long *)ptr;
162
163 for (pos = 0; pos != PAGE_SIZE / sizeof(*page); pos++) {
164 if (page[pos])
165 return 0;
166 }
167
168 return 1;
169}
170
171static void handle_zero_page(struct bio_vec *bvec)
172{
173 struct page *page = bvec->bv_page;
174 void *user_mem;
175
176 user_mem = kmap_atomic(page);
177 if (is_partial_io(bvec))
178 memset(user_mem + bvec->bv_offset, 0, bvec->bv_len);
179 else
180 clear_page(user_mem);
181 kunmap_atomic(user_mem);
182
183 flush_dcache_page(page);
9b3bb7ab
SS
184}
185
186static ssize_t initstate_show(struct device *dev,
187 struct device_attribute *attr, char *buf)
188{
a68eb3b6 189 u32 val;
9b3bb7ab
SS
190 struct zram *zram = dev_to_zram(dev);
191
a68eb3b6
SS
192 down_read(&zram->init_lock);
193 val = init_done(zram);
194 up_read(&zram->init_lock);
9b3bb7ab 195
56b4e8cb 196 return scnprintf(buf, PAGE_SIZE, "%u\n", val);
9b3bb7ab
SS
197}
198
522698d7
SS
199static ssize_t disksize_show(struct device *dev,
200 struct device_attribute *attr, char *buf)
201{
202 struct zram *zram = dev_to_zram(dev);
203
204 return scnprintf(buf, PAGE_SIZE, "%llu\n", zram->disksize);
205}
206
9b3bb7ab
SS
207static ssize_t orig_data_size_show(struct device *dev,
208 struct device_attribute *attr, char *buf)
209{
210 struct zram *zram = dev_to_zram(dev);
211
8f7d282c 212 deprecated_attr_warn("orig_data_size");
56b4e8cb 213 return scnprintf(buf, PAGE_SIZE, "%llu\n",
90a7806e 214 (u64)(atomic64_read(&zram->stats.pages_stored)) << PAGE_SHIFT);
9b3bb7ab
SS
215}
216
9b3bb7ab
SS
217static ssize_t mem_used_total_show(struct device *dev,
218 struct device_attribute *attr, char *buf)
219{
220 u64 val = 0;
221 struct zram *zram = dev_to_zram(dev);
9b3bb7ab 222
8f7d282c 223 deprecated_attr_warn("mem_used_total");
9b3bb7ab 224 down_read(&zram->init_lock);
5a99e95b
WY
225 if (init_done(zram)) {
226 struct zram_meta *meta = zram->meta;
722cdc17 227 val = zs_get_total_pages(meta->mem_pool);
5a99e95b 228 }
9b3bb7ab
SS
229 up_read(&zram->init_lock);
230
722cdc17 231 return scnprintf(buf, PAGE_SIZE, "%llu\n", val << PAGE_SHIFT);
9b3bb7ab
SS
232}
233
9ada9da9
MK
234static ssize_t mem_limit_show(struct device *dev,
235 struct device_attribute *attr, char *buf)
236{
237 u64 val;
238 struct zram *zram = dev_to_zram(dev);
239
8f7d282c 240 deprecated_attr_warn("mem_limit");
9ada9da9
MK
241 down_read(&zram->init_lock);
242 val = zram->limit_pages;
243 up_read(&zram->init_lock);
244
245 return scnprintf(buf, PAGE_SIZE, "%llu\n", val << PAGE_SHIFT);
246}
247
248static ssize_t mem_limit_store(struct device *dev,
249 struct device_attribute *attr, const char *buf, size_t len)
250{
251 u64 limit;
252 char *tmp;
253 struct zram *zram = dev_to_zram(dev);
254
255 limit = memparse(buf, &tmp);
256 if (buf == tmp) /* no chars parsed, invalid input */
257 return -EINVAL;
258
259 down_write(&zram->init_lock);
260 zram->limit_pages = PAGE_ALIGN(limit) >> PAGE_SHIFT;
261 up_write(&zram->init_lock);
262
263 return len;
264}
265
461a8eee
MK
266static ssize_t mem_used_max_show(struct device *dev,
267 struct device_attribute *attr, char *buf)
268{
269 u64 val = 0;
270 struct zram *zram = dev_to_zram(dev);
271
8f7d282c 272 deprecated_attr_warn("mem_used_max");
461a8eee
MK
273 down_read(&zram->init_lock);
274 if (init_done(zram))
275 val = atomic_long_read(&zram->stats.max_used_pages);
276 up_read(&zram->init_lock);
277
278 return scnprintf(buf, PAGE_SIZE, "%llu\n", val << PAGE_SHIFT);
279}
280
281static ssize_t mem_used_max_store(struct device *dev,
282 struct device_attribute *attr, const char *buf, size_t len)
283{
284 int err;
285 unsigned long val;
286 struct zram *zram = dev_to_zram(dev);
461a8eee
MK
287
288 err = kstrtoul(buf, 10, &val);
289 if (err || val != 0)
290 return -EINVAL;
291
292 down_read(&zram->init_lock);
5a99e95b
WY
293 if (init_done(zram)) {
294 struct zram_meta *meta = zram->meta;
461a8eee
MK
295 atomic_long_set(&zram->stats.max_used_pages,
296 zs_get_total_pages(meta->mem_pool));
5a99e95b 297 }
461a8eee
MK
298 up_read(&zram->init_lock);
299
300 return len;
301}
302
522698d7
SS
303static ssize_t max_comp_streams_show(struct device *dev,
304 struct device_attribute *attr, char *buf)
305{
306 int val;
307 struct zram *zram = dev_to_zram(dev);
308
309 down_read(&zram->init_lock);
310 val = zram->max_comp_streams;
311 up_read(&zram->init_lock);
312
313 return scnprintf(buf, PAGE_SIZE, "%d\n", val);
314}
315
beca3ec7
SS
316static ssize_t max_comp_streams_store(struct device *dev,
317 struct device_attribute *attr, const char *buf, size_t len)
318{
319 int num;
320 struct zram *zram = dev_to_zram(dev);
60a726e3 321 int ret;
beca3ec7 322
60a726e3
MK
323 ret = kstrtoint(buf, 0, &num);
324 if (ret < 0)
325 return ret;
beca3ec7
SS
326 if (num < 1)
327 return -EINVAL;
60a726e3 328
beca3ec7
SS
329 down_write(&zram->init_lock);
330 if (init_done(zram)) {
60a726e3 331 if (!zcomp_set_max_streams(zram->comp, num)) {
fe8eb122 332 pr_info("Cannot change max compression streams\n");
60a726e3
MK
333 ret = -EINVAL;
334 goto out;
335 }
beca3ec7 336 }
60a726e3 337
beca3ec7 338 zram->max_comp_streams = num;
60a726e3
MK
339 ret = len;
340out:
beca3ec7 341 up_write(&zram->init_lock);
60a726e3 342 return ret;
beca3ec7
SS
343}
344
e46b8a03
SS
345static ssize_t comp_algorithm_show(struct device *dev,
346 struct device_attribute *attr, char *buf)
347{
348 size_t sz;
349 struct zram *zram = dev_to_zram(dev);
350
351 down_read(&zram->init_lock);
352 sz = zcomp_available_show(zram->compressor, buf);
353 up_read(&zram->init_lock);
354
355 return sz;
356}
357
358static ssize_t comp_algorithm_store(struct device *dev,
359 struct device_attribute *attr, const char *buf, size_t len)
360{
361 struct zram *zram = dev_to_zram(dev);
362 down_write(&zram->init_lock);
363 if (init_done(zram)) {
364 up_write(&zram->init_lock);
365 pr_info("Can't change algorithm for initialized device\n");
366 return -EBUSY;
367 }
368 strlcpy(zram->compressor, buf, sizeof(zram->compressor));
369 up_write(&zram->init_lock);
370 return len;
371}
372
522698d7
SS
373static ssize_t compact_store(struct device *dev,
374 struct device_attribute *attr, const char *buf, size_t len)
306b0c95 375{
522698d7
SS
376 unsigned long nr_migrated;
377 struct zram *zram = dev_to_zram(dev);
378 struct zram_meta *meta;
306b0c95 379
522698d7
SS
380 down_read(&zram->init_lock);
381 if (!init_done(zram)) {
382 up_read(&zram->init_lock);
383 return -EINVAL;
384 }
306b0c95 385
522698d7
SS
386 meta = zram->meta;
387 nr_migrated = zs_compact(meta->mem_pool);
388 atomic64_add(nr_migrated, &zram->stats.num_migrated);
389 up_read(&zram->init_lock);
d2d5e762 390
522698d7 391 return len;
d2d5e762
WY
392}
393
522698d7
SS
394static ssize_t io_stat_show(struct device *dev,
395 struct device_attribute *attr, char *buf)
d2d5e762 396{
522698d7
SS
397 struct zram *zram = dev_to_zram(dev);
398 ssize_t ret;
d2d5e762 399
522698d7
SS
400 down_read(&zram->init_lock);
401 ret = scnprintf(buf, PAGE_SIZE,
402 "%8llu %8llu %8llu %8llu\n",
403 (u64)atomic64_read(&zram->stats.failed_reads),
404 (u64)atomic64_read(&zram->stats.failed_writes),
405 (u64)atomic64_read(&zram->stats.invalid_io),
406 (u64)atomic64_read(&zram->stats.notify_free));
407 up_read(&zram->init_lock);
306b0c95 408
522698d7 409 return ret;
9b3bb7ab
SS
410}
411
522698d7
SS
412static ssize_t mm_stat_show(struct device *dev,
413 struct device_attribute *attr, char *buf)
9b3bb7ab 414{
522698d7
SS
415 struct zram *zram = dev_to_zram(dev);
416 u64 orig_size, mem_used = 0;
417 long max_used;
418 ssize_t ret;
a539c72a 419
522698d7
SS
420 down_read(&zram->init_lock);
421 if (init_done(zram))
422 mem_used = zs_get_total_pages(zram->meta->mem_pool);
9b3bb7ab 423
522698d7
SS
424 orig_size = atomic64_read(&zram->stats.pages_stored);
425 max_used = atomic_long_read(&zram->stats.max_used_pages);
9b3bb7ab 426
522698d7
SS
427 ret = scnprintf(buf, PAGE_SIZE,
428 "%8llu %8llu %8llu %8lu %8ld %8llu %8llu\n",
429 orig_size << PAGE_SHIFT,
430 (u64)atomic64_read(&zram->stats.compr_data_size),
431 mem_used << PAGE_SHIFT,
432 zram->limit_pages << PAGE_SHIFT,
433 max_used << PAGE_SHIFT,
434 (u64)atomic64_read(&zram->stats.zero_pages),
435 (u64)atomic64_read(&zram->stats.num_migrated));
436 up_read(&zram->init_lock);
9b3bb7ab 437
522698d7
SS
438 return ret;
439}
440
441static DEVICE_ATTR_RO(io_stat);
442static DEVICE_ATTR_RO(mm_stat);
443ZRAM_ATTR_RO(num_reads);
444ZRAM_ATTR_RO(num_writes);
445ZRAM_ATTR_RO(failed_reads);
446ZRAM_ATTR_RO(failed_writes);
447ZRAM_ATTR_RO(invalid_io);
448ZRAM_ATTR_RO(notify_free);
449ZRAM_ATTR_RO(zero_pages);
450ZRAM_ATTR_RO(compr_data_size);
451
452static inline bool zram_meta_get(struct zram *zram)
453{
454 if (atomic_inc_not_zero(&zram->refcount))
455 return true;
456 return false;
457}
458
459static inline void zram_meta_put(struct zram *zram)
460{
461 atomic_dec(&zram->refcount);
462}
463
464static void zram_meta_free(struct zram_meta *meta, u64 disksize)
465{
466 size_t num_pages = disksize >> PAGE_SHIFT;
467 size_t index;
1fec1172
GM
468
469 /* Free all pages that are still in this zram device */
470 for (index = 0; index < num_pages; index++) {
471 unsigned long handle = meta->table[index].handle;
472
473 if (!handle)
474 continue;
475
476 zs_free(meta->mem_pool, handle);
477 }
478
9b3bb7ab 479 zs_destroy_pool(meta->mem_pool);
9b3bb7ab
SS
480 vfree(meta->table);
481 kfree(meta);
482}
483
3eba0c6a 484static struct zram_meta *zram_meta_alloc(int device_id, u64 disksize)
9b3bb7ab
SS
485{
486 size_t num_pages;
3eba0c6a 487 char pool_name[8];
9b3bb7ab 488 struct zram_meta *meta = kmalloc(sizeof(*meta), GFP_KERNEL);
b8179958 489
9b3bb7ab 490 if (!meta)
b8179958 491 return NULL;
9b3bb7ab 492
9b3bb7ab
SS
493 num_pages = disksize >> PAGE_SHIFT;
494 meta->table = vzalloc(num_pages * sizeof(*meta->table));
495 if (!meta->table) {
496 pr_err("Error allocating zram address table\n");
b8179958 497 goto out_error;
9b3bb7ab
SS
498 }
499
3eba0c6a
GM
500 snprintf(pool_name, sizeof(pool_name), "zram%d", device_id);
501 meta->mem_pool = zs_create_pool(pool_name, GFP_NOIO | __GFP_HIGHMEM);
9b3bb7ab
SS
502 if (!meta->mem_pool) {
503 pr_err("Error creating memory pool\n");
b8179958 504 goto out_error;
9b3bb7ab
SS
505 }
506
507 return meta;
508
b8179958 509out_error:
9b3bb7ab 510 vfree(meta->table);
9b3bb7ab 511 kfree(meta);
b8179958 512 return NULL;
9b3bb7ab
SS
513}
514
d2d5e762
WY
515/*
516 * To protect concurrent access to the same index entry,
517 * caller should hold this table index entry's bit_spinlock to
518 * indicate this index entry is accessing.
519 */
f1e3cfff 520static void zram_free_page(struct zram *zram, size_t index)
306b0c95 521{
8b3cc3ed
MK
522 struct zram_meta *meta = zram->meta;
523 unsigned long handle = meta->table[index].handle;
306b0c95 524
fd1a30de 525 if (unlikely(!handle)) {
2e882281
NG
526 /*
527 * No memory is allocated for zero filled pages.
528 * Simply clear zero page flag.
529 */
8b3cc3ed
MK
530 if (zram_test_flag(meta, index, ZRAM_ZERO)) {
531 zram_clear_flag(meta, index, ZRAM_ZERO);
90a7806e 532 atomic64_dec(&zram->stats.zero_pages);
306b0c95
NG
533 }
534 return;
535 }
536
8b3cc3ed 537 zs_free(meta->mem_pool, handle);
306b0c95 538
d2d5e762
WY
539 atomic64_sub(zram_get_obj_size(meta, index),
540 &zram->stats.compr_data_size);
90a7806e 541 atomic64_dec(&zram->stats.pages_stored);
306b0c95 542
8b3cc3ed 543 meta->table[index].handle = 0;
d2d5e762 544 zram_set_obj_size(meta, index, 0);
306b0c95
NG
545}
546
37b51fdd 547static int zram_decompress_page(struct zram *zram, char *mem, u32 index)
306b0c95 548{
b7ca232e 549 int ret = 0;
37b51fdd 550 unsigned char *cmem;
8b3cc3ed 551 struct zram_meta *meta = zram->meta;
92967471 552 unsigned long handle;
023b409f 553 size_t size;
92967471 554
d2d5e762 555 bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
92967471 556 handle = meta->table[index].handle;
d2d5e762 557 size = zram_get_obj_size(meta, index);
306b0c95 558
8b3cc3ed 559 if (!handle || zram_test_flag(meta, index, ZRAM_ZERO)) {
d2d5e762 560 bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
42e99bd9 561 clear_page(mem);
8c921b2b
JM
562 return 0;
563 }
306b0c95 564
8b3cc3ed 565 cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_RO);
92967471 566 if (size == PAGE_SIZE)
42e99bd9 567 copy_page(mem, cmem);
37b51fdd 568 else
b7ca232e 569 ret = zcomp_decompress(zram->comp, cmem, size, mem);
8b3cc3ed 570 zs_unmap_object(meta->mem_pool, handle);
d2d5e762 571 bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
a1dd52af 572
8c921b2b 573 /* Should NEVER happen. Return bio error if it does. */
b7ca232e 574 if (unlikely(ret)) {
8c921b2b 575 pr_err("Decompression failed! err=%d, page=%u\n", ret, index);
8c921b2b 576 return ret;
a1dd52af 577 }
306b0c95 578
8c921b2b 579 return 0;
306b0c95
NG
580}
581
37b51fdd 582static int zram_bvec_read(struct zram *zram, struct bio_vec *bvec,
b627cff3 583 u32 index, int offset)
924bd88d
JM
584{
585 int ret;
37b51fdd
SS
586 struct page *page;
587 unsigned char *user_mem, *uncmem = NULL;
8b3cc3ed 588 struct zram_meta *meta = zram->meta;
37b51fdd
SS
589 page = bvec->bv_page;
590
d2d5e762 591 bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
8b3cc3ed
MK
592 if (unlikely(!meta->table[index].handle) ||
593 zram_test_flag(meta, index, ZRAM_ZERO)) {
d2d5e762 594 bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
37b51fdd 595 handle_zero_page(bvec);
924bd88d
JM
596 return 0;
597 }
d2d5e762 598 bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
924bd88d 599
37b51fdd
SS
600 if (is_partial_io(bvec))
601 /* Use a temporary buffer to decompress the page */
7e5a5104
MK
602 uncmem = kmalloc(PAGE_SIZE, GFP_NOIO);
603
604 user_mem = kmap_atomic(page);
605 if (!is_partial_io(bvec))
37b51fdd
SS
606 uncmem = user_mem;
607
608 if (!uncmem) {
609 pr_info("Unable to allocate temp memory\n");
610 ret = -ENOMEM;
611 goto out_cleanup;
612 }
924bd88d 613
37b51fdd 614 ret = zram_decompress_page(zram, uncmem, index);
924bd88d 615 /* Should NEVER happen. Return bio error if it does. */
b7ca232e 616 if (unlikely(ret))
37b51fdd 617 goto out_cleanup;
924bd88d 618
37b51fdd
SS
619 if (is_partial_io(bvec))
620 memcpy(user_mem + bvec->bv_offset, uncmem + offset,
621 bvec->bv_len);
622
623 flush_dcache_page(page);
624 ret = 0;
625out_cleanup:
626 kunmap_atomic(user_mem);
627 if (is_partial_io(bvec))
628 kfree(uncmem);
629 return ret;
924bd88d
JM
630}
631
632static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
633 int offset)
306b0c95 634{
397c6066 635 int ret = 0;
8c921b2b 636 size_t clen;
c2344348 637 unsigned long handle;
130f315a 638 struct page *page;
924bd88d 639 unsigned char *user_mem, *cmem, *src, *uncmem = NULL;
8b3cc3ed 640 struct zram_meta *meta = zram->meta;
b7ca232e 641 struct zcomp_strm *zstrm;
e46e3315 642 bool locked = false;
461a8eee 643 unsigned long alloced_pages;
306b0c95 644
8c921b2b 645 page = bvec->bv_page;
924bd88d
JM
646 if (is_partial_io(bvec)) {
647 /*
648 * This is a partial IO. We need to read the full page
649 * before to write the changes.
650 */
7e5a5104 651 uncmem = kmalloc(PAGE_SIZE, GFP_NOIO);
924bd88d 652 if (!uncmem) {
924bd88d
JM
653 ret = -ENOMEM;
654 goto out;
655 }
37b51fdd 656 ret = zram_decompress_page(zram, uncmem, index);
397c6066 657 if (ret)
924bd88d 658 goto out;
924bd88d
JM
659 }
660
b7ca232e 661 zstrm = zcomp_strm_find(zram->comp);
e46e3315 662 locked = true;
ba82fe2e 663 user_mem = kmap_atomic(page);
924bd88d 664
397c6066 665 if (is_partial_io(bvec)) {
924bd88d
JM
666 memcpy(uncmem + offset, user_mem + bvec->bv_offset,
667 bvec->bv_len);
397c6066
NG
668 kunmap_atomic(user_mem);
669 user_mem = NULL;
670 } else {
924bd88d 671 uncmem = user_mem;
397c6066 672 }
924bd88d
JM
673
674 if (page_zero_filled(uncmem)) {
c4065152
WY
675 if (user_mem)
676 kunmap_atomic(user_mem);
f40ac2ae 677 /* Free memory associated with this sector now. */
d2d5e762 678 bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
f40ac2ae 679 zram_free_page(zram, index);
92967471 680 zram_set_flag(meta, index, ZRAM_ZERO);
d2d5e762 681 bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
f40ac2ae 682
90a7806e 683 atomic64_inc(&zram->stats.zero_pages);
924bd88d
JM
684 ret = 0;
685 goto out;
8c921b2b 686 }
306b0c95 687
b7ca232e 688 ret = zcomp_compress(zram->comp, zstrm, uncmem, &clen);
397c6066
NG
689 if (!is_partial_io(bvec)) {
690 kunmap_atomic(user_mem);
691 user_mem = NULL;
692 uncmem = NULL;
693 }
306b0c95 694
b7ca232e 695 if (unlikely(ret)) {
8c921b2b 696 pr_err("Compression failed! err=%d\n", ret);
924bd88d 697 goto out;
8c921b2b 698 }
b7ca232e 699 src = zstrm->buffer;
c8f2f0db 700 if (unlikely(clen > max_zpage_size)) {
c8f2f0db 701 clen = PAGE_SIZE;
397c6066
NG
702 if (is_partial_io(bvec))
703 src = uncmem;
c8f2f0db 704 }
a1dd52af 705
8b3cc3ed 706 handle = zs_malloc(meta->mem_pool, clen);
fd1a30de 707 if (!handle) {
596b3dd4
MR
708 pr_info("Error allocating memory for compressed page: %u, size=%zu\n",
709 index, clen);
924bd88d
JM
710 ret = -ENOMEM;
711 goto out;
8c921b2b 712 }
9ada9da9 713
461a8eee
MK
714 alloced_pages = zs_get_total_pages(meta->mem_pool);
715 if (zram->limit_pages && alloced_pages > zram->limit_pages) {
9ada9da9
MK
716 zs_free(meta->mem_pool, handle);
717 ret = -ENOMEM;
718 goto out;
719 }
720
461a8eee
MK
721 update_used_max(zram, alloced_pages);
722
8b3cc3ed 723 cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_WO);
306b0c95 724
42e99bd9 725 if ((clen == PAGE_SIZE) && !is_partial_io(bvec)) {
397c6066 726 src = kmap_atomic(page);
42e99bd9 727 copy_page(cmem, src);
397c6066 728 kunmap_atomic(src);
42e99bd9
JL
729 } else {
730 memcpy(cmem, src, clen);
731 }
306b0c95 732
b7ca232e
SS
733 zcomp_strm_release(zram->comp, zstrm);
734 locked = false;
8b3cc3ed 735 zs_unmap_object(meta->mem_pool, handle);
fd1a30de 736
f40ac2ae
SS
737 /*
738 * Free memory associated with this sector
739 * before overwriting unused sectors.
740 */
d2d5e762 741 bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
f40ac2ae
SS
742 zram_free_page(zram, index);
743
8b3cc3ed 744 meta->table[index].handle = handle;
d2d5e762
WY
745 zram_set_obj_size(meta, index, clen);
746 bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
306b0c95 747
8c921b2b 748 /* Update stats */
90a7806e
SS
749 atomic64_add(clen, &zram->stats.compr_data_size);
750 atomic64_inc(&zram->stats.pages_stored);
924bd88d 751out:
e46e3315 752 if (locked)
b7ca232e 753 zcomp_strm_release(zram->comp, zstrm);
397c6066
NG
754 if (is_partial_io(bvec))
755 kfree(uncmem);
924bd88d 756 return ret;
8c921b2b
JM
757}
758
f4659d8e
JK
759/*
760 * zram_bio_discard - handler on discard request
761 * @index: physical block index in PAGE_SIZE units
762 * @offset: byte offset within physical block
763 */
764static void zram_bio_discard(struct zram *zram, u32 index,
765 int offset, struct bio *bio)
766{
767 size_t n = bio->bi_iter.bi_size;
d2d5e762 768 struct zram_meta *meta = zram->meta;
f4659d8e
JK
769
770 /*
771 * zram manages data in physical block size units. Because logical block
772 * size isn't identical with physical block size on some arch, we
773 * could get a discard request pointing to a specific offset within a
774 * certain physical block. Although we can handle this request by
775 * reading that physiclal block and decompressing and partially zeroing
776 * and re-compressing and then re-storing it, this isn't reasonable
777 * because our intent with a discard request is to save memory. So
778 * skipping this logical block is appropriate here.
779 */
780 if (offset) {
38515c73 781 if (n <= (PAGE_SIZE - offset))
f4659d8e
JK
782 return;
783
38515c73 784 n -= (PAGE_SIZE - offset);
f4659d8e
JK
785 index++;
786 }
787
788 while (n >= PAGE_SIZE) {
d2d5e762 789 bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
f4659d8e 790 zram_free_page(zram, index);
d2d5e762 791 bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
015254da 792 atomic64_inc(&zram->stats.notify_free);
f4659d8e
JK
793 index++;
794 n -= PAGE_SIZE;
795 }
796}
797
522698d7
SS
798static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
799 int offset, int rw)
9b3bb7ab 800{
522698d7 801 unsigned long start_time = jiffies;
9b3bb7ab 802 int ret;
9b3bb7ab 803
522698d7
SS
804 generic_start_io_acct(rw, bvec->bv_len >> SECTOR_SHIFT,
805 &zram->disk->part0);
46a51c80 806
522698d7
SS
807 if (rw == READ) {
808 atomic64_inc(&zram->stats.num_reads);
809 ret = zram_bvec_read(zram, bvec, index, offset);
810 } else {
811 atomic64_inc(&zram->stats.num_writes);
812 ret = zram_bvec_write(zram, bvec, index, offset);
1b672224 813 }
9b3bb7ab 814
522698d7 815 generic_end_io_acct(rw, &zram->disk->part0, start_time);
9b3bb7ab 816
522698d7
SS
817 if (unlikely(ret)) {
818 if (rw == READ)
819 atomic64_inc(&zram->stats.failed_reads);
820 else
821 atomic64_inc(&zram->stats.failed_writes);
1b672224 822 }
9b3bb7ab 823
1b672224 824 return ret;
8c921b2b
JM
825}
826
be257c61 827static void __zram_make_request(struct zram *zram, struct bio *bio)
8c921b2b 828{
b627cff3 829 int offset, rw;
8c921b2b 830 u32 index;
7988613b
KO
831 struct bio_vec bvec;
832 struct bvec_iter iter;
8c921b2b 833
4f024f37
KO
834 index = bio->bi_iter.bi_sector >> SECTORS_PER_PAGE_SHIFT;
835 offset = (bio->bi_iter.bi_sector &
836 (SECTORS_PER_PAGE - 1)) << SECTOR_SHIFT;
8c921b2b 837
f4659d8e
JK
838 if (unlikely(bio->bi_rw & REQ_DISCARD)) {
839 zram_bio_discard(zram, index, offset, bio);
840 bio_endio(bio, 0);
841 return;
842 }
843
b627cff3 844 rw = bio_data_dir(bio);
7988613b 845 bio_for_each_segment(bvec, bio, iter) {
924bd88d
JM
846 int max_transfer_size = PAGE_SIZE - offset;
847
7988613b 848 if (bvec.bv_len > max_transfer_size) {
924bd88d
JM
849 /*
850 * zram_bvec_rw() can only make operation on a single
851 * zram page. Split the bio vector.
852 */
853 struct bio_vec bv;
854
7988613b 855 bv.bv_page = bvec.bv_page;
924bd88d 856 bv.bv_len = max_transfer_size;
7988613b 857 bv.bv_offset = bvec.bv_offset;
924bd88d 858
b627cff3 859 if (zram_bvec_rw(zram, &bv, index, offset, rw) < 0)
924bd88d
JM
860 goto out;
861
7988613b 862 bv.bv_len = bvec.bv_len - max_transfer_size;
924bd88d 863 bv.bv_offset += max_transfer_size;
b627cff3 864 if (zram_bvec_rw(zram, &bv, index + 1, 0, rw) < 0)
924bd88d
JM
865 goto out;
866 } else
b627cff3 867 if (zram_bvec_rw(zram, &bvec, index, offset, rw) < 0)
924bd88d
JM
868 goto out;
869
7988613b 870 update_position(&index, &offset, &bvec);
a1dd52af 871 }
306b0c95
NG
872
873 set_bit(BIO_UPTODATE, &bio->bi_flags);
874 bio_endio(bio, 0);
7d7854b4 875 return;
306b0c95
NG
876
877out:
306b0c95 878 bio_io_error(bio);
306b0c95
NG
879}
880
306b0c95 881/*
f1e3cfff 882 * Handler function for all zram I/O requests.
306b0c95 883 */
5a7bbad2 884static void zram_make_request(struct request_queue *queue, struct bio *bio)
306b0c95 885{
f1e3cfff 886 struct zram *zram = queue->queuedata;
306b0c95 887
08eee69f 888 if (unlikely(!zram_meta_get(zram)))
3de738cd 889 goto error;
0900beae 890
54850e73 891 if (!valid_io_request(zram, bio->bi_iter.bi_sector,
892 bio->bi_iter.bi_size)) {
da5cc7d3 893 atomic64_inc(&zram->stats.invalid_io);
08eee69f 894 goto put_zram;
6642a67c
JM
895 }
896
be257c61 897 __zram_make_request(zram, bio);
08eee69f 898 zram_meta_put(zram);
b4fdcb02 899 return;
08eee69f
MK
900put_zram:
901 zram_meta_put(zram);
0900beae
JM
902error:
903 bio_io_error(bio);
306b0c95
NG
904}
905
2ccbec05
NG
906static void zram_slot_free_notify(struct block_device *bdev,
907 unsigned long index)
107c161b 908{
f1e3cfff 909 struct zram *zram;
f614a9f4 910 struct zram_meta *meta;
107c161b 911
f1e3cfff 912 zram = bdev->bd_disk->private_data;
f614a9f4 913 meta = zram->meta;
a0c516cb 914
d2d5e762 915 bit_spin_lock(ZRAM_ACCESS, &meta->table[index].value);
f614a9f4 916 zram_free_page(zram, index);
d2d5e762 917 bit_spin_unlock(ZRAM_ACCESS, &meta->table[index].value);
f614a9f4 918 atomic64_inc(&zram->stats.notify_free);
107c161b
NG
919}
920
8c7f0102 921static int zram_rw_page(struct block_device *bdev, sector_t sector,
922 struct page *page, int rw)
923{
08eee69f 924 int offset, err = -EIO;
8c7f0102 925 u32 index;
926 struct zram *zram;
927 struct bio_vec bv;
928
929 zram = bdev->bd_disk->private_data;
08eee69f
MK
930 if (unlikely(!zram_meta_get(zram)))
931 goto out;
932
8c7f0102 933 if (!valid_io_request(zram, sector, PAGE_SIZE)) {
934 atomic64_inc(&zram->stats.invalid_io);
08eee69f
MK
935 err = -EINVAL;
936 goto put_zram;
8c7f0102 937 }
938
939 index = sector >> SECTORS_PER_PAGE_SHIFT;
940 offset = sector & (SECTORS_PER_PAGE - 1) << SECTOR_SHIFT;
941
942 bv.bv_page = page;
943 bv.bv_len = PAGE_SIZE;
944 bv.bv_offset = 0;
945
946 err = zram_bvec_rw(zram, &bv, index, offset, rw);
08eee69f
MK
947put_zram:
948 zram_meta_put(zram);
949out:
8c7f0102 950 /*
951 * If I/O fails, just return error(ie, non-zero) without
952 * calling page_endio.
953 * It causes resubmit the I/O with bio request by upper functions
954 * of rw_page(e.g., swap_readpage, __swap_writepage) and
955 * bio->bi_end_io does things to handle the error
956 * (e.g., SetPageError, set_page_dirty and extra works).
957 */
958 if (err == 0)
959 page_endio(page, rw, 0);
960 return err;
961}
962
522698d7
SS
963static void zram_reset_device(struct zram *zram)
964{
965 struct zram_meta *meta;
966 struct zcomp *comp;
967 u64 disksize;
306b0c95 968
522698d7 969 down_write(&zram->init_lock);
9b3bb7ab 970
522698d7
SS
971 zram->limit_pages = 0;
972
973 if (!init_done(zram)) {
974 up_write(&zram->init_lock);
975 return;
976 }
977
978 meta = zram->meta;
979 comp = zram->comp;
980 disksize = zram->disksize;
981 /*
982 * Refcount will go down to 0 eventually and r/w handler
983 * cannot handle further I/O so it will bail out by
984 * check zram_meta_get.
985 */
986 zram_meta_put(zram);
987 /*
988 * We want to free zram_meta in process context to avoid
989 * deadlock between reclaim path and any other locks.
990 */
991 wait_event(zram->io_done, atomic_read(&zram->refcount) == 0);
992
993 /* Reset stats */
994 memset(&zram->stats, 0, sizeof(zram->stats));
995 zram->disksize = 0;
996 zram->max_comp_streams = 1;
997
998 set_capacity(zram->disk, 0);
999 part_stat_set_all(&zram->disk->part0, 0);
1000
1001 up_write(&zram->init_lock);
1002 /* I/O operation under all of CPU are done so let's free */
1003 zram_meta_free(meta, disksize);
1004 zcomp_destroy(comp);
1005}
1006
1007static ssize_t disksize_store(struct device *dev,
1008 struct device_attribute *attr, const char *buf, size_t len)
2f6a3bed 1009{
522698d7
SS
1010 u64 disksize;
1011 struct zcomp *comp;
1012 struct zram_meta *meta;
2f6a3bed 1013 struct zram *zram = dev_to_zram(dev);
522698d7 1014 int err;
2f6a3bed 1015
522698d7
SS
1016 disksize = memparse(buf, NULL);
1017 if (!disksize)
1018 return -EINVAL;
2f6a3bed 1019
522698d7
SS
1020 disksize = PAGE_ALIGN(disksize);
1021 meta = zram_meta_alloc(zram->disk->first_minor, disksize);
1022 if (!meta)
1023 return -ENOMEM;
1024
1025 comp = zcomp_create(zram->compressor, zram->max_comp_streams);
1026 if (IS_ERR(comp)) {
1027 pr_info("Cannot initialise %s compressing backend\n",
1028 zram->compressor);
1029 err = PTR_ERR(comp);
1030 goto out_free_meta;
1031 }
1032
1033 down_write(&zram->init_lock);
1034 if (init_done(zram)) {
1035 pr_info("Cannot change disksize for initialized device\n");
1036 err = -EBUSY;
1037 goto out_destroy_comp;
1038 }
1039
1040 init_waitqueue_head(&zram->io_done);
1041 atomic_set(&zram->refcount, 1);
1042 zram->meta = meta;
1043 zram->comp = comp;
1044 zram->disksize = disksize;
1045 set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
1046 up_write(&zram->init_lock);
1047
1048 /*
1049 * Revalidate disk out of the init_lock to avoid lockdep splat.
1050 * It's okay because disk's capacity is protected by init_lock
1051 * so that revalidate_disk always sees up-to-date capacity.
1052 */
1053 revalidate_disk(zram->disk);
1054
1055 return len;
1056
1057out_destroy_comp:
1058 up_write(&zram->init_lock);
1059 zcomp_destroy(comp);
1060out_free_meta:
1061 zram_meta_free(meta, disksize);
1062 return err;
2f6a3bed
SS
1063}
1064
522698d7
SS
1065static ssize_t reset_store(struct device *dev,
1066 struct device_attribute *attr, const char *buf, size_t len)
4f2109f6 1067{
522698d7
SS
1068 int ret;
1069 unsigned short do_reset;
1070 struct zram *zram;
1071 struct block_device *bdev;
4f2109f6 1072
522698d7
SS
1073 zram = dev_to_zram(dev);
1074 bdev = bdget_disk(zram->disk, 0);
4f2109f6 1075
522698d7
SS
1076 if (!bdev)
1077 return -ENOMEM;
4f2109f6 1078
522698d7
SS
1079 mutex_lock(&bdev->bd_mutex);
1080 /* Do not reset an active device! */
1081 if (bdev->bd_openers) {
1082 ret = -EBUSY;
1083 goto out;
1084 }
1085
1086 ret = kstrtou16(buf, 10, &do_reset);
1087 if (ret)
1088 goto out;
1089
1090 if (!do_reset) {
1091 ret = -EINVAL;
1092 goto out;
1093 }
1094
1095 /* Make sure all pending I/O is finished */
1096 fsync_bdev(bdev);
1097 zram_reset_device(zram);
1098
1099 mutex_unlock(&bdev->bd_mutex);
1100 revalidate_disk(zram->disk);
1101 bdput(bdev);
1102
1103 return len;
4f2109f6 1104
522698d7
SS
1105out:
1106 mutex_unlock(&bdev->bd_mutex);
1107 bdput(bdev);
4f2109f6
SS
1108 return ret;
1109}
1110
522698d7
SS
1111static const struct block_device_operations zram_devops = {
1112 .swap_slot_free_notify = zram_slot_free_notify,
1113 .rw_page = zram_rw_page,
1114 .owner = THIS_MODULE
1115};
1116
1117static DEVICE_ATTR_WO(compact);
1118static DEVICE_ATTR_RW(disksize);
1119static DEVICE_ATTR_RO(initstate);
1120static DEVICE_ATTR_WO(reset);
1121static DEVICE_ATTR_RO(orig_data_size);
1122static DEVICE_ATTR_RO(mem_used_total);
1123static DEVICE_ATTR_RW(mem_limit);
1124static DEVICE_ATTR_RW(mem_used_max);
1125static DEVICE_ATTR_RW(max_comp_streams);
1126static DEVICE_ATTR_RW(comp_algorithm);
a68eb3b6 1127
9b3bb7ab
SS
1128static struct attribute *zram_disk_attrs[] = {
1129 &dev_attr_disksize.attr,
1130 &dev_attr_initstate.attr,
1131 &dev_attr_reset.attr,
1132 &dev_attr_num_reads.attr,
1133 &dev_attr_num_writes.attr,
64447249
SS
1134 &dev_attr_failed_reads.attr,
1135 &dev_attr_failed_writes.attr,
99ebbd30 1136 &dev_attr_compact.attr,
9b3bb7ab
SS
1137 &dev_attr_invalid_io.attr,
1138 &dev_attr_notify_free.attr,
1139 &dev_attr_zero_pages.attr,
1140 &dev_attr_orig_data_size.attr,
1141 &dev_attr_compr_data_size.attr,
1142 &dev_attr_mem_used_total.attr,
9ada9da9 1143 &dev_attr_mem_limit.attr,
461a8eee 1144 &dev_attr_mem_used_max.attr,
beca3ec7 1145 &dev_attr_max_comp_streams.attr,
e46b8a03 1146 &dev_attr_comp_algorithm.attr,
2f6a3bed 1147 &dev_attr_io_stat.attr,
4f2109f6 1148 &dev_attr_mm_stat.attr,
9b3bb7ab
SS
1149 NULL,
1150};
1151
1152static struct attribute_group zram_disk_attr_group = {
1153 .attrs = zram_disk_attrs,
1154};
1155
92ff1528
SS
1156/*
1157 * Allocate and initialize new zram device. the function returns
1158 * '>= 0' device_id upon success, and negative value otherwise.
1159 */
1160static int zram_add(void)
306b0c95 1161{
85508ec6 1162 struct zram *zram;
ee980160 1163 struct request_queue *queue;
92ff1528 1164 int ret, device_id;
85508ec6
SS
1165
1166 zram = kzalloc(sizeof(struct zram), GFP_KERNEL);
1167 if (!zram)
1168 return -ENOMEM;
1169
92ff1528 1170 ret = idr_alloc(&zram_index_idr, zram, 0, 0, GFP_KERNEL);
85508ec6
SS
1171 if (ret < 0)
1172 goto out_free_dev;
92ff1528 1173 device_id = ret;
de1a21a0 1174
0900beae 1175 init_rwsem(&zram->init_lock);
306b0c95 1176
ee980160
SS
1177 queue = blk_alloc_queue(GFP_KERNEL);
1178 if (!queue) {
306b0c95
NG
1179 pr_err("Error allocating disk queue for device %d\n",
1180 device_id);
85508ec6
SS
1181 ret = -ENOMEM;
1182 goto out_free_idr;
306b0c95
NG
1183 }
1184
ee980160 1185 blk_queue_make_request(queue, zram_make_request);
306b0c95 1186
85508ec6 1187 /* gendisk structure */
f1e3cfff
NG
1188 zram->disk = alloc_disk(1);
1189 if (!zram->disk) {
94b8435f 1190 pr_warn("Error allocating disk structure for device %d\n",
306b0c95 1191 device_id);
201c7b72 1192 ret = -ENOMEM;
39a9b8ac 1193 goto out_free_queue;
306b0c95
NG
1194 }
1195
f1e3cfff
NG
1196 zram->disk->major = zram_major;
1197 zram->disk->first_minor = device_id;
1198 zram->disk->fops = &zram_devops;
ee980160
SS
1199 zram->disk->queue = queue;
1200 zram->disk->queue->queuedata = zram;
f1e3cfff
NG
1201 zram->disk->private_data = zram;
1202 snprintf(zram->disk->disk_name, 16, "zram%d", device_id);
306b0c95 1203
33863c21 1204 /* Actual capacity set using syfs (/sys/block/zram<id>/disksize */
f1e3cfff 1205 set_capacity(zram->disk, 0);
b67d1ec1
SS
1206 /* zram devices sort of resembles non-rotational disks */
1207 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, zram->disk->queue);
b277da0a 1208 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, zram->disk->queue);
a1dd52af
NG
1209 /*
1210 * To ensure that we always get PAGE_SIZE aligned
1211 * and n*PAGE_SIZED sized I/O requests.
1212 */
f1e3cfff 1213 blk_queue_physical_block_size(zram->disk->queue, PAGE_SIZE);
7b19b8d4
RJ
1214 blk_queue_logical_block_size(zram->disk->queue,
1215 ZRAM_LOGICAL_BLOCK_SIZE);
f1e3cfff
NG
1216 blk_queue_io_min(zram->disk->queue, PAGE_SIZE);
1217 blk_queue_io_opt(zram->disk->queue, PAGE_SIZE);
f4659d8e
JK
1218 zram->disk->queue->limits.discard_granularity = PAGE_SIZE;
1219 zram->disk->queue->limits.max_discard_sectors = UINT_MAX;
1220 /*
1221 * zram_bio_discard() will clear all logical blocks if logical block
1222 * size is identical with physical block size(PAGE_SIZE). But if it is
1223 * different, we will skip discarding some parts of logical blocks in
1224 * the part of the request range which isn't aligned to physical block
1225 * size. So we can't ensure that all discarded logical blocks are
1226 * zeroed.
1227 */
1228 if (ZRAM_LOGICAL_BLOCK_SIZE == PAGE_SIZE)
1229 zram->disk->queue->limits.discard_zeroes_data = 1;
1230 else
1231 zram->disk->queue->limits.discard_zeroes_data = 0;
1232 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, zram->disk->queue);
5d83d5a0 1233
f1e3cfff 1234 add_disk(zram->disk);
306b0c95 1235
33863c21
NG
1236 ret = sysfs_create_group(&disk_to_dev(zram->disk)->kobj,
1237 &zram_disk_attr_group);
1238 if (ret < 0) {
94b8435f 1239 pr_warn("Error creating sysfs group");
39a9b8ac 1240 goto out_free_disk;
33863c21 1241 }
e46b8a03 1242 strlcpy(zram->compressor, default_compressor, sizeof(zram->compressor));
be2d1d56 1243 zram->meta = NULL;
beca3ec7 1244 zram->max_comp_streams = 1;
d12b63c9
SS
1245
1246 pr_info("Added device: %s\n", zram->disk->disk_name);
92ff1528 1247 return device_id;
de1a21a0 1248
39a9b8ac
JL
1249out_free_disk:
1250 del_gendisk(zram->disk);
1251 put_disk(zram->disk);
1252out_free_queue:
ee980160 1253 blk_cleanup_queue(queue);
85508ec6
SS
1254out_free_idr:
1255 idr_remove(&zram_index_idr, device_id);
1256out_free_dev:
1257 kfree(zram);
de1a21a0 1258 return ret;
306b0c95
NG
1259}
1260
85508ec6 1261static void zram_remove(struct zram *zram)
306b0c95 1262{
d12b63c9 1263 pr_info("Removed device: %s\n", zram->disk->disk_name);
85508ec6
SS
1264 /*
1265 * Remove sysfs first, so no one will perform a disksize
1266 * store while we destroy the devices
1267 */
1268 sysfs_remove_group(&disk_to_dev(zram->disk)->kobj,
1269 &zram_disk_attr_group);
306b0c95 1270
85508ec6
SS
1271 zram_reset_device(zram);
1272 idr_remove(&zram_index_idr, zram->disk->first_minor);
1273 blk_cleanup_queue(zram->disk->queue);
1274 del_gendisk(zram->disk);
1275 put_disk(zram->disk);
1276 kfree(zram);
1277}
a096cafc 1278
85508ec6
SS
1279static int zram_remove_cb(int id, void *ptr, void *data)
1280{
1281 zram_remove(ptr);
1282 return 0;
1283}
a096cafc 1284
85508ec6
SS
1285static void destroy_devices(void)
1286{
1287 idr_for_each(&zram_index_idr, &zram_remove_cb, NULL);
1288 idr_destroy(&zram_index_idr);
a096cafc 1289 unregister_blkdev(zram_major, "zram");
306b0c95
NG
1290}
1291
f1e3cfff 1292static int __init zram_init(void)
306b0c95 1293{
92ff1528 1294 int ret;
306b0c95 1295
f1e3cfff
NG
1296 zram_major = register_blkdev(0, "zram");
1297 if (zram_major <= 0) {
94b8435f 1298 pr_warn("Unable to get major number\n");
a096cafc 1299 return -EBUSY;
306b0c95
NG
1300 }
1301
92ff1528
SS
1302 while (num_devices != 0) {
1303 ret = zram_add();
1304 if (ret < 0)
a096cafc 1305 goto out_error;
92ff1528 1306 num_devices--;
de1a21a0
NG
1307 }
1308
306b0c95 1309 return 0;
de1a21a0 1310
a096cafc 1311out_error:
85508ec6 1312 destroy_devices();
306b0c95
NG
1313 return ret;
1314}
1315
f1e3cfff 1316static void __exit zram_exit(void)
306b0c95 1317{
85508ec6 1318 destroy_devices();
306b0c95
NG
1319}
1320
f1e3cfff
NG
1321module_init(zram_init);
1322module_exit(zram_exit);
306b0c95 1323
9b3bb7ab 1324module_param(num_devices, uint, 0);
c3cdb40e 1325MODULE_PARM_DESC(num_devices, "Number of pre-created zram devices");
9b3bb7ab 1326
306b0c95
NG
1327MODULE_LICENSE("Dual BSD/GPL");
1328MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
f1e3cfff 1329MODULE_DESCRIPTION("Compressed RAM Block Device");