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
)
60 struct zram_meta
*meta
;
61 struct zram
*zram
= dev_to_zram(dev
);
63 /* Get disksize from user */
64 ret
= kstrtoull(buf
, 10, &disksize
);
68 /* If disksize is 0, then we give it a default setting. */
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) ;
79 disksize
= round_up(disksize
, DISKSIZE_ALIGNMENT
);
80 /* disksize = PAGE_ALIGN(disksize); */
82 meta
= zram_meta_alloc(disksize
);
83 /* Check whether meta is null */
85 printk(KERN_ALERT
"Failed to allocate memory for meta!\n");
88 down_write(&zram
->init_lock
);
89 if (zram
->init_done
) {
90 up_write(&zram
->init_lock
);
92 pr_info("Cannot change disksize for initialized device\n");
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
);
104 static ssize_t
initstate_show(struct device
*dev
,
105 struct device_attribute
*attr
, char *buf
)
107 struct zram
*zram
= dev_to_zram(dev
);
109 return sprintf(buf
, "%u\n", zram
->init_done
);
112 static ssize_t
reset_store(struct device
*dev
,
113 struct device_attribute
*attr
, const char *buf
, size_t len
)
116 unsigned short do_reset
;
118 struct block_device
*bdev
;
120 zram
= dev_to_zram(dev
);
121 bdev
= bdget_disk(zram
->disk
, 0);
123 /* Do not reset an active device! */
124 if (bdev
->bd_holders
)
127 ret
= kstrtou16(buf
, 10, &do_reset
);
134 /* Make sure all pending I/O is finished */
138 zram_reset_device(zram
);
142 static ssize_t
num_reads_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_reads
));
151 static ssize_t
num_writes_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
.num_writes
));
160 static ssize_t
invalid_io_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
.invalid_io
));
169 static ssize_t
notify_free_show(struct device
*dev
,
170 struct device_attribute
*attr
, char *buf
)
172 struct zram
*zram
= dev_to_zram(dev
);
174 return sprintf(buf
, "%llu\n",
175 zram_stat64_read(zram
, &zram
->stats
.notify_free
));
178 static ssize_t
zero_pages_show(struct device
*dev
,
179 struct device_attribute
*attr
, char *buf
)
181 struct zram
*zram
= dev_to_zram(dev
);
183 return sprintf(buf
, "%u\n", zram
->stats
.pages_zero
);
186 static ssize_t
good_compr_pages_show(struct device
*dev
,
187 struct device_attribute
*attr
, char *buf
)
189 struct zram
*zram
= dev_to_zram(dev
);
191 return sprintf(buf
, "%u\n", zram
->stats
.good_compress
);
194 static ssize_t
bad_compr_pages_show(struct device
*dev
,
195 struct device_attribute
*attr
, char *buf
)
197 struct zram
*zram
= dev_to_zram(dev
);
199 return sprintf(buf
, "%u\n", zram
->stats
.bad_compress
);
202 static ssize_t
orig_data_size_show(struct device
*dev
,
203 struct device_attribute
*attr
, char *buf
)
205 struct zram
*zram
= dev_to_zram(dev
);
207 return sprintf(buf
, "%llu\n",
208 (u64
)(zram
->stats
.pages_stored
) << PAGE_SHIFT
);
211 static ssize_t
compr_data_size_show(struct device
*dev
,
212 struct device_attribute
*attr
, char *buf
)
214 struct zram
*zram
= dev_to_zram(dev
);
216 return sprintf(buf
, "%llu\n",
217 zram_stat64_read(zram
, &zram
->stats
.compr_size
));
220 static ssize_t
mem_used_total_show(struct device
*dev
,
221 struct device_attribute
*attr
, char *buf
)
224 struct zram
*zram
= dev_to_zram(dev
);
225 struct zram_meta
*meta
= zram
->meta
;
227 down_read(&zram
->init_lock
);
229 val
= zs_get_total_size_bytes(meta
->mem_pool
);
230 up_read(&zram
->init_lock
);
232 return sprintf(buf
, "%llu\n", val
);
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
);
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
,
267 struct attribute_group zram_disk_attr_group
= {
268 .attrs
= zram_disk_attrs
,