]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/ramzswap/ramzswap_drv.h
Staging: virtual block device driver (ramzswap)
[karo-tx-linux.git] / drivers / staging / ramzswap / ramzswap_drv.h
1 /*
2  * Compressed RAM based swap device
3  *
4  * Copyright (C) 2008, 2009  Nitin Gupta
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
15 #ifndef _RAMZSWAP_DRV_H_
16 #define _RAMZSWAP_DRV_H_
17
18 #include "ramzswap_ioctl.h"
19 #include "xvmalloc.h"
20
21 /*
22  * Some arbitrary value. This is just to catch
23  * invalid value for num_devices module parameter.
24  */
25 static const unsigned max_num_devices = 32;
26
27 /*
28  * Stored at beginning of each compressed object.
29  *
30  * It stores back-reference to table entry which points to this
31  * object. This is required to support memory defragmentation or
32  * migrating compressed pages to backing swap disk.
33  */
34 struct zobj_header {
35 #if 0
36         u32 table_idx;
37 #endif
38 };
39
40 /*-- Configurable parameters */
41
42 /* Default ramzswap disk size: 25% of total RAM */
43 static const unsigned default_disksize_perc_ram = 25;
44 static const unsigned default_memlimit_perc_ram = 15;
45
46 /*
47  * Max compressed page size when backing device is provided.
48  * Pages that compress to size greater than this are sent to
49  * physical swap disk.
50  */
51 static const unsigned max_zpage_size_bdev = PAGE_SIZE / 2;
52
53 /*
54  * Max compressed page size when there is no backing dev.
55  * Pages that compress to size greater than this are stored
56  * uncompressed in memory.
57  */
58 static const unsigned max_zpage_size_nobdev = PAGE_SIZE / 4 * 3;
59
60 /*
61  * NOTE: max_zpage_size_{bdev,nobdev} sizes must be
62  * less than or equal to:
63  *   XV_MAX_ALLOC_SIZE - sizeof(struct zobj_header)
64  * since otherwise xv_malloc would always return failure.
65  */
66
67 /*-- End of configurable params */
68
69 #define SECTOR_SHIFT            9
70 #define SECTOR_SIZE             (1 << SECTOR_SHIFT)
71 #define SECTORS_PER_PAGE_SHIFT  (PAGE_SHIFT - SECTOR_SHIFT)
72 #define SECTORS_PER_PAGE        (1 << SECTORS_PER_PAGE_SHIFT)
73
74 /* Debugging and Stats */
75 #if defined(CONFIG_RAMZSWAP_STATS)
76 #define stat_inc(stat)  ((stat)++)
77 #define stat_dec(stat)  ((stat)--)
78 #else
79 #define stat_inc(x)
80 #define stat_dec(x)
81 #endif
82
83 /* Flags for ramzswap pages (table[page_no].flags) */
84 enum rzs_pageflags {
85         /* Page is stored uncompressed */
86         RZS_UNCOMPRESSED,
87
88         /* Page consists entirely of zeros */
89         RZS_ZERO,
90
91         __NR_RZS_PAGEFLAGS,
92 };
93
94 /*-- Data structures */
95
96 /*
97  * Allocated for each swap slot, indexed by page no.
98  * These table entries must fit exactly in a page.
99  */
100 struct table {
101         struct page *page;
102         u16 offset;
103         u8 count;       /* object ref count (not yet used) */
104         u8 flags;
105 } __attribute__((aligned(4)));;
106
107 /*
108  * Swap extent information in case backing swap is a regular
109  * file. These extent entries must fit exactly in a page.
110  */
111 struct ramzswap_backing_extent {
112         pgoff_t phy_pagenum;
113         pgoff_t num_pages;
114 } __attribute__((aligned(4)));
115
116 struct ramzswap_stats {
117         /* basic stats */
118         size_t compr_size;      /* compressed size of pages stored -
119                                  * needed to enforce memlimit */
120         /* more stats */
121 #if defined(CONFIG_RAMZSWAP_STATS)
122         u64 num_reads;          /* failed + successful */
123         u64 num_writes;         /* --do-- */
124         u64 failed_reads;       /* can happen when memory is too low */
125         u64 failed_writes;      /* should NEVER! happen */
126         u64 invalid_io;         /* non-swap I/O requests */
127         u32 pages_zero;         /* no. of zero filled pages */
128         u32 pages_stored;       /* no. of pages currently stored */
129         u32 good_compress;      /* % of pages with compression ratio<=50% */
130         u32 pages_expand;       /* % of incompressible pages */
131         u64 bdev_num_reads;     /* no. of reads on backing dev */
132         u64 bdev_num_writes;    /* no. of writes on backing dev */
133 #endif
134 };
135
136 struct ramzswap {
137         struct xv_pool *mem_pool;
138         void *compress_workmem;
139         void *compress_buffer;
140         struct table *table;
141         struct mutex lock;
142         struct request_queue *queue;
143         struct gendisk *disk;
144         int init_done;
145         /*
146          * This is limit on compressed data size (stats.compr_size)
147          * Its applicable only when backing swap device is present.
148          */
149         size_t memlimit;        /* bytes */
150         /*
151          * This is limit on amount of *uncompressed* worth of data
152          * we can hold. When backing swap device is provided, it is
153          * set equal to device size.
154          */
155         size_t disksize;        /* bytes */
156
157         struct ramzswap_stats stats;
158
159         /* backing swap device info */
160         struct ramzswap_backing_extent *curr_extent;
161         struct list_head backing_swap_extent_list;
162         unsigned long num_extents;
163         char backing_swap_name[MAX_SWAP_NAME_LEN];
164         struct block_device *backing_swap;
165         struct file *swap_file;
166 };
167
168 /*-- */
169
170 #endif
171