2 * Compressed RAM based swap 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
15 #ifndef _RAMZSWAP_DRV_H_
16 #define _RAMZSWAP_DRV_H_
18 #include <linux/spinlock.h>
19 #include <linux/mutex.h>
21 #include "ramzswap_ioctl.h"
25 * Some arbitrary value. This is just to catch
26 * invalid value for num_devices module parameter.
28 static const unsigned max_num_devices = 32;
31 * Stored at beginning of each compressed object.
33 * It stores back-reference to table entry which points to this
34 * object. This is required to support memory defragmentation or
35 * migrating compressed pages to backing swap disk.
43 /*-- Configurable parameters */
45 /* Default ramzswap disk size: 25% of total RAM */
46 static const unsigned default_disksize_perc_ram = 25;
47 static const unsigned default_memlimit_perc_ram = 15;
50 * Max compressed page size when backing device is provided.
51 * Pages that compress to size greater than this are sent to
54 static const unsigned max_zpage_size_bdev = PAGE_SIZE / 2;
57 * Max compressed page size when there is no backing dev.
58 * Pages that compress to size greater than this are stored
59 * uncompressed in memory.
61 static const unsigned max_zpage_size_nobdev = PAGE_SIZE / 4 * 3;
64 * NOTE: max_zpage_size_{bdev,nobdev} sizes must be
65 * less than or equal to:
66 * XV_MAX_ALLOC_SIZE - sizeof(struct zobj_header)
67 * since otherwise xv_malloc would always return failure.
70 /*-- End of configurable params */
72 #define SECTOR_SHIFT 9
73 #define SECTOR_SIZE (1 << SECTOR_SHIFT)
74 #define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
75 #define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT)
77 /* Flags for ramzswap pages (table[page_no].flags) */
79 /* Page is stored uncompressed */
82 /* Page consists entirely of zeros */
88 /*-- Data structures */
91 * Allocated for each swap slot, indexed by page no.
92 * These table entries must fit exactly in a page.
97 u8 count; /* object ref count (not yet used) */
99 } __attribute__((aligned(4)));
102 * Swap extent information in case backing swap is a regular
103 * file. These extent entries must fit exactly in a page.
105 struct ramzswap_backing_extent {
108 } __attribute__((aligned(4)));
110 struct ramzswap_stats {
112 size_t compr_size; /* compressed size of pages stored -
113 * needed to enforce memlimit */
115 #if defined(CONFIG_RAMZSWAP_STATS)
116 u64 num_reads; /* failed + successful */
117 u64 num_writes; /* --do-- */
118 u64 failed_reads; /* should NEVER! happen */
119 u64 failed_writes; /* can happen when memory is too low */
120 u64 invalid_io; /* non-swap I/O requests */
121 u64 notify_free; /* no. of swap slot free notifications */
122 u32 pages_zero; /* no. of zero filled pages */
123 u32 pages_stored; /* no. of pages currently stored */
124 u32 good_compress; /* % of pages with compression ratio<=50% */
125 u32 pages_expand; /* % of incompressible pages */
126 u64 bdev_num_reads; /* no. of reads on backing dev */
127 u64 bdev_num_writes; /* no. of writes on backing dev */
132 struct xv_pool *mem_pool;
133 void *compress_workmem;
134 void *compress_buffer;
136 spinlock_t stat64_lock; /* protect 64-bit stats */
138 struct request_queue *queue;
139 struct gendisk *disk;
142 * This is limit on compressed data size (stats.compr_size)
143 * Its applicable only when backing swap device is present.
145 size_t memlimit; /* bytes */
147 * This is limit on amount of *uncompressed* worth of data
148 * we can hold. When backing swap device is provided, it is
149 * set equal to device size.
151 size_t disksize; /* bytes */
153 struct ramzswap_stats stats;
155 /* backing swap device info */
156 struct ramzswap_backing_extent *curr_extent;
157 struct list_head backing_swap_extent_list;
158 unsigned long num_extents;
159 char backing_swap_name[MAX_SWAP_NAME_LEN];
160 struct block_device *backing_swap;
161 struct file *swap_file;
166 /* Debugging and Stats */
167 #if defined(CONFIG_RAMZSWAP_STATS)
168 static void rzs_stat_inc(u32 *v)
173 static void rzs_stat_dec(u32 *v)
178 static void rzs_stat64_inc(struct ramzswap *rzs, u64 *v)
180 spin_lock(&rzs->stat64_lock);
182 spin_unlock(&rzs->stat64_lock);
185 static void rzs_stat64_dec(struct ramzswap *rzs, u64 *v)
187 spin_lock(&rzs->stat64_lock);
189 spin_unlock(&rzs->stat64_lock);
192 static u64 rzs_stat64_read(struct ramzswap *rzs, u64 *v)
196 spin_lock(&rzs->stat64_lock);
198 spin_unlock(&rzs->stat64_lock);
203 #define rzs_stat_inc(v)
204 #define rzs_stat_dec(v)
205 #define rzs_stat64_inc(r, v)
206 #define rzs_stat64_dec(r, v)
207 #define rzs_stat64_read(r, v)
208 #endif /* CONFIG_RAMZSWAP_STATS */