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> |
| 19 | #include <linux/mutex.h> |
Minchan Kim | bcf1647 | 2014-01-30 15:45:50 -0800 | [diff] [blame] | 20 | #include <linux/zsmalloc.h> |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 21 | |
| 22 | /* |
| 23 | * Some arbitrary value. This is just to catch |
| 24 | * invalid value for num_devices module parameter. |
| 25 | */ |
| 26 | static const unsigned max_num_devices = 32; |
| 27 | |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 28 | /*-- Configurable parameters */ |
| 29 | |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 30 | /* |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 31 | * Pages that compress to size greater than this are stored |
| 32 | * uncompressed in memory. |
| 33 | */ |
Nitin Gupta | 2ccbec0 | 2011-09-09 19:01:00 -0400 | [diff] [blame] | 34 | static const size_t max_zpage_size = PAGE_SIZE / 4 * 3; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 35 | |
| 36 | /* |
Nitin Gupta | 97a0638 | 2010-05-13 14:24:21 +0530 | [diff] [blame] | 37 | * NOTE: max_zpage_size must be less than or equal to: |
Minchan Kim | 55dcbbb | 2012-10-10 08:49:52 +0900 | [diff] [blame] | 38 | * ZS_MAX_ALLOC_SIZE. Otherwise, zs_malloc() would |
| 39 | * always return failure. |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 40 | */ |
| 41 | |
| 42 | /*-- End of configurable params */ |
| 43 | |
| 44 | #define SECTOR_SHIFT 9 |
| 45 | #define SECTOR_SIZE (1 << SECTOR_SHIFT) |
| 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 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 53 | /* Flags for zram pages (table[page_no].flags) */ |
| 54 | enum zram_pageflags { |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 55 | /* Page consists entirely of zeros */ |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 56 | ZRAM_ZERO, |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 57 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 58 | __NR_ZRAM_PAGEFLAGS, |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | /*-- Data structures */ |
| 62 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 63 | /* Allocated for each disk page */ |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 64 | struct table { |
Minchan Kim | c234434 | 2012-06-08 15:39:25 +0900 | [diff] [blame] | 65 | unsigned long handle; |
Nitin Gupta | fd1a30d | 2012-01-09 16:51:59 -0600 | [diff] [blame] | 66 | u16 size; /* object size (excluding header) */ |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 67 | u8 count; /* object ref count (not yet used) */ |
| 68 | u8 flags; |
Sam Hansen | 80677c2 | 2012-06-07 16:03:48 -0700 | [diff] [blame] | 69 | } __aligned(4); |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 70 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 71 | struct zram_stats { |
Sergey Senozhatsky | 90a7806 | 2014-04-07 15:38:03 -0700 | [diff] [blame^] | 72 | atomic64_t compr_data_size; /* compressed size of pages stored */ |
Jiang Liu | da5cc7d | 2013-06-07 00:07:31 +0800 | [diff] [blame] | 73 | atomic64_t num_reads; /* failed + successful */ |
| 74 | atomic64_t num_writes; /* --do-- */ |
| 75 | atomic64_t failed_reads; /* should NEVER! happen */ |
| 76 | atomic64_t failed_writes; /* can happen when memory is too low */ |
| 77 | atomic64_t invalid_io; /* non-page-aligned I/O requests */ |
| 78 | atomic64_t notify_free; /* no. of swap slot free notifications */ |
Sergey Senozhatsky | 90a7806 | 2014-04-07 15:38:03 -0700 | [diff] [blame^] | 79 | atomic64_t zero_pages; /* no. of zero filled pages */ |
| 80 | atomic64_t pages_stored; /* no. of pages currently stored */ |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 81 | }; |
| 82 | |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 83 | struct zram_meta { |
Minchan Kim | 9296747 | 2014-01-30 15:46:03 -0800 | [diff] [blame] | 84 | rwlock_t tb_lock; /* protect table */ |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 85 | void *compress_workmem; |
| 86 | void *compress_buffer; |
| 87 | struct table *table; |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 88 | struct zs_pool *mem_pool; |
Minchan Kim | e46e331 | 2014-01-30 15:46:06 -0800 | [diff] [blame] | 89 | struct mutex buffer_lock; /* protect compress buffers */ |
Minchan Kim | 8b3cc3e | 2013-02-06 08:48:53 +0900 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | struct zram { |
| 93 | struct zram_meta *meta; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 94 | struct request_queue *queue; |
| 95 | struct gendisk *disk; |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 96 | /* Prevent concurrent execution of device init, reset and R/W request */ |
| 97 | struct rw_semaphore init_lock; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 98 | /* |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 99 | * This is the limit on amount of *uncompressed* worth of data |
| 100 | * we can store in a disk. |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 101 | */ |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 102 | u64 disksize; /* bytes */ |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 103 | |
Nitin Gupta | f1e3cff | 2010-06-01 13:31:25 +0530 | [diff] [blame] | 104 | struct zram_stats stats; |
Nitin Gupta | 306b0c9 | 2009-09-22 10:26:53 +0530 | [diff] [blame] | 105 | }; |
Nitin Gupta | 6a90772 | 2010-01-28 21:13:37 +0530 | [diff] [blame] | 106 | #endif |