Commit | Line | Data |
---|---|---|
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 NG |
15 | #ifndef _ZRAM_DRV_H_ |
16 | #define _ZRAM_DRV_H_ | |
306b0c95 | 17 | |
415403be | 18 | #include <linux/rwsem.h> |
bcf1647d | 19 | #include <linux/zsmalloc.h> |
415403be | 20 | #include <linux/crypto.h> |
306b0c95 | 21 | |
b7ca232e SS |
22 | #include "zcomp.h" |
23 | ||
306b0c95 NG |
24 | #define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT) |
25 | #define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT) | |
924bd88d JM |
26 | #define ZRAM_LOGICAL_BLOCK_SHIFT 12 |
27 | #define ZRAM_LOGICAL_BLOCK_SIZE (1 << ZRAM_LOGICAL_BLOCK_SHIFT) | |
28 | #define ZRAM_SECTOR_PER_LOGICAL_BLOCK \ | |
29 | (1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT)) | |
306b0c95 | 30 | |
d2d5e762 WY |
31 | |
32 | /* | |
33 | * The lower ZRAM_FLAG_SHIFT bits of table.value is for | |
34 | * object size (excluding header), the higher bits is for | |
35 | * zram_pageflags. | |
36 | * | |
37 | * zram is mainly used for memory efficiency so we want to keep memory | |
38 | * footprint small so we can squeeze size and flags into a field. | |
39 | * The lower ZRAM_FLAG_SHIFT bits is for object size (excluding header), | |
40 | * the higher bits is for zram_pageflags. | |
41 | */ | |
42 | #define ZRAM_FLAG_SHIFT 24 | |
43 | ||
44 | /* Flags for zram pages (table[page_no].value) */ | |
f1e3cfff | 45 | enum zram_pageflags { |
c4d6c4cc MK |
46 | /* zram slot is locked */ |
47 | ZRAM_LOCK = ZRAM_FLAG_SHIFT, | |
48 | ZRAM_SAME, /* Page consists the same element */ | |
db8ffbd4 | 49 | ZRAM_WB, /* page is stored on backing_device */ |
89e85bce | 50 | ZRAM_HUGE, /* Incompressible page */ |
306b0c95 | 51 | |
f1e3cfff | 52 | __NR_ZRAM_PAGEFLAGS, |
306b0c95 NG |
53 | }; |
54 | ||
55 | /*-- Data structures */ | |
56 | ||
f1e3cfff | 57 | /* Allocated for each disk page */ |
cb8f2eec | 58 | struct zram_table_entry { |
8e19d540 | 59 | union { |
60 | unsigned long handle; | |
61 | unsigned long element; | |
62 | }; | |
d2d5e762 | 63 | unsigned long value; |
c0265342 MK |
64 | #ifdef CONFIG_ZRAM_MEMORY_TRACKING |
65 | ktime_t ac_time; | |
66 | #endif | |
d2d5e762 | 67 | }; |
306b0c95 | 68 | |
f1e3cfff | 69 | struct zram_stats { |
90a7806e | 70 | atomic64_t compr_data_size; /* compressed size of pages stored */ |
da5cc7d3 JL |
71 | atomic64_t num_reads; /* failed + successful */ |
72 | atomic64_t num_writes; /* --do-- */ | |
0cf1e9d6 | 73 | atomic64_t failed_reads; /* can happen when memory is too low */ |
da5cc7d3 JL |
74 | atomic64_t failed_writes; /* can happen when memory is too low */ |
75 | atomic64_t invalid_io; /* non-page-aligned I/O requests */ | |
76 | atomic64_t notify_free; /* no. of swap slot free notifications */ | |
8e19d540 | 77 | atomic64_t same_pages; /* no. of same element filled pages */ |
89e85bce | 78 | atomic64_t huge_pages; /* no. of huge pages */ |
90a7806e | 79 | atomic64_t pages_stored; /* no. of pages currently stored */ |
461a8eee | 80 | atomic_long_t max_used_pages; /* no. of maximum pages stored */ |
623e47fc | 81 | atomic64_t writestall; /* no. of write slow paths */ |
306b0c95 NG |
82 | }; |
83 | ||
beb6602c | 84 | struct zram { |
cb8f2eec | 85 | struct zram_table_entry *table; |
8b3cc3ed | 86 | struct zs_pool *mem_pool; |
08eee69f | 87 | struct zcomp *comp; |
306b0c95 | 88 | struct gendisk *disk; |
08eee69f | 89 | /* Prevent concurrent execution of device init */ |
0900beae | 90 | struct rw_semaphore init_lock; |
306b0c95 | 91 | /* |
08eee69f | 92 | * the number of pages zram can consume for storing compressed data |
306b0c95 | 93 | */ |
08eee69f | 94 | unsigned long limit_pages; |
08eee69f | 95 | |
f1e3cfff | 96 | struct zram_stats stats; |
9ada9da9 | 97 | /* |
08eee69f MK |
98 | * This is the limit on amount of *uncompressed* worth of data |
99 | * we can store in a disk. | |
9ada9da9 | 100 | */ |
08eee69f | 101 | u64 disksize; /* bytes */ |
415403be | 102 | char compressor[CRYPTO_MAX_ALG_NAME]; |
f405c445 SS |
103 | /* |
104 | * zram is claimed so open request will be failed | |
105 | */ | |
106 | bool claim; /* Protected by bdev->bd_mutex */ | |
013bf95a MK |
107 | #ifdef CONFIG_ZRAM_WRITEBACK |
108 | struct file *backing_dev; | |
109 | struct block_device *bdev; | |
110 | unsigned int old_block_size; | |
1363d466 MK |
111 | unsigned long *bitmap; |
112 | unsigned long nr_pages; | |
113 | spinlock_t bitmap_lock; | |
013bf95a | 114 | #endif |
c0265342 MK |
115 | #ifdef CONFIG_ZRAM_MEMORY_TRACKING |
116 | struct dentry *debugfs_dir; | |
117 | #endif | |
306b0c95 | 118 | }; |
6a907728 | 119 | #endif |