f2c478c3424e6f40096049206c4b3e181f37d211
2 * fs/sysfs/bin.c - sysfs binary file implementation
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Matthew Wilcox
6 * Copyright (c) 2004 Silicon Graphics, Inc.
7 * Copyright (c) 2007 SUSE Linux Products GmbH
8 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
10 * This file is released under the GPLv2.
12 * Please see Documentation/filesystems/sysfs.txt for more information.
17 #include <linux/errno.h>
19 #include <linux/kernel.h>
20 #include <linux/kobject.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/mutex.h>
25 #include <asm/uaccess.h>
36 fill_read(struct dentry
*dentry
, char *buffer
, loff_t off
, size_t count
)
38 struct sysfs_dirent
*attr_sd
= dentry
->d_fsdata
;
39 struct bin_attribute
*attr
= attr_sd
->s_bin_attr
.bin_attr
;
40 struct kobject
*kobj
= attr_sd
->s_parent
->s_dir
.kobj
;
43 /* need attr_sd for attr, its parent for kobj */
44 if (!sysfs_get_active_two(attr_sd
))
49 rc
= attr
->read(kobj
, attr
, buffer
, off
, count
);
51 sysfs_put_active_two(attr_sd
);
57 read(struct file
*file
, char __user
*userbuf
, size_t bytes
, loff_t
*off
)
59 struct bin_buffer
*bb
= file
->private_data
;
60 struct dentry
*dentry
= file
->f_path
.dentry
;
61 int size
= dentry
->d_inode
->i_size
;
63 int count
= min_t(size_t, bytes
, PAGE_SIZE
);
72 if (offs
+ count
> size
)
76 temp
= kmalloc(count
, GFP_KERNEL
);
80 mutex_lock(&bb
->mutex
);
82 count
= fill_read(dentry
, bb
->buffer
, offs
, count
);
84 mutex_unlock(&bb
->mutex
);
88 memcpy(temp
, bb
->buffer
, count
);
90 mutex_unlock(&bb
->mutex
);
92 if (copy_to_user(userbuf
, temp
, count
)) {
97 pr_debug("offs = %lld, *off = %lld, count = %d\n", offs
, *off
, count
);
107 flush_write(struct dentry
*dentry
, char *buffer
, loff_t offset
, size_t count
)
109 struct sysfs_dirent
*attr_sd
= dentry
->d_fsdata
;
110 struct bin_attribute
*attr
= attr_sd
->s_bin_attr
.bin_attr
;
111 struct kobject
*kobj
= attr_sd
->s_parent
->s_dir
.kobj
;
114 /* need attr_sd for attr, its parent for kobj */
115 if (!sysfs_get_active_two(attr_sd
))
120 rc
= attr
->write(kobj
, attr
, buffer
, offset
, count
);
122 sysfs_put_active_two(attr_sd
);
127 static ssize_t
write(struct file
*file
, const char __user
*userbuf
,
128 size_t bytes
, loff_t
*off
)
130 struct bin_buffer
*bb
= file
->private_data
;
131 struct dentry
*dentry
= file
->f_path
.dentry
;
132 int size
= dentry
->d_inode
->i_size
;
134 int count
= min_t(size_t, bytes
, PAGE_SIZE
);
143 if (offs
+ count
> size
)
147 temp
= kmalloc(count
, GFP_KERNEL
);
151 if (copy_from_user(temp
, userbuf
, count
)) {
156 mutex_lock(&bb
->mutex
);
158 memcpy(bb
->buffer
, temp
, count
);
160 count
= flush_write(dentry
, bb
->buffer
, offs
, count
);
161 mutex_unlock(&bb
->mutex
);
171 static int mmap(struct file
*file
, struct vm_area_struct
*vma
)
173 struct bin_buffer
*bb
= file
->private_data
;
174 struct sysfs_dirent
*attr_sd
= file
->f_path
.dentry
->d_fsdata
;
175 struct bin_attribute
*attr
= attr_sd
->s_bin_attr
.bin_attr
;
176 struct kobject
*kobj
= attr_sd
->s_parent
->s_dir
.kobj
;
179 mutex_lock(&bb
->mutex
);
181 /* need attr_sd for attr, its parent for kobj */
182 if (!sysfs_get_active_two(attr_sd
))
187 rc
= attr
->mmap(kobj
, attr
, vma
);
189 if (rc
== 0 && !bb
->mmapped
)
192 sysfs_put_active_two(attr_sd
);
194 mutex_unlock(&bb
->mutex
);
199 static int open(struct inode
* inode
, struct file
* file
)
201 struct sysfs_dirent
*attr_sd
= file
->f_path
.dentry
->d_fsdata
;
202 struct bin_attribute
*attr
= attr_sd
->s_bin_attr
.bin_attr
;
203 struct bin_buffer
*bb
= NULL
;
206 /* binary file operations requires both @sd and its parent */
207 if (!sysfs_get_active_two(attr_sd
))
211 if ((file
->f_mode
& FMODE_WRITE
) && !(attr
->write
|| attr
->mmap
))
213 if ((file
->f_mode
& FMODE_READ
) && !(attr
->read
|| attr
->mmap
))
217 bb
= kzalloc(sizeof(*bb
), GFP_KERNEL
);
221 bb
->buffer
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
225 mutex_init(&bb
->mutex
);
226 file
->private_data
= bb
;
228 /* open succeeded, put active references */
229 sysfs_put_active_two(attr_sd
);
233 sysfs_put_active_two(attr_sd
);
238 static int release(struct inode
* inode
, struct file
* file
)
240 struct sysfs_dirent
*attr_sd
= file
->f_path
.dentry
->d_fsdata
;
241 struct bin_buffer
*bb
= file
->private_data
;
244 sysfs_put_active_two(attr_sd
);
250 const struct file_operations bin_fops
= {
254 .llseek
= generic_file_llseek
,
260 * sysfs_create_bin_file - create binary file for object.
262 * @attr: attribute descriptor.
265 int sysfs_create_bin_file(struct kobject
* kobj
, struct bin_attribute
* attr
)
267 BUG_ON(!kobj
|| !kobj
->sd
|| !attr
);
269 return sysfs_add_file(kobj
->sd
, &attr
->attr
, SYSFS_KOBJ_BIN_ATTR
);
274 * sysfs_remove_bin_file - remove binary file for object.
276 * @attr: attribute descriptor.
279 void sysfs_remove_bin_file(struct kobject
* kobj
, struct bin_attribute
* attr
)
281 sysfs_hash_and_remove(kobj
->sd
, attr
->attr
.name
);
284 EXPORT_SYMBOL_GPL(sysfs_create_bin_file
);
285 EXPORT_SYMBOL_GPL(sysfs_remove_bin_file
);
This page took 0.136531 seconds and 4 git commands to generate.