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> |
| 17 | |
| 18 | #include "zram_drv.h" |
| 19 | |
| 20 | #ifdef CONFIG_SYSFS |
| 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 < num_devices; i++) { |
| 39 | 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 | struct zram *zram = dev_to_zram(dev); |
| 60 | |
Nitin Gupta | 484875a | 2010-08-09 22:56:48 +0530 | [diff] [blame] | 61 | if (zram->init_done) { |
| 62 | pr_info("Cannot change disksize for initialized device\n"); |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 63 | return -EBUSY; |
Nitin Gupta | 484875a | 2010-08-09 22:56:48 +0530 | [diff] [blame] | 64 | } |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 65 | |
| 66 | ret = strict_strtoull(buf, 10, &zram->disksize); |
| 67 | if (ret) |
| 68 | return ret; |
| 69 | |
| 70 | zram->disksize &= PAGE_MASK; |
| 71 | set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT); |
| 72 | |
| 73 | return len; |
| 74 | } |
| 75 | |
| 76 | static ssize_t initstate_show(struct device *dev, |
| 77 | struct device_attribute *attr, char *buf) |
| 78 | { |
| 79 | struct zram *zram = dev_to_zram(dev); |
| 80 | |
| 81 | return sprintf(buf, "%u\n", zram->init_done); |
| 82 | } |
| 83 | |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 84 | static ssize_t reset_store(struct device *dev, |
| 85 | struct device_attribute *attr, const char *buf, size_t len) |
| 86 | { |
| 87 | int ret; |
| 88 | unsigned long do_reset; |
| 89 | struct zram *zram; |
| 90 | struct block_device *bdev; |
| 91 | |
| 92 | zram = dev_to_zram(dev); |
| 93 | bdev = bdget_disk(zram->disk, 0); |
| 94 | |
| 95 | /* Do not reset an active device! */ |
| 96 | if (bdev->bd_holders) |
| 97 | return -EBUSY; |
| 98 | |
| 99 | ret = strict_strtoul(buf, 10, &do_reset); |
| 100 | if (ret) |
| 101 | return ret; |
| 102 | |
| 103 | if (!do_reset) |
| 104 | return -EINVAL; |
| 105 | |
| 106 | /* Make sure all pending I/O is finished */ |
| 107 | if (bdev) |
| 108 | fsync_bdev(bdev); |
| 109 | |
| 110 | if (zram->init_done) |
| 111 | zram_reset_device(zram); |
| 112 | |
| 113 | return len; |
| 114 | } |
| 115 | |
| 116 | static ssize_t num_reads_show(struct device *dev, |
| 117 | struct device_attribute *attr, char *buf) |
| 118 | { |
| 119 | struct zram *zram = dev_to_zram(dev); |
| 120 | |
| 121 | return sprintf(buf, "%llu\n", |
| 122 | zram_stat64_read(zram, &zram->stats.num_reads)); |
| 123 | } |
| 124 | |
| 125 | static ssize_t num_writes_show(struct device *dev, |
| 126 | struct device_attribute *attr, char *buf) |
| 127 | { |
| 128 | struct zram *zram = dev_to_zram(dev); |
| 129 | |
| 130 | return sprintf(buf, "%llu\n", |
| 131 | zram_stat64_read(zram, &zram->stats.num_writes)); |
| 132 | } |
| 133 | |
| 134 | static ssize_t invalid_io_show(struct device *dev, |
| 135 | struct device_attribute *attr, char *buf) |
| 136 | { |
| 137 | struct zram *zram = dev_to_zram(dev); |
| 138 | |
| 139 | return sprintf(buf, "%llu\n", |
| 140 | zram_stat64_read(zram, &zram->stats.invalid_io)); |
| 141 | } |
| 142 | |
| 143 | static ssize_t notify_free_show(struct device *dev, |
| 144 | struct device_attribute *attr, char *buf) |
| 145 | { |
| 146 | struct zram *zram = dev_to_zram(dev); |
| 147 | |
| 148 | return sprintf(buf, "%llu\n", |
| 149 | zram_stat64_read(zram, &zram->stats.notify_free)); |
| 150 | } |
| 151 | |
| 152 | static ssize_t zero_pages_show(struct device *dev, |
| 153 | struct device_attribute *attr, char *buf) |
| 154 | { |
| 155 | struct zram *zram = dev_to_zram(dev); |
| 156 | |
| 157 | return sprintf(buf, "%u\n", zram->stats.pages_zero); |
| 158 | } |
| 159 | |
| 160 | static ssize_t orig_data_size_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 | (u64)(zram->stats.pages_stored) << PAGE_SHIFT); |
| 167 | } |
| 168 | |
| 169 | static ssize_t compr_data_size_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.compr_size)); |
| 176 | } |
| 177 | |
| 178 | static ssize_t mem_used_total_show(struct device *dev, |
| 179 | struct device_attribute *attr, char *buf) |
| 180 | { |
| 181 | u64 val = 0; |
| 182 | struct zram *zram = dev_to_zram(dev); |
| 183 | |
| 184 | if (zram->init_done) { |
| 185 | val = xv_get_total_size_bytes(zram->mem_pool) + |
| 186 | ((u64)(zram->stats.pages_expand) << PAGE_SHIFT); |
| 187 | } |
| 188 | |
| 189 | return sprintf(buf, "%llu\n", val); |
| 190 | } |
| 191 | |
| 192 | static DEVICE_ATTR(disksize, S_IRUGO | S_IWUGO, |
| 193 | disksize_show, disksize_store); |
Nitin Gupta | 484875a | 2010-08-09 22:56:48 +0530 | [diff] [blame] | 194 | static DEVICE_ATTR(initstate, S_IRUGO, initstate_show, NULL); |
Nitin Gupta | 33863c2 | 2010-08-09 22:56:47 +0530 | [diff] [blame] | 195 | static DEVICE_ATTR(reset, S_IWUGO, NULL, reset_store); |
| 196 | static DEVICE_ATTR(num_reads, S_IRUGO, num_reads_show, NULL); |
| 197 | static DEVICE_ATTR(num_writes, S_IRUGO, num_writes_show, NULL); |
| 198 | static DEVICE_ATTR(invalid_io, S_IRUGO, invalid_io_show, NULL); |
| 199 | static DEVICE_ATTR(notify_free, S_IRUGO, notify_free_show, NULL); |
| 200 | static DEVICE_ATTR(zero_pages, S_IRUGO, zero_pages_show, NULL); |
| 201 | static DEVICE_ATTR(orig_data_size, S_IRUGO, orig_data_size_show, NULL); |
| 202 | static DEVICE_ATTR(compr_data_size, S_IRUGO, compr_data_size_show, NULL); |
| 203 | static DEVICE_ATTR(mem_used_total, S_IRUGO, mem_used_total_show, NULL); |
| 204 | |
| 205 | static struct attribute *zram_disk_attrs[] = { |
| 206 | &dev_attr_disksize.attr, |
| 207 | &dev_attr_initstate.attr, |
| 208 | &dev_attr_reset.attr, |
| 209 | &dev_attr_num_reads.attr, |
| 210 | &dev_attr_num_writes.attr, |
| 211 | &dev_attr_invalid_io.attr, |
| 212 | &dev_attr_notify_free.attr, |
| 213 | &dev_attr_zero_pages.attr, |
| 214 | &dev_attr_orig_data_size.attr, |
| 215 | &dev_attr_compr_data_size.attr, |
| 216 | &dev_attr_mem_used_total.attr, |
| 217 | NULL, |
| 218 | }; |
| 219 | |
| 220 | struct attribute_group zram_disk_attr_group = { |
| 221 | .attrs = zram_disk_attrs, |
| 222 | }; |
| 223 | |
| 224 | #endif /* CONFIG_SYSFS */ |