Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 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 | #include <linux/device.h> |
| 16 | #include <linux/genhd.h> |
Jerome Marchand | f2da987 | 2010-12-17 17:02:28 +0100 | [diff] [blame] | 17 | #include <linux/mm.h> |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 18 | |
| 19 | #include "zram_drv.h" |
| 20 | |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 21 | static u64 zram_stat64_read(struct zram *zram, u64 *v) |
| 22 | { |
| 23 | u64 val; |
| 24 | |
| 25 | spin_lock(&zram->stat64_lock); |
| 26 | val = *v; |
| 27 | spin_unlock(&zram->stat64_lock); |
| 28 | |
| 29 | return val; |
| 30 | } |
| 31 | |
| 32 | static struct zram *dev_to_zram(struct device *dev) |
| 33 | { |
| 34 | int i; |
| 35 | struct zram *zram = NULL; |
| 36 | |
Noah Watkins | efd54f4 | 2011-07-20 17:06:08 -0600 | [diff] [blame] | 37 | for (i = 0; i < zram_num_devices; i++) { |
Noah Watkins | 43801f6 | 2011-07-20 17:05:57 -0600 | [diff] [blame] | 38 | zram = &zram_devices[i]; |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 39 | if (disk_to_dev(zram->disk) == dev) |
| 40 | break; |
| 41 | } |
| 42 | |
| 43 | return zram; |
| 44 | } |
| 45 | |
| 46 | static ssize_t disksize_show(struct device *dev, |
| 47 | struct device_attribute *attr, char *buf) |
| 48 | { |
| 49 | struct zram *zram = dev_to_zram(dev); |
| 50 | |
| 51 | return sprintf(buf, "%llu\n", zram->disksize); |
| 52 | } |
| 53 | |
| 54 | static ssize_t disksize_store(struct device *dev, |
| 55 | struct device_attribute *attr, const char *buf, size_t len) |
| 56 | { |
| 57 | int ret; |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 58 | u64 disksize; |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 59 | struct zram *zram = dev_to_zram(dev); |
| 60 | |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 61 | ret = strict_strtoull(buf, 10, &disksize); |
| 62 | if (ret) |
| 63 | return ret; |
| 64 | |
| 65 | down_write(&zram->init_lock); |
Nitin Gupta | 484875a | 2010-08-09 22:56:48 +0530 | [diff] [blame] | 66 | if (zram->init_done) { |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 67 | up_write(&zram->init_lock); |
Nitin Gupta | 484875a | 2010-08-09 22:56:48 +0530 | [diff] [blame] | 68 | pr_info("Cannot change disksize for initialized device\n"); |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 69 | return -EBUSY; |
Nitin Gupta | 484875a | 2010-08-09 22:56:48 +0530 | [diff] [blame] | 70 | } |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 71 | |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 72 | zram->disksize = PAGE_ALIGN(disksize); |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 73 | set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT); |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 74 | up_write(&zram->init_lock); |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 75 | |
| 76 | return len; |
| 77 | } |
| 78 | |
| 79 | static ssize_t initstate_show(struct device *dev, |
| 80 | struct device_attribute *attr, char *buf) |
| 81 | { |
| 82 | struct zram *zram = dev_to_zram(dev); |
| 83 | |
| 84 | return sprintf(buf, "%u\n", zram->init_done); |
| 85 | } |
| 86 | |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 87 | static ssize_t reset_store(struct device *dev, |
| 88 | struct device_attribute *attr, const char *buf, size_t len) |
| 89 | { |
| 90 | int ret; |
| 91 | unsigned long do_reset; |
| 92 | struct zram *zram; |
| 93 | struct block_device *bdev; |
| 94 | |
| 95 | zram = dev_to_zram(dev); |
| 96 | bdev = bdget_disk(zram->disk, 0); |
| 97 | |
| 98 | /* Do not reset an active device! */ |
| 99 | if (bdev->bd_holders) |
| 100 | return -EBUSY; |
| 101 | |
| 102 | ret = strict_strtoul(buf, 10, &do_reset); |
| 103 | if (ret) |
| 104 | return ret; |
| 105 | |
| 106 | if (!do_reset) |
| 107 | return -EINVAL; |
| 108 | |
| 109 | /* Make sure all pending I/O is finished */ |
| 110 | if (bdev) |
| 111 | fsync_bdev(bdev); |
| 112 | |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 113 | down_write(&zram->init_lock); |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 114 | if (zram->init_done) |
Jerome Marchand | 0900bea | 2011-09-06 15:02:11 +0200 | [diff] [blame] | 115 | __zram_reset_device(zram); |
| 116 | up_write(&zram->init_lock); |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 117 | |
| 118 | return len; |
| 119 | } |
| 120 | |
| 121 | static ssize_t num_reads_show(struct device *dev, |
| 122 | struct device_attribute *attr, char *buf) |
| 123 | { |
| 124 | struct zram *zram = dev_to_zram(dev); |
| 125 | |
| 126 | return sprintf(buf, "%llu\n", |
| 127 | zram_stat64_read(zram, &zram->stats.num_reads)); |
| 128 | } |
| 129 | |
| 130 | static ssize_t num_writes_show(struct device *dev, |
| 131 | struct device_attribute *attr, char *buf) |
| 132 | { |
| 133 | struct zram *zram = dev_to_zram(dev); |
| 134 | |
| 135 | return sprintf(buf, "%llu\n", |
| 136 | zram_stat64_read(zram, &zram->stats.num_writes)); |
| 137 | } |
| 138 | |
| 139 | static ssize_t invalid_io_show(struct device *dev, |
| 140 | struct device_attribute *attr, char *buf) |
| 141 | { |
| 142 | struct zram *zram = dev_to_zram(dev); |
| 143 | |
| 144 | return sprintf(buf, "%llu\n", |
| 145 | zram_stat64_read(zram, &zram->stats.invalid_io)); |
| 146 | } |
| 147 | |
| 148 | static ssize_t notify_free_show(struct device *dev, |
| 149 | struct device_attribute *attr, char *buf) |
| 150 | { |
| 151 | struct zram *zram = dev_to_zram(dev); |
| 152 | |
| 153 | return sprintf(buf, "%llu\n", |
| 154 | zram_stat64_read(zram, &zram->stats.notify_free)); |
| 155 | } |
| 156 | |
| 157 | static ssize_t zero_pages_show(struct device *dev, |
| 158 | struct device_attribute *attr, char *buf) |
| 159 | { |
| 160 | struct zram *zram = dev_to_zram(dev); |
| 161 | |
| 162 | return sprintf(buf, "%u\n", zram->stats.pages_zero); |
| 163 | } |
| 164 | |
| 165 | static ssize_t orig_data_size_show(struct device *dev, |
| 166 | struct device_attribute *attr, char *buf) |
| 167 | { |
| 168 | struct zram *zram = dev_to_zram(dev); |
| 169 | |
| 170 | return sprintf(buf, "%llu\n", |
| 171 | (u64)(zram->stats.pages_stored) << PAGE_SHIFT); |
| 172 | } |
| 173 | |
| 174 | static ssize_t compr_data_size_show(struct device *dev, |
| 175 | struct device_attribute *attr, char *buf) |
| 176 | { |
| 177 | struct zram *zram = dev_to_zram(dev); |
| 178 | |
| 179 | return sprintf(buf, "%llu\n", |
| 180 | zram_stat64_read(zram, &zram->stats.compr_size)); |
| 181 | } |
| 182 | |
| 183 | static ssize_t mem_used_total_show(struct device *dev, |
| 184 | struct device_attribute *attr, char *buf) |
| 185 | { |
| 186 | u64 val = 0; |
| 187 | struct zram *zram = dev_to_zram(dev); |
| 188 | |
| 189 | if (zram->init_done) { |
| 190 | val = xv_get_total_size_bytes(zram->mem_pool) + |
| 191 | ((u64)(zram->stats.pages_expand) << PAGE_SHIFT); |
| 192 | } |
| 193 | |
| 194 | return sprintf(buf, "%llu\n", val); |
| 195 | } |
| 196 | |
Greg Kroah-Hartman | a3d1310 | 2010-11-18 11:21:04 -0800 | [diff] [blame] | 197 | static DEVICE_ATTR(disksize, S_IRUGO | S_IWUSR, |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 198 | disksize_show, disksize_store); |
Nitin Gupta | 484875a | 2010-08-09 22:56:48 +0530 | [diff] [blame] | 199 | static DEVICE_ATTR(initstate, S_IRUGO, initstate_show, NULL); |
Greg Kroah-Hartman | a3d1310 | 2010-11-18 11:21:04 -0800 | [diff] [blame] | 200 | static DEVICE_ATTR(reset, S_IWUSR, NULL, reset_store); |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 201 | static DEVICE_ATTR(num_reads, S_IRUGO, num_reads_show, NULL); |
| 202 | static DEVICE_ATTR(num_writes, S_IRUGO, num_writes_show, NULL); |
| 203 | static DEVICE_ATTR(invalid_io, S_IRUGO, invalid_io_show, NULL); |
| 204 | static DEVICE_ATTR(notify_free, S_IRUGO, notify_free_show, NULL); |
| 205 | static DEVICE_ATTR(zero_pages, S_IRUGO, zero_pages_show, NULL); |
| 206 | static DEVICE_ATTR(orig_data_size, S_IRUGO, orig_data_size_show, NULL); |
| 207 | static DEVICE_ATTR(compr_data_size, S_IRUGO, compr_data_size_show, NULL); |
| 208 | static DEVICE_ATTR(mem_used_total, S_IRUGO, mem_used_total_show, NULL); |
| 209 | |
| 210 | static struct attribute *zram_disk_attrs[] = { |
| 211 | &dev_attr_disksize.attr, |
| 212 | &dev_attr_initstate.attr, |
| 213 | &dev_attr_reset.attr, |
| 214 | &dev_attr_num_reads.attr, |
| 215 | &dev_attr_num_writes.attr, |
| 216 | &dev_attr_invalid_io.attr, |
| 217 | &dev_attr_notify_free.attr, |
| 218 | &dev_attr_zero_pages.attr, |
| 219 | &dev_attr_orig_data_size.attr, |
| 220 | &dev_attr_compr_data_size.attr, |
| 221 | &dev_attr_mem_used_total.attr, |
| 222 | NULL, |
| 223 | }; |
| 224 | |
| 225 | struct attribute_group zram_disk_attr_group = { |
| 226 | .attrs = zram_disk_attrs, |
| 227 | }; |