blob: 247ea19721c39fcacef7adb8fe9840ac63787ce3 [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>
Dave Young869512a2007-07-26 14:53:53 +000017#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <asm/uaccess.h>
20
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;
Tejun Heob1fc3d62007-09-20 16:05:11 +090033 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
34 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Tejun Heo0ab66082007-06-14 03:45:16 +090035 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 Heo93e3cd82007-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 Heo93e3cd82007-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 Heo93e3cd82007-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;
Tejun Heob1fc3d62007-09-20 16:05:11 +090090 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
91 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Tejun Heo0ab66082007-06-14 03:45:16 +090092 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 Heo93e3cd82007-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 Heo93e3cd82007-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;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900143 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
144 struct kobject *kobj = attr_sd->s_parent->s_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;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900170 struct bin_attribute *attr = attr_sd->s_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 Heo078ce642007-09-20 16:05:10 +0900174 /* binary file operations requires both @sd and its parent */
175 if (!sysfs_get_active_two(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900176 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 Heo078ce642007-09-20 16:05:10 +0900196 /* open succeeded, put active references */
197 sysfs_put_active_two(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900198 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Tejun Heo7b595752007-06-14 03:45:17 +0900200 err_out:
Tejun Heo078ce642007-09-20 16:05:10 +0900201 sysfs_put_active_two(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900202 kfree(bb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return error;
204}
205
206static int release(struct inode * inode, struct file * file)
207{
Tejun Heo3e519032007-06-14 03:45:15 +0900208 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heoeb361652007-06-14 03:45:16 +0900209 struct bin_buffer *bb = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Tejun Heo0ab66082007-06-14 03:45:16 +0900211 if (bb->mmapped)
212 sysfs_put_active_two(attr_sd);
Tejun Heoeb361652007-06-14 03:45:16 +0900213 kfree(bb->buffer);
214 kfree(bb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return 0;
216}
217
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800218const struct file_operations bin_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 .read = read,
220 .write = write,
221 .mmap = mmap,
222 .llseek = generic_file_llseek,
223 .open = open,
224 .release = release,
225};
226
227/**
228 * sysfs_create_bin_file - create binary file for object.
229 * @kobj: object.
230 * @attr: attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 */
232
233int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr)
234{
Tejun Heo608e2662007-06-14 04:27:22 +0900235 BUG_ON(!kobj || !kobj->sd || !attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Tejun Heo608e2662007-06-14 04:27:22 +0900237 return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
240
241/**
242 * sysfs_remove_bin_file - remove binary file for object.
243 * @kobj: object.
244 * @attr: attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 */
246
Randy.Dunlap995982c2006-07-10 23:05:25 -0700247void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Alan Stern5f1835d2007-08-16 16:13:06 -0400249 sysfs_hash_and_remove(kobj->sd, attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
253EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);