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