2 * Compressed RAM block device
4 * Copyright (C) 2008, 2009, 2010 Nitin Gupta
6 * This code is released using a dual license strategy: BSD/GPL
7 * You can choose the licence that better fits your requirements.
9 * Released under the terms of 3-clause BSD License
10 * Released under the terms of GNU General Public License Version 2.0
12 * Project home: http://compcache.googlecode.com/
15 #include <linux/device.h>
16 #include <linux/genhd.h>
18 #include <linux/kernel.h>
22 static u64 zram_stat64_read(struct zram *zram, u64 *v)
26 spin_lock(&zram->stat64_lock);
28 spin_unlock(&zram->stat64_lock);
33 static struct zram *dev_to_zram(struct device *dev)
36 struct zram *zram = NULL;
38 for (i = 0; i < zram_get_num_devices(); i++) {
39 zram = &zram_devices[i];
40 if (disk_to_dev(zram->disk) == dev)
47 static ssize_t disksize_show(struct device *dev,
48 struct device_attribute *attr, char *buf)
50 struct zram *zram = dev_to_zram(dev);
52 return sprintf(buf, "%llu\n", zram->disksize);
55 static ssize_t disksize_store(struct device *dev,
56 struct device_attribute *attr, const char *buf, size_t len)
59 struct zram_meta *meta;
60 struct zram *zram = dev_to_zram(dev);
62 disksize = memparse(buf, NULL);
66 disksize = PAGE_ALIGN(disksize);
67 meta = zram_meta_alloc(disksize);
68 down_write(&zram->init_lock);
69 if (zram->init_done) {
70 up_write(&zram->init_lock);
72 pr_info("Cannot change disksize for initialized device\n");
76 zram->disksize = disksize;
77 set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
78 zram_init_device(zram, meta);
79 up_write(&zram->init_lock);
84 static ssize_t initstate_show(struct device *dev,
85 struct device_attribute *attr, char *buf)
87 struct zram *zram = dev_to_zram(dev);
89 return sprintf(buf, "%u\n", zram->init_done);
92 static ssize_t reset_store(struct device *dev,
93 struct device_attribute *attr, const char *buf, size_t len)
96 unsigned short do_reset;
98 struct block_device *bdev;
100 zram = dev_to_zram(dev);
101 bdev = bdget_disk(zram->disk, 0);
103 /* Do not reset an active device! */
104 if (bdev->bd_holders)
107 ret = kstrtou16(buf, 10, &do_reset);
114 /* Make sure all pending I/O is finished */
118 zram_reset_device(zram);
122 static ssize_t num_reads_show(struct device *dev,
123 struct device_attribute *attr, char *buf)
125 struct zram *zram = dev_to_zram(dev);
127 return sprintf(buf, "%llu\n",
128 zram_stat64_read(zram, &zram->stats.num_reads));
131 static ssize_t num_writes_show(struct device *dev,
132 struct device_attribute *attr, char *buf)
134 struct zram *zram = dev_to_zram(dev);
136 return sprintf(buf, "%llu\n",
137 zram_stat64_read(zram, &zram->stats.num_writes));
140 static ssize_t invalid_io_show(struct device *dev,
141 struct device_attribute *attr, char *buf)
143 struct zram *zram = dev_to_zram(dev);
145 return sprintf(buf, "%llu\n",
146 zram_stat64_read(zram, &zram->stats.invalid_io));
149 static ssize_t notify_free_show(struct device *dev,
150 struct device_attribute *attr, char *buf)
152 struct zram *zram = dev_to_zram(dev);
154 return sprintf(buf, "%llu\n",
155 zram_stat64_read(zram, &zram->stats.notify_free));
158 static ssize_t zero_pages_show(struct device *dev,
159 struct device_attribute *attr, char *buf)
161 struct zram *zram = dev_to_zram(dev);
163 return sprintf(buf, "%u\n", zram->stats.pages_zero);
166 static ssize_t orig_data_size_show(struct device *dev,
167 struct device_attribute *attr, char *buf)
169 struct zram *zram = dev_to_zram(dev);
171 return sprintf(buf, "%llu\n",
172 (u64)(zram->stats.pages_stored) << PAGE_SHIFT);
175 static ssize_t compr_data_size_show(struct device *dev,
176 struct device_attribute *attr, char *buf)
178 struct zram *zram = dev_to_zram(dev);
180 return sprintf(buf, "%llu\n",
181 zram_stat64_read(zram, &zram->stats.compr_size));
184 static ssize_t mem_used_total_show(struct device *dev,
185 struct device_attribute *attr, char *buf)
188 struct zram *zram = dev_to_zram(dev);
189 struct zram_meta *meta = zram->meta;
192 val = zs_get_total_size_bytes(meta->mem_pool);
194 return sprintf(buf, "%llu\n", val);
197 static DEVICE_ATTR(disksize, S_IRUGO | S_IWUSR,
198 disksize_show, disksize_store);
199 static DEVICE_ATTR(initstate, S_IRUGO, initstate_show, NULL);
200 static DEVICE_ATTR(reset, S_IWUSR, NULL, reset_store);
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);
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,
225 struct attribute_group zram_disk_attr_group = {
226 .attrs = zram_disk_attrs,