]>
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 | |
6a907728 NG |
18 | #include <linux/spinlock.h> |
19 | #include <linux/mutex.h> | |
bcf1647d | 20 | #include <linux/zsmalloc.h> |
306b0c95 NG |
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 | ||
306b0c95 NG |
28 | /*-- Configurable parameters */ |
29 | ||
306b0c95 | 30 | /* |
306b0c95 NG |
31 | * Pages that compress to size greater than this are stored |
32 | * uncompressed in memory. | |
33 | */ | |
2ccbec05 | 34 | static const size_t max_zpage_size = PAGE_SIZE / 4 * 3; |
306b0c95 NG |
35 | |
36 | /* | |
97a06382 | 37 | * NOTE: max_zpage_size must be less than or equal to: |
55dcbbb1 MK |
38 | * ZS_MAX_ALLOC_SIZE. Otherwise, zs_malloc() would |
39 | * always return failure. | |
306b0c95 NG |
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) | |
924bd88d JM |
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)) | |
306b0c95 | 52 | |
f1e3cfff NG |
53 | /* Flags for zram pages (table[page_no].flags) */ |
54 | enum zram_pageflags { | |
306b0c95 | 55 | /* Page consists entirely of zeros */ |
f1e3cfff | 56 | ZRAM_ZERO, |
306b0c95 | 57 | |
f1e3cfff | 58 | __NR_ZRAM_PAGEFLAGS, |
306b0c95 NG |
59 | }; |
60 | ||
61 | /*-- Data structures */ | |
62 | ||
f1e3cfff | 63 | /* Allocated for each disk page */ |
306b0c95 | 64 | struct table { |
c2344348 | 65 | unsigned long handle; |
fd1a30de | 66 | u16 size; /* object size (excluding header) */ |
306b0c95 NG |
67 | u8 count; /* object ref count (not yet used) */ |
68 | u8 flags; | |
80677c25 | 69 | } __aligned(4); |
306b0c95 | 70 | |
da5cc7d3 JL |
71 | /* |
72 | * All 64bit fields should only be manipulated by 64bit atomic accessors. | |
73 | * All modifications to 32bit counter should be protected by zram->lock. | |
74 | */ | |
f1e3cfff | 75 | struct zram_stats { |
da5cc7d3 JL |
76 | atomic64_t compr_size; /* compressed size of pages stored */ |
77 | atomic64_t num_reads; /* failed + successful */ | |
78 | atomic64_t num_writes; /* --do-- */ | |
79 | atomic64_t failed_reads; /* should NEVER! happen */ | |
80 | atomic64_t failed_writes; /* can happen when memory is too low */ | |
81 | atomic64_t invalid_io; /* non-page-aligned I/O requests */ | |
82 | atomic64_t notify_free; /* no. of swap slot free notifications */ | |
306b0c95 NG |
83 | u32 pages_zero; /* no. of zero filled pages */ |
84 | u32 pages_stored; /* no. of pages currently stored */ | |
85 | u32 good_compress; /* % of pages with compression ratio<=50% */ | |
130f315a | 86 | u32 bad_compress; /* % of pages with compression ratio>=75% */ |
306b0c95 NG |
87 | }; |
88 | ||
8b3cc3ed | 89 | struct zram_meta { |
306b0c95 NG |
90 | void *compress_workmem; |
91 | void *compress_buffer; | |
92 | struct table *table; | |
8b3cc3ed MK |
93 | struct zs_pool *mem_pool; |
94 | }; | |
95 | ||
a0c516cb MK |
96 | struct zram_slot_free { |
97 | unsigned long index; | |
98 | struct zram_slot_free *next; | |
99 | }; | |
100 | ||
8b3cc3ed MK |
101 | struct zram { |
102 | struct zram_meta *meta; | |
57ab0485 JL |
103 | struct rw_semaphore lock; /* protect compression buffers, table, |
104 | * 32bit stat counters against concurrent | |
105 | * notifications, reads and writes */ | |
a0c516cb MK |
106 | |
107 | struct work_struct free_work; /* handle pending free request */ | |
108 | struct zram_slot_free *slot_free_rq; /* list head of free request */ | |
109 | ||
306b0c95 NG |
110 | struct request_queue *queue; |
111 | struct gendisk *disk; | |
112 | int init_done; | |
0900beae JM |
113 | /* Prevent concurrent execution of device init, reset and R/W request */ |
114 | struct rw_semaphore init_lock; | |
306b0c95 | 115 | /* |
f1e3cfff NG |
116 | * This is the limit on amount of *uncompressed* worth of data |
117 | * we can store in a disk. | |
306b0c95 | 118 | */ |
33863c21 | 119 | u64 disksize; /* bytes */ |
a0c516cb | 120 | spinlock_t slot_free_lock; |
306b0c95 | 121 | |
f1e3cfff | 122 | struct zram_stats stats; |
306b0c95 | 123 | }; |
6a907728 | 124 | #endif |