blob: e5cd2469b6a088e034c1dea8c098dd8fd3463313 [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 Gupta306b0c92009-09-22 10:26:53 +053021#include "xvmalloc.h"
22
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
29/*
30 * Stored at beginning of each compressed object.
31 *
32 * It stores back-reference to table entry which points to this
Nitin Gupta97a06382010-05-13 14:24:21 +053033 * object. This is required to support memory defragmentation.
Nitin Gupta306b0c92009-09-22 10:26:53 +053034 */
35struct zobj_header {
36#if 0
37 u32 table_idx;
38#endif
39};
40
41/*-- Configurable parameters */
42
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053043/* Default zram disk size: 25% of total RAM */
Nitin Gupta306b0c92009-09-22 10:26:53 +053044static const unsigned default_disksize_perc_ram = 25;
Nitin Gupta306b0c92009-09-22 10:26:53 +053045
46/*
Nitin Gupta306b0c92009-09-22 10:26:53 +053047 * Pages that compress to size greater than this are stored
48 * uncompressed in memory.
49 */
Nitin Gupta2ccbec02011-09-09 19:01:00 -040050static const size_t max_zpage_size = PAGE_SIZE / 4 * 3;
Nitin Gupta306b0c92009-09-22 10:26:53 +053051
52/*
Nitin Gupta97a06382010-05-13 14:24:21 +053053 * NOTE: max_zpage_size must be less than or equal to:
Nitin Gupta306b0c92009-09-22 10:26:53 +053054 * XV_MAX_ALLOC_SIZE - sizeof(struct zobj_header)
Nitin Gupta97a06382010-05-13 14:24:21 +053055 * otherwise, xv_malloc() would always return failure.
Nitin Gupta306b0c92009-09-22 10:26:53 +053056 */
57
58/*-- End of configurable params */
59
60#define SECTOR_SHIFT 9
61#define SECTOR_SIZE (1 << SECTOR_SHIFT)
62#define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT)
63#define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT)
Jerome Marchand924bd882011-06-10 15:28:48 +020064#define ZRAM_LOGICAL_BLOCK_SHIFT 12
65#define ZRAM_LOGICAL_BLOCK_SIZE (1 << ZRAM_LOGICAL_BLOCK_SHIFT)
66#define ZRAM_SECTOR_PER_LOGICAL_BLOCK \
67 (1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT))
Nitin Gupta306b0c92009-09-22 10:26:53 +053068
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053069/* Flags for zram pages (table[page_no].flags) */
70enum zram_pageflags {
Nitin Gupta306b0c92009-09-22 10:26:53 +053071 /* Page is stored uncompressed */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053072 ZRAM_UNCOMPRESSED,
Nitin Gupta306b0c92009-09-22 10:26:53 +053073
74 /* Page consists entirely of zeros */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053075 ZRAM_ZERO,
Nitin Gupta306b0c92009-09-22 10:26:53 +053076
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053077 __NR_ZRAM_PAGEFLAGS,
Nitin Gupta306b0c92009-09-22 10:26:53 +053078};
79
80/*-- Data structures */
81
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053082/* Allocated for each disk page */
Nitin Gupta306b0c92009-09-22 10:26:53 +053083struct table {
84 struct page *page;
85 u16 offset;
86 u8 count; /* object ref count (not yet used) */
87 u8 flags;
C ypef4ffb72010-01-06 13:42:00 +010088} __attribute__((aligned(4)));
Nitin Gupta306b0c92009-09-22 10:26:53 +053089
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053090struct zram_stats {
Nitin Gupta33863c22010-08-09 22:56:47 +053091 u64 compr_size; /* compressed size of pages stored */
Nitin Gupta306b0c92009-09-22 10:26:53 +053092 u64 num_reads; /* failed + successful */
93 u64 num_writes; /* --do-- */
C ypef4ffb72010-01-06 13:42:00 +010094 u64 failed_reads; /* should NEVER! happen */
95 u64 failed_writes; /* can happen when memory is too low */
Nitin Guptaf1e3cff2010-06-01 13:31:25 +053096 u64 invalid_io; /* non-page-aligned I/O requests */
Nitin Gupta6a907722010-01-28 21:13:37 +053097 u64 notify_free; /* no. of swap slot free notifications */
Nitin Gupta306b0c92009-09-22 10:26:53 +053098 u32 pages_zero; /* no. of zero filled pages */
99 u32 pages_stored; /* no. of pages currently stored */
100 u32 good_compress; /* % of pages with compression ratio<=50% */
101 u32 pages_expand; /* % of incompressible pages */
Nitin Gupta306b0c92009-09-22 10:26:53 +0530102};
103
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530104struct zram {
Nitin Gupta306b0c92009-09-22 10:26:53 +0530105 struct xv_pool *mem_pool;
106 void *compress_workmem;
107 void *compress_buffer;
108 struct table *table;
Nitin Gupta6a907722010-01-28 21:13:37 +0530109 spinlock_t stat64_lock; /* protect 64-bit stats */
Jerome Marchandc5bde232011-06-10 15:28:49 +0200110 struct rw_semaphore lock; /* protect compression buffers and table
111 * against concurrent read and writes */
Nitin Gupta306b0c92009-09-22 10:26:53 +0530112 struct request_queue *queue;
113 struct gendisk *disk;
114 int init_done;
Jerome Marchand0900bea2011-09-06 15:02:11 +0200115 /* Prevent concurrent execution of device init, reset and R/W request */
116 struct rw_semaphore init_lock;
Nitin Gupta306b0c92009-09-22 10:26:53 +0530117 /*
Nitin Guptaf1e3cff2010-06-01 13:31:25 +0530118 * This is the limit on amount of *uncompressed* worth of data
119 * we can store in a disk.
Nitin Gupta306b0c92009-09-22 10:26:53 +0530120 */
Nitin Gupta33863c22010-08-09 22:56:47 +0530121 u64 disksize; /* bytes */
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};
125
Noah Watkins43801f62011-07-20 17:05:57 -0600126extern struct zram *zram_devices;
Noah Watkinsefd54f42011-07-20 17:06:08 -0600127extern unsigned int zram_num_devices;
Nitin Gupta33863c22010-08-09 22:56:47 +0530128#ifdef CONFIG_SYSFS
129extern struct attribute_group zram_disk_attr_group;
130#endif
Nitin Gupta306b0c92009-09-22 10:26:53 +0530131
Nitin Gupta33863c22010-08-09 22:56:47 +0530132extern int zram_init_device(struct zram *zram);
Jerome Marchand0900bea2011-09-06 15:02:11 +0200133extern void __zram_reset_device(struct zram *zram);
Nitin Gupta6a907722010-01-28 21:13:37 +0530134
135#endif