blob: 97a3acf6ab7613ebf4cee59710511162a9719fa0 [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
Jiang Liuda5cc7d2013-06-07 00:07:31 +080072/*
73 * All 64bit fields should only be manipulated by 64bit atomic accessors.
74 * All modifications to 32bit counter should be protected by zram->lock.
75 */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053076struct zram_stats {
Jiang Liuda5cc7d2013-06-07 00:07:31 +080077 atomic64_t compr_size; /* compressed size of pages stored */
78 atomic64_t num_reads; /* failed + successful */
79 atomic64_t num_writes; /* --do-- */
80 atomic64_t failed_reads; /* should NEVER! happen */
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 */
Nitin Gupta306b0c92009-09-22 10:26:53 +053084 u32 pages_zero; /* no. of zero filled pages */
85 u32 pages_stored; /* no. of pages currently stored */
86 u32 good_compress; /* % of pages with compression ratio<=50% */
Minchan Kim130f3152012-06-08 15:39:27 +090087 u32 bad_compress; /* % of pages with compression ratio>=75% */
Nitin Gupta306b0c92009-09-22 10:26:53 +053088};
89
Minchan Kim8b3cc3e2013-02-06 08:48:53 +090090struct zram_meta {
Nitin Gupta306b0c92009-09-22 10:26:53 +053091 void *compress_workmem;
92 void *compress_buffer;
93 struct table *table;
Minchan Kim8b3cc3e2013-02-06 08:48:53 +090094 struct zs_pool *mem_pool;
95};
96
Minchan Kima0c516c2013-08-12 15:13:56 +090097struct zram_slot_free {
98 unsigned long index;
99 struct zram_slot_free *next;
100};
101
Minchan Kim8b3cc3e2013-02-06 08:48:53 +0900102struct zram {
103 struct zram_meta *meta;
Jiang Liu57ab0482013-06-07 00:07:23 +0800104 struct rw_semaphore lock; /* protect compression buffers, table,
105 * 32bit stat counters against concurrent
106 * notifications, reads and writes */
Minchan Kima0c516c2013-08-12 15:13:56 +0900107
108 struct work_struct free_work; /* handle pending free request */
109 struct zram_slot_free *slot_free_rq; /* list head of free request */
110
Nitin Gupta306b0c92009-09-22 10:26:53 +0530111 struct request_queue *queue;
112 struct gendisk *disk;
113 int init_done;
Jerome Marchand0900bea2011-09-06 15:02:11 +0200114 /* Prevent concurrent execution of device init, reset and R/W request */
115 struct rw_semaphore init_lock;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530116 /*
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530117 * This is the limit on amount of *uncompressed* worth of data
118 * we can store in a disk.
Nitin Gupta306b0c92009-09-22 10:26:53 +0530119 */
Nitin Gupta33863c22010-08-09 22:56:47 +0530120 u64 disksize; /* bytes */
Minchan Kima0c516c2013-08-12 15:13:56 +0900121 spinlock_t slot_free_lock;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530122
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530123 struct zram_stats stats;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530124};
Nitin Gupta6a907722010-01-28 21:13:37 +0530125#endif