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>
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 < num_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 *zram = dev_to_zram(dev);
64 ret = strict_strtoull(buf, 10, &zram->disksize);
68 zram->disksize &= PAGE_MASK;
69 set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
74 static ssize_t initstate_show(struct device *dev,
75 struct device_attribute *attr, char *buf)
77 struct zram *zram = dev_to_zram(dev);
79 return sprintf(buf, "%u\n", zram->init_done);
82 static ssize_t initstate_store(struct device *dev,
83 struct device_attribute *attr, const char *buf, size_t len)
86 unsigned long do_init;
87 struct zram *zram = dev_to_zram(dev);
89 ret = strict_strtoul(buf, 10, &do_init);
96 zram_init_device(zram);
101 static ssize_t reset_store(struct device *dev,
102 struct device_attribute *attr, const char *buf, size_t len)
105 unsigned long do_reset;
107 struct block_device *bdev;
109 zram = dev_to_zram(dev);
110 bdev = bdget_disk(zram->disk, 0);
112 /* Do not reset an active device! */
113 if (bdev->bd_holders)
116 ret = strict_strtoul(buf, 10, &do_reset);
123 /* Make sure all pending I/O is finished */
128 zram_reset_device(zram);
133 static ssize_t num_reads_show(struct device *dev,
134 struct device_attribute *attr, char *buf)
136 struct zram *zram = dev_to_zram(dev);
138 return sprintf(buf, "%llu\n",
139 zram_stat64_read(zram, &zram->stats.num_reads));
142 static ssize_t num_writes_show(struct device *dev,
143 struct device_attribute *attr, char *buf)
145 struct zram *zram = dev_to_zram(dev);
147 return sprintf(buf, "%llu\n",
148 zram_stat64_read(zram, &zram->stats.num_writes));
151 static ssize_t invalid_io_show(struct device *dev,
152 struct device_attribute *attr, char *buf)
154 struct zram *zram = dev_to_zram(dev);
156 return sprintf(buf, "%llu\n",
157 zram_stat64_read(zram, &zram->stats.invalid_io));
160 static ssize_t notify_free_show(struct device *dev,
161 struct device_attribute *attr, char *buf)
163 struct zram *zram = dev_to_zram(dev);
165 return sprintf(buf, "%llu\n",
166 zram_stat64_read(zram, &zram->stats.notify_free));
169 static ssize_t zero_pages_show(struct device *dev,
170 struct device_attribute *attr, char *buf)
172 struct zram *zram = dev_to_zram(dev);
174 return sprintf(buf, "%u\n", zram->stats.pages_zero);
177 static ssize_t orig_data_size_show(struct device *dev,
178 struct device_attribute *attr, char *buf)
180 struct zram *zram = dev_to_zram(dev);
182 return sprintf(buf, "%llu\n",
183 (u64)(zram->stats.pages_stored) << PAGE_SHIFT);
186 static ssize_t compr_data_size_show(struct device *dev,
187 struct device_attribute *attr, char *buf)
189 struct zram *zram = dev_to_zram(dev);
191 return sprintf(buf, "%llu\n",
192 zram_stat64_read(zram, &zram->stats.compr_size));
195 static ssize_t mem_used_total_show(struct device *dev,
196 struct device_attribute *attr, char *buf)
199 struct zram *zram = dev_to_zram(dev);
201 if (zram->init_done) {
202 val = xv_get_total_size_bytes(zram->mem_pool) +
203 ((u64)(zram->stats.pages_expand) << PAGE_SHIFT);
206 return sprintf(buf, "%llu\n", val);
209 static DEVICE_ATTR(disksize, S_IRUGO | S_IWUGO,
210 disksize_show, disksize_store);
211 static DEVICE_ATTR(initstate, S_IRUGO | S_IWUGO,
212 initstate_show, initstate_store);
213 static DEVICE_ATTR(reset, S_IWUGO, NULL, reset_store);
214 static DEVICE_ATTR(num_reads, S_IRUGO, num_reads_show, NULL);
215 static DEVICE_ATTR(num_writes, S_IRUGO, num_writes_show, NULL);
216 static DEVICE_ATTR(invalid_io, S_IRUGO, invalid_io_show, NULL);
217 static DEVICE_ATTR(notify_free, S_IRUGO, notify_free_show, NULL);
218 static DEVICE_ATTR(zero_pages, S_IRUGO, zero_pages_show, NULL);
219 static DEVICE_ATTR(orig_data_size, S_IRUGO, orig_data_size_show, NULL);
220 static DEVICE_ATTR(compr_data_size, S_IRUGO, compr_data_size_show, NULL);
221 static DEVICE_ATTR(mem_used_total, S_IRUGO, mem_used_total_show, NULL);
223 static struct attribute *zram_disk_attrs[] = {
224 &dev_attr_disksize.attr,
225 &dev_attr_initstate.attr,
226 &dev_attr_reset.attr,
227 &dev_attr_num_reads.attr,
228 &dev_attr_num_writes.attr,
229 &dev_attr_invalid_io.attr,
230 &dev_attr_notify_free.attr,
231 &dev_attr_zero_pages.attr,
232 &dev_attr_orig_data_size.attr,
233 &dev_attr_compr_data_size.attr,
234 &dev_attr_mem_used_total.attr,
238 struct attribute_group zram_disk_attr_group = {
239 .attrs = zram_disk_attrs,
242 #endif /* CONFIG_SYSFS */