]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/zram/zram_sysfs.c
Staging: zram: Replace ioctls with sysfs interface
[mv-sheeva.git] / drivers / staging / zram / zram_sysfs.c
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
61         if (zram->init_done)
62                 return -EBUSY;
63
64         ret = strict_strtoull(buf, 10, &zram->disksize);
65         if (ret)
66                 return ret;
67
68         zram->disksize &= PAGE_MASK;
69         set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
70
71         return len;
72 }
73
74 static ssize_t initstate_show(struct device *dev,
75                 struct device_attribute *attr, char *buf)
76 {
77         struct zram *zram = dev_to_zram(dev);
78
79         return sprintf(buf, "%u\n", zram->init_done);
80 }
81
82 static ssize_t initstate_store(struct device *dev,
83                 struct device_attribute *attr, const char *buf, size_t len)
84 {
85         int ret;
86         unsigned long do_init;
87         struct zram *zram = dev_to_zram(dev);
88
89         ret = strict_strtoul(buf, 10, &do_init);
90         if (ret)
91                 return ret;
92
93         if (!do_init)
94                 return -EINVAL;
95
96         zram_init_device(zram);
97
98         return len;
99 }
100
101 static ssize_t reset_store(struct device *dev,
102                 struct device_attribute *attr, const char *buf, size_t len)
103 {
104         int ret;
105         unsigned long do_reset;
106         struct zram *zram;
107         struct block_device *bdev;
108
109         zram = dev_to_zram(dev);
110         bdev = bdget_disk(zram->disk, 0);
111
112         /* Do not reset an active device! */
113         if (bdev->bd_holders)
114                 return -EBUSY;
115
116         ret = strict_strtoul(buf, 10, &do_reset);
117         if (ret)
118                 return ret;
119
120         if (!do_reset)
121                 return -EINVAL;
122
123         /* Make sure all pending I/O is finished */
124         if (bdev)
125                 fsync_bdev(bdev);
126
127         if (zram->init_done)
128                 zram_reset_device(zram);
129
130         return len;
131 }
132
133 static ssize_t num_reads_show(struct device *dev,
134                 struct device_attribute *attr, char *buf)
135 {
136         struct zram *zram = dev_to_zram(dev);
137
138         return sprintf(buf, "%llu\n",
139                 zram_stat64_read(zram, &zram->stats.num_reads));
140 }
141
142 static ssize_t num_writes_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_writes));
149 }
150
151 static ssize_t invalid_io_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.invalid_io));
158 }
159
160 static ssize_t notify_free_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.notify_free));
167 }
168
169 static ssize_t zero_pages_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, "%u\n", zram->stats.pages_zero);
175 }
176
177 static ssize_t orig_data_size_show(struct device *dev,
178                 struct device_attribute *attr, char *buf)
179 {
180         struct zram *zram = dev_to_zram(dev);
181
182         return sprintf(buf, "%llu\n",
183                 (u64)(zram->stats.pages_stored) << PAGE_SHIFT);
184 }
185
186 static ssize_t compr_data_size_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, "%llu\n",
192                 zram_stat64_read(zram, &zram->stats.compr_size));
193 }
194
195 static ssize_t mem_used_total_show(struct device *dev,
196                 struct device_attribute *attr, char *buf)
197 {
198         u64 val = 0;
199         struct zram *zram = dev_to_zram(dev);
200
201         if (zram->init_done) {
202                 val = xv_get_total_size_bytes(zram->mem_pool) +
203                         ((u64)(zram->stats.pages_expand) << PAGE_SHIFT);
204         }
205
206         return sprintf(buf, "%llu\n", val);
207 }
208
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);
222
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,
235         NULL,
236 };
237
238 struct attribute_group zram_disk_attr_group = {
239         .attrs = zram_disk_attrs,
240 };
241
242 #endif  /* CONFIG_SYSFS */