| 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> |
| 17 | #include <linux/mm.h> |
| 18 | #include <linux/kernel.h> |
| 19 | |
| 20 | #include "zram_drv.h" |
| 21 | |
| 22 | static u64 zram_stat64_read(struct zram *zram, u64 *v) |
| 23 | { |
| 24 | u64 val; |
| 25 | |
| 26 | spin_lock(&zram->stat64_lock); |
| 27 | val = *v; |
| 28 | spin_unlock(&zram->stat64_lock); |
| 29 | |
| 30 | return val; |
| 31 | } |
| 32 | |
| 33 | static struct zram *dev_to_zram(struct device *dev) |
| 34 | { |
| 35 | int i; |
| 36 | struct zram *zram = NULL; |
| 37 | |
| 38 | for (i = 0; i < zram_get_num_devices(); i++) { |
| 39 | zram = &zram_devices[i]; |
| 40 | if (disk_to_dev(zram->disk) == dev) |
| 41 | break; |
| 42 | } |
| 43 | |
| 44 | return zram; |
| 45 | } |
| 46 | |
| 47 | static ssize_t disksize_show(struct device *dev, |
| 48 | struct device_attribute *attr, char *buf) |
| 49 | { |
| 50 | struct zram *zram = dev_to_zram(dev); |
| 51 | |
| 52 | return sprintf(buf, "%llu\n", zram->disksize); |
| 53 | } |
| 54 | |
| 55 | static ssize_t disksize_store(struct device *dev, |
| 56 | struct device_attribute *attr, const char *buf, size_t len) |
| 57 | { |
| 58 | int ret; |
| 59 | u64 disksize; |
| 60 | struct zram_meta *meta; |
| 61 | struct zram *zram = dev_to_zram(dev); |
| 62 | |
| 63 | /* Get disksize from user */ |
| 64 | ret = kstrtoull(buf, 10, &disksize); |
| 65 | if (ret) |
| 66 | return ret; |
| 67 | |
| 68 | /* If disksize is 0, then we give it a default setting. */ |
| 69 | if (disksize == 0) { |
| 70 | /* Fix disksize */ |
| 71 | disksize = default_disksize_perc_ram * ((totalram_pages << PAGE_SHIFT) / 100); |
| 72 | /* Expand its disksize if we have little system ram! */ |
| 73 | if (totalram_pages < SUPPOSED_TOTALRAM) { |
| 74 | disksize += (disksize >> 1) ; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /* Align it! */ |
| 79 | disksize = round_up(disksize, DISKSIZE_ALIGNMENT); |
| 80 | /* disksize = PAGE_ALIGN(disksize); */ |
| 81 | |
| 82 | meta = zram_meta_alloc(disksize); |
| 83 | /* Check whether meta is null */ |
| 84 | if (!meta) { |
| 85 | printk(KERN_ALERT"Failed to allocate memory for meta!\n"); |
| 86 | return len; |
| 87 | } |
| 88 | down_write(&zram->init_lock); |
| 89 | if (zram->init_done) { |
| 90 | up_write(&zram->init_lock); |
| 91 | zram_meta_free(meta); |
| 92 | pr_info("Cannot change disksize for initialized device\n"); |
| 93 | return -EBUSY; |
| 94 | } |
| 95 | |
| 96 | zram->disksize = disksize; |
| 97 | set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT); |
| 98 | zram_init_device(zram, meta); |
| 99 | up_write(&zram->init_lock); |
| 100 | |
| 101 | return len; |
| 102 | } |
| 103 | |
| 104 | static ssize_t initstate_show(struct device *dev, |
| 105 | struct device_attribute *attr, char *buf) |
| 106 | { |
| 107 | struct zram *zram = dev_to_zram(dev); |
| 108 | |
| 109 | return sprintf(buf, "%u\n", zram->init_done); |
| 110 | } |
| 111 | |
| 112 | static ssize_t reset_store(struct device *dev, |
| 113 | struct device_attribute *attr, const char *buf, size_t len) |
| 114 | { |
| 115 | int ret; |
| 116 | unsigned short do_reset; |
| 117 | struct zram *zram; |
| 118 | struct block_device *bdev; |
| 119 | |
| 120 | zram = dev_to_zram(dev); |
| 121 | bdev = bdget_disk(zram->disk, 0); |
| 122 | |
| 123 | /* Do not reset an active device! */ |
| 124 | if (bdev->bd_holders) |
| 125 | return -EBUSY; |
| 126 | |
| 127 | ret = kstrtou16(buf, 10, &do_reset); |
| 128 | if (ret) |
| 129 | return ret; |
| 130 | |
| 131 | if (!do_reset) |
| 132 | return -EINVAL; |
| 133 | |
| 134 | /* Make sure all pending I/O is finished */ |
| 135 | if (bdev) |
| 136 | fsync_bdev(bdev); |
| 137 | |
| 138 | zram_reset_device(zram); |
| 139 | return len; |
| 140 | } |
| 141 | |
| 142 | static ssize_t num_reads_show(struct device *dev, |
| 143 | struct device_attribute *attr, char *buf) |
| 144 | { |
| 145 | struct zram *zram = dev_to_zram(dev); |
| 146 | |
| 147 | return sprintf(buf, "%llu\n", |
| 148 | zram_stat64_read(zram, &zram->stats.num_reads)); |
| 149 | } |
| 150 | |
| 151 | static ssize_t num_writes_show(struct device *dev, |
| 152 | struct device_attribute *attr, char *buf) |
| 153 | { |
| 154 | struct zram *zram = dev_to_zram(dev); |
| 155 | |
| 156 | return sprintf(buf, "%llu\n", |
| 157 | zram_stat64_read(zram, &zram->stats.num_writes)); |
| 158 | } |
| 159 | |
| 160 | static ssize_t invalid_io_show(struct device *dev, |
| 161 | struct device_attribute *attr, char *buf) |
| 162 | { |
| 163 | struct zram *zram = dev_to_zram(dev); |
| 164 | |
| 165 | return sprintf(buf, "%llu\n", |
| 166 | zram_stat64_read(zram, &zram->stats.invalid_io)); |
| 167 | } |
| 168 | |
| 169 | static ssize_t notify_free_show(struct device *dev, |
| 170 | struct device_attribute *attr, char *buf) |
| 171 | { |
| 172 | struct zram *zram = dev_to_zram(dev); |
| 173 | |
| 174 | return sprintf(buf, "%llu\n", |
| 175 | zram_stat64_read(zram, &zram->stats.notify_free)); |
| 176 | } |
| 177 | |
| 178 | static ssize_t zero_pages_show(struct device *dev, |
| 179 | struct device_attribute *attr, char *buf) |
| 180 | { |
| 181 | struct zram *zram = dev_to_zram(dev); |
| 182 | |
| 183 | return sprintf(buf, "%u\n", zram->stats.pages_zero); |
| 184 | } |
| 185 | |
| 186 | static ssize_t good_compr_pages_show(struct device *dev, |
| 187 | struct device_attribute *attr, char *buf) |
| 188 | { |
| 189 | struct zram *zram = dev_to_zram(dev); |
| 190 | |
| 191 | return sprintf(buf, "%u\n", zram->stats.good_compress); |
| 192 | } |
| 193 | |
| 194 | static ssize_t bad_compr_pages_show(struct device *dev, |
| 195 | struct device_attribute *attr, char *buf) |
| 196 | { |
| 197 | struct zram *zram = dev_to_zram(dev); |
| 198 | |
| 199 | return sprintf(buf, "%u\n", zram->stats.bad_compress); |
| 200 | } |
| 201 | |
| 202 | static ssize_t orig_data_size_show(struct device *dev, |
| 203 | struct device_attribute *attr, char *buf) |
| 204 | { |
| 205 | struct zram *zram = dev_to_zram(dev); |
| 206 | |
| 207 | return sprintf(buf, "%llu\n", |
| 208 | (u64)(zram->stats.pages_stored) << PAGE_SHIFT); |
| 209 | } |
| 210 | |
| 211 | static ssize_t compr_data_size_show(struct device *dev, |
| 212 | struct device_attribute *attr, char *buf) |
| 213 | { |
| 214 | struct zram *zram = dev_to_zram(dev); |
| 215 | |
| 216 | return sprintf(buf, "%llu\n", |
| 217 | zram_stat64_read(zram, &zram->stats.compr_size)); |
| 218 | } |
| 219 | |
| 220 | static ssize_t mem_used_total_show(struct device *dev, |
| 221 | struct device_attribute *attr, char *buf) |
| 222 | { |
| 223 | u64 val = 0; |
| 224 | struct zram *zram = dev_to_zram(dev); |
| 225 | struct zram_meta *meta = zram->meta; |
| 226 | |
| 227 | down_read(&zram->init_lock); |
| 228 | if (zram->init_done) |
| 229 | val = zs_get_total_size_bytes(meta->mem_pool); |
| 230 | up_read(&zram->init_lock); |
| 231 | |
| 232 | return sprintf(buf, "%llu\n", val); |
| 233 | } |
| 234 | |
| 235 | static DEVICE_ATTR(disksize, S_IRUGO | S_IWUSR, |
| 236 | disksize_show, disksize_store); |
| 237 | static DEVICE_ATTR(initstate, S_IRUGO, initstate_show, NULL); |
| 238 | static DEVICE_ATTR(reset, S_IWUSR, NULL, reset_store); |
| 239 | static DEVICE_ATTR(num_reads, S_IRUGO, num_reads_show, NULL); |
| 240 | static DEVICE_ATTR(num_writes, S_IRUGO, num_writes_show, NULL); |
| 241 | static DEVICE_ATTR(invalid_io, S_IRUGO, invalid_io_show, NULL); |
| 242 | static DEVICE_ATTR(notify_free, S_IRUGO, notify_free_show, NULL); |
| 243 | static DEVICE_ATTR(zero_pages, S_IRUGO, zero_pages_show, NULL); |
| 244 | static DEVICE_ATTR(good_compr_pages, S_IRUGO, good_compr_pages_show, NULL); |
| 245 | static DEVICE_ATTR(bad_compr_pages, S_IRUGO, bad_compr_pages_show, NULL); |
| 246 | static DEVICE_ATTR(orig_data_size, S_IRUGO, orig_data_size_show, NULL); |
| 247 | static DEVICE_ATTR(compr_data_size, S_IRUGO, compr_data_size_show, NULL); |
| 248 | static DEVICE_ATTR(mem_used_total, S_IRUGO, mem_used_total_show, NULL); |
| 249 | |
| 250 | static struct attribute *zram_disk_attrs[] = { |
| 251 | &dev_attr_disksize.attr, |
| 252 | &dev_attr_initstate.attr, |
| 253 | &dev_attr_reset.attr, |
| 254 | &dev_attr_num_reads.attr, |
| 255 | &dev_attr_num_writes.attr, |
| 256 | &dev_attr_invalid_io.attr, |
| 257 | &dev_attr_notify_free.attr, |
| 258 | &dev_attr_zero_pages.attr, |
| 259 | &dev_attr_good_compr_pages.attr, |
| 260 | &dev_attr_bad_compr_pages.attr, |
| 261 | &dev_attr_orig_data_size.attr, |
| 262 | &dev_attr_compr_data_size.attr, |
| 263 | &dev_attr_mem_used_total.attr, |
| 264 | NULL, |
| 265 | }; |
| 266 | |
| 267 | struct attribute_group zram_disk_attr_group = { |
| 268 | .attrs = zram_disk_attrs, |
| 269 | }; |