Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[pandora-kernel.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 #include <linux/mm.h>
18
19 #include "zram_drv.h"
20
21 static u64 zram_stat64_read(struct zram *zram, u64 *v)
22 {
23         u64 val;
24
25         spin_lock(&zram->stat64_lock);
26         val = *v;
27         spin_unlock(&zram->stat64_lock);
28
29         return val;
30 }
31
32 static struct zram *dev_to_zram(struct device *dev)
33 {
34         int i;
35         struct zram *zram = NULL;
36
37         for (i = 0; i < num_devices; i++) {
38                 zram = &devices[i];
39                 if (disk_to_dev(zram->disk) == dev)
40                         break;
41         }
42
43         return zram;
44 }
45
46 static ssize_t disksize_show(struct device *dev,
47                 struct device_attribute *attr, char *buf)
48 {
49         struct zram *zram = dev_to_zram(dev);
50
51         return sprintf(buf, "%llu\n", zram->disksize);
52 }
53
54 static ssize_t disksize_store(struct device *dev,
55                 struct device_attribute *attr, const char *buf, size_t len)
56 {
57         int ret;
58         struct zram *zram = dev_to_zram(dev);
59
60         if (zram->init_done) {
61                 pr_info("Cannot change disksize for initialized device\n");
62                 return -EBUSY;
63         }
64
65         ret = strict_strtoull(buf, 10, &zram->disksize);
66         if (ret)
67                 return ret;
68
69         zram->disksize = PAGE_ALIGN(zram->disksize);
70         set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
71
72         return len;
73 }
74
75 static ssize_t initstate_show(struct device *dev,
76                 struct device_attribute *attr, char *buf)
77 {
78         struct zram *zram = dev_to_zram(dev);
79
80         return sprintf(buf, "%u\n", zram->init_done);
81 }
82
83 static ssize_t reset_store(struct device *dev,
84                 struct device_attribute *attr, const char *buf, size_t len)
85 {
86         int ret;
87         unsigned long do_reset;
88         struct zram *zram;
89         struct block_device *bdev;
90
91         zram = dev_to_zram(dev);
92         bdev = bdget_disk(zram->disk, 0);
93
94         /* Do not reset an active device! */
95         if (bdev->bd_holders)
96                 return -EBUSY;
97
98         ret = strict_strtoul(buf, 10, &do_reset);
99         if (ret)
100                 return ret;
101
102         if (!do_reset)
103                 return -EINVAL;
104
105         /* Make sure all pending I/O is finished */
106         if (bdev)
107                 fsync_bdev(bdev);
108
109         if (zram->init_done)
110                 zram_reset_device(zram);
111
112         return len;
113 }
114
115 static ssize_t num_reads_show(struct device *dev,
116                 struct device_attribute *attr, char *buf)
117 {
118         struct zram *zram = dev_to_zram(dev);
119
120         return sprintf(buf, "%llu\n",
121                 zram_stat64_read(zram, &zram->stats.num_reads));
122 }
123
124 static ssize_t num_writes_show(struct device *dev,
125                 struct device_attribute *attr, char *buf)
126 {
127         struct zram *zram = dev_to_zram(dev);
128
129         return sprintf(buf, "%llu\n",
130                 zram_stat64_read(zram, &zram->stats.num_writes));
131 }
132
133 static ssize_t invalid_io_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.invalid_io));
140 }
141
142 static ssize_t notify_free_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.notify_free));
149 }
150
151 static ssize_t zero_pages_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, "%u\n", zram->stats.pages_zero);
157 }
158
159 static ssize_t orig_data_size_show(struct device *dev,
160                 struct device_attribute *attr, char *buf)
161 {
162         struct zram *zram = dev_to_zram(dev);
163
164         return sprintf(buf, "%llu\n",
165                 (u64)(zram->stats.pages_stored) << PAGE_SHIFT);
166 }
167
168 static ssize_t compr_data_size_show(struct device *dev,
169                 struct device_attribute *attr, char *buf)
170 {
171         struct zram *zram = dev_to_zram(dev);
172
173         return sprintf(buf, "%llu\n",
174                 zram_stat64_read(zram, &zram->stats.compr_size));
175 }
176
177 static ssize_t mem_used_total_show(struct device *dev,
178                 struct device_attribute *attr, char *buf)
179 {
180         u64 val = 0;
181         struct zram *zram = dev_to_zram(dev);
182
183         if (zram->init_done) {
184                 val = xv_get_total_size_bytes(zram->mem_pool) +
185                         ((u64)(zram->stats.pages_expand) << PAGE_SHIFT);
186         }
187
188         return sprintf(buf, "%llu\n", val);
189 }
190
191 static DEVICE_ATTR(disksize, S_IRUGO | S_IWUSR,
192                 disksize_show, disksize_store);
193 static DEVICE_ATTR(initstate, S_IRUGO, initstate_show, NULL);
194 static DEVICE_ATTR(reset, S_IWUSR, NULL, reset_store);
195 static DEVICE_ATTR(num_reads, S_IRUGO, num_reads_show, NULL);
196 static DEVICE_ATTR(num_writes, S_IRUGO, num_writes_show, NULL);
197 static DEVICE_ATTR(invalid_io, S_IRUGO, invalid_io_show, NULL);
198 static DEVICE_ATTR(notify_free, S_IRUGO, notify_free_show, NULL);
199 static DEVICE_ATTR(zero_pages, S_IRUGO, zero_pages_show, NULL);
200 static DEVICE_ATTR(orig_data_size, S_IRUGO, orig_data_size_show, NULL);
201 static DEVICE_ATTR(compr_data_size, S_IRUGO, compr_data_size_show, NULL);
202 static DEVICE_ATTR(mem_used_total, S_IRUGO, mem_used_total_show, NULL);
203
204 static struct attribute *zram_disk_attrs[] = {
205         &dev_attr_disksize.attr,
206         &dev_attr_initstate.attr,
207         &dev_attr_reset.attr,
208         &dev_attr_num_reads.attr,
209         &dev_attr_num_writes.attr,
210         &dev_attr_invalid_io.attr,
211         &dev_attr_notify_free.attr,
212         &dev_attr_zero_pages.attr,
213         &dev_attr_orig_data_size.attr,
214         &dev_attr_compr_data_size.attr,
215         &dev_attr_mem_used_total.attr,
216         NULL,
217 };
218
219 struct attribute_group zram_disk_attr_group = {
220         .attrs = zram_disk_attrs,
221 };