blob: 66f6e58a7e4bb1a52590f4eecbb4094647c61c4f [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 Heo93e3cd82007-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 Heo93e3cd82007-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
66 if (size) {
67 if (offs > size)
68 return 0;
69 if (offs + count > size)
70 count = size - offs;
71 }
72
Nick Pigginb31ca3f2008-09-12 11:24:11 +020073 temp = kmalloc(count, GFP_KERNEL);
74 if (!temp)
75 return -ENOMEM;
76
Tejun Heoeb361652007-06-14 03:45:16 +090077 mutex_lock(&bb->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Tejun Heoeb361652007-06-14 03:45:16 +090079 count = fill_read(dentry, bb->buffer, offs, count);
Nick Pigginb31ca3f2008-09-12 11:24:11 +020080 if (count < 0) {
81 mutex_unlock(&bb->mutex);
82 goto out_free;
83 }
Tejun Heoeb361652007-06-14 03:45:16 +090084
Nick Pigginb31ca3f2008-09-12 11:24:11 +020085 memcpy(temp, bb->buffer, count);
86
87 mutex_unlock(&bb->mutex);
88
89 if (copy_to_user(userbuf, temp, count)) {
Tejun Heoeb361652007-06-14 03:45:16 +090090 count = -EFAULT;
Nick Pigginb31ca3f2008-09-12 11:24:11 +020091 goto out_free;
Tejun Heoeb361652007-06-14 03:45:16 +090092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Tejun Heo93e3cd82007-06-14 03:45:13 +090094 pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 *off = offs + count;
97
Nick Pigginb31ca3f2008-09-12 11:24:11 +020098 out_free:
99 kfree(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return count;
101}
102
103static int
104flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count)
105{
Tejun Heo3e519032007-06-14 03:45:15 +0900106 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900107 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
108 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Tejun Heo0ab66082007-06-14 03:45:16 +0900109 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Tejun Heo0ab66082007-06-14 03:45:16 +0900111 /* need attr_sd for attr, its parent for kobj */
112 if (!sysfs_get_active_two(attr_sd))
113 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Tejun Heo0ab66082007-06-14 03:45:16 +0900115 rc = -EIO;
116 if (attr->write)
Zhang Rui91a69022007-06-09 13:57:22 +0800117 rc = attr->write(kobj, attr, buffer, offset, count);
Tejun Heo0ab66082007-06-14 03:45:16 +0900118
119 sysfs_put_active_two(attr_sd);
120
121 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Tejun Heo93e3cd82007-06-14 03:45:13 +0900124static ssize_t write(struct file *file, const char __user *userbuf,
125 size_t bytes, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Tejun Heoeb361652007-06-14 03:45:16 +0900127 struct bin_buffer *bb = file->private_data;
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800128 struct dentry *dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 int size = dentry->d_inode->i_size;
130 loff_t offs = *off;
Tejun Heo93e3cd82007-06-14 03:45:13 +0900131 int count = min_t(size_t, bytes, PAGE_SIZE);
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200132 char *temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (size) {
135 if (offs > size)
136 return 0;
137 if (offs + count > size)
138 count = size - offs;
139 }
140
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200141 temp = kmalloc(count, GFP_KERNEL);
142 if (!temp)
143 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200145 if (copy_from_user(temp, userbuf, count)) {
Tejun Heoeb361652007-06-14 03:45:16 +0900146 count = -EFAULT;
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200147 goto out_free;
Tejun Heoeb361652007-06-14 03:45:16 +0900148 }
149
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200150 mutex_lock(&bb->mutex);
151
152 memcpy(bb->buffer, temp, count);
153
Tejun Heoeb361652007-06-14 03:45:16 +0900154 count = flush_write(dentry, bb->buffer, offs, count);
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200155 mutex_unlock(&bb->mutex);
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (count > 0)
158 *off = offs + count;
Tejun Heoeb361652007-06-14 03:45:16 +0900159
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200160out_free:
161 kfree(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return count;
163}
164
165static int mmap(struct file *file, struct vm_area_struct *vma)
166{
Tejun Heoeb361652007-06-14 03:45:16 +0900167 struct bin_buffer *bb = file->private_data;
Tejun Heo3e519032007-06-14 03:45:15 +0900168 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900169 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
170 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Tejun Heoeb361652007-06-14 03:45:16 +0900171 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Tejun Heoeb361652007-06-14 03:45:16 +0900173 mutex_lock(&bb->mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900174
175 /* need attr_sd for attr, its parent for kobj */
176 if (!sysfs_get_active_two(attr_sd))
177 return -ENODEV;
178
179 rc = -EINVAL;
180 if (attr->mmap)
181 rc = attr->mmap(kobj, attr, vma);
182
183 if (rc == 0 && !bb->mmapped)
184 bb->mmapped = 1;
185 else
186 sysfs_put_active_two(attr_sd);
187
Tejun Heoeb361652007-06-14 03:45:16 +0900188 mutex_unlock(&bb->mutex);
189
190 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
193static int open(struct inode * inode, struct file * file)
194{
Tejun Heo3e519032007-06-14 03:45:15 +0900195 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900196 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
Tejun Heoeb361652007-06-14 03:45:16 +0900197 struct bin_buffer *bb = NULL;
Tejun Heo0ab66082007-06-14 03:45:16 +0900198 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Tejun Heo078ce642007-09-20 16:05:10 +0900200 /* binary file operations requires both @sd and its parent */
201 if (!sysfs_get_active_two(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900202 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 error = -EACCES;
205 if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap))
Tejun Heo7b595752007-06-14 03:45:17 +0900206 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap))
Tejun Heo7b595752007-06-14 03:45:17 +0900208 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 error = -ENOMEM;
Tejun Heoeb361652007-06-14 03:45:16 +0900211 bb = kzalloc(sizeof(*bb), GFP_KERNEL);
212 if (!bb)
Tejun Heo7b595752007-06-14 03:45:17 +0900213 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Tejun Heoeb361652007-06-14 03:45:16 +0900215 bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
216 if (!bb->buffer)
Tejun Heo7b595752007-06-14 03:45:17 +0900217 goto err_out;
Tejun Heoeb361652007-06-14 03:45:16 +0900218
219 mutex_init(&bb->mutex);
220 file->private_data = bb;
221
Tejun Heo078ce642007-09-20 16:05:10 +0900222 /* open succeeded, put active references */
223 sysfs_put_active_two(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900224 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Tejun Heo7b595752007-06-14 03:45:17 +0900226 err_out:
Tejun Heo078ce642007-09-20 16:05:10 +0900227 sysfs_put_active_two(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900228 kfree(bb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return error;
230}
231
232static int release(struct inode * inode, struct file * file)
233{
Tejun Heo3e519032007-06-14 03:45:15 +0900234 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heoeb361652007-06-14 03:45:16 +0900235 struct bin_buffer *bb = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Tejun Heo0ab66082007-06-14 03:45:16 +0900237 if (bb->mmapped)
238 sysfs_put_active_two(attr_sd);
Tejun Heoeb361652007-06-14 03:45:16 +0900239 kfree(bb->buffer);
240 kfree(bb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return 0;
242}
243
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800244const struct file_operations bin_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 .read = read,
246 .write = write,
247 .mmap = mmap,
248 .llseek = generic_file_llseek,
249 .open = open,
250 .release = release,
251};
252
253/**
254 * sysfs_create_bin_file - create binary file for object.
255 * @kobj: object.
256 * @attr: attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 */
258
259int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr)
260{
Tejun Heo608e2662007-06-14 04:27:22 +0900261 BUG_ON(!kobj || !kobj->sd || !attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Tejun Heo608e2662007-06-14 04:27:22 +0900263 return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266
267/**
268 * sysfs_remove_bin_file - remove binary file for object.
269 * @kobj: object.
270 * @attr: attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 */
272
Randy.Dunlap995982c2006-07-10 23:05:25 -0700273void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Alan Stern5f1835d2007-08-16 16:13:06 -0400275 sysfs_hash_and_remove(kobj->sd, attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
279EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);