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 "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 33 * object. This is required to support memory defragmentation. 34 */ 35struct zobj_header { 36#if 0 37 u32 table_idx; 38#endif 39}; 40 41/*-- Configurable parameters */ 42 43/* Default zram disk size: 25% of total RAM */ 44static const unsigned default_disksize_perc_ram = 25; 45 46/* 47 * Pages that compress to size greater than this are stored 48 * uncompressed in memory. 49 */ 50static const unsigned max_zpage_size = PAGE_SIZE / 4 * 3; 51 52/* 53 * NOTE: max_zpage_size must be less than or equal to: 54 * XV_MAX_ALLOC_SIZE - sizeof(struct zobj_header) 55 * otherwise, xv_malloc() would always return failure. 56 */ 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) 64#define ZRAM_LOGICAL_BLOCK_SIZE 4096 65 66/* Flags for zram pages (table[page_no].flags) */ 67enum zram_pageflags { 68 /* Page is stored uncompressed */ 69 ZRAM_UNCOMPRESSED, 70 71 /* Page consists entirely of zeros */ 72 ZRAM_ZERO, 73 74 __NR_ZRAM_PAGEFLAGS, 75}; 76 77/*-- Data structures */ 78 79/* Allocated for each disk page */ 80struct table { 81 struct page *page; 82 u16 offset; 83 u8 count; /* object ref count (not yet used) */ 84 u8 flags; 85} __attribute__((aligned(4))); 86 87struct zram_stats { 88 u64 compr_size; /* compressed size of pages stored */ 89 u64 num_reads; /* failed + successful */ 90 u64 num_writes; /* --do-- */ 91 u64 failed_reads; /* should NEVER! happen */ 92 u64 failed_writes; /* can happen when memory is too low */ 93 u64 invalid_io; /* non-page-aligned I/O requests */ 94 u64 notify_free; /* no. of swap slot free notifications */ 95 u32 pages_zero; /* no. of zero filled pages */ 96 u32 pages_stored; /* no. of pages currently stored */ 97 u32 good_compress; /* % of pages with compression ratio<=50% */ 98 u32 pages_expand; /* % of incompressible pages */ 99}; 100 101struct zram { 102 struct xv_pool *mem_pool; 103 void *compress_workmem; 104 void *compress_buffer; 105 struct table *table; 106 spinlock_t stat64_lock; /* protect 64-bit stats */ 107 struct mutex lock; /* protect compression buffers against 108 * concurrent writes */ 109 struct request_queue *queue; 110 struct gendisk *disk; 111 int init_done; 112 /* Prevent concurrent execution of device init and reset */ 113 struct mutex init_lock; 114 /* 115 * This is the limit on amount of *uncompressed* worth of data 116 * we can store in a disk. 117 */ 118 u64 disksize; /* bytes */ 119 120 struct zram_stats stats; 121}; 122 123extern struct zram *devices; 124extern unsigned int num_devices; 125#ifdef CONFIG_SYSFS 126extern struct attribute_group zram_disk_attr_group; 127#endif 128 129extern int zram_init_device(struct zram *zram); 130extern void zram_reset_device(struct zram *zram); 131 132#endif 133