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>
24 fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count)
26 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
27 struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr;
28 struct kobject * kobj = to_kobj(dentry->d_parent);
33 return attr->read(kobj, buffer, off, count);
37 read(struct file *file, char __user *userbuf, size_t bytes, loff_t *off)
39 char *buffer = file->private_data;
40 struct dentry *dentry = file->f_path.dentry;
41 int size = dentry->d_inode->i_size;
43 int count = min_t(size_t, bytes, PAGE_SIZE);
48 if (offs + count > size)
52 count = fill_read(dentry, buffer, offs, count);
56 if (copy_to_user(userbuf, buffer, count))
59 pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
67 flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count)
69 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
70 struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr;
71 struct kobject *kobj = to_kobj(dentry->d_parent);
76 return attr->write(kobj, buffer, offset, count);
79 static ssize_t write(struct file *file, const char __user *userbuf,
80 size_t bytes, loff_t *off)
82 char *buffer = file->private_data;
83 struct dentry *dentry = file->f_path.dentry;
84 int size = dentry->d_inode->i_size;
86 int count = min_t(size_t, bytes, PAGE_SIZE);
91 if (offs + count > size)
95 if (copy_from_user(buffer, userbuf, count))
98 count = flush_write(dentry, buffer, offs, count);
104 static int mmap(struct file *file, struct vm_area_struct *vma)
106 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
107 struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr;
108 struct kobject *kobj = to_kobj(file->f_path.dentry->d_parent);
113 return attr->mmap(kobj, attr, vma);
116 static int open(struct inode * inode, struct file * file)
118 struct kobject *kobj = sysfs_get_kobject(file->f_path.dentry->d_parent);
119 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
120 struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr;
126 /* Grab the module reference for this attribute if we have one */
128 if (!try_module_get(attr->attr.owner))
132 if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap))
134 if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap))
138 file->private_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
139 if (!file->private_data)
146 module_put(attr->attr.owner);
153 static int release(struct inode * inode, struct file * file)
155 struct kobject * kobj = to_kobj(file->f_path.dentry->d_parent);
156 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
157 struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr;
158 u8 * buffer = file->private_data;
161 module_put(attr->attr.owner);
166 const struct file_operations bin_fops = {
170 .llseek = generic_file_llseek,
176 * sysfs_create_bin_file - create binary file for object.
178 * @attr: attribute descriptor.
181 int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr)
183 BUG_ON(!kobj || !kobj->dentry || !attr);
185 return sysfs_add_file(kobj->dentry, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
190 * sysfs_remove_bin_file - remove binary file for object.
192 * @attr: attribute descriptor.
195 void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
197 if (sysfs_hash_and_remove(kobj->dentry, attr->attr.name) < 0) {
198 printk(KERN_ERR "%s: "
199 "bad dentry or inode or no such file: \"%s\"\n",
200 __FUNCTION__, attr->attr.name);
205 EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
206 EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);