Commit | Line | Data |
---|---|---|
306b0c95 | 1 | /* |
f1e3cfff | 2 | * Compressed RAM block device |
306b0c95 | 3 | * |
1130ebba | 4 | * Copyright (C) 2008, 2009, 2010 Nitin Gupta |
306b0c95 NG |
5 | * |
6 | * This code is released using a dual license strategy: BSD/GPL | |
7 | * You can choose the licence that better fits your requirements. | |
8 | * | |
9 | * Released under the terms of 3-clause BSD License | |
10 | * Released under the terms of GNU General Public License Version 2.0 | |
11 | * | |
12 | * Project home: http://compcache.googlecode.com | |
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> | |
20 | ||
fd1a30de | 21 | #include "../zsmalloc/zsmalloc.h" |
306b0c95 NG |
22 | |
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 | ||
29 | /* | |
30 | * Stored at beginning of each compressed object. | |
31 | * | |
32 | * It stores back-reference to table entry which points to this | |
97a06382 | 33 | * object. This is required to support memory defragmentation. |
306b0c95 NG |
34 | */ |
35 | struct zobj_header { | |
36 | #if 0 | |
37 | u32 table_idx; | |
38 | #endif | |
39 | }; | |
40 | ||
41 | /*-- Configurable parameters */ | |
42 | ||
f1e3cfff | 43 | /* Default zram disk size: 25% of total RAM */ |
306b0c95 | 44 | static const unsigned default_disksize_perc_ram = 25; |
306b0c95 NG |
45 | |
46 | /* | |
306b0c95 NG |
47 | * Pages that compress to size greater than this are stored |
48 | * uncompressed in memory. | |
49 | */ | |
2ccbec05 | 50 | static const size_t max_zpage_size = PAGE_SIZE / 4 * 3; |
306b0c95 NG |
51 | |
52 | /* | |
97a06382 | 53 | * NOTE: max_zpage_size must be less than or equal to: |
fd1a30de | 54 | * ZS_MAX_ALLOC_SIZE - sizeof(struct zobj_header) |
97a06382 | 55 | * otherwise, xv_malloc() would always return failure. |
306b0c95 NG |
56 | */ |
57 | ||
58 | /*-- End of configurable params */ | |
59 | ||
60 | #define SECTOR_SHIFT 9 | |
61 | #define SECTOR_SIZE (1 << SECTOR_SHIFT) | |
62 | #define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT) | |
63 | #define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT) | |
924bd88d JM |
64 | #define ZRAM_LOGICAL_BLOCK_SHIFT 12 |
65 | #define ZRAM_LOGICAL_BLOCK_SIZE (1 << ZRAM_LOGICAL_BLOCK_SHIFT) | |
66 | #define ZRAM_SECTOR_PER_LOGICAL_BLOCK \ | |
67 | (1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT)) | |
306b0c95 | 68 | |
f1e3cfff NG |
69 | /* Flags for zram pages (table[page_no].flags) */ |
70 | enum zram_pageflags { | |
306b0c95 | 71 | /* Page is stored uncompressed */ |
f1e3cfff | 72 | ZRAM_UNCOMPRESSED, |
306b0c95 NG |
73 | |
74 | /* Page consists entirely of zeros */ | |
f1e3cfff | 75 | ZRAM_ZERO, |
306b0c95 | 76 | |
f1e3cfff | 77 | __NR_ZRAM_PAGEFLAGS, |
306b0c95 NG |
78 | }; |
79 | ||
80 | /*-- Data structures */ | |
81 | ||
f1e3cfff | 82 | /* Allocated for each disk page */ |
306b0c95 | 83 | struct table { |
fd1a30de NG |
84 | void *handle; |
85 | u16 size; /* object size (excluding header) */ | |
306b0c95 NG |
86 | u8 count; /* object ref count (not yet used) */ |
87 | u8 flags; | |
ef4ffb7a | 88 | } __attribute__((aligned(4))); |
306b0c95 | 89 | |
f1e3cfff | 90 | struct zram_stats { |
33863c21 | 91 | u64 compr_size; /* compressed size of pages stored */ |
306b0c95 NG |
92 | u64 num_reads; /* failed + successful */ |
93 | u64 num_writes; /* --do-- */ | |
ef4ffb7a | 94 | u64 failed_reads; /* should NEVER! happen */ |
95 | u64 failed_writes; /* can happen when memory is too low */ | |
f1e3cfff | 96 | u64 invalid_io; /* non-page-aligned I/O requests */ |
6a907728 | 97 | u64 notify_free; /* no. of swap slot free notifications */ |
306b0c95 NG |
98 | u32 pages_zero; /* no. of zero filled pages */ |
99 | u32 pages_stored; /* no. of pages currently stored */ | |
100 | u32 good_compress; /* % of pages with compression ratio<=50% */ | |
101 | u32 pages_expand; /* % of incompressible pages */ | |
306b0c95 NG |
102 | }; |
103 | ||
f1e3cfff | 104 | struct zram { |
fd1a30de | 105 | struct zs_pool *mem_pool; |
306b0c95 NG |
106 | void *compress_workmem; |
107 | void *compress_buffer; | |
108 | struct table *table; | |
6a907728 | 109 | spinlock_t stat64_lock; /* protect 64-bit stats */ |
c5bde238 JM |
110 | struct rw_semaphore lock; /* protect compression buffers and table |
111 | * against concurrent read and writes */ | |
306b0c95 NG |
112 | struct request_queue *queue; |
113 | struct gendisk *disk; | |
114 | int init_done; | |
0900beae JM |
115 | /* Prevent concurrent execution of device init, reset and R/W request */ |
116 | struct rw_semaphore init_lock; | |
306b0c95 | 117 | /* |
f1e3cfff NG |
118 | * This is the limit on amount of *uncompressed* worth of data |
119 | * we can store in a disk. | |
306b0c95 | 120 | */ |
33863c21 | 121 | u64 disksize; /* bytes */ |
306b0c95 | 122 | |
f1e3cfff | 123 | struct zram_stats stats; |
306b0c95 NG |
124 | }; |
125 | ||
43801f6e | 126 | extern struct zram *zram_devices; |
efd54f43 | 127 | extern unsigned int zram_num_devices; |
33863c21 NG |
128 | #ifdef CONFIG_SYSFS |
129 | extern struct attribute_group zram_disk_attr_group; | |
130 | #endif | |
131 | ||
132 | extern int zram_init_device(struct zram *zram); | |
0900beae | 133 | extern void __zram_reset_device(struct zram *zram); |
306b0c95 | 134 | |
6a907728 | 135 | #endif |