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>
23 fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count)
25 struct bin_attribute * attr = to_bin_attr(dentry);
26 struct kobject * kobj = to_kobj(dentry->d_parent);
31 return attr->read(kobj, buffer, off, count);
35 read(struct file * file, char __user * userbuf, size_t count, loff_t * off)
37 char *buffer = file->private_data;
38 struct dentry *dentry = file->f_dentry;
39 int size = dentry->d_inode->i_size;
43 if (count > PAGE_SIZE)
49 if (offs + count > size)
53 ret = fill_read(dentry, buffer, offs, count);
58 if (copy_to_user(userbuf, buffer, count))
61 pr_debug("offs = %lld, *off = %lld, count = %zd\n", offs, *off, count);
69 flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count)
71 struct bin_attribute *attr = to_bin_attr(dentry);
72 struct kobject *kobj = to_kobj(dentry->d_parent);
77 return attr->write(kobj, buffer, offset, count);
80 static ssize_t write(struct file * file, const char __user * userbuf,
81 size_t count, loff_t * off)
83 char *buffer = file->private_data;
84 struct dentry *dentry = file->f_dentry;
85 int size = dentry->d_inode->i_size;
88 if (count > PAGE_SIZE)
93 if (offs + count > size)
97 if (copy_from_user(buffer, userbuf, count))
100 count = flush_write(dentry, buffer, offs, count);
106 static int mmap(struct file *file, struct vm_area_struct *vma)
108 struct dentry *dentry = file->f_dentry;
109 struct bin_attribute *attr = to_bin_attr(dentry);
110 struct kobject *kobj = to_kobj(dentry->d_parent);
115 return attr->mmap(kobj, attr, vma);
118 static int open(struct inode * inode, struct file * file)
120 struct kobject *kobj = sysfs_get_kobject(file->f_dentry->d_parent);
121 struct bin_attribute * attr = to_bin_attr(file->f_dentry);
127 /* Grab the module reference for this attribute if we have one */
129 if (!try_module_get(attr->attr.owner))
133 if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap))
135 if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap))
139 file->private_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
140 if (!file->private_data)
147 module_put(attr->attr.owner);
154 static int release(struct inode * inode, struct file * file)
156 struct kobject * kobj = to_kobj(file->f_dentry->d_parent);
157 struct bin_attribute * attr = to_bin_attr(file->f_dentry);
158 u8 * buffer = file->private_data;
162 module_put(attr->attr.owner);
167 const struct file_operations bin_fops = {
171 .llseek = generic_file_llseek,
177 * sysfs_create_bin_file - create binary file for object.
179 * @attr: attribute descriptor.
182 int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr)
184 BUG_ON(!kobj || !kobj->dentry || !attr);
186 return sysfs_add_file(kobj->dentry, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
191 * sysfs_remove_bin_file - remove binary file for object.
193 * @attr: attribute descriptor.
196 void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
198 if (sysfs_hash_and_remove(kobj->dentry, attr->attr.name) < 0) {
199 printk(KERN_ERR "%s: "
200 "bad dentry or inode or no such file: \"%s\"\n",
201 __FUNCTION__, attr->attr.name);
206 EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
207 EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);