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 | ||
306b0c95 NG |
29 | /*-- Configurable parameters */ |
30 | ||
f1e3cfff | 31 | /* Default zram disk size: 25% of total RAM */ |
306b0c95 | 32 | static const unsigned default_disksize_perc_ram = 25; |
306b0c95 NG |
33 | |
34 | /* | |
306b0c95 NG |
35 | * Pages that compress to size greater than this are stored |
36 | * uncompressed in memory. | |
37 | */ | |
2ccbec05 | 38 | static const size_t max_zpage_size = PAGE_SIZE / 4 * 3; |
306b0c95 NG |
39 | |
40 | /* | |
97a06382 | 41 | * NOTE: max_zpage_size must be less than or equal to: |
55dcbbb1 MK |
42 | * ZS_MAX_ALLOC_SIZE. Otherwise, zs_malloc() would |
43 | * always return failure. | |
306b0c95 NG |
44 | */ |
45 | ||
46 | /*-- End of configurable params */ | |
47 | ||
48 | #define SECTOR_SHIFT 9 | |
49 | #define SECTOR_SIZE (1 << SECTOR_SHIFT) | |
50 | #define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT) | |
51 | #define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT) | |
924bd88d JM |
52 | #define ZRAM_LOGICAL_BLOCK_SHIFT 12 |
53 | #define ZRAM_LOGICAL_BLOCK_SIZE (1 << ZRAM_LOGICAL_BLOCK_SHIFT) | |
54 | #define ZRAM_SECTOR_PER_LOGICAL_BLOCK \ | |
55 | (1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT)) | |
306b0c95 | 56 | |
f1e3cfff NG |
57 | /* Flags for zram pages (table[page_no].flags) */ |
58 | enum zram_pageflags { | |
306b0c95 | 59 | /* Page consists entirely of zeros */ |
f1e3cfff | 60 | ZRAM_ZERO, |
306b0c95 | 61 | |
f1e3cfff | 62 | __NR_ZRAM_PAGEFLAGS, |
306b0c95 NG |
63 | }; |
64 | ||
65 | /*-- Data structures */ | |
66 | ||
f1e3cfff | 67 | /* Allocated for each disk page */ |
306b0c95 | 68 | struct table { |
c2344348 | 69 | unsigned long handle; |
fd1a30de | 70 | u16 size; /* object size (excluding header) */ |
306b0c95 NG |
71 | u8 count; /* object ref count (not yet used) */ |
72 | u8 flags; | |
80677c25 | 73 | } __aligned(4); |
306b0c95 | 74 | |
f1e3cfff | 75 | struct zram_stats { |
33863c21 | 76 | u64 compr_size; /* compressed size of pages stored */ |
306b0c95 NG |
77 | u64 num_reads; /* failed + successful */ |
78 | u64 num_writes; /* --do-- */ | |
ef4ffb7a | 79 | u64 failed_reads; /* should NEVER! happen */ |
80 | u64 failed_writes; /* can happen when memory is too low */ | |
f1e3cfff | 81 | u64 invalid_io; /* non-page-aligned I/O requests */ |
6a907728 | 82 | u64 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 | ||
f1e3cfff | 89 | struct zram { |
fd1a30de | 90 | struct zs_pool *mem_pool; |
306b0c95 NG |
91 | void *compress_workmem; |
92 | void *compress_buffer; | |
93 | struct table *table; | |
6a907728 | 94 | spinlock_t stat64_lock; /* protect 64-bit stats */ |
c5bde238 JM |
95 | struct rw_semaphore lock; /* protect compression buffers and table |
96 | * against concurrent read and writes */ | |
306b0c95 NG |
97 | struct request_queue *queue; |
98 | struct gendisk *disk; | |
99 | int init_done; | |
0900beae JM |
100 | /* Prevent concurrent execution of device init, reset and R/W request */ |
101 | struct rw_semaphore init_lock; | |
306b0c95 | 102 | /* |
f1e3cfff NG |
103 | * This is the limit on amount of *uncompressed* worth of data |
104 | * we can store in a disk. | |
306b0c95 | 105 | */ |
33863c21 | 106 | u64 disksize; /* bytes */ |
306b0c95 | 107 | |
f1e3cfff | 108 | struct zram_stats stats; |
306b0c95 NG |
109 | }; |
110 | ||
43801f6e | 111 | extern struct zram *zram_devices; |
5fa5a901 | 112 | unsigned int zram_get_num_devices(void); |
33863c21 NG |
113 | #ifdef CONFIG_SYSFS |
114 | extern struct attribute_group zram_disk_attr_group; | |
115 | #endif | |
116 | ||
117 | extern int zram_init_device(struct zram *zram); | |
0900beae | 118 | extern void __zram_reset_device(struct zram *zram); |
306b0c95 | 119 | |
6a907728 | 120 | #endif |