2 * bin.c - binary file operations for sysfs.
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Matthew Wilcox
6 * Copyright (c) 2004 Silicon Graphics, Inc.
11 #include <linux/errno.h>
13 #include <linux/kernel.h>
14 #include <linux/kobject.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
18 #include <asm/uaccess.h>
19 #include <asm/semaphore.h>
30 fill_read(struct dentry
*dentry
, char *buffer
, loff_t off
, size_t count
)
32 struct sysfs_dirent
*attr_sd
= dentry
->d_fsdata
;
33 struct bin_attribute
*attr
= attr_sd
->s_elem
.bin_attr
.bin_attr
;
34 struct kobject
*kobj
= attr_sd
->s_parent
->s_elem
.dir
.kobj
;
37 /* need attr_sd for attr, its parent for kobj */
38 if (!sysfs_get_active_two(attr_sd
))
43 rc
= attr
->read(kobj
, attr
, buffer
, off
, count
);
45 sysfs_put_active_two(attr_sd
);
51 read(struct file
*file
, char __user
*userbuf
, size_t bytes
, loff_t
*off
)
53 struct bin_buffer
*bb
= file
->private_data
;
54 struct dentry
*dentry
= file
->f_path
.dentry
;
55 int size
= dentry
->d_inode
->i_size
;
57 int count
= min_t(size_t, bytes
, PAGE_SIZE
);
62 if (offs
+ count
> size
)
66 mutex_lock(&bb
->mutex
);
68 count
= fill_read(dentry
, bb
->buffer
, offs
, count
);
72 if (copy_to_user(userbuf
, bb
->buffer
, count
)) {
77 pr_debug("offs = %lld, *off = %lld, count = %d\n", offs
, *off
, count
);
82 mutex_unlock(&bb
->mutex
);
87 flush_write(struct dentry
*dentry
, char *buffer
, loff_t offset
, size_t count
)
89 struct sysfs_dirent
*attr_sd
= dentry
->d_fsdata
;
90 struct bin_attribute
*attr
= attr_sd
->s_elem
.bin_attr
.bin_attr
;
91 struct kobject
*kobj
= attr_sd
->s_parent
->s_elem
.dir
.kobj
;
94 /* need attr_sd for attr, its parent for kobj */
95 if (!sysfs_get_active_two(attr_sd
))
100 rc
= attr
->write(kobj
, attr
, buffer
, offset
, count
);
102 sysfs_put_active_two(attr_sd
);
107 static ssize_t
write(struct file
*file
, const char __user
*userbuf
,
108 size_t bytes
, loff_t
*off
)
110 struct bin_buffer
*bb
= file
->private_data
;
111 struct dentry
*dentry
= file
->f_path
.dentry
;
112 int size
= dentry
->d_inode
->i_size
;
114 int count
= min_t(size_t, bytes
, PAGE_SIZE
);
119 if (offs
+ count
> size
)
123 mutex_lock(&bb
->mutex
);
125 if (copy_from_user(bb
->buffer
, userbuf
, count
)) {
130 count
= flush_write(dentry
, bb
->buffer
, offs
, count
);
135 mutex_unlock(&bb
->mutex
);
139 static int mmap(struct file
*file
, struct vm_area_struct
*vma
)
141 struct bin_buffer
*bb
= file
->private_data
;
142 struct sysfs_dirent
*attr_sd
= file
->f_path
.dentry
->d_fsdata
;
143 struct bin_attribute
*attr
= attr_sd
->s_elem
.bin_attr
.bin_attr
;
144 struct kobject
*kobj
= attr_sd
->s_parent
->s_elem
.dir
.kobj
;
147 mutex_lock(&bb
->mutex
);
149 /* need attr_sd for attr, its parent for kobj */
150 if (!sysfs_get_active_two(attr_sd
))
155 rc
= attr
->mmap(kobj
, attr
, vma
);
157 if (rc
== 0 && !bb
->mmapped
)
160 sysfs_put_active_two(attr_sd
);
162 mutex_unlock(&bb
->mutex
);
167 static int open(struct inode
* inode
, struct file
* file
)
169 struct sysfs_dirent
*attr_sd
= file
->f_path
.dentry
->d_fsdata
;
170 struct bin_attribute
*attr
= attr_sd
->s_elem
.bin_attr
.bin_attr
;
171 struct bin_buffer
*bb
= NULL
;
174 /* need attr_sd for attr */
175 if (!sysfs_get_active(attr_sd
))
179 if ((file
->f_mode
& FMODE_WRITE
) && !(attr
->write
|| attr
->mmap
))
181 if ((file
->f_mode
& FMODE_READ
) && !(attr
->read
|| attr
->mmap
))
185 bb
= kzalloc(sizeof(*bb
), GFP_KERNEL
);
189 bb
->buffer
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
193 mutex_init(&bb
->mutex
);
194 file
->private_data
= bb
;
196 /* open succeeded, put active reference and pin attr_sd */
197 sysfs_put_active(attr_sd
);
202 sysfs_put_active(attr_sd
);
207 static int release(struct inode
* inode
, struct file
* file
)
209 struct sysfs_dirent
*attr_sd
= file
->f_path
.dentry
->d_fsdata
;
210 struct bin_buffer
*bb
= file
->private_data
;
213 sysfs_put_active_two(attr_sd
);
220 const struct file_operations bin_fops
= {
224 .llseek
= generic_file_llseek
,
230 * sysfs_create_bin_file - create binary file for object.
232 * @attr: attribute descriptor.
235 int sysfs_create_bin_file(struct kobject
* kobj
, struct bin_attribute
* attr
)
237 BUG_ON(!kobj
|| !kobj
->sd
|| !attr
);
239 return sysfs_add_file(kobj
->sd
, &attr
->attr
, SYSFS_KOBJ_BIN_ATTR
);
244 * sysfs_remove_bin_file - remove binary file for object.
246 * @attr: attribute descriptor.
249 void sysfs_remove_bin_file(struct kobject
* kobj
, struct bin_attribute
* attr
)
251 if (sysfs_hash_and_remove(kobj
->sd
, attr
->attr
.name
) < 0) {
252 printk(KERN_ERR
"%s: "
253 "bad dentry or inode or no such file: \"%s\"\n",
254 __FUNCTION__
, attr
->attr
.name
);
259 EXPORT_SYMBOL_GPL(sysfs_create_bin_file
);
260 EXPORT_SYMBOL_GPL(sysfs_remove_bin_file
);