1/* 2 * Compressed RAM block device 3 * 4 * Copyright (C) 2008, 2009, 2010 Nitin Gupta 5 * 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 15#ifndef _ZRAM_DRV_H_ 16#define _ZRAM_DRV_H_ 17 18#include <linux/spinlock.h> 19#include <linux/mutex.h> 20 21#include "zram_ioctl.h" 22#include "xvmalloc.h" 23 24/* 25 * Some arbitrary value. This is just to catch 26 * invalid value for num_devices module parameter. 27 */ 28static const unsigned max_num_devices = 32; 29 30/* 31 * Stored at beginning of each compressed object. 32 * 33 * It stores back-reference to table entry which points to this 34 * object. This is required to support memory defragmentation. 35 */ 36struct zobj_header { 37}; 38 39/*-- Configurable parameters */ 40 41/* Default zram disk size: 25% of total RAM */ 42static const unsigned default_disksize_perc_ram = 25; 43 44/* 45 * Pages that compress to size greater than this are stored 46 * uncompressed in memory. 47 */ 48static const unsigned max_zpage_size = PAGE_SIZE / 4 * 3; 49 50/* 51 * NOTE: max_zpage_size must be less than or equal to: 52 * XV_MAX_ALLOC_SIZE - sizeof(struct zobj_header) 53 * otherwise, xv_malloc() would always return failure. 54 */ 55 56/*-- End of configurable params */ 57 58#define SECTOR_SHIFT 9 59#define SECTOR_SIZE (1 << SECTOR_SHIFT) 60#define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT) 61#define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT) 62 63/* Flags for zram pages (table[page_no].flags) */ 64enum zram_pageflags { 65 /* Page is stored uncompressed */ 66 ZRAM_UNCOMPRESSED, 67 68 /* Page consists entirely of zeros */ 69 ZRAM_ZERO, 70 71 __NR_ZRAM_PAGEFLAGS, 72}; 73 74/*-- Data structures */ 75 76/* Allocated for each disk page */ 77struct table { 78 struct page *page; 79 u16 offset; 80 u8 count; /* object ref count (not yet used) */ 81 u8 flags; 82} __attribute__((aligned(4))); 83 84struct zram_stats { 85 /* basic stats */ 86 size_t compr_size; /* compressed size of pages stored - 87 * needed to enforce memlimit */ 88 /* more stats */ 89#if defined(CONFIG_ZRAM_STATS) 90 u64 num_reads; /* failed + successful */ 91 u64 num_writes; /* --do-- */ 92 u64 failed_reads; /* should NEVER! happen */ 93 u64 failed_writes; /* can happen when memory is too low */ 94 u64 invalid_io; /* non-page-aligned I/O requests */ 95 u64 notify_free; /* no. of swap slot free notifications */ 96 u32 pages_zero; /* no. of zero filled pages */ 97 u32 pages_stored; /* no. of pages currently stored */ 98 u32 good_compress; /* % of pages with compression ratio<=50% */ 99 u32 pages_expand; /* % of incompressible pages */ 100#endif 101}; 102 103struct zram { 104 struct xv_pool *mem_pool; 105 void *compress_workmem; 106 void *compress_buffer; 107 struct table *table; 108 spinlock_t stat64_lock; /* protect 64-bit stats */ 109 struct mutex lock; /* protect compression buffers against 110 * concurrent writes */ 111 struct request_queue *queue; 112 struct gendisk *disk; 113 int init_done; 114 /* 115 * This is the limit on amount of *uncompressed* worth of data 116 * we can store in a disk. 117 */ 118 size_t disksize; /* bytes */ 119 120 struct zram_stats stats; 121}; 122 123/*-- */ 124 125/* Debugging and Stats */ 126#if defined(CONFIG_ZRAM_STATS) 127static void zram_stat_inc(u32 *v) 128{ 129 *v = *v + 1; 130} 131 132static void zram_stat_dec(u32 *v) 133{ 134 *v = *v - 1; 135} 136 137static void zram_stat64_inc(struct zram *zram, u64 *v) 138{ 139 spin_lock(&zram->stat64_lock); 140 *v = *v + 1; 141 spin_unlock(&zram->stat64_lock); 142} 143 144static u64 zram_stat64_read(struct zram *zram, u64 *v) 145{ 146 u64 val; 147 148 spin_lock(&zram->stat64_lock); 149 val = *v; 150 spin_unlock(&zram->stat64_lock); 151 152 return val; 153} 154#else 155#define zram_stat_inc(v) 156#define zram_stat_dec(v) 157#define zram_stat64_inc(r, v) 158#define zram_stat64_read(r, v) 159#endif /* CONFIG_ZRAM_STATS */ 160 161#endif 162