blob: 008861220723cbb0da9338e390ff9f2bf3205818 [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
Minchan Kim7bfb3de2014-01-30 15:45:55 -08005 * 2012, 2013 Minchan Kim
Nitin Gupta306b0c92009-09-22 10:26:53 +05306 *
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 *
Nitin Gupta306b0c92009-09-22 10:26:53 +053013 */
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
Sergey Senozhatsky415403b2016-07-26 15:22:48 -070018#include <linux/rwsem.h>
Minchan Kimbcf16472014-01-30 15:45:50 -080019#include <linux/zsmalloc.h>
Sergey Senozhatsky415403b2016-07-26 15:22:48 -070020#include <linux/crypto.h>
Nitin Gupta306b0c92009-09-22 10:26:53 +053021
Sergey Senozhatskyb7ca2322014-04-07 15:38:12 -070022#include "zcomp.h"
23
Nitin Gupta306b0c92009-09-22 10:26:53 +053024#define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
25#define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT)
Jerome Marchand924bd882011-06-10 15:28:48 +020026#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))
Nitin Gupta306b0c92009-09-22 10:26:53 +053030
Weijie Yangd2d5e762014-08-06 16:08:31 -070031
32/*
33 * The lower ZRAM_FLAG_SHIFT bits of table.value is for
34 * object size (excluding header), the higher bits is for
35 * zram_pageflags.
36 *
37 * zram is mainly used for memory efficiency so we want to keep memory
38 * footprint small so we can squeeze size and flags into a field.
39 * The lower ZRAM_FLAG_SHIFT bits is for object size (excluding header),
40 * the higher bits is for zram_pageflags.
41 */
42#define ZRAM_FLAG_SHIFT 24
43
44/* Flags for zram pages (table[page_no].value) */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053045enum zram_pageflags {
Minchan Kimdb8ffbd2017-09-06 16:20:03 -070046 /* Page consists the same element */
zhouxianrong8e19d542017-02-24 14:59:27 -080047 ZRAM_SAME = ZRAM_FLAG_SHIFT,
Mahendran Ganeshd49b1c22014-12-12 16:57:04 -080048 ZRAM_ACCESS, /* page is now accessed */
Minchan Kimdb8ffbd2017-09-06 16:20:03 -070049 ZRAM_WB, /* page is stored on backing_device */
Nitin Gupta306b0c92009-09-22 10:26:53 +053050
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053051 __NR_ZRAM_PAGEFLAGS,
Nitin Gupta306b0c92009-09-22 10:26:53 +053052};
53
54/*-- Data structures */
55
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053056/* Allocated for each disk page */
Sergey Senozhatskycb8f2ee2014-08-06 16:08:25 -070057struct zram_table_entry {
zhouxianrong8e19d542017-02-24 14:59:27 -080058 union {
59 unsigned long handle;
60 unsigned long element;
61 };
Weijie Yangd2d5e762014-08-06 16:08:31 -070062 unsigned long value;
63};
Nitin Gupta306b0c92009-09-22 10:26:53 +053064
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053065struct zram_stats {
Sergey Senozhatsky90a78062014-04-07 15:38:03 -070066 atomic64_t compr_data_size; /* compressed size of pages stored */
Jiang Liuda5cc7d2013-06-07 00:07:31 +080067 atomic64_t num_reads; /* failed + successful */
68 atomic64_t num_writes; /* --do-- */
Chao Yu0cf1e9d2014-08-29 15:18:37 -070069 atomic64_t failed_reads; /* can happen when memory is too low */
Jiang Liuda5cc7d2013-06-07 00:07:31 +080070 atomic64_t failed_writes; /* can happen when memory is too low */
71 atomic64_t invalid_io; /* non-page-aligned I/O requests */
72 atomic64_t notify_free; /* no. of swap slot free notifications */
zhouxianrong8e19d542017-02-24 14:59:27 -080073 atomic64_t same_pages; /* no. of same element filled pages */
Sergey Senozhatsky90a78062014-04-07 15:38:03 -070074 atomic64_t pages_stored; /* no. of pages currently stored */
Minchan Kim461a8ee2014-10-09 15:29:55 -070075 atomic_long_t max_used_pages; /* no. of maximum pages stored */
Sergey Senozhatsky623e47f2016-05-20 17:00:02 -070076 atomic64_t writestall; /* no. of write slow paths */
Nitin Gupta306b0c92009-09-22 10:26:53 +053077};
78
Minchan Kimbeb66022017-05-03 14:55:47 -070079struct zram {
Sergey Senozhatskycb8f2ee2014-08-06 16:08:25 -070080 struct zram_table_entry *table;
Minchan Kim8b3cc3e2013-02-06 08:48:53 +090081 struct zs_pool *mem_pool;
Minchan Kim08eee692015-02-12 15:00:45 -080082 struct zcomp *comp;
Nitin Gupta306b0c92009-09-22 10:26:53 +053083 struct gendisk *disk;
Minchan Kim08eee692015-02-12 15:00:45 -080084 /* Prevent concurrent execution of device init */
Jerome Marchand0900bea2011-09-06 15:02:11 +020085 struct rw_semaphore init_lock;
Nitin Gupta306b0c92009-09-22 10:26:53 +053086 /*
Minchan Kim08eee692015-02-12 15:00:45 -080087 * the number of pages zram can consume for storing compressed data
88 */
89 unsigned long limit_pages;
Minchan Kim08eee692015-02-12 15:00:45 -080090
91 struct zram_stats stats;
Minchan Kim08eee692015-02-12 15:00:45 -080092 /*
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053093 * This is the limit on amount of *uncompressed* worth of data
94 * we can store in a disk.
Nitin Gupta306b0c92009-09-22 10:26:53 +053095 */
Nitin Gupta33863c22010-08-09 22:56:47 +053096 u64 disksize; /* bytes */
Sergey Senozhatsky415403b2016-07-26 15:22:48 -070097 char compressor[CRYPTO_MAX_ALG_NAME];
Sergey Senozhatskyf405c442015-06-25 15:00:21 -070098 /*
99 * zram is claimed so open request will be failed
100 */
101 bool claim; /* Protected by bdev->bd_mutex */
Minchan Kim013bf952017-09-06 16:19:54 -0700102#ifdef CONFIG_ZRAM_WRITEBACK
103 struct file *backing_dev;
104 struct block_device *bdev;
105 unsigned int old_block_size;
Minchan Kim1363d462017-09-06 16:19:57 -0700106 unsigned long *bitmap;
107 unsigned long nr_pages;
108 spinlock_t bitmap_lock;
Minchan Kim013bf952017-09-06 16:19:54 -0700109#endif
Nitin Gupta306b0c92009-09-22 10:26:53 +0530110};
Nitin Gupta6a907722010-01-28 21:13:37 +0530111#endif