zram_drv.h 3.4 KB
Newer Older
1
/*
2
 * Compressed RAM block device
3
 *
4
 * Copyright (C) 2008, 2009, 2010  Nitin Gupta
5 6 7 8 9 10 11 12 13
 *
 * 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
 *
 */

14 15
#ifndef _ZRAM_DRV_H_
#define _ZRAM_DRV_H_
16

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

/*
 * 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 已提交
33
static const size_t max_zpage_size = PAGE_SIZE / 4 * 3;
34 35

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

/*-- 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)
47 48 49 50
#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))
51

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

57
	__NR_ZRAM_PAGEFLAGS,
58 59 60 61
};

/*-- Data structures */

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

70 71 72 73
/*
 * All 64bit fields should only be manipulated by 64bit atomic accessors.
 * All modifications to 32bit counter should be protected by zram->lock.
 */
74
struct zram_stats {
75 76 77 78 79 80 81
	atomic64_t compr_size;	/* compressed size of pages stored */
	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 */
82 83 84
	u32 pages_zero;		/* no. of zero filled pages */
	u32 pages_stored;	/* no. of pages currently stored */
	u32 good_compress;	/* % of pages with compression ratio<=50% */
85
	u32 bad_compress;	/* % of pages with compression ratio>=75% */
86 87
};

M
Minchan Kim 已提交
88
struct zram_meta {
89 90 91
	void *compress_workmem;
	void *compress_buffer;
	struct table *table;
M
Minchan Kim 已提交
92 93 94
	struct zs_pool *mem_pool;
};

95 96 97 98 99
struct zram_slot_free {
	unsigned long index;
	struct zram_slot_free *next;
};

M
Minchan Kim 已提交
100 101
struct zram {
	struct zram_meta *meta;
102 103 104
	struct rw_semaphore lock; /* protect compression buffers, table,
				   * 32bit stat counters against concurrent
				   * notifications, reads and writes */
105 106 107 108

	struct work_struct free_work;  /* handle pending free request */
	struct zram_slot_free *slot_free_rq; /* list head of free request */

109 110 111
	struct request_queue *queue;
	struct gendisk *disk;
	int init_done;
112 113
	/* Prevent concurrent execution of device init, reset and R/W request */
	struct rw_semaphore init_lock;
114
	/*
115 116
	 * This is the limit on amount of *uncompressed* worth of data
	 * we can store in a disk.
117
	 */
118
	u64 disksize;	/* bytes */
119
	spinlock_t slot_free_lock;
120

121
	struct zram_stats stats;
122
};
123
#endif