1 /* 2 * Compressed RAM block device 3 * 4 * Copyright (C) 2008, 2009, 2010 Nitin Gupta 5 * 2012, 2013 Minchan Kim 6 * 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 * 13 */ 14 15 #ifndef _ZRAM_DRV_H_ 16 #define _ZRAM_DRV_H_ 17 18 #include <linux/rwsem.h> 19 #include <linux/zsmalloc.h> 20 #include <linux/crypto.h> 21 22 #include "zcomp.h" 23 24 #define SECTORS_PER_PAGE_SHIFT (PAGE_SHIFT - SECTOR_SHIFT) 25 #define SECTORS_PER_PAGE (1 << SECTORS_PER_PAGE_SHIFT) 26 #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)) 30 31 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) */ 45 enum zram_pageflags { 46 /* Page consists the same element */ 47 ZRAM_SAME = ZRAM_FLAG_SHIFT, 48 ZRAM_ACCESS, /* page is now accessed */ 49 ZRAM_WB, /* page is stored on backing_device */ 50 51 __NR_ZRAM_PAGEFLAGS, 52 }; 53 54 /*-- Data structures */ 55 56 /* Allocated for each disk page */ 57 struct zram_table_entry { 58 union { 59 unsigned long handle; 60 unsigned long element; 61 }; 62 unsigned long value; 63 }; 64 65 struct zram_stats { 66 atomic64_t compr_data_size; /* compressed size of pages stored */ 67 atomic64_t num_reads; /* failed + successful */ 68 atomic64_t num_writes; /* --do-- */ 69 atomic64_t failed_reads; /* can happen when memory is too low */ 70 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 */ 73 atomic64_t same_pages; /* no. of same element filled pages */ 74 atomic64_t pages_stored; /* no. of pages currently stored */ 75 atomic_long_t max_used_pages; /* no. of maximum pages stored */ 76 atomic64_t writestall; /* no. of write slow paths */ 77 }; 78 79 struct zram { 80 struct zram_table_entry *table; 81 struct zs_pool *mem_pool; 82 struct zcomp *comp; 83 struct gendisk *disk; 84 /* Prevent concurrent execution of device init */ 85 struct rw_semaphore init_lock; 86 /* 87 * the number of pages zram can consume for storing compressed data 88 */ 89 unsigned long limit_pages; 90 91 struct zram_stats stats; 92 /* 93 * This is the limit on amount of *uncompressed* worth of data 94 * we can store in a disk. 95 */ 96 u64 disksize; /* bytes */ 97 char compressor[CRYPTO_MAX_ALG_NAME]; 98 /* 99 * zram is claimed so open request will be failed 100 */ 101 bool claim; /* Protected by bdev->bd_mutex */ 102 #ifdef CONFIG_ZRAM_WRITEBACK 103 struct file *backing_dev; 104 struct block_device *bdev; 105 unsigned int old_block_size; 106 unsigned long *bitmap; 107 unsigned long nr_pages; 108 spinlock_t bitmap_lock; 109 #endif 110 }; 111 #endif 112