Staging: zram: fix up some sysfs attribute permissions
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / zram / zram_sysfs.c
blobc9b52b941d6812e6c28aef2a2a4c944c296cdac4
1 /*
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 "zram_drv.h"
20 #ifdef CONFIG_SYSFS
22 static u64 zram_stat64_read(struct zram *zram, u64 *v)
24 u64 val;
26 spin_lock(&zram->stat64_lock);
27 val = *v;
28 spin_unlock(&zram->stat64_lock);
30 return val;
33 static struct zram *dev_to_zram(struct device *dev)
35 int i;
36 struct zram *zram = NULL;
38 for (i = 0; i < num_devices; i++) {
39 zram = &devices[i];
40 if (disk_to_dev(zram->disk) == dev)
41 break;
44 return zram;
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)
58 int ret;
59 struct zram *zram = dev_to_zram(dev);
61 if (zram->init_done) {
62 pr_info("Cannot change disksize for initialized device\n");
63 return -EBUSY;
66 ret = strict_strtoull(buf, 10, &zram->disksize);
67 if (ret)
68 return ret;
70 zram->disksize &= PAGE_MASK;
71 set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
73 return len;
76 static ssize_t initstate_show(struct device *dev,
77 struct device_attribute *attr, char *buf)
79 struct zram *zram = dev_to_zram(dev);
81 return sprintf(buf, "%u\n", zram->init_done);
84 static ssize_t reset_store(struct device *dev,
85 struct device_attribute *attr, const char *buf, size_t len)
87 int ret;
88 unsigned long do_reset;
89 struct zram *zram;
90 struct block_device *bdev;
92 zram = dev_to_zram(dev);
93 bdev = bdget_disk(zram->disk, 0);
95 /* Do not reset an active device! */
96 if (bdev->bd_holders)
97 return -EBUSY;
99 ret = strict_strtoul(buf, 10, &do_reset);
100 if (ret)
101 return ret;
103 if (!do_reset)
104 return -EINVAL;
106 /* Make sure all pending I/O is finished */
107 if (bdev)
108 fsync_bdev(bdev);
110 if (zram->init_done)
111 zram_reset_device(zram);
113 return len;
116 static ssize_t num_reads_show(struct device *dev,
117 struct device_attribute *attr, char *buf)
119 struct zram *zram = dev_to_zram(dev);
121 return sprintf(buf, "%llu\n",
122 zram_stat64_read(zram, &zram->stats.num_reads));
125 static ssize_t num_writes_show(struct device *dev,
126 struct device_attribute *attr, char *buf)
128 struct zram *zram = dev_to_zram(dev);
130 return sprintf(buf, "%llu\n",
131 zram_stat64_read(zram, &zram->stats.num_writes));
134 static ssize_t invalid_io_show(struct device *dev,
135 struct device_attribute *attr, char *buf)
137 struct zram *zram = dev_to_zram(dev);
139 return sprintf(buf, "%llu\n",
140 zram_stat64_read(zram, &zram->stats.invalid_io));
143 static ssize_t notify_free_show(struct device *dev,
144 struct device_attribute *attr, char *buf)
146 struct zram *zram = dev_to_zram(dev);
148 return sprintf(buf, "%llu\n",
149 zram_stat64_read(zram, &zram->stats.notify_free));
152 static ssize_t zero_pages_show(struct device *dev,
153 struct device_attribute *attr, char *buf)
155 struct zram *zram = dev_to_zram(dev);
157 return sprintf(buf, "%u\n", zram->stats.pages_zero);
160 static ssize_t orig_data_size_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 (u64)(zram->stats.pages_stored) << PAGE_SHIFT);
169 static ssize_t compr_data_size_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.compr_size));
178 static ssize_t mem_used_total_show(struct device *dev,
179 struct device_attribute *attr, char *buf)
181 u64 val = 0;
182 struct zram *zram = dev_to_zram(dev);
184 if (zram->init_done) {
185 val = xv_get_total_size_bytes(zram->mem_pool) +
186 ((u64)(zram->stats.pages_expand) << PAGE_SHIFT);
189 return sprintf(buf, "%llu\n", val);
192 static DEVICE_ATTR(disksize, S_IRUGO | S_IRUSR,
193 disksize_show, disksize_store);
194 static DEVICE_ATTR(initstate, S_IRUGO, initstate_show, NULL);
195 static DEVICE_ATTR(reset, S_IRUSR, NULL, reset_store);
196 static DEVICE_ATTR(num_reads, S_IRUGO, num_reads_show, NULL);
197 static DEVICE_ATTR(num_writes, S_IRUGO, num_writes_show, NULL);
198 static DEVICE_ATTR(invalid_io, S_IRUGO, invalid_io_show, NULL);
199 static DEVICE_ATTR(notify_free, S_IRUGO, notify_free_show, NULL);
200 static DEVICE_ATTR(zero_pages, S_IRUGO, zero_pages_show, NULL);
201 static DEVICE_ATTR(orig_data_size, S_IRUGO, orig_data_size_show, NULL);
202 static DEVICE_ATTR(compr_data_size, S_IRUGO, compr_data_size_show, NULL);
203 static DEVICE_ATTR(mem_used_total, S_IRUGO, mem_used_total_show, NULL);
205 static struct attribute *zram_disk_attrs[] = {
206 &dev_attr_disksize.attr,
207 &dev_attr_initstate.attr,
208 &dev_attr_reset.attr,
209 &dev_attr_num_reads.attr,
210 &dev_attr_num_writes.attr,
211 &dev_attr_invalid_io.attr,
212 &dev_attr_notify_free.attr,
213 &dev_attr_zero_pages.attr,
214 &dev_attr_orig_data_size.attr,
215 &dev_attr_compr_data_size.attr,
216 &dev_attr_mem_used_total.attr,
217 NULL,
220 struct attribute_group zram_disk_attr_group = {
221 .attrs = zram_disk_attrs,
224 #endif /* CONFIG_SYSFS */