zram_drv.h 3.3 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
#include <linux/spinlock.h>
M
Minchan Kim 已提交
19
#include <linux/zsmalloc.h>
20

21 22
#include "zcomp.h"

23 24 25 26 27 28
/*-- Configurable parameters */

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

/*
32
 * NOTE: max_zpage_size must be less than or equal to:
33 34
 *   ZS_MAX_ALLOC_SIZE. Otherwise, zs_malloc() would
 * always return failure.
35 36 37 38 39 40 41
 */

/*-- End of configurable params */

#define SECTOR_SHIFT		9
#define SECTORS_PER_PAGE_SHIFT	(PAGE_SHIFT - SECTOR_SHIFT)
#define SECTORS_PER_PAGE	(1 << SECTORS_PER_PAGE_SHIFT)
42 43 44 45
#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))
46

47 48 49 50 51 52 53 54 55 56 57 58 59 60

/*
 * The lower ZRAM_FLAG_SHIFT bits of table.value is for
 * object size (excluding header), the higher bits is for
 * zram_pageflags.
 *
 * zram is mainly used for memory efficiency so we want to keep memory
 * footprint small so we can squeeze size and flags into a field.
 * The lower ZRAM_FLAG_SHIFT bits is for object size (excluding header),
 * the higher bits is for zram_pageflags.
 */
#define ZRAM_FLAG_SHIFT 24

/* Flags for zram pages (table[page_no].value) */
61
enum zram_pageflags {
62
	/* Page consists entirely of zeros */
63 64
	ZRAM_ZERO = ZRAM_FLAG_SHIFT,
	ZRAM_ACCESS,	/* page is now accessed */
65

66
	__NR_ZRAM_PAGEFLAGS,
67 68 69 70
};

/*-- Data structures */

71
/* Allocated for each disk page */
72
struct zram_table_entry {
73
	unsigned long handle;
74 75
	unsigned long value;
};
76

77
struct zram_stats {
78
	atomic64_t compr_data_size;	/* compressed size of pages stored */
79 80
	atomic64_t num_reads;	/* failed + successful */
	atomic64_t num_writes;	/* --do-- */
81
	atomic64_t failed_reads;	/* can happen when memory is too low */
82 83 84
	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 */
85 86
	atomic64_t zero_pages;		/* no. of zero filled pages */
	atomic64_t pages_stored;	/* no. of pages currently stored */
M
Minchan Kim 已提交
87
	atomic_long_t max_used_pages;	/* no. of maximum pages stored */
88
	atomic64_t writestall;		/* no. of write slow paths */
89 90
};

M
Minchan Kim 已提交
91
struct zram_meta {
92
	struct zram_table_entry *table;
M
Minchan Kim 已提交
93 94 95 96 97
	struct zs_pool *mem_pool;
};

struct zram {
	struct zram_meta *meta;
98
	struct zcomp *comp;
99
	struct gendisk *disk;
100
	/* Prevent concurrent execution of device init */
101
	struct rw_semaphore init_lock;
102
	/*
103
	 * the number of pages zram can consume for storing compressed data
104
	 */
105 106
	unsigned long limit_pages;

107
	struct zram_stats stats;
108 109 110
	atomic_t refcount; /* refcount for zram_meta */
	/* wait all IO under all of cpu are done */
	wait_queue_head_t io_done;
M
Minchan Kim 已提交
111
	/*
112 113
	 * This is the limit on amount of *uncompressed* worth of data
	 * we can store in a disk.
M
Minchan Kim 已提交
114
	 */
115
	u64 disksize;	/* bytes */
116
	char compressor[10];
117 118 119 120
	/*
	 * zram is claimed so open request will be failed
	 */
	bool claim; /* Protected by bdev->bd_mutex */
121
};
122
#endif