blob: f2c478c3424e6f40096049206c4b3e181f37d211 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heo6d66f5c2007-09-20 17:31:38 +09002 * fs/sysfs/bin.c - sysfs binary file implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Matthew Wilcox
6 * Copyright (c) 2004 Silicon Graphics, Inc.
Tejun Heo6d66f5c2007-09-20 17:31:38 +09007 * Copyright (c) 2007 SUSE Linux Products GmbH
8 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
9 *
10 * This file is released under the GPLv2.
11 *
12 * Please see Documentation/filesystems/sysfs.txt for more information.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
14
15#undef DEBUG
16
17#include <linux/errno.h>
18#include <linux/fs.h>
Randy.Dunlap995982c2006-07-10 23:05:25 -070019#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/kobject.h>
21#include <linux/module.h>
22#include <linux/slab.h>
Dave Young869512a2007-07-26 14:53:53 +000023#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/uaccess.h>
26
27#include "sysfs.h"
28
Tejun Heoeb361652007-06-14 03:45:16 +090029struct bin_buffer {
30 struct mutex mutex;
31 void *buffer;
Tejun Heo0ab66082007-06-14 03:45:16 +090032 int mmapped;
Tejun Heoeb361652007-06-14 03:45:16 +090033};
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static int
36fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count)
37{
Tejun Heo3e519032007-06-14 03:45:15 +090038 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +090039 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
40 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Tejun Heo0ab66082007-06-14 03:45:16 +090041 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Tejun Heo0ab66082007-06-14 03:45:16 +090043 /* need attr_sd for attr, its parent for kobj */
44 if (!sysfs_get_active_two(attr_sd))
45 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Tejun Heo0ab66082007-06-14 03:45:16 +090047 rc = -EIO;
48 if (attr->read)
Zhang Rui91a69022007-06-09 13:57:22 +080049 rc = attr->read(kobj, attr, buffer, off, count);
Tejun Heo0ab66082007-06-14 03:45:16 +090050
51 sysfs_put_active_two(attr_sd);
52
53 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
56static ssize_t
Tejun Heo93e3cd822007-06-14 03:45:13 +090057read(struct file *file, char __user *userbuf, size_t bytes, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Tejun Heoeb361652007-06-14 03:45:16 +090059 struct bin_buffer *bb = file->private_data;
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -080060 struct dentry *dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 int size = dentry->d_inode->i_size;
62 loff_t offs = *off;
Tejun Heo93e3cd822007-06-14 03:45:13 +090063 int count = min_t(size_t, bytes, PAGE_SIZE);
Nick Pigginb31ca3f2008-09-12 11:24:11 +020064 char *temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Greg Kroah-Hartman4503efd2009-01-20 15:51:16 -080066 if (!bytes)
67 return 0;
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (size) {
70 if (offs > size)
71 return 0;
72 if (offs + count > size)
73 count = size - offs;
74 }
75
Nick Pigginb31ca3f2008-09-12 11:24:11 +020076 temp = kmalloc(count, GFP_KERNEL);
77 if (!temp)
78 return -ENOMEM;
79
Tejun Heoeb361652007-06-14 03:45:16 +090080 mutex_lock(&bb->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Tejun Heoeb361652007-06-14 03:45:16 +090082 count = fill_read(dentry, bb->buffer, offs, count);
Nick Pigginb31ca3f2008-09-12 11:24:11 +020083 if (count < 0) {
84 mutex_unlock(&bb->mutex);
85 goto out_free;
86 }
Tejun Heoeb361652007-06-14 03:45:16 +090087
Nick Pigginb31ca3f2008-09-12 11:24:11 +020088 memcpy(temp, bb->buffer, count);
89
90 mutex_unlock(&bb->mutex);
91
92 if (copy_to_user(userbuf, temp, count)) {
Tejun Heoeb361652007-06-14 03:45:16 +090093 count = -EFAULT;
Nick Pigginb31ca3f2008-09-12 11:24:11 +020094 goto out_free;
Tejun Heoeb361652007-06-14 03:45:16 +090095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Tejun Heo93e3cd822007-06-14 03:45:13 +090097 pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 *off = offs + count;
100
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200101 out_free:
102 kfree(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return count;
104}
105
106static int
107flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count)
108{
Tejun Heo3e519032007-06-14 03:45:15 +0900109 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900110 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
111 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Tejun Heo0ab66082007-06-14 03:45:16 +0900112 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Tejun Heo0ab66082007-06-14 03:45:16 +0900114 /* need attr_sd for attr, its parent for kobj */
115 if (!sysfs_get_active_two(attr_sd))
116 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Tejun Heo0ab66082007-06-14 03:45:16 +0900118 rc = -EIO;
119 if (attr->write)
Zhang Rui91a69022007-06-09 13:57:22 +0800120 rc = attr->write(kobj, attr, buffer, offset, count);
Tejun Heo0ab66082007-06-14 03:45:16 +0900121
122 sysfs_put_active_two(attr_sd);
123
124 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Tejun Heo93e3cd822007-06-14 03:45:13 +0900127static ssize_t write(struct file *file, const char __user *userbuf,
128 size_t bytes, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Tejun Heoeb361652007-06-14 03:45:16 +0900130 struct bin_buffer *bb = file->private_data;
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800131 struct dentry *dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 int size = dentry->d_inode->i_size;
133 loff_t offs = *off;
Tejun Heo93e3cd822007-06-14 03:45:13 +0900134 int count = min_t(size_t, bytes, PAGE_SIZE);
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200135 char *temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Greg Kroah-Hartman4503efd2009-01-20 15:51:16 -0800137 if (!bytes)
138 return 0;
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (size) {
141 if (offs > size)
142 return 0;
143 if (offs + count > size)
144 count = size - offs;
145 }
146
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200147 temp = kmalloc(count, GFP_KERNEL);
148 if (!temp)
149 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200151 if (copy_from_user(temp, userbuf, count)) {
Tejun Heoeb361652007-06-14 03:45:16 +0900152 count = -EFAULT;
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200153 goto out_free;
Tejun Heoeb361652007-06-14 03:45:16 +0900154 }
155
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200156 mutex_lock(&bb->mutex);
157
158 memcpy(bb->buffer, temp, count);
159
Tejun Heoeb361652007-06-14 03:45:16 +0900160 count = flush_write(dentry, bb->buffer, offs, count);
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200161 mutex_unlock(&bb->mutex);
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (count > 0)
164 *off = offs + count;
Tejun Heoeb361652007-06-14 03:45:16 +0900165
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200166out_free:
167 kfree(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return count;
169}
170
171static int mmap(struct file *file, struct vm_area_struct *vma)
172{
Tejun Heoeb361652007-06-14 03:45:16 +0900173 struct bin_buffer *bb = file->private_data;
Tejun Heo3e519032007-06-14 03:45:15 +0900174 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900175 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
176 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Tejun Heoeb361652007-06-14 03:45:16 +0900177 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Tejun Heoeb361652007-06-14 03:45:16 +0900179 mutex_lock(&bb->mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900180
181 /* need attr_sd for attr, its parent for kobj */
182 if (!sysfs_get_active_two(attr_sd))
183 return -ENODEV;
184
185 rc = -EINVAL;
186 if (attr->mmap)
187 rc = attr->mmap(kobj, attr, vma);
188
189 if (rc == 0 && !bb->mmapped)
190 bb->mmapped = 1;
191 else
192 sysfs_put_active_two(attr_sd);
193
Tejun Heoeb361652007-06-14 03:45:16 +0900194 mutex_unlock(&bb->mutex);
195
196 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
199static int open(struct inode * inode, struct file * file)
200{
Tejun Heo3e519032007-06-14 03:45:15 +0900201 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900202 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
Tejun Heoeb361652007-06-14 03:45:16 +0900203 struct bin_buffer *bb = NULL;
Tejun Heo0ab66082007-06-14 03:45:16 +0900204 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Tejun Heo078ce642007-09-20 16:05:10 +0900206 /* binary file operations requires both @sd and its parent */
207 if (!sysfs_get_active_two(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900208 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 error = -EACCES;
211 if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap))
Tejun Heo7b595752007-06-14 03:45:17 +0900212 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap))
Tejun Heo7b595752007-06-14 03:45:17 +0900214 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 error = -ENOMEM;
Tejun Heoeb361652007-06-14 03:45:16 +0900217 bb = kzalloc(sizeof(*bb), GFP_KERNEL);
218 if (!bb)
Tejun Heo7b595752007-06-14 03:45:17 +0900219 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Tejun Heoeb361652007-06-14 03:45:16 +0900221 bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
222 if (!bb->buffer)
Tejun Heo7b595752007-06-14 03:45:17 +0900223 goto err_out;
Tejun Heoeb361652007-06-14 03:45:16 +0900224
225 mutex_init(&bb->mutex);
226 file->private_data = bb;
227
Tejun Heo078ce642007-09-20 16:05:10 +0900228 /* open succeeded, put active references */
229 sysfs_put_active_two(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900230 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Tejun Heo7b595752007-06-14 03:45:17 +0900232 err_out:
Tejun Heo078ce642007-09-20 16:05:10 +0900233 sysfs_put_active_two(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900234 kfree(bb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return error;
236}
237
238static int release(struct inode * inode, struct file * file)
239{
Tejun Heo3e519032007-06-14 03:45:15 +0900240 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heoeb361652007-06-14 03:45:16 +0900241 struct bin_buffer *bb = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Tejun Heo0ab66082007-06-14 03:45:16 +0900243 if (bb->mmapped)
244 sysfs_put_active_two(attr_sd);
Tejun Heoeb361652007-06-14 03:45:16 +0900245 kfree(bb->buffer);
246 kfree(bb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return 0;
248}
249
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800250const struct file_operations bin_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 .read = read,
252 .write = write,
253 .mmap = mmap,
254 .llseek = generic_file_llseek,
255 .open = open,
256 .release = release,
257};
258
259/**
260 * sysfs_create_bin_file - create binary file for object.
261 * @kobj: object.
262 * @attr: attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 */
264
265int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr)
266{
Tejun Heo608e2662007-06-14 04:27:22 +0900267 BUG_ON(!kobj || !kobj->sd || !attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Tejun Heo608e2662007-06-14 04:27:22 +0900269 return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
272
273/**
274 * sysfs_remove_bin_file - remove binary file for object.
275 * @kobj: object.
276 * @attr: attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 */
278
Randy.Dunlap995982c2006-07-10 23:05:25 -0700279void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Alan Stern5f1835d2007-08-16 16:13:06 -0400281 sysfs_hash_and_remove(kobj->sd, attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
284EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
285EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);