Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[linux-2.6-block.git] / drivers / block / zram / zram_drv.h
CommitLineData
306b0c95 1/*
f1e3cfff 2 * Compressed RAM block device
306b0c95 3 *
1130ebba 4 * Copyright (C) 2008, 2009, 2010 Nitin Gupta
7bfb3de8 5 * 2012, 2013 Minchan Kim
306b0c95
NG
6 *
7 * This code is released using a dual license strategy: BSD/GPL
8 * You can choose the licence that better fits your requirements.
9 *
10 * Released under the terms of 3-clause BSD License
11 * Released under the terms of GNU General Public License Version 2.0
12 *
306b0c95
NG
13 */
14
f1e3cfff
NG
15#ifndef _ZRAM_DRV_H_
16#define _ZRAM_DRV_H_
306b0c95 17
415403be 18#include <linux/rwsem.h>
bcf1647d 19#include <linux/zsmalloc.h>
415403be 20#include <linux/crypto.h>
306b0c95 21
b7ca232e
SS
22#include "zcomp.h"
23
306b0c95
NG
24#define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
25#define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT)
924bd88d
JM
26#define ZRAM_LOGICAL_BLOCK_SHIFT 12
27#define ZRAM_LOGICAL_BLOCK_SIZE (1 << ZRAM_LOGICAL_BLOCK_SHIFT)
28#define ZRAM_SECTOR_PER_LOGICAL_BLOCK \
29 (1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT))
306b0c95 30
d2d5e762
WY
31
32/*
f635725c
SS
33 * ZRAM is mainly used for memory efficiency so we want to keep memory
34 * footprint small and thus squeeze size and zram pageflags into a flags
35 * member. The lower ZRAM_FLAG_SHIFT bits is for object size (excluding
36 * header), which cannot be larger than PAGE_SIZE (requiring PAGE_SHIFT
37 * bits), the higher bits are for zram_pageflags.
d2d5e762 38 *
f635725c 39 * We use BUILD_BUG_ON() to make sure that zram pageflags don't overflow.
d2d5e762 40 */
f635725c 41#define ZRAM_FLAG_SHIFT (PAGE_SHIFT + 1)
d2d5e762 42
84b33bf7
SS
43/* Only 2 bits are allowed for comp priority index */
44#define ZRAM_COMP_PRIORITY_MASK 0x3
45
7e529283 46/* Flags for zram pages (table[page_no].flags) */
f1e3cfff 47enum zram_pageflags {
6086aeb4 48 ZRAM_SAME = ZRAM_FLAG_SHIFT, /* Page consists the same element */
db8ffbd4 49 ZRAM_WB, /* page is stored on backing_device */
a939888e 50 ZRAM_UNDER_WB, /* page is under writeback */
89e85bce 51 ZRAM_HUGE, /* Incompressible page */
e82592c4 52 ZRAM_IDLE, /* not accessed page since last idle marking */
84b33bf7
SS
53 ZRAM_INCOMPRESSIBLE, /* none of the algorithms could compress it */
54
55 ZRAM_COMP_PRIORITY_BIT1, /* First bit of comp priority index */
56 ZRAM_COMP_PRIORITY_BIT2, /* Second bit of comp priority index */
306b0c95 57
f1e3cfff 58 __NR_ZRAM_PAGEFLAGS,
306b0c95
NG
59};
60
61/*-- Data structures */
62
f1e3cfff 63/* Allocated for each disk page */
cb8f2eec 64struct zram_table_entry {
8e19d540 65 union {
66 unsigned long handle;
67 unsigned long element;
68 };
68d20eb6 69 unsigned int flags;
9518e5bf 70 spinlock_t lock;
a7a03505 71#ifdef CONFIG_ZRAM_TRACK_ENTRY_ACTIME
c0265342
MK
72 ktime_t ac_time;
73#endif
d2d5e762 74};
306b0c95 75
f1e3cfff 76struct zram_stats {
90a7806e 77 atomic64_t compr_data_size; /* compressed size of pages stored */
0cf1e9d6 78 atomic64_t failed_reads; /* can happen when memory is too low */
da5cc7d3 79 atomic64_t failed_writes; /* can happen when memory is too low */
da5cc7d3 80 atomic64_t notify_free; /* no. of swap slot free notifications */
8e19d540 81 atomic64_t same_pages; /* no. of same element filled pages */
89e85bce 82 atomic64_t huge_pages; /* no. of huge pages */
194e28da 83 atomic64_t huge_pages_since; /* no. of huge pages since zram set up */
90a7806e 84 atomic64_t pages_stored; /* no. of pages currently stored */
461a8eee 85 atomic_long_t max_used_pages; /* no. of maximum pages stored */
37887783 86 atomic64_t writestall; /* no. of write slow paths */
3c9959e0 87 atomic64_t miss_free; /* no. of missed free */
23eddf39
MK
88#ifdef CONFIG_ZRAM_WRITEBACK
89 atomic64_t bd_count; /* no. of pages in backing device */
90 atomic64_t bd_reads; /* no. of reads from backing device */
91 atomic64_t bd_writes; /* no. of writes from backing device */
92#endif
306b0c95
NG
93};
94
7ac07a26
SS
95#ifdef CONFIG_ZRAM_MULTI_COMP
96#define ZRAM_PRIMARY_COMP 0U
97#define ZRAM_SECONDARY_COMP 1U
98#define ZRAM_MAX_COMPS 4U
99#else
100#define ZRAM_PRIMARY_COMP 0U
101#define ZRAM_SECONDARY_COMP 0U
102#define ZRAM_MAX_COMPS 1U
103#endif
104
beb6602c 105struct zram {
cb8f2eec 106 struct zram_table_entry *table;
8b3cc3ed 107 struct zs_pool *mem_pool;
7ac07a26 108 struct zcomp *comps[ZRAM_MAX_COMPS];
f2bac7ad 109 struct zcomp_params params[ZRAM_MAX_COMPS];
306b0c95 110 struct gendisk *disk;
08eee69f 111 /* Prevent concurrent execution of device init */
0900beae 112 struct rw_semaphore init_lock;
306b0c95 113 /*
08eee69f 114 * the number of pages zram can consume for storing compressed data
306b0c95 115 */
08eee69f 116 unsigned long limit_pages;
08eee69f 117
f1e3cfff 118 struct zram_stats stats;
9ada9da9 119 /*
08eee69f
MK
120 * This is the limit on amount of *uncompressed* worth of data
121 * we can store in a disk.
9ada9da9 122 */
08eee69f 123 u64 disksize; /* bytes */
7ac07a26 124 const char *comp_algs[ZRAM_MAX_COMPS];
a55cf964 125 s8 num_active_comps;
f405c445
SS
126 /*
127 * zram is claimed so open request will be failed
128 */
a8698707 129 bool claim; /* Protected by disk->open_mutex */
7e529283 130#ifdef CONFIG_ZRAM_WRITEBACK
dd794835 131 struct file *backing_dev;
1d69a3f8
MK
132 spinlock_t wb_limit_lock;
133 bool wb_limit_enable;
134 u64 bd_wb_limit;
ebb0173d 135 struct block_device *bdev;
1363d466
MK
136 unsigned long *bitmap;
137 unsigned long nr_pages;
013bf95a 138#endif
c0265342
MK
139#ifdef CONFIG_ZRAM_MEMORY_TRACKING
140 struct dentry *debugfs_dir;
141#endif
306b0c95 142};
6a907728 143#endif