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>
26 #include <asm/uaccess.h>
31 * There's one bin_buffer for each open file.
33 * filp->private_data points to bin_buffer and
34 * sysfs_dirent->s_bin_attr.buffers points to a the bin_buffer s
35 * sysfs_dirent->s_bin_attr.buffers is protected by sysfs_bin_lock
37 static DEFINE_MUTEX(sysfs_bin_lock);
43 struct vm_operations_struct *vm_ops;
45 struct hlist_node list;
49 fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count)
51 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
52 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
53 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
56 /* need attr_sd for attr, its parent for kobj */
57 if (!sysfs_get_active_two(attr_sd))
62 rc = attr->read(kobj, attr, buffer, off, count);
64 sysfs_put_active_two(attr_sd);
70 read(struct file *file, char __user *userbuf, size_t bytes, loff_t *off)
72 struct bin_buffer *bb = file->private_data;
73 struct dentry *dentry = file->f_path.dentry;
74 int size = dentry->d_inode->i_size;
76 int count = min_t(size_t, bytes, PAGE_SIZE);
85 if (offs + count > size)
89 temp = kmalloc(count, GFP_KERNEL);
93 mutex_lock(&bb->mutex);
95 count = fill_read(dentry, bb->buffer, offs, count);
97 mutex_unlock(&bb->mutex);
101 memcpy(temp, bb->buffer, count);
103 mutex_unlock(&bb->mutex);
105 if (copy_to_user(userbuf, temp, count)) {
110 pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
120 flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count)
122 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
123 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
124 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
127 /* need attr_sd for attr, its parent for kobj */
128 if (!sysfs_get_active_two(attr_sd))
133 rc = attr->write(kobj, attr, buffer, offset, count);
135 sysfs_put_active_two(attr_sd);
140 static ssize_t write(struct file *file, const char __user *userbuf,
141 size_t bytes, loff_t *off)
143 struct bin_buffer *bb = file->private_data;
144 struct dentry *dentry = file->f_path.dentry;
145 int size = dentry->d_inode->i_size;
147 int count = min_t(size_t, bytes, PAGE_SIZE);
156 if (offs + count > size)
160 temp = kmalloc(count, GFP_KERNEL);
164 if (copy_from_user(temp, userbuf, count)) {
169 mutex_lock(&bb->mutex);
171 memcpy(bb->buffer, temp, count);
173 count = flush_write(dentry, bb->buffer, offs, count);
174 mutex_unlock(&bb->mutex);
184 static void bin_vma_open(struct vm_area_struct *vma)
186 struct file *file = vma->vm_file;
187 struct bin_buffer *bb = file->private_data;
188 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
190 if (!bb->vm_ops || !bb->vm_ops->open)
193 if (!sysfs_get_active_two(attr_sd))
196 bb->vm_ops->open(vma);
198 sysfs_put_active_two(attr_sd);
201 static void bin_vma_close(struct vm_area_struct *vma)
203 struct file *file = vma->vm_file;
204 struct bin_buffer *bb = file->private_data;
205 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
207 if (!bb->vm_ops || !bb->vm_ops->close)
210 if (!sysfs_get_active_two(attr_sd))
213 bb->vm_ops->close(vma);
215 sysfs_put_active_two(attr_sd);
218 static int bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
220 struct file *file = vma->vm_file;
221 struct bin_buffer *bb = file->private_data;
222 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
225 if (!bb->vm_ops || !bb->vm_ops->fault)
226 return VM_FAULT_SIGBUS;
228 if (!sysfs_get_active_two(attr_sd))
229 return VM_FAULT_SIGBUS;
231 ret = bb->vm_ops->fault(vma, vmf);
233 sysfs_put_active_two(attr_sd);
237 static int bin_page_mkwrite(struct vm_area_struct *vma, struct page *page)
239 struct file *file = vma->vm_file;
240 struct bin_buffer *bb = file->private_data;
241 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
244 if (!bb->vm_ops || !bb->vm_ops->page_mkwrite)
247 if (!sysfs_get_active_two(attr_sd))
250 ret = bb->vm_ops->page_mkwrite(vma, page);
252 sysfs_put_active_two(attr_sd);
256 static int bin_access(struct vm_area_struct *vma, unsigned long addr,
257 void *buf, int len, int write)
259 struct file *file = vma->vm_file;
260 struct bin_buffer *bb = file->private_data;
261 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
264 if (!bb->vm_ops || !bb->vm_ops->access)
267 if (!sysfs_get_active_two(attr_sd))
270 ret = bb->vm_ops->access(vma, addr, buf, len, write);
272 sysfs_put_active_two(attr_sd);
276 static struct vm_operations_struct bin_vm_ops = {
277 .open = bin_vma_open,
278 .close = bin_vma_close,
280 .page_mkwrite = bin_page_mkwrite,
281 .access = bin_access,
284 static int mmap(struct file *file, struct vm_area_struct *vma)
286 struct bin_buffer *bb = file->private_data;
287 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
288 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
289 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
290 struct vm_operations_struct *vm_ops;
293 mutex_lock(&bb->mutex);
295 /* need attr_sd for attr, its parent for kobj */
297 if (!sysfs_get_active_two(attr_sd))
304 rc = attr->mmap(kobj, attr, vma);
305 vm_ops = vma->vm_ops;
306 vma->vm_ops = &bin_vm_ops;
311 if (bb->mmapped && bb->vm_ops != vma->vm_ops)
316 if (vm_ops && ((vm_ops->set_policy || vm_ops->get_policy || vm_ops->migrate)))
324 sysfs_put_active_two(attr_sd);
326 mutex_unlock(&bb->mutex);
331 static int open(struct inode * inode, struct file * file)
333 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
334 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
335 struct bin_buffer *bb = NULL;
338 /* binary file operations requires both @sd and its parent */
339 if (!sysfs_get_active_two(attr_sd))
343 if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap))
345 if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap))
349 bb = kzalloc(sizeof(*bb), GFP_KERNEL);
353 bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
357 mutex_init(&bb->mutex);
359 file->private_data = bb;
361 mutex_lock(&sysfs_bin_lock);
362 hlist_add_head(&bb->list, &attr_sd->s_bin_attr.buffers);
363 mutex_unlock(&sysfs_bin_lock);
365 /* open succeeded, put active references */
366 sysfs_put_active_two(attr_sd);
370 sysfs_put_active_two(attr_sd);
375 static int release(struct inode * inode, struct file * file)
377 struct bin_buffer *bb = file->private_data;
379 mutex_lock(&sysfs_bin_lock);
380 hlist_del(&bb->list);
381 mutex_unlock(&sysfs_bin_lock);
388 const struct file_operations bin_fops = {
392 .llseek = generic_file_llseek,
398 void unmap_bin_file(struct sysfs_dirent *attr_sd)
400 struct bin_buffer *bb;
401 struct hlist_node *tmp;
403 if (sysfs_type(attr_sd) != SYSFS_KOBJ_BIN_ATTR)
406 mutex_lock(&sysfs_bin_lock);
408 hlist_for_each_entry(bb, tmp, &attr_sd->s_bin_attr.buffers, list) {
409 struct inode *inode = bb->file->f_path.dentry->d_inode;
411 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
414 mutex_unlock(&sysfs_bin_lock);
418 * sysfs_create_bin_file - create binary file for object.
420 * @attr: attribute descriptor.
423 int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr)
425 BUG_ON(!kobj || !kobj->sd || !attr);
427 return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
432 * sysfs_remove_bin_file - remove binary file for object.
434 * @attr: attribute descriptor.
437 void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
439 sysfs_hash_and_remove(kobj->sd, attr->attr.name);
442 EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
443 EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);