Staging: zram: Fix memory leak by refcount mismatch
[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 < zram_num_devices; i++) {
38                 zram = &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         u64 disksize;
59         struct zram *zram = dev_to_zram(dev);
60
61         ret = strict_strtoull(buf, 10, &disksize);
62         if (ret)
63                 return ret;
64
65         down_write(&zram->init_lock);
66         if (zram->init_done) {
67                 up_write(&zram->init_lock);
68                 pr_info("Cannot change disksize for initialized device\n");
69                 return -EBUSY;
70         }
71
72         zram->disksize = PAGE_ALIGN(disksize);
73         set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
74         up_write(&zram->init_lock);
75
76         return len;
77 }
78
79 static ssize_t initstate_show(struct device *dev,
80                 struct device_attribute *attr, char *buf)
81 {
82         struct zram *zram = dev_to_zram(dev);
83
84         return sprintf(buf, "%u\n", zram->init_done);
85 }
86
87 static ssize_t reset_store(struct device *dev,
88                 struct device_attribute *attr, const char *buf, size_t len)
89 {
90         int ret;
91         unsigned long do_reset;
92         struct zram *zram;
93         struct block_device *bdev;
94
95         zram = dev_to_zram(dev);
96         bdev = bdget_disk(zram->disk, 0);
97
98         if (!bdev)
99                 return -ENOMEM;
100
101         /* Do not reset an active device! */
102         if (bdev->bd_holders) {
103                 ret = -EBUSY;
104                 goto out;
105         }
106
107         ret = strict_strtoul(buf, 10, &do_reset);
108         if (ret)
109                 goto out;
110
111         if (!do_reset) {
112                 ret = -EINVAL;
113                 goto out;
114         }
115
116         /* Make sure all pending I/O is finished */
117         fsync_bdev(bdev);
118         bdput(bdev);
119
120         down_write(&zram->init_lock);
121         if (zram->init_done)
122                 __zram_reset_device(zram);
123         up_write(&zram->init_lock);
124
125         return len;
126
127 out:
128         bdput(bdev);
129         return ret;
130 }
131
132 static ssize_t num_reads_show(struct device *dev,
133                 struct device_attribute *attr, char *buf)
134 {
135         struct zram *zram = dev_to_zram(dev);
136
137         return sprintf(buf, "%llu\n",
138                 zram_stat64_read(zram, &zram->stats.num_reads));
139 }
140
141 static ssize_t num_writes_show(struct device *dev,
142                 struct device_attribute *attr, char *buf)
143 {
144         struct zram *zram = dev_to_zram(dev);
145
146         return sprintf(buf, "%llu\n",
147                 zram_stat64_read(zram, &zram->stats.num_writes));
148 }
149
150 static ssize_t invalid_io_show(struct device *dev,
151                 struct device_attribute *attr, char *buf)
152 {
153         struct zram *zram = dev_to_zram(dev);
154
155         return sprintf(buf, "%llu\n",
156                 zram_stat64_read(zram, &zram->stats.invalid_io));
157 }
158
159 static ssize_t notify_free_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                 zram_stat64_read(zram, &zram->stats.notify_free));
166 }
167
168 static ssize_t zero_pages_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, "%u\n", zram->stats.pages_zero);
174 }
175
176 static ssize_t orig_data_size_show(struct device *dev,
177                 struct device_attribute *attr, char *buf)
178 {
179         struct zram *zram = dev_to_zram(dev);
180
181         return sprintf(buf, "%llu\n",
182                 (u64)(zram->stats.pages_stored) << PAGE_SHIFT);
183 }
184
185 static ssize_t compr_data_size_show(struct device *dev,
186                 struct device_attribute *attr, char *buf)
187 {
188         struct zram *zram = dev_to_zram(dev);
189
190         return sprintf(buf, "%llu\n",
191                 zram_stat64_read(zram, &zram->stats.compr_size));
192 }
193
194 static ssize_t mem_used_total_show(struct device *dev,
195                 struct device_attribute *attr, char *buf)
196 {
197         u64 val = 0;
198         struct zram *zram = dev_to_zram(dev);
199
200         down_read(&zram->init_lock);
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         up_read(&zram->init_lock);
206
207         return sprintf(buf, "%llu\n", val);
208 }
209
210 static DEVICE_ATTR(disksize, S_IRUGO | S_IWUSR,
211                 disksize_show, disksize_store);
212 static DEVICE_ATTR(initstate, S_IRUGO, initstate_show, NULL);
213 static DEVICE_ATTR(reset, S_IWUSR, 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 };