blob: 5afe2a26f5d877f69b268c1fb827aa8dbea55ea0 [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)
Zhang Rui91a69022007-06-09 13:57:22 +080043 rc = attr->read(kobj, attr, buffer, off, count);
Tejun Heo0ab66082007-06-14 03:45:16 +090044
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)
Zhang Rui91a69022007-06-09 13:57:22 +0800100 rc = attr->write(kobj, attr, buffer, offset, count);
Tejun Heo0ab66082007-06-14 03:45:16 +0900101
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 error = -EACCES;
179 if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap))
Tejun Heo7b595752007-06-14 03:45:17 +0900180 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap))
Tejun Heo7b595752007-06-14 03:45:17 +0900182 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 error = -ENOMEM;
Tejun Heoeb361652007-06-14 03:45:16 +0900185 bb = kzalloc(sizeof(*bb), GFP_KERNEL);
186 if (!bb)
Tejun Heo7b595752007-06-14 03:45:17 +0900187 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Tejun Heoeb361652007-06-14 03:45:16 +0900189 bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
190 if (!bb->buffer)
Tejun Heo7b595752007-06-14 03:45:17 +0900191 goto err_out;
Tejun Heoeb361652007-06-14 03:45:16 +0900192
193 mutex_init(&bb->mutex);
194 file->private_data = bb;
195
Tejun Heo0ab66082007-06-14 03:45:16 +0900196 /* open succeeded, put active reference and pin attr_sd */
197 sysfs_put_active(attr_sd);
198 sysfs_get(attr_sd);
199 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Tejun Heo7b595752007-06-14 03:45:17 +0900201 err_out:
Tejun Heo0ab66082007-06-14 03:45:16 +0900202 sysfs_put_active(attr_sd);
203 kfree(bb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return error;
205}
206
207static int release(struct inode * inode, struct file * file)
208{
Tejun Heo3e519032007-06-14 03:45:15 +0900209 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heoeb361652007-06-14 03:45:16 +0900210 struct bin_buffer *bb = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Tejun Heo0ab66082007-06-14 03:45:16 +0900212 if (bb->mmapped)
213 sysfs_put_active_two(attr_sd);
214 sysfs_put(attr_sd);
Tejun Heoeb361652007-06-14 03:45:16 +0900215 kfree(bb->buffer);
216 kfree(bb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return 0;
218}
219
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800220const struct file_operations bin_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 .read = read,
222 .write = write,
223 .mmap = mmap,
224 .llseek = generic_file_llseek,
225 .open = open,
226 .release = release,
227};
228
229/**
230 * sysfs_create_bin_file - create binary file for object.
231 * @kobj: object.
232 * @attr: attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 */
234
235int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr)
236{
Tejun Heo608e2662007-06-14 04:27:22 +0900237 BUG_ON(!kobj || !kobj->sd || !attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Tejun Heo608e2662007-06-14 04:27:22 +0900239 return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
242
243/**
244 * sysfs_remove_bin_file - remove binary file for object.
245 * @kobj: object.
246 * @attr: attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 */
248
Randy.Dunlap995982c2006-07-10 23:05:25 -0700249void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Alan Stern5f1835d2007-08-16 16:13:06 -0400251 sysfs_hash_and_remove(kobj->sd, attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
254EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
255EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);