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 */
32 * Pages that compress to size greater than this are stored
33 * uncompressed in memory.
35 static const size_t max_zpage_size = PAGE_SIZE / 4 * 3;
38 * NOTE: max_zpage_size must be less than or equal to:
39 * ZS_MAX_ALLOC_SIZE. Otherwise, zs_malloc() would
40 * always return failure.
43 /*-- End of configurable params */
45 #define SECTOR_SHIFT 9
46 #define SECTOR_SIZE (1 << SECTOR_SHIFT)
47 #define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
48 #define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT)
49 #define ZRAM_LOGICAL_BLOCK_SHIFT 12
50 #define ZRAM_LOGICAL_BLOCK_SIZE (1 << ZRAM_LOGICAL_BLOCK_SHIFT)
51 #define ZRAM_SECTOR_PER_LOGICAL_BLOCK \
52 (1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT))
54 /* Flags for zram pages (table[page_no].flags) */
56 /* Page consists entirely of zeros */
62 /*-- Data structures */
64 /* Allocated for each disk page */
67 u16 size; /* object size (excluding header) */
68 u8 count; /* object ref count (not yet used) */
73 u64 compr_size; /* compressed size of pages stored */
74 u64 num_reads; /* failed + successful */
75 u64 num_writes; /* --do-- */
76 u64 failed_reads; /* should NEVER! happen */
77 u64 failed_writes; /* can happen when memory is too low */
78 u64 invalid_io; /* non-page-aligned I/O requests */
79 u64 notify_free; /* no. of swap slot free notifications */
80 u32 pages_zero; /* no. of zero filled pages */
81 u32 pages_stored; /* no. of pages currently stored */
82 u32 good_compress; /* % of pages with compression ratio<=50% */
83 u32 bad_compress; /* % of pages with compression ratio>=75% */
87 void *compress_workmem;
88 void *compress_buffer;
90 struct zs_pool *mem_pool;
94 struct zram_meta *meta;
95 spinlock_t stat64_lock; /* protect 64-bit stats */
96 struct rw_semaphore lock; /* protect compression buffers, table,
97 * 32bit stat counters against concurrent
98 * notifications, reads and writes */
99 struct request_queue *queue;
100 struct gendisk *disk;
102 /* Prevent concurrent execution of device init, reset and R/W request */
103 struct rw_semaphore init_lock;
105 * This is the limit on amount of *uncompressed* worth of data
106 * we can store in a disk.
108 u64 disksize; /* bytes */
110 struct zram_stats stats;
113 extern struct zram *zram_devices;
114 unsigned int zram_get_num_devices(void);
116 extern struct attribute_group zram_disk_attr_group;
119 extern void zram_reset_device(struct zram *zram);
120 extern struct zram_meta *zram_meta_alloc(u64 disksize);
121 extern void zram_meta_free(struct zram_meta *meta);
122 extern void zram_init_device(struct zram *zram, struct zram_meta *meta);