• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Compressed RAM block device
3  *
4  * Copyright (C) 2008, 2009, 2010  Nitin Gupta
5  *               2012, 2013 Minchan Kim
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  *
13  */
14 
15 #ifndef _ZRAM_DRV_H_
16 #define _ZRAM_DRV_H_
17 
18 #include <linux/spinlock.h>
19 #include <linux/zsmalloc.h>
20 
21 #include "zcomp.h"
22 
23 /*-- Configurable parameters */
24 
25 /*
26  * Pages that compress to size greater than this are stored
27  * uncompressed in memory.
28  */
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  *   ZS_MAX_ALLOC_SIZE. Otherwise, zs_malloc() would
34  * always return failure.
35  */
36 
37 /*-- End of configurable params */
38 
39 #define SECTORS_PER_PAGE_SHIFT	(PAGE_SHIFT - SECTOR_SHIFT)
40 #define SECTORS_PER_PAGE	(1 << SECTORS_PER_PAGE_SHIFT)
41 #define ZRAM_LOGICAL_BLOCK_SHIFT 12
42 #define ZRAM_LOGICAL_BLOCK_SIZE	(1 << ZRAM_LOGICAL_BLOCK_SHIFT)
43 #define ZRAM_SECTOR_PER_LOGICAL_BLOCK	\
44 	(1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT))
45 
46 
47 /*
48  * The lower ZRAM_FLAG_SHIFT bits of table.value is for
49  * object size (excluding header), the higher bits is for
50  * zram_pageflags.
51  *
52  * zram is mainly used for memory efficiency so we want to keep memory
53  * footprint small so we can squeeze size and flags into a field.
54  * The lower ZRAM_FLAG_SHIFT bits is for object size (excluding header),
55  * the higher bits is for zram_pageflags.
56  */
57 #define ZRAM_FLAG_SHIFT 24
58 
59 /* Flags for zram pages (table[page_no].value) */
60 enum zram_pageflags {
61 	/* Page consists entirely of zeros */
62 	ZRAM_ZERO = ZRAM_FLAG_SHIFT,
63 	ZRAM_ACCESS,	/* page is now accessed */
64 
65 	__NR_ZRAM_PAGEFLAGS,
66 };
67 
68 /*-- Data structures */
69 
70 /* Allocated for each disk page */
71 struct zram_table_entry {
72 	unsigned long handle;
73 	unsigned long value;
74 };
75 
76 struct zram_stats {
77 	atomic64_t compr_data_size;	/* compressed size of pages stored */
78 	atomic64_t num_reads;	/* failed + successful */
79 	atomic64_t num_writes;	/* --do-- */
80 	atomic64_t failed_reads;	/* can happen when memory is too low */
81 	atomic64_t failed_writes;	/* can happen when memory is too low */
82 	atomic64_t invalid_io;	/* non-page-aligned I/O requests */
83 	atomic64_t notify_free;	/* no. of swap slot free notifications */
84 	atomic64_t zero_pages;		/* no. of zero filled pages */
85 	atomic64_t pages_stored;	/* no. of pages currently stored */
86 	atomic_long_t max_used_pages;	/* no. of maximum pages stored */
87 };
88 
89 struct zram_meta {
90 	struct zram_table_entry *table;
91 	struct zs_pool *mem_pool;
92 };
93 
94 struct zram {
95 	struct zram_meta *meta;
96 	struct zcomp *comp;
97 	struct gendisk *disk;
98 	/* Prevent concurrent execution of device init */
99 	struct rw_semaphore init_lock;
100 	/*
101 	 * the number of pages zram can consume for storing compressed data
102 	 */
103 	unsigned long limit_pages;
104 	int max_comp_streams;
105 
106 	struct zram_stats stats;
107 	atomic_t refcount; /* refcount for zram_meta */
108 	/* wait all IO under all of cpu are done */
109 	wait_queue_head_t io_done;
110 	/*
111 	 * This is the limit on amount of *uncompressed* worth of data
112 	 * we can store in a disk.
113 	 */
114 	u64 disksize;	/* bytes */
115 	char compressor[10];
116 	/*
117 	 * zram is claimed so open request will be failed
118 	 */
119 	bool claim; /* Protected by bdev->bd_mutex */
120 };
121 #endif
122