zram_drv.h 2.8 KB
Newer Older
1
/*
2
 * Compressed RAM block device
3
 *
4
 * Copyright (C) 2008, 2009, 2010  Nitin Gupta
M
Minchan Kim 已提交
5
 *               2012, 2013 Minchan Kim
6 7 8 9 10 11 12 13 14
 *
 * This code is released using a dual license strategy: BSD/GPL
 * You can choose the licence that better fits your requirements.
 *
 * Released under the terms of 3-clause BSD License
 * Released under the terms of GNU General Public License Version 2.0
 *
 */

15 16
#ifndef _ZRAM_DRV_H_
#define _ZRAM_DRV_H_
17

18 19
#include <linux/spinlock.h>
#include <linux/mutex.h>
M
Minchan Kim 已提交
20
#include <linux/zsmalloc.h>
21 22 23 24 25 26 27 28 29 30 31 32 33

/*
 * Some arbitrary value. This is just to catch
 * invalid value for num_devices module parameter.
 */
static const unsigned max_num_devices = 32;

/*-- Configurable parameters */

/*
 * Pages that compress to size greater than this are stored
 * uncompressed in memory.
 */
N
Nitin Gupta 已提交
34
static const size_t max_zpage_size = PAGE_SIZE / 4 * 3;
35 36

/*
37
 * NOTE: max_zpage_size must be less than or equal to:
38 39
 *   ZS_MAX_ALLOC_SIZE. Otherwise, zs_malloc() would
 * always return failure.
40 41 42 43 44 45 46 47
 */

/*-- End of configurable params */

#define SECTOR_SHIFT		9
#define SECTOR_SIZE		(1 << SECTOR_SHIFT)
#define SECTORS_PER_PAGE_SHIFT	(PAGE_SHIFT - SECTOR_SHIFT)
#define SECTORS_PER_PAGE	(1 << SECTORS_PER_PAGE_SHIFT)
48 49 50 51
#define ZRAM_LOGICAL_BLOCK_SHIFT 12
#define ZRAM_LOGICAL_BLOCK_SIZE	(1 << ZRAM_LOGICAL_BLOCK_SHIFT)
#define ZRAM_SECTOR_PER_LOGICAL_BLOCK	\
	(1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT))
52

53 54
/* Flags for zram pages (table[page_no].flags) */
enum zram_pageflags {
55
	/* Page consists entirely of zeros */
56
	ZRAM_ZERO,
57

58
	__NR_ZRAM_PAGEFLAGS,
59 60 61 62
};

/*-- Data structures */

63
/* Allocated for each disk page */
64
struct table {
65
	unsigned long handle;
66
	u16 size;	/* object size (excluding header) */
67 68
	u8 count;	/* object ref count (not yet used) */
	u8 flags;
69
} __aligned(4);
70

71
struct zram_stats {
72
	atomic64_t compr_data_size;	/* compressed size of pages stored */
73 74 75 76 77 78
	atomic64_t num_reads;	/* failed + successful */
	atomic64_t num_writes;	/* --do-- */
	atomic64_t failed_reads;	/* should NEVER! happen */
	atomic64_t failed_writes;	/* can happen when memory is too low */
	atomic64_t invalid_io;	/* non-page-aligned I/O requests */
	atomic64_t notify_free;	/* no. of swap slot free notifications */
79 80
	atomic64_t zero_pages;		/* no. of zero filled pages */
	atomic64_t pages_stored;	/* no. of pages currently stored */
81 82
};

M
Minchan Kim 已提交
83
struct zram_meta {
M
Minchan Kim 已提交
84
	rwlock_t tb_lock;	/* protect table */
85 86 87
	void *compress_workmem;
	void *compress_buffer;
	struct table *table;
M
Minchan Kim 已提交
88
	struct zs_pool *mem_pool;
89
	struct mutex buffer_lock; /* protect compress buffers */
M
Minchan Kim 已提交
90 91 92 93
};

struct zram {
	struct zram_meta *meta;
94 95
	struct request_queue *queue;
	struct gendisk *disk;
96 97
	/* Prevent concurrent execution of device init, reset and R/W request */
	struct rw_semaphore init_lock;
98
	/*
99 100
	 * This is the limit on amount of *uncompressed* worth of data
	 * we can store in a disk.
101
	 */
102
	u64 disksize;	/* bytes */
103

104
	struct zram_stats stats;
105
};
106
#endif