blob: 618b8aea6a7ba869b15f6d35731440d945631641 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * bin.c - binary file operations for sysfs.
3 *
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Matthew Wilcox
6 * Copyright (c) 2004 Silicon Graphics, Inc.
7 */
8
9#undef DEBUG
10
11#include <linux/errno.h>
12#include <linux/fs.h>
Randy.Dunlap995982c2006-07-10 23:05:25 -070013#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kobject.h>
15#include <linux/module.h>
16#include <linux/slab.h>
17
18#include <asm/uaccess.h>
Oliver Neukum94bebf42006-12-20 10:52:44 +010019#include <asm/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include "sysfs.h"
22
Tejun Heoeb361652007-06-14 03:45:16 +090023struct bin_buffer {
24 struct mutex mutex;
25 void *buffer;
Tejun Heo0ab66082007-06-14 03:45:16 +090026 int mmapped;
Tejun Heoeb361652007-06-14 03:45:16 +090027};
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static int
30fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count)
31{
Tejun Heo3e519032007-06-14 03:45:15 +090032 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
33 struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr;
Tejun Heo0ab66082007-06-14 03:45:16 +090034 struct kobject *kobj = attr_sd->s_parent->s_elem.dir.kobj;
35 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Tejun Heo0ab66082007-06-14 03:45:16 +090037 /* need attr_sd for attr, its parent for kobj */
38 if (!sysfs_get_active_two(attr_sd))
39 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Tejun Heo0ab66082007-06-14 03:45:16 +090041 rc = -EIO;
42 if (attr->read)
43 rc = attr->read(kobj, buffer, off, count);
44
45 sysfs_put_active_two(attr_sd);
46
47 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
50static ssize_t
Tejun Heo93e3cd822007-06-14 03:45:13 +090051read(struct file *file, char __user *userbuf, size_t bytes, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Tejun Heoeb361652007-06-14 03:45:16 +090053 struct bin_buffer *bb = file->private_data;
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -080054 struct dentry *dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 int size = dentry->d_inode->i_size;
56 loff_t offs = *off;
Tejun Heo93e3cd822007-06-14 03:45:13 +090057 int count = min_t(size_t, bytes, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 if (size) {
60 if (offs > size)
61 return 0;
62 if (offs + count > size)
63 count = size - offs;
64 }
65
Tejun Heoeb361652007-06-14 03:45:16 +090066 mutex_lock(&bb->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Tejun Heoeb361652007-06-14 03:45:16 +090068 count = fill_read(dentry, bb->buffer, offs, count);
69 if (count < 0)
70 goto out_unlock;
71
72 if (copy_to_user(userbuf, bb->buffer, count)) {
73 count = -EFAULT;
74 goto out_unlock;
75 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Tejun Heo93e3cd822007-06-14 03:45:13 +090077 pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 *off = offs + count;
80
Tejun Heoeb361652007-06-14 03:45:16 +090081 out_unlock:
82 mutex_unlock(&bb->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return count;
84}
85
86static int
87flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count)
88{
Tejun Heo3e519032007-06-14 03:45:15 +090089 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
90 struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr;
Tejun Heo0ab66082007-06-14 03:45:16 +090091 struct kobject *kobj = attr_sd->s_parent->s_elem.dir.kobj;
92 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Tejun Heo0ab66082007-06-14 03:45:16 +090094 /* need attr_sd for attr, its parent for kobj */
95 if (!sysfs_get_active_two(attr_sd))
96 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Tejun Heo0ab66082007-06-14 03:45:16 +090098 rc = -EIO;
99 if (attr->write)
100 rc = attr->write(kobj, buffer, offset, count);
101
102 sysfs_put_active_two(attr_sd);
103
104 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Tejun Heo93e3cd822007-06-14 03:45:13 +0900107static ssize_t write(struct file *file, const char __user *userbuf,
108 size_t bytes, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Tejun Heoeb361652007-06-14 03:45:16 +0900110 struct bin_buffer *bb = file->private_data;
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800111 struct dentry *dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 int size = dentry->d_inode->i_size;
113 loff_t offs = *off;
Tejun Heo93e3cd822007-06-14 03:45:13 +0900114 int count = min_t(size_t, bytes, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (size) {
117 if (offs > size)
118 return 0;
119 if (offs + count > size)
120 count = size - offs;
121 }
122
Tejun Heoeb361652007-06-14 03:45:16 +0900123 mutex_lock(&bb->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Tejun Heoeb361652007-06-14 03:45:16 +0900125 if (copy_from_user(bb->buffer, userbuf, count)) {
126 count = -EFAULT;
127 goto out_unlock;
128 }
129
130 count = flush_write(dentry, bb->buffer, offs, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (count > 0)
132 *off = offs + count;
Tejun Heoeb361652007-06-14 03:45:16 +0900133
134 out_unlock:
135 mutex_unlock(&bb->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return count;
137}
138
139static int mmap(struct file *file, struct vm_area_struct *vma)
140{
Tejun Heoeb361652007-06-14 03:45:16 +0900141 struct bin_buffer *bb = file->private_data;
Tejun Heo3e519032007-06-14 03:45:15 +0900142 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
143 struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr;
Tejun Heo0ab66082007-06-14 03:45:16 +0900144 struct kobject *kobj = attr_sd->s_parent->s_elem.dir.kobj;
Tejun Heoeb361652007-06-14 03:45:16 +0900145 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Tejun Heoeb361652007-06-14 03:45:16 +0900147 mutex_lock(&bb->mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900148
149 /* need attr_sd for attr, its parent for kobj */
150 if (!sysfs_get_active_two(attr_sd))
151 return -ENODEV;
152
153 rc = -EINVAL;
154 if (attr->mmap)
155 rc = attr->mmap(kobj, attr, vma);
156
157 if (rc == 0 && !bb->mmapped)
158 bb->mmapped = 1;
159 else
160 sysfs_put_active_two(attr_sd);
161
Tejun Heoeb361652007-06-14 03:45:16 +0900162 mutex_unlock(&bb->mutex);
163
164 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
167static int open(struct inode * inode, struct file * file)
168{
Tejun Heo3e519032007-06-14 03:45:15 +0900169 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
170 struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr;
Tejun Heoeb361652007-06-14 03:45:16 +0900171 struct bin_buffer *bb = NULL;
Tejun Heo0ab66082007-06-14 03:45:16 +0900172 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Tejun Heo0ab66082007-06-14 03:45:16 +0900174 /* need attr_sd for attr */
175 if (!sysfs_get_active(attr_sd))
176 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Tejun Heo0ab66082007-06-14 03:45:16 +0900178 /* Grab the module reference for this attribute */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 error = -ENODEV;
Tejun Heo0ab66082007-06-14 03:45:16 +0900180 if (!try_module_get(attr->attr.owner))
181 goto err_sput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 error = -EACCES;
184 if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap))
Tejun Heo0ab66082007-06-14 03:45:16 +0900185 goto err_mput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap))
Tejun Heo0ab66082007-06-14 03:45:16 +0900187 goto err_mput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 error = -ENOMEM;
Tejun Heoeb361652007-06-14 03:45:16 +0900190 bb = kzalloc(sizeof(*bb), GFP_KERNEL);
191 if (!bb)
Tejun Heo0ab66082007-06-14 03:45:16 +0900192 goto err_mput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Tejun Heoeb361652007-06-14 03:45:16 +0900194 bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
195 if (!bb->buffer)
Tejun Heo0ab66082007-06-14 03:45:16 +0900196 goto err_mput;
Tejun Heoeb361652007-06-14 03:45:16 +0900197
198 mutex_init(&bb->mutex);
199 file->private_data = bb;
200
Tejun Heo0ab66082007-06-14 03:45:16 +0900201 /* open succeeded, put active reference and pin attr_sd */
202 sysfs_put_active(attr_sd);
203 sysfs_get(attr_sd);
204 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Tejun Heo0ab66082007-06-14 03:45:16 +0900206 err_mput:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 module_put(attr->attr.owner);
Tejun Heo0ab66082007-06-14 03:45:16 +0900208 err_sput:
209 sysfs_put_active(attr_sd);
210 kfree(bb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return error;
212}
213
214static int release(struct inode * inode, struct file * file)
215{
Tejun Heo3e519032007-06-14 03:45:15 +0900216 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
217 struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr;
Tejun Heoeb361652007-06-14 03:45:16 +0900218 struct bin_buffer *bb = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Tejun Heo0ab66082007-06-14 03:45:16 +0900220 if (bb->mmapped)
221 sysfs_put_active_two(attr_sd);
222 sysfs_put(attr_sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 module_put(attr->attr.owner);
Tejun Heoeb361652007-06-14 03:45:16 +0900224 kfree(bb->buffer);
225 kfree(bb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return 0;
227}
228
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800229const struct file_operations bin_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 .read = read,
231 .write = write,
232 .mmap = mmap,
233 .llseek = generic_file_llseek,
234 .open = open,
235 .release = release,
236};
237
238/**
239 * sysfs_create_bin_file - create binary file for object.
240 * @kobj: object.
241 * @attr: attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 */
243
244int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr)
245{
246 BUG_ON(!kobj || !kobj->dentry || !attr);
247
248 return sysfs_add_file(kobj->dentry, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
249}
250
251
252/**
253 * sysfs_remove_bin_file - remove binary file for object.
254 * @kobj: object.
255 * @attr: attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 */
257
Randy.Dunlap995982c2006-07-10 23:05:25 -0700258void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Randy.Dunlap995982c2006-07-10 23:05:25 -0700260 if (sysfs_hash_and_remove(kobj->dentry, attr->attr.name) < 0) {
261 printk(KERN_ERR "%s: "
262 "bad dentry or inode or no such file: \"%s\"\n",
263 __FUNCTION__, attr->attr.name);
264 dump_stack();
265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
269EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);