2 * Compressed RAM block device
4 * Copyright (C) 2008, 2009, 2010 Nitin Gupta
6 * This code is released using a dual license strategy: BSD/GPL
7 * You can choose the licence that better fits your requirements.
9 * Released under the terms of 3-clause BSD License
10 * Released under the terms of GNU General Public License Version 2.0
12 * Project home: http://compcache.googlecode.com
18 #include <linux/spinlock.h>
19 #include <linux/mutex.h>
21 #include "../zsmalloc/zsmalloc.h"
24 * Some arbitrary value. This is just to catch
25 * invalid value for num_devices module parameter.
27 static const unsigned max_num_devices
= 32;
29 /*-- Configurable parameters */
31 /* Default zram disk size: 25% of total RAM */
32 static const unsigned default_disksize_perc_ram
= 25;
35 * Pages that compress to size greater than this are stored
36 * uncompressed in memory.
38 static const size_t max_zpage_size
= PAGE_SIZE
/ 4 * 3;
41 * NOTE: max_zpage_size must be less than or equal to:
42 * ZS_MAX_ALLOC_SIZE - sizeof(struct zobj_header)
43 * otherwise, xv_malloc() would always return failure.
46 /*-- End of configurable params */
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)
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))
57 /* Flags for zram pages (table[page_no].flags) */
59 /* Page consists entirely of zeros */
65 /*-- Data structures */
67 /* Allocated for each disk page */
70 u16 size
; /* object size (excluding header) */
71 u8 count
; /* object ref count (not yet used) */
76 u64 compr_size
; /* compressed size of pages stored */
77 u64 num_reads
; /* failed + successful */
78 u64 num_writes
; /* --do-- */
79 u64 failed_reads
; /* should NEVER! happen */
80 u64 failed_writes
; /* can happen when memory is too low */
81 u64 invalid_io
; /* non-page-aligned I/O requests */
82 u64 notify_free
; /* no. of swap slot free notifications */
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% */
86 u32 bad_compress
; /* % of pages with compression ratio>=75% */
90 struct zs_pool
*mem_pool
;
91 void *compress_workmem
;
92 void *compress_buffer
;
94 spinlock_t stat64_lock
; /* protect 64-bit stats */
95 struct rw_semaphore lock
; /* protect compression buffers and table
96 * against concurrent read and writes */
97 struct request_queue
*queue
;
100 /* Prevent concurrent execution of device init, reset and R/W request */
101 struct rw_semaphore init_lock
;
103 * This is the limit on amount of *uncompressed* worth of data
104 * we can store in a disk.
106 u64 disksize
; /* bytes */
108 struct zram_stats stats
;
111 extern struct zram
*zram_devices
;
112 unsigned int zram_get_num_devices(void);
114 extern struct attribute_group zram_disk_attr_group
;
117 extern int zram_init_device(struct zram
*zram
);
118 extern void __zram_reset_device(struct zram
*zram
);