Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 1 | /* |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 2 | * Compressed RAM block device |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 3 | * |
Nitin Gupta | 1130ebb | 2010-01-28 21:21:35 +0530 | [diff] [blame] | 4 | * Copyright (C) 2008, 2009, 2010 Nitin Gupta |
Minchan Kim | 7bfb3de | 2014-01-30 15:45:55 -0800 | [diff] [blame] | 5 | * 2012, 2013 Minchan Kim |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 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 | * |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 13 | */ |
| 14 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 15 | #ifndef _ZRAM_DRV_H_ |
| 16 | #define _ZRAM_DRV_H_ |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 17 | |
Nitin Gupta | 6a90772 | 2010-01-28 21:13:37 +0530 | [diff] [blame] | 18 | #include <linux/spinlock.h> |
Minchan Kim | bcf1647 | 2014-01-30 15:45:50 -0800 | [diff] [blame] | 19 | #include <linux/zsmalloc.h> |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 20 | |
Sergey Senozhatsky | b7ca232 | 2014-04-07 15:38:12 -0700 | [diff] [blame] | 21 | #include "zcomp.h" |
| 22 | |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 23 | /* |
| 24 | * Some arbitrary value. This is just to catch |
| 25 | * invalid value for num_devices module parameter. |
| 26 | */ |
| 27 | static const unsigned max_num_devices = 32; |
| 28 | |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 29 | /*-- Configurable parameters */ |
| 30 | |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 31 | /* |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 32 | * Pages that compress to size greater than this are stored |
| 33 | * uncompressed in memory. |
| 34 | */ |
Nitin Gupta | 2ccbec0 | 2011-09-09 19:01:00 -0400 | [diff] [blame] | 35 | static const size_t max_zpage_size = PAGE_SIZE / 4 * 3; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 36 | |
| 37 | /* |
Nitin Gupta | 97a0638 | 2010-05-13 14:24:21 +0530 | [diff] [blame] | 38 | * NOTE: max_zpage_size must be less than or equal to: |
Minchan Kim | 55dcbbb | 2012-10-10 08:49:52 +0900 | [diff] [blame] | 39 | * ZS_MAX_ALLOC_SIZE. Otherwise, zs_malloc() would |
| 40 | * always return failure. |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 41 | */ |
| 42 | |
| 43 | /*-- End of configurable params */ |
| 44 | |
| 45 | #define SECTOR_SHIFT 9 |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 46 | #define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT) |
| 47 | #define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT) |
Jerome Marchand | 924bd88 | 2011-06-10 15:28:48 +0200 | [diff] [blame] | 48 | #define ZRAM_LOGICAL_BLOCK_SHIFT 12 |
| 49 | #define ZRAM_LOGICAL_BLOCK_SIZE (1 << ZRAM_LOGICAL_BLOCK_SHIFT) |
| 50 | #define ZRAM_SECTOR_PER_LOGICAL_BLOCK \ |
| 51 | (1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT)) |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 52 | |
Weijie Yang | d2d5e76 | 2014-08-06 16:08:31 -0700 | [diff] [blame^] | 53 | |
| 54 | /* |
| 55 | * The lower ZRAM_FLAG_SHIFT bits of table.value is for |
| 56 | * object size (excluding header), the higher bits is for |
| 57 | * zram_pageflags. |
| 58 | * |
| 59 | * zram is mainly used for memory efficiency so we want to keep memory |
| 60 | * footprint small so we can squeeze size and flags into a field. |
| 61 | * The lower ZRAM_FLAG_SHIFT bits is for object size (excluding header), |
| 62 | * the higher bits is for zram_pageflags. |
| 63 | */ |
| 64 | #define ZRAM_FLAG_SHIFT 24 |
| 65 | |
| 66 | /* Flags for zram pages (table[page_no].value) */ |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 67 | enum zram_pageflags { |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 68 | /* Page consists entirely of zeros */ |
Weijie Yang | d2d5e76 | 2014-08-06 16:08:31 -0700 | [diff] [blame^] | 69 | ZRAM_ZERO = ZRAM_FLAG_SHIFT + 1, |
| 70 | ZRAM_ACCESS, /* page in now accessed */ |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 71 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 72 | __NR_ZRAM_PAGEFLAGS, |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | /*-- Data structures */ |
| 76 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 77 | /* Allocated for each disk page */ |
Sergey Senozhatsky | cb8f2ee | 2014-08-06 16:08:25 -0700 | [diff] [blame] | 78 | struct zram_table_entry { |
Minchan Kim | c234434 | 2012-06-08 15:39:25 +0900 | [diff] [blame] | 79 | unsigned long handle; |
Weijie Yang | d2d5e76 | 2014-08-06 16:08:31 -0700 | [diff] [blame^] | 80 | unsigned long value; |
| 81 | }; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 82 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 83 | struct zram_stats { |
Sergey Senozhatsky | 90a7806 | 2014-04-07 15:38:03 -0700 | [diff] [blame] | 84 | atomic64_t compr_data_size; /* compressed size of pages stored */ |
Jiang Liu | da5cc7d | 2013-06-07 00:07:31 +0800 | [diff] [blame] | 85 | atomic64_t num_reads; /* failed + successful */ |
| 86 | atomic64_t num_writes; /* --do-- */ |
| 87 | atomic64_t failed_reads; /* should NEVER! happen */ |
| 88 | atomic64_t failed_writes; /* can happen when memory is too low */ |
| 89 | atomic64_t invalid_io; /* non-page-aligned I/O requests */ |
| 90 | atomic64_t notify_free; /* no. of swap slot free notifications */ |
Sergey Senozhatsky | 90a7806 | 2014-04-07 15:38:03 -0700 | [diff] [blame] | 91 | atomic64_t zero_pages; /* no. of zero filled pages */ |
| 92 | atomic64_t pages_stored; /* no. of pages currently stored */ |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 93 | }; |
| 94 | |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 95 | struct zram_meta { |
Sergey Senozhatsky | cb8f2ee | 2014-08-06 16:08:25 -0700 | [diff] [blame] | 96 | struct zram_table_entry *table; |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 97 | struct zs_pool *mem_pool; |
| 98 | }; |
| 99 | |
| 100 | struct zram { |
| 101 | struct zram_meta *meta; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 102 | struct request_queue *queue; |
| 103 | struct gendisk *disk; |
Sergey Senozhatsky | b7ca232 | 2014-04-07 15:38:12 -0700 | [diff] [blame] | 104 | struct zcomp *comp; |
| 105 | |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 106 | /* Prevent concurrent execution of device init, reset and R/W request */ |
| 107 | struct rw_semaphore init_lock; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 108 | /* |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 109 | * This is the limit on amount of *uncompressed* worth of data |
| 110 | * we can store in a disk. |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 111 | */ |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 112 | u64 disksize; /* bytes */ |
Sergey Senozhatsky | beca3ec | 2014-04-07 15:38:14 -0700 | [diff] [blame] | 113 | int max_comp_streams; |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 114 | struct zram_stats stats; |
Sergey Senozhatsky | e46b8a0 | 2014-04-07 15:38:17 -0700 | [diff] [blame] | 115 | char compressor[10]; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 116 | }; |
Nitin Gupta | 6a90772 | 2010-01-28 21:13:37 +0530 | [diff] [blame] | 117 | #endif |