blob: d542eee81357582cf95bd4e296bb1610c2a7d7f8 [file] [log] [blame]
Nitin Gupta306b0c92009-09-22 10:26:53 +05301/*
Nitin Guptaf1e3cff2010-06-01 13:31:25 +05302 * Compressed RAM block device
Nitin Gupta306b0c92009-09-22 10:26:53 +05303 *
Nitin Gupta1130ebb2010-01-28 21:21:35 +05304 * Copyright (C) 2008, 2009, 2010 Nitin Gupta
Nitin Gupta306b0c92009-09-22 10:26:53 +05305 *
6 * This code is released using a dual license strategy: BSD/GPL
7 * You can choose the licence that better fits your requirements.
8 *
9 * Released under the terms of 3-clause BSD License
10 * Released under the terms of GNU General Public License Version 2.0
11 *
12 * Project home: http://compcache.googlecode.com
13 */
14
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053015#ifndef _ZRAM_DRV_H_
16#define _ZRAM_DRV_H_
Nitin Gupta306b0c92009-09-22 10:26:53 +053017
Nitin Gupta6a907722010-01-28 21:13:37 +053018#include <linux/spinlock.h>
19#include <linux/mutex.h>
20
Nitin Guptafd1a30d2012-01-09 16:51:59 -060021#include "../zsmalloc/zsmalloc.h"
Nitin Gupta306b0c92009-09-22 10:26:53 +053022
23/*
24 * Some arbitrary value. This is just to catch
25 * invalid value for num_devices module parameter.
26 */
27static const unsigned max_num_devices = 32;
28
Nitin Gupta306b0c92009-09-22 10:26:53 +053029/*-- Configurable parameters */
30
Nitin Gupta306b0c92009-09-22 10:26:53 +053031/*
Nitin Gupta306b0c92009-09-22 10:26:53 +053032 * Pages that compress to size greater than this are stored
33 * uncompressed in memory.
34 */
Nitin Gupta2ccbec02011-09-09 19:01:00 -040035static const size_t max_zpage_size = PAGE_SIZE / 4 * 3;
Nitin Gupta306b0c92009-09-22 10:26:53 +053036
37/*
Nitin Gupta97a06382010-05-13 14:24:21 +053038 * NOTE: max_zpage_size must be less than or equal to:
Minchan Kim55dcbbb2012-10-10 08:49:52 +090039 * ZS_MAX_ALLOC_SIZE. Otherwise, zs_malloc() would
40 * always return failure.
Nitin Gupta306b0c92009-09-22 10:26:53 +053041 */
42
43/*-- End of configurable params */
44
45#define SECTOR_SHIFT 9
46#define SECTOR_SIZE (1 << SECTOR_SHIFT)
47#define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
48#define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT)
Jerome Marchand924bd882011-06-10 15:28:48 +020049#define ZRAM_LOGICAL_BLOCK_SHIFT 12
50#define ZRAM_LOGICAL_BLOCK_SIZE (1 << ZRAM_LOGICAL_BLOCK_SHIFT)
51#define ZRAM_SECTOR_PER_LOGICAL_BLOCK \
52 (1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT))
Nitin Gupta306b0c92009-09-22 10:26:53 +053053
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053054/* Flags for zram pages (table[page_no].flags) */
55enum zram_pageflags {
Nitin Gupta306b0c92009-09-22 10:26:53 +053056 /* Page consists entirely of zeros */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053057 ZRAM_ZERO,
Nitin Gupta306b0c92009-09-22 10:26:53 +053058
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053059 __NR_ZRAM_PAGEFLAGS,
Nitin Gupta306b0c92009-09-22 10:26:53 +053060};
61
62/*-- Data structures */
63
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053064/* Allocated for each disk page */
Nitin Gupta306b0c92009-09-22 10:26:53 +053065struct table {
Minchan Kimc2344342012-06-08 15:39:25 +090066 unsigned long handle;
Nitin Guptafd1a30d2012-01-09 16:51:59 -060067 u16 size; /* object size (excluding header) */
Nitin Gupta306b0c92009-09-22 10:26:53 +053068 u8 count; /* object ref count (not yet used) */
69 u8 flags;
Sam Hansen80677c22012-06-07 16:03:48 -070070} __aligned(4);
Nitin Gupta306b0c92009-09-22 10:26:53 +053071
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053072struct zram_stats {
Nitin Gupta33863c22010-08-09 22:56:47 +053073 u64 compr_size; /* compressed size of pages stored */
Nitin Gupta306b0c92009-09-22 10:26:53 +053074 u64 num_reads; /* failed + successful */
75 u64 num_writes; /* --do-- */
C ypef4ffb72010-01-06 13:42:00 +010076 u64 failed_reads; /* should NEVER! happen */
77 u64 failed_writes; /* can happen when memory is too low */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053078 u64 invalid_io; /* non-page-aligned I/O requests */
Nitin Gupta6a907722010-01-28 21:13:37 +053079 u64 notify_free; /* no. of swap slot free notifications */
Nitin Gupta306b0c92009-09-22 10:26:53 +053080 u32 pages_zero; /* no. of zero filled pages */
81 u32 pages_stored; /* no. of pages currently stored */
82 u32 good_compress; /* % of pages with compression ratio<=50% */
Minchan Kim130f3152012-06-08 15:39:27 +090083 u32 bad_compress; /* % of pages with compression ratio>=75% */
Nitin Gupta306b0c92009-09-22 10:26:53 +053084};
85
Minchan Kim8b3cc3e2013-02-06 08:48:53 +090086struct zram_meta {
Nitin Gupta306b0c92009-09-22 10:26:53 +053087 void *compress_workmem;
88 void *compress_buffer;
89 struct table *table;
Minchan Kim8b3cc3e2013-02-06 08:48:53 +090090 struct zs_pool *mem_pool;
91};
92
93struct zram {
94 struct zram_meta *meta;
Nitin Gupta6a907722010-01-28 21:13:37 +053095 spinlock_t stat64_lock; /* protect 64-bit stats */
Jiang Liu57ab0482013-06-07 00:07:23 +080096 struct rw_semaphore lock; /* protect compression buffers, table,
97 * 32bit stat counters against concurrent
98 * notifications, reads and writes */
Nitin Gupta306b0c92009-09-22 10:26:53 +053099 struct request_queue *queue;
100 struct gendisk *disk;
101 int init_done;
Jerome Marchand0900bea2011-09-06 15:02:11 +0200102 /* Prevent concurrent execution of device init, reset and R/W request */
103 struct rw_semaphore init_lock;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530104 /*
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530105 * This is the limit on amount of *uncompressed* worth of data
106 * we can store in a disk.
Nitin Gupta306b0c92009-09-22 10:26:53 +0530107 */
Nitin Gupta33863c22010-08-09 22:56:47 +0530108 u64 disksize; /* bytes */
Nitin Gupta306b0c92009-09-22 10:26:53 +0530109
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530110 struct zram_stats stats;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530111};
112
Noah Watkins43801f62011-07-20 17:05:57 -0600113extern struct zram *zram_devices;
Nitin Gupta5fa5a902012-02-12 23:04:45 -0500114unsigned int zram_get_num_devices(void);
Nitin Gupta33863c22010-08-09 22:56:47 +0530115#ifdef CONFIG_SYSFS
116extern struct attribute_group zram_disk_attr_group;
117#endif
Nitin Gupta306b0c92009-09-22 10:26:53 +0530118
Minchan Kim0231c402013-01-30 11:41:40 +0900119extern void zram_reset_device(struct zram *zram);
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900120extern struct zram_meta *zram_meta_alloc(u64 disksize);
121extern void zram_meta_free(struct zram_meta *meta);
122extern void zram_init_device(struct zram *zram, struct zram_meta *meta);
Nitin Gupta6a907722010-01-28 21:13:37 +0530123
124#endif